xref: /illumos-gate/usr/src/uts/common/io/usb/usba/hubdi.c (revision 0d2006e4)
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(
59 			dev_info_t		*dip,
60 			dev_info_t		*rdip,
61 			ddi_ctl_enum_t		op,
62 			void			*arg,
63 			void			*result);
64 
65 static int	usba_hubdi_map_fault(
66 			dev_info_t		*dip,
67 			dev_info_t		*rdip,
68 			struct hat		*hat,
69 			struct seg		*seg,
70 			caddr_t 		addr,
71 			struct devpage		*dp,
72 			pfn_t			pfn,
73 			uint_t			prot,
74 			uint_t			lock);
75 
76 static int hubd_busop_get_eventcookie(dev_info_t *dip,
77 			dev_info_t *rdip,
78 			char *eventname,
79 			ddi_eventcookie_t *cookie);
80 static int hubd_busop_add_eventcall(dev_info_t *dip,
81 			dev_info_t *rdip,
82 			ddi_eventcookie_t cookie,
83 			void (*callback)(dev_info_t *dip,
84 				ddi_eventcookie_t cookie, void *arg,
85 				void *bus_impldata),
86 			void *arg, ddi_callback_id_t *cb_id);
87 static int hubd_busop_remove_eventcall(dev_info_t *dip,
88 			ddi_callback_id_t cb_id);
89 static int hubd_bus_config(dev_info_t *dip,
90 			uint_t flag,
91 			ddi_bus_config_op_t op,
92 			void *arg,
93 			dev_info_t **child);
94 static int hubd_bus_unconfig(dev_info_t *dip,
95 			uint_t flag,
96 			ddi_bus_config_op_t op,
97 			void *arg);
98 static int hubd_bus_power(dev_info_t *dip, void *impl_arg,
99 			pm_bus_power_op_t op, void *arg, void *result);
100 
101 static usb_port_t  hubd_get_port_num(hubd_t *, struct devctl_iocdata *);
102 static dev_info_t *hubd_get_child_dip(hubd_t *, usb_port_t);
103 static uint_t hubd_cfgadm_state(hubd_t *, usb_port_t);
104 static int hubd_toggle_port(hubd_t *, usb_port_t);
105 static void hubd_register_cpr_callback(hubd_t *);
106 static void hubd_unregister_cpr_callback(hubd_t *);
107 
108 /*
109  * Busops vector for USB HUB's
110  */
111 struct bus_ops usba_hubdi_busops =	{
112 	BUSO_REV,
113 	nullbusmap,			/* bus_map */
114 	NULL,				/* bus_get_intrspec */
115 	NULL,				/* bus_add_intrspec */
116 	NULL,				/* bus_remove_intrspec */
117 	usba_hubdi_map_fault,		/* bus_map_fault */
118 	NULL,				/* bus_dma_map */
119 	ddi_dma_allochdl,
120 	ddi_dma_freehdl,
121 	ddi_dma_bindhdl,
122 	ddi_dma_unbindhdl,
123 	ddi_dma_flush,
124 	ddi_dma_win,
125 	ddi_dma_mctl,			/* bus_dma_ctl */
126 	usba_hubdi_bus_ctl,		/* bus_ctl */
127 	ddi_bus_prop_op,		/* bus_prop_op */
128 	hubd_busop_get_eventcookie,
129 	hubd_busop_add_eventcall,
130 	hubd_busop_remove_eventcall,
131 	NULL,				/* bus_post_event */
132 	NULL,				/* bus_intr_ctl */
133 	hubd_bus_config,		/* bus_config */
134 	hubd_bus_unconfig,		/* bus_unconfig */
135 	NULL,				/* bus_fm_init */
136 	NULL,				/* bus_fm_fini */
137 	NULL,				/* bus_fm_access_enter */
138 	NULL,				/* bus_fm_access_exit */
139 	hubd_bus_power			/* bus_power */
140 };
141 
142 #define	USB_HUB_INTEL_VID	0x8087
143 #define	USB_HUB_INTEL_PID	0x0020
144 
145 /*
146  * local variables
147  */
148 static kmutex_t	usba_hubdi_mutex;	/* protects USBA HUB data structures */
149 
150 static usba_list_entry_t	usba_hubdi_list;
151 
152 usb_log_handle_t	hubdi_log_handle;
153 uint_t			hubdi_errlevel = USB_LOG_L4;
154 uint_t			hubdi_errmask = (uint_t)-1;
155 uint8_t			hubdi_min_pm_threshold = 5; /* seconds */
156 uint8_t			hubdi_reset_delay = 20; /* seconds */
157 extern int modrootloaded;
158 
159 /*
160  * initialize private data
161  */
162 void
163 usba_hubdi_initialization()
164 {
165 	hubdi_log_handle = usb_alloc_log_hdl(NULL, "hubdi", &hubdi_errlevel,
166 	    &hubdi_errmask, NULL, 0);
167 
168 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
169 	    "usba_hubdi_initialization");
170 
171 	mutex_init(&usba_hubdi_mutex, NULL, MUTEX_DRIVER, NULL);
172 
173 	usba_init_list(&usba_hubdi_list, NULL, NULL);
174 }
175 
176 
177 void
178 usba_hubdi_destroy()
179 {
180 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
181 	    "usba_hubdi_destroy");
182 
183 	mutex_destroy(&usba_hubdi_mutex);
184 	usba_destroy_list(&usba_hubdi_list);
185 
186 	usb_free_log_hdl(hubdi_log_handle);
187 }
188 
189 
190 /*
191  * Called by an	HUB to attach an instance of the driver
192  *	make this instance known to USBA
193  *	the HUB	should initialize usba_hubdi structure prior
194  *	to calling this	interface
195  */
196 int
197 usba_hubdi_register(dev_info_t	*dip, uint_t flags)
198 {
199 	usba_hubdi_t *hubdi = kmem_zalloc(sizeof (usba_hubdi_t), KM_SLEEP);
200 	usba_device_t *usba_device = usba_get_usba_device(dip);
201 
202 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
203 	    "usba_hubdi_register: %s", ddi_node_name(dip));
204 
205 	hubdi->hubdi_dip = dip;
206 	hubdi->hubdi_flags = flags;
207 
208 	usba_device->usb_hubdi = hubdi;
209 
210 	/*
211 	 * add this hubdi instance to the list of known hubdi's
212 	 */
213 	usba_init_list(&hubdi->hubdi_list, (usb_opaque_t)hubdi,
214 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)->
215 	    hcdi_iblock_cookie);
216 	mutex_enter(&usba_hubdi_mutex);
217 	usba_add_to_list(&usba_hubdi_list, &hubdi->hubdi_list);
218 	mutex_exit(&usba_hubdi_mutex);
219 
220 	return (DDI_SUCCESS);
221 }
222 
223 
224 /*
225  * Called by an	HUB to detach an instance of the driver
226  */
227 int
228 usba_hubdi_unregister(dev_info_t *dip)
229 {
230 	usba_device_t *usba_device = usba_get_usba_device(dip);
231 	usba_hubdi_t *hubdi = usba_device->usb_hubdi;
232 
233 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
234 	    "usba_hubdi_unregister: %s", ddi_node_name(dip));
235 
236 	mutex_enter(&usba_hubdi_mutex);
237 	(void) usba_rm_from_list(&usba_hubdi_list, &hubdi->hubdi_list);
238 	mutex_exit(&usba_hubdi_mutex);
239 
240 	usba_destroy_list(&hubdi->hubdi_list);
241 
242 	kmem_free(hubdi, sizeof (usba_hubdi_t));
243 
244 	return (DDI_SUCCESS);
245 }
246 
247 
248 /*
249  * misc bus routines currently not used
250  */
251 /*ARGSUSED*/
252 static int
253 usba_hubdi_map_fault(dev_info_t *dip,
254 	dev_info_t	*rdip,
255 	struct hat	*hat,
256 	struct seg	*seg,
257 	caddr_t 	addr,
258 	struct devpage	*dp,
259 	pfn_t		pfn,
260 	uint_t		prot,
261 	uint_t		lock)
262 {
263 	return (DDI_FAILURE);
264 }
265 
266 
267 /*
268  * root hub support. the root hub uses the same devi as the HCD
269  */
270 int
271 usba_hubdi_bind_root_hub(dev_info_t *dip,
272 	uchar_t	*root_hub_config_descriptor,
273 	size_t config_length,
274 	usb_dev_descr_t *root_hub_device_descriptor)
275 {
276 	usba_device_t *usba_device;
277 	usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
278 	hubd_t	*root_hubd;
279 	usb_pipe_handle_t ph = NULL;
280 	dev_info_t *child = ddi_get_child(dip);
281 
282 	if (ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
283 	    "root-hub") != NDI_SUCCESS) {
284 
285 		return (USB_FAILURE);
286 	}
287 
288 	usba_add_root_hub(dip);
289 
290 	root_hubd = kmem_zalloc(sizeof (hubd_t), KM_SLEEP);
291 
292 	/*
293 	 * create and initialize a usba_device structure
294 	 */
295 	usba_device = usba_alloc_usba_device(dip);
296 
297 	mutex_enter(&usba_device->usb_mutex);
298 	usba_device->usb_hcdi_ops = hcdi->hcdi_ops;
299 	usba_device->usb_cfg = root_hub_config_descriptor;
300 	usba_device->usb_cfg_length = config_length;
301 	usba_device->usb_dev_descr = root_hub_device_descriptor;
302 	usba_device->usb_port = 1;
303 	usba_device->usb_addr = ROOT_HUB_ADDR;
304 	usba_device->usb_root_hubd = root_hubd;
305 	usba_device->usb_cfg_array = kmem_zalloc(sizeof (uchar_t *),
306 	    KM_SLEEP);
307 	usba_device->usb_cfg_array_length = sizeof (uchar_t *);
308 
309 	usba_device->usb_cfg_array_len = kmem_zalloc(sizeof (uint16_t),
310 	    KM_SLEEP);
311 	usba_device->usb_cfg_array_len_length = sizeof (uint16_t);
312 
313 	usba_device->usb_cfg_array[0] = root_hub_config_descriptor;
314 	usba_device->usb_cfg_array_len[0] =
315 	    sizeof (root_hub_config_descriptor);
316 
317 	usba_device->usb_cfg_str_descr = kmem_zalloc(sizeof (uchar_t *),
318 	    KM_SLEEP);
319 	usba_device->usb_n_cfgs = 1;
320 	usba_device->usb_n_ifs = 1;
321 	usba_device->usb_dip = dip;
322 
323 	usba_device->usb_client_flags = kmem_zalloc(
324 	    usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
325 
326 	usba_device->usb_client_attach_list = kmem_zalloc(
327 	    usba_device->usb_n_ifs *
328 	    sizeof (*usba_device->usb_client_attach_list), KM_SLEEP);
329 
330 	usba_device->usb_client_ev_cb_list = kmem_zalloc(
331 	    usba_device->usb_n_ifs *
332 	    sizeof (*usba_device->usb_client_ev_cb_list), KM_SLEEP);
333 
334 	/*
335 	 * The bDeviceProtocol field of root hub device specifies,
336 	 * whether root hub is a Super, High, or Full speed usb device.
337 	 */
338 	if (root_hub_device_descriptor->bDeviceProtocol >= 0x3) {
339 		usba_device->usb_port_status = USBA_SUPER_SPEED_DEV;
340 	} else if (root_hub_device_descriptor->bDeviceProtocol > 0) {
341 		usba_device->usb_port_status = USBA_HIGH_SPEED_DEV;
342 	} else {
343 		usba_device->usb_port_status = USBA_FULL_SPEED_DEV;
344 	}
345 
346 	mutex_exit(&usba_device->usb_mutex);
347 
348 	usba_set_usba_device(dip, usba_device);
349 
350 	/*
351 	 * For the root hub the default pipe is not yet open
352 	 */
353 	if (usb_pipe_open(dip, NULL, NULL,
354 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != USB_SUCCESS) {
355 		goto fail;
356 	}
357 
358 	/*
359 	 * kill off all OBP children, they may not be fully
360 	 * enumerated
361 	 */
362 	while (child) {
363 		dev_info_t *next = ddi_get_next_sibling(child);
364 		(void) ddi_remove_child(child, 0);
365 		child = next;
366 	}
367 
368 	/*
369 	 * "attach" the root hub driver
370 	 */
371 	if (usba_hubdi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
372 		goto fail;
373 	}
374 
375 	return (USB_SUCCESS);
376 
377 fail:
378 	if (ph) {
379 		usb_pipe_close(dip, ph,
380 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
381 	}
382 
383 	kmem_free(usba_device->usb_cfg_array,
384 	    usba_device->usb_cfg_array_length);
385 	kmem_free(usba_device->usb_cfg_array_len,
386 	    usba_device->usb_cfg_array_len_length);
387 
388 	kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
389 
390 	usba_free_usba_device(usba_device);
391 
392 	usba_set_usba_device(dip, NULL);
393 
394 	if (root_hubd) {
395 		kmem_free(root_hubd, sizeof (hubd_t));
396 	}
397 
398 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
399 
400 	usba_rem_root_hub(dip);
401 
402 	return (USB_FAILURE);
403 }
404 
405 
406 int
407 usba_hubdi_unbind_root_hub(dev_info_t *dip)
408 {
409 	usba_device_t *usba_device;
410 
411 	/* was root hub attached? */
412 	if (!(usba_is_root_hub(dip))) {
413 
414 		/* return success anyway */
415 		return (USB_SUCCESS);
416 	}
417 
418 	/*
419 	 * usba_hubdi_detach also closes the default pipe
420 	 * and removes properties so there is no need to
421 	 * do it here
422 	 */
423 	if (usba_hubdi_detach(dip, DDI_DETACH) != DDI_SUCCESS) {
424 
425 		if (DEVI_IS_ATTACHING(dip)) {
426 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
427 			    "failure to unbind root hub after attach failure");
428 		}
429 
430 		return (USB_FAILURE);
431 	}
432 
433 	usba_device = usba_get_usba_device(dip);
434 
435 	kmem_free(usba_device->usb_root_hubd, sizeof (hubd_t));
436 
437 	kmem_free(usba_device->usb_cfg_array,
438 	    usba_device->usb_cfg_array_length);
439 	kmem_free(usba_device->usb_cfg_array_len,
440 	    usba_device->usb_cfg_array_len_length);
441 
442 	kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
443 
444 	usba_free_usba_device(usba_device);
445 
446 	usba_rem_root_hub(dip);
447 
448 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
449 
450 	return (USB_SUCCESS);
451 }
452 
453 
454 /*
455  * Actual Hub Driver support code:
456  *	shared by root hub and non-root hubs
457  */
458 #include <sys/usb/usba/usbai_version.h>
459 
460 /* Debugging support */
461 uint_t hubd_errlevel	= USB_LOG_L4;
462 uint_t hubd_errmask	= (uint_t)DPRINT_MASK_ALL;
463 uint_t hubd_instance_debug = (uint_t)-1;
464 static uint_t hubdi_bus_config_debug = 0;
465 
466 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel))
467 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask))
468 _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug))
469 
470 _NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
471 _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
472 
473 
474 /*
475  * local variables:
476  *
477  * Amount of time to wait between resetting the port and accessing
478  * the device.	The value is in microseconds.
479  */
480 static uint_t hubd_device_delay = 1000000;
481 
482 /*
483  * enumeration retry
484  */
485 #define	HUBD_PORT_RETRY 5
486 static uint_t hubd_retry_enumerate = HUBD_PORT_RETRY;
487 
488 /*
489  * Stale hotremoved device cleanup delay
490  */
491 #define	HUBD_STALE_DIP_CLEANUP_DELAY	5000000
492 static uint_t hubd_dip_cleanup_delay = HUBD_STALE_DIP_CLEANUP_DELAY;
493 
494 /*
495  * retries for USB suspend and resume
496  */
497 #define	HUBD_SUS_RES_RETRY	2
498 
499 void	*hubd_statep;
500 
501 /*
502  * prototypes
503  */
504 static int hubd_cleanup(dev_info_t *dip, hubd_t  *hubd);
505 static int hubd_check_ports(hubd_t  *hubd);
506 
507 static int  hubd_open_intr_pipe(hubd_t *hubd);
508 static void hubd_start_polling(hubd_t *hubd, int always);
509 static void hubd_stop_polling(hubd_t *hubd);
510 static void hubd_close_intr_pipe(hubd_t *hubd);
511 
512 static void hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *req);
513 static void hubd_exception_cb(usb_pipe_handle_t pipe,
514 						usb_intr_req_t *req);
515 static void hubd_hotplug_thread(void *arg);
516 static void hubd_reset_thread(void *arg);
517 static int hubd_create_child(dev_info_t *dip,
518 		hubd_t		*hubd,
519 		usba_device_t	*usba_device,
520 		usb_port_status_t port_status,
521 		usb_port_t	port,
522 		int		iteration);
523 
524 static int hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag,
525 	boolean_t retry);
526 
527 static int hubd_get_hub_descriptor(hubd_t *hubd);
528 
529 static int hubd_set_hub_depth(hubd_t *hubd);
530 
531 static int hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status);
532 
533 static int hubd_reset_port(hubd_t *hubd, usb_port_t port);
534 
535 static int hubd_get_hub_status(hubd_t *hubd);
536 
537 static int hubd_handle_port_connect(hubd_t *hubd, usb_port_t port);
538 
539 static int hubd_disable_port(hubd_t *hubd, usb_port_t port);
540 
541 static int hubd_enable_port(hubd_t *hubd, usb_port_t port);
542 static int hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port);
543 
544 static int hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
545 	uint16_t *status, uint16_t *change, usb_port_status_t *speed,
546 	uint_t ack_flag);
547 
548 static int hubd_enable_all_port_power(hubd_t *hubd);
549 static int hubd_disable_all_port_power(hubd_t *hubd);
550 static int hubd_disable_port_power(hubd_t *hubd, usb_port_t port);
551 static int hubd_enable_port_power(hubd_t *hubd, usb_port_t port);
552 
553 static void hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device);
554 
555 static int hubd_can_suspend(hubd_t *hubd);
556 static void hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd);
557 static int hubd_setdevaddr(hubd_t *hubd, usb_port_t port);
558 static void hubd_setdevconfig(hubd_t *hubd, usb_port_t port);
559 
560 static int hubd_register_events(hubd_t *hubd);
561 static void hubd_do_callback(hubd_t *hubd, dev_info_t *dip,
562 	ddi_eventcookie_t cookie);
563 static void hubd_run_callbacks(hubd_t *hubd, usba_event_t type);
564 static void hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type);
565 static void hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd);
566 
567 static int hubd_disconnect_event_cb(dev_info_t *dip);
568 static int hubd_reconnect_event_cb(dev_info_t *dip);
569 static int hubd_pre_suspend_event_cb(dev_info_t *dip);
570 static int hubd_post_resume_event_cb(dev_info_t *dip);
571 static int hubd_cpr_suspend(hubd_t *hubd);
572 static void hubd_cpr_resume(dev_info_t *dip);
573 static int hubd_restore_state_cb(dev_info_t *dip);
574 static int hubd_check_same_device(hubd_t *hubd, usb_port_t port);
575 
576 static int hubd_init_power_budget(hubd_t *hubd);
577 
578 static ndi_event_definition_t hubd_ndi_event_defs[] = {
579 	{USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
580 						NDI_EVENT_POST_TO_ALL},
581 	{USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
582 						NDI_EVENT_POST_TO_ALL},
583 	{USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
584 						NDI_EVENT_POST_TO_ALL},
585 	{USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
586 						NDI_EVENT_POST_TO_ALL}
587 };
588 
589 #define	HUBD_N_NDI_EVENTS \
590 	(sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t))
591 
592 static ndi_event_set_t hubd_ndi_events = {
593 	NDI_EVENTS_REV1, HUBD_N_NDI_EVENTS, hubd_ndi_event_defs};
594 
595 /* events received from parent */
596 static usb_event_t hubd_events = {
597 	hubd_disconnect_event_cb,
598 	hubd_reconnect_event_cb,
599 	hubd_pre_suspend_event_cb,
600 	hubd_post_resume_event_cb
601 };
602 
603 
604 /*
605  * hubd_get_soft_state() returns the hubd soft state
606  */
607 hubd_t *
608 hubd_get_soft_state(dev_info_t *dip)
609 {
610 	if (dip == NULL) {
611 		return (NULL);
612 	}
613 
614 	if (usba_is_root_hub(dip)) {
615 		usba_device_t *usba_device = usba_get_usba_device(dip);
616 
617 		return (usba_device->usb_root_hubd);
618 	} else {
619 		int instance = ddi_get_instance(dip);
620 
621 		return (ddi_get_soft_state(hubd_statep, instance));
622 	}
623 }
624 
625 
626 /*
627  * PM support functions:
628  */
629 /*ARGSUSED*/
630 static void
631 hubd_pm_busy_component(hubd_t *hubd, dev_info_t *dip, int component)
632 {
633 	if (hubd->h_hubpm != NULL) {
634 		hubd->h_hubpm->hubp_busy_pm++;
635 		mutex_exit(HUBD_MUTEX(hubd));
636 		if (pm_busy_component(dip, 0) != DDI_SUCCESS) {
637 			mutex_enter(HUBD_MUTEX(hubd));
638 			hubd->h_hubpm->hubp_busy_pm--;
639 			mutex_exit(HUBD_MUTEX(hubd));
640 		}
641 		mutex_enter(HUBD_MUTEX(hubd));
642 		USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
643 		    "hubd_pm_busy_component: %d", hubd->h_hubpm->hubp_busy_pm);
644 	}
645 }
646 
647 
648 /*ARGSUSED*/
649 static void
650 hubd_pm_idle_component(hubd_t *hubd, dev_info_t *dip, int component)
651 {
652 	if (hubd->h_hubpm != NULL) {
653 		mutex_exit(HUBD_MUTEX(hubd));
654 		if (pm_idle_component(dip, 0) == DDI_SUCCESS) {
655 			mutex_enter(HUBD_MUTEX(hubd));
656 			ASSERT(hubd->h_hubpm->hubp_busy_pm > 0);
657 			hubd->h_hubpm->hubp_busy_pm--;
658 			mutex_exit(HUBD_MUTEX(hubd));
659 		}
660 		mutex_enter(HUBD_MUTEX(hubd));
661 		USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
662 		    "hubd_pm_idle_component: %d", hubd->h_hubpm->hubp_busy_pm);
663 	}
664 }
665 
666 
667 /*
668  * track power level changes for children of this instance
669  */
670 static void
671 hubd_set_child_pwrlvl(hubd_t *hubd, usb_port_t port, uint8_t power)
672 {
673 	int	old_power, new_power, pwr;
674 	usb_port_t	portno;
675 	hub_power_t	*hubpm;
676 
677 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
678 	    "hubd_set_child_pwrlvl: port=%d power=%d",
679 	    port, power);
680 
681 	mutex_enter(HUBD_MUTEX(hubd));
682 	hubpm = hubd->h_hubpm;
683 
684 	old_power = 0;
685 	for (portno = 1; portno <= hubd->h_nports; portno++) {
686 		old_power += hubpm->hubp_child_pwrstate[portno];
687 	}
688 
689 	/* assign the port power */
690 	pwr = hubd->h_hubpm->hubp_child_pwrstate[port];
691 	hubd->h_hubpm->hubp_child_pwrstate[port] = power;
692 	new_power = old_power - pwr + power;
693 
694 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
695 	    "hubd_set_child_pwrlvl: new_power=%d old_power=%d",
696 	    new_power, old_power);
697 
698 	if ((new_power > 0) && (old_power == 0)) {
699 		/* we have the first child coming out of low power */
700 		(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
701 	} else if ((new_power == 0) && (old_power > 0)) {
702 		/* we have the last child going to low power */
703 		(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
704 	}
705 	mutex_exit(HUBD_MUTEX(hubd));
706 }
707 
708 
709 /*
710  * given a child dip, locate its port number
711  */
712 static usb_port_t
713 hubd_child_dip2port(hubd_t *hubd, dev_info_t *dip)
714 {
715 	usb_port_t	port;
716 
717 	mutex_enter(HUBD_MUTEX(hubd));
718 	for (port = 1; port <= hubd->h_nports; port++) {
719 		if (hubd->h_children_dips[port] == dip) {
720 
721 			break;
722 		}
723 	}
724 	ASSERT(port <= hubd->h_nports);
725 	mutex_exit(HUBD_MUTEX(hubd));
726 
727 	return (port);
728 }
729 
730 
731 /*
732  * if the hub can be put into low power mode, return success
733  * NOTE: suspend here means going to lower power, not CPR suspend.
734  */
735 static int
736 hubd_can_suspend(hubd_t *hubd)
737 {
738 	hub_power_t	*hubpm;
739 	int		total_power = 0;
740 	usb_port_t	port;
741 
742 	hubpm = hubd->h_hubpm;
743 
744 	if (DEVI_IS_DETACHING(hubd->h_dip)) {
745 
746 		return (USB_SUCCESS);
747 	}
748 
749 	/*
750 	 * Don't go to lower power if haven't been at full power for enough
751 	 * time to let hotplug thread kickoff.
752 	 */
753 	if (gethrtime() < (hubpm->hubp_time_at_full_power +
754 	    hubpm->hubp_min_pm_threshold)) {
755 
756 		return (USB_FAILURE);
757 	}
758 
759 	for (port = 1; (total_power == 0) &&
760 	    (port <= hubd->h_nports); port++) {
761 		total_power += hubpm->hubp_child_pwrstate[port];
762 	}
763 
764 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
765 	    "hubd_can_suspend: %d", total_power);
766 
767 	return (total_power ? USB_FAILURE : USB_SUCCESS);
768 }
769 
770 
771 /*
772  * resume port depending on current device state
773  */
774 static int
775 hubd_resume_port(hubd_t *hubd, usb_port_t port)
776 {
777 	int		rval, retry;
778 	usb_cr_t	completion_reason;
779 	usb_cb_flags_t	cb_flags;
780 	uint16_t	status;
781 	uint16_t	change;
782 	int		retval = USB_FAILURE;
783 
784 	mutex_enter(HUBD_MUTEX(hubd));
785 
786 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
787 	    "hubd_resume_port: port=%d state=0x%x (%s)", port,
788 	    hubd->h_dev_state, usb_str_dev_state(hubd->h_dev_state));
789 
790 	switch (hubd->h_dev_state) {
791 	case USB_DEV_HUB_CHILD_PWRLVL:
792 		/*
793 		 * This could be a bus ctl for a port other than the one
794 		 * that has a remote wakeup condition. So check.
795 		 */
796 		if ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0) {
797 			/* the port isn't suspended, so don't resume */
798 			retval = USB_SUCCESS;
799 
800 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
801 			    "hubd_resume_port: port=%d not suspended", port);
802 
803 			break;
804 		}
805 		/*
806 		 * Device has initiated a wakeup.
807 		 * Issue a ClearFeature(PortSuspend)
808 		 */
809 		mutex_exit(HUBD_MUTEX(hubd));
810 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
811 		    hubd->h_default_pipe,
812 		    HUB_HANDLE_PORT_FEATURE_TYPE,
813 		    USB_REQ_CLEAR_FEATURE,
814 		    CFS_PORT_SUSPEND,
815 		    port,
816 		    0, NULL, 0,
817 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
818 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
819 			    "ClearFeature(PortSuspend) fails "
820 			    "rval=%d cr=%d cb=0x%x", rval,
821 			    completion_reason, cb_flags);
822 		}
823 		mutex_enter(HUBD_MUTEX(hubd));
824 
825 		/* either way ack changes on the port */
826 		(void) hubd_determine_port_status(hubd, port,
827 		    &status, &change, NULL, PORT_CHANGE_PSSC);
828 		retval = USB_SUCCESS;
829 
830 		break;
831 	case USB_DEV_HUB_STATE_RECOVER:
832 		/*
833 		 * When hubd's connect event callback posts a connect
834 		 * event to its child, it results in this busctl call
835 		 * which is valid
836 		 */
837 		/* FALLTHRU */
838 	case USB_DEV_ONLINE:
839 		if (((hubd->h_port_state[port] & PORT_STATUS_CCS) == 0) ||
840 		    ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0)) {
841 			/*
842 			 * the port isn't suspended, or connected
843 			 * so don't resume
844 			 */
845 			retval = USB_SUCCESS;
846 
847 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
848 			    "hubd_resume_port: port=%d not suspended", port);
849 
850 			break;
851 		}
852 		/*
853 		 * prevent kicking off the hotplug thread
854 		 */
855 		hubd->h_hotplug_thread++;
856 		hubd_stop_polling(hubd);
857 
858 		/* Now ClearFeature(PortSuspend) */
859 		for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
860 			mutex_exit(HUBD_MUTEX(hubd));
861 			rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
862 			    hubd->h_default_pipe,
863 			    HUB_HANDLE_PORT_FEATURE_TYPE,
864 			    USB_REQ_CLEAR_FEATURE,
865 			    CFS_PORT_SUSPEND,
866 			    port,
867 			    0, NULL, 0,
868 			    &completion_reason, &cb_flags, 0);
869 			mutex_enter(HUBD_MUTEX(hubd));
870 			if (rval != USB_SUCCESS) {
871 				USB_DPRINTF_L2(DPRINT_MASK_PM,
872 				    hubd->h_log_handle,
873 				    "ClearFeature(PortSuspend) fails"
874 				    "rval=%d cr=%d cb=0x%x", rval,
875 				    completion_reason, cb_flags);
876 			} else {
877 				/*
878 				 * As per spec section 11.9 and 7.1.7.7
879 				 * hub need to provide at least 20ms of
880 				 * resume signalling, and s/w provide 10ms of
881 				 * recovery time before accessing the port.
882 				 */
883 				mutex_exit(HUBD_MUTEX(hubd));
884 				delay(drv_usectohz(40000));
885 				mutex_enter(HUBD_MUTEX(hubd));
886 				(void) hubd_determine_port_status(hubd, port,
887 				    &status, &change, NULL, PORT_CHANGE_PSSC);
888 
889 				if ((status & PORT_STATUS_PSS) == 0) {
890 					/* the port did finally resume */
891 					retval = USB_SUCCESS;
892 
893 					break;
894 				}
895 			}
896 		}
897 
898 		/* allow hotplug thread again */
899 		hubd->h_hotplug_thread--;
900 		hubd_start_polling(hubd, 0);
901 
902 		break;
903 	case USB_DEV_DISCONNECTED:
904 		/* Ignore - NO Operation */
905 		retval = USB_SUCCESS;
906 
907 		break;
908 	case USB_DEV_SUSPENDED:
909 	case USB_DEV_PWRED_DOWN:
910 	default:
911 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
912 		    "Improper state for port Resume");
913 
914 		break;
915 	}
916 	mutex_exit(HUBD_MUTEX(hubd));
917 
918 	return (retval);
919 }
920 
921 
922 /*
923  * suspend port depending on device state
924  */
925 static int
926 hubd_suspend_port(hubd_t *hubd, usb_port_t port)
927 {
928 	int		rval, retry;
929 	int		retval = USB_FAILURE;
930 	usb_cr_t	completion_reason;
931 	usb_cb_flags_t	cb_flags;
932 	uint16_t	status;
933 	uint16_t	change;
934 
935 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
936 	    "hubd_suspend_port: port=%d", port);
937 
938 	mutex_enter(HUBD_MUTEX(hubd));
939 
940 	switch (hubd->h_dev_state) {
941 	case USB_DEV_HUB_STATE_RECOVER:
942 		/*
943 		 * When hubd's connect event callback posts a connect
944 		 * event to its child, it results in this busctl call
945 		 * which is valid
946 		 */
947 		/* FALLTHRU */
948 	case USB_DEV_HUB_CHILD_PWRLVL:
949 		/*
950 		 * When one child is resuming, the other could timeout
951 		 * and go to low power mode, which is valid
952 		 */
953 		/* FALLTHRU */
954 	case USB_DEV_ONLINE:
955 		hubd->h_hotplug_thread++;
956 		hubd_stop_polling(hubd);
957 
958 		/*
959 		 * Some devices start an unprovoked resume.  According to spec,
960 		 * normal resume time for port is 10ms.  Wait for double that
961 		 * time, then check to be sure port is really suspended.
962 		 */
963 		for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
964 			/* Now SetFeature(PortSuspend) */
965 			mutex_exit(HUBD_MUTEX(hubd));
966 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
967 			    hubd->h_default_pipe,
968 			    HUB_HANDLE_PORT_FEATURE_TYPE,
969 			    USB_REQ_SET_FEATURE,
970 			    CFS_PORT_SUSPEND,
971 			    port,
972 			    0, NULL, 0,
973 			    &completion_reason, &cb_flags, 0)) !=
974 			    USB_SUCCESS) {
975 				USB_DPRINTF_L2(DPRINT_MASK_PM,
976 				    hubd->h_log_handle,
977 				    "SetFeature(PortSuspend) fails"
978 				    "rval=%d cr=%d cb=0x%x",
979 				    rval, completion_reason, cb_flags);
980 			}
981 
982 			/*
983 			 * some devices start an unprovoked resume
984 			 * wait and check port status after some time
985 			 */
986 			delay(drv_usectohz(20000));
987 
988 			/* either ways ack changes on the port */
989 			mutex_enter(HUBD_MUTEX(hubd));
990 			(void) hubd_determine_port_status(hubd, port,
991 			    &status, &change, NULL, PORT_CHANGE_PSSC);
992 			if (status & PORT_STATUS_PSS) {
993 				/* the port is indeed suspended */
994 				retval = USB_SUCCESS;
995 
996 				break;
997 			} else {
998 				USB_DPRINTF_L0(DPRINT_MASK_PM,
999 				    hubd->h_log_handle,
1000 				    "hubdi: port%d failed to be suspended!",
1001 				    port);
1002 			}
1003 		}
1004 
1005 		hubd->h_hotplug_thread--;
1006 		hubd_start_polling(hubd, 0);
1007 
1008 		break;
1009 
1010 	case USB_DEV_DISCONNECTED:
1011 		/* Ignore - No Operation */
1012 		retval = USB_SUCCESS;
1013 
1014 		break;
1015 
1016 	case USB_DEV_SUSPENDED:
1017 	case USB_DEV_PWRED_DOWN:
1018 	default:
1019 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1020 		    "Improper state for port Suspend");
1021 
1022 		break;
1023 	}
1024 	mutex_exit(HUBD_MUTEX(hubd));
1025 
1026 	return (retval);
1027 }
1028 
1029 
1030 /*
1031  * child post attach/detach notifications
1032  */
1033 static void
1034 hubd_post_attach(hubd_t *hubd, usb_port_t port, struct attachspec *as)
1035 {
1036 	dev_info_t	*dip;
1037 
1038 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
1039 	    "hubd_post_attach: port=%d result=%d",
1040 	    port, as->result);
1041 
1042 	if (as->result == DDI_SUCCESS) {
1043 		/*
1044 		 * Check if the child created wants to be power managed.
1045 		 * If yes, the childs power level gets automatically tracked
1046 		 * by DDI_CTLOPS_POWER busctl.
1047 		 * If no, we set power of the new child by default
1048 		 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
1049 		 */
1050 		mutex_enter(HUBD_MUTEX(hubd));
1051 		dip = hubd->h_children_dips[port];
1052 		mutex_exit(HUBD_MUTEX(hubd));
1053 		if (DEVI(dip)->devi_pm_info == NULL) {
1054 			hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_FULL_PWR);
1055 		}
1056 	}
1057 }
1058 
1059 
1060 static void
1061 hubd_post_detach(hubd_t *hubd, usb_port_t port, struct detachspec *ds)
1062 {
1063 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
1064 	    "hubd_post_detach: port=%d result=%d", port, ds->result);
1065 
1066 	/*
1067 	 * if the device is successfully detached and is the
1068 	 * last device to detach, mark component as idle
1069 	 */
1070 	mutex_enter(HUBD_MUTEX(hubd));
1071 	if (ds->result == DDI_SUCCESS) {
1072 		usba_device_t	*usba_device = hubd->h_usba_devices[port];
1073 		dev_info_t	*pdip = hubd->h_dip;
1074 		mutex_exit(HUBD_MUTEX(hubd));
1075 
1076 		usba_hubdi_incr_power_budget(pdip, usba_device);
1077 
1078 		/*
1079 		 * We set power of the detached child
1080 		 * to 0, so that we can suspend if all
1081 		 * our children are gone
1082 		 */
1083 		hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_PWR_OFF);
1084 
1085 		/* check for leaks on detaching */
1086 		if ((usba_device) && (ds->cmd == DDI_DETACH)) {
1087 			usba_check_for_leaks(usba_device);
1088 		}
1089 	} else {
1090 		mutex_exit(HUBD_MUTEX(hubd));
1091 	}
1092 }
1093 
1094 
1095 /*
1096  * hubd_post_power
1097  *	After the child's power entry point has been called
1098  *	we record its power level in our local struct.
1099  *	If the device has powered off, we suspend port
1100  */
1101 static int
1102 hubd_post_power(hubd_t *hubd, usb_port_t port, pm_bp_child_pwrchg_t *bpc,
1103     int result)
1104 {
1105 	int	retval = USB_SUCCESS;
1106 
1107 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1108 	    "hubd_post_power: port=%d", port);
1109 
1110 	if (result == DDI_SUCCESS) {
1111 
1112 		/* record this power in our local struct */
1113 		hubd_set_child_pwrlvl(hubd, port, bpc->bpc_nlevel);
1114 
1115 		if (bpc->bpc_nlevel == USB_DEV_OS_PWR_OFF) {
1116 
1117 			/* now suspend the port */
1118 			retval = hubd_suspend_port(hubd, port);
1119 		} else if (bpc->bpc_nlevel == USB_DEV_OS_FULL_PWR) {
1120 
1121 			/* make sure the port is resumed */
1122 			retval = hubd_resume_port(hubd, port);
1123 		}
1124 	} else {
1125 
1126 		/* record old power in our local struct */
1127 		hubd_set_child_pwrlvl(hubd, port, bpc->bpc_olevel);
1128 
1129 		if (bpc->bpc_olevel == USB_DEV_OS_PWR_OFF) {
1130 
1131 			/*
1132 			 * As this device failed to transition from
1133 			 * power off state, suspend the port again
1134 			 */
1135 			retval = hubd_suspend_port(hubd, port);
1136 		}
1137 	}
1138 
1139 	return (retval);
1140 }
1141 
1142 
1143 /*
1144  * bus ctl notifications are handled here, the rest goes up to root hub/hcd
1145  */
1146 static int
1147 usba_hubdi_bus_ctl(dev_info_t *dip,
1148 	dev_info_t	*rdip,
1149 	ddi_ctl_enum_t	op,
1150 	void		*arg,
1151 	void		*result)
1152 {
1153 	usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
1154 	dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
1155 	struct attachspec *as;
1156 	struct detachspec *ds;
1157 	hubd_t		*hubd;
1158 	usb_port_t	port;
1159 	int		circ, rval;
1160 	int		retval = DDI_FAILURE;
1161 
1162 	hubd = hubd_get_soft_state(dip);
1163 
1164 	mutex_enter(HUBD_MUTEX(hubd));
1165 
1166 	/* flag that we are currently running bus_ctl */
1167 	hubd->h_bus_ctls++;
1168 	mutex_exit(HUBD_MUTEX(hubd));
1169 
1170 	USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1171 	    "usba_hubdi_bus_ctl:\n\t"
1172 	    "dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p",
1173 	    (void *)dip, (void *)rdip, op, arg);
1174 
1175 	switch (op) {
1176 	case DDI_CTLOPS_ATTACH:
1177 		as = (struct attachspec *)arg;
1178 		port = hubd_child_dip2port(hubd, rdip);
1179 
1180 		/* there is nothing to do at resume time */
1181 		if (as->cmd == DDI_RESUME) {
1182 			break;
1183 		}
1184 
1185 		/* serialize access */
1186 		ndi_devi_enter(hubd->h_dip, &circ);
1187 
1188 		switch (as->when) {
1189 		case DDI_PRE:
1190 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1191 			    "DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1192 			    (void *)rdip, port);
1193 
1194 			mutex_enter(HUBD_MUTEX(hubd));
1195 			hubd->h_port_state[port] |= HUBD_CHILD_ATTACHING;
1196 
1197 			/* Go busy here.  Matching idle is DDI_POST case. */
1198 			(void) hubd_pm_busy_component(hubd, dip, 0);
1199 			mutex_exit(HUBD_MUTEX(hubd));
1200 
1201 			/*
1202 			 * if we suspended the port previously
1203 			 * because child went to low power state, and
1204 			 * someone unloaded the driver, the port would
1205 			 * still be suspended and needs to be resumed
1206 			 */
1207 			rval = hubd_resume_port(hubd, port);
1208 			if (rval == USB_SUCCESS) {
1209 				retval = DDI_SUCCESS;
1210 			}
1211 
1212 			break;
1213 		case DDI_POST:
1214 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1215 			    "DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1216 			    (void *)rdip, port);
1217 
1218 			mutex_enter(HUBD_MUTEX(hubd));
1219 			hubd->h_port_state[port] &= ~HUBD_CHILD_ATTACHING;
1220 			mutex_exit(HUBD_MUTEX(hubd));
1221 
1222 			hubd_post_attach(hubd, port, (struct attachspec *)arg);
1223 			retval = DDI_SUCCESS;
1224 			mutex_enter(HUBD_MUTEX(hubd));
1225 
1226 			/* Matching idle call for DDI_PRE busy call. */
1227 			(void) hubd_pm_idle_component(hubd, dip, 0);
1228 			mutex_exit(HUBD_MUTEX(hubd));
1229 		}
1230 		ndi_devi_exit(hubd->h_dip, circ);
1231 
1232 		break;
1233 	case DDI_CTLOPS_DETACH:
1234 		ds = (struct detachspec *)arg;
1235 		port = hubd_child_dip2port(hubd, rdip);
1236 
1237 		/* there is nothing to do at suspend time */
1238 		if (ds->cmd == DDI_SUSPEND) {
1239 			break;
1240 		}
1241 
1242 		/* serialize access */
1243 		ndi_devi_enter(hubd->h_dip, &circ);
1244 
1245 		switch (ds->when) {
1246 		case DDI_PRE:
1247 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1248 			    "DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d",
1249 			    (void *)rdip, port);
1250 
1251 			mutex_enter(HUBD_MUTEX(hubd));
1252 			hubd->h_port_state[port] |= HUBD_CHILD_DETACHING;
1253 
1254 			/* Go busy here.  Matching idle is DDI_POST case. */
1255 			(void) hubd_pm_busy_component(hubd, dip, 0);
1256 
1257 			mutex_exit(HUBD_MUTEX(hubd));
1258 			retval = DDI_SUCCESS;
1259 
1260 			break;
1261 		case DDI_POST:
1262 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1263 			    "DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d",
1264 			    (void *)rdip, port);
1265 
1266 			mutex_enter(HUBD_MUTEX(hubd));
1267 			hubd->h_port_state[port] &= ~HUBD_CHILD_DETACHING;
1268 			mutex_exit(HUBD_MUTEX(hubd));
1269 
1270 			/* Matching idle call for DDI_PRE busy call. */
1271 			hubd_post_detach(hubd, port, (struct detachspec *)arg);
1272 			retval = DDI_SUCCESS;
1273 			mutex_enter(HUBD_MUTEX(hubd));
1274 			(void) hubd_pm_idle_component(hubd, dip, 0);
1275 			mutex_exit(HUBD_MUTEX(hubd));
1276 
1277 			break;
1278 		}
1279 		ndi_devi_exit(hubd->h_dip, circ);
1280 
1281 		break;
1282 	default:
1283 		retval = usba_bus_ctl(root_hub_dip, rdip, op, arg, result);
1284 	}
1285 
1286 	/* decrement bus_ctls count */
1287 	mutex_enter(HUBD_MUTEX(hubd));
1288 	hubd->h_bus_ctls--;
1289 	ASSERT(hubd->h_bus_ctls >= 0);
1290 	mutex_exit(HUBD_MUTEX(hubd));
1291 
1292 	return (retval);
1293 }
1294 
1295 /*
1296  * hubd_config_one:
1297  * 	enumerate one child according to 'port'
1298  */
1299 
1300 static boolean_t
1301 hubd_config_one(hubd_t *hubd, int port)
1302 {
1303 	dev_info_t	*hdip = hubd->h_dip;
1304 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
1305 	boolean_t	online_child = B_FALSE, found = B_FALSE;
1306 	int		prh_circ, rh_circ, circ;
1307 
1308 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
1309 	    "hubd_config_one:  started, hubd_reset_port = 0x%x", port);
1310 
1311 	ndi_hold_devi(hdip); /* so we don't race with detach */
1312 
1313 	/*
1314 	 * this ensures one config activity per system at a time.
1315 	 * we enter the parent PCI node to have this serialization.
1316 	 * this also excludes ioctls and deathrow thread
1317 	 */
1318 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
1319 	ndi_devi_enter(rh_dip, &rh_circ);
1320 
1321 	/* exclude other threads */
1322 	ndi_devi_enter(hdip, &circ);
1323 	mutex_enter(HUBD_MUTEX(hubd));
1324 
1325 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
1326 
1327 	if (!hubd->h_children_dips[port]) {
1328 		uint16_t	status, change;
1329 
1330 		(void) hubd_determine_port_status(hubd, port,
1331 		    &status, &change, NULL, HUBD_ACK_ALL_CHANGES);
1332 
1333 		if (status & PORT_STATUS_CCS) {
1334 			online_child |=	(hubd_handle_port_connect(hubd,
1335 			    port) == USB_SUCCESS);
1336 			found = online_child;
1337 		}
1338 	} else {
1339 		found = B_TRUE;
1340 	}
1341 
1342 	mutex_exit(HUBD_MUTEX(hubd));
1343 
1344 	ndi_devi_exit(hdip, circ);
1345 	ndi_devi_exit(rh_dip, rh_circ);
1346 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
1347 
1348 	if (online_child) {
1349 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
1350 		    "hubd_config_one: onlining child");
1351 
1352 		(void) ndi_devi_online(hubd->h_dip, 0);
1353 	}
1354 
1355 	mutex_enter(HUBD_MUTEX(hubd));
1356 
1357 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
1358 
1359 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
1360 	    "hubd_config_one: exit");
1361 
1362 	mutex_exit(HUBD_MUTEX(hubd));
1363 
1364 	ndi_rele_devi(hdip);
1365 
1366 	return (found);
1367 }
1368 
1369 /*
1370  * bus enumeration entry points
1371  */
1372 static int
1373 hubd_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
1374     void *arg, dev_info_t **child)
1375 {
1376 	hubd_t	*hubd = hubd_get_soft_state(dip);
1377 	int	rval, circ;
1378 	long port;
1379 
1380 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1381 	    "hubd_bus_config: op=%d", op);
1382 
1383 	if (hubdi_bus_config_debug) {
1384 		flag |= NDI_DEVI_DEBUG;
1385 	}
1386 
1387 	if (op == BUS_CONFIG_ONE) {
1388 		boolean_t found;
1389 		char cname[80];
1390 		char *name, *addr;
1391 
1392 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1393 		    "hubd_bus_config: op=%d (BUS_CONFIG_ONE)", op);
1394 
1395 		(void) snprintf(cname, 80, "%s", (char *)arg);
1396 		/* split name into "name@addr" parts */
1397 		i_ddi_parse_name(cname, &name, &addr, NULL);
1398 		if (addr && *addr) {
1399 			(void) ddi_strtol(addr, NULL, 16, &port);
1400 		} else {
1401 			return (NDI_FAILURE);
1402 		}
1403 
1404 		found = hubd_config_one(hubd, port);
1405 
1406 		if (found == 0) {
1407 			return (NDI_FAILURE);
1408 		}
1409 
1410 	}
1411 	ndi_devi_enter(hubd->h_dip, &circ);
1412 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
1413 	ndi_devi_exit(hubd->h_dip, circ);
1414 
1415 	return (rval);
1416 }
1417 
1418 
1419 static int
1420 hubd_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
1421     void *arg)
1422 {
1423 	hubd_t		*hubd = hubd_get_soft_state(dip);
1424 	dev_info_t	*cdip;
1425 	usb_port_t	port;
1426 	int		circ;
1427 	int		rval;
1428 
1429 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1430 	    "hubd_bus_unconfig: op=%d", op);
1431 
1432 	if (hubdi_bus_config_debug) {
1433 		flag |= NDI_DEVI_DEBUG;
1434 	}
1435 
1436 	if ((op == BUS_UNCONFIG_ALL) && (flag & NDI_AUTODETACH) == 0) {
1437 		flag |= NDI_DEVI_REMOVE;
1438 	}
1439 
1440 	/* serialize access */
1441 	ndi_devi_enter(dip, &circ);
1442 
1443 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
1444 
1445 	/* logically zap children's list */
1446 	mutex_enter(HUBD_MUTEX(hubd));
1447 	for (port = 1; port <= hubd->h_nports; port++) {
1448 		hubd->h_port_state[port] |= HUBD_CHILD_ZAP;
1449 	}
1450 	mutex_exit(HUBD_MUTEX(hubd));
1451 
1452 	/* fill in what's left */
1453 	for (cdip = ddi_get_child(dip); cdip;
1454 	    cdip = ddi_get_next_sibling(cdip)) {
1455 		usba_device_t *usba_device = usba_get_usba_device(cdip);
1456 
1457 		if (usba_device == NULL) {
1458 
1459 			continue;
1460 		}
1461 		mutex_enter(HUBD_MUTEX(hubd));
1462 		port = usba_device->usb_port;
1463 		hubd->h_children_dips[port] = cdip;
1464 		hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
1465 		mutex_exit(HUBD_MUTEX(hubd));
1466 	}
1467 
1468 	/* physically zap the children we didn't find */
1469 	mutex_enter(HUBD_MUTEX(hubd));
1470 	for (port = 1; port <= hubd->h_nports; port++) {
1471 		if (hubd->h_port_state[port] &	HUBD_CHILD_ZAP) {
1472 			/* zap the dip and usba_device structure as well */
1473 			hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
1474 			hubd->h_children_dips[port] = NULL;
1475 			hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
1476 		}
1477 	}
1478 	mutex_exit(HUBD_MUTEX(hubd));
1479 
1480 	ndi_devi_exit(dip, circ);
1481 
1482 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
1483 	    "hubd_bus_unconfig: rval=%d", rval);
1484 
1485 	return (rval);
1486 }
1487 
1488 
1489 /* bus_power entry point */
1490 static int
1491 hubd_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
1492     void *arg, void *result)
1493 {
1494 	hubd_t		*hubd;
1495 	int		rval, pwrup_res;
1496 	usb_port_t	port;
1497 	int		retval = DDI_FAILURE;
1498 	pm_bp_child_pwrchg_t	*bpc;
1499 	pm_bp_nexus_pwrup_t	bpn;
1500 
1501 	hubd = hubd_get_soft_state(dip);
1502 
1503 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1504 	    "hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, "
1505 	    "result=%d\n", (void *)dip, impl_arg, op, arg, *(int *)result);
1506 
1507 	bpc = (pm_bp_child_pwrchg_t *)arg;
1508 
1509 	mutex_enter(HUBD_MUTEX(hubd));
1510 	hubd->h_bus_pwr++;
1511 	mutex_exit(HUBD_MUTEX(hubd));
1512 
1513 	switch (op) {
1514 	case BUS_POWER_PRE_NOTIFICATION:
1515 		port = hubd_child_dip2port(hubd, bpc->bpc_dip);
1516 		USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1517 		    "hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d",
1518 		    port);
1519 
1520 		/* go to full power if we are powered down */
1521 		mutex_enter(HUBD_MUTEX(hubd));
1522 
1523 		/*
1524 		 * If this case completes normally, idle will be in
1525 		 * hubd_bus_power / BUS_POWER_POST_NOTIFICATION
1526 		 */
1527 		hubd_pm_busy_component(hubd, dip, 0);
1528 
1529 		/*
1530 		 * raise power only if we have created the components
1531 		 * and are currently in low power
1532 		 */
1533 		if ((hubd->h_dev_state == USB_DEV_PWRED_DOWN) &&
1534 		    hubd->h_hubpm->hubp_wakeup_enabled) {
1535 			mutex_exit(HUBD_MUTEX(hubd));
1536 
1537 			bpn.bpn_comp = 0;
1538 			bpn.bpn_dip = dip;
1539 			bpn.bpn_level = USB_DEV_OS_FULL_PWR;
1540 			bpn.bpn_private = bpc->bpc_private;
1541 
1542 			rval = pm_busop_bus_power(dip, impl_arg,
1543 			    BUS_POWER_NEXUS_PWRUP, (void *)&bpn,
1544 			    (void *)&pwrup_res);
1545 
1546 			if (rval != DDI_SUCCESS || pwrup_res != DDI_SUCCESS) {
1547 				mutex_enter(HUBD_MUTEX(hubd));
1548 				hubd_pm_idle_component(hubd, dip, 0);
1549 				mutex_exit(HUBD_MUTEX(hubd));
1550 
1551 				break;
1552 			}
1553 			mutex_enter(HUBD_MUTEX(hubd));
1554 		}
1555 
1556 		/* indicate that child is changing power level */
1557 		hubd->h_port_state[port] |= HUBD_CHILD_PWRLVL_CHNG;
1558 		mutex_exit(HUBD_MUTEX(hubd));
1559 
1560 		if ((bpc->bpc_olevel == 0) &&
1561 		    (bpc->bpc_nlevel > bpc->bpc_olevel)) {
1562 			/*
1563 			 * this child is transitioning from power off
1564 			 * to power on state - resume port
1565 			 */
1566 			rval = hubd_resume_port(hubd, port);
1567 			if (rval == USB_SUCCESS) {
1568 				retval = DDI_SUCCESS;
1569 			} else {
1570 				/* reset this flag on failure */
1571 				mutex_enter(HUBD_MUTEX(hubd));
1572 				hubd->h_port_state[port] &=
1573 				    ~HUBD_CHILD_PWRLVL_CHNG;
1574 				hubd_pm_idle_component(hubd, dip, 0);
1575 				mutex_exit(HUBD_MUTEX(hubd));
1576 			}
1577 		} else {
1578 			retval = DDI_SUCCESS;
1579 		}
1580 
1581 		break;
1582 	case BUS_POWER_POST_NOTIFICATION:
1583 		port = hubd_child_dip2port(hubd, bpc->bpc_dip);
1584 		USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1585 		    "hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d",
1586 		    port);
1587 
1588 		mutex_enter(HUBD_MUTEX(hubd));
1589 		hubd->h_port_state[port] &= ~HUBD_CHILD_PWRLVL_CHNG;
1590 		mutex_exit(HUBD_MUTEX(hubd));
1591 
1592 		/* record child's pwr and suspend port if required */
1593 		rval = hubd_post_power(hubd, port, bpc, *(int *)result);
1594 		if (rval == USB_SUCCESS) {
1595 
1596 			retval = DDI_SUCCESS;
1597 		}
1598 
1599 		mutex_enter(HUBD_MUTEX(hubd));
1600 
1601 		/*
1602 		 * Matching idle for the busy in
1603 		 * hubd_bus_power / BUS_POWER_PRE_NOTIFICATION
1604 		 */
1605 		hubd_pm_idle_component(hubd, dip, 0);
1606 
1607 		mutex_exit(HUBD_MUTEX(hubd));
1608 
1609 		break;
1610 	default:
1611 		retval = pm_busop_bus_power(dip, impl_arg, op, arg, result);
1612 
1613 		break;
1614 	}
1615 
1616 	mutex_enter(HUBD_MUTEX(hubd));
1617 	hubd->h_bus_pwr--;
1618 	mutex_exit(HUBD_MUTEX(hubd));
1619 
1620 	return (retval);
1621 }
1622 
1623 
1624 /*
1625  * functions to handle power transition for OS levels 0 -> 3
1626  */
1627 static int
1628 hubd_pwrlvl0(hubd_t *hubd)
1629 {
1630 	hub_power_t	*hubpm;
1631 
1632 	/* We can't power down if hotplug thread is running */
1633 	if (hubd->h_hotplug_thread || hubd->h_hubpm->hubp_busy_pm ||
1634 	    (hubd_can_suspend(hubd) == USB_FAILURE)) {
1635 
1636 		return (USB_FAILURE);
1637 	}
1638 
1639 	switch (hubd->h_dev_state) {
1640 	case USB_DEV_ONLINE:
1641 		hubpm = hubd->h_hubpm;
1642 
1643 		/*
1644 		 * To avoid race with bus_power pre_notify on check over
1645 		 * dev_state, we need to correctly set the dev state
1646 		 * before the mutex is dropped in stop polling.
1647 		 */
1648 		hubd->h_dev_state = USB_DEV_PWRED_DOWN;
1649 		hubpm->hubp_current_power = USB_DEV_OS_PWR_OFF;
1650 
1651 		/*
1652 		 * if we are the root hub, do not stop polling
1653 		 * otherwise, we will never see a resume
1654 		 */
1655 		if (usba_is_root_hub(hubd->h_dip)) {
1656 			/* place holder to implement Global Suspend */
1657 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1658 			    "Global Suspend: Not Yet Implemented");
1659 		} else {
1660 			hubd_stop_polling(hubd);
1661 		}
1662 
1663 		/* Issue USB D3 command to the device here */
1664 		(void) usb_set_device_pwrlvl3(hubd->h_dip);
1665 
1666 		break;
1667 	case USB_DEV_DISCONNECTED:
1668 	case USB_DEV_SUSPENDED:
1669 	case USB_DEV_PWRED_DOWN:
1670 	default:
1671 
1672 		break;
1673 	}
1674 
1675 	return (USB_SUCCESS);
1676 }
1677 
1678 
1679 /* ARGSUSED */
1680 static int
1681 hubd_pwrlvl1(hubd_t *hubd)
1682 {
1683 	/* Issue USB D2 command to the device here */
1684 	(void) usb_set_device_pwrlvl2(hubd->h_dip);
1685 
1686 	return (USB_FAILURE);
1687 }
1688 
1689 
1690 /* ARGSUSED */
1691 static int
1692 hubd_pwrlvl2(hubd_t *hubd)
1693 {
1694 	/* Issue USB D1 command to the device here */
1695 	(void) usb_set_device_pwrlvl1(hubd->h_dip);
1696 
1697 	return (USB_FAILURE);
1698 }
1699 
1700 
1701 static int
1702 hubd_pwrlvl3(hubd_t *hubd)
1703 {
1704 	hub_power_t	*hubpm;
1705 	int		rval;
1706 
1707 	USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, "hubd_pwrlvl3");
1708 
1709 	hubpm = hubd->h_hubpm;
1710 	switch (hubd->h_dev_state) {
1711 	case USB_DEV_PWRED_DOWN:
1712 		ASSERT(hubpm->hubp_current_power == USB_DEV_OS_PWR_OFF);
1713 		if (usba_is_root_hub(hubd->h_dip)) {
1714 			/* implement global resume here */
1715 			USB_DPRINTF_L2(DPRINT_MASK_PM,
1716 			    hubd->h_log_handle,
1717 			    "Global Resume: Not Yet Implemented");
1718 		}
1719 		/* Issue USB D0 command to the device here */
1720 		rval = usb_set_device_pwrlvl0(hubd->h_dip);
1721 		ASSERT(rval == USB_SUCCESS);
1722 		hubd->h_dev_state = USB_DEV_ONLINE;
1723 		hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
1724 		hubpm->hubp_time_at_full_power = gethrtime();
1725 		hubd_start_polling(hubd, 0);
1726 
1727 		/* FALLTHRU */
1728 	case USB_DEV_ONLINE:
1729 		/* we are already in full power */
1730 
1731 		/* FALLTHRU */
1732 	case USB_DEV_DISCONNECTED:
1733 	case USB_DEV_SUSPENDED:
1734 		/*
1735 		 * PM framework tries to put you in full power
1736 		 * during system shutdown. If we are disconnected
1737 		 * return success. Also, we should not change state
1738 		 * when we are disconnected or suspended or about to
1739 		 * transition to that state
1740 		 */
1741 
1742 		return (USB_SUCCESS);
1743 	default:
1744 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1745 		    "hubd_pwrlvl3: Illegal dev_state=%d", hubd->h_dev_state);
1746 
1747 		return (USB_FAILURE);
1748 	}
1749 }
1750 
1751 
1752 /* power entry point */
1753 /* ARGSUSED */
1754 int
1755 usba_hubdi_power(dev_info_t *dip, int comp, int level)
1756 {
1757 	hubd_t		*hubd;
1758 	hub_power_t	*hubpm;
1759 	int		retval;
1760 	int		circ;
1761 
1762 	hubd = hubd_get_soft_state(dip);
1763 	USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1764 	    "usba_hubdi_power: level=%d", level);
1765 
1766 	ndi_devi_enter(dip, &circ);
1767 
1768 	mutex_enter(HUBD_MUTEX(hubd));
1769 	hubpm = hubd->h_hubpm;
1770 
1771 	/* check if we are transitioning to a legal power level */
1772 	if (USB_DEV_PWRSTATE_OK(hubpm->hubp_pwr_states, level)) {
1773 		USB_DPRINTF_L2(DPRINT_MASK_HUBDI, hubd->h_log_handle,
1774 		    "usba_hubdi_power: illegal power level=%d "
1775 		    "hubp_pwr_states=0x%x", level, hubpm->hubp_pwr_states);
1776 		mutex_exit(HUBD_MUTEX(hubd));
1777 
1778 		ndi_devi_exit(dip, circ);
1779 
1780 		return (DDI_FAILURE);
1781 	}
1782 
1783 	switch (level) {
1784 	case USB_DEV_OS_PWR_OFF:
1785 		retval = hubd_pwrlvl0(hubd);
1786 
1787 		break;
1788 	case USB_DEV_OS_PWR_1:
1789 		retval = hubd_pwrlvl1(hubd);
1790 
1791 		break;
1792 	case USB_DEV_OS_PWR_2:
1793 		retval = hubd_pwrlvl2(hubd);
1794 
1795 		break;
1796 	case USB_DEV_OS_FULL_PWR:
1797 		retval = hubd_pwrlvl3(hubd);
1798 
1799 		break;
1800 	default:
1801 		retval = USB_FAILURE;
1802 
1803 		break;
1804 	}
1805 	mutex_exit(HUBD_MUTEX(hubd));
1806 
1807 	ndi_devi_exit(dip, circ);
1808 
1809 	return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
1810 }
1811 
1812 
1813 /* power entry point for the root hub */
1814 int
1815 usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level)
1816 {
1817 	return (usba_hubdi_power(dip, comp, level));
1818 }
1819 
1820 
1821 /*
1822  * standard driver entry points support code
1823  */
1824 int
1825 usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1826 {
1827 	int			instance = ddi_get_instance(dip);
1828 	hubd_t			*hubd = NULL;
1829 	int			i, rval;
1830 	int			minor;
1831 	uint8_t			ports_count;
1832 	char			*log_name = NULL;
1833 	const char		*root_hub_drvname;
1834 	usb_ep_data_t		*ep_data;
1835 	usba_device_t		*child_ud = NULL;
1836 	usb_dev_descr_t		*usb_dev_descr;
1837 	usb_port_status_t	parent_port_status, child_port_status;
1838 
1839 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle,
1840 	    "hubd_attach instance %d, cmd=0x%x", instance, cmd);
1841 
1842 	switch (cmd) {
1843 	case DDI_ATTACH:
1844 
1845 		break;
1846 	case DDI_RESUME:
1847 		hubd_cpr_resume(dip);
1848 
1849 		return (DDI_SUCCESS);
1850 	default:
1851 		return (DDI_FAILURE);
1852 	}
1853 
1854 	/*
1855 	 * Allocate softc information.
1856 	 */
1857 	if (usba_is_root_hub(dip)) {
1858 		/* soft state has already been allocated */
1859 		hubd = hubd_get_soft_state(dip);
1860 		minor = HUBD_IS_ROOT_HUB;
1861 
1862 		/* generate readable labels for different root hubs */
1863 		root_hub_drvname = ddi_driver_name(dip);
1864 		if (strcmp(root_hub_drvname, "xhci") == 0) {
1865 			log_name = "xusb";
1866 		} else if (strcmp(root_hub_drvname, "ehci") == 0) {
1867 			log_name = "eusb";
1868 		} else if (strcmp(root_hub_drvname, "uhci") == 0) {
1869 			log_name = "uusb";
1870 		} else {
1871 			/* std. for ohci */
1872 			log_name = "usb";
1873 		}
1874 	} else {
1875 		rval = ddi_soft_state_zalloc(hubd_statep, instance);
1876 		minor = 0;
1877 
1878 		if (rval != DDI_SUCCESS) {
1879 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
1880 			    "cannot allocate soft state (%d)", instance);
1881 			goto fail;
1882 		}
1883 
1884 		hubd = hubd_get_soft_state(dip);
1885 		if (hubd == NULL) {
1886 			goto fail;
1887 		}
1888 	}
1889 
1890 	hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel,
1891 	    &hubd_errmask, &hubd_instance_debug, 0);
1892 
1893 	hubd->h_usba_device	= child_ud = usba_get_usba_device(dip);
1894 	hubd->h_dip		= dip;
1895 	hubd->h_instance	= instance;
1896 
1897 	mutex_enter(&child_ud->usb_mutex);
1898 	child_port_status = child_ud->usb_port_status;
1899 	usb_dev_descr = child_ud->usb_dev_descr;
1900 	parent_port_status = (child_ud->usb_hs_hub_usba_dev) ?
1901 	    child_ud->usb_hs_hub_usba_dev->usb_port_status : 0;
1902 	mutex_exit(&child_ud->usb_mutex);
1903 
1904 	if ((child_port_status == USBA_FULL_SPEED_DEV) &&
1905 	    (parent_port_status >= USBA_HIGH_SPEED_DEV) &&
1906 	    (usb_dev_descr->bcdUSB == 0x100)) {
1907 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
1908 		    "Use of a USB1.0 hub behind a higher speed port may "
1909 		    "cause unexpected failures");
1910 	}
1911 
1912 	hubd->h_pipe_policy.pp_max_async_reqs = 1;
1913 
1914 	/* register with USBA as client driver */
1915 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
1916 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1917 		    "client attach failed");
1918 
1919 		goto fail;
1920 	}
1921 
1922 	if (usb_get_dev_data(dip, &hubd->h_dev_data,
1923 	    USB_PARSE_LVL_IF, 0) != USB_SUCCESS) {
1924 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1925 		    "cannot get dev_data");
1926 
1927 		goto fail;
1928 	}
1929 
1930 	if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data,
1931 	    hubd->h_dev_data->dev_curr_if, 0, 0,
1932 	    (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) {
1933 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1934 		    "no interrupt IN endpoint found");
1935 
1936 		goto fail;
1937 	}
1938 
1939 	if (usb_ep_xdescr_fill(USB_EP_XDESCR_CURRENT_VERSION, dip, ep_data,
1940 	    &hubd->h_ep1_xdescr) != USB_SUCCESS) {
1941 		goto fail;
1942 	}
1943 
1944 	hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph;
1945 
1946 	mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER,
1947 	    hubd->h_dev_data->dev_iblock_cookie);
1948 	cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL);
1949 	cv_init(&hubd->h_cv_hotplug_dev, NULL, CV_DRIVER, NULL);
1950 
1951 	hubd->h_init_state |= HUBD_LOCKS_DONE;
1952 
1953 	usb_free_descr_tree(dip, hubd->h_dev_data);
1954 
1955 	/*
1956 	 * register this hub instance with usba
1957 	 */
1958 	rval = usba_hubdi_register(dip, 0);
1959 	if (rval != USB_SUCCESS) {
1960 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1961 		    "usba_hubdi_register failed");
1962 		goto fail;
1963 	}
1964 
1965 	mutex_enter(HUBD_MUTEX(hubd));
1966 	hubd->h_init_state |= HUBD_HUBDI_REGISTERED;
1967 	hubd->h_dev_state = USB_DEV_ONLINE;
1968 	mutex_exit(HUBD_MUTEX(hubd));
1969 
1970 	/* now create components to power manage this device */
1971 	hubd_create_pm_components(dip, hubd);
1972 
1973 	/*
1974 	 * Event handling: definition and registration
1975 	 *
1976 	 * first the  definition:
1977 	 * get event handle
1978 	 */
1979 	(void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP);
1980 
1981 	/* bind event set to the handle */
1982 	if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events,
1983 	    NDI_SLEEP)) {
1984 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
1985 		    "binding event set failed");
1986 
1987 		goto fail;
1988 	}
1989 
1990 	/* event registration */
1991 	if (hubd_register_events(hubd) != USB_SUCCESS) {
1992 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1993 		    "hubd_register_events failed");
1994 
1995 		goto fail;
1996 	}
1997 
1998 	mutex_enter(HUBD_MUTEX(hubd));
1999 	hubd->h_init_state |= HUBD_EVENTS_REGISTERED;
2000 
2001 	if (hubd_get_hub_descriptor(hubd) != USB_SUCCESS) {
2002 		mutex_exit(HUBD_MUTEX(hubd));
2003 
2004 		goto fail;
2005 	}
2006 
2007 	/*
2008 	 * Now that we have our descriptors, we need to give the host controller
2009 	 * a chance to properly configure the device if needed (this is required
2010 	 * for xHCI).
2011 	 *
2012 	 * Note, if anyone ever adds support for using the Multi TT mode for USB
2013 	 * 2 High speed Hubs, the xhci driver will need to be updated and that
2014 	 * will need to be done before this is called.
2015 	 */
2016 	if (hubd->h_usba_device->usb_hcdi_ops->usba_hcdi_hub_update != NULL &&
2017 	    !usba_is_root_hub(dip)) {
2018 		int ret;
2019 		uint8_t chars;
2020 		usba_device_t *ud = hubd->h_usba_device;
2021 
2022 		chars = (hubd->h_hub_chars & HUB_CHARS_TT_THINK_TIME) >>
2023 		    HUB_CHARS_TT_SHIFT;
2024 		ret = ud->usb_hcdi_ops->usba_hcdi_hub_update(ud,
2025 		    hubd->h_nports, chars);
2026 		if (ret != USB_SUCCESS) {
2027 			mutex_exit(HUBD_MUTEX(hubd));
2028 			goto fail;
2029 		}
2030 	}
2031 
2032 	if (hubd_set_hub_depth(hubd) != USB_SUCCESS) {
2033 
2034 		goto fail;
2035 	}
2036 
2037 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
2038 	    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2039 	    "hub-ignore-power-budget") == 1) {
2040 		hubd->h_ignore_pwr_budget = B_TRUE;
2041 	} else {
2042 		hubd->h_ignore_pwr_budget = B_FALSE;
2043 
2044 		/* initialize hub power budget variables */
2045 		if (hubd_init_power_budget(hubd) != USB_SUCCESS) {
2046 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2047 			    "hubd_init_power_budget failed");
2048 			mutex_exit(HUBD_MUTEX(hubd));
2049 
2050 			goto fail;
2051 		}
2052 	}
2053 
2054 	/* initialize and create children */
2055 	if (hubd_check_ports(hubd) != USB_SUCCESS) {
2056 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2057 		    "hubd_check_ports failed");
2058 		mutex_exit(HUBD_MUTEX(hubd));
2059 
2060 		goto fail;
2061 	}
2062 
2063 	/*
2064 	 * create cfgadm nodes
2065 	 */
2066 	hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP);
2067 	hubd_get_ancestry_str(hubd);
2068 
2069 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2070 	    "#ports=0x%x", hubd->h_nports);
2071 
2072 	for (i = 1; i <= hubd->h_nports; i++) {
2073 		char ap_name[HUBD_APID_NAMELEN];
2074 
2075 		(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
2076 		    hubd->h_ancestry_str, i);
2077 		USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2078 		    "ap_name=%s", ap_name);
2079 
2080 		if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance,
2081 		    DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
2082 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2083 			    "cannot create attachment point node (%d)",
2084 			    instance);
2085 			mutex_exit(HUBD_MUTEX(hubd));
2086 
2087 			goto fail;
2088 		}
2089 	}
2090 
2091 	ports_count = hubd->h_nports;
2092 	mutex_exit(HUBD_MUTEX(hubd));
2093 
2094 	/* create minor nodes */
2095 	if (ddi_create_minor_node(dip, "hubd", S_IFCHR,
2096 	    instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
2097 
2098 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2099 		    "cannot create devctl minor node (%d)", instance);
2100 
2101 		goto fail;
2102 	}
2103 
2104 	mutex_enter(HUBD_MUTEX(hubd));
2105 	hubd->h_init_state |= HUBD_MINOR_NODE_CREATED;
2106 	mutex_exit(HUBD_MUTEX(hubd));
2107 
2108 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2109 	    "usb-port-count", ports_count) != DDI_PROP_SUCCESS) {
2110 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2111 		    "usb-port-count update failed");
2112 	}
2113 
2114 	/*
2115 	 * host controller driver has already reported this dev
2116 	 * if we are the root hub
2117 	 */
2118 	if (!usba_is_root_hub(dip)) {
2119 		ddi_report_dev(dip);
2120 	}
2121 
2122 	/* enable deathrow thread */
2123 	hubd->h_cleanup_enabled = B_TRUE;
2124 	mutex_enter(HUBD_MUTEX(hubd));
2125 	hubd_pm_idle_component(hubd, dip, 0);
2126 	mutex_exit(HUBD_MUTEX(hubd));
2127 
2128 	return (DDI_SUCCESS);
2129 
2130 fail:
2131 	{
2132 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2133 
2134 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
2135 		    "cannot attach %s", ddi_pathname(dip, pathname));
2136 
2137 		kmem_free(pathname, MAXPATHLEN);
2138 	}
2139 
2140 	if (hubd != NULL) {
2141 		mutex_enter(HUBD_MUTEX(hubd));
2142 		hubd_pm_idle_component(hubd, dip, 0);
2143 		mutex_exit(HUBD_MUTEX(hubd));
2144 
2145 		rval = hubd_cleanup(dip, hubd);
2146 		if (rval != USB_SUCCESS) {
2147 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
2148 			    "failure to complete cleanup after attach failure");
2149 		}
2150 	}
2151 
2152 	return (DDI_FAILURE);
2153 }
2154 
2155 
2156 int
2157 usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2158 {
2159 	hubd_t	*hubd = hubd_get_soft_state(dip);
2160 	int	rval;
2161 
2162 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2163 	    "hubd_detach: cmd=0x%x", cmd);
2164 
2165 	switch (cmd) {
2166 	case DDI_DETACH:
2167 		rval = hubd_cleanup(dip, hubd);
2168 
2169 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
2170 	case DDI_SUSPEND:
2171 		rval = hubd_cpr_suspend(hubd);
2172 
2173 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
2174 	default:
2175 		return (DDI_FAILURE);
2176 	}
2177 }
2178 
2179 
2180 /*
2181  * hubd_setdevaddr
2182  *	set the device addrs on this port
2183  */
2184 static int
2185 hubd_setdevaddr(hubd_t *hubd, usb_port_t port)
2186 {
2187 	int		rval = USB_FAILURE;
2188 	usb_cr_t	completion_reason;
2189 	usb_cb_flags_t	cb_flags;
2190 	usb_pipe_handle_t ph;
2191 	dev_info_t	*child_dip = NULL;
2192 	uchar_t		address = 0;
2193 	usba_device_t	*usba_device;
2194 	int		retry = 0;
2195 	long		time_delay;
2196 
2197 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2198 	    "hubd_setdevaddr: port=%d", port);
2199 
2200 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
2201 
2202 	child_dip = hubd->h_children_dips[port];
2203 	address = hubd->h_usba_devices[port]->usb_addr;
2204 	usba_device = hubd->h_usba_devices[port];
2205 
2206 	/* close the default pipe with addr x */
2207 	mutex_exit(HUBD_MUTEX(hubd));
2208 	ph = usba_get_dflt_pipe_handle(child_dip);
2209 	usb_pipe_close(child_dip, ph,
2210 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2211 	mutex_enter(HUBD_MUTEX(hubd));
2212 
2213 	/*
2214 	 * If the host controller is in charge of addressing, have it do that
2215 	 * now and skip everything else.
2216 	 */
2217 	if (usba_device->usb_hcdi_ops->usba_hcdi_device_address != NULL) {
2218 		mutex_exit(HUBD_MUTEX(hubd));
2219 		rval = usba_device->usb_hcdi_ops->usba_hcdi_device_address(
2220 		    usba_device);
2221 		mutex_enter(HUBD_MUTEX(hubd));
2222 
2223 		usba_clear_data_toggle(usba_device);
2224 		return (rval);
2225 	}
2226 
2227 	/*
2228 	 * As this device has been reset, temporarily
2229 	 * assign the default address
2230 	 */
2231 	mutex_enter(&usba_device->usb_mutex);
2232 	address = usba_device->usb_addr;
2233 	usba_device->usb_addr = USBA_DEFAULT_ADDR;
2234 	mutex_exit(&usba_device->usb_mutex);
2235 
2236 	mutex_exit(HUBD_MUTEX(hubd));
2237 
2238 	time_delay = drv_usectohz(hubd_device_delay / 20);
2239 	for (retry = 0; retry < hubd_retry_enumerate; retry++) {
2240 
2241 		/* open child's default pipe with USBA_DEFAULT_ADDR */
2242 		if ((rval = usb_pipe_open(child_dip, NULL, NULL,
2243 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) !=
2244 		    USB_SUCCESS) {
2245 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2246 			    "hubd_setdevaddr: Unable to open default pipe");
2247 
2248 			break;
2249 		}
2250 
2251 		/* Set the address of the device */
2252 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
2253 		    USB_DEV_REQ_HOST_TO_DEV,
2254 		    USB_REQ_SET_ADDRESS,	/* bRequest */
2255 		    address,			/* wValue */
2256 		    0,				/* wIndex */
2257 		    0,				/* wLength */
2258 		    NULL, 0,
2259 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
2260 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2261 			    "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x",
2262 			    retry, rval, completion_reason, cb_flags);
2263 		}
2264 
2265 		usb_pipe_close(child_dip, ph,
2266 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2267 
2268 		if (rval == USB_SUCCESS) {
2269 
2270 			break;
2271 		}
2272 
2273 		delay(time_delay);
2274 	}
2275 
2276 	/* Reset to the old address */
2277 	mutex_enter(&usba_device->usb_mutex);
2278 	usba_device->usb_addr = address;
2279 	mutex_exit(&usba_device->usb_mutex);
2280 	mutex_enter(HUBD_MUTEX(hubd));
2281 
2282 	usba_clear_data_toggle(usba_device);
2283 
2284 	return (rval);
2285 }
2286 
2287 
2288 /*
2289  * hubd_setdevconfig
2290  *	set the device addrs on this port
2291  */
2292 static void
2293 hubd_setdevconfig(hubd_t *hubd, usb_port_t port)
2294 {
2295 	int			rval;
2296 	usb_cr_t		completion_reason;
2297 	usb_cb_flags_t		cb_flags;
2298 	usb_pipe_handle_t	ph;
2299 	dev_info_t		*child_dip = NULL;
2300 	usba_device_t		*usba_device = NULL;
2301 	uint16_t		config_value;
2302 
2303 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2304 	    "hubd_setdevconfig: port=%d", port);
2305 
2306 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
2307 
2308 	child_dip = hubd->h_children_dips[port];
2309 	usba_device = hubd->h_usba_devices[port];
2310 	config_value = hubd->h_usba_devices[port]->usb_cfg_value;
2311 	mutex_exit(HUBD_MUTEX(hubd));
2312 
2313 	/* open the default control pipe */
2314 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
2315 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) ==
2316 	    USB_SUCCESS) {
2317 
2318 		/* Set the default configuration of the device */
2319 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
2320 		    USB_DEV_REQ_HOST_TO_DEV,
2321 		    USB_REQ_SET_CFG,		/* bRequest */
2322 		    config_value,		/* wValue */
2323 		    0,				/* wIndex */
2324 		    0,				/* wLength */
2325 		    NULL, 0,
2326 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
2327 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2328 			    "hubd_setdevconfig: set device config failed: "
2329 			    "cr=%d cb_fl=0x%x rval=%d",
2330 			    completion_reason, cb_flags, rval);
2331 		}
2332 		/*
2333 		 * After setting the configuration, we make this default
2334 		 * control pipe persistent, so that it gets re-opened
2335 		 * on posting a connect event
2336 		 */
2337 		usba_persistent_pipe_close(usba_device);
2338 	} else {
2339 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2340 		    "pipe open fails: rval=%d", rval);
2341 	}
2342 	mutex_enter(HUBD_MUTEX(hubd));
2343 }
2344 
2345 
2346 /*ARGSUSED*/
2347 static int
2348 hubd_check_disconnected_ports(dev_info_t *dip, void *arg)
2349 {
2350 	int circ;
2351 	usb_port_t port;
2352 	hubd_t *hubd;
2353 	major_t hub_major = ddi_name_to_major("hubd");
2354 	major_t hwahc_major = ddi_name_to_major("hwahc");
2355 	major_t usbmid_major = ddi_name_to_major("usb_mid");
2356 
2357 	/*
2358 	 * make sure dip is a usb hub, major of root hub is HCD
2359 	 * major
2360 	 */
2361 	if (!usba_is_root_hub(dip)) {
2362 		if (ddi_driver_major(dip) == usbmid_major) {
2363 			/*
2364 			 * need to walk the children since it might be a
2365 			 * HWA device
2366 			 */
2367 
2368 			return (DDI_WALK_CONTINUE);
2369 		}
2370 
2371 		/* TODO: DWA device may also need special handling */
2372 
2373 		if (((ddi_driver_major(dip) != hub_major) &&
2374 		    (ddi_driver_major(dip) != hwahc_major)) ||
2375 		    !i_ddi_devi_attached(dip)) {
2376 
2377 			return (DDI_WALK_PRUNECHILD);
2378 		}
2379 	}
2380 
2381 	hubd = hubd_get_soft_state(dip);
2382 	if (hubd == NULL) {
2383 
2384 		return (DDI_WALK_PRUNECHILD);
2385 	}
2386 
2387 	/* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */
2388 	ndi_devi_enter(dip, &circ);
2389 
2390 	if (ddi_driver_major(dip) != hwahc_major) {
2391 		/* for normal usb hub or root hub */
2392 		mutex_enter(HUBD_MUTEX(hubd));
2393 		for (port = 1; port <= hubd->h_nports; port++) {
2394 			dev_info_t *cdip = hubd->h_children_dips[port];
2395 
2396 			if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) {
2397 
2398 				continue;
2399 			}
2400 
2401 			(void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE,
2402 			    B_TRUE);
2403 		}
2404 		mutex_exit(HUBD_MUTEX(hubd));
2405 	} else {
2406 		/* for HWA */
2407 		if (hubd->h_cleanup_child != NULL) {
2408 			if (hubd->h_cleanup_child(dip) != USB_SUCCESS) {
2409 				ndi_devi_exit(dip, circ);
2410 
2411 				return (DDI_WALK_PRUNECHILD);
2412 			}
2413 		} else {
2414 			ndi_devi_exit(dip, circ);
2415 
2416 			return (DDI_WALK_PRUNECHILD);
2417 		}
2418 	}
2419 
2420 	ndi_devi_exit(dip, circ);
2421 
2422 	/* skip siblings of root hub */
2423 	if (usba_is_root_hub(dip)) {
2424 
2425 		return (DDI_WALK_PRUNESIB);
2426 	}
2427 
2428 	return (DDI_WALK_CONTINUE);
2429 }
2430 
2431 
2432 /*
2433  * this thread will walk all children under the root hub for this
2434  * USB bus instance and attempt to remove them
2435  */
2436 static void
2437 hubd_root_hub_cleanup_thread(void *arg)
2438 {
2439 	int circ;
2440 	hubd_t *root_hubd = (hubd_t *)arg;
2441 	dev_info_t *rh_dip = root_hubd->h_dip;
2442 #ifndef __lock_lint
2443 	callb_cpr_t cprinfo;
2444 
2445 	CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr,
2446 	    "USB root hub");
2447 #endif
2448 
2449 	for (;;) {
2450 		/* don't race with detach */
2451 		ndi_hold_devi(rh_dip);
2452 
2453 		mutex_enter(HUBD_MUTEX(root_hubd));
2454 		root_hubd->h_cleanup_needed = 0;
2455 		mutex_exit(HUBD_MUTEX(root_hubd));
2456 
2457 		(void) devfs_clean(rh_dip, NULL, 0);
2458 
2459 		ndi_devi_enter(ddi_get_parent(rh_dip), &circ);
2460 		ddi_walk_devs(rh_dip, hubd_check_disconnected_ports,
2461 		    NULL);
2462 #ifdef __lock_lint
2463 		(void) hubd_check_disconnected_ports(rh_dip, NULL);
2464 #endif
2465 		ndi_devi_exit(ddi_get_parent(rh_dip), circ);
2466 
2467 		/* quit if we are not enabled anymore */
2468 		mutex_enter(HUBD_MUTEX(root_hubd));
2469 		if ((root_hubd->h_cleanup_enabled == B_FALSE) ||
2470 		    (root_hubd->h_cleanup_needed == B_FALSE)) {
2471 			root_hubd->h_cleanup_active = B_FALSE;
2472 			mutex_exit(HUBD_MUTEX(root_hubd));
2473 			ndi_rele_devi(rh_dip);
2474 
2475 			break;
2476 		}
2477 		mutex_exit(HUBD_MUTEX(root_hubd));
2478 		ndi_rele_devi(rh_dip);
2479 
2480 #ifndef __lock_lint
2481 		mutex_enter(HUBD_MUTEX(root_hubd));
2482 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2483 		mutex_exit(HUBD_MUTEX(root_hubd));
2484 
2485 		delay(drv_usectohz(hubd_dip_cleanup_delay));
2486 
2487 		mutex_enter(HUBD_MUTEX(root_hubd));
2488 		CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd));
2489 		mutex_exit(HUBD_MUTEX(root_hubd));
2490 #endif
2491 	}
2492 
2493 #ifndef __lock_lint
2494 	mutex_enter(HUBD_MUTEX(root_hubd));
2495 	CALLB_CPR_EXIT(&cprinfo);
2496 #endif
2497 }
2498 
2499 
2500 void
2501 hubd_schedule_cleanup(dev_info_t *rh_dip)
2502 {
2503 	hubd_t	*root_hubd;
2504 
2505 	/*
2506 	 * The usb_root_hub_dip pointer for the child hub of the WUSB
2507 	 * wire adapter class device points to the wire adapter, not
2508 	 * the root hub. Need to find the real root hub dip so that
2509 	 * the cleanup thread only starts from the root hub.
2510 	 */
2511 	while (!usba_is_root_hub(rh_dip)) {
2512 		root_hubd = hubd_get_soft_state(rh_dip);
2513 		if (root_hubd != NULL) {
2514 			rh_dip = root_hubd->h_usba_device->usb_root_hub_dip;
2515 			if (rh_dip == NULL) {
2516 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2517 				    root_hubd->h_log_handle,
2518 				    "hubd_schedule_cleanup: null rh dip");
2519 
2520 				return;
2521 			}
2522 		} else {
2523 			USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2524 			    root_hubd->h_log_handle,
2525 			    "hubd_schedule_cleanup: cannot find root hub");
2526 
2527 			return;
2528 		}
2529 	}
2530 	root_hubd = hubd_get_soft_state(rh_dip);
2531 
2532 	mutex_enter(HUBD_MUTEX(root_hubd));
2533 	root_hubd->h_cleanup_needed = B_TRUE;
2534 	if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) {
2535 		root_hubd->h_cleanup_active = B_TRUE;
2536 		mutex_exit(HUBD_MUTEX(root_hubd));
2537 		(void) thread_create(NULL, 0,
2538 		    hubd_root_hub_cleanup_thread,
2539 		    (void *)root_hubd, 0, &p0, TS_RUN,
2540 		    minclsyspri);
2541 	} else {
2542 		mutex_exit(HUBD_MUTEX(root_hubd));
2543 	}
2544 }
2545 
2546 
2547 /*
2548  * hubd_restore_device_state:
2549  *	- set config for the hub
2550  *	- power cycle all the ports
2551  *	- for each port that was connected
2552  *		- reset port
2553  *		- assign addrs to the device on this port
2554  *	- restart polling
2555  *	- reset suspend flag
2556  */
2557 static void
2558 hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd)
2559 {
2560 	int		rval;
2561 	int		retry;
2562 	uint_t		hub_prev_state;
2563 	usb_port_t	port;
2564 	uint16_t	status;
2565 	uint16_t	change;
2566 	dev_info_t	*ch_dip;
2567 	boolean_t	ehci_root_hub;
2568 
2569 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2570 	    "hubd_restore_device_state:");
2571 
2572 	mutex_enter(HUBD_MUTEX(hubd));
2573 	hub_prev_state = hubd->h_dev_state;
2574 	ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN);
2575 
2576 	/* First bring the device to full power */
2577 	(void) hubd_pm_busy_component(hubd, dip, 0);
2578 	mutex_exit(HUBD_MUTEX(hubd));
2579 
2580 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
2581 
2582 	if (!usba_is_root_hub(dip) &&
2583 	    (usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0,
2584 	    DPRINT_MASK_HOTPLUG,
2585 	    USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) {
2586 
2587 		/* change the device state to disconnected */
2588 		mutex_enter(HUBD_MUTEX(hubd));
2589 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
2590 		(void) hubd_pm_idle_component(hubd, dip, 0);
2591 		mutex_exit(HUBD_MUTEX(hubd));
2592 
2593 		return;
2594 	}
2595 
2596 	ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0);
2597 
2598 	mutex_enter(HUBD_MUTEX(hubd));
2599 	/* First turn off all port power */
2600 	rval = hubd_disable_all_port_power(hubd);
2601 	if (rval != USB_SUCCESS) {
2602 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
2603 		    "hubd_restore_device_state:"
2604 		    "turning off port power failed");
2605 	}
2606 
2607 	/* Settling time before turning on again */
2608 	mutex_exit(HUBD_MUTEX(hubd));
2609 	delay(drv_usectohz(hubd_device_delay / 100));
2610 	mutex_enter(HUBD_MUTEX(hubd));
2611 
2612 	/* enable power on all ports so we can see connects */
2613 	if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) {
2614 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2615 		    "hubd_restore_device_state: turn on port power failed");
2616 
2617 		/* disable whatever was enabled */
2618 		(void) hubd_disable_all_port_power(hubd);
2619 
2620 		(void) hubd_pm_idle_component(hubd, dip, 0);
2621 		mutex_exit(HUBD_MUTEX(hubd));
2622 
2623 		return;
2624 	}
2625 
2626 	/*
2627 	 * wait at least 3 frames before accessing devices
2628 	 * (note that delay's minimal time is one clock tick which
2629 	 * is 10ms unless hires_tick has been changed)
2630 	 */
2631 	mutex_exit(HUBD_MUTEX(hubd));
2632 	delay(drv_usectohz(10000));
2633 	mutex_enter(HUBD_MUTEX(hubd));
2634 
2635 	hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER;
2636 
2637 	for (port = 1; port <= hubd->h_nports; port++) {
2638 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
2639 		    "hubd_restore_device_state: port=%d", port);
2640 
2641 		/*
2642 		 * the childen_dips list may have dips that have been
2643 		 * already deallocated. we only get a post_detach notification
2644 		 * but not a destroy notification
2645 		 */
2646 		ch_dip = hubd->h_children_dips[port];
2647 		if (ch_dip) {
2648 			/* get port status */
2649 			(void) hubd_determine_port_status(hubd, port,
2650 			    &status, &change, NULL, PORT_CHANGE_CSC);
2651 
2652 			/* check if it is truly connected */
2653 			if (status & PORT_STATUS_CCS) {
2654 				/*
2655 				 * Now reset port and assign the device
2656 				 * its original address
2657 				 */
2658 				retry = 0;
2659 				do {
2660 					(void) hubd_reset_port(hubd, port);
2661 
2662 					/* required for ppx */
2663 					(void) hubd_enable_port(hubd, port);
2664 
2665 					if (retry) {
2666 						mutex_exit(HUBD_MUTEX(hubd));
2667 						delay(drv_usectohz(
2668 						    hubd_device_delay/2));
2669 						mutex_enter(HUBD_MUTEX(hubd));
2670 					}
2671 
2672 					rval = hubd_setdevaddr(hubd, port);
2673 					retry++;
2674 				} while ((rval != USB_SUCCESS) &&
2675 				    (retry < hubd_retry_enumerate));
2676 
2677 				hubd_setdevconfig(hubd, port);
2678 
2679 				if (hub_prev_state == USB_DEV_DISCONNECTED) {
2680 					/* post a connect event */
2681 					mutex_exit(HUBD_MUTEX(hubd));
2682 					hubd_post_event(hubd, port,
2683 					    USBA_EVENT_TAG_HOT_INSERTION);
2684 					mutex_enter(HUBD_MUTEX(hubd));
2685 				} else {
2686 					/*
2687 					 * Since we have this device connected
2688 					 * mark it reinserted to prevent
2689 					 * cleanup thread from stepping in.
2690 					 */
2691 					mutex_exit(HUBD_MUTEX(hubd));
2692 					mutex_enter(&(DEVI(ch_dip)->devi_lock));
2693 					DEVI_SET_DEVICE_REINSERTED(ch_dip);
2694 					mutex_exit(&(DEVI(ch_dip)->devi_lock));
2695 
2696 					/*
2697 					 * reopen pipes for children for
2698 					 * their DDI_RESUME
2699 					 */
2700 					rval = usba_persistent_pipe_open(
2701 					    usba_get_usba_device(ch_dip));
2702 					mutex_enter(HUBD_MUTEX(hubd));
2703 					ASSERT(rval == USB_SUCCESS);
2704 				}
2705 			} else {
2706 				/*
2707 				 * Mark this dip for deletion as the device
2708 				 * is not physically present, and schedule
2709 				 * cleanup thread upon post resume
2710 				 */
2711 				mutex_exit(HUBD_MUTEX(hubd));
2712 
2713 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2714 				    hubd->h_log_handle,
2715 				    "hubd_restore_device_state: "
2716 				    "dip=%p on port=%d marked for cleanup",
2717 				    (void *)ch_dip, port);
2718 				mutex_enter(&(DEVI(ch_dip)->devi_lock));
2719 				DEVI_SET_DEVICE_REMOVED(ch_dip);
2720 				mutex_exit(&(DEVI(ch_dip)->devi_lock));
2721 
2722 				mutex_enter(HUBD_MUTEX(hubd));
2723 			}
2724 		} else if (ehci_root_hub) {
2725 			/* get port status */
2726 			(void) hubd_determine_port_status(hubd, port,
2727 			    &status, &change, NULL, PORT_CHANGE_CSC);
2728 
2729 			/* check if it is truly connected */
2730 			if (status & PORT_STATUS_CCS) {
2731 				/*
2732 				 * reset the port to find out if we have
2733 				 * 2.0 device connected or 1.X. A 2.0
2734 				 * device will still be seen as connected,
2735 				 * while a 1.X device will switch over to
2736 				 * the companion controller.
2737 				 */
2738 				(void) hubd_reset_port(hubd, port);
2739 
2740 				(void) hubd_determine_port_status(hubd, port,
2741 				    &status, &change, NULL, PORT_CHANGE_CSC);
2742 
2743 				if (status &
2744 				    (PORT_STATUS_CCS | PORT_STATUS_HSDA)) {
2745 					/*
2746 					 * We have a USB 2.0 device
2747 					 * connected. Power cycle this port
2748 					 * so that hotplug thread can
2749 					 * enumerate this device.
2750 					 */
2751 					(void) hubd_toggle_port(hubd, port);
2752 				} else {
2753 					USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2754 					    hubd->h_log_handle,
2755 					    "hubd_restore_device_state: "
2756 					    "device on port %d switched over",
2757 					    port);
2758 				}
2759 			}
2760 
2761 		}
2762 	}
2763 
2764 
2765 	/* if the device had remote wakeup earlier, enable it again */
2766 	if (hubd->h_hubpm->hubp_wakeup_enabled) {
2767 		mutex_exit(HUBD_MUTEX(hubd));
2768 		(void) usb_handle_remote_wakeup(hubd->h_dip,
2769 		    USB_REMOTE_WAKEUP_ENABLE);
2770 		mutex_enter(HUBD_MUTEX(hubd));
2771 	}
2772 
2773 	hubd->h_dev_state = USB_DEV_ONLINE;
2774 	hubd_start_polling(hubd, 0);
2775 	(void) hubd_pm_idle_component(hubd, dip, 0);
2776 	mutex_exit(HUBD_MUTEX(hubd));
2777 }
2778 
2779 
2780 /*
2781  * hubd_cleanup:
2782  *	cleanup hubd and deallocate. this function is called for
2783  *	handling attach failures and detaching including dynamic
2784  *	reconfiguration. If called from attaching, it must clean
2785  *	up the whole thing and return success.
2786  */
2787 /*ARGSUSED*/
2788 static int
2789 hubd_cleanup(dev_info_t *dip, hubd_t *hubd)
2790 {
2791 	int		circ, rval, old_dev_state;
2792 	hub_power_t	*hubpm;
2793 #ifdef DEBUG
2794 	usb_port_t	port;
2795 #endif
2796 
2797 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2798 	    "hubd_cleanup:");
2799 
2800 	if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) {
2801 		goto done;
2802 	}
2803 
2804 	/* ensure we are the only one active */
2805 	ndi_devi_enter(dip, &circ);
2806 
2807 	mutex_enter(HUBD_MUTEX(hubd));
2808 
2809 	/* Cleanup failure is only allowed if called from detach */
2810 	if (DEVI_IS_DETACHING(dip)) {
2811 		dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
2812 
2813 		/*
2814 		 * We are being called from detach.
2815 		 * Fail immediately if the hotplug thread is running
2816 		 * else set the dev_state to disconnected so that
2817 		 * hotplug thread just exits without doing anything.
2818 		 */
2819 		if (hubd->h_bus_ctls || hubd->h_bus_pwr ||
2820 		    hubd->h_hotplug_thread) {
2821 			mutex_exit(HUBD_MUTEX(hubd));
2822 			ndi_devi_exit(dip, circ);
2823 
2824 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2825 			    "hubd_cleanup: hotplug thread/bus ctl active "
2826 			    "- failing detach");
2827 
2828 			return (USB_FAILURE);
2829 		}
2830 
2831 		/*
2832 		 * if the deathrow thread is still active or about
2833 		 * to become active, fail detach
2834 		 * the roothup can only be detached if nexus drivers
2835 		 * are unloaded or explicitly offlined
2836 		 */
2837 		if (rh_dip == dip) {
2838 			if (hubd->h_cleanup_needed ||
2839 			    hubd->h_cleanup_active) {
2840 				mutex_exit(HUBD_MUTEX(hubd));
2841 				ndi_devi_exit(dip, circ);
2842 
2843 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2844 				    hubd->h_log_handle,
2845 				    "hubd_cleanup: deathrow still active?"
2846 				    "- failing detach");
2847 
2848 				return (USB_FAILURE);
2849 			}
2850 		}
2851 	}
2852 
2853 	old_dev_state = hubd->h_dev_state;
2854 	hubd->h_dev_state = USB_DEV_DISCONNECTED;
2855 
2856 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2857 	    "hubd_cleanup: stop polling");
2858 	hubd_close_intr_pipe(hubd);
2859 
2860 	ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr ||
2861 	    hubd->h_hotplug_thread) == 0);
2862 	mutex_exit(HUBD_MUTEX(hubd));
2863 
2864 	/*
2865 	 * deallocate events, if events are still registered
2866 	 * (ie. children still attached) then we have to fail the detach
2867 	 */
2868 	if (hubd->h_ndi_event_hdl) {
2869 
2870 		rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl);
2871 		if (DEVI_IS_ATTACHING(dip)) {
2872 
2873 			/* It must return success if attaching. */
2874 			ASSERT(rval == NDI_SUCCESS);
2875 
2876 		} else if (rval != NDI_SUCCESS) {
2877 
2878 			USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle,
2879 			    "hubd_cleanup: ndi_event_free_hdl failed");
2880 			ndi_devi_exit(dip, circ);
2881 
2882 			return (USB_FAILURE);
2883 
2884 		}
2885 	}
2886 
2887 	mutex_enter(HUBD_MUTEX(hubd));
2888 
2889 	if (hubd->h_init_state & HUBD_CHILDREN_CREATED) {
2890 #ifdef DEBUG
2891 		for (port = 1; port <= hubd->h_nports; port++) {
2892 			ASSERT(hubd->h_usba_devices[port] == NULL);
2893 			ASSERT(hubd->h_children_dips[port] == NULL);
2894 		}
2895 #endif
2896 		kmem_free(hubd->h_children_dips, hubd->h_cd_list_length);
2897 		kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length);
2898 	}
2899 
2900 	/*
2901 	 * Disable the event callbacks first, after this point, event
2902 	 * callbacks will never get called. Note we shouldn't hold
2903 	 * mutex while unregistering events because there may be a
2904 	 * competing event callback thread. Event callbacks are done
2905 	 * with ndi mutex held and this can cause a potential deadlock.
2906 	 * Note that cleanup can't fail after deregistration of events.
2907 	 */
2908 	if (hubd->h_init_state &  HUBD_EVENTS_REGISTERED) {
2909 		mutex_exit(HUBD_MUTEX(hubd));
2910 		usb_unregister_event_cbs(dip, &hubd_events);
2911 		hubd_unregister_cpr_callback(hubd);
2912 		mutex_enter(HUBD_MUTEX(hubd));
2913 	}
2914 
2915 	/* restore the old dev state so that device can be put into low power */
2916 	hubd->h_dev_state = old_dev_state;
2917 	hubpm = hubd->h_hubpm;
2918 
2919 	if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) {
2920 		(void) hubd_pm_busy_component(hubd, dip, 0);
2921 		mutex_exit(HUBD_MUTEX(hubd));
2922 		if (hubd->h_hubpm->hubp_wakeup_enabled) {
2923 			/*
2924 			 * Bring the hub to full power before
2925 			 * issuing the disable remote wakeup command
2926 			 */
2927 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
2928 
2929 			if ((rval = usb_handle_remote_wakeup(hubd->h_dip,
2930 			    USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) {
2931 				USB_DPRINTF_L2(DPRINT_MASK_PM,
2932 				    hubd->h_log_handle,
2933 				    "hubd_cleanup: disable remote wakeup "
2934 				    "fails=%d", rval);
2935 			}
2936 		}
2937 
2938 		(void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF);
2939 
2940 		mutex_enter(HUBD_MUTEX(hubd));
2941 		(void) hubd_pm_idle_component(hubd, dip, 0);
2942 	}
2943 
2944 	if (hubpm) {
2945 		if (hubpm->hubp_child_pwrstate) {
2946 			kmem_free(hubpm->hubp_child_pwrstate,
2947 			    MAX_PORTS + 1);
2948 		}
2949 		kmem_free(hubpm, sizeof (hub_power_t));
2950 	}
2951 	mutex_exit(HUBD_MUTEX(hubd));
2952 
2953 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2954 	    "hubd_cleanup: freeing space");
2955 
2956 	if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) {
2957 		rval = usba_hubdi_unregister(dip);
2958 		ASSERT(rval == USB_SUCCESS);
2959 	}
2960 
2961 	if (hubd->h_init_state & HUBD_LOCKS_DONE) {
2962 		mutex_destroy(HUBD_MUTEX(hubd));
2963 		cv_destroy(&hubd->h_cv_reset_port);
2964 		cv_destroy(&hubd->h_cv_hotplug_dev);
2965 	}
2966 
2967 	ndi_devi_exit(dip, circ);
2968 
2969 	if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) {
2970 		ddi_remove_minor_node(dip, NULL);
2971 	}
2972 
2973 	if (usba_is_root_hub(dip)) {
2974 		usb_pipe_close(dip, hubd->h_default_pipe,
2975 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2976 	}
2977 
2978 done:
2979 	if (hubd->h_ancestry_str) {
2980 		kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN);
2981 	}
2982 
2983 	usb_client_detach(dip, hubd->h_dev_data);
2984 
2985 	usb_free_log_hdl(hubd->h_log_handle);
2986 
2987 	if (!usba_is_root_hub(dip)) {
2988 		ddi_soft_state_free(hubd_statep, ddi_get_instance(dip));
2989 	}
2990 
2991 	ddi_prop_remove_all(dip);
2992 
2993 	return (USB_SUCCESS);
2994 }
2995 
2996 
2997 /*
2998  * hubd_determine_port_connection:
2999  *	Determine which port is in connect status but does not
3000  *	have connect status change bit set, and mark port change
3001  *	bit accordingly.
3002  *	This function is applied during hub attach time.
3003  */
3004 static usb_port_mask_t
3005 hubd_determine_port_connection(hubd_t	*hubd)
3006 {
3007 	usb_port_t	port;
3008 	uint16_t	status;
3009 	uint16_t	change;
3010 	usb_port_mask_t	port_change = 0;
3011 
3012 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3013 
3014 	for (port = 1; port <= hubd->h_nports; port++) {
3015 
3016 		(void) hubd_determine_port_status(hubd, port, &status,
3017 		    &change, NULL, 0);
3018 
3019 		/* Check if port is in connect status */
3020 		if (!(status & PORT_STATUS_CCS)) {
3021 
3022 			continue;
3023 		}
3024 
3025 		/*
3026 		 * Check if port Connect Status Change bit has been set.
3027 		 * If already set, the connection will be handled by
3028 		 * intr polling callback, not during attach.
3029 		 */
3030 		if (change & PORT_CHANGE_CSC) {
3031 
3032 			continue;
3033 		}
3034 
3035 		port_change |= 1 << port;
3036 	}
3037 
3038 	return (port_change);
3039 }
3040 
3041 
3042 /*
3043  * hubd_check_ports:
3044  *	- get hub descriptor
3045  *	- check initial port status
3046  *	- enable power on all ports
3047  *	- enable polling on ep1
3048  */
3049 static int
3050 hubd_check_ports(hubd_t  *hubd)
3051 {
3052 	int			rval;
3053 	usb_port_mask_t		port_change = 0;
3054 	hubd_hotplug_arg_t	*arg;
3055 
3056 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3057 
3058 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
3059 	    "hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip));
3060 
3061 	/*
3062 	 * First turn off all port power
3063 	 */
3064 	if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) {
3065 
3066 		/* disable whatever was enabled */
3067 		(void) hubd_disable_all_port_power(hubd);
3068 
3069 		return (rval);
3070 	}
3071 
3072 	/*
3073 	 * do not switch on immediately (instantly on root hub)
3074 	 * and allow time to settle
3075 	 */
3076 	mutex_exit(HUBD_MUTEX(hubd));
3077 	delay(drv_usectohz(10000));
3078 	mutex_enter(HUBD_MUTEX(hubd));
3079 
3080 	/*
3081 	 * enable power on all ports so we can see connects
3082 	 */
3083 	if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) {
3084 		/* disable whatever was enabled */
3085 		(void) hubd_disable_all_port_power(hubd);
3086 
3087 		return (rval);
3088 	}
3089 
3090 	/* wait at least 3 frames before accessing devices */
3091 	mutex_exit(HUBD_MUTEX(hubd));
3092 	delay(drv_usectohz(10000));
3093 	mutex_enter(HUBD_MUTEX(hubd));
3094 
3095 	/*
3096 	 * allocate arrays for saving the dips of each child per port
3097 	 *
3098 	 * ports go from 1 - n, allocate 1 more entry
3099 	 */
3100 	hubd->h_cd_list_length =
3101 	    (sizeof (dev_info_t **)) * (hubd->h_nports + 1);
3102 
3103 	hubd->h_children_dips = (dev_info_t **)kmem_zalloc(
3104 	    hubd->h_cd_list_length, KM_SLEEP);
3105 	hubd->h_usba_devices = (usba_device_t **)kmem_zalloc(
3106 	    hubd->h_cd_list_length, KM_SLEEP);
3107 
3108 	hubd->h_init_state |= HUBD_CHILDREN_CREATED;
3109 
3110 	mutex_exit(HUBD_MUTEX(hubd));
3111 	arg = (hubd_hotplug_arg_t *)kmem_zalloc(
3112 	    sizeof (hubd_hotplug_arg_t), KM_SLEEP);
3113 	mutex_enter(HUBD_MUTEX(hubd));
3114 
3115 	if ((rval = hubd_open_intr_pipe(hubd)) != USB_SUCCESS) {
3116 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3117 
3118 		return (rval);
3119 	}
3120 
3121 	hubd_start_polling(hubd, 0);
3122 
3123 	/*
3124 	 * Some hub devices, like the embedded hub in the CKS ErgoMagic
3125 	 * keyboard, may only have connection status bit set, but not
3126 	 * have connect status change bit set when a device has been
3127 	 * connected to its downstream port before the hub is enumerated.
3128 	 * Then when the hub is in enumeration, the devices connected to
3129 	 * it cannot be detected by the intr pipe and won't be enumerated.
3130 	 * We need to check such situation here and enumerate the downstream
3131 	 * devices for such hubs.
3132 	 */
3133 	port_change = hubd_determine_port_connection(hubd);
3134 
3135 	if (port_change != 0 || hubd->h_port_change != 0) {
3136 		hubd_pm_busy_component(hubd, hubd->h_dip, 0);
3137 
3138 		arg->hubd = hubd;
3139 		arg->hotplug_during_attach = B_TRUE;
3140 		hubd->h_port_change |= port_change;
3141 
3142 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
3143 		    "hubd_check_ports: port change=0x%x, need to connect",
3144 		    hubd->h_port_change);
3145 
3146 		if (usb_async_req(hubd->h_dip, hubd_hotplug_thread,
3147 		    (void *)arg, 0) == USB_SUCCESS) {
3148 			hubd->h_hotplug_thread++;
3149 		} else {
3150 			/* mark this device as idle */
3151 			hubd_pm_idle_component(hubd, hubd->h_dip, 0);
3152 			kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3153 		}
3154 	} else {
3155 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3156 	}
3157 
3158 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
3159 	    "hubd_check_ports done");
3160 
3161 	return (USB_SUCCESS);
3162 }
3163 
3164 
3165 /*
3166  * hubd_get_hub_descriptor:
3167  */
3168 static int
3169 hubd_get_hub_descriptor(hubd_t *hubd)
3170 {
3171 	mblk_t		*data = NULL;
3172 	usb_cr_t	completion_reason;
3173 	usb_cb_flags_t	cb_flags;
3174 	uint16_t	length, wValue;
3175 	int		rval;
3176 	usb_req_attrs_t attr = 0;
3177 
3178 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3179 	    "hubd_get_hub_descriptor:");
3180 
3181 	if ((hubd->h_dev_data->dev_descr->idVendor == USB_HUB_INTEL_VID) &&
3182 	    (hubd->h_dev_data->dev_descr->idProduct == USB_HUB_INTEL_PID)) {
3183 		attr = USB_ATTRS_SHORT_XFER_OK;
3184 	}
3185 
3186 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3187 	ASSERT(hubd->h_default_pipe != 0);
3188 
3189 	mutex_exit(HUBD_MUTEX(hubd));
3190 
3191 	/*
3192 	 * The contents of wValue change depending on whether this is a USB 2 or
3193 	 * USB 3 device. SuperSpeed Hubs have different descriptors and you
3194 	 * cannot ask them for the traditional USB 2 descriptor.
3195 	 */
3196 	if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
3197 		wValue = USB_DESCR_TYPE_SS_HUB << 8 | HUBD_DEFAULT_DESC_INDEX;
3198 	} else {
3199 		wValue = USB_DESCR_TYPE_HUB << 8 | HUBD_DEFAULT_DESC_INDEX;
3200 	}
3201 
3202 	/*
3203 	 * The hub descriptor length varies in various versions of USB. For
3204 	 * example, in USB 2 it's at least 9 bytes long. To start with, we
3205 	 * always get the first 8 bytes so we can figure out how long it
3206 	 * actually is.
3207 	 */
3208 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3209 	    hubd->h_default_pipe,
3210 	    HUB_CLASS_REQ_TYPE,
3211 	    USB_REQ_GET_DESCR,		/* bRequest */
3212 	    wValue,			/* wValue */
3213 	    0,				/* wIndex */
3214 	    8,				/* wLength */
3215 	    &data, 0,
3216 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
3217 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3218 		    "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
3219 		    completion_reason, cb_flags, rval);
3220 		freemsg(data);
3221 		mutex_enter(HUBD_MUTEX(hubd));
3222 
3223 		return (rval);
3224 	}
3225 
3226 	length = *(data->b_rptr);
3227 
3228 	if (length > 8) {
3229 		freemsg(data);
3230 		data = NULL;
3231 
3232 		/* get complete hub descriptor */
3233 		rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3234 		    hubd->h_default_pipe,
3235 		    HUB_CLASS_REQ_TYPE,
3236 		    USB_REQ_GET_DESCR,		/* bRequest */
3237 		    wValue,			/* wValue */
3238 		    0,				/* wIndex */
3239 		    length,			/* wLength */
3240 		    &data, attr,
3241 		    &completion_reason, &cb_flags, 0);
3242 
3243 		/*
3244 		 * Hub descriptor data less than 9 bytes is not valid and
3245 		 * may cause trouble if we use it. See USB2.0 Tab11-13.
3246 		 */
3247 		if ((rval != USB_SUCCESS) || (MBLKL(data) <= 8)) {
3248 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3249 			    "get hub descriptor failed: "
3250 			    "cr=%d cb_fl=0x%x rval=%d, len=%ld",
3251 			    completion_reason, cb_flags, rval,
3252 			    (data)?MBLKL(data):0);
3253 			freemsg(data);
3254 			mutex_enter(HUBD_MUTEX(hubd));
3255 
3256 			return (rval);
3257 		}
3258 	}
3259 
3260 	mutex_enter(HUBD_MUTEX(hubd));
3261 
3262 	/*
3263 	 * Parse the hub descriptor. Note that the format of the descriptor
3264 	 * itself depends on the USB version. We handle the different ones and
3265 	 * transform it into a single uniform view.
3266 	 */
3267 
3268 	ASSERT(*(data->b_rptr + 2) <= (MAX_PORTS + 1));
3269 	if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
3270 		usb_ss_hub_descr_t hub_descr;
3271 		char *desc = "cccscccs";
3272 		ASSERT(*(data->b_rptr + 1) == ROOT_HUB_SS_DESCRIPTOR_TYPE);
3273 
3274 		/*
3275 		 * Note many hubs may support less than the 255 devices that the
3276 		 * USB specification allows for. In those cases, we'll simply
3277 		 * read less and it should be okay.
3278 		 */
3279 		if (usb_parse_CV_descr(desc, data->b_rptr, MBLKL(data),
3280 		    (void *)&hub_descr, sizeof (hub_descr)) == 0) {
3281 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3282 			    "parsing hub descriptor failed");
3283 			freemsg(data);
3284 			return (USB_FAILURE);
3285 		}
3286 
3287 		hubd->h_nports = hub_descr.bNbrPorts;
3288 		hubd->h_hub_chars = hub_descr.wHubCharacteristics;
3289 		hubd->h_power_good = hub_descr.bPwrOn2PwrGood;
3290 		hubd->h_current = hub_descr.bHubContrCurrent;
3291 	} else {
3292 		usb_hub_descr_t hub_descr;
3293 		if (usb_parse_CV_descr("cccscccccc",
3294 		    data->b_rptr, MBLKL(data),
3295 		    (void *)&hub_descr, sizeof (usb_hub_descr_t)) == 0) {
3296 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3297 			    "parsing hub descriptor failed");
3298 			freemsg(data);
3299 			return (USB_FAILURE);
3300 		}
3301 
3302 		hubd->h_nports = hub_descr.bNbrPorts;
3303 		hubd->h_hub_chars = hub_descr.wHubCharacteristics;
3304 		hubd->h_power_good = hub_descr.bPwrOn2PwrGood;
3305 		hubd->h_current = hub_descr.bHubContrCurrent;
3306 	}
3307 
3308 	freemsg(data);
3309 
3310 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
3311 	    "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
3312 	    "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval,
3313 	    hubd->h_nports, hubd->h_hub_chars,
3314 	    hubd->h_power_good, hubd->h_current);
3315 
3316 	if (hubd->h_nports > MAX_PORTS) {
3317 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
3318 		    "Hub driver supports max of %d ports on hub. "
3319 		    "Hence using the first %d port of %d ports available",
3320 		    MAX_PORTS, MAX_PORTS, hubd->h_nports);
3321 
3322 		hubd->h_nports = MAX_PORTS;
3323 	}
3324 
3325 	return (USB_SUCCESS);
3326 }
3327 
3328 static int
3329 hubd_set_hub_depth(hubd_t *hubd)
3330 {
3331 	int rval;
3332 	usb_cr_t	completion_reason;
3333 	usb_cb_flags_t	cb_flags;
3334 	usba_device_t 	*ud;
3335 	uint16_t 	depth;
3336 
3337 	/*
3338 	 * We only need to set the hub depth devices for hubs that are at least
3339 	 * SuperSpeed devices. This didn't exist for USB 2.0 and older hubs.
3340 	 * There's also no need to call this on the root hub.
3341 	 */
3342 	if (hubd->h_usba_device->usb_port_status < USBA_SUPER_SPEED_DEV ||
3343 	    usba_is_root_hub(hubd->h_dip))
3344 		return (USB_SUCCESS);
3345 
3346 	depth = 0;
3347 	ud = hubd->h_usba_device;
3348 	while (ud->usb_parent_hub != NULL) {
3349 		depth++;
3350 		ud = ud->usb_parent_hub;
3351 	}
3352 	ASSERT(depth > 0);
3353 
3354 	if (depth > HUBD_SS_MAX_DEPTH) {
3355 		const char *mfg, *prod;
3356 
3357 		ud = hubd->h_usba_device;
3358 		prod = ud->usb_product_str;
3359 		if (prod == NULL)
3360 			prod = "Unknown Device";
3361 		mfg = ud->usb_mfg_str;
3362 		if (mfg == NULL)
3363 			mfg = "Unknown Manufacturer";
3364 		cmn_err(CE_WARN, "Unable to attach USB 3.x hub %s %s. A "
3365 		    "maximum of %d hubs may be cascaded", mfg, prod,
3366 		    HUBD_SS_MAX_DEPTH);
3367 		return (USB_FAILURE);
3368 	}
3369 
3370 	/*
3371 	 * When making the HUB_REQ_SET_HUB_DEPTH request, a hub connected to a
3372 	 * root port is considered to have a hub depth of zero whereas we
3373 	 * consider having a hub depth of one above.
3374 	 */
3375 	depth--;
3376 
3377 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3378 	    hubd->h_default_pipe,
3379 	    HUB_SET_HUB_DEPTH_TYPE,
3380 	    HUB_REQ_SET_HUB_DEPTH,	/* bRequest */
3381 	    depth,			/* wValue */
3382 	    0,				/* wIndex */
3383 	    0,				/* wLength */
3384 	    NULL, 0,
3385 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
3386 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3387 		    "get set hub depth failed: cr=%d cb=0x%x",
3388 		    completion_reason, cb_flags);
3389 	}
3390 
3391 	return (rval);
3392 }
3393 
3394 /*
3395  * hubd_get_hub_status_words:
3396  */
3397 static int
3398 hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status)
3399 {
3400 	usb_cr_t	completion_reason;
3401 	usb_cb_flags_t	cb_flags;
3402 	mblk_t		*data = NULL;
3403 
3404 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3405 
3406 	mutex_exit(HUBD_MUTEX(hubd));
3407 
3408 	if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe,
3409 	    HUB_CLASS_REQ_TYPE,
3410 	    USB_REQ_GET_STATUS,
3411 	    0,
3412 	    0,
3413 	    GET_STATUS_LENGTH,
3414 	    &data, 0,
3415 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
3416 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3417 		    "get hub status failed: cr=%d cb=0x%x",
3418 		    completion_reason, cb_flags);
3419 
3420 		if (data) {
3421 			freemsg(data);
3422 		}
3423 
3424 		mutex_enter(HUBD_MUTEX(hubd));
3425 
3426 		return (USB_FAILURE);
3427 	}
3428 
3429 	mutex_enter(HUBD_MUTEX(hubd));
3430 
3431 	status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
3432 	status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
3433 
3434 	USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
3435 	    "hub status=0x%x change=0x%x", status[0], status[1]);
3436 
3437 	freemsg(data);
3438 
3439 	return (USB_SUCCESS);
3440 }
3441 
3442 
3443 /*
3444  * hubd_open_intr_pipe:
3445  *	we read all descriptors first for curiosity and then simply
3446  *	open the pipe
3447  */
3448 static int
3449 hubd_open_intr_pipe(hubd_t	*hubd)
3450 {
3451 	int			rval;
3452 
3453 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3454 	    "hubd_open_intr_pipe:");
3455 
3456 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE);
3457 
3458 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING;
3459 	mutex_exit(HUBD_MUTEX(hubd));
3460 
3461 	if ((rval = usb_pipe_xopen(hubd->h_dip,
3462 	    &hubd->h_ep1_xdescr, &hubd->h_pipe_policy,
3463 	    0, &hubd->h_ep1_ph)) != USB_SUCCESS) {
3464 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3465 		    "open intr pipe failed (%d)", rval);
3466 
3467 		mutex_enter(HUBD_MUTEX(hubd));
3468 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
3469 
3470 		return (rval);
3471 	}
3472 
3473 	mutex_enter(HUBD_MUTEX(hubd));
3474 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
3475 
3476 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3477 	    "open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph);
3478 
3479 	return (USB_SUCCESS);
3480 }
3481 
3482 
3483 /*
3484  * hubd_start_polling:
3485  *	start or restart the polling
3486  */
3487 static void
3488 hubd_start_polling(hubd_t *hubd, int always)
3489 {
3490 	usb_intr_req_t	*reqp;
3491 	int			rval;
3492 	usb_pipe_state_t	pipe_state;
3493 
3494 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3495 	    "start polling: always=%d dev_state=%d pipe_state=%d\n\t"
3496 	    "thread=%d ep1_ph=0x%p",
3497 	    always, hubd->h_dev_state, hubd->h_intr_pipe_state,
3498 	    hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph);
3499 
3500 	/*
3501 	 * start or restart polling on the intr pipe
3502 	 * only if hotplug thread is not running
3503 	 */
3504 	if ((always == HUBD_ALWAYS_START_POLLING) ||
3505 	    ((hubd->h_dev_state == USB_DEV_ONLINE) &&
3506 	    (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
3507 	    (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) {
3508 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3509 		    "start polling requested");
3510 
3511 		reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP);
3512 
3513 		reqp->intr_client_private = (usb_opaque_t)hubd;
3514 		reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
3515 		    USB_ATTRS_AUTOCLEARING;
3516 		reqp->intr_len = hubd->h_ep1_xdescr.uex_ep.wMaxPacketSize;
3517 		reqp->intr_cb = hubd_read_cb;
3518 		reqp->intr_exc_cb = hubd_exception_cb;
3519 		mutex_exit(HUBD_MUTEX(hubd));
3520 		if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp,
3521 		    USB_FLAGS_SLEEP)) != USB_SUCCESS) {
3522 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3523 			    "start polling failed, rval=%d", rval);
3524 			usb_free_intr_req(reqp);
3525 		}
3526 
3527 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
3528 		    USB_FLAGS_SLEEP);
3529 		if (pipe_state != USB_PIPE_STATE_ACTIVE) {
3530 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
3531 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
3532 		}
3533 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3534 		    "start polling request 0x%p", (void *)reqp);
3535 
3536