source: mainline/uspace/drv/bus/usb/ohci/root_hub.c@ 1368a6b

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

OHCI: Root hub: Nice and clean initialization of descriptors.

Last malloc is out.

  • Property mode set to 100644
File size: 29.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/dev/driver.h>
43#include "ohci_regs.h"
44
45#include <usb/dev/request.h>
46#include <usb/classes/hub.h>
47
48/**
49 * standart device descriptor for ohci root hub
50 */
51static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
52 .configuration_count = 1,
53 .descriptor_type = USB_DESCTYPE_DEVICE,
54 .device_class = USB_CLASS_HUB,
55 .device_protocol = 0,
56 .device_subclass = 0,
57 .device_version = 0,
58 .length = sizeof (usb_standard_device_descriptor_t),
59 .max_packet_size = 8,
60 .vendor_id = 0x16db,
61 .product_id = 0x0001,
62 .str_serial_number = 0,
63 .usb_spec_version = 0x110,
64};
65
66/**
67 * standart configuration descriptor with filled common values
68 * for ohci root hubs
69 */
70static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
71 .attributes = 1 << 7,
72 .configuration_number = 1,
73 .descriptor_type = USB_DESCTYPE_CONFIGURATION,
74 .interface_count = 1,
75 .length = sizeof (usb_standard_configuration_descriptor_t),
76 .max_power = 0, /* root hubs don't need no power */
77 .str_configuration = 0,
78};
79
80/**
81 * standart ohci root hub interface descriptor
82 */
83static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
84 .alternate_setting = 0,
85 .descriptor_type = USB_DESCTYPE_INTERFACE,
86 .endpoint_count = 1,
87 .interface_class = USB_CLASS_HUB,
88 .interface_number = 1,
89 .interface_protocol = 0,
90 .interface_subclass = 0,
91 .length = sizeof (usb_standard_interface_descriptor_t),
92 .str_interface = 0,
93};
94
95/**
96 * standart ohci root hub endpoint descriptor
97 */
98static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
99 .attributes = USB_TRANSFER_INTERRUPT,
100 .descriptor_type = USB_DESCTYPE_ENDPOINT,
101 .endpoint_address = 1 + (1 << 7),
102 .length = sizeof (usb_standard_endpoint_descriptor_t),
103 .max_packet_size = 8,
104 .poll_interval = 255,
105};
106
107/**
108 * bitmask of hub features that are valid to be cleared
109 */
110static const uint32_t hub_clear_feature_valid_mask =
111 RHS_OCIC_FLAG |
112 RHS_CLEAR_PORT_POWER;
113
114/**
115 * bitmask of hub features that are cleared by writing 1 (and not 0)
116 */
117static const uint32_t hub_clear_feature_by_writing_one_mask =
118 RHS_CLEAR_PORT_POWER;
119
120/**
121 * bitmask of hub features that are valid to be set
122 */
123static const uint32_t hub_set_feature_valid_mask =
124 RHS_LPSC_FLAG |
125 RHS_OCIC_FLAG;
126
127/**
128 * bitmask of hub features that are set by writing 1 and cleared by writing 0
129 */
130static const uint32_t hub_set_feature_direct_mask =
131 RHS_SET_PORT_POWER;
132
133/**
134 * bitmask of port features that are valid to be set
135 */
136static const uint32_t port_set_feature_valid_mask =
137 RHPS_SET_PORT_ENABLE |
138 RHPS_SET_PORT_SUSPEND |
139 RHPS_SET_PORT_RESET |
140 RHPS_SET_PORT_POWER;
141
142/**
143 * bitmask of port features that can be cleared
144 */
145static const uint32_t port_clear_feature_valid_mask =
146 RHPS_CCS_FLAG |
147 RHPS_SET_PORT_SUSPEND |
148 RHPS_POCI_FLAG |
149 RHPS_SET_PORT_POWER |
150 RHPS_CSC_FLAG |
151 RHPS_PESC_FLAG |
152 RHPS_PSSC_FLAG |
153 RHPS_OCIC_FLAG |
154 RHPS_PRSC_FLAG;
155
156//note that USB_HUB_FEATURE_PORT_POWER bit is translated into
157//USB_HUB_FEATURE_PORT_LOW_SPEED for port set feature request
158
159/**
160 * bitmask with port status changes
161 */
162static const uint32_t port_status_change_mask = RHPS_CHANGE_WC_MASK;
163
164static void create_serialized_hub_descriptor(rh_t *instance);
165
166static int rh_init_descriptors(rh_t *instance);
167
168static void create_interrupt_mask_in_instance(rh_t *instance);
169
170static int process_get_port_status_request(
171 rh_t *instance, uint16_t port, usb_transfer_batch_t *request);
172
173static int process_get_hub_status_request(
174 rh_t *instance, usb_transfer_batch_t *request);
175
176static int process_get_status_request(
177 rh_t *instance, usb_transfer_batch_t *request);
178
179
180static int process_get_descriptor_request(
181 rh_t *instance, usb_transfer_batch_t *request);
182
183static int process_get_configuration_request(
184 rh_t *instance, usb_transfer_batch_t *request);
185
186static int process_hub_feature_set_request(rh_t *instance, uint16_t feature);
187
188static int process_hub_feature_clear_request(
189 rh_t *instance, uint16_t feature);
190
191static int process_port_feature_set_request(
192 rh_t *instance, uint16_t feature, uint16_t port);
193
194static int process_port_feature_clear_request(
195 rh_t *instance, uint16_t feature, uint16_t port);
196
197static int process_request_with_input(
198 rh_t *instance, usb_transfer_batch_t *request);
199
200static int process_request_with_output(
201 rh_t *instance, usb_transfer_batch_t *request);
202
203static int process_request_without_data(
204 rh_t *instance, usb_transfer_batch_t *request);
205
206static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
207
208static int process_interrupt_mask_in_instance(
209 rh_t *instance, usb_transfer_batch_t *request);
210
211static bool is_zeros(const void *buffer, size_t size);
212
213/**
214 * Register address to this device
215 *
216 * @param instance Root hub instance
217 * @param address New address
218 * @return Error code
219 */
220static inline int process_address_set_request(rh_t *instance, uint16_t address)
221 { return ENOTSUP; }
222
223/** Root hub initialization
224 * @return Error code.
225 */
226int rh_init(rh_t *instance, ohci_regs_t *regs)
227{
228 assert(instance);
229 assert(regs);
230
231 instance->registers = regs;
232 instance->port_count =
233 (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
234 if (instance->port_count > 15) {
235 usb_log_error("OHCI specification does not allow more than 15"
236 " ports. Max 15 ports will be used");
237 instance->port_count = 15;
238 }
239
240 /* Don't forget the hub status bit and round up */
241 instance->interrupt_mask_size = (instance->port_count + 1 + 8) / 8;
242 instance->interrupt_buffer[0] = 0;
243 instance->interrupt_buffer[1] = 0;
244 instance->unfinished_interrupt_transfer = NULL;
245
246 /* Set port power mode to no power-switching. (always on) */
247 instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
248
249 int ret = rh_init_descriptors(instance);
250 if (ret != EOK) {
251 return ret;
252 }
253
254 usb_log_info("Root hub (%zu ports) initialized.\n",
255 instance->port_count);
256
257 return EOK;
258}
259/*----------------------------------------------------------------------------*/
260/**
261 * Process root hub request.
262 *
263 * @param instance Root hub instance
264 * @param request Structure containing both request and response information
265 * @return Error code
266 */
267int rh_request(rh_t *instance, usb_transfer_batch_t *request)
268{
269 assert(instance);
270 assert(request);
271
272 int opResult;
273 switch (request->ep->transfer_type)
274 {
275 case USB_TRANSFER_CONTROL:
276 usb_log_debug("Root hub got CONTROL packet\n");
277 opResult = process_ctrl_request(instance, request);
278 usb_transfer_batch_finish_error(request, opResult);
279 break;
280 case USB_TRANSFER_INTERRUPT:
281 usb_log_debug("Root hub got INTERRUPT packet\n");
282 create_interrupt_mask_in_instance(instance);
283 if (is_zeros(instance->interrupt_buffer,
284 instance->interrupt_mask_size)) {
285 usb_log_debug("No changes..\n");
286 instance->unfinished_interrupt_transfer = request;
287 //will be finished later
288 } else {
289 usb_log_debug("Processing changes..\n");
290 process_interrupt_mask_in_instance(instance, request);
291 }
292 break;
293 default:
294 usb_log_error("Root hub got unsupported request.\n");
295 usb_transfer_batch_finish_error(request, EINVAL);
296 }
297 return EOK;
298}
299/*----------------------------------------------------------------------------*/
300/**
301 * process interrupt on a hub
302 *
303 * If there is no pending interrupt transfer, nothing happens.
304 * @param instance
305 */
306void rh_interrupt(rh_t *instance)
307{
308 if (!instance->unfinished_interrupt_transfer)
309 return;
310
311 usb_log_debug("Finalizing interrupt transfer\n");
312 create_interrupt_mask_in_instance(instance);
313 process_interrupt_mask_in_instance(instance,
314 instance->unfinished_interrupt_transfer);
315}
316/*----------------------------------------------------------------------------*/
317/**
318 * Create hub descriptor.
319 *
320 * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
321 *
322 * @param instance Root hub instance
323 * @return Error code
324 */
325void create_serialized_hub_descriptor(rh_t *instance)
326{
327 assert(instance);
328
329 const size_t bit_field_size = (instance->port_count + 1 + 7) / 8;
330 assert(bit_field_size == 2 || bit_field_size == 1);
331 /* 7 bytes + 2 port bit fields (port count + global bit) */
332 const size_t size = 7 + (bit_field_size * 2);
333 assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
334 instance->hub_descriptor_size = size;
335
336 const uint32_t hub_desc = instance->registers->rh_desc_a;
337 const uint32_t port_desc = instance->registers->rh_desc_b;
338
339 /* bDescLength */
340 instance->descriptors.hub[0] = size;
341 /* bDescriptorType */
342 instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
343 /* bNmbrPorts */
344 instance->descriptors.hub[2] = instance->port_count;
345 /* wHubCharacteristics */
346 instance->descriptors.hub[3] = 0 |
347 /* The lowest 2 bits indicate power switching mode */
348 (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
349 (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
350 /* Bit 3 indicates device type (compound device) */
351 (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
352 /* Bits 4,5 indicate over-current protection mode */
353 (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
354 (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
355
356 /* Reserved */
357 instance->descriptors.hub[4] = 0;
358 /* bPwrOn2PwrGood */
359 instance->descriptors.hub[5] =
360 (hub_desc >> RHDA_POTPGT_SHIFT) & RHDA_POTPGT_MASK;
361 /* bHubContrCurrent, root hubs don't need no power. */
362 instance->descriptors.hub[6] = 0;
363
364 /* Device Removable and some legacy 1.0 stuff*/
365 instance->descriptors.hub[7] =
366 (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK & 0xff;
367 instance->descriptors.hub[8] = 0xff;
368 if (bit_field_size == 2) {
369 instance->descriptors.hub[8] =
370 (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK >> 8;
371 instance->descriptors.hub[9] = 0xff;
372 instance->descriptors.hub[10] = 0xff;
373 }
374}
375/*----------------------------------------------------------------------------*/
376/** Initialize hub descriptors.
377 *
378 * Device and full configuration descriptor are created. These need to
379 * be initialized only once per hub.
380 * @param instance Root hub instance
381 * @return Error code
382 */
383int rh_init_descriptors(rh_t *instance)
384{
385 assert(instance);
386
387 instance->descriptors.configuration = ohci_rh_conf_descriptor;
388 instance->descriptors.interface = ohci_rh_iface_descriptor;
389 instance->descriptors.endpoint = ohci_rh_ep_descriptor;
390 create_serialized_hub_descriptor(instance);
391
392 instance->descriptors.configuration.total_length =
393 sizeof(usb_standard_configuration_descriptor_t) +
394 sizeof(usb_standard_endpoint_descriptor_t) +
395 sizeof(usb_standard_interface_descriptor_t) +
396 instance->hub_descriptor_size;
397
398 return EOK;
399}
400/*----------------------------------------------------------------------------*/
401/**
402 * Create answer to port status_request
403 *
404 * Copy content of corresponding port status register to answer buffer. The
405 * format of the port status register and port status data is the same (
406 * see OHCI root hub and USB hub documentation).
407 *
408 * @param instance Root hub instance
409 * @param port Port number, counted from 1
410 * @param request Structure containing both request and response information
411 * @return Error code
412 */
413int process_get_port_status_request(
414 rh_t *instance, uint16_t port, usb_transfer_batch_t * request)
415{
416 assert(instance);
417 assert(request);
418
419 if (port < 1 || port > instance->port_count)
420 return EINVAL;
421
422 const uint32_t data = instance->registers->rh_port_status[port - 1];
423 memcpy(request->data_buffer, &data, 4);
424 request->transfered_size = 4;
425 return EOK;
426}
427/*----------------------------------------------------------------------------*/
428/**
429 * Create answer to port status_request.
430 *
431 * This copies flags in hub status register into the buffer. The format of the
432 * status register and status message is the same, according to USB hub
433 * specification and OHCI root hub specification.
434 *
435 * @param instance Root hub instance.
436 * @param request Structure containing both request and response information.
437 * @return Error code
438 */
439int process_get_hub_status_request(
440 rh_t *instance, usb_transfer_batch_t *request)
441{
442 assert(instance);
443 assert(request);
444
445 /* bits, 0,1,16,17 -- TODO: What do they mean?? Why not 0x0303 */
446 const uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
447 const uint32_t data = mask & instance->registers->rh_status;
448 memcpy(request->data_buffer, &data, 4);
449 request->transfered_size = 4;
450
451 return EOK;
452}
453/*----------------------------------------------------------------------------*/
454/**
455 * Create answer to status request.
456 *
457 * This might be either hub status or port status request. If neither,
458 * ENOTSUP is returned.
459 * @param instance root hub instance
460 * @param request structure containing both request and response information
461 * @return error code
462 */
463int process_get_status_request(rh_t *instance, usb_transfer_batch_t *request)
464{
465 assert(instance);
466 assert(request);
467
468 const usb_device_request_setup_packet_t *request_packet =
469 (usb_device_request_setup_packet_t*)request->setup_buffer;
470
471 const usb_hub_bm_request_type_t request_type =
472 request_packet->request_type;
473
474 if (request->buffer_size < 4) {
475 usb_log_error("Buffer too small for get status request.\n");
476 return EOVERFLOW;
477 }
478
479 if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
480 return process_get_hub_status_request(instance, request);
481 if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
482 return process_get_port_status_request(instance,
483 request_packet->index, request);
484
485 return ENOTSUP;
486}
487/*----------------------------------------------------------------------------*/
488/**
489 * Create bitmap of changes to answer status interrupt.
490 *
491 * Result contains bitmap where bit 0 indicates change on hub and
492 * bit i indicates change on i`th port (i>0). For more info see
493 * Hub and Port status bitmap specification in USB specification
494 * (chapter 11.13.4).
495 * Uses instance`s interrupt buffer to store the interrupt information.
496 * @param instance root hub instance
497 */
498void create_interrupt_mask_in_instance(rh_t *instance)
499{
500 assert(instance);
501
502 uint8_t * bitmap = (uint8_t*) (instance->interrupt_buffer);
503 uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
504 | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
505 bzero(bitmap, instance->interrupt_mask_size);
506 if ((instance->registers->rh_status & mask) != 0) {
507 bitmap[0] = 1;
508 }
509 mask = port_status_change_mask;
510 size_t port = 1;
511 for (; port <= instance->port_count; ++port) {
512 if ((mask & instance->registers->rh_port_status[port - 1]) != 0) {
513
514 bitmap[(port) / 8] += 1 << (port % 8);
515 }
516 }
517}
518/*----------------------------------------------------------------------------*/
519/**
520 * Create answer to a descriptor request.
521 *
522 * This might be a request for standard (configuration, device, endpoint or
523 * interface) or device specific (hub) descriptor.
524 * @param instance Root hub instance
525 * @param request Structure containing both request and response information
526 * @return Error code
527 */
528int process_get_descriptor_request(
529 rh_t *instance, usb_transfer_batch_t *request)
530{
531 assert(instance);
532 assert(request);
533
534 const usb_device_request_setup_packet_t *setup_request =
535 (usb_device_request_setup_packet_t *) request->setup_buffer;
536 size_t size;
537 const void * result_descriptor = NULL;
538 const uint16_t setup_request_value = setup_request->value_high;
539 //(setup_request->value_low << 8);
540 switch (setup_request_value)
541 {
542 case USB_DESCTYPE_HUB:
543 usb_log_debug2("USB_DESCTYPE_HUB\n");
544 result_descriptor = instance->descriptors.hub;
545 size = instance->hub_descriptor_size;
546 break;
547
548 case USB_DESCTYPE_DEVICE:
549 usb_log_debug2("USB_DESCTYPE_DEVICE\n");
550 result_descriptor = &ohci_rh_device_descriptor;
551 size = sizeof(ohci_rh_device_descriptor);
552 break;
553
554 case USB_DESCTYPE_CONFIGURATION:
555 usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
556 result_descriptor = &instance->descriptors;
557 size = instance->descriptors.configuration.total_length;
558 break;
559
560 case USB_DESCTYPE_INTERFACE:
561 usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
562 result_descriptor = &ohci_rh_iface_descriptor;
563 size = sizeof(ohci_rh_iface_descriptor);
564 break;
565
566 case USB_DESCTYPE_ENDPOINT:
567 usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
568 result_descriptor = &ohci_rh_ep_descriptor;
569 size = sizeof(ohci_rh_ep_descriptor);
570 break;
571
572 default:
573 usb_log_debug2("USB_DESCTYPE_EINVAL %d \n"
574 "\ttype %d\n\trequest %d\n\tvalue "
575 "%d\n\tindex %d\n\tlen %d\n ",
576 setup_request->value,
577 setup_request->request_type, setup_request->request,
578 setup_request_value, setup_request->index,
579 setup_request->length);
580 return EINVAL;
581 }
582 if (request->buffer_size < size) {
583 size = request->buffer_size;
584 }
585 memcpy(request->data_buffer, result_descriptor, size);
586 request->transfered_size = size;
587
588 return EOK;
589}
590/*----------------------------------------------------------------------------*/
591/**
592 * Answer to get configuration request.
593 *
594 * Root hub works independently on the configuration.
595 * Set and get configuration requests do not have any meaning,
596 * dummy values are returned.
597 *
598 * @param instance Root hub instance
599 * @param request Structure containing both request and response information
600 * @return Error code
601 */
602int process_get_configuration_request(
603 rh_t *instance, usb_transfer_batch_t *request)
604{
605 assert(request);
606
607 if (request->buffer_size != 1)
608 return EINVAL;
609 request->data_buffer[0] = 1;
610 request->transfered_size = 1;
611
612 return EOK;
613}
614/*----------------------------------------------------------------------------*/
615/**
616 * process feature-enabling request on hub
617 *
618 * @param instance root hub instance
619 * @param feature feature selector
620 * @return error code
621 */
622static int process_hub_feature_set_request(rh_t *instance,
623 uint16_t feature) {
624 if (!((1 << feature) & hub_set_feature_valid_mask))
625 return EINVAL;
626 if (feature == USB_HUB_FEATURE_C_HUB_LOCAL_POWER)
627 feature = USB_HUB_FEATURE_C_HUB_LOCAL_POWER << 16;
628 instance->registers->rh_status =
629 (instance->registers->rh_status | (1 << feature))
630 & (~hub_clear_feature_by_writing_one_mask);
631
632 return EOK;
633}
634/*----------------------------------------------------------------------------*/
635/**
636 * process feature-disabling request on hub
637 *
638 * @param instance root hub instance
639 * @param feature feature selector
640 * @return error code
641 */
642int process_hub_feature_clear_request(rh_t *instance, uint16_t feature)
643{
644 assert(instance);
645
646 if (!((1 << feature) & hub_clear_feature_valid_mask))
647 return EINVAL;
648
649 //is the feature cleared directly?
650 if ((1 << feature) & hub_set_feature_direct_mask) {
651 instance->registers->rh_status =
652 (instance->registers->rh_status & (~(1 << feature)))
653 & (~hub_clear_feature_by_writing_one_mask);
654 } else {//the feature is cleared by writing '1'
655
656 instance->registers->rh_status =
657 (instance->registers->rh_status
658 & (~hub_clear_feature_by_writing_one_mask))
659 | (1 << feature);
660 }
661 return EOK;
662}
663/*----------------------------------------------------------------------------*/
664/**
665 * process feature-enabling request on hub
666 *
667 * @param instance root hub instance
668 * @param feature feature selector
669 * @param port port number, counted from 1
670 * @param enable enable or disable the specified feature
671 * @return error code
672 */
673int process_port_feature_set_request(
674 rh_t *instance, uint16_t feature, uint16_t port)
675{
676 assert(instance);
677
678 if (!((1 << feature) & port_set_feature_valid_mask))
679 return EINVAL;
680 if (port < 1 || port > instance->port_count)
681 return EINVAL;
682
683 instance->registers->rh_port_status[port - 1] =
684 (instance->registers->rh_port_status[port - 1] | (1 << feature))
685 & (~port_clear_feature_valid_mask);
686 return EOK;
687}
688/*----------------------------------------------------------------------------*/
689/**
690 * process feature-disabling request on hub
691 *
692 * @param instance root hub instance
693 * @param feature feature selector
694 * @param port port number, counted from 1
695 * @param enable enable or disable the specified feature
696 * @return error code
697 */
698int process_port_feature_clear_request(
699 rh_t *instance, uint16_t feature, uint16_t port)
700{
701 assert(instance);
702
703 if (!((1 << feature) & port_clear_feature_valid_mask))
704 return EINVAL;
705 if (port < 1 || port > instance->port_count)
706 return EINVAL;
707
708 /* Some weird stuff... */
709 if (feature == USB_HUB_FEATURE_PORT_POWER)
710 feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
711 if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
712 feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
713
714 instance->registers->rh_port_status[port - 1] =
715 (instance->registers->rh_port_status[port - 1]
716 & (~port_clear_feature_valid_mask))
717 | (1 << feature);
718
719 return EOK;
720}
721/*----------------------------------------------------------------------------*/
722/**
723 * process one of requests that requere output data
724 *
725 * Request can be one of USB_DEVREQ_GET_STATUS, USB_DEVREQ_GET_DESCRIPTOR or
726 * USB_DEVREQ_GET_CONFIGURATION.
727 * @param instance root hub instance
728 * @param request structure containing both request and response information
729 * @return error code
730 */
731int process_request_with_output(rh_t *instance, usb_transfer_batch_t *request)
732{
733 assert(instance);
734 assert(request);
735
736 const usb_device_request_setup_packet_t *setup_request =
737 (usb_device_request_setup_packet_t *) request->setup_buffer;
738 switch (setup_request->request)
739 {
740 case USB_DEVREQ_GET_STATUS:
741 usb_log_debug("USB_DEVREQ_GET_STATUS\n");
742 return process_get_status_request(instance, request);
743 case USB_DEVREQ_GET_DESCRIPTOR:
744 usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
745 return process_get_descriptor_request(instance, request);
746 case USB_DEVREQ_GET_CONFIGURATION:
747 usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
748 return process_get_configuration_request(instance, request);
749 }
750 return ENOTSUP;
751}
752/*----------------------------------------------------------------------------*/
753/**
754 * process one of requests that carry input data
755 *
756 * Request can be one of USB_DEVREQ_SET_DESCRIPTOR or
757 * USB_DEVREQ_SET_CONFIGURATION.
758 * @param instance root hub instance
759 * @param request structure containing both request and response information
760 * @return error code
761 */
762int process_request_with_input(rh_t *instance, usb_transfer_batch_t *request)
763{
764 assert(instance);
765 assert(request);
766
767 const usb_device_request_setup_packet_t *setup_request =
768 (usb_device_request_setup_packet_t *) request->setup_buffer;
769 request->transfered_size = 0;
770 if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
771 //set and get configuration requests do not have any meaning,
772 //only dummy values are returned
773 return EOK;
774 }
775 /* USB_DEVREQ_SET_DESCRIPTOR is also not supported */
776 return ENOTSUP;
777}
778/*----------------------------------------------------------------------------*/
779/**
780 * process one of requests that do not request nor carry additional data
781 *
782 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
783 * USB_DEVREQ_SET_ADDRESS.
784 * @param instance root hub instance
785 * @param request structure containing both request and response information
786 * @return error code
787 */
788int process_request_without_data(rh_t *instance, usb_transfer_batch_t *request)
789{
790 assert(instance);
791 assert(request);
792
793 const usb_device_request_setup_packet_t *setup_request =
794 (usb_device_request_setup_packet_t *) request->setup_buffer;
795 request->transfered_size = 0;
796 const int request_type = setup_request->request_type;
797 switch (setup_request->request)
798 {
799 case USB_DEVREQ_CLEAR_FEATURE:
800 if (request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
801 usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
802 return process_hub_feature_clear_request(instance,
803 setup_request->value);
804 }
805 if (request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
806 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
807 return process_port_feature_clear_request(instance,
808 setup_request->value, setup_request->index);
809 }
810 usb_log_error("Invalid HUB clear feature request type: %d\n",
811 request_type);
812 return EINVAL;
813
814 case USB_DEVREQ_SET_FEATURE:
815 if (request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
816 usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
817 return process_hub_feature_set_request(instance,
818 setup_request->value);
819 }
820 if (request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
821 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
822 return process_port_feature_set_request(instance,
823 setup_request->value, setup_request->index);
824 }
825 usb_log_error("Invalid HUB set feature request type: %d\n",
826 request_type);
827 return EINVAL;
828
829 case USB_DEVREQ_SET_ADDRESS:
830 usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
831 return process_address_set_request(instance,
832 setup_request->value);
833
834 default:
835 usb_log_error("Invalid HUB request: %d\n",
836 setup_request->request);
837 return ENOTSUP;
838 }
839}
840/*----------------------------------------------------------------------------*/
841/**
842 * Process hub control request.
843 *
844 * If needed, writes answer into the request structure.
845 * Request can be one of
846 * USB_DEVREQ_GET_STATUS,
847 * USB_DEVREQ_GET_DESCRIPTOR,
848 * USB_DEVREQ_GET_CONFIGURATION,
849 * USB_DEVREQ_CLEAR_FEATURE,
850 * USB_DEVREQ_SET_FEATURE,
851 * USB_DEVREQ_SET_ADDRESS,
852 * USB_DEVREQ_SET_DESCRIPTOR or
853 * USB_DEVREQ_SET_CONFIGURATION.
854 *
855 * @param instance root hub instance
856 * @param request structure containing both request and response information
857 * @return error code
858 */
859int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request)
860{
861 assert(instance);
862 assert(request);
863
864 if (!request->setup_buffer) {
865 usb_log_error("Root hub received empty transaction!");
866 return EINVAL;
867 }
868 if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
869 usb_log_error("Setup packet too small\n");
870 return EOVERFLOW;
871 }
872 usb_log_debug2("CTRL packet: %s.\n",
873 usb_debug_str_buffer((uint8_t *) request->setup_buffer, 8, 8));
874 const usb_device_request_setup_packet_t *setup_request =
875 (usb_device_request_setup_packet_t *) request->setup_buffer;
876 switch (setup_request->request)
877 {
878 case USB_DEVREQ_GET_STATUS:
879 case USB_DEVREQ_GET_DESCRIPTOR:
880 case USB_DEVREQ_GET_CONFIGURATION:
881 usb_log_debug2("Processing request with output\n");
882 return process_request_with_output(instance, request);
883 case USB_DEVREQ_CLEAR_FEATURE:
884 case USB_DEVREQ_SET_FEATURE:
885 case USB_DEVREQ_SET_ADDRESS:
886 usb_log_debug2("Processing request without "
887 "additional data\n");
888 return process_request_without_data(instance, request);
889 case USB_DEVREQ_SET_DESCRIPTOR:
890 case USB_DEVREQ_SET_CONFIGURATION:
891 usb_log_debug2("Processing request with input\n");
892 return process_request_with_input(instance, request);
893 default:
894 usb_log_error("Received unsupported request: %d.\n",
895 setup_request->request);
896 return ENOTSUP;
897 }
898}
899/*----------------------------------------------------------------------------*/
900/**
901 * process hanging interrupt request
902 *
903 * If an interrupt transfer has been received and there was no change,
904 * the driver stores the transfer information and waits for change to occur.
905 * This routine is called when that happens and it finalizes the interrupt
906 * transfer.
907 *
908 * @param instance hub instance
909 * @param request batch request to be processed
910 *
911 * @return
912 */
913int process_interrupt_mask_in_instance(
914 rh_t *instance, usb_transfer_batch_t *request)
915{
916 assert(instance);
917 assert(request);
918
919 memcpy(request->data_buffer, instance->interrupt_buffer,
920 instance->interrupt_mask_size);
921 request->transfered_size = instance->interrupt_mask_size;
922 instance->unfinished_interrupt_transfer = NULL;
923 usb_transfer_batch_finish_error(request, EOK);
924
925 return EOK;
926}
927/*----------------------------------------------------------------------------*/
928/**
929 * return whether the buffer is full of zeros
930 *
931 * Convenience function.
932 * @param buffer
933 * @param size
934 * @return
935 */
936bool is_zeros(const void *buffer, size_t size)
937{
938 if (!buffer) return true;
939 const char * const end = buffer + size;
940 const char *data = buffer;
941 for (; data < end; ++data) {
942 if (*data)
943 return false;
944 }
945 return true;
946}
947
948/**
949 * @}
950 */
Note: See TracBrowser for help on using the repository browser.