swconfig_leds.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * swconfig_led.c: LED trigger support for the switch configuration API
  3. *
  4. * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. */
  12. #ifdef CONFIG_SWCONFIG_LEDS
  13. #include <linux/leds.h>
  14. #include <linux/ctype.h>
  15. #include <linux/device.h>
  16. #include <linux/workqueue.h>
  17. #define SWCONFIG_LED_TIMER_INTERVAL (HZ / 10)
  18. #define SWCONFIG_LED_NUM_PORTS 32
  19. #define SWCONFIG_LED_PORT_SPEED_NA 0x01 /* unknown speed */
  20. #define SWCONFIG_LED_PORT_SPEED_10 0x02 /* 10 Mbps */
  21. #define SWCONFIG_LED_PORT_SPEED_100 0x04 /* 100 Mbps */
  22. #define SWCONFIG_LED_PORT_SPEED_1000 0x08 /* 1000 Mbps */
  23. #define SWCONFIG_LED_PORT_SPEED_ALL (SWCONFIG_LED_PORT_SPEED_NA | \
  24. SWCONFIG_LED_PORT_SPEED_10 | \
  25. SWCONFIG_LED_PORT_SPEED_100 | \
  26. SWCONFIG_LED_PORT_SPEED_1000)
  27. struct switch_led_trigger {
  28. struct led_trigger trig;
  29. struct switch_dev *swdev;
  30. struct delayed_work sw_led_work;
  31. u32 port_mask;
  32. u32 port_link;
  33. unsigned long port_traffic[SWCONFIG_LED_NUM_PORTS];
  34. u8 link_speed[SWCONFIG_LED_NUM_PORTS];
  35. };
  36. struct swconfig_trig_data {
  37. struct led_classdev *led_cdev;
  38. struct switch_dev *swdev;
  39. rwlock_t lock;
  40. u32 port_mask;
  41. bool prev_link;
  42. unsigned long prev_traffic;
  43. enum led_brightness prev_brightness;
  44. u8 speed_mask;
  45. };
  46. static void
  47. swconfig_trig_set_brightness(struct swconfig_trig_data *trig_data,
  48. enum led_brightness brightness)
  49. {
  50. led_set_brightness(trig_data->led_cdev, brightness);
  51. trig_data->prev_brightness = brightness;
  52. }
  53. static void
  54. swconfig_trig_update_port_mask(struct led_trigger *trigger)
  55. {
  56. struct list_head *entry;
  57. struct switch_led_trigger *sw_trig;
  58. u32 port_mask;
  59. if (!trigger)
  60. return;
  61. sw_trig = (void *) trigger;
  62. port_mask = 0;
  63. read_lock(&trigger->leddev_list_lock);
  64. list_for_each(entry, &trigger->led_cdevs) {
  65. struct led_classdev *led_cdev;
  66. struct swconfig_trig_data *trig_data;
  67. led_cdev = list_entry(entry, struct led_classdev, trig_list);
  68. trig_data = led_cdev->trigger_data;
  69. if (trig_data) {
  70. read_lock(&trig_data->lock);
  71. port_mask |= trig_data->port_mask;
  72. read_unlock(&trig_data->lock);
  73. }
  74. }
  75. read_unlock(&trigger->leddev_list_lock);
  76. sw_trig->port_mask = port_mask;
  77. if (port_mask)
  78. schedule_delayed_work(&sw_trig->sw_led_work,
  79. SWCONFIG_LED_TIMER_INTERVAL);
  80. else
  81. cancel_delayed_work_sync(&sw_trig->sw_led_work);
  82. }
  83. static ssize_t
  84. swconfig_trig_port_mask_store(struct device *dev, struct device_attribute *attr,
  85. const char *buf, size_t size)
  86. {
  87. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  88. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  89. unsigned long port_mask;
  90. int ret;
  91. bool changed;
  92. ret = kstrtoul(buf, 0, &port_mask);
  93. if (ret)
  94. return ret;
  95. write_lock(&trig_data->lock);
  96. changed = (trig_data->port_mask != port_mask);
  97. if (changed) {
  98. trig_data->port_mask = port_mask;
  99. if (port_mask == 0)
  100. swconfig_trig_set_brightness(trig_data, LED_OFF);
  101. }
  102. write_unlock(&trig_data->lock);
  103. if (changed)
  104. swconfig_trig_update_port_mask(led_cdev->trigger);
  105. return size;
  106. }
  107. static ssize_t
  108. swconfig_trig_port_mask_show(struct device *dev, struct device_attribute *attr,
  109. char *buf)
  110. {
  111. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  112. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  113. read_lock(&trig_data->lock);
  114. sprintf(buf, "%#x\n", trig_data->port_mask);
  115. read_unlock(&trig_data->lock);
  116. return strlen(buf) + 1;
  117. }
  118. static DEVICE_ATTR(port_mask, 0644, swconfig_trig_port_mask_show,
  119. swconfig_trig_port_mask_store);
  120. /* speed_mask file handler - display value */
  121. static ssize_t swconfig_trig_speed_mask_show(struct device *dev,
  122. struct device_attribute *attr,
  123. char *buf)
  124. {
  125. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  126. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  127. read_lock(&trig_data->lock);
  128. sprintf(buf, "%#x\n", trig_data->speed_mask);
  129. read_unlock(&trig_data->lock);
  130. return strlen(buf) + 1;
  131. }
  132. /* speed_mask file handler - store value */
  133. static ssize_t swconfig_trig_speed_mask_store(struct device *dev,
  134. struct device_attribute *attr,
  135. const char *buf, size_t size)
  136. {
  137. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  138. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  139. u8 speed_mask;
  140. int ret;
  141. ret = kstrtou8(buf, 0, &speed_mask);
  142. if (ret)
  143. return ret;
  144. write_lock(&trig_data->lock);
  145. trig_data->speed_mask = speed_mask & SWCONFIG_LED_PORT_SPEED_ALL;
  146. write_unlock(&trig_data->lock);
  147. return size;
  148. }
  149. /* speed_mask special file */
  150. static DEVICE_ATTR(speed_mask, 0644, swconfig_trig_speed_mask_show,
  151. swconfig_trig_speed_mask_store);
  152. static void
  153. swconfig_trig_activate(struct led_classdev *led_cdev)
  154. {
  155. struct switch_led_trigger *sw_trig;
  156. struct swconfig_trig_data *trig_data;
  157. int err;
  158. if (led_cdev->trigger->activate != swconfig_trig_activate)
  159. return;
  160. trig_data = kzalloc(sizeof(struct swconfig_trig_data), GFP_KERNEL);
  161. if (!trig_data)
  162. return;
  163. sw_trig = (void *) led_cdev->trigger;
  164. rwlock_init(&trig_data->lock);
  165. trig_data->led_cdev = led_cdev;
  166. trig_data->swdev = sw_trig->swdev;
  167. trig_data->speed_mask = SWCONFIG_LED_PORT_SPEED_ALL;
  168. led_cdev->trigger_data = trig_data;
  169. err = device_create_file(led_cdev->dev, &dev_attr_port_mask);
  170. if (err)
  171. goto err_free;
  172. err = device_create_file(led_cdev->dev, &dev_attr_speed_mask);
  173. if (err)
  174. goto err_dev_free;
  175. return;
  176. err_dev_free:
  177. device_remove_file(led_cdev->dev, &dev_attr_port_mask);
  178. err_free:
  179. led_cdev->trigger_data = NULL;
  180. kfree(trig_data);
  181. }
  182. static void
  183. swconfig_trig_deactivate(struct led_classdev *led_cdev)
  184. {
  185. struct swconfig_trig_data *trig_data;
  186. swconfig_trig_update_port_mask(led_cdev->trigger);
  187. trig_data = (void *) led_cdev->trigger_data;
  188. if (trig_data) {
  189. device_remove_file(led_cdev->dev, &dev_attr_port_mask);
  190. device_remove_file(led_cdev->dev, &dev_attr_speed_mask);
  191. kfree(trig_data);
  192. }
  193. }
  194. static void
  195. swconfig_trig_led_event(struct switch_led_trigger *sw_trig,
  196. struct led_classdev *led_cdev)
  197. {
  198. struct swconfig_trig_data *trig_data;
  199. u32 port_mask;
  200. bool link;
  201. u8 speed_mask;
  202. trig_data = led_cdev->trigger_data;
  203. if (!trig_data)
  204. return;
  205. read_lock(&trig_data->lock);
  206. port_mask = trig_data->port_mask;
  207. speed_mask = trig_data->speed_mask;
  208. read_unlock(&trig_data->lock);
  209. link = !!(sw_trig->port_link & port_mask);
  210. if (!link) {
  211. if (link != trig_data->prev_link)
  212. swconfig_trig_set_brightness(trig_data, LED_OFF);
  213. } else {
  214. unsigned long traffic;
  215. int speedok; /* link speed flag */
  216. int i;
  217. traffic = 0;
  218. speedok = 0;
  219. for (i = 0; i < SWCONFIG_LED_NUM_PORTS; i++) {
  220. if (port_mask & (1 << i))
  221. if (sw_trig->link_speed[i] & speed_mask) {
  222. traffic += sw_trig->port_traffic[i];
  223. speedok = 1;
  224. }
  225. }
  226. if (speedok) {
  227. /* At least one port speed matches speed_mask */
  228. if (trig_data->prev_brightness != LED_FULL)
  229. swconfig_trig_set_brightness(trig_data,
  230. LED_FULL);
  231. else if (traffic != trig_data->prev_traffic)
  232. swconfig_trig_set_brightness(trig_data,
  233. LED_OFF);
  234. } else if (trig_data->prev_brightness != LED_OFF)
  235. swconfig_trig_set_brightness(trig_data, LED_OFF);
  236. trig_data->prev_traffic = traffic;
  237. }
  238. trig_data->prev_link = link;
  239. }
  240. static void
  241. swconfig_trig_update_leds(struct switch_led_trigger *sw_trig)
  242. {
  243. struct list_head *entry;
  244. struct led_trigger *trigger;
  245. trigger = &sw_trig->trig;
  246. read_lock(&trigger->leddev_list_lock);
  247. list_for_each(entry, &trigger->led_cdevs) {
  248. struct led_classdev *led_cdev;
  249. led_cdev = list_entry(entry, struct led_classdev, trig_list);
  250. swconfig_trig_led_event(sw_trig, led_cdev);
  251. }
  252. read_unlock(&trigger->leddev_list_lock);
  253. }
  254. static void
  255. swconfig_led_work_func(struct work_struct *work)
  256. {
  257. struct switch_led_trigger *sw_trig;
  258. struct switch_dev *swdev;
  259. u32 port_mask;
  260. u32 link;
  261. int i;
  262. sw_trig = container_of(work, struct switch_led_trigger,
  263. sw_led_work.work);
  264. port_mask = sw_trig->port_mask;
  265. swdev = sw_trig->swdev;
  266. link = 0;
  267. for (i = 0; i < SWCONFIG_LED_NUM_PORTS; i++) {
  268. u32 port_bit;
  269. sw_trig->link_speed[i] = 0;
  270. port_bit = BIT(i);
  271. if ((port_mask & port_bit) == 0)
  272. continue;
  273. if (swdev->ops->get_port_link) {
  274. struct switch_port_link port_link;
  275. memset(&port_link, '\0', sizeof(port_link));
  276. swdev->ops->get_port_link(swdev, i, &port_link);
  277. if (port_link.link) {
  278. link |= port_bit;
  279. switch (port_link.speed) {
  280. case SWITCH_PORT_SPEED_UNKNOWN:
  281. sw_trig->link_speed[i] =
  282. SWCONFIG_LED_PORT_SPEED_NA;
  283. break;
  284. case SWITCH_PORT_SPEED_10:
  285. sw_trig->link_speed[i] =
  286. SWCONFIG_LED_PORT_SPEED_10;
  287. break;
  288. case SWITCH_PORT_SPEED_100:
  289. sw_trig->link_speed[i] =
  290. SWCONFIG_LED_PORT_SPEED_100;
  291. break;
  292. case SWITCH_PORT_SPEED_1000:
  293. sw_trig->link_speed[i] =
  294. SWCONFIG_LED_PORT_SPEED_1000;
  295. break;
  296. }
  297. }
  298. }
  299. if (swdev->ops->get_port_stats) {
  300. struct switch_port_stats port_stats;
  301. memset(&port_stats, '\0', sizeof(port_stats));
  302. swdev->ops->get_port_stats(swdev, i, &port_stats);
  303. sw_trig->port_traffic[i] = port_stats.tx_bytes +
  304. port_stats.rx_bytes;
  305. }
  306. }
  307. sw_trig->port_link = link;
  308. swconfig_trig_update_leds(sw_trig);
  309. schedule_delayed_work(&sw_trig->sw_led_work,
  310. SWCONFIG_LED_TIMER_INTERVAL);
  311. }
  312. static int
  313. swconfig_create_led_trigger(struct switch_dev *swdev)
  314. {
  315. struct switch_led_trigger *sw_trig;
  316. int err;
  317. if (!swdev->ops->get_port_link)
  318. return 0;
  319. sw_trig = kzalloc(sizeof(struct switch_led_trigger), GFP_KERNEL);
  320. if (!sw_trig)
  321. return -ENOMEM;
  322. sw_trig->swdev = swdev;
  323. sw_trig->trig.name = swdev->devname;
  324. sw_trig->trig.activate = swconfig_trig_activate;
  325. sw_trig->trig.deactivate = swconfig_trig_deactivate;
  326. INIT_DELAYED_WORK(&sw_trig->sw_led_work, swconfig_led_work_func);
  327. err = led_trigger_register(&sw_trig->trig);
  328. if (err)
  329. goto err_free;
  330. swdev->led_trigger = sw_trig;
  331. return 0;
  332. err_free:
  333. kfree(sw_trig);
  334. return err;
  335. }
  336. static void
  337. swconfig_destroy_led_trigger(struct switch_dev *swdev)
  338. {
  339. struct switch_led_trigger *sw_trig;
  340. sw_trig = swdev->led_trigger;
  341. if (sw_trig) {
  342. cancel_delayed_work_sync(&sw_trig->sw_led_work);
  343. led_trigger_unregister(&sw_trig->trig);
  344. kfree(sw_trig);
  345. }
  346. }
  347. #else /* SWCONFIG_LEDS */
  348. static inline int
  349. swconfig_create_led_trigger(struct switch_dev *swdev) { return 0; }
  350. static inline void
  351. swconfig_destroy_led_trigger(struct switch_dev *swdev) { }
  352. #endif /* CONFIG_SWCONFIG_LEDS */