xref: /titanic_50/usr/src/uts/common/io/usb/usba/hubdi.c (revision 1e67f0f0096be223aa7f51802953bebd95866ddc)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 /*
28  * USBA: Solaris USB Architecture support for the hub
29  * including root hub
30  * Most of the code for hubd resides in this file and
31  * is shared between the HCD root hub support and hubd
32  */
33 #define	USBA_FRAMEWORK
34 #include <sys/usb/usba.h>
35 #include <sys/usb/usba/usba_devdb.h>
36 #include <sys/sunndi.h>
37 #include <sys/usb/usba/usba_impl.h>
38 #include <sys/usb/usba/usba_types.h>
39 #include <sys/usb/usba/hubdi.h>
40 #include <sys/usb/usba/hcdi_impl.h>
41 #include <sys/usb/hubd/hub.h>
42 #include <sys/usb/hubd/hubdvar.h>
43 #include <sys/usb/hubd/hubd_impl.h>
44 #include <sys/kobj.h>
45 #include <sys/kobj_lex.h>
46 #include <sys/fs/dv_node.h>
47 #include <sys/strsun.h>
48 
49 /*
50  * External functions
51  */
52 extern boolean_t consconfig_console_is_ready(void);
53 
54 /*
55  * Prototypes for static functions
56  */
57 static	int	usba_hubdi_bus_ctl(
58 			dev_info_t		*dip,
59 			dev_info_t		*rdip,
60 			ddi_ctl_enum_t		op,
61 			void			*arg,
62 			void			*result);
63 
64 static int	usba_hubdi_map_fault(
65 			dev_info_t		*dip,
66 			dev_info_t		*rdip,
67 			struct hat		*hat,
68 			struct seg		*seg,
69 			caddr_t 		addr,
70 			struct devpage		*dp,
71 			pfn_t			pfn,
72 			uint_t			prot,
73 			uint_t			lock);
74 
75 static int hubd_busop_get_eventcookie(dev_info_t *dip,
76 			dev_info_t *rdip,
77 			char *eventname,
78 			ddi_eventcookie_t *cookie);
79 static int hubd_busop_add_eventcall(dev_info_t *dip,
80 			dev_info_t *rdip,
81 			ddi_eventcookie_t cookie,
82 			void (*callback)(dev_info_t *dip,
83 				ddi_eventcookie_t cookie, void *arg,
84 				void *bus_impldata),
85 			void *arg, ddi_callback_id_t *cb_id);
86 static int hubd_busop_remove_eventcall(dev_info_t *dip,
87 			ddi_callback_id_t cb_id);
88 static int hubd_bus_config(dev_info_t *dip,
89 			uint_t flag,
90 			ddi_bus_config_op_t op,
91 			void *arg,
92 			dev_info_t **child);
93 static int hubd_bus_unconfig(dev_info_t *dip,
94 			uint_t flag,
95 			ddi_bus_config_op_t op,
96 			void *arg);
97 static int hubd_bus_power(dev_info_t *dip, void *impl_arg,
98 			pm_bus_power_op_t op, void *arg, void *result);
99 
100 static usb_port_t  hubd_get_port_num(hubd_t *, struct devctl_iocdata *);
101 static dev_info_t *hubd_get_child_dip(hubd_t *, usb_port_t);
102 static uint_t hubd_cfgadm_state(hubd_t *, usb_port_t);
103 static int hubd_toggle_port(hubd_t *, usb_port_t);
104 static void hubd_register_cpr_callback(hubd_t *);
105 static void hubd_unregister_cpr_callback(hubd_t *);
106 
107 /*
108  * Busops vector for USB HUB's
109  */
110 struct bus_ops usba_hubdi_busops =	{
111 	BUSO_REV,
112 	nullbusmap,			/* bus_map */
113 	NULL,				/* bus_get_intrspec */
114 	NULL,				/* bus_add_intrspec */
115 	NULL,				/* bus_remove_intrspec */
116 	usba_hubdi_map_fault,		/* bus_map_fault */
117 	ddi_dma_map,			/* bus_dma_map */
118 	ddi_dma_allochdl,
119 	ddi_dma_freehdl,
120 	ddi_dma_bindhdl,
121 	ddi_dma_unbindhdl,
122 	ddi_dma_flush,
123 	ddi_dma_win,
124 	ddi_dma_mctl,			/* bus_dma_ctl */
125 	usba_hubdi_bus_ctl,		/* bus_ctl */
126 	ddi_bus_prop_op,		/* bus_prop_op */
127 	hubd_busop_get_eventcookie,
128 	hubd_busop_add_eventcall,
129 	hubd_busop_remove_eventcall,
130 	NULL,				/* bus_post_event */
131 	NULL,				/* bus_intr_ctl */
132 	hubd_bus_config,		/* bus_config */
133 	hubd_bus_unconfig,		/* bus_unconfig */
134 	NULL,				/* bus_fm_init */
135 	NULL,				/* bus_fm_fini */
136 	NULL,				/* bus_fm_access_enter */
137 	NULL,				/* bus_fm_access_exit */
138 	hubd_bus_power			/* bus_power */
139 };
140 
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 
3120 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3121 	    "hubd_get_hub_descriptor:");
3122 
3123 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3124 	ASSERT(hubd->h_default_pipe != 0);
3125 
3126 	/* get hub descriptor length first by requesting 8 bytes only */
3127 	mutex_exit(HUBD_MUTEX(hubd));
3128 
3129 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3130 	    hubd->h_default_pipe,
3131 	    HUB_CLASS_REQ_TYPE,
3132 	    USB_REQ_GET_DESCR,		/* bRequest */
3133 	    USB_DESCR_TYPE_SETUP_HUB,	/* wValue */
3134 	    0,				/* wIndex */
3135 	    8,				/* wLength */
3136 	    &data, 0,
3137 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
3138 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3139 		    "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
3140 		    completion_reason, cb_flags, rval);
3141 		freemsg(data);
3142 		mutex_enter(HUBD_MUTEX(hubd));
3143 
3144 		return (rval);
3145 	}
3146 
3147 	length = *(data->b_rptr);
3148 
3149 	if (length > 8) {
3150 		freemsg(data);
3151 		data = NULL;
3152 
3153 		/* get complete hub descriptor */
3154 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3155 		    hubd->h_default_pipe,
3156 		    HUB_CLASS_REQ_TYPE,
3157 		    USB_REQ_GET_DESCR,		/* bRequest */
3158 		    USB_DESCR_TYPE_SETUP_HUB,	/* wValue */
3159 		    0,				/* wIndex */
3160 		    length,			/* wLength */
3161 		    &data, 0,
3162 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
3163 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3164 			    "get hub descriptor failed: "
3165 			    "cr=%d cb_fl=0x%x rval=%d",
3166 			    completion_reason, cb_flags, rval);
3167 			freemsg(data);
3168 			mutex_enter(HUBD_MUTEX(hubd));
3169 
3170 			return (rval);
3171 		}
3172 	}
3173 
3174 	mutex_enter(HUBD_MUTEX(hubd));
3175 
3176 	/* parse the hub descriptor */
3177 	/* only 32 ports are supported at present */
3178 	ASSERT(*(data->b_rptr + 2) <= 32);
3179 	if (usb_parse_CV_descr("cccscccccc",
3180 	    data->b_rptr, MBLKL(data),
3181 	    (void *)hub_descr, sizeof (usb_hub_descr_t)) == 0) {
3182 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3183 		    "parsing hub descriptor failed");
3184 
3185 		freemsg(data);
3186 
3187 		return (USB_FAILURE);
3188 	}
3189 
3190 	freemsg(data);
3191 
3192 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
3193 	    "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
3194 	    "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval,
3195 	    hub_descr->bNbrPorts, hub_descr->wHubCharacteristics,
3196 	    hub_descr->bPwrOn2PwrGood, hub_descr->bHubContrCurrent);
3197 
3198 	if (hub_descr->bNbrPorts > MAX_PORTS) {
3199 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
3200 		    "Hub driver supports max of %d ports on hub. "
3201 		    "Hence using the first %d port of %d ports available",
3202 		    MAX_PORTS, MAX_PORTS, hub_descr->bNbrPorts);
3203 
3204 		hub_descr->bNbrPorts = MAX_PORTS;
3205 	}
3206 
3207 	return (USB_SUCCESS);
3208 }
3209 
3210 
3211 /*
3212  * hubd_get_hub_status_words:
3213  */
3214 static int
3215 hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status)
3216 {
3217 	usb_cr_t	completion_reason;
3218 	usb_cb_flags_t	cb_flags;
3219 	mblk_t		*data = NULL;
3220 
3221 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3222 
3223 	mutex_exit(HUBD_MUTEX(hubd));
3224 
3225 	if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe,
3226 	    HUB_CLASS_REQ_TYPE,
3227 	    USB_REQ_GET_STATUS,
3228 	    0,
3229 	    0,
3230 	    GET_STATUS_LENGTH,
3231 	    &data, 0,
3232 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
3233 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3234 		    "get hub status failed: cr=%d cb=0x%x",
3235 		    completion_reason, cb_flags);
3236 
3237 		if (data) {
3238 			freemsg(data);
3239 		}
3240 
3241 		mutex_enter(HUBD_MUTEX(hubd));
3242 
3243 		return (USB_FAILURE);
3244 	}
3245 
3246 	mutex_enter(HUBD_MUTEX(hubd));
3247 
3248 	status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
3249 	status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
3250 
3251 	USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
3252 	    "hub status=0x%x change=0x%x", status[0], status[1]);
3253 
3254 	freemsg(data);
3255 
3256 	return (USB_SUCCESS);
3257 }
3258 
3259 
3260 /*
3261  * hubd_open_intr_pipe:
3262  *	we read all descriptors first for curiosity and then simply
3263  *	open the pipe
3264  */
3265 static int
3266 hubd_open_intr_pipe(hubd_t	*hubd)
3267 {
3268 	int			rval;
3269 
3270 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3271 	    "hubd_open_intr_pipe:");
3272 
3273 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE);
3274 
3275 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING;
3276 	mutex_exit(HUBD_MUTEX(hubd));
3277 
3278 	if ((rval = usb_pipe_open(hubd->h_dip,
3279 	    &hubd->h_ep1_descr, &hubd->h_pipe_policy,
3280 	    0, &hubd->h_ep1_ph)) != USB_SUCCESS) {
3281 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3282 		    "open intr pipe failed (%d)", rval);
3283 
3284 		mutex_enter(HUBD_MUTEX(hubd));
3285 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
3286 
3287 		return (rval);
3288 	}
3289 
3290 	mutex_enter(HUBD_MUTEX(hubd));
3291 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
3292 
3293 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3294 	    "open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph);
3295 
3296 	return (USB_SUCCESS);
3297 }
3298 
3299 
3300 /*
3301  * hubd_start_polling:
3302  *	start or restart the polling
3303  */
3304 static void
3305 hubd_start_polling(hubd_t *hubd, int always)
3306 {
3307 	usb_intr_req_t	*reqp;
3308 	int			rval;
3309 	usb_pipe_state_t	pipe_state;
3310 
3311 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3312 	    "start polling: always=%d dev_state=%d pipe_state=%d\n\t"
3313 	    "thread=%d ep1_ph=0x%p",
3314 	    always, hubd->h_dev_state, hubd->h_intr_pipe_state,
3315 	    hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph);
3316 
3317 	/*
3318 	 * start or restart polling on the intr pipe
3319 	 * only if hotplug thread is not running
3320 	 */
3321 	if ((always == HUBD_ALWAYS_START_POLLING) ||
3322 	    ((hubd->h_dev_state == USB_DEV_ONLINE) &&
3323 	    (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
3324 	    (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) {
3325 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3326 		    "start polling requested");
3327 
3328 		reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP);
3329 
3330 		reqp->intr_client_private = (usb_opaque_t)hubd;
3331 		reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
3332 		    USB_ATTRS_AUTOCLEARING;
3333 		reqp->intr_len = hubd->h_ep1_descr.wMaxPacketSize;
3334 		reqp->intr_cb = hubd_read_cb;
3335 		reqp->intr_exc_cb = hubd_exception_cb;
3336 		mutex_exit(HUBD_MUTEX(hubd));
3337 		if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp,
3338 		    USB_FLAGS_SLEEP)) != USB_SUCCESS) {
3339 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3340 			    "start polling failed, rval=%d", rval);
3341 			usb_free_intr_req(reqp);
3342 		}
3343 
3344 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
3345 		    USB_FLAGS_SLEEP);
3346 		if (pipe_state != USB_PIPE_STATE_ACTIVE) {
3347 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
3348 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
3349 		}
3350 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3351 		    "start polling request 0x%p", (void *)reqp);
3352 
3353 		mutex_enter(HUBD_MUTEX(hubd));
3354 	}
3355 }
3356 
3357 
3358 /*
3359  * hubd_stop_polling
3360  *	stop polling but do not close the pipe
3361  */
3362 static void
3363 hubd_stop_polling(hubd_t *hubd)
3364 {
3365 	int			rval;
3366 	usb_pipe_state_t	pipe_state;
3367 
3368 	if (hubd->h_ep1_ph) {
3369 		USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
3370 		    "hubd_stop_polling:");
3371 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_STOPPED;
3372 		mutex_exit(HUBD_MUTEX(hubd));
3373 
3374 		usb_pipe_stop_intr_polling(hubd->h_ep1_ph, USB_FLAGS_SLEEP);
3375 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
3376 		    USB_FLAGS_SLEEP);
3377 
3378 		if (pipe_state != USB_PIPE_STATE_IDLE) {
3379 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
3380 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
3381 		}
3382 		mutex_enter(HUBD_MUTEX(hubd));
3383 		if (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_STOPPED) {
3384 			hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
3385 		}
3386 	}
3387 }
3388 
3389 
3390 /*
3391  * hubd_close_intr_pipe:
3392  *	close the pipe (which also stops the polling
3393  *	and wait for the hotplug thread to exit
3394  */
3395 static void
3396 hubd_close_intr_pipe(hubd_t *hubd)
3397 {
3398 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3399 	    "hubd_close_intr_pipe:");
3400 
3401 	/*
3402 	 * Now that no async operation is outstanding on pipe,
3403 	 * we can change the state to HUBD_INTR_PIPE_CLOSING
3404 	 */
3405 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_CLOSING;
3406 
3407 	ASSERT(hubd->h_hotplug_thread == 0);
3408 
3409 	if (hubd->h_ep1_ph) {
3410 		mutex_exit(HUBD_MUTEX(hubd));
3411 		usb_pipe_close(hubd->h_dip, hubd->h_ep1_ph, USB_FLAGS_SLEEP,
3412 		    NULL, NULL);
3413 		mutex_enter(HUBD_MUTEX(hubd));
3414 		hubd->h_ep1_ph = NULL;
3415 	}
3416 
3417 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
3418 }
3419 
3420 
3421 /*
3422  * hubd_exception_cb
3423  *	interrupt ep1 exception callback function.
3424  *	this callback executes in taskq thread context and assumes
3425  *	autoclearing
3426  */
3427 /*ARGSUSED*/
3428 static void
3429 hubd_exception_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
3430 {
3431 	hubd_t		*hubd = (hubd_t *)(reqp->intr_client_private);
3432 
3433 	USB_DPRINTF_L2(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
3434 	    "hubd_exception_cb: "
3435 	    "req=0x%p cr=%d data=0x%p cb_flags=0x%x", (void *)reqp,
3436 	    reqp->intr_completion_reason, (void *)reqp->intr_data,
3437 	    reqp->intr_cb_flags);
3438 
3439 	ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
3440 
3441 	mutex_enter(HUBD_MUTEX(hubd));
3442 	(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
3443 
3444 	switch (reqp->intr_completion_reason) {
3445 	case USB_CR_PIPE_RESET:
3446 		/* only restart polling after autoclearing */
3447 		if ((hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
3448 		    (hubd->h_port_reset_wait == 0)) {
3449 			hubd_start_polling(hubd, 0);
3450 		}
3451 
3452 		break;
3453 	case USB_CR_DEV_NOT_RESP:
3454 	case USB_CR_STOPPED_POLLING:
3455 	case USB_CR_PIPE_CLOSING:
3456 	case USB_CR_UNSPECIFIED_ERR:
3457 		/* never restart polling on these conditions */
3458 	default:
3459 		/* for all others, wait for the autoclearing PIPE_RESET cb */
3460 
3461 		break;
3462 	}
3463 
3464 	usb_free_intr_req(reqp);
3465 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
3466 	mutex_exit(HUBD_MUTEX(hubd));
3467 }
3468 
3469 
3470 /*
3471  * helper function to convert LE bytes to a portmask
3472  */
3473 static usb_port_mask_t
3474 hubd_mblk2portmask(mblk_t *data)
3475 {
3476 	int len = min(MBLKL(data), sizeof (usb_port_mask_t));
3477 	usb_port_mask_t rval = 0;
3478 	int i;
3479 
3480 	for (i = 0; i < len; i++) {
3481 		rval |= data->b_rptr[i] << (i * 8);
3482 	}
3483 
3484 	return (rval);
3485 }
3486 
3487 
3488 /*
3489  * hubd_read_cb:
3490  *	interrupt ep1 callback function
3491  *
3492  *	the status indicates just a change on the pipe with no indication
3493  *	of what the change was
3494  *
3495  *	known conditions:
3496  *		- reset port completion
3497  *		- connect
3498  *		- disconnect
3499  *
3500  *	for handling the hotplugging, create a new thread that can do
3501  *	synchronous usba calls
3502  */
3503 static void
3504 hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
3505 {
3506 	hubd_t		*hubd = (hubd_t *)(reqp->intr_client_private);
3507 	size_t		length;
3508 	mblk_t		*data = reqp->intr_data;
3509 	int		mem_flag = 0;
3510 	hubd_hotplug_arg_t *arg;
3511 
3512 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3513 	    "hubd_read_cb: ph=0x%p req=0x%p", (void *)pipe, (void *)reqp);
3514 
3515 	ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
3516 
3517 	/*
3518 	 * At present, we are not handling notification for completion of
3519 	 * asynchronous pipe reset, for which this data ptr could be NULL
3520 	 */
3521 
3522 	if (data == NULL) {
3523 		usb_free_intr_req(reqp);
3524 
3525 		return;
3526 	}
3527 
3528 	arg = (hubd_hotplug_arg_t *)kmem_zalloc(
3529 	    sizeof (hubd_hotplug_arg_t), KM_SLEEP);
3530 	mem_flag = 1;
3531 
3532 	mutex_enter(HUBD_MUTEX(hubd));
3533 
3534 	if ((hubd->h_dev_state == USB_DEV_SUSPENDED) ||
3535 	    (hubd->h_intr_pipe_state != HUBD_INTR_PIPE_ACTIVE)) {
3536 		mutex_exit(HUBD_MUTEX(hubd));
3537 		usb_free_intr_req(reqp);
3538 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3539 
3540 		return;
3541 	}
3542 
3543 	ASSERT(hubd->h_ep1_ph == pipe);
3544 
3545 	length = MBLKL(data);
3546 
3547 	/*
3548 	 * Only look at the data and startup the hotplug thread if
3549 	 * there actually is data.
3550 	 */
3551 	if (length != 0) {
3552 		usb_port_mask_t port_change = hubd_mblk2portmask(data);
3553 
3554 		/*
3555 		 * if a port change was already reported and we are waiting for
3556 		 * reset port completion then wake up the hotplug thread which
3557 		 * should be waiting on reset port completion
3558 		 *
3559 		 * if there is disconnect event instead of reset completion, let
3560 		 * the hotplug thread figure this out
3561 		 */
3562 
3563 		/* remove the reset wait bits from the status */
3564 		hubd->h_port_change |= port_change &
3565 		    ~hubd->h_port_reset_wait;
3566 
3567 		USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
3568 		    "port change=0x%x port_reset_wait=0x%x",
3569 		    hubd->h_port_change, hubd->h_port_reset_wait);
3570 
3571 		/* there should be only one reset bit active at the time */
3572 		if (hubd->h_port_reset_wait & port_change) {
3573 			hubd->h_port_reset_wait = 0;
3574 			cv_signal(&hubd->h_cv_reset_port);
3575 		}
3576 
3577 		/*
3578 		 * kick off the thread only if device is ONLINE and it is not
3579 		 * during attaching or detaching
3580 		 */
3581 		if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
3582 		    (!DEVI_IS_ATTACHING(hubd->h_dip)) &&
3583 		    (!DEVI_IS_DETACHING(hubd->h_dip)) &&
3584 		    (hubd->h_port_change) &&
3585 		    (hubd->h_hotplug_thread == 0)) {
3586 			USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
3587 			    "creating hotplug thread: "
3588 			    "dev_state=%d", hubd->h_dev_state);
3589 
3590 			/*
3591 			 * Mark this device as busy. The will be marked idle
3592 			 * if the async req fails or at the exit of  hotplug
3593 			 * thread
3594 			 */
3595 			(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
3596 
3597 			arg->hubd = hubd;
3598 			arg->hotplug_during_attach = B_FALSE;
3599 
3600 			if (usb_async_req(hubd->h_dip,
3601 			    hubd_hotplug_thread,
3602 			    (void *)arg, 0) == USB_SUCCESS) {
3603 				hubd->h_hotplug_thread++;
3604 				mem_flag = 0;
3605 			} else {
3606 				/* mark this device as idle */
3607 				(void) hubd_pm_idle_component(hubd,
3608 				    hubd->h_dip, 0);
3609 			}
3610 		}
3611 	}
3612 	mutex_exit(HUBD_MUTEX(hubd));
3613 
3614 	if (mem_flag == 1) {
3615 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3616 	}
3617 
3618 	usb_free_intr_req(reqp);
3619 }
3620 
3621 
3622 /*
3623  * hubd_hotplug_thread:
3624  *	handles resetting of port, and creating children
3625  *
3626  *	the ports to check are indicated in h_port_change bit mask
3627  * XXX note that one time poll doesn't work on the root hub
3628  */
3629 static void
3630 hubd_hotplug_thread(void *arg)
3631 {
3632 	hubd_hotplug_arg_t *hd_arg = (hubd_hotplug_arg_t *)arg;
3633 	hubd_t		*hubd = hd_arg->hubd;
3634 	boolean_t	attach_flg = hd_arg->hotplug_during_attach;
3635 	usb_port_t	port;
3636 	uint16_t	nports;
3637 	uint16_t	status, change;
3638 	hub_power_t	*hubpm;
3639 	dev_info_t	*hdip = hubd->h_dip;
3640 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
3641 	dev_info_t	*child_dip;
3642 	boolean_t	online_child = B_FALSE;
3643 	boolean_t	offline_child = B_FALSE;
3644 	boolean_t	pwrup_child = B_FALSE;
3645 	int		prh_circ, rh_circ, chld_circ, circ, old_state;
3646 
3647 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3648 	    "hubd_hotplug_thread:  started");
3649 
3650 	/*
3651 	 * Before console is init'd, we temporarily block the hotplug
3652 	 * threads so that BUS_CONFIG_ONE through hubd_bus_config() can be
3653 	 * processed quickly. This reduces the time needed for vfs_mountroot()
3654 	 * to mount the root FS from a USB disk. And on SPARC platform,
3655 	 * in order to load 'consconfig' successfully after OBP is gone,
3656 	 * we need to check 'modrootloaded' to make sure root filesystem is
3657 	 * available.
3658 	 */
3659 	while (!modrootloaded || !consconfig_console_is_ready()) {
3660 		delay(drv_usectohz(10000));
3661 	}
3662 
3663 	kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3664 
3665 	/*
3666 	 * if our bus power entry point is active, process the change
3667 	 * on the next notification of interrupt pipe
3668 	 */
3669 	mutex_enter(HUBD_MUTEX(hubd));
3670 	if (hubd->h_bus_pwr || (hubd->h_hotplug_thread > 1)) {
3671 		hubd->h_hotplug_thread--;
3672 
3673 		/* mark this device as idle */
3674 		hubd_pm_idle_component(hubd, hubd->h_dip, 0);
3675 		mutex_exit(HUBD_MUTEX(hubd));
3676 
3677 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3678 		    "hubd_hotplug_thread: "
3679 		    "bus_power in progress/hotplugging undesirable - quit");
3680 
3681 		return;
3682 	}
3683 	mutex_exit(HUBD_MUTEX(hubd));
3684 
3685 	ndi_hold_devi(hdip); /* so we don't race with detach */
3686 
3687 	mutex_enter(HUBD_MUTEX(hubd));
3688 
3689 	/* is this the root hub? */
3690 	if (hdip == rh_dip) {
3691 		if (hubd->h_dev_state == USB_DEV_PWRED_DOWN) {
3692 			hubpm = hubd->h_hubpm;
3693 
3694 			/* mark the root hub as full power */
3695 			hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
3696 			hubpm->hubp_time_at_full_power = ddi_get_time();
3697 			mutex_exit(HUBD_MUTEX(hubd));
3698 
3699 			USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3700 			    "hubd_hotplug_thread: call pm_power_has_changed");
3701 
3702 			(void) pm_power_has_changed(hdip, 0,
3703 			    USB_DEV_OS_FULL_PWR);
3704 
3705 			mutex_enter(HUBD_MUTEX(hubd));
3706 			hubd->h_dev_state = USB_DEV_ONLINE;
3707 		}
3708 
3709 	} else {
3710 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3711 		    "hubd_hotplug_thread: not root hub");
3712 	}
3713 
3714 	mutex_exit(HUBD_MUTEX(hubd));
3715 
3716 	/*
3717 	 * this ensures one hotplug activity per system at a time.
3718 	 * we enter the parent PCI node to have this serialization.
3719 	 * this also excludes ioctls and deathrow thread
3720 	 * (a bit crude but easier to debug)
3721 	 */
3722 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
3723 	ndi_devi_enter(rh_dip, &rh_circ);
3724 
3725 	/* exclude other threads */
3726 	ndi_devi_enter(hdip, &circ);
3727 	mutex_enter(HUBD_MUTEX(hubd));
3728 
3729 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE);
3730 
3731 	nports = hubd->h_hub_descr.bNbrPorts;
3732 
3733 	hubd_stop_polling(hubd);
3734 
3735 	while ((hubd->h_dev_state == USB_DEV_ONLINE) &&
3736 	    (hubd->h_port_change)) {
3737 		/*
3738 		 * The 0th bit is the hub status change bit.
3739 		 * handle loss of local power here
3740 		 */
3741 		if (hubd->h_port_change & HUB_CHANGE_STATUS) {
3742 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3743 			    "hubd_hotplug_thread: hub status change!");
3744 
3745 			/*
3746 			 * This should be handled properly.  For now,
3747 			 * mask off the bit.
3748 			 */
3749 			hubd->h_port_change &= ~HUB_CHANGE_STATUS;
3750 
3751 			/*
3752 			 * check and ack hub status
3753 			 * this causes stall conditions
3754 			 * when local power is removed
3755 			 */
3756 			(void) hubd_get_hub_status(hubd);
3757 		}
3758 
3759 		for (port = 1; port <= nports; port++) {
3760 			usb_port_mask_t port_mask;
3761 			boolean_t was_connected;
3762 
3763 			port_mask = 1 << port;
3764 			was_connected =
3765 			    (hubd->h_port_state[port] & PORT_STATUS_CCS) &&
3766 			    (hubd->h_children_dips[port]);
3767 
3768 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3769 			    "hubd_hotplug_thread: "
3770 			    "port %d mask=0x%x change=0x%x connected=0x%x",
3771 			    port, port_mask, hubd->h_port_change,
3772 			    was_connected);
3773 
3774 			/*
3775 			 * is this a port connection that changed?
3776 			 */
3777 			if ((hubd->h_port_change & port_mask) == 0) {
3778 
3779 				continue;
3780 			}
3781 			hubd->h_port_change &= ~port_mask;
3782 
3783 			/* ack all changes */
3784 			(void) hubd_determine_port_status(hubd, port,
3785 			    &status, &change, HUBD_ACK_ALL_CHANGES);
3786 
3787 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3788 			    "handle port %d:\n\t"
3789 			    "new status=0x%x change=0x%x was_conn=0x%x ",
3790 			    port, status, change, was_connected);
3791 
3792 			/* Recover a disabled port */
3793 			if (change & PORT_CHANGE_PESC) {
3794 				USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
3795 				    hubd->h_log_handle,
3796 				    "port%d Disabled - "
3797 				    "status=0x%x, change=0x%x",
3798 				    port, status, change);
3799 
3800 				/*
3801 				 * if the port was connected and is still
3802 				 * connected, recover the port
3803 				 */
3804 				if (was_connected && (status &
3805 				    PORT_STATUS_CCS)) {
3806 					online_child |=
3807 					    (hubd_recover_disabled_port(hubd,
3808 					    port) == USB_SUCCESS);
3809 				}
3810 			}
3811 
3812 			/*
3813 			 * Now check what changed on the port
3814 			 */
3815 			if ((change & PORT_CHANGE_CSC) || attach_flg) {
3816 				if ((status & PORT_STATUS_CCS) &&
3817 				    (!was_connected)) {
3818 					/* new device plugged in */
3819 					online_child |=
3820 					    (hubd_handle_port_connect(hubd,
3821 					    port) == USB_SUCCESS);
3822 
3823 				} else if ((status & PORT_STATUS_CCS) &&
3824 				    was_connected) {
3825 					/*
3826 					 * In this case we can never be sure
3827 					 * if the device indeed got hotplugged
3828 					 * or the hub is falsely reporting the
3829 					 * change.
3830 					 */
3831 					child_dip = hubd->h_children_dips[port];
3832 
3833 					mutex_exit(HUBD_MUTEX(hubd));
3834 					/*
3835 					 * this ensures we do not race with
3836 					 * other threads which are detaching
3837 					 * the child driver at the same time.
3838 					 */
3839 					ndi_devi_enter(child_dip, &chld_circ);
3840 					/*
3841 					 * Now check if the driver remains
3842 					 * attached.
3843 					 */
3844 					if (i_ddi_devi_attached(child_dip)) {
3845 						/*
3846 						 * first post a disconnect event
3847 						 * to the child.
3848 						 */
3849 						hubd_post_event(hubd, port,
3850 						    USBA_EVENT_TAG_HOT_REMOVAL);
3851 						mutex_enter(HUBD_MUTEX(hubd));
3852 
3853 						/*
3854 						 * then reset the port and
3855 						 * recover the device
3856 						 */
3857 						online_child |=
3858 						    (hubd_handle_port_connect(
3859 						    hubd, port) == USB_SUCCESS);
3860 
3861 						mutex_exit(HUBD_MUTEX(hubd));
3862 					}
3863 
3864 					ndi_devi_exit(child_dip, chld_circ);
3865 					mutex_enter(HUBD_MUTEX(hubd));
3866 				} else if (was_connected) {
3867 					/* this is a disconnect */
3868 					mutex_exit(HUBD_MUTEX(hubd));
3869 					hubd_post_event(hubd, port,
3870 					    USBA_EVENT_TAG_HOT_REMOVAL);
3871 					mutex_enter(HUBD_MUTEX(hubd));
3872 
3873 					offline_child = B_TRUE;
3874 				}
3875 			}
3876 
3877 			/*
3878 			 * Check if any port is coming out of suspend
3879 			 */
3880 			if (change & PORT_CHANGE_PSSC) {
3881 				/* a resuming device could have disconnected */
3882 				if (was_connected &&
3883 				    hubd->h_children_dips[port]) {
3884 
3885 					/* device on this port resuming */
3886 					dev_info_t *dip;
3887 
3888 					dip = hubd->h_children_dips[port];
3889 
3890 					/*
3891 					 * Don't raise power on detaching child
3892 					 */
3893 					if (!DEVI_IS_DETACHING(dip)) {
3894 						/*
3895 						 * As this child is not
3896 						 * detaching, we set this
3897 						 * flag, causing bus_ctls
3898 						 * to stall detach till
3899 						 * pm_raise_power returns
3900 						 * and flag it for a deferred
3901 						 * raise_power.
3902 						 *
3903 						 * pm_raise_power is deferred
3904 						 * because we need to release
3905 						 * the locks first.
3906 						 */
3907 						hubd->h_port_state[port] |=
3908 						    HUBD_CHILD_RAISE_POWER;
3909 						pwrup_child = B_TRUE;
3910 						mutex_exit(HUBD_MUTEX(hubd));
3911 
3912 						/*
3913 						 * make sure that child
3914 						 * doesn't disappear
3915 						 */
3916 						ndi_hold_devi(dip);
3917 
3918 						mutex_enter(HUBD_MUTEX(hubd));
3919 					}
3920 				}
3921 			}
3922 
3923 			/*
3924 			 * Check if the port is over-current
3925 			 */
3926 			if (change & PORT_CHANGE_OCIC) {
3927 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
3928 				    hubd->h_log_handle,
3929 				    "Port%d in over current condition, "
3930 				    "please check the attached device to "
3931 				    "clear the condition. The system will "
3932 				    "try to recover the port, but if not "
3933 				    "successful, you need to re-connect "
3934 				    "the hub or reboot the system to bring "
3935 				    "the port back to work", port);
3936 
3937 				if (!(status & PORT_STATUS_PPS)) {
3938 					/*
3939 					 * Try to enable port power, but
3940 					 * possibly fail. Ignore failure
3941 					 */
3942 					(void) hubd_enable_port_power(hubd,
3943 					    port);
3944 
3945 					/*
3946 					 * Delay some time to avoid
3947 					 * over-current event to happen
3948 					 * too frequently in some cases
3949 					 */
3950 					mutex_exit(HUBD_MUTEX(hubd));
3951 					delay(drv_usectohz(500000));
3952 					mutex_enter(HUBD_MUTEX(hubd));
3953 				}
3954 			}
3955 		}
3956 	}
3957 
3958 	/* release locks so we can do a devfs_clean */
3959 	mutex_exit(HUBD_MUTEX(hubd));
3960 
3961 	/* delete cached dv_node's but drop locks first */
3962 	ndi_devi_exit(hdip, circ);
3963 	ndi_devi_exit(rh_dip, rh_circ);
3964 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
3965 
3966 	(void) devfs_clean(rh_dip, NULL, 0);
3967 
3968 	/* now check if any children need onlining */
3969 	if (online_child) {
3970 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3971 		    "hubd_hotplug_thread: onlining children");
3972 
3973 		(void) ndi_devi_online(hubd->h_dip, 0);
3974 	}
3975 
3976 	/* now check if any disconnected devices need to be cleaned up */
3977 	if (offline_child) {
3978 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
3979 		    "hubd_hotplug_thread: scheduling cleanup");
3980 
3981 		hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
3982 	}
3983 
3984 	mutex_enter(HUBD_MUTEX(hubd));
3985 
3986 	/* now raise power on the children that have woken up */
3987 	if (pwrup_child) {
3988 		old_state = hubd->h_dev_state;
3989 		hubd->h_dev_state = USB_DEV_HUB_CHILD_PWRLVL;
3990 		for (port = 1; port <= nports; port++) {
3991 			if (hubd->h_port_state[port] & HUBD_CHILD_RAISE_POWER) {
3992 				dev_info_t *dip = hubd->h_children_dips[port];
3993 
3994 				mutex_exit(HUBD_MUTEX(hubd));
3995 
3996 				/* Get the device to full power */
3997 				(void) pm_busy_component(dip, 0);
3998 				(void) pm_raise_power(dip, 0,
3999 				    USB_DEV_OS_FULL_PWR);
4000 				(void) pm_idle_component(dip, 0);
4001 
4002 				/* release the hold on the child */
4003 				ndi_rele_devi(dip);
4004 				mutex_enter(HUBD_MUTEX(hubd));
4005 				hubd->h_port_state[port] &=
4006 				    ~HUBD_CHILD_RAISE_POWER;
4007 			}
4008 		}
4009 		/*
4010 		 * make sure that we don't accidentally
4011 		 * over write the disconnect state
4012 		 */
4013 		if (hubd->h_dev_state == USB_DEV_HUB_CHILD_PWRLVL) {
4014 			hubd->h_dev_state = old_state;
4015 		}
4016 	}
4017 
4018 	/*
4019 	 * start polling can immediately kick off read callback
4020 	 * we need to set the h_hotplug_thread to 0 so that
4021 	 * the callback is not dropped
4022 	 *
4023 	 * if there is device during reset, still stop polling to avoid the
4024 	 * read callback interrupting the reset, the polling will be started
4025 	 * in hubd_reset_thread.
4026 	 */
4027 	for (port = 1; port <= MAX_PORTS; port++) {
4028 		if (hubd->h_reset_port[port]) {
4029 
4030 			break;
4031 		}
4032 	}
4033 	if (port > MAX_PORTS) {
4034 		hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
4035 	}
4036 
4037 	/*
4038 	 * Earlier we would set the h_hotplug_thread = 0 before
4039 	 * polling was restarted  so that
4040 	 * if there is any root hub status change interrupt, we can still kick
4041 	 * off the hotplug thread. This was valid when this interrupt was
4042 	 * delivered in hardware, and only ONE interrupt would be delivered.
4043 	 * Now that we poll on the root hub looking for status change in
4044 	 * software, this assignment is no longer required.
4045 	 */
4046 	hubd->h_hotplug_thread--;
4047 
4048 	/* mark this device as idle */
4049 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
4050 
4051 	cv_broadcast(&hubd->h_cv_hotplug_dev);
4052 
4053 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4054 	    "hubd_hotplug_thread: exit");
4055 
4056 	mutex_exit(HUBD_MUTEX(hubd));
4057 
4058 	ndi_rele_devi(hdip);
4059 }
4060 
4061 
4062 /*
4063  * hubd_handle_port_connect:
4064  *	Transition a port from Disabled to Enabled.  Ensure that the
4065  *	port is in the correct state before attempting to
4066  *	access the device.
4067  */
4068 static int
4069 hubd_handle_port_connect(hubd_t *hubd, usb_port_t port)
4070 {
4071 	int			rval;
4072 	int			retry;
4073 	long			time_delay;
4074 	long			settling_time;
4075 	uint16_t		status;
4076 	uint16_t		change;
4077 	usb_addr_t		hubd_usb_addr;
4078 	usba_device_t		*usba_device;
4079 	usb_port_status_t	port_status = 0;
4080 	usb_port_status_t	hub_port_status = 0;
4081 
4082 	/* Get the hub address and port status */
4083 	usba_device = hubd->h_usba_device;
4084 	mutex_enter(&usba_device->usb_mutex);
4085 	hubd_usb_addr = usba_device->usb_addr;
4086 	hub_port_status = usba_device->usb_port_status;
4087 	mutex_exit(&usba_device->usb_mutex);
4088 
4089 	/*
4090 	 * If a device is connected, transition the
4091 	 * port from Disabled to the Enabled state.
4092 	 * The device will receive downstream packets
4093 	 * in the Enabled state.
4094 	 *
4095 	 * reset port and wait for the hub to report
4096 	 * completion
4097 	 */
4098 	change = status = 0;
4099 
4100 	/*
4101 	 * According to section 9.1.2 of USB 2.0 spec, the host should
4102 	 * wait for atleast 100ms to allow completion of an insertion
4103 	 * process and for power at the device to become stable.
4104 	 * We wait for 200 ms
4105 	 */
4106 	settling_time = drv_usectohz(hubd_device_delay / 5);
4107 	mutex_exit(HUBD_MUTEX(hubd));
4108 	delay(settling_time);
4109 	mutex_enter(HUBD_MUTEX(hubd));
4110 
4111 	/* calculate 600 ms delay time */
4112 	time_delay = (6 * drv_usectohz(hubd_device_delay)) / 10;
4113 
4114 	for (retry = 0; (hubd->h_dev_state == USB_DEV_ONLINE) &&
4115 	    (retry < hubd_retry_enumerate); retry++) {
4116 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4117 		    "resetting port%d, retry=%d", port, retry);
4118 
4119 		if ((rval = hubd_reset_port(hubd, port)) != USB_SUCCESS) {
4120 			(void) hubd_determine_port_status(hubd,
4121 			    port, &status, &change, 0);
4122 
4123 			/* continue only if port is still connected */
4124 			if (status & PORT_STATUS_CCS) {
4125 				continue;
4126 			}
4127 
4128 			/* carry on regardless */
4129 		}
4130 
4131 		/*
4132 		 * according to USB 2.0 spec section 11.24.2.7.1.2
4133 		 * at the end of port reset, the hub enables the port.
4134 		 * But for some strange reasons, uhci port remains disabled.
4135 		 * And because the port remains disabled for the settling
4136 		 * time below, the device connected to the port gets wedged
4137 		 * - fails to enumerate (device not responding)
4138 		 * Hence, we enable it here immediately and later again after
4139 		 * the delay
4140 		 */
4141 		(void) hubd_enable_port(hubd, port);
4142 
4143 		/* we skip this delay in the first iteration */
4144 		if (retry) {
4145 			/*
4146 			 * delay for device to signal disconnect/connect so
4147 			 * that hub properly recognizes the speed of the device
4148 			 */
4149 			mutex_exit(HUBD_MUTEX(hubd));
4150 			delay(settling_time);
4151 			mutex_enter(HUBD_MUTEX(hubd));
4152 
4153 			/*
4154 			 * When a low speed device is connected to any port of
4155 			 * PPX it has to be explicitly enabled
4156 			 * Also, if device intentionally signals
4157 			 * disconnect/connect, it will disable the port.
4158 			 * So enable it again.
4159 			 */
4160 			(void) hubd_enable_port(hubd, port);
4161 		}
4162 
4163 		if ((rval = hubd_determine_port_status(hubd, port, &status,
4164 		    &change, 0)) != USB_SUCCESS) {
4165 
4166 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4167 			    "getting status failed (%d)", rval);
4168 
4169 			(void) hubd_disable_port(hubd, port);
4170 
4171 			continue;
4172 		}
4173 
4174 		if (status & PORT_STATUS_POCI) {
4175 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4176 			    "port %d overcurrent", port);
4177 
4178 			(void) hubd_disable_port(hubd, port);
4179 
4180 			/* ack changes */
4181 			(void) hubd_determine_port_status(hubd,
4182 			    port, &status, &change, PORT_CHANGE_OCIC);
4183 
4184 			continue;
4185 		}
4186 
4187 		/* is status really OK? */
4188 		if ((status & PORT_STATUS_OK) != PORT_STATUS_OK) {
4189 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4190 			    "port %d status (0x%x) not OK on retry %d",
4191 			    port, status, retry);
4192 
4193 			/* check if we still have the connection */
4194 			if (!(status & PORT_STATUS_CCS)) {
4195 				/* lost connection, set exit condition */
4196 				retry = hubd_retry_enumerate;
4197 
4198 				break;
4199 			}
4200 		} else {
4201 			/*
4202 			 * Determine if the device is high or full
4203 			 * or low speed.
4204 			 */
4205 			if (status & PORT_STATUS_LSDA) {
4206 				port_status = USBA_LOW_SPEED_DEV;
4207 			} else if (status & PORT_STATUS_HSDA) {
4208 				port_status = USBA_HIGH_SPEED_DEV;
4209 			} else {
4210 				port_status = USBA_FULL_SPEED_DEV;
4211 			}
4212 
4213 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4214 			    "creating child port%d, status=0x%x "
4215 			    "port status=0x%x",
4216 			    port, status, port_status);
4217 
4218 			/*
4219 			 * if the child already exists, set addrs and config
4220 			 * to the device post connect event to the child
4221 			 */
4222 			if (hubd->h_children_dips[port]) {
4223 				/* set addrs to this device */
4224 				rval = hubd_setdevaddr(hubd, port);
4225 
4226 				/*
4227 				 * This delay is important for the CATC hub
4228 				 * to enumerate. But, avoid delay in the first
4229 				 * iteration
4230 				 */
4231 				if (retry) {
4232 					mutex_exit(HUBD_MUTEX(hubd));
4233 					delay(drv_usectohz(
4234 					    hubd_device_delay/100));
4235 					mutex_enter(HUBD_MUTEX(hubd));
4236 				}
4237 
4238 				if (rval == USB_SUCCESS) {
4239 					/*
4240 					 * if the port is resetting, check if
4241 					 * device's descriptors have changed.
4242 					 */
4243 					if ((hubd->h_reset_port[port]) &&
4244 					    (hubd_check_same_device(hubd,
4245 					    port) != USB_SUCCESS)) {
4246 						retry = hubd_retry_enumerate;
4247 
4248 						break;
4249 					}
4250 
4251 					/*
4252 					 * set the default config for
4253 					 * this device
4254 					 */
4255 					hubd_setdevconfig(hubd, port);
4256 
4257 					/*
4258 					 * if we are doing Default reset, do
4259 					 * not post reconnect event since we
4260 					 * don't know where reset function is
4261 					 * called.
4262 					 */
4263 					if (hubd->h_reset_port[port]) {
4264 
4265 						return (USB_SUCCESS);
4266 					}
4267 
4268 					/*
4269 					 * indicate to the child that
4270 					 * it is online again
4271 					 */
4272 					mutex_exit(HUBD_MUTEX(hubd));
4273 					hubd_post_event(hubd, port,
4274 					    USBA_EVENT_TAG_HOT_INSERTION);
4275 					mutex_enter(HUBD_MUTEX(hubd));
4276 
4277 					return (USB_SUCCESS);
4278 				}
4279 			} else {
4280 				/*
4281 				 * We need to release access here
4282 				 * so that busctls on other ports can
4283 				 * continue and don't cause a deadlock
4284 				 * when busctl and removal of prom node
4285 				 * takes concurrently. This also ensures
4286 				 * busctls for attach of successfully
4287 				 * enumerated devices on other ports can
4288 				 * continue concurrently with the process
4289 				 * of enumerating the new devices. This
4290 				 * reduces the overall boot time of the system.
4291 				 */
4292 				rval = hubd_create_child(hubd->h_dip,
4293 				    hubd,
4294 				    hubd->h_usba_device,
4295 				    port_status, port,
4296 				    retry);
4297 				if (rval == USB_SUCCESS) {
4298 					usba_update_hotplug_stats(hubd->h_dip,
4299 					    USBA_TOTAL_HOTPLUG_SUCCESS|
4300 					    USBA_HOTPLUG_SUCCESS);
4301 					hubd->h_total_hotplug_success++;
4302 
4303 					if (retry > 0) {
4304 						USB_DPRINTF_L2(
4305 						    DPRINT_MASK_HOTPLUG,
4306 						    hubd->h_log_handle,
4307 						    "device on port %d "
4308 						    "enumerated after %d %s",
4309 						    port, retry,
4310 						    (retry > 1) ? "retries" :
4311 						    "retry");
4312 
4313 					}
4314 
4315 					return (USB_SUCCESS);
4316 				}
4317 			}
4318 		}
4319 
4320 		/* wait a while until it settles? */
4321 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4322 		    "disabling port %d again", port);
4323 
4324 		(void) hubd_disable_port(hubd, port);
4325 		if (retry) {
4326 			mutex_exit(HUBD_MUTEX(hubd));
4327 			delay(time_delay);
4328 			mutex_enter(HUBD_MUTEX(hubd));
4329 		}
4330 
4331 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4332 		    "retrying on port %d", port);
4333 	}
4334 
4335 	if (retry >= hubd_retry_enumerate) {
4336 		/*
4337 		 * If it is a High Speed Root Hub and connected device
4338 		 * Is a Low/Full Speed, it will be handled by USB 1.1
4339 		 * Host Controller. In this case, USB 2.0 Host Controller
4340 		 * will transfer the ownership of this port to USB 1.1
4341 		 * Host Controller. So don't display any error message on
4342 		 * the console.
4343 		 */
4344 		if ((hubd_usb_addr == ROOT_HUB_ADDR) &&
4345 		    (hub_port_status == USBA_HIGH_SPEED_DEV) &&
4346 		    (port_status != USBA_HIGH_SPEED_DEV)) {
4347 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
4348 			    hubd->h_log_handle,
4349 			    "hubd_handle_port_connect: Low/Full speed "
4350 			    "device is connected to High Speed root hub");
4351 		} else {
4352 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4353 			    hubd->h_log_handle,
4354 			    "Connecting device on port %d failed", port);
4355 		}
4356 
4357 		(void) hubd_disable_port(hubd, port);
4358 		usba_update_hotplug_stats(hubd->h_dip,
4359 		    USBA_TOTAL_HOTPLUG_FAILURE|USBA_HOTPLUG_FAILURE);
4360 		hubd->h_total_hotplug_failure++;
4361 
4362 		/*
4363 		 * the port should be automagically
4364 		 * disabled but just in case, we do
4365 		 * it here
4366 		 */
4367 		(void) hubd_disable_port(hubd, port);
4368 
4369 		/* ack all changes because we disabled this port */
4370 		(void) hubd_determine_port_status(hubd,
4371 		    port, &status, &change, HUBD_ACK_ALL_CHANGES);
4372 
4373 	}
4374 
4375 	return (USB_FAILURE);
4376 }
4377 
4378 
4379 /*
4380  * hubd_get_hub_status:
4381  */
4382 static int
4383 hubd_get_hub_status(hubd_t *hubd)
4384 {
4385 	int		rval;
4386 	usb_cr_t	completion_reason;
4387 	usb_cb_flags_t	cb_flags;
4388 	uint16_t	stword[2];
4389 	uint16_t	status;
4390 	uint16_t	change;
4391 	usb_cfg_descr_t	cfg_descr;
4392 	size_t		cfg_length;
4393 	uchar_t		*usb_cfg;
4394 	uint8_t		MaxPower;
4395 	usb_hub_descr_t	*hub_descr;
4396 	usb_port_t	port;
4397 
4398 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4399 	    "hubd_get_hub_status:");
4400 
4401 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4402 
4403 	if ((hubd_get_hub_status_words(hubd, stword)) != USB_SUCCESS) {
4404 
4405 		return (USB_FAILURE);
4406 	}
4407 	status = stword[0];
4408 	change = stword[1];
4409 
4410 	mutex_exit(HUBD_MUTEX(hubd));
4411 
4412 	/* Obtain the raw configuration descriptor */
4413 	usb_cfg = usb_get_raw_cfg_data(hubd->h_dip, &cfg_length);
4414 
4415 	/* get configuration descriptor */
4416 	rval = usb_parse_cfg_descr(usb_cfg, cfg_length,
4417 	    &cfg_descr, USB_CFG_DESCR_SIZE);
4418 
4419 	if (rval != USB_CFG_DESCR_SIZE) {
4420 
4421 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4422 		    "get hub configuration descriptor failed.");
4423 
4424 		mutex_enter(HUBD_MUTEX(hubd));
4425 
4426 		return (USB_FAILURE);
4427 	} else {
4428 		MaxPower = cfg_descr.bMaxPower;
4429 	}
4430 
4431 	/* check if local power status changed. */
4432 	if (change & C_HUB_LOCAL_POWER_STATUS) {
4433 
4434 		/*
4435 		 * local power has been lost, check the maximum
4436 		 * power consumption of current configuration.
4437 		 * see USB2.0 spec Table 11-12.
4438 		 */
4439 		if (status & HUB_LOCAL_POWER_STATUS) {
4440 
4441 			if (MaxPower == 0) {
4442 
4443 				/*
4444 				 * Self-powered only hub. Because it could
4445 				 * not draw any power from USB bus.
4446 				 * It can't work well on this condition.
4447 				 */
4448 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
4449 				    hubd->h_log_handle,
4450 				    "local power has been lost, "
4451 				    "please disconnect hub");
4452 			} else {
4453 
4454 				/*
4455 				 * Bus-powered only or self/bus-powered hub.
4456 				 */
4457 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
4458 				    hubd->h_log_handle,
4459 				    "local power has been lost,"
4460 				    "the hub could draw %d"
4461 				    " mA power from the USB bus.",
4462 				    2*MaxPower);
4463 			}
4464 
4465 		}
4466 
4467 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4468 		    "clearing feature C_HUB_LOCAL_POWER ");
4469 
4470 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4471 		    hubd->h_default_pipe,
4472 		    HUB_HANDLE_HUB_FEATURE_TYPE,
4473 		    USB_REQ_CLEAR_FEATURE,
4474 		    CFS_C_HUB_LOCAL_POWER,
4475 		    0,
4476 		    0,
4477 		    NULL, 0,
4478 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4479 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
4480 			    hubd->h_log_handle,
4481 			    "clear feature C_HUB_LOCAL_POWER "
4482 			    "failed (%d 0x%x %d)",
4483 			    rval, completion_reason, cb_flags);
4484 		}
4485 
4486 	}
4487 
4488 	if (change & C_HUB_OVER_CURRENT) {
4489 
4490 		if (status & HUB_OVER_CURRENT) {
4491 
4492 			if (usba_is_root_hub(hubd->h_dip)) {
4493 				/*
4494 				 * The root hub should be automatically
4495 				 * recovered when over-current condition is
4496 				 * cleared. But there might be exception and
4497 				 * need user interaction to recover.
4498 				 */
4499 				USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4500 				    hubd->h_log_handle,
4501 				    "Root hub over current condition, "
4502 				    "please check your system to clear the "
4503 				    "condition as soon as possible. And you "
4504 				    "may need to reboot the system to bring "
4505 				    "the root hub back to work if it cannot "
4506 				    "recover automatically");
4507 			} else {
4508 				/*
4509 				 * The driver would try to recover port power
4510 				 * on over current condition. When the recovery
4511 				 * fails, the user may still need to offline
4512 				 * this hub in order to recover.
4513 				 * The port power is automatically disabled,
4514 				 * so we won't see disconnects.
4515 				 */
4516 				USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4517 				    hubd->h_log_handle,
4518 				    "Hub global over current condition, "
4519 				    "please disconnect the devices connected "
4520 				    "to the hub to clear the condition. And "
4521 				    "you may need to re-connect the hub if "
4522 				    "the ports do not work");
4523 			}
4524 		}
4525 
4526 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
4527 		    "clearing feature C_HUB_OVER_CURRENT");
4528 
4529 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4530 		    hubd->h_default_pipe,
4531 		    HUB_HANDLE_HUB_FEATURE_TYPE,
4532 		    USB_REQ_CLEAR_FEATURE,
4533 		    CFS_C_HUB_OVER_CURRENT,
4534 		    0,
4535 		    0,
4536 		    NULL, 0,
4537 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4538 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
4539 			    hubd->h_log_handle,
4540 			    "clear feature C_HUB_OVER_CURRENT "
4541 			    "failed (%d 0x%x %d)",
4542 			    rval, completion_reason, cb_flags);
4543 		}
4544 
4545 		/*
4546 		 * Try to recover all port power if they are turned off.
4547 		 * Don't do this for root hub, but rely on the root hub
4548 		 * to recover itself.
4549 		 */
4550 		if (!usba_is_root_hub(hubd->h_dip)) {
4551 
4552 			mutex_enter(HUBD_MUTEX(hubd));
4553 
4554 			/*
4555 			 * Only check the power status of the 1st port
4556 			 * since all port power status should be the same.
4557 			 */
4558 			(void) hubd_determine_port_status(hubd, 1, &status,
4559 			    &change, 0);
4560 
4561 			if (status & PORT_STATUS_PPS) {
4562 
4563 				return (USB_SUCCESS);
4564 			}
4565 
4566 			hub_descr = &hubd->h_hub_descr;
4567 
4568 			for (port = 1; port <= hub_descr->bNbrPorts;
4569 			    port++) {
4570 
4571 				(void) hubd_enable_port_power(hubd, port);
4572 			}
4573 
4574 			mutex_exit(HUBD_MUTEX(hubd));
4575 
4576 			/*
4577 			 * Delay some time to avoid over-current event
4578 			 * to happen too frequently in some cases
4579 			 */
4580 			delay(drv_usectohz(500000));
4581 		}
4582 	}
4583 
4584 	mutex_enter(HUBD_MUTEX(hubd));
4585 
4586 	return (USB_SUCCESS);
4587 }
4588 
4589 
4590 /*
4591  * hubd_reset_port:
4592  */
4593 static int
4594 hubd_reset_port(hubd_t *hubd, usb_port_t port)
4595 {
4596 	int	rval;
4597 	usb_cr_t completion_reason;
4598 	usb_cb_flags_t cb_flags;
4599 	usb_port_mask_t port_mask = 1 << port;
4600 	mblk_t	*data;
4601 	uint16_t status;
4602 	uint16_t change;
4603 	int	i;
4604 	clock_t	delta;
4605 
4606 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4607 	    "hubd_reset_port: port=%d", port);
4608 
4609 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4610 
4611 	hubd->h_port_reset_wait |= port_mask;
4612 
4613 	mutex_exit(HUBD_MUTEX(hubd));
4614 
4615 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4616 	    hubd->h_default_pipe,
4617 	    HUB_HANDLE_PORT_FEATURE_TYPE,
4618 	    USB_REQ_SET_FEATURE,
4619 	    CFS_PORT_RESET,
4620 	    port,
4621 	    0,
4622 	    NULL, 0,
4623 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4624 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4625 		    "reset port%d failed (%d 0x%x %d)",
4626 		    port, completion_reason, cb_flags, rval);
4627 
4628 		mutex_enter(HUBD_MUTEX(hubd));
4629 
4630 		return (USB_FAILURE);
4631 	}
4632 
4633 	mutex_enter(HUBD_MUTEX(hubd));
4634 
4635 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4636 	    "waiting on cv for reset completion");
4637 
4638 	/*
4639 	 * wait for port status change event
4640 	 */
4641 	delta = drv_usectohz(hubd_device_delay / 10);
4642 	for (i = 0; i < hubd_retry_enumerate; i++) {
4643 		/*
4644 		 * start polling ep1 for receiving notification on
4645 		 * reset completion
4646 		 */
4647 		hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
4648 
4649 		/*
4650 		 * sleep a max of 100ms for reset completion
4651 		 * notification to be received
4652 		 */
4653 		if (hubd->h_port_reset_wait & port_mask) {
4654 			rval = cv_reltimedwait(&hubd->h_cv_reset_port,
4655 			    &hubd->h_mutex, delta, TR_CLOCK_TICK);
4656 			if ((rval <= 0) &&
4657 			    (hubd->h_port_reset_wait & port_mask)) {
4658 				/* we got woken up because of a timeout */
4659 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
4660 				    hubd->h_log_handle,
4661 				    "timeout: reset port=%d failed", port);
4662 
4663 				hubd->h_port_reset_wait &=  ~port_mask;
4664 
4665 				hubd_stop_polling(hubd);
4666 
4667 				return (USB_FAILURE);
4668 			}
4669 		}
4670 
4671 		USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4672 		    "reset completion received");
4673 
4674 		hubd_stop_polling(hubd);
4675 
4676 		data = NULL;
4677 
4678 		/* check status to determine whether reset completed */
4679 		mutex_exit(HUBD_MUTEX(hubd));
4680 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4681 		    hubd->h_default_pipe,
4682 		    HUB_GET_PORT_STATUS_TYPE,
4683 		    USB_REQ_GET_STATUS,
4684 		    0,
4685 		    port,
4686 		    GET_STATUS_LENGTH,
4687 		    &data, 0,
4688 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4689 			USB_DPRINTF_L2(DPRINT_MASK_PORT,
4690 			    hubd->h_log_handle,
4691 			    "get status port%d failed (%d 0x%x %d)",
4692 			    port, completion_reason, cb_flags, rval);
4693 
4694 			if (data) {
4695 				freemsg(data);
4696 				data = NULL;
4697 			}
4698 			mutex_enter(HUBD_MUTEX(hubd));
4699 
4700 			continue;
4701 		}
4702 
4703 		status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
4704 		change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
4705 
4706 		freemsg(data);
4707 
4708 		/* continue only if port is still connected */
4709 		if (!(status & PORT_STATUS_CCS)) {
4710 
4711 			/* lost connection, set exit condition */
4712 			i = hubd_retry_enumerate;
4713 
4714 			mutex_enter(HUBD_MUTEX(hubd));
4715 
4716 			break;
4717 		}
4718 
4719 		if (status & PORT_STATUS_PRS) {
4720 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4721 			    "port%d reset active", port);
4722 			mutex_enter(HUBD_MUTEX(hubd));
4723 
4724 			continue;
4725 		} else {
4726 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4727 			    "port%d reset inactive", port);
4728 		}
4729 
4730 		if (change & PORT_CHANGE_PRSC) {
4731 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4732 			    "clearing feature CFS_C_PORT_RESET");
4733 
4734 			if (usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4735 			    hubd->h_default_pipe,
4736 			    HUB_HANDLE_PORT_FEATURE_TYPE,
4737 			    USB_REQ_CLEAR_FEATURE,
4738 			    CFS_C_PORT_RESET,
4739 			    port,
4740 			    0,
4741 			    NULL, 0,
4742 			    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
4743 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
4744 				    hubd->h_log_handle,
4745 				    "clear feature CFS_C_PORT_RESET"
4746 				    " port%d failed (%d 0x%x %d)",
4747 				    port, completion_reason, cb_flags, rval);
4748 			}
4749 		}
4750 		mutex_enter(HUBD_MUTEX(hubd));
4751 
4752 		break;
4753 	}
4754 
4755 	if (i >= hubd_retry_enumerate) {
4756 		/* port reset has failed */
4757 		rval = USB_FAILURE;
4758 	}
4759 
4760 	return (rval);
4761 }
4762 
4763 
4764 /*
4765  * hubd_enable_port:
4766  *	this may fail if the hub as been disconnected
4767  */
4768 static int
4769 hubd_enable_port(hubd_t *hubd, usb_port_t port)
4770 {
4771 	int	rval;
4772 	usb_cr_t completion_reason;
4773 	usb_cb_flags_t cb_flags;
4774 
4775 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4776 	    "hubd_enable_port: port=%d", port);
4777 
4778 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4779 
4780 	mutex_exit(HUBD_MUTEX(hubd));
4781 
4782 	/* Do not issue a SetFeature(PORT_ENABLE) on external hubs */
4783 	if (!usba_is_root_hub(hubd->h_dip)) {
4784 		mutex_enter(HUBD_MUTEX(hubd));
4785 
4786 		return (USB_SUCCESS);
4787 	}
4788 
4789 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4790 	    hubd->h_default_pipe,
4791 	    HUB_HANDLE_PORT_FEATURE_TYPE,
4792 	    USB_REQ_SET_FEATURE,
4793 	    CFS_PORT_ENABLE,
4794 	    port,
4795 	    0,
4796 	    NULL, 0,
4797 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4798 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4799 		    "enable port%d failed (%d 0x%x %d)",
4800 		    port, completion_reason, cb_flags, rval);
4801 	}
4802 
4803 	mutex_enter(HUBD_MUTEX(hubd));
4804 
4805 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4806 	    "enabling port done");
4807 
4808 	return (rval);
4809 }
4810 
4811 
4812 /*
4813  * hubd_disable_port
4814  */
4815 static int
4816 hubd_disable_port(hubd_t *hubd, usb_port_t port)
4817 {
4818 	int	rval;
4819 	usb_cr_t completion_reason;
4820 	usb_cb_flags_t cb_flags;
4821 
4822 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4823 	    "hubd_disable_port: port=%d", port);
4824 
4825 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4826 
4827 	mutex_exit(HUBD_MUTEX(hubd));
4828 
4829 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4830 	    hubd->h_default_pipe,
4831 	    HUB_HANDLE_PORT_FEATURE_TYPE,
4832 	    USB_REQ_CLEAR_FEATURE,
4833 	    CFS_PORT_ENABLE,
4834 	    port,
4835 	    0,
4836 	    NULL, 0,
4837 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4838 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4839 		    "disable port%d failed (%d 0x%x %d)", port,
4840 		    completion_reason, cb_flags, rval);
4841 		mutex_enter(HUBD_MUTEX(hubd));
4842 
4843 		return (USB_FAILURE);
4844 	}
4845 
4846 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4847 	    "clearing feature CFS_C_PORT_ENABLE");
4848 
4849 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4850 	    hubd->h_default_pipe,
4851 	    HUB_HANDLE_PORT_FEATURE_TYPE,
4852 	    USB_REQ_CLEAR_FEATURE,
4853 	    CFS_C_PORT_ENABLE,
4854 	    port,
4855 	    0,
4856 	    NULL, 0,
4857 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4858 		USB_DPRINTF_L2(DPRINT_MASK_PORT,
4859 		    hubd->h_log_handle,
4860 		    "clear feature CFS_C_PORT_ENABLE port%d failed "
4861 		    "(%d 0x%x %d)",
4862 		    port, completion_reason, cb_flags, rval);
4863 
4864 		mutex_enter(HUBD_MUTEX(hubd));
4865 
4866 		return (USB_FAILURE);
4867 	}
4868 
4869 	mutex_enter(HUBD_MUTEX(hubd));
4870 
4871 	return (USB_SUCCESS);
4872 }
4873 
4874 
4875 /*
4876  * hubd_determine_port_status:
4877  */
4878 static int
4879 hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
4880 		uint16_t *status, uint16_t *change, uint_t ack_flag)
4881 {
4882 	int rval;
4883 	mblk_t	*data = NULL;
4884 	usb_cr_t completion_reason;
4885 	usb_cb_flags_t cb_flags;
4886 
4887 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
4888 	    "hubd_determine_port_status: port=%d, state=0x%x ack=0x%x", port,
4889 	    hubd->h_port_state[port], ack_flag);
4890 
4891 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
4892 
4893 	mutex_exit(HUBD_MUTEX(hubd));
4894 
4895 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4896 	    hubd->h_default_pipe,
4897 	    HUB_GET_PORT_STATUS_TYPE,
4898 	    USB_REQ_GET_STATUS,
4899 	    0,
4900 	    port,
4901 	    GET_STATUS_LENGTH,
4902 	    &data, 0,
4903 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4904 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4905 		    "port=%d get status failed (%d 0x%x %d)",
4906 		    port, completion_reason, cb_flags, rval);
4907 
4908 		if (data) {
4909 			freemsg(data);
4910 		}
4911 
4912 		*status = *change = 0;
4913 		mutex_enter(HUBD_MUTEX(hubd));
4914 
4915 		return (rval);
4916 	}
4917 
4918 	mutex_enter(HUBD_MUTEX(hubd));
4919 	if (MBLKL(data) != GET_STATUS_LENGTH) {
4920 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4921 		    "port %d: length incorrect %ld",
4922 		    port, MBLKL(data));
4923 		freemsg(data);
4924 		*status = *change = 0;
4925 
4926 		return (rval);
4927 	}
4928 
4929 
4930 	*status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
4931 	*change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
4932 
4933 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4934 	    "port%d status=0x%x, change=0x%x", port, *status, *change);
4935 
4936 	freemsg(data);
4937 
4938 	if (*status & PORT_STATUS_CCS) {
4939 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4940 		    "port%d connected", port);
4941 
4942 		hubd->h_port_state[port] |= (PORT_STATUS_CCS & ack_flag);
4943 	} else {
4944 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4945 		    "port%d disconnected", port);
4946 
4947 		hubd->h_port_state[port] &= ~(PORT_STATUS_CCS & ack_flag);
4948 	}
4949 
4950 	if (*status & PORT_STATUS_PES) {
4951 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4952 		    "port%d enabled", port);
4953 
4954 		hubd->h_port_state[port] |= (PORT_STATUS_PES & ack_flag);
4955 	} else {
4956 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4957 		    "port%d disabled", port);
4958 
4959 		hubd->h_port_state[port] &= ~(PORT_STATUS_PES & ack_flag);
4960 	}
4961 
4962 	if (*status & PORT_STATUS_PSS) {
4963 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4964 		    "port%d suspended", port);
4965 
4966 		hubd->h_port_state[port] |= (PORT_STATUS_PSS & ack_flag);
4967 	} else {
4968 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4969 		    "port%d not suspended", port);
4970 
4971 		hubd->h_port_state[port] &= ~(PORT_STATUS_PSS & ack_flag);
4972 	}
4973 
4974 	if (*change & PORT_CHANGE_PRSC) {
4975 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4976 		    "port%d reset completed", port);
4977 
4978 		hubd->h_port_state[port] |= (PORT_CHANGE_PRSC & ack_flag);
4979 	} else {
4980 
4981 		hubd->h_port_state[port] &= ~(PORT_CHANGE_PRSC & ack_flag);
4982 	}
4983 
4984 	if (*status & PORT_STATUS_POCI) {
4985 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4986 		    "port%d overcurrent!", port);
4987 
4988 		hubd->h_port_state[port] |= (PORT_STATUS_POCI & ack_flag);
4989 	} else {
4990 
4991 		hubd->h_port_state[port] &= ~(PORT_STATUS_POCI & ack_flag);
4992 	}
4993 
4994 	if (*status & PORT_STATUS_PRS) {
4995 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4996 		    "port%d reset active", port);
4997 
4998 		hubd->h_port_state[port] |= (PORT_STATUS_PRS & ack_flag);
4999 	} else {
5000 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5001 		    "port%d reset inactive", port);
5002 
5003 		hubd->h_port_state[port] &= ~(PORT_STATUS_PRS & ack_flag);
5004 	}
5005 	if (*status & PORT_STATUS_PPS) {
5006 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5007 		    "port%d power on", port);
5008 
5009 		hubd->h_port_state[port] |= (PORT_STATUS_PPS & ack_flag);
5010 	} else {
5011 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5012 		    "port%d power off", port);
5013 
5014 		hubd->h_port_state[port] &= ~(PORT_STATUS_PPS & ack_flag);
5015 	}
5016 	if (*status & PORT_STATUS_LSDA) {
5017 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5018 		    "port%d low speed", port);
5019 
5020 		hubd->h_port_state[port] |= (PORT_STATUS_LSDA & ack_flag);
5021 	} else {
5022 		hubd->h_port_state[port] &= ~(PORT_STATUS_LSDA & ack_flag);
5023 		if (*status & PORT_STATUS_HSDA) {
5024 			USB_DPRINTF_L3(DPRINT_MASK_PORT,
5025 			    hubd->h_log_handle, "port%d "
5026 			    "high speed", port);
5027 
5028 			hubd->h_port_state[port] |=
5029 			    (PORT_STATUS_HSDA & ack_flag);
5030 		} else {
5031 			USB_DPRINTF_L3(DPRINT_MASK_PORT,
5032 			    hubd->h_log_handle, "port%d "
5033 			    "full speed", port);
5034 
5035 			hubd->h_port_state[port] &=
5036 			    ~(PORT_STATUS_HSDA & ack_flag);
5037 		}
5038 	}
5039 
5040 	/*
5041 	 * Acknowledge connection, enable, reset status
5042 	 */
5043 	if (ack_flag) {
5044 		mutex_exit(HUBD_MUTEX(hubd));
5045 		if (*change & PORT_CHANGE_CSC & ack_flag) {
5046 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5047 			    "clearing feature CFS_C_PORT_CONNECTION");
5048 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5049 			    hubd->h_default_pipe,
5050 			    HUB_HANDLE_PORT_FEATURE_TYPE,
5051 			    USB_REQ_CLEAR_FEATURE,
5052 			    CFS_C_PORT_CONNECTION,
5053 			    port,
5054 			    0, NULL, 0,
5055 			    &completion_reason, &cb_flags, 0)) !=
5056 			    USB_SUCCESS) {
5057 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
5058 				    hubd->h_log_handle,
5059 				    "clear feature CFS_C_PORT_CONNECTION"
5060 				    " port%d failed (%d 0x%x %d)",
5061 				    port, completion_reason, cb_flags, rval);
5062 			}
5063 		}
5064 		if (*change & PORT_CHANGE_PESC & ack_flag) {
5065 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5066 			    "clearing feature CFS_C_PORT_ENABLE");
5067 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5068 			    hubd->h_default_pipe,
5069 			    HUB_HANDLE_PORT_FEATURE_TYPE,
5070 			    USB_REQ_CLEAR_FEATURE,
5071 			    CFS_C_PORT_ENABLE,
5072 			    port,
5073 			    0, NULL, 0,
5074 			    &completion_reason, &cb_flags, 0)) !=
5075 			    USB_SUCCESS) {
5076 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
5077 				    hubd->h_log_handle,
5078 				    "clear feature CFS_C_PORT_ENABLE"
5079 				    " port%d failed (%d 0x%x %d)",
5080 				    port, completion_reason, cb_flags, rval);
5081 			}
5082 		}
5083 		if (*change & PORT_CHANGE_PSSC & ack_flag) {
5084 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5085 			    "clearing feature CFS_C_PORT_SUSPEND");
5086 
5087 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5088 			    hubd->h_default_pipe,
5089 			    HUB_HANDLE_PORT_FEATURE_TYPE,
5090 			    USB_REQ_CLEAR_FEATURE,
5091 			    CFS_C_PORT_SUSPEND,
5092 			    port,
5093 			    0, NULL, 0,
5094 			    &completion_reason, &cb_flags, 0)) !=
5095 			    USB_SUCCESS) {
5096 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
5097 				    hubd->h_log_handle,
5098 				    "clear feature CFS_C_PORT_SUSPEND"
5099 				    " port%d failed (%d 0x%x %d)",
5100 				    port, completion_reason, cb_flags, rval);
5101 			}
5102 		}
5103 		if (*change & PORT_CHANGE_OCIC & ack_flag) {
5104 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5105 			    "clearing feature CFS_C_PORT_OVER_CURRENT");
5106 
5107 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5108 			    hubd->h_default_pipe,
5109 			    HUB_HANDLE_PORT_FEATURE_TYPE,
5110 			    USB_REQ_CLEAR_FEATURE,
5111 			    CFS_C_PORT_OVER_CURRENT,
5112 			    port,
5113 			    0, NULL, 0,
5114 			    &completion_reason, &cb_flags, 0)) !=
5115 			    USB_SUCCESS) {
5116 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
5117 				    hubd->h_log_handle,
5118 				    "clear feature CFS_C_PORT_OVER_CURRENT"
5119 				    " port%d failed (%d 0x%x %d)",
5120 				    port, completion_reason, cb_flags, rval);
5121 			}
5122 		}
5123 		if (*change & PORT_CHANGE_PRSC & ack_flag) {
5124 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5125 			    "clearing feature CFS_C_PORT_RESET");
5126 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5127 			    hubd->h_default_pipe,
5128 			    HUB_HANDLE_PORT_FEATURE_TYPE,
5129 			    USB_REQ_CLEAR_FEATURE,
5130 			    CFS_C_PORT_RESET,
5131 			    port,
5132 			    0, NULL, 0,
5133 			    &completion_reason, &cb_flags, 0)) !=
5134 			    USB_SUCCESS) {
5135 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
5136 				    hubd->h_log_handle,
5137 				    "clear feature CFS_C_PORT_RESET"
5138 				    " port%d failed (%d 0x%x %d)",
5139 				    port, completion_reason, cb_flags, rval);
5140 			}
5141 		}
5142 		mutex_enter(HUBD_MUTEX(hubd));
5143 	}
5144 
5145 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5146 	    "new port%d state 0x%x", port, hubd->h_port_state[port]);
5147 
5148 
5149 	return (USB_SUCCESS);
5150 }
5151 
5152 
5153 /*
5154  * hubd_recover_disabled_port
5155  * if the port got disabled because of an error
5156  * enable it. If hub doesn't suport enable port,
5157  * reset the port to bring the device to life again
5158  */
5159 static int
5160 hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port)
5161 {
5162 	uint16_t	status;
5163 	uint16_t	change;
5164 	int		rval = USB_FAILURE;
5165 
5166 	/* first try enabling the port */
5167 	(void) hubd_enable_port(hubd, port);
5168 
5169 	/* read the port status */
5170 	(void) hubd_determine_port_status(hubd, port, &status, &change,
5171 	    PORT_CHANGE_PESC);
5172 
5173 	if (status & PORT_STATUS_PES) {
5174 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5175 		    "Port%d now Enabled", port);
5176 	} else if (status & PORT_STATUS_CCS) {
5177 		/* first post a disconnect event to the child */
5178 		mutex_exit(HUBD_MUTEX(hubd));
5179 		hubd_post_event(hubd, port, USBA_EVENT_TAG_HOT_REMOVAL);
5180 		mutex_enter(HUBD_MUTEX(hubd));
5181 
5182 		/* then reset the port and recover the device */
5183 		rval = hubd_handle_port_connect(hubd, port);
5184 
5185 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5186 		    "Port%d now Enabled by force", port);
5187 	}
5188 
5189 	return (rval);
5190 }
5191 
5192 
5193 /*
5194  * hubd_enable_all_port_power:
5195  */
5196 static int
5197 hubd_enable_all_port_power(hubd_t *hubd)
5198 {
5199 	usb_hub_descr_t	*hub_descr;
5200 	int		wait;
5201 	usb_port_t	port;
5202 	uint_t		retry;
5203 	uint16_t	status;
5204 	uint16_t	change;
5205 
5206 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5207 	    "hubd_enable_all_port_power");
5208 
5209 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5210 
5211 	hub_descr = &hubd->h_hub_descr;
5212 
5213 	/*
5214 	 * According to section 11.11 of USB, for hubs with no power
5215 	 * switches, bPwrOn2PwrGood is zero. But we wait for some
5216 	 * arbitrary time to enable power to become stable.
5217 	 *
5218 	 * If an hub supports port power switching, we need to wait
5219 	 * at least 20ms before accessing corresponding usb port.
5220 	 */
5221 	if ((hub_descr->wHubCharacteristics &
5222 	    HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) {
5223 		wait = hubd_device_delay / 10;
5224 	} else {
5225 		wait = max(HUB_DEFAULT_POPG,
5226 		    hub_descr->bPwrOn2PwrGood) * 2 * 1000;
5227 	}
5228 
5229 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5230 	    "hubd_enable_all_port_power: popg=%d wait=%d",
5231 	    hub_descr->bPwrOn2PwrGood, wait);
5232 
5233 	/*
5234 	 * Enable power per port. we ignore gang power and power mask
5235 	 * and always enable all ports one by one.
5236 	 */
5237 	for (port = 1; port <= hub_descr->bNbrPorts; port++) {
5238 		/*
5239 		 * Transition the port from the Powered Off to the
5240 		 * Disconnected state by supplying power to the port.
5241 		 */
5242 		USB_DPRINTF_L4(DPRINT_MASK_PORT,
5243 		    hubd->h_log_handle,
5244 		    "hubd_enable_all_port_power: power port=%d", port);
5245 
5246 		(void) hubd_enable_port_power(hubd, port);
5247 	}
5248 
5249 	mutex_exit(HUBD_MUTEX(hubd));
5250 	delay(drv_usectohz(wait));
5251 	mutex_enter(HUBD_MUTEX(hubd));
5252 
5253 	/* For retry if any, use some extra delay */
5254 	wait = max(wait, hubd_device_delay / 10);
5255 
5256 	/* Check each port power status for a given usb hub */
5257 	for (port = 1; port <= hub_descr->bNbrPorts; port++) {
5258 
5259 		/* Get port status */
5260 		(void) hubd_determine_port_status(hubd, port,
5261 		    &status, &change, 0);
5262 
5263 		for (retry = 0; ((!(status & PORT_STATUS_PPS)) &&
5264 		    (retry < HUBD_PORT_RETRY)); retry++) {
5265 
5266 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5267 			    "Retry is in progress %d: port %d status %d",
5268 			    retry, port, status);
5269 
5270 			(void) hubd_enable_port_power(hubd, port);
5271 
5272 			mutex_exit(HUBD_MUTEX(hubd));
5273 			delay(drv_usectohz(wait));
5274 			mutex_enter(HUBD_MUTEX(hubd));
5275 
5276 			/* Get port status */
5277 			(void) hubd_determine_port_status(hubd, port,
5278 			    &status, &change, 0);
5279 		}
5280 
5281 		/* Print warning message if port has no power */
5282 		if (!(status & PORT_STATUS_PPS)) {
5283 
5284 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5285 			    "hubd_enable_all_port_power: port %d power-on "
5286 			    "failed, port status 0x%x", port, status);
5287 		}
5288 	}
5289 
5290 	return (USB_SUCCESS);
5291 }
5292 
5293 
5294 /*
5295  * hubd_enable_port_power:
5296  *	enable individual port power
5297  */
5298 static int
5299 hubd_enable_port_power(hubd_t *hubd, usb_port_t port)
5300 {
5301 	int		rval;
5302 	usb_cr_t	completion_reason;
5303 	usb_cb_flags_t	cb_flags;
5304 
5305 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5306 	    "hubd_enable_port_power: port=%d", port);
5307 
5308 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5309 	ASSERT(hubd->h_default_pipe != 0);
5310 
5311 	mutex_exit(HUBD_MUTEX(hubd));
5312 
5313 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5314 	    hubd->h_default_pipe,
5315 	    HUB_HANDLE_PORT_FEATURE_TYPE,
5316 	    USB_REQ_SET_FEATURE,
5317 	    CFS_PORT_POWER,
5318 	    port,
5319 	    0, NULL, 0,
5320 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
5321 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5322 		    "set port power failed (%d 0x%x %d)",
5323 		    completion_reason, cb_flags, rval);
5324 		mutex_enter(HUBD_MUTEX(hubd));
5325 
5326 		return (USB_FAILURE);
5327 	} else {
5328 		mutex_enter(HUBD_MUTEX(hubd));
5329 		hubd->h_port_state[port] |= PORT_STATUS_PPS;
5330 
5331 		return (USB_SUCCESS);
5332 	}
5333 }
5334 
5335 
5336 /*
5337  * hubd_disable_all_port_power:
5338  */
5339 static int
5340 hubd_disable_all_port_power(hubd_t *hubd)
5341 {
5342 	usb_port_t port;
5343 
5344 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5345 	    "hubd_disable_all_port_power");
5346 
5347 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5348 
5349 	/*
5350 	 * disable power per port, ignore gang power and power mask
5351 	 */
5352 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
5353 		(void) hubd_disable_port_power(hubd, port);
5354 	}
5355 
5356 	return (USB_SUCCESS);
5357 }
5358 
5359 
5360 /*
5361  * hubd_disable_port_power:
5362  *	disable individual port power
5363  */
5364 static int
5365 hubd_disable_port_power(hubd_t *hubd, usb_port_t port)
5366 {
5367 	int		rval;
5368 	usb_cr_t	completion_reason;
5369 	usb_cb_flags_t	cb_flags;
5370 
5371 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
5372 	    "hubd_disable_port_power: port=%d", port);
5373 
5374 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5375 
5376 	mutex_exit(HUBD_MUTEX(hubd));
5377 
5378 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5379 	    hubd->h_default_pipe,
5380 	    HUB_HANDLE_PORT_FEATURE_TYPE,
5381 	    USB_REQ_CLEAR_FEATURE,
5382 	    CFS_PORT_POWER,
5383 	    port,
5384 	    0, NULL, 0,
5385 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
5386 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5387 		    "clearing port%d power failed (%d 0x%x %d)",
5388 		    port, completion_reason, cb_flags, rval);
5389 
5390 		mutex_enter(HUBD_MUTEX(hubd));
5391 
5392 		return (USB_FAILURE);
5393 	} else {
5394 
5395 		mutex_enter(HUBD_MUTEX(hubd));
5396 		ASSERT(completion_reason == 0);
5397 		hubd->h_port_state[port] &= ~PORT_STATUS_PPS;
5398 
5399 		return (USB_SUCCESS);
5400 	}
5401 }
5402 
5403 
5404 /*
5405  * Search the database of user preferences and find out the preferred
5406  * configuration for this new device
5407  */
5408 int
5409 hubd_select_device_configuration(hubd_t *hubd, usb_port_t port,
5410 	dev_info_t *child_dip, usba_device_t *child_ud)
5411 {
5412 	char		*pathname = NULL;
5413 	char		*tmp_path = NULL;
5414 	int		user_conf;
5415 	int		pathlen;
5416 	usb_dev_descr_t	*usbdev_ptr;
5417 	usba_configrec_t *user_pref;
5418 
5419 	mutex_enter(&child_ud->usb_mutex);
5420 	usbdev_ptr = child_ud->usb_dev_descr;
5421 	mutex_exit(&child_ud->usb_mutex);
5422 
5423 	/* try to get pathname for this device */
5424 	tmp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5425 	(void) ddi_pathname(child_dip, tmp_path);
5426 
5427 	pathlen = strlen(tmp_path) + 32;
5428 	pathname = kmem_zalloc(pathlen, KM_SLEEP);
5429 
5430 	/*
5431 	 * We haven't initialized the node and it doesn't have an address
5432 	 * yet. Append port number to the physical pathname
5433 	 */
5434 	(void) sprintf(pathname, "%s@%d", tmp_path, port);
5435 
5436 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5437 	    "hubd_select_device_configuration: Device=%s\n\t"
5438 	    "Child path=%s",
5439 	    usba_get_mfg_prod_sn_str(child_dip, tmp_path, MAXPATHLEN),
5440 	    pathname);
5441 	kmem_free(tmp_path, MAXPATHLEN);
5442 
5443 
5444 	/* database search for user preferences */
5445 	user_pref = usba_devdb_get_user_preferences(usbdev_ptr->idVendor,
5446 	    usbdev_ptr->idProduct, child_ud->usb_serialno_str, pathname);
5447 
5448 	if (user_pref) {
5449 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5450 		    "hubd_select_device_configuration: "
5451 		    "usba_devdb_get_user_preferences "
5452 		    "return user_conf=%d\npreferred driver=%s path=%s",
5453 		    user_pref->cfg_index, user_pref->driver,
5454 		    user_pref->pathname);
5455 
5456 		user_conf = user_pref->cfg_index;
5457 
5458 		if (user_pref->driver) {
5459 			mutex_enter(&child_ud->usb_mutex);
5460 			child_ud->usb_preferred_driver = user_pref->driver;
5461 			mutex_exit(&child_ud->usb_mutex);
5462 		}
5463 	} else {
5464 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5465 		    "hubd_select_device_configuration: No match found");
5466 
5467 		/* select default configuration for this device */
5468 		user_conf = USBA_DEV_CONFIG_INDEX_UNDEFINED;
5469 	}
5470 	kmem_free(pathname, pathlen);
5471 
5472 	/* if the device has just one configuration, set default value */
5473 	if (usbdev_ptr->bNumConfigurations == 1) {
5474 		user_conf = USB_DEV_DEFAULT_CONFIG_INDEX;
5475 	}
5476 
5477 	return (user_conf);
5478 }
5479 
5480 
5481 /*
5482  * Retrieves config cloud for this configuration
5483  */
5484 int
5485 hubd_get_this_config_cloud(hubd_t *hubd, dev_info_t *dip,
5486 	usba_device_t *child_ud, uint16_t conf_index)
5487 {
5488 	usb_cfg_descr_t	*confdescr;
5489 	mblk_t		*pdata = NULL;
5490 	int		rval;
5491 	size_t		size;
5492 	char		*tmpbuf;
5493 	usb_cr_t	completion_reason;
5494 	usb_cb_flags_t	cb_flags;
5495 	usb_pipe_handle_t	def_ph;
5496 
5497 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5498 	    "hubd_get_this_config_cloud: conf_index=%d", conf_index);
5499 
5500 
5501 	/* alloc temporary space for config descriptor */
5502 	confdescr = (usb_cfg_descr_t *)kmem_zalloc(USB_CFG_DESCR_SIZE,
5503 	    KM_SLEEP);
5504 
5505 	/* alloc temporary space for string descriptor */
5506 	tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
5507 
5508 	def_ph = usba_get_dflt_pipe_handle(dip);
5509 
5510 	if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
5511 	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
5512 	    USB_REQ_GET_DESCR,
5513 	    USB_DESCR_TYPE_SETUP_CFG | conf_index,
5514 	    0,
5515 	    USB_CFG_DESCR_SIZE,
5516 	    &pdata,
5517 	    0,
5518 	    &completion_reason,
5519 	    &cb_flags,
5520 	    0)) == USB_SUCCESS) {
5521 
5522 		/* this must be true since we didn't allow data underruns */
5523 		if (MBLKL(pdata) != USB_CFG_DESCR_SIZE) {
5524 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5525 			    "device returned incorrect configuration "
5526 			    "descriptor size.");
5527 
5528 			rval = USB_FAILURE;
5529 			goto done;
5530 		}
5531 
5532 		/*
5533 		 * Parse the configuration descriptor
5534 		 */
5535 		size = usb_parse_cfg_descr(pdata->b_rptr,
5536 		    MBLKL(pdata), confdescr,
5537 		    USB_CFG_DESCR_SIZE);
5538 
5539 		/* if parse cfg descr error, it should return failure */
5540 		if (size == USB_PARSE_ERROR) {
5541 
5542 			if (pdata->b_rptr[1] != USB_DESCR_TYPE_CFG) {
5543 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
5544 				    hubd->h_log_handle,
5545 				    "device returned incorrect "
5546 				    "configuration descriptor type.");
5547 			}
5548 			rval = USB_FAILURE;
5549 			goto done;
5550 		}
5551 
5552 		if (confdescr->wTotalLength < USB_CFG_DESCR_SIZE) {
5553 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
5554 			    hubd->h_log_handle,
5555 			    "device returned incorrect "
5556 			    "configuration descriptor size.");
5557 
5558 			rval = USB_FAILURE;
5559 			goto done;
5560 		}
5561 
5562 		freemsg(pdata);
5563 		pdata = NULL;
5564 
5565 		/* Now fetch the complete config cloud */
5566 		if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
5567 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
5568 		    USB_REQ_GET_DESCR,
5569 		    USB_DESCR_TYPE_SETUP_CFG | conf_index,
5570 		    0,
5571 		    confdescr->wTotalLength,
5572 		    &pdata,
5573 		    0,
5574 		    &completion_reason,
5575 		    &cb_flags,
5576 		    0)) == USB_SUCCESS) {
5577 
5578 			if (MBLKL(pdata) !=
5579 			    confdescr->wTotalLength) {
5580 
5581 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
5582 				    hubd->h_log_handle,
5583 				    "device returned incorrect "
5584 				    "configuration descriptor.");
5585 
5586 				rval = USB_FAILURE;
5587 				goto done;
5588 			}
5589 
5590 			/*
5591 			 * copy config descriptor into usba_device
5592 			 */
5593 			mutex_enter(&child_ud->usb_mutex);
5594 			child_ud->usb_cfg_array[conf_index] =
5595 			    kmem_alloc(confdescr->wTotalLength, KM_SLEEP);
5596 			child_ud->usb_cfg_array_len[conf_index] =
5597 			    confdescr->wTotalLength;
5598 			bcopy((caddr_t)pdata->b_rptr,
5599 			    (caddr_t)child_ud->usb_cfg_array[conf_index],
5600 			    confdescr->wTotalLength);
5601 			mutex_exit(&child_ud->usb_mutex);
5602 
5603 			/*
5604 			 * retrieve string descriptor describing this
5605 			 * configuration
5606 			 */
5607 			if (confdescr->iConfiguration) {
5608 
5609 				USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
5610 				    hubd->h_log_handle,
5611 				    "Get conf str descr for config_index=%d",
5612 				    conf_index);
5613 
5614 				/*
5615 				 * Now fetch the string descriptor describing
5616 				 * this configuration
5617 				 */
5618 				if ((rval = usb_get_string_descr(dip,
5619 				    USB_LANG_ID, confdescr->iConfiguration,
5620 				    tmpbuf, USB_MAXSTRINGLEN)) ==
5621 				    USB_SUCCESS) {
5622 					size = strlen(tmpbuf);
5623 					if (size > 0) {
5624 						child_ud->usb_cfg_str_descr
5625 						    [conf_index] = (char *)
5626 						    kmem_zalloc(size + 1,
5627 						    KM_SLEEP);
5628 						(void) strcpy(
5629 						    child_ud->usb_cfg_str_descr
5630 						    [conf_index], tmpbuf);
5631 					}
5632 				} else {
5633 					USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
5634 					    hubd->h_log_handle,
5635 					    "hubd_get_this_config_cloud: "
5636 					    "getting config string (%d) "
5637 					    "failed",
5638 					    confdescr->iConfiguration);
5639 
5640 					/* ignore this error */
5641 					rval = USB_SUCCESS;
5642 				}
5643 			}
5644 		}
5645 	}
5646 
5647 done:
5648 	if (rval != USB_SUCCESS) {
5649 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5650 		    "hubd_get_this_config_cloud: "
5651 		    "error in retrieving config descriptor for "
5652 		    "config index=%d rval=%d cr=%d",
5653 		    conf_index, rval, completion_reason);
5654 	}
5655 
5656 	if (pdata) {
5657 		freemsg(pdata);
5658 		pdata = NULL;
5659 	}
5660 
5661 	kmem_free(confdescr, USB_CFG_DESCR_SIZE);
5662 	kmem_free(tmpbuf, USB_MAXSTRINGLEN);
5663 
5664 	return (rval);
5665 }
5666 
5667 
5668 /*
5669  * Retrieves the entire config cloud for all configurations of the device
5670  */
5671 int
5672 hubd_get_all_device_config_cloud(hubd_t *hubd, dev_info_t *dip,
5673 	usba_device_t *child_ud)
5674 {
5675 	int		rval = USB_SUCCESS;
5676 	int		ncfgs;
5677 	uint16_t	size;
5678 	uint16_t	conf_index;
5679 	uchar_t		**cfg_array;
5680 	uint16_t	*cfg_array_len;
5681 	char		**str_descr;
5682 
5683 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5684 	    "hubd_get_all_device_config_cloud: Start");
5685 
5686 	/* alloc pointer array for conf. descriptors */
5687 	mutex_enter(&child_ud->usb_mutex);
5688 	ncfgs = child_ud->usb_n_cfgs;
5689 	mutex_exit(&child_ud->usb_mutex);
5690 
5691 	size = sizeof (uchar_t *) * ncfgs;
5692 	cfg_array = kmem_zalloc(size, KM_SLEEP);
5693 	cfg_array_len = kmem_zalloc(ncfgs * sizeof (uint16_t), KM_SLEEP);
5694 	str_descr = kmem_zalloc(size, KM_SLEEP);
5695 
5696 	mutex_enter(&child_ud->usb_mutex);
5697 	child_ud->usb_cfg_array = cfg_array;
5698 	child_ud->usb_cfg_array_len = cfg_array_len;
5699 	child_ud->usb_cfg_array_length = size;
5700 	child_ud->usb_cfg_array_len_length = ncfgs * sizeof (uint16_t);
5701 	child_ud->usb_cfg_str_descr = str_descr;
5702 	mutex_exit(&child_ud->usb_mutex);
5703 
5704 	/* Get configuration descriptor for each configuration */
5705 	for (conf_index = 0; (conf_index < ncfgs) &&
5706 	    (rval == USB_SUCCESS); conf_index++) {
5707 
5708 		rval = hubd_get_this_config_cloud(hubd, dip, child_ud,
5709 		    conf_index);
5710 	}
5711 
5712 	return (rval);
5713 }
5714 
5715 
5716 /*
5717  * hubd_ready_device:
5718  *	Update the usba_device structure
5719  *	Set the given configuration
5720  *	Prepares the device node for driver to online. If an existing
5721  *	OBP node is found, it will switch to the OBP node.
5722  */
5723 dev_info_t *
5724 hubd_ready_device(hubd_t *hubd, dev_info_t *child_dip, usba_device_t *child_ud,
5725     uint_t config_index)
5726 {
5727 	usb_cr_t	completion_reason;
5728 	usb_cb_flags_t	cb_flags;
5729 	size_t		size;
5730 	usb_cfg_descr_t	config_descriptor;
5731 	usb_pipe_handle_t def_ph;
5732 	usba_pipe_handle_data_t	*ph;
5733 
5734 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5735 	    "hubd_ready_device: dip=0x%p, user_conf_index=%d",
5736 	    (void *)child_dip, config_index);
5737 
5738 	size = usb_parse_cfg_descr(
5739 	    child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
5740 	    &config_descriptor, USB_CFG_DESCR_SIZE);
5741 	ASSERT(size == USB_CFG_DESCR_SIZE);
5742 
5743 	def_ph = usba_get_dflt_pipe_handle(child_dip);
5744 
5745 	/* Set the configuration */
5746 	(void) usb_pipe_sync_ctrl_xfer(child_dip, def_ph,
5747 	    USB_DEV_REQ_HOST_TO_DEV,
5748 	    USB_REQ_SET_CFG,	/* bRequest */
5749 	    config_descriptor.bConfigurationValue,	/* wValue */
5750 	    0,				/* wIndex */
5751 	    0,				/* wLength */
5752 	    NULL,
5753 	    0,
5754 	    &completion_reason,
5755 	    &cb_flags,
5756 	    0);
5757 
5758 	mutex_enter(&child_ud->usb_mutex);
5759 	child_ud->usb_active_cfg_ndx	= config_index;
5760 	child_ud->usb_cfg		= child_ud->usb_cfg_array[config_index];
5761 	child_ud->usb_cfg_length	= config_descriptor.wTotalLength;
5762 	child_ud->usb_cfg_value 	= config_descriptor.bConfigurationValue;
5763 	child_ud->usb_n_ifs		= config_descriptor.bNumInterfaces;
5764 	child_ud->usb_dip		= child_dip;
5765 
5766 	child_ud->usb_client_flags	= kmem_zalloc(
5767 	    child_ud->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
5768 
5769 	child_ud->usb_client_attach_list = kmem_zalloc(
5770 	    child_ud->usb_n_ifs *
5771 	    sizeof (*child_ud->usb_client_attach_list), KM_SLEEP);
5772 
5773 	child_ud->usb_client_ev_cb_list = kmem_zalloc(
5774 	    child_ud->usb_n_ifs *
5775 	    sizeof (*child_ud->usb_client_ev_cb_list), KM_SLEEP);
5776 
5777 	mutex_exit(&child_ud->usb_mutex);
5778 
5779 	/* ready the device node */
5780 	child_dip = usba_ready_device_node(child_dip);
5781 
5782 	/* set owner of default pipe to child dip */
5783 	ph = usba_get_ph_data(def_ph);
5784 	mutex_enter(&ph->p_mutex);
5785 	mutex_enter(&ph->p_ph_impl->usba_ph_mutex);
5786 	ph->p_ph_impl->usba_ph_dip = ph->p_dip = child_dip;
5787 	mutex_exit(&ph->p_ph_impl->usba_ph_mutex);
5788 	mutex_exit(&ph->p_mutex);
5789 
5790 	return (child_dip);
5791 }
5792 
5793 
5794 /*
5795  * hubd_create_child
5796  *	- create child dip
5797  *	- open default pipe
5798  *	- get device descriptor
5799  *	- set the address
5800  *	- get device string descriptors
5801  *	- get the entire config cloud (all configurations) of the device
5802  *	- set user preferred configuration
5803  *	- close default pipe
5804  *	- load appropriate driver(s)
5805  */
5806 static int
5807 hubd_create_child(dev_info_t *dip,
5808 		hubd_t		*hubd,
5809 		usba_device_t	*hubd_ud,
5810 		usb_port_status_t port_status,
5811 		usb_port_t	port,
5812 		int		iteration)
5813 {
5814 	dev_info_t		*child_dip = NULL;
5815 	usb_dev_descr_t	usb_dev_descr;
5816 	int			rval;
5817 	usba_device_t		*child_ud = NULL;
5818 	usba_device_t		*parent_ud = NULL;
5819 	usb_pipe_handle_t	ph = NULL; /* default pipe handle */
5820 	mblk_t			*pdata = NULL;
5821 	usb_cr_t		completion_reason;
5822 	int			user_conf_index;
5823 	uint_t			config_index;
5824 	usb_cb_flags_t		cb_flags;
5825 	uchar_t			address = 0;
5826 	uint16_t		length;
5827 	size_t			size;
5828 	usb_addr_t		parent_usb_addr;
5829 	usb_port_t		parent_usb_port;
5830 	usba_device_t		*parent_usba_dev;
5831 	usb_port_status_t	parent_port_status;
5832 
5833 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5834 	    "hubd_create_child: port=%d", port);
5835 
5836 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
5837 	ASSERT(hubd->h_usba_devices[port] == NULL);
5838 
5839 	mutex_exit(HUBD_MUTEX(hubd));
5840 
5841 	/*
5842 	 * create a dip which can be used to open the pipe. we set
5843 	 * the name after getting the descriptors from the device
5844 	 */
5845 	rval = usba_create_child_devi(dip,
5846 	    "device",		/* driver name */
5847 	    hubd_ud->usb_hcdi_ops, /* usba_hcdi ops */
5848 	    hubd_ud->usb_root_hub_dip,
5849 	    port_status,		/* low speed device */
5850 	    child_ud,
5851 	    &child_dip);
5852 
5853 	if (rval != USB_SUCCESS) {
5854 
5855 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5856 		    "usb_create_child_devi failed (%d)", rval);
5857 
5858 		goto fail_cleanup;
5859 	}
5860 
5861 	child_ud = usba_get_usba_device(child_dip);
5862 	ASSERT(child_ud != NULL);
5863 
5864 	parent_ud = hubd->h_usba_device;
5865 	mutex_enter(&parent_ud->usb_mutex);
5866 	parent_port_status = parent_ud->usb_port_status;
5867 
5868 	/*
5869 	 * To support split transactions, update address and port
5870 	 * of high speed hub to which given device is connected.
5871 	 */
5872 	if (parent_port_status == USBA_HIGH_SPEED_DEV) {
5873 		parent_usba_dev = parent_ud;
5874 		parent_usb_addr = parent_ud->usb_addr;
5875 		parent_usb_port = port;
5876 	} else {
5877 		parent_usba_dev = parent_ud->usb_hs_hub_usba_dev;
5878 		parent_usb_addr = parent_ud->usb_hs_hub_addr;
5879 		parent_usb_port = parent_ud->usb_hs_hub_port;
5880 	}
5881 	mutex_exit(&parent_ud->usb_mutex);
5882 
5883 	mutex_enter(&child_ud->usb_mutex);
5884 	address = child_ud->usb_addr;
5885 	child_ud->usb_addr = 0;
5886 	child_ud->usb_dev_descr = kmem_alloc(sizeof (usb_dev_descr_t),
5887 	    KM_SLEEP);
5888 	bzero(&usb_dev_descr, sizeof (usb_dev_descr_t));
5889 	usb_dev_descr.bMaxPacketSize0 =
5890 	    (port_status == USBA_LOW_SPEED_DEV) ? 8 : 64;
5891 	bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
5892 	    sizeof (usb_dev_descr_t));
5893 	child_ud->usb_port = port;
5894 	child_ud->usb_hs_hub_usba_dev = parent_usba_dev;
5895 	child_ud->usb_hs_hub_addr = parent_usb_addr;
5896 	child_ud->usb_hs_hub_port = parent_usb_port;
5897 	mutex_exit(&child_ud->usb_mutex);
5898 
5899 	/* Open the default pipe */
5900 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
5901 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
5902 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5903 		    "usb_pipe_open failed (%d)", rval);
5904 
5905 		goto fail_cleanup;
5906 	}
5907 
5908 	/*
5909 	 * get device descriptor
5910 	 */
5911 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5912 	    "hubd_create_child: get device descriptor: 64 bytes");
5913 
5914 	rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
5915 	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
5916 	    USB_REQ_GET_DESCR,			/* bRequest */
5917 	    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
5918 	    0,					/* wIndex */
5919 	    64,					/* wLength */
5920 	    &pdata, USB_ATTRS_SHORT_XFER_OK,
5921 	    &completion_reason, &cb_flags, 0);
5922 
5923 	if ((rval != USB_SUCCESS) &&
5924 	    (!((completion_reason == USB_CR_DATA_OVERRUN) && pdata))) {
5925 
5926 		/*
5927 		 * rval != USB_SUCCESS AND
5928 		 * completion_reason != USB_CR_DATA_OVERRUN
5929 		 * pdata could be != NULL.
5930 		 * Free pdata now to prevent memory leak.
5931 		 */
5932 		freemsg(pdata);
5933 		pdata = NULL;
5934 
5935 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5936 		    "hubd_create_child: get device descriptor: 8 bytes");
5937 
5938 		rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
5939 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
5940 		    USB_REQ_GET_DESCR,			/* bRequest */
5941 		    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
5942 		    0,					/* wIndex */
5943 		    8,					/* wLength */
5944 		    &pdata, USB_ATTRS_NONE,
5945 		    &completion_reason, &cb_flags, 0);
5946 
5947 		if (rval != USB_SUCCESS) {
5948 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5949 			    "getting device descriptor failed (%s 0x%x %d)",
5950 			    usb_str_cr(completion_reason), cb_flags, rval);
5951 			goto fail_cleanup;
5952 		}
5953 	} else {
5954 		ASSERT(completion_reason == USB_CR_OK);
5955 	}
5956 
5957 	ASSERT(pdata != NULL);
5958 
5959 	size = usb_parse_dev_descr(
5960 	    pdata->b_rptr,
5961 	    MBLKL(pdata),
5962 	    &usb_dev_descr,
5963 	    sizeof (usb_dev_descr_t));
5964 
5965 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5966 	    "parsing device descriptor returned %lu", size);
5967 
5968 	length = *(pdata->b_rptr);
5969 	freemsg(pdata);
5970 	pdata = NULL;
5971 	if (size < 8) {
5972 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5973 		    "get device descriptor returned %lu bytes", size);
5974 
5975 		goto fail_cleanup;
5976 	}
5977 
5978 	if (length < 8) {
5979 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5980 		    "fail enumeration: bLength=%d", length);
5981 
5982 		goto fail_cleanup;
5983 	}
5984 
5985 	/* Set the address of the device */
5986 	if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
5987 	    USB_DEV_REQ_HOST_TO_DEV,
5988 	    USB_REQ_SET_ADDRESS,	/* bRequest */
5989 	    address,			/* wValue */
5990 	    0,				/* wIndex */
5991 	    0,				/* wLength */
5992 	    NULL, 0,
5993 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
5994 		char buffer[64];
5995 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5996 		    "setting address failed (cr=%s cb_flags=%s rval=%d)",
5997 		    usb_str_cr(completion_reason),
5998 		    usb_str_cb_flags(cb_flags, buffer, sizeof (buffer)),
5999 		    rval);
6000 
6001 		goto fail_cleanup;
6002 	}
6003 
6004 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6005 	    "set address 0x%x done", address);
6006 
6007 	/* now close the pipe for addr 0 */
6008 	usb_pipe_close(child_dip, ph,
6009 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
6010 
6011 	/*
6012 	 * This delay is important for the CATC hub to enumerate
6013 	 * But, avoid delay in the first iteration
6014 	 */
6015 	if (iteration) {
6016 		delay(drv_usectohz(hubd_device_delay/100));
6017 	}
6018 
6019 	/* assign the address in the usba_device structure */
6020 	mutex_enter(&child_ud->usb_mutex);
6021 	child_ud->usb_addr = address;
6022 	child_ud->usb_no_cpr = 0;
6023 	child_ud->usb_port_status = port_status;
6024 	/* save this device descriptor */
6025 	bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
6026 	    sizeof (usb_dev_descr_t));
6027 	child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
6028 	mutex_exit(&child_ud->usb_mutex);
6029 
6030 	/* re-open the pipe for the device with the new address */
6031 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
6032 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
6033 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6034 		    "usb_pipe_open failed (%d)", rval);
6035 
6036 		goto fail_cleanup;
6037 	}
6038 
6039 	/*
6040 	 * Get full device descriptor only if we have not received full
6041 	 * device descriptor earlier.
6042 	 */
6043 	if (size < length) {
6044 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6045 		    "hubd_create_child: get full device descriptor: "
6046 		    "%d bytes", length);
6047 
6048 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
6049 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
6050 		    USB_REQ_GET_DESCR,			/* bRequest */
6051 		    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
6052 		    0,					/* wIndex */
6053 		    length,				/* wLength */
6054 		    &pdata, 0,
6055 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
6056 			freemsg(pdata);
6057 			pdata = NULL;
6058 
6059 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
6060 			    hubd->h_log_handle,
6061 			    "hubd_create_child: get full device descriptor: "
6062 			    "64 bytes");
6063 
6064 			rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
6065 			    USB_DEV_REQ_DEV_TO_HOST |
6066 			    USB_DEV_REQ_TYPE_STANDARD,
6067 			    USB_REQ_GET_DESCR,		/* bRequest */
6068 			    USB_DESCR_TYPE_SETUP_DEV,	/* wValue */
6069 			    0,				/* wIndex */
6070 			    64,				/* wLength */
6071 			    &pdata, USB_ATTRS_SHORT_XFER_OK,
6072 			    &completion_reason, &cb_flags, 0);
6073 
6074 			/* we have to trust the data now */
6075 			if (pdata) {
6076 				int len = *(pdata->b_rptr);
6077 
6078 				length = MBLKL(pdata);
6079 				if (length < len) {
6080 
6081 					goto fail_cleanup;
6082 				}
6083 			} else if (rval != USB_SUCCESS) {
6084 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
6085 				    hubd->h_log_handle,
6086 				    "getting device descriptor failed "
6087 				    "(%d 0x%x %d)",
6088 				    completion_reason, cb_flags, rval);
6089 
6090 				goto fail_cleanup;
6091 			}
6092 		}
6093 
6094 		size = usb_parse_dev_descr(
6095 		    pdata->b_rptr,
6096 		    MBLKL(pdata),
6097 		    &usb_dev_descr,
6098 		    sizeof (usb_dev_descr_t));
6099 
6100 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6101 		    "parsing device descriptor returned %lu", size);
6102 
6103 		/*
6104 		 * For now, free the data
6105 		 * eventually, each configuration may need to be looked at
6106 		 */
6107 		freemsg(pdata);
6108 		pdata = NULL;
6109 
6110 		if (size != USB_DEV_DESCR_SIZE) {
6111 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6112 			    "fail enumeration: descriptor size=%lu "
6113 			    "expected size=%u", size, USB_DEV_DESCR_SIZE);
6114 
6115 			goto fail_cleanup;
6116 		}
6117 
6118 		/*
6119 		 * save the device descriptor in usba_device since it is needed
6120 		 * later on again
6121 		 */
6122 		mutex_enter(&child_ud->usb_mutex);
6123 		bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
6124 		    sizeof (usb_dev_descr_t));
6125 		child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
6126 		mutex_exit(&child_ud->usb_mutex);
6127 	}
6128 
6129 	if (usb_dev_descr.bNumConfigurations == 0) {
6130 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6131 		    "device descriptor:\n\t"
6132 		    "l=0x%x type=0x%x USB=0x%x class=0x%x subclass=0x%x\n\t"
6133 		    "protocol=0x%x maxpktsize=0x%x "
6134 		    "Vid=0x%x Pid=0x%x rel=0x%x\n\t"
6135 		    "Mfg=0x%x P=0x%x sn=0x%x #config=0x%x",
6136 		    usb_dev_descr.bLength, usb_dev_descr.bDescriptorType,
6137 		    usb_dev_descr.bcdUSB, usb_dev_descr.bDeviceClass,
6138 		    usb_dev_descr.bDeviceSubClass,
6139 		    usb_dev_descr.bDeviceProtocol,
6140 		    usb_dev_descr.bMaxPacketSize0,
6141 		    usb_dev_descr.idVendor,
6142 		    usb_dev_descr.idProduct, usb_dev_descr.bcdDevice,
6143 		    usb_dev_descr.iManufacturer, usb_dev_descr.iProduct,
6144 		    usb_dev_descr.iSerialNumber,
6145 		    usb_dev_descr.bNumConfigurations);
6146 		goto fail_cleanup;
6147 	}
6148 
6149 
6150 	/* get the device string descriptor(s) */
6151 	usba_get_dev_string_descrs(child_dip, child_ud);
6152 
6153 	/* retrieve config cloud for all configurations */
6154 	rval = hubd_get_all_device_config_cloud(hubd, child_dip, child_ud);
6155 	if (rval != USB_SUCCESS) {
6156 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6157 		    "failed to get configuration descriptor(s)");
6158 
6159 		goto fail_cleanup;
6160 	}
6161 
6162 	/* get the preferred configuration for this device */
6163 	user_conf_index = hubd_select_device_configuration(hubd, port,
6164 	    child_dip, child_ud);
6165 
6166 	/* Check if the user selected configuration index is in range */
6167 	if ((user_conf_index >= usb_dev_descr.bNumConfigurations) ||
6168 	    (user_conf_index < 0)) {
6169 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6170 		    "Configuration index for device idVendor=%d "
6171 		    "idProduct=%d is=%d, and is out of range[0..%d]",
6172 		    usb_dev_descr.idVendor, usb_dev_descr.idProduct,
6173 		    user_conf_index, usb_dev_descr.bNumConfigurations - 1);
6174 
6175 		/* treat this as user didn't specify configuration */
6176 		user_conf_index = USBA_DEV_CONFIG_INDEX_UNDEFINED;
6177 	}
6178 
6179 
6180 	/*
6181 	 * Warn users of a performance hit if connecting a
6182 	 * High Speed behind a 1.1 hub, which is behind a
6183 	 * 2.0 port.
6184 	 */
6185 	if ((parent_port_status != USBA_HIGH_SPEED_DEV) &&
6186 	    !(usba_is_root_hub(parent_ud->usb_dip)) &&
6187 	    (parent_usb_addr)) {
6188 
6189 		/*
6190 		 * Now that we know the root port is a high speed port
6191 		 * and that the parent port is not a high speed port,
6192 		 * let's find out if the device itself is a high speed
6193 		 * device.  If it is a high speed device,
6194 		 * USB_DESCR_TYPE_SETUP_DEV_QLF should return a value,
6195 		 * otherwise the command will fail.
6196 		 */
6197 		rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
6198 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
6199 		    USB_REQ_GET_DESCR,			/* bRequest */
6200 		    USB_DESCR_TYPE_SETUP_DEV_QLF,	/* wValue */
6201 		    0,					/* wIndex */
6202 		    10,					/* wLength */
6203 		    &pdata, USB_ATTRS_SHORT_XFER_OK,
6204 		    &completion_reason, &cb_flags, 0);
6205 
6206 		if (pdata) {
6207 			freemsg(pdata);
6208 			pdata = NULL;
6209 		}
6210 
6211 		/*
6212 		 * USB_DESCR_TYPE_SETUP_DEV_QLF query was successful
6213 		 * that means this is a high speed device behind a
6214 		 * high speed root hub, but running at full speed
6215 		 * because there is a full speed hub in the middle.
6216 		 */
6217 		if (rval == USB_SUCCESS) {
6218 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
6219 			    hubd->h_log_handle,
6220 			    "Connecting a high speed device to a "
6221 			    "non high speed hub (port %d) will result "
6222 			    "in a loss of performance.	Please connect "
6223 			    "the device to a high speed hub to get "
6224 			    "the maximum performance.",
6225 			    port);
6226 		}
6227 	}
6228 
6229 	/*
6230 	 * Now we try to online the device by attaching a driver
6231 	 * The following truth table illustrates the logic:-
6232 	 * Cfgndx	Driver	Action
6233 	 * 0		0	loop all configs for driver with full
6234 	 *			compatible properties.
6235 	 * 0		1	set first configuration,
6236 	 *			compatible prop = drivername.
6237 	 * 1		0	Set config, full compatible prop
6238 	 * 1		1	Set config, compatible prop = drivername.
6239 	 *
6240 	 * Note:
6241 	 *	cfgndx = user_conf_index
6242 	 *	Driver = usb_preferred_driver
6243 	 */
6244 	if (user_conf_index == USBA_DEV_CONFIG_INDEX_UNDEFINED) {
6245 		if (child_ud->usb_preferred_driver) {
6246 			/*
6247 			 * It is the job of the "preferred driver" to put the
6248 			 * device in the desired configuration. Till then
6249 			 * put the device in config index 0.
6250 			 */
6251 			if ((rval = usba_hubdi_check_power_budget(dip, child_ud,
6252 			    USB_DEV_DEFAULT_CONFIG_INDEX)) != USB_SUCCESS) {
6253 
6254 				goto fail_cleanup;
6255 			}
6256 
6257 			child_dip = hubd_ready_device(hubd, child_dip,
6258 			    child_ud, USB_DEV_DEFAULT_CONFIG_INDEX);
6259 
6260 			/*
6261 			 * Assign the dip before onlining to avoid race
6262 			 * with busctl
6263 			 */
6264 			mutex_enter(HUBD_MUTEX(hubd));
6265 			hubd->h_children_dips[port] = child_dip;
6266 			mutex_exit(HUBD_MUTEX(hubd));
6267 
6268 			(void) usba_bind_driver(child_dip);
6269 		} else {
6270 			/*
6271 			 * loop through all the configurations to see if we
6272 			 * can find a driver for any one config. If not, set
6273 			 * the device in config_index 0
6274 			 */
6275 			rval = USB_FAILURE;
6276 			for (config_index = 0;
6277 			    (config_index < usb_dev_descr.bNumConfigurations) &&
6278 			    (rval != USB_SUCCESS); config_index++) {
6279 
6280 				child_dip = hubd_ready_device(hubd, child_dip,
6281 				    child_ud, config_index);
6282 
6283 				/*
6284 				 * Assign the dip before onlining to avoid race
6285 				 * with busctl
6286 				 */
6287 				mutex_enter(HUBD_MUTEX(hubd));
6288 				hubd->h_children_dips[port] = child_dip;
6289 				mutex_exit(HUBD_MUTEX(hubd));
6290 
6291 				rval = usba_bind_driver(child_dip);
6292 
6293 				/*
6294 				 * Normally power budget should be checked
6295 				 * before device is configured. A failure in
6296 				 * power budget checking will stop the device
6297 				 * from being configured with current
6298 				 * config_index and may enable the device to
6299 				 * be configured in another configuration.
6300 				 * This may break the user experience that a
6301 				 * device which previously worked in config
6302 				 * A now works in config B after power budget
6303 				 * control is enabled. To avoid such situation,
6304 				 * power budget checking is moved here and will
6305 				 * fail the child creation directly if config
6306 				 * A exceeds the power available.
6307 				 */
6308 				if (rval == USB_SUCCESS) {
6309 					if ((usba_hubdi_check_power_budget(dip,
6310 					    child_ud, config_index)) !=
6311 					    USB_SUCCESS) {
6312 
6313 						goto fail_cleanup;
6314 					}
6315 				}
6316 			}
6317 			if (rval != USB_SUCCESS) {
6318 
6319 				if ((usba_hubdi_check_power_budget(dip,
6320 				    child_ud, 0)) != USB_SUCCESS) {
6321 
6322 					goto fail_cleanup;
6323 				}
6324 
6325 				child_dip = hubd_ready_device(hubd, child_dip,
6326 				    child_ud, 0);
6327 				mutex_enter(HUBD_MUTEX(hubd));
6328 				hubd->h_children_dips[port] = child_dip;
6329 				mutex_exit(HUBD_MUTEX(hubd));
6330 			}
6331 		} /* end else loop all configs */
6332 	} else {
6333 
6334 		if ((usba_hubdi_check_power_budget(dip, child_ud,
6335 		    (uint_t)user_conf_index)) != USB_SUCCESS) {
6336 
6337 			goto fail_cleanup;
6338 		}
6339 
6340 		child_dip = hubd_ready_device(hubd, child_dip,
6341 		    child_ud, (uint_t)user_conf_index);
6342 
6343 		/*
6344 		 * Assign the dip before onlining to avoid race
6345 		 * with busctl
6346 		 */
6347 		mutex_enter(HUBD_MUTEX(hubd));
6348 		hubd->h_children_dips[port] = child_dip;
6349 		mutex_exit(HUBD_MUTEX(hubd));
6350 
6351 		(void) usba_bind_driver(child_dip);
6352 	}
6353 
6354 	usba_hubdi_decr_power_budget(dip, child_ud);
6355 
6356 	mutex_enter(HUBD_MUTEX(hubd));
6357 	if (hubd->h_usba_devices[port] == NULL) {
6358 		hubd->h_usba_devices[port] = usba_get_usba_device(child_dip);
6359 	} else {
6360 		ASSERT(hubd->h_usba_devices[port] ==
6361 		    usba_get_usba_device(child_dip));
6362 	}
6363 
6364 	return (USB_SUCCESS);
6365 
6366 
6367 fail_cleanup:
6368 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6369 	    "hubd_create_child: fail_cleanup");
6370 
6371 	mutex_enter(HUBD_MUTEX(hubd));
6372 	hubd->h_children_dips[port] = NULL;
6373 	mutex_exit(HUBD_MUTEX(hubd));
6374 
6375 	if (pdata) {
6376 		freemsg(pdata);
6377 	}
6378 
6379 	if (ph) {
6380 		usb_pipe_close(child_dip, ph,
6381 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
6382 	}
6383 
6384 	if (child_dip) {
6385 		int rval = usba_destroy_child_devi(child_dip,
6386 		    NDI_DEVI_REMOVE);
6387 		if (rval != USB_SUCCESS) {
6388 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6389 			    "failure to remove child node");
6390 		}
6391 	}
6392 
6393 	if (child_ud) {
6394 		/* to make sure we free the address */
6395 		mutex_enter(&child_ud->usb_mutex);
6396 		child_ud->usb_addr = address;
6397 		ASSERT(child_ud->usb_ref_count == 0);
6398 		mutex_exit(&child_ud->usb_mutex);
6399 
6400 		mutex_enter(HUBD_MUTEX(hubd));
6401 		if (hubd->h_usba_devices[port] == NULL) {
6402 			mutex_exit(HUBD_MUTEX(hubd));
6403 			usba_free_usba_device(child_ud);
6404 		} else {
6405 			hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
6406 			mutex_exit(HUBD_MUTEX(hubd));
6407 		}
6408 	}
6409 
6410 	mutex_enter(HUBD_MUTEX(hubd));
6411 
6412 	return (USB_FAILURE);
6413 }
6414 
6415 
6416 /*
6417  * hubd_delete_child:
6418  *	- free usb address
6419  *	- lookup child dips, there may be multiple on this port
6420  *	- offline each child devi
6421  */
6422 static int
6423 hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, boolean_t retry)
6424 {
6425 	dev_info_t	*child_dip;
6426 	usba_device_t	*usba_device;
6427 	int		rval = USB_SUCCESS;
6428 
6429 	child_dip = hubd->h_children_dips[port];
6430 	usba_device = hubd->h_usba_devices[port];
6431 
6432 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6433 	    "hubd_delete_child: port=%d, dip=0x%p usba_device=0x%p",
6434 	    port, (void *)child_dip, (void *)usba_device);
6435 
6436 	mutex_exit(HUBD_MUTEX(hubd));
6437 	if (child_dip) {
6438 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6439 		    "hubd_delete_child:\n\t"
6440 		    "dip = 0x%p (%s) at port %d",
6441 		    (void *)child_dip, ddi_node_name(child_dip), port);
6442 
6443 		if (usba_device) {
6444 			usba_hubdi_incr_power_budget(hubd->h_dip, usba_device);
6445 		}
6446 
6447 		rval = usba_destroy_child_devi(child_dip, flag);
6448 
6449 		if ((rval != USB_SUCCESS) && usba_is_hwa(child_dip)) {
6450 			/*
6451 			 * This is only useful for HWA device node.
6452 			 * Since hwahc interface must hold hwarc interface
6453 			 * open until hwahc is detached, the first call to
6454 			 * ndi_devi_unconfig_one() can only offline hwahc
6455 			 * driver but not hwarc driver. Need to make a second
6456 			 * call to ndi_devi_unconfig_one() to make the hwarc
6457 			 * driver detach.
6458 			 */
6459 			rval = usba_destroy_child_devi(child_dip, flag);
6460 		}
6461 
6462 		if ((rval == USB_SUCCESS) && (flag & NDI_DEVI_REMOVE)) {
6463 			/*
6464 			 * if the child was still < DS_INITIALIZED
6465 			 * then our bus_unconfig was not called and
6466 			 * we have to zap the child here
6467 			 */
6468 			mutex_enter(HUBD_MUTEX(hubd));
6469 			if (hubd->h_children_dips[port] == child_dip) {
6470 				usba_device_t *ud =
6471 				    hubd->h_usba_devices[port];
6472 					hubd->h_children_dips[port] = NULL;
6473 				if (ud) {
6474 					mutex_exit(HUBD_MUTEX(hubd));
6475 
6476 					mutex_enter(&ud->usb_mutex);
6477 					ud->usb_ref_count = 0;
6478 					mutex_exit(&ud->usb_mutex);
6479 
6480 					usba_free_usba_device(ud);
6481 					mutex_enter(HUBD_MUTEX(hubd));
6482 					hubd->h_usba_devices[port] = NULL;
6483 				}
6484 			}
6485 			mutex_exit(HUBD_MUTEX(hubd));
6486 		}
6487 	}
6488 
6489 	if ((rval != USB_SUCCESS) && retry) {
6490 
6491 		hubd_schedule_cleanup(usba_device->usb_root_hub_dip);
6492 	}
6493 	mutex_enter(HUBD_MUTEX(hubd));
6494 
6495 	return (rval);
6496 }
6497 
6498 
6499 /*
6500  * hubd_free_usba_device:
6501  *	free usb device structure unless it is associated with
6502  *	the root hub which is handled differently
6503  */
6504 static void
6505 hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device)
6506 {
6507 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6508 	    "hubd_free_usba_device: hubd=0x%p, usba_device=0x%p",
6509 	    (void *)hubd, (void *)usba_device);
6510 
6511 	if (usba_device && (usba_device->usb_addr != ROOT_HUB_ADDR)) {
6512 		usb_port_t port = usba_device->usb_port;
6513 		dev_info_t *dip = hubd->h_children_dips[port];
6514 
6515 #ifdef DEBUG
6516 		if (dip) {
6517 			ASSERT(i_ddi_node_state(dip) < DS_INITIALIZED);
6518 		}
6519 #endif
6520 
6521 		port = usba_device->usb_port;
6522 		hubd->h_usba_devices[port] = NULL;
6523 
6524 		mutex_exit(HUBD_MUTEX(hubd));
6525 		usba_free_usba_device(usba_device);
6526 		mutex_enter(HUBD_MUTEX(hubd));
6527 	}
6528 }
6529 
6530 
6531 /*
6532  * event support
6533  *
6534  * busctl event support
6535  */
6536 static int
6537 hubd_busop_get_eventcookie(dev_info_t *dip,
6538 	dev_info_t	*rdip,
6539 	char		*eventname,
6540 	ddi_eventcookie_t *cookie)
6541 {
6542 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
6543 
6544 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6545 	    "hubd_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
6546 	    "event=%s", (void *)dip, (void *)rdip, eventname);
6547 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6548 	    "(dip=%s%d, rdip=%s%d)",
6549 	    ddi_driver_name(dip), ddi_get_instance(dip),
6550 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
6551 
6552 	/* return event cookie, iblock cookie, and level */
6553 	return (ndi_event_retrieve_cookie(hubd->h_ndi_event_hdl,
6554 	    rdip, eventname, cookie, NDI_EVENT_NOPASS));
6555 }
6556 
6557 
6558 static int
6559 hubd_busop_add_eventcall(dev_info_t *dip,
6560 	dev_info_t	*rdip,
6561 	ddi_eventcookie_t cookie,
6562 	void		(*callback)(dev_info_t *dip,
6563 			ddi_eventcookie_t cookie, void *arg,
6564 			void *bus_impldata),
6565 	void *arg, ddi_callback_id_t *cb_id)
6566 {
6567 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
6568 	usb_port_t port = hubd_child_dip2port(hubd, rdip);
6569 
6570 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6571 	    "hubd_busop_add_eventcall: dip=0x%p, rdip=0x%p "
6572 	    "cookie=0x%p, cb=0x%p, arg=0x%p",
6573 	    (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
6574 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6575 	    "(dip=%s%d, rdip=%s%d, event=%s)",
6576 	    ddi_driver_name(dip), ddi_get_instance(dip),
6577 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
6578 	    ndi_event_cookie_to_name(hubd->h_ndi_event_hdl, cookie));
6579 
6580 	/* Set flag on children registering events */
6581 	switch (ndi_event_cookie_to_tag(hubd->h_ndi_event_hdl, cookie)) {
6582 	case USBA_EVENT_TAG_HOT_REMOVAL:
6583 		mutex_enter(HUBD_MUTEX(hubd));
6584 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
6585 		mutex_exit(HUBD_MUTEX(hubd));
6586 
6587 		break;
6588 	case USBA_EVENT_TAG_PRE_SUSPEND:
6589 		mutex_enter(HUBD_MUTEX(hubd));
6590 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
6591 		mutex_exit(HUBD_MUTEX(hubd));
6592 
6593 		break;
6594 	default:
6595 
6596 		break;
6597 	}
6598 
6599 	/* add callback to our event set */
6600 	return (ndi_event_add_callback(hubd->h_ndi_event_hdl,
6601 	    rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
6602 }
6603 
6604 
6605 static int
6606 hubd_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
6607 {
6608 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
6609 	ndi_event_callbacks_t *id = (ndi_event_callbacks_t *)cb_id;
6610 
6611 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6612 	    "hubd_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
6613 	    "cookie=0x%p", (void *)dip, (void *)id->ndi_evtcb_dip,
6614 	    (void *)id->ndi_evtcb_cookie);
6615 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6616 	    "(dip=%s%d, rdip=%s%d, event=%s)",
6617 	    ddi_driver_name(dip), ddi_get_instance(dip),
6618 	    ddi_driver_name(id->ndi_evtcb_dip),
6619 	    ddi_get_instance(id->ndi_evtcb_dip),
6620 	    ndi_event_cookie_to_name(hubd->h_ndi_event_hdl,
6621 	    id->ndi_evtcb_cookie));
6622 
6623 	/* remove event registration from our event set */
6624 	return (ndi_event_remove_callback(hubd->h_ndi_event_hdl, cb_id));
6625 }
6626 
6627 
6628 /*
6629  * event distribution
6630  *
6631  * hubd_do_callback:
6632  *	Post this event to the specified child
6633  */
6634 static void
6635 hubd_do_callback(hubd_t *hubd, dev_info_t *cdip, ddi_eventcookie_t cookie)
6636 {
6637 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6638 	    "hubd_do_callback");
6639 
6640 	(void) ndi_event_do_callback(hubd->h_ndi_event_hdl, cdip, cookie, NULL);
6641 }
6642 
6643 
6644 /*
6645  * hubd_run_callbacks:
6646  *	Send this event to all children
6647  */
6648 static void
6649 hubd_run_callbacks(hubd_t *hubd, usba_event_t type)
6650 {
6651 	usb_port_t	port;
6652 
6653 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6654 	    "hubd_run_callbacks");
6655 
6656 	mutex_enter(HUBD_MUTEX(hubd));
6657 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
6658 		/*
6659 		 * the childen_dips list may have dips that have been
6660 		 * already deallocated. we only get a post_detach notification
6661 		 * but not a destroy notification
6662 		 */
6663 		if (hubd->h_children_dips[port]) {
6664 			mutex_exit(HUBD_MUTEX(hubd));
6665 			hubd_post_event(hubd, port, type);
6666 			mutex_enter(HUBD_MUTEX(hubd));
6667 		}
6668 	}
6669 	mutex_exit(HUBD_MUTEX(hubd));
6670 }
6671 
6672 
6673 /*
6674  * hubd_post_event
6675  *	post event to a child on the port depending on the type
6676  */
6677 static void
6678 hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type)
6679 {
6680 	int	rval;
6681 	dev_info_t	*dip;
6682 	usba_device_t	*usba_device;
6683 	ddi_eventcookie_t cookie, rm_cookie, suspend_cookie;
6684 
6685 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6686 	    "hubd_post_event: port=%d event=%s", port,
6687 	    ndi_event_tag_to_name(hubd->h_ndi_event_hdl, type));
6688 
6689 	cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, type);
6690 	rm_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
6691 	    USBA_EVENT_TAG_HOT_REMOVAL);
6692 	suspend_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
6693 	    USBA_EVENT_TAG_PRE_SUSPEND);
6694 
6695 	/*
6696 	 * Hotplug daemon may be attaching a driver that may be registering
6697 	 * event callbacks. So it already has got the device tree lock and
6698 	 * event handle mutex. So to prevent a deadlock while posting events,
6699 	 * we grab and release the locks in the same order.
6700 	 */
6701 	mutex_enter(HUBD_MUTEX(hubd));
6702 	dip = hubd->h_children_dips[port];
6703 	usba_device = hubd->h_usba_devices[port];
6704 	mutex_exit(HUBD_MUTEX(hubd));
6705 
6706 	switch (type) {
6707 	case USBA_EVENT_TAG_HOT_REMOVAL:
6708 		/* Clear the registered event flag */
6709 		mutex_enter(HUBD_MUTEX(hubd));
6710 		hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_DISCONNECT;
6711 		mutex_exit(HUBD_MUTEX(hubd));
6712 
6713 		hubd_do_callback(hubd, dip, cookie);
6714 		usba_persistent_pipe_close(usba_device);
6715 
6716 		/*
6717 		 * Mark the dip for deletion only after the driver has
6718 		 * seen the disconnect event to prevent cleanup thread
6719 		 * from stepping in between.
6720 		 */
6721 		mutex_enter(&(DEVI(dip)->devi_lock));
6722 		DEVI_SET_DEVICE_REMOVED(dip);
6723 		mutex_exit(&(DEVI(dip)->devi_lock));
6724 
6725 		break;
6726 	case USBA_EVENT_TAG_PRE_SUSPEND:
6727 		mutex_enter(HUBD_MUTEX(hubd));
6728 		hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_PRESUSPEND;
6729 		mutex_exit(HUBD_MUTEX(hubd));
6730 
6731 		hubd_do_callback(hubd, dip, cookie);
6732 		/*
6733 		 * persistent pipe close for this event is taken care by the
6734 		 * caller after verfying that all children can suspend
6735 		 */
6736 
6737 		break;
6738 	case USBA_EVENT_TAG_HOT_INSERTION:
6739 		/*
6740 		 * Check if this child has missed the disconnect event before
6741 		 * it registered for event callbacks
6742 		 */
6743 		mutex_enter(HUBD_MUTEX(hubd));
6744 		if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_DISCONNECT) {
6745 			/* clear the flag and post disconnect event */
6746 			hubd->h_child_events[port] &=
6747 			    ~HUBD_CHILD_EVENT_DISCONNECT;
6748 			mutex_exit(HUBD_MUTEX(hubd));
6749 			hubd_do_callback(hubd, dip, rm_cookie);
6750 			usba_persistent_pipe_close(usba_device);
6751 			mutex_enter(HUBD_MUTEX(hubd));
6752 		}
6753 		mutex_exit(HUBD_MUTEX(hubd));
6754 
6755 		/*
6756 		 * Mark the dip as reinserted to prevent cleanup thread
6757 		 * from stepping in.
6758 		 */
6759 		mutex_enter(&(DEVI(dip)->devi_lock));
6760 		DEVI_SET_DEVICE_REINSERTED(dip);
6761 		mutex_exit(&(DEVI(dip)->devi_lock));
6762 
6763 		rval = usba_persistent_pipe_open(usba_device);
6764 		if (rval != USB_SUCCESS) {
6765 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
6766 			    hubd->h_log_handle,
6767 			    "failed to reopen all pipes on reconnect");
6768 		}
6769 
6770 		hubd_do_callback(hubd, dip, cookie);
6771 
6772 		/*
6773 		 * We might see a connect event only if hotplug thread for
6774 		 * disconnect event don't run in time.
6775 		 * Set the flag again, so we don't miss posting a
6776 		 * disconnect event.
6777 		 */
6778 		mutex_enter(HUBD_MUTEX(hubd));
6779 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
6780 		mutex_exit(HUBD_MUTEX(hubd));
6781 
6782 		break;
6783 	case USBA_EVENT_TAG_POST_RESUME:
6784 		/*
6785 		 * Check if this child has missed the pre-suspend event before
6786 		 * it registered for event callbacks
6787 		 */
6788 		mutex_enter(HUBD_MUTEX(hubd));
6789 		if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_PRESUSPEND) {
6790 			/* clear the flag and post pre_suspend event */
6791 			hubd->h_port_state[port] &=
6792 			    ~HUBD_CHILD_EVENT_PRESUSPEND;
6793 			mutex_exit(HUBD_MUTEX(hubd));
6794 			hubd_do_callback(hubd, dip, suspend_cookie);
6795 			mutex_enter(HUBD_MUTEX(hubd));
6796 		}
6797 		mutex_exit(HUBD_MUTEX(hubd));
6798 
6799 		mutex_enter(&usba_device->usb_mutex);
6800 		usba_device->usb_no_cpr = 0;
6801 		mutex_exit(&usba_device->usb_mutex);
6802 
6803 		/*
6804 		 * Since the pipe has already been opened by hub
6805 		 * at DDI_RESUME time, there is no need for a
6806 		 * persistent pipe open
6807 		 */
6808 		hubd_do_callback(hubd, dip, cookie);
6809 
6810 		/*
6811 		 * Set the flag again, so we don't miss posting a
6812 		 * pre-suspend event. This enforces a tighter
6813 		 * dev_state model.
6814 		 */
6815 		mutex_enter(HUBD_MUTEX(hubd));
6816 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
6817 		mutex_exit(HUBD_MUTEX(hubd));
6818 		break;
6819 	}
6820 }
6821 
6822 
6823 /*
6824  * handling of events coming from above
6825  */
6826 static int
6827 hubd_disconnect_event_cb(dev_info_t *dip)
6828 {
6829 	hubd_t		*hubd = (hubd_t *)hubd_get_soft_state(dip);
6830 	usb_port_t	port, nports;
6831 	usba_device_t	*usba_dev;
6832 	usba_event_t	tag = USBA_EVENT_TAG_HOT_REMOVAL;
6833 	int		circ;
6834 
6835 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6836 	    "hubd_disconnect_event_cb: tag=%d", tag);
6837 
6838 	ndi_devi_enter(dip, &circ);
6839 
6840 	mutex_enter(HUBD_MUTEX(hubd));
6841 	switch (hubd->h_dev_state) {
6842 	case USB_DEV_ONLINE:
6843 	case USB_DEV_PWRED_DOWN:
6844 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
6845 		/* stop polling on the interrupt pipe */
6846 		hubd_stop_polling(hubd);
6847 
6848 		/* FALLTHROUGH */
6849 	case USB_DEV_SUSPENDED:
6850 		/* we remain in this state */
6851 		mutex_exit(HUBD_MUTEX(hubd));
6852 		hubd_run_callbacks(hubd, tag);
6853 		mutex_enter(HUBD_MUTEX(hubd));
6854 
6855 		/* close all the open pipes of our children */
6856 		nports = hubd->h_hub_descr.bNbrPorts;
6857 		for (port = 1; port <= nports; port++) {
6858 			usba_dev = hubd->h_usba_devices[port];
6859 			if (usba_dev != NULL) {
6860 				mutex_exit(HUBD_MUTEX(hubd));
6861 				usba_persistent_pipe_close(usba_dev);
6862 				mutex_enter(HUBD_MUTEX(hubd));
6863 			}
6864 		}
6865 
6866 		break;
6867 	case USB_DEV_DISCONNECTED:
6868 		/* avoid passing multiple disconnects to children */
6869 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6870 		    "hubd_disconnect_event_cb: Already disconnected");
6871 
6872 		break;
6873 	default:
6874 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6875 		    "hubd_disconnect_event_cb: Illegal devstate=%d",
6876 		    hubd->h_dev_state);
6877 
6878 		break;
6879 	}
6880 	mutex_exit(HUBD_MUTEX(hubd));
6881 
6882 	ndi_devi_exit(dip, circ);
6883 
6884 	return (USB_SUCCESS);
6885 }
6886 
6887 
6888 static int
6889 hubd_reconnect_event_cb(dev_info_t *dip)
6890 {
6891 	int	rval, circ;
6892 
6893 	ndi_devi_enter(dip, &circ);
6894 	rval = hubd_restore_state_cb(dip);
6895 	ndi_devi_exit(dip, circ);
6896 
6897 	return (rval);
6898 }
6899 
6900 
6901 /*
6902  * hubd_pre_suspend_event_cb
6903  *	propogate event for binary compatibility of old drivers
6904  */
6905 static int
6906 hubd_pre_suspend_event_cb(dev_info_t *dip)
6907 {
6908 	int	circ;
6909 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
6910 
6911 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
6912 	    "hubd_pre_suspend_event_cb");
6913 
6914 	/* disable hotplug thread */
6915 	mutex_enter(HUBD_MUTEX(hubd));
6916 	hubd->h_hotplug_thread++;
6917 	hubd_stop_polling(hubd);
6918 
6919 	/* keep PM out till we see a cpr resume */
6920 	(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
6921 	mutex_exit(HUBD_MUTEX(hubd));
6922 
6923 	ndi_devi_enter(dip, &circ);
6924 	hubd_run_callbacks(hubd, USBA_EVENT_TAG_PRE_SUSPEND);
6925 	ndi_devi_exit(dip, circ);
6926 
6927 	return (USB_SUCCESS);
6928 }
6929 
6930 
6931 /*
6932  * hubd_post_resume_event_cb
6933  *	propogate event for binary compatibility of old drivers
6934  */
6935 static int
6936 hubd_post_resume_event_cb(dev_info_t *dip)
6937 {
6938 	int	circ;
6939 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
6940 
6941 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
6942 	    "hubd_post_resume_event_cb");
6943 
6944 	ndi_devi_enter(dip, &circ);
6945 	hubd_run_callbacks(hubd, USBA_EVENT_TAG_POST_RESUME);
6946 	ndi_devi_exit(dip, circ);
6947 
6948 	mutex_enter(HUBD_MUTEX(hubd));
6949 
6950 	/* enable PM */
6951 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
6952 
6953 	/* allow hotplug thread */
6954 	hubd->h_hotplug_thread--;
6955 
6956 	/* start polling */
6957 	hubd_start_polling(hubd, 0);
6958 	mutex_exit(HUBD_MUTEX(hubd));
6959 
6960 	return (USB_SUCCESS);
6961 }
6962 
6963 
6964 /*
6965  * hubd_cpr_suspend
6966  *	save the current state of the driver/device
6967  */
6968 static int
6969 hubd_cpr_suspend(hubd_t *hubd)
6970 {
6971 	usb_port_t	port, nports;
6972 	usba_device_t	*usba_dev;
6973 	uchar_t		no_cpr = 0;
6974 	int		rval = USB_FAILURE;
6975 
6976 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6977 	    "hubd_cpr_suspend: Begin");
6978 
6979 	/* Make sure device is powered up to save state. */
6980 	mutex_enter(HUBD_MUTEX(hubd));
6981 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
6982 	mutex_exit(HUBD_MUTEX(hubd));
6983 
6984 	/* bring the device to full power */
6985 	(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
6986 	mutex_enter(HUBD_MUTEX(hubd));
6987 
6988 	switch (hubd->h_dev_state) {
6989 	case USB_DEV_ONLINE:
6990 	case USB_DEV_PWRED_DOWN:
6991 	case USB_DEV_DISCONNECTED:
6992 		/* find out if all our children have been quiesced */
6993 		nports = hubd->h_hub_descr.bNbrPorts;
6994 		for (port = 1; (no_cpr == 0) && (port <= nports); port++) {
6995 			usba_dev = hubd->h_usba_devices[port];
6996 			if (usba_dev != NULL) {
6997 				mutex_enter(&usba_dev->usb_mutex);
6998 				no_cpr += usba_dev->usb_no_cpr;
6999 				mutex_exit(&usba_dev->usb_mutex);
7000 			}
7001 		}
7002 		if (no_cpr > 0) {
7003 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
7004 			    "Children busy - can't checkpoint");
7005 			/* remain in same state to fail checkpoint */
7006 
7007 			break;
7008 		} else {
7009 			/*
7010 			 * do not suspend if our hotplug thread
7011 			 * or the deathrow thread is active
7012 			 */
7013 			if ((hubd->h_hotplug_thread > 1) ||
7014 			    (hubd->h_cleanup_active == B_TRUE)) {
7015 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
7016 				    hubd->h_log_handle,
7017 				    "hotplug thread active  - can't cpr");
7018 				/* remain in same state to fail checkpoint */
7019 
7020 				break;
7021 			}
7022 
7023 			/* quiesce ourselves now */
7024 			hubd->h_dev_state = USB_DEV_SUSPENDED;
7025 			hubd_stop_polling(hubd);
7026 
7027 			/* close all the open pipes of our children */
7028 			for (port = 1; port <= nports; port++) {
7029 				usba_dev = hubd->h_usba_devices[port];
7030 				if (usba_dev != NULL) {
7031 					mutex_exit(HUBD_MUTEX(hubd));
7032 					usba_persistent_pipe_close(usba_dev);
7033 					mutex_enter(HUBD_MUTEX(hubd));
7034 				}
7035 			}
7036 			/*
7037 			 * turn off power to all the ports so that we
7038 			 * don't see any spurious activity
7039 			 */
7040 			(void) hubd_disable_all_port_power(hubd);
7041 
7042 			/*
7043 			 * if we are the root hub, we close our pipes
7044 			 * ourselves.
7045 			 */
7046 			if (usba_is_root_hub(hubd->h_dip)) {
7047 				mutex_exit(HUBD_MUTEX(hubd));
7048 				usba_persistent_pipe_close(
7049 				    usba_get_usba_device(hubd->h_dip));
7050 				mutex_enter(HUBD_MUTEX(hubd));
7051 			}
7052 			rval = USB_SUCCESS;
7053 
7054 			break;
7055 		}
7056 	case USB_DEV_SUSPENDED:
7057 	default:
7058 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
7059 		    "hubd_cpr_suspend: Illegal dev state=%d",
7060 		    hubd->h_dev_state);
7061 
7062 		break;
7063 	}
7064 
7065 	hubd_pm_idle_component(hubd, hubd->h_dip, 0);
7066 	mutex_exit(HUBD_MUTEX(hubd));
7067 
7068 	return (rval);
7069 }
7070 
7071 static void
7072 hubd_cpr_resume(dev_info_t *dip)
7073 {
7074 	int	rval, circ;
7075 
7076 	ndi_devi_enter(dip, &circ);
7077 	/*
7078 	 * if we are the root hub, we open our pipes
7079 	 * ourselves.
7080 	 */
7081 	if (usba_is_root_hub(dip)) {
7082 		rval = usba_persistent_pipe_open(
7083 		    usba_get_usba_device(dip));
7084 		ASSERT(rval == USB_SUCCESS);
7085 	}
7086 	(void) hubd_restore_state_cb(dip);
7087 	ndi_devi_exit(dip, circ);
7088 }
7089 
7090 
7091 /*
7092  * hubd_restore_state_cb
7093  *	Event callback to restore device state
7094  */
7095 static int
7096 hubd_restore_state_cb(dev_info_t *dip)
7097 {
7098 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
7099 
7100 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
7101 	    "hubd_restore_state_cb: Begin");
7102 
7103 	/* restore the state of this device */
7104 	hubd_restore_device_state(dip, hubd);
7105 
7106 	return (USB_SUCCESS);
7107 }
7108 
7109 
7110 /*
7111  * registering for events
7112  */
7113 static int
7114 hubd_register_events(hubd_t *hubd)
7115 {
7116 	int		rval = USB_SUCCESS;
7117 
7118 	if (usba_is_root_hub(hubd->h_dip)) {
7119 		hubd_register_cpr_callback(hubd);
7120 	} else {
7121 		rval = usb_register_event_cbs(hubd->h_dip, &hubd_events, 0);
7122 	}
7123 
7124 	return (rval);
7125 }
7126 
7127 
7128 /*
7129  * hubd cpr callback related functions
7130  *
7131  * hubd_cpr_post_user_callb:
7132  *	This function is called during checkpoint & resume -
7133  *		1. after user threads are stopped during checkpoint
7134  *		2. after kernel threads are resumed during resume
7135  */
7136 /* ARGSUSED */
7137 static boolean_t
7138 hubd_cpr_post_user_callb(void *arg, int code)
7139 {
7140 	hubd_cpr_t	*cpr_cb = (hubd_cpr_t *)arg;
7141 	hubd_t		*hubd = cpr_cb->statep;
7142 	int		retry = 0;
7143 
7144 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7145 	    "hubd_cpr_post_user_callb");
7146 
7147 	switch (code) {
7148 	case CB_CODE_CPR_CHKPT:
7149 		USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7150 		    "hubd_cpr_post_user_callb: CB_CODE_CPR_CHKPT");
7151 
7152 		mutex_enter(HUBD_MUTEX(hubd));
7153 
7154 		/* turn off deathrow thread */
7155 		hubd->h_cleanup_enabled = B_FALSE;
7156 
7157 		/* give up if deathrow thread doesn't exit */
7158 		while ((hubd->h_cleanup_active == B_TRUE) && (retry++ < 3)) {
7159 			mutex_exit(HUBD_MUTEX(hubd));
7160 			delay(drv_usectohz(hubd_dip_cleanup_delay));
7161 
7162 			USB_DPRINTF_L2(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7163 			    "hubd_cpr_post_user_callb, waiting for "
7164 			    "deathrow thread to exit");
7165 			mutex_enter(HUBD_MUTEX(hubd));
7166 		}
7167 
7168 		mutex_exit(HUBD_MUTEX(hubd));
7169 
7170 		/* save the state of the device */
7171 		(void) hubd_pre_suspend_event_cb(hubd->h_dip);
7172 
7173 		return (B_TRUE);
7174 	case CB_CODE_CPR_RESUME:
7175 		USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7176 		    "hubd_cpr_post_user_callb: CB_CODE_CPR_RESUME");
7177 
7178 		/* restore the state of the device */
7179 		(void) hubd_post_resume_event_cb(hubd->h_dip);
7180 
7181 		/* turn on deathrow thread */
7182 		mutex_enter(HUBD_MUTEX(hubd));
7183 		hubd->h_cleanup_enabled = B_TRUE;
7184 		mutex_exit(HUBD_MUTEX(hubd));
7185 
7186 		hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
7187 
7188 		return (B_TRUE);
7189 	default:
7190 
7191 		return (B_FALSE);
7192 	}
7193 
7194 }
7195 
7196 
7197 /* register callback with cpr framework */
7198 void
7199 hubd_register_cpr_callback(hubd_t *hubd)
7200 {
7201 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7202 	    "hubd_register_cpr_callback");
7203 
7204 	mutex_enter(HUBD_MUTEX(hubd));
7205 	hubd->h_cpr_cb =
7206 	    (hubd_cpr_t *)kmem_zalloc(sizeof (hubd_cpr_t), KM_SLEEP);
7207 	mutex_exit(HUBD_MUTEX(hubd));
7208 	mutex_init(&hubd->h_cpr_cb->lockp, NULL, MUTEX_DRIVER,
7209 	    hubd->h_dev_data->dev_iblock_cookie);
7210 	hubd->h_cpr_cb->statep = hubd;
7211 	hubd->h_cpr_cb->cpr.cc_lockp = &hubd->h_cpr_cb->lockp;
7212 	hubd->h_cpr_cb->cpr.cc_id = callb_add(hubd_cpr_post_user_callb,
7213 	    (void *)hubd->h_cpr_cb, CB_CL_CPR_POST_USER, "hubd");
7214 }
7215 
7216 
7217 /* unregister callback with cpr framework */
7218 void
7219 hubd_unregister_cpr_callback(hubd_t *hubd)
7220 {
7221 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
7222 	    "hubd_unregister_cpr_callback");
7223 
7224 	if (hubd->h_cpr_cb) {
7225 		(void) callb_delete(hubd->h_cpr_cb->cpr.cc_id);
7226 		mutex_destroy(&hubd->h_cpr_cb->lockp);
7227 		mutex_enter(HUBD_MUTEX(hubd));
7228 		kmem_free(hubd->h_cpr_cb, sizeof (hubd_cpr_t));
7229 		mutex_exit(HUBD_MUTEX(hubd));
7230 	}
7231 }
7232 
7233 
7234 /*
7235  * Power management
7236  *
7237  * create the pm components required for power management
7238  */
7239 static void
7240 hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd)
7241 {
7242 	hub_power_t	*hubpm;
7243 
7244 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
7245 	    "hubd_create_pm_components: Begin");
7246 
7247 	/* Allocate the state structure */
7248 	hubpm = kmem_zalloc(sizeof (hub_power_t), KM_SLEEP);
7249 
7250 	hubd->h_hubpm = hubpm;
7251 	hubpm->hubp_hubd = hubd;
7252 	hubpm->hubp_pm_capabilities = 0;
7253 	hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
7254 	hubpm->hubp_time_at_full_power = ddi_get_time();
7255 	hubpm->hubp_min_pm_threshold = hubdi_min_pm_threshold;
7256 
7257 	/* alloc memory to save power states of children */
7258 	hubpm->hubp_child_pwrstate = (uint8_t *)
7259 	    kmem_zalloc(MAX_PORTS + 1, KM_SLEEP);
7260 
7261 	/*
7262 	 * if the enable remote wakeup fails
7263 	 * we still want to enable
7264 	 * parent notification so we can PM the children
7265 	 */
7266 	usb_enable_parent_notification(dip);
7267 
7268 	if (usb_handle_remote_wakeup(dip,
7269 	    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
7270 		uint_t		pwr_states;
7271 
7272 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
7273 		    "hubd_create_pm_components: "
7274 		    "Remote Wakeup Enabled");
7275 
7276 		if (usb_create_pm_components(dip, &pwr_states) ==
7277 		    USB_SUCCESS) {
7278 			mutex_enter(HUBD_MUTEX(hubd));
7279 			hubpm->hubp_wakeup_enabled = 1;
7280 			hubpm->hubp_pwr_states = (uint8_t)pwr_states;
7281 
7282 			/* we are busy now till end of the attach */
7283 			hubd_pm_busy_component(hubd, dip, 0);
7284 			mutex_exit(HUBD_MUTEX(hubd));
7285 
7286 			/* bring the device to full power */
7287 			(void) pm_raise_power(dip, 0,
7288 			    USB_DEV_OS_FULL_PWR);
7289 		}
7290 	}
7291 
7292 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
7293 	    "hubd_create_pm_components: END");
7294 }
7295 
7296 
7297 /*
7298  * Attachment point management
7299  */
7300 /* ARGSUSED */
7301 int
7302 usba_hubdi_open(dev_info_t *dip, dev_t *devp, int flags, int otyp,
7303 	cred_t *credp)
7304 {
7305 	hubd_t *hubd;
7306 
7307 	if (otyp != OTYP_CHR)
7308 		return (EINVAL);
7309 
7310 	hubd = hubd_get_soft_state(dip);
7311 	if (hubd == NULL) {
7312 		return (ENXIO);
7313 	}
7314 
7315 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7316 	    "hubd_open:");
7317 
7318 	mutex_enter(HUBD_MUTEX(hubd));
7319 	if ((flags & FEXCL) && (hubd->h_softstate & HUBD_SS_ISOPEN)) {
7320 		mutex_exit(HUBD_MUTEX(hubd));
7321 
7322 		return (EBUSY);
7323 	}
7324 
7325 	hubd->h_softstate |= HUBD_SS_ISOPEN;
7326 	mutex_exit(HUBD_MUTEX(hubd));
7327 
7328 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "opened");
7329 
7330 	return (0);
7331 }
7332 
7333 
7334 /* ARGSUSED */
7335 int
7336 usba_hubdi_close(dev_info_t *dip, dev_t dev, int flag, int otyp,
7337 	cred_t *credp)
7338 {
7339 	hubd_t *hubd;
7340 
7341 	if (otyp != OTYP_CHR) {
7342 		return (EINVAL);
7343 	}
7344 
7345 	hubd = hubd_get_soft_state(dip);
7346 
7347 	if (hubd == NULL) {
7348 		return (ENXIO);
7349 	}
7350 
7351 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "hubd_close:");
7352 
7353 	mutex_enter(HUBD_MUTEX(hubd));
7354 	hubd->h_softstate &= ~HUBD_SS_ISOPEN;
7355 	mutex_exit(HUBD_MUTEX(hubd));
7356 
7357 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "closed");
7358 
7359 	return (0);
7360 }
7361 
7362 
7363 /*
7364  * hubd_ioctl: cfgadm controls
7365  */
7366 /* ARGSUSED */
7367 int
7368 usba_hubdi_ioctl(dev_info_t *self, dev_t dev, int cmd, intptr_t arg,
7369 	int mode, cred_t *credp, int *rvalp)
7370 {
7371 	int			rv = 0;
7372 	char			*msg;	/* for messages */
7373 	hubd_t			*hubd;
7374 	usb_port_t		port = 0;
7375 	dev_info_t		*child_dip = NULL;
7376 	dev_info_t		*rh_dip;
7377 	devctl_ap_state_t	ap_state;
7378 	struct devctl_iocdata	*dcp = NULL;
7379 	usb_pipe_state_t	prev_pipe_state = 0;
7380 	int			circ, rh_circ, prh_circ;
7381 
7382 	if ((hubd = hubd_get_soft_state(self)) == NULL) {
7383 
7384 		return (ENXIO);
7385 	}
7386 
7387 	rh_dip = hubd->h_usba_device->usb_root_hub_dip;
7388 
7389 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7390 	    "usba_hubdi_ioctl: "
7391 	    "cmd=%x, arg=%lx, mode=%x, cred=%p, rval=%p dev=0x%lx",
7392 	    cmd, arg, mode, (void *)credp, (void *)rvalp, dev);
7393 
7394 	/* read devctl ioctl data */
7395 	if ((cmd != DEVCTL_AP_CONTROL) &&
7396 	    (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)) {
7397 
7398 		return (EFAULT);
7399 	}
7400 
7401 	/*
7402 	 * make sure the hub is connected before trying any
7403 	 * of the following operations:
7404 	 * configure, connect, disconnect
7405 	 */
7406 	mutex_enter(HUBD_MUTEX(hubd));
7407 
7408 	switch (cmd) {
7409 	case DEVCTL_AP_DISCONNECT:
7410 	case DEVCTL_AP_UNCONFIGURE:
7411 	case DEVCTL_AP_CONFIGURE:
7412 		if (hubd->h_dev_state == USB_DEV_DISCONNECTED) {
7413 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
7414 			    "hubd: already gone");
7415 			mutex_exit(HUBD_MUTEX(hubd));
7416 			if (dcp) {
7417 				ndi_dc_freehdl(dcp);
7418 			}
7419 
7420 			return (EIO);
7421 		}
7422 
7423 		/* FALLTHROUGH */
7424 	case DEVCTL_AP_GETSTATE:
7425 		if ((port = hubd_get_port_num(hubd, dcp)) == 0) {
7426 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
7427 			    "hubd: bad port");
7428 			mutex_exit(HUBD_MUTEX(hubd));
7429 			if (dcp) {
7430 				ndi_dc_freehdl(dcp);
7431 			}
7432 
7433 			return (EINVAL);
7434 		}
7435 		break;
7436 
7437 	case DEVCTL_AP_CONTROL:
7438 
7439 		break;
7440 	default:
7441 		mutex_exit(HUBD_MUTEX(hubd));
7442 		if (dcp) {
7443 			ndi_dc_freehdl(dcp);
7444 		}
7445 
7446 		return (ENOTTY);
7447 	}
7448 
7449 	/* should not happen, just in case */
7450 	if (hubd->h_dev_state == USB_DEV_SUSPENDED) {
7451 		mutex_exit(HUBD_MUTEX(hubd));
7452 		if (dcp) {
7453 			ndi_dc_freehdl(dcp);
7454 		}
7455 
7456 		return (EIO);
7457 	}
7458 
7459 	if (hubd->h_reset_port[port]) {
7460 		USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7461 		    "This port is resetting, just return");
7462 		mutex_exit(HUBD_MUTEX(hubd));
7463 		if (dcp) {
7464 			ndi_dc_freehdl(dcp);
7465 		}
7466 
7467 		return (EIO);
7468 	}
7469 
7470 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
7471 	mutex_exit(HUBD_MUTEX(hubd));
7472 
7473 	/* go full power */
7474 	(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
7475 
7476 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
7477 	ndi_devi_enter(rh_dip, &rh_circ);
7478 	ndi_devi_enter(hubd->h_dip, &circ);
7479 
7480 	mutex_enter(HUBD_MUTEX(hubd));
7481 
7482 	hubd->h_hotplug_thread++;
7483 
7484 	/* stop polling if it was active */
7485 	if (hubd->h_ep1_ph) {
7486 		mutex_exit(HUBD_MUTEX(hubd));
7487 		(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
7488 		    USB_FLAGS_SLEEP);
7489 		mutex_enter(HUBD_MUTEX(hubd));
7490 
7491 		if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
7492 			hubd_stop_polling(hubd);
7493 		}
7494 	}
7495 
7496 	switch (cmd) {
7497 	case DEVCTL_AP_DISCONNECT:
7498 		if (hubd_delete_child(hubd, port,
7499 		    NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS) {
7500 			rv = EIO;
7501 		}
7502 
7503 		break;
7504 	case DEVCTL_AP_UNCONFIGURE:
7505 		if (hubd_delete_child(hubd, port,
7506 		    NDI_UNCONFIG, B_FALSE) != USB_SUCCESS) {
7507 			rv = EIO;
7508 		}
7509 
7510 		break;
7511 	case DEVCTL_AP_CONFIGURE:
7512 		/* toggle port */
7513 		if (hubd_toggle_port(hubd, port) != USB_SUCCESS) {
7514 			rv = EIO;
7515 
7516 			break;
7517 		}
7518 
7519 		(void) hubd_handle_port_connect(hubd, port);
7520 		child_dip = hubd_get_child_dip(hubd, port);
7521 		mutex_exit(HUBD_MUTEX(hubd));
7522 
7523 		ndi_devi_exit(hubd->h_dip, circ);
7524 		ndi_devi_exit(rh_dip, rh_circ);
7525 		ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
7526 		if (child_dip == NULL) {
7527 			rv = EIO;
7528 		} else {
7529 			ndi_hold_devi(child_dip);
7530 			if (ndi_devi_online(child_dip, 0) != NDI_SUCCESS)
7531 				rv = EIO;
7532 			ndi_rele_devi(child_dip);
7533 		}
7534 		ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
7535 		ndi_devi_enter(rh_dip, &rh_circ);
7536 		ndi_devi_enter(hubd->h_dip, &circ);
7537 
7538 		mutex_enter(HUBD_MUTEX(hubd));
7539 
7540 		break;
7541 	case DEVCTL_AP_GETSTATE:
7542 		switch (hubd_cfgadm_state(hubd, port)) {
7543 		case HUBD_CFGADM_DISCONNECTED:
7544 			/* port previously 'disconnected' by cfgadm */
7545 			ap_state.ap_rstate = AP_RSTATE_DISCONNECTED;
7546 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
7547 			ap_state.ap_condition = AP_COND_OK;
7548 
7549 			break;
7550 		case HUBD_CFGADM_UNCONFIGURED:
7551 			ap_state.ap_rstate = AP_RSTATE_CONNECTED;
7552 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
7553 			ap_state.ap_condition = AP_COND_OK;
7554 
7555 			break;
7556 		case HUBD_CFGADM_CONFIGURED:
7557 			ap_state.ap_rstate = AP_RSTATE_CONNECTED;
7558 			ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
7559 			ap_state.ap_condition = AP_COND_OK;
7560 
7561 			break;
7562 		case HUBD_CFGADM_STILL_REFERENCED:
7563 			ap_state.ap_rstate = AP_RSTATE_EMPTY;
7564 			ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
7565 			ap_state.ap_condition = AP_COND_UNUSABLE;
7566 
7567 			break;
7568 		case HUBD_CFGADM_EMPTY:
7569 		default:
7570 			ap_state.ap_rstate = AP_RSTATE_EMPTY;
7571 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
7572 			ap_state.ap_condition = AP_COND_OK;
7573 
7574 			break;
7575 		}
7576 
7577 		ap_state.ap_last_change = (time_t)-1;
7578 		ap_state.ap_error_code = 0;
7579 		ap_state.ap_in_transition = 0;
7580 
7581 		USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7582 		    "DEVCTL_AP_GETSTATE: "
7583 		    "ostate=0x%x, rstate=0x%x, condition=0x%x",
7584 		    ap_state.ap_ostate,
7585 		    ap_state.ap_rstate, ap_state.ap_condition);
7586 
7587 		/* copy the return-AP-state information to the user space */
7588 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
7589 			rv = EFAULT;
7590 		}
7591 
7592 		break;
7593 	case DEVCTL_AP_CONTROL:
7594 	{
7595 		/*
7596 		 * Generic devctl for hardware-specific functionality.
7597 		 * For list of sub-commands see hubd_impl.h
7598 		 */
7599 		hubd_ioctl_data_t	ioc;	/* for 64 byte copies */
7600 
7601 		/* copy user ioctl data in first */
7602 #ifdef _MULTI_DATAMODEL
7603 		if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
7604 			hubd_ioctl_data_32_t ioc32;
7605 
7606 			if (ddi_copyin((void *)arg, (void *)&ioc32,
7607 			    sizeof (ioc32), mode) != 0) {
7608 				rv = EFAULT;
7609 
7610 				break;
7611 			}
7612 			ioc.cmd		= (uint_t)ioc32.cmd;
7613 			ioc.port	= (uint_t)ioc32.port;
7614 			ioc.get_size	= (uint_t)ioc32.get_size;
7615 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
7616 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
7617 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
7618 		} else
7619 #endif /* _MULTI_DATAMODEL */
7620 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
7621 		    mode) != 0) {
7622 			rv = EFAULT;
7623 
7624 			break;
7625 		}
7626 
7627 		USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7628 		    "DEVCTL_AP_CONTROL: ioc: cmd=0x%x port=%d get_size=%d"
7629 		    "\n\tbuf=0x%p, bufsiz=%d,  misc_arg=%d", ioc.cmd,
7630 		    ioc.port, ioc.get_size, (void *)ioc.buf, ioc.bufsiz,
7631 		    ioc.misc_arg);
7632 
7633 		/*
7634 		 * To avoid BE/LE and 32/64 issues, a get_size always
7635 		 * returns a 32-bit number.
7636 		 */
7637 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
7638 			rv = EINVAL;
7639 
7640 			break;
7641 		}
7642 
7643 		switch (ioc.cmd) {
7644 		case USB_DESCR_TYPE_DEV:
7645 			msg = "DEVCTL_AP_CONTROL: GET_DEVICE_DESC";
7646 			if (ioc.get_size) {
7647 				/* uint32 so this works 32/64 */
7648 				uint32_t size = sizeof (usb_dev_descr_t);
7649 
7650 				if (ddi_copyout((void *)&size, ioc.buf,
7651 				    ioc.bufsiz, mode) != 0) {
7652 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7653 					    hubd->h_log_handle,
7654 					    "%s: get_size copyout failed", msg);
7655 					rv = EIO;
7656 
7657 					break;
7658 				}
7659 			} else {	/* send out the actual descr */
7660 				usb_dev_descr_t *dev_descrp;
7661 
7662 				/* check child_dip */
7663 				if ((child_dip = hubd_get_child_dip(hubd,
7664 				    ioc.port)) == NULL) {
7665 					rv = EINVAL;
7666 
7667 					break;
7668 				}
7669 
7670 				dev_descrp = usb_get_dev_descr(child_dip);
7671 				if (ioc.bufsiz != sizeof (*dev_descrp)) {
7672 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7673 					    hubd->h_log_handle,
7674 					    "%s: bufsize passed (%d) != sizeof "
7675 					    "usba_device_descr_t (%d)", msg,
7676 					    ioc.bufsiz, dev_descrp->bLength);
7677 					rv = EINVAL;
7678 
7679 					break;
7680 				}
7681 
7682 				if (ddi_copyout((void *)dev_descrp,
7683 				    ioc.buf, ioc.bufsiz, mode) != 0) {
7684 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7685 					    hubd->h_log_handle,
7686 					    "%s: copyout failed.", msg);
7687 					rv = EIO;
7688 
7689 					break;
7690 				}
7691 			}
7692 			break;
7693 		case USB_DESCR_TYPE_STRING:
7694 		{
7695 			char		*str;
7696 			uint32_t	size;
7697 			usba_device_t	*usba_device;
7698 
7699 			msg = "DEVCTL_AP_CONTROL: GET_STRING_DESCR";
7700 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7701 			    "%s: string request: %d", msg, ioc.misc_arg);
7702 
7703 			/* recheck */
7704 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
7705 			    NULL) {
7706 				rv = EINVAL;
7707 
7708 				break;
7709 			}
7710 			usba_device = usba_get_usba_device(child_dip);
7711 
7712 			switch (ioc.misc_arg) {
7713 			case HUBD_MFG_STR:
7714 				str = usba_device->usb_mfg_str;
7715 
7716 				break;
7717 			case HUBD_PRODUCT_STR:
7718 				str = usba_device->usb_product_str;
7719 
7720 				break;
7721 			case HUBD_SERIALNO_STR:
7722 				str = usba_device->usb_serialno_str;
7723 
7724 				break;
7725 			case HUBD_CFG_DESCR_STR:
7726 				mutex_enter(&usba_device->usb_mutex);
7727 				str = usba_device->usb_cfg_str_descr[
7728 				    usba_device->usb_active_cfg_ndx];
7729 				mutex_exit(&usba_device->usb_mutex);
7730 
7731 				break;
7732 			default:
7733 				USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7734 				    hubd->h_log_handle,
7735 				    "%s: Invalid string request", msg);
7736 				rv = EINVAL;
7737 
7738 				break;
7739 			} /* end of switch */
7740 
7741 			if (rv != 0) {
7742 
7743 				break;
7744 			}
7745 
7746 			size = (str != NULL) ? strlen(str) + 1 : 0;
7747 			if (ioc.get_size) {
7748 				if (ddi_copyout((void *)&size, ioc.buf,
7749 				    ioc.bufsiz, mode) != 0) {
7750 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7751 					    hubd->h_log_handle,
7752 					    "%s: copyout of size failed.", msg);
7753 					rv = EIO;
7754 
7755 					break;
7756 				}
7757 			} else {
7758 				if (size == 0) {
7759 					USB_DPRINTF_L3(DPRINT_MASK_CBOPS,
7760 					    hubd->h_log_handle,
7761 					    "%s: String is NULL", msg);
7762 					rv = EINVAL;
7763 
7764 					break;
7765 				}
7766 
7767 				if (ioc.bufsiz != size) {
7768 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7769 					    hubd->h_log_handle,
7770 					    "%s: string buf size wrong", msg);
7771 					rv = EINVAL;
7772 
7773 					break;
7774 				}
7775 
7776 				if (ddi_copyout((void *)str, ioc.buf,
7777 				    ioc.bufsiz, mode) != 0) {
7778 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7779 					    hubd->h_log_handle,
7780 					    "%s: copyout failed.", msg);
7781 					rv = EIO;
7782 
7783 					break;
7784 				}
7785 			}
7786 			break;
7787 		}
7788 		case HUBD_GET_CFGADM_NAME:
7789 		{
7790 			uint32_t   name_len;
7791 			const char *name;
7792 
7793 			/* recheck */
7794 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
7795 			    NULL) {
7796 				rv = EINVAL;
7797 
7798 				break;
7799 			}
7800 			name = ddi_node_name(child_dip);
7801 			if (name == NULL) {
7802 				name = "unsupported";
7803 			}
7804 			name_len = strlen(name) + 1;
7805 
7806 			msg = "DEVCTL_AP_CONTROL: HUBD_GET_CFGADM_NAME";
7807 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7808 			    "%s: name=%s name_len=%d", msg, name, name_len);
7809 
7810 			if (ioc.get_size) {
7811 				if (ddi_copyout((void *)&name_len,
7812 				    ioc.buf, ioc.bufsiz, mode) != 0) {
7813 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7814 					    hubd->h_log_handle,
7815 					    "%s: copyout of size failed", msg);
7816 					rv = EIO;
7817 
7818 					break;
7819 				}
7820 			} else {
7821 				if (ioc.bufsiz != name_len) {
7822 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7823 					    hubd->h_log_handle,
7824 					    "%s: string buf length wrong", msg);
7825 					rv = EINVAL;
7826 
7827 					break;
7828 				}
7829 
7830 				if (ddi_copyout((void *)name, ioc.buf,
7831 				    ioc.bufsiz, mode) != 0) {
7832 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7833 					    hubd->h_log_handle,
7834 					    "%s: copyout failed.", msg);
7835 					rv = EIO;
7836 
7837 					break;
7838 				}
7839 			}
7840 
7841 			break;
7842 		}
7843 
7844 		/*
7845 		 * Return the config index for the currently-configured
7846 		 * configuration.
7847 		 */
7848 		case HUBD_GET_CURRENT_CONFIG:
7849 		{
7850 			uint_t		config_index;
7851 			uint32_t	size = sizeof (config_index);
7852 			usba_device_t	*usba_device;
7853 
7854 			msg = "DEVCTL_AP_CONTROL: GET_CURRENT_CONFIG";
7855 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7856 			    "%s", msg);
7857 
7858 			/*
7859 			 * Return the config index for the configuration
7860 			 * currently in use.
7861 			 * Recheck if child_dip exists
7862 			 */
7863 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
7864 			    NULL) {
7865 				rv = EINVAL;
7866 
7867 				break;
7868 			}
7869 
7870 			usba_device = usba_get_usba_device(child_dip);
7871 			mutex_enter(&usba_device->usb_mutex);
7872 			config_index = usba_device->usb_active_cfg_ndx;
7873 			mutex_exit(&usba_device->usb_mutex);
7874 
7875 			if (ioc.get_size) {
7876 				if (ddi_copyout((void *)&size,
7877 				    ioc.buf, ioc.bufsiz, mode) != 0) {
7878 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7879 					    hubd->h_log_handle,
7880 					    "%s: copyout of size failed.", msg);
7881 					rv = EIO;
7882 
7883 					break;
7884 				}
7885 			} else {
7886 				if (ioc.bufsiz != size) {
7887 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7888 					    hubd->h_log_handle,
7889 					    "%s: buffer size wrong", msg);
7890 					rv = EINVAL;
7891 
7892 					break;
7893 				}
7894 				if (ddi_copyout((void *)&config_index,
7895 				    ioc.buf, ioc.bufsiz, mode) != 0) {
7896 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7897 					    hubd->h_log_handle,
7898 					    "%s: copyout failed", msg);
7899 					rv = EIO;
7900 				}
7901 			}
7902 
7903 			break;
7904 		}
7905 		case HUBD_GET_DEVICE_PATH:
7906 		{
7907 			char		*path;
7908 			uint32_t	size;
7909 
7910 			msg = "DEVCTL_AP_CONTROL: GET_DEVICE_PATH";
7911 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7912 			    "%s", msg);
7913 
7914 			/* Recheck if child_dip exists */
7915 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
7916 			    NULL) {
7917 				rv = EINVAL;
7918 
7919 				break;
7920 			}
7921 
7922 			/* ddi_pathname doesn't supply /devices, so we do. */
7923 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7924 			(void) strcpy(path, "/devices");
7925 			(void) ddi_pathname(child_dip, path + strlen(path));
7926 			size = strlen(path) + 1;
7927 
7928 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7929 			    "%s: device path=%s  size=%d", msg, path, size);
7930 
7931 			if (ioc.get_size) {
7932 				if (ddi_copyout((void *)&size,
7933 				    ioc.buf, ioc.bufsiz, mode) != 0) {
7934 
7935 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7936 					    hubd->h_log_handle,
7937 					    "%s: copyout of size failed.", msg);
7938 					rv = EIO;
7939 				}
7940 			} else {
7941 				if (ioc.bufsiz != size) {
7942 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7943 					    hubd->h_log_handle,
7944 					    "%s: buffer wrong size.", msg);
7945 					rv = EINVAL;
7946 				} else if (ddi_copyout((void *)path,
7947 				    ioc.buf, ioc.bufsiz, mode) != 0) {
7948 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7949 					    hubd->h_log_handle,
7950 					    "%s: copyout failed.", msg);
7951 					rv = EIO;
7952 				}
7953 			}
7954 			kmem_free(path, MAXPATHLEN);
7955 
7956 			break;
7957 		}
7958 		case HUBD_REFRESH_DEVDB:
7959 			msg = "DEVCTL_AP_CONTROL: HUBD_REFRESH_DEVDB";
7960 			USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7961 			    "%s", msg);
7962 
7963 			if ((rv = usba_devdb_refresh()) != USB_SUCCESS) {
7964 				USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
7965 				    hubd->h_log_handle,
7966 				    "%s: Failed: %d", msg, rv);
7967 				rv = EIO;
7968 			}
7969 
7970 			break;
7971 		default:
7972 			rv = ENOTSUP;
7973 		}	/* end switch */
7974 
7975 		break;
7976 	}
7977 
7978 	default:
7979 		rv = ENOTTY;
7980 	}
7981 
7982 	if (dcp) {
7983 		ndi_dc_freehdl(dcp);
7984 	}
7985 
7986 	/* allow hotplug thread now */
7987 	hubd->h_hotplug_thread--;
7988 
7989 	if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
7990 	    hubd->h_ep1_ph && (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
7991 		hubd_start_polling(hubd, 0);
7992 	}
7993 	mutex_exit(HUBD_MUTEX(hubd));
7994 
7995 	ndi_devi_exit(hubd->h_dip, circ);
7996 	ndi_devi_exit(rh_dip, rh_circ);
7997 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
7998 
7999 	mutex_enter(HUBD_MUTEX(hubd));
8000 	hubd_pm_idle_component(hubd, hubd->h_dip, 0);
8001 	mutex_exit(HUBD_MUTEX(hubd));
8002 
8003 	return (rv);
8004 }
8005 
8006 
8007 /*
8008  * Helper func used only to help construct the names for the attachment point
8009  * minor nodes.  Used only in usba_hubdi_attach.
8010  * Returns whether it found ancestry or not (USB_SUCCESS if yes).
8011  * ports between the root hub and the device represented by dip.
8012  * E.g.,  "2.4.3.1" means this device is
8013  *	plugged into port 1 of a hub that is
8014  *	plugged into port 3 of a hub that is
8015  *	plugged into port 4 of a hub that is
8016  *	plugged into port 2 of the root hub.
8017  * NOTE: Max ap_id path len is HUBD_APID_NAMELEN (32 chars), which is
8018  * more than sufficient (as hubs are a max 6 levels deep, port needs 3
8019  * chars plus NULL each)
8020  */
8021 void
8022 hubd_get_ancestry_str(hubd_t *hubd)
8023 {
8024 	char		ap_name[HUBD_APID_NAMELEN];
8025 	dev_info_t	*pdip;
8026 	hubd_t		*phubd;
8027 	usb_port_t	port;
8028 
8029 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
8030 	    "hubd_get_ancestry_str: hubd=0x%p", (void *)hubd);
8031 
8032 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8033 
8034 	/*
8035 	 * The function is extended to support wire adapter class
8036 	 * devices introduced by WUSB spec. The node name is no
8037 	 * longer "hub" only.
8038 	 * Generate the ap_id str based on the parent and child
8039 	 * relationship instead of retrieving it from the hub
8040 	 * device path, which simplifies the algorithm.
8041 	 */
8042 	if (usba_is_root_hub(hubd->h_dip)) {
8043 		hubd->h_ancestry_str[0] = '\0';
8044 	} else {
8045 		port = hubd->h_usba_device->usb_port;
8046 		mutex_exit(HUBD_MUTEX(hubd));
8047 
8048 		pdip = ddi_get_parent(hubd->h_dip);
8049 		/*
8050 		 * The parent of wire adapter device might be usb_mid.
8051 		 * Need to look further up for hub device
8052 		 */
8053 		if (strcmp(ddi_driver_name(pdip), "usb_mid") == 0) {
8054 			pdip = ddi_get_parent(pdip);
8055 			ASSERT(pdip != NULL);
8056 		}
8057 
8058 		phubd = hubd_get_soft_state(pdip);
8059 
8060 		mutex_enter(HUBD_MUTEX(phubd));
8061 		(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
8062 		    phubd->h_ancestry_str, port);
8063 		mutex_exit(HUBD_MUTEX(phubd));
8064 
8065 		mutex_enter(HUBD_MUTEX(hubd));
8066 		(void) strcpy(hubd->h_ancestry_str, ap_name);
8067 		(void) strcat(hubd->h_ancestry_str, ".");
8068 	}
8069 }
8070 
8071 
8072 /* Get which port to operate on.  */
8073 static usb_port_t
8074 hubd_get_port_num(hubd_t *hubd, struct devctl_iocdata *dcp)
8075 {
8076 	int32_t port;
8077 
8078 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8079 
8080 	/* Get which port to operate on.  */
8081 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
8082 		USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
8083 		    "hubd_get_port_num: port lookup failed");
8084 		port = 0;
8085 	}
8086 
8087 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8088 	    "hubd_get_port_num: hubd=0x%p, port=%d", (void *)hubd, port);
8089 
8090 	return ((usb_port_t)port);
8091 }
8092 
8093 
8094 /* check if child still exists */
8095 static dev_info_t *
8096 hubd_get_child_dip(hubd_t *hubd, usb_port_t port)
8097 {
8098 	dev_info_t *child_dip = hubd->h_children_dips[port];
8099 
8100 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8101 	    "hubd_get_child_dip: hubd=0x%p, port=%d", (void *)hubd, port);
8102 
8103 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8104 
8105 	return (child_dip);
8106 }
8107 
8108 
8109 /*
8110  * hubd_cfgadm_state:
8111  *
8112  *	child_dip list		port_state		cfgadm_state
8113  *	--------------		----------		------------
8114  *	!= NULL			connected		configured or
8115  *							unconfigured
8116  *	!= NULL			not connected		disconnect but
8117  *							busy/still referenced
8118  *	NULL			connected		logically disconnected
8119  *	NULL			not connected		empty
8120  */
8121 static uint_t
8122 hubd_cfgadm_state(hubd_t *hubd, usb_port_t port)
8123 {
8124 	uint_t		state;
8125 	dev_info_t	*child_dip = hubd_get_child_dip(hubd, port);
8126 
8127 	if (child_dip) {
8128 		if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
8129 			/*
8130 			 * connected,  now check if driver exists
8131 			 */
8132 			if (DEVI_IS_DEVICE_OFFLINE(child_dip) ||
8133 			    !i_ddi_devi_attached(child_dip)) {
8134 				state = HUBD_CFGADM_UNCONFIGURED;
8135 			} else {
8136 				state = HUBD_CFGADM_CONFIGURED;
8137 			}
8138 		} else {
8139 			/*
8140 			 * this means that the dip is around for
8141 			 * a device that is still referenced but
8142 			 * has been yanked out. So the cfgadm info
8143 			 * for this state should be EMPTY (port empty)
8144 			 * and CONFIGURED (dip still valid).
8145 			 */
8146 			state = HUBD_CFGADM_STILL_REFERENCED;
8147 		}
8148 	} else {
8149 		/* connected but no child dip */
8150 		if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
8151 			/* logically disconnected */
8152 			state = HUBD_CFGADM_DISCONNECTED;
8153 		} else {
8154 			/* physically disconnected */
8155 			state = HUBD_CFGADM_EMPTY;
8156 		}
8157 	}
8158 
8159 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8160 	    "hubd_cfgadm_state: hubd=0x%p, port=%d state=0x%x",
8161 	    (void *)hubd, port, state);
8162 
8163 	return (state);
8164 }
8165 
8166 
8167 /*
8168  * hubd_toggle_port:
8169  */
8170 static int
8171 hubd_toggle_port(hubd_t *hubd, usb_port_t port)
8172 {
8173 	usb_hub_descr_t	*hub_descr;
8174 	int		wait;
8175 	uint_t		retry;
8176 	uint16_t	status;
8177 	uint16_t	change;
8178 
8179 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8180 	    "hubd_toggle_port: hubd=0x%p, port=%d", (void *)hubd, port);
8181 
8182 	if ((hubd_disable_port_power(hubd, port)) != USB_SUCCESS) {
8183 
8184 		return (USB_FAILURE);
8185 	}
8186 
8187 	/*
8188 	 * see hubd_enable_all_port_power() which
8189 	 * requires longer delay for hubs.
8190 	 */
8191 	mutex_exit(HUBD_MUTEX(hubd));
8192 	delay(drv_usectohz(hubd_device_delay / 10));
8193 	mutex_enter(HUBD_MUTEX(hubd));
8194 
8195 	hub_descr = &hubd->h_hub_descr;
8196 
8197 	/*
8198 	 * According to section 11.11 of USB, for hubs with no power
8199 	 * switches, bPwrOn2PwrGood is zero. But we wait for some
8200 	 * arbitrary time to enable power to become stable.
8201 	 *
8202 	 * If an hub supports port power swicthing, we need to wait
8203 	 * at least 20ms before accesing corresonding usb port.
8204 	 */
8205 	if ((hub_descr->wHubCharacteristics &
8206 	    HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) {
8207 		wait = hubd_device_delay / 10;
8208 	} else {
8209 		wait = max(HUB_DEFAULT_POPG,
8210 		    hub_descr->bPwrOn2PwrGood) * 2 * 1000;
8211 	}
8212 
8213 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
8214 	    "hubd_toggle_port: popg=%d wait=%d",
8215 	    hub_descr->bPwrOn2PwrGood, wait);
8216 
8217 	retry = 0;
8218 
8219 	do {
8220 		(void) hubd_enable_port_power(hubd, port);
8221 
8222 		mutex_exit(HUBD_MUTEX(hubd));
8223 		delay(drv_usectohz(wait));
8224 		mutex_enter(HUBD_MUTEX(hubd));
8225 
8226 		/* Get port status */
8227 		(void) hubd_determine_port_status(hubd, port,
8228 		    &status, &change, 0);
8229 
8230 		/* For retry if any, use some extra delay */
8231 		wait = max(wait, hubd_device_delay / 10);
8232 
8233 		retry++;
8234 
8235 	} while ((!(status & PORT_STATUS_PPS)) && (retry < HUBD_PORT_RETRY));
8236 
8237 	/* Print warning message if port has no power */
8238 	if (!(status & PORT_STATUS_PPS)) {
8239 
8240 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
8241 		    "hubd_toggle_port: port %d power-on failed, "
8242 		    "port status 0x%x", port, status);
8243 
8244 		return (USB_FAILURE);
8245 	}
8246 
8247 	return (USB_SUCCESS);
8248 }
8249 
8250 
8251 /*
8252  * hubd_init_power_budget:
8253  *	Init power budget variables in hubd structure. According
8254  *	to USB spec, the power budget rules are:
8255  *	1. local-powered hubs including root-hubs can supply
8256  *	   500mA to each port at maximum
8257  *	2. two bus-powered hubs are not allowed to concatenate
8258  *	3. bus-powered hubs can supply 100mA to each port at
8259  *	   maximum, and the power consumed by all downstream
8260  *	   ports and the hub itself cannot exceed the max power
8261  *	   supplied by the upstream port, i.e., 500mA
8262  *	The routine is only called during hub attach time
8263  */
8264 static int
8265 hubd_init_power_budget(hubd_t *hubd)
8266 {
8267 	uint16_t	status = 0;
8268 	usba_device_t	*hubd_ud = NULL;
8269 	size_t		size;
8270 	usb_cfg_descr_t	cfg_descr;
8271 	dev_info_t	*pdip = NULL;
8272 	hubd_t		*phubd = NULL;
8273 
8274 	if (hubd->h_ignore_pwr_budget) {
8275 
8276 		return (USB_SUCCESS);
8277 	}
8278 
8279 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
8280 	    "hubd_init_power_budget:");
8281 
8282 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8283 	ASSERT(hubd->h_default_pipe != 0);
8284 	mutex_exit(HUBD_MUTEX(hubd));
8285 
8286 	/* get device status */
8287 	if ((usb_get_status(hubd->h_dip, hubd->h_default_pipe,
8288 	    HUB_GET_DEVICE_STATUS_TYPE,
8289 	    0, &status, 0)) != USB_SUCCESS) {
8290 		mutex_enter(HUBD_MUTEX(hubd));
8291 
8292 		return (USB_FAILURE);
8293 	}
8294 
8295 	hubd_ud = usba_get_usba_device(hubd->h_dip);
8296 
8297 	size = usb_parse_cfg_descr(hubd_ud->usb_cfg, hubd_ud->usb_cfg_length,
8298 	    &cfg_descr, USB_CFG_DESCR_SIZE);
8299 
8300 	if (size != USB_CFG_DESCR_SIZE) {
8301 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
8302 		    "get hub configuration descriptor failed");
8303 		mutex_enter(HUBD_MUTEX(hubd));
8304 
8305 		return (USB_FAILURE);
8306 	}
8307 
8308 	mutex_enter(HUBD_MUTEX(hubd));
8309 
8310 	hubd->h_local_pwr_capable = (cfg_descr.bmAttributes &
8311 	    USB_CFG_ATTR_SELFPWR);
8312 
8313 	if (hubd->h_local_pwr_capable) {
8314 		USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
8315 		    "hub is capable of local power");
8316 	}
8317 
8318 	hubd->h_local_pwr_on = (status &
8319 	    USB_DEV_SLF_PWRD_STATUS) && hubd->h_local_pwr_capable;
8320 
8321 	if (hubd->h_local_pwr_on) {
8322 		USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
8323 		    "hub is local-powered");
8324 
8325 		hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
8326 		    USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
8327 	} else {
8328 		hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
8329 		    USB_LOW_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
8330 
8331 		hubd->h_pwr_left = (USB_PWR_UNIT_LOAD *
8332 		    USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
8333 
8334 		ASSERT(!usba_is_root_hub(hubd->h_dip));
8335 
8336 		if (!usba_is_root_hub(hubd->h_dip)) {
8337 			/*
8338 			 * two bus-powered hubs are not
8339 			 * allowed to be concatenated
8340 			 */
8341 			mutex_exit(HUBD_MUTEX(hubd));
8342 
8343 			pdip = ddi_get_parent(hubd->h_dip);
8344 			phubd = hubd_get_soft_state(pdip);
8345 			ASSERT(phubd != NULL);
8346 
8347 			if (!phubd->h_ignore_pwr_budget) {
8348 				mutex_enter(HUBD_MUTEX(phubd));
8349 				if (phubd->h_local_pwr_on == B_FALSE) {
8350 					USB_DPRINTF_L1(DPRINT_MASK_HUB,
8351 					    hubd->h_log_handle,
8352 					    "two bus-powered hubs cannot "
8353 					    "be concatenated");
8354 
8355 					mutex_exit(HUBD_MUTEX(phubd));
8356 					mutex_enter(HUBD_MUTEX(hubd));
8357 
8358 					return (USB_FAILURE);
8359 				}
8360 				mutex_exit(HUBD_MUTEX(phubd));
8361 			}
8362 
8363 			mutex_enter(HUBD_MUTEX(hubd));
8364 
8365 			USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
8366 			    "hub is bus-powered");
8367 		} else {
8368 			USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
8369 			    "root-hub must be local-powered");
8370 		}
8371 
8372 		/*
8373 		 * Subtract the power consumed by the hub itself
8374 		 * and get the power that can be supplied to
8375 		 * downstream ports
8376 		 */
8377 		hubd->h_pwr_left -=
8378 		    hubd->h_hub_descr.bHubContrCurrent /
8379 		    USB_CFG_DESCR_PWR_UNIT;
8380 		if (hubd->h_pwr_left < 0) {
8381 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
8382 			    "hubd->h_pwr_left is less than bHubContrCurrent, "
8383 			    "should fail");
8384 
8385 			return (USB_FAILURE);
8386 		}
8387 	}
8388 
8389 	return (USB_SUCCESS);
8390 }
8391 
8392 
8393 /*
8394  * usba_hubdi_check_power_budget:
8395  *	Check if the hub has enough power budget to allow a
8396  *	child device to select a configuration of config_index.
8397  */
8398 int
8399 usba_hubdi_check_power_budget(dev_info_t *dip, usba_device_t *child_ud,
8400 	uint_t config_index)
8401 {
8402 	int16_t		pwr_left, pwr_limit, pwr_required;
8403 	size_t		size;
8404 	usb_cfg_descr_t cfg_descr;
8405 	hubd_t		*hubd;
8406 
8407 	if ((hubd = hubd_get_soft_state(dip)) == NULL) {
8408 
8409 		return (USB_FAILURE);
8410 	}
8411 
8412 	if (hubd->h_ignore_pwr_budget) {
8413 
8414 		return (USB_SUCCESS);
8415 	}
8416 
8417 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8418 	    "usba_hubdi_check_power_budget: "
8419 	    "dip=0x%p child_ud=0x%p conf_index=%d", (void *)dip,
8420 	    (void *)child_ud, config_index);
8421 
8422 	mutex_enter(HUBD_MUTEX(hubd));
8423 	pwr_limit = hubd->h_pwr_limit;
8424 	if (hubd->h_local_pwr_on == B_FALSE) {
8425 		pwr_left = hubd->h_pwr_left;
8426 		pwr_limit = (pwr_limit <= pwr_left) ? pwr_limit : pwr_left;
8427 	}
8428 	mutex_exit(HUBD_MUTEX(hubd));
8429 
8430 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8431 	    "usba_hubdi_check_power_budget: "
8432 	    "available power is %dmA", pwr_limit * USB_CFG_DESCR_PWR_UNIT);
8433 
8434 	size = usb_parse_cfg_descr(
8435 	    child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
8436 	    &cfg_descr, USB_CFG_DESCR_SIZE);
8437 
8438 	if (size != USB_CFG_DESCR_SIZE) {
8439 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8440 		    "get hub configuration descriptor failed");
8441 
8442 		return (USB_FAILURE);
8443 	}
8444 
8445 	pwr_required = cfg_descr.bMaxPower;
8446 
8447 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8448 	    "usba_hubdi_check_power_budget: "
8449 	    "child bmAttributes=0x%x bMaxPower=%d "
8450 	    "with config_index=%d", cfg_descr.bmAttributes,
8451 	    pwr_required, config_index);
8452 
8453 	if (pwr_required > pwr_limit) {
8454 		USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8455 		    "configuration %d for device %s %s at port %d "
8456 		    "exceeds power available for this port, please "
8457 		    "re-insert your device into another hub port which "
8458 		    "has enough power",
8459 		    config_index,
8460 		    child_ud->usb_mfg_str,
8461 		    child_ud->usb_product_str,
8462 		    child_ud->usb_port);
8463 
8464 		return (USB_FAILURE);
8465 	}
8466 
8467 	return (USB_SUCCESS);
8468 }
8469 
8470 
8471 /*
8472  * usba_hubdi_incr_power_budget:
8473  *	Increase the hub power budget value when a child device
8474  *	is removed from a bus-powered hub port.
8475  */
8476 void
8477 usba_hubdi_incr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
8478 {
8479 	uint16_t	pwr_value;
8480 	hubd_t		*hubd = hubd_get_soft_state(dip);
8481 
8482 	ASSERT(hubd != NULL);
8483 
8484 	if (hubd->h_ignore_pwr_budget) {
8485 
8486 		return;
8487 	}
8488 
8489 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
8490 	    "usba_hubdi_incr_power_budget: "
8491 	    "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
8492 
8493 	mutex_enter(HUBD_MUTEX(hubd));
8494 	if (hubd->h_local_pwr_on == B_TRUE) {
8495 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
8496 		    "usba_hubdi_incr_power_budget: "
8497 		    "hub is local powered");
8498 		mutex_exit(HUBD_MUTEX(hubd));
8499 
8500 		return;
8501 	}
8502 	mutex_exit(HUBD_MUTEX(hubd));
8503 
8504 	mutex_enter(&child_ud->usb_mutex);
8505 	if (child_ud->usb_pwr_from_hub == 0) {
8506 		mutex_exit(&child_ud->usb_mutex);
8507 
8508 		return;
8509 	}
8510 	pwr_value = child_ud->usb_pwr_from_hub;
8511 	mutex_exit(&child_ud->usb_mutex);
8512 
8513 	mutex_enter(HUBD_MUTEX(hubd));
8514 	hubd->h_pwr_left += pwr_value;
8515 
8516 	USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
8517 	    "usba_hubdi_incr_power_budget: "
8518 	    "available power is %dmA, increased by %dmA",
8519 	    hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
8520 	    pwr_value * USB_CFG_DESCR_PWR_UNIT);
8521 
8522 	mutex_exit(HUBD_MUTEX(hubd));
8523 
8524 	mutex_enter(&child_ud->usb_mutex);
8525 	child_ud->usb_pwr_from_hub = 0;
8526 	mutex_exit(&child_ud->usb_mutex);
8527 }
8528 
8529 
8530 /*
8531  * usba_hubdi_decr_power_budget:
8532  *	Decrease the hub power budget value when a child device
8533  *	is inserted to a bus-powered hub port.
8534  */
8535 void
8536 usba_hubdi_decr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
8537 {
8538 	uint16_t	pwr_value;
8539 	size_t		size;
8540 	usb_cfg_descr_t	cfg_descr;
8541 	hubd_t		*hubd = hubd_get_soft_state(dip);
8542 
8543 	ASSERT(hubd != NULL);
8544 
8545 	if (hubd->h_ignore_pwr_budget) {
8546 
8547 		return;
8548 	}
8549 
8550 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
8551 	    "usba_hubdi_decr_power_budget: "
8552 	    "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
8553 
8554 	mutex_enter(HUBD_MUTEX(hubd));
8555 	if (hubd->h_local_pwr_on == B_TRUE) {
8556 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
8557 		    "usba_hubdi_decr_power_budget: "
8558 		    "hub is local powered");
8559 		mutex_exit(HUBD_MUTEX(hubd));
8560 
8561 		return;
8562 	}
8563 	mutex_exit(HUBD_MUTEX(hubd));
8564 
8565 	mutex_enter(&child_ud->usb_mutex);
8566 	if (child_ud->usb_pwr_from_hub > 0) {
8567 		mutex_exit(&child_ud->usb_mutex);
8568 
8569 		return;
8570 	}
8571 	mutex_exit(&child_ud->usb_mutex);
8572 
8573 	size = usb_parse_cfg_descr(
8574 	    child_ud->usb_cfg, child_ud->usb_cfg_length,
8575 	    &cfg_descr, USB_CFG_DESCR_SIZE);
8576 	ASSERT(size == USB_CFG_DESCR_SIZE);
8577 
8578 	mutex_enter(HUBD_MUTEX(hubd));
8579 	pwr_value = cfg_descr.bMaxPower;
8580 	hubd->h_pwr_left -= pwr_value;
8581 	ASSERT(hubd->h_pwr_left >= 0);
8582 
8583 	USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
8584 	    "usba_hubdi_decr_power_budget: "
8585 	    "available power is %dmA, decreased by %dmA",
8586 	    hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
8587 	    pwr_value * USB_CFG_DESCR_PWR_UNIT);
8588 
8589 	mutex_exit(HUBD_MUTEX(hubd));
8590 
8591 	mutex_enter(&child_ud->usb_mutex);
8592 	child_ud->usb_pwr_from_hub = pwr_value;
8593 	mutex_exit(&child_ud->usb_mutex);
8594 }
8595 
8596 /*
8597  * hubd_wait_for_hotplug_exit:
8598  *	Waiting for the exit of the running hotplug thread or ioctl thread.
8599  */
8600 static int
8601 hubd_wait_for_hotplug_exit(hubd_t *hubd)
8602 {
8603 	clock_t		until = drv_usectohz(1000000);
8604 	int		rval;
8605 
8606 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8607 
8608 	if (hubd->h_hotplug_thread) {
8609 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8610 		    "waiting for hubd hotplug thread exit");
8611 		rval = cv_reltimedwait(&hubd->h_cv_hotplug_dev,
8612 		    &hubd->h_mutex, until, TR_CLOCK_TICK);
8613 
8614 		if ((rval <= 0) && (hubd->h_hotplug_thread)) {
8615 
8616 			return (USB_FAILURE);
8617 		}
8618 	}
8619 
8620 	return (USB_SUCCESS);
8621 }
8622 
8623 /*
8624  * hubd_reset_thread:
8625  *	handles the "USB_RESET_LVL_REATTACH" reset of usb device.
8626  *
8627  *	- delete the child (force detaching the device and its children)
8628  *	- reset the corresponding parent hub port
8629  *	- create the child (force re-attaching the device and its children)
8630  */
8631 static void
8632 hubd_reset_thread(void *arg)
8633 {
8634 	hubd_reset_arg_t *hd_arg = (hubd_reset_arg_t *)arg;
8635 	hubd_t		*hubd = hd_arg->hubd;
8636 	uint16_t	reset_port = hd_arg->reset_port;
8637 	uint16_t	status, change;
8638 	hub_power_t	*hubpm;
8639 	dev_info_t	*hdip = hubd->h_dip;
8640 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
8641 	dev_info_t	*child_dip;
8642 	boolean_t	online_child = B_FALSE;
8643 	int		prh_circ, rh_circ, circ, devinst;
8644 	char		*devname;
8645 	int		i = 0;
8646 	int		rval = USB_FAILURE;
8647 
8648 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8649 	    "hubd_reset_thread:  started, hubd_reset_port = 0x%x", reset_port);
8650 
8651 	kmem_free(arg, sizeof (hubd_reset_arg_t));
8652 
8653 	mutex_enter(HUBD_MUTEX(hubd));
8654 
8655 	child_dip = hubd->h_children_dips[reset_port];
8656 	ASSERT(child_dip != NULL);
8657 
8658 	devname = (char *)ddi_driver_name(child_dip);
8659 	devinst = ddi_get_instance(child_dip);
8660 
8661 	/* if our bus power entry point is active, quit the reset */
8662 	if (hubd->h_bus_pwr) {
8663 		USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8664 		    "%s%d is under bus power management, cannot be reset. "
8665 		    "Please disconnect and reconnect this device.",
8666 		    devname, devinst);
8667 
8668 		goto Fail;
8669 	}
8670 
8671 	if (hubd_wait_for_hotplug_exit(hubd) == USB_FAILURE) {
8672 		/* we got woken up because of a timeout */
8673 		USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
8674 		    hubd->h_log_handle, "Time out when resetting the device"
8675 		    " %s%d. Please disconnect and reconnect this device.",
8676 		    devname, devinst);
8677 
8678 		goto Fail;
8679 	}
8680 
8681 	hubd->h_hotplug_thread++;
8682 
8683 	/* is this the root hub? */
8684 	if ((hdip == rh_dip) &&
8685 	    (hubd->h_dev_state == USB_DEV_PWRED_DOWN)) {
8686 		hubpm = hubd->h_hubpm;
8687 
8688 		/* mark the root hub as full power */
8689 		hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
8690 		hubpm->hubp_time_at_full_power = ddi_get_time();
8691 		mutex_exit(HUBD_MUTEX(hubd));
8692 
8693 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8694 		    "hubd_reset_thread: call pm_power_has_changed");
8695 
8696 		(void) pm_power_has_changed(hdip, 0,
8697 		    USB_DEV_OS_FULL_PWR);
8698 
8699 		mutex_enter(HUBD_MUTEX(hubd));
8700 		hubd->h_dev_state = USB_DEV_ONLINE;
8701 	}
8702 
8703 	mutex_exit(HUBD_MUTEX(hubd));
8704 
8705 	/*
8706 	 * this ensures one reset activity per system at a time.
8707 	 * we enter the parent PCI node to have this serialization.
8708 	 * this also excludes ioctls and deathrow thread
8709 	 */
8710 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
8711 	ndi_devi_enter(rh_dip, &rh_circ);
8712 
8713 	/* exclude other threads */
8714 	ndi_devi_enter(hdip, &circ);
8715 	mutex_enter(HUBD_MUTEX(hubd));
8716 
8717 	/*
8718 	 * We need to make sure that the child is still online for a hotplug
8719 	 * thread could have inserted which detached the child.
8720 	 */
8721 	if (hubd->h_children_dips[reset_port]) {
8722 		mutex_exit(HUBD_MUTEX(hubd));
8723 		/* First disconnect the device */
8724 		hubd_post_event(hubd, reset_port, USBA_EVENT_TAG_HOT_REMOVAL);
8725 
8726 		/* delete cached dv_node's but drop locks first */
8727 		ndi_devi_exit(hdip, circ);
8728 		ndi_devi_exit(rh_dip, rh_circ);
8729 		ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
8730 
8731 		(void) devfs_clean(rh_dip, NULL, DV_CLEAN_FORCE);
8732 
8733 		/*
8734 		 * workaround only for storage device. When it's able to force
8735 		 * detach a driver, this code can be removed safely.
8736 		 *
8737 		 * If we're to reset storage device and the device is used, we
8738 		 * will wait at most extra 20s for applications to exit and
8739 		 * close the device. This is especially useful for HAL-based
8740 		 * applications.
8741 		 */
8742 		if ((strcmp(devname, "scsa2usb") == 0) &&
8743 		    DEVI(child_dip)->devi_ref != 0) {
8744 			while (i++ < hubdi_reset_delay) {
8745 				mutex_enter(HUBD_MUTEX(hubd));
8746 				rval = hubd_delete_child(hubd, reset_port,
8747 				    NDI_DEVI_REMOVE, B_FALSE);
8748 				mutex_exit(HUBD_MUTEX(hubd));
8749 				if (rval == USB_SUCCESS)
8750 					break;
8751 
8752 				delay(drv_usectohz(1000000)); /* 1s */
8753 			}
8754 		}
8755 
8756 		ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
8757 		ndi_devi_enter(rh_dip, &rh_circ);
8758 		ndi_devi_enter(hdip, &circ);
8759 
8760 		mutex_enter(HUBD_MUTEX(hubd));
8761 
8762 		/* Then force detaching the device */
8763 		if ((rval != USB_SUCCESS) && (hubd_delete_child(hubd,
8764 		    reset_port, NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS)) {
8765 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8766 			    "%s%d cannot be reset due to other applications "
8767 			    "are using it, please first close these "
8768 			    "applications, then disconnect and reconnect"
8769 			    "the device.", devname, devinst);
8770 
8771 			mutex_exit(HUBD_MUTEX(hubd));
8772 			/* post a re-connect event */
8773 			hubd_post_event(hubd, reset_port,
8774 			    USBA_EVENT_TAG_HOT_INSERTION);
8775 			mutex_enter(HUBD_MUTEX(hubd));
8776 		} else {
8777 			(void) hubd_determine_port_status(hubd, reset_port,
8778 			    &status, &change, HUBD_ACK_ALL_CHANGES);
8779 
8780 			/* Reset the parent hubd port and create new child */
8781 			if (status & PORT_STATUS_CCS) {
8782 				online_child |=	(hubd_handle_port_connect(hubd,
8783 				    reset_port) == USB_SUCCESS);
8784 			}
8785 		}
8786 	}
8787 
8788 	/* release locks so we can do a devfs_clean */
8789 	mutex_exit(HUBD_MUTEX(hubd));
8790 
8791 	/* delete cached dv_node's but drop locks first */
8792 	ndi_devi_exit(hdip, circ);
8793 	ndi_devi_exit(rh_dip, rh_circ);
8794 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
8795 
8796 	(void) devfs_clean(rh_dip, NULL, 0);
8797 
8798 	/* now check if any children need onlining */
8799 	if (online_child) {
8800 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8801 		    "hubd_reset_thread: onlining children");
8802 
8803 		(void) ndi_devi_online(hubd->h_dip, 0);
8804 	}
8805 
8806 	mutex_enter(HUBD_MUTEX(hubd));
8807 
8808 	/* allow hotplug thread now */
8809 	hubd->h_hotplug_thread--;
8810 Fail:
8811 	hubd_start_polling(hubd, 0);
8812 
8813 	/* mark this device as idle */
8814 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
8815 
8816 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8817 	    "hubd_reset_thread: exit, %d", hubd->h_hotplug_thread);
8818 
8819 	hubd->h_reset_port[reset_port] = B_FALSE;
8820 
8821 	mutex_exit(HUBD_MUTEX(hubd));
8822 
8823 	ndi_rele_devi(hdip);
8824 }
8825 
8826 /*
8827  * hubd_check_same_device:
8828  *	- open the default pipe of the device.
8829  *	- compare the old and new descriptors of the device.
8830  *	- close the default pipe.
8831  */
8832 static int
8833 hubd_check_same_device(hubd_t *hubd, usb_port_t port)
8834 {
8835 	dev_info_t		*dip = hubd->h_children_dips[port];
8836 	usb_pipe_handle_t	ph;
8837 	int			rval = USB_FAILURE;
8838 
8839 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8840 
8841 	mutex_exit(HUBD_MUTEX(hubd));
8842 	/* Open the default pipe to operate the device */
8843 	if (usb_pipe_open(dip, NULL, NULL,
8844 	    USB_FLAGS_SLEEP| USBA_FLAGS_PRIVILEGED,
8845 	    &ph) == USB_SUCCESS) {
8846 		/*
8847 		 * Check that if the device's descriptors are different
8848 		 * from the values saved before the port reset.
8849 		 */
8850 		rval = usb_check_same_device(dip,
8851 		    hubd->h_log_handle, USB_LOG_L0,
8852 		    DPRINT_MASK_ALL, USB_CHK_ALL, NULL);
8853 
8854 		usb_pipe_close(dip, ph, USB_FLAGS_SLEEP |
8855 		    USBA_FLAGS_PRIVILEGED, NULL, NULL);
8856 	}
8857 	mutex_enter(HUBD_MUTEX(hubd));
8858 
8859 	return (rval);
8860 }
8861 
8862 /*
8863  * usba_hubdi_reset_device
8864  *	Called by usb_reset_device to handle usb device reset.
8865  */
8866 int
8867 usba_hubdi_reset_device(dev_info_t *dip, usb_dev_reset_lvl_t reset_level)
8868 {
8869 	hubd_t			*hubd;
8870 	usb_port_t		port = 0;
8871 	dev_info_t		*hdip;
8872 	usb_pipe_state_t	prev_pipe_state = 0;
8873 	usba_device_t		*usba_device;
8874 	hubd_reset_arg_t	*arg;
8875 	int			i, ph_open_cnt;
8876 	int			rval = USB_FAILURE;
8877 
8878 	if ((!dip) || usba_is_root_hub(dip)) {
8879 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8880 		    "usba_hubdi_reset_device: NULL dip or root hub");
8881 
8882 		return (USB_INVALID_ARGS);
8883 	}
8884 
8885 	if (!usb_owns_device(dip)) {
8886 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8887 		    "usba_hubdi_reset_device: Not owns the device");
8888 
8889 		return (USB_INVALID_PERM);
8890 	}
8891 
8892 	if ((reset_level != USB_RESET_LVL_REATTACH) &&
8893 	    (reset_level != USB_RESET_LVL_DEFAULT)) {
8894 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8895 		    "usba_hubdi_reset_device: Unknown flags");
8896 
8897 		return (USB_INVALID_ARGS);
8898 	}
8899 
8900 	if ((hdip = ddi_get_parent(dip)) == NULL) {
8901 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8902 		    "usba_hubdi_reset_device: fail to get parent hub");
8903 
8904 		return (USB_INVALID_ARGS);
8905 	}
8906 
8907 	if ((hubd = hubd_get_soft_state(hdip)) == NULL) {
8908 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8909 		    "usba_hubdi_reset_device: fail to get hub softstate");
8910 
8911 		return (USB_INVALID_ARGS);
8912 	}
8913 
8914 	mutex_enter(HUBD_MUTEX(hubd));
8915 
8916 	/* make sure the hub is connected before trying any kinds of reset. */
8917 	if ((hubd->h_dev_state == USB_DEV_DISCONNECTED) ||
8918 	    (hubd->h_dev_state == USB_DEV_SUSPENDED)) {
8919 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8920 		    "usb_reset_device: the state %d of the hub/roothub "
8921 		    "associated to the device 0x%p is incorrect",
8922 		    hubd->h_dev_state, (void *)dip);
8923 		mutex_exit(HUBD_MUTEX(hubd));
8924 
8925 		return (USB_INVALID_ARGS);
8926 	}
8927 
8928 	mutex_exit(HUBD_MUTEX(hubd));
8929 
8930 	port = hubd_child_dip2port(hubd, dip);
8931 
8932 	mutex_enter(HUBD_MUTEX(hubd));
8933 
8934 	if (hubd->h_reset_port[port]) {
8935 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8936 		    "usb_reset_device: the corresponding port is resetting");
8937 		mutex_exit(HUBD_MUTEX(hubd));
8938 
8939 		return (USB_SUCCESS);
8940 	}
8941 
8942 	/*
8943 	 * For Default reset, client drivers should first close all the pipes
8944 	 * except default pipe before calling the function, also should not
8945 	 * call the function during interrupt context.
8946 	 */
8947 	if (reset_level == USB_RESET_LVL_DEFAULT) {
8948 		usba_device = hubd->h_usba_devices[port];
8949 		mutex_exit(HUBD_MUTEX(hubd));
8950 
8951 		if (servicing_interrupt()) {
8952 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8953 			    "usb_reset_device: during interrput context, quit");
8954 
8955 			return (USB_INVALID_CONTEXT);
8956 		}
8957 		/* Check if all the pipes have been closed */
8958 		for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) {
8959 			if (usba_device->usb_ph_list[i].usba_ph_data) {
8960 				ph_open_cnt++;
8961 				break;
8962 			}
8963 		}
8964 		if (ph_open_cnt) {
8965 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8966 			    "usb_reset_device: %d pipes are still open",
8967 			    ph_open_cnt);
8968 
8969 			return (USB_BUSY);
8970 		}
8971 		mutex_enter(HUBD_MUTEX(hubd));
8972 	}
8973 
8974 	/* Don't perform reset while the device is detaching */
8975 	if (hubd->h_port_state[port] & HUBD_CHILD_DETACHING) {
8976 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8977 		    "usb_reset_device: the device is detaching, "
8978 		    "cannot be reset");
8979 		mutex_exit(HUBD_MUTEX(hubd));
8980 
8981 		return (USB_FAILURE);
8982 	}
8983 
8984 	hubd->h_reset_port[port] = B_TRUE;
8985 	hdip = hubd->h_dip;
8986 	mutex_exit(HUBD_MUTEX(hubd));
8987 
8988 	/* Don't allow hub detached during the reset */
8989 	ndi_hold_devi(hdip);
8990 
8991 	mutex_enter(HUBD_MUTEX(hubd));
8992 	hubd_pm_busy_component(hubd, hdip, 0);
8993 	mutex_exit(HUBD_MUTEX(hubd));
8994 	/* go full power */
8995 	(void) pm_raise_power(hdip, 0, USB_DEV_OS_FULL_PWR);
8996 	mutex_enter(HUBD_MUTEX(hubd));
8997 
8998 	hubd->h_hotplug_thread++;
8999 
9000 	/* stop polling if it was active */
9001 	if (hubd->h_ep1_ph) {
9002 		mutex_exit(HUBD_MUTEX(hubd));
9003 		(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
9004 		    USB_FLAGS_SLEEP);
9005 		mutex_enter(HUBD_MUTEX(hubd));
9006 
9007 		if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
9008 			hubd_stop_polling(hubd);
9009 		}
9010 	}
9011 
9012 	switch (reset_level) {
9013 	case USB_RESET_LVL_REATTACH:
9014 		mutex_exit(HUBD_MUTEX(hubd));
9015 		arg = (hubd_reset_arg_t *)kmem_zalloc(
9016 		    sizeof (hubd_reset_arg_t), KM_SLEEP);
9017 		arg->hubd = hubd;
9018 		arg->reset_port = port;
9019 		mutex_enter(HUBD_MUTEX(hubd));
9020 
9021 		if ((rval = usb_async_req(hdip, hubd_reset_thread,
9022 		    (void *)arg, 0)) == USB_SUCCESS) {
9023 			hubd->h_hotplug_thread--;
9024 			mutex_exit(HUBD_MUTEX(hubd));
9025 
9026 			return (USB_SUCCESS);
9027 		} else {
9028 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9029 			    "Cannot create reset thread, the device %s%d failed"
9030 			    " to reset", ddi_driver_name(dip),
9031 			    ddi_get_instance(dip));
9032 
9033 			kmem_free(arg, sizeof (hubd_reset_arg_t));
9034 		}
9035 
9036 		break;
9037 	case USB_RESET_LVL_DEFAULT:
9038 		/*
9039 		 * Reset hub port and then recover device's address, set back
9040 		 * device's configuration, hubd_handle_port_connect() will
9041 		 * handle errors happened during this process.
9042 		 */
9043 		if ((rval = hubd_handle_port_connect(hubd, port))
9044 		    == USB_SUCCESS) {
9045 			mutex_exit(HUBD_MUTEX(hubd));
9046 			/* re-open the default pipe */
9047 			rval = usba_persistent_pipe_open(usba_device);
9048 			mutex_enter(HUBD_MUTEX(hubd));
9049 			if (rval != USB_SUCCESS) {
9050 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
9051 				    hubd->h_log_handle, "failed to reopen "
9052 				    "default pipe after reset, disable hub"
9053 				    "port for %s%d", ddi_driver_name(dip),
9054 				    ddi_get_instance(dip));
9055 				/*
9056 				 * Disable port to set out a hotplug thread
9057 				 * which will handle errors.
9058 				 */
9059 				(void) hubd_disable_port(hubd, port);
9060 			}
9061 		}
9062 
9063 		break;
9064 	default:
9065 
9066 		break;
9067 	}
9068 
9069 	/* allow hotplug thread now */
9070 	hubd->h_hotplug_thread--;
9071 
9072 	if ((hubd->h_dev_state == USB_DEV_ONLINE) && hubd->h_ep1_ph &&
9073 	    (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
9074 		hubd_start_polling(hubd, 0);
9075 	}
9076 
9077 	hubd_pm_idle_component(hubd, hdip, 0);
9078 
9079 	/* Clear reset mark for the port. */
9080 	hubd->h_reset_port[port] = B_FALSE;
9081 
9082 	mutex_exit(HUBD_MUTEX(hubd));
9083 
9084 	ndi_rele_devi(hdip);
9085 
9086 	return (rval);
9087 }
9088