xref: /illumos-gate/usr/src/uts/common/io/usb/usba/hubdi.c (revision fef1e07e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*fef1e07eSsl  * Common Development and Distribution License (the "License").
6*fef1e07eSsl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22737d277aScth  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * USBA: Solaris USB Architecture support for the hub
317c478bd9Sstevel@tonic-gate  * including root hub
327c478bd9Sstevel@tonic-gate  * Most of the code for hubd resides in this file and
337c478bd9Sstevel@tonic-gate  * is shared between the HCD root hub support and hubd
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate #define	USBA_FRAMEWORK
367c478bd9Sstevel@tonic-gate #include <sys/usb/usba.h>
377c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_devdb.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
397c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_types.h>
417c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hubdi.h>
427c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hub.h>
447c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hubdvar.h>
457c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hubd_impl.h>
467c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
477c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
487c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Prototypes for static functions
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate static	int	usba_hubdi_bus_ctl(
547c478bd9Sstevel@tonic-gate 			dev_info_t		*dip,
557c478bd9Sstevel@tonic-gate 			dev_info_t		*rdip,
567c478bd9Sstevel@tonic-gate 			ddi_ctl_enum_t		op,
577c478bd9Sstevel@tonic-gate 			void			*arg,
587c478bd9Sstevel@tonic-gate 			void			*result);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static int	usba_hubdi_map_fault(
617c478bd9Sstevel@tonic-gate 			dev_info_t		*dip,
627c478bd9Sstevel@tonic-gate 			dev_info_t		*rdip,
637c478bd9Sstevel@tonic-gate 			struct hat		*hat,
647c478bd9Sstevel@tonic-gate 			struct seg		*seg,
657c478bd9Sstevel@tonic-gate 			caddr_t 		addr,
667c478bd9Sstevel@tonic-gate 			struct devpage		*dp,
677c478bd9Sstevel@tonic-gate 			pfn_t			pfn,
687c478bd9Sstevel@tonic-gate 			uint_t			prot,
697c478bd9Sstevel@tonic-gate 			uint_t			lock);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static int hubd_busop_get_eventcookie(dev_info_t *dip,
727c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
737c478bd9Sstevel@tonic-gate 			char *eventname,
747c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t *cookie);
757c478bd9Sstevel@tonic-gate static int hubd_busop_add_eventcall(dev_info_t *dip,
767c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
777c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t cookie,
787c478bd9Sstevel@tonic-gate 			void (*callback)(dev_info_t *dip,
797c478bd9Sstevel@tonic-gate 				ddi_eventcookie_t cookie, void *arg,
807c478bd9Sstevel@tonic-gate 				void *bus_impldata),
817c478bd9Sstevel@tonic-gate 			void *arg, ddi_callback_id_t *cb_id);
827c478bd9Sstevel@tonic-gate static int hubd_busop_remove_eventcall(dev_info_t *dip,
837c478bd9Sstevel@tonic-gate 			ddi_callback_id_t cb_id);
847c478bd9Sstevel@tonic-gate static int hubd_bus_config(dev_info_t *dip,
857c478bd9Sstevel@tonic-gate 			uint_t flag,
867c478bd9Sstevel@tonic-gate 			ddi_bus_config_op_t op,
877c478bd9Sstevel@tonic-gate 			void *arg,
887c478bd9Sstevel@tonic-gate 			dev_info_t **child);
897c478bd9Sstevel@tonic-gate static int hubd_bus_unconfig(dev_info_t *dip,
907c478bd9Sstevel@tonic-gate 			uint_t flag,
917c478bd9Sstevel@tonic-gate 			ddi_bus_config_op_t op,
927c478bd9Sstevel@tonic-gate 			void *arg);
937c478bd9Sstevel@tonic-gate static int hubd_bus_power(dev_info_t *dip, void *impl_arg,
947c478bd9Sstevel@tonic-gate 			pm_bus_power_op_t op, void *arg, void *result);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static void hubd_get_ancestry_str(hubd_t *);
977c478bd9Sstevel@tonic-gate static usb_port_t  hubd_get_port_num(hubd_t *, struct devctl_iocdata *);
987c478bd9Sstevel@tonic-gate static dev_info_t *hubd_get_child_dip(hubd_t *, usb_port_t);
997c478bd9Sstevel@tonic-gate static uint_t hubd_cfgadm_state(hubd_t *, usb_port_t);
1007c478bd9Sstevel@tonic-gate static int hubd_toggle_port(hubd_t *, usb_port_t);
1017c478bd9Sstevel@tonic-gate static void hubd_register_cpr_callback(hubd_t *);
1027c478bd9Sstevel@tonic-gate static void hubd_unregister_cpr_callback(hubd_t *);
1037c478bd9Sstevel@tonic-gate static hubd_t *hubd_get_soft_state(dev_info_t *dip);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Busops vector for USB HUB's
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate struct bus_ops usba_hubdi_busops =	{
1097c478bd9Sstevel@tonic-gate 	BUSO_REV,
1107c478bd9Sstevel@tonic-gate 	nullbusmap,			/* bus_map */
1117c478bd9Sstevel@tonic-gate 	NULL,				/* bus_get_intrspec */
1127c478bd9Sstevel@tonic-gate 	NULL,				/* bus_add_intrspec */
1137c478bd9Sstevel@tonic-gate 	NULL,				/* bus_remove_intrspec */
1147c478bd9Sstevel@tonic-gate 	usba_hubdi_map_fault,		/* bus_map_fault */
1157c478bd9Sstevel@tonic-gate 	ddi_dma_map,			/* bus_dma_map */
1167c478bd9Sstevel@tonic-gate 	ddi_dma_allochdl,
1177c478bd9Sstevel@tonic-gate 	ddi_dma_freehdl,
1187c478bd9Sstevel@tonic-gate 	ddi_dma_bindhdl,
1197c478bd9Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1207c478bd9Sstevel@tonic-gate 	ddi_dma_flush,
1217c478bd9Sstevel@tonic-gate 	ddi_dma_win,
1227c478bd9Sstevel@tonic-gate 	ddi_dma_mctl,			/* bus_dma_ctl */
1237c478bd9Sstevel@tonic-gate 	usba_hubdi_bus_ctl,		/* bus_ctl */
1247c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,		/* bus_prop_op */
1257c478bd9Sstevel@tonic-gate 	hubd_busop_get_eventcookie,
1267c478bd9Sstevel@tonic-gate 	hubd_busop_add_eventcall,
1277c478bd9Sstevel@tonic-gate 	hubd_busop_remove_eventcall,
1287c478bd9Sstevel@tonic-gate 	NULL,				/* bus_post_event */
1297c478bd9Sstevel@tonic-gate 	NULL,				/* bus_intr_ctl */
1307c478bd9Sstevel@tonic-gate 	hubd_bus_config,		/* bus_config */
1317c478bd9Sstevel@tonic-gate 	hubd_bus_unconfig,		/* bus_unconfig */
1327c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_init */
1337c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_fini */
1347c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_enter */
1357c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_exit */
1367c478bd9Sstevel@tonic-gate 	hubd_bus_power			/* bus_power */
1377c478bd9Sstevel@tonic-gate };
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * local variables
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate static kmutex_t	usba_hubdi_mutex;	/* protects USBA HUB data structures */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static usba_list_entry_t	usba_hubdi_list;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate usb_log_handle_t	hubdi_log_handle;
1487c478bd9Sstevel@tonic-gate uint_t			hubdi_errlevel = USB_LOG_L4;
1497c478bd9Sstevel@tonic-gate uint_t			hubdi_errmask = (uint_t)-1;
1507c478bd9Sstevel@tonic-gate uint_t			hubdi_min_pm_threshold = 5; /* seconds */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * initialize private data
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate void
1567c478bd9Sstevel@tonic-gate usba_hubdi_initialization()
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	hubdi_log_handle = usb_alloc_log_hdl(NULL, "hubdi", &hubdi_errlevel,
1597c478bd9Sstevel@tonic-gate 				&hubdi_errmask, NULL, 0);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
1627c478bd9Sstevel@tonic-gate 	    "usba_hubdi_initialization");
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	mutex_init(&usba_hubdi_mutex, NULL, MUTEX_DRIVER, NULL);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	usba_init_list(&usba_hubdi_list, NULL, NULL);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate void
1717c478bd9Sstevel@tonic-gate usba_hubdi_destroy()
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
1747c478bd9Sstevel@tonic-gate 	    "usba_hubdi_destroy");
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	mutex_destroy(&usba_hubdi_mutex);
1777c478bd9Sstevel@tonic-gate 	usba_destroy_list(&usba_hubdi_list);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(hubdi_log_handle);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * Called by an	HUB to attach an instance of the driver
1857c478bd9Sstevel@tonic-gate  *	make this instance known to USBA
1867c478bd9Sstevel@tonic-gate  *	the HUB	should initialize usba_hubdi structure prior
1877c478bd9Sstevel@tonic-gate  *	to calling this	interface
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate static int
1907c478bd9Sstevel@tonic-gate usba_hubdi_register(dev_info_t	*dip,
1917c478bd9Sstevel@tonic-gate 		uint_t		flags)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	usba_hubdi_t *hubdi = kmem_zalloc(sizeof (usba_hubdi_t), KM_SLEEP);
1947c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
1977c478bd9Sstevel@tonic-gate 	    "usba_hubdi_register: %s", ddi_node_name(dip));
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	hubdi->hubdi_dip = dip;
2007c478bd9Sstevel@tonic-gate 	hubdi->hubdi_flags = flags;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	usba_device->usb_hubdi = hubdi;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * add this hubdi instance to the list of known hubdi's
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 	usba_init_list(&hubdi->hubdi_list, (usb_opaque_t)hubdi,
2087c478bd9Sstevel@tonic-gate 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)->
2097c478bd9Sstevel@tonic-gate 	    hcdi_iblock_cookie);
2107c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_hubdi_mutex);
2117c478bd9Sstevel@tonic-gate 	usba_add_to_list(&usba_hubdi_list, &hubdi->hubdi_list);
2127c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_hubdi_mutex);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Called by an	HUB to detach an instance of the driver
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate static int
2227c478bd9Sstevel@tonic-gate usba_hubdi_unregister(dev_info_t *dip)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
2257c478bd9Sstevel@tonic-gate 	usba_hubdi_t *hubdi = usba_device->usb_hubdi;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
2287c478bd9Sstevel@tonic-gate 	    "usba_hubdi_unregister: %s", ddi_node_name(dip));
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_hubdi_mutex);
2317c478bd9Sstevel@tonic-gate 	(void) usba_rm_from_list(&usba_hubdi_list, &hubdi->hubdi_list);
2327c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_hubdi_mutex);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	usba_destroy_list(&hubdi->hubdi_list);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	kmem_free(hubdi, sizeof (usba_hubdi_t));
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * misc bus routines currently not used
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2467c478bd9Sstevel@tonic-gate static int
2477c478bd9Sstevel@tonic-gate usba_hubdi_map_fault(dev_info_t *dip,
2487c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
2497c478bd9Sstevel@tonic-gate 	struct hat	*hat,
2507c478bd9Sstevel@tonic-gate 	struct seg	*seg,
2517c478bd9Sstevel@tonic-gate 	caddr_t 	addr,
2527c478bd9Sstevel@tonic-gate 	struct devpage	*dp,
2537c478bd9Sstevel@tonic-gate 	pfn_t		pfn,
2547c478bd9Sstevel@tonic-gate 	uint_t		prot,
2557c478bd9Sstevel@tonic-gate 	uint_t		lock)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * root hub support. the root hub uses the same devi as the HCD
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate int
2657c478bd9Sstevel@tonic-gate usba_hubdi_bind_root_hub(dev_info_t *dip,
2667c478bd9Sstevel@tonic-gate 	uchar_t	*root_hub_config_descriptor,
2677c478bd9Sstevel@tonic-gate 	size_t config_length,
2687c478bd9Sstevel@tonic-gate 	usb_dev_descr_t *root_hub_device_descriptor)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device;
2717c478bd9Sstevel@tonic-gate 	usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
2727c478bd9Sstevel@tonic-gate 	hubd_t	*root_hubd;
2737c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t ph = NULL;
2747c478bd9Sstevel@tonic-gate 	dev_info_t *child = ddi_get_child(dip);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
2777c478bd9Sstevel@tonic-gate 	    "root-hub") != NDI_SUCCESS) {
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	root_hubd = kmem_zalloc(sizeof (hubd_t), KM_SLEEP);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * create and initialize a usba_device structure
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	usba_device = usba_alloc_usba_device(dip);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
2907c478bd9Sstevel@tonic-gate 	usba_device->usb_hcdi_ops = hcdi->hcdi_ops;
2917c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg = root_hub_config_descriptor;
2927c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_length = config_length;
2937c478bd9Sstevel@tonic-gate 	usba_device->usb_dev_descr = root_hub_device_descriptor;
2947c478bd9Sstevel@tonic-gate 	usba_device->usb_port = 1;
2957c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = ROOT_HUB_ADDR;
2967c478bd9Sstevel@tonic-gate 	usba_device->usb_root_hubd = root_hubd;
2977c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array = kmem_zalloc(sizeof (uchar_t *),
2987c478bd9Sstevel@tonic-gate 								KM_SLEEP);
2997c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array_length = sizeof (uchar_t *);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array_len = kmem_zalloc(sizeof (uint16_t),
3027c478bd9Sstevel@tonic-gate 								KM_SLEEP);
3037c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array_len_length = sizeof (uint16_t);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array[0] = root_hub_config_descriptor;
3067c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array_len[0] =
3077c478bd9Sstevel@tonic-gate 				sizeof (root_hub_config_descriptor);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_str_descr = kmem_zalloc(sizeof (uchar_t *),
3107c478bd9Sstevel@tonic-gate 								KM_SLEEP);
3117c478bd9Sstevel@tonic-gate 	usba_device->usb_n_cfgs = 1;
3127c478bd9Sstevel@tonic-gate 	usba_device->usb_n_ifs = 1;
3137c478bd9Sstevel@tonic-gate 	usba_device->usb_dip = dip;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	usba_device->usb_client_flags = kmem_zalloc(
3167c478bd9Sstevel@tonic-gate 	    usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	usba_device->usb_client_attach_list = kmem_zalloc(
3197c478bd9Sstevel@tonic-gate 	    usba_device->usb_n_ifs *
3207c478bd9Sstevel@tonic-gate 	    sizeof (*usba_device->usb_client_attach_list), KM_SLEEP);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	usba_device->usb_client_ev_cb_list = kmem_zalloc(
3237c478bd9Sstevel@tonic-gate 	    usba_device->usb_n_ifs *
3247c478bd9Sstevel@tonic-gate 	    sizeof (*usba_device->usb_client_ev_cb_list), KM_SLEEP);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * The bDeviceProtocol field of root hub device specifies,
3287c478bd9Sstevel@tonic-gate 	 * whether root hub is a High or Full speed usb device.
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	if (root_hub_device_descriptor->bDeviceProtocol) {
3317c478bd9Sstevel@tonic-gate 		usba_device->usb_port_status = USBA_HIGH_SPEED_DEV;
3327c478bd9Sstevel@tonic-gate 	} else {
3337c478bd9Sstevel@tonic-gate 		usba_device->usb_port_status = USBA_FULL_SPEED_DEV;
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	usba_set_usba_device(dip, usba_device);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * For the root hub the default pipe is not yet open
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 	if (usb_pipe_open(dip, NULL, NULL,
3447c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != USB_SUCCESS) {
3457c478bd9Sstevel@tonic-gate 		goto fail;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * kill off all OBP children, they may not be fully
3507c478bd9Sstevel@tonic-gate 	 * enumerated
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	while (child) {
3537c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(child);
3547c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(child, 0);
3557c478bd9Sstevel@tonic-gate 		child = next;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * "attach" the root hub driver
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	if (usba_hubdi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
3627c478bd9Sstevel@tonic-gate 		goto fail;
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate fail:
3687c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (ph) {
3717c478bd9Sstevel@tonic-gate 		usb_pipe_close(dip, ph,
3727c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array,
3767c478bd9Sstevel@tonic-gate 			usba_device->usb_cfg_array_length);
3777c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array_len,
3787c478bd9Sstevel@tonic-gate 			usba_device->usb_cfg_array_len_length);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	usba_free_usba_device(usba_device);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	usba_set_usba_device(dip, NULL);
3857c478bd9Sstevel@tonic-gate 	if (root_hubd) {
3867c478bd9Sstevel@tonic-gate 		kmem_free(root_hubd, sizeof (hubd_t));
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate int
3947c478bd9Sstevel@tonic-gate usba_hubdi_unbind_root_hub(dev_info_t *dip)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/* was root hub attached? */
3997c478bd9Sstevel@tonic-gate 	if (!(usba_is_root_hub(dip))) {
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		/* return success anyway */
4027c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * usba_hubdi_detach also closes the default pipe
4077c478bd9Sstevel@tonic-gate 	 * and removes properties so there is no need to
4087c478bd9Sstevel@tonic-gate 	 * do it here
4097c478bd9Sstevel@tonic-gate 	 */
4107c478bd9Sstevel@tonic-gate 	if (usba_hubdi_detach(dip, DDI_DETACH) != DDI_SUCCESS) {
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(dip)) {
413d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
4147c478bd9Sstevel@tonic-gate 			    "failure to unbind root hub after attach failure");
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_root_hubd, sizeof (hubd_t));
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array,
4257c478bd9Sstevel@tonic-gate 			usba_device->usb_cfg_array_length);
4267c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array_len,
4277c478bd9Sstevel@tonic-gate 			usba_device->usb_cfg_array_len_length);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	usba_free_usba_device(usba_device);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * Actual Hub Driver support code:
4417c478bd9Sstevel@tonic-gate  *	shared by root hub and non-root hubs
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate /* Debugging support */
4467c478bd9Sstevel@tonic-gate static uint_t hubd_errlevel	= USB_LOG_L4;
4477c478bd9Sstevel@tonic-gate static uint_t hubd_errmask	= (uint_t)DPRINT_MASK_ALL;
4487c478bd9Sstevel@tonic-gate static uint_t hubd_instance_debug = (uint_t)-1;
4497c478bd9Sstevel@tonic-gate static uint_t hubdi_bus_config_debug = 0;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel))
4527c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask))
4537c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug))
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
4567c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate  * local variables:
4617c478bd9Sstevel@tonic-gate  *
4627c478bd9Sstevel@tonic-gate  * Amount of time to wait between resetting the port and accessing
4637c478bd9Sstevel@tonic-gate  * the device.	The value is in microseconds.
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate static uint_t hubd_device_delay = 1000000;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * enumeration retry
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate #define	HUBD_PORT_RETRY 5
4717c478bd9Sstevel@tonic-gate static uint_t hubd_retry_enumerate = HUBD_PORT_RETRY;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * Stale hotremoved device cleanup delay
4757c478bd9Sstevel@tonic-gate  */
4767c478bd9Sstevel@tonic-gate #define	HUBD_STALE_DIP_CLEANUP_DELAY	5000000
4777c478bd9Sstevel@tonic-gate static uint_t hubd_dip_cleanup_delay = HUBD_STALE_DIP_CLEANUP_DELAY;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  * retries for USB suspend and resume
4817c478bd9Sstevel@tonic-gate  */
4827c478bd9Sstevel@tonic-gate #define	HUBD_SUS_RES_RETRY	2
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate void	*hubd_statep;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * prototypes
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate static int hubd_cleanup(dev_info_t *dip, hubd_t  *hubd);
4907c478bd9Sstevel@tonic-gate static int hubd_check_ports(hubd_t  *hubd);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate static void hubd_schedule_cleanup(dev_info_t *);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate static int  hubd_open_intr_pipe(hubd_t *hubd);
4957c478bd9Sstevel@tonic-gate static void hubd_start_polling(hubd_t *hubd, int always);
4967c478bd9Sstevel@tonic-gate static void hubd_stop_polling(hubd_t *hubd);
4977c478bd9Sstevel@tonic-gate static void hubd_close_intr_pipe(hubd_t *hubd);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static void hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *req);
5007c478bd9Sstevel@tonic-gate static void hubd_exception_cb(usb_pipe_handle_t pipe,
5017c478bd9Sstevel@tonic-gate 						usb_intr_req_t *req);
5027c478bd9Sstevel@tonic-gate static void hubd_hotplug_thread(void *arg);
5037c478bd9Sstevel@tonic-gate static int hubd_create_child(dev_info_t *dip,
5047c478bd9Sstevel@tonic-gate 		hubd_t		*hubd,
5057c478bd9Sstevel@tonic-gate 		usba_device_t	*usba_device,
5067c478bd9Sstevel@tonic-gate 		usb_port_status_t port_status,
5077c478bd9Sstevel@tonic-gate 		usb_port_t	port,
5087c478bd9Sstevel@tonic-gate 		int		iteration);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate static int hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag,
5117c478bd9Sstevel@tonic-gate 	boolean_t retry);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate static int hubd_get_hub_descriptor(hubd_t *hubd);
5147c478bd9Sstevel@tonic-gate 
51535f36846Ssl static int hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status);
51635f36846Ssl 
5177c478bd9Sstevel@tonic-gate static int hubd_reset_port(hubd_t *hubd, usb_port_t port);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate static int hubd_get_hub_status(hubd_t *hubd);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate static int hubd_handle_port_connect(hubd_t *hubd, usb_port_t port);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate static int hubd_disable_port(hubd_t *hubd, usb_port_t port);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate static int hubd_enable_port(hubd_t *hubd, usb_port_t port);
5267c478bd9Sstevel@tonic-gate static int hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate static int hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
5297c478bd9Sstevel@tonic-gate 	uint16_t *status, uint16_t *change, uint_t ack_flag);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate static int hubd_enable_all_port_power(hubd_t *hubd);
5327c478bd9Sstevel@tonic-gate static int hubd_disable_all_port_power(hubd_t *hubd);
5337c478bd9Sstevel@tonic-gate static int hubd_disable_port_power(hubd_t *hubd, usb_port_t port);
5347c478bd9Sstevel@tonic-gate static int hubd_enable_port_power(hubd_t *hubd, usb_port_t port);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate static void hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate static int hubd_can_suspend(hubd_t *hubd);
5397c478bd9Sstevel@tonic-gate static void hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd);
5407c478bd9Sstevel@tonic-gate static int hubd_setdevaddr(hubd_t *hubd, usb_port_t port);
5417c478bd9Sstevel@tonic-gate static void hubd_setdevconfig(hubd_t *hubd, usb_port_t port);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate static int hubd_register_events(hubd_t *hubd);
5447c478bd9Sstevel@tonic-gate static void hubd_do_callback(hubd_t *hubd, dev_info_t *dip,
5457c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie);
5467c478bd9Sstevel@tonic-gate static void hubd_run_callbacks(hubd_t *hubd, usba_event_t type);
5477c478bd9Sstevel@tonic-gate static void hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type);
5487c478bd9Sstevel@tonic-gate static void hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate static int hubd_disconnect_event_cb(dev_info_t *dip);
5517c478bd9Sstevel@tonic-gate static int hubd_reconnect_event_cb(dev_info_t *dip);
5527c478bd9Sstevel@tonic-gate static int hubd_pre_suspend_event_cb(dev_info_t *dip);
5537c478bd9Sstevel@tonic-gate static int hubd_post_resume_event_cb(dev_info_t *dip);
5547c478bd9Sstevel@tonic-gate static int hubd_cpr_suspend(hubd_t *hubd);
5557c478bd9Sstevel@tonic-gate static void hubd_cpr_resume(dev_info_t *dip);
5567c478bd9Sstevel@tonic-gate static int hubd_restore_state_cb(dev_info_t *dip);
5577c478bd9Sstevel@tonic-gate 
55835f36846Ssl static int hubd_init_power_budget(hubd_t *hubd);
55935f36846Ssl 
5607c478bd9Sstevel@tonic-gate static ndi_event_definition_t hubd_ndi_event_defs[] = {
5617c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
5627c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5637c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
5647c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5657c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
5667c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5677c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
5687c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL}
5697c478bd9Sstevel@tonic-gate };
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate #define	HUBD_N_NDI_EVENTS \
5727c478bd9Sstevel@tonic-gate 	(sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t))
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate static ndi_event_set_t hubd_ndi_events = {
5757c478bd9Sstevel@tonic-gate 	NDI_EVENTS_REV1, HUBD_N_NDI_EVENTS, hubd_ndi_event_defs};
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /* events received from parent */
5787c478bd9Sstevel@tonic-gate static usb_event_t hubd_events = {
5797c478bd9Sstevel@tonic-gate 	hubd_disconnect_event_cb,
5807c478bd9Sstevel@tonic-gate 	hubd_reconnect_event_cb,
5817c478bd9Sstevel@tonic-gate 	hubd_pre_suspend_event_cb,
5827c478bd9Sstevel@tonic-gate 	hubd_post_resume_event_cb
5837c478bd9Sstevel@tonic-gate };
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate  * hubd_get_soft_state() returns the hubd soft state
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate static hubd_t *
5907c478bd9Sstevel@tonic-gate hubd_get_soft_state(dev_info_t *dip)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		return (NULL);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
5987c478bd9Sstevel@tonic-gate 		usba_device_t *usba_device = usba_get_usba_device(dip);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		return (usba_device->usb_root_hubd);
6017c478bd9Sstevel@tonic-gate 	} else {
6027c478bd9Sstevel@tonic-gate 		int instance = ddi_get_instance(dip);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		return (ddi_get_soft_state(hubd_statep, instance));
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * PM support functions:
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6137c478bd9Sstevel@tonic-gate static void
6147c478bd9Sstevel@tonic-gate hubd_pm_busy_component(hubd_t *hubd, dev_info_t *dip, int component)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	if (hubd->h_hubpm != NULL) {
6177c478bd9Sstevel@tonic-gate 		hubd->h_hubpm->hubp_busy_pm++;
6187c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
6197c478bd9Sstevel@tonic-gate 		if (pm_busy_component(dip, 0) != DDI_SUCCESS) {
6207c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
6217c478bd9Sstevel@tonic-gate 			hubd->h_hubpm->hubp_busy_pm--;
6227c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
6257c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
6267c478bd9Sstevel@tonic-gate 		    "hubd_pm_busy_component: %d", hubd->h_hubpm->hubp_busy_pm);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6327c478bd9Sstevel@tonic-gate static void
6337c478bd9Sstevel@tonic-gate hubd_pm_idle_component(hubd_t *hubd, dev_info_t *dip, int component)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	if (hubd->h_hubpm != NULL) {
6367c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
6377c478bd9Sstevel@tonic-gate 		if (pm_idle_component(dip, 0) == DDI_SUCCESS) {
6387c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
6397c478bd9Sstevel@tonic-gate 			ASSERT(hubd->h_hubpm->hubp_busy_pm > 0);
6407c478bd9Sstevel@tonic-gate 			hubd->h_hubpm->hubp_busy_pm--;
6417c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
6447c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
6457c478bd9Sstevel@tonic-gate 		    "hubd_pm_idle_component: %d", hubd->h_hubpm->hubp_busy_pm);
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * track power level changes for children of this instance
6527c478bd9Sstevel@tonic-gate  */
6537c478bd9Sstevel@tonic-gate static void
6547c478bd9Sstevel@tonic-gate hubd_set_child_pwrlvl(hubd_t *hubd, usb_port_t port, uint8_t power)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	int	old_power, new_power, pwr;
6577c478bd9Sstevel@tonic-gate 	usb_port_t	portno;
6587c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
6617c478bd9Sstevel@tonic-gate 	    "hubd_set_child_pwrlvl: port=%d power=%d",
6627c478bd9Sstevel@tonic-gate 	    port, power);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
6657c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	old_power = 0;
6687c478bd9Sstevel@tonic-gate 	for (portno = 1; portno <= hubd->h_hub_descr.bNbrPorts; portno++) {
6697c478bd9Sstevel@tonic-gate 		old_power += hubpm->hubp_child_pwrstate[portno];
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/* assign the port power */
6737c478bd9Sstevel@tonic-gate 	pwr = hubd->h_hubpm->hubp_child_pwrstate[port];
6747c478bd9Sstevel@tonic-gate 	hubd->h_hubpm->hubp_child_pwrstate[port] = power;
6757c478bd9Sstevel@tonic-gate 	new_power = old_power - pwr + power;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
6787c478bd9Sstevel@tonic-gate 	    "hubd_set_child_pwrlvl: new_power=%d old_power=%d",
6797c478bd9Sstevel@tonic-gate 	    new_power, old_power);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if ((new_power > 0) && (old_power == 0)) {
6827c478bd9Sstevel@tonic-gate 		/* we have the first child coming out of low power */
6837c478bd9Sstevel@tonic-gate 		(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
6847c478bd9Sstevel@tonic-gate 	} else if ((new_power == 0) && (old_power > 0)) {
6857c478bd9Sstevel@tonic-gate 		/* we have the last child going to low power */
6867c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * given a child dip, locate its port number
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate static usb_port_t
6967c478bd9Sstevel@tonic-gate hubd_child_dip2port(hubd_t *hubd, dev_info_t *dip)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	usb_port_t	port;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
7017c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
7027c478bd9Sstevel@tonic-gate 		if (hubd->h_children_dips[port] == dip) {
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 			break;
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	ASSERT(port <= hubd->h_hub_descr.bNbrPorts);
7087c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	return (port);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate /*
7157c478bd9Sstevel@tonic-gate  * if the hub can be put into low power mode, return success
7167c478bd9Sstevel@tonic-gate  * NOTE: suspend here means going to lower power, not CPR suspend.
7177c478bd9Sstevel@tonic-gate  */
7187c478bd9Sstevel@tonic-gate static int
7197c478bd9Sstevel@tonic-gate hubd_can_suspend(hubd_t *hubd)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
7227c478bd9Sstevel@tonic-gate 	int		total_power = 0;
7237c478bd9Sstevel@tonic-gate 	usb_port_t	port;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DETACHING(hubd->h_dip)) {
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * Don't go to lower power if haven't been at full power for enough
7347c478bd9Sstevel@tonic-gate 	 * time to let hotplug thread kickoff.
7357c478bd9Sstevel@tonic-gate 	 */
7367c478bd9Sstevel@tonic-gate 	if (ddi_get_time() < (hubpm->hubp_time_at_full_power +
7377c478bd9Sstevel@tonic-gate 	    hubpm->hubp_min_pm_threshold)) {
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	for (port = 1; (total_power == 0) &&
7437c478bd9Sstevel@tonic-gate 	    (port <= hubd->h_hub_descr.bNbrPorts); port++) {
7447c478bd9Sstevel@tonic-gate 		total_power += hubpm->hubp_child_pwrstate[port];
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
7487c478bd9Sstevel@tonic-gate 		"hubd_can_suspend: %d", total_power);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	return (total_power ? USB_FAILURE : USB_SUCCESS);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate  * resume port depending on current device state
7567c478bd9Sstevel@tonic-gate  */
7577c478bd9Sstevel@tonic-gate static int
7587c478bd9Sstevel@tonic-gate hubd_resume_port(hubd_t *hubd, usb_port_t port)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	int		rval, retry;
7617c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
7627c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
7637c478bd9Sstevel@tonic-gate 	uint16_t	status;
7647c478bd9Sstevel@tonic-gate 	uint16_t	change;
7657c478bd9Sstevel@tonic-gate 	int		retval = USB_FAILURE;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
7707c478bd9Sstevel@tonic-gate 	    "hubd_resume_port: port=%d state=0x%x (%s)", port,
7717c478bd9Sstevel@tonic-gate 	    hubd->h_dev_state, usb_str_dev_state(hubd->h_dev_state));
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
7747c478bd9Sstevel@tonic-gate 	case USB_DEV_HUB_CHILD_PWRLVL:
7757c478bd9Sstevel@tonic-gate 		/*
7767c478bd9Sstevel@tonic-gate 		 * This could be a bus ctl for a port other than the one
7777c478bd9Sstevel@tonic-gate 		 * that has a remote wakeup condition. So check.
7787c478bd9Sstevel@tonic-gate 		 */
7797c478bd9Sstevel@tonic-gate 		if ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0) {
7807c478bd9Sstevel@tonic-gate 			/* the port isn't suspended, so don't resume */
7817c478bd9Sstevel@tonic-gate 			retval = USB_SUCCESS;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
7847c478bd9Sstevel@tonic-gate 			    "hubd_resume_port: port=%d not suspended", port);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 			break;
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 		/*
7897c478bd9Sstevel@tonic-gate 		 * Device has initiated a wakeup.
7907c478bd9Sstevel@tonic-gate 		 * Issue a ClearFeature(PortSuspend)
7917c478bd9Sstevel@tonic-gate 		 */
7927c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
7937c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
7947c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
79535f36846Ssl 		    HUB_HANDLE_PORT_FEATURE_TYPE,
7967c478bd9Sstevel@tonic-gate 		    USB_REQ_CLEAR_FEATURE,
7977c478bd9Sstevel@tonic-gate 		    CFS_PORT_SUSPEND,
7987c478bd9Sstevel@tonic-gate 		    port,
7997c478bd9Sstevel@tonic-gate 		    0, NULL, 0,
8007c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
8017c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
8027c478bd9Sstevel@tonic-gate 			    "ClearFeature(PortSuspend) fails "
8037c478bd9Sstevel@tonic-gate 			    "rval=%d cr=%d cb=0x%x", rval,
8047c478bd9Sstevel@tonic-gate 			    completion_reason, cb_flags);
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		/* either way ack changes on the port */
8097c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd, port,
8107c478bd9Sstevel@tonic-gate 			&status, &change, PORT_CHANGE_PSSC);
8117c478bd9Sstevel@tonic-gate 		retval = USB_SUCCESS;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		break;
8147c478bd9Sstevel@tonic-gate 	case USB_DEV_HUB_STATE_RECOVER:
8157c478bd9Sstevel@tonic-gate 		/*
8167c478bd9Sstevel@tonic-gate 		 * When hubd's connect event callback posts a connect
8177c478bd9Sstevel@tonic-gate 		 * event to its child, it results in this busctl call
8187c478bd9Sstevel@tonic-gate 		 * which is valid
8197c478bd9Sstevel@tonic-gate 		 */
8207c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
8217c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
8227c478bd9Sstevel@tonic-gate 		if ((hubd->h_port_state[port] &
8237c478bd9Sstevel@tonic-gate 		    (PORT_STATUS_PSS | PORT_STATUS_CCS)) == 0) {
8247c478bd9Sstevel@tonic-gate 			/*
8257c478bd9Sstevel@tonic-gate 			 * the port isn't suspended, or connected
8267c478bd9Sstevel@tonic-gate 			 * so don't resume
8277c478bd9Sstevel@tonic-gate 			 */
8287c478bd9Sstevel@tonic-gate 			retval = USB_SUCCESS;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
8317c478bd9Sstevel@tonic-gate 			    "hubd_resume_port: port=%d not suspended", port);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 			break;
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 		/*
8367c478bd9Sstevel@tonic-gate 		 * prevent kicking off the hotplug thread
8377c478bd9Sstevel@tonic-gate 		 */
8387c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread++;
8397c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		/* Now ClearFeature(PortSuspend) */
8427c478bd9Sstevel@tonic-gate 		for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
8437c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
8447c478bd9Sstevel@tonic-gate 			rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
8457c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
84635f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
8477c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
8487c478bd9Sstevel@tonic-gate 			    CFS_PORT_SUSPEND,
8497c478bd9Sstevel@tonic-gate 			    port,
8507c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
8517c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0);
8527c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
8537c478bd9Sstevel@tonic-gate 			if (rval != USB_SUCCESS) {
8547c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PM,
8557c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
8567c478bd9Sstevel@tonic-gate 				    "ClearFeature(PortSuspend) fails"
8577c478bd9Sstevel@tonic-gate 				    "rval=%d cr=%d cb=0x%x", rval,
8587c478bd9Sstevel@tonic-gate 				    completion_reason, cb_flags);
8597c478bd9Sstevel@tonic-gate 			} else {
8607c478bd9Sstevel@tonic-gate 				/*
8617c478bd9Sstevel@tonic-gate 				 * As per spec section 11.9 and 7.1.7.7
8627c478bd9Sstevel@tonic-gate 				 * hub need to provide at least 20ms of
8637c478bd9Sstevel@tonic-gate 				 * resume signalling, and s/w provide 10ms of
8647c478bd9Sstevel@tonic-gate 				 * recovery time before accessing the port.
8657c478bd9Sstevel@tonic-gate 				 */
8667c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
8677c478bd9Sstevel@tonic-gate 				delay(drv_usectohz(40000));
8687c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
8697c478bd9Sstevel@tonic-gate 				(void) hubd_determine_port_status(hubd, port,
8707c478bd9Sstevel@tonic-gate 				    &status, &change, PORT_CHANGE_PSSC);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 				if ((status & PORT_STATUS_PSS) == 0) {
8737c478bd9Sstevel@tonic-gate 					/* the port did finally resume */
8747c478bd9Sstevel@tonic-gate 					retval = USB_SUCCESS;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 					break;
8777c478bd9Sstevel@tonic-gate 				}
8787c478bd9Sstevel@tonic-gate 			}
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 		/* allow hotplug thread again */
8827c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread--;
8837c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		break;
8867c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
8877c478bd9Sstevel@tonic-gate 		/* Ignore - NO Operation */
8887c478bd9Sstevel@tonic-gate 		retval = USB_SUCCESS;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		break;
8917c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
8927c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
8937c478bd9Sstevel@tonic-gate 	default:
8947c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
8957c478bd9Sstevel@tonic-gate 		    "Improper state for port Resume");
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		break;
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	return (retval);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate /*
9067c478bd9Sstevel@tonic-gate  * suspend port depending on device state
9077c478bd9Sstevel@tonic-gate  */
9087c478bd9Sstevel@tonic-gate static int
9097c478bd9Sstevel@tonic-gate hubd_suspend_port(hubd_t *hubd, usb_port_t port)
9107c478bd9Sstevel@tonic-gate {
9117c478bd9Sstevel@tonic-gate 	int		rval, retry;
9127c478bd9Sstevel@tonic-gate 	int		retval = USB_FAILURE;
9137c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
9147c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
9157c478bd9Sstevel@tonic-gate 	uint16_t	status;
9167c478bd9Sstevel@tonic-gate 	uint16_t	change;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
9197c478bd9Sstevel@tonic-gate 	    "hubd_suspend_port: port=%d", port);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
9247c478bd9Sstevel@tonic-gate 	case USB_DEV_HUB_STATE_RECOVER:
9257c478bd9Sstevel@tonic-gate 		/*
9267c478bd9Sstevel@tonic-gate 		 * When hubd's connect event callback posts a connect
9277c478bd9Sstevel@tonic-gate 		 * event to its child, it results in this busctl call
9287c478bd9Sstevel@tonic-gate 		 * which is valid
9297c478bd9Sstevel@tonic-gate 		 */
9307c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
9317c478bd9Sstevel@tonic-gate 	case USB_DEV_HUB_CHILD_PWRLVL:
9327c478bd9Sstevel@tonic-gate 		/*
9337c478bd9Sstevel@tonic-gate 		 * When one child is resuming, the other could timeout
9347c478bd9Sstevel@tonic-gate 		 * and go to low power mode, which is valid
9357c478bd9Sstevel@tonic-gate 		 */
9367c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
9377c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
9387c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread++;
9397c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		/*
9427c478bd9Sstevel@tonic-gate 		 * Some devices start an unprovoked resume.  According to spec,
9437c478bd9Sstevel@tonic-gate 		 * normal resume time for port is 10ms.  Wait for double that
9447c478bd9Sstevel@tonic-gate 		 * time, then check to be sure port is really suspended.
9457c478bd9Sstevel@tonic-gate 		 */
9467c478bd9Sstevel@tonic-gate 		for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
9477c478bd9Sstevel@tonic-gate 			/* Now SetFeature(PortSuspend) */
9487c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
9497c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
9507c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
95135f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
9527c478bd9Sstevel@tonic-gate 			    USB_REQ_SET_FEATURE,
9537c478bd9Sstevel@tonic-gate 			    CFS_PORT_SUSPEND,
9547c478bd9Sstevel@tonic-gate 			    port,
9557c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
9567c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
9577c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
9587c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PM,
9597c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
9607c478bd9Sstevel@tonic-gate 				    "SetFeature(PortSuspend) fails"
9617c478bd9Sstevel@tonic-gate 				    "rval=%d cr=%d cb=0x%x",
9627c478bd9Sstevel@tonic-gate 				    rval, completion_reason, cb_flags);
9637c478bd9Sstevel@tonic-gate 			}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 			/*
9667c478bd9Sstevel@tonic-gate 			 * some devices start an unprovoked resume
9677c478bd9Sstevel@tonic-gate 			 * wait and check port status after some time
9687c478bd9Sstevel@tonic-gate 			 */
9697c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(20000));
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 			/* either ways ack changes on the port */
9727c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
9737c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
9747c478bd9Sstevel@tonic-gate 			    &status, &change, PORT_CHANGE_PSSC);
9757c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_PSS) {
9767c478bd9Sstevel@tonic-gate 				/* the port is indeed suspended */
9777c478bd9Sstevel@tonic-gate 				retval = USB_SUCCESS;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 				break;
9807c478bd9Sstevel@tonic-gate 			}
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread--;
9847c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		break;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
9897c478bd9Sstevel@tonic-gate 		/* Ignore - No Operation */
9907c478bd9Sstevel@tonic-gate 		retval = USB_SUCCESS;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		break;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
9957c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
9967c478bd9Sstevel@tonic-gate 	default:
9977c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
9987c478bd9Sstevel@tonic-gate 		    "Improper state for port Suspend");
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 		break;
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	return (retval);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate /*
10097c478bd9Sstevel@tonic-gate  * child post attach/detach notifications
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate static void
10127c478bd9Sstevel@tonic-gate hubd_post_attach(hubd_t *hubd, usb_port_t port, struct attachspec *as)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
10177c478bd9Sstevel@tonic-gate 	    "hubd_post_attach: port=%d result=%d",
10187c478bd9Sstevel@tonic-gate 	    port, as->result);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if (as->result == DDI_SUCCESS) {
10217c478bd9Sstevel@tonic-gate 		/*
10227c478bd9Sstevel@tonic-gate 		 * Check if the child created wants to be power managed.
10237c478bd9Sstevel@tonic-gate 		 * If yes, the childs power level gets automatically tracked
10247c478bd9Sstevel@tonic-gate 		 * by DDI_CTLOPS_POWER busctl.
10257c478bd9Sstevel@tonic-gate 		 * If no, we set power of the new child by default
10267c478bd9Sstevel@tonic-gate 		 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
10277c478bd9Sstevel@tonic-gate 		 */
10287c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
10297c478bd9Sstevel@tonic-gate 		dip = hubd->h_children_dips[port];
10307c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10317c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_pm_info == NULL) {
10327c478bd9Sstevel@tonic-gate 			hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_FULL_PWR);
10337c478bd9Sstevel@tonic-gate 		}
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate static void
10397c478bd9Sstevel@tonic-gate hubd_post_detach(hubd_t *hubd, usb_port_t port, struct detachspec *ds)
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
10427c478bd9Sstevel@tonic-gate 	    "hubd_post_detach: port=%d result=%d", port, ds->result);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	/*
10457c478bd9Sstevel@tonic-gate 	 * if the device is successfully detached and is the
10467c478bd9Sstevel@tonic-gate 	 * last device to detach, mark component as idle
10477c478bd9Sstevel@tonic-gate 	 */
10487c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
10497c478bd9Sstevel@tonic-gate 	if (ds->result == DDI_SUCCESS) {
10507c478bd9Sstevel@tonic-gate 		usba_device_t	*usba_device = hubd->h_usba_devices[port];
105135f36846Ssl 		dev_info_t	*pdip = hubd->h_dip;
10527c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10537c478bd9Sstevel@tonic-gate 
105435f36846Ssl 		usba_hubdi_incr_power_budget(pdip, usba_device);
105535f36846Ssl 
10567c478bd9Sstevel@tonic-gate 		/*
10577c478bd9Sstevel@tonic-gate 		 * We set power of the detached child
10587c478bd9Sstevel@tonic-gate 		 * to 0, so that we can suspend if all
10597c478bd9Sstevel@tonic-gate 		 * our children are gone
10607c478bd9Sstevel@tonic-gate 		 */
10617c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_PWR_OFF);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		/* check for leaks on detaching */
10647c478bd9Sstevel@tonic-gate 		if ((usba_device) && (ds->cmd == DDI_DETACH)) {
10657c478bd9Sstevel@tonic-gate 			usba_check_for_leaks(usba_device);
10667c478bd9Sstevel@tonic-gate 		}
10677c478bd9Sstevel@tonic-gate 	} else {
10687c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate  * hubd_post_power
10757c478bd9Sstevel@tonic-gate  *	After the child's power entry point has been called
10767c478bd9Sstevel@tonic-gate  *	we record its power level in our local struct.
10777c478bd9Sstevel@tonic-gate  *	If the device has powered off, we suspend port
10787c478bd9Sstevel@tonic-gate  */
10797c478bd9Sstevel@tonic-gate static int
10807c478bd9Sstevel@tonic-gate hubd_post_power(hubd_t *hubd, usb_port_t port, pm_bp_child_pwrchg_t *bpc,
10817c478bd9Sstevel@tonic-gate     int result)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	int	retval = USB_SUCCESS;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
10867c478bd9Sstevel@tonic-gate 	    "hubd_post_power: port=%d", port);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	if (result == DDI_SUCCESS) {
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		/* record this power in our local struct */
10917c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, bpc->bpc_nlevel);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel == USB_DEV_OS_PWR_OFF) {
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 			/* now suspend the port */
10967c478bd9Sstevel@tonic-gate 			retval = hubd_suspend_port(hubd, port);
10977c478bd9Sstevel@tonic-gate 		} else if (bpc->bpc_nlevel == USB_DEV_OS_FULL_PWR) {
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 			/* make sure the port is resumed */
11007c478bd9Sstevel@tonic-gate 			retval = hubd_resume_port(hubd, port);
11017c478bd9Sstevel@tonic-gate 		}
11027c478bd9Sstevel@tonic-gate 	} else {
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 		/* record old power in our local struct */
11057c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, bpc->bpc_olevel);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		if (bpc->bpc_olevel == USB_DEV_OS_PWR_OFF) {
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 			/*
11107c478bd9Sstevel@tonic-gate 			 * As this device failed to transition from
11117c478bd9Sstevel@tonic-gate 			 * power off state, suspend the port again
11127c478bd9Sstevel@tonic-gate 			 */
11137c478bd9Sstevel@tonic-gate 			retval = hubd_suspend_port(hubd, port);
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	return (retval);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate  * bus ctl notifications are handled here, the rest goes up to root hub/hcd
11237c478bd9Sstevel@tonic-gate  */
11247c478bd9Sstevel@tonic-gate static int
11257c478bd9Sstevel@tonic-gate usba_hubdi_bus_ctl(dev_info_t *dip,
11267c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
11277c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t	op,
11287c478bd9Sstevel@tonic-gate 	void		*arg,
11297c478bd9Sstevel@tonic-gate 	void		*result)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
11327c478bd9Sstevel@tonic-gate 	dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
11337c478bd9Sstevel@tonic-gate 	struct attachspec *as;
11347c478bd9Sstevel@tonic-gate 	struct detachspec *ds;
11357c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
11367c478bd9Sstevel@tonic-gate 	usb_port_t	port;
11377c478bd9Sstevel@tonic-gate 	int		circ, rval;
11387c478bd9Sstevel@tonic-gate 	int		retval = DDI_FAILURE;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	/* flag that we are currently running bus_ctl */
11457c478bd9Sstevel@tonic-gate 	hubd->h_bus_ctls++;
11467c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
11497c478bd9Sstevel@tonic-gate 	    "usba_hubdi_bus_ctl:\n\t"
11507c478bd9Sstevel@tonic-gate 	    "dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p",
11517c478bd9Sstevel@tonic-gate 	    dip, rdip, op, arg);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	switch (op) {
11547c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
11557c478bd9Sstevel@tonic-gate 		as = (struct attachspec *)arg;
11567c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, rdip);
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 		/* there is nothing to do at resume time */
11597c478bd9Sstevel@tonic-gate 		if (as->cmd == DDI_RESUME) {
11607c478bd9Sstevel@tonic-gate 			break;
11617c478bd9Sstevel@tonic-gate 		}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 		/* serialize access */
11647c478bd9Sstevel@tonic-gate 		ndi_devi_enter(hubd->h_dip, &circ);
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		switch (as->when) {
11677c478bd9Sstevel@tonic-gate 		case DDI_PRE:
11687c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
11697c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d",
11707c478bd9Sstevel@tonic-gate 			    rdip, port);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
11737c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] |= HUBD_CHILD_ATTACHING;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 			/* Go busy here.  Matching idle is DDI_POST case. */
11767c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, dip, 0);
11777c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 			/*
11807c478bd9Sstevel@tonic-gate 			 * if we suspended the port previously
11817c478bd9Sstevel@tonic-gate 			 * because child went to low power state, and
11827c478bd9Sstevel@tonic-gate 			 * someone unloaded the driver, the port would
11837c478bd9Sstevel@tonic-gate 			 * still be suspended and needs to be resumed
11847c478bd9Sstevel@tonic-gate 			 */
11857c478bd9Sstevel@tonic-gate 			rval = hubd_resume_port(hubd, port);
11867c478bd9Sstevel@tonic-gate 			if (rval == USB_SUCCESS) {
11877c478bd9Sstevel@tonic-gate 				retval = DDI_SUCCESS;
11887c478bd9Sstevel@tonic-gate 			}
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 			break;
11917c478bd9Sstevel@tonic-gate 		case DDI_POST:
11927c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
11937c478bd9Sstevel@tonic-gate 			    "DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d",
11947c478bd9Sstevel@tonic-gate 			    rdip, port);
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
11977c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_ATTACHING;
11987c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 			hubd_post_attach(hubd, port, (struct attachspec *)arg);
12017c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12027c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 			/* Matching idle call for DDI_PRE busy call. */
12057c478bd9Sstevel@tonic-gate 			(void) hubd_pm_idle_component(hubd, dip, 0);
12067c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12077c478bd9Sstevel@tonic-gate 		}
12087c478bd9Sstevel@tonic-gate 		ndi_devi_exit(hubd->h_dip, circ);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 		break;
12117c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
12127c478bd9Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
12137c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, rdip);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		/* there is nothing to do at suspend time */
12167c478bd9Sstevel@tonic-gate 		if (ds->cmd == DDI_SUSPEND) {
12177c478bd9Sstevel@tonic-gate 			break;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		/* serialize access */
12217c478bd9Sstevel@tonic-gate 		ndi_devi_enter(hubd->h_dip, &circ);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 		switch (ds->when) {
12247c478bd9Sstevel@tonic-gate 		case DDI_PRE:
12257c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12267c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d",
12277c478bd9Sstevel@tonic-gate 			    rdip, port);
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12307c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] |= HUBD_CHILD_DETACHING;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			/* Go busy here.  Matching idle is DDI_POST case. */
12337c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, dip, 0);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12367c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 			break;
12397c478bd9Sstevel@tonic-gate 		case DDI_POST:
12407c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12417c478bd9Sstevel@tonic-gate 			    "DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d",
12427c478bd9Sstevel@tonic-gate 			    rdip, port);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12457c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_DETACHING;
12467c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 			/* Matching idle call for DDI_PRE busy call. */
12497c478bd9Sstevel@tonic-gate 			hubd_post_detach(hubd, port, (struct detachspec *)arg);
12507c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12517c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12527c478bd9Sstevel@tonic-gate 			(void) hubd_pm_idle_component(hubd, dip, 0);
12537c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 			break;
12567c478bd9Sstevel@tonic-gate 		}
12577c478bd9Sstevel@tonic-gate 		ndi_devi_exit(hubd->h_dip, circ);
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 		break;
12607c478bd9Sstevel@tonic-gate 	default:
12617c478bd9Sstevel@tonic-gate 		retval = usba_bus_ctl(root_hub_dip, rdip, op, arg, result);
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	/* decrement bus_ctls count */
12657c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
12667c478bd9Sstevel@tonic-gate 	hubd->h_bus_ctls--;
12677c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_bus_ctls >= 0);
12687c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	return (retval);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate /*
12757c478bd9Sstevel@tonic-gate  * bus enumeration entry points
12767c478bd9Sstevel@tonic-gate  */
12777c478bd9Sstevel@tonic-gate static int
12787c478bd9Sstevel@tonic-gate hubd_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
12797c478bd9Sstevel@tonic-gate     void *arg, dev_info_t **child)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	extern int modrootloaded;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = hubd_get_soft_state(dip);
12847c478bd9Sstevel@tonic-gate 	int	rval, circ;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12877c478bd9Sstevel@tonic-gate 	    "hubd_bus_config: op=%d", op);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	if (hubdi_bus_config_debug) {
12907c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	/*
12947c478bd9Sstevel@tonic-gate 	 * there must be a smarter way to do this but for
12957c478bd9Sstevel@tonic-gate 	 * now, a hack for booting USB storage.
12967c478bd9Sstevel@tonic-gate 	 */
12977c478bd9Sstevel@tonic-gate 	if (!modrootloaded) {
12987c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(1000000));
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 	ndi_devi_enter(hubd->h_dip, &circ);
13017c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
13027c478bd9Sstevel@tonic-gate 	ndi_devi_exit(hubd->h_dip, circ);
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	return (rval);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate static int
13097c478bd9Sstevel@tonic-gate hubd_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
13107c478bd9Sstevel@tonic-gate     void *arg)
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = hubd_get_soft_state(dip);
13137c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
13147c478bd9Sstevel@tonic-gate 	usb_port_t	port;
13157c478bd9Sstevel@tonic-gate 	int		circ;
13167c478bd9Sstevel@tonic-gate 	int		rval;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
13197c478bd9Sstevel@tonic-gate 	    "hubd_bus_unconfig: op=%d", op);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	if (hubdi_bus_config_debug) {
13227c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	if ((op == BUS_UNCONFIG_ALL) && (flag & NDI_AUTODETACH) == 0) {
13267c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_REMOVE;
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	/* serialize access */
13307c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	/* logically zap children's list */
13357c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
13367c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
13377c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= HUBD_CHILD_ZAP;
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	/* fill in what's left */
13427c478bd9Sstevel@tonic-gate 	for (cdip = ddi_get_child(dip); cdip;
13437c478bd9Sstevel@tonic-gate 	    cdip = ddi_get_next_sibling(cdip)) {
13447c478bd9Sstevel@tonic-gate 		usba_device_t *usba_device = usba_get_usba_device(cdip);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 		if (usba_device == NULL) {
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 			continue;
13497c478bd9Sstevel@tonic-gate 		}
13507c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
13517c478bd9Sstevel@tonic-gate 		port = usba_device->usb_port;
13527c478bd9Sstevel@tonic-gate 		hubd->h_children_dips[port] = cdip;
13537c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
13547c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
13557c478bd9Sstevel@tonic-gate 	}
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	/* physically zap the children we didn't find */
13587c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
13597c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
13607c478bd9Sstevel@tonic-gate 		if (hubd->h_port_state[port] &  HUBD_CHILD_ZAP) {
13617c478bd9Sstevel@tonic-gate 			/* zap the dip and usba_device structure as well */
13627c478bd9Sstevel@tonic-gate 			hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
13637c478bd9Sstevel@tonic-gate 			hubd->h_children_dips[port] = NULL;
13647c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
13727c478bd9Sstevel@tonic-gate 	    "hubd_bus_unconfig: rval=%d", rval);
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	return (rval);
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate /* bus_power entry point */
13797c478bd9Sstevel@tonic-gate static int
13807c478bd9Sstevel@tonic-gate hubd_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
13817c478bd9Sstevel@tonic-gate     void *arg, void *result)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
13847c478bd9Sstevel@tonic-gate 	int		rval, pwrup_res;
13857c478bd9Sstevel@tonic-gate 	usb_port_t	port;
13867c478bd9Sstevel@tonic-gate 	int		retval = DDI_FAILURE;
13877c478bd9Sstevel@tonic-gate 	pm_bp_child_pwrchg_t	*bpc;
13887c478bd9Sstevel@tonic-gate 	pm_bp_nexus_pwrup_t	bpn;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubd->h_log_handle,
13937c478bd9Sstevel@tonic-gate 	    "hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, "
13947c478bd9Sstevel@tonic-gate 	    "result=%d\n", dip, impl_arg, op, arg, *(int *)result);
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	bpc = (pm_bp_child_pwrchg_t *)arg;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
13997c478bd9Sstevel@tonic-gate 	hubd->h_bus_pwr++;
14007c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	switch (op) {
14037c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
14047c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, bpc->bpc_dip);
14057c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
14067c478bd9Sstevel@tonic-gate 		    "hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d",
14077c478bd9Sstevel@tonic-gate 		    port);
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 		/* go to full power if we are powered down */
14107c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		/*
14137c478bd9Sstevel@tonic-gate 		 * If this case completes normally, idle will be in
14147c478bd9Sstevel@tonic-gate 		 * hubd_bus_power / BUS_POWER_POST_NOTIFICATION
14157c478bd9Sstevel@tonic-gate 		 */
14167c478bd9Sstevel@tonic-gate 		hubd_pm_busy_component(hubd, dip, 0);
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 		/*
14197c478bd9Sstevel@tonic-gate 		 * raise power only if we have created the components
14207c478bd9Sstevel@tonic-gate 		 * and are currently in low power
14217c478bd9Sstevel@tonic-gate 		 */
14227c478bd9Sstevel@tonic-gate 		if ((hubd->h_dev_state == USB_DEV_PWRED_DOWN) &&
14237c478bd9Sstevel@tonic-gate 		    hubd->h_hubpm->hubp_wakeup_enabled) {
14247c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 			bpn.bpn_comp = 0;
14277c478bd9Sstevel@tonic-gate 			bpn.bpn_dip = dip;
14287c478bd9Sstevel@tonic-gate 			bpn.bpn_level = USB_DEV_OS_FULL_PWR;
14297c478bd9Sstevel@tonic-gate 			bpn.bpn_private = bpc->bpc_private;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 			rval = pm_busop_bus_power(dip, impl_arg,
14327c478bd9Sstevel@tonic-gate 			    BUS_POWER_NEXUS_PWRUP, (void *)&bpn,
14337c478bd9Sstevel@tonic-gate 			    (void *)&pwrup_res);
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 			if (rval != DDI_SUCCESS || pwrup_res != DDI_SUCCESS) {
14367c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
14377c478bd9Sstevel@tonic-gate 				hubd_pm_idle_component(hubd, dip, 0);
14387c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 				break;
14417c478bd9Sstevel@tonic-gate 			}
14427c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
14437c478bd9Sstevel@tonic-gate 		}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 		/* indicate that child is changing power level */
14467c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= HUBD_CHILD_PWRLVL_CHNG;
14477c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 		if ((bpc->bpc_olevel == 0) &&
14507c478bd9Sstevel@tonic-gate 		    (bpc->bpc_nlevel > bpc->bpc_olevel)) {
14517c478bd9Sstevel@tonic-gate 			/*
14527c478bd9Sstevel@tonic-gate 			 * this child is transitioning from power off
14537c478bd9Sstevel@tonic-gate 			 * to power on state - resume port
14547c478bd9Sstevel@tonic-gate 			 */
14557c478bd9Sstevel@tonic-gate 			rval = hubd_resume_port(hubd, port);
14567c478bd9Sstevel@tonic-gate 			if (rval == USB_SUCCESS) {
14577c478bd9Sstevel@tonic-gate 				retval = DDI_SUCCESS;
14587c478bd9Sstevel@tonic-gate 			} else {
14597c478bd9Sstevel@tonic-gate 				/* reset this flag on failure */
14607c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
14617c478bd9Sstevel@tonic-gate 				hubd->h_port_state[port] &=
14627c478bd9Sstevel@tonic-gate 				    ~HUBD_CHILD_PWRLVL_CHNG;
14637c478bd9Sstevel@tonic-gate 				hubd_pm_idle_component(hubd, dip, 0);
14647c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
14657c478bd9Sstevel@tonic-gate 			}
14667c478bd9Sstevel@tonic-gate 		} else {
14677c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
14687c478bd9Sstevel@tonic-gate 		}
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 		break;
14717c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
14727c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, bpc->bpc_dip);
14737c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
14747c478bd9Sstevel@tonic-gate 		    "hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d",
14757c478bd9Sstevel@tonic-gate 		    port);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
14787c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~HUBD_CHILD_PWRLVL_CHNG;
14797c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 		/* record child's pwr and suspend port if required */
14827c478bd9Sstevel@tonic-gate 		rval = hubd_post_power(hubd, port, bpc, *(int *)result);
14837c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 		/*
14917c478bd9Sstevel@tonic-gate 		 * Matching idle for the busy in
14927c478bd9Sstevel@tonic-gate 		 * hubd_bus_power / BUS_POWER_PRE_NOTIFICATION
14937c478bd9Sstevel@tonic-gate 		 */
14947c478bd9Sstevel@tonic-gate 		hubd_pm_idle_component(hubd, dip, 0);
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		break;
14997c478bd9Sstevel@tonic-gate 	default:
15007c478bd9Sstevel@tonic-gate 		retval = pm_busop_bus_power(dip, impl_arg, op, arg, result);
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 		break;
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
15067c478bd9Sstevel@tonic-gate 	hubd->h_bus_pwr--;
15077c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	return (retval);
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate /*
15147c478bd9Sstevel@tonic-gate  * functions to handle power transition for OS levels 0 -> 3
15157c478bd9Sstevel@tonic-gate  */
15167c478bd9Sstevel@tonic-gate static int
15177c478bd9Sstevel@tonic-gate hubd_pwrlvl0(hubd_t *hubd)
15187c478bd9Sstevel@tonic-gate {
15197c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	/* We can't power down if hotplug thread is running */
15227c478bd9Sstevel@tonic-gate 	if (hubd->h_hotplug_thread || hubd->h_hubpm->hubp_busy_pm ||
15237c478bd9Sstevel@tonic-gate 	    (hubd_can_suspend(hubd) == USB_FAILURE)) {
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
15267c478bd9Sstevel@tonic-gate 	}
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
15297c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
15307c478bd9Sstevel@tonic-gate 		hubpm = hubd->h_hubpm;
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		/*
15337c478bd9Sstevel@tonic-gate 		 * To avoid race with bus_power pre_notify on check over
15347c478bd9Sstevel@tonic-gate 		 * dev_state, we need to correctly set the dev state
15357c478bd9Sstevel@tonic-gate 		 * before the mutex is dropped in stop polling.
15367c478bd9Sstevel@tonic-gate 		 */
15377c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_PWRED_DOWN;
15387c478bd9Sstevel@tonic-gate 		hubpm->hubp_current_power = USB_DEV_OS_PWR_OFF;
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 		/*
15417c478bd9Sstevel@tonic-gate 		 * if we are the root hub, do not stop polling
15427c478bd9Sstevel@tonic-gate 		 * otherwise, we will never see a resume
15437c478bd9Sstevel@tonic-gate 		 */
15447c478bd9Sstevel@tonic-gate 		if (usba_is_root_hub(hubd->h_dip)) {
15457c478bd9Sstevel@tonic-gate 			/* place holder to implement Global Suspend */
15467c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
15477c478bd9Sstevel@tonic-gate 			    "Global Suspend: Not Yet Implemented");
15487c478bd9Sstevel@tonic-gate 		} else {
15497c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 		/* Issue USB D3 command to the device here */
15537c478bd9Sstevel@tonic-gate 		(void) usb_set_device_pwrlvl3(hubd->h_dip);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 		break;
15567c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
15577c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
15587c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
15597c478bd9Sstevel@tonic-gate 	default:
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 		break;
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
15657c478bd9Sstevel@tonic-gate }
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate /* ARGSUSED */
15697c478bd9Sstevel@tonic-gate static int
15707c478bd9Sstevel@tonic-gate hubd_pwrlvl1(hubd_t *hubd)
15717c478bd9Sstevel@tonic-gate {
15727c478bd9Sstevel@tonic-gate 	/* Issue USB D2 command to the device here */
15737c478bd9Sstevel@tonic-gate 	(void) usb_set_device_pwrlvl2(hubd->h_dip);
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate /* ARGSUSED */
15807c478bd9Sstevel@tonic-gate static int
15817c478bd9Sstevel@tonic-gate hubd_pwrlvl2(hubd_t *hubd)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	/* Issue USB D1 command to the device here */
15847c478bd9Sstevel@tonic-gate 	(void) usb_set_device_pwrlvl1(hubd->h_dip);
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
15877c478bd9Sstevel@tonic-gate }
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate static int
15917c478bd9Sstevel@tonic-gate hubd_pwrlvl3(hubd_t *hubd)
15927c478bd9Sstevel@tonic-gate {
15937c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
15947c478bd9Sstevel@tonic-gate 	int		rval;
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, "hubd_pwrlvl3");
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
15997c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
16007c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
16017c478bd9Sstevel@tonic-gate 		ASSERT(hubpm->hubp_current_power == USB_DEV_OS_PWR_OFF);
16027c478bd9Sstevel@tonic-gate 		if (usba_is_root_hub(hubd->h_dip)) {
16037c478bd9Sstevel@tonic-gate 			/* implement global resume here */
16047c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM,
16057c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
16067c478bd9Sstevel@tonic-gate 			    "Global Resume: Not Yet Implemented");
16077c478bd9Sstevel@tonic-gate 		}
16087c478bd9Sstevel@tonic-gate 		/* Issue USB D0 command to the device here */
16097c478bd9Sstevel@tonic-gate 		rval = usb_set_device_pwrlvl0(hubd->h_dip);
16107c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
16117c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_ONLINE;
16127c478bd9Sstevel@tonic-gate 		hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
16137c478bd9Sstevel@tonic-gate 		hubpm->hubp_time_at_full_power = ddi_get_time();
16147c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
16177c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
16187c478bd9Sstevel@tonic-gate 		/* we are already in full power */
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
16217c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
16227c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
16237c478bd9Sstevel@tonic-gate 		/*
16247c478bd9Sstevel@tonic-gate 		 * PM framework tries to put you in full power
16257c478bd9Sstevel@tonic-gate 		 * during system shutdown. If we are disconnected
16267c478bd9Sstevel@tonic-gate 		 * return success. Also, we should not change state
16277c478bd9Sstevel@tonic-gate 		 * when we are disconnected or suspended or about to
16287c478bd9Sstevel@tonic-gate 		 * transition to that state
16297c478bd9Sstevel@tonic-gate 		 */
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
16327c478bd9Sstevel@tonic-gate 	default:
16337c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
16347c478bd9Sstevel@tonic-gate 		    "hubd_pwrlvl3: Illegal dev_state=%d", hubd->h_dev_state);
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate /* power entry point */
16427c478bd9Sstevel@tonic-gate /* ARGSUSED */
16437c478bd9Sstevel@tonic-gate int
16447c478bd9Sstevel@tonic-gate usba_hubdi_power(dev_info_t *dip, int comp, int level)
16457c478bd9Sstevel@tonic-gate {
16467c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
16477c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
16487c478bd9Sstevel@tonic-gate 	int		retval;
16497c478bd9Sstevel@tonic-gate 	int		circ;
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
16527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
16537c478bd9Sstevel@tonic-gate 	    "usba_hubdi_power: level=%d", level);
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
16587c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	/* check if we are transitioning to a legal power level */
16617c478bd9Sstevel@tonic-gate 	if (USB_DEV_PWRSTATE_OK(hubpm->hubp_pwr_states, level)) {
16627c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HUBDI, hubd->h_log_handle,
16637c478bd9Sstevel@tonic-gate 		    "usba_hubdi_power: illegal power level=%d "
16647c478bd9Sstevel@tonic-gate 		    "hubp_pwr_states=0x%x", level, hubpm->hubp_pwr_states);
16657c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 		ndi_devi_exit(dip, circ);
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	switch (level) {
16737c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_OFF:
16747c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl0(hubd);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		break;
16777c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_1:
16787c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl1(hubd);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 		break;
16817c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_2:
16827c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl2(hubd);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 		break;
16857c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_FULL_PWR:
16867c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl3(hubd);
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		break;
16897c478bd9Sstevel@tonic-gate 	}
16907c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate /* power entry point for the root hub */
16997c478bd9Sstevel@tonic-gate int
17007c478bd9Sstevel@tonic-gate usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	return (usba_hubdi_power(dip, comp, level));
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate /*
17077c478bd9Sstevel@tonic-gate  * standard driver entry points support code
17087c478bd9Sstevel@tonic-gate  */
17097c478bd9Sstevel@tonic-gate int
17107c478bd9Sstevel@tonic-gate usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
17117c478bd9Sstevel@tonic-gate {
17127c478bd9Sstevel@tonic-gate 	int			instance = ddi_get_instance(dip);
17137c478bd9Sstevel@tonic-gate 	hubd_t			*hubd = NULL;
17147c478bd9Sstevel@tonic-gate 	int			i, rval;
17157c478bd9Sstevel@tonic-gate 	int			minor;
17167c478bd9Sstevel@tonic-gate 	char			*log_name = NULL;
17177c478bd9Sstevel@tonic-gate 	const char		*root_hub_drvname;
17187c478bd9Sstevel@tonic-gate 	usb_ep_data_t		*ep_data;
17197c478bd9Sstevel@tonic-gate 	usba_device_t		*child_ud = NULL;
17207c478bd9Sstevel@tonic-gate 	usb_dev_descr_t		*usb_dev_descr;
17217c478bd9Sstevel@tonic-gate 	usb_port_status_t	parent_port_status, child_port_status;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle,
17247c478bd9Sstevel@tonic-gate 		"hubd_attach instance %d, cmd=0x%x", instance, cmd);
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	switch (cmd) {
17277c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 		break;
17307c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
17317c478bd9Sstevel@tonic-gate 		hubd_cpr_resume(dip);
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
17347c478bd9Sstevel@tonic-gate 	default:
17357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
17367c478bd9Sstevel@tonic-gate 	}
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	/*
17397c478bd9Sstevel@tonic-gate 	 * Allocate softc information.
17407c478bd9Sstevel@tonic-gate 	 */
17417c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
17427c478bd9Sstevel@tonic-gate 		/* soft state has already been allocated */
17437c478bd9Sstevel@tonic-gate 		hubd = hubd_get_soft_state(dip);
17447c478bd9Sstevel@tonic-gate 		minor = HUBD_IS_ROOT_HUB;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 		/* generate readable labels for different root hubs */
17477c478bd9Sstevel@tonic-gate 		root_hub_drvname = ddi_driver_name(dip);
17487c478bd9Sstevel@tonic-gate 		if (strcmp(root_hub_drvname, "ehci") == 0) {
17497c478bd9Sstevel@tonic-gate 			log_name = "eusb";
17507c478bd9Sstevel@tonic-gate 		} else if (strcmp(root_hub_drvname, "uhci") == 0) {
17517c478bd9Sstevel@tonic-gate 			log_name = "uusb";
17527c478bd9Sstevel@tonic-gate 		} else {
17537c478bd9Sstevel@tonic-gate 			/* std. for ohci */
17547c478bd9Sstevel@tonic-gate 			log_name = "usb";
17557c478bd9Sstevel@tonic-gate 		}
17567c478bd9Sstevel@tonic-gate 	} else {
17577c478bd9Sstevel@tonic-gate 		rval = ddi_soft_state_zalloc(hubd_statep, instance);
17587c478bd9Sstevel@tonic-gate 		minor = 0;
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 		if (rval != DDI_SUCCESS) {
17617c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
17627c478bd9Sstevel@tonic-gate 			    "cannot allocate soft state (%d)", instance);
17637c478bd9Sstevel@tonic-gate 			goto fail;
17647c478bd9Sstevel@tonic-gate 		}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 		hubd = hubd_get_soft_state(dip);
17677c478bd9Sstevel@tonic-gate 		if (hubd == NULL) {
17687c478bd9Sstevel@tonic-gate 			goto fail;
17697c478bd9Sstevel@tonic-gate 		}
17707c478bd9Sstevel@tonic-gate 	}
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel,
17737c478bd9Sstevel@tonic-gate 				&hubd_errmask, &hubd_instance_debug, 0);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	hubd->h_usba_device	= child_ud = usba_get_usba_device(dip);
17767c478bd9Sstevel@tonic-gate 	hubd->h_dip		= dip;
17777c478bd9Sstevel@tonic-gate 	hubd->h_instance	= instance;
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
17807c478bd9Sstevel@tonic-gate 	child_port_status = child_ud->usb_port_status;
17817c478bd9Sstevel@tonic-gate 	usb_dev_descr = child_ud->usb_dev_descr;
17827c478bd9Sstevel@tonic-gate 	parent_port_status = (child_ud->usb_hs_hub_usba_dev) ?
17837c478bd9Sstevel@tonic-gate 	    child_ud->usb_hs_hub_usba_dev->usb_port_status : 0;
17847c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	if ((child_port_status == USBA_FULL_SPEED_DEV) &&
17877c478bd9Sstevel@tonic-gate 	    (parent_port_status == USBA_HIGH_SPEED_DEV) &&
17887c478bd9Sstevel@tonic-gate 	    (usb_dev_descr->bcdUSB == 0x100)) {
17897c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
17907c478bd9Sstevel@tonic-gate 		    "Use of a USB1.0 hub behind a high speed port may "
17917c478bd9Sstevel@tonic-gate 		    "cause unexpected failures");
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	hubd->h_pipe_policy.pp_max_async_reqs = 1;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	/* register with USBA as client driver */
17977c478bd9Sstevel@tonic-gate 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
17987c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
17997c478bd9Sstevel@tonic-gate 		    "client attach failed");
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 		goto fail;
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	if (usb_get_dev_data(dip, &hubd->h_dev_data,
18057c478bd9Sstevel@tonic-gate 	    USB_PARSE_LVL_IF, 0) != USB_SUCCESS) {
18067c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
18077c478bd9Sstevel@tonic-gate 		    "cannot get dev_data");
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 		goto fail;
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data,
18137c478bd9Sstevel@tonic-gate 	    hubd->h_dev_data->dev_curr_if, 0, 0,
18147c478bd9Sstevel@tonic-gate 	    (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) {
18157c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
18167c478bd9Sstevel@tonic-gate 		    "no interrupt IN endpoint found");
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 		goto fail;
18197c478bd9Sstevel@tonic-gate 	}
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	hubd->h_ep1_descr = ep_data->ep_descr;
18227c478bd9Sstevel@tonic-gate 	hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER,
18257c478bd9Sstevel@tonic-gate 				hubd->h_dev_data->dev_iblock_cookie);
18267c478bd9Sstevel@tonic-gate 	cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL);
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_LOCKS_DONE;
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	usb_free_descr_tree(dip, hubd->h_dev_data);
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	/*
18337c478bd9Sstevel@tonic-gate 	 * register this hub instance with usba
18347c478bd9Sstevel@tonic-gate 	 */
18357c478bd9Sstevel@tonic-gate 	rval = usba_hubdi_register(dip, 0);
18367c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
1837d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
18387c478bd9Sstevel@tonic-gate 		    "usba_hubdi_register failed");
18397c478bd9Sstevel@tonic-gate 		goto fail;
18407c478bd9Sstevel@tonic-gate 	}
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
18437c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_HUBDI_REGISTERED;
18447c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_ONLINE;
18457c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	/* now create components to power manage this device */
18487c478bd9Sstevel@tonic-gate 	hubd_create_pm_components(dip, hubd);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/*
18517c478bd9Sstevel@tonic-gate 	 * Event handling: definition and registration
18527c478bd9Sstevel@tonic-gate 	 *
18537c478bd9Sstevel@tonic-gate 	 * first the  definition:
18547c478bd9Sstevel@tonic-gate 	 * get event handle
18557c478bd9Sstevel@tonic-gate 	 */
18567c478bd9Sstevel@tonic-gate 	(void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP);
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	/* bind event set to the handle */
18597c478bd9Sstevel@tonic-gate 	if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events,
18607c478bd9Sstevel@tonic-gate 	    NDI_SLEEP)) {
18617c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
18627c478bd9Sstevel@tonic-gate 		    "binding event set failed");
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 		goto fail;
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	/* event registration */
18687c478bd9Sstevel@tonic-gate 	if (hubd_register_events(hubd) != USB_SUCCESS) {
1869d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
18707c478bd9Sstevel@tonic-gate 		    "hubd_register_events failed");
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 		goto fail;
18737c478bd9Sstevel@tonic-gate 	}
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
18767c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_EVENTS_REGISTERED;
18777c478bd9Sstevel@tonic-gate 
187835f36846Ssl 	if ((hubd_get_hub_descriptor(hubd)) != USB_SUCCESS) {
187935f36846Ssl 		mutex_exit(HUBD_MUTEX(hubd));
188035f36846Ssl 
188135f36846Ssl 		goto fail;
188235f36846Ssl 	}
188335f36846Ssl 
188435f36846Ssl 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
188535f36846Ssl 	    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
188635f36846Ssl 	    "hub-ignore-power-budget") == 1) {
188735f36846Ssl 		hubd->h_ignore_pwr_budget = B_TRUE;
188835f36846Ssl 	} else {
188935f36846Ssl 		hubd->h_ignore_pwr_budget = B_FALSE;
189035f36846Ssl 
189135f36846Ssl 		/* initialize hub power budget variables */
189235f36846Ssl 		if (hubd_init_power_budget(hubd) != USB_SUCCESS) {
189335f36846Ssl 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
189435f36846Ssl 			    "hubd_init_power_budget failed");
189535f36846Ssl 			mutex_exit(HUBD_MUTEX(hubd));
189635f36846Ssl 
189735f36846Ssl 			goto fail;
189835f36846Ssl 		}
189935f36846Ssl 	}
190035f36846Ssl 
19017c478bd9Sstevel@tonic-gate 	/* initialize and create children */
19027c478bd9Sstevel@tonic-gate 	if (hubd_check_ports(hubd) != USB_SUCCESS) {
1903d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19047c478bd9Sstevel@tonic-gate 		    "hubd_check_ports failed");
19057c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 		goto fail;
19087c478bd9Sstevel@tonic-gate 	}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	/*
19117c478bd9Sstevel@tonic-gate 	 * create cfgadm nodes
19127c478bd9Sstevel@tonic-gate 	 */
19137c478bd9Sstevel@tonic-gate 	hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP);
19147c478bd9Sstevel@tonic-gate 	hubd_get_ancestry_str(hubd);
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
19177c478bd9Sstevel@tonic-gate 	    "#ports=0x%x", hubd->h_hub_descr.bNbrPorts);
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	for (i = 1; i <= hubd->h_hub_descr.bNbrPorts; i++) {
19207c478bd9Sstevel@tonic-gate 		char ap_name[HUBD_APID_NAMELEN];
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 		(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
19237c478bd9Sstevel@tonic-gate 		    hubd->h_ancestry_str, i);
19247c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
19257c478bd9Sstevel@tonic-gate 		    "ap_name=%s", ap_name);
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance,
19287c478bd9Sstevel@tonic-gate 		    DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
1929d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19307c478bd9Sstevel@tonic-gate 			    "cannot create attachment point node (%d)",
19317c478bd9Sstevel@tonic-gate 			    instance);
19327c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 			goto fail;
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	/* create minor nodes */
19407c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "hubd", S_IFCHR,
19417c478bd9Sstevel@tonic-gate 	    instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
19427c478bd9Sstevel@tonic-gate 
1943d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19447c478bd9Sstevel@tonic-gate 		    "cannot create devctl minor node (%d)", instance);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 		goto fail;
19477c478bd9Sstevel@tonic-gate 	}
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
19507c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_MINOR_NODE_CREATED;
19517c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * host controller driver has already reported this dev
19557c478bd9Sstevel@tonic-gate 	 * if we are the root hub
19567c478bd9Sstevel@tonic-gate 	 */
19577c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
19587c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	/* enable deathrow thread */
19627c478bd9Sstevel@tonic-gate 	hubd->h_cleanup_enabled = B_TRUE;
19637c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
19647c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, dip, 0);
19657c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate fail:
19707c478bd9Sstevel@tonic-gate 	{
19717c478bd9Sstevel@tonic-gate 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubdi_log_handle,
19747c478bd9Sstevel@tonic-gate 		    "cannot attach %s", ddi_pathname(dip, pathname));
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
19807c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, dip, 0);
19817c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	if (hubd) {
19847c478bd9Sstevel@tonic-gate 		rval = hubd_cleanup(dip, hubd);
19857c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
1986d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
19877c478bd9Sstevel@tonic-gate 			    "failure to complete cleanup after attach failure");
19887c478bd9Sstevel@tonic-gate 		}
19897c478bd9Sstevel@tonic-gate 	}
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
19927c478bd9Sstevel@tonic-gate }
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate int
19967c478bd9Sstevel@tonic-gate usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
19977c478bd9Sstevel@tonic-gate {
19987c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = hubd_get_soft_state(dip);
19997c478bd9Sstevel@tonic-gate 	int	rval;
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
20027c478bd9Sstevel@tonic-gate 	    "hubd_detach: cmd=0x%x", cmd);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	switch (cmd) {
20057c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
20067c478bd9Sstevel@tonic-gate 		rval = hubd_cleanup(dip, hubd);
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
20097c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
20107c478bd9Sstevel@tonic-gate 		rval = hubd_cpr_suspend(hubd);
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
20137c478bd9Sstevel@tonic-gate 	default:
20147c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate /*
20207c478bd9Sstevel@tonic-gate  * hubd_setdevaddr
20217c478bd9Sstevel@tonic-gate  *	set the device addrs on this port
20227c478bd9Sstevel@tonic-gate  */
20237c478bd9Sstevel@tonic-gate static int
20247c478bd9Sstevel@tonic-gate hubd_setdevaddr(hubd_t *hubd, usb_port_t port)
20257c478bd9Sstevel@tonic-gate {
20267c478bd9Sstevel@tonic-gate 	int		rval;
20277c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
20287c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
20297c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t ph;
20307c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip = NULL;
20317c478bd9Sstevel@tonic-gate 	uchar_t		address = 0;
20327c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
20337c478bd9Sstevel@tonic-gate 	int		retry = 0;
20347c478bd9Sstevel@tonic-gate 	long		time_delay;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
20377c478bd9Sstevel@tonic-gate 	    "hubd_setdevaddr: port=%d", port);
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
20427c478bd9Sstevel@tonic-gate 	address = hubd->h_usba_devices[port]->usb_addr;
20437c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	/* close the default pipe with addr x */
20467c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
20477c478bd9Sstevel@tonic-gate 	ph = usba_get_dflt_pipe_handle(child_dip);
20487c478bd9Sstevel@tonic-gate 	usb_pipe_close(child_dip, ph,
20497c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
20507c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	/*
20537c478bd9Sstevel@tonic-gate 	 * As this device has been reset, temporarily
20547c478bd9Sstevel@tonic-gate 	 * assign the default address
20557c478bd9Sstevel@tonic-gate 	 */
20567c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
20577c478bd9Sstevel@tonic-gate 	address = usba_device->usb_addr;
20587c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = USBA_DEFAULT_ADDR;
20597c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	time_delay = drv_usectohz(hubd_device_delay / 20);
20647c478bd9Sstevel@tonic-gate 	for (retry = 0; retry < hubd_retry_enumerate; retry++) {
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 		/* open child's default pipe with USBA_DEFAULT_ADDR */
20677c478bd9Sstevel@tonic-gate 		if (usb_pipe_open(child_dip, NULL, NULL,
20687c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) !=
20697c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
20707c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
20717c478bd9Sstevel@tonic-gate 			    "hubd_setdevaddr: Unable to open default pipe");
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 			break;
20747c478bd9Sstevel@tonic-gate 		}
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 		/* Set the address of the device */
20777c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
20787c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_HOST_TO_DEV,
20797c478bd9Sstevel@tonic-gate 		    USB_REQ_SET_ADDRESS,	/* bRequest */
20807c478bd9Sstevel@tonic-gate 		    address,			/* wValue */
20817c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
20827c478bd9Sstevel@tonic-gate 		    0,				/* wLength */
20837c478bd9Sstevel@tonic-gate 		    NULL, 0,
20847c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
20857c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
20867c478bd9Sstevel@tonic-gate 			    "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x",
20877c478bd9Sstevel@tonic-gate 			    retry, rval, completion_reason, cb_flags);
20887c478bd9Sstevel@tonic-gate 		}
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 		usb_pipe_close(child_dip, ph,
20917c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 			break;
20967c478bd9Sstevel@tonic-gate 		}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 		delay(time_delay);
20997c478bd9Sstevel@tonic-gate 	}
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	/* Reset to the old address */
21027c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
21037c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = address;
21047c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
21057c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	usba_clear_data_toggle(usba_device);
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	return (rval);
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate /*
21147c478bd9Sstevel@tonic-gate  * hubd_setdevconfig
21157c478bd9Sstevel@tonic-gate  *	set the device addrs on this port
21167c478bd9Sstevel@tonic-gate  */
21177c478bd9Sstevel@tonic-gate static void
21187c478bd9Sstevel@tonic-gate hubd_setdevconfig(hubd_t *hubd, usb_port_t port)
21197c478bd9Sstevel@tonic-gate {
21207c478bd9Sstevel@tonic-gate 	int			rval;
21217c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
21227c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
21237c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	ph;
21247c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
21257c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device = NULL;
21267c478bd9Sstevel@tonic-gate 	uint16_t		config_value;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
21297c478bd9Sstevel@tonic-gate 	    "hubd_setdevconfig: port=%d", port);
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
21347c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
21357c478bd9Sstevel@tonic-gate 	config_value = hubd->h_usba_devices[port]->usb_cfg_value;
21367c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	/* open the default control pipe */
21397c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
21407c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) ==
21417c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 		/* Set the default configuration of the device */
21447c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
21457c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_HOST_TO_DEV,
21467c478bd9Sstevel@tonic-gate 		    USB_REQ_SET_CFG,		/* bRequest */
21477c478bd9Sstevel@tonic-gate 		    config_value,		/* wValue */
21487c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
21497c478bd9Sstevel@tonic-gate 		    0,				/* wLength */
21507c478bd9Sstevel@tonic-gate 		    NULL, 0,
21517c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
21527c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
21537c478bd9Sstevel@tonic-gate 			    "hubd_setdevconfig: set device config failed: "
21547c478bd9Sstevel@tonic-gate 			    "cr=%d cb_fl=0x%x rval=%d",
21557c478bd9Sstevel@tonic-gate 			    completion_reason, cb_flags, rval);
21567c478bd9Sstevel@tonic-gate 		}
21577c478bd9Sstevel@tonic-gate 		/*
21587c478bd9Sstevel@tonic-gate 		 * After setting the configuration, we make this default
21597c478bd9Sstevel@tonic-gate 		 * control pipe persistent, so that it gets re-opened
21607c478bd9Sstevel@tonic-gate 		 * on posting a connect event
21617c478bd9Sstevel@tonic-gate 		 */
21627c478bd9Sstevel@tonic-gate 		usba_persistent_pipe_close(usba_device);
21637c478bd9Sstevel@tonic-gate 	} else {
21647c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
21657c478bd9Sstevel@tonic-gate 		    "pipe open fails: rval=%d", rval);
21667c478bd9Sstevel@tonic-gate 	}
21677c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21727c478bd9Sstevel@tonic-gate static int
21737c478bd9Sstevel@tonic-gate hubd_check_disconnected_ports(dev_info_t *dip, void *arg)
21747c478bd9Sstevel@tonic-gate {
21757c478bd9Sstevel@tonic-gate 	int circ;
21767c478bd9Sstevel@tonic-gate 	usb_port_t port;
21777c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
21787c478bd9Sstevel@tonic-gate 	major_t hub_major = ddi_name_to_major("hubd");
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	/*
21817c478bd9Sstevel@tonic-gate 	 * make sure dip is a usb hub, major of root hub is HCD
21827c478bd9Sstevel@tonic-gate 	 * major
21837c478bd9Sstevel@tonic-gate 	 */
21847c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
21857c478bd9Sstevel@tonic-gate 		if ((ddi_driver_major(dip) != hub_major) ||
2186737d277aScth 		    !i_ddi_devi_attached(dip)) {
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 			return (DDI_WALK_PRUNECHILD);
21897c478bd9Sstevel@tonic-gate 		}
21907c478bd9Sstevel@tonic-gate 	}
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
21937c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
21967c478bd9Sstevel@tonic-gate 	}
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	/* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */
21997c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
22027c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
22037c478bd9Sstevel@tonic-gate 		dev_info_t *cdip = hubd->h_children_dips[port];
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 		if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) {
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 			continue;
22087c478bd9Sstevel@tonic-gate 		}
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 		(void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE, B_TRUE);
22117c478bd9Sstevel@tonic-gate 	}
22127c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
22137c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	/* skip siblings of root hub */
22167c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNESIB);
22197c478bd9Sstevel@tonic-gate 	}
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
22227c478bd9Sstevel@tonic-gate }
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate /*
22267c478bd9Sstevel@tonic-gate  * this thread will walk all children under the root hub for this
22277c478bd9Sstevel@tonic-gate  * USB bus instance and attempt to remove them
22287c478bd9Sstevel@tonic-gate  */
22297c478bd9Sstevel@tonic-gate static void
22307c478bd9Sstevel@tonic-gate hubd_root_hub_cleanup_thread(void *arg)
22317c478bd9Sstevel@tonic-gate {
22327c478bd9Sstevel@tonic-gate 	int circ;
22337c478bd9Sstevel@tonic-gate 	hubd_t *root_hubd = (hubd_t *)arg;
22347c478bd9Sstevel@tonic-gate 	dev_info_t *rh_dip = root_hubd->h_dip;
22357c478bd9Sstevel@tonic-gate #ifndef __lock_lint
22367c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr,
22397c478bd9Sstevel@tonic-gate 	    "USB root hub");
22407c478bd9Sstevel@tonic-gate #endif
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	for (;;) {
22437c478bd9Sstevel@tonic-gate 		/* don't race with detach */
22447c478bd9Sstevel@tonic-gate 		ndi_hold_devi(rh_dip);
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
22477c478bd9Sstevel@tonic-gate 		root_hubd->h_cleanup_needed = 0;
22487c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 		(void) devfs_clean(rh_dip, NULL, 0);
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 		ndi_devi_enter(ddi_get_parent(rh_dip), &circ);
22537c478bd9Sstevel@tonic-gate 		ddi_walk_devs(rh_dip, hubd_check_disconnected_ports,
22547c478bd9Sstevel@tonic-gate 							NULL);
22557c478bd9Sstevel@tonic-gate #ifdef __lock_lint
22567c478bd9Sstevel@tonic-gate 		(void) hubd_check_disconnected_ports(rh_dip, NULL);
22577c478bd9Sstevel@tonic-gate #endif
22587c478bd9Sstevel@tonic-gate 		ndi_devi_exit(ddi_get_parent(rh_dip), circ);
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 		/* quit if we are not enabled anymore */
22617c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
22627c478bd9Sstevel@tonic-gate 		if ((root_hubd->h_cleanup_enabled == B_FALSE) ||
22637c478bd9Sstevel@tonic-gate 		    (root_hubd->h_cleanup_needed == B_FALSE)) {
22647c478bd9Sstevel@tonic-gate 			root_hubd->h_cleanup_active = B_FALSE;
22657c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(root_hubd));
22667c478bd9Sstevel@tonic-gate 			ndi_rele_devi(rh_dip);
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 			break;
22697c478bd9Sstevel@tonic-gate 		}
22707c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
22717c478bd9Sstevel@tonic-gate 		ndi_rele_devi(rh_dip);
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate #ifndef __lock_lint
22747c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
22757c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
22767c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(hubd_dip_cleanup_delay));
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
22817c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd));
22827c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
22837c478bd9Sstevel@tonic-gate #endif
22847c478bd9Sstevel@tonic-gate 	}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate #ifndef __lock_lint
22877c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(root_hubd));
22887c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
22897c478bd9Sstevel@tonic-gate #endif
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate static void
22947c478bd9Sstevel@tonic-gate hubd_schedule_cleanup(dev_info_t *rh_dip)
22957c478bd9Sstevel@tonic-gate {
22967c478bd9Sstevel@tonic-gate 	hubd_t	*root_hubd = (hubd_t *)hubd_get_soft_state(rh_dip);
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(root_hubd));
22997c478bd9Sstevel@tonic-gate 	root_hubd->h_cleanup_needed = B_TRUE;
23007c478bd9Sstevel@tonic-gate 	if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) {
23017c478bd9Sstevel@tonic-gate 		root_hubd->h_cleanup_active = B_TRUE;
23027c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
23037c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, 0,
23047c478bd9Sstevel@tonic-gate 		    hubd_root_hub_cleanup_thread,
23057c478bd9Sstevel@tonic-gate 		    (void *)root_hubd, 0, &p0, TS_RUN,
23067c478bd9Sstevel@tonic-gate 		    minclsyspri);
23077c478bd9Sstevel@tonic-gate 	} else {
23087c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
23097c478bd9Sstevel@tonic-gate 	}
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate /*
23147c478bd9Sstevel@tonic-gate  * hubd_restore_device_state:
23157c478bd9Sstevel@tonic-gate  *	- set config for the hub
23167c478bd9Sstevel@tonic-gate  *	- power cycle all the ports
23177c478bd9Sstevel@tonic-gate  *	- for each port that was connected
23187c478bd9Sstevel@tonic-gate  *		- reset port
23197c478bd9Sstevel@tonic-gate  *		- assign addrs to the device on this port
23207c478bd9Sstevel@tonic-gate  *	- restart polling
23217c478bd9Sstevel@tonic-gate  *	- reset suspend flag
23227c478bd9Sstevel@tonic-gate  */
23237c478bd9Sstevel@tonic-gate static void
23247c478bd9Sstevel@tonic-gate hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd)
23257c478bd9Sstevel@tonic-gate {
23267c478bd9Sstevel@tonic-gate 	int		rval;
23277c478bd9Sstevel@tonic-gate 	int		retry;
23287c478bd9Sstevel@tonic-gate 	uint_t		hub_prev_state;
23297c478bd9Sstevel@tonic-gate 	usb_port_t	port;
23307c478bd9Sstevel@tonic-gate 	uint16_t	status;
23317c478bd9Sstevel@tonic-gate 	uint16_t	change;
23327c478bd9Sstevel@tonic-gate 	dev_info_t	*ch_dip;
23337c478bd9Sstevel@tonic-gate 	boolean_t	ehci_root_hub;
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
23367c478bd9Sstevel@tonic-gate 	    "hubd_restore_device_state:");
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
23397c478bd9Sstevel@tonic-gate 	hub_prev_state = hubd->h_dev_state;
23407c478bd9Sstevel@tonic-gate 	ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN);
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	/* First bring the device to full power */
23437c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, dip, 0);
23447c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip) &&
23497c478bd9Sstevel@tonic-gate 	    (usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0,
23507c478bd9Sstevel@tonic-gate 	    DPRINT_MASK_HOTPLUG,
23517c478bd9Sstevel@tonic-gate 	    USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) {
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 		/* change the device state to disconnected */
23547c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
23557c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
23567c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
23577c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 		return;
23607c478bd9Sstevel@tonic-gate 	}
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0);
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
23657c478bd9Sstevel@tonic-gate 	/* First turn off all port power */
23667c478bd9Sstevel@tonic-gate 	rval = hubd_disable_all_port_power(hubd);
23677c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
23687c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
23697c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state:"
23707c478bd9Sstevel@tonic-gate 		    "turning off port power failed");
23717c478bd9Sstevel@tonic-gate 	}
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	/* Settling time before turning on again */
23747c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
23757c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(hubd_device_delay / 100));
23767c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	/* enable power on all ports so we can see connects */
23797c478bd9Sstevel@tonic-gate 	if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) {
23807c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
23817c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state: turn on port power failed");
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
23847c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
23877c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 		return;
23907c478bd9Sstevel@tonic-gate 	}
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	/*
23937c478bd9Sstevel@tonic-gate 	 * wait at least 3 frames before accessing devices
23947c478bd9Sstevel@tonic-gate 	 * (note that delay's minimal time is one clock tick which
23957c478bd9Sstevel@tonic-gate 	 * is 10ms unless hires_tick has been changed)
23967c478bd9Sstevel@tonic-gate 	 */
23977c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
23987c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
23997c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER;
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
24047c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
24057c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state: port=%d", port);
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 		/*
24087c478bd9Sstevel@tonic-gate 		 * the childen_dips list may have dips that have been
24097c478bd9Sstevel@tonic-gate 		 * already deallocated. we only get a post_detach notification
24107c478bd9Sstevel@tonic-gate 		 * but not a destroy notification
24117c478bd9Sstevel@tonic-gate 		 */
24127c478bd9Sstevel@tonic-gate 		ch_dip = hubd->h_children_dips[port];
24137c478bd9Sstevel@tonic-gate 		if (ch_dip) {
24147c478bd9Sstevel@tonic-gate 			/* get port status */
24157c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
24167c478bd9Sstevel@tonic-gate 			    &status, &change, PORT_CHANGE_CSC);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 			/* check if it is truly connected */
24197c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
24207c478bd9Sstevel@tonic-gate 				/*
24217c478bd9Sstevel@tonic-gate 				 * Now reset port and assign the device
24227c478bd9Sstevel@tonic-gate 				 * its original address
24237c478bd9Sstevel@tonic-gate 				 */
24247c478bd9Sstevel@tonic-gate 				retry = 0;
24257c478bd9Sstevel@tonic-gate 				do {
24267c478bd9Sstevel@tonic-gate 					(void) hubd_reset_port(hubd, port);
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 					/* required for ppx */
24297c478bd9Sstevel@tonic-gate 					(void) hubd_enable_port(hubd, port);
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 					if (retry) {
24327c478bd9Sstevel@tonic-gate 						mutex_exit(HUBD_MUTEX(hubd));
24337c478bd9Sstevel@tonic-gate 						delay(drv_usectohz(
24347c478bd9Sstevel@tonic-gate 							hubd_device_delay/2));
24357c478bd9Sstevel@tonic-gate 						mutex_enter(HUBD_MUTEX(hubd));
24367c478bd9Sstevel@tonic-gate 					}
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 					rval = hubd_setdevaddr(hubd, port);
24397c478bd9Sstevel@tonic-gate 					retry++;
24407c478bd9Sstevel@tonic-gate 				} while ((rval != USB_SUCCESS) &&
24417c478bd9Sstevel@tonic-gate 				    (retry < hubd_retry_enumerate));
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 				hubd_setdevconfig(hubd, port);
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 				if (hub_prev_state == USB_DEV_DISCONNECTED) {
24467c478bd9Sstevel@tonic-gate 					/* post a connect event */
24477c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
24487c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
24497c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_INSERTION);
24507c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
24517c478bd9Sstevel@tonic-gate 				} else {
24527c478bd9Sstevel@tonic-gate 					/*
24537c478bd9Sstevel@tonic-gate 					 * Since we have this device connected
24547c478bd9Sstevel@tonic-gate 					 * mark it reinserted to prevent
24557c478bd9Sstevel@tonic-gate 					 * cleanup thread from stepping in.
24567c478bd9Sstevel@tonic-gate 					 */
24577c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
245816747f41Scth 					mutex_enter(&(DEVI(ch_dip)->devi_lock));
245916747f41Scth 					DEVI_SET_DEVICE_REINSERTED(ch_dip);
246016747f41Scth 					mutex_exit(&(DEVI(ch_dip)->devi_lock));
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 					/*
24637c478bd9Sstevel@tonic-gate 					 * reopen pipes for children for
24647c478bd9Sstevel@tonic-gate 					 * their DDI_RESUME
24657c478bd9Sstevel@tonic-gate 					 */
24667c478bd9Sstevel@tonic-gate 					rval = usba_persistent_pipe_open(
24677c478bd9Sstevel@tonic-gate 					    usba_get_usba_device(ch_dip));
24687c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
24697c478bd9Sstevel@tonic-gate 					ASSERT(rval == USB_SUCCESS);
24707c478bd9Sstevel@tonic-gate 				}
24717c478bd9Sstevel@tonic-gate 			} else {
24727c478bd9Sstevel@tonic-gate 				/*
24737c478bd9Sstevel@tonic-gate 				 * Mark this dip for deletion as the device
24747c478bd9Sstevel@tonic-gate 				 * is not physically present, and schedule
24757c478bd9Sstevel@tonic-gate 				 * cleanup thread upon post resume
24767c478bd9Sstevel@tonic-gate 				 */
24777c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
24807c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
24817c478bd9Sstevel@tonic-gate 				    "hubd_restore_device_state: "
24827c478bd9Sstevel@tonic-gate 				    "dip=%p on port=%d marked for cleanup",
24837c478bd9Sstevel@tonic-gate 				    ch_dip, port);
248416747f41Scth 				mutex_enter(&(DEVI(ch_dip)->devi_lock));
24857c478bd9Sstevel@tonic-gate 				DEVI_SET_DEVICE_REMOVED(ch_dip);
248616747f41Scth 				mutex_exit(&(DEVI(ch_dip)->devi_lock));
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
24897c478bd9Sstevel@tonic-gate 			}
24907c478bd9Sstevel@tonic-gate 		} else if (ehci_root_hub) {
24917c478bd9Sstevel@tonic-gate 			/* get port status */
24927c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
24937c478bd9Sstevel@tonic-gate 			    &status, &change, PORT_CHANGE_CSC);
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 			/* check if it is truly connected */
24967c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
24977c478bd9Sstevel@tonic-gate 				/*
24987c478bd9Sstevel@tonic-gate 				 * reset the port to find out if we have
24997c478bd9Sstevel@tonic-gate 				 * 2.0 device connected or 1.X. A 2.0
25007c478bd9Sstevel@tonic-gate 				 * device will still be seen as connected,
25017c478bd9Sstevel@tonic-gate 				 * while a 1.X device will switch over to
25027c478bd9Sstevel@tonic-gate 				 * the companion controller.
25037c478bd9Sstevel@tonic-gate 				 */
25047c478bd9Sstevel@tonic-gate 				(void) hubd_reset_port(hubd, port);
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 				(void) hubd_determine_port_status(hubd, port,
25077c478bd9Sstevel@tonic-gate 				    &status, &change, PORT_CHANGE_CSC);
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 				if (status &
25107c478bd9Sstevel@tonic-gate 				    (PORT_STATUS_CCS | PORT_STATUS_HSDA)) {
25117c478bd9Sstevel@tonic-gate 					/*
25127c478bd9Sstevel@tonic-gate 					 * We have a USB 2.0 device
25137c478bd9Sstevel@tonic-gate 					 * connected. Power cycle this port
25147c478bd9Sstevel@tonic-gate 					 * so that hotplug thread can
25157c478bd9Sstevel@tonic-gate 					 * enumerate this device.
25167c478bd9Sstevel@tonic-gate 					 */
25177c478bd9Sstevel@tonic-gate 					(void) hubd_toggle_port(hubd, port);
25187c478bd9Sstevel@tonic-gate 				} else {
25197c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_ATTA,
25207c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
25217c478bd9Sstevel@tonic-gate 					    "hubd_restore_device_state: "
25227c478bd9Sstevel@tonic-gate 					    "device on port %d switched over",
25237c478bd9Sstevel@tonic-gate 					    port);
25247c478bd9Sstevel@tonic-gate 				}
25257c478bd9Sstevel@tonic-gate 			}
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 		}
25287c478bd9Sstevel@tonic-gate 	}
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	/* if the device had remote wakeup earlier, enable it again */
25327c478bd9Sstevel@tonic-gate 	if (hubd->h_hubpm->hubp_wakeup_enabled) {
25337c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
25347c478bd9Sstevel@tonic-gate 		(void) usb_handle_remote_wakeup(hubd->h_dip,
25357c478bd9Sstevel@tonic-gate 		    USB_REMOTE_WAKEUP_ENABLE);
25367c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
25377c478bd9Sstevel@tonic-gate 	}
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_ONLINE;
25407c478bd9Sstevel@tonic-gate 	hubd_start_polling(hubd, 0);
25417c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, dip, 0);
25427c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
25437c478bd9Sstevel@tonic-gate }
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate /*
25477c478bd9Sstevel@tonic-gate  * hubd_cleanup:
25487c478bd9Sstevel@tonic-gate  *	cleanup hubd and deallocate. this function is called for
25497c478bd9Sstevel@tonic-gate  *	handling attach failures and detaching including dynamic
25507c478bd9Sstevel@tonic-gate  *	reconfiguration. If called from attaching, it must clean
25517c478bd9Sstevel@tonic-gate  *	up the whole thing and return success.
25527c478bd9Sstevel@tonic-gate  */
25537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25547c478bd9Sstevel@tonic-gate static int
25557c478bd9Sstevel@tonic-gate hubd_cleanup(dev_info_t *dip, hubd_t *hubd)
25567c478bd9Sstevel@tonic-gate {
25577c478bd9Sstevel@tonic-gate 	int		circ, rval, old_dev_state;
25587c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
25597c478bd9Sstevel@tonic-gate #ifdef DEBUG
25607c478bd9Sstevel@tonic-gate 	usb_port_t	port;
25617c478bd9Sstevel@tonic-gate #endif
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
25647c478bd9Sstevel@tonic-gate 	    "hubd_cleanup:");
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) {
25677c478bd9Sstevel@tonic-gate 		goto done;
25687c478bd9Sstevel@tonic-gate 	}
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	/* ensure we are the only one active */
25717c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate 	/* Cleanup failure is only allowed if called from detach */
25767c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DETACHING(dip)) {
25777c478bd9Sstevel@tonic-gate 		dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 		/*
25807c478bd9Sstevel@tonic-gate 		 * We are being called from detach.
25817c478bd9Sstevel@tonic-gate 		 * Fail immediately if the hotplug thread is running
25827c478bd9Sstevel@tonic-gate 		 * else set the dev_state to disconnected so that
25837c478bd9Sstevel@tonic-gate 		 * hotplug thread just exits without doing anything.
25847c478bd9Sstevel@tonic-gate 		 */
25857c478bd9Sstevel@tonic-gate 		if (hubd->h_bus_ctls || hubd->h_bus_pwr ||
25867c478bd9Sstevel@tonic-gate 		    hubd->h_hotplug_thread) {
25877c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
25887c478bd9Sstevel@tonic-gate 			ndi_devi_exit(dip, circ);
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
25917c478bd9Sstevel@tonic-gate 			    "hubd_cleanup: hotplug thread/bus ctl active "
25927c478bd9Sstevel@tonic-gate 			    "- failing detach");
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
25957c478bd9Sstevel@tonic-gate 		}
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 		/*
25987c478bd9Sstevel@tonic-gate 		 * if the deathrow thread is still active or about
25997c478bd9Sstevel@tonic-gate 		 * to become active, fail detach
26007c478bd9Sstevel@tonic-gate 		 * the roothup can only be detached if nexus drivers
26017c478bd9Sstevel@tonic-gate 		 * are unloaded or explicitly offlined
26027c478bd9Sstevel@tonic-gate 		 */
26037c478bd9Sstevel@tonic-gate 		if (rh_dip == dip) {
26047c478bd9Sstevel@tonic-gate 			if (hubd->h_cleanup_needed ||
26057c478bd9Sstevel@tonic-gate 			    hubd->h_cleanup_active) {
26067c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
26077c478bd9Sstevel@tonic-gate 				ndi_devi_exit(dip, circ);
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
26107c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
26117c478bd9Sstevel@tonic-gate 				    "hubd_cleanup: deathrow still active?"
26127c478bd9Sstevel@tonic-gate 				    "- failing detach");
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 				return (USB_FAILURE);
26157c478bd9Sstevel@tonic-gate 			}
26167c478bd9Sstevel@tonic-gate 		}
26177c478bd9Sstevel@tonic-gate 	}
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	old_dev_state = hubd->h_dev_state;
26207c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_DISCONNECTED;
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
26237c478bd9Sstevel@tonic-gate 	    "hubd_cleanup: stop polling");
26247c478bd9Sstevel@tonic-gate 	hubd_close_intr_pipe(hubd);
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr ||
26277c478bd9Sstevel@tonic-gate 	    hubd->h_hotplug_thread) == 0);
26287c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	/*
26317c478bd9Sstevel@tonic-gate 	 * deallocate events, if events are still registered
26327c478bd9Sstevel@tonic-gate 	 * (ie. children still attached) then we have to fail the detach
26337c478bd9Sstevel@tonic-gate 	 */
26347c478bd9Sstevel@tonic-gate 	if (hubd->h_ndi_event_hdl) {
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 		rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl);
26377c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(dip)) {
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate 			/* It must return success if attaching. */
26407c478bd9Sstevel@tonic-gate 			ASSERT(rval == NDI_SUCCESS);
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 		} else if (rval != NDI_SUCCESS) {
26437c478bd9Sstevel@tonic-gate 
2644d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle,
26457c478bd9Sstevel@tonic-gate 			    "hubd_cleanup: ndi_event_free_hdl failed");
26467c478bd9Sstevel@tonic-gate 			ndi_devi_exit(dip, circ);
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 		}
26517c478bd9Sstevel@tonic-gate 	}
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
26547c478bd9Sstevel@tonic-gate 
265535f36846Ssl 	if (hubd->h_init_state & HUBD_CHILDREN_CREATED) {
26567c478bd9Sstevel@tonic-gate #ifdef DEBUG
265735f36846Ssl 		for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
265835f36846Ssl 			ASSERT(hubd->h_usba_devices[port] == NULL);
265935f36846Ssl 			ASSERT(hubd->h_children_dips[port] == NULL);
266035f36846Ssl 		}
26617c478bd9Sstevel@tonic-gate #endif
266235f36846Ssl 		kmem_free(hubd->h_children_dips, hubd->h_cd_list_length);
266335f36846Ssl 		kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length);
266435f36846Ssl 	}
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	/*
26677c478bd9Sstevel@tonic-gate 	 * Disable the event callbacks first, after this point, event
26687c478bd9Sstevel@tonic-gate 	 * callbacks will never get called. Note we shouldn't hold
26697c478bd9Sstevel@tonic-gate 	 * mutex while unregistering events because there may be a
26707c478bd9Sstevel@tonic-gate 	 * competing event callback thread. Event callbacks are done
26717c478bd9Sstevel@tonic-gate 	 * with ndi mutex held and this can cause a potential deadlock.
26727c478bd9Sstevel@tonic-gate 	 * Note that cleanup can't fail after deregistration of events.
26737c478bd9Sstevel@tonic-gate 	 */
26747c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state &  HUBD_EVENTS_REGISTERED) {
26757c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
26767c478bd9Sstevel@tonic-gate 		usb_unregister_event_cbs(dip, &hubd_events);
26777c478bd9Sstevel@tonic-gate 		hubd_unregister_cpr_callback(hubd);
26787c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
26797c478bd9Sstevel@tonic-gate 	}
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	/* restore the old dev state so that device can be put into low power */
26827c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = old_dev_state;
26837c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) {
26867c478bd9Sstevel@tonic-gate 		(void) hubd_pm_busy_component(hubd, dip, 0);
26877c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
26887c478bd9Sstevel@tonic-gate 		if (hubd->h_hubpm->hubp_wakeup_enabled) {
26897c478bd9Sstevel@tonic-gate 			/*
26907c478bd9Sstevel@tonic-gate 			 * Bring the hub to full power before
26917c478bd9Sstevel@tonic-gate 			 * issuing the disable remote wakeup command
26927c478bd9Sstevel@tonic-gate 			 */
26937c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 			if ((rval = usb_handle_remote_wakeup(hubd->h_dip,
26967c478bd9Sstevel@tonic-gate 			    USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) {
26977c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PM,
26987c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
26997c478bd9Sstevel@tonic-gate 				    "hubd_cleanup: disable remote wakeup "
27007c478bd9Sstevel@tonic-gate 				    "fails=%d", rval);
27017c478bd9Sstevel@tonic-gate 			}
27027c478bd9Sstevel@tonic-gate 		}
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 		(void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF);
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
27077c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
27087c478bd9Sstevel@tonic-gate 	}
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 	if (hubpm) {
27117c478bd9Sstevel@tonic-gate 		if (hubpm->hubp_child_pwrstate) {
27127c478bd9Sstevel@tonic-gate 			kmem_free(hubpm->hubp_child_pwrstate,
27137c478bd9Sstevel@tonic-gate 			    MAX_PORTS + 1);
27147c478bd9Sstevel@tonic-gate 		}
27157c478bd9Sstevel@tonic-gate 		kmem_free(hubpm, sizeof (hub_power_t));
27167c478bd9Sstevel@tonic-gate 	}
27177c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
27207c478bd9Sstevel@tonic-gate 	    "hubd_cleanup: freeing space");
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) {
27237c478bd9Sstevel@tonic-gate 		rval = usba_hubdi_unregister(dip);
27247c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
27257c478bd9Sstevel@tonic-gate 	}
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_LOCKS_DONE) {
27287c478bd9Sstevel@tonic-gate 		mutex_destroy(HUBD_MUTEX(hubd));
27297c478bd9Sstevel@tonic-gate 		cv_destroy(&hubd->h_cv_reset_port);
27307c478bd9Sstevel@tonic-gate 	}
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) {
27357c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
27397c478bd9Sstevel@tonic-gate 		usb_pipe_close(dip, hubd->h_default_pipe,
27407c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
27417c478bd9Sstevel@tonic-gate 	}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate done:
27447c478bd9Sstevel@tonic-gate 	if (hubd->h_ancestry_str) {
27457c478bd9Sstevel@tonic-gate 		kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN);
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	usb_client_detach(dip, hubd->h_dev_data);
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(hubd->h_log_handle);
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
27537c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(hubd_statep, ddi_get_instance(dip));
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
27597c478bd9Sstevel@tonic-gate }
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate /*
27637c478bd9Sstevel@tonic-gate  * hubd_check_ports:
27647c478bd9Sstevel@tonic-gate  *	- get hub descriptor
27657c478bd9Sstevel@tonic-gate  *	- check initial port status
27667c478bd9Sstevel@tonic-gate  *	- enable power on all ports
27677c478bd9Sstevel@tonic-gate  *	- enable polling on ep1
27687c478bd9Sstevel@tonic-gate  */
27697c478bd9Sstevel@tonic-gate static int
27707c478bd9Sstevel@tonic-gate hubd_check_ports(hubd_t  *hubd)
27717c478bd9Sstevel@tonic-gate {
277235f36846Ssl 	int		rval;
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
27777c478bd9Sstevel@tonic-gate 	    "hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip));
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	/*
27807c478bd9Sstevel@tonic-gate 	 * First turn off all port power
27817c478bd9Sstevel@tonic-gate 	 */
27827c478bd9Sstevel@tonic-gate 	if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) {
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
27857c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 		return (rval);
27887c478bd9Sstevel@tonic-gate 	}
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	/*
27917c478bd9Sstevel@tonic-gate 	 * do not switch on immediately (instantly on root hub)
27927c478bd9Sstevel@tonic-gate 	 * and allow time to settle
27937c478bd9Sstevel@tonic-gate 	 */
27947c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
27957c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
27967c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	/*
27997c478bd9Sstevel@tonic-gate 	 * enable power on all ports so we can see connects
28007c478bd9Sstevel@tonic-gate 	 */
28017c478bd9Sstevel@tonic-gate 	if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) {
28027c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
28037c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 		return (rval);
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	/* wait at least 3 frames before accessing devices */
28097c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
28107c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
28117c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	/*
28147c478bd9Sstevel@tonic-gate 	 * allocate arrays for saving the dips of each child per port
28157c478bd9Sstevel@tonic-gate 	 *
28167c478bd9Sstevel@tonic-gate 	 * ports go from 1 - n, allocate 1 more entry
28177c478bd9Sstevel@tonic-gate 	 */
28187c478bd9Sstevel@tonic-gate 	hubd->h_cd_list_length =
28197c478bd9Sstevel@tonic-gate 		(sizeof (dev_info_t **)) * (hubd->h_hub_descr.bNbrPorts + 1);
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	hubd->h_children_dips = (dev_info_t **)kmem_zalloc(
28227c478bd9Sstevel@tonic-gate 			hubd->h_cd_list_length, KM_SLEEP);
28237c478bd9Sstevel@tonic-gate 	hubd->h_usba_devices = (usba_device_t **)kmem_zalloc(
28247c478bd9Sstevel@tonic-gate 			hubd->h_cd_list_length, KM_SLEEP);
28257c478bd9Sstevel@tonic-gate 
282635f36846Ssl 	hubd->h_init_state |= HUBD_CHILDREN_CREATED;
282735f36846Ssl 
28287c478bd9Sstevel@tonic-gate 	if ((rval = hubd_open_intr_pipe(hubd)) == USB_SUCCESS) {
28297c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
28307c478bd9Sstevel@tonic-gate 	}
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
28337c478bd9Sstevel@tonic-gate 	    "hubd_check_ports done");
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	return (rval);
28367c478bd9Sstevel@tonic-gate }
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate /*
28407c478bd9Sstevel@tonic-gate  * hubd_get_hub_descriptor:
28417c478bd9Sstevel@tonic-gate  */
28427c478bd9Sstevel@tonic-gate static int
28437c478bd9Sstevel@tonic-gate hubd_get_hub_descriptor(hubd_t *hubd)
28447c478bd9Sstevel@tonic-gate {
28457c478bd9Sstevel@tonic-gate 	usb_hub_descr_t	*hub_descr = &hubd->h_hub_descr;
28467c478bd9Sstevel@tonic-gate 	mblk_t		*data = NULL;
28477c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
28487c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
28497c478bd9Sstevel@tonic-gate 	uint16_t	length;
28507c478bd9Sstevel@tonic-gate 	int		rval;
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
28537c478bd9Sstevel@tonic-gate 	    "hubd_get_hub_descriptor:");
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
28567c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_default_pipe != 0);
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	/* get hub descriptor length first by requesting 8 bytes only */
28597c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
28627c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
286335f36846Ssl 	    HUB_CLASS_REQ_TYPE,
28647c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,		/* bRequest */
28657c478bd9Sstevel@tonic-gate 	    USB_DESCR_TYPE_SETUP_HUB,	/* wValue */
28667c478bd9Sstevel@tonic-gate 	    0,				/* wIndex */
28677c478bd9Sstevel@tonic-gate 	    8,				/* wLength */
28687c478bd9Sstevel@tonic-gate 	    &data, 0,
28697c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
28707c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
28717c478bd9Sstevel@tonic-gate 		    "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
28727c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
28737c478bd9Sstevel@tonic-gate 		freemsg(data);
28747c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 		return (rval);
28777c478bd9Sstevel@tonic-gate 	}
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	length = *(data->b_rptr);
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 	if (length > 8) {
28827c478bd9Sstevel@tonic-gate 		freemsg(data);
28837c478bd9Sstevel@tonic-gate 		data = NULL;
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 		/* get complete hub descriptor */
28867c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
28877c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
288835f36846Ssl 		    HUB_CLASS_REQ_TYPE,
28897c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,		/* bRequest */
28907c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_HUB,	/* wValue */
28917c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
28927c478bd9Sstevel@tonic-gate 		    length,			/* wLength */
28937c478bd9Sstevel@tonic-gate 		    &data, 0,
28947c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
28957c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
28967c478bd9Sstevel@tonic-gate 			    "get hub descriptor failed: "
28977c478bd9Sstevel@tonic-gate 			    "cr=%d cb_fl=0x%x rval=%d",
28987c478bd9Sstevel@tonic-gate 			    completion_reason, cb_flags, rval);
28997c478bd9Sstevel@tonic-gate 			freemsg(data);
29007c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 			return (rval);
29037c478bd9Sstevel@tonic-gate 		}
29047c478bd9Sstevel@tonic-gate 	}
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	/* parse the hub descriptor */
29097c478bd9Sstevel@tonic-gate 	/* only 32 ports are supported at present */
29107c478bd9Sstevel@tonic-gate 	ASSERT(*(data->b_rptr + 2) <= 32);
29117c478bd9Sstevel@tonic-gate 	if (usb_parse_CV_descr("cccscccccc",
29127c478bd9Sstevel@tonic-gate 	    data->b_rptr, data->b_wptr - data->b_rptr,
29137c478bd9Sstevel@tonic-gate 	    (void *)hub_descr, sizeof (usb_hub_descr_t)) == 0) {
29147c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
29157c478bd9Sstevel@tonic-gate 		    "parsing hub descriptor failed");
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate 		freemsg(data);
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
29207c478bd9Sstevel@tonic-gate 	}
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	freemsg(data);
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
292535f36846Ssl 	    "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
292635f36846Ssl 	    "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval,
29277c478bd9Sstevel@tonic-gate 	    hub_descr->bNbrPorts, hub_descr->wHubCharacteristics,
292835f36846Ssl 	    hub_descr->bPwrOn2PwrGood, hub_descr->bHubContrCurrent);
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	if (hub_descr->bNbrPorts > MAX_PORTS) {
29317c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
29327c478bd9Sstevel@tonic-gate 		    "Hub driver supports max of %d ports on hub. "
29337c478bd9Sstevel@tonic-gate 		    "Hence using the first %d port of %d ports available",
29347c478bd9Sstevel@tonic-gate 		    MAX_PORTS, MAX_PORTS, hub_descr->bNbrPorts);
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 		hub_descr->bNbrPorts = MAX_PORTS;
29377c478bd9Sstevel@tonic-gate 	}
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
29407c478bd9Sstevel@tonic-gate }
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 
294335f36846Ssl /*
294435f36846Ssl  * hubd_get_hub_status_words:
294535f36846Ssl  */
294635f36846Ssl static int
294735f36846Ssl hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status)
294835f36846Ssl {
294935f36846Ssl 	usb_cr_t	completion_reason;
295035f36846Ssl 	usb_cb_flags_t	cb_flags;
295135f36846Ssl 	mblk_t		*data = NULL;
295235f36846Ssl 
295335f36846Ssl 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
295435f36846Ssl 
295535f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
295635f36846Ssl 
295735f36846Ssl 	if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe,
295835f36846Ssl 	    HUB_CLASS_REQ_TYPE,
295935f36846Ssl 	    USB_REQ_GET_STATUS,
296035f36846Ssl 	    0,
296135f36846Ssl 	    0,
296235f36846Ssl 	    GET_STATUS_LENGTH,
296335f36846Ssl 	    &data, 0,
296435f36846Ssl 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
296535f36846Ssl 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
296635f36846Ssl 		    "get hub status failed: cr=%d cb=0x%x",
296735f36846Ssl 		    completion_reason, cb_flags);
296835f36846Ssl 
296935f36846Ssl 		if (data) {
297035f36846Ssl 			freemsg(data);
297135f36846Ssl 		}
297235f36846Ssl 
297335f36846Ssl 		mutex_enter(HUBD_MUTEX(hubd));
297435f36846Ssl 
297535f36846Ssl 		return (USB_FAILURE);
297635f36846Ssl 	}
297735f36846Ssl 
297835f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
297935f36846Ssl 
298035f36846Ssl 	status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
298135f36846Ssl 	status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
298235f36846Ssl 
298335f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
298435f36846Ssl 	    "hub status=0x%x change=0x%x", status[0], status[1]);
298535f36846Ssl 
298635f36846Ssl 	freemsg(data);
298735f36846Ssl 
298835f36846Ssl 	return (USB_SUCCESS);
298935f36846Ssl }
299035f36846Ssl 
299135f36846Ssl 
29927c478bd9Sstevel@tonic-gate /*
29937c478bd9Sstevel@tonic-gate  * hubd_open_intr_pipe:
29947c478bd9Sstevel@tonic-gate  *	we read all descriptors first for curiosity and then simply
29957c478bd9Sstevel@tonic-gate  *	open the pipe
29967c478bd9Sstevel@tonic-gate  */
29977c478bd9Sstevel@tonic-gate static int
29987c478bd9Sstevel@tonic-gate hubd_open_intr_pipe(hubd_t	*hubd)
29997c478bd9Sstevel@tonic-gate {
30007c478bd9Sstevel@tonic-gate 	int			rval;
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
30037c478bd9Sstevel@tonic-gate 	    "hubd_open_intr_pipe:");
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE);
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING;
30087c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(hubd->h_dip,
30117c478bd9Sstevel@tonic-gate 	    &hubd->h_ep1_descr, &hubd->h_pipe_policy,
30127c478bd9Sstevel@tonic-gate 	    0, &hubd->h_ep1_ph)) != USB_SUCCESS) {
3013d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
30147c478bd9Sstevel@tonic-gate 		    "open intr pipe failed (%d)", rval);
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
30177c478bd9Sstevel@tonic-gate 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 		return (rval);
30207c478bd9Sstevel@tonic-gate 	}
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
30237c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
30267c478bd9Sstevel@tonic-gate 	    "open intr pipe succeeded, ph=0x%p", hubd->h_ep1_ph);
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
30297c478bd9Sstevel@tonic-gate }
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate /*
30337c478bd9Sstevel@tonic-gate  * hubd_start_polling:
30347c478bd9Sstevel@tonic-gate  *	start or restart the polling
30357c478bd9Sstevel@tonic-gate  */
30367c478bd9Sstevel@tonic-gate static void
30377c478bd9Sstevel@tonic-gate hubd_start_polling(hubd_t *hubd, int always)
30387c478bd9Sstevel@tonic-gate {
30397c478bd9Sstevel@tonic-gate 	usb_intr_req_t	*reqp;
30407c478bd9Sstevel@tonic-gate 	int			rval;
30417c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	pipe_state;
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
30447c478bd9Sstevel@tonic-gate 	    "start polling: always=%d dev_state=%d pipe_state=%d\n\t"
30457c478bd9Sstevel@tonic-gate 	    "thread=%d ep1_ph=0x%p",
30467c478bd9Sstevel@tonic-gate 	    always, hubd->h_dev_state, hubd->h_intr_pipe_state,
30477c478bd9Sstevel@tonic-gate 	    hubd->h_hotplug_thread, hubd->h_ep1_ph);
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	/*
30507c478bd9Sstevel@tonic-gate 	 * start or restart polling on the intr pipe
30517c478bd9Sstevel@tonic-gate 	 * only if hotplug thread is not running
30527c478bd9Sstevel@tonic-gate 	 */
30537c478bd9Sstevel@tonic-gate 	if ((always == HUBD_ALWAYS_START_POLLING) ||
30547c478bd9Sstevel@tonic-gate 	    ((hubd->h_dev_state == USB_DEV_ONLINE) &&
30557c478bd9Sstevel@tonic-gate 	    (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
30567c478bd9Sstevel@tonic-gate 	    (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) {
30577c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
30587c478bd9Sstevel@tonic-gate 		    "start polling requested");
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate 		reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP);
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 		reqp->intr_client_private = (usb_opaque_t)hubd;
30637c478bd9Sstevel@tonic-gate 		reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
30647c478bd9Sstevel@tonic-gate 					USB_ATTRS_AUTOCLEARING;
30657c478bd9Sstevel@tonic-gate 		reqp->intr_len = hubd->h_ep1_descr.wMaxPacketSize;
30667c478bd9Sstevel@tonic-gate 		reqp->intr_cb = hubd_read_cb;
30677c478bd9Sstevel@tonic-gate 		reqp->intr_exc_cb = hubd_exception_cb;
30687c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
30697c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp,
30707c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP)) != USB_SUCCESS) {
30717c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
30727c478bd9Sstevel@tonic-gate 			    "start polling failed, rval=%d", rval);
30737c478bd9Sstevel@tonic-gate 			usb_free_intr_req(reqp);
30747c478bd9Sstevel@tonic-gate 		}
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
30777c478bd9Sstevel@tonic-gate 			USB_FLAGS_SLEEP);
30787c478bd9Sstevel@tonic-gate 		if (pipe_state != USB_PIPE_STATE_ACTIVE) {
30797c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
30807c478bd9Sstevel@tonic-gate 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
30817c478bd9Sstevel@tonic-gate 		}
30827c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
30837c478bd9Sstevel@tonic-gate 		    "start polling request 0x%p", reqp);
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
30867c478bd9Sstevel@tonic-gate 	}
30877c478bd9Sstevel@tonic-gate }
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate /*
30917c478bd9Sstevel@tonic-gate  * hubd_stop_polling
30927c478bd9Sstevel@tonic-gate  *	stop polling but do not close the pipe
30937c478bd9Sstevel@tonic-gate  */
30947c478bd9Sstevel@tonic-gate static void
30957c478bd9Sstevel@tonic-gate hubd_stop_polling(hubd_t *hubd)
30967c478bd9Sstevel@tonic-gate {
30977c478bd9Sstevel@tonic-gate 	int			rval;
30987c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	pipe_state;
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
31017c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
31027c478bd9Sstevel@tonic-gate 		    "hubd_stop_polling:");
31037c478bd9Sstevel@tonic-gate 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_STOPPED;
31047c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 		usb_pipe_stop_intr_polling(hubd->h_ep1_ph, USB_FLAGS_SLEEP);
31077c478bd9Sstevel@tonic-gate 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
31087c478bd9Sstevel@tonic-gate 			USB_FLAGS_SLEEP);
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 		if (pipe_state != USB_PIPE_STATE_IDLE) {
31117c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
31127c478bd9Sstevel@tonic-gate 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
31137c478bd9Sstevel@tonic-gate 		}
31147c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
31157c478bd9Sstevel@tonic-gate 		if (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_STOPPED) {
31167c478bd9Sstevel@tonic-gate 			hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
31177c478bd9Sstevel@tonic-gate 		}
31187c478bd9Sstevel@tonic-gate 	}
31197c478bd9Sstevel@tonic-gate }
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate /*
31237c478bd9Sstevel@tonic-gate  * hubd_close_intr_pipe:
31247c478bd9Sstevel@tonic-gate  *	close the pipe (which also stops the polling
31257c478bd9Sstevel@tonic-gate  *	and wait for the hotplug thread to exit
31267c478bd9Sstevel@tonic-gate  */
31277c478bd9Sstevel@tonic-gate static void
31287c478bd9Sstevel@tonic-gate hubd_close_intr_pipe(hubd_t *hubd)
31297c478bd9Sstevel@tonic-gate {
31307c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
31317c478bd9Sstevel@tonic-gate 	    "hubd_close_intr_pipe:");
31327c478bd9Sstevel@tonic-gate 
31337c478bd9Sstevel@tonic-gate 	/*
31347c478bd9Sstevel@tonic-gate 	 * Now that no async operation is outstanding on pipe,
31357c478bd9Sstevel@tonic-gate 	 * we can change the state to HUBD_INTR_PIPE_CLOSING
31367c478bd9Sstevel@tonic-gate 	 */
31377c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_CLOSING;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_hotplug_thread == 0);
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
31427c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
31437c478bd9Sstevel@tonic-gate 		usb_pipe_close(hubd->h_dip, hubd->h_ep1_ph, USB_FLAGS_SLEEP,
31447c478bd9Sstevel@tonic-gate 							NULL, NULL);
31457c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
31467c478bd9Sstevel@tonic-gate 		hubd->h_ep1_ph = NULL;
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
31507c478bd9Sstevel@tonic-gate }
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate /*
31547c478bd9Sstevel@tonic-gate  * hubd_exception_cb
31557c478bd9Sstevel@tonic-gate  *	interrupt ep1 exception callback function.
31567c478bd9Sstevel@tonic-gate  *	this callback executes in taskq thread context and assumes
31577c478bd9Sstevel@tonic-gate  *	autoclearing
31587c478bd9Sstevel@tonic-gate  */
31597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31607c478bd9Sstevel@tonic-gate static void
31617c478bd9Sstevel@tonic-gate hubd_exception_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
31627c478bd9Sstevel@tonic-gate {
31637c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)(reqp->intr_client_private);
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
31667c478bd9Sstevel@tonic-gate 	    "hubd_exception_cb: "
31677c478bd9Sstevel@tonic-gate 	    "req=0x%p cr=%d data=0x%p cb_flags=0x%x", reqp,
31687c478bd9Sstevel@tonic-gate 	    reqp->intr_completion_reason, reqp->intr_data,
31697c478bd9Sstevel@tonic-gate 	    reqp->intr_cb_flags);
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 	ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
31747c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	switch (reqp->intr_completion_reason) {
31777c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_RESET:
31787c478bd9Sstevel@tonic-gate 		/* only restart polling after autoclearing */
31797c478bd9Sstevel@tonic-gate 		if ((hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
31807c478bd9Sstevel@tonic-gate 		    (hubd->h_port_reset_wait == 0)) {
31817c478bd9Sstevel@tonic-gate 			hubd_start_polling(hubd, 0);
31827c478bd9Sstevel@tonic-gate 		}
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 		break;
31857c478bd9Sstevel@tonic-gate 	case USB_CR_DEV_NOT_RESP:
31867c478bd9Sstevel@tonic-gate 	case USB_CR_STOPPED_POLLING:
31877c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_CLOSING:
31887c478bd9Sstevel@tonic-gate 	case USB_CR_UNSPECIFIED_ERR:
31897c478bd9Sstevel@tonic-gate 		/* never restart polling on these conditions */
31907c478bd9Sstevel@tonic-gate 	default:
31917c478bd9Sstevel@tonic-gate 		/* for all others, wait for the autoclearing PIPE_RESET cb */
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 		break;
31947c478bd9Sstevel@tonic-gate 	}
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	usb_free_intr_req(reqp);
31977c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
31987c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
31997c478bd9Sstevel@tonic-gate }
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate /*
32037c478bd9Sstevel@tonic-gate  * helper function to convert LE bytes to a portmask
32047c478bd9Sstevel@tonic-gate  */
32057c478bd9Sstevel@tonic-gate static usb_port_mask_t
32067c478bd9Sstevel@tonic-gate hubd_mblk2portmask(mblk_t *data)
32077c478bd9Sstevel@tonic-gate {
32087c478bd9Sstevel@tonic-gate 	int len = min(data->b_wptr - data->b_rptr, sizeof (usb_port_mask_t));
32097c478bd9Sstevel@tonic-gate 	usb_port_mask_t rval = 0;
32107c478bd9Sstevel@tonic-gate 	int i;
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
32137c478bd9Sstevel@tonic-gate 		rval |= data->b_rptr[i] << (i * 8);
32147c478bd9Sstevel@tonic-gate 	}
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	return (rval);
32177c478bd9Sstevel@tonic-gate }
32187c478bd9Sstevel@tonic-gate 
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate /*
32217c478bd9Sstevel@tonic-gate  * hubd_read_cb:
32227c478bd9Sstevel@tonic-gate  *	interrupt ep1 callback function
32237c478bd9Sstevel@tonic-gate  *
32247c478bd9Sstevel@tonic-gate  *	the status indicates just a change on the pipe with no indication
32257c478bd9Sstevel@tonic-gate  *	of what the change was
32267c478bd9Sstevel@tonic-gate  *
32277c478bd9Sstevel@tonic-gate  *	known conditions:
32287c478bd9Sstevel@tonic-gate  *		- reset port completion
32297c478bd9Sstevel@tonic-gate  *		- connect
32307c478bd9Sstevel@tonic-gate  *		- disconnect
32317c478bd9Sstevel@tonic-gate  *
32327c478bd9Sstevel@tonic-gate  *	for handling the hotplugging, create a new thread that can do
32337c478bd9Sstevel@tonic-gate  *	synchronous usba calls
32347c478bd9Sstevel@tonic-gate  */
32357c478bd9Sstevel@tonic-gate static void
32367c478bd9Sstevel@tonic-gate hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
32377c478bd9Sstevel@tonic-gate {
32387c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)(reqp->intr_client_private);
32397c478bd9Sstevel@tonic-gate 	size_t		length;
32407c478bd9Sstevel@tonic-gate 	mblk_t		*data = reqp->intr_data;
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
32437c478bd9Sstevel@tonic-gate 	    "hubd_read_cb: ph=0x%p req=0x%p", pipe, reqp);
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 	ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 	/*
32487c478bd9Sstevel@tonic-gate 	 * At present, we are not handling notification for completion of
32497c478bd9Sstevel@tonic-gate 	 * asynchronous pipe reset, for which this data ptr could be NULL
32507c478bd9Sstevel@tonic-gate 	 */
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	if (data == NULL) {
32537c478bd9Sstevel@tonic-gate 		usb_free_intr_req(reqp);
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 		return;
32567c478bd9Sstevel@tonic-gate 	}
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate 	if ((hubd->h_dev_state == USB_DEV_SUSPENDED) ||
32617c478bd9Sstevel@tonic-gate 	    (hubd->h_intr_pipe_state != HUBD_INTR_PIPE_ACTIVE)) {
32627c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
32637c478bd9Sstevel@tonic-gate 		usb_free_intr_req(reqp);
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 		return;
32667c478bd9Sstevel@tonic-gate 	}
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_ep1_ph == pipe);
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	length = data->b_wptr - data->b_rptr;
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	/*
32737c478bd9Sstevel@tonic-gate 	 * Only look at the data and startup the hotplug thread if
32747c478bd9Sstevel@tonic-gate 	 * there actually is data.
32757c478bd9Sstevel@tonic-gate 	 */
32767c478bd9Sstevel@tonic-gate 	if (length != 0) {
32777c478bd9Sstevel@tonic-gate 		usb_port_mask_t port_change = hubd_mblk2portmask(data);
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate 		/*
32807c478bd9Sstevel@tonic-gate 		 * if a port change was already reported and we are waiting for
32817c478bd9Sstevel@tonic-gate 		 * reset port completion then wake up the hotplug thread which
32827c478bd9Sstevel@tonic-gate 		 * should be waiting on reset port completion
32837c478bd9Sstevel@tonic-gate 		 *
32847c478bd9Sstevel@tonic-gate 		 * if there is disconnect event instead of reset completion, let
32857c478bd9Sstevel@tonic-gate 		 * the hotplug thread figure this out
32867c478bd9Sstevel@tonic-gate 		 */
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 		/* remove the reset wait bits from the status */
32897c478bd9Sstevel@tonic-gate 		hubd->h_port_change |= port_change &
32907c478bd9Sstevel@tonic-gate 						~hubd->h_port_reset_wait;
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
32937c478bd9Sstevel@tonic-gate 		    "port change=0x%x port_reset_wait=0x%x",
32947c478bd9Sstevel@tonic-gate 		    hubd->h_port_change, hubd->h_port_reset_wait);
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 		/* there should be only one reset bit active at the time */
32977c478bd9Sstevel@tonic-gate 		if (hubd->h_port_reset_wait & port_change) {
32987c478bd9Sstevel@tonic-gate 			hubd->h_port_reset_wait = 0;
32997c478bd9Sstevel@tonic-gate 			cv_signal(&hubd->h_cv_reset_port);
33007c478bd9Sstevel@tonic-gate 		}
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 		/*
33037c478bd9Sstevel@tonic-gate 		 * kick off the thread only if device is ONLINE and it is not
33047c478bd9Sstevel@tonic-gate 		 * during attaching or detaching
33057c478bd9Sstevel@tonic-gate 		 */
33067c478bd9Sstevel@tonic-gate 		if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
33077c478bd9Sstevel@tonic-gate 		    (!DEVI_IS_ATTACHING(hubd->h_dip)) &&
33087c478bd9Sstevel@tonic-gate 		    (!DEVI_IS_DETACHING(hubd->h_dip)) &&
33097c478bd9Sstevel@tonic-gate 		    (hubd->h_port_change) &&
33107c478bd9Sstevel@tonic-gate 		    (hubd->h_hotplug_thread == 0)) {
33117c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
33127c478bd9Sstevel@tonic-gate 			    "creating hotplug thread: "
33137c478bd9Sstevel@tonic-gate 			    "dev_state=%d", hubd->h_dev_state);
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 			/*
33167c478bd9Sstevel@tonic-gate 			 * Mark this device as busy. The will be marked idle
33177c478bd9Sstevel@tonic-gate 			 * if the async req fails or at the exit of  hotplug
33187c478bd9Sstevel@tonic-gate 			 * thread
33197c478bd9Sstevel@tonic-gate 			 */
33207c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate 			if (usb_async_req(hubd->h_dip,
33237c478bd9Sstevel@tonic-gate 			    hubd_hotplug_thread,
33247c478bd9Sstevel@tonic-gate 			    (void *)hubd, 0) == USB_SUCCESS) {
33257c478bd9Sstevel@tonic-gate 				hubd->h_hotplug_thread++;
33267c478bd9Sstevel@tonic-gate 			} else {
33277c478bd9Sstevel@tonic-gate 				/* mark this device as idle */
33287c478bd9Sstevel@tonic-gate 				(void) hubd_pm_idle_component(hubd,
33297c478bd9Sstevel@tonic-gate 				    hubd->h_dip, 0);
33307c478bd9Sstevel@tonic-gate 			}
33317c478bd9Sstevel@tonic-gate 		}
33327c478bd9Sstevel@tonic-gate 	}
33337c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate 	usb_free_intr_req(reqp);
33367c478bd9Sstevel@tonic-gate }
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate /*
33407c478bd9Sstevel@tonic-gate  * hubd_hotplug_thread:
33417c478bd9Sstevel@tonic-gate  *	handles resetting of port, and creating children
33427c478bd9Sstevel@tonic-gate  *
33437c478bd9Sstevel@tonic-gate  *	the ports to check are indicated in h_port_change bit mask
33447c478bd9Sstevel@tonic-gate  * XXX note that one time poll doesn't work on the root hub
33457c478bd9Sstevel@tonic-gate  */
33467c478bd9Sstevel@tonic-gate static void
33477c478bd9Sstevel@tonic-gate hubd_hotplug_thread(void *arg)
33487c478bd9Sstevel@tonic-gate {
33497c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)arg;
33507c478bd9Sstevel@tonic-gate 	usb_port_t	port;
33517c478bd9Sstevel@tonic-gate 	uint16_t	nports;
33527c478bd9Sstevel@tonic-gate 	uint16_t	status, change;
33537c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
33547c478bd9Sstevel@tonic-gate 	dev_info_t	*hdip = hubd->h_dip;
33557c478bd9Sstevel@tonic-gate 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
33567c478bd9Sstevel@tonic-gate 	boolean_t	online_child = B_FALSE;
33577c478bd9Sstevel@tonic-gate 	boolean_t	offline_child = B_FALSE;
33587c478bd9Sstevel@tonic-gate 	boolean_t	pwrup_child = B_FALSE;
33597c478bd9Sstevel@tonic-gate 	int		prh_circ, rh_circ, circ, old_state;
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
33627c478bd9Sstevel@tonic-gate 	    "hubd_hotplug_thread:  started");
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 	/*
33657c478bd9Sstevel@tonic-gate 	 * if our bus power entry point is active, process the change
33667c478bd9Sstevel@tonic-gate 	 * on the next notification of interrupt pipe
33677c478bd9Sstevel@tonic-gate 	 */
33687c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
33697c478bd9Sstevel@tonic-gate 	if (hubd->h_bus_pwr || (hubd->h_hotplug_thread > 1)) {
33707c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread--;
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 		/* mark this device as idle */
33737c478bd9Sstevel@tonic-gate 		hubd_pm_idle_component(hubd, hubd->h_dip, 0);
33747c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
33777c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: "
33787c478bd9Sstevel@tonic-gate 		    "bus_power in progress/hotplugging undesirable - quit");
33797c478bd9Sstevel@tonic-gate 
33807c478bd9Sstevel@tonic-gate 		return;
33817c478bd9Sstevel@tonic-gate 	}
33827c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 	ndi_hold_devi(hdip); /* so we don't race with detach */
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	/* is this the root hub? */
33897c478bd9Sstevel@tonic-gate 	if (hdip == rh_dip) {
33907c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_PWRED_DOWN) {
33917c478bd9Sstevel@tonic-gate 			hubpm = hubd->h_hubpm;
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 			/* mark the root hub as full power */
33947c478bd9Sstevel@tonic-gate 			hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
33957c478bd9Sstevel@tonic-gate 			hubpm->hubp_time_at_full_power = ddi_get_time();
33967c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
33997c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: call pm_power_has_changed");
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 			(void) pm_power_has_changed(hdip, 0,
34027c478bd9Sstevel@tonic-gate 				USB_DEV_OS_FULL_PWR);
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
34057c478bd9Sstevel@tonic-gate 			hubd->h_dev_state = USB_DEV_ONLINE;
34067c478bd9Sstevel@tonic-gate 		}
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 	} else {
34097c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
34107c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: not root hub");
34117c478bd9Sstevel@tonic-gate 	}
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE);
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 	nports = hubd->h_hub_descr.bNbrPorts;
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	hubd_stop_polling(hubd);
34187c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	/*
34217c478bd9Sstevel@tonic-gate 	 * this ensures one hotplug activity per system at a time.
34227c478bd9Sstevel@tonic-gate 	 * we enter the parent PCI node to have this serialization.
34237c478bd9Sstevel@tonic-gate 	 * this also excludes ioctls and deathrow thread
34247c478bd9Sstevel@tonic-gate 	 * (a bit crude but easier to debug)
34257c478bd9Sstevel@tonic-gate 	 */
34267c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
34277c478bd9Sstevel@tonic-gate 	ndi_devi_enter(rh_dip, &rh_circ);
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	/* exclude other threads */
34307c478bd9Sstevel@tonic-gate 	ndi_devi_enter(hdip, &circ);
34317c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 	while ((hubd->h_dev_state == USB_DEV_ONLINE) &&
34347c478bd9Sstevel@tonic-gate 	    (hubd->h_port_change)) {
34357c478bd9Sstevel@tonic-gate 		/*
34367c478bd9Sstevel@tonic-gate 		 * The 0th bit is the hub status change bit.
34377c478bd9Sstevel@tonic-gate 		 * handle loss of local power here
34387c478bd9Sstevel@tonic-gate 		 */
34397c478bd9Sstevel@tonic-gate 		if (hubd->h_port_change & HUB_CHANGE_STATUS) {
34407c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
34417c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: hub status change!");
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 			/*
34447c478bd9Sstevel@tonic-gate 			 * This should be handled properly.  For now,
34457c478bd9Sstevel@tonic-gate 			 * mask off the bit.
34467c478bd9Sstevel@tonic-gate 			 */
34477c478bd9Sstevel@tonic-gate 			hubd->h_port_change &= ~HUB_CHANGE_STATUS;
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 			/*
34507c478bd9Sstevel@tonic-gate 			 * check and ack hub status
34517c478bd9Sstevel@tonic-gate 			 * this causes stall conditions
34527c478bd9Sstevel@tonic-gate 			 * when local power is removed
34537c478bd9Sstevel@tonic-gate 			 */
34547c478bd9Sstevel@tonic-gate 			(void) hubd_get_hub_status(hubd);
34557c478bd9Sstevel@tonic-gate 		}
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
34587c478bd9Sstevel@tonic-gate 			usb_port_mask_t port_mask;
34597c478bd9Sstevel@tonic-gate 			boolean_t was_connected;
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 			port_mask = 1 << port;
34627c478bd9Sstevel@tonic-gate 			was_connected =
34637c478bd9Sstevel@tonic-gate 				(hubd->h_port_state[port] & PORT_STATUS_CCS) &&
34647c478bd9Sstevel@tonic-gate 				(hubd->h_children_dips[port]);
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
34677c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: "
34687c478bd9Sstevel@tonic-gate 			    "port %d mask=0x%x change=0x%x connected=0x%x",
34697c478bd9Sstevel@tonic-gate 			    port, port_mask, hubd->h_port_change,
34707c478bd9Sstevel@tonic-gate 			    was_connected);
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 			/*
34737c478bd9Sstevel@tonic-gate 			 * is this a port connection that changed?
34747c478bd9Sstevel@tonic-gate 			 */
34757c478bd9Sstevel@tonic-gate 			if ((hubd->h_port_change & port_mask) == 0) {
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 				continue;
34787c478bd9Sstevel@tonic-gate 			}
34797c478bd9Sstevel@tonic-gate 			hubd->h_port_change &= ~port_mask;
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate 			/* ack all changes */
34827c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
34837c478bd9Sstevel@tonic-gate 			    &status, &change, HUBD_ACK_ALL_CHANGES);
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
34867c478bd9Sstevel@tonic-gate 			    "handle port %d:\n\t"
34877c478bd9Sstevel@tonic-gate 			    "new status=0x%x change=0x%x was_conn=0x%x ",
34887c478bd9Sstevel@tonic-gate 			    port, status, change, was_connected);
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 			/* Recover a disabled port */
34917c478bd9Sstevel@tonic-gate 			if (change & PORT_CHANGE_PESC) {
34927c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
34937c478bd9Sstevel@tonic-gate 					hubd->h_log_handle,
34947c478bd9Sstevel@tonic-gate 					"port%d Disabled - "
34957c478bd9Sstevel@tonic-gate 					"status=0x%x, change=0x%x",
34967c478bd9Sstevel@tonic-gate 					port, status, change);
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 				/*
34997c478bd9Sstevel@tonic-gate 				 * if the port was connected and is still
35007c478bd9Sstevel@tonic-gate 				 * connected, recover the port
35017c478bd9Sstevel@tonic-gate 				 */
35027c478bd9Sstevel@tonic-gate 				if (was_connected && (status &
35037c478bd9Sstevel@tonic-gate 				    PORT_STATUS_CCS)) {
35047c478bd9Sstevel@tonic-gate 					online_child |=
35057c478bd9Sstevel@tonic-gate 					    (hubd_recover_disabled_port(hubd,
35067c478bd9Sstevel@tonic-gate 					    port) == USB_SUCCESS);
35077c478bd9Sstevel@tonic-gate 				}
35087c478bd9Sstevel@tonic-gate 			}
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 			/*
35117c478bd9Sstevel@tonic-gate 			 * Now check what changed on the port
35127c478bd9Sstevel@tonic-gate 			 */
35137c478bd9Sstevel@tonic-gate 			if (change & PORT_CHANGE_CSC) {
35147c478bd9Sstevel@tonic-gate 				if ((status & PORT_STATUS_CCS) &&
35157c478bd9Sstevel@tonic-gate 				    (!was_connected)) {
35167c478bd9Sstevel@tonic-gate 					/* new device plugged in */
35177c478bd9Sstevel@tonic-gate 					online_child |=
35187c478bd9Sstevel@tonic-gate 					    (hubd_handle_port_connect(hubd,
35197c478bd9Sstevel@tonic-gate 					    port) == USB_SUCCESS);
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate 				} else if ((status & PORT_STATUS_CCS) &&
35227c478bd9Sstevel@tonic-gate 				    was_connected) {
35237c478bd9Sstevel@tonic-gate 					/*
35247c478bd9Sstevel@tonic-gate 					 * In this case we can never be sure
35257c478bd9Sstevel@tonic-gate 					 * if the device indeed got hotplugged
35267c478bd9Sstevel@tonic-gate 					 * or the hub is falsely reporting the
35277c478bd9Sstevel@tonic-gate 					 * change.
35287c478bd9Sstevel@tonic-gate 					 * first post a disconnect event
35297c478bd9Sstevel@tonic-gate 					 * to the child
35307c478bd9Sstevel@tonic-gate 					 */
35317c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
35327c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
35337c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_REMOVAL);
35347c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate 					/*
35377c478bd9Sstevel@tonic-gate 					 * then reset the port and recover
35387c478bd9Sstevel@tonic-gate 					 * the device
35397c478bd9Sstevel@tonic-gate 					 */
35407c478bd9Sstevel@tonic-gate 					online_child |=
35417c478bd9Sstevel@tonic-gate 					    (hubd_handle_port_connect(hubd,
35427c478bd9Sstevel@tonic-gate 					    port) == USB_SUCCESS);
35437c478bd9Sstevel@tonic-gate 				} else if (was_connected) {
35447c478bd9Sstevel@tonic-gate 					/* this is a disconnect */
35457c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
35467c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
35477c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_REMOVAL);
35487c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 					offline_child = B_TRUE;
35517c478bd9Sstevel@tonic-gate 				}
35527c478bd9Sstevel@tonic-gate 			}
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 			/*
35557c478bd9Sstevel@tonic-gate 			 * Check if any port is coming out of suspend
35567c478bd9Sstevel@tonic-gate 			 */
35577c478bd9Sstevel@tonic-gate 			if (change & PORT_CHANGE_PSSC) {
35587c478bd9Sstevel@tonic-gate 				/* a resuming device could have disconnected */
35597c478bd9Sstevel@tonic-gate 				if (was_connected &&
35607c478bd9Sstevel@tonic-gate 				    hubd->h_children_dips[port]) {
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 					/* device on this port resuming */
35637c478bd9Sstevel@tonic-gate 					dev_info_t *dip;
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate 					dip = hubd->h_children_dips[port];
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 					/*
35687c478bd9Sstevel@tonic-gate 					 * Don't raise power on detaching child
35697c478bd9Sstevel@tonic-gate 					 */
35707c478bd9Sstevel@tonic-gate 					if (!DEVI_IS_DETACHING(dip)) {
35717c478bd9Sstevel@tonic-gate 						/*
35727c478bd9Sstevel@tonic-gate 						 * As this child is not
35737c478bd9Sstevel@tonic-gate 						 * detaching, we set this
35747c478bd9Sstevel@tonic-gate 						 * flag, causing bus_ctls
35757c478bd9Sstevel@tonic-gate 						 * to stall detach till
35767c478bd9Sstevel@tonic-gate 						 * pm_raise_power returns
35777c478bd9Sstevel@tonic-gate 						 * and flag it for a deferred
35787c478bd9Sstevel@tonic-gate 						 * raise_power.
35797c478bd9Sstevel@tonic-gate 						 *
35807c478bd9Sstevel@tonic-gate 						 * pm_raise_power is deferred
35817c478bd9Sstevel@tonic-gate 						 * because we need to release
35827c478bd9Sstevel@tonic-gate 						 * the locks first.
35837c478bd9Sstevel@tonic-gate 						 */
35847c478bd9Sstevel@tonic-gate 						hubd->h_port_state[port] |=
35857c478bd9Sstevel@tonic-gate 							HUBD_CHILD_RAISE_POWER;
35867c478bd9Sstevel@tonic-gate 						pwrup_child = B_TRUE;
35877c478bd9Sstevel@tonic-gate 						mutex_exit(HUBD_MUTEX(hubd));
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 						/*
35907c478bd9Sstevel@tonic-gate 						 * make sure that child
35917c478bd9Sstevel@tonic-gate 						 * doesn't disappear
35927c478bd9Sstevel@tonic-gate 						 */
35937c478bd9Sstevel@tonic-gate 						ndi_hold_devi(dip);
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 						mutex_enter(HUBD_MUTEX(hubd));
35967c478bd9Sstevel@tonic-gate 					}
35977c478bd9Sstevel@tonic-gate 				}
35987c478bd9Sstevel@tonic-gate 			}
3599*fef1e07eSsl 
3600*fef1e07eSsl 			/*
3601*fef1e07eSsl 			 * Check if the port is over-current
3602*fef1e07eSsl 			 */
3603*fef1e07eSsl 			if (change & PORT_CHANGE_OCIC) {
3604*fef1e07eSsl 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
3605*fef1e07eSsl 				    hubd->h_log_handle,
3606*fef1e07eSsl 				    "Port%d in over current condition, "
3607*fef1e07eSsl 				    "please check the attached device to "
3608*fef1e07eSsl 				    "clear the condition. The system will "
3609*fef1e07eSsl 				    "try to recover the port, but if not "
3610*fef1e07eSsl 				    "successful, you need to re-connect "
3611*fef1e07eSsl 				    "the hub or reboot the system to bring "
3612*fef1e07eSsl 				    "the port back to work", port);
3613*fef1e07eSsl 
3614*fef1e07eSsl 				if (!(status & PORT_STATUS_PPS)) {
3615*fef1e07eSsl 					/*
3616*fef1e07eSsl 					 * Try to enable port power, but
3617*fef1e07eSsl 					 * possibly fail. Ignore failure
3618*fef1e07eSsl 					 */
3619*fef1e07eSsl 					(void) hubd_enable_port_power(hubd,
3620*fef1e07eSsl 					    port);
3621*fef1e07eSsl 
3622*fef1e07eSsl 					/*
3623*fef1e07eSsl 					 * Delay some time to avoid
3624*fef1e07eSsl 					 * over-current event to happen
3625*fef1e07eSsl 					 * too frequently in some cases
3626*fef1e07eSsl 					 */
3627*fef1e07eSsl 					mutex_exit(HUBD_MUTEX(hubd));
3628*fef1e07eSsl 					delay(drv_usectohz(500000));
3629*fef1e07eSsl 					mutex_enter(HUBD_MUTEX(hubd));
3630*fef1e07eSsl 				}
3631*fef1e07eSsl 			}
36327c478bd9Sstevel@tonic-gate 		}
36337c478bd9Sstevel@tonic-gate 	}
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	/* release locks so we can do a devfs_clean */
36367c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate 	/* delete cached dv_node's but drop locks first */
36397c478bd9Sstevel@tonic-gate 	ndi_devi_exit(hdip, circ);
36407c478bd9Sstevel@tonic-gate 	ndi_devi_exit(rh_dip, rh_circ);
36417c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate 	(void) devfs_clean(rh_dip, NULL, 0);
36447c478bd9Sstevel@tonic-gate 
36457c478bd9Sstevel@tonic-gate 	/* now check if any children need onlining */
36467c478bd9Sstevel@tonic-gate 	if (online_child) {
36477c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
36487c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: onlining children");
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate 		(void) ndi_devi_online(hubd->h_dip, 0);
36517c478bd9Sstevel@tonic-gate 	}
36527c478bd9Sstevel@tonic-gate 
36537c478bd9Sstevel@tonic-gate 	/* now check if any disconnected devices need to be cleaned up */
36547c478bd9Sstevel@tonic-gate 	if (offline_child) {
36557c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
36567c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: scheduling cleanup");
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
36597c478bd9Sstevel@tonic-gate 	}
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 	/* now raise power on the children that have woken up */
36647c478bd9Sstevel@tonic-gate 	if (pwrup_child) {
36657c478bd9Sstevel@tonic-gate 		old_state = hubd->h_dev_state;
36667c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_HUB_CHILD_PWRLVL;
36677c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
36687c478bd9Sstevel@tonic-gate 			if (hubd->h_port_state[port] & HUBD_CHILD_RAISE_POWER) {
36697c478bd9Sstevel@tonic-gate 				dev_info_t *dip = hubd->h_children_dips[port];
36707c478bd9Sstevel@tonic-gate 
36717c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 				/* Get the device to full power */
36747c478bd9Sstevel@tonic-gate 				(void) pm_busy_component(dip, 0);
36757c478bd9Sstevel@tonic-gate 				(void) pm_raise_power(dip, 0,
36767c478bd9Sstevel@tonic-gate 				    USB_DEV_OS_FULL_PWR);
36777c478bd9Sstevel@tonic-gate 				(void) pm_idle_component(dip, 0);
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 				/* release the hold on the child */
36807c478bd9Sstevel@tonic-gate 				ndi_rele_devi(dip);
36817c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
36827c478bd9Sstevel@tonic-gate 				hubd->h_port_state[port] &=
36837c478bd9Sstevel@tonic-gate 				    ~HUBD_CHILD_RAISE_POWER;
36847c478bd9Sstevel@tonic-gate 			}
36857c478bd9Sstevel@tonic-gate 		}
36867c478bd9Sstevel@tonic-gate 		/*
36877c478bd9Sstevel@tonic-gate 		 * make sure that we don't accidentally
36887c478bd9Sstevel@tonic-gate 		 * over write the disconnect state
36897c478bd9Sstevel@tonic-gate 		 */
36907c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_HUB_CHILD_PWRLVL) {
36917c478bd9Sstevel@tonic-gate 			hubd->h_dev_state = old_state;
36927c478bd9Sstevel@tonic-gate 		}
36937c478bd9Sstevel@tonic-gate 	}
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 	/*
36967c478bd9Sstevel@tonic-gate 	 * start polling can immediately kick off read callback
36977c478bd9Sstevel@tonic-gate 	 * we need to set the h_hotplug_thread to 0 so that
36987c478bd9Sstevel@tonic-gate 	 * the callback is not dropped
36997c478bd9Sstevel@tonic-gate 	 */
37007c478bd9Sstevel@tonic-gate 	hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 	/*
37037c478bd9Sstevel@tonic-gate 	 * Earlier we would set the h_hotplug_thread = 0 before
37047c478bd9Sstevel@tonic-gate 	 * polling was restarted  so that
37057c478bd9Sstevel@tonic-gate 	 * if there is any root hub status change interrupt, we can still kick
37067c478bd9Sstevel@tonic-gate 	 * off the hotplug thread. This was valid when this interrupt was
37077c478bd9Sstevel@tonic-gate 	 * delivered in hardware, and only ONE interrupt would be delivered.
37087c478bd9Sstevel@tonic-gate 	 * Now that we poll on the root hub looking for status change in
37097c478bd9Sstevel@tonic-gate 	 * software, this assignment is no longer required.
37107c478bd9Sstevel@tonic-gate 	 */
37117c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	/* mark this device as idle */
37147c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
37177c478bd9Sstevel@tonic-gate 	    "hubd_hotplug_thread: exit");
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate 	ndi_rele_devi(hdip);
37227c478bd9Sstevel@tonic-gate }
37237c478bd9Sstevel@tonic-gate 
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate /*
37267c478bd9Sstevel@tonic-gate  * hubd_handle_port_connect:
37277c478bd9Sstevel@tonic-gate  *	Transition a port from Disabled to Enabled.  Ensure that the
37287c478bd9Sstevel@tonic-gate  *	port is in the correct state before attempting to
37297c478bd9Sstevel@tonic-gate  *	access the device.
37307c478bd9Sstevel@tonic-gate  */
37317c478bd9Sstevel@tonic-gate static int
37327c478bd9Sstevel@tonic-gate hubd_handle_port_connect(hubd_t *hubd, usb_port_t port)
37337c478bd9Sstevel@tonic-gate {
37347c478bd9Sstevel@tonic-gate 	int			rval;
37357c478bd9Sstevel@tonic-gate 	int			retry;
37367c478bd9Sstevel@tonic-gate 	long			time_delay;
37377c478bd9Sstevel@tonic-gate 	long			settling_time;
37387c478bd9Sstevel@tonic-gate 	uint16_t		status;
37397c478bd9Sstevel@tonic-gate 	uint16_t		change;
37407c478bd9Sstevel@tonic-gate 	usb_addr_t		hubd_usb_addr;
37417c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
37427c478bd9Sstevel@tonic-gate 	usb_port_status_t	port_status = 0;
37437c478bd9Sstevel@tonic-gate 	usb_port_status_t	hub_port_status = 0;
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate 	/* Get the hub address and port status */
37467c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_device;
37477c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
37487c478bd9Sstevel@tonic-gate 	hubd_usb_addr = usba_device->usb_addr;
37497c478bd9Sstevel@tonic-gate 	hub_port_status = usba_device->usb_port_status;
37507c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 	/*
37537c478bd9Sstevel@tonic-gate 	 * If a device is connected, transition the
37547c478bd9Sstevel@tonic-gate 	 * port from Disabled to the Enabled state.
37557c478bd9Sstevel@tonic-gate 	 * The device will receive downstream packets
37567c478bd9Sstevel@tonic-gate 	 * in the Enabled state.
37577c478bd9Sstevel@tonic-gate 	 *
37587c478bd9Sstevel@tonic-gate 	 * reset port and wait for the hub to report
37597c478bd9Sstevel@tonic-gate 	 * completion
37607c478bd9Sstevel@tonic-gate 	 */
37617c478bd9Sstevel@tonic-gate 	change = status = 0;
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 	/*
37647c478bd9Sstevel@tonic-gate 	 * According to section 9.1.2 of USB 2.0 spec, the host should
37657c478bd9Sstevel@tonic-gate 	 * wait for atleast 100ms to allow completion of an insertion
37667c478bd9Sstevel@tonic-gate 	 * process and for power at the device to become stable.
37677c478bd9Sstevel@tonic-gate 	 * We wait for 200 ms
37687c478bd9Sstevel@tonic-gate 	 */
37697c478bd9Sstevel@tonic-gate 	settling_time = drv_usectohz(hubd_device_delay / 5);
37707c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
37717c478bd9Sstevel@tonic-gate 	delay(settling_time);
37727c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	/* calculate 600 ms delay time */
37757c478bd9Sstevel@tonic-gate 	time_delay = (6 * drv_usectohz(hubd_device_delay)) / 10;
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate 	for (retry = 0; (hubd->h_dev_state == USB_DEV_ONLINE) &&
37787c478bd9Sstevel@tonic-gate 	    (retry < hubd_retry_enumerate); retry++) {
37797c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
37807c478bd9Sstevel@tonic-gate 		    "resetting port%d, retry=%d", port, retry);
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 		if ((rval = hubd_reset_port(hubd, port)) != USB_SUCCESS) {
37837c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd,
37847c478bd9Sstevel@tonic-gate 			    port, &status, &change, 0);
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate 			/* continue only if port is still connected */
37877c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
37887c478bd9Sstevel@tonic-gate 				continue;
37897c478bd9Sstevel@tonic-gate 			}
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate 			/* carry on regardless */
37927c478bd9Sstevel@tonic-gate 		}
37937c478bd9Sstevel@tonic-gate 
37947c478bd9Sstevel@tonic-gate 		/*
37957c478bd9Sstevel@tonic-gate 		 * according to USB 2.0 spec section 11.24.2.7.1.2
37967c478bd9Sstevel@tonic-gate 		 * at the end of port reset, the hub enables the port.
37977c478bd9Sstevel@tonic-gate 		 * But for some strange reasons, uhci port remains disabled.
37987c478bd9Sstevel@tonic-gate 		 * And because the port remains disabled for the settling
37997c478bd9Sstevel@tonic-gate 		 * time below, the device connected to the port gets wedged
38007c478bd9Sstevel@tonic-gate 		 * - fails to enumerate (device not responding)
38017c478bd9Sstevel@tonic-gate 		 * Hence, we enable it here immediately and later again after
38027c478bd9Sstevel@tonic-gate 		 * the delay
38037c478bd9Sstevel@tonic-gate 		 */
38047c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port(hubd, port);
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 		/* we skip this delay in the first iteration */
38077c478bd9Sstevel@tonic-gate 		if (retry) {
38087c478bd9Sstevel@tonic-gate 			/*
38097c478bd9Sstevel@tonic-gate 			 * delay for device to signal disconnect/connect so
38107c478bd9Sstevel@tonic-gate 			 * that hub properly recognizes the speed of the device
38117c478bd9Sstevel@tonic-gate 			 */
38127c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
38137c478bd9Sstevel@tonic-gate 			delay(settling_time);
38147c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 			/*
38177c478bd9Sstevel@tonic-gate 			 * When a low speed device is connected to any port of
38187c478bd9Sstevel@tonic-gate 			 * PPX it has to be explicitly enabled
38197c478bd9Sstevel@tonic-gate 			 * Also, if device intentionally signals
38207c478bd9Sstevel@tonic-gate 			 * disconnect/connect, it will disable the port.
38217c478bd9Sstevel@tonic-gate 			 * So enable it again.
38227c478bd9Sstevel@tonic-gate 			 */
38237c478bd9Sstevel@tonic-gate 			(void) hubd_enable_port(hubd, port);
38247c478bd9Sstevel@tonic-gate 		}
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 		if ((rval = hubd_determine_port_status(hubd, port, &status,
38277c478bd9Sstevel@tonic-gate 		    &change, 0)) != USB_SUCCESS) {
38287c478bd9Sstevel@tonic-gate 
3829d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
38307c478bd9Sstevel@tonic-gate 			    "getting status failed (%d)", rval);
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 			(void) hubd_disable_port(hubd, port);
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 			continue;
38357c478bd9Sstevel@tonic-gate 		}
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate 		if (status & PORT_STATUS_POCI) {
3838d291d9f2Sfrits 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
38397c478bd9Sstevel@tonic-gate 			    "port %d overcurrent", port);
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 			(void) hubd_disable_port(hubd, port);
38427c478bd9Sstevel@tonic-gate 
38437c478bd9Sstevel@tonic-gate 			/* ack changes */
38447c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd,
38457c478bd9Sstevel@tonic-gate 			    port, &status, &change, PORT_CHANGE_OCIC);
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 			continue;
38487c478bd9Sstevel@tonic-gate 		}
38497c478bd9Sstevel@tonic-gate 
38507c478bd9Sstevel@tonic-gate 		/* is status really OK? */
38517c478bd9Sstevel@tonic-gate 		if ((status & PORT_STATUS_OK) != PORT_STATUS_OK) {
38527c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
38537c478bd9Sstevel@tonic-gate 			    "port %d status (0x%x) not OK on retry %d",
38547c478bd9Sstevel@tonic-gate 			    port, status, retry);
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 			/* check if we still have the connection */
38577c478bd9Sstevel@tonic-gate 			if (!(status & PORT_STATUS_CCS)) {
38587c478bd9Sstevel@tonic-gate 				/* lost connection, set exit condition */
38597c478bd9Sstevel@tonic-gate 				retry = hubd_retry_enumerate;
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 				break;
38627c478bd9Sstevel@tonic-gate 			}
38637c478bd9Sstevel@tonic-gate 		} else {
38647c478bd9Sstevel@tonic-gate 			/*
38657c478bd9Sstevel@tonic-gate 			 * Determine if the device is high or full
38667c478bd9Sstevel@tonic-gate 			 * or low speed.
38677c478bd9Sstevel@tonic-gate 			 */
38687c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_LSDA) {
38697c478bd9Sstevel@tonic-gate 				port_status = USBA_LOW_SPEED_DEV;
38707c478bd9Sstevel@tonic-gate 			} else if (status & PORT_STATUS_HSDA) {
38717c478bd9Sstevel@tonic-gate 				port_status = USBA_HIGH_SPEED_DEV;
38727c478bd9Sstevel@tonic-gate 			} else {
38737c478bd9Sstevel@tonic-gate 				port_status = USBA_FULL_SPEED_DEV;
38747c478bd9Sstevel@tonic-gate 			}
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
38777c478bd9Sstevel@tonic-gate 			    "creating child port%d, status=0x%x "
38787c478bd9Sstevel@tonic-gate 			    "port status=0x%x",
38797c478bd9Sstevel@tonic-gate 			    port, status, port_status);
38807c478bd9Sstevel@tonic-gate 
38817c478bd9Sstevel@tonic-gate 			/*
38827c478bd9Sstevel@tonic-gate 			 * if the child already exists, set addrs and config
38837c478bd9Sstevel@tonic-gate 			 * to the device post connect event to the child
38847c478bd9Sstevel@tonic-gate 			 */
38857c478bd9Sstevel@tonic-gate 			if (hubd->h_children_dips[port]) {
38867c478bd9Sstevel@tonic-gate 				/* set addrs to this device */
38877c478bd9Sstevel@tonic-gate 				rval = hubd_setdevaddr(hubd, port);
38887c478bd9Sstevel@tonic-gate 
38897c478bd9Sstevel@tonic-gate 				/*
38907c478bd9Sstevel@tonic-gate 				 * This delay is important for the CATC hub
38917c478bd9Sstevel@tonic-gate 				 * to enumerate. But, avoid delay in the first
38927c478bd9Sstevel@tonic-gate 				 * iteration
38937c478bd9Sstevel@tonic-gate 				 */
38947c478bd9Sstevel@tonic-gate 				if (retry) {
38957c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
38967c478bd9Sstevel@tonic-gate 					delay(drv_usectohz(
38977c478bd9Sstevel@tonic-gate 					    hubd_device_delay/100));
38987c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
38997c478bd9Sstevel@tonic-gate 				}
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 				if (rval == USB_SUCCESS) {
39027c478bd9Sstevel@tonic-gate 					/*
39037c478bd9Sstevel@tonic-gate 					 * set the default config for
39047c478bd9Sstevel@tonic-gate 					 * this device
39057c478bd9Sstevel@tonic-gate 					 */
39067c478bd9Sstevel@tonic-gate 					hubd_setdevconfig(hubd, port);
39077c478bd9Sstevel@tonic-gate 
39087c478bd9Sstevel@tonic-gate 					/*
39097c478bd9Sstevel@tonic-gate 					 * indicate to the child that
39107c478bd9Sstevel@tonic-gate 					 * it is online again
39117c478bd9Sstevel@tonic-gate 					 */
39127c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
39137c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
39147c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_INSERTION);
39157c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
39167c478bd9Sstevel@tonic-gate 
39177c478bd9Sstevel@tonic-gate 					return (USB_SUCCESS);
39187c478bd9Sstevel@tonic-gate 				}
39197c478bd9Sstevel@tonic-gate 			} else {
39207c478bd9Sstevel@tonic-gate 				/*
39217c478bd9Sstevel@tonic-gate 				 * We need to release access here
39227c478bd9Sstevel@tonic-gate 				 * so that busctls on other ports can
39237c478bd9Sstevel@tonic-gate 				 * continue and don't cause a deadlock
39247c478bd9Sstevel@tonic-gate 				 * when busctl and removal of prom node
39257c478bd9Sstevel@tonic-gate 				 * takes concurrently. This also ensures
39267c478bd9Sstevel@tonic-gate 				 * busctls for attach of successfully
39277c478bd9Sstevel@tonic-gate 				 * enumerated devices on other ports can
39287c478bd9Sstevel@tonic-gate 				 * continue concurrently with the process
39297c478bd9Sstevel@tonic-gate 				 * of enumerating the new devices. This
39307c478bd9Sstevel@tonic-gate 				 * reduces the overall boot time of the system.
39317c478bd9Sstevel@tonic-gate 				 */
39327c478bd9Sstevel@tonic-gate 				rval = hubd_create_child(hubd->h_dip,
39337c478bd9Sstevel@tonic-gate 							hubd,
39347c478bd9Sstevel@tonic-gate 							hubd->h_usba_device,
39357c478bd9Sstevel@tonic-gate 							port_status, port,
39367c478bd9Sstevel@tonic-gate 							retry);
39377c478bd9Sstevel@tonic-gate 				if (rval == USB_SUCCESS) {
39387c478bd9Sstevel@tonic-gate 					usba_update_hotplug_stats(hubd->h_dip,
39397c478bd9Sstevel@tonic-gate 					    USBA_TOTAL_HOTPLUG_SUCCESS|
39407c478bd9Sstevel@tonic-gate 					    USBA_HOTPLUG_SUCCESS);
39417c478bd9Sstevel@tonic-gate 					hubd->h_total_hotplug_success++;
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 					if (retry > 0) {
3944d291d9f2Sfrits 						USB_DPRINTF_L2(
39457c478bd9Sstevel@tonic-gate 						    DPRINT_MASK_HOTPLUG,
39467c478bd9Sstevel@tonic-gate 						    hubd->h_log_handle,
39477c478bd9Sstevel@tonic-gate 						    "device on port %d "
39487c478bd9Sstevel@tonic-gate 						    "enumerated after %d %s",
39497c478bd9Sstevel@tonic-gate 						    port, retry,
39507c478bd9Sstevel@tonic-gate 						    (retry > 1) ? "retries" :
39517c478bd9Sstevel@tonic-gate 						    "retry");
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate 					}
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate 					return (USB_SUCCESS);
39567c478bd9Sstevel@tonic-gate 				}
39577c478bd9Sstevel@tonic-gate 			}
39587c478bd9Sstevel@tonic-gate 		}
39597c478bd9Sstevel@tonic-gate 
39607c478bd9Sstevel@tonic-gate 		/* wait a while until it settles? */
39617c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
39627c478bd9Sstevel@tonic-gate 		    "disabling port %d again", port);
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
39657c478bd9Sstevel@tonic-gate 		if (retry) {
39667c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
39677c478bd9Sstevel@tonic-gate 			delay(time_delay);
39687c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
39697c478bd9Sstevel@tonic-gate 		}
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
39727c478bd9Sstevel@tonic-gate 		    "retrying on port %d", port);
39737c478bd9Sstevel@tonic-gate 	}
39747c478bd9Sstevel@tonic-gate 
39757c478bd9Sstevel@tonic-gate 	if (retry >= hubd_retry_enumerate) {
39767c478bd9Sstevel@tonic-gate 		/*
39777c478bd9Sstevel@tonic-gate 		 * If it is a High Speed Root Hub and connected device
39787c478bd9Sstevel@tonic-gate 		 * Is a Low/Full Speed, it will be handled by USB 1.1
39797c478bd9Sstevel@tonic-gate 		 * Host Controller. In this case, USB 2.0 Host Controller
39807c478bd9Sstevel@tonic-gate 		 * will transfer the ownership of this port to USB 1.1
39817c478bd9Sstevel@tonic-gate 		 * Host Controller. So don't display any error message on
39827c478bd9Sstevel@tonic-gate 		 * the console.
39837c478bd9Sstevel@tonic-gate 		 */
39847c478bd9Sstevel@tonic-gate 		if ((hubd_usb_addr == ROOT_HUB_ADDR) &&
39857c478bd9Sstevel@tonic-gate 		    (hub_port_status == USBA_HIGH_SPEED_DEV) &&
39867c478bd9Sstevel@tonic-gate 		    (port_status != USBA_HIGH_SPEED_DEV)) {
39877c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
39887c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
39897c478bd9Sstevel@tonic-gate 			    "hubd_handle_port_connect: Low/Full speed "
39907c478bd9Sstevel@tonic-gate 			    "device is connected to High Speed root hub");
39917c478bd9Sstevel@tonic-gate 		} else {
39927c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
39937c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
39947c478bd9Sstevel@tonic-gate 			    "Connecting device on port %d failed", port);
39957c478bd9Sstevel@tonic-gate 		}
39967c478bd9Sstevel@tonic-gate 
39977c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
39987c478bd9Sstevel@tonic-gate 		usba_update_hotplug_stats(hubd->h_dip,
39997c478bd9Sstevel@tonic-gate 		    USBA_TOTAL_HOTPLUG_FAILURE|USBA_HOTPLUG_FAILURE);
40007c478bd9Sstevel@tonic-gate 		hubd->h_total_hotplug_failure++;
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate 		/*
40037c478bd9Sstevel@tonic-gate 		 * the port should be automagically
40047c478bd9Sstevel@tonic-gate 		 * disabled but just in case, we do
40057c478bd9Sstevel@tonic-gate 		 * it here
40067c478bd9Sstevel@tonic-gate 		 */
40077c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
40087c478bd9Sstevel@tonic-gate 
40097c478bd9Sstevel@tonic-gate 		/* ack all changes because we disabled this port */
40107c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd,
40117c478bd9Sstevel@tonic-gate 		    port, &status, &change, HUBD_ACK_ALL_CHANGES);
40127c478bd9Sstevel@tonic-gate 
40137c478bd9Sstevel@tonic-gate 	}
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
40167c478bd9Sstevel@tonic-gate }
40177c478bd9Sstevel@tonic-gate 
40187c478bd9Sstevel@tonic-gate 
40197c478bd9Sstevel@tonic-gate /*
40207c478bd9Sstevel@tonic-gate  * hubd_get_hub_status:
40217c478bd9Sstevel@tonic-gate  */
40227c478bd9Sstevel@tonic-gate static int
40237c478bd9Sstevel@tonic-gate hubd_get_hub_status(hubd_t *hubd)
40247c478bd9Sstevel@tonic-gate {
40257c478bd9Sstevel@tonic-gate 	int		rval;
40267c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
40277c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
402835f36846Ssl 	uint16_t	stword[2];
40297c478bd9Sstevel@tonic-gate 	uint16_t	status;
40307c478bd9Sstevel@tonic-gate 	uint16_t	change;
40317c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	cfg_descr;
40327c478bd9Sstevel@tonic-gate 	size_t		cfg_length;
40337c478bd9Sstevel@tonic-gate 	uchar_t		*usb_cfg;
40347c478bd9Sstevel@tonic-gate 	uint8_t		MaxPower;
4035*fef1e07eSsl 	usb_hub_descr_t	*hub_descr;
4036*fef1e07eSsl 	usb_port_t	port;
40377c478bd9Sstevel@tonic-gate 
4038*fef1e07eSsl 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
403935f36846Ssl 	    "hubd_get_hub_status:");
40407c478bd9Sstevel@tonic-gate 
404135f36846Ssl 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
40427c478bd9Sstevel@tonic-gate 
404335f36846Ssl 	if ((hubd_get_hub_status_words(hubd, stword)) != USB_SUCCESS) {
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
40467c478bd9Sstevel@tonic-gate 	}
404735f36846Ssl 	status = stword[0];
404835f36846Ssl 	change = stword[1];
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate 	/* Obtain the raw configuration descriptor */
40537c478bd9Sstevel@tonic-gate 	usb_cfg = usb_get_raw_cfg_data(hubd->h_dip, &cfg_length);
40547c478bd9Sstevel@tonic-gate 
40557c478bd9Sstevel@tonic-gate 	/* get configuration descriptor */
40567c478bd9Sstevel@tonic-gate 	rval = usb_parse_cfg_descr(usb_cfg, cfg_length,
40577c478bd9Sstevel@tonic-gate 				&cfg_descr, USB_CFG_DESCR_SIZE);
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 	if (rval != USB_CFG_DESCR_SIZE) {
40607c478bd9Sstevel@tonic-gate 
4061*fef1e07eSsl 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
40627c478bd9Sstevel@tonic-gate 		    "get hub configuration descriptor failed.");
40637c478bd9Sstevel@tonic-gate 
40647c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
40657c478bd9Sstevel@tonic-gate 
40667c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
40677c478bd9Sstevel@tonic-gate 	} else {
40687c478bd9Sstevel@tonic-gate 		MaxPower = cfg_descr.bMaxPower;
40697c478bd9Sstevel@tonic-gate 	}
40707c478bd9Sstevel@tonic-gate 
40717c478bd9Sstevel@tonic-gate 	/* check if local power status changed. */
40727c478bd9Sstevel@tonic-gate 	if (change & C_HUB_LOCAL_POWER_STATUS) {
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate 		/*
40757c478bd9Sstevel@tonic-gate 		 * local power has been lost, check the maximum
40767c478bd9Sstevel@tonic-gate 		 * power consumption of current configuration.
40777c478bd9Sstevel@tonic-gate 		 * see USB2.0 spec Table 11-12.
40787c478bd9Sstevel@tonic-gate 		 */
40797c478bd9Sstevel@tonic-gate 		if (status & HUB_LOCAL_POWER_STATUS) {
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate 			if (MaxPower == 0) {
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate 				/*
40847c478bd9Sstevel@tonic-gate 				 * Self-powered only hub. Because it could
40857c478bd9Sstevel@tonic-gate 				 * not draw any power from USB bus.
40867c478bd9Sstevel@tonic-gate 				 * It can't work well on this condition.
40877c478bd9Sstevel@tonic-gate 				 */
4088*fef1e07eSsl 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
40897c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
40907c478bd9Sstevel@tonic-gate 				    "local power has been lost, "
40917c478bd9Sstevel@tonic-gate 				    "please disconnect hub");
40927c478bd9Sstevel@tonic-gate 			} else {
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate 				/*
40957c478bd9Sstevel@tonic-gate 				 * Bus-powered only or self/bus-powered hub.
40967c478bd9Sstevel@tonic-gate 				 */
4097*fef1e07eSsl 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
40987c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
40997c478bd9Sstevel@tonic-gate 				    "local power has been lost,"
41007c478bd9Sstevel@tonic-gate 				    "the hub could draw %d"
41017c478bd9Sstevel@tonic-gate 				    " mA power from the USB bus.",
41027c478bd9Sstevel@tonic-gate 				    2*MaxPower);
41037c478bd9Sstevel@tonic-gate 			}
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate 		}
41067c478bd9Sstevel@tonic-gate 
4107*fef1e07eSsl 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
41087c478bd9Sstevel@tonic-gate 		    "clearing feature C_HUB_LOCAL_POWER ");
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
41117c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
4112*fef1e07eSsl 		    HUB_HANDLE_HUB_FEATURE_TYPE,
41137c478bd9Sstevel@tonic-gate 		    USB_REQ_CLEAR_FEATURE,
41147c478bd9Sstevel@tonic-gate 		    CFS_C_HUB_LOCAL_POWER,
41157c478bd9Sstevel@tonic-gate 		    0,
41167c478bd9Sstevel@tonic-gate 		    0,
41177c478bd9Sstevel@tonic-gate 		    NULL, 0,
41187c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4119*fef1e07eSsl 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
41207c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
41217c478bd9Sstevel@tonic-gate 			    "clear feature C_HUB_LOCAL_POWER "
41227c478bd9Sstevel@tonic-gate 			    "failed (%d 0x%x %d)",
41237c478bd9Sstevel@tonic-gate 			    rval, completion_reason, cb_flags);
41247c478bd9Sstevel@tonic-gate 		}
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 	}
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate 	if (change & C_HUB_OVER_CURRENT) {
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate 		if (status & HUB_OVER_CURRENT) {
4131*fef1e07eSsl 
4132*fef1e07eSsl 			if (usba_is_root_hub(hubd->h_dip)) {
4133*fef1e07eSsl 				/*
4134*fef1e07eSsl 				 * The root hub should be automatically
4135*fef1e07eSsl 				 * recovered when over-current condition is
4136*fef1e07eSsl 				 * cleared. But there might be exception and
4137*fef1e07eSsl 				 * need user interaction to recover.
4138*fef1e07eSsl 				 */
4139*fef1e07eSsl 				USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4140*fef1e07eSsl 				    hubd->h_log_handle,
4141*fef1e07eSsl 				    "Root hub over current condition, "
4142*fef1e07eSsl 				    "please check your system to clear the "
4143*fef1e07eSsl 				    "condition as soon as possible. And you "
4144*fef1e07eSsl 				    "may need to reboot the system to bring "
4145*fef1e07eSsl 				    "the root hub back to work if it cannot "
4146*fef1e07eSsl 				    "recover automatically");
4147*fef1e07eSsl 			} else {
4148*fef1e07eSsl 				/*
4149*fef1e07eSsl 				 * The driver would try to recover port power
4150*fef1e07eSsl 				 * on over current condition. When the recovery
4151*fef1e07eSsl 				 * fails, the user may still need to offline
4152*fef1e07eSsl 				 * this hub in order to recover.
4153*fef1e07eSsl 				 * The port power is automatically disabled,
4154*fef1e07eSsl 				 * so we won't see disconnects.
4155*fef1e07eSsl 				 */
4156*fef1e07eSsl 				USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4157*fef1e07eSsl 				    hubd->h_log_handle,
4158*fef1e07eSsl 				    "Hub global over current condition, "
4159*fef1e07eSsl 				    "please disconnect the devices connected "
4160*fef1e07eSsl 				    "to the hub to clear the condition. And "
4161*fef1e07eSsl 				    "you may need to re-connect the hub if "
4162*fef1e07eSsl 				    "the ports do not work");
4163*fef1e07eSsl 			}
41647c478bd9Sstevel@tonic-gate 		}
41657c478bd9Sstevel@tonic-gate 
4166*fef1e07eSsl 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
41677c478bd9Sstevel@tonic-gate 		    "clearing feature C_HUB_OVER_CURRENT");
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
41707c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
4171*fef1e07eSsl 		    HUB_HANDLE_HUB_FEATURE_TYPE,
41727c478bd9Sstevel@tonic-gate 		    USB_REQ_CLEAR_FEATURE,
41737c478bd9Sstevel@tonic-gate 		    CFS_C_HUB_OVER_CURRENT,
41747c478bd9Sstevel@tonic-gate 		    0,
41757c478bd9Sstevel@tonic-gate 		    0,
41767c478bd9Sstevel@tonic-gate 		    NULL, 0,
41777c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4178*fef1e07eSsl 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
41797c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
41807c478bd9Sstevel@tonic-gate 			    "clear feature C_HUB_OVER_CURRENT "
41817c478bd9Sstevel@tonic-gate 			    "failed (%d 0x%x %d)",
41827c478bd9Sstevel@tonic-gate 			    rval, completion_reason, cb_flags);
41837c478bd9Sstevel@tonic-gate 		}
4184*fef1e07eSsl 
4185*fef1e07eSsl 		/*
4186*fef1e07eSsl 		 * Try to recover all port power if they are turned off.
4187*fef1e07eSsl 		 * Don't do this for root hub, but rely on the root hub
4188*fef1e07eSsl 		 * to recover itself.
4189*fef1e07eSsl 		 */
4190*fef1e07eSsl 		if (!usba_is_root_hub(hubd->h_dip)) {
4191*fef1e07eSsl 
4192*fef1e07eSsl 			mutex_enter(HUBD_MUTEX(hubd));
4193*fef1e07eSsl 
4194*fef1e07eSsl 			/*
4195*fef1e07eSsl 			 * Only check the power status of the 1st port
4196*fef1e07eSsl 			 * since all port power status should be the same.
4197*fef1e07eSsl 			 */
4198*fef1e07eSsl 			(void) hubd_determine_port_status(hubd, 1, &status,
4199*fef1e07eSsl 			    &change, 0);
4200*fef1e07eSsl 
4201*fef1e07eSsl 			if (status & PORT_STATUS_PPS) {
4202*fef1e07eSsl 
4203*fef1e07eSsl 				return (USB_SUCCESS);
4204*fef1e07eSsl 			}
4205*fef1e07eSsl 
4206*fef1e07eSsl 			hub_descr = &hubd->h_hub_descr;
4207*fef1e07eSsl 
4208*fef1e07eSsl 			for (port = 1; port <= hub_descr->bNbrPorts;
4209*fef1e07eSsl 			    port++) {
4210*fef1e07eSsl 
4211*fef1e07eSsl 				(void) hubd_enable_port_power(hubd, port);
4212*fef1e07eSsl 			}
4213*fef1e07eSsl 
4214*fef1e07eSsl 			mutex_exit(HUBD_MUTEX(hubd));
4215*fef1e07eSsl 
4216*fef1e07eSsl 			/*
4217*fef1e07eSsl 			 * Delay some time to avoid over-current event
4218*fef1e07eSsl 			 * to happen too frequently in some cases
4219*fef1e07eSsl 			 */
4220*fef1e07eSsl 			delay(drv_usectohz(500000));
4221*fef1e07eSsl 		}
42227c478bd9Sstevel@tonic-gate 	}
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
42257c478bd9Sstevel@tonic-gate 
42267c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
42277c478bd9Sstevel@tonic-gate }
42287c478bd9Sstevel@tonic-gate 
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate /*
42317c478bd9Sstevel@tonic-gate  * hubd_reset_port:
42327c478bd9Sstevel@tonic-gate  */
42337c478bd9Sstevel@tonic-gate static int
42347c478bd9Sstevel@tonic-gate hubd_reset_port(hubd_t *hubd, usb_port_t port)
42357c478bd9Sstevel@tonic-gate {
42367c478bd9Sstevel@tonic-gate 	int	rval;
42377c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
42387c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
42397c478bd9Sstevel@tonic-gate 	usb_port_mask_t port_mask = 1 << port;
42407c478bd9Sstevel@tonic-gate 	mblk_t	*data;
42417c478bd9Sstevel@tonic-gate 	uint16_t status;
42427c478bd9Sstevel@tonic-gate 	uint16_t change;
42437c478bd9Sstevel@tonic-gate 	int	i;
42447c478bd9Sstevel@tonic-gate 	clock_t	current_time;
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
42477c478bd9Sstevel@tonic-gate 	    "hubd_reset_port: port=%d", port);
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
42507c478bd9Sstevel@tonic-gate 
42517c478bd9Sstevel@tonic-gate 	hubd->h_port_reset_wait |= port_mask;
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
42547c478bd9Sstevel@tonic-gate 
42557c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
42567c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
425735f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
42587c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
42597c478bd9Sstevel@tonic-gate 	    CFS_PORT_RESET,
42607c478bd9Sstevel@tonic-gate 	    port,
42617c478bd9Sstevel@tonic-gate 	    0,
42627c478bd9Sstevel@tonic-gate 	    NULL, 0,
42637c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4264d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
42657c478bd9Sstevel@tonic-gate 		    "reset port%d failed (%d 0x%x %d)",
42667c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
42677c478bd9Sstevel@tonic-gate 
42687c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
42717c478bd9Sstevel@tonic-gate 	}
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
42767c478bd9Sstevel@tonic-gate 	    "waiting on cv for reset completion");
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 	/*
42797c478bd9Sstevel@tonic-gate 	 * wait for port status change event
42807c478bd9Sstevel@tonic-gate 	 */
42817c478bd9Sstevel@tonic-gate 	for (i = 0; i < hubd_retry_enumerate; i++) {
42827c478bd9Sstevel@tonic-gate 		/*
42837c478bd9Sstevel@tonic-gate 		 * start polling ep1 for receiving notification on
42847c478bd9Sstevel@tonic-gate 		 * reset completion
42857c478bd9Sstevel@tonic-gate 		 */
42867c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate 		/*
42897c478bd9Sstevel@tonic-gate 		 * sleep a max of 100ms for reset completion
42907c478bd9Sstevel@tonic-gate 		 * notification to be received
42917c478bd9Sstevel@tonic-gate 		 */
42927c478bd9Sstevel@tonic-gate 		current_time = ddi_get_lbolt();
42937c478bd9Sstevel@tonic-gate 		if (hubd->h_port_reset_wait & port_mask) {
42947c478bd9Sstevel@tonic-gate 			rval = cv_timedwait(&hubd->h_cv_reset_port,
42957c478bd9Sstevel@tonic-gate 				&hubd->h_mutex,
42967c478bd9Sstevel@tonic-gate 				current_time +
42977c478bd9Sstevel@tonic-gate 				drv_usectohz(hubd_device_delay / 10));
42987c478bd9Sstevel@tonic-gate 			if ((rval <= 0) &&
42997c478bd9Sstevel@tonic-gate 			    (hubd->h_port_reset_wait & port_mask)) {
43007c478bd9Sstevel@tonic-gate 				/* we got woken up because of a timeout */
43017c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
43027c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
43037c478bd9Sstevel@tonic-gate 				    "timeout: reset port=%d failed", port);
43047c478bd9Sstevel@tonic-gate 
43057c478bd9Sstevel@tonic-gate 				hubd->h_port_reset_wait &=  ~port_mask;
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 				hubd_stop_polling(hubd);
43087c478bd9Sstevel@tonic-gate 
43097c478bd9Sstevel@tonic-gate 				return (USB_FAILURE);
43107c478bd9Sstevel@tonic-gate 			}
43117c478bd9Sstevel@tonic-gate 		}
43127c478bd9Sstevel@tonic-gate 
43137c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
43147c478bd9Sstevel@tonic-gate 		    "reset completion received");
43157c478bd9Sstevel@tonic-gate 
43167c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate 		data = NULL;
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate 		/* check status to determine whether reset completed */
43217c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
43227c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
43237c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
432435f36846Ssl 		    HUB_GET_PORT_STATUS_TYPE,
43257c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_STATUS,
43267c478bd9Sstevel@tonic-gate 		    0,
43277c478bd9Sstevel@tonic-gate 		    port,
43287c478bd9Sstevel@tonic-gate 		    GET_STATUS_LENGTH,
43297c478bd9Sstevel@tonic-gate 		    &data, 0,
43307c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4331d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_PORT,
43327c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
43337c478bd9Sstevel@tonic-gate 			    "get status port%d failed (%d 0x%x %d)",
43347c478bd9Sstevel@tonic-gate 			    port, completion_reason, cb_flags, rval);
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate 			if (data) {
43377c478bd9Sstevel@tonic-gate 				freemsg(data);
43387c478bd9Sstevel@tonic-gate 				data = NULL;
43397c478bd9Sstevel@tonic-gate 			}
43407c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
43417c478bd9Sstevel@tonic-gate 
43427c478bd9Sstevel@tonic-gate 			continue;
43437c478bd9Sstevel@tonic-gate 		}
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate 		status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
43467c478bd9Sstevel@tonic-gate 		change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
43477c478bd9Sstevel@tonic-gate 
43487c478bd9Sstevel@tonic-gate 		freemsg(data);
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 		/* continue only if port is still connected */
43517c478bd9Sstevel@tonic-gate 		if (!(status & PORT_STATUS_CCS)) {
43527c478bd9Sstevel@tonic-gate 
43537c478bd9Sstevel@tonic-gate 			/* lost connection, set exit condition */
43547c478bd9Sstevel@tonic-gate 			i = hubd_retry_enumerate;
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
43577c478bd9Sstevel@tonic-gate 
43587c478bd9Sstevel@tonic-gate 			break;
43597c478bd9Sstevel@tonic-gate 		}
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 		if (status & PORT_STATUS_PRS) {
43627c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
43637c478bd9Sstevel@tonic-gate 			    "port%d reset active", port);
43647c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 			continue;
43677c478bd9Sstevel@tonic-gate 		} else {
43687c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
43697c478bd9Sstevel@tonic-gate 			    "port%d reset inactive", port);
43707c478bd9Sstevel@tonic-gate 		}
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 		if (change & PORT_CHANGE_PRSC) {
43737c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
43747c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_RESET");
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate 			if (usb_pipe_sync_ctrl_xfer(hubd->h_dip,
43777c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
437835f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
43797c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
43807c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_RESET,
43817c478bd9Sstevel@tonic-gate 			    port,
43827c478bd9Sstevel@tonic-gate 			    0,
43837c478bd9Sstevel@tonic-gate 			    NULL, 0,
43847c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
43857c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
43867c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
43877c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_RESET"
43887c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
43897c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
43907c478bd9Sstevel@tonic-gate 			}
43917c478bd9Sstevel@tonic-gate 		}
43927c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 		break;
43957c478bd9Sstevel@tonic-gate 	}
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate 	if (i >= hubd_retry_enumerate) {
43987c478bd9Sstevel@tonic-gate 		/* port reset has failed */
43997c478bd9Sstevel@tonic-gate 		rval = USB_FAILURE;
44007c478bd9Sstevel@tonic-gate 	}
44017c478bd9Sstevel@tonic-gate 
44027c478bd9Sstevel@tonic-gate 	return (rval);
44037c478bd9Sstevel@tonic-gate }
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate 
44067c478bd9Sstevel@tonic-gate /*
44077c478bd9Sstevel@tonic-gate  * hubd_enable_port:
44087c478bd9Sstevel@tonic-gate  *	this may fail if the hub as been disconnected
44097c478bd9Sstevel@tonic-gate  */
44107c478bd9Sstevel@tonic-gate static int
44117c478bd9Sstevel@tonic-gate hubd_enable_port(hubd_t *hubd, usb_port_t port)
44127c478bd9Sstevel@tonic-gate {
44137c478bd9Sstevel@tonic-gate 	int	rval;
44147c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
44157c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
44187c478bd9Sstevel@tonic-gate 	    "hubd_enable_port: port=%d", port);
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
44217c478bd9Sstevel@tonic-gate 
44227c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 	/* Do not issue a SetFeature(PORT_ENABLE) on external hubs */
44257c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(hubd->h_dip)) {
44267c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
44297c478bd9Sstevel@tonic-gate 	}
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
44327c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
443335f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
44347c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
44357c478bd9Sstevel@tonic-gate 	    CFS_PORT_ENABLE,
44367c478bd9Sstevel@tonic-gate 	    port,
44377c478bd9Sstevel@tonic-gate 	    0,
44387c478bd9Sstevel@tonic-gate 	    NULL, 0,
44397c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
44407c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
44417c478bd9Sstevel@tonic-gate 		    "enable port%d failed (%d 0x%x %d)",
44427c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
44437c478bd9Sstevel@tonic-gate 	}
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
44487c478bd9Sstevel@tonic-gate 	    "enabling port done");
44497c478bd9Sstevel@tonic-gate 
44507c478bd9Sstevel@tonic-gate 	return (rval);
44517c478bd9Sstevel@tonic-gate }
44527c478bd9Sstevel@tonic-gate 
44537c478bd9Sstevel@tonic-gate 
44547c478bd9Sstevel@tonic-gate /*
44557c478bd9Sstevel@tonic-gate  * hubd_disable_port
44567c478bd9Sstevel@tonic-gate  */
44577c478bd9Sstevel@tonic-gate static int
44587c478bd9Sstevel@tonic-gate hubd_disable_port(hubd_t *hubd, usb_port_t port)
44597c478bd9Sstevel@tonic-gate {
44607c478bd9Sstevel@tonic-gate 	int	rval;
44617c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
44627c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
44637c478bd9Sstevel@tonic-gate 
44647c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
44657c478bd9Sstevel@tonic-gate 	    "hubd_disable_port: port=%d", port);
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
44687c478bd9Sstevel@tonic-gate 
44697c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
44727c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
447335f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
44747c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
44757c478bd9Sstevel@tonic-gate 	    CFS_PORT_ENABLE,
44767c478bd9Sstevel@tonic-gate 	    port,
44777c478bd9Sstevel@tonic-gate 	    0,
44787c478bd9Sstevel@tonic-gate 	    NULL, 0,
44797c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
44807c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
44817c478bd9Sstevel@tonic-gate 		    "disable port%d failed (%d 0x%x %d)", port,
44827c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
44837c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
44867c478bd9Sstevel@tonic-gate 	}
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
44897c478bd9Sstevel@tonic-gate 	    "clearing feature CFS_C_PORT_ENABLE");
44907c478bd9Sstevel@tonic-gate 
44917c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
44927c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
449335f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
44947c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
44957c478bd9Sstevel@tonic-gate 	    CFS_C_PORT_ENABLE,
44967c478bd9Sstevel@tonic-gate 	    port,
44977c478bd9Sstevel@tonic-gate 	    0,
44987c478bd9Sstevel@tonic-gate 	    NULL, 0,
44997c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
45007c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT,
45017c478bd9Sstevel@tonic-gate 		    hubd->h_log_handle,
45027c478bd9Sstevel@tonic-gate 		    "clear feature CFS_C_PORT_ENABLE port%d failed "
45037c478bd9Sstevel@tonic-gate 		    "(%d 0x%x %d)",
45047c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
45057c478bd9Sstevel@tonic-gate 
45067c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
45097c478bd9Sstevel@tonic-gate 	}
45107c478bd9Sstevel@tonic-gate 
45117c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
45147c478bd9Sstevel@tonic-gate }
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate 
45177c478bd9Sstevel@tonic-gate /*
45187c478bd9Sstevel@tonic-gate  * hubd_determine_port_status:
45197c478bd9Sstevel@tonic-gate  */
45207c478bd9Sstevel@tonic-gate static int
45217c478bd9Sstevel@tonic-gate hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
45227c478bd9Sstevel@tonic-gate 		uint16_t *status, uint16_t *change, uint_t ack_flag)
45237c478bd9Sstevel@tonic-gate {
45247c478bd9Sstevel@tonic-gate 	int rval;
45257c478bd9Sstevel@tonic-gate 	mblk_t	*data = NULL;
45267c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
45277c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
45307c478bd9Sstevel@tonic-gate 	    "hubd_determine_port_status: port=%d, state=0x%x ack=0x%x", port,
45317c478bd9Sstevel@tonic-gate 	    hubd->h_port_state[port], ack_flag);
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
45387c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
453935f36846Ssl 	    HUB_GET_PORT_STATUS_TYPE,
45407c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_STATUS,
45417c478bd9Sstevel@tonic-gate 	    0,
45427c478bd9Sstevel@tonic-gate 	    port,
45437c478bd9Sstevel@tonic-gate 	    GET_STATUS_LENGTH,
45447c478bd9Sstevel@tonic-gate 	    &data, 0,
45457c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
45467c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
45477c478bd9Sstevel@tonic-gate 		    "port=%d get status failed (%d 0x%x %d)",
45487c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
45497c478bd9Sstevel@tonic-gate 
45507c478bd9Sstevel@tonic-gate 		if (data) {
45517c478bd9Sstevel@tonic-gate 			freemsg(data);
45527c478bd9Sstevel@tonic-gate 		}
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 		*status = *change = 0;
45557c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate 		return (rval);
45587c478bd9Sstevel@tonic-gate 	}
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
45617c478bd9Sstevel@tonic-gate 	if ((data->b_wptr - data->b_rptr) != GET_STATUS_LENGTH) {
45627c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
45637c478bd9Sstevel@tonic-gate 		    "port %d: length incorrect %d",
45647c478bd9Sstevel@tonic-gate 		    port, data->b_wptr - data->b_rptr);
45657c478bd9Sstevel@tonic-gate 		freemsg(data);
45667c478bd9Sstevel@tonic-gate 		*status = *change = 0;
45677c478bd9Sstevel@tonic-gate 
45687c478bd9Sstevel@tonic-gate 		return (rval);
45697c478bd9Sstevel@tonic-gate 	}
45707c478bd9Sstevel@tonic-gate 
45717c478bd9Sstevel@tonic-gate 
45727c478bd9Sstevel@tonic-gate 	*status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
45737c478bd9Sstevel@tonic-gate 	*change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
45747c478bd9Sstevel@tonic-gate 
45757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
45767c478bd9Sstevel@tonic-gate 	    "port%d status=0x%x, change=0x%x", port, *status, *change);
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate 	freemsg(data);
45797c478bd9Sstevel@tonic-gate 
45807c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_CCS) {
45817c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
45827c478bd9Sstevel@tonic-gate 		    "port%d connected", port);
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_CCS & ack_flag);
45857c478bd9Sstevel@tonic-gate 	} else {
45867c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
45877c478bd9Sstevel@tonic-gate 		    "port%d disconnected", port);
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_CCS & ack_flag);
45907c478bd9Sstevel@tonic-gate 	}
45917c478bd9Sstevel@tonic-gate 
45927c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PES) {
45937c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
45947c478bd9Sstevel@tonic-gate 		    "port%d enabled", port);
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PES & ack_flag);
45977c478bd9Sstevel@tonic-gate 	} else {
45987c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
45997c478bd9Sstevel@tonic-gate 		    "port%d disabled", port);
46007c478bd9Sstevel@tonic-gate 
46017c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PES & ack_flag);
46027c478bd9Sstevel@tonic-gate 	}
46037c478bd9Sstevel@tonic-gate 
46047c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PSS) {
46057c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46067c478bd9Sstevel@tonic-gate 		    "port%d suspended", port);
46077c478bd9Sstevel@tonic-gate 
46087c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PSS & ack_flag);
46097c478bd9Sstevel@tonic-gate 	} else {
46107c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46117c478bd9Sstevel@tonic-gate 		    "port%d not suspended", port);
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PSS & ack_flag);
46147c478bd9Sstevel@tonic-gate 	}
46157c478bd9Sstevel@tonic-gate 
46167c478bd9Sstevel@tonic-gate 	if (*change & PORT_CHANGE_PRSC) {
46177c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46187c478bd9Sstevel@tonic-gate 		    "port%d reset completed", port);
46197c478bd9Sstevel@tonic-gate 
46207c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_CHANGE_PRSC & ack_flag);
46217c478bd9Sstevel@tonic-gate 	} else {
46227c478bd9Sstevel@tonic-gate 
46237c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_CHANGE_PRSC & ack_flag);
46247c478bd9Sstevel@tonic-gate 	}
46257c478bd9Sstevel@tonic-gate 
46267c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_POCI) {
4627d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4628d291d9f2Sfrits 		    "port%d overcurrent!", port);
46297c478bd9Sstevel@tonic-gate 
46307c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_POCI & ack_flag);
46317c478bd9Sstevel@tonic-gate 	} else {
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_POCI & ack_flag);
46347c478bd9Sstevel@tonic-gate 	}
46357c478bd9Sstevel@tonic-gate 
46367c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PRS) {
46377c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46387c478bd9Sstevel@tonic-gate 		    "port%d reset active", port);
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PRS & ack_flag);
46417c478bd9Sstevel@tonic-gate 	} else {
46427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46437c478bd9Sstevel@tonic-gate 		    "port%d reset inactive", port);
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PRS & ack_flag);
46467c478bd9Sstevel@tonic-gate 	}
46477c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PPS) {
46487c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46497c478bd9Sstevel@tonic-gate 		    "port%d power on", port);
46507c478bd9Sstevel@tonic-gate 
46517c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PPS & ack_flag);
46527c478bd9Sstevel@tonic-gate 	} else {
46537c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46547c478bd9Sstevel@tonic-gate 		    "port%d power off", port);
46557c478bd9Sstevel@tonic-gate 
46567c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PPS & ack_flag);
46577c478bd9Sstevel@tonic-gate 	}
46587c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_LSDA) {
46597c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46607c478bd9Sstevel@tonic-gate 		    "port%d low speed", port);
46617c478bd9Sstevel@tonic-gate 
46627c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_LSDA & ack_flag);
46637c478bd9Sstevel@tonic-gate 	} else {
46647c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_LSDA & ack_flag);
46657c478bd9Sstevel@tonic-gate 		if (*status & PORT_STATUS_HSDA) {
46667c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT,
46677c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle, "port%d "
46687c478bd9Sstevel@tonic-gate 			    "high speed", port);
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] |=
46717c478bd9Sstevel@tonic-gate 					(PORT_STATUS_HSDA & ack_flag);
46727c478bd9Sstevel@tonic-gate 		} else {
46737c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT,
46747c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle, "port%d "
46757c478bd9Sstevel@tonic-gate 			    "full speed", port);
46767c478bd9Sstevel@tonic-gate 
46777c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &=
46787c478bd9Sstevel@tonic-gate 					~(PORT_STATUS_HSDA & ack_flag);
46797c478bd9Sstevel@tonic-gate 		}
46807c478bd9Sstevel@tonic-gate 	}
46817c478bd9Sstevel@tonic-gate 
46827c478bd9Sstevel@tonic-gate 	/*
46837c478bd9Sstevel@tonic-gate 	 * Acknowledge connection, enable, reset status
46847c478bd9Sstevel@tonic-gate 	 */
46857c478bd9Sstevel@tonic-gate 	if (ack_flag) {
46867c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
46877c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_CSC & ack_flag) {
46887c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
46897c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_CONNECTION");
46907c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
46917c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
469235f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
46937c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
46947c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_CONNECTION,
46957c478bd9Sstevel@tonic-gate 			    port,
46967c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
46977c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
46987c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
46997c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
47007c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
47017c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_CONNECTION"
47027c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
47037c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
47047c478bd9Sstevel@tonic-gate 			}
47057c478bd9Sstevel@tonic-gate 		}
47067c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PESC & ack_flag) {
47077c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
47087c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_ENABLE");
47097c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
47107c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
471135f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
47127c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
47137c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_ENABLE,
47147c478bd9Sstevel@tonic-gate 			    port,
47157c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
47167c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
47177c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
47187c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
47197c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
47207c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_ENABLE"
47217c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
47227c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
47237c478bd9Sstevel@tonic-gate 			}
47247c478bd9Sstevel@tonic-gate 		}
47257c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PSSC & ack_flag) {
47267c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
47277c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_SUSPEND");
47287c478bd9Sstevel@tonic-gate 
47297c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
47307c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
473135f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
47327c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
47337c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_SUSPEND,
47347c478bd9Sstevel@tonic-gate 			    port,
47357c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
47367c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
47377c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
47387c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
47397c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
47407c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_SUSPEND"
47417c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
47427c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
47437c478bd9Sstevel@tonic-gate 			}
47447c478bd9Sstevel@tonic-gate 		}
47457c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_OCIC & ack_flag) {
47467c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
47477c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_OVER_CURRENT");
47487c478bd9Sstevel@tonic-gate 
47497c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
47507c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
475135f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
47527c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
47537c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_OVER_CURRENT,
47547c478bd9Sstevel@tonic-gate 			    port,
47557c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
47567c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
47577c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
47587c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
47597c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
47607c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_OVER_CURRENT"
47617c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
47627c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
47637c478bd9Sstevel@tonic-gate 			}
47647c478bd9Sstevel@tonic-gate 		}
47657c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PRSC & ack_flag) {
47667c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
47677c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_RESET");
47687c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
47697c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
477035f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
47717c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
47727c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_RESET,
47737c478bd9Sstevel@tonic-gate 			    port,
47747c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
47757c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
47767c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
47777c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
47787c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
47797c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_RESET"
47807c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
47817c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
47827c478bd9Sstevel@tonic-gate 			}
47837c478bd9Sstevel@tonic-gate 		}
47847c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
47857c478bd9Sstevel@tonic-gate 	}
47867c478bd9Sstevel@tonic-gate 
47877c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
47887c478bd9Sstevel@tonic-gate 	    "new port%d state 0x%x", port, hubd->h_port_state[port]);
47897c478bd9Sstevel@tonic-gate 
47907c478bd9Sstevel@tonic-gate 
47917c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
47927c478bd9Sstevel@tonic-gate }
47937c478bd9Sstevel@tonic-gate 
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate /*
47967c478bd9Sstevel@tonic-gate  * hubd_recover_disabled_port
47977c478bd9Sstevel@tonic-gate  * if the port got disabled because of an error
47987c478bd9Sstevel@tonic-gate  * enable it. If hub doesn't suport enable port,
47997c478bd9Sstevel@tonic-gate  * reset the port to bring the device to life again
48007c478bd9Sstevel@tonic-gate  */
48017c478bd9Sstevel@tonic-gate static int
48027c478bd9Sstevel@tonic-gate hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port)
48037c478bd9Sstevel@tonic-gate {
48047c478bd9Sstevel@tonic-gate 	uint16_t	status;
48057c478bd9Sstevel@tonic-gate 	uint16_t	change;
48067c478bd9Sstevel@tonic-gate 	int		rval = USB_FAILURE;
48077c478bd9Sstevel@tonic-gate 
48087c478bd9Sstevel@tonic-gate 	/* first try enabling the port */
48097c478bd9Sstevel@tonic-gate 	(void) hubd_enable_port(hubd, port);
48107c478bd9Sstevel@tonic-gate 
48117c478bd9Sstevel@tonic-gate 	/* read the port status */
48127c478bd9Sstevel@tonic-gate 	(void) hubd_determine_port_status(hubd, port, &status, &change,
48137c478bd9Sstevel@tonic-gate 	    PORT_CHANGE_PESC);
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 	if (status & PORT_STATUS_PES) {
48167c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
48177c478bd9Sstevel@tonic-gate 			"Port%d now Enabled", port);
48187c478bd9Sstevel@tonic-gate 	} else if (status & PORT_STATUS_CCS) {
48197c478bd9Sstevel@tonic-gate 		/* first post a disconnect event to the child */
48207c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
48217c478bd9Sstevel@tonic-gate 		hubd_post_event(hubd, port, USBA_EVENT_TAG_HOT_REMOVAL);
48227c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
48237c478bd9Sstevel@tonic-gate 
48247c478bd9Sstevel@tonic-gate 		/* then reset the port and recover the device */
48257c478bd9Sstevel@tonic-gate 		rval = hubd_handle_port_connect(hubd, port);
48267c478bd9Sstevel@tonic-gate 
48277c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
48287c478bd9Sstevel@tonic-gate 			"Port%d now Enabled by force", port);
48297c478bd9Sstevel@tonic-gate 	}
48307c478bd9Sstevel@tonic-gate 
48317c478bd9Sstevel@tonic-gate 	return (rval);
48327c478bd9Sstevel@tonic-gate }
48337c478bd9Sstevel@tonic-gate 
48347c478bd9Sstevel@tonic-gate 
48357c478bd9Sstevel@tonic-gate /*
48367c478bd9Sstevel@tonic-gate  * hubd_enable_all_port_power:
48377c478bd9Sstevel@tonic-gate  */
48387c478bd9Sstevel@tonic-gate static int
48397c478bd9Sstevel@tonic-gate hubd_enable_all_port_power(hubd_t *hubd)
48407c478bd9Sstevel@tonic-gate {
48417c478bd9Sstevel@tonic-gate 	usb_hub_descr_t	*hub_descr;
48427c478bd9Sstevel@tonic-gate 	int		wait;
48437c478bd9Sstevel@tonic-gate 	usb_port_t	port;
48447c478bd9Sstevel@tonic-gate 	uint_t		retry;
48457c478bd9Sstevel@tonic-gate 	uint16_t	status;
48467c478bd9Sstevel@tonic-gate 	uint16_t	change;
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
48497c478bd9Sstevel@tonic-gate 	    "hubd_enable_all_port_power");
48507c478bd9Sstevel@tonic-gate 
48517c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	hub_descr = &hubd->h_hub_descr;
48547c478bd9Sstevel@tonic-gate 
48557c478bd9Sstevel@tonic-gate 	/*
48567c478bd9Sstevel@tonic-gate 	 * According to section 11.11 of USB, for hubs with no power
48577c478bd9Sstevel@tonic-gate 	 * switches, bPwrOn2PwrGood is zero. But we wait for some
48587c478bd9Sstevel@tonic-gate 	 * arbitrary time to enable power to become stable.
48597c478bd9Sstevel@tonic-gate 	 *
48607c478bd9Sstevel@tonic-gate 	 * If an hub supports port power switching, we need to wait
48617c478bd9Sstevel@tonic-gate 	 * at least 20ms before accessing corresponding usb port.
48627c478bd9Sstevel@tonic-gate 	 */
48637c478bd9Sstevel@tonic-gate 	if ((hub_descr->wHubCharacteristics &
48647c478bd9Sstevel@tonic-gate 	    HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) {
48657c478bd9Sstevel@tonic-gate 		wait = hubd_device_delay / 10;
48667c478bd9Sstevel@tonic-gate 	} else {
48677c478bd9Sstevel@tonic-gate 		wait = max(HUB_DEFAULT_POPG,
48687c478bd9Sstevel@tonic-gate 		    hub_descr->bPwrOn2PwrGood) * 2 * 1000;
48697c478bd9Sstevel@tonic-gate 	}
48707c478bd9Sstevel@tonic-gate 
48717c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
48727c478bd9Sstevel@tonic-gate 	    "hubd_enable_all_port_power: popg=%d wait=%d",
48737c478bd9Sstevel@tonic-gate 	    hub_descr->bPwrOn2PwrGood, wait);
48747c478bd9Sstevel@tonic-gate 
48757c478bd9Sstevel@tonic-gate 	/*
48767c478bd9Sstevel@tonic-gate 	 * Enable power per port. we ignore gang power and power mask
48777c478bd9Sstevel@tonic-gate 	 * and always enable all ports one by one.
48787c478bd9Sstevel@tonic-gate 	 */
48797c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hub_descr->bNbrPorts; port++) {
48807c478bd9Sstevel@tonic-gate 		/*
48817c478bd9Sstevel@tonic-gate 		 * Transition the port from the Powered Off to the
48827c478bd9Sstevel@tonic-gate 		 * Disconnected state by supplying power to the port.
48837c478bd9Sstevel@tonic-gate 		 */
48847c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PORT,
48857c478bd9Sstevel@tonic-gate 		    hubd->h_log_handle,
48867c478bd9Sstevel@tonic-gate 		    "hubd_enable_all_port_power: power port=%d", port);
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port_power(hubd, port);
48897c478bd9Sstevel@tonic-gate 	}
48907c478bd9Sstevel@tonic-gate 
48917c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
48927c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(wait));
48937c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
48947c478bd9Sstevel@tonic-gate 
48957c478bd9Sstevel@tonic-gate 	/* For retry if any, use some extra delay */
48967c478bd9Sstevel@tonic-gate 	wait = max(wait, hubd_device_delay / 10);
48977c478bd9Sstevel@tonic-gate 
48987c478bd9Sstevel@tonic-gate 	/* Check each port power status for a given usb hub */
48997c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hub_descr->bNbrPorts; port++) {
49007c478bd9Sstevel@tonic-gate 
49017c478bd9Sstevel@tonic-gate 		/* Get port status */
49027c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd, port,
49037c478bd9Sstevel@tonic-gate 		    &status, &change, 0);
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate 		for (retry = 0; ((!(status & PORT_STATUS_PPS)) &&
49067c478bd9Sstevel@tonic-gate 		    (retry < HUBD_PORT_RETRY)); retry++) {
49077c478bd9Sstevel@tonic-gate 
49087c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
49097c478bd9Sstevel@tonic-gate 			    "Retry is in progress %d: port %d status %d",
49107c478bd9Sstevel@tonic-gate 			    retry, port, status);
49117c478bd9Sstevel@tonic-gate 
49127c478bd9Sstevel@tonic-gate 			(void) hubd_enable_port_power(hubd, port);
49137c478bd9Sstevel@tonic-gate 
49147c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
49157c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(wait));
49167c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 			/* Get port status */
49197c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
49207c478bd9Sstevel@tonic-gate 			    &status, &change, 0);
49217c478bd9Sstevel@tonic-gate 		}
49227c478bd9Sstevel@tonic-gate 
49237c478bd9Sstevel@tonic-gate 		/* Print warning message if port has no power */
49247c478bd9Sstevel@tonic-gate 		if (!(status & PORT_STATUS_PPS)) {
49257c478bd9Sstevel@tonic-gate 
4926d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
49277c478bd9Sstevel@tonic-gate 			    "hubd_enable_all_port_power: port %d power-on "
49287c478bd9Sstevel@tonic-gate 			    "failed, port status 0x%x", port, status);
49297c478bd9Sstevel@tonic-gate 		}
49307c478bd9Sstevel@tonic-gate 	}
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
49337c478bd9Sstevel@tonic-gate }
49347c478bd9Sstevel@tonic-gate 
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate /*
49377c478bd9Sstevel@tonic-gate  * hubd_enable_port_power:
49387c478bd9Sstevel@tonic-gate  *	enable individual port power
49397c478bd9Sstevel@tonic-gate  */
49407c478bd9Sstevel@tonic-gate static int
49417c478bd9Sstevel@tonic-gate hubd_enable_port_power(hubd_t *hubd, usb_port_t port)
49427c478bd9Sstevel@tonic-gate {
49437c478bd9Sstevel@tonic-gate 	int		rval;
49447c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
49457c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
49467c478bd9Sstevel@tonic-gate 
49477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
49487c478bd9Sstevel@tonic-gate 	    "hubd_enable_port_power: port=%d", port);
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
49517c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_default_pipe != 0);
49527c478bd9Sstevel@tonic-gate 
49537c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
49547c478bd9Sstevel@tonic-gate 
49557c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
49567c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
495735f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
49587c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
49597c478bd9Sstevel@tonic-gate 	    CFS_PORT_POWER,
49607c478bd9Sstevel@tonic-gate 	    port,
49617c478bd9Sstevel@tonic-gate 	    0, NULL, 0,
49627c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
49637c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
49647c478bd9Sstevel@tonic-gate 		    "set port power failed (%d 0x%x %d)",
49657c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
49667c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
49677c478bd9Sstevel@tonic-gate 
49687c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
49697c478bd9Sstevel@tonic-gate 	} else {
49707c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
49717c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= PORT_STATUS_PPS;
49727c478bd9Sstevel@tonic-gate 
49737c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
49747c478bd9Sstevel@tonic-gate 	}
49757c478bd9Sstevel@tonic-gate }
49767c478bd9Sstevel@tonic-gate 
49777c478bd9Sstevel@tonic-gate 
49787c478bd9Sstevel@tonic-gate /*
49797c478bd9Sstevel@tonic-gate  * hubd_disable_all_port_power:
49807c478bd9Sstevel@tonic-gate  */
49817c478bd9Sstevel@tonic-gate static int
49827c478bd9Sstevel@tonic-gate hubd_disable_all_port_power(hubd_t *hubd)
49837c478bd9Sstevel@tonic-gate {
49847c478bd9Sstevel@tonic-gate 	usb_port_t port;
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
49877c478bd9Sstevel@tonic-gate 	    "hubd_disable_all_port_power");
49887c478bd9Sstevel@tonic-gate 
49897c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
49907c478bd9Sstevel@tonic-gate 
49917c478bd9Sstevel@tonic-gate 	/*
49927c478bd9Sstevel@tonic-gate 	 * disable power per port, ignore gang power and power mask
49937c478bd9Sstevel@tonic-gate 	 */
49947c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
49957c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port_power(hubd, port);
49967c478bd9Sstevel@tonic-gate 	}
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
49997c478bd9Sstevel@tonic-gate }
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 
50027c478bd9Sstevel@tonic-gate /*
50037c478bd9Sstevel@tonic-gate  * hubd_disable_port_power:
50047c478bd9Sstevel@tonic-gate  *	disable individual port power
50057c478bd9Sstevel@tonic-gate  */
50067c478bd9Sstevel@tonic-gate static int
50077c478bd9Sstevel@tonic-gate hubd_disable_port_power(hubd_t *hubd, usb_port_t port)
50087c478bd9Sstevel@tonic-gate {
50097c478bd9Sstevel@tonic-gate 	int		rval;
50107c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
50117c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
50127c478bd9Sstevel@tonic-gate 
50137c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
50147c478bd9Sstevel@tonic-gate 	    "hubd_disable_port_power: port=%d", port);
50157c478bd9Sstevel@tonic-gate 
50167c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
50217c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
502235f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
50237c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
50247c478bd9Sstevel@tonic-gate 	    CFS_PORT_POWER,
50257c478bd9Sstevel@tonic-gate 	    port,
50267c478bd9Sstevel@tonic-gate 	    0, NULL, 0,
50277c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
50287c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
50297c478bd9Sstevel@tonic-gate 		    "clearing port%d power failed (%d 0x%x %d)",
50307c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
50317c478bd9Sstevel@tonic-gate 
50327c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
50337c478bd9Sstevel@tonic-gate 
50347c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
50357c478bd9Sstevel@tonic-gate 	} else {
50367c478bd9Sstevel@tonic-gate 
50377c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
50387c478bd9Sstevel@tonic-gate 		ASSERT(completion_reason == 0);
50397c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~PORT_STATUS_PPS;
50407c478bd9Sstevel@tonic-gate 
50417c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
50427c478bd9Sstevel@tonic-gate 	}
50437c478bd9Sstevel@tonic-gate }
50447c478bd9Sstevel@tonic-gate 
50457c478bd9Sstevel@tonic-gate 
50467c478bd9Sstevel@tonic-gate /*
50477c478bd9Sstevel@tonic-gate  * Search the database of user preferences and find out the preferred
50487c478bd9Sstevel@tonic-gate  * configuration for this new device
50497c478bd9Sstevel@tonic-gate  */
50507c478bd9Sstevel@tonic-gate static int
50517c478bd9Sstevel@tonic-gate hubd_select_device_configuration(hubd_t *hubd, usb_port_t port,
50527c478bd9Sstevel@tonic-gate 	dev_info_t *child_dip, usba_device_t *child_ud)
50537c478bd9Sstevel@tonic-gate {
50547c478bd9Sstevel@tonic-gate 	char		*pathname = NULL;
50557c478bd9Sstevel@tonic-gate 	char		*tmp_path = NULL;
50567c478bd9Sstevel@tonic-gate 	int		user_conf;
50577c478bd9Sstevel@tonic-gate 	int		pathlen;
50587c478bd9Sstevel@tonic-gate 	usb_dev_descr_t	*usbdev_ptr;
50597c478bd9Sstevel@tonic-gate 	usba_configrec_t *user_pref;
50607c478bd9Sstevel@tonic-gate 
50617c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
50627c478bd9Sstevel@tonic-gate 	usbdev_ptr = child_ud->usb_dev_descr;
50637c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
50647c478bd9Sstevel@tonic-gate 
50657c478bd9Sstevel@tonic-gate 	/* try to get pathname for this device */
50667c478bd9Sstevel@tonic-gate 	tmp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
50677c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(child_dip, tmp_path);
50687c478bd9Sstevel@tonic-gate 
50697c478bd9Sstevel@tonic-gate 	pathlen = strlen(tmp_path) + 32;
50707c478bd9Sstevel@tonic-gate 	pathname = kmem_zalloc(pathlen, KM_SLEEP);
50717c478bd9Sstevel@tonic-gate 
50727c478bd9Sstevel@tonic-gate 	/*
50737c478bd9Sstevel@tonic-gate 	 * We haven't initialized the node and it doesn't have an address
50747c478bd9Sstevel@tonic-gate 	 * yet. Append port number to the physical pathname
50757c478bd9Sstevel@tonic-gate 	 */
50767c478bd9Sstevel@tonic-gate 	(void) sprintf(pathname, "%s@%d", tmp_path, port);
50777c478bd9Sstevel@tonic-gate 
50787c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
50797c478bd9Sstevel@tonic-gate 	    "hubd_select_device_configuration: Device=%s\n\t"
50807c478bd9Sstevel@tonic-gate 	    "Child path=%s",
50817c478bd9Sstevel@tonic-gate 	    usba_get_mfg_prod_sn_str(child_dip, tmp_path, MAXPATHLEN),
50827c478bd9Sstevel@tonic-gate 	    pathname);
50837c478bd9Sstevel@tonic-gate 	kmem_free(tmp_path, MAXPATHLEN);
50847c478bd9Sstevel@tonic-gate 
50857c478bd9Sstevel@tonic-gate 
50867c478bd9Sstevel@tonic-gate 	/* database search for user preferences */
50877c478bd9Sstevel@tonic-gate 	user_pref = usba_devdb_get_user_preferences(usbdev_ptr->idVendor,
50887c478bd9Sstevel@tonic-gate 	    usbdev_ptr->idProduct, child_ud->usb_serialno_str, pathname);
50897c478bd9Sstevel@tonic-gate 
50907c478bd9Sstevel@tonic-gate 	if (user_pref) {
50917c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
50927c478bd9Sstevel@tonic-gate 		    "hubd_select_device_configuration: "
50937c478bd9Sstevel@tonic-gate 		    "usba_devdb_get_user_preferences "
50947c478bd9Sstevel@tonic-gate 		    "return user_conf=%d\npreferred driver=%s path=%s",
50957c478bd9Sstevel@tonic-gate 		    user_pref->cfg_index, user_pref->driver,
50967c478bd9Sstevel@tonic-gate 		    user_pref->pathname);
50977c478bd9Sstevel@tonic-gate 
50987c478bd9Sstevel@tonic-gate 		user_conf = user_pref->cfg_index;
50997c478bd9Sstevel@tonic-gate 
51007c478bd9Sstevel@tonic-gate 		if (user_pref->driver) {
51017c478bd9Sstevel@tonic-gate 			mutex_enter(&child_ud->usb_mutex);
51027c478bd9Sstevel@tonic-gate 			child_ud->usb_preferred_driver = user_pref->driver;
51037c478bd9Sstevel@tonic-gate 			mutex_exit(&child_ud->usb_mutex);
51047c478bd9Sstevel@tonic-gate 		}
51057c478bd9Sstevel@tonic-gate 	} else {
51067c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
51077c478bd9Sstevel@tonic-gate 		    "hubd_select_device_configuration: No match found");
51087c478bd9Sstevel@tonic-gate 
51097c478bd9Sstevel@tonic-gate 		/* select default configuration for this device */
51107c478bd9Sstevel@tonic-gate 		user_conf = USBA_DEV_CONFIG_INDEX_UNDEFINED;
51117c478bd9Sstevel@tonic-gate 	}
51127c478bd9Sstevel@tonic-gate 	kmem_free(pathname, pathlen);
51137c478bd9Sstevel@tonic-gate 
51147c478bd9Sstevel@tonic-gate 	/* if the device has just one configuration, set default value */
51157c478bd9Sstevel@tonic-gate 	if (usbdev_ptr->bNumConfigurations == 1) {
51167c478bd9Sstevel@tonic-gate 		user_conf = USB_DEV_DEFAULT_CONFIG_INDEX;
51177c478bd9Sstevel@tonic-gate 	}
51187c478bd9Sstevel@tonic-gate 
51197c478bd9Sstevel@tonic-gate 	return (user_conf);
51207c478bd9Sstevel@tonic-gate }
51217c478bd9Sstevel@tonic-gate 
51227c478bd9Sstevel@tonic-gate 
51237c478bd9Sstevel@tonic-gate /*
51247c478bd9Sstevel@tonic-gate  * Retrieves config cloud for this configuration
51257c478bd9Sstevel@tonic-gate  */
51267c478bd9Sstevel@tonic-gate int
51277c478bd9Sstevel@tonic-gate hubd_get_this_config_cloud(hubd_t *hubd, dev_info_t *dip,
51287c478bd9Sstevel@tonic-gate 	usba_device_t *child_ud, uint16_t conf_index)
51297c478bd9Sstevel@tonic-gate {
51307c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	*confdescr;
51317c478bd9Sstevel@tonic-gate 	mblk_t		*pdata = NULL;
51327c478bd9Sstevel@tonic-gate 	int		rval;
51337c478bd9Sstevel@tonic-gate 	size_t		size;
51347c478bd9Sstevel@tonic-gate 	char		*tmpbuf;
51357c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
51367c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
51377c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	def_ph;
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
51407c478bd9Sstevel@tonic-gate 	    "hubd_get_this_config_cloud: conf_index=%d", conf_index);
51417c478bd9Sstevel@tonic-gate 
51427c478bd9Sstevel@tonic-gate 
51437c478bd9Sstevel@tonic-gate 	/* alloc temporary space for config descriptor */
51447c478bd9Sstevel@tonic-gate 	confdescr = (usb_cfg_descr_t *)kmem_zalloc(USB_CFG_DESCR_SIZE,
51457c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
51467c478bd9Sstevel@tonic-gate 
51477c478bd9Sstevel@tonic-gate 	/* alloc temporary space for string descriptor */
51487c478bd9Sstevel@tonic-gate 	tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
51497c478bd9Sstevel@tonic-gate 
51507c478bd9Sstevel@tonic-gate 	def_ph = usba_get_dflt_pipe_handle(dip);
51517c478bd9Sstevel@tonic-gate 
51527c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
51537c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
51547c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,
51557c478bd9Sstevel@tonic-gate 	    USB_DESCR_TYPE_SETUP_CFG | conf_index,
51567c478bd9Sstevel@tonic-gate 	    0,
51577c478bd9Sstevel@tonic-gate 	    USB_CFG_DESCR_SIZE,
51587c478bd9Sstevel@tonic-gate 	    &pdata,
51597c478bd9Sstevel@tonic-gate 	    0,
51607c478bd9Sstevel@tonic-gate 	    &completion_reason,
51617c478bd9Sstevel@tonic-gate 	    &cb_flags,
51627c478bd9Sstevel@tonic-gate 	    0)) == USB_SUCCESS) {
51637c478bd9Sstevel@tonic-gate 
51647c478bd9Sstevel@tonic-gate 		/* this must be true since we didn't allow data underruns */
516535f36846Ssl 		if ((pdata->b_wptr - pdata->b_rptr) != USB_CFG_DESCR_SIZE) {
516635f36846Ssl 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
516735f36846Ssl 			    "device returned incorrect configuration "
516835f36846Ssl 			    "descriptor size.");
516935f36846Ssl 
517035f36846Ssl 			rval = USB_FAILURE;
517135f36846Ssl 			goto done;
517235f36846Ssl 		}
51737c478bd9Sstevel@tonic-gate 
51747c478bd9Sstevel@tonic-gate 		/*
51757c478bd9Sstevel@tonic-gate 		 * Parse the configuration descriptor
51767c478bd9Sstevel@tonic-gate 		 */
51777c478bd9Sstevel@tonic-gate 		size = usb_parse_cfg_descr(pdata->b_rptr,
51787c478bd9Sstevel@tonic-gate 		    pdata->b_wptr - pdata->b_rptr, confdescr,
51797c478bd9Sstevel@tonic-gate 		    USB_CFG_DESCR_SIZE);
51807c478bd9Sstevel@tonic-gate 
51817c478bd9Sstevel@tonic-gate 		/* if parse cfg descr error, it should return failure */
51827c478bd9Sstevel@tonic-gate 		if (size == USB_PARSE_ERROR) {
51837c478bd9Sstevel@tonic-gate 
51847c478bd9Sstevel@tonic-gate 			if (pdata->b_rptr[1] != USB_DESCR_TYPE_CFG) {
5185d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
51867c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
51877c478bd9Sstevel@tonic-gate 				    "device returned incorrect "
51887c478bd9Sstevel@tonic-gate 				    "configuration descriptor type.");
51897c478bd9Sstevel@tonic-gate 			}
51907c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
51917c478bd9Sstevel@tonic-gate 			goto done;
51927c478bd9Sstevel@tonic-gate 		}
51937c478bd9Sstevel@tonic-gate 
51947c478bd9Sstevel@tonic-gate 		if (confdescr->wTotalLength < USB_CFG_DESCR_SIZE) {
5195d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
51967c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
51977c478bd9Sstevel@tonic-gate 			    "device returned incorrect "
51987c478bd9Sstevel@tonic-gate 			    "configuration descriptor size.");
51997c478bd9Sstevel@tonic-gate 
52007c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
52017c478bd9Sstevel@tonic-gate 			goto done;
52027c478bd9Sstevel@tonic-gate 		}
52037c478bd9Sstevel@tonic-gate 
52047c478bd9Sstevel@tonic-gate 		freemsg(pdata);
52057c478bd9Sstevel@tonic-gate 		pdata = NULL;
52067c478bd9Sstevel@tonic-gate 
52077c478bd9Sstevel@tonic-gate 		/* Now fetch the complete config cloud */
52087c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
52097c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
52107c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,
52117c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_CFG | conf_index,
52127c478bd9Sstevel@tonic-gate 		    0,
52137c478bd9Sstevel@tonic-gate 		    confdescr->wTotalLength,
52147c478bd9Sstevel@tonic-gate 		    &pdata,
52157c478bd9Sstevel@tonic-gate 		    0,
52167c478bd9Sstevel@tonic-gate 		    &completion_reason,
52177c478bd9Sstevel@tonic-gate 		    &cb_flags,
52187c478bd9Sstevel@tonic-gate 		    0)) == USB_SUCCESS) {
52197c478bd9Sstevel@tonic-gate 
52207c478bd9Sstevel@tonic-gate 			if ((pdata->b_wptr - pdata->b_rptr) !=
52217c478bd9Sstevel@tonic-gate 			    confdescr->wTotalLength) {
52227c478bd9Sstevel@tonic-gate 
5223d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
52247c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
52257c478bd9Sstevel@tonic-gate 				    "device returned incorrect "
52267c478bd9Sstevel@tonic-gate 				    "configuration descriptor.");
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate 				rval = USB_FAILURE;
52297c478bd9Sstevel@tonic-gate 				goto done;
52307c478bd9Sstevel@tonic-gate 			}
52317c478bd9Sstevel@tonic-gate 
52327c478bd9Sstevel@tonic-gate 			/*
52337c478bd9Sstevel@tonic-gate 			 * copy config descriptor into usba_device
52347c478bd9Sstevel@tonic-gate 			 */
52357c478bd9Sstevel@tonic-gate 			mutex_enter(&child_ud->usb_mutex);
52367c478bd9Sstevel@tonic-gate 			child_ud->usb_cfg_array[conf_index] =
52377c478bd9Sstevel@tonic-gate 			    kmem_alloc(confdescr->wTotalLength, KM_SLEEP);
52387c478bd9Sstevel@tonic-gate 			child_ud->usb_cfg_array_len[conf_index] =
52397c478bd9Sstevel@tonic-gate 					confdescr->wTotalLength;
52407c478bd9Sstevel@tonic-gate 			bcopy((caddr_t)pdata->b_rptr,
52417c478bd9Sstevel@tonic-gate 			    (caddr_t)child_ud->usb_cfg_array[conf_index],
52427c478bd9Sstevel@tonic-gate 			    confdescr->wTotalLength);
52437c478bd9Sstevel@tonic-gate 			mutex_exit(&child_ud->usb_mutex);
52447c478bd9Sstevel@tonic-gate 
52457c478bd9Sstevel@tonic-gate 			/*
52467c478bd9Sstevel@tonic-gate 			 * retrieve string descriptor describing this
52477c478bd9Sstevel@tonic-gate 			 * configuration
52487c478bd9Sstevel@tonic-gate 			 */
52497c478bd9Sstevel@tonic-gate 			if (confdescr->iConfiguration) {
52507c478bd9Sstevel@tonic-gate 
52517c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
52527c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
52537c478bd9Sstevel@tonic-gate 				    "Get conf str descr for config_index=%d",
52547c478bd9Sstevel@tonic-gate 				    conf_index);
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate 				/*
52577c478bd9Sstevel@tonic-gate 				 * Now fetch the string descriptor describing
52587c478bd9Sstevel@tonic-gate 				 * this configuration
52597c478bd9Sstevel@tonic-gate 				 */
52607c478bd9Sstevel@tonic-gate 				if ((rval = usb_get_string_descr(dip,
52617c478bd9Sstevel@tonic-gate 				    USB_LANG_ID, confdescr->iConfiguration,
52627c478bd9Sstevel@tonic-gate 				    tmpbuf, USB_MAXSTRINGLEN)) ==
52637c478bd9Sstevel@tonic-gate 				    USB_SUCCESS) {
52647c478bd9Sstevel@tonic-gate 					size = strlen(tmpbuf);
52657c478bd9Sstevel@tonic-gate 					if (size > 0) {
52667c478bd9Sstevel@tonic-gate 						child_ud->usb_cfg_str_descr
52677c478bd9Sstevel@tonic-gate 						    [conf_index] = (char *)
52687c478bd9Sstevel@tonic-gate 						    kmem_zalloc(size + 1,
52697c478bd9Sstevel@tonic-gate 						    KM_SLEEP);
52707c478bd9Sstevel@tonic-gate 						(void) strcpy(
52717c478bd9Sstevel@tonic-gate 						    child_ud->usb_cfg_str_descr
52727c478bd9Sstevel@tonic-gate 						    [conf_index], tmpbuf);
52737c478bd9Sstevel@tonic-gate 					}
52747c478bd9Sstevel@tonic-gate 				} else {
52757c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
52767c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
52777c478bd9Sstevel@tonic-gate 					    "hubd_get_this_config_cloud: "
52787c478bd9Sstevel@tonic-gate 					    "getting config string (%d) "
52797c478bd9Sstevel@tonic-gate 					    "failed",
52807c478bd9Sstevel@tonic-gate 					    confdescr->iConfiguration);
52817c478bd9Sstevel@tonic-gate 
52827c478bd9Sstevel@tonic-gate 					/* ignore this error */
52837c478bd9Sstevel@tonic-gate 					rval = USB_SUCCESS;
52847c478bd9Sstevel@tonic-gate 				}
52857c478bd9Sstevel@tonic-gate 			}
52867c478bd9Sstevel@tonic-gate 		}
52877c478bd9Sstevel@tonic-gate 	}
52887c478bd9Sstevel@tonic-gate 
52897c478bd9Sstevel@tonic-gate done:
52907c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
52917c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
52927c478bd9Sstevel@tonic-gate 		    "hubd_get_this_config_cloud: "
52937c478bd9Sstevel@tonic-gate 		    "error in retrieving config descriptor for "
52947c478bd9Sstevel@tonic-gate 		    "config index=%d rval=%d cr=%d",
52957c478bd9Sstevel@tonic-gate 		    conf_index, rval, completion_reason);
52967c478bd9Sstevel@tonic-gate 	}
52977c478bd9Sstevel@tonic-gate 
52987c478bd9Sstevel@tonic-gate 	if (pdata) {
52997c478bd9Sstevel@tonic-gate 		freemsg(pdata);
53007c478bd9Sstevel@tonic-gate 		pdata = NULL;
53017c478bd9Sstevel@tonic-gate 	}
53027c478bd9Sstevel@tonic-gate 
53037c478bd9Sstevel@tonic-gate 	kmem_free(confdescr, USB_CFG_DESCR_SIZE);
53047c478bd9Sstevel@tonic-gate 	kmem_free(tmpbuf, USB_MAXSTRINGLEN);
53057c478bd9Sstevel@tonic-gate 
53067c478bd9Sstevel@tonic-gate 	return (rval);
53077c478bd9Sstevel@tonic-gate }
53087c478bd9Sstevel@tonic-gate 
53097c478bd9Sstevel@tonic-gate 
53107c478bd9Sstevel@tonic-gate /*
53117c478bd9Sstevel@tonic-gate  * Retrieves the entire config cloud for all configurations of the device
53127c478bd9Sstevel@tonic-gate  */
53137c478bd9Sstevel@tonic-gate int
53147c478bd9Sstevel@tonic-gate hubd_get_all_device_config_cloud(hubd_t *hubd, dev_info_t *dip,
53157c478bd9Sstevel@tonic-gate 	usba_device_t *child_ud)
53167c478bd9Sstevel@tonic-gate {
53177c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
53187c478bd9Sstevel@tonic-gate 	int		ncfgs;
53197c478bd9Sstevel@tonic-gate 	uint16_t	size;
53207c478bd9Sstevel@tonic-gate 	uint16_t	conf_index;
53217c478bd9Sstevel@tonic-gate 	uchar_t		**cfg_array;
53227c478bd9Sstevel@tonic-gate 	uint16_t	*cfg_array_len;
53237c478bd9Sstevel@tonic-gate 	char		**str_descr;
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
53267c478bd9Sstevel@tonic-gate 	    "hubd_get_all_device_config_cloud: Start");
53277c478bd9Sstevel@tonic-gate 
53287c478bd9Sstevel@tonic-gate 	/* alloc pointer array for conf. descriptors */
53297c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
53307c478bd9Sstevel@tonic-gate 	ncfgs = child_ud->usb_n_cfgs;
53317c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
53327c478bd9Sstevel@tonic-gate 
53337c478bd9Sstevel@tonic-gate 	size = sizeof (uchar_t *) * ncfgs;
53347c478bd9Sstevel@tonic-gate 	cfg_array = kmem_zalloc(size, KM_SLEEP);
53357c478bd9Sstevel@tonic-gate 	cfg_array_len = kmem_zalloc(ncfgs * sizeof (uint16_t), KM_SLEEP);
53367c478bd9Sstevel@tonic-gate 	str_descr = kmem_zalloc(size, KM_SLEEP);
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
53397c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array = cfg_array;
53407c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_len = cfg_array_len;
53417c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_length = size;
53427c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_len_length = ncfgs * sizeof (uint16_t);
53437c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_str_descr = str_descr;
53447c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
53457c478bd9Sstevel@tonic-gate 
53467c478bd9Sstevel@tonic-gate 	/* Get configuration descriptor for each configuration */
53477c478bd9Sstevel@tonic-gate 	for (conf_index = 0; (conf_index < ncfgs) &&
53487c478bd9Sstevel@tonic-gate 	    (rval == USB_SUCCESS); conf_index++) {
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate 		rval = hubd_get_this_config_cloud(hubd, dip, child_ud,
53517c478bd9Sstevel@tonic-gate 		    conf_index);
53527c478bd9Sstevel@tonic-gate 	}
53537c478bd9Sstevel@tonic-gate 
53547c478bd9Sstevel@tonic-gate 	return (rval);
53557c478bd9Sstevel@tonic-gate }
53567c478bd9Sstevel@tonic-gate 
53577c478bd9Sstevel@tonic-gate 
53587c478bd9Sstevel@tonic-gate /*
53597c478bd9Sstevel@tonic-gate  * hubd_ready_device:
53607c478bd9Sstevel@tonic-gate  *	Update the usba_device structure
53617c478bd9Sstevel@tonic-gate  *	Set the given configuration
53627c478bd9Sstevel@tonic-gate  *	Prepares the device node for driver to online. If an existing
53637c478bd9Sstevel@tonic-gate  *	OBP node is found, it will switch to the OBP node.
53647c478bd9Sstevel@tonic-gate  */
53657c478bd9Sstevel@tonic-gate static dev_info_t *
53667c478bd9Sstevel@tonic-gate hubd_ready_device(hubd_t *hubd, dev_info_t *child_dip, usba_device_t *child_ud,
536735f36846Ssl     uint_t config_index)
53687c478bd9Sstevel@tonic-gate {
53697c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
53707c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
53717c478bd9Sstevel@tonic-gate 	size_t		size;
53727c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	config_descriptor;
53737c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t def_ph;
53747c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph;
53757c478bd9Sstevel@tonic-gate 
53767c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
53777c478bd9Sstevel@tonic-gate 	    "hubd_ready_device: dip=0x%p, user_conf_index=%d", child_dip,
53787c478bd9Sstevel@tonic-gate 	    config_index);
53797c478bd9Sstevel@tonic-gate 
53807c478bd9Sstevel@tonic-gate 	size = usb_parse_cfg_descr(
53817c478bd9Sstevel@tonic-gate 	    child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
53827c478bd9Sstevel@tonic-gate 	    &config_descriptor, USB_CFG_DESCR_SIZE);
53837c478bd9Sstevel@tonic-gate 	ASSERT(size == USB_CFG_DESCR_SIZE);
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate 	def_ph = usba_get_dflt_pipe_handle(child_dip);
53867c478bd9Sstevel@tonic-gate 
53877c478bd9Sstevel@tonic-gate 	/* Set the configuration */
53887c478bd9Sstevel@tonic-gate 	(void) usb_pipe_sync_ctrl_xfer(child_dip, def_ph,
53897c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_HOST_TO_DEV,
53907c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_CFG,	/* bRequest */
53917c478bd9Sstevel@tonic-gate 	    config_descriptor.bConfigurationValue,	/* wValue */
53927c478bd9Sstevel@tonic-gate 	    0,				/* wIndex */
53937c478bd9Sstevel@tonic-gate 	    0,				/* wLength */
53947c478bd9Sstevel@tonic-gate 	    NULL,
53957c478bd9Sstevel@tonic-gate 	    0,
53967c478bd9Sstevel@tonic-gate 	    &completion_reason,
53977c478bd9Sstevel@tonic-gate 	    &cb_flags,
53987c478bd9Sstevel@tonic-gate 	    0);
53997c478bd9Sstevel@tonic-gate 
54007c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
54017c478bd9Sstevel@tonic-gate 	child_ud->usb_active_cfg_ndx	= config_index;
54027c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg		= child_ud->usb_cfg_array[config_index];
54037c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_length	= config_descriptor.wTotalLength;
54047c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_value 	= config_descriptor.bConfigurationValue;
54057c478bd9Sstevel@tonic-gate 	child_ud->usb_n_ifs		= config_descriptor.bNumInterfaces;
54067c478bd9Sstevel@tonic-gate 	child_ud->usb_dip		= child_dip;
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate 	child_ud->usb_client_flags	= kmem_zalloc(
54097c478bd9Sstevel@tonic-gate 		child_ud->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
54107c478bd9Sstevel@tonic-gate 
54117c478bd9Sstevel@tonic-gate 	child_ud->usb_client_attach_list = kmem_zalloc(
54127c478bd9Sstevel@tonic-gate 		child_ud->usb_n_ifs *
54137c478bd9Sstevel@tonic-gate 		sizeof (*child_ud->usb_client_attach_list), KM_SLEEP);
54147c478bd9Sstevel@tonic-gate 
54157c478bd9Sstevel@tonic-gate 	child_ud->usb_client_ev_cb_list = kmem_zalloc(
54167c478bd9Sstevel@tonic-gate 		child_ud->usb_n_ifs *
54177c478bd9Sstevel@tonic-gate 		sizeof (*child_ud->usb_client_ev_cb_list), KM_SLEEP);
54187c478bd9Sstevel@tonic-gate 
54197c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
54207c478bd9Sstevel@tonic-gate 
54217c478bd9Sstevel@tonic-gate 	/* ready the device node */
54227c478bd9Sstevel@tonic-gate 	child_dip = usba_ready_device_node(child_dip);
54237c478bd9Sstevel@tonic-gate 
54247c478bd9Sstevel@tonic-gate 	/* set owner of default pipe to child dip */
54257c478bd9Sstevel@tonic-gate 	ph = usba_get_ph_data(def_ph);
54267c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
54277c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_ph_impl->usba_ph_mutex);
54287c478bd9Sstevel@tonic-gate 	ph->p_ph_impl->usba_ph_dip = ph->p_dip = child_dip;
54297c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_ph_impl->usba_ph_mutex);
54307c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
54317c478bd9Sstevel@tonic-gate 
54327c478bd9Sstevel@tonic-gate 	return (child_dip);
54337c478bd9Sstevel@tonic-gate }
54347c478bd9Sstevel@tonic-gate 
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate /*
54377c478bd9Sstevel@tonic-gate  * hubd_create_child
54387c478bd9Sstevel@tonic-gate  *	- create child dip
54397c478bd9Sstevel@tonic-gate  *	- open default pipe
54407c478bd9Sstevel@tonic-gate  *	- get device descriptor
54417c478bd9Sstevel@tonic-gate  *	- set the address
54427c478bd9Sstevel@tonic-gate  *	- get device string descriptors
54437c478bd9Sstevel@tonic-gate  *	- get the entire config cloud (all configurations) of the device
54447c478bd9Sstevel@tonic-gate  *	- set user preferred configuration
54457c478bd9Sstevel@tonic-gate  *	- close default pipe
54467c478bd9Sstevel@tonic-gate  *	- load appropriate driver(s)
54477c478bd9Sstevel@tonic-gate  */
54487c478bd9Sstevel@tonic-gate static int
54497c478bd9Sstevel@tonic-gate hubd_create_child(dev_info_t *dip,
54507c478bd9Sstevel@tonic-gate 		hubd_t		*hubd,
54517c478bd9Sstevel@tonic-gate 		usba_device_t	*hubd_ud,
54527c478bd9Sstevel@tonic-gate 		usb_port_status_t port_status,
54537c478bd9Sstevel@tonic-gate 		usb_port_t	port,
54547c478bd9Sstevel@tonic-gate 		int		iteration)
54557c478bd9Sstevel@tonic-gate {
54567c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
54577c478bd9Sstevel@tonic-gate 	usb_dev_descr_t	usb_dev_descr;
54587c478bd9Sstevel@tonic-gate 	int			rval;
54597c478bd9Sstevel@tonic-gate 	usba_device_t		*child_ud = NULL;
54607c478bd9Sstevel@tonic-gate 	usba_device_t		*parent_ud = NULL;
54617c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	ph = NULL; /* default pipe handle */
54627c478bd9Sstevel@tonic-gate 	mblk_t			*pdata = NULL;
54637c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
546435f36846Ssl 	int			user_conf_index;
546535f36846Ssl 	uint_t			config_index;
54667c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
54677c478bd9Sstevel@tonic-gate 	uchar_t			address = 0;
54687c478bd9Sstevel@tonic-gate 	uint16_t		length;
54697c478bd9Sstevel@tonic-gate 	size_t			size;
54707c478bd9Sstevel@tonic-gate 	usb_addr_t		parent_usb_addr;
54717c478bd9Sstevel@tonic-gate 	usb_port_t		parent_usb_port;
54727c478bd9Sstevel@tonic-gate 	usba_device_t		*parent_usba_dev;
54737c478bd9Sstevel@tonic-gate 	usb_port_status_t	parent_port_status;
54747c478bd9Sstevel@tonic-gate 
54757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
54767c478bd9Sstevel@tonic-gate 	    "hubd_create_child: port=%d", port);
54777c478bd9Sstevel@tonic-gate 
54787c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
54797c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_usba_devices[port] == NULL);
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
54827c478bd9Sstevel@tonic-gate 
54837c478bd9Sstevel@tonic-gate 	/*
54847c478bd9Sstevel@tonic-gate 	 * create a dip which can be used to open the pipe. we set
54857c478bd9Sstevel@tonic-gate 	 * the name after getting the descriptors from the device
54867c478bd9Sstevel@tonic-gate 	 */
54877c478bd9Sstevel@tonic-gate 	rval = usba_create_child_devi(dip,
54887c478bd9Sstevel@tonic-gate 			"device",		/* driver name */
54897c478bd9Sstevel@tonic-gate 			hubd_ud->usb_hcdi_ops, /* usba_hcdi ops */
54907c478bd9Sstevel@tonic-gate 			hubd_ud->usb_root_hub_dip,
54917c478bd9Sstevel@tonic-gate 			port_status,		/* low speed device */
54927c478bd9Sstevel@tonic-gate 			child_ud,
54937c478bd9Sstevel@tonic-gate 			&child_dip);
54947c478bd9Sstevel@tonic-gate 
54957c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
54967c478bd9Sstevel@tonic-gate 
5497d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
54987c478bd9Sstevel@tonic-gate 		    "usb_create_child_devi failed (%d)", rval);
54997c478bd9Sstevel@tonic-gate 
55007c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
55017c478bd9Sstevel@tonic-gate 	}
55027c478bd9Sstevel@tonic-gate 
55037c478bd9Sstevel@tonic-gate 	child_ud = usba_get_usba_device(child_dip);
55047c478bd9Sstevel@tonic-gate 	ASSERT(child_ud != NULL);
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate 	parent_ud = hubd->h_usba_device;
55077c478bd9Sstevel@tonic-gate 	mutex_enter(&parent_ud->usb_mutex);
55087c478bd9Sstevel@tonic-gate 	parent_port_status = parent_ud->usb_port_status;
55097c478bd9Sstevel@tonic-gate 
55107c478bd9Sstevel@tonic-gate 	/*
55117c478bd9Sstevel@tonic-gate 	 * To support split transactions, update address and port
55127c478bd9Sstevel@tonic-gate 	 * of high speed hub to which given device is connected.
55137c478bd9Sstevel@tonic-gate 	 */
55147c478bd9Sstevel@tonic-gate 	if (parent_port_status == USBA_HIGH_SPEED_DEV) {
55157c478bd9Sstevel@tonic-gate 		parent_usba_dev = parent_ud;
55167c478bd9Sstevel@tonic-gate 		parent_usb_addr = parent_ud->usb_addr;
55177c478bd9Sstevel@tonic-gate 		parent_usb_port = port;
55187c478bd9Sstevel@tonic-gate 	} else {
55197c478bd9Sstevel@tonic-gate 		parent_usba_dev = parent_ud->usb_hs_hub_usba_dev;
55207c478bd9Sstevel@tonic-gate 		parent_usb_addr = parent_ud->usb_hs_hub_addr;
55217c478bd9Sstevel@tonic-gate 		parent_usb_port = parent_ud->usb_hs_hub_port;
55227c478bd9Sstevel@tonic-gate 	}
55237c478bd9Sstevel@tonic-gate 	mutex_exit(&parent_ud->usb_mutex);
55247c478bd9Sstevel@tonic-gate 
55257c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
55267c478bd9Sstevel@tonic-gate 	address = child_ud->usb_addr;
55277c478bd9Sstevel@tonic-gate 	child_ud->usb_addr = 0;
55287c478bd9Sstevel@tonic-gate 	child_ud->usb_dev_descr = kmem_alloc(sizeof (usb_dev_descr_t),
55297c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
55307c478bd9Sstevel@tonic-gate 	bzero(&usb_dev_descr, sizeof (usb_dev_descr_t));
55317c478bd9Sstevel@tonic-gate 	usb_dev_descr.bMaxPacketSize0 =
55327c478bd9Sstevel@tonic-gate 	    (port_status == USBA_LOW_SPEED_DEV) ? 8 : 64;
55337c478bd9Sstevel@tonic-gate 	bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
55347c478bd9Sstevel@tonic-gate 	    sizeof (usb_dev_descr_t));
55357c478bd9Sstevel@tonic-gate 	child_ud->usb_port = port;
55367c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_usba_dev = parent_usba_dev;
55377c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_addr = parent_usb_addr;
55387c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_port = parent_usb_port;
55397c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate 	/* Open the default pipe */
55427c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
55437c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
5544d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5545d291d9f2Sfrits 		    "usb_pipe_open failed (%d)", rval);
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
55487c478bd9Sstevel@tonic-gate 	}
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 	/*
55517c478bd9Sstevel@tonic-gate 	 * get device descriptor
55527c478bd9Sstevel@tonic-gate 	 */
55537c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
55547c478bd9Sstevel@tonic-gate 	    "hubd_create_child: get device descriptor: 64 bytes");
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate 	rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
55577c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
55587c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,			/* bRequest */
55597c478bd9Sstevel@tonic-gate 	    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
55607c478bd9Sstevel@tonic-gate 	    0,					/* wIndex */
55617c478bd9Sstevel@tonic-gate 	    64,					/* wLength */
55627c478bd9Sstevel@tonic-gate 	    &pdata, USB_ATTRS_SHORT_XFER_OK,
55637c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0);
55647c478bd9Sstevel@tonic-gate 
55657c478bd9Sstevel@tonic-gate 	if ((rval != USB_SUCCESS) &&
55667c478bd9Sstevel@tonic-gate 	    (!((completion_reason == USB_CR_DATA_OVERRUN) && pdata))) {
55677c478bd9Sstevel@tonic-gate 
55687c478bd9Sstevel@tonic-gate 		/*
55697c478bd9Sstevel@tonic-gate 		 * rval != USB_SUCCESS AND
55707c478bd9Sstevel@tonic-gate 		 * completion_reason != USB_CR_DATA_OVERRUN
55717c478bd9Sstevel@tonic-gate 		 * pdata could be != NULL.
55727c478bd9Sstevel@tonic-gate 		 * Free pdata now to prevent memory leak.
55737c478bd9Sstevel@tonic-gate 		 */
55747c478bd9Sstevel@tonic-gate 		freemsg(pdata);
55757c478bd9Sstevel@tonic-gate 		pdata = NULL;
55767c478bd9Sstevel@tonic-gate 
55777c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
55787c478bd9Sstevel@tonic-gate 		    "hubd_create_child: get device descriptor: 8 bytes");
55797c478bd9Sstevel@tonic-gate 
55807c478bd9Sstevel@tonic-gate 		rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
55817c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
55827c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
55837c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
55847c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
55857c478bd9Sstevel@tonic-gate 		    8,					/* wLength */
55867c478bd9Sstevel@tonic-gate 		    &pdata, USB_ATTRS_NONE,
55877c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0);
55887c478bd9Sstevel@tonic-gate 
55897c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
5590d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
55917c478bd9Sstevel@tonic-gate 			    "getting device descriptor failed (%s 0x%x %d)",
55927c478bd9Sstevel@tonic-gate 			    usb_str_cr(completion_reason), cb_flags, rval);
55937c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
55947c478bd9Sstevel@tonic-gate 		}
55957c478bd9Sstevel@tonic-gate 	} else {
55967c478bd9Sstevel@tonic-gate 		ASSERT(completion_reason == USB_CR_OK);
55977c478bd9Sstevel@tonic-gate 	}
55987c478bd9Sstevel@tonic-gate 
55997c478bd9Sstevel@tonic-gate 	ASSERT(pdata != NULL);
56007c478bd9Sstevel@tonic-gate 
56017c478bd9Sstevel@tonic-gate 	size = usb_parse_dev_descr(
56027c478bd9Sstevel@tonic-gate 			pdata->b_rptr,
56037c478bd9Sstevel@tonic-gate 			pdata->b_wptr - pdata->b_rptr,
56047c478bd9Sstevel@tonic-gate 			&usb_dev_descr,
56057c478bd9Sstevel@tonic-gate 			sizeof (usb_dev_descr_t));
56067c478bd9Sstevel@tonic-gate 
56077c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56087c478bd9Sstevel@tonic-gate 	    "parsing device descriptor returned %lu", size);
56097c478bd9Sstevel@tonic-gate 
56107c478bd9Sstevel@tonic-gate 	length = *(pdata->b_rptr);
56117c478bd9Sstevel@tonic-gate 	freemsg(pdata);
56127c478bd9Sstevel@tonic-gate 	pdata = NULL;
56137c478bd9Sstevel@tonic-gate 	if (size < 8) {
5614d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56157c478bd9Sstevel@tonic-gate 		    "get device descriptor returned %lu bytes", size);
56167c478bd9Sstevel@tonic-gate 
56177c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
56187c478bd9Sstevel@tonic-gate 	}
56197c478bd9Sstevel@tonic-gate 
56207c478bd9Sstevel@tonic-gate 	if (length < 8) {
5621d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56227c478bd9Sstevel@tonic-gate 		    "fail enumeration: bLength=%d", length);
56237c478bd9Sstevel@tonic-gate 
56247c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
56257c478bd9Sstevel@tonic-gate 	}
56267c478bd9Sstevel@tonic-gate 
56277c478bd9Sstevel@tonic-gate 	/* Set the address of the device */
56287c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
56297c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_HOST_TO_DEV,
56307c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_ADDRESS,	/* bRequest */
56317c478bd9Sstevel@tonic-gate 	    address,			/* wValue */
56327c478bd9Sstevel@tonic-gate 	    0,				/* wIndex */
56337c478bd9Sstevel@tonic-gate 	    0,				/* wLength */
56347c478bd9Sstevel@tonic-gate 	    NULL, 0,
56357c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
56367c478bd9Sstevel@tonic-gate 		char buffer[64];
5637d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56387c478bd9Sstevel@tonic-gate 		    "setting address failed (cr=%s cb_flags=%s rval=%d)",
56397c478bd9Sstevel@tonic-gate 		    usb_str_cr(completion_reason),
56407c478bd9Sstevel@tonic-gate 		    usb_str_cb_flags(cb_flags, buffer, sizeof (buffer)),
56417c478bd9Sstevel@tonic-gate 		    rval);
56427c478bd9Sstevel@tonic-gate 
56437c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
56447c478bd9Sstevel@tonic-gate 	}
56457c478bd9Sstevel@tonic-gate 
56467c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56477c478bd9Sstevel@tonic-gate 	    "set address 0x%x done", address);
56487c478bd9Sstevel@tonic-gate 
56497c478bd9Sstevel@tonic-gate 	/* now close the pipe for addr 0 */
56507c478bd9Sstevel@tonic-gate 	usb_pipe_close(child_dip, ph,
56517c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate 	/*
56547c478bd9Sstevel@tonic-gate 	 * This delay is important for the CATC hub to enumerate
56557c478bd9Sstevel@tonic-gate 	 * But, avoid delay in the first iteration
56567c478bd9Sstevel@tonic-gate 	 */
56577c478bd9Sstevel@tonic-gate 	if (iteration) {
56587c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(hubd_device_delay/100));
56597c478bd9Sstevel@tonic-gate 	}
56607c478bd9Sstevel@tonic-gate 
56617c478bd9Sstevel@tonic-gate 	/* assign the address in the usba_device structure */
56627c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
56637c478bd9Sstevel@tonic-gate 	child_ud->usb_addr = address;
56647c478bd9Sstevel@tonic-gate 	child_ud->usb_no_cpr = 0;
56657c478bd9Sstevel@tonic-gate 	child_ud->usb_port_status = port_status;
56667c478bd9Sstevel@tonic-gate 	/* save this device descriptor */
56677c478bd9Sstevel@tonic-gate 	bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
56687c478bd9Sstevel@tonic-gate 					sizeof (usb_dev_descr_t));
56697c478bd9Sstevel@tonic-gate 	child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
56707c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
56717c478bd9Sstevel@tonic-gate 
56727c478bd9Sstevel@tonic-gate 	/* re-open the pipe for the device with the new address */
56737c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
56747c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
5675d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56767c478bd9Sstevel@tonic-gate 		    "usb_pipe_open failed (%d)", rval);
56777c478bd9Sstevel@tonic-gate 
56787c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
56797c478bd9Sstevel@tonic-gate 	}
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate 	/*
56827c478bd9Sstevel@tonic-gate 	 * Get full device descriptor only if we have not received full
56837c478bd9Sstevel@tonic-gate 	 * device descriptor earlier.
56847c478bd9Sstevel@tonic-gate 	 */
56857c478bd9Sstevel@tonic-gate 	if (size < length) {
56867c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56877c478bd9Sstevel@tonic-gate 		    "hubd_create_child: get full device descriptor: "
56887c478bd9Sstevel@tonic-gate 		    "%d bytes", length);
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
56917c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
56927c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
56937c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
56947c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
56957c478bd9Sstevel@tonic-gate 		    length,				/* wLength */
56967c478bd9Sstevel@tonic-gate 		    &pdata, 0,
56977c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
56987c478bd9Sstevel@tonic-gate 			freemsg(pdata);
56997c478bd9Sstevel@tonic-gate 			pdata = NULL;
57007c478bd9Sstevel@tonic-gate 
57017c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
57027c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
57037c478bd9Sstevel@tonic-gate 			    "hubd_create_child: get full device descriptor: "
57047c478bd9Sstevel@tonic-gate 			    "64 bytes");
57057c478bd9Sstevel@tonic-gate 
57067c478bd9Sstevel@tonic-gate 			rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
57077c478bd9Sstevel@tonic-gate 			    USB_DEV_REQ_DEV_TO_HOST |
57087c478bd9Sstevel@tonic-gate 			    USB_DEV_REQ_TYPE_STANDARD,
57097c478bd9Sstevel@tonic-gate 			    USB_REQ_GET_DESCR,		/* bRequest */
57107c478bd9Sstevel@tonic-gate 			    USB_DESCR_TYPE_SETUP_DEV,	/* wValue */
57117c478bd9Sstevel@tonic-gate 			    0,				/* wIndex */
57127c478bd9Sstevel@tonic-gate 			    64,				/* wLength */
57137c478bd9Sstevel@tonic-gate 			    &pdata, USB_ATTRS_SHORT_XFER_OK,
57147c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0);
57157c478bd9Sstevel@tonic-gate 
57167c478bd9Sstevel@tonic-gate 			/* we have to trust the data now */
57177c478bd9Sstevel@tonic-gate 			if (pdata) {
57187c478bd9Sstevel@tonic-gate 				int len = *(pdata->b_rptr);
57197c478bd9Sstevel@tonic-gate 
57207c478bd9Sstevel@tonic-gate 				length = pdata->b_wptr - pdata->b_rptr;
57217c478bd9Sstevel@tonic-gate 				if (length < len) {
57227c478bd9Sstevel@tonic-gate 
57237c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
57247c478bd9Sstevel@tonic-gate 				}
57257c478bd9Sstevel@tonic-gate 			} else if (rval != USB_SUCCESS) {
5726d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
57277c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
57287c478bd9Sstevel@tonic-gate 				    "getting device descriptor failed "
57297c478bd9Sstevel@tonic-gate 				    "(%d 0x%x %d)",
57307c478bd9Sstevel@tonic-gate 				completion_reason, cb_flags, rval);
57317c478bd9Sstevel@tonic-gate 
57327c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
57337c478bd9Sstevel@tonic-gate 			}
57347c478bd9Sstevel@tonic-gate 		}
57357c478bd9Sstevel@tonic-gate 
57367c478bd9Sstevel@tonic-gate 		size = usb_parse_dev_descr(
57377c478bd9Sstevel@tonic-gate 				pdata->b_rptr,
57387c478bd9Sstevel@tonic-gate 				pdata->b_wptr - pdata->b_rptr,
57397c478bd9Sstevel@tonic-gate 				&usb_dev_descr,
57407c478bd9Sstevel@tonic-gate 				sizeof (usb_dev_descr_t));
57417c478bd9Sstevel@tonic-gate 
57427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
57437c478bd9Sstevel@tonic-gate 		    "parsing device descriptor returned %lu", size);
57447c478bd9Sstevel@tonic-gate 
57457c478bd9Sstevel@tonic-gate 		/*
57467c478bd9Sstevel@tonic-gate 		 * For now, free the data
57477c478bd9Sstevel@tonic-gate 		 * eventually, each configuration may need to be looked at
57487c478bd9Sstevel@tonic-gate 		 */
57497c478bd9Sstevel@tonic-gate 		freemsg(pdata);
57507c478bd9Sstevel@tonic-gate 		pdata = NULL;
57517c478bd9Sstevel@tonic-gate 
57527c478bd9Sstevel@tonic-gate 		if (size != USB_DEV_DESCR_SIZE) {
5753d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
57547c478bd9Sstevel@tonic-gate 			    "fail enumeration: descriptor size=%lu "
57557c478bd9Sstevel@tonic-gate 			    "expected size=%u", size, USB_DEV_DESCR_SIZE);
57567c478bd9Sstevel@tonic-gate 
57577c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
57587c478bd9Sstevel@tonic-gate 		}
57597c478bd9Sstevel@tonic-gate 
57607c478bd9Sstevel@tonic-gate 		/*
57617c478bd9Sstevel@tonic-gate 		 * save the device descriptor in usba_device since it is needed
57627c478bd9Sstevel@tonic-gate 		 * later on again
57637c478bd9Sstevel@tonic-gate 		 */
57647c478bd9Sstevel@tonic-gate 		mutex_enter(&child_ud->usb_mutex);
57657c478bd9Sstevel@tonic-gate 		bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
57667c478bd9Sstevel@tonic-gate 			sizeof (usb_dev_descr_t));
57677c478bd9Sstevel@tonic-gate 		child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
57687c478bd9Sstevel@tonic-gate 		mutex_exit(&child_ud->usb_mutex);
57697c478bd9Sstevel@tonic-gate 	}
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate 	if (usb_dev_descr.bNumConfigurations == 0) {
5772d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
57737c478bd9Sstevel@tonic-gate 		    "device descriptor:\n\t"
57747c478bd9Sstevel@tonic-gate 		    "l=0x%x type=0x%x USB=0x%x class=0x%x subclass=0x%x\n\t"
57757c478bd9Sstevel@tonic-gate 		    "protocol=0x%x maxpktsize=0x%x "
57767c478bd9Sstevel@tonic-gate 		    "Vid=0x%x Pid=0x%x rel=0x%x\n\t"
57777c478bd9Sstevel@tonic-gate 		    "Mfg=0x%x P=0x%x sn=0x%x #config=0x%x",
57787c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bLength, usb_dev_descr.bDescriptorType,
57797c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bcdUSB, usb_dev_descr.bDeviceClass,
57807c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bDeviceSubClass,
57817c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bDeviceProtocol,
57827c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bMaxPacketSize0,
57837c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idVendor,
57847c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idProduct, usb_dev_descr.bcdDevice,
57857c478bd9Sstevel@tonic-gate 		    usb_dev_descr.iManufacturer, usb_dev_descr.iProduct,
57867c478bd9Sstevel@tonic-gate 		    usb_dev_descr.iSerialNumber,
57877c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bNumConfigurations);
57887c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
57897c478bd9Sstevel@tonic-gate 	}
57907c478bd9Sstevel@tonic-gate 
57917c478bd9Sstevel@tonic-gate 
57927c478bd9Sstevel@tonic-gate 	/* get the device string descriptor(s) */
57937c478bd9Sstevel@tonic-gate 	usba_get_dev_string_descrs(child_dip, child_ud);
57947c478bd9Sstevel@tonic-gate 
57957c478bd9Sstevel@tonic-gate 	/* retrieve config cloud for all configurations */
57967c478bd9Sstevel@tonic-gate 	rval = hubd_get_all_device_config_cloud(hubd, child_dip, child_ud);
57977c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
5798d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
57997c478bd9Sstevel@tonic-gate 		    "failed to get configuration descriptor(s)");
58007c478bd9Sstevel@tonic-gate 
58017c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
58027c478bd9Sstevel@tonic-gate 	}
58037c478bd9Sstevel@tonic-gate 
58047c478bd9Sstevel@tonic-gate 	/* get the preferred configuration for this device */
58057c478bd9Sstevel@tonic-gate 	user_conf_index = hubd_select_device_configuration(hubd, port,
58067c478bd9Sstevel@tonic-gate 	    child_dip, child_ud);
58077c478bd9Sstevel@tonic-gate 
58087c478bd9Sstevel@tonic-gate 	/* Check if the user selected configuration index is in range */
580935f36846Ssl 	if ((user_conf_index >= usb_dev_descr.bNumConfigurations) ||
581035f36846Ssl 	    (user_conf_index < 0)) {
5811d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
58127c478bd9Sstevel@tonic-gate 		    "Configuration index for device idVendor=%d "
58137c478bd9Sstevel@tonic-gate 		    "idProduct=%d is=%d, and is out of range[0..%d]",
58147c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idVendor, usb_dev_descr.idProduct,
58157c478bd9Sstevel@tonic-gate 		    user_conf_index, usb_dev_descr.bNumConfigurations - 1);
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate 		/* treat this as user didn't specify configuration */
58187c478bd9Sstevel@tonic-gate 		user_conf_index = USBA_DEV_CONFIG_INDEX_UNDEFINED;
58197c478bd9Sstevel@tonic-gate 	}
58207c478bd9Sstevel@tonic-gate 
58217c478bd9Sstevel@tonic-gate 
58227c478bd9Sstevel@tonic-gate 	/*
58237c478bd9Sstevel@tonic-gate 	 * Warn users of a performance hit if connecting a
58247c478bd9Sstevel@tonic-gate 	 * High Speed behind a 1.1 hub, which is behind a
58257c478bd9Sstevel@tonic-gate 	 * 2.0 port.
58267c478bd9Sstevel@tonic-gate 	 */
58277c478bd9Sstevel@tonic-gate 	if ((parent_port_status != USBA_HIGH_SPEED_DEV) &&
58287c478bd9Sstevel@tonic-gate 	    !(usba_is_root_hub(parent_ud->usb_dip)) &&
58297c478bd9Sstevel@tonic-gate 	    (parent_usb_addr)) {
58307c478bd9Sstevel@tonic-gate 
58317c478bd9Sstevel@tonic-gate 		/*
58327c478bd9Sstevel@tonic-gate 		 * Now that we know the root port is a high speed port
58337c478bd9Sstevel@tonic-gate 		 * and that the parent port is not a high speed port,
58347c478bd9Sstevel@tonic-gate 		 * let's find out if the device itself is a high speed
58357c478bd9Sstevel@tonic-gate 		 * device.  If it is a high speed device,
58367c478bd9Sstevel@tonic-gate 		 * USB_DESCR_TYPE_SETUP_DEV_QLF should return a value,
58377c478bd9Sstevel@tonic-gate 		 * otherwise the command will fail.
58387c478bd9Sstevel@tonic-gate 		 */
58397c478bd9Sstevel@tonic-gate 		rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
58407c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
58417c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
58427c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV_QLF,	/* wValue */
58437c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
58447c478bd9Sstevel@tonic-gate 		    10,					/* wLength */
58457c478bd9Sstevel@tonic-gate 		    &pdata, USB_ATTRS_SHORT_XFER_OK,
58467c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0);
58477c478bd9Sstevel@tonic-gate 
58487c478bd9Sstevel@tonic-gate 		if (pdata) {
58497c478bd9Sstevel@tonic-gate 			freemsg(pdata);
58507c478bd9Sstevel@tonic-gate 			pdata = NULL;
58517c478bd9Sstevel@tonic-gate 		}
58527c478bd9Sstevel@tonic-gate 
58537c478bd9Sstevel@tonic-gate 		/*
58547c478bd9Sstevel@tonic-gate 		 * USB_DESCR_TYPE_SETUP_DEV_QLF query was successful
58557c478bd9Sstevel@tonic-gate 		 * that means this is a high speed device behind a
58567c478bd9Sstevel@tonic-gate 		 * high speed root hub, but running at full speed
58577c478bd9Sstevel@tonic-gate 		 * because there is a full speed hub in the middle.
58587c478bd9Sstevel@tonic-gate 		 */
58597c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
58607c478bd9Sstevel@tonic-gate 		    USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
58617c478bd9Sstevel@tonic-gate 			hubd->h_log_handle,
58627c478bd9Sstevel@tonic-gate 			"Connecting a high speed device to a "
58637c478bd9Sstevel@tonic-gate 			"non high speed hub (port %d) will result "
58647c478bd9Sstevel@tonic-gate 			"in a loss of performance.  Please connect "
58657c478bd9Sstevel@tonic-gate 			"the device to a high speed hub to get "
58667c478bd9Sstevel@tonic-gate 			"the maximum performance.",
58677c478bd9Sstevel@tonic-gate 			port);
58687c478bd9Sstevel@tonic-gate 		}
58697c478bd9Sstevel@tonic-gate 	}
58707c478bd9Sstevel@tonic-gate 
58717c478bd9Sstevel@tonic-gate 	/*
58727c478bd9Sstevel@tonic-gate 	 * Now we try to online the device by attaching a driver
58737c478bd9Sstevel@tonic-gate 	 * The following truth table illustrates the logic:-
58747c478bd9Sstevel@tonic-gate 	 * Cfgndx	Driver	Action
58757c478bd9Sstevel@tonic-gate 	 * 0		0	loop all configs for driver with full
58767c478bd9Sstevel@tonic-gate 	 *			compatible properties.
58777c478bd9Sstevel@tonic-gate 	 * 0		1	set first configuration,
58787c478bd9Sstevel@tonic-gate 	 *			compatible prop = drivername.
58797c478bd9Sstevel@tonic-gate 	 * 1		0	Set config, full compatible prop
58807c478bd9Sstevel@tonic-gate 	 * 1		1	Set config, compatible prop = drivername.
58817c478bd9Sstevel@tonic-gate 	 *
58827c478bd9Sstevel@tonic-gate 	 * Note:
58837c478bd9Sstevel@tonic-gate 	 *	cfgndx = user_conf_index
58847c478bd9Sstevel@tonic-gate 	 *	Driver = usb_preferred_driver
58857c478bd9Sstevel@tonic-gate 	 */
58867c478bd9Sstevel@tonic-gate 	if (user_conf_index == USBA_DEV_CONFIG_INDEX_UNDEFINED) {
58877c478bd9Sstevel@tonic-gate 		if (child_ud->usb_preferred_driver) {
58887c478bd9Sstevel@tonic-gate 			/*
58897c478bd9Sstevel@tonic-gate 			 * It is the job of the "preferred driver" to put the
58907c478bd9Sstevel@tonic-gate 			 * device in the desired configuration. Till then
58917c478bd9Sstevel@tonic-gate 			 * put the device in config index 0.
58927c478bd9Sstevel@tonic-gate 			 */
589335f36846Ssl 			if ((rval = usba_hubdi_check_power_budget(dip, child_ud,
589435f36846Ssl 			    USB_DEV_DEFAULT_CONFIG_INDEX)) != USB_SUCCESS) {
589535f36846Ssl 
589635f36846Ssl 				goto fail_cleanup;
589735f36846Ssl 			}
589835f36846Ssl 
58997c478bd9Sstevel@tonic-gate 			child_dip = hubd_ready_device(hubd, child_dip,
59007c478bd9Sstevel@tonic-gate 			    child_ud, USB_DEV_DEFAULT_CONFIG_INDEX);
59017c478bd9Sstevel@tonic-gate 
59027c478bd9Sstevel@tonic-gate 			/*
59037c478bd9Sstevel@tonic-gate 			 * Assign the dip before onlining to avoid race
59047c478bd9Sstevel@tonic-gate 			 * with busctl
59057c478bd9Sstevel@tonic-gate 			 */
59067c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
59077c478bd9Sstevel@tonic-gate 			hubd->h_children_dips[port] = child_dip;
59087c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
59097c478bd9Sstevel@tonic-gate 
59107c478bd9Sstevel@tonic-gate 			(void) usba_bind_driver(child_dip);
59117c478bd9Sstevel@tonic-gate 		} else {
59127c478bd9Sstevel@tonic-gate 			/*
59137c478bd9Sstevel@tonic-gate 			 * loop through all the configurations to see if we
59147c478bd9Sstevel@tonic-gate 			 * can find a driver for any one config. If not, set
59157c478bd9Sstevel@tonic-gate 			 * the device in config_index 0
59167c478bd9Sstevel@tonic-gate 			 */
59177c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
59187c478bd9Sstevel@tonic-gate 			for (config_index = 0;
59197c478bd9Sstevel@tonic-gate 			    (config_index < usb_dev_descr.bNumConfigurations) &&
59207c478bd9Sstevel@tonic-gate 			    (rval != USB_SUCCESS); config_index++) {
59217c478bd9Sstevel@tonic-gate 
59227c478bd9Sstevel@tonic-gate 				child_dip = hubd_ready_device(hubd, child_dip,
59237c478bd9Sstevel@tonic-gate 				    child_ud, config_index);
59247c478bd9Sstevel@tonic-gate 
59257c478bd9Sstevel@tonic-gate 				/*
59267c478bd9Sstevel@tonic-gate 				 * Assign the dip before onlining to avoid race
59277c478bd9Sstevel@tonic-gate 				 * with busctl
59287c478bd9Sstevel@tonic-gate 				 */
59297c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
59307c478bd9Sstevel@tonic-gate 				hubd->h_children_dips[port] = child_dip;
59317c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
59327c478bd9Sstevel@tonic-gate 
59337c478bd9Sstevel@tonic-gate 				rval = usba_bind_driver(child_dip);
593435f36846Ssl 
593535f36846Ssl 				/*
593635f36846Ssl 				 * Normally power budget should be checked
593735f36846Ssl 				 * before device is configured. A failure in
593835f36846Ssl 				 * power budget checking will stop the device
593935f36846Ssl 				 * from being configured with current
594035f36846Ssl 				 * config_index and may enable the device to
594135f36846Ssl 				 * be configured in another configuration.
594235f36846Ssl 				 * This may break the user experience that a
594335f36846Ssl 				 * device which previously worked in config
594435f36846Ssl 				 * A now works in config B after power budget
594535f36846Ssl 				 * control is enabled. To avoid such situation,
594635f36846Ssl 				 * power budget checking is moved here and will
594735f36846Ssl 				 * fail the child creation directly if config
594835f36846Ssl 				 * A exceeds the power available.
594935f36846Ssl 				 */
595035f36846Ssl 				if (rval == USB_SUCCESS) {
595135f36846Ssl 					if ((usba_hubdi_check_power_budget(dip,
595235f36846Ssl 					    child_ud, config_index)) !=
595335f36846Ssl 					    USB_SUCCESS) {
595435f36846Ssl 
595535f36846Ssl 						goto fail_cleanup;
595635f36846Ssl 					}
595735f36846Ssl 				}
59587c478bd9Sstevel@tonic-gate 			}
59597c478bd9Sstevel@tonic-gate 			if (rval != USB_SUCCESS) {
596035f36846Ssl 
596135f36846Ssl 				if ((usba_hubdi_check_power_budget(dip,
596235f36846Ssl 				    child_ud, 0)) != USB_SUCCESS) {
596335f36846Ssl 
596435f36846Ssl 					goto fail_cleanup;
596535f36846Ssl 				}
596635f36846Ssl 
59677c478bd9Sstevel@tonic-gate 				child_dip = hubd_ready_device(hubd, child_dip,
59687c478bd9Sstevel@tonic-gate 				    child_ud, 0);
59697c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
59707c478bd9Sstevel@tonic-gate 				hubd->h_children_dips[port] = child_dip;
59717c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
59727c478bd9Sstevel@tonic-gate 			}
59737c478bd9Sstevel@tonic-gate 		} /* end else loop all configs */
59747c478bd9Sstevel@tonic-gate 	} else {
597535f36846Ssl 
597635f36846Ssl 		if ((usba_hubdi_check_power_budget(dip, child_ud,
597735f36846Ssl 		    (uint_t)user_conf_index)) != USB_SUCCESS) {
597835f36846Ssl 
597935f36846Ssl 			goto fail_cleanup;
598035f36846Ssl 		}
598135f36846Ssl 
59827c478bd9Sstevel@tonic-gate 		child_dip = hubd_ready_device(hubd, child_dip,
598335f36846Ssl 		    child_ud, (uint_t)user_conf_index);
59847c478bd9Sstevel@tonic-gate 
59857c478bd9Sstevel@tonic-gate 		/*
59867c478bd9Sstevel@tonic-gate 		 * Assign the dip before onlining to avoid race
59877c478bd9Sstevel@tonic-gate 		 * with busctl
59887c478bd9Sstevel@tonic-gate 		 */
59897c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
59907c478bd9Sstevel@tonic-gate 		hubd->h_children_dips[port] = child_dip;
59917c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
59927c478bd9Sstevel@tonic-gate 
59937c478bd9Sstevel@tonic-gate 		(void) usba_bind_driver(child_dip);
59947c478bd9Sstevel@tonic-gate 	}
59957c478bd9Sstevel@tonic-gate 
599635f36846Ssl 	usba_hubdi_decr_power_budget(dip, child_ud);
599735f36846Ssl 
59987c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
59997c478bd9Sstevel@tonic-gate 	if (hubd->h_usba_devices[port] == NULL) {
60007c478bd9Sstevel@tonic-gate 		hubd->h_usba_devices[port] = usba_get_usba_device(child_dip);
60017c478bd9Sstevel@tonic-gate 	} else {
60027c478bd9Sstevel@tonic-gate 		ASSERT(hubd->h_usba_devices[port] ==
60037c478bd9Sstevel@tonic-gate 				usba_get_usba_device(child_dip));
60047c478bd9Sstevel@tonic-gate 	}
60057c478bd9Sstevel@tonic-gate 
60067c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
60077c478bd9Sstevel@tonic-gate 
60087c478bd9Sstevel@tonic-gate 
60097c478bd9Sstevel@tonic-gate fail_cleanup:
60107c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
60117c478bd9Sstevel@tonic-gate 	    "hubd_create_child: fail_cleanup");
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
60147c478bd9Sstevel@tonic-gate 	hubd->h_children_dips[port] = NULL;
60157c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
60167c478bd9Sstevel@tonic-gate 
60177c478bd9Sstevel@tonic-gate 	if (pdata) {
60187c478bd9Sstevel@tonic-gate 		freemsg(pdata);
60197c478bd9Sstevel@tonic-gate 	}
60207c478bd9Sstevel@tonic-gate 
60217c478bd9Sstevel@tonic-gate 	if (ph) {
60227c478bd9Sstevel@tonic-gate 		usb_pipe_close(child_dip, ph,
60237c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
60247c478bd9Sstevel@tonic-gate 	}
60257c478bd9Sstevel@tonic-gate 
60267c478bd9Sstevel@tonic-gate 	if (child_dip) {
60277c478bd9Sstevel@tonic-gate 		int rval = usba_destroy_child_devi(child_dip,
60287c478bd9Sstevel@tonic-gate 		    NDI_DEVI_REMOVE);
60297c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
6030d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
60317c478bd9Sstevel@tonic-gate 			    "failure to remove child node");
60327c478bd9Sstevel@tonic-gate 		}
60337c478bd9Sstevel@tonic-gate 	}
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate 	if (child_ud) {
60367c478bd9Sstevel@tonic-gate 		/* to make sure we free the address */
60377c478bd9Sstevel@tonic-gate 		mutex_enter(&child_ud->usb_mutex);
60387c478bd9Sstevel@tonic-gate 		child_ud->usb_addr = address;
60397c478bd9Sstevel@tonic-gate 		ASSERT(child_ud->usb_ref_count == 0);
60407c478bd9Sstevel@tonic-gate 		mutex_exit(&child_ud->usb_mutex);
60417c478bd9Sstevel@tonic-gate 
60427c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
60437c478bd9Sstevel@tonic-gate 		if (hubd->h_usba_devices[port] == NULL) {
60447c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
60457c478bd9Sstevel@tonic-gate 			usba_free_usba_device(child_ud);
60467c478bd9Sstevel@tonic-gate 		} else {
60477c478bd9Sstevel@tonic-gate 			hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
60487c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
60497c478bd9Sstevel@tonic-gate 		}
60507c478bd9Sstevel@tonic-gate 	}
60517c478bd9Sstevel@tonic-gate 
60527c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
60537c478bd9Sstevel@tonic-gate 
60547c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
60557c478bd9Sstevel@tonic-gate }
60567c478bd9Sstevel@tonic-gate 
60577c478bd9Sstevel@tonic-gate 
60587c478bd9Sstevel@tonic-gate /*
60597c478bd9Sstevel@tonic-gate  * hubd_delete_child:
60607c478bd9Sstevel@tonic-gate  *	- free usb address
60617c478bd9Sstevel@tonic-gate  *	- lookup child dips, there may be multiple on this port
60627c478bd9Sstevel@tonic-gate  *	- offline each child devi
60637c478bd9Sstevel@tonic-gate  */
60647c478bd9Sstevel@tonic-gate static int
60657c478bd9Sstevel@tonic-gate hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, boolean_t retry)
60667c478bd9Sstevel@tonic-gate {
60677c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip;
60687c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
60697c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
60707c478bd9Sstevel@tonic-gate 
60717c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
607235f36846Ssl 	usba_device = hubd->h_usba_devices[port];
60737c478bd9Sstevel@tonic-gate 
60747c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
60757c478bd9Sstevel@tonic-gate 	    "hubd_delete_child: port=%d, dip=0x%p usba_device=0x%p",
607635f36846Ssl 	    port, child_dip, usba_device);
60777c478bd9Sstevel@tonic-gate 
60787c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
60797c478bd9Sstevel@tonic-gate 	if (child_dip) {
60807c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
60817c478bd9Sstevel@tonic-gate 		    "hubd_delete_child:\n\t"
60827c478bd9Sstevel@tonic-gate 		    "dip = 0x%p (%s) at port %d",
60837c478bd9Sstevel@tonic-gate 		    child_dip, ddi_node_name(child_dip), port);
60847c478bd9Sstevel@tonic-gate 
608535f36846Ssl 		if (usba_device) {
608635f36846Ssl 			usba_hubdi_incr_power_budget(hubd->h_dip, usba_device);
608735f36846Ssl 		}
608835f36846Ssl 
60897c478bd9Sstevel@tonic-gate 		rval = usba_destroy_child_devi(child_dip, flag);
60907c478bd9Sstevel@tonic-gate 
60917c478bd9Sstevel@tonic-gate 		if ((rval == USB_SUCCESS) && (flag & NDI_DEVI_REMOVE)) {
60927c478bd9Sstevel@tonic-gate 			/*
60937c478bd9Sstevel@tonic-gate 			 * if the child was still < DS_INITIALIZED
60947c478bd9Sstevel@tonic-gate 			 * then our bus_unconfig was not called and
60957c478bd9Sstevel@tonic-gate 			 * we have to zap the child here
60967c478bd9Sstevel@tonic-gate 			 */
60977c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
60987c478bd9Sstevel@tonic-gate 			if (hubd->h_children_dips[port] == child_dip) {
60997c478bd9Sstevel@tonic-gate 				usba_device_t *ud =
61007c478bd9Sstevel@tonic-gate 						hubd->h_usba_devices[port];
61017c478bd9Sstevel@tonic-gate 				hubd->h_children_dips[port] = NULL;
61027c478bd9Sstevel@tonic-gate 				if (ud) {
61037c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
61047c478bd9Sstevel@tonic-gate 
61057c478bd9Sstevel@tonic-gate 					mutex_enter(&ud->usb_mutex);
61067c478bd9Sstevel@tonic-gate 					ud->usb_ref_count = 0;
61077c478bd9Sstevel@tonic-gate 					mutex_exit(&ud->usb_mutex);
61087c478bd9Sstevel@tonic-gate 
61097c478bd9Sstevel@tonic-gate 					usba_free_usba_device(ud);
61107c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
61117c478bd9Sstevel@tonic-gate 					hubd->h_usba_devices[port] = NULL;
61127c478bd9Sstevel@tonic-gate 				}
61137c478bd9Sstevel@tonic-gate 			}
61147c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
61157c478bd9Sstevel@tonic-gate 		}
61167c478bd9Sstevel@tonic-gate 	}
61177c478bd9Sstevel@tonic-gate 
61187c478bd9Sstevel@tonic-gate 	if ((rval != USB_SUCCESS) && retry) {
61197c478bd9Sstevel@tonic-gate 
61207c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(usba_device->usb_root_hub_dip);
61217c478bd9Sstevel@tonic-gate 	}
61227c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
61237c478bd9Sstevel@tonic-gate 
61247c478bd9Sstevel@tonic-gate 	return (rval);
61257c478bd9Sstevel@tonic-gate }
61267c478bd9Sstevel@tonic-gate 
61277c478bd9Sstevel@tonic-gate 
61287c478bd9Sstevel@tonic-gate /*
61297c478bd9Sstevel@tonic-gate  * hubd_free_usba_device:
61307c478bd9Sstevel@tonic-gate  *	free usb device structure unless it is associated with
61317c478bd9Sstevel@tonic-gate  *	the root hub which is handled differently
61327c478bd9Sstevel@tonic-gate  */
61337c478bd9Sstevel@tonic-gate static void
61347c478bd9Sstevel@tonic-gate hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device)
61357c478bd9Sstevel@tonic-gate {
61367c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61377c478bd9Sstevel@tonic-gate 	    "hubd_free_usba_device: hubd=0x%p, usba_device=0x%p",
61387c478bd9Sstevel@tonic-gate 	    hubd, usba_device);
61397c478bd9Sstevel@tonic-gate 
61407c478bd9Sstevel@tonic-gate 	if (usba_device && (usba_device->usb_addr != ROOT_HUB_ADDR)) {
61417c478bd9Sstevel@tonic-gate 		usb_port_t port = usba_device->usb_port;
61427c478bd9Sstevel@tonic-gate 		dev_info_t *dip = hubd->h_children_dips[port];
61437c478bd9Sstevel@tonic-gate 
61447c478bd9Sstevel@tonic-gate #ifdef DEBUG
61457c478bd9Sstevel@tonic-gate 		if (dip) {
61467c478bd9Sstevel@tonic-gate 			ASSERT(i_ddi_node_state(dip) < DS_INITIALIZED);
61477c478bd9Sstevel@tonic-gate 		}
61487c478bd9Sstevel@tonic-gate #endif
61497c478bd9Sstevel@tonic-gate 
61507c478bd9Sstevel@tonic-gate 		port = usba_device->usb_port;
61517c478bd9Sstevel@tonic-gate 		hubd->h_usba_devices[port] = NULL;
61527c478bd9Sstevel@tonic-gate 
61537c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
61547c478bd9Sstevel@tonic-gate 		usba_free_usba_device(usba_device);
61557c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
61567c478bd9Sstevel@tonic-gate 	}
61577c478bd9Sstevel@tonic-gate }
61587c478bd9Sstevel@tonic-gate 
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate /*
61617c478bd9Sstevel@tonic-gate  * event support
61627c478bd9Sstevel@tonic-gate  *
61637c478bd9Sstevel@tonic-gate  * busctl event support
61647c478bd9Sstevel@tonic-gate  */
61657c478bd9Sstevel@tonic-gate static int
61667c478bd9Sstevel@tonic-gate hubd_busop_get_eventcookie(dev_info_t *dip,
61677c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
61687c478bd9Sstevel@tonic-gate 	char		*eventname,
61697c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t *cookie)
61707c478bd9Sstevel@tonic-gate {
61717c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
61727c478bd9Sstevel@tonic-gate 
61737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61747c478bd9Sstevel@tonic-gate 	    "hubd_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
61757c478bd9Sstevel@tonic-gate 	    "event=%s", (void *)dip, (void *)rdip, eventname);
61767c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61777c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d)",
61787c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
61797c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
61807c478bd9Sstevel@tonic-gate 
61817c478bd9Sstevel@tonic-gate 	/* return event cookie, iblock cookie, and level */
61827c478bd9Sstevel@tonic-gate 	return (ndi_event_retrieve_cookie(hubd->h_ndi_event_hdl,
61837c478bd9Sstevel@tonic-gate 		rdip, eventname, cookie, NDI_EVENT_NOPASS));
61847c478bd9Sstevel@tonic-gate }
61857c478bd9Sstevel@tonic-gate 
61867c478bd9Sstevel@tonic-gate 
61877c478bd9Sstevel@tonic-gate static int
61887c478bd9Sstevel@tonic-gate hubd_busop_add_eventcall(dev_info_t *dip,
61897c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
61907c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
61917c478bd9Sstevel@tonic-gate 	void		(*callback)(dev_info_t *dip,
61927c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t cookie, void *arg,
61937c478bd9Sstevel@tonic-gate 			void *bus_impldata),
61947c478bd9Sstevel@tonic-gate 	void *arg, ddi_callback_id_t *cb_id)
61957c478bd9Sstevel@tonic-gate {
61967c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
61977c478bd9Sstevel@tonic-gate 	usb_port_t port = hubd_child_dip2port(hubd, rdip);
61987c478bd9Sstevel@tonic-gate 
61997c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62007c478bd9Sstevel@tonic-gate 	    "hubd_busop_add_eventcall: dip=0x%p, rdip=0x%p "
62017c478bd9Sstevel@tonic-gate 	    "cookie=0x%p, cb=0x%p, arg=0x%p",
62027c478bd9Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
62037c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62047c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d, event=%s)",
62057c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
62067c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
62077c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(hubd->h_ndi_event_hdl, cookie));
62087c478bd9Sstevel@tonic-gate 
62097c478bd9Sstevel@tonic-gate 	/* Set flag on children registering events */
62107c478bd9Sstevel@tonic-gate 	switch (ndi_event_cookie_to_tag(hubd->h_ndi_event_hdl, cookie)) {
62117c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
62127c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
62137c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
62147c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
62157c478bd9Sstevel@tonic-gate 
62167c478bd9Sstevel@tonic-gate 		break;
62177c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
62187c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
62197c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
62207c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
62217c478bd9Sstevel@tonic-gate 
62227c478bd9Sstevel@tonic-gate 		break;
62237c478bd9Sstevel@tonic-gate 	default:
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate 		break;
62267c478bd9Sstevel@tonic-gate 	}
62277c478bd9Sstevel@tonic-gate 
62287c478bd9Sstevel@tonic-gate 	/* add callback to our event set */
62297c478bd9Sstevel@tonic-gate 	return (ndi_event_add_callback(hubd->h_ndi_event_hdl,
62307c478bd9Sstevel@tonic-gate 		rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
62317c478bd9Sstevel@tonic-gate }
62327c478bd9Sstevel@tonic-gate 
62337c478bd9Sstevel@tonic-gate 
62347c478bd9Sstevel@tonic-gate static int
62357c478bd9Sstevel@tonic-gate hubd_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
62367c478bd9Sstevel@tonic-gate {
62377c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
62387c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *id = (ndi_event_callbacks_t *)cb_id;
62397c478bd9Sstevel@tonic-gate 
62407c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62417c478bd9Sstevel@tonic-gate 	    "hubd_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
62427c478bd9Sstevel@tonic-gate 	    "cookie=0x%p", (void *)dip, id->ndi_evtcb_dip,
62437c478bd9Sstevel@tonic-gate 	    id->ndi_evtcb_cookie);
62447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62457c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d, event=%s)",
62467c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
62477c478bd9Sstevel@tonic-gate 	    ddi_driver_name(id->ndi_evtcb_dip),
62487c478bd9Sstevel@tonic-gate 	    ddi_get_instance(id->ndi_evtcb_dip),
62497c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(hubd->h_ndi_event_hdl,
62507c478bd9Sstevel@tonic-gate 	    id->ndi_evtcb_cookie));
62517c478bd9Sstevel@tonic-gate 
62527c478bd9Sstevel@tonic-gate 	/* remove event registration from our event set */
62537c478bd9Sstevel@tonic-gate 	return (ndi_event_remove_callback(hubd->h_ndi_event_hdl, cb_id));
62547c478bd9Sstevel@tonic-gate }
62557c478bd9Sstevel@tonic-gate 
62567c478bd9Sstevel@tonic-gate 
62577c478bd9Sstevel@tonic-gate /*
62587c478bd9Sstevel@tonic-gate  * event distribution
62597c478bd9Sstevel@tonic-gate  *
62607c478bd9Sstevel@tonic-gate  * hubd_do_callback:
62617c478bd9Sstevel@tonic-gate  *	Post this event to the specified child
62627c478bd9Sstevel@tonic-gate  */
62637c478bd9Sstevel@tonic-gate static void
62647c478bd9Sstevel@tonic-gate hubd_do_callback(hubd_t *hubd, dev_info_t *cdip, ddi_eventcookie_t cookie)
62657c478bd9Sstevel@tonic-gate {
62667c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62677c478bd9Sstevel@tonic-gate 	    "hubd_do_callback");
62687c478bd9Sstevel@tonic-gate 
62697c478bd9Sstevel@tonic-gate 	(void) ndi_event_do_callback(hubd->h_ndi_event_hdl, cdip, cookie, NULL);
62707c478bd9Sstevel@tonic-gate }
62717c478bd9Sstevel@tonic-gate 
62727c478bd9Sstevel@tonic-gate 
62737c478bd9Sstevel@tonic-gate /*
62747c478bd9Sstevel@tonic-gate  * hubd_run_callbacks:
62757c478bd9Sstevel@tonic-gate  *	Send this event to all children
62767c478bd9Sstevel@tonic-gate  */
62777c478bd9Sstevel@tonic-gate static void
62787c478bd9Sstevel@tonic-gate hubd_run_callbacks(hubd_t *hubd, usba_event_t type)
62797c478bd9Sstevel@tonic-gate {
62807c478bd9Sstevel@tonic-gate 	usb_port_t	port;
62817c478bd9Sstevel@tonic-gate 
62827c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62837c478bd9Sstevel@tonic-gate 	    "hubd_run_callbacks");
62847c478bd9Sstevel@tonic-gate 
62857c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
62867c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
62877c478bd9Sstevel@tonic-gate 		/*
62887c478bd9Sstevel@tonic-gate 		 * the childen_dips list may have dips that have been
62897c478bd9Sstevel@tonic-gate 		 * already deallocated. we only get a post_detach notification
62907c478bd9Sstevel@tonic-gate 		 * but not a destroy notification
62917c478bd9Sstevel@tonic-gate 		 */
62927c478bd9Sstevel@tonic-gate 		if (hubd->h_children_dips[port]) {
62937c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
62947c478bd9Sstevel@tonic-gate 			hubd_post_event(hubd, port, type);
62957c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
62967c478bd9Sstevel@tonic-gate 		}
62977c478bd9Sstevel@tonic-gate 	}
62987c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
62997c478bd9Sstevel@tonic-gate }
63007c478bd9Sstevel@tonic-gate 
63017c478bd9Sstevel@tonic-gate 
63027c478bd9Sstevel@tonic-gate /*
63037c478bd9Sstevel@tonic-gate  * hubd_post_event
63047c478bd9Sstevel@tonic-gate  *	post event to a child on the port depending on the type
63057c478bd9Sstevel@tonic-gate  */
63067c478bd9Sstevel@tonic-gate static void
63077c478bd9Sstevel@tonic-gate hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type)
63087c478bd9Sstevel@tonic-gate {
63097c478bd9Sstevel@tonic-gate 	int	rval;
63107c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
63117c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
63127c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie, rm_cookie, suspend_cookie;
63137c478bd9Sstevel@tonic-gate 
63147c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
63157c478bd9Sstevel@tonic-gate 	    "hubd_post_event: port=%d event=%s", port,
63167c478bd9Sstevel@tonic-gate 	    ndi_event_tag_to_name(hubd->h_ndi_event_hdl, type));
63177c478bd9Sstevel@tonic-gate 
63187c478bd9Sstevel@tonic-gate 	cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, type);
63197c478bd9Sstevel@tonic-gate 	rm_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
63207c478bd9Sstevel@tonic-gate 	    USBA_EVENT_TAG_HOT_REMOVAL);
63217c478bd9Sstevel@tonic-gate 	suspend_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
63227c478bd9Sstevel@tonic-gate 	    USBA_EVENT_TAG_PRE_SUSPEND);
63237c478bd9Sstevel@tonic-gate 
63247c478bd9Sstevel@tonic-gate 	/*
63257c478bd9Sstevel@tonic-gate 	 * Hotplug daemon may be attaching a driver that may be registering
63267c478bd9Sstevel@tonic-gate 	 * event callbacks. So it already has got the device tree lock and
63277c478bd9Sstevel@tonic-gate 	 * event handle mutex. So to prevent a deadlock while posting events,
63287c478bd9Sstevel@tonic-gate 	 * we grab and release the locks in the same order.
63297c478bd9Sstevel@tonic-gate 	 */
63307c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
63317c478bd9Sstevel@tonic-gate 	dip = hubd->h_children_dips[port];
63327c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
63337c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
63347c478bd9Sstevel@tonic-gate 
63357c478bd9Sstevel@tonic-gate 	switch (type) {
63367c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
63377c478bd9Sstevel@tonic-gate 		/* Clear the registered event flag */
63387c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
63397c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_DISCONNECT;
63407c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
63417c478bd9Sstevel@tonic-gate 
63427c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
63437c478bd9Sstevel@tonic-gate 		usba_persistent_pipe_close(usba_device);
63447c478bd9Sstevel@tonic-gate 
63457c478bd9Sstevel@tonic-gate 		/*
63467c478bd9Sstevel@tonic-gate 		 * Mark the dip for deletion only after the driver has
63477c478bd9Sstevel@tonic-gate 		 * seen the disconnect event to prevent cleanup thread
63487c478bd9Sstevel@tonic-gate 		 * from stepping in between.
63497c478bd9Sstevel@tonic-gate 		 */
635016747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
63517c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_REMOVED(dip);
635216747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
63537c478bd9Sstevel@tonic-gate 
63547c478bd9Sstevel@tonic-gate 		break;
63557c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
63567c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
63577c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_PRESUSPEND;
63587c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
63597c478bd9Sstevel@tonic-gate 
63607c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
63617c478bd9Sstevel@tonic-gate 		/*
63627c478bd9Sstevel@tonic-gate 		 * persistent pipe close for this event is taken care by the
63637c478bd9Sstevel@tonic-gate 		 * caller after verfying that all children can suspend
63647c478bd9Sstevel@tonic-gate 		 */
63657c478bd9Sstevel@tonic-gate 
63667c478bd9Sstevel@tonic-gate 		break;
63677c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_INSERTION:
63687c478bd9Sstevel@tonic-gate 		/*
63697c478bd9Sstevel@tonic-gate 		 * Check if this child has missed the disconnect event before
63707c478bd9Sstevel@tonic-gate 		 * it registered for event callbacks
63717c478bd9Sstevel@tonic-gate 		 */
63727c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
63737c478bd9Sstevel@tonic-gate 		if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_DISCONNECT) {
63747c478bd9Sstevel@tonic-gate 			/* clear the flag and post disconnect event */
63757c478bd9Sstevel@tonic-gate 			hubd->h_child_events[port] &=
63767c478bd9Sstevel@tonic-gate 			    ~HUBD_CHILD_EVENT_DISCONNECT;
63777c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
63787c478bd9Sstevel@tonic-gate 			hubd_do_callback(hubd, dip, rm_cookie);
63797c478bd9Sstevel@tonic-gate 			usba_persistent_pipe_close(usba_device);
63807c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
63817c478bd9Sstevel@tonic-gate 		}
63827c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
63837c478bd9Sstevel@tonic-gate 
63847c478bd9Sstevel@tonic-gate 		/*
63857c478bd9Sstevel@tonic-gate 		 * Mark the dip as reinserted to prevent cleanup thread
63867c478bd9Sstevel@tonic-gate 		 * from stepping in.
63877c478bd9Sstevel@tonic-gate 		 */
638816747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
63897c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_REINSERTED(dip);
639016747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
63917c478bd9Sstevel@tonic-gate 
63927c478bd9Sstevel@tonic-gate 		rval = usba_persistent_pipe_open(usba_device);
63937c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
6394d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
63957c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
63967c478bd9Sstevel@tonic-gate 			    "failed to reopen all pipes on reconnect");
63977c478bd9Sstevel@tonic-gate 		}
63987c478bd9Sstevel@tonic-gate 
63997c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
64007c478bd9Sstevel@tonic-gate 
64017c478bd9Sstevel@tonic-gate 		/*
64027c478bd9Sstevel@tonic-gate 		 * We might see a connect event only if hotplug thread for
64037c478bd9Sstevel@tonic-gate 		 * disconnect event don't run in time.
64047c478bd9Sstevel@tonic-gate 		 * Set the flag again, so we don't miss posting a
64057c478bd9Sstevel@tonic-gate 		 * disconnect event.
64067c478bd9Sstevel@tonic-gate 		 */
64077c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
64087c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
64097c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
64107c478bd9Sstevel@tonic-gate 
64117c478bd9Sstevel@tonic-gate 		break;
64127c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_POST_RESUME:
64137c478bd9Sstevel@tonic-gate 		/*
64147c478bd9Sstevel@tonic-gate 		 * Check if this child has missed the pre-suspend event before
64157c478bd9Sstevel@tonic-gate 		 * it registered for event callbacks
64167c478bd9Sstevel@tonic-gate 		 */
64177c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
64187c478bd9Sstevel@tonic-gate 		if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_PRESUSPEND) {
64197c478bd9Sstevel@tonic-gate 			/* clear the flag and post pre_suspend event */
64207c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &=
64217c478bd9Sstevel@tonic-gate 			    ~HUBD_CHILD_EVENT_PRESUSPEND;
64227c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
64237c478bd9Sstevel@tonic-gate 			hubd_do_callback(hubd, dip, suspend_cookie);
64247c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
64257c478bd9Sstevel@tonic-gate 		}
64267c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
64277c478bd9Sstevel@tonic-gate 
64287c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
64297c478bd9Sstevel@tonic-gate 		usba_device->usb_no_cpr = 0;
64307c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
64317c478bd9Sstevel@tonic-gate 
64327c478bd9Sstevel@tonic-gate 		/*
64337c478bd9Sstevel@tonic-gate 		 * Since the pipe has already been opened by hub
64347c478bd9Sstevel@tonic-gate 		 * at DDI_RESUME time, there is no need for a
64357c478bd9Sstevel@tonic-gate 		 * persistent pipe open
64367c478bd9Sstevel@tonic-gate 		 */
64377c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
64387c478bd9Sstevel@tonic-gate 
64397c478bd9Sstevel@tonic-gate 		/*
64407c478bd9Sstevel@tonic-gate 		 * Set the flag again, so we don't miss posting a
64417c478bd9Sstevel@tonic-gate 		 * pre-suspend event. This enforces a tighter
64427c478bd9Sstevel@tonic-gate 		 * dev_state model.
64437c478bd9Sstevel@tonic-gate 		 */
64447c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
64457c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
64467c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
64477c478bd9Sstevel@tonic-gate 		break;
64487c478bd9Sstevel@tonic-gate 	}
64497c478bd9Sstevel@tonic-gate }
64507c478bd9Sstevel@tonic-gate 
64517c478bd9Sstevel@tonic-gate 
64527c478bd9Sstevel@tonic-gate /*
64537c478bd9Sstevel@tonic-gate  * handling of events coming from above
64547c478bd9Sstevel@tonic-gate  */
64557c478bd9Sstevel@tonic-gate static int
64567c478bd9Sstevel@tonic-gate hubd_disconnect_event_cb(dev_info_t *dip)
64577c478bd9Sstevel@tonic-gate {
64587c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)hubd_get_soft_state(dip);
64597c478bd9Sstevel@tonic-gate 	usb_port_t	port, nports;
64607c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_dev;
64617c478bd9Sstevel@tonic-gate 	usba_event_t	tag = USBA_EVENT_TAG_HOT_REMOVAL;
64627c478bd9Sstevel@tonic-gate 	int		circ;
64637c478bd9Sstevel@tonic-gate 
64647c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
64657c478bd9Sstevel@tonic-gate 	    "hubd_disconnect_event_cb: tag=%d", tag);
64667c478bd9Sstevel@tonic-gate 
64677c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
64687c478bd9Sstevel@tonic-gate 
64697c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
64707c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
64717c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
64727c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
64737c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
64747c478bd9Sstevel@tonic-gate 		/* stop polling on the interrupt pipe */
64757c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
64767c478bd9Sstevel@tonic-gate 
64777c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
64787c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
64797c478bd9Sstevel@tonic-gate 		/* we remain in this state */
64807c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
64817c478bd9Sstevel@tonic-gate 		hubd_run_callbacks(hubd, tag);
64827c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
64837c478bd9Sstevel@tonic-gate 
64847c478bd9Sstevel@tonic-gate 		/* close all the open pipes of our children */
64857c478bd9Sstevel@tonic-gate 		nports = hubd->h_hub_descr.bNbrPorts;
64867c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
64877c478bd9Sstevel@tonic-gate 			usba_dev = hubd->h_usba_devices[port];
64887c478bd9Sstevel@tonic-gate 			if (usba_dev != NULL) {
64897c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
64907c478bd9Sstevel@tonic-gate 				usba_persistent_pipe_close(usba_dev);
64917c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
64927c478bd9Sstevel@tonic-gate 			}
64937c478bd9Sstevel@tonic-gate 		}
64947c478bd9Sstevel@tonic-gate 
64957c478bd9Sstevel@tonic-gate 		break;
64967c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
64977c478bd9Sstevel@tonic-gate 		/* avoid passing multiple disconnects to children */
64987c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
64997c478bd9Sstevel@tonic-gate 		    "hubd_disconnect_event_cb: Already disconnected");
65007c478bd9Sstevel@tonic-gate 
65017c478bd9Sstevel@tonic-gate 		break;
65027c478bd9Sstevel@tonic-gate 	default:
65037c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
65047c478bd9Sstevel@tonic-gate 		    "hubd_disconnect_event_cb: Illegal devstate=%d",
65057c478bd9Sstevel@tonic-gate 		    hubd->h_dev_state);
65067c478bd9Sstevel@tonic-gate 
65077c478bd9Sstevel@tonic-gate 		break;
65087c478bd9Sstevel@tonic-gate 	}
65097c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
65107c478bd9Sstevel@tonic-gate 
65117c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
65127c478bd9Sstevel@tonic-gate 
65137c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
65147c478bd9Sstevel@tonic-gate }
65157c478bd9Sstevel@tonic-gate 
65167c478bd9Sstevel@tonic-gate 
65177c478bd9Sstevel@tonic-gate static int
65187c478bd9Sstevel@tonic-gate hubd_reconnect_event_cb(dev_info_t *dip)
65197c478bd9Sstevel@tonic-gate {
65207c478bd9Sstevel@tonic-gate 	int	rval, circ;
65217c478bd9Sstevel@tonic-gate 
65227c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
65237c478bd9Sstevel@tonic-gate 	rval = hubd_restore_state_cb(dip);
65247c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
65257c478bd9Sstevel@tonic-gate 
65267c478bd9Sstevel@tonic-gate 	return (rval);
65277c478bd9Sstevel@tonic-gate }
65287c478bd9Sstevel@tonic-gate 
65297c478bd9Sstevel@tonic-gate 
65307c478bd9Sstevel@tonic-gate /*
65317c478bd9Sstevel@tonic-gate  * hubd_pre_suspend_event_cb
65327c478bd9Sstevel@tonic-gate  *	propogate event for binary compatibility of old drivers
65337c478bd9Sstevel@tonic-gate  */
65347c478bd9Sstevel@tonic-gate static int
65357c478bd9Sstevel@tonic-gate hubd_pre_suspend_event_cb(dev_info_t *dip)
65367c478bd9Sstevel@tonic-gate {
65377c478bd9Sstevel@tonic-gate 	int	circ;
65387c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
65397c478bd9Sstevel@tonic-gate 
65407c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
65417c478bd9Sstevel@tonic-gate 	    "hubd_pre_suspend_event_cb");
65427c478bd9Sstevel@tonic-gate 
65437c478bd9Sstevel@tonic-gate 	/* disable hotplug thread */
65447c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
65457c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread++;
65467c478bd9Sstevel@tonic-gate 	hubd_stop_polling(hubd);
65477c478bd9Sstevel@tonic-gate 
65487c478bd9Sstevel@tonic-gate 	/* keep PM out till we see a cpr resume */
65497c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
65507c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
65517c478bd9Sstevel@tonic-gate 
65527c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
65537c478bd9Sstevel@tonic-gate 	hubd_run_callbacks(hubd, USBA_EVENT_TAG_PRE_SUSPEND);
65547c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
65557c478bd9Sstevel@tonic-gate 
65567c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
65577c478bd9Sstevel@tonic-gate }
65587c478bd9Sstevel@tonic-gate 
65597c478bd9Sstevel@tonic-gate 
65607c478bd9Sstevel@tonic-gate /*
65617c478bd9Sstevel@tonic-gate  * hubd_post_resume_event_cb
65627c478bd9Sstevel@tonic-gate  *	propogate event for binary compatibility of old drivers
65637c478bd9Sstevel@tonic-gate  */
65647c478bd9Sstevel@tonic-gate static int
65657c478bd9Sstevel@tonic-gate hubd_post_resume_event_cb(dev_info_t *dip)
65667c478bd9Sstevel@tonic-gate {
65677c478bd9Sstevel@tonic-gate 	int	circ;
65687c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
65697c478bd9Sstevel@tonic-gate 
65707c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
65717c478bd9Sstevel@tonic-gate 	    "hubd_post_resume_event_cb");
65727c478bd9Sstevel@tonic-gate 
65737c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
65747c478bd9Sstevel@tonic-gate 	hubd_run_callbacks(hubd, USBA_EVENT_TAG_POST_RESUME);
65757c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
65767c478bd9Sstevel@tonic-gate 
65777c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
65787c478bd9Sstevel@tonic-gate 
65797c478bd9Sstevel@tonic-gate 	/* enable PM */
65807c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
65817c478bd9Sstevel@tonic-gate 
65827c478bd9Sstevel@tonic-gate 	/* allow hotplug thread */
65837c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
65847c478bd9Sstevel@tonic-gate 
65857c478bd9Sstevel@tonic-gate 	/* start polling */
65867c478bd9Sstevel@tonic-gate 	hubd_start_polling(hubd, 0);
65877c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
65887c478bd9Sstevel@tonic-gate 
65897c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
65907c478bd9Sstevel@tonic-gate }
65917c478bd9Sstevel@tonic-gate 
65927c478bd9Sstevel@tonic-gate 
65937c478bd9Sstevel@tonic-gate /*
65947c478bd9Sstevel@tonic-gate  * hubd_cpr_suspend
65957c478bd9Sstevel@tonic-gate  *	save the current state of the driver/device
65967c478bd9Sstevel@tonic-gate  */
65977c478bd9Sstevel@tonic-gate static int
65987c478bd9Sstevel@tonic-gate hubd_cpr_suspend(hubd_t *hubd)
65997c478bd9Sstevel@tonic-gate {
66007c478bd9Sstevel@tonic-gate 	usb_port_t	port, nports;
66017c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_dev;
66027c478bd9Sstevel@tonic-gate 	uchar_t		no_cpr = 0;
66037c478bd9Sstevel@tonic-gate 	int		rval = USB_FAILURE;
66047c478bd9Sstevel@tonic-gate 
66057c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
66067c478bd9Sstevel@tonic-gate 	    "hubd_cpr_suspend: Begin");
66077c478bd9Sstevel@tonic-gate 
66087c478bd9Sstevel@tonic-gate 	/* Make sure device is powered up to save state. */
66097c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
66107c478bd9Sstevel@tonic-gate 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
66117c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
66127c478bd9Sstevel@tonic-gate 
66137c478bd9Sstevel@tonic-gate 	/* bring the device to full power */
66147c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
66157c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
66167c478bd9Sstevel@tonic-gate 
66177c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
66187c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
66197c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
66207c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
66217c478bd9Sstevel@tonic-gate 		/* find out if all our children have been quiesced */
66227c478bd9Sstevel@tonic-gate 		nports = hubd->h_hub_descr.bNbrPorts;
66237c478bd9Sstevel@tonic-gate 		for (port = 1; (no_cpr == 0) && (port <= nports); port++) {
66247c478bd9Sstevel@tonic-gate 			usba_dev = hubd->h_usba_devices[port];
66257c478bd9Sstevel@tonic-gate 			if (usba_dev != NULL) {
66267c478bd9Sstevel@tonic-gate 				mutex_enter(&usba_dev->usb_mutex);
66277c478bd9Sstevel@tonic-gate 				no_cpr += usba_dev->usb_no_cpr;
66287c478bd9Sstevel@tonic-gate 				mutex_exit(&usba_dev->usb_mutex);
66297c478bd9Sstevel@tonic-gate 			}
66307c478bd9Sstevel@tonic-gate 		}
66317c478bd9Sstevel@tonic-gate 		if (no_cpr > 0) {
66327c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
66337c478bd9Sstevel@tonic-gate 			    "Children busy - can't checkpoint");
66347c478bd9Sstevel@tonic-gate 			/* remain in same state to fail checkpoint */
66357c478bd9Sstevel@tonic-gate 
66367c478bd9Sstevel@tonic-gate 			break;
66377c478bd9Sstevel@tonic-gate 		} else {
66387c478bd9Sstevel@tonic-gate 			/*
66397c478bd9Sstevel@tonic-gate 			 * do not suspend if our hotplug thread
66407c478bd9Sstevel@tonic-gate 			 * or the deathrow thread is active
66417c478bd9Sstevel@tonic-gate 			 */
66427c478bd9Sstevel@tonic-gate 			if ((hubd->h_hotplug_thread > 1) ||
66437c478bd9Sstevel@tonic-gate 			    (hubd->h_cleanup_active == B_TRUE)) {
66447c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
66457c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
66467c478bd9Sstevel@tonic-gate 				    "hotplug thread active  - can't cpr");
66477c478bd9Sstevel@tonic-gate 				/* remain in same state to fail checkpoint */
66487c478bd9Sstevel@tonic-gate 
66497c478bd9Sstevel@tonic-gate 				break;
66507c478bd9Sstevel@tonic-gate 			}
66517c478bd9Sstevel@tonic-gate 
66527c478bd9Sstevel@tonic-gate 			/* quiesce ourselves now */
66537c478bd9Sstevel@tonic-gate 			hubd->h_dev_state = USB_DEV_SUSPENDED;
66547c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
66557c478bd9Sstevel@tonic-gate 
66567c478bd9Sstevel@tonic-gate 			/* close all the open pipes of our children */
66577c478bd9Sstevel@tonic-gate 			for (port = 1; port <= nports; port++) {
66587c478bd9Sstevel@tonic-gate 				usba_dev = hubd->h_usba_devices[port];
66597c478bd9Sstevel@tonic-gate 				if (usba_dev != NULL) {
66607c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
66617c478bd9Sstevel@tonic-gate 					usba_persistent_pipe_close(usba_dev);
66627c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
66637c478bd9Sstevel@tonic-gate 				}
66647c478bd9Sstevel@tonic-gate 			}
66657c478bd9Sstevel@tonic-gate 			/*
66667c478bd9Sstevel@tonic-gate 			 * turn off power to all the ports so that we
66677c478bd9Sstevel@tonic-gate 			 * don't see any spurious activity
66687c478bd9Sstevel@tonic-gate 			 */
66697c478bd9Sstevel@tonic-gate 			(void) hubd_disable_all_port_power(hubd);
66707c478bd9Sstevel@tonic-gate 
66717c478bd9Sstevel@tonic-gate 			/*
66727c478bd9Sstevel@tonic-gate 			 * if we are the root hub, we close our pipes
66737c478bd9Sstevel@tonic-gate 			 * ourselves.
66747c478bd9Sstevel@tonic-gate 			 */
66757c478bd9Sstevel@tonic-gate 			if (usba_is_root_hub(hubd->h_dip)) {
66767c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
66777c478bd9Sstevel@tonic-gate 				usba_persistent_pipe_close(
66787c478bd9Sstevel@tonic-gate 				    usba_get_usba_device(hubd->h_dip));
66797c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
66807c478bd9Sstevel@tonic-gate 			}
66817c478bd9Sstevel@tonic-gate 			rval = USB_SUCCESS;
66827c478bd9Sstevel@tonic-gate 
66837c478bd9Sstevel@tonic-gate 			break;
66847c478bd9Sstevel@tonic-gate 		}
66857c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
66867c478bd9Sstevel@tonic-gate 	default:
66877c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
66887c478bd9Sstevel@tonic-gate 		    "hubd_cpr_suspend: Illegal dev state=%d",
66897c478bd9Sstevel@tonic-gate 		    hubd->h_dev_state);
66907c478bd9Sstevel@tonic-gate 
66917c478bd9Sstevel@tonic-gate 		break;
66927c478bd9Sstevel@tonic-gate 	}
66937c478bd9Sstevel@tonic-gate 
66947c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, hubd->h_dip, 0);
66957c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
66967c478bd9Sstevel@tonic-gate 
66977c478bd9Sstevel@tonic-gate 	return (rval);
66987c478bd9Sstevel@tonic-gate }
66997c478bd9Sstevel@tonic-gate 
67007c478bd9Sstevel@tonic-gate static void
67017c478bd9Sstevel@tonic-gate hubd_cpr_resume(dev_info_t *dip)
67027c478bd9Sstevel@tonic-gate {
67037c478bd9Sstevel@tonic-gate 	int	rval, circ;
67047c478bd9Sstevel@tonic-gate 
67057c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
67067c478bd9Sstevel@tonic-gate 	/*
67077c478bd9Sstevel@tonic-gate 	 * if we are the root hub, we open our pipes
67087c478bd9Sstevel@tonic-gate 	 * ourselves.
67097c478bd9Sstevel@tonic-gate 	 */
67107c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
67117c478bd9Sstevel@tonic-gate 		rval = usba_persistent_pipe_open(
67127c478bd9Sstevel@tonic-gate 		    usba_get_usba_device(dip));
67137c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
67147c478bd9Sstevel@tonic-gate 	}
67157c478bd9Sstevel@tonic-gate 	(void) hubd_restore_state_cb(dip);
67167c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
67177c478bd9Sstevel@tonic-gate }
67187c478bd9Sstevel@tonic-gate 
67197c478bd9Sstevel@tonic-gate 
67207c478bd9Sstevel@tonic-gate /*
67217c478bd9Sstevel@tonic-gate  * hubd_restore_state_cb
67227c478bd9Sstevel@tonic-gate  *	Event callback to restore device state
67237c478bd9Sstevel@tonic-gate  */
67247c478bd9Sstevel@tonic-gate static int
67257c478bd9Sstevel@tonic-gate hubd_restore_state_cb(dev_info_t *dip)
67267c478bd9Sstevel@tonic-gate {
67277c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
67287c478bd9Sstevel@tonic-gate 
67297c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
67307c478bd9Sstevel@tonic-gate 	    "hubd_restore_state_cb: Begin");
67317c478bd9Sstevel@tonic-gate 
67327c478bd9Sstevel@tonic-gate 	/* restore the state of this device */
67337c478bd9Sstevel@tonic-gate 	hubd_restore_device_state(dip, hubd);
67347c478bd9Sstevel@tonic-gate 
67357c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
67367c478bd9Sstevel@tonic-gate }
67377c478bd9Sstevel@tonic-gate 
67387c478bd9Sstevel@tonic-gate 
67397c478bd9Sstevel@tonic-gate /*
67407c478bd9Sstevel@tonic-gate  * registering for events
67417c478bd9Sstevel@tonic-gate  */
67427c478bd9Sstevel@tonic-gate static int
67437c478bd9Sstevel@tonic-gate hubd_register_events(hubd_t *hubd)
67447c478bd9Sstevel@tonic-gate {
67457c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
67467c478bd9Sstevel@tonic-gate 
67477c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(hubd->h_dip)) {
67487c478bd9Sstevel@tonic-gate 		hubd_register_cpr_callback(hubd);
67497c478bd9Sstevel@tonic-gate 	} else {
67507c478bd9Sstevel@tonic-gate 		rval = usb_register_event_cbs(hubd->h_dip, &hubd_events, 0);
67517c478bd9Sstevel@tonic-gate 	}
67527c478bd9Sstevel@tonic-gate 
67537c478bd9Sstevel@tonic-gate 	return (rval);
67547c478bd9Sstevel@tonic-gate }
67557c478bd9Sstevel@tonic-gate 
67567c478bd9Sstevel@tonic-gate 
67577c478bd9Sstevel@tonic-gate /*
67587c478bd9Sstevel@tonic-gate  * hubd cpr callback related functions
67597c478bd9Sstevel@tonic-gate  *
67607c478bd9Sstevel@tonic-gate  * hubd_cpr_post_user_callb:
67617c478bd9Sstevel@tonic-gate  *	This function is called during checkpoint & resume -
67627c478bd9Sstevel@tonic-gate  *		1. after user threads are stopped during checkpoint
67637c478bd9Sstevel@tonic-gate  *		2. after kernel threads are resumed during resume
67647c478bd9Sstevel@tonic-gate  */
67657c478bd9Sstevel@tonic-gate /* ARGSUSED */
67667c478bd9Sstevel@tonic-gate static boolean_t
67677c478bd9Sstevel@tonic-gate hubd_cpr_post_user_callb(void *arg, int code)
67687c478bd9Sstevel@tonic-gate {
67697c478bd9Sstevel@tonic-gate 	hubd_cpr_t	*cpr_cb = (hubd_cpr_t *)arg;
67707c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = cpr_cb->statep;
67717c478bd9Sstevel@tonic-gate 	int		retry = 0;
67727c478bd9Sstevel@tonic-gate 
67737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
67747c478bd9Sstevel@tonic-gate 	    "hubd_cpr_post_user_callb");
67757c478bd9Sstevel@tonic-gate 
67767c478bd9Sstevel@tonic-gate 	switch (code) {
67777c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_CHKPT:
67787c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
67797c478bd9Sstevel@tonic-gate 		    "hubd_cpr_post_user_callb: CB_CODE_CPR_CHKPT");
67807c478bd9Sstevel@tonic-gate 
67817c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
67827c478bd9Sstevel@tonic-gate 
67837c478bd9Sstevel@tonic-gate 		/* turn off deathrow thread */
67847c478bd9Sstevel@tonic-gate 		hubd->h_cleanup_enabled = B_FALSE;
67857c478bd9Sstevel@tonic-gate 
67867c478bd9Sstevel@tonic-gate 		/* give up if deathrow thread doesn't exit */
67877c478bd9Sstevel@tonic-gate 		while ((hubd->h_cleanup_active == B_TRUE) && (retry++ < 3)) {
67887c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
67897c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(hubd_dip_cleanup_delay));
67907c478bd9Sstevel@tonic-gate 
67917c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_EVENTS, hubd->h_log_handle,
67927c478bd9Sstevel@tonic-gate 			    "hubd_cpr_post_user_callb, waiting for "
67937c478bd9Sstevel@tonic-gate 			    "deathrow thread to exit");
67947c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
67957c478bd9Sstevel@tonic-gate 		}
67967c478bd9Sstevel@tonic-gate 
67977c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
67987c478bd9Sstevel@tonic-gate 
67997c478bd9Sstevel@tonic-gate 		/* save the state of the device */
68007c478bd9Sstevel@tonic-gate 		(void) hubd_pre_suspend_event_cb(hubd->h_dip);
68017c478bd9Sstevel@tonic-gate 
68027c478bd9Sstevel@tonic-gate 		return (B_TRUE);
68037c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_RESUME:
68047c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
68057c478bd9Sstevel@tonic-gate 		    "hubd_cpr_post_user_callb: CB_CODE_CPR_RESUME");
68067c478bd9Sstevel@tonic-gate 
68077c478bd9Sstevel@tonic-gate 		/* restore the state of the device */
68087c478bd9Sstevel@tonic-gate 		(void) hubd_post_resume_event_cb(hubd->h_dip);
68097c478bd9Sstevel@tonic-gate 
68107c478bd9Sstevel@tonic-gate 		/* turn on deathrow thread */
68117c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
68127c478bd9Sstevel@tonic-gate 		hubd->h_cleanup_enabled = B_TRUE;
68137c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
68147c478bd9Sstevel@tonic-gate 
68157c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
68167c478bd9Sstevel@tonic-gate 
68177c478bd9Sstevel@tonic-gate 		return (B_TRUE);
68187c478bd9Sstevel@tonic-gate 	default:
68197c478bd9Sstevel@tonic-gate 
68207c478bd9Sstevel@tonic-gate 		return (B_FALSE);
68217c478bd9Sstevel@tonic-gate 	}
68227c478bd9Sstevel@tonic-gate 
68237c478bd9Sstevel@tonic-gate }
68247c478bd9Sstevel@tonic-gate 
68257c478bd9Sstevel@tonic-gate 
68267c478bd9Sstevel@tonic-gate /* register callback with cpr framework */
68277c478bd9Sstevel@tonic-gate void
68287c478bd9Sstevel@tonic-gate hubd_register_cpr_callback(hubd_t *hubd)
68297c478bd9Sstevel@tonic-gate {
68307c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
68317c478bd9Sstevel@tonic-gate 	    "hubd_register_cpr_callback");
68327c478bd9Sstevel@tonic-gate 
68337c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
68347c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb =
68357c478bd9Sstevel@tonic-gate 	    (hubd_cpr_t *)kmem_zalloc(sizeof (hubd_cpr_t), KM_SLEEP);
68367c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
68377c478bd9Sstevel@tonic-gate 	mutex_init(&hubd->h_cpr_cb->lockp, NULL, MUTEX_DRIVER,
68387c478bd9Sstevel@tonic-gate 	    hubd->h_dev_data->dev_iblock_cookie);
68397c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->statep = hubd;
68407c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->cpr.cc_lockp = &hubd->h_cpr_cb->lockp;
68417c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->cpr.cc_id = callb_add(hubd_cpr_post_user_callb,
68427c478bd9Sstevel@tonic-gate 	    (void *)hubd->h_cpr_cb, CB_CL_CPR_POST_USER, "hubd");
68437c478bd9Sstevel@tonic-gate }
68447c478bd9Sstevel@tonic-gate 
68457c478bd9Sstevel@tonic-gate 
68467c478bd9Sstevel@tonic-gate /* unregister callback with cpr framework */
68477c478bd9Sstevel@tonic-gate void
68487c478bd9Sstevel@tonic-gate hubd_unregister_cpr_callback(hubd_t *hubd)
68497c478bd9Sstevel@tonic-gate {
68507c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
68517c478bd9Sstevel@tonic-gate 	    "hubd_unregister_cpr_callback");
68527c478bd9Sstevel@tonic-gate 
68537c478bd9Sstevel@tonic-gate 	if (hubd->h_cpr_cb) {
68547c478bd9Sstevel@tonic-gate 		(void) callb_delete(hubd->h_cpr_cb->cpr.cc_id);
68557c478bd9Sstevel@tonic-gate 		mutex_destroy(&hubd->h_cpr_cb->lockp);
68567c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
68577c478bd9Sstevel@tonic-gate 		kmem_free(hubd->h_cpr_cb, sizeof (hubd_cpr_t));
68587c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
68597c478bd9Sstevel@tonic-gate 	}
68607c478bd9Sstevel@tonic-gate }
68617c478bd9Sstevel@tonic-gate 
68627c478bd9Sstevel@tonic-gate 
68637c478bd9Sstevel@tonic-gate /*
68647c478bd9Sstevel@tonic-gate  * Power management
68657c478bd9Sstevel@tonic-gate  *
68667c478bd9Sstevel@tonic-gate  * create the pm components required for power management
68677c478bd9Sstevel@tonic-gate  */
68687c478bd9Sstevel@tonic-gate static void
68697c478bd9Sstevel@tonic-gate hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd)
68707c478bd9Sstevel@tonic-gate {
68717c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
68727c478bd9Sstevel@tonic-gate 
68737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
68747c478bd9Sstevel@tonic-gate 	    "hubd_create_pm_components: Begin");
68757c478bd9Sstevel@tonic-gate 
68767c478bd9Sstevel@tonic-gate 	/* Allocate the state structure */
68777c478bd9Sstevel@tonic-gate 	hubpm = kmem_zalloc(sizeof (hub_power_t), KM_SLEEP);
68787c478bd9Sstevel@tonic-gate 
68797c478bd9Sstevel@tonic-gate 	hubd->h_hubpm = hubpm;
68807c478bd9Sstevel@tonic-gate 	hubpm->hubp_hubd = hubd;
68817c478bd9Sstevel@tonic-gate 	hubpm->hubp_pm_capabilities = 0;
68827c478bd9Sstevel@tonic-gate 	hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
68837c478bd9Sstevel@tonic-gate 	hubpm->hubp_time_at_full_power = ddi_get_time();
68847c478bd9Sstevel@tonic-gate 	hubpm->hubp_min_pm_threshold = hubdi_min_pm_threshold;
68857c478bd9Sstevel@tonic-gate 
68867c478bd9Sstevel@tonic-gate 	/* alloc memory to save power states of children */
68877c478bd9Sstevel@tonic-gate 	hubpm->hubp_child_pwrstate = (uint8_t *)
68887c478bd9Sstevel@tonic-gate 		kmem_zalloc(MAX_PORTS + 1, KM_SLEEP);
68897c478bd9Sstevel@tonic-gate 
68907c478bd9Sstevel@tonic-gate 	/*
68917c478bd9Sstevel@tonic-gate 	 * if the enable remote wakeup fails
68927c478bd9Sstevel@tonic-gate 	 * we still want to enable
68937c478bd9Sstevel@tonic-gate 	 * parent notification so we can PM the children
68947c478bd9Sstevel@tonic-gate 	 */
68957c478bd9Sstevel@tonic-gate 	usb_enable_parent_notification(dip);
68967c478bd9Sstevel@tonic-gate 
68977c478bd9Sstevel@tonic-gate 	if (usb_handle_remote_wakeup(dip,
68987c478bd9Sstevel@tonic-gate 	    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
68997c478bd9Sstevel@tonic-gate 		uint_t		pwr_states;
69007c478bd9Sstevel@tonic-gate 
69017c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
69027c478bd9Sstevel@tonic-gate 		    "hubd_create_pm_components: "
69037c478bd9Sstevel@tonic-gate 		    "Remote Wakeup Enabled");
69047c478bd9Sstevel@tonic-gate 
69057c478bd9Sstevel@tonic-gate 		if (usb_create_pm_components(dip, &pwr_states) ==
69067c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
69077c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
69087c478bd9Sstevel@tonic-gate 			hubpm->hubp_wakeup_enabled = 1;
69097c478bd9Sstevel@tonic-gate 			hubpm->hubp_pwr_states = (uint8_t)pwr_states;
69107c478bd9Sstevel@tonic-gate 
69117c478bd9Sstevel@tonic-gate 			/* we are busy now till end of the attach */
69127c478bd9Sstevel@tonic-gate 			hubd_pm_busy_component(hubd, dip, 0);
69137c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
69147c478bd9Sstevel@tonic-gate 
69157c478bd9Sstevel@tonic-gate 			/* bring the device to full power */
69167c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0,
69177c478bd9Sstevel@tonic-gate 			    USB_DEV_OS_FULL_PWR);
69187c478bd9Sstevel@tonic-gate 		}
69197c478bd9Sstevel@tonic-gate 	}
69207c478bd9Sstevel@tonic-gate 
69217c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
69227c478bd9Sstevel@tonic-gate 	    "hubd_create_pm_components: END");
69237c478bd9Sstevel@tonic-gate }
69247c478bd9Sstevel@tonic-gate 
69257c478bd9Sstevel@tonic-gate 
69267c478bd9Sstevel@tonic-gate /*
69277c478bd9Sstevel@tonic-gate  * Attachment point management
69287c478bd9Sstevel@tonic-gate  */
69297c478bd9Sstevel@tonic-gate /* ARGSUSED */
69307c478bd9Sstevel@tonic-gate int
69317c478bd9Sstevel@tonic-gate usba_hubdi_open(dev_info_t *dip, dev_t *devp, int flags, int otyp,
69327c478bd9Sstevel@tonic-gate 	cred_t *credp)
69337c478bd9Sstevel@tonic-gate {
69347c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
69357c478bd9Sstevel@tonic-gate 
69367c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
69377c478bd9Sstevel@tonic-gate 		return (EINVAL);
69387c478bd9Sstevel@tonic-gate 
69397c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
69407c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
69417c478bd9Sstevel@tonic-gate 		return (ENXIO);
69427c478bd9Sstevel@tonic-gate 	}
69437c478bd9Sstevel@tonic-gate 
69447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
69457c478bd9Sstevel@tonic-gate 	    "hubd_open:");
69467c478bd9Sstevel@tonic-gate 
69477c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
69487c478bd9Sstevel@tonic-gate 	if ((flags & FEXCL) && (hubd->h_softstate & HUBD_SS_ISOPEN)) {
69497c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
69507c478bd9Sstevel@tonic-gate 
69517c478bd9Sstevel@tonic-gate 		return (EBUSY);
69527c478bd9Sstevel@tonic-gate 	}
69537c478bd9Sstevel@tonic-gate 
69547c478bd9Sstevel@tonic-gate 	hubd->h_softstate |= HUBD_SS_ISOPEN;
69557c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
69567c478bd9Sstevel@tonic-gate 
69577c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "opened");
69587c478bd9Sstevel@tonic-gate 
69597c478bd9Sstevel@tonic-gate 	return (0);
69607c478bd9Sstevel@tonic-gate }
69617c478bd9Sstevel@tonic-gate 
69627c478bd9Sstevel@tonic-gate 
69637c478bd9Sstevel@tonic-gate /* ARGSUSED */
69647c478bd9Sstevel@tonic-gate int
69657c478bd9Sstevel@tonic-gate usba_hubdi_close(dev_info_t *dip, dev_t dev, int flag, int otyp,
69667c478bd9Sstevel@tonic-gate 	cred_t *credp)
69677c478bd9Sstevel@tonic-gate {
69687c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
69697c478bd9Sstevel@tonic-gate 
69707c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
69717c478bd9Sstevel@tonic-gate 		return (EINVAL);
69727c478bd9Sstevel@tonic-gate 	}
69737c478bd9Sstevel@tonic-gate 
69747c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
69757c478bd9Sstevel@tonic-gate 
69767c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
69777c478bd9Sstevel@tonic-gate 		return (ENXIO);
69787c478bd9Sstevel@tonic-gate 	}
69797c478bd9Sstevel@tonic-gate 
69807c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "hubd_close:");
69817c478bd9Sstevel@tonic-gate 
69827c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
69837c478bd9Sstevel@tonic-gate 	hubd->h_softstate &= ~HUBD_SS_ISOPEN;
69847c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
69857c478bd9Sstevel@tonic-gate 
69867c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "closed");
69877c478bd9Sstevel@tonic-gate 
69887c478bd9Sstevel@tonic-gate 	return (0);
69897c478bd9Sstevel@tonic-gate }
69907c478bd9Sstevel@tonic-gate 
69917c478bd9Sstevel@tonic-gate 
69927c478bd9Sstevel@tonic-gate /*
69937c478bd9Sstevel@tonic-gate  * hubd_ioctl: cfgadm controls
69947c478bd9Sstevel@tonic-gate  */
69957c478bd9Sstevel@tonic-gate /* ARGSUSED */
69967c478bd9Sstevel@tonic-gate int
69977c478bd9Sstevel@tonic-gate usba_hubdi_ioctl(dev_info_t *self, dev_t dev, int cmd, intptr_t arg,
69987c478bd9Sstevel@tonic-gate 	int mode, cred_t *credp, int *rvalp)
69997c478bd9Sstevel@tonic-gate {
70007c478bd9Sstevel@tonic-gate 	int			rv = 0;
70017c478bd9Sstevel@tonic-gate 	char			*msg;	/* for messages */
70027c478bd9Sstevel@tonic-gate 	hubd_t			*hubd;
70037c478bd9Sstevel@tonic-gate 	usb_port_t		port = 0;
70047c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
70057c478bd9Sstevel@tonic-gate 	dev_info_t		*rh_dip;
70067c478bd9Sstevel@tonic-gate 	devctl_ap_state_t	ap_state;
70077c478bd9Sstevel@tonic-gate 	struct devctl_iocdata	*dcp = NULL;
70087c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	prev_pipe_state = 0;
70097c478bd9Sstevel@tonic-gate 	int			circ, rh_circ, prh_circ;
70107c478bd9Sstevel@tonic-gate 
70117c478bd9Sstevel@tonic-gate 	if ((hubd = hubd_get_soft_state(self)) == NULL) {
70127c478bd9Sstevel@tonic-gate 
70137c478bd9Sstevel@tonic-gate 		return (ENXIO);
70147c478bd9Sstevel@tonic-gate 	}
70157c478bd9Sstevel@tonic-gate 
70167c478bd9Sstevel@tonic-gate 	rh_dip = hubd->h_usba_device->usb_root_hub_dip;
70177c478bd9Sstevel@tonic-gate 
70187c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
70197c478bd9Sstevel@tonic-gate 	    "usba_hubdi_ioctl: "
70207c478bd9Sstevel@tonic-gate 	    "cmd=%x, arg=%lx, mode=%x, cred=%p, rval=%p dev=0x%lx",
70217c478bd9Sstevel@tonic-gate 	    cmd, arg, mode, credp, rvalp, dev);
70227c478bd9Sstevel@tonic-gate 
70237c478bd9Sstevel@tonic-gate 	/* read devctl ioctl data */
70247c478bd9Sstevel@tonic-gate 	if ((cmd != DEVCTL_AP_CONTROL) &&
70257c478bd9Sstevel@tonic-gate 	    (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)) {
70267c478bd9Sstevel@tonic-gate 
70277c478bd9Sstevel@tonic-gate 		return (EFAULT);
70287c478bd9Sstevel@tonic-gate 	}
70297c478bd9Sstevel@tonic-gate 
70307c478bd9Sstevel@tonic-gate 	/*
70317c478bd9Sstevel@tonic-gate 	 * make sure the hub is connected before trying any
70327c478bd9Sstevel@tonic-gate 	 * of the following operations:
70337c478bd9Sstevel@tonic-gate 	 * configure, connect, disconnect
70347c478bd9Sstevel@tonic-gate 	 */
70357c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
70367c478bd9Sstevel@tonic-gate 
70377c478bd9Sstevel@tonic-gate 	switch (cmd) {
70387c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
70397c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
70407c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
70417c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_DISCONNECTED) {
70427c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
70437c478bd9Sstevel@tonic-gate 			    "hubd: already gone");
70447c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
70457c478bd9Sstevel@tonic-gate 			if (dcp) {
70467c478bd9Sstevel@tonic-gate 				ndi_dc_freehdl(dcp);
70477c478bd9Sstevel@tonic-gate 			}
70487c478bd9Sstevel@tonic-gate 
70497c478bd9Sstevel@tonic-gate 			return (EIO);
70507c478bd9Sstevel@tonic-gate 		}
70517c478bd9Sstevel@tonic-gate 
70527c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
70537c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
70547c478bd9Sstevel@tonic-gate 		if ((port = hubd_get_port_num(hubd, dcp)) == 0) {
70557c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
70567c478bd9Sstevel@tonic-gate 			    "hubd: bad port");
70577c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
70587c478bd9Sstevel@tonic-gate 			if (dcp) {
70597c478bd9Sstevel@tonic-gate 				ndi_dc_freehdl(dcp);
70607c478bd9Sstevel@tonic-gate 			}
70617c478bd9Sstevel@tonic-gate 
70627c478bd9Sstevel@tonic-gate 			return (EINVAL);
70637c478bd9Sstevel@tonic-gate 		}
70647c478bd9Sstevel@tonic-gate 		break;
70657c478bd9Sstevel@tonic-gate 
70667c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
70677c478bd9Sstevel@tonic-gate 
70687c478bd9Sstevel@tonic-gate 		break;
70697c478bd9Sstevel@tonic-gate 	default:
70707c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
70717c478bd9Sstevel@tonic-gate 		if (dcp) {
70727c478bd9Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
70737c478bd9Sstevel@tonic-gate 		}
70747c478bd9Sstevel@tonic-gate 
70757c478bd9Sstevel@tonic-gate 		return (ENOTTY);
70767c478bd9Sstevel@tonic-gate 	}
70777c478bd9Sstevel@tonic-gate 
70787c478bd9Sstevel@tonic-gate 	/* should not happen, just in case */
70797c478bd9Sstevel@tonic-gate 	if (hubd->h_dev_state == USB_DEV_SUSPENDED) {
70807c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
70817c478bd9Sstevel@tonic-gate 		if (dcp) {
70827c478bd9Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
70837c478bd9Sstevel@tonic-gate 		}
70847c478bd9Sstevel@tonic-gate 
70857c478bd9Sstevel@tonic-gate 		return (EIO);
70867c478bd9Sstevel@tonic-gate 	}
70877c478bd9Sstevel@tonic-gate 
70887c478bd9Sstevel@tonic-gate 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
70897c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
70907c478bd9Sstevel@tonic-gate 
70917c478bd9Sstevel@tonic-gate 	/* go full power */
70927c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
70937c478bd9Sstevel@tonic-gate 
70947c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
70957c478bd9Sstevel@tonic-gate 	ndi_devi_enter(rh_dip, &rh_circ);
70967c478bd9Sstevel@tonic-gate 	ndi_devi_enter(hubd->h_dip, &circ);
70977c478bd9Sstevel@tonic-gate 
70987c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
70997c478bd9Sstevel@tonic-gate 
71007c478bd9Sstevel@tonic-gate 	/* stop polling iff it was active */
71017c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
71027c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
71037c478bd9Sstevel@tonic-gate 		(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
71047c478bd9Sstevel@tonic-gate 							    USB_FLAGS_SLEEP);
71057c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
71067c478bd9Sstevel@tonic-gate 
71077c478bd9Sstevel@tonic-gate 		if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
71087c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
71097c478bd9Sstevel@tonic-gate 		}
71107c478bd9Sstevel@tonic-gate 	}
71117c478bd9Sstevel@tonic-gate 
71127c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread++;
71137c478bd9Sstevel@tonic-gate 
71147c478bd9Sstevel@tonic-gate 	switch (cmd) {
71157c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
71167c478bd9Sstevel@tonic-gate 		if (hubd_delete_child(hubd, port,
71177c478bd9Sstevel@tonic-gate 		    NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS) {
71187c478bd9Sstevel@tonic-gate 			rv = EIO;
71197c478bd9Sstevel@tonic-gate 		}
71207c478bd9Sstevel@tonic-gate 
71217c478bd9Sstevel@tonic-gate 		break;
71227c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
71237c478bd9Sstevel@tonic-gate 		if (hubd_delete_child(hubd, port,
71247c478bd9Sstevel@tonic-gate 		    NDI_UNCONFIG, B_FALSE) != USB_SUCCESS) {
71257c478bd9Sstevel@tonic-gate 			rv = EIO;
71267c478bd9Sstevel@tonic-gate 		}
71277c478bd9Sstevel@tonic-gate 
71287c478bd9Sstevel@tonic-gate 		break;
71297c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
71307c478bd9Sstevel@tonic-gate 		/* toggle port */
71317c478bd9Sstevel@tonic-gate 		if (hubd_toggle_port(hubd, port) != USB_SUCCESS) {
71327c478bd9Sstevel@tonic-gate 			(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
71337c478bd9Sstevel@tonic-gate 			rv = EIO;
71347c478bd9Sstevel@tonic-gate 
71357c478bd9Sstevel@tonic-gate 			break;
71367c478bd9Sstevel@tonic-gate 		}
71377c478bd9Sstevel@tonic-gate 
71387c478bd9Sstevel@tonic-gate 		(void) hubd_handle_port_connect(hubd, port);
71397c478bd9Sstevel@tonic-gate 		child_dip = hubd_get_child_dip(hubd, port);
71407c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
71417c478bd9Sstevel@tonic-gate 
71427c478bd9Sstevel@tonic-gate 		ndi_devi_exit(hubd->h_dip, circ);
71437c478bd9Sstevel@tonic-gate 		ndi_devi_exit(rh_dip, rh_circ);
71447c478bd9Sstevel@tonic-gate 		ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
71457c478bd9Sstevel@tonic-gate 		if ((child_dip == NULL) ||
71467c478bd9Sstevel@tonic-gate 		    (ndi_devi_online(child_dip, 0) != NDI_SUCCESS)) {
71477c478bd9Sstevel@tonic-gate 			rv = EIO;
71487c478bd9Sstevel@tonic-gate 		}
71497c478bd9Sstevel@tonic-gate 		ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
71507c478bd9Sstevel@tonic-gate 		ndi_devi_enter(rh_dip, &rh_circ);
71517c478bd9Sstevel@tonic-gate 		ndi_devi_enter(hubd->h_dip, &circ);
71527c478bd9Sstevel@tonic-gate 
71537c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
71547c478bd9Sstevel@tonic-gate 
71557c478bd9Sstevel@tonic-gate 		break;
71567c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
71577c478bd9Sstevel@tonic-gate 		switch (hubd_cfgadm_state(hubd, port)) {
71587c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_DISCONNECTED:
71597c478bd9Sstevel@tonic-gate 			/* port previously 'disconnected' by cfgadm */
71607c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_DISCONNECTED;
71617c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
71627c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
71637c478bd9Sstevel@tonic-gate 
71647c478bd9Sstevel@tonic-gate 			break;
71657c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_UNCONFIGURED:
71667c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_CONNECTED;
71677c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
71687c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
71697c478bd9Sstevel@tonic-gate 
71707c478bd9Sstevel@tonic-gate 			break;
71717c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_CONFIGURED:
71727c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_CONNECTED;
71737c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
71747c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
71757c478bd9Sstevel@tonic-gate 
71767c478bd9Sstevel@tonic-gate 			break;
71777c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_STILL_REFERENCED:
71787c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_EMPTY;
71797c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
71807c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_UNUSABLE;
71817c478bd9Sstevel@tonic-gate 
71827c478bd9Sstevel@tonic-gate 			break;
71837c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_EMPTY:
71847c478bd9Sstevel@tonic-gate 		default:
71857c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_EMPTY;
71867c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
71877c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
71887c478bd9Sstevel@tonic-gate 
71897c478bd9Sstevel@tonic-gate 			break;
71907c478bd9Sstevel@tonic-gate 		}
71917c478bd9Sstevel@tonic-gate 
71927c478bd9Sstevel@tonic-gate 		ap_state.ap_last_change = (time_t)-1;
71937c478bd9Sstevel@tonic-gate 		ap_state.ap_error_code = 0;
71947c478bd9Sstevel@tonic-gate 		ap_state.ap_in_transition = 0;
71957c478bd9Sstevel@tonic-gate 
71967c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
71977c478bd9Sstevel@tonic-gate 		    "DEVCTL_AP_GETSTATE: "
71987c478bd9Sstevel@tonic-gate 		    "ostate=0x%x, rstate=0x%x, condition=0x%x",
71997c478bd9Sstevel@tonic-gate 		    ap_state.ap_ostate,
72007c478bd9Sstevel@tonic-gate 		    ap_state.ap_rstate, ap_state.ap_condition);
72017c478bd9Sstevel@tonic-gate 
72027c478bd9Sstevel@tonic-gate 		/* copy the return-AP-state information to the user space */
72037c478bd9Sstevel@tonic-gate 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
72047c478bd9Sstevel@tonic-gate 			rv = EFAULT;
72057c478bd9Sstevel@tonic-gate 		}
72067c478bd9Sstevel@tonic-gate 
72077c478bd9Sstevel@tonic-gate 		break;
72087c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
72097c478bd9Sstevel@tonic-gate 	{
72107c478bd9Sstevel@tonic-gate 		/*
72117c478bd9Sstevel@tonic-gate 		 * Generic devctl for hardware-specific functionality.
72127c478bd9Sstevel@tonic-gate 		 * For list of sub-commands see hubd_impl.h
72137c478bd9Sstevel@tonic-gate 		 */
72147c478bd9Sstevel@tonic-gate 		hubd_ioctl_data_t	ioc;	/* for 64 byte copies */
72157c478bd9Sstevel@tonic-gate 
72167c478bd9Sstevel@tonic-gate 		/* copy user ioctl data in first */
72177c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
72187c478bd9Sstevel@tonic-gate 		if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
72197c478bd9Sstevel@tonic-gate 			hubd_ioctl_data_32_t ioc32;
72207c478bd9Sstevel@tonic-gate 
72217c478bd9Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, (void *)&ioc32,
72227c478bd9Sstevel@tonic-gate 				sizeof (ioc32), mode) != 0) {
72237c478bd9Sstevel@tonic-gate 				rv = EFAULT;
72247c478bd9Sstevel@tonic-gate 
72257c478bd9Sstevel@tonic-gate 				break;
72267c478bd9Sstevel@tonic-gate 			}
72277c478bd9Sstevel@tonic-gate 			ioc.cmd		= (uint_t)ioc32.cmd;
72287c478bd9Sstevel@tonic-gate 			ioc.port	= (uint_t)ioc32.port;
72297c478bd9Sstevel@tonic-gate 			ioc.get_size	= (uint_t)ioc32.get_size;
72307c478bd9Sstevel@tonic-gate 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
72317c478bd9Sstevel@tonic-gate 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
72327c478bd9Sstevel@tonic-gate 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
72337c478bd9Sstevel@tonic-gate 		} else
72347c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
72357c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
72367c478bd9Sstevel@tonic-gate 		    mode) != 0) {
72377c478bd9Sstevel@tonic-gate 			rv = EFAULT;
72387c478bd9Sstevel@tonic-gate 
72397c478bd9Sstevel@tonic-gate 			break;
72407c478bd9Sstevel@tonic-gate 		}
72417c478bd9Sstevel@tonic-gate 
72427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
72437c478bd9Sstevel@tonic-gate 		    "DEVCTL_AP_CONTROL: ioc: cmd=0x%x port=%d get_size=%d"
72447c478bd9Sstevel@tonic-gate 		    "\n\tbuf=0x%p, bufsiz=%d,  misc_arg=%d", ioc.cmd,
72457c478bd9Sstevel@tonic-gate 		    ioc.port, ioc.get_size, ioc.buf, ioc.bufsiz, ioc.misc_arg);
72467c478bd9Sstevel@tonic-gate 
72477c478bd9Sstevel@tonic-gate 		/*
72487c478bd9Sstevel@tonic-gate 		 * To avoid BE/LE and 32/64 issues, a get_size always
72497c478bd9Sstevel@tonic-gate 		 * returns a 32-bit number.
72507c478bd9Sstevel@tonic-gate 		 */
72517c478bd9Sstevel@tonic-gate 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
72527c478bd9Sstevel@tonic-gate 			rv = EINVAL;
72537c478bd9Sstevel@tonic-gate 
72547c478bd9Sstevel@tonic-gate 			break;
72557c478bd9Sstevel@tonic-gate 		}
72567c478bd9Sstevel@tonic-gate 
72577c478bd9Sstevel@tonic-gate 		switch (ioc.cmd) {
72587c478bd9Sstevel@tonic-gate 		case USB_DESCR_TYPE_DEV:
72597c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_DEVICE_DESC";
72607c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
72617c478bd9Sstevel@tonic-gate 				/* uint32 so this works 32/64 */
72627c478bd9Sstevel@tonic-gate 				uint32_t size = sizeof (usb_dev_descr_t);
72637c478bd9Sstevel@tonic-gate 
72647c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size, ioc.buf,
72657c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
72667c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
72677c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
72687c478bd9Sstevel@tonic-gate 					    "%s: get_size copyout failed", msg);
72697c478bd9Sstevel@tonic-gate 					rv = EIO;
72707c478bd9Sstevel@tonic-gate 
72717c478bd9Sstevel@tonic-gate 					break;
72727c478bd9Sstevel@tonic-gate 				}
72737c478bd9Sstevel@tonic-gate 			} else {	/* send out the actual descr */
72747c478bd9Sstevel@tonic-gate 				usb_dev_descr_t *dev_descrp;
72757c478bd9Sstevel@tonic-gate 
72767c478bd9Sstevel@tonic-gate 				/* check child_dip */
72777c478bd9Sstevel@tonic-gate 				if ((child_dip = hubd_get_child_dip(hubd,
72787c478bd9Sstevel@tonic-gate 				    ioc.port)) == NULL) {
72797c478bd9Sstevel@tonic-gate 					rv = EINVAL;
72807c478bd9Sstevel@tonic-gate 
72817c478bd9Sstevel@tonic-gate 					break;
72827c478bd9Sstevel@tonic-gate 				}
72837c478bd9Sstevel@tonic-gate 
72847c478bd9Sstevel@tonic-gate 				dev_descrp = usb_get_dev_descr(child_dip);
72857c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != sizeof (*dev_descrp)) {
72867c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
72877c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
72887c478bd9Sstevel@tonic-gate 					    "%s: bufsize passed (%d) != sizeof "
72897c478bd9Sstevel@tonic-gate 					    "usba_device_descr_t (%d)", msg,
72907c478bd9Sstevel@tonic-gate 					    ioc.bufsiz, dev_descrp->bLength);
72917c478bd9Sstevel@tonic-gate 					rv = EINVAL;
72927c478bd9Sstevel@tonic-gate 
72937c478bd9Sstevel@tonic-gate 					break;
72947c478bd9Sstevel@tonic-gate 				}
72957c478bd9Sstevel@tonic-gate 
72967c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)dev_descrp,
72977c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
72987c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
72997c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
73007c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
73017c478bd9Sstevel@tonic-gate 					rv = EIO;
73027c478bd9Sstevel@tonic-gate 
73037c478bd9Sstevel@tonic-gate 					break;
73047c478bd9Sstevel@tonic-gate 				}
73057c478bd9Sstevel@tonic-gate 			}
73067c478bd9Sstevel@tonic-gate 			break;
73077c478bd9Sstevel@tonic-gate 		case USB_DESCR_TYPE_STRING:
73087c478bd9Sstevel@tonic-gate 		{
73097c478bd9Sstevel@tonic-gate 			char		*str;
73107c478bd9Sstevel@tonic-gate 			uint32_t	size;
73117c478bd9Sstevel@tonic-gate 			usba_device_t	*usba_device;
73127c478bd9Sstevel@tonic-gate 
73137c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_STRING_DESCR";
73147c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
73157c478bd9Sstevel@tonic-gate 			    "%s: string request: %d", msg, ioc.misc_arg);
73167c478bd9Sstevel@tonic-gate 
73177c478bd9Sstevel@tonic-gate 			/* recheck */
73187c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
73197c478bd9Sstevel@tonic-gate 			    NULL) {
73207c478bd9Sstevel@tonic-gate 				rv = EINVAL;
73217c478bd9Sstevel@tonic-gate 
73227c478bd9Sstevel@tonic-gate 				break;
73237c478bd9Sstevel@tonic-gate 			}
73247c478bd9Sstevel@tonic-gate 			usba_device = usba_get_usba_device(child_dip);
73257c478bd9Sstevel@tonic-gate 
73267c478bd9Sstevel@tonic-gate 			switch (ioc.misc_arg) {
73277c478bd9Sstevel@tonic-gate 			case HUBD_MFG_STR:
73287c478bd9Sstevel@tonic-gate 				str = usba_device->usb_mfg_str;
73297c478bd9Sstevel@tonic-gate 
73307c478bd9Sstevel@tonic-gate 				break;
73317c478bd9Sstevel@tonic-gate 			case HUBD_PRODUCT_STR:
73327c478bd9Sstevel@tonic-gate 				str = usba_device->usb_product_str;
73337c478bd9Sstevel@tonic-gate 
73347c478bd9Sstevel@tonic-gate 				break;
73357c478bd9Sstevel@tonic-gate 			case HUBD_SERIALNO_STR:
73367c478bd9Sstevel@tonic-gate 				str = usba_device->usb_serialno_str;
73377c478bd9Sstevel@tonic-gate 
73387c478bd9Sstevel@tonic-gate 				break;
73397c478bd9Sstevel@tonic-gate 			case HUBD_CFG_DESCR_STR:
73407c478bd9Sstevel@tonic-gate 				mutex_enter(&usba_device->usb_mutex);
73417c478bd9Sstevel@tonic-gate 				str = usba_device->usb_cfg_str_descr[
73427c478bd9Sstevel@tonic-gate 					usba_device->usb_active_cfg_ndx];
73437c478bd9Sstevel@tonic-gate 				mutex_exit(&usba_device->usb_mutex);
73447c478bd9Sstevel@tonic-gate 
73457c478bd9Sstevel@tonic-gate 				break;
73467c478bd9Sstevel@tonic-gate 			default:
73477c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
73487c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
73497c478bd9Sstevel@tonic-gate 				    "%s: Invalid string request", msg);
73507c478bd9Sstevel@tonic-gate 				rv = EINVAL;
73517c478bd9Sstevel@tonic-gate 
73527c478bd9Sstevel@tonic-gate 				break;
73537c478bd9Sstevel@tonic-gate 			} /* end of switch */
73547c478bd9Sstevel@tonic-gate 
73557c478bd9Sstevel@tonic-gate 			if (rv != 0) {
73567c478bd9Sstevel@tonic-gate 
73577c478bd9Sstevel@tonic-gate 				break;
73587c478bd9Sstevel@tonic-gate 			}
73597c478bd9Sstevel@tonic-gate 
73607c478bd9Sstevel@tonic-gate 			size = (str != NULL) ? strlen(str) + 1 : 0;
73617c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
73627c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size, ioc.buf,
73637c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
73647c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
73657c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
73667c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
73677c478bd9Sstevel@tonic-gate 					rv = EIO;
73687c478bd9Sstevel@tonic-gate 
73697c478bd9Sstevel@tonic-gate 					break;
73707c478bd9Sstevel@tonic-gate 				}
73717c478bd9Sstevel@tonic-gate 			} else {
73727c478bd9Sstevel@tonic-gate 				if (size == 0) {
73737c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L3(DPRINT_MASK_CBOPS,
73747c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
73757c478bd9Sstevel@tonic-gate 					    "%s: String is NULL", msg);
73767c478bd9Sstevel@tonic-gate 					rv = EINVAL;
73777c478bd9Sstevel@tonic-gate 
73787c478bd9Sstevel@tonic-gate 					break;
73797c478bd9Sstevel@tonic-gate 				}
73807c478bd9Sstevel@tonic-gate 
73817c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
73827c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
73837c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
73847c478bd9Sstevel@tonic-gate 					    "%s: string buf size wrong", msg);
73857c478bd9Sstevel@tonic-gate 					rv = EINVAL;
73867c478bd9Sstevel@tonic-gate 
73877c478bd9Sstevel@tonic-gate 					break;
73887c478bd9Sstevel@tonic-gate 				}
73897c478bd9Sstevel@tonic-gate 
73907c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)str, ioc.buf,
73917c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
73927c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
73937c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
73947c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
73957c478bd9Sstevel@tonic-gate 					rv = EIO;
73967c478bd9Sstevel@tonic-gate 
73977c478bd9Sstevel@tonic-gate 					break;
73987c478bd9Sstevel@tonic-gate 				}
73997c478bd9Sstevel@tonic-gate 			}
74007c478bd9Sstevel@tonic-gate 			break;
74017c478bd9Sstevel@tonic-gate 		}
74027c478bd9Sstevel@tonic-gate 		case HUBD_GET_CFGADM_NAME:
74037c478bd9Sstevel@tonic-gate 		{
74047c478bd9Sstevel@tonic-gate 			uint32_t   name_len;
74057c478bd9Sstevel@tonic-gate 			const char *name;
74067c478bd9Sstevel@tonic-gate 
74077c478bd9Sstevel@tonic-gate 			/* recheck */
74087c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
74097c478bd9Sstevel@tonic-gate 			    NULL) {
74107c478bd9Sstevel@tonic-gate 				rv = EINVAL;
74117c478bd9Sstevel@tonic-gate 
74127c478bd9Sstevel@tonic-gate 				break;
74137c478bd9Sstevel@tonic-gate 			}
74147c478bd9Sstevel@tonic-gate 			name = ddi_node_name(child_dip);
74157c478bd9Sstevel@tonic-gate 			if (name == NULL) {
74167c478bd9Sstevel@tonic-gate 				name = "unsupported";
74177c478bd9Sstevel@tonic-gate 			}
74187c478bd9Sstevel@tonic-gate 			name_len = strlen(name) + 1;
74197c478bd9Sstevel@tonic-gate 
74207c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: HUBD_GET_CFGADM_NAME";
74217c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
74227c478bd9Sstevel@tonic-gate 			    "%s: name=%s name_len=%d", msg, name, name_len);
74237c478bd9Sstevel@tonic-gate 
74247c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
74257c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&name_len,
74267c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
74277c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
74287c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
74297c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed", msg);
74307c478bd9Sstevel@tonic-gate 					rv = EIO;
74317c478bd9Sstevel@tonic-gate 
74327c478bd9Sstevel@tonic-gate 					break;
74337c478bd9Sstevel@tonic-gate 				}
74347c478bd9Sstevel@tonic-gate 			} else {
74357c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != name_len) {
74367c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
74377c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
74387c478bd9Sstevel@tonic-gate 					    "%s: string buf length wrong", msg);
74397c478bd9Sstevel@tonic-gate 					rv = EINVAL;
74407c478bd9Sstevel@tonic-gate 
74417c478bd9Sstevel@tonic-gate 					break;
74427c478bd9Sstevel@tonic-gate 				}
74437c478bd9Sstevel@tonic-gate 
74447c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)name, ioc.buf,
74457c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
74467c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
74477c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
74487c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
74497c478bd9Sstevel@tonic-gate 					rv = EIO;
74507c478bd9Sstevel@tonic-gate 
74517c478bd9Sstevel@tonic-gate 					break;
74527c478bd9Sstevel@tonic-gate 				}
74537c478bd9Sstevel@tonic-gate 			}
74547c478bd9Sstevel@tonic-gate 
74557c478bd9Sstevel@tonic-gate 			break;
74567c478bd9Sstevel@tonic-gate 		}
74577c478bd9Sstevel@tonic-gate 
74587c478bd9Sstevel@tonic-gate 		/*
74597c478bd9Sstevel@tonic-gate 		 * Return the config index for the currently-configured
74607c478bd9Sstevel@tonic-gate 		 * configuration.
74617c478bd9Sstevel@tonic-gate 		 */
74627c478bd9Sstevel@tonic-gate 		case HUBD_GET_CURRENT_CONFIG:
74637c478bd9Sstevel@tonic-gate 		{
74647c478bd9Sstevel@tonic-gate 			uint_t		config_index;
74657c478bd9Sstevel@tonic-gate 			uint32_t	size = sizeof (config_index);
74667c478bd9Sstevel@tonic-gate 			usba_device_t	*usba_device;
74677c478bd9Sstevel@tonic-gate 
74687c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_CURRENT_CONFIG";
74697c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
74707c478bd9Sstevel@tonic-gate 			    "%s", msg);
74717c478bd9Sstevel@tonic-gate 
74727c478bd9Sstevel@tonic-gate 			/*
74737c478bd9Sstevel@tonic-gate 			 * Return the config index for the configuration
74747c478bd9Sstevel@tonic-gate 			 * currently in use.
74757c478bd9Sstevel@tonic-gate 			 * Recheck if child_dip exists
74767c478bd9Sstevel@tonic-gate 			 */
74777c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
74787c478bd9Sstevel@tonic-gate 			    NULL) {
74797c478bd9Sstevel@tonic-gate 				rv = EINVAL;
74807c478bd9Sstevel@tonic-gate 
74817c478bd9Sstevel@tonic-gate 				break;
74827c478bd9Sstevel@tonic-gate 			}
74837c478bd9Sstevel@tonic-gate 
74847c478bd9Sstevel@tonic-gate 			usba_device = usba_get_usba_device(child_dip);
74857c478bd9Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
74867c478bd9Sstevel@tonic-gate 			config_index = usba_device->usb_active_cfg_ndx;
74877c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
74887c478bd9Sstevel@tonic-gate 
74897c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
74907c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size,
74917c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
74927c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
74937c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
74947c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
74957c478bd9Sstevel@tonic-gate 					rv = EIO;
74967c478bd9Sstevel@tonic-gate 
74977c478bd9Sstevel@tonic-gate 					break;
74987c478bd9Sstevel@tonic-gate 				}
74997c478bd9Sstevel@tonic-gate 			} else {
75007c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
75017c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
75027c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
75037c478bd9Sstevel@tonic-gate 					    "%s: buffer size wrong", msg);
75047c478bd9Sstevel@tonic-gate 					rv = EINVAL;
75057c478bd9Sstevel@tonic-gate 
75067c478bd9Sstevel@tonic-gate 					break;
75077c478bd9Sstevel@tonic-gate 				}
75087c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&config_index,
75097c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
75107c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
75117c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
75127c478bd9Sstevel@tonic-gate 					    "%s: copyout failed", msg);
75137c478bd9Sstevel@tonic-gate 					rv = EIO;
75147c478bd9Sstevel@tonic-gate 				}
75157c478bd9Sstevel@tonic-gate 			}
75167c478bd9Sstevel@tonic-gate 
75177c478bd9Sstevel@tonic-gate 			break;
75187c478bd9Sstevel@tonic-gate 		}
75197c478bd9Sstevel@tonic-gate 		case HUBD_GET_DEVICE_PATH:
75207c478bd9Sstevel@tonic-gate 		{
75217c478bd9Sstevel@tonic-gate 			char		*path;
75227c478bd9Sstevel@tonic-gate 			uint32_t	size;
75237c478bd9Sstevel@tonic-gate 
75247c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_DEVICE_PATH";
75257c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
75267c478bd9Sstevel@tonic-gate 			    "%s", msg);
75277c478bd9Sstevel@tonic-gate 
75287c478bd9Sstevel@tonic-gate 			/* Recheck if child_dip exists */
75297c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
75307c478bd9Sstevel@tonic-gate 			    NULL) {
75317c478bd9Sstevel@tonic-gate 				rv = EINVAL;
75327c478bd9Sstevel@tonic-gate 
75337c478bd9Sstevel@tonic-gate 				break;
75347c478bd9Sstevel@tonic-gate 			}
75357c478bd9Sstevel@tonic-gate 
75367c478bd9Sstevel@tonic-gate 			/* ddi_pathname doesn't supply /devices, so we do. */
75377c478bd9Sstevel@tonic-gate 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
75387c478bd9Sstevel@tonic-gate 			(void) strcpy(path, "/devices");
75397c478bd9Sstevel@tonic-gate 			(void) ddi_pathname(child_dip, path + strlen(path));
75407c478bd9Sstevel@tonic-gate 			size = strlen(path) + 1;
75417c478bd9Sstevel@tonic-gate 
75427c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
75437c478bd9Sstevel@tonic-gate 			    "%s: device path=%s  size=%d", msg, path, size);
75447c478bd9Sstevel@tonic-gate 
75457c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
75467c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size,
75477c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
75487c478bd9Sstevel@tonic-gate 
75497c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
75507c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
75517c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
75527c478bd9Sstevel@tonic-gate 					rv = EIO;
75537c478bd9Sstevel@tonic-gate 				}
75547c478bd9Sstevel@tonic-gate 			} else {
75557c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
75567c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
75577c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
75587c478bd9Sstevel@tonic-gate 					    "%s: buffer wrong size.", msg);
75597c478bd9Sstevel@tonic-gate 					rv = EINVAL;
75607c478bd9Sstevel@tonic-gate 				} else if (ddi_copyout((void *)path,
75617c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
75627c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
75637c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
75647c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
75657c478bd9Sstevel@tonic-gate 					rv = EIO;
75667c478bd9Sstevel@tonic-gate 				}
75677c478bd9Sstevel@tonic-gate 			}
75687c478bd9Sstevel@tonic-gate 			kmem_free(path, MAXPATHLEN);
75697c478bd9Sstevel@tonic-gate 
75707c478bd9Sstevel@tonic-gate 			break;
75717c478bd9Sstevel@tonic-gate 		}
75727c478bd9Sstevel@tonic-gate 		case HUBD_REFRESH_DEVDB:
75737c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: HUBD_REFRESH_DEVDB";
75747c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
75757c478bd9Sstevel@tonic-gate 			    "%s", msg);
75767c478bd9Sstevel@tonic-gate 
75777c478bd9Sstevel@tonic-gate 			if ((rv = usba_devdb_refresh()) != USB_SUCCESS) {
75787c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
75797c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
75807c478bd9Sstevel@tonic-gate 				    "%s: Failed: %d", msg, rv);
75817c478bd9Sstevel@tonic-gate 				rv = EIO;
75827c478bd9Sstevel@tonic-gate 			}
75837c478bd9Sstevel@tonic-gate 
75847c478bd9Sstevel@tonic-gate 			break;
75857c478bd9Sstevel@tonic-gate 		default:
75867c478bd9Sstevel@tonic-gate 			rv = ENOTSUP;
75877c478bd9Sstevel@tonic-gate 		}	/* end switch */
75887c478bd9Sstevel@tonic-gate 
75897c478bd9Sstevel@tonic-gate 		break;
75907c478bd9Sstevel@tonic-gate 	}
75917c478bd9Sstevel@tonic-gate 
75927c478bd9Sstevel@tonic-gate 	default:
75937c478bd9Sstevel@tonic-gate 		rv = ENOTTY;
75947c478bd9Sstevel@tonic-gate 	}
75957c478bd9Sstevel@tonic-gate 
75967c478bd9Sstevel@tonic-gate 	if (dcp) {
75977c478bd9Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
75987c478bd9Sstevel@tonic-gate 	}
75997c478bd9Sstevel@tonic-gate 
76007c478bd9Sstevel@tonic-gate 	/* allow hotplug thread now */
76017c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
76027c478bd9Sstevel@tonic-gate 
76037c478bd9Sstevel@tonic-gate 	if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
76047c478bd9Sstevel@tonic-gate 	    hubd->h_ep1_ph && (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
76057c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
76067c478bd9Sstevel@tonic-gate 	}
76077c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
76087c478bd9Sstevel@tonic-gate 
76097c478bd9Sstevel@tonic-gate 	ndi_devi_exit(hubd->h_dip, circ);
76107c478bd9Sstevel@tonic-gate 	ndi_devi_exit(rh_dip, rh_circ);
76117c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
76127c478bd9Sstevel@tonic-gate 
76137c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
76147c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, hubd->h_dip, 0);
76157c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
76167c478bd9Sstevel@tonic-gate 
76177c478bd9Sstevel@tonic-gate 	return (rv);
76187c478bd9Sstevel@tonic-gate }
76197c478bd9Sstevel@tonic-gate 
76207c478bd9Sstevel@tonic-gate 
76217c478bd9Sstevel@tonic-gate /*
76227c478bd9Sstevel@tonic-gate  * Helper func used only to help construct the names for the attachment point
76237c478bd9Sstevel@tonic-gate  * minor nodes.  Used only in usba_hubdi_attach.
76247c478bd9Sstevel@tonic-gate  * Returns whether it found ancestry or not (USB_SUCCESS if yes).
76257c478bd9Sstevel@tonic-gate  * ports between the root hub and the device represented by dip.
76267c478bd9Sstevel@tonic-gate  * E.g.,  "2.4.3.1" means this device is
76277c478bd9Sstevel@tonic-gate  *	plugged into port 1 of a hub that is
76287c478bd9Sstevel@tonic-gate  *	plugged into port 3 of a hub that is
76297c478bd9Sstevel@tonic-gate  *	plugged into port 4 of a hub that is
76307c478bd9Sstevel@tonic-gate  *	plugged into port 2 of the root hub.
76317c478bd9Sstevel@tonic-gate  * NOTE: Max ap_id path len is HUBD_APID_NAMELEN (32 chars), which is
76327c478bd9Sstevel@tonic-gate  * more than sufficient (as hubs are a max 6 levels deep, port needs 3
76337c478bd9Sstevel@tonic-gate  * chars plus NULL each)
76347c478bd9Sstevel@tonic-gate  */
76357c478bd9Sstevel@tonic-gate static void
76367c478bd9Sstevel@tonic-gate hubd_get_ancestry_str(hubd_t *hubd)
76377c478bd9Sstevel@tonic-gate {
76387c478bd9Sstevel@tonic-gate 	char	dev_path[MAXPATHLEN];
76397c478bd9Sstevel@tonic-gate 	char	*port_num_pos;
76407c478bd9Sstevel@tonic-gate 	char	port_list[HUBD_APID_NAMELEN];
76417c478bd9Sstevel@tonic-gate 	char	*port_list_end = port_list;
76427c478bd9Sstevel@tonic-gate 
76437c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
76447c478bd9Sstevel@tonic-gate 	    "hubd_get_ancestry_str: hubd=0x%p", hubd);
76457c478bd9Sstevel@tonic-gate 
76467c478bd9Sstevel@tonic-gate 	dev_path[0] = '\0';
76477c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(hubd->h_dip, dev_path);
76487c478bd9Sstevel@tonic-gate 	port_num_pos = dev_path;
76497c478bd9Sstevel@tonic-gate 
76507c478bd9Sstevel@tonic-gate 	port_list[0] = NULL;
76517c478bd9Sstevel@tonic-gate 	while ((port_num_pos = (char *)strstr(port_num_pos, "hub@")) != NULL) {
76527c478bd9Sstevel@tonic-gate 		/*
76537c478bd9Sstevel@tonic-gate 		 * Found a non-root hub between the root hub port and device.
76547c478bd9Sstevel@tonic-gate 		 * Get the number of the port this hub is plugged into,
76557c478bd9Sstevel@tonic-gate 		 * and append it to the ancestry string.
76567c478bd9Sstevel@tonic-gate 		 */
76577c478bd9Sstevel@tonic-gate 		if (port_list_end != port_list) { /* have list already */
76587c478bd9Sstevel@tonic-gate 			(void) strcat(port_list_end, ".");
76597c478bd9Sstevel@tonic-gate 			port_list_end++;
76607c478bd9Sstevel@tonic-gate 		}
76617c478bd9Sstevel@tonic-gate 
76627c478bd9Sstevel@tonic-gate 		while (!isdigit(*port_num_pos)) {
76637c478bd9Sstevel@tonic-gate 			if (*port_num_pos++ == '\0') {
76647c478bd9Sstevel@tonic-gate 
76657c478bd9Sstevel@tonic-gate 				break;
76667c478bd9Sstevel@tonic-gate 			}
76677c478bd9Sstevel@tonic-gate 		}
76687c478bd9Sstevel@tonic-gate 
76697c478bd9Sstevel@tonic-gate 		while (isdigit(*port_num_pos)) {
76707c478bd9Sstevel@tonic-gate 			*port_list_end++ = *port_num_pos++;
76717c478bd9Sstevel@tonic-gate 			ASSERT(port_list_end <
76727c478bd9Sstevel@tonic-gate 			    (port_list + sizeof (port_list)));
76737c478bd9Sstevel@tonic-gate 			ASSERT(port_num_pos < (dev_path + sizeof (dev_path)));
76747c478bd9Sstevel@tonic-gate 		}
76757c478bd9Sstevel@tonic-gate 		*port_list_end = '\0';
76767c478bd9Sstevel@tonic-gate 	}
76777c478bd9Sstevel@tonic-gate 
76787c478bd9Sstevel@tonic-gate 	if (port_list_end != port_list) {
76797c478bd9Sstevel@tonic-gate 		(void) strcpy(hubd->h_ancestry_str, port_list);
76807c478bd9Sstevel@tonic-gate 		(void) strcat(hubd->h_ancestry_str, ".");
76817c478bd9Sstevel@tonic-gate 	}
76827c478bd9Sstevel@tonic-gate }
76837c478bd9Sstevel@tonic-gate 
76847c478bd9Sstevel@tonic-gate 
76857c478bd9Sstevel@tonic-gate /* Get which port to operate on.  */
76867c478bd9Sstevel@tonic-gate static usb_port_t
76877c478bd9Sstevel@tonic-gate hubd_get_port_num(hubd_t *hubd, struct devctl_iocdata *dcp)
76887c478bd9Sstevel@tonic-gate {
76897c478bd9Sstevel@tonic-gate 	int32_t port;
76907c478bd9Sstevel@tonic-gate 
76917c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
76927c478bd9Sstevel@tonic-gate 
76937c478bd9Sstevel@tonic-gate 	/* Get which port to operate on.  */
76947c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
76957c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
76967c478bd9Sstevel@tonic-gate 		    "hubd_get_port_num: port lookup failed");
76977c478bd9Sstevel@tonic-gate 		port = 0;
76987c478bd9Sstevel@tonic-gate 	}
76997c478bd9Sstevel@tonic-gate 
77007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
77017c478bd9Sstevel@tonic-gate 	    "hubd_get_port_num: hubd=0x%p, port=%d", hubd, port);
77027c478bd9Sstevel@tonic-gate 
77037c478bd9Sstevel@tonic-gate 	return ((usb_port_t)port);
77047c478bd9Sstevel@tonic-gate }
77057c478bd9Sstevel@tonic-gate 
77067c478bd9Sstevel@tonic-gate 
77077c478bd9Sstevel@tonic-gate /* check if child still exists */
77087c478bd9Sstevel@tonic-gate static dev_info_t *
77097c478bd9Sstevel@tonic-gate hubd_get_child_dip(hubd_t *hubd, usb_port_t port)
77107c478bd9Sstevel@tonic-gate {
77117c478bd9Sstevel@tonic-gate 	dev_info_t *child_dip = hubd->h_children_dips[port];
77127c478bd9Sstevel@tonic-gate 
77137c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
77147c478bd9Sstevel@tonic-gate 	    "hubd_get_child_dip: hubd=0x%p, port=%d", hubd, port);
77157c478bd9Sstevel@tonic-gate 
77167c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
77177c478bd9Sstevel@tonic-gate 
77187c478bd9Sstevel@tonic-gate 	return (child_dip);
77197c478bd9Sstevel@tonic-gate }
77207c478bd9Sstevel@tonic-gate 
77217c478bd9Sstevel@tonic-gate 
77227c478bd9Sstevel@tonic-gate /*
77237c478bd9Sstevel@tonic-gate  * hubd_cfgadm_state:
77247c478bd9Sstevel@tonic-gate  *
77257c478bd9Sstevel@tonic-gate  *	child_dip list		port_state		cfgadm_state
77267c478bd9Sstevel@tonic-gate  *	--------------		----------		------------
77277c478bd9Sstevel@tonic-gate  *	!= NULL			connected		configured or
77287c478bd9Sstevel@tonic-gate  *							unconfigured
77297c478bd9Sstevel@tonic-gate  *	!= NULL			not connected		disconnect but
77307c478bd9Sstevel@tonic-gate  *							busy/still referenced
77317c478bd9Sstevel@tonic-gate  *	NULL			connected		logically disconnected
77327c478bd9Sstevel@tonic-gate  *	NULL			not connected		empty
77337c478bd9Sstevel@tonic-gate  */
77347c478bd9Sstevel@tonic-gate static uint_t
77357c478bd9Sstevel@tonic-gate hubd_cfgadm_state(hubd_t *hubd, usb_port_t port)
77367c478bd9Sstevel@tonic-gate {
77377c478bd9Sstevel@tonic-gate 	uint_t		state;
77387c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip = hubd_get_child_dip(hubd, port);
77397c478bd9Sstevel@tonic-gate 
77407c478bd9Sstevel@tonic-gate 	if (child_dip) {
77417c478bd9Sstevel@tonic-gate 		if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
77427c478bd9Sstevel@tonic-gate 			/*
77437c478bd9Sstevel@tonic-gate 			 * connected,  now check if driver exists
77447c478bd9Sstevel@tonic-gate 			 */
77457c478bd9Sstevel@tonic-gate 			if (DEVI_IS_DEVICE_OFFLINE(child_dip) ||
7746737d277aScth 			    !i_ddi_devi_attached(child_dip)) {
77477c478bd9Sstevel@tonic-gate 				state = HUBD_CFGADM_UNCONFIGURED;
77487c478bd9Sstevel@tonic-gate 			} else {
77497c478bd9Sstevel@tonic-gate 				state = HUBD_CFGADM_CONFIGURED;
77507c478bd9Sstevel@tonic-gate 			}
77517c478bd9Sstevel@tonic-gate 		} else {
77527c478bd9Sstevel@tonic-gate 			/*
77537c478bd9Sstevel@tonic-gate 			 * this means that the dip is around for
77547c478bd9Sstevel@tonic-gate 			 * a device that is still referenced but
77557c478bd9Sstevel@tonic-gate 			 * has been yanked out. So the cfgadm info
77567c478bd9Sstevel@tonic-gate 			 * for this state should be EMPTY (port empty)
77577c478bd9Sstevel@tonic-gate 			 * and CONFIGURED (dip still valid).
77587c478bd9Sstevel@tonic-gate 			 */
77597c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_STILL_REFERENCED;
77607c478bd9Sstevel@tonic-gate 		}
77617c478bd9Sstevel@tonic-gate 	} else {
77627c478bd9Sstevel@tonic-gate 		/* connected but no child dip */
77637c478bd9Sstevel@tonic-gate 		if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
77647c478bd9Sstevel@tonic-gate 			/* logically disconnected */
77657c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_DISCONNECTED;
77667c478bd9Sstevel@tonic-gate 		} else {
77677c478bd9Sstevel@tonic-gate 			/* physically disconnected */
77687c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_EMPTY;
77697c478bd9Sstevel@tonic-gate 		}
77707c478bd9Sstevel@tonic-gate 	}
77717c478bd9Sstevel@tonic-gate 
77727c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
77737c478bd9Sstevel@tonic-gate 	    "hubd_cfgadm_state: hubd=0x%p, port=%d state=0x%x",
77747c478bd9Sstevel@tonic-gate 	    hubd, port, state);
77757c478bd9Sstevel@tonic-gate 
77767c478bd9Sstevel@tonic-gate 	return (state);
77777c478bd9Sstevel@tonic-gate }
77787c478bd9Sstevel@tonic-gate 
77797c478bd9Sstevel@tonic-gate 
77807c478bd9Sstevel@tonic-gate /*
77817c478bd9Sstevel@tonic-gate  * hubd_toggle_port:
77827c478bd9Sstevel@tonic-gate  */
77837c478bd9Sstevel@tonic-gate static int
77847c478bd9Sstevel@tonic-gate hubd_toggle_port(hubd_t *hubd, usb_port_t port)
77857c478bd9Sstevel@tonic-gate {
77867c478bd9Sstevel@tonic-gate 	usb_hub_descr_t	*hub_descr;
77877c478bd9Sstevel@tonic-gate 	int		wait;
77887c478bd9Sstevel@tonic-gate 	uint_t		retry;
77897c478bd9Sstevel@tonic-gate 	uint16_t	status;
77907c478bd9Sstevel@tonic-gate 	uint16_t	change;
77917c478bd9Sstevel@tonic-gate 
77927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
77937c478bd9Sstevel@tonic-gate 	    "hubd_toggle_port: hubd=0x%p, port=%d", hubd, port);
77947c478bd9Sstevel@tonic-gate 
77957c478bd9Sstevel@tonic-gate 	if ((hubd_disable_port_power(hubd, port)) != USB_SUCCESS) {
77967c478bd9Sstevel@tonic-gate 
77977c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
77987c478bd9Sstevel@tonic-gate 	}
77997c478bd9Sstevel@tonic-gate 
78007c478bd9Sstevel@tonic-gate 	/*
78017c478bd9Sstevel@tonic-gate 	 * see hubd_enable_all_port_power() which
78027c478bd9Sstevel@tonic-gate 	 * requires longer delay for hubs.
78037c478bd9Sstevel@tonic-gate 	 */
78047c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
78057c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(hubd_device_delay / 10));
78067c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
78077c478bd9Sstevel@tonic-gate 
78087c478bd9Sstevel@tonic-gate 	hub_descr = &hubd->h_hub_descr;
78097c478bd9Sstevel@tonic-gate 
78107c478bd9Sstevel@tonic-gate 	/*
78117c478bd9Sstevel@tonic-gate 	 * According to section 11.11 of USB, for hubs with no power
78127c478bd9Sstevel@tonic-gate 	 * switches, bPwrOn2PwrGood is zero. But we wait for some
78137c478bd9Sstevel@tonic-gate 	 * arbitrary time to enable power to become stable.
78147c478bd9Sstevel@tonic-gate 	 *
78157c478bd9Sstevel@tonic-gate 	 * If an hub supports port power swicthing, we need to wait
78167c478bd9Sstevel@tonic-gate 	 * at least 20ms before accesing corresonding usb port.
78177c478bd9Sstevel@tonic-gate 	 */
78187c478bd9Sstevel@tonic-gate 	if ((hub_descr->wHubCharacteristics &
78197c478bd9Sstevel@tonic-gate 	    HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) {
78207c478bd9Sstevel@tonic-gate 		wait = hubd_device_delay / 10;
78217c478bd9Sstevel@tonic-gate 	} else {
78227c478bd9Sstevel@tonic-gate 		wait = max(HUB_DEFAULT_POPG,
78237c478bd9Sstevel@tonic-gate 		    hub_descr->bPwrOn2PwrGood) * 2 * 1000;
78247c478bd9Sstevel@tonic-gate 	}
78257c478bd9Sstevel@tonic-gate 
78267c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
78277c478bd9Sstevel@tonic-gate 	    "hubd_toggle_port: popg=%d wait=%d",
78287c478bd9Sstevel@tonic-gate 	    hub_descr->bPwrOn2PwrGood, wait);
78297c478bd9Sstevel@tonic-gate 
78307c478bd9Sstevel@tonic-gate 	retry = 0;
78317c478bd9Sstevel@tonic-gate 
78327c478bd9Sstevel@tonic-gate 	do {
78337c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port_power(hubd, port);
78347c478bd9Sstevel@tonic-gate 
78357c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
78367c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(wait));
78377c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
78387c478bd9Sstevel@tonic-gate 
78397c478bd9Sstevel@tonic-gate 		/* Get port status */
78407c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd, port,
78417c478bd9Sstevel@tonic-gate 		    &status, &change, 0);
78427c478bd9Sstevel@tonic-gate 
78437c478bd9Sstevel@tonic-gate 		/* For retry if any, use some extra delay */
78447c478bd9Sstevel@tonic-gate 		wait = max(wait, hubd_device_delay / 10);
78457c478bd9Sstevel@tonic-gate 
78467c478bd9Sstevel@tonic-gate 		retry++;
78477c478bd9Sstevel@tonic-gate 
78487c478bd9Sstevel@tonic-gate 	} while ((!(status & PORT_STATUS_PPS)) && (retry < HUBD_PORT_RETRY));
78497c478bd9Sstevel@tonic-gate 
78507c478bd9Sstevel@tonic-gate 	/* Print warning message if port has no power */
78517c478bd9Sstevel@tonic-gate 	if (!(status & PORT_STATUS_PPS)) {
78527c478bd9Sstevel@tonic-gate 
7853d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
78547c478bd9Sstevel@tonic-gate 		    "hubd_toggle_port: port %d power-on failed, "
78557c478bd9Sstevel@tonic-gate 		    "port status 0x%x", port, status);
78567c478bd9Sstevel@tonic-gate 
78577c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
78587c478bd9Sstevel@tonic-gate 	}
78597c478bd9Sstevel@tonic-gate 
78607c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
78617c478bd9Sstevel@tonic-gate }
786235f36846Ssl 
786335f36846Ssl 
786435f36846Ssl /*
786535f36846Ssl  * hubd_init_power_budget:
786635f36846Ssl  *	Init power budget variables in hubd structure. According
786735f36846Ssl  *	to USB spec, the power budget rules are:
786835f36846Ssl  *	1. local-powered hubs including root-hubs can supply
786935f36846Ssl  *	   500mA to each port at maximum
787035f36846Ssl  *	2. two bus-powered hubs are not allowed to concatenate
787135f36846Ssl  *	3. bus-powered hubs can supply 100mA to each port at
787235f36846Ssl  *	   maximum, and the power consumed by all downstream
787335f36846Ssl  *	   ports and the hub itself cannot exceed the max power
787435f36846Ssl  *	   supplied by the upstream port, i.e., 500mA
787535f36846Ssl  *	The routine is only called during hub attach time
787635f36846Ssl  */
787735f36846Ssl static int
787835f36846Ssl hubd_init_power_budget(hubd_t *hubd)
787935f36846Ssl {
788035f36846Ssl 	uint16_t	status = 0;
788135f36846Ssl 	usba_device_t	*hubd_ud = NULL;
788235f36846Ssl 	size_t		size;
788335f36846Ssl 	usb_cfg_descr_t	cfg_descr;
788435f36846Ssl 	dev_info_t	*pdip = NULL;
788535f36846Ssl 	hubd_t		*phubd = NULL;
788635f36846Ssl 
788735f36846Ssl 	if (hubd->h_ignore_pwr_budget) {
788835f36846Ssl 
788935f36846Ssl 		return (USB_SUCCESS);
789035f36846Ssl 	}
789135f36846Ssl 
789235f36846Ssl 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
789335f36846Ssl 	    "hubd_init_power_budget:");
789435f36846Ssl 
789535f36846Ssl 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
789635f36846Ssl 	ASSERT(hubd->h_default_pipe != 0);
789735f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
789835f36846Ssl 
789935f36846Ssl 	/* get device status */
790035f36846Ssl 	if ((usb_get_status(hubd->h_dip, hubd->h_default_pipe,
790135f36846Ssl 	    HUB_GET_DEVICE_STATUS_TYPE,
790235f36846Ssl 	    0, &status, 0)) != USB_SUCCESS) {
790335f36846Ssl 		mutex_enter(HUBD_MUTEX(hubd));
790435f36846Ssl 
790535f36846Ssl 		return (USB_FAILURE);
790635f36846Ssl 	}
790735f36846Ssl 
790835f36846Ssl 	hubd_ud = usba_get_usba_device(hubd->h_dip);
790935f36846Ssl 
791035f36846Ssl 	size = usb_parse_cfg_descr(hubd_ud->usb_cfg, hubd_ud->usb_cfg_length,
791135f36846Ssl 	    &cfg_descr, USB_CFG_DESCR_SIZE);
791235f36846Ssl 
791335f36846Ssl 	if (size != USB_CFG_DESCR_SIZE) {
791435f36846Ssl 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
791535f36846Ssl 		    "get hub configuration descriptor failed");
791635f36846Ssl 		mutex_enter(HUBD_MUTEX(hubd));
791735f36846Ssl 
791835f36846Ssl 		return (USB_FAILURE);
791935f36846Ssl 	}
792035f36846Ssl 
792135f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
792235f36846Ssl 
792335f36846Ssl 	hubd->h_local_pwr_capable = (cfg_descr.bmAttributes &
792435f36846Ssl 	    USB_CFG_ATTR_SELFPWR);
792535f36846Ssl 
792635f36846Ssl 	if (hubd->h_local_pwr_capable) {
792735f36846Ssl 		USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
792835f36846Ssl 		    "hub is capable of local power");
792935f36846Ssl 	}
793035f36846Ssl 
793135f36846Ssl 	hubd->h_local_pwr_on = (status &
793235f36846Ssl 	    USB_DEV_SLF_PWRD_STATUS) && hubd->h_local_pwr_capable;
793335f36846Ssl 
793435f36846Ssl 	if (hubd->h_local_pwr_on) {
793535f36846Ssl 		USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
793635f36846Ssl 		    "hub is local-powered");
793735f36846Ssl 
793835f36846Ssl 		hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
793935f36846Ssl 		    USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
794035f36846Ssl 	} else {
794135f36846Ssl 		hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
794235f36846Ssl 		    USB_LOW_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
794335f36846Ssl 
794435f36846Ssl 		hubd->h_pwr_left = (USB_PWR_UNIT_LOAD *
794535f36846Ssl 		    USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
794635f36846Ssl 
794735f36846Ssl 		ASSERT(!usba_is_root_hub(hubd->h_dip));
794835f36846Ssl 
794935f36846Ssl 		if (!usba_is_root_hub(hubd->h_dip)) {
795035f36846Ssl 			/*
795135f36846Ssl 			 * two bus-powered hubs are not
795235f36846Ssl 			 * allowed to be concatenated
795335f36846Ssl 			 */
795435f36846Ssl 			mutex_exit(HUBD_MUTEX(hubd));
795535f36846Ssl 
795635f36846Ssl 			pdip = ddi_get_parent(hubd->h_dip);
795735f36846Ssl 			phubd = hubd_get_soft_state(pdip);
795835f36846Ssl 			ASSERT(phubd != NULL);
795935f36846Ssl 
796035f36846Ssl 			if (!phubd->h_ignore_pwr_budget) {
796135f36846Ssl 				mutex_enter(HUBD_MUTEX(phubd));
796235f36846Ssl 				if (phubd->h_local_pwr_on == B_FALSE) {
796335f36846Ssl 					USB_DPRINTF_L0(DPRINT_MASK_HUB,
796435f36846Ssl 					    hubd->h_log_handle,
796535f36846Ssl 					    "two bus-powered hubs cannot "
796635f36846Ssl 					    "be concatenated");
796735f36846Ssl 
796835f36846Ssl 					mutex_exit(HUBD_MUTEX(phubd));
796935f36846Ssl 					mutex_enter(HUBD_MUTEX(hubd));
797035f36846Ssl 
797135f36846Ssl 					return (USB_FAILURE);
797235f36846Ssl 				}
797335f36846Ssl 				mutex_exit(HUBD_MUTEX(phubd));
797435f36846Ssl 			}
797535f36846Ssl 
797635f36846Ssl 			mutex_enter(HUBD_MUTEX(hubd));
797735f36846Ssl 
797835f36846Ssl 			USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
797935f36846Ssl 			    "hub is bus-powered");
798035f36846Ssl 		} else {
798135f36846Ssl 			USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
798235f36846Ssl 			    "root-hub must be local-powered");
798335f36846Ssl 		}
798435f36846Ssl 
798535f36846Ssl 		/*
798635f36846Ssl 		 * Subtract the power consumed by the hub itself
798735f36846Ssl 		 * and get the power that can be supplied to
798835f36846Ssl 		 * downstream ports
798935f36846Ssl 		 */
799035f36846Ssl 		hubd->h_pwr_left -=
799135f36846Ssl 		    hubd->h_hub_descr.bHubContrCurrent /
799235f36846Ssl 		    USB_CFG_DESCR_PWR_UNIT;
799335f36846Ssl 		if (hubd->h_pwr_left < 0) {
799435f36846Ssl 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
799535f36846Ssl 			    "hubd->h_pwr_left is less than bHubContrCurrent, "
799635f36846Ssl 			    "should fail");
799735f36846Ssl 
799835f36846Ssl 			return (USB_FAILURE);
799935f36846Ssl 		}
800035f36846Ssl 	}
800135f36846Ssl 
800235f36846Ssl 	return (USB_SUCCESS);
800335f36846Ssl }
800435f36846Ssl 
800535f36846Ssl 
800635f36846Ssl /*
800735f36846Ssl  * usba_hubdi_check_power_budget:
800835f36846Ssl  *	Check if the hub has enough power budget to allow a
800935f36846Ssl  *	child device to select a configuration of config_index.
801035f36846Ssl  */
801135f36846Ssl int
801235f36846Ssl usba_hubdi_check_power_budget(dev_info_t *dip, usba_device_t *child_ud,
801335f36846Ssl 	uint_t config_index)
801435f36846Ssl {
801535f36846Ssl 	int16_t		pwr_left, pwr_limit, pwr_required;
801635f36846Ssl 	size_t		size;
801735f36846Ssl 	usb_cfg_descr_t cfg_descr;
801835f36846Ssl 	hubd_t		*hubd;
801935f36846Ssl 
802035f36846Ssl 	if ((hubd = hubd_get_soft_state(dip)) == NULL) {
802135f36846Ssl 
802235f36846Ssl 		return (USB_FAILURE);
802335f36846Ssl 	}
802435f36846Ssl 
802535f36846Ssl 	if (hubd->h_ignore_pwr_budget) {
802635f36846Ssl 
802735f36846Ssl 		return (USB_SUCCESS);
802835f36846Ssl 	}
802935f36846Ssl 
803035f36846Ssl 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
803135f36846Ssl 	    "usba_hubdi_check_power_budget: "
803235f36846Ssl 	    "dip=0x%p child_ud=0x%p conf_index=%d", dip,
803335f36846Ssl 	    child_ud, config_index);
803435f36846Ssl 
803535f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
803635f36846Ssl 	pwr_limit = hubd->h_pwr_limit;
803735f36846Ssl 	if (hubd->h_local_pwr_on == B_FALSE) {
803835f36846Ssl 		pwr_left = hubd->h_pwr_left;
803935f36846Ssl 		pwr_limit = (pwr_limit <= pwr_left) ? pwr_limit : pwr_left;
804035f36846Ssl 	}
804135f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
804235f36846Ssl 
804335f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
804435f36846Ssl 	    "usba_hubdi_check_power_budget: "
804535f36846Ssl 	    "available power is %dmA", pwr_limit * USB_CFG_DESCR_PWR_UNIT);
804635f36846Ssl 
804735f36846Ssl 	size = usb_parse_cfg_descr(
804835f36846Ssl 	    child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
804935f36846Ssl 	    &cfg_descr, USB_CFG_DESCR_SIZE);
805035f36846Ssl 
805135f36846Ssl 	if (size != USB_CFG_DESCR_SIZE) {
805235f36846Ssl 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
805335f36846Ssl 		    "get hub configuration descriptor failed");
805435f36846Ssl 
805535f36846Ssl 		return (USB_FAILURE);
805635f36846Ssl 	}
805735f36846Ssl 
805835f36846Ssl 	pwr_required = cfg_descr.bMaxPower;
805935f36846Ssl 
806035f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
806135f36846Ssl 	    "usba_hubdi_check_power_budget: "
806235f36846Ssl 	    "child bmAttributes=0x%x bMaxPower=%d "
806335f36846Ssl 	    "with config_index=%d", cfg_descr.bmAttributes,
806435f36846Ssl 	    pwr_required, config_index);
806535f36846Ssl 
806635f36846Ssl 	if (pwr_required > pwr_limit) {
806735f36846Ssl 		USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
806835f36846Ssl 		    "configuration %d for device %s %s at port %d "
806935f36846Ssl 		    "exceeds power available for this port, please "
807035f36846Ssl 		    "re-insert your device into another hub port which "
807135f36846Ssl 		    "has enough power",
807235f36846Ssl 		    config_index,
807335f36846Ssl 		    child_ud->usb_mfg_str,
807435f36846Ssl 		    child_ud->usb_product_str,
807535f36846Ssl 		    child_ud->usb_port);
807635f36846Ssl 
807735f36846Ssl 		return (USB_FAILURE);
807835f36846Ssl 	}
807935f36846Ssl 
808035f36846Ssl 	return (USB_SUCCESS);
808135f36846Ssl }
808235f36846Ssl 
808335f36846Ssl 
808435f36846Ssl /*
808535f36846Ssl  * usba_hubdi_incr_power_budget:
808635f36846Ssl  *	Increase the hub power budget value when a child device
808735f36846Ssl  *	is removed from a bus-powered hub port.
808835f36846Ssl  */
808935f36846Ssl void
809035f36846Ssl usba_hubdi_incr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
809135f36846Ssl {
809235f36846Ssl 	uint16_t	pwr_value;
809335f36846Ssl 	hubd_t		*hubd = hubd_get_soft_state(dip);
809435f36846Ssl 
809535f36846Ssl 	ASSERT(hubd != NULL);
809635f36846Ssl 
809735f36846Ssl 	if (hubd->h_ignore_pwr_budget) {
809835f36846Ssl 
809935f36846Ssl 		return;
810035f36846Ssl 	}
810135f36846Ssl 
810235f36846Ssl 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
810335f36846Ssl 	    "usba_hubdi_incr_power_budget: "
810435f36846Ssl 	    "dip=0x%p child_ud=0x%p", dip, child_ud);
810535f36846Ssl 
810635f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
810735f36846Ssl 	if (hubd->h_local_pwr_on == B_TRUE) {
810835f36846Ssl 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
810935f36846Ssl 		    "usba_hubdi_incr_power_budget: "
811035f36846Ssl 		    "hub is local powered");
811135f36846Ssl 		mutex_exit(HUBD_MUTEX(hubd));
811235f36846Ssl 
811335f36846Ssl 		return;
811435f36846Ssl 	}
811535f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
811635f36846Ssl 
811735f36846Ssl 	mutex_enter(&child_ud->usb_mutex);
811835f36846Ssl 	if (child_ud->usb_pwr_from_hub == 0) {
811935f36846Ssl 		mutex_exit(&child_ud->usb_mutex);
812035f36846Ssl 
812135f36846Ssl 		return;
812235f36846Ssl 	}
812335f36846Ssl 	pwr_value = child_ud->usb_pwr_from_hub;
812435f36846Ssl 	mutex_exit(&child_ud->usb_mutex);
812535f36846Ssl 
812635f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
812735f36846Ssl 	hubd->h_pwr_left += pwr_value;
812835f36846Ssl 
812935f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
813035f36846Ssl 	    "usba_hubdi_incr_power_budget: "
813135f36846Ssl 	    "available power is %dmA, increased by %dmA",
813235f36846Ssl 	    hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
813335f36846Ssl 	    pwr_value * USB_CFG_DESCR_PWR_UNIT);
813435f36846Ssl 
813535f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
813635f36846Ssl 
813735f36846Ssl 	mutex_enter(&child_ud->usb_mutex);
813835f36846Ssl 	child_ud->usb_pwr_from_hub = 0;
813935f36846Ssl 	mutex_exit(&child_ud->usb_mutex);
814035f36846Ssl }
814135f36846Ssl 
814235f36846Ssl 
814335f36846Ssl /*
814435f36846Ssl  * usba_hubdi_decr_power_budget:
814535f36846Ssl  *	Decrease the hub power budget value when a child device
814635f36846Ssl  *	is inserted to a bus-powered hub port.
814735f36846Ssl  */
814835f36846Ssl void
814935f36846Ssl usba_hubdi_decr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
815035f36846Ssl {
815135f36846Ssl 	uint16_t	pwr_value;
815235f36846Ssl 	size_t		size;
815335f36846Ssl 	usb_cfg_descr_t	cfg_descr;
815435f36846Ssl 	hubd_t		*hubd = hubd_get_soft_state(dip);
815535f36846Ssl 
815635f36846Ssl 	ASSERT(hubd != NULL);
815735f36846Ssl 
815835f36846Ssl 	if (hubd->h_ignore_pwr_budget) {
815935f36846Ssl 
816035f36846Ssl 		return;
816135f36846Ssl 	}
816235f36846Ssl 
816335f36846Ssl 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
816435f36846Ssl 	    "usba_hubdi_decr_power_budget: "
816535f36846Ssl 	    "dip=0x%p child_ud=0x%p", dip, child_ud);
816635f36846Ssl 
816735f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
816835f36846Ssl 	if (hubd->h_local_pwr_on == B_TRUE) {
816935f36846Ssl 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
817035f36846Ssl 		    "usba_hubdi_decr_power_budget: "
817135f36846Ssl 		    "hub is local powered");
817235f36846Ssl 		mutex_exit(HUBD_MUTEX(hubd));
817335f36846Ssl 
817435f36846Ssl 		return;
817535f36846Ssl 	}
817635f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
817735f36846Ssl 
817835f36846Ssl 	mutex_enter(&child_ud->usb_mutex);
817935f36846Ssl 	if (child_ud->usb_pwr_from_hub > 0) {
818035f36846Ssl 		mutex_exit(&child_ud->usb_mutex);
818135f36846Ssl 
818235f36846Ssl 		return;
818335f36846Ssl 	}
818435f36846Ssl 	mutex_exit(&child_ud->usb_mutex);
818535f36846Ssl 
818635f36846Ssl 	size = usb_parse_cfg_descr(
818735f36846Ssl 	    child_ud->usb_cfg, child_ud->usb_cfg_length,
818835f36846Ssl 	    &cfg_descr, USB_CFG_DESCR_SIZE);
818935f36846Ssl 	ASSERT(size == USB_CFG_DESCR_SIZE);
819035f36846Ssl 
819135f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
819235f36846Ssl 	pwr_value = cfg_descr.bMaxPower;
819335f36846Ssl 	hubd->h_pwr_left -= pwr_value;
819435f36846Ssl 	ASSERT(hubd->h_pwr_left >= 0);
819535f36846Ssl 
819635f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
819735f36846Ssl 	    "usba_hubdi_decr_power_budget: "
819835f36846Ssl 	    "available power is %dmA, decreased by %dmA",
819935f36846Ssl 	    hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
820035f36846Ssl 	    pwr_value * USB_CFG_DESCR_PWR_UNIT);
820135f36846Ssl 
820235f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
820335f36846Ssl 
820435f36846Ssl 	mutex_enter(&child_ud->usb_mutex);
820535f36846Ssl 	child_ud->usb_pwr_from_hub = pwr_value;
820635f36846Ssl 	mutex_exit(&child_ud->usb_mutex);
820735f36846Ssl }
8208