linux/drivers/hid/hid-sony.c
<<
>>
Prefs
   1/*
   2 *  HID driver for some sony "special" devices
   3 *
   4 *  Copyright (c) 1999 Andreas Gal
   5 *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
   6 *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
   7 *  Copyright (c) 2008 Jiri Slaby
   8 *  Copyright (c) 2006-2008 Jiri Kosina
   9 */
  10
  11/*
  12 * This program is free software; you can redistribute it and/or modify it
  13 * under the terms of the GNU General Public License as published by the Free
  14 * Software Foundation; either version 2 of the License, or (at your option)
  15 * any later version.
  16 */
  17
  18#include <linux/device.h>
  19#include <linux/hid.h>
  20#include <linux/module.h>
  21#include <linux/slab.h>
  22#include <linux/usb.h>
  23
  24#include "hid-ids.h"
  25
  26#define VAIO_RDESC_CONSTANT     (1 << 0)
  27#define SIXAXIS_CONTROLLER_USB  (1 << 1)
  28#define SIXAXIS_CONTROLLER_BT   (1 << 2)
  29
  30static const u8 sixaxis_rdesc_fixup[] = {
  31        0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
  32        0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
  33        0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
  34};
  35
  36struct sony_sc {
  37        unsigned long quirks;
  38};
  39
  40/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
  41static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  42                unsigned int *rsize)
  43{
  44        struct sony_sc *sc = hid_get_drvdata(hdev);
  45
  46        /*
  47         * Some Sony RF receivers wrongly declare the mouse pointer as a
  48         * a constant non-data variable.
  49         */
  50        if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
  51            /* usage page: generic desktop controls */
  52            /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
  53            /* usage: mouse */
  54            rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
  55            /* input (usage page for x,y axes): constant, variable, relative */
  56            rdesc[54] == 0x81 && rdesc[55] == 0x07) {
  57                hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
  58                /* input: data, variable, relative */
  59                rdesc[55] = 0x06;
  60        }
  61
  62        /* The HID descriptor exposed over BT has a trailing zero byte */
  63        if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
  64                        ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
  65                        rdesc[83] == 0x75) {
  66                hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
  67                memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
  68                        sizeof(sixaxis_rdesc_fixup));
  69        }
  70        return rdesc;
  71}
  72
  73static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
  74                __u8 *rd, int size)
  75{
  76        struct sony_sc *sc = hid_get_drvdata(hdev);
  77
  78        /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
  79         * has to be BYTE_SWAPPED before passing up to joystick interface
  80         */
  81        if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
  82                        rd[0] == 0x01 && size == 49) {
  83                swap(rd[41], rd[42]);
  84                swap(rd[43], rd[44]);
  85                swap(rd[45], rd[46]);
  86                swap(rd[47], rd[48]);
  87        }
  88
  89        return 0;
  90}
  91
  92/*
  93 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
  94 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
  95 * so we need to override that forcing HID Output Reports on the Control EP.
  96 *
  97 * There is also another issue about HID Output Reports via USB, the Sixaxis
  98 * does not want the report_id as part of the data packet, so we have to
  99 * discard buf[0] when sending the actual control message, even for numbered
 100 * reports, humpf!
 101 */
 102static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
 103                size_t count, unsigned char report_type)
 104{
 105        struct usb_interface *intf = to_usb_interface(hid->dev.parent);
 106        struct usb_device *dev = interface_to_usbdev(intf);
 107        struct usb_host_interface *interface = intf->cur_altsetting;
 108        int report_id = buf[0];
 109        int ret;
 110
 111        if (report_type == HID_OUTPUT_REPORT) {
 112                /* Don't send the Report ID */
 113                buf++;
 114                count--;
 115        }
 116
 117        ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
 118                HID_REQ_SET_REPORT,
 119                USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 120                ((report_type + 1) << 8) | report_id,
 121                interface->desc.bInterfaceNumber, buf, count,
 122                USB_CTRL_SET_TIMEOUT);
 123
 124        /* Count also the Report ID, in case of an Output report. */
 125        if (ret > 0 && report_type == HID_OUTPUT_REPORT)
 126                ret++;
 127
 128        return ret;
 129}
 130
 131/*
 132 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
 133 * to "operational".  Without this, the ps3 controller will not report any
 134 * events.
 135 */
 136static int sixaxis_set_operational_usb(struct hid_device *hdev)
 137{
 138        struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
 139        struct usb_device *dev = interface_to_usbdev(intf);
 140        __u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
 141        int ret;
 142        char *buf = kmalloc(18, GFP_KERNEL);
 143
 144        if (!buf)
 145                return -ENOMEM;
 146
 147        ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
 148                                 HID_REQ_GET_REPORT,
 149                                 USB_DIR_IN | USB_TYPE_CLASS |
 150                                 USB_RECIP_INTERFACE,
 151                                 (3 << 8) | 0xf2, ifnum, buf, 17,
 152                                 USB_CTRL_GET_TIMEOUT);
 153        if (ret < 0)
 154                hid_err(hdev, "can't set operational mode\n");
 155
 156        kfree(buf);
 157
 158        return ret;
 159}
 160
 161static int sixaxis_set_operational_bt(struct hid_device *hdev)
 162{
 163        unsigned char buf[] = { 0xf4,  0x42, 0x03, 0x00, 0x00 };
 164        return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
 165}
 166
 167static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
 168{
 169        int ret;
 170        unsigned long quirks = id->driver_data;
 171        struct sony_sc *sc;
 172
 173        sc = kzalloc(sizeof(*sc), GFP_KERNEL);
 174        if (sc == NULL) {
 175                hid_err(hdev, "can't alloc sony descriptor\n");
 176                return -ENOMEM;
 177        }
 178
 179        sc->quirks = quirks;
 180        hid_set_drvdata(hdev, sc);
 181
 182        ret = hid_parse(hdev);
 183        if (ret) {
 184                hid_err(hdev, "parse failed\n");
 185                goto err_free;
 186        }
 187
 188        ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
 189                        HID_CONNECT_HIDDEV_FORCE);
 190        if (ret) {
 191                hid_err(hdev, "hw start failed\n");
 192                goto err_free;
 193        }
 194
 195        if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
 196                hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
 197                ret = sixaxis_set_operational_usb(hdev);
 198        }
 199        else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
 200                ret = sixaxis_set_operational_bt(hdev);
 201        else
 202                ret = 0;
 203
 204        if (ret < 0)
 205                goto err_stop;
 206
 207        return 0;
 208err_stop:
 209        hid_hw_stop(hdev);
 210err_free:
 211        kfree(sc);
 212        return ret;
 213}
 214
 215static void sony_remove(struct hid_device *hdev)
 216{
 217        hid_hw_stop(hdev);
 218        kfree(hid_get_drvdata(hdev));
 219}
 220
 221static const struct hid_device_id sony_devices[] = {
 222        { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
 223                .driver_data = SIXAXIS_CONTROLLER_USB },
 224        { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
 225                .driver_data = SIXAXIS_CONTROLLER_USB },
 226        { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
 227                .driver_data = SIXAXIS_CONTROLLER_BT },
 228        { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
 229                .driver_data = VAIO_RDESC_CONSTANT },
 230        { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
 231                .driver_data = VAIO_RDESC_CONSTANT },
 232        { }
 233};
 234MODULE_DEVICE_TABLE(hid, sony_devices);
 235
 236static struct hid_driver sony_driver = {
 237        .name = "sony",
 238        .id_table = sony_devices,
 239        .probe = sony_probe,
 240        .remove = sony_remove,
 241        .report_fixup = sony_report_fixup,
 242        .raw_event = sony_raw_event
 243};
 244
 245static int __init sony_init(void)
 246{
 247        return hid_register_driver(&sony_driver);
 248}
 249
 250static void __exit sony_exit(void)
 251{
 252        hid_unregister_driver(&sony_driver);
 253}
 254
 255module_init(sony_init);
 256module_exit(sony_exit);
 257MODULE_LICENSE("GPL");
 258
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.