diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/usb/misc/usbled.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/usb/misc/usbled.c')
-rw-r--r-- | drivers/usb/misc/usbled.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/drivers/usb/misc/usbled.c b/drivers/usb/misc/usbled.c new file mode 100644 index 000000000000..ee329d5e1c5e --- /dev/null +++ b/drivers/usb/misc/usbled.c | |||
@@ -0,0 +1,181 @@ | |||
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, 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, 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"); | ||