3c93921cb6b38e59a8d00ff9b20929feac98a06e
[linux-2.6.git] / 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 = kmalloc(sizeof(struct usb_led), GFP_KERNEL);
110         if (dev == NULL) {
111                 dev_err(&interface->dev, "Out of memory\n");
112                 goto error;
113         }
114         memset (dev, 0x00, sizeof (*dev));
115
116         dev->udev = usb_get_dev(udev);
117
118         usb_set_intfdata (interface, dev);
119
120         device_create_file(&interface->dev, &dev_attr_blue);
121         device_create_file(&interface->dev, &dev_attr_red);
122         device_create_file(&interface->dev, &dev_attr_green);
123
124         dev_info(&interface->dev, "USB LED device now attached\n");
125         return 0;
126
127 error:
128         kfree(dev);
129         return retval;
130 }
131
132 static void led_disconnect(struct usb_interface *interface)
133 {
134         struct usb_led *dev;
135
136         dev = usb_get_intfdata (interface);
137         usb_set_intfdata (interface, NULL);
138
139         device_remove_file(&interface->dev, &dev_attr_blue);
140         device_remove_file(&interface->dev, &dev_attr_red);
141         device_remove_file(&interface->dev, &dev_attr_green);
142
143         usb_put_dev(dev->udev);
144
145         kfree(dev);
146
147         dev_info(&interface->dev, "USB LED now disconnected\n");
148 }
149
150 static struct usb_driver led_driver = {
151         .owner =        THIS_MODULE,
152         .name =         "usbled",
153         .probe =        led_probe,
154         .disconnect =   led_disconnect,
155         .id_table =     id_table,
156 };
157
158 static int __init usb_led_init(void)
159 {
160         int retval = 0;
161
162         retval = usb_register(&led_driver);
163         if (retval)
164                 err("usb_register failed. Error number %d", retval);
165         return retval;
166 }
167
168 static void __exit usb_led_exit(void)
169 {
170         usb_deregister(&led_driver);
171 }
172
173 module_init (usb_led_init);
174 module_exit (usb_led_exit);
175
176 MODULE_AUTHOR(DRIVER_AUTHOR);
177 MODULE_DESCRIPTION(DRIVER_DESC);
178 MODULE_LICENSE("GPL");