source: mainline/uspace/drv/bus/usb/ohci/root_hub.c@ 76fbd9a

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

usb drivers: remove optical separators

  • Property mode set to 100644
File size: 26.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 <byteorder.h>
36#include <errno.h>
37#include <str_error.h>
38#include <fibril_synch.h>
39
40#include <usb/usb.h>
41#include <usb/debug.h>
42#include <usb/dev/request.h>
43#include <usb/classes/hub.h>
44
45#include <usb/classes/classes.h>
46#include <usb/classes/hub.h>
47#include <usb/dev/driver.h>
48#include "ohci_regs.h"
49#include "root_hub.h"
50
51/**
52 * standart device descriptor for ohci root hub
53 */
54static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
55 .configuration_count = 1,
56 .descriptor_type = USB_DESCTYPE_DEVICE,
57 .device_class = USB_CLASS_HUB,
58 .device_protocol = 0,
59 .device_subclass = 0,
60 .device_version = 0,
61 .length = sizeof(usb_standard_device_descriptor_t),
62 .max_packet_size = 64,
63 .vendor_id = 0x16db, /* HelenOS does not have USB vendor ID assigned.*/
64 .product_id = 0x0001,
65 .str_serial_number = 0,
66 .usb_spec_version = 0x110,
67};
68
69/**
70 * standart configuration descriptor with filled common values
71 * for ohci root hubs
72 */
73static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
74 .attributes = 1 << 7,
75 .configuration_number = 1,
76 .descriptor_type = USB_DESCTYPE_CONFIGURATION,
77 .interface_count = 1,
78 .length = sizeof(usb_standard_configuration_descriptor_t),
79 .max_power = 0, /* root hubs don't need no power */
80 .str_configuration = 0,
81};
82
83/**
84 * standart ohci root hub interface descriptor
85 */
86static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
87 .alternate_setting = 0,
88 .descriptor_type = USB_DESCTYPE_INTERFACE,
89 .endpoint_count = 1,
90 .interface_class = USB_CLASS_HUB,
91 .interface_number = 1,
92 .interface_protocol = 0,
93 .interface_subclass = 0,
94 .length = sizeof(usb_standard_interface_descriptor_t),
95 .str_interface = 0,
96};
97
98/**
99 * standart ohci root hub endpoint descriptor
100 */
101static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
102 .attributes = USB_TRANSFER_INTERRUPT,
103 .descriptor_type = USB_DESCTYPE_ENDPOINT,
104 .endpoint_address = 1 | (1 << 7),
105 .length = sizeof(usb_standard_endpoint_descriptor_t),
106 .max_packet_size = 2,
107 .poll_interval = 255,
108};
109
110static void create_serialized_hub_descriptor(rh_t *instance);
111static void rh_init_descriptors(rh_t *instance);
112static uint16_t create_interrupt_mask(const rh_t *instance);
113static void get_status(const rh_t *instance, usb_transfer_batch_t *request);
114static void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request);
115static void set_feature(const rh_t *instance, usb_transfer_batch_t *request);
116static void clear_feature(const rh_t *instance, usb_transfer_batch_t *request);
117static int set_feature_port(
118 const rh_t *instance, uint16_t feature, uint16_t port);
119static int clear_feature_port(
120 const rh_t *instance, uint16_t feature, uint16_t port);
121static void control_request(rh_t *instance, usb_transfer_batch_t *request);
122static inline void interrupt_request(
123 usb_transfer_batch_t *request, uint16_t mask, size_t size)
124{
125 assert(request);
126 usb_log_debug("Sending interrupt vector(%zu) %hhx:%hhx.\n",
127 size, ((uint8_t*)&mask)[0], ((uint8_t*)&mask)[1]);
128 usb_transfer_batch_finish_error(request, &mask, size, EOK);
129 usb_transfer_batch_destroy(request);
130}
131
132#define TRANSFER_END_DATA(request, data, bytes) \
133do { \
134 usb_transfer_batch_finish_error(request, data, bytes, EOK); \
135 usb_transfer_batch_destroy(request); \
136 return; \
137} while (0)
138
139#define TRANSFER_END(request, error) \
140do { \
141 usb_transfer_batch_finish_error(request, NULL, 0, error); \
142 usb_transfer_batch_destroy(request); \
143 return; \
144} while (0)
145
146/** Root Hub driver structure initialization.
147 *
148 * Reads info registers and prepares descriptors. Sets power mode.
149 */
150void rh_init(rh_t *instance, ohci_regs_t *regs)
151{
152 assert(instance);
153 assert(regs);
154
155 instance->registers = regs;
156 instance->port_count = OHCI_RD(regs->rh_desc_a) & RHDA_NDS_MASK;
157 usb_log_debug2("rh_desc_a: %x.\n", OHCI_RD(regs->rh_desc_a));
158 if (instance->port_count > 15) {
159 usb_log_warning("OHCI specification does not allow more than 15"
160 " ports. Max 15 ports will be used");
161 instance->port_count = 15;
162 }
163
164 /* Don't forget the hub status bit and round up */
165 instance->interrupt_mask_size = 1 + (instance->port_count / 8);
166 instance->unfinished_interrupt_transfer = NULL;
167
168#if defined OHCI_POWER_SWITCH_no
169 usb_log_debug("OHCI rh: Set power mode to no power switching.\n");
170 /* Set port power mode to no power-switching. (always on) */
171 OHCI_SET(regs->rh_desc_a, RHDA_NPS_FLAG);
172
173 /* Set to no over-current reporting */
174 OHCI_SET(regs->rh_desc_a, RHDA_NOCP_FLAG);
175
176#elif defined OHCI_POWER_SWITCH_ganged
177 usb_log_debug("OHCI rh: Set power mode to ganged power switching.\n");
178 /* Set port power mode to ganged power-switching. */
179 OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
180 OHCI_CLR(regs->rh_desc_a, RHDA_PSM_FLAG);
181
182 /* Turn off power (hub driver will turn this back on)*/
183 OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
184
185 /* Set to global over-current */
186 OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
187 OHCI_CLR(regs->rh_desc_a, RHDA_OCPM_FLAG);
188#else
189 usb_log_debug("OHCI rh: Set power mode to per-port power switching.\n");
190 /* Set port power mode to per port power-switching. */
191 OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
192 OHCI_SET(regs->rh_desc_a, RHDA_PSM_FLAG);
193
194 /* Control all ports by global switch and turn them off */
195 OHCI_CLR(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
196 OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
197
198 /* Return control to per port state */
199 OHCI_SET(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
200
201 /* Set per port over-current */
202 OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
203 OHCI_SET(regs->rh_desc_a, RHDA_OCPM_FLAG);
204#endif
205
206 fibril_mutex_initialize(&instance->guard);
207 rh_init_descriptors(instance);
208
209 usb_log_info("Root hub (%zu ports) initialized.\n",
210 instance->port_count);
211}
212
213/**
214 * Process root hub request.
215 *
216 * @param instance Root hub instance
217 * @param request Structure containing both request and response information
218 * @return Error code
219 */
220void rh_request(rh_t *instance, usb_transfer_batch_t *request)
221{
222 assert(instance);
223 assert(request);
224
225 switch (request->ep->transfer_type)
226 {
227 case USB_TRANSFER_CONTROL:
228 usb_log_debug("Root hub got CONTROL packet\n");
229 control_request(instance, request);
230 break;
231
232 case USB_TRANSFER_INTERRUPT:
233 usb_log_debug("Root hub got INTERRUPT packet\n");
234 fibril_mutex_lock(&instance->guard);
235 assert(instance->unfinished_interrupt_transfer == NULL);
236 const uint16_t mask = create_interrupt_mask(instance);
237 if (mask == 0) {
238 usb_log_debug("No changes(%hx)...\n", mask);
239 instance->unfinished_interrupt_transfer = request;
240 } else {
241 usb_log_debug("Processing changes...\n");
242 interrupt_request(
243 request, mask, instance->interrupt_mask_size);
244 }
245 fibril_mutex_unlock(&instance->guard);
246 break;
247
248 default:
249 usb_log_error("Root hub got unsupported request.\n");
250 TRANSFER_END(request, ENOTSUP);
251 }
252}
253
254/**
255 * Process interrupt on a hub device.
256 *
257 * If there is no pending interrupt transfer, nothing happens.
258 * @param instance
259 */
260void rh_interrupt(rh_t *instance)
261{
262 assert(instance);
263
264 fibril_mutex_lock(&instance->guard);
265 if (instance->unfinished_interrupt_transfer) {
266 usb_log_debug("Finalizing interrupt transfer\n");
267 const uint16_t mask = create_interrupt_mask(instance);
268 interrupt_request(instance->unfinished_interrupt_transfer,
269 mask, instance->interrupt_mask_size);
270 instance->unfinished_interrupt_transfer = NULL;
271 }
272 fibril_mutex_unlock(&instance->guard);
273}
274
275/**
276 * Create hub descriptor.
277 *
278 * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
279 *
280 * @param instance Root hub instance
281 * @return Error code
282 */
283void create_serialized_hub_descriptor(rh_t *instance)
284{
285 assert(instance);
286
287 /* 7 bytes + 2 port bit fields (port count + global bit) */
288 size_t size = 7 + (instance->interrupt_mask_size * 2);
289 assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
290 instance->hub_descriptor_size = size;
291
292 const uint32_t hub_desc = OHCI_RD(instance->registers->rh_desc_a);
293 const uint32_t port_desc = OHCI_RD(instance->registers->rh_desc_b);
294
295 /* bDescLength */
296 instance->descriptors.hub[0] = size;
297 /* bDescriptorType */
298 instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
299 /* bNmbrPorts */
300 instance->descriptors.hub[2] = instance->port_count;
301 /* wHubCharacteristics */
302 instance->descriptors.hub[3] = 0 |
303 /* The lowest 2 bits indicate power switching mode */
304 (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
305 (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
306 /* Bit 3 indicates device type (compound device) */
307 (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
308 /* Bits 4,5 indicate over-current protection mode */
309 (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
310 (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
311
312 /* Reserved */
313 instance->descriptors.hub[4] = 0;
314 /* bPwrOn2PwrGood */
315 instance->descriptors.hub[5] = hub_desc >> RHDA_POTPGT_SHIFT;
316 /* bHubContrCurrent, root hubs don't need no power. */
317 instance->descriptors.hub[6] = 0;
318
319 /* Device Removable and some legacy 1.0 stuff*/
320 instance->descriptors.hub[7] = (port_desc >> RHDB_DR_SHIFT) & 0xff;
321 instance->descriptors.hub[8] = 0xff;
322 if (instance->interrupt_mask_size == 2) {
323 instance->descriptors.hub[8] =
324 (port_desc >> RHDB_DR_SHIFT) >> 8;
325 instance->descriptors.hub[9] = 0xff;
326 instance->descriptors.hub[10] = 0xff;
327 }
328}
329
330/** Initialize hub descriptors.
331 *
332 * A full configuration descriptor is assembled. The configuration and endpoint
333 * descriptors have local modifications.
334 * @param instance Root hub instance
335 * @return Error code
336 */
337void rh_init_descriptors(rh_t *instance)
338{
339 assert(instance);
340
341 instance->descriptors.configuration = ohci_rh_conf_descriptor;
342 instance->descriptors.interface = ohci_rh_iface_descriptor;
343 instance->descriptors.endpoint = ohci_rh_ep_descriptor;
344 create_serialized_hub_descriptor(instance);
345
346 instance->descriptors.endpoint.max_packet_size =
347 instance->interrupt_mask_size;
348
349 instance->descriptors.configuration.total_length = uint16_host2usb(
350 sizeof(usb_standard_configuration_descriptor_t) +
351 sizeof(usb_standard_endpoint_descriptor_t) +
352 sizeof(usb_standard_interface_descriptor_t) +
353 instance->hub_descriptor_size);
354}
355
356/**
357 * Create bitmap of changes to answer status interrupt.
358 *
359 * Result contains bitmap where bit 0 indicates change on hub and
360 * bit i indicates change on i`th port (i>0). For more info see
361 * Hub and Port status bitmap specification in USB specification
362 * (chapter 11.13.4).
363 * @param instance root hub instance
364 * @return Mask of changes.
365 */
366uint16_t create_interrupt_mask(const rh_t *instance)
367{
368 assert(instance);
369 uint16_t mask = 0;
370
371 /* Only local power source change and over-current change can happen */
372 if (OHCI_RD(instance->registers->rh_status)
373 & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
374 mask |= 1;
375 }
376 for (size_t port = 1; port <= instance->port_count; ++port) {
377 /* Write-clean bits are those that indicate change */
378 if (OHCI_RD(instance->registers->rh_port_status[port - 1])
379 & RHPS_CHANGE_WC_MASK) {
380 mask |= (1 << port);
381 }
382 }
383 usb_log_debug2("OHCI root hub interrupt mask: %hx.\n", mask);
384 return uint16_host2usb(mask);
385}
386
387/**
388 * Create answer to status request.
389 *
390 * This might be either hub status or port status request. If neither,
391 * ENOTSUP is returned.
392 * @param instance root hub instance
393 * @param request structure containing both request and response information
394 * @return error code
395 */
396void get_status(const rh_t *instance, usb_transfer_batch_t *request)
397{
398 assert(instance);
399 assert(request);
400
401
402 usb_device_request_setup_packet_t *request_packet =
403 (usb_device_request_setup_packet_t*)request->setup_buffer;
404
405 const uint16_t index = uint16_usb2host(request_packet->index);
406
407 switch (request_packet->request_type)
408 {
409 case USB_HUB_REQ_TYPE_GET_HUB_STATUS:
410 /* Hub status: just filter relevant info from rh_status reg */
411 if (request->buffer_size < 4) {
412 usb_log_error("Buffer(%zu) too small for hub get "
413 "status request.\n", request->buffer_size);
414 TRANSFER_END(request, EOVERFLOW);
415 } else {
416 const uint32_t data =
417 OHCI_RD(instance->registers->rh_status) &
418 (RHS_LPS_FLAG | RHS_LPSC_FLAG
419 | RHS_OCI_FLAG | RHS_OCIC_FLAG);
420 TRANSFER_END_DATA(request, &data, sizeof(data));
421 }
422
423 /* Copy appropriate rh_port_status register, OHCI designers were
424 * kind enough to make those bit values match USB specification */
425 case USB_HUB_REQ_TYPE_GET_PORT_STATUS:
426 if (request->buffer_size < 4) {
427 usb_log_error("Buffer(%zu) too small for hub get "
428 "status request.\n", request->buffer_size);
429 TRANSFER_END(request, EOVERFLOW);
430 } else {
431 const unsigned port = index;
432 if (port < 1 || port > instance->port_count)
433 TRANSFER_END(request, EINVAL);
434 /* Register format matches the format of port status
435 * field */
436 const uint32_t data = uint32_usb2host(OHCI_RD(
437 instance->registers->rh_port_status[port - 1]));
438 TRANSFER_END_DATA(request, &data, sizeof(data));
439 }
440 case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE):
441 if (request->buffer_size < 2) {
442 usb_log_error("Buffer(%zu) too small for hub generic "
443 "get status request.\n", request->buffer_size);
444 TRANSFER_END(request, EOVERFLOW);
445 } else {
446 const uint16_t data =
447 uint16_host2usb(USB_DEVICE_STATUS_SELF_POWERED);
448 TRANSFER_END_DATA(request, &data, sizeof(data));
449 }
450
451 case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE):
452 /* Hubs are allowed to have only one interface */
453 if (index != 0)
454 TRANSFER_END(request, EINVAL);
455 /* Fall through, as the answer will be the same: 0x0000 */
456 case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT):
457 /* Endpoint 0 (default control) and 1 (interrupt) */
458 if (index >= 2)
459 TRANSFER_END(request, EINVAL);
460
461 if (request->buffer_size < 2) {
462 usb_log_error("Buffer(%zu) too small for hub generic "
463 "get status request.\n", request->buffer_size);
464 TRANSFER_END(request, EOVERFLOW);
465 } else {
466 /* Endpoints are OK. (We don't halt) */
467 const uint16_t data = 0;
468 TRANSFER_END_DATA(request, &data, sizeof(data));
469 }
470
471 default:
472 usb_log_error("Unsupported GET_STATUS request.\n");
473 TRANSFER_END(request, ENOTSUP);
474 }
475
476}
477
478/**
479 * Create answer to a descriptor request.
480 *
481 * This might be a request for standard (configuration, device, endpoint or
482 * interface) or device specific (hub) descriptor.
483 * @param instance Root hub instance
484 * @param request Structure containing both request and response information
485 * @return Error code
486 */
487void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request)
488{
489 assert(instance);
490 assert(request);
491
492 usb_device_request_setup_packet_t *setup_request =
493 (usb_device_request_setup_packet_t *) request->setup_buffer;
494 /* "The wValue field specifies the descriptor type in the high byte
495 * and the descriptor index in the low byte (refer to Table 9-5)." */
496 const int desc_type = uint16_usb2host(setup_request->value) >> 8;
497 switch (desc_type)
498 {
499 case USB_DESCTYPE_HUB:
500 usb_log_debug2("USB_DESCTYPE_HUB\n");
501 /* Hub descriptor was generated locally.
502 * Class specific request. */
503 TRANSFER_END_DATA(request, instance->descriptors.hub,
504 instance->hub_descriptor_size);
505
506 case USB_DESCTYPE_DEVICE:
507 usb_log_debug2("USB_DESCTYPE_DEVICE\n");
508 /* Device descriptor is shared
509 * (No one should ask for it, as the device is already setup)
510 * Standard USB device request. */
511 TRANSFER_END_DATA(request, &ohci_rh_device_descriptor,
512 sizeof(ohci_rh_device_descriptor));
513
514 case USB_DESCTYPE_CONFIGURATION:
515 usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
516 /* Start with configuration and add others depending on
517 * request size. Standard USB request. */
518 TRANSFER_END_DATA(request, &instance->descriptors,
519 instance->descriptors.configuration.total_length);
520
521 case USB_DESCTYPE_INTERFACE:
522 usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
523 /* Use local interface descriptor. There is one and it
524 * might be modified. Hub driver should not ask or this
525 * descriptor as it is not part of standard requests set. */
526 TRANSFER_END_DATA(request, &instance->descriptors.interface,
527 sizeof(instance->descriptors.interface));
528
529 case USB_DESCTYPE_ENDPOINT:
530 /* Use local endpoint descriptor. There is one
531 * it might have max_packet_size field modified. Hub driver
532 * should not ask for this descriptor as it is not part
533 * of standard requests set. */
534 usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
535 TRANSFER_END_DATA(request, &instance->descriptors.endpoint,
536 sizeof(instance->descriptors.endpoint));
537
538 default:
539 usb_log_debug2("USB_DESCTYPE_EINVAL %d \n"
540 "\ttype %d\n\trequest %d\n\tvalue "
541 "%d\n\tindex %d\n\tlen %d\n ",
542 setup_request->value,
543 setup_request->request_type, setup_request->request,
544 desc_type, setup_request->index,
545 setup_request->length);
546 TRANSFER_END(request, EINVAL);
547 }
548
549 TRANSFER_END(request, ENOTSUP);
550}
551
552/**
553 * process feature-enabling request on hub
554 *
555 * @param instance root hub instance
556 * @param feature feature selector
557 * @param port port number, counted from 1
558 * @param enable enable or disable the specified feature
559 * @return error code
560 */
561int set_feature_port(const rh_t *instance, uint16_t feature, uint16_t port)
562{
563 assert(instance);
564
565 if (port < 1 || port > instance->port_count)
566 return EINVAL;
567
568 switch (feature)
569 {
570 case USB_HUB_FEATURE_PORT_POWER: /*8*/
571 {
572 const uint32_t rhda =
573 OHCI_RD(instance->registers->rh_desc_a);
574 /* No power switching */
575 if (rhda & RHDA_NPS_FLAG)
576 return EOK;
577 /* Ganged power switching, one port powers all */
578 if (!(rhda & RHDA_PSM_FLAG)) {
579 OHCI_WR(instance->registers->rh_status,
580 RHS_SET_GLOBAL_POWER);
581 return EOK;
582 }
583 }
584 /* Fall through */
585 case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
586 case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
587 case USB_HUB_FEATURE_PORT_RESET: /*4*/
588 usb_log_debug2("Setting port POWER, ENABLE, SUSPEND or RESET "
589 "on port %zu.\n", port);
590 OHCI_WR(instance->registers->rh_port_status[port - 1],
591 1 << feature);
592 return EOK;
593 default:
594 return ENOTSUP;
595 }
596}
597
598/**
599 * Process feature clear request.
600 *
601 * @param instance root hub instance
602 * @param feature feature selector
603 * @param port port number, counted from 1
604 * @param enable enable or disable the specified feature
605 * @return error code
606 */
607int clear_feature_port(const rh_t *instance, uint16_t feature, uint16_t port)
608{
609 assert(instance);
610
611 if (port < 1 || port > instance->port_count)
612 return EINVAL;
613
614 /* Enabled features to clear: see page 269 of USB specs */
615 switch (feature)
616 {
617 case USB_HUB_FEATURE_PORT_POWER: /*8*/
618 {
619 const uint32_t rhda =
620 OHCI_RD(instance->registers->rh_desc_a);
621 /* No power switching */
622 if (rhda & RHDA_NPS_FLAG)
623 return ENOTSUP;
624 /* Ganged power switching, one port powers all */
625 if (!(rhda & RHDA_PSM_FLAG)) {
626 OHCI_WR(instance->registers->rh_status,
627 RHS_CLEAR_GLOBAL_POWER);
628 return EOK;
629 }
630 OHCI_WR(instance->registers->rh_port_status[port - 1],
631 RHPS_CLEAR_PORT_POWER);
632 return EOK;
633 }
634
635 case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
636 OHCI_WR(instance->registers->rh_port_status[port - 1],
637 RHPS_CLEAR_PORT_ENABLE);
638 return EOK;
639
640 case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
641 OHCI_WR(instance->registers->rh_port_status[port - 1],
642 RHPS_CLEAR_PORT_SUSPEND);
643 return EOK;
644
645 case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
646 case USB_HUB_FEATURE_C_PORT_ENABLE: /*17*/
647 case USB_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
648 case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
649 case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
650 usb_log_debug2("Clearing port C_CONNECTION, C_ENABLE, "
651 "C_SUSPEND, C_OC or C_RESET on port %zu.\n", port);
652 /* Bit offsets correspond to the feature number */
653 OHCI_WR(instance->registers->rh_port_status[port - 1],
654 1 << feature);
655 return EOK;
656
657 default:
658 return ENOTSUP;
659 }
660}
661
662/**
663 * process one of requests that do not request nor carry additional data
664 *
665 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
666 * USB_DEVREQ_SET_ADDRESS.
667 * @param instance root hub instance
668 * @param request structure containing both request and response information
669 * @return error code
670 */
671void set_feature(const rh_t *instance, usb_transfer_batch_t *request)
672{
673 assert(instance);
674 assert(request);
675
676 usb_device_request_setup_packet_t *setup_request =
677 (usb_device_request_setup_packet_t *) request->setup_buffer;
678 switch (setup_request->request_type)
679 {
680 case USB_HUB_REQ_TYPE_SET_PORT_FEATURE:
681 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
682 const int ret = set_feature_port(instance,
683 setup_request->value, setup_request->index);
684 TRANSFER_END(request, ret);
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 TRANSFER_END(request, ENOTSUP);
692 //TODO: Consider standard USB requests: REMOTE WAKEUP, ENDPOINT STALL
693 default:
694 usb_log_error("Invalid set feature request type: %d\n",
695 setup_request->request_type);
696 TRANSFER_END(request, ENOTSUP);
697 }
698}
699
700/**
701 * process one of requests that do not request nor carry additional data
702 *
703 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
704 * USB_DEVREQ_SET_ADDRESS.
705 * @param instance root hub instance
706 * @param request structure containing both request and response information
707 * @return error code
708 */
709void clear_feature(const rh_t *instance, usb_transfer_batch_t *request)
710{
711 assert(instance);
712 assert(request);
713
714 usb_device_request_setup_packet_t *setup_request =
715 (usb_device_request_setup_packet_t *) request->setup_buffer;
716
717 switch (setup_request->request_type)
718 {
719 case USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE:
720 usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE\n");
721 const int ret = clear_feature_port(instance,
722 setup_request->value, setup_request->index);
723 TRANSFER_END(request, ret);
724
725 case USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE:
726 usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE\n");
727 /*
728 * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
729 * C_HUB_OVER_CURRENT are supported.
730 * C_HUB_OVER_CURRENT is represented by OHCI RHS_OCIC_FLAG.
731 * C_HUB_LOCAL_POWER is not supported
732 * as root hubs do not support local power status feature.
733 * (OHCI pg. 127) */
734 if (uint16_usb2host(setup_request->value)
735 == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
736 OHCI_WR(instance->registers->rh_status, RHS_OCIC_FLAG);
737 TRANSFER_END(request, EOK);
738 }
739 //TODO: Consider standard USB requests: REMOTE WAKEUP, ENDPOINT STALL
740 default:
741 usb_log_error("Invalid clear feature request type: %d\n",
742 setup_request->request_type);
743 TRANSFER_END(request, ENOTSUP);
744 }
745}
746
747/**
748 * Process hub control request.
749 *
750 * If needed, writes answer into the request structure.
751 * Request can be one of
752 * USB_DEVREQ_GET_STATUS,
753 * USB_DEVREQ_GET_DESCRIPTOR,
754 * USB_DEVREQ_GET_CONFIGURATION,
755 * USB_DEVREQ_CLEAR_FEATURE,
756 * USB_DEVREQ_SET_FEATURE,
757 * USB_DEVREQ_SET_ADDRESS,
758 * USB_DEVREQ_SET_DESCRIPTOR or
759 * USB_DEVREQ_SET_CONFIGURATION.
760 *
761 * @param instance root hub instance
762 * @param request structure containing both request and response information
763 * @return error code
764 */
765void control_request(rh_t *instance, usb_transfer_batch_t *request)
766{
767 assert(instance);
768 assert(request);
769
770 if (!request->setup_buffer) {
771 usb_log_error("Root hub received empty transaction!");
772 TRANSFER_END(request, EBADMEM);
773 }
774
775 if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
776 usb_log_error("Setup packet too small\n");
777 TRANSFER_END(request, EOVERFLOW);
778 }
779
780 usb_log_debug2("CTRL packet: %s.\n",
781 usb_debug_str_buffer((uint8_t *) request->setup_buffer, 8, 8));
782 usb_device_request_setup_packet_t *setup_request =
783 (usb_device_request_setup_packet_t *) request->setup_buffer;
784 switch (setup_request->request)
785 {
786 case USB_DEVREQ_GET_STATUS:
787 usb_log_debug("USB_DEVREQ_GET_STATUS\n");
788 get_status(instance, request);
789 break;
790
791 case USB_DEVREQ_GET_DESCRIPTOR:
792 usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
793 get_descriptor(instance, request);
794 break;
795
796 case USB_DEVREQ_GET_CONFIGURATION:
797 usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
798 if (request->buffer_size == 0)
799 TRANSFER_END(request, EOVERFLOW);
800 const uint8_t config = 1;
801 TRANSFER_END_DATA(request, &config, sizeof(config));
802
803 case USB_DEVREQ_CLEAR_FEATURE:
804 usb_log_debug2("USB_DEVREQ_CLEAR_FEATURE\n");
805 clear_feature(instance, request);
806 break;
807
808 case USB_DEVREQ_SET_FEATURE:
809 usb_log_debug2("USB_DEVREQ_SET_FEATURE\n");
810 set_feature(instance, request);
811 break;
812
813 case USB_DEVREQ_SET_ADDRESS:
814 usb_log_debug("USB_DEVREQ_SET_ADDRESS: %u\n",
815 setup_request->value);
816 if (uint16_usb2host(setup_request->value) > 127)
817 TRANSFER_END(request, EINVAL);
818
819 instance->address = uint16_usb2host(setup_request->value);
820 TRANSFER_END(request, EOK);
821
822 case USB_DEVREQ_SET_CONFIGURATION:
823 usb_log_debug("USB_DEVREQ_SET_CONFIGURATION: %u\n",
824 uint16_usb2host(setup_request->value));
825 /* We have only one configuration, it's number is 1 */
826 if (uint16_usb2host(setup_request->value) != 1)
827 TRANSFER_END(request, EINVAL);
828 TRANSFER_END(request, EOK);
829
830 /* Both class specific and std is optional for hubs */
831 case USB_DEVREQ_SET_DESCRIPTOR:
832 /* Hubs have only one interface GET/SET is not supported */
833 case USB_DEVREQ_GET_INTERFACE:
834 case USB_DEVREQ_SET_INTERFACE:
835 default:
836 /* Hub class GET_STATE(2) falls in here too. */
837 usb_log_error("Received unsupported request: %d.\n",
838 setup_request->request);
839 TRANSFER_END(request, ENOTSUP);
840 }
841}
842/**
843 * @}
844 */
Note: See TracBrowser for help on using the repository browser.