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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5abaf4 was 9b8958b, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

ohci: Declare iteration variables inside for loops.

  • Property mode set to 100644
File size: 23.9 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>
[40c6cdf]42#include <usb/classes/hub.h>
[7d521e24]43#include <usb/dev/driver.h>
[3476be8]44#include "ohci_regs.h"
45
[7d521e24]46#include <usb/dev/request.h>
[4d0c40be]47#include <usb/classes/hub.h>
48
[0368669c]49/**
[95285378]50 * standart device descriptor for ohci root hub
[0368669c]51 */
[8b74997f]52static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
53 .configuration_count = 1,
54 .descriptor_type = USB_DESCTYPE_DEVICE,
55 .device_class = USB_CLASS_HUB,
56 .device_protocol = 0,
57 .device_subclass = 0,
58 .device_version = 0,
[7d5708d]59 .length = sizeof(usb_standard_device_descriptor_t),
[00694c5]60 .max_packet_size = 64,
[7d5708d]61 .vendor_id = 0x16db, /* HelenOS does not have USB vendor ID assigned.*/
[8b74997f]62 .product_id = 0x0001,
63 .str_serial_number = 0,
64 .usb_spec_version = 0x110,
[b3f655f]65};
66
[0368669c]67/**
68 * standart configuration descriptor with filled common values
69 * for ohci root hubs
70 */
[8b74997f]71static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
72 .attributes = 1 << 7,
[b3f655f]73 .configuration_number = 1,
74 .descriptor_type = USB_DESCTYPE_CONFIGURATION,
75 .interface_count = 1,
[7d5708d]76 .length = sizeof(usb_standard_configuration_descriptor_t),
[01bbbb2]77 .max_power = 0, /* root hubs don't need no power */
[b3f655f]78 .str_configuration = 0,
79};
80
[0368669c]81/**
82 * standart ohci root hub interface descriptor
83 */
[8b74997f]84static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
[b3f655f]85 .alternate_setting = 0,
86 .descriptor_type = USB_DESCTYPE_INTERFACE,
87 .endpoint_count = 1,
88 .interface_class = USB_CLASS_HUB,
89 .interface_number = 1,
90 .interface_protocol = 0,
91 .interface_subclass = 0,
[7d5708d]92 .length = sizeof(usb_standard_interface_descriptor_t),
[b3f655f]93 .str_interface = 0,
94};
95
[0368669c]96/**
97 * standart ohci root hub endpoint descriptor
98 */
[8b74997f]99static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
[b3f655f]100 .attributes = USB_TRANSFER_INTERRUPT,
101 .descriptor_type = USB_DESCTYPE_ENDPOINT,
[8b74997f]102 .endpoint_address = 1 + (1 << 7),
[00694c5]103 .length = sizeof(usb_standard_endpoint_descriptor_t),
104 .max_packet_size = 2,
[b3f655f]105 .poll_interval = 255,
106};
[41b96b4]107
[062b25f]108static void create_serialized_hub_descriptor(rh_t *instance);
[b7c2757]109static void rh_init_descriptors(rh_t *instance);
[14426a0]110static uint16_t create_interrupt_mask(rh_t *instance);
[7d5708d]111static int get_status(rh_t *instance, usb_transfer_batch_t *request);
112static int get_descriptor(rh_t *instance, usb_transfer_batch_t *request);
113static int set_feature(rh_t *instance, usb_transfer_batch_t *request);
114static int clear_feature(rh_t *instance, usb_transfer_batch_t *request);
115static int set_feature_port(rh_t *instance, uint16_t feature, uint16_t port);
116static int clear_feature_port(rh_t *instance, uint16_t feature, uint16_t port);
117static int control_request(rh_t *instance, usb_transfer_batch_t *request);
118static inline void interrupt_request(
119 usb_transfer_batch_t *request, uint16_t mask, size_t size)
120{
121 assert(request);
122
123 request->transfered_size = size;
[9c10e51]124 usb_transfer_batch_finish_error(request, &mask, size, EOK);
[7d5708d]125}
[bb58dc0b]126
127#define TRANSFER_OK(bytes) \
128do { \
129 request->transfered_size = bytes; \
130 return EOK; \
[56c6b88]131} while (0)
[8123695a]132
[7d5708d]133/** Root Hub driver structure initialization.
134 *
135 * Reads info registers and prepares descriptors. Sets power mode.
[8b74997f]136 */
[b7c2757]137void rh_init(rh_t *instance, ohci_regs_t *regs)
[ece7f78]138{
[8b74997f]139 assert(instance);
[062b25f]140 assert(regs);
[ece7f78]141
[8b74997f]142 instance->registers = regs;
[112d159]143 instance->port_count =
144 (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
[c85804f]145 if (instance->port_count > 15) {
[7d5708d]146 usb_log_warning("OHCI specification does not allow more than 15"
[ece7f78]147 " ports. Max 15 ports will be used");
148 instance->port_count = 15;
149 }
150
151 /* Don't forget the hub status bit and round up */
[7d5708d]152 instance->interrupt_mask_size = 1 + (instance->port_count / 8);
[c85804f]153 instance->unfinished_interrupt_transfer = NULL;
[7d5708d]154
[b4f291d]155#ifdef OHCI_POWER_SWITCH_no
[01bbbb2]156 /* Set port power mode to no power-switching. (always on) */
[c85804f]157 instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
[b4f291d]158 /* Set to no over-current reporting */
[2fe28ca1]159 instance->registers->rh_desc_a |= RHDA_NOCP_FLAG;
[b4f291d]160#elif defined OHCI_POWER_SWITCH_ganged
[7372a9a0]161 /* Set port power mode to no ganged power-switching. */
162 instance->registers->rh_desc_a &= ~RHDA_NPS_FLAG;
163 instance->registers->rh_desc_a &= ~RHDA_PSM_FLAG;
[302f9b2]164 instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
[2fe28ca1]165 /* Set to global over-current */
166 instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
167 instance->registers->rh_desc_a &= ~RHDA_OCPM_FLAG;
[7372a9a0]168#else
169 /* Set port power mode to no per port power-switching. */
170 instance->registers->rh_desc_a &= ~RHDA_NPS_FLAG;
171 instance->registers->rh_desc_a |= RHDA_PSM_FLAG;
172
[302f9b2]173 /* Control all ports by global switch and turn them off */
174 instance->registers->rh_desc_b &= (RHDB_PCC_MASK << RHDB_PCC_SHIFT);
175 instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
176 /* Return control to per port state */
177 instance->registers->rh_desc_b |=
178 ((1 << (instance->port_count + 1)) - 1) << RHDB_PCC_SHIFT;
[2fe28ca1]179 /* Set per port over-current */
180 instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
181 instance->registers->rh_desc_a |= RHDA_OCPM_FLAG;
[7372a9a0]182#endif
[d0c060b]183
[b7c2757]184 rh_init_descriptors(instance);
[01bbbb2]185
[c4fb5ecd]186 usb_log_info("Root hub (%zu ports) initialized.\n",
[95285378]187 instance->port_count);
[8b74997f]188}
189/*----------------------------------------------------------------------------*/
190/**
[a00768c]191 * Process root hub request.
[8b74997f]192 *
[a00768c]193 * @param instance Root hub instance
194 * @param request Structure containing both request and response information
195 * @return Error code
[8b74997f]196 */
[7d5708d]197void rh_request(rh_t *instance, usb_transfer_batch_t *request)
[a00768c]198{
[8b74997f]199 assert(instance);
200 assert(request);
[a00768c]201
202 switch (request->ep->transfer_type)
203 {
204 case USB_TRANSFER_CONTROL:
[65c3794]205 usb_log_debug("Root hub got CONTROL packet\n");
[7d5708d]206 const int ret = control_request(instance, request);
[9c10e51]207 usb_transfer_batch_finish_error(request, NULL, 0, ret);
[a00768c]208 break;
209 case USB_TRANSFER_INTERRUPT:
[65c3794]210 usb_log_debug("Root hub got INTERRUPT packet\n");
[14426a0]211 const uint16_t mask = create_interrupt_mask(instance);
[f20bc82]212 if (mask == 0) {
[049a16f]213 usb_log_debug("No changes..\n");
[7d5708d]214 assert(instance->unfinished_interrupt_transfer == NULL);
[361fcec]215 instance->unfinished_interrupt_transfer = request;
[9c10e51]216 return;
[3a85a2b]217 }
[7d5708d]218 usb_log_debug("Processing changes...\n");
219 interrupt_request(request, mask, instance->interrupt_mask_size);
[a00768c]220 break;
[7d5708d]221
[a00768c]222 default:
223 usb_log_error("Root hub got unsupported request.\n");
[9c10e51]224 usb_transfer_batch_finish_error(request, NULL, 0, EINVAL);
[8b74997f]225 }
[9c10e51]226 usb_transfer_batch_dispose(request);
[8b74997f]227}
228/*----------------------------------------------------------------------------*/
[361fcec]229/**
[7d5708d]230 * Process interrupt on a hub device.
[361fcec]231 *
232 * If there is no pending interrupt transfer, nothing happens.
233 * @param instance
234 */
[a00768c]235void rh_interrupt(rh_t *instance)
236{
[7d5708d]237 assert(instance);
238
[a00768c]239 if (!instance->unfinished_interrupt_transfer)
[3a85a2b]240 return;
[a00768c]241
[049a16f]242 usb_log_debug("Finalizing interrupt transfer\n");
[14426a0]243 const uint16_t mask = create_interrupt_mask(instance);
[7d5708d]244 interrupt_request(instance->unfinished_interrupt_transfer,
245 mask, instance->interrupt_mask_size);
[9c10e51]246 usb_transfer_batch_dispose(instance->unfinished_interrupt_transfer);
[f20bc82]247
248 instance->unfinished_interrupt_transfer = NULL;
[8b74997f]249}
250/*----------------------------------------------------------------------------*/
[66a54cc]251/**
[c85804f]252 * Create hub descriptor.
[66a54cc]253 *
[c85804f]254 * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
[66a54cc]255 *
[a00768c]256 * @param instance Root hub instance
257 * @return Error code
[66a54cc]258 */
[062b25f]259void create_serialized_hub_descriptor(rh_t *instance)
[a00768c]260{
261 assert(instance);
262
[c85804f]263 /* 7 bytes + 2 port bit fields (port count + global bit) */
[7d5708d]264 const size_t size = 7 + (instance->interrupt_mask_size * 2);
[062b25f]265 assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
[1368a6b]266 instance->hub_descriptor_size = size;
[c85804f]267
[062b25f]268 const uint32_t hub_desc = instance->registers->rh_desc_a;
269 const uint32_t port_desc = instance->registers->rh_desc_b;
[361fcec]270
[c85804f]271 /* bDescLength */
[1368a6b]272 instance->descriptors.hub[0] = size;
[c85804f]273 /* bDescriptorType */
[1368a6b]274 instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
[c85804f]275 /* bNmbrPorts */
[1368a6b]276 instance->descriptors.hub[2] = instance->port_count;
[c85804f]277 /* wHubCharacteristics */
[1368a6b]278 instance->descriptors.hub[3] = 0 |
[c85804f]279 /* The lowest 2 bits indicate power switching mode */
280 (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
281 (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
282 /* Bit 3 indicates device type (compound device) */
283 (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
284 /* Bits 4,5 indicate over-current protection mode */
285 (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
286 (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
287
288 /* Reserved */
[1368a6b]289 instance->descriptors.hub[4] = 0;
[c85804f]290 /* bPwrOn2PwrGood */
[1368a6b]291 instance->descriptors.hub[5] =
[062b25f]292 (hub_desc >> RHDA_POTPGT_SHIFT) & RHDA_POTPGT_MASK;
[c85804f]293 /* bHubContrCurrent, root hubs don't need no power. */
[1368a6b]294 instance->descriptors.hub[6] = 0;
[c85804f]295
296 /* Device Removable and some legacy 1.0 stuff*/
[1368a6b]297 instance->descriptors.hub[7] =
[062b25f]298 (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK & 0xff;
[1368a6b]299 instance->descriptors.hub[8] = 0xff;
[7d5708d]300 if (instance->interrupt_mask_size == 2) {
[1368a6b]301 instance->descriptors.hub[8] =
[062b25f]302 (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK >> 8;
[1368a6b]303 instance->descriptors.hub[9] = 0xff;
304 instance->descriptors.hub[10] = 0xff;
[66a54cc]305 }
306}
[8b74997f]307/*----------------------------------------------------------------------------*/
[a00768c]308/** Initialize hub descriptors.
[66a54cc]309 *
[7d5708d]310 * A full configuration descriptor is assembled. The configuration and endpoint
311 * descriptors have local modifications.
[a00768c]312 * @param instance Root hub instance
313 * @return Error code
[66a54cc]314 */
[b7c2757]315void rh_init_descriptors(rh_t *instance)
[a00768c]316{
317 assert(instance);
318
[1368a6b]319 instance->descriptors.configuration = ohci_rh_conf_descriptor;
320 instance->descriptors.interface = ohci_rh_iface_descriptor;
321 instance->descriptors.endpoint = ohci_rh_ep_descriptor;
[062b25f]322 create_serialized_hub_descriptor(instance);
[c85804f]323
[7d5708d]324 instance->descriptors.endpoint.max_packet_size =
325 instance->interrupt_mask_size;
326
[1368a6b]327 instance->descriptors.configuration.total_length =
[c85804f]328 sizeof(usb_standard_configuration_descriptor_t) +
329 sizeof(usb_standard_endpoint_descriptor_t) +
330 sizeof(usb_standard_interface_descriptor_t) +
[1368a6b]331 instance->hub_descriptor_size;
[66a54cc]332}
[41b96b4]333/*----------------------------------------------------------------------------*/
[7d5708d]334/**
335 * Create bitmap of changes to answer status interrupt.
336 *
337 * Result contains bitmap where bit 0 indicates change on hub and
338 * bit i indicates change on i`th port (i>0). For more info see
339 * Hub and Port status bitmap specification in USB specification
340 * (chapter 11.13.4).
341 * @param instance root hub instance
342 * @return Mask of changes.
343 */
344uint16_t create_interrupt_mask(rh_t *instance)
345{
346 assert(instance);
347 uint16_t mask = 0;
348
349 /* Only local power source change and over-current change can happen */
350 if (instance->registers->rh_status & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
351 mask |= 1;
352 }
[9b8958b]353 for (size_t port = 1; port <= instance->port_count; ++port) {
[7d5708d]354 /* Write-clean bits are those that indicate change */
355 if (RHPS_CHANGE_WC_MASK
356 & instance->registers->rh_port_status[port - 1]) {
357
358 mask |= (1 << port);
359 }
360 }
361 /* USB is little endian */
362 return host2uint32_t_le(mask);
363}
364/*----------------------------------------------------------------------------*/
[f3da9b2]365/**
[a00768c]366 * Create answer to status request.
[f3da9b2]367 *
368 * This might be either hub status or port status request. If neither,
369 * ENOTSUP is returned.
370 * @param instance root hub instance
371 * @param request structure containing both request and response information
372 * @return error code
373 */
[7d5708d]374int get_status(rh_t *instance, usb_transfer_batch_t *request)
[a00768c]375{
376 assert(instance);
377 assert(request);
[d8421c4]378
[a00768c]379 const usb_device_request_setup_packet_t *request_packet =
380 (usb_device_request_setup_packet_t*)request->setup_buffer;
381
382 if (request->buffer_size < 4) {
383 usb_log_error("Buffer too small for get status request.\n");
384 return EOVERFLOW;
[d8421c4]385 }
[4d0c40be]386
[00694c5]387 /* Hub status: just filter relevant info from rh_status reg */
[c3bc8a8]388 if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS) {
389 const uint32_t data = instance->registers->rh_status &
390 (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
[9c10e51]391 memcpy(request->buffer, &data, sizeof(data));
392 TRANSFER_OK(sizeof(data));
[c3bc8a8]393 }
394
[00694c5]395 /* Copy appropriate rh_port_status register, OHCI designers were
396 * kind enough to make those bit values match USB specification */
[c3bc8a8]397 if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS) {
[00694c5]398 const unsigned port = request_packet->index;
[c3bc8a8]399 if (port < 1 || port > instance->port_count)
400 return EINVAL;
401
402 const uint32_t data =
403 instance->registers->rh_port_status[port - 1];
[9c10e51]404 memcpy(request->buffer, &data, sizeof(data));
405 TRANSFER_OK(sizeof(data));
[c3bc8a8]406 }
[d0c060b]407
[4d0c40be]408 return ENOTSUP;
409}
[8b74997f]410/*----------------------------------------------------------------------------*/
[f3da9b2]411/**
[a00768c]412 * Create answer to a descriptor request.
[f3da9b2]413 *
414 * This might be a request for standard (configuration, device, endpoint or
415 * interface) or device specific (hub) descriptor.
[a00768c]416 * @param instance Root hub instance
417 * @param request Structure containing both request and response information
418 * @return Error code
[f3da9b2]419 */
[7d5708d]420int get_descriptor(rh_t *instance, usb_transfer_batch_t *request)
[a00768c]421{
422 assert(instance);
423 assert(request);
424
425 const usb_device_request_setup_packet_t *setup_request =
426 (usb_device_request_setup_packet_t *) request->setup_buffer;
[d8421c4]427 size_t size;
[7d5708d]428 const void *descriptor = NULL;
[b3f655f]429 const uint16_t setup_request_value = setup_request->value_high;
[8b74997f]430 //(setup_request->value_low << 8);
[a00768c]431 switch (setup_request_value)
432 {
433 case USB_DESCTYPE_HUB:
434 usb_log_debug2("USB_DESCTYPE_HUB\n");
[7d5708d]435 /* Hub descriptor was generated locally */
436 descriptor = instance->descriptors.hub;
[1368a6b]437 size = instance->hub_descriptor_size;
[a00768c]438 break;
439
440 case USB_DESCTYPE_DEVICE:
441 usb_log_debug2("USB_DESCTYPE_DEVICE\n");
[7d5708d]442 /* Device descriptor is shared (No one should ask for it)*/
443 descriptor = &ohci_rh_device_descriptor;
[a00768c]444 size = sizeof(ohci_rh_device_descriptor);
445 break;
446
447 case USB_DESCTYPE_CONFIGURATION:
448 usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
[7d5708d]449 /* Start with configuration and add others depending on
450 * request size */
451 descriptor = &instance->descriptors;
[1368a6b]452 size = instance->descriptors.configuration.total_length;
[a00768c]453 break;
454
455 case USB_DESCTYPE_INTERFACE:
456 usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
[7d5708d]457 /* Use local interface descriptor. There is one and it
458 * might be modified */
459 descriptor = &instance->descriptors.interface;
460 size = sizeof(instance->descriptors.interface);
[a00768c]461 break;
462
463 case USB_DESCTYPE_ENDPOINT:
[7d5708d]464 /* Use local endpoint descriptor. There is one
465 * it might have max_packet_size field modified*/
[a00768c]466 usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
[7d5708d]467 descriptor = &instance->descriptors.endpoint;
468 size = sizeof(instance->descriptors.endpoint);
[a00768c]469 break;
470
471 default:
472 usb_log_debug2("USB_DESCTYPE_EINVAL %d \n"
473 "\ttype %d\n\trequest %d\n\tvalue "
474 "%d\n\tindex %d\n\tlen %d\n ",
475 setup_request->value,
476 setup_request->request_type, setup_request->request,
477 setup_request_value, setup_request->index,
478 setup_request->length);
479 return EINVAL;
[4d0c40be]480 }
[8b74997f]481 if (request->buffer_size < size) {
[d8421c4]482 size = request->buffer_size;
483 }
[d0c060b]484
[9c10e51]485 memcpy(request->buffer, descriptor, size);
[56c6b88]486 TRANSFER_OK(size);
[4d0c40be]487}
[8b74997f]488/*----------------------------------------------------------------------------*/
[8123695a]489/**
490 * process feature-enabling request on hub
[8b74997f]491 *
[f3da9b2]492 * @param instance root hub instance
493 * @param feature feature selector
494 * @param port port number, counted from 1
495 * @param enable enable or disable the specified feature
496 * @return error code
497 */
[7d5708d]498int set_feature_port(rh_t *instance, uint16_t feature, uint16_t port)
[a00768c]499{
500 assert(instance);
501
[8b74997f]502 if (port < 1 || port > instance->port_count)
[4d0c40be]503 return EINVAL;
[a00768c]504
[735236a]505 switch (feature)
506 {
507 case USB_HUB_FEATURE_PORT_POWER: //8
508 /* No power switching */
509 if (instance->registers->rh_desc_a & RHDA_NPS_FLAG)
510 return EOK;
511 /* Ganged power switching */
512 if (!(instance->registers->rh_desc_a & RHDA_PSM_FLAG)) {
513 instance->registers->rh_status = RHS_SET_GLOBAL_POWER;
514 return EOK;
515 }
516 case USB_HUB_FEATURE_PORT_ENABLE: //1
517 case USB_HUB_FEATURE_PORT_SUSPEND: //2
518 case USB_HUB_FEATURE_PORT_RESET: //4
519 /* Nice thing is that these shifts correspond to the position
520 * of control bits in register */
521 instance->registers->rh_port_status[port - 1] = (1 << feature);
522 return EOK;
523 default:
524 return ENOTSUP;
525 }
[8123695a]526}
[8b74997f]527/*----------------------------------------------------------------------------*/
[8123695a]528/**
[7d5708d]529 * Process feature clear request.
[8123695a]530 *
531 * @param instance root hub instance
532 * @param feature feature selector
533 * @param port port number, counted from 1
534 * @param enable enable or disable the specified feature
535 * @return error code
536 */
[7d5708d]537int clear_feature_port(rh_t *instance, uint16_t feature, uint16_t port)
[a00768c]538{
539 assert(instance);
540
[8b74997f]541 if (port < 1 || port > instance->port_count)
[8123695a]542 return EINVAL;
[a00768c]543
[735236a]544 /* Enabled features to clear: see page 269 of USB specs */
545 switch (feature)
546 {
547 case USB_HUB_FEATURE_PORT_POWER: //8
548 /* No power switching */
549 if (instance->registers->rh_desc_a & RHDA_NPS_FLAG)
550 return ENOTSUP;
551 /* Ganged power switching */
552 if (!(instance->registers->rh_desc_a & RHDA_PSM_FLAG)) {
553 instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
554 return EOK;
555 }
556 instance->registers->rh_port_status[port - 1] =
557 RHPS_CLEAR_PORT_POWER;
558 return EOK;
559
560 case USB_HUB_FEATURE_PORT_ENABLE: //1
561 instance->registers->rh_port_status[port - 1] =
562 RHPS_CLEAR_PORT_ENABLE;
563 return EOK;
564
565 case USB_HUB_FEATURE_PORT_SUSPEND: //2
566 instance->registers->rh_port_status[port - 1] =
567 RHPS_CLEAR_PORT_SUSPEND;
568 return EOK;
569
570 case USB_HUB_FEATURE_C_PORT_CONNECTION: //16
571 case USB_HUB_FEATURE_C_PORT_ENABLE: //17
572 case USB_HUB_FEATURE_C_PORT_SUSPEND: //18
573 case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: //19
574 case USB_HUB_FEATURE_C_PORT_RESET: //20
575 /* Nice thing is that these shifts correspond to the position
576 * of control bits in register */
577 instance->registers->rh_port_status[port - 1] = (1 << feature);
578 return EOK;
[7d5708d]579
[735236a]580 default:
581 return ENOTSUP;
582 }
[d8421c4]583}
[8b74997f]584/*----------------------------------------------------------------------------*/
[f3da9b2]585/**
[7d5708d]586 * process one of requests that do not request nor carry additional data
[f3da9b2]587 *
[7d5708d]588 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
589 * USB_DEVREQ_SET_ADDRESS.
[f3da9b2]590 * @param instance root hub instance
591 * @param request structure containing both request and response information
592 * @return error code
593 */
[7d5708d]594int set_feature(rh_t *instance, usb_transfer_batch_t *request)
[a00768c]595{
596 assert(instance);
597 assert(request);
598
599 const usb_device_request_setup_packet_t *setup_request =
600 (usb_device_request_setup_packet_t *) request->setup_buffer;
[7d5708d]601 switch (setup_request->request_type)
[a00768c]602 {
[7d5708d]603 case USB_HUB_REQ_TYPE_SET_PORT_FEATURE:
604 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
605 return set_feature_port(instance,
606 setup_request->value, setup_request->index);
607
608 case USB_HUB_REQ_TYPE_SET_HUB_FEATURE:
609 /* Chapter 11.16.2 specifies that hub can be recipient
610 * only for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
611 * features. It makes no sense to SET either. */
612 usb_log_error("Invalid HUB set feature request.\n");
613 return ENOTSUP;
614 default:
615 usb_log_error("Invalid set feature request type: %d\n",
616 setup_request->request_type);
617 return EINVAL;
[4d0c40be]618 }
619}
[8b74997f]620/*----------------------------------------------------------------------------*/
[f3da9b2]621/**
622 * process one of requests that do not request nor carry additional data
623 *
624 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
625 * USB_DEVREQ_SET_ADDRESS.
626 * @param instance root hub instance
627 * @param request structure containing both request and response information
628 * @return error code
629 */
[7d5708d]630int clear_feature(rh_t *instance, usb_transfer_batch_t *request)
[a00768c]631{
632 assert(instance);
633 assert(request);
634
635 const usb_device_request_setup_packet_t *setup_request =
636 (usb_device_request_setup_packet_t *) request->setup_buffer;
[d8421c4]637 request->transfered_size = 0;
[7d5708d]638 switch (setup_request->request_type)
[a00768c]639 {
[7d5708d]640 case USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE:
641 usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE\n");
642 return clear_feature_port(instance,
643 setup_request->value, setup_request->index);
644
645 case USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE:
646 usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE\n");
647 /*
648 * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
649 * C_HUB_OVER_CURRENT are supported. C_HUB_OVER_CURRENT is represented
650 * by OHCI RHS_OCIC_FLAG. C_HUB_LOCAL_POWER is not supported
651 * as root hubs do not support local power status feature.
652 * (OHCI pg. 127) */
653 if (setup_request->value == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
654 instance->registers->rh_status = RHS_OCIC_FLAG;
655 TRANSFER_OK(0);
[8123695a]656 }
[a00768c]657 default:
[7d5708d]658 usb_log_error("Invalid clear feature request type: %d\n",
659 setup_request->request_type);
660 return EINVAL;
[a00768c]661 }
[4d0c40be]662}
[8b74997f]663/*----------------------------------------------------------------------------*/
[f3da9b2]664/**
[a00768c]665 * Process hub control request.
[f3da9b2]666 *
667 * If needed, writes answer into the request structure.
668 * Request can be one of
669 * USB_DEVREQ_GET_STATUS,
670 * USB_DEVREQ_GET_DESCRIPTOR,
671 * USB_DEVREQ_GET_CONFIGURATION,
672 * USB_DEVREQ_CLEAR_FEATURE,
673 * USB_DEVREQ_SET_FEATURE,
674 * USB_DEVREQ_SET_ADDRESS,
675 * USB_DEVREQ_SET_DESCRIPTOR or
676 * USB_DEVREQ_SET_CONFIGURATION.
677 *
678 * @param instance root hub instance
679 * @param request structure containing both request and response information
680 * @return error code
681 */
[7d5708d]682int control_request(rh_t *instance, usb_transfer_batch_t *request)
[a00768c]683{
684 assert(instance);
685 assert(request);
686
[8b74997f]687 if (!request->setup_buffer) {
[a00768c]688 usb_log_error("Root hub received empty transaction!");
[8b74997f]689 return EINVAL;
690 }
[7d5708d]691
[a00768c]692 if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
[049a16f]693 usb_log_error("Setup packet too small\n");
[a00768c]694 return EOVERFLOW;
[8b74997f]695 }
[7d5708d]696
[a00768c]697 usb_log_debug2("CTRL packet: %s.\n",
698 usb_debug_str_buffer((uint8_t *) request->setup_buffer, 8, 8));
699 const usb_device_request_setup_packet_t *setup_request =
700 (usb_device_request_setup_packet_t *) request->setup_buffer;
701 switch (setup_request->request)
702 {
703 case USB_DEVREQ_GET_STATUS:
[7d5708d]704 usb_log_debug("USB_DEVREQ_GET_STATUS\n");
705 return get_status(instance, request);
706
[a00768c]707 case USB_DEVREQ_GET_DESCRIPTOR:
[7d5708d]708 usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
709 return get_descriptor(instance, request);
710
[a00768c]711 case USB_DEVREQ_GET_CONFIGURATION:
[7d5708d]712 usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
713 if (request->buffer_size != 1)
714 return EINVAL;
[9c10e51]715 request->buffer[0] = 1;
[7d5708d]716 TRANSFER_OK(1);
717
[a00768c]718 case USB_DEVREQ_CLEAR_FEATURE:
[7d5708d]719 usb_log_debug2("Processing request without "
720 "additional data\n");
721 return clear_feature(instance, request);
[a00768c]722 case USB_DEVREQ_SET_FEATURE:
723 usb_log_debug2("Processing request without "
724 "additional data\n");
[7d5708d]725 return set_feature(instance, request);
726
727 case USB_DEVREQ_SET_ADDRESS:
728 usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
729 instance->address = setup_request->value;
730 TRANSFER_OK(0);
731
732 case USB_DEVREQ_SET_CONFIGURATION:
733 usb_log_debug("USB_DEVREQ_SET_CONFIGURATION\n");
734 /* We don't need to do anything */
735 TRANSFER_OK(0);
736
[56c6b88]737 case USB_DEVREQ_SET_DESCRIPTOR: /* Not supported by OHCI RH */
[a00768c]738 default:
739 usb_log_error("Received unsupported request: %d.\n",
740 setup_request->request);
741 return ENOTSUP;
[f3da9b2]742 }
743}
[4d0c40be]744
[41b96b4]745/**
746 * @}
747 */
Note: See TracBrowser for help on using the repository browser.