source: mainline/uspace/drv/ohci/root_hub.c@ 7d521e24

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7d521e24 was 7d521e24, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

libusbdev uses include usb/dev

  • Property mode set to 100644
File size: 30.1 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.
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 */
429static int process_get_port_status_request(rh_t *instance, uint16_t port,
430 usb_transfer_batch_t * request) {
431 if (port < 1 || port > instance->port_count)
432 return EINVAL;
433 uint32_t * uint32_buffer = (uint32_t*) request->data_buffer;
434 request->transfered_size = 4;
435 uint32_buffer[0] = instance->registers->rh_port_status[port - 1];
436#if 0
437 int i;
438 for (i = 0; i < instance->port_count; ++i) {
439
440 usb_log_debug("port status %d,x%x\n",
441 instance->registers->rh_port_status[i],
442 instance->registers->rh_port_status[i]);
443 }
444#endif
445 return EOK;
446}
447/*----------------------------------------------------------------------------*/
448
449/**
450 * create answer to port status_request
451 *
452 * Copy content of hub status register to answer buffer.
453 *
454 * @param instance root hub instance
455 * @param request structure containing both request and response information
456 * @return error code
457 */
458static int process_get_hub_status_request(rh_t *instance,
459 usb_transfer_batch_t * request) {
460 uint32_t * uint32_buffer = (uint32_t*) request->data_buffer;
461 request->transfered_size = 4;
462 //bits, 0,1,16,17
463 uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
464 uint32_buffer[0] = mask & instance->registers->rh_status;
465
466 return EOK;
467}
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 */
479static int process_get_status_request(rh_t *instance,
480 usb_transfer_batch_t * request) {
481 size_t buffer_size = request->buffer_size;
482 usb_device_request_setup_packet_t * request_packet =
483 (usb_device_request_setup_packet_t*)
484 request->setup_buffer;
485
486 usb_hub_bm_request_type_t request_type = request_packet->request_type;
487 if (buffer_size < 4/*request_packet->length*/) {///\TODO
488 usb_log_warning("requested more data than buffer size\n");
489 return EINVAL;
490 }
491
492 if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
493 return process_get_hub_status_request(instance, request);
494 if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
495 return process_get_port_status_request(instance,
496 request_packet->index,
497 request);
498
499 return ENOTSUP;
500}
501/*----------------------------------------------------------------------------*/
502
503/**
504 * create answer to status interrupt consisting of change bitmap
505 *
506 * Result contains bitmap where bit 0 indicates change on hub and
507 * bit i indicates change on i`th port (i>0). For more info see
508 * Hub and Port status bitmap specification in USB specification
509 * (chapter 11.13.4).
510 * Uses instance`s interrupt buffer to store the interrupt information.
511 * @param instance root hub instance
512 */
513static void create_interrupt_mask_in_instance(rh_t * instance) {
514 uint8_t * bitmap = (uint8_t*) (instance->interrupt_buffer);
515 uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
516 | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
517 bzero(bitmap, instance->interrupt_mask_size);
518 if (instance->registers->rh_status & mask) {
519 bitmap[0] = 1;
520 }
521 size_t port;
522 mask = port_status_change_mask;
523 for (port = 1; port <= instance->port_count; ++port) {
524 if (mask & instance->registers->rh_port_status[port - 1]) {
525
526 bitmap[(port) / 8] += 1 << (port % 8);
527 }
528 }
529}
530/*----------------------------------------------------------------------------*/
531
532/**
533 * create answer to a descriptor request
534 *
535 * This might be a request for standard (configuration, device, endpoint or
536 * interface) or device specific (hub) descriptor.
537 * @param instance root hub instance
538 * @param request structure containing both request and response information
539 * @return error code
540 */
541static int process_get_descriptor_request(rh_t *instance,
542 usb_transfer_batch_t *request) {
543 usb_device_request_setup_packet_t * setup_request =
544 (usb_device_request_setup_packet_t*) request->setup_buffer;
545 size_t size;
546 const void * result_descriptor = NULL;
547 const uint16_t setup_request_value = setup_request->value_high;
548 //(setup_request->value_low << 8);
549 switch (setup_request_value) {
550 case USB_DESCTYPE_HUB:
551 {
552 usb_log_debug("USB_DESCTYPE_HUB\n");
553 result_descriptor = instance->hub_descriptor;
554 size = instance->descriptor_size;
555 break;
556 }
557 case USB_DESCTYPE_DEVICE:
558 {
559 usb_log_debug("USB_DESCTYPE_DEVICE\n");
560 result_descriptor = &ohci_rh_device_descriptor;
561 size = sizeof (ohci_rh_device_descriptor);
562 break;
563 }
564 case USB_DESCTYPE_CONFIGURATION:
565 {
566 usb_log_debug("USB_DESCTYPE_CONFIGURATION\n");
567 result_descriptor = instance->descriptors.configuration;
568 size = instance->descriptors.configuration_size;
569 break;
570 }
571 case USB_DESCTYPE_INTERFACE:
572 {
573 usb_log_debug("USB_DESCTYPE_INTERFACE\n");
574 result_descriptor = &ohci_rh_iface_descriptor;
575 size = sizeof (ohci_rh_iface_descriptor);
576 break;
577 }
578 case USB_DESCTYPE_ENDPOINT:
579 {
580 usb_log_debug("USB_DESCTYPE_ENDPOINT\n");
581 result_descriptor = &ohci_rh_ep_descriptor;
582 size = sizeof (ohci_rh_ep_descriptor);
583 break;
584 }
585 default:
586 {
587 usb_log_debug("USB_DESCTYPE_EINVAL %d \n",
588 setup_request->value);
589 usb_log_debug("\ttype %d\n\trequest %d\n\tvalue "
590 "%d\n\tindex %d\n\tlen %d\n ",
591 setup_request->request_type,
592 setup_request->request,
593 setup_request_value,
594 setup_request->index,
595 setup_request->length
596 );
597 return EINVAL;
598 }
599 }
600 if (request->buffer_size < size) {
601 size = request->buffer_size;
602 }
603 request->transfered_size = size;
604 memcpy(request->data_buffer, result_descriptor, size);
605
606 return EOK;
607}
608/*----------------------------------------------------------------------------*/
609
610/**
611 * answer to get configuration request
612 *
613 * Root hub works independently on the configuration.
614 * @param instance root hub instance
615 * @param request structure containing both request and response information
616 * @return error code
617 */
618static int process_get_configuration_request(rh_t *instance,
619 usb_transfer_batch_t *request) {
620 //set and get configuration requests do not have any meaning, only dummy
621 //values are returned
622 if (request->buffer_size != 1)
623 return EINVAL;
624 request->data_buffer[0] = 1;
625 request->transfered_size = 1;
626
627 return EOK;
628}
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/**
653 * process feature-disabling request on hub
654 *
655 * @param instance root hub instance
656 * @param feature feature selector
657 * @return error code
658 */
659static int process_hub_feature_clear_request(rh_t *instance,
660 uint16_t feature) {
661 if (!((1 << feature) & hub_clear_feature_valid_mask))
662 return EINVAL;
663 //is the feature cleared directly?
664 if ((1 << feature) & hub_set_feature_direct_mask) {
665 instance->registers->rh_status =
666 (instance->registers->rh_status & (~(1 << feature)))
667 & (~hub_clear_feature_by_writing_one_mask);
668 } else {//the feature is cleared by writing '1'
669
670 instance->registers->rh_status =
671 (instance->registers->rh_status
672 & (~hub_clear_feature_by_writing_one_mask))
673 | (1 << feature);
674 }
675 return EOK;
676}
677/*----------------------------------------------------------------------------*/
678
679/**
680 * process feature-enabling request on hub
681 *
682 * @param instance root hub instance
683 * @param feature feature selector
684 * @param port port number, counted from 1
685 * @param enable enable or disable the specified feature
686 * @return error code
687 */
688static int process_port_feature_set_request(rh_t *instance,
689 uint16_t feature, uint16_t port) {
690 if (!((1 << feature) & port_set_feature_valid_mask))
691 return EINVAL;
692 if (port < 1 || port > instance->port_count)
693 return EINVAL;
694 instance->registers->rh_port_status[port - 1] =
695 (instance->registers->rh_port_status[port - 1] | (1 << feature))
696 & (~port_clear_feature_valid_mask);
697 /// \TODO any error?
698
699 return EOK;
700}
701/*----------------------------------------------------------------------------*/
702
703/**
704 * process feature-disabling request on hub
705 *
706 * @param instance root hub instance
707 * @param feature feature selector
708 * @param port port number, counted from 1
709 * @param enable enable or disable the specified feature
710 * @return error code
711 */
712static int process_port_feature_clear_request(rh_t *instance,
713 uint16_t feature, uint16_t port) {
714 if (!((1 << feature) & port_clear_feature_valid_mask))
715 return EINVAL;
716 if (port < 1 || port > instance->port_count)
717 return EINVAL;
718 if (feature == USB_HUB_FEATURE_PORT_POWER)
719 feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
720 if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
721 feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
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 /// \TODO any error?
727
728 return EOK;
729}
730/*----------------------------------------------------------------------------*/
731
732/**
733 * register address to this device
734 *
735 * @param instance root hub instance
736 * @param address new address
737 * @return error code
738 */
739static int process_address_set_request(rh_t *instance,
740 uint16_t address) {
741 instance->address = address;
742
743 return EOK;
744}
745/*----------------------------------------------------------------------------*/
746
747/**
748 * process one of requests that requere output data
749 *
750 * Request can be one of USB_DEVREQ_GET_STATUS, USB_DEVREQ_GET_DESCRIPTOR or
751 * USB_DEVREQ_GET_CONFIGURATION.
752 * @param instance root hub instance
753 * @param request structure containing both request and response information
754 * @return error code
755 */
756static int process_request_with_output(rh_t *instance,
757 usb_transfer_batch_t *request) {
758 usb_device_request_setup_packet_t * setup_request =
759 (usb_device_request_setup_packet_t*) request->setup_buffer;
760 if (setup_request->request == USB_DEVREQ_GET_STATUS) {
761 usb_log_debug("USB_DEVREQ_GET_STATUS\n");
762 return process_get_status_request(instance, request);
763 }
764 if (setup_request->request == USB_DEVREQ_GET_DESCRIPTOR) {
765 usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
766 return process_get_descriptor_request(instance, request);
767 }
768 if (setup_request->request == USB_DEVREQ_GET_CONFIGURATION) {
769 usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
770
771 return process_get_configuration_request(instance, request);
772 }
773 return ENOTSUP;
774}
775/*----------------------------------------------------------------------------*/
776
777/**
778 * process one of requests that carry input data
779 *
780 * Request can be one of USB_DEVREQ_SET_DESCRIPTOR or
781 * USB_DEVREQ_SET_CONFIGURATION.
782 * @param instance root hub instance
783 * @param request structure containing both request and response information
784 * @return error code
785 */
786static int process_request_with_input(rh_t *instance,
787 usb_transfer_batch_t *request) {
788 usb_device_request_setup_packet_t * setup_request =
789 (usb_device_request_setup_packet_t*) request->setup_buffer;
790 request->transfered_size = 0;
791 if (setup_request->request == USB_DEVREQ_SET_DESCRIPTOR) {
792 return ENOTSUP;
793 }
794 if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
795 //set and get configuration requests do not have any meaning,
796 //only dummy values are returned
797
798 return EOK;
799 }
800 return ENOTSUP;
801}
802/*----------------------------------------------------------------------------*/
803
804/**
805 * process one of requests that do not request nor carry additional data
806 *
807 * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
808 * USB_DEVREQ_SET_ADDRESS.
809 * @param instance root hub instance
810 * @param request structure containing both request and response information
811 * @return error code
812 */
813static int process_request_without_data(rh_t *instance,
814 usb_transfer_batch_t *request) {
815 usb_device_request_setup_packet_t * setup_request =
816 (usb_device_request_setup_packet_t*) request->setup_buffer;
817 request->transfered_size = 0;
818 if (setup_request->request == USB_DEVREQ_CLEAR_FEATURE) {
819 if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
820 usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
821 return process_hub_feature_clear_request(instance,
822 setup_request->value);
823 }
824 if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
825 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
826 return process_port_feature_clear_request(instance,
827 setup_request->value,
828 setup_request->index);
829 }
830 usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
831 setup_request->request_type);
832 return EINVAL;
833 }
834 if (setup_request->request == USB_DEVREQ_SET_FEATURE) {
835 if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
836 usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
837 return process_hub_feature_set_request(instance,
838 setup_request->value);
839 }
840 if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
841 usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
842 return process_port_feature_set_request(instance,
843 setup_request->value,
844 setup_request->index);
845 }
846 usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
847 setup_request->request_type);
848 return EINVAL;
849 }
850 if (setup_request->request == USB_DEVREQ_SET_ADDRESS) {
851 usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
852 return process_address_set_request(instance,
853 setup_request->value);
854 }
855 usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",
856 setup_request->request_type);
857
858 return ENOTSUP;
859}
860/*----------------------------------------------------------------------------*/
861
862/**
863 * process hub control request
864 *
865 * If needed, writes answer into the request structure.
866 * Request can be one of
867 * USB_DEVREQ_GET_STATUS,
868 * USB_DEVREQ_GET_DESCRIPTOR,
869 * USB_DEVREQ_GET_CONFIGURATION,
870 * USB_DEVREQ_CLEAR_FEATURE,
871 * USB_DEVREQ_SET_FEATURE,
872 * USB_DEVREQ_SET_ADDRESS,
873 * USB_DEVREQ_SET_DESCRIPTOR or
874 * USB_DEVREQ_SET_CONFIGURATION.
875 *
876 * @param instance root hub instance
877 * @param request structure containing both request and response information
878 * @return error code
879 */
880static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request) {
881 if (!request->setup_buffer) {
882 usb_log_error("root hub received empty transaction?");
883 return EINVAL;
884 }
885 int opResult;
886 if (sizeof (usb_device_request_setup_packet_t) > request->setup_size) {
887 usb_log_error("setup packet too small\n");
888 return EINVAL;
889 }
890 usb_log_info("CTRL packet: %s.\n",
891 usb_debug_str_buffer(
892 (const uint8_t *) request->setup_buffer, 8, 8));
893 usb_device_request_setup_packet_t * setup_request =
894 (usb_device_request_setup_packet_t*)
895 request->setup_buffer;
896 switch (setup_request->request) {
897 case USB_DEVREQ_GET_STATUS:
898 case USB_DEVREQ_GET_DESCRIPTOR:
899 case USB_DEVREQ_GET_CONFIGURATION:
900 usb_log_debug("processing request with output\n");
901 opResult = process_request_with_output(
902 instance, request);
903 break;
904 case USB_DEVREQ_CLEAR_FEATURE:
905 case USB_DEVREQ_SET_FEATURE:
906 case USB_DEVREQ_SET_ADDRESS:
907 usb_log_debug("processing request without "
908 "additional data\n");
909 opResult = process_request_without_data(
910 instance, request);
911 break;
912 case USB_DEVREQ_SET_DESCRIPTOR:
913 case USB_DEVREQ_SET_CONFIGURATION:
914 usb_log_debug("processing request with "
915 "input\n");
916 opResult = process_request_with_input(
917 instance, request);
918
919 break;
920 default:
921 usb_log_warning("received unsuported request: "
922 "%d\n",
923 setup_request->request
924 );
925 opResult = ENOTSUP;
926 }
927 return opResult;
928}
929/*----------------------------------------------------------------------------*/
930
931/**
932 * process hanging interrupt request
933 *
934 * If an interrupt transfer has been received and there was no change,
935 * the driver stores the transfer information and waits for change to occcur.
936 * This routine is called when that happens and it finalizes the interrupt
937 * transfer.
938 *
939 * @param instance hub instance
940 * @param request batch request to be processed
941 *
942 * @return
943 */
944static int process_interrupt_mask_in_instance(rh_t *instance, usb_transfer_batch_t * request) {
945 memcpy(request->data_buffer, instance->interrupt_buffer,
946 instance->interrupt_mask_size);
947 request->transfered_size = instance->interrupt_mask_size;
948 instance->unfinished_interrupt_transfer = NULL;
949 usb_transfer_batch_finish_error(request, EOK);
950
951 return EOK;
952}
953
954/*----------------------------------------------------------------------------*/
955
956/**
957 * return whether the buffer is full of zeros
958 *
959 * Convenience function.
960 * @param buffer
961 * @param size
962 * @return
963 */
964static bool is_zeros(void * buffer, size_t size) {
965 if (!buffer) return true;
966 if (!size) return true;
967 size_t i;
968 for (i = 0; i < size; ++i) {
969 if (((char*) buffer)[i])
970 return false;
971 }
972 return true;
973}
974
975/**
976 * @}
977 */
Note: See TracBrowser for help on using the repository browser.