hid-sony.c 6.6 KB

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