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