|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * Copyright 2025 NXP |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <zephyr/usb/usbh.h> |
| 9 | +#include <zephyr/logging/log.h> |
| 10 | + |
| 11 | +#include "usbh_class.h" |
| 12 | +#include "usbh_class_api.h" |
| 13 | +#include "usbh_desc.h" |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(usbh_class, CONFIG_USBH_LOG_LEVEL); |
| 16 | + |
| 17 | +void usbh_class_init_all(struct usbh_context *const uhs_ctx) |
| 18 | +{ |
| 19 | + int ret; |
| 20 | + |
| 21 | + STRUCT_SECTION_FOREACH(usbh_class_node, c_node) { |
| 22 | + struct usbh_class_data *const c_data = c_node->c_data; |
| 23 | + |
| 24 | + if (c_node->state != USBH_CLASS_STATE_IDLE) { |
| 25 | + LOG_DBG("Skipping '%s' in state %u", c_data->name, c_node->state); |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + ret = usbh_class_init(c_data, uhs_ctx); |
| 30 | + if (ret != 0) { |
| 31 | + LOG_WRN("Failed to initialize class %s (%d)", c_data->name, ret); |
| 32 | + c_node->state = USBH_CLASS_STATE_ERROR; |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void usbh_class_remove_all(struct usb_device *const udev) |
| 38 | +{ |
| 39 | + int ret; |
| 40 | + |
| 41 | + k_mutex_lock(&udev->mutex, K_FOREVER); |
| 42 | + |
| 43 | + STRUCT_SECTION_FOREACH(usbh_class_node, c_node) { |
| 44 | + struct usbh_class_data *const c_data = c_node->c_data; |
| 45 | + |
| 46 | + if (c_data->udev == udev) { |
| 47 | + ret = usbh_class_removed(c_data); |
| 48 | + if (ret != 0) { |
| 49 | + LOG_ERR("Failed to handle device removal for each class (%d)", ret); |
| 50 | + c_node->state = USBH_CLASS_STATE_ERROR; |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + /* The class instance is now free to bind to a new device */ |
| 55 | + c_data->udev = NULL; |
| 56 | + c_node->state = USBH_CLASS_STATE_IDLE; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + k_mutex_unlock(&udev->mutex); |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * Probe an USB device function against all available classes of the system. |
| 65 | + * |
| 66 | + * Try to match a class from the global list of all system classes, using their filter rules |
| 67 | + * and return status to tell if a class matches or not. |
| 68 | + * |
| 69 | + * The first matched class will stop the loop, and the status will be updated so that classes |
| 70 | + * are only matched for a single USB function at a time. |
| 71 | + * |
| 72 | + * USB functions will only have one class matching, and calling usbh_class_probe_function() |
| 73 | + * multiple times consequently has no effect. |
| 74 | + */ |
| 75 | +static void usbh_class_probe_function(struct usb_device *const udev, |
| 76 | + struct usbh_class_filter *const info, uint8_t iface) |
| 77 | +{ |
| 78 | + int ret; |
| 79 | + |
| 80 | + /* Assumes that udev->mutex is locked */ |
| 81 | + |
| 82 | + /* First check if any interface is already bound to this */ |
| 83 | + STRUCT_SECTION_FOREACH(usbh_class_node, c_node) { |
| 84 | + struct usbh_class_data *const c_data = c_node->c_data; |
| 85 | + |
| 86 | + if (c_node->state == USBH_CLASS_STATE_BOUND && |
| 87 | + c_data->udev == udev && c_data->iface == iface) { |
| 88 | + LOG_DBG("Interface %u bound to '%s', skipping", iface, c_data->name); |
| 89 | + return; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /* Then try to match this function against all interfaces */ |
| 94 | + STRUCT_SECTION_FOREACH(usbh_class_node, c_node) { |
| 95 | + struct usbh_class_data *const c_data = c_node->c_data; |
| 96 | + |
| 97 | + if (c_node->state != USBH_CLASS_STATE_IDLE) { |
| 98 | + LOG_DBG("Class %s already matched, skipping", c_data->name); |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if (!usbh_class_is_matching(c_node->filters, c_node->num_filters, info)) { |
| 103 | + LOG_DBG("Class %s not matching interface %u", c_data->name, iface); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + ret = usbh_class_probe(c_data, udev, iface); |
| 108 | + if (ret == -ENOTSUP) { |
| 109 | + LOG_DBG("Class %s not supporting this device, skipping", c_data->name); |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + LOG_INF("Class '%s' matches interface %u", c_data->name, iface); |
| 114 | + c_node->state = USBH_CLASS_STATE_BOUND; |
| 115 | + c_data->udev = udev; |
| 116 | + c_data->iface = iface; |
| 117 | + break; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +void usbh_class_probe_device(struct usb_device *const udev) |
| 122 | +{ |
| 123 | + const void *desc_end; |
| 124 | + const struct usb_desc_header *desc; |
| 125 | + struct usbh_class_filter info; |
| 126 | + uint8_t iface; |
| 127 | + int ret; |
| 128 | + |
| 129 | + k_mutex_lock(&udev->mutex, K_FOREVER); |
| 130 | + |
| 131 | + desc = usbh_desc_get_cfg_beg(udev); |
| 132 | + desc_end = usbh_desc_get_cfg_end(udev); |
| 133 | + info.vid = udev->dev_desc.idVendor; |
| 134 | + info.pid = udev->dev_desc.idProduct; |
| 135 | + |
| 136 | + while (true) { |
| 137 | + desc = usbh_desc_get_next_function(desc, desc_end); |
| 138 | + if (desc == NULL) { |
| 139 | + break; |
| 140 | + } |
| 141 | + |
| 142 | + ret = usbh_desc_get_iface_info(desc, &info, &iface); |
| 143 | + if (ret < 0) { |
| 144 | + LOG_ERR("Failed to collect class codes for matching interface %u", iface); |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + usbh_class_probe_function(udev, &info, iface); |
| 149 | + } |
| 150 | + |
| 151 | + k_mutex_unlock(&udev->mutex); |
| 152 | +} |
| 153 | + |
| 154 | +bool usbh_class_is_matching(struct usbh_class_filter *const filters, size_t num_filters, |
| 155 | + struct usbh_class_filter *const info) |
| 156 | +{ |
| 157 | + /* Make empty filter set match everything (use class_api->probe() only) */ |
| 158 | + if (num_filters == 0) { |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + /* Try to find a rule that matches completely */ |
| 163 | + for (int i = 0; i < num_filters; i++) { |
| 164 | + const struct usbh_class_filter *filt = &filters[i]; |
| 165 | + |
| 166 | + if ((filt->flags & USBH_CLASS_MATCH_VID) && |
| 167 | + info->vid != filt->vid) { |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + if ((filt->flags & USBH_CLASS_MATCH_PID) && |
| 172 | + info->pid != filt->pid) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + if (filt->flags & USBH_CLASS_MATCH_CLASS && |
| 177 | + info->class != filt->class) { |
| 178 | + continue; |
| 179 | + } |
| 180 | + |
| 181 | + if ((filt->flags & USBH_CLASS_MATCH_SUB) && |
| 182 | + info->sub != filt->sub) { |
| 183 | + continue; |
| 184 | + } |
| 185 | + |
| 186 | + if ((filt->flags & USBH_CLASS_MATCH_PROTO) && |
| 187 | + info->proto != filt->proto) { |
| 188 | + continue; |
| 189 | + } |
| 190 | + |
| 191 | + /* All the selected filters did match */ |
| 192 | + return true; |
| 193 | + } |
| 194 | + |
| 195 | + /* At the end of the filter table and still no match */ |
| 196 | + return false; |
| 197 | +} |
0 commit comments