Merge branch 'upstream'
[linux-2.6] / drivers / usb / misc / usbled.c
1 /*
2  * USB LED driver - 1.1
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
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19
20
21 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com"
22 #define DRIVER_DESC "USB LED Driver"
23
24 #define VENDOR_ID       0x0fc5
25 #define PRODUCT_ID      0x1223
26
27 /* table of devices that work with this driver */
28 static struct usb_device_id id_table [] = {
29         { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
30         { },
31 };
32 MODULE_DEVICE_TABLE (usb, id_table);
33
34 struct usb_led {
35         struct usb_device *     udev;
36         unsigned char           blue;
37         unsigned char           red;
38         unsigned char           green;
39 };
40
41 #define BLUE    0x04
42 #define RED     0x02
43 #define GREEN   0x01
44 static void change_color(struct usb_led *led)
45 {
46         int retval;
47         unsigned char color = 0x07;
48         unsigned char *buffer;
49
50         buffer = kmalloc(8, GFP_KERNEL);
51         if (!buffer) {
52                 dev_err(&led->udev->dev, "out of memory\n");
53                 return;
54         }
55
56         if (led->blue)
57                 color &= ~(BLUE);
58         if (led->red)
59                 color &= ~(RED);
60         if (led->green)
61                 color &= ~(GREEN);
62         dev_dbg(&led->udev->dev,
63                 "blue = %d, red = %d, green = %d, color = %.2x\n",
64                 led->blue, led->red, led->green, color);
65
66         retval = usb_control_msg(led->udev,
67                                 usb_sndctrlpipe(led->udev, 0),
68                                 0x12,
69                                 0xc8,
70                                 (0x02 * 0x100) + 0x0a,
71                                 (0x00 * 0x100) + color,
72                                 buffer, 
73                                 8,
74                                 2000);
75         if (retval)
76                 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
77         kfree(buffer);
78 }
79
80 #define show_set(value) \
81 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)               \
82 {                                                                       \
83         struct usb_interface *intf = to_usb_interface(dev);             \
84         struct usb_led *led = usb_get_intfdata(intf);                   \
85                                                                         \
86         return sprintf(buf, "%d\n", led->value);                        \
87 }                                                                       \
88 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)    \
89 {                                                                       \
90         struct usb_interface *intf = to_usb_interface(dev);             \
91         struct usb_led *led = usb_get_intfdata(intf);                   \
92         int temp = simple_strtoul(buf, NULL, 10);                       \
93                                                                         \
94         led->value = temp;                                              \
95         change_color(led);                                              \
96         return count;                                                   \
97 }                                                                       \
98 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
99 show_set(blue);
100 show_set(red);
101 show_set(green);
102
103 static int led_probe(struct usb_interface *interface, const struct usb_device_id *id)
104 {
105         struct usb_device *udev = interface_to_usbdev(interface);
106         struct usb_led *dev = NULL;
107         int retval = -ENOMEM;
108
109         dev = kzalloc(sizeof(struct usb_led), GFP_KERNEL);
110         if (dev == NULL) {
111                 dev_err(&interface->dev, "Out of memory\n");
112                 goto error;
113         }
114
115         dev->udev = usb_get_dev(udev);
116
117         usb_set_intfdata (interface, dev);
118
119         device_create_file(&interface->dev, &dev_attr_blue);
120         device_create_file(&interface->dev, &dev_attr_red);
121         device_create_file(&interface->dev, &dev_attr_green);
122
123         dev_info(&interface->dev, "USB LED device now attached\n");
124         return 0;
125
126 error:
127         kfree(dev);
128         return retval;
129 }
130
131 static void led_disconnect(struct usb_interface *interface)
132 {
133         struct usb_led *dev;
134
135         dev = usb_get_intfdata (interface);
136         usb_set_intfdata (interface, NULL);
137
138         device_remove_file(&interface->dev, &dev_attr_blue);
139         device_remove_file(&interface->dev, &dev_attr_red);
140         device_remove_file(&interface->dev, &dev_attr_green);
141
142         usb_put_dev(dev->udev);
143
144         kfree(dev);
145
146         dev_info(&interface->dev, "USB LED now disconnected\n");
147 }
148
149 static struct usb_driver led_driver = {
150         .name =         "usbled",
151         .probe =        led_probe,
152         .disconnect =   led_disconnect,
153         .id_table =     id_table,
154 };
155
156 static int __init usb_led_init(void)
157 {
158         int retval = 0;
159
160         retval = usb_register(&led_driver);
161         if (retval)
162                 err("usb_register failed. Error number %d", retval);
163         return retval;
164 }
165
166 static void __exit usb_led_exit(void)
167 {
168         usb_deregister(&led_driver);
169 }
170
171 module_init (usb_led_init);
172 module_exit (usb_led_exit);
173
174 MODULE_AUTHOR(DRIVER_AUTHOR);
175 MODULE_DESCRIPTION(DRIVER_DESC);
176 MODULE_LICENSE("GPL");