usbled.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * USB LED driver
  3. *
  4. * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/usb.h>
  17. #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com"
  18. #define DRIVER_DESC "USB LED Driver"
  19. enum led_type {
  20. DELCOM_VISUAL_SIGNAL_INDICATOR,
  21. DREAM_CHEEKY_WEBMAIL_NOTIFIER,
  22. };
  23. /* table of devices that work with this driver */
  24. static const struct usb_device_id id_table[] = {
  25. { USB_DEVICE(0x0fc5, 0x1223),
  26. .driver_info = DELCOM_VISUAL_SIGNAL_INDICATOR },
  27. { USB_DEVICE(0x1d34, 0x0004),
  28. .driver_info = DREAM_CHEEKY_WEBMAIL_NOTIFIER },
  29. { USB_DEVICE(0x1d34, 0x000a),
  30. .driver_info = DREAM_CHEEKY_WEBMAIL_NOTIFIER },
  31. { },
  32. };
  33. MODULE_DEVICE_TABLE(usb, id_table);
  34. struct usb_led {
  35. struct usb_device *udev;
  36. unsigned char blue;
  37. unsigned char red;
  38. unsigned char green;
  39. enum led_type type;
  40. };
  41. static void change_color(struct usb_led *led)
  42. {
  43. int retval = 0;
  44. unsigned char *buffer;
  45. buffer = kmalloc(8, GFP_KERNEL);
  46. if (!buffer) {
  47. dev_err(&led->udev->dev, "out of memory\n");
  48. return;
  49. }
  50. switch (led->type) {
  51. case DELCOM_VISUAL_SIGNAL_INDICATOR: {
  52. unsigned char color = 0x07;
  53. if (led->blue)
  54. color &= ~0x04;
  55. if (led->red)
  56. color &= ~0x02;
  57. if (led->green)
  58. color &= ~0x01;
  59. dev_dbg(&led->udev->dev,
  60. "blue = %d, red = %d, green = %d, color = %.2x\n",
  61. led->blue, led->red, led->green, color);
  62. retval = usb_control_msg(led->udev,
  63. usb_sndctrlpipe(led->udev, 0),
  64. 0x12,
  65. 0xc8,
  66. (0x02 * 0x100) + 0x0a,
  67. (0x00 * 0x100) + color,
  68. buffer,
  69. 8,
  70. 2000);
  71. break;
  72. }
  73. case DREAM_CHEEKY_WEBMAIL_NOTIFIER:
  74. dev_dbg(&led->udev->dev,
  75. "red = %d, green = %d, blue = %d\n",
  76. led->red, led->green, led->blue);
  77. buffer[0] = led->red;
  78. buffer[1] = led->green;
  79. buffer[2] = led->blue;
  80. buffer[3] = buffer[4] = buffer[5] = 0;
  81. buffer[6] = 0x1a;
  82. buffer[7] = 0x05;
  83. retval = usb_control_msg(led->udev,
  84. usb_sndctrlpipe(led->udev, 0),
  85. 0x09,
  86. 0x21,
  87. 0x200,
  88. 0,
  89. buffer,
  90. 8,
  91. 2000);
  92. break;
  93. default:
  94. dev_err(&led->udev->dev, "unknown device type %d\n", led->type);
  95. }
  96. if (retval)
  97. dev_dbg(&led->udev->dev, "retval = %d\n", retval);
  98. kfree(buffer);
  99. }
  100. #define show_set(value) \
  101. static ssize_t show_##value(struct device *dev, struct device_attribute *attr,\
  102. char *buf) \
  103. { \
  104. struct usb_interface *intf = to_usb_interface(dev); \
  105. struct usb_led *led = usb_get_intfdata(intf); \
  106. \
  107. return sprintf(buf, "%d\n", led->value); \
  108. } \
  109. static ssize_t set_##value(struct device *dev, struct device_attribute *attr,\
  110. const char *buf, size_t count) \
  111. { \
  112. struct usb_interface *intf = to_usb_interface(dev); \
  113. struct usb_led *led = usb_get_intfdata(intf); \
  114. int temp = simple_strtoul(buf, NULL, 10); \
  115. \
  116. led->value = temp; \
  117. change_color(led); \
  118. return count; \
  119. } \
  120. static DEVICE_ATTR(value, S_IRUGO | S_IWUSR, show_##value, set_##value);
  121. show_set(blue);
  122. show_set(red);
  123. show_set(green);
  124. static int led_probe(struct usb_interface *interface,
  125. const struct usb_device_id *id)
  126. {
  127. struct usb_device *udev = interface_to_usbdev(interface);
  128. struct usb_led *dev = NULL;
  129. int retval = -ENOMEM;
  130. dev = kzalloc(sizeof(struct usb_led), GFP_KERNEL);
  131. if (dev == NULL) {
  132. dev_err(&interface->dev, "out of memory\n");
  133. goto error_mem;
  134. }
  135. dev->udev = usb_get_dev(udev);
  136. dev->type = id->driver_info;
  137. usb_set_intfdata(interface, dev);
  138. retval = device_create_file(&interface->dev, &dev_attr_blue);
  139. if (retval)
  140. goto error;
  141. retval = device_create_file(&interface->dev, &dev_attr_red);
  142. if (retval)
  143. goto error;
  144. retval = device_create_file(&interface->dev, &dev_attr_green);
  145. if (retval)
  146. goto error;
  147. if (dev->type == DREAM_CHEEKY_WEBMAIL_NOTIFIER) {
  148. unsigned char *enable;
  149. enable = kmemdup("\x1f\x02\0\x5f\0\0\x1a\x03", 8, GFP_KERNEL);
  150. if (!enable) {
  151. dev_err(&interface->dev, "out of memory\n");
  152. retval = -ENOMEM;
  153. goto error;
  154. }
  155. retval = usb_control_msg(udev,
  156. usb_sndctrlpipe(udev, 0),
  157. 0x09,
  158. 0x21,
  159. 0x200,
  160. 0,
  161. enable,
  162. 8,
  163. 2000);
  164. kfree(enable);
  165. if (retval != 8)
  166. goto error;
  167. }
  168. dev_info(&interface->dev, "USB LED device now attached\n");
  169. return 0;
  170. error:
  171. device_remove_file(&interface->dev, &dev_attr_blue);
  172. device_remove_file(&interface->dev, &dev_attr_red);
  173. device_remove_file(&interface->dev, &dev_attr_green);
  174. usb_set_intfdata(interface, NULL);
  175. usb_put_dev(dev->udev);
  176. kfree(dev);
  177. error_mem:
  178. return retval;
  179. }
  180. static void led_disconnect(struct usb_interface *interface)
  181. {
  182. struct usb_led *dev;
  183. dev = usb_get_intfdata(interface);
  184. device_remove_file(&interface->dev, &dev_attr_blue);
  185. device_remove_file(&interface->dev, &dev_attr_red);
  186. device_remove_file(&interface->dev, &dev_attr_green);
  187. /* first remove the files, then set the pointer to NULL */
  188. usb_set_intfdata(interface, NULL);
  189. usb_put_dev(dev->udev);
  190. kfree(dev);
  191. dev_info(&interface->dev, "USB LED now disconnected\n");
  192. }
  193. static struct usb_driver led_driver = {
  194. .name = "usbled",
  195. .probe = led_probe,
  196. .disconnect = led_disconnect,
  197. .id_table = id_table,
  198. };
  199. module_usb_driver(led_driver);
  200. MODULE_AUTHOR(DRIVER_AUTHOR);
  201. MODULE_DESCRIPTION(DRIVER_DESC);
  202. MODULE_LICENSE("GPL");