source: mainline/uspace/drv/ohci/root_hub.c@ 48d1c228

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

fix for usb hub
loc in ohci root hub

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