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