Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / usb / misc / usbled.c
blob43f84e50d514bc4667b93e52f807a7448c305e10
1 /*
2 * USB LED driver
4 * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com)
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.
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/usb.h>
20 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com"
21 #define DRIVER_DESC "USB LED Driver"
23 enum led_type {
24 DELCOM_VISUAL_SIGNAL_INDICATOR,
25 DREAM_CHEEKY_WEBMAIL_NOTIFIER,
28 /* table of devices that work with this driver */
29 static const struct usb_device_id id_table[] = {
30 { USB_DEVICE(0x0fc5, 0x1223),
31 .driver_info = DELCOM_VISUAL_SIGNAL_INDICATOR },
32 { USB_DEVICE(0x1d34, 0x0004),
33 .driver_info = DREAM_CHEEKY_WEBMAIL_NOTIFIER },
34 { },
36 MODULE_DEVICE_TABLE(usb, id_table);
38 struct usb_led {
39 struct usb_device *udev;
40 unsigned char blue;
41 unsigned char red;
42 unsigned char green;
43 enum led_type type;
46 static void change_color(struct usb_led *led)
48 int retval = 0;
49 unsigned char *buffer;
51 buffer = kmalloc(8, GFP_KERNEL);
52 if (!buffer) {
53 dev_err(&led->udev->dev, "out of memory\n");
54 return;
57 switch (led->type) {
58 case DELCOM_VISUAL_SIGNAL_INDICATOR: {
59 unsigned char color = 0x07;
61 if (led->blue)
62 color &= ~0x04;
63 if (led->red)
64 color &= ~0x02;
65 if (led->green)
66 color &= ~0x01;
67 dev_dbg(&led->udev->dev,
68 "blue = %d, red = %d, green = %d, color = %.2x\n",
69 led->blue, led->red, led->green, color);
71 retval = usb_control_msg(led->udev,
72 usb_sndctrlpipe(led->udev, 0),
73 0x12,
74 0xc8,
75 (0x02 * 0x100) + 0x0a,
76 (0x00 * 0x100) + color,
77 buffer,
79 2000);
80 break;
83 case DREAM_CHEEKY_WEBMAIL_NOTIFIER:
84 dev_dbg(&led->udev->dev,
85 "red = %d, green = %d, blue = %d\n",
86 led->red, led->green, led->blue);
88 buffer[0] = led->red;
89 buffer[1] = led->green;
90 buffer[2] = led->blue;
91 buffer[3] = buffer[4] = buffer[5] = 0;
92 buffer[6] = 0x1a;
93 buffer[7] = 0x05;
95 retval = usb_control_msg(led->udev,
96 usb_sndctrlpipe(led->udev, 0),
97 0x09,
98 0x21,
99 0x200,
101 buffer,
103 2000);
104 break;
106 default:
107 dev_err(&led->udev->dev, "unknown device type %d\n", led->type);
110 if (retval)
111 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
112 kfree(buffer);
115 #define show_set(value) \
116 static ssize_t show_##value(struct device *dev, struct device_attribute *attr,\
117 char *buf) \
119 struct usb_interface *intf = to_usb_interface(dev); \
120 struct usb_led *led = usb_get_intfdata(intf); \
122 return sprintf(buf, "%d\n", led->value); \
124 static ssize_t set_##value(struct device *dev, struct device_attribute *attr,\
125 const char *buf, size_t count) \
127 struct usb_interface *intf = to_usb_interface(dev); \
128 struct usb_led *led = usb_get_intfdata(intf); \
129 int temp = simple_strtoul(buf, NULL, 10); \
131 led->value = temp; \
132 change_color(led); \
133 return count; \
135 static DEVICE_ATTR(value, S_IRUGO | S_IWUSR, show_##value, set_##value);
136 show_set(blue);
137 show_set(red);
138 show_set(green);
140 static int led_probe(struct usb_interface *interface,
141 const struct usb_device_id *id)
143 struct usb_device *udev = interface_to_usbdev(interface);
144 struct usb_led *dev = NULL;
145 int retval = -ENOMEM;
147 dev = kzalloc(sizeof(struct usb_led), GFP_KERNEL);
148 if (dev == NULL) {
149 dev_err(&interface->dev, "out of memory\n");
150 goto error_mem;
153 dev->udev = usb_get_dev(udev);
154 dev->type = id->driver_info;
156 usb_set_intfdata(interface, dev);
158 retval = device_create_file(&interface->dev, &dev_attr_blue);
159 if (retval)
160 goto error;
161 retval = device_create_file(&interface->dev, &dev_attr_red);
162 if (retval)
163 goto error;
164 retval = device_create_file(&interface->dev, &dev_attr_green);
165 if (retval)
166 goto error;
168 if (dev->type == DREAM_CHEEKY_WEBMAIL_NOTIFIER) {
169 unsigned char *enable;
171 enable = kmemdup("\x1f\x02\0\x5f\0\0\x1a\x03", 8, GFP_KERNEL);
172 if (!enable) {
173 dev_err(&interface->dev, "out of memory\n");
174 retval = -ENOMEM;
175 goto error;
178 retval = usb_control_msg(udev,
179 usb_sndctrlpipe(udev, 0),
180 0x09,
181 0x21,
182 0x200,
184 enable,
186 2000);
188 kfree(enable);
189 if (retval != 8)
190 goto error;
193 dev_info(&interface->dev, "USB LED device now attached\n");
194 return 0;
196 error:
197 device_remove_file(&interface->dev, &dev_attr_blue);
198 device_remove_file(&interface->dev, &dev_attr_red);
199 device_remove_file(&interface->dev, &dev_attr_green);
200 usb_set_intfdata(interface, NULL);
201 usb_put_dev(dev->udev);
202 kfree(dev);
203 error_mem:
204 return retval;
207 static void led_disconnect(struct usb_interface *interface)
209 struct usb_led *dev;
211 dev = usb_get_intfdata(interface);
213 device_remove_file(&interface->dev, &dev_attr_blue);
214 device_remove_file(&interface->dev, &dev_attr_red);
215 device_remove_file(&interface->dev, &dev_attr_green);
217 /* first remove the files, then set the pointer to NULL */
218 usb_set_intfdata(interface, NULL);
220 usb_put_dev(dev->udev);
222 kfree(dev);
224 dev_info(&interface->dev, "USB LED now disconnected\n");
227 static struct usb_driver led_driver = {
228 .name = "usbled",
229 .probe = led_probe,
230 .disconnect = led_disconnect,
231 .id_table = id_table,
234 static int __init usb_led_init(void)
236 int retval = 0;
238 retval = usb_register(&led_driver);
239 if (retval)
240 err("usb_register failed. Error number %d", retval);
241 return retval;
244 static void __exit usb_led_exit(void)
246 usb_deregister(&led_driver);
249 module_init(usb_led_init);
250 module_exit(usb_led_exit);
252 MODULE_AUTHOR(DRIVER_AUTHOR);
253 MODULE_DESCRIPTION(DRIVER_DESC);
254 MODULE_LICENSE("GPL");