effcae6f9ec3676a910edab744d58c86118ffb13
[openwrt/openwrt.git] / target / linux / generic / files / drivers / leds / ledtrig-usbdev.c
1 /*
2 * LED USB device Trigger
3 *
4 * Toggles the LED to reflect the presence and activity of an USB device
5 * Copyright (C) Gabor Juhos <juhosg@openwrt.org>
6 *
7 * derived from ledtrig-netdev.c:
8 * Copyright 2007 Oliver Jowett <oliver@opencloud.com>
9 *
10 * ledtrig-netdev.c derived from ledtrig-timer.c:
11 * Copyright 2005-2006 Openedhand Ltd.
12 * Author: Richard Purdie <rpurdie@openedhand.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 *
18 */
19
20 #include <linux/module.h>
21 #include <linux/jiffies.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/device.h>
27 #include <linux/sysdev.h>
28 #include <linux/timer.h>
29 #include <linux/ctype.h>
30 #include <linux/slab.h>
31 #include <linux/leds.h>
32 #include <linux/usb.h>
33
34 #include "leds.h"
35
36 #define DEV_BUS_ID_SIZE 32
37
38 /*
39 * Configurable sysfs attributes:
40 *
41 * device_name - name of the USB device to monitor
42 * activity_interval - duration of LED blink, in milliseconds
43 */
44
45 struct usbdev_trig_data {
46 rwlock_t lock;
47
48 struct timer_list timer;
49 struct notifier_block notifier;
50
51 struct led_classdev *led_cdev;
52 struct usb_device *usb_dev;
53
54 char device_name[DEV_BUS_ID_SIZE];
55 unsigned interval;
56 int last_urbnum;
57 };
58
59 static void usbdev_trig_update_state(struct usbdev_trig_data *td)
60 {
61 if (td->usb_dev)
62 led_set_brightness(td->led_cdev, LED_FULL);
63 else
64 led_set_brightness(td->led_cdev, LED_OFF);
65
66 if (td->interval && td->usb_dev)
67 mod_timer(&td->timer, jiffies + td->interval);
68 else
69 del_timer(&td->timer);
70 }
71
72 static ssize_t usbdev_trig_name_show(struct device *dev,
73 struct device_attribute *attr,
74 char *buf)
75 {
76 struct led_classdev *led_cdev = dev_get_drvdata(dev);
77 struct usbdev_trig_data *td = led_cdev->trigger_data;
78
79 read_lock(&td->lock);
80 sprintf(buf, "%s\n", td->device_name);
81 read_unlock(&td->lock);
82
83 return strlen(buf) + 1;
84 }
85
86 struct usbdev_trig_match {
87 char *device_name;
88 struct usb_device *usb_dev;
89 };
90
91 static int usbdev_trig_find_usb_dev(struct usb_device *usb_dev, void *data)
92 {
93 struct usbdev_trig_match *match = data;
94
95 if (WARN_ON(match->usb_dev))
96 return 0;
97
98 if (!strcmp(dev_name(&usb_dev->dev), match->device_name)) {
99 dev_dbg(&usb_dev->dev, "matched this device!\n");
100 match->usb_dev = usb_get_dev(usb_dev);
101 }
102
103 return 0;
104 }
105
106 static ssize_t usbdev_trig_name_store(struct device *dev,
107 struct device_attribute *attr,
108 const char *buf,
109 size_t size)
110 {
111 struct led_classdev *led_cdev = dev_get_drvdata(dev);
112 struct usbdev_trig_data *td = led_cdev->trigger_data;
113
114 if (size < 0 || size >= DEV_BUS_ID_SIZE)
115 return -EINVAL;
116
117 write_lock(&td->lock);
118
119 strcpy(td->device_name, buf);
120 if (size > 0 && td->device_name[size - 1] == '\n')
121 td->device_name[size - 1] = 0;
122
123 if (td->device_name[0] != 0) {
124 struct usbdev_trig_match match = {
125 .device_name = td->device_name,
126 };
127
128 /* check for existing device to update from */
129 usb_for_each_dev(&match, usbdev_trig_find_usb_dev);
130 if (match.usb_dev) {
131 if (td->usb_dev)
132 usb_put_dev(td->usb_dev);
133
134 td->usb_dev = match.usb_dev;
135 td->last_urbnum = atomic_read(&match.usb_dev->urbnum);
136 }
137
138 /* updates LEDs, may start timers */
139 usbdev_trig_update_state(td);
140 }
141
142 write_unlock(&td->lock);
143 return size;
144 }
145
146 static DEVICE_ATTR(device_name, 0644, usbdev_trig_name_show,
147 usbdev_trig_name_store);
148
149 static ssize_t usbdev_trig_interval_show(struct device *dev,
150 struct device_attribute *attr,
151 char *buf)
152 {
153 struct led_classdev *led_cdev = dev_get_drvdata(dev);
154 struct usbdev_trig_data *td = led_cdev->trigger_data;
155
156 read_lock(&td->lock);
157 sprintf(buf, "%u\n", jiffies_to_msecs(td->interval));
158 read_unlock(&td->lock);
159
160 return strlen(buf) + 1;
161 }
162
163 static ssize_t usbdev_trig_interval_store(struct device *dev,
164 struct device_attribute *attr,
165 const char *buf,
166 size_t size)
167 {
168 struct led_classdev *led_cdev = dev_get_drvdata(dev);
169 struct usbdev_trig_data *td = led_cdev->trigger_data;
170 int ret = -EINVAL;
171 char *after;
172 unsigned long value = simple_strtoul(buf, &after, 10);
173 size_t count = after - buf;
174
175 if (*after && isspace(*after))
176 count++;
177
178 if (count == size && value <= 10000) {
179 write_lock(&td->lock);
180 td->interval = msecs_to_jiffies(value);
181 usbdev_trig_update_state(td); /* resets timer */
182 write_unlock(&td->lock);
183 ret = count;
184 }
185
186 return ret;
187 }
188
189 static DEVICE_ATTR(activity_interval, 0644, usbdev_trig_interval_show,
190 usbdev_trig_interval_store);
191
192 static int usbdev_trig_notify(struct notifier_block *nb,
193 unsigned long evt,
194 void *data)
195 {
196 struct usb_device *usb_dev;
197 struct usbdev_trig_data *td;
198
199 if (evt != USB_DEVICE_ADD && evt != USB_DEVICE_REMOVE)
200 return NOTIFY_DONE;
201
202 usb_dev = data;
203 td = container_of(nb, struct usbdev_trig_data, notifier);
204
205 write_lock(&td->lock);
206
207 if (strcmp(dev_name(&usb_dev->dev), td->device_name))
208 goto done;
209
210 if (evt == USB_DEVICE_ADD) {
211 usb_get_dev(usb_dev);
212 if (td->usb_dev != NULL)
213 usb_put_dev(td->usb_dev);
214 td->usb_dev = usb_dev;
215 td->last_urbnum = atomic_read(&usb_dev->urbnum);
216 } else if (evt == USB_DEVICE_REMOVE) {
217 if (td->usb_dev != NULL) {
218 usb_put_dev(td->usb_dev);
219 td->usb_dev = NULL;
220 }
221 }
222
223 usbdev_trig_update_state(td);
224
225 done:
226 write_unlock(&td->lock);
227 return NOTIFY_DONE;
228 }
229
230 /* here's the real work! */
231 static void usbdev_trig_timer(unsigned long arg)
232 {
233 struct usbdev_trig_data *td = (struct usbdev_trig_data *)arg;
234 int new_urbnum;
235
236 write_lock(&td->lock);
237
238 if (!td->usb_dev || td->interval == 0) {
239 /*
240 * we don't need to do timer work, just reflect device presence
241 */
242 if (td->usb_dev)
243 led_set_brightness(td->led_cdev, LED_FULL);
244 else
245 led_set_brightness(td->led_cdev, LED_OFF);
246
247 goto no_restart;
248 }
249
250 if (td->interval)
251 new_urbnum = atomic_read(&td->usb_dev->urbnum);
252 else
253 new_urbnum = 0;
254
255 if (td->usb_dev) {
256 /*
257 * Base state is ON (device is present). If there's no device,
258 * we don't get this far and the LED is off.
259 * OFF -> ON always
260 * ON -> OFF on activity
261 */
262 if (td->led_cdev->brightness == LED_OFF)
263 led_set_brightness(td->led_cdev, LED_FULL);
264 else if (td->last_urbnum != new_urbnum)
265 led_set_brightness(td->led_cdev, LED_OFF);
266 } else {
267 /*
268 * base state is OFF
269 * ON -> OFF always
270 * OFF -> ON on activity
271 */
272 if (td->led_cdev->brightness == LED_FULL)
273 led_set_brightness(td->led_cdev, LED_OFF);
274 else if (td->last_urbnum != new_urbnum)
275 led_set_brightness(td->led_cdev, LED_FULL);
276 }
277
278 td->last_urbnum = new_urbnum;
279 mod_timer(&td->timer, jiffies + td->interval);
280
281 no_restart:
282 write_unlock(&td->lock);
283 }
284
285 static void usbdev_trig_activate(struct led_classdev *led_cdev)
286 {
287 struct usbdev_trig_data *td;
288 int rc;
289
290 td = kzalloc(sizeof(struct usbdev_trig_data), GFP_KERNEL);
291 if (!td)
292 return;
293
294 rwlock_init(&td->lock);
295
296 td->notifier.notifier_call = usbdev_trig_notify;
297 td->notifier.priority = 10;
298
299 setup_timer(&td->timer, usbdev_trig_timer, (unsigned long) td);
300
301 td->led_cdev = led_cdev;
302 td->interval = msecs_to_jiffies(50);
303
304 led_cdev->trigger_data = td;
305
306 rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
307 if (rc)
308 goto err_out;
309
310 rc = device_create_file(led_cdev->dev, &dev_attr_activity_interval);
311 if (rc)
312 goto err_out_device_name;
313
314 usb_register_notify(&td->notifier);
315 return;
316
317 err_out_device_name:
318 device_remove_file(led_cdev->dev, &dev_attr_device_name);
319 err_out:
320 led_cdev->trigger_data = NULL;
321 kfree(td);
322 }
323
324 static void usbdev_trig_deactivate(struct led_classdev *led_cdev)
325 {
326 struct usbdev_trig_data *td = led_cdev->trigger_data;
327
328 if (td) {
329 usb_unregister_notify(&td->notifier);
330
331 device_remove_file(led_cdev->dev, &dev_attr_device_name);
332 device_remove_file(led_cdev->dev, &dev_attr_activity_interval);
333
334 write_lock(&td->lock);
335
336 if (td->usb_dev) {
337 usb_put_dev(td->usb_dev);
338 td->usb_dev = NULL;
339 }
340
341 write_unlock(&td->lock);
342
343 del_timer_sync(&td->timer);
344
345 kfree(td);
346 }
347 }
348
349 static struct led_trigger usbdev_led_trigger = {
350 .name = "usbdev",
351 .activate = usbdev_trig_activate,
352 .deactivate = usbdev_trig_deactivate,
353 };
354
355 static int __init usbdev_trig_init(void)
356 {
357 return led_trigger_register(&usbdev_led_trigger);
358 }
359
360 static void __exit usbdev_trig_exit(void)
361 {
362 led_trigger_unregister(&usbdev_led_trigger);
363 }
364
365 module_init(usbdev_trig_init);
366 module_exit(usbdev_trig_exit);
367
368 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
369 MODULE_DESCRIPTION("USB device LED trigger");
370 MODULE_LICENSE("GPL v2");