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