hid-sony.c 3.9 KB

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