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