source: mainline/uspace/drv/bus/usb/ohci/root_hub.c@ 5203e256

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5203e256 was 5203e256, checked in by Martin Decky <martin@…>, 14 years ago

keep the drivers source tree tidy by using logical subdirectories

  • Property mode set to 100644
File size: 29.1 KB
RevLine 
[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]51static 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]70static 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]83static 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]98static 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]110static 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]117static 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]123static 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]130static 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]136static 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]145static 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]162static const uint32_t port_status_change_mask = RHPS_CHANGE_WC_MASK;
[8b74997f]163
[361fcec]164static int create_serialized_hub_descriptor(rh_t *instance);
[8b74997f]165
[361fcec]166static int rh_init_descriptors(rh_t *instance);
[8b74997f]167
168static int process_get_port_status_request(rh_t *instance, uint16_t port,
[d0c060b]169 usb_transfer_batch_t * request);
[8b74997f]170
171static int process_get_hub_status_request(rh_t *instance,
[d0c060b]172 usb_transfer_batch_t * request);
[8b74997f]173
174static int process_get_status_request(rh_t *instance,
[d0c060b]175 usb_transfer_batch_t * request);
[8b74997f]176
[361fcec]177static void create_interrupt_mask_in_instance(rh_t *instance);
[8b74997f]178
179static int process_get_descriptor_request(rh_t *instance,
[d0c060b]180 usb_transfer_batch_t *request);
[8b74997f]181
182static int process_get_configuration_request(rh_t *instance,
[d0c060b]183 usb_transfer_batch_t *request);
[8b74997f]184
185static int process_hub_feature_set_request(rh_t *instance, uint16_t feature);
186
187static int process_hub_feature_clear_request(rh_t *instance,
[d0c060b]188 uint16_t feature);
[8b74997f]189
190static int process_port_feature_set_request(rh_t *instance,
[d0c060b]191 uint16_t feature, uint16_t port);
[8b74997f]192
193static int process_port_feature_clear_request(rh_t *instance,
[d0c060b]194 uint16_t feature, uint16_t port);
[8b74997f]195
196static int process_address_set_request(rh_t *instance,
[d0c060b]197 uint16_t address);
[8b74997f]198
199static int process_request_with_output(rh_t *instance,
[d0c060b]200 usb_transfer_batch_t *request);
[8b74997f]201
202static int process_request_with_input(rh_t *instance,
[d0c060b]203 usb_transfer_batch_t *request);
[8b74997f]204
205static int process_request_without_data(rh_t *instance,
[d0c060b]206 usb_transfer_batch_t *request);
[8123695a]207
[8b74997f]208static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
[8123695a]209
[361fcec]210static int process_interrupt_mask_in_instance(rh_t *instance, usb_transfer_batch_t * request);
[8123695a]211
[3a85a2b]212static bool is_zeros(void * buffer, size_t size);
[8123695a]213
[8b74997f]214/** Root hub initialization
215 * @return Error code.
216 */
[8148ee3a]217int 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
[c4fb5ecd]234 usb_log_info("Root hub (%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 */
248int 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) {
[65c3794]253 usb_log_debug("Root hub got CONTROL packet\n");
[8b74997f]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) {
[65c3794]257 usb_log_debug("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)) {
[049a16f]261 usb_log_debug("No changes..\n");
[361fcec]262 instance->unfinished_interrupt_transfer = request;
[3a85a2b]263 //will be finished later
[d0c060b]264 } else {
[049a16f]265 usb_log_debug("Processing changes..\n");
[361fcec]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]285void rh_interrupt(rh_t *instance) {
[d0c060b]286 if (!instance->unfinished_interrupt_transfer) {
[3a85a2b]287 return;
288 }
[049a16f]289 usb_log_debug("Finalizing interrupt transfer\n");
[361fcec]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]305static 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]354static 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]407static 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);
[4d0c40b]414 return EOK;
415}
[8b74997f]416/*----------------------------------------------------------------------------*/
[4d0c40b]417
[f3da9b2]418/**
419 * create answer to port status_request
420 *
[8bb61e6]421 * This copies flags in hub status register into the buffer. The format of the
422 * status register and status message is the same, according to USB hub
423 * specification and OHCI root hub specification.
[f3da9b2]424 *
425 * @param instance root hub instance
426 * @param request structure containing both request and response information
427 * @return error code
428 */
[d8421c4]429static int process_get_hub_status_request(rh_t *instance,
[d0c060b]430 usb_transfer_batch_t * request) {
[d8421c4]431 request->transfered_size = 4;
[8b74997f]432 //bits, 0,1,16,17
433 uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
[63d4d4fd]434 uint32_t data = mask & instance->registers->rh_status;
[6ab7f3e9]435 memcpy(request->data_buffer, &data, 4);
[d0c060b]436
[4d0c40b]437 return EOK;
438}
[8b74997f]439/*----------------------------------------------------------------------------*/
[d8421c4]440
[f3da9b2]441/**
442 * create answer to status request
443 *
444 * This might be either hub status or port status request. If neither,
445 * ENOTSUP is returned.
446 * @param instance root hub instance
447 * @param request structure containing both request and response information
448 * @return error code
449 */
[d8421c4]450static int process_get_status_request(rh_t *instance,
[d0c060b]451 usb_transfer_batch_t * request) {
[d8421c4]452 size_t buffer_size = request->buffer_size;
453 usb_device_request_setup_packet_t * request_packet =
[d0c060b]454 (usb_device_request_setup_packet_t*)
455 request->setup_buffer;
[d8421c4]456
457 usb_hub_bm_request_type_t request_type = request_packet->request_type;
[b2387c2]458 if (buffer_size < 4) {
[049a16f]459 usb_log_warning("Requested more data than buffer size\n");
[d8421c4]460 return EINVAL;
461 }
[4d0c40b]462
[8b74997f]463 if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
[d8421c4]464 return process_get_hub_status_request(instance, request);
[8b74997f]465 if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
466 return process_get_port_status_request(instance,
[d0c060b]467 request_packet->index,
468 request);
469
[4d0c40b]470 return ENOTSUP;
471}
[8b74997f]472/*----------------------------------------------------------------------------*/
[4d0c40b]473
[f3da9b2]474/**
475 * create answer to status interrupt consisting of change bitmap
476 *
477 * Result contains bitmap where bit 0 indicates change on hub and
478 * bit i indicates change on i`th port (i>0). For more info see
[8b74997f]479 * Hub and Port status bitmap specification in USB specification
[361fcec]480 * (chapter 11.13.4).
481 * Uses instance`s interrupt buffer to store the interrupt information.
[f3da9b2]482 * @param instance root hub instance
483 */
[361fcec]484static void create_interrupt_mask_in_instance(rh_t * instance) {
485 uint8_t * bitmap = (uint8_t*) (instance->interrupt_buffer);
[8b74997f]486 uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
[d0c060b]487 | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
[361fcec]488 bzero(bitmap, instance->interrupt_mask_size);
[6ab7f3e9]489 if ((instance->registers->rh_status & mask) != 0) {
[d8421c4]490 bitmap[0] = 1;
491 }
[d0c060b]492 size_t port;
[8b74997f]493 mask = port_status_change_mask;
494 for (port = 1; port <= instance->port_count; ++port) {
[e89bb50]495 if ((mask & instance->registers->rh_port_status[port - 1]) != 0) {
[d0c060b]496
[8b74997f]497 bitmap[(port) / 8] += 1 << (port % 8);
[d8421c4]498 }
499 }
500}
[8b74997f]501/*----------------------------------------------------------------------------*/
502
[f3da9b2]503/**
504 * create answer to a descriptor request
505 *
506 * This might be a request for standard (configuration, device, endpoint or
507 * interface) or device specific (hub) descriptor.
508 * @param instance root hub instance
509 * @param request structure containing both request and response information
510 * @return error code
511 */
[4d0c40b]512static int process_get_descriptor_request(rh_t *instance,
[d0c060b]513 usb_transfer_batch_t *request) {
[4d0c40b]514 usb_device_request_setup_packet_t * setup_request =
[d0c060b]515 (usb_device_request_setup_packet_t*) request->setup_buffer;
[d8421c4]516 size_t size;
[aee6c73]517 const void * result_descriptor = NULL;
[b3f655f]518 const uint16_t setup_request_value = setup_request->value_high;
[8b74997f]519 //(setup_request->value_low << 8);
520 switch (setup_request_value) {
521 case USB_DESCTYPE_HUB:
522 {
[c4fb5ecd]523 usb_log_debug2("USB_DESCTYPE_HUB\n");
[361fcec]524 result_descriptor = instance->hub_descriptor;
525 size = instance->descriptor_size;
[0368669c]526 break;
527 }
[8b74997f]528 case USB_DESCTYPE_DEVICE:
529 {
[c4fb5ecd]530 usb_log_debug2("USB_DESCTYPE_DEVICE\n");
[0368669c]531 result_descriptor = &ohci_rh_device_descriptor;
[8b74997f]532 size = sizeof (ohci_rh_device_descriptor);
[0368669c]533 break;
534 }
[8b74997f]535 case USB_DESCTYPE_CONFIGURATION:
536 {
[c4fb5ecd]537 usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
[66a54cc]538 result_descriptor = instance->descriptors.configuration;
539 size = instance->descriptors.configuration_size;
[0368669c]540 break;
541 }
[8b74997f]542 case USB_DESCTYPE_INTERFACE:
543 {
[c4fb5ecd]544 usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
[0368669c]545 result_descriptor = &ohci_rh_iface_descriptor;
[8b74997f]546 size = sizeof (ohci_rh_iface_descriptor);
[0368669c]547 break;
548 }
[8b74997f]549 case USB_DESCTYPE_ENDPOINT:
550 {
[c4fb5ecd]551 usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
[0368669c]552 result_descriptor = &ohci_rh_ep_descriptor;
[8b74997f]553 size = sizeof (ohci_rh_ep_descriptor);
[0368669c]554 break;
555 }
[8b74997f]556 default:
557 {
[c4fb5ecd]558 usb_log_debug2("USB_DESCTYPE_EINVAL %d \n",
[d0c060b]559 setup_request->value);
[c4fb5ecd]560 usb_log_debug2("\ttype %d\n\trequest %d\n\tvalue "
[d0c060b]561 "%d\n\tindex %d\n\tlen %d\n ",
562 setup_request->request_type,
563 setup_request->request,
564 setup_request_value,
565 setup_request->index,
566 setup_request->length
567 );
[0368669c]568 return EINVAL;
569 }
[4d0c40b]570 }
[8b74997f]571 if (request->buffer_size < size) {
[d8421c4]572 size = request->buffer_size;
573 }
574 request->transfered_size = size;
[d017cea]575 memcpy(request->data_buffer, result_descriptor, size);
[d0c060b]576
[4d0c40b]577 return EOK;
578}
[8b74997f]579/*----------------------------------------------------------------------------*/
[4d0c40b]580
[f3da9b2]581/**
582 * answer to get configuration request
583 *
584 * Root hub works independently on the configuration.
585 * @param instance root hub instance
586 * @param request structure containing both request and response information
587 * @return error code
588 */
[8b74997f]589static int process_get_configuration_request(rh_t *instance,
[d0c060b]590 usb_transfer_batch_t *request) {
[4d0c40b]591 //set and get configuration requests do not have any meaning, only dummy
592 //values are returned
[8b74997f]593 if (request->buffer_size != 1)
[4d0c40b]594 return EINVAL;
[d017cea]595 request->data_buffer[0] = 1;
[d8421c4]596 request->transfered_size = 1;
[d0c060b]597
[4d0c40b]598 return EOK;
599}
[8b74997f]600/*----------------------------------------------------------------------------*/
[4d0c40b]601
[f3da9b2]602/**
[8123695a]603 * process feature-enabling request on hub
[8b74997f]604 *
[f3da9b2]605 * @param instance root hub instance
606 * @param feature feature selector
607 * @return error code
608 */
[4d0c40b]609static int process_hub_feature_set_request(rh_t *instance,
[d0c060b]610 uint16_t feature) {
[8b74997f]611 if (!((1 << feature) & hub_set_feature_valid_mask))
[4d0c40b]612 return EINVAL;
[d0c060b]613 if (feature == USB_HUB_FEATURE_C_HUB_LOCAL_POWER)
[f35b294]614 feature = USB_HUB_FEATURE_C_HUB_LOCAL_POWER << 16;
[4d0c40b]615 instance->registers->rh_status =
[d0c060b]616 (instance->registers->rh_status | (1 << feature))
617 & (~hub_clear_feature_by_writing_one_mask);
618
[4d0c40b]619 return EOK;
620}
[8b74997f]621/*----------------------------------------------------------------------------*/
[4d0c40b]622
[f3da9b2]623/**
[8123695a]624 * process feature-disabling request on hub
625 *
626 * @param instance root hub instance
627 * @param feature feature selector
628 * @return error code
629 */
630static int process_hub_feature_clear_request(rh_t *instance,
[d0c060b]631 uint16_t feature) {
[8b74997f]632 if (!((1 << feature) & hub_clear_feature_valid_mask))
[8123695a]633 return EINVAL;
634 //is the feature cleared directly?
[8b74997f]635 if ((1 << feature) & hub_set_feature_direct_mask) {
[8123695a]636 instance->registers->rh_status =
[d0c060b]637 (instance->registers->rh_status & (~(1 << feature)))
638 & (~hub_clear_feature_by_writing_one_mask);
[8b74997f]639 } else {//the feature is cleared by writing '1'
[d0c060b]640
[8123695a]641 instance->registers->rh_status =
[d0c060b]642 (instance->registers->rh_status
643 & (~hub_clear_feature_by_writing_one_mask))
644 | (1 << feature);
[8123695a]645 }
646 return EOK;
647}
[8b74997f]648/*----------------------------------------------------------------------------*/
[8123695a]649
650/**
651 * process feature-enabling request on hub
[8b74997f]652 *
[f3da9b2]653 * @param instance root hub instance
654 * @param feature feature selector
655 * @param port port number, counted from 1
656 * @param enable enable or disable the specified feature
657 * @return error code
658 */
[4d0c40b]659static int process_port_feature_set_request(rh_t *instance,
[d0c060b]660 uint16_t feature, uint16_t port) {
[8b74997f]661 if (!((1 << feature) & port_set_feature_valid_mask))
[4d0c40b]662 return EINVAL;
[8b74997f]663 if (port < 1 || port > instance->port_count)
[4d0c40b]664 return EINVAL;
665 instance->registers->rh_port_status[port - 1] =
[d0c060b]666 (instance->registers->rh_port_status[port - 1] | (1 << feature))
667 & (~port_clear_feature_valid_mask);
[8123695a]668 return EOK;
669}
[8b74997f]670/*----------------------------------------------------------------------------*/
[8123695a]671
672/**
673 * process feature-disabling request on hub
674 *
675 * @param instance root hub instance
676 * @param feature feature selector
677 * @param port port number, counted from 1
678 * @param enable enable or disable the specified feature
679 * @return error code
680 */
681static int process_port_feature_clear_request(rh_t *instance,
[d0c060b]682 uint16_t feature, uint16_t port) {
[8b74997f]683 if (!((1 << feature) & port_clear_feature_valid_mask))
[8123695a]684 return EINVAL;
[8b74997f]685 if (port < 1 || port > instance->port_count)
[8123695a]686 return EINVAL;
[8b74997f]687 if (feature == USB_HUB_FEATURE_PORT_POWER)
[8123695a]688 feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
[8b74997f]689 if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
[8123695a]690 feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
691 instance->registers->rh_port_status[port - 1] =
[d0c060b]692 (instance->registers->rh_port_status[port - 1]
693 & (~port_clear_feature_valid_mask))
694 | (1 << feature);
695
[4d0c40b]696 return EOK;
[d8421c4]697}
[8b74997f]698/*----------------------------------------------------------------------------*/
[8123695a]699
[f3da9b2]700/**
701 * register address to this device
[8b74997f]702 *
[f3da9b2]703 * @param instance root hub instance
704 * @param address new address
705 * @return error code
706 */
[d8421c4]707static int process_address_set_request(rh_t *instance,
[d0c060b]708 uint16_t address) {
[049a16f]709 return ENOTSUP;
[4d0c40b]710}
[8b74997f]711/*----------------------------------------------------------------------------*/
[4d0c40b]712
[f3da9b2]713/**
714 * process one of requests that requere output data
715 *
716 * Request can be one of USB_DEVREQ_GET_STATUS, USB_DEVREQ_GET_DESCRIPTOR or
717 * USB_DEVREQ_GET_CONFIGURATION.
718 * @param instance root hub instance
719 * @param request structure containing both request and response information
720 * @return error code
721 */
[4d0c40b]722static int process_request_with_output(rh_t *instance,
[d0c060b]723 usb_transfer_batch_t *request) {
[4d0c40b]724 usb_device_request_setup_packet_t * setup_request =
[d0c060b]725 (usb_device_request_setup_packet_t*) request->setup_buffer;
[8b74997f]726 if (setup_request->request == USB_DEVREQ_GET_STATUS) {
[d8421c4]727 usb_log_debug("USB_DEVREQ_GET_STATUS\n");
728 return process_get_status_request(instance, request);
[4d0c40b]729 }
[8b74997f]730 if (setup_request->request == USB_DEVREQ_GET_DESCRIPTOR) {
[d8421c4]731 usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
[4d0c40b]732 return process_get_descriptor_request(instance, request);
733 }
[8b74997f]734 if (setup_request->request == USB_DEVREQ_GET_CONFIGURATION) {
[d8421c4]735 usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
[d0c060b]736
[d8421c4]737 return process_get_configuration_request(instance, request);
[4d0c40b]738 }
739 return ENOTSUP;
740}
[8b74997f]741/*----------------------------------------------------------------------------*/
[4d0c40b]742
[f3da9b2]743/**
744 * process one of requests that carry input data
745 *
746 * Request can be one of USB_DEVREQ_SET_DESCRIPTOR or
747 * USB_DEVREQ_SET_CONFIGURATION.
748 * @param instance root hub instance
749 * @param request structure containing both request and response information
750 * @return error code
751 */
[4d0c40b]752static int process_request_with_input(rh_t *instance,
[d0c060b]753 usb_transfer_batch_t *request) {
[4d0c40b]754 usb_device_request_setup_packet_t * setup_request =
[d0c060b]755 (usb_device_request_setup_packet_t*) request->setup_buffer;
[d8421c4]756 request->transfered_size = 0;
[8b74997f]757 if (setup_request->request == USB_DEVREQ_SET_DESCRIPTOR) {
[4d0c40b]758 return ENOTSUP;
759 }
[8b74997f]760 if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
[4d0c40b]761 //set and get configuration requests do not have any meaning,
762 //only dummy values are returned
[d0c060b]763
[4d0c40b]764 return EOK;
765 }
766 return ENOTSUP;
767}
[8b74997f]768/*----------------------------------------------------------------------------*/
[4d0c40b]769
[f3da9b2]770/**
771 * process one of requests that do not request nor carry additional data
772 *
773 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
774 * USB_DEVREQ_SET_ADDRESS.
775 * @param instance root hub instance
776 * @param request structure containing both request and response information
777 * @return error code
778 */
[4d0c40b]779static int process_request_without_data(rh_t *instance,
[d0c060b]780 usb_transfer_batch_t *request) {
[4d0c40b]781 usb_device_request_setup_packet_t * setup_request =
[d0c060b]782 (usb_device_request_setup_packet_t*) request->setup_buffer;
[d8421c4]783 request->transfered_size = 0;
[8b74997f]784 if (setup_request->request == USB_DEVREQ_CLEAR_FEATURE) {
785 if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
[d8421c4]786 usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
[8123695a]787 return process_hub_feature_clear_request(instance,
[d0c060b]788 setup_request->value);
[d8421c4]789 }
[8b74997f]790 if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
[c4fb5ecd]791 usb_log_debug2("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
[8123695a]792 return process_port_feature_clear_request(instance,
[d0c060b]793 setup_request->value,
794 setup_request->index);
[8123695a]795 }
796 usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
[d0c060b]797 setup_request->request_type);
[8123695a]798 return EINVAL;
799 }
[8b74997f]800 if (setup_request->request == USB_DEVREQ_SET_FEATURE) {
801 if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
[8123695a]802 usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
803 return process_hub_feature_set_request(instance,
[d0c060b]804 setup_request->value);
[8123695a]805 }
[8b74997f]806 if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
[8123695a]807 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
808 return process_port_feature_set_request(instance,
[d0c060b]809 setup_request->value,
810 setup_request->index);
[d8421c4]811 }
[8b74997f]812 usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
[d0c060b]813 setup_request->request_type);
[d8421c4]814 return EINVAL;
[4d0c40b]815 }
[8b74997f]816 if (setup_request->request == USB_DEVREQ_SET_ADDRESS) {
[d8421c4]817 usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
[8b74997f]818 return process_address_set_request(instance,
[d0c060b]819 setup_request->value);
[4d0c40b]820 }
[8b74997f]821 usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",
[d0c060b]822 setup_request->request_type);
823
[4d0c40b]824 return ENOTSUP;
825}
[8b74997f]826/*----------------------------------------------------------------------------*/
[4d0c40b]827
[f3da9b2]828/**
829 * process hub control request
830 *
831 * If needed, writes answer into the request structure.
832 * Request can be one of
833 * USB_DEVREQ_GET_STATUS,
834 * USB_DEVREQ_GET_DESCRIPTOR,
835 * USB_DEVREQ_GET_CONFIGURATION,
836 * USB_DEVREQ_CLEAR_FEATURE,
837 * USB_DEVREQ_SET_FEATURE,
838 * USB_DEVREQ_SET_ADDRESS,
839 * USB_DEVREQ_SET_DESCRIPTOR or
840 * USB_DEVREQ_SET_CONFIGURATION.
841 *
842 * @param instance root hub instance
843 * @param request structure containing both request and response information
844 * @return error code
845 */
[8b74997f]846static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request) {
847 if (!request->setup_buffer) {
848 usb_log_error("root hub received empty transaction?");
849 return EINVAL;
850 }
[f3da9b2]851 int opResult;
[8b74997f]852 if (sizeof (usb_device_request_setup_packet_t) > request->setup_size) {
[049a16f]853 usb_log_error("Setup packet too small\n");
[8b74997f]854 return EINVAL;
855 }
[c4fb5ecd]856 usb_log_debug("CTRL packet: %s.\n",
[d0c060b]857 usb_debug_str_buffer(
858 (const uint8_t *) request->setup_buffer, 8, 8));
[8b74997f]859 usb_device_request_setup_packet_t * setup_request =
[d0c060b]860 (usb_device_request_setup_packet_t*)
861 request->setup_buffer;
[8b74997f]862 switch (setup_request->request) {
863 case USB_DEVREQ_GET_STATUS:
864 case USB_DEVREQ_GET_DESCRIPTOR:
865 case USB_DEVREQ_GET_CONFIGURATION:
[c4fb5ecd]866 usb_log_debug2("Processing request with output\n");
[8b74997f]867 opResult = process_request_with_output(
[d0c060b]868 instance, request);
[8b74997f]869 break;
870 case USB_DEVREQ_CLEAR_FEATURE:
871 case USB_DEVREQ_SET_FEATURE:
872 case USB_DEVREQ_SET_ADDRESS:
[c4fb5ecd]873 usb_log_debug2("Processing request without "
[d0c060b]874 "additional data\n");
[8b74997f]875 opResult = process_request_without_data(
[d0c060b]876 instance, request);
[8b74997f]877 break;
878 case USB_DEVREQ_SET_DESCRIPTOR:
879 case USB_DEVREQ_SET_CONFIGURATION:
[c4fb5ecd]880 usb_log_debug2("Processing request with input\n");
[8b74997f]881 opResult = process_request_with_input(
[d0c060b]882 instance, request);
883
[8b74997f]884 break;
885 default:
[c4fb5ecd]886 usb_log_warning("Received unsupported request: %d.\n",
887 setup_request->request);
[f3da9b2]888 opResult = ENOTSUP;
889 }
890 return opResult;
891}
[3a85a2b]892/*----------------------------------------------------------------------------*/
893
894/**
895 * process hanging interrupt request
896 *
897 * If an interrupt transfer has been received and there was no change,
898 * the driver stores the transfer information and waits for change to occcur.
899 * This routine is called when that happens and it finalizes the interrupt
900 * transfer.
901 *
902 * @param instance hub instance
903 * @param request batch request to be processed
904 *
905 * @return
906 */
[6ab7f3e9]907static int process_interrupt_mask_in_instance(rh_t *instance,
908 usb_transfer_batch_t * request) {
[361fcec]909 memcpy(request->data_buffer, instance->interrupt_buffer,
910 instance->interrupt_mask_size);
911 request->transfered_size = instance->interrupt_mask_size;
[3a85a2b]912 instance->unfinished_interrupt_transfer = NULL;
913 usb_transfer_batch_finish_error(request, EOK);
[d0c060b]914
[3a85a2b]915 return EOK;
916}
917
918/*----------------------------------------------------------------------------*/
919
920/**
921 * return whether the buffer is full of zeros
922 *
923 * Convenience function.
924 * @param buffer
925 * @param size
926 * @return
927 */
[6ab7f3e9]928static bool is_zeros(void *buffer, size_t size) {
[d0c060b]929 if (!buffer) return true;
930 if (!size) return true;
[3a85a2b]931 size_t i;
[d0c060b]932 for (i = 0; i < size; ++i) {
933 if (((char*) buffer)[i])
[3a85a2b]934 return false;
935 }
936 return true;
937}
[4d0c40b]938
[41b96b4]939/**
940 * @}
941 */
Note: See TracBrowser for help on using the repository browser.