084-0001-usb-core-Introduce-a-USB-port-LED-trigger.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. From 0f247626cbbfa2010d2b86fdee652605e084e248 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
  3. Date: Fri, 16 Sep 2016 16:13:48 +0200
  4. Subject: [PATCH] usb: core: Introduce a USB port LED trigger
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. This commit adds a new trigger responsible for turning on LED when USB
  9. device gets connected to the selected USB port. This can can useful for
  10. various home routers that have USB port(s) and a proper LED telling user
  11. a device is connected.
  12. The trigger gets its documentation file but basically it just requires
  13. enabling it and selecting USB ports (e.g. echo 1 > ports/usb1-1).
  14. There was a long discussion on design of this driver. Its current state
  15. is a result of picking them most adjustable solution as others couldn't
  16. handle all cases.
  17. 1) It wasn't possible for the driver to register separated trigger for
  18. each USB port. Some physical USB ports are handled by more than one
  19. controller and so by more than one USB port. E.g. USB 2.0 physical
  20. port may be handled by OHCI's port and EHCI's port.
  21. It's also not possible to assign more than 1 trigger to a single LED
  22. and implementing such feature would be tricky due to syncing triggers
  23. and sysfs conflicts with old triggers.
  24. 2) Another idea was to register trigger per USB hub. This wouldn't allow
  25. handling devices with multiple USB LEDs and controllers (hubs)
  26. controlling more than 1 physical port. It's common for hubs to have
  27. few ports and each may have its own LED.
  28. This final trigger is highly flexible. It allows selecting any USB ports
  29. for any LED. It was also modified (comparing to the initial version) to
  30. allow choosing ports rather than having user /guess/ proper names. It
  31. was successfully tested on SmartRG SR400ac which has 3 USB LEDs,
  32. 2 physical ports and 3 controllers.
  33. It was noted USB subsystem already has usb-gadget and usb-host triggers
  34. but they are pretty trivial ones. They indicate activity only and can't
  35. have ports specified.
  36. In future it may be good idea to consider adding activity support to
  37. usbport as well. This should allow switching to this more generic driver
  38. and maybe marking old ones as obsolete.
  39. This can be implemented with another sysfs file for setting mode. The
  40. default mode wouldn't change so there won't be ABI breakage and so such
  41. feature can be safely implemented later.
  42. There was also an idea of supporting other devices (PCI, SDIO, etc.) but
  43. as this driver already contains some USB specific code (and will get
  44. more) these should be probably separated drivers (triggers).
  45. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
  46. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  47. ---
  48. .../ABI/testing/sysfs-class-led-trigger-usbport | 12 +
  49. Documentation/leds/ledtrig-usbport.txt | 41 +++
  50. drivers/usb/core/Kconfig | 8 +
  51. drivers/usb/core/Makefile | 2 +
  52. drivers/usb/core/ledtrig-usbport.c | 314 +++++++++++++++++++++
  53. 5 files changed, 377 insertions(+)
  54. create mode 100644 Documentation/ABI/testing/sysfs-class-led-trigger-usbport
  55. create mode 100644 Documentation/leds/ledtrig-usbport.txt
  56. create mode 100644 drivers/usb/core/ledtrig-usbport.c
  57. --- /dev/null
  58. +++ b/Documentation/ABI/testing/sysfs-class-led-trigger-usbport
  59. @@ -0,0 +1,12 @@
  60. +What: /sys/class/leds/<led>/ports/<port>
  61. +Date: September 2016
  62. +KernelVersion: 4.9
  63. +Contact: linux-leds@vger.kernel.org
  64. + linux-usb@vger.kernel.org
  65. +Description:
  66. + Every dir entry represents a single USB port that can be
  67. + selected for the USB port trigger. Selecting ports makes trigger
  68. + observing them for any connected devices and lighting on LED if
  69. + there are any.
  70. + Echoing "1" value selects USB port. Echoing "0" unselects it.
  71. + Current state can be also read.
  72. --- /dev/null
  73. +++ b/Documentation/leds/ledtrig-usbport.txt
  74. @@ -0,0 +1,41 @@
  75. +USB port LED trigger
  76. +====================
  77. +
  78. +This LED trigger can be used for signalling to the user a presence of USB device
  79. +in a given port. It simply turns on LED when device appears and turns it off
  80. +when it disappears.
  81. +
  82. +It requires selecting USB ports that should be observed. All available ones are
  83. +listed as separated entries in a "ports" subdirectory. Selecting is handled by
  84. +echoing "1" to a chosen port.
  85. +
  86. +Please note that this trigger allows selecting multiple USB ports for a single
  87. +LED. This can be useful in two cases:
  88. +
  89. +1) Device with single USB LED and few physical ports
  90. +
  91. +In such a case LED will be turned on as long as there is at least one connected
  92. +USB device.
  93. +
  94. +2) Device with a physical port handled by few controllers
  95. +
  96. +Some devices may have one controller per PHY standard. E.g. USB 3.0 physical
  97. +port may be handled by ohci-platform, ehci-platform and xhci-hcd. If there is
  98. +only one LED user will most likely want to assign ports from all 3 hubs.
  99. +
  100. +
  101. +This trigger can be activated from user space on led class devices as shown
  102. +below:
  103. +
  104. + echo usbport > trigger
  105. +
  106. +This adds sysfs attributes to the LED that are documented in:
  107. +Documentation/ABI/testing/sysfs-class-led-trigger-usbport
  108. +
  109. +Example use-case:
  110. +
  111. + echo usbport > trigger
  112. + echo 1 > ports/usb1-port1
  113. + echo 1 > ports/usb2-port1
  114. + cat ports/usb1-port1
  115. + echo 0 > ports/usb1-port1
  116. --- a/drivers/usb/core/Kconfig
  117. +++ b/drivers/usb/core/Kconfig
  118. @@ -103,3 +103,11 @@ config USB_ULPI_BUS
  119. To compile this driver as a module, choose M here: the module will
  120. be called ulpi.
  121. +
  122. +config USB_LEDS_TRIGGER_USBPORT
  123. + tristate "USB port LED trigger"
  124. + depends on USB && LEDS_TRIGGERS
  125. + help
  126. + This driver allows LEDs to be controlled by USB events. Enabling this
  127. + trigger allows specifying list of USB ports that should turn on LED
  128. + when some USB device gets connected.
  129. --- a/drivers/usb/core/Makefile
  130. +++ b/drivers/usb/core/Makefile
  131. @@ -11,3 +11,5 @@ usbcore-$(CONFIG_PCI) += hcd-pci.o
  132. usbcore-$(CONFIG_ACPI) += usb-acpi.o
  133. obj-$(CONFIG_USB) += usbcore.o
  134. +
  135. +obj-$(CONFIG_USB_LEDS_TRIGGER_USBPORT) += ledtrig-usbport.o
  136. --- /dev/null
  137. +++ b/drivers/usb/core/ledtrig-usbport.c
  138. @@ -0,0 +1,314 @@
  139. +/*
  140. + * USB port LED trigger
  141. + *
  142. + * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
  143. + *
  144. + * This program is free software; you can redistribute it and/or modify
  145. + * it under the terms of the GNU General Public License version 2 as
  146. + * published by the Free Software Foundation.
  147. + */
  148. +
  149. +#include <linux/device.h>
  150. +#include <linux/leds.h>
  151. +#include <linux/module.h>
  152. +#include <linux/slab.h>
  153. +#include <linux/usb.h>
  154. +
  155. +struct usbport_trig_data {
  156. + struct led_classdev *led_cdev;
  157. + struct list_head ports;
  158. + struct notifier_block nb;
  159. + int count; /* Amount of connected matching devices */
  160. +};
  161. +
  162. +struct usbport_trig_port {
  163. + struct usbport_trig_data *data;
  164. + struct usb_device *hub;
  165. + int portnum;
  166. + char *port_name;
  167. + bool observed;
  168. + struct device_attribute attr;
  169. + struct list_head list;
  170. +};
  171. +
  172. +/***************************************
  173. + * Helpers
  174. + ***************************************/
  175. +
  176. +/**
  177. + * usbport_trig_usb_dev_observed - Check if dev is connected to observed port
  178. + */
  179. +static bool usbport_trig_usb_dev_observed(struct usbport_trig_data *usbport_data,
  180. + struct usb_device *usb_dev)
  181. +{
  182. + struct usbport_trig_port *port;
  183. +
  184. + if (!usb_dev->parent)
  185. + return false;
  186. +
  187. + list_for_each_entry(port, &usbport_data->ports, list) {
  188. + if (usb_dev->parent == port->hub &&
  189. + usb_dev->portnum == port->portnum)
  190. + return port->observed;
  191. + }
  192. +
  193. + return false;
  194. +}
  195. +
  196. +static int usbport_trig_usb_dev_check(struct usb_device *usb_dev, void *data)
  197. +{
  198. + struct usbport_trig_data *usbport_data = data;
  199. +
  200. + if (usbport_trig_usb_dev_observed(usbport_data, usb_dev))
  201. + usbport_data->count++;
  202. +
  203. + return 0;
  204. +}
  205. +
  206. +/**
  207. + * usbport_trig_update_count - Recalculate amount of connected matching devices
  208. + */
  209. +static void usbport_trig_update_count(struct usbport_trig_data *usbport_data)
  210. +{
  211. + struct led_classdev *led_cdev = usbport_data->led_cdev;
  212. +
  213. + usbport_data->count = 0;
  214. + usb_for_each_dev(usbport_data, usbport_trig_usb_dev_check);
  215. + led_cdev->brightness_set(led_cdev,
  216. + usbport_data->count ? LED_FULL : LED_OFF);
  217. +}
  218. +
  219. +/***************************************
  220. + * Device attr
  221. + ***************************************/
  222. +
  223. +static ssize_t usbport_trig_port_show(struct device *dev,
  224. + struct device_attribute *attr, char *buf)
  225. +{
  226. + struct usbport_trig_port *port = container_of(attr,
  227. + struct usbport_trig_port,
  228. + attr);
  229. +
  230. + return sprintf(buf, "%d\n", port->observed) + 1;
  231. +}
  232. +
  233. +static ssize_t usbport_trig_port_store(struct device *dev,
  234. + struct device_attribute *attr,
  235. + const char *buf, size_t size)
  236. +{
  237. + struct usbport_trig_port *port = container_of(attr,
  238. + struct usbport_trig_port,
  239. + attr);
  240. +
  241. + if (!strcmp(buf, "0") || !strcmp(buf, "0\n"))
  242. + port->observed = 0;
  243. + else if (!strcmp(buf, "1") || !strcmp(buf, "1\n"))
  244. + port->observed = 1;
  245. + else
  246. + return -EINVAL;
  247. +
  248. + usbport_trig_update_count(port->data);
  249. +
  250. + return size;
  251. +}
  252. +
  253. +static struct attribute *ports_attrs[] = {
  254. + NULL,
  255. +};
  256. +static const struct attribute_group ports_group = {
  257. + .name = "ports",
  258. + .attrs = ports_attrs,
  259. +};
  260. +
  261. +/***************************************
  262. + * Adding & removing ports
  263. + ***************************************/
  264. +
  265. +static int usbport_trig_add_port(struct usbport_trig_data *usbport_data,
  266. + struct usb_device *usb_dev,
  267. + const char *hub_name, int portnum)
  268. +{
  269. + struct led_classdev *led_cdev = usbport_data->led_cdev;
  270. + struct usbport_trig_port *port;
  271. + size_t len;
  272. + int err;
  273. +
  274. + port = kzalloc(sizeof(*port), GFP_KERNEL);
  275. + if (!port) {
  276. + err = -ENOMEM;
  277. + goto err_out;
  278. + }
  279. +
  280. + port->data = usbport_data;
  281. + port->hub = usb_dev;
  282. + port->portnum = portnum;
  283. +
  284. + len = strlen(hub_name) + 8;
  285. + port->port_name = kzalloc(len, GFP_KERNEL);
  286. + if (!port->port_name) {
  287. + err = -ENOMEM;
  288. + goto err_free_port;
  289. + }
  290. + snprintf(port->port_name, len, "%s-port%d", hub_name, portnum);
  291. +
  292. + port->attr.attr.name = port->port_name;
  293. + port->attr.attr.mode = S_IRUSR | S_IWUSR;
  294. + port->attr.show = usbport_trig_port_show;
  295. + port->attr.store = usbport_trig_port_store;
  296. +
  297. + err = sysfs_add_file_to_group(&led_cdev->dev->kobj, &port->attr.attr,
  298. + ports_group.name);
  299. + if (err)
  300. + goto err_free_port_name;
  301. +
  302. + list_add_tail(&port->list, &usbport_data->ports);
  303. +
  304. + return 0;
  305. +
  306. +err_free_port_name:
  307. + kfree(port->port_name);
  308. +err_free_port:
  309. + kfree(port);
  310. +err_out:
  311. + return err;
  312. +}
  313. +
  314. +static int usbport_trig_add_usb_dev_ports(struct usb_device *usb_dev,
  315. + void *data)
  316. +{
  317. + struct usbport_trig_data *usbport_data = data;
  318. + int i;
  319. +
  320. + for (i = 1; i <= usb_dev->maxchild; i++)
  321. + usbport_trig_add_port(usbport_data, usb_dev,
  322. + dev_name(&usb_dev->dev), i);
  323. +
  324. + return 0;
  325. +}
  326. +
  327. +static void usbport_trig_remove_port(struct usbport_trig_data *usbport_data,
  328. + struct usbport_trig_port *port)
  329. +{
  330. + struct led_classdev *led_cdev = usbport_data->led_cdev;
  331. +
  332. + list_del(&port->list);
  333. + sysfs_remove_file_from_group(&led_cdev->dev->kobj, &port->attr.attr,
  334. + ports_group.name);
  335. + kfree(port->port_name);
  336. + kfree(port);
  337. +}
  338. +
  339. +static void usbport_trig_remove_usb_dev_ports(struct usbport_trig_data *usbport_data,
  340. + struct usb_device *usb_dev)
  341. +{
  342. + struct usbport_trig_port *port, *tmp;
  343. +
  344. + list_for_each_entry_safe(port, tmp, &usbport_data->ports, list) {
  345. + if (port->hub == usb_dev)
  346. + usbport_trig_remove_port(usbport_data, port);
  347. + }
  348. +}
  349. +
  350. +/***************************************
  351. + * Init, exit, etc.
  352. + ***************************************/
  353. +
  354. +static int usbport_trig_notify(struct notifier_block *nb, unsigned long action,
  355. + void *data)
  356. +{
  357. + struct usbport_trig_data *usbport_data =
  358. + container_of(nb, struct usbport_trig_data, nb);
  359. + struct led_classdev *led_cdev = usbport_data->led_cdev;
  360. + struct usb_device *usb_dev = data;
  361. + bool observed;
  362. +
  363. + observed = usbport_trig_usb_dev_observed(usbport_data, usb_dev);
  364. +
  365. + switch (action) {
  366. + case USB_DEVICE_ADD:
  367. + usbport_trig_add_usb_dev_ports(usb_dev, usbport_data);
  368. + if (observed && usbport_data->count++ == 0)
  369. + led_cdev->brightness_set(led_cdev, LED_FULL);
  370. + return NOTIFY_OK;
  371. + case USB_DEVICE_REMOVE:
  372. + usbport_trig_remove_usb_dev_ports(usbport_data, usb_dev);
  373. + if (observed && --usbport_data->count == 0)
  374. + led_cdev->brightness_set(led_cdev, LED_OFF);
  375. + return NOTIFY_OK;
  376. + }
  377. +
  378. + return NOTIFY_DONE;
  379. +}
  380. +
  381. +static void usbport_trig_activate(struct led_classdev *led_cdev)
  382. +{
  383. + struct usbport_trig_data *usbport_data;
  384. + int err;
  385. +
  386. + usbport_data = kzalloc(sizeof(*usbport_data), GFP_KERNEL);
  387. + if (!usbport_data)
  388. + return;
  389. + usbport_data->led_cdev = led_cdev;
  390. +
  391. + /* List of ports */
  392. + INIT_LIST_HEAD(&usbport_data->ports);
  393. + err = sysfs_create_group(&led_cdev->dev->kobj, &ports_group);
  394. + if (err)
  395. + goto err_free;
  396. + usb_for_each_dev(usbport_data, usbport_trig_add_usb_dev_ports);
  397. +
  398. + /* Notifications */
  399. + usbport_data->nb.notifier_call = usbport_trig_notify,
  400. + led_cdev->trigger_data = usbport_data;
  401. + usb_register_notify(&usbport_data->nb);
  402. +
  403. + led_cdev->activated = true;
  404. + return;
  405. +
  406. +err_free:
  407. + kfree(usbport_data);
  408. +}
  409. +
  410. +static void usbport_trig_deactivate(struct led_classdev *led_cdev)
  411. +{
  412. + struct usbport_trig_data *usbport_data = led_cdev->trigger_data;
  413. + struct usbport_trig_port *port, *tmp;
  414. +
  415. + if (!led_cdev->activated)
  416. + return;
  417. +
  418. + list_for_each_entry_safe(port, tmp, &usbport_data->ports, list) {
  419. + usbport_trig_remove_port(usbport_data, port);
  420. + }
  421. +
  422. + usb_unregister_notify(&usbport_data->nb);
  423. +
  424. + sysfs_remove_group(&led_cdev->dev->kobj, &ports_group);
  425. +
  426. + kfree(usbport_data);
  427. +
  428. + led_cdev->activated = false;
  429. +}
  430. +
  431. +static struct led_trigger usbport_led_trigger = {
  432. + .name = "usbport",
  433. + .activate = usbport_trig_activate,
  434. + .deactivate = usbport_trig_deactivate,
  435. +};
  436. +
  437. +static int __init usbport_trig_init(void)
  438. +{
  439. + return led_trigger_register(&usbport_led_trigger);
  440. +}
  441. +
  442. +static void __exit usbport_trig_exit(void)
  443. +{
  444. + led_trigger_unregister(&usbport_led_trigger);
  445. +}
  446. +
  447. +module_init(usbport_trig_init);
  448. +module_exit(usbport_trig_exit);
  449. +
  450. +MODULE_AUTHOR("Rafał Miłecki <rafal@milecki.pl>");
  451. +MODULE_DESCRIPTION("USB port trigger");
  452. +MODULE_LICENSE("GPL v2");