hid-sony.c 15 KB

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