|
| 1 | +/* |
| 2 | + * Copyright 2025 NXP |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/logging/log.h> |
| 9 | +#include <zephyr/usb/usbh.h> |
| 10 | +#include <zephyr/sys/byteorder.h> |
| 11 | +#include "usbh_device.h" |
| 12 | +#include "usbh_ch9.h" |
| 13 | +#include "usbh_hub.h" |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(usbh_hub, CONFIG_USBH_LOG_LEVEL); |
| 16 | + |
| 17 | +/** |
| 18 | + * @brief Common hub control request function |
| 19 | + */ |
| 20 | +static int usbh_hub_class_request_common(struct usbh_hub_instance *hub_instance, |
| 21 | + uint8_t request_type, uint8_t request, |
| 22 | + uint16_t wvalue, uint16_t windex, |
| 23 | + uint8_t *buffer, uint16_t buffer_length, |
| 24 | + usbh_hub_callback_t callback_fn, |
| 25 | + void *callback_param) |
| 26 | +{ |
| 27 | + struct net_buf *buf; |
| 28 | + int ret; |
| 29 | + |
| 30 | + if (!hub_instance || !hub_instance->hub_udev) { |
| 31 | + return -EINVAL; |
| 32 | + } |
| 33 | + |
| 34 | + k_mutex_lock(&hub_instance->ctrl_lock, K_FOREVER); |
| 35 | + |
| 36 | + /* Allocate buffer for transfer */ |
| 37 | + if (buffer_length > 0) { |
| 38 | + buf = usbh_xfer_buf_alloc(hub_instance->hub_udev, buffer_length); |
| 39 | + if (!buf) { |
| 40 | + LOG_ERR("Failed to allocate buffer for hub control request"); |
| 41 | + k_mutex_unlock(&hub_instance->ctrl_lock); |
| 42 | + return -ENOMEM; |
| 43 | + } |
| 44 | + |
| 45 | + /* For OUT transfers, copy data to buffer */ |
| 46 | + if (!(request_type & 0x80) && buffer) { |
| 47 | + memcpy(buf->data, buffer, buffer_length); |
| 48 | + net_buf_add(buf, buffer_length); |
| 49 | + } |
| 50 | + } else { |
| 51 | + buf = usbh_xfer_buf_alloc(hub_instance->hub_udev, 0); |
| 52 | + if (!buf) { |
| 53 | + LOG_ERR("Failed to allocate zero-length buffer"); |
| 54 | + k_mutex_unlock(&hub_instance->ctrl_lock); |
| 55 | + return -ENOMEM; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /* Execute synchronous control transfer - 参考CDC ECM的方式 */ |
| 60 | + ret = usbh_req_setup(hub_instance->hub_udev, |
| 61 | + request_type, |
| 62 | + request, |
| 63 | + wvalue, |
| 64 | + windex, |
| 65 | + buffer_length, |
| 66 | + buf); |
| 67 | + |
| 68 | + if (ret < 0) { |
| 69 | + LOG_ERR("Hub control request failed: %d", ret); |
| 70 | + goto cleanup; |
| 71 | + } |
| 72 | + |
| 73 | + /* For IN transfers, copy data back to user buffer */ |
| 74 | + if ((request_type & 0x80) && |
| 75 | + buffer_length > 0 && buffer && buf->len > 0) { |
| 76 | + memcpy(buffer, buf->data, MIN(buffer_length, buf->len)); |
| 77 | + } |
| 78 | + |
| 79 | + /* Call callback if provided */ |
| 80 | + if (callback_fn) { |
| 81 | + callback_fn(callback_param, buffer, |
| 82 | + (ret == 0) ? buf->len : 0, ret); |
| 83 | + } |
| 84 | + |
| 85 | + LOG_DBG("Hub control request completed: type=0x%02x, req=0x%02x, status=%d", |
| 86 | + request_type, request, ret); |
| 87 | + |
| 88 | +cleanup: |
| 89 | + usbh_xfer_buf_free(hub_instance->hub_udev, buf); |
| 90 | + k_mutex_unlock(&hub_instance->ctrl_lock); |
| 91 | + return ret; |
| 92 | +} |
| 93 | + |
| 94 | +int usbh_hub_init_instance(struct usbh_hub_instance *hub_instance, |
| 95 | + struct usb_device *udev) |
| 96 | +{ |
| 97 | + if (!hub_instance || !udev) { |
| 98 | + return -EINVAL; |
| 99 | + } |
| 100 | + |
| 101 | + memset(hub_instance, 0, sizeof(*hub_instance)); |
| 102 | + hub_instance->hub_udev = udev; |
| 103 | + |
| 104 | + k_sem_init(&hub_instance->ctrl_sem, 0, 1); |
| 105 | + k_mutex_init(&hub_instance->ctrl_lock); |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +int usbh_hub_get_descriptor(struct usbh_hub_instance *hub_instance, |
| 111 | + uint8_t *buffer, uint16_t buffer_length, |
| 112 | + usbh_hub_callback_t callback_fn, void *callback_param) |
| 113 | +{ |
| 114 | + return usbh_hub_class_request_common(hub_instance, |
| 115 | + (USB_REQTYPE_DIR_TO_HOST << 7) | |
| 116 | + (USB_REQTYPE_TYPE_CLASS << 5) | |
| 117 | + (USB_REQTYPE_RECIPIENT_DEVICE << 0), |
| 118 | + USB_HUB_REQ_GET_DESCRIPTOR, |
| 119 | + (USB_HUB_DESCRIPTOR_TYPE << 8), |
| 120 | + 0, |
| 121 | + buffer, buffer_length, callback_fn, callback_param); |
| 122 | +} |
| 123 | + |
| 124 | +int usbh_hub_set_port_feature(struct usbh_hub_instance *hub_instance, |
| 125 | + uint8_t port_number, uint8_t feature, |
| 126 | + usbh_hub_callback_t callback_fn, void *callback_param) |
| 127 | +{ |
| 128 | + return usbh_hub_class_request_common(hub_instance, |
| 129 | + (USB_REQTYPE_DIR_TO_DEVICE << 7) | |
| 130 | + (USB_REQTYPE_TYPE_CLASS << 5) | |
| 131 | + (USB_REQTYPE_RECIPIENT_OTHER << 0), |
| 132 | + USB_HUB_REQ_SET_FEATURE, |
| 133 | + feature, |
| 134 | + port_number, |
| 135 | + NULL, 0, callback_fn, callback_param); |
| 136 | +} |
| 137 | + |
| 138 | +int usbh_hub_clear_port_feature(struct usbh_hub_instance *hub_instance, |
| 139 | + uint8_t port_number, uint8_t feature, |
| 140 | + usbh_hub_callback_t callback_fn, void *callback_param) |
| 141 | +{ |
| 142 | + return usbh_hub_class_request_common(hub_instance, |
| 143 | + (USB_REQTYPE_DIR_TO_DEVICE << 7) | |
| 144 | + (USB_REQTYPE_TYPE_CLASS << 5) | |
| 145 | + (USB_REQTYPE_RECIPIENT_OTHER << 0), |
| 146 | + USB_HUB_REQ_CLEAR_FEATURE, |
| 147 | + feature, |
| 148 | + port_number, |
| 149 | + NULL, 0, callback_fn, callback_param); |
| 150 | +} |
| 151 | + |
| 152 | +int usbh_hub_get_port_status(struct usbh_hub_instance *hub_instance, |
| 153 | + uint8_t port_number, uint8_t *buffer, uint16_t buffer_length, |
| 154 | + usbh_hub_callback_t callback_fn, void *callback_param) |
| 155 | +{ |
| 156 | + return usbh_hub_class_request_common(hub_instance, |
| 157 | + (USB_REQTYPE_DIR_TO_HOST << 7) | |
| 158 | + (USB_REQTYPE_TYPE_CLASS << 5) | |
| 159 | + (USB_REQTYPE_RECIPIENT_OTHER << 0), |
| 160 | + USB_HUB_REQ_GET_STATUS, |
| 161 | + 0, |
| 162 | + port_number, |
| 163 | + buffer, buffer_length, callback_fn, callback_param); |
| 164 | +} |
| 165 | + |
| 166 | +int usbh_hub_get_hub_status(struct usbh_hub_instance *hub_instance, |
| 167 | + uint8_t *buffer, uint16_t buffer_length, |
| 168 | + usbh_hub_callback_t callback_fn, void *callback_param) |
| 169 | +{ |
| 170 | + return usbh_hub_class_request_common(hub_instance, |
| 171 | + (USB_REQTYPE_DIR_TO_HOST << 7) | |
| 172 | + (USB_REQTYPE_TYPE_CLASS << 5) | |
| 173 | + (USB_REQTYPE_RECIPIENT_DEVICE << 0), |
| 174 | + USB_HUB_REQ_GET_STATUS, |
| 175 | + 0, |
| 176 | + 0, |
| 177 | + buffer, buffer_length, callback_fn, callback_param); |
| 178 | +} |
| 179 | + |
| 180 | +void usbh_hub_cleanup_instance(struct usbh_hub_instance *hub_instance) |
| 181 | +{ |
| 182 | + if (hub_instance) { |
| 183 | + /* Clean up any pending operations */ |
| 184 | + memset(hub_instance, 0, sizeof(*hub_instance)); |
| 185 | + } |
| 186 | +} |
0 commit comments