usbhost_findclass.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /****************************************************************************
  2. * drivers/usbhost/usbhost_findclass.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdbool.h>
  26. #include <assert.h>
  27. #include <debug.h>
  28. #include <nuttx/irq.h>
  29. #include <nuttx/usb/usb.h>
  30. #include <nuttx/usb/usbhost.h>
  31. #include "usbhost_registry.h"
  32. /****************************************************************************
  33. * Private Functions
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Name: usbhost_idmatch
  37. *
  38. * Description:
  39. * Check if the class ID matches what the host controller found.
  40. *
  41. * Input Parameters:
  42. * classid - ID info for the class under consideration.
  43. * devid - ID info reported by the device.
  44. *
  45. * Returned Value:
  46. * TRUE - the class will support this device.
  47. *
  48. ****************************************************************************/
  49. static bool usbhost_idmatch(const struct usbhost_id_s *classid,
  50. const struct usbhost_id_s *devid)
  51. {
  52. uinfo("Compare to class:%d subclass:%d protocol:%d vid:%04x pid:%04x\n",
  53. classid->base, classid->subclass, classid->proto,
  54. classid->vid, classid->pid);
  55. /* The base class ID, subclass and protocol have to match up in any event */
  56. if (devid->base == classid->base &&
  57. devid->subclass == classid->subclass &&
  58. devid->proto == classid->proto)
  59. {
  60. /* If this is a vendor-specific class ID, then the VID and PID have to
  61. * match as well.
  62. */
  63. if (devid->base == USB_CLASS_VENDOR_SPEC)
  64. {
  65. /* Vendor specific... do the VID and PID also match? */
  66. if (devid->vid == classid->vid && devid->pid == classid->pid)
  67. {
  68. /* Yes.. then we have a match */
  69. return true;
  70. }
  71. }
  72. else
  73. {
  74. /* Not vendor specific? Then we have a match */
  75. return true;
  76. }
  77. }
  78. /* No match.. not supported */
  79. return false;
  80. }
  81. /****************************************************************************
  82. * Public Functions
  83. ****************************************************************************/
  84. /****************************************************************************
  85. * Name: usbhost_findclass
  86. *
  87. * Description:
  88. * Find a USB host class implementation previously registered by
  89. * usbhost_registerclass(). On success, an instance of struct
  90. * usbhost_registry_s will be returned. That instance will contain all of
  91. * the information that will be needed to obtain and bind a struct
  92. * usbhost_class_s instance for the device.
  93. *
  94. * Input Parameters:
  95. * id - Identifies the USB device class that has connect to the USB host.
  96. *
  97. * Returned Value:
  98. * On success this function will return a non-NULL instance of struct
  99. * usbhost_registry_s. NULL will be returned on failure. This function
  100. * can only fail if (1) id is NULL, or (2) no USB host class is registered
  101. * that matches the device class ID.
  102. *
  103. ****************************************************************************/
  104. const struct usbhost_registry_s *usbhost_findclass(
  105. const struct usbhost_id_s *id)
  106. {
  107. struct usbhost_registry_s *usbclass;
  108. irqstate_t flags;
  109. int ndx;
  110. DEBUGASSERT(id);
  111. uinfo("Looking for class:%d subclass:%d protocol:%d vid:%04x pid:%04x\n",
  112. id->base, id->subclass, id->proto, id->vid, id->pid);
  113. /* g_classregistry is a singly-linked list of class ID information added by
  114. * calls to usbhost_registerclass(). Since this list is accessed from USB
  115. * host controller interrupt handling logic, accesses to this list must be
  116. * protected by disabling interrupts.
  117. */
  118. flags = enter_critical_section();
  119. /* Examine each register class in the linked list */
  120. for (usbclass = g_classregistry; usbclass; usbclass = usbclass->flink)
  121. {
  122. /* If the registered class supports more than one ID, subclass, or
  123. * protocol, then try each.
  124. */
  125. uinfo("Checking class:%p nids:%d\n", usbclass, usbclass->nids);
  126. for (ndx = 0; ndx < usbclass->nids; ndx++)
  127. {
  128. /* Did we find a matching ID? */
  129. if (usbhost_idmatch(&usbclass->id[ndx], id))
  130. {
  131. /* Yes.. restore interrupts and return the class info */
  132. leave_critical_section(flags);
  133. return usbclass;
  134. }
  135. }
  136. }
  137. /* Not found... restore interrupts and return NULL */
  138. leave_critical_section(flags);
  139. return NULL;
  140. }