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