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