of.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * of.c The helpers for hcd device tree support
  4. *
  5. * Copyright (C) 2016 Freescale Semiconductor, Inc.
  6. * Author: Peter Chen <peter.chen@freescale.com>
  7. * Copyright (C) 2017 Johan Hovold <johan@kernel.org>
  8. */
  9. #include <linux/of.h>
  10. #include <linux/of_graph.h>
  11. #include <linux/usb/of.h>
  12. /**
  13. * usb_of_get_device_node() - get a USB device node
  14. * @hub: hub to which device is connected
  15. * @port1: one-based index of port
  16. *
  17. * Look up the node of a USB device given its parent hub device and one-based
  18. * port number.
  19. *
  20. * Return: A pointer to the node with incremented refcount if found, or
  21. * %NULL otherwise.
  22. */
  23. struct device_node *usb_of_get_device_node(struct usb_device *hub, int port1)
  24. {
  25. struct device_node *node;
  26. u32 reg;
  27. for_each_child_of_node(hub->dev.of_node, node) {
  28. if (of_property_read_u32(node, "reg", &reg))
  29. continue;
  30. if (reg == port1)
  31. return node;
  32. }
  33. return NULL;
  34. }
  35. EXPORT_SYMBOL_GPL(usb_of_get_device_node);
  36. /**
  37. * usb_of_has_combined_node() - determine whether a device has a combined node
  38. * @udev: USB device
  39. *
  40. * Determine whether a USB device has a so called combined node which is
  41. * shared with its sole interface. This is the case if and only if the device
  42. * has a node and its descriptors report the following:
  43. *
  44. * 1) bDeviceClass is 0 or 9, and
  45. * 2) bNumConfigurations is 1, and
  46. * 3) bNumInterfaces is 1.
  47. *
  48. * Return: True iff the device has a device node and its descriptors match the
  49. * criteria for a combined node.
  50. */
  51. bool usb_of_has_combined_node(struct usb_device *udev)
  52. {
  53. struct usb_device_descriptor *ddesc = &udev->descriptor;
  54. struct usb_config_descriptor *cdesc;
  55. if (!udev->dev.of_node)
  56. return false;
  57. switch (ddesc->bDeviceClass) {
  58. case USB_CLASS_PER_INTERFACE:
  59. case USB_CLASS_HUB:
  60. if (ddesc->bNumConfigurations == 1) {
  61. cdesc = &udev->config->desc;
  62. if (cdesc->bNumInterfaces == 1)
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. EXPORT_SYMBOL_GPL(usb_of_has_combined_node);
  69. static bool usb_of_has_devices_or_graph(const struct usb_device *hub)
  70. {
  71. const struct device_node *np = hub->dev.of_node;
  72. struct device_node *child;
  73. if (of_graph_is_present(np))
  74. return true;
  75. for_each_child_of_node(np, child) {
  76. if (of_property_present(child, "reg")) {
  77. of_node_put(child);
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * usb_of_get_connect_type() - get a USB hub's port connect_type
  85. * @hub: hub to which port is for @port1
  86. * @port1: one-based index of port
  87. *
  88. * Get the connect_type of @port1 based on the device node for @hub. If the
  89. * port is described in the OF graph, the connect_type is "hotplug". If the
  90. * @hub has a child device has with a 'reg' property equal to @port1 the
  91. * connect_type is "hard-wired". If there isn't an OF graph or child node at
  92. * all then the connect_type is "unknown". Otherwise, the port is considered
  93. * "unused" because it isn't described at all.
  94. *
  95. * Return: A connect_type for @port1 based on the device node for @hub.
  96. */
  97. enum usb_port_connect_type usb_of_get_connect_type(struct usb_device *hub, int port1)
  98. {
  99. struct device_node *np, *child, *ep, *remote_np;
  100. enum usb_port_connect_type connect_type;
  101. /* Only set connect_type if binding has ports/hardwired devices. */
  102. if (!usb_of_has_devices_or_graph(hub))
  103. return USB_PORT_CONNECT_TYPE_UNKNOWN;
  104. /* Assume port is unused if there's a graph or a child node. */
  105. connect_type = USB_PORT_NOT_USED;
  106. np = hub->dev.of_node;
  107. /*
  108. * Hotplug ports are connected to an available remote node, e.g.
  109. * usb-a-connector compatible node, in the OF graph.
  110. */
  111. if (of_graph_is_present(np)) {
  112. ep = of_graph_get_endpoint_by_regs(np, port1, -1);
  113. if (ep) {
  114. remote_np = of_graph_get_remote_port_parent(ep);
  115. of_node_put(ep);
  116. if (of_device_is_available(remote_np))
  117. connect_type = USB_PORT_CONNECT_TYPE_HOT_PLUG;
  118. of_node_put(remote_np);
  119. }
  120. }
  121. /*
  122. * Hard-wired ports are child nodes with a reg property corresponding
  123. * to the port number, i.e. a usb device.
  124. */
  125. child = usb_of_get_device_node(hub, port1);
  126. if (of_device_is_available(child))
  127. connect_type = USB_PORT_CONNECT_TYPE_HARD_WIRED;
  128. of_node_put(child);
  129. return connect_type;
  130. }
  131. EXPORT_SYMBOL_GPL(usb_of_get_connect_type);
  132. /**
  133. * usb_of_get_interface_node() - get a USB interface node
  134. * @udev: USB device of interface
  135. * @config: configuration value
  136. * @ifnum: interface number
  137. *
  138. * Look up the node of a USB interface given its USB device, configuration
  139. * value and interface number.
  140. *
  141. * Return: A pointer to the node with incremented refcount if found, or
  142. * %NULL otherwise.
  143. */
  144. struct device_node *
  145. usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
  146. {
  147. struct device_node *node;
  148. u32 reg[2];
  149. for_each_child_of_node(udev->dev.of_node, node) {
  150. if (of_property_read_u32_array(node, "reg", reg, 2))
  151. continue;
  152. if (reg[0] == ifnum && reg[1] == config)
  153. return node;
  154. }
  155. return NULL;
  156. }
  157. EXPORT_SYMBOL_GPL(usb_of_get_interface_node);