source: mainline/uspace/drv/usbhub/usbhub.c@ d7f3040

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

fix attempt #2

  • Property mode set to 100644
File size: 30.3 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->usb_device->ctrl_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 hub_info->attached_devs = (usb_hc_attached_device_t*)
379 malloc((hub_info->port_count + 1) *
380 sizeof (usb_hc_attached_device_t)
381 );
382 int i;
383 for (i = 0; i < hub_info->port_count + 1; ++i) {
384 hub_info->attached_devs[i].handle = 0;
385 hub_info->attached_devs[i].address = 0;
386 }
387 //handle non-removable devices
388 usb_hub_trigger_connecting_non_removable_devices(hub_info, descriptor);
389 usb_log_debug2("freeing data\n");
390 free(serialized_descriptor);
391 hub_info->descriptor = descriptor;
392 hub_info->not_initialized_non_removables =
393 (uint8_t*) malloc((hub_info->port_count + 8) / 8);
394 memcpy(hub_info->not_initialized_non_removables,
395 descriptor->devices_removable,
396 (hub_info->port_count + 8) / 8
397 );
398
399 //free(descriptor->devices_removable);
400 //free(descriptor);
401 return EOK;
402}
403
404/**
405 * Set configuration of hub
406 *
407 * Check whether there is at least one configuration and sets the first one.
408 * This function should be run prior to running any hub-specific action.
409 * @param hub_info hub representation
410 * @return error code
411 */
412static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
413 //device descriptor
414 usb_standard_device_descriptor_t *std_descriptor
415 = &hub_info->usb_device->descriptors.device;
416 usb_log_debug("hub has %d configurations\n",
417 std_descriptor->configuration_count);
418 if (std_descriptor->configuration_count < 1) {
419 usb_log_error("there are no configurations available\n");
420 return EINVAL;
421 }
422
423 usb_standard_configuration_descriptor_t *config_descriptor
424 = (usb_standard_configuration_descriptor_t *)
425 hub_info->usb_device->descriptors.configuration;
426
427 /* Set configuration. */
428 int opResult = usb_request_set_configuration(
429 &hub_info->usb_device->ctrl_pipe,
430 config_descriptor->configuration_number);
431
432 if (opResult != EOK) {
433 usb_log_error("Failed to set hub configuration: %s.\n",
434 str_error(opResult));
435 return opResult;
436 }
437 usb_log_debug("\tused configuration %d\n",
438 config_descriptor->configuration_number);
439
440 return EOK;
441}
442
443/**
444 * release default address used by given hub
445 *
446 * Also unsets hub->is_default_address_used. Convenience wrapper function.
447 * @note hub->connection MUST be open for communication
448 * @param hub hub representation
449 * @return error code
450 */
451static int usb_hub_release_default_address(usb_hub_info_t * hub) {
452 int opResult = usb_hc_release_default_address(&hub->connection);
453 if (opResult != EOK) {
454 usb_log_error("could not release default address, errno %d\n",
455 opResult);
456 return opResult;
457 }
458 hub->is_default_address_used = false;
459 return EOK;
460}
461
462/**
463 * Reset the port with new device and reserve the default address.
464 * @param hub hub representation
465 * @param port port number, starting from 1
466 * @param speed transfer speed of attached device, one of low, full or high
467 * @return error code
468 */
469static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
470 usb_speed_t speed) {
471 //if this hub already uses default address, it cannot request it once more
472 if (hub->is_default_address_used) {
473 usb_log_info("default address used, another time\n");
474 return EREFUSED;
475 }
476 usb_log_debug("some connection changed\n");
477 assert(hub->control_pipe->hc_phone);
478 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
479 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
480 if (opResult != EOK) {
481 usb_log_warning("could not clear port-change-connection flag\n");
482 }
483 usb_device_request_setup_packet_t request;
484
485 //get default address
486 opResult = usb_hc_reserve_default_address(&hub->connection, speed);
487
488 if (opResult != EOK) {
489 usb_log_warning("cannot assign default address, it is probably "
490 "used %d\n",
491 opResult);
492 return opResult;
493 }
494 hub->is_default_address_used = true;
495 //reset port
496 usb_hub_set_reset_port_request(&request, port);
497 opResult = usb_pipe_control_write(
498 hub->control_pipe,
499 &request, sizeof (usb_device_request_setup_packet_t),
500 NULL, 0
501 );
502 if (opResult != EOK) {
503 usb_log_error("something went wrong when reseting a port %d\n",
504 opResult);
505 usb_hub_release_default_address(hub);
506 }
507 return opResult;
508}
509
510/**
511 * Finalize adding new device after port reset
512 *
513 * Set device`s address and start it`s driver.
514 * @param hub hub representation
515 * @param port port number, starting from 1
516 * @param speed transfer speed of attached device, one of low, full or high
517 */
518static void usb_hub_finalize_add_device(usb_hub_info_t * hub,
519 uint16_t port, usb_speed_t speed) {
520
521 int opResult;
522 usb_log_debug("finalizing add device\n");
523 opResult = usb_hub_clear_port_feature(hub->control_pipe,
524 port, USB_HUB_FEATURE_C_PORT_RESET);
525
526 if (opResult != EOK) {
527 usb_log_error("failed to clear port reset feature\n");
528 usb_hub_release_default_address(hub);
529 return;
530 }
531 //create connection to device
532 usb_pipe_t new_device_pipe;
533 usb_device_connection_t new_device_connection;
534 usb_device_connection_initialize_on_default_address(
535 &new_device_connection,
536 &hub->connection
537 );
538 usb_pipe_initialize_default_control(
539 &new_device_pipe,
540 &new_device_connection);
541 usb_pipe_probe_default_control(&new_device_pipe);
542
543 /* Request address from host controller. */
544 usb_address_t new_device_address = usb_hc_request_address(
545 &hub->connection,
546 speed
547 );
548 if (new_device_address < 0) {
549 usb_log_error("failed to get free USB address\n");
550 opResult = new_device_address;
551 usb_hub_release_default_address(hub);
552 return;
553 }
554 usb_log_debug("setting new address %d\n", new_device_address);
555 //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
556 // new_device_address);
557 usb_pipe_start_session(&new_device_pipe);
558 opResult = usb_request_set_address(&new_device_pipe,
559 new_device_address);
560 usb_pipe_end_session(&new_device_pipe);
561 if (opResult != EOK) {
562 usb_log_error("could not set address for new device %d\n",
563 opResult);
564 usb_hub_release_default_address(hub);
565 return;
566 }
567
568 //opResult = usb_hub_release_default_address(hc);
569 opResult = usb_hub_release_default_address(hub);
570 if (opResult != EOK) {
571 return;
572 }
573
574 devman_handle_t child_handle;
575 //??
576 opResult = usb_device_register_child_in_devman(new_device_address,
577 hub->connection.hc_handle, hub->usb_device->ddf_dev,
578 &child_handle,
579 NULL, NULL, NULL);
580
581 if (opResult != EOK) {
582 usb_log_error("could not start driver for new device %d\n",
583 opResult);
584 return;
585 }
586 hub->attached_devs[port].handle = child_handle;
587 hub->attached_devs[port].address = new_device_address;
588
589 //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
590 opResult = usb_hc_register_device(
591 &hub->connection,
592 &hub->attached_devs[port]);
593 if (opResult != EOK) {
594 usb_log_error("could not assign address of device in hcd %d\n",
595 opResult);
596 return;
597 }
598 usb_log_info("Detected new device on `%s' (port %d), " \
599 "address %d (handle %llu).\n",
600 hub->usb_device->ddf_dev->name, (int) port,
601 new_device_address, child_handle);
602}
603
604/**
605 * routine called when a device on port has been removed
606 *
607 * If the device on port had default address, it releases default address.
608 * Otherwise does not do anything, because DDF does not allow to remove device
609 * from it`s device tree.
610 * @param hub hub representation
611 * @param port port number, starting from 1
612 */
613static void usb_hub_removed_device(
614 usb_hub_info_t * hub, uint16_t port) {
615
616 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
617 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
618 if (opResult != EOK) {
619 usb_log_warning("could not clear port-change-connection flag\n");
620 }
621 /** \TODO remove device from device manager - not yet implemented in
622 * devide manager
623 */
624
625 //close address
626 if (hub->attached_devs[port].address != 0) {
627 /*uncomment this code to use it when DDF allows device removal
628 opResult = usb_hc_unregister_device(
629 &hub->connection,
630 hub->attached_devs[port].address);
631 if(opResult != EOK) {
632 dprintf(USB_LOG_LEVEL_WARNING, "could not release "
633 "address of "
634 "removed device: %d", opResult);
635 }
636 hub->attached_devs[port].address = 0;
637 hub->attached_devs[port].handle = 0;
638 */
639 } else {
640 usb_log_warning("this is strange, disconnected device had "
641 "no address\n");
642 //device was disconnected before it`s port was reset -
643 //return default address
644 usb_hub_release_default_address(hub);
645 }
646}
647
648/**
649 * Process over current condition on port.
650 *
651 * Turn off the power on the port.
652 *
653 * @param hub hub representation
654 * @param port port number, starting from 1
655 */
656static void usb_hub_port_over_current(usb_hub_info_t * hub,
657 uint16_t port, uint32_t status) {
658 int opResult;
659 if(usb_port_over_current(&status)){
660 opResult = usb_hub_clear_port_feature(hub->control_pipe,
661 port, USB_HUB_FEATURE_PORT_POWER);
662 if (opResult != EOK) {
663 usb_log_error("cannot power off port %d; %d\n",
664 port, opResult);
665 }
666 }else{
667 opResult = usb_hub_set_port_feature(hub->control_pipe,
668 port, USB_HUB_FEATURE_PORT_POWER);
669 if (opResult != EOK) {
670 usb_log_error("cannot power on port %d; %d\n",
671 port, opResult);
672 }
673 }
674}
675
676/**
677 * Process interrupts on given hub port
678 *
679 * Accepts connection, over current and port reset change.
680 * @param hub hub representation
681 * @param port port number, starting from 1
682 */
683static void usb_hub_process_interrupt(usb_hub_info_t * hub,
684 uint16_t port) {
685 usb_log_debug("interrupt at port %d\n", port);
686 //determine type of change
687 usb_pipe_t *pipe = hub->control_pipe;
688
689 int opResult;
690
691 usb_port_status_t status;
692 size_t rcvd_size;
693 usb_device_request_setup_packet_t request;
694 //int opResult;
695 usb_hub_set_port_status_request(&request, port);
696 //endpoint 0
697
698 opResult = usb_pipe_control_read(
699 pipe,
700 &request, sizeof (usb_device_request_setup_packet_t),
701 &status, 4, &rcvd_size
702 );
703 if (opResult != EOK) {
704 usb_log_error("could not get port status\n");
705 return;
706 }
707 if (rcvd_size != sizeof (usb_port_status_t)) {
708 usb_log_error("received status has incorrect size\n");
709 return;
710 }
711 //something connected/disconnected
712 if (usb_port_connect_change(&status)) {
713 usb_log_debug("connection change on port\n");
714 if (usb_port_dev_connected(&status)) {
715 usb_hub_init_add_device(hub, port,
716 usb_port_speed(&status));
717 } else {
718 usb_hub_removed_device(hub, port);
719 }
720 }
721 //over current
722 if (usb_port_overcurrent_change(&status)) {
723 //check if it was not auto-resolved
724 usb_log_debug("overcurrent change on port\n");
725 usb_hub_port_over_current(hub, port, status);
726 }
727 //port reset
728 if (usb_port_reset_completed(&status)) {
729 usb_log_debug("port reset complete\n");
730 if (usb_port_enabled(&status)) {
731 usb_hub_finalize_add_device(hub, port,
732 usb_port_speed(&status));
733 } else {
734 usb_log_warning("port reset, but port still not "
735 "enabled\n");
736 }
737 }
738 usb_log_debug("status x%x : %d\n ", status, status);
739
740 usb_port_set_connect_change(&status, false);
741 usb_port_set_reset(&status, false);
742 usb_port_set_reset_completed(&status, false);
743 usb_port_set_dev_connected(&status, false);
744 usb_port_set_overcurrent_change(&status,false);
745 /// \TODO what about port power change?
746 if (status >> 16) {
747 usb_log_info("there was unsupported change on port %d: %X\n",
748 port, status);
749
750 }
751}
752
753/**
754 * process hub over current change
755 *
756 * This means either to power off the hub or power it on.
757 * @param hub_info hub instance
758 * @param status hub status bitmask
759 * @return error code
760 */
761static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
762 usb_hub_status_t status)
763{
764 int opResult;
765 if(usb_hub_over_current(&status)){
766 opResult = usb_hub_clear_feature(hub_info->control_pipe,
767 USB_HUB_FEATURE_PORT_POWER);
768 if (opResult != EOK) {
769 usb_log_error("cannot power off hub: %d\n",
770 opResult);
771 }
772 }else{
773 opResult = usb_hub_set_feature(hub_info->control_pipe,
774 USB_HUB_FEATURE_PORT_POWER);
775 if (opResult != EOK) {
776 usb_log_error("cannot power on hub: %d\n",
777 opResult);
778 }
779 }
780 return opResult;
781}
782
783/**
784 * process hub power change
785 *
786 * If the power has been lost, reestablish it.
787 * If it was reestablished, re-power all ports.
788 * @param hub_info hub instance
789 * @param status hub status bitmask
790 * @return error code
791 */
792static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
793 usb_hub_status_t status)
794{
795 int opResult;
796 if(usb_hub_local_power_lost(&status)){
797 //restart power on hub
798 opResult = usb_hub_set_feature(hub_info->control_pipe,
799 USB_HUB_FEATURE_PORT_POWER);
800 if (opResult != EOK) {
801 usb_log_error("cannot power on hub: %d\n",
802 opResult);
803 }
804 }else{//power reestablished on hub- restart ports
805 int port;
806 for(port=0;port<hub_info->port_count;++port){
807 opResult = usb_hub_set_port_feature(
808 hub_info->control_pipe,
809 port, USB_HUB_FEATURE_PORT_POWER);
810 if (opResult != EOK) {
811 usb_log_error("cannot power on port %d; %d\n",
812 port, opResult);
813 }
814 }
815 }
816 return opResult;
817}
818
819/**
820 * process hub interrupts
821 *
822 * The change can be either in the over-current condition or
823 * local-power lost condition.
824 * @param hub_info hub instance
825 */
826static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info){
827 usb_log_debug("global interrupt on a hub\n");
828 usb_pipe_t *pipe = hub_info->control_pipe;
829 int opResult;
830
831 usb_port_status_t status;
832 size_t rcvd_size;
833 usb_device_request_setup_packet_t request;
834 //int opResult;
835 usb_hub_set_hub_status_request(&request);
836 //endpoint 0
837
838 opResult = usb_pipe_control_read(
839 pipe,
840 &request, sizeof (usb_device_request_setup_packet_t),
841 &status, 4, &rcvd_size
842 );
843 if (opResult != EOK) {
844 usb_log_error("could not get hub status\n");
845 return;
846 }
847 if (rcvd_size != sizeof (usb_port_status_t)) {
848 usb_log_error("received status has incorrect size\n");
849 return;
850 }
851 //port reset
852 if (usb_hub_over_current_change(&status)) {
853 usb_process_hub_over_current(hub_info,status);
854 }
855 if (usb_hub_local_power_change(&status)) {
856 usb_process_hub_power_change(hub_info,status);
857 }
858}
859
860//-----------attempts to solve non-removable------------------------
861//-----------attempts to solve non-removable------------------------
862//-----------attempts to solve non-removable------------------------
863//-----------attempts to solve non-removable------------------------
864
865/**
866 * this is an attempt to initialize non-removable devices in the hub
867 *
868 * @param hub_info hub instance
869 * @param port port number, counting from 1
870 * @return error code
871 */
872static int initialize_non_removable(usb_hub_info_t * hub_info,
873 unsigned int port) {
874 int opResult;
875 usb_log_debug("there is not pluged in non-removable device on "
876 "port %d\n", port
877 );
878 //usb_hub_init_add_device(hub_info, port, usb_port_speed(&status));
879 usb_port_status_t status;
880 size_t rcvd_size;
881 usb_device_request_setup_packet_t request;
882 //int opResult;
883 usb_hub_set_port_status_request(&request, port);
884 //endpoint 0
885
886 opResult = usb_pipe_control_read(
887 hub_info->control_pipe,
888 &request, sizeof (usb_device_request_setup_packet_t),
889 &status, 4, &rcvd_size
890 );
891 if (opResult != EOK) {
892 usb_log_error("could not get port status %d\n", opResult);
893 return opResult;
894 }
895 if (rcvd_size != sizeof (usb_port_status_t)) {
896 usb_log_error("received status has incorrect size\n");
897 return opResult;
898 }
899 usb_log_debug("port status %d, x%x\n", status, status);
900 if (usb_port_dev_connected(&status)) {
901 usb_log_debug("there is connected device on this port\n");
902 opResult = usb_hub_init_add_device(hub_info, port,
903 usb_port_speed(&status));
904 }else{
905 usb_log_debug("the non-removable device is not connected\n");
906 opResult = EINVAL;
907 }
908
909 return opResult;
910}
911
912/**
913 * triggers actions to connect non0removable devices
914 *
915 * This will trigger operations leading to activated non-removable device.
916 * Control pipe of the hub must be open fo communication.
917 * @param hub hub representation
918 * @param descriptor usb hub descriptor
919 * @return error code
920 */
921static int usb_hub_trigger_connecting_non_removable_devices(
922 usb_hub_info_t * hub,
923 usb_hub_descriptor_t * descriptor) {
924 usb_log_info("attaching non-removable devices(if any)\n");
925 //usb_device_request_setup_packet_t request;
926 int opResult;
927 //size_t rcvd_size;
928 //usb_port_status_t status;
929 uint8_t * non_removable_dev_bitmap = descriptor->devices_removable;
930 int port;
931#if 0
932 opResult = usb_request_set_configuration(hub->control_pipe,
933 1);
934 if (opResult != EOK) {
935 usb_log_error("could not set default configuration, errno %d",
936 opResult);
937 return opResult;
938 }
939
940 for (port = 1; port <= descriptor->ports_count; ++port) {
941 bool is_non_removable =
942 ((non_removable_dev_bitmap[port / 8]) >> (port % 8)) % 2;
943 if (is_non_removable) {
944 usb_log_debug("non-removable device on port %d\n", port);
945 usb_hub_set_port_status_request(&request, port);
946 opResult = usb_pipe_control_read(
947 hub->control_pipe,
948 &request,
949 sizeof (usb_device_request_setup_packet_t),
950 &status, 4, &rcvd_size
951 );
952 if (opResult != EOK) {
953 usb_log_error("could not get port status of "
954 "port %d errno:%d\n",
955 port, opResult);
956 return opResult;
957 }
958 //try to reset port
959 if (usb_port_dev_connected(&status) || true) {
960 usb_hub_set_enable_port_feature_request(
961 &request, port,
962 USB_HUB_FEATURE_PORT_RESET);
963 opResult = usb_pipe_control_read(
964 hub->control_pipe,
965 &request,
966 sizeof (usb_device_request_setup_packet_t),
967 &status, 4, &rcvd_size
968 );
969 if (opResult != EOK) {
970 usb_log_warning(
971 "could not reset port %d "
972 "errno:%d\n",
973 port, opResult);
974 }
975 usb_log_debug("port reset, should look like "
976 "%d,x%x\n",
977 (1 << USB_HUB_FEATURE_PORT_RESET),
978 (1 << USB_HUB_FEATURE_PORT_RESET)
979 );
980 }
981 //set the status change bit, so it will be noticed
982 //in driver loop
983 if (usb_port_dev_connected(&status) && false) {
984 usb_hub_set_disable_port_feature_request(
985 &request, port,
986 USB_HUB_FEATURE_PORT_CONNECTION);
987 opResult = usb_pipe_control_read(
988 hub->control_pipe,
989 &request,
990 sizeof (usb_device_request_setup_packet_t),
991 &status, 4, &rcvd_size
992 );
993 if (opResult != EOK) {
994 usb_log_warning(
995 "could not clear port "
996 "connection on port %d "
997 "errno:%d\n",
998 port, opResult);
999 }
1000 usb_log_debug("cleared port connection\n");
1001 usb_hub_set_enable_port_feature_request(&request,
1002 port,
1003 USB_HUB_FEATURE_PORT_ENABLE);
1004 opResult = usb_pipe_control_read(
1005 hub->control_pipe,
1006 &request,
1007 sizeof (usb_device_request_setup_packet_t),
1008 &status, 4, &rcvd_size
1009 );
1010 if (opResult != EOK) {
1011 usb_log_warning(
1012 "could not set port enabled "
1013 "on port %d errno:%d\n",
1014 port, opResult);
1015 }
1016 usb_log_debug("port set to enabled - "
1017 "should lead to connection change\n");
1018 }
1019 }
1020 }
1021#endif
1022
1023 /// \TODO this is just a debug code
1024 for (port = 1; port <= descriptor->ports_count; ++port) {
1025 bool is_non_removable =
1026 ((non_removable_dev_bitmap[(port-1) / 8]) >> ((port-1) % 8)) % 2;
1027 if (is_non_removable) {
1028 usb_log_debug("CHECKING port %d is non-removable\n",
1029 port);
1030 usb_port_status_t status;
1031 size_t rcvd_size;
1032 usb_device_request_setup_packet_t request;
1033 //int opResult;
1034 usb_hub_set_port_status_request(&request, port);
1035 //endpoint 0
1036 opResult = usb_pipe_control_read(
1037 hub->control_pipe,
1038 &request,
1039 sizeof (usb_device_request_setup_packet_t),
1040 &status, 4, &rcvd_size
1041 );
1042 if (opResult != EOK) {
1043 usb_log_error("could not get port status %d\n",
1044 opResult);
1045 }
1046 if (rcvd_size != sizeof (usb_port_status_t)) {
1047 usb_log_error("received status has incorrect"
1048 " size\n");
1049 }
1050 //something connected/disconnected
1051 if (usb_port_connect_change(&status)) {
1052 usb_log_debug("some connection changed\n");
1053 }
1054 if(usb_port_dev_connected(&status)){
1055 usb_log_debug("device connected on port\n");
1056 }
1057 usb_log_debug("status: %s\n", usb_debug_str_buffer(
1058 (uint8_t *) & status, 4, 4));
1059 }
1060 }
1061 async_usleep(1000*1000*10);
1062 return EOK;
1063}
1064
1065
1066/**
1067 * @}
1068 */
Note: See TracBrowser for help on using the repository browser.