source: mainline/uspace/drv/usbhub/usbhub.c@ 6c399765

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

attempt to solve non-removable devices
some fixes in root hub driver

  • Property mode set to 100644
File size: 25.2 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 int usb_hub_trigger_connecting_non_removable_devices(
60 usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
61
62/**
63 * control loop running in hub`s fibril
64 *
65 * Hub`s fibril periodically asks for changes on hub and if needded calls
66 * change handling routine.
67 * @warning currently hub driver asks for changes once a second
68 * @param hub_info_param hub representation pointer
69 * @return zero
70 */
71int usb_hub_control_loop(void * hub_info_param){
72 usb_hub_info_t * hub_info = (usb_hub_info_t*)hub_info_param;
73 int errorCode = EOK;
74
75 while(errorCode == EOK){
76 async_usleep(1000 * 1000 * 10 );/// \TODO proper number once
77 errorCode = usb_hub_check_hub_changes(hub_info);
78
79 }
80 usb_log_error("something in ctrl loop went wrong, errno %d\n",errorCode);
81
82 return 0;
83}
84
85
86//*********************************************
87//
88// hub driver code, initialization
89//
90//*********************************************
91
92/**
93 * create usb_hub_info_t structure
94 *
95 * Does only basic copying of known information into new structure.
96 * @param usb_dev usb device structure
97 * @return basic usb_hub_info_t structure
98 */
99static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
100 usb_hub_info_t * result = usb_new(usb_hub_info_t);
101 if(!result) return NULL;
102 result->usb_device = usb_dev;
103 result->status_change_pipe = usb_dev->pipes[0].pipe;
104 result->control_pipe = &usb_dev->ctrl_pipe;
105 result->is_default_address_used = false;
106 return result;
107}
108
109/**
110 * Load hub-specific information into hub_info structure and process if needed
111 *
112 * Particularly read port count and initialize structure holding port
113 * information. If there are non-removable devices, start initializing them.
114 * This function is hub-specific and should be run only after the hub is
115 * configured using usb_hub_set_configuration function.
116 * @param hub_info hub representation
117 * @return error code
118 */
119static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info){
120 // get hub descriptor
121 usb_log_debug("creating serialized descriptor\n");
122 void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
123 usb_hub_descriptor_t * descriptor;
124
125 /* this was one fix of some bug, should not be needed anymore
126 * these lines allow to reset hub once more, it can be used as
127 * brute-force initialization for non-removable devices
128 */
129 int opResult = usb_request_set_configuration(hub_info->control_pipe,
130 1);
131 if(opResult!=EOK){
132 usb_log_error("could not set default configuration, errno %d",opResult);
133 return opResult;
134 }
135
136
137 size_t received_size;
138 opResult = usb_request_get_descriptor(&hub_info->usb_device->ctrl_pipe,
139 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
140 USB_DESCTYPE_HUB,
141 0, 0, serialized_descriptor,
142 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
143
144 if (opResult != EOK) {
145 usb_log_error("failed when receiving hub descriptor, badcode = %d\n",
146 opResult);
147 free(serialized_descriptor);
148 return opResult;
149 }
150 usb_log_debug2("deserializing descriptor\n");
151 descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
152 if(descriptor==NULL){
153 usb_log_warning("could not deserialize descriptor \n");
154 return opResult;
155 }
156 usb_log_debug("setting port count to %d\n",descriptor->ports_count);
157 hub_info->port_count = descriptor->ports_count;
158 hub_info->attached_devs = (usb_hc_attached_device_t*)
159 malloc((hub_info->port_count+1) * sizeof(usb_hc_attached_device_t));
160 int i;
161 for(i=0;i<hub_info->port_count+1;++i){
162 hub_info->attached_devs[i].handle=0;
163 hub_info->attached_devs[i].address=0;
164 }
165 //handle non-removable devices
166 usb_hub_trigger_connecting_non_removable_devices(hub_info, descriptor);
167 usb_log_debug2("freeing data\n");
168 free(serialized_descriptor);
169 hub_info->descriptor = descriptor;
170 hub_info->not_initialized_non_removables =
171 (uint8_t*)malloc((hub_info->port_count+8)/8);
172 memcpy(hub_info->not_initialized_non_removables,
173 descriptor->devices_removable,
174 (hub_info->port_count+8)/8
175 );
176
177 //free(descriptor->devices_removable);
178 //free(descriptor);
179 return EOK;
180}
181/**
182 * Set configuration of hub
183 *
184 * Check whether there is at least one configuration and sets the first one.
185 * This function should be run prior to running any hub-specific action.
186 * @param hub_info hub representation
187 * @return error code
188 */
189static int usb_hub_set_configuration(usb_hub_info_t * hub_info){
190 //device descriptor
191 usb_standard_device_descriptor_t *std_descriptor
192 = &hub_info->usb_device->descriptors.device;
193 usb_log_debug("hub has %d configurations\n",
194 std_descriptor->configuration_count);
195 if(std_descriptor->configuration_count<1){
196 usb_log_error("there are no configurations available\n");
197 return EINVAL;
198 }
199
200 usb_standard_configuration_descriptor_t *config_descriptor
201 = (usb_standard_configuration_descriptor_t *)
202 hub_info->usb_device->descriptors.configuration;
203
204 /* Set configuration. */
205 int opResult = usb_request_set_configuration(
206 &hub_info->usb_device->ctrl_pipe,
207 config_descriptor->configuration_number);
208
209 if (opResult != EOK) {
210 usb_log_error("Failed to set hub configuration: %s.\n",
211 str_error(opResult));
212 return opResult;
213 }
214 usb_log_debug("\tused configuration %d\n",
215 config_descriptor->configuration_number);
216
217 return EOK;
218}
219
220/**
221 * Initialize hub device driver fibril
222 *
223 * Creates hub representation and fibril that periodically checks hub`s status.
224 * Hub representation is passed to the fibril.
225 * @param usb_dev generic usb device information
226 * @return error code
227 */
228int usb_hub_add_device(usb_device_t * usb_dev){
229 if(!usb_dev) return EINVAL;
230 usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
231 //create hc connection
232 usb_log_debug("Initializing USB wire abstraction.\n");
233 int opResult = usb_hc_connection_initialize_from_device(
234 &hub_info->connection,
235 hub_info->usb_device->ddf_dev);
236 if(opResult != EOK){
237 usb_log_error("could not initialize connection to device, errno %d\n",
238 opResult);
239 free(hub_info);
240 return opResult;
241 }
242
243 usb_pipe_start_session(hub_info->control_pipe);
244 //set hub configuration
245 opResult = usb_hub_set_configuration(hub_info);
246 if(opResult!=EOK){
247 usb_log_error("could not set hub configuration, errno %d\n",opResult);
248 free(hub_info);
249 return opResult;
250 }
251 //get port count and create attached_devs
252 opResult = usb_hub_process_hub_specific_info(hub_info);
253 if(opResult!=EOK){
254 usb_log_error("could not set hub configuration, errno %d\n",opResult);
255 free(hub_info);
256 return opResult;
257 }
258 usb_pipe_end_session(hub_info->control_pipe);
259
260
261 /// \TODO what is this?
262 usb_log_debug("Creating `hub' function.\n");
263 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
264 fun_exposed, "hub");
265 assert(hub_fun != NULL);
266 hub_fun->ops = NULL;
267
268 int rc = ddf_fun_bind(hub_fun);
269 assert(rc == EOK);
270 rc = ddf_fun_add_to_class(hub_fun, "hub");
271 assert(rc == EOK);
272
273 //create fibril for the hub control loop
274 fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
275 if (fid == 0) {
276 usb_log_error("failed to start monitoring fibril for new hub.\n");
277 return ENOMEM;
278 }
279 fibril_add_ready(fid);
280 usb_log_debug("Hub fibril created.\n");
281
282 usb_log_info("Controlling hub `%s' (%d ports).\n",
283 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
284 return EOK;
285}
286
287
288//*********************************************
289//
290// hub driver code, main loop and port handling
291//
292//*********************************************
293
294/**
295 * triggers actions to connect non0removable devices
296 *
297 * This will trigger operations leading to activated non-removable device.
298 * Control pipe of the hub must be open fo communication.
299 * @param hub hub representation
300 * @param descriptor usb hub descriptor
301 * @return error code
302 */
303static int usb_hub_trigger_connecting_non_removable_devices(
304 usb_hub_info_t * hub,
305 usb_hub_descriptor_t * descriptor)
306{
307 usb_log_info("attaching non-removable devices(if any)\n");
308 //usb_device_request_setup_packet_t request;
309 int opResult;
310 //size_t rcvd_size;
311 //usb_port_status_t status;
312 uint8_t * non_removable_dev_bitmap = descriptor->devices_removable;
313 int port;
314
315 opResult = usb_request_set_configuration(hub->control_pipe,
316 1);
317 if(opResult!=EOK){
318 usb_log_error("could not set default configuration, errno %d",opResult);
319 return opResult;
320 }
321#if 0
322 for(port=1;port<=descriptor->ports_count;++port){
323 bool is_non_removable =
324 ((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
325 if(is_non_removable){
326 usb_log_debug("non-removable device on port %d\n",port);
327 usb_hub_set_port_status_request(&request, port);
328 opResult = usb_pipe_control_read(
329 hub->control_pipe,
330 &request, sizeof(usb_device_request_setup_packet_t),
331 &status, 4, &rcvd_size
332 );
333 if (opResult != EOK) {
334 usb_log_error("could not get port status of port %d errno:%d\n",
335 port, opResult);
336 return opResult;
337 }
338 //try to reset port
339 if(usb_port_dev_connected(&status) || true){
340 usb_hub_set_enable_port_feature_request(&request, port,
341 USB_HUB_FEATURE_PORT_RESET);
342 opResult = usb_pipe_control_read(
343 hub->control_pipe,
344 &request, sizeof(usb_device_request_setup_packet_t),
345 &status, 4, &rcvd_size
346 );
347 if (opResult != EOK) {
348 usb_log_warning(
349 "could not reset port %d errno:%d\n",
350 port, opResult);
351 }
352 usb_log_debug("port reset, should look like %d,x%x\n",
353 (1<<USB_HUB_FEATURE_PORT_RESET),
354 (1<<USB_HUB_FEATURE_PORT_RESET)
355 );
356 }
357 //set the status change bit, so it will be noticed in driver loop
358 if(usb_port_dev_connected(&status) && false){
359 usb_hub_set_disable_port_feature_request(&request, port,
360 USB_HUB_FEATURE_PORT_CONNECTION);
361 opResult = usb_pipe_control_read(
362 hub->control_pipe,
363 &request, sizeof(usb_device_request_setup_packet_t),
364 &status, 4, &rcvd_size
365 );
366 if (opResult != EOK) {
367 usb_log_warning(
368 "could not clear port connection on port %d errno:%d\n",
369 port, opResult);
370 }
371 usb_log_debug("cleared port connection\n");
372 usb_hub_set_enable_port_feature_request(&request, port,
373 USB_HUB_FEATURE_PORT_ENABLE);
374 opResult = usb_pipe_control_read(
375 hub->control_pipe,
376 &request, sizeof(usb_device_request_setup_packet_t),
377 &status, 4, &rcvd_size
378 );
379 if (opResult != EOK) {
380 usb_log_warning(
381 "could not set port enabled on port %d errno:%d\n",
382 port, opResult);
383 }
384 usb_log_debug("port set to enabled - should lead to connection change\n");
385 }
386 }
387 }
388#endif
389
390 /// \TODO this is just a debug code
391 for(port=1;port<=descriptor->ports_count;++port){
392 bool is_non_removable =
393 ((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
394 if(is_non_removable){
395 usb_log_debug("CHECKING port %d is non-removable\n",port);
396 usb_port_status_t status;
397 size_t rcvd_size;
398 usb_device_request_setup_packet_t request;
399 //int opResult;
400 usb_hub_set_port_status_request(&request, port);
401 //endpoint 0
402 opResult = usb_pipe_control_read(
403 hub->control_pipe,
404 &request, sizeof(usb_device_request_setup_packet_t),
405 &status, 4, &rcvd_size
406 );
407 if (opResult != EOK) {
408 usb_log_error("could not get port status %d\n",opResult);
409 }
410 if (rcvd_size != sizeof (usb_port_status_t)) {
411 usb_log_error("received status has incorrect size\n");
412 }
413 //something connected/disconnected
414 if (usb_port_connect_change(&status)) {
415 usb_log_debug("some connection changed\n");
416 }
417 usb_log_debug("status: %s\n",usb_debug_str_buffer(
418 (uint8_t *)&status,4,4));
419 }
420 }
421
422 return EOK;
423}
424
425
426/**
427 * release default address used by given hub
428 *
429 * Also unsets hub->is_default_address_used. Convenience wrapper function.
430 * @note hub->connection MUST be open for communication
431 * @param hub hub representation
432 * @return error code
433 */
434static int usb_hub_release_default_address(usb_hub_info_t * hub){
435 int opResult = usb_hc_release_default_address(&hub->connection);
436 if(opResult!=EOK){
437 usb_log_error("could not release default address, errno %d\n",opResult);
438 return opResult;
439 }
440 hub->is_default_address_used = false;
441 return EOK;
442}
443
444/**
445 * Reset the port with new device and reserve the default address.
446 * @param hub hub representation
447 * @param port port number, starting from 1
448 * @param speed transfer speed of attached device, one of low, full or high
449 * @return error code
450 */
451static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
452 usb_speed_t speed) {
453 //if this hub already uses default address, it cannot request it once more
454 if(hub->is_default_address_used) return EREFUSED;
455 usb_log_debug("some connection changed\n");
456 assert(hub->control_pipe->hc_phone);
457 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
458 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
459 if(opResult != EOK){
460 usb_log_warning("could not clear port-change-connection flag\n");
461 }
462 usb_device_request_setup_packet_t request;
463
464 //get default address
465 opResult = usb_hc_reserve_default_address(&hub->connection, speed);
466
467 if (opResult != EOK) {
468 usb_log_warning("cannot assign default address, it is probably used %d\n",
469 opResult);
470 return opResult;
471 }
472 hub->is_default_address_used = true;
473 //reset port
474 usb_hub_set_reset_port_request(&request, port);
475 opResult = usb_pipe_control_write(
476 hub->control_pipe,
477 &request,sizeof(usb_device_request_setup_packet_t),
478 NULL, 0
479 );
480 if (opResult != EOK) {
481 usb_log_error("something went wrong when reseting a port %d\n",opResult);
482 usb_hub_release_default_address(hub);
483 }
484 return opResult;
485}
486
487/**
488 * Finalize adding new device after port reset
489 *
490 * Set device`s address and start it`s driver.
491 * @param hub hub representation
492 * @param port port number, starting from 1
493 * @param speed transfer speed of attached device, one of low, full or high
494 */
495static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
496 uint16_t port, usb_speed_t speed) {
497
498 int opResult;
499 usb_log_debug("finalizing add device\n");
500 opResult = usb_hub_clear_port_feature(hub->control_pipe,
501 port, USB_HUB_FEATURE_C_PORT_RESET);
502
503 if (opResult != EOK) {
504 usb_log_error("failed to clear port reset feature\n");
505 usb_hub_release_default_address(hub);
506 return;
507 }
508 //create connection to device
509 usb_pipe_t new_device_pipe;
510 usb_device_connection_t new_device_connection;
511 usb_device_connection_initialize_on_default_address(
512 &new_device_connection,
513 &hub->connection
514 );
515 usb_pipe_initialize_default_control(
516 &new_device_pipe,
517 &new_device_connection);
518 usb_pipe_probe_default_control(&new_device_pipe);
519
520 /* Request address from host controller. */
521 usb_address_t new_device_address = usb_hc_request_address(
522 &hub->connection,
523 speed
524 );
525 if (new_device_address < 0) {
526 usb_log_error("failed to get free USB address\n");
527 opResult = new_device_address;
528 usb_hub_release_default_address(hub);
529 return;
530 }
531 usb_log_debug("setting new address %d\n",new_device_address);
532 //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
533 // new_device_address);
534 usb_pipe_start_session(&new_device_pipe);
535 opResult = usb_request_set_address(&new_device_pipe,new_device_address);
536 usb_pipe_end_session(&new_device_pipe);
537 if (opResult != EOK) {
538 usb_log_error("could not set address for new device %d\n",opResult);
539 usb_hub_release_default_address(hub);
540 return;
541 }
542
543 //opResult = usb_hub_release_default_address(hc);
544 opResult = usb_hub_release_default_address(hub);
545 if(opResult!=EOK){
546 return;
547 }
548
549 devman_handle_t child_handle;
550 //??
551 opResult = usb_device_register_child_in_devman(new_device_address,
552 hub->connection.hc_handle, hub->usb_device->ddf_dev, &child_handle,
553 NULL, NULL, NULL);
554
555 if (opResult != EOK) {
556 usb_log_error("could not start driver for new device %d\n",opResult);
557 return;
558 }
559 hub->attached_devs[port].handle = child_handle;
560 hub->attached_devs[port].address = new_device_address;
561
562 //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
563 opResult = usb_hc_register_device(
564 &hub->connection,
565 &hub->attached_devs[port]);
566 if (opResult != EOK) {
567 usb_log_error("could not assign address of device in hcd %d\n",opResult);
568 return;
569 }
570 usb_log_info("Detected new device on `%s' (port %d), " \
571 "address %d (handle %llu).\n",
572 hub->usb_device->ddf_dev->name, (int) port,
573 new_device_address, child_handle);
574}
575
576/**
577 * routine called when a device on port has been removed
578 *
579 * If the device on port had default address, it releases default address.
580 * Otherwise does not do anything, because DDF does not allow to remove device
581 * from it`s device tree.
582 * @param hub hub representation
583 * @param port port number, starting from 1
584 */
585static void usb_hub_removed_device(
586 usb_hub_info_t * hub,uint16_t port) {
587
588 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
589 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
590 if(opResult != EOK){
591 usb_log_warning("could not clear port-change-connection flag\n");
592 }
593 /** \TODO remove device from device manager - not yet implemented in
594 * devide manager
595 */
596
597 //close address
598 if(hub->attached_devs[port].address!=0){
599 /*uncomment this code to use it when DDF allows device removal
600 opResult = usb_hc_unregister_device(
601 &hub->connection, hub->attached_devs[port].address);
602 if(opResult != EOK) {
603 dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
604 "removed device: %d", opResult);
605 }
606 hub->attached_devs[port].address = 0;
607 hub->attached_devs[port].handle = 0;
608 */
609 }else{
610 usb_log_warning("this is strange, disconnected device had no address\n");
611 //device was disconnected before it`s port was reset - return default address
612 usb_hub_release_default_address(hub);
613 }
614}
615
616
617/**
618 * Process over current condition on port.
619 *
620 * Turn off the power on the port.
621 *
622 * @param hub hub representation
623 * @param port port number, starting from 1
624 */
625static void usb_hub_over_current( usb_hub_info_t * hub,
626 uint16_t port){
627 int opResult;
628 opResult = usb_hub_clear_port_feature(hub->control_pipe,
629 port, USB_HUB_FEATURE_PORT_POWER);
630 if(opResult!=EOK){
631 usb_log_error("cannot power off port %d; %d\n",
632 port, opResult);
633 }
634}
635
636/**
637 * Process interrupts on given hub port
638 *
639 * Accepts connection, over current and port reset change.
640 * @param hub hub representation
641 * @param port port number, starting from 1
642 */
643static void usb_hub_process_interrupt(usb_hub_info_t * hub,
644 uint16_t port) {
645 usb_log_debug("interrupt at port %d\n", port);
646 //determine type of change
647 usb_pipe_t *pipe = hub->control_pipe;
648
649 int opResult;
650
651 usb_port_status_t status;
652 size_t rcvd_size;
653 usb_device_request_setup_packet_t request;
654 //int opResult;
655 usb_hub_set_port_status_request(&request, port);
656 //endpoint 0
657
658 opResult = usb_pipe_control_read(
659 pipe,
660 &request, sizeof(usb_device_request_setup_packet_t),
661 &status, 4, &rcvd_size
662 );
663 if (opResult != EOK) {
664 usb_log_error("could not get port status\n");
665 return;
666 }
667 if (rcvd_size != sizeof (usb_port_status_t)) {
668 usb_log_error("received status has incorrect size\n");
669 return;
670 }
671 //something connected/disconnected
672 if (usb_port_connect_change(&status)) {
673 usb_log_debug("connection change on port\n");
674 if (usb_port_dev_connected(&status)) {
675 usb_log_debug("some connection changed\n");
676 usb_hub_init_add_device(hub, port, usb_port_speed(&status));
677 } else {
678 usb_hub_removed_device(hub, port);
679 }
680 }
681 //over current
682 if (usb_port_overcurrent_change(&status)) {
683 //check if it was not auto-resolved
684 usb_log_debug("overcurrent change on port\n");
685 if(usb_port_over_current(&status)){
686 usb_hub_over_current(hub,port);
687 }else{
688 usb_log_debug("over current condition was auto-resolved on port %d\n",
689 port);
690 }
691 }
692 //port reset
693 if (usb_port_reset_completed(&status)) {
694 usb_log_debug("port reset complete\n");
695 if (usb_port_enabled(&status)) {
696 usb_hub_finalize_add_device(hub, port, usb_port_speed(&status));
697 } else {
698 usb_log_warning("port reset, but port still not enabled\n");
699 }
700 }
701 usb_log_debug("status x%x : %d\n ",status,status);
702
703 usb_port_set_connect_change(&status, false);
704 usb_port_set_reset(&status, false);
705 usb_port_set_reset_completed(&status, false);
706 usb_port_set_dev_connected(&status, false);
707 if (status>>16) {
708 usb_log_info("there was some unsupported change on port %d: %X\n",
709 port,status);
710
711 }
712}
713
714
715static int initialize_non_removable(usb_hub_info_t * hub_info,
716 unsigned int port){
717 int opResult;
718 usb_log_debug("there is not pluged in non-removable device on "
719 "port %d\n",port
720 );
721 //usb_hub_init_add_device(hub_info, port, usb_port_speed(&status));
722 usb_port_status_t status;
723 size_t rcvd_size;
724 usb_device_request_setup_packet_t request;
725 //int opResult;
726 usb_hub_set_port_status_request(&request, port);
727 //endpoint 0
728
729 opResult = usb_pipe_control_read(
730 hub_info->control_pipe,
731 &request, sizeof(usb_device_request_setup_packet_t),
732 &status, 4, &rcvd_size
733 );
734 if (opResult != EOK) {
735 usb_log_error("could not get port status %d\n",opResult);
736 return opResult;
737 }
738 if (rcvd_size != sizeof (usb_port_status_t)) {
739 usb_log_error("received status has incorrect size\n");
740 return opResult;
741 }
742 usb_log_debug("port status %d, x%x\n",status,status);
743 if(usb_port_dev_connected(&status)){
744 usb_log_debug("there is connected device on this port\n");
745 }
746 if(!hub_info->is_default_address_used)
747 usb_hub_init_add_device(hub_info, port, usb_port_speed(&status));
748 return opResult;
749}
750
751/**
752 * check changes on hub
753 *
754 * Handles changes on each port with a status change.
755 * @param hub_info hub representation
756 * @return error code
757 */
758int usb_hub_check_hub_changes(usb_hub_info_t * hub_info){
759 int opResult;
760 opResult = usb_pipe_start_session(
761 hub_info->status_change_pipe);
762 //this might not be necessary - if all non-removables are ok, it is not needed here
763 opResult = usb_pipe_start_session(hub_info->control_pipe);
764 if(opResult != EOK){
765 usb_log_error("could not initialize communication for hub; %d\n",
766 opResult);
767 return opResult;
768 }
769
770 size_t port_count = hub_info->port_count;
771 //first check non-removable devices
772 {
773 unsigned int port;
774 for(port = 1; port<=port_count; ++port){
775 bool is_non_removable =
776 hub_info->not_initialized_non_removables[port/8]
777 & (1 << (port%8));
778 if(is_non_removable){
779 opResult = initialize_non_removable(hub_info,port);
780 }
781 }
782 }
783
784
785 /// FIXME: count properly
786 size_t byte_length = ((port_count+1) / 8) + 1;
787 void *change_bitmap = malloc(byte_length);
788 size_t actual_size;
789
790 /*
791 * Send the request.
792 */
793 opResult = usb_pipe_read(
794 hub_info->status_change_pipe,
795 change_bitmap, byte_length, &actual_size
796 );
797
798 if (opResult != EOK) {
799 free(change_bitmap);
800 usb_log_warning("something went wrong while getting status of hub\n");
801 usb_pipe_end_session(hub_info->status_change_pipe);
802 return opResult;
803 }
804 unsigned int port;
805
806 if(opResult!=EOK){
807 usb_log_error("could not start control pipe session %d\n", opResult);
808 usb_pipe_end_session(hub_info->status_change_pipe);
809 return opResult;
810 }
811 opResult = usb_hc_connection_open(&hub_info->connection);
812 if(opResult!=EOK){
813 usb_log_error("could not start host controller session %d\n",
814 opResult);
815 usb_pipe_end_session(hub_info->control_pipe);
816 usb_pipe_end_session(hub_info->status_change_pipe);
817 return opResult;
818 }
819
820 ///todo, opresult check, pre obe konekce
821 for (port = 1; port < port_count+1; ++port) {
822 bool interrupt =
823 (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
824 if (interrupt) {
825 usb_hub_process_interrupt(
826 hub_info, port);
827 }
828 }
829 usb_hc_connection_close(&hub_info->connection);
830 usb_pipe_end_session(hub_info->control_pipe);
831 usb_pipe_end_session(hub_info->status_change_pipe);
832 free(change_bitmap);
833 return EOK;
834}
835
836
837
838/**
839 * @}
840 */
Note: See TracBrowser for help on using the repository browser.