xref: /illumos-gate/usr/src/uts/common/io/usb/usba/hubdi.c (revision 8e1b7aa1)
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
5fef1e07eSsl  * Common Development and Distribution License (the "License").
6fef1e07eSsl  * 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 /*
22*8e1b7aa1Sfei feng - Sun Microsystems - Beijing China  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * USBA: Solaris USB Architecture support for the hub
297c478bd9Sstevel@tonic-gate  * including root hub
307c478bd9Sstevel@tonic-gate  * Most of the code for hubd resides in this file and
317c478bd9Sstevel@tonic-gate  * is shared between the HCD root hub support and hubd
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate #define	USBA_FRAMEWORK
347c478bd9Sstevel@tonic-gate #include <sys/usb/usba.h>
357c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_devdb.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
377c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
387c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_types.h>
397c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hubdi.h>
407c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
417c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hub.h>
427c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hubdvar.h>
437c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hubd_impl.h>
447c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
457c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
467c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
47d29f5a71Szhigang lu - Sun Microsystems - Beijing China #include <sys/strsun.h>
487c478bd9Sstevel@tonic-gate 
4940482326SVincent Wang /*
5040482326SVincent Wang  * External functions
5140482326SVincent Wang  */
5240482326SVincent Wang extern boolean_t consconfig_console_is_ready(void);
5340482326SVincent Wang 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Prototypes for static functions
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate static	int	usba_hubdi_bus_ctl(
587c478bd9Sstevel@tonic-gate 			dev_info_t		*dip,
597c478bd9Sstevel@tonic-gate 			dev_info_t		*rdip,
607c478bd9Sstevel@tonic-gate 			ddi_ctl_enum_t		op,
617c478bd9Sstevel@tonic-gate 			void			*arg,
627c478bd9Sstevel@tonic-gate 			void			*result);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static int	usba_hubdi_map_fault(
657c478bd9Sstevel@tonic-gate 			dev_info_t		*dip,
667c478bd9Sstevel@tonic-gate 			dev_info_t		*rdip,
677c478bd9Sstevel@tonic-gate 			struct hat		*hat,
687c478bd9Sstevel@tonic-gate 			struct seg		*seg,
697c478bd9Sstevel@tonic-gate 			caddr_t 		addr,
707c478bd9Sstevel@tonic-gate 			struct devpage		*dp,
717c478bd9Sstevel@tonic-gate 			pfn_t			pfn,
727c478bd9Sstevel@tonic-gate 			uint_t			prot,
737c478bd9Sstevel@tonic-gate 			uint_t			lock);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static int hubd_busop_get_eventcookie(dev_info_t *dip,
767c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
777c478bd9Sstevel@tonic-gate 			char *eventname,
787c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t *cookie);
797c478bd9Sstevel@tonic-gate static int hubd_busop_add_eventcall(dev_info_t *dip,
807c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
817c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t cookie,
827c478bd9Sstevel@tonic-gate 			void (*callback)(dev_info_t *dip,
837c478bd9Sstevel@tonic-gate 				ddi_eventcookie_t cookie, void *arg,
847c478bd9Sstevel@tonic-gate 				void *bus_impldata),
857c478bd9Sstevel@tonic-gate 			void *arg, ddi_callback_id_t *cb_id);
867c478bd9Sstevel@tonic-gate static int hubd_busop_remove_eventcall(dev_info_t *dip,
877c478bd9Sstevel@tonic-gate 			ddi_callback_id_t cb_id);
887c478bd9Sstevel@tonic-gate static int hubd_bus_config(dev_info_t *dip,
897c478bd9Sstevel@tonic-gate 			uint_t flag,
907c478bd9Sstevel@tonic-gate 			ddi_bus_config_op_t op,
917c478bd9Sstevel@tonic-gate 			void *arg,
927c478bd9Sstevel@tonic-gate 			dev_info_t **child);
937c478bd9Sstevel@tonic-gate static int hubd_bus_unconfig(dev_info_t *dip,
947c478bd9Sstevel@tonic-gate 			uint_t flag,
957c478bd9Sstevel@tonic-gate 			ddi_bus_config_op_t op,
967c478bd9Sstevel@tonic-gate 			void *arg);
977c478bd9Sstevel@tonic-gate static int hubd_bus_power(dev_info_t *dip, void *impl_arg,
987c478bd9Sstevel@tonic-gate 			pm_bus_power_op_t op, void *arg, void *result);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static usb_port_t  hubd_get_port_num(hubd_t *, struct devctl_iocdata *);
1017c478bd9Sstevel@tonic-gate static dev_info_t *hubd_get_child_dip(hubd_t *, usb_port_t);
1027c478bd9Sstevel@tonic-gate static uint_t hubd_cfgadm_state(hubd_t *, usb_port_t);
1037c478bd9Sstevel@tonic-gate static int hubd_toggle_port(hubd_t *, usb_port_t);
1047c478bd9Sstevel@tonic-gate static void hubd_register_cpr_callback(hubd_t *);
1057c478bd9Sstevel@tonic-gate static void hubd_unregister_cpr_callback(hubd_t *);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Busops vector for USB HUB's
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate struct bus_ops usba_hubdi_busops =	{
1117c478bd9Sstevel@tonic-gate 	BUSO_REV,
1127c478bd9Sstevel@tonic-gate 	nullbusmap,			/* bus_map */
1137c478bd9Sstevel@tonic-gate 	NULL,				/* bus_get_intrspec */
1147c478bd9Sstevel@tonic-gate 	NULL,				/* bus_add_intrspec */
1157c478bd9Sstevel@tonic-gate 	NULL,				/* bus_remove_intrspec */
1167c478bd9Sstevel@tonic-gate 	usba_hubdi_map_fault,		/* bus_map_fault */
1177c478bd9Sstevel@tonic-gate 	ddi_dma_map,			/* bus_dma_map */
1187c478bd9Sstevel@tonic-gate 	ddi_dma_allochdl,
1197c478bd9Sstevel@tonic-gate 	ddi_dma_freehdl,
1207c478bd9Sstevel@tonic-gate 	ddi_dma_bindhdl,
1217c478bd9Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1227c478bd9Sstevel@tonic-gate 	ddi_dma_flush,
1237c478bd9Sstevel@tonic-gate 	ddi_dma_win,
1247c478bd9Sstevel@tonic-gate 	ddi_dma_mctl,			/* bus_dma_ctl */
1257c478bd9Sstevel@tonic-gate 	usba_hubdi_bus_ctl,		/* bus_ctl */
1267c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,		/* bus_prop_op */
1277c478bd9Sstevel@tonic-gate 	hubd_busop_get_eventcookie,
1287c478bd9Sstevel@tonic-gate 	hubd_busop_add_eventcall,
1297c478bd9Sstevel@tonic-gate 	hubd_busop_remove_eventcall,
1307c478bd9Sstevel@tonic-gate 	NULL,				/* bus_post_event */
1317c478bd9Sstevel@tonic-gate 	NULL,				/* bus_intr_ctl */
1327c478bd9Sstevel@tonic-gate 	hubd_bus_config,		/* bus_config */
1337c478bd9Sstevel@tonic-gate 	hubd_bus_unconfig,		/* bus_unconfig */
1347c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_init */
1357c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_fini */
1367c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_enter */
1377c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_exit */
1387c478bd9Sstevel@tonic-gate 	hubd_bus_power			/* bus_power */
1397c478bd9Sstevel@tonic-gate };
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * local variables
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate static kmutex_t	usba_hubdi_mutex;	/* protects USBA HUB data structures */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static usba_list_entry_t	usba_hubdi_list;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate usb_log_handle_t	hubdi_log_handle;
1507c478bd9Sstevel@tonic-gate uint_t			hubdi_errlevel = USB_LOG_L4;
1517c478bd9Sstevel@tonic-gate uint_t			hubdi_errmask = (uint_t)-1;
152d29f5a71Szhigang lu - Sun Microsystems - Beijing China uint8_t			hubdi_min_pm_threshold = 5; /* seconds */
153aa041649SRaymond Chen uint8_t			hubdi_reset_delay = 20; /* seconds */
154*8e1b7aa1Sfei feng - Sun Microsystems - Beijing China extern int modrootloaded;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * initialize private data
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate void
1607c478bd9Sstevel@tonic-gate usba_hubdi_initialization()
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	hubdi_log_handle = usb_alloc_log_hdl(NULL, "hubdi", &hubdi_errlevel,
163c0f24e5bSlg 	    &hubdi_errmask, NULL, 0);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
1667c478bd9Sstevel@tonic-gate 	    "usba_hubdi_initialization");
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	mutex_init(&usba_hubdi_mutex, NULL, MUTEX_DRIVER, NULL);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	usba_init_list(&usba_hubdi_list, NULL, NULL);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate void
1757c478bd9Sstevel@tonic-gate usba_hubdi_destroy()
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
1787c478bd9Sstevel@tonic-gate 	    "usba_hubdi_destroy");
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	mutex_destroy(&usba_hubdi_mutex);
1817c478bd9Sstevel@tonic-gate 	usba_destroy_list(&usba_hubdi_list);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(hubdi_log_handle);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Called by an	HUB to attach an instance of the driver
1897c478bd9Sstevel@tonic-gate  *	make this instance known to USBA
1907c478bd9Sstevel@tonic-gate  *	the HUB	should initialize usba_hubdi structure prior
1917c478bd9Sstevel@tonic-gate  *	to calling this	interface
1927c478bd9Sstevel@tonic-gate  */
193ff0e937bSRaymond Chen int
1947c478bd9Sstevel@tonic-gate usba_hubdi_register(dev_info_t	*dip,
1957c478bd9Sstevel@tonic-gate 		uint_t		flags)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	usba_hubdi_t *hubdi = kmem_zalloc(sizeof (usba_hubdi_t), KM_SLEEP);
1987c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
2017c478bd9Sstevel@tonic-gate 	    "usba_hubdi_register: %s", ddi_node_name(dip));
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	hubdi->hubdi_dip = dip;
2047c478bd9Sstevel@tonic-gate 	hubdi->hubdi_flags = flags;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	usba_device->usb_hubdi = hubdi;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * add this hubdi instance to the list of known hubdi's
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	usba_init_list(&hubdi->hubdi_list, (usb_opaque_t)hubdi,
2127c478bd9Sstevel@tonic-gate 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)->
2137c478bd9Sstevel@tonic-gate 	    hcdi_iblock_cookie);
2147c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_hubdi_mutex);
2157c478bd9Sstevel@tonic-gate 	usba_add_to_list(&usba_hubdi_list, &hubdi->hubdi_list);
2167c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_hubdi_mutex);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Called by an	HUB to detach an instance of the driver
2247c478bd9Sstevel@tonic-gate  */
225ff0e937bSRaymond Chen int
2267c478bd9Sstevel@tonic-gate usba_hubdi_unregister(dev_info_t *dip)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
2297c478bd9Sstevel@tonic-gate 	usba_hubdi_t *hubdi = usba_device->usb_hubdi;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
2327c478bd9Sstevel@tonic-gate 	    "usba_hubdi_unregister: %s", ddi_node_name(dip));
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_hubdi_mutex);
2357c478bd9Sstevel@tonic-gate 	(void) usba_rm_from_list(&usba_hubdi_list, &hubdi->hubdi_list);
2367c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_hubdi_mutex);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	usba_destroy_list(&hubdi->hubdi_list);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	kmem_free(hubdi, sizeof (usba_hubdi_t));
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * misc bus routines currently not used
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2507c478bd9Sstevel@tonic-gate static int
2517c478bd9Sstevel@tonic-gate usba_hubdi_map_fault(dev_info_t *dip,
2527c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
2537c478bd9Sstevel@tonic-gate 	struct hat	*hat,
2547c478bd9Sstevel@tonic-gate 	struct seg	*seg,
2557c478bd9Sstevel@tonic-gate 	caddr_t 	addr,
2567c478bd9Sstevel@tonic-gate 	struct devpage	*dp,
2577c478bd9Sstevel@tonic-gate 	pfn_t		pfn,
2587c478bd9Sstevel@tonic-gate 	uint_t		prot,
2597c478bd9Sstevel@tonic-gate 	uint_t		lock)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * root hub support. the root hub uses the same devi as the HCD
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate int
2697c478bd9Sstevel@tonic-gate usba_hubdi_bind_root_hub(dev_info_t *dip,
2707c478bd9Sstevel@tonic-gate 	uchar_t	*root_hub_config_descriptor,
2717c478bd9Sstevel@tonic-gate 	size_t config_length,
2727c478bd9Sstevel@tonic-gate 	usb_dev_descr_t *root_hub_device_descriptor)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device;
2757c478bd9Sstevel@tonic-gate 	usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
2767c478bd9Sstevel@tonic-gate 	hubd_t	*root_hubd;
2777c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t ph = NULL;
2787c478bd9Sstevel@tonic-gate 	dev_info_t *child = ddi_get_child(dip);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
2817c478bd9Sstevel@tonic-gate 	    "root-hub") != NDI_SUCCESS) {
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
286de6f998eSrui wang - Sun Microsystems - Beijing China 	usba_add_root_hub(dip);
287de6f998eSrui wang - Sun Microsystems - Beijing China 
2887c478bd9Sstevel@tonic-gate 	root_hubd = kmem_zalloc(sizeof (hubd_t), KM_SLEEP);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * create and initialize a usba_device structure
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	usba_device = usba_alloc_usba_device(dip);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
2967c478bd9Sstevel@tonic-gate 	usba_device->usb_hcdi_ops = hcdi->hcdi_ops;
2977c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg = root_hub_config_descriptor;
2987c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_length = config_length;
2997c478bd9Sstevel@tonic-gate 	usba_device->usb_dev_descr = root_hub_device_descriptor;
3007c478bd9Sstevel@tonic-gate 	usba_device->usb_port = 1;
3017c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = ROOT_HUB_ADDR;
3027c478bd9Sstevel@tonic-gate 	usba_device->usb_root_hubd = root_hubd;
3037c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array = kmem_zalloc(sizeof (uchar_t *),
304c0f24e5bSlg 	    KM_SLEEP);
3057c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array_length = sizeof (uchar_t *);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array_len = kmem_zalloc(sizeof (uint16_t),
308c0f24e5bSlg 	    KM_SLEEP);
3097c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array_len_length = sizeof (uint16_t);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array[0] = root_hub_config_descriptor;
3127c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_array_len[0] =
313c0f24e5bSlg 	    sizeof (root_hub_config_descriptor);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_str_descr = kmem_zalloc(sizeof (uchar_t *),
316c0f24e5bSlg 	    KM_SLEEP);
3177c478bd9Sstevel@tonic-gate 	usba_device->usb_n_cfgs = 1;
3187c478bd9Sstevel@tonic-gate 	usba_device->usb_n_ifs = 1;
3197c478bd9Sstevel@tonic-gate 	usba_device->usb_dip = dip;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	usba_device->usb_client_flags = kmem_zalloc(
3227c478bd9Sstevel@tonic-gate 	    usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	usba_device->usb_client_attach_list = kmem_zalloc(
3257c478bd9Sstevel@tonic-gate 	    usba_device->usb_n_ifs *
3267c478bd9Sstevel@tonic-gate 	    sizeof (*usba_device->usb_client_attach_list), KM_SLEEP);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	usba_device->usb_client_ev_cb_list = kmem_zalloc(
3297c478bd9Sstevel@tonic-gate 	    usba_device->usb_n_ifs *
3307c478bd9Sstevel@tonic-gate 	    sizeof (*usba_device->usb_client_ev_cb_list), KM_SLEEP);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * The bDeviceProtocol field of root hub device specifies,
3347c478bd9Sstevel@tonic-gate 	 * whether root hub is a High or Full speed usb device.
3357c478bd9Sstevel@tonic-gate 	 */
3367c478bd9Sstevel@tonic-gate 	if (root_hub_device_descriptor->bDeviceProtocol) {
3377c478bd9Sstevel@tonic-gate 		usba_device->usb_port_status = USBA_HIGH_SPEED_DEV;
3387c478bd9Sstevel@tonic-gate 	} else {
3397c478bd9Sstevel@tonic-gate 		usba_device->usb_port_status = USBA_FULL_SPEED_DEV;
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	usba_set_usba_device(dip, usba_device);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/*
3477c478bd9Sstevel@tonic-gate 	 * For the root hub the default pipe is not yet open
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 	if (usb_pipe_open(dip, NULL, NULL,
3507c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != USB_SUCCESS) {
3517c478bd9Sstevel@tonic-gate 		goto fail;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * kill off all OBP children, they may not be fully
3567c478bd9Sstevel@tonic-gate 	 * enumerated
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 	while (child) {
3597c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(child);
3607c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(child, 0);
3617c478bd9Sstevel@tonic-gate 		child = next;
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * "attach" the root hub driver
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	if (usba_hubdi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
3687c478bd9Sstevel@tonic-gate 		goto fail;
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate fail:
3747c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
3757c478bd9Sstevel@tonic-gate 
376de6f998eSrui wang - Sun Microsystems - Beijing China 	usba_rem_root_hub(dip);
377de6f998eSrui wang - Sun Microsystems - Beijing China 
3787c478bd9Sstevel@tonic-gate 	if (ph) {
3797c478bd9Sstevel@tonic-gate 		usb_pipe_close(dip, ph,
3807c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array,
384c0f24e5bSlg 	    usba_device->usb_cfg_array_length);
3857c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array_len,
386c0f24e5bSlg 	    usba_device->usb_cfg_array_len_length);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	usba_free_usba_device(usba_device);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	usba_set_usba_device(dip, NULL);
3937c478bd9Sstevel@tonic-gate 	if (root_hubd) {
3947c478bd9Sstevel@tonic-gate 		kmem_free(root_hubd, sizeof (hubd_t));
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate int
4027c478bd9Sstevel@tonic-gate usba_hubdi_unbind_root_hub(dev_info_t *dip)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* was root hub attached? */
4077c478bd9Sstevel@tonic-gate 	if (!(usba_is_root_hub(dip))) {
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		/* return success anyway */
4107c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * usba_hubdi_detach also closes the default pipe
4157c478bd9Sstevel@tonic-gate 	 * and removes properties so there is no need to
4167c478bd9Sstevel@tonic-gate 	 * do it here
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	if (usba_hubdi_detach(dip, DDI_DETACH) != DDI_SUCCESS) {
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(dip)) {
421d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
4227c478bd9Sstevel@tonic-gate 			    "failure to unbind root hub after attach failure");
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_root_hubd, sizeof (hubd_t));
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array,
433c0f24e5bSlg 	    usba_device->usb_cfg_array_length);
4347c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array_len,
435c0f24e5bSlg 	    usba_device->usb_cfg_array_len_length);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	usba_free_usba_device(usba_device);
4407c478bd9Sstevel@tonic-gate 
441de6f998eSrui wang - Sun Microsystems - Beijing China 	usba_rem_root_hub(dip);
442de6f998eSrui wang - Sun Microsystems - Beijing China 
4437c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * Actual Hub Driver support code:
4517c478bd9Sstevel@tonic-gate  *	shared by root hub and non-root hubs
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate /* Debugging support */
456ff0e937bSRaymond Chen uint_t hubd_errlevel	= USB_LOG_L4;
457ff0e937bSRaymond Chen uint_t hubd_errmask	= (uint_t)DPRINT_MASK_ALL;
458ff0e937bSRaymond Chen uint_t hubd_instance_debug = (uint_t)-1;
4597c478bd9Sstevel@tonic-gate static uint_t hubdi_bus_config_debug = 0;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel))
4627c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask))
4637c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug))
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
4667c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate  * local variables:
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  * Amount of time to wait between resetting the port and accessing
4737c478bd9Sstevel@tonic-gate  * the device.	The value is in microseconds.
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate static uint_t hubd_device_delay = 1000000;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * enumeration retry
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate #define	HUBD_PORT_RETRY 5
4817c478bd9Sstevel@tonic-gate static uint_t hubd_retry_enumerate = HUBD_PORT_RETRY;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * Stale hotremoved device cleanup delay
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate #define	HUBD_STALE_DIP_CLEANUP_DELAY	5000000
4877c478bd9Sstevel@tonic-gate static uint_t hubd_dip_cleanup_delay = HUBD_STALE_DIP_CLEANUP_DELAY;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * retries for USB suspend and resume
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate #define	HUBD_SUS_RES_RETRY	2
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate void	*hubd_statep;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * prototypes
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate static int hubd_cleanup(dev_info_t *dip, hubd_t  *hubd);
5007c478bd9Sstevel@tonic-gate static int hubd_check_ports(hubd_t  *hubd);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static int  hubd_open_intr_pipe(hubd_t *hubd);
5037c478bd9Sstevel@tonic-gate static void hubd_start_polling(hubd_t *hubd, int always);
5047c478bd9Sstevel@tonic-gate static void hubd_stop_polling(hubd_t *hubd);
5057c478bd9Sstevel@tonic-gate static void hubd_close_intr_pipe(hubd_t *hubd);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate static void hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *req);
5087c478bd9Sstevel@tonic-gate static void hubd_exception_cb(usb_pipe_handle_t pipe,
5097c478bd9Sstevel@tonic-gate 						usb_intr_req_t *req);
5107c478bd9Sstevel@tonic-gate static void hubd_hotplug_thread(void *arg);
511ffcd51f3Slg static void hubd_reset_thread(void *arg);
5127c478bd9Sstevel@tonic-gate static int hubd_create_child(dev_info_t *dip,
5137c478bd9Sstevel@tonic-gate 		hubd_t		*hubd,
5147c478bd9Sstevel@tonic-gate 		usba_device_t	*usba_device,
5157c478bd9Sstevel@tonic-gate 		usb_port_status_t port_status,
5167c478bd9Sstevel@tonic-gate 		usb_port_t	port,
5177c478bd9Sstevel@tonic-gate 		int		iteration);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate static int hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag,
5207c478bd9Sstevel@tonic-gate 	boolean_t retry);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate static int hubd_get_hub_descriptor(hubd_t *hubd);
5237c478bd9Sstevel@tonic-gate 
52435f36846Ssl static int hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status);
52535f36846Ssl 
5267c478bd9Sstevel@tonic-gate static int hubd_reset_port(hubd_t *hubd, usb_port_t port);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate static int hubd_get_hub_status(hubd_t *hubd);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate static int hubd_handle_port_connect(hubd_t *hubd, usb_port_t port);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate static int hubd_disable_port(hubd_t *hubd, usb_port_t port);
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate static int hubd_enable_port(hubd_t *hubd, usb_port_t port);
5357c478bd9Sstevel@tonic-gate static int hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static int hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
5387c478bd9Sstevel@tonic-gate 	uint16_t *status, uint16_t *change, uint_t ack_flag);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate static int hubd_enable_all_port_power(hubd_t *hubd);
5417c478bd9Sstevel@tonic-gate static int hubd_disable_all_port_power(hubd_t *hubd);
5427c478bd9Sstevel@tonic-gate static int hubd_disable_port_power(hubd_t *hubd, usb_port_t port);
5437c478bd9Sstevel@tonic-gate static int hubd_enable_port_power(hubd_t *hubd, usb_port_t port);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate static void hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate static int hubd_can_suspend(hubd_t *hubd);
5487c478bd9Sstevel@tonic-gate static void hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd);
5497c478bd9Sstevel@tonic-gate static int hubd_setdevaddr(hubd_t *hubd, usb_port_t port);
5507c478bd9Sstevel@tonic-gate static void hubd_setdevconfig(hubd_t *hubd, usb_port_t port);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate static int hubd_register_events(hubd_t *hubd);
5537c478bd9Sstevel@tonic-gate static void hubd_do_callback(hubd_t *hubd, dev_info_t *dip,
5547c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie);
5557c478bd9Sstevel@tonic-gate static void hubd_run_callbacks(hubd_t *hubd, usba_event_t type);
5567c478bd9Sstevel@tonic-gate static void hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type);
5577c478bd9Sstevel@tonic-gate static void hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate static int hubd_disconnect_event_cb(dev_info_t *dip);
5607c478bd9Sstevel@tonic-gate static int hubd_reconnect_event_cb(dev_info_t *dip);
5617c478bd9Sstevel@tonic-gate static int hubd_pre_suspend_event_cb(dev_info_t *dip);
5627c478bd9Sstevel@tonic-gate static int hubd_post_resume_event_cb(dev_info_t *dip);
5637c478bd9Sstevel@tonic-gate static int hubd_cpr_suspend(hubd_t *hubd);
5647c478bd9Sstevel@tonic-gate static void hubd_cpr_resume(dev_info_t *dip);
5657c478bd9Sstevel@tonic-gate static int hubd_restore_state_cb(dev_info_t *dip);
566ffcd51f3Slg static int hubd_check_same_device(hubd_t *hubd, usb_port_t port);
5677c478bd9Sstevel@tonic-gate 
56835f36846Ssl static int hubd_init_power_budget(hubd_t *hubd);
56935f36846Ssl 
5707c478bd9Sstevel@tonic-gate static ndi_event_definition_t hubd_ndi_event_defs[] = {
5717c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
5727c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5737c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
5747c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5757c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
5767c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5777c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
5787c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL}
5797c478bd9Sstevel@tonic-gate };
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate #define	HUBD_N_NDI_EVENTS \
5827c478bd9Sstevel@tonic-gate 	(sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t))
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate static ndi_event_set_t hubd_ndi_events = {
5857c478bd9Sstevel@tonic-gate 	NDI_EVENTS_REV1, HUBD_N_NDI_EVENTS, hubd_ndi_event_defs};
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /* events received from parent */
5887c478bd9Sstevel@tonic-gate static usb_event_t hubd_events = {
5897c478bd9Sstevel@tonic-gate 	hubd_disconnect_event_cb,
5907c478bd9Sstevel@tonic-gate 	hubd_reconnect_event_cb,
5917c478bd9Sstevel@tonic-gate 	hubd_pre_suspend_event_cb,
5927c478bd9Sstevel@tonic-gate 	hubd_post_resume_event_cb
5937c478bd9Sstevel@tonic-gate };
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate  * hubd_get_soft_state() returns the hubd soft state
598ff0e937bSRaymond Chen  *
599ff0e937bSRaymond Chen  * WUSB support extends this function to support wire adapter class
600ff0e937bSRaymond Chen  * devices. The hubd soft state for the wire adapter class device
601ff0e937bSRaymond Chen  * would be stored in usb_root_hubd field of the usba_device structure,
602ff0e937bSRaymond Chen  * just as the USB host controller drivers do.
6037c478bd9Sstevel@tonic-gate  */
604ff0e937bSRaymond Chen hubd_t *
6057c478bd9Sstevel@tonic-gate hubd_get_soft_state(dev_info_t *dip)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		return (NULL);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
612ff0e937bSRaymond Chen 	if (usba_is_root_hub(dip) || usba_is_wa(dip)) {
6137c478bd9Sstevel@tonic-gate 		usba_device_t *usba_device = usba_get_usba_device(dip);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 		return (usba_device->usb_root_hubd);
6167c478bd9Sstevel@tonic-gate 	} else {
6177c478bd9Sstevel@tonic-gate 		int instance = ddi_get_instance(dip);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		return (ddi_get_soft_state(hubd_statep, instance));
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * PM support functions:
6267c478bd9Sstevel@tonic-gate  */
6277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6287c478bd9Sstevel@tonic-gate static void
6297c478bd9Sstevel@tonic-gate hubd_pm_busy_component(hubd_t *hubd, dev_info_t *dip, int component)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	if (hubd->h_hubpm != NULL) {
6327c478bd9Sstevel@tonic-gate 		hubd->h_hubpm->hubp_busy_pm++;
6337c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
6347c478bd9Sstevel@tonic-gate 		if (pm_busy_component(dip, 0) != DDI_SUCCESS) {
6357c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
6367c478bd9Sstevel@tonic-gate 			hubd->h_hubpm->hubp_busy_pm--;
6377c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
6407c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
6417c478bd9Sstevel@tonic-gate 		    "hubd_pm_busy_component: %d", hubd->h_hubpm->hubp_busy_pm);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6477c478bd9Sstevel@tonic-gate static void
6487c478bd9Sstevel@tonic-gate hubd_pm_idle_component(hubd_t *hubd, dev_info_t *dip, int component)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	if (hubd->h_hubpm != NULL) {
6517c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
6527c478bd9Sstevel@tonic-gate 		if (pm_idle_component(dip, 0) == DDI_SUCCESS) {
6537c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
6547c478bd9Sstevel@tonic-gate 			ASSERT(hubd->h_hubpm->hubp_busy_pm > 0);
6557c478bd9Sstevel@tonic-gate 			hubd->h_hubpm->hubp_busy_pm--;
6567c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
6597c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
6607c478bd9Sstevel@tonic-gate 		    "hubd_pm_idle_component: %d", hubd->h_hubpm->hubp_busy_pm);
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * track power level changes for children of this instance
6677c478bd9Sstevel@tonic-gate  */
6687c478bd9Sstevel@tonic-gate static void
6697c478bd9Sstevel@tonic-gate hubd_set_child_pwrlvl(hubd_t *hubd, usb_port_t port, uint8_t power)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	int	old_power, new_power, pwr;
6727c478bd9Sstevel@tonic-gate 	usb_port_t	portno;
6737c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
6767c478bd9Sstevel@tonic-gate 	    "hubd_set_child_pwrlvl: port=%d power=%d",
6777c478bd9Sstevel@tonic-gate 	    port, power);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
6807c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	old_power = 0;
6837c478bd9Sstevel@tonic-gate 	for (portno = 1; portno <= hubd->h_hub_descr.bNbrPorts; portno++) {
6847c478bd9Sstevel@tonic-gate 		old_power += hubpm->hubp_child_pwrstate[portno];
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* assign the port power */
6887c478bd9Sstevel@tonic-gate 	pwr = hubd->h_hubpm->hubp_child_pwrstate[port];
6897c478bd9Sstevel@tonic-gate 	hubd->h_hubpm->hubp_child_pwrstate[port] = power;
6907c478bd9Sstevel@tonic-gate 	new_power = old_power - pwr + power;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
6937c478bd9Sstevel@tonic-gate 	    "hubd_set_child_pwrlvl: new_power=%d old_power=%d",
6947c478bd9Sstevel@tonic-gate 	    new_power, old_power);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if ((new_power > 0) && (old_power == 0)) {
6977c478bd9Sstevel@tonic-gate 		/* we have the first child coming out of low power */
6987c478bd9Sstevel@tonic-gate 		(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
6997c478bd9Sstevel@tonic-gate 	} else if ((new_power == 0) && (old_power > 0)) {
7007c478bd9Sstevel@tonic-gate 		/* we have the last child going to low power */
7017c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate /*
7087c478bd9Sstevel@tonic-gate  * given a child dip, locate its port number
7097c478bd9Sstevel@tonic-gate  */
7107c478bd9Sstevel@tonic-gate static usb_port_t
7117c478bd9Sstevel@tonic-gate hubd_child_dip2port(hubd_t *hubd, dev_info_t *dip)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	usb_port_t	port;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
7167c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
7177c478bd9Sstevel@tonic-gate 		if (hubd->h_children_dips[port] == dip) {
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 			break;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 	ASSERT(port <= hubd->h_hub_descr.bNbrPorts);
7237c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	return (port);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate /*
7307c478bd9Sstevel@tonic-gate  * if the hub can be put into low power mode, return success
7317c478bd9Sstevel@tonic-gate  * NOTE: suspend here means going to lower power, not CPR suspend.
7327c478bd9Sstevel@tonic-gate  */
7337c478bd9Sstevel@tonic-gate static int
7347c478bd9Sstevel@tonic-gate hubd_can_suspend(hubd_t *hubd)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
7377c478bd9Sstevel@tonic-gate 	int		total_power = 0;
7387c478bd9Sstevel@tonic-gate 	usb_port_t	port;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DETACHING(hubd->h_dip)) {
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/*
7487c478bd9Sstevel@tonic-gate 	 * Don't go to lower power if haven't been at full power for enough
7497c478bd9Sstevel@tonic-gate 	 * time to let hotplug thread kickoff.
7507c478bd9Sstevel@tonic-gate 	 */
7517c478bd9Sstevel@tonic-gate 	if (ddi_get_time() < (hubpm->hubp_time_at_full_power +
7527c478bd9Sstevel@tonic-gate 	    hubpm->hubp_min_pm_threshold)) {
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	for (port = 1; (total_power == 0) &&
7587c478bd9Sstevel@tonic-gate 	    (port <= hubd->h_hub_descr.bNbrPorts); port++) {
7597c478bd9Sstevel@tonic-gate 		total_power += hubpm->hubp_child_pwrstate[port];
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
763c0f24e5bSlg 	    "hubd_can_suspend: %d", total_power);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	return (total_power ? USB_FAILURE : USB_SUCCESS);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate /*
7707c478bd9Sstevel@tonic-gate  * resume port depending on current device state
7717c478bd9Sstevel@tonic-gate  */
7727c478bd9Sstevel@tonic-gate static int
7737c478bd9Sstevel@tonic-gate hubd_resume_port(hubd_t *hubd, usb_port_t port)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	int		rval, retry;
7767c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
7777c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
7787c478bd9Sstevel@tonic-gate 	uint16_t	status;
7797c478bd9Sstevel@tonic-gate 	uint16_t	change;
7807c478bd9Sstevel@tonic-gate 	int		retval = USB_FAILURE;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
7857c478bd9Sstevel@tonic-gate 	    "hubd_resume_port: port=%d state=0x%x (%s)", port,
7867c478bd9Sstevel@tonic-gate 	    hubd->h_dev_state, usb_str_dev_state(hubd->h_dev_state));
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
7897c478bd9Sstevel@tonic-gate 	case USB_DEV_HUB_CHILD_PWRLVL:
7907c478bd9Sstevel@tonic-gate 		/*
7917c478bd9Sstevel@tonic-gate 		 * This could be a bus ctl for a port other than the one
7927c478bd9Sstevel@tonic-gate 		 * that has a remote wakeup condition. So check.
7937c478bd9Sstevel@tonic-gate 		 */
7947c478bd9Sstevel@tonic-gate 		if ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0) {
7957c478bd9Sstevel@tonic-gate 			/* the port isn't suspended, so don't resume */
7967c478bd9Sstevel@tonic-gate 			retval = USB_SUCCESS;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
7997c478bd9Sstevel@tonic-gate 			    "hubd_resume_port: port=%d not suspended", port);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 			break;
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 		/*
8047c478bd9Sstevel@tonic-gate 		 * Device has initiated a wakeup.
8057c478bd9Sstevel@tonic-gate 		 * Issue a ClearFeature(PortSuspend)
8067c478bd9Sstevel@tonic-gate 		 */
8077c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
8087c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
8097c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
81035f36846Ssl 		    HUB_HANDLE_PORT_FEATURE_TYPE,
8117c478bd9Sstevel@tonic-gate 		    USB_REQ_CLEAR_FEATURE,
8127c478bd9Sstevel@tonic-gate 		    CFS_PORT_SUSPEND,
8137c478bd9Sstevel@tonic-gate 		    port,
8147c478bd9Sstevel@tonic-gate 		    0, NULL, 0,
8157c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
8167c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
8177c478bd9Sstevel@tonic-gate 			    "ClearFeature(PortSuspend) fails "
8187c478bd9Sstevel@tonic-gate 			    "rval=%d cr=%d cb=0x%x", rval,
8197c478bd9Sstevel@tonic-gate 			    completion_reason, cb_flags);
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		/* either way ack changes on the port */
8247c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd, port,
825c0f24e5bSlg 		    &status, &change, PORT_CHANGE_PSSC);
8267c478bd9Sstevel@tonic-gate 		retval = USB_SUCCESS;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		break;
8297c478bd9Sstevel@tonic-gate 	case USB_DEV_HUB_STATE_RECOVER:
8307c478bd9Sstevel@tonic-gate 		/*
8317c478bd9Sstevel@tonic-gate 		 * When hubd's connect event callback posts a connect
8327c478bd9Sstevel@tonic-gate 		 * event to its child, it results in this busctl call
8337c478bd9Sstevel@tonic-gate 		 * which is valid
8347c478bd9Sstevel@tonic-gate 		 */
8357c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
8367c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
8376c7181fcSsl 		if (((hubd->h_port_state[port] & PORT_STATUS_CCS) == 0) ||
8386c7181fcSsl 		    ((hubd->h_port_state[port] & PORT_STATUS_PSS) == 0)) {
8397c478bd9Sstevel@tonic-gate 			/*
8407c478bd9Sstevel@tonic-gate 			 * the port isn't suspended, or connected
8417c478bd9Sstevel@tonic-gate 			 * so don't resume
8427c478bd9Sstevel@tonic-gate 			 */
8437c478bd9Sstevel@tonic-gate 			retval = USB_SUCCESS;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
8467c478bd9Sstevel@tonic-gate 			    "hubd_resume_port: port=%d not suspended", port);
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 			break;
8497c478bd9Sstevel@tonic-gate 		}
8507c478bd9Sstevel@tonic-gate 		/*
8517c478bd9Sstevel@tonic-gate 		 * prevent kicking off the hotplug thread
8527c478bd9Sstevel@tonic-gate 		 */
8537c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread++;
8547c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		/* Now ClearFeature(PortSuspend) */
8577c478bd9Sstevel@tonic-gate 		for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
8587c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
8597c478bd9Sstevel@tonic-gate 			rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
8607c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
86135f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
8627c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
8637c478bd9Sstevel@tonic-gate 			    CFS_PORT_SUSPEND,
8647c478bd9Sstevel@tonic-gate 			    port,
8657c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
8667c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0);
8677c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
8687c478bd9Sstevel@tonic-gate 			if (rval != USB_SUCCESS) {
8697c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PM,
8707c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
8717c478bd9Sstevel@tonic-gate 				    "ClearFeature(PortSuspend) fails"
8727c478bd9Sstevel@tonic-gate 				    "rval=%d cr=%d cb=0x%x", rval,
8737c478bd9Sstevel@tonic-gate 				    completion_reason, cb_flags);
8747c478bd9Sstevel@tonic-gate 			} else {
8757c478bd9Sstevel@tonic-gate 				/*
8767c478bd9Sstevel@tonic-gate 				 * As per spec section 11.9 and 7.1.7.7
8777c478bd9Sstevel@tonic-gate 				 * hub need to provide at least 20ms of
8787c478bd9Sstevel@tonic-gate 				 * resume signalling, and s/w provide 10ms of
8797c478bd9Sstevel@tonic-gate 				 * recovery time before accessing the port.
8807c478bd9Sstevel@tonic-gate 				 */
8817c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
8827c478bd9Sstevel@tonic-gate 				delay(drv_usectohz(40000));
8837c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
8847c478bd9Sstevel@tonic-gate 				(void) hubd_determine_port_status(hubd, port,
8857c478bd9Sstevel@tonic-gate 				    &status, &change, PORT_CHANGE_PSSC);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 				if ((status & PORT_STATUS_PSS) == 0) {
8887c478bd9Sstevel@tonic-gate 					/* the port did finally resume */
8897c478bd9Sstevel@tonic-gate 					retval = USB_SUCCESS;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 					break;
8927c478bd9Sstevel@tonic-gate 				}
8937c478bd9Sstevel@tonic-gate 			}
8947c478bd9Sstevel@tonic-gate 		}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		/* allow hotplug thread again */
8977c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread--;
8987c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		break;
9017c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
9027c478bd9Sstevel@tonic-gate 		/* Ignore - NO Operation */
9037c478bd9Sstevel@tonic-gate 		retval = USB_SUCCESS;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 		break;
9067c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
9077c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
9087c478bd9Sstevel@tonic-gate 	default:
9097c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
9107c478bd9Sstevel@tonic-gate 		    "Improper state for port Resume");
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		break;
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	return (retval);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate  * suspend port depending on device state
9227c478bd9Sstevel@tonic-gate  */
9237c478bd9Sstevel@tonic-gate static int
9247c478bd9Sstevel@tonic-gate hubd_suspend_port(hubd_t *hubd, usb_port_t port)
9257c478bd9Sstevel@tonic-gate {
9267c478bd9Sstevel@tonic-gate 	int		rval, retry;
9277c478bd9Sstevel@tonic-gate 	int		retval = USB_FAILURE;
9287c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
9297c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
9307c478bd9Sstevel@tonic-gate 	uint16_t	status;
9317c478bd9Sstevel@tonic-gate 	uint16_t	change;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
9347c478bd9Sstevel@tonic-gate 	    "hubd_suspend_port: port=%d", port);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
9397c478bd9Sstevel@tonic-gate 	case USB_DEV_HUB_STATE_RECOVER:
9407c478bd9Sstevel@tonic-gate 		/*
9417c478bd9Sstevel@tonic-gate 		 * When hubd's connect event callback posts a connect
9427c478bd9Sstevel@tonic-gate 		 * event to its child, it results in this busctl call
9437c478bd9Sstevel@tonic-gate 		 * which is valid
9447c478bd9Sstevel@tonic-gate 		 */
9457c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
9467c478bd9Sstevel@tonic-gate 	case USB_DEV_HUB_CHILD_PWRLVL:
9477c478bd9Sstevel@tonic-gate 		/*
9487c478bd9Sstevel@tonic-gate 		 * When one child is resuming, the other could timeout
9497c478bd9Sstevel@tonic-gate 		 * and go to low power mode, which is valid
9507c478bd9Sstevel@tonic-gate 		 */
9517c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
9527c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
9537c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread++;
9547c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		/*
9577c478bd9Sstevel@tonic-gate 		 * Some devices start an unprovoked resume.  According to spec,
9587c478bd9Sstevel@tonic-gate 		 * normal resume time for port is 10ms.  Wait for double that
9597c478bd9Sstevel@tonic-gate 		 * time, then check to be sure port is really suspended.
9607c478bd9Sstevel@tonic-gate 		 */
9617c478bd9Sstevel@tonic-gate 		for (retry = 0; retry < HUBD_SUS_RES_RETRY; retry++) {
9627c478bd9Sstevel@tonic-gate 			/* Now SetFeature(PortSuspend) */
9637c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
9647c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
9657c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
96635f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
9677c478bd9Sstevel@tonic-gate 			    USB_REQ_SET_FEATURE,
9687c478bd9Sstevel@tonic-gate 			    CFS_PORT_SUSPEND,
9697c478bd9Sstevel@tonic-gate 			    port,
9707c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
9717c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
9727c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
9737c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PM,
9747c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
9757c478bd9Sstevel@tonic-gate 				    "SetFeature(PortSuspend) fails"
9767c478bd9Sstevel@tonic-gate 				    "rval=%d cr=%d cb=0x%x",
9777c478bd9Sstevel@tonic-gate 				    rval, completion_reason, cb_flags);
9787c478bd9Sstevel@tonic-gate 			}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 			/*
9817c478bd9Sstevel@tonic-gate 			 * some devices start an unprovoked resume
9827c478bd9Sstevel@tonic-gate 			 * wait and check port status after some time
9837c478bd9Sstevel@tonic-gate 			 */
9847c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(20000));
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 			/* either ways ack changes on the port */
9877c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
9887c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
9897c478bd9Sstevel@tonic-gate 			    &status, &change, PORT_CHANGE_PSSC);
9907c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_PSS) {
9917c478bd9Sstevel@tonic-gate 				/* the port is indeed suspended */
9927c478bd9Sstevel@tonic-gate 				retval = USB_SUCCESS;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 				break;
9957c478bd9Sstevel@tonic-gate 			}
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread--;
9997c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		break;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
10047c478bd9Sstevel@tonic-gate 		/* Ignore - No Operation */
10057c478bd9Sstevel@tonic-gate 		retval = USB_SUCCESS;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 		break;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
10107c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
10117c478bd9Sstevel@tonic-gate 	default:
10127c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
10137c478bd9Sstevel@tonic-gate 		    "Improper state for port Suspend");
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		break;
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	return (retval);
10207c478bd9Sstevel@tonic-gate }
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate /*
10247c478bd9Sstevel@tonic-gate  * child post attach/detach notifications
10257c478bd9Sstevel@tonic-gate  */
10267c478bd9Sstevel@tonic-gate static void
10277c478bd9Sstevel@tonic-gate hubd_post_attach(hubd_t *hubd, usb_port_t port, struct attachspec *as)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
10327c478bd9Sstevel@tonic-gate 	    "hubd_post_attach: port=%d result=%d",
10337c478bd9Sstevel@tonic-gate 	    port, as->result);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	if (as->result == DDI_SUCCESS) {
10367c478bd9Sstevel@tonic-gate 		/*
10377c478bd9Sstevel@tonic-gate 		 * Check if the child created wants to be power managed.
10387c478bd9Sstevel@tonic-gate 		 * If yes, the childs power level gets automatically tracked
10397c478bd9Sstevel@tonic-gate 		 * by DDI_CTLOPS_POWER busctl.
10407c478bd9Sstevel@tonic-gate 		 * If no, we set power of the new child by default
10417c478bd9Sstevel@tonic-gate 		 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
10427c478bd9Sstevel@tonic-gate 		 */
10437c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
10447c478bd9Sstevel@tonic-gate 		dip = hubd->h_children_dips[port];
10457c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10467c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_pm_info == NULL) {
10477c478bd9Sstevel@tonic-gate 			hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_FULL_PWR);
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate static void
10547c478bd9Sstevel@tonic-gate hubd_post_detach(hubd_t *hubd, usb_port_t port, struct detachspec *ds)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
10577c478bd9Sstevel@tonic-gate 	    "hubd_post_detach: port=%d result=%d", port, ds->result);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
10607c478bd9Sstevel@tonic-gate 	 * if the device is successfully detached and is the
10617c478bd9Sstevel@tonic-gate 	 * last device to detach, mark component as idle
10627c478bd9Sstevel@tonic-gate 	 */
10637c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
10647c478bd9Sstevel@tonic-gate 	if (ds->result == DDI_SUCCESS) {
10657c478bd9Sstevel@tonic-gate 		usba_device_t	*usba_device = hubd->h_usba_devices[port];
106635f36846Ssl 		dev_info_t	*pdip = hubd->h_dip;
10677c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10687c478bd9Sstevel@tonic-gate 
106935f36846Ssl 		usba_hubdi_incr_power_budget(pdip, usba_device);
107035f36846Ssl 
10717c478bd9Sstevel@tonic-gate 		/*
10727c478bd9Sstevel@tonic-gate 		 * We set power of the detached child
10737c478bd9Sstevel@tonic-gate 		 * to 0, so that we can suspend if all
10747c478bd9Sstevel@tonic-gate 		 * our children are gone
10757c478bd9Sstevel@tonic-gate 		 */
10767c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_PWR_OFF);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		/* check for leaks on detaching */
10797c478bd9Sstevel@tonic-gate 		if ((usba_device) && (ds->cmd == DDI_DETACH)) {
10807c478bd9Sstevel@tonic-gate 			usba_check_for_leaks(usba_device);
10817c478bd9Sstevel@tonic-gate 		}
10827c478bd9Sstevel@tonic-gate 	} else {
10837c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /*
10897c478bd9Sstevel@tonic-gate  * hubd_post_power
10907c478bd9Sstevel@tonic-gate  *	After the child's power entry point has been called
10917c478bd9Sstevel@tonic-gate  *	we record its power level in our local struct.
10927c478bd9Sstevel@tonic-gate  *	If the device has powered off, we suspend port
10937c478bd9Sstevel@tonic-gate  */
10947c478bd9Sstevel@tonic-gate static int
10957c478bd9Sstevel@tonic-gate hubd_post_power(hubd_t *hubd, usb_port_t port, pm_bp_child_pwrchg_t *bpc,
10967c478bd9Sstevel@tonic-gate     int result)
10977c478bd9Sstevel@tonic-gate {
10987c478bd9Sstevel@tonic-gate 	int	retval = USB_SUCCESS;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
11017c478bd9Sstevel@tonic-gate 	    "hubd_post_power: port=%d", port);
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	if (result == DDI_SUCCESS) {
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		/* record this power in our local struct */
11067c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, bpc->bpc_nlevel);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel == USB_DEV_OS_PWR_OFF) {
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 			/* now suspend the port */
11117c478bd9Sstevel@tonic-gate 			retval = hubd_suspend_port(hubd, port);
11127c478bd9Sstevel@tonic-gate 		} else if (bpc->bpc_nlevel == USB_DEV_OS_FULL_PWR) {
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 			/* make sure the port is resumed */
11157c478bd9Sstevel@tonic-gate 			retval = hubd_resume_port(hubd, port);
11167c478bd9Sstevel@tonic-gate 		}
11177c478bd9Sstevel@tonic-gate 	} else {
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 		/* record old power in our local struct */
11207c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, bpc->bpc_olevel);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 		if (bpc->bpc_olevel == USB_DEV_OS_PWR_OFF) {
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 			/*
11257c478bd9Sstevel@tonic-gate 			 * As this device failed to transition from
11267c478bd9Sstevel@tonic-gate 			 * power off state, suspend the port again
11277c478bd9Sstevel@tonic-gate 			 */
11287c478bd9Sstevel@tonic-gate 			retval = hubd_suspend_port(hubd, port);
11297c478bd9Sstevel@tonic-gate 		}
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	return (retval);
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate /*
11377c478bd9Sstevel@tonic-gate  * bus ctl notifications are handled here, the rest goes up to root hub/hcd
11387c478bd9Sstevel@tonic-gate  */
11397c478bd9Sstevel@tonic-gate static int
11407c478bd9Sstevel@tonic-gate usba_hubdi_bus_ctl(dev_info_t *dip,
11417c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
11427c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t	op,
11437c478bd9Sstevel@tonic-gate 	void		*arg,
11447c478bd9Sstevel@tonic-gate 	void		*result)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
11477c478bd9Sstevel@tonic-gate 	dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
11487c478bd9Sstevel@tonic-gate 	struct attachspec *as;
11497c478bd9Sstevel@tonic-gate 	struct detachspec *ds;
11507c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
11517c478bd9Sstevel@tonic-gate 	usb_port_t	port;
11527c478bd9Sstevel@tonic-gate 	int		circ, rval;
11537c478bd9Sstevel@tonic-gate 	int		retval = DDI_FAILURE;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	/* flag that we are currently running bus_ctl */
11607c478bd9Sstevel@tonic-gate 	hubd->h_bus_ctls++;
11617c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
11647c478bd9Sstevel@tonic-gate 	    "usba_hubdi_bus_ctl:\n\t"
11657c478bd9Sstevel@tonic-gate 	    "dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p",
1166112116d8Sfb 	    (void *)dip, (void *)rdip, op, arg);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	switch (op) {
11697c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
11707c478bd9Sstevel@tonic-gate 		as = (struct attachspec *)arg;
11717c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, rdip);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		/* there is nothing to do at resume time */
11747c478bd9Sstevel@tonic-gate 		if (as->cmd == DDI_RESUME) {
11757c478bd9Sstevel@tonic-gate 			break;
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		/* serialize access */
11797c478bd9Sstevel@tonic-gate 		ndi_devi_enter(hubd->h_dip, &circ);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		switch (as->when) {
11827c478bd9Sstevel@tonic-gate 		case DDI_PRE:
11837c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
11847c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1185112116d8Sfb 			    (void *)rdip, port);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
11887c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] |= HUBD_CHILD_ATTACHING;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 			/* Go busy here.  Matching idle is DDI_POST case. */
11917c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, dip, 0);
11927c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 			/*
11957c478bd9Sstevel@tonic-gate 			 * if we suspended the port previously
11967c478bd9Sstevel@tonic-gate 			 * because child went to low power state, and
11977c478bd9Sstevel@tonic-gate 			 * someone unloaded the driver, the port would
11987c478bd9Sstevel@tonic-gate 			 * still be suspended and needs to be resumed
11997c478bd9Sstevel@tonic-gate 			 */
12007c478bd9Sstevel@tonic-gate 			rval = hubd_resume_port(hubd, port);
12017c478bd9Sstevel@tonic-gate 			if (rval == USB_SUCCESS) {
12027c478bd9Sstevel@tonic-gate 				retval = DDI_SUCCESS;
12037c478bd9Sstevel@tonic-gate 			}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 			break;
12067c478bd9Sstevel@tonic-gate 		case DDI_POST:
12077c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12087c478bd9Sstevel@tonic-gate 			    "DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1209112116d8Sfb 			    (void *)rdip, port);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12127c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_ATTACHING;
12137c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 			hubd_post_attach(hubd, port, (struct attachspec *)arg);
12167c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12177c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 			/* Matching idle call for DDI_PRE busy call. */
12207c478bd9Sstevel@tonic-gate 			(void) hubd_pm_idle_component(hubd, dip, 0);
12217c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 		ndi_devi_exit(hubd->h_dip, circ);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		break;
12267c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
12277c478bd9Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
12287c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, rdip);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 		/* there is nothing to do at suspend time */
12317c478bd9Sstevel@tonic-gate 		if (ds->cmd == DDI_SUSPEND) {
12327c478bd9Sstevel@tonic-gate 			break;
12337c478bd9Sstevel@tonic-gate 		}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 		/* serialize access */
12367c478bd9Sstevel@tonic-gate 		ndi_devi_enter(hubd->h_dip, &circ);
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		switch (ds->when) {
12397c478bd9Sstevel@tonic-gate 		case DDI_PRE:
12407c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12417c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d",
1242112116d8Sfb 			    (void *)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 
12477c478bd9Sstevel@tonic-gate 			/* Go busy here.  Matching idle is DDI_POST case. */
12487c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, dip, 0);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12517c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 			break;
12547c478bd9Sstevel@tonic-gate 		case DDI_POST:
12557c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12567c478bd9Sstevel@tonic-gate 			    "DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d",
1257112116d8Sfb 			    (void *)rdip, port);
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12607c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_DETACHING;
12617c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 			/* Matching idle call for DDI_PRE busy call. */
12647c478bd9Sstevel@tonic-gate 			hubd_post_detach(hubd, port, (struct detachspec *)arg);
12657c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12667c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12677c478bd9Sstevel@tonic-gate 			(void) hubd_pm_idle_component(hubd, dip, 0);
12687c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 			break;
12717c478bd9Sstevel@tonic-gate 		}
12727c478bd9Sstevel@tonic-gate 		ndi_devi_exit(hubd->h_dip, circ);
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 		break;
12757c478bd9Sstevel@tonic-gate 	default:
12767c478bd9Sstevel@tonic-gate 		retval = usba_bus_ctl(root_hub_dip, rdip, op, arg, result);
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	/* decrement bus_ctls count */
12807c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
12817c478bd9Sstevel@tonic-gate 	hubd->h_bus_ctls--;
12827c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_bus_ctls >= 0);
12837c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	return (retval);
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate 
128840482326SVincent Wang /*
128940482326SVincent Wang  * hubd_config_one:
129040482326SVincent Wang  * 	enumerate one child according to 'port'
129140482326SVincent Wang  */
129240482326SVincent Wang 
129340482326SVincent Wang static boolean_t
129440482326SVincent Wang hubd_config_one(hubd_t *hubd, int port)
129540482326SVincent Wang {
129640482326SVincent Wang 	uint16_t	status, change;
129740482326SVincent Wang 	dev_info_t	*hdip = hubd->h_dip;
129840482326SVincent Wang 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
129940482326SVincent Wang 	boolean_t	online_child = B_FALSE, found = B_FALSE;
130040482326SVincent Wang 	int		prh_circ, rh_circ, circ;
130140482326SVincent Wang 
130240482326SVincent Wang 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
130340482326SVincent Wang 	    "hubd_config_one:  started, hubd_reset_port = 0x%x", port);
130440482326SVincent Wang 
130540482326SVincent Wang 	ndi_hold_devi(hdip); /* so we don't race with detach */
130640482326SVincent Wang 
130740482326SVincent Wang 	/*
130840482326SVincent Wang 	 * this ensures one config activity per system at a time.
130940482326SVincent Wang 	 * we enter the parent PCI node to have this serialization.
131040482326SVincent Wang 	 * this also excludes ioctls and deathrow thread
131140482326SVincent Wang 	 */
131240482326SVincent Wang 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
131340482326SVincent Wang 	ndi_devi_enter(rh_dip, &rh_circ);
131440482326SVincent Wang 
131540482326SVincent Wang 	/* exclude other threads */
131640482326SVincent Wang 	ndi_devi_enter(hdip, &circ);
131740482326SVincent Wang 	mutex_enter(HUBD_MUTEX(hubd));
131840482326SVincent Wang 
1319e4c316c4SVincent Wang 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
1320e4c316c4SVincent Wang 
132140482326SVincent Wang 	if (!hubd->h_children_dips[port]) {
132240482326SVincent Wang 
132340482326SVincent Wang 		(void) hubd_determine_port_status(hubd, port,
132440482326SVincent Wang 		    &status, &change, HUBD_ACK_ALL_CHANGES);
132540482326SVincent Wang 
132640482326SVincent Wang 		if (status & PORT_STATUS_CCS) {
132740482326SVincent Wang 			online_child |=	(hubd_handle_port_connect(hubd,
132840482326SVincent Wang 			    port) == USB_SUCCESS);
132940482326SVincent Wang 			found = online_child;
133040482326SVincent Wang 		}
133140482326SVincent Wang 	} else {
133240482326SVincent Wang 		found = B_TRUE;
133340482326SVincent Wang 	}
133440482326SVincent Wang 
133540482326SVincent Wang 	mutex_exit(HUBD_MUTEX(hubd));
133640482326SVincent Wang 
133740482326SVincent Wang 	ndi_devi_exit(hdip, circ);
133840482326SVincent Wang 	ndi_devi_exit(rh_dip, rh_circ);
133940482326SVincent Wang 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
134040482326SVincent Wang 
134140482326SVincent Wang 	if (online_child) {
134240482326SVincent Wang 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
134340482326SVincent Wang 		    "hubd_config_one: onlining child");
134440482326SVincent Wang 
134540482326SVincent Wang 		(void) ndi_devi_online(hubd->h_dip, 0);
134640482326SVincent Wang 	}
134740482326SVincent Wang 
134840482326SVincent Wang 	mutex_enter(HUBD_MUTEX(hubd));
134940482326SVincent Wang 
135040482326SVincent Wang 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
135140482326SVincent Wang 
135240482326SVincent Wang 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
135340482326SVincent Wang 	    "hubd_config_one: exit");
135440482326SVincent Wang 
135540482326SVincent Wang 	mutex_exit(HUBD_MUTEX(hubd));
135640482326SVincent Wang 
135740482326SVincent Wang 	ndi_rele_devi(hdip);
135840482326SVincent Wang 
135940482326SVincent Wang 	return (found);
136040482326SVincent Wang }
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate /*
13637c478bd9Sstevel@tonic-gate  * bus enumeration entry points
13647c478bd9Sstevel@tonic-gate  */
13657c478bd9Sstevel@tonic-gate static int
13667c478bd9Sstevel@tonic-gate hubd_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
13677c478bd9Sstevel@tonic-gate     void *arg, dev_info_t **child)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = hubd_get_soft_state(dip);
13707c478bd9Sstevel@tonic-gate 	int	rval, circ;
137140482326SVincent Wang 	long port;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
13747c478bd9Sstevel@tonic-gate 	    "hubd_bus_config: op=%d", op);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	if (hubdi_bus_config_debug) {
13777c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 
138040482326SVincent Wang 	if (op == BUS_CONFIG_ONE) {
138140482326SVincent Wang 		boolean_t found;
1382d724deadSguoqing zhu - Sun Microsystems - Beijing China 		char cname[80];
1383d724deadSguoqing zhu - Sun Microsystems - Beijing China 		char *name, *addr;
1384d724deadSguoqing zhu - Sun Microsystems - Beijing China 
1385d724deadSguoqing zhu - Sun Microsystems - Beijing China 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
1386d724deadSguoqing zhu - Sun Microsystems - Beijing China 		    "hubd_bus_config: op=%d (BUS_CONFIG_ONE)", op);
1387d724deadSguoqing zhu - Sun Microsystems - Beijing China 
138840482326SVincent Wang 		(void) snprintf(cname, 80, "%s", (char *)arg);
138940482326SVincent Wang 		/* split name into "name@addr" parts */
139040482326SVincent Wang 		i_ddi_parse_name(cname, &name, &addr, NULL);
139140482326SVincent Wang 		if (addr && *addr) {
139240482326SVincent Wang 			(void) ddi_strtol(addr, NULL, 16, &port);
139340482326SVincent Wang 		} else {
139440482326SVincent Wang 			return (NDI_FAILURE);
139540482326SVincent Wang 		}
1396d724deadSguoqing zhu - Sun Microsystems - Beijing China 
139740482326SVincent Wang 		found = hubd_config_one(hubd, port);
13985321cfb7Spengcheng chen - Sun Microsystems - Beijing China 
1399d724deadSguoqing zhu - Sun Microsystems - Beijing China 		if (found == 0) {
1400d724deadSguoqing zhu - Sun Microsystems - Beijing China 			return (NDI_FAILURE);
1401d724deadSguoqing zhu - Sun Microsystems - Beijing China 		}
1402d724deadSguoqing zhu - Sun Microsystems - Beijing China 
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 	ndi_devi_enter(hubd->h_dip, &circ);
14057c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
14067c478bd9Sstevel@tonic-gate 	ndi_devi_exit(hubd->h_dip, circ);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	return (rval);
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate static int
14137c478bd9Sstevel@tonic-gate hubd_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
14147c478bd9Sstevel@tonic-gate     void *arg)
14157c478bd9Sstevel@tonic-gate {
14167c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = hubd_get_soft_state(dip);
14177c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
14187c478bd9Sstevel@tonic-gate 	usb_port_t	port;
14197c478bd9Sstevel@tonic-gate 	int		circ;
14207c478bd9Sstevel@tonic-gate 	int		rval;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
14237c478bd9Sstevel@tonic-gate 	    "hubd_bus_unconfig: op=%d", op);
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	if (hubdi_bus_config_debug) {
14267c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	if ((op == BUS_UNCONFIG_ALL) && (flag & NDI_AUTODETACH) == 0) {
14307c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_REMOVE;
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	/* serialize access */
14347c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	/* logically zap children's list */
14397c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
14407c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
14417c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= HUBD_CHILD_ZAP;
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	/* fill in what's left */
14467c478bd9Sstevel@tonic-gate 	for (cdip = ddi_get_child(dip); cdip;
14477c478bd9Sstevel@tonic-gate 	    cdip = ddi_get_next_sibling(cdip)) {
14487c478bd9Sstevel@tonic-gate 		usba_device_t *usba_device = usba_get_usba_device(cdip);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 		if (usba_device == NULL) {
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 			continue;
14537c478bd9Sstevel@tonic-gate 		}
14547c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
14557c478bd9Sstevel@tonic-gate 		port = usba_device->usb_port;
14567c478bd9Sstevel@tonic-gate 		hubd->h_children_dips[port] = cdip;
14577c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
14587c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	/* physically zap the children we didn't find */
14627c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
14637c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
1464112cd14aSqz 		if (hubd->h_port_state[port] &	HUBD_CHILD_ZAP) {
14657c478bd9Sstevel@tonic-gate 			/* zap the dip and usba_device structure as well */
14667c478bd9Sstevel@tonic-gate 			hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
14677c478bd9Sstevel@tonic-gate 			hubd->h_children_dips[port] = NULL;
14687c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
14697c478bd9Sstevel@tonic-gate 		}
14707c478bd9Sstevel@tonic-gate 	}
14717c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
14767c478bd9Sstevel@tonic-gate 	    "hubd_bus_unconfig: rval=%d", rval);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	return (rval);
14797c478bd9Sstevel@tonic-gate }
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate /* bus_power entry point */
14837c478bd9Sstevel@tonic-gate static int
14847c478bd9Sstevel@tonic-gate hubd_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
14857c478bd9Sstevel@tonic-gate     void *arg, void *result)
14867c478bd9Sstevel@tonic-gate {
14877c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
14887c478bd9Sstevel@tonic-gate 	int		rval, pwrup_res;
14897c478bd9Sstevel@tonic-gate 	usb_port_t	port;
14907c478bd9Sstevel@tonic-gate 	int		retval = DDI_FAILURE;
14917c478bd9Sstevel@tonic-gate 	pm_bp_child_pwrchg_t	*bpc;
14927c478bd9Sstevel@tonic-gate 	pm_bp_nexus_pwrup_t	bpn;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubd->h_log_handle,
14977c478bd9Sstevel@tonic-gate 	    "hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, "
1498112116d8Sfb 	    "result=%d\n", (void *)dip, impl_arg, op, arg, *(int *)result);
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	bpc = (pm_bp_child_pwrchg_t *)arg;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
15037c478bd9Sstevel@tonic-gate 	hubd->h_bus_pwr++;
15047c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	switch (op) {
15077c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
15087c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, bpc->bpc_dip);
15097c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
15107c478bd9Sstevel@tonic-gate 		    "hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d",
15117c478bd9Sstevel@tonic-gate 		    port);
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 		/* go to full power if we are powered down */
15147c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 		/*
15177c478bd9Sstevel@tonic-gate 		 * If this case completes normally, idle will be in
15187c478bd9Sstevel@tonic-gate 		 * hubd_bus_power / BUS_POWER_POST_NOTIFICATION
15197c478bd9Sstevel@tonic-gate 		 */
15207c478bd9Sstevel@tonic-gate 		hubd_pm_busy_component(hubd, dip, 0);
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 		/*
15237c478bd9Sstevel@tonic-gate 		 * raise power only if we have created the components
15247c478bd9Sstevel@tonic-gate 		 * and are currently in low power
15257c478bd9Sstevel@tonic-gate 		 */
15267c478bd9Sstevel@tonic-gate 		if ((hubd->h_dev_state == USB_DEV_PWRED_DOWN) &&
15277c478bd9Sstevel@tonic-gate 		    hubd->h_hubpm->hubp_wakeup_enabled) {
15287c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 			bpn.bpn_comp = 0;
15317c478bd9Sstevel@tonic-gate 			bpn.bpn_dip = dip;
15327c478bd9Sstevel@tonic-gate 			bpn.bpn_level = USB_DEV_OS_FULL_PWR;
15337c478bd9Sstevel@tonic-gate 			bpn.bpn_private = bpc->bpc_private;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 			rval = pm_busop_bus_power(dip, impl_arg,
15367c478bd9Sstevel@tonic-gate 			    BUS_POWER_NEXUS_PWRUP, (void *)&bpn,
15377c478bd9Sstevel@tonic-gate 			    (void *)&pwrup_res);
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 			if (rval != DDI_SUCCESS || pwrup_res != DDI_SUCCESS) {
15407c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
15417c478bd9Sstevel@tonic-gate 				hubd_pm_idle_component(hubd, dip, 0);
15427c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 				break;
15457c478bd9Sstevel@tonic-gate 			}
15467c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 		/* indicate that child is changing power level */
15507c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= HUBD_CHILD_PWRLVL_CHNG;
15517c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		if ((bpc->bpc_olevel == 0) &&
15547c478bd9Sstevel@tonic-gate 		    (bpc->bpc_nlevel > bpc->bpc_olevel)) {
15557c478bd9Sstevel@tonic-gate 			/*
15567c478bd9Sstevel@tonic-gate 			 * this child is transitioning from power off
15577c478bd9Sstevel@tonic-gate 			 * to power on state - resume port
15587c478bd9Sstevel@tonic-gate 			 */
15597c478bd9Sstevel@tonic-gate 			rval = hubd_resume_port(hubd, port);
15607c478bd9Sstevel@tonic-gate 			if (rval == USB_SUCCESS) {
15617c478bd9Sstevel@tonic-gate 				retval = DDI_SUCCESS;
15627c478bd9Sstevel@tonic-gate 			} else {
15637c478bd9Sstevel@tonic-gate 				/* reset this flag on failure */
15647c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
15657c478bd9Sstevel@tonic-gate 				hubd->h_port_state[port] &=
15667c478bd9Sstevel@tonic-gate 				    ~HUBD_CHILD_PWRLVL_CHNG;
15677c478bd9Sstevel@tonic-gate 				hubd_pm_idle_component(hubd, dip, 0);
15687c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
15697c478bd9Sstevel@tonic-gate 			}
15707c478bd9Sstevel@tonic-gate 		} else {
15717c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
15727c478bd9Sstevel@tonic-gate 		}
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 		break;
15757c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
15767c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, bpc->bpc_dip);
15777c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
15787c478bd9Sstevel@tonic-gate 		    "hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d",
15797c478bd9Sstevel@tonic-gate 		    port);
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
15827c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~HUBD_CHILD_PWRLVL_CHNG;
15837c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 		/* record child's pwr and suspend port if required */
15867c478bd9Sstevel@tonic-gate 		rval = hubd_post_power(hubd, port, bpc, *(int *)result);
15877c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
15907c478bd9Sstevel@tonic-gate 		}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 		/*
15957c478bd9Sstevel@tonic-gate 		 * Matching idle for the busy in
15967c478bd9Sstevel@tonic-gate 		 * hubd_bus_power / BUS_POWER_PRE_NOTIFICATION
15977c478bd9Sstevel@tonic-gate 		 */
15987c478bd9Sstevel@tonic-gate 		hubd_pm_idle_component(hubd, dip, 0);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 		break;
16037c478bd9Sstevel@tonic-gate 	default:
16047c478bd9Sstevel@tonic-gate 		retval = pm_busop_bus_power(dip, impl_arg, op, arg, result);
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 		break;
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
16107c478bd9Sstevel@tonic-gate 	hubd->h_bus_pwr--;
16117c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	return (retval);
16147c478bd9Sstevel@tonic-gate }
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate /*
16187c478bd9Sstevel@tonic-gate  * functions to handle power transition for OS levels 0 -> 3
16197c478bd9Sstevel@tonic-gate  */
16207c478bd9Sstevel@tonic-gate static int
16217c478bd9Sstevel@tonic-gate hubd_pwrlvl0(hubd_t *hubd)
16227c478bd9Sstevel@tonic-gate {
16237c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	/* We can't power down if hotplug thread is running */
16267c478bd9Sstevel@tonic-gate 	if (hubd->h_hotplug_thread || hubd->h_hubpm->hubp_busy_pm ||
16277c478bd9Sstevel@tonic-gate 	    (hubd_can_suspend(hubd) == USB_FAILURE)) {
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
16337c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
16347c478bd9Sstevel@tonic-gate 		hubpm = hubd->h_hubpm;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 		/*
16377c478bd9Sstevel@tonic-gate 		 * To avoid race with bus_power pre_notify on check over
16387c478bd9Sstevel@tonic-gate 		 * dev_state, we need to correctly set the dev state
16397c478bd9Sstevel@tonic-gate 		 * before the mutex is dropped in stop polling.
16407c478bd9Sstevel@tonic-gate 		 */
16417c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_PWRED_DOWN;
16427c478bd9Sstevel@tonic-gate 		hubpm->hubp_current_power = USB_DEV_OS_PWR_OFF;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 		/*
16457c478bd9Sstevel@tonic-gate 		 * if we are the root hub, do not stop polling
16467c478bd9Sstevel@tonic-gate 		 * otherwise, we will never see a resume
16477c478bd9Sstevel@tonic-gate 		 */
16487c478bd9Sstevel@tonic-gate 		if (usba_is_root_hub(hubd->h_dip)) {
16497c478bd9Sstevel@tonic-gate 			/* place holder to implement Global Suspend */
16507c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
16517c478bd9Sstevel@tonic-gate 			    "Global Suspend: Not Yet Implemented");
16527c478bd9Sstevel@tonic-gate 		} else {
16537c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
16547c478bd9Sstevel@tonic-gate 		}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 		/* Issue USB D3 command to the device here */
16577c478bd9Sstevel@tonic-gate 		(void) usb_set_device_pwrlvl3(hubd->h_dip);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 		break;
16607c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
16617c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
16627c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
16637c478bd9Sstevel@tonic-gate 	default:
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 		break;
16667c478bd9Sstevel@tonic-gate 	}
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
16697c478bd9Sstevel@tonic-gate }
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate /* ARGSUSED */
16737c478bd9Sstevel@tonic-gate static int
16747c478bd9Sstevel@tonic-gate hubd_pwrlvl1(hubd_t *hubd)
16757c478bd9Sstevel@tonic-gate {
16767c478bd9Sstevel@tonic-gate 	/* Issue USB D2 command to the device here */
16777c478bd9Sstevel@tonic-gate 	(void) usb_set_device_pwrlvl2(hubd->h_dip);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
16807c478bd9Sstevel@tonic-gate }
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate /* ARGSUSED */
16847c478bd9Sstevel@tonic-gate static int
16857c478bd9Sstevel@tonic-gate hubd_pwrlvl2(hubd_t *hubd)
16867c478bd9Sstevel@tonic-gate {
16877c478bd9Sstevel@tonic-gate 	/* Issue USB D1 command to the device here */
16887c478bd9Sstevel@tonic-gate 	(void) usb_set_device_pwrlvl1(hubd->h_dip);
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate static int
16957c478bd9Sstevel@tonic-gate hubd_pwrlvl3(hubd_t *hubd)
16967c478bd9Sstevel@tonic-gate {
16977c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
16987c478bd9Sstevel@tonic-gate 	int		rval;
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, "hubd_pwrlvl3");
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
17037c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
17047c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
17057c478bd9Sstevel@tonic-gate 		ASSERT(hubpm->hubp_current_power == USB_DEV_OS_PWR_OFF);
17067c478bd9Sstevel@tonic-gate 		if (usba_is_root_hub(hubd->h_dip)) {
17077c478bd9Sstevel@tonic-gate 			/* implement global resume here */
17087c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM,
17097c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
17107c478bd9Sstevel@tonic-gate 			    "Global Resume: Not Yet Implemented");
17117c478bd9Sstevel@tonic-gate 		}
17127c478bd9Sstevel@tonic-gate 		/* Issue USB D0 command to the device here */
17137c478bd9Sstevel@tonic-gate 		rval = usb_set_device_pwrlvl0(hubd->h_dip);
17147c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
17157c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_ONLINE;
17167c478bd9Sstevel@tonic-gate 		hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
17177c478bd9Sstevel@tonic-gate 		hubpm->hubp_time_at_full_power = ddi_get_time();
17187c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
17217c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
17227c478bd9Sstevel@tonic-gate 		/* we are already in full power */
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
17257c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
17267c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
17277c478bd9Sstevel@tonic-gate 		/*
17287c478bd9Sstevel@tonic-gate 		 * PM framework tries to put you in full power
17297c478bd9Sstevel@tonic-gate 		 * during system shutdown. If we are disconnected
17307c478bd9Sstevel@tonic-gate 		 * return success. Also, we should not change state
17317c478bd9Sstevel@tonic-gate 		 * when we are disconnected or suspended or about to
17327c478bd9Sstevel@tonic-gate 		 * transition to that state
17337c478bd9Sstevel@tonic-gate 		 */
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
17367c478bd9Sstevel@tonic-gate 	default:
17377c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
17387c478bd9Sstevel@tonic-gate 		    "hubd_pwrlvl3: Illegal dev_state=%d", hubd->h_dev_state);
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
17417c478bd9Sstevel@tonic-gate 	}
17427c478bd9Sstevel@tonic-gate }
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate /* power entry point */
17467c478bd9Sstevel@tonic-gate /* ARGSUSED */
17477c478bd9Sstevel@tonic-gate int
17487c478bd9Sstevel@tonic-gate usba_hubdi_power(dev_info_t *dip, int comp, int level)
17497c478bd9Sstevel@tonic-gate {
17507c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
17517c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
17527c478bd9Sstevel@tonic-gate 	int		retval;
17537c478bd9Sstevel@tonic-gate 	int		circ;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
17567c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
17577c478bd9Sstevel@tonic-gate 	    "usba_hubdi_power: level=%d", level);
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
17627c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	/* check if we are transitioning to a legal power level */
17657c478bd9Sstevel@tonic-gate 	if (USB_DEV_PWRSTATE_OK(hubpm->hubp_pwr_states, level)) {
17667c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HUBDI, hubd->h_log_handle,
17677c478bd9Sstevel@tonic-gate 		    "usba_hubdi_power: illegal power level=%d "
17687c478bd9Sstevel@tonic-gate 		    "hubp_pwr_states=0x%x", level, hubpm->hubp_pwr_states);
17697c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 		ndi_devi_exit(dip, circ);
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
17747c478bd9Sstevel@tonic-gate 	}
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	switch (level) {
17777c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_OFF:
17787c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl0(hubd);
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 		break;
17817c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_1:
17827c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl1(hubd);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 		break;
17857c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_2:
17867c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl2(hubd);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 		break;
17897c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_FULL_PWR:
17907c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl3(hubd);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 		break;
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
17997c478bd9Sstevel@tonic-gate }
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate /* power entry point for the root hub */
18037c478bd9Sstevel@tonic-gate int
18047c478bd9Sstevel@tonic-gate usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate 	return (usba_hubdi_power(dip, comp, level));
18077c478bd9Sstevel@tonic-gate }
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate /*
18117c478bd9Sstevel@tonic-gate  * standard driver entry points support code
18127c478bd9Sstevel@tonic-gate  */
18137c478bd9Sstevel@tonic-gate int
18147c478bd9Sstevel@tonic-gate usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
18157c478bd9Sstevel@tonic-gate {
18167c478bd9Sstevel@tonic-gate 	int			instance = ddi_get_instance(dip);
18177c478bd9Sstevel@tonic-gate 	hubd_t			*hubd = NULL;
18187c478bd9Sstevel@tonic-gate 	int			i, rval;
18197c478bd9Sstevel@tonic-gate 	int			minor;
1820112cd14aSqz 	uint8_t			ports_count;
18217c478bd9Sstevel@tonic-gate 	char			*log_name = NULL;
18227c478bd9Sstevel@tonic-gate 	const char		*root_hub_drvname;
18237c478bd9Sstevel@tonic-gate 	usb_ep_data_t		*ep_data;
18247c478bd9Sstevel@tonic-gate 	usba_device_t		*child_ud = NULL;
18257c478bd9Sstevel@tonic-gate 	usb_dev_descr_t		*usb_dev_descr;
18267c478bd9Sstevel@tonic-gate 	usb_port_status_t	parent_port_status, child_port_status;
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle,
1829c0f24e5bSlg 	    "hubd_attach instance %d, cmd=0x%x", instance, cmd);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	switch (cmd) {
18327c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 		break;
18357c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
18367c478bd9Sstevel@tonic-gate 		hubd_cpr_resume(dip);
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
18397c478bd9Sstevel@tonic-gate 	default:
18407c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	/*
18447c478bd9Sstevel@tonic-gate 	 * Allocate softc information.
18457c478bd9Sstevel@tonic-gate 	 */
18467c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
18477c478bd9Sstevel@tonic-gate 		/* soft state has already been allocated */
18487c478bd9Sstevel@tonic-gate 		hubd = hubd_get_soft_state(dip);
18497c478bd9Sstevel@tonic-gate 		minor = HUBD_IS_ROOT_HUB;
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 		/* generate readable labels for different root hubs */
18527c478bd9Sstevel@tonic-gate 		root_hub_drvname = ddi_driver_name(dip);
18537c478bd9Sstevel@tonic-gate 		if (strcmp(root_hub_drvname, "ehci") == 0) {
18547c478bd9Sstevel@tonic-gate 			log_name = "eusb";
18557c478bd9Sstevel@tonic-gate 		} else if (strcmp(root_hub_drvname, "uhci") == 0) {
18567c478bd9Sstevel@tonic-gate 			log_name = "uusb";
18577c478bd9Sstevel@tonic-gate 		} else {
18587c478bd9Sstevel@tonic-gate 			/* std. for ohci */
18597c478bd9Sstevel@tonic-gate 			log_name = "usb";
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate 	} else {
18627c478bd9Sstevel@tonic-gate 		rval = ddi_soft_state_zalloc(hubd_statep, instance);
18637c478bd9Sstevel@tonic-gate 		minor = 0;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 		if (rval != DDI_SUCCESS) {
18667c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
18677c478bd9Sstevel@tonic-gate 			    "cannot allocate soft state (%d)", instance);
18687c478bd9Sstevel@tonic-gate 			goto fail;
18697c478bd9Sstevel@tonic-gate 		}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 		hubd = hubd_get_soft_state(dip);
18727c478bd9Sstevel@tonic-gate 		if (hubd == NULL) {
18737c478bd9Sstevel@tonic-gate 			goto fail;
18747c478bd9Sstevel@tonic-gate 		}
18757c478bd9Sstevel@tonic-gate 	}
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel,
1878c0f24e5bSlg 	    &hubd_errmask, &hubd_instance_debug, 0);
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	hubd->h_usba_device	= child_ud = usba_get_usba_device(dip);
18817c478bd9Sstevel@tonic-gate 	hubd->h_dip		= dip;
18827c478bd9Sstevel@tonic-gate 	hubd->h_instance	= instance;
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
18857c478bd9Sstevel@tonic-gate 	child_port_status = child_ud->usb_port_status;
18867c478bd9Sstevel@tonic-gate 	usb_dev_descr = child_ud->usb_dev_descr;
18877c478bd9Sstevel@tonic-gate 	parent_port_status = (child_ud->usb_hs_hub_usba_dev) ?
18887c478bd9Sstevel@tonic-gate 	    child_ud->usb_hs_hub_usba_dev->usb_port_status : 0;
18897c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	if ((child_port_status == USBA_FULL_SPEED_DEV) &&
18927c478bd9Sstevel@tonic-gate 	    (parent_port_status == USBA_HIGH_SPEED_DEV) &&
18937c478bd9Sstevel@tonic-gate 	    (usb_dev_descr->bcdUSB == 0x100)) {
18947c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
18957c478bd9Sstevel@tonic-gate 		    "Use of a USB1.0 hub behind a high speed port may "
18967c478bd9Sstevel@tonic-gate 		    "cause unexpected failures");
18977c478bd9Sstevel@tonic-gate 	}
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	hubd->h_pipe_policy.pp_max_async_reqs = 1;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	/* register with USBA as client driver */
19027c478bd9Sstevel@tonic-gate 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
19037c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19047c478bd9Sstevel@tonic-gate 		    "client attach failed");
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 		goto fail;
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	if (usb_get_dev_data(dip, &hubd->h_dev_data,
19107c478bd9Sstevel@tonic-gate 	    USB_PARSE_LVL_IF, 0) != USB_SUCCESS) {
19117c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19127c478bd9Sstevel@tonic-gate 		    "cannot get dev_data");
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 		goto fail;
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data,
19187c478bd9Sstevel@tonic-gate 	    hubd->h_dev_data->dev_curr_if, 0, 0,
19197c478bd9Sstevel@tonic-gate 	    (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) {
19207c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19217c478bd9Sstevel@tonic-gate 		    "no interrupt IN endpoint found");
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 		goto fail;
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	hubd->h_ep1_descr = ep_data->ep_descr;
19277c478bd9Sstevel@tonic-gate 	hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph;
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER,
1930c0f24e5bSlg 	    hubd->h_dev_data->dev_iblock_cookie);
19317c478bd9Sstevel@tonic-gate 	cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL);
1932ffcd51f3Slg 	cv_init(&hubd->h_cv_hotplug_dev, NULL, CV_DRIVER, NULL);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_LOCKS_DONE;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	usb_free_descr_tree(dip, hubd->h_dev_data);
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	/*
19397c478bd9Sstevel@tonic-gate 	 * register this hub instance with usba
19407c478bd9Sstevel@tonic-gate 	 */
19417c478bd9Sstevel@tonic-gate 	rval = usba_hubdi_register(dip, 0);
19427c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
1943d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19447c478bd9Sstevel@tonic-gate 		    "usba_hubdi_register failed");
19457c478bd9Sstevel@tonic-gate 		goto fail;
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
19497c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_HUBDI_REGISTERED;
19507c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_ONLINE;
19517c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/* now create components to power manage this device */
19547c478bd9Sstevel@tonic-gate 	hubd_create_pm_components(dip, hubd);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	/*
19577c478bd9Sstevel@tonic-gate 	 * Event handling: definition and registration
19587c478bd9Sstevel@tonic-gate 	 *
19597c478bd9Sstevel@tonic-gate 	 * first the  definition:
19607c478bd9Sstevel@tonic-gate 	 * get event handle
19617c478bd9Sstevel@tonic-gate 	 */
19627c478bd9Sstevel@tonic-gate 	(void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP);
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	/* bind event set to the handle */
19657c478bd9Sstevel@tonic-gate 	if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events,
19667c478bd9Sstevel@tonic-gate 	    NDI_SLEEP)) {
19677c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
19687c478bd9Sstevel@tonic-gate 		    "binding event set failed");
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 		goto fail;
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	/* event registration */
19747c478bd9Sstevel@tonic-gate 	if (hubd_register_events(hubd) != USB_SUCCESS) {
1975d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19767c478bd9Sstevel@tonic-gate 		    "hubd_register_events failed");
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 		goto fail;
19797c478bd9Sstevel@tonic-gate 	}
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
19827c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_EVENTS_REGISTERED;
19837c478bd9Sstevel@tonic-gate 
198435f36846Ssl 	if ((hubd_get_hub_descriptor(hubd)) != USB_SUCCESS) {
198535f36846Ssl 		mutex_exit(HUBD_MUTEX(hubd));
198635f36846Ssl 
198735f36846Ssl 		goto fail;
198835f36846Ssl 	}
198935f36846Ssl 
199035f36846Ssl 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
199135f36846Ssl 	    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
199235f36846Ssl 	    "hub-ignore-power-budget") == 1) {
199335f36846Ssl 		hubd->h_ignore_pwr_budget = B_TRUE;
199435f36846Ssl 	} else {
199535f36846Ssl 		hubd->h_ignore_pwr_budget = B_FALSE;
199635f36846Ssl 
199735f36846Ssl 		/* initialize hub power budget variables */
199835f36846Ssl 		if (hubd_init_power_budget(hubd) != USB_SUCCESS) {
199935f36846Ssl 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
200035f36846Ssl 			    "hubd_init_power_budget failed");
200135f36846Ssl 			mutex_exit(HUBD_MUTEX(hubd));
200235f36846Ssl 
200335f36846Ssl 			goto fail;
200435f36846Ssl 		}
200535f36846Ssl 	}
200635f36846Ssl 
20077c478bd9Sstevel@tonic-gate 	/* initialize and create children */
20087c478bd9Sstevel@tonic-gate 	if (hubd_check_ports(hubd) != USB_SUCCESS) {
2009d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
20107c478bd9Sstevel@tonic-gate 		    "hubd_check_ports failed");
20117c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 		goto fail;
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	/*
20177c478bd9Sstevel@tonic-gate 	 * create cfgadm nodes
20187c478bd9Sstevel@tonic-gate 	 */
20197c478bd9Sstevel@tonic-gate 	hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP);
20207c478bd9Sstevel@tonic-gate 	hubd_get_ancestry_str(hubd);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
20237c478bd9Sstevel@tonic-gate 	    "#ports=0x%x", hubd->h_hub_descr.bNbrPorts);
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 	for (i = 1; i <= hubd->h_hub_descr.bNbrPorts; i++) {
20267c478bd9Sstevel@tonic-gate 		char ap_name[HUBD_APID_NAMELEN];
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 		(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
20297c478bd9Sstevel@tonic-gate 		    hubd->h_ancestry_str, i);
20307c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
20317c478bd9Sstevel@tonic-gate 		    "ap_name=%s", ap_name);
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance,
20347c478bd9Sstevel@tonic-gate 		    DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
2035d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
20367c478bd9Sstevel@tonic-gate 			    "cannot create attachment point node (%d)",
20377c478bd9Sstevel@tonic-gate 			    instance);
20387c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 			goto fail;
20417c478bd9Sstevel@tonic-gate 		}
20427c478bd9Sstevel@tonic-gate 	}
2043112cd14aSqz 
2044112cd14aSqz 	ports_count = hubd->h_hub_descr.bNbrPorts;
20457c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	/* create minor nodes */
20487c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "hubd", S_IFCHR,
20497c478bd9Sstevel@tonic-gate 	    instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
20507c478bd9Sstevel@tonic-gate 
2051d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
20527c478bd9Sstevel@tonic-gate 		    "cannot create devctl minor node (%d)", instance);
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 		goto fail;
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
20587c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_MINOR_NODE_CREATED;
20597c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
20607c478bd9Sstevel@tonic-gate 
2061112cd14aSqz 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2062112cd14aSqz 	    "usb-port-count", ports_count) != DDI_PROP_SUCCESS) {
2063112cd14aSqz 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2064ff0e937bSRaymond Chen 		    "usb-port-count update failed");
2065112cd14aSqz 	}
2066112cd14aSqz 
20677c478bd9Sstevel@tonic-gate 	/*
20687c478bd9Sstevel@tonic-gate 	 * host controller driver has already reported this dev
20697c478bd9Sstevel@tonic-gate 	 * if we are the root hub
20707c478bd9Sstevel@tonic-gate 	 */
20717c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
20727c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
20737c478bd9Sstevel@tonic-gate 	}
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/* enable deathrow thread */
20767c478bd9Sstevel@tonic-gate 	hubd->h_cleanup_enabled = B_TRUE;
20777c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
20787c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, dip, 0);
20797c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate fail:
20847c478bd9Sstevel@tonic-gate 	{
20857c478bd9Sstevel@tonic-gate 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
20867c478bd9Sstevel@tonic-gate 
20878668df41Slg 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
20887c478bd9Sstevel@tonic-gate 		    "cannot attach %s", ddi_pathname(dip, pathname));
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
20947c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, dip, 0);
20957c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	if (hubd) {
20987c478bd9Sstevel@tonic-gate 		rval = hubd_cleanup(dip, hubd);
20997c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
2100d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
21017c478bd9Sstevel@tonic-gate 			    "failure to complete cleanup after attach failure");
21027c478bd9Sstevel@tonic-gate 		}
21037c478bd9Sstevel@tonic-gate 	}
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
21067c478bd9Sstevel@tonic-gate }
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate int
21107c478bd9Sstevel@tonic-gate usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = hubd_get_soft_state(dip);
21137c478bd9Sstevel@tonic-gate 	int	rval;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
21167c478bd9Sstevel@tonic-gate 	    "hubd_detach: cmd=0x%x", cmd);
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	switch (cmd) {
21197c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
21207c478bd9Sstevel@tonic-gate 		rval = hubd_cleanup(dip, hubd);
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
21237c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
21247c478bd9Sstevel@tonic-gate 		rval = hubd_cpr_suspend(hubd);
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
21277c478bd9Sstevel@tonic-gate 	default:
21287c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
21297c478bd9Sstevel@tonic-gate 	}
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate /*
21347c478bd9Sstevel@tonic-gate  * hubd_setdevaddr
21357c478bd9Sstevel@tonic-gate  *	set the device addrs on this port
21367c478bd9Sstevel@tonic-gate  */
21377c478bd9Sstevel@tonic-gate static int
21387c478bd9Sstevel@tonic-gate hubd_setdevaddr(hubd_t *hubd, usb_port_t port)
21397c478bd9Sstevel@tonic-gate {
21407c478bd9Sstevel@tonic-gate 	int		rval;
21417c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
21427c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
21437c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t ph;
21447c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip = NULL;
21457c478bd9Sstevel@tonic-gate 	uchar_t		address = 0;
21467c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
21477c478bd9Sstevel@tonic-gate 	int		retry = 0;
21487c478bd9Sstevel@tonic-gate 	long		time_delay;
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
21517c478bd9Sstevel@tonic-gate 	    "hubd_setdevaddr: port=%d", port);
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
21567c478bd9Sstevel@tonic-gate 	address = hubd->h_usba_devices[port]->usb_addr;
21577c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	/* close the default pipe with addr x */
21607c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
21617c478bd9Sstevel@tonic-gate 	ph = usba_get_dflt_pipe_handle(child_dip);
21627c478bd9Sstevel@tonic-gate 	usb_pipe_close(child_dip, ph,
21637c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
21647c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	/*
21677c478bd9Sstevel@tonic-gate 	 * As this device has been reset, temporarily
21687c478bd9Sstevel@tonic-gate 	 * assign the default address
21697c478bd9Sstevel@tonic-gate 	 */
21707c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
21717c478bd9Sstevel@tonic-gate 	address = usba_device->usb_addr;
21727c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = USBA_DEFAULT_ADDR;
21737c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	time_delay = drv_usectohz(hubd_device_delay / 20);
21787c478bd9Sstevel@tonic-gate 	for (retry = 0; retry < hubd_retry_enumerate; retry++) {
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 		/* open child's default pipe with USBA_DEFAULT_ADDR */
21817c478bd9Sstevel@tonic-gate 		if (usb_pipe_open(child_dip, NULL, NULL,
21827c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) !=
21837c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
21847c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
21857c478bd9Sstevel@tonic-gate 			    "hubd_setdevaddr: Unable to open default pipe");
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 			break;
21887c478bd9Sstevel@tonic-gate 		}
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 		/* Set the address of the device */
21917c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
21927c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_HOST_TO_DEV,
21937c478bd9Sstevel@tonic-gate 		    USB_REQ_SET_ADDRESS,	/* bRequest */
21947c478bd9Sstevel@tonic-gate 		    address,			/* wValue */
21957c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
21967c478bd9Sstevel@tonic-gate 		    0,				/* wLength */
21977c478bd9Sstevel@tonic-gate 		    NULL, 0,
21987c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
21997c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
22007c478bd9Sstevel@tonic-gate 			    "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x",
22017c478bd9Sstevel@tonic-gate 			    retry, rval, completion_reason, cb_flags);
22027c478bd9Sstevel@tonic-gate 		}
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 		usb_pipe_close(child_dip, ph,
22057c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 			break;
22107c478bd9Sstevel@tonic-gate 		}
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 		delay(time_delay);
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	/* Reset to the old address */
22167c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
22177c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = address;
22187c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
22197c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	usba_clear_data_toggle(usba_device);
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 	return (rval);
22247c478bd9Sstevel@tonic-gate }
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate /*
22287c478bd9Sstevel@tonic-gate  * hubd_setdevconfig
22297c478bd9Sstevel@tonic-gate  *	set the device addrs on this port
22307c478bd9Sstevel@tonic-gate  */
22317c478bd9Sstevel@tonic-gate static void
22327c478bd9Sstevel@tonic-gate hubd_setdevconfig(hubd_t *hubd, usb_port_t port)
22337c478bd9Sstevel@tonic-gate {
22347c478bd9Sstevel@tonic-gate 	int			rval;
22357c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
22367c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
22377c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	ph;
22387c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
22397c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device = NULL;
22407c478bd9Sstevel@tonic-gate 	uint16_t		config_value;
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
22437c478bd9Sstevel@tonic-gate 	    "hubd_setdevconfig: port=%d", port);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
22487c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
22497c478bd9Sstevel@tonic-gate 	config_value = hubd->h_usba_devices[port]->usb_cfg_value;
22507c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	/* open the default control pipe */
22537c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
22547c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) ==
22557c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 		/* Set the default configuration of the device */
22587c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
22597c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_HOST_TO_DEV,
22607c478bd9Sstevel@tonic-gate 		    USB_REQ_SET_CFG,		/* bRequest */
22617c478bd9Sstevel@tonic-gate 		    config_value,		/* wValue */
22627c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
22637c478bd9Sstevel@tonic-gate 		    0,				/* wLength */
22647c478bd9Sstevel@tonic-gate 		    NULL, 0,
22657c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
22667c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
22677c478bd9Sstevel@tonic-gate 			    "hubd_setdevconfig: set device config failed: "
22687c478bd9Sstevel@tonic-gate 			    "cr=%d cb_fl=0x%x rval=%d",
22697c478bd9Sstevel@tonic-gate 			    completion_reason, cb_flags, rval);
22707c478bd9Sstevel@tonic-gate 		}
22717c478bd9Sstevel@tonic-gate 		/*
22727c478bd9Sstevel@tonic-gate 		 * After setting the configuration, we make this default
22737c478bd9Sstevel@tonic-gate 		 * control pipe persistent, so that it gets re-opened
22747c478bd9Sstevel@tonic-gate 		 * on posting a connect event
22757c478bd9Sstevel@tonic-gate 		 */
22767c478bd9Sstevel@tonic-gate 		usba_persistent_pipe_close(usba_device);
22777c478bd9Sstevel@tonic-gate 	} else {
22787c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
22797c478bd9Sstevel@tonic-gate 		    "pipe open fails: rval=%d", rval);
22807c478bd9Sstevel@tonic-gate 	}
22817c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
22827c478bd9Sstevel@tonic-gate }
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22867c478bd9Sstevel@tonic-gate static int
22877c478bd9Sstevel@tonic-gate hubd_check_disconnected_ports(dev_info_t *dip, void *arg)
22887c478bd9Sstevel@tonic-gate {
22897c478bd9Sstevel@tonic-gate 	int circ;
22907c478bd9Sstevel@tonic-gate 	usb_port_t port;
22917c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
22927c478bd9Sstevel@tonic-gate 	major_t hub_major = ddi_name_to_major("hubd");
2293ff0e937bSRaymond Chen 	major_t hwahc_major = ddi_name_to_major("hwahc");
2294ff0e937bSRaymond Chen 	major_t usbmid_major = ddi_name_to_major("usb_mid");
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	/*
22977c478bd9Sstevel@tonic-gate 	 * make sure dip is a usb hub, major of root hub is HCD
22987c478bd9Sstevel@tonic-gate 	 * major
22997c478bd9Sstevel@tonic-gate 	 */
23007c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
2301ff0e937bSRaymond Chen 		if (ddi_driver_major(dip) == usbmid_major) {
2302ff0e937bSRaymond Chen 			/*
2303ff0e937bSRaymond Chen 			 * need to walk the children since it might be a
2304ff0e937bSRaymond Chen 			 * HWA device
2305ff0e937bSRaymond Chen 			 */
2306ff0e937bSRaymond Chen 
2307ff0e937bSRaymond Chen 			return (DDI_WALK_CONTINUE);
2308ff0e937bSRaymond Chen 		}
2309ff0e937bSRaymond Chen 
2310ff0e937bSRaymond Chen 		/* TODO: DWA device may also need special handling */
2311ff0e937bSRaymond Chen 
2312ff0e937bSRaymond Chen 		if (((ddi_driver_major(dip) != hub_major) &&
2313ff0e937bSRaymond Chen 		    (ddi_driver_major(dip) != hwahc_major)) ||
2314737d277aScth 		    !i_ddi_devi_attached(dip)) {
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 			return (DDI_WALK_PRUNECHILD);
23177c478bd9Sstevel@tonic-gate 		}
23187c478bd9Sstevel@tonic-gate 	}
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
23217c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	/* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */
23277c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
23287c478bd9Sstevel@tonic-gate 
2329ff0e937bSRaymond Chen 	if (ddi_driver_major(dip) != hwahc_major) {
2330ff0e937bSRaymond Chen 		/* for normal usb hub or root hub */
2331ff0e937bSRaymond Chen 		mutex_enter(HUBD_MUTEX(hubd));
2332ff0e937bSRaymond Chen 		for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
2333ff0e937bSRaymond Chen 			dev_info_t *cdip = hubd->h_children_dips[port];
23347c478bd9Sstevel@tonic-gate 
2335ff0e937bSRaymond Chen 			if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) {
23367c478bd9Sstevel@tonic-gate 
2337ff0e937bSRaymond Chen 				continue;
2338ff0e937bSRaymond Chen 			}
2339ff0e937bSRaymond Chen 
2340ff0e937bSRaymond Chen 			(void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE,
2341ff0e937bSRaymond Chen 			    B_TRUE);
23427c478bd9Sstevel@tonic-gate 		}
2343ff0e937bSRaymond Chen 		mutex_exit(HUBD_MUTEX(hubd));
2344ff0e937bSRaymond Chen 	} else {
2345ff0e937bSRaymond Chen 		/* for HWA */
2346ff0e937bSRaymond Chen 		if (hubd->h_cleanup_child != NULL) {
2347ff0e937bSRaymond Chen 			if (hubd->h_cleanup_child(dip) != USB_SUCCESS) {
2348ff0e937bSRaymond Chen 				ndi_devi_exit(dip, circ);
2349ff0e937bSRaymond Chen 
2350ff0e937bSRaymond Chen 				return (DDI_WALK_PRUNECHILD);
2351ff0e937bSRaymond Chen 			}
2352ff0e937bSRaymond Chen 		} else {
2353ff0e937bSRaymond Chen 			ndi_devi_exit(dip, circ);
23547c478bd9Sstevel@tonic-gate 
2355ff0e937bSRaymond Chen 			return (DDI_WALK_PRUNECHILD);
2356ff0e937bSRaymond Chen 		}
23577c478bd9Sstevel@tonic-gate 	}
2358ff0e937bSRaymond Chen 
23597c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	/* skip siblings of root hub */
23627c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNESIB);
23657c478bd9Sstevel@tonic-gate 	}
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate /*
23727c478bd9Sstevel@tonic-gate  * this thread will walk all children under the root hub for this
23737c478bd9Sstevel@tonic-gate  * USB bus instance and attempt to remove them
23747c478bd9Sstevel@tonic-gate  */
23757c478bd9Sstevel@tonic-gate static void
23767c478bd9Sstevel@tonic-gate hubd_root_hub_cleanup_thread(void *arg)
23777c478bd9Sstevel@tonic-gate {
23787c478bd9Sstevel@tonic-gate 	int circ;
23797c478bd9Sstevel@tonic-gate 	hubd_t *root_hubd = (hubd_t *)arg;
23807c478bd9Sstevel@tonic-gate 	dev_info_t *rh_dip = root_hubd->h_dip;
23817c478bd9Sstevel@tonic-gate #ifndef __lock_lint
23827c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr,
23857c478bd9Sstevel@tonic-gate 	    "USB root hub");
23867c478bd9Sstevel@tonic-gate #endif
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	for (;;) {
23897c478bd9Sstevel@tonic-gate 		/* don't race with detach */
23907c478bd9Sstevel@tonic-gate 		ndi_hold_devi(rh_dip);
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
23937c478bd9Sstevel@tonic-gate 		root_hubd->h_cleanup_needed = 0;
23947c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		(void) devfs_clean(rh_dip, NULL, 0);
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 		ndi_devi_enter(ddi_get_parent(rh_dip), &circ);
23997c478bd9Sstevel@tonic-gate 		ddi_walk_devs(rh_dip, hubd_check_disconnected_ports,
2400c0f24e5bSlg 		    NULL);
24017c478bd9Sstevel@tonic-gate #ifdef __lock_lint
24027c478bd9Sstevel@tonic-gate 		(void) hubd_check_disconnected_ports(rh_dip, NULL);
24037c478bd9Sstevel@tonic-gate #endif
24047c478bd9Sstevel@tonic-gate 		ndi_devi_exit(ddi_get_parent(rh_dip), circ);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 		/* quit if we are not enabled anymore */
24077c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
24087c478bd9Sstevel@tonic-gate 		if ((root_hubd->h_cleanup_enabled == B_FALSE) ||
24097c478bd9Sstevel@tonic-gate 		    (root_hubd->h_cleanup_needed == B_FALSE)) {
24107c478bd9Sstevel@tonic-gate 			root_hubd->h_cleanup_active = B_FALSE;
24117c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(root_hubd));
24127c478bd9Sstevel@tonic-gate 			ndi_rele_devi(rh_dip);
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 			break;
24157c478bd9Sstevel@tonic-gate 		}
24167c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24177c478bd9Sstevel@tonic-gate 		ndi_rele_devi(rh_dip);
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate #ifndef __lock_lint
24207c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
24217c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
24227c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(hubd_dip_cleanup_delay));
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
24277c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd));
24287c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24297c478bd9Sstevel@tonic-gate #endif
24307c478bd9Sstevel@tonic-gate 	}
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate #ifndef __lock_lint
24337c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(root_hubd));
24347c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
24357c478bd9Sstevel@tonic-gate #endif
24367c478bd9Sstevel@tonic-gate }
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 
2439ff0e937bSRaymond Chen void
24407c478bd9Sstevel@tonic-gate hubd_schedule_cleanup(dev_info_t *rh_dip)
24417c478bd9Sstevel@tonic-gate {
2442ff0e937bSRaymond Chen 	hubd_t	*root_hubd;
2443ff0e937bSRaymond Chen 
2444ff0e937bSRaymond Chen 	/*
2445ff0e937bSRaymond Chen 	 * The usb_root_hub_dip pointer for the child hub of the WUSB
2446ff0e937bSRaymond Chen 	 * wire adapter class device points to the wire adapter, not
2447ff0e937bSRaymond Chen 	 * the root hub. Need to find the real root hub dip so that
2448ff0e937bSRaymond Chen 	 * the cleanup thread only starts from the root hub.
2449ff0e937bSRaymond Chen 	 */
2450ff0e937bSRaymond Chen 	while (!usba_is_root_hub(rh_dip)) {
2451ff0e937bSRaymond Chen 		root_hubd = hubd_get_soft_state(rh_dip);
2452ff0e937bSRaymond Chen 		if (root_hubd != NULL) {
2453ff0e937bSRaymond Chen 			rh_dip = root_hubd->h_usba_device->usb_root_hub_dip;
2454ff0e937bSRaymond Chen 			if (rh_dip == NULL) {
2455ff0e937bSRaymond Chen 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2456ff0e937bSRaymond Chen 				    root_hubd->h_log_handle,
2457ff0e937bSRaymond Chen 				    "hubd_schedule_cleanup: null rh dip");
2458ff0e937bSRaymond Chen 
2459ff0e937bSRaymond Chen 				return;
2460ff0e937bSRaymond Chen 			}
2461ff0e937bSRaymond Chen 		} else {
2462ff0e937bSRaymond Chen 			USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2463ff0e937bSRaymond Chen 			    root_hubd->h_log_handle,
2464ff0e937bSRaymond Chen 			    "hubd_schedule_cleanup: cannot find root hub");
2465ff0e937bSRaymond Chen 
2466ff0e937bSRaymond Chen 			return;
2467ff0e937bSRaymond Chen 		}
2468ff0e937bSRaymond Chen 	}
2469ff0e937bSRaymond Chen 	root_hubd = hubd_get_soft_state(rh_dip);
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(root_hubd));
24727c478bd9Sstevel@tonic-gate 	root_hubd->h_cleanup_needed = B_TRUE;
24737c478bd9Sstevel@tonic-gate 	if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) {
24747c478bd9Sstevel@tonic-gate 		root_hubd->h_cleanup_active = B_TRUE;
24757c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24767c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, 0,
24777c478bd9Sstevel@tonic-gate 		    hubd_root_hub_cleanup_thread,
24787c478bd9Sstevel@tonic-gate 		    (void *)root_hubd, 0, &p0, TS_RUN,
24797c478bd9Sstevel@tonic-gate 		    minclsyspri);
24807c478bd9Sstevel@tonic-gate 	} else {
24817c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24827c478bd9Sstevel@tonic-gate 	}
24837c478bd9Sstevel@tonic-gate }
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate /*
24877c478bd9Sstevel@tonic-gate  * hubd_restore_device_state:
24887c478bd9Sstevel@tonic-gate  *	- set config for the hub
24897c478bd9Sstevel@tonic-gate  *	- power cycle all the ports
24907c478bd9Sstevel@tonic-gate  *	- for each port that was connected
24917c478bd9Sstevel@tonic-gate  *		- reset port
24927c478bd9Sstevel@tonic-gate  *		- assign addrs to the device on this port
24937c478bd9Sstevel@tonic-gate  *	- restart polling
24947c478bd9Sstevel@tonic-gate  *	- reset suspend flag
24957c478bd9Sstevel@tonic-gate  */
24967c478bd9Sstevel@tonic-gate static void
24977c478bd9Sstevel@tonic-gate hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd)
24987c478bd9Sstevel@tonic-gate {
24997c478bd9Sstevel@tonic-gate 	int		rval;
25007c478bd9Sstevel@tonic-gate 	int		retry;
25017c478bd9Sstevel@tonic-gate 	uint_t		hub_prev_state;
25027c478bd9Sstevel@tonic-gate 	usb_port_t	port;
25037c478bd9Sstevel@tonic-gate 	uint16_t	status;
25047c478bd9Sstevel@tonic-gate 	uint16_t	change;
25057c478bd9Sstevel@tonic-gate 	dev_info_t	*ch_dip;
25067c478bd9Sstevel@tonic-gate 	boolean_t	ehci_root_hub;
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
25097c478bd9Sstevel@tonic-gate 	    "hubd_restore_device_state:");
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
25127c478bd9Sstevel@tonic-gate 	hub_prev_state = hubd->h_dev_state;
25137c478bd9Sstevel@tonic-gate 	ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN);
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	/* First bring the device to full power */
25167c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, dip, 0);
25177c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip) &&
25227c478bd9Sstevel@tonic-gate 	    (usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0,
25237c478bd9Sstevel@tonic-gate 	    DPRINT_MASK_HOTPLUG,
25247c478bd9Sstevel@tonic-gate 	    USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) {
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 		/* change the device state to disconnected */
25277c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
25287c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
25297c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
25307c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 		return;
25337c478bd9Sstevel@tonic-gate 	}
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0);
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
25387c478bd9Sstevel@tonic-gate 	/* First turn off all port power */
25397c478bd9Sstevel@tonic-gate 	rval = hubd_disable_all_port_power(hubd);
25407c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
25417c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
25427c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state:"
25437c478bd9Sstevel@tonic-gate 		    "turning off port power failed");
25447c478bd9Sstevel@tonic-gate 	}
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	/* Settling time before turning on again */
25477c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
25487c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(hubd_device_delay / 100));
25497c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	/* enable power on all ports so we can see connects */
25527c478bd9Sstevel@tonic-gate 	if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) {
25537c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
25547c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state: turn on port power failed");
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
25577c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
25607c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 		return;
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	/*
25667c478bd9Sstevel@tonic-gate 	 * wait at least 3 frames before accessing devices
25677c478bd9Sstevel@tonic-gate 	 * (note that delay's minimal time is one clock tick which
25687c478bd9Sstevel@tonic-gate 	 * is 10ms unless hires_tick has been changed)
25697c478bd9Sstevel@tonic-gate 	 */
25707c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
25717c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
25727c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER;
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
25777c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
25787c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state: port=%d", port);
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 		/*
25817c478bd9Sstevel@tonic-gate 		 * the childen_dips list may have dips that have been
25827c478bd9Sstevel@tonic-gate 		 * already deallocated. we only get a post_detach notification
25837c478bd9Sstevel@tonic-gate 		 * but not a destroy notification
25847c478bd9Sstevel@tonic-gate 		 */
25857c478bd9Sstevel@tonic-gate 		ch_dip = hubd->h_children_dips[port];
25867c478bd9Sstevel@tonic-gate 		if (ch_dip) {
25877c478bd9Sstevel@tonic-gate 			/* get port status */
25887c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
25897c478bd9Sstevel@tonic-gate 			    &status, &change, PORT_CHANGE_CSC);
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 			/* check if it is truly connected */
25927c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
25937c478bd9Sstevel@tonic-gate 				/*
25947c478bd9Sstevel@tonic-gate 				 * Now reset port and assign the device
25957c478bd9Sstevel@tonic-gate 				 * its original address
25967c478bd9Sstevel@tonic-gate 				 */
25977c478bd9Sstevel@tonic-gate 				retry = 0;
25987c478bd9Sstevel@tonic-gate 				do {
25997c478bd9Sstevel@tonic-gate 					(void) hubd_reset_port(hubd, port);
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 					/* required for ppx */
26027c478bd9Sstevel@tonic-gate 					(void) hubd_enable_port(hubd, port);
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 					if (retry) {
26057c478bd9Sstevel@tonic-gate 						mutex_exit(HUBD_MUTEX(hubd));
26067c478bd9Sstevel@tonic-gate 						delay(drv_usectohz(
2607c0f24e5bSlg 						    hubd_device_delay/2));
26087c478bd9Sstevel@tonic-gate 						mutex_enter(HUBD_MUTEX(hubd));
26097c478bd9Sstevel@tonic-gate 					}
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 					rval = hubd_setdevaddr(hubd, port);
26127c478bd9Sstevel@tonic-gate 					retry++;
26137c478bd9Sstevel@tonic-gate 				} while ((rval != USB_SUCCESS) &&
26147c478bd9Sstevel@tonic-gate 				    (retry < hubd_retry_enumerate));
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 				hubd_setdevconfig(hubd, port);
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 				if (hub_prev_state == USB_DEV_DISCONNECTED) {
26197c478bd9Sstevel@tonic-gate 					/* post a connect event */
26207c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
26217c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
26227c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_INSERTION);
26237c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
26247c478bd9Sstevel@tonic-gate 				} else {
26257c478bd9Sstevel@tonic-gate 					/*
26267c478bd9Sstevel@tonic-gate 					 * Since we have this device connected
26277c478bd9Sstevel@tonic-gate 					 * mark it reinserted to prevent
26287c478bd9Sstevel@tonic-gate 					 * cleanup thread from stepping in.
26297c478bd9Sstevel@tonic-gate 					 */
26307c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
263116747f41Scth 					mutex_enter(&(DEVI(ch_dip)->devi_lock));
263216747f41Scth 					DEVI_SET_DEVICE_REINSERTED(ch_dip);
263316747f41Scth 					mutex_exit(&(DEVI(ch_dip)->devi_lock));
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 					/*
26367c478bd9Sstevel@tonic-gate 					 * reopen pipes for children for
26377c478bd9Sstevel@tonic-gate 					 * their DDI_RESUME
26387c478bd9Sstevel@tonic-gate 					 */
26397c478bd9Sstevel@tonic-gate 					rval = usba_persistent_pipe_open(
26407c478bd9Sstevel@tonic-gate 					    usba_get_usba_device(ch_dip));
26417c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
26427c478bd9Sstevel@tonic-gate 					ASSERT(rval == USB_SUCCESS);
26437c478bd9Sstevel@tonic-gate 				}
26447c478bd9Sstevel@tonic-gate 			} else {
26457c478bd9Sstevel@tonic-gate 				/*
26467c478bd9Sstevel@tonic-gate 				 * Mark this dip for deletion as the device
26477c478bd9Sstevel@tonic-gate 				 * is not physically present, and schedule
26487c478bd9Sstevel@tonic-gate 				 * cleanup thread upon post resume
26497c478bd9Sstevel@tonic-gate 				 */
26507c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
26537c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
26547c478bd9Sstevel@tonic-gate 				    "hubd_restore_device_state: "
26557c478bd9Sstevel@tonic-gate 				    "dip=%p on port=%d marked for cleanup",
2656112116d8Sfb 				    (void *)ch_dip, port);
265716747f41Scth 				mutex_enter(&(DEVI(ch_dip)->devi_lock));
26587c478bd9Sstevel@tonic-gate 				DEVI_SET_DEVICE_REMOVED(ch_dip);
265916747f41Scth 				mutex_exit(&(DEVI(ch_dip)->devi_lock));
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
26627c478bd9Sstevel@tonic-gate 			}
26637c478bd9Sstevel@tonic-gate 		} else if (ehci_root_hub) {
26647c478bd9Sstevel@tonic-gate 			/* get port status */
26657c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
26667c478bd9Sstevel@tonic-gate 			    &status, &change, PORT_CHANGE_CSC);
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 			/* check if it is truly connected */
26697c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
26707c478bd9Sstevel@tonic-gate 				/*
26717c478bd9Sstevel@tonic-gate 				 * reset the port to find out if we have
26727c478bd9Sstevel@tonic-gate 				 * 2.0 device connected or 1.X. A 2.0
26737c478bd9Sstevel@tonic-gate 				 * device will still be seen as connected,
26747c478bd9Sstevel@tonic-gate 				 * while a 1.X device will switch over to
26757c478bd9Sstevel@tonic-gate 				 * the companion controller.
26767c478bd9Sstevel@tonic-gate 				 */
26777c478bd9Sstevel@tonic-gate 				(void) hubd_reset_port(hubd, port);
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 				(void) hubd_determine_port_status(hubd, port,
26807c478bd9Sstevel@tonic-gate 				    &status, &change, PORT_CHANGE_CSC);
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 				if (status &
26837c478bd9Sstevel@tonic-gate 				    (PORT_STATUS_CCS | PORT_STATUS_HSDA)) {
26847c478bd9Sstevel@tonic-gate 					/*
26857c478bd9Sstevel@tonic-gate 					 * We have a USB 2.0 device
26867c478bd9Sstevel@tonic-gate 					 * connected. Power cycle this port
26877c478bd9Sstevel@tonic-gate 					 * so that hotplug thread can
26887c478bd9Sstevel@tonic-gate 					 * enumerate this device.
26897c478bd9Sstevel@tonic-gate 					 */
26907c478bd9Sstevel@tonic-gate 					(void) hubd_toggle_port(hubd, port);
26917c478bd9Sstevel@tonic-gate 				} else {
26927c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_ATTA,
26937c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
26947c478bd9Sstevel@tonic-gate 					    "hubd_restore_device_state: "
26957c478bd9Sstevel@tonic-gate 					    "device on port %d switched over",
26967c478bd9Sstevel@tonic-gate 					    port);
26977c478bd9Sstevel@tonic-gate 				}
26987c478bd9Sstevel@tonic-gate 			}
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 		}
27017c478bd9Sstevel@tonic-gate 	}
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	/* if the device had remote wakeup earlier, enable it again */
27057c478bd9Sstevel@tonic-gate 	if (hubd->h_hubpm->hubp_wakeup_enabled) {
27067c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
27077c478bd9Sstevel@tonic-gate 		(void) usb_handle_remote_wakeup(hubd->h_dip,
27087c478bd9Sstevel@tonic-gate 		    USB_REMOTE_WAKEUP_ENABLE);
27097c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
27107c478bd9Sstevel@tonic-gate 	}
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_ONLINE;
27137c478bd9Sstevel@tonic-gate 	hubd_start_polling(hubd, 0);
27147c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, dip, 0);
27157c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate /*
27207c478bd9Sstevel@tonic-gate  * hubd_cleanup:
27217c478bd9Sstevel@tonic-gate  *	cleanup hubd and deallocate. this function is called for
27227c478bd9Sstevel@tonic-gate  *	handling attach failures and detaching including dynamic
27237c478bd9Sstevel@tonic-gate  *	reconfiguration. If called from attaching, it must clean
27247c478bd9Sstevel@tonic-gate  *	up the whole thing and return success.
27257c478bd9Sstevel@tonic-gate  */
27267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27277c478bd9Sstevel@tonic-gate static int
27287c478bd9Sstevel@tonic-gate hubd_cleanup(dev_info_t *dip, hubd_t *hubd)
27297c478bd9Sstevel@tonic-gate {
27307c478bd9Sstevel@tonic-gate 	int		circ, rval, old_dev_state;
27317c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
27327c478bd9Sstevel@tonic-gate #ifdef DEBUG
27337c478bd9Sstevel@tonic-gate 	usb_port_t	port;
27347c478bd9Sstevel@tonic-gate #endif
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
27377c478bd9Sstevel@tonic-gate 	    "hubd_cleanup:");
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 	if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) {
27407c478bd9Sstevel@tonic-gate 		goto done;
27417c478bd9Sstevel@tonic-gate 	}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	/* ensure we are the only one active */
27447c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	/* Cleanup failure is only allowed if called from detach */
27497c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DETACHING(dip)) {
27507c478bd9Sstevel@tonic-gate 		dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 		/*
27537c478bd9Sstevel@tonic-gate 		 * We are being called from detach.
27547c478bd9Sstevel@tonic-gate 		 * Fail immediately if the hotplug thread is running
27557c478bd9Sstevel@tonic-gate 		 * else set the dev_state to disconnected so that
27567c478bd9Sstevel@tonic-gate 		 * hotplug thread just exits without doing anything.
27577c478bd9Sstevel@tonic-gate 		 */
27587c478bd9Sstevel@tonic-gate 		if (hubd->h_bus_ctls || hubd->h_bus_pwr ||
27597c478bd9Sstevel@tonic-gate 		    hubd->h_hotplug_thread) {
27607c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
27617c478bd9Sstevel@tonic-gate 			ndi_devi_exit(dip, circ);
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
27647c478bd9Sstevel@tonic-gate 			    "hubd_cleanup: hotplug thread/bus ctl active "
27657c478bd9Sstevel@tonic-gate 			    "- failing detach");
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
27687c478bd9Sstevel@tonic-gate 		}
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate 		/*
27717c478bd9Sstevel@tonic-gate 		 * if the deathrow thread is still active or about
27727c478bd9Sstevel@tonic-gate 		 * to become active, fail detach
27737c478bd9Sstevel@tonic-gate 		 * the roothup can only be detached if nexus drivers
27747c478bd9Sstevel@tonic-gate 		 * are unloaded or explicitly offlined
27757c478bd9Sstevel@tonic-gate 		 */
27767c478bd9Sstevel@tonic-gate 		if (rh_dip == dip) {
27777c478bd9Sstevel@tonic-gate 			if (hubd->h_cleanup_needed ||
27787c478bd9Sstevel@tonic-gate 			    hubd->h_cleanup_active) {
27797c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
27807c478bd9Sstevel@tonic-gate 				ndi_devi_exit(dip, circ);
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
27837c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
27847c478bd9Sstevel@tonic-gate 				    "hubd_cleanup: deathrow still active?"
27857c478bd9Sstevel@tonic-gate 				    "- failing detach");
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 				return (USB_FAILURE);
27887c478bd9Sstevel@tonic-gate 			}
27897c478bd9Sstevel@tonic-gate 		}
27907c478bd9Sstevel@tonic-gate 	}
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	old_dev_state = hubd->h_dev_state;
27937c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_DISCONNECTED;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
27967c478bd9Sstevel@tonic-gate 	    "hubd_cleanup: stop polling");
27977c478bd9Sstevel@tonic-gate 	hubd_close_intr_pipe(hubd);
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr ||
28007c478bd9Sstevel@tonic-gate 	    hubd->h_hotplug_thread) == 0);
28017c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	/*
28047c478bd9Sstevel@tonic-gate 	 * deallocate events, if events are still registered
28057c478bd9Sstevel@tonic-gate 	 * (ie. children still attached) then we have to fail the detach
28067c478bd9Sstevel@tonic-gate 	 */
28077c478bd9Sstevel@tonic-gate 	if (hubd->h_ndi_event_hdl) {
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 		rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl);
28107c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(dip)) {
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 			/* It must return success if attaching. */
28137c478bd9Sstevel@tonic-gate 			ASSERT(rval == NDI_SUCCESS);
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 		} else if (rval != NDI_SUCCESS) {
28167c478bd9Sstevel@tonic-gate 
2817d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle,
28187c478bd9Sstevel@tonic-gate 			    "hubd_cleanup: ndi_event_free_hdl failed");
28197c478bd9Sstevel@tonic-gate 			ndi_devi_exit(dip, circ);
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 		}
28247c478bd9Sstevel@tonic-gate 	}
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
28277c478bd9Sstevel@tonic-gate 
282835f36846Ssl 	if (hubd->h_init_state & HUBD_CHILDREN_CREATED) {
28297c478bd9Sstevel@tonic-gate #ifdef DEBUG
283035f36846Ssl 		for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
283135f36846Ssl 			ASSERT(hubd->h_usba_devices[port] == NULL);
283235f36846Ssl 			ASSERT(hubd->h_children_dips[port] == NULL);
283335f36846Ssl 		}
28347c478bd9Sstevel@tonic-gate #endif
283535f36846Ssl 		kmem_free(hubd->h_children_dips, hubd->h_cd_list_length);
283635f36846Ssl 		kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length);
283735f36846Ssl 	}
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	/*
28407c478bd9Sstevel@tonic-gate 	 * Disable the event callbacks first, after this point, event
28417c478bd9Sstevel@tonic-gate 	 * callbacks will never get called. Note we shouldn't hold
28427c478bd9Sstevel@tonic-gate 	 * mutex while unregistering events because there may be a
28437c478bd9Sstevel@tonic-gate 	 * competing event callback thread. Event callbacks are done
28447c478bd9Sstevel@tonic-gate 	 * with ndi mutex held and this can cause a potential deadlock.
28457c478bd9Sstevel@tonic-gate 	 * Note that cleanup can't fail after deregistration of events.
28467c478bd9Sstevel@tonic-gate 	 */
28477c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state &  HUBD_EVENTS_REGISTERED) {
28487c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
28497c478bd9Sstevel@tonic-gate 		usb_unregister_event_cbs(dip, &hubd_events);
28507c478bd9Sstevel@tonic-gate 		hubd_unregister_cpr_callback(hubd);
28517c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
28527c478bd9Sstevel@tonic-gate 	}
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	/* restore the old dev state so that device can be put into low power */
28557c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = old_dev_state;
28567c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) {
28597c478bd9Sstevel@tonic-gate 		(void) hubd_pm_busy_component(hubd, dip, 0);
28607c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
28617c478bd9Sstevel@tonic-gate 		if (hubd->h_hubpm->hubp_wakeup_enabled) {
28627c478bd9Sstevel@tonic-gate 			/*
28637c478bd9Sstevel@tonic-gate 			 * Bring the hub to full power before
28647c478bd9Sstevel@tonic-gate 			 * issuing the disable remote wakeup command
28657c478bd9Sstevel@tonic-gate 			 */
28667c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 			if ((rval = usb_handle_remote_wakeup(hubd->h_dip,
28697c478bd9Sstevel@tonic-gate 			    USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) {
28707c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PM,
28717c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
28727c478bd9Sstevel@tonic-gate 				    "hubd_cleanup: disable remote wakeup "
28737c478bd9Sstevel@tonic-gate 				    "fails=%d", rval);
28747c478bd9Sstevel@tonic-gate 			}
28757c478bd9Sstevel@tonic-gate 		}
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 		(void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF);
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
28807c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
28817c478bd9Sstevel@tonic-gate 	}
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	if (hubpm) {
28847c478bd9Sstevel@tonic-gate 		if (hubpm->hubp_child_pwrstate) {
28857c478bd9Sstevel@tonic-gate 			kmem_free(hubpm->hubp_child_pwrstate,
28867c478bd9Sstevel@tonic-gate 			    MAX_PORTS + 1);
28877c478bd9Sstevel@tonic-gate 		}
28887c478bd9Sstevel@tonic-gate 		kmem_free(hubpm, sizeof (hub_power_t));
28897c478bd9Sstevel@tonic-gate 	}
28907c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
28937c478bd9Sstevel@tonic-gate 	    "hubd_cleanup: freeing space");
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) {
28967c478bd9Sstevel@tonic-gate 		rval = usba_hubdi_unregister(dip);
28977c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
28987c478bd9Sstevel@tonic-gate 	}
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_LOCKS_DONE) {
29017c478bd9Sstevel@tonic-gate 		mutex_destroy(HUBD_MUTEX(hubd));
29027c478bd9Sstevel@tonic-gate 		cv_destroy(&hubd->h_cv_reset_port);
2903ffcd51f3Slg 		cv_destroy(&hubd->h_cv_hotplug_dev);
29047c478bd9Sstevel@tonic-gate 	}
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) {
29097c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
29107c478bd9Sstevel@tonic-gate 	}
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
29137c478bd9Sstevel@tonic-gate 		usb_pipe_close(dip, hubd->h_default_pipe,
29147c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
29157c478bd9Sstevel@tonic-gate 	}
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate done:
29187c478bd9Sstevel@tonic-gate 	if (hubd->h_ancestry_str) {
29197c478bd9Sstevel@tonic-gate 		kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN);
29207c478bd9Sstevel@tonic-gate 	}
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	usb_client_detach(dip, hubd->h_dev_data);
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(hubd->h_log_handle);
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
29277c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(hubd_statep, ddi_get_instance(dip));
29287c478bd9Sstevel@tonic-gate 	}
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
29337c478bd9Sstevel@tonic-gate }
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 
29366c7181fcSsl /*
29376c7181fcSsl  * hubd_determine_port_connection:
29386c7181fcSsl  *	Determine which port is in connect status but does not
29396c7181fcSsl  *	have connect status change bit set, and mark port change
29406c7181fcSsl  *	bit accordingly.
29416c7181fcSsl  *	This function is applied during hub attach time.
29426c7181fcSsl  */
29436c7181fcSsl static usb_port_mask_t
29446c7181fcSsl hubd_determine_port_connection(hubd_t	*hubd)
29456c7181fcSsl {
29466c7181fcSsl 	usb_port_t	port;
29476c7181fcSsl 	usb_hub_descr_t	*hub_descr;
29486c7181fcSsl 	uint16_t	status;
29496c7181fcSsl 	uint16_t	change;
29506c7181fcSsl 	usb_port_mask_t	port_change = 0;
29516c7181fcSsl 
29526c7181fcSsl 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
29536c7181fcSsl 
29546c7181fcSsl 	hub_descr = &hubd->h_hub_descr;
29556c7181fcSsl 
29566c7181fcSsl 	for (port = 1; port <= hub_descr->bNbrPorts; port++) {
29576c7181fcSsl 
29586c7181fcSsl 		(void) hubd_determine_port_status(hubd, port, &status,
29596c7181fcSsl 		    &change, 0);
29606c7181fcSsl 
29616c7181fcSsl 		/* Check if port is in connect status */
29626c7181fcSsl 		if (!(status & PORT_STATUS_CCS)) {
29636c7181fcSsl 
29646c7181fcSsl 			continue;
29656c7181fcSsl 		}
29666c7181fcSsl 
29676c7181fcSsl 		/*
29686c7181fcSsl 		 * Check if port Connect Status Change bit has been set.
29696c7181fcSsl 		 * If already set, the connection will be handled by
29706c7181fcSsl 		 * intr polling callback, not during attach.
29716c7181fcSsl 		 */
29726c7181fcSsl 		if (change & PORT_CHANGE_CSC) {
29736c7181fcSsl 
29746c7181fcSsl 			continue;
29756c7181fcSsl 		}
29766c7181fcSsl 
29776c7181fcSsl 		port_change |= 1 << port;
29786c7181fcSsl 	}
29796c7181fcSsl 
29806c7181fcSsl 	return (port_change);
29816c7181fcSsl }
29826c7181fcSsl 
29836c7181fcSsl 
29847c478bd9Sstevel@tonic-gate /*
29857c478bd9Sstevel@tonic-gate  * hubd_check_ports:
29867c478bd9Sstevel@tonic-gate  *	- get hub descriptor
29877c478bd9Sstevel@tonic-gate  *	- check initial port status
29887c478bd9Sstevel@tonic-gate  *	- enable power on all ports
29897c478bd9Sstevel@tonic-gate  *	- enable polling on ep1
29907c478bd9Sstevel@tonic-gate  */
29917c478bd9Sstevel@tonic-gate static int
29927c478bd9Sstevel@tonic-gate hubd_check_ports(hubd_t  *hubd)
29937c478bd9Sstevel@tonic-gate {
29946c7181fcSsl 	int			rval;
29956c7181fcSsl 	usb_port_mask_t		port_change = 0;
29966c7181fcSsl 	hubd_hotplug_arg_t	*arg;
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
30017c478bd9Sstevel@tonic-gate 	    "hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip));
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate 	/*
30047c478bd9Sstevel@tonic-gate 	 * First turn off all port power
30057c478bd9Sstevel@tonic-gate 	 */
30067c478bd9Sstevel@tonic-gate 	if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) {
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
30097c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 		return (rval);
30127c478bd9Sstevel@tonic-gate 	}
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	/*
30157c478bd9Sstevel@tonic-gate 	 * do not switch on immediately (instantly on root hub)
30167c478bd9Sstevel@tonic-gate 	 * and allow time to settle
30177c478bd9Sstevel@tonic-gate 	 */
30187c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
30197c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
30207c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 	/*
30237c478bd9Sstevel@tonic-gate 	 * enable power on all ports so we can see connects
30247c478bd9Sstevel@tonic-gate 	 */
30257c478bd9Sstevel@tonic-gate 	if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) {
30267c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
30277c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 		return (rval);
30307c478bd9Sstevel@tonic-gate 	}
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 	/* wait at least 3 frames before accessing devices */
30337c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
30347c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
30357c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	/*
30387c478bd9Sstevel@tonic-gate 	 * allocate arrays for saving the dips of each child per port
30397c478bd9Sstevel@tonic-gate 	 *
30407c478bd9Sstevel@tonic-gate 	 * ports go from 1 - n, allocate 1 more entry
30417c478bd9Sstevel@tonic-gate 	 */
30427c478bd9Sstevel@tonic-gate 	hubd->h_cd_list_length =
3043c0f24e5bSlg 	    (sizeof (dev_info_t **)) * (hubd->h_hub_descr.bNbrPorts + 1);
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	hubd->h_children_dips = (dev_info_t **)kmem_zalloc(
3046c0f24e5bSlg 	    hubd->h_cd_list_length, KM_SLEEP);
30477c478bd9Sstevel@tonic-gate 	hubd->h_usba_devices = (usba_device_t **)kmem_zalloc(
3048c0f24e5bSlg 	    hubd->h_cd_list_length, KM_SLEEP);
30497c478bd9Sstevel@tonic-gate 
305035f36846Ssl 	hubd->h_init_state |= HUBD_CHILDREN_CREATED;
305135f36846Ssl 
30526c7181fcSsl 	mutex_exit(HUBD_MUTEX(hubd));
30536c7181fcSsl 	arg = (hubd_hotplug_arg_t *)kmem_zalloc(
30546c7181fcSsl 	    sizeof (hubd_hotplug_arg_t), KM_SLEEP);
30556c7181fcSsl 	mutex_enter(HUBD_MUTEX(hubd));
30566c7181fcSsl 
30576c7181fcSsl 	if ((rval = hubd_open_intr_pipe(hubd)) != USB_SUCCESS) {
30586c7181fcSsl 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
30596c7181fcSsl 
30606c7181fcSsl 		return (rval);
30616c7181fcSsl 	}
30626c7181fcSsl 
30636c7181fcSsl 	hubd_start_polling(hubd, 0);
30646c7181fcSsl 
30656c7181fcSsl 	/*
30666c7181fcSsl 	 * Some hub devices, like the embedded hub in the CKS ErgoMagic
30676c7181fcSsl 	 * keyboard, may only have connection status bit set, but not
30686c7181fcSsl 	 * have connect status change bit set when a device has been
30696c7181fcSsl 	 * connected to its downstream port before the hub is enumerated.
30706c7181fcSsl 	 * Then when the hub is in enumeration, the devices connected to
30716c7181fcSsl 	 * it cannot be detected by the intr pipe and won't be enumerated.
30726c7181fcSsl 	 * We need to check such situation here and enumerate the downstream
30736c7181fcSsl 	 * devices for such hubs.
30746c7181fcSsl 	 */
30756c7181fcSsl 	port_change = hubd_determine_port_connection(hubd);
30766c7181fcSsl 
30776c7181fcSsl 	if (port_change) {
30786c7181fcSsl 		hubd_pm_busy_component(hubd, hubd->h_dip, 0);
30796c7181fcSsl 
30806c7181fcSsl 		arg->hubd = hubd;
30816c7181fcSsl 		arg->hotplug_during_attach = B_TRUE;
30826c7181fcSsl 		hubd->h_port_change |= port_change;
30836c7181fcSsl 
30846c7181fcSsl 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
30856c7181fcSsl 		    "hubd_check_ports: port change=0x%x, need to connect",
30866c7181fcSsl 		    hubd->h_port_change);
30876c7181fcSsl 
30886c7181fcSsl 		if (usb_async_req(hubd->h_dip, hubd_hotplug_thread,
30896c7181fcSsl 		    (void *)arg, 0) == USB_SUCCESS) {
30906c7181fcSsl 			hubd->h_hotplug_thread++;
30916c7181fcSsl 		} else {
30926c7181fcSsl 			/* mark this device as idle */
30936c7181fcSsl 			hubd_pm_idle_component(hubd, hubd->h_dip, 0);
30946c7181fcSsl 			kmem_free(arg, sizeof (hubd_hotplug_arg_t));
30956c7181fcSsl 		}
30966c7181fcSsl 	} else {
30976c7181fcSsl 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
30987c478bd9Sstevel@tonic-gate 	}
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
31017c478bd9Sstevel@tonic-gate 	    "hubd_check_ports done");
31027c478bd9Sstevel@tonic-gate 
31036c7181fcSsl 	return (USB_SUCCESS);
31047c478bd9Sstevel@tonic-gate }
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate /*
31087c478bd9Sstevel@tonic-gate  * hubd_get_hub_descriptor:
31097c478bd9Sstevel@tonic-gate  */
31107c478bd9Sstevel@tonic-gate static int
31117c478bd9Sstevel@tonic-gate hubd_get_hub_descriptor(hubd_t *hubd)
31127c478bd9Sstevel@tonic-gate {
31137c478bd9Sstevel@tonic-gate 	usb_hub_descr_t	*hub_descr = &hubd->h_hub_descr;
31147c478bd9Sstevel@tonic-gate 	mblk_t		*data = NULL;
31157c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
31167c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
31177c478bd9Sstevel@tonic-gate 	uint16_t	length;
31187c478bd9Sstevel@tonic-gate 	int		rval;
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
31217c478bd9Sstevel@tonic-gate 	    "hubd_get_hub_descriptor:");
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
31247c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_default_pipe != 0);
31257c478bd9Sstevel@tonic-gate 
31267c478bd9Sstevel@tonic-gate 	/* get hub descriptor length first by requesting 8 bytes only */
31277c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
31307c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
313135f36846Ssl 	    HUB_CLASS_REQ_TYPE,
31327c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,		/* bRequest */
31337c478bd9Sstevel@tonic-gate 	    USB_DESCR_TYPE_SETUP_HUB,	/* wValue */
31347c478bd9Sstevel@tonic-gate 	    0,				/* wIndex */
31357c478bd9Sstevel@tonic-gate 	    8,				/* wLength */
31367c478bd9Sstevel@tonic-gate 	    &data, 0,
31377c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
31387c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
31397c478bd9Sstevel@tonic-gate 		    "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
31407c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
31417c478bd9Sstevel@tonic-gate 		freemsg(data);
31427c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 		return (rval);
31457c478bd9Sstevel@tonic-gate 	}
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 	length = *(data->b_rptr);
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	if (length > 8) {
31507c478bd9Sstevel@tonic-gate 		freemsg(data);
31517c478bd9Sstevel@tonic-gate 		data = NULL;
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate 		/* get complete hub descriptor */
31547c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
31557c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
315635f36846Ssl 		    HUB_CLASS_REQ_TYPE,
31577c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,		/* bRequest */
31587c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_HUB,	/* wValue */
31597c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
31607c478bd9Sstevel@tonic-gate 		    length,			/* wLength */
31617c478bd9Sstevel@tonic-gate 		    &data, 0,
31627c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
31637c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
31647c478bd9Sstevel@tonic-gate 			    "get hub descriptor failed: "
31657c478bd9Sstevel@tonic-gate 			    "cr=%d cb_fl=0x%x rval=%d",
31667c478bd9Sstevel@tonic-gate 			    completion_reason, cb_flags, rval);
31677c478bd9Sstevel@tonic-gate 			freemsg(data);
31687c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 			return (rval);
31717c478bd9Sstevel@tonic-gate 		}
31727c478bd9Sstevel@tonic-gate 	}
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	/* parse the hub descriptor */
31777c478bd9Sstevel@tonic-gate 	/* only 32 ports are supported at present */
31787c478bd9Sstevel@tonic-gate 	ASSERT(*(data->b_rptr + 2) <= 32);
31797c478bd9Sstevel@tonic-gate 	if (usb_parse_CV_descr("cccscccccc",
3180d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	    data->b_rptr, MBLKL(data),
31817c478bd9Sstevel@tonic-gate 	    (void *)hub_descr, sizeof (usb_hub_descr_t)) == 0) {
31827c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
31837c478bd9Sstevel@tonic-gate 		    "parsing hub descriptor failed");
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 		freemsg(data);
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
31887c478bd9Sstevel@tonic-gate 	}
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	freemsg(data);
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
319335f36846Ssl 	    "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
319435f36846Ssl 	    "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval,
31957c478bd9Sstevel@tonic-gate 	    hub_descr->bNbrPorts, hub_descr->wHubCharacteristics,
319635f36846Ssl 	    hub_descr->bPwrOn2PwrGood, hub_descr->bHubContrCurrent);
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 	if (hub_descr->bNbrPorts > MAX_PORTS) {
31997c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
32007c478bd9Sstevel@tonic-gate 		    "Hub driver supports max of %d ports on hub. "
32017c478bd9Sstevel@tonic-gate 		    "Hence using the first %d port of %d ports available",
32027c478bd9Sstevel@tonic-gate 		    MAX_PORTS, MAX_PORTS, hub_descr->bNbrPorts);
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 		hub_descr->bNbrPorts = MAX_PORTS;
32057c478bd9Sstevel@tonic-gate 	}
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
32087c478bd9Sstevel@tonic-gate }
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 
321135f36846Ssl /*
321235f36846Ssl  * hubd_get_hub_status_words:
321335f36846Ssl  */
321435f36846Ssl static int
321535f36846Ssl hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status)
321635f36846Ssl {
321735f36846Ssl 	usb_cr_t	completion_reason;
321835f36846Ssl 	usb_cb_flags_t	cb_flags;
321935f36846Ssl 	mblk_t		*data = NULL;
322035f36846Ssl 
322135f36846Ssl 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
322235f36846Ssl 
322335f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
322435f36846Ssl 
322535f36846Ssl 	if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe,
322635f36846Ssl 	    HUB_CLASS_REQ_TYPE,
322735f36846Ssl 	    USB_REQ_GET_STATUS,
322835f36846Ssl 	    0,
322935f36846Ssl 	    0,
323035f36846Ssl 	    GET_STATUS_LENGTH,
323135f36846Ssl 	    &data, 0,
323235f36846Ssl 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
323335f36846Ssl 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
323435f36846Ssl 		    "get hub status failed: cr=%d cb=0x%x",
323535f36846Ssl 		    completion_reason, cb_flags);
323635f36846Ssl 
323735f36846Ssl 		if (data) {
323835f36846Ssl 			freemsg(data);
323935f36846Ssl 		}
324035f36846Ssl 
324135f36846Ssl 		mutex_enter(HUBD_MUTEX(hubd));
324235f36846Ssl 
324335f36846Ssl 		return (USB_FAILURE);
324435f36846Ssl 	}
324535f36846Ssl 
324635f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
324735f36846Ssl 
324835f36846Ssl 	status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
324935f36846Ssl 	status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
325035f36846Ssl 
325135f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
325235f36846Ssl 	    "hub status=0x%x change=0x%x", status[0], status[1]);
325335f36846Ssl 
325435f36846Ssl 	freemsg(data);
325535f36846Ssl 
325635f36846Ssl 	return (USB_SUCCESS);
325735f36846Ssl }
325835f36846Ssl 
325935f36846Ssl 
32607c478bd9Sstevel@tonic-gate /*
32617c478bd9Sstevel@tonic-gate  * hubd_open_intr_pipe:
32627c478bd9Sstevel@tonic-gate  *	we read all descriptors first for curiosity and then simply
32637c478bd9Sstevel@tonic-gate  *	open the pipe
32647c478bd9Sstevel@tonic-gate  */
32657c478bd9Sstevel@tonic-gate static int
32667c478bd9Sstevel@tonic-gate hubd_open_intr_pipe(hubd_t	*hubd)
32677c478bd9Sstevel@tonic-gate {
32687c478bd9Sstevel@tonic-gate 	int			rval;
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
32717c478bd9Sstevel@tonic-gate 	    "hubd_open_intr_pipe:");
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE);
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING;
32767c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(hubd->h_dip,
32797c478bd9Sstevel@tonic-gate 	    &hubd->h_ep1_descr, &hubd->h_pipe_policy,
32807c478bd9Sstevel@tonic-gate 	    0, &hubd->h_ep1_ph)) != USB_SUCCESS) {
3281d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
32827c478bd9Sstevel@tonic-gate 		    "open intr pipe failed (%d)", rval);
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
32857c478bd9Sstevel@tonic-gate 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate 		return (rval);
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
32917c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3294112116d8Sfb 	    "open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph);
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
32977c478bd9Sstevel@tonic-gate }
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate /*
33017c478bd9Sstevel@tonic-gate  * hubd_start_polling:
33027c478bd9Sstevel@tonic-gate  *	start or restart the polling
33037c478bd9Sstevel@tonic-gate  */
33047c478bd9Sstevel@tonic-gate static void
33057c478bd9Sstevel@tonic-gate hubd_start_polling(hubd_t *hubd, int always)
33067c478bd9Sstevel@tonic-gate {
33077c478bd9Sstevel@tonic-gate 	usb_intr_req_t	*reqp;
33087c478bd9Sstevel@tonic-gate 	int			rval;
33097c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	pipe_state;
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
33127c478bd9Sstevel@tonic-gate 	    "start polling: always=%d dev_state=%d pipe_state=%d\n\t"
33137c478bd9Sstevel@tonic-gate 	    "thread=%d ep1_ph=0x%p",
33147c478bd9Sstevel@tonic-gate 	    always, hubd->h_dev_state, hubd->h_intr_pipe_state,
3315112116d8Sfb 	    hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph);
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate 	/*
33187c478bd9Sstevel@tonic-gate 	 * start or restart polling on the intr pipe
33197c478bd9Sstevel@tonic-gate 	 * only if hotplug thread is not running
33207c478bd9Sstevel@tonic-gate 	 */
33217c478bd9Sstevel@tonic-gate 	if ((always == HUBD_ALWAYS_START_POLLING) ||
33227c478bd9Sstevel@tonic-gate 	    ((hubd->h_dev_state == USB_DEV_ONLINE) &&
33237c478bd9Sstevel@tonic-gate 	    (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
33247c478bd9Sstevel@tonic-gate 	    (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) {
33257c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
33267c478bd9Sstevel@tonic-gate 		    "start polling requested");
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate 		reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP);
33297c478bd9Sstevel@tonic-gate 
33307c478bd9Sstevel@tonic-gate 		reqp->intr_client_private = (usb_opaque_t)hubd;
33317c478bd9Sstevel@tonic-gate 		reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
3332c0f24e5bSlg 		    USB_ATTRS_AUTOCLEARING;
33337c478bd9Sstevel@tonic-gate 		reqp->intr_len = hubd->h_ep1_descr.wMaxPacketSize;
33347c478bd9Sstevel@tonic-gate 		reqp->intr_cb = hubd_read_cb;
33357c478bd9Sstevel@tonic-gate 		reqp->intr_exc_cb = hubd_exception_cb;
33367c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
33377c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp,
33387c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP)) != USB_SUCCESS) {
33397c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
33407c478bd9Sstevel@tonic-gate 			    "start polling failed, rval=%d", rval);
33417c478bd9Sstevel@tonic-gate 			usb_free_intr_req(reqp);
33427c478bd9Sstevel@tonic-gate 		}
33437c478bd9Sstevel@tonic-gate 
33447c478bd9Sstevel@tonic-gate 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
3345c0f24e5bSlg 		    USB_FLAGS_SLEEP);
33467c478bd9Sstevel@tonic-gate 		if (pipe_state != USB_PIPE_STATE_ACTIVE) {
33477c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
33487c478bd9Sstevel@tonic-gate 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
33497c478bd9Sstevel@tonic-gate 		}
33507c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3351112116d8Sfb 		    "start polling request 0x%p", (void *)reqp);
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
33547c478bd9Sstevel@tonic-gate 	}
33557c478bd9Sstevel@tonic-gate }
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate /*
33597c478bd9Sstevel@tonic-gate  * hubd_stop_polling
33607c478bd9Sstevel@tonic-gate  *	stop polling but do not close the pipe
33617c478bd9Sstevel@tonic-gate  */
33627c478bd9Sstevel@tonic-gate static void
33637c478bd9Sstevel@tonic-gate hubd_stop_polling(hubd_t *hubd)
33647c478bd9Sstevel@tonic-gate {
33657c478bd9Sstevel@tonic-gate 	int			rval;
33667c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	pipe_state;
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
33697c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
33707c478bd9Sstevel@tonic-gate 		    "hubd_stop_polling:");
33717c478bd9Sstevel@tonic-gate 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_STOPPED;
33727c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 		usb_pipe_stop_intr_polling(hubd->h_ep1_ph, USB_FLAGS_SLEEP);
33757c478bd9Sstevel@tonic-gate 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
3376c0f24e5bSlg 		    USB_FLAGS_SLEEP);
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate 		if (pipe_state != USB_PIPE_STATE_IDLE) {
33797c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
33807c478bd9Sstevel@tonic-gate 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
33817c478bd9Sstevel@tonic-gate 		}
33827c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
33837c478bd9Sstevel@tonic-gate 		if (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_STOPPED) {
33847c478bd9Sstevel@tonic-gate 			hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
33857c478bd9Sstevel@tonic-gate 		}
33867c478bd9Sstevel@tonic-gate 	}
33877c478bd9Sstevel@tonic-gate }
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate 
33907c478bd9Sstevel@tonic-gate /*
33917c478bd9Sstevel@tonic-gate  * hubd_close_intr_pipe:
33927c478bd9Sstevel@tonic-gate  *	close the pipe (which also stops the polling
33937c478bd9Sstevel@tonic-gate  *	and wait for the hotplug thread to exit
33947c478bd9Sstevel@tonic-gate  */
33957c478bd9Sstevel@tonic-gate static void
33967c478bd9Sstevel@tonic-gate hubd_close_intr_pipe(hubd_t *hubd)
33977c478bd9Sstevel@tonic-gate {
33987c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
33997c478bd9Sstevel@tonic-gate 	    "hubd_close_intr_pipe:");
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 	/*
34027c478bd9Sstevel@tonic-gate 	 * Now that no async operation is outstanding on pipe,
34037c478bd9Sstevel@tonic-gate 	 * we can change the state to HUBD_INTR_PIPE_CLOSING
34047c478bd9Sstevel@tonic-gate 	 */
34057c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_CLOSING;
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_hotplug_thread == 0);
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
34107c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
34117c478bd9Sstevel@tonic-gate 		usb_pipe_close(hubd->h_dip, hubd->h_ep1_ph, USB_FLAGS_SLEEP,
3412c0f24e5bSlg 		    NULL, NULL);
34137c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
34147c478bd9Sstevel@tonic-gate 		hubd->h_ep1_ph = NULL;
34157c478bd9Sstevel@tonic-gate 	}
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
34187c478bd9Sstevel@tonic-gate }
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate /*
34227c478bd9Sstevel@tonic-gate  * hubd_exception_cb
34237c478bd9Sstevel@tonic-gate  *	interrupt ep1 exception callback function.
34247c478bd9Sstevel@tonic-gate  *	this callback executes in taskq thread context and assumes
34257c478bd9Sstevel@tonic-gate  *	autoclearing
34267c478bd9Sstevel@tonic-gate  */
34277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34287c478bd9Sstevel@tonic-gate static void
34297c478bd9Sstevel@tonic-gate hubd_exception_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
34307c478bd9Sstevel@tonic-gate {
34317c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)(reqp->intr_client_private);
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
34347c478bd9Sstevel@tonic-gate 	    "hubd_exception_cb: "
3435112116d8Sfb 	    "req=0x%p cr=%d data=0x%p cb_flags=0x%x", (void *)reqp,
3436112116d8Sfb 	    reqp->intr_completion_reason, (void *)reqp->intr_data,
34377c478bd9Sstevel@tonic-gate 	    reqp->intr_cb_flags);
34387c478bd9Sstevel@tonic-gate 
34397c478bd9Sstevel@tonic-gate 	ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
34427c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate 	switch (reqp->intr_completion_reason) {
34457c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_RESET:
34467c478bd9Sstevel@tonic-gate 		/* only restart polling after autoclearing */
34477c478bd9Sstevel@tonic-gate 		if ((hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
34487c478bd9Sstevel@tonic-gate 		    (hubd->h_port_reset_wait == 0)) {
34497c478bd9Sstevel@tonic-gate 			hubd_start_polling(hubd, 0);
34507c478bd9Sstevel@tonic-gate 		}
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 		break;
34537c478bd9Sstevel@tonic-gate 	case USB_CR_DEV_NOT_RESP:
34547c478bd9Sstevel@tonic-gate 	case USB_CR_STOPPED_POLLING:
34557c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_CLOSING:
34567c478bd9Sstevel@tonic-gate 	case USB_CR_UNSPECIFIED_ERR:
34577c478bd9Sstevel@tonic-gate 		/* never restart polling on these conditions */
34587c478bd9Sstevel@tonic-gate 	default:
34597c478bd9Sstevel@tonic-gate 		/* for all others, wait for the autoclearing PIPE_RESET cb */
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 		break;
34627c478bd9Sstevel@tonic-gate 	}
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	usb_free_intr_req(reqp);
34657c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
34667c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
34677c478bd9Sstevel@tonic-gate }
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate /*
34717c478bd9Sstevel@tonic-gate  * helper function to convert LE bytes to a portmask
34727c478bd9Sstevel@tonic-gate  */
34737c478bd9Sstevel@tonic-gate static usb_port_mask_t
34747c478bd9Sstevel@tonic-gate hubd_mblk2portmask(mblk_t *data)
34757c478bd9Sstevel@tonic-gate {
3476d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	int len = min(MBLKL(data), sizeof (usb_port_mask_t));
34777c478bd9Sstevel@tonic-gate 	usb_port_mask_t rval = 0;
34787c478bd9Sstevel@tonic-gate 	int i;
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
34817c478bd9Sstevel@tonic-gate 		rval |= data->b_rptr[i] << (i * 8);
34827c478bd9Sstevel@tonic-gate 	}
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 	return (rval);
34857c478bd9Sstevel@tonic-gate }
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate /*
34897c478bd9Sstevel@tonic-gate  * hubd_read_cb:
34907c478bd9Sstevel@tonic-gate  *	interrupt ep1 callback function
34917c478bd9Sstevel@tonic-gate  *
34927c478bd9Sstevel@tonic-gate  *	the status indicates just a change on the pipe with no indication
34937c478bd9Sstevel@tonic-gate  *	of what the change was
34947c478bd9Sstevel@tonic-gate  *
34957c478bd9Sstevel@tonic-gate  *	known conditions:
34967c478bd9Sstevel@tonic-gate  *		- reset port completion
34977c478bd9Sstevel@tonic-gate  *		- connect
34987c478bd9Sstevel@tonic-gate  *		- disconnect
34997c478bd9Sstevel@tonic-gate  *
35007c478bd9Sstevel@tonic-gate  *	for handling the hotplugging, create a new thread that can do
35017c478bd9Sstevel@tonic-gate  *	synchronous usba calls
35027c478bd9Sstevel@tonic-gate  */
35037c478bd9Sstevel@tonic-gate static void
35047c478bd9Sstevel@tonic-gate hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
35057c478bd9Sstevel@tonic-gate {
35067c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)(reqp->intr_client_private);
35077c478bd9Sstevel@tonic-gate 	size_t		length;
35087c478bd9Sstevel@tonic-gate 	mblk_t		*data = reqp->intr_data;
35096c7181fcSsl 	int		mem_flag = 0;
35106c7181fcSsl 	hubd_hotplug_arg_t *arg;
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3513112116d8Sfb 	    "hubd_read_cb: ph=0x%p req=0x%p", (void *)pipe, (void *)reqp);
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 	ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate 	/*
35187c478bd9Sstevel@tonic-gate 	 * At present, we are not handling notification for completion of
35197c478bd9Sstevel@tonic-gate 	 * asynchronous pipe reset, for which this data ptr could be NULL
35207c478bd9Sstevel@tonic-gate 	 */
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	if (data == NULL) {
35237c478bd9Sstevel@tonic-gate 		usb_free_intr_req(reqp);
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 		return;
35267c478bd9Sstevel@tonic-gate 	}
35277c478bd9Sstevel@tonic-gate 
35286c7181fcSsl 	arg = (hubd_hotplug_arg_t *)kmem_zalloc(
35296c7181fcSsl 	    sizeof (hubd_hotplug_arg_t), KM_SLEEP);
35306c7181fcSsl 	mem_flag = 1;
35316c7181fcSsl 
35327c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate 	if ((hubd->h_dev_state == USB_DEV_SUSPENDED) ||
35357c478bd9Sstevel@tonic-gate 	    (hubd->h_intr_pipe_state != HUBD_INTR_PIPE_ACTIVE)) {
35367c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
35377c478bd9Sstevel@tonic-gate 		usb_free_intr_req(reqp);
35386c7181fcSsl 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 		return;
35417c478bd9Sstevel@tonic-gate 	}
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_ep1_ph == pipe);
35447c478bd9Sstevel@tonic-gate 
3545d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	length = MBLKL(data);
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 	/*
35487c478bd9Sstevel@tonic-gate 	 * Only look at the data and startup the hotplug thread if
35497c478bd9Sstevel@tonic-gate 	 * there actually is data.
35507c478bd9Sstevel@tonic-gate 	 */
35517c478bd9Sstevel@tonic-gate 	if (length != 0) {
35527c478bd9Sstevel@tonic-gate 		usb_port_mask_t port_change = hubd_mblk2portmask(data);
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 		/*
35557c478bd9Sstevel@tonic-gate 		 * if a port change was already reported and we are waiting for
35567c478bd9Sstevel@tonic-gate 		 * reset port completion then wake up the hotplug thread which
35577c478bd9Sstevel@tonic-gate 		 * should be waiting on reset port completion
35587c478bd9Sstevel@tonic-gate 		 *
35597c478bd9Sstevel@tonic-gate 		 * if there is disconnect event instead of reset completion, let
35607c478bd9Sstevel@tonic-gate 		 * the hotplug thread figure this out
35617c478bd9Sstevel@tonic-gate 		 */
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate 		/* remove the reset wait bits from the status */
35647c478bd9Sstevel@tonic-gate 		hubd->h_port_change |= port_change &
3565c0f24e5bSlg 		    ~hubd->h_port_reset_wait;
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
35687c478bd9Sstevel@tonic-gate 		    "port change=0x%x port_reset_wait=0x%x",
35697c478bd9Sstevel@tonic-gate 		    hubd->h_port_change, hubd->h_port_reset_wait);
35707c478bd9Sstevel@tonic-gate 
35717c478bd9Sstevel@tonic-gate 		/* there should be only one reset bit active at the time */
35727c478bd9Sstevel@tonic-gate 		if (hubd->h_port_reset_wait & port_change) {
35737c478bd9Sstevel@tonic-gate 			hubd->h_port_reset_wait = 0;
35747c478bd9Sstevel@tonic-gate 			cv_signal(&hubd->h_cv_reset_port);
35757c478bd9Sstevel@tonic-gate 		}
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 		/*
35787c478bd9Sstevel@tonic-gate 		 * kick off the thread only if device is ONLINE and it is not
35797c478bd9Sstevel@tonic-gate 		 * during attaching or detaching
35807c478bd9Sstevel@tonic-gate 		 */
35817c478bd9Sstevel@tonic-gate 		if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
35827c478bd9Sstevel@tonic-gate 		    (!DEVI_IS_ATTACHING(hubd->h_dip)) &&
35837c478bd9Sstevel@tonic-gate 		    (!DEVI_IS_DETACHING(hubd->h_dip)) &&
35847c478bd9Sstevel@tonic-gate 		    (hubd->h_port_change) &&
35857c478bd9Sstevel@tonic-gate 		    (hubd->h_hotplug_thread == 0)) {
35867c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
35877c478bd9Sstevel@tonic-gate 			    "creating hotplug thread: "
35887c478bd9Sstevel@tonic-gate 			    "dev_state=%d", hubd->h_dev_state);
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 			/*
35917c478bd9Sstevel@tonic-gate 			 * Mark this device as busy. The will be marked idle
35927c478bd9Sstevel@tonic-gate 			 * if the async req fails or at the exit of  hotplug
35937c478bd9Sstevel@tonic-gate 			 * thread
35947c478bd9Sstevel@tonic-gate 			 */
35957c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
35967c478bd9Sstevel@tonic-gate 
35976c7181fcSsl 			arg->hubd = hubd;
35986c7181fcSsl 			arg->hotplug_during_attach = B_FALSE;
35996c7181fcSsl 
36007c478bd9Sstevel@tonic-gate 			if (usb_async_req(hubd->h_dip,
36017c478bd9Sstevel@tonic-gate 			    hubd_hotplug_thread,
36026c7181fcSsl 			    (void *)arg, 0) == USB_SUCCESS) {
36037c478bd9Sstevel@tonic-gate 				hubd->h_hotplug_thread++;
36046c7181fcSsl 				mem_flag = 0;
36057c478bd9Sstevel@tonic-gate 			} else {
36067c478bd9Sstevel@tonic-gate 				/* mark this device as idle */
36077c478bd9Sstevel@tonic-gate 				(void) hubd_pm_idle_component(hubd,
36087c478bd9Sstevel@tonic-gate 				    hubd->h_dip, 0);
36097c478bd9Sstevel@tonic-gate 			}
36107c478bd9Sstevel@tonic-gate 		}
36117c478bd9Sstevel@tonic-gate 	}
36127c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
36137c478bd9Sstevel@tonic-gate 
36146c7181fcSsl 	if (mem_flag == 1) {
36156c7181fcSsl 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
36166c7181fcSsl 	}
36176c7181fcSsl 
36187c478bd9Sstevel@tonic-gate 	usb_free_intr_req(reqp);
36197c478bd9Sstevel@tonic-gate }
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 
36227c478bd9Sstevel@tonic-gate /*
36237c478bd9Sstevel@tonic-gate  * hubd_hotplug_thread:
36247c478bd9Sstevel@tonic-gate  *	handles resetting of port, and creating children
36257c478bd9Sstevel@tonic-gate  *
36267c478bd9Sstevel@tonic-gate  *	the ports to check are indicated in h_port_change bit mask
36277c478bd9Sstevel@tonic-gate  * XXX note that one time poll doesn't work on the root hub
36287c478bd9Sstevel@tonic-gate  */
36297c478bd9Sstevel@tonic-gate static void
36307c478bd9Sstevel@tonic-gate hubd_hotplug_thread(void *arg)
36317c478bd9Sstevel@tonic-gate {
36326c7181fcSsl 	hubd_hotplug_arg_t *hd_arg = (hubd_hotplug_arg_t *)arg;
36336c7181fcSsl 	hubd_t		*hubd = hd_arg->hubd;
36346c7181fcSsl 	boolean_t	attach_flg = hd_arg->hotplug_during_attach;
36357c478bd9Sstevel@tonic-gate 	usb_port_t	port;
36367c478bd9Sstevel@tonic-gate 	uint16_t	nports;
36377c478bd9Sstevel@tonic-gate 	uint16_t	status, change;
36387c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
36397c478bd9Sstevel@tonic-gate 	dev_info_t	*hdip = hubd->h_dip;
36407c478bd9Sstevel@tonic-gate 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
3641c0f24e5bSlg 	dev_info_t	*child_dip;
36427c478bd9Sstevel@tonic-gate 	boolean_t	online_child = B_FALSE;
36437c478bd9Sstevel@tonic-gate 	boolean_t	offline_child = B_FALSE;
36447c478bd9Sstevel@tonic-gate 	boolean_t	pwrup_child = B_FALSE;
3645c0f24e5bSlg 	int		prh_circ, rh_circ, chld_circ, circ, old_state;
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
36487c478bd9Sstevel@tonic-gate 	    "hubd_hotplug_thread:  started");
36497c478bd9Sstevel@tonic-gate 
365040482326SVincent Wang 	/*
365140482326SVincent Wang 	 * Before console is init'd, we temporarily block the hotplug
365240482326SVincent Wang 	 * threads so that BUS_CONFIG_ONE through hubd_bus_config() can be
365340482326SVincent Wang 	 * processed quickly. This reduces the time needed for vfs_mountroot()
3654*8e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	 * to mount the root FS from a USB disk. And on SPARC platform,
3655*8e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	 * in order to load 'consconfig' successfully after OBP is gone,
3656*8e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	 * we need to check 'modrootloaded' to make sure root filesystem is
3657*8e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	 * available.
365840482326SVincent Wang 	 */
3659*8e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	while (!modrootloaded || !consconfig_console_is_ready()) {
366040482326SVincent Wang 		delay(drv_usectohz(10000));
366140482326SVincent Wang 	}
366240482326SVincent Wang 
36636c7181fcSsl 	kmem_free(arg, sizeof (hubd_hotplug_arg_t));
36646c7181fcSsl 
36657c478bd9Sstevel@tonic-gate 	/*
36667c478bd9Sstevel@tonic-gate 	 * if our bus power entry point is active, process the change
36677c478bd9Sstevel@tonic-gate 	 * on the next notification of interrupt pipe
36687c478bd9Sstevel@tonic-gate 	 */
36697c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
36707c478bd9Sstevel@tonic-gate 	if (hubd->h_bus_pwr || (hubd->h_hotplug_thread > 1)) {
36717c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread--;
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 		/* mark this device as idle */
36747c478bd9Sstevel@tonic-gate 		hubd_pm_idle_component(hubd, hubd->h_dip, 0);
36757c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
36787c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: "
36797c478bd9Sstevel@tonic-gate 		    "bus_power in progress/hotplugging undesirable - quit");
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate 		return;
36827c478bd9Sstevel@tonic-gate 	}
36837c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 	ndi_hold_devi(hdip); /* so we don't race with detach */
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
36887c478bd9Sstevel@tonic-gate 
36897c478bd9Sstevel@tonic-gate 	/* is this the root hub? */
36907c478bd9Sstevel@tonic-gate 	if (hdip == rh_dip) {
36917c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_PWRED_DOWN) {
36927c478bd9Sstevel@tonic-gate 			hubpm = hubd->h_hubpm;
36937c478bd9Sstevel@tonic-gate 
36947c478bd9Sstevel@tonic-gate 			/* mark the root hub as full power */
36957c478bd9Sstevel@tonic-gate 			hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
36967c478bd9Sstevel@tonic-gate 			hubpm->hubp_time_at_full_power = ddi_get_time();
36977c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
36987c478bd9Sstevel@tonic-gate 
36997c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
37007c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: call pm_power_has_changed");
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 			(void) pm_power_has_changed(hdip, 0,
3703c0f24e5bSlg 			    USB_DEV_OS_FULL_PWR);
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
37067c478bd9Sstevel@tonic-gate 			hubd->h_dev_state = USB_DEV_ONLINE;
37077c478bd9Sstevel@tonic-gate 		}
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate 	} else {
37107c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
37117c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: not root hub");
37127c478bd9Sstevel@tonic-gate 	}
37137c478bd9Sstevel@tonic-gate 
37147c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	/*
37177c478bd9Sstevel@tonic-gate 	 * this ensures one hotplug activity per system at a time.
37187c478bd9Sstevel@tonic-gate 	 * we enter the parent PCI node to have this serialization.
37197c478bd9Sstevel@tonic-gate 	 * this also excludes ioctls and deathrow thread
37207c478bd9Sstevel@tonic-gate 	 * (a bit crude but easier to debug)
37217c478bd9Sstevel@tonic-gate 	 */
37227c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
37237c478bd9Sstevel@tonic-gate 	ndi_devi_enter(rh_dip, &rh_circ);
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 	/* exclude other threads */
37267c478bd9Sstevel@tonic-gate 	ndi_devi_enter(hdip, &circ);
37277c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
37287c478bd9Sstevel@tonic-gate 
3729e4c316c4SVincent Wang 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE);
3730e4c316c4SVincent Wang 
3731e4c316c4SVincent Wang 	nports = hubd->h_hub_descr.bNbrPorts;
3732e4c316c4SVincent Wang 
3733e4c316c4SVincent Wang 	hubd_stop_polling(hubd);
3734e4c316c4SVincent Wang 
37357c478bd9Sstevel@tonic-gate 	while ((hubd->h_dev_state == USB_DEV_ONLINE) &&
37367c478bd9Sstevel@tonic-gate 	    (hubd->h_port_change)) {
37377c478bd9Sstevel@tonic-gate 		/*
37387c478bd9Sstevel@tonic-gate 		 * The 0th bit is the hub status change bit.
37397c478bd9Sstevel@tonic-gate 		 * handle loss of local power here
37407c478bd9Sstevel@tonic-gate 		 */
37417c478bd9Sstevel@tonic-gate 		if (hubd->h_port_change & HUB_CHANGE_STATUS) {
37427c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
37437c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: hub status change!");
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate 			/*
37467c478bd9Sstevel@tonic-gate 			 * This should be handled properly.  For now,
37477c478bd9Sstevel@tonic-gate 			 * mask off the bit.
37487c478bd9Sstevel@tonic-gate 			 */
37497c478bd9Sstevel@tonic-gate 			hubd->h_port_change &= ~HUB_CHANGE_STATUS;
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate 			/*
37527c478bd9Sstevel@tonic-gate 			 * check and ack hub status
37537c478bd9Sstevel@tonic-gate 			 * this causes stall conditions
37547c478bd9Sstevel@tonic-gate 			 * when local power is removed
37557c478bd9Sstevel@tonic-gate 			 */
37567c478bd9Sstevel@tonic-gate 			(void) hubd_get_hub_status(hubd);
37577c478bd9Sstevel@tonic-gate 		}
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
37607c478bd9Sstevel@tonic-gate 			usb_port_mask_t port_mask;
37617c478bd9Sstevel@tonic-gate 			boolean_t was_connected;
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 			port_mask = 1 << port;
37647c478bd9Sstevel@tonic-gate 			was_connected =
3765c0f24e5bSlg 			    (hubd->h_port_state[port] & PORT_STATUS_CCS) &&
3766c0f24e5bSlg 			    (hubd->h_children_dips[port]);
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
37697c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: "
37707c478bd9Sstevel@tonic-gate 			    "port %d mask=0x%x change=0x%x connected=0x%x",
37717c478bd9Sstevel@tonic-gate 			    port, port_mask, hubd->h_port_change,
37727c478bd9Sstevel@tonic-gate 			    was_connected);
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 			/*
37757c478bd9Sstevel@tonic-gate 			 * is this a port connection that changed?
37767c478bd9Sstevel@tonic-gate 			 */
37777c478bd9Sstevel@tonic-gate 			if ((hubd->h_port_change & port_mask) == 0) {
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 				continue;
37807c478bd9Sstevel@tonic-gate 			}
37817c478bd9Sstevel@tonic-gate 			hubd->h_port_change &= ~port_mask;
37827c478bd9Sstevel@tonic-gate 
37837c478bd9Sstevel@tonic-gate 			/* ack all changes */
37847c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
37857c478bd9Sstevel@tonic-gate 			    &status, &change, HUBD_ACK_ALL_CHANGES);
37867c478bd9Sstevel@tonic-gate 
37877c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
37887c478bd9Sstevel@tonic-gate 			    "handle port %d:\n\t"
37897c478bd9Sstevel@tonic-gate 			    "new status=0x%x change=0x%x was_conn=0x%x ",
37907c478bd9Sstevel@tonic-gate 			    port, status, change, was_connected);
37917c478bd9Sstevel@tonic-gate 
37927c478bd9Sstevel@tonic-gate 			/* Recover a disabled port */
37937c478bd9Sstevel@tonic-gate 			if (change & PORT_CHANGE_PESC) {
37947c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
3795c0f24e5bSlg 				    hubd->h_log_handle,
3796c0f24e5bSlg 				    "port%d Disabled - "
3797c0f24e5bSlg 				    "status=0x%x, change=0x%x",
3798c0f24e5bSlg 				    port, status, change);
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 				/*
38017c478bd9Sstevel@tonic-gate 				 * if the port was connected and is still
38027c478bd9Sstevel@tonic-gate 				 * connected, recover the port
38037c478bd9Sstevel@tonic-gate 				 */
38047c478bd9Sstevel@tonic-gate 				if (was_connected && (status &
38057c478bd9Sstevel@tonic-gate 				    PORT_STATUS_CCS)) {
38067c478bd9Sstevel@tonic-gate 					online_child |=
38077c478bd9Sstevel@tonic-gate 					    (hubd_recover_disabled_port(hubd,
38087c478bd9Sstevel@tonic-gate 					    port) == USB_SUCCESS);
38097c478bd9Sstevel@tonic-gate 				}
38107c478bd9Sstevel@tonic-gate 			}
38117c478bd9Sstevel@tonic-gate 
38127c478bd9Sstevel@tonic-gate 			/*
38137c478bd9Sstevel@tonic-gate 			 * Now check what changed on the port
38147c478bd9Sstevel@tonic-gate 			 */
38156c7181fcSsl 			if ((change & PORT_CHANGE_CSC) || attach_flg) {
38167c478bd9Sstevel@tonic-gate 				if ((status & PORT_STATUS_CCS) &&
38177c478bd9Sstevel@tonic-gate 				    (!was_connected)) {
38187c478bd9Sstevel@tonic-gate 					/* new device plugged in */
38197c478bd9Sstevel@tonic-gate 					online_child |=
38207c478bd9Sstevel@tonic-gate 					    (hubd_handle_port_connect(hubd,
38217c478bd9Sstevel@tonic-gate 					    port) == USB_SUCCESS);
38227c478bd9Sstevel@tonic-gate 
38237c478bd9Sstevel@tonic-gate 				} else if ((status & PORT_STATUS_CCS) &&
38247c478bd9Sstevel@tonic-gate 				    was_connected) {
38257c478bd9Sstevel@tonic-gate 					/*
38267c478bd9Sstevel@tonic-gate 					 * In this case we can never be sure
38277c478bd9Sstevel@tonic-gate 					 * if the device indeed got hotplugged
38287c478bd9Sstevel@tonic-gate 					 * or the hub is falsely reporting the
38297c478bd9Sstevel@tonic-gate 					 * change.
38307c478bd9Sstevel@tonic-gate 					 */
3831c0f24e5bSlg 					child_dip = hubd->h_children_dips[port];
38327c478bd9Sstevel@tonic-gate 
3833c0f24e5bSlg 					mutex_exit(HUBD_MUTEX(hubd));
38347c478bd9Sstevel@tonic-gate 					/*
3835c0f24e5bSlg 					 * this ensures we do not race with
3836c0f24e5bSlg 					 * other threads which are detaching
3837c0f24e5bSlg 					 * the child driver at the same time.
38387c478bd9Sstevel@tonic-gate 					 */
3839c0f24e5bSlg 					ndi_devi_enter(child_dip, &chld_circ);
3840c0f24e5bSlg 					/*
3841c0f24e5bSlg 					 * Now check if the driver remains
3842c0f24e5bSlg 					 * attached.
3843c0f24e5bSlg 					 */
3844c0f24e5bSlg 					if (i_ddi_devi_attached(child_dip)) {
3845c0f24e5bSlg 						/*
3846c0f24e5bSlg 						 * first post a disconnect event
3847c0f24e5bSlg 						 * to the child.
3848c0f24e5bSlg 						 */
3849c0f24e5bSlg 						hubd_post_event(hubd, port,
3850c0f24e5bSlg 						    USBA_EVENT_TAG_HOT_REMOVAL);
3851c0f24e5bSlg 						mutex_enter(HUBD_MUTEX(hubd));
3852c0f24e5bSlg 
3853c0f24e5bSlg 						/*
3854c0f24e5bSlg 						 * then reset the port and
3855c0f24e5bSlg 						 * recover the device
3856c0f24e5bSlg 						 */
3857c0f24e5bSlg 						online_child |=
3858c0f24e5bSlg 						    (hubd_handle_port_connect(
3859c0f24e5bSlg 						    hubd, port) == USB_SUCCESS);
3860c0f24e5bSlg 
3861c0f24e5bSlg 						mutex_exit(HUBD_MUTEX(hubd));
3862c0f24e5bSlg 					}
3863c0f24e5bSlg 
3864c0f24e5bSlg 					ndi_devi_exit(child_dip, chld_circ);
3865c0f24e5bSlg 					mutex_enter(HUBD_MUTEX(hubd));
38667c478bd9Sstevel@tonic-gate 				} else if (was_connected) {
38677c478bd9Sstevel@tonic-gate 					/* this is a disconnect */
38687c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
38697c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
38707c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_REMOVAL);
38717c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 					offline_child = B_TRUE;
38747c478bd9Sstevel@tonic-gate 				}
38757c478bd9Sstevel@tonic-gate 			}
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 			/*
38787c478bd9Sstevel@tonic-gate 			 * Check if any port is coming out of suspend
38797c478bd9Sstevel@tonic-gate 			 */
38807c478bd9Sstevel@tonic-gate 			if (change & PORT_CHANGE_PSSC) {
38817c478bd9Sstevel@tonic-gate 				/* a resuming device could have disconnected */
38827c478bd9Sstevel@tonic-gate 				if (was_connected &&
38837c478bd9Sstevel@tonic-gate 				    hubd->h_children_dips[port]) {
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate 					/* device on this port resuming */
38867c478bd9Sstevel@tonic-gate 					dev_info_t *dip;
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate 					dip = hubd->h_children_dips[port];
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 					/*
38917c478bd9Sstevel@tonic-gate 					 * Don't raise power on detaching child
38927c478bd9Sstevel@tonic-gate 					 */
38937c478bd9Sstevel@tonic-gate 					if (!DEVI_IS_DETACHING(dip)) {
38947c478bd9Sstevel@tonic-gate 						/*
38957c478bd9Sstevel@tonic-gate 						 * As this child is not
38967c478bd9Sstevel@tonic-gate 						 * detaching, we set this
38977c478bd9Sstevel@tonic-gate 						 * flag, causing bus_ctls
38987c478bd9Sstevel@tonic-gate 						 * to stall detach till
38997c478bd9Sstevel@tonic-gate 						 * pm_raise_power returns
39007c478bd9Sstevel@tonic-gate 						 * and flag it for a deferred
39017c478bd9Sstevel@tonic-gate 						 * raise_power.
39027c478bd9Sstevel@tonic-gate 						 *
39037c478bd9Sstevel@tonic-gate 						 * pm_raise_power is deferred
39047c478bd9Sstevel@tonic-gate 						 * because we need to release
39057c478bd9Sstevel@tonic-gate 						 * the locks first.
39067c478bd9Sstevel@tonic-gate 						 */
39077c478bd9Sstevel@tonic-gate 						hubd->h_port_state[port] |=
3908c0f24e5bSlg 						    HUBD_CHILD_RAISE_POWER;
39097c478bd9Sstevel@tonic-gate 						pwrup_child = B_TRUE;
39107c478bd9Sstevel@tonic-gate 						mutex_exit(HUBD_MUTEX(hubd));
39117c478bd9Sstevel@tonic-gate 
39127c478bd9Sstevel@tonic-gate 						/*
39137c478bd9Sstevel@tonic-gate 						 * make sure that child
39147c478bd9Sstevel@tonic-gate 						 * doesn't disappear
39157c478bd9Sstevel@tonic-gate 						 */
39167c478bd9Sstevel@tonic-gate 						ndi_hold_devi(dip);
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate 						mutex_enter(HUBD_MUTEX(hubd));
39197c478bd9Sstevel@tonic-gate 					}
39207c478bd9Sstevel@tonic-gate 				}
39217c478bd9Sstevel@tonic-gate 			}
3922fef1e07eSsl 
3923fef1e07eSsl 			/*
3924fef1e07eSsl 			 * Check if the port is over-current
3925fef1e07eSsl 			 */
3926fef1e07eSsl 			if (change & PORT_CHANGE_OCIC) {
3927fef1e07eSsl 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
3928fef1e07eSsl 				    hubd->h_log_handle,
3929fef1e07eSsl 				    "Port%d in over current condition, "
3930fef1e07eSsl 				    "please check the attached device to "
3931fef1e07eSsl 				    "clear the condition. The system will "
3932fef1e07eSsl 				    "try to recover the port, but if not "
3933fef1e07eSsl 				    "successful, you need to re-connect "
3934fef1e07eSsl 				    "the hub or reboot the system to bring "
3935fef1e07eSsl 				    "the port back to work", port);
3936fef1e07eSsl 
3937fef1e07eSsl 				if (!(status & PORT_STATUS_PPS)) {
3938fef1e07eSsl 					/*
3939fef1e07eSsl 					 * Try to enable port power, but
3940fef1e07eSsl 					 * possibly fail. Ignore failure
3941fef1e07eSsl 					 */
3942fef1e07eSsl 					(void) hubd_enable_port_power(hubd,
3943fef1e07eSsl 					    port);
3944fef1e07eSsl 
3945fef1e07eSsl 					/*
3946fef1e07eSsl 					 * Delay some time to avoid
3947fef1e07eSsl 					 * over-current event to happen
3948fef1e07eSsl 					 * too frequently in some cases
3949fef1e07eSsl 					 */
3950fef1e07eSsl 					mutex_exit(HUBD_MUTEX(hubd));
3951fef1e07eSsl 					delay(drv_usectohz(500000));
3952fef1e07eSsl 					mutex_enter(HUBD_MUTEX(hubd));
3953fef1e07eSsl 				}
3954fef1e07eSsl 			}
39557c478bd9Sstevel@tonic-gate 		}
39567c478bd9Sstevel@tonic-gate 	}
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate 	/* release locks so we can do a devfs_clean */
39597c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
39607c478bd9Sstevel@tonic-gate 
39617c478bd9Sstevel@tonic-gate 	/* delete cached dv_node's but drop locks first */
39627c478bd9Sstevel@tonic-gate 	ndi_devi_exit(hdip, circ);
39637c478bd9Sstevel@tonic-gate 	ndi_devi_exit(rh_dip, rh_circ);
39647c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate 	(void) devfs_clean(rh_dip, NULL, 0);
39677c478bd9Sstevel@tonic-gate 
39687c478bd9Sstevel@tonic-gate 	/* now check if any children need onlining */
39697c478bd9Sstevel@tonic-gate 	if (online_child) {
39707c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
39717c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: onlining children");
39727c478bd9Sstevel@tonic-gate 
397340482326SVincent Wang 		(void) ndi_devi_online(hubd->h_dip, 0);
39747c478bd9Sstevel@tonic-gate 	}
39757c478bd9Sstevel@tonic-gate 
39767c478bd9Sstevel@tonic-gate 	/* now check if any disconnected devices need to be cleaned up */
39777c478bd9Sstevel@tonic-gate 	if (offline_child) {
39787c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
39797c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: scheduling cleanup");
39807c478bd9Sstevel@tonic-gate 
39817c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
39827c478bd9Sstevel@tonic-gate 	}
39837c478bd9Sstevel@tonic-gate 
39847c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
39857c478bd9Sstevel@tonic-gate 
39867c478bd9Sstevel@tonic-gate 	/* now raise power on the children that have woken up */
39877c478bd9Sstevel@tonic-gate 	if (pwrup_child) {
39887c478bd9Sstevel@tonic-gate 		old_state = hubd->h_dev_state;
39897c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_HUB_CHILD_PWRLVL;
39907c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
39917c478bd9Sstevel@tonic-gate 			if (hubd->h_port_state[port] & HUBD_CHILD_RAISE_POWER) {
39927c478bd9Sstevel@tonic-gate 				dev_info_t *dip = hubd->h_children_dips[port];
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate 				/* Get the device to full power */
39977c478bd9Sstevel@tonic-gate 				(void) pm_busy_component(dip, 0);
39987c478bd9Sstevel@tonic-gate 				(void) pm_raise_power(dip, 0,
39997c478bd9Sstevel@tonic-gate 				    USB_DEV_OS_FULL_PWR);
40007c478bd9Sstevel@tonic-gate 				(void) pm_idle_component(dip, 0);
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate 				/* release the hold on the child */
40037c478bd9Sstevel@tonic-gate 				ndi_rele_devi(dip);
40047c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
40057c478bd9Sstevel@tonic-gate 				hubd->h_port_state[port] &=
40067c478bd9Sstevel@tonic-gate 				    ~HUBD_CHILD_RAISE_POWER;
40077c478bd9Sstevel@tonic-gate 			}
40087c478bd9Sstevel@tonic-gate 		}
40097c478bd9Sstevel@tonic-gate 		/*
40107c478bd9Sstevel@tonic-gate 		 * make sure that we don't accidentally
40117c478bd9Sstevel@tonic-gate 		 * over write the disconnect state
40127c478bd9Sstevel@tonic-gate 		 */
40137c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_HUB_CHILD_PWRLVL) {
40147c478bd9Sstevel@tonic-gate 			hubd->h_dev_state = old_state;
40157c478bd9Sstevel@tonic-gate 		}
40167c478bd9Sstevel@tonic-gate 	}
40177c478bd9Sstevel@tonic-gate 
40187c478bd9Sstevel@tonic-gate 	/*
40197c478bd9Sstevel@tonic-gate 	 * start polling can immediately kick off read callback
40207c478bd9Sstevel@tonic-gate 	 * we need to set the h_hotplug_thread to 0 so that
40217c478bd9Sstevel@tonic-gate 	 * the callback is not dropped
4022ffcd51f3Slg 	 *
4023ffcd51f3Slg 	 * if there is device during reset, still stop polling to avoid the
4024ffcd51f3Slg 	 * read callback interrupting the reset, the polling will be started
4025ffcd51f3Slg 	 * in hubd_reset_thread.
40267c478bd9Sstevel@tonic-gate 	 */
4027ffcd51f3Slg 	for (port = 1; port <= MAX_PORTS; port++) {
4028ffcd51f3Slg 		if (hubd->h_reset_port[port]) {
4029ffcd51f3Slg 
4030ffcd51f3Slg 			break;
4031ffcd51f3Slg 		}
4032ffcd51f3Slg 	}
4033ffcd51f3Slg 	if (port > MAX_PORTS) {
4034ffcd51f3Slg 		hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
4035ffcd51f3Slg 	}
40367c478bd9Sstevel@tonic-gate 
40377c478bd9Sstevel@tonic-gate 	/*
40387c478bd9Sstevel@tonic-gate 	 * Earlier we would set the h_hotplug_thread = 0 before
40397c478bd9Sstevel@tonic-gate 	 * polling was restarted  so that
40407c478bd9Sstevel@tonic-gate 	 * if there is any root hub status change interrupt, we can still kick
40417c478bd9Sstevel@tonic-gate 	 * off the hotplug thread. This was valid when this interrupt was
40427c478bd9Sstevel@tonic-gate 	 * delivered in hardware, and only ONE interrupt would be delivered.
40437c478bd9Sstevel@tonic-gate 	 * Now that we poll on the root hub looking for status change in
40447c478bd9Sstevel@tonic-gate 	 * software, this assignment is no longer required.
40457c478bd9Sstevel@tonic-gate 	 */
40467c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 	/* mark this device as idle */
40497c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
40507c478bd9Sstevel@tonic-gate 
4051ffcd51f3Slg 	cv_broadcast(&hubd->h_cv_hotplug_dev);
4052ffcd51f3Slg 
40537c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
40547c478bd9Sstevel@tonic-gate 	    "hubd_hotplug_thread: exit");
40557c478bd9Sstevel@tonic-gate 
40567c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	ndi_rele_devi(hdip);
40597c478bd9Sstevel@tonic-gate }
40607c478bd9Sstevel@tonic-gate 
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate /*
40637c478bd9Sstevel@tonic-gate  * hubd_handle_port_connect:
40647c478bd9Sstevel@tonic-gate  *	Transition a port from Disabled to Enabled.  Ensure that the
40657c478bd9Sstevel@tonic-gate  *	port is in the correct state before attempting to
40667c478bd9Sstevel@tonic-gate  *	access the device.
40677c478bd9Sstevel@tonic-gate  */
40687c478bd9Sstevel@tonic-gate static int
40697c478bd9Sstevel@tonic-gate hubd_handle_port_connect(hubd_t *hubd, usb_port_t port)
40707c478bd9Sstevel@tonic-gate {
40717c478bd9Sstevel@tonic-gate 	int			rval;
40727c478bd9Sstevel@tonic-gate 	int			retry;
40737c478bd9Sstevel@tonic-gate 	long			time_delay;
40747c478bd9Sstevel@tonic-gate 	long			settling_time;
40757c478bd9Sstevel@tonic-gate 	uint16_t		status;
40767c478bd9Sstevel@tonic-gate 	uint16_t		change;
40777c478bd9Sstevel@tonic-gate 	usb_addr_t		hubd_usb_addr;
40787c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
40797c478bd9Sstevel@tonic-gate 	usb_port_status_t	port_status = 0;
40807c478bd9Sstevel@tonic-gate 	usb_port_status_t	hub_port_status = 0;
40817c478bd9Sstevel@tonic-gate 
40827c478bd9Sstevel@tonic-gate 	/* Get the hub address and port status */
40837c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_device;
40847c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
40857c478bd9Sstevel@tonic-gate 	hubd_usb_addr = usba_device->usb_addr;
40867c478bd9Sstevel@tonic-gate 	hub_port_status = usba_device->usb_port_status;
40877c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 	/*
40907c478bd9Sstevel@tonic-gate 	 * If a device is connected, transition the
40917c478bd9Sstevel@tonic-gate 	 * port from Disabled to the Enabled state.
40927c478bd9Sstevel@tonic-gate 	 * The device will receive downstream packets
40937c478bd9Sstevel@tonic-gate 	 * in the Enabled state.
40947c478bd9Sstevel@tonic-gate 	 *
40957c478bd9Sstevel@tonic-gate 	 * reset port and wait for the hub to report
40967c478bd9Sstevel@tonic-gate 	 * completion
40977c478bd9Sstevel@tonic-gate 	 */
40987c478bd9Sstevel@tonic-gate 	change = status = 0;
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	/*
41017c478bd9Sstevel@tonic-gate 	 * According to section 9.1.2 of USB 2.0 spec, the host should
41027c478bd9Sstevel@tonic-gate 	 * wait for atleast 100ms to allow completion of an insertion
41037c478bd9Sstevel@tonic-gate 	 * process and for power at the device to become stable.
41047c478bd9Sstevel@tonic-gate 	 * We wait for 200 ms
41057c478bd9Sstevel@tonic-gate 	 */
41067c478bd9Sstevel@tonic-gate 	settling_time = drv_usectohz(hubd_device_delay / 5);
41077c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
41087c478bd9Sstevel@tonic-gate 	delay(settling_time);
41097c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate 	/* calculate 600 ms delay time */
41127c478bd9Sstevel@tonic-gate 	time_delay = (6 * drv_usectohz(hubd_device_delay)) / 10;
41137c478bd9Sstevel@tonic-gate 
41147c478bd9Sstevel@tonic-gate 	for (retry = 0; (hubd->h_dev_state == USB_DEV_ONLINE) &&
41157c478bd9Sstevel@tonic-gate 	    (retry < hubd_retry_enumerate); retry++) {
41167c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
41177c478bd9Sstevel@tonic-gate 		    "resetting port%d, retry=%d", port, retry);
41187c478bd9Sstevel@tonic-gate 
41197c478bd9Sstevel@tonic-gate 		if ((rval = hubd_reset_port(hubd, port)) != USB_SUCCESS) {
41207c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd,
41217c478bd9Sstevel@tonic-gate 			    port, &status, &change, 0);
41227c478bd9Sstevel@tonic-gate 
41237c478bd9Sstevel@tonic-gate 			/* continue only if port is still connected */
41247c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
41257c478bd9Sstevel@tonic-gate 				continue;
41267c478bd9Sstevel@tonic-gate 			}
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate 			/* carry on regardless */
41297c478bd9Sstevel@tonic-gate 		}
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate 		/*
41327c478bd9Sstevel@tonic-gate 		 * according to USB 2.0 spec section 11.24.2.7.1.2
41337c478bd9Sstevel@tonic-gate 		 * at the end of port reset, the hub enables the port.
41347c478bd9Sstevel@tonic-gate 		 * But for some strange reasons, uhci port remains disabled.
41357c478bd9Sstevel@tonic-gate 		 * And because the port remains disabled for the settling
41367c478bd9Sstevel@tonic-gate 		 * time below, the device connected to the port gets wedged
41377c478bd9Sstevel@tonic-gate 		 * - fails to enumerate (device not responding)
41387c478bd9Sstevel@tonic-gate 		 * Hence, we enable it here immediately and later again after
41397c478bd9Sstevel@tonic-gate 		 * the delay
41407c478bd9Sstevel@tonic-gate 		 */
41417c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port(hubd, port);
41427c478bd9Sstevel@tonic-gate 
41437c478bd9Sstevel@tonic-gate 		/* we skip this delay in the first iteration */
41447c478bd9Sstevel@tonic-gate 		if (retry) {
41457c478bd9Sstevel@tonic-gate 			/*
41467c478bd9Sstevel@tonic-gate 			 * delay for device to signal disconnect/connect so
41477c478bd9Sstevel@tonic-gate 			 * that hub properly recognizes the speed of the device
41487c478bd9Sstevel@tonic-gate 			 */
41497c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
41507c478bd9Sstevel@tonic-gate 			delay(settling_time);
41517c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
41527c478bd9Sstevel@tonic-gate 
41537c478bd9Sstevel@tonic-gate 			/*
41547c478bd9Sstevel@tonic-gate 			 * When a low speed device is connected to any port of
41557c478bd9Sstevel@tonic-gate 			 * PPX it has to be explicitly enabled
41567c478bd9Sstevel@tonic-gate 			 * Also, if device intentionally signals
41577c478bd9Sstevel@tonic-gate 			 * disconnect/connect, it will disable the port.
41587c478bd9Sstevel@tonic-gate 			 * So enable it again.
41597c478bd9Sstevel@tonic-gate 			 */
41607c478bd9Sstevel@tonic-gate 			(void) hubd_enable_port(hubd, port);
41617c478bd9Sstevel@tonic-gate 		}
41627c478bd9Sstevel@tonic-gate 
41637c478bd9Sstevel@tonic-gate 		if ((rval = hubd_determine_port_status(hubd, port, &status,
41647c478bd9Sstevel@tonic-gate 		    &change, 0)) != USB_SUCCESS) {
41657c478bd9Sstevel@tonic-gate 
4166d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
41677c478bd9Sstevel@tonic-gate 			    "getting status failed (%d)", rval);
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 			(void) hubd_disable_port(hubd, port);
41707c478bd9Sstevel@tonic-gate 
41717c478bd9Sstevel@tonic-gate 			continue;
41727c478bd9Sstevel@tonic-gate 		}
41737c478bd9Sstevel@tonic-gate 
41747c478bd9Sstevel@tonic-gate 		if (status & PORT_STATUS_POCI) {
4175d291d9f2Sfrits 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
41767c478bd9Sstevel@tonic-gate 			    "port %d overcurrent", port);
41777c478bd9Sstevel@tonic-gate 
41787c478bd9Sstevel@tonic-gate 			(void) hubd_disable_port(hubd, port);
41797c478bd9Sstevel@tonic-gate 
41807c478bd9Sstevel@tonic-gate 			/* ack changes */
41817c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd,
41827c478bd9Sstevel@tonic-gate 			    port, &status, &change, PORT_CHANGE_OCIC);
41837c478bd9Sstevel@tonic-gate 
41847c478bd9Sstevel@tonic-gate 			continue;
41857c478bd9Sstevel@tonic-gate 		}
41867c478bd9Sstevel@tonic-gate 
41877c478bd9Sstevel@tonic-gate 		/* is status really OK? */
41887c478bd9Sstevel@tonic-gate 		if ((status & PORT_STATUS_OK) != PORT_STATUS_OK) {
41897c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
41907c478bd9Sstevel@tonic-gate 			    "port %d status (0x%x) not OK on retry %d",
41917c478bd9Sstevel@tonic-gate 			    port, status, retry);
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate 			/* check if we still have the connection */
41947c478bd9Sstevel@tonic-gate 			if (!(status & PORT_STATUS_CCS)) {
41957c478bd9Sstevel@tonic-gate 				/* lost connection, set exit condition */
41967c478bd9Sstevel@tonic-gate 				retry = hubd_retry_enumerate;
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate 				break;
41997c478bd9Sstevel@tonic-gate 			}
42007c478bd9Sstevel@tonic-gate 		} else {
42017c478bd9Sstevel@tonic-gate 			/*
42027c478bd9Sstevel@tonic-gate 			 * Determine if the device is high or full
42037c478bd9Sstevel@tonic-gate 			 * or low speed.
42047c478bd9Sstevel@tonic-gate 			 */
42057c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_LSDA) {
42067c478bd9Sstevel@tonic-gate 				port_status = USBA_LOW_SPEED_DEV;
42077c478bd9Sstevel@tonic-gate 			} else if (status & PORT_STATUS_HSDA) {
42087c478bd9Sstevel@tonic-gate 				port_status = USBA_HIGH_SPEED_DEV;
42097c478bd9Sstevel@tonic-gate 			} else {
42107c478bd9Sstevel@tonic-gate 				port_status = USBA_FULL_SPEED_DEV;
42117c478bd9Sstevel@tonic-gate 			}
42127c478bd9Sstevel@tonic-gate 
42137c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
42147c478bd9Sstevel@tonic-gate 			    "creating child port%d, status=0x%x "
42157c478bd9Sstevel@tonic-gate 			    "port status=0x%x",
42167c478bd9Sstevel@tonic-gate 			    port, status, port_status);
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 			/*
42197c478bd9Sstevel@tonic-gate 			 * if the child already exists, set addrs and config
42207c478bd9Sstevel@tonic-gate 			 * to the device post connect event to the child
42217c478bd9Sstevel@tonic-gate 			 */
42227c478bd9Sstevel@tonic-gate 			if (hubd->h_children_dips[port]) {
42237c478bd9Sstevel@tonic-gate 				/* set addrs to this device */
42247c478bd9Sstevel@tonic-gate 				rval = hubd_setdevaddr(hubd, port);
42257c478bd9Sstevel@tonic-gate 
42267c478bd9Sstevel@tonic-gate 				/*
42277c478bd9Sstevel@tonic-gate 				 * This delay is important for the CATC hub
42287c478bd9Sstevel@tonic-gate 				 * to enumerate. But, avoid delay in the first
42297c478bd9Sstevel@tonic-gate 				 * iteration
42307c478bd9Sstevel@tonic-gate 				 */
42317c478bd9Sstevel@tonic-gate 				if (retry) {
42327c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
42337c478bd9Sstevel@tonic-gate 					delay(drv_usectohz(
42347c478bd9Sstevel@tonic-gate 					    hubd_device_delay/100));
42357c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
42367c478bd9Sstevel@tonic-gate 				}
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 				if (rval == USB_SUCCESS) {
4239ffcd51f3Slg 					/*
4240ffcd51f3Slg 					 * if the port is resetting, check if
4241ffcd51f3Slg 					 * device's descriptors have changed.
4242ffcd51f3Slg 					 */
4243ffcd51f3Slg 					if ((hubd->h_reset_port[port]) &&
4244ffcd51f3Slg 					    (hubd_check_same_device(hubd,
4245ffcd51f3Slg 					    port) != USB_SUCCESS)) {
4246ffcd51f3Slg 						retry = hubd_retry_enumerate;
4247ffcd51f3Slg 
4248ffcd51f3Slg 						break;
4249ffcd51f3Slg 					}
4250ffcd51f3Slg 
42517c478bd9Sstevel@tonic-gate 					/*
42527c478bd9Sstevel@tonic-gate 					 * set the default config for
42537c478bd9Sstevel@tonic-gate 					 * this device
42547c478bd9Sstevel@tonic-gate 					 */
42557c478bd9Sstevel@tonic-gate 					hubd_setdevconfig(hubd, port);
42567c478bd9Sstevel@tonic-gate 
4257ffcd51f3Slg 					/*
4258ffcd51f3Slg 					 * if we are doing Default reset, do
4259ffcd51f3Slg 					 * not post reconnect event since we
4260ffcd51f3Slg 					 * don't know where reset function is
4261ffcd51f3Slg 					 * called.
4262ffcd51f3Slg 					 */
4263ffcd51f3Slg 					if (hubd->h_reset_port[port]) {
4264ffcd51f3Slg 
4265ffcd51f3Slg 						return (USB_SUCCESS);
4266ffcd51f3Slg 					}
4267ffcd51f3Slg 
42687c478bd9Sstevel@tonic-gate 					/*
42697c478bd9Sstevel@tonic-gate 					 * indicate to the child that
42707c478bd9Sstevel@tonic-gate 					 * it is online again
42717c478bd9Sstevel@tonic-gate 					 */
42727c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
42737c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
42747c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_INSERTION);
42757c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
42767c478bd9Sstevel@tonic-gate 
42777c478bd9Sstevel@tonic-gate 					return (USB_SUCCESS);
42787c478bd9Sstevel@tonic-gate 				}
42797c478bd9Sstevel@tonic-gate 			} else {
42807c478bd9Sstevel@tonic-gate 				/*
42817c478bd9Sstevel@tonic-gate 				 * We need to release access here
42827c478bd9Sstevel@tonic-gate 				 * so that busctls on other ports can
42837c478bd9Sstevel@tonic-gate 				 * continue and don't cause a deadlock
42847c478bd9Sstevel@tonic-gate 				 * when busctl and removal of prom node
42857c478bd9Sstevel@tonic-gate 				 * takes concurrently. This also ensures
42867c478bd9Sstevel@tonic-gate 				 * busctls for attach of successfully
42877c478bd9Sstevel@tonic-gate 				 * enumerated devices on other ports can
42887c478bd9Sstevel@tonic-gate 				 * continue concurrently with the process
42897c478bd9Sstevel@tonic-gate 				 * of enumerating the new devices. This
42907c478bd9Sstevel@tonic-gate 				 * reduces the overall boot time of the system.
42917c478bd9Sstevel@tonic-gate 				 */
42927c478bd9Sstevel@tonic-gate 				rval = hubd_create_child(hubd->h_dip,
4293c0f24e5bSlg 				    hubd,
4294c0f24e5bSlg 				    hubd->h_usba_device,
4295c0f24e5bSlg 				    port_status, port,
4296c0f24e5bSlg 				    retry);
42977c478bd9Sstevel@tonic-gate 				if (rval == USB_SUCCESS) {
42987c478bd9Sstevel@tonic-gate 					usba_update_hotplug_stats(hubd->h_dip,
42997c478bd9Sstevel@tonic-gate 					    USBA_TOTAL_HOTPLUG_SUCCESS|
43007c478bd9Sstevel@tonic-gate 					    USBA_HOTPLUG_SUCCESS);
43017c478bd9Sstevel@tonic-gate 					hubd->h_total_hotplug_success++;
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 					if (retry > 0) {
4304d291d9f2Sfrits 						USB_DPRINTF_L2(
43057c478bd9Sstevel@tonic-gate 						    DPRINT_MASK_HOTPLUG,
43067c478bd9Sstevel@tonic-gate 						    hubd->h_log_handle,
43077c478bd9Sstevel@tonic-gate 						    "device on port %d "
43087c478bd9Sstevel@tonic-gate 						    "enumerated after %d %s",
43097c478bd9Sstevel@tonic-gate 						    port, retry,
43107c478bd9Sstevel@tonic-gate 						    (retry > 1) ? "retries" :
43117c478bd9Sstevel@tonic-gate 						    "retry");
43127c478bd9Sstevel@tonic-gate 
43137c478bd9Sstevel@tonic-gate 					}
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate 					return (USB_SUCCESS);
43167c478bd9Sstevel@tonic-gate 				}
43177c478bd9Sstevel@tonic-gate 			}
43187c478bd9Sstevel@tonic-gate 		}
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate 		/* wait a while until it settles? */
43217c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
43227c478bd9Sstevel@tonic-gate 		    "disabling port %d again", port);
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
43257c478bd9Sstevel@tonic-gate 		if (retry) {
43267c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
43277c478bd9Sstevel@tonic-gate 			delay(time_delay);
43287c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
43297c478bd9Sstevel@tonic-gate 		}
43307c478bd9Sstevel@tonic-gate 
43317c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
43327c478bd9Sstevel@tonic-gate 		    "retrying on port %d", port);
43337c478bd9Sstevel@tonic-gate 	}
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate 	if (retry >= hubd_retry_enumerate) {
43367c478bd9Sstevel@tonic-gate 		/*
43377c478bd9Sstevel@tonic-gate 		 * If it is a High Speed Root Hub and connected device
43387c478bd9Sstevel@tonic-gate 		 * Is a Low/Full Speed, it will be handled by USB 1.1
43397c478bd9Sstevel@tonic-gate 		 * Host Controller. In this case, USB 2.0 Host Controller
43407c478bd9Sstevel@tonic-gate 		 * will transfer the ownership of this port to USB 1.1
43417c478bd9Sstevel@tonic-gate 		 * Host Controller. So don't display any error message on
43427c478bd9Sstevel@tonic-gate 		 * the console.
43437c478bd9Sstevel@tonic-gate 		 */
43447c478bd9Sstevel@tonic-gate 		if ((hubd_usb_addr == ROOT_HUB_ADDR) &&
43457c478bd9Sstevel@tonic-gate 		    (hub_port_status == USBA_HIGH_SPEED_DEV) &&
43467c478bd9Sstevel@tonic-gate 		    (port_status != USBA_HIGH_SPEED_DEV)) {
43477c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
43487c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
43497c478bd9Sstevel@tonic-gate 			    "hubd_handle_port_connect: Low/Full speed "
43507c478bd9Sstevel@tonic-gate 			    "device is connected to High Speed root hub");
43517c478bd9Sstevel@tonic-gate 		} else {
43527c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
43537c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
43547c478bd9Sstevel@tonic-gate 			    "Connecting device on port %d failed", port);
43557c478bd9Sstevel@tonic-gate 		}
43567c478bd9Sstevel@tonic-gate 
43577c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
43587c478bd9Sstevel@tonic-gate 		usba_update_hotplug_stats(hubd->h_dip,
43597c478bd9Sstevel@tonic-gate 		    USBA_TOTAL_HOTPLUG_FAILURE|USBA_HOTPLUG_FAILURE);
43607c478bd9Sstevel@tonic-gate 		hubd->h_total_hotplug_failure++;
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 		/*
43637c478bd9Sstevel@tonic-gate 		 * the port should be automagically
43647c478bd9Sstevel@tonic-gate 		 * disabled but just in case, we do
43657c478bd9Sstevel@tonic-gate 		 * it here
43667c478bd9Sstevel@tonic-gate 		 */
43677c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 		/* ack all changes because we disabled this port */
43707c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd,
43717c478bd9Sstevel@tonic-gate 		    port, &status, &change, HUBD_ACK_ALL_CHANGES);
43727c478bd9Sstevel@tonic-gate 
43737c478bd9Sstevel@tonic-gate 	}
43747c478bd9Sstevel@tonic-gate 
43757c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
43767c478bd9Sstevel@tonic-gate }
43777c478bd9Sstevel@tonic-gate 
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate /*
43807c478bd9Sstevel@tonic-gate  * hubd_get_hub_status:
43817c478bd9Sstevel@tonic-gate  */
43827c478bd9Sstevel@tonic-gate static int
43837c478bd9Sstevel@tonic-gate hubd_get_hub_status(hubd_t *hubd)
43847c478bd9Sstevel@tonic-gate {
43857c478bd9Sstevel@tonic-gate 	int		rval;
43867c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
43877c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
438835f36846Ssl 	uint16_t	stword[2];
43897c478bd9Sstevel@tonic-gate 	uint16_t	status;
43907c478bd9Sstevel@tonic-gate 	uint16_t	change;
43917c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	cfg_descr;
43927c478bd9Sstevel@tonic-gate 	size_t		cfg_length;
43937c478bd9Sstevel@tonic-gate 	uchar_t		*usb_cfg;
43947c478bd9Sstevel@tonic-gate 	uint8_t		MaxPower;
4395fef1e07eSsl 	usb_hub_descr_t	*hub_descr;
4396fef1e07eSsl 	usb_port_t	port;
43977c478bd9Sstevel@tonic-gate 
4398fef1e07eSsl 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
439935f36846Ssl 	    "hubd_get_hub_status:");
44007c478bd9Sstevel@tonic-gate 
440135f36846Ssl 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
44027c478bd9Sstevel@tonic-gate 
440335f36846Ssl 	if ((hubd_get_hub_status_words(hubd, stword)) != USB_SUCCESS) {
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
44067c478bd9Sstevel@tonic-gate 	}
440735f36846Ssl 	status = stword[0];
440835f36846Ssl 	change = stword[1];
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
44117c478bd9Sstevel@tonic-gate 
44127c478bd9Sstevel@tonic-gate 	/* Obtain the raw configuration descriptor */
44137c478bd9Sstevel@tonic-gate 	usb_cfg = usb_get_raw_cfg_data(hubd->h_dip, &cfg_length);
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate 	/* get configuration descriptor */
44167c478bd9Sstevel@tonic-gate 	rval = usb_parse_cfg_descr(usb_cfg, cfg_length,
4417c0f24e5bSlg 	    &cfg_descr, USB_CFG_DESCR_SIZE);
44187c478bd9Sstevel@tonic-gate 
44197c478bd9Sstevel@tonic-gate 	if (rval != USB_CFG_DESCR_SIZE) {
44207c478bd9Sstevel@tonic-gate 
4421fef1e07eSsl 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
44227c478bd9Sstevel@tonic-gate 		    "get hub configuration descriptor failed.");
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
44257c478bd9Sstevel@tonic-gate 
44267c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
44277c478bd9Sstevel@tonic-gate 	} else {
44287c478bd9Sstevel@tonic-gate 		MaxPower = cfg_descr.bMaxPower;
44297c478bd9Sstevel@tonic-gate 	}
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	/* check if local power status changed. */
44327c478bd9Sstevel@tonic-gate 	if (change & C_HUB_LOCAL_POWER_STATUS) {
44337c478bd9Sstevel@tonic-gate 
44347c478bd9Sstevel@tonic-gate 		/*
44357c478bd9Sstevel@tonic-gate 		 * local power has been lost, check the maximum
44367c478bd9Sstevel@tonic-gate 		 * power consumption of current configuration.
44377c478bd9Sstevel@tonic-gate 		 * see USB2.0 spec Table 11-12.
44387c478bd9Sstevel@tonic-gate 		 */
44397c478bd9Sstevel@tonic-gate 		if (status & HUB_LOCAL_POWER_STATUS) {
44407c478bd9Sstevel@tonic-gate 
44417c478bd9Sstevel@tonic-gate 			if (MaxPower == 0) {
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate 				/*
44447c478bd9Sstevel@tonic-gate 				 * Self-powered only hub. Because it could
44457c478bd9Sstevel@tonic-gate 				 * not draw any power from USB bus.
44467c478bd9Sstevel@tonic-gate 				 * It can't work well on this condition.
44477c478bd9Sstevel@tonic-gate 				 */
4448fef1e07eSsl 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
44497c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
44507c478bd9Sstevel@tonic-gate 				    "local power has been lost, "
44517c478bd9Sstevel@tonic-gate 				    "please disconnect hub");
44527c478bd9Sstevel@tonic-gate 			} else {
44537c478bd9Sstevel@tonic-gate 
44547c478bd9Sstevel@tonic-gate 				/*
44557c478bd9Sstevel@tonic-gate 				 * Bus-powered only or self/bus-powered hub.
44567c478bd9Sstevel@tonic-gate 				 */
4457fef1e07eSsl 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
44587c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
44597c478bd9Sstevel@tonic-gate 				    "local power has been lost,"
44607c478bd9Sstevel@tonic-gate 				    "the hub could draw %d"
44617c478bd9Sstevel@tonic-gate 				    " mA power from the USB bus.",
44627c478bd9Sstevel@tonic-gate 				    2*MaxPower);
44637c478bd9Sstevel@tonic-gate 			}
44647c478bd9Sstevel@tonic-gate 
44657c478bd9Sstevel@tonic-gate 		}
44667c478bd9Sstevel@tonic-gate 
4467fef1e07eSsl 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
44687c478bd9Sstevel@tonic-gate 		    "clearing feature C_HUB_LOCAL_POWER ");
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
44717c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
4472fef1e07eSsl 		    HUB_HANDLE_HUB_FEATURE_TYPE,
44737c478bd9Sstevel@tonic-gate 		    USB_REQ_CLEAR_FEATURE,
44747c478bd9Sstevel@tonic-gate 		    CFS_C_HUB_LOCAL_POWER,
44757c478bd9Sstevel@tonic-gate 		    0,
44767c478bd9Sstevel@tonic-gate 		    0,
44777c478bd9Sstevel@tonic-gate 		    NULL, 0,
44787c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4479fef1e07eSsl 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
44807c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
44817c478bd9Sstevel@tonic-gate 			    "clear feature C_HUB_LOCAL_POWER "
44827c478bd9Sstevel@tonic-gate 			    "failed (%d 0x%x %d)",
44837c478bd9Sstevel@tonic-gate 			    rval, completion_reason, cb_flags);
44847c478bd9Sstevel@tonic-gate 		}
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	}
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 	if (change & C_HUB_OVER_CURRENT) {
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 		if (status & HUB_OVER_CURRENT) {
4491fef1e07eSsl 
4492fef1e07eSsl 			if (usba_is_root_hub(hubd->h_dip)) {
4493fef1e07eSsl 				/*
4494fef1e07eSsl 				 * The root hub should be automatically
4495fef1e07eSsl 				 * recovered when over-current condition is
4496fef1e07eSsl 				 * cleared. But there might be exception and
4497fef1e07eSsl 				 * need user interaction to recover.
4498fef1e07eSsl 				 */
4499fef1e07eSsl 				USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4500fef1e07eSsl 				    hubd->h_log_handle,
4501fef1e07eSsl 				    "Root hub over current condition, "
4502fef1e07eSsl 				    "please check your system to clear the "
4503fef1e07eSsl 				    "condition as soon as possible. And you "
4504fef1e07eSsl 				    "may need to reboot the system to bring "
4505fef1e07eSsl 				    "the root hub back to work if it cannot "
4506fef1e07eSsl 				    "recover automatically");
4507fef1e07eSsl 			} else {
4508fef1e07eSsl 				/*
4509fef1e07eSsl 				 * The driver would try to recover port power
4510fef1e07eSsl 				 * on over current condition. When the recovery
4511fef1e07eSsl 				 * fails, the user may still need to offline
4512fef1e07eSsl 				 * this hub in order to recover.
4513fef1e07eSsl 				 * The port power is automatically disabled,
4514fef1e07eSsl 				 * so we won't see disconnects.
4515fef1e07eSsl 				 */
4516fef1e07eSsl 				USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4517fef1e07eSsl 				    hubd->h_log_handle,
4518fef1e07eSsl 				    "Hub global over current condition, "
4519fef1e07eSsl 				    "please disconnect the devices connected "
4520fef1e07eSsl 				    "to the hub to clear the condition. And "
4521fef1e07eSsl 				    "you may need to re-connect the hub if "
4522fef1e07eSsl 				    "the ports do not work");
4523fef1e07eSsl 			}
45247c478bd9Sstevel@tonic-gate 		}
45257c478bd9Sstevel@tonic-gate 
4526fef1e07eSsl 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
45277c478bd9Sstevel@tonic-gate 		    "clearing feature C_HUB_OVER_CURRENT");
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
45307c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
4531fef1e07eSsl 		    HUB_HANDLE_HUB_FEATURE_TYPE,
45327c478bd9Sstevel@tonic-gate 		    USB_REQ_CLEAR_FEATURE,
45337c478bd9Sstevel@tonic-gate 		    CFS_C_HUB_OVER_CURRENT,
45347c478bd9Sstevel@tonic-gate 		    0,
45357c478bd9Sstevel@tonic-gate 		    0,
45367c478bd9Sstevel@tonic-gate 		    NULL, 0,
45377c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4538fef1e07eSsl 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
45397c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
45407c478bd9Sstevel@tonic-gate 			    "clear feature C_HUB_OVER_CURRENT "
45417c478bd9Sstevel@tonic-gate 			    "failed (%d 0x%x %d)",
45427c478bd9Sstevel@tonic-gate 			    rval, completion_reason, cb_flags);
45437c478bd9Sstevel@tonic-gate 		}
4544fef1e07eSsl 
4545fef1e07eSsl 		/*
4546fef1e07eSsl 		 * Try to recover all port power if they are turned off.
4547fef1e07eSsl 		 * Don't do this for root hub, but rely on the root hub
4548fef1e07eSsl 		 * to recover itself.
4549fef1e07eSsl 		 */
4550fef1e07eSsl 		if (!usba_is_root_hub(hubd->h_dip)) {
4551fef1e07eSsl 
4552fef1e07eSsl 			mutex_enter(HUBD_MUTEX(hubd));
4553fef1e07eSsl 
4554fef1e07eSsl 			/*
4555fef1e07eSsl 			 * Only check the power status of the 1st port
4556fef1e07eSsl 			 * since all port power status should be the same.
4557fef1e07eSsl 			 */
4558fef1e07eSsl 			(void) hubd_determine_port_status(hubd, 1, &status,
4559fef1e07eSsl 			    &change, 0);
4560fef1e07eSsl 
4561fef1e07eSsl 			if (status & PORT_STATUS_PPS) {
4562fef1e07eSsl 
4563fef1e07eSsl 				return (USB_SUCCESS);
4564fef1e07eSsl 			}
4565fef1e07eSsl 
4566fef1e07eSsl 			hub_descr = &hubd->h_hub_descr;
4567fef1e07eSsl 
4568fef1e07eSsl 			for (port = 1; port <= hub_descr->bNbrPorts;
4569fef1e07eSsl 			    port++) {
4570fef1e07eSsl 
4571fef1e07eSsl 				(void) hubd_enable_port_power(hubd, port);
4572fef1e07eSsl 			}
4573fef1e07eSsl 
4574fef1e07eSsl 			mutex_exit(HUBD_MUTEX(hubd));
4575fef1e07eSsl 
4576fef1e07eSsl 			/*
4577fef1e07eSsl 			 * Delay some time to avoid over-current event
4578fef1e07eSsl 			 * to happen too frequently in some cases
4579fef1e07eSsl 			 */
4580fef1e07eSsl 			delay(drv_usectohz(500000));
4581fef1e07eSsl 		}
45827c478bd9Sstevel@tonic-gate 	}
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
45877c478bd9Sstevel@tonic-gate }
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate /*
45917c478bd9Sstevel@tonic-gate  * hubd_reset_port:
45927c478bd9Sstevel@tonic-gate  */
45937c478bd9Sstevel@tonic-gate static int
45947c478bd9Sstevel@tonic-gate hubd_reset_port(hubd_t *hubd, usb_port_t port)
45957c478bd9Sstevel@tonic-gate {
45967c478bd9Sstevel@tonic-gate 	int	rval;
45977c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
45987c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
45997c478bd9Sstevel@tonic-gate 	usb_port_mask_t port_mask = 1 << port;
46007c478bd9Sstevel@tonic-gate 	mblk_t	*data;
46017c478bd9Sstevel@tonic-gate 	uint16_t status;
46027c478bd9Sstevel@tonic-gate 	uint16_t change;
46037c478bd9Sstevel@tonic-gate 	int	i;
4604d3d50737SRafael Vanoni 	clock_t	delta;
46057c478bd9Sstevel@tonic-gate 
46067c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
46077c478bd9Sstevel@tonic-gate 	    "hubd_reset_port: port=%d", port);
46087c478bd9Sstevel@tonic-gate 
46097c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
46107c478bd9Sstevel@tonic-gate 
46117c478bd9Sstevel@tonic-gate 	hubd->h_port_reset_wait |= port_mask;
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
46147c478bd9Sstevel@tonic-gate 
46157c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
46167c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
461735f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
46187c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
46197c478bd9Sstevel@tonic-gate 	    CFS_PORT_RESET,
46207c478bd9Sstevel@tonic-gate 	    port,
46217c478bd9Sstevel@tonic-gate 	    0,
46227c478bd9Sstevel@tonic-gate 	    NULL, 0,
46237c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4624d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
46257c478bd9Sstevel@tonic-gate 		    "reset port%d failed (%d 0x%x %d)",
46267c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
46297c478bd9Sstevel@tonic-gate 
46307c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
46317c478bd9Sstevel@tonic-gate 	}
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
46367c478bd9Sstevel@tonic-gate 	    "waiting on cv for reset completion");
46377c478bd9Sstevel@tonic-gate 
46387c478bd9Sstevel@tonic-gate 	/*
46397c478bd9Sstevel@tonic-gate 	 * wait for port status change event
46407c478bd9Sstevel@tonic-gate 	 */
4641d3d50737SRafael Vanoni 	delta = drv_usectohz(hubd_device_delay / 10);
46427c478bd9Sstevel@tonic-gate 	for (i = 0; i < hubd_retry_enumerate; i++) {
46437c478bd9Sstevel@tonic-gate 		/*
46447c478bd9Sstevel@tonic-gate 		 * start polling ep1 for receiving notification on
46457c478bd9Sstevel@tonic-gate 		 * reset completion
46467c478bd9Sstevel@tonic-gate 		 */
46477c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
46487c478bd9Sstevel@tonic-gate 
46497c478bd9Sstevel@tonic-gate 		/*
46507c478bd9Sstevel@tonic-gate 		 * sleep a max of 100ms for reset completion
46517c478bd9Sstevel@tonic-gate 		 * notification to be received
46527c478bd9Sstevel@tonic-gate 		 */
46537c478bd9Sstevel@tonic-gate 		if (hubd->h_port_reset_wait & port_mask) {
4654d3d50737SRafael Vanoni 			rval = cv_reltimedwait(&hubd->h_cv_reset_port,
4655d3d50737SRafael Vanoni 			    &hubd->h_mutex, delta, TR_CLOCK_TICK);
46567c478bd9Sstevel@tonic-gate 			if ((rval <= 0) &&
46577c478bd9Sstevel@tonic-gate 			    (hubd->h_port_reset_wait & port_mask)) {
46587c478bd9Sstevel@tonic-gate 				/* we got woken up because of a timeout */
46597c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
46607c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
46617c478bd9Sstevel@tonic-gate 				    "timeout: reset port=%d failed", port);
46627c478bd9Sstevel@tonic-gate 
46637c478bd9Sstevel@tonic-gate 				hubd->h_port_reset_wait &=  ~port_mask;
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 				hubd_stop_polling(hubd);
46667c478bd9Sstevel@tonic-gate 
46677c478bd9Sstevel@tonic-gate 				return (USB_FAILURE);
46687c478bd9Sstevel@tonic-gate 			}
46697c478bd9Sstevel@tonic-gate 		}
46707c478bd9Sstevel@tonic-gate 
46717c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
46727c478bd9Sstevel@tonic-gate 		    "reset completion received");
46737c478bd9Sstevel@tonic-gate 
46747c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
46757c478bd9Sstevel@tonic-gate 
46767c478bd9Sstevel@tonic-gate 		data = NULL;
46777c478bd9Sstevel@tonic-gate 
46787c478bd9Sstevel@tonic-gate 		/* check status to determine whether reset completed */
46797c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
46807c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
46817c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
468235f36846Ssl 		    HUB_GET_PORT_STATUS_TYPE,
46837c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_STATUS,
46847c478bd9Sstevel@tonic-gate 		    0,
46857c478bd9Sstevel@tonic-gate 		    port,
46867c478bd9Sstevel@tonic-gate 		    GET_STATUS_LENGTH,
46877c478bd9Sstevel@tonic-gate 		    &data, 0,
46887c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4689d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_PORT,
46907c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
46917c478bd9Sstevel@tonic-gate 			    "get status port%d failed (%d 0x%x %d)",
46927c478bd9Sstevel@tonic-gate 			    port, completion_reason, cb_flags, rval);
46937c478bd9Sstevel@tonic-gate 
46947c478bd9Sstevel@tonic-gate 			if (data) {
46957c478bd9Sstevel@tonic-gate 				freemsg(data);
46967c478bd9Sstevel@tonic-gate 				data = NULL;
46977c478bd9Sstevel@tonic-gate 			}
46987c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
46997c478bd9Sstevel@tonic-gate 
47007c478bd9Sstevel@tonic-gate 			continue;
47017c478bd9Sstevel@tonic-gate 		}
47027c478bd9Sstevel@tonic-gate 
47037c478bd9Sstevel@tonic-gate 		status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
47047c478bd9Sstevel@tonic-gate 		change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
47057c478bd9Sstevel@tonic-gate 
47067c478bd9Sstevel@tonic-gate 		freemsg(data);
47077c478bd9Sstevel@tonic-gate 
47087c478bd9Sstevel@tonic-gate 		/* continue only if port is still connected */
47097c478bd9Sstevel@tonic-gate 		if (!(status & PORT_STATUS_CCS)) {
47107c478bd9Sstevel@tonic-gate 
47117c478bd9Sstevel@tonic-gate 			/* lost connection, set exit condition */
47127c478bd9Sstevel@tonic-gate 			i = hubd_retry_enumerate;
47137c478bd9Sstevel@tonic-gate 
47147c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate 			break;
47177c478bd9Sstevel@tonic-gate 		}
47187c478bd9Sstevel@tonic-gate 
47197c478bd9Sstevel@tonic-gate 		if (status & PORT_STATUS_PRS) {
47207c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
47217c478bd9Sstevel@tonic-gate 			    "port%d reset active", port);
47227c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
47237c478bd9Sstevel@tonic-gate 
47247c478bd9Sstevel@tonic-gate 			continue;
47257c478bd9Sstevel@tonic-gate 		} else {
47267c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
47277c478bd9Sstevel@tonic-gate 			    "port%d reset inactive", port);
47287c478bd9Sstevel@tonic-gate 		}
47297c478bd9Sstevel@tonic-gate 
47307c478bd9Sstevel@tonic-gate 		if (change & PORT_CHANGE_PRSC) {
47317c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
47327c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_RESET");
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate 			if (usb_pipe_sync_ctrl_xfer(hubd->h_dip,
47357c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
473635f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
47377c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
47387c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_RESET,
47397c478bd9Sstevel@tonic-gate 			    port,
47407c478bd9Sstevel@tonic-gate 			    0,
47417c478bd9Sstevel@tonic-gate 			    NULL, 0,
47427c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
47437c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
47447c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
47457c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_RESET"
47467c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
47477c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
47487c478bd9Sstevel@tonic-gate 			}
47497c478bd9Sstevel@tonic-gate 		}
47507c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
47517c478bd9Sstevel@tonic-gate 
47527c478bd9Sstevel@tonic-gate 		break;
47537c478bd9Sstevel@tonic-gate 	}
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate 	if (i >= hubd_retry_enumerate) {
47567c478bd9Sstevel@tonic-gate 		/* port reset has failed */
47577c478bd9Sstevel@tonic-gate 		rval = USB_FAILURE;
47587c478bd9Sstevel@tonic-gate 	}
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 	return (rval);
47617c478bd9Sstevel@tonic-gate }
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate /*
47657c478bd9Sstevel@tonic-gate  * hubd_enable_port:
47667c478bd9Sstevel@tonic-gate  *	this may fail if the hub as been disconnected
47677c478bd9Sstevel@tonic-gate  */
47687c478bd9Sstevel@tonic-gate static int
47697c478bd9Sstevel@tonic-gate hubd_enable_port(hubd_t *hubd, usb_port_t port)
47707c478bd9Sstevel@tonic-gate {
47717c478bd9Sstevel@tonic-gate 	int	rval;
47727c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
47737c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
47767c478bd9Sstevel@tonic-gate 	    "hubd_enable_port: port=%d", port);
47777c478bd9Sstevel@tonic-gate 
47787c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
47797c478bd9Sstevel@tonic-gate 
47807c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
47817c478bd9Sstevel@tonic-gate 
47827c478bd9Sstevel@tonic-gate 	/* Do not issue a SetFeature(PORT_ENABLE) on external hubs */
47837c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(hubd->h_dip)) {
47847c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
47857c478bd9Sstevel@tonic-gate 
47867c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
47877c478bd9Sstevel@tonic-gate 	}
47887c478bd9Sstevel@tonic-gate 
47897c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
47907c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
479135f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
47927c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
47937c478bd9Sstevel@tonic-gate 	    CFS_PORT_ENABLE,
47947c478bd9Sstevel@tonic-gate 	    port,
47957c478bd9Sstevel@tonic-gate 	    0,
47967c478bd9Sstevel@tonic-gate 	    NULL, 0,
47977c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
47987c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
47997c478bd9Sstevel@tonic-gate 		    "enable port%d failed (%d 0x%x %d)",
48007c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
48017c478bd9Sstevel@tonic-gate 	}
48027c478bd9Sstevel@tonic-gate 
48037c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
48047c478bd9Sstevel@tonic-gate 
48057c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
48067c478bd9Sstevel@tonic-gate 	    "enabling port done");
48077c478bd9Sstevel@tonic-gate 
48087c478bd9Sstevel@tonic-gate 	return (rval);
48097c478bd9Sstevel@tonic-gate }
48107c478bd9Sstevel@tonic-gate 
48117c478bd9Sstevel@tonic-gate 
48127c478bd9Sstevel@tonic-gate /*
48137c478bd9Sstevel@tonic-gate  * hubd_disable_port
48147c478bd9Sstevel@tonic-gate  */
48157c478bd9Sstevel@tonic-gate static int
48167c478bd9Sstevel@tonic-gate hubd_disable_port(hubd_t *hubd, usb_port_t port)
48177c478bd9Sstevel@tonic-gate {
48187c478bd9Sstevel@tonic-gate 	int	rval;
48197c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
48207c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
48217c478bd9Sstevel@tonic-gate 
48227c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
48237c478bd9Sstevel@tonic-gate 	    "hubd_disable_port: port=%d", port);
48247c478bd9Sstevel@tonic-gate 
48257c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
48267c478bd9Sstevel@tonic-gate 
48277c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
48287c478bd9Sstevel@tonic-gate 
48297c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
48307c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
483135f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
48327c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
48337c478bd9Sstevel@tonic-gate 	    CFS_PORT_ENABLE,
48347c478bd9Sstevel@tonic-gate 	    port,
48357c478bd9Sstevel@tonic-gate 	    0,
48367c478bd9Sstevel@tonic-gate 	    NULL, 0,
48377c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
48387c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
48397c478bd9Sstevel@tonic-gate 		    "disable port%d failed (%d 0x%x %d)", port,
48407c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
48417c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
48427c478bd9Sstevel@tonic-gate 
48437c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
48447c478bd9Sstevel@tonic-gate 	}
48457c478bd9Sstevel@tonic-gate 
48467c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
48477c478bd9Sstevel@tonic-gate 	    "clearing feature CFS_C_PORT_ENABLE");
48487c478bd9Sstevel@tonic-gate 
48497c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
48507c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
485135f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
48527c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
48537c478bd9Sstevel@tonic-gate 	    CFS_C_PORT_ENABLE,
48547c478bd9Sstevel@tonic-gate 	    port,
48557c478bd9Sstevel@tonic-gate 	    0,
48567c478bd9Sstevel@tonic-gate 	    NULL, 0,
48577c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
48587c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT,
48597c478bd9Sstevel@tonic-gate 		    hubd->h_log_handle,
48607c478bd9Sstevel@tonic-gate 		    "clear feature CFS_C_PORT_ENABLE port%d failed "
48617c478bd9Sstevel@tonic-gate 		    "(%d 0x%x %d)",
48627c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
48637c478bd9Sstevel@tonic-gate 
48647c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
48677c478bd9Sstevel@tonic-gate 	}
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
48707c478bd9Sstevel@tonic-gate 
48717c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
48727c478bd9Sstevel@tonic-gate }
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate 
48757c478bd9Sstevel@tonic-gate /*
48767c478bd9Sstevel@tonic-gate  * hubd_determine_port_status:
48777c478bd9Sstevel@tonic-gate  */
48787c478bd9Sstevel@tonic-gate static int
48797c478bd9Sstevel@tonic-gate hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
48807c478bd9Sstevel@tonic-gate 		uint16_t *status, uint16_t *change, uint_t ack_flag)
48817c478bd9Sstevel@tonic-gate {
48827c478bd9Sstevel@tonic-gate 	int rval;
48837c478bd9Sstevel@tonic-gate 	mblk_t	*data = NULL;
48847c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
48857c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
48867c478bd9Sstevel@tonic-gate 
48877c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
48887c478bd9Sstevel@tonic-gate 	    "hubd_determine_port_status: port=%d, state=0x%x ack=0x%x", port,
48897c478bd9Sstevel@tonic-gate 	    hubd->h_port_state[port], ack_flag);
48907c478bd9Sstevel@tonic-gate 
48917c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
48927c478bd9Sstevel@tonic-gate 
48937c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
48947c478bd9Sstevel@tonic-gate 
48957c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
48967c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
489735f36846Ssl 	    HUB_GET_PORT_STATUS_TYPE,
48987c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_STATUS,
48997c478bd9Sstevel@tonic-gate 	    0,
49007c478bd9Sstevel@tonic-gate 	    port,
49017c478bd9Sstevel@tonic-gate 	    GET_STATUS_LENGTH,
49027c478bd9Sstevel@tonic-gate 	    &data, 0,
49037c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
49047c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
49057c478bd9Sstevel@tonic-gate 		    "port=%d get status failed (%d 0x%x %d)",
49067c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
49077c478bd9Sstevel@tonic-gate 
49087c478bd9Sstevel@tonic-gate 		if (data) {
49097c478bd9Sstevel@tonic-gate 			freemsg(data);
49107c478bd9Sstevel@tonic-gate 		}
49117c478bd9Sstevel@tonic-gate 
49127c478bd9Sstevel@tonic-gate 		*status = *change = 0;
49137c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
49147c478bd9Sstevel@tonic-gate 
49157c478bd9Sstevel@tonic-gate 		return (rval);
49167c478bd9Sstevel@tonic-gate 	}
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
4919d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	if (MBLKL(data) != GET_STATUS_LENGTH) {
49207c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4921112116d8Sfb 		    "port %d: length incorrect %ld",
4922d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		    port, MBLKL(data));
49237c478bd9Sstevel@tonic-gate 		freemsg(data);
49247c478bd9Sstevel@tonic-gate 		*status = *change = 0;
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 		return (rval);
49277c478bd9Sstevel@tonic-gate 	}
49287c478bd9Sstevel@tonic-gate 
49297c478bd9Sstevel@tonic-gate 
49307c478bd9Sstevel@tonic-gate 	*status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
49317c478bd9Sstevel@tonic-gate 	*change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
49327c478bd9Sstevel@tonic-gate 
49337c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49347c478bd9Sstevel@tonic-gate 	    "port%d status=0x%x, change=0x%x", port, *status, *change);
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate 	freemsg(data);
49377c478bd9Sstevel@tonic-gate 
49387c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_CCS) {
49397c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49407c478bd9Sstevel@tonic-gate 		    "port%d connected", port);
49417c478bd9Sstevel@tonic-gate 
49427c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_CCS & ack_flag);
49437c478bd9Sstevel@tonic-gate 	} else {
49447c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49457c478bd9Sstevel@tonic-gate 		    "port%d disconnected", port);
49467c478bd9Sstevel@tonic-gate 
49477c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_CCS & ack_flag);
49487c478bd9Sstevel@tonic-gate 	}
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PES) {
49517c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49527c478bd9Sstevel@tonic-gate 		    "port%d enabled", port);
49537c478bd9Sstevel@tonic-gate 
49547c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PES & ack_flag);
49557c478bd9Sstevel@tonic-gate 	} else {
49567c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49577c478bd9Sstevel@tonic-gate 		    "port%d disabled", port);
49587c478bd9Sstevel@tonic-gate 
49597c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PES & ack_flag);
49607c478bd9Sstevel@tonic-gate 	}
49617c478bd9Sstevel@tonic-gate 
49627c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PSS) {
49637c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49647c478bd9Sstevel@tonic-gate 		    "port%d suspended", port);
49657c478bd9Sstevel@tonic-gate 
49667c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PSS & ack_flag);
49677c478bd9Sstevel@tonic-gate 	} else {
49687c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49697c478bd9Sstevel@tonic-gate 		    "port%d not suspended", port);
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PSS & ack_flag);
49727c478bd9Sstevel@tonic-gate 	}
49737c478bd9Sstevel@tonic-gate 
49747c478bd9Sstevel@tonic-gate 	if (*change & PORT_CHANGE_PRSC) {
49757c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49767c478bd9Sstevel@tonic-gate 		    "port%d reset completed", port);
49777c478bd9Sstevel@tonic-gate 
49787c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_CHANGE_PRSC & ack_flag);
49797c478bd9Sstevel@tonic-gate 	} else {
49807c478bd9Sstevel@tonic-gate 
49817c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_CHANGE_PRSC & ack_flag);
49827c478bd9Sstevel@tonic-gate 	}
49837c478bd9Sstevel@tonic-gate 
49847c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_POCI) {
4985d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
4986d291d9f2Sfrits 		    "port%d overcurrent!", port);
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_POCI & ack_flag);
49897c478bd9Sstevel@tonic-gate 	} else {
49907c478bd9Sstevel@tonic-gate 
49917c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_POCI & ack_flag);
49927c478bd9Sstevel@tonic-gate 	}
49937c478bd9Sstevel@tonic-gate 
49947c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PRS) {
49957c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49967c478bd9Sstevel@tonic-gate 		    "port%d reset active", port);
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PRS & ack_flag);
49997c478bd9Sstevel@tonic-gate 	} else {
50007c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
50017c478bd9Sstevel@tonic-gate 		    "port%d reset inactive", port);
50027c478bd9Sstevel@tonic-gate 
50037c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PRS & ack_flag);
50047c478bd9Sstevel@tonic-gate 	}
50057c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PPS) {
50067c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
50077c478bd9Sstevel@tonic-gate 		    "port%d power on", port);
50087c478bd9Sstevel@tonic-gate 
50097c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PPS & ack_flag);
50107c478bd9Sstevel@tonic-gate 	} else {
50117c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
50127c478bd9Sstevel@tonic-gate 		    "port%d power off", port);
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PPS & ack_flag);
50157c478bd9Sstevel@tonic-gate 	}
50167c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_LSDA) {
50177c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
50187c478bd9Sstevel@tonic-gate 		    "port%d low speed", port);
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_LSDA & ack_flag);
50217c478bd9Sstevel@tonic-gate 	} else {
50227c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_LSDA & ack_flag);
50237c478bd9Sstevel@tonic-gate 		if (*status & PORT_STATUS_HSDA) {
50247c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT,
50257c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle, "port%d "
50267c478bd9Sstevel@tonic-gate 			    "high speed", port);
50277c478bd9Sstevel@tonic-gate 
50287c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] |=
5029c0f24e5bSlg 			    (PORT_STATUS_HSDA & ack_flag);
50307c478bd9Sstevel@tonic-gate 		} else {
50317c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT,
50327c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle, "port%d "
50337c478bd9Sstevel@tonic-gate 			    "full speed", port);
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &=
5036c0f24e5bSlg 			    ~(PORT_STATUS_HSDA & ack_flag);
50377c478bd9Sstevel@tonic-gate 		}
50387c478bd9Sstevel@tonic-gate 	}
50397c478bd9Sstevel@tonic-gate 
50407c478bd9Sstevel@tonic-gate 	/*
50417c478bd9Sstevel@tonic-gate 	 * Acknowledge connection, enable, reset status
50427c478bd9Sstevel@tonic-gate 	 */
50437c478bd9Sstevel@tonic-gate 	if (ack_flag) {
50447c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
50457c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_CSC & ack_flag) {
50467c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
50477c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_CONNECTION");
50487c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
50497c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
505035f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
50517c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
50527c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_CONNECTION,
50537c478bd9Sstevel@tonic-gate 			    port,
50547c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
50557c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
50567c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
50577c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
50587c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
50597c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_CONNECTION"
50607c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
50617c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
50627c478bd9Sstevel@tonic-gate 			}
50637c478bd9Sstevel@tonic-gate 		}
50647c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PESC & ack_flag) {
50657c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
50667c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_ENABLE");
50677c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
50687c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
506935f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
50707c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
50717c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_ENABLE,
50727c478bd9Sstevel@tonic-gate 			    port,
50737c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
50747c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
50757c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
50767c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
50777c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
50787c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_ENABLE"
50797c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
50807c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
50817c478bd9Sstevel@tonic-gate 			}
50827c478bd9Sstevel@tonic-gate 		}
50837c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PSSC & ack_flag) {
50847c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
50857c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_SUSPEND");
50867c478bd9Sstevel@tonic-gate 
50877c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
50887c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
508935f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
50907c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
50917c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_SUSPEND,
50927c478bd9Sstevel@tonic-gate 			    port,
50937c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
50947c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
50957c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
50967c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
50977c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
50987c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_SUSPEND"
50997c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
51007c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
51017c478bd9Sstevel@tonic-gate 			}
51027c478bd9Sstevel@tonic-gate 		}
51037c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_OCIC & ack_flag) {
51047c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
51057c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_OVER_CURRENT");
51067c478bd9Sstevel@tonic-gate 
51077c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
51087c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
510935f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
51107c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
51117c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_OVER_CURRENT,
51127c478bd9Sstevel@tonic-gate 			    port,
51137c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
51147c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
51157c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
51167c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
51177c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
51187c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_OVER_CURRENT"
51197c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
51207c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
51217c478bd9Sstevel@tonic-gate 			}
51227c478bd9Sstevel@tonic-gate 		}
51237c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PRSC & ack_flag) {
51247c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
51257c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_RESET");
51267c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
51277c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
512835f36846Ssl 			    HUB_HANDLE_PORT_FEATURE_TYPE,
51297c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
51307c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_RESET,
51317c478bd9Sstevel@tonic-gate 			    port,
51327c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
51337c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
51347c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
51357c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
51367c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
51377c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_RESET"
51387c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
51397c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
51407c478bd9Sstevel@tonic-gate 			}
51417c478bd9Sstevel@tonic-gate 		}
51427c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
51437c478bd9Sstevel@tonic-gate 	}
51447c478bd9Sstevel@tonic-gate 
51457c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
51467c478bd9Sstevel@tonic-gate 	    "new port%d state 0x%x", port, hubd->h_port_state[port]);
51477c478bd9Sstevel@tonic-gate 
51487c478bd9Sstevel@tonic-gate 
51497c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
51507c478bd9Sstevel@tonic-gate }
51517c478bd9Sstevel@tonic-gate 
51527c478bd9Sstevel@tonic-gate 
51537c478bd9Sstevel@tonic-gate /*
51547c478bd9Sstevel@tonic-gate  * hubd_recover_disabled_port
51557c478bd9Sstevel@tonic-gate  * if the port got disabled because of an error
51567c478bd9Sstevel@tonic-gate  * enable it. If hub doesn't suport enable port,
51577c478bd9Sstevel@tonic-gate  * reset the port to bring the device to life again
51587c478bd9Sstevel@tonic-gate  */
51597c478bd9Sstevel@tonic-gate static int
51607c478bd9Sstevel@tonic-gate hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port)
51617c478bd9Sstevel@tonic-gate {
51627c478bd9Sstevel@tonic-gate 	uint16_t	status;
51637c478bd9Sstevel@tonic-gate 	uint16_t	change;
51647c478bd9Sstevel@tonic-gate 	int		rval = USB_FAILURE;
51657c478bd9Sstevel@tonic-gate 
51667c478bd9Sstevel@tonic-gate 	/* first try enabling the port */
51677c478bd9Sstevel@tonic-gate 	(void) hubd_enable_port(hubd, port);
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate 	/* read the port status */
51707c478bd9Sstevel@tonic-gate 	(void) hubd_determine_port_status(hubd, port, &status, &change,
51717c478bd9Sstevel@tonic-gate 	    PORT_CHANGE_PESC);
51727c478bd9Sstevel@tonic-gate 
51737c478bd9Sstevel@tonic-gate 	if (status & PORT_STATUS_PES) {
51747c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5175c0f24e5bSlg 		    "Port%d now Enabled", port);
51767c478bd9Sstevel@tonic-gate 	} else if (status & PORT_STATUS_CCS) {
51777c478bd9Sstevel@tonic-gate 		/* first post a disconnect event to the child */
51787c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
51797c478bd9Sstevel@tonic-gate 		hubd_post_event(hubd, port, USBA_EVENT_TAG_HOT_REMOVAL);
51807c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
51817c478bd9Sstevel@tonic-gate 
51827c478bd9Sstevel@tonic-gate 		/* then reset the port and recover the device */
51837c478bd9Sstevel@tonic-gate 		rval = hubd_handle_port_connect(hubd, port);
51847c478bd9Sstevel@tonic-gate 
51857c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5186c0f24e5bSlg 		    "Port%d now Enabled by force", port);
51877c478bd9Sstevel@tonic-gate 	}
51887c478bd9Sstevel@tonic-gate 
51897c478bd9Sstevel@tonic-gate 	return (rval);
51907c478bd9Sstevel@tonic-gate }
51917c478bd9Sstevel@tonic-gate 
51927c478bd9Sstevel@tonic-gate 
51937c478bd9Sstevel@tonic-gate /*
51947c478bd9Sstevel@tonic-gate  * hubd_enable_all_port_power:
51957c478bd9Sstevel@tonic-gate  */
51967c478bd9Sstevel@tonic-gate static int
51977c478bd9Sstevel@tonic-gate hubd_enable_all_port_power(hubd_t *hubd)
51987c478bd9Sstevel@tonic-gate {
51997c478bd9Sstevel@tonic-gate 	usb_hub_descr_t	*hub_descr;
52007c478bd9Sstevel@tonic-gate 	int		wait;
52017c478bd9Sstevel@tonic-gate 	usb_port_t	port;
52027c478bd9Sstevel@tonic-gate 	uint_t		retry;
52037c478bd9Sstevel@tonic-gate 	uint16_t	status;
52047c478bd9Sstevel@tonic-gate 	uint16_t	change;
52057c478bd9Sstevel@tonic-gate 
52067c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
52077c478bd9Sstevel@tonic-gate 	    "hubd_enable_all_port_power");
52087c478bd9Sstevel@tonic-gate 
52097c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
52107c478bd9Sstevel@tonic-gate 
52117c478bd9Sstevel@tonic-gate 	hub_descr = &hubd->h_hub_descr;
52127c478bd9Sstevel@tonic-gate 
52137c478bd9Sstevel@tonic-gate 	/*
52147c478bd9Sstevel@tonic-gate 	 * According to section 11.11 of USB, for hubs with no power
52157c478bd9Sstevel@tonic-gate 	 * switches, bPwrOn2PwrGood is zero. But we wait for some
52167c478bd9Sstevel@tonic-gate 	 * arbitrary time to enable power to become stable.
52177c478bd9Sstevel@tonic-gate 	 *
52187c478bd9Sstevel@tonic-gate 	 * If an hub supports port power switching, we need to wait
52197c478bd9Sstevel@tonic-gate 	 * at least 20ms before accessing corresponding usb port.
52207c478bd9Sstevel@tonic-gate 	 */
52217c478bd9Sstevel@tonic-gate 	if ((hub_descr->wHubCharacteristics &
52227c478bd9Sstevel@tonic-gate 	    HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) {
52237c478bd9Sstevel@tonic-gate 		wait = hubd_device_delay / 10;
52247c478bd9Sstevel@tonic-gate 	} else {
52257c478bd9Sstevel@tonic-gate 		wait = max(HUB_DEFAULT_POPG,
52267c478bd9Sstevel@tonic-gate 		    hub_descr->bPwrOn2PwrGood) * 2 * 1000;
52277c478bd9Sstevel@tonic-gate 	}
52287c478bd9Sstevel@tonic-gate 
52297c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
52307c478bd9Sstevel@tonic-gate 	    "hubd_enable_all_port_power: popg=%d wait=%d",
52317c478bd9Sstevel@tonic-gate 	    hub_descr->bPwrOn2PwrGood, wait);
52327c478bd9Sstevel@tonic-gate 
52337c478bd9Sstevel@tonic-gate 	/*
52347c478bd9Sstevel@tonic-gate 	 * Enable power per port. we ignore gang power and power mask
52357c478bd9Sstevel@tonic-gate 	 * and always enable all ports one by one.
52367c478bd9Sstevel@tonic-gate 	 */
52377c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hub_descr->bNbrPorts; port++) {
52387c478bd9Sstevel@tonic-gate 		/*
52397c478bd9Sstevel@tonic-gate 		 * Transition the port from the Powered Off to the
52407c478bd9Sstevel@tonic-gate 		 * Disconnected state by supplying power to the port.
52417c478bd9Sstevel@tonic-gate 		 */
52427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PORT,
52437c478bd9Sstevel@tonic-gate 		    hubd->h_log_handle,
52447c478bd9Sstevel@tonic-gate 		    "hubd_enable_all_port_power: power port=%d", port);
52457c478bd9Sstevel@tonic-gate 
52467c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port_power(hubd, port);
52477c478bd9Sstevel@tonic-gate 	}
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
52507c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(wait));
52517c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
52527c478bd9Sstevel@tonic-gate 
52537c478bd9Sstevel@tonic-gate 	/* For retry if any, use some extra delay */
52547c478bd9Sstevel@tonic-gate 	wait = max(wait, hubd_device_delay / 10);
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate 	/* Check each port power status for a given usb hub */
52577c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hub_descr->bNbrPorts; port++) {
52587c478bd9Sstevel@tonic-gate 
52597c478bd9Sstevel@tonic-gate 		/* Get port status */
52607c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd, port,
52617c478bd9Sstevel@tonic-gate 		    &status, &change, 0);
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate 		for (retry = 0; ((!(status & PORT_STATUS_PPS)) &&
52647c478bd9Sstevel@tonic-gate 		    (retry < HUBD_PORT_RETRY)); retry++) {
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
52677c478bd9Sstevel@tonic-gate 			    "Retry is in progress %d: port %d status %d",
52687c478bd9Sstevel@tonic-gate 			    retry, port, status);
52697c478bd9Sstevel@tonic-gate 
52707c478bd9Sstevel@tonic-gate 			(void) hubd_enable_port_power(hubd, port);
52717c478bd9Sstevel@tonic-gate 
52727c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
52737c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(wait));
52747c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 			/* Get port status */
52777c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
52787c478bd9Sstevel@tonic-gate 			    &status, &change, 0);
52797c478bd9Sstevel@tonic-gate 		}
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate 		/* Print warning message if port has no power */
52827c478bd9Sstevel@tonic-gate 		if (!(status & PORT_STATUS_PPS)) {
52837c478bd9Sstevel@tonic-gate 
5284d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
52857c478bd9Sstevel@tonic-gate 			    "hubd_enable_all_port_power: port %d power-on "
52867c478bd9Sstevel@tonic-gate 			    "failed, port status 0x%x", port, status);
52877c478bd9Sstevel@tonic-gate 		}
52887c478bd9Sstevel@tonic-gate 	}
52897c478bd9Sstevel@tonic-gate 
52907c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
52917c478bd9Sstevel@tonic-gate }
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate 
52947c478bd9Sstevel@tonic-gate /*
52957c478bd9Sstevel@tonic-gate  * hubd_enable_port_power:
52967c478bd9Sstevel@tonic-gate  *	enable individual port power
52977c478bd9Sstevel@tonic-gate  */
52987c478bd9Sstevel@tonic-gate static int
52997c478bd9Sstevel@tonic-gate hubd_enable_port_power(hubd_t *hubd, usb_port_t port)
53007c478bd9Sstevel@tonic-gate {
53017c478bd9Sstevel@tonic-gate 	int		rval;
53027c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
53037c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
53047c478bd9Sstevel@tonic-gate 
53057c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
53067c478bd9Sstevel@tonic-gate 	    "hubd_enable_port_power: port=%d", port);
53077c478bd9Sstevel@tonic-gate 
53087c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
53097c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_default_pipe != 0);
53107c478bd9Sstevel@tonic-gate 
53117c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
53127c478bd9Sstevel@tonic-gate 
53137c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
53147c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
531535f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
53167c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
53177c478bd9Sstevel@tonic-gate 	    CFS_PORT_POWER,
53187c478bd9Sstevel@tonic-gate 	    port,
53197c478bd9Sstevel@tonic-gate 	    0, NULL, 0,
53207c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
53217c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
53227c478bd9Sstevel@tonic-gate 		    "set port power failed (%d 0x%x %d)",
53237c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
53247c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
53257c478bd9Sstevel@tonic-gate 
53267c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
53277c478bd9Sstevel@tonic-gate 	} else {
53287c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
53297c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= PORT_STATUS_PPS;
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
53327c478bd9Sstevel@tonic-gate 	}
53337c478bd9Sstevel@tonic-gate }
53347c478bd9Sstevel@tonic-gate 
53357c478bd9Sstevel@tonic-gate 
53367c478bd9Sstevel@tonic-gate /*
53377c478bd9Sstevel@tonic-gate  * hubd_disable_all_port_power:
53387c478bd9Sstevel@tonic-gate  */
53397c478bd9Sstevel@tonic-gate static int
53407c478bd9Sstevel@tonic-gate hubd_disable_all_port_power(hubd_t *hubd)
53417c478bd9Sstevel@tonic-gate {
53427c478bd9Sstevel@tonic-gate 	usb_port_t port;
53437c478bd9Sstevel@tonic-gate 
53447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
53457c478bd9Sstevel@tonic-gate 	    "hubd_disable_all_port_power");
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
53487c478bd9Sstevel@tonic-gate 
53497c478bd9Sstevel@tonic-gate 	/*
53507c478bd9Sstevel@tonic-gate 	 * disable power per port, ignore gang power and power mask
53517c478bd9Sstevel@tonic-gate 	 */
53527c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
53537c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port_power(hubd, port);
53547c478bd9Sstevel@tonic-gate 	}
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
53577c478bd9Sstevel@tonic-gate }
53587c478bd9Sstevel@tonic-gate 
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate /*
53617c478bd9Sstevel@tonic-gate  * hubd_disable_port_power:
53627c478bd9Sstevel@tonic-gate  *	disable individual port power
53637c478bd9Sstevel@tonic-gate  */
53647c478bd9Sstevel@tonic-gate static int
53657c478bd9Sstevel@tonic-gate hubd_disable_port_power(hubd_t *hubd, usb_port_t port)
53667c478bd9Sstevel@tonic-gate {
53677c478bd9Sstevel@tonic-gate 	int		rval;
53687c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
53697c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
53707c478bd9Sstevel@tonic-gate 
53717c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
53727c478bd9Sstevel@tonic-gate 	    "hubd_disable_port_power: port=%d", port);
53737c478bd9Sstevel@tonic-gate 
53747c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
53757c478bd9Sstevel@tonic-gate 
53767c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
53777c478bd9Sstevel@tonic-gate 
53787c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
53797c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
538035f36846Ssl 	    HUB_HANDLE_PORT_FEATURE_TYPE,
53817c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
53827c478bd9Sstevel@tonic-gate 	    CFS_PORT_POWER,
53837c478bd9Sstevel@tonic-gate 	    port,
53847c478bd9Sstevel@tonic-gate 	    0, NULL, 0,
53857c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
53867c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
53877c478bd9Sstevel@tonic-gate 		    "clearing port%d power failed (%d 0x%x %d)",
53887c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
53897c478bd9Sstevel@tonic-gate 
53907c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
53917c478bd9Sstevel@tonic-gate 
53927c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
53937c478bd9Sstevel@tonic-gate 	} else {
53947c478bd9Sstevel@tonic-gate 
53957c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
53967c478bd9Sstevel@tonic-gate 		ASSERT(completion_reason == 0);
53977c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~PORT_STATUS_PPS;
53987c478bd9Sstevel@tonic-gate 
53997c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
54007c478bd9Sstevel@tonic-gate 	}
54017c478bd9Sstevel@tonic-gate }
54027c478bd9Sstevel@tonic-gate 
54037c478bd9Sstevel@tonic-gate 
54047c478bd9Sstevel@tonic-gate /*
54057c478bd9Sstevel@tonic-gate  * Search the database of user preferences and find out the preferred
54067c478bd9Sstevel@tonic-gate  * configuration for this new device
54077c478bd9Sstevel@tonic-gate  */
5408ff0e937bSRaymond Chen int
54097c478bd9Sstevel@tonic-gate hubd_select_device_configuration(hubd_t *hubd, usb_port_t port,
54107c478bd9Sstevel@tonic-gate 	dev_info_t *child_dip, usba_device_t *child_ud)
54117c478bd9Sstevel@tonic-gate {
54127c478bd9Sstevel@tonic-gate 	char		*pathname = NULL;
54137c478bd9Sstevel@tonic-gate 	char		*tmp_path = NULL;
54147c478bd9Sstevel@tonic-gate 	int		user_conf;
54157c478bd9Sstevel@tonic-gate 	int		pathlen;
54167c478bd9Sstevel@tonic-gate 	usb_dev_descr_t	*usbdev_ptr;
54177c478bd9Sstevel@tonic-gate 	usba_configrec_t *user_pref;
54187c478bd9Sstevel@tonic-gate 
54197c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
54207c478bd9Sstevel@tonic-gate 	usbdev_ptr = child_ud->usb_dev_descr;
54217c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
54227c478bd9Sstevel@tonic-gate 
54237c478bd9Sstevel@tonic-gate 	/* try to get pathname for this device */
54247c478bd9Sstevel@tonic-gate 	tmp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
54257c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(child_dip, tmp_path);
54267c478bd9Sstevel@tonic-gate 
54277c478bd9Sstevel@tonic-gate 	pathlen = strlen(tmp_path) + 32;
54287c478bd9Sstevel@tonic-gate 	pathname = kmem_zalloc(pathlen, KM_SLEEP);
54297c478bd9Sstevel@tonic-gate 
54307c478bd9Sstevel@tonic-gate 	/*
54317c478bd9Sstevel@tonic-gate 	 * We haven't initialized the node and it doesn't have an address
54327c478bd9Sstevel@tonic-gate 	 * yet. Append port number to the physical pathname
54337c478bd9Sstevel@tonic-gate 	 */
54347c478bd9Sstevel@tonic-gate 	(void) sprintf(pathname, "%s@%d", tmp_path, port);
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
54377c478bd9Sstevel@tonic-gate 	    "hubd_select_device_configuration: Device=%s\n\t"
54387c478bd9Sstevel@tonic-gate 	    "Child path=%s",
54397c478bd9Sstevel@tonic-gate 	    usba_get_mfg_prod_sn_str(child_dip, tmp_path, MAXPATHLEN),
54407c478bd9Sstevel@tonic-gate 	    pathname);
54417c478bd9Sstevel@tonic-gate 	kmem_free(tmp_path, MAXPATHLEN);
54427c478bd9Sstevel@tonic-gate 
54437c478bd9Sstevel@tonic-gate 
54447c478bd9Sstevel@tonic-gate 	/* database search for user preferences */
54457c478bd9Sstevel@tonic-gate 	user_pref = usba_devdb_get_user_preferences(usbdev_ptr->idVendor,
54467c478bd9Sstevel@tonic-gate 	    usbdev_ptr->idProduct, child_ud->usb_serialno_str, pathname);
54477c478bd9Sstevel@tonic-gate 
54487c478bd9Sstevel@tonic-gate 	if (user_pref) {
54497c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
54507c478bd9Sstevel@tonic-gate 		    "hubd_select_device_configuration: "
54517c478bd9Sstevel@tonic-gate 		    "usba_devdb_get_user_preferences "
54527c478bd9Sstevel@tonic-gate 		    "return user_conf=%d\npreferred driver=%s path=%s",
54537c478bd9Sstevel@tonic-gate 		    user_pref->cfg_index, user_pref->driver,
54547c478bd9Sstevel@tonic-gate 		    user_pref->pathname);
54557c478bd9Sstevel@tonic-gate 
54567c478bd9Sstevel@tonic-gate 		user_conf = user_pref->cfg_index;
54577c478bd9Sstevel@tonic-gate 
54587c478bd9Sstevel@tonic-gate 		if (user_pref->driver) {
54597c478bd9Sstevel@tonic-gate 			mutex_enter(&child_ud->usb_mutex);
54607c478bd9Sstevel@tonic-gate 			child_ud->usb_preferred_driver = user_pref->driver;
54617c478bd9Sstevel@tonic-gate 			mutex_exit(&child_ud->usb_mutex);
54627c478bd9Sstevel@tonic-gate 		}
54637c478bd9Sstevel@tonic-gate 	} else {
54647c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
54657c478bd9Sstevel@tonic-gate 		    "hubd_select_device_configuration: No match found");
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 		/* select default configuration for this device */
54687c478bd9Sstevel@tonic-gate 		user_conf = USBA_DEV_CONFIG_INDEX_UNDEFINED;
54697c478bd9Sstevel@tonic-gate 	}
54707c478bd9Sstevel@tonic-gate 	kmem_free(pathname, pathlen);
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate 	/* if the device has just one configuration, set default value */
54737c478bd9Sstevel@tonic-gate 	if (usbdev_ptr->bNumConfigurations == 1) {
54747c478bd9Sstevel@tonic-gate 		user_conf = USB_DEV_DEFAULT_CONFIG_INDEX;
54757c478bd9Sstevel@tonic-gate 	}
54767c478bd9Sstevel@tonic-gate 
54777c478bd9Sstevel@tonic-gate 	return (user_conf);
54787c478bd9Sstevel@tonic-gate }
54797c478bd9Sstevel@tonic-gate 
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate /*
54827c478bd9Sstevel@tonic-gate  * Retrieves config cloud for this configuration
54837c478bd9Sstevel@tonic-gate  */
54847c478bd9Sstevel@tonic-gate int
54857c478bd9Sstevel@tonic-gate hubd_get_this_config_cloud(hubd_t *hubd, dev_info_t *dip,
54867c478bd9Sstevel@tonic-gate 	usba_device_t *child_ud, uint16_t conf_index)
54877c478bd9Sstevel@tonic-gate {
54887c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	*confdescr;
54897c478bd9Sstevel@tonic-gate 	mblk_t		*pdata = NULL;
54907c478bd9Sstevel@tonic-gate 	int		rval;
54917c478bd9Sstevel@tonic-gate 	size_t		size;
54927c478bd9Sstevel@tonic-gate 	char		*tmpbuf;
54937c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
54947c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
54957c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	def_ph;
54967c478bd9Sstevel@tonic-gate 
54977c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
54987c478bd9Sstevel@tonic-gate 	    "hubd_get_this_config_cloud: conf_index=%d", conf_index);
54997c478bd9Sstevel@tonic-gate 
55007c478bd9Sstevel@tonic-gate 
55017c478bd9Sstevel@tonic-gate 	/* alloc temporary space for config descriptor */
55027c478bd9Sstevel@tonic-gate 	confdescr = (usb_cfg_descr_t *)kmem_zalloc(USB_CFG_DESCR_SIZE,
55037c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
55047c478bd9Sstevel@tonic-gate 
55057c478bd9Sstevel@tonic-gate 	/* alloc temporary space for string descriptor */
55067c478bd9Sstevel@tonic-gate 	tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
55077c478bd9Sstevel@tonic-gate 
55087c478bd9Sstevel@tonic-gate 	def_ph = usba_get_dflt_pipe_handle(dip);
55097c478bd9Sstevel@tonic-gate 
55107c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
55117c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
55127c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,
55137c478bd9Sstevel@tonic-gate 	    USB_DESCR_TYPE_SETUP_CFG | conf_index,
55147c478bd9Sstevel@tonic-gate 	    0,
55157c478bd9Sstevel@tonic-gate 	    USB_CFG_DESCR_SIZE,
55167c478bd9Sstevel@tonic-gate 	    &pdata,
55177c478bd9Sstevel@tonic-gate 	    0,
55187c478bd9Sstevel@tonic-gate 	    &completion_reason,
55197c478bd9Sstevel@tonic-gate 	    &cb_flags,
55207c478bd9Sstevel@tonic-gate 	    0)) == USB_SUCCESS) {
55217c478bd9Sstevel@tonic-gate 
55227c478bd9Sstevel@tonic-gate 		/* this must be true since we didn't allow data underruns */
5523d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		if (MBLKL(pdata) != USB_CFG_DESCR_SIZE) {
552435f36846Ssl 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
552535f36846Ssl 			    "device returned incorrect configuration "
552635f36846Ssl 			    "descriptor size.");
552735f36846Ssl 
552835f36846Ssl 			rval = USB_FAILURE;
552935f36846Ssl 			goto done;
553035f36846Ssl 		}
55317c478bd9Sstevel@tonic-gate 
55327c478bd9Sstevel@tonic-gate 		/*
55337c478bd9Sstevel@tonic-gate 		 * Parse the configuration descriptor
55347c478bd9Sstevel@tonic-gate 		 */
55357c478bd9Sstevel@tonic-gate 		size = usb_parse_cfg_descr(pdata->b_rptr,
5536d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		    MBLKL(pdata), confdescr,
55377c478bd9Sstevel@tonic-gate 		    USB_CFG_DESCR_SIZE);
55387c478bd9Sstevel@tonic-gate 
55397c478bd9Sstevel@tonic-gate 		/* if parse cfg descr error, it should return failure */
55407c478bd9Sstevel@tonic-gate 		if (size == USB_PARSE_ERROR) {
55417c478bd9Sstevel@tonic-gate 
55427c478bd9Sstevel@tonic-gate 			if (pdata->b_rptr[1] != USB_DESCR_TYPE_CFG) {
5543d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
55447c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
55457c478bd9Sstevel@tonic-gate 				    "device returned incorrect "
55467c478bd9Sstevel@tonic-gate 				    "configuration descriptor type.");
55477c478bd9Sstevel@tonic-gate 			}
55487c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
55497c478bd9Sstevel@tonic-gate 			goto done;
55507c478bd9Sstevel@tonic-gate 		}
55517c478bd9Sstevel@tonic-gate 
55527c478bd9Sstevel@tonic-gate 		if (confdescr->wTotalLength < USB_CFG_DESCR_SIZE) {
5553d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
55547c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
55557c478bd9Sstevel@tonic-gate 			    "device returned incorrect "
55567c478bd9Sstevel@tonic-gate 			    "configuration descriptor size.");
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
55597c478bd9Sstevel@tonic-gate 			goto done;
55607c478bd9Sstevel@tonic-gate 		}
55617c478bd9Sstevel@tonic-gate 
55627c478bd9Sstevel@tonic-gate 		freemsg(pdata);
55637c478bd9Sstevel@tonic-gate 		pdata = NULL;
55647c478bd9Sstevel@tonic-gate 
55657c478bd9Sstevel@tonic-gate 		/* Now fetch the complete config cloud */
55667c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
55677c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
55687c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,
55697c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_CFG | conf_index,
55707c478bd9Sstevel@tonic-gate 		    0,
55717c478bd9Sstevel@tonic-gate 		    confdescr->wTotalLength,
55727c478bd9Sstevel@tonic-gate 		    &pdata,
55737c478bd9Sstevel@tonic-gate 		    0,
55747c478bd9Sstevel@tonic-gate 		    &completion_reason,
55757c478bd9Sstevel@tonic-gate 		    &cb_flags,
55767c478bd9Sstevel@tonic-gate 		    0)) == USB_SUCCESS) {
55777c478bd9Sstevel@tonic-gate 
5578d29f5a71Szhigang lu - Sun Microsystems - Beijing China 			if (MBLKL(pdata) !=
55797c478bd9Sstevel@tonic-gate 			    confdescr->wTotalLength) {
55807c478bd9Sstevel@tonic-gate 
5581d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
55827c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
55837c478bd9Sstevel@tonic-gate 				    "device returned incorrect "
55847c478bd9Sstevel@tonic-gate 				    "configuration descriptor.");
55857c478bd9Sstevel@tonic-gate 
55867c478bd9Sstevel@tonic-gate 				rval = USB_FAILURE;
55877c478bd9Sstevel@tonic-gate 				goto done;
55887c478bd9Sstevel@tonic-gate 			}
55897c478bd9Sstevel@tonic-gate 
55907c478bd9Sstevel@tonic-gate 			/*
55917c478bd9Sstevel@tonic-gate 			 * copy config descriptor into usba_device
55927c478bd9Sstevel@tonic-gate 			 */
55937c478bd9Sstevel@tonic-gate 			mutex_enter(&child_ud->usb_mutex);
55947c478bd9Sstevel@tonic-gate 			child_ud->usb_cfg_array[conf_index] =
55957c478bd9Sstevel@tonic-gate 			    kmem_alloc(confdescr->wTotalLength, KM_SLEEP);
55967c478bd9Sstevel@tonic-gate 			child_ud->usb_cfg_array_len[conf_index] =
5597c0f24e5bSlg 			    confdescr->wTotalLength;
55987c478bd9Sstevel@tonic-gate 			bcopy((caddr_t)pdata->b_rptr,
55997c478bd9Sstevel@tonic-gate 			    (caddr_t)child_ud->usb_cfg_array[conf_index],
56007c478bd9Sstevel@tonic-gate 			    confdescr->wTotalLength);
56017c478bd9Sstevel@tonic-gate 			mutex_exit(&child_ud->usb_mutex);
56027c478bd9Sstevel@tonic-gate 
56037c478bd9Sstevel@tonic-gate 			/*
56047c478bd9Sstevel@tonic-gate 			 * retrieve string descriptor describing this
56057c478bd9Sstevel@tonic-gate 			 * configuration
56067c478bd9Sstevel@tonic-gate 			 */
56077c478bd9Sstevel@tonic-gate 			if (confdescr->iConfiguration) {
56087c478bd9Sstevel@tonic-gate 
56097c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
56107c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
56117c478bd9Sstevel@tonic-gate 				    "Get conf str descr for config_index=%d",
56127c478bd9Sstevel@tonic-gate 				    conf_index);
56137c478bd9Sstevel@tonic-gate 
56147c478bd9Sstevel@tonic-gate 				/*
56157c478bd9Sstevel@tonic-gate 				 * Now fetch the string descriptor describing
56167c478bd9Sstevel@tonic-gate 				 * this configuration
56177c478bd9Sstevel@tonic-gate 				 */
56187c478bd9Sstevel@tonic-gate 				if ((rval = usb_get_string_descr(dip,
56197c478bd9Sstevel@tonic-gate 				    USB_LANG_ID, confdescr->iConfiguration,
56207c478bd9Sstevel@tonic-gate 				    tmpbuf, USB_MAXSTRINGLEN)) ==
56217c478bd9Sstevel@tonic-gate 				    USB_SUCCESS) {
56227c478bd9Sstevel@tonic-gate 					size = strlen(tmpbuf);
56237c478bd9Sstevel@tonic-gate 					if (size > 0) {
56247c478bd9Sstevel@tonic-gate 						child_ud->usb_cfg_str_descr
56257c478bd9Sstevel@tonic-gate 						    [conf_index] = (char *)
56267c478bd9Sstevel@tonic-gate 						    kmem_zalloc(size + 1,
56277c478bd9Sstevel@tonic-gate 						    KM_SLEEP);
56287c478bd9Sstevel@tonic-gate 						(void) strcpy(
56297c478bd9Sstevel@tonic-gate 						    child_ud->usb_cfg_str_descr
56307c478bd9Sstevel@tonic-gate 						    [conf_index], tmpbuf);
56317c478bd9Sstevel@tonic-gate 					}
56327c478bd9Sstevel@tonic-gate 				} else {
56337c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
56347c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
56357c478bd9Sstevel@tonic-gate 					    "hubd_get_this_config_cloud: "
56367c478bd9Sstevel@tonic-gate 					    "getting config string (%d) "
56377c478bd9Sstevel@tonic-gate 					    "failed",
56387c478bd9Sstevel@tonic-gate 					    confdescr->iConfiguration);
56397c478bd9Sstevel@tonic-gate 
56407c478bd9Sstevel@tonic-gate 					/* ignore this error */
56417c478bd9Sstevel@tonic-gate 					rval = USB_SUCCESS;
56427c478bd9Sstevel@tonic-gate 				}
56437c478bd9Sstevel@tonic-gate 			}
56447c478bd9Sstevel@tonic-gate 		}
56457c478bd9Sstevel@tonic-gate 	}
56467c478bd9Sstevel@tonic-gate 
56477c478bd9Sstevel@tonic-gate done:
56487c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
56497c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56507c478bd9Sstevel@tonic-gate 		    "hubd_get_this_config_cloud: "
56517c478bd9Sstevel@tonic-gate 		    "error in retrieving config descriptor for "
56527c478bd9Sstevel@tonic-gate 		    "config index=%d rval=%d cr=%d",
56537c478bd9Sstevel@tonic-gate 		    conf_index, rval, completion_reason);
56547c478bd9Sstevel@tonic-gate 	}
56557c478bd9Sstevel@tonic-gate 
56567c478bd9Sstevel@tonic-gate 	if (pdata) {
56577c478bd9Sstevel@tonic-gate 		freemsg(pdata);
56587c478bd9Sstevel@tonic-gate 		pdata = NULL;
56597c478bd9Sstevel@tonic-gate 	}
56607c478bd9Sstevel@tonic-gate 
56617c478bd9Sstevel@tonic-gate 	kmem_free(confdescr, USB_CFG_DESCR_SIZE);
56627c478bd9Sstevel@tonic-gate 	kmem_free(tmpbuf, USB_MAXSTRINGLEN);
56637c478bd9Sstevel@tonic-gate 
56647c478bd9Sstevel@tonic-gate 	return (rval);
56657c478bd9Sstevel@tonic-gate }
56667c478bd9Sstevel@tonic-gate 
56677c478bd9Sstevel@tonic-gate 
56687c478bd9Sstevel@tonic-gate /*
56697c478bd9Sstevel@tonic-gate  * Retrieves the entire config cloud for all configurations of the device
56707c478bd9Sstevel@tonic-gate  */
56717c478bd9Sstevel@tonic-gate int
56727c478bd9Sstevel@tonic-gate hubd_get_all_device_config_cloud(hubd_t *hubd, dev_info_t *dip,
56737c478bd9Sstevel@tonic-gate 	usba_device_t *child_ud)
56747c478bd9Sstevel@tonic-gate {
56757c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
56767c478bd9Sstevel@tonic-gate 	int		ncfgs;
56777c478bd9Sstevel@tonic-gate 	uint16_t	size;
56787c478bd9Sstevel@tonic-gate 	uint16_t	conf_index;
56797c478bd9Sstevel@tonic-gate 	uchar_t		**cfg_array;
56807c478bd9Sstevel@tonic-gate 	uint16_t	*cfg_array_len;
56817c478bd9Sstevel@tonic-gate 	char		**str_descr;
56827c478bd9Sstevel@tonic-gate 
56837c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
56847c478bd9Sstevel@tonic-gate 	    "hubd_get_all_device_config_cloud: Start");
56857c478bd9Sstevel@tonic-gate 
56867c478bd9Sstevel@tonic-gate 	/* alloc pointer array for conf. descriptors */
56877c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
56887c478bd9Sstevel@tonic-gate 	ncfgs = child_ud->usb_n_cfgs;
56897c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
56907c478bd9Sstevel@tonic-gate 
56917c478bd9Sstevel@tonic-gate 	size = sizeof (uchar_t *) * ncfgs;
56927c478bd9Sstevel@tonic-gate 	cfg_array = kmem_zalloc(size, KM_SLEEP);
56937c478bd9Sstevel@tonic-gate 	cfg_array_len = kmem_zalloc(ncfgs * sizeof (uint16_t), KM_SLEEP);
56947c478bd9Sstevel@tonic-gate 	str_descr = kmem_zalloc(size, KM_SLEEP);
56957c478bd9Sstevel@tonic-gate 
56967c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
56977c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array = cfg_array;
56987c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_len = cfg_array_len;
56997c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_length = size;
57007c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_len_length = ncfgs * sizeof (uint16_t);
57017c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_str_descr = str_descr;
57027c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
57037c478bd9Sstevel@tonic-gate 
57047c478bd9Sstevel@tonic-gate 	/* Get configuration descriptor for each configuration */
57057c478bd9Sstevel@tonic-gate 	for (conf_index = 0; (conf_index < ncfgs) &&
57067c478bd9Sstevel@tonic-gate 	    (rval == USB_SUCCESS); conf_index++) {
57077c478bd9Sstevel@tonic-gate 
57087c478bd9Sstevel@tonic-gate 		rval = hubd_get_this_config_cloud(hubd, dip, child_ud,
57097c478bd9Sstevel@tonic-gate 		    conf_index);
57107c478bd9Sstevel@tonic-gate 	}
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate 	return (rval);
57137c478bd9Sstevel@tonic-gate }
57147c478bd9Sstevel@tonic-gate 
57157c478bd9Sstevel@tonic-gate 
57167c478bd9Sstevel@tonic-gate /*
57177c478bd9Sstevel@tonic-gate  * hubd_ready_device:
57187c478bd9Sstevel@tonic-gate  *	Update the usba_device structure
57197c478bd9Sstevel@tonic-gate  *	Set the given configuration
57207c478bd9Sstevel@tonic-gate  *	Prepares the device node for driver to online. If an existing
57217c478bd9Sstevel@tonic-gate  *	OBP node is found, it will switch to the OBP node.
57227c478bd9Sstevel@tonic-gate  */
5723ff0e937bSRaymond Chen dev_info_t *
57247c478bd9Sstevel@tonic-gate hubd_ready_device(hubd_t *hubd, dev_info_t *child_dip, usba_device_t *child_ud,
572535f36846Ssl     uint_t config_index)
57267c478bd9Sstevel@tonic-gate {
57277c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
57287c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
57297c478bd9Sstevel@tonic-gate 	size_t		size;
57307c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	config_descriptor;
57317c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t def_ph;
57327c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph;
57337c478bd9Sstevel@tonic-gate 
57347c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5735112116d8Sfb 	    "hubd_ready_device: dip=0x%p, user_conf_index=%d",
5736112116d8Sfb 	    (void *)child_dip, config_index);
57377c478bd9Sstevel@tonic-gate 
57387c478bd9Sstevel@tonic-gate 	size = usb_parse_cfg_descr(
57397c478bd9Sstevel@tonic-gate 	    child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
57407c478bd9Sstevel@tonic-gate 	    &config_descriptor, USB_CFG_DESCR_SIZE);
57417c478bd9Sstevel@tonic-gate 	ASSERT(size == USB_CFG_DESCR_SIZE);
57427c478bd9Sstevel@tonic-gate 
57437c478bd9Sstevel@tonic-gate 	def_ph = usba_get_dflt_pipe_handle(child_dip);
57447c478bd9Sstevel@tonic-gate 
57457c478bd9Sstevel@tonic-gate 	/* Set the configuration */
57467c478bd9Sstevel@tonic-gate 	(void) usb_pipe_sync_ctrl_xfer(child_dip, def_ph,
57477c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_HOST_TO_DEV,
57487c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_CFG,	/* bRequest */
57497c478bd9Sstevel@tonic-gate 	    config_descriptor.bConfigurationValue,	/* wValue */
57507c478bd9Sstevel@tonic-gate 	    0,				/* wIndex */
57517c478bd9Sstevel@tonic-gate 	    0,				/* wLength */
57527c478bd9Sstevel@tonic-gate 	    NULL,
57537c478bd9Sstevel@tonic-gate 	    0,
57547c478bd9Sstevel@tonic-gate 	    &completion_reason,
57557c478bd9Sstevel@tonic-gate 	    &cb_flags,
57567c478bd9Sstevel@tonic-gate 	    0);
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
57597c478bd9Sstevel@tonic-gate 	child_ud->usb_active_cfg_ndx	= config_index;
57607c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg		= child_ud->usb_cfg_array[config_index];
57617c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_length	= config_descriptor.wTotalLength;
57627c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_value 	= config_descriptor.bConfigurationValue;
57637c478bd9Sstevel@tonic-gate 	child_ud->usb_n_ifs		= config_descriptor.bNumInterfaces;
57647c478bd9Sstevel@tonic-gate 	child_ud->usb_dip		= child_dip;
57657c478bd9Sstevel@tonic-gate 
57667c478bd9Sstevel@tonic-gate 	child_ud->usb_client_flags	= kmem_zalloc(
5767c0f24e5bSlg 	    child_ud->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
57687c478bd9Sstevel@tonic-gate 
57697c478bd9Sstevel@tonic-gate 	child_ud->usb_client_attach_list = kmem_zalloc(
5770c0f24e5bSlg 	    child_ud->usb_n_ifs *
5771c0f24e5bSlg 	    sizeof (*child_ud->usb_client_attach_list), KM_SLEEP);
57727c478bd9Sstevel@tonic-gate 
57737c478bd9Sstevel@tonic-gate 	child_ud->usb_client_ev_cb_list = kmem_zalloc(
5774c0f24e5bSlg 	    child_ud->usb_n_ifs *
5775c0f24e5bSlg 	    sizeof (*child_ud->usb_client_ev_cb_list), KM_SLEEP);
57767c478bd9Sstevel@tonic-gate 
57777c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
57787c478bd9Sstevel@tonic-gate 
57797c478bd9Sstevel@tonic-gate 	/* ready the device node */
57807c478bd9Sstevel@tonic-gate 	child_dip = usba_ready_device_node(child_dip);
57817c478bd9Sstevel@tonic-gate 
57827c478bd9Sstevel@tonic-gate 	/* set owner of default pipe to child dip */
57837c478bd9Sstevel@tonic-gate 	ph = usba_get_ph_data(def_ph);
57847c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
57857c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_ph_impl->usba_ph_mutex);
57867c478bd9Sstevel@tonic-gate 	ph->p_ph_impl->usba_ph_dip = ph->p_dip = child_dip;
57877c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_ph_impl->usba_ph_mutex);
57887c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
57897c478bd9Sstevel@tonic-gate 
57907c478bd9Sstevel@tonic-gate 	return (child_dip);
57917c478bd9Sstevel@tonic-gate }
57927c478bd9Sstevel@tonic-gate 
57937c478bd9Sstevel@tonic-gate 
57947c478bd9Sstevel@tonic-gate /*
57957c478bd9Sstevel@tonic-gate  * hubd_create_child
57967c478bd9Sstevel@tonic-gate  *	- create child dip
57977c478bd9Sstevel@tonic-gate  *	- open default pipe
57987c478bd9Sstevel@tonic-gate  *	- get device descriptor
57997c478bd9Sstevel@tonic-gate  *	- set the address
58007c478bd9Sstevel@tonic-gate  *	- get device string descriptors
58017c478bd9Sstevel@tonic-gate  *	- get the entire config cloud (all configurations) of the device
58027c478bd9Sstevel@tonic-gate  *	- set user preferred configuration
58037c478bd9Sstevel@tonic-gate  *	- close default pipe
58047c478bd9Sstevel@tonic-gate  *	- load appropriate driver(s)
58057c478bd9Sstevel@tonic-gate  */
58067c478bd9Sstevel@tonic-gate static int
58077c478bd9Sstevel@tonic-gate hubd_create_child(dev_info_t *dip,
58087c478bd9Sstevel@tonic-gate 		hubd_t		*hubd,
58097c478bd9Sstevel@tonic-gate 		usba_device_t	*hubd_ud,
58107c478bd9Sstevel@tonic-gate 		usb_port_status_t port_status,
58117c478bd9Sstevel@tonic-gate 		usb_port_t	port,
58127c478bd9Sstevel@tonic-gate 		int		iteration)
58137c478bd9Sstevel@tonic-gate {
58147c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
58157c478bd9Sstevel@tonic-gate 	usb_dev_descr_t	usb_dev_descr;
58167c478bd9Sstevel@tonic-gate 	int			rval;
58177c478bd9Sstevel@tonic-gate 	usba_device_t		*child_ud = NULL;
58187c478bd9Sstevel@tonic-gate 	usba_device_t		*parent_ud = NULL;
58197c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	ph = NULL; /* default pipe handle */
58207c478bd9Sstevel@tonic-gate 	mblk_t			*pdata = NULL;
58217c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
582235f36846Ssl 	int			user_conf_index;
582335f36846Ssl 	uint_t			config_index;
58247c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
58257c478bd9Sstevel@tonic-gate 	uchar_t			address = 0;
58267c478bd9Sstevel@tonic-gate 	uint16_t		length;
58277c478bd9Sstevel@tonic-gate 	size_t			size;
58287c478bd9Sstevel@tonic-gate 	usb_addr_t		parent_usb_addr;
58297c478bd9Sstevel@tonic-gate 	usb_port_t		parent_usb_port;
58307c478bd9Sstevel@tonic-gate 	usba_device_t		*parent_usba_dev;
58317c478bd9Sstevel@tonic-gate 	usb_port_status_t	parent_port_status;
58327c478bd9Sstevel@tonic-gate 
58337c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
58347c478bd9Sstevel@tonic-gate 	    "hubd_create_child: port=%d", port);
58357c478bd9Sstevel@tonic-gate 
58367c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
58377c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_usba_devices[port] == NULL);
58387c478bd9Sstevel@tonic-gate 
58397c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
58407c478bd9Sstevel@tonic-gate 
58417c478bd9Sstevel@tonic-gate 	/*
58427c478bd9Sstevel@tonic-gate 	 * create a dip which can be used to open the pipe. we set
58437c478bd9Sstevel@tonic-gate 	 * the name after getting the descriptors from the device
58447c478bd9Sstevel@tonic-gate 	 */
58457c478bd9Sstevel@tonic-gate 	rval = usba_create_child_devi(dip,
5846c0f24e5bSlg 	    "device",		/* driver name */
5847c0f24e5bSlg 	    hubd_ud->usb_hcdi_ops, /* usba_hcdi ops */
5848c0f24e5bSlg 	    hubd_ud->usb_root_hub_dip,
5849c0f24e5bSlg 	    port_status,		/* low speed device */
5850c0f24e5bSlg 	    child_ud,
5851c0f24e5bSlg 	    &child_dip);
58527c478bd9Sstevel@tonic-gate 
58537c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
58547c478bd9Sstevel@tonic-gate 
5855d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
58567c478bd9Sstevel@tonic-gate 		    "usb_create_child_devi failed (%d)", rval);
58577c478bd9Sstevel@tonic-gate 
58587c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
58597c478bd9Sstevel@tonic-gate 	}
58607c478bd9Sstevel@tonic-gate 
58617c478bd9Sstevel@tonic-gate 	child_ud = usba_get_usba_device(child_dip);
58627c478bd9Sstevel@tonic-gate 	ASSERT(child_ud != NULL);
58637c478bd9Sstevel@tonic-gate 
58647c478bd9Sstevel@tonic-gate 	parent_ud = hubd->h_usba_device;
58657c478bd9Sstevel@tonic-gate 	mutex_enter(&parent_ud->usb_mutex);
58667c478bd9Sstevel@tonic-gate 	parent_port_status = parent_ud->usb_port_status;
58677c478bd9Sstevel@tonic-gate 
58687c478bd9Sstevel@tonic-gate 	/*
58697c478bd9Sstevel@tonic-gate 	 * To support split transactions, update address and port
58707c478bd9Sstevel@tonic-gate 	 * of high speed hub to which given device is connected.
58717c478bd9Sstevel@tonic-gate 	 */
58727c478bd9Sstevel@tonic-gate 	if (parent_port_status == USBA_HIGH_SPEED_DEV) {
58737c478bd9Sstevel@tonic-gate 		parent_usba_dev = parent_ud;
58747c478bd9Sstevel@tonic-gate 		parent_usb_addr = parent_ud->usb_addr;
58757c478bd9Sstevel@tonic-gate 		parent_usb_port = port;
58767c478bd9Sstevel@tonic-gate 	} else {
58777c478bd9Sstevel@tonic-gate 		parent_usba_dev = parent_ud->usb_hs_hub_usba_dev;
58787c478bd9Sstevel@tonic-gate 		parent_usb_addr = parent_ud->usb_hs_hub_addr;
58797c478bd9Sstevel@tonic-gate 		parent_usb_port = parent_ud->usb_hs_hub_port;
58807c478bd9Sstevel@tonic-gate 	}
58817c478bd9Sstevel@tonic-gate 	mutex_exit(&parent_ud->usb_mutex);
58827c478bd9Sstevel@tonic-gate 
58837c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
58847c478bd9Sstevel@tonic-gate 	address = child_ud->usb_addr;
58857c478bd9Sstevel@tonic-gate 	child_ud->usb_addr = 0;
58867c478bd9Sstevel@tonic-gate 	child_ud->usb_dev_descr = kmem_alloc(sizeof (usb_dev_descr_t),
58877c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
58887c478bd9Sstevel@tonic-gate 	bzero(&usb_dev_descr, sizeof (usb_dev_descr_t));
58897c478bd9Sstevel@tonic-gate 	usb_dev_descr.bMaxPacketSize0 =
58907c478bd9Sstevel@tonic-gate 	    (port_status == USBA_LOW_SPEED_DEV) ? 8 : 64;
58917c478bd9Sstevel@tonic-gate 	bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
58927c478bd9Sstevel@tonic-gate 	    sizeof (usb_dev_descr_t));
58937c478bd9Sstevel@tonic-gate 	child_ud->usb_port = port;
58947c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_usba_dev = parent_usba_dev;
58957c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_addr = parent_usb_addr;
58967c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_port = parent_usb_port;
58977c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
58987c478bd9Sstevel@tonic-gate 
58997c478bd9Sstevel@tonic-gate 	/* Open the default pipe */
59007c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
59017c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
5902d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
5903d291d9f2Sfrits 		    "usb_pipe_open failed (%d)", rval);
59047c478bd9Sstevel@tonic-gate 
59057c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
59067c478bd9Sstevel@tonic-gate 	}
59077c478bd9Sstevel@tonic-gate 
59087c478bd9Sstevel@tonic-gate 	/*
59097c478bd9Sstevel@tonic-gate 	 * get device descriptor
59107c478bd9Sstevel@tonic-gate 	 */
59117c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59127c478bd9Sstevel@tonic-gate 	    "hubd_create_child: get device descriptor: 64 bytes");
59137c478bd9Sstevel@tonic-gate 
59147c478bd9Sstevel@tonic-gate 	rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
59157c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
59167c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,			/* bRequest */
59177c478bd9Sstevel@tonic-gate 	    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
59187c478bd9Sstevel@tonic-gate 	    0,					/* wIndex */
59197c478bd9Sstevel@tonic-gate 	    64,					/* wLength */
59207c478bd9Sstevel@tonic-gate 	    &pdata, USB_ATTRS_SHORT_XFER_OK,
59217c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0);
59227c478bd9Sstevel@tonic-gate 
59237c478bd9Sstevel@tonic-gate 	if ((rval != USB_SUCCESS) &&
59247c478bd9Sstevel@tonic-gate 	    (!((completion_reason == USB_CR_DATA_OVERRUN) && pdata))) {
59257c478bd9Sstevel@tonic-gate 
59267c478bd9Sstevel@tonic-gate 		/*
59277c478bd9Sstevel@tonic-gate 		 * rval != USB_SUCCESS AND
59287c478bd9Sstevel@tonic-gate 		 * completion_reason != USB_CR_DATA_OVERRUN
59297c478bd9Sstevel@tonic-gate 		 * pdata could be != NULL.
59307c478bd9Sstevel@tonic-gate 		 * Free pdata now to prevent memory leak.
59317c478bd9Sstevel@tonic-gate 		 */
59327c478bd9Sstevel@tonic-gate 		freemsg(pdata);
59337c478bd9Sstevel@tonic-gate 		pdata = NULL;
59347c478bd9Sstevel@tonic-gate 
59357c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59367c478bd9Sstevel@tonic-gate 		    "hubd_create_child: get device descriptor: 8 bytes");
59377c478bd9Sstevel@tonic-gate 
59387c478bd9Sstevel@tonic-gate 		rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
59397c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
59407c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
59417c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
59427c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
59437c478bd9Sstevel@tonic-gate 		    8,					/* wLength */
59447c478bd9Sstevel@tonic-gate 		    &pdata, USB_ATTRS_NONE,
59457c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0);
59467c478bd9Sstevel@tonic-gate 
59477c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
5948d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59497c478bd9Sstevel@tonic-gate 			    "getting device descriptor failed (%s 0x%x %d)",
59507c478bd9Sstevel@tonic-gate 			    usb_str_cr(completion_reason), cb_flags, rval);
59517c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
59527c478bd9Sstevel@tonic-gate 		}
59537c478bd9Sstevel@tonic-gate 	} else {
59547c478bd9Sstevel@tonic-gate 		ASSERT(completion_reason == USB_CR_OK);
59557c478bd9Sstevel@tonic-gate 	}
59567c478bd9Sstevel@tonic-gate 
59577c478bd9Sstevel@tonic-gate 	ASSERT(pdata != NULL);
59587c478bd9Sstevel@tonic-gate 
59597c478bd9Sstevel@tonic-gate 	size = usb_parse_dev_descr(
5960c0f24e5bSlg 	    pdata->b_rptr,
5961d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	    MBLKL(pdata),
5962c0f24e5bSlg 	    &usb_dev_descr,
5963c0f24e5bSlg 	    sizeof (usb_dev_descr_t));
59647c478bd9Sstevel@tonic-gate 
59657c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59667c478bd9Sstevel@tonic-gate 	    "parsing device descriptor returned %lu", size);
59677c478bd9Sstevel@tonic-gate 
59687c478bd9Sstevel@tonic-gate 	length = *(pdata->b_rptr);
59697c478bd9Sstevel@tonic-gate 	freemsg(pdata);
59707c478bd9Sstevel@tonic-gate 	pdata = NULL;
59717c478bd9Sstevel@tonic-gate 	if (size < 8) {
5972d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59737c478bd9Sstevel@tonic-gate 		    "get device descriptor returned %lu bytes", size);
59747c478bd9Sstevel@tonic-gate 
59757c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
59767c478bd9Sstevel@tonic-gate 	}
59777c478bd9Sstevel@tonic-gate 
59787c478bd9Sstevel@tonic-gate 	if (length < 8) {
5979d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59807c478bd9Sstevel@tonic-gate 		    "fail enumeration: bLength=%d", length);
59817c478bd9Sstevel@tonic-gate 
59827c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
59837c478bd9Sstevel@tonic-gate 	}
59847c478bd9Sstevel@tonic-gate 
59857c478bd9Sstevel@tonic-gate 	/* Set the address of the device */
59867c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
59877c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_HOST_TO_DEV,
59887c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_ADDRESS,	/* bRequest */
59897c478bd9Sstevel@tonic-gate 	    address,			/* wValue */
59907c478bd9Sstevel@tonic-gate 	    0,				/* wIndex */
59917c478bd9Sstevel@tonic-gate 	    0,				/* wLength */
59927c478bd9Sstevel@tonic-gate 	    NULL, 0,
59937c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
59947c478bd9Sstevel@tonic-gate 		char buffer[64];
5995d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59967c478bd9Sstevel@tonic-gate 		    "setting address failed (cr=%s cb_flags=%s rval=%d)",
59977c478bd9Sstevel@tonic-gate 		    usb_str_cr(completion_reason),
59987c478bd9Sstevel@tonic-gate 		    usb_str_cb_flags(cb_flags, buffer, sizeof (buffer)),
59997c478bd9Sstevel@tonic-gate 		    rval);
60007c478bd9Sstevel@tonic-gate 
60017c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
60027c478bd9Sstevel@tonic-gate 	}
60037c478bd9Sstevel@tonic-gate 
60047c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
60057c478bd9Sstevel@tonic-gate 	    "set address 0x%x done", address);
60067c478bd9Sstevel@tonic-gate 
60077c478bd9Sstevel@tonic-gate 	/* now close the pipe for addr 0 */
60087c478bd9Sstevel@tonic-gate 	usb_pipe_close(child_dip, ph,
60097c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
60107c478bd9Sstevel@tonic-gate 
60117c478bd9Sstevel@tonic-gate 	/*
60127c478bd9Sstevel@tonic-gate 	 * This delay is important for the CATC hub to enumerate
60137c478bd9Sstevel@tonic-gate 	 * But, avoid delay in the first iteration
60147c478bd9Sstevel@tonic-gate 	 */
60157c478bd9Sstevel@tonic-gate 	if (iteration) {
60167c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(hubd_device_delay/100));
60177c478bd9Sstevel@tonic-gate 	}
60187c478bd9Sstevel@tonic-gate 
60197c478bd9Sstevel@tonic-gate 	/* assign the address in the usba_device structure */
60207c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
60217c478bd9Sstevel@tonic-gate 	child_ud->usb_addr = address;
60227c478bd9Sstevel@tonic-gate 	child_ud->usb_no_cpr = 0;
60237c478bd9Sstevel@tonic-gate 	child_ud->usb_port_status = port_status;
60247c478bd9Sstevel@tonic-gate 	/* save this device descriptor */
60257c478bd9Sstevel@tonic-gate 	bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
6026c0f24e5bSlg 	    sizeof (usb_dev_descr_t));
60277c478bd9Sstevel@tonic-gate 	child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
60287c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
60297c478bd9Sstevel@tonic-gate 
60307c478bd9Sstevel@tonic-gate 	/* re-open the pipe for the device with the new address */
60317c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
60327c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
6033d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
60347c478bd9Sstevel@tonic-gate 		    "usb_pipe_open failed (%d)", rval);
60357c478bd9Sstevel@tonic-gate 
60367c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
60377c478bd9Sstevel@tonic-gate 	}
60387c478bd9Sstevel@tonic-gate 
60397c478bd9Sstevel@tonic-gate 	/*
60407c478bd9Sstevel@tonic-gate 	 * Get full device descriptor only if we have not received full
60417c478bd9Sstevel@tonic-gate 	 * device descriptor earlier.
60427c478bd9Sstevel@tonic-gate 	 */
60437c478bd9Sstevel@tonic-gate 	if (size < length) {
60447c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
60457c478bd9Sstevel@tonic-gate 		    "hubd_create_child: get full device descriptor: "
60467c478bd9Sstevel@tonic-gate 		    "%d bytes", length);
60477c478bd9Sstevel@tonic-gate 
60487c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
60497c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
60507c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
60517c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
60527c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
60537c478bd9Sstevel@tonic-gate 		    length,				/* wLength */
60547c478bd9Sstevel@tonic-gate 		    &pdata, 0,
60557c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
60567c478bd9Sstevel@tonic-gate 			freemsg(pdata);
60577c478bd9Sstevel@tonic-gate 			pdata = NULL;
60587c478bd9Sstevel@tonic-gate 
60597c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
60607c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
60617c478bd9Sstevel@tonic-gate 			    "hubd_create_child: get full device descriptor: "
60627c478bd9Sstevel@tonic-gate 			    "64 bytes");
60637c478bd9Sstevel@tonic-gate 
60647c478bd9Sstevel@tonic-gate 			rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
60657c478bd9Sstevel@tonic-gate 			    USB_DEV_REQ_DEV_TO_HOST |
60667c478bd9Sstevel@tonic-gate 			    USB_DEV_REQ_TYPE_STANDARD,
60677c478bd9Sstevel@tonic-gate 			    USB_REQ_GET_DESCR,		/* bRequest */
60687c478bd9Sstevel@tonic-gate 			    USB_DESCR_TYPE_SETUP_DEV,	/* wValue */
60697c478bd9Sstevel@tonic-gate 			    0,				/* wIndex */
60707c478bd9Sstevel@tonic-gate 			    64,				/* wLength */
60717c478bd9Sstevel@tonic-gate 			    &pdata, USB_ATTRS_SHORT_XFER_OK,
60727c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0);
60737c478bd9Sstevel@tonic-gate 
60747c478bd9Sstevel@tonic-gate 			/* we have to trust the data now */
60757c478bd9Sstevel@tonic-gate 			if (pdata) {
60767c478bd9Sstevel@tonic-gate 				int len = *(pdata->b_rptr);
60777c478bd9Sstevel@tonic-gate 
6078d29f5a71Szhigang lu - Sun Microsystems - Beijing China 				length = MBLKL(pdata);
60797c478bd9Sstevel@tonic-gate 				if (length < len) {
60807c478bd9Sstevel@tonic-gate 
60817c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
60827c478bd9Sstevel@tonic-gate 				}
60837c478bd9Sstevel@tonic-gate 			} else if (rval != USB_SUCCESS) {
6084d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
60857c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
60867c478bd9Sstevel@tonic-gate 				    "getting device descriptor failed "
60877c478bd9Sstevel@tonic-gate 				    "(%d 0x%x %d)",
6088c0f24e5bSlg 				    completion_reason, cb_flags, rval);
60897c478bd9Sstevel@tonic-gate 
60907c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
60917c478bd9Sstevel@tonic-gate 			}
60927c478bd9Sstevel@tonic-gate 		}
60937c478bd9Sstevel@tonic-gate 
60947c478bd9Sstevel@tonic-gate 		size = usb_parse_dev_descr(
6095c0f24e5bSlg 		    pdata->b_rptr,
6096d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		    MBLKL(pdata),
6097c0f24e5bSlg 		    &usb_dev_descr,
6098c0f24e5bSlg 		    sizeof (usb_dev_descr_t));
60997c478bd9Sstevel@tonic-gate 
61007c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61017c478bd9Sstevel@tonic-gate 		    "parsing device descriptor returned %lu", size);
61027c478bd9Sstevel@tonic-gate 
61037c478bd9Sstevel@tonic-gate 		/*
61047c478bd9Sstevel@tonic-gate 		 * For now, free the data
61057c478bd9Sstevel@tonic-gate 		 * eventually, each configuration may need to be looked at
61067c478bd9Sstevel@tonic-gate 		 */
61077c478bd9Sstevel@tonic-gate 		freemsg(pdata);
61087c478bd9Sstevel@tonic-gate 		pdata = NULL;
61097c478bd9Sstevel@tonic-gate 
61107c478bd9Sstevel@tonic-gate 		if (size != USB_DEV_DESCR_SIZE) {
6111d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61127c478bd9Sstevel@tonic-gate 			    "fail enumeration: descriptor size=%lu "
61137c478bd9Sstevel@tonic-gate 			    "expected size=%u", size, USB_DEV_DESCR_SIZE);
61147c478bd9Sstevel@tonic-gate 
61157c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
61167c478bd9Sstevel@tonic-gate 		}
61177c478bd9Sstevel@tonic-gate 
61187c478bd9Sstevel@tonic-gate 		/*
61197c478bd9Sstevel@tonic-gate 		 * save the device descriptor in usba_device since it is needed
61207c478bd9Sstevel@tonic-gate 		 * later on again
61217c478bd9Sstevel@tonic-gate 		 */
61227c478bd9Sstevel@tonic-gate 		mutex_enter(&child_ud->usb_mutex);
61237c478bd9Sstevel@tonic-gate 		bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
6124c0f24e5bSlg 		    sizeof (usb_dev_descr_t));
61257c478bd9Sstevel@tonic-gate 		child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
61267c478bd9Sstevel@tonic-gate 		mutex_exit(&child_ud->usb_mutex);
61277c478bd9Sstevel@tonic-gate 	}
61287c478bd9Sstevel@tonic-gate 
61297c478bd9Sstevel@tonic-gate 	if (usb_dev_descr.bNumConfigurations == 0) {
6130d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61317c478bd9Sstevel@tonic-gate 		    "device descriptor:\n\t"
61327c478bd9Sstevel@tonic-gate 		    "l=0x%x type=0x%x USB=0x%x class=0x%x subclass=0x%x\n\t"
61337c478bd9Sstevel@tonic-gate 		    "protocol=0x%x maxpktsize=0x%x "
61347c478bd9Sstevel@tonic-gate 		    "Vid=0x%x Pid=0x%x rel=0x%x\n\t"
61357c478bd9Sstevel@tonic-gate 		    "Mfg=0x%x P=0x%x sn=0x%x #config=0x%x",
61367c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bLength, usb_dev_descr.bDescriptorType,
61377c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bcdUSB, usb_dev_descr.bDeviceClass,
61387c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bDeviceSubClass,
61397c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bDeviceProtocol,
61407c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bMaxPacketSize0,
61417c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idVendor,
61427c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idProduct, usb_dev_descr.bcdDevice,
61437c478bd9Sstevel@tonic-gate 		    usb_dev_descr.iManufacturer, usb_dev_descr.iProduct,
61447c478bd9Sstevel@tonic-gate 		    usb_dev_descr.iSerialNumber,
61457c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bNumConfigurations);
61467c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
61477c478bd9Sstevel@tonic-gate 	}
61487c478bd9Sstevel@tonic-gate 
61497c478bd9Sstevel@tonic-gate 
61507c478bd9Sstevel@tonic-gate 	/* get the device string descriptor(s) */
61517c478bd9Sstevel@tonic-gate 	usba_get_dev_string_descrs(child_dip, child_ud);
61527c478bd9Sstevel@tonic-gate 
61537c478bd9Sstevel@tonic-gate 	/* retrieve config cloud for all configurations */
61547c478bd9Sstevel@tonic-gate 	rval = hubd_get_all_device_config_cloud(hubd, child_dip, child_ud);
61557c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
6156d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61577c478bd9Sstevel@tonic-gate 		    "failed to get configuration descriptor(s)");
61587c478bd9Sstevel@tonic-gate 
61597c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
61607c478bd9Sstevel@tonic-gate 	}
61617c478bd9Sstevel@tonic-gate 
61627c478bd9Sstevel@tonic-gate 	/* get the preferred configuration for this device */
61637c478bd9Sstevel@tonic-gate 	user_conf_index = hubd_select_device_configuration(hubd, port,
61647c478bd9Sstevel@tonic-gate 	    child_dip, child_ud);
61657c478bd9Sstevel@tonic-gate 
61667c478bd9Sstevel@tonic-gate 	/* Check if the user selected configuration index is in range */
616735f36846Ssl 	if ((user_conf_index >= usb_dev_descr.bNumConfigurations) ||
616835f36846Ssl 	    (user_conf_index < 0)) {
6169d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61707c478bd9Sstevel@tonic-gate 		    "Configuration index for device idVendor=%d "
61717c478bd9Sstevel@tonic-gate 		    "idProduct=%d is=%d, and is out of range[0..%d]",
61727c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idVendor, usb_dev_descr.idProduct,
61737c478bd9Sstevel@tonic-gate 		    user_conf_index, usb_dev_descr.bNumConfigurations - 1);
61747c478bd9Sstevel@tonic-gate 
61757c478bd9Sstevel@tonic-gate 		/* treat this as user didn't specify configuration */
61767c478bd9Sstevel@tonic-gate 		user_conf_index = USBA_DEV_CONFIG_INDEX_UNDEFINED;
61777c478bd9Sstevel@tonic-gate 	}
61787c478bd9Sstevel@tonic-gate 
61797c478bd9Sstevel@tonic-gate 
61807c478bd9Sstevel@tonic-gate 	/*
61817c478bd9Sstevel@tonic-gate 	 * Warn users of a performance hit if connecting a
61827c478bd9Sstevel@tonic-gate 	 * High Speed behind a 1.1 hub, which is behind a
61837c478bd9Sstevel@tonic-gate 	 * 2.0 port.
61847c478bd9Sstevel@tonic-gate 	 */
61857c478bd9Sstevel@tonic-gate 	if ((parent_port_status != USBA_HIGH_SPEED_DEV) &&
61867c478bd9Sstevel@tonic-gate 	    !(usba_is_root_hub(parent_ud->usb_dip)) &&
61877c478bd9Sstevel@tonic-gate 	    (parent_usb_addr)) {
61887c478bd9Sstevel@tonic-gate 
61897c478bd9Sstevel@tonic-gate 		/*
61907c478bd9Sstevel@tonic-gate 		 * Now that we know the root port is a high speed port
61917c478bd9Sstevel@tonic-gate 		 * and that the parent port is not a high speed port,
61927c478bd9Sstevel@tonic-gate 		 * let's find out if the device itself is a high speed
61937c478bd9Sstevel@tonic-gate 		 * device.  If it is a high speed device,
61947c478bd9Sstevel@tonic-gate 		 * USB_DESCR_TYPE_SETUP_DEV_QLF should return a value,
61957c478bd9Sstevel@tonic-gate 		 * otherwise the command will fail.
61967c478bd9Sstevel@tonic-gate 		 */
61977c478bd9Sstevel@tonic-gate 		rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
61987c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
61997c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
62007c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV_QLF,	/* wValue */
62017c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
62027c478bd9Sstevel@tonic-gate 		    10,					/* wLength */
62037c478bd9Sstevel@tonic-gate 		    &pdata, USB_ATTRS_SHORT_XFER_OK,
62047c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0);
62057c478bd9Sstevel@tonic-gate 
62067c478bd9Sstevel@tonic-gate 		if (pdata) {
62077c478bd9Sstevel@tonic-gate 			freemsg(pdata);
62087c478bd9Sstevel@tonic-gate 			pdata = NULL;
62097c478bd9Sstevel@tonic-gate 		}
62107c478bd9Sstevel@tonic-gate 
62117c478bd9Sstevel@tonic-gate 		/*
62127c478bd9Sstevel@tonic-gate 		 * USB_DESCR_TYPE_SETUP_DEV_QLF query was successful
62137c478bd9Sstevel@tonic-gate 		 * that means this is a high speed device behind a
62147c478bd9Sstevel@tonic-gate 		 * high speed root hub, but running at full speed
62157c478bd9Sstevel@tonic-gate 		 * because there is a full speed hub in the middle.
62167c478bd9Sstevel@tonic-gate 		 */
62177c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
6218c0f24e5bSlg 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
6219c0f24e5bSlg 			    hubd->h_log_handle,
6220c0f24e5bSlg 			    "Connecting a high speed device to a "
6221c0f24e5bSlg 			    "non high speed hub (port %d) will result "
6222112cd14aSqz 			    "in a loss of performance.	Please connect "
6223c0f24e5bSlg 			    "the device to a high speed hub to get "
6224c0f24e5bSlg 			    "the maximum performance.",
6225c0f24e5bSlg 			    port);
62267c478bd9Sstevel@tonic-gate 		}
62277c478bd9Sstevel@tonic-gate 	}
62287c478bd9Sstevel@tonic-gate 
62297c478bd9Sstevel@tonic-gate 	/*
62307c478bd9Sstevel@tonic-gate 	 * Now we try to online the device by attaching a driver
62317c478bd9Sstevel@tonic-gate 	 * The following truth table illustrates the logic:-
62327c478bd9Sstevel@tonic-gate 	 * Cfgndx	Driver	Action
62337c478bd9Sstevel@tonic-gate 	 * 0		0	loop all configs for driver with full
62347c478bd9Sstevel@tonic-gate 	 *			compatible properties.
62357c478bd9Sstevel@tonic-gate 	 * 0		1	set first configuration,
62367c478bd9Sstevel@tonic-gate 	 *			compatible prop = drivername.
62377c478bd9Sstevel@tonic-gate 	 * 1		0	Set config, full compatible prop
62387c478bd9Sstevel@tonic-gate 	 * 1		1	Set config, compatible prop = drivername.
62397c478bd9Sstevel@tonic-gate 	 *
62407c478bd9Sstevel@tonic-gate 	 * Note:
62417c478bd9Sstevel@tonic-gate 	 *	cfgndx = user_conf_index
62427c478bd9Sstevel@tonic-gate 	 *	Driver = usb_preferred_driver
62437c478bd9Sstevel@tonic-gate 	 */
62447c478bd9Sstevel@tonic-gate 	if (user_conf_index == USBA_DEV_CONFIG_INDEX_UNDEFINED) {
62457c478bd9Sstevel@tonic-gate 		if (child_ud->usb_preferred_driver) {
62467c478bd9Sstevel@tonic-gate 			/*
62477c478bd9Sstevel@tonic-gate 			 * It is the job of the "preferred driver" to put the
62487c478bd9Sstevel@tonic-gate 			 * device in the desired configuration. Till then
62497c478bd9Sstevel@tonic-gate 			 * put the device in config index 0.
62507c478bd9Sstevel@tonic-gate 			 */
625135f36846Ssl 			if ((rval = usba_hubdi_check_power_budget(dip, child_ud,
625235f36846Ssl 			    USB_DEV_DEFAULT_CONFIG_INDEX)) != USB_SUCCESS) {
625335f36846Ssl 
625435f36846Ssl 				goto fail_cleanup;
625535f36846Ssl 			}
625635f36846Ssl 
62577c478bd9Sstevel@tonic-gate 			child_dip = hubd_ready_device(hubd, child_dip,
62587c478bd9Sstevel@tonic-gate 			    child_ud, USB_DEV_DEFAULT_CONFIG_INDEX);
62597c478bd9Sstevel@tonic-gate 
62607c478bd9Sstevel@tonic-gate 			/*
62617c478bd9Sstevel@tonic-gate 			 * Assign the dip before onlining to avoid race
62627c478bd9Sstevel@tonic-gate 			 * with busctl
62637c478bd9Sstevel@tonic-gate 			 */
62647c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
62657c478bd9Sstevel@tonic-gate 			hubd->h_children_dips[port] = child_dip;
62667c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
62677c478bd9Sstevel@tonic-gate 
62687c478bd9Sstevel@tonic-gate 			(void) usba_bind_driver(child_dip);
62697c478bd9Sstevel@tonic-gate 		} else {
62707c478bd9Sstevel@tonic-gate 			/*
62717c478bd9Sstevel@tonic-gate 			 * loop through all the configurations to see if we
62727c478bd9Sstevel@tonic-gate 			 * can find a driver for any one config. If not, set
62737c478bd9Sstevel@tonic-gate 			 * the device in config_index 0
62747c478bd9Sstevel@tonic-gate 			 */
62757c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
62767c478bd9Sstevel@tonic-gate 			for (config_index = 0;
62777c478bd9Sstevel@tonic-gate 			    (config_index < usb_dev_descr.bNumConfigurations) &&
62787c478bd9Sstevel@tonic-gate 			    (rval != USB_SUCCESS); config_index++) {
62797c478bd9Sstevel@tonic-gate 
62807c478bd9Sstevel@tonic-gate 				child_dip = hubd_ready_device(hubd, child_dip,
62817c478bd9Sstevel@tonic-gate 				    child_ud, config_index);
62827c478bd9Sstevel@tonic-gate 
62837c478bd9Sstevel@tonic-gate 				/*
62847c478bd9Sstevel@tonic-gate 				 * Assign the dip before onlining to avoid race
62857c478bd9Sstevel@tonic-gate 				 * with busctl
62867c478bd9Sstevel@tonic-gate 				 */
62877c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
62887c478bd9Sstevel@tonic-gate 				hubd->h_children_dips[port] = child_dip;
62897c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
62907c478bd9Sstevel@tonic-gate 
62917c478bd9Sstevel@tonic-gate 				rval = usba_bind_driver(child_dip);
629235f36846Ssl 
629335f36846Ssl 				/*
629435f36846Ssl 				 * Normally power budget should be checked
629535f36846Ssl 				 * before device is configured. A failure in
629635f36846Ssl 				 * power budget checking will stop the device
629735f36846Ssl 				 * from being configured with current
629835f36846Ssl 				 * config_index and may enable the device to
629935f36846Ssl 				 * be configured in another configuration.
630035f36846Ssl 				 * This may break the user experience that a
630135f36846Ssl 				 * device which previously worked in config
630235f36846Ssl 				 * A now works in config B after power budget
630335f36846Ssl 				 * control is enabled. To avoid such situation,
630435f36846Ssl 				 * power budget checking is moved here and will
630535f36846Ssl 				 * fail the child creation directly if config
630635f36846Ssl 				 * A exceeds the power available.
630735f36846Ssl 				 */
630835f36846Ssl 				if (rval == USB_SUCCESS) {
630935f36846Ssl 					if ((usba_hubdi_check_power_budget(dip,
631035f36846Ssl 					    child_ud, config_index)) !=
631135f36846Ssl 					    USB_SUCCESS) {
631235f36846Ssl 
631335f36846Ssl 						goto fail_cleanup;
631435f36846Ssl 					}
631535f36846Ssl 				}
63167c478bd9Sstevel@tonic-gate 			}
63177c478bd9Sstevel@tonic-gate 			if (rval != USB_SUCCESS) {
631835f36846Ssl 
631935f36846Ssl 				if ((usba_hubdi_check_power_budget(dip,
632035f36846Ssl 				    child_ud, 0)) != USB_SUCCESS) {
632135f36846Ssl 
632235f36846Ssl 					goto fail_cleanup;
632335f36846Ssl 				}
632435f36846Ssl 
63257c478bd9Sstevel@tonic-gate 				child_dip = hubd_ready_device(hubd, child_dip,
63267c478bd9Sstevel@tonic-gate 				    child_ud, 0);
63277c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
63287c478bd9Sstevel@tonic-gate 				hubd->h_children_dips[port] = child_dip;
63297c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
63307c478bd9Sstevel@tonic-gate 			}
63317c478bd9Sstevel@tonic-gate 		} /* end else loop all configs */
63327c478bd9Sstevel@tonic-gate 	} else {
633335f36846Ssl 
633435f36846Ssl 		if ((usba_hubdi_check_power_budget(dip, child_ud,
633535f36846Ssl 		    (uint_t)user_conf_index)) != USB_SUCCESS) {
633635f36846Ssl 
633735f36846Ssl 			goto fail_cleanup;
633835f36846Ssl 		}
633935f36846Ssl 
63407c478bd9Sstevel@tonic-gate 		child_dip = hubd_ready_device(hubd, child_dip,
634135f36846Ssl 		    child_ud, (uint_t)user_conf_index);
63427c478bd9Sstevel@tonic-gate 
63437c478bd9Sstevel@tonic-gate 		/*
63447c478bd9Sstevel@tonic-gate 		 * Assign the dip before onlining to avoid race
63457c478bd9Sstevel@tonic-gate 		 * with busctl
63467c478bd9Sstevel@tonic-gate 		 */
63477c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
63487c478bd9Sstevel@tonic-gate 		hubd->h_children_dips[port] = child_dip;
63497c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
63507c478bd9Sstevel@tonic-gate 
63517c478bd9Sstevel@tonic-gate 		(void) usba_bind_driver(child_dip);
63527c478bd9Sstevel@tonic-gate 	}
63537c478bd9Sstevel@tonic-gate 
635435f36846Ssl 	usba_hubdi_decr_power_budget(dip, child_ud);
635535f36846Ssl 
63567c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
63577c478bd9Sstevel@tonic-gate 	if (hubd->h_usba_devices[port] == NULL) {
63587c478bd9Sstevel@tonic-gate 		hubd->h_usba_devices[port] = usba_get_usba_device(child_dip);
63597c478bd9Sstevel@tonic-gate 	} else {
63607c478bd9Sstevel@tonic-gate 		ASSERT(hubd->h_usba_devices[port] ==
6361c0f24e5bSlg 		    usba_get_usba_device(child_dip));
63627c478bd9Sstevel@tonic-gate 	}
63637c478bd9Sstevel@tonic-gate 
63647c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
63657c478bd9Sstevel@tonic-gate 
63667c478bd9Sstevel@tonic-gate 
63677c478bd9Sstevel@tonic-gate fail_cleanup:
63687c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
63697c478bd9Sstevel@tonic-gate 	    "hubd_create_child: fail_cleanup");
63707c478bd9Sstevel@tonic-gate 
63717c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
63727c478bd9Sstevel@tonic-gate 	hubd->h_children_dips[port] = NULL;
63737c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
63747c478bd9Sstevel@tonic-gate 
63757c478bd9Sstevel@tonic-gate 	if (pdata) {
63767c478bd9Sstevel@tonic-gate 		freemsg(pdata);
63777c478bd9Sstevel@tonic-gate 	}
63787c478bd9Sstevel@tonic-gate 
63797c478bd9Sstevel@tonic-gate 	if (ph) {
63807c478bd9Sstevel@tonic-gate 		usb_pipe_close(child_dip, ph,
63817c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
63827c478bd9Sstevel@tonic-gate 	}
63837c478bd9Sstevel@tonic-gate 
63847c478bd9Sstevel@tonic-gate 	if (child_dip) {
63857c478bd9Sstevel@tonic-gate 		int rval = usba_destroy_child_devi(child_dip,
63867c478bd9Sstevel@tonic-gate 		    NDI_DEVI_REMOVE);
63877c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
6388d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
63897c478bd9Sstevel@tonic-gate 			    "failure to remove child node");
63907c478bd9Sstevel@tonic-gate 		}
63917c478bd9Sstevel@tonic-gate 	}
63927c478bd9Sstevel@tonic-gate 
63937c478bd9Sstevel@tonic-gate 	if (child_ud) {
63947c478bd9Sstevel@tonic-gate 		/* to make sure we free the address */
63957c478bd9Sstevel@tonic-gate 		mutex_enter(&child_ud->usb_mutex);
63967c478bd9Sstevel@tonic-gate 		child_ud->usb_addr = address;
63977c478bd9Sstevel@tonic-gate 		ASSERT(child_ud->usb_ref_count == 0);
63987c478bd9Sstevel@tonic-gate 		mutex_exit(&child_ud->usb_mutex);
63997c478bd9Sstevel@tonic-gate 
64007c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
64017c478bd9Sstevel@tonic-gate 		if (hubd->h_usba_devices[port] == NULL) {
64027c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
64037c478bd9Sstevel@tonic-gate 			usba_free_usba_device(child_ud);
64047c478bd9Sstevel@tonic-gate 		} else {
64057c478bd9Sstevel@tonic-gate 			hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
64067c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
64077c478bd9Sstevel@tonic-gate 		}
64087c478bd9Sstevel@tonic-gate 	}
64097c478bd9Sstevel@tonic-gate 
64107c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
64117c478bd9Sstevel@tonic-gate 
64127c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
64137c478bd9Sstevel@tonic-gate }
64147c478bd9Sstevel@tonic-gate 
64157c478bd9Sstevel@tonic-gate 
64167c478bd9Sstevel@tonic-gate /*
64177c478bd9Sstevel@tonic-gate  * hubd_delete_child:
64187c478bd9Sstevel@tonic-gate  *	- free usb address
64197c478bd9Sstevel@tonic-gate  *	- lookup child dips, there may be multiple on this port
64207c478bd9Sstevel@tonic-gate  *	- offline each child devi
64217c478bd9Sstevel@tonic-gate  */
64227c478bd9Sstevel@tonic-gate static int
64237c478bd9Sstevel@tonic-gate hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, boolean_t retry)
64247c478bd9Sstevel@tonic-gate {
64257c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip;
64267c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
64277c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
64287c478bd9Sstevel@tonic-gate 
64297c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
643035f36846Ssl 	usba_device = hubd->h_usba_devices[port];
64317c478bd9Sstevel@tonic-gate 
64327c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
64337c478bd9Sstevel@tonic-gate 	    "hubd_delete_child: port=%d, dip=0x%p usba_device=0x%p",
6434112116d8Sfb 	    port, (void *)child_dip, (void *)usba_device);
64357c478bd9Sstevel@tonic-gate 
64367c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
64377c478bd9Sstevel@tonic-gate 	if (child_dip) {
64387c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
64397c478bd9Sstevel@tonic-gate 		    "hubd_delete_child:\n\t"
64407c478bd9Sstevel@tonic-gate 		    "dip = 0x%p (%s) at port %d",
6441112116d8Sfb 		    (void *)child_dip, ddi_node_name(child_dip), port);
64427c478bd9Sstevel@tonic-gate 
644335f36846Ssl 		if (usba_device) {
644435f36846Ssl 			usba_hubdi_incr_power_budget(hubd->h_dip, usba_device);
644535f36846Ssl 		}
644635f36846Ssl 
64477c478bd9Sstevel@tonic-gate 		rval = usba_destroy_child_devi(child_dip, flag);
64487c478bd9Sstevel@tonic-gate 
6449ff0e937bSRaymond Chen 		if ((rval != USB_SUCCESS) && usba_is_hwa(child_dip)) {
6450ff0e937bSRaymond Chen 			/*
6451ff0e937bSRaymond Chen 			 * This is only useful for HWA device node.
6452ff0e937bSRaymond Chen 			 * Since hwahc interface must hold hwarc interface
6453ff0e937bSRaymond Chen 			 * open until hwahc is detached, the first call to
6454ff0e937bSRaymond Chen 			 * ndi_devi_unconfig_one() can only offline hwahc
6455ff0e937bSRaymond Chen 			 * driver but not hwarc driver. Need to make a second
6456ff0e937bSRaymond Chen 			 * call to ndi_devi_unconfig_one() to make the hwarc
6457ff0e937bSRaymond Chen 			 * driver detach.
6458ff0e937bSRaymond Chen 			 */
6459ff0e937bSRaymond Chen 			rval = usba_destroy_child_devi(child_dip, flag);
6460ff0e937bSRaymond Chen 		}
6461ff0e937bSRaymond Chen 
64627c478bd9Sstevel@tonic-gate 		if ((rval == USB_SUCCESS) && (flag & NDI_DEVI_REMOVE)) {
64637c478bd9Sstevel@tonic-gate 			/*
64647c478bd9Sstevel@tonic-gate 			 * if the child was still < DS_INITIALIZED
64657c478bd9Sstevel@tonic-gate 			 * then our bus_unconfig was not called and
64667c478bd9Sstevel@tonic-gate 			 * we have to zap the child here
64677c478bd9Sstevel@tonic-gate 			 */
64687c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
64697c478bd9Sstevel@tonic-gate 			if (hubd->h_children_dips[port] == child_dip) {
64707c478bd9Sstevel@tonic-gate 				usba_device_t *ud =
6471c0f24e5bSlg 				    hubd->h_usba_devices[port];
6472ffcd51f3Slg 					hubd->h_children_dips[port] = NULL;
64737c478bd9Sstevel@tonic-gate 				if (ud) {
64747c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
64757c478bd9Sstevel@tonic-gate 
64767c478bd9Sstevel@tonic-gate 					mutex_enter(&ud->usb_mutex);
64777c478bd9Sstevel@tonic-gate 					ud->usb_ref_count = 0;
64787c478bd9Sstevel@tonic-gate 					mutex_exit(&ud->usb_mutex);
64797c478bd9Sstevel@tonic-gate 
64807c478bd9Sstevel@tonic-gate 					usba_free_usba_device(ud);
64817c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
64827c478bd9Sstevel@tonic-gate 					hubd->h_usba_devices[port] = NULL;
64837c478bd9Sstevel@tonic-gate 				}
64847c478bd9Sstevel@tonic-gate 			}
64857c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
64867c478bd9Sstevel@tonic-gate 		}
64877c478bd9Sstevel@tonic-gate 	}
64887c478bd9Sstevel@tonic-gate 
64897c478bd9Sstevel@tonic-gate 	if ((rval != USB_SUCCESS) && retry) {
64907c478bd9Sstevel@tonic-gate 
64917c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(usba_device->usb_root_hub_dip);
64927c478bd9Sstevel@tonic-gate 	}
64937c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
64947c478bd9Sstevel@tonic-gate 
64957c478bd9Sstevel@tonic-gate 	return (rval);
64967c478bd9Sstevel@tonic-gate }
64977c478bd9Sstevel@tonic-gate 
64987c478bd9Sstevel@tonic-gate 
64997c478bd9Sstevel@tonic-gate /*
65007c478bd9Sstevel@tonic-gate  * hubd_free_usba_device:
65017c478bd9Sstevel@tonic-gate  *	free usb device structure unless it is associated with
65027c478bd9Sstevel@tonic-gate  *	the root hub which is handled differently
65037c478bd9Sstevel@tonic-gate  */
65047c478bd9Sstevel@tonic-gate static void
65057c478bd9Sstevel@tonic-gate hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device)
65067c478bd9Sstevel@tonic-gate {
65077c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
65087c478bd9Sstevel@tonic-gate 	    "hubd_free_usba_device: hubd=0x%p, usba_device=0x%p",
6509112116d8Sfb 	    (void *)hubd, (void *)usba_device);
65107c478bd9Sstevel@tonic-gate 
65117c478bd9Sstevel@tonic-gate 	if (usba_device && (usba_device->usb_addr != ROOT_HUB_ADDR)) {
65127c478bd9Sstevel@tonic-gate 		usb_port_t port = usba_device->usb_port;
65137c478bd9Sstevel@tonic-gate 		dev_info_t *dip = hubd->h_children_dips[port];
65147c478bd9Sstevel@tonic-gate 
65157c478bd9Sstevel@tonic-gate #ifdef DEBUG
65167c478bd9Sstevel@tonic-gate 		if (dip) {
65177c478bd9Sstevel@tonic-gate 			ASSERT(i_ddi_node_state(dip) < DS_INITIALIZED);
65187c478bd9Sstevel@tonic-gate 		}
65197c478bd9Sstevel@tonic-gate #endif
65207c478bd9Sstevel@tonic-gate 
65217c478bd9Sstevel@tonic-gate 		port = usba_device->usb_port;
65227c478bd9Sstevel@tonic-gate 		hubd->h_usba_devices[port] = NULL;
65237c478bd9Sstevel@tonic-gate 
65247c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
65257c478bd9Sstevel@tonic-gate 		usba_free_usba_device(usba_device);
65267c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
65277c478bd9Sstevel@tonic-gate 	}
65287c478bd9Sstevel@tonic-gate }
65297c478bd9Sstevel@tonic-gate 
65307c478bd9Sstevel@tonic-gate 
65317c478bd9Sstevel@tonic-gate /*
65327c478bd9Sstevel@tonic-gate  * event support
65337c478bd9Sstevel@tonic-gate  *
65347c478bd9Sstevel@tonic-gate  * busctl event support
65357c478bd9Sstevel@tonic-gate  */
65367c478bd9Sstevel@tonic-gate static int
65377c478bd9Sstevel@tonic-gate hubd_busop_get_eventcookie(dev_info_t *dip,
65387c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
65397c478bd9Sstevel@tonic-gate 	char		*eventname,
65407c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t *cookie)
65417c478bd9Sstevel@tonic-gate {
65427c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
65437c478bd9Sstevel@tonic-gate 
65447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
65457c478bd9Sstevel@tonic-gate 	    "hubd_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
65467c478bd9Sstevel@tonic-gate 	    "event=%s", (void *)dip, (void *)rdip, eventname);
65477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
65487c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d)",
65497c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
65507c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
65517c478bd9Sstevel@tonic-gate 
65527c478bd9Sstevel@tonic-gate 	/* return event cookie, iblock cookie, and level */
65537c478bd9Sstevel@tonic-gate 	return (ndi_event_retrieve_cookie(hubd->h_ndi_event_hdl,
6554c0f24e5bSlg 	    rdip, eventname, cookie, NDI_EVENT_NOPASS));
65557c478bd9Sstevel@tonic-gate }
65567c478bd9Sstevel@tonic-gate 
65577c478bd9Sstevel@tonic-gate 
65587c478bd9Sstevel@tonic-gate static int
65597c478bd9Sstevel@tonic-gate hubd_busop_add_eventcall(dev_info_t *dip,
65607c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
65617c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
65627c478bd9Sstevel@tonic-gate 	void		(*callback)(dev_info_t *dip,
65637c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t cookie, void *arg,
65647c478bd9Sstevel@tonic-gate 			void *bus_impldata),
65657c478bd9Sstevel@tonic-gate 	void *arg, ddi_callback_id_t *cb_id)
65667c478bd9Sstevel@tonic-gate {
65677c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
65687c478bd9Sstevel@tonic-gate 	usb_port_t port = hubd_child_dip2port(hubd, rdip);
65697c478bd9Sstevel@tonic-gate 
65707c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
65717c478bd9Sstevel@tonic-gate 	    "hubd_busop_add_eventcall: dip=0x%p, rdip=0x%p "
65727c478bd9Sstevel@tonic-gate 	    "cookie=0x%p, cb=0x%p, arg=0x%p",
65737c478bd9Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
65747c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
65757c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d, event=%s)",
65767c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
65777c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
65787c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(hubd->h_ndi_event_hdl, cookie));
65797c478bd9Sstevel@tonic-gate 
65807c478bd9Sstevel@tonic-gate 	/* Set flag on children registering events */
65817c478bd9Sstevel@tonic-gate 	switch (ndi_event_cookie_to_tag(hubd->h_ndi_event_hdl, cookie)) {
65827c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
65837c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
65847c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
65857c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
65867c478bd9Sstevel@tonic-gate 
65877c478bd9Sstevel@tonic-gate 		break;
65887c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
65897c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
65907c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
65917c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
65927c478bd9Sstevel@tonic-gate 
65937c478bd9Sstevel@tonic-gate 		break;
65947c478bd9Sstevel@tonic-gate 	default:
65957c478bd9Sstevel@tonic-gate 
65967c478bd9Sstevel@tonic-gate 		break;
65977c478bd9Sstevel@tonic-gate 	}
65987c478bd9Sstevel@tonic-gate 
65997c478bd9Sstevel@tonic-gate 	/* add callback to our event set */
66007c478bd9Sstevel@tonic-gate 	return (ndi_event_add_callback(hubd->h_ndi_event_hdl,
6601c0f24e5bSlg 	    rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
66027c478bd9Sstevel@tonic-gate }
66037c478bd9Sstevel@tonic-gate 
66047c478bd9Sstevel@tonic-gate 
66057c478bd9Sstevel@tonic-gate static int
66067c478bd9Sstevel@tonic-gate hubd_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
66077c478bd9Sstevel@tonic-gate {
66087c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
66097c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *id = (ndi_event_callbacks_t *)cb_id;
66107c478bd9Sstevel@tonic-gate 
66117c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
66127c478bd9Sstevel@tonic-gate 	    "hubd_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
6613112116d8Sfb 	    "cookie=0x%p", (void *)dip, (void *)id->ndi_evtcb_dip,
6614112116d8Sfb 	    (void *)id->ndi_evtcb_cookie);
66157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
66167c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d, event=%s)",
66177c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
66187c478bd9Sstevel@tonic-gate 	    ddi_driver_name(id->ndi_evtcb_dip),
66197c478bd9Sstevel@tonic-gate 	    ddi_get_instance(id->ndi_evtcb_dip),
66207c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(hubd->h_ndi_event_hdl,
66217c478bd9Sstevel@tonic-gate 	    id->ndi_evtcb_cookie));
66227c478bd9Sstevel@tonic-gate 
66237c478bd9Sstevel@tonic-gate 	/* remove event registration from our event set */
66247c478bd9Sstevel@tonic-gate 	return (ndi_event_remove_callback(hubd->h_ndi_event_hdl, cb_id));
66257c478bd9Sstevel@tonic-gate }
66267c478bd9Sstevel@tonic-gate 
66277c478bd9Sstevel@tonic-gate 
66287c478bd9Sstevel@tonic-gate /*
66297c478bd9Sstevel@tonic-gate  * event distribution
66307c478bd9Sstevel@tonic-gate  *
66317c478bd9Sstevel@tonic-gate  * hubd_do_callback:
66327c478bd9Sstevel@tonic-gate  *	Post this event to the specified child
66337c478bd9Sstevel@tonic-gate  */
66347c478bd9Sstevel@tonic-gate static void
66357c478bd9Sstevel@tonic-gate hubd_do_callback(hubd_t *hubd, dev_info_t *cdip, ddi_eventcookie_t cookie)
66367c478bd9Sstevel@tonic-gate {
66377c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
66387c478bd9Sstevel@tonic-gate 	    "hubd_do_callback");
66397c478bd9Sstevel@tonic-gate 
66407c478bd9Sstevel@tonic-gate 	(void) ndi_event_do_callback(hubd->h_ndi_event_hdl, cdip, cookie, NULL);
66417c478bd9Sstevel@tonic-gate }
66427c478bd9Sstevel@tonic-gate 
66437c478bd9Sstevel@tonic-gate 
66447c478bd9Sstevel@tonic-gate /*
66457c478bd9Sstevel@tonic-gate  * hubd_run_callbacks:
66467c478bd9Sstevel@tonic-gate  *	Send this event to all children
66477c478bd9Sstevel@tonic-gate  */
66487c478bd9Sstevel@tonic-gate static void
66497c478bd9Sstevel@tonic-gate hubd_run_callbacks(hubd_t *hubd, usba_event_t type)
66507c478bd9Sstevel@tonic-gate {
66517c478bd9Sstevel@tonic-gate 	usb_port_t	port;
66527c478bd9Sstevel@tonic-gate 
66537c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
66547c478bd9Sstevel@tonic-gate 	    "hubd_run_callbacks");
66557c478bd9Sstevel@tonic-gate 
66567c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
66577c478bd9Sstevel@tonic-gate 	for (port = 1; port <= hubd->h_hub_descr.bNbrPorts; port++) {
66587c478bd9Sstevel@tonic-gate 		/*
66597c478bd9Sstevel@tonic-gate 		 * the childen_dips list may have dips that have been
66607c478bd9Sstevel@tonic-gate 		 * already deallocated. we only get a post_detach notification
66617c478bd9Sstevel@tonic-gate 		 * but not a destroy notification
66627c478bd9Sstevel@tonic-gate 		 */
66637c478bd9Sstevel@tonic-gate 		if (hubd->h_children_dips[port]) {
66647c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
66657c478bd9Sstevel@tonic-gate 			hubd_post_event(hubd, port, type);
66667c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
66677c478bd9Sstevel@tonic-gate 		}
66687c478bd9Sstevel@tonic-gate 	}
66697c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
66707c478bd9Sstevel@tonic-gate }
66717c478bd9Sstevel@tonic-gate 
66727c478bd9Sstevel@tonic-gate 
66737c478bd9Sstevel@tonic-gate /*
66747c478bd9Sstevel@tonic-gate  * hubd_post_event
66757c478bd9Sstevel@tonic-gate  *	post event to a child on the port depending on the type
66767c478bd9Sstevel@tonic-gate  */
66777c478bd9Sstevel@tonic-gate static void
66787c478bd9Sstevel@tonic-gate hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type)
66797c478bd9Sstevel@tonic-gate {
66807c478bd9Sstevel@tonic-gate 	int	rval;
66817c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
66827c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
66837c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie, rm_cookie, suspend_cookie;
66847c478bd9Sstevel@tonic-gate 
66857c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
66867c478bd9Sstevel@tonic-gate 	    "hubd_post_event: port=%d event=%s", port,
66877c478bd9Sstevel@tonic-gate 	    ndi_event_tag_to_name(hubd->h_ndi_event_hdl, type));
66887c478bd9Sstevel@tonic-gate 
66897c478bd9Sstevel@tonic-gate 	cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, type);
66907c478bd9Sstevel@tonic-gate 	rm_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
66917c478bd9Sstevel@tonic-gate 	    USBA_EVENT_TAG_HOT_REMOVAL);
66927c478bd9Sstevel@tonic-gate 	suspend_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
66937c478bd9Sstevel@tonic-gate 	    USBA_EVENT_TAG_PRE_SUSPEND);
66947c478bd9Sstevel@tonic-gate 
66957c478bd9Sstevel@tonic-gate 	/*
66967c478bd9Sstevel@tonic-gate 	 * Hotplug daemon may be attaching a driver that may be registering
66977c478bd9Sstevel@tonic-gate 	 * event callbacks. So it already has got the device tree lock and
66987c478bd9Sstevel@tonic-gate 	 * event handle mutex. So to prevent a deadlock while posting events,
66997c478bd9Sstevel@tonic-gate 	 * we grab and release the locks in the same order.
67007c478bd9Sstevel@tonic-gate 	 */
67017c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
67027c478bd9Sstevel@tonic-gate 	dip = hubd->h_children_dips[port];
67037c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
67047c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
67057c478bd9Sstevel@tonic-gate 
67067c478bd9Sstevel@tonic-gate 	switch (type) {
67077c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
67087c478bd9Sstevel@tonic-gate 		/* Clear the registered event flag */
67097c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
67107c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_DISCONNECT;
67117c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
67127c478bd9Sstevel@tonic-gate 
67137c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
67147c478bd9Sstevel@tonic-gate 		usba_persistent_pipe_close(usba_device);
67157c478bd9Sstevel@tonic-gate 
67167c478bd9Sstevel@tonic-gate 		/*
67177c478bd9Sstevel@tonic-gate 		 * Mark the dip for deletion only after the driver has
67187c478bd9Sstevel@tonic-gate 		 * seen the disconnect event to prevent cleanup thread
67197c478bd9Sstevel@tonic-gate 		 * from stepping in between.
67207c478bd9Sstevel@tonic-gate 		 */
672116747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
67227c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_REMOVED(dip);
672316747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
67247c478bd9Sstevel@tonic-gate 
67257c478bd9Sstevel@tonic-gate 		break;
67267c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
67277c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
67287c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_PRESUSPEND;
67297c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
67307c478bd9Sstevel@tonic-gate 
67317c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
67327c478bd9Sstevel@tonic-gate 		/*
67337c478bd9Sstevel@tonic-gate 		 * persistent pipe close for this event is taken care by the
67347c478bd9Sstevel@tonic-gate 		 * caller after verfying that all children can suspend
67357c478bd9Sstevel@tonic-gate 		 */
67367c478bd9Sstevel@tonic-gate 
67377c478bd9Sstevel@tonic-gate 		break;
67387c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_INSERTION:
67397c478bd9Sstevel@tonic-gate 		/*
67407c478bd9Sstevel@tonic-gate 		 * Check if this child has missed the disconnect event before
67417c478bd9Sstevel@tonic-gate 		 * it registered for event callbacks
67427c478bd9Sstevel@tonic-gate 		 */
67437c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
67447c478bd9Sstevel@tonic-gate 		if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_DISCONNECT) {
67457c478bd9Sstevel@tonic-gate 			/* clear the flag and post disconnect event */
67467c478bd9Sstevel@tonic-gate 			hubd->h_child_events[port] &=
67477c478bd9Sstevel@tonic-gate 			    ~HUBD_CHILD_EVENT_DISCONNECT;
67487c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
67497c478bd9Sstevel@tonic-gate 			hubd_do_callback(hubd, dip, rm_cookie);
67507c478bd9Sstevel@tonic-gate 			usba_persistent_pipe_close(usba_device);
67517c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
67527c478bd9Sstevel@tonic-gate 		}
67537c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
67547c478bd9Sstevel@tonic-gate 
67557c478bd9Sstevel@tonic-gate 		/*
67567c478bd9Sstevel@tonic-gate 		 * Mark the dip as reinserted to prevent cleanup thread
67577c478bd9Sstevel@tonic-gate 		 * from stepping in.
67587c478bd9Sstevel@tonic-gate 		 */
675916747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
67607c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_REINSERTED(dip);
676116747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
67627c478bd9Sstevel@tonic-gate 
67637c478bd9Sstevel@tonic-gate 		rval = usba_persistent_pipe_open(usba_device);
67647c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
6765d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
67667c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
67677c478bd9Sstevel@tonic-gate 			    "failed to reopen all pipes on reconnect");
67687c478bd9Sstevel@tonic-gate 		}
67697c478bd9Sstevel@tonic-gate 
67707c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
67717c478bd9Sstevel@tonic-gate 
67727c478bd9Sstevel@tonic-gate 		/*
67737c478bd9Sstevel@tonic-gate 		 * We might see a connect event only if hotplug thread for
67747c478bd9Sstevel@tonic-gate 		 * disconnect event don't run in time.
67757c478bd9Sstevel@tonic-gate 		 * Set the flag again, so we don't miss posting a
67767c478bd9Sstevel@tonic-gate 		 * disconnect event.
67777c478bd9Sstevel@tonic-gate 		 */
67787c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
67797c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
67807c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
67817c478bd9Sstevel@tonic-gate 
67827c478bd9Sstevel@tonic-gate 		break;
67837c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_POST_RESUME:
67847c478bd9Sstevel@tonic-gate 		/*
67857c478bd9Sstevel@tonic-gate 		 * Check if this child has missed the pre-suspend event before
67867c478bd9Sstevel@tonic-gate 		 * it registered for event callbacks
67877c478bd9Sstevel@tonic-gate 		 */
67887c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
67897c478bd9Sstevel@tonic-gate 		if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_PRESUSPEND) {
67907c478bd9Sstevel@tonic-gate 			/* clear the flag and post pre_suspend event */
67917c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &=
67927c478bd9Sstevel@tonic-gate 			    ~HUBD_CHILD_EVENT_PRESUSPEND;
67937c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
67947c478bd9Sstevel@tonic-gate 			hubd_do_callback(hubd, dip, suspend_cookie);
67957c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
67967c478bd9Sstevel@tonic-gate 		}
67977c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
67987c478bd9Sstevel@tonic-gate 
67997c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
68007c478bd9Sstevel@tonic-gate 		usba_device->usb_no_cpr = 0;
68017c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
68027c478bd9Sstevel@tonic-gate 
68037c478bd9Sstevel@tonic-gate 		/*
68047c478bd9Sstevel@tonic-gate 		 * Since the pipe has already been opened by hub
68057c478bd9Sstevel@tonic-gate 		 * at DDI_RESUME time, there is no need for a
68067c478bd9Sstevel@tonic-gate 		 * persistent pipe open
68077c478bd9Sstevel@tonic-gate 		 */
68087c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
68097c478bd9Sstevel@tonic-gate 
68107c478bd9Sstevel@tonic-gate 		/*
68117c478bd9Sstevel@tonic-gate 		 * Set the flag again, so we don't miss posting a
68127c478bd9Sstevel@tonic-gate 		 * pre-suspend event. This enforces a tighter
68137c478bd9Sstevel@tonic-gate 		 * dev_state model.
68147c478bd9Sstevel@tonic-gate 		 */
68157c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
68167c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
68177c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
68187c478bd9Sstevel@tonic-gate 		break;
68197c478bd9Sstevel@tonic-gate 	}
68207c478bd9Sstevel@tonic-gate }
68217c478bd9Sstevel@tonic-gate 
68227c478bd9Sstevel@tonic-gate 
68237c478bd9Sstevel@tonic-gate /*
68247c478bd9Sstevel@tonic-gate  * handling of events coming from above
68257c478bd9Sstevel@tonic-gate  */
68267c478bd9Sstevel@tonic-gate static int
68277c478bd9Sstevel@tonic-gate hubd_disconnect_event_cb(dev_info_t *dip)
68287c478bd9Sstevel@tonic-gate {
68297c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)hubd_get_soft_state(dip);
68307c478bd9Sstevel@tonic-gate 	usb_port_t	port, nports;
68317c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_dev;
68327c478bd9Sstevel@tonic-gate 	usba_event_t	tag = USBA_EVENT_TAG_HOT_REMOVAL;
68337c478bd9Sstevel@tonic-gate 	int		circ;
68347c478bd9Sstevel@tonic-gate 
68357c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
68367c478bd9Sstevel@tonic-gate 	    "hubd_disconnect_event_cb: tag=%d", tag);
68377c478bd9Sstevel@tonic-gate 
68387c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
68397c478bd9Sstevel@tonic-gate 
68407c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
68417c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
68427c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
68437c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
68447c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
68457c478bd9Sstevel@tonic-gate 		/* stop polling on the interrupt pipe */
68467c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
68477c478bd9Sstevel@tonic-gate 
68487c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
68497c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
68507c478bd9Sstevel@tonic-gate 		/* we remain in this state */
68517c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
68527c478bd9Sstevel@tonic-gate 		hubd_run_callbacks(hubd, tag);
68537c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
68547c478bd9Sstevel@tonic-gate 
68557c478bd9Sstevel@tonic-gate 		/* close all the open pipes of our children */
68567c478bd9Sstevel@tonic-gate 		nports = hubd->h_hub_descr.bNbrPorts;
68577c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
68587c478bd9Sstevel@tonic-gate 			usba_dev = hubd->h_usba_devices[port];
68597c478bd9Sstevel@tonic-gate 			if (usba_dev != NULL) {
68607c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
68617c478bd9Sstevel@tonic-gate 				usba_persistent_pipe_close(usba_dev);
68627c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
68637c478bd9Sstevel@tonic-gate 			}
68647c478bd9Sstevel@tonic-gate 		}
68657c478bd9Sstevel@tonic-gate 
68667c478bd9Sstevel@tonic-gate 		break;
68677c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
68687c478bd9Sstevel@tonic-gate 		/* avoid passing multiple disconnects to children */
68697c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
68707c478bd9Sstevel@tonic-gate 		    "hubd_disconnect_event_cb: Already disconnected");
68717c478bd9Sstevel@tonic-gate 
68727c478bd9Sstevel@tonic-gate 		break;
68737c478bd9Sstevel@tonic-gate 	default:
68747c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
68757c478bd9Sstevel@tonic-gate 		    "hubd_disconnect_event_cb: Illegal devstate=%d",
68767c478bd9Sstevel@tonic-gate 		    hubd->h_dev_state);
68777c478bd9Sstevel@tonic-gate 
68787c478bd9Sstevel@tonic-gate 		break;
68797c478bd9Sstevel@tonic-gate 	}
68807c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
68817c478bd9Sstevel@tonic-gate 
68827c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
68837c478bd9Sstevel@tonic-gate 
68847c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
68857c478bd9Sstevel@tonic-gate }
68867c478bd9Sstevel@tonic-gate 
68877c478bd9Sstevel@tonic-gate 
68887c478bd9Sstevel@tonic-gate static int
68897c478bd9Sstevel@tonic-gate hubd_reconnect_event_cb(dev_info_t *dip)
68907c478bd9Sstevel@tonic-gate {
68917c478bd9Sstevel@tonic-gate 	int	rval, circ;
68927c478bd9Sstevel@tonic-gate 
68937c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
68947c478bd9Sstevel@tonic-gate 	rval = hubd_restore_state_cb(dip);
68957c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
68967c478bd9Sstevel@tonic-gate 
68977c478bd9Sstevel@tonic-gate 	return (rval);
68987c478bd9Sstevel@tonic-gate }
68997c478bd9Sstevel@tonic-gate 
69007c478bd9Sstevel@tonic-gate 
69017c478bd9Sstevel@tonic-gate /*
69027c478bd9Sstevel@tonic-gate  * hubd_pre_suspend_event_cb
69037c478bd9Sstevel@tonic-gate  *	propogate event for binary compatibility of old drivers
69047c478bd9Sstevel@tonic-gate  */
69057c478bd9Sstevel@tonic-gate static int
69067c478bd9Sstevel@tonic-gate hubd_pre_suspend_event_cb(dev_info_t *dip)
69077c478bd9Sstevel@tonic-gate {
69087c478bd9Sstevel@tonic-gate 	int	circ;
69097c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
69107c478bd9Sstevel@tonic-gate 
69117c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
69127c478bd9Sstevel@tonic-gate 	    "hubd_pre_suspend_event_cb");
69137c478bd9Sstevel@tonic-gate 
69147c478bd9Sstevel@tonic-gate 	/* disable hotplug thread */
69157c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
69167c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread++;
69177c478bd9Sstevel@tonic-gate 	hubd_stop_polling(hubd);
69187c478bd9Sstevel@tonic-gate 
69197c478bd9Sstevel@tonic-gate 	/* keep PM out till we see a cpr resume */
69207c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
69217c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
69227c478bd9Sstevel@tonic-gate 
69237c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
69247c478bd9Sstevel@tonic-gate 	hubd_run_callbacks(hubd, USBA_EVENT_TAG_PRE_SUSPEND);
69257c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
69267c478bd9Sstevel@tonic-gate 
69277c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
69287c478bd9Sstevel@tonic-gate }
69297c478bd9Sstevel@tonic-gate 
69307c478bd9Sstevel@tonic-gate 
69317c478bd9Sstevel@tonic-gate /*
69327c478bd9Sstevel@tonic-gate  * hubd_post_resume_event_cb
69337c478bd9Sstevel@tonic-gate  *	propogate event for binary compatibility of old drivers
69347c478bd9Sstevel@tonic-gate  */
69357c478bd9Sstevel@tonic-gate static int
69367c478bd9Sstevel@tonic-gate hubd_post_resume_event_cb(dev_info_t *dip)
69377c478bd9Sstevel@tonic-gate {
69387c478bd9Sstevel@tonic-gate 	int	circ;
69397c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
69407c478bd9Sstevel@tonic-gate 
69417c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
69427c478bd9Sstevel@tonic-gate 	    "hubd_post_resume_event_cb");
69437c478bd9Sstevel@tonic-gate 
69447c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
69457c478bd9Sstevel@tonic-gate 	hubd_run_callbacks(hubd, USBA_EVENT_TAG_POST_RESUME);
69467c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
69477c478bd9Sstevel@tonic-gate 
69487c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
69497c478bd9Sstevel@tonic-gate 
69507c478bd9Sstevel@tonic-gate 	/* enable PM */
69517c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
69527c478bd9Sstevel@tonic-gate 
69537c478bd9Sstevel@tonic-gate 	/* allow hotplug thread */
69547c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
69557c478bd9Sstevel@tonic-gate 
69567c478bd9Sstevel@tonic-gate 	/* start polling */
69577c478bd9Sstevel@tonic-gate 	hubd_start_polling(hubd, 0);
69587c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
69597c478bd9Sstevel@tonic-gate 
69607c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
69617c478bd9Sstevel@tonic-gate }
69627c478bd9Sstevel@tonic-gate 
69637c478bd9Sstevel@tonic-gate 
69647c478bd9Sstevel@tonic-gate /*
69657c478bd9Sstevel@tonic-gate  * hubd_cpr_suspend
69667c478bd9Sstevel@tonic-gate  *	save the current state of the driver/device
69677c478bd9Sstevel@tonic-gate  */
69687c478bd9Sstevel@tonic-gate static int
69697c478bd9Sstevel@tonic-gate hubd_cpr_suspend(hubd_t *hubd)
69707c478bd9Sstevel@tonic-gate {
69717c478bd9Sstevel@tonic-gate 	usb_port_t	port, nports;
69727c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_dev;
69737c478bd9Sstevel@tonic-gate 	uchar_t		no_cpr = 0;
69747c478bd9Sstevel@tonic-gate 	int		rval = USB_FAILURE;
69757c478bd9Sstevel@tonic-gate 
69767c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
69777c478bd9Sstevel@tonic-gate 	    "hubd_cpr_suspend: Begin");
69787c478bd9Sstevel@tonic-gate 
69797c478bd9Sstevel@tonic-gate 	/* Make sure device is powered up to save state. */
69807c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
69817c478bd9Sstevel@tonic-gate 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
69827c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
69837c478bd9Sstevel@tonic-gate 
69847c478bd9Sstevel@tonic-gate 	/* bring the device to full power */
69857c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
69867c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
69877c478bd9Sstevel@tonic-gate 
69887c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
69897c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
69907c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
69917c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
69927c478bd9Sstevel@tonic-gate 		/* find out if all our children have been quiesced */
69937c478bd9Sstevel@tonic-gate 		nports = hubd->h_hub_descr.bNbrPorts;
69947c478bd9Sstevel@tonic-gate 		for (port = 1; (no_cpr == 0) && (port <= nports); port++) {
69957c478bd9Sstevel@tonic-gate 			usba_dev = hubd->h_usba_devices[port];
69967c478bd9Sstevel@tonic-gate 			if (usba_dev != NULL) {
69977c478bd9Sstevel@tonic-gate 				mutex_enter(&usba_dev->usb_mutex);
69987c478bd9Sstevel@tonic-gate 				no_cpr += usba_dev->usb_no_cpr;
69997c478bd9Sstevel@tonic-gate 				mutex_exit(&usba_dev->usb_mutex);
70007c478bd9Sstevel@tonic-gate 			}
70017c478bd9Sstevel@tonic-gate 		}
70027c478bd9Sstevel@tonic-gate 		if (no_cpr > 0) {
70037c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
70047c478bd9Sstevel@tonic-gate 			    "Children busy - can't checkpoint");
70057c478bd9Sstevel@tonic-gate 			/* remain in same state to fail checkpoint */
70067c478bd9Sstevel@tonic-gate 
70077c478bd9Sstevel@tonic-gate 			break;
70087c478bd9Sstevel@tonic-gate 		} else {
70097c478bd9Sstevel@tonic-gate 			/*
70107c478bd9Sstevel@tonic-gate 			 * do not suspend if our hotplug thread
70117c478bd9Sstevel@tonic-gate 			 * or the deathrow thread is active
70127c478bd9Sstevel@tonic-gate 			 */
70137c478bd9Sstevel@tonic-gate 			if ((hubd->h_hotplug_thread > 1) ||
70147c478bd9Sstevel@tonic-gate 			    (hubd->h_cleanup_active == B_TRUE)) {
70157c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
70167c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
70177c478bd9Sstevel@tonic-gate 				    "hotplug thread active  - can't cpr");
70187c478bd9Sstevel@tonic-gate 				/* remain in same state to fail checkpoint */
70197c478bd9Sstevel@tonic-gate 
70207c478bd9Sstevel@tonic-gate 				break;
70217c478bd9Sstevel@tonic-gate 			}
70227c478bd9Sstevel@tonic-gate 
70237c478bd9Sstevel@tonic-gate 			/* quiesce ourselves now */
70247c478bd9Sstevel@tonic-gate 			hubd->h_dev_state = USB_DEV_SUSPENDED;
70257c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
70267c478bd9Sstevel@tonic-gate 
70277c478bd9Sstevel@tonic-gate 			/* close all the open pipes of our children */
70287c478bd9Sstevel@tonic-gate 			for (port = 1; port <= nports; port++) {
70297c478bd9Sstevel@tonic-gate 				usba_dev = hubd->h_usba_devices[port];
70307c478bd9Sstevel@tonic-gate 				if (usba_dev != NULL) {
70317c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
70327c478bd9Sstevel@tonic-gate 					usba_persistent_pipe_close(usba_dev);
70337c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
70347c478bd9Sstevel@tonic-gate 				}
70357c478bd9Sstevel@tonic-gate 			}
70367c478bd9Sstevel@tonic-gate 			/*
70377c478bd9Sstevel@tonic-gate 			 * turn off power to all the ports so that we
70387c478bd9Sstevel@tonic-gate 			 * don't see any spurious activity
70397c478bd9Sstevel@tonic-gate 			 */
70407c478bd9Sstevel@tonic-gate 			(void) hubd_disable_all_port_power(hubd);
70417c478bd9Sstevel@tonic-gate 
70427c478bd9Sstevel@tonic-gate 			/*
70437c478bd9Sstevel@tonic-gate 			 * if we are the root hub, we close our pipes
70447c478bd9Sstevel@tonic-gate 			 * ourselves.
70457c478bd9Sstevel@tonic-gate 			 */
70467c478bd9Sstevel@tonic-gate 			if (usba_is_root_hub(hubd->h_dip)) {
70477c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
70487c478bd9Sstevel@tonic-gate 				usba_persistent_pipe_close(
70497c478bd9Sstevel@tonic-gate 				    usba_get_usba_device(hubd->h_dip));
70507c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
70517c478bd9Sstevel@tonic-gate 			}
70527c478bd9Sstevel@tonic-gate 			rval = USB_SUCCESS;
70537c478bd9Sstevel@tonic-gate 
70547c478bd9Sstevel@tonic-gate 			break;
70557c478bd9Sstevel@tonic-gate 		}
70567c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
70577c478bd9Sstevel@tonic-gate 	default:
70587c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
70597c478bd9Sstevel@tonic-gate 		    "hubd_cpr_suspend: Illegal dev state=%d",
70607c478bd9Sstevel@tonic-gate 		    hubd->h_dev_state);
70617c478bd9Sstevel@tonic-gate 
70627c478bd9Sstevel@tonic-gate 		break;
70637c478bd9Sstevel@tonic-gate 	}
70647c478bd9Sstevel@tonic-gate 
70657c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, hubd->h_dip, 0);
70667c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
70677c478bd9Sstevel@tonic-gate 
70687c478bd9Sstevel@tonic-gate 	return (rval);
70697c478bd9Sstevel@tonic-gate }
70707c478bd9Sstevel@tonic-gate 
70717c478bd9Sstevel@tonic-gate static void
70727c478bd9Sstevel@tonic-gate hubd_cpr_resume(dev_info_t *dip)
70737c478bd9Sstevel@tonic-gate {
70747c478bd9Sstevel@tonic-gate 	int	rval, circ;
70757c478bd9Sstevel@tonic-gate 
70767c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
70777c478bd9Sstevel@tonic-gate 	/*
70787c478bd9Sstevel@tonic-gate 	 * if we are the root hub, we open our pipes
70797c478bd9Sstevel@tonic-gate 	 * ourselves.
70807c478bd9Sstevel@tonic-gate 	 */
70817c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
70827c478bd9Sstevel@tonic-gate 		rval = usba_persistent_pipe_open(
70837c478bd9Sstevel@tonic-gate 		    usba_get_usba_device(dip));
70847c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
70857c478bd9Sstevel@tonic-gate 	}
70867c478bd9Sstevel@tonic-gate 	(void) hubd_restore_state_cb(dip);
70877c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
70887c478bd9Sstevel@tonic-gate }
70897c478bd9Sstevel@tonic-gate 
70907c478bd9Sstevel@tonic-gate 
70917c478bd9Sstevel@tonic-gate /*
70927c478bd9Sstevel@tonic-gate  * hubd_restore_state_cb
70937c478bd9Sstevel@tonic-gate  *	Event callback to restore device state
70947c478bd9Sstevel@tonic-gate  */
70957c478bd9Sstevel@tonic-gate static int
70967c478bd9Sstevel@tonic-gate hubd_restore_state_cb(dev_info_t *dip)
70977c478bd9Sstevel@tonic-gate {
70987c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
70997c478bd9Sstevel@tonic-gate 
71007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
71017c478bd9Sstevel@tonic-gate 	    "hubd_restore_state_cb: Begin");
71027c478bd9Sstevel@tonic-gate 
71037c478bd9Sstevel@tonic-gate 	/* restore the state of this device */
71047c478bd9Sstevel@tonic-gate 	hubd_restore_device_state(dip, hubd);
71057c478bd9Sstevel@tonic-gate 
71067c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
71077c478bd9Sstevel@tonic-gate }
71087c478bd9Sstevel@tonic-gate 
71097c478bd9Sstevel@tonic-gate 
71107c478bd9Sstevel@tonic-gate /*
71117c478bd9Sstevel@tonic-gate  * registering for events
71127c478bd9Sstevel@tonic-gate  */
71137c478bd9Sstevel@tonic-gate static int
71147c478bd9Sstevel@tonic-gate hubd_register_events(hubd_t *hubd)
71157c478bd9Sstevel@tonic-gate {
71167c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
71177c478bd9Sstevel@tonic-gate 
71187c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(hubd->h_dip)) {
71197c478bd9Sstevel@tonic-gate 		hubd_register_cpr_callback(hubd);
71207c478bd9Sstevel@tonic-gate 	} else {
71217c478bd9Sstevel@tonic-gate 		rval = usb_register_event_cbs(hubd->h_dip, &hubd_events, 0);
71227c478bd9Sstevel@tonic-gate 	}
71237c478bd9Sstevel@tonic-gate 
71247c478bd9Sstevel@tonic-gate 	return (rval);
71257c478bd9Sstevel@tonic-gate }
71267c478bd9Sstevel@tonic-gate 
71277c478bd9Sstevel@tonic-gate 
71287c478bd9Sstevel@tonic-gate /*
71297c478bd9Sstevel@tonic-gate  * hubd cpr callback related functions
71307c478bd9Sstevel@tonic-gate  *
71317c478bd9Sstevel@tonic-gate  * hubd_cpr_post_user_callb:
71327c478bd9Sstevel@tonic-gate  *	This function is called during checkpoint & resume -
71337c478bd9Sstevel@tonic-gate  *		1. after user threads are stopped during checkpoint
71347c478bd9Sstevel@tonic-gate  *		2. after kernel threads are resumed during resume
71357c478bd9Sstevel@tonic-gate  */
71367c478bd9Sstevel@tonic-gate /* ARGSUSED */
71377c478bd9Sstevel@tonic-gate static boolean_t
71387c478bd9Sstevel@tonic-gate hubd_cpr_post_user_callb(void *arg, int code)
71397c478bd9Sstevel@tonic-gate {
71407c478bd9Sstevel@tonic-gate 	hubd_cpr_t	*cpr_cb = (hubd_cpr_t *)arg;
71417c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = cpr_cb->statep;
71427c478bd9Sstevel@tonic-gate 	int		retry = 0;
71437c478bd9Sstevel@tonic-gate 
71447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
71457c478bd9Sstevel@tonic-gate 	    "hubd_cpr_post_user_callb");
71467c478bd9Sstevel@tonic-gate 
71477c478bd9Sstevel@tonic-gate 	switch (code) {
71487c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_CHKPT:
71497c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
71507c478bd9Sstevel@tonic-gate 		    "hubd_cpr_post_user_callb: CB_CODE_CPR_CHKPT");
71517c478bd9Sstevel@tonic-gate 
71527c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
71537c478bd9Sstevel@tonic-gate 
71547c478bd9Sstevel@tonic-gate 		/* turn off deathrow thread */
71557c478bd9Sstevel@tonic-gate 		hubd->h_cleanup_enabled = B_FALSE;
71567c478bd9Sstevel@tonic-gate 
71577c478bd9Sstevel@tonic-gate 		/* give up if deathrow thread doesn't exit */
71587c478bd9Sstevel@tonic-gate 		while ((hubd->h_cleanup_active == B_TRUE) && (retry++ < 3)) {
71597c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
71607c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(hubd_dip_cleanup_delay));
71617c478bd9Sstevel@tonic-gate 
71627c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_EVENTS, hubd->h_log_handle,
71637c478bd9Sstevel@tonic-gate 			    "hubd_cpr_post_user_callb, waiting for "
71647c478bd9Sstevel@tonic-gate 			    "deathrow thread to exit");
71657c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
71667c478bd9Sstevel@tonic-gate 		}
71677c478bd9Sstevel@tonic-gate 
71687c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
71697c478bd9Sstevel@tonic-gate 
71707c478bd9Sstevel@tonic-gate 		/* save the state of the device */
71717c478bd9Sstevel@tonic-gate 		(void) hubd_pre_suspend_event_cb(hubd->h_dip);
71727c478bd9Sstevel@tonic-gate 
71737c478bd9Sstevel@tonic-gate 		return (B_TRUE);
71747c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_RESUME:
71757c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
71767c478bd9Sstevel@tonic-gate 		    "hubd_cpr_post_user_callb: CB_CODE_CPR_RESUME");
71777c478bd9Sstevel@tonic-gate 
71787c478bd9Sstevel@tonic-gate 		/* restore the state of the device */
71797c478bd9Sstevel@tonic-gate 		(void) hubd_post_resume_event_cb(hubd->h_dip);
71807c478bd9Sstevel@tonic-gate 
71817c478bd9Sstevel@tonic-gate 		/* turn on deathrow thread */
71827c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
71837c478bd9Sstevel@tonic-gate 		hubd->h_cleanup_enabled = B_TRUE;
71847c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
71857c478bd9Sstevel@tonic-gate 
71867c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
71877c478bd9Sstevel@tonic-gate 
71887c478bd9Sstevel@tonic-gate 		return (B_TRUE);
71897c478bd9Sstevel@tonic-gate 	default:
71907c478bd9Sstevel@tonic-gate 
71917c478bd9Sstevel@tonic-gate 		return (B_FALSE);
71927c478bd9Sstevel@tonic-gate 	}
71937c478bd9Sstevel@tonic-gate 
71947c478bd9Sstevel@tonic-gate }
71957c478bd9Sstevel@tonic-gate 
71967c478bd9Sstevel@tonic-gate 
71977c478bd9Sstevel@tonic-gate /* register callback with cpr framework */
71987c478bd9Sstevel@tonic-gate void
71997c478bd9Sstevel@tonic-gate hubd_register_cpr_callback(hubd_t *hubd)
72007c478bd9Sstevel@tonic-gate {
72017c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
72027c478bd9Sstevel@tonic-gate 	    "hubd_register_cpr_callback");
72037c478bd9Sstevel@tonic-gate 
72047c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
72057c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb =
72067c478bd9Sstevel@tonic-gate 	    (hubd_cpr_t *)kmem_zalloc(sizeof (hubd_cpr_t), KM_SLEEP);
72077c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
72087c478bd9Sstevel@tonic-gate 	mutex_init(&hubd->h_cpr_cb->lockp, NULL, MUTEX_DRIVER,
72097c478bd9Sstevel@tonic-gate 	    hubd->h_dev_data->dev_iblock_cookie);
72107c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->statep = hubd;
72117c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->cpr.cc_lockp = &hubd->h_cpr_cb->lockp;
72127c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->cpr.cc_id = callb_add(hubd_cpr_post_user_callb,
72137c478bd9Sstevel@tonic-gate 	    (void *)hubd->h_cpr_cb, CB_CL_CPR_POST_USER, "hubd");
72147c478bd9Sstevel@tonic-gate }
72157c478bd9Sstevel@tonic-gate 
72167c478bd9Sstevel@tonic-gate 
72177c478bd9Sstevel@tonic-gate /* unregister callback with cpr framework */
72187c478bd9Sstevel@tonic-gate void
72197c478bd9Sstevel@tonic-gate hubd_unregister_cpr_callback(hubd_t *hubd)
72207c478bd9Sstevel@tonic-gate {
72217c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
72227c478bd9Sstevel@tonic-gate 	    "hubd_unregister_cpr_callback");
72237c478bd9Sstevel@tonic-gate 
72247c478bd9Sstevel@tonic-gate 	if (hubd->h_cpr_cb) {
72257c478bd9Sstevel@tonic-gate 		(void) callb_delete(hubd->h_cpr_cb->cpr.cc_id);
72267c478bd9Sstevel@tonic-gate 		mutex_destroy(&hubd->h_cpr_cb->lockp);
72277c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
72287c478bd9Sstevel@tonic-gate 		kmem_free(hubd->h_cpr_cb, sizeof (hubd_cpr_t));
72297c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
72307c478bd9Sstevel@tonic-gate 	}
72317c478bd9Sstevel@tonic-gate }
72327c478bd9Sstevel@tonic-gate 
72337c478bd9Sstevel@tonic-gate 
72347c478bd9Sstevel@tonic-gate /*
72357c478bd9Sstevel@tonic-gate  * Power management
72367c478bd9Sstevel@tonic-gate  *
72377c478bd9Sstevel@tonic-gate  * create the pm components required for power management
72387c478bd9Sstevel@tonic-gate  */
72397c478bd9Sstevel@tonic-gate static void
72407c478bd9Sstevel@tonic-gate hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd)
72417c478bd9Sstevel@tonic-gate {
72427c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
72437c478bd9Sstevel@tonic-gate 
72447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
72457c478bd9Sstevel@tonic-gate 	    "hubd_create_pm_components: Begin");
72467c478bd9Sstevel@tonic-gate 
72477c478bd9Sstevel@tonic-gate 	/* Allocate the state structure */
72487c478bd9Sstevel@tonic-gate 	hubpm = kmem_zalloc(sizeof (hub_power_t), KM_SLEEP);
72497c478bd9Sstevel@tonic-gate 
72507c478bd9Sstevel@tonic-gate 	hubd->h_hubpm = hubpm;
72517c478bd9Sstevel@tonic-gate 	hubpm->hubp_hubd = hubd;
72527c478bd9Sstevel@tonic-gate 	hubpm->hubp_pm_capabilities = 0;
72537c478bd9Sstevel@tonic-gate 	hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
72547c478bd9Sstevel@tonic-gate 	hubpm->hubp_time_at_full_power = ddi_get_time();
72557c478bd9Sstevel@tonic-gate 	hubpm->hubp_min_pm_threshold = hubdi_min_pm_threshold;
72567c478bd9Sstevel@tonic-gate 
72577c478bd9Sstevel@tonic-gate 	/* alloc memory to save power states of children */
72587c478bd9Sstevel@tonic-gate 	hubpm->hubp_child_pwrstate = (uint8_t *)
7259c0f24e5bSlg 	    kmem_zalloc(MAX_PORTS + 1, KM_SLEEP);
72607c478bd9Sstevel@tonic-gate 
72617c478bd9Sstevel@tonic-gate 	/*
72627c478bd9Sstevel@tonic-gate 	 * if the enable remote wakeup fails
72637c478bd9Sstevel@tonic-gate 	 * we still want to enable
72647c478bd9Sstevel@tonic-gate 	 * parent notification so we can PM the children
72657c478bd9Sstevel@tonic-gate 	 */
72667c478bd9Sstevel@tonic-gate 	usb_enable_parent_notification(dip);
72677c478bd9Sstevel@tonic-gate 
72687c478bd9Sstevel@tonic-gate 	if (usb_handle_remote_wakeup(dip,
72697c478bd9Sstevel@tonic-gate 	    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
72707c478bd9Sstevel@tonic-gate 		uint_t		pwr_states;
72717c478bd9Sstevel@tonic-gate 
72727c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
72737c478bd9Sstevel@tonic-gate 		    "hubd_create_pm_components: "
72747c478bd9Sstevel@tonic-gate 		    "Remote Wakeup Enabled");
72757c478bd9Sstevel@tonic-gate 
72767c478bd9Sstevel@tonic-gate 		if (usb_create_pm_components(dip, &pwr_states) ==
72777c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
72787c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
72797c478bd9Sstevel@tonic-gate 			hubpm->hubp_wakeup_enabled = 1;
72807c478bd9Sstevel@tonic-gate 			hubpm->hubp_pwr_states = (uint8_t)pwr_states;
72817c478bd9Sstevel@tonic-gate 
72827c478bd9Sstevel@tonic-gate 			/* we are busy now till end of the attach */
72837c478bd9Sstevel@tonic-gate 			hubd_pm_busy_component(hubd, dip, 0);
72847c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
72857c478bd9Sstevel@tonic-gate 
72867c478bd9Sstevel@tonic-gate 			/* bring the device to full power */
72877c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0,
72887c478bd9Sstevel@tonic-gate 			    USB_DEV_OS_FULL_PWR);
72897c478bd9Sstevel@tonic-gate 		}
72907c478bd9Sstevel@tonic-gate 	}
72917c478bd9Sstevel@tonic-gate 
72927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
72937c478bd9Sstevel@tonic-gate 	    "hubd_create_pm_components: END");
72947c478bd9Sstevel@tonic-gate }
72957c478bd9Sstevel@tonic-gate 
72967c478bd9Sstevel@tonic-gate 
72977c478bd9Sstevel@tonic-gate /*
72987c478bd9Sstevel@tonic-gate  * Attachment point management
72997c478bd9Sstevel@tonic-gate  */
73007c478bd9Sstevel@tonic-gate /* ARGSUSED */
73017c478bd9Sstevel@tonic-gate int
73027c478bd9Sstevel@tonic-gate usba_hubdi_open(dev_info_t *dip, dev_t *devp, int flags, int otyp,
73037c478bd9Sstevel@tonic-gate 	cred_t *credp)
73047c478bd9Sstevel@tonic-gate {
73057c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
73067c478bd9Sstevel@tonic-gate 
73077c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
73087c478bd9Sstevel@tonic-gate 		return (EINVAL);
73097c478bd9Sstevel@tonic-gate 
73107c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
73117c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
73127c478bd9Sstevel@tonic-gate 		return (ENXIO);
73137c478bd9Sstevel@tonic-gate 	}
73147c478bd9Sstevel@tonic-gate 
73157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
73167c478bd9Sstevel@tonic-gate 	    "hubd_open:");
73177c478bd9Sstevel@tonic-gate 
73187c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
73197c478bd9Sstevel@tonic-gate 	if ((flags & FEXCL) && (hubd->h_softstate & HUBD_SS_ISOPEN)) {
73207c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
73217c478bd9Sstevel@tonic-gate 
73227c478bd9Sstevel@tonic-gate 		return (EBUSY);
73237c478bd9Sstevel@tonic-gate 	}
73247c478bd9Sstevel@tonic-gate 
73257c478bd9Sstevel@tonic-gate 	hubd->h_softstate |= HUBD_SS_ISOPEN;
73267c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
73277c478bd9Sstevel@tonic-gate 
73287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "opened");
73297c478bd9Sstevel@tonic-gate 
73307c478bd9Sstevel@tonic-gate 	return (0);
73317c478bd9Sstevel@tonic-gate }
73327c478bd9Sstevel@tonic-gate 
73337c478bd9Sstevel@tonic-gate 
73347c478bd9Sstevel@tonic-gate /* ARGSUSED */
73357c478bd9Sstevel@tonic-gate int
73367c478bd9Sstevel@tonic-gate usba_hubdi_close(dev_info_t *dip, dev_t dev, int flag, int otyp,
73377c478bd9Sstevel@tonic-gate 	cred_t *credp)
73387c478bd9Sstevel@tonic-gate {
73397c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
73407c478bd9Sstevel@tonic-gate 
73417c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
73427c478bd9Sstevel@tonic-gate 		return (EINVAL);
73437c478bd9Sstevel@tonic-gate 	}
73447c478bd9Sstevel@tonic-gate 
73457c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
73467c478bd9Sstevel@tonic-gate 
73477c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
73487c478bd9Sstevel@tonic-gate 		return (ENXIO);
73497c478bd9Sstevel@tonic-gate 	}
73507c478bd9Sstevel@tonic-gate 
73517c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "hubd_close:");
73527c478bd9Sstevel@tonic-gate 
73537c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
73547c478bd9Sstevel@tonic-gate 	hubd->h_softstate &= ~HUBD_SS_ISOPEN;
73557c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
73567c478bd9Sstevel@tonic-gate 
73577c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "closed");
73587c478bd9Sstevel@tonic-gate 
73597c478bd9Sstevel@tonic-gate 	return (0);
73607c478bd9Sstevel@tonic-gate }
73617c478bd9Sstevel@tonic-gate 
73627c478bd9Sstevel@tonic-gate 
73637c478bd9Sstevel@tonic-gate /*
73647c478bd9Sstevel@tonic-gate  * hubd_ioctl: cfgadm controls
73657c478bd9Sstevel@tonic-gate  */
73667c478bd9Sstevel@tonic-gate /* ARGSUSED */
73677c478bd9Sstevel@tonic-gate int
73687c478bd9Sstevel@tonic-gate usba_hubdi_ioctl(dev_info_t *self, dev_t dev, int cmd, intptr_t arg,
73697c478bd9Sstevel@tonic-gate 	int mode, cred_t *credp, int *rvalp)
73707c478bd9Sstevel@tonic-gate {
73717c478bd9Sstevel@tonic-gate 	int			rv = 0;
73727c478bd9Sstevel@tonic-gate 	char			*msg;	/* for messages */
73737c478bd9Sstevel@tonic-gate 	hubd_t			*hubd;
73747c478bd9Sstevel@tonic-gate 	usb_port_t		port = 0;
73757c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
73767c478bd9Sstevel@tonic-gate 	dev_info_t		*rh_dip;
73777c478bd9Sstevel@tonic-gate 	devctl_ap_state_t	ap_state;
73787c478bd9Sstevel@tonic-gate 	struct devctl_iocdata	*dcp = NULL;
73797c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	prev_pipe_state = 0;
73807c478bd9Sstevel@tonic-gate 	int			circ, rh_circ, prh_circ;
73817c478bd9Sstevel@tonic-gate 
73827c478bd9Sstevel@tonic-gate 	if ((hubd = hubd_get_soft_state(self)) == NULL) {
73837c478bd9Sstevel@tonic-gate 
73847c478bd9Sstevel@tonic-gate 		return (ENXIO);
73857c478bd9Sstevel@tonic-gate 	}
73867c478bd9Sstevel@tonic-gate 
73877c478bd9Sstevel@tonic-gate 	rh_dip = hubd->h_usba_device->usb_root_hub_dip;
73887c478bd9Sstevel@tonic-gate 
73897c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
73907c478bd9Sstevel@tonic-gate 	    "usba_hubdi_ioctl: "
73917c478bd9Sstevel@tonic-gate 	    "cmd=%x, arg=%lx, mode=%x, cred=%p, rval=%p dev=0x%lx",
7392112116d8Sfb 	    cmd, arg, mode, (void *)credp, (void *)rvalp, dev);
73937c478bd9Sstevel@tonic-gate 
73947c478bd9Sstevel@tonic-gate 	/* read devctl ioctl data */
73957c478bd9Sstevel@tonic-gate 	if ((cmd != DEVCTL_AP_CONTROL) &&
73967c478bd9Sstevel@tonic-gate 	    (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)) {
73977c478bd9Sstevel@tonic-gate 
73987c478bd9Sstevel@tonic-gate 		return (EFAULT);
73997c478bd9Sstevel@tonic-gate 	}
74007c478bd9Sstevel@tonic-gate 
74017c478bd9Sstevel@tonic-gate 	/*
74027c478bd9Sstevel@tonic-gate 	 * make sure the hub is connected before trying any
74037c478bd9Sstevel@tonic-gate 	 * of the following operations:
74047c478bd9Sstevel@tonic-gate 	 * configure, connect, disconnect
74057c478bd9Sstevel@tonic-gate 	 */
74067c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
74077c478bd9Sstevel@tonic-gate 
74087c478bd9Sstevel@tonic-gate 	switch (cmd) {
74097c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
74107c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
74117c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
74127c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_DISCONNECTED) {
74137c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
74147c478bd9Sstevel@tonic-gate 			    "hubd: already gone");
74157c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
74167c478bd9Sstevel@tonic-gate 			if (dcp) {
74177c478bd9Sstevel@tonic-gate 				ndi_dc_freehdl(dcp);
74187c478bd9Sstevel@tonic-gate 			}
74197c478bd9Sstevel@tonic-gate 
74207c478bd9Sstevel@tonic-gate 			return (EIO);
74217c478bd9Sstevel@tonic-gate 		}
74227c478bd9Sstevel@tonic-gate 
74237c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
74247c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
74257c478bd9Sstevel@tonic-gate 		if ((port = hubd_get_port_num(hubd, dcp)) == 0) {
74267c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
74277c478bd9Sstevel@tonic-gate 			    "hubd: bad port");
74287c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
74297c478bd9Sstevel@tonic-gate 			if (dcp) {
74307c478bd9Sstevel@tonic-gate 				ndi_dc_freehdl(dcp);
74317c478bd9Sstevel@tonic-gate 			}
74327c478bd9Sstevel@tonic-gate 
74337c478bd9Sstevel@tonic-gate 			return (EINVAL);
74347c478bd9Sstevel@tonic-gate 		}
74357c478bd9Sstevel@tonic-gate 		break;
74367c478bd9Sstevel@tonic-gate 
74377c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
74387c478bd9Sstevel@tonic-gate 
74397c478bd9Sstevel@tonic-gate 		break;
74407c478bd9Sstevel@tonic-gate 	default:
74417c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
74427c478bd9Sstevel@tonic-gate 		if (dcp) {
74437c478bd9Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
74447c478bd9Sstevel@tonic-gate 		}
74457c478bd9Sstevel@tonic-gate 
74467c478bd9Sstevel@tonic-gate 		return (ENOTTY);
74477c478bd9Sstevel@tonic-gate 	}
74487c478bd9Sstevel@tonic-gate 
74497c478bd9Sstevel@tonic-gate 	/* should not happen, just in case */
74507c478bd9Sstevel@tonic-gate 	if (hubd->h_dev_state == USB_DEV_SUSPENDED) {
74517c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
74527c478bd9Sstevel@tonic-gate 		if (dcp) {
74537c478bd9Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
74547c478bd9Sstevel@tonic-gate 		}
74557c478bd9Sstevel@tonic-gate 
74567c478bd9Sstevel@tonic-gate 		return (EIO);
74577c478bd9Sstevel@tonic-gate 	}
74587c478bd9Sstevel@tonic-gate 
7459ffcd51f3Slg 	if (hubd->h_reset_port[port]) {
7460ffcd51f3Slg 		USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7461ffcd51f3Slg 		    "This port is resetting, just return");
7462ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
7463ffcd51f3Slg 		if (dcp) {
7464ffcd51f3Slg 			ndi_dc_freehdl(dcp);
7465ffcd51f3Slg 		}
7466ffcd51f3Slg 
7467ffcd51f3Slg 		return (EIO);
7468ffcd51f3Slg 	}
7469ffcd51f3Slg 
74707c478bd9Sstevel@tonic-gate 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
74717c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
74727c478bd9Sstevel@tonic-gate 
74737c478bd9Sstevel@tonic-gate 	/* go full power */
74747c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
74757c478bd9Sstevel@tonic-gate 
74767c478bd9Sstevel@tonic-gate 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
74777c478bd9Sstevel@tonic-gate 	ndi_devi_enter(rh_dip, &rh_circ);
74787c478bd9Sstevel@tonic-gate 	ndi_devi_enter(hubd->h_dip, &circ);
74797c478bd9Sstevel@tonic-gate 
74807c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
74817c478bd9Sstevel@tonic-gate 
7482c0f24e5bSlg 	hubd->h_hotplug_thread++;
7483c0f24e5bSlg 
7484c0f24e5bSlg 	/* stop polling if it was active */
74857c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
74867c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
74877c478bd9Sstevel@tonic-gate 		(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
7488c0f24e5bSlg 		    USB_FLAGS_SLEEP);
74897c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
74907c478bd9Sstevel@tonic-gate 
74917c478bd9Sstevel@tonic-gate 		if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
74927c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
74937c478bd9Sstevel@tonic-gate 		}
74947c478bd9Sstevel@tonic-gate 	}
74957c478bd9Sstevel@tonic-gate 
74967c478bd9Sstevel@tonic-gate 	switch (cmd) {
74977c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
74987c478bd9Sstevel@tonic-gate 		if (hubd_delete_child(hubd, port,
74997c478bd9Sstevel@tonic-gate 		    NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS) {
75007c478bd9Sstevel@tonic-gate 			rv = EIO;
75017c478bd9Sstevel@tonic-gate 		}
75027c478bd9Sstevel@tonic-gate 
75037c478bd9Sstevel@tonic-gate 		break;
75047c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
75057c478bd9Sstevel@tonic-gate 		if (hubd_delete_child(hubd, port,
75067c478bd9Sstevel@tonic-gate 		    NDI_UNCONFIG, B_FALSE) != USB_SUCCESS) {
75077c478bd9Sstevel@tonic-gate 			rv = EIO;
75087c478bd9Sstevel@tonic-gate 		}
75097c478bd9Sstevel@tonic-gate 
75107c478bd9Sstevel@tonic-gate 		break;
75117c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
75127c478bd9Sstevel@tonic-gate 		/* toggle port */
75137c478bd9Sstevel@tonic-gate 		if (hubd_toggle_port(hubd, port) != USB_SUCCESS) {
75147c478bd9Sstevel@tonic-gate 			rv = EIO;
75157c478bd9Sstevel@tonic-gate 
75167c478bd9Sstevel@tonic-gate 			break;
75177c478bd9Sstevel@tonic-gate 		}
75187c478bd9Sstevel@tonic-gate 
75197c478bd9Sstevel@tonic-gate 		(void) hubd_handle_port_connect(hubd, port);
75207c478bd9Sstevel@tonic-gate 		child_dip = hubd_get_child_dip(hubd, port);
75217c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
75227c478bd9Sstevel@tonic-gate 
75237c478bd9Sstevel@tonic-gate 		ndi_devi_exit(hubd->h_dip, circ);
75247c478bd9Sstevel@tonic-gate 		ndi_devi_exit(rh_dip, rh_circ);
75257c478bd9Sstevel@tonic-gate 		ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
7526e4c316c4SVincent Wang 		if (child_dip == NULL) {
75277c478bd9Sstevel@tonic-gate 			rv = EIO;
7528e4c316c4SVincent Wang 		} else {
7529e4c316c4SVincent Wang 			ndi_hold_devi(child_dip);
7530e4c316c4SVincent Wang 			if (ndi_devi_online(child_dip, 0) != NDI_SUCCESS)
7531e4c316c4SVincent Wang 				rv = EIO;
7532e4c316c4SVincent Wang 			ndi_rele_devi(child_dip);
75337c478bd9Sstevel@tonic-gate 		}
75347c478bd9Sstevel@tonic-gate 		ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
75357c478bd9Sstevel@tonic-gate 		ndi_devi_enter(rh_dip, &rh_circ);
75367c478bd9Sstevel@tonic-gate 		ndi_devi_enter(hubd->h_dip, &circ);
75377c478bd9Sstevel@tonic-gate 
75387c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
75397c478bd9Sstevel@tonic-gate 
75407c478bd9Sstevel@tonic-gate 		break;
75417c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
75427c478bd9Sstevel@tonic-gate 		switch (hubd_cfgadm_state(hubd, port)) {
75437c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_DISCONNECTED:
75447c478bd9Sstevel@tonic-gate 			/* port previously 'disconnected' by cfgadm */
75457c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_DISCONNECTED;
75467c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
75477c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
75487c478bd9Sstevel@tonic-gate 
75497c478bd9Sstevel@tonic-gate 			break;
75507c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_UNCONFIGURED:
75517c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_CONNECTED;
75527c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
75537c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
75547c478bd9Sstevel@tonic-gate 
75557c478bd9Sstevel@tonic-gate 			break;
75567c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_CONFIGURED:
75577c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_CONNECTED;
75587c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
75597c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
75607c478bd9Sstevel@tonic-gate 
75617c478bd9Sstevel@tonic-gate 			break;
75627c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_STILL_REFERENCED:
75637c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_EMPTY;
75647c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
75657c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_UNUSABLE;
75667c478bd9Sstevel@tonic-gate 
75677c478bd9Sstevel@tonic-gate 			break;
75687c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_EMPTY:
75697c478bd9Sstevel@tonic-gate 		default:
75707c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_EMPTY;
75717c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
75727c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
75737c478bd9Sstevel@tonic-gate 
75747c478bd9Sstevel@tonic-gate 			break;
75757c478bd9Sstevel@tonic-gate 		}
75767c478bd9Sstevel@tonic-gate 
75777c478bd9Sstevel@tonic-gate 		ap_state.ap_last_change = (time_t)-1;
75787c478bd9Sstevel@tonic-gate 		ap_state.ap_error_code = 0;
75797c478bd9Sstevel@tonic-gate 		ap_state.ap_in_transition = 0;
75807c478bd9Sstevel@tonic-gate 
75817c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
75827c478bd9Sstevel@tonic-gate 		    "DEVCTL_AP_GETSTATE: "
75837c478bd9Sstevel@tonic-gate 		    "ostate=0x%x, rstate=0x%x, condition=0x%x",
75847c478bd9Sstevel@tonic-gate 		    ap_state.ap_ostate,
75857c478bd9Sstevel@tonic-gate 		    ap_state.ap_rstate, ap_state.ap_condition);
75867c478bd9Sstevel@tonic-gate 
75877c478bd9Sstevel@tonic-gate 		/* copy the return-AP-state information to the user space */
75887c478bd9Sstevel@tonic-gate 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
75897c478bd9Sstevel@tonic-gate 			rv = EFAULT;
75907c478bd9Sstevel@tonic-gate 		}
75917c478bd9Sstevel@tonic-gate 
75927c478bd9Sstevel@tonic-gate 		break;
75937c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
75947c478bd9Sstevel@tonic-gate 	{
75957c478bd9Sstevel@tonic-gate 		/*
75967c478bd9Sstevel@tonic-gate 		 * Generic devctl for hardware-specific functionality.
75977c478bd9Sstevel@tonic-gate 		 * For list of sub-commands see hubd_impl.h
75987c478bd9Sstevel@tonic-gate 		 */
75997c478bd9Sstevel@tonic-gate 		hubd_ioctl_data_t	ioc;	/* for 64 byte copies */
76007c478bd9Sstevel@tonic-gate 
76017c478bd9Sstevel@tonic-gate 		/* copy user ioctl data in first */
76027c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
76037c478bd9Sstevel@tonic-gate 		if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
76047c478bd9Sstevel@tonic-gate 			hubd_ioctl_data_32_t ioc32;
76057c478bd9Sstevel@tonic-gate 
76067c478bd9Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, (void *)&ioc32,
7607c0f24e5bSlg 			    sizeof (ioc32), mode) != 0) {
76087c478bd9Sstevel@tonic-gate 				rv = EFAULT;
76097c478bd9Sstevel@tonic-gate 
76107c478bd9Sstevel@tonic-gate 				break;
76117c478bd9Sstevel@tonic-gate 			}
76127c478bd9Sstevel@tonic-gate 			ioc.cmd		= (uint_t)ioc32.cmd;
76137c478bd9Sstevel@tonic-gate 			ioc.port	= (uint_t)ioc32.port;
76147c478bd9Sstevel@tonic-gate 			ioc.get_size	= (uint_t)ioc32.get_size;
76157c478bd9Sstevel@tonic-gate 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
76167c478bd9Sstevel@tonic-gate 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
76177c478bd9Sstevel@tonic-gate 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
76187c478bd9Sstevel@tonic-gate 		} else
76197c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
76207c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
76217c478bd9Sstevel@tonic-gate 		    mode) != 0) {
76227c478bd9Sstevel@tonic-gate 			rv = EFAULT;
76237c478bd9Sstevel@tonic-gate 
76247c478bd9Sstevel@tonic-gate 			break;
76257c478bd9Sstevel@tonic-gate 		}
76267c478bd9Sstevel@tonic-gate 
76277c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
76287c478bd9Sstevel@tonic-gate 		    "DEVCTL_AP_CONTROL: ioc: cmd=0x%x port=%d get_size=%d"
76297c478bd9Sstevel@tonic-gate 		    "\n\tbuf=0x%p, bufsiz=%d,  misc_arg=%d", ioc.cmd,
7630112116d8Sfb 		    ioc.port, ioc.get_size, (void *)ioc.buf, ioc.bufsiz,
7631112116d8Sfb 		    ioc.misc_arg);
76327c478bd9Sstevel@tonic-gate 
76337c478bd9Sstevel@tonic-gate 		/*
76347c478bd9Sstevel@tonic-gate 		 * To avoid BE/LE and 32/64 issues, a get_size always
76357c478bd9Sstevel@tonic-gate 		 * returns a 32-bit number.
76367c478bd9Sstevel@tonic-gate 		 */
76377c478bd9Sstevel@tonic-gate 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
76387c478bd9Sstevel@tonic-gate 			rv = EINVAL;
76397c478bd9Sstevel@tonic-gate 
76407c478bd9Sstevel@tonic-gate 			break;
76417c478bd9Sstevel@tonic-gate 		}
76427c478bd9Sstevel@tonic-gate 
76437c478bd9Sstevel@tonic-gate 		switch (ioc.cmd) {
76447c478bd9Sstevel@tonic-gate 		case USB_DESCR_TYPE_DEV:
76457c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_DEVICE_DESC";
76467c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
76477c478bd9Sstevel@tonic-gate 				/* uint32 so this works 32/64 */
76487c478bd9Sstevel@tonic-gate 				uint32_t size = sizeof (usb_dev_descr_t);
76497c478bd9Sstevel@tonic-gate 
76507c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size, ioc.buf,
76517c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
76527c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
76537c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
76547c478bd9Sstevel@tonic-gate 					    "%s: get_size copyout failed", msg);
76557c478bd9Sstevel@tonic-gate 					rv = EIO;
76567c478bd9Sstevel@tonic-gate 
76577c478bd9Sstevel@tonic-gate 					break;
76587c478bd9Sstevel@tonic-gate 				}
76597c478bd9Sstevel@tonic-gate 			} else {	/* send out the actual descr */
76607c478bd9Sstevel@tonic-gate 				usb_dev_descr_t *dev_descrp;
76617c478bd9Sstevel@tonic-gate 
76627c478bd9Sstevel@tonic-gate 				/* check child_dip */
76637c478bd9Sstevel@tonic-gate 				if ((child_dip = hubd_get_child_dip(hubd,
76647c478bd9Sstevel@tonic-gate 				    ioc.port)) == NULL) {
76657c478bd9Sstevel@tonic-gate 					rv = EINVAL;
76667c478bd9Sstevel@tonic-gate 
76677c478bd9Sstevel@tonic-gate 					break;
76687c478bd9Sstevel@tonic-gate 				}
76697c478bd9Sstevel@tonic-gate 
76707c478bd9Sstevel@tonic-gate 				dev_descrp = usb_get_dev_descr(child_dip);
76717c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != sizeof (*dev_descrp)) {
76727c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
76737c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
76747c478bd9Sstevel@tonic-gate 					    "%s: bufsize passed (%d) != sizeof "
76757c478bd9Sstevel@tonic-gate 					    "usba_device_descr_t (%d)", msg,
76767c478bd9Sstevel@tonic-gate 					    ioc.bufsiz, dev_descrp->bLength);
76777c478bd9Sstevel@tonic-gate 					rv = EINVAL;
76787c478bd9Sstevel@tonic-gate 
76797c478bd9Sstevel@tonic-gate 					break;
76807c478bd9Sstevel@tonic-gate 				}
76817c478bd9Sstevel@tonic-gate 
76827c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)dev_descrp,
76837c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
76847c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
76857c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
76867c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
76877c478bd9Sstevel@tonic-gate 					rv = EIO;
76887c478bd9Sstevel@tonic-gate 
76897c478bd9Sstevel@tonic-gate 					break;
76907c478bd9Sstevel@tonic-gate 				}
76917c478bd9Sstevel@tonic-gate 			}
76927c478bd9Sstevel@tonic-gate 			break;
76937c478bd9Sstevel@tonic-gate 		case USB_DESCR_TYPE_STRING:
76947c478bd9Sstevel@tonic-gate 		{
76957c478bd9Sstevel@tonic-gate 			char		*str;
76967c478bd9Sstevel@tonic-gate 			uint32_t	size;
76977c478bd9Sstevel@tonic-gate 			usba_device_t	*usba_device;
76987c478bd9Sstevel@tonic-gate 
76997c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_STRING_DESCR";
77007c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
77017c478bd9Sstevel@tonic-gate 			    "%s: string request: %d", msg, ioc.misc_arg);
77027c478bd9Sstevel@tonic-gate 
77037c478bd9Sstevel@tonic-gate 			/* recheck */
77047c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
77057c478bd9Sstevel@tonic-gate 			    NULL) {
77067c478bd9Sstevel@tonic-gate 				rv = EINVAL;
77077c478bd9Sstevel@tonic-gate 
77087c478bd9Sstevel@tonic-gate 				break;
77097c478bd9Sstevel@tonic-gate 			}
77107c478bd9Sstevel@tonic-gate 			usba_device = usba_get_usba_device(child_dip);
77117c478bd9Sstevel@tonic-gate 
77127c478bd9Sstevel@tonic-gate 			switch (ioc.misc_arg) {
77137c478bd9Sstevel@tonic-gate 			case HUBD_MFG_STR:
77147c478bd9Sstevel@tonic-gate 				str = usba_device->usb_mfg_str;
77157c478bd9Sstevel@tonic-gate 
77167c478bd9Sstevel@tonic-gate 				break;
77177c478bd9Sstevel@tonic-gate 			case HUBD_PRODUCT_STR:
77187c478bd9Sstevel@tonic-gate 				str = usba_device->usb_product_str;
77197c478bd9Sstevel@tonic-gate 
77207c478bd9Sstevel@tonic-gate 				break;
77217c478bd9Sstevel@tonic-gate 			case HUBD_SERIALNO_STR:
77227c478bd9Sstevel@tonic-gate 				str = usba_device->usb_serialno_str;
77237c478bd9Sstevel@tonic-gate 
77247c478bd9Sstevel@tonic-gate 				break;
77257c478bd9Sstevel@tonic-gate 			case HUBD_CFG_DESCR_STR:
77267c478bd9Sstevel@tonic-gate 				mutex_enter(&usba_device->usb_mutex);
77277c478bd9Sstevel@tonic-gate 				str = usba_device->usb_cfg_str_descr[
7728c0f24e5bSlg 				    usba_device->usb_active_cfg_ndx];
77297c478bd9Sstevel@tonic-gate 				mutex_exit(&usba_device->usb_mutex);
77307c478bd9Sstevel@tonic-gate 
77317c478bd9Sstevel@tonic-gate 				break;
77327c478bd9Sstevel@tonic-gate 			default:
77337c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
77347c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
77357c478bd9Sstevel@tonic-gate 				    "%s: Invalid string request", msg);
77367c478bd9Sstevel@tonic-gate 				rv = EINVAL;
77377c478bd9Sstevel@tonic-gate 
77387c478bd9Sstevel@tonic-gate 				break;
77397c478bd9Sstevel@tonic-gate 			} /* end of switch */
77407c478bd9Sstevel@tonic-gate 
77417c478bd9Sstevel@tonic-gate 			if (rv != 0) {
77427c478bd9Sstevel@tonic-gate 
77437c478bd9Sstevel@tonic-gate 				break;
77447c478bd9Sstevel@tonic-gate 			}
77457c478bd9Sstevel@tonic-gate 
77467c478bd9Sstevel@tonic-gate 			size = (str != NULL) ? strlen(str) + 1 : 0;
77477c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
77487c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size, ioc.buf,
77497c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
77507c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
77517c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
77527c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
77537c478bd9Sstevel@tonic-gate 					rv = EIO;
77547c478bd9Sstevel@tonic-gate 
77557c478bd9Sstevel@tonic-gate 					break;
77567c478bd9Sstevel@tonic-gate 				}
77577c478bd9Sstevel@tonic-gate 			} else {
77587c478bd9Sstevel@tonic-gate 				if (size == 0) {
77597c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L3(DPRINT_MASK_CBOPS,
77607c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
77617c478bd9Sstevel@tonic-gate 					    "%s: String is NULL", msg);
77627c478bd9Sstevel@tonic-gate 					rv = EINVAL;
77637c478bd9Sstevel@tonic-gate 
77647c478bd9Sstevel@tonic-gate 					break;
77657c478bd9Sstevel@tonic-gate 				}
77667c478bd9Sstevel@tonic-gate 
77677c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
77687c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
77697c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
77707c478bd9Sstevel@tonic-gate 					    "%s: string buf size wrong", msg);
77717c478bd9Sstevel@tonic-gate 					rv = EINVAL;
77727c478bd9Sstevel@tonic-gate 
77737c478bd9Sstevel@tonic-gate 					break;
77747c478bd9Sstevel@tonic-gate 				}
77757c478bd9Sstevel@tonic-gate 
77767c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)str, ioc.buf,
77777c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
77787c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
77797c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
77807c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
77817c478bd9Sstevel@tonic-gate 					rv = EIO;
77827c478bd9Sstevel@tonic-gate 
77837c478bd9Sstevel@tonic-gate 					break;
77847c478bd9Sstevel@tonic-gate 				}
77857c478bd9Sstevel@tonic-gate 			}
77867c478bd9Sstevel@tonic-gate 			break;
77877c478bd9Sstevel@tonic-gate 		}
77887c478bd9Sstevel@tonic-gate 		case HUBD_GET_CFGADM_NAME:
77897c478bd9Sstevel@tonic-gate 		{
77907c478bd9Sstevel@tonic-gate 			uint32_t   name_len;
77917c478bd9Sstevel@tonic-gate 			const char *name;
77927c478bd9Sstevel@tonic-gate 
77937c478bd9Sstevel@tonic-gate 			/* recheck */
77947c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
77957c478bd9Sstevel@tonic-gate 			    NULL) {
77967c478bd9Sstevel@tonic-gate 				rv = EINVAL;
77977c478bd9Sstevel@tonic-gate 
77987c478bd9Sstevel@tonic-gate 				break;
77997c478bd9Sstevel@tonic-gate 			}
78007c478bd9Sstevel@tonic-gate 			name = ddi_node_name(child_dip);
78017c478bd9Sstevel@tonic-gate 			if (name == NULL) {
78027c478bd9Sstevel@tonic-gate 				name = "unsupported";
78037c478bd9Sstevel@tonic-gate 			}
78047c478bd9Sstevel@tonic-gate 			name_len = strlen(name) + 1;
78057c478bd9Sstevel@tonic-gate 
78067c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: HUBD_GET_CFGADM_NAME";
78077c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
78087c478bd9Sstevel@tonic-gate 			    "%s: name=%s name_len=%d", msg, name, name_len);
78097c478bd9Sstevel@tonic-gate 
78107c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
78117c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&name_len,
78127c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
78137c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
78147c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
78157c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed", msg);
78167c478bd9Sstevel@tonic-gate 					rv = EIO;
78177c478bd9Sstevel@tonic-gate 
78187c478bd9Sstevel@tonic-gate 					break;
78197c478bd9Sstevel@tonic-gate 				}
78207c478bd9Sstevel@tonic-gate 			} else {
78217c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != name_len) {
78227c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
78237c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
78247c478bd9Sstevel@tonic-gate 					    "%s: string buf length wrong", msg);
78257c478bd9Sstevel@tonic-gate 					rv = EINVAL;
78267c478bd9Sstevel@tonic-gate 
78277c478bd9Sstevel@tonic-gate 					break;
78287c478bd9Sstevel@tonic-gate 				}
78297c478bd9Sstevel@tonic-gate 
78307c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)name, ioc.buf,
78317c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
78327c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
78337c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
78347c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
78357c478bd9Sstevel@tonic-gate 					rv = EIO;
78367c478bd9Sstevel@tonic-gate 
78377c478bd9Sstevel@tonic-gate 					break;
78387c478bd9Sstevel@tonic-gate 				}
78397c478bd9Sstevel@tonic-gate 			}
78407c478bd9Sstevel@tonic-gate 
78417c478bd9Sstevel@tonic-gate 			break;
78427c478bd9Sstevel@tonic-gate 		}
78437c478bd9Sstevel@tonic-gate 
78447c478bd9Sstevel@tonic-gate 		/*
78457c478bd9Sstevel@tonic-gate 		 * Return the config index for the currently-configured
78467c478bd9Sstevel@tonic-gate 		 * configuration.
78477c478bd9Sstevel@tonic-gate 		 */
78487c478bd9Sstevel@tonic-gate 		case HUBD_GET_CURRENT_CONFIG:
78497c478bd9Sstevel@tonic-gate 		{
78507c478bd9Sstevel@tonic-gate 			uint_t		config_index;
78517c478bd9Sstevel@tonic-gate 			uint32_t	size = sizeof (config_index);
78527c478bd9Sstevel@tonic-gate 			usba_device_t	*usba_device;
78537c478bd9Sstevel@tonic-gate 
78547c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_CURRENT_CONFIG";
78557c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
78567c478bd9Sstevel@tonic-gate 			    "%s", msg);
78577c478bd9Sstevel@tonic-gate 
78587c478bd9Sstevel@tonic-gate 			/*
78597c478bd9Sstevel@tonic-gate 			 * Return the config index for the configuration
78607c478bd9Sstevel@tonic-gate 			 * currently in use.
78617c478bd9Sstevel@tonic-gate 			 * Recheck if child_dip exists
78627c478bd9Sstevel@tonic-gate 			 */
78637c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
78647c478bd9Sstevel@tonic-gate 			    NULL) {
78657c478bd9Sstevel@tonic-gate 				rv = EINVAL;
78667c478bd9Sstevel@tonic-gate 
78677c478bd9Sstevel@tonic-gate 				break;
78687c478bd9Sstevel@tonic-gate 			}
78697c478bd9Sstevel@tonic-gate 
78707c478bd9Sstevel@tonic-gate 			usba_device = usba_get_usba_device(child_dip);
78717c478bd9Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
78727c478bd9Sstevel@tonic-gate 			config_index = usba_device->usb_active_cfg_ndx;
78737c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
78747c478bd9Sstevel@tonic-gate 
78757c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
78767c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size,
78777c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
78787c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
78797c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
78807c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
78817c478bd9Sstevel@tonic-gate 					rv = EIO;
78827c478bd9Sstevel@tonic-gate 
78837c478bd9Sstevel@tonic-gate 					break;
78847c478bd9Sstevel@tonic-gate 				}
78857c478bd9Sstevel@tonic-gate 			} else {
78867c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
78877c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
78887c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
78897c478bd9Sstevel@tonic-gate 					    "%s: buffer size wrong", msg);
78907c478bd9Sstevel@tonic-gate 					rv = EINVAL;
78917c478bd9Sstevel@tonic-gate 
78927c478bd9Sstevel@tonic-gate 					break;
78937c478bd9Sstevel@tonic-gate 				}
78947c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&config_index,
78957c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
78967c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
78977c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
78987c478bd9Sstevel@tonic-gate 					    "%s: copyout failed", msg);
78997c478bd9Sstevel@tonic-gate 					rv = EIO;
79007c478bd9Sstevel@tonic-gate 				}
79017c478bd9Sstevel@tonic-gate 			}
79027c478bd9Sstevel@tonic-gate 
79037c478bd9Sstevel@tonic-gate 			break;
79047c478bd9Sstevel@tonic-gate 		}
79057c478bd9Sstevel@tonic-gate 		case HUBD_GET_DEVICE_PATH:
79067c478bd9Sstevel@tonic-gate 		{
79077c478bd9Sstevel@tonic-gate 			char		*path;
79087c478bd9Sstevel@tonic-gate 			uint32_t	size;
79097c478bd9Sstevel@tonic-gate 
79107c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_DEVICE_PATH";
79117c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
79127c478bd9Sstevel@tonic-gate 			    "%s", msg);
79137c478bd9Sstevel@tonic-gate 
79147c478bd9Sstevel@tonic-gate 			/* Recheck if child_dip exists */
79157c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
79167c478bd9Sstevel@tonic-gate 			    NULL) {
79177c478bd9Sstevel@tonic-gate 				rv = EINVAL;
79187c478bd9Sstevel@tonic-gate 
79197c478bd9Sstevel@tonic-gate 				break;
79207c478bd9Sstevel@tonic-gate 			}
79217c478bd9Sstevel@tonic-gate 
79227c478bd9Sstevel@tonic-gate 			/* ddi_pathname doesn't supply /devices, so we do. */
79237c478bd9Sstevel@tonic-gate 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
79247c478bd9Sstevel@tonic-gate 			(void) strcpy(path, "/devices");
79257c478bd9Sstevel@tonic-gate 			(void) ddi_pathname(child_dip, path + strlen(path));
79267c478bd9Sstevel@tonic-gate 			size = strlen(path) + 1;
79277c478bd9Sstevel@tonic-gate 
79287c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
79297c478bd9Sstevel@tonic-gate 			    "%s: device path=%s  size=%d", msg, path, size);
79307c478bd9Sstevel@tonic-gate 
79317c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
79327c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size,
79337c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
79347c478bd9Sstevel@tonic-gate 
79357c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
79367c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
79377c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
79387c478bd9Sstevel@tonic-gate 					rv = EIO;
79397c478bd9Sstevel@tonic-gate 				}
79407c478bd9Sstevel@tonic-gate 			} else {
79417c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
79427c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
79437c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
79447c478bd9Sstevel@tonic-gate 					    "%s: buffer wrong size.", msg);
79457c478bd9Sstevel@tonic-gate 					rv = EINVAL;
79467c478bd9Sstevel@tonic-gate 				} else if (ddi_copyout((void *)path,
79477c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
79487c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
79497c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
79507c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
79517c478bd9Sstevel@tonic-gate 					rv = EIO;
79527c478bd9Sstevel@tonic-gate 				}
79537c478bd9Sstevel@tonic-gate 			}
79547c478bd9Sstevel@tonic-gate 			kmem_free(path, MAXPATHLEN);
79557c478bd9Sstevel@tonic-gate 
79567c478bd9Sstevel@tonic-gate 			break;
79577c478bd9Sstevel@tonic-gate 		}
79587c478bd9Sstevel@tonic-gate 		case HUBD_REFRESH_DEVDB:
79597c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: HUBD_REFRESH_DEVDB";
79607c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
79617c478bd9Sstevel@tonic-gate 			    "%s", msg);
79627c478bd9Sstevel@tonic-gate 
79637c478bd9Sstevel@tonic-gate 			if ((rv = usba_devdb_refresh()) != USB_SUCCESS) {
79647c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
79657c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
79667c478bd9Sstevel@tonic-gate 				    "%s: Failed: %d", msg, rv);
79677c478bd9Sstevel@tonic-gate 				rv = EIO;
79687c478bd9Sstevel@tonic-gate 			}
79697c478bd9Sstevel@tonic-gate 
79707c478bd9Sstevel@tonic-gate 			break;
79717c478bd9Sstevel@tonic-gate 		default:
79727c478bd9Sstevel@tonic-gate 			rv = ENOTSUP;
79737c478bd9Sstevel@tonic-gate 		}	/* end switch */
79747c478bd9Sstevel@tonic-gate 
79757c478bd9Sstevel@tonic-gate 		break;
79767c478bd9Sstevel@tonic-gate 	}
79777c478bd9Sstevel@tonic-gate 
79787c478bd9Sstevel@tonic-gate 	default:
79797c478bd9Sstevel@tonic-gate 		rv = ENOTTY;
79807c478bd9Sstevel@tonic-gate 	}
79817c478bd9Sstevel@tonic-gate 
79827c478bd9Sstevel@tonic-gate 	if (dcp) {
79837c478bd9Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
79847c478bd9Sstevel@tonic-gate 	}
79857c478bd9Sstevel@tonic-gate 
79867c478bd9Sstevel@tonic-gate 	/* allow hotplug thread now */
79877c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
79887c478bd9Sstevel@tonic-gate 
79897c478bd9Sstevel@tonic-gate 	if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
79907c478bd9Sstevel@tonic-gate 	    hubd->h_ep1_ph && (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
79917c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
79927c478bd9Sstevel@tonic-gate 	}
79937c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
79947c478bd9Sstevel@tonic-gate 
79957c478bd9Sstevel@tonic-gate 	ndi_devi_exit(hubd->h_dip, circ);
79967c478bd9Sstevel@tonic-gate 	ndi_devi_exit(rh_dip, rh_circ);
79977c478bd9Sstevel@tonic-gate 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
79987c478bd9Sstevel@tonic-gate 
79997c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
80007c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, hubd->h_dip, 0);
80017c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
80027c478bd9Sstevel@tonic-gate 
80037c478bd9Sstevel@tonic-gate 	return (rv);
80047c478bd9Sstevel@tonic-gate }
80057c478bd9Sstevel@tonic-gate 
80067c478bd9Sstevel@tonic-gate 
80077c478bd9Sstevel@tonic-gate /*
80087c478bd9Sstevel@tonic-gate  * Helper func used only to help construct the names for the attachment point
80097c478bd9Sstevel@tonic-gate  * minor nodes.  Used only in usba_hubdi_attach.
80107c478bd9Sstevel@tonic-gate  * Returns whether it found ancestry or not (USB_SUCCESS if yes).
80117c478bd9Sstevel@tonic-gate  * ports between the root hub and the device represented by dip.
80127c478bd9Sstevel@tonic-gate  * E.g.,  "2.4.3.1" means this device is
80137c478bd9Sstevel@tonic-gate  *	plugged into port 1 of a hub that is
80147c478bd9Sstevel@tonic-gate  *	plugged into port 3 of a hub that is
80157c478bd9Sstevel@tonic-gate  *	plugged into port 4 of a hub that is
80167c478bd9Sstevel@tonic-gate  *	plugged into port 2 of the root hub.
80177c478bd9Sstevel@tonic-gate  * NOTE: Max ap_id path len is HUBD_APID_NAMELEN (32 chars), which is
80187c478bd9Sstevel@tonic-gate  * more than sufficient (as hubs are a max 6 levels deep, port needs 3
80197c478bd9Sstevel@tonic-gate  * chars plus NULL each)
80207c478bd9Sstevel@tonic-gate  */
8021ff0e937bSRaymond Chen void
80227c478bd9Sstevel@tonic-gate hubd_get_ancestry_str(hubd_t *hubd)
80237c478bd9Sstevel@tonic-gate {
8024ff0e937bSRaymond Chen 	char		ap_name[HUBD_APID_NAMELEN];
8025ff0e937bSRaymond Chen 	dev_info_t	*pdip;
8026ff0e937bSRaymond Chen 	hubd_t		*phubd;
8027ff0e937bSRaymond Chen 	usb_port_t	port;
80287c478bd9Sstevel@tonic-gate 
80297c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
8030112116d8Sfb 	    "hubd_get_ancestry_str: hubd=0x%p", (void *)hubd);
80317c478bd9Sstevel@tonic-gate 
8032ff0e937bSRaymond Chen 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8033ff0e937bSRaymond Chen 
8034ff0e937bSRaymond Chen 	/*
8035ff0e937bSRaymond Chen 	 * The function is extended to support wire adapter class
8036ff0e937bSRaymond Chen 	 * devices introduced by WUSB spec. The node name is no
8037ff0e937bSRaymond Chen 	 * longer "hub" only.
8038ff0e937bSRaymond Chen 	 * Generate the ap_id str based on the parent and child
8039ff0e937bSRaymond Chen 	 * relationship instead of retrieving it from the hub
8040ff0e937bSRaymond Chen 	 * device path, which simplifies the algorithm.
8041ff0e937bSRaymond Chen 	 */
8042ff0e937bSRaymond Chen 	if (usba_is_root_hub(hubd->h_dip)) {
8043ff0e937bSRaymond Chen 		hubd->h_ancestry_str[0] = '\0';
8044ff0e937bSRaymond Chen 	} else {
8045ff0e937bSRaymond Chen 		port = hubd->h_usba_device->usb_port;
8046ff0e937bSRaymond Chen 		mutex_exit(HUBD_MUTEX(hubd));
80477c478bd9Sstevel@tonic-gate 
8048ff0e937bSRaymond Chen 		pdip = ddi_get_parent(hubd->h_dip);
80497c478bd9Sstevel@tonic-gate 		/*
8050ff0e937bSRaymond Chen 		 * The parent of wire adapter device might be usb_mid.
8051ff0e937bSRaymond Chen 		 * Need to look further up for hub device
80527c478bd9Sstevel@tonic-gate 		 */
8053ff0e937bSRaymond Chen 		if (strcmp(ddi_driver_name(pdip), "usb_mid") == 0) {
8054ff0e937bSRaymond Chen 			pdip = ddi_get_parent(pdip);
8055ff0e937bSRaymond Chen 			ASSERT(pdip != NULL);
80567c478bd9Sstevel@tonic-gate 		}
80577c478bd9Sstevel@tonic-gate 
8058ff0e937bSRaymond Chen 		phubd = hubd_get_soft_state(pdip);
80597c478bd9Sstevel@tonic-gate 
8060ff0e937bSRaymond Chen 		mutex_enter(HUBD_MUTEX(phubd));
8061ff0e937bSRaymond Chen 		(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
8062ff0e937bSRaymond Chen 		    phubd->h_ancestry_str, port);
8063ff0e937bSRaymond Chen 		mutex_exit(HUBD_MUTEX(phubd));
80647c478bd9Sstevel@tonic-gate 
8065ff0e937bSRaymond Chen 		mutex_enter(HUBD_MUTEX(hubd));
8066ff0e937bSRaymond Chen 		(void) strcpy(hubd->h_ancestry_str, ap_name);
80677c478bd9Sstevel@tonic-gate 		(void) strcat(hubd->h_ancestry_str, ".");
80687c478bd9Sstevel@tonic-gate 	}
80697c478bd9Sstevel@tonic-gate }
80707c478bd9Sstevel@tonic-gate 
80717c478bd9Sstevel@tonic-gate 
80727c478bd9Sstevel@tonic-gate /* Get which port to operate on.  */
80737c478bd9Sstevel@tonic-gate static usb_port_t
80747c478bd9Sstevel@tonic-gate hubd_get_port_num(hubd_t *hubd, struct devctl_iocdata *dcp)
80757c478bd9Sstevel@tonic-gate {
80767c478bd9Sstevel@tonic-gate 	int32_t port;
80777c478bd9Sstevel@tonic-gate 
80787c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
80797c478bd9Sstevel@tonic-gate 
80807c478bd9Sstevel@tonic-gate 	/* Get which port to operate on.  */
80817c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
80827c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
80837c478bd9Sstevel@tonic-gate 		    "hubd_get_port_num: port lookup failed");
80847c478bd9Sstevel@tonic-gate 		port = 0;
80857c478bd9Sstevel@tonic-gate 	}
80867c478bd9Sstevel@tonic-gate 
80877c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8088112116d8Sfb 	    "hubd_get_port_num: hubd=0x%p, port=%d", (void *)hubd, port);
80897c478bd9Sstevel@tonic-gate 
80907c478bd9Sstevel@tonic-gate 	return ((usb_port_t)port);
80917c478bd9Sstevel@tonic-gate }
80927c478bd9Sstevel@tonic-gate 
80937c478bd9Sstevel@tonic-gate 
80947c478bd9Sstevel@tonic-gate /* check if child still exists */
80957c478bd9Sstevel@tonic-gate static dev_info_t *
80967c478bd9Sstevel@tonic-gate hubd_get_child_dip(hubd_t *hubd, usb_port_t port)
80977c478bd9Sstevel@tonic-gate {
80987c478bd9Sstevel@tonic-gate 	dev_info_t *child_dip = hubd->h_children_dips[port];
80997c478bd9Sstevel@tonic-gate 
81007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8101112116d8Sfb 	    "hubd_get_child_dip: hubd=0x%p, port=%d", (void *)hubd, port);
81027c478bd9Sstevel@tonic-gate 
81037c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
81047c478bd9Sstevel@tonic-gate 
81057c478bd9Sstevel@tonic-gate 	return (child_dip);
81067c478bd9Sstevel@tonic-gate }
81077c478bd9Sstevel@tonic-gate 
81087c478bd9Sstevel@tonic-gate 
81097c478bd9Sstevel@tonic-gate /*
81107c478bd9Sstevel@tonic-gate  * hubd_cfgadm_state:
81117c478bd9Sstevel@tonic-gate  *
81127c478bd9Sstevel@tonic-gate  *	child_dip list		port_state		cfgadm_state
81137c478bd9Sstevel@tonic-gate  *	--------------		----------		------------
81147c478bd9Sstevel@tonic-gate  *	!= NULL			connected		configured or
81157c478bd9Sstevel@tonic-gate  *							unconfigured
81167c478bd9Sstevel@tonic-gate  *	!= NULL			not connected		disconnect but
81177c478bd9Sstevel@tonic-gate  *							busy/still referenced
81187c478bd9Sstevel@tonic-gate  *	NULL			connected		logically disconnected
81197c478bd9Sstevel@tonic-gate  *	NULL			not connected		empty
81207c478bd9Sstevel@tonic-gate  */
81217c478bd9Sstevel@tonic-gate static uint_t
81227c478bd9Sstevel@tonic-gate hubd_cfgadm_state(hubd_t *hubd, usb_port_t port)
81237c478bd9Sstevel@tonic-gate {
81247c478bd9Sstevel@tonic-gate 	uint_t		state;
81257c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip = hubd_get_child_dip(hubd, port);
81267c478bd9Sstevel@tonic-gate 
81277c478bd9Sstevel@tonic-gate 	if (child_dip) {
81287c478bd9Sstevel@tonic-gate 		if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
81297c478bd9Sstevel@tonic-gate 			/*
81307c478bd9Sstevel@tonic-gate 			 * connected,  now check if driver exists
81317c478bd9Sstevel@tonic-gate 			 */
81327c478bd9Sstevel@tonic-gate 			if (DEVI_IS_DEVICE_OFFLINE(child_dip) ||
8133737d277aScth 			    !i_ddi_devi_attached(child_dip)) {
81347c478bd9Sstevel@tonic-gate 				state = HUBD_CFGADM_UNCONFIGURED;
81357c478bd9Sstevel@tonic-gate 			} else {
81367c478bd9Sstevel@tonic-gate 				state = HUBD_CFGADM_CONFIGURED;
81377c478bd9Sstevel@tonic-gate 			}
81387c478bd9Sstevel@tonic-gate 		} else {
81397c478bd9Sstevel@tonic-gate 			/*
81407c478bd9Sstevel@tonic-gate 			 * this means that the dip is around for
81417c478bd9Sstevel@tonic-gate 			 * a device that is still referenced but
81427c478bd9Sstevel@tonic-gate 			 * has been yanked out. So the cfgadm info
81437c478bd9Sstevel@tonic-gate 			 * for this state should be EMPTY (port empty)
81447c478bd9Sstevel@tonic-gate 			 * and CONFIGURED (dip still valid).
81457c478bd9Sstevel@tonic-gate 			 */
81467c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_STILL_REFERENCED;
81477c478bd9Sstevel@tonic-gate 		}
81487c478bd9Sstevel@tonic-gate 	} else {
81497c478bd9Sstevel@tonic-gate 		/* connected but no child dip */
81507c478bd9Sstevel@tonic-gate 		if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
81517c478bd9Sstevel@tonic-gate 			/* logically disconnected */
81527c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_DISCONNECTED;
81537c478bd9Sstevel@tonic-gate 		} else {
81547c478bd9Sstevel@tonic-gate 			/* physically disconnected */
81557c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_EMPTY;
81567c478bd9Sstevel@tonic-gate 		}
81577c478bd9Sstevel@tonic-gate 	}
81587c478bd9Sstevel@tonic-gate 
81597c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
81607c478bd9Sstevel@tonic-gate 	    "hubd_cfgadm_state: hubd=0x%p, port=%d state=0x%x",
8161112116d8Sfb 	    (void *)hubd, port, state);
81627c478bd9Sstevel@tonic-gate 
81637c478bd9Sstevel@tonic-gate 	return (state);
81647c478bd9Sstevel@tonic-gate }
81657c478bd9Sstevel@tonic-gate 
81667c478bd9Sstevel@tonic-gate 
81677c478bd9Sstevel@tonic-gate /*
81687c478bd9Sstevel@tonic-gate  * hubd_toggle_port:
81697c478bd9Sstevel@tonic-gate  */
81707c478bd9Sstevel@tonic-gate static int
81717c478bd9Sstevel@tonic-gate hubd_toggle_port(hubd_t *hubd, usb_port_t port)
81727c478bd9Sstevel@tonic-gate {
81737c478bd9Sstevel@tonic-gate 	usb_hub_descr_t	*hub_descr;
81747c478bd9Sstevel@tonic-gate 	int		wait;
81757c478bd9Sstevel@tonic-gate 	uint_t		retry;
81767c478bd9Sstevel@tonic-gate 	uint16_t	status;
81777c478bd9Sstevel@tonic-gate 	uint16_t	change;
81787c478bd9Sstevel@tonic-gate 
81797c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8180112116d8Sfb 	    "hubd_toggle_port: hubd=0x%p, port=%d", (void *)hubd, port);
81817c478bd9Sstevel@tonic-gate 
81827c478bd9Sstevel@tonic-gate 	if ((hubd_disable_port_power(hubd, port)) != USB_SUCCESS) {
81837c478bd9Sstevel@tonic-gate 
81847c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
81857c478bd9Sstevel@tonic-gate 	}
81867c478bd9Sstevel@tonic-gate 
81877c478bd9Sstevel@tonic-gate 	/*
81887c478bd9Sstevel@tonic-gate 	 * see hubd_enable_all_port_power() which
81897c478bd9Sstevel@tonic-gate 	 * requires longer delay for hubs.
81907c478bd9Sstevel@tonic-gate 	 */
81917c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
81927c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(hubd_device_delay / 10));
81937c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
81947c478bd9Sstevel@tonic-gate 
81957c478bd9Sstevel@tonic-gate 	hub_descr = &hubd->h_hub_descr;
81967c478bd9Sstevel@tonic-gate 
81977c478bd9Sstevel@tonic-gate 	/*
81987c478bd9Sstevel@tonic-gate 	 * According to section 11.11 of USB, for hubs with no power
81997c478bd9Sstevel@tonic-gate 	 * switches, bPwrOn2PwrGood is zero. But we wait for some
82007c478bd9Sstevel@tonic-gate 	 * arbitrary time to enable power to become stable.
82017c478bd9Sstevel@tonic-gate 	 *
82027c478bd9Sstevel@tonic-gate 	 * If an hub supports port power swicthing, we need to wait
82037c478bd9Sstevel@tonic-gate 	 * at least 20ms before accesing corresonding usb port.
82047c478bd9Sstevel@tonic-gate 	 */
82057c478bd9Sstevel@tonic-gate 	if ((hub_descr->wHubCharacteristics &
82067c478bd9Sstevel@tonic-gate 	    HUB_CHARS_NO_POWER_SWITCHING) || (!hub_descr->bPwrOn2PwrGood)) {
82077c478bd9Sstevel@tonic-gate 		wait = hubd_device_delay / 10;
82087c478bd9Sstevel@tonic-gate 	} else {
82097c478bd9Sstevel@tonic-gate 		wait = max(HUB_DEFAULT_POPG,
82107c478bd9Sstevel@tonic-gate 		    hub_descr->bPwrOn2PwrGood) * 2 * 1000;
82117c478bd9Sstevel@tonic-gate 	}
82127c478bd9Sstevel@tonic-gate 
82137c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
82147c478bd9Sstevel@tonic-gate 	    "hubd_toggle_port: popg=%d wait=%d",
82157c478bd9Sstevel@tonic-gate 	    hub_descr->bPwrOn2PwrGood, wait);
82167c478bd9Sstevel@tonic-gate 
82177c478bd9Sstevel@tonic-gate 	retry = 0;
82187c478bd9Sstevel@tonic-gate 
82197c478bd9Sstevel@tonic-gate 	do {
82207c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port_power(hubd, port);
82217c478bd9Sstevel@tonic-gate 
82227c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
82237c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(wait));
82247c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
82257c478bd9Sstevel@tonic-gate 
82267c478bd9Sstevel@tonic-gate 		/* Get port status */
82277c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd, port,
82287c478bd9Sstevel@tonic-gate 		    &status, &change, 0);
82297c478bd9Sstevel@tonic-gate 
82307c478bd9Sstevel@tonic-gate 		/* For retry if any, use some extra delay */
82317c478bd9Sstevel@tonic-gate 		wait = max(wait, hubd_device_delay / 10);
82327c478bd9Sstevel@tonic-gate 
82337c478bd9Sstevel@tonic-gate 		retry++;
82347c478bd9Sstevel@tonic-gate 
82357c478bd9Sstevel@tonic-gate 	} while ((!(status & PORT_STATUS_PPS)) && (retry < HUBD_PORT_RETRY));
82367c478bd9Sstevel@tonic-gate 
82377c478bd9Sstevel@tonic-gate 	/* Print warning message if port has no power */
82387c478bd9Sstevel@tonic-gate 	if (!(status & PORT_STATUS_PPS)) {
82397c478bd9Sstevel@tonic-gate 
8240d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
82417c478bd9Sstevel@tonic-gate 		    "hubd_toggle_port: port %d power-on failed, "
82427c478bd9Sstevel@tonic-gate 		    "port status 0x%x", port, status);
82437c478bd9Sstevel@tonic-gate 
82447c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
82457c478bd9Sstevel@tonic-gate 	}
82467c478bd9Sstevel@tonic-gate 
82477c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
82487c478bd9Sstevel@tonic-gate }
824935f36846Ssl 
825035f36846Ssl 
825135f36846Ssl /*
825235f36846Ssl  * hubd_init_power_budget:
825335f36846Ssl  *	Init power budget variables in hubd structure. According
825435f36846Ssl  *	to USB spec, the power budget rules are:
825535f36846Ssl  *	1. local-powered hubs including root-hubs can supply
825635f36846Ssl  *	   500mA to each port at maximum
825735f36846Ssl  *	2. two bus-powered hubs are not allowed to concatenate
825835f36846Ssl  *	3. bus-powered hubs can supply 100mA to each port at
825935f36846Ssl  *	   maximum, and the power consumed by all downstream
826035f36846Ssl  *	   ports and the hub itself cannot exceed the max power
826135f36846Ssl  *	   supplied by the upstream port, i.e., 500mA
826235f36846Ssl  *	The routine is only called during hub attach time
826335f36846Ssl  */
826435f36846Ssl static int
826535f36846Ssl hubd_init_power_budget(hubd_t *hubd)
826635f36846Ssl {
826735f36846Ssl 	uint16_t	status = 0;
826835f36846Ssl 	usba_device_t	*hubd_ud = NULL;
826935f36846Ssl 	size_t		size;
827035f36846Ssl 	usb_cfg_descr_t	cfg_descr;
827135f36846Ssl 	dev_info_t	*pdip = NULL;
827235f36846Ssl 	hubd_t		*phubd = NULL;
827335f36846Ssl 
827435f36846Ssl 	if (hubd->h_ignore_pwr_budget) {
827535f36846Ssl 
827635f36846Ssl 		return (USB_SUCCESS);
827735f36846Ssl 	}
827835f36846Ssl 
827935f36846Ssl 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
828035f36846Ssl 	    "hubd_init_power_budget:");
828135f36846Ssl 
828235f36846Ssl 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
828335f36846Ssl 	ASSERT(hubd->h_default_pipe != 0);
828435f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
828535f36846Ssl 
828635f36846Ssl 	/* get device status */
828735f36846Ssl 	if ((usb_get_status(hubd->h_dip, hubd->h_default_pipe,
828835f36846Ssl 	    HUB_GET_DEVICE_STATUS_TYPE,
828935f36846Ssl 	    0, &status, 0)) != USB_SUCCESS) {
829035f36846Ssl 		mutex_enter(HUBD_MUTEX(hubd));
829135f36846Ssl 
829235f36846Ssl 		return (USB_FAILURE);
829335f36846Ssl 	}
829435f36846Ssl 
829535f36846Ssl 	hubd_ud = usba_get_usba_device(hubd->h_dip);
829635f36846Ssl 
829735f36846Ssl 	size = usb_parse_cfg_descr(hubd_ud->usb_cfg, hubd_ud->usb_cfg_length,
829835f36846Ssl 	    &cfg_descr, USB_CFG_DESCR_SIZE);
829935f36846Ssl 
830035f36846Ssl 	if (size != USB_CFG_DESCR_SIZE) {
830135f36846Ssl 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
830235f36846Ssl 		    "get hub configuration descriptor failed");
830335f36846Ssl 		mutex_enter(HUBD_MUTEX(hubd));
830435f36846Ssl 
830535f36846Ssl 		return (USB_FAILURE);
830635f36846Ssl 	}
830735f36846Ssl 
830835f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
830935f36846Ssl 
831035f36846Ssl 	hubd->h_local_pwr_capable = (cfg_descr.bmAttributes &
831135f36846Ssl 	    USB_CFG_ATTR_SELFPWR);
831235f36846Ssl 
831335f36846Ssl 	if (hubd->h_local_pwr_capable) {
831435f36846Ssl 		USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
831535f36846Ssl 		    "hub is capable of local power");
831635f36846Ssl 	}
831735f36846Ssl 
831835f36846Ssl 	hubd->h_local_pwr_on = (status &
831935f36846Ssl 	    USB_DEV_SLF_PWRD_STATUS) && hubd->h_local_pwr_capable;
832035f36846Ssl 
832135f36846Ssl 	if (hubd->h_local_pwr_on) {
832235f36846Ssl 		USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
832335f36846Ssl 		    "hub is local-powered");
832435f36846Ssl 
832535f36846Ssl 		hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
832635f36846Ssl 		    USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
832735f36846Ssl 	} else {
832835f36846Ssl 		hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
832935f36846Ssl 		    USB_LOW_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
833035f36846Ssl 
833135f36846Ssl 		hubd->h_pwr_left = (USB_PWR_UNIT_LOAD *
833235f36846Ssl 		    USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
833335f36846Ssl 
833435f36846Ssl 		ASSERT(!usba_is_root_hub(hubd->h_dip));
833535f36846Ssl 
833635f36846Ssl 		if (!usba_is_root_hub(hubd->h_dip)) {
833735f36846Ssl 			/*
833835f36846Ssl 			 * two bus-powered hubs are not
833935f36846Ssl 			 * allowed to be concatenated
834035f36846Ssl 			 */
834135f36846Ssl 			mutex_exit(HUBD_MUTEX(hubd));
834235f36846Ssl 
834335f36846Ssl 			pdip = ddi_get_parent(hubd->h_dip);
834435f36846Ssl 			phubd = hubd_get_soft_state(pdip);
834535f36846Ssl 			ASSERT(phubd != NULL);
834635f36846Ssl 
834735f36846Ssl 			if (!phubd->h_ignore_pwr_budget) {
834835f36846Ssl 				mutex_enter(HUBD_MUTEX(phubd));
834935f36846Ssl 				if (phubd->h_local_pwr_on == B_FALSE) {
83508668df41Slg 					USB_DPRINTF_L1(DPRINT_MASK_HUB,
835135f36846Ssl 					    hubd->h_log_handle,
835235f36846Ssl 					    "two bus-powered hubs cannot "
835335f36846Ssl 					    "be concatenated");
835435f36846Ssl 
835535f36846Ssl 					mutex_exit(HUBD_MUTEX(phubd));
835635f36846Ssl 					mutex_enter(HUBD_MUTEX(hubd));
835735f36846Ssl 
835835f36846Ssl 					return (USB_FAILURE);
835935f36846Ssl 				}
836035f36846Ssl 				mutex_exit(HUBD_MUTEX(phubd));
836135f36846Ssl 			}
836235f36846Ssl 
836335f36846Ssl 			mutex_enter(HUBD_MUTEX(hubd));
836435f36846Ssl 
836535f36846Ssl 			USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
836635f36846Ssl 			    "hub is bus-powered");
836735f36846Ssl 		} else {
836835f36846Ssl 			USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
836935f36846Ssl 			    "root-hub must be local-powered");
837035f36846Ssl 		}
837135f36846Ssl 
837235f36846Ssl 		/*
837335f36846Ssl 		 * Subtract the power consumed by the hub itself
837435f36846Ssl 		 * and get the power that can be supplied to
837535f36846Ssl 		 * downstream ports
837635f36846Ssl 		 */
837735f36846Ssl 		hubd->h_pwr_left -=
837835f36846Ssl 		    hubd->h_hub_descr.bHubContrCurrent /
837935f36846Ssl 		    USB_CFG_DESCR_PWR_UNIT;
838035f36846Ssl 		if (hubd->h_pwr_left < 0) {
838135f36846Ssl 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
838235f36846Ssl 			    "hubd->h_pwr_left is less than bHubContrCurrent, "
838335f36846Ssl 			    "should fail");
838435f36846Ssl 
838535f36846Ssl 			return (USB_FAILURE);
838635f36846Ssl 		}
838735f36846Ssl 	}
838835f36846Ssl 
838935f36846Ssl 	return (USB_SUCCESS);
839035f36846Ssl }
839135f36846Ssl 
839235f36846Ssl 
839335f36846Ssl /*
839435f36846Ssl  * usba_hubdi_check_power_budget:
839535f36846Ssl  *	Check if the hub has enough power budget to allow a
839635f36846Ssl  *	child device to select a configuration of config_index.
839735f36846Ssl  */
839835f36846Ssl int
839935f36846Ssl usba_hubdi_check_power_budget(dev_info_t *dip, usba_device_t *child_ud,
840035f36846Ssl 	uint_t config_index)
840135f36846Ssl {
840235f36846Ssl 	int16_t		pwr_left, pwr_limit, pwr_required;
840335f36846Ssl 	size_t		size;
840435f36846Ssl 	usb_cfg_descr_t cfg_descr;
840535f36846Ssl 	hubd_t		*hubd;
840635f36846Ssl 
840735f36846Ssl 	if ((hubd = hubd_get_soft_state(dip)) == NULL) {
840835f36846Ssl 
840935f36846Ssl 		return (USB_FAILURE);
841035f36846Ssl 	}
841135f36846Ssl 
841235f36846Ssl 	if (hubd->h_ignore_pwr_budget) {
841335f36846Ssl 
841435f36846Ssl 		return (USB_SUCCESS);
841535f36846Ssl 	}
841635f36846Ssl 
841735f36846Ssl 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
841835f36846Ssl 	    "usba_hubdi_check_power_budget: "
8419112116d8Sfb 	    "dip=0x%p child_ud=0x%p conf_index=%d", (void *)dip,
8420112116d8Sfb 	    (void *)child_ud, config_index);
842135f36846Ssl 
842235f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
842335f36846Ssl 	pwr_limit = hubd->h_pwr_limit;
842435f36846Ssl 	if (hubd->h_local_pwr_on == B_FALSE) {
842535f36846Ssl 		pwr_left = hubd->h_pwr_left;
842635f36846Ssl 		pwr_limit = (pwr_limit <= pwr_left) ? pwr_limit : pwr_left;
842735f36846Ssl 	}
842835f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
842935f36846Ssl 
843035f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
843135f36846Ssl 	    "usba_hubdi_check_power_budget: "
843235f36846Ssl 	    "available power is %dmA", pwr_limit * USB_CFG_DESCR_PWR_UNIT);
843335f36846Ssl 
843435f36846Ssl 	size = usb_parse_cfg_descr(
843535f36846Ssl 	    child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
843635f36846Ssl 	    &cfg_descr, USB_CFG_DESCR_SIZE);
843735f36846Ssl 
843835f36846Ssl 	if (size != USB_CFG_DESCR_SIZE) {
843935f36846Ssl 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
844035f36846Ssl 		    "get hub configuration descriptor failed");
844135f36846Ssl 
844235f36846Ssl 		return (USB_FAILURE);
844335f36846Ssl 	}
844435f36846Ssl 
844535f36846Ssl 	pwr_required = cfg_descr.bMaxPower;
844635f36846Ssl 
844735f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
844835f36846Ssl 	    "usba_hubdi_check_power_budget: "
844935f36846Ssl 	    "child bmAttributes=0x%x bMaxPower=%d "
845035f36846Ssl 	    "with config_index=%d", cfg_descr.bmAttributes,
845135f36846Ssl 	    pwr_required, config_index);
845235f36846Ssl 
845335f36846Ssl 	if (pwr_required > pwr_limit) {
84548668df41Slg 		USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
845535f36846Ssl 		    "configuration %d for device %s %s at port %d "
845635f36846Ssl 		    "exceeds power available for this port, please "
845735f36846Ssl 		    "re-insert your device into another hub port which "
845835f36846Ssl 		    "has enough power",
845935f36846Ssl 		    config_index,
846035f36846Ssl 		    child_ud->usb_mfg_str,
846135f36846Ssl 		    child_ud->usb_product_str,
846235f36846Ssl 		    child_ud->usb_port);
846335f36846Ssl 
846435f36846Ssl 		return (USB_FAILURE);
846535f36846Ssl 	}
846635f36846Ssl 
846735f36846Ssl 	return (USB_SUCCESS);
846835f36846Ssl }
846935f36846Ssl 
847035f36846Ssl 
847135f36846Ssl /*
847235f36846Ssl  * usba_hubdi_incr_power_budget:
847335f36846Ssl  *	Increase the hub power budget value when a child device
847435f36846Ssl  *	is removed from a bus-powered hub port.
847535f36846Ssl  */
847635f36846Ssl void
847735f36846Ssl usba_hubdi_incr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
847835f36846Ssl {
847935f36846Ssl 	uint16_t	pwr_value;
848035f36846Ssl 	hubd_t		*hubd = hubd_get_soft_state(dip);
848135f36846Ssl 
848235f36846Ssl 	ASSERT(hubd != NULL);
848335f36846Ssl 
848435f36846Ssl 	if (hubd->h_ignore_pwr_budget) {
848535f36846Ssl 
848635f36846Ssl 		return;
848735f36846Ssl 	}
848835f36846Ssl 
848935f36846Ssl 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
849035f36846Ssl 	    "usba_hubdi_incr_power_budget: "
8491112116d8Sfb 	    "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
849235f36846Ssl 
849335f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
849435f36846Ssl 	if (hubd->h_local_pwr_on == B_TRUE) {
849535f36846Ssl 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
849635f36846Ssl 		    "usba_hubdi_incr_power_budget: "
849735f36846Ssl 		    "hub is local powered");
849835f36846Ssl 		mutex_exit(HUBD_MUTEX(hubd));
849935f36846Ssl 
850035f36846Ssl 		return;
850135f36846Ssl 	}
850235f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
850335f36846Ssl 
850435f36846Ssl 	mutex_enter(&child_ud->usb_mutex);
850535f36846Ssl 	if (child_ud->usb_pwr_from_hub == 0) {
850635f36846Ssl 		mutex_exit(&child_ud->usb_mutex);
850735f36846Ssl 
850835f36846Ssl 		return;
850935f36846Ssl 	}
851035f36846Ssl 	pwr_value = child_ud->usb_pwr_from_hub;
851135f36846Ssl 	mutex_exit(&child_ud->usb_mutex);
851235f36846Ssl 
851335f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
851435f36846Ssl 	hubd->h_pwr_left += pwr_value;
851535f36846Ssl 
851635f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
851735f36846Ssl 	    "usba_hubdi_incr_power_budget: "
851835f36846Ssl 	    "available power is %dmA, increased by %dmA",
851935f36846Ssl 	    hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
852035f36846Ssl 	    pwr_value * USB_CFG_DESCR_PWR_UNIT);
852135f36846Ssl 
852235f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
852335f36846Ssl 
852435f36846Ssl 	mutex_enter(&child_ud->usb_mutex);
852535f36846Ssl 	child_ud->usb_pwr_from_hub = 0;
852635f36846Ssl 	mutex_exit(&child_ud->usb_mutex);
852735f36846Ssl }
852835f36846Ssl 
852935f36846Ssl 
853035f36846Ssl /*
853135f36846Ssl  * usba_hubdi_decr_power_budget:
853235f36846Ssl  *	Decrease the hub power budget value when a child device
853335f36846Ssl  *	is inserted to a bus-powered hub port.
853435f36846Ssl  */
853535f36846Ssl void
853635f36846Ssl usba_hubdi_decr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
853735f36846Ssl {
853835f36846Ssl 	uint16_t	pwr_value;
853935f36846Ssl 	size_t		size;
854035f36846Ssl 	usb_cfg_descr_t	cfg_descr;
854135f36846Ssl 	hubd_t		*hubd = hubd_get_soft_state(dip);
854235f36846Ssl 
854335f36846Ssl 	ASSERT(hubd != NULL);
854435f36846Ssl 
854535f36846Ssl 	if (hubd->h_ignore_pwr_budget) {
854635f36846Ssl 
854735f36846Ssl 		return;
854835f36846Ssl 	}
854935f36846Ssl 
855035f36846Ssl 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
855135f36846Ssl 	    "usba_hubdi_decr_power_budget: "
8552112116d8Sfb 	    "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
855335f36846Ssl 
855435f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
855535f36846Ssl 	if (hubd->h_local_pwr_on == B_TRUE) {
855635f36846Ssl 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
855735f36846Ssl 		    "usba_hubdi_decr_power_budget: "
855835f36846Ssl 		    "hub is local powered");
855935f36846Ssl 		mutex_exit(HUBD_MUTEX(hubd));
856035f36846Ssl 
856135f36846Ssl 		return;
856235f36846Ssl 	}
856335f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
856435f36846Ssl 
856535f36846Ssl 	mutex_enter(&child_ud->usb_mutex);
856635f36846Ssl 	if (child_ud->usb_pwr_from_hub > 0) {
856735f36846Ssl 		mutex_exit(&child_ud->usb_mutex);
856835f36846Ssl 
856935f36846Ssl 		return;
857035f36846Ssl 	}
857135f36846Ssl 	mutex_exit(&child_ud->usb_mutex);
857235f36846Ssl 
857335f36846Ssl 	size = usb_parse_cfg_descr(
857435f36846Ssl 	    child_ud->usb_cfg, child_ud->usb_cfg_length,
857535f36846Ssl 	    &cfg_descr, USB_CFG_DESCR_SIZE);
857635f36846Ssl 	ASSERT(size == USB_CFG_DESCR_SIZE);
857735f36846Ssl 
857835f36846Ssl 	mutex_enter(HUBD_MUTEX(hubd));
857935f36846Ssl 	pwr_value = cfg_descr.bMaxPower;
858035f36846Ssl 	hubd->h_pwr_left -= pwr_value;
858135f36846Ssl 	ASSERT(hubd->h_pwr_left >= 0);
858235f36846Ssl 
858335f36846Ssl 	USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
858435f36846Ssl 	    "usba_hubdi_decr_power_budget: "
858535f36846Ssl 	    "available power is %dmA, decreased by %dmA",
858635f36846Ssl 	    hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
858735f36846Ssl 	    pwr_value * USB_CFG_DESCR_PWR_UNIT);
858835f36846Ssl 
858935f36846Ssl 	mutex_exit(HUBD_MUTEX(hubd));
859035f36846Ssl 
859135f36846Ssl 	mutex_enter(&child_ud->usb_mutex);
859235f36846Ssl 	child_ud->usb_pwr_from_hub = pwr_value;
859335f36846Ssl 	mutex_exit(&child_ud->usb_mutex);
859435f36846Ssl }
8595ffcd51f3Slg 
8596ffcd51f3Slg /*
8597ffcd51f3Slg  * hubd_wait_for_hotplug_exit:
8598112cd14aSqz  *	Waiting for the exit of the running hotplug thread or ioctl thread.
8599ffcd51f3Slg  */
8600ffcd51f3Slg static int
8601ffcd51f3Slg hubd_wait_for_hotplug_exit(hubd_t *hubd)
8602ffcd51f3Slg {
8603d3d50737SRafael Vanoni 	clock_t		until = drv_usectohz(1000000);
8604ffcd51f3Slg 	int		rval;
8605ffcd51f3Slg 
8606ffcd51f3Slg 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8607ffcd51f3Slg 
8608ffcd51f3Slg 	if (hubd->h_hotplug_thread) {
8609ffcd51f3Slg 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8610ffcd51f3Slg 		    "waiting for hubd hotplug thread exit");
8611d3d50737SRafael Vanoni 		rval = cv_reltimedwait(&hubd->h_cv_hotplug_dev,
8612d3d50737SRafael Vanoni 		    &hubd->h_mutex, until, TR_CLOCK_TICK);
8613ffcd51f3Slg 
8614ffcd51f3Slg 		if ((rval <= 0) && (hubd->h_hotplug_thread)) {
8615ffcd51f3Slg 
8616ffcd51f3Slg 			return (USB_FAILURE);
8617ffcd51f3Slg 		}
8618ffcd51f3Slg 	}
8619ffcd51f3Slg 
8620ffcd51f3Slg 	return (USB_SUCCESS);
8621ffcd51f3Slg }
8622ffcd51f3Slg 
8623ffcd51f3Slg /*
8624ffcd51f3Slg  * hubd_reset_thread:
8625ffcd51f3Slg  *	handles the "USB_RESET_LVL_REATTACH" reset of usb device.
8626ffcd51f3Slg  *
8627ffcd51f3Slg  *	- delete the child (force detaching the device and its children)
8628ffcd51f3Slg  *	- reset the corresponding parent hub port
8629ffcd51f3Slg  *	- create the child (force re-attaching the device and its children)
8630ffcd51f3Slg  */
8631ffcd51f3Slg static void
8632ffcd51f3Slg hubd_reset_thread(void *arg)
8633ffcd51f3Slg {
8634ffcd51f3Slg 	hubd_reset_arg_t *hd_arg = (hubd_reset_arg_t *)arg;
8635ffcd51f3Slg 	hubd_t		*hubd = hd_arg->hubd;
8636ffcd51f3Slg 	uint16_t	reset_port = hd_arg->reset_port;
8637ffcd51f3Slg 	uint16_t	status, change;
8638ffcd51f3Slg 	hub_power_t	*hubpm;
8639ffcd51f3Slg 	dev_info_t	*hdip = hubd->h_dip;
8640ffcd51f3Slg 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
8641ffcd51f3Slg 	dev_info_t	*child_dip;
8642ffcd51f3Slg 	boolean_t	online_child = B_FALSE;
8643ffcd51f3Slg 	int		prh_circ, rh_circ, circ, devinst;
8644ffcd51f3Slg 	char		*devname;
8645aa041649SRaymond Chen 	int		i = 0;
8646aa041649SRaymond Chen 	int		rval = USB_FAILURE;
8647ffcd51f3Slg 
8648ffcd51f3Slg 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8649ffcd51f3Slg 	    "hubd_reset_thread:  started, hubd_reset_port = 0x%x", reset_port);
8650ffcd51f3Slg 
8651ffcd51f3Slg 	kmem_free(arg, sizeof (hubd_reset_arg_t));
8652ffcd51f3Slg 
8653ffcd51f3Slg 	mutex_enter(HUBD_MUTEX(hubd));
8654ffcd51f3Slg 
8655ffcd51f3Slg 	child_dip = hubd->h_children_dips[reset_port];
8656ffcd51f3Slg 	ASSERT(child_dip != NULL);
8657ffcd51f3Slg 
8658ffcd51f3Slg 	devname = (char *)ddi_driver_name(child_dip);
8659ffcd51f3Slg 	devinst = ddi_get_instance(child_dip);
8660ffcd51f3Slg 
8661ffcd51f3Slg 	/* if our bus power entry point is active, quit the reset */
8662ffcd51f3Slg 	if (hubd->h_bus_pwr) {
8663ffcd51f3Slg 		USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8664ffcd51f3Slg 		    "%s%d is under bus power management, cannot be reset. "
8665ffcd51f3Slg 		    "Please disconnect and reconnect this device.",
8666ffcd51f3Slg 		    devname, devinst);
8667ffcd51f3Slg 
8668ffcd51f3Slg 		goto Fail;
8669ffcd51f3Slg 	}
8670ffcd51f3Slg 
8671ffcd51f3Slg 	if (hubd_wait_for_hotplug_exit(hubd) == USB_FAILURE) {
8672ffcd51f3Slg 		/* we got woken up because of a timeout */
8673ffcd51f3Slg 		USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
8674ffcd51f3Slg 		    hubd->h_log_handle, "Time out when resetting the device"
8675ffcd51f3Slg 		    " %s%d. Please disconnect and reconnect this device.",
8676ffcd51f3Slg 		    devname, devinst);
8677ffcd51f3Slg 
8678ffcd51f3Slg 		goto Fail;
8679ffcd51f3Slg 	}
8680ffcd51f3Slg 
8681ffcd51f3Slg 	hubd->h_hotplug_thread++;
8682ffcd51f3Slg 
8683ffcd51f3Slg 	/* is this the root hub? */
8684ffcd51f3Slg 	if ((hdip == rh_dip) &&
8685ffcd51f3Slg 	    (hubd->h_dev_state == USB_DEV_PWRED_DOWN)) {
8686ffcd51f3Slg 		hubpm = hubd->h_hubpm;
8687ffcd51f3Slg 
8688ffcd51f3Slg 		/* mark the root hub as full power */
8689ffcd51f3Slg 		hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
8690ffcd51f3Slg 		hubpm->hubp_time_at_full_power = ddi_get_time();
8691ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
8692ffcd51f3Slg 
8693ffcd51f3Slg 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8694ffcd51f3Slg 		    "hubd_reset_thread: call pm_power_has_changed");
8695ffcd51f3Slg 
8696ffcd51f3Slg 		(void) pm_power_has_changed(hdip, 0,
8697ffcd51f3Slg 		    USB_DEV_OS_FULL_PWR);
8698ffcd51f3Slg 
8699ffcd51f3Slg 		mutex_enter(HUBD_MUTEX(hubd));
8700ffcd51f3Slg 		hubd->h_dev_state = USB_DEV_ONLINE;
8701ffcd51f3Slg 	}
8702ffcd51f3Slg 
8703ffcd51f3Slg 	mutex_exit(HUBD_MUTEX(hubd));
8704ffcd51f3Slg 
8705ffcd51f3Slg 	/*
8706ffcd51f3Slg 	 * this ensures one reset activity per system at a time.
8707ffcd51f3Slg 	 * we enter the parent PCI node to have this serialization.
8708ffcd51f3Slg 	 * this also excludes ioctls and deathrow thread
8709ffcd51f3Slg 	 */
8710ffcd51f3Slg 	ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
8711ffcd51f3Slg 	ndi_devi_enter(rh_dip, &rh_circ);
8712ffcd51f3Slg 
8713ffcd51f3Slg 	/* exclude other threads */
8714ffcd51f3Slg 	ndi_devi_enter(hdip, &circ);
8715ffcd51f3Slg 	mutex_enter(HUBD_MUTEX(hubd));
8716ffcd51f3Slg 
8717ffcd51f3Slg 	/*
8718ffcd51f3Slg 	 * We need to make sure that the child is still online for a hotplug
8719ffcd51f3Slg 	 * thread could have inserted which detached the child.
8720ffcd51f3Slg 	 */
8721ffcd51f3Slg 	if (hubd->h_children_dips[reset_port]) {
8722ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
8723ffcd51f3Slg 		/* First disconnect the device */
8724ffcd51f3Slg 		hubd_post_event(hubd, reset_port, USBA_EVENT_TAG_HOT_REMOVAL);
8725aa041649SRaymond Chen 
8726aa041649SRaymond Chen 		/* delete cached dv_node's but drop locks first */
8727aa041649SRaymond Chen 		ndi_devi_exit(hdip, circ);
8728aa041649SRaymond Chen 		ndi_devi_exit(rh_dip, rh_circ);
8729aa041649SRaymond Chen 		ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
8730aa041649SRaymond Chen 
8731aa041649SRaymond Chen 		(void) devfs_clean(rh_dip, NULL, DV_CLEAN_FORCE);
8732aa041649SRaymond Chen 
8733aa041649SRaymond Chen 		/*
8734aa041649SRaymond Chen 		 * workaround only for storage device. When it's able to force
8735aa041649SRaymond Chen 		 * detach a driver, this code can be removed safely.
8736aa041649SRaymond Chen 		 *
8737aa041649SRaymond Chen 		 * If we're to reset storage device and the device is used, we
8738aa041649SRaymond Chen 		 * will wait at most extra 20s for applications to exit and
8739aa041649SRaymond Chen 		 * close the device. This is especially useful for HAL-based
8740aa041649SRaymond Chen 		 * applications.
8741aa041649SRaymond Chen 		 */
8742aa041649SRaymond Chen 		if ((strcmp(devname, "scsa2usb") == 0) &&
8743aa041649SRaymond Chen 		    DEVI(child_dip)->devi_ref != 0) {
8744aa041649SRaymond Chen 			while (i++ < hubdi_reset_delay) {
8745aa041649SRaymond Chen 				mutex_enter(HUBD_MUTEX(hubd));
8746aa041649SRaymond Chen 				rval = hubd_delete_child(hubd, reset_port,
8747aa041649SRaymond Chen 				    NDI_DEVI_REMOVE, B_FALSE);
8748aa041649SRaymond Chen 				mutex_exit(HUBD_MUTEX(hubd));
8749aa041649SRaymond Chen 				if (rval == USB_SUCCESS)
8750aa041649SRaymond Chen 					break;
8751aa041649SRaymond Chen 
8752aa041649SRaymond Chen 				delay(drv_usectohz(1000000)); /* 1s */
8753aa041649SRaymond Chen 			}
8754aa041649SRaymond Chen 		}
8755aa041649SRaymond Chen 
8756aa041649SRaymond Chen 		ndi_devi_enter(ddi_get_parent(rh_dip), &prh_circ);
8757aa041649SRaymond Chen 		ndi_devi_enter(rh_dip, &rh_circ);
8758aa041649SRaymond Chen 		ndi_devi_enter(hdip, &circ);
8759aa041649SRaymond Chen 
8760ffcd51f3Slg 		mutex_enter(HUBD_MUTEX(hubd));
8761ffcd51f3Slg 
8762ffcd51f3Slg 		/* Then force detaching the device */
8763aa041649SRaymond Chen 		if ((rval != USB_SUCCESS) && (hubd_delete_child(hubd,
8764aa041649SRaymond Chen 		    reset_port, NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS)) {
8765ffcd51f3Slg 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8766ffcd51f3Slg 			    "%s%d cannot be reset due to other applications "
8767ffcd51f3Slg 			    "are using it, please first close these "
8768ffcd51f3Slg 			    "applications, then disconnect and reconnect"
8769ffcd51f3Slg 			    "the device.", devname, devinst);
8770ffcd51f3Slg 
8771ffcd51f3Slg 			mutex_exit(HUBD_MUTEX(hubd));
8772ffcd51f3Slg 			/* post a re-connect event */
8773ffcd51f3Slg 			hubd_post_event(hubd, reset_port,
8774ffcd51f3Slg 			    USBA_EVENT_TAG_HOT_INSERTION);
8775ffcd51f3Slg 			mutex_enter(HUBD_MUTEX(hubd));
8776ffcd51f3Slg 		} else {
8777ffcd51f3Slg 			(void) hubd_determine_port_status(hubd, reset_port,
8778ffcd51f3Slg 			    &status, &change, HUBD_ACK_ALL_CHANGES);
8779ffcd51f3Slg 
8780ffcd51f3Slg 			/* Reset the parent hubd port and create new child */
8781ffcd51f3Slg 			if (status & PORT_STATUS_CCS) {
8782ffcd51f3Slg 				online_child |=	(hubd_handle_port_connect(hubd,
8783ffcd51f3Slg 				    reset_port) == USB_SUCCESS);
8784ffcd51f3Slg 			}
8785ffcd51f3Slg 		}
8786ffcd51f3Slg 	}
8787ffcd51f3Slg 
8788ffcd51f3Slg 	/* release locks so we can do a devfs_clean */
8789ffcd51f3Slg 	mutex_exit(HUBD_MUTEX(hubd));
8790ffcd51f3Slg 
8791ffcd51f3Slg 	/* delete cached dv_node's but drop locks first */
8792ffcd51f3Slg 	ndi_devi_exit(hdip, circ);
8793ffcd51f3Slg 	ndi_devi_exit(rh_dip, rh_circ);
8794ffcd51f3Slg 	ndi_devi_exit(ddi_get_parent(rh_dip), prh_circ);
8795ffcd51f3Slg 
8796ffcd51f3Slg 	(void) devfs_clean(rh_dip, NULL, 0);
8797ffcd51f3Slg 
8798ffcd51f3Slg 	/* now check if any children need onlining */
8799ffcd51f3Slg 	if (online_child) {
8800ffcd51f3Slg 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8801ffcd51f3Slg 		    "hubd_reset_thread: onlining children");
8802ffcd51f3Slg 
8803ffcd51f3Slg 		(void) ndi_devi_online(hubd->h_dip, 0);
8804ffcd51f3Slg 	}
8805ffcd51f3Slg 
8806ffcd51f3Slg 	mutex_enter(HUBD_MUTEX(hubd));
8807ffcd51f3Slg 
8808ffcd51f3Slg 	/* allow hotplug thread now */
8809ffcd51f3Slg 	hubd->h_hotplug_thread--;
8810ffcd51f3Slg Fail:
8811ffcd51f3Slg 	hubd_start_polling(hubd, 0);
8812ffcd51f3Slg 
8813ffcd51f3Slg 	/* mark this device as idle */
8814ffcd51f3Slg 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
8815ffcd51f3Slg 
8816ffcd51f3Slg 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8817ffcd51f3Slg 	    "hubd_reset_thread: exit, %d", hubd->h_hotplug_thread);
8818ffcd51f3Slg 
8819ffcd51f3Slg 	hubd->h_reset_port[reset_port] = B_FALSE;
8820ffcd51f3Slg 
8821ffcd51f3Slg 	mutex_exit(HUBD_MUTEX(hubd));
8822ffcd51f3Slg 
8823ffcd51f3Slg 	ndi_rele_devi(hdip);
8824ffcd51f3Slg }
8825ffcd51f3Slg 
8826ffcd51f3Slg /*
8827ffcd51f3Slg  * hubd_check_same_device:
8828112cd14aSqz  *	- open the default pipe of the device.
8829112cd14aSqz  *	- compare the old and new descriptors of the device.
8830112cd14aSqz  *	- close the default pipe.
8831ffcd51f3Slg  */
8832ffcd51f3Slg static int
8833ffcd51f3Slg hubd_check_same_device(hubd_t *hubd, usb_port_t port)
8834ffcd51f3Slg {
8835112cd14aSqz 	dev_info_t		*dip = hubd->h_children_dips[port];
8836ffcd51f3Slg 	usb_pipe_handle_t	ph;
8837112cd14aSqz 	int			rval = USB_FAILURE;
8838ffcd51f3Slg 
8839ffcd51f3Slg 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8840ffcd51f3Slg 
8841ffcd51f3Slg 	mutex_exit(HUBD_MUTEX(hubd));
8842ffcd51f3Slg 	/* Open the default pipe to operate the device */
8843ffcd51f3Slg 	if (usb_pipe_open(dip, NULL, NULL,
8844ffcd51f3Slg 	    USB_FLAGS_SLEEP| USBA_FLAGS_PRIVILEGED,
8845ffcd51f3Slg 	    &ph) == USB_SUCCESS) {
8846ffcd51f3Slg 		/*
8847ffcd51f3Slg 		 * Check that if the device's descriptors are different
8848ffcd51f3Slg 		 * from the values saved before the port reset.
8849ffcd51f3Slg 		 */
8850ffcd51f3Slg 		rval = usb_check_same_device(dip,
8851ffcd51f3Slg 		    hubd->h_log_handle, USB_LOG_L0,
8852ffcd51f3Slg 		    DPRINT_MASK_ALL, USB_CHK_ALL, NULL);
8853ffcd51f3Slg 
8854ffcd51f3Slg 		usb_pipe_close(dip, ph, USB_FLAGS_SLEEP |
8855ffcd51f3Slg 		    USBA_FLAGS_PRIVILEGED, NULL, NULL);
8856ffcd51f3Slg 	}
8857ffcd51f3Slg 	mutex_enter(HUBD_MUTEX(hubd));
8858ffcd51f3Slg 
8859ffcd51f3Slg 	return (rval);
8860ffcd51f3Slg }
8861ffcd51f3Slg 
8862ffcd51f3Slg /*
8863ffcd51f3Slg  * usba_hubdi_reset_device
8864112cd14aSqz  *	Called by usb_reset_device to handle usb device reset.
8865ffcd51f3Slg  */
8866ffcd51f3Slg int
8867ffcd51f3Slg usba_hubdi_reset_device(dev_info_t *dip, usb_dev_reset_lvl_t reset_level)
8868ffcd51f3Slg {
8869ffcd51f3Slg 	hubd_t			*hubd;
8870ffcd51f3Slg 	usb_port_t		port = 0;
8871ffcd51f3Slg 	dev_info_t		*hdip;
8872ffcd51f3Slg 	usb_pipe_state_t	prev_pipe_state = 0;
8873ffcd51f3Slg 	usba_device_t		*usba_device;
8874ffcd51f3Slg 	hubd_reset_arg_t	*arg;
8875ffcd51f3Slg 	int			i, ph_open_cnt;
8876ffcd51f3Slg 	int			rval = USB_FAILURE;
8877ffcd51f3Slg 
8878ffcd51f3Slg 	if ((!dip) || usba_is_root_hub(dip)) {
8879aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8880aa041649SRaymond Chen 		    "usba_hubdi_reset_device: NULL dip or root hub");
8881ffcd51f3Slg 
8882ffcd51f3Slg 		return (USB_INVALID_ARGS);
8883ffcd51f3Slg 	}
8884ffcd51f3Slg 
8885ffcd51f3Slg 	if (!usb_owns_device(dip)) {
8886aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8887aa041649SRaymond Chen 		    "usba_hubdi_reset_device: Not owns the device");
8888ffcd51f3Slg 
8889ffcd51f3Slg 		return (USB_INVALID_PERM);
8890ffcd51f3Slg 	}
8891ffcd51f3Slg 
8892ffcd51f3Slg 	if ((reset_level != USB_RESET_LVL_REATTACH) &&
8893ffcd51f3Slg 	    (reset_level != USB_RESET_LVL_DEFAULT)) {
8894aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8895aa041649SRaymond Chen 		    "usba_hubdi_reset_device: Unknown flags");
8896ffcd51f3Slg 
8897ffcd51f3Slg 		return (USB_INVALID_ARGS);
8898ffcd51f3Slg 	}
8899ffcd51f3Slg 
8900ffcd51f3Slg 	if ((hdip = ddi_get_parent(dip)) == NULL) {
8901aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8902aa041649SRaymond Chen 		    "usba_hubdi_reset_device: fail to get parent hub");
8903ffcd51f3Slg 
8904ffcd51f3Slg 		return (USB_INVALID_ARGS);
8905ffcd51f3Slg 	}
8906ffcd51f3Slg 
8907ffcd51f3Slg 	if ((hubd = hubd_get_soft_state(hdip)) == NULL) {
8908aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
8909aa041649SRaymond Chen 		    "usba_hubdi_reset_device: fail to get hub softstate");
8910ffcd51f3Slg 
8911ffcd51f3Slg 		return (USB_INVALID_ARGS);
8912ffcd51f3Slg 	}
8913ffcd51f3Slg 
8914ffcd51f3Slg 	mutex_enter(HUBD_MUTEX(hubd));
8915ffcd51f3Slg 
8916ffcd51f3Slg 	/* make sure the hub is connected before trying any kinds of reset. */
8917ffcd51f3Slg 	if ((hubd->h_dev_state == USB_DEV_DISCONNECTED) ||
8918ffcd51f3Slg 	    (hubd->h_dev_state == USB_DEV_SUSPENDED)) {
8919ffcd51f3Slg 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8920ffcd51f3Slg 		    "usb_reset_device: the state %d of the hub/roothub "
8921112116d8Sfb 		    "associated to the device 0x%p is incorrect",
8922112116d8Sfb 		    hubd->h_dev_state, (void *)dip);
8923ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
8924ffcd51f3Slg 
8925ffcd51f3Slg 		return (USB_INVALID_ARGS);
8926ffcd51f3Slg 	}
8927ffcd51f3Slg 
8928ffcd51f3Slg 	mutex_exit(HUBD_MUTEX(hubd));
8929ffcd51f3Slg 
8930ffcd51f3Slg 	port = hubd_child_dip2port(hubd, dip);
8931ffcd51f3Slg 
8932ffcd51f3Slg 	mutex_enter(HUBD_MUTEX(hubd));
8933ffcd51f3Slg 
8934ffcd51f3Slg 	if (hubd->h_reset_port[port]) {
8935ffcd51f3Slg 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8936ffcd51f3Slg 		    "usb_reset_device: the corresponding port is resetting");
8937ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
8938ffcd51f3Slg 
8939ffcd51f3Slg 		return (USB_SUCCESS);
8940ffcd51f3Slg 	}
8941ffcd51f3Slg 
8942ffcd51f3Slg 	/*
8943ffcd51f3Slg 	 * For Default reset, client drivers should first close all the pipes
8944ffcd51f3Slg 	 * except default pipe before calling the function, also should not
8945ffcd51f3Slg 	 * call the function during interrupt context.
8946ffcd51f3Slg 	 */
8947ffcd51f3Slg 	if (reset_level == USB_RESET_LVL_DEFAULT) {
8948ffcd51f3Slg 		usba_device = hubd->h_usba_devices[port];
8949ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
8950ffcd51f3Slg 
8951ffcd51f3Slg 		if (servicing_interrupt()) {
8952ffcd51f3Slg 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8953ffcd51f3Slg 			    "usb_reset_device: during interrput context, quit");
8954ffcd51f3Slg 
8955ffcd51f3Slg 			return (USB_INVALID_CONTEXT);
8956ffcd51f3Slg 		}
8957ffcd51f3Slg 		/* Check if all the pipes have been closed */
8958ffcd51f3Slg 		for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) {
8959ffcd51f3Slg 			if (usba_device->usb_ph_list[i].usba_ph_data) {
8960ffcd51f3Slg 				ph_open_cnt++;
8961ffcd51f3Slg 				break;
8962ffcd51f3Slg 			}
8963ffcd51f3Slg 		}
8964ffcd51f3Slg 		if (ph_open_cnt) {
8965ffcd51f3Slg 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8966ffcd51f3Slg 			    "usb_reset_device: %d pipes are still open",
8967ffcd51f3Slg 			    ph_open_cnt);
8968ffcd51f3Slg 
8969ffcd51f3Slg 			return (USB_BUSY);
8970ffcd51f3Slg 		}
8971ffcd51f3Slg 		mutex_enter(HUBD_MUTEX(hubd));
8972ffcd51f3Slg 	}
8973ffcd51f3Slg 
8974ffcd51f3Slg 	/* Don't perform reset while the device is detaching */
8975ffcd51f3Slg 	if (hubd->h_port_state[port] & HUBD_CHILD_DETACHING) {
8976ffcd51f3Slg 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
8977ffcd51f3Slg 		    "usb_reset_device: the device is detaching, "
8978ffcd51f3Slg 		    "cannot be reset");
8979ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
8980ffcd51f3Slg 
8981ffcd51f3Slg 		return (USB_FAILURE);
8982ffcd51f3Slg 	}
8983ffcd51f3Slg 
8984ffcd51f3Slg 	hubd->h_reset_port[port] = B_TRUE;
8985ffcd51f3Slg 	hdip = hubd->h_dip;
8986ffcd51f3Slg 	mutex_exit(HUBD_MUTEX(hubd));
8987ffcd51f3Slg 
8988ffcd51f3Slg 	/* Don't allow hub detached during the reset */
8989ffcd51f3Slg 	ndi_hold_devi(hdip);
8990ffcd51f3Slg 
8991ffcd51f3Slg 	mutex_enter(HUBD_MUTEX(hubd));
8992ffcd51f3Slg 	hubd_pm_busy_component(hubd, hdip, 0);
8993ffcd51f3Slg 	mutex_exit(HUBD_MUTEX(hubd));
8994ffcd51f3Slg 	/* go full power */
8995ffcd51f3Slg 	(void) pm_raise_power(hdip, 0, USB_DEV_OS_FULL_PWR);
8996ffcd51f3Slg 	mutex_enter(HUBD_MUTEX(hubd));
8997ffcd51f3Slg 
8998ffcd51f3Slg 	hubd->h_hotplug_thread++;
8999ffcd51f3Slg 
9000ffcd51f3Slg 	/* stop polling if it was active */
9001ffcd51f3Slg 	if (hubd->h_ep1_ph) {
9002ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
9003ffcd51f3Slg 		(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
9004ffcd51f3Slg 		    USB_FLAGS_SLEEP);
9005ffcd51f3Slg 		mutex_enter(HUBD_MUTEX(hubd));
9006ffcd51f3Slg 
9007ffcd51f3Slg 		if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
9008ffcd51f3Slg 			hubd_stop_polling(hubd);
9009ffcd51f3Slg 		}
9010ffcd51f3Slg 	}
9011ffcd51f3Slg 
9012ffcd51f3Slg 	switch (reset_level) {
9013ffcd51f3Slg 	case USB_RESET_LVL_REATTACH:
9014ffcd51f3Slg 		mutex_exit(HUBD_MUTEX(hubd));
9015ffcd51f3Slg 		arg = (hubd_reset_arg_t *)kmem_zalloc(
9016ffcd51f3Slg 		    sizeof (hubd_reset_arg_t), KM_SLEEP);
9017ffcd51f3Slg 		arg->hubd = hubd;
9018ffcd51f3Slg 		arg->reset_port = port;
9019ffcd51f3Slg 		mutex_enter(HUBD_MUTEX(hubd));
9020ffcd51f3Slg 
9021ffcd51f3Slg 		if ((rval = usb_async_req(hdip, hubd_reset_thread,
9022ffcd51f3Slg 		    (void *)arg, 0)) == USB_SUCCESS) {
9023ffcd51f3Slg 			hubd->h_hotplug_thread--;
9024ffcd51f3Slg 			mutex_exit(HUBD_MUTEX(hubd));
9025ffcd51f3Slg 
9026ffcd51f3Slg 			return (USB_SUCCESS);
9027ffcd51f3Slg 		} else {
9028ffcd51f3Slg 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9029ffcd51f3Slg 			    "Cannot create reset thread, the device %s%d failed"
9030ffcd51f3Slg 			    " to reset", ddi_driver_name(dip),
9031ffcd51f3Slg 			    ddi_get_instance(dip));
9032ffcd51f3Slg 
9033ffcd51f3Slg 			kmem_free(arg, sizeof (hubd_reset_arg_t));
9034ffcd51f3Slg 		}
9035ffcd51f3Slg 
9036ffcd51f3Slg 		break;
9037ffcd51f3Slg 	case USB_RESET_LVL_DEFAULT:
9038ffcd51f3Slg 		/*
9039ffcd51f3Slg 		 * Reset hub port and then recover device's address, set back
9040ffcd51f3Slg 		 * device's configuration, hubd_handle_port_connect() will
9041ffcd51f3Slg 		 * handle errors happened during this process.
9042ffcd51f3Slg 		 */
9043ffcd51f3Slg 		if ((rval = hubd_handle_port_connect(hubd, port))
9044ffcd51f3Slg 		    == USB_SUCCESS) {
9045ffcd51f3Slg 			mutex_exit(HUBD_MUTEX(hubd));
9046ffcd51f3Slg 			/* re-open the default pipe */
9047ffcd51f3Slg 			rval = usba_persistent_pipe_open(usba_device);
9048ffcd51f3Slg 			mutex_enter(HUBD_MUTEX(hubd));
9049ffcd51f3Slg 			if (rval != USB_SUCCESS) {
9050ffcd51f3Slg 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
9051ffcd51f3Slg 				    hubd->h_log_handle, "failed to reopen "
9052ffcd51f3Slg 				    "default pipe after reset, disable hub"
9053ffcd51f3Slg 				    "port for %s%d", ddi_driver_name(dip),
9054ffcd51f3Slg 				    ddi_get_instance(dip));
9055ffcd51f3Slg 				/*
9056ffcd51f3Slg 				 * Disable port to set out a hotplug thread
9057ffcd51f3Slg 				 * which will handle errors.
9058ffcd51f3Slg 				 */
9059ffcd51f3Slg 				(void) hubd_disable_port(hubd, port);
9060ffcd51f3Slg 			}
9061ffcd51f3Slg 		}
9062ffcd51f3Slg 
9063ffcd51f3Slg 		break;
9064ffcd51f3Slg 	default:
9065ffcd51f3Slg 
9066ffcd51f3Slg 		break;
9067ffcd51f3Slg 	}
9068ffcd51f3Slg 
9069ffcd51f3Slg 	/* allow hotplug thread now */
9070ffcd51f3Slg 	hubd->h_hotplug_thread--;
9071ffcd51f3Slg 
9072ffcd51f3Slg 	if ((hubd->h_dev_state == USB_DEV_ONLINE) && hubd->h_ep1_ph &&
9073ffcd51f3Slg 	    (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
9074ffcd51f3Slg 		hubd_start_polling(hubd, 0);
9075ffcd51f3Slg 	}
9076ffcd51f3Slg 
9077ffcd51f3Slg 	hubd_pm_idle_component(hubd, hdip, 0);
9078ffcd51f3Slg 
9079ffcd51f3Slg 	/* Clear reset mark for the port. */
9080ffcd51f3Slg 	hubd->h_reset_port[port] = B_FALSE;
9081ffcd51f3Slg 
9082ffcd51f3Slg 	mutex_exit(HUBD_MUTEX(hubd));
9083ffcd51f3Slg 
9084ffcd51f3Slg 	ndi_rele_devi(hdip);
9085ffcd51f3Slg 
9086ffcd51f3Slg 	return (rval);
9087ffcd51f3Slg }
9088