| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 | /** @addtogroup drvusbohci
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief OHCI driver
|
|---|
| 33 | */
|
|---|
| 34 | #include <assert.h>
|
|---|
| 35 | #include <byteorder.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <fibril_synch.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <usb/usb.h>
|
|---|
| 41 | #include <usb/debug.h>
|
|---|
| 42 | #include <usb/dev/request.h>
|
|---|
| 43 | #include <usb/classes/hub.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <usb/classes/classes.h>
|
|---|
| 46 | #include <usb/dev/driver.h>
|
|---|
| 47 | #include "ohci_regs.h"
|
|---|
| 48 | #include "root_hub.h"
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * standart device descriptor for ohci root hub
|
|---|
| 52 | */
|
|---|
| 53 | static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
|
|---|
| 54 | .configuration_count = 1,
|
|---|
| 55 | .descriptor_type = USB_DESCTYPE_DEVICE,
|
|---|
| 56 | .device_class = USB_CLASS_HUB,
|
|---|
| 57 | .device_protocol = 0,
|
|---|
| 58 | .device_subclass = 0,
|
|---|
| 59 | .device_version = 0,
|
|---|
| 60 | .length = sizeof(usb_standard_device_descriptor_t),
|
|---|
| 61 | .max_packet_size = 64,
|
|---|
| 62 | .vendor_id = 0x16db, /* HelenOS does not have USB vendor ID assigned.*/
|
|---|
| 63 | .product_id = 0x0001,
|
|---|
| 64 | .str_serial_number = 0,
|
|---|
| 65 | .usb_spec_version = 0x110,
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * standart configuration descriptor with filled common values
|
|---|
| 70 | * for ohci root hubs
|
|---|
| 71 | */
|
|---|
| 72 | static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
|
|---|
| 73 | .attributes = 1 << 7,
|
|---|
| 74 | .configuration_number = 1,
|
|---|
| 75 | .descriptor_type = USB_DESCTYPE_CONFIGURATION,
|
|---|
| 76 | .interface_count = 1,
|
|---|
| 77 | .length = sizeof(usb_standard_configuration_descriptor_t),
|
|---|
| 78 | .max_power = 0, /* root hubs don't need no power */
|
|---|
| 79 | .str_configuration = 0,
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * standart ohci root hub interface descriptor
|
|---|
| 84 | */
|
|---|
| 85 | static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
|
|---|
| 86 | .alternate_setting = 0,
|
|---|
| 87 | .descriptor_type = USB_DESCTYPE_INTERFACE,
|
|---|
| 88 | .endpoint_count = 1,
|
|---|
| 89 | .interface_class = USB_CLASS_HUB,
|
|---|
| 90 | .interface_number = 1,
|
|---|
| 91 | .interface_protocol = 0,
|
|---|
| 92 | .interface_subclass = 0,
|
|---|
| 93 | .length = sizeof(usb_standard_interface_descriptor_t),
|
|---|
| 94 | .str_interface = 0,
|
|---|
| 95 | };
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * standart ohci root hub endpoint descriptor
|
|---|
| 99 | */
|
|---|
| 100 | static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
|
|---|
| 101 | .attributes = USB_TRANSFER_INTERRUPT,
|
|---|
| 102 | .descriptor_type = USB_DESCTYPE_ENDPOINT,
|
|---|
| 103 | .endpoint_address = 1 | (1 << 7),
|
|---|
| 104 | .length = sizeof(usb_standard_endpoint_descriptor_t),
|
|---|
| 105 | .max_packet_size = 2,
|
|---|
| 106 | .poll_interval = 255,
|
|---|
| 107 | };
|
|---|
| 108 |
|
|---|
| 109 | static void create_serialized_hub_descriptor(rh_t *instance);
|
|---|
| 110 | static void rh_init_descriptors(rh_t *instance);
|
|---|
| 111 | static uint16_t create_interrupt_mask(const rh_t *instance);
|
|---|
| 112 | static void get_status(const rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 113 | static void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 114 | static void set_feature(const rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 115 | static void clear_feature(const rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 116 | static int set_feature_port(
|
|---|
| 117 | const rh_t *instance, uint16_t feature, uint16_t port);
|
|---|
| 118 | static int clear_feature_port(
|
|---|
| 119 | const rh_t *instance, uint16_t feature, uint16_t port);
|
|---|
| 120 | static void control_request(rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 121 | static inline void interrupt_request(
|
|---|
| 122 | usb_transfer_batch_t *request, uint16_t mask, size_t size)
|
|---|
| 123 | {
|
|---|
| 124 | assert(request);
|
|---|
| 125 | usb_log_debug("Sending interrupt vector(%zu) %hhx:%hhx.\n",
|
|---|
| 126 | size, ((uint8_t*)&mask)[0], ((uint8_t*)&mask)[1]);
|
|---|
| 127 | usb_transfer_batch_finish_error(request, &mask, size, EOK);
|
|---|
| 128 | usb_transfer_batch_destroy(request);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | #define TRANSFER_END_DATA(request, data, bytes) \
|
|---|
| 132 | do { \
|
|---|
| 133 | usb_transfer_batch_finish_error(request, data, bytes, EOK); \
|
|---|
| 134 | usb_transfer_batch_destroy(request); \
|
|---|
| 135 | return; \
|
|---|
| 136 | } while (0)
|
|---|
| 137 |
|
|---|
| 138 | #define TRANSFER_END(request, error) \
|
|---|
| 139 | do { \
|
|---|
| 140 | usb_transfer_batch_finish_error(request, NULL, 0, error); \
|
|---|
| 141 | usb_transfer_batch_destroy(request); \
|
|---|
| 142 | return; \
|
|---|
| 143 | } while (0)
|
|---|
| 144 |
|
|---|
| 145 | /** Root Hub driver structure initialization.
|
|---|
| 146 | *
|
|---|
| 147 | * Reads info registers and prepares descriptors. Sets power mode.
|
|---|
| 148 | */
|
|---|
| 149 | void rh_init(rh_t *instance, ohci_regs_t *regs)
|
|---|
| 150 | {
|
|---|
| 151 | assert(instance);
|
|---|
| 152 | assert(regs);
|
|---|
| 153 |
|
|---|
| 154 | instance->registers = regs;
|
|---|
| 155 | instance->port_count = OHCI_RD(regs->rh_desc_a) & RHDA_NDS_MASK;
|
|---|
| 156 | usb_log_debug2("rh_desc_a: %x.\n", OHCI_RD(regs->rh_desc_a));
|
|---|
| 157 | if (instance->port_count > 15) {
|
|---|
| 158 | usb_log_warning("OHCI specification does not allow more than 15"
|
|---|
| 159 | " ports. Max 15 ports will be used");
|
|---|
| 160 | instance->port_count = 15;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /* Don't forget the hub status bit and round up */
|
|---|
| 164 | instance->interrupt_mask_size = 1 + (instance->port_count / 8);
|
|---|
| 165 | instance->unfinished_interrupt_transfer = NULL;
|
|---|
| 166 |
|
|---|
| 167 | #if defined OHCI_POWER_SWITCH_no
|
|---|
| 168 | usb_log_debug("OHCI rh: Set power mode to no power switching.\n");
|
|---|
| 169 | /* Set port power mode to no power-switching. (always on) */
|
|---|
| 170 | OHCI_SET(regs->rh_desc_a, RHDA_NPS_FLAG);
|
|---|
| 171 |
|
|---|
| 172 | /* Set to no over-current reporting */
|
|---|
| 173 | OHCI_SET(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
|---|
| 174 |
|
|---|
| 175 | #elif defined OHCI_POWER_SWITCH_ganged
|
|---|
| 176 | usb_log_debug("OHCI rh: Set power mode to ganged power switching.\n");
|
|---|
| 177 | /* Set port power mode to ganged power-switching. */
|
|---|
| 178 | OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
|
|---|
| 179 | OHCI_CLR(regs->rh_desc_a, RHDA_PSM_FLAG);
|
|---|
| 180 |
|
|---|
| 181 | /* Turn off power (hub driver will turn this back on)*/
|
|---|
| 182 | OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
|
|---|
| 183 |
|
|---|
| 184 | /* Set to global over-current */
|
|---|
| 185 | OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
|---|
| 186 | OHCI_CLR(regs->rh_desc_a, RHDA_OCPM_FLAG);
|
|---|
| 187 | #else
|
|---|
| 188 | usb_log_debug("OHCI rh: Set power mode to per-port power switching.\n");
|
|---|
| 189 | /* Set port power mode to per port power-switching. */
|
|---|
| 190 | OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
|
|---|
| 191 | OHCI_SET(regs->rh_desc_a, RHDA_PSM_FLAG);
|
|---|
| 192 |
|
|---|
| 193 | /* Control all ports by global switch and turn them off */
|
|---|
| 194 | OHCI_CLR(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
|
|---|
| 195 | OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
|
|---|
| 196 |
|
|---|
| 197 | /* Return control to per port state */
|
|---|
| 198 | OHCI_SET(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
|
|---|
| 199 |
|
|---|
| 200 | /* Set per port over-current */
|
|---|
| 201 | OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
|---|
| 202 | OHCI_SET(regs->rh_desc_a, RHDA_OCPM_FLAG);
|
|---|
| 203 | #endif
|
|---|
| 204 |
|
|---|
| 205 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 206 | rh_init_descriptors(instance);
|
|---|
| 207 |
|
|---|
| 208 | usb_log_info("Root hub (%zu ports) initialized.\n",
|
|---|
| 209 | instance->port_count);
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * Process root hub request.
|
|---|
| 214 | *
|
|---|
| 215 | * @param instance Root hub instance
|
|---|
| 216 | * @param request Structure containing both request and response information
|
|---|
| 217 | * @return Error code
|
|---|
| 218 | */
|
|---|
| 219 | void rh_request(rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 220 | {
|
|---|
| 221 | assert(instance);
|
|---|
| 222 | assert(request);
|
|---|
| 223 |
|
|---|
| 224 | switch (request->ep->transfer_type)
|
|---|
| 225 | {
|
|---|
| 226 | case USB_TRANSFER_CONTROL:
|
|---|
| 227 | usb_log_debug("Root hub got CONTROL packet\n");
|
|---|
| 228 | control_request(instance, request);
|
|---|
| 229 | break;
|
|---|
| 230 |
|
|---|
| 231 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 232 | usb_log_debug("Root hub got INTERRUPT packet\n");
|
|---|
| 233 | fibril_mutex_lock(&instance->guard);
|
|---|
| 234 | assert(instance->unfinished_interrupt_transfer == NULL);
|
|---|
| 235 | const uint16_t mask = create_interrupt_mask(instance);
|
|---|
| 236 | if (mask == 0) {
|
|---|
| 237 | usb_log_debug("No changes(%hx)...\n", mask);
|
|---|
| 238 | instance->unfinished_interrupt_transfer = request;
|
|---|
| 239 | } else {
|
|---|
| 240 | usb_log_debug("Processing changes...\n");
|
|---|
| 241 | interrupt_request(
|
|---|
| 242 | request, mask, instance->interrupt_mask_size);
|
|---|
| 243 | }
|
|---|
| 244 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 245 | break;
|
|---|
| 246 |
|
|---|
| 247 | default:
|
|---|
| 248 | usb_log_error("Root hub got unsupported request.\n");
|
|---|
| 249 | TRANSFER_END(request, ENOTSUP);
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /**
|
|---|
| 254 | * Process interrupt on a hub device.
|
|---|
| 255 | *
|
|---|
| 256 | * If there is no pending interrupt transfer, nothing happens.
|
|---|
| 257 | * @param instance
|
|---|
| 258 | */
|
|---|
| 259 | void rh_interrupt(rh_t *instance)
|
|---|
| 260 | {
|
|---|
| 261 | assert(instance);
|
|---|
| 262 |
|
|---|
| 263 | fibril_mutex_lock(&instance->guard);
|
|---|
| 264 | if (instance->unfinished_interrupt_transfer) {
|
|---|
| 265 | usb_log_debug("Finalizing interrupt transfer\n");
|
|---|
| 266 | const uint16_t mask = create_interrupt_mask(instance);
|
|---|
| 267 | interrupt_request(instance->unfinished_interrupt_transfer,
|
|---|
| 268 | mask, instance->interrupt_mask_size);
|
|---|
| 269 | instance->unfinished_interrupt_transfer = NULL;
|
|---|
| 270 | }
|
|---|
| 271 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /**
|
|---|
| 275 | * Create hub descriptor.
|
|---|
| 276 | *
|
|---|
| 277 | * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
|
|---|
| 278 | *
|
|---|
| 279 | * @param instance Root hub instance
|
|---|
| 280 | * @return Error code
|
|---|
| 281 | */
|
|---|
| 282 | void create_serialized_hub_descriptor(rh_t *instance)
|
|---|
| 283 | {
|
|---|
| 284 | assert(instance);
|
|---|
| 285 |
|
|---|
| 286 | /* 7 bytes + 2 port bit fields (port count + global bit) */
|
|---|
| 287 | size_t size = 7 + (instance->interrupt_mask_size * 2);
|
|---|
| 288 | assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
|
|---|
| 289 | instance->hub_descriptor_size = size;
|
|---|
| 290 |
|
|---|
| 291 | const uint32_t hub_desc = OHCI_RD(instance->registers->rh_desc_a);
|
|---|
| 292 | const uint32_t port_desc = OHCI_RD(instance->registers->rh_desc_b);
|
|---|
| 293 |
|
|---|
| 294 | /* bDescLength */
|
|---|
| 295 | instance->descriptors.hub[0] = size;
|
|---|
| 296 | /* bDescriptorType */
|
|---|
| 297 | instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
|
|---|
| 298 | /* bNmbrPorts */
|
|---|
| 299 | instance->descriptors.hub[2] = instance->port_count;
|
|---|
| 300 | /* wHubCharacteristics */
|
|---|
| 301 | instance->descriptors.hub[3] = 0 |
|
|---|
| 302 | /* The lowest 2 bits indicate power switching mode */
|
|---|
| 303 | (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
|
|---|
| 304 | (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
|
|---|
| 305 | /* Bit 3 indicates device type (compound device) */
|
|---|
| 306 | (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
|
|---|
| 307 | /* Bits 4,5 indicate over-current protection mode */
|
|---|
| 308 | (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
|
|---|
| 309 | (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
|
|---|
| 310 |
|
|---|
| 311 | /* Reserved */
|
|---|
| 312 | instance->descriptors.hub[4] = 0;
|
|---|
| 313 | /* bPwrOn2PwrGood */
|
|---|
| 314 | instance->descriptors.hub[5] = hub_desc >> RHDA_POTPGT_SHIFT;
|
|---|
| 315 | /* bHubContrCurrent, root hubs don't need no power. */
|
|---|
| 316 | instance->descriptors.hub[6] = 0;
|
|---|
| 317 |
|
|---|
| 318 | /* Device Removable and some legacy 1.0 stuff*/
|
|---|
| 319 | instance->descriptors.hub[7] = (port_desc >> RHDB_DR_SHIFT) & 0xff;
|
|---|
| 320 | instance->descriptors.hub[8] = 0xff;
|
|---|
| 321 | if (instance->interrupt_mask_size == 2) {
|
|---|
| 322 | instance->descriptors.hub[8] =
|
|---|
| 323 | (port_desc >> RHDB_DR_SHIFT) >> 8;
|
|---|
| 324 | instance->descriptors.hub[9] = 0xff;
|
|---|
| 325 | instance->descriptors.hub[10] = 0xff;
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | /** Initialize hub descriptors.
|
|---|
| 330 | *
|
|---|
| 331 | * A full configuration descriptor is assembled. The configuration and endpoint
|
|---|
| 332 | * descriptors have local modifications.
|
|---|
| 333 | * @param instance Root hub instance
|
|---|
| 334 | * @return Error code
|
|---|
| 335 | */
|
|---|
| 336 | void rh_init_descriptors(rh_t *instance)
|
|---|
| 337 | {
|
|---|
| 338 | assert(instance);
|
|---|
| 339 |
|
|---|
| 340 | instance->descriptors.configuration = ohci_rh_conf_descriptor;
|
|---|
| 341 | instance->descriptors.interface = ohci_rh_iface_descriptor;
|
|---|
| 342 | instance->descriptors.endpoint = ohci_rh_ep_descriptor;
|
|---|
| 343 | create_serialized_hub_descriptor(instance);
|
|---|
| 344 |
|
|---|
| 345 | instance->descriptors.endpoint.max_packet_size =
|
|---|
| 346 | instance->interrupt_mask_size;
|
|---|
| 347 |
|
|---|
| 348 | instance->descriptors.configuration.total_length = uint16_host2usb(
|
|---|
| 349 | sizeof(usb_standard_configuration_descriptor_t) +
|
|---|
| 350 | sizeof(usb_standard_endpoint_descriptor_t) +
|
|---|
| 351 | sizeof(usb_standard_interface_descriptor_t) +
|
|---|
| 352 | instance->hub_descriptor_size);
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | /**
|
|---|
| 356 | * Create bitmap of changes to answer status interrupt.
|
|---|
| 357 | *
|
|---|
| 358 | * Result contains bitmap where bit 0 indicates change on hub and
|
|---|
| 359 | * bit i indicates change on i`th port (i>0). For more info see
|
|---|
| 360 | * Hub and Port status bitmap specification in USB specification
|
|---|
| 361 | * (chapter 11.13.4).
|
|---|
| 362 | * @param instance root hub instance
|
|---|
| 363 | * @return Mask of changes.
|
|---|
| 364 | */
|
|---|
| 365 | uint16_t create_interrupt_mask(const rh_t *instance)
|
|---|
| 366 | {
|
|---|
| 367 | assert(instance);
|
|---|
| 368 | uint16_t mask = 0;
|
|---|
| 369 |
|
|---|
| 370 | /* Only local power source change and over-current change can happen */
|
|---|
| 371 | if (OHCI_RD(instance->registers->rh_status)
|
|---|
| 372 | & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
|
|---|
| 373 | mask |= 1;
|
|---|
| 374 | }
|
|---|
| 375 | for (size_t port = 1; port <= instance->port_count; ++port) {
|
|---|
| 376 | /* Write-clean bits are those that indicate change */
|
|---|
| 377 | if (OHCI_RD(instance->registers->rh_port_status[port - 1])
|
|---|
| 378 | & RHPS_CHANGE_WC_MASK) {
|
|---|
| 379 | mask |= (1 << port);
|
|---|
| 380 | }
|
|---|
| 381 | }
|
|---|
| 382 | usb_log_debug2("OHCI root hub interrupt mask: %hx.\n", mask);
|
|---|
| 383 | return uint16_host2usb(mask);
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | /**
|
|---|
| 387 | * Create answer to status request.
|
|---|
| 388 | *
|
|---|
| 389 | * This might be either hub status or port status request. If neither,
|
|---|
| 390 | * ENOTSUP is returned.
|
|---|
| 391 | * @param instance root hub instance
|
|---|
| 392 | * @param request structure containing both request and response information
|
|---|
| 393 | * @return error code
|
|---|
| 394 | */
|
|---|
| 395 | void get_status(const rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 396 | {
|
|---|
| 397 | assert(instance);
|
|---|
| 398 | assert(request);
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 | usb_device_request_setup_packet_t *request_packet =
|
|---|
| 402 | (usb_device_request_setup_packet_t*)request->setup_buffer;
|
|---|
| 403 |
|
|---|
| 404 | const uint16_t index = uint16_usb2host(request_packet->index);
|
|---|
| 405 |
|
|---|
| 406 | switch (request_packet->request_type)
|
|---|
| 407 | {
|
|---|
| 408 | case USB_HUB_REQ_TYPE_GET_HUB_STATUS:
|
|---|
| 409 | /* Hub status: just filter relevant info from rh_status reg */
|
|---|
| 410 | if (request->buffer_size < 4) {
|
|---|
| 411 | usb_log_error("Buffer(%zu) too small for hub get "
|
|---|
| 412 | "status request.\n", request->buffer_size);
|
|---|
| 413 | TRANSFER_END(request, EOVERFLOW);
|
|---|
| 414 | } else {
|
|---|
| 415 | const uint32_t data =
|
|---|
| 416 | OHCI_RD(instance->registers->rh_status) &
|
|---|
| 417 | (RHS_LPS_FLAG | RHS_LPSC_FLAG
|
|---|
| 418 | | RHS_OCI_FLAG | RHS_OCIC_FLAG);
|
|---|
| 419 | TRANSFER_END_DATA(request, &data, sizeof(data));
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | /* Copy appropriate rh_port_status register, OHCI designers were
|
|---|
| 423 | * kind enough to make those bit values match USB specification */
|
|---|
| 424 | case USB_HUB_REQ_TYPE_GET_PORT_STATUS:
|
|---|
| 425 | if (request->buffer_size < 4) {
|
|---|
| 426 | usb_log_error("Buffer(%zu) too small for hub get "
|
|---|
| 427 | "status request.\n", request->buffer_size);
|
|---|
| 428 | TRANSFER_END(request, EOVERFLOW);
|
|---|
| 429 | } else {
|
|---|
| 430 | const unsigned port = index;
|
|---|
| 431 | if (port < 1 || port > instance->port_count)
|
|---|
| 432 | TRANSFER_END(request, EINVAL);
|
|---|
| 433 | /* Register format matches the format of port status
|
|---|
| 434 | * field */
|
|---|
| 435 | const uint32_t data = uint32_host2usb(OHCI_RD(
|
|---|
| 436 | instance->registers->rh_port_status[port - 1]));
|
|---|
| 437 | TRANSFER_END_DATA(request, &data, sizeof(data));
|
|---|
| 438 | }
|
|---|
| 439 | case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE):
|
|---|
| 440 | if (request->buffer_size < 2) {
|
|---|
| 441 | usb_log_error("Buffer(%zu) too small for hub generic "
|
|---|
| 442 | "get status request.\n", request->buffer_size);
|
|---|
| 443 | TRANSFER_END(request, EOVERFLOW);
|
|---|
| 444 | } else {
|
|---|
| 445 | const uint16_t data =
|
|---|
| 446 | uint16_host2usb(USB_DEVICE_STATUS_SELF_POWERED);
|
|---|
| 447 | TRANSFER_END_DATA(request, &data, sizeof(data));
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE):
|
|---|
| 451 | /* Hubs are allowed to have only one interface */
|
|---|
| 452 | if (index != 0)
|
|---|
| 453 | TRANSFER_END(request, EINVAL);
|
|---|
| 454 | /* Fall through, as the answer will be the same: 0x0000 */
|
|---|
| 455 | case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT):
|
|---|
| 456 | /* Endpoint 0 (default control) and 1 (interrupt) */
|
|---|
| 457 | if (index >= 2)
|
|---|
| 458 | TRANSFER_END(request, EINVAL);
|
|---|
| 459 |
|
|---|
| 460 | if (request->buffer_size < 2) {
|
|---|
| 461 | usb_log_error("Buffer(%zu) too small for hub generic "
|
|---|
| 462 | "get status request.\n", request->buffer_size);
|
|---|
| 463 | TRANSFER_END(request, EOVERFLOW);
|
|---|
| 464 | } else {
|
|---|
| 465 | /* Endpoints are OK. (We don't halt) */
|
|---|
| 466 | const uint16_t data = 0;
|
|---|
| 467 | TRANSFER_END_DATA(request, &data, sizeof(data));
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | default:
|
|---|
| 471 | usb_log_error("Unsupported GET_STATUS request.\n");
|
|---|
| 472 | TRANSFER_END(request, ENOTSUP);
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | /**
|
|---|
| 478 | * Create answer to a descriptor request.
|
|---|
| 479 | *
|
|---|
| 480 | * This might be a request for standard (configuration, device, endpoint or
|
|---|
| 481 | * interface) or device specific (hub) descriptor.
|
|---|
| 482 | * @param instance Root hub instance
|
|---|
| 483 | * @param request Structure containing both request and response information
|
|---|
| 484 | * @return Error code
|
|---|
| 485 | */
|
|---|
| 486 | void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 487 | {
|
|---|
| 488 | assert(instance);
|
|---|
| 489 | assert(request);
|
|---|
| 490 |
|
|---|
| 491 | usb_device_request_setup_packet_t *setup_request =
|
|---|
| 492 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
|---|
| 493 | /* "The wValue field specifies the descriptor type in the high byte
|
|---|
| 494 | * and the descriptor index in the low byte (refer to Table 9-5)." */
|
|---|
| 495 | const int desc_type = uint16_usb2host(setup_request->value) >> 8;
|
|---|
| 496 | switch (desc_type)
|
|---|
| 497 | {
|
|---|
| 498 | case USB_DESCTYPE_HUB:
|
|---|
| 499 | usb_log_debug2("USB_DESCTYPE_HUB\n");
|
|---|
| 500 | /* Hub descriptor was generated locally.
|
|---|
| 501 | * Class specific request. */
|
|---|
| 502 | TRANSFER_END_DATA(request, instance->descriptors.hub,
|
|---|
| 503 | instance->hub_descriptor_size);
|
|---|
| 504 |
|
|---|
| 505 | case USB_DESCTYPE_DEVICE:
|
|---|
| 506 | usb_log_debug2("USB_DESCTYPE_DEVICE\n");
|
|---|
| 507 | /* Device descriptor is shared
|
|---|
| 508 | * (No one should ask for it, as the device is already setup)
|
|---|
| 509 | * Standard USB device request. */
|
|---|
| 510 | TRANSFER_END_DATA(request, &ohci_rh_device_descriptor,
|
|---|
| 511 | sizeof(ohci_rh_device_descriptor));
|
|---|
| 512 |
|
|---|
| 513 | case USB_DESCTYPE_CONFIGURATION:
|
|---|
| 514 | usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
|
|---|
| 515 | /* Start with configuration and add others depending on
|
|---|
| 516 | * request size. Standard USB request. */
|
|---|
| 517 | TRANSFER_END_DATA(request, &instance->descriptors,
|
|---|
| 518 | instance->descriptors.configuration.total_length);
|
|---|
| 519 |
|
|---|
| 520 | case USB_DESCTYPE_INTERFACE:
|
|---|
| 521 | usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
|
|---|
| 522 | /* Use local interface descriptor. There is one and it
|
|---|
| 523 | * might be modified. Hub driver should not ask or this
|
|---|
| 524 | * descriptor as it is not part of standard requests set. */
|
|---|
| 525 | TRANSFER_END_DATA(request, &instance->descriptors.interface,
|
|---|
| 526 | sizeof(instance->descriptors.interface));
|
|---|
| 527 |
|
|---|
| 528 | case USB_DESCTYPE_ENDPOINT:
|
|---|
| 529 | /* Use local endpoint descriptor. There is one
|
|---|
| 530 | * it might have max_packet_size field modified. Hub driver
|
|---|
| 531 | * should not ask for this descriptor as it is not part
|
|---|
| 532 | * of standard requests set. */
|
|---|
| 533 | usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
|
|---|
| 534 | TRANSFER_END_DATA(request, &instance->descriptors.endpoint,
|
|---|
| 535 | sizeof(instance->descriptors.endpoint));
|
|---|
| 536 |
|
|---|
| 537 | default:
|
|---|
| 538 | usb_log_debug2("USB_DESCTYPE_EINVAL %d \n"
|
|---|
| 539 | "\ttype %d\n\trequest %d\n\tvalue "
|
|---|
| 540 | "%d\n\tindex %d\n\tlen %d\n ",
|
|---|
| 541 | setup_request->value,
|
|---|
| 542 | setup_request->request_type, setup_request->request,
|
|---|
| 543 | desc_type, setup_request->index,
|
|---|
| 544 | setup_request->length);
|
|---|
| 545 | TRANSFER_END(request, EINVAL);
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | TRANSFER_END(request, ENOTSUP);
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | /**
|
|---|
| 552 | * process feature-enabling request on hub
|
|---|
| 553 | *
|
|---|
| 554 | * @param instance root hub instance
|
|---|
| 555 | * @param feature feature selector
|
|---|
| 556 | * @param port port number, counted from 1
|
|---|
| 557 | * @param enable enable or disable the specified feature
|
|---|
| 558 | * @return error code
|
|---|
| 559 | */
|
|---|
| 560 | int set_feature_port(const rh_t *instance, uint16_t feature, uint16_t port)
|
|---|
| 561 | {
|
|---|
| 562 | assert(instance);
|
|---|
| 563 |
|
|---|
| 564 | if (port < 1 || port > instance->port_count)
|
|---|
| 565 | return EINVAL;
|
|---|
| 566 |
|
|---|
| 567 | switch (feature) {
|
|---|
| 568 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
|---|
| 569 | {
|
|---|
| 570 | const uint32_t rhda =
|
|---|
| 571 | OHCI_RD(instance->registers->rh_desc_a);
|
|---|
| 572 | /* No power switching */
|
|---|
| 573 | if (rhda & RHDA_NPS_FLAG)
|
|---|
| 574 | return EOK;
|
|---|
| 575 | /* Ganged power switching, one port powers all */
|
|---|
| 576 | if (!(rhda & RHDA_PSM_FLAG)) {
|
|---|
| 577 | OHCI_WR(instance->registers->rh_status,
|
|---|
| 578 | RHS_SET_GLOBAL_POWER);
|
|---|
| 579 | return EOK;
|
|---|
| 580 | }
|
|---|
| 581 | }
|
|---|
| 582 | /* Fall through */
|
|---|
| 583 | case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
|
|---|
| 584 | case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
|---|
| 585 | case USB_HUB_FEATURE_PORT_RESET: /*4*/
|
|---|
| 586 | usb_log_debug2("Setting port POWER, ENABLE, SUSPEND or RESET "
|
|---|
| 587 | "on port %"PRIu16".\n", port);
|
|---|
| 588 | OHCI_WR(instance->registers->rh_port_status[port - 1],
|
|---|
| 589 | 1 << feature);
|
|---|
| 590 | return EOK;
|
|---|
| 591 | default:
|
|---|
| 592 | return ENOTSUP;
|
|---|
| 593 | }
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | /**
|
|---|
| 597 | * Process feature clear request.
|
|---|
| 598 | *
|
|---|
| 599 | * @param instance root hub instance
|
|---|
| 600 | * @param feature feature selector
|
|---|
| 601 | * @param port port number, counted from 1
|
|---|
| 602 | * @param enable enable or disable the specified feature
|
|---|
| 603 | * @return error code
|
|---|
| 604 | */
|
|---|
| 605 | int clear_feature_port(const rh_t *instance, uint16_t feature, uint16_t port)
|
|---|
| 606 | {
|
|---|
| 607 | assert(instance);
|
|---|
| 608 |
|
|---|
| 609 | if (port < 1 || port > instance->port_count)
|
|---|
| 610 | return EINVAL;
|
|---|
| 611 |
|
|---|
| 612 | /* Enabled features to clear: see page 269 of USB specs */
|
|---|
| 613 | switch (feature)
|
|---|
| 614 | {
|
|---|
| 615 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
|---|
| 616 | {
|
|---|
| 617 | const uint32_t rhda =
|
|---|
| 618 | OHCI_RD(instance->registers->rh_desc_a);
|
|---|
| 619 | /* No power switching */
|
|---|
| 620 | if (rhda & RHDA_NPS_FLAG)
|
|---|
| 621 | return ENOTSUP;
|
|---|
| 622 | /* Ganged power switching, one port powers all */
|
|---|
| 623 | if (!(rhda & RHDA_PSM_FLAG)) {
|
|---|
| 624 | OHCI_WR(instance->registers->rh_status,
|
|---|
| 625 | RHS_CLEAR_GLOBAL_POWER);
|
|---|
| 626 | return EOK;
|
|---|
| 627 | }
|
|---|
| 628 | OHCI_WR(instance->registers->rh_port_status[port - 1],
|
|---|
| 629 | RHPS_CLEAR_PORT_POWER);
|
|---|
| 630 | return EOK;
|
|---|
| 631 | }
|
|---|
| 632 |
|
|---|
| 633 | case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
|
|---|
| 634 | OHCI_WR(instance->registers->rh_port_status[port - 1],
|
|---|
| 635 | RHPS_CLEAR_PORT_ENABLE);
|
|---|
| 636 | return EOK;
|
|---|
| 637 |
|
|---|
| 638 | case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
|---|
| 639 | OHCI_WR(instance->registers->rh_port_status[port - 1],
|
|---|
| 640 | RHPS_CLEAR_PORT_SUSPEND);
|
|---|
| 641 | return EOK;
|
|---|
| 642 |
|
|---|
| 643 | case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
|
|---|
| 644 | case USB_HUB_FEATURE_C_PORT_ENABLE: /*17*/
|
|---|
| 645 | case USB_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
|
|---|
| 646 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
|
|---|
| 647 | case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
|
|---|
| 648 | usb_log_debug2("Clearing port C_CONNECTION, C_ENABLE, "
|
|---|
| 649 | "C_SUSPEND, C_OC or C_RESET on port %"PRIu16".\n", port);
|
|---|
| 650 | /* Bit offsets correspond to the feature number */
|
|---|
| 651 | OHCI_WR(instance->registers->rh_port_status[port - 1],
|
|---|
| 652 | 1 << feature);
|
|---|
| 653 | return EOK;
|
|---|
| 654 |
|
|---|
| 655 | default:
|
|---|
| 656 | return ENOTSUP;
|
|---|
| 657 | }
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | /**
|
|---|
| 661 | * process one of requests that do not request nor carry additional data
|
|---|
| 662 | *
|
|---|
| 663 | * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
|
|---|
| 664 | * USB_DEVREQ_SET_ADDRESS.
|
|---|
| 665 | * @param instance root hub instance
|
|---|
| 666 | * @param request structure containing both request and response information
|
|---|
| 667 | * @return error code
|
|---|
| 668 | */
|
|---|
| 669 | void set_feature(const rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 670 | {
|
|---|
| 671 | assert(instance);
|
|---|
| 672 | assert(request);
|
|---|
| 673 |
|
|---|
| 674 | usb_device_request_setup_packet_t *setup_request =
|
|---|
| 675 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
|---|
| 676 | switch (setup_request->request_type)
|
|---|
| 677 | {
|
|---|
| 678 | case USB_HUB_REQ_TYPE_SET_PORT_FEATURE:
|
|---|
| 679 | usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
|
|---|
| 680 | const int ret = set_feature_port(instance,
|
|---|
| 681 | setup_request->value, setup_request->index);
|
|---|
| 682 | TRANSFER_END(request, ret);
|
|---|
| 683 |
|
|---|
| 684 | case USB_HUB_REQ_TYPE_SET_HUB_FEATURE:
|
|---|
| 685 | /* Chapter 11.16.2 specifies that hub can be recipient
|
|---|
| 686 | * only for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
|
|---|
| 687 | * features. It makes no sense to SET either. */
|
|---|
| 688 | usb_log_error("Invalid HUB set feature request.\n");
|
|---|
| 689 | TRANSFER_END(request, ENOTSUP);
|
|---|
| 690 | //TODO: Consider standard USB requests: REMOTE WAKEUP, ENDPOINT STALL
|
|---|
| 691 | default:
|
|---|
| 692 | usb_log_error("Invalid set feature request type: %d\n",
|
|---|
| 693 | setup_request->request_type);
|
|---|
| 694 | TRANSFER_END(request, ENOTSUP);
|
|---|
| 695 | }
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| 698 | /**
|
|---|
| 699 | * process one of requests that do not request nor carry additional data
|
|---|
| 700 | *
|
|---|
| 701 | * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
|
|---|
| 702 | * USB_DEVREQ_SET_ADDRESS.
|
|---|
| 703 | * @param instance root hub instance
|
|---|
| 704 | * @param request structure containing both request and response information
|
|---|
| 705 | * @return error code
|
|---|
| 706 | */
|
|---|
| 707 | void clear_feature(const rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 708 | {
|
|---|
| 709 | assert(instance);
|
|---|
| 710 | assert(request);
|
|---|
| 711 |
|
|---|
| 712 | usb_device_request_setup_packet_t *setup_request =
|
|---|
| 713 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
|---|
| 714 |
|
|---|
| 715 | switch (setup_request->request_type)
|
|---|
| 716 | {
|
|---|
| 717 | case USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE:
|
|---|
| 718 | usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE\n");
|
|---|
| 719 | const int ret = clear_feature_port(instance,
|
|---|
| 720 | setup_request->value, setup_request->index);
|
|---|
| 721 | TRANSFER_END(request, ret);
|
|---|
| 722 |
|
|---|
| 723 | case USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE:
|
|---|
| 724 | usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE\n");
|
|---|
| 725 | /*
|
|---|
| 726 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
|---|
| 727 | * C_HUB_OVER_CURRENT are supported.
|
|---|
| 728 | * C_HUB_OVER_CURRENT is represented by OHCI RHS_OCIC_FLAG.
|
|---|
| 729 | * C_HUB_LOCAL_POWER is not supported
|
|---|
| 730 | * as root hubs do not support local power status feature.
|
|---|
| 731 | * (OHCI pg. 127) */
|
|---|
| 732 | if (uint16_usb2host(setup_request->value)
|
|---|
| 733 | == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
|
|---|
| 734 | OHCI_WR(instance->registers->rh_status, RHS_OCIC_FLAG);
|
|---|
| 735 | TRANSFER_END(request, EOK);
|
|---|
| 736 | }
|
|---|
| 737 | //TODO: Consider standard USB requests: REMOTE WAKEUP, ENDPOINT STALL
|
|---|
| 738 | default:
|
|---|
| 739 | usb_log_error("Invalid clear feature request type: %d\n",
|
|---|
| 740 | setup_request->request_type);
|
|---|
| 741 | TRANSFER_END(request, ENOTSUP);
|
|---|
| 742 | }
|
|---|
| 743 | }
|
|---|
| 744 |
|
|---|
| 745 | /**
|
|---|
| 746 | * Process hub control request.
|
|---|
| 747 | *
|
|---|
| 748 | * If needed, writes answer into the request structure.
|
|---|
| 749 | * Request can be one of
|
|---|
| 750 | * USB_DEVREQ_GET_STATUS,
|
|---|
| 751 | * USB_DEVREQ_GET_DESCRIPTOR,
|
|---|
| 752 | * USB_DEVREQ_GET_CONFIGURATION,
|
|---|
| 753 | * USB_DEVREQ_CLEAR_FEATURE,
|
|---|
| 754 | * USB_DEVREQ_SET_FEATURE,
|
|---|
| 755 | * USB_DEVREQ_SET_ADDRESS,
|
|---|
| 756 | * USB_DEVREQ_SET_DESCRIPTOR or
|
|---|
| 757 | * USB_DEVREQ_SET_CONFIGURATION.
|
|---|
| 758 | *
|
|---|
| 759 | * @param instance root hub instance
|
|---|
| 760 | * @param request structure containing both request and response information
|
|---|
| 761 | * @return error code
|
|---|
| 762 | */
|
|---|
| 763 | void control_request(rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 764 | {
|
|---|
| 765 | assert(instance);
|
|---|
| 766 | assert(request);
|
|---|
| 767 |
|
|---|
| 768 | if (!request->setup_buffer) {
|
|---|
| 769 | usb_log_error("Root hub received empty transaction!");
|
|---|
| 770 | TRANSFER_END(request, EBADMEM);
|
|---|
| 771 | }
|
|---|
| 772 |
|
|---|
| 773 | if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
|
|---|
| 774 | usb_log_error("Setup packet too small\n");
|
|---|
| 775 | TRANSFER_END(request, EOVERFLOW);
|
|---|
| 776 | }
|
|---|
| 777 |
|
|---|
| 778 | usb_log_debug2("CTRL packet: %s.\n",
|
|---|
| 779 | usb_debug_str_buffer((uint8_t *) request->setup_buffer, 8, 8));
|
|---|
| 780 | usb_device_request_setup_packet_t *setup_request =
|
|---|
| 781 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
|---|
| 782 | switch (setup_request->request)
|
|---|
| 783 | {
|
|---|
| 784 | case USB_DEVREQ_GET_STATUS:
|
|---|
| 785 | usb_log_debug("USB_DEVREQ_GET_STATUS\n");
|
|---|
| 786 | get_status(instance, request);
|
|---|
| 787 | break;
|
|---|
| 788 |
|
|---|
| 789 | case USB_DEVREQ_GET_DESCRIPTOR:
|
|---|
| 790 | usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
|
|---|
| 791 | get_descriptor(instance, request);
|
|---|
| 792 | break;
|
|---|
| 793 |
|
|---|
| 794 | case USB_DEVREQ_GET_CONFIGURATION:
|
|---|
| 795 | usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
|
|---|
| 796 | if (request->buffer_size == 0)
|
|---|
| 797 | TRANSFER_END(request, EOVERFLOW);
|
|---|
| 798 | const uint8_t config = 1;
|
|---|
| 799 | TRANSFER_END_DATA(request, &config, sizeof(config));
|
|---|
| 800 |
|
|---|
| 801 | case USB_DEVREQ_CLEAR_FEATURE:
|
|---|
| 802 | usb_log_debug2("USB_DEVREQ_CLEAR_FEATURE\n");
|
|---|
| 803 | clear_feature(instance, request);
|
|---|
| 804 | break;
|
|---|
| 805 |
|
|---|
| 806 | case USB_DEVREQ_SET_FEATURE:
|
|---|
| 807 | usb_log_debug2("USB_DEVREQ_SET_FEATURE\n");
|
|---|
| 808 | set_feature(instance, request);
|
|---|
| 809 | break;
|
|---|
| 810 |
|
|---|
| 811 | case USB_DEVREQ_SET_ADDRESS:
|
|---|
| 812 | usb_log_debug("USB_DEVREQ_SET_ADDRESS: %u\n",
|
|---|
| 813 | setup_request->value);
|
|---|
| 814 | if (uint16_usb2host(setup_request->value) > 127)
|
|---|
| 815 | TRANSFER_END(request, EINVAL);
|
|---|
| 816 |
|
|---|
| 817 | instance->address = uint16_usb2host(setup_request->value);
|
|---|
| 818 | TRANSFER_END(request, EOK);
|
|---|
| 819 |
|
|---|
| 820 | case USB_DEVREQ_SET_CONFIGURATION:
|
|---|
| 821 | usb_log_debug("USB_DEVREQ_SET_CONFIGURATION: %u\n",
|
|---|
| 822 | uint16_usb2host(setup_request->value));
|
|---|
| 823 | /* We have only one configuration, it's number is 1 */
|
|---|
| 824 | if (uint16_usb2host(setup_request->value) != 1)
|
|---|
| 825 | TRANSFER_END(request, EINVAL);
|
|---|
| 826 | TRANSFER_END(request, EOK);
|
|---|
| 827 |
|
|---|
| 828 | /* Both class specific and std is optional for hubs */
|
|---|
| 829 | case USB_DEVREQ_SET_DESCRIPTOR:
|
|---|
| 830 | /* Hubs have only one interface GET/SET is not supported */
|
|---|
| 831 | case USB_DEVREQ_GET_INTERFACE:
|
|---|
| 832 | case USB_DEVREQ_SET_INTERFACE:
|
|---|
| 833 | default:
|
|---|
| 834 | /* Hub class GET_STATE(2) falls in here too. */
|
|---|
| 835 | usb_log_error("Received unsupported request: %d.\n",
|
|---|
| 836 | setup_request->request);
|
|---|
| 837 | TRANSFER_END(request, ENOTSUP);
|
|---|
| 838 | }
|
|---|
| 839 | }
|
|---|
| 840 | /**
|
|---|
| 841 | * @}
|
|---|
| 842 | */
|
|---|