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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e882e3a was 827ec41, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Fix build errors.

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