Changes in uspace/drv/usbhub/usbhub.c [49ce810:6ab7f3e9] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/usbhub/usbhub.c
r49ce810 r6ab7f3e9 56 56 57 57 58 static usb_hub_info_t * usb_hub_info_create(usb_device_t * 59 60 static int usb_hub_process_hub_specific_info(usb_hub_info_t * 61 62 static int usb_hub_set_configuration(usb_hub_info_t * 63 64 static int usb_hub_start_hub_fibril(usb_hub_info_t * 65 66 static int usb_process_hub_over_current(usb_hub_info_t * 58 static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev); 59 60 static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info); 61 62 static int usb_hub_set_configuration(usb_hub_info_t *hub_info); 63 64 static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info); 65 66 static int usb_process_hub_over_current(usb_hub_info_t *hub_info, 67 67 usb_hub_status_t status); 68 68 69 static int usb_process_hub_local_power_change(usb_hub_info_t * 69 static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info, 70 70 usb_hub_status_t status); 71 71 72 static void usb_hub_process_global_interrupt(usb_hub_info_t * 73 74 static void usb_hub_polling_termin ted_callback(usb_device_t *device,75 bool was_error, void * 72 static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info); 73 74 static void usb_hub_polling_terminated_callback(usb_device_t *device, 75 bool was_error, void *data); 76 76 77 77 … … 90 90 * @return error code 91 91 */ 92 int usb_hub_add_device(usb_device_t * 92 int usb_hub_add_device(usb_device_t *usb_dev) { 93 93 if (!usb_dev) return EINVAL; 94 usb_hub_info_t * 94 usb_hub_info_t *hub_info = usb_hub_info_create(usb_dev); 95 95 //create hc connection 96 96 usb_log_debug("Initializing USB wire abstraction.\n"); … … 99 99 hub_info->usb_device->ddf_dev); 100 100 if (opResult != EOK) { 101 usb_log_error(" could not initialize connection to device, "102 " errno %d\n",103 opResult);101 usb_log_error("Could not initialize connection to device, " 102 " %s\n", 103 str_error(opResult)); 104 104 free(hub_info); 105 105 return opResult; … … 109 109 opResult = usb_hub_set_configuration(hub_info); 110 110 if (opResult != EOK) { 111 usb_log_error(" could not set hub configuration, errno %d\n",112 opResult);111 usb_log_error("Could not set hub configuration, %s\n", 112 str_error(opResult)); 113 113 free(hub_info); 114 114 return opResult; … … 117 117 opResult = usb_hub_process_hub_specific_info(hub_info); 118 118 if (opResult != EOK) { 119 usb_log_error(" could process hub specific info, errno %d\n",120 opResult);119 usb_log_error("Could process hub specific info, %s\n", 120 str_error(opResult)); 121 121 free(hub_info); 122 122 return opResult; … … 135 135 136 136 opResult = usb_hub_start_hub_fibril(hub_info); 137 if (opResult!=EOK)137 if (opResult != EOK) 138 138 free(hub_info); 139 139 return opResult; 140 140 } 141 142 141 143 142 /** Callback for polling hub for changes. … … 193 192 * @return basic usb_hub_info_t structure 194 193 */ 195 static usb_hub_info_t * usb_hub_info_create(usb_device_t * 196 usb_hub_info_t * result = malloc(sizeof (usb_hub_info_t));194 static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev) { 195 usb_hub_info_t * result = malloc(sizeof (usb_hub_info_t)); 197 196 if (!result) return NULL; 198 197 result->usb_device = usb_dev; … … 200 199 result->control_pipe = &usb_dev->ctrl_pipe; 201 200 result->is_default_address_used = false; 201 202 result->ports = NULL; 203 result->port_count = (size_t) - 1; 204 fibril_mutex_initialize(&result->port_mutex); 205 206 fibril_mutex_initialize(&result->pending_ops_mutex); 207 fibril_condvar_initialize(&result->pending_ops_cv); 208 result->pending_ops_count = 0; 202 209 return result; 203 210 } … … 213 220 * @return error code 214 221 */ 215 static int usb_hub_process_hub_specific_info(usb_hub_info_t * 222 static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info) { 216 223 // get hub descriptor 217 usb_log_debug(" creating serialized descriptor\n");224 usb_log_debug("Creating serialized descriptor\n"); 218 225 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE]; 219 226 usb_hub_descriptor_t * descriptor; … … 227 234 228 235 if (opResult != EOK) { 229 usb_log_error(" failed when receiving hub descriptor, "230 " badcode = %d\n",231 opResult);236 usb_log_error("Failed when receiving hub descriptor, " 237 "%s\n", 238 str_error(opResult)); 232 239 free(serialized_descriptor); 233 240 return opResult; 234 241 } 235 usb_log_debug2(" deserializing descriptor\n");242 usb_log_debug2("Deserializing descriptor\n"); 236 243 descriptor = usb_create_deserialized_hub_desriptor( 237 244 serialized_descriptor); … … 244 251 /// \TODO this is not semantically correct 245 252 bool is_power_switched = 246 ((descriptor->hub_characteristics & 1) == 0);253 ((descriptor->hub_characteristics & 1) == 0); 247 254 bool has_individual_port_powering = 248 ((descriptor->hub_characteristics & 1) != 0);255 ((descriptor->hub_characteristics & 1) != 0); 249 256 hub_info->ports = malloc( 250 257 sizeof (usb_hub_port_t) * (hub_info->port_count + 1)); 251 if (!hub_info->ports){258 if (!hub_info->ports) { 252 259 return ENOMEM; 253 260 } … … 256 263 usb_hub_port_init(&hub_info->ports[port]); 257 264 } 258 if (is_power_switched){259 usb_log_debug(" is_power_switched\n");260 261 if (!has_individual_port_powering){262 usb_log_debug(" !has_individual_port_powering\n");265 if (is_power_switched) { 266 usb_log_debug("Hub power switched\n"); 267 268 if (!has_individual_port_powering) { 269 usb_log_debug("Has_global powering\n"); 263 270 opResult = usb_hub_set_feature(hub_info->control_pipe, 264 271 USB_HUB_FEATURE_C_HUB_LOCAL_POWER); 265 272 if (opResult != EOK) { 266 usb_log_error(" cannot power hub: %s\n",273 usb_log_error("Cannot power hub: %s\n", 267 274 str_error(opResult)); 268 275 } … … 270 277 271 278 for (port = 1; port <= hub_info->port_count; ++port) { 272 usb_log_debug("Powering port %zu.\n", port);279 usb_log_debug("Powering port %zu.\n", port); 273 280 opResult = usb_hub_set_port_feature(hub_info->control_pipe, 274 281 port, USB_HUB_FEATURE_PORT_POWER); … … 278 285 } 279 286 } 280 281 } else{282 usb_log_debug(" !is_power_switched, not going to be powered\n");283 } 284 usb_log_debug2(" freeing data\n");287 288 } else { 289 usb_log_debug("Power not switched, not going to be powered\n"); 290 } 291 usb_log_debug2("Freeing data\n"); 285 292 free(descriptor); 286 293 return EOK; … … 295 302 * @return error code 296 303 */ 297 static int usb_hub_set_configuration(usb_hub_info_t * 304 static int usb_hub_set_configuration(usb_hub_info_t *hub_info) { 298 305 //device descriptor 299 306 usb_standard_device_descriptor_t *std_descriptor 300 307 = &hub_info->usb_device->descriptors.device; 301 usb_log_debug(" hub has %d configurations\n",308 usb_log_debug("Hub has %d configurations\n", 302 309 std_descriptor->configuration_count); 303 310 if (std_descriptor->configuration_count < 1) { 304 usb_log_error(" there are no configurations available\n");311 usb_log_error("There are no configurations available\n"); 305 312 return EINVAL; 306 313 } … … 320 327 return opResult; 321 328 } 322 usb_log_debug("\t used configuration %d\n",329 usb_log_debug("\tUsed configuration %d\n", 323 330 config_descriptor->configuration_number); 324 331 … … 335 342 * @return error code 336 343 */ 337 static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info){344 static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info) { 338 345 int rc; 339 346 340 347 rc = usb_device_auto_poll(hub_info->usb_device, 0, 341 348 hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1, 342 usb_hub_polling_termin ted_callback, hub_info);349 usb_hub_polling_terminated_callback, hub_info); 343 350 if (rc != EOK) { 344 351 usb_log_error("Failed to create polling fibril: %s.\n", … … 359 366 //********************************************* 360 367 361 362 368 /** 363 369 * process hub over current change … … 368 374 * @return error code 369 375 */ 370 static int usb_process_hub_over_current(usb_hub_info_t * 376 static int usb_process_hub_over_current(usb_hub_info_t *hub_info, 371 377 usb_hub_status_t status) { 372 378 int opResult; 373 if (usb_hub_is_status(status, USB_HUB_FEATURE_HUB_OVER_CURRENT)){379 if (usb_hub_is_status(status, USB_HUB_FEATURE_HUB_OVER_CURRENT)) { 374 380 //poweroff all ports 375 381 unsigned int port; 376 for (port = 1;port <= hub_info->port_count;++port){382 for (port = 1; port <= hub_info->port_count; ++port) { 377 383 opResult = usb_hub_clear_port_feature( 378 hub_info->control_pipe, port,384 hub_info->control_pipe, port, 379 385 USB_HUB_FEATURE_PORT_POWER); 380 386 if (opResult != EOK) { 381 387 usb_log_warning( 382 " cannot power off port %d; %d\n",383 port, opResult);388 "Cannot power off port %d; %s\n", 389 port, str_error(opResult)); 384 390 } 385 391 } … … 387 393 //power all ports 388 394 unsigned int port; 389 for (port = 1;port <= hub_info->port_count;++port){395 for (port = 1; port <= hub_info->port_count; ++port) { 390 396 opResult = usb_hub_set_port_feature( 391 hub_info->control_pipe, port,397 hub_info->control_pipe, port, 392 398 USB_HUB_FEATURE_PORT_POWER); 393 399 if (opResult != EOK) { 394 400 usb_log_warning( 395 " cannot power off port %d; %d\n",396 port, opResult);401 "Cannot power off port %d; %s\n", 402 port, str_error(opResult)); 397 403 } 398 404 } … … 409 415 * @return error code 410 416 */ 411 static int usb_process_hub_local_power_change(usb_hub_info_t * 417 static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info, 412 418 usb_hub_status_t status) { 413 419 int opResult = EOK; 414 420 opResult = usb_hub_clear_feature(hub_info->control_pipe, 415 421 USB_HUB_FEATURE_C_HUB_LOCAL_POWER); 416 417 usb_log_error(" cannnot clear hub power change flag: "418 "% d\n",419 opResult);422 if (opResult != EOK) { 423 usb_log_error("Cannnot clear hub power change flag: " 424 "%s\n", 425 str_error(opResult)); 420 426 } 421 427 return opResult; … … 429 435 * @param hub_info hub instance 430 436 */ 431 static void usb_hub_process_global_interrupt(usb_hub_info_t * 432 usb_log_debug(" global interrupt on a hub\n");437 static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info) { 438 usb_log_debug("Global interrupt on a hub\n"); 433 439 usb_pipe_t *pipe = hub_info->control_pipe; 434 440 int opResult; … … 447 453 ); 448 454 if (opResult != EOK) { 449 usb_log_error("could not get hub status\n"); 455 usb_log_error("Could not get hub status: %s\n", 456 str_error(opResult)); 450 457 return; 451 458 } 452 459 if (rcvd_size != sizeof (usb_port_status_t)) { 453 usb_log_error(" received status has incorrect size\n");460 usb_log_error("Received status has incorrect size\n"); 454 461 return; 455 462 } 456 463 //port reset 457 464 if ( 458 usb_hub_is_status(status, 16+USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {465 usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) { 459 466 usb_process_hub_over_current(hub_info, status); 460 467 } 461 468 if ( 462 usb_hub_is_status(status, 16+USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {469 usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) { 463 470 usb_process_hub_local_power_change(hub_info, status); 464 471 } … … 473 480 * @param data pointer to usb_hub_info_t structure 474 481 */ 475 static void usb_hub_polling_terminted_callback(usb_device_t * device, 476 bool was_error, void * data){ 477 usb_hub_info_t * hub_info = data; 478 if(!hub_info) return; 479 free(hub_info->ports); 480 free(hub_info); 482 static void usb_hub_polling_terminated_callback(usb_device_t *device, 483 bool was_error, void *data) { 484 usb_hub_info_t * hub = data; 485 assert(hub); 486 487 fibril_mutex_lock(&hub->pending_ops_mutex); 488 489 /* The device is dead. However there might be some pending operations 490 * that we need to wait for. 491 * One of them is device adding in progress. 492 * The respective fibril is probably waiting for status change 493 * in port reset (port enable) callback. 494 * Such change would never come (otherwise we would not be here). 495 * Thus, we would flush all pending port resets. 496 */ 497 if (hub->pending_ops_count > 0) { 498 fibril_mutex_lock(&hub->port_mutex); 499 size_t port; 500 for (port = 0; port < hub->port_count; port++) { 501 usb_hub_port_t *the_port = hub->ports + port; 502 fibril_mutex_lock(&the_port->reset_mutex); 503 the_port->reset_completed = true; 504 the_port->reset_okay = false; 505 fibril_condvar_broadcast(&the_port->reset_cv); 506 fibril_mutex_unlock(&the_port->reset_mutex); 507 } 508 fibril_mutex_unlock(&hub->port_mutex); 509 } 510 /* And now wait for them. */ 511 while (hub->pending_ops_count > 0) { 512 fibril_condvar_wait(&hub->pending_ops_cv, 513 &hub->pending_ops_mutex); 514 } 515 fibril_mutex_unlock(&hub->pending_ops_mutex); 516 517 free(hub->ports); 518 free(hub); 481 519 } 482 520
Note:
See TracChangeset
for help on using the changeset viewer.