Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / misc / usbled.c
blob06cb71942dc7ee87e52c56c1be0410d64a707be4
1 /*
2 * USB LED driver - 1.1
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 #define VENDOR_ID 0x0fc5
24 #define PRODUCT_ID 0x1223
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 { },
31 MODULE_DEVICE_TABLE (usb, id_table);
33 struct usb_led {
34 struct usb_device * udev;
35 unsigned char blue;
36 unsigned char red;
37 unsigned char green;
40 #define BLUE 0x04
41 #define RED 0x02
42 #define GREEN 0x01
43 static void change_color(struct usb_led *led)
45 int retval;
46 unsigned char color = 0x07;
47 unsigned char *buffer;
49 buffer = kmalloc(8, GFP_KERNEL);
50 if (!buffer) {
51 dev_err(&led->udev->dev, "out of memory\n");
52 return;
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);
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,
73 2000);
74 if (retval)
75 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
76 kfree(buffer);
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); \
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); \
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);
102 static int led_probe(struct usb_interface *interface, const struct usb_device_id *id)
104 struct usb_device *udev = interface_to_usbdev(interface);
105 struct usb_led *dev = NULL;
106 int retval = -ENOMEM;
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_mem;
114 dev->udev = usb_get_dev(udev);
116 usb_set_intfdata (interface, dev);
118 retval = device_create_file(&interface->dev, &dev_attr_blue);
119 if (retval)
120 goto error;
121 retval = device_create_file(&interface->dev, &dev_attr_red);
122 if (retval)
123 goto error;
124 retval = device_create_file(&interface->dev, &dev_attr_green);
125 if (retval)
126 goto error;
128 dev_info(&interface->dev, "USB LED device now attached\n");
129 return 0;
131 error:
132 device_remove_file(&interface->dev, &dev_attr_blue);
133 device_remove_file(&interface->dev, &dev_attr_red);
134 device_remove_file(&interface->dev, &dev_attr_green);
135 usb_set_intfdata (interface, NULL);
136 usb_put_dev(dev->udev);
137 kfree(dev);
138 error_mem:
139 return retval;
142 static void led_disconnect(struct usb_interface *interface)
144 struct usb_led *dev;
146 dev = usb_get_intfdata (interface);
148 device_remove_file(&interface->dev, &dev_attr_blue);
149 device_remove_file(&interface->dev, &dev_attr_red);
150 device_remove_file(&interface->dev, &dev_attr_green);
152 /* first remove the files, then set the pointer to NULL */
153 usb_set_intfdata (interface, NULL);
155 usb_put_dev(dev->udev);
157 kfree(dev);
159 dev_info(&interface->dev, "USB LED now disconnected\n");
162 static struct usb_driver led_driver = {
163 .name = "usbled",
164 .probe = led_probe,
165 .disconnect = led_disconnect,
166 .id_table = id_table,
169 static int __init usb_led_init(void)
171 int retval = 0;
173 retval = usb_register(&led_driver);
174 if (retval)
175 err("usb_register failed. Error number %d", retval);
176 return retval;
179 static void __exit usb_led_exit(void)
181 usb_deregister(&led_driver);
184 module_init (usb_led_init);
185 module_exit (usb_led_exit);
187 MODULE_AUTHOR(DRIVER_AUTHOR);
188 MODULE_DESCRIPTION(DRIVER_DESC);
189 MODULE_LICENSE("GPL");