ledtrig-netdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 timer_list timer;
  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. mod_timer(&trigger_data->timer, jiffies + 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. spin_lock_bh(&trigger_data->lock);
  104. del_timer_sync(&trigger_data->timer);
  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. spin_lock_bh(&trigger_data->lock);
  172. del_timer_sync(&trigger_data->timer);
  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. spin_lock_bh(&trigger_data->lock);
  203. del_timer_sync(&trigger_data->timer);
  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 && evt != NETDEV_CHANGENAME)
  219. return NOTIFY_DONE;
  220. spin_lock_bh(&trigger_data->lock);
  221. if (strcmp(dev->name, trigger_data->device_name))
  222. goto done;
  223. del_timer_sync(&trigger_data->timer);
  224. if (evt == NETDEV_REGISTER || evt == NETDEV_CHANGENAME) {
  225. if (trigger_data->net_dev != NULL)
  226. dev_put(trigger_data->net_dev);
  227. dev_hold(dev);
  228. trigger_data->net_dev = dev;
  229. trigger_data->link_up = 0;
  230. goto done;
  231. }
  232. if (evt == NETDEV_UNREGISTER && trigger_data->net_dev != NULL) {
  233. dev_put(trigger_data->net_dev);
  234. trigger_data->net_dev = NULL;
  235. goto done;
  236. }
  237. /* UP / DOWN / CHANGE */
  238. trigger_data->link_up = (evt != NETDEV_DOWN && netif_carrier_ok(dev));
  239. set_baseline_state(trigger_data);
  240. done:
  241. spin_unlock_bh(&trigger_data->lock);
  242. return NOTIFY_DONE;
  243. }
  244. /* here's the real work! */
  245. static void netdev_trig_timer(unsigned long arg)
  246. {
  247. struct led_netdev_data *trigger_data = (struct led_netdev_data *)arg;
  248. struct rtnl_link_stats64 *dev_stats;
  249. unsigned new_activity;
  250. struct rtnl_link_stats64 temp;
  251. if (!trigger_data->link_up || !trigger_data->net_dev || (trigger_data->mode & (MODE_TX | MODE_RX)) == 0) {
  252. /* we don't need to do timer work, just reflect link state. */
  253. led_set_brightness(trigger_data->led_cdev, ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up) ? LED_FULL : LED_OFF);
  254. return;
  255. }
  256. dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
  257. new_activity =
  258. ((trigger_data->mode & MODE_TX) ? dev_stats->tx_packets : 0) +
  259. ((trigger_data->mode & MODE_RX) ? dev_stats->rx_packets : 0);
  260. if (trigger_data->mode & MODE_LINK) {
  261. /* base state is ON (link present) */
  262. /* if there's no link, we don't get this far and the LED is off */
  263. /* OFF -> ON always */
  264. /* ON -> OFF on activity */
  265. if (trigger_data->led_cdev->brightness == LED_OFF) {
  266. led_set_brightness(trigger_data->led_cdev, LED_FULL);
  267. } else if (trigger_data->last_activity != new_activity) {
  268. led_set_brightness(trigger_data->led_cdev, LED_OFF);
  269. }
  270. } else {
  271. /* base state is OFF */
  272. /* ON -> OFF always */
  273. /* OFF -> ON on activity */
  274. if (trigger_data->led_cdev->brightness == LED_FULL) {
  275. led_set_brightness(trigger_data->led_cdev, LED_OFF);
  276. } else if (trigger_data->last_activity != new_activity) {
  277. led_set_brightness(trigger_data->led_cdev, LED_FULL);
  278. }
  279. }
  280. trigger_data->last_activity = new_activity;
  281. mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
  282. }
  283. static void netdev_trig_activate(struct led_classdev *led_cdev)
  284. {
  285. struct led_netdev_data *trigger_data;
  286. int rc;
  287. trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
  288. if (!trigger_data)
  289. return;
  290. spin_lock_init(&trigger_data->lock);
  291. trigger_data->notifier.notifier_call = netdev_trig_notify;
  292. trigger_data->notifier.priority = 10;
  293. setup_timer(&trigger_data->timer, netdev_trig_timer, (unsigned long) trigger_data);
  294. trigger_data->led_cdev = led_cdev;
  295. trigger_data->net_dev = NULL;
  296. trigger_data->device_name[0] = 0;
  297. trigger_data->mode = 0;
  298. trigger_data->interval = msecs_to_jiffies(50);
  299. trigger_data->link_up = 0;
  300. trigger_data->last_activity = 0;
  301. led_cdev->trigger_data = trigger_data;
  302. rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
  303. if (rc)
  304. goto err_out;
  305. rc = device_create_file(led_cdev->dev, &dev_attr_mode);
  306. if (rc)
  307. goto err_out_device_name;
  308. rc = device_create_file(led_cdev->dev, &dev_attr_interval);
  309. if (rc)
  310. goto err_out_mode;
  311. register_netdevice_notifier(&trigger_data->notifier);
  312. return;
  313. err_out_mode:
  314. device_remove_file(led_cdev->dev, &dev_attr_mode);
  315. err_out_device_name:
  316. device_remove_file(led_cdev->dev, &dev_attr_device_name);
  317. err_out:
  318. led_cdev->trigger_data = NULL;
  319. kfree(trigger_data);
  320. }
  321. static void netdev_trig_deactivate(struct led_classdev *led_cdev)
  322. {
  323. struct led_netdev_data *trigger_data = led_cdev->trigger_data;
  324. if (trigger_data) {
  325. unregister_netdevice_notifier(&trigger_data->notifier);
  326. device_remove_file(led_cdev->dev, &dev_attr_device_name);
  327. device_remove_file(led_cdev->dev, &dev_attr_mode);
  328. device_remove_file(led_cdev->dev, &dev_attr_interval);
  329. spin_lock_bh(&trigger_data->lock);
  330. del_timer_sync(&trigger_data->timer);
  331. if (trigger_data->net_dev) {
  332. dev_put(trigger_data->net_dev);
  333. trigger_data->net_dev = NULL;
  334. }
  335. spin_unlock_bh(&trigger_data->lock);
  336. kfree(trigger_data);
  337. }
  338. }
  339. static struct led_trigger netdev_led_trigger = {
  340. .name = "netdev",
  341. .activate = netdev_trig_activate,
  342. .deactivate = netdev_trig_deactivate,
  343. };
  344. static int __init netdev_trig_init(void)
  345. {
  346. return led_trigger_register(&netdev_led_trigger);
  347. }
  348. static void __exit netdev_trig_exit(void)
  349. {
  350. led_trigger_unregister(&netdev_led_trigger);
  351. }
  352. module_init(netdev_trig_init);
  353. module_exit(netdev_trig_exit);
  354. MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
  355. MODULE_DESCRIPTION("Netdev LED trigger");
  356. MODULE_LICENSE("GPL");