|
| 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 | +int 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 | + ret = usbh_class_init(c_node->c_data, uhs_ctx); |
| 23 | + if (ret != 0) { |
| 24 | + LOG_ERR("Failed to initialize all registered class instances"); |
| 25 | + return ret; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return 0; |
| 30 | +} |
| 31 | + |
| 32 | +int usbh_class_remove_all(const struct usb_device *const udev) |
| 33 | +{ |
| 34 | + int ret; |
| 35 | + |
| 36 | + STRUCT_SECTION_FOREACH(usbh_class_node, c_node) { |
| 37 | + if (c_node->c_data->udev == udev) { |
| 38 | + ret = usbh_class_removed(c_node->c_data); |
| 39 | + if (ret != 0) { |
| 40 | + LOG_ERR("Failed to handle device removal for each classes"); |
| 41 | + return ret; |
| 42 | + } |
| 43 | + |
| 44 | + /* The class instance is now free to bind to a new device */ |
| 45 | + c_node->c_data->udev = NULL; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +static int usbh_class_probe_each_iface(struct usbh_class_data *const c_data, |
| 53 | + struct usb_device *const udev) |
| 54 | +{ |
| 55 | + const struct usb_cfg_descriptor *const cfg_desc = usbh_desc_get_cfg_beg(udev); |
| 56 | + int ret; |
| 57 | + |
| 58 | + for (uint8_t next, iface = 0; iface < cfg_desc->bNumInterfaces; iface = next) { |
| 59 | + struct usbh_class_filter device_info = {}; |
| 60 | + |
| 61 | + ret = usbh_desc_get_device_info(&device_info, udev, iface); |
| 62 | + if (ret < 0) { |
| 63 | + LOG_ERR("Failed to collect class codes for matching interface %u", iface); |
| 64 | + return ret; |
| 65 | + } |
| 66 | + next = ret; |
| 67 | + |
| 68 | + if (!usbh_class_is_matching(c_data->filters, c_data->num_filters, &device_info)) { |
| 69 | + LOG_DBG("Class %s not matching interface %u", c_data->name, iface); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + ret = usbh_class_probe(c_data, udev, iface); |
| 74 | + if (ret == -ENOTSUP) { |
| 75 | + LOG_DBG("Class %s not supporting this device, skipping", c_data->name); |
| 76 | + continue; |
| 77 | + } |
| 78 | + if (ret != 0) { |
| 79 | + LOG_ERR("Error while probing the class %s for interface %u", |
| 80 | + c_data->name, iface); |
| 81 | + return ret; |
| 82 | + } |
| 83 | + |
| 84 | + LOG_INF("Class %s matches this device's interface %u", c_data->name, iface); |
| 85 | + c_data->udev = udev; |
| 86 | + c_data->iface = iface; |
| 87 | + |
| 88 | + return 0; |
| 89 | + } |
| 90 | + |
| 91 | + return -ENOTSUP; |
| 92 | +} |
| 93 | + |
| 94 | +int usbh_class_probe_all(struct usb_device *const udev) |
| 95 | +{ |
| 96 | + bool matching = false; |
| 97 | + int ret; |
| 98 | + |
| 99 | + STRUCT_SECTION_FOREACH(usbh_class_node, c_node) { |
| 100 | + struct usbh_class_data *const c_data = c_node->c_data; |
| 101 | + |
| 102 | + /* If the class is not already having a device */ |
| 103 | + if (c_data->udev == NULL) { |
| 104 | + ret = usbh_class_probe_each_iface(c_data, udev); |
| 105 | + if (ret == -ENOTSUP) { |
| 106 | + LOG_DBG("This device has no %s class matching", c_data->name); |
| 107 | + continue; |
| 108 | + } |
| 109 | + if (ret != 0) { |
| 110 | + return ret; |
| 111 | + } |
| 112 | + matching = true; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (!matching) { |
| 117 | + LOG_WRN("Could not find any class for this device"); |
| 118 | + return -ENOTSUP; |
| 119 | + } |
| 120 | + |
| 121 | + return 0; |
| 122 | +} |
| 123 | + |
| 124 | +bool usbh_class_is_matching(struct usbh_class_filter *const filters, size_t num_filters, |
| 125 | + struct usbh_class_filter *const device_info) |
| 126 | +{ |
| 127 | + /* Make empty filter set match everything (use class_api->probe() only) */ |
| 128 | + if (num_filters == 0) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + /* Try to find a rule that matches completely */ |
| 133 | + for (int i = 0; i < num_filters; i++) { |
| 134 | + const struct usbh_class_filter *filt = &filters[i]; |
| 135 | + |
| 136 | + if ((filt->flags & USBH_CLASS_MATCH_VID) && |
| 137 | + device_info->vid != filt->vid) { |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + if ((filt->flags & USBH_CLASS_MATCH_PID) && |
| 142 | + device_info->pid != filt->pid) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + if (filt->flags & USBH_CLASS_MATCH_CLASS && |
| 147 | + device_info->class != filt->class) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + if ((filt->flags & USBH_CLASS_MATCH_SUB) && |
| 152 | + device_info->sub != filt->sub) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + if ((filt->flags & USBH_CLASS_MATCH_PROTO) && |
| 157 | + device_info->proto != filt->proto) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + |
| 161 | + /* All the selected filters did match */ |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + /* At the end of the filter table and still no match */ |
| 166 | + return false; |
| 167 | +} |
0 commit comments