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

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

ohci: Fix root hub driver.

Handle request manipulation in one place (macro).

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