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