source: mainline/uspace/drv/ohci/root_hub.c@ 8bb61e6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8bb61e6 was 8bb61e6, checked in by Matus Dekanek <smekideki@…>, 15 years ago

more comments for ohci root hub

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