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