Linux-libre 3.10.98-gnu
[librecmc/linux-libre.git] / drivers / hid / hid-sony.c
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
30 static 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
36 static const u8 sixaxis_rdesc_fixup2[] = {
37         0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
38         0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
39         0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
40         0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
41         0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
42         0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
43         0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
44         0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
45         0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
46         0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
47         0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
48         0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
49         0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
50         0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
51         0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
52         0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
53         0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
54         0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
55         0xb1, 0x02, 0xc0, 0xc0,
56 };
57
58 struct sony_sc {
59         unsigned long quirks;
60 };
61
62 /* Sony Vaio VGX has wrongly mouse pointer declared as constant */
63 static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
64                 unsigned int *rsize)
65 {
66         struct sony_sc *sc = hid_get_drvdata(hdev);
67
68         /*
69          * Some Sony RF receivers wrongly declare the mouse pointer as a
70          * a constant non-data variable.
71          */
72         if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
73             /* usage page: generic desktop controls */
74             /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
75             /* usage: mouse */
76             rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
77             /* input (usage page for x,y axes): constant, variable, relative */
78             rdesc[54] == 0x81 && rdesc[55] == 0x07) {
79                 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
80                 /* input: data, variable, relative */
81                 rdesc[55] = 0x06;
82         }
83
84         /* The HID descriptor exposed over BT has a trailing zero byte */
85         if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
86                         ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
87                         rdesc[83] == 0x75) {
88                 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
89                 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
90                         sizeof(sixaxis_rdesc_fixup));
91         } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
92                    *rsize > sizeof(sixaxis_rdesc_fixup2)) {
93                 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
94                          *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
95                 *rsize = sizeof(sixaxis_rdesc_fixup2);
96                 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
97         }
98         return rdesc;
99 }
100
101 static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
102                 __u8 *rd, int size)
103 {
104         struct sony_sc *sc = hid_get_drvdata(hdev);
105
106         /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
107          * has to be BYTE_SWAPPED before passing up to joystick interface
108          */
109         if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
110                         rd[0] == 0x01 && size == 49) {
111                 swap(rd[41], rd[42]);
112                 swap(rd[43], rd[44]);
113                 swap(rd[45], rd[46]);
114                 swap(rd[47], rd[48]);
115         }
116
117         return 0;
118 }
119
120 /*
121  * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
122  * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
123  * so we need to override that forcing HID Output Reports on the Control EP.
124  *
125  * There is also another issue about HID Output Reports via USB, the Sixaxis
126  * does not want the report_id as part of the data packet, so we have to
127  * discard buf[0] when sending the actual control message, even for numbered
128  * reports, humpf!
129  */
130 static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
131                 size_t count, unsigned char report_type)
132 {
133         struct usb_interface *intf = to_usb_interface(hid->dev.parent);
134         struct usb_device *dev = interface_to_usbdev(intf);
135         struct usb_host_interface *interface = intf->cur_altsetting;
136         int report_id = buf[0];
137         int ret;
138
139         if (report_type == HID_OUTPUT_REPORT) {
140                 /* Don't send the Report ID */
141                 buf++;
142                 count--;
143         }
144
145         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
146                 HID_REQ_SET_REPORT,
147                 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
148                 ((report_type + 1) << 8) | report_id,
149                 interface->desc.bInterfaceNumber, buf, count,
150                 USB_CTRL_SET_TIMEOUT);
151
152         /* Count also the Report ID, in case of an Output report. */
153         if (ret > 0 && report_type == HID_OUTPUT_REPORT)
154                 ret++;
155
156         return ret;
157 }
158
159 /*
160  * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
161  * to "operational".  Without this, the ps3 controller will not report any
162  * events.
163  */
164 static int sixaxis_set_operational_usb(struct hid_device *hdev)
165 {
166         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
167         struct usb_device *dev = interface_to_usbdev(intf);
168         __u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
169         int ret;
170         char *buf = kmalloc(18, GFP_KERNEL);
171
172         if (!buf)
173                 return -ENOMEM;
174
175         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
176                                  HID_REQ_GET_REPORT,
177                                  USB_DIR_IN | USB_TYPE_CLASS |
178                                  USB_RECIP_INTERFACE,
179                                  (3 << 8) | 0xf2, ifnum, buf, 17,
180                                  USB_CTRL_GET_TIMEOUT);
181         if (ret < 0)
182                 hid_err(hdev, "can't set operational mode\n");
183
184         kfree(buf);
185
186         return ret;
187 }
188
189 static int sixaxis_set_operational_bt(struct hid_device *hdev)
190 {
191         unsigned char buf[] = { 0xf4,  0x42, 0x03, 0x00, 0x00 };
192         return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
193 }
194
195 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
196 {
197         int ret;
198         unsigned long quirks = id->driver_data;
199         struct sony_sc *sc;
200
201         sc = kzalloc(sizeof(*sc), GFP_KERNEL);
202         if (sc == NULL) {
203                 hid_err(hdev, "can't alloc sony descriptor\n");
204                 return -ENOMEM;
205         }
206
207         sc->quirks = quirks;
208         hid_set_drvdata(hdev, sc);
209
210         ret = hid_parse(hdev);
211         if (ret) {
212                 hid_err(hdev, "parse failed\n");
213                 goto err_free;
214         }
215
216         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
217                         HID_CONNECT_HIDDEV_FORCE);
218         if (ret) {
219                 hid_err(hdev, "hw start failed\n");
220                 goto err_free;
221         }
222
223         if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
224                 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
225                 ret = sixaxis_set_operational_usb(hdev);
226         }
227         else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
228                 ret = sixaxis_set_operational_bt(hdev);
229         else
230                 ret = 0;
231
232         if (ret < 0)
233                 goto err_stop;
234
235         return 0;
236 err_stop:
237         hid_hw_stop(hdev);
238 err_free:
239         kfree(sc);
240         return ret;
241 }
242
243 static void sony_remove(struct hid_device *hdev)
244 {
245         hid_hw_stop(hdev);
246         kfree(hid_get_drvdata(hdev));
247 }
248
249 static const struct hid_device_id sony_devices[] = {
250         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
251                 .driver_data = SIXAXIS_CONTROLLER_USB },
252         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
253                 .driver_data = SIXAXIS_CONTROLLER_USB },
254         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
255                 .driver_data = SIXAXIS_CONTROLLER_BT },
256         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
257                 .driver_data = VAIO_RDESC_CONSTANT },
258         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
259                 .driver_data = VAIO_RDESC_CONSTANT },
260         { }
261 };
262 MODULE_DEVICE_TABLE(hid, sony_devices);
263
264 static struct hid_driver sony_driver = {
265         .name = "sony",
266         .id_table = sony_devices,
267         .probe = sony_probe,
268         .remove = sony_remove,
269         .report_fixup = sony_report_fixup,
270         .raw_event = sony_raw_event
271 };
272 module_hid_driver(sony_driver);
273
274 MODULE_LICENSE("GPL");