source: mainline/uspace/drv/usbhub/usbhub.c@ 42a3a57

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

small fixes: new device speed, returning unpluged address, proposed sollution for disconnected hub

  • Property mode set to 100644
File size: 20.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
55static ddf_dev_ops_t hub_device_ops = {
56 .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
57};
58
59/** Hub status-change endpoint description
60 *
61 * For more see usb hub specification in 11.15.1 of
62 */
63static usb_endpoint_description_t status_change_endpoint_description = {
64 .transfer_type = USB_TRANSFER_INTERRUPT,
65 .direction = USB_DIRECTION_IN,
66 .interface_class = USB_CLASS_HUB,
67 .interface_subclass = 0,
68 .interface_protocol = 0,
69 .flags = 0
70};
71
72int usb_hub_control_loop(void * hub_info_param){
73 usb_hub_info_t * hub_info = (usb_hub_info_t*)hub_info_param;
74 int errorCode = EOK;
75
76 while(errorCode == EOK){
77 errorCode = usb_hub_check_hub_changes(hub_info);
78 async_usleep(1000 * 1000 );/// \TODO proper number once
79 }
80 dprintf(USB_LOG_LEVEL_ERROR,
81 "something in ctrl loop went wrong, errno %d",errorCode);
82 return 0;
83}
84
85
86//*********************************************
87//
88// hub driver code, initialization
89//
90//*********************************************
91
92/**
93 * Initialize connnections to host controller, device, and device
94 * control endpoint
95 * @param hub
96 * @param device
97 * @return
98 */
99static int usb_hub_init_communication(usb_hub_info_t * hub){
100 usb_log_debug("Initializing hub USB communication (hub->device->handle=%zu).\n", hub->device->handle);
101 int opResult;
102 opResult = usb_device_connection_initialize_from_device(
103 &hub->device_connection,
104 hub->device);
105 if(opResult != EOK){
106 dprintf(USB_LOG_LEVEL_ERROR,
107 "could not initialize connection to hc, errno %d",opResult);
108 return opResult;
109 }
110 usb_log_debug("Initializing USB wire abstraction.\n");
111 opResult = usb_hc_connection_initialize_from_device(&hub->connection,
112 hub->device);
113 if(opResult != EOK){
114 dprintf(USB_LOG_LEVEL_ERROR,
115 "could not initialize connection to device, errno %d",opResult);
116 return opResult;
117 }
118 usb_log_debug("Initializing default control pipe.\n");
119 opResult = usb_endpoint_pipe_initialize_default_control(&hub->endpoints.control,
120 &hub->device_connection);
121 if(opResult != EOK){
122 dprintf(USB_LOG_LEVEL_ERROR,
123 "could not initialize connection to device endpoint, errno %d",opResult);
124 return opResult;
125 }
126
127 opResult = usb_endpoint_pipe_probe_default_control(&hub->endpoints.control);
128 if (opResult != EOK) {
129 dprintf(USB_LOG_LEVEL_ERROR, "failed probing endpoint 0, %d", opResult);
130 return opResult;
131 }
132
133 return EOK;
134}
135
136/**
137 * When entering this function, hub->endpoints.control should be active.
138 * @param hub
139 * @return
140 */
141static int usb_hub_process_configuration_descriptors(
142 usb_hub_info_t * hub){
143 if(hub==NULL) {
144 return EINVAL;
145 }
146 int opResult;
147
148 //device descriptor
149 usb_standard_device_descriptor_t std_descriptor;
150 opResult = usb_request_get_device_descriptor(&hub->endpoints.control,
151 &std_descriptor);
152 if(opResult!=EOK){
153 dprintf(USB_LOG_LEVEL_ERROR, "could not get device descriptor, %d",opResult);
154 return opResult;
155 }
156 dprintf(USB_LOG_LEVEL_INFO, "hub has %d configurations",
157 std_descriptor.configuration_count);
158 if(std_descriptor.configuration_count<1){
159 dprintf(USB_LOG_LEVEL_ERROR, "THERE ARE NO CONFIGURATIONS AVAILABLE");
160 //shouldn`t I return?
161 }
162
163 /* Retrieve full configuration descriptor. */
164 uint8_t *descriptors = NULL;
165 size_t descriptors_size = 0;
166 opResult = usb_request_get_full_configuration_descriptor_alloc(
167 &hub->endpoints.control, 0,
168 (void **) &descriptors, &descriptors_size);
169 if (opResult != EOK) {
170 usb_log_error("Could not get configuration descriptor: %s.\n",
171 str_error(opResult));
172 return opResult;
173 }
174 usb_standard_configuration_descriptor_t *config_descriptor
175 = (usb_standard_configuration_descriptor_t *) descriptors;
176
177 /* Set configuration. */
178 opResult = usb_request_set_configuration(&hub->endpoints.control,
179 config_descriptor->configuration_number);
180
181 if (opResult != EOK) {
182 usb_log_error("Failed to set hub configuration: %s.\n",
183 str_error(opResult));
184 return opResult;
185 }
186 dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",
187 config_descriptor->configuration_number);
188
189 usb_endpoint_mapping_t endpoint_mapping[1] = {
190 {
191 .pipe = &hub->endpoints.status_change,
192 .description = &status_change_endpoint_description,
193 .interface_no =
194 usb_device_get_assigned_interface(hub->device)
195 }
196 };
197 opResult = usb_endpoint_pipe_initialize_from_configuration(
198 endpoint_mapping, 1,
199 descriptors, descriptors_size,
200 &hub->device_connection);
201 if (opResult != EOK) {
202 dprintf(USB_LOG_LEVEL_ERROR,
203 "Failed to initialize status change pipe: %s",
204 str_error(opResult));
205 return opResult;
206 }
207 if (!endpoint_mapping[0].present) {
208 dprintf(USB_LOG_LEVEL_ERROR,"Not accepting device, " \
209 "cannot understand what is happenning");
210 return EREFUSED;
211 }
212
213 free(descriptors);
214 return EOK;
215
216}
217
218
219/**
220 * Create hub representation from device information.
221 * @param device
222 * @return pointer to created structure or NULL in case of error
223 */
224usb_hub_info_t * usb_create_hub_info(ddf_dev_t * device) {
225 usb_hub_info_t* result = usb_new(usb_hub_info_t);
226 result->device = device;
227 int opResult;
228 opResult = usb_hub_init_communication(result);
229 if(opResult != EOK){
230 free(result);
231 return NULL;
232 }
233
234 //result->device = device;
235 result->port_count = -1;
236 result->device = device;
237
238 //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
239 size_t received_size;
240
241 // get hub descriptor
242 dprintf(USB_LOG_LEVEL_DEBUG, "creating serialized descripton");
243 void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
244 usb_hub_descriptor_t * descriptor;
245 dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
246 usb_endpoint_pipe_start_session(&result->endpoints.control);
247 opResult = usb_request_set_configuration(&result->endpoints.control, 1);
248 assert(opResult == EOK);
249
250 opResult = usb_request_get_descriptor(&result->endpoints.control,
251 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
252 USB_DESCTYPE_HUB,
253 0, 0, serialized_descriptor,
254 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
255 usb_endpoint_pipe_end_session(&result->endpoints.control);
256
257 if (opResult != EOK) {
258 dprintf(USB_LOG_LEVEL_ERROR, "failed when receiving hub descriptor, badcode = %d",opResult);
259 free(serialized_descriptor);
260 free(result);
261 return NULL;
262 }
263 dprintf(USB_LOG_LEVEL_DEBUG2, "deserializing descriptor");
264 descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
265 if(descriptor==NULL){
266 dprintf(USB_LOG_LEVEL_WARNING, "could not deserialize descriptor ");
267 free(result);
268 return NULL;
269 }
270
271 dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
272 result->port_count = descriptor->ports_count;
273 result->attached_devs = (usb_hc_attached_device_t*)
274 malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
275 int i;
276 for(i=0;i<result->port_count+1;++i){
277 result->attached_devs[i].handle=0;
278 result->attached_devs[i].address=0;
279 }
280 dprintf(USB_LOG_LEVEL_DEBUG2, "freeing data");
281 free(serialized_descriptor);
282 free(descriptor->devices_removable);
283 free(descriptor);
284
285 //finish
286
287 dprintf(USB_LOG_LEVEL_INFO, "hub info created");
288
289 return result;
290}
291
292/**
293 * Create hub representation and add it into hub list
294 * @param dev
295 * @return
296 */
297int usb_add_hub_device(ddf_dev_t *dev) {
298 dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
299
300 //dev->ops = &hub_device_ops;
301 (void) hub_device_ops;
302
303 usb_hub_info_t * hub_info = usb_create_hub_info(dev);
304 if(!hub_info){
305 return EINTR;
306 }
307
308 int opResult;
309
310 //perform final configurations
311 usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
312 // process descriptors
313 opResult = usb_hub_process_configuration_descriptors(hub_info);
314 if(opResult != EOK){
315 dprintf(USB_LOG_LEVEL_ERROR,"could not get configuration descriptors, %d",
316 opResult);
317 return opResult;
318 }
319 //power ports
320 usb_device_request_setup_packet_t request;
321 int port;
322 for (port = 1; port < hub_info->port_count+1; ++port) {
323 usb_hub_set_power_port_request(&request, port);
324 opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
325 &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
326 dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
327 if (opResult != EOK) {
328 dprintf(USB_LOG_LEVEL_WARNING, "something went wrong when setting hub`s %dth port", port);
329 }
330 }
331 //ports powered, hub seems to be enabled
332 usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
333
334 //add the hub to list
335 //is this needed now?
336 fibril_mutex_lock(&usb_hub_list_lock);
337 usb_lst_append(&usb_hub_list, hub_info);
338 fibril_mutex_unlock(&usb_hub_list_lock);
339 dprintf(USB_LOG_LEVEL_DEBUG, "hub info added to list");
340
341 dprintf(USB_LOG_LEVEL_DEBUG, "adding to ddf");
342 ddf_fun_t *hub_fun = ddf_fun_create(dev, fun_exposed, "hub");
343 assert(hub_fun != NULL);
344 hub_fun->ops = NULL;
345
346 int rc = ddf_fun_bind(hub_fun);
347 assert(rc == EOK);
348 rc = ddf_fun_add_to_class(hub_fun, "hub");
349 assert(rc == EOK);
350
351 fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
352 if (fid == 0) {
353 dprintf(USB_LOG_LEVEL_ERROR,
354 ": failed to start monitoring fibril for new hub");
355 return ENOMEM;
356 }
357 fibril_add_ready(fid);
358
359 dprintf(USB_LOG_LEVEL_DEBUG, "hub fibril created");
360 //(void)hub_info;
361 //usb_hub_check_hub_changes();
362
363 dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
364 //address is lost...
365 dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
366 //hub_info->endpoints.control.,
367 hub_info->port_count);
368
369 return EOK;
370 //return ENOTSUP;
371}
372
373
374//*********************************************
375//
376// hub driver code, main loop
377//
378//*********************************************
379
380/**
381 * Reset the port with new device and reserve the default address.
382 * @param hc
383 * @param port
384 * @param target
385 */
386static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
387 bool isLowSpeed) {
388 usb_device_request_setup_packet_t request;
389 int opResult;
390 dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
391 assert(hub->endpoints.control.hc_phone);
392 //get default address
393 usb_speed_t speed = isLowSpeed?USB_SPEED_LOW:USB_SPEED_FULL;
394 opResult = usb_hc_reserve_default_address(&hub->connection, speed);
395
396 if (opResult != EOK) {
397 dprintf(USB_LOG_LEVEL_WARNING,
398 "cannot assign default address, it is probably used %d",opResult);
399 return;
400 }
401 //reset port
402 usb_hub_set_reset_port_request(&request, port);
403 opResult = usb_endpoint_pipe_control_write(
404 &hub->endpoints.control,
405 &request,sizeof(usb_device_request_setup_packet_t),
406 NULL, 0
407 );
408 if (opResult != EOK) {
409 dprintf(USB_LOG_LEVEL_ERROR,
410 "something went wrong when reseting a port %d",opResult);
411 //usb_hub_release_default_address(hc);
412 usb_hc_release_default_address(&hub->connection);
413 }
414}
415
416/**
417 * Finalize adding new device after port reset
418 * @param hc
419 * @param port
420 * @param target
421 */
422static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
423 uint16_t port, bool isLowSpeed) {
424
425 int opResult;
426 dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
427 opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
428 port, USB_HUB_FEATURE_C_PORT_RESET);
429
430 if (opResult != EOK) {
431 dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
432 usb_hc_release_default_address(&hub->connection);
433 return;
434 }
435 //create connection to device
436 usb_endpoint_pipe_t new_device_pipe;
437 usb_device_connection_t new_device_connection;
438 usb_device_connection_initialize_on_default_address(
439 &new_device_connection,
440 &hub->connection
441 );
442 usb_endpoint_pipe_initialize_default_control(
443 &new_device_pipe,
444 &new_device_connection);
445 usb_endpoint_pipe_probe_default_control(&new_device_pipe);
446 /// \TODO get highspeed info
447 usb_speed_t speed = isLowSpeed?USB_SPEED_LOW:USB_SPEED_FULL;
448
449
450 /* Request address from host controller. */
451 usb_address_t new_device_address = usb_hc_request_address(
452 &hub->connection,
453 speed
454 );
455 if (new_device_address < 0) {
456 dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
457 opResult = new_device_address;
458 usb_hc_release_default_address(&hub->connection);
459 return;
460 }
461 dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
462 //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
463 // new_device_address);
464 usb_endpoint_pipe_start_session(&new_device_pipe);
465 opResult = usb_request_set_address(&new_device_pipe,new_device_address);
466 usb_endpoint_pipe_end_session(&new_device_pipe);
467 if (opResult != EOK) {
468 dprintf(USB_LOG_LEVEL_ERROR,
469 "could not set address for new device %d",opResult);
470 usb_hc_release_default_address(&hub->connection);
471 return;
472 }
473
474
475 //opResult = usb_hub_release_default_address(hc);
476 opResult = usb_hc_release_default_address(&hub->connection);
477 if(opResult!=EOK){
478 return;
479 }
480
481 devman_handle_t child_handle;
482 //??
483 opResult = usb_device_register_child_in_devman(new_device_address,
484 hub->connection.hc_handle, hub->device, &child_handle,
485 NULL, NULL, NULL);
486
487 if (opResult != EOK) {
488 dprintf(USB_LOG_LEVEL_ERROR,
489 "could not start driver for new device %d",opResult);
490 return;
491 }
492 hub->attached_devs[port].handle = child_handle;
493 hub->attached_devs[port].address = new_device_address;
494
495 //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
496 opResult = usb_hc_register_device(
497 &hub->connection,
498 &hub->attached_devs[port]);
499 if (opResult != EOK) {
500 dprintf(USB_LOG_LEVEL_ERROR,
501 "could not assign address of device in hcd %d",opResult);
502 return;
503 }
504 dprintf(USB_LOG_LEVEL_INFO, "new device address %d, handle %zu",
505 new_device_address, child_handle);
506
507}
508
509/**
510 * Unregister device address in hc
511 * @param hc
512 * @param port
513 * @param target
514 */
515static void usb_hub_removed_device(
516 usb_hub_info_t * hub,uint16_t port) {
517
518 /** \TODO remove device from device manager - not yet implemented in
519 * devide manager
520 */
521
522 //close address
523 if(hub->attached_devs[port].address!=0){
524 /*uncomment this code to use it when DDF allows device removal
525 opResult = usb_hc_unregister_device(
526 &hub->connection, hub->attached_devs[port].address);
527 if(opResult != EOK) {
528 dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
529 "removed device: %d", opResult);
530 }
531 hub->attached_devs[port].address = 0;
532 hub->attached_devs[port].handle = 0;
533 */
534 }else{
535 dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
536 //device was disconnected before it`s port was reset - return default address
537 //usb_drv_release_default_address(hc);
538 usb_hc_release_default_address(&hub->connection);
539 }
540}
541
542
543/**
544 *Process over current condition on port.
545 *
546 * Turn off the power on the port.
547 *
548 * @param hub
549 * @param port
550 */
551static void usb_hub_over_current( usb_hub_info_t * hub,
552 uint16_t port){
553 int opResult;
554 opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
555 port, USB_HUB_FEATURE_PORT_POWER);
556 if(opResult!=EOK){
557 dprintf(USB_LOG_LEVEL_ERROR, "cannot power off port %d; %d",
558 port, opResult);
559 }
560}
561
562/**
563 * Process interrupts on given hub port
564 * @param hc
565 * @param port
566 * @param target
567 */
568static void usb_hub_process_interrupt(usb_hub_info_t * hub,
569 uint16_t port) {
570 dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
571 //determine type of change
572 usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
573
574 int opResult;
575
576 usb_port_status_t status;
577 size_t rcvd_size;
578 usb_device_request_setup_packet_t request;
579 //int opResult;
580 usb_hub_set_port_status_request(&request, port);
581 //endpoint 0
582
583 opResult = usb_endpoint_pipe_control_read(
584 pipe,
585 &request, sizeof(usb_device_request_setup_packet_t),
586 &status, 4, &rcvd_size
587 );
588 if (opResult != EOK) {
589 dprintf(USB_LOG_LEVEL_ERROR, "could not get port status");
590 return;
591 }
592 if (rcvd_size != sizeof (usb_port_status_t)) {
593 dprintf(USB_LOG_LEVEL_ERROR, "received status has incorrect size");
594 return;
595 }
596 //something connected/disconnected
597 if (usb_port_connect_change(&status)) {
598 opResult = usb_hub_clear_port_feature(pipe,
599 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
600 // TODO: check opResult
601 if (usb_port_dev_connected(&status)) {
602 dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
603 usb_hub_init_add_device(hub, port, usb_port_low_speed(&status));
604 } else {
605 usb_hub_removed_device(hub, port);
606 }
607 }
608 //over current
609 if (usb_port_overcurrent_change(&status)) {
610 //check if it was not auto-resolved
611 if(usb_port_over_current(&status)){
612 usb_hub_over_current(hub,port);
613 }else{
614 dprintf(USB_LOG_LEVEL_INFO,
615 "over current condition was auto-resolved on port %d",port);
616 }
617 }
618 //port reset
619 if (usb_port_reset_completed(&status)) {
620 dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
621 if (usb_port_enabled(&status)) {
622 usb_hub_finalize_add_device(hub, port, usb_port_low_speed(&status));
623 } else {
624 dprintf(USB_LOG_LEVEL_WARNING, "port reset, but port still not enabled");
625 }
626 }
627
628 usb_port_set_connect_change(&status, false);
629 usb_port_set_reset(&status, false);
630 usb_port_set_reset_completed(&status, false);
631 usb_port_set_dev_connected(&status, false);
632 if (status>>16) {
633 dprintf(USB_LOG_LEVEL_INFO, "there was some unsupported change on port %d: %X",port,status);
634
635 }
636 /// \TODO handle other changes
637}
638
639/**
640 * Check changes on particular hub
641 * @param hub_info_param pointer to usb_hub_info_t structure
642 * @return error code if there is problem when initializing communication with
643 * hub, EOK otherwise
644 */
645int usb_hub_check_hub_changes(usb_hub_info_t * hub_info){
646 int opResult;
647 opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
648 if(opResult != EOK){
649 dprintf(USB_LOG_LEVEL_ERROR,
650 "could not initialize communication for hub; %d", opResult);
651 return opResult;
652 }
653
654 size_t port_count = hub_info->port_count;
655
656 /// FIXME: count properly
657 size_t byte_length = ((port_count+1) / 8) + 1;
658 void *change_bitmap = malloc(byte_length);
659 size_t actual_size;
660
661 /*
662 * Send the request.
663 */
664 opResult = usb_endpoint_pipe_read(
665 &hub_info->endpoints.status_change,
666 change_bitmap, byte_length, &actual_size
667 );
668
669 if (opResult != EOK) {
670 free(change_bitmap);
671 dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
672 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
673 return opResult;
674 }
675 unsigned int port;
676 opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
677 if(opResult!=EOK){
678 dprintf(USB_LOG_LEVEL_ERROR, "could not start control pipe session %d",
679 opResult);
680 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
681 return opResult;
682 }
683 opResult = usb_hc_connection_open(&hub_info->connection);
684 if(opResult!=EOK){
685 dprintf(USB_LOG_LEVEL_ERROR, "could not start host controller session %d",
686 opResult);
687 usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
688 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
689 return opResult;
690 }
691
692 ///todo, opresult check, pre obe konekce
693 for (port = 1; port < port_count+1; ++port) {
694 bool interrupt =
695 (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
696 if (interrupt) {
697 usb_hub_process_interrupt(
698 hub_info, port);
699 }
700 }
701 usb_hc_connection_close(&hub_info->connection);
702 usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
703 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
704 free(change_bitmap);
705 return EOK;
706}
707
708
709
710/**
711 * @}
712 */
Note: See TracBrowser for help on using the repository browser.