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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9515f674 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
Line 
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
32 * @brief OHCI driver
33 */
34#include <assert.h>
35#include <errno.h>
36#include <str_error.h>
37
38#include <usb/debug.h>
39
40#include "root_hub.h"
41#include <usb/classes/classes.h>
42#include <usb/classes/hub.h>
43#include <usb/dev/driver.h>
44#include "ohci_regs.h"
45
46#include <usb/dev/request.h>
47#include <usb/classes/hub.h>
48
49/**
50 * standart device descriptor for ohci root hub
51 */
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,
59 .length = sizeof(usb_standard_device_descriptor_t),
60 .max_packet_size = 64,
61 .vendor_id = 0x16db, /* HelenOS does not have USB vendor ID assigned.*/
62 .product_id = 0x0001,
63 .str_serial_number = 0,
64 .usb_spec_version = 0x110,
65};
66
67/**
68 * standart configuration descriptor with filled common values
69 * for ohci root hubs
70 */
71static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
72 .attributes = 1 << 7,
73 .configuration_number = 1,
74 .descriptor_type = USB_DESCTYPE_CONFIGURATION,
75 .interface_count = 1,
76 .length = sizeof(usb_standard_configuration_descriptor_t),
77 .max_power = 0, /* root hubs don't need no power */
78 .str_configuration = 0,
79};
80
81/**
82 * standart ohci root hub interface descriptor
83 */
84static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
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,
92 .length = sizeof(usb_standard_interface_descriptor_t),
93 .str_interface = 0,
94};
95
96/**
97 * standart ohci root hub endpoint descriptor
98 */
99static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
100 .attributes = USB_TRANSFER_INTERRUPT,
101 .descriptor_type = USB_DESCTYPE_ENDPOINT,
102 .endpoint_address = 1 + (1 << 7),
103 .length = sizeof(usb_standard_endpoint_descriptor_t),
104 .max_packet_size = 2,
105 .poll_interval = 255,
106};
107
108static void create_serialized_hub_descriptor(rh_t *instance);
109static void rh_init_descriptors(rh_t *instance);
110static uint16_t create_interrupt_mask(rh_t *instance);
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;
124 usb_transfer_batch_finish_error(request, &mask, size, EOK);
125}
126
127#define TRANSFER_OK(bytes) \
128do { \
129 request->transfered_size = bytes; \
130 return EOK; \
131} while (0)
132
133/** Root Hub driver structure initialization.
134 *
135 * Reads info registers and prepares descriptors. Sets power mode.
136 */
137void rh_init(rh_t *instance, ohci_regs_t *regs)
138{
139 assert(instance);
140 assert(regs);
141
142 instance->registers = regs;
143 instance->port_count =
144 (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
145 if (instance->port_count > 15) {
146 usb_log_warning("OHCI specification does not allow more than 15"
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 */
152 instance->interrupt_mask_size = 1 + (instance->port_count / 8);
153 instance->unfinished_interrupt_transfer = NULL;
154
155#ifdef OHCI_POWER_SWITCH_no
156 /* Set port power mode to no power-switching. (always on) */
157 instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
158 /* Set to no over-current reporting */
159 instance->registers->rh_desc_a |= RHDA_NOCP_FLAG;
160#elif defined OHCI_POWER_SWITCH_ganged
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;
164 instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
165 /* Set to global over-current */
166 instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
167 instance->registers->rh_desc_a &= ~RHDA_OCPM_FLAG;
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
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;
179 /* Set per port over-current */
180 instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
181 instance->registers->rh_desc_a |= RHDA_OCPM_FLAG;
182#endif
183
184 rh_init_descriptors(instance);
185
186 usb_log_info("Root hub (%zu ports) initialized.\n",
187 instance->port_count);
188}
189/*----------------------------------------------------------------------------*/
190/**
191 * Process root hub request.
192 *
193 * @param instance Root hub instance
194 * @param request Structure containing both request and response information
195 * @return Error code
196 */
197void rh_request(rh_t *instance, usb_transfer_batch_t *request)
198{
199 assert(instance);
200 assert(request);
201
202 switch (request->ep->transfer_type)
203 {
204 case USB_TRANSFER_CONTROL:
205 usb_log_debug("Root hub got CONTROL packet\n");
206 const int ret = control_request(instance, request);
207 usb_transfer_batch_finish_error(request, NULL, 0, ret);
208 break;
209 case USB_TRANSFER_INTERRUPT:
210 usb_log_debug("Root hub got INTERRUPT packet\n");
211 const uint16_t mask = create_interrupt_mask(instance);
212 if (mask == 0) {
213 usb_log_debug("No changes..\n");
214 assert(instance->unfinished_interrupt_transfer == NULL);
215 instance->unfinished_interrupt_transfer = request;
216 return;
217 }
218 usb_log_debug("Processing changes...\n");
219 interrupt_request(request, mask, instance->interrupt_mask_size);
220 break;
221
222 default:
223 usb_log_error("Root hub got unsupported request.\n");
224 usb_transfer_batch_finish_error(request, NULL, 0, EINVAL);
225 }
226 usb_transfer_batch_dispose(request);
227}
228/*----------------------------------------------------------------------------*/
229/**
230 * Process interrupt on a hub device.
231 *
232 * If there is no pending interrupt transfer, nothing happens.
233 * @param instance
234 */
235void rh_interrupt(rh_t *instance)
236{
237 assert(instance);
238
239 if (!instance->unfinished_interrupt_transfer)
240 return;
241
242 usb_log_debug("Finalizing interrupt transfer\n");
243 const uint16_t mask = create_interrupt_mask(instance);
244 interrupt_request(instance->unfinished_interrupt_transfer,
245 mask, instance->interrupt_mask_size);
246 usb_transfer_batch_dispose(instance->unfinished_interrupt_transfer);
247
248 instance->unfinished_interrupt_transfer = NULL;
249}
250/*----------------------------------------------------------------------------*/
251/**
252 * Create hub descriptor.
253 *
254 * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
255 *
256 * @param instance Root hub instance
257 * @return Error code
258 */
259void create_serialized_hub_descriptor(rh_t *instance)
260{
261 assert(instance);
262
263 /* 7 bytes + 2 port bit fields (port count + global bit) */
264 const size_t size = 7 + (instance->interrupt_mask_size * 2);
265 assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
266 instance->hub_descriptor_size = size;
267
268 const uint32_t hub_desc = instance->registers->rh_desc_a;
269 const uint32_t port_desc = instance->registers->rh_desc_b;
270
271 /* bDescLength */
272 instance->descriptors.hub[0] = size;
273 /* bDescriptorType */
274 instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
275 /* bNmbrPorts */
276 instance->descriptors.hub[2] = instance->port_count;
277 /* wHubCharacteristics */
278 instance->descriptors.hub[3] = 0 |
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 */
289 instance->descriptors.hub[4] = 0;
290 /* bPwrOn2PwrGood */
291 instance->descriptors.hub[5] =
292 (hub_desc >> RHDA_POTPGT_SHIFT) & RHDA_POTPGT_MASK;
293 /* bHubContrCurrent, root hubs don't need no power. */
294 instance->descriptors.hub[6] = 0;
295
296 /* Device Removable and some legacy 1.0 stuff*/
297 instance->descriptors.hub[7] =
298 (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK & 0xff;
299 instance->descriptors.hub[8] = 0xff;
300 if (instance->interrupt_mask_size == 2) {
301 instance->descriptors.hub[8] =
302 (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK >> 8;
303 instance->descriptors.hub[9] = 0xff;
304 instance->descriptors.hub[10] = 0xff;
305 }
306}
307/*----------------------------------------------------------------------------*/
308/** Initialize hub descriptors.
309 *
310 * A full configuration descriptor is assembled. The configuration and endpoint
311 * descriptors have local modifications.
312 * @param instance Root hub instance
313 * @return Error code
314 */
315void rh_init_descriptors(rh_t *instance)
316{
317 assert(instance);
318
319 instance->descriptors.configuration = ohci_rh_conf_descriptor;
320 instance->descriptors.interface = ohci_rh_iface_descriptor;
321 instance->descriptors.endpoint = ohci_rh_ep_descriptor;
322 create_serialized_hub_descriptor(instance);
323
324 instance->descriptors.endpoint.max_packet_size =
325 instance->interrupt_mask_size;
326
327 instance->descriptors.configuration.total_length =
328 sizeof(usb_standard_configuration_descriptor_t) +
329 sizeof(usb_standard_endpoint_descriptor_t) +
330 sizeof(usb_standard_interface_descriptor_t) +
331 instance->hub_descriptor_size;
332}
333/*----------------------------------------------------------------------------*/
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 }
353 for (size_t port = 1; port <= instance->port_count; ++port) {
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/*----------------------------------------------------------------------------*/
365/**
366 * Create answer to status request.
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 */
374int get_status(rh_t *instance, usb_transfer_batch_t *request)
375{
376 assert(instance);
377 assert(request);
378
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;
385 }
386
387 /* Hub status: just filter relevant info from rh_status reg */
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);
391 memcpy(request->buffer, &data, sizeof(data));
392 TRANSFER_OK(sizeof(data));
393 }
394
395 /* Copy appropriate rh_port_status register, OHCI designers were
396 * kind enough to make those bit values match USB specification */
397 if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS) {
398 const unsigned port = request_packet->index;
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];
404 memcpy(request->buffer, &data, sizeof(data));
405 TRANSFER_OK(sizeof(data));
406 }
407
408 return ENOTSUP;
409}
410/*----------------------------------------------------------------------------*/
411/**
412 * Create answer to a descriptor request.
413 *
414 * This might be a request for standard (configuration, device, endpoint or
415 * interface) or device specific (hub) descriptor.
416 * @param instance Root hub instance
417 * @param request Structure containing both request and response information
418 * @return Error code
419 */
420int get_descriptor(rh_t *instance, usb_transfer_batch_t *request)
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;
427 size_t size;
428 const void *descriptor = NULL;
429 const uint16_t setup_request_value = setup_request->value_high;
430 //(setup_request->value_low << 8);
431 switch (setup_request_value)
432 {
433 case USB_DESCTYPE_HUB:
434 usb_log_debug2("USB_DESCTYPE_HUB\n");
435 /* Hub descriptor was generated locally */
436 descriptor = instance->descriptors.hub;
437 size = instance->hub_descriptor_size;
438 break;
439
440 case USB_DESCTYPE_DEVICE:
441 usb_log_debug2("USB_DESCTYPE_DEVICE\n");
442 /* Device descriptor is shared (No one should ask for it)*/
443 descriptor = &ohci_rh_device_descriptor;
444 size = sizeof(ohci_rh_device_descriptor);
445 break;
446
447 case USB_DESCTYPE_CONFIGURATION:
448 usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
449 /* Start with configuration and add others depending on
450 * request size */
451 descriptor = &instance->descriptors;
452 size = instance->descriptors.configuration.total_length;
453 break;
454
455 case USB_DESCTYPE_INTERFACE:
456 usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
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);
461 break;
462
463 case USB_DESCTYPE_ENDPOINT:
464 /* Use local endpoint descriptor. There is one
465 * it might have max_packet_size field modified*/
466 usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
467 descriptor = &instance->descriptors.endpoint;
468 size = sizeof(instance->descriptors.endpoint);
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;
480 }
481 if (request->buffer_size < size) {
482 size = request->buffer_size;
483 }
484
485 memcpy(request->buffer, descriptor, size);
486 TRANSFER_OK(size);
487}
488/*----------------------------------------------------------------------------*/
489/**
490 * process feature-enabling request on hub
491 *
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 */
498int set_feature_port(rh_t *instance, uint16_t feature, uint16_t port)
499{
500 assert(instance);
501
502 if (port < 1 || port > instance->port_count)
503 return EINVAL;
504
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 }
526}
527/*----------------------------------------------------------------------------*/
528/**
529 * Process feature clear request.
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 */
537int clear_feature_port(rh_t *instance, uint16_t feature, uint16_t port)
538{
539 assert(instance);
540
541 if (port < 1 || port > instance->port_count)
542 return EINVAL;
543
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;
579
580 default:
581 return ENOTSUP;
582 }
583}
584/*----------------------------------------------------------------------------*/
585/**
586 * process one of requests that do not request nor carry additional data
587 *
588 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
589 * USB_DEVREQ_SET_ADDRESS.
590 * @param instance root hub instance
591 * @param request structure containing both request and response information
592 * @return error code
593 */
594int set_feature(rh_t *instance, usb_transfer_batch_t *request)
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;
601 switch (setup_request->request_type)
602 {
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;
618 }
619}
620/*----------------------------------------------------------------------------*/
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 */
630int clear_feature(rh_t *instance, usb_transfer_batch_t *request)
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;
637 request->transfered_size = 0;
638 switch (setup_request->request_type)
639 {
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);
656 }
657 default:
658 usb_log_error("Invalid clear feature request type: %d\n",
659 setup_request->request_type);
660 return EINVAL;
661 }
662}
663/*----------------------------------------------------------------------------*/
664/**
665 * Process hub control request.
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 */
682int control_request(rh_t *instance, usb_transfer_batch_t *request)
683{
684 assert(instance);
685 assert(request);
686
687 if (!request->setup_buffer) {
688 usb_log_error("Root hub received empty transaction!");
689 return EINVAL;
690 }
691
692 if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
693 usb_log_error("Setup packet too small\n");
694 return EOVERFLOW;
695 }
696
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:
704 usb_log_debug("USB_DEVREQ_GET_STATUS\n");
705 return get_status(instance, request);
706
707 case USB_DEVREQ_GET_DESCRIPTOR:
708 usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
709 return get_descriptor(instance, request);
710
711 case USB_DEVREQ_GET_CONFIGURATION:
712 usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
713 if (request->buffer_size != 1)
714 return EINVAL;
715 request->buffer[0] = 1;
716 TRANSFER_OK(1);
717
718 case USB_DEVREQ_CLEAR_FEATURE:
719 usb_log_debug2("Processing request without "
720 "additional data\n");
721 return clear_feature(instance, request);
722 case USB_DEVREQ_SET_FEATURE:
723 usb_log_debug2("Processing request without "
724 "additional data\n");
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
737 case USB_DEVREQ_SET_DESCRIPTOR: /* Not supported by OHCI RH */
738 default:
739 usb_log_error("Received unsupported request: %d.\n",
740 setup_request->request);
741 return ENOTSUP;
742 }
743}
744
745/**
746 * @}
747 */
Note: See TracBrowser for help on using the repository browser.