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

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

OHCI: Export root hub power switching modes to config.

  • Property mode set to 100644
File size: 23.8 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 memcpy(request->data_buffer, &mask, size);
124 request->transfered_size = size;
125 usb_transfer_batch_finish_error(request, EOK);
126}
127
128#define TRANSFER_OK(bytes) \
129do { \
130 request->transfered_size = bytes; \
131 return EOK; \
132} while (0)
133
134/** Root Hub driver structure initialization.
135 *
136 * Reads info registers and prepares descriptors. Sets power mode.
137 */
138void rh_init(rh_t *instance, ohci_regs_t *regs)
139{
140 assert(instance);
141 assert(regs);
142
143 instance->registers = regs;
144 instance->port_count =
145 (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
146 if (instance->port_count > 15) {
147 usb_log_warning("OHCI specification does not allow more than 15"
148 " ports. Max 15 ports will be used");
149 instance->port_count = 15;
150 }
151
152 /* Don't forget the hub status bit and round up */
153 instance->interrupt_mask_size = 1 + (instance->port_count / 8);
154 instance->unfinished_interrupt_transfer = NULL;
155
156#ifdef OHCI_POWER_SWITCH_no
157 /* Set port power mode to no power-switching. (always on) */
158 instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
159 /* Set to no over-current reporting */
160 instance->registers->rh_desc_a |= RHDA_NOCP_FLAG;
161#elif defined OHCI_POWER_SWITCH_ganged
162 /* Set port power mode to no ganged power-switching. */
163 instance->registers->rh_desc_a &= ~RHDA_NPS_FLAG;
164 instance->registers->rh_desc_a &= ~RHDA_PSM_FLAG;
165 instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
166 /* Set to global over-current */
167 instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
168 instance->registers->rh_desc_a &= ~RHDA_OCPM_FLAG;
169#else
170 /* Set port power mode to no per port power-switching. */
171 instance->registers->rh_desc_a &= ~RHDA_NPS_FLAG;
172 instance->registers->rh_desc_a |= RHDA_PSM_FLAG;
173
174 /* Control all ports by global switch and turn them off */
175 instance->registers->rh_desc_b &= (RHDB_PCC_MASK << RHDB_PCC_SHIFT);
176 instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
177 /* Return control to per port state */
178 instance->registers->rh_desc_b |=
179 ((1 << (instance->port_count + 1)) - 1) << RHDB_PCC_SHIFT;
180 /* Set per port over-current */
181 instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
182 instance->registers->rh_desc_a |= RHDA_OCPM_FLAG;
183#endif
184
185 rh_init_descriptors(instance);
186
187 usb_log_info("Root hub (%zu ports) initialized.\n",
188 instance->port_count);
189}
190/*----------------------------------------------------------------------------*/
191/**
192 * Process root hub request.
193 *
194 * @param instance Root hub instance
195 * @param request Structure containing both request and response information
196 * @return Error code
197 */
198void rh_request(rh_t *instance, usb_transfer_batch_t *request)
199{
200 assert(instance);
201 assert(request);
202
203 switch (request->ep->transfer_type)
204 {
205 case USB_TRANSFER_CONTROL:
206 usb_log_debug("Root hub got CONTROL packet\n");
207 const int ret = control_request(instance, request);
208 usb_transfer_batch_finish_error(request, ret);
209 break;
210 case USB_TRANSFER_INTERRUPT:
211 usb_log_debug("Root hub got INTERRUPT packet\n");
212 const uint16_t mask = create_interrupt_mask(instance);
213 if (mask == 0) {
214 usb_log_debug("No changes..\n");
215 assert(instance->unfinished_interrupt_transfer == NULL);
216 instance->unfinished_interrupt_transfer = request;
217 break;
218 }
219 usb_log_debug("Processing changes...\n");
220 interrupt_request(request, mask, instance->interrupt_mask_size);
221 break;
222
223 default:
224 usb_log_error("Root hub got unsupported request.\n");
225 usb_transfer_batch_finish_error(request, EINVAL);
226 }
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
247 instance->unfinished_interrupt_transfer = NULL;
248}
249/*----------------------------------------------------------------------------*/
250/**
251 * Create hub descriptor.
252 *
253 * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
254 *
255 * @param instance Root hub instance
256 * @return Error code
257 */
258void create_serialized_hub_descriptor(rh_t *instance)
259{
260 assert(instance);
261
262 /* 7 bytes + 2 port bit fields (port count + global bit) */
263 const size_t size = 7 + (instance->interrupt_mask_size * 2);
264 assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
265 instance->hub_descriptor_size = size;
266
267 const uint32_t hub_desc = instance->registers->rh_desc_a;
268 const uint32_t port_desc = instance->registers->rh_desc_b;
269
270 /* bDescLength */
271 instance->descriptors.hub[0] = size;
272 /* bDescriptorType */
273 instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
274 /* bNmbrPorts */
275 instance->descriptors.hub[2] = instance->port_count;
276 /* wHubCharacteristics */
277 instance->descriptors.hub[3] = 0 |
278 /* The lowest 2 bits indicate power switching mode */
279 (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
280 (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
281 /* Bit 3 indicates device type (compound device) */
282 (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
283 /* Bits 4,5 indicate over-current protection mode */
284 (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
285 (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
286
287 /* Reserved */
288 instance->descriptors.hub[4] = 0;
289 /* bPwrOn2PwrGood */
290 instance->descriptors.hub[5] =
291 (hub_desc >> RHDA_POTPGT_SHIFT) & RHDA_POTPGT_MASK;
292 /* bHubContrCurrent, root hubs don't need no power. */
293 instance->descriptors.hub[6] = 0;
294
295 /* Device Removable and some legacy 1.0 stuff*/
296 instance->descriptors.hub[7] =
297 (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK & 0xff;
298 instance->descriptors.hub[8] = 0xff;
299 if (instance->interrupt_mask_size == 2) {
300 instance->descriptors.hub[8] =
301 (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK >> 8;
302 instance->descriptors.hub[9] = 0xff;
303 instance->descriptors.hub[10] = 0xff;
304 }
305}
306/*----------------------------------------------------------------------------*/
307/** Initialize hub descriptors.
308 *
309 * A full configuration descriptor is assembled. The configuration and endpoint
310 * descriptors have local modifications.
311 * @param instance Root hub instance
312 * @return Error code
313 */
314void rh_init_descriptors(rh_t *instance)
315{
316 assert(instance);
317
318 instance->descriptors.configuration = ohci_rh_conf_descriptor;
319 instance->descriptors.interface = ohci_rh_iface_descriptor;
320 instance->descriptors.endpoint = ohci_rh_ep_descriptor;
321 create_serialized_hub_descriptor(instance);
322
323 instance->descriptors.endpoint.max_packet_size =
324 instance->interrupt_mask_size;
325
326 instance->descriptors.configuration.total_length =
327 sizeof(usb_standard_configuration_descriptor_t) +
328 sizeof(usb_standard_endpoint_descriptor_t) +
329 sizeof(usb_standard_interface_descriptor_t) +
330 instance->hub_descriptor_size;
331}
332/*----------------------------------------------------------------------------*/
333/**
334 * Create bitmap of changes to answer status interrupt.
335 *
336 * Result contains bitmap where bit 0 indicates change on hub and
337 * bit i indicates change on i`th port (i>0). For more info see
338 * Hub and Port status bitmap specification in USB specification
339 * (chapter 11.13.4).
340 * @param instance root hub instance
341 * @return Mask of changes.
342 */
343uint16_t create_interrupt_mask(rh_t *instance)
344{
345 assert(instance);
346 uint16_t mask = 0;
347
348 /* Only local power source change and over-current change can happen */
349 if (instance->registers->rh_status & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
350 mask |= 1;
351 }
352 size_t port = 1;
353 for (; 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->data_buffer, &data, 4);
392 TRANSFER_OK(4);
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->data_buffer, &data, 4);
405 TRANSFER_OK(4);
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->data_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->data_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.