| 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 <errno.h>
|
|---|
| 36 | #include <str_error.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <usb/debug.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "root_hub.h"
|
|---|
| 41 | #include <usb/classes/classes.h>
|
|---|
| 42 | #include <usb/classes/hub.h>
|
|---|
| 43 | #include <usb/dev/driver.h>
|
|---|
| 44 | #include "ohci_regs.h"
|
|---|
| 45 |
|
|---|
| 46 | #include <usb/dev/request.h>
|
|---|
| 47 | #include <usb/classes/hub.h>
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * standart device descriptor for ohci root hub
|
|---|
| 51 | */
|
|---|
| 52 | static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
|
|---|
| 53 | .configuration_count = 1,
|
|---|
| 54 | .descriptor_type = USB_DESCTYPE_DEVICE,
|
|---|
| 55 | .device_class = USB_CLASS_HUB,
|
|---|
| 56 | .device_protocol = 0,
|
|---|
| 57 | .device_subclass = 0,
|
|---|
| 58 | .device_version = 0,
|
|---|
| 59 | .length = sizeof(usb_standard_device_descriptor_t),
|
|---|
| 60 | .max_packet_size = 64,
|
|---|
| 61 | .vendor_id = 0x16db, /* HelenOS does not have USB vendor ID assigned.*/
|
|---|
| 62 | .product_id = 0x0001,
|
|---|
| 63 | .str_serial_number = 0,
|
|---|
| 64 | .usb_spec_version = 0x110,
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * standart configuration descriptor with filled common values
|
|---|
| 69 | * for ohci root hubs
|
|---|
| 70 | */
|
|---|
| 71 | static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
|
|---|
| 72 | .attributes = 1 << 7,
|
|---|
| 73 | .configuration_number = 1,
|
|---|
| 74 | .descriptor_type = USB_DESCTYPE_CONFIGURATION,
|
|---|
| 75 | .interface_count = 1,
|
|---|
| 76 | .length = sizeof(usb_standard_configuration_descriptor_t),
|
|---|
| 77 | .max_power = 0, /* root hubs don't need no power */
|
|---|
| 78 | .str_configuration = 0,
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * standart ohci root hub interface descriptor
|
|---|
| 83 | */
|
|---|
| 84 | static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
|
|---|
| 85 | .alternate_setting = 0,
|
|---|
| 86 | .descriptor_type = USB_DESCTYPE_INTERFACE,
|
|---|
| 87 | .endpoint_count = 1,
|
|---|
| 88 | .interface_class = USB_CLASS_HUB,
|
|---|
| 89 | .interface_number = 1,
|
|---|
| 90 | .interface_protocol = 0,
|
|---|
| 91 | .interface_subclass = 0,
|
|---|
| 92 | .length = sizeof(usb_standard_interface_descriptor_t),
|
|---|
| 93 | .str_interface = 0,
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * standart ohci root hub endpoint descriptor
|
|---|
| 98 | */
|
|---|
| 99 | static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
|
|---|
| 100 | .attributes = USB_TRANSFER_INTERRUPT,
|
|---|
| 101 | .descriptor_type = USB_DESCTYPE_ENDPOINT,
|
|---|
| 102 | .endpoint_address = 1 + (1 << 7),
|
|---|
| 103 | .length = sizeof(usb_standard_endpoint_descriptor_t),
|
|---|
| 104 | .max_packet_size = 2,
|
|---|
| 105 | .poll_interval = 255,
|
|---|
| 106 | };
|
|---|
| 107 |
|
|---|
| 108 | static void create_serialized_hub_descriptor(rh_t *instance);
|
|---|
| 109 | static void rh_init_descriptors(rh_t *instance);
|
|---|
| 110 | static uint16_t create_interrupt_mask(rh_t *instance);
|
|---|
| 111 | static int get_status(rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 112 | static int get_descriptor(rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 113 | static int set_feature(rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 114 | static int clear_feature(rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 115 | static int set_feature_port(rh_t *instance, uint16_t feature, uint16_t port);
|
|---|
| 116 | static int clear_feature_port(rh_t *instance, uint16_t feature, uint16_t port);
|
|---|
| 117 | static int control_request(rh_t *instance, usb_transfer_batch_t *request);
|
|---|
| 118 | static inline void interrupt_request(
|
|---|
| 119 | usb_transfer_batch_t *request, uint16_t mask, size_t size)
|
|---|
| 120 | {
|
|---|
| 121 | assert(request);
|
|---|
| 122 |
|
|---|
| 123 | request->transfered_size = size;
|
|---|
| 124 | usb_transfer_batch_finish_error(request, &mask, size, EOK);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | #define TRANSFER_OK(bytes) \
|
|---|
| 128 | do { \
|
|---|
| 129 | request->transfered_size = bytes; \
|
|---|
| 130 | return EOK; \
|
|---|
| 131 | } while (0)
|
|---|
| 132 |
|
|---|
| 133 | /** Root Hub driver structure initialization.
|
|---|
| 134 | *
|
|---|
| 135 | * Reads info registers and prepares descriptors. Sets power mode.
|
|---|
| 136 | */
|
|---|
| 137 | void rh_init(rh_t *instance, ohci_regs_t *regs)
|
|---|
| 138 | {
|
|---|
| 139 | assert(instance);
|
|---|
| 140 | assert(regs);
|
|---|
| 141 |
|
|---|
| 142 | instance->registers = regs;
|
|---|
| 143 | instance->port_count =
|
|---|
| 144 | (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
|
|---|
| 145 | if (instance->port_count > 15) {
|
|---|
| 146 | usb_log_warning("OHCI specification does not allow more than 15"
|
|---|
| 147 | " ports. Max 15 ports will be used");
|
|---|
| 148 | instance->port_count = 15;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /* Don't forget the hub status bit and round up */
|
|---|
| 152 | instance->interrupt_mask_size = 1 + (instance->port_count / 8);
|
|---|
| 153 | instance->unfinished_interrupt_transfer = NULL;
|
|---|
| 154 |
|
|---|
| 155 | #ifdef OHCI_POWER_SWITCH_no
|
|---|
| 156 | /* Set port power mode to no power-switching. (always on) */
|
|---|
| 157 | instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
|
|---|
| 158 | /* Set to no over-current reporting */
|
|---|
| 159 | instance->registers->rh_desc_a |= RHDA_NOCP_FLAG;
|
|---|
| 160 | #elif defined OHCI_POWER_SWITCH_ganged
|
|---|
| 161 | /* Set port power mode to no ganged power-switching. */
|
|---|
| 162 | instance->registers->rh_desc_a &= ~RHDA_NPS_FLAG;
|
|---|
| 163 | instance->registers->rh_desc_a &= ~RHDA_PSM_FLAG;
|
|---|
| 164 | instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
|
|---|
| 165 | /* Set to global over-current */
|
|---|
| 166 | instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
|
|---|
| 167 | instance->registers->rh_desc_a &= ~RHDA_OCPM_FLAG;
|
|---|
| 168 | #else
|
|---|
| 169 | /* Set port power mode to no per port power-switching. */
|
|---|
| 170 | instance->registers->rh_desc_a &= ~RHDA_NPS_FLAG;
|
|---|
| 171 | instance->registers->rh_desc_a |= RHDA_PSM_FLAG;
|
|---|
| 172 |
|
|---|
| 173 | /* Control all ports by global switch and turn them off */
|
|---|
| 174 | instance->registers->rh_desc_b &= (RHDB_PCC_MASK << RHDB_PCC_SHIFT);
|
|---|
| 175 | instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
|
|---|
| 176 | /* Return control to per port state */
|
|---|
| 177 | instance->registers->rh_desc_b |=
|
|---|
| 178 | ((1 << (instance->port_count + 1)) - 1) << RHDB_PCC_SHIFT;
|
|---|
| 179 | /* Set per port over-current */
|
|---|
| 180 | instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
|
|---|
| 181 | instance->registers->rh_desc_a |= RHDA_OCPM_FLAG;
|
|---|
| 182 | #endif
|
|---|
| 183 |
|
|---|
| 184 | rh_init_descriptors(instance);
|
|---|
| 185 |
|
|---|
| 186 | usb_log_info("Root hub (%zu ports) initialized.\n",
|
|---|
| 187 | instance->port_count);
|
|---|
| 188 | }
|
|---|
| 189 | /*----------------------------------------------------------------------------*/
|
|---|
| 190 | /**
|
|---|
| 191 | * Process root hub request.
|
|---|
| 192 | *
|
|---|
| 193 | * @param instance Root hub instance
|
|---|
| 194 | * @param request Structure containing both request and response information
|
|---|
| 195 | * @return Error code
|
|---|
| 196 | */
|
|---|
| 197 | void rh_request(rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 198 | {
|
|---|
| 199 | assert(instance);
|
|---|
| 200 | assert(request);
|
|---|
| 201 |
|
|---|
| 202 | switch (request->ep->transfer_type)
|
|---|
| 203 | {
|
|---|
| 204 | case USB_TRANSFER_CONTROL:
|
|---|
| 205 | usb_log_debug("Root hub got CONTROL packet\n");
|
|---|
| 206 | const int ret = control_request(instance, request);
|
|---|
| 207 | usb_transfer_batch_finish_error(request, NULL, 0, ret);
|
|---|
| 208 | break;
|
|---|
| 209 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 210 | usb_log_debug("Root hub got INTERRUPT packet\n");
|
|---|
| 211 | const uint16_t mask = create_interrupt_mask(instance);
|
|---|
| 212 | if (mask == 0) {
|
|---|
| 213 | usb_log_debug("No changes..\n");
|
|---|
| 214 | assert(instance->unfinished_interrupt_transfer == NULL);
|
|---|
| 215 | instance->unfinished_interrupt_transfer = request;
|
|---|
| 216 | return;
|
|---|
| 217 | }
|
|---|
| 218 | usb_log_debug("Processing changes...\n");
|
|---|
| 219 | interrupt_request(request, mask, instance->interrupt_mask_size);
|
|---|
| 220 | break;
|
|---|
| 221 |
|
|---|
| 222 | default:
|
|---|
| 223 | usb_log_error("Root hub got unsupported request.\n");
|
|---|
| 224 | usb_transfer_batch_finish_error(request, NULL, 0, EINVAL);
|
|---|
| 225 | }
|
|---|
| 226 | usb_transfer_batch_dispose(request);
|
|---|
| 227 | }
|
|---|
| 228 | /*----------------------------------------------------------------------------*/
|
|---|
| 229 | /**
|
|---|
| 230 | * Process interrupt on a hub device.
|
|---|
| 231 | *
|
|---|
| 232 | * If there is no pending interrupt transfer, nothing happens.
|
|---|
| 233 | * @param instance
|
|---|
| 234 | */
|
|---|
| 235 | void rh_interrupt(rh_t *instance)
|
|---|
| 236 | {
|
|---|
| 237 | assert(instance);
|
|---|
| 238 |
|
|---|
| 239 | if (!instance->unfinished_interrupt_transfer)
|
|---|
| 240 | return;
|
|---|
| 241 |
|
|---|
| 242 | usb_log_debug("Finalizing interrupt transfer\n");
|
|---|
| 243 | const uint16_t mask = create_interrupt_mask(instance);
|
|---|
| 244 | interrupt_request(instance->unfinished_interrupt_transfer,
|
|---|
| 245 | mask, instance->interrupt_mask_size);
|
|---|
| 246 | usb_transfer_batch_dispose(instance->unfinished_interrupt_transfer);
|
|---|
| 247 |
|
|---|
| 248 | instance->unfinished_interrupt_transfer = NULL;
|
|---|
| 249 | }
|
|---|
| 250 | /*----------------------------------------------------------------------------*/
|
|---|
| 251 | /**
|
|---|
| 252 | * Create hub descriptor.
|
|---|
| 253 | *
|
|---|
| 254 | * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
|
|---|
| 255 | *
|
|---|
| 256 | * @param instance Root hub instance
|
|---|
| 257 | * @return Error code
|
|---|
| 258 | */
|
|---|
| 259 | void create_serialized_hub_descriptor(rh_t *instance)
|
|---|
| 260 | {
|
|---|
| 261 | assert(instance);
|
|---|
| 262 |
|
|---|
| 263 | /* 7 bytes + 2 port bit fields (port count + global bit) */
|
|---|
| 264 | const size_t size = 7 + (instance->interrupt_mask_size * 2);
|
|---|
| 265 | assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
|
|---|
| 266 | instance->hub_descriptor_size = size;
|
|---|
| 267 |
|
|---|
| 268 | const uint32_t hub_desc = instance->registers->rh_desc_a;
|
|---|
| 269 | const uint32_t port_desc = instance->registers->rh_desc_b;
|
|---|
| 270 |
|
|---|
| 271 | /* bDescLength */
|
|---|
| 272 | instance->descriptors.hub[0] = size;
|
|---|
| 273 | /* bDescriptorType */
|
|---|
| 274 | instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
|
|---|
| 275 | /* bNmbrPorts */
|
|---|
| 276 | instance->descriptors.hub[2] = instance->port_count;
|
|---|
| 277 | /* wHubCharacteristics */
|
|---|
| 278 | instance->descriptors.hub[3] = 0 |
|
|---|
| 279 | /* The lowest 2 bits indicate power switching mode */
|
|---|
| 280 | (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
|
|---|
| 281 | (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
|
|---|
| 282 | /* Bit 3 indicates device type (compound device) */
|
|---|
| 283 | (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
|
|---|
| 284 | /* Bits 4,5 indicate over-current protection mode */
|
|---|
| 285 | (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
|
|---|
| 286 | (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
|
|---|
| 287 |
|
|---|
| 288 | /* Reserved */
|
|---|
| 289 | instance->descriptors.hub[4] = 0;
|
|---|
| 290 | /* bPwrOn2PwrGood */
|
|---|
| 291 | instance->descriptors.hub[5] =
|
|---|
| 292 | (hub_desc >> RHDA_POTPGT_SHIFT) & RHDA_POTPGT_MASK;
|
|---|
| 293 | /* bHubContrCurrent, root hubs don't need no power. */
|
|---|
| 294 | instance->descriptors.hub[6] = 0;
|
|---|
| 295 |
|
|---|
| 296 | /* Device Removable and some legacy 1.0 stuff*/
|
|---|
| 297 | instance->descriptors.hub[7] =
|
|---|
| 298 | (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK & 0xff;
|
|---|
| 299 | instance->descriptors.hub[8] = 0xff;
|
|---|
| 300 | if (instance->interrupt_mask_size == 2) {
|
|---|
| 301 | instance->descriptors.hub[8] =
|
|---|
| 302 | (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK >> 8;
|
|---|
| 303 | instance->descriptors.hub[9] = 0xff;
|
|---|
| 304 | instance->descriptors.hub[10] = 0xff;
|
|---|
| 305 | }
|
|---|
| 306 | }
|
|---|
| 307 | /*----------------------------------------------------------------------------*/
|
|---|
| 308 | /** Initialize hub descriptors.
|
|---|
| 309 | *
|
|---|
| 310 | * A full configuration descriptor is assembled. The configuration and endpoint
|
|---|
| 311 | * descriptors have local modifications.
|
|---|
| 312 | * @param instance Root hub instance
|
|---|
| 313 | * @return Error code
|
|---|
| 314 | */
|
|---|
| 315 | void rh_init_descriptors(rh_t *instance)
|
|---|
| 316 | {
|
|---|
| 317 | assert(instance);
|
|---|
| 318 |
|
|---|
| 319 | instance->descriptors.configuration = ohci_rh_conf_descriptor;
|
|---|
| 320 | instance->descriptors.interface = ohci_rh_iface_descriptor;
|
|---|
| 321 | instance->descriptors.endpoint = ohci_rh_ep_descriptor;
|
|---|
| 322 | create_serialized_hub_descriptor(instance);
|
|---|
| 323 |
|
|---|
| 324 | instance->descriptors.endpoint.max_packet_size =
|
|---|
| 325 | instance->interrupt_mask_size;
|
|---|
| 326 |
|
|---|
| 327 | instance->descriptors.configuration.total_length =
|
|---|
| 328 | sizeof(usb_standard_configuration_descriptor_t) +
|
|---|
| 329 | sizeof(usb_standard_endpoint_descriptor_t) +
|
|---|
| 330 | sizeof(usb_standard_interface_descriptor_t) +
|
|---|
| 331 | instance->hub_descriptor_size;
|
|---|
| 332 | }
|
|---|
| 333 | /*----------------------------------------------------------------------------*/
|
|---|
| 334 | /**
|
|---|
| 335 | * Create bitmap of changes to answer status interrupt.
|
|---|
| 336 | *
|
|---|
| 337 | * Result contains bitmap where bit 0 indicates change on hub and
|
|---|
| 338 | * bit i indicates change on i`th port (i>0). For more info see
|
|---|
| 339 | * Hub and Port status bitmap specification in USB specification
|
|---|
| 340 | * (chapter 11.13.4).
|
|---|
| 341 | * @param instance root hub instance
|
|---|
| 342 | * @return Mask of changes.
|
|---|
| 343 | */
|
|---|
| 344 | uint16_t create_interrupt_mask(rh_t *instance)
|
|---|
| 345 | {
|
|---|
| 346 | assert(instance);
|
|---|
| 347 | uint16_t mask = 0;
|
|---|
| 348 |
|
|---|
| 349 | /* Only local power source change and over-current change can happen */
|
|---|
| 350 | if (instance->registers->rh_status & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
|
|---|
| 351 | mask |= 1;
|
|---|
| 352 | }
|
|---|
| 353 | size_t port = 1;
|
|---|
| 354 | for (; port <= instance->port_count; ++port) {
|
|---|
| 355 | /* Write-clean bits are those that indicate change */
|
|---|
| 356 | if (RHPS_CHANGE_WC_MASK
|
|---|
| 357 | & instance->registers->rh_port_status[port - 1]) {
|
|---|
| 358 |
|
|---|
| 359 | mask |= (1 << port);
|
|---|
| 360 | }
|
|---|
| 361 | }
|
|---|
| 362 | /* USB is little endian */
|
|---|
| 363 | return host2uint32_t_le(mask);
|
|---|
| 364 | }
|
|---|
| 365 | /*----------------------------------------------------------------------------*/
|
|---|
| 366 | /**
|
|---|
| 367 | * Create answer to status request.
|
|---|
| 368 | *
|
|---|
| 369 | * This might be either hub status or port status request. If neither,
|
|---|
| 370 | * ENOTSUP is returned.
|
|---|
| 371 | * @param instance root hub instance
|
|---|
| 372 | * @param request structure containing both request and response information
|
|---|
| 373 | * @return error code
|
|---|
| 374 | */
|
|---|
| 375 | int get_status(rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 376 | {
|
|---|
| 377 | assert(instance);
|
|---|
| 378 | assert(request);
|
|---|
| 379 |
|
|---|
| 380 | const usb_device_request_setup_packet_t *request_packet =
|
|---|
| 381 | (usb_device_request_setup_packet_t*)request->setup_buffer;
|
|---|
| 382 |
|
|---|
| 383 | if (request->buffer_size < 4) {
|
|---|
| 384 | usb_log_error("Buffer too small for get status request.\n");
|
|---|
| 385 | return EOVERFLOW;
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | /* Hub status: just filter relevant info from rh_status reg */
|
|---|
| 389 | if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS) {
|
|---|
| 390 | const uint32_t data = instance->registers->rh_status &
|
|---|
| 391 | (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
|
|---|
| 392 | memcpy(request->buffer, &data, sizeof(data));
|
|---|
| 393 | TRANSFER_OK(sizeof(data));
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | /* Copy appropriate rh_port_status register, OHCI designers were
|
|---|
| 397 | * kind enough to make those bit values match USB specification */
|
|---|
| 398 | if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS) {
|
|---|
| 399 | const unsigned port = request_packet->index;
|
|---|
| 400 | if (port < 1 || port > instance->port_count)
|
|---|
| 401 | return EINVAL;
|
|---|
| 402 |
|
|---|
| 403 | const uint32_t data =
|
|---|
| 404 | instance->registers->rh_port_status[port - 1];
|
|---|
| 405 | memcpy(request->buffer, &data, sizeof(data));
|
|---|
| 406 | TRANSFER_OK(sizeof(data));
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | return ENOTSUP;
|
|---|
| 410 | }
|
|---|
| 411 | /*----------------------------------------------------------------------------*/
|
|---|
| 412 | /**
|
|---|
| 413 | * Create answer to a descriptor request.
|
|---|
| 414 | *
|
|---|
| 415 | * This might be a request for standard (configuration, device, endpoint or
|
|---|
| 416 | * interface) or device specific (hub) descriptor.
|
|---|
| 417 | * @param instance Root hub instance
|
|---|
| 418 | * @param request Structure containing both request and response information
|
|---|
| 419 | * @return Error code
|
|---|
| 420 | */
|
|---|
| 421 | int get_descriptor(rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 422 | {
|
|---|
| 423 | assert(instance);
|
|---|
| 424 | assert(request);
|
|---|
| 425 |
|
|---|
| 426 | const usb_device_request_setup_packet_t *setup_request =
|
|---|
| 427 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
|---|
| 428 | size_t size;
|
|---|
| 429 | const void *descriptor = NULL;
|
|---|
| 430 | const uint16_t setup_request_value = setup_request->value_high;
|
|---|
| 431 | //(setup_request->value_low << 8);
|
|---|
| 432 | switch (setup_request_value)
|
|---|
| 433 | {
|
|---|
| 434 | case USB_DESCTYPE_HUB:
|
|---|
| 435 | usb_log_debug2("USB_DESCTYPE_HUB\n");
|
|---|
| 436 | /* Hub descriptor was generated locally */
|
|---|
| 437 | descriptor = instance->descriptors.hub;
|
|---|
| 438 | size = instance->hub_descriptor_size;
|
|---|
| 439 | break;
|
|---|
| 440 |
|
|---|
| 441 | case USB_DESCTYPE_DEVICE:
|
|---|
| 442 | usb_log_debug2("USB_DESCTYPE_DEVICE\n");
|
|---|
| 443 | /* Device descriptor is shared (No one should ask for it)*/
|
|---|
| 444 | descriptor = &ohci_rh_device_descriptor;
|
|---|
| 445 | size = sizeof(ohci_rh_device_descriptor);
|
|---|
| 446 | break;
|
|---|
| 447 |
|
|---|
| 448 | case USB_DESCTYPE_CONFIGURATION:
|
|---|
| 449 | usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
|
|---|
| 450 | /* Start with configuration and add others depending on
|
|---|
| 451 | * request size */
|
|---|
| 452 | descriptor = &instance->descriptors;
|
|---|
| 453 | size = instance->descriptors.configuration.total_length;
|
|---|
| 454 | break;
|
|---|
| 455 |
|
|---|
| 456 | case USB_DESCTYPE_INTERFACE:
|
|---|
| 457 | usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
|
|---|
| 458 | /* Use local interface descriptor. There is one and it
|
|---|
| 459 | * might be modified */
|
|---|
| 460 | descriptor = &instance->descriptors.interface;
|
|---|
| 461 | size = sizeof(instance->descriptors.interface);
|
|---|
| 462 | break;
|
|---|
| 463 |
|
|---|
| 464 | case USB_DESCTYPE_ENDPOINT:
|
|---|
| 465 | /* Use local endpoint descriptor. There is one
|
|---|
| 466 | * it might have max_packet_size field modified*/
|
|---|
| 467 | usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
|
|---|
| 468 | descriptor = &instance->descriptors.endpoint;
|
|---|
| 469 | size = sizeof(instance->descriptors.endpoint);
|
|---|
| 470 | break;
|
|---|
| 471 |
|
|---|
| 472 | default:
|
|---|
| 473 | usb_log_debug2("USB_DESCTYPE_EINVAL %d \n"
|
|---|
| 474 | "\ttype %d\n\trequest %d\n\tvalue "
|
|---|
| 475 | "%d\n\tindex %d\n\tlen %d\n ",
|
|---|
| 476 | setup_request->value,
|
|---|
| 477 | setup_request->request_type, setup_request->request,
|
|---|
| 478 | setup_request_value, setup_request->index,
|
|---|
| 479 | setup_request->length);
|
|---|
| 480 | return EINVAL;
|
|---|
| 481 | }
|
|---|
| 482 | if (request->buffer_size < size) {
|
|---|
| 483 | size = request->buffer_size;
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | memcpy(request->buffer, descriptor, size);
|
|---|
| 487 | TRANSFER_OK(size);
|
|---|
| 488 | }
|
|---|
| 489 | /*----------------------------------------------------------------------------*/
|
|---|
| 490 | /**
|
|---|
| 491 | * process feature-enabling request on hub
|
|---|
| 492 | *
|
|---|
| 493 | * @param instance root hub instance
|
|---|
| 494 | * @param feature feature selector
|
|---|
| 495 | * @param port port number, counted from 1
|
|---|
| 496 | * @param enable enable or disable the specified feature
|
|---|
| 497 | * @return error code
|
|---|
| 498 | */
|
|---|
| 499 | int set_feature_port(rh_t *instance, uint16_t feature, uint16_t port)
|
|---|
| 500 | {
|
|---|
| 501 | assert(instance);
|
|---|
| 502 |
|
|---|
| 503 | if (port < 1 || port > instance->port_count)
|
|---|
| 504 | return EINVAL;
|
|---|
| 505 |
|
|---|
| 506 | switch (feature)
|
|---|
| 507 | {
|
|---|
| 508 | case USB_HUB_FEATURE_PORT_POWER: //8
|
|---|
| 509 | /* No power switching */
|
|---|
| 510 | if (instance->registers->rh_desc_a & RHDA_NPS_FLAG)
|
|---|
| 511 | return EOK;
|
|---|
| 512 | /* Ganged power switching */
|
|---|
| 513 | if (!(instance->registers->rh_desc_a & RHDA_PSM_FLAG)) {
|
|---|
| 514 | instance->registers->rh_status = RHS_SET_GLOBAL_POWER;
|
|---|
| 515 | return EOK;
|
|---|
| 516 | }
|
|---|
| 517 | case USB_HUB_FEATURE_PORT_ENABLE: //1
|
|---|
| 518 | case USB_HUB_FEATURE_PORT_SUSPEND: //2
|
|---|
| 519 | case USB_HUB_FEATURE_PORT_RESET: //4
|
|---|
| 520 | /* Nice thing is that these shifts correspond to the position
|
|---|
| 521 | * of control bits in register */
|
|---|
| 522 | instance->registers->rh_port_status[port - 1] = (1 << feature);
|
|---|
| 523 | return EOK;
|
|---|
| 524 | default:
|
|---|
| 525 | return ENOTSUP;
|
|---|
| 526 | }
|
|---|
| 527 | }
|
|---|
| 528 | /*----------------------------------------------------------------------------*/
|
|---|
| 529 | /**
|
|---|
| 530 | * Process feature clear request.
|
|---|
| 531 | *
|
|---|
| 532 | * @param instance root hub instance
|
|---|
| 533 | * @param feature feature selector
|
|---|
| 534 | * @param port port number, counted from 1
|
|---|
| 535 | * @param enable enable or disable the specified feature
|
|---|
| 536 | * @return error code
|
|---|
| 537 | */
|
|---|
| 538 | int clear_feature_port(rh_t *instance, uint16_t feature, uint16_t port)
|
|---|
| 539 | {
|
|---|
| 540 | assert(instance);
|
|---|
| 541 |
|
|---|
| 542 | if (port < 1 || port > instance->port_count)
|
|---|
| 543 | return EINVAL;
|
|---|
| 544 |
|
|---|
| 545 | /* Enabled features to clear: see page 269 of USB specs */
|
|---|
| 546 | switch (feature)
|
|---|
| 547 | {
|
|---|
| 548 | case USB_HUB_FEATURE_PORT_POWER: //8
|
|---|
| 549 | /* No power switching */
|
|---|
| 550 | if (instance->registers->rh_desc_a & RHDA_NPS_FLAG)
|
|---|
| 551 | return ENOTSUP;
|
|---|
| 552 | /* Ganged power switching */
|
|---|
| 553 | if (!(instance->registers->rh_desc_a & RHDA_PSM_FLAG)) {
|
|---|
| 554 | instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
|
|---|
| 555 | return EOK;
|
|---|
| 556 | }
|
|---|
| 557 | instance->registers->rh_port_status[port - 1] =
|
|---|
| 558 | RHPS_CLEAR_PORT_POWER;
|
|---|
| 559 | return EOK;
|
|---|
| 560 |
|
|---|
| 561 | case USB_HUB_FEATURE_PORT_ENABLE: //1
|
|---|
| 562 | instance->registers->rh_port_status[port - 1] =
|
|---|
| 563 | RHPS_CLEAR_PORT_ENABLE;
|
|---|
| 564 | return EOK;
|
|---|
| 565 |
|
|---|
| 566 | case USB_HUB_FEATURE_PORT_SUSPEND: //2
|
|---|
| 567 | instance->registers->rh_port_status[port - 1] =
|
|---|
| 568 | RHPS_CLEAR_PORT_SUSPEND;
|
|---|
| 569 | return EOK;
|
|---|
| 570 |
|
|---|
| 571 | case USB_HUB_FEATURE_C_PORT_CONNECTION: //16
|
|---|
| 572 | case USB_HUB_FEATURE_C_PORT_ENABLE: //17
|
|---|
| 573 | case USB_HUB_FEATURE_C_PORT_SUSPEND: //18
|
|---|
| 574 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: //19
|
|---|
| 575 | case USB_HUB_FEATURE_C_PORT_RESET: //20
|
|---|
| 576 | /* Nice thing is that these shifts correspond to the position
|
|---|
| 577 | * of control bits in register */
|
|---|
| 578 | instance->registers->rh_port_status[port - 1] = (1 << feature);
|
|---|
| 579 | return EOK;
|
|---|
| 580 |
|
|---|
| 581 | default:
|
|---|
| 582 | return ENOTSUP;
|
|---|
| 583 | }
|
|---|
| 584 | }
|
|---|
| 585 | /*----------------------------------------------------------------------------*/
|
|---|
| 586 | /**
|
|---|
| 587 | * process one of requests that do not request nor carry additional data
|
|---|
| 588 | *
|
|---|
| 589 | * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
|
|---|
| 590 | * USB_DEVREQ_SET_ADDRESS.
|
|---|
| 591 | * @param instance root hub instance
|
|---|
| 592 | * @param request structure containing both request and response information
|
|---|
| 593 | * @return error code
|
|---|
| 594 | */
|
|---|
| 595 | int set_feature(rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 596 | {
|
|---|
| 597 | assert(instance);
|
|---|
| 598 | assert(request);
|
|---|
| 599 |
|
|---|
| 600 | const usb_device_request_setup_packet_t *setup_request =
|
|---|
| 601 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
|---|
| 602 | switch (setup_request->request_type)
|
|---|
| 603 | {
|
|---|
| 604 | case USB_HUB_REQ_TYPE_SET_PORT_FEATURE:
|
|---|
| 605 | usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
|
|---|
| 606 | return set_feature_port(instance,
|
|---|
| 607 | setup_request->value, setup_request->index);
|
|---|
| 608 |
|
|---|
| 609 | case USB_HUB_REQ_TYPE_SET_HUB_FEATURE:
|
|---|
| 610 | /* Chapter 11.16.2 specifies that hub can be recipient
|
|---|
| 611 | * only for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
|
|---|
| 612 | * features. It makes no sense to SET either. */
|
|---|
| 613 | usb_log_error("Invalid HUB set feature request.\n");
|
|---|
| 614 | return ENOTSUP;
|
|---|
| 615 | default:
|
|---|
| 616 | usb_log_error("Invalid set feature request type: %d\n",
|
|---|
| 617 | setup_request->request_type);
|
|---|
| 618 | return EINVAL;
|
|---|
| 619 | }
|
|---|
| 620 | }
|
|---|
| 621 | /*----------------------------------------------------------------------------*/
|
|---|
| 622 | /**
|
|---|
| 623 | * process one of requests that do not request nor carry additional data
|
|---|
| 624 | *
|
|---|
| 625 | * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
|
|---|
| 626 | * USB_DEVREQ_SET_ADDRESS.
|
|---|
| 627 | * @param instance root hub instance
|
|---|
| 628 | * @param request structure containing both request and response information
|
|---|
| 629 | * @return error code
|
|---|
| 630 | */
|
|---|
| 631 | int clear_feature(rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 632 | {
|
|---|
| 633 | assert(instance);
|
|---|
| 634 | assert(request);
|
|---|
| 635 |
|
|---|
| 636 | const usb_device_request_setup_packet_t *setup_request =
|
|---|
| 637 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
|---|
| 638 | request->transfered_size = 0;
|
|---|
| 639 | switch (setup_request->request_type)
|
|---|
| 640 | {
|
|---|
| 641 | case USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE:
|
|---|
| 642 | usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE\n");
|
|---|
| 643 | return clear_feature_port(instance,
|
|---|
| 644 | setup_request->value, setup_request->index);
|
|---|
| 645 |
|
|---|
| 646 | case USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE:
|
|---|
| 647 | usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE\n");
|
|---|
| 648 | /*
|
|---|
| 649 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
|---|
| 650 | * C_HUB_OVER_CURRENT are supported. C_HUB_OVER_CURRENT is represented
|
|---|
| 651 | * by OHCI RHS_OCIC_FLAG. C_HUB_LOCAL_POWER is not supported
|
|---|
| 652 | * as root hubs do not support local power status feature.
|
|---|
| 653 | * (OHCI pg. 127) */
|
|---|
| 654 | if (setup_request->value == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
|
|---|
| 655 | instance->registers->rh_status = RHS_OCIC_FLAG;
|
|---|
| 656 | TRANSFER_OK(0);
|
|---|
| 657 | }
|
|---|
| 658 | default:
|
|---|
| 659 | usb_log_error("Invalid clear feature request type: %d\n",
|
|---|
| 660 | setup_request->request_type);
|
|---|
| 661 | return EINVAL;
|
|---|
| 662 | }
|
|---|
| 663 | }
|
|---|
| 664 | /*----------------------------------------------------------------------------*/
|
|---|
| 665 | /**
|
|---|
| 666 | * Process hub control request.
|
|---|
| 667 | *
|
|---|
| 668 | * If needed, writes answer into the request structure.
|
|---|
| 669 | * Request can be one of
|
|---|
| 670 | * USB_DEVREQ_GET_STATUS,
|
|---|
| 671 | * USB_DEVREQ_GET_DESCRIPTOR,
|
|---|
| 672 | * USB_DEVREQ_GET_CONFIGURATION,
|
|---|
| 673 | * USB_DEVREQ_CLEAR_FEATURE,
|
|---|
| 674 | * USB_DEVREQ_SET_FEATURE,
|
|---|
| 675 | * USB_DEVREQ_SET_ADDRESS,
|
|---|
| 676 | * USB_DEVREQ_SET_DESCRIPTOR or
|
|---|
| 677 | * USB_DEVREQ_SET_CONFIGURATION.
|
|---|
| 678 | *
|
|---|
| 679 | * @param instance root hub instance
|
|---|
| 680 | * @param request structure containing both request and response information
|
|---|
| 681 | * @return error code
|
|---|
| 682 | */
|
|---|
| 683 | int control_request(rh_t *instance, usb_transfer_batch_t *request)
|
|---|
| 684 | {
|
|---|
| 685 | assert(instance);
|
|---|
| 686 | assert(request);
|
|---|
| 687 |
|
|---|
| 688 | if (!request->setup_buffer) {
|
|---|
| 689 | usb_log_error("Root hub received empty transaction!");
|
|---|
| 690 | return EINVAL;
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
|
|---|
| 694 | usb_log_error("Setup packet too small\n");
|
|---|
| 695 | return EOVERFLOW;
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| 698 | usb_log_debug2("CTRL packet: %s.\n",
|
|---|
| 699 | usb_debug_str_buffer((uint8_t *) request->setup_buffer, 8, 8));
|
|---|
| 700 | const usb_device_request_setup_packet_t *setup_request =
|
|---|
| 701 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
|---|
| 702 | switch (setup_request->request)
|
|---|
| 703 | {
|
|---|
| 704 | case USB_DEVREQ_GET_STATUS:
|
|---|
| 705 | usb_log_debug("USB_DEVREQ_GET_STATUS\n");
|
|---|
| 706 | return get_status(instance, request);
|
|---|
| 707 |
|
|---|
| 708 | case USB_DEVREQ_GET_DESCRIPTOR:
|
|---|
| 709 | usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
|
|---|
| 710 | return get_descriptor(instance, request);
|
|---|
| 711 |
|
|---|
| 712 | case USB_DEVREQ_GET_CONFIGURATION:
|
|---|
| 713 | usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
|
|---|
| 714 | if (request->buffer_size != 1)
|
|---|
| 715 | return EINVAL;
|
|---|
| 716 | request->buffer[0] = 1;
|
|---|
| 717 | TRANSFER_OK(1);
|
|---|
| 718 |
|
|---|
| 719 | case USB_DEVREQ_CLEAR_FEATURE:
|
|---|
| 720 | usb_log_debug2("Processing request without "
|
|---|
| 721 | "additional data\n");
|
|---|
| 722 | return clear_feature(instance, request);
|
|---|
| 723 | case USB_DEVREQ_SET_FEATURE:
|
|---|
| 724 | usb_log_debug2("Processing request without "
|
|---|
| 725 | "additional data\n");
|
|---|
| 726 | return set_feature(instance, request);
|
|---|
| 727 |
|
|---|
| 728 | case USB_DEVREQ_SET_ADDRESS:
|
|---|
| 729 | usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
|
|---|
| 730 | instance->address = setup_request->value;
|
|---|
| 731 | TRANSFER_OK(0);
|
|---|
| 732 |
|
|---|
| 733 | case USB_DEVREQ_SET_CONFIGURATION:
|
|---|
| 734 | usb_log_debug("USB_DEVREQ_SET_CONFIGURATION\n");
|
|---|
| 735 | /* We don't need to do anything */
|
|---|
| 736 | TRANSFER_OK(0);
|
|---|
| 737 |
|
|---|
| 738 | case USB_DEVREQ_SET_DESCRIPTOR: /* Not supported by OHCI RH */
|
|---|
| 739 | default:
|
|---|
| 740 | usb_log_error("Received unsupported request: %d.\n",
|
|---|
| 741 | setup_request->request);
|
|---|
| 742 | return ENOTSUP;
|
|---|
| 743 | }
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | /**
|
|---|
| 747 | * @}
|
|---|
| 748 | */
|
|---|