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