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