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