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

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

OHCI: Root hub: remove allocation and fix rare bug.

Limit port-count to 15 (specs say so on page 124).
Use array of max possible size (2 bytes), instead of heap allocation.
Fix issue with changes happening on port 8 on 8-port hub.

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