084-0003-usb-core-read-USB-ports-from-DT-in-the-usbport-LED-t.patch 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. From 4f04c210d031667e503d6538a72345a36f3b5d71 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
  3. Date: Thu, 8 Jun 2017 18:08:32 +0200
  4. Subject: [PATCH] usb: core: read USB ports from DT in the usbport LED trigger
  5. driver
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. This uses DT info to read relation description of LEDs and USB ports. If
  10. DT has properly described LEDs, trigger will know when to turn them on.
  11. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
  12. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  13. ---
  14. drivers/usb/core/ledtrig-usbport.c | 56 ++++++++++++++++++++++++++++++++++++++
  15. 1 file changed, 56 insertions(+)
  16. --- a/drivers/usb/core/ledtrig-usbport.c
  17. +++ b/drivers/usb/core/ledtrig-usbport.c
  18. @@ -11,8 +11,10 @@
  19. #include <linux/device.h>
  20. #include <linux/leds.h>
  21. #include <linux/module.h>
  22. +#include <linux/of.h>
  23. #include <linux/slab.h>
  24. #include <linux/usb.h>
  25. +#include <linux/usb/of.h>
  26. struct usbport_trig_data {
  27. struct led_classdev *led_cdev;
  28. @@ -123,6 +125,57 @@ static const struct attribute_group port
  29. * Adding & removing ports
  30. ***************************************/
  31. +/**
  32. + * usbport_trig_port_observed - Check if port should be observed
  33. + */
  34. +static bool usbport_trig_port_observed(struct usbport_trig_data *usbport_data,
  35. + struct usb_device *usb_dev, int port1)
  36. +{
  37. + struct device *dev = usbport_data->led_cdev->dev;
  38. + struct device_node *led_np = dev->of_node;
  39. + struct of_phandle_args args;
  40. + struct device_node *port_np;
  41. + int count, i;
  42. +
  43. + if (!led_np)
  44. + return false;
  45. +
  46. + /* Get node of port being added */
  47. + port_np = usb_of_get_child_node(usb_dev->dev.of_node, port1);
  48. + if (!port_np)
  49. + return false;
  50. +
  51. + /* Amount of trigger sources for this LED */
  52. + count = of_count_phandle_with_args(led_np, "trigger-sources",
  53. + "#trigger-source-cells");
  54. + if (count < 0) {
  55. + dev_warn(dev, "Failed to get trigger sources for %s\n",
  56. + led_np->full_name);
  57. + return false;
  58. + }
  59. +
  60. + /* Check list of sources for this specific port */
  61. + for (i = 0; i < count; i++) {
  62. + int err;
  63. +
  64. + err = of_parse_phandle_with_args(led_np, "trigger-sources",
  65. + "#trigger-source-cells", i,
  66. + &args);
  67. + if (err) {
  68. + dev_err(dev, "Failed to get trigger source phandle at index %d: %d\n",
  69. + i, err);
  70. + continue;
  71. + }
  72. +
  73. + of_node_put(args.np);
  74. +
  75. + if (args.np == port_np)
  76. + return true;
  77. + }
  78. +
  79. + return false;
  80. +}
  81. +
  82. static int usbport_trig_add_port(struct usbport_trig_data *usbport_data,
  83. struct usb_device *usb_dev,
  84. const char *hub_name, int portnum)
  85. @@ -141,6 +194,8 @@ static int usbport_trig_add_port(struct
  86. port->data = usbport_data;
  87. port->hub = usb_dev;
  88. port->portnum = portnum;
  89. + port->observed = usbport_trig_port_observed(usbport_data, usb_dev,
  90. + portnum);
  91. len = strlen(hub_name) + 8;
  92. port->port_name = kzalloc(len, GFP_KERNEL);
  93. @@ -255,6 +310,7 @@ static void usbport_trig_activate(struct
  94. if (err)
  95. goto err_free;
  96. usb_for_each_dev(usbport_data, usbport_trig_add_usb_dev_ports);
  97. + usbport_trig_update_count(usbport_data);
  98. /* Notifications */
  99. usbport_data->nb.notifier_call = usbport_trig_notify,