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