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