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