hid-thingm.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * ThingM blink(1) USB RGB LED driver
  3. *
  4. * Copyright 2013-2014 Savoir-faire Linux Inc.
  5. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, version 2.
  10. */
  11. #include <linux/hid.h>
  12. #include <linux/hidraw.h>
  13. #include <linux/leds.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/workqueue.h>
  17. #include "hid-ids.h"
  18. #define REPORT_ID 1
  19. #define REPORT_SIZE 9
  20. /* Firmware major number of supported devices */
  21. #define THINGM_MAJOR_MK1 '1'
  22. #define THINGM_MAJOR_MK2 '2'
  23. struct thingm_fwinfo {
  24. char major;
  25. unsigned numrgb;
  26. unsigned first;
  27. };
  28. static const struct thingm_fwinfo thingm_fwinfo[] = {
  29. {
  30. .major = THINGM_MAJOR_MK1,
  31. .numrgb = 1,
  32. .first = 0,
  33. }, {
  34. .major = THINGM_MAJOR_MK2,
  35. .numrgb = 2,
  36. .first = 1,
  37. }
  38. };
  39. /* A red, green or blue channel, part of an RGB chip */
  40. struct thingm_led {
  41. struct thingm_rgb *rgb;
  42. struct led_classdev ldev;
  43. char name[32];
  44. };
  45. /* Basically a WS2812 5050 RGB LED chip */
  46. struct thingm_rgb {
  47. struct thingm_device *tdev;
  48. struct thingm_led red;
  49. struct thingm_led green;
  50. struct thingm_led blue;
  51. struct work_struct work;
  52. u8 num;
  53. };
  54. struct thingm_device {
  55. struct hid_device *hdev;
  56. struct {
  57. char major;
  58. char minor;
  59. } version;
  60. const struct thingm_fwinfo *fwinfo;
  61. struct mutex lock;
  62. struct thingm_rgb *rgb;
  63. };
  64. static int thingm_send(struct thingm_device *tdev, u8 buf[REPORT_SIZE])
  65. {
  66. int ret;
  67. hid_dbg(tdev->hdev, "-> %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n",
  68. buf[0], buf[1], buf[2], buf[3], buf[4],
  69. buf[5], buf[6], buf[7], buf[8]);
  70. ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
  71. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  72. return ret < 0 ? ret : 0;
  73. }
  74. static int thingm_recv(struct thingm_device *tdev, u8 buf[REPORT_SIZE])
  75. {
  76. int ret;
  77. ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
  78. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  79. if (ret < 0)
  80. return ret;
  81. hid_dbg(tdev->hdev, "<- %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n",
  82. buf[0], buf[1], buf[2], buf[3], buf[4],
  83. buf[5], buf[6], buf[7], buf[8]);
  84. return 0;
  85. }
  86. static int thingm_version(struct thingm_device *tdev)
  87. {
  88. u8 buf[REPORT_SIZE] = { REPORT_ID, 'v', 0, 0, 0, 0, 0, 0, 0 };
  89. int err;
  90. err = thingm_send(tdev, buf);
  91. if (err)
  92. return err;
  93. err = thingm_recv(tdev, buf);
  94. if (err)
  95. return err;
  96. tdev->version.major = buf[3];
  97. tdev->version.minor = buf[4];
  98. return 0;
  99. }
  100. static int thingm_write_color(struct thingm_rgb *rgb)
  101. {
  102. u8 buf[REPORT_SIZE] = { REPORT_ID, 'c', 0, 0, 0, 0, 0, rgb->num, 0 };
  103. buf[2] = rgb->red.ldev.brightness;
  104. buf[3] = rgb->green.ldev.brightness;
  105. buf[4] = rgb->blue.ldev.brightness;
  106. return thingm_send(rgb->tdev, buf);
  107. }
  108. static void thingm_work(struct work_struct *work)
  109. {
  110. struct thingm_rgb *rgb = container_of(work, struct thingm_rgb, work);
  111. mutex_lock(&rgb->tdev->lock);
  112. if (thingm_write_color(rgb))
  113. hid_err(rgb->tdev->hdev, "failed to write color\n");
  114. mutex_unlock(&rgb->tdev->lock);
  115. }
  116. static void thingm_led_set(struct led_classdev *ldev,
  117. enum led_brightness brightness)
  118. {
  119. struct thingm_led *led = container_of(ldev, struct thingm_led, ldev);
  120. /* the ledclass has already stored the brightness value */
  121. schedule_work(&led->rgb->work);
  122. }
  123. static int thingm_init_rgb(struct thingm_rgb *rgb)
  124. {
  125. const int minor = ((struct hidraw *) rgb->tdev->hdev->hidraw)->minor;
  126. int err;
  127. /* Register the red diode */
  128. snprintf(rgb->red.name, sizeof(rgb->red.name),
  129. "thingm%d:red:led%d", minor, rgb->num);
  130. rgb->red.ldev.name = rgb->red.name;
  131. rgb->red.ldev.max_brightness = 255;
  132. rgb->red.ldev.brightness_set = thingm_led_set;
  133. rgb->red.rgb = rgb;
  134. err = led_classdev_register(&rgb->tdev->hdev->dev, &rgb->red.ldev);
  135. if (err)
  136. return err;
  137. /* Register the green diode */
  138. snprintf(rgb->green.name, sizeof(rgb->green.name),
  139. "thingm%d:green:led%d", minor, rgb->num);
  140. rgb->green.ldev.name = rgb->green.name;
  141. rgb->green.ldev.max_brightness = 255;
  142. rgb->green.ldev.brightness_set = thingm_led_set;
  143. rgb->green.rgb = rgb;
  144. err = led_classdev_register(&rgb->tdev->hdev->dev, &rgb->green.ldev);
  145. if (err)
  146. goto unregister_red;
  147. /* Register the blue diode */
  148. snprintf(rgb->blue.name, sizeof(rgb->blue.name),
  149. "thingm%d:blue:led%d", minor, rgb->num);
  150. rgb->blue.ldev.name = rgb->blue.name;
  151. rgb->blue.ldev.max_brightness = 255;
  152. rgb->blue.ldev.brightness_set = thingm_led_set;
  153. rgb->blue.rgb = rgb;
  154. err = led_classdev_register(&rgb->tdev->hdev->dev, &rgb->blue.ldev);
  155. if (err)
  156. goto unregister_green;
  157. INIT_WORK(&rgb->work, thingm_work);
  158. return 0;
  159. unregister_green:
  160. led_classdev_unregister(&rgb->green.ldev);
  161. unregister_red:
  162. led_classdev_unregister(&rgb->red.ldev);
  163. return err;
  164. }
  165. static void thingm_remove_rgb(struct thingm_rgb *rgb)
  166. {
  167. led_classdev_unregister(&rgb->red.ldev);
  168. led_classdev_unregister(&rgb->green.ldev);
  169. led_classdev_unregister(&rgb->blue.ldev);
  170. flush_work(&rgb->work);
  171. }
  172. static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id)
  173. {
  174. struct thingm_device *tdev;
  175. int i, err;
  176. tdev = devm_kzalloc(&hdev->dev, sizeof(struct thingm_device),
  177. GFP_KERNEL);
  178. if (!tdev)
  179. return -ENOMEM;
  180. tdev->hdev = hdev;
  181. hid_set_drvdata(hdev, tdev);
  182. err = hid_parse(hdev);
  183. if (err)
  184. goto error;
  185. err = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  186. if (err)
  187. goto error;
  188. mutex_init(&tdev->lock);
  189. err = thingm_version(tdev);
  190. if (err)
  191. goto stop;
  192. hid_dbg(hdev, "firmware version: %c.%c\n",
  193. tdev->version.major, tdev->version.minor);
  194. for (i = 0; i < ARRAY_SIZE(thingm_fwinfo) && !tdev->fwinfo; ++i)
  195. if (thingm_fwinfo[i].major == tdev->version.major)
  196. tdev->fwinfo = &thingm_fwinfo[i];
  197. if (!tdev->fwinfo) {
  198. hid_err(hdev, "unsupported firmware %c\n", tdev->version.major);
  199. err = -ENODEV;
  200. goto stop;
  201. }
  202. tdev->rgb = devm_kzalloc(&hdev->dev,
  203. sizeof(struct thingm_rgb) * tdev->fwinfo->numrgb,
  204. GFP_KERNEL);
  205. if (!tdev->rgb) {
  206. err = -ENOMEM;
  207. goto stop;
  208. }
  209. for (i = 0; i < tdev->fwinfo->numrgb; ++i) {
  210. struct thingm_rgb *rgb = tdev->rgb + i;
  211. rgb->tdev = tdev;
  212. rgb->num = tdev->fwinfo->first + i;
  213. err = thingm_init_rgb(rgb);
  214. if (err) {
  215. while (--i >= 0)
  216. thingm_remove_rgb(tdev->rgb + i);
  217. goto stop;
  218. }
  219. }
  220. return 0;
  221. stop:
  222. hid_hw_stop(hdev);
  223. error:
  224. return err;
  225. }
  226. static void thingm_remove(struct hid_device *hdev)
  227. {
  228. struct thingm_device *tdev = hid_get_drvdata(hdev);
  229. int i;
  230. hid_hw_stop(hdev);
  231. for (i = 0; i < tdev->fwinfo->numrgb; ++i)
  232. thingm_remove_rgb(tdev->rgb + i);
  233. }
  234. static const struct hid_device_id thingm_table[] = {
  235. { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
  236. { }
  237. };
  238. MODULE_DEVICE_TABLE(hid, thingm_table);
  239. static struct hid_driver thingm_driver = {
  240. .name = "thingm",
  241. .probe = thingm_probe,
  242. .remove = thingm_remove,
  243. .id_table = thingm_table,
  244. };
  245. module_hid_driver(thingm_driver);
  246. MODULE_LICENSE("GPL");
  247. MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
  248. MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");