1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 27 /* 28 * USBA: Solaris USB Architecture support for the hub 29 * including root hub 30 * Most of the code for hubd resides in this file and 31 * is shared between the HCD root hub support and hubd 32 */ 33 #define USBA_FRAMEWORK 34 #include <sys/usb/usba.h> 35 #include <sys/usb/usba/usba_devdb.h> 36 #include <sys/sunndi.h> 37 #include <sys/usb/usba/usba_impl.h> 38 #include <sys/usb/usba/usba_types.h> 39 #include <sys/usb/usba/hubdi.h> 40 #include <sys/usb/usba/hcdi_impl.h> 41 #include <sys/usb/hubd/hub.h> 42 #include <sys/usb/hubd/hubdvar.h> 43 #include <sys/usb/hubd/hubd_impl.h> 44 #include <sys/kobj.h> 45 #include <sys/kobj_lex.h> 46 #include <sys/fs/dv_node.h> 47 #include <sys/strsun.h> 48 49 /* 50 * External functions 51 */ 52 extern boolean_t consconfig_console_is_ready(void); 53 54 /* 55 * Prototypes for static functions 56 */ 57 static int usba_hubdi_bus_ctl( 58 dev_info_t *dip, 59 dev_info_t *rdip, 60 ddi_ctl_enum_t op, 61 void *arg, 62 void *result); 63 64 static int usba_hubdi_map_fault( 65 dev_info_t *dip, 66 dev_info_t *rdip, 67 struct hat *hat, 68 struct seg *seg, 69 caddr_t addr, 70 struct devpage *dp, 71 pfn_t pfn, 72 uint_t prot, 73 uint_t lock); 74 75 static int hubd_busop_get_eventcookie(dev_info_t *dip, 76 dev_info_t *rdip, 77 char *eventname, 78 ddi_eventcookie_t *cookie); 79 static int hubd_busop_add_eventcall(dev_info_t *dip, 80 dev_info_t *rdip, 81 ddi_eventcookie_t cookie, 82 void (*callback)(dev_info_t *dip, 83 ddi_eventcookie_t cookie, void *arg, 84 void *bus_impldata), 85 void *arg, ddi_callback_id_t *cb_id); 86 static int hubd_busop_remove_eventcall(dev_info_t *dip, 87 ddi_callback_id_t cb_id); 88 static int hubd_bus_config(dev_info_t *dip, 89 uint_t flag, 90 ddi_bus_config_op_t op, 91 void *arg, 92 dev_info_t **child); 93 static int hubd_bus_unconfig(dev_info_t *dip, 94 uint_t flag, 95 ddi_bus_config_op_t op, 96 void *arg); 97 static int hubd_bus_power(dev_info_t *dip, void *impl_arg, 98 pm_bus_power_op_t op, void *arg, void *result); 99 100 static usb_port_t hubd_get_port_num(hubd_t *, struct devctl_iocdata *); 101 static dev_info_t *hubd_get_child_dip(hubd_t *, usb_port_t); 102 static uint_t hubd_cfgadm_state(hubd_t *, usb_port_t); 103 static int hubd_toggle_port(hubd_t *, usb_port_t); 104 static void hubd_register_cpr_callback(hubd_t *); 105 static void hubd_unregister_cpr_callback(hubd_t *); 106 107 /* 108 * Busops vector for USB HUB's 109 */ 110 struct bus_ops usba_hubdi_busops = { 111 BUSO_REV, 112 nullbusmap, /* bus_map */ 113 NULL, /* bus_get_intrspec */ 114 NULL, /* bus_add_intrspec */ 115 NULL, /* bus_remove_intrspec */ 116 usba_hubdi_map_fault, /* bus_map_fault */ 117 ddi_dma_map, /* bus_dma_map */ 118 ddi_dma_allochdl, 119 ddi_dma_freehdl, 120 ddi_dma_bindhdl, 121 ddi_dma_unbindhdl, 122 ddi_dma_flush, 123 ddi_dma_win, 124 ddi_dma_mctl, /* bus_dma_ctl */ 125 usba_hubdi_bus_ctl, /* bus_ctl */ 126 ddi_bus_prop_op, /* bus_prop_op */ 127 hubd_busop_get_eventcookie, 128 hubd_busop_add_eventcall, 129 hubd_busop_remove_eventcall, 130 NULL, /* bus_post_event */ 131 NULL, /* bus_intr_ctl */ 132 hubd_bus_config, /* bus_config */ 133 hubd_bus_unconfig, /* bus_unconfig */ 134 NULL, /* bus_fm_init */ 135 NULL, /* bus_fm_fini */ 136 NULL, /* bus_fm_access_enter */ 137 NULL, /* bus_fm_access_exit */ 138 hubd_bus_power /* bus_power */ 139 }; 140 141 142 /* 143 * local variables 144 */ 145 static kmutex_t usba_hubdi_mutex; /* protects USBA HUB data structures */ 146 147 static usba_list_entry_t usba_hubdi_list; 148 149 usb_log_handle_t hubdi_log_handle; 150 uint_t hubdi_errlevel = USB_LOG_L4; 151 uint_t hubdi_errmask = (uint_t)-1; 152 uint8_t hubdi_min_pm_threshold = 5; /* seconds */ 153 uint8_t hubdi_reset_delay = 20; /* seconds */ 154 155 /* 156 * initialize private data 157 */ 158 void 159 usba_hubdi_initialization() 160 { 161 hubdi_log_handle = usb_alloc_log_hdl(NULL, "hubdi", &hubdi_errlevel, 162 &hubdi_errmask, NULL, 0); 163 164 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle, 165 "usba_hubdi_initialization"); 166 167 mutex_init(&usba_hubdi_mutex, NULL, MUTEX_DRIVER, NULL); 168 169 usba_init_list(&usba_hubdi_list, NULL, NULL); 170 } 171 172 173 void 174 usba_hubdi_destroy() 175 { 176 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle, 177 "usba_hubdi_destroy"); 178 179 mutex_destroy(&usba_hubdi_mutex); 180 usba_destroy_list(&usba_hubdi_list); 181 182 usb_free_log_hdl(hubdi_log_handle); 183 } 184 185 186 /* 187 * Called by an HUB to attach an instance of the driver 188 * make this instance known to USBA 189 * the HUB should initialize usba_hubdi structure prior 190 * to calling this interface 191 */ 192 int 193 usba_hubdi_register(dev_info_t *dip, 194 uint_t flags) 195 { 196 usba_hubdi_t *hubdi = kmem_zalloc(sizeof (usba_hubdi_t), KM_SLEEP); 197 usba_device_t *usba_device = usba_get_usba_device(dip); 198 199 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle, 200 "usba_hubdi_register: %s", ddi_node_name(dip)); 201 202 hubdi->hubdi_dip = dip; 203 hubdi->hubdi_flags = flags; 204 205 usba_device->usb_hubdi = hubdi; 206 207 /* 208 * add this hubdi instance to the list of known hubdi's 209 */ 210 usba_init_list(&hubdi->hubdi_list, (usb_opaque_t)hubdi, 211 usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)-> 212 hcdi_iblock_cookie); 213 mutex_enter(&usba_hubdi_mutex); 214 usba_add_to_list(&usba_hubdi_list, &hubdi->hubdi_list); 215 mutex_exit(&usba_hubdi_mutex); 216 217 return (DDI_SUCCESS); 218 } 219 220 221 /* 222 * Called by an HUB to detach an instance of the driver 223 */ 224 int 225 usba_hubdi_unregister(dev_info_t *dip) 226 { 227 usba_device_t *usba_device = usba_get_usba_device(dip); 228 usba_hubdi_t *hubdi = usba_device->usb_hubdi; 229 230 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle, 231 "usba_hubdi_unregister: %s", ddi_node_name(dip)); 232 233 mutex_enter(&usba_hubdi_mutex); 234 (void) usba_rm_from_list(&usba_hubdi_list, &hubdi->hubdi_list); 235 mutex_exit(&usba_hubdi_mutex); 236 237 usba_destroy_list(&hubdi->hubdi_list); 238 239 kmem_free(hubdi, sizeof (usba_hubdi_t)); 240 241 return (DDI_SUCCESS); 242 } 243 244 245 /* 246 * misc bus routines currently not used 247 */ 248 /*ARGSUSED*/ 249 static int 250 usba_hubdi_map_fault(dev_info_t *dip, 251 dev_info_t *rdip, 252 struct hat *hat, 253 struct seg *seg, 254 caddr_t addr, 255 struct devpage *dp, 256 pfn_t pfn, 257 uint_t prot, 258 uint_t lock) 259 { 260 return (DDI_FAILURE); 261 } 262 263 264 /* 265 * root hub support. the root hub uses the same devi as the HCD 266 */ 267 int 268 usba_hubdi_bind_root_hub(dev_info_t *dip, 269 uchar_t *root_hub_config_descriptor, 270 size_t config_length, 271 usb_dev_descr_t *root_hub_device_descriptor) 272 { 273 usba_device_t *usba_device; 274 usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip); 275 hubd_t *root_hubd; 276 usb_pipe_handle_t ph = NULL; 277 dev_info_t *child = ddi_get_child(dip); 278 279 if (ndi_prop_create_boolean(DDI_DEV_T_NONE, dip, 280 "root-hub") != NDI_SUCCESS) { 281 282 return (USB_FAILURE); 283 } 284 285 usba_add_root_hub(dip); 286 287 root_hubd = kmem_zalloc(sizeof (hubd_t), KM_SLEEP); 288 289 /* 290 * create and initialize a usba_device structure 291 */ 292 usba_device = usba_alloc_usba_device(dip); 293 294 mutex_enter(&usba_device->usb_mutex); 295 usba_device->usb_hcdi_ops = hcdi->hcdi_ops; 296 usba_device->usb_cfg = root_hub_config_descriptor; 297 usba_device->usb_cfg_length = config_length; 298 usba_device->usb_dev_descr = root_hub_device_descriptor; 299 usba_device->usb_port = 1; 300 usba_device->usb_addr = ROOT_HUB_ADDR; 301 usba_device->usb_root_hubd = root_hubd; 302 usba_device->usb_cfg_array = kmem_zalloc(sizeof (uchar_t *), 303 KM_SLEEP); 304 usba_device->usb_cfg_array_length = sizeof (uchar_t *); 305 306 usba_device->usb_cfg_array_len = kmem_zalloc(sizeof (uint16_t), 307 KM_SLEEP); 308 usba_device->usb_cfg_array_len_length = sizeof (uint16_t); 309 310 usba_device->usb_cfg_array[0] = root_hub_config_descriptor; 311 usba_device->usb_cfg_array_len[0] = 312 sizeof (root_hub_config_descriptor); 313 314 usba_device->usb_cfg_str_descr = kmem_zalloc(sizeof (uchar_t *), 315 KM_SLEEP); 316 usba_device->usb_n_cfgs = 1; 317 usba_device->usb_n_ifs = 1; 318 usba_device->usb_dip = dip; 319 320 usba_device->usb_client_flags = kmem_zalloc( 321 usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP); 322 323 usba_device->usb_client_attach_list = kmem_zalloc( 324 usba_device->usb_n_ifs * 325 sizeof (*usba_device->usb_client_attach_list), KM_SLEEP); 326 327 usba_device->usb_client_ev_cb_list = kmem_zalloc( 328 usba_device->usb_n_ifs * 329 sizeof (*usba_device->usb_client_ev_cb_list), KM_SLEEP); 330 331 /* 332 * The bDeviceProtocol field of root hub device specifies, 333 * whether root hub is a High or Full speed usb device. 334 */ 335 if (root_hub_device_descriptor->bDeviceProtocol) { 336 usba_device->usb_port_status = USBA_HIGH_SPEED_DEV; 337 } else { 338 usba_device->usb_port_status = USBA_FULL_SPEED_DEV; 339 } 340 341 mutex_exit(&usba_device->usb_mutex); 342 343 usba_set_usba_device(dip, usba_device); 344 345 /* 346 * For the root hub the default pipe is not yet open 347 */ 348 if (usb_pipe_open(dip, NULL, NULL, 349 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != USB_SUCCESS) { 350 goto fail; 351 } 352 353 /* 354 * kill off all OBP children, they may not be fully 355 * enumerated 356 */ 357 while (child) { 358 dev_info_t *next = ddi_get_next_sibling(child); 359 (void) ddi_remove_child(child, 0); 360 child = next; 361 } 362 363 /* 364 * "attach" the root hub driver 365 */ 366 if (usba_hubdi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) { 367 goto fail; 368 } 369 370 return (USB_SUCCESS); 371 372 fail: 373 (void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub"); 374 375 usba_rem_root_hub(dip); 376 377 if (ph) { 378 usb_pipe_close(dip, ph, 379 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 380 } 381 382 kmem_free(usba_device->usb_cfg_array, 383 usba_device->usb_cfg_array_length); 384 kmem_free(usba_device->usb_cfg_array_len, 385 usba_device->usb_cfg_array_len_length); 386 387 kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *)); 388 389 usba_free_usba_device(usba_device); 390 391 usba_set_usba_device(dip, NULL); 392 if (root_hubd) { 393 kmem_free(root_hubd, sizeof (hubd_t)); 394 } 395 396 return (USB_FAILURE); 397 } 398 399 400 int 401 usba_hubdi_unbind_root_hub(dev_info_t *dip) 402 { 403 usba_device_t *usba_device; 404 405 /* was root hub attached? */ 406 if (!(usba_is_root_hub(dip))) { 407 408 /* return success anyway */ 409 return (USB_SUCCESS); 410 } 411 412 /* 413 * usba_hubdi_detach also closes the default pipe 414 * and removes properties so there is no need to 415 * do it here 416 */ 417 if (usba_hubdi_detach(dip, DDI_DETACH) != DDI_SUCCESS) { 418 419 if (DEVI_IS_ATTACHING(dip)) { 420 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 421 "failure to unbind root hub after attach failure"); 422 } 423 424 return (USB_FAILURE); 425 } 426 427 usba_device = usba_get_usba_device(dip); 428 429 kmem_free(usba_device->usb_root_hubd, sizeof (hubd_t)); 430 431 kmem_free(usba_device->usb_cfg_array, 432 usba_device->usb_cfg_array_length); 433 kmem_free(usba_device->usb_cfg_array_len, 434 usba_device->usb_cfg_array_len_length); 435 436 kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *)); 437 438 usba_free_usba_device(usba_device); 439 440 usba_rem_root_hub(dip); 441 442 (void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub"); 443 444 return (USB_SUCCESS); 445 } 446 447 448 /* 449 * Actual Hub Driver support code: 450 * shared by root hub and non-root hubs 451 */ 452 #include <sys/usb/usba/usbai_version.h> 453 454 /* Debugging support */ 455 uint_t hubd_errlevel = USB_LOG_L4; 456 uint_t hubd_errmask = (uint_t)DPRINT_MASK_ALL; 457 uint_t hubd_instance_debug = (uint_t)-1; 458 static uint_t hubdi_bus_config_debug = 0; 459 460 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel)) 461 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask)) 462 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug)) 463 464 _NOTE(SCHEME_PROTECTS_DATA("unique", msgb)) 465 _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info)) 466 467 468 /* 469 * local variables: 470 * 471 * Amount of time to wait between resetting the port and accessing 472 * the device. The value is in microseconds. 473 */ 474 static uint_t hubd_device_delay = 1000000; 475 476 /* 477 * enumeration retry 478 */ 479 #define HUBD_PORT_RETRY 5 480 static uint_t hubd_retry_enumerate = HUBD_PORT_RETRY; 481 482 /* 483 * Stale hotremoved device cleanup delay 484 */ 485 #define HUBD_STALE_DIP_CLEANUP_DELAY 5000000 486 static uint_t hubd_dip_cleanup_delay = HUBD_STALE_DIP_CLEANUP_DELAY; 487 488 /* 489 * retries for USB suspend and resume 490 */ 491 #define HUBD_SUS_RES_RETRY 2 492 493 void *hubd_statep; 494 495 /* 496 * prototypes 497 */ 498 static int hubd_cleanup(dev_info_t *dip, hubd_t *hubd); 499 static int hubd_check_ports(hubd_t *hubd); 500 501 static int hubd_open_intr_pipe(hubd_t *hubd); 502 static void hubd_start_polling(hubd_t *hubd, int always); 503 static void hubd_stop_polling(hubd_t *hubd); 504 static void hubd_close_intr_pipe(hubd_t *hubd); 505 506 static void hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *req); 507 static void hubd_exception_cb(usb_pipe_handle_t pipe, 508 usb_intr_req_t *req); 509 static void hubd_hotplug_thread(void *arg); 510 static void hubd_reset_thread(void *arg); 511 static int hubd_create_child(dev_info_t *dip, 512 hubd_t *hubd, 513 usba_device_t *usba_device, 514 usb_port_status_t port_status, 515 usb_port_t port, 516 int iteration); 517 518 static int hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, 519 boolean_t retry); 520 521 static int hubd_get_hub_descriptor(hubd_t *hubd); 522 523 static int hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status); 524 525 static int hubd_reset_port(hubd_t *hubd, usb_port_t port); 526 527 static int hubd_get_hub_status(hubd_t *hubd); 528 529 static int hubd_handle_port_connect(hubd_t *hubd, usb_port_t port); 530 531 static int hubd_disable_port(hubd_t *hubd, usb_port_t port); 532 533 static int hubd_enable_port(hubd_t *hubd, usb_port_t port); 534 static int hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port); 535 536 static int hubd_determine_port_status(hubd_t *hubd, usb_port_t port, 537 uint16_t *status, uint16_t *change, uint_t ack_flag); 538 539 static int hubd_enable_all_port_power(hubd_t *hubd); 540 static int hubd_disable_all_port_power(hubd_t *hubd); 541 static int hubd_disable_port_power(hubd_t *hubd, usb_port_t port); 542 static int hubd_enable_port_power(hubd_t *hubd, usb_port_t port); 543 544 static void hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device); 545 546 static int hubd_can_suspend(hubd_t *hubd); 547 static void hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd); 548 static int hubd_setdevaddr(hubd_t *hubd, usb_port_t port); 549 static void hubd_setdevconfig(hubd_t *hubd, usb_port_t port); 550 551 static int hubd_register_events(hubd_t *hubd); 552 static void hubd_do_callback(hubd_t *hubd, dev_info_t *dip, 553 ddi_eventcookie_t cookie); 554 static void hubd_run_callbacks(hubd_t *hubd, usba_event_t type); 555 static void hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type); 556 static void hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd); 557 558 static int hubd_disconnect_event_cb(dev_info_t *dip); 559 static int hubd_reconnect_event_cb(dev_info_t *dip); 560 static int hubd_pre_suspend_event_cb(dev_info_t *dip); 561 static int hubd_post_resume_event_cb(dev_info_t *dip); 562 static int hubd_cpr_suspend(hubd_t *hubd); 563 static void hubd_cpr_resume(dev_info_t *dip); 564 static int hubd_restore_state_cb(dev_info_t *dip); 565 static int hubd_check_same_device(hubd_t *hubd, usb_port_t port); 566 567 static int hubd_init_power_budget(hubd_t *hubd); 568 569 static ndi_event_definition_t hubd_ndi_event_defs[] = { 570 {USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL, 571 NDI_EVENT_POST_TO_ALL}, 572 {USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL, 573 NDI_EVENT_POST_TO_ALL}, 574 {USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL, 575 NDI_EVENT_POST_TO_ALL}, 576 {USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL, 577 NDI_EVENT_POST_TO_ALL} 578 }; 579 580 #define HUBD_N_NDI_EVENTS \ 581 (sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t)) 582 583 static ndi_event_set_t hubd_ndi_events = { 584 NDI_EVENTS_REV1, HUBD_N_NDI_EVENTS, hubd_ndi_event_defs}; 585 586 /* events received from parent */ 587 static usb_event_t hubd_events = { 588 hubd_disconnect_event_cb, 589 hubd_reconnect_event_cb, 590 hubd_pre_suspend_event_cb, 591 hubd_post_resume_event_cb 592 }; 593 594 595 /* 596 * hubd_get_soft_state() returns the hubd soft state 597 * 598 * WUSB support extends this function to support wire adapter class 599 * devices. The hubd soft state for the wire adapter class device 600 * would be stored in usb_root_hubd field of the usba_device structure, 601 * just as the USB host controller drivers do. 602 */ 603 hubd_t * 604 hubd_get_soft_state(dev_info_t *dip) 605 { 606 if (dip == NULL) { 607 608 return (NULL); 609 } 610 611 if (usba_is_root_hub(dip) || usba_is_wa(dip)) { 612 usba_device_t *usba_device = usba_get_usba_device(dip); 613 614 return (usba_device->usb_root_hubd); 615 } else { 616 int instance = ddi_get_instance(dip); 617 618 return (ddi_get_soft_state(hubd_statep, instance)); 619 } 620 } 621 622 623 /* 624 * PM support functions: 625 */ 626 /*ARGSUSED*/ 627 static void 628 hubd_pm_busy_component(hubd_t *hubd, dev_info_t *dip, int component) 629 { 630 if (hubd->h_hubpm != NULL) { 631 hubd->h_hubpm->hubp_busy_pm++; 632 mutex_exit(HUBD_MUTEX(hubd)); 633 if (pm_busy_component(dip, 0) != DDI_SUCCESS) { 634 mutex_enter(HUBD_MUTEX(hubd)); 635 hubd->h_hubpm->hubp_busy_pm--; 636 mutex_exit(HUBD_MUTEX(hubd)); 637 } 638 mutex_enter(HUBD_MUTEX(hubd)); 639 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 640 "hubd_pm_busy_component: %d", hubd->h_hubpm->hubp_busy_pm); 641 } 642 } 643 644 645 /*ARGSUSED*/ 646 static void 647 hubd_pm_idle_component(hubd_t *hubd, dev_info_t *dip, int component) 648 { 649 if (hubd->h_hubpm != NULL) { 650 mutex_exit(HUBD_MUTEX(hubd)); 651 if (pm_idle_component(dip, 0) == DDI_SUCCESS) { 652 mutex_enter(HUBD_MUTEX(hubd)); 653 ASSERT(hubd->h_hubpm->hubp_busy_pm > 0); 654 hubd->h_hubpm->hubp_busy_pm--; 655 mutex_exit(HUBD_MUTEX(hubd)); 656 } 657 mutex_enter(HUBD_MUTEX(hubd)); 658 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 659 "hubd_pm_idle_component: %d", hubd->h_hubpm->hubp_busy_pm); 660 } 661 } 662 663 664 /* 665 * track power level changes for children of this instance 666 */ 667 static void 668 hubd_set_child_pwrlvl(hubd_t *hubd, usb_port_t port, uint8_t power) 669 { 670 int old_power, new_power, pwr; 671 usb_port_t portno; 672 hub_power_t *hubpm; 673 674 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 675 "hubd_set_child_pwrlvl: port=%d power=%d", 676 port, power); 677 678 mutex_enter(HUBD_MUTEX(hubd)); 679 hubpm = hubd->h_hubpm; 680 681 old_power = 0; 682 for (portno = 1; portno <= hubd->h_hub_descr.bNbrPorts; portno++) { 683 old_power += hubpm->hubp_child_pwrstate[portno]; 684 } 685 686 /* assign the port power */ 687 pwr = hubd->h_hubpm->hubp_child_pwrstate[port]; 688 hubd->h_hubpm->hubp_child_pwrstate[port] = power; 689 new_power = old_power - pwr + power; 690 691 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 692 "hubd_set_child_pwrlvl: new_power=%d old_power=%d", 693 new_power, old_power); 694 695 if ((new_power > 0) && (old_power == 0)) { 696 /* we have the first child coming out of low power */ 697 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0); 698 } else if ((new_power == 0) && (old_power > 0)) { 699 /* we have the last child going to low power */ 700 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 701 } 702 mutex_exit(HUBD_MUTEX(hubd)); 703 } 704 705 706 /* 707 * given a child dip, locate its port number 708 */ 709 static usb_port_t 710 hubd_child_dip2port(hubd_t *hubd, dev_info_t *dip) 711 { 712 usb_port_t port; 713 714 mutex_enter(HUBD_MUTEX(hubd)); 715 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 716 if (hubd->h_children_dips[port] == dip) { 717 718 break; 719 } 720 } 721 ASSERT(port <= hubd->h_hub_descr.bNbrPorts); 722 mutex_exit(HUBD_MUTEX(hubd)); 723 724 return (port); 725 } 726 727 728 /* 729 * if the hub can be put into low power mode, return success 730 * NOTE: suspend here means going to lower power, not CPR suspend. 731 */ 732 static int 733 hubd_can_suspend(hubd_t *hubd) 734 { 735 hub_power_t *hubpm; 736 int total_power = 0; 737 usb_port_t port; 738 739 hubpm = hubd->h_hubpm; 740 741 if (DEVI_IS_DETACHING(hubd->h_dip)) { 742 743 return (USB_SUCCESS); 744 } 745 746 /* 747 * Don't go to lower power if haven't been at full power for enough 748 * time to let hotplug thread kickoff. 749 */ 750 if (ddi_get_time() < (hubpm->hubp_time_at_full_power + 751 hubpm->hubp_min_pm_threshold)) { 752 753 return (USB_FAILURE); 754 } 755 756 for (port = 1; (total_power == 0) && 757 (port <= hubd->h_hub_descr.bNbrPorts); port++) { 758 total_power += hubpm->hubp_child_pwrstate[port]; 759 } 760 761 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 762 "hubd_can_suspend: %d", total_power); 763 764 return (total_power ? USB_FAILURE : USB_SUCCESS); 765 } 766 767 768 /* 769 * resume port depending on current device state 770 */ 771 static int 772 hubd_resume_port(hubd_t *hubd, usb_port_t port) 773 { 774 int rval, retry; 775 usb_cr_t completion_reason; 776 usb_cb_flags_t cb_flags; 777 uint16_t status; 778 uint16_t change; 779 int retval = USB_FAILURE; 780 781 mutex_enter(HUBD_MUTEX(hubd)); 782 783 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 784 "hubd_resume_port: port=%d state=0x%x (%s)", port, 785 hubd->h_dev_state, usb_str_dev_state(hubd->h_dev_state)); 786 787 switch (hubd->h_dev_state) { 788 case USB_DEV_HUB_CHILD_PWRLVL: 789 /* 790 * This could be a bus ctl for a port other than the one 791 * that has a remote wakeup condition. So check. 792 */ 793 if ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0) { 794 /* the port isn't suspended, so don't resume */ 795 retval = USB_SUCCESS; 796 797 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 798 "hubd_resume_port: port=%d not suspended", port); 799 800 break; 801 } 802 /* 803 * Device has initiated a wakeup. 804 * Issue a ClearFeature(PortSuspend) 805 */ 806 mutex_exit(HUBD_MUTEX(hubd)); 807 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 808 hubd->h_default_pipe, 809 HUB_HANDLE_PORT_FEATURE_TYPE, 810 USB_REQ_CLEAR_FEATURE, 811 CFS_PORT_SUSPEND, 812 port, 813 0, NULL, 0, 814 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 815 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 816 "ClearFeature(PortSuspend) fails " 817 "rval=%d cr=%d cb=0x%x", rval, 818 completion_reason, cb_flags); 819 } 820 mutex_enter(HUBD_MUTEX(hubd)); 821 822 /* either way ack changes on the port */ 823 (void) hubd_determine_port_status(hubd, port, 824 &status, &change, PORT_CHANGE_PSSC); 825 retval = USB_SUCCESS; 826 827 break; 828 case USB_DEV_HUB_STATE_RECOVER: 829 /* 830 * When hubd's connect event callback posts a connect 831 * event to its child, it results in this busctl call 832 * which is valid 833 */ 834 /* FALLTHRU */ 835 case USB_DEV_ONLINE: 836 if (((hubd->h_port_state[port] & PORT_STATUS_CCS) == 0) || 837 ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0)) { 838 /* 839 * the port isn't suspended, or connected 840 * so don't resume 841 */ 842 retval = USB_SUCCESS; 843 844 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 845 "hubd_resume_port: port=%d not suspended", port); 846 847 break; 848 } 849 /* 850 * prevent kicking off the hotplug thread 851 */ 852 hubd->h_hotplug_thread++; 853 hubd_stop_polling(hubd); 854 855 /* Now ClearFeature(PortSuspend) */ 856 for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) { 857 mutex_exit(HUBD_MUTEX(hubd)); 858 rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 859 hubd->h_default_pipe, 860 HUB_HANDLE_PORT_FEATURE_TYPE, 861 USB_REQ_CLEAR_FEATURE, 862 CFS_PORT_SUSPEND, 863 port, 864 0, NULL, 0, 865 &completion_reason, &cb_flags, 0); 866 mutex_enter(HUBD_MUTEX(hubd)); 867 if (rval != USB_SUCCESS) { 868 USB_DPRINTF_L2(DPRINT_MASK_PM, 869 hubd->h_log_handle, 870 "ClearFeature(PortSuspend) fails" 871 "rval=%d cr=%d cb=0x%x", rval, 872 completion_reason, cb_flags); 873 } else { 874 /* 875 * As per spec section 11.9 and 7.1.7.7 876 * hub need to provide at least 20ms of 877 * resume signalling, and s/w provide 10ms of 878 * recovery time before accessing the port. 879 */ 880 mutex_exit(HUBD_MUTEX(hubd)); 881 delay(drv_usectohz(40000)); 882 mutex_enter(HUBD_MUTEX(hubd)); 883 (void) hubd_determine_port_status(hubd, port, 884 &status, &change, PORT_CHANGE_PSSC); 885 886 if ((status & PORT_STATUS_PSS) == 0) { 887 /* the port did finally resume */ 888 retval = USB_SUCCESS; 889 890 break; 891 } 892 } 893 } 894 895 /* allow hotplug thread again */ 896 hubd->h_hotplug_thread--; 897 hubd_start_polling(hubd, 0); 898 899 break; 900 case USB_DEV_DISCONNECTED: 901 /* Ignore - NO Operation */ 902 retval = USB_SUCCESS; 903 904 break; 905 case USB_DEV_SUSPENDED: 906 case USB_DEV_PWRED_DOWN: 907 default: 908 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 909 "Improper state for port Resume"); 910 911 break; 912 } 913 mutex_exit(HUBD_MUTEX(hubd)); 914 915 return (retval); 916 } 917 918 919 /* 920 * suspend port depending on device state 921 */ 922 static int 923 hubd_suspend_port(hubd_t *hubd, usb_port_t port) 924 { 925 int rval, retry; 926 int retval = USB_FAILURE; 927 usb_cr_t completion_reason; 928 usb_cb_flags_t cb_flags; 929 uint16_t status; 930 uint16_t change; 931 932 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 933 "hubd_suspend_port: port=%d", port); 934 935 mutex_enter(HUBD_MUTEX(hubd)); 936 937 switch (hubd->h_dev_state) { 938 case USB_DEV_HUB_STATE_RECOVER: 939 /* 940 * When hubd's connect event callback posts a connect 941 * event to its child, it results in this busctl call 942 * which is valid 943 */ 944 /* FALLTHRU */ 945 case USB_DEV_HUB_CHILD_PWRLVL: 946 /* 947 * When one child is resuming, the other could timeout 948 * and go to low power mode, which is valid 949 */ 950 /* FALLTHRU */ 951 case USB_DEV_ONLINE: 952 hubd->h_hotplug_thread++; 953 hubd_stop_polling(hubd); 954 955 /* 956 * Some devices start an unprovoked resume. According to spec, 957 * normal resume time for port is 10ms. Wait for double that 958 * time, then check to be sure port is really suspended. 959 */ 960 for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) { 961 /* Now SetFeature(PortSuspend) */ 962 mutex_exit(HUBD_MUTEX(hubd)); 963 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 964 hubd->h_default_pipe, 965 HUB_HANDLE_PORT_FEATURE_TYPE, 966 USB_REQ_SET_FEATURE, 967 CFS_PORT_SUSPEND, 968 port, 969 0, NULL, 0, 970 &completion_reason, &cb_flags, 0)) != 971 USB_SUCCESS) { 972 USB_DPRINTF_L2(DPRINT_MASK_PM, 973 hubd->h_log_handle, 974 "SetFeature(PortSuspend) fails" 975 "rval=%d cr=%d cb=0x%x", 976 rval, completion_reason, cb_flags); 977 } 978 979 /* 980 * some devices start an unprovoked resume 981 * wait and check port status after some time 982 */ 983 delay(drv_usectohz(20000)); 984 985 /* either ways ack changes on the port */ 986 mutex_enter(HUBD_MUTEX(hubd)); 987 (void) hubd_determine_port_status(hubd, port, 988 &status, &change, PORT_CHANGE_PSSC); 989 if (status & PORT_STATUS_PSS) { 990 /* the port is indeed suspended */ 991 retval = USB_SUCCESS; 992 993 break; 994 } 995 } 996 997 hubd->h_hotplug_thread--; 998 hubd_start_polling(hubd, 0); 999 1000 break; 1001 1002 case USB_DEV_DISCONNECTED: 1003 /* Ignore - No Operation */ 1004 retval = USB_SUCCESS; 1005 1006 break; 1007 1008 case USB_DEV_SUSPENDED: 1009 case USB_DEV_PWRED_DOWN: 1010 default: 1011 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 1012 "Improper state for port Suspend"); 1013 1014 break; 1015 } 1016 mutex_exit(HUBD_MUTEX(hubd)); 1017 1018 return (retval); 1019 } 1020 1021 1022 /* 1023 * child post attach/detach notifications 1024 */ 1025 static void 1026 hubd_post_attach(hubd_t *hubd, usb_port_t port, struct attachspec *as) 1027 { 1028 dev_info_t *dip; 1029 1030 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 1031 "hubd_post_attach: port=%d result=%d", 1032 port, as->result); 1033 1034 if (as->result == DDI_SUCCESS) { 1035 /* 1036 * Check if the child created wants to be power managed. 1037 * If yes, the childs power level gets automatically tracked 1038 * by DDI_CTLOPS_POWER busctl. 1039 * If no, we set power of the new child by default 1040 * to USB_DEV_OS_FULL_PWR. Because we should never suspend. 1041 */ 1042 mutex_enter(HUBD_MUTEX(hubd)); 1043 dip = hubd->h_children_dips[port]; 1044 mutex_exit(HUBD_MUTEX(hubd)); 1045 if (DEVI(dip)->devi_pm_info == NULL) { 1046 hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_FULL_PWR); 1047 } 1048 } 1049 } 1050 1051 1052 static void 1053 hubd_post_detach(hubd_t *hubd, usb_port_t port, struct detachspec *ds) 1054 { 1055 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 1056 "hubd_post_detach: port=%d result=%d", port, ds->result); 1057 1058 /* 1059 * if the device is successfully detached and is the 1060 * last device to detach, mark component as idle 1061 */ 1062 mutex_enter(HUBD_MUTEX(hubd)); 1063 if (ds->result == DDI_SUCCESS) { 1064 usba_device_t *usba_device = hubd->h_usba_devices[port]; 1065 dev_info_t *pdip = hubd->h_dip; 1066 mutex_exit(HUBD_MUTEX(hubd)); 1067 1068 usba_hubdi_incr_power_budget(pdip, usba_device); 1069 1070 /* 1071 * We set power of the detached child 1072 * to 0, so that we can suspend if all 1073 * our children are gone 1074 */ 1075 hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_PWR_OFF); 1076 1077 /* check for leaks on detaching */ 1078 if ((usba_device) && (ds->cmd == DDI_DETACH)) { 1079 usba_check_for_leaks(usba_device); 1080 } 1081 } else { 1082 mutex_exit(HUBD_MUTEX(hubd)); 1083 } 1084 } 1085 1086 1087 /* 1088 * hubd_post_power 1089 * After the child's power entry point has been called 1090 * we record its power level in our local struct. 1091 * If the device has powered off, we suspend port 1092 */ 1093 static int 1094 hubd_post_power(hubd_t *hubd, usb_port_t port, pm_bp_child_pwrchg_t *bpc, 1095 int result) 1096 { 1097 int retval = USB_SUCCESS; 1098 1099 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1100 "hubd_post_power: port=%d", port); 1101 1102 if (result == DDI_SUCCESS) { 1103 1104 /* record this power in our local struct */ 1105 hubd_set_child_pwrlvl(hubd, port, bpc->bpc_nlevel); 1106 1107 if (bpc->bpc_nlevel == USB_DEV_OS_PWR_OFF) { 1108 1109 /* now suspend the port */ 1110 retval = hubd_suspend_port(hubd, port); 1111 } else if (bpc->bpc_nlevel == USB_DEV_OS_FULL_PWR) { 1112 1113 /* make sure the port is resumed */ 1114 retval = hubd_resume_port(hubd, port); 1115 } 1116 } else { 1117 1118 /* record old power in our local struct */ 1119 hubd_set_child_pwrlvl(hubd, port, bpc->bpc_olevel); 1120 1121 if (bpc->bpc_olevel == USB_DEV_OS_PWR_OFF) { 1122 1123 /* 1124 * As this device failed to transition from 1125 * power off state, suspend the port again 1126 */ 1127 retval = hubd_suspend_port(hubd, port); 1128 } 1129 } 1130 1131 return (retval); 1132 } 1133 1134 1135 /* 1136 * bus ctl notifications are handled here, the rest goes up to root hub/hcd 1137 */ 1138 static int 1139 usba_hubdi_bus_ctl(dev_info_t *dip, 1140 dev_info_t *rdip, 1141 ddi_ctl_enum_t op, 1142 void *arg, 1143 void *result) 1144 { 1145 usba_device_t *hub_usba_device = usba_get_usba_device(rdip); 1146 dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip; 1147 struct attachspec *as; 1148 struct detachspec *ds; 1149 hubd_t *hubd; 1150 usb_port_t port; 1151 int circ, rval; 1152 int retval = DDI_FAILURE; 1153 1154 hubd = hubd_get_soft_state(dip); 1155 1156 mutex_enter(HUBD_MUTEX(hubd)); 1157 1158 /* flag that we are currently running bus_ctl */ 1159 hubd->h_bus_ctls++; 1160 mutex_exit(HUBD_MUTEX(hubd)); 1161 1162 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1163 "usba_hubdi_bus_ctl:\n\t" 1164 "dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p", 1165 (void *)dip, (void *)rdip, op, arg); 1166 1167 switch (op) { 1168 case DDI_CTLOPS_ATTACH: 1169 as = (struct attachspec *)arg; 1170 port = hubd_child_dip2port(hubd, rdip); 1171 1172 /* there is nothing to do at resume time */ 1173 if (as->cmd == DDI_RESUME) { 1174 break; 1175 } 1176 1177 /* serialize access */ 1178 ndi_devi_enter(hubd->h_dip, &circ); 1179 1180 switch (as->when) { 1181 case DDI_PRE: 1182 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1183 "DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d", 1184 (void *)rdip, port); 1185 1186 mutex_enter(HUBD_MUTEX(hubd)); 1187 hubd->h_port_state[port] |= HUBD_CHILD_ATTACHING; 1188 1189 /* Go busy here. Matching idle is DDI_POST case. */ 1190 (void) hubd_pm_busy_component(hubd, dip, 0); 1191 mutex_exit(HUBD_MUTEX(hubd)); 1192 1193 /* 1194 * if we suspended the port previously 1195 * because child went to low power state, and 1196 * someone unloaded the driver, the port would 1197 * still be suspended and needs to be resumed 1198 */ 1199 rval = hubd_resume_port(hubd, port); 1200 if (rval == USB_SUCCESS) { 1201 retval = DDI_SUCCESS; 1202 } 1203 1204 break; 1205 case DDI_POST: 1206 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1207 "DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d", 1208 (void *)rdip, port); 1209 1210 mutex_enter(HUBD_MUTEX(hubd)); 1211 hubd->h_port_state[port] &= ~HUBD_CHILD_ATTACHING; 1212 mutex_exit(HUBD_MUTEX(hubd)); 1213 1214 hubd_post_attach(hubd, port, (struct attachspec *)arg); 1215 retval = DDI_SUCCESS; 1216 mutex_enter(HUBD_MUTEX(hubd)); 1217 1218 /* Matching idle call for DDI_PRE busy call. */ 1219 (void) hubd_pm_idle_component(hubd, dip, 0); 1220 mutex_exit(HUBD_MUTEX(hubd)); 1221 } 1222 ndi_devi_exit(hubd->h_dip, circ); 1223 1224 break; 1225 case DDI_CTLOPS_DETACH: 1226 ds = (struct detachspec *)arg; 1227 port = hubd_child_dip2port(hubd, rdip); 1228 1229 /* there is nothing to do at suspend time */ 1230 if (ds->cmd == DDI_SUSPEND) { 1231 break; 1232 } 1233 1234 /* serialize access */ 1235 ndi_devi_enter(hubd->h_dip, &circ); 1236 1237 switch (ds->when) { 1238 case DDI_PRE: 1239 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1240 "DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d", 1241 (void *)rdip, port); 1242 1243 mutex_enter(HUBD_MUTEX(hubd)); 1244 hubd->h_port_state[port] |= HUBD_CHILD_DETACHING; 1245 1246 /* Go busy here. Matching idle is DDI_POST case. */ 1247 (void) hubd_pm_busy_component(hubd, dip, 0); 1248 1249 mutex_exit(HUBD_MUTEX(hubd)); 1250 retval = DDI_SUCCESS; 1251 1252 break; 1253 case DDI_POST: 1254 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1255 "DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d", 1256 (void *)rdip, port); 1257 1258 mutex_enter(HUBD_MUTEX(hubd)); 1259 hubd->h_port_state[port] &= ~HUBD_CHILD_DETACHING; 1260 mutex_exit(HUBD_MUTEX(hubd)); 1261 1262 /* Matching idle call for DDI_PRE busy call. */ 1263 hubd_post_detach(hubd, port, (struct detachspec *)arg); 1264 retval = DDI_SUCCESS; 1265 mutex_enter(HUBD_MUTEX(hubd)); 1266 (void) hubd_pm_idle_component(hubd, dip, 0); 1267 mutex_exit(HUBD_MUTEX(hubd)); 1268 1269 break; 1270 } 1271 ndi_devi_exit(hubd->h_dip, circ); 1272 1273 break; 1274 default: 1275 retval = usba_bus_ctl(root_hub_dip, rdip, op, arg, result); 1276 } 1277 1278 /* decrement bus_ctls count */ 1279 mutex_enter(HUBD_MUTEX(hubd)); 1280 hubd->h_bus_ctls--; 1281 ASSERT(hubd->h_bus_ctls >= 0); 1282 mutex_exit(HUBD_MUTEX(hubd)); 1283 1284 return (retval); 1285 } 1286 1287 /* 1288 * hubd_config_one: 1289 * enumerate one child according to 'port' 1290 */ 1291 1292 static boolean_t 1293 hubd_config_one(hubd_t *hubd, int port) 1294 { 1295 uint16_t status, change; 1296 dev_info_t *hdip = hubd->h_dip; 1297 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip; 1298 boolean_t online_child = B_FALSE, found = B_FALSE; 1299 int prh_circ, rh_circ, circ; 1300 1301 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 1302 "hubd_config_one: started, hubd_reset_port = 0x%x", port); 1303 1304 ndi_hold_devi(hdip); /* so we don't race with detach */ 1305 1306 /* 1307 * this ensures one config activity per system at a time. 1308 * we enter the parent PCI node to have this serialization. 1309 * this also excludes ioctls and deathrow thread 1310 */ 1311 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ); 1312 ndi_devi_enter(rh_dip, &rh_circ); 1313 1314 /* exclude other threads */ 1315 ndi_devi_enter(hdip, &circ); 1316 mutex_enter(HUBD_MUTEX(hubd)); 1317 1318 hubd_pm_busy_component(hubd, hubd->h_dip, 0); 1319 hubd_stop_polling(hubd); 1320 1321 1322 1323 if (!hubd->h_children_dips[port]) { 1324 1325 (void) hubd_determine_port_status(hubd, port, 1326 &status, &change, HUBD_ACK_ALL_CHANGES); 1327 1328 if (status & PORT_STATUS_CCS) { 1329 online_child |= (hubd_handle_port_connect(hubd, 1330 port) == USB_SUCCESS); 1331 found = online_child; 1332 } 1333 } else { 1334 found = B_TRUE; 1335 } 1336 1337 mutex_exit(HUBD_MUTEX(hubd)); 1338 1339 ndi_devi_exit(hdip, circ); 1340 ndi_devi_exit(rh_dip, rh_circ); 1341 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ); 1342 1343 if (online_child) { 1344 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 1345 "hubd_config_one: onlining child"); 1346 1347 (void) ndi_devi_online(hubd->h_dip, 0); 1348 } 1349 1350 mutex_enter(HUBD_MUTEX(hubd)); 1351 1352 hubd_start_polling(hubd, 0); 1353 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 1354 1355 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 1356 "hubd_config_one: exit"); 1357 1358 mutex_exit(HUBD_MUTEX(hubd)); 1359 1360 ndi_rele_devi(hdip); 1361 1362 return (found); 1363 } 1364 1365 /* 1366 * bus enumeration entry points 1367 */ 1368 static int 1369 hubd_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op, 1370 void *arg, dev_info_t **child) 1371 { 1372 hubd_t *hubd = hubd_get_soft_state(dip); 1373 int rval, circ; 1374 long port; 1375 1376 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1377 "hubd_bus_config: op=%d", op); 1378 1379 if (hubdi_bus_config_debug) { 1380 flag |= NDI_DEVI_DEBUG; 1381 } 1382 1383 if (op == BUS_CONFIG_ONE) { 1384 boolean_t found; 1385 char cname[80]; 1386 char *name, *addr; 1387 1388 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 1389 "hubd_bus_config: op=%d (BUS_CONFIG_ONE)", op); 1390 1391 (void) snprintf(cname, 80, "%s", (char *)arg); 1392 /* split name into "name@addr" parts */ 1393 i_ddi_parse_name(cname, &name, &addr, NULL); 1394 if (addr && *addr) { 1395 (void) ddi_strtol(addr, NULL, 16, &port); 1396 } else { 1397 return (NDI_FAILURE); 1398 } 1399 1400 found = hubd_config_one(hubd, port); 1401 1402 if (found == 0) { 1403 return (NDI_FAILURE); 1404 } 1405 1406 } 1407 ndi_devi_enter(hubd->h_dip, &circ); 1408 rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0); 1409 ndi_devi_exit(hubd->h_dip, circ); 1410 1411 return (rval); 1412 } 1413 1414 1415 static int 1416 hubd_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op, 1417 void *arg) 1418 { 1419 hubd_t *hubd = hubd_get_soft_state(dip); 1420 dev_info_t *cdip; 1421 usb_port_t port; 1422 int circ; 1423 int rval; 1424 1425 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1426 "hubd_bus_unconfig: op=%d", op); 1427 1428 if (hubdi_bus_config_debug) { 1429 flag |= NDI_DEVI_DEBUG; 1430 } 1431 1432 if ((op == BUS_UNCONFIG_ALL) && (flag & NDI_AUTODETACH) == 0) { 1433 flag |= NDI_DEVI_REMOVE; 1434 } 1435 1436 /* serialize access */ 1437 ndi_devi_enter(dip, &circ); 1438 1439 rval = ndi_busop_bus_unconfig(dip, flag, op, arg); 1440 1441 /* logically zap children's list */ 1442 mutex_enter(HUBD_MUTEX(hubd)); 1443 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 1444 hubd->h_port_state[port] |= HUBD_CHILD_ZAP; 1445 } 1446 mutex_exit(HUBD_MUTEX(hubd)); 1447 1448 /* fill in what's left */ 1449 for (cdip = ddi_get_child(dip); cdip; 1450 cdip = ddi_get_next_sibling(cdip)) { 1451 usba_device_t *usba_device = usba_get_usba_device(cdip); 1452 1453 if (usba_device == NULL) { 1454 1455 continue; 1456 } 1457 mutex_enter(HUBD_MUTEX(hubd)); 1458 port = usba_device->usb_port; 1459 hubd->h_children_dips[port] = cdip; 1460 hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP; 1461 mutex_exit(HUBD_MUTEX(hubd)); 1462 } 1463 1464 /* physically zap the children we didn't find */ 1465 mutex_enter(HUBD_MUTEX(hubd)); 1466 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 1467 if (hubd->h_port_state[port] & HUBD_CHILD_ZAP) { 1468 /* zap the dip and usba_device structure as well */ 1469 hubd_free_usba_device(hubd, hubd->h_usba_devices[port]); 1470 hubd->h_children_dips[port] = NULL; 1471 hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP; 1472 } 1473 } 1474 mutex_exit(HUBD_MUTEX(hubd)); 1475 1476 ndi_devi_exit(dip, circ); 1477 1478 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1479 "hubd_bus_unconfig: rval=%d", rval); 1480 1481 return (rval); 1482 } 1483 1484 1485 /* bus_power entry point */ 1486 static int 1487 hubd_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op, 1488 void *arg, void *result) 1489 { 1490 hubd_t *hubd; 1491 int rval, pwrup_res; 1492 usb_port_t port; 1493 int retval = DDI_FAILURE; 1494 pm_bp_child_pwrchg_t *bpc; 1495 pm_bp_nexus_pwrup_t bpn; 1496 1497 hubd = hubd_get_soft_state(dip); 1498 1499 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1500 "hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, " 1501 "result=%d\n", (void *)dip, impl_arg, op, arg, *(int *)result); 1502 1503 bpc = (pm_bp_child_pwrchg_t *)arg; 1504 1505 mutex_enter(HUBD_MUTEX(hubd)); 1506 hubd->h_bus_pwr++; 1507 mutex_exit(HUBD_MUTEX(hubd)); 1508 1509 switch (op) { 1510 case BUS_POWER_PRE_NOTIFICATION: 1511 port = hubd_child_dip2port(hubd, bpc->bpc_dip); 1512 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1513 "hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d", 1514 port); 1515 1516 /* go to full power if we are powered down */ 1517 mutex_enter(HUBD_MUTEX(hubd)); 1518 1519 /* 1520 * If this case completes normally, idle will be in 1521 * hubd_bus_power / BUS_POWER_POST_NOTIFICATION 1522 */ 1523 hubd_pm_busy_component(hubd, dip, 0); 1524 1525 /* 1526 * raise power only if we have created the components 1527 * and are currently in low power 1528 */ 1529 if ((hubd->h_dev_state == USB_DEV_PWRED_DOWN) && 1530 hubd->h_hubpm->hubp_wakeup_enabled) { 1531 mutex_exit(HUBD_MUTEX(hubd)); 1532 1533 bpn.bpn_comp = 0; 1534 bpn.bpn_dip = dip; 1535 bpn.bpn_level = USB_DEV_OS_FULL_PWR; 1536 bpn.bpn_private = bpc->bpc_private; 1537 1538 rval = pm_busop_bus_power(dip, impl_arg, 1539 BUS_POWER_NEXUS_PWRUP, (void *)&bpn, 1540 (void *)&pwrup_res); 1541 1542 if (rval != DDI_SUCCESS || pwrup_res != DDI_SUCCESS) { 1543 mutex_enter(HUBD_MUTEX(hubd)); 1544 hubd_pm_idle_component(hubd, dip, 0); 1545 mutex_exit(HUBD_MUTEX(hubd)); 1546 1547 break; 1548 } 1549 mutex_enter(HUBD_MUTEX(hubd)); 1550 } 1551 1552 /* indicate that child is changing power level */ 1553 hubd->h_port_state[port] |= HUBD_CHILD_PWRLVL_CHNG; 1554 mutex_exit(HUBD_MUTEX(hubd)); 1555 1556 if ((bpc->bpc_olevel == 0) && 1557 (bpc->bpc_nlevel > bpc->bpc_olevel)) { 1558 /* 1559 * this child is transitioning from power off 1560 * to power on state - resume port 1561 */ 1562 rval = hubd_resume_port(hubd, port); 1563 if (rval == USB_SUCCESS) { 1564 retval = DDI_SUCCESS; 1565 } else { 1566 /* reset this flag on failure */ 1567 mutex_enter(HUBD_MUTEX(hubd)); 1568 hubd->h_port_state[port] &= 1569 ~HUBD_CHILD_PWRLVL_CHNG; 1570 hubd_pm_idle_component(hubd, dip, 0); 1571 mutex_exit(HUBD_MUTEX(hubd)); 1572 } 1573 } else { 1574 retval = DDI_SUCCESS; 1575 } 1576 1577 break; 1578 case BUS_POWER_POST_NOTIFICATION: 1579 port = hubd_child_dip2port(hubd, bpc->bpc_dip); 1580 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1581 "hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d", 1582 port); 1583 1584 mutex_enter(HUBD_MUTEX(hubd)); 1585 hubd->h_port_state[port] &= ~HUBD_CHILD_PWRLVL_CHNG; 1586 mutex_exit(HUBD_MUTEX(hubd)); 1587 1588 /* record child's pwr and suspend port if required */ 1589 rval = hubd_post_power(hubd, port, bpc, *(int *)result); 1590 if (rval == USB_SUCCESS) { 1591 1592 retval = DDI_SUCCESS; 1593 } 1594 1595 mutex_enter(HUBD_MUTEX(hubd)); 1596 1597 /* 1598 * Matching idle for the busy in 1599 * hubd_bus_power / BUS_POWER_PRE_NOTIFICATION 1600 */ 1601 hubd_pm_idle_component(hubd, dip, 0); 1602 1603 mutex_exit(HUBD_MUTEX(hubd)); 1604 1605 break; 1606 default: 1607 retval = pm_busop_bus_power(dip, impl_arg, op, arg, result); 1608 1609 break; 1610 } 1611 1612 mutex_enter(HUBD_MUTEX(hubd)); 1613 hubd->h_bus_pwr--; 1614 mutex_exit(HUBD_MUTEX(hubd)); 1615 1616 return (retval); 1617 } 1618 1619 1620 /* 1621 * functions to handle power transition for OS levels 0 -> 3 1622 */ 1623 static int 1624 hubd_pwrlvl0(hubd_t *hubd) 1625 { 1626 hub_power_t *hubpm; 1627 1628 /* We can't power down if hotplug thread is running */ 1629 if (hubd->h_hotplug_thread || hubd->h_hubpm->hubp_busy_pm || 1630 (hubd_can_suspend(hubd) == USB_FAILURE)) { 1631 1632 return (USB_FAILURE); 1633 } 1634 1635 switch (hubd->h_dev_state) { 1636 case USB_DEV_ONLINE: 1637 hubpm = hubd->h_hubpm; 1638 1639 /* 1640 * To avoid race with bus_power pre_notify on check over 1641 * dev_state, we need to correctly set the dev state 1642 * before the mutex is dropped in stop polling. 1643 */ 1644 hubd->h_dev_state = USB_DEV_PWRED_DOWN; 1645 hubpm->hubp_current_power = USB_DEV_OS_PWR_OFF; 1646 1647 /* 1648 * if we are the root hub, do not stop polling 1649 * otherwise, we will never see a resume 1650 */ 1651 if (usba_is_root_hub(hubd->h_dip)) { 1652 /* place holder to implement Global Suspend */ 1653 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 1654 "Global Suspend: Not Yet Implemented"); 1655 } else { 1656 hubd_stop_polling(hubd); 1657 } 1658 1659 /* Issue USB D3 command to the device here */ 1660 (void) usb_set_device_pwrlvl3(hubd->h_dip); 1661 1662 break; 1663 case USB_DEV_DISCONNECTED: 1664 case USB_DEV_SUSPENDED: 1665 case USB_DEV_PWRED_DOWN: 1666 default: 1667 1668 break; 1669 } 1670 1671 return (USB_SUCCESS); 1672 } 1673 1674 1675 /* ARGSUSED */ 1676 static int 1677 hubd_pwrlvl1(hubd_t *hubd) 1678 { 1679 /* Issue USB D2 command to the device here */ 1680 (void) usb_set_device_pwrlvl2(hubd->h_dip); 1681 1682 return (USB_FAILURE); 1683 } 1684 1685 1686 /* ARGSUSED */ 1687 static int 1688 hubd_pwrlvl2(hubd_t *hubd) 1689 { 1690 /* Issue USB D1 command to the device here */ 1691 (void) usb_set_device_pwrlvl1(hubd->h_dip); 1692 1693 return (USB_FAILURE); 1694 } 1695 1696 1697 static int 1698 hubd_pwrlvl3(hubd_t *hubd) 1699 { 1700 hub_power_t *hubpm; 1701 int rval; 1702 1703 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, "hubd_pwrlvl3"); 1704 1705 hubpm = hubd->h_hubpm; 1706 switch (hubd->h_dev_state) { 1707 case USB_DEV_PWRED_DOWN: 1708 ASSERT(hubpm->hubp_current_power == USB_DEV_OS_PWR_OFF); 1709 if (usba_is_root_hub(hubd->h_dip)) { 1710 /* implement global resume here */ 1711 USB_DPRINTF_L2(DPRINT_MASK_PM, 1712 hubd->h_log_handle, 1713 "Global Resume: Not Yet Implemented"); 1714 } 1715 /* Issue USB D0 command to the device here */ 1716 rval = usb_set_device_pwrlvl0(hubd->h_dip); 1717 ASSERT(rval == USB_SUCCESS); 1718 hubd->h_dev_state = USB_DEV_ONLINE; 1719 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR; 1720 hubpm->hubp_time_at_full_power = ddi_get_time(); 1721 hubd_start_polling(hubd, 0); 1722 1723 /* FALLTHRU */ 1724 case USB_DEV_ONLINE: 1725 /* we are already in full power */ 1726 1727 /* FALLTHRU */ 1728 case USB_DEV_DISCONNECTED: 1729 case USB_DEV_SUSPENDED: 1730 /* 1731 * PM framework tries to put you in full power 1732 * during system shutdown. If we are disconnected 1733 * return success. Also, we should not change state 1734 * when we are disconnected or suspended or about to 1735 * transition to that state 1736 */ 1737 1738 return (USB_SUCCESS); 1739 default: 1740 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 1741 "hubd_pwrlvl3: Illegal dev_state=%d", hubd->h_dev_state); 1742 1743 return (USB_FAILURE); 1744 } 1745 } 1746 1747 1748 /* power entry point */ 1749 /* ARGSUSED */ 1750 int 1751 usba_hubdi_power(dev_info_t *dip, int comp, int level) 1752 { 1753 hubd_t *hubd; 1754 hub_power_t *hubpm; 1755 int retval; 1756 int circ; 1757 1758 hubd = hubd_get_soft_state(dip); 1759 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1760 "usba_hubdi_power: level=%d", level); 1761 1762 ndi_devi_enter(dip, &circ); 1763 1764 mutex_enter(HUBD_MUTEX(hubd)); 1765 hubpm = hubd->h_hubpm; 1766 1767 /* check if we are transitioning to a legal power level */ 1768 if (USB_DEV_PWRSTATE_OK(hubpm->hubp_pwr_states, level)) { 1769 USB_DPRINTF_L2(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1770 "usba_hubdi_power: illegal power level=%d " 1771 "hubp_pwr_states=0x%x", level, hubpm->hubp_pwr_states); 1772 mutex_exit(HUBD_MUTEX(hubd)); 1773 1774 ndi_devi_exit(dip, circ); 1775 1776 return (DDI_FAILURE); 1777 } 1778 1779 switch (level) { 1780 case USB_DEV_OS_PWR_OFF: 1781 retval = hubd_pwrlvl0(hubd); 1782 1783 break; 1784 case USB_DEV_OS_PWR_1: 1785 retval = hubd_pwrlvl1(hubd); 1786 1787 break; 1788 case USB_DEV_OS_PWR_2: 1789 retval = hubd_pwrlvl2(hubd); 1790 1791 break; 1792 case USB_DEV_OS_FULL_PWR: 1793 retval = hubd_pwrlvl3(hubd); 1794 1795 break; 1796 } 1797 mutex_exit(HUBD_MUTEX(hubd)); 1798 1799 ndi_devi_exit(dip, circ); 1800 1801 return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 1802 } 1803 1804 1805 /* power entry point for the root hub */ 1806 int 1807 usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level) 1808 { 1809 return (usba_hubdi_power(dip, comp, level)); 1810 } 1811 1812 1813 /* 1814 * standard driver entry points support code 1815 */ 1816 int 1817 usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1818 { 1819 int instance = ddi_get_instance(dip); 1820 hubd_t *hubd = NULL; 1821 int i, rval; 1822 int minor; 1823 uint8_t ports_count; 1824 char *log_name = NULL; 1825 const char *root_hub_drvname; 1826 usb_ep_data_t *ep_data; 1827 usba_device_t *child_ud = NULL; 1828 usb_dev_descr_t *usb_dev_descr; 1829 usb_port_status_t parent_port_status, child_port_status; 1830 1831 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle, 1832 "hubd_attach instance %d, cmd=0x%x", instance, cmd); 1833 1834 switch (cmd) { 1835 case DDI_ATTACH: 1836 1837 break; 1838 case DDI_RESUME: 1839 hubd_cpr_resume(dip); 1840 1841 return (DDI_SUCCESS); 1842 default: 1843 return (DDI_FAILURE); 1844 } 1845 1846 /* 1847 * Allocate softc information. 1848 */ 1849 if (usba_is_root_hub(dip)) { 1850 /* soft state has already been allocated */ 1851 hubd = hubd_get_soft_state(dip); 1852 minor = HUBD_IS_ROOT_HUB; 1853 1854 /* generate readable labels for different root hubs */ 1855 root_hub_drvname = ddi_driver_name(dip); 1856 if (strcmp(root_hub_drvname, "ehci") == 0) { 1857 log_name = "eusb"; 1858 } else if (strcmp(root_hub_drvname, "uhci") == 0) { 1859 log_name = "uusb"; 1860 } else { 1861 /* std. for ohci */ 1862 log_name = "usb"; 1863 } 1864 } else { 1865 rval = ddi_soft_state_zalloc(hubd_statep, instance); 1866 minor = 0; 1867 1868 if (rval != DDI_SUCCESS) { 1869 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 1870 "cannot allocate soft state (%d)", instance); 1871 goto fail; 1872 } 1873 1874 hubd = hubd_get_soft_state(dip); 1875 if (hubd == NULL) { 1876 goto fail; 1877 } 1878 } 1879 1880 hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel, 1881 &hubd_errmask, &hubd_instance_debug, 0); 1882 1883 hubd->h_usba_device = child_ud = usba_get_usba_device(dip); 1884 hubd->h_dip = dip; 1885 hubd->h_instance = instance; 1886 1887 mutex_enter(&child_ud->usb_mutex); 1888 child_port_status = child_ud->usb_port_status; 1889 usb_dev_descr = child_ud->usb_dev_descr; 1890 parent_port_status = (child_ud->usb_hs_hub_usba_dev) ? 1891 child_ud->usb_hs_hub_usba_dev->usb_port_status : 0; 1892 mutex_exit(&child_ud->usb_mutex); 1893 1894 if ((child_port_status == USBA_FULL_SPEED_DEV) && 1895 (parent_port_status == USBA_HIGH_SPEED_DEV) && 1896 (usb_dev_descr->bcdUSB == 0x100)) { 1897 USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle, 1898 "Use of a USB1.0 hub behind a high speed port may " 1899 "cause unexpected failures"); 1900 } 1901 1902 hubd->h_pipe_policy.pp_max_async_reqs = 1; 1903 1904 /* register with USBA as client driver */ 1905 if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) { 1906 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1907 "client attach failed"); 1908 1909 goto fail; 1910 } 1911 1912 if (usb_get_dev_data(dip, &hubd->h_dev_data, 1913 USB_PARSE_LVL_IF, 0) != USB_SUCCESS) { 1914 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1915 "cannot get dev_data"); 1916 1917 goto fail; 1918 } 1919 1920 if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data, 1921 hubd->h_dev_data->dev_curr_if, 0, 0, 1922 (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) { 1923 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1924 "no interrupt IN endpoint found"); 1925 1926 goto fail; 1927 } 1928 1929 hubd->h_ep1_descr = ep_data->ep_descr; 1930 hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph; 1931 1932 mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER, 1933 hubd->h_dev_data->dev_iblock_cookie); 1934 cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL); 1935 cv_init(&hubd->h_cv_hotplug_dev, NULL, CV_DRIVER, NULL); 1936 1937 hubd->h_init_state |= HUBD_LOCKS_DONE; 1938 1939 usb_free_descr_tree(dip, hubd->h_dev_data); 1940 1941 /* 1942 * register this hub instance with usba 1943 */ 1944 rval = usba_hubdi_register(dip, 0); 1945 if (rval != USB_SUCCESS) { 1946 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1947 "usba_hubdi_register failed"); 1948 goto fail; 1949 } 1950 1951 mutex_enter(HUBD_MUTEX(hubd)); 1952 hubd->h_init_state |= HUBD_HUBDI_REGISTERED; 1953 hubd->h_dev_state = USB_DEV_ONLINE; 1954 mutex_exit(HUBD_MUTEX(hubd)); 1955 1956 /* now create components to power manage this device */ 1957 hubd_create_pm_components(dip, hubd); 1958 1959 /* 1960 * Event handling: definition and registration 1961 * 1962 * first the definition: 1963 * get event handle 1964 */ 1965 (void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP); 1966 1967 /* bind event set to the handle */ 1968 if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events, 1969 NDI_SLEEP)) { 1970 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 1971 "binding event set failed"); 1972 1973 goto fail; 1974 } 1975 1976 /* event registration */ 1977 if (hubd_register_events(hubd) != USB_SUCCESS) { 1978 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1979 "hubd_register_events failed"); 1980 1981 goto fail; 1982 } 1983 1984 mutex_enter(HUBD_MUTEX(hubd)); 1985 hubd->h_init_state |= HUBD_EVENTS_REGISTERED; 1986 1987 if ((hubd_get_hub_descriptor(hubd)) != USB_SUCCESS) { 1988 mutex_exit(HUBD_MUTEX(hubd)); 1989 1990 goto fail; 1991 } 1992 1993 if (ddi_prop_exists(DDI_DEV_T_ANY, dip, 1994 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM), 1995 "hub-ignore-power-budget") == 1) { 1996 hubd->h_ignore_pwr_budget = B_TRUE; 1997 } else { 1998 hubd->h_ignore_pwr_budget = B_FALSE; 1999 2000 /* initialize hub power budget variables */ 2001 if (hubd_init_power_budget(hubd) != USB_SUCCESS) { 2002 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2003 "hubd_init_power_budget failed"); 2004 mutex_exit(HUBD_MUTEX(hubd)); 2005 2006 goto fail; 2007 } 2008 } 2009 2010 /* initialize and create children */ 2011 if (hubd_check_ports(hubd) != USB_SUCCESS) { 2012 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2013 "hubd_check_ports failed"); 2014 mutex_exit(HUBD_MUTEX(hubd)); 2015 2016 goto fail; 2017 } 2018 2019 /* 2020 * create cfgadm nodes 2021 */ 2022 hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP); 2023 hubd_get_ancestry_str(hubd); 2024 2025 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2026 "#ports=0x%x", hubd->h_hub_descr.bNbrPorts); 2027 2028 for (i = 1; i <= hubd->h_hub_descr.bNbrPorts; i++) { 2029 char ap_name[HUBD_APID_NAMELEN]; 2030 2031 (void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d", 2032 hubd->h_ancestry_str, i); 2033 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2034 "ap_name=%s", ap_name); 2035 2036 if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance, 2037 DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) { 2038 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2039 "cannot create attachment point node (%d)", 2040 instance); 2041 mutex_exit(HUBD_MUTEX(hubd)); 2042 2043 goto fail; 2044 } 2045 } 2046 2047 ports_count = hubd->h_hub_descr.bNbrPorts; 2048 mutex_exit(HUBD_MUTEX(hubd)); 2049 2050 /* create minor nodes */ 2051 if (ddi_create_minor_node(dip, "hubd", S_IFCHR, 2052 instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) { 2053 2054 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2055 "cannot create devctl minor node (%d)", instance); 2056 2057 goto fail; 2058 } 2059 2060 mutex_enter(HUBD_MUTEX(hubd)); 2061 hubd->h_init_state |= HUBD_MINOR_NODE_CREATED; 2062 mutex_exit(HUBD_MUTEX(hubd)); 2063 2064 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2065 "usb-port-count", ports_count) != DDI_PROP_SUCCESS) { 2066 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2067 "usb-port-count update failed"); 2068 } 2069 2070 /* 2071 * host controller driver has already reported this dev 2072 * if we are the root hub 2073 */ 2074 if (!usba_is_root_hub(dip)) { 2075 ddi_report_dev(dip); 2076 } 2077 2078 /* enable deathrow thread */ 2079 hubd->h_cleanup_enabled = B_TRUE; 2080 mutex_enter(HUBD_MUTEX(hubd)); 2081 hubd_pm_idle_component(hubd, dip, 0); 2082 mutex_exit(HUBD_MUTEX(hubd)); 2083 2084 return (DDI_SUCCESS); 2085 2086 fail: 2087 { 2088 char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2089 2090 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 2091 "cannot attach %s", ddi_pathname(dip, pathname)); 2092 2093 kmem_free(pathname, MAXPATHLEN); 2094 } 2095 2096 mutex_enter(HUBD_MUTEX(hubd)); 2097 hubd_pm_idle_component(hubd, dip, 0); 2098 mutex_exit(HUBD_MUTEX(hubd)); 2099 2100 if (hubd) { 2101 rval = hubd_cleanup(dip, hubd); 2102 if (rval != USB_SUCCESS) { 2103 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 2104 "failure to complete cleanup after attach failure"); 2105 } 2106 } 2107 2108 return (DDI_FAILURE); 2109 } 2110 2111 2112 int 2113 usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2114 { 2115 hubd_t *hubd = hubd_get_soft_state(dip); 2116 int rval; 2117 2118 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2119 "hubd_detach: cmd=0x%x", cmd); 2120 2121 switch (cmd) { 2122 case DDI_DETACH: 2123 rval = hubd_cleanup(dip, hubd); 2124 2125 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 2126 case DDI_SUSPEND: 2127 rval = hubd_cpr_suspend(hubd); 2128 2129 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 2130 default: 2131 return (DDI_FAILURE); 2132 } 2133 } 2134 2135 2136 /* 2137 * hubd_setdevaddr 2138 * set the device addrs on this port 2139 */ 2140 static int 2141 hubd_setdevaddr(hubd_t *hubd, usb_port_t port) 2142 { 2143 int rval; 2144 usb_cr_t completion_reason; 2145 usb_cb_flags_t cb_flags; 2146 usb_pipe_handle_t ph; 2147 dev_info_t *child_dip = NULL; 2148 uchar_t address = 0; 2149 usba_device_t *usba_device; 2150 int retry = 0; 2151 long time_delay; 2152 2153 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2154 "hubd_setdevaddr: port=%d", port); 2155 2156 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 2157 2158 child_dip = hubd->h_children_dips[port]; 2159 address = hubd->h_usba_devices[port]->usb_addr; 2160 usba_device = hubd->h_usba_devices[port]; 2161 2162 /* close the default pipe with addr x */ 2163 mutex_exit(HUBD_MUTEX(hubd)); 2164 ph = usba_get_dflt_pipe_handle(child_dip); 2165 usb_pipe_close(child_dip, ph, 2166 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 2167 mutex_enter(HUBD_MUTEX(hubd)); 2168 2169 /* 2170 * As this device has been reset, temporarily 2171 * assign the default address 2172 */ 2173 mutex_enter(&usba_device->usb_mutex); 2174 address = usba_device->usb_addr; 2175 usba_device->usb_addr = USBA_DEFAULT_ADDR; 2176 mutex_exit(&usba_device->usb_mutex); 2177 2178 mutex_exit(HUBD_MUTEX(hubd)); 2179 2180 time_delay = drv_usectohz(hubd_device_delay / 20); 2181 for (retry = 0; retry < hubd_retry_enumerate; retry++) { 2182 2183 /* open child's default pipe with USBA_DEFAULT_ADDR */ 2184 if (usb_pipe_open(child_dip, NULL, NULL, 2185 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != 2186 USB_SUCCESS) { 2187 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2188 "hubd_setdevaddr: Unable to open default pipe"); 2189 2190 break; 2191 } 2192 2193 /* Set the address of the device */ 2194 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 2195 USB_DEV_REQ_HOST_TO_DEV, 2196 USB_REQ_SET_ADDRESS, /* bRequest */ 2197 address, /* wValue */ 2198 0, /* wIndex */ 2199 0, /* wLength */ 2200 NULL, 0, 2201 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 2202 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2203 "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x", 2204 retry, rval, completion_reason, cb_flags); 2205 } 2206 2207 usb_pipe_close(child_dip, ph, 2208 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 2209 2210 if (rval == USB_SUCCESS) { 2211 2212 break; 2213 } 2214 2215 delay(time_delay); 2216 } 2217 2218 /* Reset to the old address */ 2219 mutex_enter(&usba_device->usb_mutex); 2220 usba_device->usb_addr = address; 2221 mutex_exit(&usba_device->usb_mutex); 2222 mutex_enter(HUBD_MUTEX(hubd)); 2223 2224 usba_clear_data_toggle(usba_device); 2225 2226 return (rval); 2227 } 2228 2229 2230 /* 2231 * hubd_setdevconfig 2232 * set the device addrs on this port 2233 */ 2234 static void 2235 hubd_setdevconfig(hubd_t *hubd, usb_port_t port) 2236 { 2237 int rval; 2238 usb_cr_t completion_reason; 2239 usb_cb_flags_t cb_flags; 2240 usb_pipe_handle_t ph; 2241 dev_info_t *child_dip = NULL; 2242 usba_device_t *usba_device = NULL; 2243 uint16_t config_value; 2244 2245 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2246 "hubd_setdevconfig: port=%d", port); 2247 2248 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 2249 2250 child_dip = hubd->h_children_dips[port]; 2251 usba_device = hubd->h_usba_devices[port]; 2252 config_value = hubd->h_usba_devices[port]->usb_cfg_value; 2253 mutex_exit(HUBD_MUTEX(hubd)); 2254 2255 /* open the default control pipe */ 2256 if ((rval = usb_pipe_open(child_dip, NULL, NULL, 2257 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) == 2258 USB_SUCCESS) { 2259 2260 /* Set the default configuration of the device */ 2261 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 2262 USB_DEV_REQ_HOST_TO_DEV, 2263 USB_REQ_SET_CFG, /* bRequest */ 2264 config_value, /* wValue */ 2265 0, /* wIndex */ 2266 0, /* wLength */ 2267 NULL, 0, 2268 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 2269 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2270 "hubd_setdevconfig: set device config failed: " 2271 "cr=%d cb_fl=0x%x rval=%d", 2272 completion_reason, cb_flags, rval); 2273 } 2274 /* 2275 * After setting the configuration, we make this default 2276 * control pipe persistent, so that it gets re-opened 2277 * on posting a connect event 2278 */ 2279 usba_persistent_pipe_close(usba_device); 2280 } else { 2281 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2282 "pipe open fails: rval=%d", rval); 2283 } 2284 mutex_enter(HUBD_MUTEX(hubd)); 2285 } 2286 2287 2288 /*ARGSUSED*/ 2289 static int 2290 hubd_check_disconnected_ports(dev_info_t *dip, void *arg) 2291 { 2292 int circ; 2293 usb_port_t port; 2294 hubd_t *hubd; 2295 major_t hub_major = ddi_name_to_major("hubd"); 2296 major_t hwahc_major = ddi_name_to_major("hwahc"); 2297 major_t usbmid_major = ddi_name_to_major("usb_mid"); 2298 2299 /* 2300 * make sure dip is a usb hub, major of root hub is HCD 2301 * major 2302 */ 2303 if (!usba_is_root_hub(dip)) { 2304 if (ddi_driver_major(dip) == usbmid_major) { 2305 /* 2306 * need to walk the children since it might be a 2307 * HWA device 2308 */ 2309 2310 return (DDI_WALK_CONTINUE); 2311 } 2312 2313 /* TODO: DWA device may also need special handling */ 2314 2315 if (((ddi_driver_major(dip) != hub_major) && 2316 (ddi_driver_major(dip) != hwahc_major)) || 2317 !i_ddi_devi_attached(dip)) { 2318 2319 return (DDI_WALK_PRUNECHILD); 2320 } 2321 } 2322 2323 hubd = hubd_get_soft_state(dip); 2324 if (hubd == NULL) { 2325 2326 return (DDI_WALK_PRUNECHILD); 2327 } 2328 2329 /* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */ 2330 ndi_devi_enter(dip, &circ); 2331 2332 if (ddi_driver_major(dip) != hwahc_major) { 2333 /* for normal usb hub or root hub */ 2334 mutex_enter(HUBD_MUTEX(hubd)); 2335 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 2336 dev_info_t *cdip = hubd->h_children_dips[port]; 2337 2338 if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) { 2339 2340 continue; 2341 } 2342 2343 (void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE, 2344 B_TRUE); 2345 } 2346 mutex_exit(HUBD_MUTEX(hubd)); 2347 } else { 2348 /* for HWA */ 2349 if (hubd->h_cleanup_child != NULL) { 2350 if (hubd->h_cleanup_child(dip) != USB_SUCCESS) { 2351 ndi_devi_exit(dip, circ); 2352 2353 return (DDI_WALK_PRUNECHILD); 2354 } 2355 } else { 2356 ndi_devi_exit(dip, circ); 2357 2358 return (DDI_WALK_PRUNECHILD); 2359 } 2360 } 2361 2362 ndi_devi_exit(dip, circ); 2363 2364 /* skip siblings of root hub */ 2365 if (usba_is_root_hub(dip)) { 2366 2367 return (DDI_WALK_PRUNESIB); 2368 } 2369 2370 return (DDI_WALK_CONTINUE); 2371 } 2372 2373 2374 /* 2375 * this thread will walk all children under the root hub for this 2376 * USB bus instance and attempt to remove them 2377 */ 2378 static void 2379 hubd_root_hub_cleanup_thread(void *arg) 2380 { 2381 int circ; 2382 hubd_t *root_hubd = (hubd_t *)arg; 2383 dev_info_t *rh_dip = root_hubd->h_dip; 2384 #ifndef __lock_lint 2385 callb_cpr_t cprinfo; 2386 2387 CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr, 2388 "USB root hub"); 2389 #endif 2390 2391 for (;;) { 2392 /* don't race with detach */ 2393 ndi_hold_devi(rh_dip); 2394 2395 mutex_enter(HUBD_MUTEX(root_hubd)); 2396 root_hubd->h_cleanup_needed = 0; 2397 mutex_exit(HUBD_MUTEX(root_hubd)); 2398 2399 (void) devfs_clean(rh_dip, NULL, 0); 2400 2401 ndi_devi_enter(ddi_get_parent(rh_dip), &circ); 2402 ddi_walk_devs(rh_dip, hubd_check_disconnected_ports, 2403 NULL); 2404 #ifdef __lock_lint 2405 (void) hubd_check_disconnected_ports(rh_dip, NULL); 2406 #endif 2407 ndi_devi_exit(ddi_get_parent(rh_dip), circ); 2408 2409 /* quit if we are not enabled anymore */ 2410 mutex_enter(HUBD_MUTEX(root_hubd)); 2411 if ((root_hubd->h_cleanup_enabled == B_FALSE) || 2412 (root_hubd->h_cleanup_needed == B_FALSE)) { 2413 root_hubd->h_cleanup_active = B_FALSE; 2414 mutex_exit(HUBD_MUTEX(root_hubd)); 2415 ndi_rele_devi(rh_dip); 2416 2417 break; 2418 } 2419 mutex_exit(HUBD_MUTEX(root_hubd)); 2420 ndi_rele_devi(rh_dip); 2421 2422 #ifndef __lock_lint 2423 mutex_enter(HUBD_MUTEX(root_hubd)); 2424 CALLB_CPR_SAFE_BEGIN(&cprinfo); 2425 mutex_exit(HUBD_MUTEX(root_hubd)); 2426 2427 delay(drv_usectohz(hubd_dip_cleanup_delay)); 2428 2429 mutex_enter(HUBD_MUTEX(root_hubd)); 2430 CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd)); 2431 mutex_exit(HUBD_MUTEX(root_hubd)); 2432 #endif 2433 } 2434 2435 #ifndef __lock_lint 2436 mutex_enter(HUBD_MUTEX(root_hubd)); 2437 CALLB_CPR_EXIT(&cprinfo); 2438 #endif 2439 } 2440 2441 2442 void 2443 hubd_schedule_cleanup(dev_info_t *rh_dip) 2444 { 2445 hubd_t *root_hubd; 2446 2447 /* 2448 * The usb_root_hub_dip pointer for the child hub of the WUSB 2449 * wire adapter class device points to the wire adapter, not 2450 * the root hub. Need to find the real root hub dip so that 2451 * the cleanup thread only starts from the root hub. 2452 */ 2453 while (!usba_is_root_hub(rh_dip)) { 2454 root_hubd = hubd_get_soft_state(rh_dip); 2455 if (root_hubd != NULL) { 2456 rh_dip = root_hubd->h_usba_device->usb_root_hub_dip; 2457 if (rh_dip == NULL) { 2458 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2459 root_hubd->h_log_handle, 2460 "hubd_schedule_cleanup: null rh dip"); 2461 2462 return; 2463 } 2464 } else { 2465 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2466 root_hubd->h_log_handle, 2467 "hubd_schedule_cleanup: cannot find root hub"); 2468 2469 return; 2470 } 2471 } 2472 root_hubd = hubd_get_soft_state(rh_dip); 2473 2474 mutex_enter(HUBD_MUTEX(root_hubd)); 2475 root_hubd->h_cleanup_needed = B_TRUE; 2476 if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) { 2477 root_hubd->h_cleanup_active = B_TRUE; 2478 mutex_exit(HUBD_MUTEX(root_hubd)); 2479 (void) thread_create(NULL, 0, 2480 hubd_root_hub_cleanup_thread, 2481 (void *)root_hubd, 0, &p0, TS_RUN, 2482 minclsyspri); 2483 } else { 2484 mutex_exit(HUBD_MUTEX(root_hubd)); 2485 } 2486 } 2487 2488 2489 /* 2490 * hubd_restore_device_state: 2491 * - set config for the hub 2492 * - power cycle all the ports 2493 * - for each port that was connected 2494 * - reset port 2495 * - assign addrs to the device on this port 2496 * - restart polling 2497 * - reset suspend flag 2498 */ 2499 static void 2500 hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd) 2501 { 2502 int rval; 2503 int retry; 2504 uint_t hub_prev_state; 2505 usb_port_t port; 2506 uint16_t status; 2507 uint16_t change; 2508 dev_info_t *ch_dip; 2509 boolean_t ehci_root_hub; 2510 2511 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2512 "hubd_restore_device_state:"); 2513 2514 mutex_enter(HUBD_MUTEX(hubd)); 2515 hub_prev_state = hubd->h_dev_state; 2516 ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN); 2517 2518 /* First bring the device to full power */ 2519 (void) hubd_pm_busy_component(hubd, dip, 0); 2520 mutex_exit(HUBD_MUTEX(hubd)); 2521 2522 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR); 2523 2524 if (!usba_is_root_hub(dip) && 2525 (usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0, 2526 DPRINT_MASK_HOTPLUG, 2527 USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) { 2528 2529 /* change the device state to disconnected */ 2530 mutex_enter(HUBD_MUTEX(hubd)); 2531 hubd->h_dev_state = USB_DEV_DISCONNECTED; 2532 (void) hubd_pm_idle_component(hubd, dip, 0); 2533 mutex_exit(HUBD_MUTEX(hubd)); 2534 2535 return; 2536 } 2537 2538 ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0); 2539 2540 mutex_enter(HUBD_MUTEX(hubd)); 2541 /* First turn off all port power */ 2542 rval = hubd_disable_all_port_power(hubd); 2543 if (rval != USB_SUCCESS) { 2544 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 2545 "hubd_restore_device_state:" 2546 "turning off port power failed"); 2547 } 2548 2549 /* Settling time before turning on again */ 2550 mutex_exit(HUBD_MUTEX(hubd)); 2551 delay(drv_usectohz(hubd_device_delay / 100)); 2552 mutex_enter(HUBD_MUTEX(hubd)); 2553 2554 /* enable power on all ports so we can see connects */ 2555 if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) { 2556 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2557 "hubd_restore_device_state: turn on port power failed"); 2558 2559 /* disable whatever was enabled */ 2560 (void) hubd_disable_all_port_power(hubd); 2561 2562 (void) hubd_pm_idle_component(hubd, dip, 0); 2563 mutex_exit(HUBD_MUTEX(hubd)); 2564 2565 return; 2566 } 2567 2568 /* 2569 * wait at least 3 frames before accessing devices 2570 * (note that delay's minimal time is one clock tick which 2571 * is 10ms unless hires_tick has been changed) 2572 */ 2573 mutex_exit(HUBD_MUTEX(hubd)); 2574 delay(drv_usectohz(10000)); 2575 mutex_enter(HUBD_MUTEX(hubd)); 2576 2577 hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER; 2578 2579 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 2580 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 2581 "hubd_restore_device_state: port=%d", port); 2582 2583 /* 2584 * the childen_dips list may have dips that have been 2585 * already deallocated. we only get a post_detach notification 2586 * but not a destroy notification 2587 */ 2588 ch_dip = hubd->h_children_dips[port]; 2589 if (ch_dip) { 2590 /* get port status */ 2591 (void) hubd_determine_port_status(hubd, port, 2592 &status, &change, PORT_CHANGE_CSC); 2593 2594 /* check if it is truly connected */ 2595 if (status & PORT_STATUS_CCS) { 2596 /* 2597 * Now reset port and assign the device 2598 * its original address 2599 */ 2600 retry = 0; 2601 do { 2602 (void) hubd_reset_port(hubd, port); 2603 2604 /* required for ppx */ 2605 (void) hubd_enable_port(hubd, port); 2606 2607 if (retry) { 2608 mutex_exit(HUBD_MUTEX(hubd)); 2609 delay(drv_usectohz( 2610 hubd_device_delay/2)); 2611 mutex_enter(HUBD_MUTEX(hubd)); 2612 } 2613 2614 rval = hubd_setdevaddr(hubd, port); 2615 retry++; 2616 } while ((rval != USB_SUCCESS) && 2617 (retry < hubd_retry_enumerate)); 2618 2619 hubd_setdevconfig(hubd, port); 2620 2621 if (hub_prev_state == USB_DEV_DISCONNECTED) { 2622 /* post a connect event */ 2623 mutex_exit(HUBD_MUTEX(hubd)); 2624 hubd_post_event(hubd, port, 2625 USBA_EVENT_TAG_HOT_INSERTION); 2626 mutex_enter(HUBD_MUTEX(hubd)); 2627 } else { 2628 /* 2629 * Since we have this device connected 2630 * mark it reinserted to prevent 2631 * cleanup thread from stepping in. 2632 */ 2633 mutex_exit(HUBD_MUTEX(hubd)); 2634 mutex_enter(&(DEVI(ch_dip)->devi_lock)); 2635 DEVI_SET_DEVICE_REINSERTED(ch_dip); 2636 mutex_exit(&(DEVI(ch_dip)->devi_lock)); 2637 2638 /* 2639 * reopen pipes for children for 2640 * their DDI_RESUME 2641 */ 2642 rval = usba_persistent_pipe_open( 2643 usba_get_usba_device(ch_dip)); 2644 mutex_enter(HUBD_MUTEX(hubd)); 2645 ASSERT(rval == USB_SUCCESS); 2646 } 2647 } else { 2648 /* 2649 * Mark this dip for deletion as the device 2650 * is not physically present, and schedule 2651 * cleanup thread upon post resume 2652 */ 2653 mutex_exit(HUBD_MUTEX(hubd)); 2654 2655 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2656 hubd->h_log_handle, 2657 "hubd_restore_device_state: " 2658 "dip=%p on port=%d marked for cleanup", 2659 (void *)ch_dip, port); 2660 mutex_enter(&(DEVI(ch_dip)->devi_lock)); 2661 DEVI_SET_DEVICE_REMOVED(ch_dip); 2662 mutex_exit(&(DEVI(ch_dip)->devi_lock)); 2663 2664 mutex_enter(HUBD_MUTEX(hubd)); 2665 } 2666 } else if (ehci_root_hub) { 2667 /* get port status */ 2668 (void) hubd_determine_port_status(hubd, port, 2669 &status, &change, PORT_CHANGE_CSC); 2670 2671 /* check if it is truly connected */ 2672 if (status & PORT_STATUS_CCS) { 2673 /* 2674 * reset the port to find out if we have 2675 * 2.0 device connected or 1.X. A 2.0 2676 * device will still be seen as connected, 2677 * while a 1.X device will switch over to 2678 * the companion controller. 2679 */ 2680 (void) hubd_reset_port(hubd, port); 2681 2682 (void) hubd_determine_port_status(hubd, port, 2683 &status, &change, PORT_CHANGE_CSC); 2684 2685 if (status & 2686 (PORT_STATUS_CCS | PORT_STATUS_HSDA)) { 2687 /* 2688 * We have a USB 2.0 device 2689 * connected. Power cycle this port 2690 * so that hotplug thread can 2691 * enumerate this device. 2692 */ 2693 (void) hubd_toggle_port(hubd, port); 2694 } else { 2695 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2696 hubd->h_log_handle, 2697 "hubd_restore_device_state: " 2698 "device on port %d switched over", 2699 port); 2700 } 2701 } 2702 2703 } 2704 } 2705 2706 2707 /* if the device had remote wakeup earlier, enable it again */ 2708 if (hubd->h_hubpm->hubp_wakeup_enabled) { 2709 mutex_exit(HUBD_MUTEX(hubd)); 2710 (void) usb_handle_remote_wakeup(hubd->h_dip, 2711 USB_REMOTE_WAKEUP_ENABLE); 2712 mutex_enter(HUBD_MUTEX(hubd)); 2713 } 2714 2715 hubd->h_dev_state = USB_DEV_ONLINE; 2716 hubd_start_polling(hubd, 0); 2717 (void) hubd_pm_idle_component(hubd, dip, 0); 2718 mutex_exit(HUBD_MUTEX(hubd)); 2719 } 2720 2721 2722 /* 2723 * hubd_cleanup: 2724 * cleanup hubd and deallocate. this function is called for 2725 * handling attach failures and detaching including dynamic 2726 * reconfiguration. If called from attaching, it must clean 2727 * up the whole thing and return success. 2728 */ 2729 /*ARGSUSED*/ 2730 static int 2731 hubd_cleanup(dev_info_t *dip, hubd_t *hubd) 2732 { 2733 int circ, rval, old_dev_state; 2734 hub_power_t *hubpm; 2735 #ifdef DEBUG 2736 usb_port_t port; 2737 #endif 2738 2739 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2740 "hubd_cleanup:"); 2741 2742 if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) { 2743 goto done; 2744 } 2745 2746 /* ensure we are the only one active */ 2747 ndi_devi_enter(dip, &circ); 2748 2749 mutex_enter(HUBD_MUTEX(hubd)); 2750 2751 /* Cleanup failure is only allowed if called from detach */ 2752 if (DEVI_IS_DETACHING(dip)) { 2753 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip; 2754 2755 /* 2756 * We are being called from detach. 2757 * Fail immediately if the hotplug thread is running 2758 * else set the dev_state to disconnected so that 2759 * hotplug thread just exits without doing anything. 2760 */ 2761 if (hubd->h_bus_ctls || hubd->h_bus_pwr || 2762 hubd->h_hotplug_thread) { 2763 mutex_exit(HUBD_MUTEX(hubd)); 2764 ndi_devi_exit(dip, circ); 2765 2766 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2767 "hubd_cleanup: hotplug thread/bus ctl active " 2768 "- failing detach"); 2769 2770 return (USB_FAILURE); 2771 } 2772 2773 /* 2774 * if the deathrow thread is still active or about 2775 * to become active, fail detach 2776 * the roothup can only be detached if nexus drivers 2777 * are unloaded or explicitly offlined 2778 */ 2779 if (rh_dip == dip) { 2780 if (hubd->h_cleanup_needed || 2781 hubd->h_cleanup_active) { 2782 mutex_exit(HUBD_MUTEX(hubd)); 2783 ndi_devi_exit(dip, circ); 2784 2785 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2786 hubd->h_log_handle, 2787 "hubd_cleanup: deathrow still active?" 2788 "- failing detach"); 2789 2790 return (USB_FAILURE); 2791 } 2792 } 2793 } 2794 2795 old_dev_state = hubd->h_dev_state; 2796 hubd->h_dev_state = USB_DEV_DISCONNECTED; 2797 2798 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2799 "hubd_cleanup: stop polling"); 2800 hubd_close_intr_pipe(hubd); 2801 2802 ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr || 2803 hubd->h_hotplug_thread) == 0); 2804 mutex_exit(HUBD_MUTEX(hubd)); 2805 2806 /* 2807 * deallocate events, if events are still registered 2808 * (ie. children still attached) then we have to fail the detach 2809 */ 2810 if (hubd->h_ndi_event_hdl) { 2811 2812 rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl); 2813 if (DEVI_IS_ATTACHING(dip)) { 2814 2815 /* It must return success if attaching. */ 2816 ASSERT(rval == NDI_SUCCESS); 2817 2818 } else if (rval != NDI_SUCCESS) { 2819 2820 USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle, 2821 "hubd_cleanup: ndi_event_free_hdl failed"); 2822 ndi_devi_exit(dip, circ); 2823 2824 return (USB_FAILURE); 2825 2826 } 2827 } 2828 2829 mutex_enter(HUBD_MUTEX(hubd)); 2830 2831 if (hubd->h_init_state & HUBD_CHILDREN_CREATED) { 2832 #ifdef DEBUG 2833 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 2834 ASSERT(hubd->h_usba_devices[port] == NULL); 2835 ASSERT(hubd->h_children_dips[port] == NULL); 2836 } 2837 #endif 2838 kmem_free(hubd->h_children_dips, hubd->h_cd_list_length); 2839 kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length); 2840 } 2841 2842 /* 2843 * Disable the event callbacks first, after this point, event 2844 * callbacks will never get called. Note we shouldn't hold 2845 * mutex while unregistering events because there may be a 2846 * competing event callback thread. Event callbacks are done 2847 * with ndi mutex held and this can cause a potential deadlock. 2848 * Note that cleanup can't fail after deregistration of events. 2849 */ 2850 if (hubd->h_init_state & HUBD_EVENTS_REGISTERED) { 2851 mutex_exit(HUBD_MUTEX(hubd)); 2852 usb_unregister_event_cbs(dip, &hubd_events); 2853 hubd_unregister_cpr_callback(hubd); 2854 mutex_enter(HUBD_MUTEX(hubd)); 2855 } 2856 2857 /* restore the old dev state so that device can be put into low power */ 2858 hubd->h_dev_state = old_dev_state; 2859 hubpm = hubd->h_hubpm; 2860 2861 if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) { 2862 (void) hubd_pm_busy_component(hubd, dip, 0); 2863 mutex_exit(HUBD_MUTEX(hubd)); 2864 if (hubd->h_hubpm->hubp_wakeup_enabled) { 2865 /* 2866 * Bring the hub to full power before 2867 * issuing the disable remote wakeup command 2868 */ 2869 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR); 2870 2871 if ((rval = usb_handle_remote_wakeup(hubd->h_dip, 2872 USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) { 2873 USB_DPRINTF_L2(DPRINT_MASK_PM, 2874 hubd->h_log_handle, 2875 "hubd_cleanup: disable remote wakeup " 2876 "fails=%d", rval); 2877 } 2878 } 2879 2880 (void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF); 2881 2882 mutex_enter(HUBD_MUTEX(hubd)); 2883 (void) hubd_pm_idle_component(hubd, dip, 0); 2884 } 2885 2886 if (hubpm) { 2887 if (hubpm->hubp_child_pwrstate) { 2888 kmem_free(hubpm->hubp_child_pwrstate, 2889 MAX_PORTS + 1); 2890 } 2891 kmem_free(hubpm, sizeof (hub_power_t)); 2892 } 2893 mutex_exit(HUBD_MUTEX(hubd)); 2894 2895 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2896 "hubd_cleanup: freeing space"); 2897 2898 if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) { 2899 rval = usba_hubdi_unregister(dip); 2900 ASSERT(rval == USB_SUCCESS); 2901 } 2902 2903 if (hubd->h_init_state & HUBD_LOCKS_DONE) { 2904 mutex_destroy(HUBD_MUTEX(hubd)); 2905 cv_destroy(&hubd->h_cv_reset_port); 2906 cv_destroy(&hubd->h_cv_hotplug_dev); 2907 } 2908 2909 ndi_devi_exit(dip, circ); 2910 2911 if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) { 2912 ddi_remove_minor_node(dip, NULL); 2913 } 2914 2915 if (usba_is_root_hub(dip)) { 2916 usb_pipe_close(dip, hubd->h_default_pipe, 2917 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 2918 } 2919 2920 done: 2921 if (hubd->h_ancestry_str) { 2922 kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN); 2923 } 2924 2925 usb_client_detach(dip, hubd->h_dev_data); 2926 2927 usb_free_log_hdl(hubd->h_log_handle); 2928 2929 if (!usba_is_root_hub(dip)) { 2930 ddi_soft_state_free(hubd_statep, ddi_get_instance(dip)); 2931 } 2932 2933 ddi_prop_remove_all(dip); 2934 2935 return (USB_SUCCESS); 2936 } 2937 2938 2939 /* 2940 * hubd_determine_port_connection: 2941 * Determine which port is in connect status but does not 2942 * have connect status change bit set, and mark port change 2943 * bit accordingly. 2944 * This function is applied during hub attach time. 2945 */ 2946 static usb_port_mask_t 2947 hubd_determine_port_connection(hubd_t *hubd) 2948 { 2949 usb_port_t port; 2950 usb_hub_descr_t *hub_descr; 2951 uint16_t status; 2952 uint16_t change; 2953 usb_port_mask_t port_change = 0; 2954 2955 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 2956 2957 hub_descr = &hubd->h_hub_descr; 2958 2959 for (port = 1; port <= hub_descr->bNbrPorts; port++) { 2960 2961 (void) hubd_determine_port_status(hubd, port, &status, 2962 &change, 0); 2963 2964 /* Check if port is in connect status */ 2965 if (!(status & PORT_STATUS_CCS)) { 2966 2967 continue; 2968 } 2969 2970 /* 2971 * Check if port Connect Status Change bit has been set. 2972 * If already set, the connection will be handled by 2973 * intr polling callback, not during attach. 2974 */ 2975 if (change & PORT_CHANGE_CSC) { 2976 2977 continue; 2978 } 2979 2980 port_change |= 1 << port; 2981 } 2982 2983 return (port_change); 2984 } 2985 2986 2987 /* 2988 * hubd_check_ports: 2989 * - get hub descriptor 2990 * - check initial port status 2991 * - enable power on all ports 2992 * - enable polling on ep1 2993 */ 2994 static int 2995 hubd_check_ports(hubd_t *hubd) 2996 { 2997 int rval; 2998 usb_port_mask_t port_change = 0; 2999 hubd_hotplug_arg_t *arg; 3000 3001 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 3002 3003 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 3004 "hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip)); 3005 3006 /* 3007 * First turn off all port power 3008 */ 3009 if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) { 3010 3011 /* disable whatever was enabled */ 3012 (void) hubd_disable_all_port_power(hubd); 3013 3014 return (rval); 3015 } 3016 3017 /* 3018 * do not switch on immediately (instantly on root hub) 3019 * and allow time to settle 3020 */ 3021 mutex_exit(HUBD_MUTEX(hubd)); 3022 delay(drv_usectohz(10000)); 3023 mutex_enter(HUBD_MUTEX(hubd)); 3024 3025 /* 3026 * enable power on all ports so we can see connects 3027 */ 3028 if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) { 3029 /* disable whatever was enabled */ 3030 (void) hubd_disable_all_port_power(hubd); 3031 3032 return (rval); 3033 } 3034 3035 /* wait at least 3 frames before accessing devices */ 3036 mutex_exit(HUBD_MUTEX(hubd)); 3037 delay(drv_usectohz(10000)); 3038 mutex_enter(HUBD_MUTEX(hubd)); 3039 3040 /* 3041 * allocate arrays for saving the dips of each child per port 3042 * 3043 * ports go from 1 - n, allocate 1 more entry 3044 */ 3045 hubd->h_cd_list_length = 3046 (sizeof (dev_info_t **)) * (hubd->h_hub_descr.bNbrPorts + 1); 3047 3048 hubd->h_children_dips = (dev_info_t **)kmem_zalloc( 3049 hubd->h_cd_list_length, KM_SLEEP); 3050 hubd->h_usba_devices = (usba_device_t **)kmem_zalloc( 3051 hubd->h_cd_list_length, KM_SLEEP); 3052 3053 hubd->h_init_state |= HUBD_CHILDREN_CREATED; 3054 3055 mutex_exit(HUBD_MUTEX(hubd)); 3056 arg = (hubd_hotplug_arg_t *)kmem_zalloc( 3057 sizeof (hubd_hotplug_arg_t), KM_SLEEP); 3058 mutex_enter(HUBD_MUTEX(hubd)); 3059 3060 if ((rval = hubd_open_intr_pipe(hubd)) != USB_SUCCESS) { 3061 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3062 3063 return (rval); 3064 } 3065 3066 hubd_start_polling(hubd, 0); 3067 3068 /* 3069 * Some hub devices, like the embedded hub in the CKS ErgoMagic 3070 * keyboard, may only have connection status bit set, but not 3071 * have connect status change bit set when a device has been 3072 * connected to its downstream port before the hub is enumerated. 3073 * Then when the hub is in enumeration, the devices connected to 3074 * it cannot be detected by the intr pipe and won't be enumerated. 3075 * We need to check such situation here and enumerate the downstream 3076 * devices for such hubs. 3077 */ 3078 port_change = hubd_determine_port_connection(hubd); 3079 3080 if (port_change) { 3081 hubd_pm_busy_component(hubd, hubd->h_dip, 0); 3082 3083 arg->hubd = hubd; 3084 arg->hotplug_during_attach = B_TRUE; 3085 hubd->h_port_change |= port_change; 3086 3087 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 3088 "hubd_check_ports: port change=0x%x, need to connect", 3089 hubd->h_port_change); 3090 3091 if (usb_async_req(hubd->h_dip, hubd_hotplug_thread, 3092 (void *)arg, 0) == USB_SUCCESS) { 3093 hubd->h_hotplug_thread++; 3094 } else { 3095 /* mark this device as idle */ 3096 hubd_pm_idle_component(hubd, hubd->h_dip, 0); 3097 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3098 } 3099 } else { 3100 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3101 } 3102 3103 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 3104 "hubd_check_ports done"); 3105 3106 return (USB_SUCCESS); 3107 } 3108 3109 3110 /* 3111 * hubd_get_hub_descriptor: 3112 */ 3113 static int 3114 hubd_get_hub_descriptor(hubd_t *hubd) 3115 { 3116 usb_hub_descr_t *hub_descr = &hubd->h_hub_descr; 3117 mblk_t *data = NULL; 3118 usb_cr_t completion_reason; 3119 usb_cb_flags_t cb_flags; 3120 uint16_t length; 3121 int rval; 3122 3123 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3124 "hubd_get_hub_descriptor:"); 3125 3126 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 3127 ASSERT(hubd->h_default_pipe != 0); 3128 3129 /* get hub descriptor length first by requesting 8 bytes only */ 3130 mutex_exit(HUBD_MUTEX(hubd)); 3131 3132 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 3133 hubd->h_default_pipe, 3134 HUB_CLASS_REQ_TYPE, 3135 USB_REQ_GET_DESCR, /* bRequest */ 3136 USB_DESCR_TYPE_SETUP_HUB, /* wValue */ 3137 0, /* wIndex */ 3138 8, /* wLength */ 3139 &data, 0, 3140 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 3141 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 3142 "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d", 3143 completion_reason, cb_flags, rval); 3144 freemsg(data); 3145 mutex_enter(HUBD_MUTEX(hubd)); 3146 3147 return (rval); 3148 } 3149 3150 length = *(data->b_rptr); 3151 3152 if (length > 8) { 3153 freemsg(data); 3154 data = NULL; 3155 3156 /* get complete hub descriptor */ 3157 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 3158 hubd->h_default_pipe, 3159 HUB_CLASS_REQ_TYPE, 3160 USB_REQ_GET_DESCR, /* bRequest */ 3161 USB_DESCR_TYPE_SETUP_HUB, /* wValue */ 3162 0, /* wIndex */ 3163 length, /* wLength */ 3164 &data, 0, 3165 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 3166 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 3167 "get hub descriptor failed: " 3168 "cr=%d cb_fl=0x%x rval=%d", 3169 completion_reason, cb_flags, rval); 3170 freemsg(data); 3171 mutex_enter(HUBD_MUTEX(hubd)); 3172 3173 return (rval); 3174 } 3175 } 3176 3177 mutex_enter(HUBD_MUTEX(hubd)); 3178 3179 /* parse the hub descriptor */ 3180 /* only 32 ports are supported at present */ 3181 ASSERT(*(data->b_rptr + 2) <= 32); 3182 if (usb_parse_CV_descr("cccscccccc", 3183 data->b_rptr, MBLKL(data), 3184 (void *)hub_descr, sizeof (usb_hub_descr_t)) == 0) { 3185 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 3186 "parsing hub descriptor failed"); 3187 3188 freemsg(data); 3189 3190 return (USB_FAILURE); 3191 } 3192 3193 freemsg(data); 3194 3195 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 3196 "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x " 3197 "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval, 3198 hub_descr->bNbrPorts, hub_descr->wHubCharacteristics, 3199 hub_descr->bPwrOn2PwrGood, hub_descr->bHubContrCurrent); 3200 3201 if (hub_descr->bNbrPorts > MAX_PORTS) { 3202 USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle, 3203 "Hub driver supports max of %d ports on hub. " 3204 "Hence using the first %d port of %d ports available", 3205 MAX_PORTS, MAX_PORTS, hub_descr->bNbrPorts); 3206 3207 hub_descr->bNbrPorts = MAX_PORTS; 3208 } 3209 3210 return (USB_SUCCESS); 3211 } 3212 3213 3214 /* 3215 * hubd_get_hub_status_words: 3216 */ 3217 static int 3218 hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status) 3219 { 3220 usb_cr_t completion_reason; 3221 usb_cb_flags_t cb_flags; 3222 mblk_t *data = NULL; 3223 3224 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 3225 3226 mutex_exit(HUBD_MUTEX(hubd)); 3227 3228 if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe, 3229 HUB_CLASS_REQ_TYPE, 3230 USB_REQ_GET_STATUS, 3231 0, 3232 0, 3233 GET_STATUS_LENGTH, 3234 &data, 0, 3235 &completion_reason, &cb_flags, 0) != USB_SUCCESS) { 3236 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle, 3237 "get hub status failed: cr=%d cb=0x%x", 3238 completion_reason, cb_flags); 3239 3240 if (data) { 3241 freemsg(data); 3242 } 3243 3244 mutex_enter(HUBD_MUTEX(hubd)); 3245 3246 return (USB_FAILURE); 3247 } 3248 3249 mutex_enter(HUBD_MUTEX(hubd)); 3250 3251 status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr); 3252 status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2); 3253 3254 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle, 3255 "hub status=0x%x change=0x%x", status[0], status[1]); 3256 3257 freemsg(data); 3258 3259 return (USB_SUCCESS); 3260 } 3261 3262 3263 /* 3264 * hubd_open_intr_pipe: 3265 * we read all descriptors first for curiosity and then simply 3266 * open the pipe 3267 */ 3268 static int 3269 hubd_open_intr_pipe(hubd_t *hubd) 3270 { 3271 int rval; 3272 3273 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3274 "hubd_open_intr_pipe:"); 3275 3276 ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE); 3277 3278 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING; 3279 mutex_exit(HUBD_MUTEX(hubd)); 3280 3281 if ((rval = usb_pipe_open(hubd->h_dip, 3282 &hubd->h_ep1_descr, &hubd->h_pipe_policy, 3283 0, &hubd->h_ep1_ph)) != USB_SUCCESS) { 3284 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle, 3285 "open intr pipe failed (%d)", rval); 3286 3287 mutex_enter(HUBD_MUTEX(hubd)); 3288 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE; 3289 3290 return (rval); 3291 } 3292 3293 mutex_enter(HUBD_MUTEX(hubd)); 3294 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE; 3295 3296 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3297 "open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph); 3298 3299 return (USB_SUCCESS); 3300 } 3301 3302 3303 /* 3304 * hubd_start_polling: 3305 * start or restart the polling 3306 */ 3307 static void 3308 hubd_start_polling(hubd_t *hubd, int always) 3309 { 3310 usb_intr_req_t *reqp; 3311 int rval; 3312 usb_pipe_state_t pipe_state; 3313 3314 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3315 "start polling: always=%d dev_state=%d pipe_state=%d\n\t" 3316 "thread=%d ep1_ph=0x%p", 3317 always, hubd->h_dev_state, hubd->h_intr_pipe_state, 3318 hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph); 3319 3320 /* 3321 * start or restart polling on the intr pipe 3322 * only if hotplug thread is not running 3323 */ 3324 if ((always == HUBD_ALWAYS_START_POLLING) || 3325 ((hubd->h_dev_state == USB_DEV_ONLINE) && 3326 (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) && 3327 (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) { 3328 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3329 "start polling requested"); 3330 3331 reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP); 3332 3333 reqp->intr_client_private = (usb_opaque_t)hubd; 3334 reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK | 3335 USB_ATTRS_AUTOCLEARING; 3336 reqp->intr_len = hubd->h_ep1_descr.wMaxPacketSize; 3337 reqp->intr_cb = hubd_read_cb; 3338 reqp->intr_exc_cb = hubd_exception_cb; 3339 mutex_exit(HUBD_MUTEX(hubd)); 3340 if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp, 3341 USB_FLAGS_SLEEP)) != USB_SUCCESS) { 3342 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle, 3343 "start polling failed, rval=%d", rval); 3344 usb_free_intr_req(reqp); 3345 } 3346 3347 rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state, 3348 USB_FLAGS_SLEEP); 3349 if (pipe_state != USB_PIPE_STATE_ACTIVE) { 3350 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 3351 "intr pipe state=%d, rval=%d", pipe_state, rval); 3352 } 3353 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3354 "start polling request 0x%p", (void *)reqp); 3355 3356 mutex_enter(HUBD_MUTEX(hubd)); 3357 } 3358 } 3359 3360 3361 /* 3362 * hubd_stop_polling 3363 * stop polling but do not close the pipe 3364 */ 3365 static void 3366 hubd_stop_polling(hubd_t *hubd) 3367 { 3368 int rval; 3369 usb_pipe_state_t pipe_state; 3370 3371 if (hubd->h_ep1_ph) { 3372 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 3373 "hubd_stop_polling:"); 3374 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_STOPPED; 3375 mutex_exit(HUBD_MUTEX(hubd)); 3376 3377 usb_pipe_stop_intr_polling(hubd->h_ep1_ph, USB_FLAGS_SLEEP); 3378 rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state, 3379 USB_FLAGS_SLEEP); 3380 3381 if (pipe_state != USB_PIPE_STATE_IDLE) { 3382 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 3383 "intr pipe state=%d, rval=%d", pipe_state, rval); 3384 } 3385 mutex_enter(HUBD_MUTEX(hubd)); 3386 if (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_STOPPED) { 3387 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE; 3388 } 3389 } 3390 } 3391 3392 3393 /* 3394 * hubd_close_intr_pipe: 3395 * close the pipe (which also stops the polling 3396 * and wait for the hotplug thread to exit 3397 */ 3398 static void 3399 hubd_close_intr_pipe(hubd_t *hubd) 3400 { 3401 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3402 "hubd_close_intr_pipe:"); 3403 3404 /* 3405 * Now that no async operation is outstanding on pipe, 3406 * we can change the state to HUBD_INTR_PIPE_CLOSING 3407 */ 3408 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_CLOSING; 3409 3410 ASSERT(hubd->h_hotplug_thread == 0); 3411 3412 if (hubd->h_ep1_ph) { 3413 mutex_exit(HUBD_MUTEX(hubd)); 3414 usb_pipe_close(hubd->h_dip, hubd->h_ep1_ph, USB_FLAGS_SLEEP, 3415 NULL, NULL); 3416 mutex_enter(HUBD_MUTEX(hubd)); 3417 hubd->h_ep1_ph = NULL; 3418 } 3419 3420 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE; 3421 } 3422 3423 3424 /* 3425 * hubd_exception_cb 3426 * interrupt ep1 exception callback function. 3427 * this callback executes in taskq thread context and assumes 3428 * autoclearing 3429 */ 3430 /*ARGSUSED*/ 3431 static void 3432 hubd_exception_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp) 3433 { 3434 hubd_t *hubd = (hubd_t *)(reqp->intr_client_private); 3435 3436 USB_DPRINTF_L2(DPRINT_MASK_CALLBACK, hubd->h_log_handle, 3437 "hubd_exception_cb: " 3438 "req=0x%p cr=%d data=0x%p cb_flags=0x%x", (void *)reqp, 3439 reqp->intr_completion_reason, (void *)reqp->intr_data, 3440 reqp->intr_cb_flags); 3441 3442 ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0); 3443 3444 mutex_enter(HUBD_MUTEX(hubd)); 3445 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0); 3446 3447 switch (reqp->intr_completion_reason) { 3448 case USB_CR_PIPE_RESET: 3449 /* only restart polling after autoclearing */ 3450 if ((hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) && 3451 (hubd->h_port_reset_wait == 0)) { 3452 hubd_start_polling(hubd, 0); 3453 } 3454 3455 break; 3456 case USB_CR_DEV_NOT_RESP: 3457 case USB_CR_STOPPED_POLLING: 3458 case USB_CR_PIPE_CLOSING: 3459 case USB_CR_UNSPECIFIED_ERR: 3460 /* never restart polling on these conditions */ 3461 default: 3462 /* for all others, wait for the autoclearing PIPE_RESET cb */ 3463 3464 break; 3465 } 3466 3467 usb_free_intr_req(reqp); 3468 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 3469 mutex_exit(HUBD_MUTEX(hubd)); 3470 } 3471 3472 3473 /* 3474 * helper function to convert LE bytes to a portmask 3475 */ 3476 static usb_port_mask_t 3477 hubd_mblk2portmask(mblk_t *data) 3478 { 3479 int len = min(MBLKL(data), sizeof (usb_port_mask_t)); 3480 usb_port_mask_t rval = 0; 3481 int i; 3482 3483 for (i = 0; i < len; i++) { 3484 rval |= data->b_rptr[i] << (i * 8); 3485 } 3486 3487 return (rval); 3488 } 3489 3490 3491 /* 3492 * hubd_read_cb: 3493 * interrupt ep1 callback function 3494 * 3495 * the status indicates just a change on the pipe with no indication 3496 * of what the change was 3497 * 3498 * known conditions: 3499 * - reset port completion 3500 * - connect 3501 * - disconnect 3502 * 3503 * for handling the hotplugging, create a new thread that can do 3504 * synchronous usba calls 3505 */ 3506 static void 3507 hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp) 3508 { 3509 hubd_t *hubd = (hubd_t *)(reqp->intr_client_private); 3510 size_t length; 3511 mblk_t *data = reqp->intr_data; 3512 int mem_flag = 0; 3513 hubd_hotplug_arg_t *arg; 3514 3515 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3516 "hubd_read_cb: ph=0x%p req=0x%p", (void *)pipe, (void *)reqp); 3517 3518 ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0); 3519 3520 /* 3521 * At present, we are not handling notification for completion of 3522 * asynchronous pipe reset, for which this data ptr could be NULL 3523 */ 3524 3525 if (data == NULL) { 3526 usb_free_intr_req(reqp); 3527 3528 return; 3529 } 3530 3531 arg = (hubd_hotplug_arg_t *)kmem_zalloc( 3532 sizeof (hubd_hotplug_arg_t), KM_SLEEP); 3533 mem_flag = 1; 3534 3535 mutex_enter(HUBD_MUTEX(hubd)); 3536 3537 if ((hubd->h_dev_state == USB_DEV_SUSPENDED) || 3538 (hubd->h_intr_pipe_state != HUBD_INTR_PIPE_ACTIVE)) { 3539 mutex_exit(HUBD_MUTEX(hubd)); 3540 usb_free_intr_req(reqp); 3541 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3542 3543 return; 3544 } 3545 3546 ASSERT(hubd->h_ep1_ph == pipe); 3547 3548 length = MBLKL(data); 3549 3550 /* 3551 * Only look at the data and startup the hotplug thread if 3552 * there actually is data. 3553 */ 3554 if (length != 0) { 3555 usb_port_mask_t port_change = hubd_mblk2portmask(data); 3556 3557 /* 3558 * if a port change was already reported and we are waiting for 3559 * reset port completion then wake up the hotplug thread which 3560 * should be waiting on reset port completion 3561 * 3562 * if there is disconnect event instead of reset completion, let 3563 * the hotplug thread figure this out 3564 */ 3565 3566 /* remove the reset wait bits from the status */ 3567 hubd->h_port_change |= port_change & 3568 ~hubd->h_port_reset_wait; 3569 3570 USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle, 3571 "port change=0x%x port_reset_wait=0x%x", 3572 hubd->h_port_change, hubd->h_port_reset_wait); 3573 3574 /* there should be only one reset bit active at the time */ 3575 if (hubd->h_port_reset_wait & port_change) { 3576 hubd->h_port_reset_wait = 0; 3577 cv_signal(&hubd->h_cv_reset_port); 3578 } 3579 3580 /* 3581 * kick off the thread only if device is ONLINE and it is not 3582 * during attaching or detaching 3583 */ 3584 if ((hubd->h_dev_state == USB_DEV_ONLINE) && 3585 (!DEVI_IS_ATTACHING(hubd->h_dip)) && 3586 (!DEVI_IS_DETACHING(hubd->h_dip)) && 3587 (hubd->h_port_change) && 3588 (hubd->h_hotplug_thread == 0)) { 3589 USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle, 3590 "creating hotplug thread: " 3591 "dev_state=%d", hubd->h_dev_state); 3592 3593 /* 3594 * Mark this device as busy. The will be marked idle 3595 * if the async req fails or at the exit of hotplug 3596 * thread 3597 */ 3598 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0); 3599 3600 arg->hubd = hubd; 3601 arg->hotplug_during_attach = B_FALSE; 3602 3603 if (usb_async_req(hubd->h_dip, 3604 hubd_hotplug_thread, 3605 (void *)arg, 0) == USB_SUCCESS) { 3606 hubd->h_hotplug_thread++; 3607 mem_flag = 0; 3608 } else { 3609 /* mark this device as idle */ 3610 (void) hubd_pm_idle_component(hubd, 3611 hubd->h_dip, 0); 3612 } 3613 } 3614 } 3615 mutex_exit(HUBD_MUTEX(hubd)); 3616 3617 if (mem_flag == 1) { 3618 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3619 } 3620 3621 usb_free_intr_req(reqp); 3622 } 3623 3624 3625 /* 3626 * hubd_hotplug_thread: 3627 * handles resetting of port, and creating children 3628 * 3629 * the ports to check are indicated in h_port_change bit mask 3630 * XXX note that one time poll doesn't work on the root hub 3631 */ 3632 static void 3633 hubd_hotplug_thread(void *arg) 3634 { 3635 hubd_hotplug_arg_t *hd_arg = (hubd_hotplug_arg_t *)arg; 3636 hubd_t *hubd = hd_arg->hubd; 3637 boolean_t attach_flg = hd_arg->hotplug_during_attach; 3638 usb_port_t port; 3639 uint16_t nports; 3640 uint16_t status, change; 3641 hub_power_t *hubpm; 3642 dev_info_t *hdip = hubd->h_dip; 3643 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip; 3644 dev_info_t *child_dip; 3645 boolean_t online_child = B_FALSE; 3646 boolean_t offline_child = B_FALSE; 3647 boolean_t pwrup_child = B_FALSE; 3648 int prh_circ, rh_circ, chld_circ, circ, old_state; 3649 3650 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3651 "hubd_hotplug_thread: started"); 3652 3653 /* 3654 * Before console is init'd, we temporarily block the hotplug 3655 * threads so that BUS_CONFIG_ONE through hubd_bus_config() can be 3656 * processed quickly. This reduces the time needed for vfs_mountroot() 3657 * to mount the root FS from a USB disk. 3658 */ 3659 while (!consconfig_console_is_ready()) { 3660 delay(drv_usectohz(10000)); 3661 } 3662 3663 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3664 3665 /* 3666 * if our bus power entry point is active, process the change 3667 * on the next notification of interrupt pipe 3668 */ 3669 mutex_enter(HUBD_MUTEX(hubd)); 3670 if (hubd->h_bus_pwr || (hubd->h_hotplug_thread > 1)) { 3671 hubd->h_hotplug_thread--; 3672 3673 /* mark this device as idle */ 3674 hubd_pm_idle_component(hubd, hubd->h_dip, 0); 3675 mutex_exit(HUBD_MUTEX(hubd)); 3676 3677 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3678 "hubd_hotplug_thread: " 3679 "bus_power in progress/hotplugging undesirable - quit"); 3680 3681 return; 3682 } 3683 mutex_exit(HUBD_MUTEX(hubd)); 3684 3685 ndi_hold_devi(hdip); /* so we don't race with detach */ 3686 3687 mutex_enter(HUBD_MUTEX(hubd)); 3688 3689 /* is this the root hub? */ 3690 if (hdip == rh_dip) { 3691 if (hubd->h_dev_state == USB_DEV_PWRED_DOWN) { 3692 hubpm = hubd->h_hubpm; 3693 3694 /* mark the root hub as full power */ 3695 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR; 3696 hubpm->hubp_time_at_full_power = ddi_get_time(); 3697 mutex_exit(HUBD_MUTEX(hubd)); 3698 3699 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3700 "hubd_hotplug_thread: call pm_power_has_changed"); 3701 3702 (void) pm_power_has_changed(hdip, 0, 3703 USB_DEV_OS_FULL_PWR); 3704 3705 mutex_enter(HUBD_MUTEX(hubd)); 3706 hubd->h_dev_state = USB_DEV_ONLINE; 3707 } 3708 3709 } else { 3710 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3711 "hubd_hotplug_thread: not root hub"); 3712 } 3713 3714 mutex_exit(HUBD_MUTEX(hubd)); 3715 3716 /* 3717 * this ensures one hotplug activity per system at a time. 3718 * we enter the parent PCI node to have this serialization. 3719 * this also excludes ioctls and deathrow thread 3720 * (a bit crude but easier to debug) 3721 */ 3722 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ); 3723 ndi_devi_enter(rh_dip, &rh_circ); 3724 3725 /* exclude other threads */ 3726 ndi_devi_enter(hdip, &circ); 3727 mutex_enter(HUBD_MUTEX(hubd)); 3728 3729 ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE); 3730 3731 nports = hubd->h_hub_descr.bNbrPorts; 3732 3733 hubd_stop_polling(hubd); 3734 3735 while ((hubd->h_dev_state == USB_DEV_ONLINE) && 3736 (hubd->h_port_change)) { 3737 /* 3738 * The 0th bit is the hub status change bit. 3739 * handle loss of local power here 3740 */ 3741 if (hubd->h_port_change & HUB_CHANGE_STATUS) { 3742 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3743 "hubd_hotplug_thread: hub status change!"); 3744 3745 /* 3746 * This should be handled properly. For now, 3747 * mask off the bit. 3748 */ 3749 hubd->h_port_change &= ~HUB_CHANGE_STATUS; 3750 3751 /* 3752 * check and ack hub status 3753 * this causes stall conditions 3754 * when local power is removed 3755 */ 3756 (void) hubd_get_hub_status(hubd); 3757 } 3758 3759 for (port = 1; port <= nports; port++) { 3760 usb_port_mask_t port_mask; 3761 boolean_t was_connected; 3762 3763 port_mask = 1 << port; 3764 was_connected = 3765 (hubd->h_port_state[port] & PORT_STATUS_CCS) && 3766 (hubd->h_children_dips[port]); 3767 3768 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3769 "hubd_hotplug_thread: " 3770 "port %d mask=0x%x change=0x%x connected=0x%x", 3771 port, port_mask, hubd->h_port_change, 3772 was_connected); 3773 3774 /* 3775 * is this a port connection that changed? 3776 */ 3777 if ((hubd->h_port_change & port_mask) == 0) { 3778 3779 continue; 3780 } 3781 hubd->h_port_change &= ~port_mask; 3782 3783 /* ack all changes */ 3784 (void) hubd_determine_port_status(hubd, port, 3785 &status, &change, HUBD_ACK_ALL_CHANGES); 3786 3787 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3788 "handle port %d:\n\t" 3789 "new status=0x%x change=0x%x was_conn=0x%x ", 3790 port, status, change, was_connected); 3791 3792 /* Recover a disabled port */ 3793 if (change & PORT_CHANGE_PESC) { 3794 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, 3795 hubd->h_log_handle, 3796 "port%d Disabled - " 3797 "status=0x%x, change=0x%x", 3798 port, status, change); 3799 3800 /* 3801 * if the port was connected and is still 3802 * connected, recover the port 3803 */ 3804 if (was_connected && (status & 3805 PORT_STATUS_CCS)) { 3806 online_child |= 3807 (hubd_recover_disabled_port(hubd, 3808 port) == USB_SUCCESS); 3809 } 3810 } 3811 3812 /* 3813 * Now check what changed on the port 3814 */ 3815 if ((change & PORT_CHANGE_CSC) || attach_flg) { 3816 if ((status & PORT_STATUS_CCS) && 3817 (!was_connected)) { 3818 /* new device plugged in */ 3819 online_child |= 3820 (hubd_handle_port_connect(hubd, 3821 port) == USB_SUCCESS); 3822 3823 } else if ((status & PORT_STATUS_CCS) && 3824 was_connected) { 3825 /* 3826 * In this case we can never be sure 3827 * if the device indeed got hotplugged 3828 * or the hub is falsely reporting the 3829 * change. 3830 */ 3831 child_dip = hubd->h_children_dips[port]; 3832 3833 mutex_exit(HUBD_MUTEX(hubd)); 3834 /* 3835 * this ensures we do not race with 3836 * other threads which are detaching 3837 * the child driver at the same time. 3838 */ 3839 ndi_devi_enter(child_dip, &chld_circ); 3840 /* 3841 * Now check if the driver remains 3842 * attached. 3843 */ 3844 if (i_ddi_devi_attached(child_dip)) { 3845 /* 3846 * first post a disconnect event 3847 * to the child. 3848 */ 3849 hubd_post_event(hubd, port, 3850 USBA_EVENT_TAG_HOT_REMOVAL); 3851 mutex_enter(HUBD_MUTEX(hubd)); 3852 3853 /* 3854 * then reset the port and 3855 * recover the device 3856 */ 3857 online_child |= 3858 (hubd_handle_port_connect( 3859 hubd, port) == USB_SUCCESS); 3860 3861 mutex_exit(HUBD_MUTEX(hubd)); 3862 } 3863 3864 ndi_devi_exit(child_dip, chld_circ); 3865 mutex_enter(HUBD_MUTEX(hubd)); 3866 } else if (was_connected) { 3867 /* this is a disconnect */ 3868 mutex_exit(HUBD_MUTEX(hubd)); 3869 hubd_post_event(hubd, port, 3870 USBA_EVENT_TAG_HOT_REMOVAL); 3871 mutex_enter(HUBD_MUTEX(hubd)); 3872 3873 offline_child = B_TRUE; 3874 } 3875 } 3876 3877 /* 3878 * Check if any port is coming out of suspend 3879 */ 3880 if (change & PORT_CHANGE_PSSC) { 3881 /* a resuming device could have disconnected */ 3882 if (was_connected && 3883 hubd->h_children_dips[port]) { 3884 3885 /* device on this port resuming */ 3886 dev_info_t *dip; 3887 3888 dip = hubd->h_children_dips[port]; 3889 3890 /* 3891 * Don't raise power on detaching child 3892 */ 3893 if (!DEVI_IS_DETACHING(dip)) { 3894 /* 3895 * As this child is not 3896 * detaching, we set this 3897 * flag, causing bus_ctls 3898 * to stall detach till 3899 * pm_raise_power returns 3900 * and flag it for a deferred 3901 * raise_power. 3902 * 3903 * pm_raise_power is deferred 3904 * because we need to release 3905 * the locks first. 3906 */ 3907 hubd->h_port_state[port] |= 3908 HUBD_CHILD_RAISE_POWER; 3909 pwrup_child = B_TRUE; 3910 mutex_exit(HUBD_MUTEX(hubd)); 3911 3912 /* 3913 * make sure that child 3914 * doesn't disappear 3915 */ 3916 ndi_hold_devi(dip); 3917 3918 mutex_enter(HUBD_MUTEX(hubd)); 3919 } 3920 } 3921 } 3922 3923 /* 3924 * Check if the port is over-current 3925 */ 3926 if (change & PORT_CHANGE_OCIC) { 3927 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, 3928 hubd->h_log_handle, 3929 "Port%d in over current condition, " 3930 "please check the attached device to " 3931 "clear the condition. The system will " 3932 "try to recover the port, but if not " 3933 "successful, you need to re-connect " 3934 "the hub or reboot the system to bring " 3935 "the port back to work", port); 3936 3937 if (!(status & PORT_STATUS_PPS)) { 3938 /* 3939 * Try to enable port power, but 3940 * possibly fail. Ignore failure 3941 */ 3942 (void) hubd_enable_port_power(hubd, 3943 port); 3944 3945 /* 3946 * Delay some time to avoid 3947 * over-current event to happen 3948 * too frequently in some cases 3949 */ 3950 mutex_exit(HUBD_MUTEX(hubd)); 3951 delay(drv_usectohz(500000)); 3952 mutex_enter(HUBD_MUTEX(hubd)); 3953 } 3954 } 3955 } 3956 } 3957 3958 /* release locks so we can do a devfs_clean */ 3959 mutex_exit(HUBD_MUTEX(hubd)); 3960 3961 /* delete cached dv_node's but drop locks first */ 3962 ndi_devi_exit(hdip, circ); 3963 ndi_devi_exit(rh_dip, rh_circ); 3964 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ); 3965 3966 (void) devfs_clean(rh_dip, NULL, 0); 3967 3968 /* now check if any children need onlining */ 3969 if (online_child) { 3970 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3971 "hubd_hotplug_thread: onlining children"); 3972 3973 (void) ndi_devi_online(hubd->h_dip, 0); 3974 } 3975 3976 /* now check if any disconnected devices need to be cleaned up */ 3977 if (offline_child) { 3978 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 3979 "hubd_hotplug_thread: scheduling cleanup"); 3980 3981 hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip); 3982 } 3983 3984 mutex_enter(HUBD_MUTEX(hubd)); 3985 3986 /* now raise power on the children that have woken up */ 3987 if (pwrup_child) { 3988 old_state = hubd->h_dev_state; 3989 hubd->h_dev_state = USB_DEV_HUB_CHILD_PWRLVL; 3990 for (port = 1; port <= nports; port++) { 3991 if (hubd->h_port_state[port] & HUBD_CHILD_RAISE_POWER) { 3992 dev_info_t *dip = hubd->h_children_dips[port]; 3993 3994 mutex_exit(HUBD_MUTEX(hubd)); 3995 3996 /* Get the device to full power */ 3997 (void) pm_busy_component(dip, 0); 3998 (void) pm_raise_power(dip, 0, 3999 USB_DEV_OS_FULL_PWR); 4000 (void) pm_idle_component(dip, 0); 4001 4002 /* release the hold on the child */ 4003 ndi_rele_devi(dip); 4004 mutex_enter(HUBD_MUTEX(hubd)); 4005 hubd->h_port_state[port] &= 4006 ~HUBD_CHILD_RAISE_POWER; 4007 } 4008 } 4009 /* 4010 * make sure that we don't accidentally 4011 * over write the disconnect state 4012 */ 4013 if (hubd->h_dev_state == USB_DEV_HUB_CHILD_PWRLVL) { 4014 hubd->h_dev_state = old_state; 4015 } 4016 } 4017 4018 /* 4019 * start polling can immediately kick off read callback 4020 * we need to set the h_hotplug_thread to 0 so that 4021 * the callback is not dropped 4022 * 4023 * if there is device during reset, still stop polling to avoid the 4024 * read callback interrupting the reset, the polling will be started 4025 * in hubd_reset_thread. 4026 */ 4027 for (port = 1; port <= MAX_PORTS; port++) { 4028 if (hubd->h_reset_port[port]) { 4029 4030 break; 4031 } 4032 } 4033 if (port > MAX_PORTS) { 4034 hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING); 4035 } 4036 4037 /* 4038 * Earlier we would set the h_hotplug_thread = 0 before 4039 * polling was restarted so that 4040 * if there is any root hub status change interrupt, we can still kick 4041 * off the hotplug thread. This was valid when this interrupt was 4042 * delivered in hardware, and only ONE interrupt would be delivered. 4043 * Now that we poll on the root hub looking for status change in 4044 * software, this assignment is no longer required. 4045 */ 4046 hubd->h_hotplug_thread--; 4047 4048 /* mark this device as idle */ 4049 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 4050 4051 cv_broadcast(&hubd->h_cv_hotplug_dev); 4052 4053 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4054 "hubd_hotplug_thread: exit"); 4055 4056 mutex_exit(HUBD_MUTEX(hubd)); 4057 4058 ndi_rele_devi(hdip); 4059 } 4060 4061 4062 /* 4063 * hubd_handle_port_connect: 4064 * Transition a port from Disabled to Enabled. Ensure that the 4065 * port is in the correct state before attempting to 4066 * access the device. 4067 */ 4068 static int 4069 hubd_handle_port_connect(hubd_t *hubd, usb_port_t port) 4070 { 4071 int rval; 4072 int retry; 4073 long time_delay; 4074 long settling_time; 4075 uint16_t status; 4076 uint16_t change; 4077 usb_addr_t hubd_usb_addr; 4078 usba_device_t *usba_device; 4079 usb_port_status_t port_status = 0; 4080 usb_port_status_t hub_port_status = 0; 4081 4082 /* Get the hub address and port status */ 4083 usba_device = hubd->h_usba_device; 4084 mutex_enter(&usba_device->usb_mutex); 4085 hubd_usb_addr = usba_device->usb_addr; 4086 hub_port_status = usba_device->usb_port_status; 4087 mutex_exit(&usba_device->usb_mutex); 4088 4089 /* 4090 * If a device is connected, transition the 4091 * port from Disabled to the Enabled state. 4092 * The device will receive downstream packets 4093 * in the Enabled state. 4094 * 4095 * reset port and wait for the hub to report 4096 * completion 4097 */ 4098 change = status = 0; 4099 4100 /* 4101 * According to section 9.1.2 of USB 2.0 spec, the host should 4102 * wait for atleast 100ms to allow completion of an insertion 4103 * process and for power at the device to become stable. 4104 * We wait for 200 ms 4105 */ 4106 settling_time = drv_usectohz(hubd_device_delay / 5); 4107 mutex_exit(HUBD_MUTEX(hubd)); 4108 delay(settling_time); 4109 mutex_enter(HUBD_MUTEX(hubd)); 4110 4111 /* calculate 600 ms delay time */ 4112 time_delay = (6 * drv_usectohz(hubd_device_delay)) / 10; 4113 4114 for (retry = 0; (hubd->h_dev_state == USB_DEV_ONLINE) && 4115 (retry < hubd_retry_enumerate); retry++) { 4116 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4117 "resetting port%d, retry=%d", port, retry); 4118 4119 if ((rval = hubd_reset_port(hubd, port)) != USB_SUCCESS) { 4120 (void) hubd_determine_port_status(hubd, 4121 port, &status, &change, 0); 4122 4123 /* continue only if port is still connected */ 4124 if (status & PORT_STATUS_CCS) { 4125 continue; 4126 } 4127 4128 /* carry on regardless */ 4129 } 4130 4131 /* 4132 * according to USB 2.0 spec section 11.24.2.7.1.2 4133 * at the end of port reset, the hub enables the port. 4134 * But for some strange reasons, uhci port remains disabled. 4135 * And because the port remains disabled for the settling 4136 * time below, the device connected to the port gets wedged 4137 * - fails to enumerate (device not responding) 4138 * Hence, we enable it here immediately and later again after 4139 * the delay 4140 */ 4141 (void) hubd_enable_port(hubd, port); 4142 4143 /* we skip this delay in the first iteration */ 4144 if (retry) { 4145 /* 4146 * delay for device to signal disconnect/connect so 4147 * that hub properly recognizes the speed of the device 4148 */ 4149 mutex_exit(HUBD_MUTEX(hubd)); 4150 delay(settling_time); 4151 mutex_enter(HUBD_MUTEX(hubd)); 4152 4153 /* 4154 * When a low speed device is connected to any port of 4155 * PPX it has to be explicitly enabled 4156 * Also, if device intentionally signals 4157 * disconnect/connect, it will disable the port. 4158 * So enable it again. 4159 */ 4160 (void) hubd_enable_port(hubd, port); 4161 } 4162 4163 if ((rval = hubd_determine_port_status(hubd, port, &status, 4164 &change, 0)) != USB_SUCCESS) { 4165 4166 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4167 "getting status failed (%d)", rval); 4168 4169 (void) hubd_disable_port(hubd, port); 4170 4171 continue; 4172 } 4173 4174 if (status & PORT_STATUS_POCI) { 4175 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4176 "port %d overcurrent", port); 4177 4178 (void) hubd_disable_port(hubd, port); 4179 4180 /* ack changes */ 4181 (void) hubd_determine_port_status(hubd, 4182 port, &status, &change, PORT_CHANGE_OCIC); 4183 4184 continue; 4185 } 4186 4187 /* is status really OK? */ 4188 if ((status & PORT_STATUS_OK) != PORT_STATUS_OK) { 4189 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4190 "port %d status (0x%x) not OK on retry %d", 4191 port, status, retry); 4192 4193 /* check if we still have the connection */ 4194 if (!(status & PORT_STATUS_CCS)) { 4195 /* lost connection, set exit condition */ 4196 retry = hubd_retry_enumerate; 4197 4198 break; 4199 } 4200 } else { 4201 /* 4202 * Determine if the device is high or full 4203 * or low speed. 4204 */ 4205 if (status & PORT_STATUS_LSDA) { 4206 port_status = USBA_LOW_SPEED_DEV; 4207 } else if (status & PORT_STATUS_HSDA) { 4208 port_status = USBA_HIGH_SPEED_DEV; 4209 } else { 4210 port_status = USBA_FULL_SPEED_DEV; 4211 } 4212 4213 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4214 "creating child port%d, status=0x%x " 4215 "port status=0x%x", 4216 port, status, port_status); 4217 4218 /* 4219 * if the child already exists, set addrs and config 4220 * to the device post connect event to the child 4221 */ 4222 if (hubd->h_children_dips[port]) { 4223 /* set addrs to this device */ 4224 rval = hubd_setdevaddr(hubd, port); 4225 4226 /* 4227 * This delay is important for the CATC hub 4228 * to enumerate. But, avoid delay in the first 4229 * iteration 4230 */ 4231 if (retry) { 4232 mutex_exit(HUBD_MUTEX(hubd)); 4233 delay(drv_usectohz( 4234 hubd_device_delay/100)); 4235 mutex_enter(HUBD_MUTEX(hubd)); 4236 } 4237 4238 if (rval == USB_SUCCESS) { 4239 /* 4240 * if the port is resetting, check if 4241 * device's descriptors have changed. 4242 */ 4243 if ((hubd->h_reset_port[port]) && 4244 (hubd_check_same_device(hubd, 4245 port) != USB_SUCCESS)) { 4246 retry = hubd_retry_enumerate; 4247 4248 break; 4249 } 4250 4251 /* 4252 * set the default config for 4253 * this device 4254 */ 4255 hubd_setdevconfig(hubd, port); 4256 4257 /* 4258 * if we are doing Default reset, do 4259 * not post reconnect event since we 4260 * don't know where reset function is 4261 * called. 4262 */ 4263 if (hubd->h_reset_port[port]) { 4264 4265 return (USB_SUCCESS); 4266 } 4267 4268 /* 4269 * indicate to the child that 4270 * it is online again 4271 */ 4272 mutex_exit(HUBD_MUTEX(hubd)); 4273 hubd_post_event(hubd, port, 4274 USBA_EVENT_TAG_HOT_INSERTION); 4275 mutex_enter(HUBD_MUTEX(hubd)); 4276 4277 return (USB_SUCCESS); 4278 } 4279 } else { 4280 /* 4281 * We need to release access here 4282 * so that busctls on other ports can 4283 * continue and don't cause a deadlock 4284 * when busctl and removal of prom node 4285 * takes concurrently. This also ensures 4286 * busctls for attach of successfully 4287 * enumerated devices on other ports can 4288 * continue concurrently with the process 4289 * of enumerating the new devices. This 4290 * reduces the overall boot time of the system. 4291 */ 4292 rval = hubd_create_child(hubd->h_dip, 4293 hubd, 4294 hubd->h_usba_device, 4295 port_status, port, 4296 retry); 4297 if (rval == USB_SUCCESS) { 4298 usba_update_hotplug_stats(hubd->h_dip, 4299 USBA_TOTAL_HOTPLUG_SUCCESS| 4300 USBA_HOTPLUG_SUCCESS); 4301 hubd->h_total_hotplug_success++; 4302 4303 if (retry > 0) { 4304 USB_DPRINTF_L2( 4305 DPRINT_MASK_HOTPLUG, 4306 hubd->h_log_handle, 4307 "device on port %d " 4308 "enumerated after %d %s", 4309 port, retry, 4310 (retry > 1) ? "retries" : 4311 "retry"); 4312 4313 } 4314 4315 return (USB_SUCCESS); 4316 } 4317 } 4318 } 4319 4320 /* wait a while until it settles? */ 4321 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4322 "disabling port %d again", port); 4323 4324 (void) hubd_disable_port(hubd, port); 4325 if (retry) { 4326 mutex_exit(HUBD_MUTEX(hubd)); 4327 delay(time_delay); 4328 mutex_enter(HUBD_MUTEX(hubd)); 4329 } 4330 4331 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4332 "retrying on port %d", port); 4333 } 4334 4335 if (retry >= hubd_retry_enumerate) { 4336 /* 4337 * If it is a High Speed Root Hub and connected device 4338 * Is a Low/Full Speed, it will be handled by USB 1.1 4339 * Host Controller. In this case, USB 2.0 Host Controller 4340 * will transfer the ownership of this port to USB 1.1 4341 * Host Controller. So don't display any error message on 4342 * the console. 4343 */ 4344 if ((hubd_usb_addr == ROOT_HUB_ADDR) && 4345 (hub_port_status == USBA_HIGH_SPEED_DEV) && 4346 (port_status != USBA_HIGH_SPEED_DEV)) { 4347 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 4348 hubd->h_log_handle, 4349 "hubd_handle_port_connect: Low/Full speed " 4350 "device is connected to High Speed root hub"); 4351 } else { 4352 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, 4353 hubd->h_log_handle, 4354 "Connecting device on port %d failed", port); 4355 } 4356 4357 (void) hubd_disable_port(hubd, port); 4358 usba_update_hotplug_stats(hubd->h_dip, 4359 USBA_TOTAL_HOTPLUG_FAILURE|USBA_HOTPLUG_FAILURE); 4360 hubd->h_total_hotplug_failure++; 4361 4362 /* 4363 * the port should be automagically 4364 * disabled but just in case, we do 4365 * it here 4366 */ 4367 (void) hubd_disable_port(hubd, port); 4368 4369 /* ack all changes because we disabled this port */ 4370 (void) hubd_determine_port_status(hubd, 4371 port, &status, &change, HUBD_ACK_ALL_CHANGES); 4372 4373 } 4374 4375 return (USB_FAILURE); 4376 } 4377 4378 4379 /* 4380 * hubd_get_hub_status: 4381 */ 4382 static int 4383 hubd_get_hub_status(hubd_t *hubd) 4384 { 4385 int rval; 4386 usb_cr_t completion_reason; 4387 usb_cb_flags_t cb_flags; 4388 uint16_t stword[2]; 4389 uint16_t status; 4390 uint16_t change; 4391 usb_cfg_descr_t cfg_descr; 4392 size_t cfg_length; 4393 uchar_t *usb_cfg; 4394 uint8_t MaxPower; 4395 usb_hub_descr_t *hub_descr; 4396 usb_port_t port; 4397 4398 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4399 "hubd_get_hub_status:"); 4400 4401 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 4402 4403 if ((hubd_get_hub_status_words(hubd, stword)) != USB_SUCCESS) { 4404 4405 return (USB_FAILURE); 4406 } 4407 status = stword[0]; 4408 change = stword[1]; 4409 4410 mutex_exit(HUBD_MUTEX(hubd)); 4411 4412 /* Obtain the raw configuration descriptor */ 4413 usb_cfg = usb_get_raw_cfg_data(hubd->h_dip, &cfg_length); 4414 4415 /* get configuration descriptor */ 4416 rval = usb_parse_cfg_descr(usb_cfg, cfg_length, 4417 &cfg_descr, USB_CFG_DESCR_SIZE); 4418 4419 if (rval != USB_CFG_DESCR_SIZE) { 4420 4421 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4422 "get hub configuration descriptor failed."); 4423 4424 mutex_enter(HUBD_MUTEX(hubd)); 4425 4426 return (USB_FAILURE); 4427 } else { 4428 MaxPower = cfg_descr.bMaxPower; 4429 } 4430 4431 /* check if local power status changed. */ 4432 if (change & C_HUB_LOCAL_POWER_STATUS) { 4433 4434 /* 4435 * local power has been lost, check the maximum 4436 * power consumption of current configuration. 4437 * see USB2.0 spec Table 11-12. 4438 */ 4439 if (status & HUB_LOCAL_POWER_STATUS) { 4440 4441 if (MaxPower == 0) { 4442 4443 /* 4444 * Self-powered only hub. Because it could 4445 * not draw any power from USB bus. 4446 * It can't work well on this condition. 4447 */ 4448 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, 4449 hubd->h_log_handle, 4450 "local power has been lost, " 4451 "please disconnect hub"); 4452 } else { 4453 4454 /* 4455 * Bus-powered only or self/bus-powered hub. 4456 */ 4457 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, 4458 hubd->h_log_handle, 4459 "local power has been lost," 4460 "the hub could draw %d" 4461 " mA power from the USB bus.", 4462 2*MaxPower); 4463 } 4464 4465 } 4466 4467 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4468 "clearing feature C_HUB_LOCAL_POWER "); 4469 4470 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4471 hubd->h_default_pipe, 4472 HUB_HANDLE_HUB_FEATURE_TYPE, 4473 USB_REQ_CLEAR_FEATURE, 4474 CFS_C_HUB_LOCAL_POWER, 4475 0, 4476 0, 4477 NULL, 0, 4478 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 4479 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 4480 hubd->h_log_handle, 4481 "clear feature C_HUB_LOCAL_POWER " 4482 "failed (%d 0x%x %d)", 4483 rval, completion_reason, cb_flags); 4484 } 4485 4486 } 4487 4488 if (change & C_HUB_OVER_CURRENT) { 4489 4490 if (status & HUB_OVER_CURRENT) { 4491 4492 if (usba_is_root_hub(hubd->h_dip)) { 4493 /* 4494 * The root hub should be automatically 4495 * recovered when over-current condition is 4496 * cleared. But there might be exception and 4497 * need user interaction to recover. 4498 */ 4499 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, 4500 hubd->h_log_handle, 4501 "Root hub over current condition, " 4502 "please check your system to clear the " 4503 "condition as soon as possible. And you " 4504 "may need to reboot the system to bring " 4505 "the root hub back to work if it cannot " 4506 "recover automatically"); 4507 } else { 4508 /* 4509 * The driver would try to recover port power 4510 * on over current condition. When the recovery 4511 * fails, the user may still need to offline 4512 * this hub in order to recover. 4513 * The port power is automatically disabled, 4514 * so we won't see disconnects. 4515 */ 4516 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, 4517 hubd->h_log_handle, 4518 "Hub global over current condition, " 4519 "please disconnect the devices connected " 4520 "to the hub to clear the condition. And " 4521 "you may need to re-connect the hub if " 4522 "the ports do not work"); 4523 } 4524 } 4525 4526 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 4527 "clearing feature C_HUB_OVER_CURRENT"); 4528 4529 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4530 hubd->h_default_pipe, 4531 HUB_HANDLE_HUB_FEATURE_TYPE, 4532 USB_REQ_CLEAR_FEATURE, 4533 CFS_C_HUB_OVER_CURRENT, 4534 0, 4535 0, 4536 NULL, 0, 4537 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 4538 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 4539 hubd->h_log_handle, 4540 "clear feature C_HUB_OVER_CURRENT " 4541 "failed (%d 0x%x %d)", 4542 rval, completion_reason, cb_flags); 4543 } 4544 4545 /* 4546 * Try to recover all port power if they are turned off. 4547 * Don't do this for root hub, but rely on the root hub 4548 * to recover itself. 4549 */ 4550 if (!usba_is_root_hub(hubd->h_dip)) { 4551 4552 mutex_enter(HUBD_MUTEX(hubd)); 4553 4554 /* 4555 * Only check the power status of the 1st port 4556 * since all port power status should be the same. 4557 */ 4558 (void) hubd_determine_port_status(hubd, 1, &status, 4559 &change, 0); 4560 4561 if (status & PORT_STATUS_PPS) { 4562 4563 return (USB_SUCCESS); 4564 } 4565 4566 hub_descr = &hubd->h_hub_descr; 4567 4568 for (port = 1; port <= hub_descr->bNbrPorts; 4569 port++) { 4570 4571 (void) hubd_enable_port_power(hubd, port); 4572 } 4573 4574 mutex_exit(HUBD_MUTEX(hubd)); 4575 4576 /* 4577 * Delay some time to avoid over-current event 4578 * to happen too frequently in some cases 4579 */ 4580 delay(drv_usectohz(500000)); 4581 } 4582 } 4583 4584 mutex_enter(HUBD_MUTEX(hubd)); 4585 4586 return (USB_SUCCESS); 4587 } 4588 4589 4590 /* 4591 * hubd_reset_port: 4592 */ 4593 static int 4594 hubd_reset_port(hubd_t *hubd, usb_port_t port) 4595 { 4596 int rval; 4597 usb_cr_t completion_reason; 4598 usb_cb_flags_t cb_flags; 4599 usb_port_mask_t port_mask = 1 << port; 4600 mblk_t *data; 4601 uint16_t status; 4602 uint16_t change; 4603 int i; 4604 clock_t delta; 4605 4606 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 4607 "hubd_reset_port: port=%d", port); 4608 4609 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 4610 4611 hubd->h_port_reset_wait |= port_mask; 4612 4613 mutex_exit(HUBD_MUTEX(hubd)); 4614 4615 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4616 hubd->h_default_pipe, 4617 HUB_HANDLE_PORT_FEATURE_TYPE, 4618 USB_REQ_SET_FEATURE, 4619 CFS_PORT_RESET, 4620 port, 4621 0, 4622 NULL, 0, 4623 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 4624 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 4625 "reset port%d failed (%d 0x%x %d)", 4626 port, completion_reason, cb_flags, rval); 4627 4628 mutex_enter(HUBD_MUTEX(hubd)); 4629 4630 return (USB_FAILURE); 4631 } 4632 4633 mutex_enter(HUBD_MUTEX(hubd)); 4634 4635 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 4636 "waiting on cv for reset completion"); 4637 4638 /* 4639 * wait for port status change event 4640 */ 4641 delta = drv_usectohz(hubd_device_delay / 10); 4642 for (i = 0; i < hubd_retry_enumerate; i++) { 4643 /* 4644 * start polling ep1 for receiving notification on 4645 * reset completion 4646 */ 4647 hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING); 4648 4649 /* 4650 * sleep a max of 100ms for reset completion 4651 * notification to be received 4652 */ 4653 if (hubd->h_port_reset_wait & port_mask) { 4654 rval = cv_reltimedwait(&hubd->h_cv_reset_port, 4655 &hubd->h_mutex, delta, TR_CLOCK_TICK); 4656 if ((rval <= 0) && 4657 (hubd->h_port_reset_wait & port_mask)) { 4658 /* we got woken up because of a timeout */ 4659 USB_DPRINTF_L2(DPRINT_MASK_PORT, 4660 hubd->h_log_handle, 4661 "timeout: reset port=%d failed", port); 4662 4663 hubd->h_port_reset_wait &= ~port_mask; 4664 4665 hubd_stop_polling(hubd); 4666 4667 return (USB_FAILURE); 4668 } 4669 } 4670 4671 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 4672 "reset completion received"); 4673 4674 hubd_stop_polling(hubd); 4675 4676 data = NULL; 4677 4678 /* check status to determine whether reset completed */ 4679 mutex_exit(HUBD_MUTEX(hubd)); 4680 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4681 hubd->h_default_pipe, 4682 HUB_GET_PORT_STATUS_TYPE, 4683 USB_REQ_GET_STATUS, 4684 0, 4685 port, 4686 GET_STATUS_LENGTH, 4687 &data, 0, 4688 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 4689 USB_DPRINTF_L2(DPRINT_MASK_PORT, 4690 hubd->h_log_handle, 4691 "get status port%d failed (%d 0x%x %d)", 4692 port, completion_reason, cb_flags, rval); 4693 4694 if (data) { 4695 freemsg(data); 4696 data = NULL; 4697 } 4698 mutex_enter(HUBD_MUTEX(hubd)); 4699 4700 continue; 4701 } 4702 4703 status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr); 4704 change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2); 4705 4706 freemsg(data); 4707 4708 /* continue only if port is still connected */ 4709 if (!(status & PORT_STATUS_CCS)) { 4710 4711 /* lost connection, set exit condition */ 4712 i = hubd_retry_enumerate; 4713 4714 mutex_enter(HUBD_MUTEX(hubd)); 4715 4716 break; 4717 } 4718 4719 if (status & PORT_STATUS_PRS) { 4720 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4721 "port%d reset active", port); 4722 mutex_enter(HUBD_MUTEX(hubd)); 4723 4724 continue; 4725 } else { 4726 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4727 "port%d reset inactive", port); 4728 } 4729 4730 if (change & PORT_CHANGE_PRSC) { 4731 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4732 "clearing feature CFS_C_PORT_RESET"); 4733 4734 if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4735 hubd->h_default_pipe, 4736 HUB_HANDLE_PORT_FEATURE_TYPE, 4737 USB_REQ_CLEAR_FEATURE, 4738 CFS_C_PORT_RESET, 4739 port, 4740 0, 4741 NULL, 0, 4742 &completion_reason, &cb_flags, 0) != USB_SUCCESS) { 4743 USB_DPRINTF_L2(DPRINT_MASK_PORT, 4744 hubd->h_log_handle, 4745 "clear feature CFS_C_PORT_RESET" 4746 " port%d failed (%d 0x%x %d)", 4747 port, completion_reason, cb_flags, rval); 4748 } 4749 } 4750 mutex_enter(HUBD_MUTEX(hubd)); 4751 4752 break; 4753 } 4754 4755 if (i >= hubd_retry_enumerate) { 4756 /* port reset has failed */ 4757 rval = USB_FAILURE; 4758 } 4759 4760 return (rval); 4761 } 4762 4763 4764 /* 4765 * hubd_enable_port: 4766 * this may fail if the hub as been disconnected 4767 */ 4768 static int 4769 hubd_enable_port(hubd_t *hubd, usb_port_t port) 4770 { 4771 int rval; 4772 usb_cr_t completion_reason; 4773 usb_cb_flags_t cb_flags; 4774 4775 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 4776 "hubd_enable_port: port=%d", port); 4777 4778 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 4779 4780 mutex_exit(HUBD_MUTEX(hubd)); 4781 4782 /* Do not issue a SetFeature(PORT_ENABLE) on external hubs */ 4783 if (!usba_is_root_hub(hubd->h_dip)) { 4784 mutex_enter(HUBD_MUTEX(hubd)); 4785 4786 return (USB_SUCCESS); 4787 } 4788 4789 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4790 hubd->h_default_pipe, 4791 HUB_HANDLE_PORT_FEATURE_TYPE, 4792 USB_REQ_SET_FEATURE, 4793 CFS_PORT_ENABLE, 4794 port, 4795 0, 4796 NULL, 0, 4797 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 4798 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 4799 "enable port%d failed (%d 0x%x %d)", 4800 port, completion_reason, cb_flags, rval); 4801 } 4802 4803 mutex_enter(HUBD_MUTEX(hubd)); 4804 4805 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 4806 "enabling port done"); 4807 4808 return (rval); 4809 } 4810 4811 4812 /* 4813 * hubd_disable_port 4814 */ 4815 static int 4816 hubd_disable_port(hubd_t *hubd, usb_port_t port) 4817 { 4818 int rval; 4819 usb_cr_t completion_reason; 4820 usb_cb_flags_t cb_flags; 4821 4822 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 4823 "hubd_disable_port: port=%d", port); 4824 4825 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 4826 4827 mutex_exit(HUBD_MUTEX(hubd)); 4828 4829 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4830 hubd->h_default_pipe, 4831 HUB_HANDLE_PORT_FEATURE_TYPE, 4832 USB_REQ_CLEAR_FEATURE, 4833 CFS_PORT_ENABLE, 4834 port, 4835 0, 4836 NULL, 0, 4837 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 4838 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 4839 "disable port%d failed (%d 0x%x %d)", port, 4840 completion_reason, cb_flags, rval); 4841 mutex_enter(HUBD_MUTEX(hubd)); 4842 4843 return (USB_FAILURE); 4844 } 4845 4846 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4847 "clearing feature CFS_C_PORT_ENABLE"); 4848 4849 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4850 hubd->h_default_pipe, 4851 HUB_HANDLE_PORT_FEATURE_TYPE, 4852 USB_REQ_CLEAR_FEATURE, 4853 CFS_C_PORT_ENABLE, 4854 port, 4855 0, 4856 NULL, 0, 4857 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 4858 USB_DPRINTF_L2(DPRINT_MASK_PORT, 4859 hubd->h_log_handle, 4860 "clear feature CFS_C_PORT_ENABLE port%d failed " 4861 "(%d 0x%x %d)", 4862 port, completion_reason, cb_flags, rval); 4863 4864 mutex_enter(HUBD_MUTEX(hubd)); 4865 4866 return (USB_FAILURE); 4867 } 4868 4869 mutex_enter(HUBD_MUTEX(hubd)); 4870 4871 return (USB_SUCCESS); 4872 } 4873 4874 4875 /* 4876 * hubd_determine_port_status: 4877 */ 4878 static int 4879 hubd_determine_port_status(hubd_t *hubd, usb_port_t port, 4880 uint16_t *status, uint16_t *change, uint_t ack_flag) 4881 { 4882 int rval; 4883 mblk_t *data = NULL; 4884 usb_cr_t completion_reason; 4885 usb_cb_flags_t cb_flags; 4886 4887 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 4888 "hubd_determine_port_status: port=%d, state=0x%x ack=0x%x", port, 4889 hubd->h_port_state[port], ack_flag); 4890 4891 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 4892 4893 mutex_exit(HUBD_MUTEX(hubd)); 4894 4895 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 4896 hubd->h_default_pipe, 4897 HUB_GET_PORT_STATUS_TYPE, 4898 USB_REQ_GET_STATUS, 4899 0, 4900 port, 4901 GET_STATUS_LENGTH, 4902 &data, 0, 4903 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 4904 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 4905 "port=%d get status failed (%d 0x%x %d)", 4906 port, completion_reason, cb_flags, rval); 4907 4908 if (data) { 4909 freemsg(data); 4910 } 4911 4912 *status = *change = 0; 4913 mutex_enter(HUBD_MUTEX(hubd)); 4914 4915 return (rval); 4916 } 4917 4918 mutex_enter(HUBD_MUTEX(hubd)); 4919 if (MBLKL(data) != GET_STATUS_LENGTH) { 4920 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 4921 "port %d: length incorrect %ld", 4922 port, MBLKL(data)); 4923 freemsg(data); 4924 *status = *change = 0; 4925 4926 return (rval); 4927 } 4928 4929 4930 *status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr); 4931 *change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2); 4932 4933 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4934 "port%d status=0x%x, change=0x%x", port, *status, *change); 4935 4936 freemsg(data); 4937 4938 if (*status & PORT_STATUS_CCS) { 4939 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4940 "port%d connected", port); 4941 4942 hubd->h_port_state[port] |= (PORT_STATUS_CCS & ack_flag); 4943 } else { 4944 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4945 "port%d disconnected", port); 4946 4947 hubd->h_port_state[port] &= ~(PORT_STATUS_CCS & ack_flag); 4948 } 4949 4950 if (*status & PORT_STATUS_PES) { 4951 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4952 "port%d enabled", port); 4953 4954 hubd->h_port_state[port] |= (PORT_STATUS_PES & ack_flag); 4955 } else { 4956 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4957 "port%d disabled", port); 4958 4959 hubd->h_port_state[port] &= ~(PORT_STATUS_PES & ack_flag); 4960 } 4961 4962 if (*status & PORT_STATUS_PSS) { 4963 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4964 "port%d suspended", port); 4965 4966 hubd->h_port_state[port] |= (PORT_STATUS_PSS & ack_flag); 4967 } else { 4968 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4969 "port%d not suspended", port); 4970 4971 hubd->h_port_state[port] &= ~(PORT_STATUS_PSS & ack_flag); 4972 } 4973 4974 if (*change & PORT_CHANGE_PRSC) { 4975 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4976 "port%d reset completed", port); 4977 4978 hubd->h_port_state[port] |= (PORT_CHANGE_PRSC & ack_flag); 4979 } else { 4980 4981 hubd->h_port_state[port] &= ~(PORT_CHANGE_PRSC & ack_flag); 4982 } 4983 4984 if (*status & PORT_STATUS_POCI) { 4985 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 4986 "port%d overcurrent!", port); 4987 4988 hubd->h_port_state[port] |= (PORT_STATUS_POCI & ack_flag); 4989 } else { 4990 4991 hubd->h_port_state[port] &= ~(PORT_STATUS_POCI & ack_flag); 4992 } 4993 4994 if (*status & PORT_STATUS_PRS) { 4995 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 4996 "port%d reset active", port); 4997 4998 hubd->h_port_state[port] |= (PORT_STATUS_PRS & ack_flag); 4999 } else { 5000 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5001 "port%d reset inactive", port); 5002 5003 hubd->h_port_state[port] &= ~(PORT_STATUS_PRS & ack_flag); 5004 } 5005 if (*status & PORT_STATUS_PPS) { 5006 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5007 "port%d power on", port); 5008 5009 hubd->h_port_state[port] |= (PORT_STATUS_PPS & ack_flag); 5010 } else { 5011 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5012 "port%d power off", port); 5013 5014 hubd->h_port_state[port] &= ~(PORT_STATUS_PPS & ack_flag); 5015 } 5016 if (*status & PORT_STATUS_LSDA) { 5017 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5018 "port%d low speed", port); 5019 5020 hubd->h_port_state[port] |= (PORT_STATUS_LSDA & ack_flag); 5021 } else { 5022 hubd->h_port_state[port] &= ~(PORT_STATUS_LSDA & ack_flag); 5023 if (*status & PORT_STATUS_HSDA) { 5024 USB_DPRINTF_L3(DPRINT_MASK_PORT, 5025 hubd->h_log_handle, "port%d " 5026 "high speed", port); 5027 5028 hubd->h_port_state[port] |= 5029 (PORT_STATUS_HSDA & ack_flag); 5030 } else { 5031 USB_DPRINTF_L3(DPRINT_MASK_PORT, 5032 hubd->h_log_handle, "port%d " 5033 "full speed", port); 5034 5035 hubd->h_port_state[port] &= 5036 ~(PORT_STATUS_HSDA & ack_flag); 5037 } 5038 } 5039 5040 /* 5041 * Acknowledge connection, enable, reset status 5042 */ 5043 if (ack_flag) { 5044 mutex_exit(HUBD_MUTEX(hubd)); 5045 if (*change & PORT_CHANGE_CSC & ack_flag) { 5046 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5047 "clearing feature CFS_C_PORT_CONNECTION"); 5048 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 5049 hubd->h_default_pipe, 5050 HUB_HANDLE_PORT_FEATURE_TYPE, 5051 USB_REQ_CLEAR_FEATURE, 5052 CFS_C_PORT_CONNECTION, 5053 port, 5054 0, NULL, 0, 5055 &completion_reason, &cb_flags, 0)) != 5056 USB_SUCCESS) { 5057 USB_DPRINTF_L2(DPRINT_MASK_PORT, 5058 hubd->h_log_handle, 5059 "clear feature CFS_C_PORT_CONNECTION" 5060 " port%d failed (%d 0x%x %d)", 5061 port, completion_reason, cb_flags, rval); 5062 } 5063 } 5064 if (*change & PORT_CHANGE_PESC & ack_flag) { 5065 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5066 "clearing feature CFS_C_PORT_ENABLE"); 5067 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 5068 hubd->h_default_pipe, 5069 HUB_HANDLE_PORT_FEATURE_TYPE, 5070 USB_REQ_CLEAR_FEATURE, 5071 CFS_C_PORT_ENABLE, 5072 port, 5073 0, NULL, 0, 5074 &completion_reason, &cb_flags, 0)) != 5075 USB_SUCCESS) { 5076 USB_DPRINTF_L2(DPRINT_MASK_PORT, 5077 hubd->h_log_handle, 5078 "clear feature CFS_C_PORT_ENABLE" 5079 " port%d failed (%d 0x%x %d)", 5080 port, completion_reason, cb_flags, rval); 5081 } 5082 } 5083 if (*change & PORT_CHANGE_PSSC & ack_flag) { 5084 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5085 "clearing feature CFS_C_PORT_SUSPEND"); 5086 5087 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 5088 hubd->h_default_pipe, 5089 HUB_HANDLE_PORT_FEATURE_TYPE, 5090 USB_REQ_CLEAR_FEATURE, 5091 CFS_C_PORT_SUSPEND, 5092 port, 5093 0, NULL, 0, 5094 &completion_reason, &cb_flags, 0)) != 5095 USB_SUCCESS) { 5096 USB_DPRINTF_L2(DPRINT_MASK_PORT, 5097 hubd->h_log_handle, 5098 "clear feature CFS_C_PORT_SUSPEND" 5099 " port%d failed (%d 0x%x %d)", 5100 port, completion_reason, cb_flags, rval); 5101 } 5102 } 5103 if (*change & PORT_CHANGE_OCIC & ack_flag) { 5104 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5105 "clearing feature CFS_C_PORT_OVER_CURRENT"); 5106 5107 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 5108 hubd->h_default_pipe, 5109 HUB_HANDLE_PORT_FEATURE_TYPE, 5110 USB_REQ_CLEAR_FEATURE, 5111 CFS_C_PORT_OVER_CURRENT, 5112 port, 5113 0, NULL, 0, 5114 &completion_reason, &cb_flags, 0)) != 5115 USB_SUCCESS) { 5116 USB_DPRINTF_L2(DPRINT_MASK_PORT, 5117 hubd->h_log_handle, 5118 "clear feature CFS_C_PORT_OVER_CURRENT" 5119 " port%d failed (%d 0x%x %d)", 5120 port, completion_reason, cb_flags, rval); 5121 } 5122 } 5123 if (*change & PORT_CHANGE_PRSC & ack_flag) { 5124 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 5125 "clearing feature CFS_C_PORT_RESET"); 5126 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 5127 hubd->h_default_pipe, 5128 HUB_HANDLE_PORT_FEATURE_TYPE, 5129 USB_REQ_CLEAR_FEATURE, 5130 CFS_C_PORT_RESET, 5131 port, 5132 0, NULL, 0, 5133 &completion_reason, &cb_flags, 0)) != 5134 USB_SUCCESS) { 5135 USB_DPRINTF_L2(DPRINT_MASK_PORT, 5136 hubd->h_log_handle, 5137 "clear feature CFS_C_PORT_RESET" 5138 " port%d failed (%d 0x%x %d)", 5139 port, completion_reason, cb_flags, rval); 5140 } 5141 } 5142 mutex_enter(HUBD_MUTEX(hubd)); 5143 } 5144 5145 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 5146 "new port%d state 0x%x", port, hubd->h_port_state[port]); 5147 5148 5149 return (USB_SUCCESS); 5150 } 5151 5152 5153 /* 5154 * hubd_recover_disabled_port 5155 * if the port got disabled because of an error 5156 * enable it. If hub doesn't suport enable port, 5157 * reset the port to bring the device to life again 5158 */ 5159 static int 5160 hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port) 5161 { 5162 uint16_t status; 5163 uint16_t change; 5164 int rval = USB_FAILURE; 5165 5166 /* first try enabling the port */ 5167 (void) hubd_enable_port(hubd, port); 5168 5169 /* read the port status */ 5170 (void) hubd_determine_port_status(hubd, port, &status, &change, 5171 PORT_CHANGE_PESC); 5172 5173 if (status & PORT_STATUS_PES) { 5174 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5175 "Port%d now Enabled", port); 5176 } else if (status & PORT_STATUS_CCS) { 5177 /* first post a disconnect event to the child */ 5178 mutex_exit(HUBD_MUTEX(hubd)); 5179 hubd_post_event(hubd, port, USBA_EVENT_TAG_HOT_REMOVAL); 5180 mutex_enter(HUBD_MUTEX(hubd)); 5181 5182 /* then reset the port and recover the device */ 5183 rval = hubd_handle_port_connect(hubd, port); 5184 5185 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5186 "Port%d now Enabled by force", port); 5187 } 5188 5189 return (rval); 5190 } 5191 5192 5193 /* 5194 * hubd_enable_all_port_power: 5195 */ 5196 static int 5197 hubd_enable_all_port_power(hubd_t *hubd) 5198 { 5199 usb_hub_descr_t *hub_descr; 5200 int wait; 5201 usb_port_t port; 5202 uint_t retry; 5203 uint16_t status; 5204 uint16_t change; 5205 5206 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 5207 "hubd_enable_all_port_power"); 5208 5209 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 5210 5211 hub_descr = &hubd->h_hub_descr; 5212 5213 /* 5214 * According to section 11.11 of USB, for hubs with no power 5215 * switches, bPwrOn2PwrGood is zero. But we wait for some 5216 * arbitrary time to enable power to become stable. 5217 * 5218 * If an hub supports port power switching, we need to wait 5219 * at least 20ms before accessing corresponding usb port. 5220 */ 5221 if ((hub_descr->wHubCharacteristics & 5222 HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) { 5223 wait = hubd_device_delay / 10; 5224 } else { 5225 wait = max(HUB_DEFAULT_POPG, 5226 hub_descr->bPwrOn2PwrGood) * 2 * 1000; 5227 } 5228 5229 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 5230 "hubd_enable_all_port_power: popg=%d wait=%d", 5231 hub_descr->bPwrOn2PwrGood, wait); 5232 5233 /* 5234 * Enable power per port. we ignore gang power and power mask 5235 * and always enable all ports one by one. 5236 */ 5237 for (port = 1; port <= hub_descr->bNbrPorts; port++) { 5238 /* 5239 * Transition the port from the Powered Off to the 5240 * Disconnected state by supplying power to the port. 5241 */ 5242 USB_DPRINTF_L4(DPRINT_MASK_PORT, 5243 hubd->h_log_handle, 5244 "hubd_enable_all_port_power: power port=%d", port); 5245 5246 (void) hubd_enable_port_power(hubd, port); 5247 } 5248 5249 mutex_exit(HUBD_MUTEX(hubd)); 5250 delay(drv_usectohz(wait)); 5251 mutex_enter(HUBD_MUTEX(hubd)); 5252 5253 /* For retry if any, use some extra delay */ 5254 wait = max(wait, hubd_device_delay / 10); 5255 5256 /* Check each port power status for a given usb hub */ 5257 for (port = 1; port <= hub_descr->bNbrPorts; port++) { 5258 5259 /* Get port status */ 5260 (void) hubd_determine_port_status(hubd, port, 5261 &status, &change, 0); 5262 5263 for (retry = 0; ((!(status & PORT_STATUS_PPS)) && 5264 (retry < HUBD_PORT_RETRY)); retry++) { 5265 5266 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 5267 "Retry is in progress %d: port %d status %d", 5268 retry, port, status); 5269 5270 (void) hubd_enable_port_power(hubd, port); 5271 5272 mutex_exit(HUBD_MUTEX(hubd)); 5273 delay(drv_usectohz(wait)); 5274 mutex_enter(HUBD_MUTEX(hubd)); 5275 5276 /* Get port status */ 5277 (void) hubd_determine_port_status(hubd, port, 5278 &status, &change, 0); 5279 } 5280 5281 /* Print warning message if port has no power */ 5282 if (!(status & PORT_STATUS_PPS)) { 5283 5284 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 5285 "hubd_enable_all_port_power: port %d power-on " 5286 "failed, port status 0x%x", port, status); 5287 } 5288 } 5289 5290 return (USB_SUCCESS); 5291 } 5292 5293 5294 /* 5295 * hubd_enable_port_power: 5296 * enable individual port power 5297 */ 5298 static int 5299 hubd_enable_port_power(hubd_t *hubd, usb_port_t port) 5300 { 5301 int rval; 5302 usb_cr_t completion_reason; 5303 usb_cb_flags_t cb_flags; 5304 5305 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 5306 "hubd_enable_port_power: port=%d", port); 5307 5308 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 5309 ASSERT(hubd->h_default_pipe != 0); 5310 5311 mutex_exit(HUBD_MUTEX(hubd)); 5312 5313 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 5314 hubd->h_default_pipe, 5315 HUB_HANDLE_PORT_FEATURE_TYPE, 5316 USB_REQ_SET_FEATURE, 5317 CFS_PORT_POWER, 5318 port, 5319 0, NULL, 0, 5320 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 5321 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 5322 "set port power failed (%d 0x%x %d)", 5323 completion_reason, cb_flags, rval); 5324 mutex_enter(HUBD_MUTEX(hubd)); 5325 5326 return (USB_FAILURE); 5327 } else { 5328 mutex_enter(HUBD_MUTEX(hubd)); 5329 hubd->h_port_state[port] |= PORT_STATUS_PPS; 5330 5331 return (USB_SUCCESS); 5332 } 5333 } 5334 5335 5336 /* 5337 * hubd_disable_all_port_power: 5338 */ 5339 static int 5340 hubd_disable_all_port_power(hubd_t *hubd) 5341 { 5342 usb_port_t port; 5343 5344 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 5345 "hubd_disable_all_port_power"); 5346 5347 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 5348 5349 /* 5350 * disable power per port, ignore gang power and power mask 5351 */ 5352 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 5353 (void) hubd_disable_port_power(hubd, port); 5354 } 5355 5356 return (USB_SUCCESS); 5357 } 5358 5359 5360 /* 5361 * hubd_disable_port_power: 5362 * disable individual port power 5363 */ 5364 static int 5365 hubd_disable_port_power(hubd_t *hubd, usb_port_t port) 5366 { 5367 int rval; 5368 usb_cr_t completion_reason; 5369 usb_cb_flags_t cb_flags; 5370 5371 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 5372 "hubd_disable_port_power: port=%d", port); 5373 5374 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 5375 5376 mutex_exit(HUBD_MUTEX(hubd)); 5377 5378 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 5379 hubd->h_default_pipe, 5380 HUB_HANDLE_PORT_FEATURE_TYPE, 5381 USB_REQ_CLEAR_FEATURE, 5382 CFS_PORT_POWER, 5383 port, 5384 0, NULL, 0, 5385 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 5386 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 5387 "clearing port%d power failed (%d 0x%x %d)", 5388 port, completion_reason, cb_flags, rval); 5389 5390 mutex_enter(HUBD_MUTEX(hubd)); 5391 5392 return (USB_FAILURE); 5393 } else { 5394 5395 mutex_enter(HUBD_MUTEX(hubd)); 5396 ASSERT(completion_reason == 0); 5397 hubd->h_port_state[port] &= ~PORT_STATUS_PPS; 5398 5399 return (USB_SUCCESS); 5400 } 5401 } 5402 5403 5404 /* 5405 * Search the database of user preferences and find out the preferred 5406 * configuration for this new device 5407 */ 5408 int 5409 hubd_select_device_configuration(hubd_t *hubd, usb_port_t port, 5410 dev_info_t *child_dip, usba_device_t *child_ud) 5411 { 5412 char *pathname = NULL; 5413 char *tmp_path = NULL; 5414 int user_conf; 5415 int pathlen; 5416 usb_dev_descr_t *usbdev_ptr; 5417 usba_configrec_t *user_pref; 5418 5419 mutex_enter(&child_ud->usb_mutex); 5420 usbdev_ptr = child_ud->usb_dev_descr; 5421 mutex_exit(&child_ud->usb_mutex); 5422 5423 /* try to get pathname for this device */ 5424 tmp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5425 (void) ddi_pathname(child_dip, tmp_path); 5426 5427 pathlen = strlen(tmp_path) + 32; 5428 pathname = kmem_zalloc(pathlen, KM_SLEEP); 5429 5430 /* 5431 * We haven't initialized the node and it doesn't have an address 5432 * yet. Append port number to the physical pathname 5433 */ 5434 (void) sprintf(pathname, "%s@%d", tmp_path, port); 5435 5436 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5437 "hubd_select_device_configuration: Device=%s\n\t" 5438 "Child path=%s", 5439 usba_get_mfg_prod_sn_str(child_dip, tmp_path, MAXPATHLEN), 5440 pathname); 5441 kmem_free(tmp_path, MAXPATHLEN); 5442 5443 5444 /* database search for user preferences */ 5445 user_pref = usba_devdb_get_user_preferences(usbdev_ptr->idVendor, 5446 usbdev_ptr->idProduct, child_ud->usb_serialno_str, pathname); 5447 5448 if (user_pref) { 5449 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5450 "hubd_select_device_configuration: " 5451 "usba_devdb_get_user_preferences " 5452 "return user_conf=%d\npreferred driver=%s path=%s", 5453 user_pref->cfg_index, user_pref->driver, 5454 user_pref->pathname); 5455 5456 user_conf = user_pref->cfg_index; 5457 5458 if (user_pref->driver) { 5459 mutex_enter(&child_ud->usb_mutex); 5460 child_ud->usb_preferred_driver = user_pref->driver; 5461 mutex_exit(&child_ud->usb_mutex); 5462 } 5463 } else { 5464 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5465 "hubd_select_device_configuration: No match found"); 5466 5467 /* select default configuration for this device */ 5468 user_conf = USBA_DEV_CONFIG_INDEX_UNDEFINED; 5469 } 5470 kmem_free(pathname, pathlen); 5471 5472 /* if the device has just one configuration, set default value */ 5473 if (usbdev_ptr->bNumConfigurations == 1) { 5474 user_conf = USB_DEV_DEFAULT_CONFIG_INDEX; 5475 } 5476 5477 return (user_conf); 5478 } 5479 5480 5481 /* 5482 * Retrieves config cloud for this configuration 5483 */ 5484 int 5485 hubd_get_this_config_cloud(hubd_t *hubd, dev_info_t *dip, 5486 usba_device_t *child_ud, uint16_t conf_index) 5487 { 5488 usb_cfg_descr_t *confdescr; 5489 mblk_t *pdata = NULL; 5490 int rval; 5491 size_t size; 5492 char *tmpbuf; 5493 usb_cr_t completion_reason; 5494 usb_cb_flags_t cb_flags; 5495 usb_pipe_handle_t def_ph; 5496 5497 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5498 "hubd_get_this_config_cloud: conf_index=%d", conf_index); 5499 5500 5501 /* alloc temporary space for config descriptor */ 5502 confdescr = (usb_cfg_descr_t *)kmem_zalloc(USB_CFG_DESCR_SIZE, 5503 KM_SLEEP); 5504 5505 /* alloc temporary space for string descriptor */ 5506 tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP); 5507 5508 def_ph = usba_get_dflt_pipe_handle(dip); 5509 5510 if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph, 5511 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD, 5512 USB_REQ_GET_DESCR, 5513 USB_DESCR_TYPE_SETUP_CFG | conf_index, 5514 0, 5515 USB_CFG_DESCR_SIZE, 5516 &pdata, 5517 0, 5518 &completion_reason, 5519 &cb_flags, 5520 0)) == USB_SUCCESS) { 5521 5522 /* this must be true since we didn't allow data underruns */ 5523 if (MBLKL(pdata) != USB_CFG_DESCR_SIZE) { 5524 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5525 "device returned incorrect configuration " 5526 "descriptor size."); 5527 5528 rval = USB_FAILURE; 5529 goto done; 5530 } 5531 5532 /* 5533 * Parse the configuration descriptor 5534 */ 5535 size = usb_parse_cfg_descr(pdata->b_rptr, 5536 MBLKL(pdata), confdescr, 5537 USB_CFG_DESCR_SIZE); 5538 5539 /* if parse cfg descr error, it should return failure */ 5540 if (size == USB_PARSE_ERROR) { 5541 5542 if (pdata->b_rptr[1] != USB_DESCR_TYPE_CFG) { 5543 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 5544 hubd->h_log_handle, 5545 "device returned incorrect " 5546 "configuration descriptor type."); 5547 } 5548 rval = USB_FAILURE; 5549 goto done; 5550 } 5551 5552 if (confdescr->wTotalLength < USB_CFG_DESCR_SIZE) { 5553 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 5554 hubd->h_log_handle, 5555 "device returned incorrect " 5556 "configuration descriptor size."); 5557 5558 rval = USB_FAILURE; 5559 goto done; 5560 } 5561 5562 freemsg(pdata); 5563 pdata = NULL; 5564 5565 /* Now fetch the complete config cloud */ 5566 if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph, 5567 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD, 5568 USB_REQ_GET_DESCR, 5569 USB_DESCR_TYPE_SETUP_CFG | conf_index, 5570 0, 5571 confdescr->wTotalLength, 5572 &pdata, 5573 0, 5574 &completion_reason, 5575 &cb_flags, 5576 0)) == USB_SUCCESS) { 5577 5578 if (MBLKL(pdata) != 5579 confdescr->wTotalLength) { 5580 5581 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 5582 hubd->h_log_handle, 5583 "device returned incorrect " 5584 "configuration descriptor."); 5585 5586 rval = USB_FAILURE; 5587 goto done; 5588 } 5589 5590 /* 5591 * copy config descriptor into usba_device 5592 */ 5593 mutex_enter(&child_ud->usb_mutex); 5594 child_ud->usb_cfg_array[conf_index] = 5595 kmem_alloc(confdescr->wTotalLength, KM_SLEEP); 5596 child_ud->usb_cfg_array_len[conf_index] = 5597 confdescr->wTotalLength; 5598 bcopy((caddr_t)pdata->b_rptr, 5599 (caddr_t)child_ud->usb_cfg_array[conf_index], 5600 confdescr->wTotalLength); 5601 mutex_exit(&child_ud->usb_mutex); 5602 5603 /* 5604 * retrieve string descriptor describing this 5605 * configuration 5606 */ 5607 if (confdescr->iConfiguration) { 5608 5609 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, 5610 hubd->h_log_handle, 5611 "Get conf str descr for config_index=%d", 5612 conf_index); 5613 5614 /* 5615 * Now fetch the string descriptor describing 5616 * this configuration 5617 */ 5618 if ((rval = usb_get_string_descr(dip, 5619 USB_LANG_ID, confdescr->iConfiguration, 5620 tmpbuf, USB_MAXSTRINGLEN)) == 5621 USB_SUCCESS) { 5622 size = strlen(tmpbuf); 5623 if (size > 0) { 5624 child_ud->usb_cfg_str_descr 5625 [conf_index] = (char *) 5626 kmem_zalloc(size + 1, 5627 KM_SLEEP); 5628 (void) strcpy( 5629 child_ud->usb_cfg_str_descr 5630 [conf_index], tmpbuf); 5631 } 5632 } else { 5633 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 5634 hubd->h_log_handle, 5635 "hubd_get_this_config_cloud: " 5636 "getting config string (%d) " 5637 "failed", 5638 confdescr->iConfiguration); 5639 5640 /* ignore this error */ 5641 rval = USB_SUCCESS; 5642 } 5643 } 5644 } 5645 } 5646 5647 done: 5648 if (rval != USB_SUCCESS) { 5649 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5650 "hubd_get_this_config_cloud: " 5651 "error in retrieving config descriptor for " 5652 "config index=%d rval=%d cr=%d", 5653 conf_index, rval, completion_reason); 5654 } 5655 5656 if (pdata) { 5657 freemsg(pdata); 5658 pdata = NULL; 5659 } 5660 5661 kmem_free(confdescr, USB_CFG_DESCR_SIZE); 5662 kmem_free(tmpbuf, USB_MAXSTRINGLEN); 5663 5664 return (rval); 5665 } 5666 5667 5668 /* 5669 * Retrieves the entire config cloud for all configurations of the device 5670 */ 5671 int 5672 hubd_get_all_device_config_cloud(hubd_t *hubd, dev_info_t *dip, 5673 usba_device_t *child_ud) 5674 { 5675 int rval = USB_SUCCESS; 5676 int ncfgs; 5677 uint16_t size; 5678 uint16_t conf_index; 5679 uchar_t **cfg_array; 5680 uint16_t *cfg_array_len; 5681 char **str_descr; 5682 5683 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5684 "hubd_get_all_device_config_cloud: Start"); 5685 5686 /* alloc pointer array for conf. descriptors */ 5687 mutex_enter(&child_ud->usb_mutex); 5688 ncfgs = child_ud->usb_n_cfgs; 5689 mutex_exit(&child_ud->usb_mutex); 5690 5691 size = sizeof (uchar_t *) * ncfgs; 5692 cfg_array = kmem_zalloc(size, KM_SLEEP); 5693 cfg_array_len = kmem_zalloc(ncfgs * sizeof (uint16_t), KM_SLEEP); 5694 str_descr = kmem_zalloc(size, KM_SLEEP); 5695 5696 mutex_enter(&child_ud->usb_mutex); 5697 child_ud->usb_cfg_array = cfg_array; 5698 child_ud->usb_cfg_array_len = cfg_array_len; 5699 child_ud->usb_cfg_array_length = size; 5700 child_ud->usb_cfg_array_len_length = ncfgs * sizeof (uint16_t); 5701 child_ud->usb_cfg_str_descr = str_descr; 5702 mutex_exit(&child_ud->usb_mutex); 5703 5704 /* Get configuration descriptor for each configuration */ 5705 for (conf_index = 0; (conf_index < ncfgs) && 5706 (rval == USB_SUCCESS); conf_index++) { 5707 5708 rval = hubd_get_this_config_cloud(hubd, dip, child_ud, 5709 conf_index); 5710 } 5711 5712 return (rval); 5713 } 5714 5715 5716 /* 5717 * hubd_ready_device: 5718 * Update the usba_device structure 5719 * Set the given configuration 5720 * Prepares the device node for driver to online. If an existing 5721 * OBP node is found, it will switch to the OBP node. 5722 */ 5723 dev_info_t * 5724 hubd_ready_device(hubd_t *hubd, dev_info_t *child_dip, usba_device_t *child_ud, 5725 uint_t config_index) 5726 { 5727 usb_cr_t completion_reason; 5728 usb_cb_flags_t cb_flags; 5729 size_t size; 5730 usb_cfg_descr_t config_descriptor; 5731 usb_pipe_handle_t def_ph; 5732 usba_pipe_handle_data_t *ph; 5733 5734 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5735 "hubd_ready_device: dip=0x%p, user_conf_index=%d", 5736 (void *)child_dip, config_index); 5737 5738 size = usb_parse_cfg_descr( 5739 child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE, 5740 &config_descriptor, USB_CFG_DESCR_SIZE); 5741 ASSERT(size == USB_CFG_DESCR_SIZE); 5742 5743 def_ph = usba_get_dflt_pipe_handle(child_dip); 5744 5745 /* Set the configuration */ 5746 (void) usb_pipe_sync_ctrl_xfer(child_dip, def_ph, 5747 USB_DEV_REQ_HOST_TO_DEV, 5748 USB_REQ_SET_CFG, /* bRequest */ 5749 config_descriptor.bConfigurationValue, /* wValue */ 5750 0, /* wIndex */ 5751 0, /* wLength */ 5752 NULL, 5753 0, 5754 &completion_reason, 5755 &cb_flags, 5756 0); 5757 5758 mutex_enter(&child_ud->usb_mutex); 5759 child_ud->usb_active_cfg_ndx = config_index; 5760 child_ud->usb_cfg = child_ud->usb_cfg_array[config_index]; 5761 child_ud->usb_cfg_length = config_descriptor.wTotalLength; 5762 child_ud->usb_cfg_value = config_descriptor.bConfigurationValue; 5763 child_ud->usb_n_ifs = config_descriptor.bNumInterfaces; 5764 child_ud->usb_dip = child_dip; 5765 5766 child_ud->usb_client_flags = kmem_zalloc( 5767 child_ud->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP); 5768 5769 child_ud->usb_client_attach_list = kmem_zalloc( 5770 child_ud->usb_n_ifs * 5771 sizeof (*child_ud->usb_client_attach_list), KM_SLEEP); 5772 5773 child_ud->usb_client_ev_cb_list = kmem_zalloc( 5774 child_ud->usb_n_ifs * 5775 sizeof (*child_ud->usb_client_ev_cb_list), KM_SLEEP); 5776 5777 mutex_exit(&child_ud->usb_mutex); 5778 5779 /* ready the device node */ 5780 child_dip = usba_ready_device_node(child_dip); 5781 5782 /* set owner of default pipe to child dip */ 5783 ph = usba_get_ph_data(def_ph); 5784 mutex_enter(&ph->p_mutex); 5785 mutex_enter(&ph->p_ph_impl->usba_ph_mutex); 5786 ph->p_ph_impl->usba_ph_dip = ph->p_dip = child_dip; 5787 mutex_exit(&ph->p_ph_impl->usba_ph_mutex); 5788 mutex_exit(&ph->p_mutex); 5789 5790 return (child_dip); 5791 } 5792 5793 5794 /* 5795 * hubd_create_child 5796 * - create child dip 5797 * - open default pipe 5798 * - get device descriptor 5799 * - set the address 5800 * - get device string descriptors 5801 * - get the entire config cloud (all configurations) of the device 5802 * - set user preferred configuration 5803 * - close default pipe 5804 * - load appropriate driver(s) 5805 */ 5806 static int 5807 hubd_create_child(dev_info_t *dip, 5808 hubd_t *hubd, 5809 usba_device_t *hubd_ud, 5810 usb_port_status_t port_status, 5811 usb_port_t port, 5812 int iteration) 5813 { 5814 dev_info_t *child_dip = NULL; 5815 usb_dev_descr_t usb_dev_descr; 5816 int rval; 5817 usba_device_t *child_ud = NULL; 5818 usba_device_t *parent_ud = NULL; 5819 usb_pipe_handle_t ph = NULL; /* default pipe handle */ 5820 mblk_t *pdata = NULL; 5821 usb_cr_t completion_reason; 5822 int user_conf_index; 5823 uint_t config_index; 5824 usb_cb_flags_t cb_flags; 5825 uchar_t address = 0; 5826 uint16_t length; 5827 size_t size; 5828 usb_addr_t parent_usb_addr; 5829 usb_port_t parent_usb_port; 5830 usba_device_t *parent_usba_dev; 5831 usb_port_status_t parent_port_status; 5832 5833 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5834 "hubd_create_child: port=%d", port); 5835 5836 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 5837 ASSERT(hubd->h_usba_devices[port] == NULL); 5838 5839 mutex_exit(HUBD_MUTEX(hubd)); 5840 5841 /* 5842 * create a dip which can be used to open the pipe. we set 5843 * the name after getting the descriptors from the device 5844 */ 5845 rval = usba_create_child_devi(dip, 5846 "device", /* driver name */ 5847 hubd_ud->usb_hcdi_ops, /* usba_hcdi ops */ 5848 hubd_ud->usb_root_hub_dip, 5849 port_status, /* low speed device */ 5850 child_ud, 5851 &child_dip); 5852 5853 if (rval != USB_SUCCESS) { 5854 5855 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5856 "usb_create_child_devi failed (%d)", rval); 5857 5858 goto fail_cleanup; 5859 } 5860 5861 child_ud = usba_get_usba_device(child_dip); 5862 ASSERT(child_ud != NULL); 5863 5864 parent_ud = hubd->h_usba_device; 5865 mutex_enter(&parent_ud->usb_mutex); 5866 parent_port_status = parent_ud->usb_port_status; 5867 5868 /* 5869 * To support split transactions, update address and port 5870 * of high speed hub to which given device is connected. 5871 */ 5872 if (parent_port_status == USBA_HIGH_SPEED_DEV) { 5873 parent_usba_dev = parent_ud; 5874 parent_usb_addr = parent_ud->usb_addr; 5875 parent_usb_port = port; 5876 } else { 5877 parent_usba_dev = parent_ud->usb_hs_hub_usba_dev; 5878 parent_usb_addr = parent_ud->usb_hs_hub_addr; 5879 parent_usb_port = parent_ud->usb_hs_hub_port; 5880 } 5881 mutex_exit(&parent_ud->usb_mutex); 5882 5883 mutex_enter(&child_ud->usb_mutex); 5884 address = child_ud->usb_addr; 5885 child_ud->usb_addr = 0; 5886 child_ud->usb_dev_descr = kmem_alloc(sizeof (usb_dev_descr_t), 5887 KM_SLEEP); 5888 bzero(&usb_dev_descr, sizeof (usb_dev_descr_t)); 5889 usb_dev_descr.bMaxPacketSize0 = 5890 (port_status == USBA_LOW_SPEED_DEV) ? 8 : 64; 5891 bcopy(&usb_dev_descr, child_ud->usb_dev_descr, 5892 sizeof (usb_dev_descr_t)); 5893 child_ud->usb_port = port; 5894 child_ud->usb_hs_hub_usba_dev = parent_usba_dev; 5895 child_ud->usb_hs_hub_addr = parent_usb_addr; 5896 child_ud->usb_hs_hub_port = parent_usb_port; 5897 mutex_exit(&child_ud->usb_mutex); 5898 5899 /* Open the default pipe */ 5900 if ((rval = usb_pipe_open(child_dip, NULL, NULL, 5901 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) { 5902 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5903 "usb_pipe_open failed (%d)", rval); 5904 5905 goto fail_cleanup; 5906 } 5907 5908 /* 5909 * get device descriptor 5910 */ 5911 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5912 "hubd_create_child: get device descriptor: 64 bytes"); 5913 5914 rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 5915 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD, 5916 USB_REQ_GET_DESCR, /* bRequest */ 5917 USB_DESCR_TYPE_SETUP_DEV, /* wValue */ 5918 0, /* wIndex */ 5919 64, /* wLength */ 5920 &pdata, USB_ATTRS_SHORT_XFER_OK, 5921 &completion_reason, &cb_flags, 0); 5922 5923 if ((rval != USB_SUCCESS) && 5924 (!((completion_reason == USB_CR_DATA_OVERRUN) && pdata))) { 5925 5926 /* 5927 * rval != USB_SUCCESS AND 5928 * completion_reason != USB_CR_DATA_OVERRUN 5929 * pdata could be != NULL. 5930 * Free pdata now to prevent memory leak. 5931 */ 5932 freemsg(pdata); 5933 pdata = NULL; 5934 5935 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5936 "hubd_create_child: get device descriptor: 8 bytes"); 5937 5938 rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 5939 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD, 5940 USB_REQ_GET_DESCR, /* bRequest */ 5941 USB_DESCR_TYPE_SETUP_DEV, /* wValue */ 5942 0, /* wIndex */ 5943 8, /* wLength */ 5944 &pdata, USB_ATTRS_NONE, 5945 &completion_reason, &cb_flags, 0); 5946 5947 if (rval != USB_SUCCESS) { 5948 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5949 "getting device descriptor failed (%s 0x%x %d)", 5950 usb_str_cr(completion_reason), cb_flags, rval); 5951 goto fail_cleanup; 5952 } 5953 } else { 5954 ASSERT(completion_reason == USB_CR_OK); 5955 } 5956 5957 ASSERT(pdata != NULL); 5958 5959 size = usb_parse_dev_descr( 5960 pdata->b_rptr, 5961 MBLKL(pdata), 5962 &usb_dev_descr, 5963 sizeof (usb_dev_descr_t)); 5964 5965 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5966 "parsing device descriptor returned %lu", size); 5967 5968 length = *(pdata->b_rptr); 5969 freemsg(pdata); 5970 pdata = NULL; 5971 if (size < 8) { 5972 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5973 "get device descriptor returned %lu bytes", size); 5974 5975 goto fail_cleanup; 5976 } 5977 5978 if (length < 8) { 5979 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5980 "fail enumeration: bLength=%d", length); 5981 5982 goto fail_cleanup; 5983 } 5984 5985 /* Set the address of the device */ 5986 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 5987 USB_DEV_REQ_HOST_TO_DEV, 5988 USB_REQ_SET_ADDRESS, /* bRequest */ 5989 address, /* wValue */ 5990 0, /* wIndex */ 5991 0, /* wLength */ 5992 NULL, 0, 5993 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 5994 char buffer[64]; 5995 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 5996 "setting address failed (cr=%s cb_flags=%s rval=%d)", 5997 usb_str_cr(completion_reason), 5998 usb_str_cb_flags(cb_flags, buffer, sizeof (buffer)), 5999 rval); 6000 6001 goto fail_cleanup; 6002 } 6003 6004 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6005 "set address 0x%x done", address); 6006 6007 /* now close the pipe for addr 0 */ 6008 usb_pipe_close(child_dip, ph, 6009 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 6010 6011 /* 6012 * This delay is important for the CATC hub to enumerate 6013 * But, avoid delay in the first iteration 6014 */ 6015 if (iteration) { 6016 delay(drv_usectohz(hubd_device_delay/100)); 6017 } 6018 6019 /* assign the address in the usba_device structure */ 6020 mutex_enter(&child_ud->usb_mutex); 6021 child_ud->usb_addr = address; 6022 child_ud->usb_no_cpr = 0; 6023 child_ud->usb_port_status = port_status; 6024 /* save this device descriptor */ 6025 bcopy(&usb_dev_descr, child_ud->usb_dev_descr, 6026 sizeof (usb_dev_descr_t)); 6027 child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations; 6028 mutex_exit(&child_ud->usb_mutex); 6029 6030 /* re-open the pipe for the device with the new address */ 6031 if ((rval = usb_pipe_open(child_dip, NULL, NULL, 6032 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) { 6033 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6034 "usb_pipe_open failed (%d)", rval); 6035 6036 goto fail_cleanup; 6037 } 6038 6039 /* 6040 * Get full device descriptor only if we have not received full 6041 * device descriptor earlier. 6042 */ 6043 if (size < length) { 6044 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6045 "hubd_create_child: get full device descriptor: " 6046 "%d bytes", length); 6047 6048 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 6049 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD, 6050 USB_REQ_GET_DESCR, /* bRequest */ 6051 USB_DESCR_TYPE_SETUP_DEV, /* wValue */ 6052 0, /* wIndex */ 6053 length, /* wLength */ 6054 &pdata, 0, 6055 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 6056 freemsg(pdata); 6057 pdata = NULL; 6058 6059 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, 6060 hubd->h_log_handle, 6061 "hubd_create_child: get full device descriptor: " 6062 "64 bytes"); 6063 6064 rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 6065 USB_DEV_REQ_DEV_TO_HOST | 6066 USB_DEV_REQ_TYPE_STANDARD, 6067 USB_REQ_GET_DESCR, /* bRequest */ 6068 USB_DESCR_TYPE_SETUP_DEV, /* wValue */ 6069 0, /* wIndex */ 6070 64, /* wLength */ 6071 &pdata, USB_ATTRS_SHORT_XFER_OK, 6072 &completion_reason, &cb_flags, 0); 6073 6074 /* we have to trust the data now */ 6075 if (pdata) { 6076 int len = *(pdata->b_rptr); 6077 6078 length = MBLKL(pdata); 6079 if (length < len) { 6080 6081 goto fail_cleanup; 6082 } 6083 } else if (rval != USB_SUCCESS) { 6084 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 6085 hubd->h_log_handle, 6086 "getting device descriptor failed " 6087 "(%d 0x%x %d)", 6088 completion_reason, cb_flags, rval); 6089 6090 goto fail_cleanup; 6091 } 6092 } 6093 6094 size = usb_parse_dev_descr( 6095 pdata->b_rptr, 6096 MBLKL(pdata), 6097 &usb_dev_descr, 6098 sizeof (usb_dev_descr_t)); 6099 6100 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6101 "parsing device descriptor returned %lu", size); 6102 6103 /* 6104 * For now, free the data 6105 * eventually, each configuration may need to be looked at 6106 */ 6107 freemsg(pdata); 6108 pdata = NULL; 6109 6110 if (size != USB_DEV_DESCR_SIZE) { 6111 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6112 "fail enumeration: descriptor size=%lu " 6113 "expected size=%u", size, USB_DEV_DESCR_SIZE); 6114 6115 goto fail_cleanup; 6116 } 6117 6118 /* 6119 * save the device descriptor in usba_device since it is needed 6120 * later on again 6121 */ 6122 mutex_enter(&child_ud->usb_mutex); 6123 bcopy(&usb_dev_descr, child_ud->usb_dev_descr, 6124 sizeof (usb_dev_descr_t)); 6125 child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations; 6126 mutex_exit(&child_ud->usb_mutex); 6127 } 6128 6129 if (usb_dev_descr.bNumConfigurations == 0) { 6130 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6131 "device descriptor:\n\t" 6132 "l=0x%x type=0x%x USB=0x%x class=0x%x subclass=0x%x\n\t" 6133 "protocol=0x%x maxpktsize=0x%x " 6134 "Vid=0x%x Pid=0x%x rel=0x%x\n\t" 6135 "Mfg=0x%x P=0x%x sn=0x%x #config=0x%x", 6136 usb_dev_descr.bLength, usb_dev_descr.bDescriptorType, 6137 usb_dev_descr.bcdUSB, usb_dev_descr.bDeviceClass, 6138 usb_dev_descr.bDeviceSubClass, 6139 usb_dev_descr.bDeviceProtocol, 6140 usb_dev_descr.bMaxPacketSize0, 6141 usb_dev_descr.idVendor, 6142 usb_dev_descr.idProduct, usb_dev_descr.bcdDevice, 6143 usb_dev_descr.iManufacturer, usb_dev_descr.iProduct, 6144 usb_dev_descr.iSerialNumber, 6145 usb_dev_descr.bNumConfigurations); 6146 goto fail_cleanup; 6147 } 6148 6149 6150 /* get the device string descriptor(s) */ 6151 usba_get_dev_string_descrs(child_dip, child_ud); 6152 6153 /* retrieve config cloud for all configurations */ 6154 rval = hubd_get_all_device_config_cloud(hubd, child_dip, child_ud); 6155 if (rval != USB_SUCCESS) { 6156 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6157 "failed to get configuration descriptor(s)"); 6158 6159 goto fail_cleanup; 6160 } 6161 6162 /* get the preferred configuration for this device */ 6163 user_conf_index = hubd_select_device_configuration(hubd, port, 6164 child_dip, child_ud); 6165 6166 /* Check if the user selected configuration index is in range */ 6167 if ((user_conf_index >= usb_dev_descr.bNumConfigurations) || 6168 (user_conf_index < 0)) { 6169 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6170 "Configuration index for device idVendor=%d " 6171 "idProduct=%d is=%d, and is out of range[0..%d]", 6172 usb_dev_descr.idVendor, usb_dev_descr.idProduct, 6173 user_conf_index, usb_dev_descr.bNumConfigurations - 1); 6174 6175 /* treat this as user didn't specify configuration */ 6176 user_conf_index = USBA_DEV_CONFIG_INDEX_UNDEFINED; 6177 } 6178 6179 6180 /* 6181 * Warn users of a performance hit if connecting a 6182 * High Speed behind a 1.1 hub, which is behind a 6183 * 2.0 port. 6184 */ 6185 if ((parent_port_status != USBA_HIGH_SPEED_DEV) && 6186 !(usba_is_root_hub(parent_ud->usb_dip)) && 6187 (parent_usb_addr)) { 6188 6189 /* 6190 * Now that we know the root port is a high speed port 6191 * and that the parent port is not a high speed port, 6192 * let's find out if the device itself is a high speed 6193 * device. If it is a high speed device, 6194 * USB_DESCR_TYPE_SETUP_DEV_QLF should return a value, 6195 * otherwise the command will fail. 6196 */ 6197 rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 6198 USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD, 6199 USB_REQ_GET_DESCR, /* bRequest */ 6200 USB_DESCR_TYPE_SETUP_DEV_QLF, /* wValue */ 6201 0, /* wIndex */ 6202 10, /* wLength */ 6203 &pdata, USB_ATTRS_SHORT_XFER_OK, 6204 &completion_reason, &cb_flags, 0); 6205 6206 if (pdata) { 6207 freemsg(pdata); 6208 pdata = NULL; 6209 } 6210 6211 /* 6212 * USB_DESCR_TYPE_SETUP_DEV_QLF query was successful 6213 * that means this is a high speed device behind a 6214 * high speed root hub, but running at full speed 6215 * because there is a full speed hub in the middle. 6216 */ 6217 if (rval == USB_SUCCESS) { 6218 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, 6219 hubd->h_log_handle, 6220 "Connecting a high speed device to a " 6221 "non high speed hub (port %d) will result " 6222 "in a loss of performance. Please connect " 6223 "the device to a high speed hub to get " 6224 "the maximum performance.", 6225 port); 6226 } 6227 } 6228 6229 /* 6230 * Now we try to online the device by attaching a driver 6231 * The following truth table illustrates the logic:- 6232 * Cfgndx Driver Action 6233 * 0 0 loop all configs for driver with full 6234 * compatible properties. 6235 * 0 1 set first configuration, 6236 * compatible prop = drivername. 6237 * 1 0 Set config, full compatible prop 6238 * 1 1 Set config, compatible prop = drivername. 6239 * 6240 * Note: 6241 * cfgndx = user_conf_index 6242 * Driver = usb_preferred_driver 6243 */ 6244 if (user_conf_index == USBA_DEV_CONFIG_INDEX_UNDEFINED) { 6245 if (child_ud->usb_preferred_driver) { 6246 /* 6247 * It is the job of the "preferred driver" to put the 6248 * device in the desired configuration. Till then 6249 * put the device in config index 0. 6250 */ 6251 if ((rval = usba_hubdi_check_power_budget(dip, child_ud, 6252 USB_DEV_DEFAULT_CONFIG_INDEX)) != USB_SUCCESS) { 6253 6254 goto fail_cleanup; 6255 } 6256 6257 child_dip = hubd_ready_device(hubd, child_dip, 6258 child_ud, USB_DEV_DEFAULT_CONFIG_INDEX); 6259 6260 /* 6261 * Assign the dip before onlining to avoid race 6262 * with busctl 6263 */ 6264 mutex_enter(HUBD_MUTEX(hubd)); 6265 hubd->h_children_dips[port] = child_dip; 6266 mutex_exit(HUBD_MUTEX(hubd)); 6267 6268 (void) usba_bind_driver(child_dip); 6269 } else { 6270 /* 6271 * loop through all the configurations to see if we 6272 * can find a driver for any one config. If not, set 6273 * the device in config_index 0 6274 */ 6275 rval = USB_FAILURE; 6276 for (config_index = 0; 6277 (config_index < usb_dev_descr.bNumConfigurations) && 6278 (rval != USB_SUCCESS); config_index++) { 6279 6280 child_dip = hubd_ready_device(hubd, child_dip, 6281 child_ud, config_index); 6282 6283 /* 6284 * Assign the dip before onlining to avoid race 6285 * with busctl 6286 */ 6287 mutex_enter(HUBD_MUTEX(hubd)); 6288 hubd->h_children_dips[port] = child_dip; 6289 mutex_exit(HUBD_MUTEX(hubd)); 6290 6291 rval = usba_bind_driver(child_dip); 6292 6293 /* 6294 * Normally power budget should be checked 6295 * before device is configured. A failure in 6296 * power budget checking will stop the device 6297 * from being configured with current 6298 * config_index and may enable the device to 6299 * be configured in another configuration. 6300 * This may break the user experience that a 6301 * device which previously worked in config 6302 * A now works in config B after power budget 6303 * control is enabled. To avoid such situation, 6304 * power budget checking is moved here and will 6305 * fail the child creation directly if config 6306 * A exceeds the power available. 6307 */ 6308 if (rval == USB_SUCCESS) { 6309 if ((usba_hubdi_check_power_budget(dip, 6310 child_ud, config_index)) != 6311 USB_SUCCESS) { 6312 6313 goto fail_cleanup; 6314 } 6315 } 6316 } 6317 if (rval != USB_SUCCESS) { 6318 6319 if ((usba_hubdi_check_power_budget(dip, 6320 child_ud, 0)) != USB_SUCCESS) { 6321 6322 goto fail_cleanup; 6323 } 6324 6325 child_dip = hubd_ready_device(hubd, child_dip, 6326 child_ud, 0); 6327 mutex_enter(HUBD_MUTEX(hubd)); 6328 hubd->h_children_dips[port] = child_dip; 6329 mutex_exit(HUBD_MUTEX(hubd)); 6330 } 6331 } /* end else loop all configs */ 6332 } else { 6333 6334 if ((usba_hubdi_check_power_budget(dip, child_ud, 6335 (uint_t)user_conf_index)) != USB_SUCCESS) { 6336 6337 goto fail_cleanup; 6338 } 6339 6340 child_dip = hubd_ready_device(hubd, child_dip, 6341 child_ud, (uint_t)user_conf_index); 6342 6343 /* 6344 * Assign the dip before onlining to avoid race 6345 * with busctl 6346 */ 6347 mutex_enter(HUBD_MUTEX(hubd)); 6348 hubd->h_children_dips[port] = child_dip; 6349 mutex_exit(HUBD_MUTEX(hubd)); 6350 6351 (void) usba_bind_driver(child_dip); 6352 } 6353 6354 usba_hubdi_decr_power_budget(dip, child_ud); 6355 6356 mutex_enter(HUBD_MUTEX(hubd)); 6357 if (hubd->h_usba_devices[port] == NULL) { 6358 hubd->h_usba_devices[port] = usba_get_usba_device(child_dip); 6359 } else { 6360 ASSERT(hubd->h_usba_devices[port] == 6361 usba_get_usba_device(child_dip)); 6362 } 6363 6364 return (USB_SUCCESS); 6365 6366 6367 fail_cleanup: 6368 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6369 "hubd_create_child: fail_cleanup"); 6370 6371 mutex_enter(HUBD_MUTEX(hubd)); 6372 hubd->h_children_dips[port] = NULL; 6373 mutex_exit(HUBD_MUTEX(hubd)); 6374 6375 if (pdata) { 6376 freemsg(pdata); 6377 } 6378 6379 if (ph) { 6380 usb_pipe_close(child_dip, ph, 6381 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 6382 } 6383 6384 if (child_dip) { 6385 int rval = usba_destroy_child_devi(child_dip, 6386 NDI_DEVI_REMOVE); 6387 if (rval != USB_SUCCESS) { 6388 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6389 "failure to remove child node"); 6390 } 6391 } 6392 6393 if (child_ud) { 6394 /* to make sure we free the address */ 6395 mutex_enter(&child_ud->usb_mutex); 6396 child_ud->usb_addr = address; 6397 ASSERT(child_ud->usb_ref_count == 0); 6398 mutex_exit(&child_ud->usb_mutex); 6399 6400 mutex_enter(HUBD_MUTEX(hubd)); 6401 if (hubd->h_usba_devices[port] == NULL) { 6402 mutex_exit(HUBD_MUTEX(hubd)); 6403 usba_free_usba_device(child_ud); 6404 } else { 6405 hubd_free_usba_device(hubd, hubd->h_usba_devices[port]); 6406 mutex_exit(HUBD_MUTEX(hubd)); 6407 } 6408 } 6409 6410 mutex_enter(HUBD_MUTEX(hubd)); 6411 6412 return (USB_FAILURE); 6413 } 6414 6415 6416 /* 6417 * hubd_delete_child: 6418 * - free usb address 6419 * - lookup child dips, there may be multiple on this port 6420 * - offline each child devi 6421 */ 6422 static int 6423 hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, boolean_t retry) 6424 { 6425 dev_info_t *child_dip; 6426 usba_device_t *usba_device; 6427 int rval = USB_SUCCESS; 6428 6429 child_dip = hubd->h_children_dips[port]; 6430 usba_device = hubd->h_usba_devices[port]; 6431 6432 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6433 "hubd_delete_child: port=%d, dip=0x%p usba_device=0x%p", 6434 port, (void *)child_dip, (void *)usba_device); 6435 6436 mutex_exit(HUBD_MUTEX(hubd)); 6437 if (child_dip) { 6438 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6439 "hubd_delete_child:\n\t" 6440 "dip = 0x%p (%s) at port %d", 6441 (void *)child_dip, ddi_node_name(child_dip), port); 6442 6443 if (usba_device) { 6444 usba_hubdi_incr_power_budget(hubd->h_dip, usba_device); 6445 } 6446 6447 rval = usba_destroy_child_devi(child_dip, flag); 6448 6449 if ((rval != USB_SUCCESS) && usba_is_hwa(child_dip)) { 6450 /* 6451 * This is only useful for HWA device node. 6452 * Since hwahc interface must hold hwarc interface 6453 * open until hwahc is detached, the first call to 6454 * ndi_devi_unconfig_one() can only offline hwahc 6455 * driver but not hwarc driver. Need to make a second 6456 * call to ndi_devi_unconfig_one() to make the hwarc 6457 * driver detach. 6458 */ 6459 rval = usba_destroy_child_devi(child_dip, flag); 6460 } 6461 6462 if ((rval == USB_SUCCESS) && (flag & NDI_DEVI_REMOVE)) { 6463 /* 6464 * if the child was still < DS_INITIALIZED 6465 * then our bus_unconfig was not called and 6466 * we have to zap the child here 6467 */ 6468 mutex_enter(HUBD_MUTEX(hubd)); 6469 if (hubd->h_children_dips[port] == child_dip) { 6470 usba_device_t *ud = 6471 hubd->h_usba_devices[port]; 6472 hubd->h_children_dips[port] = NULL; 6473 if (ud) { 6474 mutex_exit(HUBD_MUTEX(hubd)); 6475 6476 mutex_enter(&ud->usb_mutex); 6477 ud->usb_ref_count = 0; 6478 mutex_exit(&ud->usb_mutex); 6479 6480 usba_free_usba_device(ud); 6481 mutex_enter(HUBD_MUTEX(hubd)); 6482 hubd->h_usba_devices[port] = NULL; 6483 } 6484 } 6485 mutex_exit(HUBD_MUTEX(hubd)); 6486 } 6487 } 6488 6489 if ((rval != USB_SUCCESS) && retry) { 6490 6491 hubd_schedule_cleanup(usba_device->usb_root_hub_dip); 6492 } 6493 mutex_enter(HUBD_MUTEX(hubd)); 6494 6495 return (rval); 6496 } 6497 6498 6499 /* 6500 * hubd_free_usba_device: 6501 * free usb device structure unless it is associated with 6502 * the root hub which is handled differently 6503 */ 6504 static void 6505 hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device) 6506 { 6507 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6508 "hubd_free_usba_device: hubd=0x%p, usba_device=0x%p", 6509 (void *)hubd, (void *)usba_device); 6510 6511 if (usba_device && (usba_device->usb_addr != ROOT_HUB_ADDR)) { 6512 usb_port_t port = usba_device->usb_port; 6513 dev_info_t *dip = hubd->h_children_dips[port]; 6514 6515 #ifdef DEBUG 6516 if (dip) { 6517 ASSERT(i_ddi_node_state(dip) < DS_INITIALIZED); 6518 } 6519 #endif 6520 6521 port = usba_device->usb_port; 6522 hubd->h_usba_devices[port] = NULL; 6523 6524 mutex_exit(HUBD_MUTEX(hubd)); 6525 usba_free_usba_device(usba_device); 6526 mutex_enter(HUBD_MUTEX(hubd)); 6527 } 6528 } 6529 6530 6531 /* 6532 * event support 6533 * 6534 * busctl event support 6535 */ 6536 static int 6537 hubd_busop_get_eventcookie(dev_info_t *dip, 6538 dev_info_t *rdip, 6539 char *eventname, 6540 ddi_eventcookie_t *cookie) 6541 { 6542 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip); 6543 6544 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6545 "hubd_busop_get_eventcookie: dip=0x%p, rdip=0x%p, " 6546 "event=%s", (void *)dip, (void *)rdip, eventname); 6547 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6548 "(dip=%s%d, rdip=%s%d)", 6549 ddi_driver_name(dip), ddi_get_instance(dip), 6550 ddi_driver_name(rdip), ddi_get_instance(rdip)); 6551 6552 /* return event cookie, iblock cookie, and level */ 6553 return (ndi_event_retrieve_cookie(hubd->h_ndi_event_hdl, 6554 rdip, eventname, cookie, NDI_EVENT_NOPASS)); 6555 } 6556 6557 6558 static int 6559 hubd_busop_add_eventcall(dev_info_t *dip, 6560 dev_info_t *rdip, 6561 ddi_eventcookie_t cookie, 6562 void (*callback)(dev_info_t *dip, 6563 ddi_eventcookie_t cookie, void *arg, 6564 void *bus_impldata), 6565 void *arg, ddi_callback_id_t *cb_id) 6566 { 6567 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip); 6568 usb_port_t port = hubd_child_dip2port(hubd, rdip); 6569 6570 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6571 "hubd_busop_add_eventcall: dip=0x%p, rdip=0x%p " 6572 "cookie=0x%p, cb=0x%p, arg=0x%p", 6573 (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg); 6574 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6575 "(dip=%s%d, rdip=%s%d, event=%s)", 6576 ddi_driver_name(dip), ddi_get_instance(dip), 6577 ddi_driver_name(rdip), ddi_get_instance(rdip), 6578 ndi_event_cookie_to_name(hubd->h_ndi_event_hdl, cookie)); 6579 6580 /* Set flag on children registering events */ 6581 switch (ndi_event_cookie_to_tag(hubd->h_ndi_event_hdl, cookie)) { 6582 case USBA_EVENT_TAG_HOT_REMOVAL: 6583 mutex_enter(HUBD_MUTEX(hubd)); 6584 hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT; 6585 mutex_exit(HUBD_MUTEX(hubd)); 6586 6587 break; 6588 case USBA_EVENT_TAG_PRE_SUSPEND: 6589 mutex_enter(HUBD_MUTEX(hubd)); 6590 hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND; 6591 mutex_exit(HUBD_MUTEX(hubd)); 6592 6593 break; 6594 default: 6595 6596 break; 6597 } 6598 6599 /* add callback to our event set */ 6600 return (ndi_event_add_callback(hubd->h_ndi_event_hdl, 6601 rdip, cookie, callback, arg, NDI_SLEEP, cb_id)); 6602 } 6603 6604 6605 static int 6606 hubd_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id) 6607 { 6608 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip); 6609 ndi_event_callbacks_t *id = (ndi_event_callbacks_t *)cb_id; 6610 6611 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6612 "hubd_busop_remove_eventcall: dip=0x%p, rdip=0x%p " 6613 "cookie=0x%p", (void *)dip, (void *)id->ndi_evtcb_dip, 6614 (void *)id->ndi_evtcb_cookie); 6615 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6616 "(dip=%s%d, rdip=%s%d, event=%s)", 6617 ddi_driver_name(dip), ddi_get_instance(dip), 6618 ddi_driver_name(id->ndi_evtcb_dip), 6619 ddi_get_instance(id->ndi_evtcb_dip), 6620 ndi_event_cookie_to_name(hubd->h_ndi_event_hdl, 6621 id->ndi_evtcb_cookie)); 6622 6623 /* remove event registration from our event set */ 6624 return (ndi_event_remove_callback(hubd->h_ndi_event_hdl, cb_id)); 6625 } 6626 6627 6628 /* 6629 * event distribution 6630 * 6631 * hubd_do_callback: 6632 * Post this event to the specified child 6633 */ 6634 static void 6635 hubd_do_callback(hubd_t *hubd, dev_info_t *cdip, ddi_eventcookie_t cookie) 6636 { 6637 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6638 "hubd_do_callback"); 6639 6640 (void) ndi_event_do_callback(hubd->h_ndi_event_hdl, cdip, cookie, NULL); 6641 } 6642 6643 6644 /* 6645 * hubd_run_callbacks: 6646 * Send this event to all children 6647 */ 6648 static void 6649 hubd_run_callbacks(hubd_t *hubd, usba_event_t type) 6650 { 6651 usb_port_t port; 6652 6653 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6654 "hubd_run_callbacks"); 6655 6656 mutex_enter(HUBD_MUTEX(hubd)); 6657 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 6658 /* 6659 * the childen_dips list may have dips that have been 6660 * already deallocated. we only get a post_detach notification 6661 * but not a destroy notification 6662 */ 6663 if (hubd->h_children_dips[port]) { 6664 mutex_exit(HUBD_MUTEX(hubd)); 6665 hubd_post_event(hubd, port, type); 6666 mutex_enter(HUBD_MUTEX(hubd)); 6667 } 6668 } 6669 mutex_exit(HUBD_MUTEX(hubd)); 6670 } 6671 6672 6673 /* 6674 * hubd_post_event 6675 * post event to a child on the port depending on the type 6676 */ 6677 static void 6678 hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type) 6679 { 6680 int rval; 6681 dev_info_t *dip; 6682 usba_device_t *usba_device; 6683 ddi_eventcookie_t cookie, rm_cookie, suspend_cookie; 6684 6685 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6686 "hubd_post_event: port=%d event=%s", port, 6687 ndi_event_tag_to_name(hubd->h_ndi_event_hdl, type)); 6688 6689 cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, type); 6690 rm_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, 6691 USBA_EVENT_TAG_HOT_REMOVAL); 6692 suspend_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, 6693 USBA_EVENT_TAG_PRE_SUSPEND); 6694 6695 /* 6696 * Hotplug daemon may be attaching a driver that may be registering 6697 * event callbacks. So it already has got the device tree lock and 6698 * event handle mutex. So to prevent a deadlock while posting events, 6699 * we grab and release the locks in the same order. 6700 */ 6701 mutex_enter(HUBD_MUTEX(hubd)); 6702 dip = hubd->h_children_dips[port]; 6703 usba_device = hubd->h_usba_devices[port]; 6704 mutex_exit(HUBD_MUTEX(hubd)); 6705 6706 switch (type) { 6707 case USBA_EVENT_TAG_HOT_REMOVAL: 6708 /* Clear the registered event flag */ 6709 mutex_enter(HUBD_MUTEX(hubd)); 6710 hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_DISCONNECT; 6711 mutex_exit(HUBD_MUTEX(hubd)); 6712 6713 hubd_do_callback(hubd, dip, cookie); 6714 usba_persistent_pipe_close(usba_device); 6715 6716 /* 6717 * Mark the dip for deletion only after the driver has 6718 * seen the disconnect event to prevent cleanup thread 6719 * from stepping in between. 6720 */ 6721 mutex_enter(&(DEVI(dip)->devi_lock)); 6722 DEVI_SET_DEVICE_REMOVED(dip); 6723 mutex_exit(&(DEVI(dip)->devi_lock)); 6724 6725 break; 6726 case USBA_EVENT_TAG_PRE_SUSPEND: 6727 mutex_enter(HUBD_MUTEX(hubd)); 6728 hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_PRESUSPEND; 6729 mutex_exit(HUBD_MUTEX(hubd)); 6730 6731 hubd_do_callback(hubd, dip, cookie); 6732 /* 6733 * persistent pipe close for this event is taken care by the 6734 * caller after verfying that all children can suspend 6735 */ 6736 6737 break; 6738 case USBA_EVENT_TAG_HOT_INSERTION: 6739 /* 6740 * Check if this child has missed the disconnect event before 6741 * it registered for event callbacks 6742 */ 6743 mutex_enter(HUBD_MUTEX(hubd)); 6744 if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_DISCONNECT) { 6745 /* clear the flag and post disconnect event */ 6746 hubd->h_child_events[port] &= 6747 ~HUBD_CHILD_EVENT_DISCONNECT; 6748 mutex_exit(HUBD_MUTEX(hubd)); 6749 hubd_do_callback(hubd, dip, rm_cookie); 6750 usba_persistent_pipe_close(usba_device); 6751 mutex_enter(HUBD_MUTEX(hubd)); 6752 } 6753 mutex_exit(HUBD_MUTEX(hubd)); 6754 6755 /* 6756 * Mark the dip as reinserted to prevent cleanup thread 6757 * from stepping in. 6758 */ 6759 mutex_enter(&(DEVI(dip)->devi_lock)); 6760 DEVI_SET_DEVICE_REINSERTED(dip); 6761 mutex_exit(&(DEVI(dip)->devi_lock)); 6762 6763 rval = usba_persistent_pipe_open(usba_device); 6764 if (rval != USB_SUCCESS) { 6765 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 6766 hubd->h_log_handle, 6767 "failed to reopen all pipes on reconnect"); 6768 } 6769 6770 hubd_do_callback(hubd, dip, cookie); 6771 6772 /* 6773 * We might see a connect event only if hotplug thread for 6774 * disconnect event don't run in time. 6775 * Set the flag again, so we don't miss posting a 6776 * disconnect event. 6777 */ 6778 mutex_enter(HUBD_MUTEX(hubd)); 6779 hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT; 6780 mutex_exit(HUBD_MUTEX(hubd)); 6781 6782 break; 6783 case USBA_EVENT_TAG_POST_RESUME: 6784 /* 6785 * Check if this child has missed the pre-suspend event before 6786 * it registered for event callbacks 6787 */ 6788 mutex_enter(HUBD_MUTEX(hubd)); 6789 if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_PRESUSPEND) { 6790 /* clear the flag and post pre_suspend event */ 6791 hubd->h_port_state[port] &= 6792 ~HUBD_CHILD_EVENT_PRESUSPEND; 6793 mutex_exit(HUBD_MUTEX(hubd)); 6794 hubd_do_callback(hubd, dip, suspend_cookie); 6795 mutex_enter(HUBD_MUTEX(hubd)); 6796 } 6797 mutex_exit(HUBD_MUTEX(hubd)); 6798 6799 mutex_enter(&usba_device->usb_mutex); 6800 usba_device->usb_no_cpr = 0; 6801 mutex_exit(&usba_device->usb_mutex); 6802 6803 /* 6804 * Since the pipe has already been opened by hub 6805 * at DDI_RESUME time, there is no need for a 6806 * persistent pipe open 6807 */ 6808 hubd_do_callback(hubd, dip, cookie); 6809 6810 /* 6811 * Set the flag again, so we don't miss posting a 6812 * pre-suspend event. This enforces a tighter 6813 * dev_state model. 6814 */ 6815 mutex_enter(HUBD_MUTEX(hubd)); 6816 hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND; 6817 mutex_exit(HUBD_MUTEX(hubd)); 6818 break; 6819 } 6820 } 6821 6822 6823 /* 6824 * handling of events coming from above 6825 */ 6826 static int 6827 hubd_disconnect_event_cb(dev_info_t *dip) 6828 { 6829 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip); 6830 usb_port_t port, nports; 6831 usba_device_t *usba_dev; 6832 usba_event_t tag = USBA_EVENT_TAG_HOT_REMOVAL; 6833 int circ; 6834 6835 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6836 "hubd_disconnect_event_cb: tag=%d", tag); 6837 6838 ndi_devi_enter(dip, &circ); 6839 6840 mutex_enter(HUBD_MUTEX(hubd)); 6841 switch (hubd->h_dev_state) { 6842 case USB_DEV_ONLINE: 6843 case USB_DEV_PWRED_DOWN: 6844 hubd->h_dev_state = USB_DEV_DISCONNECTED; 6845 /* stop polling on the interrupt pipe */ 6846 hubd_stop_polling(hubd); 6847 6848 /* FALLTHROUGH */ 6849 case USB_DEV_SUSPENDED: 6850 /* we remain in this state */ 6851 mutex_exit(HUBD_MUTEX(hubd)); 6852 hubd_run_callbacks(hubd, tag); 6853 mutex_enter(HUBD_MUTEX(hubd)); 6854 6855 /* close all the open pipes of our children */ 6856 nports = hubd->h_hub_descr.bNbrPorts; 6857 for (port = 1; port <= nports; port++) { 6858 usba_dev = hubd->h_usba_devices[port]; 6859 if (usba_dev != NULL) { 6860 mutex_exit(HUBD_MUTEX(hubd)); 6861 usba_persistent_pipe_close(usba_dev); 6862 mutex_enter(HUBD_MUTEX(hubd)); 6863 } 6864 } 6865 6866 break; 6867 case USB_DEV_DISCONNECTED: 6868 /* avoid passing multiple disconnects to children */ 6869 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6870 "hubd_disconnect_event_cb: Already disconnected"); 6871 6872 break; 6873 default: 6874 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6875 "hubd_disconnect_event_cb: Illegal devstate=%d", 6876 hubd->h_dev_state); 6877 6878 break; 6879 } 6880 mutex_exit(HUBD_MUTEX(hubd)); 6881 6882 ndi_devi_exit(dip, circ); 6883 6884 return (USB_SUCCESS); 6885 } 6886 6887 6888 static int 6889 hubd_reconnect_event_cb(dev_info_t *dip) 6890 { 6891 int rval, circ; 6892 6893 ndi_devi_enter(dip, &circ); 6894 rval = hubd_restore_state_cb(dip); 6895 ndi_devi_exit(dip, circ); 6896 6897 return (rval); 6898 } 6899 6900 6901 /* 6902 * hubd_pre_suspend_event_cb 6903 * propogate event for binary compatibility of old drivers 6904 */ 6905 static int 6906 hubd_pre_suspend_event_cb(dev_info_t *dip) 6907 { 6908 int circ; 6909 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip); 6910 6911 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle, 6912 "hubd_pre_suspend_event_cb"); 6913 6914 /* disable hotplug thread */ 6915 mutex_enter(HUBD_MUTEX(hubd)); 6916 hubd->h_hotplug_thread++; 6917 hubd_stop_polling(hubd); 6918 6919 /* keep PM out till we see a cpr resume */ 6920 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0); 6921 mutex_exit(HUBD_MUTEX(hubd)); 6922 6923 ndi_devi_enter(dip, &circ); 6924 hubd_run_callbacks(hubd, USBA_EVENT_TAG_PRE_SUSPEND); 6925 ndi_devi_exit(dip, circ); 6926 6927 return (USB_SUCCESS); 6928 } 6929 6930 6931 /* 6932 * hubd_post_resume_event_cb 6933 * propogate event for binary compatibility of old drivers 6934 */ 6935 static int 6936 hubd_post_resume_event_cb(dev_info_t *dip) 6937 { 6938 int circ; 6939 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip); 6940 6941 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle, 6942 "hubd_post_resume_event_cb"); 6943 6944 ndi_devi_enter(dip, &circ); 6945 hubd_run_callbacks(hubd, USBA_EVENT_TAG_POST_RESUME); 6946 ndi_devi_exit(dip, circ); 6947 6948 mutex_enter(HUBD_MUTEX(hubd)); 6949 6950 /* enable PM */ 6951 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 6952 6953 /* allow hotplug thread */ 6954 hubd->h_hotplug_thread--; 6955 6956 /* start polling */ 6957 hubd_start_polling(hubd, 0); 6958 mutex_exit(HUBD_MUTEX(hubd)); 6959 6960 return (USB_SUCCESS); 6961 } 6962 6963 6964 /* 6965 * hubd_cpr_suspend 6966 * save the current state of the driver/device 6967 */ 6968 static int 6969 hubd_cpr_suspend(hubd_t *hubd) 6970 { 6971 usb_port_t port, nports; 6972 usba_device_t *usba_dev; 6973 uchar_t no_cpr = 0; 6974 int rval = USB_FAILURE; 6975 6976 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 6977 "hubd_cpr_suspend: Begin"); 6978 6979 /* Make sure device is powered up to save state. */ 6980 mutex_enter(HUBD_MUTEX(hubd)); 6981 hubd_pm_busy_component(hubd, hubd->h_dip, 0); 6982 mutex_exit(HUBD_MUTEX(hubd)); 6983 6984 /* bring the device to full power */ 6985 (void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR); 6986 mutex_enter(HUBD_MUTEX(hubd)); 6987 6988 switch (hubd->h_dev_state) { 6989 case USB_DEV_ONLINE: 6990 case USB_DEV_PWRED_DOWN: 6991 case USB_DEV_DISCONNECTED: 6992 /* find out if all our children have been quiesced */ 6993 nports = hubd->h_hub_descr.bNbrPorts; 6994 for (port = 1; (no_cpr == 0) && (port <= nports); port++) { 6995 usba_dev = hubd->h_usba_devices[port]; 6996 if (usba_dev != NULL) { 6997 mutex_enter(&usba_dev->usb_mutex); 6998 no_cpr += usba_dev->usb_no_cpr; 6999 mutex_exit(&usba_dev->usb_mutex); 7000 } 7001 } 7002 if (no_cpr > 0) { 7003 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 7004 "Children busy - can't checkpoint"); 7005 /* remain in same state to fail checkpoint */ 7006 7007 break; 7008 } else { 7009 /* 7010 * do not suspend if our hotplug thread 7011 * or the deathrow thread is active 7012 */ 7013 if ((hubd->h_hotplug_thread > 1) || 7014 (hubd->h_cleanup_active == B_TRUE)) { 7015 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, 7016 hubd->h_log_handle, 7017 "hotplug thread active - can't cpr"); 7018 /* remain in same state to fail checkpoint */ 7019 7020 break; 7021 } 7022 7023 /* quiesce ourselves now */ 7024 hubd->h_dev_state = USB_DEV_SUSPENDED; 7025 hubd_stop_polling(hubd); 7026 7027 /* close all the open pipes of our children */ 7028 for (port = 1; port <= nports; port++) { 7029 usba_dev = hubd->h_usba_devices[port]; 7030 if (usba_dev != NULL) { 7031 mutex_exit(HUBD_MUTEX(hubd)); 7032 usba_persistent_pipe_close(usba_dev); 7033 mutex_enter(HUBD_MUTEX(hubd)); 7034 } 7035 } 7036 /* 7037 * turn off power to all the ports so that we 7038 * don't see any spurious activity 7039 */ 7040 (void) hubd_disable_all_port_power(hubd); 7041 7042 /* 7043 * if we are the root hub, we close our pipes 7044 * ourselves. 7045 */ 7046 if (usba_is_root_hub(hubd->h_dip)) { 7047 mutex_exit(HUBD_MUTEX(hubd)); 7048 usba_persistent_pipe_close( 7049 usba_get_usba_device(hubd->h_dip)); 7050 mutex_enter(HUBD_MUTEX(hubd)); 7051 } 7052 rval = USB_SUCCESS; 7053 7054 break; 7055 } 7056 case USB_DEV_SUSPENDED: 7057 default: 7058 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 7059 "hubd_cpr_suspend: Illegal dev state=%d", 7060 hubd->h_dev_state); 7061 7062 break; 7063 } 7064 7065 hubd_pm_idle_component(hubd, hubd->h_dip, 0); 7066 mutex_exit(HUBD_MUTEX(hubd)); 7067 7068 return (rval); 7069 } 7070 7071 static void 7072 hubd_cpr_resume(dev_info_t *dip) 7073 { 7074 int rval, circ; 7075 7076 ndi_devi_enter(dip, &circ); 7077 /* 7078 * if we are the root hub, we open our pipes 7079 * ourselves. 7080 */ 7081 if (usba_is_root_hub(dip)) { 7082 rval = usba_persistent_pipe_open( 7083 usba_get_usba_device(dip)); 7084 ASSERT(rval == USB_SUCCESS); 7085 } 7086 (void) hubd_restore_state_cb(dip); 7087 ndi_devi_exit(dip, circ); 7088 } 7089 7090 7091 /* 7092 * hubd_restore_state_cb 7093 * Event callback to restore device state 7094 */ 7095 static int 7096 hubd_restore_state_cb(dev_info_t *dip) 7097 { 7098 hubd_t *hubd = (hubd_t *)hubd_get_soft_state(dip); 7099 7100 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 7101 "hubd_restore_state_cb: Begin"); 7102 7103 /* restore the state of this device */ 7104 hubd_restore_device_state(dip, hubd); 7105 7106 return (USB_SUCCESS); 7107 } 7108 7109 7110 /* 7111 * registering for events 7112 */ 7113 static int 7114 hubd_register_events(hubd_t *hubd) 7115 { 7116 int rval = USB_SUCCESS; 7117 7118 if (usba_is_root_hub(hubd->h_dip)) { 7119 hubd_register_cpr_callback(hubd); 7120 } else { 7121 rval = usb_register_event_cbs(hubd->h_dip, &hubd_events, 0); 7122 } 7123 7124 return (rval); 7125 } 7126 7127 7128 /* 7129 * hubd cpr callback related functions 7130 * 7131 * hubd_cpr_post_user_callb: 7132 * This function is called during checkpoint & resume - 7133 * 1. after user threads are stopped during checkpoint 7134 * 2. after kernel threads are resumed during resume 7135 */ 7136 /* ARGSUSED */ 7137 static boolean_t 7138 hubd_cpr_post_user_callb(void *arg, int code) 7139 { 7140 hubd_cpr_t *cpr_cb = (hubd_cpr_t *)arg; 7141 hubd_t *hubd = cpr_cb->statep; 7142 int retry = 0; 7143 7144 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle, 7145 "hubd_cpr_post_user_callb"); 7146 7147 switch (code) { 7148 case CB_CODE_CPR_CHKPT: 7149 USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle, 7150 "hubd_cpr_post_user_callb: CB_CODE_CPR_CHKPT"); 7151 7152 mutex_enter(HUBD_MUTEX(hubd)); 7153 7154 /* turn off deathrow thread */ 7155 hubd->h_cleanup_enabled = B_FALSE; 7156 7157 /* give up if deathrow thread doesn't exit */ 7158 while ((hubd->h_cleanup_active == B_TRUE) && (retry++ < 3)) { 7159 mutex_exit(HUBD_MUTEX(hubd)); 7160 delay(drv_usectohz(hubd_dip_cleanup_delay)); 7161 7162 USB_DPRINTF_L2(DPRINT_MASK_EVENTS, hubd->h_log_handle, 7163 "hubd_cpr_post_user_callb, waiting for " 7164 "deathrow thread to exit"); 7165 mutex_enter(HUBD_MUTEX(hubd)); 7166 } 7167 7168 mutex_exit(HUBD_MUTEX(hubd)); 7169 7170 /* save the state of the device */ 7171 (void) hubd_pre_suspend_event_cb(hubd->h_dip); 7172 7173 return (B_TRUE); 7174 case CB_CODE_CPR_RESUME: 7175 USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle, 7176 "hubd_cpr_post_user_callb: CB_CODE_CPR_RESUME"); 7177 7178 /* restore the state of the device */ 7179 (void) hubd_post_resume_event_cb(hubd->h_dip); 7180 7181 /* turn on deathrow thread */ 7182 mutex_enter(HUBD_MUTEX(hubd)); 7183 hubd->h_cleanup_enabled = B_TRUE; 7184 mutex_exit(HUBD_MUTEX(hubd)); 7185 7186 hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip); 7187 7188 return (B_TRUE); 7189 default: 7190 7191 return (B_FALSE); 7192 } 7193 7194 } 7195 7196 7197 /* register callback with cpr framework */ 7198 void 7199 hubd_register_cpr_callback(hubd_t *hubd) 7200 { 7201 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle, 7202 "hubd_register_cpr_callback"); 7203 7204 mutex_enter(HUBD_MUTEX(hubd)); 7205 hubd->h_cpr_cb = 7206 (hubd_cpr_t *)kmem_zalloc(sizeof (hubd_cpr_t), KM_SLEEP); 7207 mutex_exit(HUBD_MUTEX(hubd)); 7208 mutex_init(&hubd->h_cpr_cb->lockp, NULL, MUTEX_DRIVER, 7209 hubd->h_dev_data->dev_iblock_cookie); 7210 hubd->h_cpr_cb->statep = hubd; 7211 hubd->h_cpr_cb->cpr.cc_lockp = &hubd->h_cpr_cb->lockp; 7212 hubd->h_cpr_cb->cpr.cc_id = callb_add(hubd_cpr_post_user_callb, 7213 (void *)hubd->h_cpr_cb, CB_CL_CPR_POST_USER, "hubd"); 7214 } 7215 7216 7217 /* unregister callback with cpr framework */ 7218 void 7219 hubd_unregister_cpr_callback(hubd_t *hubd) 7220 { 7221 USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle, 7222 "hubd_unregister_cpr_callback"); 7223 7224 if (hubd->h_cpr_cb) { 7225 (void) callb_delete(hubd->h_cpr_cb->cpr.cc_id); 7226 mutex_destroy(&hubd->h_cpr_cb->lockp); 7227 mutex_enter(HUBD_MUTEX(hubd)); 7228 kmem_free(hubd->h_cpr_cb, sizeof (hubd_cpr_t)); 7229 mutex_exit(HUBD_MUTEX(hubd)); 7230 } 7231 } 7232 7233 7234 /* 7235 * Power management 7236 * 7237 * create the pm components required for power management 7238 */ 7239 static void 7240 hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd) 7241 { 7242 hub_power_t *hubpm; 7243 7244 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 7245 "hubd_create_pm_components: Begin"); 7246 7247 /* Allocate the state structure */ 7248 hubpm = kmem_zalloc(sizeof (hub_power_t), KM_SLEEP); 7249 7250 hubd->h_hubpm = hubpm; 7251 hubpm->hubp_hubd = hubd; 7252 hubpm->hubp_pm_capabilities = 0; 7253 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR; 7254 hubpm->hubp_time_at_full_power = ddi_get_time(); 7255 hubpm->hubp_min_pm_threshold = hubdi_min_pm_threshold; 7256 7257 /* alloc memory to save power states of children */ 7258 hubpm->hubp_child_pwrstate = (uint8_t *) 7259 kmem_zalloc(MAX_PORTS + 1, KM_SLEEP); 7260 7261 /* 7262 * if the enable remote wakeup fails 7263 * we still want to enable 7264 * parent notification so we can PM the children 7265 */ 7266 usb_enable_parent_notification(dip); 7267 7268 if (usb_handle_remote_wakeup(dip, 7269 USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) { 7270 uint_t pwr_states; 7271 7272 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 7273 "hubd_create_pm_components: " 7274 "Remote Wakeup Enabled"); 7275 7276 if (usb_create_pm_components(dip, &pwr_states) == 7277 USB_SUCCESS) { 7278 mutex_enter(HUBD_MUTEX(hubd)); 7279 hubpm->hubp_wakeup_enabled = 1; 7280 hubpm->hubp_pwr_states = (uint8_t)pwr_states; 7281 7282 /* we are busy now till end of the attach */ 7283 hubd_pm_busy_component(hubd, dip, 0); 7284 mutex_exit(HUBD_MUTEX(hubd)); 7285 7286 /* bring the device to full power */ 7287 (void) pm_raise_power(dip, 0, 7288 USB_DEV_OS_FULL_PWR); 7289 } 7290 } 7291 7292 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 7293 "hubd_create_pm_components: END"); 7294 } 7295 7296 7297 /* 7298 * Attachment point management 7299 */ 7300 /* ARGSUSED */ 7301 int 7302 usba_hubdi_open(dev_info_t *dip, dev_t *devp, int flags, int otyp, 7303 cred_t *credp) 7304 { 7305 hubd_t *hubd; 7306 7307 if (otyp != OTYP_CHR) 7308 return (EINVAL); 7309 7310 hubd = hubd_get_soft_state(dip); 7311 if (hubd == NULL) { 7312 return (ENXIO); 7313 } 7314 7315 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7316 "hubd_open:"); 7317 7318 mutex_enter(HUBD_MUTEX(hubd)); 7319 if ((flags & FEXCL) && (hubd->h_softstate & HUBD_SS_ISOPEN)) { 7320 mutex_exit(HUBD_MUTEX(hubd)); 7321 7322 return (EBUSY); 7323 } 7324 7325 hubd->h_softstate |= HUBD_SS_ISOPEN; 7326 mutex_exit(HUBD_MUTEX(hubd)); 7327 7328 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "opened"); 7329 7330 return (0); 7331 } 7332 7333 7334 /* ARGSUSED */ 7335 int 7336 usba_hubdi_close(dev_info_t *dip, dev_t dev, int flag, int otyp, 7337 cred_t *credp) 7338 { 7339 hubd_t *hubd; 7340 7341 if (otyp != OTYP_CHR) { 7342 return (EINVAL); 7343 } 7344 7345 hubd = hubd_get_soft_state(dip); 7346 7347 if (hubd == NULL) { 7348 return (ENXIO); 7349 } 7350 7351 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "hubd_close:"); 7352 7353 mutex_enter(HUBD_MUTEX(hubd)); 7354 hubd->h_softstate &= ~HUBD_SS_ISOPEN; 7355 mutex_exit(HUBD_MUTEX(hubd)); 7356 7357 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "closed"); 7358 7359 return (0); 7360 } 7361 7362 7363 /* 7364 * hubd_ioctl: cfgadm controls 7365 */ 7366 /* ARGSUSED */ 7367 int 7368 usba_hubdi_ioctl(dev_info_t *self, dev_t dev, int cmd, intptr_t arg, 7369 int mode, cred_t *credp, int *rvalp) 7370 { 7371 int rv = 0; 7372 char *msg; /* for messages */ 7373 hubd_t *hubd; 7374 usb_port_t port = 0; 7375 dev_info_t *child_dip = NULL; 7376 dev_info_t *rh_dip; 7377 devctl_ap_state_t ap_state; 7378 struct devctl_iocdata *dcp = NULL; 7379 usb_pipe_state_t prev_pipe_state = 0; 7380 int circ, rh_circ, prh_circ; 7381 7382 if ((hubd = hubd_get_soft_state(self)) == NULL) { 7383 7384 return (ENXIO); 7385 } 7386 7387 rh_dip = hubd->h_usba_device->usb_root_hub_dip; 7388 7389 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7390 "usba_hubdi_ioctl: " 7391 "cmd=%x, arg=%lx, mode=%x, cred=%p, rval=%p dev=0x%lx", 7392 cmd, arg, mode, (void *)credp, (void *)rvalp, dev); 7393 7394 /* read devctl ioctl data */ 7395 if ((cmd != DEVCTL_AP_CONTROL) && 7396 (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)) { 7397 7398 return (EFAULT); 7399 } 7400 7401 /* 7402 * make sure the hub is connected before trying any 7403 * of the following operations: 7404 * configure, connect, disconnect 7405 */ 7406 mutex_enter(HUBD_MUTEX(hubd)); 7407 7408 switch (cmd) { 7409 case DEVCTL_AP_DISCONNECT: 7410 case DEVCTL_AP_UNCONFIGURE: 7411 case DEVCTL_AP_CONFIGURE: 7412 if (hubd->h_dev_state == USB_DEV_DISCONNECTED) { 7413 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 7414 "hubd: already gone"); 7415 mutex_exit(HUBD_MUTEX(hubd)); 7416 if (dcp) { 7417 ndi_dc_freehdl(dcp); 7418 } 7419 7420 return (EIO); 7421 } 7422 7423 /* FALLTHROUGH */ 7424 case DEVCTL_AP_GETSTATE: 7425 if ((port = hubd_get_port_num(hubd, dcp)) == 0) { 7426 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 7427 "hubd: bad port"); 7428 mutex_exit(HUBD_MUTEX(hubd)); 7429 if (dcp) { 7430 ndi_dc_freehdl(dcp); 7431 } 7432 7433 return (EINVAL); 7434 } 7435 break; 7436 7437 case DEVCTL_AP_CONTROL: 7438 7439 break; 7440 default: 7441 mutex_exit(HUBD_MUTEX(hubd)); 7442 if (dcp) { 7443 ndi_dc_freehdl(dcp); 7444 } 7445 7446 return (ENOTTY); 7447 } 7448 7449 /* should not happen, just in case */ 7450 if (hubd->h_dev_state == USB_DEV_SUSPENDED) { 7451 mutex_exit(HUBD_MUTEX(hubd)); 7452 if (dcp) { 7453 ndi_dc_freehdl(dcp); 7454 } 7455 7456 return (EIO); 7457 } 7458 7459 if (hubd->h_reset_port[port]) { 7460 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7461 "This port is resetting, just return"); 7462 mutex_exit(HUBD_MUTEX(hubd)); 7463 if (dcp) { 7464 ndi_dc_freehdl(dcp); 7465 } 7466 7467 return (EIO); 7468 } 7469 7470 hubd_pm_busy_component(hubd, hubd->h_dip, 0); 7471 mutex_exit(HUBD_MUTEX(hubd)); 7472 7473 /* go full power */ 7474 (void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR); 7475 7476 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ); 7477 ndi_devi_enter(rh_dip, &rh_circ); 7478 ndi_devi_enter(hubd->h_dip, &circ); 7479 7480 mutex_enter(HUBD_MUTEX(hubd)); 7481 7482 hubd->h_hotplug_thread++; 7483 7484 /* stop polling if it was active */ 7485 if (hubd->h_ep1_ph) { 7486 mutex_exit(HUBD_MUTEX(hubd)); 7487 (void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state, 7488 USB_FLAGS_SLEEP); 7489 mutex_enter(HUBD_MUTEX(hubd)); 7490 7491 if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) { 7492 hubd_stop_polling(hubd); 7493 } 7494 } 7495 7496 switch (cmd) { 7497 case DEVCTL_AP_DISCONNECT: 7498 if (hubd_delete_child(hubd, port, 7499 NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS) { 7500 rv = EIO; 7501 } 7502 7503 break; 7504 case DEVCTL_AP_UNCONFIGURE: 7505 if (hubd_delete_child(hubd, port, 7506 NDI_UNCONFIG, B_FALSE) != USB_SUCCESS) { 7507 rv = EIO; 7508 } 7509 7510 break; 7511 case DEVCTL_AP_CONFIGURE: 7512 /* toggle port */ 7513 if (hubd_toggle_port(hubd, port) != USB_SUCCESS) { 7514 rv = EIO; 7515 7516 break; 7517 } 7518 7519 (void) hubd_handle_port_connect(hubd, port); 7520 child_dip = hubd_get_child_dip(hubd, port); 7521 mutex_exit(HUBD_MUTEX(hubd)); 7522 7523 ndi_devi_exit(hubd->h_dip, circ); 7524 ndi_devi_exit(rh_dip, rh_circ); 7525 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ); 7526 if (child_dip == NULL) { 7527 rv = EIO; 7528 } else { 7529 ndi_hold_devi(child_dip); 7530 if (ndi_devi_online(child_dip, 0) != NDI_SUCCESS) 7531 rv = EIO; 7532 ndi_rele_devi(child_dip); 7533 } 7534 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ); 7535 ndi_devi_enter(rh_dip, &rh_circ); 7536 ndi_devi_enter(hubd->h_dip, &circ); 7537 7538 mutex_enter(HUBD_MUTEX(hubd)); 7539 7540 break; 7541 case DEVCTL_AP_GETSTATE: 7542 switch (hubd_cfgadm_state(hubd, port)) { 7543 case HUBD_CFGADM_DISCONNECTED: 7544 /* port previously 'disconnected' by cfgadm */ 7545 ap_state.ap_rstate = AP_RSTATE_DISCONNECTED; 7546 ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED; 7547 ap_state.ap_condition = AP_COND_OK; 7548 7549 break; 7550 case HUBD_CFGADM_UNCONFIGURED: 7551 ap_state.ap_rstate = AP_RSTATE_CONNECTED; 7552 ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED; 7553 ap_state.ap_condition = AP_COND_OK; 7554 7555 break; 7556 case HUBD_CFGADM_CONFIGURED: 7557 ap_state.ap_rstate = AP_RSTATE_CONNECTED; 7558 ap_state.ap_ostate = AP_OSTATE_CONFIGURED; 7559 ap_state.ap_condition = AP_COND_OK; 7560 7561 break; 7562 case HUBD_CFGADM_STILL_REFERENCED: 7563 ap_state.ap_rstate = AP_RSTATE_EMPTY; 7564 ap_state.ap_ostate = AP_OSTATE_CONFIGURED; 7565 ap_state.ap_condition = AP_COND_UNUSABLE; 7566 7567 break; 7568 case HUBD_CFGADM_EMPTY: 7569 default: 7570 ap_state.ap_rstate = AP_RSTATE_EMPTY; 7571 ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED; 7572 ap_state.ap_condition = AP_COND_OK; 7573 7574 break; 7575 } 7576 7577 ap_state.ap_last_change = (time_t)-1; 7578 ap_state.ap_error_code = 0; 7579 ap_state.ap_in_transition = 0; 7580 7581 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7582 "DEVCTL_AP_GETSTATE: " 7583 "ostate=0x%x, rstate=0x%x, condition=0x%x", 7584 ap_state.ap_ostate, 7585 ap_state.ap_rstate, ap_state.ap_condition); 7586 7587 /* copy the return-AP-state information to the user space */ 7588 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 7589 rv = EFAULT; 7590 } 7591 7592 break; 7593 case DEVCTL_AP_CONTROL: 7594 { 7595 /* 7596 * Generic devctl for hardware-specific functionality. 7597 * For list of sub-commands see hubd_impl.h 7598 */ 7599 hubd_ioctl_data_t ioc; /* for 64 byte copies */ 7600 7601 /* copy user ioctl data in first */ 7602 #ifdef _MULTI_DATAMODEL 7603 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) { 7604 hubd_ioctl_data_32_t ioc32; 7605 7606 if (ddi_copyin((void *)arg, (void *)&ioc32, 7607 sizeof (ioc32), mode) != 0) { 7608 rv = EFAULT; 7609 7610 break; 7611 } 7612 ioc.cmd = (uint_t)ioc32.cmd; 7613 ioc.port = (uint_t)ioc32.port; 7614 ioc.get_size = (uint_t)ioc32.get_size; 7615 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 7616 ioc.bufsiz = (uint_t)ioc32.bufsiz; 7617 ioc.misc_arg = (uint_t)ioc32.misc_arg; 7618 } else 7619 #endif /* _MULTI_DATAMODEL */ 7620 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 7621 mode) != 0) { 7622 rv = EFAULT; 7623 7624 break; 7625 } 7626 7627 USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7628 "DEVCTL_AP_CONTROL: ioc: cmd=0x%x port=%d get_size=%d" 7629 "\n\tbuf=0x%p, bufsiz=%d, misc_arg=%d", ioc.cmd, 7630 ioc.port, ioc.get_size, (void *)ioc.buf, ioc.bufsiz, 7631 ioc.misc_arg); 7632 7633 /* 7634 * To avoid BE/LE and 32/64 issues, a get_size always 7635 * returns a 32-bit number. 7636 */ 7637 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 7638 rv = EINVAL; 7639 7640 break; 7641 } 7642 7643 switch (ioc.cmd) { 7644 case USB_DESCR_TYPE_DEV: 7645 msg = "DEVCTL_AP_CONTROL: GET_DEVICE_DESC"; 7646 if (ioc.get_size) { 7647 /* uint32 so this works 32/64 */ 7648 uint32_t size = sizeof (usb_dev_descr_t); 7649 7650 if (ddi_copyout((void *)&size, ioc.buf, 7651 ioc.bufsiz, mode) != 0) { 7652 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7653 hubd->h_log_handle, 7654 "%s: get_size copyout failed", msg); 7655 rv = EIO; 7656 7657 break; 7658 } 7659 } else { /* send out the actual descr */ 7660 usb_dev_descr_t *dev_descrp; 7661 7662 /* check child_dip */ 7663 if ((child_dip = hubd_get_child_dip(hubd, 7664 ioc.port)) == NULL) { 7665 rv = EINVAL; 7666 7667 break; 7668 } 7669 7670 dev_descrp = usb_get_dev_descr(child_dip); 7671 if (ioc.bufsiz != sizeof (*dev_descrp)) { 7672 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7673 hubd->h_log_handle, 7674 "%s: bufsize passed (%d) != sizeof " 7675 "usba_device_descr_t (%d)", msg, 7676 ioc.bufsiz, dev_descrp->bLength); 7677 rv = EINVAL; 7678 7679 break; 7680 } 7681 7682 if (ddi_copyout((void *)dev_descrp, 7683 ioc.buf, ioc.bufsiz, mode) != 0) { 7684 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7685 hubd->h_log_handle, 7686 "%s: copyout failed.", msg); 7687 rv = EIO; 7688 7689 break; 7690 } 7691 } 7692 break; 7693 case USB_DESCR_TYPE_STRING: 7694 { 7695 char *str; 7696 uint32_t size; 7697 usba_device_t *usba_device; 7698 7699 msg = "DEVCTL_AP_CONTROL: GET_STRING_DESCR"; 7700 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7701 "%s: string request: %d", msg, ioc.misc_arg); 7702 7703 /* recheck */ 7704 if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) == 7705 NULL) { 7706 rv = EINVAL; 7707 7708 break; 7709 } 7710 usba_device = usba_get_usba_device(child_dip); 7711 7712 switch (ioc.misc_arg) { 7713 case HUBD_MFG_STR: 7714 str = usba_device->usb_mfg_str; 7715 7716 break; 7717 case HUBD_PRODUCT_STR: 7718 str = usba_device->usb_product_str; 7719 7720 break; 7721 case HUBD_SERIALNO_STR: 7722 str = usba_device->usb_serialno_str; 7723 7724 break; 7725 case HUBD_CFG_DESCR_STR: 7726 mutex_enter(&usba_device->usb_mutex); 7727 str = usba_device->usb_cfg_str_descr[ 7728 usba_device->usb_active_cfg_ndx]; 7729 mutex_exit(&usba_device->usb_mutex); 7730 7731 break; 7732 default: 7733 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7734 hubd->h_log_handle, 7735 "%s: Invalid string request", msg); 7736 rv = EINVAL; 7737 7738 break; 7739 } /* end of switch */ 7740 7741 if (rv != 0) { 7742 7743 break; 7744 } 7745 7746 size = (str != NULL) ? strlen(str) + 1 : 0; 7747 if (ioc.get_size) { 7748 if (ddi_copyout((void *)&size, ioc.buf, 7749 ioc.bufsiz, mode) != 0) { 7750 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7751 hubd->h_log_handle, 7752 "%s: copyout of size failed.", msg); 7753 rv = EIO; 7754 7755 break; 7756 } 7757 } else { 7758 if (size == 0) { 7759 USB_DPRINTF_L3(DPRINT_MASK_CBOPS, 7760 hubd->h_log_handle, 7761 "%s: String is NULL", msg); 7762 rv = EINVAL; 7763 7764 break; 7765 } 7766 7767 if (ioc.bufsiz != size) { 7768 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7769 hubd->h_log_handle, 7770 "%s: string buf size wrong", msg); 7771 rv = EINVAL; 7772 7773 break; 7774 } 7775 7776 if (ddi_copyout((void *)str, ioc.buf, 7777 ioc.bufsiz, mode) != 0) { 7778 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7779 hubd->h_log_handle, 7780 "%s: copyout failed.", msg); 7781 rv = EIO; 7782 7783 break; 7784 } 7785 } 7786 break; 7787 } 7788 case HUBD_GET_CFGADM_NAME: 7789 { 7790 uint32_t name_len; 7791 const char *name; 7792 7793 /* recheck */ 7794 if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) == 7795 NULL) { 7796 rv = EINVAL; 7797 7798 break; 7799 } 7800 name = ddi_node_name(child_dip); 7801 if (name == NULL) { 7802 name = "unsupported"; 7803 } 7804 name_len = strlen(name) + 1; 7805 7806 msg = "DEVCTL_AP_CONTROL: HUBD_GET_CFGADM_NAME"; 7807 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7808 "%s: name=%s name_len=%d", msg, name, name_len); 7809 7810 if (ioc.get_size) { 7811 if (ddi_copyout((void *)&name_len, 7812 ioc.buf, ioc.bufsiz, mode) != 0) { 7813 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7814 hubd->h_log_handle, 7815 "%s: copyout of size failed", msg); 7816 rv = EIO; 7817 7818 break; 7819 } 7820 } else { 7821 if (ioc.bufsiz != name_len) { 7822 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7823 hubd->h_log_handle, 7824 "%s: string buf length wrong", msg); 7825 rv = EINVAL; 7826 7827 break; 7828 } 7829 7830 if (ddi_copyout((void *)name, ioc.buf, 7831 ioc.bufsiz, mode) != 0) { 7832 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7833 hubd->h_log_handle, 7834 "%s: copyout failed.", msg); 7835 rv = EIO; 7836 7837 break; 7838 } 7839 } 7840 7841 break; 7842 } 7843 7844 /* 7845 * Return the config index for the currently-configured 7846 * configuration. 7847 */ 7848 case HUBD_GET_CURRENT_CONFIG: 7849 { 7850 uint_t config_index; 7851 uint32_t size = sizeof (config_index); 7852 usba_device_t *usba_device; 7853 7854 msg = "DEVCTL_AP_CONTROL: GET_CURRENT_CONFIG"; 7855 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7856 "%s", msg); 7857 7858 /* 7859 * Return the config index for the configuration 7860 * currently in use. 7861 * Recheck if child_dip exists 7862 */ 7863 if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) == 7864 NULL) { 7865 rv = EINVAL; 7866 7867 break; 7868 } 7869 7870 usba_device = usba_get_usba_device(child_dip); 7871 mutex_enter(&usba_device->usb_mutex); 7872 config_index = usba_device->usb_active_cfg_ndx; 7873 mutex_exit(&usba_device->usb_mutex); 7874 7875 if (ioc.get_size) { 7876 if (ddi_copyout((void *)&size, 7877 ioc.buf, ioc.bufsiz, mode) != 0) { 7878 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7879 hubd->h_log_handle, 7880 "%s: copyout of size failed.", msg); 7881 rv = EIO; 7882 7883 break; 7884 } 7885 } else { 7886 if (ioc.bufsiz != size) { 7887 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7888 hubd->h_log_handle, 7889 "%s: buffer size wrong", msg); 7890 rv = EINVAL; 7891 7892 break; 7893 } 7894 if (ddi_copyout((void *)&config_index, 7895 ioc.buf, ioc.bufsiz, mode) != 0) { 7896 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7897 hubd->h_log_handle, 7898 "%s: copyout failed", msg); 7899 rv = EIO; 7900 } 7901 } 7902 7903 break; 7904 } 7905 case HUBD_GET_DEVICE_PATH: 7906 { 7907 char *path; 7908 uint32_t size; 7909 7910 msg = "DEVCTL_AP_CONTROL: GET_DEVICE_PATH"; 7911 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7912 "%s", msg); 7913 7914 /* Recheck if child_dip exists */ 7915 if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) == 7916 NULL) { 7917 rv = EINVAL; 7918 7919 break; 7920 } 7921 7922 /* ddi_pathname doesn't supply /devices, so we do. */ 7923 path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 7924 (void) strcpy(path, "/devices"); 7925 (void) ddi_pathname(child_dip, path + strlen(path)); 7926 size = strlen(path) + 1; 7927 7928 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7929 "%s: device path=%s size=%d", msg, path, size); 7930 7931 if (ioc.get_size) { 7932 if (ddi_copyout((void *)&size, 7933 ioc.buf, ioc.bufsiz, mode) != 0) { 7934 7935 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7936 hubd->h_log_handle, 7937 "%s: copyout of size failed.", msg); 7938 rv = EIO; 7939 } 7940 } else { 7941 if (ioc.bufsiz != size) { 7942 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7943 hubd->h_log_handle, 7944 "%s: buffer wrong size.", msg); 7945 rv = EINVAL; 7946 } else if (ddi_copyout((void *)path, 7947 ioc.buf, ioc.bufsiz, mode) != 0) { 7948 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7949 hubd->h_log_handle, 7950 "%s: copyout failed.", msg); 7951 rv = EIO; 7952 } 7953 } 7954 kmem_free(path, MAXPATHLEN); 7955 7956 break; 7957 } 7958 case HUBD_REFRESH_DEVDB: 7959 msg = "DEVCTL_AP_CONTROL: HUBD_REFRESH_DEVDB"; 7960 USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle, 7961 "%s", msg); 7962 7963 if ((rv = usba_devdb_refresh()) != USB_SUCCESS) { 7964 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, 7965 hubd->h_log_handle, 7966 "%s: Failed: %d", msg, rv); 7967 rv = EIO; 7968 } 7969 7970 break; 7971 default: 7972 rv = ENOTSUP; 7973 } /* end switch */ 7974 7975 break; 7976 } 7977 7978 default: 7979 rv = ENOTTY; 7980 } 7981 7982 if (dcp) { 7983 ndi_dc_freehdl(dcp); 7984 } 7985 7986 /* allow hotplug thread now */ 7987 hubd->h_hotplug_thread--; 7988 7989 if ((hubd->h_dev_state == USB_DEV_ONLINE) && 7990 hubd->h_ep1_ph && (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) { 7991 hubd_start_polling(hubd, 0); 7992 } 7993 mutex_exit(HUBD_MUTEX(hubd)); 7994 7995 ndi_devi_exit(hubd->h_dip, circ); 7996 ndi_devi_exit(rh_dip, rh_circ); 7997 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ); 7998 7999 mutex_enter(HUBD_MUTEX(hubd)); 8000 hubd_pm_idle_component(hubd, hubd->h_dip, 0); 8001 mutex_exit(HUBD_MUTEX(hubd)); 8002 8003 return (rv); 8004 } 8005 8006 8007 /* 8008 * Helper func used only to help construct the names for the attachment point 8009 * minor nodes. Used only in usba_hubdi_attach. 8010 * Returns whether it found ancestry or not (USB_SUCCESS if yes). 8011 * ports between the root hub and the device represented by dip. 8012 * E.g., "2.4.3.1" means this device is 8013 * plugged into port 1 of a hub that is 8014 * plugged into port 3 of a hub that is 8015 * plugged into port 4 of a hub that is 8016 * plugged into port 2 of the root hub. 8017 * NOTE: Max ap_id path len is HUBD_APID_NAMELEN (32 chars), which is 8018 * more than sufficient (as hubs are a max 6 levels deep, port needs 3 8019 * chars plus NULL each) 8020 */ 8021 void 8022 hubd_get_ancestry_str(hubd_t *hubd) 8023 { 8024 char ap_name[HUBD_APID_NAMELEN]; 8025 dev_info_t *pdip; 8026 hubd_t *phubd; 8027 usb_port_t port; 8028 8029 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 8030 "hubd_get_ancestry_str: hubd=0x%p", (void *)hubd); 8031 8032 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 8033 8034 /* 8035 * The function is extended to support wire adapter class 8036 * devices introduced by WUSB spec. The node name is no 8037 * longer "hub" only. 8038 * Generate the ap_id str based on the parent and child 8039 * relationship instead of retrieving it from the hub 8040 * device path, which simplifies the algorithm. 8041 */ 8042 if (usba_is_root_hub(hubd->h_dip)) { 8043 hubd->h_ancestry_str[0] = '\0'; 8044 } else { 8045 port = hubd->h_usba_device->usb_port; 8046 mutex_exit(HUBD_MUTEX(hubd)); 8047 8048 pdip = ddi_get_parent(hubd->h_dip); 8049 /* 8050 * The parent of wire adapter device might be usb_mid. 8051 * Need to look further up for hub device 8052 */ 8053 if (strcmp(ddi_driver_name(pdip), "usb_mid") == 0) { 8054 pdip = ddi_get_parent(pdip); 8055 ASSERT(pdip != NULL); 8056 } 8057 8058 phubd = hubd_get_soft_state(pdip); 8059 8060 mutex_enter(HUBD_MUTEX(phubd)); 8061 (void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d", 8062 phubd->h_ancestry_str, port); 8063 mutex_exit(HUBD_MUTEX(phubd)); 8064 8065 mutex_enter(HUBD_MUTEX(hubd)); 8066 (void) strcpy(hubd->h_ancestry_str, ap_name); 8067 (void) strcat(hubd->h_ancestry_str, "."); 8068 } 8069 } 8070 8071 8072 /* Get which port to operate on. */ 8073 static usb_port_t 8074 hubd_get_port_num(hubd_t *hubd, struct devctl_iocdata *dcp) 8075 { 8076 int32_t port; 8077 8078 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 8079 8080 /* Get which port to operate on. */ 8081 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 8082 USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle, 8083 "hubd_get_port_num: port lookup failed"); 8084 port = 0; 8085 } 8086 8087 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 8088 "hubd_get_port_num: hubd=0x%p, port=%d", (void *)hubd, port); 8089 8090 return ((usb_port_t)port); 8091 } 8092 8093 8094 /* check if child still exists */ 8095 static dev_info_t * 8096 hubd_get_child_dip(hubd_t *hubd, usb_port_t port) 8097 { 8098 dev_info_t *child_dip = hubd->h_children_dips[port]; 8099 8100 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 8101 "hubd_get_child_dip: hubd=0x%p, port=%d", (void *)hubd, port); 8102 8103 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 8104 8105 return (child_dip); 8106 } 8107 8108 8109 /* 8110 * hubd_cfgadm_state: 8111 * 8112 * child_dip list port_state cfgadm_state 8113 * -------------- ---------- ------------ 8114 * != NULL connected configured or 8115 * unconfigured 8116 * != NULL not connected disconnect but 8117 * busy/still referenced 8118 * NULL connected logically disconnected 8119 * NULL not connected empty 8120 */ 8121 static uint_t 8122 hubd_cfgadm_state(hubd_t *hubd, usb_port_t port) 8123 { 8124 uint_t state; 8125 dev_info_t *child_dip = hubd_get_child_dip(hubd, port); 8126 8127 if (child_dip) { 8128 if (hubd->h_port_state[port] & PORT_STATUS_CCS) { 8129 /* 8130 * connected, now check if driver exists 8131 */ 8132 if (DEVI_IS_DEVICE_OFFLINE(child_dip) || 8133 !i_ddi_devi_attached(child_dip)) { 8134 state = HUBD_CFGADM_UNCONFIGURED; 8135 } else { 8136 state = HUBD_CFGADM_CONFIGURED; 8137 } 8138 } else { 8139 /* 8140 * this means that the dip is around for 8141 * a device that is still referenced but 8142 * has been yanked out. So the cfgadm info 8143 * for this state should be EMPTY (port empty) 8144 * and CONFIGURED (dip still valid). 8145 */ 8146 state = HUBD_CFGADM_STILL_REFERENCED; 8147 } 8148 } else { 8149 /* connected but no child dip */ 8150 if (hubd->h_port_state[port] & PORT_STATUS_CCS) { 8151 /* logically disconnected */ 8152 state = HUBD_CFGADM_DISCONNECTED; 8153 } else { 8154 /* physically disconnected */ 8155 state = HUBD_CFGADM_EMPTY; 8156 } 8157 } 8158 8159 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 8160 "hubd_cfgadm_state: hubd=0x%p, port=%d state=0x%x", 8161 (void *)hubd, port, state); 8162 8163 return (state); 8164 } 8165 8166 8167 /* 8168 * hubd_toggle_port: 8169 */ 8170 static int 8171 hubd_toggle_port(hubd_t *hubd, usb_port_t port) 8172 { 8173 usb_hub_descr_t *hub_descr; 8174 int wait; 8175 uint_t retry; 8176 uint16_t status; 8177 uint16_t change; 8178 8179 USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, 8180 "hubd_toggle_port: hubd=0x%p, port=%d", (void *)hubd, port); 8181 8182 if ((hubd_disable_port_power(hubd, port)) != USB_SUCCESS) { 8183 8184 return (USB_FAILURE); 8185 } 8186 8187 /* 8188 * see hubd_enable_all_port_power() which 8189 * requires longer delay for hubs. 8190 */ 8191 mutex_exit(HUBD_MUTEX(hubd)); 8192 delay(drv_usectohz(hubd_device_delay / 10)); 8193 mutex_enter(HUBD_MUTEX(hubd)); 8194 8195 hub_descr = &hubd->h_hub_descr; 8196 8197 /* 8198 * According to section 11.11 of USB, for hubs with no power 8199 * switches, bPwrOn2PwrGood is zero. But we wait for some 8200 * arbitrary time to enable power to become stable. 8201 * 8202 * If an hub supports port power swicthing, we need to wait 8203 * at least 20ms before accesing corresonding usb port. 8204 */ 8205 if ((hub_descr->wHubCharacteristics & 8206 HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) { 8207 wait = hubd_device_delay / 10; 8208 } else { 8209 wait = max(HUB_DEFAULT_POPG, 8210 hub_descr->bPwrOn2PwrGood) * 2 * 1000; 8211 } 8212 8213 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 8214 "hubd_toggle_port: popg=%d wait=%d", 8215 hub_descr->bPwrOn2PwrGood, wait); 8216 8217 retry = 0; 8218 8219 do { 8220 (void) hubd_enable_port_power(hubd, port); 8221 8222 mutex_exit(HUBD_MUTEX(hubd)); 8223 delay(drv_usectohz(wait)); 8224 mutex_enter(HUBD_MUTEX(hubd)); 8225 8226 /* Get port status */ 8227 (void) hubd_determine_port_status(hubd, port, 8228 &status, &change, 0); 8229 8230 /* For retry if any, use some extra delay */ 8231 wait = max(wait, hubd_device_delay / 10); 8232 8233 retry++; 8234 8235 } while ((!(status & PORT_STATUS_PPS)) && (retry < HUBD_PORT_RETRY)); 8236 8237 /* Print warning message if port has no power */ 8238 if (!(status & PORT_STATUS_PPS)) { 8239 8240 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 8241 "hubd_toggle_port: port %d power-on failed, " 8242 "port status 0x%x", port, status); 8243 8244 return (USB_FAILURE); 8245 } 8246 8247 return (USB_SUCCESS); 8248 } 8249 8250 8251 /* 8252 * hubd_init_power_budget: 8253 * Init power budget variables in hubd structure. According 8254 * to USB spec, the power budget rules are: 8255 * 1. local-powered hubs including root-hubs can supply 8256 * 500mA to each port at maximum 8257 * 2. two bus-powered hubs are not allowed to concatenate 8258 * 3. bus-powered hubs can supply 100mA to each port at 8259 * maximum, and the power consumed by all downstream 8260 * ports and the hub itself cannot exceed the max power 8261 * supplied by the upstream port, i.e., 500mA 8262 * The routine is only called during hub attach time 8263 */ 8264 static int 8265 hubd_init_power_budget(hubd_t *hubd) 8266 { 8267 uint16_t status = 0; 8268 usba_device_t *hubd_ud = NULL; 8269 size_t size; 8270 usb_cfg_descr_t cfg_descr; 8271 dev_info_t *pdip = NULL; 8272 hubd_t *phubd = NULL; 8273 8274 if (hubd->h_ignore_pwr_budget) { 8275 8276 return (USB_SUCCESS); 8277 } 8278 8279 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 8280 "hubd_init_power_budget:"); 8281 8282 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 8283 ASSERT(hubd->h_default_pipe != 0); 8284 mutex_exit(HUBD_MUTEX(hubd)); 8285 8286 /* get device status */ 8287 if ((usb_get_status(hubd->h_dip, hubd->h_default_pipe, 8288 HUB_GET_DEVICE_STATUS_TYPE, 8289 0, &status, 0)) != USB_SUCCESS) { 8290 mutex_enter(HUBD_MUTEX(hubd)); 8291 8292 return (USB_FAILURE); 8293 } 8294 8295 hubd_ud = usba_get_usba_device(hubd->h_dip); 8296 8297 size = usb_parse_cfg_descr(hubd_ud->usb_cfg, hubd_ud->usb_cfg_length, 8298 &cfg_descr, USB_CFG_DESCR_SIZE); 8299 8300 if (size != USB_CFG_DESCR_SIZE) { 8301 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle, 8302 "get hub configuration descriptor failed"); 8303 mutex_enter(HUBD_MUTEX(hubd)); 8304 8305 return (USB_FAILURE); 8306 } 8307 8308 mutex_enter(HUBD_MUTEX(hubd)); 8309 8310 hubd->h_local_pwr_capable = (cfg_descr.bmAttributes & 8311 USB_CFG_ATTR_SELFPWR); 8312 8313 if (hubd->h_local_pwr_capable) { 8314 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle, 8315 "hub is capable of local power"); 8316 } 8317 8318 hubd->h_local_pwr_on = (status & 8319 USB_DEV_SLF_PWRD_STATUS) && hubd->h_local_pwr_capable; 8320 8321 if (hubd->h_local_pwr_on) { 8322 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle, 8323 "hub is local-powered"); 8324 8325 hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD * 8326 USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT; 8327 } else { 8328 hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD * 8329 USB_LOW_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT; 8330 8331 hubd->h_pwr_left = (USB_PWR_UNIT_LOAD * 8332 USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT; 8333 8334 ASSERT(!usba_is_root_hub(hubd->h_dip)); 8335 8336 if (!usba_is_root_hub(hubd->h_dip)) { 8337 /* 8338 * two bus-powered hubs are not 8339 * allowed to be concatenated 8340 */ 8341 mutex_exit(HUBD_MUTEX(hubd)); 8342 8343 pdip = ddi_get_parent(hubd->h_dip); 8344 phubd = hubd_get_soft_state(pdip); 8345 ASSERT(phubd != NULL); 8346 8347 if (!phubd->h_ignore_pwr_budget) { 8348 mutex_enter(HUBD_MUTEX(phubd)); 8349 if (phubd->h_local_pwr_on == B_FALSE) { 8350 USB_DPRINTF_L1(DPRINT_MASK_HUB, 8351 hubd->h_log_handle, 8352 "two bus-powered hubs cannot " 8353 "be concatenated"); 8354 8355 mutex_exit(HUBD_MUTEX(phubd)); 8356 mutex_enter(HUBD_MUTEX(hubd)); 8357 8358 return (USB_FAILURE); 8359 } 8360 mutex_exit(HUBD_MUTEX(phubd)); 8361 } 8362 8363 mutex_enter(HUBD_MUTEX(hubd)); 8364 8365 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle, 8366 "hub is bus-powered"); 8367 } else { 8368 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle, 8369 "root-hub must be local-powered"); 8370 } 8371 8372 /* 8373 * Subtract the power consumed by the hub itself 8374 * and get the power that can be supplied to 8375 * downstream ports 8376 */ 8377 hubd->h_pwr_left -= 8378 hubd->h_hub_descr.bHubContrCurrent / 8379 USB_CFG_DESCR_PWR_UNIT; 8380 if (hubd->h_pwr_left < 0) { 8381 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle, 8382 "hubd->h_pwr_left is less than bHubContrCurrent, " 8383 "should fail"); 8384 8385 return (USB_FAILURE); 8386 } 8387 } 8388 8389 return (USB_SUCCESS); 8390 } 8391 8392 8393 /* 8394 * usba_hubdi_check_power_budget: 8395 * Check if the hub has enough power budget to allow a 8396 * child device to select a configuration of config_index. 8397 */ 8398 int 8399 usba_hubdi_check_power_budget(dev_info_t *dip, usba_device_t *child_ud, 8400 uint_t config_index) 8401 { 8402 int16_t pwr_left, pwr_limit, pwr_required; 8403 size_t size; 8404 usb_cfg_descr_t cfg_descr; 8405 hubd_t *hubd; 8406 8407 if ((hubd = hubd_get_soft_state(dip)) == NULL) { 8408 8409 return (USB_FAILURE); 8410 } 8411 8412 if (hubd->h_ignore_pwr_budget) { 8413 8414 return (USB_SUCCESS); 8415 } 8416 8417 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8418 "usba_hubdi_check_power_budget: " 8419 "dip=0x%p child_ud=0x%p conf_index=%d", (void *)dip, 8420 (void *)child_ud, config_index); 8421 8422 mutex_enter(HUBD_MUTEX(hubd)); 8423 pwr_limit = hubd->h_pwr_limit; 8424 if (hubd->h_local_pwr_on == B_FALSE) { 8425 pwr_left = hubd->h_pwr_left; 8426 pwr_limit = (pwr_limit <= pwr_left) ? pwr_limit : pwr_left; 8427 } 8428 mutex_exit(HUBD_MUTEX(hubd)); 8429 8430 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8431 "usba_hubdi_check_power_budget: " 8432 "available power is %dmA", pwr_limit * USB_CFG_DESCR_PWR_UNIT); 8433 8434 size = usb_parse_cfg_descr( 8435 child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE, 8436 &cfg_descr, USB_CFG_DESCR_SIZE); 8437 8438 if (size != USB_CFG_DESCR_SIZE) { 8439 USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8440 "get hub configuration descriptor failed"); 8441 8442 return (USB_FAILURE); 8443 } 8444 8445 pwr_required = cfg_descr.bMaxPower; 8446 8447 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8448 "usba_hubdi_check_power_budget: " 8449 "child bmAttributes=0x%x bMaxPower=%d " 8450 "with config_index=%d", cfg_descr.bmAttributes, 8451 pwr_required, config_index); 8452 8453 if (pwr_required > pwr_limit) { 8454 USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8455 "configuration %d for device %s %s at port %d " 8456 "exceeds power available for this port, please " 8457 "re-insert your device into another hub port which " 8458 "has enough power", 8459 config_index, 8460 child_ud->usb_mfg_str, 8461 child_ud->usb_product_str, 8462 child_ud->usb_port); 8463 8464 return (USB_FAILURE); 8465 } 8466 8467 return (USB_SUCCESS); 8468 } 8469 8470 8471 /* 8472 * usba_hubdi_incr_power_budget: 8473 * Increase the hub power budget value when a child device 8474 * is removed from a bus-powered hub port. 8475 */ 8476 void 8477 usba_hubdi_incr_power_budget(dev_info_t *dip, usba_device_t *child_ud) 8478 { 8479 uint16_t pwr_value; 8480 hubd_t *hubd = hubd_get_soft_state(dip); 8481 8482 ASSERT(hubd != NULL); 8483 8484 if (hubd->h_ignore_pwr_budget) { 8485 8486 return; 8487 } 8488 8489 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 8490 "usba_hubdi_incr_power_budget: " 8491 "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud); 8492 8493 mutex_enter(HUBD_MUTEX(hubd)); 8494 if (hubd->h_local_pwr_on == B_TRUE) { 8495 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 8496 "usba_hubdi_incr_power_budget: " 8497 "hub is local powered"); 8498 mutex_exit(HUBD_MUTEX(hubd)); 8499 8500 return; 8501 } 8502 mutex_exit(HUBD_MUTEX(hubd)); 8503 8504 mutex_enter(&child_ud->usb_mutex); 8505 if (child_ud->usb_pwr_from_hub == 0) { 8506 mutex_exit(&child_ud->usb_mutex); 8507 8508 return; 8509 } 8510 pwr_value = child_ud->usb_pwr_from_hub; 8511 mutex_exit(&child_ud->usb_mutex); 8512 8513 mutex_enter(HUBD_MUTEX(hubd)); 8514 hubd->h_pwr_left += pwr_value; 8515 8516 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 8517 "usba_hubdi_incr_power_budget: " 8518 "available power is %dmA, increased by %dmA", 8519 hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT, 8520 pwr_value * USB_CFG_DESCR_PWR_UNIT); 8521 8522 mutex_exit(HUBD_MUTEX(hubd)); 8523 8524 mutex_enter(&child_ud->usb_mutex); 8525 child_ud->usb_pwr_from_hub = 0; 8526 mutex_exit(&child_ud->usb_mutex); 8527 } 8528 8529 8530 /* 8531 * usba_hubdi_decr_power_budget: 8532 * Decrease the hub power budget value when a child device 8533 * is inserted to a bus-powered hub port. 8534 */ 8535 void 8536 usba_hubdi_decr_power_budget(dev_info_t *dip, usba_device_t *child_ud) 8537 { 8538 uint16_t pwr_value; 8539 size_t size; 8540 usb_cfg_descr_t cfg_descr; 8541 hubd_t *hubd = hubd_get_soft_state(dip); 8542 8543 ASSERT(hubd != NULL); 8544 8545 if (hubd->h_ignore_pwr_budget) { 8546 8547 return; 8548 } 8549 8550 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 8551 "usba_hubdi_decr_power_budget: " 8552 "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud); 8553 8554 mutex_enter(HUBD_MUTEX(hubd)); 8555 if (hubd->h_local_pwr_on == B_TRUE) { 8556 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 8557 "usba_hubdi_decr_power_budget: " 8558 "hub is local powered"); 8559 mutex_exit(HUBD_MUTEX(hubd)); 8560 8561 return; 8562 } 8563 mutex_exit(HUBD_MUTEX(hubd)); 8564 8565 mutex_enter(&child_ud->usb_mutex); 8566 if (child_ud->usb_pwr_from_hub > 0) { 8567 mutex_exit(&child_ud->usb_mutex); 8568 8569 return; 8570 } 8571 mutex_exit(&child_ud->usb_mutex); 8572 8573 size = usb_parse_cfg_descr( 8574 child_ud->usb_cfg, child_ud->usb_cfg_length, 8575 &cfg_descr, USB_CFG_DESCR_SIZE); 8576 ASSERT(size == USB_CFG_DESCR_SIZE); 8577 8578 mutex_enter(HUBD_MUTEX(hubd)); 8579 pwr_value = cfg_descr.bMaxPower; 8580 hubd->h_pwr_left -= pwr_value; 8581 ASSERT(hubd->h_pwr_left >= 0); 8582 8583 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 8584 "usba_hubdi_decr_power_budget: " 8585 "available power is %dmA, decreased by %dmA", 8586 hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT, 8587 pwr_value * USB_CFG_DESCR_PWR_UNIT); 8588 8589 mutex_exit(HUBD_MUTEX(hubd)); 8590 8591 mutex_enter(&child_ud->usb_mutex); 8592 child_ud->usb_pwr_from_hub = pwr_value; 8593 mutex_exit(&child_ud->usb_mutex); 8594 } 8595 8596 /* 8597 * hubd_wait_for_hotplug_exit: 8598 * Waiting for the exit of the running hotplug thread or ioctl thread. 8599 */ 8600 static int 8601 hubd_wait_for_hotplug_exit(hubd_t *hubd) 8602 { 8603 clock_t until = drv_usectohz(1000000); 8604 int rval; 8605 8606 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 8607 8608 if (hubd->h_hotplug_thread) { 8609 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8610 "waiting for hubd hotplug thread exit"); 8611 rval = cv_reltimedwait(&hubd->h_cv_hotplug_dev, 8612 &hubd->h_mutex, until, TR_CLOCK_TICK); 8613 8614 if ((rval <= 0) && (hubd->h_hotplug_thread)) { 8615 8616 return (USB_FAILURE); 8617 } 8618 } 8619 8620 return (USB_SUCCESS); 8621 } 8622 8623 /* 8624 * hubd_reset_thread: 8625 * handles the "USB_RESET_LVL_REATTACH" reset of usb device. 8626 * 8627 * - delete the child (force detaching the device and its children) 8628 * - reset the corresponding parent hub port 8629 * - create the child (force re-attaching the device and its children) 8630 */ 8631 static void 8632 hubd_reset_thread(void *arg) 8633 { 8634 hubd_reset_arg_t *hd_arg = (hubd_reset_arg_t *)arg; 8635 hubd_t *hubd = hd_arg->hubd; 8636 uint16_t reset_port = hd_arg->reset_port; 8637 uint16_t status, change; 8638 hub_power_t *hubpm; 8639 dev_info_t *hdip = hubd->h_dip; 8640 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip; 8641 dev_info_t *child_dip; 8642 boolean_t online_child = B_FALSE; 8643 int prh_circ, rh_circ, circ, devinst; 8644 char *devname; 8645 int i = 0; 8646 int rval = USB_FAILURE; 8647 8648 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8649 "hubd_reset_thread: started, hubd_reset_port = 0x%x", reset_port); 8650 8651 kmem_free(arg, sizeof (hubd_reset_arg_t)); 8652 8653 mutex_enter(HUBD_MUTEX(hubd)); 8654 8655 child_dip = hubd->h_children_dips[reset_port]; 8656 ASSERT(child_dip != NULL); 8657 8658 devname = (char *)ddi_driver_name(child_dip); 8659 devinst = ddi_get_instance(child_dip); 8660 8661 /* if our bus power entry point is active, quit the reset */ 8662 if (hubd->h_bus_pwr) { 8663 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8664 "%s%d is under bus power management, cannot be reset. " 8665 "Please disconnect and reconnect this device.", 8666 devname, devinst); 8667 8668 goto Fail; 8669 } 8670 8671 if (hubd_wait_for_hotplug_exit(hubd) == USB_FAILURE) { 8672 /* we got woken up because of a timeout */ 8673 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, 8674 hubd->h_log_handle, "Time out when resetting the device" 8675 " %s%d. Please disconnect and reconnect this device.", 8676 devname, devinst); 8677 8678 goto Fail; 8679 } 8680 8681 hubd->h_hotplug_thread++; 8682 8683 /* is this the root hub? */ 8684 if ((hdip == rh_dip) && 8685 (hubd->h_dev_state == USB_DEV_PWRED_DOWN)) { 8686 hubpm = hubd->h_hubpm; 8687 8688 /* mark the root hub as full power */ 8689 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR; 8690 hubpm->hubp_time_at_full_power = ddi_get_time(); 8691 mutex_exit(HUBD_MUTEX(hubd)); 8692 8693 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8694 "hubd_reset_thread: call pm_power_has_changed"); 8695 8696 (void) pm_power_has_changed(hdip, 0, 8697 USB_DEV_OS_FULL_PWR); 8698 8699 mutex_enter(HUBD_MUTEX(hubd)); 8700 hubd->h_dev_state = USB_DEV_ONLINE; 8701 } 8702 8703 mutex_exit(HUBD_MUTEX(hubd)); 8704 8705 /* 8706 * this ensures one reset activity per system at a time. 8707 * we enter the parent PCI node to have this serialization. 8708 * this also excludes ioctls and deathrow thread 8709 */ 8710 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ); 8711 ndi_devi_enter(rh_dip, &rh_circ); 8712 8713 /* exclude other threads */ 8714 ndi_devi_enter(hdip, &circ); 8715 mutex_enter(HUBD_MUTEX(hubd)); 8716 8717 /* 8718 * We need to make sure that the child is still online for a hotplug 8719 * thread could have inserted which detached the child. 8720 */ 8721 if (hubd->h_children_dips[reset_port]) { 8722 mutex_exit(HUBD_MUTEX(hubd)); 8723 /* First disconnect the device */ 8724 hubd_post_event(hubd, reset_port, USBA_EVENT_TAG_HOT_REMOVAL); 8725 8726 /* delete cached dv_node's but drop locks first */ 8727 ndi_devi_exit(hdip, circ); 8728 ndi_devi_exit(rh_dip, rh_circ); 8729 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ); 8730 8731 (void) devfs_clean(rh_dip, NULL, DV_CLEAN_FORCE); 8732 8733 /* 8734 * workaround only for storage device. When it's able to force 8735 * detach a driver, this code can be removed safely. 8736 * 8737 * If we're to reset storage device and the device is used, we 8738 * will wait at most extra 20s for applications to exit and 8739 * close the device. This is especially useful for HAL-based 8740 * applications. 8741 */ 8742 if ((strcmp(devname, "scsa2usb") == 0) && 8743 DEVI(child_dip)->devi_ref != 0) { 8744 while (i++ < hubdi_reset_delay) { 8745 mutex_enter(HUBD_MUTEX(hubd)); 8746 rval = hubd_delete_child(hubd, reset_port, 8747 NDI_DEVI_REMOVE, B_FALSE); 8748 mutex_exit(HUBD_MUTEX(hubd)); 8749 if (rval == USB_SUCCESS) 8750 break; 8751 8752 delay(drv_usectohz(1000000)); /* 1s */ 8753 } 8754 } 8755 8756 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ); 8757 ndi_devi_enter(rh_dip, &rh_circ); 8758 ndi_devi_enter(hdip, &circ); 8759 8760 mutex_enter(HUBD_MUTEX(hubd)); 8761 8762 /* Then force detaching the device */ 8763 if ((rval != USB_SUCCESS) && (hubd_delete_child(hubd, 8764 reset_port, NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS)) { 8765 USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8766 "%s%d cannot be reset due to other applications " 8767 "are using it, please first close these " 8768 "applications, then disconnect and reconnect" 8769 "the device.", devname, devinst); 8770 8771 mutex_exit(HUBD_MUTEX(hubd)); 8772 /* post a re-connect event */ 8773 hubd_post_event(hubd, reset_port, 8774 USBA_EVENT_TAG_HOT_INSERTION); 8775 mutex_enter(HUBD_MUTEX(hubd)); 8776 } else { 8777 (void) hubd_determine_port_status(hubd, reset_port, 8778 &status, &change, HUBD_ACK_ALL_CHANGES); 8779 8780 /* Reset the parent hubd port and create new child */ 8781 if (status & PORT_STATUS_CCS) { 8782 online_child |= (hubd_handle_port_connect(hubd, 8783 reset_port) == USB_SUCCESS); 8784 } 8785 } 8786 } 8787 8788 /* release locks so we can do a devfs_clean */ 8789 mutex_exit(HUBD_MUTEX(hubd)); 8790 8791 /* delete cached dv_node's but drop locks first */ 8792 ndi_devi_exit(hdip, circ); 8793 ndi_devi_exit(rh_dip, rh_circ); 8794 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ); 8795 8796 (void) devfs_clean(rh_dip, NULL, 0); 8797 8798 /* now check if any children need onlining */ 8799 if (online_child) { 8800 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8801 "hubd_reset_thread: onlining children"); 8802 8803 (void) ndi_devi_online(hubd->h_dip, 0); 8804 } 8805 8806 mutex_enter(HUBD_MUTEX(hubd)); 8807 8808 /* allow hotplug thread now */ 8809 hubd->h_hotplug_thread--; 8810 Fail: 8811 hubd_start_polling(hubd, 0); 8812 8813 /* mark this device as idle */ 8814 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 8815 8816 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 8817 "hubd_reset_thread: exit, %d", hubd->h_hotplug_thread); 8818 8819 hubd->h_reset_port[reset_port] = B_FALSE; 8820 8821 mutex_exit(HUBD_MUTEX(hubd)); 8822 8823 ndi_rele_devi(hdip); 8824 } 8825 8826 /* 8827 * hubd_check_same_device: 8828 * - open the default pipe of the device. 8829 * - compare the old and new descriptors of the device. 8830 * - close the default pipe. 8831 */ 8832 static int 8833 hubd_check_same_device(hubd_t *hubd, usb_port_t port) 8834 { 8835 dev_info_t *dip = hubd->h_children_dips[port]; 8836 usb_pipe_handle_t ph; 8837 int rval = USB_FAILURE; 8838 8839 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 8840 8841 mutex_exit(HUBD_MUTEX(hubd)); 8842 /* Open the default pipe to operate the device */ 8843 if (usb_pipe_open(dip, NULL, NULL, 8844 USB_FLAGS_SLEEP| USBA_FLAGS_PRIVILEGED, 8845 &ph) == USB_SUCCESS) { 8846 /* 8847 * Check that if the device's descriptors are different 8848 * from the values saved before the port reset. 8849 */ 8850 rval = usb_check_same_device(dip, 8851 hubd->h_log_handle, USB_LOG_L0, 8852 DPRINT_MASK_ALL, USB_CHK_ALL, NULL); 8853 8854 usb_pipe_close(dip, ph, USB_FLAGS_SLEEP | 8855 USBA_FLAGS_PRIVILEGED, NULL, NULL); 8856 } 8857 mutex_enter(HUBD_MUTEX(hubd)); 8858 8859 return (rval); 8860 } 8861 8862 /* 8863 * usba_hubdi_reset_device 8864 * Called by usb_reset_device to handle usb device reset. 8865 */ 8866 int 8867 usba_hubdi_reset_device(dev_info_t *dip, usb_dev_reset_lvl_t reset_level) 8868 { 8869 hubd_t *hubd; 8870 usb_port_t port = 0; 8871 dev_info_t *hdip; 8872 usb_pipe_state_t prev_pipe_state = 0; 8873 usba_device_t *usba_device; 8874 hubd_reset_arg_t *arg; 8875 int i, ph_open_cnt; 8876 int rval = USB_FAILURE; 8877 8878 if ((!dip) || usba_is_root_hub(dip)) { 8879 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 8880 "usba_hubdi_reset_device: NULL dip or root hub"); 8881 8882 return (USB_INVALID_ARGS); 8883 } 8884 8885 if (!usb_owns_device(dip)) { 8886 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 8887 "usba_hubdi_reset_device: Not owns the device"); 8888 8889 return (USB_INVALID_PERM); 8890 } 8891 8892 if ((reset_level != USB_RESET_LVL_REATTACH) && 8893 (reset_level != USB_RESET_LVL_DEFAULT)) { 8894 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 8895 "usba_hubdi_reset_device: Unknown flags"); 8896 8897 return (USB_INVALID_ARGS); 8898 } 8899 8900 if ((hdip = ddi_get_parent(dip)) == NULL) { 8901 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 8902 "usba_hubdi_reset_device: fail to get parent hub"); 8903 8904 return (USB_INVALID_ARGS); 8905 } 8906 8907 if ((hubd = hubd_get_soft_state(hdip)) == NULL) { 8908 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 8909 "usba_hubdi_reset_device: fail to get hub softstate"); 8910 8911 return (USB_INVALID_ARGS); 8912 } 8913 8914 mutex_enter(HUBD_MUTEX(hubd)); 8915 8916 /* make sure the hub is connected before trying any kinds of reset. */ 8917 if ((hubd->h_dev_state == USB_DEV_DISCONNECTED) || 8918 (hubd->h_dev_state == USB_DEV_SUSPENDED)) { 8919 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 8920 "usb_reset_device: the state %d of the hub/roothub " 8921 "associated to the device 0x%p is incorrect", 8922 hubd->h_dev_state, (void *)dip); 8923 mutex_exit(HUBD_MUTEX(hubd)); 8924 8925 return (USB_INVALID_ARGS); 8926 } 8927 8928 mutex_exit(HUBD_MUTEX(hubd)); 8929 8930 port = hubd_child_dip2port(hubd, dip); 8931 8932 mutex_enter(HUBD_MUTEX(hubd)); 8933 8934 if (hubd->h_reset_port[port]) { 8935 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 8936 "usb_reset_device: the corresponding port is resetting"); 8937 mutex_exit(HUBD_MUTEX(hubd)); 8938 8939 return (USB_SUCCESS); 8940 } 8941 8942 /* 8943 * For Default reset, client drivers should first close all the pipes 8944 * except default pipe before calling the function, also should not 8945 * call the function during interrupt context. 8946 */ 8947 if (reset_level == USB_RESET_LVL_DEFAULT) { 8948 usba_device = hubd->h_usba_devices[port]; 8949 mutex_exit(HUBD_MUTEX(hubd)); 8950 8951 if (servicing_interrupt()) { 8952 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 8953 "usb_reset_device: during interrput context, quit"); 8954 8955 return (USB_INVALID_CONTEXT); 8956 } 8957 /* Check if all the pipes have been closed */ 8958 for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) { 8959 if (usba_device->usb_ph_list[i].usba_ph_data) { 8960 ph_open_cnt++; 8961 break; 8962 } 8963 } 8964 if (ph_open_cnt) { 8965 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 8966 "usb_reset_device: %d pipes are still open", 8967 ph_open_cnt); 8968 8969 return (USB_BUSY); 8970 } 8971 mutex_enter(HUBD_MUTEX(hubd)); 8972 } 8973 8974 /* Don't perform reset while the device is detaching */ 8975 if (hubd->h_port_state[port] & HUBD_CHILD_DETACHING) { 8976 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 8977 "usb_reset_device: the device is detaching, " 8978 "cannot be reset"); 8979 mutex_exit(HUBD_MUTEX(hubd)); 8980 8981 return (USB_FAILURE); 8982 } 8983 8984 hubd->h_reset_port[port] = B_TRUE; 8985 hdip = hubd->h_dip; 8986 mutex_exit(HUBD_MUTEX(hubd)); 8987 8988 /* Don't allow hub detached during the reset */ 8989 ndi_hold_devi(hdip); 8990 8991 mutex_enter(HUBD_MUTEX(hubd)); 8992 hubd_pm_busy_component(hubd, hdip, 0); 8993 mutex_exit(HUBD_MUTEX(hubd)); 8994 /* go full power */ 8995 (void) pm_raise_power(hdip, 0, USB_DEV_OS_FULL_PWR); 8996 mutex_enter(HUBD_MUTEX(hubd)); 8997 8998 hubd->h_hotplug_thread++; 8999 9000 /* stop polling if it was active */ 9001 if (hubd->h_ep1_ph) { 9002 mutex_exit(HUBD_MUTEX(hubd)); 9003 (void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state, 9004 USB_FLAGS_SLEEP); 9005 mutex_enter(HUBD_MUTEX(hubd)); 9006 9007 if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) { 9008 hubd_stop_polling(hubd); 9009 } 9010 } 9011 9012 switch (reset_level) { 9013 case USB_RESET_LVL_REATTACH: 9014 mutex_exit(HUBD_MUTEX(hubd)); 9015 arg = (hubd_reset_arg_t *)kmem_zalloc( 9016 sizeof (hubd_reset_arg_t), KM_SLEEP); 9017 arg->hubd = hubd; 9018 arg->reset_port = port; 9019 mutex_enter(HUBD_MUTEX(hubd)); 9020 9021 if ((rval = usb_async_req(hdip, hubd_reset_thread, 9022 (void *)arg, 0)) == USB_SUCCESS) { 9023 hubd->h_hotplug_thread--; 9024 mutex_exit(HUBD_MUTEX(hubd)); 9025 9026 return (USB_SUCCESS); 9027 } else { 9028 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 9029 "Cannot create reset thread, the device %s%d failed" 9030 " to reset", ddi_driver_name(dip), 9031 ddi_get_instance(dip)); 9032 9033 kmem_free(arg, sizeof (hubd_reset_arg_t)); 9034 } 9035 9036 break; 9037 case USB_RESET_LVL_DEFAULT: 9038 /* 9039 * Reset hub port and then recover device's address, set back 9040 * device's configuration, hubd_handle_port_connect() will 9041 * handle errors happened during this process. 9042 */ 9043 if ((rval = hubd_handle_port_connect(hubd, port)) 9044 == USB_SUCCESS) { 9045 mutex_exit(HUBD_MUTEX(hubd)); 9046 /* re-open the default pipe */ 9047 rval = usba_persistent_pipe_open(usba_device); 9048 mutex_enter(HUBD_MUTEX(hubd)); 9049 if (rval != USB_SUCCESS) { 9050 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 9051 hubd->h_log_handle, "failed to reopen " 9052 "default pipe after reset, disable hub" 9053 "port for %s%d", ddi_driver_name(dip), 9054 ddi_get_instance(dip)); 9055 /* 9056 * Disable port to set out a hotplug thread 9057 * which will handle errors. 9058 */ 9059 (void) hubd_disable_port(hubd, port); 9060 } 9061 } 9062 9063 break; 9064 default: 9065 9066 break; 9067 } 9068 9069 /* allow hotplug thread now */ 9070 hubd->h_hotplug_thread--; 9071 9072 if ((hubd->h_dev_state == USB_DEV_ONLINE) && hubd->h_ep1_ph && 9073 (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) { 9074 hubd_start_polling(hubd, 0); 9075 } 9076 9077 hubd_pm_idle_component(hubd, hdip, 0); 9078 9079 /* Clear reset mark for the port. */ 9080 hubd->h_reset_port[port] = B_FALSE; 9081 9082 mutex_exit(HUBD_MUTEX(hubd)); 9083 9084 ndi_rele_devi(hdip); 9085 9086 return (rval); 9087 } 9088