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