blob: 1616ad1793a4ac28970e33a5b0a58efd0fcb56a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Melchior FRANZ73bc7d32010-12-22 02:04:33 +01002 * USB LED driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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
Melchior FRANZ73bc7d32010-12-22 02:04:33 +010023enum led_type {
24 DELCOM_VISUAL_SIGNAL_INDICATOR,
25 DREAM_CHEEKY_WEBMAIL_NOTIFIER,
26};
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/* table of devices that work with this driver */
Németh Márton33b9e162010-01-10 15:34:45 +010029static const struct usb_device_id id_table[] = {
Melchior FRANZ73bc7d32010-12-22 02:04:33 +010030 { USB_DEVICE(0x0fc5, 0x1223),
31 .driver_info = DELCOM_VISUAL_SIGNAL_INDICATOR },
32 { USB_DEVICE(0x1d34, 0x0004),
33 .driver_info = DREAM_CHEEKY_WEBMAIL_NOTIFIER },
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 { },
35};
36MODULE_DEVICE_TABLE (usb, id_table);
37
38struct usb_led {
39 struct usb_device * udev;
40 unsigned char blue;
41 unsigned char red;
42 unsigned char green;
Melchior FRANZ73bc7d32010-12-22 02:04:33 +010043 enum led_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static void change_color(struct usb_led *led)
47{
Melchior FRANZ952eca02010-12-22 13:55:24 +010048 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned char *buffer;
50
51 buffer = kmalloc(8, GFP_KERNEL);
52 if (!buffer) {
53 dev_err(&led->udev->dev, "out of memory\n");
54 return;
55 }
56
Melchior FRANZ73bc7d32010-12-22 02:04:33 +010057 switch (led->type) {
58 case DELCOM_VISUAL_SIGNAL_INDICATOR: {
59 unsigned char color = 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Melchior FRANZ73bc7d32010-12-22 02:04:33 +010061 if (led->blue)
62 color &= ~0x04;
63 if (led->red)
64 color &= ~0x02;
65 if (led->green)
66 color &= ~0x01;
67 dev_dbg(&led->udev->dev,
68 "blue = %d, red = %d, green = %d, color = %.2x\n",
69 led->blue, led->red, led->green, color);
70
71 retval = usb_control_msg(led->udev,
72 usb_sndctrlpipe(led->udev, 0),
73 0x12,
74 0xc8,
75 (0x02 * 0x100) + 0x0a,
76 (0x00 * 0x100) + color,
77 buffer,
78 8,
79 2000);
80 break;
81 }
82
83 case DREAM_CHEEKY_WEBMAIL_NOTIFIER:
84 dev_dbg(&led->udev->dev,
85 "red = %d, green = %d, blue = %d\n",
86 led->red, led->green, led->blue);
87
88 buffer[0] = led->red;
89 buffer[1] = led->green;
90 buffer[2] = led->blue;
91 buffer[3] = buffer[4] = buffer[5] = 0;
92 buffer[6] = 0x1a;
93 buffer[7] = 0x05;
94
95 retval = usb_control_msg(led->udev,
96 usb_sndctrlpipe(led->udev, 0),
97 0x09,
98 0x21,
99 0x200,
100 0,
101 buffer,
102 8,
103 2000);
104 break;
105
106 default:
107 dev_err(&led->udev->dev, "unknown device type %d\n", led->type);
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (retval)
111 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
112 kfree(buffer);
113}
114
115#define show_set(value) \
Yani Ioannou060b8842005-05-17 06:44:04 -0400116static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{ \
118 struct usb_interface *intf = to_usb_interface(dev); \
119 struct usb_led *led = usb_get_intfdata(intf); \
120 \
121 return sprintf(buf, "%d\n", led->value); \
122} \
Yani Ioannou060b8842005-05-17 06:44:04 -0400123static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{ \
125 struct usb_interface *intf = to_usb_interface(dev); \
126 struct usb_led *led = usb_get_intfdata(intf); \
127 int temp = simple_strtoul(buf, NULL, 10); \
128 \
129 led->value = temp; \
130 change_color(led); \
131 return count; \
132} \
Greg Kroah-Hartman48f11542010-11-15 11:35:49 -0800133static DEVICE_ATTR(value, S_IRUGO | S_IWUSR, show_##value, set_##value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134show_set(blue);
135show_set(red);
136show_set(green);
137
138static int led_probe(struct usb_interface *interface, const struct usb_device_id *id)
139{
140 struct usb_device *udev = interface_to_usbdev(interface);
141 struct usb_led *dev = NULL;
142 int retval = -ENOMEM;
143
Oliver Neukum06d69472006-01-06 22:44:52 +0100144 dev = kzalloc(sizeof(struct usb_led), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (dev == NULL) {
Melchior FRANZ73bc7d32010-12-22 02:04:33 +0100146 dev_err(&interface->dev, "out of memory\n");
Greg Kroah-Hartman4d42e1b2006-08-28 11:43:25 -0700147 goto error_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 dev->udev = usb_get_dev(udev);
Melchior FRANZ73bc7d32010-12-22 02:04:33 +0100151 dev->type = id->driver_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 usb_set_intfdata (interface, dev);
154
Greg Kroah-Hartman4d42e1b2006-08-28 11:43:25 -0700155 retval = device_create_file(&interface->dev, &dev_attr_blue);
156 if (retval)
157 goto error;
158 retval = device_create_file(&interface->dev, &dev_attr_red);
159 if (retval)
160 goto error;
161 retval = device_create_file(&interface->dev, &dev_attr_green);
162 if (retval)
163 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Melchior FRANZ73bc7d32010-12-22 02:04:33 +0100165 if (dev->type == DREAM_CHEEKY_WEBMAIL_NOTIFIER) {
166 unsigned char *enable;
167
168 enable = kmemdup("\x1f\x02\0\x5f\0\0\x1a\x03", 8, GFP_KERNEL);
169 if (!enable) {
170 dev_err(&interface->dev, "out of memory\n");
171 retval = -ENOMEM;
172 goto error;
173 }
174
175 retval = usb_control_msg(udev,
176 usb_sndctrlpipe(udev, 0),
177 0x09,
178 0x21,
179 0x200,
180 0,
181 enable,
182 8,
183 2000);
184
185 kfree(enable);
186 if (retval != 8)
187 goto error;
188 }
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 dev_info(&interface->dev, "USB LED device now attached\n");
191 return 0;
192
193error:
Greg Kroah-Hartman4d42e1b2006-08-28 11:43:25 -0700194 device_remove_file(&interface->dev, &dev_attr_blue);
195 device_remove_file(&interface->dev, &dev_attr_red);
196 device_remove_file(&interface->dev, &dev_attr_green);
197 usb_set_intfdata (interface, NULL);
198 usb_put_dev(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 kfree(dev);
Greg Kroah-Hartman4d42e1b2006-08-28 11:43:25 -0700200error_mem:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return retval;
202}
203
204static void led_disconnect(struct usb_interface *interface)
205{
206 struct usb_led *dev;
207
208 dev = usb_get_intfdata (interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 device_remove_file(&interface->dev, &dev_attr_blue);
211 device_remove_file(&interface->dev, &dev_attr_red);
212 device_remove_file(&interface->dev, &dev_attr_green);
213
Oliver Neukumed206ec2007-10-28 08:21:59 +0100214 /* first remove the files, then set the pointer to NULL */
215 usb_set_intfdata (interface, NULL);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 usb_put_dev(dev->udev);
218
219 kfree(dev);
220
221 dev_info(&interface->dev, "USB LED now disconnected\n");
222}
223
224static struct usb_driver led_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .name = "usbled",
226 .probe = led_probe,
227 .disconnect = led_disconnect,
228 .id_table = id_table,
229};
230
231static int __init usb_led_init(void)
232{
233 int retval = 0;
234
235 retval = usb_register(&led_driver);
236 if (retval)
237 err("usb_register failed. Error number %d", retval);
238 return retval;
239}
240
241static void __exit usb_led_exit(void)
242{
243 usb_deregister(&led_driver);
244}
245
246module_init (usb_led_init);
247module_exit (usb_led_exit);
248
249MODULE_AUTHOR(DRIVER_AUTHOR);
250MODULE_DESCRIPTION(DRIVER_DESC);
251MODULE_LICENSE("GPL");