source: mainline/uspace/drv/usbhub/usbhub.c@ 3dba1ca

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

some more comments

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