hid-steelseries.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for Steelseries SRW-S1
  4. *
  5. * Copyright (c) 2013 Simon Wood
  6. */
  7. /*
  8. */
  9. #include <linux/device.h>
  10. #include <linux/hid.h>
  11. #include <linux/module.h>
  12. #include "hid-ids.h"
  13. #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
  14. (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
  15. #define SRWS1_NUMBER_LEDS 15
  16. struct steelseries_srws1_data {
  17. __u16 led_state;
  18. /* the last element is used for setting all leds simultaneously */
  19. struct led_classdev *led[SRWS1_NUMBER_LEDS + 1];
  20. };
  21. #endif
  22. /* Fixed report descriptor for Steelseries SRW-S1 wheel controller
  23. *
  24. * The original descriptor hides the sensitivity and assists dials
  25. * a custom vendor usage page. This inserts a patch to make them
  26. * appear in the 'Generic Desktop' usage.
  27. */
  28. static __u8 steelseries_srws1_rdesc_fixed[] = {
  29. 0x05, 0x01, /* Usage Page (Desktop) */
  30. 0x09, 0x08, /* Usage (MultiAxis), Changed */
  31. 0xA1, 0x01, /* Collection (Application), */
  32. 0xA1, 0x02, /* Collection (Logical), */
  33. 0x95, 0x01, /* Report Count (1), */
  34. 0x05, 0x01, /* Changed Usage Page (Desktop), */
  35. 0x09, 0x30, /* Changed Usage (X), */
  36. 0x16, 0xF8, 0xF8, /* Logical Minimum (-1800), */
  37. 0x26, 0x08, 0x07, /* Logical Maximum (1800), */
  38. 0x65, 0x14, /* Unit (Degrees), */
  39. 0x55, 0x0F, /* Unit Exponent (15), */
  40. 0x75, 0x10, /* Report Size (16), */
  41. 0x81, 0x02, /* Input (Variable), */
  42. 0x09, 0x31, /* Changed Usage (Y), */
  43. 0x15, 0x00, /* Logical Minimum (0), */
  44. 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
  45. 0x75, 0x0C, /* Report Size (12), */
  46. 0x81, 0x02, /* Input (Variable), */
  47. 0x09, 0x32, /* Changed Usage (Z), */
  48. 0x15, 0x00, /* Logical Minimum (0), */
  49. 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
  50. 0x75, 0x0C, /* Report Size (12), */
  51. 0x81, 0x02, /* Input (Variable), */
  52. 0x05, 0x01, /* Usage Page (Desktop), */
  53. 0x09, 0x39, /* Usage (Hat Switch), */
  54. 0x25, 0x07, /* Logical Maximum (7), */
  55. 0x35, 0x00, /* Physical Minimum (0), */
  56. 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
  57. 0x65, 0x14, /* Unit (Degrees), */
  58. 0x75, 0x04, /* Report Size (4), */
  59. 0x95, 0x01, /* Report Count (1), */
  60. 0x81, 0x02, /* Input (Variable), */
  61. 0x25, 0x01, /* Logical Maximum (1), */
  62. 0x45, 0x01, /* Physical Maximum (1), */
  63. 0x65, 0x00, /* Unit, */
  64. 0x75, 0x01, /* Report Size (1), */
  65. 0x95, 0x03, /* Report Count (3), */
  66. 0x81, 0x01, /* Input (Constant), */
  67. 0x05, 0x09, /* Usage Page (Button), */
  68. 0x19, 0x01, /* Usage Minimum (01h), */
  69. 0x29, 0x11, /* Usage Maximum (11h), */
  70. 0x95, 0x11, /* Report Count (17), */
  71. 0x81, 0x02, /* Input (Variable), */
  72. /* ---- Dial patch starts here ---- */
  73. 0x05, 0x01, /* Usage Page (Desktop), */
  74. 0x09, 0x33, /* Usage (RX), */
  75. 0x75, 0x04, /* Report Size (4), */
  76. 0x95, 0x02, /* Report Count (2), */
  77. 0x15, 0x00, /* Logical Minimum (0), */
  78. 0x25, 0x0b, /* Logical Maximum (b), */
  79. 0x81, 0x02, /* Input (Variable), */
  80. 0x09, 0x35, /* Usage (RZ), */
  81. 0x75, 0x04, /* Report Size (4), */
  82. 0x95, 0x01, /* Report Count (1), */
  83. 0x25, 0x03, /* Logical Maximum (3), */
  84. 0x81, 0x02, /* Input (Variable), */
  85. /* ---- Dial patch ends here ---- */
  86. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  87. 0x09, 0x01, /* Usage (01h), */
  88. 0x75, 0x04, /* Changed Report Size (4), */
  89. 0x95, 0x0D, /* Changed Report Count (13), */
  90. 0x81, 0x02, /* Input (Variable), */
  91. 0xC0, /* End Collection, */
  92. 0xA1, 0x02, /* Collection (Logical), */
  93. 0x09, 0x02, /* Usage (02h), */
  94. 0x75, 0x08, /* Report Size (8), */
  95. 0x95, 0x10, /* Report Count (16), */
  96. 0x91, 0x02, /* Output (Variable), */
  97. 0xC0, /* End Collection, */
  98. 0xC0 /* End Collection */
  99. };
  100. #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
  101. (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
  102. static void steelseries_srws1_set_leds(struct hid_device *hdev, __u16 leds)
  103. {
  104. struct list_head *report_list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
  105. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  106. __s32 *value = report->field[0]->value;
  107. value[0] = 0x40;
  108. value[1] = leds & 0xFF;
  109. value[2] = leds >> 8;
  110. value[3] = 0x00;
  111. value[4] = 0x00;
  112. value[5] = 0x00;
  113. value[6] = 0x00;
  114. value[7] = 0x00;
  115. value[8] = 0x00;
  116. value[9] = 0x00;
  117. value[10] = 0x00;
  118. value[11] = 0x00;
  119. value[12] = 0x00;
  120. value[13] = 0x00;
  121. value[14] = 0x00;
  122. value[15] = 0x00;
  123. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  124. /* Note: LED change does not show on device until the device is read/polled */
  125. }
  126. static void steelseries_srws1_led_all_set_brightness(struct led_classdev *led_cdev,
  127. enum led_brightness value)
  128. {
  129. struct device *dev = led_cdev->dev->parent;
  130. struct hid_device *hid = to_hid_device(dev);
  131. struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
  132. if (!drv_data) {
  133. hid_err(hid, "Device data not found.");
  134. return;
  135. }
  136. if (value == LED_OFF)
  137. drv_data->led_state = 0;
  138. else
  139. drv_data->led_state = (1 << (SRWS1_NUMBER_LEDS + 1)) - 1;
  140. steelseries_srws1_set_leds(hid, drv_data->led_state);
  141. }
  142. static enum led_brightness steelseries_srws1_led_all_get_brightness(struct led_classdev *led_cdev)
  143. {
  144. struct device *dev = led_cdev->dev->parent;
  145. struct hid_device *hid = to_hid_device(dev);
  146. struct steelseries_srws1_data *drv_data;
  147. drv_data = hid_get_drvdata(hid);
  148. if (!drv_data) {
  149. hid_err(hid, "Device data not found.");
  150. return LED_OFF;
  151. }
  152. return (drv_data->led_state >> SRWS1_NUMBER_LEDS) ? LED_FULL : LED_OFF;
  153. }
  154. static void steelseries_srws1_led_set_brightness(struct led_classdev *led_cdev,
  155. enum led_brightness value)
  156. {
  157. struct device *dev = led_cdev->dev->parent;
  158. struct hid_device *hid = to_hid_device(dev);
  159. struct steelseries_srws1_data *drv_data = hid_get_drvdata(hid);
  160. int i, state = 0;
  161. if (!drv_data) {
  162. hid_err(hid, "Device data not found.");
  163. return;
  164. }
  165. for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
  166. if (led_cdev != drv_data->led[i])
  167. continue;
  168. state = (drv_data->led_state >> i) & 1;
  169. if (value == LED_OFF && state) {
  170. drv_data->led_state &= ~(1 << i);
  171. steelseries_srws1_set_leds(hid, drv_data->led_state);
  172. } else if (value != LED_OFF && !state) {
  173. drv_data->led_state |= 1 << i;
  174. steelseries_srws1_set_leds(hid, drv_data->led_state);
  175. }
  176. break;
  177. }
  178. }
  179. static enum led_brightness steelseries_srws1_led_get_brightness(struct led_classdev *led_cdev)
  180. {
  181. struct device *dev = led_cdev->dev->parent;
  182. struct hid_device *hid = to_hid_device(dev);
  183. struct steelseries_srws1_data *drv_data;
  184. int i, value = 0;
  185. drv_data = hid_get_drvdata(hid);
  186. if (!drv_data) {
  187. hid_err(hid, "Device data not found.");
  188. return LED_OFF;
  189. }
  190. for (i = 0; i < SRWS1_NUMBER_LEDS; i++)
  191. if (led_cdev == drv_data->led[i]) {
  192. value = (drv_data->led_state >> i) & 1;
  193. break;
  194. }
  195. return value ? LED_FULL : LED_OFF;
  196. }
  197. static int steelseries_srws1_probe(struct hid_device *hdev,
  198. const struct hid_device_id *id)
  199. {
  200. int ret, i;
  201. struct led_classdev *led;
  202. size_t name_sz;
  203. char *name;
  204. struct steelseries_srws1_data *drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
  205. if (drv_data == NULL) {
  206. hid_err(hdev, "can't alloc SRW-S1 memory\n");
  207. return -ENOMEM;
  208. }
  209. hid_set_drvdata(hdev, drv_data);
  210. ret = hid_parse(hdev);
  211. if (ret) {
  212. hid_err(hdev, "parse failed\n");
  213. goto err_free;
  214. }
  215. if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 16)) {
  216. ret = -ENODEV;
  217. goto err_free;
  218. }
  219. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  220. if (ret) {
  221. hid_err(hdev, "hw start failed\n");
  222. goto err_free;
  223. }
  224. /* register led subsystem */
  225. drv_data->led_state = 0;
  226. for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++)
  227. drv_data->led[i] = NULL;
  228. steelseries_srws1_set_leds(hdev, 0);
  229. name_sz = strlen(hdev->uniq) + 16;
  230. /* 'ALL', for setting all LEDs simultaneously */
  231. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  232. if (!led) {
  233. hid_err(hdev, "can't allocate memory for LED ALL\n");
  234. goto err_led;
  235. }
  236. name = (void *)(&led[1]);
  237. snprintf(name, name_sz, "SRWS1::%s::RPMALL", hdev->uniq);
  238. led->name = name;
  239. led->brightness = 0;
  240. led->max_brightness = 1;
  241. led->brightness_get = steelseries_srws1_led_all_get_brightness;
  242. led->brightness_set = steelseries_srws1_led_all_set_brightness;
  243. drv_data->led[SRWS1_NUMBER_LEDS] = led;
  244. ret = led_classdev_register(&hdev->dev, led);
  245. if (ret)
  246. goto err_led;
  247. /* Each individual LED */
  248. for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
  249. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  250. if (!led) {
  251. hid_err(hdev, "can't allocate memory for LED %d\n", i);
  252. goto err_led;
  253. }
  254. name = (void *)(&led[1]);
  255. snprintf(name, name_sz, "SRWS1::%s::RPM%d", hdev->uniq, i+1);
  256. led->name = name;
  257. led->brightness = 0;
  258. led->max_brightness = 1;
  259. led->brightness_get = steelseries_srws1_led_get_brightness;
  260. led->brightness_set = steelseries_srws1_led_set_brightness;
  261. drv_data->led[i] = led;
  262. ret = led_classdev_register(&hdev->dev, led);
  263. if (ret) {
  264. hid_err(hdev, "failed to register LED %d. Aborting.\n", i);
  265. err_led:
  266. /* Deregister all LEDs (if any) */
  267. for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
  268. led = drv_data->led[i];
  269. drv_data->led[i] = NULL;
  270. if (!led)
  271. continue;
  272. led_classdev_unregister(led);
  273. kfree(led);
  274. }
  275. goto out; /* but let the driver continue without LEDs */
  276. }
  277. }
  278. out:
  279. return 0;
  280. err_free:
  281. kfree(drv_data);
  282. return ret;
  283. }
  284. static void steelseries_srws1_remove(struct hid_device *hdev)
  285. {
  286. int i;
  287. struct led_classdev *led;
  288. struct steelseries_srws1_data *drv_data = hid_get_drvdata(hdev);
  289. if (drv_data) {
  290. /* Deregister LEDs (if any) */
  291. for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
  292. led = drv_data->led[i];
  293. drv_data->led[i] = NULL;
  294. if (!led)
  295. continue;
  296. led_classdev_unregister(led);
  297. kfree(led);
  298. }
  299. }
  300. hid_hw_stop(hdev);
  301. kfree(drv_data);
  302. return;
  303. }
  304. #endif
  305. static __u8 *steelseries_srws1_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  306. unsigned int *rsize)
  307. {
  308. if (*rsize >= 115 && rdesc[11] == 0x02 && rdesc[13] == 0xc8
  309. && rdesc[29] == 0xbb && rdesc[40] == 0xc5) {
  310. hid_info(hdev, "Fixing up Steelseries SRW-S1 report descriptor\n");
  311. rdesc = steelseries_srws1_rdesc_fixed;
  312. *rsize = sizeof(steelseries_srws1_rdesc_fixed);
  313. }
  314. return rdesc;
  315. }
  316. static const struct hid_device_id steelseries_srws1_devices[] = {
  317. { HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1) },
  318. { }
  319. };
  320. MODULE_DEVICE_TABLE(hid, steelseries_srws1_devices);
  321. static struct hid_driver steelseries_srws1_driver = {
  322. .name = "steelseries_srws1",
  323. .id_table = steelseries_srws1_devices,
  324. #if IS_BUILTIN(CONFIG_LEDS_CLASS) || \
  325. (IS_MODULE(CONFIG_LEDS_CLASS) && IS_MODULE(CONFIG_HID_STEELSERIES))
  326. .probe = steelseries_srws1_probe,
  327. .remove = steelseries_srws1_remove,
  328. #endif
  329. .report_fixup = steelseries_srws1_report_fixup
  330. };
  331. module_hid_driver(steelseries_srws1_driver);
  332. MODULE_LICENSE("GPL");