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

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

OHCI: Root hub: Set over-current protection mode to match power mode.

Fix typo.

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