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