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 (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved. 24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 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 NULL, /* 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 #define USB_HUB_INTEL_VID 0x8087 142 #define USB_HUB_INTEL_PID 0x0020 143 144 /* 145 * local variables 146 */ 147 static kmutex_t usba_hubdi_mutex; /* protects USBA HUB data structures */ 148 149 static usba_list_entry_t usba_hubdi_list; 150 151 usb_log_handle_t hubdi_log_handle; 152 uint_t hubdi_errlevel = USB_LOG_L4; 153 uint_t hubdi_errmask = (uint_t)-1; 154 uint8_t hubdi_min_pm_threshold = 5; /* seconds */ 155 uint8_t hubdi_reset_delay = 20; /* seconds */ 156 extern int modrootloaded; 157 158 /* 159 * initialize private data 160 */ 161 void 162 usba_hubdi_initialization() 163 { 164 hubdi_log_handle = usb_alloc_log_hdl(NULL, "hubdi", &hubdi_errlevel, 165 &hubdi_errmask, NULL, 0); 166 167 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle, 168 "usba_hubdi_initialization"); 169 170 mutex_init(&usba_hubdi_mutex, NULL, MUTEX_DRIVER, NULL); 171 172 usba_init_list(&usba_hubdi_list, NULL, NULL); 173 } 174 175 176 void 177 usba_hubdi_destroy() 178 { 179 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle, 180 "usba_hubdi_destroy"); 181 182 mutex_destroy(&usba_hubdi_mutex); 183 usba_destroy_list(&usba_hubdi_list); 184 185 usb_free_log_hdl(hubdi_log_handle); 186 } 187 188 189 /* 190 * Called by an HUB to attach an instance of the driver 191 * make this instance known to USBA 192 * the HUB should initialize usba_hubdi structure prior 193 * to calling this interface 194 */ 195 int 196 usba_hubdi_register(dev_info_t *dip, 197 uint_t flags) 198 { 199 usba_hubdi_t *hubdi = kmem_zalloc(sizeof (usba_hubdi_t), KM_SLEEP); 200 usba_device_t *usba_device = usba_get_usba_device(dip); 201 202 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle, 203 "usba_hubdi_register: %s", ddi_node_name(dip)); 204 205 hubdi->hubdi_dip = dip; 206 hubdi->hubdi_flags = flags; 207 208 usba_device->usb_hubdi = hubdi; 209 210 /* 211 * add this hubdi instance to the list of known hubdi's 212 */ 213 usba_init_list(&hubdi->hubdi_list, (usb_opaque_t)hubdi, 214 usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)-> 215 hcdi_iblock_cookie); 216 mutex_enter(&usba_hubdi_mutex); 217 usba_add_to_list(&usba_hubdi_list, &hubdi->hubdi_list); 218 mutex_exit(&usba_hubdi_mutex); 219 220 return (DDI_SUCCESS); 221 } 222 223 224 /* 225 * Called by an HUB to detach an instance of the driver 226 */ 227 int 228 usba_hubdi_unregister(dev_info_t *dip) 229 { 230 usba_device_t *usba_device = usba_get_usba_device(dip); 231 usba_hubdi_t *hubdi = usba_device->usb_hubdi; 232 233 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle, 234 "usba_hubdi_unregister: %s", ddi_node_name(dip)); 235 236 mutex_enter(&usba_hubdi_mutex); 237 (void) usba_rm_from_list(&usba_hubdi_list, &hubdi->hubdi_list); 238 mutex_exit(&usba_hubdi_mutex); 239 240 usba_destroy_list(&hubdi->hubdi_list); 241 242 kmem_free(hubdi, sizeof (usba_hubdi_t)); 243 244 return (DDI_SUCCESS); 245 } 246 247 248 /* 249 * misc bus routines currently not used 250 */ 251 /*ARGSUSED*/ 252 static int 253 usba_hubdi_map_fault(dev_info_t *dip, 254 dev_info_t *rdip, 255 struct hat *hat, 256 struct seg *seg, 257 caddr_t addr, 258 struct devpage *dp, 259 pfn_t pfn, 260 uint_t prot, 261 uint_t lock) 262 { 263 return (DDI_FAILURE); 264 } 265 266 267 /* 268 * root hub support. the root hub uses the same devi as the HCD 269 */ 270 int 271 usba_hubdi_bind_root_hub(dev_info_t *dip, 272 uchar_t *root_hub_config_descriptor, 273 size_t config_length, 274 usb_dev_descr_t *root_hub_device_descriptor) 275 { 276 usba_device_t *usba_device; 277 usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip); 278 hubd_t *root_hubd; 279 usb_pipe_handle_t ph = NULL; 280 dev_info_t *child = ddi_get_child(dip); 281 282 if (ndi_prop_create_boolean(DDI_DEV_T_NONE, dip, 283 "root-hub") != NDI_SUCCESS) { 284 285 return (USB_FAILURE); 286 } 287 288 usba_add_root_hub(dip); 289 290 root_hubd = kmem_zalloc(sizeof (hubd_t), KM_SLEEP); 291 292 /* 293 * create and initialize a usba_device structure 294 */ 295 usba_device = usba_alloc_usba_device(dip); 296 297 mutex_enter(&usba_device->usb_mutex); 298 usba_device->usb_hcdi_ops = hcdi->hcdi_ops; 299 usba_device->usb_cfg = root_hub_config_descriptor; 300 usba_device->usb_cfg_length = config_length; 301 usba_device->usb_dev_descr = root_hub_device_descriptor; 302 usba_device->usb_port = 1; 303 usba_device->usb_addr = ROOT_HUB_ADDR; 304 usba_device->usb_root_hubd = root_hubd; 305 usba_device->usb_cfg_array = kmem_zalloc(sizeof (uchar_t *), 306 KM_SLEEP); 307 usba_device->usb_cfg_array_length = sizeof (uchar_t *); 308 309 usba_device->usb_cfg_array_len = kmem_zalloc(sizeof (uint16_t), 310 KM_SLEEP); 311 usba_device->usb_cfg_array_len_length = sizeof (uint16_t); 312 313 usba_device->usb_cfg_array[0] = root_hub_config_descriptor; 314 usba_device->usb_cfg_array_len[0] = 315 sizeof (root_hub_config_descriptor); 316 317 usba_device->usb_cfg_str_descr = kmem_zalloc(sizeof (uchar_t *), 318 KM_SLEEP); 319 usba_device->usb_n_cfgs = 1; 320 usba_device->usb_n_ifs = 1; 321 usba_device->usb_dip = dip; 322 323 usba_device->usb_client_flags = kmem_zalloc( 324 usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP); 325 326 usba_device->usb_client_attach_list = kmem_zalloc( 327 usba_device->usb_n_ifs * 328 sizeof (*usba_device->usb_client_attach_list), KM_SLEEP); 329 330 usba_device->usb_client_ev_cb_list = kmem_zalloc( 331 usba_device->usb_n_ifs * 332 sizeof (*usba_device->usb_client_ev_cb_list), KM_SLEEP); 333 334 /* 335 * The bDeviceProtocol field of root hub device specifies, 336 * whether root hub is a High or Full speed usb device. 337 */ 338 if (root_hub_device_descriptor->bDeviceProtocol) { 339 usba_device->usb_port_status = USBA_HIGH_SPEED_DEV; 340 } else { 341 usba_device->usb_port_status = USBA_FULL_SPEED_DEV; 342 } 343 344 mutex_exit(&usba_device->usb_mutex); 345 346 usba_set_usba_device(dip, usba_device); 347 348 /* 349 * For the root hub the default pipe is not yet open 350 */ 351 if (usb_pipe_open(dip, NULL, NULL, 352 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != USB_SUCCESS) { 353 goto fail; 354 } 355 356 /* 357 * kill off all OBP children, they may not be fully 358 * enumerated 359 */ 360 while (child) { 361 dev_info_t *next = ddi_get_next_sibling(child); 362 (void) ddi_remove_child(child, 0); 363 child = next; 364 } 365 366 /* 367 * "attach" the root hub driver 368 */ 369 if (usba_hubdi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) { 370 goto fail; 371 } 372 373 return (USB_SUCCESS); 374 375 fail: 376 (void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub"); 377 378 usba_rem_root_hub(dip); 379 380 if (ph) { 381 usb_pipe_close(dip, ph, 382 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 383 } 384 385 kmem_free(usba_device->usb_cfg_array, 386 usba_device->usb_cfg_array_length); 387 kmem_free(usba_device->usb_cfg_array_len, 388 usba_device->usb_cfg_array_len_length); 389 390 kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *)); 391 392 usba_free_usba_device(usba_device); 393 394 usba_set_usba_device(dip, NULL); 395 if (root_hubd) { 396 kmem_free(root_hubd, sizeof (hubd_t)); 397 } 398 399 return (USB_FAILURE); 400 } 401 402 403 int 404 usba_hubdi_unbind_root_hub(dev_info_t *dip) 405 { 406 usba_device_t *usba_device; 407 408 /* was root hub attached? */ 409 if (!(usba_is_root_hub(dip))) { 410 411 /* return success anyway */ 412 return (USB_SUCCESS); 413 } 414 415 /* 416 * usba_hubdi_detach also closes the default pipe 417 * and removes properties so there is no need to 418 * do it here 419 */ 420 if (usba_hubdi_detach(dip, DDI_DETACH) != DDI_SUCCESS) { 421 422 if (DEVI_IS_ATTACHING(dip)) { 423 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 424 "failure to unbind root hub after attach failure"); 425 } 426 427 return (USB_FAILURE); 428 } 429 430 usba_device = usba_get_usba_device(dip); 431 432 kmem_free(usba_device->usb_root_hubd, sizeof (hubd_t)); 433 434 kmem_free(usba_device->usb_cfg_array, 435 usba_device->usb_cfg_array_length); 436 kmem_free(usba_device->usb_cfg_array_len, 437 usba_device->usb_cfg_array_len_length); 438 439 kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *)); 440 441 usba_free_usba_device(usba_device); 442 443 usba_rem_root_hub(dip); 444 445 (void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub"); 446 447 return (USB_SUCCESS); 448 } 449 450 451 /* 452 * Actual Hub Driver support code: 453 * shared by root hub and non-root hubs 454 */ 455 #include <sys/usb/usba/usbai_version.h> 456 457 /* Debugging support */ 458 uint_t hubd_errlevel = USB_LOG_L4; 459 uint_t hubd_errmask = (uint_t)DPRINT_MASK_ALL; 460 uint_t hubd_instance_debug = (uint_t)-1; 461 static uint_t hubdi_bus_config_debug = 0; 462 463 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel)) 464 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask)) 465 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug)) 466 467 _NOTE(SCHEME_PROTECTS_DATA("unique", msgb)) 468 _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info)) 469 470 471 /* 472 * local variables: 473 * 474 * Amount of time to wait between resetting the port and accessing 475 * the device. The value is in microseconds. 476 */ 477 static uint_t hubd_device_delay = 1000000; 478 479 /* 480 * enumeration retry 481 */ 482 #define HUBD_PORT_RETRY 5 483 static uint_t hubd_retry_enumerate = HUBD_PORT_RETRY; 484 485 /* 486 * Stale hotremoved device cleanup delay 487 */ 488 #define HUBD_STALE_DIP_CLEANUP_DELAY 5000000 489 static uint_t hubd_dip_cleanup_delay = HUBD_STALE_DIP_CLEANUP_DELAY; 490 491 /* 492 * retries for USB suspend and resume 493 */ 494 #define HUBD_SUS_RES_RETRY 2 495 496 void *hubd_statep; 497 498 /* 499 * prototypes 500 */ 501 static int hubd_cleanup(dev_info_t *dip, hubd_t *hubd); 502 static int hubd_check_ports(hubd_t *hubd); 503 504 static int hubd_open_intr_pipe(hubd_t *hubd); 505 static void hubd_start_polling(hubd_t *hubd, int always); 506 static void hubd_stop_polling(hubd_t *hubd); 507 static void hubd_close_intr_pipe(hubd_t *hubd); 508 509 static void hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *req); 510 static void hubd_exception_cb(usb_pipe_handle_t pipe, 511 usb_intr_req_t *req); 512 static void hubd_hotplug_thread(void *arg); 513 static void hubd_reset_thread(void *arg); 514 static int hubd_create_child(dev_info_t *dip, 515 hubd_t *hubd, 516 usba_device_t *usba_device, 517 usb_port_status_t port_status, 518 usb_port_t port, 519 int iteration); 520 521 static int hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, 522 boolean_t retry); 523 524 static int hubd_get_hub_descriptor(hubd_t *hubd); 525 526 static int hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status); 527 528 static int hubd_reset_port(hubd_t *hubd, usb_port_t port); 529 530 static int hubd_get_hub_status(hubd_t *hubd); 531 532 static int hubd_handle_port_connect(hubd_t *hubd, usb_port_t port); 533 534 static int hubd_disable_port(hubd_t *hubd, usb_port_t port); 535 536 static int hubd_enable_port(hubd_t *hubd, usb_port_t port); 537 static int hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port); 538 539 static int hubd_determine_port_status(hubd_t *hubd, usb_port_t port, 540 uint16_t *status, uint16_t *change, uint_t ack_flag); 541 542 static int hubd_enable_all_port_power(hubd_t *hubd); 543 static int hubd_disable_all_port_power(hubd_t *hubd); 544 static int hubd_disable_port_power(hubd_t *hubd, usb_port_t port); 545 static int hubd_enable_port_power(hubd_t *hubd, usb_port_t port); 546 547 static void hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device); 548 549 static int hubd_can_suspend(hubd_t *hubd); 550 static void hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd); 551 static int hubd_setdevaddr(hubd_t *hubd, usb_port_t port); 552 static void hubd_setdevconfig(hubd_t *hubd, usb_port_t port); 553 554 static int hubd_register_events(hubd_t *hubd); 555 static void hubd_do_callback(hubd_t *hubd, dev_info_t *dip, 556 ddi_eventcookie_t cookie); 557 static void hubd_run_callbacks(hubd_t *hubd, usba_event_t type); 558 static void hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type); 559 static void hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd); 560 561 static int hubd_disconnect_event_cb(dev_info_t *dip); 562 static int hubd_reconnect_event_cb(dev_info_t *dip); 563 static int hubd_pre_suspend_event_cb(dev_info_t *dip); 564 static int hubd_post_resume_event_cb(dev_info_t *dip); 565 static int hubd_cpr_suspend(hubd_t *hubd); 566 static void hubd_cpr_resume(dev_info_t *dip); 567 static int hubd_restore_state_cb(dev_info_t *dip); 568 static int hubd_check_same_device(hubd_t *hubd, usb_port_t port); 569 570 static int hubd_init_power_budget(hubd_t *hubd); 571 572 static ndi_event_definition_t hubd_ndi_event_defs[] = { 573 {USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL, 574 NDI_EVENT_POST_TO_ALL}, 575 {USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL, 576 NDI_EVENT_POST_TO_ALL}, 577 {USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL, 578 NDI_EVENT_POST_TO_ALL}, 579 {USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL, 580 NDI_EVENT_POST_TO_ALL} 581 }; 582 583 #define HUBD_N_NDI_EVENTS \ 584 (sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t)) 585 586 static ndi_event_set_t hubd_ndi_events = { 587 NDI_EVENTS_REV1, HUBD_N_NDI_EVENTS, hubd_ndi_event_defs}; 588 589 /* events received from parent */ 590 static usb_event_t hubd_events = { 591 hubd_disconnect_event_cb, 592 hubd_reconnect_event_cb, 593 hubd_pre_suspend_event_cb, 594 hubd_post_resume_event_cb 595 }; 596 597 598 /* 599 * hubd_get_soft_state() returns the hubd soft state 600 */ 601 hubd_t * 602 hubd_get_soft_state(dev_info_t *dip) 603 { 604 if (dip == NULL) { 605 606 return (NULL); 607 } 608 609 if (usba_is_root_hub(dip)) { 610 usba_device_t *usba_device = usba_get_usba_device(dip); 611 612 return (usba_device->usb_root_hubd); 613 } else { 614 int instance = ddi_get_instance(dip); 615 616 return (ddi_get_soft_state(hubd_statep, instance)); 617 } 618 } 619 620 621 /* 622 * PM support functions: 623 */ 624 /*ARGSUSED*/ 625 static void 626 hubd_pm_busy_component(hubd_t *hubd, dev_info_t *dip, int component) 627 { 628 if (hubd->h_hubpm != NULL) { 629 hubd->h_hubpm->hubp_busy_pm++; 630 mutex_exit(HUBD_MUTEX(hubd)); 631 if (pm_busy_component(dip, 0) != DDI_SUCCESS) { 632 mutex_enter(HUBD_MUTEX(hubd)); 633 hubd->h_hubpm->hubp_busy_pm--; 634 mutex_exit(HUBD_MUTEX(hubd)); 635 } 636 mutex_enter(HUBD_MUTEX(hubd)); 637 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 638 "hubd_pm_busy_component: %d", hubd->h_hubpm->hubp_busy_pm); 639 } 640 } 641 642 643 /*ARGSUSED*/ 644 static void 645 hubd_pm_idle_component(hubd_t *hubd, dev_info_t *dip, int component) 646 { 647 if (hubd->h_hubpm != NULL) { 648 mutex_exit(HUBD_MUTEX(hubd)); 649 if (pm_idle_component(dip, 0) == DDI_SUCCESS) { 650 mutex_enter(HUBD_MUTEX(hubd)); 651 ASSERT(hubd->h_hubpm->hubp_busy_pm > 0); 652 hubd->h_hubpm->hubp_busy_pm--; 653 mutex_exit(HUBD_MUTEX(hubd)); 654 } 655 mutex_enter(HUBD_MUTEX(hubd)); 656 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 657 "hubd_pm_idle_component: %d", hubd->h_hubpm->hubp_busy_pm); 658 } 659 } 660 661 662 /* 663 * track power level changes for children of this instance 664 */ 665 static void 666 hubd_set_child_pwrlvl(hubd_t *hubd, usb_port_t port, uint8_t power) 667 { 668 int old_power, new_power, pwr; 669 usb_port_t portno; 670 hub_power_t *hubpm; 671 672 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 673 "hubd_set_child_pwrlvl: port=%d power=%d", 674 port, power); 675 676 mutex_enter(HUBD_MUTEX(hubd)); 677 hubpm = hubd->h_hubpm; 678 679 old_power = 0; 680 for (portno = 1; portno <= hubd->h_hub_descr.bNbrPorts; portno++) { 681 old_power += hubpm->hubp_child_pwrstate[portno]; 682 } 683 684 /* assign the port power */ 685 pwr = hubd->h_hubpm->hubp_child_pwrstate[port]; 686 hubd->h_hubpm->hubp_child_pwrstate[port] = power; 687 new_power = old_power - pwr + power; 688 689 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 690 "hubd_set_child_pwrlvl: new_power=%d old_power=%d", 691 new_power, old_power); 692 693 if ((new_power > 0) && (old_power == 0)) { 694 /* we have the first child coming out of low power */ 695 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0); 696 } else if ((new_power == 0) && (old_power > 0)) { 697 /* we have the last child going to low power */ 698 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 699 } 700 mutex_exit(HUBD_MUTEX(hubd)); 701 } 702 703 704 /* 705 * given a child dip, locate its port number 706 */ 707 static usb_port_t 708 hubd_child_dip2port(hubd_t *hubd, dev_info_t *dip) 709 { 710 usb_port_t port; 711 712 mutex_enter(HUBD_MUTEX(hubd)); 713 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 714 if (hubd->h_children_dips[port] == dip) { 715 716 break; 717 } 718 } 719 ASSERT(port <= hubd->h_hub_descr.bNbrPorts); 720 mutex_exit(HUBD_MUTEX(hubd)); 721 722 return (port); 723 } 724 725 726 /* 727 * if the hub can be put into low power mode, return success 728 * NOTE: suspend here means going to lower power, not CPR suspend. 729 */ 730 static int 731 hubd_can_suspend(hubd_t *hubd) 732 { 733 hub_power_t *hubpm; 734 int total_power = 0; 735 usb_port_t port; 736 737 hubpm = hubd->h_hubpm; 738 739 if (DEVI_IS_DETACHING(hubd->h_dip)) { 740 741 return (USB_SUCCESS); 742 } 743 744 /* 745 * Don't go to lower power if haven't been at full power for enough 746 * time to let hotplug thread kickoff. 747 */ 748 if (gethrtime() < (hubpm->hubp_time_at_full_power + 749 hubpm->hubp_min_pm_threshold)) { 750 751 return (USB_FAILURE); 752 } 753 754 for (port = 1; (total_power == 0) && 755 (port <= hubd->h_hub_descr.bNbrPorts); port++) { 756 total_power += hubpm->hubp_child_pwrstate[port]; 757 } 758 759 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 760 "hubd_can_suspend: %d", total_power); 761 762 return (total_power ? USB_FAILURE : USB_SUCCESS); 763 } 764 765 766 /* 767 * resume port depending on current device state 768 */ 769 static int 770 hubd_resume_port(hubd_t *hubd, usb_port_t port) 771 { 772 int rval, retry; 773 usb_cr_t completion_reason; 774 usb_cb_flags_t cb_flags; 775 uint16_t status; 776 uint16_t change; 777 int retval = USB_FAILURE; 778 779 mutex_enter(HUBD_MUTEX(hubd)); 780 781 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 782 "hubd_resume_port: port=%d state=0x%x (%s)", port, 783 hubd->h_dev_state, usb_str_dev_state(hubd->h_dev_state)); 784 785 switch (hubd->h_dev_state) { 786 case USB_DEV_HUB_CHILD_PWRLVL: 787 /* 788 * This could be a bus ctl for a port other than the one 789 * that has a remote wakeup condition. So check. 790 */ 791 if ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0) { 792 /* the port isn't suspended, so don't resume */ 793 retval = USB_SUCCESS; 794 795 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 796 "hubd_resume_port: port=%d not suspended", port); 797 798 break; 799 } 800 /* 801 * Device has initiated a wakeup. 802 * Issue a ClearFeature(PortSuspend) 803 */ 804 mutex_exit(HUBD_MUTEX(hubd)); 805 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 806 hubd->h_default_pipe, 807 HUB_HANDLE_PORT_FEATURE_TYPE, 808 USB_REQ_CLEAR_FEATURE, 809 CFS_PORT_SUSPEND, 810 port, 811 0, NULL, 0, 812 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 813 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 814 "ClearFeature(PortSuspend) fails " 815 "rval=%d cr=%d cb=0x%x", rval, 816 completion_reason, cb_flags); 817 } 818 mutex_enter(HUBD_MUTEX(hubd)); 819 820 /* either way ack changes on the port */ 821 (void) hubd_determine_port_status(hubd, port, 822 &status, &change, PORT_CHANGE_PSSC); 823 retval = USB_SUCCESS; 824 825 break; 826 case USB_DEV_HUB_STATE_RECOVER: 827 /* 828 * When hubd's connect event callback posts a connect 829 * event to its child, it results in this busctl call 830 * which is valid 831 */ 832 /* FALLTHRU */ 833 case USB_DEV_ONLINE: 834 if (((hubd->h_port_state[port] & PORT_STATUS_CCS) == 0) || 835 ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0)) { 836 /* 837 * the port isn't suspended, or connected 838 * so don't resume 839 */ 840 retval = USB_SUCCESS; 841 842 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 843 "hubd_resume_port: port=%d not suspended", port); 844 845 break; 846 } 847 /* 848 * prevent kicking off the hotplug thread 849 */ 850 hubd->h_hotplug_thread++; 851 hubd_stop_polling(hubd); 852 853 /* Now ClearFeature(PortSuspend) */ 854 for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) { 855 mutex_exit(HUBD_MUTEX(hubd)); 856 rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 857 hubd->h_default_pipe, 858 HUB_HANDLE_PORT_FEATURE_TYPE, 859 USB_REQ_CLEAR_FEATURE, 860 CFS_PORT_SUSPEND, 861 port, 862 0, NULL, 0, 863 &completion_reason, &cb_flags, 0); 864 mutex_enter(HUBD_MUTEX(hubd)); 865 if (rval != USB_SUCCESS) { 866 USB_DPRINTF_L2(DPRINT_MASK_PM, 867 hubd->h_log_handle, 868 "ClearFeature(PortSuspend) fails" 869 "rval=%d cr=%d cb=0x%x", rval, 870 completion_reason, cb_flags); 871 } else { 872 /* 873 * As per spec section 11.9 and 7.1.7.7 874 * hub need to provide at least 20ms of 875 * resume signalling, and s/w provide 10ms of 876 * recovery time before accessing the port. 877 */ 878 mutex_exit(HUBD_MUTEX(hubd)); 879 delay(drv_usectohz(40000)); 880 mutex_enter(HUBD_MUTEX(hubd)); 881 (void) hubd_determine_port_status(hubd, port, 882 &status, &change, PORT_CHANGE_PSSC); 883 884 if ((status & PORT_STATUS_PSS) == 0) { 885 /* the port did finally resume */ 886 retval = USB_SUCCESS; 887 888 break; 889 } 890 } 891 } 892 893 /* allow hotplug thread again */ 894 hubd->h_hotplug_thread--; 895 hubd_start_polling(hubd, 0); 896 897 break; 898 case USB_DEV_DISCONNECTED: 899 /* Ignore - NO Operation */ 900 retval = USB_SUCCESS; 901 902 break; 903 case USB_DEV_SUSPENDED: 904 case USB_DEV_PWRED_DOWN: 905 default: 906 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 907 "Improper state for port Resume"); 908 909 break; 910 } 911 mutex_exit(HUBD_MUTEX(hubd)); 912 913 return (retval); 914 } 915 916 917 /* 918 * suspend port depending on device state 919 */ 920 static int 921 hubd_suspend_port(hubd_t *hubd, usb_port_t port) 922 { 923 int rval, retry; 924 int retval = USB_FAILURE; 925 usb_cr_t completion_reason; 926 usb_cb_flags_t cb_flags; 927 uint16_t status; 928 uint16_t change; 929 930 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 931 "hubd_suspend_port: port=%d", port); 932 933 mutex_enter(HUBD_MUTEX(hubd)); 934 935 switch (hubd->h_dev_state) { 936 case USB_DEV_HUB_STATE_RECOVER: 937 /* 938 * When hubd's connect event callback posts a connect 939 * event to its child, it results in this busctl call 940 * which is valid 941 */ 942 /* FALLTHRU */ 943 case USB_DEV_HUB_CHILD_PWRLVL: 944 /* 945 * When one child is resuming, the other could timeout 946 * and go to low power mode, which is valid 947 */ 948 /* FALLTHRU */ 949 case USB_DEV_ONLINE: 950 hubd->h_hotplug_thread++; 951 hubd_stop_polling(hubd); 952 953 /* 954 * Some devices start an unprovoked resume. According to spec, 955 * normal resume time for port is 10ms. Wait for double that 956 * time, then check to be sure port is really suspended. 957 */ 958 for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) { 959 /* Now SetFeature(PortSuspend) */ 960 mutex_exit(HUBD_MUTEX(hubd)); 961 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 962 hubd->h_default_pipe, 963 HUB_HANDLE_PORT_FEATURE_TYPE, 964 USB_REQ_SET_FEATURE, 965 CFS_PORT_SUSPEND, 966 port, 967 0, NULL, 0, 968 &completion_reason, &cb_flags, 0)) != 969 USB_SUCCESS) { 970 USB_DPRINTF_L2(DPRINT_MASK_PM, 971 hubd->h_log_handle, 972 "SetFeature(PortSuspend) fails" 973 "rval=%d cr=%d cb=0x%x", 974 rval, completion_reason, cb_flags); 975 } 976 977 /* 978 * some devices start an unprovoked resume 979 * wait and check port status after some time 980 */ 981 delay(drv_usectohz(20000)); 982 983 /* either ways ack changes on the port */ 984 mutex_enter(HUBD_MUTEX(hubd)); 985 (void) hubd_determine_port_status(hubd, port, 986 &status, &change, PORT_CHANGE_PSSC); 987 if (status & PORT_STATUS_PSS) { 988 /* the port is indeed suspended */ 989 retval = USB_SUCCESS; 990 991 break; 992 } else { 993 USB_DPRINTF_L0(DPRINT_MASK_PM, 994 hubd->h_log_handle, 995 "hubdi: port%d failed to be suspended!", 996 port); 997 } 998 } 999 1000 hubd->h_hotplug_thread--; 1001 hubd_start_polling(hubd, 0); 1002 1003 break; 1004 1005 case USB_DEV_DISCONNECTED: 1006 /* Ignore - No Operation */ 1007 retval = USB_SUCCESS; 1008 1009 break; 1010 1011 case USB_DEV_SUSPENDED: 1012 case USB_DEV_PWRED_DOWN: 1013 default: 1014 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 1015 "Improper state for port Suspend"); 1016 1017 break; 1018 } 1019 mutex_exit(HUBD_MUTEX(hubd)); 1020 1021 return (retval); 1022 } 1023 1024 1025 /* 1026 * child post attach/detach notifications 1027 */ 1028 static void 1029 hubd_post_attach(hubd_t *hubd, usb_port_t port, struct attachspec *as) 1030 { 1031 dev_info_t *dip; 1032 1033 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 1034 "hubd_post_attach: port=%d result=%d", 1035 port, as->result); 1036 1037 if (as->result == DDI_SUCCESS) { 1038 /* 1039 * Check if the child created wants to be power managed. 1040 * If yes, the childs power level gets automatically tracked 1041 * by DDI_CTLOPS_POWER busctl. 1042 * If no, we set power of the new child by default 1043 * to USB_DEV_OS_FULL_PWR. Because we should never suspend. 1044 */ 1045 mutex_enter(HUBD_MUTEX(hubd)); 1046 dip = hubd->h_children_dips[port]; 1047 mutex_exit(HUBD_MUTEX(hubd)); 1048 if (DEVI(dip)->devi_pm_info == NULL) { 1049 hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_FULL_PWR); 1050 } 1051 } 1052 } 1053 1054 1055 static void 1056 hubd_post_detach(hubd_t *hubd, usb_port_t port, struct detachspec *ds) 1057 { 1058 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 1059 "hubd_post_detach: port=%d result=%d", port, ds->result); 1060 1061 /* 1062 * if the device is successfully detached and is the 1063 * last device to detach, mark component as idle 1064 */ 1065 mutex_enter(HUBD_MUTEX(hubd)); 1066 if (ds->result == DDI_SUCCESS) { 1067 usba_device_t *usba_device = hubd->h_usba_devices[port]; 1068 dev_info_t *pdip = hubd->h_dip; 1069 mutex_exit(HUBD_MUTEX(hubd)); 1070 1071 usba_hubdi_incr_power_budget(pdip, usba_device); 1072 1073 /* 1074 * We set power of the detached child 1075 * to 0, so that we can suspend if all 1076 * our children are gone 1077 */ 1078 hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_PWR_OFF); 1079 1080 /* check for leaks on detaching */ 1081 if ((usba_device) && (ds->cmd == DDI_DETACH)) { 1082 usba_check_for_leaks(usba_device); 1083 } 1084 } else { 1085 mutex_exit(HUBD_MUTEX(hubd)); 1086 } 1087 } 1088 1089 1090 /* 1091 * hubd_post_power 1092 * After the child's power entry point has been called 1093 * we record its power level in our local struct. 1094 * If the device has powered off, we suspend port 1095 */ 1096 static int 1097 hubd_post_power(hubd_t *hubd, usb_port_t port, pm_bp_child_pwrchg_t *bpc, 1098 int result) 1099 { 1100 int retval = USB_SUCCESS; 1101 1102 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1103 "hubd_post_power: port=%d", port); 1104 1105 if (result == DDI_SUCCESS) { 1106 1107 /* record this power in our local struct */ 1108 hubd_set_child_pwrlvl(hubd, port, bpc->bpc_nlevel); 1109 1110 if (bpc->bpc_nlevel == USB_DEV_OS_PWR_OFF) { 1111 1112 /* now suspend the port */ 1113 retval = hubd_suspend_port(hubd, port); 1114 } else if (bpc->bpc_nlevel == USB_DEV_OS_FULL_PWR) { 1115 1116 /* make sure the port is resumed */ 1117 retval = hubd_resume_port(hubd, port); 1118 } 1119 } else { 1120 1121 /* record old power in our local struct */ 1122 hubd_set_child_pwrlvl(hubd, port, bpc->bpc_olevel); 1123 1124 if (bpc->bpc_olevel == USB_DEV_OS_PWR_OFF) { 1125 1126 /* 1127 * As this device failed to transition from 1128 * power off state, suspend the port again 1129 */ 1130 retval = hubd_suspend_port(hubd, port); 1131 } 1132 } 1133 1134 return (retval); 1135 } 1136 1137 1138 /* 1139 * bus ctl notifications are handled here, the rest goes up to root hub/hcd 1140 */ 1141 static int 1142 usba_hubdi_bus_ctl(dev_info_t *dip, 1143 dev_info_t *rdip, 1144 ddi_ctl_enum_t op, 1145 void *arg, 1146 void *result) 1147 { 1148 usba_device_t *hub_usba_device = usba_get_usba_device(rdip); 1149 dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip; 1150 struct attachspec *as; 1151 struct detachspec *ds; 1152 hubd_t *hubd; 1153 usb_port_t port; 1154 int circ, rval; 1155 int retval = DDI_FAILURE; 1156 1157 hubd = hubd_get_soft_state(dip); 1158 1159 mutex_enter(HUBD_MUTEX(hubd)); 1160 1161 /* flag that we are currently running bus_ctl */ 1162 hubd->h_bus_ctls++; 1163 mutex_exit(HUBD_MUTEX(hubd)); 1164 1165 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1166 "usba_hubdi_bus_ctl:\n\t" 1167 "dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p", 1168 (void *)dip, (void *)rdip, op, arg); 1169 1170 switch (op) { 1171 case DDI_CTLOPS_ATTACH: 1172 as = (struct attachspec *)arg; 1173 port = hubd_child_dip2port(hubd, rdip); 1174 1175 /* there is nothing to do at resume time */ 1176 if (as->cmd == DDI_RESUME) { 1177 break; 1178 } 1179 1180 /* serialize access */ 1181 ndi_devi_enter(hubd->h_dip, &circ); 1182 1183 switch (as->when) { 1184 case DDI_PRE: 1185 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1186 "DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d", 1187 (void *)rdip, port); 1188 1189 mutex_enter(HUBD_MUTEX(hubd)); 1190 hubd->h_port_state[port] |= HUBD_CHILD_ATTACHING; 1191 1192 /* Go busy here. Matching idle is DDI_POST case. */ 1193 (void) hubd_pm_busy_component(hubd, dip, 0); 1194 mutex_exit(HUBD_MUTEX(hubd)); 1195 1196 /* 1197 * if we suspended the port previously 1198 * because child went to low power state, and 1199 * someone unloaded the driver, the port would 1200 * still be suspended and needs to be resumed 1201 */ 1202 rval = hubd_resume_port(hubd, port); 1203 if (rval == USB_SUCCESS) { 1204 retval = DDI_SUCCESS; 1205 } 1206 1207 break; 1208 case DDI_POST: 1209 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1210 "DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d", 1211 (void *)rdip, port); 1212 1213 mutex_enter(HUBD_MUTEX(hubd)); 1214 hubd->h_port_state[port] &= ~HUBD_CHILD_ATTACHING; 1215 mutex_exit(HUBD_MUTEX(hubd)); 1216 1217 hubd_post_attach(hubd, port, (struct attachspec *)arg); 1218 retval = DDI_SUCCESS; 1219 mutex_enter(HUBD_MUTEX(hubd)); 1220 1221 /* Matching idle call for DDI_PRE busy call. */ 1222 (void) hubd_pm_idle_component(hubd, dip, 0); 1223 mutex_exit(HUBD_MUTEX(hubd)); 1224 } 1225 ndi_devi_exit(hubd->h_dip, circ); 1226 1227 break; 1228 case DDI_CTLOPS_DETACH: 1229 ds = (struct detachspec *)arg; 1230 port = hubd_child_dip2port(hubd, rdip); 1231 1232 /* there is nothing to do at suspend time */ 1233 if (ds->cmd == DDI_SUSPEND) { 1234 break; 1235 } 1236 1237 /* serialize access */ 1238 ndi_devi_enter(hubd->h_dip, &circ); 1239 1240 switch (ds->when) { 1241 case DDI_PRE: 1242 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1243 "DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d", 1244 (void *)rdip, port); 1245 1246 mutex_enter(HUBD_MUTEX(hubd)); 1247 hubd->h_port_state[port] |= HUBD_CHILD_DETACHING; 1248 1249 /* Go busy here. Matching idle is DDI_POST case. */ 1250 (void) hubd_pm_busy_component(hubd, dip, 0); 1251 1252 mutex_exit(HUBD_MUTEX(hubd)); 1253 retval = DDI_SUCCESS; 1254 1255 break; 1256 case DDI_POST: 1257 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1258 "DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d", 1259 (void *)rdip, port); 1260 1261 mutex_enter(HUBD_MUTEX(hubd)); 1262 hubd->h_port_state[port] &= ~HUBD_CHILD_DETACHING; 1263 mutex_exit(HUBD_MUTEX(hubd)); 1264 1265 /* Matching idle call for DDI_PRE busy call. */ 1266 hubd_post_detach(hubd, port, (struct detachspec *)arg); 1267 retval = DDI_SUCCESS; 1268 mutex_enter(HUBD_MUTEX(hubd)); 1269 (void) hubd_pm_idle_component(hubd, dip, 0); 1270 mutex_exit(HUBD_MUTEX(hubd)); 1271 1272 break; 1273 } 1274 ndi_devi_exit(hubd->h_dip, circ); 1275 1276 break; 1277 default: 1278 retval = usba_bus_ctl(root_hub_dip, rdip, op, arg, result); 1279 } 1280 1281 /* decrement bus_ctls count */ 1282 mutex_enter(HUBD_MUTEX(hubd)); 1283 hubd->h_bus_ctls--; 1284 ASSERT(hubd->h_bus_ctls >= 0); 1285 mutex_exit(HUBD_MUTEX(hubd)); 1286 1287 return (retval); 1288 } 1289 1290 /* 1291 * hubd_config_one: 1292 * enumerate one child according to 'port' 1293 */ 1294 1295 static boolean_t 1296 hubd_config_one(hubd_t *hubd, int port) 1297 { 1298 uint16_t status, change; 1299 dev_info_t *hdip = hubd->h_dip; 1300 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip; 1301 boolean_t online_child = B_FALSE, found = B_FALSE; 1302 int prh_circ, rh_circ, circ; 1303 1304 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 1305 "hubd_config_one: started, hubd_reset_port = 0x%x", port); 1306 1307 ndi_hold_devi(hdip); /* so we don't race with detach */ 1308 1309 /* 1310 * this ensures one config activity per system at a time. 1311 * we enter the parent PCI node to have this serialization. 1312 * this also excludes ioctls and deathrow thread 1313 */ 1314 ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ); 1315 ndi_devi_enter(rh_dip, &rh_circ); 1316 1317 /* exclude other threads */ 1318 ndi_devi_enter(hdip, &circ); 1319 mutex_enter(HUBD_MUTEX(hubd)); 1320 1321 hubd_pm_busy_component(hubd, hubd->h_dip, 0); 1322 1323 if (!hubd->h_children_dips[port]) { 1324 1325 (void) hubd_determine_port_status(hubd, port, 1326 &status, &change, HUBD_ACK_ALL_CHANGES); 1327 1328 if (status & PORT_STATUS_CCS) { 1329 online_child |= (hubd_handle_port_connect(hubd, 1330 port) == USB_SUCCESS); 1331 found = online_child; 1332 } 1333 } else { 1334 found = B_TRUE; 1335 } 1336 1337 mutex_exit(HUBD_MUTEX(hubd)); 1338 1339 ndi_devi_exit(hdip, circ); 1340 ndi_devi_exit(rh_dip, rh_circ); 1341 ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ); 1342 1343 if (online_child) { 1344 USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 1345 "hubd_config_one: onlining child"); 1346 1347 (void) ndi_devi_online(hubd->h_dip, 0); 1348 } 1349 1350 mutex_enter(HUBD_MUTEX(hubd)); 1351 1352 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 1353 1354 USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle, 1355 "hubd_config_one: exit"); 1356 1357 mutex_exit(HUBD_MUTEX(hubd)); 1358 1359 ndi_rele_devi(hdip); 1360 1361 return (found); 1362 } 1363 1364 /* 1365 * bus enumeration entry points 1366 */ 1367 static int 1368 hubd_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op, 1369 void *arg, dev_info_t **child) 1370 { 1371 hubd_t *hubd = hubd_get_soft_state(dip); 1372 int rval, circ; 1373 long port; 1374 1375 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1376 "hubd_bus_config: op=%d", op); 1377 1378 if (hubdi_bus_config_debug) { 1379 flag |= NDI_DEVI_DEBUG; 1380 } 1381 1382 if (op == BUS_CONFIG_ONE) { 1383 boolean_t found; 1384 char cname[80]; 1385 char *name, *addr; 1386 1387 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 1388 "hubd_bus_config: op=%d (BUS_CONFIG_ONE)", op); 1389 1390 (void) snprintf(cname, 80, "%s", (char *)arg); 1391 /* split name into "name@addr" parts */ 1392 i_ddi_parse_name(cname, &name, &addr, NULL); 1393 if (addr && *addr) { 1394 (void) ddi_strtol(addr, NULL, 16, &port); 1395 } else { 1396 return (NDI_FAILURE); 1397 } 1398 1399 found = hubd_config_one(hubd, port); 1400 1401 if (found == 0) { 1402 return (NDI_FAILURE); 1403 } 1404 1405 } 1406 ndi_devi_enter(hubd->h_dip, &circ); 1407 rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0); 1408 ndi_devi_exit(hubd->h_dip, circ); 1409 1410 return (rval); 1411 } 1412 1413 1414 static int 1415 hubd_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op, 1416 void *arg) 1417 { 1418 hubd_t *hubd = hubd_get_soft_state(dip); 1419 dev_info_t *cdip; 1420 usb_port_t port; 1421 int circ; 1422 int rval; 1423 1424 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1425 "hubd_bus_unconfig: op=%d", op); 1426 1427 if (hubdi_bus_config_debug) { 1428 flag |= NDI_DEVI_DEBUG; 1429 } 1430 1431 if ((op == BUS_UNCONFIG_ALL) && (flag & NDI_AUTODETACH) == 0) { 1432 flag |= NDI_DEVI_REMOVE; 1433 } 1434 1435 /* serialize access */ 1436 ndi_devi_enter(dip, &circ); 1437 1438 rval = ndi_busop_bus_unconfig(dip, flag, op, arg); 1439 1440 /* logically zap children's list */ 1441 mutex_enter(HUBD_MUTEX(hubd)); 1442 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 1443 hubd->h_port_state[port] |= HUBD_CHILD_ZAP; 1444 } 1445 mutex_exit(HUBD_MUTEX(hubd)); 1446 1447 /* fill in what's left */ 1448 for (cdip = ddi_get_child(dip); cdip; 1449 cdip = ddi_get_next_sibling(cdip)) { 1450 usba_device_t *usba_device = usba_get_usba_device(cdip); 1451 1452 if (usba_device == NULL) { 1453 1454 continue; 1455 } 1456 mutex_enter(HUBD_MUTEX(hubd)); 1457 port = usba_device->usb_port; 1458 hubd->h_children_dips[port] = cdip; 1459 hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP; 1460 mutex_exit(HUBD_MUTEX(hubd)); 1461 } 1462 1463 /* physically zap the children we didn't find */ 1464 mutex_enter(HUBD_MUTEX(hubd)); 1465 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 1466 if (hubd->h_port_state[port] & HUBD_CHILD_ZAP) { 1467 /* zap the dip and usba_device structure as well */ 1468 hubd_free_usba_device(hubd, hubd->h_usba_devices[port]); 1469 hubd->h_children_dips[port] = NULL; 1470 hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP; 1471 } 1472 } 1473 mutex_exit(HUBD_MUTEX(hubd)); 1474 1475 ndi_devi_exit(dip, circ); 1476 1477 USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle, 1478 "hubd_bus_unconfig: rval=%d", rval); 1479 1480 return (rval); 1481 } 1482 1483 1484 /* bus_power entry point */ 1485 static int 1486 hubd_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op, 1487 void *arg, void *result) 1488 { 1489 hubd_t *hubd; 1490 int rval, pwrup_res; 1491 usb_port_t port; 1492 int retval = DDI_FAILURE; 1493 pm_bp_child_pwrchg_t *bpc; 1494 pm_bp_nexus_pwrup_t bpn; 1495 1496 hubd = hubd_get_soft_state(dip); 1497 1498 USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1499 "hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, " 1500 "result=%d\n", (void *)dip, impl_arg, op, arg, *(int *)result); 1501 1502 bpc = (pm_bp_child_pwrchg_t *)arg; 1503 1504 mutex_enter(HUBD_MUTEX(hubd)); 1505 hubd->h_bus_pwr++; 1506 mutex_exit(HUBD_MUTEX(hubd)); 1507 1508 switch (op) { 1509 case BUS_POWER_PRE_NOTIFICATION: 1510 port = hubd_child_dip2port(hubd, bpc->bpc_dip); 1511 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1512 "hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d", 1513 port); 1514 1515 /* go to full power if we are powered down */ 1516 mutex_enter(HUBD_MUTEX(hubd)); 1517 1518 /* 1519 * If this case completes normally, idle will be in 1520 * hubd_bus_power / BUS_POWER_POST_NOTIFICATION 1521 */ 1522 hubd_pm_busy_component(hubd, dip, 0); 1523 1524 /* 1525 * raise power only if we have created the components 1526 * and are currently in low power 1527 */ 1528 if ((hubd->h_dev_state == USB_DEV_PWRED_DOWN) && 1529 hubd->h_hubpm->hubp_wakeup_enabled) { 1530 mutex_exit(HUBD_MUTEX(hubd)); 1531 1532 bpn.bpn_comp = 0; 1533 bpn.bpn_dip = dip; 1534 bpn.bpn_level = USB_DEV_OS_FULL_PWR; 1535 bpn.bpn_private = bpc->bpc_private; 1536 1537 rval = pm_busop_bus_power(dip, impl_arg, 1538 BUS_POWER_NEXUS_PWRUP, (void *)&bpn, 1539 (void *)&pwrup_res); 1540 1541 if (rval != DDI_SUCCESS || pwrup_res != DDI_SUCCESS) { 1542 mutex_enter(HUBD_MUTEX(hubd)); 1543 hubd_pm_idle_component(hubd, dip, 0); 1544 mutex_exit(HUBD_MUTEX(hubd)); 1545 1546 break; 1547 } 1548 mutex_enter(HUBD_MUTEX(hubd)); 1549 } 1550 1551 /* indicate that child is changing power level */ 1552 hubd->h_port_state[port] |= HUBD_CHILD_PWRLVL_CHNG; 1553 mutex_exit(HUBD_MUTEX(hubd)); 1554 1555 if ((bpc->bpc_olevel == 0) && 1556 (bpc->bpc_nlevel > bpc->bpc_olevel)) { 1557 /* 1558 * this child is transitioning from power off 1559 * to power on state - resume port 1560 */ 1561 rval = hubd_resume_port(hubd, port); 1562 if (rval == USB_SUCCESS) { 1563 retval = DDI_SUCCESS; 1564 } else { 1565 /* reset this flag on failure */ 1566 mutex_enter(HUBD_MUTEX(hubd)); 1567 hubd->h_port_state[port] &= 1568 ~HUBD_CHILD_PWRLVL_CHNG; 1569 hubd_pm_idle_component(hubd, dip, 0); 1570 mutex_exit(HUBD_MUTEX(hubd)); 1571 } 1572 } else { 1573 retval = DDI_SUCCESS; 1574 } 1575 1576 break; 1577 case BUS_POWER_POST_NOTIFICATION: 1578 port = hubd_child_dip2port(hubd, bpc->bpc_dip); 1579 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1580 "hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d", 1581 port); 1582 1583 mutex_enter(HUBD_MUTEX(hubd)); 1584 hubd->h_port_state[port] &= ~HUBD_CHILD_PWRLVL_CHNG; 1585 mutex_exit(HUBD_MUTEX(hubd)); 1586 1587 /* record child's pwr and suspend port if required */ 1588 rval = hubd_post_power(hubd, port, bpc, *(int *)result); 1589 if (rval == USB_SUCCESS) { 1590 1591 retval = DDI_SUCCESS; 1592 } 1593 1594 mutex_enter(HUBD_MUTEX(hubd)); 1595 1596 /* 1597 * Matching idle for the busy in 1598 * hubd_bus_power / BUS_POWER_PRE_NOTIFICATION 1599 */ 1600 hubd_pm_idle_component(hubd, dip, 0); 1601 1602 mutex_exit(HUBD_MUTEX(hubd)); 1603 1604 break; 1605 default: 1606 retval = pm_busop_bus_power(dip, impl_arg, op, arg, result); 1607 1608 break; 1609 } 1610 1611 mutex_enter(HUBD_MUTEX(hubd)); 1612 hubd->h_bus_pwr--; 1613 mutex_exit(HUBD_MUTEX(hubd)); 1614 1615 return (retval); 1616 } 1617 1618 1619 /* 1620 * functions to handle power transition for OS levels 0 -> 3 1621 */ 1622 static int 1623 hubd_pwrlvl0(hubd_t *hubd) 1624 { 1625 hub_power_t *hubpm; 1626 1627 /* We can't power down if hotplug thread is running */ 1628 if (hubd->h_hotplug_thread || hubd->h_hubpm->hubp_busy_pm || 1629 (hubd_can_suspend(hubd) == USB_FAILURE)) { 1630 1631 return (USB_FAILURE); 1632 } 1633 1634 switch (hubd->h_dev_state) { 1635 case USB_DEV_ONLINE: 1636 hubpm = hubd->h_hubpm; 1637 1638 /* 1639 * To avoid race with bus_power pre_notify on check over 1640 * dev_state, we need to correctly set the dev state 1641 * before the mutex is dropped in stop polling. 1642 */ 1643 hubd->h_dev_state = USB_DEV_PWRED_DOWN; 1644 hubpm->hubp_current_power = USB_DEV_OS_PWR_OFF; 1645 1646 /* 1647 * if we are the root hub, do not stop polling 1648 * otherwise, we will never see a resume 1649 */ 1650 if (usba_is_root_hub(hubd->h_dip)) { 1651 /* place holder to implement Global Suspend */ 1652 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 1653 "Global Suspend: Not Yet Implemented"); 1654 } else { 1655 hubd_stop_polling(hubd); 1656 } 1657 1658 /* Issue USB D3 command to the device here */ 1659 (void) usb_set_device_pwrlvl3(hubd->h_dip); 1660 1661 break; 1662 case USB_DEV_DISCONNECTED: 1663 case USB_DEV_SUSPENDED: 1664 case USB_DEV_PWRED_DOWN: 1665 default: 1666 1667 break; 1668 } 1669 1670 return (USB_SUCCESS); 1671 } 1672 1673 1674 /* ARGSUSED */ 1675 static int 1676 hubd_pwrlvl1(hubd_t *hubd) 1677 { 1678 /* Issue USB D2 command to the device here */ 1679 (void) usb_set_device_pwrlvl2(hubd->h_dip); 1680 1681 return (USB_FAILURE); 1682 } 1683 1684 1685 /* ARGSUSED */ 1686 static int 1687 hubd_pwrlvl2(hubd_t *hubd) 1688 { 1689 /* Issue USB D1 command to the device here */ 1690 (void) usb_set_device_pwrlvl1(hubd->h_dip); 1691 1692 return (USB_FAILURE); 1693 } 1694 1695 1696 static int 1697 hubd_pwrlvl3(hubd_t *hubd) 1698 { 1699 hub_power_t *hubpm; 1700 int rval; 1701 1702 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, "hubd_pwrlvl3"); 1703 1704 hubpm = hubd->h_hubpm; 1705 switch (hubd->h_dev_state) { 1706 case USB_DEV_PWRED_DOWN: 1707 ASSERT(hubpm->hubp_current_power == USB_DEV_OS_PWR_OFF); 1708 if (usba_is_root_hub(hubd->h_dip)) { 1709 /* implement global resume here */ 1710 USB_DPRINTF_L2(DPRINT_MASK_PM, 1711 hubd->h_log_handle, 1712 "Global Resume: Not Yet Implemented"); 1713 } 1714 /* Issue USB D0 command to the device here */ 1715 rval = usb_set_device_pwrlvl0(hubd->h_dip); 1716 ASSERT(rval == USB_SUCCESS); 1717 hubd->h_dev_state = USB_DEV_ONLINE; 1718 hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR; 1719 hubpm->hubp_time_at_full_power = gethrtime(); 1720 hubd_start_polling(hubd, 0); 1721 1722 /* FALLTHRU */ 1723 case USB_DEV_ONLINE: 1724 /* we are already in full power */ 1725 1726 /* FALLTHRU */ 1727 case USB_DEV_DISCONNECTED: 1728 case USB_DEV_SUSPENDED: 1729 /* 1730 * PM framework tries to put you in full power 1731 * during system shutdown. If we are disconnected 1732 * return success. Also, we should not change state 1733 * when we are disconnected or suspended or about to 1734 * transition to that state 1735 */ 1736 1737 return (USB_SUCCESS); 1738 default: 1739 USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, 1740 "hubd_pwrlvl3: Illegal dev_state=%d", hubd->h_dev_state); 1741 1742 return (USB_FAILURE); 1743 } 1744 } 1745 1746 1747 /* power entry point */ 1748 /* ARGSUSED */ 1749 int 1750 usba_hubdi_power(dev_info_t *dip, int comp, int level) 1751 { 1752 hubd_t *hubd; 1753 hub_power_t *hubpm; 1754 int retval; 1755 int circ; 1756 1757 hubd = hubd_get_soft_state(dip); 1758 USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1759 "usba_hubdi_power: level=%d", level); 1760 1761 ndi_devi_enter(dip, &circ); 1762 1763 mutex_enter(HUBD_MUTEX(hubd)); 1764 hubpm = hubd->h_hubpm; 1765 1766 /* check if we are transitioning to a legal power level */ 1767 if (USB_DEV_PWRSTATE_OK(hubpm->hubp_pwr_states, level)) { 1768 USB_DPRINTF_L2(DPRINT_MASK_HUBDI, hubd->h_log_handle, 1769 "usba_hubdi_power: illegal power level=%d " 1770 "hubp_pwr_states=0x%x", level, hubpm->hubp_pwr_states); 1771 mutex_exit(HUBD_MUTEX(hubd)); 1772 1773 ndi_devi_exit(dip, circ); 1774 1775 return (DDI_FAILURE); 1776 } 1777 1778 switch (level) { 1779 case USB_DEV_OS_PWR_OFF: 1780 retval = hubd_pwrlvl0(hubd); 1781 1782 break; 1783 case USB_DEV_OS_PWR_1: 1784 retval = hubd_pwrlvl1(hubd); 1785 1786 break; 1787 case USB_DEV_OS_PWR_2: 1788 retval = hubd_pwrlvl2(hubd); 1789 1790 break; 1791 case USB_DEV_OS_FULL_PWR: 1792 retval = hubd_pwrlvl3(hubd); 1793 1794 break; 1795 } 1796 mutex_exit(HUBD_MUTEX(hubd)); 1797 1798 ndi_devi_exit(dip, circ); 1799 1800 return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 1801 } 1802 1803 1804 /* power entry point for the root hub */ 1805 int 1806 usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level) 1807 { 1808 return (usba_hubdi_power(dip, comp, level)); 1809 } 1810 1811 1812 /* 1813 * standard driver entry points support code 1814 */ 1815 int 1816 usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1817 { 1818 int instance = ddi_get_instance(dip); 1819 hubd_t *hubd = NULL; 1820 int i, rval; 1821 int minor; 1822 uint8_t ports_count; 1823 char *log_name = NULL; 1824 const char *root_hub_drvname; 1825 usb_ep_data_t *ep_data; 1826 usba_device_t *child_ud = NULL; 1827 usb_dev_descr_t *usb_dev_descr; 1828 usb_port_status_t parent_port_status, child_port_status; 1829 1830 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle, 1831 "hubd_attach instance %d, cmd=0x%x", instance, cmd); 1832 1833 switch (cmd) { 1834 case DDI_ATTACH: 1835 1836 break; 1837 case DDI_RESUME: 1838 hubd_cpr_resume(dip); 1839 1840 return (DDI_SUCCESS); 1841 default: 1842 return (DDI_FAILURE); 1843 } 1844 1845 /* 1846 * Allocate softc information. 1847 */ 1848 if (usba_is_root_hub(dip)) { 1849 /* soft state has already been allocated */ 1850 hubd = hubd_get_soft_state(dip); 1851 minor = HUBD_IS_ROOT_HUB; 1852 1853 /* generate readable labels for different root hubs */ 1854 root_hub_drvname = ddi_driver_name(dip); 1855 if (strcmp(root_hub_drvname, "ehci") == 0) { 1856 log_name = "eusb"; 1857 } else if (strcmp(root_hub_drvname, "uhci") == 0) { 1858 log_name = "uusb"; 1859 } else { 1860 /* std. for ohci */ 1861 log_name = "usb"; 1862 } 1863 } else { 1864 rval = ddi_soft_state_zalloc(hubd_statep, instance); 1865 minor = 0; 1866 1867 if (rval != DDI_SUCCESS) { 1868 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 1869 "cannot allocate soft state (%d)", instance); 1870 goto fail; 1871 } 1872 1873 hubd = hubd_get_soft_state(dip); 1874 if (hubd == NULL) { 1875 goto fail; 1876 } 1877 } 1878 1879 hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel, 1880 &hubd_errmask, &hubd_instance_debug, 0); 1881 1882 hubd->h_usba_device = child_ud = usba_get_usba_device(dip); 1883 hubd->h_dip = dip; 1884 hubd->h_instance = instance; 1885 1886 mutex_enter(&child_ud->usb_mutex); 1887 child_port_status = child_ud->usb_port_status; 1888 usb_dev_descr = child_ud->usb_dev_descr; 1889 parent_port_status = (child_ud->usb_hs_hub_usba_dev) ? 1890 child_ud->usb_hs_hub_usba_dev->usb_port_status : 0; 1891 mutex_exit(&child_ud->usb_mutex); 1892 1893 if ((child_port_status == USBA_FULL_SPEED_DEV) && 1894 (parent_port_status == USBA_HIGH_SPEED_DEV) && 1895 (usb_dev_descr->bcdUSB == 0x100)) { 1896 USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle, 1897 "Use of a USB1.0 hub behind a high speed port may " 1898 "cause unexpected failures"); 1899 } 1900 1901 hubd->h_pipe_policy.pp_max_async_reqs = 1; 1902 1903 /* register with USBA as client driver */ 1904 if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) { 1905 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1906 "client attach failed"); 1907 1908 goto fail; 1909 } 1910 1911 if (usb_get_dev_data(dip, &hubd->h_dev_data, 1912 USB_PARSE_LVL_IF, 0) != USB_SUCCESS) { 1913 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1914 "cannot get dev_data"); 1915 1916 goto fail; 1917 } 1918 1919 if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data, 1920 hubd->h_dev_data->dev_curr_if, 0, 0, 1921 (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) { 1922 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1923 "no interrupt IN endpoint found"); 1924 1925 goto fail; 1926 } 1927 1928 hubd->h_ep1_descr = ep_data->ep_descr; 1929 hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph; 1930 1931 mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER, 1932 hubd->h_dev_data->dev_iblock_cookie); 1933 cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL); 1934 cv_init(&hubd->h_cv_hotplug_dev, NULL, CV_DRIVER, NULL); 1935 1936 hubd->h_init_state |= HUBD_LOCKS_DONE; 1937 1938 usb_free_descr_tree(dip, hubd->h_dev_data); 1939 1940 /* 1941 * register this hub instance with usba 1942 */ 1943 rval = usba_hubdi_register(dip, 0); 1944 if (rval != USB_SUCCESS) { 1945 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1946 "usba_hubdi_register failed"); 1947 goto fail; 1948 } 1949 1950 mutex_enter(HUBD_MUTEX(hubd)); 1951 hubd->h_init_state |= HUBD_HUBDI_REGISTERED; 1952 hubd->h_dev_state = USB_DEV_ONLINE; 1953 mutex_exit(HUBD_MUTEX(hubd)); 1954 1955 /* now create components to power manage this device */ 1956 hubd_create_pm_components(dip, hubd); 1957 1958 /* 1959 * Event handling: definition and registration 1960 * 1961 * first the definition: 1962 * get event handle 1963 */ 1964 (void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP); 1965 1966 /* bind event set to the handle */ 1967 if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events, 1968 NDI_SLEEP)) { 1969 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 1970 "binding event set failed"); 1971 1972 goto fail; 1973 } 1974 1975 /* event registration */ 1976 if (hubd_register_events(hubd) != USB_SUCCESS) { 1977 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 1978 "hubd_register_events failed"); 1979 1980 goto fail; 1981 } 1982 1983 mutex_enter(HUBD_MUTEX(hubd)); 1984 hubd->h_init_state |= HUBD_EVENTS_REGISTERED; 1985 1986 if ((hubd_get_hub_descriptor(hubd)) != USB_SUCCESS) { 1987 mutex_exit(HUBD_MUTEX(hubd)); 1988 1989 goto fail; 1990 } 1991 1992 if (ddi_prop_exists(DDI_DEV_T_ANY, dip, 1993 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM), 1994 "hub-ignore-power-budget") == 1) { 1995 hubd->h_ignore_pwr_budget = B_TRUE; 1996 } else { 1997 hubd->h_ignore_pwr_budget = B_FALSE; 1998 1999 /* initialize hub power budget variables */ 2000 if (hubd_init_power_budget(hubd) != USB_SUCCESS) { 2001 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2002 "hubd_init_power_budget failed"); 2003 mutex_exit(HUBD_MUTEX(hubd)); 2004 2005 goto fail; 2006 } 2007 } 2008 2009 /* initialize and create children */ 2010 if (hubd_check_ports(hubd) != USB_SUCCESS) { 2011 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2012 "hubd_check_ports failed"); 2013 mutex_exit(HUBD_MUTEX(hubd)); 2014 2015 goto fail; 2016 } 2017 2018 /* 2019 * create cfgadm nodes 2020 */ 2021 hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP); 2022 hubd_get_ancestry_str(hubd); 2023 2024 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2025 "#ports=0x%x", hubd->h_hub_descr.bNbrPorts); 2026 2027 for (i = 1; i <= hubd->h_hub_descr.bNbrPorts; i++) { 2028 char ap_name[HUBD_APID_NAMELEN]; 2029 2030 (void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d", 2031 hubd->h_ancestry_str, i); 2032 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2033 "ap_name=%s", ap_name); 2034 2035 if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance, 2036 DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) { 2037 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2038 "cannot create attachment point node (%d)", 2039 instance); 2040 mutex_exit(HUBD_MUTEX(hubd)); 2041 2042 goto fail; 2043 } 2044 } 2045 2046 ports_count = hubd->h_hub_descr.bNbrPorts; 2047 mutex_exit(HUBD_MUTEX(hubd)); 2048 2049 /* create minor nodes */ 2050 if (ddi_create_minor_node(dip, "hubd", S_IFCHR, 2051 instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) { 2052 2053 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2054 "cannot create devctl minor node (%d)", instance); 2055 2056 goto fail; 2057 } 2058 2059 mutex_enter(HUBD_MUTEX(hubd)); 2060 hubd->h_init_state |= HUBD_MINOR_NODE_CREATED; 2061 mutex_exit(HUBD_MUTEX(hubd)); 2062 2063 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2064 "usb-port-count", ports_count) != DDI_PROP_SUCCESS) { 2065 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2066 "usb-port-count update failed"); 2067 } 2068 2069 /* 2070 * host controller driver has already reported this dev 2071 * if we are the root hub 2072 */ 2073 if (!usba_is_root_hub(dip)) { 2074 ddi_report_dev(dip); 2075 } 2076 2077 /* enable deathrow thread */ 2078 hubd->h_cleanup_enabled = B_TRUE; 2079 mutex_enter(HUBD_MUTEX(hubd)); 2080 hubd_pm_idle_component(hubd, dip, 0); 2081 mutex_exit(HUBD_MUTEX(hubd)); 2082 2083 return (DDI_SUCCESS); 2084 2085 fail: 2086 { 2087 char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2088 2089 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 2090 "cannot attach %s", ddi_pathname(dip, pathname)); 2091 2092 kmem_free(pathname, MAXPATHLEN); 2093 } 2094 2095 mutex_enter(HUBD_MUTEX(hubd)); 2096 hubd_pm_idle_component(hubd, dip, 0); 2097 mutex_exit(HUBD_MUTEX(hubd)); 2098 2099 if (hubd) { 2100 rval = hubd_cleanup(dip, hubd); 2101 if (rval != USB_SUCCESS) { 2102 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle, 2103 "failure to complete cleanup after attach failure"); 2104 } 2105 } 2106 2107 return (DDI_FAILURE); 2108 } 2109 2110 2111 int 2112 usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2113 { 2114 hubd_t *hubd = hubd_get_soft_state(dip); 2115 int rval; 2116 2117 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2118 "hubd_detach: cmd=0x%x", cmd); 2119 2120 switch (cmd) { 2121 case DDI_DETACH: 2122 rval = hubd_cleanup(dip, hubd); 2123 2124 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 2125 case DDI_SUSPEND: 2126 rval = hubd_cpr_suspend(hubd); 2127 2128 return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 2129 default: 2130 return (DDI_FAILURE); 2131 } 2132 } 2133 2134 2135 /* 2136 * hubd_setdevaddr 2137 * set the device addrs on this port 2138 */ 2139 static int 2140 hubd_setdevaddr(hubd_t *hubd, usb_port_t port) 2141 { 2142 int rval; 2143 usb_cr_t completion_reason; 2144 usb_cb_flags_t cb_flags; 2145 usb_pipe_handle_t ph; 2146 dev_info_t *child_dip = NULL; 2147 uchar_t address = 0; 2148 usba_device_t *usba_device; 2149 int retry = 0; 2150 long time_delay; 2151 2152 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2153 "hubd_setdevaddr: port=%d", port); 2154 2155 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 2156 2157 child_dip = hubd->h_children_dips[port]; 2158 address = hubd->h_usba_devices[port]->usb_addr; 2159 usba_device = hubd->h_usba_devices[port]; 2160 2161 /* close the default pipe with addr x */ 2162 mutex_exit(HUBD_MUTEX(hubd)); 2163 ph = usba_get_dflt_pipe_handle(child_dip); 2164 usb_pipe_close(child_dip, ph, 2165 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 2166 mutex_enter(HUBD_MUTEX(hubd)); 2167 2168 /* 2169 * As this device has been reset, temporarily 2170 * assign the default address 2171 */ 2172 mutex_enter(&usba_device->usb_mutex); 2173 address = usba_device->usb_addr; 2174 usba_device->usb_addr = USBA_DEFAULT_ADDR; 2175 mutex_exit(&usba_device->usb_mutex); 2176 2177 mutex_exit(HUBD_MUTEX(hubd)); 2178 2179 time_delay = drv_usectohz(hubd_device_delay / 20); 2180 for (retry = 0; retry < hubd_retry_enumerate; retry++) { 2181 2182 /* open child's default pipe with USBA_DEFAULT_ADDR */ 2183 if (usb_pipe_open(child_dip, NULL, NULL, 2184 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != 2185 USB_SUCCESS) { 2186 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2187 "hubd_setdevaddr: Unable to open default pipe"); 2188 2189 break; 2190 } 2191 2192 /* Set the address of the device */ 2193 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 2194 USB_DEV_REQ_HOST_TO_DEV, 2195 USB_REQ_SET_ADDRESS, /* bRequest */ 2196 address, /* wValue */ 2197 0, /* wIndex */ 2198 0, /* wLength */ 2199 NULL, 0, 2200 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 2201 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2202 "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x", 2203 retry, rval, completion_reason, cb_flags); 2204 } 2205 2206 usb_pipe_close(child_dip, ph, 2207 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 2208 2209 if (rval == USB_SUCCESS) { 2210 2211 break; 2212 } 2213 2214 delay(time_delay); 2215 } 2216 2217 /* Reset to the old address */ 2218 mutex_enter(&usba_device->usb_mutex); 2219 usba_device->usb_addr = address; 2220 mutex_exit(&usba_device->usb_mutex); 2221 mutex_enter(HUBD_MUTEX(hubd)); 2222 2223 usba_clear_data_toggle(usba_device); 2224 2225 return (rval); 2226 } 2227 2228 2229 /* 2230 * hubd_setdevconfig 2231 * set the device addrs on this port 2232 */ 2233 static void 2234 hubd_setdevconfig(hubd_t *hubd, usb_port_t port) 2235 { 2236 int rval; 2237 usb_cr_t completion_reason; 2238 usb_cb_flags_t cb_flags; 2239 usb_pipe_handle_t ph; 2240 dev_info_t *child_dip = NULL; 2241 usba_device_t *usba_device = NULL; 2242 uint16_t config_value; 2243 2244 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2245 "hubd_setdevconfig: port=%d", port); 2246 2247 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 2248 2249 child_dip = hubd->h_children_dips[port]; 2250 usba_device = hubd->h_usba_devices[port]; 2251 config_value = hubd->h_usba_devices[port]->usb_cfg_value; 2252 mutex_exit(HUBD_MUTEX(hubd)); 2253 2254 /* open the default control pipe */ 2255 if ((rval = usb_pipe_open(child_dip, NULL, NULL, 2256 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) == 2257 USB_SUCCESS) { 2258 2259 /* Set the default configuration of the device */ 2260 if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph, 2261 USB_DEV_REQ_HOST_TO_DEV, 2262 USB_REQ_SET_CFG, /* bRequest */ 2263 config_value, /* wValue */ 2264 0, /* wIndex */ 2265 0, /* wLength */ 2266 NULL, 0, 2267 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 2268 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2269 "hubd_setdevconfig: set device config failed: " 2270 "cr=%d cb_fl=0x%x rval=%d", 2271 completion_reason, cb_flags, rval); 2272 } 2273 /* 2274 * After setting the configuration, we make this default 2275 * control pipe persistent, so that it gets re-opened 2276 * on posting a connect event 2277 */ 2278 usba_persistent_pipe_close(usba_device); 2279 } else { 2280 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2281 "pipe open fails: rval=%d", rval); 2282 } 2283 mutex_enter(HUBD_MUTEX(hubd)); 2284 } 2285 2286 2287 /*ARGSUSED*/ 2288 static int 2289 hubd_check_disconnected_ports(dev_info_t *dip, void *arg) 2290 { 2291 int circ; 2292 usb_port_t port; 2293 hubd_t *hubd; 2294 major_t hub_major = ddi_name_to_major("hubd"); 2295 major_t hwahc_major = ddi_name_to_major("hwahc"); 2296 major_t usbmid_major = ddi_name_to_major("usb_mid"); 2297 2298 /* 2299 * make sure dip is a usb hub, major of root hub is HCD 2300 * major 2301 */ 2302 if (!usba_is_root_hub(dip)) { 2303 if (ddi_driver_major(dip) == usbmid_major) { 2304 /* 2305 * need to walk the children since it might be a 2306 * HWA device 2307 */ 2308 2309 return (DDI_WALK_CONTINUE); 2310 } 2311 2312 /* TODO: DWA device may also need special handling */ 2313 2314 if (((ddi_driver_major(dip) != hub_major) && 2315 (ddi_driver_major(dip) != hwahc_major)) || 2316 !i_ddi_devi_attached(dip)) { 2317 2318 return (DDI_WALK_PRUNECHILD); 2319 } 2320 } 2321 2322 hubd = hubd_get_soft_state(dip); 2323 if (hubd == NULL) { 2324 2325 return (DDI_WALK_PRUNECHILD); 2326 } 2327 2328 /* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */ 2329 ndi_devi_enter(dip, &circ); 2330 2331 if (ddi_driver_major(dip) != hwahc_major) { 2332 /* for normal usb hub or root hub */ 2333 mutex_enter(HUBD_MUTEX(hubd)); 2334 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 2335 dev_info_t *cdip = hubd->h_children_dips[port]; 2336 2337 if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) { 2338 2339 continue; 2340 } 2341 2342 (void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE, 2343 B_TRUE); 2344 } 2345 mutex_exit(HUBD_MUTEX(hubd)); 2346 } else { 2347 /* for HWA */ 2348 if (hubd->h_cleanup_child != NULL) { 2349 if (hubd->h_cleanup_child(dip) != USB_SUCCESS) { 2350 ndi_devi_exit(dip, circ); 2351 2352 return (DDI_WALK_PRUNECHILD); 2353 } 2354 } else { 2355 ndi_devi_exit(dip, circ); 2356 2357 return (DDI_WALK_PRUNECHILD); 2358 } 2359 } 2360 2361 ndi_devi_exit(dip, circ); 2362 2363 /* skip siblings of root hub */ 2364 if (usba_is_root_hub(dip)) { 2365 2366 return (DDI_WALK_PRUNESIB); 2367 } 2368 2369 return (DDI_WALK_CONTINUE); 2370 } 2371 2372 2373 /* 2374 * this thread will walk all children under the root hub for this 2375 * USB bus instance and attempt to remove them 2376 */ 2377 static void 2378 hubd_root_hub_cleanup_thread(void *arg) 2379 { 2380 int circ; 2381 hubd_t *root_hubd = (hubd_t *)arg; 2382 dev_info_t *rh_dip = root_hubd->h_dip; 2383 #ifndef __lock_lint 2384 callb_cpr_t cprinfo; 2385 2386 CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr, 2387 "USB root hub"); 2388 #endif 2389 2390 for (;;) { 2391 /* don't race with detach */ 2392 ndi_hold_devi(rh_dip); 2393 2394 mutex_enter(HUBD_MUTEX(root_hubd)); 2395 root_hubd->h_cleanup_needed = 0; 2396 mutex_exit(HUBD_MUTEX(root_hubd)); 2397 2398 (void) devfs_clean(rh_dip, NULL, 0); 2399 2400 ndi_devi_enter(ddi_get_parent(rh_dip), &circ); 2401 ddi_walk_devs(rh_dip, hubd_check_disconnected_ports, 2402 NULL); 2403 #ifdef __lock_lint 2404 (void) hubd_check_disconnected_ports(rh_dip, NULL); 2405 #endif 2406 ndi_devi_exit(ddi_get_parent(rh_dip), circ); 2407 2408 /* quit if we are not enabled anymore */ 2409 mutex_enter(HUBD_MUTEX(root_hubd)); 2410 if ((root_hubd->h_cleanup_enabled == B_FALSE) || 2411 (root_hubd->h_cleanup_needed == B_FALSE)) { 2412 root_hubd->h_cleanup_active = B_FALSE; 2413 mutex_exit(HUBD_MUTEX(root_hubd)); 2414 ndi_rele_devi(rh_dip); 2415 2416 break; 2417 } 2418 mutex_exit(HUBD_MUTEX(root_hubd)); 2419 ndi_rele_devi(rh_dip); 2420 2421 #ifndef __lock_lint 2422 mutex_enter(HUBD_MUTEX(root_hubd)); 2423 CALLB_CPR_SAFE_BEGIN(&cprinfo); 2424 mutex_exit(HUBD_MUTEX(root_hubd)); 2425 2426 delay(drv_usectohz(hubd_dip_cleanup_delay)); 2427 2428 mutex_enter(HUBD_MUTEX(root_hubd)); 2429 CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd)); 2430 mutex_exit(HUBD_MUTEX(root_hubd)); 2431 #endif 2432 } 2433 2434 #ifndef __lock_lint 2435 mutex_enter(HUBD_MUTEX(root_hubd)); 2436 CALLB_CPR_EXIT(&cprinfo); 2437 #endif 2438 } 2439 2440 2441 void 2442 hubd_schedule_cleanup(dev_info_t *rh_dip) 2443 { 2444 hubd_t *root_hubd; 2445 2446 /* 2447 * The usb_root_hub_dip pointer for the child hub of the WUSB 2448 * wire adapter class device points to the wire adapter, not 2449 * the root hub. Need to find the real root hub dip so that 2450 * the cleanup thread only starts from the root hub. 2451 */ 2452 while (!usba_is_root_hub(rh_dip)) { 2453 root_hubd = hubd_get_soft_state(rh_dip); 2454 if (root_hubd != NULL) { 2455 rh_dip = root_hubd->h_usba_device->usb_root_hub_dip; 2456 if (rh_dip == NULL) { 2457 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2458 root_hubd->h_log_handle, 2459 "hubd_schedule_cleanup: null rh dip"); 2460 2461 return; 2462 } 2463 } else { 2464 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2465 root_hubd->h_log_handle, 2466 "hubd_schedule_cleanup: cannot find root hub"); 2467 2468 return; 2469 } 2470 } 2471 root_hubd = hubd_get_soft_state(rh_dip); 2472 2473 mutex_enter(HUBD_MUTEX(root_hubd)); 2474 root_hubd->h_cleanup_needed = B_TRUE; 2475 if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) { 2476 root_hubd->h_cleanup_active = B_TRUE; 2477 mutex_exit(HUBD_MUTEX(root_hubd)); 2478 (void) thread_create(NULL, 0, 2479 hubd_root_hub_cleanup_thread, 2480 (void *)root_hubd, 0, &p0, TS_RUN, 2481 minclsyspri); 2482 } else { 2483 mutex_exit(HUBD_MUTEX(root_hubd)); 2484 } 2485 } 2486 2487 2488 /* 2489 * hubd_restore_device_state: 2490 * - set config for the hub 2491 * - power cycle all the ports 2492 * - for each port that was connected 2493 * - reset port 2494 * - assign addrs to the device on this port 2495 * - restart polling 2496 * - reset suspend flag 2497 */ 2498 static void 2499 hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd) 2500 { 2501 int rval; 2502 int retry; 2503 uint_t hub_prev_state; 2504 usb_port_t port; 2505 uint16_t status; 2506 uint16_t change; 2507 dev_info_t *ch_dip; 2508 boolean_t ehci_root_hub; 2509 2510 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2511 "hubd_restore_device_state:"); 2512 2513 mutex_enter(HUBD_MUTEX(hubd)); 2514 hub_prev_state = hubd->h_dev_state; 2515 ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN); 2516 2517 /* First bring the device to full power */ 2518 (void) hubd_pm_busy_component(hubd, dip, 0); 2519 mutex_exit(HUBD_MUTEX(hubd)); 2520 2521 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR); 2522 2523 if (!usba_is_root_hub(dip) && 2524 (usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0, 2525 DPRINT_MASK_HOTPLUG, 2526 USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) { 2527 2528 /* change the device state to disconnected */ 2529 mutex_enter(HUBD_MUTEX(hubd)); 2530 hubd->h_dev_state = USB_DEV_DISCONNECTED; 2531 (void) hubd_pm_idle_component(hubd, dip, 0); 2532 mutex_exit(HUBD_MUTEX(hubd)); 2533 2534 return; 2535 } 2536 2537 ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0); 2538 2539 mutex_enter(HUBD_MUTEX(hubd)); 2540 /* First turn off all port power */ 2541 rval = hubd_disable_all_port_power(hubd); 2542 if (rval != USB_SUCCESS) { 2543 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 2544 "hubd_restore_device_state:" 2545 "turning off port power failed"); 2546 } 2547 2548 /* Settling time before turning on again */ 2549 mutex_exit(HUBD_MUTEX(hubd)); 2550 delay(drv_usectohz(hubd_device_delay / 100)); 2551 mutex_enter(HUBD_MUTEX(hubd)); 2552 2553 /* enable power on all ports so we can see connects */ 2554 if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) { 2555 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2556 "hubd_restore_device_state: turn on port power failed"); 2557 2558 /* disable whatever was enabled */ 2559 (void) hubd_disable_all_port_power(hubd); 2560 2561 (void) hubd_pm_idle_component(hubd, dip, 0); 2562 mutex_exit(HUBD_MUTEX(hubd)); 2563 2564 return; 2565 } 2566 2567 /* 2568 * wait at least 3 frames before accessing devices 2569 * (note that delay's minimal time is one clock tick which 2570 * is 10ms unless hires_tick has been changed) 2571 */ 2572 mutex_exit(HUBD_MUTEX(hubd)); 2573 delay(drv_usectohz(10000)); 2574 mutex_enter(HUBD_MUTEX(hubd)); 2575 2576 hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER; 2577 2578 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 2579 USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle, 2580 "hubd_restore_device_state: port=%d", port); 2581 2582 /* 2583 * the childen_dips list may have dips that have been 2584 * already deallocated. we only get a post_detach notification 2585 * but not a destroy notification 2586 */ 2587 ch_dip = hubd->h_children_dips[port]; 2588 if (ch_dip) { 2589 /* get port status */ 2590 (void) hubd_determine_port_status(hubd, port, 2591 &status, &change, PORT_CHANGE_CSC); 2592 2593 /* check if it is truly connected */ 2594 if (status & PORT_STATUS_CCS) { 2595 /* 2596 * Now reset port and assign the device 2597 * its original address 2598 */ 2599 retry = 0; 2600 do { 2601 (void) hubd_reset_port(hubd, port); 2602 2603 /* required for ppx */ 2604 (void) hubd_enable_port(hubd, port); 2605 2606 if (retry) { 2607 mutex_exit(HUBD_MUTEX(hubd)); 2608 delay(drv_usectohz( 2609 hubd_device_delay/2)); 2610 mutex_enter(HUBD_MUTEX(hubd)); 2611 } 2612 2613 rval = hubd_setdevaddr(hubd, port); 2614 retry++; 2615 } while ((rval != USB_SUCCESS) && 2616 (retry < hubd_retry_enumerate)); 2617 2618 hubd_setdevconfig(hubd, port); 2619 2620 if (hub_prev_state == USB_DEV_DISCONNECTED) { 2621 /* post a connect event */ 2622 mutex_exit(HUBD_MUTEX(hubd)); 2623 hubd_post_event(hubd, port, 2624 USBA_EVENT_TAG_HOT_INSERTION); 2625 mutex_enter(HUBD_MUTEX(hubd)); 2626 } else { 2627 /* 2628 * Since we have this device connected 2629 * mark it reinserted to prevent 2630 * cleanup thread from stepping in. 2631 */ 2632 mutex_exit(HUBD_MUTEX(hubd)); 2633 mutex_enter(&(DEVI(ch_dip)->devi_lock)); 2634 DEVI_SET_DEVICE_REINSERTED(ch_dip); 2635 mutex_exit(&(DEVI(ch_dip)->devi_lock)); 2636 2637 /* 2638 * reopen pipes for children for 2639 * their DDI_RESUME 2640 */ 2641 rval = usba_persistent_pipe_open( 2642 usba_get_usba_device(ch_dip)); 2643 mutex_enter(HUBD_MUTEX(hubd)); 2644 ASSERT(rval == USB_SUCCESS); 2645 } 2646 } else { 2647 /* 2648 * Mark this dip for deletion as the device 2649 * is not physically present, and schedule 2650 * cleanup thread upon post resume 2651 */ 2652 mutex_exit(HUBD_MUTEX(hubd)); 2653 2654 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2655 hubd->h_log_handle, 2656 "hubd_restore_device_state: " 2657 "dip=%p on port=%d marked for cleanup", 2658 (void *)ch_dip, port); 2659 mutex_enter(&(DEVI(ch_dip)->devi_lock)); 2660 DEVI_SET_DEVICE_REMOVED(ch_dip); 2661 mutex_exit(&(DEVI(ch_dip)->devi_lock)); 2662 2663 mutex_enter(HUBD_MUTEX(hubd)); 2664 } 2665 } else if (ehci_root_hub) { 2666 /* get port status */ 2667 (void) hubd_determine_port_status(hubd, port, 2668 &status, &change, PORT_CHANGE_CSC); 2669 2670 /* check if it is truly connected */ 2671 if (status & PORT_STATUS_CCS) { 2672 /* 2673 * reset the port to find out if we have 2674 * 2.0 device connected or 1.X. A 2.0 2675 * device will still be seen as connected, 2676 * while a 1.X device will switch over to 2677 * the companion controller. 2678 */ 2679 (void) hubd_reset_port(hubd, port); 2680 2681 (void) hubd_determine_port_status(hubd, port, 2682 &status, &change, PORT_CHANGE_CSC); 2683 2684 if (status & 2685 (PORT_STATUS_CCS | PORT_STATUS_HSDA)) { 2686 /* 2687 * We have a USB 2.0 device 2688 * connected. Power cycle this port 2689 * so that hotplug thread can 2690 * enumerate this device. 2691 */ 2692 (void) hubd_toggle_port(hubd, port); 2693 } else { 2694 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2695 hubd->h_log_handle, 2696 "hubd_restore_device_state: " 2697 "device on port %d switched over", 2698 port); 2699 } 2700 } 2701 2702 } 2703 } 2704 2705 2706 /* if the device had remote wakeup earlier, enable it again */ 2707 if (hubd->h_hubpm->hubp_wakeup_enabled) { 2708 mutex_exit(HUBD_MUTEX(hubd)); 2709 (void) usb_handle_remote_wakeup(hubd->h_dip, 2710 USB_REMOTE_WAKEUP_ENABLE); 2711 mutex_enter(HUBD_MUTEX(hubd)); 2712 } 2713 2714 hubd->h_dev_state = USB_DEV_ONLINE; 2715 hubd_start_polling(hubd, 0); 2716 (void) hubd_pm_idle_component(hubd, dip, 0); 2717 mutex_exit(HUBD_MUTEX(hubd)); 2718 } 2719 2720 2721 /* 2722 * hubd_cleanup: 2723 * cleanup hubd and deallocate. this function is called for 2724 * handling attach failures and detaching including dynamic 2725 * reconfiguration. If called from attaching, it must clean 2726 * up the whole thing and return success. 2727 */ 2728 /*ARGSUSED*/ 2729 static int 2730 hubd_cleanup(dev_info_t *dip, hubd_t *hubd) 2731 { 2732 int circ, rval, old_dev_state; 2733 hub_power_t *hubpm; 2734 #ifdef DEBUG 2735 usb_port_t port; 2736 #endif 2737 2738 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2739 "hubd_cleanup:"); 2740 2741 if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) { 2742 goto done; 2743 } 2744 2745 /* ensure we are the only one active */ 2746 ndi_devi_enter(dip, &circ); 2747 2748 mutex_enter(HUBD_MUTEX(hubd)); 2749 2750 /* Cleanup failure is only allowed if called from detach */ 2751 if (DEVI_IS_DETACHING(dip)) { 2752 dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip; 2753 2754 /* 2755 * We are being called from detach. 2756 * Fail immediately if the hotplug thread is running 2757 * else set the dev_state to disconnected so that 2758 * hotplug thread just exits without doing anything. 2759 */ 2760 if (hubd->h_bus_ctls || hubd->h_bus_pwr || 2761 hubd->h_hotplug_thread) { 2762 mutex_exit(HUBD_MUTEX(hubd)); 2763 ndi_devi_exit(dip, circ); 2764 2765 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 2766 "hubd_cleanup: hotplug thread/bus ctl active " 2767 "- failing detach"); 2768 2769 return (USB_FAILURE); 2770 } 2771 2772 /* 2773 * if the deathrow thread is still active or about 2774 * to become active, fail detach 2775 * the roothup can only be detached if nexus drivers 2776 * are unloaded or explicitly offlined 2777 */ 2778 if (rh_dip == dip) { 2779 if (hubd->h_cleanup_needed || 2780 hubd->h_cleanup_active) { 2781 mutex_exit(HUBD_MUTEX(hubd)); 2782 ndi_devi_exit(dip, circ); 2783 2784 USB_DPRINTF_L2(DPRINT_MASK_ATTA, 2785 hubd->h_log_handle, 2786 "hubd_cleanup: deathrow still active?" 2787 "- failing detach"); 2788 2789 return (USB_FAILURE); 2790 } 2791 } 2792 } 2793 2794 old_dev_state = hubd->h_dev_state; 2795 hubd->h_dev_state = USB_DEV_DISCONNECTED; 2796 2797 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2798 "hubd_cleanup: stop polling"); 2799 hubd_close_intr_pipe(hubd); 2800 2801 ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr || 2802 hubd->h_hotplug_thread) == 0); 2803 mutex_exit(HUBD_MUTEX(hubd)); 2804 2805 /* 2806 * deallocate events, if events are still registered 2807 * (ie. children still attached) then we have to fail the detach 2808 */ 2809 if (hubd->h_ndi_event_hdl) { 2810 2811 rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl); 2812 if (DEVI_IS_ATTACHING(dip)) { 2813 2814 /* It must return success if attaching. */ 2815 ASSERT(rval == NDI_SUCCESS); 2816 2817 } else if (rval != NDI_SUCCESS) { 2818 2819 USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle, 2820 "hubd_cleanup: ndi_event_free_hdl failed"); 2821 ndi_devi_exit(dip, circ); 2822 2823 return (USB_FAILURE); 2824 2825 } 2826 } 2827 2828 mutex_enter(HUBD_MUTEX(hubd)); 2829 2830 if (hubd->h_init_state & HUBD_CHILDREN_CREATED) { 2831 #ifdef DEBUG 2832 for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) { 2833 ASSERT(hubd->h_usba_devices[port] == NULL); 2834 ASSERT(hubd->h_children_dips[port] == NULL); 2835 } 2836 #endif 2837 kmem_free(hubd->h_children_dips, hubd->h_cd_list_length); 2838 kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length); 2839 } 2840 2841 /* 2842 * Disable the event callbacks first, after this point, event 2843 * callbacks will never get called. Note we shouldn't hold 2844 * mutex while unregistering events because there may be a 2845 * competing event callback thread. Event callbacks are done 2846 * with ndi mutex held and this can cause a potential deadlock. 2847 * Note that cleanup can't fail after deregistration of events. 2848 */ 2849 if (hubd->h_init_state & HUBD_EVENTS_REGISTERED) { 2850 mutex_exit(HUBD_MUTEX(hubd)); 2851 usb_unregister_event_cbs(dip, &hubd_events); 2852 hubd_unregister_cpr_callback(hubd); 2853 mutex_enter(HUBD_MUTEX(hubd)); 2854 } 2855 2856 /* restore the old dev state so that device can be put into low power */ 2857 hubd->h_dev_state = old_dev_state; 2858 hubpm = hubd->h_hubpm; 2859 2860 if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) { 2861 (void) hubd_pm_busy_component(hubd, dip, 0); 2862 mutex_exit(HUBD_MUTEX(hubd)); 2863 if (hubd->h_hubpm->hubp_wakeup_enabled) { 2864 /* 2865 * Bring the hub to full power before 2866 * issuing the disable remote wakeup command 2867 */ 2868 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR); 2869 2870 if ((rval = usb_handle_remote_wakeup(hubd->h_dip, 2871 USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) { 2872 USB_DPRINTF_L2(DPRINT_MASK_PM, 2873 hubd->h_log_handle, 2874 "hubd_cleanup: disable remote wakeup " 2875 "fails=%d", rval); 2876 } 2877 } 2878 2879 (void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF); 2880 2881 mutex_enter(HUBD_MUTEX(hubd)); 2882 (void) hubd_pm_idle_component(hubd, dip, 0); 2883 } 2884 2885 if (hubpm) { 2886 if (hubpm->hubp_child_pwrstate) { 2887 kmem_free(hubpm->hubp_child_pwrstate, 2888 MAX_PORTS + 1); 2889 } 2890 kmem_free(hubpm, sizeof (hub_power_t)); 2891 } 2892 mutex_exit(HUBD_MUTEX(hubd)); 2893 2894 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 2895 "hubd_cleanup: freeing space"); 2896 2897 if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) { 2898 rval = usba_hubdi_unregister(dip); 2899 ASSERT(rval == USB_SUCCESS); 2900 } 2901 2902 if (hubd->h_init_state & HUBD_LOCKS_DONE) { 2903 mutex_destroy(HUBD_MUTEX(hubd)); 2904 cv_destroy(&hubd->h_cv_reset_port); 2905 cv_destroy(&hubd->h_cv_hotplug_dev); 2906 } 2907 2908 ndi_devi_exit(dip, circ); 2909 2910 if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) { 2911 ddi_remove_minor_node(dip, NULL); 2912 } 2913 2914 if (usba_is_root_hub(dip)) { 2915 usb_pipe_close(dip, hubd->h_default_pipe, 2916 USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL); 2917 } 2918 2919 done: 2920 if (hubd->h_ancestry_str) { 2921 kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN); 2922 } 2923 2924 usb_client_detach(dip, hubd->h_dev_data); 2925 2926 usb_free_log_hdl(hubd->h_log_handle); 2927 2928 if (!usba_is_root_hub(dip)) { 2929 ddi_soft_state_free(hubd_statep, ddi_get_instance(dip)); 2930 } 2931 2932 ddi_prop_remove_all(dip); 2933 2934 return (USB_SUCCESS); 2935 } 2936 2937 2938 /* 2939 * hubd_determine_port_connection: 2940 * Determine which port is in connect status but does not 2941 * have connect status change bit set, and mark port change 2942 * bit accordingly. 2943 * This function is applied during hub attach time. 2944 */ 2945 static usb_port_mask_t 2946 hubd_determine_port_connection(hubd_t *hubd) 2947 { 2948 usb_port_t port; 2949 usb_hub_descr_t *hub_descr; 2950 uint16_t status; 2951 uint16_t change; 2952 usb_port_mask_t port_change = 0; 2953 2954 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 2955 2956 hub_descr = &hubd->h_hub_descr; 2957 2958 for (port = 1; port <= hub_descr->bNbrPorts; port++) { 2959 2960 (void) hubd_determine_port_status(hubd, port, &status, 2961 &change, 0); 2962 2963 /* Check if port is in connect status */ 2964 if (!(status & PORT_STATUS_CCS)) { 2965 2966 continue; 2967 } 2968 2969 /* 2970 * Check if port Connect Status Change bit has been set. 2971 * If already set, the connection will be handled by 2972 * intr polling callback, not during attach. 2973 */ 2974 if (change & PORT_CHANGE_CSC) { 2975 2976 continue; 2977 } 2978 2979 port_change |= 1 << port; 2980 } 2981 2982 return (port_change); 2983 } 2984 2985 2986 /* 2987 * hubd_check_ports: 2988 * - get hub descriptor 2989 * - check initial port status 2990 * - enable power on all ports 2991 * - enable polling on ep1 2992 */ 2993 static int 2994 hubd_check_ports(hubd_t *hubd) 2995 { 2996 int rval; 2997 usb_port_mask_t port_change = 0; 2998 hubd_hotplug_arg_t *arg; 2999 3000 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 3001 3002 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 3003 "hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip)); 3004 3005 /* 3006 * First turn off all port power 3007 */ 3008 if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) { 3009 3010 /* disable whatever was enabled */ 3011 (void) hubd_disable_all_port_power(hubd); 3012 3013 return (rval); 3014 } 3015 3016 /* 3017 * do not switch on immediately (instantly on root hub) 3018 * and allow time to settle 3019 */ 3020 mutex_exit(HUBD_MUTEX(hubd)); 3021 delay(drv_usectohz(10000)); 3022 mutex_enter(HUBD_MUTEX(hubd)); 3023 3024 /* 3025 * enable power on all ports so we can see connects 3026 */ 3027 if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) { 3028 /* disable whatever was enabled */ 3029 (void) hubd_disable_all_port_power(hubd); 3030 3031 return (rval); 3032 } 3033 3034 /* wait at least 3 frames before accessing devices */ 3035 mutex_exit(HUBD_MUTEX(hubd)); 3036 delay(drv_usectohz(10000)); 3037 mutex_enter(HUBD_MUTEX(hubd)); 3038 3039 /* 3040 * allocate arrays for saving the dips of each child per port 3041 * 3042 * ports go from 1 - n, allocate 1 more entry 3043 */ 3044 hubd->h_cd_list_length = 3045 (sizeof (dev_info_t **)) * (hubd->h_hub_descr.bNbrPorts + 1); 3046 3047 hubd->h_children_dips = (dev_info_t **)kmem_zalloc( 3048 hubd->h_cd_list_length, KM_SLEEP); 3049 hubd->h_usba_devices = (usba_device_t **)kmem_zalloc( 3050 hubd->h_cd_list_length, KM_SLEEP); 3051 3052 hubd->h_init_state |= HUBD_CHILDREN_CREATED; 3053 3054 mutex_exit(HUBD_MUTEX(hubd)); 3055 arg = (hubd_hotplug_arg_t *)kmem_zalloc( 3056 sizeof (hubd_hotplug_arg_t), KM_SLEEP); 3057 mutex_enter(HUBD_MUTEX(hubd)); 3058 3059 if ((rval = hubd_open_intr_pipe(hubd)) != USB_SUCCESS) { 3060 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3061 3062 return (rval); 3063 } 3064 3065 hubd_start_polling(hubd, 0); 3066 3067 /* 3068 * Some hub devices, like the embedded hub in the CKS ErgoMagic 3069 * keyboard, may only have connection status bit set, but not 3070 * have connect status change bit set when a device has been 3071 * connected to its downstream port before the hub is enumerated. 3072 * Then when the hub is in enumeration, the devices connected to 3073 * it cannot be detected by the intr pipe and won't be enumerated. 3074 * We need to check such situation here and enumerate the downstream 3075 * devices for such hubs. 3076 */ 3077 port_change = hubd_determine_port_connection(hubd); 3078 3079 if (port_change) { 3080 hubd_pm_busy_component(hubd, hubd->h_dip, 0); 3081 3082 arg->hubd = hubd; 3083 arg->hotplug_during_attach = B_TRUE; 3084 hubd->h_port_change |= port_change; 3085 3086 USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle, 3087 "hubd_check_ports: port change=0x%x, need to connect", 3088 hubd->h_port_change); 3089 3090 if (usb_async_req(hubd->h_dip, hubd_hotplug_thread, 3091 (void *)arg, 0) == USB_SUCCESS) { 3092 hubd->h_hotplug_thread++; 3093 } else { 3094 /* mark this device as idle */ 3095 hubd_pm_idle_component(hubd, hubd->h_dip, 0); 3096 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3097 } 3098 } else { 3099 kmem_free(arg, sizeof (hubd_hotplug_arg_t)); 3100 } 3101 3102 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 3103 "hubd_check_ports done"); 3104 3105 return (USB_SUCCESS); 3106 } 3107 3108 3109 /* 3110 * hubd_get_hub_descriptor: 3111 */ 3112 static int 3113 hubd_get_hub_descriptor(hubd_t *hubd) 3114 { 3115 usb_hub_descr_t *hub_descr = &hubd->h_hub_descr; 3116 mblk_t *data = NULL; 3117 usb_cr_t completion_reason; 3118 usb_cb_flags_t cb_flags; 3119 uint16_t length; 3120 int rval; 3121 usb_req_attrs_t attr = 0; 3122 3123 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3124 "hubd_get_hub_descriptor:"); 3125 3126 if ((hubd->h_dev_data->dev_descr->idVendor == USB_HUB_INTEL_VID) && 3127 (hubd->h_dev_data->dev_descr->idProduct == USB_HUB_INTEL_PID)) { 3128 attr = USB_ATTRS_SHORT_XFER_OK; 3129 } 3130 3131 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 3132 ASSERT(hubd->h_default_pipe != 0); 3133 3134 /* get hub descriptor length first by requesting 8 bytes only */ 3135 mutex_exit(HUBD_MUTEX(hubd)); 3136 3137 if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 3138 hubd->h_default_pipe, 3139 HUB_CLASS_REQ_TYPE, 3140 USB_REQ_GET_DESCR, /* bRequest */ 3141 USB_DESCR_TYPE_SETUP_HUB, /* wValue */ 3142 0, /* wIndex */ 3143 8, /* wLength */ 3144 &data, 0, 3145 &completion_reason, &cb_flags, 0)) != USB_SUCCESS) { 3146 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 3147 "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d", 3148 completion_reason, cb_flags, rval); 3149 freemsg(data); 3150 mutex_enter(HUBD_MUTEX(hubd)); 3151 3152 return (rval); 3153 } 3154 3155 length = *(data->b_rptr); 3156 3157 if (length > 8) { 3158 freemsg(data); 3159 data = NULL; 3160 3161 /* get complete hub descriptor */ 3162 rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip, 3163 hubd->h_default_pipe, 3164 HUB_CLASS_REQ_TYPE, 3165 USB_REQ_GET_DESCR, /* bRequest */ 3166 USB_DESCR_TYPE_SETUP_HUB, /* wValue */ 3167 0, /* wIndex */ 3168 length, /* wLength */ 3169 &data, attr, 3170 &completion_reason, &cb_flags, 0); 3171 3172 /* 3173 * Hub descriptor data less than 9 bytes is not valid and 3174 * may cause trouble if we use it. See USB2.0 Tab11-13. 3175 */ 3176 if ((rval != USB_SUCCESS) || (MBLKL(data) <= 8)) { 3177 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 3178 "get hub descriptor failed: " 3179 "cr=%d cb_fl=0x%x rval=%d, len=%ld", 3180 completion_reason, cb_flags, rval, 3181 (data)?MBLKL(data):0); 3182 freemsg(data); 3183 mutex_enter(HUBD_MUTEX(hubd)); 3184 3185 return (rval); 3186 } 3187 } 3188 3189 mutex_enter(HUBD_MUTEX(hubd)); 3190 3191 /* parse the hub descriptor */ 3192 /* only 32 ports are supported at present */ 3193 ASSERT(*(data->b_rptr + 2) <= 32); 3194 if (usb_parse_CV_descr("cccscccccc", 3195 data->b_rptr, MBLKL(data), 3196 (void *)hub_descr, sizeof (usb_hub_descr_t)) == 0) { 3197 USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle, 3198 "parsing hub descriptor failed"); 3199 3200 freemsg(data); 3201 3202 return (USB_FAILURE); 3203 } 3204 3205 freemsg(data); 3206 3207 USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle, 3208 "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x " 3209 "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval, 3210 hub_descr->bNbrPorts, hub_descr->wHubCharacteristics, 3211 hub_descr->bPwrOn2PwrGood, hub_descr->bHubContrCurrent); 3212 3213 if (hub_descr->bNbrPorts > MAX_PORTS) { 3214 USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle, 3215 "Hub driver supports max of %d ports on hub. " 3216 "Hence using the first %d port of %d ports available", 3217 MAX_PORTS, MAX_PORTS, hub_descr->bNbrPorts); 3218 3219 hub_descr->bNbrPorts = MAX_PORTS; 3220 } 3221 3222 return (USB_SUCCESS); 3223 } 3224 3225 3226 /* 3227 * hubd_get_hub_status_words: 3228 */ 3229 static int 3230 hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status) 3231 { 3232 usb_cr_t completion_reason; 3233 usb_cb_flags_t cb_flags; 3234 mblk_t *data = NULL; 3235 3236 ASSERT(mutex_owned(HUBD_MUTEX(hubd))); 3237 3238 mutex_exit(HUBD_MUTEX(hubd)); 3239 3240 if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe, 3241 HUB_CLASS_REQ_TYPE, 3242 USB_REQ_GET_STATUS, 3243 0, 3244 0, 3245 GET_STATUS_LENGTH, 3246 &data, 0, 3247 &completion_reason, &cb_flags, 0) != USB_SUCCESS) { 3248 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle, 3249 "get hub status failed: cr=%d cb=0x%x", 3250 completion_reason, cb_flags); 3251 3252 if (data) { 3253 freemsg(data); 3254 } 3255 3256 mutex_enter(HUBD_MUTEX(hubd)); 3257 3258 return (USB_FAILURE); 3259 } 3260 3261 mutex_enter(HUBD_MUTEX(hubd)); 3262 3263 status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr); 3264 status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2); 3265 3266 USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle, 3267 "hub status=0x%x change=0x%x", status[0], status[1]); 3268 3269 freemsg(data); 3270 3271 return (USB_SUCCESS); 3272 } 3273 3274 3275 /* 3276 * hubd_open_intr_pipe: 3277 * we read all descriptors first for curiosity and then simply 3278 * open the pipe 3279 */ 3280 static int 3281 hubd_open_intr_pipe(hubd_t *hubd) 3282 { 3283 int rval; 3284 3285 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3286 "hubd_open_intr_pipe:"); 3287 3288 ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE); 3289 3290 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING; 3291 mutex_exit(HUBD_MUTEX(hubd)); 3292 3293 if ((rval = usb_pipe_open(hubd->h_dip, 3294 &hubd->h_ep1_descr, &hubd->h_pipe_policy, 3295 0, &hubd->h_ep1_ph)) != USB_SUCCESS) { 3296 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle, 3297 "open intr pipe failed (%d)", rval); 3298 3299 mutex_enter(HUBD_MUTEX(hubd)); 3300 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE; 3301 3302 return (rval); 3303 } 3304 3305 mutex_enter(HUBD_MUTEX(hubd)); 3306 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE; 3307 3308 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3309 "open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph); 3310 3311 return (USB_SUCCESS); 3312 } 3313 3314 3315 /* 3316 * hubd_start_polling: 3317 * start or restart the polling 3318 */ 3319 static void 3320 hubd_start_polling(hubd_t *hubd, int always) 3321 { 3322 usb_intr_req_t *reqp; 3323 int rval; 3324 usb_pipe_state_t pipe_state; 3325 3326 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3327 "start polling: always=%d dev_state=%d pipe_state=%d\n\t" 3328 "thread=%d ep1_ph=0x%p", 3329 always, hubd->h_dev_state, hubd->h_intr_pipe_state, 3330 hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph); 3331 3332 /* 3333 * start or restart polling on the intr pipe 3334 * only if hotplug thread is not running 3335 */ 3336 if ((always == HUBD_ALWAYS_START_POLLING) || 3337 ((hubd->h_dev_state == USB_DEV_ONLINE) && 3338 (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) && 3339 (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) { 3340 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3341 "start polling requested"); 3342 3343 reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP); 3344 3345 reqp->intr_client_private = (usb_opaque_t)hubd; 3346 reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK | 3347 USB_ATTRS_AUTOCLEARING; 3348 reqp->intr_len = hubd->h_ep1_descr.wMaxPacketSize; 3349 reqp->intr_cb = hubd_read_cb; 3350 reqp->intr_exc_cb = hubd_exception_cb; 3351 mutex_exit(HUBD_MUTEX(hubd)); 3352 if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp, 3353 USB_FLAGS_SLEEP)) != USB_SUCCESS) { 3354 USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle, 3355 "start polling failed, rval=%d", rval); 3356 usb_free_intr_req(reqp); 3357 } 3358 3359 rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state, 3360 USB_FLAGS_SLEEP); 3361 if (pipe_state != USB_PIPE_STATE_ACTIVE) { 3362 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 3363 "intr pipe state=%d, rval=%d", pipe_state, rval); 3364 } 3365 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3366 "start polling request 0x%p", (void *)reqp); 3367 3368 mutex_enter(HUBD_MUTEX(hubd)); 3369 } 3370 } 3371 3372 3373 /* 3374 * hubd_stop_polling 3375 * stop polling but do not close the pipe 3376 */ 3377 static void 3378 hubd_stop_polling(hubd_t *hubd) 3379 { 3380 int rval; 3381 usb_pipe_state_t pipe_state; 3382 3383 if (hubd->h_ep1_ph) { 3384 USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle, 3385 "hubd_stop_polling:"); 3386 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_STOPPED; 3387 mutex_exit(HUBD_MUTEX(hubd)); 3388 3389 usb_pipe_stop_intr_polling(hubd->h_ep1_ph, USB_FLAGS_SLEEP); 3390 rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state, 3391 USB_FLAGS_SLEEP); 3392 3393 if (pipe_state != USB_PIPE_STATE_IDLE) { 3394 USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle, 3395 "intr pipe state=%d, rval=%d", pipe_state, rval); 3396 } 3397 mutex_enter(HUBD_MUTEX(hubd)); 3398 if (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_STOPPED) { 3399 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE; 3400 } 3401 } 3402 } 3403 3404 3405 /* 3406 * hubd_close_intr_pipe: 3407 * close the pipe (which also stops the polling 3408 * and wait for the hotplug thread to exit 3409 */ 3410 static void 3411 hubd_close_intr_pipe(hubd_t *hubd) 3412 { 3413 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3414 "hubd_close_intr_pipe:"); 3415 3416 /* 3417 * Now that no async operation is outstanding on pipe, 3418 * we can change the state to HUBD_INTR_PIPE_CLOSING 3419 */ 3420 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_CLOSING; 3421 3422 ASSERT(hubd->h_hotplug_thread == 0); 3423 3424 if (hubd->h_ep1_ph) { 3425 mutex_exit(HUBD_MUTEX(hubd)); 3426 usb_pipe_close(hubd->h_dip, hubd->h_ep1_ph, USB_FLAGS_SLEEP, 3427 NULL, NULL); 3428 mutex_enter(HUBD_MUTEX(hubd)); 3429 hubd->h_ep1_ph = NULL; 3430 } 3431 3432 hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE; 3433 } 3434 3435 3436 /* 3437 * hubd_exception_cb 3438 * interrupt ep1 exception callback function. 3439 * this callback executes in taskq thread context and assumes 3440 * autoclearing 3441 */ 3442 /*ARGSUSED*/ 3443 static void 3444 hubd_exception_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp) 3445 { 3446 hubd_t *hubd = (hubd_t *)(reqp->intr_client_private); 3447 3448 USB_DPRINTF_L2(DPRINT_MASK_CALLBACK, hubd->h_log_handle, 3449 "hubd_exception_cb: " 3450 "req=0x%p cr=%d data=0x%p cb_flags=0x%x", (void *)reqp, 3451 reqp->intr_completion_reason, (void *)reqp->intr_data, 3452 reqp->intr_cb_flags); 3453 3454 ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0); 3455 3456 mutex_enter(HUBD_MUTEX(hubd)); 3457 (void) hubd_pm_busy_component(hubd, hubd->h_dip, 0); 3458 3459 switch (reqp->intr_completion_reason) { 3460 case USB_CR_PIPE_RESET: 3461 /* only restart polling after autoclearing */ 3462 if ((hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) && 3463 (hubd->h_port_reset_wait == 0)) { 3464 hubd_start_polling(hubd, 0); 3465 } 3466 3467 break; 3468 case USB_CR_DEV_NOT_RESP: 3469 case USB_CR_STOPPED_POLLING: 3470 case USB_CR_PIPE_CLOSING: 3471 case USB_CR_UNSPECIFIED_ERR: 3472 /* never restart polling on these conditions */ 3473 default: 3474 /* for all others, wait for the autoclearing PIPE_RESET cb */ 3475 3476 break; 3477 } 3478 3479 usb_free_intr_req(reqp); 3480 (void) hubd_pm_idle_component(hubd, hubd->h_dip, 0); 3481 mutex_exit(HUBD_MUTEX(hubd)); 3482 } 3483 3484 3485 /* 3486 * helper function to convert LE bytes to a portmask 3487 */ 3488 static usb_port_mask_t 3489 hubd_mblk2portmask(mblk_t *data) 3490 { 3491 int len = min(MBLKL(data), sizeof (usb_port_mask_t)); 3492 usb_port_mask_t rval = 0; 3493 int i; 3494 3495 for (i = 0; i < len; i++) { 3496 rval |= data->b_rptr[i] << (i * 8); 3497 } 3498 3499 return (rval); 3500 } 3501 3502 3503 /* 3504 * hubd_read_cb: 3505 * interrupt ep1 callback function 3506 * 3507 * the status indicates just a change on the pipe with no indication 3508 * of what the change was 3509 * 3510 * known conditions: 3511 * - reset port completion 3512 * - connect 3513 * - disconnect 3514 * 3515 * for handling the hotplugging, create a new thread that can do 3516 * synchronous usba calls 3517 */ 3518 static void 3519 hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp) 3520 { 3521 hubd_t *hubd = (hubd_t *)(reqp->intr_client_private); 3522 size_t length; 3523 mblk_t *data = reqp->intr_data; 3524 int mem_flag = 0; 3525 hubd_hotplug_arg_t *arg; 3526 3527 USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle, 3528 "hubd_read_cb: ph=0x%p req=0x%p", (void *)pipe, (void *)reqp); 3529 3530 ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0); 3531 3532 /* 3533 * At present, we are not handling notification for completion of 3534 * asynchronous pipe reset, for which this data ptr could be NULL 3535 */ 3536 3537 if (data == NULL) { 3538 usb_free_intr_req(reqp); 3539 3540 return; 3541 } 3542 3543 arg = (hubd_hotplug_arg_t *)kmem_zalloc( 3544 sizeof (hubd_hotplug_arg_t), KM_SLEEP); 3545 mem_flag = 1; 3546 3547 mutex_enter(HUBD_MUTEX(hubd)); 3548 3549 if ((hubd->h_dev_state == USB_DEV_SUSPENDED) || 3550 (hubd->h_intr_pipe_state != HUBD_INTR_PIPE_ACTIVE)) { 3551 mutex_exit(HUBD_MUTEX(hubd)); 3552