|
10 | 10 |
|
11 | 11 | #include "usbh_class.h"
|
12 | 12 | #include "usbh_class_api.h"
|
| 13 | +#include "usbh_desc.h" |
13 | 14 |
|
14 | 15 | LOG_MODULE_REGISTER(usbh_class, CONFIG_USBH_LOG_LEVEL);
|
15 | 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_one(struct usbh_class_data *const c_data, struct usb_device *const udev) |
| 53 | +{ |
| 54 | + const uint8_t *const desc_beg = usbh_desc_get_cfg_beg(udev); |
| 55 | + const uint8_t *const desc_end = usbh_desc_get_cfg_end(udev); |
| 56 | + const struct usb_desc_header *desc; |
| 57 | + const struct usb_cfg_descriptor *cfg_desc; |
| 58 | + int ret; |
| 59 | + |
| 60 | + desc = usbh_desc_get_by_type(desc_beg, desc_end, USB_DESC_CONFIGURATION); |
| 61 | + if (desc == NULL) { |
| 62 | + LOG_ERR("Unable to find a configuration descriptor"); |
| 63 | + return -EBADMSG; |
| 64 | + } |
| 65 | + |
| 66 | + cfg_desc = (struct usb_cfg_descriptor *)desc; |
| 67 | + |
| 68 | + for (uint8_t ifnum = 0; ifnum < cfg_desc->bNumInterfaces; ifnum++) { |
| 69 | + ret = usbh_class_probe(c_data, udev, ifnum); |
| 70 | + if (ret == -ENOTSUP) { |
| 71 | + LOG_DBG("Class %s not supporting this device, skipping", c_data->name); |
| 72 | + continue; |
| 73 | + } |
| 74 | + if (ret != 0) { |
| 75 | + LOG_ERR("Failed to register the class %s for interface %u", |
| 76 | + c_data->name, ifnum); |
| 77 | + return ret; |
| 78 | + } |
| 79 | + if (ret == 0) { |
| 80 | + LOG_INF("Class %s is matching this device for interface %u, assigning it", |
| 81 | + c_data->name, ifnum); |
| 82 | + c_data->udev = udev; |
| 83 | + c_data->ifnum = ifnum; |
| 84 | + return 0; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return -ENOTSUP; |
| 89 | +} |
| 90 | + |
| 91 | +int usbh_class_probe_all(struct usb_device *const udev) |
| 92 | +{ |
| 93 | + int ret; |
| 94 | + |
| 95 | + STRUCT_SECTION_FOREACH(usbh_class_node, c_node) { |
| 96 | + struct usbh_class_data *const c_data = c_node->c_data; |
| 97 | + |
| 98 | + /* If the class is not already having a device */ |
| 99 | + if (c_data->udev == NULL) { |
| 100 | + ret = usbh_class_probe_one(c_data, udev); |
| 101 | + if (ret == -ENOTSUP) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + if (ret != 0) { |
| 105 | + return ret; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + LOG_WRN("Could not find any class for this device"); |
| 111 | + return -ENODEV; |
| 112 | +} |
| 113 | + |
16 | 114 | bool usbh_class_is_matching(struct usbh_class_filter *const filters, size_t n_filters,
|
17 | 115 | struct usb_device_descriptor *const desc)
|
18 | 116 | {
|
|
0 commit comments