[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>
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <str_error.h>
|
---|
| 37 |
|
---|
| 38 | #include <usb/debug.h>
|
---|
| 39 |
|
---|
[bab71635] | 40 | #include "root_hub.h"
|
---|
[d8421c4] | 41 | #include "usb/classes/classes.h"
|
---|
[66a54cc] | 42 | #include "usb/devdrv.h"
|
---|
[4d0c40b] | 43 | #include <usb/request.h>
|
---|
| 44 | #include <usb/classes/hub.h>
|
---|
| 45 |
|
---|
[0368669c] | 46 | /**
|
---|
| 47 | * standart device descriptor for ohci root hub
|
---|
| 48 | */
|
---|
[8b74997f] | 49 | static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
|
---|
| 50 | .configuration_count = 1,
|
---|
| 51 | .descriptor_type = USB_DESCTYPE_DEVICE,
|
---|
| 52 | .device_class = USB_CLASS_HUB,
|
---|
| 53 | .device_protocol = 0,
|
---|
| 54 | .device_subclass = 0,
|
---|
| 55 | .device_version = 0,
|
---|
| 56 | .length = sizeof (usb_standard_device_descriptor_t),
|
---|
| 57 | /// \TODO this value is guessed
|
---|
| 58 | .max_packet_size = 8,
|
---|
| 59 | .vendor_id = 0x16db,
|
---|
| 60 | .product_id = 0x0001,
|
---|
| 61 | /// \TODO these values migt be different
|
---|
| 62 | .str_serial_number = 0,
|
---|
| 63 | .usb_spec_version = 0x110,
|
---|
[b3f655f] | 64 | };
|
---|
| 65 |
|
---|
[0368669c] | 66 | /**
|
---|
| 67 | * standart configuration descriptor with filled common values
|
---|
| 68 | * for ohci root hubs
|
---|
| 69 | */
|
---|
[8b74997f] | 70 | static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
|
---|
[b3f655f] | 71 | /// \TODO some values are default or guessed
|
---|
[8b74997f] | 72 | .attributes = 1 << 7,
|
---|
[b3f655f] | 73 | .configuration_number = 1,
|
---|
| 74 | .descriptor_type = USB_DESCTYPE_CONFIGURATION,
|
---|
| 75 | .interface_count = 1,
|
---|
[8b74997f] | 76 | .length = sizeof (usb_standard_configuration_descriptor_t),
|
---|
[b3f655f] | 77 | .max_power = 100,
|
---|
| 78 | .str_configuration = 0,
|
---|
| 79 | };
|
---|
| 80 |
|
---|
[0368669c] | 81 | /**
|
---|
| 82 | * standart ohci root hub interface descriptor
|
---|
| 83 | */
|
---|
[8b74997f] | 84 | static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
|
---|
[b3f655f] | 85 | .alternate_setting = 0,
|
---|
| 86 | .descriptor_type = USB_DESCTYPE_INTERFACE,
|
---|
| 87 | .endpoint_count = 1,
|
---|
| 88 | .interface_class = USB_CLASS_HUB,
|
---|
| 89 | /// \TODO is this correct?
|
---|
| 90 | .interface_number = 1,
|
---|
| 91 | .interface_protocol = 0,
|
---|
| 92 | .interface_subclass = 0,
|
---|
[8b74997f] | 93 | .length = sizeof (usb_standard_interface_descriptor_t),
|
---|
[b3f655f] | 94 | .str_interface = 0,
|
---|
| 95 | };
|
---|
| 96 |
|
---|
[0368669c] | 97 | /**
|
---|
| 98 | * standart ohci root hub endpoint descriptor
|
---|
| 99 | */
|
---|
[8b74997f] | 100 | static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
|
---|
[b3f655f] | 101 | .attributes = USB_TRANSFER_INTERRUPT,
|
---|
| 102 | .descriptor_type = USB_DESCTYPE_ENDPOINT,
|
---|
[8b74997f] | 103 | .endpoint_address = 1 + (1 << 7),
|
---|
| 104 | .length = sizeof (usb_standard_endpoint_descriptor_t),
|
---|
[b3f655f] | 105 | .max_packet_size = 8,
|
---|
| 106 | .poll_interval = 255,
|
---|
| 107 | };
|
---|
[41b96b4] | 108 |
|
---|
[8123695a] | 109 | static const uint32_t hub_clear_feature_valid_mask =
|
---|
[8b74997f] | 110 | (1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER) |
|
---|
| 111 | (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
|
---|
[8123695a] | 112 |
|
---|
| 113 | static const uint32_t hub_clear_feature_by_writing_one_mask =
|
---|
| 114 | 1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER;
|
---|
| 115 |
|
---|
| 116 | static const uint32_t hub_set_feature_valid_mask =
|
---|
| 117 | (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
|
---|
| 118 |
|
---|
[8b74997f] | 119 |
|
---|
[8123695a] | 120 | static const uint32_t hub_set_feature_direct_mask =
|
---|
| 121 | (1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
|
---|
| 122 |
|
---|
| 123 | static const uint32_t port_set_feature_valid_mask =
|
---|
[8b74997f] | 124 | (1 << USB_HUB_FEATURE_PORT_ENABLE) |
|
---|
| 125 | (1 << USB_HUB_FEATURE_PORT_SUSPEND) |
|
---|
| 126 | (1 << USB_HUB_FEATURE_PORT_RESET) |
|
---|
| 127 | (1 << USB_HUB_FEATURE_PORT_POWER);
|
---|
[8123695a] | 128 |
|
---|
| 129 | static const uint32_t port_clear_feature_valid_mask =
|
---|
[8b74997f] | 130 | (1 << USB_HUB_FEATURE_PORT_CONNECTION) |
|
---|
| 131 | (1 << USB_HUB_FEATURE_PORT_SUSPEND) |
|
---|
| 132 | (1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) |
|
---|
| 133 | (1 << USB_HUB_FEATURE_PORT_POWER) |
|
---|
| 134 | (1 << USB_HUB_FEATURE_C_PORT_CONNECTION) |
|
---|
| 135 | (1 << USB_HUB_FEATURE_C_PORT_ENABLE) |
|
---|
| 136 | (1 << USB_HUB_FEATURE_C_PORT_SUSPEND) |
|
---|
| 137 | (1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
|
---|
| 138 | (1 << USB_HUB_FEATURE_C_PORT_RESET);
|
---|
| 139 | //note that USB_HUB_FEATURE_PORT_POWER bit is translated into
|
---|
| 140 | //USB_HUB_FEATURE_PORT_LOW_SPEED
|
---|
| 141 |
|
---|
| 142 | static const uint32_t port_status_change_mask =
|
---|
| 143 | (1<< USB_HUB_FEATURE_C_PORT_CONNECTION) |
|
---|
| 144 | (1<< USB_HUB_FEATURE_C_PORT_ENABLE) |
|
---|
| 145 | (1<< USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
|
---|
| 146 | (1<< USB_HUB_FEATURE_C_PORT_RESET) |
|
---|
| 147 | (1<< USB_HUB_FEATURE_C_PORT_SUSPEND);
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | static void usb_create_serialized_hub_descriptor(rh_t *instance,
|
---|
| 151 | uint8_t ** out_result,
|
---|
| 152 | size_t * out_size);
|
---|
| 153 |
|
---|
| 154 | static void rh_init_descriptors(rh_t *instance);
|
---|
| 155 |
|
---|
| 156 | static int process_get_port_status_request(rh_t *instance, uint16_t port,
|
---|
| 157 | usb_transfer_batch_t * request);
|
---|
| 158 |
|
---|
| 159 | static int process_get_hub_status_request(rh_t *instance,
|
---|
| 160 | usb_transfer_batch_t * request);
|
---|
| 161 |
|
---|
| 162 | static int process_get_status_request(rh_t *instance,
|
---|
| 163 | usb_transfer_batch_t * request);
|
---|
| 164 |
|
---|
| 165 | static void create_interrupt_mask(rh_t *instance, void ** buffer,
|
---|
| 166 | size_t * buffer_size);
|
---|
| 167 |
|
---|
| 168 | static int process_get_descriptor_request(rh_t *instance,
|
---|
| 169 | usb_transfer_batch_t *request);
|
---|
| 170 |
|
---|
| 171 | static int process_get_configuration_request(rh_t *instance,
|
---|
| 172 | usb_transfer_batch_t *request);
|
---|
| 173 |
|
---|
| 174 | static int process_hub_feature_set_request(rh_t *instance, uint16_t feature);
|
---|
| 175 |
|
---|
| 176 | static int process_hub_feature_clear_request(rh_t *instance,
|
---|
| 177 | uint16_t feature);
|
---|
| 178 |
|
---|
| 179 | static int process_port_feature_set_request(rh_t *instance,
|
---|
| 180 | uint16_t feature, uint16_t port);
|
---|
| 181 |
|
---|
| 182 | static int process_port_feature_clear_request(rh_t *instance,
|
---|
| 183 | uint16_t feature, uint16_t port);
|
---|
| 184 |
|
---|
| 185 | static int process_address_set_request(rh_t *instance,
|
---|
| 186 | uint16_t address);
|
---|
| 187 |
|
---|
| 188 | static int process_request_with_output(rh_t *instance,
|
---|
| 189 | usb_transfer_batch_t *request);
|
---|
| 190 |
|
---|
| 191 | static int process_request_with_input(rh_t *instance,
|
---|
| 192 | usb_transfer_batch_t *request);
|
---|
| 193 |
|
---|
| 194 | static int process_request_without_data(rh_t *instance,
|
---|
| 195 | usb_transfer_batch_t *request);
|
---|
[8123695a] | 196 |
|
---|
[8b74997f] | 197 | static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
|
---|
[8123695a] | 198 |
|
---|
| 199 |
|
---|
| 200 |
|
---|
[8b74997f] | 201 |
|
---|
| 202 |
|
---|
| 203 | /** Root hub initialization
|
---|
| 204 | * @return Error code.
|
---|
| 205 | */
|
---|
| 206 | int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs) {
|
---|
| 207 | assert(instance);
|
---|
| 208 | //instance->address = -1;
|
---|
| 209 | instance->registers = regs;
|
---|
| 210 | instance->device = dev;
|
---|
[112d159] | 211 | instance->port_count =
|
---|
| 212 | (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
|
---|
[8b74997f] | 213 | rh_init_descriptors(instance);
|
---|
| 214 | // set port power mode to no-power-switching
|
---|
| 215 | instance->registers->rh_desc_a =
|
---|
| 216 | instance->registers->rh_desc_a | (1<<9);
|
---|
| 217 |
|
---|
| 218 | usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
|
---|
| 219 |
|
---|
| 220 | //start generic usb hub driver
|
---|
| 221 |
|
---|
| 222 | /* TODO: implement */
|
---|
| 223 | return EOK;
|
---|
| 224 | }
|
---|
| 225 | /*----------------------------------------------------------------------------*/
|
---|
| 226 |
|
---|
| 227 | /**
|
---|
| 228 | * process root hub request
|
---|
| 229 | *
|
---|
| 230 | * @param instance root hub instance
|
---|
| 231 | * @param request structure containing both request and response information
|
---|
| 232 | * @return error code
|
---|
| 233 | */
|
---|
| 234 | int rh_request(rh_t *instance, usb_transfer_batch_t *request) {
|
---|
| 235 | assert(instance);
|
---|
| 236 | assert(request);
|
---|
| 237 | int opResult;
|
---|
| 238 | if (request->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 239 | usb_log_info("Root hub got CONTROL packet\n");
|
---|
| 240 | opResult = process_ctrl_request(instance, request);
|
---|
| 241 | } else if (request->transfer_type == USB_TRANSFER_INTERRUPT) {
|
---|
| 242 | usb_log_info("Root hub got INTERRUPT packet\n");
|
---|
| 243 | void * buffer;
|
---|
| 244 | create_interrupt_mask(instance, &buffer,
|
---|
| 245 | &(request->transfered_size));
|
---|
| 246 | memcpy(request->transport_buffer, buffer,
|
---|
| 247 | request->transfered_size);
|
---|
| 248 | opResult = EOK;
|
---|
| 249 | } else {
|
---|
| 250 | opResult = EINVAL;
|
---|
| 251 | }
|
---|
[4b39af4] | 252 | usb_transfer_batch_finish_error(request, opResult);
|
---|
[8b74997f] | 253 | return EOK;
|
---|
| 254 | }
|
---|
[8123695a] | 255 |
|
---|
[8b74997f] | 256 | /*----------------------------------------------------------------------------*/
|
---|
[8123695a] | 257 |
|
---|
| 258 |
|
---|
[8b74997f] | 259 | void rh_interrupt(rh_t *instance) {
|
---|
| 260 | usb_log_info("Whoa whoa wait, I`m not supposed to receive any "
|
---|
| 261 | "interrupts, am I?\n");
|
---|
| 262 | /* TODO: implement? */
|
---|
| 263 | }
|
---|
| 264 | /*----------------------------------------------------------------------------*/
|
---|
[8123695a] | 265 |
|
---|
[66a54cc] | 266 | /**
|
---|
| 267 | * Create hub descriptor used in hub-driver <-> hub communication
|
---|
| 268 | *
|
---|
| 269 | * This means creating byt array from data in root hub registers. For more
|
---|
| 270 | * info see usb hub specification.
|
---|
| 271 | *
|
---|
| 272 | * @param instance root hub instance
|
---|
| 273 | * @param@out out_result pointer to resultant serialized descriptor
|
---|
| 274 | * @param@out out_size size of serialized descriptor
|
---|
| 275 | */
|
---|
| 276 | static void usb_create_serialized_hub_descriptor(rh_t *instance,
|
---|
[8b74997f] | 277 | uint8_t ** out_result,
|
---|
| 278 | size_t * out_size) {
|
---|
[66a54cc] | 279 | //base size
|
---|
| 280 | size_t size = 7;
|
---|
| 281 | //variable size according to port count
|
---|
| 282 | size_t var_size = instance->port_count / 8 +
|
---|
[8b74997f] | 283 | ((instance->port_count % 8 > 0) ? 1 : 0);
|
---|
[66a54cc] | 284 | size += 2 * var_size;
|
---|
| 285 | uint8_t * result = (uint8_t*) malloc(size);
|
---|
[8b74997f] | 286 | bzero(result, size);
|
---|
[66a54cc] | 287 | //size
|
---|
| 288 | result[0] = size;
|
---|
| 289 | //descriptor type
|
---|
| 290 | result[1] = USB_DESCTYPE_HUB;
|
---|
| 291 | result[2] = instance->port_count;
|
---|
| 292 | uint32_t hub_desc_reg = instance->registers->rh_desc_a;
|
---|
| 293 | result[3] =
|
---|
[8b74997f] | 294 | ((hub_desc_reg >> 8) % 2) +
|
---|
| 295 | (((hub_desc_reg >> 9) % 2) << 1) +
|
---|
| 296 | (((hub_desc_reg >> 10) % 2) << 2) +
|
---|
| 297 | (((hub_desc_reg >> 11) % 2) << 3) +
|
---|
| 298 | (((hub_desc_reg >> 12) % 2) << 4);
|
---|
[66a54cc] | 299 | result[4] = 0;
|
---|
| 300 | result[5] = /*descriptor->pwr_on_2_good_time*/ 50;
|
---|
| 301 | result[6] = 50;
|
---|
| 302 |
|
---|
| 303 | int port;
|
---|
| 304 | for (port = 1; port <= instance->port_count; ++port) {
|
---|
[8b74997f] | 305 | uint8_t is_non_removable =
|
---|
| 306 | instance->registers->rh_desc_b >> port % 2;
|
---|
| 307 | result[7 + port / 8] +=
|
---|
| 308 | is_non_removable << (port % 8);
|
---|
[66a54cc] | 309 | }
|
---|
| 310 | size_t i;
|
---|
| 311 | for (i = 0; i < var_size; ++i) {
|
---|
| 312 | result[7 + var_size + i] = 255;
|
---|
| 313 | }
|
---|
| 314 | (*out_result) = result;
|
---|
| 315 | (*out_size) = size;
|
---|
| 316 | }
|
---|
[8b74997f] | 317 | /*----------------------------------------------------------------------------*/
|
---|
[66a54cc] | 318 |
|
---|
| 319 | /** initialize hub descriptors
|
---|
| 320 | *
|
---|
| 321 | * Initialized are device and full configuration descriptor. These need to
|
---|
| 322 | * be initialized only once per hub.
|
---|
| 323 | * @instance root hub instance
|
---|
| 324 | */
|
---|
[8b74997f] | 325 | static void rh_init_descriptors(rh_t *instance) {
|
---|
[66a54cc] | 326 | memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor,
|
---|
[8b74997f] | 327 | sizeof (ohci_rh_device_descriptor)
|
---|
| 328 | );
|
---|
[66a54cc] | 329 | usb_standard_configuration_descriptor_t descriptor;
|
---|
[8b74997f] | 330 | memcpy(&descriptor, &ohci_rh_conf_descriptor,
|
---|
| 331 | sizeof (ohci_rh_conf_descriptor));
|
---|
[66a54cc] | 332 | uint8_t * hub_descriptor;
|
---|
| 333 | size_t hub_desc_size;
|
---|
| 334 | usb_create_serialized_hub_descriptor(instance, &hub_descriptor,
|
---|
[8b74997f] | 335 | &hub_desc_size);
|
---|
[66a54cc] | 336 |
|
---|
| 337 | descriptor.total_length =
|
---|
[8b74997f] | 338 | sizeof (usb_standard_configuration_descriptor_t) +
|
---|
| 339 | sizeof (usb_standard_endpoint_descriptor_t) +
|
---|
| 340 | sizeof (usb_standard_interface_descriptor_t) +
|
---|
| 341 | hub_desc_size;
|
---|
| 342 |
|
---|
[66a54cc] | 343 | uint8_t * full_config_descriptor =
|
---|
[8b74997f] | 344 | (uint8_t*) malloc(descriptor.total_length);
|
---|
| 345 | memcpy(full_config_descriptor, &descriptor, sizeof (descriptor));
|
---|
| 346 | memcpy(full_config_descriptor + sizeof (descriptor),
|
---|
| 347 | &ohci_rh_iface_descriptor, sizeof (ohci_rh_iface_descriptor));
|
---|
| 348 | memcpy(full_config_descriptor + sizeof (descriptor) +
|
---|
| 349 | sizeof (ohci_rh_iface_descriptor),
|
---|
| 350 | &ohci_rh_ep_descriptor, sizeof (ohci_rh_ep_descriptor));
|
---|
| 351 | memcpy(full_config_descriptor + sizeof (descriptor) +
|
---|
| 352 | sizeof (ohci_rh_iface_descriptor) +
|
---|
| 353 | sizeof (ohci_rh_ep_descriptor),
|
---|
| 354 | hub_descriptor, hub_desc_size);
|
---|
| 355 |
|
---|
[66a54cc] | 356 | instance->descriptors.configuration = full_config_descriptor;
|
---|
| 357 | instance->descriptors.configuration_size = descriptor.total_length;
|
---|
| 358 | }
|
---|
[41b96b4] | 359 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 360 |
|
---|
[f3da9b2] | 361 | /**
|
---|
| 362 | * create answer to port status_request
|
---|
| 363 | *
|
---|
| 364 | * Copy content of corresponding port status register to answer buffer.
|
---|
| 365 | *
|
---|
| 366 | * @param instance root hub instance
|
---|
| 367 | * @param port port number, counted from 1
|
---|
| 368 | * @param request structure containing both request and response information
|
---|
| 369 | * @return error code
|
---|
| 370 | */
|
---|
[4d0c40b] | 371 | static int process_get_port_status_request(rh_t *instance, uint16_t port,
|
---|
[8b74997f] | 372 | usb_transfer_batch_t * request) {
|
---|
| 373 | if (port < 1 || port > instance->port_count)
|
---|
[4d0c40b] | 374 | return EINVAL;
|
---|
[8b74997f] | 375 | uint32_t * uint32_buffer = (uint32_t*) request->transport_buffer;
|
---|
[d8421c4] | 376 | request->transfered_size = 4;
|
---|
[8b74997f] | 377 | uint32_buffer[0] = instance->registers->rh_port_status[port - 1];
|
---|
| 378 | #if 0
|
---|
| 379 | int i;
|
---|
| 380 | for (i = 0; i < instance->port_count; ++i) {
|
---|
| 381 | usb_log_debug("port status %d,x%x\n",
|
---|
| 382 | instance->registers->rh_port_status[i],
|
---|
| 383 | instance->registers->rh_port_status[i]);
|
---|
| 384 | }
|
---|
| 385 | #endif
|
---|
[4d0c40b] | 386 | return EOK;
|
---|
| 387 | }
|
---|
[8b74997f] | 388 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 389 |
|
---|
[f3da9b2] | 390 | /**
|
---|
| 391 | * create answer to port status_request
|
---|
| 392 | *
|
---|
| 393 | * Copy content of hub status register to answer buffer.
|
---|
| 394 | *
|
---|
| 395 | * @param instance root hub instance
|
---|
| 396 | * @param request structure containing both request and response information
|
---|
| 397 | * @return error code
|
---|
| 398 | */
|
---|
[d8421c4] | 399 | static int process_get_hub_status_request(rh_t *instance,
|
---|
[8b74997f] | 400 | usb_transfer_batch_t * request) {
|
---|
| 401 | uint32_t * uint32_buffer = (uint32_t*) request->transport_buffer;
|
---|
[d8421c4] | 402 | request->transfered_size = 4;
|
---|
[8b74997f] | 403 | //bits, 0,1,16,17
|
---|
| 404 | uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
|
---|
[4d0c40b] | 405 | uint32_buffer[0] = mask & instance->registers->rh_status;
|
---|
| 406 | return EOK;
|
---|
| 407 | }
|
---|
[8b74997f] | 408 | /*----------------------------------------------------------------------------*/
|
---|
[d8421c4] | 409 |
|
---|
[f3da9b2] | 410 | /**
|
---|
| 411 | * create answer to status request
|
---|
| 412 | *
|
---|
| 413 | * This might be either hub status or port status request. If neither,
|
---|
| 414 | * ENOTSUP is returned.
|
---|
| 415 | * @param instance root hub instance
|
---|
| 416 | * @param request structure containing both request and response information
|
---|
| 417 | * @return error code
|
---|
| 418 | */
|
---|
[d8421c4] | 419 | static int process_get_status_request(rh_t *instance,
|
---|
[8b74997f] | 420 | usb_transfer_batch_t * request) {
|
---|
[d8421c4] | 421 | size_t buffer_size = request->buffer_size;
|
---|
| 422 | usb_device_request_setup_packet_t * request_packet =
|
---|
[8b74997f] | 423 | (usb_device_request_setup_packet_t*)
|
---|
| 424 | request->setup_buffer;
|
---|
[d8421c4] | 425 |
|
---|
| 426 | usb_hub_bm_request_type_t request_type = request_packet->request_type;
|
---|
[8b74997f] | 427 | if (buffer_size < 4/*request_packet->length*/) {///\TODO
|
---|
[d8421c4] | 428 | usb_log_warning("requested more data than buffer size\n");
|
---|
| 429 | return EINVAL;
|
---|
| 430 | }
|
---|
[4d0c40b] | 431 |
|
---|
[8b74997f] | 432 | if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
|
---|
[d8421c4] | 433 | return process_get_hub_status_request(instance, request);
|
---|
[8b74997f] | 434 | if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
|
---|
| 435 | return process_get_port_status_request(instance,
|
---|
| 436 | request_packet->index,
|
---|
| 437 | request);
|
---|
[4d0c40b] | 438 | return ENOTSUP;
|
---|
| 439 | }
|
---|
[8b74997f] | 440 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 441 |
|
---|
[f3da9b2] | 442 | /**
|
---|
| 443 | * create answer to status interrupt consisting of change bitmap
|
---|
| 444 | *
|
---|
| 445 | * Result contains bitmap where bit 0 indicates change on hub and
|
---|
| 446 | * bit i indicates change on i`th port (i>0). For more info see
|
---|
[8b74997f] | 447 | * Hub and Port status bitmap specification in USB specification
|
---|
| 448 | * (chapter 11.13.4)
|
---|
[f3da9b2] | 449 | * @param instance root hub instance
|
---|
| 450 | * @param@out buffer pointer to created interrupt mas
|
---|
| 451 | * @param@out buffer_size size of created interrupt mask
|
---|
| 452 | */
|
---|
[d8421c4] | 453 | static void create_interrupt_mask(rh_t *instance, void ** buffer,
|
---|
[8b74997f] | 454 | size_t * buffer_size) {
|
---|
[d8421c4] | 455 | int bit_count = instance->port_count + 1;
|
---|
[8b74997f] | 456 | (*buffer_size) = (bit_count / 8) + ((bit_count % 8 == 0) ? 0 : 1);
|
---|
| 457 |
|
---|
[d8421c4] | 458 | (*buffer) = malloc(*buffer_size);
|
---|
[8b74997f] | 459 | uint8_t * bitmap = (uint8_t*) (*buffer);
|
---|
| 460 | uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
|
---|
| 461 | | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
|
---|
| 462 | bzero(bitmap, (*buffer_size));
|
---|
| 463 | if (instance->registers->rh_status & mask) {
|
---|
[d8421c4] | 464 | bitmap[0] = 1;
|
---|
| 465 | }
|
---|
| 466 | int port;
|
---|
[8b74997f] | 467 | mask = port_status_change_mask;
|
---|
| 468 | for (port = 1; port <= instance->port_count; ++port) {
|
---|
| 469 | if (mask & instance->registers->rh_port_status[port - 1]) {
|
---|
| 470 | bitmap[(port) / 8] += 1 << (port % 8);
|
---|
[d8421c4] | 471 | }
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
[8b74997f] | 474 | /*----------------------------------------------------------------------------*/
|
---|
| 475 |
|
---|
[f3da9b2] | 476 | /**
|
---|
| 477 | * create answer to a descriptor request
|
---|
| 478 | *
|
---|
| 479 | * This might be a request for standard (configuration, device, endpoint or
|
---|
| 480 | * interface) or device specific (hub) descriptor.
|
---|
| 481 | * @param instance root hub instance
|
---|
| 482 | * @param request structure containing both request and response information
|
---|
| 483 | * @return error code
|
---|
| 484 | */
|
---|
[4d0c40b] | 485 | static int process_get_descriptor_request(rh_t *instance,
|
---|
[8b74997f] | 486 | usb_transfer_batch_t *request) {
|
---|
[4d0c40b] | 487 | usb_device_request_setup_packet_t * setup_request =
|
---|
[8b74997f] | 488 | (usb_device_request_setup_packet_t*) request->setup_buffer;
|
---|
[d8421c4] | 489 | size_t size;
|
---|
[aee6c73] | 490 | const void * result_descriptor = NULL;
|
---|
[b3f655f] | 491 | const uint16_t setup_request_value = setup_request->value_high;
|
---|
[8b74997f] | 492 | //(setup_request->value_low << 8);
|
---|
[b3f655f] | 493 | bool del = false;
|
---|
[8b74997f] | 494 | switch (setup_request_value) {
|
---|
| 495 | case USB_DESCTYPE_HUB:
|
---|
| 496 | {
|
---|
[0368669c] | 497 | uint8_t * descriptor;
|
---|
| 498 | usb_create_serialized_hub_descriptor(
|
---|
| 499 | instance, &descriptor, &size);
|
---|
| 500 | result_descriptor = descriptor;
|
---|
[8b74997f] | 501 | if (result_descriptor) del = true;
|
---|
[0368669c] | 502 | break;
|
---|
| 503 | }
|
---|
[8b74997f] | 504 | case USB_DESCTYPE_DEVICE:
|
---|
| 505 | {
|
---|
[0368669c] | 506 | usb_log_debug("USB_DESCTYPE_DEVICE\n");
|
---|
| 507 | result_descriptor = &ohci_rh_device_descriptor;
|
---|
[8b74997f] | 508 | size = sizeof (ohci_rh_device_descriptor);
|
---|
[0368669c] | 509 | break;
|
---|
| 510 | }
|
---|
[8b74997f] | 511 | case USB_DESCTYPE_CONFIGURATION:
|
---|
| 512 | {
|
---|
[0368669c] | 513 | usb_log_debug("USB_DESCTYPE_CONFIGURATION\n");
|
---|
[66a54cc] | 514 | result_descriptor = instance->descriptors.configuration;
|
---|
| 515 | size = instance->descriptors.configuration_size;
|
---|
[0368669c] | 516 | break;
|
---|
| 517 | }
|
---|
[8b74997f] | 518 | case USB_DESCTYPE_INTERFACE:
|
---|
| 519 | {
|
---|
[0368669c] | 520 | usb_log_debug("USB_DESCTYPE_INTERFACE\n");
|
---|
| 521 | result_descriptor = &ohci_rh_iface_descriptor;
|
---|
[8b74997f] | 522 | size = sizeof (ohci_rh_iface_descriptor);
|
---|
[0368669c] | 523 | break;
|
---|
| 524 | }
|
---|
[8b74997f] | 525 | case USB_DESCTYPE_ENDPOINT:
|
---|
| 526 | {
|
---|
[0368669c] | 527 | usb_log_debug("USB_DESCTYPE_ENDPOINT\n");
|
---|
| 528 | result_descriptor = &ohci_rh_ep_descriptor;
|
---|
[8b74997f] | 529 | size = sizeof (ohci_rh_ep_descriptor);
|
---|
[0368669c] | 530 | break;
|
---|
| 531 | }
|
---|
[8b74997f] | 532 | default:
|
---|
| 533 | {
|
---|
| 534 | usb_log_debug("USB_DESCTYPE_EINVAL %d \n",
|
---|
| 535 | setup_request->value);
|
---|
| 536 | usb_log_debug("\ttype %d\n\trequest %d\n\tvalue "
|
---|
| 537 | "%d\n\tindex %d\n\tlen %d\n ",
|
---|
| 538 | setup_request->request_type,
|
---|
| 539 | setup_request->request,
|
---|
| 540 | setup_request_value,
|
---|
| 541 | setup_request->index,
|
---|
| 542 | setup_request->length
|
---|
| 543 | );
|
---|
[0368669c] | 544 | return EINVAL;
|
---|
| 545 | }
|
---|
[4d0c40b] | 546 | }
|
---|
[8b74997f] | 547 | if (request->buffer_size < size) {
|
---|
[d8421c4] | 548 | size = request->buffer_size;
|
---|
| 549 | }
|
---|
| 550 | request->transfered_size = size;
|
---|
[8b74997f] | 551 | memcpy(request->transport_buffer, result_descriptor, size);
|
---|
[0368669c] | 552 | if (del)
|
---|
[b3f655f] | 553 | free(result_descriptor);
|
---|
[4d0c40b] | 554 | return EOK;
|
---|
| 555 | }
|
---|
[8b74997f] | 556 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 557 |
|
---|
[f3da9b2] | 558 | /**
|
---|
| 559 | * answer to get configuration request
|
---|
| 560 | *
|
---|
| 561 | * Root hub works independently on the configuration.
|
---|
| 562 | * @param instance root hub instance
|
---|
| 563 | * @param request structure containing both request and response information
|
---|
| 564 | * @return error code
|
---|
| 565 | */
|
---|
[8b74997f] | 566 | static int process_get_configuration_request(rh_t *instance,
|
---|
| 567 | usb_transfer_batch_t *request) {
|
---|
[4d0c40b] | 568 | //set and get configuration requests do not have any meaning, only dummy
|
---|
| 569 | //values are returned
|
---|
[8b74997f] | 570 | if (request->buffer_size != 1)
|
---|
[4d0c40b] | 571 | return EINVAL;
|
---|
[ccbcd895] | 572 | request->transport_buffer[0] = 1;
|
---|
[d8421c4] | 573 | request->transfered_size = 1;
|
---|
[4d0c40b] | 574 | return EOK;
|
---|
| 575 | }
|
---|
[8b74997f] | 576 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 577 |
|
---|
[f3da9b2] | 578 | /**
|
---|
[8123695a] | 579 | * process feature-enabling request on hub
|
---|
[8b74997f] | 580 | *
|
---|
[f3da9b2] | 581 | * @param instance root hub instance
|
---|
| 582 | * @param feature feature selector
|
---|
| 583 | * @return error code
|
---|
| 584 | */
|
---|
[4d0c40b] | 585 | static int process_hub_feature_set_request(rh_t *instance,
|
---|
[8b74997f] | 586 | uint16_t feature) {
|
---|
| 587 | if (!((1 << feature) & hub_set_feature_valid_mask))
|
---|
[4d0c40b] | 588 | return EINVAL;
|
---|
| 589 | instance->registers->rh_status =
|
---|
[8b74997f] | 590 | (instance->registers->rh_status | (1 << feature))
|
---|
| 591 | & (~hub_clear_feature_by_writing_one_mask);
|
---|
[4d0c40b] | 592 | return EOK;
|
---|
| 593 | }
|
---|
[8b74997f] | 594 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 595 |
|
---|
[f3da9b2] | 596 | /**
|
---|
[8123695a] | 597 | * process feature-disabling request on hub
|
---|
| 598 | *
|
---|
| 599 | * @param instance root hub instance
|
---|
| 600 | * @param feature feature selector
|
---|
| 601 | * @return error code
|
---|
| 602 | */
|
---|
| 603 | static int process_hub_feature_clear_request(rh_t *instance,
|
---|
[8b74997f] | 604 | uint16_t feature) {
|
---|
| 605 | if (!((1 << feature) & hub_clear_feature_valid_mask))
|
---|
[8123695a] | 606 | return EINVAL;
|
---|
| 607 | //is the feature cleared directly?
|
---|
[8b74997f] | 608 | if ((1 << feature) & hub_set_feature_direct_mask) {
|
---|
[8123695a] | 609 | instance->registers->rh_status =
|
---|
[8b74997f] | 610 | (instance->registers->rh_status & (~(1 << feature)))
|
---|
| 611 | & (~hub_clear_feature_by_writing_one_mask);
|
---|
| 612 | } else {//the feature is cleared by writing '1'
|
---|
[8123695a] | 613 | instance->registers->rh_status =
|
---|
[8b74997f] | 614 | (instance->registers->rh_status
|
---|
| 615 | & (~hub_clear_feature_by_writing_one_mask))
|
---|
| 616 | | (1 << feature);
|
---|
[8123695a] | 617 | }
|
---|
| 618 | return EOK;
|
---|
| 619 | }
|
---|
[8b74997f] | 620 | /*----------------------------------------------------------------------------*/
|
---|
[8123695a] | 621 |
|
---|
| 622 | /**
|
---|
| 623 | * process feature-enabling request on hub
|
---|
[8b74997f] | 624 | *
|
---|
[f3da9b2] | 625 | * @param instance root hub instance
|
---|
| 626 | * @param feature feature selector
|
---|
| 627 | * @param port port number, counted from 1
|
---|
| 628 | * @param enable enable or disable the specified feature
|
---|
| 629 | * @return error code
|
---|
| 630 | */
|
---|
[4d0c40b] | 631 | static int process_port_feature_set_request(rh_t *instance,
|
---|
[8b74997f] | 632 | uint16_t feature, uint16_t port) {
|
---|
| 633 | if (!((1 << feature) & port_set_feature_valid_mask))
|
---|
[4d0c40b] | 634 | return EINVAL;
|
---|
[8b74997f] | 635 | if (port < 1 || port > instance->port_count)
|
---|
[4d0c40b] | 636 | return EINVAL;
|
---|
| 637 | instance->registers->rh_port_status[port - 1] =
|
---|
[8b74997f] | 638 | (instance->registers->rh_port_status[port - 1] | (1 << feature))
|
---|
| 639 | & (~port_clear_feature_valid_mask);
|
---|
[8123695a] | 640 | /// \TODO any error?
|
---|
| 641 | return EOK;
|
---|
| 642 | }
|
---|
[8b74997f] | 643 | /*----------------------------------------------------------------------------*/
|
---|
[8123695a] | 644 |
|
---|
| 645 | /**
|
---|
| 646 | * process feature-disabling request on hub
|
---|
| 647 | *
|
---|
| 648 | * @param instance root hub instance
|
---|
| 649 | * @param feature feature selector
|
---|
| 650 | * @param port port number, counted from 1
|
---|
| 651 | * @param enable enable or disable the specified feature
|
---|
| 652 | * @return error code
|
---|
| 653 | */
|
---|
| 654 | static int process_port_feature_clear_request(rh_t *instance,
|
---|
[8b74997f] | 655 | uint16_t feature, uint16_t port) {
|
---|
| 656 | if (!((1 << feature) & port_clear_feature_valid_mask))
|
---|
[8123695a] | 657 | return EINVAL;
|
---|
[8b74997f] | 658 | if (port < 1 || port > instance->port_count)
|
---|
[8123695a] | 659 | return EINVAL;
|
---|
[8b74997f] | 660 | if (feature == USB_HUB_FEATURE_PORT_POWER)
|
---|
[8123695a] | 661 | feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
|
---|
[8b74997f] | 662 | if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
|
---|
[8123695a] | 663 | feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
|
---|
| 664 | instance->registers->rh_port_status[port - 1] =
|
---|
[8b74997f] | 665 | (instance->registers->rh_port_status[port - 1]
|
---|
| 666 | & (~port_clear_feature_valid_mask))
|
---|
| 667 | | (1 << feature);
|
---|
[4d0c40b] | 668 | /// \TODO any error?
|
---|
| 669 | return EOK;
|
---|
[d8421c4] | 670 | }
|
---|
[8b74997f] | 671 | /*----------------------------------------------------------------------------*/
|
---|
[8123695a] | 672 |
|
---|
[f3da9b2] | 673 | /**
|
---|
| 674 | * register address to this device
|
---|
[8b74997f] | 675 | *
|
---|
[f3da9b2] | 676 | * @param instance root hub instance
|
---|
| 677 | * @param address new address
|
---|
| 678 | * @return error code
|
---|
| 679 | */
|
---|
[d8421c4] | 680 | static int process_address_set_request(rh_t *instance,
|
---|
[8b74997f] | 681 | uint16_t address) {
|
---|
[d8421c4] | 682 | instance->address = address;
|
---|
| 683 | return EOK;
|
---|
[4d0c40b] | 684 | }
|
---|
[8b74997f] | 685 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 686 |
|
---|
[f3da9b2] | 687 | /**
|
---|
| 688 | * process one of requests that requere output data
|
---|
| 689 | *
|
---|
| 690 | * Request can be one of USB_DEVREQ_GET_STATUS, USB_DEVREQ_GET_DESCRIPTOR or
|
---|
| 691 | * USB_DEVREQ_GET_CONFIGURATION.
|
---|
| 692 | * @param instance root hub instance
|
---|
| 693 | * @param request structure containing both request and response information
|
---|
| 694 | * @return error code
|
---|
| 695 | */
|
---|
[4d0c40b] | 696 | static int process_request_with_output(rh_t *instance,
|
---|
[8b74997f] | 697 | usb_transfer_batch_t *request) {
|
---|
[4d0c40b] | 698 | usb_device_request_setup_packet_t * setup_request =
|
---|
[8b74997f] | 699 | (usb_device_request_setup_packet_t*) request->setup_buffer;
|
---|
| 700 | if (setup_request->request == USB_DEVREQ_GET_STATUS) {
|
---|
[d8421c4] | 701 | usb_log_debug("USB_DEVREQ_GET_STATUS\n");
|
---|
| 702 | return process_get_status_request(instance, request);
|
---|
[4d0c40b] | 703 | }
|
---|
[8b74997f] | 704 | if (setup_request->request == USB_DEVREQ_GET_DESCRIPTOR) {
|
---|
[d8421c4] | 705 | usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
|
---|
[4d0c40b] | 706 | return process_get_descriptor_request(instance, request);
|
---|
| 707 | }
|
---|
[8b74997f] | 708 | if (setup_request->request == USB_DEVREQ_GET_CONFIGURATION) {
|
---|
[d8421c4] | 709 | usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
|
---|
| 710 | return process_get_configuration_request(instance, request);
|
---|
[4d0c40b] | 711 | }
|
---|
| 712 | return ENOTSUP;
|
---|
| 713 | }
|
---|
[8b74997f] | 714 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 715 |
|
---|
[f3da9b2] | 716 | /**
|
---|
| 717 | * process one of requests that carry input data
|
---|
| 718 | *
|
---|
| 719 | * Request can be one of USB_DEVREQ_SET_DESCRIPTOR or
|
---|
| 720 | * USB_DEVREQ_SET_CONFIGURATION.
|
---|
| 721 | * @param instance root hub instance
|
---|
| 722 | * @param request structure containing both request and response information
|
---|
| 723 | * @return error code
|
---|
| 724 | */
|
---|
[4d0c40b] | 725 | static int process_request_with_input(rh_t *instance,
|
---|
[8b74997f] | 726 | usb_transfer_batch_t *request) {
|
---|
[4d0c40b] | 727 | usb_device_request_setup_packet_t * setup_request =
|
---|
[8b74997f] | 728 | (usb_device_request_setup_packet_t*) request->setup_buffer;
|
---|
[d8421c4] | 729 | request->transfered_size = 0;
|
---|
[8b74997f] | 730 | if (setup_request->request == USB_DEVREQ_SET_DESCRIPTOR) {
|
---|
[4d0c40b] | 731 | return ENOTSUP;
|
---|
| 732 | }
|
---|
[8b74997f] | 733 | if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
|
---|
[4d0c40b] | 734 | //set and get configuration requests do not have any meaning,
|
---|
| 735 | //only dummy values are returned
|
---|
| 736 | return EOK;
|
---|
| 737 | }
|
---|
| 738 | return ENOTSUP;
|
---|
| 739 | }
|
---|
[8b74997f] | 740 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 741 |
|
---|
[f3da9b2] | 742 | /**
|
---|
| 743 | * process one of requests that do not request nor carry additional data
|
---|
| 744 | *
|
---|
| 745 | * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
|
---|
| 746 | * USB_DEVREQ_SET_ADDRESS.
|
---|
| 747 | * @param instance root hub instance
|
---|
| 748 | * @param request structure containing both request and response information
|
---|
| 749 | * @return error code
|
---|
| 750 | */
|
---|
[4d0c40b] | 751 | static int process_request_without_data(rh_t *instance,
|
---|
[8b74997f] | 752 | usb_transfer_batch_t *request) {
|
---|
[4d0c40b] | 753 | usb_device_request_setup_packet_t * setup_request =
|
---|
[8b74997f] | 754 | (usb_device_request_setup_packet_t*) request->setup_buffer;
|
---|
[d8421c4] | 755 | request->transfered_size = 0;
|
---|
[8b74997f] | 756 | if (setup_request->request == USB_DEVREQ_CLEAR_FEATURE) {
|
---|
| 757 | if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
|
---|
[d8421c4] | 758 | usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
|
---|
[8123695a] | 759 | return process_hub_feature_clear_request(instance,
|
---|
[8b74997f] | 760 | setup_request->value);
|
---|
[d8421c4] | 761 | }
|
---|
[8b74997f] | 762 | if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
|
---|
[d8421c4] | 763 | usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
|
---|
[8123695a] | 764 | return process_port_feature_clear_request(instance,
|
---|
[8b74997f] | 765 | setup_request->value,
|
---|
| 766 | setup_request->index);
|
---|
[8123695a] | 767 | }
|
---|
| 768 | usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
|
---|
[8b74997f] | 769 | setup_request->request_type);
|
---|
[8123695a] | 770 | return EINVAL;
|
---|
| 771 | }
|
---|
[8b74997f] | 772 | if (setup_request->request == USB_DEVREQ_SET_FEATURE) {
|
---|
| 773 | if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
|
---|
[8123695a] | 774 | usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
|
---|
| 775 | return process_hub_feature_set_request(instance,
|
---|
[8b74997f] | 776 | setup_request->value);
|
---|
[8123695a] | 777 | }
|
---|
[8b74997f] | 778 | if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
|
---|
[8123695a] | 779 | usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
|
---|
| 780 | return process_port_feature_set_request(instance,
|
---|
[8b74997f] | 781 | setup_request->value,
|
---|
| 782 | setup_request->index);
|
---|
[d8421c4] | 783 | }
|
---|
[8b74997f] | 784 | usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
|
---|
| 785 | setup_request->request_type);
|
---|
[d8421c4] | 786 | return EINVAL;
|
---|
[4d0c40b] | 787 | }
|
---|
[8b74997f] | 788 | if (setup_request->request == USB_DEVREQ_SET_ADDRESS) {
|
---|
[d8421c4] | 789 | usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
|
---|
[8b74997f] | 790 | return process_address_set_request(instance,
|
---|
| 791 | setup_request->value);
|
---|
[4d0c40b] | 792 | }
|
---|
[8b74997f] | 793 | usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",
|
---|
| 794 | setup_request->request_type);
|
---|
[4d0c40b] | 795 | return ENOTSUP;
|
---|
| 796 | }
|
---|
[8b74997f] | 797 | /*----------------------------------------------------------------------------*/
|
---|
[4d0c40b] | 798 |
|
---|
[f3da9b2] | 799 | /**
|
---|
| 800 | * process hub control request
|
---|
| 801 | *
|
---|
| 802 | * If needed, writes answer into the request structure.
|
---|
| 803 | * Request can be one of
|
---|
| 804 | * USB_DEVREQ_GET_STATUS,
|
---|
| 805 | * USB_DEVREQ_GET_DESCRIPTOR,
|
---|
| 806 | * USB_DEVREQ_GET_CONFIGURATION,
|
---|
| 807 | * USB_DEVREQ_CLEAR_FEATURE,
|
---|
| 808 | * USB_DEVREQ_SET_FEATURE,
|
---|
| 809 | * USB_DEVREQ_SET_ADDRESS,
|
---|
| 810 | * USB_DEVREQ_SET_DESCRIPTOR or
|
---|
| 811 | * USB_DEVREQ_SET_CONFIGURATION.
|
---|
| 812 | *
|
---|
| 813 | * @param instance root hub instance
|
---|
| 814 | * @param request structure containing both request and response information
|
---|
| 815 | * @return error code
|
---|
| 816 | */
|
---|
[8b74997f] | 817 | static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request) {
|
---|
| 818 | if (!request->setup_buffer) {
|
---|
| 819 | usb_log_error("root hub received empty transaction?");
|
---|
| 820 | return EINVAL;
|
---|
| 821 | }
|
---|
[f3da9b2] | 822 | int opResult;
|
---|
[8b74997f] | 823 | if (sizeof (usb_device_request_setup_packet_t) > request->setup_size) {
|
---|
| 824 | usb_log_error("setup packet too small\n");
|
---|
| 825 | return EINVAL;
|
---|
| 826 | }
|
---|
| 827 | usb_log_info("CTRL packet: %s.\n",
|
---|
| 828 | usb_debug_str_buffer(
|
---|
| 829 | (const uint8_t *) request->setup_buffer, 8, 8));
|
---|
| 830 | usb_device_request_setup_packet_t * setup_request =
|
---|
| 831 | (usb_device_request_setup_packet_t*)
|
---|
| 832 | request->setup_buffer;
|
---|
| 833 | switch (setup_request->request) {
|
---|
| 834 | case USB_DEVREQ_GET_STATUS:
|
---|
| 835 | case USB_DEVREQ_GET_DESCRIPTOR:
|
---|
| 836 | case USB_DEVREQ_GET_CONFIGURATION:
|
---|
[f3da9b2] | 837 | usb_log_debug("processing request with output\n");
|
---|
[8b74997f] | 838 | opResult = process_request_with_output(
|
---|
| 839 | instance, request);
|
---|
| 840 | break;
|
---|
| 841 | case USB_DEVREQ_CLEAR_FEATURE:
|
---|
| 842 | case USB_DEVREQ_SET_FEATURE:
|
---|
| 843 | case USB_DEVREQ_SET_ADDRESS:
|
---|
| 844 | usb_log_debug("processing request without "
|
---|
| 845 | "additional data\n");
|
---|
| 846 | opResult = process_request_without_data(
|
---|
| 847 | instance, request);
|
---|
| 848 | break;
|
---|
| 849 | case USB_DEVREQ_SET_DESCRIPTOR:
|
---|
| 850 | case USB_DEVREQ_SET_CONFIGURATION:
|
---|
| 851 | usb_log_debug("processing request with "
|
---|
| 852 | "input\n");
|
---|
| 853 | opResult = process_request_with_input(
|
---|
| 854 | instance, request);
|
---|
| 855 | break;
|
---|
| 856 | default:
|
---|
| 857 | usb_log_warning("received unsuported request: "
|
---|
| 858 | "%d\n",
|
---|
| 859 | setup_request->request
|
---|
| 860 | );
|
---|
[f3da9b2] | 861 | opResult = ENOTSUP;
|
---|
| 862 | }
|
---|
| 863 | return opResult;
|
---|
| 864 | }
|
---|
[4d0c40b] | 865 |
|
---|
[41b96b4] | 866 | /**
|
---|
| 867 | * @}
|
---|
| 868 | */
|
---|