swconfig_leds.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. #define SWCONFIG_LED_MODE_LINK 0x01
  28. #define SWCONFIG_LED_MODE_TX 0x02
  29. #define SWCONFIG_LED_MODE_RX 0x04
  30. #define SWCONFIG_LED_MODE_TXRX (SWCONFIG_LED_MODE_TX | \
  31. SWCONFIG_LED_MODE_RX)
  32. #define SWCONFIG_LED_MODE_ALL (SWCONFIG_LED_MODE_LINK | \
  33. SWCONFIG_LED_MODE_TX | \
  34. SWCONFIG_LED_MODE_RX)
  35. struct switch_led_trigger {
  36. struct led_trigger trig;
  37. struct switch_dev *swdev;
  38. struct delayed_work sw_led_work;
  39. u32 port_mask;
  40. u32 port_link;
  41. unsigned long long port_tx_traffic[SWCONFIG_LED_NUM_PORTS];
  42. unsigned long long port_rx_traffic[SWCONFIG_LED_NUM_PORTS];
  43. u8 link_speed[SWCONFIG_LED_NUM_PORTS];
  44. };
  45. struct swconfig_trig_data {
  46. struct led_classdev *led_cdev;
  47. struct switch_dev *swdev;
  48. rwlock_t lock;
  49. u32 port_mask;
  50. bool prev_link;
  51. unsigned long prev_traffic;
  52. enum led_brightness prev_brightness;
  53. u8 mode;
  54. u8 speed_mask;
  55. };
  56. static void
  57. swconfig_trig_set_brightness(struct swconfig_trig_data *trig_data,
  58. enum led_brightness brightness)
  59. {
  60. led_set_brightness(trig_data->led_cdev, brightness);
  61. trig_data->prev_brightness = brightness;
  62. }
  63. static void
  64. swconfig_trig_update_port_mask(struct led_trigger *trigger)
  65. {
  66. struct list_head *entry;
  67. struct switch_led_trigger *sw_trig;
  68. u32 port_mask;
  69. if (!trigger)
  70. return;
  71. sw_trig = (void *) trigger;
  72. port_mask = 0;
  73. read_lock(&trigger->leddev_list_lock);
  74. list_for_each(entry, &trigger->led_cdevs) {
  75. struct led_classdev *led_cdev;
  76. struct swconfig_trig_data *trig_data;
  77. led_cdev = list_entry(entry, struct led_classdev, trig_list);
  78. trig_data = led_cdev->trigger_data;
  79. if (trig_data) {
  80. read_lock(&trig_data->lock);
  81. port_mask |= trig_data->port_mask;
  82. read_unlock(&trig_data->lock);
  83. }
  84. }
  85. read_unlock(&trigger->leddev_list_lock);
  86. sw_trig->port_mask = port_mask;
  87. if (port_mask)
  88. schedule_delayed_work(&sw_trig->sw_led_work,
  89. SWCONFIG_LED_TIMER_INTERVAL);
  90. else
  91. cancel_delayed_work_sync(&sw_trig->sw_led_work);
  92. }
  93. static ssize_t
  94. swconfig_trig_port_mask_store(struct device *dev, struct device_attribute *attr,
  95. const char *buf, size_t size)
  96. {
  97. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  98. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  99. unsigned long port_mask;
  100. int ret;
  101. bool changed;
  102. ret = kstrtoul(buf, 0, &port_mask);
  103. if (ret)
  104. return ret;
  105. write_lock(&trig_data->lock);
  106. changed = (trig_data->port_mask != port_mask);
  107. trig_data->port_mask = port_mask;
  108. write_unlock(&trig_data->lock);
  109. if (changed) {
  110. if (port_mask == 0)
  111. swconfig_trig_set_brightness(trig_data, LED_OFF);
  112. swconfig_trig_update_port_mask(led_cdev->trigger);
  113. }
  114. return size;
  115. }
  116. static ssize_t
  117. swconfig_trig_port_mask_show(struct device *dev, struct device_attribute *attr,
  118. char *buf)
  119. {
  120. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  121. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  122. u32 port_mask;
  123. read_lock(&trig_data->lock);
  124. port_mask = trig_data->port_mask;
  125. read_unlock(&trig_data->lock);
  126. sprintf(buf, "%#x\n", port_mask);
  127. return strlen(buf) + 1;
  128. }
  129. static DEVICE_ATTR(port_mask, 0644, swconfig_trig_port_mask_show,
  130. swconfig_trig_port_mask_store);
  131. /* speed_mask file handler - display value */
  132. static ssize_t swconfig_trig_speed_mask_show(struct device *dev,
  133. struct device_attribute *attr,
  134. char *buf)
  135. {
  136. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  137. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  138. u8 speed_mask;
  139. read_lock(&trig_data->lock);
  140. speed_mask = trig_data->speed_mask;
  141. read_unlock(&trig_data->lock);
  142. sprintf(buf, "%#x\n", speed_mask);
  143. return strlen(buf) + 1;
  144. }
  145. /* speed_mask file handler - store value */
  146. static ssize_t swconfig_trig_speed_mask_store(struct device *dev,
  147. struct device_attribute *attr,
  148. const char *buf, size_t size)
  149. {
  150. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  151. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  152. u8 speed_mask;
  153. int ret;
  154. ret = kstrtou8(buf, 0, &speed_mask);
  155. if (ret)
  156. return ret;
  157. write_lock(&trig_data->lock);
  158. trig_data->speed_mask = speed_mask & SWCONFIG_LED_PORT_SPEED_ALL;
  159. write_unlock(&trig_data->lock);
  160. return size;
  161. }
  162. /* speed_mask special file */
  163. static DEVICE_ATTR(speed_mask, 0644, swconfig_trig_speed_mask_show,
  164. swconfig_trig_speed_mask_store);
  165. static ssize_t swconfig_trig_mode_show(struct device *dev,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  169. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  170. u8 mode;
  171. read_lock(&trig_data->lock);
  172. mode = trig_data->mode;
  173. read_unlock(&trig_data->lock);
  174. if (mode == 0) {
  175. strcpy(buf, "none\n");
  176. } else {
  177. if (mode & SWCONFIG_LED_MODE_LINK)
  178. strcat(buf, "link ");
  179. if (mode & SWCONFIG_LED_MODE_TX)
  180. strcat(buf, "tx ");
  181. if (mode & SWCONFIG_LED_MODE_RX)
  182. strcat(buf, "rx ");
  183. strcat(buf, "\n");
  184. }
  185. return strlen(buf)+1;
  186. }
  187. static ssize_t swconfig_trig_mode_store(struct device *dev,
  188. struct device_attribute *attr, const char *buf, size_t size)
  189. {
  190. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  191. struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  192. char copybuf[128];
  193. int new_mode = -1;
  194. char *p, *token;
  195. /* take a copy since we don't want to trash the inbound buffer when using strsep */
  196. strncpy(copybuf, buf, sizeof(copybuf));
  197. copybuf[sizeof(copybuf) - 1] = 0;
  198. p = copybuf;
  199. while ((token = strsep(&p, " \t\n")) != NULL) {
  200. if (!*token)
  201. continue;
  202. if (new_mode < 0)
  203. new_mode = 0;
  204. if (!strcmp(token, "none"))
  205. new_mode = 0;
  206. else if (!strcmp(token, "tx"))
  207. new_mode |= SWCONFIG_LED_MODE_TX;
  208. else if (!strcmp(token, "rx"))
  209. new_mode |= SWCONFIG_LED_MODE_RX;
  210. else if (!strcmp(token, "link"))
  211. new_mode |= SWCONFIG_LED_MODE_LINK;
  212. else
  213. return -EINVAL;
  214. }
  215. if (new_mode < 0)
  216. return -EINVAL;
  217. write_lock(&trig_data->lock);
  218. trig_data->mode = (u8)new_mode;
  219. write_unlock(&trig_data->lock);
  220. return size;
  221. }
  222. /* mode special file */
  223. static DEVICE_ATTR(mode, 0644, swconfig_trig_mode_show,
  224. swconfig_trig_mode_store);
  225. static int
  226. swconfig_trig_activate(struct led_classdev *led_cdev)
  227. {
  228. struct switch_led_trigger *sw_trig;
  229. struct swconfig_trig_data *trig_data;
  230. int err;
  231. trig_data = kzalloc(sizeof(struct swconfig_trig_data), GFP_KERNEL);
  232. if (!trig_data)
  233. return -ENOMEM;
  234. sw_trig = (void *) led_cdev->trigger;
  235. rwlock_init(&trig_data->lock);
  236. trig_data->led_cdev = led_cdev;
  237. trig_data->swdev = sw_trig->swdev;
  238. trig_data->speed_mask = SWCONFIG_LED_PORT_SPEED_ALL;
  239. trig_data->mode = SWCONFIG_LED_MODE_ALL;
  240. led_cdev->trigger_data = trig_data;
  241. err = device_create_file(led_cdev->dev, &dev_attr_port_mask);
  242. if (err)
  243. goto err_free;
  244. err = device_create_file(led_cdev->dev, &dev_attr_speed_mask);
  245. if (err)
  246. goto err_dev_free;
  247. err = device_create_file(led_cdev->dev, &dev_attr_mode);
  248. if (err)
  249. goto err_mode_free;
  250. return 0;
  251. err_mode_free:
  252. device_remove_file(led_cdev->dev, &dev_attr_speed_mask);
  253. err_dev_free:
  254. device_remove_file(led_cdev->dev, &dev_attr_port_mask);
  255. err_free:
  256. led_cdev->trigger_data = NULL;
  257. kfree(trig_data);
  258. return err;
  259. }
  260. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,19,0)
  261. static void
  262. swconfig_trig_activate_void(struct led_classdev *led_cdev)
  263. {
  264. swconfig_trig_activate(led_cdev);
  265. }
  266. #endif
  267. static void
  268. swconfig_trig_deactivate(struct led_classdev *led_cdev)
  269. {
  270. struct swconfig_trig_data *trig_data;
  271. swconfig_trig_update_port_mask(led_cdev->trigger);
  272. trig_data = (void *) led_cdev->trigger_data;
  273. if (trig_data) {
  274. device_remove_file(led_cdev->dev, &dev_attr_port_mask);
  275. device_remove_file(led_cdev->dev, &dev_attr_speed_mask);
  276. device_remove_file(led_cdev->dev, &dev_attr_mode);
  277. kfree(trig_data);
  278. }
  279. }
  280. /*
  281. * link off -> led off (can't be any other reason to turn it on)
  282. * link on:
  283. * mode link: led on by default only if speed matches, else off
  284. * mode txrx: blink only if speed matches, else off
  285. */
  286. static void
  287. swconfig_trig_led_event(struct switch_led_trigger *sw_trig,
  288. struct led_classdev *led_cdev)
  289. {
  290. struct swconfig_trig_data *trig_data;
  291. u32 port_mask;
  292. bool link;
  293. u8 speed_mask, mode;
  294. enum led_brightness led_base, led_blink;
  295. trig_data = led_cdev->trigger_data;
  296. if (!trig_data)
  297. return;
  298. read_lock(&trig_data->lock);
  299. port_mask = trig_data->port_mask;
  300. speed_mask = trig_data->speed_mask;
  301. mode = trig_data->mode;
  302. read_unlock(&trig_data->lock);
  303. link = !!(sw_trig->port_link & port_mask);
  304. if (!link) {
  305. if (trig_data->prev_brightness != LED_OFF)
  306. swconfig_trig_set_brightness(trig_data, LED_OFF); /* and stop */
  307. }
  308. else {
  309. unsigned long traffic;
  310. int speedok; /* link speed flag */
  311. int i;
  312. led_base = LED_FULL;
  313. led_blink = LED_OFF;
  314. traffic = 0;
  315. speedok = 0;
  316. for (i = 0; i < SWCONFIG_LED_NUM_PORTS; i++) {
  317. if (port_mask & (1 << i)) {
  318. if (sw_trig->link_speed[i] & speed_mask) {
  319. traffic += ((mode & SWCONFIG_LED_MODE_TX) ?
  320. sw_trig->port_tx_traffic[i] : 0) +
  321. ((mode & SWCONFIG_LED_MODE_RX) ?
  322. sw_trig->port_rx_traffic[i] : 0);
  323. speedok = 1;
  324. }
  325. }
  326. }
  327. if (speedok) {
  328. /* At least one port speed matches speed_mask */
  329. if (!(mode & SWCONFIG_LED_MODE_LINK)) {
  330. led_base = LED_OFF;
  331. led_blink = LED_FULL;
  332. }
  333. if (trig_data->prev_brightness != led_base)
  334. swconfig_trig_set_brightness(trig_data,
  335. led_base);
  336. else if (traffic != trig_data->prev_traffic)
  337. swconfig_trig_set_brightness(trig_data,
  338. led_blink);
  339. } else if (trig_data->prev_brightness != LED_OFF)
  340. swconfig_trig_set_brightness(trig_data, LED_OFF);
  341. trig_data->prev_traffic = traffic;
  342. }
  343. trig_data->prev_link = link;
  344. }
  345. static void
  346. swconfig_trig_update_leds(struct switch_led_trigger *sw_trig)
  347. {
  348. struct list_head *entry;
  349. struct led_trigger *trigger;
  350. trigger = &sw_trig->trig;
  351. read_lock(&trigger->leddev_list_lock);
  352. list_for_each(entry, &trigger->led_cdevs) {
  353. struct led_classdev *led_cdev;
  354. led_cdev = list_entry(entry, struct led_classdev, trig_list);
  355. swconfig_trig_led_event(sw_trig, led_cdev);
  356. }
  357. read_unlock(&trigger->leddev_list_lock);
  358. }
  359. static void
  360. swconfig_led_work_func(struct work_struct *work)
  361. {
  362. struct switch_led_trigger *sw_trig;
  363. struct switch_dev *swdev;
  364. u32 port_mask;
  365. u32 link;
  366. int i;
  367. sw_trig = container_of(work, struct switch_led_trigger,
  368. sw_led_work.work);
  369. port_mask = sw_trig->port_mask;
  370. swdev = sw_trig->swdev;
  371. link = 0;
  372. for (i = 0; i < SWCONFIG_LED_NUM_PORTS; i++) {
  373. u32 port_bit;
  374. sw_trig->link_speed[i] = 0;
  375. port_bit = BIT(i);
  376. if ((port_mask & port_bit) == 0)
  377. continue;
  378. if (swdev->ops->get_port_link) {
  379. struct switch_port_link port_link;
  380. memset(&port_link, '\0', sizeof(port_link));
  381. swdev->ops->get_port_link(swdev, i, &port_link);
  382. if (port_link.link) {
  383. link |= port_bit;
  384. switch (port_link.speed) {
  385. case SWITCH_PORT_SPEED_UNKNOWN:
  386. sw_trig->link_speed[i] =
  387. SWCONFIG_LED_PORT_SPEED_NA;
  388. break;
  389. case SWITCH_PORT_SPEED_10:
  390. sw_trig->link_speed[i] =
  391. SWCONFIG_LED_PORT_SPEED_10;
  392. break;
  393. case SWITCH_PORT_SPEED_100:
  394. sw_trig->link_speed[i] =
  395. SWCONFIG_LED_PORT_SPEED_100;
  396. break;
  397. case SWITCH_PORT_SPEED_1000:
  398. sw_trig->link_speed[i] =
  399. SWCONFIG_LED_PORT_SPEED_1000;
  400. break;
  401. }
  402. }
  403. }
  404. if (swdev->ops->get_port_stats) {
  405. struct switch_port_stats port_stats;
  406. memset(&port_stats, '\0', sizeof(port_stats));
  407. swdev->ops->get_port_stats(swdev, i, &port_stats);
  408. sw_trig->port_tx_traffic[i] = port_stats.tx_bytes;
  409. sw_trig->port_rx_traffic[i] = port_stats.rx_bytes;
  410. }
  411. }
  412. sw_trig->port_link = link;
  413. swconfig_trig_update_leds(sw_trig);
  414. schedule_delayed_work(&sw_trig->sw_led_work,
  415. SWCONFIG_LED_TIMER_INTERVAL);
  416. }
  417. static int
  418. swconfig_create_led_trigger(struct switch_dev *swdev)
  419. {
  420. struct switch_led_trigger *sw_trig;
  421. int err;
  422. if (!swdev->ops->get_port_link)
  423. return 0;
  424. sw_trig = kzalloc(sizeof(struct switch_led_trigger), GFP_KERNEL);
  425. if (!sw_trig)
  426. return -ENOMEM;
  427. sw_trig->swdev = swdev;
  428. sw_trig->trig.name = swdev->devname;
  429. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,19,0)
  430. sw_trig->trig.activate = swconfig_trig_activate_void;
  431. #else
  432. sw_trig->trig.activate = swconfig_trig_activate;
  433. #endif
  434. sw_trig->trig.deactivate = swconfig_trig_deactivate;
  435. INIT_DELAYED_WORK(&sw_trig->sw_led_work, swconfig_led_work_func);
  436. err = led_trigger_register(&sw_trig->trig);
  437. if (err)
  438. goto err_free;
  439. swdev->led_trigger = sw_trig;
  440. return 0;
  441. err_free:
  442. kfree(sw_trig);
  443. return err;
  444. }
  445. static void
  446. swconfig_destroy_led_trigger(struct switch_dev *swdev)
  447. {
  448. struct switch_led_trigger *sw_trig;
  449. sw_trig = swdev->led_trigger;
  450. if (sw_trig) {
  451. cancel_delayed_work_sync(&sw_trig->sw_led_work);
  452. led_trigger_unregister(&sw_trig->trig);
  453. kfree(sw_trig);
  454. }
  455. }
  456. #else /* SWCONFIG_LEDS */
  457. static inline int
  458. swconfig_create_led_trigger(struct switch_dev *swdev) { return 0; }
  459. static inline void
  460. swconfig_destroy_led_trigger(struct switch_dev *swdev) { }
  461. #endif /* CONFIG_SWCONFIG_LEDS */