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

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

OHCI: Root hub code cleanup, more changes will follow.

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