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