hid-sony.c 8.2 KB

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