ledtrig-netdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * LED Kernel Netdev Trigger
  3. *
  4. * Toggles the LED to reflect the link and traffic state of a named net device
  5. *
  6. * Copyright 2007 Oliver Jowett <oliver@opencloud.com>
  7. *
  8. * Derived from ledtrig-timer.c which is:
  9. * Copyright 2005-2006 Openedhand Ltd.
  10. * Author: Richard Purdie <rpurdie@openedhand.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/list.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/device.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/timer.h>
  27. #include <linux/ctype.h>
  28. #include <linux/leds.h>
  29. #include "leds.h"
  30. /*
  31. * Configurable sysfs attributes:
  32. *
  33. * device_name - network device name to monitor
  34. *
  35. * interval - duration of LED blink, in milliseconds
  36. *
  37. * mode - either "none" (LED is off) or a space separated list of one or more of:
  38. * link: LED's normal state reflects whether the link is up (has carrier) or not
  39. * tx: LED blinks on transmitted data
  40. * rx: LED blinks on receive data
  41. *
  42. * Some suggestions:
  43. *
  44. * Simple link status LED:
  45. * $ echo netdev >someled/trigger
  46. * $ echo eth0 >someled/device_name
  47. * $ echo link >someled/mode
  48. *
  49. * Ethernet-style link/activity LED:
  50. * $ echo netdev >someled/trigger
  51. * $ echo eth0 >someled/device_name
  52. * $ echo "link tx rx" >someled/mode
  53. *
  54. * Modem-style tx/rx LEDs:
  55. * $ echo netdev >led1/trigger
  56. * $ echo ppp0 >led1/device_name
  57. * $ echo tx >led1/mode
  58. * $ echo netdev >led2/trigger
  59. * $ echo ppp0 >led2/device_name
  60. * $ echo rx >led2/mode
  61. *
  62. */
  63. #define MODE_LINK 1
  64. #define MODE_TX 2
  65. #define MODE_RX 4
  66. struct led_netdev_data {
  67. rwlock_t lock;
  68. struct timer_list timer;
  69. struct notifier_block notifier;
  70. struct led_classdev *led_cdev;
  71. struct net_device *net_dev;
  72. char device_name[IFNAMSIZ];
  73. unsigned interval;
  74. unsigned mode;
  75. unsigned link_up;
  76. unsigned last_activity;
  77. };
  78. static void set_baseline_state(struct led_netdev_data *trigger_data)
  79. {
  80. if ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up)
  81. led_set_brightness(trigger_data->led_cdev, LED_FULL);
  82. else
  83. led_set_brightness(trigger_data->led_cdev, LED_OFF);
  84. if ((trigger_data->mode & (MODE_TX | MODE_RX)) != 0 && trigger_data->link_up)
  85. mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
  86. else
  87. del_timer(&trigger_data->timer);
  88. }
  89. static ssize_t led_device_name_show(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  93. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  94. read_lock(&trigger_data->lock);
  95. sprintf(buf, "%s\n", trigger_data->device_name);
  96. read_unlock(&trigger_data->lock);
  97. return strlen(buf) + 1;
  98. }
  99. static ssize_t led_device_name_store(struct device *dev,
  100. struct device_attribute *attr, const char *buf, size_t size)
  101. {
  102. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  103. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  104. if (size < 0 || size >= IFNAMSIZ)
  105. return -EINVAL;
  106. write_lock(&trigger_data->lock);
  107. strcpy(trigger_data->device_name, buf);
  108. if (size > 0 && trigger_data->device_name[size-1] == '\n')
  109. trigger_data->device_name[size-1] = 0;
  110. if (trigger_data->device_name[0] != 0) {
  111. /* check for existing device to update from */
  112. trigger_data->net_dev = dev_get_by_name(&init_net, trigger_data->device_name);
  113. if (trigger_data->net_dev != NULL)
  114. trigger_data->link_up = (dev_get_flags(trigger_data->net_dev) & IFF_LOWER_UP) != 0;
  115. set_baseline_state(trigger_data); /* updates LEDs, may start timers */
  116. }
  117. write_unlock(&trigger_data->lock);
  118. return size;
  119. }
  120. static DEVICE_ATTR(device_name, 0644, led_device_name_show, led_device_name_store);
  121. static ssize_t led_mode_show(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  125. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  126. read_lock(&trigger_data->lock);
  127. if (trigger_data->mode == 0) {
  128. strcpy(buf, "none\n");
  129. } else {
  130. if (trigger_data->mode & MODE_LINK)
  131. strcat(buf, "link ");
  132. if (trigger_data->mode & MODE_TX)
  133. strcat(buf, "tx ");
  134. if (trigger_data->mode & MODE_RX)
  135. strcat(buf, "rx ");
  136. strcat(buf, "\n");
  137. }
  138. read_unlock(&trigger_data->lock);
  139. return strlen(buf)+1;
  140. }
  141. static ssize_t led_mode_store(struct device *dev,
  142. struct device_attribute *attr, const char *buf, size_t size)
  143. {
  144. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  145. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  146. char copybuf[128];
  147. int new_mode = -1;
  148. char *p, *token;
  149. /* take a copy since we don't want to trash the inbound buffer when using strsep */
  150. strncpy(copybuf, buf, sizeof(copybuf));
  151. copybuf[sizeof(copybuf) - 1] = 0;
  152. p = copybuf;
  153. while ((token = strsep(&p, " \t\n")) != NULL) {
  154. if (!*token)
  155. continue;
  156. if (new_mode == -1)
  157. new_mode = 0;
  158. if (!strcmp(token, "none"))
  159. new_mode = 0;
  160. else if (!strcmp(token, "tx"))
  161. new_mode |= MODE_TX;
  162. else if (!strcmp(token, "rx"))
  163. new_mode |= MODE_RX;
  164. else if (!strcmp(token, "link"))
  165. new_mode |= MODE_LINK;
  166. else
  167. return -EINVAL;
  168. }
  169. if (new_mode == -1)
  170. return -EINVAL;
  171. write_lock(&trigger_data->lock);
  172. trigger_data->mode = new_mode;
  173. set_baseline_state(trigger_data);
  174. write_unlock(&trigger_data->lock);
  175. return size;
  176. }
  177. static DEVICE_ATTR(mode, 0644, led_mode_show, led_mode_store);
  178. static ssize_t led_interval_show(struct device *dev,
  179. struct device_attribute *attr, char *buf)
  180. {
  181. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  182. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  183. read_lock(&trigger_data->lock);
  184. sprintf(buf, "%u\n", jiffies_to_msecs(trigger_data->interval));
  185. read_unlock(&trigger_data->lock);
  186. return strlen(buf) + 1;
  187. }
  188. static ssize_t led_interval_store(struct device *dev,
  189. struct device_attribute *attr, const char *buf, size_t size)
  190. {
  191. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  192. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  193. int ret = -EINVAL;
  194. char *after;
  195. unsigned long value = simple_strtoul(buf, &after, 10);
  196. size_t count = after - buf;
  197. if (isspace(*after))
  198. count++;
  199. /* impose some basic bounds on the timer interval */
  200. if (count == size && value >= 5 && value <= 10000) {
  201. write_lock(&trigger_data->lock);
  202. trigger_data->interval = msecs_to_jiffies(value);
  203. set_baseline_state(trigger_data); /* resets timer */
  204. write_unlock(&trigger_data->lock);
  205. ret = count;
  206. }
  207. return ret;
  208. }
  209. static DEVICE_ATTR(interval, 0644, led_interval_show, led_interval_store);
  210. static int netdev_trig_notify(struct notifier_block *nb,
  211. unsigned long evt,
  212. void *dv)
  213. {
  214. struct net_device *dev = dv;
  215. struct led_netdev_data *trigger_data = container_of(nb, struct led_netdev_data, notifier);
  216. if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER)
  217. return NOTIFY_DONE;
  218. write_lock(&trigger_data->lock);
  219. if (strcmp(dev->name, trigger_data->device_name))
  220. goto done;
  221. if (evt == NETDEV_REGISTER) {
  222. if (trigger_data->net_dev != NULL)
  223. dev_put(trigger_data->net_dev);
  224. dev_hold(dev);
  225. trigger_data->net_dev = dev;
  226. trigger_data->link_up = 0;
  227. goto done;
  228. }
  229. if (evt == NETDEV_UNREGISTER && trigger_data->net_dev != NULL) {
  230. dev_put(trigger_data->net_dev);
  231. trigger_data->net_dev = NULL;
  232. goto done;
  233. }
  234. /* UP / DOWN / CHANGE */
  235. trigger_data->link_up = (evt != NETDEV_DOWN && netif_carrier_ok(dev));
  236. set_baseline_state(trigger_data);
  237. done:
  238. write_unlock(&trigger_data->lock);
  239. return NOTIFY_DONE;
  240. }
  241. /* here's the real work! */
  242. static void netdev_trig_timer(unsigned long arg)
  243. {
  244. struct led_netdev_data *trigger_data = (struct led_netdev_data *)arg;
  245. const struct net_device_stats *dev_stats;
  246. unsigned new_activity;
  247. write_lock(&trigger_data->lock);
  248. if (!trigger_data->link_up || !trigger_data->net_dev || (trigger_data->mode & (MODE_TX | MODE_RX)) == 0) {
  249. /* we don't need to do timer work, just reflect link state. */
  250. led_set_brightness(trigger_data->led_cdev, ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up) ? LED_FULL : LED_OFF);
  251. goto no_restart;
  252. }
  253. dev_stats = dev_get_stats(trigger_data->net_dev);
  254. new_activity =
  255. ((trigger_data->mode & MODE_TX) ? dev_stats->tx_packets : 0) +
  256. ((trigger_data->mode & MODE_RX) ? dev_stats->rx_packets : 0);
  257. if (trigger_data->mode & MODE_LINK) {
  258. /* base state is ON (link present) */
  259. /* if there's no link, we don't get this far and the LED is off */
  260. /* OFF -> ON always */
  261. /* ON -> OFF on activity */
  262. if (trigger_data->led_cdev->brightness == LED_OFF) {
  263. led_set_brightness(trigger_data->led_cdev, LED_FULL);
  264. } else if (trigger_data->last_activity != new_activity) {
  265. led_set_brightness(trigger_data->led_cdev, LED_OFF);
  266. }
  267. } else {
  268. /* base state is OFF */
  269. /* ON -> OFF always */
  270. /* OFF -> ON on activity */
  271. if (trigger_data->led_cdev->brightness == LED_FULL) {
  272. led_set_brightness(trigger_data->led_cdev, LED_OFF);
  273. } else if (trigger_data->last_activity != new_activity) {
  274. led_set_brightness(trigger_data->led_cdev, LED_FULL);
  275. }
  276. }
  277. trigger_data->last_activity = new_activity;
  278. mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
  279. no_restart:
  280. write_unlock(&trigger_data->lock);
  281. }
  282. static void netdev_trig_activate(struct led_classdev *led_cdev)
  283. {
  284. struct led_netdev_data *trigger_data;
  285. int rc;
  286. trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
  287. if (!trigger_data)
  288. return;
  289. rwlock_init(&trigger_data->lock);
  290. trigger_data->notifier.notifier_call = netdev_trig_notify;
  291. trigger_data->notifier.priority = 10;
  292. setup_timer(&trigger_data->timer, netdev_trig_timer, (unsigned long) trigger_data);
  293. trigger_data->led_cdev = led_cdev;
  294. trigger_data->net_dev = NULL;
  295. trigger_data->device_name[0] = 0;
  296. trigger_data->mode = 0;
  297. trigger_data->interval = msecs_to_jiffies(50);
  298. trigger_data->link_up = 0;
  299. trigger_data->last_activity = 0;
  300. led_cdev->trigger_data = trigger_data;
  301. rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
  302. if (rc)
  303. goto err_out;
  304. rc = device_create_file(led_cdev->dev, &dev_attr_mode);
  305. if (rc)
  306. goto err_out_device_name;
  307. rc = device_create_file(led_cdev->dev, &dev_attr_interval);
  308. if (rc)
  309. goto err_out_mode;
  310. register_netdevice_notifier(&trigger_data->notifier);
  311. return;
  312. err_out_mode:
  313. device_remove_file(led_cdev->dev, &dev_attr_mode);
  314. err_out_device_name:
  315. device_remove_file(led_cdev->dev, &dev_attr_device_name);
  316. err_out:
  317. led_cdev->trigger_data = NULL;
  318. kfree(trigger_data);
  319. }
  320. static void netdev_trig_deactivate(struct led_classdev *led_cdev)
  321. {
  322. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  323. if (trigger_data) {
  324. unregister_netdevice_notifier(&trigger_data->notifier);
  325. device_remove_file(led_cdev->dev, &dev_attr_device_name);
  326. device_remove_file(led_cdev->dev, &dev_attr_mode);
  327. device_remove_file(led_cdev->dev, &dev_attr_interval);
  328. write_lock(&trigger_data->lock);
  329. if (trigger_data->net_dev) {
  330. dev_put(trigger_data->net_dev);
  331. trigger_data->net_dev = NULL;
  332. }
  333. write_unlock(&trigger_data->lock);
  334. del_timer_sync(&trigger_data->timer);
  335. kfree(trigger_data);
  336. }
  337. }
  338. static struct led_trigger netdev_led_trigger = {
  339. .name = "netdev",
  340. .activate = netdev_trig_activate,
  341. .deactivate = netdev_trig_deactivate,
  342. };
  343. static int __init netdev_trig_init(void)
  344. {
  345. return led_trigger_register(&netdev_led_trigger);
  346. }
  347. static void __exit netdev_trig_exit(void)
  348. {
  349. led_trigger_unregister(&netdev_led_trigger);
  350. }
  351. module_init(netdev_trig_init);
  352. module_exit(netdev_trig_exit);
  353. MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
  354. MODULE_DESCRIPTION("Netdev LED trigger");
  355. MODULE_LICENSE("GPL");