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