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