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

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

OHCI: Root hub: Make descriptor generation a bit cleaner.

Does not change functionality.

  • Property mode set to 100644
File size: 29.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
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 = 100,
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 int 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
230 instance->registers = regs;
231 instance->port_count =
232 (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
233 if (instance->port_count > 15) {
234 usb_log_error("OHCI specification does not allow more than 15"
235 " ports. Max 15 ports will be used");
236 instance->port_count = 15;
237 }
238
239 int ret = rh_init_descriptors(instance);
240 if (ret != EOK) {
241 return ret;
242 }
243 /* Don't forget the hub status bit and round up */
244 instance->interrupt_mask_size = (instance->port_count + 1 + 8) / 8;
245 instance->interrupt_buffer[0] = 0;
246 instance->interrupt_buffer[1] = 0;
247 instance->unfinished_interrupt_transfer = NULL;
248
249 /* Set port power mode to no-power-switching. */
250 instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
251
252 usb_log_info("Root hub (%zu ports) initialized.\n",
253 instance->port_count);
254
255 return EOK;
256}
257/*----------------------------------------------------------------------------*/
258/**
259 * Process root hub request.
260 *
261 * @param instance Root hub instance
262 * @param request Structure containing both request and response information
263 * @return Error code
264 */
265int rh_request(rh_t *instance, usb_transfer_batch_t *request)
266{
267 assert(instance);
268 assert(request);
269
270 int opResult;
271 switch (request->ep->transfer_type)
272 {
273 case USB_TRANSFER_CONTROL:
274 usb_log_debug("Root hub got CONTROL packet\n");
275 opResult = process_ctrl_request(instance, request);
276 usb_transfer_batch_finish_error(request, opResult);
277 break;
278 case USB_TRANSFER_INTERRUPT:
279 usb_log_debug("Root hub got INTERRUPT packet\n");
280 create_interrupt_mask_in_instance(instance);
281 if (is_zeros(instance->interrupt_buffer,
282 instance->interrupt_mask_size)) {
283 usb_log_debug("No changes..\n");
284 instance->unfinished_interrupt_transfer = request;
285 //will be finished later
286 } else {
287 usb_log_debug("Processing changes..\n");
288 process_interrupt_mask_in_instance(instance, request);
289 }
290 break;
291 default:
292 usb_log_error("Root hub got unsupported request.\n");
293 usb_transfer_batch_finish_error(request, EINVAL);
294 }
295 return EOK;
296}
297/*----------------------------------------------------------------------------*/
298/**
299 * process interrupt on a hub
300 *
301 * If there is no pending interrupt transfer, nothing happens.
302 * @param instance
303 */
304void rh_interrupt(rh_t *instance)
305{
306 if (!instance->unfinished_interrupt_transfer)
307 return;
308
309 usb_log_debug("Finalizing interrupt transfer\n");
310 create_interrupt_mask_in_instance(instance);
311 process_interrupt_mask_in_instance(instance,
312 instance->unfinished_interrupt_transfer);
313}
314/*----------------------------------------------------------------------------*/
315/**
316 * Create hub descriptor.
317 *
318 * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
319 *
320 * @param instance Root hub instance
321 * @return Error code
322 */
323int create_serialized_hub_descriptor(rh_t *instance)
324{
325 assert(instance);
326
327 const size_t bit_field_size = (instance->port_count + 1 + 7) / 8;
328 assert(bit_field_size == 2 || bit_field_size == 1);
329 /* 7 bytes + 2 port bit fields (port count + global bit) */
330 const size_t size = 7 + (bit_field_size * 2);
331
332 uint8_t *result = malloc(size);
333 if (!result)
334 return ENOMEM;
335
336 /* bDescLength */
337 result[0] = size;
338 /* bDescriptorType */
339 result[1] = USB_DESCTYPE_HUB;
340 /* bNmbrPorts */
341 result[2] = instance->port_count;
342 const uint32_t hub_desc = instance->registers->rh_desc_a;
343 /* wHubCharacteristics */
344 result[3] = 0 |
345 /* The lowest 2 bits indicate power switching mode */
346 (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
347 (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
348 /* Bit 3 indicates device type (compound device) */
349 (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
350 /* Bits 4,5 indicate over-current protection mode */
351 (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
352 (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
353
354 /* Reserved */
355 result[4] = 0;
356 /* bPwrOn2PwrGood */
357 result[5] = (hub_desc >> RHDA_POTPGT_SHIFT) & RHDA_POTPGT_MASK;
358 /* bHubContrCurrent, root hubs don't need no power. */
359 result[6] = 0;
360
361 const uint32_t port_desc = instance->registers->rh_desc_a;
362 /* Device Removable and some legacy 1.0 stuff*/
363 result[7] = (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK & 0xff;
364 result[8] = 0xff;
365 if (bit_field_size == 2) {
366 result[8] = (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK >> 8;
367 result[9] = 0xff;
368 result[10] = 0xff;
369 }
370 instance->hub_descriptor = result;
371 instance->descriptor_size = size;
372
373 return EOK;
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 memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor,
388 sizeof(ohci_rh_device_descriptor));
389
390 usb_standard_configuration_descriptor_t descriptor;
391 memcpy(&descriptor, &ohci_rh_conf_descriptor,
392 sizeof(ohci_rh_conf_descriptor));
393
394 int opResult = create_serialized_hub_descriptor(instance);
395 if (opResult != EOK)
396 return opResult;
397
398 descriptor.total_length =
399 sizeof(usb_standard_configuration_descriptor_t) +
400 sizeof(usb_standard_endpoint_descriptor_t) +
401 sizeof(usb_standard_interface_descriptor_t) +
402 instance->descriptor_size;
403
404 uint8_t *full_config_descriptor = malloc(descriptor.total_length);
405 if (!full_config_descriptor)
406 return ENOMEM;
407
408 uint8_t *place = full_config_descriptor;
409 memcpy(place, &descriptor, sizeof(descriptor));
410
411 place += sizeof(descriptor);
412 memcpy(place, &ohci_rh_iface_descriptor,
413 sizeof(ohci_rh_iface_descriptor));
414
415 place += sizeof(ohci_rh_iface_descriptor);
416 memcpy(place, &ohci_rh_ep_descriptor, sizeof(ohci_rh_ep_descriptor));
417
418 place += sizeof(ohci_rh_iface_descriptor);
419 memcpy(place, instance->hub_descriptor, instance->descriptor_size);
420
421 instance->descriptors.configuration = full_config_descriptor;
422 instance->descriptors.configuration_size = descriptor.total_length;
423
424 return EOK;
425}
426/*----------------------------------------------------------------------------*/
427/**
428 * Create answer to port status_request
429 *
430 * Copy content of corresponding port status register to answer buffer. The
431 * format of the port status register and port status data is the same (
432 * see OHCI root hub and USB hub documentation).
433 *
434 * @param instance Root hub instance
435 * @param port Port number, counted from 1
436 * @param request Structure containing both request and response information
437 * @return Error code
438 */
439int process_get_port_status_request(
440 rh_t *instance, uint16_t port, usb_transfer_batch_t * request)
441{
442 assert(instance);
443 assert(request);
444
445 if (port < 1 || port > instance->port_count)
446 return EINVAL;
447
448 const uint32_t data = instance->registers->rh_port_status[port - 1];
449 memcpy(request->data_buffer, &data, 4);
450 request->transfered_size = 4;
451 return EOK;
452}
453/*----------------------------------------------------------------------------*/
454/**
455 * Create answer to port status_request.
456 *
457 * This copies flags in hub status register into the buffer. The format of the
458 * status register and status message is the same, according to USB hub
459 * specification and OHCI root hub specification.
460 *
461 * @param instance Root hub instance.
462 * @param request Structure containing both request and response information.
463 * @return Error code
464 */
465int process_get_hub_status_request(
466 rh_t *instance, usb_transfer_batch_t *request)
467{
468 assert(instance);
469 assert(request);
470
471 /* bits, 0,1,16,17 -- TODO: What do they mean?? Why not 0x0303 */
472 const uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
473 const uint32_t data = mask & instance->registers->rh_status;
474 memcpy(request->data_buffer, &data, 4);
475 request->transfered_size = 4;
476
477 return EOK;
478}
479/*----------------------------------------------------------------------------*/
480/**
481 * Create answer to status request.
482 *
483 * This might be either hub status or port status request. If neither,
484 * ENOTSUP is returned.
485 * @param instance root hub instance
486 * @param request structure containing both request and response information
487 * @return error code
488 */
489int process_get_status_request(rh_t *instance, usb_transfer_batch_t *request)
490{
491 assert(instance);
492 assert(request);
493
494 const usb_device_request_setup_packet_t *request_packet =
495 (usb_device_request_setup_packet_t*)request->setup_buffer;
496
497 const usb_hub_bm_request_type_t request_type =
498 request_packet->request_type;
499
500 if (request->buffer_size < 4) {
501 usb_log_error("Buffer too small for get status request.\n");
502 return EOVERFLOW;
503 }
504
505 if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
506 return process_get_hub_status_request(instance, request);
507 if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
508 return process_get_port_status_request(instance,
509 request_packet->index, request);
510
511 return ENOTSUP;
512}
513/*----------------------------------------------------------------------------*/
514/**
515 * Create bitmap of changes to answer status interrupt.
516 *
517 * Result contains bitmap where bit 0 indicates change on hub and
518 * bit i indicates change on i`th port (i>0). For more info see
519 * Hub and Port status bitmap specification in USB specification
520 * (chapter 11.13.4).
521 * Uses instance`s interrupt buffer to store the interrupt information.
522 * @param instance root hub instance
523 */
524void create_interrupt_mask_in_instance(rh_t *instance)
525{
526 assert(instance);
527
528 uint8_t * bitmap = (uint8_t*) (instance->interrupt_buffer);
529 uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
530 | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
531 bzero(bitmap, instance->interrupt_mask_size);
532 if ((instance->registers->rh_status & mask) != 0) {
533 bitmap[0] = 1;
534 }
535 mask = port_status_change_mask;
536 size_t port = 1;
537 for (; port <= instance->port_count; ++port) {
538 if ((mask & instance->registers->rh_port_status[port - 1]) != 0) {
539
540 bitmap[(port) / 8] += 1 << (port % 8);
541 }
542 }
543}
544/*----------------------------------------------------------------------------*/
545/**
546 * Create answer to a descriptor request.
547 *
548 * This might be a request for standard (configuration, device, endpoint or
549 * interface) or device specific (hub) descriptor.
550 * @param instance Root hub instance
551 * @param request Structure containing both request and response information
552 * @return Error code
553 */
554int process_get_descriptor_request(
555 rh_t *instance, usb_transfer_batch_t *request)
556{
557 assert(instance);
558 assert(request);
559
560 const usb_device_request_setup_packet_t *setup_request =
561 (usb_device_request_setup_packet_t *) request->setup_buffer;
562 size_t size;
563 const void * result_descriptor = NULL;
564 const uint16_t setup_request_value = setup_request->value_high;
565 //(setup_request->value_low << 8);
566 switch (setup_request_value)
567 {
568 case USB_DESCTYPE_HUB:
569 usb_log_debug2("USB_DESCTYPE_HUB\n");
570 result_descriptor = instance->hub_descriptor;
571 size = instance->descriptor_size;
572 break;
573
574 case USB_DESCTYPE_DEVICE:
575 usb_log_debug2("USB_DESCTYPE_DEVICE\n");
576 result_descriptor = &ohci_rh_device_descriptor;
577 size = sizeof(ohci_rh_device_descriptor);
578 break;
579
580 case USB_DESCTYPE_CONFIGURATION:
581 usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
582 result_descriptor = instance->descriptors.configuration;
583 size = instance->descriptors.configuration_size;
584 break;
585
586 case USB_DESCTYPE_INTERFACE:
587 usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
588 result_descriptor = &ohci_rh_iface_descriptor;
589 size = sizeof(ohci_rh_iface_descriptor);
590 break;
591
592 case USB_DESCTYPE_ENDPOINT:
593 usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
594 result_descriptor = &ohci_rh_ep_descriptor;
595 size = sizeof(ohci_rh_ep_descriptor);
596 break;
597
598 default:
599 usb_log_debug2("USB_DESCTYPE_EINVAL %d \n"
600 "\ttype %d\n\trequest %d\n\tvalue "
601 "%d\n\tindex %d\n\tlen %d\n ",
602 setup_request->value,
603 setup_request->request_type, setup_request->request,
604 setup_request_value, setup_request->index,
605 setup_request->length);
606 return EINVAL;
607 }
608 if (request->buffer_size < size) {
609 size = request->buffer_size;
610 }
611 memcpy(request->data_buffer, result_descriptor, size);
612 request->transfered_size = size;
613
614 return EOK;
615}
616/*----------------------------------------------------------------------------*/
617/**
618 * Answer to get configuration request.
619 *
620 * Root hub works independently on the configuration.
621 * Set and get configuration requests do not have any meaning,
622 * dummy values are returned.
623 *
624 * @param instance Root hub instance
625 * @param request Structure containing both request and response information
626 * @return Error code
627 */
628int process_get_configuration_request(
629 rh_t *instance, usb_transfer_batch_t *request)
630{
631 assert(request);
632
633 if (request->buffer_size != 1)
634 return EINVAL;
635 request->data_buffer[0] = 1;
636 request->transfered_size = 1;
637
638 return EOK;
639}
640/*----------------------------------------------------------------------------*/
641/**
642 * process feature-enabling request on hub
643 *
644 * @param instance root hub instance
645 * @param feature feature selector
646 * @return error code
647 */
648static int process_hub_feature_set_request(rh_t *instance,
649 uint16_t feature) {
650 if (!((1 << feature) & hub_set_feature_valid_mask))
651 return EINVAL;
652 if (feature == USB_HUB_FEATURE_C_HUB_LOCAL_POWER)
653 feature = USB_HUB_FEATURE_C_HUB_LOCAL_POWER << 16;
654 instance->registers->rh_status =
655 (instance->registers->rh_status | (1 << feature))
656 & (~hub_clear_feature_by_writing_one_mask);
657
658 return EOK;
659}
660/*----------------------------------------------------------------------------*/
661/**
662 * process feature-disabling request on hub
663 *
664 * @param instance root hub instance
665 * @param feature feature selector
666 * @return error code
667 */
668int process_hub_feature_clear_request(rh_t *instance, uint16_t feature)
669{
670 assert(instance);
671
672 if (!((1 << feature) & hub_clear_feature_valid_mask))
673 return EINVAL;
674
675 //is the feature cleared directly?
676 if ((1 << feature) & hub_set_feature_direct_mask) {
677 instance->registers->rh_status =
678 (instance->registers->rh_status & (~(1 << feature)))
679 & (~hub_clear_feature_by_writing_one_mask);
680 } else {//the feature is cleared by writing '1'
681
682 instance->registers->rh_status =
683 (instance->registers->rh_status
684 & (~hub_clear_feature_by_writing_one_mask))
685 | (1 << feature);
686 }
687 return EOK;
688}
689/*----------------------------------------------------------------------------*/
690/**
691 * process feature-enabling request on hub
692 *
693 * @param instance root hub instance
694 * @param feature feature selector
695 * @param port port number, counted from 1
696 * @param enable enable or disable the specified feature
697 * @return error code
698 */
699int process_port_feature_set_request(
700 rh_t *instance, uint16_t feature, uint16_t port)
701{
702 assert(instance);
703
704 if (!((1 << feature) & port_set_feature_valid_mask))
705 return EINVAL;
706 if (port < 1 || port > instance->port_count)
707 return EINVAL;
708
709 instance->registers->rh_port_status[port - 1] =
710 (instance->registers->rh_port_status[port - 1] | (1 << feature))
711 & (~port_clear_feature_valid_mask);
712 return EOK;
713}
714/*----------------------------------------------------------------------------*/
715/**
716 * process feature-disabling request on hub
717 *
718 * @param instance root hub instance
719 * @param feature feature selector
720 * @param port port number, counted from 1
721 * @param enable enable or disable the specified feature
722 * @return error code
723 */
724int process_port_feature_clear_request(
725 rh_t *instance, uint16_t feature, uint16_t port)
726{
727 assert(instance);
728
729 if (!((1 << feature) & port_clear_feature_valid_mask))
730 return EINVAL;
731 if (port < 1 || port > instance->port_count)
732 return EINVAL;
733
734 /* Some weird stuff... */
735 if (feature == USB_HUB_FEATURE_PORT_POWER)
736 feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
737 if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
738 feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
739
740 instance->registers->rh_port_status[port - 1] =
741 (instance->registers->rh_port_status[port - 1]
742 & (~port_clear_feature_valid_mask))
743 | (1 << feature);
744
745 return EOK;
746}
747/*----------------------------------------------------------------------------*/
748/**
749 * process one of requests that requere output data
750 *
751 * Request can be one of USB_DEVREQ_GET_STATUS, USB_DEVREQ_GET_DESCRIPTOR or
752 * USB_DEVREQ_GET_CONFIGURATION.
753 * @param instance root hub instance
754 * @param request structure containing both request and response information
755 * @return error code
756 */
757int process_request_with_output(rh_t *instance, usb_transfer_batch_t *request)
758{
759 assert(instance);
760 assert(request);
761
762 const usb_device_request_setup_packet_t *setup_request =
763 (usb_device_request_setup_packet_t *) request->setup_buffer;
764 switch (setup_request->request)
765 {
766 case USB_DEVREQ_GET_STATUS:
767 usb_log_debug("USB_DEVREQ_GET_STATUS\n");
768 return process_get_status_request(instance, request);
769 case USB_DEVREQ_GET_DESCRIPTOR:
770 usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
771 return process_get_descriptor_request(instance, request);
772 case USB_DEVREQ_GET_CONFIGURATION:
773 usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
774 return process_get_configuration_request(instance, request);
775 }
776 return ENOTSUP;
777}
778/*----------------------------------------------------------------------------*/
779/**
780 * process one of requests that carry input data
781 *
782 * Request can be one of USB_DEVREQ_SET_DESCRIPTOR or
783 * USB_DEVREQ_SET_CONFIGURATION.
784 * @param instance root hub instance
785 * @param request structure containing both request and response information
786 * @return error code
787 */
788int process_request_with_input(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 if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
797 //set and get configuration requests do not have any meaning,
798 //only dummy values are returned
799 return EOK;
800 }
801 /* USB_DEVREQ_SET_DESCRIPTOR is also not supported */
802 return ENOTSUP;
803}
804/*----------------------------------------------------------------------------*/
805/**
806 * process one of requests that do not request nor carry additional data
807 *
808 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
809 * USB_DEVREQ_SET_ADDRESS.
810 * @param instance root hub instance
811 * @param request structure containing both request and response information
812 * @return error code
813 */
814int process_request_without_data(rh_t *instance, usb_transfer_batch_t *request)
815{
816 assert(instance);
817 assert(request);
818
819 const usb_device_request_setup_packet_t *setup_request =
820 (usb_device_request_setup_packet_t *) request->setup_buffer;
821 request->transfered_size = 0;
822 const int request_type = setup_request->request_type;
823 switch (setup_request->request)
824 {
825 case USB_DEVREQ_CLEAR_FEATURE:
826 if (request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
827 usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
828 return process_hub_feature_clear_request(instance,
829 setup_request->value);
830 }
831 if (request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
832 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
833 return process_port_feature_clear_request(instance,
834 setup_request->value, setup_request->index);
835 }
836 usb_log_error("Invalid HUB clear feature request type: %d\n",
837 request_type);
838 return EINVAL;
839
840 case USB_DEVREQ_SET_FEATURE:
841 if (request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
842 usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
843 return process_hub_feature_set_request(instance,
844 setup_request->value);
845 }
846 if (request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
847 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
848 return process_port_feature_set_request(instance,
849 setup_request->value, setup_request->index);
850 }
851 usb_log_error("Invalid HUB set feature request type: %d\n",
852 request_type);
853 return EINVAL;
854
855 case USB_DEVREQ_SET_ADDRESS:
856 usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
857 return process_address_set_request(instance,
858 setup_request->value);
859
860 default:
861 usb_log_error("Invalid HUB request: %d\n",
862 setup_request->request);
863 return ENOTSUP;
864 }
865}
866/*----------------------------------------------------------------------------*/
867/**
868 * Process hub control request.
869 *
870 * If needed, writes answer into the request structure.
871 * Request can be one of
872 * USB_DEVREQ_GET_STATUS,
873 * USB_DEVREQ_GET_DESCRIPTOR,
874 * USB_DEVREQ_GET_CONFIGURATION,
875 * USB_DEVREQ_CLEAR_FEATURE,
876 * USB_DEVREQ_SET_FEATURE,
877 * USB_DEVREQ_SET_ADDRESS,
878 * USB_DEVREQ_SET_DESCRIPTOR or
879 * USB_DEVREQ_SET_CONFIGURATION.
880 *
881 * @param instance root hub instance
882 * @param request structure containing both request and response information
883 * @return error code
884 */
885int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request)
886{
887 assert(instance);
888 assert(request);
889
890 if (!request->setup_buffer) {
891 usb_log_error("Root hub received empty transaction!");
892 return EINVAL;
893 }
894 if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
895 usb_log_error("Setup packet too small\n");
896 return EOVERFLOW;
897 }
898 usb_log_debug2("CTRL packet: %s.\n",
899 usb_debug_str_buffer((uint8_t *) request->setup_buffer, 8, 8));
900 const usb_device_request_setup_packet_t *setup_request =
901 (usb_device_request_setup_packet_t *) request->setup_buffer;
902 switch (setup_request->request)
903 {
904 case USB_DEVREQ_GET_STATUS:
905 case USB_DEVREQ_GET_DESCRIPTOR:
906 case USB_DEVREQ_GET_CONFIGURATION:
907 usb_log_debug2("Processing request with output\n");
908 return process_request_with_output(instance, request);
909 case USB_DEVREQ_CLEAR_FEATURE:
910 case USB_DEVREQ_SET_FEATURE:
911 case USB_DEVREQ_SET_ADDRESS:
912 usb_log_debug2("Processing request without "
913 "additional data\n");
914 return process_request_without_data(instance, request);
915 case USB_DEVREQ_SET_DESCRIPTOR:
916 case USB_DEVREQ_SET_CONFIGURATION:
917 usb_log_debug2("Processing request with input\n");
918 return process_request_with_input(instance, request);
919 default:
920 usb_log_error("Received unsupported request: %d.\n",
921 setup_request->request);
922 return ENOTSUP;
923 }
924}
925/*----------------------------------------------------------------------------*/
926/**
927 * process hanging interrupt request
928 *
929 * If an interrupt transfer has been received and there was no change,
930 * the driver stores the transfer information and waits for change to occur.
931 * This routine is called when that happens and it finalizes the interrupt
932 * transfer.
933 *
934 * @param instance hub instance
935 * @param request batch request to be processed
936 *
937 * @return
938 */
939int process_interrupt_mask_in_instance(
940 rh_t *instance, usb_transfer_batch_t *request)
941{
942 assert(instance);
943 assert(request);
944
945 memcpy(request->data_buffer, instance->interrupt_buffer,
946 instance->interrupt_mask_size);
947 request->transfered_size = instance->interrupt_mask_size;
948 instance->unfinished_interrupt_transfer = NULL;
949 usb_transfer_batch_finish_error(request, EOK);
950
951 return EOK;
952}
953/*----------------------------------------------------------------------------*/
954/**
955 * return whether the buffer is full of zeros
956 *
957 * Convenience function.
958 * @param buffer
959 * @param size
960 * @return
961 */
962bool is_zeros(const void *buffer, size_t size)
963{
964 if (!buffer) return true;
965 const char * const end = buffer + size;
966 const char *data = buffer;
967 for (; data < end; ++data) {
968 if (*data)
969 return false;
970 }
971 return true;
972}
973
974/**
975 * @}
976 */
Note: See TracBrowser for help on using the repository browser.