hid-thingm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "hid-ids.h"
  17. #define REPORT_ID 1
  18. #define REPORT_SIZE 9
  19. /* Firmware major number of supported devices */
  20. #define THINGM_MAJOR_MK1 '1'
  21. #define THINGM_MAJOR_MK2 '2'
  22. struct thingm_fwinfo {
  23. char major;
  24. unsigned numrgb;
  25. unsigned first;
  26. };
  27. static const struct thingm_fwinfo thingm_fwinfo[] = {
  28. {
  29. .major = THINGM_MAJOR_MK1,
  30. .numrgb = 1,
  31. .first = 0,
  32. }, {
  33. .major = THINGM_MAJOR_MK2,
  34. .numrgb = 2,
  35. .first = 1,
  36. }
  37. };
  38. /* A red, green or blue channel, part of an RGB chip */
  39. struct thingm_led {
  40. struct thingm_rgb *rgb;
  41. struct led_classdev ldev;
  42. char name[32];
  43. };
  44. /* Basically a WS2812 5050 RGB LED chip */
  45. struct thingm_rgb {
  46. struct thingm_device *tdev;
  47. struct thingm_led red;
  48. struct thingm_led green;
  49. struct thingm_led blue;
  50. u8 num;
  51. };
  52. struct thingm_device {
  53. struct hid_device *hdev;
  54. struct {
  55. char major;
  56. char minor;
  57. } version;
  58. const struct thingm_fwinfo *fwinfo;
  59. struct mutex lock;
  60. struct thingm_rgb *rgb;
  61. };
  62. static int thingm_send(struct thingm_device *tdev, u8 buf[REPORT_SIZE])
  63. {
  64. int ret;
  65. hid_dbg(tdev->hdev, "-> %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n",
  66. buf[0], buf[1], buf[2], buf[3], buf[4],
  67. buf[5], buf[6], buf[7], buf[8]);
  68. mutex_lock(&tdev->lock);
  69. ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
  70. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  71. mutex_unlock(&tdev->lock);
  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. /*
  78. * A read consists of two operations: sending the read command
  79. * and the actual read from the device. Use the mutex to protect
  80. * the full sequence of both operations.
  81. */
  82. mutex_lock(&tdev->lock);
  83. ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
  84. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  85. if (ret < 0)
  86. goto err;
  87. ret = hid_hw_raw_request(tdev->hdev, buf[0], buf, REPORT_SIZE,
  88. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  89. if (ret < 0)
  90. goto err;
  91. ret = 0;
  92. hid_dbg(tdev->hdev, "<- %d %c %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n",
  93. buf[0], buf[1], buf[2], buf[3], buf[4],
  94. buf[5], buf[6], buf[7], buf[8]);
  95. err:
  96. mutex_unlock(&tdev->lock);
  97. return ret;
  98. }
  99. static int thingm_version(struct thingm_device *tdev)
  100. {
  101. u8 buf[REPORT_SIZE] = { REPORT_ID, 'v', 0, 0, 0, 0, 0, 0, 0 };
  102. int err;
  103. err = thingm_recv(tdev, buf);
  104. if (err)
  105. return err;
  106. tdev->version.major = buf[3];
  107. tdev->version.minor = buf[4];
  108. return 0;
  109. }
  110. static int thingm_write_color(struct thingm_rgb *rgb)
  111. {
  112. u8 buf[REPORT_SIZE] = { REPORT_ID, 'c', 0, 0, 0, 0, 0, rgb->num, 0 };
  113. buf[2] = rgb->red.ldev.brightness;
  114. buf[3] = rgb->green.ldev.brightness;
  115. buf[4] = rgb->blue.ldev.brightness;
  116. return thingm_send(rgb->tdev, buf);
  117. }
  118. static int thingm_led_set(struct led_classdev *ldev,
  119. enum led_brightness brightness)
  120. {
  121. struct thingm_led *led = container_of(ldev, struct thingm_led, ldev);
  122. int ret;
  123. ret = thingm_write_color(led->rgb);
  124. if (ret)
  125. hid_err(led->rgb->tdev->hdev, "failed to write color\n");
  126. return ret;
  127. }
  128. static int thingm_init_rgb(struct thingm_rgb *rgb)
  129. {
  130. const int minor = ((struct hidraw *) rgb->tdev->hdev->hidraw)->minor;
  131. int err;
  132. /* Register the red diode */
  133. snprintf(rgb->red.name, sizeof(rgb->red.name),
  134. "thingm%d:red:led%d", minor, rgb->num);
  135. rgb->red.ldev.name = rgb->red.name;
  136. rgb->red.ldev.max_brightness = 255;
  137. rgb->red.ldev.brightness_set_blocking = thingm_led_set;
  138. rgb->red.rgb = rgb;
  139. err = devm_led_classdev_register(&rgb->tdev->hdev->dev,
  140. &rgb->red.ldev);
  141. if (err)
  142. return err;
  143. /* Register the green diode */
  144. snprintf(rgb->green.name, sizeof(rgb->green.name),
  145. "thingm%d:green:led%d", minor, rgb->num);
  146. rgb->green.ldev.name = rgb->green.name;
  147. rgb->green.ldev.max_brightness = 255;
  148. rgb->green.ldev.brightness_set_blocking = thingm_led_set;
  149. rgb->green.rgb = rgb;
  150. err = devm_led_classdev_register(&rgb->tdev->hdev->dev,
  151. &rgb->green.ldev);
  152. if (err)
  153. return err;
  154. /* Register the blue diode */
  155. snprintf(rgb->blue.name, sizeof(rgb->blue.name),
  156. "thingm%d:blue:led%d", minor, rgb->num);
  157. rgb->blue.ldev.name = rgb->blue.name;
  158. rgb->blue.ldev.max_brightness = 255;
  159. rgb->blue.ldev.brightness_set_blocking = thingm_led_set;
  160. rgb->blue.rgb = rgb;
  161. err = devm_led_classdev_register(&rgb->tdev->hdev->dev,
  162. &rgb->blue.ldev);
  163. return err;
  164. }
  165. static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id)
  166. {
  167. struct thingm_device *tdev;
  168. int i, err;
  169. tdev = devm_kzalloc(&hdev->dev, sizeof(struct thingm_device),
  170. GFP_KERNEL);
  171. if (!tdev)
  172. return -ENOMEM;
  173. tdev->hdev = hdev;
  174. hid_set_drvdata(hdev, tdev);
  175. err = hid_parse(hdev);
  176. if (err)
  177. return err;
  178. mutex_init(&tdev->lock);
  179. err = thingm_version(tdev);
  180. if (err)
  181. return err;
  182. hid_dbg(hdev, "firmware version: %c.%c\n",
  183. tdev->version.major, tdev->version.minor);
  184. for (i = 0; i < ARRAY_SIZE(thingm_fwinfo) && !tdev->fwinfo; ++i)
  185. if (thingm_fwinfo[i].major == tdev->version.major)
  186. tdev->fwinfo = &thingm_fwinfo[i];
  187. if (!tdev->fwinfo) {
  188. hid_err(hdev, "unsupported firmware %c\n", tdev->version.major);
  189. return -ENODEV;
  190. }
  191. tdev->rgb = devm_kzalloc(&hdev->dev,
  192. sizeof(struct thingm_rgb) * tdev->fwinfo->numrgb,
  193. GFP_KERNEL);
  194. if (!tdev->rgb)
  195. return -ENOMEM;
  196. err = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  197. if (err)
  198. return err;
  199. for (i = 0; i < tdev->fwinfo->numrgb; ++i) {
  200. struct thingm_rgb *rgb = tdev->rgb + i;
  201. rgb->tdev = tdev;
  202. rgb->num = tdev->fwinfo->first + i;
  203. err = thingm_init_rgb(rgb);
  204. if (err) {
  205. hid_hw_stop(hdev);
  206. return err;
  207. }
  208. }
  209. return 0;
  210. }
  211. static const struct hid_device_id thingm_table[] = {
  212. { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
  213. { }
  214. };
  215. MODULE_DEVICE_TABLE(hid, thingm_table);
  216. static struct hid_driver thingm_driver = {
  217. .name = "thingm",
  218. .probe = thingm_probe,
  219. .id_table = thingm_table,
  220. };
  221. module_hid_driver(thingm_driver);
  222. MODULE_LICENSE("GPL");
  223. MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
  224. MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");