of.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_platform.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. /**
  70. * usb_of_get_interface_node() - get a USB interface node
  71. * @udev: USB device of interface
  72. * @config: configuration value
  73. * @ifnum: interface number
  74. *
  75. * Look up the node of a USB interface given its USB device, configuration
  76. * value and interface number.
  77. *
  78. * Return: A pointer to the node with incremented refcount if found, or
  79. * %NULL otherwise.
  80. */
  81. struct device_node *
  82. usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
  83. {
  84. struct device_node *node;
  85. u32 reg[2];
  86. for_each_child_of_node(udev->dev.of_node, node) {
  87. if (of_property_read_u32_array(node, "reg", reg, 2))
  88. continue;
  89. if (reg[0] == ifnum && reg[1] == config)
  90. return node;
  91. }
  92. return NULL;
  93. }
  94. EXPORT_SYMBOL_GPL(usb_of_get_interface_node);