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