hid-sony.c 7.1 KB

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