hid-sony.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 0x0001
  24. struct sony_sc {
  25. unsigned long quirks;
  26. };
  27. /* Sony Vaio VGX has wrongly mouse pointer declared as constant */
  28. static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  29. unsigned int *rsize)
  30. {
  31. struct sony_sc *sc = hid_get_drvdata(hdev);
  32. if ((sc->quirks & VAIO_RDESC_CONSTANT) &&
  33. *rsize >= 56 && rdesc[54] == 0x81 && rdesc[55] == 0x07) {
  34. dev_info(&hdev->dev, "Fixing up Sony Vaio VGX report "
  35. "descriptor\n");
  36. rdesc[55] = 0x06;
  37. }
  38. return rdesc;
  39. }
  40. /*
  41. * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
  42. * to "operational". Without this, the ps3 controller will not report any
  43. * events.
  44. */
  45. static int sony_set_operational_usb(struct hid_device *hdev)
  46. {
  47. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  48. struct usb_device *dev = interface_to_usbdev(intf);
  49. __u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  50. int ret;
  51. char *buf = kmalloc(18, GFP_KERNEL);
  52. if (!buf)
  53. return -ENOMEM;
  54. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  55. HID_REQ_GET_REPORT,
  56. USB_DIR_IN | USB_TYPE_CLASS |
  57. USB_RECIP_INTERFACE,
  58. (3 << 8) | 0xf2, ifnum, buf, 17,
  59. USB_CTRL_GET_TIMEOUT);
  60. if (ret < 0)
  61. dev_err(&hdev->dev, "can't set operational mode\n");
  62. kfree(buf);
  63. return ret;
  64. }
  65. static int sony_set_operational_bt(struct hid_device *hdev)
  66. {
  67. unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  68. return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
  69. }
  70. static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
  71. {
  72. int ret;
  73. unsigned long quirks = id->driver_data;
  74. struct sony_sc *sc;
  75. sc = kzalloc(sizeof(*sc), GFP_KERNEL);
  76. if (sc == NULL) {
  77. dev_err(&hdev->dev, "can't alloc sony descriptor\n");
  78. return -ENOMEM;
  79. }
  80. sc->quirks = quirks;
  81. hid_set_drvdata(hdev, sc);
  82. ret = hid_parse(hdev);
  83. if (ret) {
  84. dev_err(&hdev->dev, "parse failed\n");
  85. goto err_free;
  86. }
  87. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
  88. HID_CONNECT_HIDDEV_FORCE);
  89. if (ret) {
  90. dev_err(&hdev->dev, "hw start failed\n");
  91. goto err_free;
  92. }
  93. switch (hdev->bus) {
  94. case BUS_USB:
  95. ret = sony_set_operational_usb(hdev);
  96. break;
  97. case BUS_BLUETOOTH:
  98. ret = sony_set_operational_bt(hdev);
  99. break;
  100. default:
  101. ret = 0;
  102. }
  103. if (ret < 0)
  104. goto err_stop;
  105. return 0;
  106. err_stop:
  107. hid_hw_stop(hdev);
  108. err_free:
  109. kfree(sc);
  110. return ret;
  111. }
  112. static void sony_remove(struct hid_device *hdev)
  113. {
  114. hid_hw_stop(hdev);
  115. kfree(hid_get_drvdata(hdev));
  116. }
  117. static const struct hid_device_id sony_devices[] = {
  118. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
  119. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
  120. { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
  121. .driver_data = VAIO_RDESC_CONSTANT },
  122. { }
  123. };
  124. MODULE_DEVICE_TABLE(hid, sony_devices);
  125. static struct hid_driver sony_driver = {
  126. .name = "sony",
  127. .id_table = sony_devices,
  128. .probe = sony_probe,
  129. .remove = sony_remove,
  130. .report_fixup = sony_report_fixup,
  131. };
  132. static int __init sony_init(void)
  133. {
  134. return hid_register_driver(&sony_driver);
  135. }
  136. static void __exit sony_exit(void)
  137. {
  138. hid_unregister_driver(&sony_driver);
  139. }
  140. module_init(sony_init);
  141. module_exit(sony_exit);
  142. MODULE_LICENSE("GPL");