source: mainline/uspace/drv/usbhub/usbhub.c@ 0f21c0c

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0f21c0c was 0f21c0c, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Removal of API that use phones directly

  • Property mode set to 100644
File size: 16.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 <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
47#include "usbhub.h"
48#include "usbhub_private.h"
49#include "port_status.h"
50#include "usb/usb.h"
51#include "usb/pipes.h"
52
53static device_ops_t hub_device_ops = {
54 .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
55};
56
57//*********************************************
58//
59// hub driver code, initialization
60//
61//*********************************************
62
63usb_hub_info_t * usb_create_hub_info(device_t * device) {
64 usb_hub_info_t* result = usb_new(usb_hub_info_t);
65 usb_device_connection_initialize_from_device(&result->device_connection,
66 device);
67 usb_hc_connection_initialize_from_device(&result->connection,
68 device);
69 usb_endpoint_pipe_initialize_default_control(&result->endpoints.control,
70 &result->device_connection);
71
72
73 //result->device = device;
74 result->port_count = -1;
75 /// \TODO is this correct? is the device stored?
76 result->device = device;
77
78 //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
79
80 // get hub descriptor
81
82 dprintf(USB_LOG_LEVEL_DEBUG, "creating serialized descripton");
83 void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
84 usb_hub_descriptor_t * descriptor;
85 size_t received_size;
86 int opResult;
87 dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
88 usb_endpoint_pipe_start_session(&result->endpoints.control);
89 opResult = usb_request_get_descriptor(&result->endpoints.control,
90 USB_REQUEST_TYPE_CLASS,
91 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
92 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
93 usb_endpoint_pipe_end_session(&result->endpoints.control);
94
95 /* Initialize the interrupt endpoint.
96 usb_endpoint_pipe_initalize(
97 &hub_data->endpoints->status_change,
98 &endpiont_descriptor, &hub_data->connection);
99
100 */ /// \TODO add this call
101
102 if (opResult != EOK) {
103 dprintf(USB_LOG_LEVEL_ERROR, "failed when receiving hub descriptor, badcode = %d",opResult);
104 free(serialized_descriptor);
105 return result;
106 }
107 dprintf(USB_LOG_LEVEL_DEBUG2, "deserializing descriptor");
108 descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
109 if(descriptor==NULL){
110 dprintf(USB_LOG_LEVEL_WARNING, "could not deserialize descriptor ");
111 result->port_count = 1;///\TODO this code is only for debug!!!
112 return result;
113 }
114 dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
115 result->port_count = descriptor->ports_count;
116 result->attached_devs = (usb_hc_attached_device_t*)
117 malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
118 int i;
119 for(i=0;i<result->port_count+1;++i){
120 result->attached_devs[i].handle=0;
121 result->attached_devs[i].address=0;
122 }
123 dprintf(USB_LOG_LEVEL_DEBUG2, "freeing data");
124 free(serialized_descriptor);
125 free(descriptor->devices_removable);
126 free(descriptor);
127
128 //finish
129
130 dprintf(USB_LOG_LEVEL_INFO, "hub info created");
131
132 return result;
133}
134
135int usb_add_hub_device(device_t *dev) {
136 dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
137
138 /*
139 * We are some (probably deeply nested) hub.
140 * Thus, assign our own operations and explore already
141 * connected devices.
142 */
143 dev->ops = &hub_device_ops;
144
145
146 usb_hub_info_t * hub_info = usb_create_hub_info(dev);
147 usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
148
149 int port;
150 int opResult;
151 //usb_target_t target;
152 //target.address = hub_info->usb_device->address;
153 //target.endpoint = 0;
154
155 //get configuration descriptor
156 // this is not fully correct - there are more configurations
157 // and all should be checked
158 usb_standard_device_descriptor_t std_descriptor;
159 opResult = usb_request_get_device_descriptor(&hub_info->endpoints.control,
160 &std_descriptor);
161 if(opResult!=EOK){
162 dprintf(USB_LOG_LEVEL_ERROR, "could not get device descriptor, %d",opResult);
163 return opResult;
164 }
165 dprintf(USB_LOG_LEVEL_INFO, "hub has %d configurations",std_descriptor.configuration_count);
166 if(std_descriptor.configuration_count<1){
167 dprintf(USB_LOG_LEVEL_ERROR, "THERE ARE NO CONFIGURATIONS AVAILABLE");
168 //shouldn`t I return?
169 }
170 /// \TODO check other configurations
171 usb_standard_configuration_descriptor_t config_descriptor;
172 opResult = usb_request_get_bare_configuration_descriptor(
173 &hub_info->endpoints.control, 0,
174 &config_descriptor);
175 if(opResult!=EOK){
176 dprintf(USB_LOG_LEVEL_ERROR, "could not get configuration descriptor, %d",opResult);
177 return opResult;
178 }
179 //set configuration
180 opResult = usb_request_set_configuration(&hub_info->endpoints.control,
181 config_descriptor.configuration_number);
182
183 if (opResult != EOK) {
184 dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when setting hub`s configuration, %d", opResult);
185 }
186
187 usb_device_request_setup_packet_t request;
188 for (port = 1; port < hub_info->port_count+1; ++port) {
189 usb_hub_set_power_port_request(&request, port);
190 opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
191 &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
192 dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
193 if (opResult != EOK) {
194 dprintf(USB_LOG_LEVEL_WARNING, "something went wrong when setting hub`s %dth port", port);
195 }
196 }
197 //ports powered, hub seems to be enabled
198
199 usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
200 //async_hangup(hc);
201
202 //add the hub to list
203 fibril_mutex_lock(&usb_hub_list_lock);
204 usb_lst_append(&usb_hub_list, hub_info);
205 fibril_mutex_unlock(&usb_hub_list_lock);
206
207 dprintf(USB_LOG_LEVEL_DEBUG, "hub info added to list");
208 //(void)hub_info;
209 usb_hub_check_hub_changes();
210
211
212
213 dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
214 //address is lost...
215 dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
216 //hub_info->endpoints.control.,
217 hub_info->port_count);
218 dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",config_descriptor.configuration_number);
219
220 return EOK;
221 //return ENOTSUP;
222}
223
224
225//*********************************************
226//
227// hub driver code, main loop
228//
229//*********************************************
230
231/**
232 * Reset the port with new device and reserve the default address.
233 * @param hc
234 * @param port
235 * @param target
236 */
237static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port) {
238 usb_device_request_setup_packet_t request;
239 int opResult;
240 dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
241 assert(hub->endpoints.control.hc_phone);
242 //get default address
243 //opResult = usb_drv_reserve_default_address(hc);
244 opResult = usb_hc_reserve_default_address(&hub->connection, USB_SPEED_LOW);
245
246 if (opResult != EOK) {
247 dprintf(USB_LOG_LEVEL_WARNING, "cannot assign default address, it is probably used");
248 return;
249 }
250 //reset port
251 usb_hub_set_reset_port_request(&request, port);
252 opResult = usb_endpoint_pipe_control_write(
253 &hub->endpoints.control,
254 &request,sizeof(usb_device_request_setup_packet_t),
255 NULL, 0
256 );
257 if (opResult != EOK) {
258 dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when reseting a port");
259 //usb_hub_release_default_address(hc);
260 usb_hc_release_default_address(&hub->connection);
261 }
262}
263
264/**
265 * Finalize adding new device after port reset
266 * @param hc
267 * @param port
268 * @param target
269 */
270static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
271 uint16_t port) {
272
273 int opResult;
274 dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
275 opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
276 port, USB_HUB_FEATURE_C_PORT_RESET);
277
278 if (opResult != EOK) {
279 dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
280 usb_hc_release_default_address(&hub->connection);
281 return;
282 }
283 //create connection to device
284 usb_endpoint_pipe_t new_device_pipe;
285 usb_device_connection_t new_device_connection;
286 usb_device_connection_initialize_on_default_address(
287 &new_device_connection,
288 &hub->connection
289 );
290 usb_endpoint_pipe_initialize_default_control(
291 &new_device_pipe,
292 &new_device_connection);
293 /// \TODO get highspeed info
294
295
296
297
298
299 /* Request address from host controller. */
300 usb_address_t new_device_address = usb_hc_request_address(
301 &hub->connection,
302 USB_SPEED_LOW/// \TODO fullspeed??
303 );
304 if (new_device_address < 0) {
305 dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
306 opResult = new_device_address;
307 usb_hc_release_default_address(&hub->connection);
308 return;
309 }
310 dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
311 //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
312 // new_device_address);
313 opResult = usb_request_set_address(&new_device_pipe,new_device_address);
314
315 if (opResult != EOK) {
316 dprintf(USB_LOG_LEVEL_ERROR, "could not set address for new device");
317 usb_hc_release_default_address(&hub->connection);
318 return;
319 }
320
321
322 //opResult = usb_hub_release_default_address(hc);
323 opResult = usb_hc_release_default_address(&hub->connection);
324 if(opResult!=EOK){
325 return;
326 }
327
328 devman_handle_t child_handle;
329 //??
330 opResult = usb_device_register_child_in_devman(new_device_address,
331 hub->connection.hc_handle, hub->device, &child_handle);
332
333 if (opResult != EOK) {
334 dprintf(USB_LOG_LEVEL_ERROR, "could not start driver for new device");
335 return;
336 }
337 hub->attached_devs[port].handle = child_handle;
338 hub->attached_devs[port].address = new_device_address;
339
340 //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
341 opResult = usb_hc_register_device(
342 &hub->connection,
343 &hub->attached_devs[port]);
344 if (opResult != EOK) {
345 dprintf(USB_LOG_LEVEL_ERROR, "could not assign address of device in hcd");
346 return;
347 }
348 dprintf(USB_LOG_LEVEL_INFO, "new device address %d, handle %zu",
349 new_device_address, child_handle);
350
351}
352
353/**
354 * Unregister device address in hc
355 * @param hc
356 * @param port
357 * @param target
358 */
359static void usb_hub_removed_device(
360 usb_hub_info_t * hub,uint16_t port) {
361 //usb_device_request_setup_packet_t request;
362 int opResult;
363
364 /** \TODO remove device from device manager - not yet implemented in
365 * devide manager
366 */
367
368 //close address
369 if(hub->attached_devs[port].address!=0){
370 //opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
371 opResult = usb_hc_unregister_device(
372 &hub->connection, hub->attached_devs[port].address);
373 if(opResult != EOK) {
374 dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
375 "removed device: %d", opResult);
376 }
377 hub->attached_devs[port].address = 0;
378 hub->attached_devs[port].handle = 0;
379 }else{
380 dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
381 //device was disconnected before it`s port was reset - return default address
382 //usb_drv_release_default_address(hc);
383 usb_hc_release_default_address(&hub->connection);
384 }
385}
386
387/**
388 * Process interrupts on given hub port
389 * @param hc
390 * @param port
391 * @param target
392 */
393static void usb_hub_process_interrupt(usb_hub_info_t * hub,
394 uint16_t port) {
395 dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
396 //determine type of change
397 usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
398 int opResult = usb_endpoint_pipe_start_session(pipe);
399
400 if(opResult != EOK){
401 dprintf(USB_LOG_LEVEL_ERROR, "cannot open pipe %d", opResult);
402 }
403
404 /*
405 usb_target_t target;
406 target.address=address;
407 target.endpoint=0;
408 */
409
410 usb_port_status_t status;
411 size_t rcvd_size;
412 usb_device_request_setup_packet_t request;
413 //int opResult;
414 usb_hub_set_port_status_request(&request, port);
415 //endpoint 0
416
417 opResult = usb_endpoint_pipe_control_read(
418 pipe,
419 &request, sizeof(usb_device_request_setup_packet_t),
420 &status, 4, &rcvd_size
421 );
422 if (opResult != EOK) {
423 dprintf(USB_LOG_LEVEL_ERROR, "ERROR: could not get port status");
424 return;
425 }
426 if (rcvd_size != sizeof (usb_port_status_t)) {
427 dprintf(USB_LOG_LEVEL_ERROR, "ERROR: received status has incorrect size");
428 return;
429 }
430 //something connected/disconnected
431 if (usb_port_connect_change(&status)) {
432 opResult = usb_hub_clear_port_feature(pipe,
433 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
434 // TODO: check opResult
435 if (usb_port_dev_connected(&status)) {
436 dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
437 usb_hub_init_add_device(hub, port);
438 } else {
439 usb_hub_removed_device(hub, port);
440 }
441 }
442 //port reset
443 if (usb_port_reset_completed(&status)) {
444 dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
445 if (usb_port_enabled(&status)) {
446 usb_hub_finalize_add_device(hub, port);
447 } else {
448 dprintf(USB_LOG_LEVEL_WARNING, "ERROR: port reset, but port still not enabled");
449 }
450 }
451
452 usb_port_set_connect_change(&status, false);
453 usb_port_set_reset(&status, false);
454 usb_port_set_reset_completed(&status, false);
455 usb_port_set_dev_connected(&status, false);
456 if (status>>16) {
457 dprintf(USB_LOG_LEVEL_INFO, "there was some unsupported change on port %d: %X",port,status);
458
459 }
460 /// \TODO handle other changes
461 /// \TODO debug log for various situations
462 usb_endpoint_pipe_end_session(pipe);
463
464
465}
466
467/**
468 * Check changes on all known hubs.
469 */
470void usb_hub_check_hub_changes(void) {
471 /*
472 * Iterate through all hubs.
473 */
474 usb_general_list_t * lst_item;
475 fibril_mutex_lock(&usb_hub_list_lock);
476 for (lst_item = usb_hub_list.next;
477 lst_item != &usb_hub_list;
478 lst_item = lst_item->next) {
479 fibril_mutex_unlock(&usb_hub_list_lock);
480 usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);
481 int opResult;
482
483 opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
484 if(opResult != EOK){
485 continue;
486 }
487 /*
488 * Check status change pipe of this hub.
489 */
490 /*
491 usb_target_t target;
492 target.address = hub_info->address;
493 target.endpoint = 1;/// \TODO get from endpoint descriptor
494 dprintf(USB_LOG_LEVEL_INFO, "checking changes for hub at addr %d",
495 target.address);
496 */
497 size_t port_count = hub_info->port_count;
498
499 /*
500 * Connect to respective HC.
501 *
502 int hc = usb_drv_hc_connect_auto(hub_info->device, 0);
503 if (hc < 0) {
504 continue;
505 }*/
506
507 /// FIXME: count properly
508 size_t byte_length = ((port_count+1) / 8) + 1;
509
510 void *change_bitmap = malloc(byte_length);
511 size_t actual_size;
512 //usb_handle_t handle;
513
514 /*
515 * Send the request.
516 */
517 opResult = usb_endpoint_pipe_read(
518 &hub_info->endpoints.status_change,
519 change_bitmap, byte_length, &actual_size
520 );
521
522 //usb_drv_async_wait_for(handle);
523
524 if (opResult != EOK) {
525 free(change_bitmap);
526 dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
527 continue;
528 }
529 unsigned int port;
530 for (port = 1; port < port_count+1; ++port) {
531 bool interrupt =
532 (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
533 if (interrupt) {
534 usb_hub_process_interrupt(
535 hub_info, port);
536 }
537 }
538 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
539 free(change_bitmap);
540
541
542 //async_hangup(hc);
543 fibril_mutex_lock(&usb_hub_list_lock);
544 }
545 fibril_mutex_unlock(&usb_hub_list_lock);
546}
547
548
549
550
551
552/**
553 * @}
554 */
Note: See TracBrowser for help on using the repository browser.