source: mainline/uspace/drv/ohci/root_hub.c@ 05e21ffc

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

minor changes

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