hid-thingm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * ThingM blink(1) USB RGB LED driver
  3. *
  4. * Copyright 2013 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/leds.h>
  13. #include <linux/module.h>
  14. #include <linux/usb.h>
  15. #include "hid-ids.h"
  16. #define BLINK1_CMD_SIZE 9
  17. #define blink1_rgb_to_r(rgb) ((rgb & 0xFF0000) >> 16)
  18. #define blink1_rgb_to_g(rgb) ((rgb & 0x00FF00) >> 8)
  19. #define blink1_rgb_to_b(rgb) ((rgb & 0x0000FF) >> 0)
  20. /**
  21. * struct blink1_data - blink(1) device specific data
  22. * @hdev: HID device.
  23. * @led_cdev: LED class instance.
  24. * @rgb: 8-bit per channel RGB notation.
  25. * @fade: fade time in hundredths of a second.
  26. * @brightness: brightness coefficient.
  27. * @play: play/pause in-memory patterns.
  28. */
  29. struct blink1_data {
  30. struct hid_device *hdev;
  31. struct led_classdev led_cdev;
  32. u32 rgb;
  33. u16 fade;
  34. u8 brightness;
  35. bool play;
  36. };
  37. static int blink1_send_command(struct blink1_data *data,
  38. u8 buf[BLINK1_CMD_SIZE])
  39. {
  40. int ret;
  41. hid_dbg(data->hdev, "command: %d%c%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n",
  42. buf[0], buf[1], buf[2], buf[3], buf[4],
  43. buf[5], buf[6], buf[7], buf[8]);
  44. ret = data->hdev->hid_output_raw_report(data->hdev, buf,
  45. BLINK1_CMD_SIZE, HID_FEATURE_REPORT);
  46. return ret < 0 ? ret : 0;
  47. }
  48. static int blink1_update_color(struct blink1_data *data)
  49. {
  50. u8 buf[BLINK1_CMD_SIZE] = { 1, 'n', 0, 0, 0, 0, 0, 0, 0 };
  51. if (data->brightness) {
  52. unsigned int coef = DIV_ROUND_CLOSEST(255, data->brightness);
  53. buf[2] = DIV_ROUND_CLOSEST(blink1_rgb_to_r(data->rgb), coef);
  54. buf[3] = DIV_ROUND_CLOSEST(blink1_rgb_to_g(data->rgb), coef);
  55. buf[4] = DIV_ROUND_CLOSEST(blink1_rgb_to_b(data->rgb), coef);
  56. }
  57. if (data->fade) {
  58. buf[1] = 'c';
  59. buf[5] = (data->fade & 0xFF00) >> 8;
  60. buf[6] = (data->fade & 0x00FF);
  61. }
  62. return blink1_send_command(data, buf);
  63. }
  64. static void blink1_led_set(struct led_classdev *led_cdev,
  65. enum led_brightness brightness)
  66. {
  67. struct blink1_data *data = dev_get_drvdata(led_cdev->dev->parent);
  68. data->brightness = brightness;
  69. if (blink1_update_color(data))
  70. hid_err(data->hdev, "failed to update color\n");
  71. }
  72. static enum led_brightness blink1_led_get(struct led_classdev *led_cdev)
  73. {
  74. struct blink1_data *data = dev_get_drvdata(led_cdev->dev->parent);
  75. return data->brightness;
  76. }
  77. static ssize_t blink1_show_rgb(struct device *dev,
  78. struct device_attribute *attr, char *buf)
  79. {
  80. struct blink1_data *data = dev_get_drvdata(dev->parent);
  81. return sprintf(buf, "%.6X\n", data->rgb);
  82. }
  83. static ssize_t blink1_store_rgb(struct device *dev,
  84. struct device_attribute *attr, const char *buf, size_t count)
  85. {
  86. struct blink1_data *data = dev_get_drvdata(dev->parent);
  87. long unsigned int rgb;
  88. int ret;
  89. ret = kstrtoul(buf, 16, &rgb);
  90. if (ret)
  91. return ret;
  92. /* RGB triplet notation is 24-bit hexadecimal */
  93. if (rgb > 0xFFFFFF)
  94. return -EINVAL;
  95. data->rgb = rgb;
  96. ret = blink1_update_color(data);
  97. return ret ? ret : count;
  98. }
  99. static DEVICE_ATTR(rgb, S_IRUGO | S_IWUSR, blink1_show_rgb, blink1_store_rgb);
  100. static ssize_t blink1_show_fade(struct device *dev,
  101. struct device_attribute *attr, char *buf)
  102. {
  103. struct blink1_data *data = dev_get_drvdata(dev->parent);
  104. return sprintf(buf, "%d\n", data->fade * 10);
  105. }
  106. static ssize_t blink1_store_fade(struct device *dev,
  107. struct device_attribute *attr, const char *buf, size_t count)
  108. {
  109. struct blink1_data *data = dev_get_drvdata(dev->parent);
  110. long unsigned int fade;
  111. int ret;
  112. ret = kstrtoul(buf, 10, &fade);
  113. if (ret)
  114. return ret;
  115. /* blink(1) accepts 16-bit fade time, number of 10ms ticks */
  116. fade = DIV_ROUND_CLOSEST(fade, 10);
  117. if (fade > 65535)
  118. return -EINVAL;
  119. data->fade = fade;
  120. return count;
  121. }
  122. static DEVICE_ATTR(fade, S_IRUGO | S_IWUSR,
  123. blink1_show_fade, blink1_store_fade);
  124. static ssize_t blink1_show_play(struct device *dev,
  125. struct device_attribute *attr, char *buf)
  126. {
  127. struct blink1_data *data = dev_get_drvdata(dev->parent);
  128. return sprintf(buf, "%d\n", data->play);
  129. }
  130. static ssize_t blink1_store_play(struct device *dev,
  131. struct device_attribute *attr, const char *buf, size_t count)
  132. {
  133. struct blink1_data *data = dev_get_drvdata(dev->parent);
  134. u8 cmd[BLINK1_CMD_SIZE] = { 1, 'p', 0, 0, 0, 0, 0, 0, 0 };
  135. long unsigned int play;
  136. int ret;
  137. ret = kstrtoul(buf, 10, &play);
  138. if (ret)
  139. return ret;
  140. data->play = !!play;
  141. cmd[2] = data->play;
  142. ret = blink1_send_command(data, cmd);
  143. return ret ? ret : count;
  144. }
  145. static DEVICE_ATTR(play, S_IRUGO | S_IWUSR,
  146. blink1_show_play, blink1_store_play);
  147. static const struct attribute_group blink1_sysfs_group = {
  148. .attrs = (struct attribute *[]) {
  149. &dev_attr_rgb.attr,
  150. &dev_attr_fade.attr,
  151. &dev_attr_play.attr,
  152. NULL
  153. },
  154. };
  155. static int thingm_probe(struct hid_device *hdev, const struct hid_device_id *id)
  156. {
  157. struct blink1_data *data;
  158. struct led_classdev *led;
  159. char led_name[13];
  160. int ret;
  161. data = devm_kzalloc(&hdev->dev, sizeof(struct blink1_data), GFP_KERNEL);
  162. if (!data)
  163. return -ENOMEM;
  164. hid_set_drvdata(hdev, data);
  165. data->hdev = hdev;
  166. data->rgb = 0xFFFFFF; /* set a default white color */
  167. ret = hid_parse(hdev);
  168. if (ret)
  169. goto error;
  170. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  171. if (ret)
  172. goto error;
  173. /* blink(1) serial numbers range is 0x1A001000 to 0x1A002FFF */
  174. led = &data->led_cdev;
  175. snprintf(led_name, sizeof(led_name), "blink1::%s", hdev->uniq + 4);
  176. led->name = led_name;
  177. led->brightness_set = blink1_led_set;
  178. led->brightness_get = blink1_led_get;
  179. ret = led_classdev_register(&hdev->dev, led);
  180. if (ret)
  181. goto stop;
  182. ret = sysfs_create_group(&led->dev->kobj, &blink1_sysfs_group);
  183. if (ret)
  184. goto remove_led;
  185. return 0;
  186. remove_led:
  187. led_classdev_unregister(led);
  188. stop:
  189. hid_hw_stop(hdev);
  190. error:
  191. return ret;
  192. }
  193. static void thingm_remove(struct hid_device *hdev)
  194. {
  195. struct blink1_data *data = hid_get_drvdata(hdev);
  196. struct led_classdev *led = &data->led_cdev;
  197. sysfs_remove_group(&led->dev->kobj, &blink1_sysfs_group);
  198. led_classdev_unregister(led);
  199. hid_hw_stop(hdev);
  200. }
  201. static const struct hid_device_id thingm_table[] = {
  202. { HID_USB_DEVICE(USB_VENDOR_ID_THINGM, USB_DEVICE_ID_BLINK1) },
  203. { }
  204. };
  205. MODULE_DEVICE_TABLE(hid, thingm_table);
  206. static struct hid_driver thingm_driver = {
  207. .name = "thingm",
  208. .probe = thingm_probe,
  209. .remove = thingm_remove,
  210. .id_table = thingm_table,
  211. };
  212. module_hid_driver(thingm_driver);
  213. MODULE_LICENSE("GPL");
  214. MODULE_AUTHOR("Vivien Didelot <vivien.didelot@savoirfairelinux.com>");
  215. MODULE_DESCRIPTION("ThingM blink(1) USB RGB LED driver");