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