]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/hid/hid-sony.c
HID: sony: fix leds dependency
[mirror_ubuntu-artful-kernel.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 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
10 */
11
12 /*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19 #include <linux/device.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
24 #include <linux/leds.h>
25
26 #include "hid-ids.h"
27
28 #define VAIO_RDESC_CONSTANT (1 << 0)
29 #define SIXAXIS_CONTROLLER_USB (1 << 1)
30 #define SIXAXIS_CONTROLLER_BT (1 << 2)
31 #define BUZZ_CONTROLLER (1 << 3)
32
33 static const u8 sixaxis_rdesc_fixup[] = {
34 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
35 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
36 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
37 };
38
39 static const u8 sixaxis_rdesc_fixup2[] = {
40 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
41 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
42 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
43 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
44 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
45 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
46 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
47 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
48 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
49 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
50 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
51 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
52 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
53 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
54 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
55 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
56 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
57 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
58 0xb1, 0x02, 0xc0, 0xc0,
59 };
60
61 static const unsigned int buzz_keymap[] = {
62 /* The controller has 4 remote buzzers, each with one LED and 5
63 * buttons.
64 *
65 * We use the mapping chosen by the controller, which is:
66 *
67 * Key Offset
68 * -------------------
69 * Buzz 1
70 * Blue 5
71 * Orange 4
72 * Green 3
73 * Yellow 2
74 *
75 * So, for example, the orange button on the third buzzer is mapped to
76 * BTN_TRIGGER_HAPPY14
77 */
78 [ 1] = BTN_TRIGGER_HAPPY1,
79 [ 2] = BTN_TRIGGER_HAPPY2,
80 [ 3] = BTN_TRIGGER_HAPPY3,
81 [ 4] = BTN_TRIGGER_HAPPY4,
82 [ 5] = BTN_TRIGGER_HAPPY5,
83 [ 6] = BTN_TRIGGER_HAPPY6,
84 [ 7] = BTN_TRIGGER_HAPPY7,
85 [ 8] = BTN_TRIGGER_HAPPY8,
86 [ 9] = BTN_TRIGGER_HAPPY9,
87 [10] = BTN_TRIGGER_HAPPY10,
88 [11] = BTN_TRIGGER_HAPPY11,
89 [12] = BTN_TRIGGER_HAPPY12,
90 [13] = BTN_TRIGGER_HAPPY13,
91 [14] = BTN_TRIGGER_HAPPY14,
92 [15] = BTN_TRIGGER_HAPPY15,
93 [16] = BTN_TRIGGER_HAPPY16,
94 [17] = BTN_TRIGGER_HAPPY17,
95 [18] = BTN_TRIGGER_HAPPY18,
96 [19] = BTN_TRIGGER_HAPPY19,
97 [20] = BTN_TRIGGER_HAPPY20,
98 };
99
100 struct sony_sc {
101 unsigned long quirks;
102
103 void *extra;
104 };
105
106 struct buzz_extra {
107 int led_state;
108 struct led_classdev *leds[4];
109 };
110
111 /* Sony Vaio VGX has wrongly mouse pointer declared as constant */
112 static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
113 unsigned int *rsize)
114 {
115 struct sony_sc *sc = hid_get_drvdata(hdev);
116
117 /*
118 * Some Sony RF receivers wrongly declare the mouse pointer as a
119 * a constant non-data variable.
120 */
121 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
122 /* usage page: generic desktop controls */
123 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
124 /* usage: mouse */
125 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
126 /* input (usage page for x,y axes): constant, variable, relative */
127 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
128 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
129 /* input: data, variable, relative */
130 rdesc[55] = 0x06;
131 }
132
133 /* The HID descriptor exposed over BT has a trailing zero byte */
134 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
135 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
136 rdesc[83] == 0x75) {
137 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
138 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
139 sizeof(sixaxis_rdesc_fixup));
140 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
141 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
142 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
143 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
144 *rsize = sizeof(sixaxis_rdesc_fixup2);
145 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
146 }
147 return rdesc;
148 }
149
150 static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
151 __u8 *rd, int size)
152 {
153 struct sony_sc *sc = hid_get_drvdata(hdev);
154
155 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
156 * has to be BYTE_SWAPPED before passing up to joystick interface
157 */
158 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
159 rd[0] == 0x01 && size == 49) {
160 swap(rd[41], rd[42]);
161 swap(rd[43], rd[44]);
162 swap(rd[45], rd[46]);
163 swap(rd[47], rd[48]);
164 }
165
166 return 0;
167 }
168
169 static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
170 struct hid_field *field, struct hid_usage *usage,
171 unsigned long **bit, int *max)
172 {
173 struct sony_sc *sc = hid_get_drvdata(hdev);
174
175 if (sc->quirks & BUZZ_CONTROLLER) {
176 unsigned int key = usage->hid & HID_USAGE;
177
178 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
179 return -1;
180
181 switch (usage->collection_index) {
182 case 1:
183 if (key >= ARRAY_SIZE(buzz_keymap))
184 return -1;
185
186 key = buzz_keymap[key];
187 if (!key)
188 return -1;
189 break;
190 default:
191 return -1;
192 }
193
194 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
195 return 1;
196 }
197
198 return -1;
199 }
200
201 /*
202 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
203 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
204 * so we need to override that forcing HID Output Reports on the Control EP.
205 *
206 * There is also another issue about HID Output Reports via USB, the Sixaxis
207 * does not want the report_id as part of the data packet, so we have to
208 * discard buf[0] when sending the actual control message, even for numbered
209 * reports, humpf!
210 */
211 static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
212 size_t count, unsigned char report_type)
213 {
214 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
215 struct usb_device *dev = interface_to_usbdev(intf);
216 struct usb_host_interface *interface = intf->cur_altsetting;
217 int report_id = buf[0];
218 int ret;
219
220 if (report_type == HID_OUTPUT_REPORT) {
221 /* Don't send the Report ID */
222 buf++;
223 count--;
224 }
225
226 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
227 HID_REQ_SET_REPORT,
228 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
229 ((report_type + 1) << 8) | report_id,
230 interface->desc.bInterfaceNumber, buf, count,
231 USB_CTRL_SET_TIMEOUT);
232
233 /* Count also the Report ID, in case of an Output report. */
234 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
235 ret++;
236
237 return ret;
238 }
239
240 /*
241 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
242 * to "operational". Without this, the ps3 controller will not report any
243 * events.
244 */
245 static int sixaxis_set_operational_usb(struct hid_device *hdev)
246 {
247 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
248 struct usb_device *dev = interface_to_usbdev(intf);
249 __u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
250 int ret;
251 char *buf = kmalloc(18, GFP_KERNEL);
252
253 if (!buf)
254 return -ENOMEM;
255
256 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
257 HID_REQ_GET_REPORT,
258 USB_DIR_IN | USB_TYPE_CLASS |
259 USB_RECIP_INTERFACE,
260 (3 << 8) | 0xf2, ifnum, buf, 17,
261 USB_CTRL_GET_TIMEOUT);
262 if (ret < 0)
263 hid_err(hdev, "can't set operational mode\n");
264
265 kfree(buf);
266
267 return ret;
268 }
269
270 static int sixaxis_set_operational_bt(struct hid_device *hdev)
271 {
272 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
273 return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
274 }
275
276 static void buzz_set_leds(struct hid_device *hdev, int leds)
277 {
278 struct list_head *report_list =
279 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
280 struct hid_report *report = list_entry(report_list->next,
281 struct hid_report, list);
282 __s32 *value = report->field[0]->value;
283
284 value[0] = 0x00;
285 value[1] = (leds & 1) ? 0xff : 0x00;
286 value[2] = (leds & 2) ? 0xff : 0x00;
287 value[3] = (leds & 4) ? 0xff : 0x00;
288 value[4] = (leds & 8) ? 0xff : 0x00;
289 value[5] = 0x00;
290 value[6] = 0x00;
291 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
292 }
293
294 static void buzz_led_set_brightness(struct led_classdev *led,
295 enum led_brightness value)
296 {
297 struct device *dev = led->dev->parent;
298 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
299 struct sony_sc *drv_data;
300 struct buzz_extra *buzz;
301
302 int n;
303
304 drv_data = hid_get_drvdata(hdev);
305 if (!drv_data || !drv_data->extra) {
306 hid_err(hdev, "No device data\n");
307 return;
308 }
309 buzz = drv_data->extra;
310
311 for (n = 0; n < 4; n++) {
312 if (led == buzz->leds[n]) {
313 int on = !! (buzz->led_state & (1 << n));
314 if (value == LED_OFF && on) {
315 buzz->led_state &= ~(1 << n);
316 buzz_set_leds(hdev, buzz->led_state);
317 } else if (value != LED_OFF && !on) {
318 buzz->led_state |= (1 << n);
319 buzz_set_leds(hdev, buzz->led_state);
320 }
321 break;
322 }
323 }
324 }
325
326 static enum led_brightness buzz_led_get_brightness(struct led_classdev *led)
327 {
328 struct device *dev = led->dev->parent;
329 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
330 struct sony_sc *drv_data;
331 struct buzz_extra *buzz;
332
333 int n;
334 int on = 0;
335
336 drv_data = hid_get_drvdata(hdev);
337 if (!drv_data || !drv_data->extra) {
338 hid_err(hdev, "No device data\n");
339 return LED_OFF;
340 }
341 buzz = drv_data->extra;
342
343 for (n = 0; n < 4; n++) {
344 if (led == buzz->leds[n]) {
345 on = !! (buzz->led_state & (1 << n));
346 break;
347 }
348 }
349
350 return on ? LED_FULL : LED_OFF;
351 }
352
353 static int buzz_init(struct hid_device *hdev)
354 {
355 struct sony_sc *drv_data;
356 struct buzz_extra *buzz;
357 int n, ret = 0;
358 struct led_classdev *led;
359 size_t name_sz;
360 char *name;
361
362 drv_data = hid_get_drvdata(hdev);
363 BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
364
365 buzz = kzalloc(sizeof(*buzz), GFP_KERNEL);
366 if (!buzz) {
367 hid_err(hdev, "Insufficient memory, cannot allocate driver data\n");
368 return -ENOMEM;
369 }
370 drv_data->extra = buzz;
371
372 /* Clear LEDs as we have no way of reading their initial state. This is
373 * only relevant if the driver is loaded after somebody actively set the
374 * LEDs to on */
375 buzz_set_leds(hdev, 0x00);
376
377 name_sz = strlen(dev_name(&hdev->dev)) + strlen("::buzz#") + 1;
378
379 for (n = 0; n < 4; n++) {
380 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
381 if (!led) {
382 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
383 goto error_leds;
384 }
385
386 name = (void *)(&led[1]);
387 snprintf(name, name_sz, "%s::buzz%d", dev_name(&hdev->dev), n + 1);
388 led->name = name;
389 led->brightness = 0;
390 led->max_brightness = 1;
391 led->brightness_get = buzz_led_get_brightness;
392 led->brightness_set = buzz_led_set_brightness;
393
394 if (led_classdev_register(&hdev->dev, led)) {
395 hid_err(hdev, "Failed to register LED %d\n", n);
396 kfree(led);
397 goto error_leds;
398 }
399
400 buzz->leds[n] = led;
401 }
402
403 return ret;
404
405 error_leds:
406 for (n = 0; n < 4; n++) {
407 led = buzz->leds[n];
408 buzz->leds[n] = NULL;
409 if (!led)
410 continue;
411 led_classdev_unregister(led);
412 kfree(led);
413 }
414
415 kfree(drv_data->extra);
416 drv_data->extra = NULL;
417 return ret;
418 }
419
420 static void buzz_remove(struct hid_device *hdev)
421 {
422 struct sony_sc *drv_data;
423 struct buzz_extra *buzz;
424 struct led_classdev *led;
425 int n;
426
427 drv_data = hid_get_drvdata(hdev);
428 BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER));
429
430 buzz = drv_data->extra;
431
432 for (n = 0; n < 4; n++) {
433 led = buzz->leds[n];
434 buzz->leds[n] = NULL;
435 if (!led)
436 continue;
437 led_classdev_unregister(led);
438 kfree(led);
439 }
440
441 kfree(drv_data->extra);
442 drv_data->extra = NULL;
443 }
444
445 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
446 {
447 int ret;
448 unsigned long quirks = id->driver_data;
449 struct sony_sc *sc;
450 unsigned int connect_mask = HID_CONNECT_DEFAULT;
451
452 sc = kzalloc(sizeof(*sc), GFP_KERNEL);
453 if (sc == NULL) {
454 hid_err(hdev, "can't alloc sony descriptor\n");
455 return -ENOMEM;
456 }
457
458 sc->quirks = quirks;
459 hid_set_drvdata(hdev, sc);
460
461 ret = hid_parse(hdev);
462 if (ret) {
463 hid_err(hdev, "parse failed\n");
464 goto err_free;
465 }
466
467 if (sc->quirks & VAIO_RDESC_CONSTANT)
468 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
469 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
470 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
471 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
472 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
473
474 ret = hid_hw_start(hdev, connect_mask);
475 if (ret) {
476 hid_err(hdev, "hw start failed\n");
477 goto err_free;
478 }
479
480 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
481 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
482 ret = sixaxis_set_operational_usb(hdev);
483 }
484 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
485 ret = sixaxis_set_operational_bt(hdev);
486 else if (sc->quirks & BUZZ_CONTROLLER)
487 ret = buzz_init(hdev);
488 else
489 ret = 0;
490
491 if (ret < 0)
492 goto err_stop;
493
494 return 0;
495 err_stop:
496 hid_hw_stop(hdev);
497 err_free:
498 kfree(sc);
499 return ret;
500 }
501
502 static void sony_remove(struct hid_device *hdev)
503 {
504 struct sony_sc *sc = hid_get_drvdata(hdev);
505
506 if (sc->quirks & BUZZ_CONTROLLER)
507 buzz_remove(hdev);
508
509 hid_hw_stop(hdev);
510 kfree(sc);
511 }
512
513 static const struct hid_device_id sony_devices[] = {
514 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
515 .driver_data = SIXAXIS_CONTROLLER_USB },
516 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
517 .driver_data = SIXAXIS_CONTROLLER_USB },
518 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
519 .driver_data = SIXAXIS_CONTROLLER_BT },
520 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
521 .driver_data = VAIO_RDESC_CONSTANT },
522 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
523 .driver_data = VAIO_RDESC_CONSTANT },
524 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
525 * Logitech joystick from the device descriptor. */
526 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
527 .driver_data = BUZZ_CONTROLLER },
528 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
529 .driver_data = BUZZ_CONTROLLER },
530 { }
531 };
532 MODULE_DEVICE_TABLE(hid, sony_devices);
533
534 static struct hid_driver sony_driver = {
535 .name = "sony",
536 .id_table = sony_devices,
537 .input_mapping = sony_mapping,
538 .probe = sony_probe,
539 .remove = sony_remove,
540 .report_fixup = sony_report_fixup,
541 .raw_event = sony_raw_event
542 };
543 module_hid_driver(sony_driver);
544
545 MODULE_LICENSE("GPL");