source: mainline/uspace/drv/usbhub/usbhub.c@ 1c89f74

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

fix attempt #3

  • Property mode set to 100644
File size: 30.6 KB
Line 
1/*
2 * Copyright (c) 2010 Matus Dekanek
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 drvusbhub
29 * @{
30 */
31/** @file
32 * @brief usb hub main functionality
33 */
34
35#include <ddf/driver.h>
36#include <bool.h>
37#include <errno.h>
38#include <str_error.h>
39
40#include <usb_iface.h>
41#include <usb/ddfiface.h>
42#include <usb/descriptor.h>
43#include <usb/recognise.h>
44#include <usb/request.h>
45#include <usb/classes/hub.h>
46#include <stdio.h>
47
48#include "usbhub.h"
49#include "usbhub_private.h"
50#include "port_status.h"
51#include "usb/usb.h"
52#include "usb/pipes.h"
53#include "usb/classes/classes.h"
54
55
56static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
57 usb_speed_t speed);
58
59static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev);
60
61static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info);
62
63static int usb_hub_set_configuration(usb_hub_info_t * hub_info);
64
65static int usb_hub_release_default_address(usb_hub_info_t * hub);
66
67static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
68 usb_speed_t speed);
69
70static void usb_hub_finalize_add_device(usb_hub_info_t * hub,
71 uint16_t port, usb_speed_t speed);
72
73static void usb_hub_removed_device(
74 usb_hub_info_t * hub, uint16_t port);
75
76static void usb_hub_port_over_current(usb_hub_info_t * hub,
77 uint16_t port, uint32_t status);
78
79static void usb_hub_process_interrupt(usb_hub_info_t * hub,
80 uint16_t port);
81
82static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
83 usb_hub_status_t status);
84
85static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
86 usb_hub_status_t status);
87
88static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info);
89
90static int initialize_non_removable(usb_hub_info_t * hub_info,
91 unsigned int port);
92
93static int usb_hub_trigger_connecting_non_removable_devices(
94 usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
95
96
97/**
98 * control loop running in hub`s fibril
99 *
100 * Hub`s fibril periodically asks for changes on hub and if needded calls
101 * change handling routine.
102 * @warning currently hub driver asks for changes once a second
103 * @param hub_info_param hub representation pointer
104 * @return zero
105 */
106int usb_hub_control_loop(void * hub_info_param) {
107 usb_hub_info_t * hub_info = (usb_hub_info_t*) hub_info_param;
108 int errorCode = EOK;
109
110 while (errorCode == EOK) {
111 async_usleep(1000 * 1000 * 10); /// \TODO proper number once
112 errorCode = usb_hub_check_hub_changes(hub_info);
113 }
114 usb_log_error("something in ctrl loop went wrong, errno %d\n",
115 errorCode);
116
117 return 0;
118}
119/// \TODO malloc checking
120
121//*********************************************
122//
123// hub driver code, initialization
124//
125//*********************************************
126
127
128
129/**
130 * Initialize hub device driver fibril
131 *
132 * Creates hub representation and fibril that periodically checks hub`s status.
133 * Hub representation is passed to the fibril.
134 * @param usb_dev generic usb device information
135 * @return error code
136 */
137int usb_hub_add_device(usb_device_t * usb_dev) {
138 if (!usb_dev) return EINVAL;
139 usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
140 //create hc connection
141 usb_log_debug("Initializing USB wire abstraction.\n");
142 int opResult = usb_hc_connection_initialize_from_device(
143 &hub_info->connection,
144 hub_info->usb_device->ddf_dev);
145 if (opResult != EOK) {
146 usb_log_error("could not initialize connection to device, "
147 "errno %d\n",
148 opResult);
149 free(hub_info);
150 return opResult;
151 }
152
153 usb_pipe_start_session(hub_info->control_pipe);
154 //set hub configuration
155 opResult = usb_hub_set_configuration(hub_info);
156 if (opResult != EOK) {
157 usb_log_error("could not set hub configuration, errno %d\n",
158 opResult);
159 free(hub_info);
160 return opResult;
161 }
162 //get port count and create attached_devs
163 opResult = usb_hub_process_hub_specific_info(hub_info);
164 if (opResult != EOK) {
165 usb_log_error("could not set hub configuration, errno %d\n",
166 opResult);
167 free(hub_info);
168 return opResult;
169 }
170 usb_pipe_end_session(hub_info->control_pipe);
171
172
173 /// \TODO what is this?
174 usb_log_debug("Creating `hub' function.\n");
175 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
176 fun_exposed, "hub");
177 assert(hub_fun != NULL);
178 hub_fun->ops = NULL;
179
180 int rc = ddf_fun_bind(hub_fun);
181 assert(rc == EOK);
182 rc = ddf_fun_add_to_class(hub_fun, "hub");
183 assert(rc == EOK);
184
185 //create fibril for the hub control loop
186 fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
187 if (fid == 0) {
188 usb_log_error("failed to start monitoring fibril for new"
189 " hub.\n");
190 return ENOMEM;
191 }
192 fibril_add_ready(fid);
193 usb_log_debug("Hub fibril created.\n");
194
195 usb_log_info("Controlling hub `%s' (%d ports).\n",
196 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
197 return EOK;
198}
199
200
201//*********************************************
202//
203// hub driver code, main loop and port handling
204//
205//*********************************************
206
207/**
208 * check changes on hub
209 *
210 * Handles changes on each port with a status change.
211 * @param hub_info hub representation
212 * @return error code
213 */
214int usb_hub_check_hub_changes(usb_hub_info_t * hub_info) {
215 int opResult;
216 opResult = usb_pipe_start_session(
217 hub_info->status_change_pipe);
218 //this might not be necessary - if all non-removables are ok, it is
219 //not needed here
220 opResult = usb_pipe_start_session(hub_info->control_pipe);
221 if (opResult != EOK) {
222 usb_log_error("could not initialize communication for hub; %d\n",
223 opResult);
224 return opResult;
225 }
226
227 size_t port_count = hub_info->port_count;
228 //first check non-removable devices
229 {
230 unsigned int port;
231 for (port = 0; port < port_count; ++port) {
232 bool is_non_removable =
233 hub_info->not_initialized_non_removables[port/8]
234 & (1 << (port-1 % 8));
235 if (is_non_removable) {
236 opResult = initialize_non_removable(hub_info,
237 port+1);
238 }
239 }
240 }
241
242
243 /// FIXME: count properly
244 size_t byte_length = ((port_count + 1) / 8) + 1;
245 void *change_bitmap = malloc(byte_length);
246 size_t actual_size;
247
248 /*
249 * Send the request.
250 */
251 opResult = usb_pipe_read(
252 hub_info->status_change_pipe,
253 change_bitmap, byte_length, &actual_size
254 );
255
256 if (opResult != EOK) {
257 free(change_bitmap);
258 usb_log_warning("something went wrong while getting the"
259 "status of hub\n");
260 usb_pipe_end_session(hub_info->status_change_pipe);
261 return opResult;
262 }
263 unsigned int port;
264
265 if (opResult != EOK) {
266 usb_log_error("could not start control pipe session %d\n",
267 opResult);
268 usb_pipe_end_session(hub_info->status_change_pipe);
269 return opResult;
270 }
271 opResult = usb_hc_connection_open(&hub_info->connection);
272 if (opResult != EOK) {
273 usb_log_error("could not start host controller session %d\n",
274 opResult);
275 usb_pipe_end_session(hub_info->control_pipe);
276 usb_pipe_end_session(hub_info->status_change_pipe);
277 return opResult;
278 }
279
280 ///todo, opresult check, pre obe konekce
281 bool interrupt;
282 interrupt = ((uint8_t*)change_bitmap)[0] & 1;
283 if(interrupt){
284 usb_hub_process_global_interrupt(hub_info);
285 }
286 for (port = 1; port < port_count + 1; ++port) {
287 interrupt =
288 ((uint8_t*) change_bitmap)[port / 8] & (1<<(port % 8));
289 if (interrupt) {
290 usb_hub_process_interrupt(
291 hub_info, port);
292 }
293 }
294 /// \todo check hub status
295 usb_hc_connection_close(&hub_info->connection);
296 usb_pipe_end_session(hub_info->control_pipe);
297 usb_pipe_end_session(hub_info->status_change_pipe);
298 free(change_bitmap);
299 return EOK;
300}
301
302//*********************************************
303//
304// support functions
305//
306//*********************************************
307
308/**
309 * create usb_hub_info_t structure
310 *
311 * Does only basic copying of known information into new structure.
312 * @param usb_dev usb device structure
313 * @return basic usb_hub_info_t structure
314 */
315static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
316 usb_hub_info_t * result = usb_new(usb_hub_info_t);
317 if (!result) return NULL;
318 result->usb_device = usb_dev;
319 result->status_change_pipe = usb_dev->pipes[0].pipe;
320 result->control_pipe = &usb_dev->ctrl_pipe;
321 result->is_default_address_used = false;
322 return result;
323}
324
325
326/**
327 * Load hub-specific information into hub_info structure and process if needed
328 *
329 * Particularly read port count and initialize structure holding port
330 * information. If there are non-removable devices, start initializing them.
331 * This function is hub-specific and should be run only after the hub is
332 * configured using usb_hub_set_configuration function.
333 * @param hub_info hub representation
334 * @return error code
335 */
336static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info) {
337 // get hub descriptor
338 usb_log_debug("creating serialized descriptor\n");
339 void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
340 usb_hub_descriptor_t * descriptor;
341 int opResult;
342
343 /* this was one fix of some bug, should not be needed anymore
344 * these lines allow to reset hub once more, it can be used as
345 * brute-force initialization for non-removable devices
346 */
347 opResult = usb_request_set_configuration(hub_info->control_pipe,
348 1);
349 if (opResult != EOK) {
350 usb_log_error("could not set default configuration, errno %d",
351 opResult);
352 return opResult;
353 }
354
355
356 size_t received_size;
357 opResult = usb_request_get_descriptor(hub_info->control_pipe,
358 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
359 USB_DESCTYPE_HUB,
360 0, 0, serialized_descriptor,
361 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
362
363 if (opResult != EOK) {
364 usb_log_error("failed when receiving hub descriptor, "
365 "badcode = %d\n",
366 opResult);
367 free(serialized_descriptor);
368 return opResult;
369 }
370 usb_log_debug2("deserializing descriptor\n");
371 descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
372 if (descriptor == NULL) {
373 usb_log_warning("could not deserialize descriptor \n");
374 return opResult;
375 }
376 usb_log_debug("setting port count to %d\n", descriptor->ports_count);
377 hub_info->port_count = descriptor->ports_count;
378 /// \TODO check attached_devices array: this is not semantically correct
379 hub_info->attached_devs = (usb_hc_attached_device_t*)
380 malloc((hub_info->port_count + 1) *
381 sizeof (usb_hc_attached_device_t)
382 );
383 int i;
384 for (i = 1; i <= hub_info->port_count; ++i) {
385 hub_info->attached_devs[i].handle = 0;
386 hub_info->attached_devs[i].address = 0;
387 usb_log_info("powering port %d\n",i);
388 opResult = usb_hub_set_port_feature(
389 hub_info->control_pipe,
390 i,
391 USB_HUB_FEATURE_PORT_POWER);
392 if(opResult!=EOK)
393 usb_log_warning("could not power port %d\n",i);
394
395 }
396 //handle non-removable devices
397 usb_hub_trigger_connecting_non_removable_devices(hub_info, descriptor);
398 usb_log_debug2("freeing data\n");
399 free(serialized_descriptor);
400 hub_info->descriptor = descriptor;
401 hub_info->not_initialized_non_removables =
402 (uint8_t*) malloc((hub_info->port_count + 8) / 8);
403 memcpy(hub_info->not_initialized_non_removables,
404 descriptor->devices_removable,
405 (hub_info->port_count + 8) / 8
406 );
407
408 //free(descriptor->devices_removable);
409 //free(descriptor);
410 return EOK;
411}
412
413/**
414 * Set configuration of hub
415 *
416 * Check whether there is at least one configuration and sets the first one.
417 * This function should be run prior to running any hub-specific action.
418 * @param hub_info hub representation
419 * @return error code
420 */
421static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
422 //device descriptor
423 usb_standard_device_descriptor_t *std_descriptor
424 = &hub_info->usb_device->descriptors.device;
425 usb_log_debug("hub has %d configurations\n",
426 std_descriptor->configuration_count);
427 if (std_descriptor->configuration_count < 1) {
428 usb_log_error("there are no configurations available\n");
429 return EINVAL;
430 }
431
432 usb_standard_configuration_descriptor_t *config_descriptor
433 = (usb_standard_configuration_descriptor_t *)
434 hub_info->usb_device->descriptors.configuration;
435
436 /* Set configuration. */
437 int opResult = usb_request_set_configuration(
438 &hub_info->usb_device->ctrl_pipe,
439 config_descriptor->configuration_number);
440
441 if (opResult != EOK) {
442 usb_log_error("Failed to set hub configuration: %s.\n",
443 str_error(opResult));
444 return opResult;
445 }
446 usb_log_debug("\tused configuration %d\n",
447 config_descriptor->configuration_number);
448
449 return EOK;
450}
451
452/**
453 * release default address used by given hub
454 *
455 * Also unsets hub->is_default_address_used. Convenience wrapper function.
456 * @note hub->connection MUST be open for communication
457 * @param hub hub representation
458 * @return error code
459 */
460static int usb_hub_release_default_address(usb_hub_info_t * hub) {
461 int opResult = usb_hc_release_default_address(&hub->connection);
462 if (opResult != EOK) {
463 usb_log_error("could not release default address, errno %d\n",
464 opResult);
465 return opResult;
466 }
467 hub->is_default_address_used = false;
468 return EOK;
469}
470
471/**
472 * Reset the port with new device and reserve the default address.
473 * @param hub hub representation
474 * @param port port number, starting from 1
475 * @param speed transfer speed of attached device, one of low, full or high
476 * @return error code
477 */
478static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
479 usb_speed_t speed) {
480 //if this hub already uses default address, it cannot request it once more
481 if (hub->is_default_address_used) {
482 usb_log_info("default address used, another time\n");
483 return EREFUSED;
484 }
485 usb_log_debug("some connection changed\n");
486 assert(hub->control_pipe->hc_phone);
487 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
488 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
489 if (opResult != EOK) {
490 usb_log_warning("could not clear port-change-connection flag\n");
491 }
492 usb_device_request_setup_packet_t request;
493
494 //get default address
495 opResult = usb_hc_reserve_default_address(&hub->connection, speed);
496
497 if (opResult != EOK) {
498 usb_log_warning("cannot assign default address, it is probably "
499 "used %d\n",
500 opResult);
501 return opResult;
502 }
503 hub->is_default_address_used = true;
504 //reset port
505 usb_hub_set_reset_port_request(&request, port);
506 opResult = usb_pipe_control_write(
507 hub->control_pipe,
508 &request, sizeof (usb_device_request_setup_packet_t),
509 NULL, 0
510 );
511 if (opResult != EOK) {
512 usb_log_error("something went wrong when reseting a port %d\n",
513 opResult);
514 usb_hub_release_default_address(hub);
515 }
516 return opResult;
517}
518
519/**
520 * Finalize adding new device after port reset
521 *
522 * Set device`s address and start it`s driver.
523 * @param hub hub representation
524 * @param port port number, starting from 1
525 * @param speed transfer speed of attached device, one of low, full or high
526 */
527static void usb_hub_finalize_add_device(usb_hub_info_t * hub,
528 uint16_t port, usb_speed_t speed) {
529
530 int opResult;
531 usb_log_debug("finalizing add device\n");
532 opResult = usb_hub_clear_port_feature(hub->control_pipe,
533 port, USB_HUB_FEATURE_C_PORT_RESET);
534
535 if (opResult != EOK) {
536 usb_log_error("failed to clear port reset feature\n");
537 usb_hub_release_default_address(hub);
538 return;
539 }
540 //create connection to device
541 usb_pipe_t new_device_pipe;
542 usb_device_connection_t new_device_connection;
543 usb_device_connection_initialize_on_default_address(
544 &new_device_connection,
545 &hub->connection
546 );
547 usb_pipe_initialize_default_control(
548 &new_device_pipe,
549 &new_device_connection);
550 usb_pipe_probe_default_control(&new_device_pipe);
551
552 /* Request address from host controller. */
553 usb_address_t new_device_address = usb_hc_request_address(
554 &hub->connection,
555 speed
556 );
557 if (new_device_address < 0) {
558 usb_log_error("failed to get free USB address\n");
559 opResult = new_device_address;
560 usb_hub_release_default_address(hub);
561 return;
562 }
563 usb_log_debug("setting new address %d\n", new_device_address);
564 //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
565 // new_device_address);
566 usb_pipe_start_session(&new_device_pipe);
567 opResult = usb_request_set_address(&new_device_pipe,
568 new_device_address);
569 usb_pipe_end_session(&new_device_pipe);
570 if (opResult != EOK) {
571 usb_log_error("could not set address for new device %d\n",
572 opResult);
573 usb_hub_release_default_address(hub);
574 return;
575 }
576
577 //opResult = usb_hub_release_default_address(hc);
578 opResult = usb_hub_release_default_address(hub);
579 if (opResult != EOK) {
580 return;
581 }
582
583 devman_handle_t child_handle;
584 //??
585 opResult = usb_device_register_child_in_devman(new_device_address,
586 hub->connection.hc_handle, hub->usb_device->ddf_dev,
587 &child_handle,
588 NULL, NULL, NULL);
589
590 if (opResult != EOK) {
591 usb_log_error("could not start driver for new device %d\n",
592 opResult);
593 return;
594 }
595 hub->attached_devs[port].handle = child_handle;
596 hub->attached_devs[port].address = new_device_address;
597
598 //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
599 opResult = usb_hc_register_device(
600 &hub->connection,
601 &hub->attached_devs[port]);
602 if (opResult != EOK) {
603 usb_log_error("could not assign address of device in hcd %d\n",
604 opResult);
605 return;
606 }
607 usb_log_info("Detected new device on `%s' (port %d), " \
608 "address %d (handle %llu).\n",
609 hub->usb_device->ddf_dev->name, (int) port,
610 new_device_address, child_handle);
611}
612
613/**
614 * routine called when a device on port has been removed
615 *
616 * If the device on port had default address, it releases default address.
617 * Otherwise does not do anything, because DDF does not allow to remove device
618 * from it`s device tree.
619 * @param hub hub representation
620 * @param port port number, starting from 1
621 */
622static void usb_hub_removed_device(
623 usb_hub_info_t * hub, uint16_t port) {
624
625 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
626 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
627 if (opResult != EOK) {
628 usb_log_warning("could not clear port-change-connection flag\n");
629 }
630 /** \TODO remove device from device manager - not yet implemented in
631 * devide manager
632 */
633
634 //close address
635 if (hub->attached_devs[port].address != 0) {
636 /*uncomment this code to use it when DDF allows device removal
637 opResult = usb_hc_unregister_device(
638 &hub->connection,
639 hub->attached_devs[port].address);
640 if(opResult != EOK) {
641 dprintf(USB_LOG_LEVEL_WARNING, "could not release "
642 "address of "
643 "removed device: %d", opResult);
644 }
645 hub->attached_devs[port].address = 0;
646 hub->attached_devs[port].handle = 0;
647 */
648 } else {
649 usb_log_warning("this is strange, disconnected device had "
650 "no address\n");
651 //device was disconnected before it`s port was reset -
652 //return default address
653 usb_hub_release_default_address(hub);
654 }
655}
656
657/**
658 * Process over current condition on port.
659 *
660 * Turn off the power on the port.
661 *
662 * @param hub hub representation
663 * @param port port number, starting from 1
664 */
665static void usb_hub_port_over_current(usb_hub_info_t * hub,
666 uint16_t port, uint32_t status) {
667 int opResult;
668 if(usb_port_over_current(&status)){
669 opResult = usb_hub_clear_port_feature(hub->control_pipe,
670 port, USB_HUB_FEATURE_PORT_POWER);
671 if (opResult != EOK) {
672 usb_log_error("cannot power off port %d; %d\n",
673 port, opResult);
674 }
675 }else{
676 opResult = usb_hub_set_port_feature(hub->control_pipe,
677 port, USB_HUB_FEATURE_PORT_POWER);
678 if (opResult != EOK) {
679 usb_log_error("cannot power on port %d; %d\n",
680 port, opResult);
681 }
682 }
683}
684
685/**
686 * Process interrupts on given hub port
687 *
688 * Accepts connection, over current and port reset change.
689 * @param hub hub representation
690 * @param port port number, starting from 1
691 */
692static void usb_hub_process_interrupt(usb_hub_info_t * hub,
693 uint16_t port) {
694 usb_log_debug("interrupt at port %d\n", port);
695 //determine type of change
696 usb_pipe_t *pipe = hub->control_pipe;
697
698 int opResult;
699
700 usb_port_status_t status;
701 size_t rcvd_size;
702 usb_device_request_setup_packet_t request;
703 //int opResult;
704 usb_hub_set_port_status_request(&request, port);
705 //endpoint 0
706
707 opResult = usb_pipe_control_read(
708 pipe,
709 &request, sizeof (usb_device_request_setup_packet_t),
710 &status, 4, &rcvd_size
711 );
712 if (opResult != EOK) {
713 usb_log_error("could not get port status\n");
714 return;
715 }
716 if (rcvd_size != sizeof (usb_port_status_t)) {
717 usb_log_error("received status has incorrect size\n");
718 return;
719 }
720 //something connected/disconnected
721 if (usb_port_connect_change(&status)) {
722 usb_log_debug("connection change on port\n");
723 if (usb_port_dev_connected(&status)) {
724 usb_hub_init_add_device(hub, port,
725 usb_port_speed(&status));
726 } else {
727 usb_hub_removed_device(hub, port);
728 }
729 }
730 //over current
731 if (usb_port_overcurrent_change(&status)) {
732 //check if it was not auto-resolved
733 usb_log_debug("overcurrent change on port\n");
734 usb_hub_port_over_current(hub, port, status);
735 }
736 //port reset
737 if (usb_port_reset_completed(&status)) {
738 usb_log_debug("port reset complete\n");
739 if (usb_port_enabled(&status)) {
740 usb_hub_finalize_add_device(hub, port,
741 usb_port_speed(&status));
742 } else {
743 usb_log_warning("port reset, but port still not "
744 "enabled\n");
745 }
746 }
747 usb_log_debug("status x%x : %d\n ", status, status);
748
749 usb_port_set_connect_change(&status, false);
750 usb_port_set_reset(&status, false);
751 usb_port_set_reset_completed(&status, false);
752 usb_port_set_dev_connected(&status, false);
753 usb_port_set_overcurrent_change(&status,false);
754 /// \TODO what about port power change?
755 if (status >> 16) {
756 usb_log_info("there was unsupported change on port %d: %X\n",
757 port, status);
758
759 }
760}
761
762/**
763 * process hub over current change
764 *
765 * This means either to power off the hub or power it on.
766 * @param hub_info hub instance
767 * @param status hub status bitmask
768 * @return error code
769 */
770static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
771 usb_hub_status_t status)
772{
773 int opResult;
774 if(usb_hub_over_current(&status)){
775 opResult = usb_hub_clear_feature(hub_info->control_pipe,
776 USB_HUB_FEATURE_PORT_POWER);
777 if (opResult != EOK) {
778 usb_log_error("cannot power off hub: %d\n",
779 opResult);
780 }
781 }else{
782 opResult = usb_hub_set_feature(hub_info->control_pipe,
783 USB_HUB_FEATURE_PORT_POWER);
784 if (opResult != EOK) {
785 usb_log_error("cannot power on hub: %d\n",
786 opResult);
787 }
788 }
789 return opResult;
790}
791
792/**
793 * process hub power change
794 *
795 * If the power has been lost, reestablish it.
796 * If it was reestablished, re-power all ports.
797 * @param hub_info hub instance
798 * @param status hub status bitmask
799 * @return error code
800 */
801static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
802 usb_hub_status_t status)
803{
804 int opResult;
805 if(usb_hub_local_power_lost(&status)){
806 //restart power on hub
807 opResult = usb_hub_set_feature(hub_info->control_pipe,
808 USB_HUB_FEATURE_PORT_POWER);
809 if (opResult != EOK) {
810 usb_log_error("cannot power on hub: %d\n",
811 opResult);
812 }
813 }else{//power reestablished on hub- restart ports
814 int port;
815 for(port=0;port<hub_info->port_count;++port){
816 opResult = usb_hub_set_port_feature(
817 hub_info->control_pipe,
818 port, USB_HUB_FEATURE_PORT_POWER);
819 if (opResult != EOK) {
820 usb_log_error("cannot power on port %d; %d\n",
821 port, opResult);
822 }
823 }
824 }
825 return opResult;
826}
827
828/**
829 * process hub interrupts
830 *
831 * The change can be either in the over-current condition or
832 * local-power lost condition.
833 * @param hub_info hub instance
834 */
835static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info){
836 usb_log_debug("global interrupt on a hub\n");
837 usb_pipe_t *pipe = hub_info->control_pipe;
838 int opResult;
839
840 usb_port_status_t status;
841 size_t rcvd_size;
842 usb_device_request_setup_packet_t request;
843 //int opResult;
844 usb_hub_set_hub_status_request(&request);
845 //endpoint 0
846
847 opResult = usb_pipe_control_read(
848 pipe,
849 &request, sizeof (usb_device_request_setup_packet_t),
850 &status, 4, &rcvd_size
851 );
852 if (opResult != EOK) {
853 usb_log_error("could not get hub status\n");
854 return;
855 }
856 if (rcvd_size != sizeof (usb_port_status_t)) {
857 usb_log_error("received status has incorrect size\n");
858 return;
859 }
860 //port reset
861 if (usb_hub_over_current_change(&status)) {
862 usb_process_hub_over_current(hub_info,status);
863 }
864 if (usb_hub_local_power_change(&status)) {
865 usb_process_hub_power_change(hub_info,status);
866 }
867}
868
869//-----------attempts to solve non-removable------------------------
870//-----------attempts to solve non-removable------------------------
871//-----------attempts to solve non-removable------------------------
872//-----------attempts to solve non-removable------------------------
873
874/**
875 * this is an attempt to initialize non-removable devices in the hub
876 *
877 * @param hub_info hub instance
878 * @param port port number, counting from 1
879 * @return error code
880 */
881static int initialize_non_removable(usb_hub_info_t * hub_info,
882 unsigned int port) {
883 int opResult;
884 usb_log_debug("there is not pluged in non-removable device on "
885 "port %d\n", port
886 );
887 //usb_hub_init_add_device(hub_info, port, usb_port_speed(&status));
888 usb_port_status_t status;
889 size_t rcvd_size;
890 usb_device_request_setup_packet_t request;
891 //int opResult;
892 usb_hub_set_port_status_request(&request, port);
893 //endpoint 0
894
895 opResult = usb_pipe_control_read(
896 hub_info->control_pipe,
897 &request, sizeof (usb_device_request_setup_packet_t),
898 &status, 4, &rcvd_size
899 );
900 if (opResult != EOK) {
901 usb_log_error("could not get port status %d\n", opResult);
902 return opResult;
903 }
904 if (rcvd_size != sizeof (usb_port_status_t)) {
905 usb_log_error("received status has incorrect size\n");
906 return opResult;
907 }
908 usb_log_debug("port status %d, x%x\n", status, status);
909 if (usb_port_dev_connected(&status)) {
910 usb_log_debug("there is connected device on this port\n");
911 opResult = usb_hub_init_add_device(hub_info, port,
912 usb_port_speed(&status));
913 }else{
914 usb_log_debug("the non-removable device is not connected\n");
915 opResult = EINVAL;
916 }
917
918 return opResult;
919}
920
921/**
922 * triggers actions to connect non0removable devices
923 *
924 * This will trigger operations leading to activated non-removable device.
925 * Control pipe of the hub must be open fo communication.
926 * @param hub hub representation
927 * @param descriptor usb hub descriptor
928 * @return error code
929 */
930static int usb_hub_trigger_connecting_non_removable_devices(
931 usb_hub_info_t * hub,
932 usb_hub_descriptor_t * descriptor) {
933 usb_log_info("attaching non-removable devices(if any)\n");
934 //usb_device_request_setup_packet_t request;
935 int opResult;
936 //size_t rcvd_size;
937 //usb_port_status_t status;
938 uint8_t * non_removable_dev_bitmap = descriptor->devices_removable;
939 int port;
940#if 0
941 opResult = usb_request_set_configuration(hub->control_pipe,
942 1);
943 if (opResult != EOK) {
944 usb_log_error("could not set default configuration, errno %d",
945 opResult);
946 return opResult;
947 }
948
949 for (port = 1; port <= descriptor->ports_count; ++port) {
950 bool is_non_removable =
951 ((non_removable_dev_bitmap[port / 8]) >> (port % 8)) % 2;
952 if (is_non_removable) {
953 usb_log_debug("non-removable device on port %d\n", port);
954 usb_hub_set_port_status_request(&request, port);
955 opResult = usb_pipe_control_read(
956 hub->control_pipe,
957 &request,
958 sizeof (usb_device_request_setup_packet_t),
959 &status, 4, &rcvd_size
960 );
961 if (opResult != EOK) {
962 usb_log_error("could not get port status of "
963 "port %d errno:%d\n",
964 port, opResult);
965 return opResult;
966 }
967 //try to reset port
968 if (usb_port_dev_connected(&status) || true) {
969 usb_hub_set_enable_port_feature_request(
970 &request, port,
971 USB_HUB_FEATURE_PORT_RESET);
972 opResult = usb_pipe_control_read(
973 hub->control_pipe,
974 &request,
975 sizeof (usb_device_request_setup_packet_t),
976 &status, 4, &rcvd_size
977 );
978 if (opResult != EOK) {
979 usb_log_warning(
980 "could not reset port %d "
981 "errno:%d\n",
982 port, opResult);
983 }
984 usb_log_debug("port reset, should look like "
985 "%d,x%x\n",
986 (1 << USB_HUB_FEATURE_PORT_RESET),
987 (1 << USB_HUB_FEATURE_PORT_RESET)
988 );
989 }
990 //set the status change bit, so it will be noticed
991 //in driver loop
992 if (usb_port_dev_connected(&status) && false) {
993 usb_hub_set_disable_port_feature_request(
994 &request, port,
995 USB_HUB_FEATURE_PORT_CONNECTION);
996 opResult = usb_pipe_control_read(
997 hub->control_pipe,
998 &request,
999 sizeof (usb_device_request_setup_packet_t),
1000 &status, 4, &rcvd_size
1001 );
1002 if (opResult != EOK) {
1003 usb_log_warning(
1004 "could not clear port "
1005 "connection on port %d "
1006 "errno:%d\n",
1007 port, opResult);
1008 }
1009 usb_log_debug("cleared port connection\n");
1010 usb_hub_set_enable_port_feature_request(&request,
1011 port,
1012 USB_HUB_FEATURE_PORT_ENABLE);
1013 opResult = usb_pipe_control_read(
1014 hub->control_pipe,
1015 &request,
1016 sizeof (usb_device_request_setup_packet_t),
1017 &status, 4, &rcvd_size
1018 );
1019 if (opResult != EOK) {
1020 usb_log_warning(
1021 "could not set port enabled "
1022 "on port %d errno:%d\n",
1023 port, opResult);
1024 }
1025 usb_log_debug("port set to enabled - "
1026 "should lead to connection change\n");
1027 }
1028 }
1029 }
1030#endif
1031
1032 /// \TODO this is just a debug code
1033 for (port = 1; port <= descriptor->ports_count; ++port) {
1034 bool is_non_removable =
1035 ((non_removable_dev_bitmap[(port-1) / 8]) >> ((port-1) % 8)) % 2;
1036 if (is_non_removable) {
1037 usb_log_debug("CHECKING port %d is non-removable\n",
1038 port);
1039 usb_port_status_t status;
1040 size_t rcvd_size;
1041 usb_device_request_setup_packet_t request;
1042 //int opResult;
1043 usb_hub_set_port_status_request(&request, port);
1044 //endpoint 0
1045 opResult = usb_pipe_control_read(
1046 hub->control_pipe,
1047 &request,
1048 sizeof (usb_device_request_setup_packet_t),
1049 &status, 4, &rcvd_size
1050 );
1051 if (opResult != EOK) {
1052 usb_log_error("could not get port status %d\n",
1053 opResult);
1054 }
1055 if (rcvd_size != sizeof (usb_port_status_t)) {
1056 usb_log_error("received status has incorrect"
1057 " size\n");
1058 }
1059 //something connected/disconnected
1060 if (usb_port_connect_change(&status)) {
1061 usb_log_debug("some connection changed\n");
1062 }
1063 if(usb_port_dev_connected(&status)){
1064 usb_log_debug("device connected on port\n");
1065 }
1066 usb_log_debug("status: %s\n", usb_debug_str_buffer(
1067 (uint8_t *) & status, 4, 4));
1068 }
1069 }
1070 async_usleep(1000*1000*10);
1071 return EOK;
1072}
1073
1074
1075/**
1076 * @}
1077 */
Note: See TracBrowser for help on using the repository browser.