source: mainline/uspace/drv/ohci/root_hub.c@ e89bb50

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

oops, one parenthes missed

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