hid-saitek.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * HID driver for Saitek devices.
  3. *
  4. * PS1000 (USB gamepad):
  5. * Fixes the HID report descriptor by removing a non-existent axis and
  6. * clearing the constant bit on the input reports for buttons and d-pad.
  7. * (This module is based on "hid-ortek".)
  8. * Copyright (c) 2012 Andreas Hübner
  9. *
  10. * R.A.T.7, R.A.T.9, M.M.O.7 (USB gaming mice):
  11. * Fixes the mode button which cycles through three constantly pressed
  12. * buttons. All three press events are mapped to one button and the
  13. * missing release event is generated immediately.
  14. *
  15. */
  16. /*
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the Free
  19. * Software Foundation; either version 2 of the License, or (at your option)
  20. * any later version.
  21. */
  22. #include <linux/device.h>
  23. #include <linux/hid.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include "hid-ids.h"
  27. #define SAITEK_FIX_PS1000 0x0001
  28. #define SAITEK_RELEASE_MODE_RAT7 0x0002
  29. #define SAITEK_RELEASE_MODE_MMO7 0x0004
  30. struct saitek_sc {
  31. unsigned long quirks;
  32. int mode;
  33. };
  34. static int saitek_probe(struct hid_device *hdev,
  35. const struct hid_device_id *id)
  36. {
  37. unsigned long quirks = id->driver_data;
  38. struct saitek_sc *ssc;
  39. int ret;
  40. ssc = devm_kzalloc(&hdev->dev, sizeof(*ssc), GFP_KERNEL);
  41. if (ssc == NULL) {
  42. hid_err(hdev, "can't alloc saitek descriptor\n");
  43. return -ENOMEM;
  44. }
  45. ssc->quirks = quirks;
  46. ssc->mode = -1;
  47. hid_set_drvdata(hdev, ssc);
  48. ret = hid_parse(hdev);
  49. if (ret) {
  50. hid_err(hdev, "parse failed\n");
  51. return ret;
  52. }
  53. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  54. if (ret) {
  55. hid_err(hdev, "hw start failed\n");
  56. return ret;
  57. }
  58. return 0;
  59. }
  60. static __u8 *saitek_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  61. unsigned int *rsize)
  62. {
  63. struct saitek_sc *ssc = hid_get_drvdata(hdev);
  64. if ((ssc->quirks & SAITEK_FIX_PS1000) && *rsize == 137 &&
  65. rdesc[20] == 0x09 && rdesc[21] == 0x33 &&
  66. rdesc[94] == 0x81 && rdesc[95] == 0x03 &&
  67. rdesc[110] == 0x81 && rdesc[111] == 0x03) {
  68. hid_info(hdev, "Fixing up Saitek PS1000 report descriptor\n");
  69. /* convert spurious axis to a "noop" Logical Minimum (0) */
  70. rdesc[20] = 0x15;
  71. rdesc[21] = 0x00;
  72. /* clear constant bit on buttons and d-pad */
  73. rdesc[95] = 0x02;
  74. rdesc[111] = 0x02;
  75. }
  76. return rdesc;
  77. }
  78. static int saitek_raw_event(struct hid_device *hdev,
  79. struct hid_report *report, u8 *raw_data, int size)
  80. {
  81. struct saitek_sc *ssc = hid_get_drvdata(hdev);
  82. if (ssc->quirks & SAITEK_RELEASE_MODE_RAT7 && size == 7) {
  83. /* R.A.T.7 uses bits 13, 14, 15 for the mode */
  84. int mode = -1;
  85. if (raw_data[1] & 0x01)
  86. mode = 0;
  87. else if (raw_data[1] & 0x02)
  88. mode = 1;
  89. else if (raw_data[1] & 0x04)
  90. mode = 2;
  91. /* clear mode bits */
  92. raw_data[1] &= ~0x07;
  93. if (mode != ssc->mode) {
  94. hid_dbg(hdev, "entered mode %d\n", mode);
  95. if (ssc->mode != -1) {
  96. /* use bit 13 as the mode button */
  97. raw_data[1] |= 0x04;
  98. }
  99. ssc->mode = mode;
  100. }
  101. } else if (ssc->quirks & SAITEK_RELEASE_MODE_MMO7 && size == 8) {
  102. /* M.M.O.7 uses bits 8, 22, 23 for the mode */
  103. int mode = -1;
  104. if (raw_data[1] & 0x80)
  105. mode = 0;
  106. else if (raw_data[2] & 0x01)
  107. mode = 1;
  108. else if (raw_data[2] & 0x02)
  109. mode = 2;
  110. /* clear mode bits */
  111. raw_data[1] &= ~0x80;
  112. raw_data[2] &= ~0x03;
  113. if (mode != ssc->mode) {
  114. hid_dbg(hdev, "entered mode %d\n", mode);
  115. if (ssc->mode != -1) {
  116. /* use bit 8 as the mode button, bits 22
  117. * and 23 do not represent buttons
  118. * according to the HID report descriptor
  119. */
  120. raw_data[1] |= 0x80;
  121. }
  122. ssc->mode = mode;
  123. }
  124. }
  125. return 0;
  126. }
  127. static int saitek_event(struct hid_device *hdev, struct hid_field *field,
  128. struct hid_usage *usage, __s32 value)
  129. {
  130. struct saitek_sc *ssc = hid_get_drvdata(hdev);
  131. struct input_dev *input = field->hidinput->input;
  132. if (usage->type == EV_KEY && value &&
  133. (((ssc->quirks & SAITEK_RELEASE_MODE_RAT7) &&
  134. usage->code - BTN_MOUSE == 10) ||
  135. ((ssc->quirks & SAITEK_RELEASE_MODE_MMO7) &&
  136. usage->code - BTN_MOUSE == 15))) {
  137. input_report_key(input, usage->code, 1);
  138. /* report missing release event */
  139. input_report_key(input, usage->code, 0);
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. static const struct hid_device_id saitek_devices[] = {
  145. { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_PS1000),
  146. .driver_data = SAITEK_FIX_PS1000 },
  147. { HID_USB_DEVICE(USB_VENDOR_ID_MADCATZ, USB_DEVICE_ID_MADCATZ_RAT5),
  148. .driver_data = SAITEK_RELEASE_MODE_RAT7 },
  149. { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RAT7_OLD),
  150. .driver_data = SAITEK_RELEASE_MODE_RAT7 },
  151. { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RAT7),
  152. .driver_data = SAITEK_RELEASE_MODE_RAT7 },
  153. { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RAT9),
  154. .driver_data = SAITEK_RELEASE_MODE_RAT7 },
  155. { HID_USB_DEVICE(USB_VENDOR_ID_MADCATZ, USB_DEVICE_ID_MADCATZ_RAT9),
  156. .driver_data = SAITEK_RELEASE_MODE_RAT7 },
  157. { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_MMO7),
  158. .driver_data = SAITEK_RELEASE_MODE_MMO7 },
  159. { }
  160. };
  161. MODULE_DEVICE_TABLE(hid, saitek_devices);
  162. static struct hid_driver saitek_driver = {
  163. .name = "saitek",
  164. .id_table = saitek_devices,
  165. .probe = saitek_probe,
  166. .report_fixup = saitek_report_fixup,
  167. .raw_event = saitek_raw_event,
  168. .event = saitek_event,
  169. };
  170. module_hid_driver(saitek_driver);
  171. MODULE_LICENSE("GPL");