2 * LED USB device Trigger
4 * Toggles the LED to reflect the presence and activity of an USB device
5 * Copyright (C) Gabor Juhos <juhosg@openwrt.org>
7 * derived from ledtrig-netdev.c:
8 * Copyright 2007 Oliver Jowett <oliver@opencloud.com>
10 * ledtrig-netdev.c derived from ledtrig-timer.c:
11 * Copyright 2005-2006 Openedhand Ltd.
12 * Author: Richard Purdie <rpurdie@openedhand.com>
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.
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>
36 #define DEV_BUS_ID_SIZE 32
39 * Configurable sysfs attributes:
41 * device_name - name of the USB device to monitor
42 * activity_interval - duration of LED blink, in milliseconds
45 struct usbdev_trig_data {
48 struct timer_list timer;
49 struct notifier_block notifier;
51 struct led_classdev *led_cdev;
52 struct usb_device *usb_dev;
54 char device_name[DEV_BUS_ID_SIZE];
59 static void usbdev_trig_update_state(struct usbdev_trig_data *td)
62 led_set_brightness(td->led_cdev, LED_FULL);
64 led_set_brightness(td->led_cdev, LED_OFF);
66 if (td->interval && td->usb_dev)
67 mod_timer(&td->timer, jiffies + td->interval);
69 del_timer(&td->timer);
72 static ssize_t usbdev_trig_name_show(struct device *dev,
73 struct device_attribute *attr,
76 struct led_classdev *led_cdev = dev_get_drvdata(dev);
77 struct usbdev_trig_data *td = led_cdev->trigger_data;
80 sprintf(buf, "%s\n", td->device_name);
81 read_unlock(&td->lock);
83 return strlen(buf) + 1;
86 struct usbdev_trig_match {
88 struct usb_device *usb_dev;
91 static int usbdev_trig_find_usb_dev(struct usb_device *usb_dev, void *data)
93 struct usbdev_trig_match *match = data;
95 if (WARN_ON(match->usb_dev))
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);
106 static ssize_t usbdev_trig_name_store(struct device *dev,
107 struct device_attribute *attr,
111 struct led_classdev *led_cdev = dev_get_drvdata(dev);
112 struct usbdev_trig_data *td = led_cdev->trigger_data;
114 if (size < 0 || size >= DEV_BUS_ID_SIZE)
117 write_lock(&td->lock);
119 strcpy(td->device_name, buf);
120 if (size > 0 && td->device_name[size - 1] == '\n')
121 td->device_name[size - 1] = 0;
123 if (td->device_name[0] != 0) {
124 struct usbdev_trig_match match = {
125 .device_name = td->device_name,
128 /* check for existing device to update from */
129 usb_for_each_dev(&match, usbdev_trig_find_usb_dev);
132 usb_put_dev(td->usb_dev);
134 td->usb_dev = match.usb_dev;
135 td->last_urbnum = atomic_read(&match.usb_dev->urbnum);
138 /* updates LEDs, may start timers */
139 usbdev_trig_update_state(td);
142 write_unlock(&td->lock);
146 static DEVICE_ATTR(device_name, 0644, usbdev_trig_name_show,
147 usbdev_trig_name_store);
149 static ssize_t usbdev_trig_interval_show(struct device *dev,
150 struct device_attribute *attr,
153 struct led_classdev *led_cdev = dev_get_drvdata(dev);
154 struct usbdev_trig_data *td = led_cdev->trigger_data;
156 read_lock(&td->lock);
157 sprintf(buf, "%u\n", jiffies_to_msecs(td->interval));
158 read_unlock(&td->lock);
160 return strlen(buf) + 1;
163 static ssize_t usbdev_trig_interval_store(struct device *dev,
164 struct device_attribute *attr,
168 struct led_classdev *led_cdev = dev_get_drvdata(dev);
169 struct usbdev_trig_data *td = led_cdev->trigger_data;
172 unsigned long value = simple_strtoul(buf, &after, 10);
173 size_t count = after - buf;
175 if (*after && isspace(*after))
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);
189 static DEVICE_ATTR(activity_interval, 0644, usbdev_trig_interval_show,
190 usbdev_trig_interval_store);
192 static int usbdev_trig_notify(struct notifier_block *nb,
196 struct usb_device *usb_dev;
197 struct usbdev_trig_data *td;
199 if (evt != USB_DEVICE_ADD && evt != USB_DEVICE_REMOVE)
203 td = container_of(nb, struct usbdev_trig_data, notifier);
205 write_lock(&td->lock);
207 if (strcmp(dev_name(&usb_dev->dev), td->device_name))
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);
223 usbdev_trig_update_state(td);
226 write_unlock(&td->lock);
230 /* here's the real work! */
231 static void usbdev_trig_timer(unsigned long arg)
233 struct usbdev_trig_data *td = (struct usbdev_trig_data *)arg;
236 write_lock(&td->lock);
238 if (!td->usb_dev || td->interval == 0) {
240 * we don't need to do timer work, just reflect device presence
243 led_set_brightness(td->led_cdev, LED_FULL);
245 led_set_brightness(td->led_cdev, LED_OFF);
251 new_urbnum = atomic_read(&td->usb_dev->urbnum);
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.
260 * ON -> OFF on activity
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);
270 * OFF -> ON on activity
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);
278 td->last_urbnum = new_urbnum;
279 mod_timer(&td->timer, jiffies + td->interval);
282 write_unlock(&td->lock);
285 static void usbdev_trig_activate(struct led_classdev *led_cdev)
287 struct usbdev_trig_data *td;
290 td = kzalloc(sizeof(struct usbdev_trig_data), GFP_KERNEL);
294 rwlock_init(&td->lock);
296 td->notifier.notifier_call = usbdev_trig_notify;
297 td->notifier.priority = 10;
299 setup_timer(&td->timer, usbdev_trig_timer, (unsigned long) td);
301 td->led_cdev = led_cdev;
302 td->interval = msecs_to_jiffies(50);
304 led_cdev->trigger_data = td;
306 rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
310 rc = device_create_file(led_cdev->dev, &dev_attr_activity_interval);
312 goto err_out_device_name;
314 usb_register_notify(&td->notifier);
318 device_remove_file(led_cdev->dev, &dev_attr_device_name);
320 led_cdev->trigger_data = NULL;
324 static void usbdev_trig_deactivate(struct led_classdev *led_cdev)
326 struct usbdev_trig_data *td = led_cdev->trigger_data;
329 usb_unregister_notify(&td->notifier);
331 device_remove_file(led_cdev->dev, &dev_attr_device_name);
332 device_remove_file(led_cdev->dev, &dev_attr_activity_interval);
334 write_lock(&td->lock);
337 usb_put_dev(td->usb_dev);
341 write_unlock(&td->lock);
343 del_timer_sync(&td->timer);
349 static struct led_trigger usbdev_led_trigger = {
351 .activate = usbdev_trig_activate,
352 .deactivate = usbdev_trig_deactivate,
355 static int __init usbdev_trig_init(void)
357 return led_trigger_register(&usbdev_led_trigger);
360 static void __exit usbdev_trig_exit(void)
362 led_trigger_unregister(&usbdev_led_trigger);
365 module_init(usbdev_trig_init);
366 module_exit(usbdev_trig_exit);
368 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
369 MODULE_DESCRIPTION("USB device LED trigger");
370 MODULE_LICENSE("GPL v2");