hid-sony.c 3.9 KB

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