xref: /illumos-gate/usr/src/uts/common/io/usb/usba/hubdi.c (revision 993e3faf)
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 2016 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 	}
1801 	mutex_exit(HUBD_MUTEX(hubd));
1802 
1803 	ndi_devi_exit(dip, circ);
1804 
1805 	return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
1806 }
1807 
1808 
1809 /* power entry point for the root hub */
1810 int
1811 usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level)
1812 {
1813 	return (usba_hubdi_power(dip, comp, level));
1814 }
1815 
1816 
1817 /*
1818  * standard driver entry points support code
1819  */
1820 int
1821 usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1822 {
1823 	int			instance = ddi_get_instance(dip);
1824 	hubd_t			*hubd = NULL;
1825 	int			i, rval;
1826 	int			minor;
1827 	uint8_t			ports_count;
1828 	char			*log_name = NULL;
1829 	const char		*root_hub_drvname;
1830 	usb_ep_data_t		*ep_data;
1831 	usba_device_t		*child_ud = NULL;
1832 	usb_dev_descr_t		*usb_dev_descr;
1833 	usb_port_status_t	parent_port_status, child_port_status;
1834 
1835 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle,
1836 	    "hubd_attach instance %d, cmd=0x%x", instance, cmd);
1837 
1838 	switch (cmd) {
1839 	case DDI_ATTACH:
1840 
1841 		break;
1842 	case DDI_RESUME:
1843 		hubd_cpr_resume(dip);
1844 
1845 		return (DDI_SUCCESS);
1846 	default:
1847 		return (DDI_FAILURE);
1848 	}
1849 
1850 	/*
1851 	 * Allocate softc information.
1852 	 */
1853 	if (usba_is_root_hub(dip)) {
1854 		/* soft state has already been allocated */
1855 		hubd = hubd_get_soft_state(dip);
1856 		minor = HUBD_IS_ROOT_HUB;
1857 
1858 		/* generate readable labels for different root hubs */
1859 		root_hub_drvname = ddi_driver_name(dip);
1860 		if (strcmp(root_hub_drvname, "xhci") == 0) {
1861 			log_name = "xusb";
1862 		} else if (strcmp(root_hub_drvname, "ehci") == 0) {
1863 			log_name = "eusb";
1864 		} else if (strcmp(root_hub_drvname, "uhci") == 0) {
1865 			log_name = "uusb";
1866 		} else {
1867 			/* std. for ohci */
1868 			log_name = "usb";
1869 		}
1870 	} else {
1871 		rval = ddi_soft_state_zalloc(hubd_statep, instance);
1872 		minor = 0;
1873 
1874 		if (rval != DDI_SUCCESS) {
1875 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
1876 			    "cannot allocate soft state (%d)", instance);
1877 			goto fail;
1878 		}
1879 
1880 		hubd = hubd_get_soft_state(dip);
1881 		if (hubd == NULL) {
1882 			goto fail;
1883 		}
1884 	}
1885 
1886 	hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel,
1887 	    &hubd_errmask, &hubd_instance_debug, 0);
1888 
1889 	hubd->h_usba_device	= child_ud = usba_get_usba_device(dip);
1890 	hubd->h_dip		= dip;
1891 	hubd->h_instance	= instance;
1892 
1893 	mutex_enter(&child_ud->usb_mutex);
1894 	child_port_status = child_ud->usb_port_status;
1895 	usb_dev_descr = child_ud->usb_dev_descr;
1896 	parent_port_status = (child_ud->usb_hs_hub_usba_dev) ?
1897 	    child_ud->usb_hs_hub_usba_dev->usb_port_status : 0;
1898 	mutex_exit(&child_ud->usb_mutex);
1899 
1900 	if ((child_port_status == USBA_FULL_SPEED_DEV) &&
1901 	    (parent_port_status >= USBA_HIGH_SPEED_DEV) &&
1902 	    (usb_dev_descr->bcdUSB == 0x100)) {
1903 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
1904 		    "Use of a USB1.0 hub behind a higher speed port may "
1905 		    "cause unexpected failures");
1906 	}
1907 
1908 	hubd->h_pipe_policy.pp_max_async_reqs = 1;
1909 
1910 	/* register with USBA as client driver */
1911 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
1912 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1913 		    "client attach failed");
1914 
1915 		goto fail;
1916 	}
1917 
1918 	if (usb_get_dev_data(dip, &hubd->h_dev_data,
1919 	    USB_PARSE_LVL_IF, 0) != USB_SUCCESS) {
1920 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1921 		    "cannot get dev_data");
1922 
1923 		goto fail;
1924 	}
1925 
1926 	if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data,
1927 	    hubd->h_dev_data->dev_curr_if, 0, 0,
1928 	    (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) {
1929 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1930 		    "no interrupt IN endpoint found");
1931 
1932 		goto fail;
1933 	}
1934 
1935 	if (usb_ep_xdescr_fill(USB_EP_XDESCR_CURRENT_VERSION, dip, ep_data,
1936 	    &hubd->h_ep1_xdescr) != USB_SUCCESS) {
1937 		goto fail;
1938 	}
1939 
1940 	hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph;
1941 
1942 	mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER,
1943 	    hubd->h_dev_data->dev_iblock_cookie);
1944 	cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL);
1945 	cv_init(&hubd->h_cv_hotplug_dev, NULL, CV_DRIVER, NULL);
1946 
1947 	hubd->h_init_state |= HUBD_LOCKS_DONE;
1948 
1949 	usb_free_descr_tree(dip, hubd->h_dev_data);
1950 
1951 	/*
1952 	 * register this hub instance with usba
1953 	 */
1954 	rval = usba_hubdi_register(dip, 0);
1955 	if (rval != USB_SUCCESS) {
1956 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1957 		    "usba_hubdi_register failed");
1958 		goto fail;
1959 	}
1960 
1961 	mutex_enter(HUBD_MUTEX(hubd));
1962 	hubd->h_init_state |= HUBD_HUBDI_REGISTERED;
1963 	hubd->h_dev_state = USB_DEV_ONLINE;
1964 	mutex_exit(HUBD_MUTEX(hubd));
1965 
1966 	/* now create components to power manage this device */
1967 	hubd_create_pm_components(dip, hubd);
1968 
1969 	/*
1970 	 * Event handling: definition and registration
1971 	 *
1972 	 * first the  definition:
1973 	 * get event handle
1974 	 */
1975 	(void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP);
1976 
1977 	/* bind event set to the handle */
1978 	if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events,
1979 	    NDI_SLEEP)) {
1980 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
1981 		    "binding event set failed");
1982 
1983 		goto fail;
1984 	}
1985 
1986 	/* event registration */
1987 	if (hubd_register_events(hubd) != USB_SUCCESS) {
1988 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
1989 		    "hubd_register_events failed");
1990 
1991 		goto fail;
1992 	}
1993 
1994 	mutex_enter(HUBD_MUTEX(hubd));
1995 	hubd->h_init_state |= HUBD_EVENTS_REGISTERED;
1996 
1997 	if (hubd_get_hub_descriptor(hubd) != USB_SUCCESS) {
1998 		mutex_exit(HUBD_MUTEX(hubd));
1999 
2000 		goto fail;
2001 	}
2002 
2003 	/*
2004 	 * Now that we have our descriptors, we need to give the host controller
2005 	 * a chance to properly configure the device if needed (this is required
2006 	 * for xHCI).
2007 	 *
2008 	 * Note, if anyone ever adds support for using the Multi TT mode for USB
2009 	 * 2 High speed Hubs, the xhci driver will need to be updated and that
2010 	 * will need to be done before this is called.
2011 	 */
2012 	if (hubd->h_usba_device->usb_hcdi_ops->usba_hcdi_hub_update != NULL &&
2013 	    !usba_is_root_hub(dip)) {
2014 		int ret;
2015 		uint8_t chars;
2016 		usba_device_t *ud = hubd->h_usba_device;
2017 
2018 		chars = (hubd->h_hub_chars & HUB_CHARS_TT_THINK_TIME) >>
2019 		    HUB_CHARS_TT_SHIFT;
2020 		ret = ud->usb_hcdi_ops->usba_hcdi_hub_update(ud,
2021 		    hubd->h_nports, chars);
2022 		if (ret != USB_SUCCESS) {
2023 			mutex_exit(HUBD_MUTEX(hubd));
2024 			goto fail;
2025 		}
2026 	}
2027 
2028 	if (hubd_set_hub_depth(hubd) != USB_SUCCESS) {
2029 
2030 		goto fail;
2031 	}
2032 
2033 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
2034 	    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2035 	    "hub-ignore-power-budget") == 1) {
2036 		hubd->h_ignore_pwr_budget = B_TRUE;
2037 	} else {
2038 		hubd->h_ignore_pwr_budget = B_FALSE;
2039 
2040 		/* initialize hub power budget variables */
2041 		if (hubd_init_power_budget(hubd) != USB_SUCCESS) {
2042 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2043 			    "hubd_init_power_budget failed");
2044 			mutex_exit(HUBD_MUTEX(hubd));
2045 
2046 			goto fail;
2047 		}
2048 	}
2049 
2050 	/* initialize and create children */
2051 	if (hubd_check_ports(hubd) != USB_SUCCESS) {
2052 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2053 		    "hubd_check_ports failed");
2054 		mutex_exit(HUBD_MUTEX(hubd));
2055 
2056 		goto fail;
2057 	}
2058 
2059 	/*
2060 	 * create cfgadm nodes
2061 	 */
2062 	hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP);
2063 	hubd_get_ancestry_str(hubd);
2064 
2065 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2066 	    "#ports=0x%x", hubd->h_nports);
2067 
2068 	for (i = 1; i <= hubd->h_nports; i++) {
2069 		char ap_name[HUBD_APID_NAMELEN];
2070 
2071 		(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
2072 		    hubd->h_ancestry_str, i);
2073 		USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2074 		    "ap_name=%s", ap_name);
2075 
2076 		if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance,
2077 		    DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
2078 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2079 			    "cannot create attachment point node (%d)",
2080 			    instance);
2081 			mutex_exit(HUBD_MUTEX(hubd));
2082 
2083 			goto fail;
2084 		}
2085 	}
2086 
2087 	ports_count = hubd->h_nports;
2088 	mutex_exit(HUBD_MUTEX(hubd));
2089 
2090 	/* create minor nodes */
2091 	if (ddi_create_minor_node(dip, "hubd", S_IFCHR,
2092 	    instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
2093 
2094 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2095 		    "cannot create devctl minor node (%d)", instance);
2096 
2097 		goto fail;
2098 	}
2099 
2100 	mutex_enter(HUBD_MUTEX(hubd));
2101 	hubd->h_init_state |= HUBD_MINOR_NODE_CREATED;
2102 	mutex_exit(HUBD_MUTEX(hubd));
2103 
2104 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2105 	    "usb-port-count", ports_count) != DDI_PROP_SUCCESS) {
2106 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2107 		    "usb-port-count update failed");
2108 	}
2109 
2110 	/*
2111 	 * host controller driver has already reported this dev
2112 	 * if we are the root hub
2113 	 */
2114 	if (!usba_is_root_hub(dip)) {
2115 		ddi_report_dev(dip);
2116 	}
2117 
2118 	/* enable deathrow thread */
2119 	hubd->h_cleanup_enabled = B_TRUE;
2120 	mutex_enter(HUBD_MUTEX(hubd));
2121 	hubd_pm_idle_component(hubd, dip, 0);
2122 	mutex_exit(HUBD_MUTEX(hubd));
2123 
2124 	return (DDI_SUCCESS);
2125 
2126 fail:
2127 	{
2128 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2129 
2130 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
2131 		    "cannot attach %s", ddi_pathname(dip, pathname));
2132 
2133 		kmem_free(pathname, MAXPATHLEN);
2134 	}
2135 
2136 	mutex_enter(HUBD_MUTEX(hubd));
2137 	hubd_pm_idle_component(hubd, dip, 0);
2138 	mutex_exit(HUBD_MUTEX(hubd));
2139 
2140 	if (hubd) {
2141 		rval = hubd_cleanup(dip, hubd);
2142 		if (rval != USB_SUCCESS) {
2143 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
2144 			    "failure to complete cleanup after attach failure");
2145 		}
2146 	}
2147 
2148 	return (DDI_FAILURE);
2149 }
2150 
2151 
2152 int
2153 usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2154 {
2155 	hubd_t	*hubd = hubd_get_soft_state(dip);
2156 	int	rval;
2157 
2158 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2159 	    "hubd_detach: cmd=0x%x", cmd);
2160 
2161 	switch (cmd) {
2162 	case DDI_DETACH:
2163 		rval = hubd_cleanup(dip, hubd);
2164 
2165 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
2166 	case DDI_SUSPEND:
2167 		rval = hubd_cpr_suspend(hubd);
2168 
2169 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
2170 	default:
2171 		return (DDI_FAILURE);
2172 	}
2173 }
2174 
2175 
2176 /*
2177  * hubd_setdevaddr
2178  *	set the device addrs on this port
2179  */
2180 static int
2181 hubd_setdevaddr(hubd_t *hubd, usb_port_t port)
2182 {
2183 	int		rval;
2184 	usb_cr_t	completion_reason;
2185 	usb_cb_flags_t	cb_flags;
2186 	usb_pipe_handle_t ph;
2187 	dev_info_t	*child_dip = NULL;
2188 	uchar_t		address = 0;
2189 	usba_device_t	*usba_device;
2190 	int		retry = 0;
2191 	long		time_delay;
2192 
2193 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2194 	    "hubd_setdevaddr: port=%d", port);
2195 
2196 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
2197 
2198 	child_dip = hubd->h_children_dips[port];
2199 	address = hubd->h_usba_devices[port]->usb_addr;
2200 	usba_device = hubd->h_usba_devices[port];
2201 
2202 	/* close the default pipe with addr x */
2203 	mutex_exit(HUBD_MUTEX(hubd));
2204 	ph = usba_get_dflt_pipe_handle(child_dip);
2205 	usb_pipe_close(child_dip, ph,
2206 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2207 	mutex_enter(HUBD_MUTEX(hubd));
2208 
2209 	/*
2210 	 * If the host controller is in charge of addressing, have it do that
2211 	 * now and skip everything else.
2212 	 */
2213 	if (usba_device->usb_hcdi_ops->usba_hcdi_device_address != NULL) {
2214 		mutex_exit(HUBD_MUTEX(hubd));
2215 		rval = usba_device->usb_hcdi_ops->usba_hcdi_device_address(
2216 		    usba_device);
2217 		mutex_enter(HUBD_MUTEX(hubd));
2218 
2219 		usba_clear_data_toggle(usba_device);
2220 		return (rval);
2221 	}
2222 
2223 	/*
2224 	 * As this device has been reset, temporarily
2225 	 * assign the default address
2226 	 */
2227 	mutex_enter(&usba_device->usb_mutex);
2228 	address = usba_device->usb_addr;
2229 	usba_device->usb_addr = USBA_DEFAULT_ADDR;
2230 	mutex_exit(&usba_device->usb_mutex);
2231 
2232 	mutex_exit(HUBD_MUTEX(hubd));
2233 
2234 	time_delay = drv_usectohz(hubd_device_delay / 20);
2235 	for (retry = 0; retry < hubd_retry_enumerate; retry++) {
2236 
2237 		/* open child's default pipe with USBA_DEFAULT_ADDR */
2238 		if (usb_pipe_open(child_dip, NULL, NULL,
2239 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) !=
2240 		    USB_SUCCESS) {
2241 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2242 			    "hubd_setdevaddr: Unable to open default pipe");
2243 
2244 			break;
2245 		}
2246 
2247 		/* Set the address of the device */
2248 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
2249 		    USB_DEV_REQ_HOST_TO_DEV,
2250 		    USB_REQ_SET_ADDRESS,	/* bRequest */
2251 		    address,			/* wValue */
2252 		    0,				/* wIndex */
2253 		    0,				/* wLength */
2254 		    NULL, 0,
2255 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
2256 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2257 			    "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x",
2258 			    retry, rval, completion_reason, cb_flags);
2259 		}
2260 
2261 		usb_pipe_close(child_dip, ph,
2262 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2263 
2264 		if (rval == USB_SUCCESS) {
2265 
2266 			break;
2267 		}
2268 
2269 		delay(time_delay);
2270 	}
2271 
2272 	/* Reset to the old address */
2273 	mutex_enter(&usba_device->usb_mutex);
2274 	usba_device->usb_addr = address;
2275 	mutex_exit(&usba_device->usb_mutex);
2276 	mutex_enter(HUBD_MUTEX(hubd));
2277 
2278 	usba_clear_data_toggle(usba_device);
2279 
2280 	return (rval);
2281 }
2282 
2283 
2284 /*
2285  * hubd_setdevconfig
2286  *	set the device addrs on this port
2287  */
2288 static void
2289 hubd_setdevconfig(hubd_t *hubd, usb_port_t port)
2290 {
2291 	int			rval;
2292 	usb_cr_t		completion_reason;
2293 	usb_cb_flags_t		cb_flags;
2294 	usb_pipe_handle_t	ph;
2295 	dev_info_t		*child_dip = NULL;
2296 	usba_device_t		*usba_device = NULL;
2297 	uint16_t		config_value;
2298 
2299 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2300 	    "hubd_setdevconfig: port=%d", port);
2301 
2302 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
2303 
2304 	child_dip = hubd->h_children_dips[port];
2305 	usba_device = hubd->h_usba_devices[port];
2306 	config_value = hubd->h_usba_devices[port]->usb_cfg_value;
2307 	mutex_exit(HUBD_MUTEX(hubd));
2308 
2309 	/* open the default control pipe */
2310 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
2311 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) ==
2312 	    USB_SUCCESS) {
2313 
2314 		/* Set the default configuration of the device */
2315 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
2316 		    USB_DEV_REQ_HOST_TO_DEV,
2317 		    USB_REQ_SET_CFG,		/* bRequest */
2318 		    config_value,		/* wValue */
2319 		    0,				/* wIndex */
2320 		    0,				/* wLength */
2321 		    NULL, 0,
2322 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
2323 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2324 			    "hubd_setdevconfig: set device config failed: "
2325 			    "cr=%d cb_fl=0x%x rval=%d",
2326 			    completion_reason, cb_flags, rval);
2327 		}
2328 		/*
2329 		 * After setting the configuration, we make this default
2330 		 * control pipe persistent, so that it gets re-opened
2331 		 * on posting a connect event
2332 		 */
2333 		usba_persistent_pipe_close(usba_device);
2334 	} else {
2335 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2336 		    "pipe open fails: rval=%d", rval);
2337 	}
2338 	mutex_enter(HUBD_MUTEX(hubd));
2339 }
2340 
2341 
2342 /*ARGSUSED*/
2343 static int
2344 hubd_check_disconnected_ports(dev_info_t *dip, void *arg)
2345 {
2346 	int circ;
2347 	usb_port_t port;
2348 	hubd_t *hubd;
2349 	major_t hub_major = ddi_name_to_major("hubd");
2350 	major_t hwahc_major = ddi_name_to_major("hwahc");
2351 	major_t usbmid_major = ddi_name_to_major("usb_mid");
2352 
2353 	/*
2354 	 * make sure dip is a usb hub, major of root hub is HCD
2355 	 * major
2356 	 */
2357 	if (!usba_is_root_hub(dip)) {
2358 		if (ddi_driver_major(dip) == usbmid_major) {
2359 			/*
2360 			 * need to walk the children since it might be a
2361 			 * HWA device
2362 			 */
2363 
2364 			return (DDI_WALK_CONTINUE);
2365 		}
2366 
2367 		/* TODO: DWA device may also need special handling */
2368 
2369 		if (((ddi_driver_major(dip) != hub_major) &&
2370 		    (ddi_driver_major(dip) != hwahc_major)) ||
2371 		    !i_ddi_devi_attached(dip)) {
2372 
2373 			return (DDI_WALK_PRUNECHILD);
2374 		}
2375 	}
2376 
2377 	hubd = hubd_get_soft_state(dip);
2378 	if (hubd == NULL) {
2379 
2380 		return (DDI_WALK_PRUNECHILD);
2381 	}
2382 
2383 	/* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */
2384 	ndi_devi_enter(dip, &circ);
2385 
2386 	if (ddi_driver_major(dip) != hwahc_major) {
2387 		/* for normal usb hub or root hub */
2388 		mutex_enter(HUBD_MUTEX(hubd));
2389 		for (port = 1; port <= hubd->h_nports; port++) {
2390 			dev_info_t *cdip = hubd->h_children_dips[port];
2391 
2392 			if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) {
2393 
2394 				continue;
2395 			}
2396 
2397 			(void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE,
2398 			    B_TRUE);
2399 		}
2400 		mutex_exit(HUBD_MUTEX(hubd));
2401 	} else {
2402 		/* for HWA */
2403 		if (hubd->h_cleanup_child != NULL) {
2404 			if (hubd->h_cleanup_child(dip) != USB_SUCCESS) {
2405 				ndi_devi_exit(dip, circ);
2406 
2407 				return (DDI_WALK_PRUNECHILD);
2408 			}
2409 		} else {
2410 			ndi_devi_exit(dip, circ);
2411 
2412 			return (DDI_WALK_PRUNECHILD);
2413 		}
2414 	}
2415 
2416 	ndi_devi_exit(dip, circ);
2417 
2418 	/* skip siblings of root hub */
2419 	if (usba_is_root_hub(dip)) {
2420 
2421 		return (DDI_WALK_PRUNESIB);
2422 	}
2423 
2424 	return (DDI_WALK_CONTINUE);
2425 }
2426 
2427 
2428 /*
2429  * this thread will walk all children under the root hub for this
2430  * USB bus instance and attempt to remove them
2431  */
2432 static void
2433 hubd_root_hub_cleanup_thread(void *arg)
2434 {
2435 	int circ;
2436 	hubd_t *root_hubd = (hubd_t *)arg;
2437 	dev_info_t *rh_dip = root_hubd->h_dip;
2438 #ifndef __lock_lint
2439 	callb_cpr_t cprinfo;
2440 
2441 	CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr,
2442 	    "USB root hub");
2443 #endif
2444 
2445 	for (;;) {
2446 		/* don't race with detach */
2447 		ndi_hold_devi(rh_dip);
2448 
2449 		mutex_enter(HUBD_MUTEX(root_hubd));
2450 		root_hubd->h_cleanup_needed = 0;
2451 		mutex_exit(HUBD_MUTEX(root_hubd));
2452 
2453 		(void) devfs_clean(rh_dip, NULL, 0);
2454 
2455 		ndi_devi_enter(ddi_get_parent(rh_dip), &circ);
2456 		ddi_walk_devs(rh_dip, hubd_check_disconnected_ports,
2457 		    NULL);
2458 #ifdef __lock_lint
2459 		(void) hubd_check_disconnected_ports(rh_dip, NULL);
2460 #endif
2461 		ndi_devi_exit(ddi_get_parent(rh_dip), circ);
2462 
2463 		/* quit if we are not enabled anymore */
2464 		mutex_enter(HUBD_MUTEX(root_hubd));
2465 		if ((root_hubd->h_cleanup_enabled == B_FALSE) ||
2466 		    (root_hubd->h_cleanup_needed == B_FALSE)) {
2467 			root_hubd->h_cleanup_active = B_FALSE;
2468 			mutex_exit(HUBD_MUTEX(root_hubd));
2469 			ndi_rele_devi(rh_dip);
2470 
2471 			break;
2472 		}
2473 		mutex_exit(HUBD_MUTEX(root_hubd));
2474 		ndi_rele_devi(rh_dip);
2475 
2476 #ifndef __lock_lint
2477 		mutex_enter(HUBD_MUTEX(root_hubd));
2478 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2479 		mutex_exit(HUBD_MUTEX(root_hubd));
2480 
2481 		delay(drv_usectohz(hubd_dip_cleanup_delay));
2482 
2483 		mutex_enter(HUBD_MUTEX(root_hubd));
2484 		CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd));
2485 		mutex_exit(HUBD_MUTEX(root_hubd));
2486 #endif
2487 	}
2488 
2489 #ifndef __lock_lint
2490 	mutex_enter(HUBD_MUTEX(root_hubd));
2491 	CALLB_CPR_EXIT(&cprinfo);
2492 #endif
2493 }
2494 
2495 
2496 void
2497 hubd_schedule_cleanup(dev_info_t *rh_dip)
2498 {
2499 	hubd_t	*root_hubd;
2500 
2501 	/*
2502 	 * The usb_root_hub_dip pointer for the child hub of the WUSB
2503 	 * wire adapter class device points to the wire adapter, not
2504 	 * the root hub. Need to find the real root hub dip so that
2505 	 * the cleanup thread only starts from the root hub.
2506 	 */
2507 	while (!usba_is_root_hub(rh_dip)) {
2508 		root_hubd = hubd_get_soft_state(rh_dip);
2509 		if (root_hubd != NULL) {
2510 			rh_dip = root_hubd->h_usba_device->usb_root_hub_dip;
2511 			if (rh_dip == NULL) {
2512 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2513 				    root_hubd->h_log_handle,
2514 				    "hubd_schedule_cleanup: null rh dip");
2515 
2516 				return;
2517 			}
2518 		} else {
2519 			USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2520 			    root_hubd->h_log_handle,
2521 			    "hubd_schedule_cleanup: cannot find root hub");
2522 
2523 			return;
2524 		}
2525 	}
2526 	root_hubd = hubd_get_soft_state(rh_dip);
2527 
2528 	mutex_enter(HUBD_MUTEX(root_hubd));
2529 	root_hubd->h_cleanup_needed = B_TRUE;
2530 	if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) {
2531 		root_hubd->h_cleanup_active = B_TRUE;
2532 		mutex_exit(HUBD_MUTEX(root_hubd));
2533 		(void) thread_create(NULL, 0,
2534 		    hubd_root_hub_cleanup_thread,
2535 		    (void *)root_hubd, 0, &p0, TS_RUN,
2536 		    minclsyspri);
2537 	} else {
2538 		mutex_exit(HUBD_MUTEX(root_hubd));
2539 	}
2540 }
2541 
2542 
2543 /*
2544  * hubd_restore_device_state:
2545  *	- set config for the hub
2546  *	- power cycle all the ports
2547  *	- for each port that was connected
2548  *		- reset port
2549  *		- assign addrs to the device on this port
2550  *	- restart polling
2551  *	- reset suspend flag
2552  */
2553 static void
2554 hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd)
2555 {
2556 	int		rval;
2557 	int		retry;
2558 	uint_t		hub_prev_state;
2559 	usb_port_t	port;
2560 	uint16_t	status;
2561 	uint16_t	change;
2562 	dev_info_t	*ch_dip;
2563 	boolean_t	ehci_root_hub;
2564 
2565 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2566 	    "hubd_restore_device_state:");
2567 
2568 	mutex_enter(HUBD_MUTEX(hubd));
2569 	hub_prev_state = hubd->h_dev_state;
2570 	ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN);
2571 
2572 	/* First bring the device to full power */
2573 	(void) hubd_pm_busy_component(hubd, dip, 0);
2574 	mutex_exit(HUBD_MUTEX(hubd));
2575 
2576 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
2577 
2578 	if (!usba_is_root_hub(dip) &&
2579 	    (usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0,
2580 	    DPRINT_MASK_HOTPLUG,
2581 	    USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) {
2582 
2583 		/* change the device state to disconnected */
2584 		mutex_enter(HUBD_MUTEX(hubd));
2585 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
2586 		(void) hubd_pm_idle_component(hubd, dip, 0);
2587 		mutex_exit(HUBD_MUTEX(hubd));
2588 
2589 		return;
2590 	}
2591 
2592 	ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0);
2593 
2594 	mutex_enter(HUBD_MUTEX(hubd));
2595 	/* First turn off all port power */
2596 	rval = hubd_disable_all_port_power(hubd);
2597 	if (rval != USB_SUCCESS) {
2598 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
2599 		    "hubd_restore_device_state:"
2600 		    "turning off port power failed");
2601 	}
2602 
2603 	/* Settling time before turning on again */
2604 	mutex_exit(HUBD_MUTEX(hubd));
2605 	delay(drv_usectohz(hubd_device_delay / 100));
2606 	mutex_enter(HUBD_MUTEX(hubd));
2607 
2608 	/* enable power on all ports so we can see connects */
2609 	if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) {
2610 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2611 		    "hubd_restore_device_state: turn on port power failed");
2612 
2613 		/* disable whatever was enabled */
2614 		(void) hubd_disable_all_port_power(hubd);
2615 
2616 		(void) hubd_pm_idle_component(hubd, dip, 0);
2617 		mutex_exit(HUBD_MUTEX(hubd));
2618 
2619 		return;
2620 	}
2621 
2622 	/*
2623 	 * wait at least 3 frames before accessing devices
2624 	 * (note that delay's minimal time is one clock tick which
2625 	 * is 10ms unless hires_tick has been changed)
2626 	 */
2627 	mutex_exit(HUBD_MUTEX(hubd));
2628 	delay(drv_usectohz(10000));
2629 	mutex_enter(HUBD_MUTEX(hubd));
2630 
2631 	hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER;
2632 
2633 	for (port = 1; port <= hubd->h_nports; port++) {
2634 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
2635 		    "hubd_restore_device_state: port=%d", port);
2636 
2637 		/*
2638 		 * the childen_dips list may have dips that have been
2639 		 * already deallocated. we only get a post_detach notification
2640 		 * but not a destroy notification
2641 		 */
2642 		ch_dip = hubd->h_children_dips[port];
2643 		if (ch_dip) {
2644 			/* get port status */
2645 			(void) hubd_determine_port_status(hubd, port,
2646 			    &status, &change, NULL, PORT_CHANGE_CSC);
2647 
2648 			/* check if it is truly connected */
2649 			if (status & PORT_STATUS_CCS) {
2650 				/*
2651 				 * Now reset port and assign the device
2652 				 * its original address
2653 				 */
2654 				retry = 0;
2655 				do {
2656 					(void) hubd_reset_port(hubd, port);
2657 
2658 					/* required for ppx */
2659 					(void) hubd_enable_port(hubd, port);
2660 
2661 					if (retry) {
2662 						mutex_exit(HUBD_MUTEX(hubd));
2663 						delay(drv_usectohz(
2664 						    hubd_device_delay/2));
2665 						mutex_enter(HUBD_MUTEX(hubd));
2666 					}
2667 
2668 					rval = hubd_setdevaddr(hubd, port);
2669 					retry++;
2670 				} while ((rval != USB_SUCCESS) &&
2671 				    (retry < hubd_retry_enumerate));
2672 
2673 				hubd_setdevconfig(hubd, port);
2674 
2675 				if (hub_prev_state == USB_DEV_DISCONNECTED) {
2676 					/* post a connect event */
2677 					mutex_exit(HUBD_MUTEX(hubd));
2678 					hubd_post_event(hubd, port,
2679 					    USBA_EVENT_TAG_HOT_INSERTION);
2680 					mutex_enter(HUBD_MUTEX(hubd));
2681 				} else {
2682 					/*
2683 					 * Since we have this device connected
2684 					 * mark it reinserted to prevent
2685 					 * cleanup thread from stepping in.
2686 					 */
2687 					mutex_exit(HUBD_MUTEX(hubd));
2688 					mutex_enter(&(DEVI(ch_dip)->devi_lock));
2689 					DEVI_SET_DEVICE_REINSERTED(ch_dip);
2690 					mutex_exit(&(DEVI(ch_dip)->devi_lock));
2691 
2692 					/*
2693 					 * reopen pipes for children for
2694 					 * their DDI_RESUME
2695 					 */
2696 					rval = usba_persistent_pipe_open(
2697 					    usba_get_usba_device(ch_dip));
2698 					mutex_enter(HUBD_MUTEX(hubd));
2699 					ASSERT(rval == USB_SUCCESS);
2700 				}
2701 			} else {
2702 				/*
2703 				 * Mark this dip for deletion as the device
2704 				 * is not physically present, and schedule
2705 				 * cleanup thread upon post resume
2706 				 */
2707 				mutex_exit(HUBD_MUTEX(hubd));
2708 
2709 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2710 				    hubd->h_log_handle,
2711 				    "hubd_restore_device_state: "
2712 				    "dip=%p on port=%d marked for cleanup",
2713 				    (void *)ch_dip, port);
2714 				mutex_enter(&(DEVI(ch_dip)->devi_lock));
2715 				DEVI_SET_DEVICE_REMOVED(ch_dip);
2716 				mutex_exit(&(DEVI(ch_dip)->devi_lock));
2717 
2718 				mutex_enter(HUBD_MUTEX(hubd));
2719 			}
2720 		} else if (ehci_root_hub) {
2721 			/* get port status */
2722 			(void) hubd_determine_port_status(hubd, port,
2723 			    &status, &change, NULL, PORT_CHANGE_CSC);
2724 
2725 			/* check if it is truly connected */
2726 			if (status & PORT_STATUS_CCS) {
2727 				/*
2728 				 * reset the port to find out if we have
2729 				 * 2.0 device connected or 1.X. A 2.0
2730 				 * device will still be seen as connected,
2731 				 * while a 1.X device will switch over to
2732 				 * the companion controller.
2733 				 */
2734 				(void) hubd_reset_port(hubd, port);
2735 
2736 				(void) hubd_determine_port_status(hubd, port,
2737 				    &status, &change, NULL, PORT_CHANGE_CSC);
2738 
2739 				if (status &
2740 				    (PORT_STATUS_CCS | PORT_STATUS_HSDA)) {
2741 					/*
2742 					 * We have a USB 2.0 device
2743 					 * connected. Power cycle this port
2744 					 * so that hotplug thread can
2745 					 * enumerate this device.
2746 					 */
2747 					(void) hubd_toggle_port(hubd, port);
2748 				} else {
2749 					USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2750 					    hubd->h_log_handle,
2751 					    "hubd_restore_device_state: "
2752 					    "device on port %d switched over",
2753 					    port);
2754 				}
2755 			}
2756 
2757 		}
2758 	}
2759 
2760 
2761 	/* if the device had remote wakeup earlier, enable it again */
2762 	if (hubd->h_hubpm->hubp_wakeup_enabled) {
2763 		mutex_exit(HUBD_MUTEX(hubd));
2764 		(void) usb_handle_remote_wakeup(hubd->h_dip,
2765 		    USB_REMOTE_WAKEUP_ENABLE);
2766 		mutex_enter(HUBD_MUTEX(hubd));
2767 	}
2768 
2769 	hubd->h_dev_state = USB_DEV_ONLINE;
2770 	hubd_start_polling(hubd, 0);
2771 	(void) hubd_pm_idle_component(hubd, dip, 0);
2772 	mutex_exit(HUBD_MUTEX(hubd));
2773 }
2774 
2775 
2776 /*
2777  * hubd_cleanup:
2778  *	cleanup hubd and deallocate. this function is called for
2779  *	handling attach failures and detaching including dynamic
2780  *	reconfiguration. If called from attaching, it must clean
2781  *	up the whole thing and return success.
2782  */
2783 /*ARGSUSED*/
2784 static int
2785 hubd_cleanup(dev_info_t *dip, hubd_t *hubd)
2786 {
2787 	int		circ, rval, old_dev_state;
2788 	hub_power_t	*hubpm;
2789 #ifdef DEBUG
2790 	usb_port_t	port;
2791 #endif
2792 
2793 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2794 	    "hubd_cleanup:");
2795 
2796 	if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) {
2797 		goto done;
2798 	}
2799 
2800 	/* ensure we are the only one active */
2801 	ndi_devi_enter(dip, &circ);
2802 
2803 	mutex_enter(HUBD_MUTEX(hubd));
2804 
2805 	/* Cleanup failure is only allowed if called from detach */
2806 	if (DEVI_IS_DETACHING(dip)) {
2807 		dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
2808 
2809 		/*
2810 		 * We are being called from detach.
2811 		 * Fail immediately if the hotplug thread is running
2812 		 * else set the dev_state to disconnected so that
2813 		 * hotplug thread just exits without doing anything.
2814 		 */
2815 		if (hubd->h_bus_ctls || hubd->h_bus_pwr ||
2816 		    hubd->h_hotplug_thread) {
2817 			mutex_exit(HUBD_MUTEX(hubd));
2818 			ndi_devi_exit(dip, circ);
2819 
2820 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2821 			    "hubd_cleanup: hotplug thread/bus ctl active "
2822 			    "- failing detach");
2823 
2824 			return (USB_FAILURE);
2825 		}
2826 
2827 		/*
2828 		 * if the deathrow thread is still active or about
2829 		 * to become active, fail detach
2830 		 * the roothup can only be detached if nexus drivers
2831 		 * are unloaded or explicitly offlined
2832 		 */
2833 		if (rh_dip == dip) {
2834 			if (hubd->h_cleanup_needed ||
2835 			    hubd->h_cleanup_active) {
2836 				mutex_exit(HUBD_MUTEX(hubd));
2837 				ndi_devi_exit(dip, circ);
2838 
2839 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2840 				    hubd->h_log_handle,
2841 				    "hubd_cleanup: deathrow still active?"
2842 				    "- failing detach");
2843 
2844 				return (USB_FAILURE);
2845 			}
2846 		}
2847 	}
2848 
2849 	old_dev_state = hubd->h_dev_state;
2850 	hubd->h_dev_state = USB_DEV_DISCONNECTED;
2851 
2852 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2853 	    "hubd_cleanup: stop polling");
2854 	hubd_close_intr_pipe(hubd);
2855 
2856 	ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr ||
2857 	    hubd->h_hotplug_thread) == 0);
2858 	mutex_exit(HUBD_MUTEX(hubd));
2859 
2860 	/*
2861 	 * deallocate events, if events are still registered
2862 	 * (ie. children still attached) then we have to fail the detach
2863 	 */
2864 	if (hubd->h_ndi_event_hdl) {
2865 
2866 		rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl);
2867 		if (DEVI_IS_ATTACHING(dip)) {
2868 
2869 			/* It must return success if attaching. */
2870 			ASSERT(rval == NDI_SUCCESS);
2871 
2872 		} else if (rval != NDI_SUCCESS) {
2873 
2874 			USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle,
2875 			    "hubd_cleanup: ndi_event_free_hdl failed");
2876 			ndi_devi_exit(dip, circ);
2877 
2878 			return (USB_FAILURE);
2879 
2880 		}
2881 	}
2882 
2883 	mutex_enter(HUBD_MUTEX(hubd));
2884 
2885 	if (hubd->h_init_state & HUBD_CHILDREN_CREATED) {
2886 #ifdef DEBUG
2887 		for (port = 1; port <= hubd->h_nports; port++) {
2888 			ASSERT(hubd->h_usba_devices[port] == NULL);
2889 			ASSERT(hubd->h_children_dips[port] == NULL);
2890 		}
2891 #endif
2892 		kmem_free(hubd->h_children_dips, hubd->h_cd_list_length);
2893 		kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length);
2894 	}
2895 
2896 	/*
2897 	 * Disable the event callbacks first, after this point, event
2898 	 * callbacks will never get called. Note we shouldn't hold
2899 	 * mutex while unregistering events because there may be a
2900 	 * competing event callback thread. Event callbacks are done
2901 	 * with ndi mutex held and this can cause a potential deadlock.
2902 	 * Note that cleanup can't fail after deregistration of events.
2903 	 */
2904 	if (hubd->h_init_state &  HUBD_EVENTS_REGISTERED) {
2905 		mutex_exit(HUBD_MUTEX(hubd));
2906 		usb_unregister_event_cbs(dip, &hubd_events);
2907 		hubd_unregister_cpr_callback(hubd);
2908 		mutex_enter(HUBD_MUTEX(hubd));
2909 	}
2910 
2911 	/* restore the old dev state so that device can be put into low power */
2912 	hubd->h_dev_state = old_dev_state;
2913 	hubpm = hubd->h_hubpm;
2914 
2915 	if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) {
2916 		(void) hubd_pm_busy_component(hubd, dip, 0);
2917 		mutex_exit(HUBD_MUTEX(hubd));
2918 		if (hubd->h_hubpm->hubp_wakeup_enabled) {
2919 			/*
2920 			 * Bring the hub to full power before
2921 			 * issuing the disable remote wakeup command
2922 			 */
2923 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
2924 
2925 			if ((rval = usb_handle_remote_wakeup(hubd->h_dip,
2926 			    USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) {
2927 				USB_DPRINTF_L2(DPRINT_MASK_PM,
2928 				    hubd->h_log_handle,
2929 				    "hubd_cleanup: disable remote wakeup "
2930 				    "fails=%d", rval);
2931 			}
2932 		}
2933 
2934 		(void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF);
2935 
2936 		mutex_enter(HUBD_MUTEX(hubd));
2937 		(void) hubd_pm_idle_component(hubd, dip, 0);
2938 	}
2939 
2940 	if (hubpm) {
2941 		if (hubpm->hubp_child_pwrstate) {
2942 			kmem_free(hubpm->hubp_child_pwrstate,
2943 			    MAX_PORTS + 1);
2944 		}
2945 		kmem_free(hubpm, sizeof (hub_power_t));
2946 	}
2947 	mutex_exit(HUBD_MUTEX(hubd));
2948 
2949 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2950 	    "hubd_cleanup: freeing space");
2951 
2952 	if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) {
2953 		rval = usba_hubdi_unregister(dip);
2954 		ASSERT(rval == USB_SUCCESS);
2955 	}
2956 
2957 	if (hubd->h_init_state & HUBD_LOCKS_DONE) {
2958 		mutex_destroy(HUBD_MUTEX(hubd));
2959 		cv_destroy(&hubd->h_cv_reset_port);
2960 		cv_destroy(&hubd->h_cv_hotplug_dev);
2961 	}
2962 
2963 	ndi_devi_exit(dip, circ);
2964 
2965 	if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) {
2966 		ddi_remove_minor_node(dip, NULL);
2967 	}
2968 
2969 	if (usba_is_root_hub(dip)) {
2970 		usb_pipe_close(dip, hubd->h_default_pipe,
2971 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
2972 	}
2973 
2974 done:
2975 	if (hubd->h_ancestry_str) {
2976 		kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN);
2977 	}
2978 
2979 	usb_client_detach(dip, hubd->h_dev_data);
2980 
2981 	usb_free_log_hdl(hubd->h_log_handle);
2982 
2983 	if (!usba_is_root_hub(dip)) {
2984 		ddi_soft_state_free(hubd_statep, ddi_get_instance(dip));
2985 	}
2986 
2987 	ddi_prop_remove_all(dip);
2988 
2989 	return (USB_SUCCESS);
2990 }
2991 
2992 
2993 /*
2994  * hubd_determine_port_connection:
2995  *	Determine which port is in connect status but does not
2996  *	have connect status change bit set, and mark port change
2997  *	bit accordingly.
2998  *	This function is applied during hub attach time.
2999  */
3000 static usb_port_mask_t
3001 hubd_determine_port_connection(hubd_t	*hubd)
3002 {
3003 	usb_port_t	port;
3004 	uint16_t	status;
3005 	uint16_t	change;
3006 	usb_port_mask_t	port_change = 0;
3007 
3008 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3009 
3010 	for (port = 1; port <= hubd->h_nports; port++) {
3011 
3012 		(void) hubd_determine_port_status(hubd, port, &status,
3013 		    &change, NULL, 0);
3014 
3015 		/* Check if port is in connect status */
3016 		if (!(status & PORT_STATUS_CCS)) {
3017 
3018 			continue;
3019 		}
3020 
3021 		/*
3022 		 * Check if port Connect Status Change bit has been set.
3023 		 * If already set, the connection will be handled by
3024 		 * intr polling callback, not during attach.
3025 		 */
3026 		if (change & PORT_CHANGE_CSC) {
3027 
3028 			continue;
3029 		}
3030 
3031 		port_change |= 1 << port;
3032 	}
3033 
3034 	return (port_change);
3035 }
3036 
3037 
3038 /*
3039  * hubd_check_ports:
3040  *	- get hub descriptor
3041  *	- check initial port status
3042  *	- enable power on all ports
3043  *	- enable polling on ep1
3044  */
3045 static int
3046 hubd_check_ports(hubd_t  *hubd)
3047 {
3048 	int			rval;
3049 	usb_port_mask_t		port_change = 0;
3050 	hubd_hotplug_arg_t	*arg;
3051 
3052 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3053 
3054 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
3055 	    "hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip));
3056 
3057 	/*
3058 	 * First turn off all port power
3059 	 */
3060 	if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) {
3061 
3062 		/* disable whatever was enabled */
3063 		(void) hubd_disable_all_port_power(hubd);
3064 
3065 		return (rval);
3066 	}
3067 
3068 	/*
3069 	 * do not switch on immediately (instantly on root hub)
3070 	 * and allow time to settle
3071 	 */
3072 	mutex_exit(HUBD_MUTEX(hubd));
3073 	delay(drv_usectohz(10000));
3074 	mutex_enter(HUBD_MUTEX(hubd));
3075 
3076 	/*
3077 	 * enable power on all ports so we can see connects
3078 	 */
3079 	if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) {
3080 		/* disable whatever was enabled */
3081 		(void) hubd_disable_all_port_power(hubd);
3082 
3083 		return (rval);
3084 	}
3085 
3086 	/* wait at least 3 frames before accessing devices */
3087 	mutex_exit(HUBD_MUTEX(hubd));
3088 	delay(drv_usectohz(10000));
3089 	mutex_enter(HUBD_MUTEX(hubd));
3090 
3091 	/*
3092 	 * allocate arrays for saving the dips of each child per port
3093 	 *
3094 	 * ports go from 1 - n, allocate 1 more entry
3095 	 */
3096 	hubd->h_cd_list_length =
3097 	    (sizeof (dev_info_t **)) * (hubd->h_nports + 1);
3098 
3099 	hubd->h_children_dips = (dev_info_t **)kmem_zalloc(
3100 	    hubd->h_cd_list_length, KM_SLEEP);
3101 	hubd->h_usba_devices = (usba_device_t **)kmem_zalloc(
3102 	    hubd->h_cd_list_length, KM_SLEEP);
3103 
3104 	hubd->h_init_state |= HUBD_CHILDREN_CREATED;
3105 
3106 	mutex_exit(HUBD_MUTEX(hubd));
3107 	arg = (hubd_hotplug_arg_t *)kmem_zalloc(
3108 	    sizeof (hubd_hotplug_arg_t), KM_SLEEP);
3109 	mutex_enter(HUBD_MUTEX(hubd));
3110 
3111 	if ((rval = hubd_open_intr_pipe(hubd)) != USB_SUCCESS) {
3112 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3113 
3114 		return (rval);
3115 	}
3116 
3117 	hubd_start_polling(hubd, 0);
3118 
3119 	/*
3120 	 * Some hub devices, like the embedded hub in the CKS ErgoMagic
3121 	 * keyboard, may only have connection status bit set, but not
3122 	 * have connect status change bit set when a device has been
3123 	 * connected to its downstream port before the hub is enumerated.
3124 	 * Then when the hub is in enumeration, the devices connected to
3125 	 * it cannot be detected by the intr pipe and won't be enumerated.
3126 	 * We need to check such situation here and enumerate the downstream
3127 	 * devices for such hubs.
3128 	 */
3129 	port_change = hubd_determine_port_connection(hubd);
3130 
3131 	if (port_change != 0 || hubd->h_port_change != 0) {
3132 		hubd_pm_busy_component(hubd, hubd->h_dip, 0);
3133 
3134 		arg->hubd = hubd;
3135 		arg->hotplug_during_attach = B_TRUE;
3136 		hubd->h_port_change |= port_change;
3137 
3138 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
3139 		    "hubd_check_ports: port change=0x%x, need to connect",
3140 		    hubd->h_port_change);
3141 
3142 		if (usb_async_req(hubd->h_dip, hubd_hotplug_thread,
3143 		    (void *)arg, 0) == USB_SUCCESS) {
3144 			hubd->h_hotplug_thread++;
3145 		} else {
3146 			/* mark this device as idle */
3147 			hubd_pm_idle_component(hubd, hubd->h_dip, 0);
3148 			kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3149 		}
3150 	} else {
3151 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
3152 	}
3153 
3154 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
3155 	    "hubd_check_ports done");
3156 
3157 	return (USB_SUCCESS);
3158 }
3159 
3160 
3161 /*
3162  * hubd_get_hub_descriptor:
3163  */
3164 static int
3165 hubd_get_hub_descriptor(hubd_t *hubd)
3166 {
3167 	mblk_t		*data = NULL;
3168 	usb_cr_t	completion_reason;
3169 	usb_cb_flags_t	cb_flags;
3170 	uint16_t	length, wValue;
3171 	int		rval;
3172 	usb_req_attrs_t attr = 0;
3173 
3174 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3175 	    "hubd_get_hub_descriptor:");
3176 
3177 	if ((hubd->h_dev_data->dev_descr->idVendor == USB_HUB_INTEL_VID) &&
3178 	    (hubd->h_dev_data->dev_descr->idProduct == USB_HUB_INTEL_PID)) {
3179 		attr = USB_ATTRS_SHORT_XFER_OK;
3180 	}
3181 
3182 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3183 	ASSERT(hubd->h_default_pipe != 0);
3184 
3185 	mutex_exit(HUBD_MUTEX(hubd));
3186 
3187 	/*
3188 	 * The contents of wValue change depending on whether this is a USB 2 or
3189 	 * USB 3 device. SuperSpeed Hubs have different descriptors and you
3190 	 * cannot ask them for the traditional USB 2 descriptor.
3191 	 */
3192 	if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
3193 		wValue = USB_DESCR_TYPE_SS_HUB << 8 | HUBD_DEFAULT_DESC_INDEX;
3194 	} else {
3195 		wValue = USB_DESCR_TYPE_HUB << 8 | HUBD_DEFAULT_DESC_INDEX;
3196 	}
3197 
3198 	/*
3199 	 * The hub descriptor length varies in various versions of USB. For
3200 	 * example, in USB 2 it's at least 9 bytes long. To start with, we
3201 	 * always get the first 8 bytes so we can figure out how long it
3202 	 * actually is.
3203 	 */
3204 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3205 	    hubd->h_default_pipe,
3206 	    HUB_CLASS_REQ_TYPE,
3207 	    USB_REQ_GET_DESCR,		/* bRequest */
3208 	    wValue,			/* wValue */
3209 	    0,				/* wIndex */
3210 	    8,				/* wLength */
3211 	    &data, 0,
3212 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
3213 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3214 		    "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
3215 		    completion_reason, cb_flags, rval);
3216 		freemsg(data);
3217 		mutex_enter(HUBD_MUTEX(hubd));
3218 
3219 		return (rval);
3220 	}
3221 
3222 	length = *(data->b_rptr);
3223 
3224 	if (length > 8) {
3225 		freemsg(data);
3226 		data = NULL;
3227 
3228 		/* get complete hub descriptor */
3229 		rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3230 		    hubd->h_default_pipe,
3231 		    HUB_CLASS_REQ_TYPE,
3232 		    USB_REQ_GET_DESCR,		/* bRequest */
3233 		    wValue,			/* wValue */
3234 		    0,				/* wIndex */
3235 		    length,			/* wLength */
3236 		    &data, attr,
3237 		    &completion_reason, &cb_flags, 0);
3238 
3239 		/*
3240 		 * Hub descriptor data less than 9 bytes is not valid and
3241 		 * may cause trouble if we use it. See USB2.0 Tab11-13.
3242 		 */
3243 		if ((rval != USB_SUCCESS) || (MBLKL(data) <= 8)) {
3244 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3245 			    "get hub descriptor failed: "
3246 			    "cr=%d cb_fl=0x%x rval=%d, len=%ld",
3247 			    completion_reason, cb_flags, rval,
3248 			    (data)?MBLKL(data):0);
3249 			freemsg(data);
3250 			mutex_enter(HUBD_MUTEX(hubd));
3251 
3252 			return (rval);
3253 		}
3254 	}
3255 
3256 	mutex_enter(HUBD_MUTEX(hubd));
3257 
3258 	/*
3259 	 * Parse the hub descriptor. Note that the format of the descriptor
3260 	 * itself depends on the USB version. We handle the different ones and
3261 	 * transform it into a single uniform view.
3262 	 */
3263 
3264 	ASSERT(*(data->b_rptr + 2) <= (MAX_PORTS + 1));
3265 	if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
3266 		usb_ss_hub_descr_t hub_descr;
3267 		char *desc = "cccscccs";
3268 		ASSERT(*(data->b_rptr + 1) == ROOT_HUB_SS_DESCRIPTOR_TYPE);
3269 
3270 		/*
3271 		 * Note many hubs may support less than the 255 devices that the
3272 		 * USB specification allows for. In those cases, we'll simply
3273 		 * read less and it should be okay.
3274 		 */
3275 		if (usb_parse_CV_descr(desc, data->b_rptr, MBLKL(data),
3276 		    (void *)&hub_descr, sizeof (hub_descr)) == 0) {
3277 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3278 			    "parsing hub descriptor failed");
3279 			freemsg(data);
3280 			return (USB_FAILURE);
3281 		}
3282 
3283 		hubd->h_nports = hub_descr.bNbrPorts;
3284 		hubd->h_hub_chars = hub_descr.wHubCharacteristics;
3285 		hubd->h_power_good = hub_descr.bPwrOn2PwrGood;
3286 		hubd->h_current = hub_descr.bHubContrCurrent;
3287 	} else {
3288 		usb_hub_descr_t hub_descr;
3289 		if (usb_parse_CV_descr("cccscccccc",
3290 		    data->b_rptr, MBLKL(data),
3291 		    (void *)&hub_descr, sizeof (usb_hub_descr_t)) == 0) {
3292 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3293 			    "parsing hub descriptor failed");
3294 			freemsg(data);
3295 			return (USB_FAILURE);
3296 		}
3297 
3298 		hubd->h_nports = hub_descr.bNbrPorts;
3299 		hubd->h_hub_chars = hub_descr.wHubCharacteristics;
3300 		hubd->h_power_good = hub_descr.bPwrOn2PwrGood;
3301 		hubd->h_current = hub_descr.bHubContrCurrent;
3302 	}
3303 
3304 	freemsg(data);
3305 
3306 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
3307 	    "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
3308 	    "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval,
3309 	    hubd->h_nports, hubd->h_hub_chars,
3310 	    hubd->h_power_good, hubd->h_current);
3311 
3312 	if (hubd->h_nports > MAX_PORTS) {
3313 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
3314 		    "Hub driver supports max of %d ports on hub. "
3315 		    "Hence using the first %d port of %d ports available",
3316 		    MAX_PORTS, MAX_PORTS, hubd->h_nports);
3317 
3318 		hubd->h_nports = MAX_PORTS;
3319 	}
3320 
3321 	return (USB_SUCCESS);
3322 }
3323 
3324 static int
3325 hubd_set_hub_depth(hubd_t *hubd)
3326 {
3327 	int rval;
3328 	usb_cr_t	completion_reason;
3329 	usb_cb_flags_t	cb_flags;
3330 	usba_device_t 	*ud;
3331 	uint16_t 	depth;
3332 
3333 	/*
3334 	 * We only need to set the hub depth devices for hubs that are at least
3335 	 * SuperSpeed devices. This didn't exist for USB 2.0 and older hubs.
3336 	 * There's also no need to call this on the root hub.
3337 	 */
3338 	if (hubd->h_usba_device->usb_port_status < USBA_SUPER_SPEED_DEV ||
3339 	    usba_is_root_hub(hubd->h_dip))
3340 		return (USB_SUCCESS);
3341 
3342 	depth = 0;
3343 	ud = hubd->h_usba_device;
3344 	while (ud->usb_parent_hub != NULL) {
3345 		depth++;
3346 		ud = ud->usb_parent_hub;
3347 	}
3348 	ASSERT(depth > 0);
3349 
3350 	if (depth > HUBD_SS_MAX_DEPTH) {
3351 		const char *mfg, *prod;
3352 
3353 		ud = hubd->h_usba_device;
3354 		prod = ud->usb_product_str;
3355 		if (prod == NULL)
3356 			prod = "Unknown Device";
3357 		mfg = ud->usb_mfg_str;
3358 		if (mfg == NULL)
3359 			mfg = "Unknown Manufacturer";
3360 		cmn_err(CE_WARN, "Unable to attach USB 3.x hub %s %s. A "
3361 		    "maximum of %d hubs may be cascaded", mfg, prod,
3362 		    HUBD_SS_MAX_DEPTH);
3363 		return (USB_FAILURE);
3364 	}
3365 
3366 	/*
3367 	 * When making the HUB_REQ_SET_HUB_DEPTH request, a hub connected to a
3368 	 * root port is considered to have a hub depth of zero whereas we
3369 	 * consider having a hub depth of one above.
3370 	 */
3371 	depth--;
3372 
3373 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3374 	    hubd->h_default_pipe,
3375 	    HUB_SET_HUB_DEPTH_TYPE,
3376 	    HUB_REQ_SET_HUB_DEPTH,	/* bRequest */
3377 	    depth,			/* wValue */
3378 	    0,				/* wIndex */
3379 	    0,				/* wLength */
3380 	    NULL, 0,
3381 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
3382 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3383 		    "get set hub depth failed: cr=%d cb=0x%x",
3384 		    completion_reason, cb_flags);
3385 	}
3386 
3387 	return (rval);
3388 }
3389 
3390 /*
3391  * hubd_get_hub_status_words:
3392  */
3393 static int
3394 hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status)
3395 {
3396 	usb_cr_t	completion_reason;
3397 	usb_cb_flags_t	cb_flags;
3398 	mblk_t		*data = NULL;
3399 
3400 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
3401 
3402 	mutex_exit(HUBD_MUTEX(hubd));
3403 
3404 	if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe,
3405 	    HUB_CLASS_REQ_TYPE,
3406 	    USB_REQ_GET_STATUS,
3407 	    0,
3408 	    0,
3409 	    GET_STATUS_LENGTH,
3410 	    &data, 0,
3411 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
3412 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3413 		    "get hub status failed: cr=%d cb=0x%x",
3414 		    completion_reason, cb_flags);
3415 
3416 		if (data) {
3417 			freemsg(data);
3418 		}
3419 
3420 		mutex_enter(HUBD_MUTEX(hubd));
3421 
3422 		return (USB_FAILURE);
3423 	}
3424 
3425 	mutex_enter(HUBD_MUTEX(hubd));
3426 
3427 	status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
3428 	status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
3429 
3430 	USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
3431 	    "hub status=0x%x change=0x%x", status[0], status[1]);
3432 
3433 	freemsg(data);
3434 
3435 	return (USB_SUCCESS);
3436 }
3437 
3438 
3439 /*
3440  * hubd_open_intr_pipe:
3441  *	we read all descriptors first for curiosity and then simply
3442  *	open the pipe
3443  */
3444 static int
3445 hubd_open_intr_pipe(hubd_t	*hubd)
3446 {
3447 	int			rval;
3448 
3449 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3450 	    "hubd_open_intr_pipe:");
3451 
3452 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE);
3453 
3454 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING;
3455 	mutex_exit(HUBD_MUTEX(hubd));
3456 
3457 	if ((rval = usb_pipe_xopen(hubd->h_dip,
3458 	    &hubd->h_ep1_xdescr, &hubd->h_pipe_policy,
3459 	    0, &hubd->h_ep1_ph)) != USB_SUCCESS) {
3460 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3461 		    "open intr pipe failed (%d)", rval);
3462 
3463 		mutex_enter(HUBD_MUTEX(hubd));
3464 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
3465 
3466 		return (rval);
3467 	}
3468 
3469 	mutex_enter(HUBD_MUTEX(hubd));
3470 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
3471 
3472 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3473 	    "open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph);
3474 
3475 	return (USB_SUCCESS);
3476 }
3477 
3478 
3479 /*
3480  * hubd_start_polling:
3481  *	start or restart the polling
3482  */
3483 static void
3484 hubd_start_polling(hubd_t *hubd, int always)
3485 {
3486 	usb_intr_req_t	*reqp;
3487 	int			rval;
3488 	usb_pipe_state_t	pipe_state;
3489 
3490 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3491 	    "start polling: always=%d dev_state=%d pipe_state=%d\n\t"
3492 	    "thread=%d ep1_ph=0x%p",
3493 	    always, hubd->h_dev_state, hubd->h_intr_pipe_state,
3494 	    hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph);
3495 
3496 	/*
3497 	 * start or restart polling on the intr pipe
3498 	 * only if hotplug thread is not running
3499 	 */
3500 	if ((always == HUBD_ALWAYS_START_POLLING) ||
3501 	    ((hubd->h_dev_state == USB_DEV_ONLINE) &&
3502 	    (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
3503 	    (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) {
3504 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3505 		    "start polling requested");
3506 
3507 		reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP);
3508 
3509 		reqp->intr_client_private = (usb_opaque_t)hubd;
3510 		reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
3511 		    USB_ATTRS_AUTOCLEARING;
3512 		reqp->intr_len = hubd->h_ep1_xdescr.uex_ep.wMaxPacketSize;
3513 		reqp->intr_cb = hubd_read_cb;
3514 		reqp->intr_exc_cb = hubd_exception_cb;
3515 		mutex_exit(HUBD_MUTEX(hubd));
3516 		if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp,
3517 		    USB_FLAGS_SLEEP)) != USB_SUCCESS) {
3518 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3519 			    "start polling failed, rval=%d", rval);
3520 			usb_free_intr_req(reqp);
3521 		}
3522 
3523 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
3524 		    USB_FLAGS_SLEEP);
3525 		if (pipe_state != USB_PIPE_STATE_ACTIVE) {
3526 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
3527 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
3528 		}
3529 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3530 		    "start polling request 0x%p", (void *)reqp);
3531 
3532 		mutex_enter(HUBD_MUTEX(hubd));
3533 	}
3534 }
3535 
3536 
3537 /*
3538  * hubd_stop_polling
3539  *	stop polling but do not close the pipe
3540