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