ledtrig-netdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/netdevice.h>
  25. #include <linux/timer.h>
  26. #include <linux/ctype.h>
  27. #include <linux/leds.h>
  28. #include "leds.h"
  29. /*
  30. * Configurable sysfs attributes:
  31. *
  32. * device_name - network device name to monitor
  33. *
  34. * interval - duration of LED blink, in milliseconds
  35. *
  36. * mode - either "none" (LED is off) or a space separated list of one or more of:
  37. * link: LED's normal state reflects whether the link is up (has carrier) or not
  38. * tx: LED blinks on transmitted data
  39. * rx: LED blinks on receive data
  40. *
  41. * Some suggestions:
  42. *
  43. * Simple link status LED:
  44. * $ echo netdev >someled/trigger
  45. * $ echo eth0 >someled/device_name
  46. * $ echo link >someled/mode
  47. *
  48. * Ethernet-style link/activity LED:
  49. * $ echo netdev >someled/trigger
  50. * $ echo eth0 >someled/device_name
  51. * $ echo "link tx rx" >someled/mode
  52. *
  53. * Modem-style tx/rx LEDs:
  54. * $ echo netdev >led1/trigger
  55. * $ echo ppp0 >led1/device_name
  56. * $ echo tx >led1/mode
  57. * $ echo netdev >led2/trigger
  58. * $ echo ppp0 >led2/device_name
  59. * $ echo rx >led2/mode
  60. *
  61. */
  62. #define MODE_LINK 1
  63. #define MODE_TX 2
  64. #define MODE_RX 4
  65. struct led_netdev_data {
  66. spinlock_t lock;
  67. struct delayed_work work;
  68. struct notifier_block notifier;
  69. struct led_classdev *led_cdev;
  70. struct net_device *net_dev;
  71. char device_name[IFNAMSIZ];
  72. unsigned interval;
  73. unsigned mode;
  74. unsigned link_up;
  75. unsigned last_activity;
  76. };
  77. static void set_baseline_state(struct led_netdev_data *trigger_data)
  78. {
  79. if ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up)
  80. led_set_brightness(trigger_data->led_cdev, LED_FULL);
  81. else
  82. led_set_brightness(trigger_data->led_cdev, LED_OFF);
  83. if ((trigger_data->mode & (MODE_TX | MODE_RX)) != 0 && trigger_data->link_up)
  84. schedule_delayed_work(&trigger_data->work, trigger_data->interval);
  85. }
  86. static ssize_t led_device_name_show(struct device *dev,
  87. struct device_attribute *attr, char *buf)
  88. {
  89. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  90. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  91. spin_lock_bh(&trigger_data->lock);
  92. sprintf(buf, "%s\n", trigger_data->device_name);
  93. spin_unlock_bh(&trigger_data->lock);
  94. return strlen(buf) + 1;
  95. }
  96. static ssize_t led_device_name_store(struct device *dev,
  97. struct device_attribute *attr, const char *buf, size_t size)
  98. {
  99. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  100. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  101. if (size < 0 || size >= IFNAMSIZ)
  102. return -EINVAL;
  103. cancel_delayed_work_sync(&trigger_data->work);
  104. spin_lock_bh(&trigger_data->lock);
  105. strcpy(trigger_data->device_name, buf);
  106. if (size > 0 && trigger_data->device_name[size-1] == '\n')
  107. trigger_data->device_name[size-1] = 0;
  108. trigger_data->link_up = 0;
  109. trigger_data->last_activity = 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. }
  116. set_baseline_state(trigger_data);
  117. spin_unlock_bh(&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. spin_lock_bh(&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. spin_unlock_bh(&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. cancel_delayed_work_sync(&trigger_data->work);
  172. spin_lock_bh(&trigger_data->lock);
  173. trigger_data->mode = new_mode;
  174. set_baseline_state(trigger_data);
  175. spin_unlock_bh(&trigger_data->lock);
  176. return size;
  177. }
  178. static DEVICE_ATTR(mode, 0644, led_mode_show, led_mode_store);
  179. static ssize_t led_interval_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  183. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  184. spin_lock_bh(&trigger_data->lock);
  185. sprintf(buf, "%u\n", jiffies_to_msecs(trigger_data->interval));
  186. spin_unlock_bh(&trigger_data->lock);
  187. return strlen(buf) + 1;
  188. }
  189. static ssize_t led_interval_store(struct device *dev,
  190. struct device_attribute *attr, const char *buf, size_t size)
  191. {
  192. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  193. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  194. int ret = -EINVAL;
  195. char *after;
  196. unsigned long value = simple_strtoul(buf, &after, 10);
  197. size_t count = after - buf;
  198. if (isspace(*after))
  199. count++;
  200. /* impose some basic bounds on the timer interval */
  201. if (count == size && value >= 5 && value <= 10000) {
  202. cancel_delayed_work_sync(&trigger_data->work);
  203. spin_lock_bh(&trigger_data->lock);
  204. trigger_data->interval = msecs_to_jiffies(value);
  205. set_baseline_state(trigger_data); /* resets timer */
  206. spin_unlock_bh(&trigger_data->lock);
  207. ret = count;
  208. }
  209. return ret;
  210. }
  211. static DEVICE_ATTR(interval, 0644, led_interval_show, led_interval_store);
  212. static int netdev_trig_notify(struct notifier_block *nb,
  213. unsigned long evt,
  214. void *dv)
  215. {
  216. struct net_device *dev = netdev_notifier_info_to_dev((struct netdev_notifier_info *) dv);
  217. struct led_netdev_data *trigger_data = container_of(nb, struct led_netdev_data, notifier);
  218. if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER)
  219. return NOTIFY_DONE;
  220. if (!(dev == trigger_data->net_dev ||
  221. (evt == NETDEV_REGISTER && !strcmp(dev->name, trigger_data->device_name))))
  222. return NOTIFY_DONE;
  223. cancel_delayed_work_sync(&trigger_data->work);
  224. spin_lock_bh(&trigger_data->lock);
  225. switch (evt) {
  226. case NETDEV_REGISTER:
  227. dev_hold(dev);
  228. trigger_data->net_dev = dev;
  229. trigger_data->link_up = 0;
  230. break;
  231. case NETDEV_UNREGISTER:
  232. dev_put(trigger_data->net_dev);
  233. trigger_data->net_dev = NULL;
  234. break;
  235. default: /* UP / DOWN / CHANGE */
  236. trigger_data->link_up = (evt != NETDEV_DOWN && netif_carrier_ok(dev));
  237. set_baseline_state(trigger_data);
  238. break;
  239. }
  240. spin_unlock_bh(&trigger_data->lock);
  241. return NOTIFY_DONE;
  242. }
  243. /* here's the real work! */
  244. static void netdev_trig_work(struct work_struct *work)
  245. {
  246. struct led_netdev_data *trigger_data = container_of(work, struct led_netdev_data, work.work);
  247. struct rtnl_link_stats64 *dev_stats;
  248. unsigned new_activity;
  249. struct rtnl_link_stats64 temp;
  250. if (!trigger_data->link_up || !trigger_data->net_dev || (trigger_data->mode & (MODE_TX | MODE_RX)) == 0) {
  251. /* we don't need to do timer work, just reflect link state. */
  252. led_set_brightness(trigger_data->led_cdev, ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up) ? LED_FULL : LED_OFF);
  253. return;
  254. }
  255. dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
  256. new_activity =
  257. ((trigger_data->mode & MODE_TX) ? dev_stats->tx_packets : 0) +
  258. ((trigger_data->mode & MODE_RX) ? dev_stats->rx_packets : 0);
  259. if (trigger_data->mode & MODE_LINK) {
  260. /* base state is ON (link present) */
  261. /* if there's no link, we don't get this far and the LED is off */
  262. /* OFF -> ON always */
  263. /* ON -> OFF on activity */
  264. if (trigger_data->led_cdev->brightness == LED_OFF) {
  265. led_set_brightness(trigger_data->led_cdev, LED_FULL);
  266. } else if (trigger_data->last_activity != new_activity) {
  267. led_set_brightness(trigger_data->led_cdev, LED_OFF);
  268. }
  269. } else {
  270. /* base state is OFF */
  271. /* ON -> OFF always */
  272. /* OFF -> ON on activity */
  273. if (trigger_data->led_cdev->brightness == LED_FULL) {
  274. led_set_brightness(trigger_data->led_cdev, LED_OFF);
  275. } else if (trigger_data->last_activity != new_activity) {
  276. led_set_brightness(trigger_data->led_cdev, LED_FULL);
  277. }
  278. }
  279. trigger_data->last_activity = new_activity;
  280. schedule_delayed_work(&trigger_data->work, trigger_data->interval);
  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. spin_lock_init(&trigger_data->lock);
  290. trigger_data->notifier.notifier_call = netdev_trig_notify;
  291. trigger_data->notifier.priority = 10;
  292. INIT_DELAYED_WORK(&trigger_data->work, netdev_trig_work);
  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. cancel_delayed_work_sync(&trigger_data->work);
  329. spin_lock_bh(&trigger_data->lock);
  330. if (trigger_data->net_dev) {
  331. dev_put(trigger_data->net_dev);
  332. trigger_data->net_dev = NULL;
  333. }
  334. spin_unlock_bh(&trigger_data->lock);
  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");