hid-steelseries.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/usb.h>
  14. #include <linux/hid.h>
  15. #include <linux/module.h>
  16. #include "usbhid/usbhid.h"
  17. #include "hid-ids.h"
  18. #if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
  19. #define SRWS1_NUMBER_LEDS 15
  20. struct steelseries_srws1_data {
  21. __u16 led_state;
  22. /* the last element is used for setting all leds simultaneously */
  23. struct led_classdev *led[SRWS1_NUMBER_LEDS + 1];
  24. };
  25. #endif
  26. /* Fixed report descriptor for Steelseries SRW-S1 wheel controller
  27. *
  28. * The original descriptor hides the sensitivity and assists dials
  29. * a custom vendor usage page. This inserts a patch to make them
  30. * appear in the 'Generic Desktop' usage.
  31. */
  32. static __u8 steelseries_srws1_rdesc_fixed[] = {
  33. 0x05, 0x01, /* Usage Page (Desktop) */
  34. 0x09, 0x08, /* Usage (MultiAxis), Changed */
  35. 0xA1, 0x01, /* Collection (Application), */
  36. 0xA1, 0x02, /* Collection (Logical), */
  37. 0x95, 0x01, /* Report Count (1), */
  38. 0x05, 0x01, /* Changed Usage Page (Desktop), */
  39. 0x09, 0x30, /* Changed Usage (X), */
  40. 0x16, 0xF8, 0xF8, /* Logical Minimum (-1800), */
  41. 0x26, 0x08, 0x07, /* Logical Maximum (1800), */
  42. 0x65, 0x14, /* Unit (Degrees), */
  43. 0x55, 0x0F, /* Unit Exponent (15), */
  44. 0x75, 0x10, /* Report Size (16), */
  45. 0x81, 0x02, /* Input (Variable), */
  46. 0x09, 0x31, /* Changed Usage (Y), */
  47. 0x15, 0x00, /* Logical Minimum (0), */
  48. 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
  49. 0x75, 0x0C, /* Report Size (12), */
  50. 0x81, 0x02, /* Input (Variable), */
  51. 0x09, 0x32, /* Changed Usage (Z), */
  52. 0x15, 0x00, /* Logical Minimum (0), */
  53. 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
  54. 0x75, 0x0C, /* Report Size (12), */
  55. 0x81, 0x02, /* Input (Variable), */
  56. 0x05, 0x01, /* Usage Page (Desktop), */
  57. 0x09, 0x39, /* Usage (Hat Switch), */
  58. 0x25, 0x07, /* Logical Maximum (7), */
  59. 0x35, 0x00, /* Physical Minimum (0), */
  60. 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
  61. 0x65, 0x14, /* Unit (Degrees), */
  62. 0x75, 0x04, /* Report Size (4), */
  63. 0x95, 0x01, /* Report Count (1), */
  64. 0x81, 0x02, /* Input (Variable), */
  65. 0x25, 0x01, /* Logical Maximum (1), */
  66. 0x45, 0x01, /* Physical Maximum (1), */
  67. 0x65, 0x00, /* Unit, */
  68. 0x75, 0x01, /* Report Size (1), */
  69. 0x95, 0x03, /* Report Count (3), */
  70. 0x81, 0x01, /* Input (Constant), */
  71. 0x05, 0x09, /* Usage Page (Button), */
  72. 0x19, 0x01, /* Usage Minimum (01h), */
  73. 0x29, 0x11, /* Usage Maximum (11h), */
  74. 0x95, 0x11, /* Report Count (17), */
  75. 0x81, 0x02, /* Input (Variable), */
  76. /* ---- Dial patch starts here ---- */
  77. 0x05, 0x01, /* Usage Page (Desktop), */
  78. 0x09, 0x33, /* Usage (RX), */
  79. 0x75, 0x04, /* Report Size (4), */
  80. 0x95, 0x02, /* Report Count (2), */
  81. 0x15, 0x00, /* Logical Minimum (0), */
  82. 0x25, 0x0b, /* Logical Maximum (b), */
  83. 0x81, 0x02, /* Input (Variable), */
  84. 0x09, 0x35, /* Usage (RZ), */
  85. 0x75, 0x04, /* Report Size (4), */
  86. 0x95, 0x01, /* Report Count (1), */
  87. 0x25, 0x03, /* Logical Maximum (3), */
  88. 0x81, 0x02, /* Input (Variable), */
  89. /* ---- Dial patch ends here ---- */
  90. 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  91. 0x09, 0x01, /* Usage (01h), */
  92. 0x75, 0x04, /* Changed Report Size (4), */
  93. 0x95, 0x0D, /* Changed Report Count (13), */
  94. 0x81, 0x02, /* Input (Variable), */
  95. 0xC0, /* End Collection, */
  96. 0xA1, 0x02, /* Collection (Logical), */
  97. 0x09, 0x02, /* Usage (02h), */
  98. 0x75, 0x08, /* Report Size (8), */
  99. 0x95, 0x10, /* Report Count (16), */
  100. 0x91, 0x02, /* Output (Variable), */
  101. 0xC0, /* End Collection, */
  102. 0xC0 /* End Collection */
  103. };
  104. #if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
  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. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  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 = container_of(dev, struct 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 = container_of(dev, struct 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 = container_of(dev, struct 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 = container_of(dev, struct 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. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  219. if (ret) {
  220. hid_err(hdev, "hw start failed\n");
  221. goto err_free;
  222. }
  223. /* register led subsystem */
  224. drv_data->led_state = 0;
  225. for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++)
  226. drv_data->led[i] = NULL;
  227. steelseries_srws1_set_leds(hdev, 0);
  228. name_sz = strlen(hdev->uniq) + 16;
  229. /* 'ALL', for setting all LEDs simultaneously */
  230. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  231. if (!led) {
  232. hid_err(hdev, "can't allocate memory for LED ALL\n");
  233. goto err_led;
  234. }
  235. name = (void *)(&led[1]);
  236. snprintf(name, name_sz, "SRWS1::%s::RPMALL", hdev->uniq);
  237. led->name = name;
  238. led->brightness = 0;
  239. led->max_brightness = 1;
  240. led->brightness_get = steelseries_srws1_led_all_get_brightness;
  241. led->brightness_set = steelseries_srws1_led_all_set_brightness;
  242. drv_data->led[SRWS1_NUMBER_LEDS] = led;
  243. ret = led_classdev_register(&hdev->dev, led);
  244. if (ret)
  245. goto err_led;
  246. /* Each individual LED */
  247. for (i = 0; i < SRWS1_NUMBER_LEDS; i++) {
  248. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  249. if (!led) {
  250. hid_err(hdev, "can't allocate memory for LED %d\n", i);
  251. goto err_led;
  252. }
  253. name = (void *)(&led[1]);
  254. snprintf(name, name_sz, "SRWS1::%s::RPM%d", hdev->uniq, i+1);
  255. led->name = name;
  256. led->brightness = 0;
  257. led->max_brightness = 1;
  258. led->brightness_get = steelseries_srws1_led_get_brightness;
  259. led->brightness_set = steelseries_srws1_led_set_brightness;
  260. drv_data->led[i] = led;
  261. ret = led_classdev_register(&hdev->dev, led);
  262. if (ret) {
  263. hid_err(hdev, "failed to register LED %d. Aborting.\n", i);
  264. err_led:
  265. /* Deregister all LEDs (if any) */
  266. for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
  267. led = drv_data->led[i];
  268. drv_data->led[i] = NULL;
  269. if (!led)
  270. continue;
  271. led_classdev_unregister(led);
  272. kfree(led);
  273. }
  274. goto out; /* but let the driver continue without LEDs */
  275. }
  276. }
  277. out:
  278. return 0;
  279. err_free:
  280. kfree(drv_data);
  281. return ret;
  282. }
  283. static void steelseries_srws1_remove(struct hid_device *hdev)
  284. {
  285. int i;
  286. struct led_classdev *led;
  287. struct steelseries_srws1_data *drv_data = hid_get_drvdata(hdev);
  288. if (drv_data) {
  289. /* Deregister LEDs (if any) */
  290. for (i = 0; i < SRWS1_NUMBER_LEDS + 1; i++) {
  291. led = drv_data->led[i];
  292. drv_data->led[i] = NULL;
  293. if (!led)
  294. continue;
  295. led_classdev_unregister(led);
  296. kfree(led);
  297. }
  298. }
  299. hid_hw_stop(hdev);
  300. kfree(drv_data);
  301. return;
  302. }
  303. #endif
  304. static __u8 *steelseries_srws1_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  305. unsigned int *rsize)
  306. {
  307. if (*rsize >= 115 && rdesc[11] == 0x02 && rdesc[13] == 0xc8
  308. && rdesc[29] == 0xbb && rdesc[40] == 0xc5) {
  309. hid_info(hdev, "Fixing up Steelseries SRW-S1 report descriptor\n");
  310. rdesc = steelseries_srws1_rdesc_fixed;
  311. *rsize = sizeof(steelseries_srws1_rdesc_fixed);
  312. }
  313. return rdesc;
  314. }
  315. static const struct hid_device_id steelseries_srws1_devices[] = {
  316. { HID_USB_DEVICE(USB_VENDOR_ID_STEELSERIES, USB_DEVICE_ID_STEELSERIES_SRWS1) },
  317. { }
  318. };
  319. MODULE_DEVICE_TABLE(hid, steelseries_srws1_devices);
  320. static struct hid_driver steelseries_srws1_driver = {
  321. .name = "steelseries_srws1",
  322. .id_table = steelseries_srws1_devices,
  323. #if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
  324. .probe = steelseries_srws1_probe,
  325. .remove = steelseries_srws1_remove,
  326. #endif
  327. .report_fixup = steelseries_srws1_report_fixup
  328. };
  329. static int __init steelseries_srws1_init(void)
  330. {
  331. return hid_register_driver(&steelseries_srws1_driver);
  332. }
  333. static void __exit steelseries_srws1_exit(void)
  334. {
  335. hid_unregister_driver(&steelseries_srws1_driver);
  336. }
  337. module_init(steelseries_srws1_init);
  338. module_exit(steelseries_srws1_exit);
  339. MODULE_LICENSE("GPL");