hid-steelseries.c 12 KB

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