hid-thingm.c 6.4 KB

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