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

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

ohci: Minor changes to root hub.

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