xref: /illumos-gate/usr/src/uts/common/io/usb/usba/hubdi.c (revision d5ebc493)
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
5fef1e07eSsl147100  * Common Development and Distribution License (the "License").
6fef1e07eSsl147100  * 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 /*
22f5b8369cSRaymond Chen  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23cd21e7c5SGarrett D'Amore  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
24e5815e7aSJosef 'Jeff' Sipek  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
250d2006e4SRobert Mustacchi  * Copyright 2019, Joyent, Inc.
263fe80ca4SDan Cross  * Copyright 2023 Oxide Computer Company
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * USBA: Solaris USB Architecture support for the hub
317c478bd9Sstevel@tonic-gate  * including root hub
327c478bd9Sstevel@tonic-gate  * Most of the code for hubd resides in this file and
337c478bd9Sstevel@tonic-gate  * is shared between the HCD root hub support and hubd
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate #define	USBA_FRAMEWORK
367c478bd9Sstevel@tonic-gate #include <sys/usb/usba.h>
377c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_devdb.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
397c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_types.h>
417c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hubdi.h>
427c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hub.h>
447c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hubdvar.h>
457c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hubd_impl.h>
467c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
477c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
487c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
49d29f5a71Szhigang lu - Sun Microsystems - Beijing China #include <sys/strsun.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
5240482326SVincent Wang  * External functions
5340482326SVincent Wang  */
5440482326SVincent Wang extern boolean_t consconfig_console_is_ready(void);
5540482326SVincent Wang 
5640482326SVincent Wang /*
577c478bd9Sstevel@tonic-gate  * Prototypes for static functions
587c478bd9Sstevel@tonic-gate  */
59d96925c4SRichard Lowe static	int	usba_hubdi_bus_ctl(dev_info_t *dip,
607c478bd9Sstevel@tonic-gate     dev_info_t *rdip,
617c478bd9Sstevel@tonic-gate     ddi_ctl_enum_t op,
627c478bd9Sstevel@tonic-gate     void *arg,
637c478bd9Sstevel@tonic-gate     void *result);
647c478bd9Sstevel@tonic-gate 
65d96925c4SRichard Lowe static int	usba_hubdi_map_fault(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,
82*d5ebc493SDan Cross     ddi_event_cb_f callback,
83*d5ebc493SDan Cross     void *arg,
84*d5ebc493SDan Cross     ddi_callback_id_t *cb_id);
857c478bd9Sstevel@tonic-gate static int hubd_busop_remove_eventcall(dev_info_t *dip,
867c478bd9Sstevel@tonic-gate     ddi_callback_id_t cb_id);
877c478bd9Sstevel@tonic-gate static int hubd_bus_config(dev_info_t *dip,
887c478bd9Sstevel@tonic-gate     uint_t flag,
897c478bd9Sstevel@tonic-gate     ddi_bus_config_op_t op,
907c478bd9Sstevel@tonic-gate     void *arg,
917c478bd9Sstevel@tonic-gate     dev_info_t **child);
927c478bd9Sstevel@tonic-gate static int hubd_bus_unconfig(dev_info_t *dip,
937c478bd9Sstevel@tonic-gate     uint_t flag,
947c478bd9Sstevel@tonic-gate     ddi_bus_config_op_t op,
957c478bd9Sstevel@tonic-gate     void *arg);
967c478bd9Sstevel@tonic-gate static int hubd_bus_power(dev_info_t *dip, void *impl_arg,
977c478bd9Sstevel@tonic-gate     pm_bus_power_op_t op, void *arg, void *result);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static usb_port_t  hubd_get_port_num(hubd_t *, struct devctl_iocdata *);
1007c478bd9Sstevel@tonic-gate static dev_info_t *hubd_get_child_dip(hubd_t *, usb_port_t);
1017c478bd9Sstevel@tonic-gate static uint_t hubd_cfgadm_state(hubd_t *, usb_port_t);
1027c478bd9Sstevel@tonic-gate static int hubd_toggle_port(hubd_t *, usb_port_t);
1037c478bd9Sstevel@tonic-gate static void hubd_register_cpr_callback(hubd_t *);
1047c478bd9Sstevel@tonic-gate static void hubd_unregister_cpr_callback(hubd_t *);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Busops vector for USB HUB's
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate struct bus_ops usba_hubdi_busops =	{
1107c478bd9Sstevel@tonic-gate 	BUSO_REV,
1117c478bd9Sstevel@tonic-gate 	nullbusmap,			/* bus_map */
1127c478bd9Sstevel@tonic-gate 	NULL,				/* bus_get_intrspec */
1137c478bd9Sstevel@tonic-gate 	NULL,				/* bus_add_intrspec */
1147c478bd9Sstevel@tonic-gate 	NULL,				/* bus_remove_intrspec */
1157c478bd9Sstevel@tonic-gate 	usba_hubdi_map_fault,		/* bus_map_fault */
116cd21e7c5SGarrett D'Amore 	NULL,				/* bus_dma_map */
1177c478bd9Sstevel@tonic-gate 	ddi_dma_allochdl,
1187c478bd9Sstevel@tonic-gate 	ddi_dma_freehdl,
1197c478bd9Sstevel@tonic-gate 	ddi_dma_bindhdl,
1207c478bd9Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1217c478bd9Sstevel@tonic-gate 	ddi_dma_flush,
1227c478bd9Sstevel@tonic-gate 	ddi_dma_win,
1237c478bd9Sstevel@tonic-gate 	ddi_dma_mctl,			/* bus_dma_ctl */
1247c478bd9Sstevel@tonic-gate 	usba_hubdi_bus_ctl,		/* bus_ctl */
1257c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,		/* bus_prop_op */
1267c478bd9Sstevel@tonic-gate 	hubd_busop_get_eventcookie,
1277c478bd9Sstevel@tonic-gate 	hubd_busop_add_eventcall,
1287c478bd9Sstevel@tonic-gate 	hubd_busop_remove_eventcall,
1297c478bd9Sstevel@tonic-gate 	NULL,				/* bus_post_event */
1307c478bd9Sstevel@tonic-gate 	NULL,				/* bus_intr_ctl */
1317c478bd9Sstevel@tonic-gate 	hubd_bus_config,		/* bus_config */
1327c478bd9Sstevel@tonic-gate 	hubd_bus_unconfig,		/* bus_unconfig */
1337c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_init */
1347c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_fini */
1357c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_enter */
1367c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_exit */
1377c478bd9Sstevel@tonic-gate 	hubd_bus_power			/* bus_power */
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate 
140f5b8369cSRaymond Chen #define	USB_HUB_INTEL_VID	0x8087
141f5b8369cSRaymond Chen #define	USB_HUB_INTEL_PID	0x0020
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * local variables
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate static kmutex_t	usba_hubdi_mutex;	/* protects USBA HUB data structures */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static usba_list_entry_t	usba_hubdi_list;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate usb_log_handle_t	hubdi_log_handle;
1517c478bd9Sstevel@tonic-gate uint_t			hubdi_errlevel = USB_LOG_L4;
1527c478bd9Sstevel@tonic-gate uint_t			hubdi_errmask = (uint_t)-1;
153d29f5a71Szhigang lu - Sun Microsystems - Beijing China uint8_t			hubdi_min_pm_threshold = 5; /* seconds */
154aa041649SRaymond Chen uint8_t			hubdi_reset_delay = 20; /* seconds */
1558e1b7aa1Sfei feng - Sun Microsystems - Beijing China extern int modrootloaded;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * initialize private data
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate void
usba_hubdi_initialization()1617c478bd9Sstevel@tonic-gate usba_hubdi_initialization()
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	hubdi_log_handle = usb_alloc_log_hdl(NULL, "hubdi", &hubdi_errlevel,
1647c478bd9Sstevel@tonic-gate 	    &hubdi_errmask, NULL, 0);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
1677c478bd9Sstevel@tonic-gate 	    "usba_hubdi_initialization");
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	mutex_init(&usba_hubdi_mutex, NULL, MUTEX_DRIVER, NULL);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	usba_init_list(&usba_hubdi_list, NULL, NULL);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate void
usba_hubdi_destroy()1767c478bd9Sstevel@tonic-gate usba_hubdi_destroy()
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubdi_log_handle,
1797c478bd9Sstevel@tonic-gate 	    "usba_hubdi_destroy");
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	mutex_destroy(&usba_hubdi_mutex);
1827c478bd9Sstevel@tonic-gate 	usba_destroy_list(&usba_hubdi_list);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(hubdi_log_handle);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Called by an	HUB to attach an instance of the driver
1907c478bd9Sstevel@tonic-gate  *	make this instance known to USBA
1917c478bd9Sstevel@tonic-gate  *	the HUB	should initialize usba_hubdi structure prior
1927c478bd9Sstevel@tonic-gate  *	to calling this	interface
1937c478bd9Sstevel@tonic-gate  */
194ff0e937bSRaymond Chen int
usba_hubdi_register(dev_info_t * dip,uint_t flags)195993e3fafSRobert Mustacchi usba_hubdi_register(dev_info_t	*dip, 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
usba_hubdi_unregister(dev_info_t * dip)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
usba_hubdi_map_fault(dev_info_t * dip,dev_info_t * rdip,struct hat * hat,struct seg * seg,caddr_t addr,struct devpage * dp,pfn_t pfn,uint_t prot,uint_t lock)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
usba_hubdi_bind_root_hub(dev_info_t * dip,uchar_t * root_hub_config_descriptor,size_t config_length,usb_dev_descr_t * root_hub_device_descriptor)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 *),
3047c478bd9Sstevel@tonic-gate 	    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),
3087c478bd9Sstevel@tonic-gate 	    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] =
3137c478bd9Sstevel@tonic-gate 	    sizeof (root_hub_config_descriptor);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	usba_device->usb_cfg_str_descr = kmem_zalloc(sizeof (uchar_t *),
3167c478bd9Sstevel@tonic-gate 	    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,
334993e3fafSRobert Mustacchi 	 * whether root hub is a Super, High, or Full speed usb device.
3357c478bd9Sstevel@tonic-gate 	 */
336993e3fafSRobert Mustacchi 	if (root_hub_device_descriptor->bDeviceProtocol >= 0x3) {
337993e3fafSRobert Mustacchi 		usba_device->usb_port_status = USBA_SUPER_SPEED_DEV;
338993e3fafSRobert Mustacchi 	} else if (root_hub_device_descriptor->bDeviceProtocol > 0) {
3397c478bd9Sstevel@tonic-gate 		usba_device->usb_port_status = USBA_HIGH_SPEED_DEV;
3407c478bd9Sstevel@tonic-gate 	} else {
3417c478bd9Sstevel@tonic-gate 		usba_device->usb_port_status = USBA_FULL_SPEED_DEV;
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	usba_set_usba_device(dip, usba_device);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * For the root hub the default pipe is not yet open
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	if (usb_pipe_open(dip, NULL, NULL,
3527c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph) != USB_SUCCESS) {
3537c478bd9Sstevel@tonic-gate 		goto fail;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * kill off all OBP children, they may not be fully
3587c478bd9Sstevel@tonic-gate 	 * enumerated
3597c478bd9Sstevel@tonic-gate 	 */
3607c478bd9Sstevel@tonic-gate 	while (child) {
3617c478bd9Sstevel@tonic-gate 		dev_info_t *next = ddi_get_next_sibling(child);
3627c478bd9Sstevel@tonic-gate 		(void) ddi_remove_child(child, 0);
3637c478bd9Sstevel@tonic-gate 		child = next;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * "attach" the root hub driver
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	if (usba_hubdi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
3707c478bd9Sstevel@tonic-gate 		goto fail;
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate fail:
3767c478bd9Sstevel@tonic-gate 	if (ph) {
3777c478bd9Sstevel@tonic-gate 		usb_pipe_close(dip, ph,
3787c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array,
3827c478bd9Sstevel@tonic-gate 	    usba_device->usb_cfg_array_length);
3837c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array_len,
3847c478bd9Sstevel@tonic-gate 	    usba_device->usb_cfg_array_len_length);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	usba_free_usba_device(usba_device);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	usba_set_usba_device(dip, NULL);
391993e3fafSRobert Mustacchi 
3927c478bd9Sstevel@tonic-gate 	if (root_hubd) {
3937c478bd9Sstevel@tonic-gate 		kmem_free(root_hubd, sizeof (hubd_t));
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
396993e3fafSRobert Mustacchi 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
397993e3fafSRobert Mustacchi 
398993e3fafSRobert Mustacchi 	usba_rem_root_hub(dip);
399993e3fafSRobert Mustacchi 
4007c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate int
usba_hubdi_unbind_root_hub(dev_info_t * dip)4057c478bd9Sstevel@tonic-gate usba_hubdi_unbind_root_hub(dev_info_t *dip)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/* was root hub attached? */
4107c478bd9Sstevel@tonic-gate 	if (!(usba_is_root_hub(dip))) {
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		/* return success anyway */
4137c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * usba_hubdi_detach also closes the default pipe
4187c478bd9Sstevel@tonic-gate 	 * and removes properties so there is no need to
4197c478bd9Sstevel@tonic-gate 	 * do it here
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	if (usba_hubdi_detach(dip, DDI_DETACH) != DDI_SUCCESS) {
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(dip)) {
424d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
4257c478bd9Sstevel@tonic-gate 			    "failure to unbind root hub after attach failure");
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_root_hubd, sizeof (hubd_t));
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array,
4367c478bd9Sstevel@tonic-gate 	    usba_device->usb_cfg_array_length);
4377c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_array_len,
4387c478bd9Sstevel@tonic-gate 	    usba_device->usb_cfg_array_len_length);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	kmem_free(usba_device->usb_cfg_str_descr, sizeof (uchar_t *));
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	usba_free_usba_device(usba_device);
4437c478bd9Sstevel@tonic-gate 
444de6f998eSrui wang - Sun Microsystems - Beijing China 	usba_rem_root_hub(dip);
445de6f998eSrui wang - Sun Microsystems - Beijing China 
4467c478bd9Sstevel@tonic-gate 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "root-hub");
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Actual Hub Driver support code:
4547c478bd9Sstevel@tonic-gate  *	shared by root hub and non-root hubs
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /* Debugging support */
459ff0e937bSRaymond Chen uint_t hubd_errlevel	= USB_LOG_L4;
460ff0e937bSRaymond Chen uint_t hubd_errmask	= (uint_t)DPRINT_MASK_ALL;
461ff0e937bSRaymond Chen uint_t hubd_instance_debug = (uint_t)-1;
4627c478bd9Sstevel@tonic-gate static uint_t hubdi_bus_config_debug = 0;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errlevel))
4657c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_errmask))
4667c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(hubd_instance_debug))
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
4697c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate  * local variables:
4747c478bd9Sstevel@tonic-gate  *
4757c478bd9Sstevel@tonic-gate  * Amount of time to wait between resetting the port and accessing
4767c478bd9Sstevel@tonic-gate  * the device.	The value is in microseconds.
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate static uint_t hubd_device_delay = 1000000;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * enumeration retry
4827c478bd9Sstevel@tonic-gate  */
4837c478bd9Sstevel@tonic-gate #define	HUBD_PORT_RETRY 5
4847c478bd9Sstevel@tonic-gate static uint_t hubd_retry_enumerate = HUBD_PORT_RETRY;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * Stale hotremoved device cleanup delay
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate #define	HUBD_STALE_DIP_CLEANUP_DELAY	5000000
4907c478bd9Sstevel@tonic-gate static uint_t hubd_dip_cleanup_delay = HUBD_STALE_DIP_CLEANUP_DELAY;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  * retries for USB suspend and resume
4947c478bd9Sstevel@tonic-gate  */
4957c478bd9Sstevel@tonic-gate #define	HUBD_SUS_RES_RETRY	2
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate void	*hubd_statep;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * prototypes
5017c478bd9Sstevel@tonic-gate  */
5027c478bd9Sstevel@tonic-gate static int hubd_cleanup(dev_info_t *dip, hubd_t  *hubd);
5037c478bd9Sstevel@tonic-gate static int hubd_check_ports(hubd_t  *hubd);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate static int  hubd_open_intr_pipe(hubd_t *hubd);
5067c478bd9Sstevel@tonic-gate static void hubd_start_polling(hubd_t *hubd, int always);
5077c478bd9Sstevel@tonic-gate static void hubd_stop_polling(hubd_t *hubd);
5087c478bd9Sstevel@tonic-gate static void hubd_close_intr_pipe(hubd_t *hubd);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate static void hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *req);
5117c478bd9Sstevel@tonic-gate static void hubd_exception_cb(usb_pipe_handle_t pipe,
5127c478bd9Sstevel@tonic-gate 						usb_intr_req_t *req);
5137c478bd9Sstevel@tonic-gate static void hubd_hotplug_thread(void *arg);
514ffcd51f3Slg150142 static void hubd_reset_thread(void *arg);
5157c478bd9Sstevel@tonic-gate static int hubd_create_child(dev_info_t *dip,
5167c478bd9Sstevel@tonic-gate 		hubd_t		*hubd,
5177c478bd9Sstevel@tonic-gate 		usba_device_t	*usba_device,
5187c478bd9Sstevel@tonic-gate 		usb_port_status_t port_status,
5197c478bd9Sstevel@tonic-gate 		usb_port_t	port,
5207c478bd9Sstevel@tonic-gate 		int		iteration);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate static int hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag,
5237c478bd9Sstevel@tonic-gate 	boolean_t retry);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate static int hubd_get_hub_descriptor(hubd_t *hubd);
5267c478bd9Sstevel@tonic-gate 
527993e3fafSRobert Mustacchi static int hubd_set_hub_depth(hubd_t *hubd);
528993e3fafSRobert Mustacchi 
52935f36846Ssl147100 static int hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status);
53035f36846Ssl147100 
5317c478bd9Sstevel@tonic-gate static int hubd_reset_port(hubd_t *hubd, usb_port_t port);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate static int hubd_get_hub_status(hubd_t *hubd);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate static int hubd_handle_port_connect(hubd_t *hubd, usb_port_t port);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static int hubd_disable_port(hubd_t *hubd, usb_port_t port);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate static int hubd_enable_port(hubd_t *hubd, usb_port_t port);
5407c478bd9Sstevel@tonic-gate static int hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate static int hubd_determine_port_status(hubd_t *hubd, usb_port_t port,
543993e3fafSRobert Mustacchi 	uint16_t *status, uint16_t *change, usb_port_status_t *speed,
544993e3fafSRobert Mustacchi 	uint_t ack_flag);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate static int hubd_enable_all_port_power(hubd_t *hubd);
5477c478bd9Sstevel@tonic-gate static int hubd_disable_all_port_power(hubd_t *hubd);
5487c478bd9Sstevel@tonic-gate static int hubd_disable_port_power(hubd_t *hubd, usb_port_t port);
5497c478bd9Sstevel@tonic-gate static int hubd_enable_port_power(hubd_t *hubd, usb_port_t port);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate static void hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate static int hubd_can_suspend(hubd_t *hubd);
5547c478bd9Sstevel@tonic-gate static void hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd);
5557c478bd9Sstevel@tonic-gate static int hubd_setdevaddr(hubd_t *hubd, usb_port_t port);
5567c478bd9Sstevel@tonic-gate static void hubd_setdevconfig(hubd_t *hubd, usb_port_t port);
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate static int hubd_register_events(hubd_t *hubd);
5597c478bd9Sstevel@tonic-gate static void hubd_do_callback(hubd_t *hubd, dev_info_t *dip,
5607c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie);
5617c478bd9Sstevel@tonic-gate static void hubd_run_callbacks(hubd_t *hubd, usba_event_t type);
5627c478bd9Sstevel@tonic-gate static void hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type);
5637c478bd9Sstevel@tonic-gate static void hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate static int hubd_disconnect_event_cb(dev_info_t *dip);
5667c478bd9Sstevel@tonic-gate static int hubd_reconnect_event_cb(dev_info_t *dip);
5677c478bd9Sstevel@tonic-gate static int hubd_pre_suspend_event_cb(dev_info_t *dip);
5687c478bd9Sstevel@tonic-gate static int hubd_post_resume_event_cb(dev_info_t *dip);
5697c478bd9Sstevel@tonic-gate static int hubd_cpr_suspend(hubd_t *hubd);
5707c478bd9Sstevel@tonic-gate static void hubd_cpr_resume(dev_info_t *dip);
5717c478bd9Sstevel@tonic-gate static int hubd_restore_state_cb(dev_info_t *dip);
572ffcd51f3Slg150142 static int hubd_check_same_device(hubd_t *hubd, usb_port_t port);
5737c478bd9Sstevel@tonic-gate 
57435f36846Ssl147100 static int hubd_init_power_budget(hubd_t *hubd);
57535f36846Ssl147100 
5767c478bd9Sstevel@tonic-gate static ndi_event_definition_t hubd_ndi_event_defs[] = {
5777c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
5787c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5797c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
5807c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5817c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
5827c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
5837c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
5847c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL}
5857c478bd9Sstevel@tonic-gate };
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate #define	HUBD_N_NDI_EVENTS \
5887c478bd9Sstevel@tonic-gate 	(sizeof (hubd_ndi_event_defs) / sizeof (ndi_event_definition_t))
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate static ndi_event_set_t hubd_ndi_events = {
5917c478bd9Sstevel@tonic-gate 	NDI_EVENTS_REV1, HUBD_N_NDI_EVENTS, hubd_ndi_event_defs};
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /* events received from parent */
5947c478bd9Sstevel@tonic-gate static usb_event_t hubd_events = {
5957c478bd9Sstevel@tonic-gate 	hubd_disconnect_event_cb,
5967c478bd9Sstevel@tonic-gate 	hubd_reconnect_event_cb,
5977c478bd9Sstevel@tonic-gate 	hubd_pre_suspend_event_cb,
5987c478bd9Sstevel@tonic-gate 	hubd_post_resume_event_cb
5997c478bd9Sstevel@tonic-gate };
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * hubd_get_soft_state() returns the hubd soft state
6047c478bd9Sstevel@tonic-gate  */
605ff0e937bSRaymond Chen hubd_t *
hubd_get_soft_state(dev_info_t * dip)6067c478bd9Sstevel@tonic-gate hubd_get_soft_state(dev_info_t *dip)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
6097c478bd9Sstevel@tonic-gate 		return (NULL);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
612e2c88f0cSGarrett D'Amore 	if (usba_is_root_hub(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
hubd_pm_busy_component(hubd_t * hubd,dev_info_t * dip,int component)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
hubd_pm_idle_component(hubd_t * hubd,dev_info_t * dip,int component)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
hubd_set_child_pwrlvl(hubd_t * hubd,usb_port_t port,uint8_t power)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;
683993e3fafSRobert Mustacchi 	for (portno = 1; portno <= hubd->h_nports; 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
hubd_child_dip2port(hubd_t * hubd,dev_info_t * dip)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));
716993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
7177c478bd9Sstevel@tonic-gate 		if (hubd->h_children_dips[port] == dip) {
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 			break;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 	}
722993e3fafSRobert Mustacchi 	ASSERT(port <= hubd->h_nports);
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
hubd_can_suspend(hubd_t * hubd)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 	 */
751e5815e7aSJosef 'Jeff' Sipek 	if (gethrtime() < (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) &&
758993e3fafSRobert Mustacchi 	    (port <= hubd->h_nports); 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,
7637c478bd9Sstevel@tonic-gate 	    "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
hubd_resume_port(hubd_t * hubd,usb_port_t port)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,
81035f36846Ssl147100 		    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,
825993e3fafSRobert Mustacchi 		    &status, &change, NULL, 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:
8376c7181fcSsl147100 		if (((hubd->h_port_state[port] & PORT_STATUS_CCS) == 0) ||
8386c7181fcSsl147100 		    ((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,
86135f36846Ssl147100 			    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,
885993e3fafSRobert Mustacchi 				    &status, &change, NULL, 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
hubd_suspend_port(hubd_t * hubd,usb_port_t port)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,
96635f36846Ssl147100 			    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,
989993e3fafSRobert Mustacchi 			    &status, &change, NULL, 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;
9956f6c7d2bSVincent Wang 			} else {
9966f6c7d2bSVincent Wang 				USB_DPRINTF_L0(DPRINT_MASK_PM,
9976f6c7d2bSVincent Wang 				    hubd->h_log_handle,
9986f6c7d2bSVincent Wang 				    "hubdi: port%d failed to be suspended!",
9996f6c7d2bSVincent Wang 				    port);
10007c478bd9Sstevel@tonic-gate 			}
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread--;
10047c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 		break;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
10097c478bd9Sstevel@tonic-gate 		/* Ignore - No Operation */
10107c478bd9Sstevel@tonic-gate 		retval = USB_SUCCESS;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 		break;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
10157c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
10167c478bd9Sstevel@tonic-gate 	default:
10177c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
10187c478bd9Sstevel@tonic-gate 		    "Improper state for port Suspend");
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 		break;
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	return (retval);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate /*
10297c478bd9Sstevel@tonic-gate  * child post attach/detach notifications
10307c478bd9Sstevel@tonic-gate  */
10317c478bd9Sstevel@tonic-gate static void
hubd_post_attach(hubd_t * hubd,usb_port_t port,struct attachspec * as)10327c478bd9Sstevel@tonic-gate hubd_post_attach(hubd_t *hubd, usb_port_t port, struct attachspec *as)
10337c478bd9Sstevel@tonic-gate {
10347c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
10377c478bd9Sstevel@tonic-gate 	    "hubd_post_attach: port=%d result=%d",
10387c478bd9Sstevel@tonic-gate 	    port, as->result);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	if (as->result == DDI_SUCCESS) {
10417c478bd9Sstevel@tonic-gate 		/*
10427c478bd9Sstevel@tonic-gate 		 * Check if the child created wants to be power managed.
10437c478bd9Sstevel@tonic-gate 		 * If yes, the childs power level gets automatically tracked
10447c478bd9Sstevel@tonic-gate 		 * by DDI_CTLOPS_POWER busctl.
10457c478bd9Sstevel@tonic-gate 		 * If no, we set power of the new child by default
10467c478bd9Sstevel@tonic-gate 		 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
10477c478bd9Sstevel@tonic-gate 		 */
10487c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
10497c478bd9Sstevel@tonic-gate 		dip = hubd->h_children_dips[port];
10507c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10517c478bd9Sstevel@tonic-gate 		if (DEVI(dip)->devi_pm_info == NULL) {
10527c478bd9Sstevel@tonic-gate 			hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_FULL_PWR);
10537c478bd9Sstevel@tonic-gate 		}
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate static void
hubd_post_detach(hubd_t * hubd,usb_port_t port,struct detachspec * ds)10597c478bd9Sstevel@tonic-gate hubd_post_detach(hubd_t *hubd, usb_port_t port, struct detachspec *ds)
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
10627c478bd9Sstevel@tonic-gate 	    "hubd_post_detach: port=%d result=%d", port, ds->result);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/*
10657c478bd9Sstevel@tonic-gate 	 * if the device is successfully detached and is the
10667c478bd9Sstevel@tonic-gate 	 * last device to detach, mark component as idle
10677c478bd9Sstevel@tonic-gate 	 */
10687c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
10697c478bd9Sstevel@tonic-gate 	if (ds->result == DDI_SUCCESS) {
10707c478bd9Sstevel@tonic-gate 		usba_device_t	*usba_device = hubd->h_usba_devices[port];
107135f36846Ssl147100 		dev_info_t	*pdip = hubd->h_dip;
10727c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10737c478bd9Sstevel@tonic-gate 
107435f36846Ssl147100 		usba_hubdi_incr_power_budget(pdip, usba_device);
107535f36846Ssl147100 
10767c478bd9Sstevel@tonic-gate 		/*
10777c478bd9Sstevel@tonic-gate 		 * We set power of the detached child
10787c478bd9Sstevel@tonic-gate 		 * to 0, so that we can suspend if all
10797c478bd9Sstevel@tonic-gate 		 * our children are gone
10807c478bd9Sstevel@tonic-gate 		 */
10817c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, USB_DEV_OS_PWR_OFF);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 		/* check for leaks on detaching */
10847c478bd9Sstevel@tonic-gate 		if ((usba_device) && (ds->cmd == DDI_DETACH)) {
10857c478bd9Sstevel@tonic-gate 			usba_check_for_leaks(usba_device);
10867c478bd9Sstevel@tonic-gate 		}
10877c478bd9Sstevel@tonic-gate 	} else {
10887c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
10897c478bd9Sstevel@tonic-gate 	}
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate /*
10947c478bd9Sstevel@tonic-gate  * hubd_post_power
10957c478bd9Sstevel@tonic-gate  *	After the child's power entry point has been called
10967c478bd9Sstevel@tonic-gate  *	we record its power level in our local struct.
10977c478bd9Sstevel@tonic-gate  *	If the device has powered off, we suspend port
10987c478bd9Sstevel@tonic-gate  */
10997c478bd9Sstevel@tonic-gate static int
hubd_post_power(hubd_t * hubd,usb_port_t port,pm_bp_child_pwrchg_t * bpc,int result)11007c478bd9Sstevel@tonic-gate hubd_post_power(hubd_t *hubd, usb_port_t port, pm_bp_child_pwrchg_t *bpc,
11017c478bd9Sstevel@tonic-gate     int result)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	int	retval = USB_SUCCESS;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
11067c478bd9Sstevel@tonic-gate 	    "hubd_post_power: port=%d", port);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (result == DDI_SUCCESS) {
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 		/* record this power in our local struct */
11117c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, bpc->bpc_nlevel);
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 		if (bpc->bpc_nlevel == USB_DEV_OS_PWR_OFF) {
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 			/* now suspend the port */
11167c478bd9Sstevel@tonic-gate 			retval = hubd_suspend_port(hubd, port);
11177c478bd9Sstevel@tonic-gate 		} else if (bpc->bpc_nlevel == USB_DEV_OS_FULL_PWR) {
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 			/* make sure the port is resumed */
11207c478bd9Sstevel@tonic-gate 			retval = hubd_resume_port(hubd, port);
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 	} else {
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 		/* record old power in our local struct */
11257c478bd9Sstevel@tonic-gate 		hubd_set_child_pwrlvl(hubd, port, bpc->bpc_olevel);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 		if (bpc->bpc_olevel == USB_DEV_OS_PWR_OFF) {
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 			/*
11307c478bd9Sstevel@tonic-gate 			 * As this device failed to transition from
11317c478bd9Sstevel@tonic-gate 			 * power off state, suspend the port again
11327c478bd9Sstevel@tonic-gate 			 */
11337c478bd9Sstevel@tonic-gate 			retval = hubd_suspend_port(hubd, port);
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	return (retval);
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate /*
11427c478bd9Sstevel@tonic-gate  * bus ctl notifications are handled here, the rest goes up to root hub/hcd
11437c478bd9Sstevel@tonic-gate  */
11447c478bd9Sstevel@tonic-gate static int
usba_hubdi_bus_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t op,void * arg,void * result)11457c478bd9Sstevel@tonic-gate usba_hubdi_bus_ctl(dev_info_t *dip,
11467c478bd9Sstevel@tonic-gate     dev_info_t	*rdip,
11477c478bd9Sstevel@tonic-gate     ddi_ctl_enum_t	op,
11487c478bd9Sstevel@tonic-gate     void		*arg,
11497c478bd9Sstevel@tonic-gate     void		*result)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
11527c478bd9Sstevel@tonic-gate 	dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
11537c478bd9Sstevel@tonic-gate 	struct attachspec *as;
11547c478bd9Sstevel@tonic-gate 	struct detachspec *ds;
11557c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
11567c478bd9Sstevel@tonic-gate 	usb_port_t	port;
11573fe80ca4SDan Cross 	int		rval;
11587c478bd9Sstevel@tonic-gate 	int		retval = DDI_FAILURE;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	/* flag that we are currently running bus_ctl */
11657c478bd9Sstevel@tonic-gate 	hubd->h_bus_ctls++;
11667c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
11697c478bd9Sstevel@tonic-gate 	    "usba_hubdi_bus_ctl:\n\t"
11707c478bd9Sstevel@tonic-gate 	    "dip=0x%p, rdip=0x%p, op=0x%x, arg=0x%p",
1171112116d8Sfb209375 	    (void *)dip, (void *)rdip, op, arg);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	switch (op) {
11747c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
11757c478bd9Sstevel@tonic-gate 		as = (struct attachspec *)arg;
11767c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, rdip);
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		/* there is nothing to do at resume time */
11797c478bd9Sstevel@tonic-gate 		if (as->cmd == DDI_RESUME) {
11807c478bd9Sstevel@tonic-gate 			break;
11817c478bd9Sstevel@tonic-gate 		}
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 		/* serialize access */
11843fe80ca4SDan Cross 		ndi_devi_enter(hubd->h_dip);
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		switch (as->when) {
11877c478bd9Sstevel@tonic-gate 		case DDI_PRE:
11887c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
11897c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1190112116d8Sfb209375 			    (void *)rdip, port);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
11937c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] |= HUBD_CHILD_ATTACHING;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 			/* Go busy here.  Matching idle is DDI_POST case. */
11967c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, dip, 0);
11977c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 			/*
12007c478bd9Sstevel@tonic-gate 			 * if we suspended the port previously
12017c478bd9Sstevel@tonic-gate 			 * because child went to low power state, and
12027c478bd9Sstevel@tonic-gate 			 * someone unloaded the driver, the port would
12037c478bd9Sstevel@tonic-gate 			 * still be suspended and needs to be resumed
12047c478bd9Sstevel@tonic-gate 			 */
12057c478bd9Sstevel@tonic-gate 			rval = hubd_resume_port(hubd, port);
12067c478bd9Sstevel@tonic-gate 			if (rval == USB_SUCCESS) {
12077c478bd9Sstevel@tonic-gate 				retval = DDI_SUCCESS;
12087c478bd9Sstevel@tonic-gate 			}
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 			break;
12117c478bd9Sstevel@tonic-gate 		case DDI_POST:
12127c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12137c478bd9Sstevel@tonic-gate 			    "DDI_POST DDI_CTLOPS_ATTACH: dip=%p, port=%d",
1214112116d8Sfb209375 			    (void *)rdip, port);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12177c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_ATTACHING;
12187c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 			hubd_post_attach(hubd, port, (struct attachspec *)arg);
12217c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12227c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 			/* Matching idle call for DDI_PRE busy call. */
12257c478bd9Sstevel@tonic-gate 			(void) hubd_pm_idle_component(hubd, dip, 0);
12267c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12277c478bd9Sstevel@tonic-gate 		}
12283fe80ca4SDan Cross 		ndi_devi_exit(hubd->h_dip);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 		break;
12317c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
12327c478bd9Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
12337c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, rdip);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 		/* there is nothing to do at suspend time */
12367c478bd9Sstevel@tonic-gate 		if (ds->cmd == DDI_SUSPEND) {
12377c478bd9Sstevel@tonic-gate 			break;
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		/* serialize access */
12413fe80ca4SDan Cross 		ndi_devi_enter(hubd->h_dip);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 		switch (ds->when) {
12447c478bd9Sstevel@tonic-gate 		case DDI_PRE:
12457c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12467c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_DETACH: dip=%p port=%d",
1247112116d8Sfb209375 			    (void *)rdip, port);
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12507c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] |= HUBD_CHILD_DETACHING;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 			/* Go busy here.  Matching idle is DDI_POST case. */
12537c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, dip, 0);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12567c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 			break;
12597c478bd9Sstevel@tonic-gate 		case DDI_POST:
12607c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
12617c478bd9Sstevel@tonic-gate 			    "DDI_POST DDI_CTLOPS_DETACH: dip=%p port=%d",
1262112116d8Sfb209375 			    (void *)rdip, port);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12657c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_DETACHING;
12667c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 			/* Matching idle call for DDI_PRE busy call. */
12697c478bd9Sstevel@tonic-gate 			hubd_post_detach(hubd, port, (struct detachspec *)arg);
12707c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
12717c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
12727c478bd9Sstevel@tonic-gate 			(void) hubd_pm_idle_component(hubd, dip, 0);
12737c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 			break;
12767c478bd9Sstevel@tonic-gate 		}
12773fe80ca4SDan Cross 		ndi_devi_exit(hubd->h_dip);
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 		break;
12807c478bd9Sstevel@tonic-gate 	default:
12817c478bd9Sstevel@tonic-gate 		retval = usba_bus_ctl(root_hub_dip, rdip, op, arg, result);
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	/* decrement bus_ctls count */
12857c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
12867c478bd9Sstevel@tonic-gate 	hubd->h_bus_ctls--;
12877c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_bus_ctls >= 0);
12887c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	return (retval);
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
129340482326SVincent Wang /*
129440482326SVincent Wang  * hubd_config_one:
129540482326SVincent Wang  *	enumerate one child according to 'port'
129640482326SVincent Wang  */
129740482326SVincent Wang 
129840482326SVincent Wang static boolean_t
hubd_config_one(hubd_t * hubd,int port)129940482326SVincent Wang hubd_config_one(hubd_t *hubd, int port)
130040482326SVincent Wang {
130140482326SVincent Wang 	dev_info_t	*hdip = hubd->h_dip;
130240482326SVincent Wang 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
130340482326SVincent Wang 	boolean_t	online_child = B_FALSE, found = B_FALSE;
130440482326SVincent Wang 
130540482326SVincent Wang 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
130640482326SVincent Wang 	    "hubd_config_one:  started, hubd_reset_port = 0x%x", port);
130740482326SVincent Wang 
130840482326SVincent Wang 	ndi_hold_devi(hdip); /* so we don't race with detach */
130940482326SVincent Wang 
131040482326SVincent Wang 	/*
131140482326SVincent Wang 	 * this ensures one config activity per system at a time.
131240482326SVincent Wang 	 * we enter the parent PCI node to have this serialization.
131340482326SVincent Wang 	 * this also excludes ioctls and deathrow thread
131440482326SVincent Wang 	 */
13153fe80ca4SDan Cross 	ndi_devi_enter(ddi_get_parent(rh_dip));
13163fe80ca4SDan Cross 	ndi_devi_enter(rh_dip);
131740482326SVincent Wang 
131840482326SVincent Wang 	/* exclude other threads */
13193fe80ca4SDan Cross 	ndi_devi_enter(hdip);
132040482326SVincent Wang 	mutex_enter(HUBD_MUTEX(hubd));
132140482326SVincent Wang 
1322e4c316c4SVincent Wang 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
1323e4c316c4SVincent Wang 
132440482326SVincent Wang 	if (!hubd->h_children_dips[port]) {
1325993e3fafSRobert Mustacchi 		uint16_t	status, change;
132640482326SVincent Wang 
132740482326SVincent Wang 		(void) hubd_determine_port_status(hubd, port,
1328993e3fafSRobert Mustacchi 		    &status, &change, NULL, HUBD_ACK_ALL_CHANGES);
132940482326SVincent Wang 
133040482326SVincent Wang 		if (status & PORT_STATUS_CCS) {
133140482326SVincent Wang 			online_child |=	(hubd_handle_port_connect(hubd,
133240482326SVincent Wang 			    port) == USB_SUCCESS);
133340482326SVincent Wang 			found = online_child;
133440482326SVincent Wang 		}
133540482326SVincent Wang 	} else {
133640482326SVincent Wang 		found = B_TRUE;
133740482326SVincent Wang 	}
133840482326SVincent Wang 
133940482326SVincent Wang 	mutex_exit(HUBD_MUTEX(hubd));
134040482326SVincent Wang 
13413fe80ca4SDan Cross 	ndi_devi_exit(hdip);
13423fe80ca4SDan Cross 	ndi_devi_exit(rh_dip);
13433fe80ca4SDan Cross 	ndi_devi_exit(ddi_get_parent(rh_dip));
134440482326SVincent Wang 
134540482326SVincent Wang 	if (online_child) {
134640482326SVincent Wang 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
134740482326SVincent Wang 		    "hubd_config_one: onlining child");
134840482326SVincent Wang 
134940482326SVincent Wang 		(void) ndi_devi_online(hubd->h_dip, 0);
135040482326SVincent Wang 	}
135140482326SVincent Wang 
135240482326SVincent Wang 	mutex_enter(HUBD_MUTEX(hubd));
135340482326SVincent Wang 
135440482326SVincent Wang 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
135540482326SVincent Wang 
135640482326SVincent Wang 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
135740482326SVincent Wang 	    "hubd_config_one: exit");
135840482326SVincent Wang 
135940482326SVincent Wang 	mutex_exit(HUBD_MUTEX(hubd));
136040482326SVincent Wang 
136140482326SVincent Wang 	ndi_rele_devi(hdip);
136240482326SVincent Wang 
136340482326SVincent Wang 	return (found);
136440482326SVincent Wang }
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate /*
13677c478bd9Sstevel@tonic-gate  * bus enumeration entry points
13687c478bd9Sstevel@tonic-gate  */
13697c478bd9Sstevel@tonic-gate static int
hubd_bus_config(dev_info_t * dip,uint_t flag,ddi_bus_config_op_t op,void * arg,dev_info_t ** child)13707c478bd9Sstevel@tonic-gate hubd_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
13717c478bd9Sstevel@tonic-gate     void *arg, dev_info_t **child)
13727c478bd9Sstevel@tonic-gate {
13737c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = hubd_get_soft_state(dip);
13743fe80ca4SDan Cross 	int	rval;
137540482326SVincent Wang 	long port;
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
13787c478bd9Sstevel@tonic-gate 	    "hubd_bus_config: op=%d", op);
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	if (hubdi_bus_config_debug) {
13817c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
13827c478bd9Sstevel@tonic-gate 	}
13837c478bd9Sstevel@tonic-gate 
138440482326SVincent Wang 	if (op == BUS_CONFIG_ONE) {
138540482326SVincent Wang 		boolean_t found;
1386d724deadSguoqing zhu - Sun Microsystems - Beijing China 		char cname[80];
1387d724deadSguoqing zhu - Sun Microsystems - Beijing China 		char *name, *addr;
1388d724deadSguoqing zhu - Sun Microsystems - Beijing China 
138940482326SVincent Wang 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
139040482326SVincent Wang 		    "hubd_bus_config: op=%d (BUS_CONFIG_ONE)", op);
139140482326SVincent Wang 
1392d724deadSguoqing zhu - Sun Microsystems - Beijing China 		(void) snprintf(cname, 80, "%s", (char *)arg);
1393d724deadSguoqing zhu - Sun Microsystems - Beijing China 		/* split name into "name@addr" parts */
1394d724deadSguoqing zhu - Sun Microsystems - Beijing China 		i_ddi_parse_name(cname, &name, &addr, NULL);
139540482326SVincent Wang 		if (addr && *addr) {
139640482326SVincent Wang 			(void) ddi_strtol(addr, NULL, 16, &port);
139740482326SVincent Wang 		} else {
139840482326SVincent Wang 			return (NDI_FAILURE);
1399d724deadSguoqing zhu - Sun Microsystems - Beijing China 		}
14005321cfb7Spengcheng chen - Sun Microsystems - Beijing China 
140140482326SVincent Wang 		found = hubd_config_one(hubd, port);
140240482326SVincent Wang 
1403d724deadSguoqing zhu - Sun Microsystems - Beijing China 		if (found == 0) {
1404d724deadSguoqing zhu - Sun Microsystems - Beijing China 			return (NDI_FAILURE);
1405d724deadSguoqing zhu - Sun Microsystems - Beijing China 		}
1406d724deadSguoqing zhu - Sun Microsystems - Beijing China 
14077c478bd9Sstevel@tonic-gate 	}
14083fe80ca4SDan Cross 	ndi_devi_enter(hubd->h_dip);
14097c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
14103fe80ca4SDan Cross 	ndi_devi_exit(hubd->h_dip);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	return (rval);
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate static int
hubd_bus_unconfig(dev_info_t * dip,uint_t flag,ddi_bus_config_op_t op,void * arg)14177c478bd9Sstevel@tonic-gate hubd_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
14187c478bd9Sstevel@tonic-gate     void *arg)
14197c478bd9Sstevel@tonic-gate {
14207c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = hubd_get_soft_state(dip);
14217c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip;
14227c478bd9Sstevel@tonic-gate 	usb_port_t	port;
14237c478bd9Sstevel@tonic-gate 	int		rval;
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
14267c478bd9Sstevel@tonic-gate 	    "hubd_bus_unconfig: op=%d", op);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	if (hubdi_bus_config_debug) {
14297c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
14307c478bd9Sstevel@tonic-gate 	}
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	if ((op == BUS_UNCONFIG_ALL) && (flag & NDI_AUTODETACH) == 0) {
14337c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_REMOVE;
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	/* serialize access */
14373fe80ca4SDan Cross 	ndi_devi_enter(dip);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	/* logically zap children's list */
14427c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
1443993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
14447c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= HUBD_CHILD_ZAP;
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	/* fill in what's left */
14497c478bd9Sstevel@tonic-gate 	for (cdip = ddi_get_child(dip); cdip;
14507c478bd9Sstevel@tonic-gate 	    cdip = ddi_get_next_sibling(cdip)) {
14517c478bd9Sstevel@tonic-gate 		usba_device_t *usba_device = usba_get_usba_device(cdip);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 		if (usba_device == NULL) {
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 			continue;
14567c478bd9Sstevel@tonic-gate 		}
14577c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
14587c478bd9Sstevel@tonic-gate 		port = usba_device->usb_port;
14597c478bd9Sstevel@tonic-gate 		hubd->h_children_dips[port] = cdip;
14607c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
14617c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
14627c478bd9Sstevel@tonic-gate 	}
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	/* physically zap the children we didn't find */
14657c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
1466993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
14677c478bd9Sstevel@tonic-gate 		if (hubd->h_port_state[port] &	HUBD_CHILD_ZAP) {
14687c478bd9Sstevel@tonic-gate 			/* zap the dip and usba_device structure as well */
14697c478bd9Sstevel@tonic-gate 			hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
14707c478bd9Sstevel@tonic-gate 			hubd->h_children_dips[port] = NULL;
14717c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &= ~HUBD_CHILD_ZAP;
14727c478bd9Sstevel@tonic-gate 		}
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
14757c478bd9Sstevel@tonic-gate 
14763fe80ca4SDan Cross 	ndi_devi_exit(dip);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
14797c478bd9Sstevel@tonic-gate 	    "hubd_bus_unconfig: rval=%d", rval);
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	return (rval);
14827c478bd9Sstevel@tonic-gate }
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate /* bus_power entry point */
14867c478bd9Sstevel@tonic-gate static int
hubd_bus_power(dev_info_t * dip,void * impl_arg,pm_bus_power_op_t op,void * arg,void * result)14877c478bd9Sstevel@tonic-gate hubd_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
14887c478bd9Sstevel@tonic-gate     void *arg, void *result)
14897c478bd9Sstevel@tonic-gate {
14907c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
14917c478bd9Sstevel@tonic-gate 	int		rval, pwrup_res;
14927c478bd9Sstevel@tonic-gate 	usb_port_t	port;
14937c478bd9Sstevel@tonic-gate 	int		retval = DDI_FAILURE;
14947c478bd9Sstevel@tonic-gate 	pm_bp_child_pwrchg_t	*bpc;
14957c478bd9Sstevel@tonic-gate 	pm_bp_nexus_pwrup_t	bpn;
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUBDI, hubd->h_log_handle,
15007c478bd9Sstevel@tonic-gate 	    "hubd_bus_power: dip=%p, impl_arg=%p, power_op=%d, arg=%p, "
1501112116d8Sfb209375 	    "result=%d\n", (void *)dip, impl_arg, op, arg, *(int *)result);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	bpc = (pm_bp_child_pwrchg_t *)arg;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
15067c478bd9Sstevel@tonic-gate 	hubd->h_bus_pwr++;
15077c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	switch (op) {
15107c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
15117c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, bpc->bpc_dip);
15127c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
15137c478bd9Sstevel@tonic-gate 		    "hubd_bus_power: BUS_POWER_PRE_NOTIFICATION, port=%d",
15147c478bd9Sstevel@tonic-gate 		    port);
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 		/* go to full power if we are powered down */
15177c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 		/*
15207c478bd9Sstevel@tonic-gate 		 * If this case completes normally, idle will be in
15217c478bd9Sstevel@tonic-gate 		 * hubd_bus_power / BUS_POWER_POST_NOTIFICATION
15227c478bd9Sstevel@tonic-gate 		 */
15237c478bd9Sstevel@tonic-gate 		hubd_pm_busy_component(hubd, dip, 0);
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		/*
15267c478bd9Sstevel@tonic-gate 		 * raise power only if we have created the components
15277c478bd9Sstevel@tonic-gate 		 * and are currently in low power
15287c478bd9Sstevel@tonic-gate 		 */
15297c478bd9Sstevel@tonic-gate 		if ((hubd->h_dev_state == USB_DEV_PWRED_DOWN) &&
15307c478bd9Sstevel@tonic-gate 		    hubd->h_hubpm->hubp_wakeup_enabled) {
15317c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 			bpn.bpn_comp = 0;
15347c478bd9Sstevel@tonic-gate 			bpn.bpn_dip = dip;
15357c478bd9Sstevel@tonic-gate 			bpn.bpn_level = USB_DEV_OS_FULL_PWR;
15367c478bd9Sstevel@tonic-gate 			bpn.bpn_private = bpc->bpc_private;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 			rval = pm_busop_bus_power(dip, impl_arg,
15397c478bd9Sstevel@tonic-gate 			    BUS_POWER_NEXUS_PWRUP, (void *)&bpn,
15407c478bd9Sstevel@tonic-gate 			    (void *)&pwrup_res);
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 			if (rval != DDI_SUCCESS || pwrup_res != DDI_SUCCESS) {
15437c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
15447c478bd9Sstevel@tonic-gate 				hubd_pm_idle_component(hubd, dip, 0);
15457c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 				break;
15487c478bd9Sstevel@tonic-gate 			}
15497c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 		/* indicate that child is changing power level */
15537c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= HUBD_CHILD_PWRLVL_CHNG;
15547c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		if ((bpc->bpc_olevel == 0) &&
15577c478bd9Sstevel@tonic-gate 		    (bpc->bpc_nlevel > bpc->bpc_olevel)) {
15587c478bd9Sstevel@tonic-gate 			/*
15597c478bd9Sstevel@tonic-gate 			 * this child is transitioning from power off
15607c478bd9Sstevel@tonic-gate 			 * to power on state - resume port
15617c478bd9Sstevel@tonic-gate 			 */
15627c478bd9Sstevel@tonic-gate 			rval = hubd_resume_port(hubd, port);
15637c478bd9Sstevel@tonic-gate 			if (rval == USB_SUCCESS) {
15647c478bd9Sstevel@tonic-gate 				retval = DDI_SUCCESS;
15657c478bd9Sstevel@tonic-gate 			} else {
15667c478bd9Sstevel@tonic-gate 				/* reset this flag on failure */
15677c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
15687c478bd9Sstevel@tonic-gate 				hubd->h_port_state[port] &=
15697c478bd9Sstevel@tonic-gate 				    ~HUBD_CHILD_PWRLVL_CHNG;
15707c478bd9Sstevel@tonic-gate 				hubd_pm_idle_component(hubd, dip, 0);
15717c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
15727c478bd9Sstevel@tonic-gate 			}
15737c478bd9Sstevel@tonic-gate 		} else {
15747c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		break;
15787c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
15797c478bd9Sstevel@tonic-gate 		port = hubd_child_dip2port(hubd, bpc->bpc_dip);
15807c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
15817c478bd9Sstevel@tonic-gate 		    "hubd_bus_power: BUS_POWER_POST_NOTIFICATION, port=%d",
15827c478bd9Sstevel@tonic-gate 		    port);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
15857c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~HUBD_CHILD_PWRLVL_CHNG;
15867c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 		/* record child's pwr and suspend port if required */
15897c478bd9Sstevel@tonic-gate 		rval = hubd_post_power(hubd, port, bpc, *(int *)result);
15907c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
15937c478bd9Sstevel@tonic-gate 		}
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 		/*
15987c478bd9Sstevel@tonic-gate 		 * Matching idle for the busy in
15997c478bd9Sstevel@tonic-gate 		 * hubd_bus_power / BUS_POWER_PRE_NOTIFICATION
16007c478bd9Sstevel@tonic-gate 		 */
16017c478bd9Sstevel@tonic-gate 		hubd_pm_idle_component(hubd, dip, 0);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 		break;
16067c478bd9Sstevel@tonic-gate 	default:
16077c478bd9Sstevel@tonic-gate 		retval = pm_busop_bus_power(dip, impl_arg, op, arg, result);
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 		break;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
16137c478bd9Sstevel@tonic-gate 	hubd->h_bus_pwr--;
16147c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	return (retval);
16177c478bd9Sstevel@tonic-gate }
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate /*
16217c478bd9Sstevel@tonic-gate  * functions to handle power transition for OS levels 0 -> 3
16227c478bd9Sstevel@tonic-gate  */
16237c478bd9Sstevel@tonic-gate static int
hubd_pwrlvl0(hubd_t * hubd)16247c478bd9Sstevel@tonic-gate hubd_pwrlvl0(hubd_t *hubd)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	/* We can't power down if hotplug thread is running */
16297c478bd9Sstevel@tonic-gate 	if (hubd->h_hotplug_thread || hubd->h_hubpm->hubp_busy_pm ||
16307c478bd9Sstevel@tonic-gate 	    (hubd_can_suspend(hubd) == USB_FAILURE)) {
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
16337c478bd9Sstevel@tonic-gate 	}
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
16367c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
16377c478bd9Sstevel@tonic-gate 		hubpm = hubd->h_hubpm;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 		/*
16407c478bd9Sstevel@tonic-gate 		 * To avoid race with bus_power pre_notify on check over
16417c478bd9Sstevel@tonic-gate 		 * dev_state, we need to correctly set the dev state
16427c478bd9Sstevel@tonic-gate 		 * before the mutex is dropped in stop polling.
16437c478bd9Sstevel@tonic-gate 		 */
16447c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_PWRED_DOWN;
16457c478bd9Sstevel@tonic-gate 		hubpm->hubp_current_power = USB_DEV_OS_PWR_OFF;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 		/*
16487c478bd9Sstevel@tonic-gate 		 * if we are the root hub, do not stop polling
16497c478bd9Sstevel@tonic-gate 		 * otherwise, we will never see a resume
16507c478bd9Sstevel@tonic-gate 		 */
16517c478bd9Sstevel@tonic-gate 		if (usba_is_root_hub(hubd->h_dip)) {
16527c478bd9Sstevel@tonic-gate 			/* place holder to implement Global Suspend */
16537c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
16547c478bd9Sstevel@tonic-gate 			    "Global Suspend: Not Yet Implemented");
16557c478bd9Sstevel@tonic-gate 		} else {
16567c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
16577c478bd9Sstevel@tonic-gate 		}
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 		/* Issue USB D3 command to the device here */
16607c478bd9Sstevel@tonic-gate 		(void) usb_set_device_pwrlvl3(hubd->h_dip);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 		break;
16637c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
16647c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
16657c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
16667c478bd9Sstevel@tonic-gate 	default:
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 		break;
16697c478bd9Sstevel@tonic-gate 	}
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
16727c478bd9Sstevel@tonic-gate }
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate /* ARGSUSED */
16767c478bd9Sstevel@tonic-gate static int
hubd_pwrlvl1(hubd_t * hubd)16777c478bd9Sstevel@tonic-gate hubd_pwrlvl1(hubd_t *hubd)
16787c478bd9Sstevel@tonic-gate {
16797c478bd9Sstevel@tonic-gate 	/* Issue USB D2 command to the device here */
16807c478bd9Sstevel@tonic-gate 	(void) usb_set_device_pwrlvl2(hubd->h_dip);
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
16837c478bd9Sstevel@tonic-gate }
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate /* ARGSUSED */
16877c478bd9Sstevel@tonic-gate static int
hubd_pwrlvl2(hubd_t * hubd)16887c478bd9Sstevel@tonic-gate hubd_pwrlvl2(hubd_t *hubd)
16897c478bd9Sstevel@tonic-gate {
16907c478bd9Sstevel@tonic-gate 	/* Issue USB D1 command to the device here */
16917c478bd9Sstevel@tonic-gate 	(void) usb_set_device_pwrlvl1(hubd->h_dip);
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
16947c478bd9Sstevel@tonic-gate }
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate static int
hubd_pwrlvl3(hubd_t * hubd)16987c478bd9Sstevel@tonic-gate hubd_pwrlvl3(hubd_t *hubd)
16997c478bd9Sstevel@tonic-gate {
17007c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
17017c478bd9Sstevel@tonic-gate 	int		rval;
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle, "hubd_pwrlvl3");
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
17067c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
17077c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
17087c478bd9Sstevel@tonic-gate 		ASSERT(hubpm->hubp_current_power == USB_DEV_OS_PWR_OFF);
17097c478bd9Sstevel@tonic-gate 		if (usba_is_root_hub(hubd->h_dip)) {
17107c478bd9Sstevel@tonic-gate 			/* implement global resume here */
17117c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM,
17127c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
17137c478bd9Sstevel@tonic-gate 			    "Global Resume: Not Yet Implemented");
17147c478bd9Sstevel@tonic-gate 		}
17157c478bd9Sstevel@tonic-gate 		/* Issue USB D0 command to the device here */
17167c478bd9Sstevel@tonic-gate 		rval = usb_set_device_pwrlvl0(hubd->h_dip);
17177c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
17187c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_ONLINE;
17197c478bd9Sstevel@tonic-gate 		hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
1720e5815e7aSJosef 'Jeff' Sipek 		hubpm->hubp_time_at_full_power = gethrtime();
17217c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
17247c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
17257c478bd9Sstevel@tonic-gate 		/* we are already in full power */
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
17287c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
17297c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
17307c478bd9Sstevel@tonic-gate 		/*
17317c478bd9Sstevel@tonic-gate 		 * PM framework tries to put you in full power
17327c478bd9Sstevel@tonic-gate 		 * during system shutdown. If we are disconnected
17337c478bd9Sstevel@tonic-gate 		 * return success. Also, we should not change state
17347c478bd9Sstevel@tonic-gate 		 * when we are disconnected or suspended or about to
17357c478bd9Sstevel@tonic-gate 		 * transition to that state
17367c478bd9Sstevel@tonic-gate 		 */
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
17397c478bd9Sstevel@tonic-gate 	default:
17407c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
17417c478bd9Sstevel@tonic-gate 		    "hubd_pwrlvl3: Illegal dev_state=%d", hubd->h_dev_state);
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate }
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate /* power entry point */
17497c478bd9Sstevel@tonic-gate /* ARGSUSED */
17507c478bd9Sstevel@tonic-gate int
usba_hubdi_power(dev_info_t * dip,int comp,int level)17517c478bd9Sstevel@tonic-gate usba_hubdi_power(dev_info_t *dip, int comp, int level)
17527c478bd9Sstevel@tonic-gate {
17537c478bd9Sstevel@tonic-gate 	hubd_t		*hubd;
17547c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
17557c478bd9Sstevel@tonic-gate 	int		retval;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
17587c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HUBDI, hubd->h_log_handle,
17597c478bd9Sstevel@tonic-gate 	    "usba_hubdi_power: level=%d", level);
17607c478bd9Sstevel@tonic-gate 
17613fe80ca4SDan Cross 	ndi_devi_enter(dip);
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
17647c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	/* check if we are transitioning to a legal power level */
17677c478bd9Sstevel@tonic-gate 	if (USB_DEV_PWRSTATE_OK(hubpm->hubp_pwr_states, level)) {
17687c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HUBDI, hubd->h_log_handle,
17697c478bd9Sstevel@tonic-gate 		    "usba_hubdi_power: illegal power level=%d "
17707c478bd9Sstevel@tonic-gate 		    "hubp_pwr_states=0x%x", level, hubpm->hubp_pwr_states);
17717c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
17727c478bd9Sstevel@tonic-gate 
17733fe80ca4SDan Cross 		ndi_devi_exit(dip);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	switch (level) {
17797c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_OFF:
17807c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl0(hubd);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 		break;
17837c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_1:
17847c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl1(hubd);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 		break;
17877c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_2:
17887c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl2(hubd);
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 		break;
17917c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_FULL_PWR:
17927c478bd9Sstevel@tonic-gate 		retval = hubd_pwrlvl3(hubd);
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 		break;
17950d2006e4SRobert Mustacchi 	default:
17960d2006e4SRobert Mustacchi 		retval = USB_FAILURE;
17970d2006e4SRobert Mustacchi 
17980d2006e4SRobert Mustacchi 		break;
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
18017c478bd9Sstevel@tonic-gate 
18023fe80ca4SDan Cross 	ndi_devi_exit(dip);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
18057c478bd9Sstevel@tonic-gate }
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate /* power entry point for the root hub */
18097c478bd9Sstevel@tonic-gate int
usba_hubdi_root_hub_power(dev_info_t * dip,int comp,int level)18107c478bd9Sstevel@tonic-gate usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level)
18117c478bd9Sstevel@tonic-gate {
18127c478bd9Sstevel@tonic-gate 	return (usba_hubdi_power(dip, comp, level));
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate /*
18177c478bd9Sstevel@tonic-gate  * standard driver entry points support code
18187c478bd9Sstevel@tonic-gate  */
18197c478bd9Sstevel@tonic-gate int
usba_hubdi_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)18207c478bd9Sstevel@tonic-gate usba_hubdi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
18217c478bd9Sstevel@tonic-gate {
18227c478bd9Sstevel@tonic-gate 	int			instance = ddi_get_instance(dip);
18237c478bd9Sstevel@tonic-gate 	hubd_t			*hubd = NULL;
18247c478bd9Sstevel@tonic-gate 	int			i, rval;
18257c478bd9Sstevel@tonic-gate 	int			minor;
1826112cd14aSqz150045 	uint8_t			ports_count;
18277c478bd9Sstevel@tonic-gate 	char			*log_name = NULL;
18287c478bd9Sstevel@tonic-gate 	const char		*root_hub_drvname;
18297c478bd9Sstevel@tonic-gate 	usb_ep_data_t		*ep_data;
18307c478bd9Sstevel@tonic-gate 	usba_device_t		*child_ud = NULL;
18317c478bd9Sstevel@tonic-gate 	usb_dev_descr_t		*usb_dev_descr;
18327c478bd9Sstevel@tonic-gate 	usb_port_status_t	parent_port_status, child_port_status;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubdi_log_handle,
18357c478bd9Sstevel@tonic-gate 	    "hubd_attach instance %d, cmd=0x%x", instance, cmd);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	switch (cmd) {
18387c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 		break;
18417c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
18427c478bd9Sstevel@tonic-gate 		hubd_cpr_resume(dip);
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
18457c478bd9Sstevel@tonic-gate 	default:
18467c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
18477c478bd9Sstevel@tonic-gate 	}
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	/*
18507c478bd9Sstevel@tonic-gate 	 * Allocate softc information.
18517c478bd9Sstevel@tonic-gate 	 */
18527c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
18537c478bd9Sstevel@tonic-gate 		/* soft state has already been allocated */
18547c478bd9Sstevel@tonic-gate 		hubd = hubd_get_soft_state(dip);
18557c478bd9Sstevel@tonic-gate 		minor = HUBD_IS_ROOT_HUB;
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 		/* generate readable labels for different root hubs */
18587c478bd9Sstevel@tonic-gate 		root_hub_drvname = ddi_driver_name(dip);
1859993e3fafSRobert Mustacchi 		if (strcmp(root_hub_drvname, "xhci") == 0) {
1860993e3fafSRobert Mustacchi 			log_name = "xusb";
1861993e3fafSRobert Mustacchi 		} else if (strcmp(root_hub_drvname, "ehci") == 0) {
18627c478bd9Sstevel@tonic-gate 			log_name = "eusb";
18637c478bd9Sstevel@tonic-gate 		} else if (strcmp(root_hub_drvname, "uhci") == 0) {
18647c478bd9Sstevel@tonic-gate 			log_name = "uusb";
18657c478bd9Sstevel@tonic-gate 		} else {
18667c478bd9Sstevel@tonic-gate 			/* std. for ohci */
18677c478bd9Sstevel@tonic-gate 			log_name = "usb";
18687c478bd9Sstevel@tonic-gate 		}
18697c478bd9Sstevel@tonic-gate 	} else {
18707c478bd9Sstevel@tonic-gate 		rval = ddi_soft_state_zalloc(hubd_statep, instance);
18717c478bd9Sstevel@tonic-gate 		minor = 0;
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 		if (rval != DDI_SUCCESS) {
18747c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
18757c478bd9Sstevel@tonic-gate 			    "cannot allocate soft state (%d)", instance);
18767c478bd9Sstevel@tonic-gate 			goto fail;
18777c478bd9Sstevel@tonic-gate 		}
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 		hubd = hubd_get_soft_state(dip);
18807c478bd9Sstevel@tonic-gate 		if (hubd == NULL) {
18817c478bd9Sstevel@tonic-gate 			goto fail;
18827c478bd9Sstevel@tonic-gate 		}
18837c478bd9Sstevel@tonic-gate 	}
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	hubd->h_log_handle = usb_alloc_log_hdl(dip, log_name, &hubd_errlevel,
18867c478bd9Sstevel@tonic-gate 	    &hubd_errmask, &hubd_instance_debug, 0);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	hubd->h_usba_device	= child_ud = usba_get_usba_device(dip);
18897c478bd9Sstevel@tonic-gate 	hubd->h_dip		= dip;
18907c478bd9Sstevel@tonic-gate 	hubd->h_instance	= instance;
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
18937c478bd9Sstevel@tonic-gate 	child_port_status = child_ud->usb_port_status;
18947c478bd9Sstevel@tonic-gate 	usb_dev_descr = child_ud->usb_dev_descr;
18957c478bd9Sstevel@tonic-gate 	parent_port_status = (child_ud->usb_hs_hub_usba_dev) ?
18967c478bd9Sstevel@tonic-gate 	    child_ud->usb_hs_hub_usba_dev->usb_port_status : 0;
18977c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	if ((child_port_status == USBA_FULL_SPEED_DEV) &&
1900993e3fafSRobert Mustacchi 	    (parent_port_status >= USBA_HIGH_SPEED_DEV) &&
19017c478bd9Sstevel@tonic-gate 	    (usb_dev_descr->bcdUSB == 0x100)) {
19027c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
1903993e3fafSRobert Mustacchi 		    "Use of a USB1.0 hub behind a higher speed port may "
19047c478bd9Sstevel@tonic-gate 		    "cause unexpected failures");
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	hubd->h_pipe_policy.pp_max_async_reqs = 1;
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	/* register with USBA as client driver */
19107c478bd9Sstevel@tonic-gate 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
19117c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19127c478bd9Sstevel@tonic-gate 		    "client attach failed");
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 		goto fail;
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (usb_get_dev_data(dip, &hubd->h_dev_data,
19187c478bd9Sstevel@tonic-gate 	    USB_PARSE_LVL_IF, 0) != USB_SUCCESS) {
19197c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19207c478bd9Sstevel@tonic-gate 		    "cannot get dev_data");
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 		goto fail;
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	if ((ep_data = usb_lookup_ep_data(dip, hubd->h_dev_data,
19267c478bd9Sstevel@tonic-gate 	    hubd->h_dev_data->dev_curr_if, 0, 0,
19277c478bd9Sstevel@tonic-gate 	    (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) {
19287c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19297c478bd9Sstevel@tonic-gate 		    "no interrupt IN endpoint found");
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 		goto fail;
19327c478bd9Sstevel@tonic-gate 	}
19337c478bd9Sstevel@tonic-gate 
1934993e3fafSRobert Mustacchi 	if (usb_ep_xdescr_fill(USB_EP_XDESCR_CURRENT_VERSION, dip, ep_data,
1935993e3fafSRobert Mustacchi 	    &hubd->h_ep1_xdescr) != USB_SUCCESS) {
1936993e3fafSRobert Mustacchi 		goto fail;
1937993e3fafSRobert Mustacchi 	}
1938993e3fafSRobert Mustacchi 
19397c478bd9Sstevel@tonic-gate 	hubd->h_default_pipe = hubd->h_dev_data->dev_default_ph;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	mutex_init(HUBD_MUTEX(hubd), NULL, MUTEX_DRIVER,
19427c478bd9Sstevel@tonic-gate 	    hubd->h_dev_data->dev_iblock_cookie);
19437c478bd9Sstevel@tonic-gate 	cv_init(&hubd->h_cv_reset_port, NULL, CV_DRIVER, NULL);
1944ffcd51f3Slg150142 	cv_init(&hubd->h_cv_hotplug_dev, NULL, CV_DRIVER, NULL);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_LOCKS_DONE;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	usb_free_descr_tree(dip, hubd->h_dev_data);
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	/*
19517c478bd9Sstevel@tonic-gate 	 * register this hub instance with usba
19527c478bd9Sstevel@tonic-gate 	 */
19537c478bd9Sstevel@tonic-gate 	rval = usba_hubdi_register(dip, 0);
19547c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
1955d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19567c478bd9Sstevel@tonic-gate 		    "usba_hubdi_register failed");
19577c478bd9Sstevel@tonic-gate 		goto fail;
19587c478bd9Sstevel@tonic-gate 	}
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
19617c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_HUBDI_REGISTERED;
19627c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_ONLINE;
19637c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	/* now create components to power manage this device */
19667c478bd9Sstevel@tonic-gate 	hubd_create_pm_components(dip, hubd);
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * Event handling: definition and registration
19707c478bd9Sstevel@tonic-gate 	 *
19717c478bd9Sstevel@tonic-gate 	 * first the  definition:
19727c478bd9Sstevel@tonic-gate 	 * get event handle
19737c478bd9Sstevel@tonic-gate 	 */
19747c478bd9Sstevel@tonic-gate 	(void) ndi_event_alloc_hdl(dip, 0, &hubd->h_ndi_event_hdl, NDI_SLEEP);
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	/* bind event set to the handle */
19777c478bd9Sstevel@tonic-gate 	if (ndi_event_bind_set(hubd->h_ndi_event_hdl, &hubd_ndi_events,
19787c478bd9Sstevel@tonic-gate 	    NDI_SLEEP)) {
19797c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
19807c478bd9Sstevel@tonic-gate 		    "binding event set failed");
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		goto fail;
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	/* event registration */
19867c478bd9Sstevel@tonic-gate 	if (hubd_register_events(hubd) != USB_SUCCESS) {
1987d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
19887c478bd9Sstevel@tonic-gate 		    "hubd_register_events failed");
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 		goto fail;
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
19947c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_EVENTS_REGISTERED;
19957c478bd9Sstevel@tonic-gate 
1996993e3fafSRobert Mustacchi 	if (hubd_get_hub_descriptor(hubd) != USB_SUCCESS) {
199735f36846Ssl147100 		mutex_exit(HUBD_MUTEX(hubd));
199835f36846Ssl147100 
199935f36846Ssl147100 		goto fail;
200035f36846Ssl147100 	}
200135f36846Ssl147100 
2002993e3fafSRobert Mustacchi 	/*
2003993e3fafSRobert Mustacchi 	 * Now that we have our descriptors, we need to give the host controller
2004993e3fafSRobert Mustacchi 	 * a chance to properly configure the device if needed (this is required
2005993e3fafSRobert Mustacchi 	 * for xHCI).
2006993e3fafSRobert Mustacchi 	 *
2007993e3fafSRobert Mustacchi 	 * Note, if anyone ever adds support for using the Multi TT mode for USB
2008993e3fafSRobert Mustacchi 	 * 2 High speed Hubs, the xhci driver will need to be updated and that
2009993e3fafSRobert Mustacchi 	 * will need to be done before this is called.
2010993e3fafSRobert Mustacchi 	 */
2011993e3fafSRobert Mustacchi 	if (hubd->h_usba_device->usb_hcdi_ops->usba_hcdi_hub_update != NULL &&
2012993e3fafSRobert Mustacchi 	    !usba_is_root_hub(dip)) {
2013993e3fafSRobert Mustacchi 		int ret;
2014993e3fafSRobert Mustacchi 		uint8_t chars;
2015993e3fafSRobert Mustacchi 		usba_device_t *ud = hubd->h_usba_device;
2016993e3fafSRobert Mustacchi 
2017993e3fafSRobert Mustacchi 		chars = (hubd->h_hub_chars & HUB_CHARS_TT_THINK_TIME) >>
2018993e3fafSRobert Mustacchi 		    HUB_CHARS_TT_SHIFT;
2019993e3fafSRobert Mustacchi 		ret = ud->usb_hcdi_ops->usba_hcdi_hub_update(ud,
2020993e3fafSRobert Mustacchi 		    hubd->h_nports, chars);
2021993e3fafSRobert Mustacchi 		if (ret != USB_SUCCESS) {
2022993e3fafSRobert Mustacchi 			mutex_exit(HUBD_MUTEX(hubd));
2023993e3fafSRobert Mustacchi 			goto fail;
2024993e3fafSRobert Mustacchi 		}
2025993e3fafSRobert Mustacchi 	}
2026993e3fafSRobert Mustacchi 
2027993e3fafSRobert Mustacchi 	if (hubd_set_hub_depth(hubd) != USB_SUCCESS) {
2028993e3fafSRobert Mustacchi 
2029993e3fafSRobert Mustacchi 		goto fail;
2030993e3fafSRobert Mustacchi 	}
2031993e3fafSRobert Mustacchi 
203235f36846Ssl147100 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
203335f36846Ssl147100 	    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
203435f36846Ssl147100 	    "hub-ignore-power-budget") == 1) {
203535f36846Ssl147100 		hubd->h_ignore_pwr_budget = B_TRUE;
203635f36846Ssl147100 	} else {
203735f36846Ssl147100 		hubd->h_ignore_pwr_budget = B_FALSE;
203835f36846Ssl147100 
203935f36846Ssl147100 		/* initialize hub power budget variables */
204035f36846Ssl147100 		if (hubd_init_power_budget(hubd) != USB_SUCCESS) {
204135f36846Ssl147100 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
204235f36846Ssl147100 			    "hubd_init_power_budget failed");
204335f36846Ssl147100 			mutex_exit(HUBD_MUTEX(hubd));
204435f36846Ssl147100 
204535f36846Ssl147100 			goto fail;
204635f36846Ssl147100 		}
204735f36846Ssl147100 	}
204835f36846Ssl147100 
20497c478bd9Sstevel@tonic-gate 	/* initialize and create children */
20507c478bd9Sstevel@tonic-gate 	if (hubd_check_ports(hubd) != USB_SUCCESS) {
2051d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
20527c478bd9Sstevel@tonic-gate 		    "hubd_check_ports failed");
20537c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 		goto fail;
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	/*
20597c478bd9Sstevel@tonic-gate 	 * create cfgadm nodes
20607c478bd9Sstevel@tonic-gate 	 */
20617c478bd9Sstevel@tonic-gate 	hubd->h_ancestry_str = (char *)kmem_zalloc(HUBD_APID_NAMELEN, KM_SLEEP);
20627c478bd9Sstevel@tonic-gate 	hubd_get_ancestry_str(hubd);
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
2065993e3fafSRobert Mustacchi 	    "#ports=0x%x", hubd->h_nports);
20667c478bd9Sstevel@tonic-gate 
2067993e3fafSRobert Mustacchi 	for (i = 1; i <= hubd->h_nports; i++) {
20687c478bd9Sstevel@tonic-gate 		char ap_name[HUBD_APID_NAMELEN];
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 		(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
20717c478bd9Sstevel@tonic-gate 		    hubd->h_ancestry_str, i);
20727c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
20737c478bd9Sstevel@tonic-gate 		    "ap_name=%s", ap_name);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, ap_name, S_IFCHR, instance,
20767c478bd9Sstevel@tonic-gate 		    DDI_NT_USB_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
2077d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
20787c478bd9Sstevel@tonic-gate 			    "cannot create attachment point node (%d)",
20797c478bd9Sstevel@tonic-gate 			    instance);
20807c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 			goto fail;
20837c478bd9Sstevel@tonic-gate 		}
20847c478bd9Sstevel@tonic-gate 	}
2085112cd14aSqz150045 
2086993e3fafSRobert Mustacchi 	ports_count = hubd->h_nports;
20877c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	/* create minor nodes */
20907c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "hubd", S_IFCHR,
20917c478bd9Sstevel@tonic-gate 	    instance | minor, DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
20927c478bd9Sstevel@tonic-gate 
2093d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
20947c478bd9Sstevel@tonic-gate 		    "cannot create devctl minor node (%d)", instance);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 		goto fail;
20977c478bd9Sstevel@tonic-gate 	}
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
21007c478bd9Sstevel@tonic-gate 	hubd->h_init_state |= HUBD_MINOR_NODE_CREATED;
21017c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
21027c478bd9Sstevel@tonic-gate 
2103112cd14aSqz150045 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2104112cd14aSqz150045 	    "usb-port-count", ports_count) != DDI_PROP_SUCCESS) {
2105112cd14aSqz150045 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
2106ff0e937bSRaymond Chen 		    "usb-port-count update failed");
2107112cd14aSqz150045 	}
2108112cd14aSqz150045 
21097c478bd9Sstevel@tonic-gate 	/*
21107c478bd9Sstevel@tonic-gate 	 * host controller driver has already reported this dev
21117c478bd9Sstevel@tonic-gate 	 * if we are the root hub
21127c478bd9Sstevel@tonic-gate 	 */
21137c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
21147c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
21157c478bd9Sstevel@tonic-gate 	}
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	/* enable deathrow thread */
21187c478bd9Sstevel@tonic-gate 	hubd->h_cleanup_enabled = B_TRUE;
21197c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
21207c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, dip, 0);
21217c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate fail:
21267c478bd9Sstevel@tonic-gate 	{
21277c478bd9Sstevel@tonic-gate 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
21287c478bd9Sstevel@tonic-gate 
21298668df41Slg150142 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
21307c478bd9Sstevel@tonic-gate 		    "cannot attach %s", ddi_pathname(dip, pathname));
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
21337c478bd9Sstevel@tonic-gate 	}
21347c478bd9Sstevel@tonic-gate 
21350d2006e4SRobert Mustacchi 	if (hubd != NULL) {
21367c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
21377c478bd9Sstevel@tonic-gate 		hubd_pm_idle_component(hubd, dip, 0);
21387c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 		rval = hubd_cleanup(dip, hubd);
21417c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
2142d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
21437c478bd9Sstevel@tonic-gate 			    "failure to complete cleanup after attach failure");
21447c478bd9Sstevel@tonic-gate 		}
21457c478bd9Sstevel@tonic-gate 	}
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
21487c478bd9Sstevel@tonic-gate }
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate int
usba_hubdi_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)21527c478bd9Sstevel@tonic-gate usba_hubdi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
21537c478bd9Sstevel@tonic-gate {
21547c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = hubd_get_soft_state(dip);
21557c478bd9Sstevel@tonic-gate 	int	rval;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
21587c478bd9Sstevel@tonic-gate 	    "hubd_detach: cmd=0x%x", cmd);
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	switch (cmd) {
21617c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
21627c478bd9Sstevel@tonic-gate 		rval = hubd_cleanup(dip, hubd);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
21657c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
21667c478bd9Sstevel@tonic-gate 		rval = hubd_cpr_suspend(hubd);
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 		return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
21697c478bd9Sstevel@tonic-gate 	default:
21707c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
21717c478bd9Sstevel@tonic-gate 	}
21727c478bd9Sstevel@tonic-gate }
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate /*
21767c478bd9Sstevel@tonic-gate  * hubd_setdevaddr
21777c478bd9Sstevel@tonic-gate  *	set the device addrs on this port
21787c478bd9Sstevel@tonic-gate  */
21797c478bd9Sstevel@tonic-gate static int
hubd_setdevaddr(hubd_t * hubd,usb_port_t port)21807c478bd9Sstevel@tonic-gate hubd_setdevaddr(hubd_t *hubd, usb_port_t port)
21817c478bd9Sstevel@tonic-gate {
21820d2006e4SRobert Mustacchi 	int		rval = USB_FAILURE;
21837c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
21847c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
21857c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t ph;
21867c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip = NULL;
21877c478bd9Sstevel@tonic-gate 	uchar_t		address = 0;
21887c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
21897c478bd9Sstevel@tonic-gate 	int		retry = 0;
21907c478bd9Sstevel@tonic-gate 	long		time_delay;
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
21937c478bd9Sstevel@tonic-gate 	    "hubd_setdevaddr: port=%d", port);
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
21987c478bd9Sstevel@tonic-gate 	address = hubd->h_usba_devices[port]->usb_addr;
21997c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	/* close the default pipe with addr x */
22027c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
22037c478bd9Sstevel@tonic-gate 	ph = usba_get_dflt_pipe_handle(child_dip);
22047c478bd9Sstevel@tonic-gate 	usb_pipe_close(child_dip, ph,
22057c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
22067c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
2209993e3fafSRobert Mustacchi 	 * If the host controller is in charge of addressing, have it do that
2210993e3fafSRobert Mustacchi 	 * now and skip everything else.
2211993e3fafSRobert Mustacchi 	 */
2212993e3fafSRobert Mustacchi 	if (usba_device->usb_hcdi_ops->usba_hcdi_device_address != NULL) {
2213993e3fafSRobert Mustacchi 		mutex_exit(HUBD_MUTEX(hubd));
2214993e3fafSRobert Mustacchi 		rval = usba_device->usb_hcdi_ops->usba_hcdi_device_address(
2215993e3fafSRobert Mustacchi 		    usba_device);
2216993e3fafSRobert Mustacchi 		mutex_enter(HUBD_MUTEX(hubd));
2217993e3fafSRobert Mustacchi 
2218993e3fafSRobert Mustacchi 		usba_clear_data_toggle(usba_device);
2219993e3fafSRobert Mustacchi 		return (rval);
2220993e3fafSRobert Mustacchi 	}
2221993e3fafSRobert Mustacchi 
2222993e3fafSRobert Mustacchi 	/*
22237c478bd9Sstevel@tonic-gate 	 * As this device has been reset, temporarily
22247c478bd9Sstevel@tonic-gate 	 * assign the default address
22257c478bd9Sstevel@tonic-gate 	 */
22267c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
22277c478bd9Sstevel@tonic-gate 	address = usba_device->usb_addr;
22287c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = USBA_DEFAULT_ADDR;
22297c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	time_delay = drv_usectohz(hubd_device_delay / 20);
22347c478bd9Sstevel@tonic-gate 	for (retry = 0; retry < hubd_retry_enumerate; retry++) {
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 		/* open child's default pipe with USBA_DEFAULT_ADDR */
22370d2006e4SRobert Mustacchi 		if ((rval = usb_pipe_open(child_dip, NULL, NULL,
22380d2006e4SRobert Mustacchi 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) !=
22397c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
22407c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
22417c478bd9Sstevel@tonic-gate 			    "hubd_setdevaddr: Unable to open default pipe");
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 			break;
22447c478bd9Sstevel@tonic-gate 		}
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 		/* Set the address of the device */
22477c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
22487c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_HOST_TO_DEV,
22497c478bd9Sstevel@tonic-gate 		    USB_REQ_SET_ADDRESS,	/* bRequest */
22507c478bd9Sstevel@tonic-gate 		    address,			/* wValue */
22517c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
22527c478bd9Sstevel@tonic-gate 		    0,				/* wLength */
22537c478bd9Sstevel@tonic-gate 		    NULL, 0,
22547c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
22557c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
22567c478bd9Sstevel@tonic-gate 			    "hubd_setdevaddr(%d): rval=%d cr=%d cb_fl=0x%x",
22577c478bd9Sstevel@tonic-gate 			    retry, rval, completion_reason, cb_flags);
22587c478bd9Sstevel@tonic-gate 		}
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 		usb_pipe_close(child_dip, ph,
22617c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 			break;
22667c478bd9Sstevel@tonic-gate 		}
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 		delay(time_delay);
22697c478bd9Sstevel@tonic-gate 	}
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	/* Reset to the old address */
22727c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
22737c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = address;
22747c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
22757c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	usba_clear_data_toggle(usba_device);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	return (rval);
22807c478bd9Sstevel@tonic-gate }
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate /*
22847c478bd9Sstevel@tonic-gate  * hubd_setdevconfig
22857c478bd9Sstevel@tonic-gate  *	set the device addrs on this port
22867c478bd9Sstevel@tonic-gate  */
22877c478bd9Sstevel@tonic-gate static void
hubd_setdevconfig(hubd_t * hubd,usb_port_t port)22887c478bd9Sstevel@tonic-gate hubd_setdevconfig(hubd_t *hubd, usb_port_t port)
22897c478bd9Sstevel@tonic-gate {
22907c478bd9Sstevel@tonic-gate 	int			rval;
22917c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
22927c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
22937c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	ph;
22947c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
22957c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device = NULL;
22967c478bd9Sstevel@tonic-gate 	uint16_t		config_value;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
22997c478bd9Sstevel@tonic-gate 	    "hubd_setdevconfig: port=%d", port);
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
23047c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
23057c478bd9Sstevel@tonic-gate 	config_value = hubd->h_usba_devices[port]->usb_cfg_value;
23067c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 	/* open the default control pipe */
23097c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
23107c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) ==
23117c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 		/* Set the default configuration of the device */
23147c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
23157c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_HOST_TO_DEV,
23167c478bd9Sstevel@tonic-gate 		    USB_REQ_SET_CFG,		/* bRequest */
23177c478bd9Sstevel@tonic-gate 		    config_value,		/* wValue */
23187c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
23197c478bd9Sstevel@tonic-gate 		    0,				/* wLength */
23207c478bd9Sstevel@tonic-gate 		    NULL, 0,
23217c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
23227c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
23237c478bd9Sstevel@tonic-gate 			    "hubd_setdevconfig: set device config failed: "
23247c478bd9Sstevel@tonic-gate 			    "cr=%d cb_fl=0x%x rval=%d",
23257c478bd9Sstevel@tonic-gate 			    completion_reason, cb_flags, rval);
23267c478bd9Sstevel@tonic-gate 		}
23277c478bd9Sstevel@tonic-gate 		/*
23287c478bd9Sstevel@tonic-gate 		 * After setting the configuration, we make this default
23297c478bd9Sstevel@tonic-gate 		 * control pipe persistent, so that it gets re-opened
23307c478bd9Sstevel@tonic-gate 		 * on posting a connect event
23317c478bd9Sstevel@tonic-gate 		 */
23327c478bd9Sstevel@tonic-gate 		usba_persistent_pipe_close(usba_device);
23337c478bd9Sstevel@tonic-gate 	} else {
23347c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
23357c478bd9Sstevel@tonic-gate 		    "pipe open fails: rval=%d", rval);
23367c478bd9Sstevel@tonic-gate 	}
23377c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
23387c478bd9Sstevel@tonic-gate }
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23427c478bd9Sstevel@tonic-gate static int
hubd_check_disconnected_ports(dev_info_t * dip,void * arg)23437c478bd9Sstevel@tonic-gate hubd_check_disconnected_ports(dev_info_t *dip, void *arg)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	usb_port_t port;
23467c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
23477c478bd9Sstevel@tonic-gate 	major_t hub_major = ddi_name_to_major("hubd");
2348ff0e937bSRaymond Chen 	major_t hwahc_major = ddi_name_to_major("hwahc");
2349ff0e937bSRaymond Chen 	major_t usbmid_major = ddi_name_to_major("usb_mid");
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	/*
23527c478bd9Sstevel@tonic-gate 	 * make sure dip is a usb hub, major of root hub is HCD
23537c478bd9Sstevel@tonic-gate 	 * major
23547c478bd9Sstevel@tonic-gate 	 */
23557c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
2356ff0e937bSRaymond Chen 		if (ddi_driver_major(dip) == usbmid_major) {
2357ff0e937bSRaymond Chen 			/*
2358ff0e937bSRaymond Chen 			 * need to walk the children since it might be a
2359ff0e937bSRaymond Chen 			 * HWA device
2360ff0e937bSRaymond Chen 			 */
2361ff0e937bSRaymond Chen 
2362ff0e937bSRaymond Chen 			return (DDI_WALK_CONTINUE);
2363ff0e937bSRaymond Chen 		}
2364ff0e937bSRaymond Chen 
2365ff0e937bSRaymond Chen 		/* TODO: DWA device may also need special handling */
2366ff0e937bSRaymond Chen 
2367ff0e937bSRaymond Chen 		if (((ddi_driver_major(dip) != hub_major) &&
2368ff0e937bSRaymond Chen 		    (ddi_driver_major(dip) != hwahc_major)) ||
2369737d277aScth 		    !i_ddi_devi_attached(dip)) {
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 			return (DDI_WALK_PRUNECHILD);
23727c478bd9Sstevel@tonic-gate 		}
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
23767c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
23797c478bd9Sstevel@tonic-gate 	}
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	/* walk child list and remove nodes with flag DEVI_DEVICE_REMOVED */
23823fe80ca4SDan Cross 	ndi_devi_enter(dip);
23837c478bd9Sstevel@tonic-gate 
2384ff0e937bSRaymond Chen 	if (ddi_driver_major(dip) != hwahc_major) {
2385ff0e937bSRaymond Chen 		/* for normal usb hub or root hub */
23867c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
2387993e3fafSRobert Mustacchi 		for (port = 1; port <= hubd->h_nports; port++) {
23887c478bd9Sstevel@tonic-gate 			dev_info_t *cdip = hubd->h_children_dips[port];
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 			if (cdip == NULL || DEVI_IS_DEVICE_REMOVED(cdip) == 0) {
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 				continue;
23937c478bd9Sstevel@tonic-gate 			}
23947c478bd9Sstevel@tonic-gate 
2395ff0e937bSRaymond Chen 			(void) hubd_delete_child(hubd, port, NDI_DEVI_REMOVE,
2396ff0e937bSRaymond Chen 			    B_TRUE);
23977c478bd9Sstevel@tonic-gate 		}
23987c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
2399ff0e937bSRaymond Chen 	} else {
2400ff0e937bSRaymond Chen 		/* for HWA */
2401ff0e937bSRaymond Chen 		if (hubd->h_cleanup_child != NULL) {
2402ff0e937bSRaymond Chen 			if (hubd->h_cleanup_child(dip) != USB_SUCCESS) {
24033fe80ca4SDan Cross 				ndi_devi_exit(dip);
2404ff0e937bSRaymond Chen 
2405ff0e937bSRaymond Chen 				return (DDI_WALK_PRUNECHILD);
2406ff0e937bSRaymond Chen 			}
2407ff0e937bSRaymond Chen 		} else {
24083fe80ca4SDan Cross 			ndi_devi_exit(dip);
2409ff0e937bSRaymond Chen 
2410ff0e937bSRaymond Chen 			return (DDI_WALK_PRUNECHILD);
2411ff0e937bSRaymond Chen 		}
2412ff0e937bSRaymond Chen 	}
2413ff0e937bSRaymond Chen 
24143fe80ca4SDan Cross 	ndi_devi_exit(dip);
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	/* skip siblings of root hub */
24177c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNESIB);
24207c478bd9Sstevel@tonic-gate 	}
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
24237c478bd9Sstevel@tonic-gate }
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate /*
24277c478bd9Sstevel@tonic-gate  * this thread will walk all children under the root hub for this
24287c478bd9Sstevel@tonic-gate  * USB bus instance and attempt to remove them
24297c478bd9Sstevel@tonic-gate  */
24307c478bd9Sstevel@tonic-gate static void
hubd_root_hub_cleanup_thread(void * arg)24317c478bd9Sstevel@tonic-gate hubd_root_hub_cleanup_thread(void *arg)
24327c478bd9Sstevel@tonic-gate {
24337c478bd9Sstevel@tonic-gate 	hubd_t *root_hubd = (hubd_t *)arg;
24347c478bd9Sstevel@tonic-gate 	dev_info_t *rh_dip = root_hubd->h_dip;
24357c478bd9Sstevel@tonic-gate #ifndef __lock_lint
24367c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, HUBD_MUTEX(root_hubd), callb_generic_cpr,
24397c478bd9Sstevel@tonic-gate 	    "USB root hub");
24407c478bd9Sstevel@tonic-gate #endif
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	for (;;) {
24437c478bd9Sstevel@tonic-gate 		/* don't race with detach */
24447c478bd9Sstevel@tonic-gate 		ndi_hold_devi(rh_dip);
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
24477c478bd9Sstevel@tonic-gate 		root_hubd->h_cleanup_needed = 0;
24487c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 		(void) devfs_clean(rh_dip, NULL, 0);
24517c478bd9Sstevel@tonic-gate 
24523fe80ca4SDan Cross 		ndi_devi_enter(ddi_get_parent(rh_dip));
24537c478bd9Sstevel@tonic-gate 		ddi_walk_devs(rh_dip, hubd_check_disconnected_ports,
24547c478bd9Sstevel@tonic-gate 		    NULL);
24557c478bd9Sstevel@tonic-gate #ifdef __lock_lint
24567c478bd9Sstevel@tonic-gate 		(void) hubd_check_disconnected_ports(rh_dip, NULL);
24577c478bd9Sstevel@tonic-gate #endif
24583fe80ca4SDan Cross 		ndi_devi_exit(ddi_get_parent(rh_dip));
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 		/* quit if we are not enabled anymore */
24617c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
24627c478bd9Sstevel@tonic-gate 		if ((root_hubd->h_cleanup_enabled == B_FALSE) ||
24637c478bd9Sstevel@tonic-gate 		    (root_hubd->h_cleanup_needed == B_FALSE)) {
24647c478bd9Sstevel@tonic-gate 			root_hubd->h_cleanup_active = B_FALSE;
24657c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(root_hubd));
24667c478bd9Sstevel@tonic-gate 			ndi_rele_devi(rh_dip);
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 			break;
24697c478bd9Sstevel@tonic-gate 		}
24707c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24717c478bd9Sstevel@tonic-gate 		ndi_rele_devi(rh_dip);
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate #ifndef __lock_lint
24747c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
24757c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
24767c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(hubd_dip_cleanup_delay));
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(root_hubd));
24817c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, HUBD_MUTEX(root_hubd));
24827c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
24837c478bd9Sstevel@tonic-gate #endif
24847c478bd9Sstevel@tonic-gate 	}
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate #ifndef __lock_lint
24877c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(root_hubd));
24887c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
24897c478bd9Sstevel@tonic-gate #endif
24907c478bd9Sstevel@tonic-gate }
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 
2493ff0e937bSRaymond Chen void
hubd_schedule_cleanup(dev_info_t * rh_dip)24947c478bd9Sstevel@tonic-gate hubd_schedule_cleanup(dev_info_t *rh_dip)
24957c478bd9Sstevel@tonic-gate {
2496ff0e937bSRaymond Chen 	hubd_t	*root_hubd;
2497ff0e937bSRaymond Chen 
2498ff0e937bSRaymond Chen 	/*
2499ff0e937bSRaymond Chen 	 * The usb_root_hub_dip pointer for the child hub of the WUSB
2500ff0e937bSRaymond Chen 	 * wire adapter class device points to the wire adapter, not
2501ff0e937bSRaymond Chen 	 * the root hub. Need to find the real root hub dip so that
2502ff0e937bSRaymond Chen 	 * the cleanup thread only starts from the root hub.
2503ff0e937bSRaymond Chen 	 */
2504ff0e937bSRaymond Chen 	while (!usba_is_root_hub(rh_dip)) {
2505ff0e937bSRaymond Chen 		root_hubd = hubd_get_soft_state(rh_dip);
2506ff0e937bSRaymond Chen 		if (root_hubd != NULL) {
2507ff0e937bSRaymond Chen 			rh_dip = root_hubd->h_usba_device->usb_root_hub_dip;
2508ff0e937bSRaymond Chen 			if (rh_dip == NULL) {
2509ff0e937bSRaymond Chen 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2510ff0e937bSRaymond Chen 				    root_hubd->h_log_handle,
2511ff0e937bSRaymond Chen 				    "hubd_schedule_cleanup: null rh dip");
2512ff0e937bSRaymond Chen 
2513ff0e937bSRaymond Chen 				return;
2514ff0e937bSRaymond Chen 			}
2515ff0e937bSRaymond Chen 		} else {
2516ff0e937bSRaymond Chen 			USB_DPRINTF_L2(DPRINT_MASK_ATTA,
2517ff0e937bSRaymond Chen 			    root_hubd->h_log_handle,
2518ff0e937bSRaymond Chen 			    "hubd_schedule_cleanup: cannot find root hub");
2519ff0e937bSRaymond Chen 
2520ff0e937bSRaymond Chen 			return;
2521ff0e937bSRaymond Chen 		}
2522ff0e937bSRaymond Chen 	}
2523ff0e937bSRaymond Chen 	root_hubd = hubd_get_soft_state(rh_dip);
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(root_hubd));
25267c478bd9Sstevel@tonic-gate 	root_hubd->h_cleanup_needed = B_TRUE;
25277c478bd9Sstevel@tonic-gate 	if (root_hubd->h_cleanup_enabled && !(root_hubd->h_cleanup_active)) {
25287c478bd9Sstevel@tonic-gate 		root_hubd->h_cleanup_active = B_TRUE;
25297c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
25307c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, 0,
25317c478bd9Sstevel@tonic-gate 		    hubd_root_hub_cleanup_thread,
25327c478bd9Sstevel@tonic-gate 		    (void *)root_hubd, 0, &p0, TS_RUN,
25337c478bd9Sstevel@tonic-gate 		    minclsyspri);
25347c478bd9Sstevel@tonic-gate 	} else {
25357c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(root_hubd));
25367c478bd9Sstevel@tonic-gate 	}
25377c478bd9Sstevel@tonic-gate }
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate /*
25417c478bd9Sstevel@tonic-gate  * hubd_restore_device_state:
25427c478bd9Sstevel@tonic-gate  *	- set config for the hub
25437c478bd9Sstevel@tonic-gate  *	- power cycle all the ports
25447c478bd9Sstevel@tonic-gate  *	- for each port that was connected
25457c478bd9Sstevel@tonic-gate  *		- reset port
25467c478bd9Sstevel@tonic-gate  *		- assign addrs to the device on this port
25477c478bd9Sstevel@tonic-gate  *	- restart polling
25487c478bd9Sstevel@tonic-gate  *	- reset suspend flag
25497c478bd9Sstevel@tonic-gate  */
25507c478bd9Sstevel@tonic-gate static void
hubd_restore_device_state(dev_info_t * dip,hubd_t * hubd)25517c478bd9Sstevel@tonic-gate hubd_restore_device_state(dev_info_t *dip, hubd_t *hubd)
25527c478bd9Sstevel@tonic-gate {
25537c478bd9Sstevel@tonic-gate 	int		rval;
25547c478bd9Sstevel@tonic-gate 	int		retry;
25557c478bd9Sstevel@tonic-gate 	uint_t		hub_prev_state;
25567c478bd9Sstevel@tonic-gate 	usb_port_t	port;
25577c478bd9Sstevel@tonic-gate 	uint16_t	status;
25587c478bd9Sstevel@tonic-gate 	uint16_t	change;
25597c478bd9Sstevel@tonic-gate 	dev_info_t	*ch_dip;
25607c478bd9Sstevel@tonic-gate 	boolean_t	ehci_root_hub;
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
25637c478bd9Sstevel@tonic-gate 	    "hubd_restore_device_state:");
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
25667c478bd9Sstevel@tonic-gate 	hub_prev_state = hubd->h_dev_state;
25677c478bd9Sstevel@tonic-gate 	ASSERT(hub_prev_state != USB_DEV_PWRED_DOWN);
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 	/* First bring the device to full power */
25707c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, dip, 0);
25717c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip) &&
25767c478bd9Sstevel@tonic-gate 	    (usb_check_same_device(dip, hubd->h_log_handle, USB_LOG_L0,
25777c478bd9Sstevel@tonic-gate 	    DPRINT_MASK_HOTPLUG,
25787c478bd9Sstevel@tonic-gate 	    USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS)) {
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 		/* change the device state to disconnected */
25817c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
25827c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
25837c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
25847c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 		return;
25877c478bd9Sstevel@tonic-gate 	}
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	ehci_root_hub = (strcmp(ddi_driver_name(dip), "ehci") == 0);
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
25927c478bd9Sstevel@tonic-gate 	/* First turn off all port power */
25937c478bd9Sstevel@tonic-gate 	rval = hubd_disable_all_port_power(hubd);
25947c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
25957c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
25967c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state:"
25977c478bd9Sstevel@tonic-gate 		    "turning off port power failed");
25987c478bd9Sstevel@tonic-gate 	}
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	/* Settling time before turning on again */
26017c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
26027c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(hubd_device_delay / 100));
26037c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	/* enable power on all ports so we can see connects */
26067c478bd9Sstevel@tonic-gate 	if (hubd_enable_all_port_power(hubd) != USB_SUCCESS) {
26077c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
26087c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state: turn on port power failed");
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
26117c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
26147c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 		return;
26177c478bd9Sstevel@tonic-gate 	}
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	/*
26207c478bd9Sstevel@tonic-gate 	 * wait at least 3 frames before accessing devices
2621d96925c4SRichard Lowe 	 * (note that delay's minimal time is one clock tick).
26227c478bd9Sstevel@tonic-gate 	 */
26237c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
26247c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
26257c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_HUB_STATE_RECOVER;
26287c478bd9Sstevel@tonic-gate 
2629993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
26307c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
26317c478bd9Sstevel@tonic-gate 		    "hubd_restore_device_state: port=%d", port);
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate 		/*
26347c478bd9Sstevel@tonic-gate 		 * the childen_dips list may have dips that have been
26357c478bd9Sstevel@tonic-gate 		 * already deallocated. we only get a post_detach notification
26367c478bd9Sstevel@tonic-gate 		 * but not a destroy notification
26377c478bd9Sstevel@tonic-gate 		 */
26387c478bd9Sstevel@tonic-gate 		ch_dip = hubd->h_children_dips[port];
26397c478bd9Sstevel@tonic-gate 		if (ch_dip) {
26407c478bd9Sstevel@tonic-gate 			/* get port status */
26417c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
2642993e3fafSRobert Mustacchi 			    &status, &change, NULL, PORT_CHANGE_CSC);
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 			/* check if it is truly connected */
26457c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
26467c478bd9Sstevel@tonic-gate 				/*
26477c478bd9Sstevel@tonic-gate 				 * Now reset port and assign the device
26487c478bd9Sstevel@tonic-gate 				 * its original address
26497c478bd9Sstevel@tonic-gate 				 */
26507c478bd9Sstevel@tonic-gate 				retry = 0;
26517c478bd9Sstevel@tonic-gate 				do {
26527c478bd9Sstevel@tonic-gate 					(void) hubd_reset_port(hubd, port);
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 					/* required for ppx */
26557c478bd9Sstevel@tonic-gate 					(void) hubd_enable_port(hubd, port);
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 					if (retry) {
26587c478bd9Sstevel@tonic-gate 						mutex_exit(HUBD_MUTEX(hubd));
26597c478bd9Sstevel@tonic-gate 						delay(drv_usectohz(
26607c478bd9Sstevel@tonic-gate 						    hubd_device_delay/2));
26617c478bd9Sstevel@tonic-gate 						mutex_enter(HUBD_MUTEX(hubd));
26627c478bd9Sstevel@tonic-gate 					}
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate 					rval = hubd_setdevaddr(hubd, port);
26657c478bd9Sstevel@tonic-gate 					retry++;
26667c478bd9Sstevel@tonic-gate 				} while ((rval != USB_SUCCESS) &&
26677c478bd9Sstevel@tonic-gate 				    (retry < hubd_retry_enumerate));
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 				hubd_setdevconfig(hubd, port);
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 				if (hub_prev_state == USB_DEV_DISCONNECTED) {
26727c478bd9Sstevel@tonic-gate 					/* post a connect event */
26737c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
26747c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
26757c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_INSERTION);
26767c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
26777c478bd9Sstevel@tonic-gate 				} else {
26787c478bd9Sstevel@tonic-gate 					/*
26797c478bd9Sstevel@tonic-gate 					 * Since we have this device connected
26807c478bd9Sstevel@tonic-gate 					 * mark it reinserted to prevent
26817c478bd9Sstevel@tonic-gate 					 * cleanup thread from stepping in.
26827c478bd9Sstevel@tonic-gate 					 */
26837c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
268416747f41Scth 					mutex_enter(&(DEVI(ch_dip)->devi_lock));
268516747f41Scth 					DEVI_SET_DEVICE_REINSERTED(ch_dip);
268616747f41Scth 					mutex_exit(&(DEVI(ch_dip)->devi_lock));
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 					/*
26897c478bd9Sstevel@tonic-gate 					 * reopen pipes for children for
26907c478bd9Sstevel@tonic-gate 					 * their DDI_RESUME
26917c478bd9Sstevel@tonic-gate 					 */
26927c478bd9Sstevel@tonic-gate 					rval = usba_persistent_pipe_open(
26937c478bd9Sstevel@tonic-gate 					    usba_get_usba_device(ch_dip));
26947c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
26957c478bd9Sstevel@tonic-gate 					ASSERT(rval == USB_SUCCESS);
26967c478bd9Sstevel@tonic-gate 				}
26977c478bd9Sstevel@tonic-gate 			} else {
26987c478bd9Sstevel@tonic-gate 				/*
26997c478bd9Sstevel@tonic-gate 				 * Mark this dip for deletion as the device
27007c478bd9Sstevel@tonic-gate 				 * is not physically present, and schedule
27017c478bd9Sstevel@tonic-gate 				 * cleanup thread upon post resume
27027c478bd9Sstevel@tonic-gate 				 */
27037c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
27067c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
27077c478bd9Sstevel@tonic-gate 				    "hubd_restore_device_state: "
27087c478bd9Sstevel@tonic-gate 				    "dip=%p on port=%d marked for cleanup",
2709112116d8Sfb209375 				    (void *)ch_dip, port);
271016747f41Scth 				mutex_enter(&(DEVI(ch_dip)->devi_lock));
27117c478bd9Sstevel@tonic-gate 				DEVI_SET_DEVICE_REMOVED(ch_dip);
271216747f41Scth 				mutex_exit(&(DEVI(ch_dip)->devi_lock));
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
27157c478bd9Sstevel@tonic-gate 			}
27167c478bd9Sstevel@tonic-gate 		} else if (ehci_root_hub) {
27177c478bd9Sstevel@tonic-gate 			/* get port status */
27187c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
2719993e3fafSRobert Mustacchi 			    &status, &change, NULL, PORT_CHANGE_CSC);
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 			/* check if it is truly connected */
27227c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
27237c478bd9Sstevel@tonic-gate 				/*
27247c478bd9Sstevel@tonic-gate 				 * reset the port to find out if we have
27257c478bd9Sstevel@tonic-gate 				 * 2.0 device connected or 1.X. A 2.0
27267c478bd9Sstevel@tonic-gate 				 * device will still be seen as connected,
27277c478bd9Sstevel@tonic-gate 				 * while a 1.X device will switch over to
27287c478bd9Sstevel@tonic-gate 				 * the companion controller.
27297c478bd9Sstevel@tonic-gate 				 */
27307c478bd9Sstevel@tonic-gate 				(void) hubd_reset_port(hubd, port);
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 				(void) hubd_determine_port_status(hubd, port,
2733993e3fafSRobert Mustacchi 				    &status, &change, NULL, PORT_CHANGE_CSC);
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate 				if (status &
27367c478bd9Sstevel@tonic-gate 				    (PORT_STATUS_CCS | PORT_STATUS_HSDA)) {
27377c478bd9Sstevel@tonic-gate 					/*
27387c478bd9Sstevel@tonic-gate 					 * We have a USB 2.0 device
27397c478bd9Sstevel@tonic-gate 					 * connected. Power cycle this port
27407c478bd9Sstevel@tonic-gate 					 * so that hotplug thread can
27417c478bd9Sstevel@tonic-gate 					 * enumerate this device.
27427c478bd9Sstevel@tonic-gate 					 */
27437c478bd9Sstevel@tonic-gate 					(void) hubd_toggle_port(hubd, port);
27447c478bd9Sstevel@tonic-gate 				} else {
27457c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_ATTA,
27467c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
27477c478bd9Sstevel@tonic-gate 					    "hubd_restore_device_state: "
27487c478bd9Sstevel@tonic-gate 					    "device on port %d switched over",
27497c478bd9Sstevel@tonic-gate 					    port);
27507c478bd9Sstevel@tonic-gate 				}
27517c478bd9Sstevel@tonic-gate 			}
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 		}
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	/* if the device had remote wakeup earlier, enable it again */
27587c478bd9Sstevel@tonic-gate 	if (hubd->h_hubpm->hubp_wakeup_enabled) {
27597c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
27607c478bd9Sstevel@tonic-gate 		(void) usb_handle_remote_wakeup(hubd->h_dip,
27617c478bd9Sstevel@tonic-gate 		    USB_REMOTE_WAKEUP_ENABLE);
27627c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
27637c478bd9Sstevel@tonic-gate 	}
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_ONLINE;
27667c478bd9Sstevel@tonic-gate 	hubd_start_polling(hubd, 0);
27677c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, dip, 0);
27687c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
27697c478bd9Sstevel@tonic-gate }
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate /*
27737c478bd9Sstevel@tonic-gate  * hubd_cleanup:
27747c478bd9Sstevel@tonic-gate  *	cleanup hubd and deallocate. this function is called for
27757c478bd9Sstevel@tonic-gate  *	handling attach failures and detaching including dynamic
27767c478bd9Sstevel@tonic-gate  *	reconfiguration. If called from attaching, it must clean
27777c478bd9Sstevel@tonic-gate  *	up the whole thing and return success.
27787c478bd9Sstevel@tonic-gate  */
27797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27807c478bd9Sstevel@tonic-gate static int
hubd_cleanup(dev_info_t * dip,hubd_t * hubd)27817c478bd9Sstevel@tonic-gate hubd_cleanup(dev_info_t *dip, hubd_t *hubd)
27827c478bd9Sstevel@tonic-gate {
27833fe80ca4SDan Cross 	int		rval, old_dev_state;
27847c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
27857c478bd9Sstevel@tonic-gate #ifdef DEBUG
27867c478bd9Sstevel@tonic-gate 	usb_port_t	port;
27877c478bd9Sstevel@tonic-gate #endif
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
27907c478bd9Sstevel@tonic-gate 	    "hubd_cleanup:");
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	if ((hubd->h_init_state & HUBD_LOCKS_DONE) == 0) {
27937c478bd9Sstevel@tonic-gate 		goto done;
27947c478bd9Sstevel@tonic-gate 	}
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	/* ensure we are the only one active */
27973fe80ca4SDan Cross 	ndi_devi_enter(dip);
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	/* Cleanup failure is only allowed if called from detach */
28027c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DETACHING(dip)) {
28037c478bd9Sstevel@tonic-gate 		dev_info_t *rh_dip = hubd->h_usba_device->usb_root_hub_dip;
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 		/*
28067c478bd9Sstevel@tonic-gate 		 * We are being called from detach.
28077c478bd9Sstevel@tonic-gate 		 * Fail immediately if the hotplug thread is running
28087c478bd9Sstevel@tonic-gate 		 * else set the dev_state to disconnected so that
28097c478bd9Sstevel@tonic-gate 		 * hotplug thread just exits without doing anything.
28107c478bd9Sstevel@tonic-gate 		 */
28117c478bd9Sstevel@tonic-gate 		if (hubd->h_bus_ctls || hubd->h_bus_pwr ||
28127c478bd9Sstevel@tonic-gate 		    hubd->h_hotplug_thread) {
28137c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
28143fe80ca4SDan Cross 			ndi_devi_exit(dip);
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
28177c478bd9Sstevel@tonic-gate 			    "hubd_cleanup: hotplug thread/bus ctl active "
28187c478bd9Sstevel@tonic-gate 			    "- failing detach");
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
28217c478bd9Sstevel@tonic-gate 		}
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 		/*
28247c478bd9Sstevel@tonic-gate 		 * if the deathrow thread is still active or about
28257c478bd9Sstevel@tonic-gate 		 * to become active, fail detach
28267c478bd9Sstevel@tonic-gate 		 * the roothup can only be detached if nexus drivers
28277c478bd9Sstevel@tonic-gate 		 * are unloaded or explicitly offlined
28287c478bd9Sstevel@tonic-gate 		 */
28297c478bd9Sstevel@tonic-gate 		if (rh_dip == dip) {
28307c478bd9Sstevel@tonic-gate 			if (hubd->h_cleanup_needed ||
28317c478bd9Sstevel@tonic-gate 			    hubd->h_cleanup_active) {
28327c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
28333fe80ca4SDan Cross 				ndi_devi_exit(dip);
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
28367c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
28377c478bd9Sstevel@tonic-gate 				    "hubd_cleanup: deathrow still active?"
28387c478bd9Sstevel@tonic-gate 				    "- failing detach");
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 				return (USB_FAILURE);
28417c478bd9Sstevel@tonic-gate 			}
28427c478bd9Sstevel@tonic-gate 		}
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 	old_dev_state = hubd->h_dev_state;
28467c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = USB_DEV_DISCONNECTED;
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
28497c478bd9Sstevel@tonic-gate 	    "hubd_cleanup: stop polling");
28507c478bd9Sstevel@tonic-gate 	hubd_close_intr_pipe(hubd);
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	ASSERT((hubd->h_bus_ctls || hubd->h_bus_pwr ||
28537c478bd9Sstevel@tonic-gate 	    hubd->h_hotplug_thread) == 0);
28547c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	/*
28577c478bd9Sstevel@tonic-gate 	 * deallocate events, if events are still registered
28587c478bd9Sstevel@tonic-gate 	 * (ie. children still attached) then we have to fail the detach
28597c478bd9Sstevel@tonic-gate 	 */
28607c478bd9Sstevel@tonic-gate 	if (hubd->h_ndi_event_hdl) {
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 		rval = ndi_event_free_hdl(hubd->h_ndi_event_hdl);
28637c478bd9Sstevel@tonic-gate 		if (DEVI_IS_ATTACHING(dip)) {
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 			/* It must return success if attaching. */
28667c478bd9Sstevel@tonic-gate 			ASSERT(rval == NDI_SUCCESS);
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 		} else if (rval != NDI_SUCCESS) {
28697c478bd9Sstevel@tonic-gate 
2870d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_ALL, hubd->h_log_handle,
28717c478bd9Sstevel@tonic-gate 			    "hubd_cleanup: ndi_event_free_hdl failed");
28723fe80ca4SDan Cross 			ndi_devi_exit(dip);
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 		}
28777c478bd9Sstevel@tonic-gate 	}
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
28807c478bd9Sstevel@tonic-gate 
288135f36846Ssl147100 	if (hubd->h_init_state & HUBD_CHILDREN_CREATED) {
28827c478bd9Sstevel@tonic-gate #ifdef DEBUG
2883993e3fafSRobert Mustacchi 		for (port = 1; port <= hubd->h_nports; port++) {
28847c478bd9Sstevel@tonic-gate 			ASSERT(hubd->h_usba_devices[port] == NULL);
28857c478bd9Sstevel@tonic-gate 			ASSERT(hubd->h_children_dips[port] == NULL);
28867c478bd9Sstevel@tonic-gate 		}
28877c478bd9Sstevel@tonic-gate #endif
28887c478bd9Sstevel@tonic-gate 		kmem_free(hubd->h_children_dips, hubd->h_cd_list_length);
28897c478bd9Sstevel@tonic-gate 		kmem_free(hubd->h_usba_devices, hubd->h_cd_list_length);
289035f36846Ssl147100 	}
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 	/*
28937c478bd9Sstevel@tonic-gate 	 * Disable the event callbacks first, after this point, event
28947c478bd9Sstevel@tonic-gate 	 * callbacks will never get called. Note we shouldn't hold
28957c478bd9Sstevel@tonic-gate 	 * mutex while unregistering events because there may be a
28967c478bd9Sstevel@tonic-gate 	 * competing event callback thread. Event callbacks are done
28977c478bd9Sstevel@tonic-gate 	 * with ndi mutex held and this can cause a potential deadlock.
28987c478bd9Sstevel@tonic-gate 	 * Note that cleanup can't fail after deregistration of events.
28997c478bd9Sstevel@tonic-gate 	 */
29007c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state &  HUBD_EVENTS_REGISTERED) {
29017c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
29027c478bd9Sstevel@tonic-gate 		usb_unregister_event_cbs(dip, &hubd_events);
29037c478bd9Sstevel@tonic-gate 		hubd_unregister_cpr_callback(hubd);
29047c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
29057c478bd9Sstevel@tonic-gate 	}
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 	/* restore the old dev state so that device can be put into low power */
29087c478bd9Sstevel@tonic-gate 	hubd->h_dev_state = old_dev_state;
29097c478bd9Sstevel@tonic-gate 	hubpm = hubd->h_hubpm;
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 	if ((hubpm) && (hubd->h_dev_state != USB_DEV_DISCONNECTED)) {
29127c478bd9Sstevel@tonic-gate 		(void) hubd_pm_busy_component(hubd, dip, 0);
29137c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
29147c478bd9Sstevel@tonic-gate 		if (hubd->h_hubpm->hubp_wakeup_enabled) {
29157c478bd9Sstevel@tonic-gate 			/*
29167c478bd9Sstevel@tonic-gate 			 * Bring the hub to full power before
29177c478bd9Sstevel@tonic-gate 			 * issuing the disable remote wakeup command
29187c478bd9Sstevel@tonic-gate 			 */
29197c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 			if ((rval = usb_handle_remote_wakeup(hubd->h_dip,
29227c478bd9Sstevel@tonic-gate 			    USB_REMOTE_WAKEUP_DISABLE)) != USB_SUCCESS) {
29237c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PM,
29247c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
29257c478bd9Sstevel@tonic-gate 				    "hubd_cleanup: disable remote wakeup "
29267c478bd9Sstevel@tonic-gate 				    "fails=%d", rval);
29277c478bd9Sstevel@tonic-gate 			}
29287c478bd9Sstevel@tonic-gate 		}
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 		(void) pm_lower_power(hubd->h_dip, 0, USB_DEV_OS_PWR_OFF);
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
29337c478bd9Sstevel@tonic-gate 		(void) hubd_pm_idle_component(hubd, dip, 0);
29347c478bd9Sstevel@tonic-gate 	}
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 	if (hubpm) {
29377c478bd9Sstevel@tonic-gate 		if (hubpm->hubp_child_pwrstate) {
29387c478bd9Sstevel@tonic-gate 			kmem_free(hubpm->hubp_child_pwrstate,
29397c478bd9Sstevel@tonic-gate 			    MAX_PORTS + 1);
29407c478bd9Sstevel@tonic-gate 		}
29417c478bd9Sstevel@tonic-gate 		kmem_free(hubpm, sizeof (hub_power_t));
29427c478bd9Sstevel@tonic-gate 	}
29437c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
29467c478bd9Sstevel@tonic-gate 	    "hubd_cleanup: freeing space");
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_HUBDI_REGISTERED) {
29497c478bd9Sstevel@tonic-gate 		rval = usba_hubdi_unregister(dip);
29507c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
29517c478bd9Sstevel@tonic-gate 	}
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_LOCKS_DONE) {
29547c478bd9Sstevel@tonic-gate 		mutex_destroy(HUBD_MUTEX(hubd));
29557c478bd9Sstevel@tonic-gate 		cv_destroy(&hubd->h_cv_reset_port);
2956ffcd51f3Slg150142 		cv_destroy(&hubd->h_cv_hotplug_dev);
29577c478bd9Sstevel@tonic-gate 	}
29587c478bd9Sstevel@tonic-gate 
29593fe80ca4SDan Cross 	ndi_devi_exit(dip);
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	if (hubd->h_init_state & HUBD_MINOR_NODE_CREATED) {
29627c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
29637c478bd9Sstevel@tonic-gate 	}
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
29667c478bd9Sstevel@tonic-gate 		usb_pipe_close(dip, hubd->h_default_pipe,
29677c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
29687c478bd9Sstevel@tonic-gate 	}
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate done:
29717c478bd9Sstevel@tonic-gate 	if (hubd->h_ancestry_str) {
29727c478bd9Sstevel@tonic-gate 		kmem_free(hubd->h_ancestry_str, HUBD_APID_NAMELEN);
29737c478bd9Sstevel@tonic-gate 	}
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 	usb_client_detach(dip, hubd->h_dev_data);
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(hubd->h_log_handle);
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(dip)) {
29807c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(hubd_statep, ddi_get_instance(dip));
29817c478bd9Sstevel@tonic-gate 	}
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
29867c478bd9Sstevel@tonic-gate }
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate /*
29906c7181fcSsl147100  * hubd_determine_port_connection:
29916c7181fcSsl147100  *	Determine which port is in connect status but does not
29926c7181fcSsl147100  *	have connect status change bit set, and mark port change
29936c7181fcSsl147100  *	bit accordingly.
29946c7181fcSsl147100  *	This function is applied during hub attach time.
29956c7181fcSsl147100  */
29966c7181fcSsl147100 static usb_port_mask_t
hubd_determine_port_connection(hubd_t * hubd)29976c7181fcSsl147100 hubd_determine_port_connection(hubd_t	*hubd)
29986c7181fcSsl147100 {
29996c7181fcSsl147100 	usb_port_t	port;
30006c7181fcSsl147100 	uint16_t	status;
30016c7181fcSsl147100 	uint16_t	change;
30026c7181fcSsl147100 	usb_port_mask_t	port_change = 0;
30036c7181fcSsl147100 
30046c7181fcSsl147100 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
30056c7181fcSsl147100 
3006993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
30076c7181fcSsl147100 
30086c7181fcSsl147100 		(void) hubd_determine_port_status(hubd, port, &status,
3009993e3fafSRobert Mustacchi 		    &change, NULL, 0);
30106c7181fcSsl147100 
30116c7181fcSsl147100 		/* Check if port is in connect status */
30126c7181fcSsl147100 		if (!(status & PORT_STATUS_CCS)) {
30136c7181fcSsl147100 
30146c7181fcSsl147100 			continue;
30156c7181fcSsl147100 		}
30166c7181fcSsl147100 
30176c7181fcSsl147100 		/*
30186c7181fcSsl147100 		 * Check if port Connect Status Change bit has been set.
30196c7181fcSsl147100 		 * If already set, the connection will be handled by
30206c7181fcSsl147100 		 * intr polling callback, not during attach.
30216c7181fcSsl147100 		 */
30226c7181fcSsl147100 		if (change & PORT_CHANGE_CSC) {
30236c7181fcSsl147100 
30246c7181fcSsl147100 			continue;
30256c7181fcSsl147100 		}
30266c7181fcSsl147100 
30276c7181fcSsl147100 		port_change |= 1 << port;
30286c7181fcSsl147100 	}
30296c7181fcSsl147100 
30306c7181fcSsl147100 	return (port_change);
30316c7181fcSsl147100 }
30326c7181fcSsl147100 
30336c7181fcSsl147100 
30346c7181fcSsl147100 /*
30357c478bd9Sstevel@tonic-gate  * hubd_check_ports:
30367c478bd9Sstevel@tonic-gate  *	- get hub descriptor
30377c478bd9Sstevel@tonic-gate  *	- check initial port status
30387c478bd9Sstevel@tonic-gate  *	- enable power on all ports
30397c478bd9Sstevel@tonic-gate  *	- enable polling on ep1
30407c478bd9Sstevel@tonic-gate  */
30417c478bd9Sstevel@tonic-gate static int
hubd_check_ports(hubd_t * hubd)30427c478bd9Sstevel@tonic-gate hubd_check_ports(hubd_t  *hubd)
30437c478bd9Sstevel@tonic-gate {
30447c478bd9Sstevel@tonic-gate 	int			rval;
30456c7181fcSsl147100 	usb_port_mask_t		port_change = 0;
30466c7181fcSsl147100 	hubd_hotplug_arg_t	*arg;
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
30517c478bd9Sstevel@tonic-gate 	    "hubd_check_ports: addr=0x%x", usb_get_addr(hubd->h_dip));
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 	/*
30547c478bd9Sstevel@tonic-gate 	 * First turn off all port power
30557c478bd9Sstevel@tonic-gate 	 */
30567c478bd9Sstevel@tonic-gate 	if ((rval = hubd_disable_all_port_power(hubd)) != USB_SUCCESS) {
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
30597c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 		return (rval);
30627c478bd9Sstevel@tonic-gate 	}
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 	/*
30657c478bd9Sstevel@tonic-gate 	 * do not switch on immediately (instantly on root hub)
30667c478bd9Sstevel@tonic-gate 	 * and allow time to settle
30677c478bd9Sstevel@tonic-gate 	 */
30687c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
30697c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
30707c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	/*
30737c478bd9Sstevel@tonic-gate 	 * enable power on all ports so we can see connects
30747c478bd9Sstevel@tonic-gate 	 */
30757c478bd9Sstevel@tonic-gate 	if ((rval = hubd_enable_all_port_power(hubd)) != USB_SUCCESS) {
30767c478bd9Sstevel@tonic-gate 		/* disable whatever was enabled */
30777c478bd9Sstevel@tonic-gate 		(void) hubd_disable_all_port_power(hubd);
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 		return (rval);
30807c478bd9Sstevel@tonic-gate 	}
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 	/* wait at least 3 frames before accessing devices */
30837c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
30847c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(10000));
30857c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	/*
30887c478bd9Sstevel@tonic-gate 	 * allocate arrays for saving the dips of each child per port
30897c478bd9Sstevel@tonic-gate 	 *
30907c478bd9Sstevel@tonic-gate 	 * ports go from 1 - n, allocate 1 more entry
30917c478bd9Sstevel@tonic-gate 	 */
30927c478bd9Sstevel@tonic-gate 	hubd->h_cd_list_length =
3093993e3fafSRobert Mustacchi 	    (sizeof (dev_info_t **)) * (hubd->h_nports + 1);
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	hubd->h_children_dips = (dev_info_t **)kmem_zalloc(
30967c478bd9Sstevel@tonic-gate 	    hubd->h_cd_list_length, KM_SLEEP);
30977c478bd9Sstevel@tonic-gate 	hubd->h_usba_devices = (usba_device_t **)kmem_zalloc(
30987c478bd9Sstevel@tonic-gate 	    hubd->h_cd_list_length, KM_SLEEP);
30997c478bd9Sstevel@tonic-gate 
310035f36846Ssl147100 	hubd->h_init_state |= HUBD_CHILDREN_CREATED;
310135f36846Ssl147100 
31026c7181fcSsl147100 	mutex_exit(HUBD_MUTEX(hubd));
31036c7181fcSsl147100 	arg = (hubd_hotplug_arg_t *)kmem_zalloc(
31046c7181fcSsl147100 	    sizeof (hubd_hotplug_arg_t), KM_SLEEP);
31056c7181fcSsl147100 	mutex_enter(HUBD_MUTEX(hubd));
31066c7181fcSsl147100 
31076c7181fcSsl147100 	if ((rval = hubd_open_intr_pipe(hubd)) != USB_SUCCESS) {
31086c7181fcSsl147100 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
31096c7181fcSsl147100 
31106c7181fcSsl147100 		return (rval);
31116c7181fcSsl147100 	}
31126c7181fcSsl147100 
31137c478bd9Sstevel@tonic-gate 	hubd_start_polling(hubd, 0);
31146c7181fcSsl147100 
31156c7181fcSsl147100 	/*
31166c7181fcSsl147100 	 * Some hub devices, like the embedded hub in the CKS ErgoMagic
31176c7181fcSsl147100 	 * keyboard, may only have connection status bit set, but not
31186c7181fcSsl147100 	 * have connect status change bit set when a device has been
31196c7181fcSsl147100 	 * connected to its downstream port before the hub is enumerated.
31206c7181fcSsl147100 	 * Then when the hub is in enumeration, the devices connected to
31216c7181fcSsl147100 	 * it cannot be detected by the intr pipe and won't be enumerated.
31226c7181fcSsl147100 	 * We need to check such situation here and enumerate the downstream
31236c7181fcSsl147100 	 * devices for such hubs.
31246c7181fcSsl147100 	 */
31256c7181fcSsl147100 	port_change = hubd_determine_port_connection(hubd);
31266c7181fcSsl147100 
3127993e3fafSRobert Mustacchi 	if (port_change != 0 || hubd->h_port_change != 0) {
31286c7181fcSsl147100 		hubd_pm_busy_component(hubd, hubd->h_dip, 0);
31296c7181fcSsl147100 
31306c7181fcSsl147100 		arg->hubd = hubd;
31316c7181fcSsl147100 		arg->hotplug_during_attach = B_TRUE;
31326c7181fcSsl147100 		hubd->h_port_change |= port_change;
31336c7181fcSsl147100 
31346c7181fcSsl147100 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
31356c7181fcSsl147100 		    "hubd_check_ports: port change=0x%x, need to connect",
31366c7181fcSsl147100 		    hubd->h_port_change);
31376c7181fcSsl147100 
31386c7181fcSsl147100 		if (usb_async_req(hubd->h_dip, hubd_hotplug_thread,
31396c7181fcSsl147100 		    (void *)arg, 0) == USB_SUCCESS) {
31406c7181fcSsl147100 			hubd->h_hotplug_thread++;
31416c7181fcSsl147100 		} else {
31426c7181fcSsl147100 			/* mark this device as idle */
31436c7181fcSsl147100 			hubd_pm_idle_component(hubd, hubd->h_dip, 0);
31446c7181fcSsl147100 			kmem_free(arg, sizeof (hubd_hotplug_arg_t));
31456c7181fcSsl147100 		}
31466c7181fcSsl147100 	} else {
31476c7181fcSsl147100 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
31487c478bd9Sstevel@tonic-gate 	}
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
31517c478bd9Sstevel@tonic-gate 	    "hubd_check_ports done");
31527c478bd9Sstevel@tonic-gate 
31536c7181fcSsl147100 	return (USB_SUCCESS);
31547c478bd9Sstevel@tonic-gate }
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate /*
31587c478bd9Sstevel@tonic-gate  * hubd_get_hub_descriptor:
31597c478bd9Sstevel@tonic-gate  */
31607c478bd9Sstevel@tonic-gate static int
hubd_get_hub_descriptor(hubd_t * hubd)31617c478bd9Sstevel@tonic-gate hubd_get_hub_descriptor(hubd_t *hubd)
31627c478bd9Sstevel@tonic-gate {
31637c478bd9Sstevel@tonic-gate 	mblk_t		*data = NULL;
31647c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
31657c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
3166993e3fafSRobert Mustacchi 	uint16_t	length, wValue;
31677c478bd9Sstevel@tonic-gate 	int		rval;
3168f5b8369cSRaymond Chen 	usb_req_attrs_t attr = 0;
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
31717c478bd9Sstevel@tonic-gate 	    "hubd_get_hub_descriptor:");
31727c478bd9Sstevel@tonic-gate 
3173f5b8369cSRaymond Chen 	if ((hubd->h_dev_data->dev_descr->idVendor == USB_HUB_INTEL_VID) &&
3174f5b8369cSRaymond Chen 	    (hubd->h_dev_data->dev_descr->idProduct == USB_HUB_INTEL_PID)) {
3175f5b8369cSRaymond Chen 		attr = USB_ATTRS_SHORT_XFER_OK;
3176f5b8369cSRaymond Chen 	}
3177f5b8369cSRaymond Chen 
31787c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
31797c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_default_pipe != 0);
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
31827c478bd9Sstevel@tonic-gate 
3183993e3fafSRobert Mustacchi 	/*
3184993e3fafSRobert Mustacchi 	 * The contents of wValue change depending on whether this is a USB 2 or
3185993e3fafSRobert Mustacchi 	 * USB 3 device. SuperSpeed Hubs have different descriptors and you
3186993e3fafSRobert Mustacchi 	 * cannot ask them for the traditional USB 2 descriptor.
3187993e3fafSRobert Mustacchi 	 */
3188993e3fafSRobert Mustacchi 	if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
3189993e3fafSRobert Mustacchi 		wValue = USB_DESCR_TYPE_SS_HUB << 8 | HUBD_DEFAULT_DESC_INDEX;
3190993e3fafSRobert Mustacchi 	} else {
3191993e3fafSRobert Mustacchi 		wValue = USB_DESCR_TYPE_HUB << 8 | HUBD_DEFAULT_DESC_INDEX;
3192993e3fafSRobert Mustacchi 	}
3193993e3fafSRobert Mustacchi 
3194993e3fafSRobert Mustacchi 	/*
3195993e3fafSRobert Mustacchi 	 * The hub descriptor length varies in various versions of USB. For
3196993e3fafSRobert Mustacchi 	 * example, in USB 2 it's at least 9 bytes long. To start with, we
3197993e3fafSRobert Mustacchi 	 * always get the first 8 bytes so we can figure out how long it
3198993e3fafSRobert Mustacchi 	 * actually is.
3199993e3fafSRobert Mustacchi 	 */
32007c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
32017c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
320235f36846Ssl147100 	    HUB_CLASS_REQ_TYPE,
32037c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,		/* bRequest */
3204993e3fafSRobert Mustacchi 	    wValue,			/* wValue */
32057c478bd9Sstevel@tonic-gate 	    0,				/* wIndex */
32067c478bd9Sstevel@tonic-gate 	    8,				/* wLength */
32077c478bd9Sstevel@tonic-gate 	    &data, 0,
32087c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
32097c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
32107c478bd9Sstevel@tonic-gate 		    "get hub descriptor failed: cr=%d cb_fl=0x%x rval=%d",
32117c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
32127c478bd9Sstevel@tonic-gate 		freemsg(data);
32137c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 		return (rval);
32167c478bd9Sstevel@tonic-gate 	}
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 	length = *(data->b_rptr);
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	if (length > 8) {
32217c478bd9Sstevel@tonic-gate 		freemsg(data);
32227c478bd9Sstevel@tonic-gate 		data = NULL;
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 		/* get complete hub descriptor */
3225f5b8369cSRaymond Chen 		rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
32267c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
322735f36846Ssl147100 		    HUB_CLASS_REQ_TYPE,
32287c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,		/* bRequest */
3229993e3fafSRobert Mustacchi 		    wValue,			/* wValue */
32307c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
32317c478bd9Sstevel@tonic-gate 		    length,			/* wLength */
3232f5b8369cSRaymond Chen 		    &data, attr,
3233f5b8369cSRaymond Chen 		    &completion_reason, &cb_flags, 0);
3234f5b8369cSRaymond Chen 
3235f5b8369cSRaymond Chen 		/*
3236f5b8369cSRaymond Chen 		 * Hub descriptor data less than 9 bytes is not valid and
3237f5b8369cSRaymond Chen 		 * may cause trouble if we use it. See USB2.0 Tab11-13.
3238f5b8369cSRaymond Chen 		 */
3239f5b8369cSRaymond Chen 		if ((rval != USB_SUCCESS) || (MBLKL(data) <= 8)) {
32407c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
32417c478bd9Sstevel@tonic-gate 			    "get hub descriptor failed: "
3242f5b8369cSRaymond Chen 			    "cr=%d cb_fl=0x%x rval=%d, len=%ld",
3243f5b8369cSRaymond Chen 			    completion_reason, cb_flags, rval,
3244f5b8369cSRaymond Chen 			    (data)?MBLKL(data):0);
32457c478bd9Sstevel@tonic-gate 			freemsg(data);
32467c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 			return (rval);
32497c478bd9Sstevel@tonic-gate 		}
32507c478bd9Sstevel@tonic-gate 	}
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
32537c478bd9Sstevel@tonic-gate 
3254993e3fafSRobert Mustacchi 	/*
3255993e3fafSRobert Mustacchi 	 * Parse the hub descriptor. Note that the format of the descriptor
3256993e3fafSRobert Mustacchi 	 * itself depends on the USB version. We handle the different ones and
3257993e3fafSRobert Mustacchi 	 * transform it into a single uniform view.
3258993e3fafSRobert Mustacchi 	 */
3259993e3fafSRobert Mustacchi 
3260993e3fafSRobert Mustacchi 	ASSERT(*(data->b_rptr + 2) <= (MAX_PORTS + 1));
3261993e3fafSRobert Mustacchi 	if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
3262993e3fafSRobert Mustacchi 		usb_ss_hub_descr_t hub_descr;
3263993e3fafSRobert Mustacchi 		char *desc = "cccscccs";
3264993e3fafSRobert Mustacchi 		ASSERT(*(data->b_rptr + 1) == ROOT_HUB_SS_DESCRIPTOR_TYPE);
3265993e3fafSRobert Mustacchi 
3266993e3fafSRobert Mustacchi 		/*
3267993e3fafSRobert Mustacchi 		 * Note many hubs may support less than the 255 devices that the
3268993e3fafSRobert Mustacchi 		 * USB specification allows for. In those cases, we'll simply
3269993e3fafSRobert Mustacchi 		 * read less and it should be okay.
3270993e3fafSRobert Mustacchi 		 */
3271993e3fafSRobert Mustacchi 		if (usb_parse_CV_descr(desc, data->b_rptr, MBLKL(data),
3272993e3fafSRobert Mustacchi 		    (void *)&hub_descr, sizeof (hub_descr)) == 0) {
32737c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
32747c478bd9Sstevel@tonic-gate 			    "parsing hub descriptor failed");
32757c478bd9Sstevel@tonic-gate 			freemsg(data);
32767c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
32777c478bd9Sstevel@tonic-gate 		}
32787c478bd9Sstevel@tonic-gate 
3279993e3fafSRobert Mustacchi 		hubd->h_nports = hub_descr.bNbrPorts;
3280993e3fafSRobert Mustacchi 		hubd->h_hub_chars = hub_descr.wHubCharacteristics;
3281993e3fafSRobert Mustacchi 		hubd->h_power_good = hub_descr.bPwrOn2PwrGood;
3282993e3fafSRobert Mustacchi 		hubd->h_current = hub_descr.bHubContrCurrent;
3283993e3fafSRobert Mustacchi 	} else {
3284993e3fafSRobert Mustacchi 		usb_hub_descr_t hub_descr;
3285993e3fafSRobert Mustacchi 		if (usb_parse_CV_descr("cccscccccc",
3286993e3fafSRobert Mustacchi 		    data->b_rptr, MBLKL(data),
3287993e3fafSRobert Mustacchi 		    (void *)&hub_descr, sizeof (usb_hub_descr_t)) == 0) {
3288993e3fafSRobert Mustacchi 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
3289993e3fafSRobert Mustacchi 			    "parsing hub descriptor failed");
3290993e3fafSRobert Mustacchi 			freemsg(data);
3291993e3fafSRobert Mustacchi 			return (USB_FAILURE);
3292993e3fafSRobert Mustacchi 		}
3293993e3fafSRobert Mustacchi 
3294993e3fafSRobert Mustacchi 		hubd->h_nports = hub_descr.bNbrPorts;
3295993e3fafSRobert Mustacchi 		hubd->h_hub_chars = hub_descr.wHubCharacteristics;
3296993e3fafSRobert Mustacchi 		hubd->h_power_good = hub_descr.bPwrOn2PwrGood;
3297993e3fafSRobert Mustacchi 		hubd->h_current = hub_descr.bHubContrCurrent;
3298993e3fafSRobert Mustacchi 	}
3299993e3fafSRobert Mustacchi 
33007c478bd9Sstevel@tonic-gate 	freemsg(data);
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
33037c478bd9Sstevel@tonic-gate 	    "rval=0x%x bNbrPorts=0x%x wHubChars=0x%x "
330435f36846Ssl147100 	    "PwrOn2PwrGood=0x%x HubContrCurrent=%dmA", rval,
3305993e3fafSRobert Mustacchi 	    hubd->h_nports, hubd->h_hub_chars,
3306993e3fafSRobert Mustacchi 	    hubd->h_power_good, hubd->h_current);
33077c478bd9Sstevel@tonic-gate 
3308993e3fafSRobert Mustacchi 	if (hubd->h_nports > MAX_PORTS) {
33097c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L0(DPRINT_MASK_ATTA, hubd->h_log_handle,
33107c478bd9Sstevel@tonic-gate 		    "Hub driver supports max of %d ports on hub. "
33117c478bd9Sstevel@tonic-gate 		    "Hence using the first %d port of %d ports available",
3312993e3fafSRobert Mustacchi 		    MAX_PORTS, MAX_PORTS, hubd->h_nports);
33137c478bd9Sstevel@tonic-gate 
3314993e3fafSRobert Mustacchi 		hubd->h_nports = MAX_PORTS;
33157c478bd9Sstevel@tonic-gate 	}
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
33187c478bd9Sstevel@tonic-gate }
33197c478bd9Sstevel@tonic-gate 
3320993e3fafSRobert Mustacchi static int
hubd_set_hub_depth(hubd_t * hubd)3321993e3fafSRobert Mustacchi hubd_set_hub_depth(hubd_t *hubd)
3322993e3fafSRobert Mustacchi {
3323993e3fafSRobert Mustacchi 	int rval;
3324993e3fafSRobert Mustacchi 	usb_cr_t	completion_reason;
3325993e3fafSRobert Mustacchi 	usb_cb_flags_t	cb_flags;
3326993e3fafSRobert Mustacchi 	usba_device_t	*ud;
3327993e3fafSRobert Mustacchi 	uint16_t	depth;
3328993e3fafSRobert Mustacchi 
3329993e3fafSRobert Mustacchi 	/*
3330993e3fafSRobert Mustacchi 	 * We only need to set the hub depth devices for hubs that are at least
3331993e3fafSRobert Mustacchi 	 * SuperSpeed devices. This didn't exist for USB 2.0 and older hubs.
3332993e3fafSRobert Mustacchi 	 * There's also no need to call this on the root hub.
3333993e3fafSRobert Mustacchi 	 */
3334993e3fafSRobert Mustacchi 	if (hubd->h_usba_device->usb_port_status < USBA_SUPER_SPEED_DEV ||
3335993e3fafSRobert Mustacchi 	    usba_is_root_hub(hubd->h_dip))
3336993e3fafSRobert Mustacchi 		return (USB_SUCCESS);
3337993e3fafSRobert Mustacchi 
3338993e3fafSRobert Mustacchi 	depth = 0;
3339993e3fafSRobert Mustacchi 	ud = hubd->h_usba_device;
3340993e3fafSRobert Mustacchi 	while (ud->usb_parent_hub != NULL) {
3341993e3fafSRobert Mustacchi 		depth++;
3342993e3fafSRobert Mustacchi 		ud = ud->usb_parent_hub;
3343993e3fafSRobert Mustacchi 	}
3344993e3fafSRobert Mustacchi 	ASSERT(depth > 0);
3345993e3fafSRobert Mustacchi 
3346993e3fafSRobert Mustacchi 	if (depth > HUBD_SS_MAX_DEPTH) {
3347993e3fafSRobert Mustacchi 		const char *mfg, *prod;
3348993e3fafSRobert Mustacchi 
3349993e3fafSRobert Mustacchi 		ud = hubd->h_usba_device;
3350993e3fafSRobert Mustacchi 		prod = ud->usb_product_str;
3351993e3fafSRobert Mustacchi 		if (prod == NULL)
3352993e3fafSRobert Mustacchi 			prod = "Unknown Device";
3353993e3fafSRobert Mustacchi 		mfg = ud->usb_mfg_str;
3354993e3fafSRobert Mustacchi 		if (mfg == NULL)
3355993e3fafSRobert Mustacchi 			mfg = "Unknown Manufacturer";
3356993e3fafSRobert Mustacchi 		cmn_err(CE_WARN, "Unable to attach USB 3.x hub %s %s. A "
3357993e3fafSRobert Mustacchi 		    "maximum of %d hubs may be cascaded", mfg, prod,
3358993e3fafSRobert Mustacchi 		    HUBD_SS_MAX_DEPTH);
3359993e3fafSRobert Mustacchi 		return (USB_FAILURE);
3360993e3fafSRobert Mustacchi 	}
3361993e3fafSRobert Mustacchi 
3362993e3fafSRobert Mustacchi 	/*
3363993e3fafSRobert Mustacchi 	 * When making the HUB_REQ_SET_HUB_DEPTH request, a hub connected to a
3364993e3fafSRobert Mustacchi 	 * root port is considered to have a hub depth of zero whereas we
3365993e3fafSRobert Mustacchi 	 * consider having a hub depth of one above.
3366993e3fafSRobert Mustacchi 	 */
3367993e3fafSRobert Mustacchi 	depth--;
3368993e3fafSRobert Mustacchi 
3369993e3fafSRobert Mustacchi 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
3370993e3fafSRobert Mustacchi 	    hubd->h_default_pipe,
3371993e3fafSRobert Mustacchi 	    HUB_SET_HUB_DEPTH_TYPE,
3372993e3fafSRobert Mustacchi 	    HUB_REQ_SET_HUB_DEPTH,	/* bRequest */
3373993e3fafSRobert Mustacchi 	    depth,			/* wValue */
3374993e3fafSRobert Mustacchi 	    0,				/* wIndex */
3375993e3fafSRobert Mustacchi 	    0,				/* wLength */
3376993e3fafSRobert Mustacchi 	    NULL, 0,
3377993e3fafSRobert Mustacchi 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
3378993e3fafSRobert Mustacchi 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
3379993e3fafSRobert Mustacchi 		    "get set hub depth failed: cr=%d cb=0x%x",
3380993e3fafSRobert Mustacchi 		    completion_reason, cb_flags);
3381993e3fafSRobert Mustacchi 	}
3382993e3fafSRobert Mustacchi 
3383993e3fafSRobert Mustacchi 	return (rval);
3384993e3fafSRobert Mustacchi }
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate /*
338735f36846Ssl147100  * hubd_get_hub_status_words:
338835f36846Ssl147100  */
338935f36846Ssl147100 static int
hubd_get_hub_status_words(hubd_t * hubd,uint16_t * status)339035f36846Ssl147100 hubd_get_hub_status_words(hubd_t *hubd, uint16_t *status)
339135f36846Ssl147100 {
339235f36846Ssl147100 	usb_cr_t	completion_reason;
339335f36846Ssl147100 	usb_cb_flags_t	cb_flags;
339435f36846Ssl147100 	mblk_t		*data = NULL;
339535f36846Ssl147100 
339635f36846Ssl147100 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
339735f36846Ssl147100 
339835f36846Ssl147100 	mutex_exit(HUBD_MUTEX(hubd));
339935f36846Ssl147100 
340035f36846Ssl147100 	if (usb_pipe_sync_ctrl_xfer(hubd->h_dip, hubd->h_default_pipe,
340135f36846Ssl147100 	    HUB_CLASS_REQ_TYPE,
340235f36846Ssl147100 	    USB_REQ_GET_STATUS,
340335f36846Ssl147100 	    0,
340435f36846Ssl147100 	    0,
340535f36846Ssl147100 	    GET_STATUS_LENGTH,
340635f36846Ssl147100 	    &data, 0,
340735f36846Ssl147100 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
340835f36846Ssl147100 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
340935f36846Ssl147100 		    "get hub status failed: cr=%d cb=0x%x",
341035f36846Ssl147100 		    completion_reason, cb_flags);
341135f36846Ssl147100 
341235f36846Ssl147100 		if (data) {
341335f36846Ssl147100 			freemsg(data);
341435f36846Ssl147100 		}
341535f36846Ssl147100 
341635f36846Ssl147100 		mutex_enter(HUBD_MUTEX(hubd));
341735f36846Ssl147100 
341835f36846Ssl147100 		return (USB_FAILURE);
341935f36846Ssl147100 	}
342035f36846Ssl147100 
342135f36846Ssl147100 	mutex_enter(HUBD_MUTEX(hubd));
342235f36846Ssl147100 
342335f36846Ssl147100 	status[0] = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
342435f36846Ssl147100 	status[1] = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
342535f36846Ssl147100 
342635f36846Ssl147100 	USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
342735f36846Ssl147100 	    "hub status=0x%x change=0x%x", status[0], status[1]);
342835f36846Ssl147100 
342935f36846Ssl147100 	freemsg(data);
343035f36846Ssl147100 
343135f36846Ssl147100 	return (USB_SUCCESS);
343235f36846Ssl147100 }
343335f36846Ssl147100 
343435f36846Ssl147100 
343535f36846Ssl147100 /*
34367c478bd9Sstevel@tonic-gate  * hubd_open_intr_pipe:
34377c478bd9Sstevel@tonic-gate  *	we read all descriptors first for curiosity and then simply
34387c478bd9Sstevel@tonic-gate  *	open the pipe
34397c478bd9Sstevel@tonic-gate  */
34407c478bd9Sstevel@tonic-gate static int
hubd_open_intr_pipe(hubd_t * hubd)34417c478bd9Sstevel@tonic-gate hubd_open_intr_pipe(hubd_t	*hubd)
34427c478bd9Sstevel@tonic-gate {
34437c478bd9Sstevel@tonic-gate 	int			rval;
34447c478bd9Sstevel@tonic-gate 
34457c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
34467c478bd9Sstevel@tonic-gate 	    "hubd_open_intr_pipe:");
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_IDLE);
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_OPENING;
34517c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
34527c478bd9Sstevel@tonic-gate 
3453993e3fafSRobert Mustacchi 	if ((rval = usb_pipe_xopen(hubd->h_dip,
3454993e3fafSRobert Mustacchi 	    &hubd->h_ep1_xdescr, &hubd->h_pipe_policy,
34557c478bd9Sstevel@tonic-gate 	    0, &hubd->h_ep1_ph)) != USB_SUCCESS) {
3456d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
34577c478bd9Sstevel@tonic-gate 		    "open intr pipe failed (%d)", rval);
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
34607c478bd9Sstevel@tonic-gate 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 		return (rval);
34637c478bd9Sstevel@tonic-gate 	}
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
34667c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3469112116d8Sfb209375 	    "open intr pipe succeeded, ph=0x%p", (void *)hubd->h_ep1_ph);
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
34727c478bd9Sstevel@tonic-gate }
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate /*
34767c478bd9Sstevel@tonic-gate  * hubd_start_polling:
34777c478bd9Sstevel@tonic-gate  *	start or restart the polling
34787c478bd9Sstevel@tonic-gate  */
34797c478bd9Sstevel@tonic-gate static void
hubd_start_polling(hubd_t * hubd,int always)34807c478bd9Sstevel@tonic-gate hubd_start_polling(hubd_t *hubd, int always)
34817c478bd9Sstevel@tonic-gate {
34827c478bd9Sstevel@tonic-gate 	usb_intr_req_t	*reqp;
34837c478bd9Sstevel@tonic-gate 	int			rval;
34847c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	pipe_state;
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
34877c478bd9Sstevel@tonic-gate 	    "start polling: always=%d dev_state=%d pipe_state=%d\n\t"
34887c478bd9Sstevel@tonic-gate 	    "thread=%d ep1_ph=0x%p",
34897c478bd9Sstevel@tonic-gate 	    always, hubd->h_dev_state, hubd->h_intr_pipe_state,
3490112116d8Sfb209375 	    hubd->h_hotplug_thread, (void *)hubd->h_ep1_ph);
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 	/*
34937c478bd9Sstevel@tonic-gate 	 * start or restart polling on the intr pipe
34947c478bd9Sstevel@tonic-gate 	 * only if hotplug thread is not running
34957c478bd9Sstevel@tonic-gate 	 */
34967c478bd9Sstevel@tonic-gate 	if ((always == HUBD_ALWAYS_START_POLLING) ||
34977c478bd9Sstevel@tonic-gate 	    ((hubd->h_dev_state == USB_DEV_ONLINE) &&
34987c478bd9Sstevel@tonic-gate 	    (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
34997c478bd9Sstevel@tonic-gate 	    (hubd->h_hotplug_thread == 0) && hubd->h_ep1_ph)) {
35007c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
35017c478bd9Sstevel@tonic-gate 		    "start polling requested");
35027c478bd9Sstevel@tonic-gate 
35037c478bd9Sstevel@tonic-gate 		reqp = usb_alloc_intr_req(hubd->h_dip, 0, USB_FLAGS_SLEEP);
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 		reqp->intr_client_private = (usb_opaque_t)hubd;
35067c478bd9Sstevel@tonic-gate 		reqp->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
35077c478bd9Sstevel@tonic-gate 		    USB_ATTRS_AUTOCLEARING;
3508993e3fafSRobert Mustacchi 		reqp->intr_len = hubd->h_ep1_xdescr.uex_ep.wMaxPacketSize;
35097c478bd9Sstevel@tonic-gate 		reqp->intr_cb = hubd_read_cb;
35107c478bd9Sstevel@tonic-gate 		reqp->intr_exc_cb = hubd_exception_cb;
35117c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
35127c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_intr_xfer(hubd->h_ep1_ph, reqp,
35137c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP)) != USB_SUCCESS) {
35147c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
35157c478bd9Sstevel@tonic-gate 			    "start polling failed, rval=%d", rval);
35167c478bd9Sstevel@tonic-gate 			usb_free_intr_req(reqp);
35177c478bd9Sstevel@tonic-gate 		}
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
35207c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP);
35217c478bd9Sstevel@tonic-gate 		if (pipe_state != USB_PIPE_STATE_ACTIVE) {
35227c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
35237c478bd9Sstevel@tonic-gate 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
35247c478bd9Sstevel@tonic-gate 		}
35257c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3526112116d8Sfb209375 		    "start polling request 0x%p", (void *)reqp);
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
35297c478bd9Sstevel@tonic-gate 	}
35307c478bd9Sstevel@tonic-gate }
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate /*
35347c478bd9Sstevel@tonic-gate  * hubd_stop_polling
35357c478bd9Sstevel@tonic-gate  *	stop polling but do not close the pipe
35367c478bd9Sstevel@tonic-gate  */
35377c478bd9Sstevel@tonic-gate static void
hubd_stop_polling(hubd_t * hubd)35387c478bd9Sstevel@tonic-gate hubd_stop_polling(hubd_t *hubd)
35397c478bd9Sstevel@tonic-gate {
35407c478bd9Sstevel@tonic-gate 	int			rval;
35417c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	pipe_state;
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
35447c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
35457c478bd9Sstevel@tonic-gate 		    "hubd_stop_polling:");
35467c478bd9Sstevel@tonic-gate 		hubd->h_intr_pipe_state = HUBD_INTR_PIPE_STOPPED;
35477c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 		usb_pipe_stop_intr_polling(hubd->h_ep1_ph, USB_FLAGS_SLEEP);
35507c478bd9Sstevel@tonic-gate 		rval = usb_pipe_get_state(hubd->h_ep1_ph, &pipe_state,
35517c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP);
35527c478bd9Sstevel@tonic-gate 
35537c478bd9Sstevel@tonic-gate 		if (pipe_state != USB_PIPE_STATE_IDLE) {
35547c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
35557c478bd9Sstevel@tonic-gate 			    "intr pipe state=%d, rval=%d", pipe_state, rval);
35567c478bd9Sstevel@tonic-gate 		}
35577c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
35587c478bd9Sstevel@tonic-gate 		if (hubd->h_intr_pipe_state == HUBD_INTR_PIPE_STOPPED) {
35597c478bd9Sstevel@tonic-gate 			hubd->h_intr_pipe_state = HUBD_INTR_PIPE_ACTIVE;
35607c478bd9Sstevel@tonic-gate 		}
35617c478bd9Sstevel@tonic-gate 	}
35627c478bd9Sstevel@tonic-gate }
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate /*
35667c478bd9Sstevel@tonic-gate  * hubd_close_intr_pipe:
35677c478bd9Sstevel@tonic-gate  *	close the pipe (which also stops the polling
35687c478bd9Sstevel@tonic-gate  *	and wait for the hotplug thread to exit
35697c478bd9Sstevel@tonic-gate  */
35707c478bd9Sstevel@tonic-gate static void
hubd_close_intr_pipe(hubd_t * hubd)35717c478bd9Sstevel@tonic-gate hubd_close_intr_pipe(hubd_t *hubd)
35727c478bd9Sstevel@tonic-gate {
35737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
35747c478bd9Sstevel@tonic-gate 	    "hubd_close_intr_pipe:");
35757c478bd9Sstevel@tonic-gate 
35767c478bd9Sstevel@tonic-gate 	/*
35777c478bd9Sstevel@tonic-gate 	 * Now that no async operation is outstanding on pipe,
35787c478bd9Sstevel@tonic-gate 	 * we can change the state to HUBD_INTR_PIPE_CLOSING
35797c478bd9Sstevel@tonic-gate 	 */
35807c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_CLOSING;
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_hotplug_thread == 0);
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
35857c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
35867c478bd9Sstevel@tonic-gate 		usb_pipe_close(hubd->h_dip, hubd->h_ep1_ph, USB_FLAGS_SLEEP,
35877c478bd9Sstevel@tonic-gate 		    NULL, NULL);
35887c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
35897c478bd9Sstevel@tonic-gate 		hubd->h_ep1_ph = NULL;
35907c478bd9Sstevel@tonic-gate 	}
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	hubd->h_intr_pipe_state = HUBD_INTR_PIPE_IDLE;
35937c478bd9Sstevel@tonic-gate }
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate /*
35977c478bd9Sstevel@tonic-gate  * hubd_exception_cb
35987c478bd9Sstevel@tonic-gate  *	interrupt ep1 exception callback function.
35997c478bd9Sstevel@tonic-gate  *	this callback executes in taskq thread context and assumes
36007c478bd9Sstevel@tonic-gate  *	autoclearing
36017c478bd9Sstevel@tonic-gate  */
36027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
36037c478bd9Sstevel@tonic-gate static void
hubd_exception_cb(usb_pipe_handle_t pipe,usb_intr_req_t * reqp)36047c478bd9Sstevel@tonic-gate hubd_exception_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
36057c478bd9Sstevel@tonic-gate {
36067c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)(reqp->intr_client_private);
36077c478bd9Sstevel@tonic-gate 
36087c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
36097c478bd9Sstevel@tonic-gate 	    "hubd_exception_cb: "
3610112116d8Sfb209375 	    "req=0x%p cr=%d data=0x%p cb_flags=0x%x", (void *)reqp,
3611112116d8Sfb209375 	    reqp->intr_completion_reason, (void *)reqp->intr_data,
36127c478bd9Sstevel@tonic-gate 	    reqp->intr_cb_flags);
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 	ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
36157c478bd9Sstevel@tonic-gate 
36167c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
36177c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 	switch (reqp->intr_completion_reason) {
36207c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_RESET:
36217c478bd9Sstevel@tonic-gate 		/* only restart polling after autoclearing */
36227c478bd9Sstevel@tonic-gate 		if ((hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE) &&
36237c478bd9Sstevel@tonic-gate 		    (hubd->h_port_reset_wait == 0)) {
36247c478bd9Sstevel@tonic-gate 			hubd_start_polling(hubd, 0);
36257c478bd9Sstevel@tonic-gate 		}
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate 		break;
36287c478bd9Sstevel@tonic-gate 	case USB_CR_DEV_NOT_RESP:
36297c478bd9Sstevel@tonic-gate 	case USB_CR_STOPPED_POLLING:
36307c478bd9Sstevel@tonic-gate 	case USB_CR_PIPE_CLOSING:
36317c478bd9Sstevel@tonic-gate 	case USB_CR_UNSPECIFIED_ERR:
36327c478bd9Sstevel@tonic-gate 		/* never restart polling on these conditions */
36337c478bd9Sstevel@tonic-gate 	default:
36347c478bd9Sstevel@tonic-gate 		/* for all others, wait for the autoclearing PIPE_RESET cb */
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate 		break;
36377c478bd9Sstevel@tonic-gate 	}
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate 	usb_free_intr_req(reqp);
36407c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
36417c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
36427c478bd9Sstevel@tonic-gate }
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate 
36457c478bd9Sstevel@tonic-gate /*
36467c478bd9Sstevel@tonic-gate  * helper function to convert LE bytes to a portmask
36477c478bd9Sstevel@tonic-gate  */
36487c478bd9Sstevel@tonic-gate static usb_port_mask_t
hubd_mblk2portmask(mblk_t * data)36497c478bd9Sstevel@tonic-gate hubd_mblk2portmask(mblk_t *data)
36507c478bd9Sstevel@tonic-gate {
3651d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	int len = min(MBLKL(data), sizeof (usb_port_mask_t));
36527c478bd9Sstevel@tonic-gate 	usb_port_mask_t rval = 0;
36537c478bd9Sstevel@tonic-gate 	int i;
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
36567c478bd9Sstevel@tonic-gate 		rval |= data->b_rptr[i] << (i * 8);
36577c478bd9Sstevel@tonic-gate 	}
36587c478bd9Sstevel@tonic-gate 
36597c478bd9Sstevel@tonic-gate 	return (rval);
36607c478bd9Sstevel@tonic-gate }
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate /*
36647c478bd9Sstevel@tonic-gate  * hubd_read_cb:
36657c478bd9Sstevel@tonic-gate  *	interrupt ep1 callback function
36667c478bd9Sstevel@tonic-gate  *
36677c478bd9Sstevel@tonic-gate  *	the status indicates just a change on the pipe with no indication
36687c478bd9Sstevel@tonic-gate  *	of what the change was
36697c478bd9Sstevel@tonic-gate  *
36707c478bd9Sstevel@tonic-gate  *	known conditions:
36717c478bd9Sstevel@tonic-gate  *		- reset port completion
36727c478bd9Sstevel@tonic-gate  *		- connect
36737c478bd9Sstevel@tonic-gate  *		- disconnect
36747c478bd9Sstevel@tonic-gate  *
36757c478bd9Sstevel@tonic-gate  *	for handling the hotplugging, create a new thread that can do
36767c478bd9Sstevel@tonic-gate  *	synchronous usba calls
36777c478bd9Sstevel@tonic-gate  */
36787c478bd9Sstevel@tonic-gate static void
hubd_read_cb(usb_pipe_handle_t pipe,usb_intr_req_t * reqp)36797c478bd9Sstevel@tonic-gate hubd_read_cb(usb_pipe_handle_t pipe, usb_intr_req_t *reqp)
36807c478bd9Sstevel@tonic-gate {
36817c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)(reqp->intr_client_private);
36827c478bd9Sstevel@tonic-gate 	size_t		length;
36837c478bd9Sstevel@tonic-gate 	mblk_t		*data = reqp->intr_data;
36846c7181fcSsl147100 	int		mem_flag = 0;
36856c7181fcSsl147100 	hubd_hotplug_arg_t *arg;
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
3688112116d8Sfb209375 	    "hubd_read_cb: ph=0x%p req=0x%p", (void *)pipe, (void *)reqp);
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	ASSERT((reqp->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 	/*
36937c478bd9Sstevel@tonic-gate 	 * At present, we are not handling notification for completion of
36947c478bd9Sstevel@tonic-gate 	 * asynchronous pipe reset, for which this data ptr could be NULL
36957c478bd9Sstevel@tonic-gate 	 */
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 	if (data == NULL) {
36987c478bd9Sstevel@tonic-gate 		usb_free_intr_req(reqp);
36997c478bd9Sstevel@tonic-gate 
37007c478bd9Sstevel@tonic-gate 		return;
37017c478bd9Sstevel@tonic-gate 	}
37027c478bd9Sstevel@tonic-gate 
37036c7181fcSsl147100 	arg = (hubd_hotplug_arg_t *)kmem_zalloc(
37046c7181fcSsl147100 	    sizeof (hubd_hotplug_arg_t), KM_SLEEP);
37056c7181fcSsl147100 	mem_flag = 1;
37066c7181fcSsl147100 
37077c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate 	if ((hubd->h_dev_state == USB_DEV_SUSPENDED) ||
37107c478bd9Sstevel@tonic-gate 	    (hubd->h_intr_pipe_state != HUBD_INTR_PIPE_ACTIVE)) {
37117c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
37127c478bd9Sstevel@tonic-gate 		usb_free_intr_req(reqp);
37136c7181fcSsl147100 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 		return;
37167c478bd9Sstevel@tonic-gate 	}
37177c478bd9Sstevel@tonic-gate 
37187c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_ep1_ph == pipe);
37197c478bd9Sstevel@tonic-gate 
3720d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	length = MBLKL(data);
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate 	/*
37237c478bd9Sstevel@tonic-gate 	 * Only look at the data and startup the hotplug thread if
37247c478bd9Sstevel@tonic-gate 	 * there actually is data.
37257c478bd9Sstevel@tonic-gate 	 */
37267c478bd9Sstevel@tonic-gate 	if (length != 0) {
37277c478bd9Sstevel@tonic-gate 		usb_port_mask_t port_change = hubd_mblk2portmask(data);
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate 		/*
37307c478bd9Sstevel@tonic-gate 		 * if a port change was already reported and we are waiting for
37317c478bd9Sstevel@tonic-gate 		 * reset port completion then wake up the hotplug thread which
37327c478bd9Sstevel@tonic-gate 		 * should be waiting on reset port completion
37337c478bd9Sstevel@tonic-gate 		 *
37347c478bd9Sstevel@tonic-gate 		 * if there is disconnect event instead of reset completion, let
37357c478bd9Sstevel@tonic-gate 		 * the hotplug thread figure this out
37367c478bd9Sstevel@tonic-gate 		 */
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 		/* remove the reset wait bits from the status */
37397c478bd9Sstevel@tonic-gate 		hubd->h_port_change |= port_change &
37407c478bd9Sstevel@tonic-gate 		    ~hubd->h_port_reset_wait;
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
37437c478bd9Sstevel@tonic-gate 		    "port change=0x%x port_reset_wait=0x%x",
37447c478bd9Sstevel@tonic-gate 		    hubd->h_port_change, hubd->h_port_reset_wait);
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate 		/* there should be only one reset bit active at the time */
37477c478bd9Sstevel@tonic-gate 		if (hubd->h_port_reset_wait & port_change) {
37487c478bd9Sstevel@tonic-gate 			hubd->h_port_reset_wait = 0;
37497c478bd9Sstevel@tonic-gate 			cv_signal(&hubd->h_cv_reset_port);
37507c478bd9Sstevel@tonic-gate 		}
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 		/*
37537c478bd9Sstevel@tonic-gate 		 * kick off the thread only if device is ONLINE and it is not
37547c478bd9Sstevel@tonic-gate 		 * during attaching or detaching
37557c478bd9Sstevel@tonic-gate 		 */
37567c478bd9Sstevel@tonic-gate 		if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
37577c478bd9Sstevel@tonic-gate 		    (!DEVI_IS_ATTACHING(hubd->h_dip)) &&
37587c478bd9Sstevel@tonic-gate 		    (!DEVI_IS_DETACHING(hubd->h_dip)) &&
37597c478bd9Sstevel@tonic-gate 		    (hubd->h_port_change) &&
37607c478bd9Sstevel@tonic-gate 		    (hubd->h_hotplug_thread == 0)) {
37617c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_CALLBACK, hubd->h_log_handle,
37627c478bd9Sstevel@tonic-gate 			    "creating hotplug thread: "
37637c478bd9Sstevel@tonic-gate 			    "dev_state=%d", hubd->h_dev_state);
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate 			/*
37667c478bd9Sstevel@tonic-gate 			 * Mark this device as busy. The will be marked idle
37677c478bd9Sstevel@tonic-gate 			 * if the async req fails or at the exit of  hotplug
37687c478bd9Sstevel@tonic-gate 			 * thread
37697c478bd9Sstevel@tonic-gate 			 */
37707c478bd9Sstevel@tonic-gate 			(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
37717c478bd9Sstevel@tonic-gate 
37726c7181fcSsl147100 			arg->hubd = hubd;
37736c7181fcSsl147100 			arg->hotplug_during_attach = B_FALSE;
37746c7181fcSsl147100 
37757c478bd9Sstevel@tonic-gate 			if (usb_async_req(hubd->h_dip,
37767c478bd9Sstevel@tonic-gate 			    hubd_hotplug_thread,
37776c7181fcSsl147100 			    (void *)arg, 0) == USB_SUCCESS) {
37787c478bd9Sstevel@tonic-gate 				hubd->h_hotplug_thread++;
37796c7181fcSsl147100 				mem_flag = 0;
37807c478bd9Sstevel@tonic-gate 			} else {
37817c478bd9Sstevel@tonic-gate 				/* mark this device as idle */
37827c478bd9Sstevel@tonic-gate 				(void) hubd_pm_idle_component(hubd,
37837c478bd9Sstevel@tonic-gate 				    hubd->h_dip, 0);
37847c478bd9Sstevel@tonic-gate 			}
37857c478bd9Sstevel@tonic-gate 		}
37867c478bd9Sstevel@tonic-gate 	}
37877c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
37887c478bd9Sstevel@tonic-gate 
37896c7181fcSsl147100 	if (mem_flag == 1) {
37906c7181fcSsl147100 		kmem_free(arg, sizeof (hubd_hotplug_arg_t));
37916c7181fcSsl147100 	}
37926c7181fcSsl147100 
37937c478bd9Sstevel@tonic-gate 	usb_free_intr_req(reqp);
37947c478bd9Sstevel@tonic-gate }
37957c478bd9Sstevel@tonic-gate 
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate /*
37987c478bd9Sstevel@tonic-gate  * hubd_hotplug_thread:
37997c478bd9Sstevel@tonic-gate  *	handles resetting of port, and creating children
38007c478bd9Sstevel@tonic-gate  *
38017c478bd9Sstevel@tonic-gate  *	the ports to check are indicated in h_port_change bit mask
38027c478bd9Sstevel@tonic-gate  * XXX note that one time poll doesn't work on the root hub
38037c478bd9Sstevel@tonic-gate  */
38047c478bd9Sstevel@tonic-gate static void
hubd_hotplug_thread(void * arg)38057c478bd9Sstevel@tonic-gate hubd_hotplug_thread(void *arg)
38067c478bd9Sstevel@tonic-gate {
38076c7181fcSsl147100 	hubd_hotplug_arg_t *hd_arg = (hubd_hotplug_arg_t *)arg;
38086c7181fcSsl147100 	hubd_t		*hubd = hd_arg->hubd;
38096c7181fcSsl147100 	boolean_t	attach_flg = hd_arg->hotplug_during_attach;
38107c478bd9Sstevel@tonic-gate 	usb_port_t	port;
38117c478bd9Sstevel@tonic-gate 	uint16_t	nports;
38127c478bd9Sstevel@tonic-gate 	uint16_t	status, change;
38137c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
38147c478bd9Sstevel@tonic-gate 	dev_info_t	*hdip = hubd->h_dip;
38157c478bd9Sstevel@tonic-gate 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
3816c0f24e5bSlg150142 	dev_info_t	*child_dip;
38177c478bd9Sstevel@tonic-gate 	boolean_t	online_child = B_FALSE;
38187c478bd9Sstevel@tonic-gate 	boolean_t	offline_child = B_FALSE;
38197c478bd9Sstevel@tonic-gate 	boolean_t	pwrup_child = B_FALSE;
38203fe80ca4SDan Cross 	int		old_state;
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
38237c478bd9Sstevel@tonic-gate 	    "hubd_hotplug_thread:  started");
38247c478bd9Sstevel@tonic-gate 
382540482326SVincent Wang 	/*
382640482326SVincent Wang 	 * Before console is init'd, we temporarily block the hotplug
382740482326SVincent Wang 	 * threads so that BUS_CONFIG_ONE through hubd_bus_config() can be
382840482326SVincent Wang 	 * processed quickly. This reduces the time needed for vfs_mountroot()
38298e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	 * to mount the root FS from a USB disk. And on SPARC platform,
38308e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	 * in order to load 'consconfig' successfully after OBP is gone,
38318e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	 * we need to check 'modrootloaded' to make sure root filesystem is
38328e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	 * available.
383340482326SVincent Wang 	 */
38348e1b7aa1Sfei feng - Sun Microsystems - Beijing China 	while (!modrootloaded || !consconfig_console_is_ready()) {
383540482326SVincent Wang 		delay(drv_usectohz(10000));
383640482326SVincent Wang 	}
383740482326SVincent Wang 
38386c7181fcSsl147100 	kmem_free(arg, sizeof (hubd_hotplug_arg_t));
38396c7181fcSsl147100 
38407c478bd9Sstevel@tonic-gate 	/*
38417c478bd9Sstevel@tonic-gate 	 * if our bus power entry point is active, process the change
38427c478bd9Sstevel@tonic-gate 	 * on the next notification of interrupt pipe
38437c478bd9Sstevel@tonic-gate 	 */
38447c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
38457c478bd9Sstevel@tonic-gate 	if (hubd->h_bus_pwr || (hubd->h_hotplug_thread > 1)) {
38467c478bd9Sstevel@tonic-gate 		hubd->h_hotplug_thread--;
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate 		/* mark this device as idle */
38497c478bd9Sstevel@tonic-gate 		hubd_pm_idle_component(hubd, hubd->h_dip, 0);
38507c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
38537c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: "
38547c478bd9Sstevel@tonic-gate 		    "bus_power in progress/hotplugging undesirable - quit");
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 		return;
38577c478bd9Sstevel@tonic-gate 	}
38587c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate 	ndi_hold_devi(hdip); /* so we don't race with detach */
38617c478bd9Sstevel@tonic-gate 
38627c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 	/* is this the root hub? */
38657c478bd9Sstevel@tonic-gate 	if (hdip == rh_dip) {
38667c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_PWRED_DOWN) {
38677c478bd9Sstevel@tonic-gate 			hubpm = hubd->h_hubpm;
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 			/* mark the root hub as full power */
38707c478bd9Sstevel@tonic-gate 			hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
3871e5815e7aSJosef 'Jeff' Sipek 			hubpm->hubp_time_at_full_power = gethrtime();
38727c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
38757c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: call pm_power_has_changed");
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 			(void) pm_power_has_changed(hdip, 0,
38787c478bd9Sstevel@tonic-gate 			    USB_DEV_OS_FULL_PWR);
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
38817c478bd9Sstevel@tonic-gate 			hubd->h_dev_state = USB_DEV_ONLINE;
38827c478bd9Sstevel@tonic-gate 		}
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate 	} else {
38857c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
38867c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: not root hub");
38877c478bd9Sstevel@tonic-gate 	}
38887c478bd9Sstevel@tonic-gate 
38897c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate 	/*
38927c478bd9Sstevel@tonic-gate 	 * this ensures one hotplug activity per system at a time.
38937c478bd9Sstevel@tonic-gate 	 * we enter the parent PCI node to have this serialization.
38947c478bd9Sstevel@tonic-gate 	 * this also excludes ioctls and deathrow thread
38957c478bd9Sstevel@tonic-gate 	 * (a bit crude but easier to debug)
38967c478bd9Sstevel@tonic-gate 	 */
38973fe80ca4SDan Cross 	ndi_devi_enter(ddi_get_parent(rh_dip));
38983fe80ca4SDan Cross 	ndi_devi_enter(rh_dip);
38997c478bd9Sstevel@tonic-gate 
39007c478bd9Sstevel@tonic-gate 	/* exclude other threads */
39013fe80ca4SDan Cross 	ndi_devi_enter(hdip);
39027c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
39037c478bd9Sstevel@tonic-gate 
3904e4c316c4SVincent Wang 	ASSERT(hubd->h_intr_pipe_state == HUBD_INTR_PIPE_ACTIVE);
3905e4c316c4SVincent Wang 
3906993e3fafSRobert Mustacchi 	nports = hubd->h_nports;
3907e4c316c4SVincent Wang 
3908e4c316c4SVincent Wang 	hubd_stop_polling(hubd);
3909e4c316c4SVincent Wang 
39107c478bd9Sstevel@tonic-gate 	while ((hubd->h_dev_state == USB_DEV_ONLINE) &&
39117c478bd9Sstevel@tonic-gate 	    (hubd->h_port_change)) {
39127c478bd9Sstevel@tonic-gate 		/*
39137c478bd9Sstevel@tonic-gate 		 * The 0th bit is the hub status change bit.
39147c478bd9Sstevel@tonic-gate 		 * handle loss of local power here
39157c478bd9Sstevel@tonic-gate 		 */
39167c478bd9Sstevel@tonic-gate 		if (hubd->h_port_change & HUB_CHANGE_STATUS) {
39177c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
39187c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: hub status change!");
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate 			/*
39217c478bd9Sstevel@tonic-gate 			 * This should be handled properly.  For now,
39227c478bd9Sstevel@tonic-gate 			 * mask off the bit.
39237c478bd9Sstevel@tonic-gate 			 */
39247c478bd9Sstevel@tonic-gate 			hubd->h_port_change &= ~HUB_CHANGE_STATUS;
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 			/*
39277c478bd9Sstevel@tonic-gate 			 * check and ack hub status
39287c478bd9Sstevel@tonic-gate 			 * this causes stall conditions
39297c478bd9Sstevel@tonic-gate 			 * when local power is removed
39307c478bd9Sstevel@tonic-gate 			 */
39317c478bd9Sstevel@tonic-gate 			(void) hubd_get_hub_status(hubd);
39327c478bd9Sstevel@tonic-gate 		}
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
39357c478bd9Sstevel@tonic-gate 			usb_port_mask_t port_mask;
39367c478bd9Sstevel@tonic-gate 			boolean_t was_connected;
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate 			port_mask = 1 << port;
39397c478bd9Sstevel@tonic-gate 			was_connected =
39407c478bd9Sstevel@tonic-gate 			    (hubd->h_port_state[port] & PORT_STATUS_CCS) &&
39417c478bd9Sstevel@tonic-gate 			    (hubd->h_children_dips[port]);
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
39447c478bd9Sstevel@tonic-gate 			    "hubd_hotplug_thread: "
39457c478bd9Sstevel@tonic-gate 			    "port %d mask=0x%x change=0x%x connected=0x%x",
39467c478bd9Sstevel@tonic-gate 			    port, port_mask, hubd->h_port_change,
39477c478bd9Sstevel@tonic-gate 			    was_connected);
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 			/*
39507c478bd9Sstevel@tonic-gate 			 * is this a port connection that changed?
39517c478bd9Sstevel@tonic-gate 			 */
39527c478bd9Sstevel@tonic-gate 			if ((hubd->h_port_change & port_mask) == 0) {
39537c478bd9Sstevel@tonic-gate 
39547c478bd9Sstevel@tonic-gate 				continue;
39557c478bd9Sstevel@tonic-gate 			}
39567c478bd9Sstevel@tonic-gate 			hubd->h_port_change &= ~port_mask;
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate 			/* ack all changes */
39597c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
3960993e3fafSRobert Mustacchi 			    &status, &change, NULL, HUBD_ACK_ALL_CHANGES);
39617c478bd9Sstevel@tonic-gate 
39627c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
39637c478bd9Sstevel@tonic-gate 			    "handle port %d:\n\t"
39647c478bd9Sstevel@tonic-gate 			    "new status=0x%x change=0x%x was_conn=0x%x ",
39657c478bd9Sstevel@tonic-gate 			    port, status, change, was_connected);
39667c478bd9Sstevel@tonic-gate 
39677c478bd9Sstevel@tonic-gate 			/* Recover a disabled port */
39687c478bd9Sstevel@tonic-gate 			if (change & PORT_CHANGE_PESC) {
39697c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
39707c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
39717c478bd9Sstevel@tonic-gate 				    "port%d Disabled - "
39727c478bd9Sstevel@tonic-gate 				    "status=0x%x, change=0x%x",
39737c478bd9Sstevel@tonic-gate 				    port, status, change);
39747c478bd9Sstevel@tonic-gate 
39757c478bd9Sstevel@tonic-gate 				/*
39767c478bd9Sstevel@tonic-gate 				 * if the port was connected and is still
39777c478bd9Sstevel@tonic-gate 				 * connected, recover the port
39787c478bd9Sstevel@tonic-gate 				 */
39797c478bd9Sstevel@tonic-gate 				if (was_connected && (status &
39807c478bd9Sstevel@tonic-gate 				    PORT_STATUS_CCS)) {
39817c478bd9Sstevel@tonic-gate 					online_child |=
39827c478bd9Sstevel@tonic-gate 					    (hubd_recover_disabled_port(hubd,
39837c478bd9Sstevel@tonic-gate 					    port) == USB_SUCCESS);
39847c478bd9Sstevel@tonic-gate 				}
39857c478bd9Sstevel@tonic-gate 			}
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 			/*
39887c478bd9Sstevel@tonic-gate 			 * Now check what changed on the port
39897c478bd9Sstevel@tonic-gate 			 */
39906c7181fcSsl147100 			if ((change & PORT_CHANGE_CSC) || attach_flg) {
39917c478bd9Sstevel@tonic-gate 				if ((status & PORT_STATUS_CCS) &&
39927c478bd9Sstevel@tonic-gate 				    (!was_connected)) {
39937c478bd9Sstevel@tonic-gate 					/* new device plugged in */
39947c478bd9Sstevel@tonic-gate 					online_child |=
39957c478bd9Sstevel@tonic-gate 					    (hubd_handle_port_connect(hubd,
39967c478bd9Sstevel@tonic-gate 					    port) == USB_SUCCESS);
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 				} else if ((status & PORT_STATUS_CCS) &&
39997c478bd9Sstevel@tonic-gate 				    was_connected) {
40007c478bd9Sstevel@tonic-gate 					/*
40017c478bd9Sstevel@tonic-gate 					 * In this case we can never be sure
40027c478bd9Sstevel@tonic-gate 					 * if the device indeed got hotplugged
40037c478bd9Sstevel@tonic-gate 					 * or the hub is falsely reporting the
40047c478bd9Sstevel@tonic-gate 					 * change.
40057c478bd9Sstevel@tonic-gate 					 */
4006c0f24e5bSlg150142 					child_dip = hubd->h_children_dips[port];
4007c0f24e5bSlg150142 
40087c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
4009c0f24e5bSlg150142 					/*
4010c0f24e5bSlg150142 					 * this ensures we do not race with
4011c0f24e5bSlg150142 					 * other threads which are detaching
4012c0f24e5bSlg150142 					 * the child driver at the same time.
4013c0f24e5bSlg150142 					 */
40143fe80ca4SDan Cross 					ndi_devi_enter(child_dip);
4015c0f24e5bSlg150142 					/*
4016c0f24e5bSlg150142 					 * Now check if the driver remains
4017c0f24e5bSlg150142 					 * attached.
4018c0f24e5bSlg150142 					 */
4019c0f24e5bSlg150142 					if (i_ddi_devi_attached(child_dip)) {
4020c0f24e5bSlg150142 						/*
4021c0f24e5bSlg150142 						 * first post a disconnect event
4022c0f24e5bSlg150142 						 * to the child.
4023c0f24e5bSlg150142 						 */
40247c478bd9Sstevel@tonic-gate 						hubd_post_event(hubd, port,
40257c478bd9Sstevel@tonic-gate 						    USBA_EVENT_TAG_HOT_REMOVAL);
40267c478bd9Sstevel@tonic-gate 						mutex_enter(HUBD_MUTEX(hubd));
40277c478bd9Sstevel@tonic-gate 
40287c478bd9Sstevel@tonic-gate 						/*
4029c0f24e5bSlg150142 						 * then reset the port and
4030c0f24e5bSlg150142 						 * recover the device
40317c478bd9Sstevel@tonic-gate 						 */
40327c478bd9Sstevel@tonic-gate 						online_child |=
4033c0f24e5bSlg150142 						    (hubd_handle_port_connect(
4034c0f24e5bSlg150142 						    hubd, port) == USB_SUCCESS);
4035c0f24e5bSlg150142 
4036c0f24e5bSlg150142 						mutex_exit(HUBD_MUTEX(hubd));
4037c0f24e5bSlg150142 					}
4038c0f24e5bSlg150142 
40393fe80ca4SDan Cross 					ndi_devi_exit(child_dip);
4040c0f24e5bSlg150142 					mutex_enter(HUBD_MUTEX(hubd));
40417c478bd9Sstevel@tonic-gate 				} else if (was_connected) {
40427c478bd9Sstevel@tonic-gate 					/* this is a disconnect */
40437c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
40447c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
40457c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_REMOVAL);
40467c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 					offline_child = B_TRUE;
40497c478bd9Sstevel@tonic-gate 				}
40507c478bd9Sstevel@tonic-gate 			}
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate 			/*
40537c478bd9Sstevel@tonic-gate 			 * Check if any port is coming out of suspend
40547c478bd9Sstevel@tonic-gate 			 */
40557c478bd9Sstevel@tonic-gate 			if (change & PORT_CHANGE_PSSC) {
40567c478bd9Sstevel@tonic-gate 				/* a resuming device could have disconnected */
40577c478bd9Sstevel@tonic-gate 				if (was_connected &&
40587c478bd9Sstevel@tonic-gate 				    hubd->h_children_dips[port]) {
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate 					/* device on this port resuming */
40617c478bd9Sstevel@tonic-gate 					dev_info_t *dip;
40627c478bd9Sstevel@tonic-gate 
40637c478bd9Sstevel@tonic-gate 					dip = hubd->h_children_dips[port];
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 					/*
40667c478bd9Sstevel@tonic-gate 					 * Don't raise power on detaching child
40677c478bd9Sstevel@tonic-gate 					 */
40687c478bd9Sstevel@tonic-gate 					if (!DEVI_IS_DETACHING(dip)) {
40697c478bd9Sstevel@tonic-gate 						/*
40707c478bd9Sstevel@tonic-gate 						 * As this child is not
40717c478bd9Sstevel@tonic-gate 						 * detaching, we set this
40727c478bd9Sstevel@tonic-gate 						 * flag, causing bus_ctls
40737c478bd9Sstevel@tonic-gate 						 * to stall detach till
40747c478bd9Sstevel@tonic-gate 						 * pm_raise_power returns
40757c478bd9Sstevel@tonic-gate 						 * and flag it for a deferred
40767c478bd9Sstevel@tonic-gate 						 * raise_power.
40777c478bd9Sstevel@tonic-gate 						 *
40787c478bd9Sstevel@tonic-gate 						 * pm_raise_power is deferred
40797c478bd9Sstevel@tonic-gate 						 * because we need to release
40807c478bd9Sstevel@tonic-gate 						 * the locks first.
40817c478bd9Sstevel@tonic-gate 						 */
40827c478bd9Sstevel@tonic-gate 						hubd->h_port_state[port] |=
40837c478bd9Sstevel@tonic-gate 						    HUBD_CHILD_RAISE_POWER;
40847c478bd9Sstevel@tonic-gate 						pwrup_child = B_TRUE;
40857c478bd9Sstevel@tonic-gate 						mutex_exit(HUBD_MUTEX(hubd));
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 						/*
40887c478bd9Sstevel@tonic-gate 						 * make sure that child
40897c478bd9Sstevel@tonic-gate 						 * doesn't disappear
40907c478bd9Sstevel@tonic-gate 						 */
40917c478bd9Sstevel@tonic-gate 						ndi_hold_devi(dip);
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate 						mutex_enter(HUBD_MUTEX(hubd));
40947c478bd9Sstevel@tonic-gate 					}
40957c478bd9Sstevel@tonic-gate 				}
40967c478bd9Sstevel@tonic-gate 			}
4097fef1e07eSsl147100 
4098fef1e07eSsl147100 			/*
4099fef1e07eSsl147100 			 * Check if the port is over-current
4100fef1e07eSsl147100 			 */
4101fef1e07eSsl147100 			if (change & PORT_CHANGE_OCIC) {
4102fef1e07eSsl147100 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
4103fef1e07eSsl147100 				    hubd->h_log_handle,
4104fef1e07eSsl147100 				    "Port%d in over current condition, "
4105fef1e07eSsl147100 				    "please check the attached device to "
4106fef1e07eSsl147100 				    "clear the condition. The system will "
4107fef1e07eSsl147100 				    "try to recover the port, but if not "
4108fef1e07eSsl147100 				    "successful, you need to re-connect "
4109fef1e07eSsl147100 				    "the hub or reboot the system to bring "
4110fef1e07eSsl147100 				    "the port back to work", port);
4111fef1e07eSsl147100 
4112fef1e07eSsl147100 				if (!(status & PORT_STATUS_PPS)) {
4113fef1e07eSsl147100 					/*
4114fef1e07eSsl147100 					 * Try to enable port power, but
4115fef1e07eSsl147100 					 * possibly fail. Ignore failure
4116fef1e07eSsl147100 					 */
4117fef1e07eSsl147100 					(void) hubd_enable_port_power(hubd,
4118fef1e07eSsl147100 					    port);
4119fef1e07eSsl147100 
4120fef1e07eSsl147100 					/*
4121fef1e07eSsl147100 					 * Delay some time to avoid
4122fef1e07eSsl147100 					 * over-current event to happen
4123fef1e07eSsl147100 					 * too frequently in some cases
4124fef1e07eSsl147100 					 */
4125fef1e07eSsl147100 					mutex_exit(HUBD_MUTEX(hubd));
4126fef1e07eSsl147100 					delay(drv_usectohz(500000));
4127fef1e07eSsl147100 					mutex_enter(HUBD_MUTEX(hubd));
4128fef1e07eSsl147100 				}
4129fef1e07eSsl147100 			}
41307c478bd9Sstevel@tonic-gate 		}
41317c478bd9Sstevel@tonic-gate 	}
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate 	/* release locks so we can do a devfs_clean */
41347c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
41357c478bd9Sstevel@tonic-gate 
41367c478bd9Sstevel@tonic-gate 	/* delete cached dv_node's but drop locks first */
41373fe80ca4SDan Cross 	ndi_devi_exit(hdip);
41383fe80ca4SDan Cross 	ndi_devi_exit(rh_dip);
41393fe80ca4SDan Cross 	ndi_devi_exit(ddi_get_parent(rh_dip));
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate 	(void) devfs_clean(rh_dip, NULL, 0);
41427c478bd9Sstevel@tonic-gate 
41437c478bd9Sstevel@tonic-gate 	/* now check if any children need onlining */
41447c478bd9Sstevel@tonic-gate 	if (online_child) {
41457c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
41467c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: onlining children");
41477c478bd9Sstevel@tonic-gate 
41487c478bd9Sstevel@tonic-gate 		(void) ndi_devi_online(hubd->h_dip, 0);
41497c478bd9Sstevel@tonic-gate 	}
41507c478bd9Sstevel@tonic-gate 
41517c478bd9Sstevel@tonic-gate 	/* now check if any disconnected devices need to be cleaned up */
41527c478bd9Sstevel@tonic-gate 	if (offline_child) {
41537c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
41547c478bd9Sstevel@tonic-gate 		    "hubd_hotplug_thread: scheduling cleanup");
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
41577c478bd9Sstevel@tonic-gate 	}
41587c478bd9Sstevel@tonic-gate 
41597c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate 	/* now raise power on the children that have woken up */
41627c478bd9Sstevel@tonic-gate 	if (pwrup_child) {
41637c478bd9Sstevel@tonic-gate 		old_state = hubd->h_dev_state;
41647c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_HUB_CHILD_PWRLVL;
41657c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
41667c478bd9Sstevel@tonic-gate 			if (hubd->h_port_state[port] & HUBD_CHILD_RAISE_POWER) {
41677c478bd9Sstevel@tonic-gate 				dev_info_t *dip = hubd->h_children_dips[port];
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
41707c478bd9Sstevel@tonic-gate 
41717c478bd9Sstevel@tonic-gate 				/* Get the device to full power */
41727c478bd9Sstevel@tonic-gate 				(void) pm_busy_component(dip, 0);
41737c478bd9Sstevel@tonic-gate 				(void) pm_raise_power(dip, 0,
41747c478bd9Sstevel@tonic-gate 				    USB_DEV_OS_FULL_PWR);
41757c478bd9Sstevel@tonic-gate 				(void) pm_idle_component(dip, 0);
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate 				/* release the hold on the child */
41787c478bd9Sstevel@tonic-gate 				ndi_rele_devi(dip);
41797c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
41807c478bd9Sstevel@tonic-gate 				hubd->h_port_state[port] &=
41817c478bd9Sstevel@tonic-gate 				    ~HUBD_CHILD_RAISE_POWER;
41827c478bd9Sstevel@tonic-gate 			}
41837c478bd9Sstevel@tonic-gate 		}
41847c478bd9Sstevel@tonic-gate 		/*
41857c478bd9Sstevel@tonic-gate 		 * make sure that we don't accidentally
41867c478bd9Sstevel@tonic-gate 		 * over write the disconnect state
41877c478bd9Sstevel@tonic-gate 		 */
41887c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_HUB_CHILD_PWRLVL) {
41897c478bd9Sstevel@tonic-gate 			hubd->h_dev_state = old_state;
41907c478bd9Sstevel@tonic-gate 		}
41917c478bd9Sstevel@tonic-gate 	}
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate 	/*
41947c478bd9Sstevel@tonic-gate 	 * start polling can immediately kick off read callback
41957c478bd9Sstevel@tonic-gate 	 * we need to set the h_hotplug_thread to 0 so that
41967c478bd9Sstevel@tonic-gate 	 * the callback is not dropped
4197ffcd51f3Slg150142 	 *
4198ffcd51f3Slg150142 	 * if there is device during reset, still stop polling to avoid the
4199ffcd51f3Slg150142 	 * read callback interrupting the reset, the polling will be started
4200ffcd51f3Slg150142 	 * in hubd_reset_thread.
42017c478bd9Sstevel@tonic-gate 	 */
4202ffcd51f3Slg150142 	for (port = 1; port <= MAX_PORTS; port++) {
4203ffcd51f3Slg150142 		if (hubd->h_reset_port[port]) {
4204ffcd51f3Slg150142 
4205ffcd51f3Slg150142 			break;
4206ffcd51f3Slg150142 		}
4207ffcd51f3Slg150142 	}
4208ffcd51f3Slg150142 	if (port > MAX_PORTS) {
42097c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
4210ffcd51f3Slg150142 	}
42117c478bd9Sstevel@tonic-gate 
42127c478bd9Sstevel@tonic-gate 	/*
42137c478bd9Sstevel@tonic-gate 	 * Earlier we would set the h_hotplug_thread = 0 before
42147c478bd9Sstevel@tonic-gate 	 * polling was restarted  so that
42157c478bd9Sstevel@tonic-gate 	 * if there is any root hub status change interrupt, we can still kick
42167c478bd9Sstevel@tonic-gate 	 * off the hotplug thread. This was valid when this interrupt was
42177c478bd9Sstevel@tonic-gate 	 * delivered in hardware, and only ONE interrupt would be delivered.
42187c478bd9Sstevel@tonic-gate 	 * Now that we poll on the root hub looking for status change in
42197c478bd9Sstevel@tonic-gate 	 * software, this assignment is no longer required.
42207c478bd9Sstevel@tonic-gate 	 */
42217c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
42227c478bd9Sstevel@tonic-gate 
42237c478bd9Sstevel@tonic-gate 	/* mark this device as idle */
42247c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
42257c478bd9Sstevel@tonic-gate 
4226ffcd51f3Slg150142 	cv_broadcast(&hubd->h_cv_hotplug_dev);
4227ffcd51f3Slg150142 
42287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
42297c478bd9Sstevel@tonic-gate 	    "hubd_hotplug_thread: exit");
42307c478bd9Sstevel@tonic-gate 
42317c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate 	ndi_rele_devi(hdip);
42347c478bd9Sstevel@tonic-gate }
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate 
42377c478bd9Sstevel@tonic-gate /*
42387c478bd9Sstevel@tonic-gate  * hubd_handle_port_connect:
42397c478bd9Sstevel@tonic-gate  *	Transition a port from Disabled to Enabled.  Ensure that the
42407c478bd9Sstevel@tonic-gate  *	port is in the correct state before attempting to
42417c478bd9Sstevel@tonic-gate  *	access the device.
42427c478bd9Sstevel@tonic-gate  */
42437c478bd9Sstevel@tonic-gate static int
hubd_handle_port_connect(hubd_t * hubd,usb_port_t port)42447c478bd9Sstevel@tonic-gate hubd_handle_port_connect(hubd_t *hubd, usb_port_t port)
42457c478bd9Sstevel@tonic-gate {
42467c478bd9Sstevel@tonic-gate 	int			rval;
42477c478bd9Sstevel@tonic-gate 	int			retry;
42487c478bd9Sstevel@tonic-gate 	long			time_delay;
42497c478bd9Sstevel@tonic-gate 	long			settling_time;
42507c478bd9Sstevel@tonic-gate 	uint16_t		status;
42517c478bd9Sstevel@tonic-gate 	uint16_t		change;
4252993e3fafSRobert Mustacchi 	usb_port_status_t	speed;
42537c478bd9Sstevel@tonic-gate 	usb_addr_t		hubd_usb_addr;
42547c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
42557c478bd9Sstevel@tonic-gate 	usb_port_status_t	port_status = 0;
42567c478bd9Sstevel@tonic-gate 	usb_port_status_t	hub_port_status = 0;
42577c478bd9Sstevel@tonic-gate 
42587c478bd9Sstevel@tonic-gate 	/* Get the hub address and port status */
42597c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_device;
42607c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
42617c478bd9Sstevel@tonic-gate 	hubd_usb_addr = usba_device->usb_addr;
42627c478bd9Sstevel@tonic-gate 	hub_port_status = usba_device->usb_port_status;
42637c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 	/*
42667c478bd9Sstevel@tonic-gate 	 * If a device is connected, transition the
42677c478bd9Sstevel@tonic-gate 	 * port from Disabled to the Enabled state.
42687c478bd9Sstevel@tonic-gate 	 * The device will receive downstream packets
42697c478bd9Sstevel@tonic-gate 	 * in the Enabled state.
42707c478bd9Sstevel@tonic-gate 	 *
42717c478bd9Sstevel@tonic-gate 	 * reset port and wait for the hub to report
42727c478bd9Sstevel@tonic-gate 	 * completion
42737c478bd9Sstevel@tonic-gate 	 */
42747c478bd9Sstevel@tonic-gate 	change = status = 0;
42757c478bd9Sstevel@tonic-gate 
42767c478bd9Sstevel@tonic-gate 	/*
42777c478bd9Sstevel@tonic-gate 	 * According to section 9.1.2 of USB 2.0 spec, the host should
42787c478bd9Sstevel@tonic-gate 	 * wait for atleast 100ms to allow completion of an insertion
42797c478bd9Sstevel@tonic-gate 	 * process and for power at the device to become stable.
42807c478bd9Sstevel@tonic-gate 	 * We wait for 200 ms
42817c478bd9Sstevel@tonic-gate 	 */
42827c478bd9Sstevel@tonic-gate 	settling_time = drv_usectohz(hubd_device_delay / 5);
42837c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
42847c478bd9Sstevel@tonic-gate 	delay(settling_time);
42857c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
42867c478bd9Sstevel@tonic-gate 
42877c478bd9Sstevel@tonic-gate 	/* calculate 600 ms delay time */
42887c478bd9Sstevel@tonic-gate 	time_delay = (6 * drv_usectohz(hubd_device_delay)) / 10;
42897c478bd9Sstevel@tonic-gate 
42907c478bd9Sstevel@tonic-gate 	for (retry = 0; (hubd->h_dev_state == USB_DEV_ONLINE) &&
42917c478bd9Sstevel@tonic-gate 	    (retry < hubd_retry_enumerate); retry++) {
42927c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
42937c478bd9Sstevel@tonic-gate 		    "resetting port%d, retry=%d", port, retry);
42947c478bd9Sstevel@tonic-gate 
42957c478bd9Sstevel@tonic-gate 		if ((rval = hubd_reset_port(hubd, port)) != USB_SUCCESS) {
42967c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd,
4297993e3fafSRobert Mustacchi 			    port, &status, &change, &speed, 0);
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate 			/* continue only if port is still connected */
43007c478bd9Sstevel@tonic-gate 			if (status & PORT_STATUS_CCS) {
43017c478bd9Sstevel@tonic-gate 				continue;
43027c478bd9Sstevel@tonic-gate 			}
43037c478bd9Sstevel@tonic-gate 
43047c478bd9Sstevel@tonic-gate 			/* carry on regardless */
43057c478bd9Sstevel@tonic-gate 		}
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 		/*
43087c478bd9Sstevel@tonic-gate 		 * according to USB 2.0 spec section 11.24.2.7.1.2
43097c478bd9Sstevel@tonic-gate 		 * at the end of port reset, the hub enables the port.
43107c478bd9Sstevel@tonic-gate 		 * But for some strange reasons, uhci port remains disabled.
43117c478bd9Sstevel@tonic-gate 		 * And because the port remains disabled for the settling
43127c478bd9Sstevel@tonic-gate 		 * time below, the device connected to the port gets wedged
43137c478bd9Sstevel@tonic-gate 		 * - fails to enumerate (device not responding)
43147c478bd9Sstevel@tonic-gate 		 * Hence, we enable it here immediately and later again after
43157c478bd9Sstevel@tonic-gate 		 * the delay
43167c478bd9Sstevel@tonic-gate 		 */
43177c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port(hubd, port);
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 		/* we skip this delay in the first iteration */
43207c478bd9Sstevel@tonic-gate 		if (retry) {
43217c478bd9Sstevel@tonic-gate 			/*
43227c478bd9Sstevel@tonic-gate 			 * delay for device to signal disconnect/connect so
43237c478bd9Sstevel@tonic-gate 			 * that hub properly recognizes the speed of the device
43247c478bd9Sstevel@tonic-gate 			 */
43257c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
43267c478bd9Sstevel@tonic-gate 			delay(settling_time);
43277c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate 			/*
43307c478bd9Sstevel@tonic-gate 			 * When a low speed device is connected to any port of
43317c478bd9Sstevel@tonic-gate 			 * PPX it has to be explicitly enabled
43327c478bd9Sstevel@tonic-gate 			 * Also, if device intentionally signals
43337c478bd9Sstevel@tonic-gate 			 * disconnect/connect, it will disable the port.
43347c478bd9Sstevel@tonic-gate 			 * So enable it again.
43357c478bd9Sstevel@tonic-gate 			 */
43367c478bd9Sstevel@tonic-gate 			(void) hubd_enable_port(hubd, port);
43377c478bd9Sstevel@tonic-gate 		}
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate 		if ((rval = hubd_determine_port_status(hubd, port, &status,
4340993e3fafSRobert Mustacchi 		    &change, &speed, 0)) != USB_SUCCESS) {
43417c478bd9Sstevel@tonic-gate 
4342d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
43437c478bd9Sstevel@tonic-gate 			    "getting status failed (%d)", rval);
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate 			(void) hubd_disable_port(hubd, port);
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 			continue;
43487c478bd9Sstevel@tonic-gate 		}
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 		if (status & PORT_STATUS_POCI) {
4351d291d9f2Sfrits 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
43527c478bd9Sstevel@tonic-gate 			    "port %d overcurrent", port);
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate 			(void) hubd_disable_port(hubd, port);
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 			/* ack changes */
43577c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd,
4358993e3fafSRobert Mustacchi 			    port, &status, &change, &speed, PORT_CHANGE_OCIC);
43597c478bd9Sstevel@tonic-gate 
43607c478bd9Sstevel@tonic-gate 			continue;
43617c478bd9Sstevel@tonic-gate 		}
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate 		/* is status really OK? */
43647c478bd9Sstevel@tonic-gate 		if ((status & PORT_STATUS_OK) != PORT_STATUS_OK) {
43657c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
43667c478bd9Sstevel@tonic-gate 			    "port %d status (0x%x) not OK on retry %d",
43677c478bd9Sstevel@tonic-gate 			    port, status, retry);
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 			/* check if we still have the connection */
43707c478bd9Sstevel@tonic-gate 			if (!(status & PORT_STATUS_CCS)) {
43717c478bd9Sstevel@tonic-gate 				/* lost connection, set exit condition */
43727c478bd9Sstevel@tonic-gate 				retry = hubd_retry_enumerate;
43737c478bd9Sstevel@tonic-gate 
43747c478bd9Sstevel@tonic-gate 				break;
43757c478bd9Sstevel@tonic-gate 			}
43767c478bd9Sstevel@tonic-gate 		} else {
4377993e3fafSRobert Mustacchi 			port_status = speed;
43787c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
43797c478bd9Sstevel@tonic-gate 			    "creating child port%d, status=0x%x "
43807c478bd9Sstevel@tonic-gate 			    "port status=0x%x",
43817c478bd9Sstevel@tonic-gate 			    port, status, port_status);
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate 			/*
43847c478bd9Sstevel@tonic-gate 			 * if the child already exists, set addrs and config
43857c478bd9Sstevel@tonic-gate 			 * to the device post connect event to the child
43867c478bd9Sstevel@tonic-gate 			 */
43877c478bd9Sstevel@tonic-gate 			if (hubd->h_children_dips[port]) {
43887c478bd9Sstevel@tonic-gate 				/* set addrs to this device */
43897c478bd9Sstevel@tonic-gate 				rval = hubd_setdevaddr(hubd, port);
43907c478bd9Sstevel@tonic-gate 
43917c478bd9Sstevel@tonic-gate 				/*
43927c478bd9Sstevel@tonic-gate 				 * This delay is important for the CATC hub
43937c478bd9Sstevel@tonic-gate 				 * to enumerate. But, avoid delay in the first
43947c478bd9Sstevel@tonic-gate 				 * iteration
43957c478bd9Sstevel@tonic-gate 				 */
43967c478bd9Sstevel@tonic-gate 				if (retry) {
43977c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
43987c478bd9Sstevel@tonic-gate 					delay(drv_usectohz(
43997c478bd9Sstevel@tonic-gate 					    hubd_device_delay/100));
44007c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
44017c478bd9Sstevel@tonic-gate 				}
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate 				if (rval == USB_SUCCESS) {
44047c478bd9Sstevel@tonic-gate 					/*
4405ffcd51f3Slg150142 					 * if the port is resetting, check if
4406ffcd51f3Slg150142 					 * device's descriptors have changed.
4407ffcd51f3Slg150142 					 */
4408ffcd51f3Slg150142 					if ((hubd->h_reset_port[port]) &&
4409ffcd51f3Slg150142 					    (hubd_check_same_device(hubd,
4410ffcd51f3Slg150142 					    port) != USB_SUCCESS)) {
4411ffcd51f3Slg150142 						retry = hubd_retry_enumerate;
4412ffcd51f3Slg150142 
4413ffcd51f3Slg150142 						break;
4414ffcd51f3Slg150142 					}
4415ffcd51f3Slg150142 
4416ffcd51f3Slg150142 					/*
44177c478bd9Sstevel@tonic-gate 					 * set the default config for
44187c478bd9Sstevel@tonic-gate 					 * this device
44197c478bd9Sstevel@tonic-gate 					 */
44207c478bd9Sstevel@tonic-gate 					hubd_setdevconfig(hubd, port);
44217c478bd9Sstevel@tonic-gate 
44227c478bd9Sstevel@tonic-gate 					/*
4423ffcd51f3Slg150142 					 * if we are doing Default reset, do
4424ffcd51f3Slg150142 					 * not post reconnect event since we
4425ffcd51f3Slg150142 					 * don't know where reset function is
4426ffcd51f3Slg150142 					 * called.
4427ffcd51f3Slg150142 					 */
4428ffcd51f3Slg150142 					if (hubd->h_reset_port[port]) {
4429ffcd51f3Slg150142 
4430ffcd51f3Slg150142 						return (USB_SUCCESS);
4431ffcd51f3Slg150142 					}
4432ffcd51f3Slg150142 
4433ffcd51f3Slg150142 					/*
44347c478bd9Sstevel@tonic-gate 					 * indicate to the child that
44357c478bd9Sstevel@tonic-gate 					 * it is online again
44367c478bd9Sstevel@tonic-gate 					 */
44377c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
44387c478bd9Sstevel@tonic-gate 					hubd_post_event(hubd, port,
44397c478bd9Sstevel@tonic-gate 					    USBA_EVENT_TAG_HOT_INSERTION);
44407c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 					return (USB_SUCCESS);
44437c478bd9Sstevel@tonic-gate 				}
44447c478bd9Sstevel@tonic-gate 			} else {
44457c478bd9Sstevel@tonic-gate 				/*
44467c478bd9Sstevel@tonic-gate 				 * We need to release access here
44477c478bd9Sstevel@tonic-gate 				 * so that busctls on other ports can
44487c478bd9Sstevel@tonic-gate 				 * continue and don't cause a deadlock
44497c478bd9Sstevel@tonic-gate 				 * when busctl and removal of prom node
44507c478bd9Sstevel@tonic-gate 				 * takes concurrently. This also ensures
44517c478bd9Sstevel@tonic-gate 				 * busctls for attach of successfully
44527c478bd9Sstevel@tonic-gate 				 * enumerated devices on other ports can
44537c478bd9Sstevel@tonic-gate 				 * continue concurrently with the process
44547c478bd9Sstevel@tonic-gate 				 * of enumerating the new devices. This
44557c478bd9Sstevel@tonic-gate 				 * reduces the overall boot time of the system.
44567c478bd9Sstevel@tonic-gate 				 */
44577c478bd9Sstevel@tonic-gate 				rval = hubd_create_child(hubd->h_dip,
44587c478bd9Sstevel@tonic-gate 				    hubd,
44597c478bd9Sstevel@tonic-gate 				    hubd->h_usba_device,
44607c478bd9Sstevel@tonic-gate 				    port_status, port,
44617c478bd9Sstevel@tonic-gate 				    retry);
44627c478bd9Sstevel@tonic-gate 				if (rval == USB_SUCCESS) {
44637c478bd9Sstevel@tonic-gate 					usba_update_hotplug_stats(hubd->h_dip,
44647c478bd9Sstevel@tonic-gate 					    USBA_TOTAL_HOTPLUG_SUCCESS|
44657c478bd9Sstevel@tonic-gate 					    USBA_HOTPLUG_SUCCESS);
44667c478bd9Sstevel@tonic-gate 					hubd->h_total_hotplug_success++;
44677c478bd9Sstevel@tonic-gate 
44687c478bd9Sstevel@tonic-gate 					if (retry > 0) {
4469d291d9f2Sfrits 						USB_DPRINTF_L2(
44707c478bd9Sstevel@tonic-gate 						    DPRINT_MASK_HOTPLUG,
44717c478bd9Sstevel@tonic-gate 						    hubd->h_log_handle,
44727c478bd9Sstevel@tonic-gate 						    "device on port %d "
44737c478bd9Sstevel@tonic-gate 						    "enumerated after %d %s",
44747c478bd9Sstevel@tonic-gate 						    port, retry,
44757c478bd9Sstevel@tonic-gate 						    (retry > 1) ? "retries" :
44767c478bd9Sstevel@tonic-gate 						    "retry");
44777c478bd9Sstevel@tonic-gate 
44787c478bd9Sstevel@tonic-gate 					}
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate 					return (USB_SUCCESS);
44817c478bd9Sstevel@tonic-gate 				}
44827c478bd9Sstevel@tonic-gate 			}
44837c478bd9Sstevel@tonic-gate 		}
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate 		/* wait a while until it settles? */
44867c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
44877c478bd9Sstevel@tonic-gate 		    "disabling port %d again", port);
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
44907c478bd9Sstevel@tonic-gate 		if (retry) {
44917c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
44927c478bd9Sstevel@tonic-gate 			delay(time_delay);
44937c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
44947c478bd9Sstevel@tonic-gate 		}
44957c478bd9Sstevel@tonic-gate 
44967c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
44977c478bd9Sstevel@tonic-gate 		    "retrying on port %d", port);
44987c478bd9Sstevel@tonic-gate 	}
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	if (retry >= hubd_retry_enumerate) {
45017c478bd9Sstevel@tonic-gate 		/*
45027c478bd9Sstevel@tonic-gate 		 * If it is a High Speed Root Hub and connected device
45037c478bd9Sstevel@tonic-gate 		 * Is a Low/Full Speed, it will be handled by USB 1.1
45047c478bd9Sstevel@tonic-gate 		 * Host Controller. In this case, USB 2.0 Host Controller
45057c478bd9Sstevel@tonic-gate 		 * will transfer the ownership of this port to USB 1.1
45067c478bd9Sstevel@tonic-gate 		 * Host Controller. So don't display any error message on
4507993e3fafSRobert Mustacchi 		 * the console. Note, this isn't the case for USB 3.x.
45087c478bd9Sstevel@tonic-gate 		 */
45097c478bd9Sstevel@tonic-gate 		if ((hubd_usb_addr == ROOT_HUB_ADDR) &&
45107c478bd9Sstevel@tonic-gate 		    (hub_port_status == USBA_HIGH_SPEED_DEV) &&
45117c478bd9Sstevel@tonic-gate 		    (port_status != USBA_HIGH_SPEED_DEV)) {
45127c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
45137c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
45147c478bd9Sstevel@tonic-gate 			    "hubd_handle_port_connect: Low/Full speed "
45157c478bd9Sstevel@tonic-gate 			    "device is connected to High Speed root hub");
45167c478bd9Sstevel@tonic-gate 		} else {
45177c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
45187c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
45197c478bd9Sstevel@tonic-gate 			    "Connecting device on port %d failed", port);
45207c478bd9Sstevel@tonic-gate 		}
45217c478bd9Sstevel@tonic-gate 
45227c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
45237c478bd9Sstevel@tonic-gate 		usba_update_hotplug_stats(hubd->h_dip,
45247c478bd9Sstevel@tonic-gate 		    USBA_TOTAL_HOTPLUG_FAILURE|USBA_HOTPLUG_FAILURE);
45257c478bd9Sstevel@tonic-gate 		hubd->h_total_hotplug_failure++;
45267c478bd9Sstevel@tonic-gate 
45277c478bd9Sstevel@tonic-gate 		/*
45287c478bd9Sstevel@tonic-gate 		 * the port should be automagically
45297c478bd9Sstevel@tonic-gate 		 * disabled but just in case, we do
45307c478bd9Sstevel@tonic-gate 		 * it here
45317c478bd9Sstevel@tonic-gate 		 */
45327c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port(hubd, port);
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 		/* ack all changes because we disabled this port */
45357c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd,
4536993e3fafSRobert Mustacchi 		    port, &status, &change, NULL, HUBD_ACK_ALL_CHANGES);
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 	}
45397c478bd9Sstevel@tonic-gate 
45407c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
45417c478bd9Sstevel@tonic-gate }
45427c478bd9Sstevel@tonic-gate 
45437c478bd9Sstevel@tonic-gate 
45447c478bd9Sstevel@tonic-gate /*
45457c478bd9Sstevel@tonic-gate  * hubd_get_hub_status:
45467c478bd9Sstevel@tonic-gate  */
45477c478bd9Sstevel@tonic-gate static int
hubd_get_hub_status(hubd_t * hubd)45487c478bd9Sstevel@tonic-gate hubd_get_hub_status(hubd_t *hubd)
45497c478bd9Sstevel@tonic-gate {
45507c478bd9Sstevel@tonic-gate 	int		rval;
45517c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
45527c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
455335f36846Ssl147100 	uint16_t	stword[2];
45547c478bd9Sstevel@tonic-gate 	uint16_t	status;
45557c478bd9Sstevel@tonic-gate 	uint16_t	change;
45567c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	cfg_descr;
45577c478bd9Sstevel@tonic-gate 	size_t		cfg_length;
45587c478bd9Sstevel@tonic-gate 	uchar_t		*usb_cfg;
45597c478bd9Sstevel@tonic-gate 	uint8_t		MaxPower;
4560fef1e07eSsl147100 	usb_port_t	port;
45617c478bd9Sstevel@tonic-gate 
4562fef1e07eSsl147100 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
456335f36846Ssl147100 	    "hubd_get_hub_status:");
45647c478bd9Sstevel@tonic-gate 
456535f36846Ssl147100 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
45667c478bd9Sstevel@tonic-gate 
456735f36846Ssl147100 	if ((hubd_get_hub_status_words(hubd, stword)) != USB_SUCCESS) {
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
45707c478bd9Sstevel@tonic-gate 	}
457135f36846Ssl147100 	status = stword[0];
457235f36846Ssl147100 	change = stword[1];
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
45757c478bd9Sstevel@tonic-gate 
45767c478bd9Sstevel@tonic-gate 	/* Obtain the raw configuration descriptor */
45777c478bd9Sstevel@tonic-gate 	usb_cfg = usb_get_raw_cfg_data(hubd->h_dip, &cfg_length);
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate 	/* get configuration descriptor */
45807c478bd9Sstevel@tonic-gate 	rval = usb_parse_cfg_descr(usb_cfg, cfg_length,
45817c478bd9Sstevel@tonic-gate 	    &cfg_descr, USB_CFG_DESCR_SIZE);
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate 	if (rval != USB_CFG_DESCR_SIZE) {
45847c478bd9Sstevel@tonic-gate 
4585fef1e07eSsl147100 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
45867c478bd9Sstevel@tonic-gate 		    "get hub configuration descriptor failed.");
45877c478bd9Sstevel@tonic-gate 
45887c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
45917c478bd9Sstevel@tonic-gate 	} else {
45927c478bd9Sstevel@tonic-gate 		MaxPower = cfg_descr.bMaxPower;
45937c478bd9Sstevel@tonic-gate 	}
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 	/* check if local power status changed. */
45967c478bd9Sstevel@tonic-gate 	if (change & C_HUB_LOCAL_POWER_STATUS) {
45977c478bd9Sstevel@tonic-gate 
45987c478bd9Sstevel@tonic-gate 		/*
45997c478bd9Sstevel@tonic-gate 		 * local power has been lost, check the maximum
46007c478bd9Sstevel@tonic-gate 		 * power consumption of current configuration.
46017c478bd9Sstevel@tonic-gate 		 * see USB2.0 spec Table 11-12.
46027c478bd9Sstevel@tonic-gate 		 */
46037c478bd9Sstevel@tonic-gate 		if (status & HUB_LOCAL_POWER_STATUS) {
46047c478bd9Sstevel@tonic-gate 
46057c478bd9Sstevel@tonic-gate 			if (MaxPower == 0) {
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate 				/*
46087c478bd9Sstevel@tonic-gate 				 * Self-powered only hub. Because it could
46097c478bd9Sstevel@tonic-gate 				 * not draw any power from USB bus.
46107c478bd9Sstevel@tonic-gate 				 * It can't work well on this condition.
46117c478bd9Sstevel@tonic-gate 				 */
4612fef1e07eSsl147100 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
46137c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
46147c478bd9Sstevel@tonic-gate 				    "local power has been lost, "
46157c478bd9Sstevel@tonic-gate 				    "please disconnect hub");
46167c478bd9Sstevel@tonic-gate 			} else {
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate 				/*
46197c478bd9Sstevel@tonic-gate 				 * Bus-powered only or self/bus-powered hub.
46207c478bd9Sstevel@tonic-gate 				 */
4621fef1e07eSsl147100 				USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG,
46227c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
46237c478bd9Sstevel@tonic-gate 				    "local power has been lost,"
46247c478bd9Sstevel@tonic-gate 				    "the hub could draw %d"
46257c478bd9Sstevel@tonic-gate 				    " mA power from the USB bus.",
46267c478bd9Sstevel@tonic-gate 				    2*MaxPower);
46277c478bd9Sstevel@tonic-gate 			}
46287c478bd9Sstevel@tonic-gate 
46297c478bd9Sstevel@tonic-gate 		}
46307c478bd9Sstevel@tonic-gate 
4631fef1e07eSsl147100 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
46327c478bd9Sstevel@tonic-gate 		    "clearing feature C_HUB_LOCAL_POWER ");
46337c478bd9Sstevel@tonic-gate 
46347c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
46357c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
4636fef1e07eSsl147100 		    HUB_HANDLE_HUB_FEATURE_TYPE,
46377c478bd9Sstevel@tonic-gate 		    USB_REQ_CLEAR_FEATURE,
46387c478bd9Sstevel@tonic-gate 		    CFS_C_HUB_LOCAL_POWER,
46397c478bd9Sstevel@tonic-gate 		    0,
46407c478bd9Sstevel@tonic-gate 		    0,
46417c478bd9Sstevel@tonic-gate 		    NULL, 0,
46427c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4643fef1e07eSsl147100 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
46447c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
46457c478bd9Sstevel@tonic-gate 			    "clear feature C_HUB_LOCAL_POWER "
46467c478bd9Sstevel@tonic-gate 			    "failed (%d 0x%x %d)",
46477c478bd9Sstevel@tonic-gate 			    rval, completion_reason, cb_flags);
46487c478bd9Sstevel@tonic-gate 		}
46497c478bd9Sstevel@tonic-gate 
46507c478bd9Sstevel@tonic-gate 	}
46517c478bd9Sstevel@tonic-gate 
46527c478bd9Sstevel@tonic-gate 	if (change & C_HUB_OVER_CURRENT) {
46537c478bd9Sstevel@tonic-gate 
46547c478bd9Sstevel@tonic-gate 		if (status & HUB_OVER_CURRENT) {
4655fef1e07eSsl147100 
4656fef1e07eSsl147100 			if (usba_is_root_hub(hubd->h_dip)) {
46577c478bd9Sstevel@tonic-gate 				/*
4658fef1e07eSsl147100 				 * The root hub should be automatically
4659fef1e07eSsl147100 				 * recovered when over-current condition is
4660fef1e07eSsl147100 				 * cleared. But there might be exception and
4661fef1e07eSsl147100 				 * need user interaction to recover.
46627c478bd9Sstevel@tonic-gate 				 */
4663fef1e07eSsl147100 				USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4664fef1e07eSsl147100 				    hubd->h_log_handle,
4665fef1e07eSsl147100 				    "Root hub over current condition, "
4666fef1e07eSsl147100 				    "please check your system to clear the "
4667fef1e07eSsl147100 				    "condition as soon as possible. And you "
4668fef1e07eSsl147100 				    "may need to reboot the system to bring "
4669fef1e07eSsl147100 				    "the root hub back to work if it cannot "
4670fef1e07eSsl147100 				    "recover automatically");
4671fef1e07eSsl147100 			} else {
4672fef1e07eSsl147100 				/*
4673fef1e07eSsl147100 				 * The driver would try to recover port power
4674fef1e07eSsl147100 				 * on over current condition. When the recovery
4675fef1e07eSsl147100 				 * fails, the user may still need to offline
4676fef1e07eSsl147100 				 * this hub in order to recover.
4677fef1e07eSsl147100 				 * The port power is automatically disabled,
4678fef1e07eSsl147100 				 * so we won't see disconnects.
4679fef1e07eSsl147100 				 */
4680fef1e07eSsl147100 				USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
4681fef1e07eSsl147100 				    hubd->h_log_handle,
4682fef1e07eSsl147100 				    "Hub global over current condition, "
4683fef1e07eSsl147100 				    "please disconnect the devices connected "
4684fef1e07eSsl147100 				    "to the hub to clear the condition. And "
4685fef1e07eSsl147100 				    "you may need to re-connect the hub if "
4686fef1e07eSsl147100 				    "the ports do not work");
4687fef1e07eSsl147100 			}
46887c478bd9Sstevel@tonic-gate 		}
46897c478bd9Sstevel@tonic-gate 
4690fef1e07eSsl147100 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
46917c478bd9Sstevel@tonic-gate 		    "clearing feature C_HUB_OVER_CURRENT");
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
46947c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
4695fef1e07eSsl147100 		    HUB_HANDLE_HUB_FEATURE_TYPE,
46967c478bd9Sstevel@tonic-gate 		    USB_REQ_CLEAR_FEATURE,
46977c478bd9Sstevel@tonic-gate 		    CFS_C_HUB_OVER_CURRENT,
46987c478bd9Sstevel@tonic-gate 		    0,
46997c478bd9Sstevel@tonic-gate 		    0,
47007c478bd9Sstevel@tonic-gate 		    NULL, 0,
47017c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4702fef1e07eSsl147100 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
47037c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
47047c478bd9Sstevel@tonic-gate 			    "clear feature C_HUB_OVER_CURRENT "
47057c478bd9Sstevel@tonic-gate 			    "failed (%d 0x%x %d)",
47067c478bd9Sstevel@tonic-gate 			    rval, completion_reason, cb_flags);
47077c478bd9Sstevel@tonic-gate 		}
4708fef1e07eSsl147100 
4709fef1e07eSsl147100 		/*
4710fef1e07eSsl147100 		 * Try to recover all port power if they are turned off.
4711fef1e07eSsl147100 		 * Don't do this for root hub, but rely on the root hub
4712fef1e07eSsl147100 		 * to recover itself.
4713fef1e07eSsl147100 		 */
4714fef1e07eSsl147100 		if (!usba_is_root_hub(hubd->h_dip)) {
4715fef1e07eSsl147100 
4716fef1e07eSsl147100 			mutex_enter(HUBD_MUTEX(hubd));
4717fef1e07eSsl147100 
4718fef1e07eSsl147100 			/*
4719fef1e07eSsl147100 			 * Only check the power status of the 1st port
4720fef1e07eSsl147100 			 * since all port power status should be the same.
4721fef1e07eSsl147100 			 */
4722fef1e07eSsl147100 			(void) hubd_determine_port_status(hubd, 1, &status,
4723993e3fafSRobert Mustacchi 			    &change, NULL, 0);
4724fef1e07eSsl147100 
4725fef1e07eSsl147100 			if (status & PORT_STATUS_PPS) {
4726fef1e07eSsl147100 				return (USB_SUCCESS);
4727fef1e07eSsl147100 			}
4728fef1e07eSsl147100 
4729993e3fafSRobert Mustacchi 			for (port = 1; port <= hubd->h_nports; port++) {
4730fef1e07eSsl147100 				(void) hubd_enable_port_power(hubd, port);
4731fef1e07eSsl147100 			}
4732fef1e07eSsl147100 
4733fef1e07eSsl147100 			mutex_exit(HUBD_MUTEX(hubd));
4734fef1e07eSsl147100 
4735fef1e07eSsl147100 			/*
4736fef1e07eSsl147100 			 * Delay some time to avoid over-current event
4737fef1e07eSsl147100 			 * to happen too frequently in some cases
4738fef1e07eSsl147100 			 */
4739fef1e07eSsl147100 			delay(drv_usectohz(500000));
4740fef1e07eSsl147100 		}
47417c478bd9Sstevel@tonic-gate 	}
47427c478bd9Sstevel@tonic-gate 
47437c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
47447c478bd9Sstevel@tonic-gate 
47457c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
47467c478bd9Sstevel@tonic-gate }
47477c478bd9Sstevel@tonic-gate 
4748993e3fafSRobert Mustacchi /*
4749993e3fafSRobert Mustacchi  * Convert a series of USB status requests from USB 2 and USB 3 into a single
4750993e3fafSRobert Mustacchi  * uniform type. We separate out the speed into its own value from both USB 2
4751993e3fafSRobert Mustacchi  * and USB 3 and from there we transform the status to look like a USB 2 one.
4752993e3fafSRobert Mustacchi  */
4753993e3fafSRobert Mustacchi static void
hubd_status_uniform(hubd_t * hubd,usb_port_t port,uint16_t * status,usb_port_status_t * speed)4754993e3fafSRobert Mustacchi hubd_status_uniform(hubd_t *hubd, usb_port_t port, uint16_t *status,
4755993e3fafSRobert Mustacchi     usb_port_status_t *speed)
4756993e3fafSRobert Mustacchi {
4757993e3fafSRobert Mustacchi 	uint16_t os = *status;
4758993e3fafSRobert Mustacchi 
4759993e3fafSRobert Mustacchi 	hubd->h_port_raw[port] = os;
4760993e3fafSRobert Mustacchi 
4761993e3fafSRobert Mustacchi 	if (hubd->h_usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
4762993e3fafSRobert Mustacchi 		/*
4763993e3fafSRobert Mustacchi 		 * USB 3 devices are always at super speed when plugged into a
4764993e3fafSRobert Mustacchi 		 * super speed hub. However, this is only true if we're talking
4765993e3fafSRobert Mustacchi 		 * about actual hubs. This doesn't hold for the root hub, which
4766993e3fafSRobert Mustacchi 		 * can support USB 3.x, USB 2.x, and USB 1.x devices operating
4767993e3fafSRobert Mustacchi 		 * at different speeds. To handle this, the USB 3 HCD driver
4768993e3fafSRobert Mustacchi 		 * (xhci) uses some of the extra status bits to stash the
4769993e3fafSRobert Mustacchi 		 * current device's detected speed.
4770993e3fafSRobert Mustacchi 		 */
4771993e3fafSRobert Mustacchi 		if (usba_is_root_hub(hubd->h_dip)) {
4772993e3fafSRobert Mustacchi 			if (speed != NULL) {
4773993e3fafSRobert Mustacchi 				*speed = (os & PORT_STATUS_SPMASK_SS) >>
4774993e3fafSRobert Mustacchi 				    PORT_STATUS_SPSHIFT_SS;
4775993e3fafSRobert Mustacchi 			}
4776993e3fafSRobert Mustacchi 		} else {
4777993e3fafSRobert Mustacchi 			if (speed != NULL)
4778993e3fafSRobert Mustacchi 				*speed = USBA_SUPER_SPEED_DEV;
4779993e3fafSRobert Mustacchi 		}
4780993e3fafSRobert Mustacchi 
4781993e3fafSRobert Mustacchi 		if (os & PORT_STATUS_PPS_SS) {
4782993e3fafSRobert Mustacchi 			os &= ~PORT_STATUS_PPS_SS;
4783993e3fafSRobert Mustacchi 			os |= PORT_STATUS_PPS;
4784993e3fafSRobert Mustacchi 			*status = os;
4785993e3fafSRobert Mustacchi 		}
4786993e3fafSRobert Mustacchi 	} else {
4787993e3fafSRobert Mustacchi 		/*
4788993e3fafSRobert Mustacchi 		 * For USB 2, the only thing we need to do is transform the
4789993e3fafSRobert Mustacchi 		 * speed.
4790993e3fafSRobert Mustacchi 		 */
4791993e3fafSRobert Mustacchi 		if (speed == NULL)
4792993e3fafSRobert Mustacchi 			return;
4793993e3fafSRobert Mustacchi 
4794993e3fafSRobert Mustacchi 		if (os & PORT_STATUS_HSDA)
4795993e3fafSRobert Mustacchi 			*speed = USBA_HIGH_SPEED_DEV;
4796993e3fafSRobert Mustacchi 		else if (os & PORT_STATUS_LSDA)
4797993e3fafSRobert Mustacchi 			*speed = USBA_LOW_SPEED_DEV;
4798993e3fafSRobert Mustacchi 		else
4799993e3fafSRobert Mustacchi 			*speed = USBA_FULL_SPEED_DEV;
4800993e3fafSRobert Mustacchi 	}
4801993e3fafSRobert Mustacchi }
4802993e3fafSRobert Mustacchi 
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate /*
4805993e3fafSRobert Mustacchi  * Attempt to reset a port. This feels a bit more complicated than it should be
4806993e3fafSRobert Mustacchi  * in part due to how HCD, change status notifications, and the hotplug thread
4807993e3fafSRobert Mustacchi  * might interact. Basically we try to block port changes by using the
4808993e3fafSRobert Mustacchi  * h_port_reset_wait which says we should get signalled rather than kicking off
4809993e3fafSRobert Mustacchi  * the hotplug thread. We'll give this a shot for about 100ms at best.
48107c478bd9Sstevel@tonic-gate  */
48117c478bd9Sstevel@tonic-gate static int
hubd_reset_port(hubd_t * hubd,usb_port_t port)48127c478bd9Sstevel@tonic-gate hubd_reset_port(hubd_t *hubd, usb_port_t port)
48137c478bd9Sstevel@tonic-gate {
48147c478bd9Sstevel@tonic-gate 	int	rval;
48157c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
48167c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
48177c478bd9Sstevel@tonic-gate 	usb_port_mask_t port_mask = 1 << port;
48187c478bd9Sstevel@tonic-gate 	mblk_t	*data;
48197c478bd9Sstevel@tonic-gate 	uint16_t status;
48207c478bd9Sstevel@tonic-gate 	uint16_t change;
4821d3d50737SRafael Vanoni 	clock_t	delta;
4822993e3fafSRobert Mustacchi 	boolean_t first;
48237c478bd9Sstevel@tonic-gate 
48247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
48257c478bd9Sstevel@tonic-gate 	    "hubd_reset_port: port=%d", port);
48267c478bd9Sstevel@tonic-gate 
48277c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
48287c478bd9Sstevel@tonic-gate 
48297c478bd9Sstevel@tonic-gate 	hubd->h_port_reset_wait |= port_mask;
48307c478bd9Sstevel@tonic-gate 
48317c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
48327c478bd9Sstevel@tonic-gate 
48337c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
48347c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
483535f36846Ssl147100 	    HUB_HANDLE_PORT_FEATURE_TYPE,
48367c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
48377c478bd9Sstevel@tonic-gate 	    CFS_PORT_RESET,
48387c478bd9Sstevel@tonic-gate 	    port,
48397c478bd9Sstevel@tonic-gate 	    0,
48407c478bd9Sstevel@tonic-gate 	    NULL, 0,
48417c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4842d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
48437c478bd9Sstevel@tonic-gate 		    "reset port%d failed (%d 0x%x %d)",
48447c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
48457c478bd9Sstevel@tonic-gate 
48467c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
48497c478bd9Sstevel@tonic-gate 	}
48507c478bd9Sstevel@tonic-gate 
48517c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
48547c478bd9Sstevel@tonic-gate 	    "waiting on cv for reset completion");
48557c478bd9Sstevel@tonic-gate 
48567c478bd9Sstevel@tonic-gate 	/*
48577c478bd9Sstevel@tonic-gate 	 * wait for port status change event
48587c478bd9Sstevel@tonic-gate 	 */
4859d3d50737SRafael Vanoni 	delta = drv_usectohz(hubd_device_delay / 10);
4860993e3fafSRobert Mustacchi 
4861993e3fafSRobert Mustacchi 	first = B_TRUE;
4862993e3fafSRobert Mustacchi 	for (;;) {
4863993e3fafSRobert Mustacchi 		if (delta < 0) {
4864993e3fafSRobert Mustacchi 			rval = USB_FAILURE;
4865993e3fafSRobert Mustacchi 			break;
4866993e3fafSRobert Mustacchi 		}
4867993e3fafSRobert Mustacchi 
4868993e3fafSRobert Mustacchi 		if (first == B_FALSE)
4869993e3fafSRobert Mustacchi 			hubd->h_port_reset_wait |= port_mask;
4870993e3fafSRobert Mustacchi 		else
4871993e3fafSRobert Mustacchi 			first = B_FALSE;
4872993e3fafSRobert Mustacchi 
48737c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, HUBD_ALWAYS_START_POLLING);
48747c478bd9Sstevel@tonic-gate 
48757c478bd9Sstevel@tonic-gate 		/*
4876993e3fafSRobert Mustacchi 		 * Regardless of the status, we always check to see if the port
4877993e3fafSRobert Mustacchi 		 * has been reset.
48787c478bd9Sstevel@tonic-gate 		 */
4879993e3fafSRobert Mustacchi 		delta = cv_reltimedwait(&hubd->h_cv_reset_port,
4880d3d50737SRafael Vanoni 		    &hubd->h_mutex, delta, TR_CLOCK_TICK);
4881993e3fafSRobert Mustacchi 		if (delta < 0)
48827c478bd9Sstevel@tonic-gate 			hubd->h_port_reset_wait &= ~port_mask;
48837c478bd9Sstevel@tonic-gate 
48847c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
48857c478bd9Sstevel@tonic-gate 
48867c478bd9Sstevel@tonic-gate 		data = NULL;
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 		/* check status to determine whether reset completed */
48897c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
48907c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
48917c478bd9Sstevel@tonic-gate 		    hubd->h_default_pipe,
489235f36846Ssl147100 		    HUB_GET_PORT_STATUS_TYPE,
48937c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_STATUS,
48947c478bd9Sstevel@tonic-gate 		    0,
48957c478bd9Sstevel@tonic-gate 		    port,
48967c478bd9Sstevel@tonic-gate 		    GET_STATUS_LENGTH,
48977c478bd9Sstevel@tonic-gate 		    &data, 0,
48987c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
4899d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_PORT,
49007c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
49017c478bd9Sstevel@tonic-gate 			    "get status port%d failed (%d 0x%x %d)",
49027c478bd9Sstevel@tonic-gate 			    port, completion_reason, cb_flags, rval);
49037c478bd9Sstevel@tonic-gate 
49047c478bd9Sstevel@tonic-gate 			if (data) {
49057c478bd9Sstevel@tonic-gate 				freemsg(data);
49067c478bd9Sstevel@tonic-gate 				data = NULL;
49077c478bd9Sstevel@tonic-gate 			}
49087c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
49097c478bd9Sstevel@tonic-gate 
49107c478bd9Sstevel@tonic-gate 			continue;
49117c478bd9Sstevel@tonic-gate 		}
49127c478bd9Sstevel@tonic-gate 
49137c478bd9Sstevel@tonic-gate 		status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
49147c478bd9Sstevel@tonic-gate 		change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
49157c478bd9Sstevel@tonic-gate 
49167c478bd9Sstevel@tonic-gate 		freemsg(data);
49177c478bd9Sstevel@tonic-gate 
4918993e3fafSRobert Mustacchi 		hubd_status_uniform(hubd, port, &status, NULL);
4919993e3fafSRobert Mustacchi 
49207c478bd9Sstevel@tonic-gate 		/* continue only if port is still connected */
49217c478bd9Sstevel@tonic-gate 		if (!(status & PORT_STATUS_CCS)) {
49227c478bd9Sstevel@tonic-gate 
49237c478bd9Sstevel@tonic-gate 			/* lost connection, set exit condition */
4924993e3fafSRobert Mustacchi 			delta = -1;
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
49277c478bd9Sstevel@tonic-gate 
49287c478bd9Sstevel@tonic-gate 			break;
49297c478bd9Sstevel@tonic-gate 		}
49307c478bd9Sstevel@tonic-gate 
49317c478bd9Sstevel@tonic-gate 		if (status & PORT_STATUS_PRS) {
49327c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49337c478bd9Sstevel@tonic-gate 			    "port%d reset active", port);
49347c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate 			continue;
49377c478bd9Sstevel@tonic-gate 		} else {
49387c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49397c478bd9Sstevel@tonic-gate 			    "port%d reset inactive", port);
49407c478bd9Sstevel@tonic-gate 		}
49417c478bd9Sstevel@tonic-gate 
49427c478bd9Sstevel@tonic-gate 		if (change & PORT_CHANGE_PRSC) {
49437c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
49447c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_RESET");
49457c478bd9Sstevel@tonic-gate 
49467c478bd9Sstevel@tonic-gate 			if (usb_pipe_sync_ctrl_xfer(hubd->h_dip,
49477c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
494835f36846Ssl147100 			    HUB_HANDLE_PORT_FEATURE_TYPE,
49497c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
49507c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_RESET,
49517c478bd9Sstevel@tonic-gate 			    port,
49527c478bd9Sstevel@tonic-gate 			    0,
49537c478bd9Sstevel@tonic-gate 			    NULL, 0,
49547c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
49557c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
49567c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
49577c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_RESET"
49587c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
49597c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
49607c478bd9Sstevel@tonic-gate 			}
49617c478bd9Sstevel@tonic-gate 		}
4962993e3fafSRobert Mustacchi 
4963993e3fafSRobert Mustacchi 		/*
4964993e3fafSRobert Mustacchi 		 * In addition to a normal reset, a warm reset may have
4965993e3fafSRobert Mustacchi 		 * happened. Acknowledge that as well.
4966993e3fafSRobert Mustacchi 		 */
4967993e3fafSRobert Mustacchi 		if (change & PORT_CHANGE_BHPR) {
4968993e3fafSRobert Mustacchi 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
4969993e3fafSRobert Mustacchi 			    "clearing feature CFS_C_BH_PORT_RESET");
4970993e3fafSRobert Mustacchi 
4971993e3fafSRobert Mustacchi 			if (usb_pipe_sync_ctrl_xfer(hubd->h_dip,
4972993e3fafSRobert Mustacchi 			    hubd->h_default_pipe,
4973993e3fafSRobert Mustacchi 			    HUB_HANDLE_PORT_FEATURE_TYPE,
4974993e3fafSRobert Mustacchi 			    USB_REQ_CLEAR_FEATURE,
4975993e3fafSRobert Mustacchi 			    CFS_C_BH_PORT_RESET,
4976993e3fafSRobert Mustacchi 			    port,
4977993e3fafSRobert Mustacchi 			    0,
4978993e3fafSRobert Mustacchi 			    NULL, 0,
4979993e3fafSRobert Mustacchi 			    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
4980993e3fafSRobert Mustacchi 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
4981993e3fafSRobert Mustacchi 				    hubd->h_log_handle,
4982993e3fafSRobert Mustacchi 				    "clear feature CFS_C_BH_PORT_RESET"
4983993e3fafSRobert Mustacchi 				    " port%d failed (%d 0x%x %d)",
4984993e3fafSRobert Mustacchi 				    port, completion_reason, cb_flags, rval);
4985993e3fafSRobert Mustacchi 			}
4986993e3fafSRobert Mustacchi 		}
4987993e3fafSRobert Mustacchi 
4988993e3fafSRobert Mustacchi 		rval = USB_SUCCESS;
49897c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
49907c478bd9Sstevel@tonic-gate 
49917c478bd9Sstevel@tonic-gate 		break;
49927c478bd9Sstevel@tonic-gate 	}
49937c478bd9Sstevel@tonic-gate 
49947c478bd9Sstevel@tonic-gate 	return (rval);
49957c478bd9Sstevel@tonic-gate }
49967c478bd9Sstevel@tonic-gate 
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate /*
49997c478bd9Sstevel@tonic-gate  * hubd_enable_port:
50007c478bd9Sstevel@tonic-gate  *	this may fail if the hub as been disconnected
50017c478bd9Sstevel@tonic-gate  */
50027c478bd9Sstevel@tonic-gate static int
hubd_enable_port(hubd_t * hubd,usb_port_t port)50037c478bd9Sstevel@tonic-gate hubd_enable_port(hubd_t *hubd, usb_port_t port)
50047c478bd9Sstevel@tonic-gate {
50057c478bd9Sstevel@tonic-gate 	int	rval;
50067c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
50077c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
50087c478bd9Sstevel@tonic-gate 
50097c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
50107c478bd9Sstevel@tonic-gate 	    "hubd_enable_port: port=%d", port);
50117c478bd9Sstevel@tonic-gate 
50127c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
50157c478bd9Sstevel@tonic-gate 
50167c478bd9Sstevel@tonic-gate 	/* Do not issue a SetFeature(PORT_ENABLE) on external hubs */
50177c478bd9Sstevel@tonic-gate 	if (!usba_is_root_hub(hubd->h_dip)) {
50187c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
50217c478bd9Sstevel@tonic-gate 	}
50227c478bd9Sstevel@tonic-gate 
50237c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
50247c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
502535f36846Ssl147100 	    HUB_HANDLE_PORT_FEATURE_TYPE,
50267c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
50277c478bd9Sstevel@tonic-gate 	    CFS_PORT_ENABLE,
50287c478bd9Sstevel@tonic-gate 	    port,
50297c478bd9Sstevel@tonic-gate 	    0,
50307c478bd9Sstevel@tonic-gate 	    NULL, 0,
50317c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
50327c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
50337c478bd9Sstevel@tonic-gate 		    "enable port%d failed (%d 0x%x %d)",
50347c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
50357c478bd9Sstevel@tonic-gate 	}
50367c478bd9Sstevel@tonic-gate 
50377c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
50407c478bd9Sstevel@tonic-gate 	    "enabling port done");
50417c478bd9Sstevel@tonic-gate 
50427c478bd9Sstevel@tonic-gate 	return (rval);
50437c478bd9Sstevel@tonic-gate }
50447c478bd9Sstevel@tonic-gate 
50457c478bd9Sstevel@tonic-gate 
50467c478bd9Sstevel@tonic-gate /*
50477c478bd9Sstevel@tonic-gate  * hubd_disable_port
50487c478bd9Sstevel@tonic-gate  */
50497c478bd9Sstevel@tonic-gate static int
hubd_disable_port(hubd_t * hubd,usb_port_t port)50507c478bd9Sstevel@tonic-gate hubd_disable_port(hubd_t *hubd, usb_port_t port)
50517c478bd9Sstevel@tonic-gate {
50527c478bd9Sstevel@tonic-gate 	int	rval;
50537c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
50547c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
50557c478bd9Sstevel@tonic-gate 
50567c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
50577c478bd9Sstevel@tonic-gate 	    "hubd_disable_port: port=%d", port);
50587c478bd9Sstevel@tonic-gate 
50597c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
50607c478bd9Sstevel@tonic-gate 
50617c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
50627c478bd9Sstevel@tonic-gate 
50637c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
50647c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
506535f36846Ssl147100 	    HUB_HANDLE_PORT_FEATURE_TYPE,
50667c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
50677c478bd9Sstevel@tonic-gate 	    CFS_PORT_ENABLE,
50687c478bd9Sstevel@tonic-gate 	    port,
50697c478bd9Sstevel@tonic-gate 	    0,
50707c478bd9Sstevel@tonic-gate 	    NULL, 0,
50717c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
50727c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
50737c478bd9Sstevel@tonic-gate 		    "disable port%d failed (%d 0x%x %d)", port,
50747c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
50757c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
50767c478bd9Sstevel@tonic-gate 
50777c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
50787c478bd9Sstevel@tonic-gate 	}
50797c478bd9Sstevel@tonic-gate 
50807c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
50817c478bd9Sstevel@tonic-gate 	    "clearing feature CFS_C_PORT_ENABLE");
50827c478bd9Sstevel@tonic-gate 
50837c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
50847c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
508535f36846Ssl147100 	    HUB_HANDLE_PORT_FEATURE_TYPE,
50867c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
50877c478bd9Sstevel@tonic-gate 	    CFS_C_PORT_ENABLE,
50887c478bd9Sstevel@tonic-gate 	    port,
50897c478bd9Sstevel@tonic-gate 	    0,
50907c478bd9Sstevel@tonic-gate 	    NULL, 0,
50917c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
50927c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT,
50937c478bd9Sstevel@tonic-gate 		    hubd->h_log_handle,
50947c478bd9Sstevel@tonic-gate 		    "clear feature CFS_C_PORT_ENABLE port%d failed "
50957c478bd9Sstevel@tonic-gate 		    "(%d 0x%x %d)",
50967c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
50977c478bd9Sstevel@tonic-gate 
50987c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
50997c478bd9Sstevel@tonic-gate 
51007c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
51017c478bd9Sstevel@tonic-gate 	}
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
51067c478bd9Sstevel@tonic-gate }
51077c478bd9Sstevel@tonic-gate 
51087c478bd9Sstevel@tonic-gate 
51097c478bd9Sstevel@tonic-gate /*
51107c478bd9Sstevel@tonic-gate  * hubd_determine_port_status:
51117c478bd9Sstevel@tonic-gate  */
51127c478bd9Sstevel@tonic-gate static int
hubd_determine_port_status(hubd_t * hubd,usb_port_t port,uint16_t * status,uint16_t * change,usb_port_status_t * speed,uint_t ack_flag)5113993e3fafSRobert Mustacchi hubd_determine_port_status(hubd_t *hubd, usb_port_t port, uint16_t *status,
5114993e3fafSRobert Mustacchi     uint16_t *change, usb_port_status_t *speed, uint_t ack_flag)
51157c478bd9Sstevel@tonic-gate {
51167c478bd9Sstevel@tonic-gate 	int rval;
51177c478bd9Sstevel@tonic-gate 	mblk_t	*data = NULL;
51187c478bd9Sstevel@tonic-gate 	usb_cr_t completion_reason;
51197c478bd9Sstevel@tonic-gate 	usb_cb_flags_t cb_flags;
5120993e3fafSRobert Mustacchi 	uint16_t st, ch;
5121993e3fafSRobert Mustacchi 	usb_port_status_t sp;
5122993e3fafSRobert Mustacchi 
5123993e3fafSRobert Mustacchi 	if (status == NULL)
5124993e3fafSRobert Mustacchi 		status = &st;
5125993e3fafSRobert Mustacchi 	if (change == NULL)
5126993e3fafSRobert Mustacchi 		change = &ch;
5127993e3fafSRobert Mustacchi 	if (speed == NULL)
5128993e3fafSRobert Mustacchi 		speed = &sp;
5129993e3fafSRobert Mustacchi 
5130993e3fafSRobert Mustacchi 	*status = *change = 0;
5131993e3fafSRobert Mustacchi 	*speed = 0;
51327c478bd9Sstevel@tonic-gate 
51337c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
51347c478bd9Sstevel@tonic-gate 	    "hubd_determine_port_status: port=%d, state=0x%x ack=0x%x", port,
51357c478bd9Sstevel@tonic-gate 	    hubd->h_port_state[port], ack_flag);
51367c478bd9Sstevel@tonic-gate 
51377c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
51407c478bd9Sstevel@tonic-gate 
51417c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
51427c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
514335f36846Ssl147100 	    HUB_GET_PORT_STATUS_TYPE,
51447c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_STATUS,
51457c478bd9Sstevel@tonic-gate 	    0,
51467c478bd9Sstevel@tonic-gate 	    port,
51477c478bd9Sstevel@tonic-gate 	    GET_STATUS_LENGTH,
51487c478bd9Sstevel@tonic-gate 	    &data, 0,
51497c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
51507c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
51517c478bd9Sstevel@tonic-gate 		    "port=%d get status failed (%d 0x%x %d)",
51527c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate 		if (data) {
51557c478bd9Sstevel@tonic-gate 			freemsg(data);
51567c478bd9Sstevel@tonic-gate 		}
51577c478bd9Sstevel@tonic-gate 
51587c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
51597c478bd9Sstevel@tonic-gate 
51607c478bd9Sstevel@tonic-gate 		return (rval);
51617c478bd9Sstevel@tonic-gate 	}
51627c478bd9Sstevel@tonic-gate 
51637c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
5164d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	if (MBLKL(data) != GET_STATUS_LENGTH) {
51657c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
5166112116d8Sfb209375 		    "port %d: length incorrect %ld",
5167d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		    port, MBLKL(data));
51687c478bd9Sstevel@tonic-gate 		freemsg(data);
51697c478bd9Sstevel@tonic-gate 
51707c478bd9Sstevel@tonic-gate 		return (rval);
51717c478bd9Sstevel@tonic-gate 	}
51727c478bd9Sstevel@tonic-gate 
51737c478bd9Sstevel@tonic-gate 
51747c478bd9Sstevel@tonic-gate 	*status = (*(data->b_rptr + 1) << 8) | *(data->b_rptr);
51757c478bd9Sstevel@tonic-gate 	*change = (*(data->b_rptr + 3) << 8) | *(data->b_rptr + 2);
5176993e3fafSRobert Mustacchi 	hubd_status_uniform(hubd, port, status, speed);
51777c478bd9Sstevel@tonic-gate 
51787c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
51797c478bd9Sstevel@tonic-gate 	    "port%d status=0x%x, change=0x%x", port, *status, *change);
51807c478bd9Sstevel@tonic-gate 
51817c478bd9Sstevel@tonic-gate 	freemsg(data);
51827c478bd9Sstevel@tonic-gate 
51837c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_CCS) {
51847c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
51857c478bd9Sstevel@tonic-gate 		    "port%d connected", port);
51867c478bd9Sstevel@tonic-gate 
51877c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_CCS & ack_flag);
51887c478bd9Sstevel@tonic-gate 	} else {
51897c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
51907c478bd9Sstevel@tonic-gate 		    "port%d disconnected", port);
51917c478bd9Sstevel@tonic-gate 
51927c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_CCS & ack_flag);
51937c478bd9Sstevel@tonic-gate 	}
51947c478bd9Sstevel@tonic-gate 
51957c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PES) {
51967c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
51977c478bd9Sstevel@tonic-gate 		    "port%d enabled", port);
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PES & ack_flag);
52007c478bd9Sstevel@tonic-gate 	} else {
52017c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52027c478bd9Sstevel@tonic-gate 		    "port%d disabled", port);
52037c478bd9Sstevel@tonic-gate 
52047c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PES & ack_flag);
52057c478bd9Sstevel@tonic-gate 	}
52067c478bd9Sstevel@tonic-gate 
52077c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PSS) {
52087c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52097c478bd9Sstevel@tonic-gate 		    "port%d suspended", port);
52107c478bd9Sstevel@tonic-gate 
52117c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PSS & ack_flag);
52127c478bd9Sstevel@tonic-gate 	} else {
52137c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52147c478bd9Sstevel@tonic-gate 		    "port%d not suspended", port);
52157c478bd9Sstevel@tonic-gate 
52167c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PSS & ack_flag);
52177c478bd9Sstevel@tonic-gate 	}
52187c478bd9Sstevel@tonic-gate 
52197c478bd9Sstevel@tonic-gate 	if (*change & PORT_CHANGE_PRSC) {
52207c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52217c478bd9Sstevel@tonic-gate 		    "port%d reset completed", port);
52227c478bd9Sstevel@tonic-gate 
52237c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_CHANGE_PRSC & ack_flag);
52247c478bd9Sstevel@tonic-gate 	} else {
52257c478bd9Sstevel@tonic-gate 
52267c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_CHANGE_PRSC & ack_flag);
52277c478bd9Sstevel@tonic-gate 	}
52287c478bd9Sstevel@tonic-gate 
52297c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_POCI) {
5230d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
52317c478bd9Sstevel@tonic-gate 		    "port%d overcurrent!", port);
52327c478bd9Sstevel@tonic-gate 
52337c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_POCI & ack_flag);
52347c478bd9Sstevel@tonic-gate 	} else {
52357c478bd9Sstevel@tonic-gate 
52367c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_POCI & ack_flag);
52377c478bd9Sstevel@tonic-gate 	}
52387c478bd9Sstevel@tonic-gate 
52397c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PRS) {
52407c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52417c478bd9Sstevel@tonic-gate 		    "port%d reset active", port);
52427c478bd9Sstevel@tonic-gate 
52437c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PRS & ack_flag);
52447c478bd9Sstevel@tonic-gate 	} else {
52457c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52467c478bd9Sstevel@tonic-gate 		    "port%d reset inactive", port);
52477c478bd9Sstevel@tonic-gate 
52487c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PRS & ack_flag);
52497c478bd9Sstevel@tonic-gate 	}
52507c478bd9Sstevel@tonic-gate 	if (*status & PORT_STATUS_PPS) {
52517c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52527c478bd9Sstevel@tonic-gate 		    "port%d power on", port);
52537c478bd9Sstevel@tonic-gate 
52547c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= (PORT_STATUS_PPS & ack_flag);
52557c478bd9Sstevel@tonic-gate 	} else {
52567c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52577c478bd9Sstevel@tonic-gate 		    "port%d power off", port);
52587c478bd9Sstevel@tonic-gate 
52597c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~(PORT_STATUS_PPS & ack_flag);
52607c478bd9Sstevel@tonic-gate 	}
52617c478bd9Sstevel@tonic-gate 
52627c478bd9Sstevel@tonic-gate 	/*
52637c478bd9Sstevel@tonic-gate 	 * Acknowledge connection, enable, reset status
52647c478bd9Sstevel@tonic-gate 	 */
52657c478bd9Sstevel@tonic-gate 	if (ack_flag) {
52667c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
52677c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_CSC & ack_flag) {
52687c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52697c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_CONNECTION");
52707c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
52717c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
527235f36846Ssl147100 			    HUB_HANDLE_PORT_FEATURE_TYPE,
52737c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
52747c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_CONNECTION,
52757c478bd9Sstevel@tonic-gate 			    port,
52767c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
52777c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
52787c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
52797c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
52807c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
52817c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_CONNECTION"
52827c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
52837c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
52847c478bd9Sstevel@tonic-gate 			}
52857c478bd9Sstevel@tonic-gate 		}
52867c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PESC & ack_flag) {
52877c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
52887c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_ENABLE");
52897c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
52907c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
529135f36846Ssl147100 			    HUB_HANDLE_PORT_FEATURE_TYPE,
52927c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
52937c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_ENABLE,
52947c478bd9Sstevel@tonic-gate 			    port,
52957c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
52967c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
52977c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
52987c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
52997c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
53007c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_ENABLE"
53017c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
53027c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
53037c478bd9Sstevel@tonic-gate 			}
53047c478bd9Sstevel@tonic-gate 		}
53057c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PSSC & ack_flag) {
53067c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
53077c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_SUSPEND");
53087c478bd9Sstevel@tonic-gate 
53097c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
53107c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
531135f36846Ssl147100 			    HUB_HANDLE_PORT_FEATURE_TYPE,
53127c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
53137c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_SUSPEND,
53147c478bd9Sstevel@tonic-gate 			    port,
53157c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
53167c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
53177c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
53187c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
53197c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
53207c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_SUSPEND"
53217c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
53227c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
53237c478bd9Sstevel@tonic-gate 			}
53247c478bd9Sstevel@tonic-gate 		}
53257c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_OCIC & ack_flag) {
53267c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
53277c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_OVER_CURRENT");
53287c478bd9Sstevel@tonic-gate 
53297c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
53307c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
533135f36846Ssl147100 			    HUB_HANDLE_PORT_FEATURE_TYPE,
53327c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
53337c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_OVER_CURRENT,
53347c478bd9Sstevel@tonic-gate 			    port,
53357c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
53367c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
53377c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
53387c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
53397c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
53407c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_OVER_CURRENT"
53417c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
53427c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
53437c478bd9Sstevel@tonic-gate 			}
53447c478bd9Sstevel@tonic-gate 		}
53457c478bd9Sstevel@tonic-gate 		if (*change & PORT_CHANGE_PRSC & ack_flag) {
53467c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
53477c478bd9Sstevel@tonic-gate 			    "clearing feature CFS_C_PORT_RESET");
53487c478bd9Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
53497c478bd9Sstevel@tonic-gate 			    hubd->h_default_pipe,
535035f36846Ssl147100 			    HUB_HANDLE_PORT_FEATURE_TYPE,
53517c478bd9Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
53527c478bd9Sstevel@tonic-gate 			    CFS_C_PORT_RESET,
53537c478bd9Sstevel@tonic-gate 			    port,
53547c478bd9Sstevel@tonic-gate 			    0, NULL, 0,
53557c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0)) !=
53567c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
53577c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
53587c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
53597c478bd9Sstevel@tonic-gate 				    "clear feature CFS_C_PORT_RESET"
53607c478bd9Sstevel@tonic-gate 				    " port%d failed (%d 0x%x %d)",
53617c478bd9Sstevel@tonic-gate 				    port, completion_reason, cb_flags, rval);
53627c478bd9Sstevel@tonic-gate 			}
53637c478bd9Sstevel@tonic-gate 		}
5364993e3fafSRobert Mustacchi 		if (*change & PORT_CHANGE_BHPR & ack_flag) {
5365993e3fafSRobert Mustacchi 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5366993e3fafSRobert Mustacchi 			    "clearing feature CFS_C_BH_PORT_RESET");
5367993e3fafSRobert Mustacchi 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5368993e3fafSRobert Mustacchi 			    hubd->h_default_pipe,
5369993e3fafSRobert Mustacchi 			    HUB_HANDLE_PORT_FEATURE_TYPE,
5370993e3fafSRobert Mustacchi 			    USB_REQ_CLEAR_FEATURE,
5371993e3fafSRobert Mustacchi 			    CFS_C_BH_PORT_RESET,
5372993e3fafSRobert Mustacchi 			    port,
5373993e3fafSRobert Mustacchi 			    0, NULL, 0,
5374993e3fafSRobert Mustacchi 			    &completion_reason, &cb_flags, 0)) !=
5375993e3fafSRobert Mustacchi 			    USB_SUCCESS) {
5376993e3fafSRobert Mustacchi 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
5377993e3fafSRobert Mustacchi 				    hubd->h_log_handle,
5378993e3fafSRobert Mustacchi 				    "clear feature CFS_C_BH_PORT_RESET"
5379993e3fafSRobert Mustacchi 				    " port%d failed (%d 0x%x %d)",
5380993e3fafSRobert Mustacchi 				    port, completion_reason, cb_flags, rval);
5381993e3fafSRobert Mustacchi 			}
5382993e3fafSRobert Mustacchi 		}
5383993e3fafSRobert Mustacchi 		if (*change & PORT_CHANGE_PLSC & ack_flag) {
5384993e3fafSRobert Mustacchi 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5385993e3fafSRobert Mustacchi 			    "clearing feature CFS_C_PORT_LINK_STATE");
5386993e3fafSRobert Mustacchi 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5387993e3fafSRobert Mustacchi 			    hubd->h_default_pipe,
5388993e3fafSRobert Mustacchi 			    HUB_HANDLE_PORT_FEATURE_TYPE,
5389993e3fafSRobert Mustacchi 			    USB_REQ_CLEAR_FEATURE,
5390993e3fafSRobert Mustacchi 			    CFS_C_PORT_LINK_STATE,
5391993e3fafSRobert Mustacchi 			    port,
5392993e3fafSRobert Mustacchi 			    0, NULL, 0,
5393993e3fafSRobert Mustacchi 			    &completion_reason, &cb_flags, 0)) !=
5394993e3fafSRobert Mustacchi 			    USB_SUCCESS) {
5395993e3fafSRobert Mustacchi 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
5396993e3fafSRobert Mustacchi 				    hubd->h_log_handle,
5397993e3fafSRobert Mustacchi 				    "clear feature CFS_C_PORT_LINK_STATE"
5398993e3fafSRobert Mustacchi 				    " port%d failed (%d 0x%x %d)",
5399993e3fafSRobert Mustacchi 				    port, completion_reason, cb_flags, rval);
5400993e3fafSRobert Mustacchi 			}
5401993e3fafSRobert Mustacchi 		}
5402993e3fafSRobert Mustacchi 		if (*change & PORT_CHANGE_PCE & ack_flag) {
5403993e3fafSRobert Mustacchi 			USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
5404993e3fafSRobert Mustacchi 			    "clearing feature CFS_C_PORT_CONFIG_ERROR");
5405993e3fafSRobert Mustacchi 			if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
5406993e3fafSRobert Mustacchi 			    hubd->h_default_pipe,
5407993e3fafSRobert Mustacchi 			    HUB_HANDLE_PORT_FEATURE_TYPE,
5408993e3fafSRobert Mustacchi 			    USB_REQ_CLEAR_FEATURE,
5409993e3fafSRobert Mustacchi 			    CFS_C_PORT_CONFIG_ERROR,
5410993e3fafSRobert Mustacchi 			    port,
5411993e3fafSRobert Mustacchi 			    0, NULL, 0,
5412993e3fafSRobert Mustacchi 			    &completion_reason, &cb_flags, 0)) !=
5413993e3fafSRobert Mustacchi 			    USB_SUCCESS) {
5414993e3fafSRobert Mustacchi 				USB_DPRINTF_L2(DPRINT_MASK_PORT,
5415993e3fafSRobert Mustacchi 				    hubd->h_log_handle,
5416993e3fafSRobert Mustacchi 				    "clear feature CFS_C_PORT_CONFIG_ERROR"
5417993e3fafSRobert Mustacchi 				    " port%d failed (%d 0x%x %d)",
5418993e3fafSRobert Mustacchi 				    port, completion_reason, cb_flags, rval);
5419993e3fafSRobert Mustacchi 			}
5420993e3fafSRobert Mustacchi 		}
54217c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
54227c478bd9Sstevel@tonic-gate 	}
54237c478bd9Sstevel@tonic-gate 
54247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
54257c478bd9Sstevel@tonic-gate 	    "new port%d state 0x%x", port, hubd->h_port_state[port]);
54267c478bd9Sstevel@tonic-gate 
54277c478bd9Sstevel@tonic-gate 
54287c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
54297c478bd9Sstevel@tonic-gate }
54307c478bd9Sstevel@tonic-gate 
54317c478bd9Sstevel@tonic-gate 
54327c478bd9Sstevel@tonic-gate /*
54337c478bd9Sstevel@tonic-gate  * hubd_recover_disabled_port
54347c478bd9Sstevel@tonic-gate  * if the port got disabled because of an error
54357c478bd9Sstevel@tonic-gate  * enable it. If hub doesn't suport enable port,
54367c478bd9Sstevel@tonic-gate  * reset the port to bring the device to life again
54377c478bd9Sstevel@tonic-gate  */
54387c478bd9Sstevel@tonic-gate static int
hubd_recover_disabled_port(hubd_t * hubd,usb_port_t port)54397c478bd9Sstevel@tonic-gate hubd_recover_disabled_port(hubd_t *hubd, usb_port_t port)
54407c478bd9Sstevel@tonic-gate {
54417c478bd9Sstevel@tonic-gate 	uint16_t	status;
54427c478bd9Sstevel@tonic-gate 	uint16_t	change;
54437c478bd9Sstevel@tonic-gate 	int		rval = USB_FAILURE;
54447c478bd9Sstevel@tonic-gate 
54457c478bd9Sstevel@tonic-gate 	/* first try enabling the port */
54467c478bd9Sstevel@tonic-gate 	(void) hubd_enable_port(hubd, port);
54477c478bd9Sstevel@tonic-gate 
54487c478bd9Sstevel@tonic-gate 	/* read the port status */
5449993e3fafSRobert Mustacchi 	(void) hubd_determine_port_status(hubd, port, &status, &change, NULL,
54507c478bd9Sstevel@tonic-gate 	    PORT_CHANGE_PESC);
54517c478bd9Sstevel@tonic-gate 
54527c478bd9Sstevel@tonic-gate 	if (status & PORT_STATUS_PES) {
54537c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
54547c478bd9Sstevel@tonic-gate 		    "Port%d now Enabled", port);
54557c478bd9Sstevel@tonic-gate 	} else if (status & PORT_STATUS_CCS) {
54567c478bd9Sstevel@tonic-gate 		/* first post a disconnect event to the child */
54577c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
54587c478bd9Sstevel@tonic-gate 		hubd_post_event(hubd, port, USBA_EVENT_TAG_HOT_REMOVAL);
54597c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
54607c478bd9Sstevel@tonic-gate 
54617c478bd9Sstevel@tonic-gate 		/* then reset the port and recover the device */
54627c478bd9Sstevel@tonic-gate 		rval = hubd_handle_port_connect(hubd, port);
54637c478bd9Sstevel@tonic-gate 
54647c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
54657c478bd9Sstevel@tonic-gate 		    "Port%d now Enabled by force", port);
54667c478bd9Sstevel@tonic-gate 	}
54677c478bd9Sstevel@tonic-gate 
54687c478bd9Sstevel@tonic-gate 	return (rval);
54697c478bd9Sstevel@tonic-gate }
54707c478bd9Sstevel@tonic-gate 
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate /*
54737c478bd9Sstevel@tonic-gate  * hubd_enable_all_port_power:
54747c478bd9Sstevel@tonic-gate  */
54757c478bd9Sstevel@tonic-gate static int
hubd_enable_all_port_power(hubd_t * hubd)54767c478bd9Sstevel@tonic-gate hubd_enable_all_port_power(hubd_t *hubd)
54777c478bd9Sstevel@tonic-gate {
54787c478bd9Sstevel@tonic-gate 	int		wait;
54797c478bd9Sstevel@tonic-gate 	usb_port_t	port;
54807c478bd9Sstevel@tonic-gate 	uint_t		retry;
54817c478bd9Sstevel@tonic-gate 	uint16_t	status;
54827c478bd9Sstevel@tonic-gate 	uint16_t	change;
54837c478bd9Sstevel@tonic-gate 
54847c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
54857c478bd9Sstevel@tonic-gate 	    "hubd_enable_all_port_power");
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
54887c478bd9Sstevel@tonic-gate 
54897c478bd9Sstevel@tonic-gate 	/*
54907c478bd9Sstevel@tonic-gate 	 * According to section 11.11 of USB, for hubs with no power
54917c478bd9Sstevel@tonic-gate 	 * switches, bPwrOn2PwrGood is zero. But we wait for some
54927c478bd9Sstevel@tonic-gate 	 * arbitrary time to enable power to become stable.
54937c478bd9Sstevel@tonic-gate 	 *
54947c478bd9Sstevel@tonic-gate 	 * If an hub supports port power switching, we need to wait
5495993e3fafSRobert Mustacchi 	 * at least 20ms before accessing corresponding usb port. Note,
5496993e3fafSRobert Mustacchi 	 * this member is stored in the h_power_good member.
54977c478bd9Sstevel@tonic-gate 	 */
5498993e3fafSRobert Mustacchi 	if ((hubd->h_hub_chars & HUB_CHARS_NO_POWER_SWITCHING) ||
5499993e3fafSRobert Mustacchi 	    (hubd->h_power_good == 0)) {
55007c478bd9Sstevel@tonic-gate 		wait = hubd_device_delay / 10;
55017c478bd9Sstevel@tonic-gate 	} else {
55027c478bd9Sstevel@tonic-gate 		wait = max(HUB_DEFAULT_POPG,
5503993e3fafSRobert Mustacchi 		    hubd->h_power_good) * 2 * 1000;
55047c478bd9Sstevel@tonic-gate 	}
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
55077c478bd9Sstevel@tonic-gate 	    "hubd_enable_all_port_power: popg=%d wait=%d",
5508993e3fafSRobert Mustacchi 	    hubd->h_power_good, wait);
55097c478bd9Sstevel@tonic-gate 
55107c478bd9Sstevel@tonic-gate 	/*
55117c478bd9Sstevel@tonic-gate 	 * Enable power per port. we ignore gang power and power mask
55127c478bd9Sstevel@tonic-gate 	 * and always enable all ports one by one.
55137c478bd9Sstevel@tonic-gate 	 */
5514993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
55157c478bd9Sstevel@tonic-gate 		/*
55167c478bd9Sstevel@tonic-gate 		 * Transition the port from the Powered Off to the
55177c478bd9Sstevel@tonic-gate 		 * Disconnected state by supplying power to the port.
55187c478bd9Sstevel@tonic-gate 		 */
55197c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_PORT,
55207c478bd9Sstevel@tonic-gate 		    hubd->h_log_handle,
55217c478bd9Sstevel@tonic-gate 		    "hubd_enable_all_port_power: power port=%d", port);
55227c478bd9Sstevel@tonic-gate 
55237c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port_power(hubd, port);
55247c478bd9Sstevel@tonic-gate 	}
55257c478bd9Sstevel@tonic-gate 
55267c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
55277c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(wait));
55287c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate 	/* For retry if any, use some extra delay */
55317c478bd9Sstevel@tonic-gate 	wait = max(wait, hubd_device_delay / 10);
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate 	/* Check each port power status for a given usb hub */
5534993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 		/* Get port status */
55377c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd, port,
5538993e3fafSRobert Mustacchi 		    &status, &change, NULL, 0);
55397c478bd9Sstevel@tonic-gate 
55407c478bd9Sstevel@tonic-gate 		for (retry = 0; ((!(status & PORT_STATUS_PPS)) &&
55417c478bd9Sstevel@tonic-gate 		    (retry < HUBD_PORT_RETRY)); retry++) {
55427c478bd9Sstevel@tonic-gate 
55437c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
55447c478bd9Sstevel@tonic-gate 			    "Retry is in progress %d: port %d status %d",
55457c478bd9Sstevel@tonic-gate 			    retry, port, status);
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 			(void) hubd_enable_port_power(hubd, port);
55487c478bd9Sstevel@tonic-gate 
55497c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
55507c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(wait));
55517c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
55527c478bd9Sstevel@tonic-gate 
55537c478bd9Sstevel@tonic-gate 			/* Get port status */
55547c478bd9Sstevel@tonic-gate 			(void) hubd_determine_port_status(hubd, port,
5555993e3fafSRobert Mustacchi 			    &status, &change, NULL, 0);
55567c478bd9Sstevel@tonic-gate 		}
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 		/* Print warning message if port has no power */
55597c478bd9Sstevel@tonic-gate 		if (!(status & PORT_STATUS_PPS)) {
55607c478bd9Sstevel@tonic-gate 
5561d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
55627c478bd9Sstevel@tonic-gate 			    "hubd_enable_all_port_power: port %d power-on "
55637c478bd9Sstevel@tonic-gate 			    "failed, port status 0x%x", port, status);
55647c478bd9Sstevel@tonic-gate 		}
55657c478bd9Sstevel@tonic-gate 	}
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
55687c478bd9Sstevel@tonic-gate }
55697c478bd9Sstevel@tonic-gate 
55707c478bd9Sstevel@tonic-gate 
55717c478bd9Sstevel@tonic-gate /*
55727c478bd9Sstevel@tonic-gate  * hubd_enable_port_power:
55737c478bd9Sstevel@tonic-gate  *	enable individual port power
55747c478bd9Sstevel@tonic-gate  */
55757c478bd9Sstevel@tonic-gate static int
hubd_enable_port_power(hubd_t * hubd,usb_port_t port)55767c478bd9Sstevel@tonic-gate hubd_enable_port_power(hubd_t *hubd, usb_port_t port)
55777c478bd9Sstevel@tonic-gate {
55787c478bd9Sstevel@tonic-gate 	int		rval;
55797c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
55807c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
55817c478bd9Sstevel@tonic-gate 
55827c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
55837c478bd9Sstevel@tonic-gate 	    "hubd_enable_port_power: port=%d", port);
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
55867c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_default_pipe != 0);
55877c478bd9Sstevel@tonic-gate 
55887c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
55897c478bd9Sstevel@tonic-gate 
55907c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
55917c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
559235f36846Ssl147100 	    HUB_HANDLE_PORT_FEATURE_TYPE,
55937c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_FEATURE,
55947c478bd9Sstevel@tonic-gate 	    CFS_PORT_POWER,
55957c478bd9Sstevel@tonic-gate 	    port,
55967c478bd9Sstevel@tonic-gate 	    0, NULL, 0,
55977c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
55987c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
55997c478bd9Sstevel@tonic-gate 		    "set port power failed (%d 0x%x %d)",
56007c478bd9Sstevel@tonic-gate 		    completion_reason, cb_flags, rval);
56017c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
56027c478bd9Sstevel@tonic-gate 
56037c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
56047c478bd9Sstevel@tonic-gate 	} else {
56057c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
56067c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] |= PORT_STATUS_PPS;
56077c478bd9Sstevel@tonic-gate 
56087c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
56097c478bd9Sstevel@tonic-gate 	}
56107c478bd9Sstevel@tonic-gate }
56117c478bd9Sstevel@tonic-gate 
56127c478bd9Sstevel@tonic-gate 
56137c478bd9Sstevel@tonic-gate /*
56147c478bd9Sstevel@tonic-gate  * hubd_disable_all_port_power:
56157c478bd9Sstevel@tonic-gate  */
56167c478bd9Sstevel@tonic-gate static int
hubd_disable_all_port_power(hubd_t * hubd)56177c478bd9Sstevel@tonic-gate hubd_disable_all_port_power(hubd_t *hubd)
56187c478bd9Sstevel@tonic-gate {
56197c478bd9Sstevel@tonic-gate 	usb_port_t port;
56207c478bd9Sstevel@tonic-gate 
56217c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
56227c478bd9Sstevel@tonic-gate 	    "hubd_disable_all_port_power");
56237c478bd9Sstevel@tonic-gate 
56247c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
56257c478bd9Sstevel@tonic-gate 
56267c478bd9Sstevel@tonic-gate 	/*
56277c478bd9Sstevel@tonic-gate 	 * disable power per port, ignore gang power and power mask
56287c478bd9Sstevel@tonic-gate 	 */
5629993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
56307c478bd9Sstevel@tonic-gate 		(void) hubd_disable_port_power(hubd, port);
56317c478bd9Sstevel@tonic-gate 	}
56327c478bd9Sstevel@tonic-gate 
56337c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
56347c478bd9Sstevel@tonic-gate }
56357c478bd9Sstevel@tonic-gate 
56367c478bd9Sstevel@tonic-gate 
56377c478bd9Sstevel@tonic-gate /*
56387c478bd9Sstevel@tonic-gate  * hubd_disable_port_power:
56397c478bd9Sstevel@tonic-gate  *	disable individual port power
56407c478bd9Sstevel@tonic-gate  */
56417c478bd9Sstevel@tonic-gate static int
hubd_disable_port_power(hubd_t * hubd,usb_port_t port)56427c478bd9Sstevel@tonic-gate hubd_disable_port_power(hubd_t *hubd, usb_port_t port)
56437c478bd9Sstevel@tonic-gate {
56447c478bd9Sstevel@tonic-gate 	int		rval;
56457c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
56467c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PORT, hubd->h_log_handle,
56497c478bd9Sstevel@tonic-gate 	    "hubd_disable_port_power: port=%d", port);
56507c478bd9Sstevel@tonic-gate 
56517c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
56547c478bd9Sstevel@tonic-gate 
56557c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(hubd->h_dip,
56567c478bd9Sstevel@tonic-gate 	    hubd->h_default_pipe,
565735f36846Ssl147100 	    HUB_HANDLE_PORT_FEATURE_TYPE,
56587c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE,
56597c478bd9Sstevel@tonic-gate 	    CFS_PORT_POWER,
56607c478bd9Sstevel@tonic-gate 	    port,
56617c478bd9Sstevel@tonic-gate 	    0, NULL, 0,
56627c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
56637c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
56647c478bd9Sstevel@tonic-gate 		    "clearing port%d power failed (%d 0x%x %d)",
56657c478bd9Sstevel@tonic-gate 		    port, completion_reason, cb_flags, rval);
56667c478bd9Sstevel@tonic-gate 
56677c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
56687c478bd9Sstevel@tonic-gate 
56697c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
56707c478bd9Sstevel@tonic-gate 	} else {
56717c478bd9Sstevel@tonic-gate 
56727c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
56737c478bd9Sstevel@tonic-gate 		ASSERT(completion_reason == 0);
56747c478bd9Sstevel@tonic-gate 		hubd->h_port_state[port] &= ~PORT_STATUS_PPS;
56757c478bd9Sstevel@tonic-gate 
56767c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
56777c478bd9Sstevel@tonic-gate 	}
56787c478bd9Sstevel@tonic-gate }
56797c478bd9Sstevel@tonic-gate 
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate /*
56827c478bd9Sstevel@tonic-gate  * Search the database of user preferences and find out the preferred
56837c478bd9Sstevel@tonic-gate  * configuration for this new device
56847c478bd9Sstevel@tonic-gate  */
5685ff0e937bSRaymond Chen int
hubd_select_device_configuration(hubd_t * hubd,usb_port_t port,dev_info_t * child_dip,usba_device_t * child_ud)56867c478bd9Sstevel@tonic-gate hubd_select_device_configuration(hubd_t *hubd, usb_port_t port,
56877c478bd9Sstevel@tonic-gate     dev_info_t *child_dip, usba_device_t *child_ud)
56887c478bd9Sstevel@tonic-gate {
56897c478bd9Sstevel@tonic-gate 	char		*pathname = NULL;
56907c478bd9Sstevel@tonic-gate 	char		*tmp_path = NULL;
56917c478bd9Sstevel@tonic-gate 	int		user_conf;
56927c478bd9Sstevel@tonic-gate 	int		pathlen;
56937c478bd9Sstevel@tonic-gate 	usb_dev_descr_t	*usbdev_ptr;
56947c478bd9Sstevel@tonic-gate 	usba_configrec_t *user_pref;
56957c478bd9Sstevel@tonic-gate 
56967c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
56977c478bd9Sstevel@tonic-gate 	usbdev_ptr = child_ud->usb_dev_descr;
56987c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
56997c478bd9Sstevel@tonic-gate 
57007c478bd9Sstevel@tonic-gate 	/* try to get pathname for this device */
57017c478bd9Sstevel@tonic-gate 	tmp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
57027c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(child_dip, tmp_path);
57037c478bd9Sstevel@tonic-gate 
57047c478bd9Sstevel@tonic-gate 	pathlen = strlen(tmp_path) + 32;
57057c478bd9Sstevel@tonic-gate 	pathname = kmem_zalloc(pathlen, KM_SLEEP);
57067c478bd9Sstevel@tonic-gate 
57077c478bd9Sstevel@tonic-gate 	/*
57087c478bd9Sstevel@tonic-gate 	 * We haven't initialized the node and it doesn't have an address
57097c478bd9Sstevel@tonic-gate 	 * yet. Append port number to the physical pathname
57107c478bd9Sstevel@tonic-gate 	 */
57117c478bd9Sstevel@tonic-gate 	(void) sprintf(pathname, "%s@%d", tmp_path, port);
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
57147c478bd9Sstevel@tonic-gate 	    "hubd_select_device_configuration: Device=%s\n\t"
57157c478bd9Sstevel@tonic-gate 	    "Child path=%s",
57167c478bd9Sstevel@tonic-gate 	    usba_get_mfg_prod_sn_str(child_dip, tmp_path, MAXPATHLEN),
57177c478bd9Sstevel@tonic-gate 	    pathname);
57187c478bd9Sstevel@tonic-gate 	kmem_free(tmp_path, MAXPATHLEN);
57197c478bd9Sstevel@tonic-gate 
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 	/* database search for user preferences */
57227c478bd9Sstevel@tonic-gate 	user_pref = usba_devdb_get_user_preferences(usbdev_ptr->idVendor,
57237c478bd9Sstevel@tonic-gate 	    usbdev_ptr->idProduct, child_ud->usb_serialno_str, pathname);
57247c478bd9Sstevel@tonic-gate 
57257c478bd9Sstevel@tonic-gate 	if (user_pref) {
57267c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
57277c478bd9Sstevel@tonic-gate 		    "hubd_select_device_configuration: "
57287c478bd9Sstevel@tonic-gate 		    "usba_devdb_get_user_preferences "
57297c478bd9Sstevel@tonic-gate 		    "return user_conf=%d\npreferred driver=%s path=%s",
57307c478bd9Sstevel@tonic-gate 		    user_pref->cfg_index, user_pref->driver,
57317c478bd9Sstevel@tonic-gate 		    user_pref->pathname);
57327c478bd9Sstevel@tonic-gate 
57337c478bd9Sstevel@tonic-gate 		user_conf = user_pref->cfg_index;
57347c478bd9Sstevel@tonic-gate 
57357c478bd9Sstevel@tonic-gate 		if (user_pref->driver) {
57367c478bd9Sstevel@tonic-gate 			mutex_enter(&child_ud->usb_mutex);
57377c478bd9Sstevel@tonic-gate 			child_ud->usb_preferred_driver = user_pref->driver;
57387c478bd9Sstevel@tonic-gate 			mutex_exit(&child_ud->usb_mutex);
57397c478bd9Sstevel@tonic-gate 		}
57407c478bd9Sstevel@tonic-gate 	} else {
57417c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
57427c478bd9Sstevel@tonic-gate 		    "hubd_select_device_configuration: No match found");
57437c478bd9Sstevel@tonic-gate 
57447c478bd9Sstevel@tonic-gate 		/* select default configuration for this device */
57457c478bd9Sstevel@tonic-gate 		user_conf = USBA_DEV_CONFIG_INDEX_UNDEFINED;
57467c478bd9Sstevel@tonic-gate 	}
57477c478bd9Sstevel@tonic-gate 	kmem_free(pathname, pathlen);
57487c478bd9Sstevel@tonic-gate 
57497c478bd9Sstevel@tonic-gate 	/* if the device has just one configuration, set default value */
57507c478bd9Sstevel@tonic-gate 	if (usbdev_ptr->bNumConfigurations == 1) {
57517c478bd9Sstevel@tonic-gate 		user_conf = USB_DEV_DEFAULT_CONFIG_INDEX;
57527c478bd9Sstevel@tonic-gate 	}
57537c478bd9Sstevel@tonic-gate 
57547c478bd9Sstevel@tonic-gate 	return (user_conf);
57557c478bd9Sstevel@tonic-gate }
57567c478bd9Sstevel@tonic-gate 
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate /*
57597c478bd9Sstevel@tonic-gate  * Retrieves config cloud for this configuration
57607c478bd9Sstevel@tonic-gate  */
57617c478bd9Sstevel@tonic-gate int
hubd_get_this_config_cloud(hubd_t * hubd,dev_info_t * dip,usba_device_t * child_ud,uint16_t conf_index)57627c478bd9Sstevel@tonic-gate hubd_get_this_config_cloud(hubd_t *hubd, dev_info_t *dip,
57637c478bd9Sstevel@tonic-gate     usba_device_t *child_ud, uint16_t conf_index)
57647c478bd9Sstevel@tonic-gate {
57657c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	*confdescr;
57667c478bd9Sstevel@tonic-gate 	mblk_t		*pdata = NULL;
57677c478bd9Sstevel@tonic-gate 	int		rval;
57687c478bd9Sstevel@tonic-gate 	size_t		size;
57697c478bd9Sstevel@tonic-gate 	char		*tmpbuf;
57707c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
57717c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
57727c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	def_ph;
57737c478bd9Sstevel@tonic-gate 
57747c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
57757c478bd9Sstevel@tonic-gate 	    "hubd_get_this_config_cloud: conf_index=%d", conf_index);
57767c478bd9Sstevel@tonic-gate 
57777c478bd9Sstevel@tonic-gate 
57787c478bd9Sstevel@tonic-gate 	/* alloc temporary space for config descriptor */
57797c478bd9Sstevel@tonic-gate 	confdescr = (usb_cfg_descr_t *)kmem_zalloc(USB_CFG_DESCR_SIZE,
57807c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
57817c478bd9Sstevel@tonic-gate 
57827c478bd9Sstevel@tonic-gate 	/* alloc temporary space for string descriptor */
57837c478bd9Sstevel@tonic-gate 	tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
57847c478bd9Sstevel@tonic-gate 
57857c478bd9Sstevel@tonic-gate 	def_ph = usba_get_dflt_pipe_handle(dip);
57867c478bd9Sstevel@tonic-gate 
57877c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
57887c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
57897c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,
57907c478bd9Sstevel@tonic-gate 	    USB_DESCR_TYPE_SETUP_CFG | conf_index,
57917c478bd9Sstevel@tonic-gate 	    0,
57927c478bd9Sstevel@tonic-gate 	    USB_CFG_DESCR_SIZE,
57937c478bd9Sstevel@tonic-gate 	    &pdata,
57947c478bd9Sstevel@tonic-gate 	    0,
57957c478bd9Sstevel@tonic-gate 	    &completion_reason,
57967c478bd9Sstevel@tonic-gate 	    &cb_flags,
57977c478bd9Sstevel@tonic-gate 	    0)) == USB_SUCCESS) {
57987c478bd9Sstevel@tonic-gate 
57997c478bd9Sstevel@tonic-gate 		/* this must be true since we didn't allow data underruns */
5800d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		if (MBLKL(pdata) != USB_CFG_DESCR_SIZE) {
580135f36846Ssl147100 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
580235f36846Ssl147100 			    "device returned incorrect configuration "
580335f36846Ssl147100 			    "descriptor size.");
580435f36846Ssl147100 
580535f36846Ssl147100 			rval = USB_FAILURE;
580635f36846Ssl147100 			goto done;
580735f36846Ssl147100 		}
58087c478bd9Sstevel@tonic-gate 
58097c478bd9Sstevel@tonic-gate 		/*
58107c478bd9Sstevel@tonic-gate 		 * Parse the configuration descriptor
58117c478bd9Sstevel@tonic-gate 		 */
58127c478bd9Sstevel@tonic-gate 		size = usb_parse_cfg_descr(pdata->b_rptr,
5813d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		    MBLKL(pdata), confdescr,
58147c478bd9Sstevel@tonic-gate 		    USB_CFG_DESCR_SIZE);
58157c478bd9Sstevel@tonic-gate 
58167c478bd9Sstevel@tonic-gate 		/* if parse cfg descr error, it should return failure */
58177c478bd9Sstevel@tonic-gate 		if (size == USB_PARSE_ERROR) {
58187c478bd9Sstevel@tonic-gate 
58197c478bd9Sstevel@tonic-gate 			if (pdata->b_rptr[1] != USB_DESCR_TYPE_CFG) {
5820d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
58217c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
58227c478bd9Sstevel@tonic-gate 				    "device returned incorrect "
58237c478bd9Sstevel@tonic-gate 				    "configuration descriptor type.");
58247c478bd9Sstevel@tonic-gate 			}
58257c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
58267c478bd9Sstevel@tonic-gate 			goto done;
58277c478bd9Sstevel@tonic-gate 		}
58287c478bd9Sstevel@tonic-gate 
58297c478bd9Sstevel@tonic-gate 		if (confdescr->wTotalLength < USB_CFG_DESCR_SIZE) {
5830d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
58317c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
58327c478bd9Sstevel@tonic-gate 			    "device returned incorrect "
58337c478bd9Sstevel@tonic-gate 			    "configuration descriptor size.");
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
58367c478bd9Sstevel@tonic-gate 			goto done;
58377c478bd9Sstevel@tonic-gate 		}
58387c478bd9Sstevel@tonic-gate 
58397c478bd9Sstevel@tonic-gate 		freemsg(pdata);
58407c478bd9Sstevel@tonic-gate 		pdata = NULL;
58417c478bd9Sstevel@tonic-gate 
58427c478bd9Sstevel@tonic-gate 		/* Now fetch the complete config cloud */
58437c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(dip, def_ph,
58447c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
58457c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,
58467c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_CFG | conf_index,
58477c478bd9Sstevel@tonic-gate 		    0,
58487c478bd9Sstevel@tonic-gate 		    confdescr->wTotalLength,
58497c478bd9Sstevel@tonic-gate 		    &pdata,
58507c478bd9Sstevel@tonic-gate 		    0,
58517c478bd9Sstevel@tonic-gate 		    &completion_reason,
58527c478bd9Sstevel@tonic-gate 		    &cb_flags,
58537c478bd9Sstevel@tonic-gate 		    0)) == USB_SUCCESS) {
58547c478bd9Sstevel@tonic-gate 
5855d29f5a71Szhigang lu - Sun Microsystems - Beijing China 			if (MBLKL(pdata) !=
58567c478bd9Sstevel@tonic-gate 			    confdescr->wTotalLength) {
58577c478bd9Sstevel@tonic-gate 
5858d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
58597c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
58607c478bd9Sstevel@tonic-gate 				    "device returned incorrect "
58617c478bd9Sstevel@tonic-gate 				    "configuration descriptor.");
58627c478bd9Sstevel@tonic-gate 
58637c478bd9Sstevel@tonic-gate 				rval = USB_FAILURE;
58647c478bd9Sstevel@tonic-gate 				goto done;
58657c478bd9Sstevel@tonic-gate 			}
58667c478bd9Sstevel@tonic-gate 
58677c478bd9Sstevel@tonic-gate 			/*
58687c478bd9Sstevel@tonic-gate 			 * copy config descriptor into usba_device
58697c478bd9Sstevel@tonic-gate 			 */
58707c478bd9Sstevel@tonic-gate 			mutex_enter(&child_ud->usb_mutex);
58717c478bd9Sstevel@tonic-gate 			child_ud->usb_cfg_array[conf_index] =
58727c478bd9Sstevel@tonic-gate 			    kmem_alloc(confdescr->wTotalLength, KM_SLEEP);
58737c478bd9Sstevel@tonic-gate 			child_ud->usb_cfg_array_len[conf_index] =
58747c478bd9Sstevel@tonic-gate 			    confdescr->wTotalLength;
58757c478bd9Sstevel@tonic-gate 			bcopy((caddr_t)pdata->b_rptr,
58767c478bd9Sstevel@tonic-gate 			    (caddr_t)child_ud->usb_cfg_array[conf_index],
58777c478bd9Sstevel@tonic-gate 			    confdescr->wTotalLength);
58787c478bd9Sstevel@tonic-gate 			mutex_exit(&child_ud->usb_mutex);
58797c478bd9Sstevel@tonic-gate 
58807c478bd9Sstevel@tonic-gate 			/*
58817c478bd9Sstevel@tonic-gate 			 * retrieve string descriptor describing this
58827c478bd9Sstevel@tonic-gate 			 * configuration
58837c478bd9Sstevel@tonic-gate 			 */
58847c478bd9Sstevel@tonic-gate 			if (confdescr->iConfiguration) {
58857c478bd9Sstevel@tonic-gate 
58867c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
58877c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
58887c478bd9Sstevel@tonic-gate 				    "Get conf str descr for config_index=%d",
58897c478bd9Sstevel@tonic-gate 				    conf_index);
58907c478bd9Sstevel@tonic-gate 
58917c478bd9Sstevel@tonic-gate 				/*
58927c478bd9Sstevel@tonic-gate 				 * Now fetch the string descriptor describing
58937c478bd9Sstevel@tonic-gate 				 * this configuration
58947c478bd9Sstevel@tonic-gate 				 */
58957c478bd9Sstevel@tonic-gate 				if ((rval = usb_get_string_descr(dip,
58967c478bd9Sstevel@tonic-gate 				    USB_LANG_ID, confdescr->iConfiguration,
58977c478bd9Sstevel@tonic-gate 				    tmpbuf, USB_MAXSTRINGLEN)) ==
58987c478bd9Sstevel@tonic-gate 				    USB_SUCCESS) {
58997c478bd9Sstevel@tonic-gate 					size = strlen(tmpbuf);
59007c478bd9Sstevel@tonic-gate 					if (size > 0) {
59017c478bd9Sstevel@tonic-gate 						child_ud->usb_cfg_str_descr
59027c478bd9Sstevel@tonic-gate 						    [conf_index] = (char *)
59037c478bd9Sstevel@tonic-gate 						    kmem_zalloc(size + 1,
59047c478bd9Sstevel@tonic-gate 						    KM_SLEEP);
59057c478bd9Sstevel@tonic-gate 						(void) strcpy(
59067c478bd9Sstevel@tonic-gate 						    child_ud->usb_cfg_str_descr
59077c478bd9Sstevel@tonic-gate 						    [conf_index], tmpbuf);
59087c478bd9Sstevel@tonic-gate 					}
59097c478bd9Sstevel@tonic-gate 				} else {
59107c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
59117c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
59127c478bd9Sstevel@tonic-gate 					    "hubd_get_this_config_cloud: "
59137c478bd9Sstevel@tonic-gate 					    "getting config string (%d) "
59147c478bd9Sstevel@tonic-gate 					    "failed",
59157c478bd9Sstevel@tonic-gate 					    confdescr->iConfiguration);
59167c478bd9Sstevel@tonic-gate 
59177c478bd9Sstevel@tonic-gate 					/* ignore this error */
59187c478bd9Sstevel@tonic-gate 					rval = USB_SUCCESS;
59197c478bd9Sstevel@tonic-gate 				}
59207c478bd9Sstevel@tonic-gate 			}
59217c478bd9Sstevel@tonic-gate 		}
59227c478bd9Sstevel@tonic-gate 	}
59237c478bd9Sstevel@tonic-gate 
59247c478bd9Sstevel@tonic-gate done:
59257c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
59267c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59277c478bd9Sstevel@tonic-gate 		    "hubd_get_this_config_cloud: "
59287c478bd9Sstevel@tonic-gate 		    "error in retrieving config descriptor for "
59297c478bd9Sstevel@tonic-gate 		    "config index=%d rval=%d cr=%d",
59307c478bd9Sstevel@tonic-gate 		    conf_index, rval, completion_reason);
59317c478bd9Sstevel@tonic-gate 	}
59327c478bd9Sstevel@tonic-gate 
59337c478bd9Sstevel@tonic-gate 	if (pdata) {
59347c478bd9Sstevel@tonic-gate 		freemsg(pdata);
59357c478bd9Sstevel@tonic-gate 		pdata = NULL;
59367c478bd9Sstevel@tonic-gate 	}
59377c478bd9Sstevel@tonic-gate 
59387c478bd9Sstevel@tonic-gate 	kmem_free(confdescr, USB_CFG_DESCR_SIZE);
59397c478bd9Sstevel@tonic-gate 	kmem_free(tmpbuf, USB_MAXSTRINGLEN);
59407c478bd9Sstevel@tonic-gate 
59417c478bd9Sstevel@tonic-gate 	return (rval);
59427c478bd9Sstevel@tonic-gate }
59437c478bd9Sstevel@tonic-gate 
59447c478bd9Sstevel@tonic-gate 
59457c478bd9Sstevel@tonic-gate /*
59467c478bd9Sstevel@tonic-gate  * Retrieves the entire config cloud for all configurations of the device
59477c478bd9Sstevel@tonic-gate  */
59487c478bd9Sstevel@tonic-gate int
hubd_get_all_device_config_cloud(hubd_t * hubd,dev_info_t * dip,usba_device_t * child_ud)59497c478bd9Sstevel@tonic-gate hubd_get_all_device_config_cloud(hubd_t *hubd, dev_info_t *dip,
59507c478bd9Sstevel@tonic-gate     usba_device_t *child_ud)
59517c478bd9Sstevel@tonic-gate {
59527c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
59537c478bd9Sstevel@tonic-gate 	int		ncfgs;
59547c478bd9Sstevel@tonic-gate 	uint16_t	size;
59557c478bd9Sstevel@tonic-gate 	uint16_t	conf_index;
59567c478bd9Sstevel@tonic-gate 	uchar_t		**cfg_array;
59577c478bd9Sstevel@tonic-gate 	uint16_t	*cfg_array_len;
59587c478bd9Sstevel@tonic-gate 	char		**str_descr;
59597c478bd9Sstevel@tonic-gate 
59607c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
59617c478bd9Sstevel@tonic-gate 	    "hubd_get_all_device_config_cloud: Start");
59627c478bd9Sstevel@tonic-gate 
59637c478bd9Sstevel@tonic-gate 	/* alloc pointer array for conf. descriptors */
59647c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
59657c478bd9Sstevel@tonic-gate 	ncfgs = child_ud->usb_n_cfgs;
59667c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
59677c478bd9Sstevel@tonic-gate 
59687c478bd9Sstevel@tonic-gate 	size = sizeof (uchar_t *) * ncfgs;
59697c478bd9Sstevel@tonic-gate 	cfg_array = kmem_zalloc(size, KM_SLEEP);
59707c478bd9Sstevel@tonic-gate 	cfg_array_len = kmem_zalloc(ncfgs * sizeof (uint16_t), KM_SLEEP);
59717c478bd9Sstevel@tonic-gate 	str_descr = kmem_zalloc(size, KM_SLEEP);
59727c478bd9Sstevel@tonic-gate 
59737c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
59747c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array = cfg_array;
59757c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_len = cfg_array_len;
59767c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_length = size;
59777c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_array_len_length = ncfgs * sizeof (uint16_t);
59787c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_str_descr = str_descr;
59797c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
59807c478bd9Sstevel@tonic-gate 
59817c478bd9Sstevel@tonic-gate 	/* Get configuration descriptor for each configuration */
59827c478bd9Sstevel@tonic-gate 	for (conf_index = 0; (conf_index < ncfgs) &&
59837c478bd9Sstevel@tonic-gate 	    (rval == USB_SUCCESS); conf_index++) {
59847c478bd9Sstevel@tonic-gate 
59857c478bd9Sstevel@tonic-gate 		rval = hubd_get_this_config_cloud(hubd, dip, child_ud,
59867c478bd9Sstevel@tonic-gate 		    conf_index);
59877c478bd9Sstevel@tonic-gate 	}
59887c478bd9Sstevel@tonic-gate 
59897c478bd9Sstevel@tonic-gate 	return (rval);
59907c478bd9Sstevel@tonic-gate }
59917c478bd9Sstevel@tonic-gate 
59927c478bd9Sstevel@tonic-gate 
59937c478bd9Sstevel@tonic-gate /*
59947c478bd9Sstevel@tonic-gate  * hubd_ready_device:
59957c478bd9Sstevel@tonic-gate  *	Update the usba_device structure
59967c478bd9Sstevel@tonic-gate  *	Set the given configuration
59977c478bd9Sstevel@tonic-gate  *	Prepares the device node for driver to online. If an existing
59987c478bd9Sstevel@tonic-gate  *	OBP node is found, it will switch to the OBP node.
59997c478bd9Sstevel@tonic-gate  */
6000ff0e937bSRaymond Chen dev_info_t *
hubd_ready_device(hubd_t * hubd,dev_info_t * child_dip,usba_device_t * child_ud,uint_t config_index)60017c478bd9Sstevel@tonic-gate hubd_ready_device(hubd_t *hubd, dev_info_t *child_dip, usba_device_t *child_ud,
600235f36846Ssl147100     uint_t config_index)
60037c478bd9Sstevel@tonic-gate {
60047c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason;
60057c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
60067c478bd9Sstevel@tonic-gate 	size_t		size;
60077c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	config_descriptor;
60087c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t def_ph;
60097c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph;
60107c478bd9Sstevel@tonic-gate 
60117c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6012112116d8Sfb209375 	    "hubd_ready_device: dip=0x%p, user_conf_index=%d",
6013112116d8Sfb209375 	    (void *)child_dip, config_index);
60147c478bd9Sstevel@tonic-gate 
60157c478bd9Sstevel@tonic-gate 	size = usb_parse_cfg_descr(
60167c478bd9Sstevel@tonic-gate 	    child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
60177c478bd9Sstevel@tonic-gate 	    &config_descriptor, USB_CFG_DESCR_SIZE);
60187c478bd9Sstevel@tonic-gate 	ASSERT(size == USB_CFG_DESCR_SIZE);
60197c478bd9Sstevel@tonic-gate 
60207c478bd9Sstevel@tonic-gate 	def_ph = usba_get_dflt_pipe_handle(child_dip);
60217c478bd9Sstevel@tonic-gate 
60227c478bd9Sstevel@tonic-gate 	/* Set the configuration */
60237c478bd9Sstevel@tonic-gate 	(void) usb_pipe_sync_ctrl_xfer(child_dip, def_ph,
60247c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_HOST_TO_DEV,
60257c478bd9Sstevel@tonic-gate 	    USB_REQ_SET_CFG,	/* bRequest */
60267c478bd9Sstevel@tonic-gate 	    config_descriptor.bConfigurationValue,	/* wValue */
60277c478bd9Sstevel@tonic-gate 	    0,				/* wIndex */
60287c478bd9Sstevel@tonic-gate 	    0,				/* wLength */
60297c478bd9Sstevel@tonic-gate 	    NULL,
60307c478bd9Sstevel@tonic-gate 	    0,
60317c478bd9Sstevel@tonic-gate 	    &completion_reason,
60327c478bd9Sstevel@tonic-gate 	    &cb_flags,
60337c478bd9Sstevel@tonic-gate 	    0);
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
60367c478bd9Sstevel@tonic-gate 	child_ud->usb_active_cfg_ndx	= config_index;
60377c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg		= child_ud->usb_cfg_array[config_index];
60387c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_length	= config_descriptor.wTotalLength;
60397c478bd9Sstevel@tonic-gate 	child_ud->usb_cfg_value		= config_descriptor.bConfigurationValue;
60407c478bd9Sstevel@tonic-gate 	child_ud->usb_n_ifs		= config_descriptor.bNumInterfaces;
60417c478bd9Sstevel@tonic-gate 	child_ud->usb_dip		= child_dip;
60427c478bd9Sstevel@tonic-gate 
60437c478bd9Sstevel@tonic-gate 	child_ud->usb_client_flags	= kmem_zalloc(
60447c478bd9Sstevel@tonic-gate 	    child_ud->usb_n_ifs * USBA_CLIENT_FLAG_SIZE, KM_SLEEP);
60457c478bd9Sstevel@tonic-gate 
60467c478bd9Sstevel@tonic-gate 	child_ud->usb_client_attach_list = kmem_zalloc(
60477c478bd9Sstevel@tonic-gate 	    child_ud->usb_n_ifs *
60487c478bd9Sstevel@tonic-gate 	    sizeof (*child_ud->usb_client_attach_list), KM_SLEEP);
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate 	child_ud->usb_client_ev_cb_list = kmem_zalloc(
60517c478bd9Sstevel@tonic-gate 	    child_ud->usb_n_ifs *
60527c478bd9Sstevel@tonic-gate 	    sizeof (*child_ud->usb_client_ev_cb_list), KM_SLEEP);
60537c478bd9Sstevel@tonic-gate 
60547c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
60557c478bd9Sstevel@tonic-gate 
60567c478bd9Sstevel@tonic-gate 	/* ready the device node */
60577c478bd9Sstevel@tonic-gate 	child_dip = usba_ready_device_node(child_dip);
60587c478bd9Sstevel@tonic-gate 
60597c478bd9Sstevel@tonic-gate 	/* set owner of default pipe to child dip */
60607c478bd9Sstevel@tonic-gate 	ph = usba_get_ph_data(def_ph);
60617c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
60627c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_ph_impl->usba_ph_mutex);
60637c478bd9Sstevel@tonic-gate 	ph->p_ph_impl->usba_ph_dip = ph->p_dip = child_dip;
60647c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_ph_impl->usba_ph_mutex);
60657c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
60667c478bd9Sstevel@tonic-gate 
60677c478bd9Sstevel@tonic-gate 	return (child_dip);
60687c478bd9Sstevel@tonic-gate }
60697c478bd9Sstevel@tonic-gate 
60707c478bd9Sstevel@tonic-gate /*
60717c478bd9Sstevel@tonic-gate  * hubd_create_child
60727c478bd9Sstevel@tonic-gate  *	- create child dip
60737c478bd9Sstevel@tonic-gate  *	- open default pipe
60747c478bd9Sstevel@tonic-gate  *	- get device descriptor
60757c478bd9Sstevel@tonic-gate  *	- set the address
60767c478bd9Sstevel@tonic-gate  *	- get device string descriptors
60777c478bd9Sstevel@tonic-gate  *	- get the entire config cloud (all configurations) of the device
60787c478bd9Sstevel@tonic-gate  *	- set user preferred configuration
60797c478bd9Sstevel@tonic-gate  *	- close default pipe
60807c478bd9Sstevel@tonic-gate  *	- load appropriate driver(s)
60817c478bd9Sstevel@tonic-gate  */
60827c478bd9Sstevel@tonic-gate static int
hubd_create_child(dev_info_t * dip,hubd_t * hubd,usba_device_t * hubd_ud,usb_port_status_t port_status,usb_port_t port,int iteration)60837c478bd9Sstevel@tonic-gate hubd_create_child(dev_info_t *dip,
60847c478bd9Sstevel@tonic-gate     hubd_t		*hubd,
60857c478bd9Sstevel@tonic-gate     usba_device_t	*hubd_ud,
60867c478bd9Sstevel@tonic-gate     usb_port_status_t port_status,
60877c478bd9Sstevel@tonic-gate     usb_port_t	port,
60887c478bd9Sstevel@tonic-gate     int		iteration)
60897c478bd9Sstevel@tonic-gate {
60907c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
60917c478bd9Sstevel@tonic-gate 	usb_dev_descr_t	usb_dev_descr;
60927c478bd9Sstevel@tonic-gate 	int			rval;
60937c478bd9Sstevel@tonic-gate 	usba_device_t		*child_ud = NULL;
60947c478bd9Sstevel@tonic-gate 	usba_device_t		*parent_ud = NULL;
60957c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	ph = NULL; /* default pipe handle */
60967c478bd9Sstevel@tonic-gate 	mblk_t			*pdata = NULL;
60977c478bd9Sstevel@tonic-gate 	usb_cr_t		completion_reason;
609835f36846Ssl147100 	int			user_conf_index;
609935f36846Ssl147100 	uint_t			config_index;
61007c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
61017c478bd9Sstevel@tonic-gate 	uchar_t			address = 0;
61027c478bd9Sstevel@tonic-gate 	uint16_t		length;
61037c478bd9Sstevel@tonic-gate 	size_t			size;
61047c478bd9Sstevel@tonic-gate 	usb_addr_t		parent_usb_addr;
61057c478bd9Sstevel@tonic-gate 	usb_port_t		parent_usb_port;
61067c478bd9Sstevel@tonic-gate 	usba_device_t		*parent_usba_dev;
61077c478bd9Sstevel@tonic-gate 	usb_port_status_t	parent_port_status;
6108993e3fafSRobert Mustacchi 	boolean_t		hcd_called = B_FALSE;
61097c478bd9Sstevel@tonic-gate 
61107c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61117c478bd9Sstevel@tonic-gate 	    "hubd_create_child: port=%d", port);
61127c478bd9Sstevel@tonic-gate 
61137c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
61147c478bd9Sstevel@tonic-gate 	ASSERT(hubd->h_usba_devices[port] == NULL);
61157c478bd9Sstevel@tonic-gate 
61167c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
61177c478bd9Sstevel@tonic-gate 
61187c478bd9Sstevel@tonic-gate 	/*
61197c478bd9Sstevel@tonic-gate 	 * create a dip which can be used to open the pipe. we set
61207c478bd9Sstevel@tonic-gate 	 * the name after getting the descriptors from the device
61217c478bd9Sstevel@tonic-gate 	 */
61227c478bd9Sstevel@tonic-gate 	rval = usba_create_child_devi(dip,
61237c478bd9Sstevel@tonic-gate 	    "device",		/* driver name */
61247c478bd9Sstevel@tonic-gate 	    hubd_ud->usb_hcdi_ops, /* usba_hcdi ops */
61257c478bd9Sstevel@tonic-gate 	    hubd_ud->usb_root_hub_dip,
61267c478bd9Sstevel@tonic-gate 	    port_status,		/* low speed device */
61277c478bd9Sstevel@tonic-gate 	    child_ud,
61287c478bd9Sstevel@tonic-gate 	    &child_dip);
61297c478bd9Sstevel@tonic-gate 
61307c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
61317c478bd9Sstevel@tonic-gate 
6132d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
61337c478bd9Sstevel@tonic-gate 		    "usb_create_child_devi failed (%d)", rval);
61347c478bd9Sstevel@tonic-gate 
61357c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
61367c478bd9Sstevel@tonic-gate 	}
61377c478bd9Sstevel@tonic-gate 
61387c478bd9Sstevel@tonic-gate 	child_ud = usba_get_usba_device(child_dip);
61397c478bd9Sstevel@tonic-gate 	ASSERT(child_ud != NULL);
61407c478bd9Sstevel@tonic-gate 
61417c478bd9Sstevel@tonic-gate 	parent_ud = hubd->h_usba_device;
61427c478bd9Sstevel@tonic-gate 	mutex_enter(&parent_ud->usb_mutex);
61437c478bd9Sstevel@tonic-gate 	parent_port_status = parent_ud->usb_port_status;
61447c478bd9Sstevel@tonic-gate 
61457c478bd9Sstevel@tonic-gate 	/*
6146993e3fafSRobert Mustacchi 	 * To support split transactions, update address and port of high speed
6147993e3fafSRobert Mustacchi 	 * hub to which given device is connected.  Note, split transactions
6148993e3fafSRobert Mustacchi 	 * only exist for high speed devices.
61497c478bd9Sstevel@tonic-gate 	 */
61507c478bd9Sstevel@tonic-gate 	if (parent_port_status == USBA_HIGH_SPEED_DEV) {
61517c478bd9Sstevel@tonic-gate 		parent_usba_dev = parent_ud;
61527c478bd9Sstevel@tonic-gate 		parent_usb_addr = parent_ud->usb_addr;
61537c478bd9Sstevel@tonic-gate 		parent_usb_port = port;
61547c478bd9Sstevel@tonic-gate 	} else {
61557c478bd9Sstevel@tonic-gate 		parent_usba_dev = parent_ud->usb_hs_hub_usba_dev;
61567c478bd9Sstevel@tonic-gate 		parent_usb_addr = parent_ud->usb_hs_hub_addr;
61577c478bd9Sstevel@tonic-gate 		parent_usb_port = parent_ud->usb_hs_hub_port;
61587c478bd9Sstevel@tonic-gate 	}
61597c478bd9Sstevel@tonic-gate 	mutex_exit(&parent_ud->usb_mutex);
61607c478bd9Sstevel@tonic-gate 
61617c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
61627c478bd9Sstevel@tonic-gate 	address = child_ud->usb_addr;
61637c478bd9Sstevel@tonic-gate 	child_ud->usb_addr = 0;
61647c478bd9Sstevel@tonic-gate 	child_ud->usb_dev_descr = kmem_alloc(sizeof (usb_dev_descr_t),
61657c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
61667c478bd9Sstevel@tonic-gate 	bzero(&usb_dev_descr, sizeof (usb_dev_descr_t));
6167993e3fafSRobert Mustacchi 
6168993e3fafSRobert Mustacchi 	switch (port_status) {
6169993e3fafSRobert Mustacchi 	case USBA_SUPER_SPEED_DEV:
6170993e3fafSRobert Mustacchi 		usb_dev_descr.bMaxPacketSize0 = 9;
6171993e3fafSRobert Mustacchi 		break;
6172993e3fafSRobert Mustacchi 	case USBA_LOW_SPEED_DEV:
6173993e3fafSRobert Mustacchi 		usb_dev_descr.bMaxPacketSize0 = 8;
6174993e3fafSRobert Mustacchi 		break;
6175993e3fafSRobert Mustacchi 	default:
6176993e3fafSRobert Mustacchi 		usb_dev_descr.bMaxPacketSize0 = 64;
6177993e3fafSRobert Mustacchi 		break;
6178993e3fafSRobert Mustacchi 	}
61797c478bd9Sstevel@tonic-gate 	bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
61807c478bd9Sstevel@tonic-gate 	    sizeof (usb_dev_descr_t));
61817c478bd9Sstevel@tonic-gate 	child_ud->usb_port = port;
6182993e3fafSRobert Mustacchi 
6183993e3fafSRobert Mustacchi 	/*
6184993e3fafSRobert Mustacchi 	 * The parent hub always keeps track of the hub this device is connected
6185993e3fafSRobert Mustacchi 	 * to; however, the hs_hub_* variables are only keeping track of the
6186993e3fafSRobert Mustacchi 	 * closest high speed hub. Unfortunately, we need both.
6187993e3fafSRobert Mustacchi 	 */
6188993e3fafSRobert Mustacchi 	child_ud->usb_parent_hub = parent_ud;
61897c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_usba_dev = parent_usba_dev;
61907c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_addr = parent_usb_addr;
61917c478bd9Sstevel@tonic-gate 	child_ud->usb_hs_hub_port = parent_usb_port;
61927c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
61937c478bd9Sstevel@tonic-gate 
6194993e3fafSRobert Mustacchi 	/*
6195993e3fafSRobert Mustacchi 	 * Before we open up the default pipe, give the HCD a chance to do
6196993e3fafSRobert Mustacchi 	 * something here.
6197993e3fafSRobert Mustacchi 	 */
6198993e3fafSRobert Mustacchi 	if (child_ud->usb_hcdi_ops->usba_hcdi_device_init != NULL) {
6199993e3fafSRobert Mustacchi 		int rval;
6200993e3fafSRobert Mustacchi 		void *priv = NULL;
6201993e3fafSRobert Mustacchi 
6202993e3fafSRobert Mustacchi 		rval = child_ud->usb_hcdi_ops->usba_hcdi_device_init(child_ud,
6203993e3fafSRobert Mustacchi 		    port, &priv);
6204993e3fafSRobert Mustacchi 		if (rval != USB_SUCCESS) {
6205993e3fafSRobert Mustacchi 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6206993e3fafSRobert Mustacchi 			    "HCD usba_hcdi_Device_init failed (%d)", rval);
6207993e3fafSRobert Mustacchi 			goto fail_cleanup;
6208993e3fafSRobert Mustacchi 		}
6209993e3fafSRobert Mustacchi 
6210993e3fafSRobert Mustacchi 		child_ud->usb_hcd_private = priv;
6211993e3fafSRobert Mustacchi 		hcd_called = B_TRUE;
6212993e3fafSRobert Mustacchi 	}
6213993e3fafSRobert Mustacchi 
6214993e3fafSRobert Mustacchi 
6215993e3fafSRobert Mustacchi 
62167c478bd9Sstevel@tonic-gate 	/* Open the default pipe */
62177c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
62187c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
6219d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62207c478bd9Sstevel@tonic-gate 		    "usb_pipe_open failed (%d)", rval);
62217c478bd9Sstevel@tonic-gate 
62227c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
62237c478bd9Sstevel@tonic-gate 	}
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate 	/*
62267c478bd9Sstevel@tonic-gate 	 * get device descriptor
62277c478bd9Sstevel@tonic-gate 	 */
62287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62297c478bd9Sstevel@tonic-gate 	    "hubd_create_child: get device descriptor: 64 bytes");
62307c478bd9Sstevel@tonic-gate 
62317c478bd9Sstevel@tonic-gate 	rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
62327c478bd9Sstevel@tonic-gate 	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
62337c478bd9Sstevel@tonic-gate 	    USB_REQ_GET_DESCR,			/* bRequest */
62347c478bd9Sstevel@tonic-gate 	    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
62357c478bd9Sstevel@tonic-gate 	    0,					/* wIndex */
62367c478bd9Sstevel@tonic-gate 	    64,					/* wLength */
62377c478bd9Sstevel@tonic-gate 	    &pdata, USB_ATTRS_SHORT_XFER_OK,
62387c478bd9Sstevel@tonic-gate 	    &completion_reason, &cb_flags, 0);
62397c478bd9Sstevel@tonic-gate 
6240993e3fafSRobert Mustacchi 	/*
6241993e3fafSRobert Mustacchi 	 * If this is a full speed device, we cannot assume that its default
6242993e3fafSRobert Mustacchi 	 * packet size is 64 bytes, it may be 8 bytes.
6243993e3fafSRobert Mustacchi 	 */
6244993e3fafSRobert Mustacchi 
62457c478bd9Sstevel@tonic-gate 	if ((rval != USB_SUCCESS) &&
62467c478bd9Sstevel@tonic-gate 	    (!((completion_reason == USB_CR_DATA_OVERRUN) && pdata))) {
62477c478bd9Sstevel@tonic-gate 
62487c478bd9Sstevel@tonic-gate 		/*
62497c478bd9Sstevel@tonic-gate 		 * rval != USB_SUCCESS AND
62507c478bd9Sstevel@tonic-gate 		 * completion_reason != USB_CR_DATA_OVERRUN
62517c478bd9Sstevel@tonic-gate 		 * pdata could be != NULL.
62527c478bd9Sstevel@tonic-gate 		 * Free pdata now to prevent memory leak.
62537c478bd9Sstevel@tonic-gate 		 */
62547c478bd9Sstevel@tonic-gate 		freemsg(pdata);
62557c478bd9Sstevel@tonic-gate 		pdata = NULL;
62567c478bd9Sstevel@tonic-gate 
62577c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62587c478bd9Sstevel@tonic-gate 		    "hubd_create_child: get device descriptor: 8 bytes");
62597c478bd9Sstevel@tonic-gate 
62607c478bd9Sstevel@tonic-gate 		rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
62617c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
62627c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
62637c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
62647c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
62657c478bd9Sstevel@tonic-gate 		    8,					/* wLength */
62667c478bd9Sstevel@tonic-gate 		    &pdata, USB_ATTRS_NONE,
62677c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0);
62687c478bd9Sstevel@tonic-gate 
62697c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
6270d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62717c478bd9Sstevel@tonic-gate 			    "getting device descriptor failed (%s 0x%x %d)",
62727c478bd9Sstevel@tonic-gate 			    usb_str_cr(completion_reason), cb_flags, rval);
62737c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
62747c478bd9Sstevel@tonic-gate 		}
62757c478bd9Sstevel@tonic-gate 	} else {
62767c478bd9Sstevel@tonic-gate 		ASSERT(completion_reason == USB_CR_OK);
62777c478bd9Sstevel@tonic-gate 	}
62787c478bd9Sstevel@tonic-gate 
62797c478bd9Sstevel@tonic-gate 	ASSERT(pdata != NULL);
62807c478bd9Sstevel@tonic-gate 
62817c478bd9Sstevel@tonic-gate 	size = usb_parse_dev_descr(
62827c478bd9Sstevel@tonic-gate 	    pdata->b_rptr,
6283d29f5a71Szhigang lu - Sun Microsystems - Beijing China 	    MBLKL(pdata),
62847c478bd9Sstevel@tonic-gate 	    &usb_dev_descr,
62857c478bd9Sstevel@tonic-gate 	    sizeof (usb_dev_descr_t));
62867c478bd9Sstevel@tonic-gate 
62877c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62887c478bd9Sstevel@tonic-gate 	    "parsing device descriptor returned %lu", size);
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 	length = *(pdata->b_rptr);
62917c478bd9Sstevel@tonic-gate 	freemsg(pdata);
62927c478bd9Sstevel@tonic-gate 	pdata = NULL;
62937c478bd9Sstevel@tonic-gate 	if (size < 8) {
6294d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
62957c478bd9Sstevel@tonic-gate 		    "get device descriptor returned %lu bytes", size);
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
62987c478bd9Sstevel@tonic-gate 	}
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate 	if (length < 8) {
6301d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
63027c478bd9Sstevel@tonic-gate 		    "fail enumeration: bLength=%d", length);
63037c478bd9Sstevel@tonic-gate 
63047c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
63057c478bd9Sstevel@tonic-gate 	}
63067c478bd9Sstevel@tonic-gate 
6307993e3fafSRobert Mustacchi 	if (child_ud->usb_hcdi_ops->usba_hcdi_device_address != NULL) {
6308993e3fafSRobert Mustacchi 		rval = child_ud->usb_hcdi_ops->usba_hcdi_device_address(
6309993e3fafSRobert Mustacchi 		    child_ud);
6310993e3fafSRobert Mustacchi 		if (rval != USB_SUCCESS)
6311993e3fafSRobert Mustacchi 			goto fail_cleanup;
6312993e3fafSRobert Mustacchi 	} else {
63137c478bd9Sstevel@tonic-gate 		/* Set the address of the device */
63147c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
63157c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_HOST_TO_DEV,
63167c478bd9Sstevel@tonic-gate 		    USB_REQ_SET_ADDRESS,	/* bRequest */
63177c478bd9Sstevel@tonic-gate 		    address,			/* wValue */
63187c478bd9Sstevel@tonic-gate 		    0,				/* wIndex */
63197c478bd9Sstevel@tonic-gate 		    0,				/* wLength */
63207c478bd9Sstevel@tonic-gate 		    NULL, 0,
63217c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
63227c478bd9Sstevel@tonic-gate 			char buffer[64];
6323d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
6324993e3fafSRobert Mustacchi 			    "setting address failed (cr=%s cb_flags=%s "
6325993e3fafSRobert Mustacchi 			    "rval=%d)", usb_str_cr(completion_reason),
63267c478bd9Sstevel@tonic-gate 			    usb_str_cb_flags(cb_flags, buffer, sizeof (buffer)),
63277c478bd9Sstevel@tonic-gate 			    rval);
63287c478bd9Sstevel@tonic-gate 
63297c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
63307c478bd9Sstevel@tonic-gate 		}
6331993e3fafSRobert Mustacchi 	}
63327c478bd9Sstevel@tonic-gate 
63337c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
63347c478bd9Sstevel@tonic-gate 	    "set address 0x%x done", address);
63357c478bd9Sstevel@tonic-gate 
63367c478bd9Sstevel@tonic-gate 	/* now close the pipe for addr 0 */
63377c478bd9Sstevel@tonic-gate 	usb_pipe_close(child_dip, ph,
63387c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
63397c478bd9Sstevel@tonic-gate 
63407c478bd9Sstevel@tonic-gate 	/*
63417c478bd9Sstevel@tonic-gate 	 * This delay is important for the CATC hub to enumerate
63427c478bd9Sstevel@tonic-gate 	 * But, avoid delay in the first iteration
63437c478bd9Sstevel@tonic-gate 	 */
63447c478bd9Sstevel@tonic-gate 	if (iteration) {
63457c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(hubd_device_delay/100));
63467c478bd9Sstevel@tonic-gate 	}
63477c478bd9Sstevel@tonic-gate 
63487c478bd9Sstevel@tonic-gate 	/* assign the address in the usba_device structure */
63497c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
63507c478bd9Sstevel@tonic-gate 	child_ud->usb_addr = address;
63517c478bd9Sstevel@tonic-gate 	child_ud->usb_no_cpr = 0;
63527c478bd9Sstevel@tonic-gate 	child_ud->usb_port_status = port_status;
63537c478bd9Sstevel@tonic-gate 	/* save this device descriptor */
63547c478bd9Sstevel@tonic-gate 	bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
63557c478bd9Sstevel@tonic-gate 	    sizeof (usb_dev_descr_t));
63567c478bd9Sstevel@tonic-gate 	child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
63577c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
63587c478bd9Sstevel@tonic-gate 
63597c478bd9Sstevel@tonic-gate 	/* re-open the pipe for the device with the new address */
63607c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_open(child_dip, NULL, NULL,
63617c478bd9Sstevel@tonic-gate 	    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, &ph)) != USB_SUCCESS) {
6362d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
63637c478bd9Sstevel@tonic-gate 		    "usb_pipe_open failed (%d)", rval);
63647c478bd9Sstevel@tonic-gate 
63657c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
63667c478bd9Sstevel@tonic-gate 	}
63677c478bd9Sstevel@tonic-gate 
63687c478bd9Sstevel@tonic-gate 	/*
63697c478bd9Sstevel@tonic-gate 	 * Get full device descriptor only if we have not received full
63707c478bd9Sstevel@tonic-gate 	 * device descriptor earlier.
63717c478bd9Sstevel@tonic-gate 	 */
63727c478bd9Sstevel@tonic-gate 	if (size < length) {
63737c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
63747c478bd9Sstevel@tonic-gate 		    "hubd_create_child: get full device descriptor: "
63757c478bd9Sstevel@tonic-gate 		    "%d bytes", length);
63767c478bd9Sstevel@tonic-gate 
63777c478bd9Sstevel@tonic-gate 		if ((rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
63787c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
63797c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
63807c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV,		/* wValue */
63817c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
63827c478bd9Sstevel@tonic-gate 		    length,				/* wLength */
63837c478bd9Sstevel@tonic-gate 		    &pdata, 0,
63847c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0)) != USB_SUCCESS) {
63857c478bd9Sstevel@tonic-gate 			freemsg(pdata);
63867c478bd9Sstevel@tonic-gate 			pdata = NULL;
63877c478bd9Sstevel@tonic-gate 
63887c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG,
63897c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
63907c478bd9Sstevel@tonic-gate 			    "hubd_create_child: get full device descriptor: "
63917c478bd9Sstevel@tonic-gate 			    "64 bytes");
63927c478bd9Sstevel@tonic-gate 
63937c478bd9Sstevel@tonic-gate 			rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
63947c478bd9Sstevel@tonic-gate 			    USB_DEV_REQ_DEV_TO_HOST |
63957c478bd9Sstevel@tonic-gate 			    USB_DEV_REQ_TYPE_STANDARD,
63967c478bd9Sstevel@tonic-gate 			    USB_REQ_GET_DESCR,		/* bRequest */
63977c478bd9Sstevel@tonic-gate 			    USB_DESCR_TYPE_SETUP_DEV,	/* wValue */
63987c478bd9Sstevel@tonic-gate 			    0,				/* wIndex */
63997c478bd9Sstevel@tonic-gate 			    64,				/* wLength */
64007c478bd9Sstevel@tonic-gate 			    &pdata, USB_ATTRS_SHORT_XFER_OK,
64017c478bd9Sstevel@tonic-gate 			    &completion_reason, &cb_flags, 0);
64027c478bd9Sstevel@tonic-gate 
64037c478bd9Sstevel@tonic-gate 			/* we have to trust the data now */
64047c478bd9Sstevel@tonic-gate 			if (pdata) {
64057c478bd9Sstevel@tonic-gate 				int len = *(pdata->b_rptr);
64067c478bd9Sstevel@tonic-gate 
6407d29f5a71Szhigang lu - Sun Microsystems - Beijing China 				length = MBLKL(pdata);
64087c478bd9Sstevel@tonic-gate 				if (length < len) {
64097c478bd9Sstevel@tonic-gate 
64107c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
64117c478bd9Sstevel@tonic-gate 				}
64127c478bd9Sstevel@tonic-gate 			} else if (rval != USB_SUCCESS) {
6413d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
64147c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
64157c478bd9Sstevel@tonic-gate 				    "getting device descriptor failed "
64167c478bd9Sstevel@tonic-gate 				    "(%d 0x%x %d)",
64177c478bd9Sstevel@tonic-gate 				    completion_reason, cb_flags, rval);
64187c478bd9Sstevel@tonic-gate 
64197c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
64207c478bd9Sstevel@tonic-gate 			}
64217c478bd9Sstevel@tonic-gate 		}
64227c478bd9Sstevel@tonic-gate 
64237c478bd9Sstevel@tonic-gate 		size = usb_parse_dev_descr(
64247c478bd9Sstevel@tonic-gate 		    pdata->b_rptr,
6425d29f5a71Szhigang lu - Sun Microsystems - Beijing China 		    MBLKL(pdata),
64267c478bd9Sstevel@tonic-gate 		    &usb_dev_descr,
64277c478bd9Sstevel@tonic-gate 		    sizeof (usb_dev_descr_t));
64287c478bd9Sstevel@tonic-gate 
64297c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
64307c478bd9Sstevel@tonic-gate 		    "parsing device descriptor returned %lu", size);
64317c478bd9Sstevel@tonic-gate 
64327c478bd9Sstevel@tonic-gate 		/*
64337c478bd9Sstevel@tonic-gate 		 * For now, free the data
64347c478bd9Sstevel@tonic-gate 		 * eventually, each configuration may need to be looked at
64357c478bd9Sstevel@tonic-gate 		 */
64367c478bd9Sstevel@tonic-gate 		freemsg(pdata);
64377c478bd9Sstevel@tonic-gate 		pdata = NULL;
64387c478bd9Sstevel@tonic-gate 
64397c478bd9Sstevel@tonic-gate 		if (size != USB_DEV_DESCR_SIZE) {
6440d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
64417c478bd9Sstevel@tonic-gate 			    "fail enumeration: descriptor size=%lu "
64427c478bd9Sstevel@tonic-gate 			    "expected size=%u", size, USB_DEV_DESCR_SIZE);
64437c478bd9Sstevel@tonic-gate 
64447c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
64457c478bd9Sstevel@tonic-gate 		}
64467c478bd9Sstevel@tonic-gate 
64477c478bd9Sstevel@tonic-gate 		/*
64487c478bd9Sstevel@tonic-gate 		 * save the device descriptor in usba_device since it is needed
64497c478bd9Sstevel@tonic-gate 		 * later on again
64507c478bd9Sstevel@tonic-gate 		 */
64517c478bd9Sstevel@tonic-gate 		mutex_enter(&child_ud->usb_mutex);
64527c478bd9Sstevel@tonic-gate 		bcopy(&usb_dev_descr, child_ud->usb_dev_descr,
64537c478bd9Sstevel@tonic-gate 		    sizeof (usb_dev_descr_t));
64547c478bd9Sstevel@tonic-gate 		child_ud->usb_n_cfgs = usb_dev_descr.bNumConfigurations;
64557c478bd9Sstevel@tonic-gate 		mutex_exit(&child_ud->usb_mutex);
64567c478bd9Sstevel@tonic-gate 	}
64577c478bd9Sstevel@tonic-gate 
64587c478bd9Sstevel@tonic-gate 	if (usb_dev_descr.bNumConfigurations == 0) {
6459d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
64607c478bd9Sstevel@tonic-gate 		    "device descriptor:\n\t"
64617c478bd9Sstevel@tonic-gate 		    "l=0x%x type=0x%x USB=0x%x class=0x%x subclass=0x%x\n\t"
64627c478bd9Sstevel@tonic-gate 		    "protocol=0x%x maxpktsize=0x%x "
64637c478bd9Sstevel@tonic-gate 		    "Vid=0x%x Pid=0x%x rel=0x%x\n\t"
64647c478bd9Sstevel@tonic-gate 		    "Mfg=0x%x P=0x%x sn=0x%x #config=0x%x",
64657c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bLength, usb_dev_descr.bDescriptorType,
64667c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bcdUSB, usb_dev_descr.bDeviceClass,
64677c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bDeviceSubClass,
64687c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bDeviceProtocol,
64697c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bMaxPacketSize0,
64707c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idVendor,
64717c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idProduct, usb_dev_descr.bcdDevice,
64727c478bd9Sstevel@tonic-gate 		    usb_dev_descr.iManufacturer, usb_dev_descr.iProduct,
64737c478bd9Sstevel@tonic-gate 		    usb_dev_descr.iSerialNumber,
64747c478bd9Sstevel@tonic-gate 		    usb_dev_descr.bNumConfigurations);
64757c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
64767c478bd9Sstevel@tonic-gate 	}
64777c478bd9Sstevel@tonic-gate 
64780d2006e4SRobert Mustacchi 	/* Read the BOS data */
64790d2006e4SRobert Mustacchi 	usba_get_binary_object_store(child_dip, child_ud);
64807c478bd9Sstevel@tonic-gate 
64817c478bd9Sstevel@tonic-gate 	/* get the device string descriptor(s) */
64827c478bd9Sstevel@tonic-gate 	usba_get_dev_string_descrs(child_dip, child_ud);
64837c478bd9Sstevel@tonic-gate 
64847c478bd9Sstevel@tonic-gate 	/* retrieve config cloud for all configurations */
64857c478bd9Sstevel@tonic-gate 	rval = hubd_get_all_device_config_cloud(hubd, child_dip, child_ud);
64867c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
6487d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
64887c478bd9Sstevel@tonic-gate 		    "failed to get configuration descriptor(s)");
64897c478bd9Sstevel@tonic-gate 
64907c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
64917c478bd9Sstevel@tonic-gate 	}
64927c478bd9Sstevel@tonic-gate 
64937c478bd9Sstevel@tonic-gate 	/* get the preferred configuration for this device */
64947c478bd9Sstevel@tonic-gate 	user_conf_index = hubd_select_device_configuration(hubd, port,
64957c478bd9Sstevel@tonic-gate 	    child_dip, child_ud);
64967c478bd9Sstevel@tonic-gate 
64977c478bd9Sstevel@tonic-gate 	/* Check if the user selected configuration index is in range */
649835f36846Ssl147100 	if ((user_conf_index >= usb_dev_descr.bNumConfigurations) ||
649935f36846Ssl147100 	    (user_conf_index < 0)) {
6500d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
65017c478bd9Sstevel@tonic-gate 		    "Configuration index for device idVendor=%d "
65027c478bd9Sstevel@tonic-gate 		    "idProduct=%d is=%d, and is out of range[0..%d]",
65037c478bd9Sstevel@tonic-gate 		    usb_dev_descr.idVendor, usb_dev_descr.idProduct,
65047c478bd9Sstevel@tonic-gate 		    user_conf_index, usb_dev_descr.bNumConfigurations - 1);
65057c478bd9Sstevel@tonic-gate 
65067c478bd9Sstevel@tonic-gate 		/* treat this as user didn't specify configuration */
65077c478bd9Sstevel@tonic-gate 		user_conf_index = USBA_DEV_CONFIG_INDEX_UNDEFINED;
65087c478bd9Sstevel@tonic-gate 	}
65097c478bd9Sstevel@tonic-gate 
65107c478bd9Sstevel@tonic-gate 
65117c478bd9Sstevel@tonic-gate 	/*
65127c478bd9Sstevel@tonic-gate 	 * Warn users of a performance hit if connecting a
65137c478bd9Sstevel@tonic-gate 	 * High Speed behind a 1.1 hub, which is behind a
6514993e3fafSRobert Mustacchi 	 * 2.0 port. Don't worry about this for USB 3.x for now.
65157c478bd9Sstevel@tonic-gate 	 */
65167c478bd9Sstevel@tonic-gate 	if ((parent_port_status != USBA_HIGH_SPEED_DEV) &&
65177c478bd9Sstevel@tonic-gate 	    !(usba_is_root_hub(parent_ud->usb_dip)) &&
65187c478bd9Sstevel@tonic-gate 	    (parent_usb_addr)) {
65197c478bd9Sstevel@tonic-gate 
65207c478bd9Sstevel@tonic-gate 		/*
65217c478bd9Sstevel@tonic-gate 		 * Now that we know the root port is a high speed port
65227c478bd9Sstevel@tonic-gate 		 * and that the parent port is not a high speed port,
65237c478bd9Sstevel@tonic-gate 		 * let's find out if the device itself is a high speed
65247c478bd9Sstevel@tonic-gate 		 * device.  If it is a high speed device,
65257c478bd9Sstevel@tonic-gate 		 * USB_DESCR_TYPE_SETUP_DEV_QLF should return a value,
65267c478bd9Sstevel@tonic-gate 		 * otherwise the command will fail.
65277c478bd9Sstevel@tonic-gate 		 */
65287c478bd9Sstevel@tonic-gate 		rval = usb_pipe_sync_ctrl_xfer(child_dip, ph,
65297c478bd9Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
65307c478bd9Sstevel@tonic-gate 		    USB_REQ_GET_DESCR,			/* bRequest */
65317c478bd9Sstevel@tonic-gate 		    USB_DESCR_TYPE_SETUP_DEV_QLF,	/* wValue */
65327c478bd9Sstevel@tonic-gate 		    0,					/* wIndex */
65337c478bd9Sstevel@tonic-gate 		    10,					/* wLength */
65347c478bd9Sstevel@tonic-gate 		    &pdata, USB_ATTRS_SHORT_XFER_OK,
65357c478bd9Sstevel@tonic-gate 		    &completion_reason, &cb_flags, 0);
65367c478bd9Sstevel@tonic-gate 
65377c478bd9Sstevel@tonic-gate 		if (pdata) {
65387c478bd9Sstevel@tonic-gate 			freemsg(pdata);
65397c478bd9Sstevel@tonic-gate 			pdata = NULL;
65407c478bd9Sstevel@tonic-gate 		}
65417c478bd9Sstevel@tonic-gate 
65427c478bd9Sstevel@tonic-gate 		/*
65437c478bd9Sstevel@tonic-gate 		 * USB_DESCR_TYPE_SETUP_DEV_QLF query was successful
65447c478bd9Sstevel@tonic-gate 		 * that means this is a high speed device behind a
65457c478bd9Sstevel@tonic-gate 		 * high speed root hub, but running at full speed
65467c478bd9Sstevel@tonic-gate 		 * because there is a full speed hub in the middle.
65477c478bd9Sstevel@tonic-gate 		 */
65487c478bd9Sstevel@tonic-gate 		if (rval == USB_SUCCESS) {
65497c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
65507c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
65517c478bd9Sstevel@tonic-gate 			    "Connecting a high speed device to a "
65527c478bd9Sstevel@tonic-gate 			    "non high speed hub (port %d) will result "
65537c478bd9Sstevel@tonic-gate 			    "in a loss of performance.	Please connect "
65547c478bd9Sstevel@tonic-gate 			    "the device to a high speed hub to get "
65557c478bd9Sstevel@tonic-gate 			    "the maximum performance.",
65567c478bd9Sstevel@tonic-gate 			    port);
65577c478bd9Sstevel@tonic-gate 		}
65587c478bd9Sstevel@tonic-gate 	}
65597c478bd9Sstevel@tonic-gate 
65607c478bd9Sstevel@tonic-gate 	/*
65617c478bd9Sstevel@tonic-gate 	 * Now we try to online the device by attaching a driver
65627c478bd9Sstevel@tonic-gate 	 * The following truth table illustrates the logic:-
65637c478bd9Sstevel@tonic-gate 	 * Cfgndx	Driver	Action
65647c478bd9Sstevel@tonic-gate 	 * 0		0	loop all configs for driver with full
65657c478bd9Sstevel@tonic-gate 	 *			compatible properties.
65667c478bd9Sstevel@tonic-gate 	 * 0		1	set first configuration,
65677c478bd9Sstevel@tonic-gate 	 *			compatible prop = drivername.
65687c478bd9Sstevel@tonic-gate 	 * 1		0	Set config, full compatible prop
65697c478bd9Sstevel@tonic-gate 	 * 1		1	Set config, compatible prop = drivername.
65707c478bd9Sstevel@tonic-gate 	 *
65717c478bd9Sstevel@tonic-gate 	 * Note:
65727c478bd9Sstevel@tonic-gate 	 *	cfgndx = user_conf_index
65737c478bd9Sstevel@tonic-gate 	 *	Driver = usb_preferred_driver
65747c478bd9Sstevel@tonic-gate 	 */
65757c478bd9Sstevel@tonic-gate 	if (user_conf_index == USBA_DEV_CONFIG_INDEX_UNDEFINED) {
65767c478bd9Sstevel@tonic-gate 		if (child_ud->usb_preferred_driver) {
65777c478bd9Sstevel@tonic-gate 			/*
65787c478bd9Sstevel@tonic-gate 			 * It is the job of the "preferred driver" to put the
65797c478bd9Sstevel@tonic-gate 			 * device in the desired configuration. Till then
65807c478bd9Sstevel@tonic-gate 			 * put the device in config index 0.
65817c478bd9Sstevel@tonic-gate 			 */
658235f36846Ssl147100 			if ((rval = usba_hubdi_check_power_budget(dip, child_ud,
658335f36846Ssl147100 			    USB_DEV_DEFAULT_CONFIG_INDEX)) != USB_SUCCESS) {
658435f36846Ssl147100 
658535f36846Ssl147100 				goto fail_cleanup;
658635f36846Ssl147100 			}
658735f36846Ssl147100 
65887c478bd9Sstevel@tonic-gate 			child_dip = hubd_ready_device(hubd, child_dip,
65897c478bd9Sstevel@tonic-gate 			    child_ud, USB_DEV_DEFAULT_CONFIG_INDEX);
65907c478bd9Sstevel@tonic-gate 
65917c478bd9Sstevel@tonic-gate 			/*
65927c478bd9Sstevel@tonic-gate 			 * Assign the dip before onlining to avoid race
65937c478bd9Sstevel@tonic-gate 			 * with busctl
65947c478bd9Sstevel@tonic-gate 			 */
65957c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
65967c478bd9Sstevel@tonic-gate 			hubd->h_children_dips[port] = child_dip;
65977c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
65987c478bd9Sstevel@tonic-gate 
65997c478bd9Sstevel@tonic-gate 			(void) usba_bind_driver(child_dip);
66007c478bd9Sstevel@tonic-gate 		} else {
66017c478bd9Sstevel@tonic-gate 			/*
66027c478bd9Sstevel@tonic-gate 			 * loop through all the configurations to see if we
66037c478bd9Sstevel@tonic-gate 			 * can find a driver for any one config. If not, set
66047c478bd9Sstevel@tonic-gate 			 * the device in config_index 0
66057c478bd9Sstevel@tonic-gate 			 */
66067c478bd9Sstevel@tonic-gate 			rval = USB_FAILURE;
66077c478bd9Sstevel@tonic-gate 			for (config_index = 0;
66087c478bd9Sstevel@tonic-gate 			    (config_index < usb_dev_descr.bNumConfigurations) &&
66097c478bd9Sstevel@tonic-gate 			    (rval != USB_SUCCESS); config_index++) {
66107c478bd9Sstevel@tonic-gate 
66117c478bd9Sstevel@tonic-gate 				child_dip = hubd_ready_device(hubd, child_dip,
66127c478bd9Sstevel@tonic-gate 				    child_ud, config_index);
66137c478bd9Sstevel@tonic-gate 
66147c478bd9Sstevel@tonic-gate 				/*
66157c478bd9Sstevel@tonic-gate 				 * Assign the dip before onlining to avoid race
66167c478bd9Sstevel@tonic-gate 				 * with busctl
66177c478bd9Sstevel@tonic-gate 				 */
66187c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
66197c478bd9Sstevel@tonic-gate 				hubd->h_children_dips[port] = child_dip;
66207c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
66217c478bd9Sstevel@tonic-gate 
66227c478bd9Sstevel@tonic-gate 				rval = usba_bind_driver(child_dip);
662335f36846Ssl147100 
662435f36846Ssl147100 				/*
662535f36846Ssl147100 				 * Normally power budget should be checked
662635f36846Ssl147100 				 * before device is configured. A failure in
662735f36846Ssl147100 				 * power budget checking will stop the device
662835f36846Ssl147100 				 * from being configured with current
662935f36846Ssl147100 				 * config_index and may enable the device to
663035f36846Ssl147100 				 * be configured in another configuration.
663135f36846Ssl147100 				 * This may break the user experience that a
663235f36846Ssl147100 				 * device which previously worked in config
663335f36846Ssl147100 				 * A now works in config B after power budget
663435f36846Ssl147100 				 * control is enabled. To avoid such situation,
663535f36846Ssl147100 				 * power budget checking is moved here and will
663635f36846Ssl147100 				 * fail the child creation directly if config
663735f36846Ssl147100 				 * A exceeds the power available.
663835f36846Ssl147100 				 */
663935f36846Ssl147100 				if (rval == USB_SUCCESS) {
664035f36846Ssl147100 					if ((usba_hubdi_check_power_budget(dip,
664135f36846Ssl147100 					    child_ud, config_index)) !=
664235f36846Ssl147100 					    USB_SUCCESS) {
664335f36846Ssl147100 
664435f36846Ssl147100 						goto fail_cleanup;
664535f36846Ssl147100 					}
664635f36846Ssl147100 				}
66477c478bd9Sstevel@tonic-gate 			}
66487c478bd9Sstevel@tonic-gate 			if (rval != USB_SUCCESS) {
664935f36846Ssl147100 
665035f36846Ssl147100 				if ((usba_hubdi_check_power_budget(dip,
665135f36846Ssl147100 				    child_ud, 0)) != USB_SUCCESS) {
665235f36846Ssl147100 
665335f36846Ssl147100 					goto fail_cleanup;
665435f36846Ssl147100 				}
665535f36846Ssl147100 
66567c478bd9Sstevel@tonic-gate 				child_dip = hubd_ready_device(hubd, child_dip,
66577c478bd9Sstevel@tonic-gate 				    child_ud, 0);
66587c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
66597c478bd9Sstevel@tonic-gate 				hubd->h_children_dips[port] = child_dip;
66607c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
66617c478bd9Sstevel@tonic-gate 			}
66627c478bd9Sstevel@tonic-gate 		} /* end else loop all configs */
66637c478bd9Sstevel@tonic-gate 	} else {
666435f36846Ssl147100 
666535f36846Ssl147100 		if ((usba_hubdi_check_power_budget(dip, child_ud,
666635f36846Ssl147100 		    (uint_t)user_conf_index)) != USB_SUCCESS) {
666735f36846Ssl147100 
666835f36846Ssl147100 			goto fail_cleanup;
666935f36846Ssl147100 		}
667035f36846Ssl147100 
66717c478bd9Sstevel@tonic-gate 		child_dip = hubd_ready_device(hubd, child_dip,
667235f36846Ssl147100 		    child_ud, (uint_t)user_conf_index);
66737c478bd9Sstevel@tonic-gate 
66747c478bd9Sstevel@tonic-gate 		/*
66757c478bd9Sstevel@tonic-gate 		 * Assign the dip before onlining to avoid race
66767c478bd9Sstevel@tonic-gate 		 * with busctl
66777c478bd9Sstevel@tonic-gate 		 */
66787c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
66797c478bd9Sstevel@tonic-gate 		hubd->h_children_dips[port] = child_dip;
66807c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
66817c478bd9Sstevel@tonic-gate 
66827c478bd9Sstevel@tonic-gate 		(void) usba_bind_driver(child_dip);
66837c478bd9Sstevel@tonic-gate 	}
66847c478bd9Sstevel@tonic-gate 
668535f36846Ssl147100 	usba_hubdi_decr_power_budget(dip, child_ud);
668635f36846Ssl147100 
66877c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
66887c478bd9Sstevel@tonic-gate 	if (hubd->h_usba_devices[port] == NULL) {
66897c478bd9Sstevel@tonic-gate 		hubd->h_usba_devices[port] = usba_get_usba_device(child_dip);
66907c478bd9Sstevel@tonic-gate 	} else {
66917c478bd9Sstevel@tonic-gate 		ASSERT(hubd->h_usba_devices[port] ==
66927c478bd9Sstevel@tonic-gate 		    usba_get_usba_device(child_dip));
66937c478bd9Sstevel@tonic-gate 	}
66947c478bd9Sstevel@tonic-gate 
66957c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
66967c478bd9Sstevel@tonic-gate 
66977c478bd9Sstevel@tonic-gate 
66987c478bd9Sstevel@tonic-gate fail_cleanup:
66997c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
67007c478bd9Sstevel@tonic-gate 	    "hubd_create_child: fail_cleanup");
67017c478bd9Sstevel@tonic-gate 
67027c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
67037c478bd9Sstevel@tonic-gate 	hubd->h_children_dips[port] = NULL;
67047c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
67057c478bd9Sstevel@tonic-gate 
67067c478bd9Sstevel@tonic-gate 	if (pdata) {
67077c478bd9Sstevel@tonic-gate 		freemsg(pdata);
67087c478bd9Sstevel@tonic-gate 	}
67097c478bd9Sstevel@tonic-gate 
67107c478bd9Sstevel@tonic-gate 	if (ph) {
67117c478bd9Sstevel@tonic-gate 		usb_pipe_close(child_dip, ph,
67127c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED, NULL, NULL);
67137c478bd9Sstevel@tonic-gate 	}
67147c478bd9Sstevel@tonic-gate 
6715993e3fafSRobert Mustacchi 	if (child_ud != NULL && hcd_called == B_TRUE &&
6716993e3fafSRobert Mustacchi 	    child_ud->usb_hcdi_ops->usba_hcdi_device_fini != NULL) {
6717993e3fafSRobert Mustacchi 		child_ud->usb_hcdi_ops->usba_hcdi_device_fini(child_ud,
6718993e3fafSRobert Mustacchi 		    child_ud->usb_hcd_private);
6719993e3fafSRobert Mustacchi 		child_ud->usb_hcd_private = NULL;
6720993e3fafSRobert Mustacchi 	}
6721993e3fafSRobert Mustacchi 
6722993e3fafSRobert Mustacchi 
67237c478bd9Sstevel@tonic-gate 	if (child_dip) {
67247c478bd9Sstevel@tonic-gate 		int rval = usba_destroy_child_devi(child_dip,
67257c478bd9Sstevel@tonic-gate 		    NDI_DEVI_REMOVE);
67267c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
6727d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
67287c478bd9Sstevel@tonic-gate 			    "failure to remove child node");
67297c478bd9Sstevel@tonic-gate 		}
67307c478bd9Sstevel@tonic-gate 	}
67317c478bd9Sstevel@tonic-gate 
67327c478bd9Sstevel@tonic-gate 	if (child_ud) {
67337c478bd9Sstevel@tonic-gate 		/* to make sure we free the address */
67347c478bd9Sstevel@tonic-gate 		mutex_enter(&child_ud->usb_mutex);
67357c478bd9Sstevel@tonic-gate 		child_ud->usb_addr = address;
67367c478bd9Sstevel@tonic-gate 		ASSERT(child_ud->usb_ref_count == 0);
67377c478bd9Sstevel@tonic-gate 		mutex_exit(&child_ud->usb_mutex);
67387c478bd9Sstevel@tonic-gate 
67397c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
67407c478bd9Sstevel@tonic-gate 		if (hubd->h_usba_devices[port] == NULL) {
67417c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
67427c478bd9Sstevel@tonic-gate 			usba_free_usba_device(child_ud);
67437c478bd9Sstevel@tonic-gate 		} else {
67447c478bd9Sstevel@tonic-gate 			hubd_free_usba_device(hubd, hubd->h_usba_devices[port]);
67457c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
67467c478bd9Sstevel@tonic-gate 		}
67477c478bd9Sstevel@tonic-gate 	}
67487c478bd9Sstevel@tonic-gate 
67497c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
67507c478bd9Sstevel@tonic-gate 
67517c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
67527c478bd9Sstevel@tonic-gate }
67537c478bd9Sstevel@tonic-gate 
67547c478bd9Sstevel@tonic-gate 
67557c478bd9Sstevel@tonic-gate /*
67567c478bd9Sstevel@tonic-gate  * hubd_delete_child:
67577c478bd9Sstevel@tonic-gate  *	- free usb address
67587c478bd9Sstevel@tonic-gate  *	- lookup child dips, there may be multiple on this port
67597c478bd9Sstevel@tonic-gate  *	- offline each child devi
67607c478bd9Sstevel@tonic-gate  */
67617c478bd9Sstevel@tonic-gate static int
hubd_delete_child(hubd_t * hubd,usb_port_t port,uint_t flag,boolean_t retry)67627c478bd9Sstevel@tonic-gate hubd_delete_child(hubd_t *hubd, usb_port_t port, uint_t flag, boolean_t retry)
67637c478bd9Sstevel@tonic-gate {
67647c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip;
67657c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
67667c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
67677c478bd9Sstevel@tonic-gate 
67687c478bd9Sstevel@tonic-gate 	child_dip = hubd->h_children_dips[port];
676935f36846Ssl147100 	usba_device = hubd->h_usba_devices[port];
67707c478bd9Sstevel@tonic-gate 
67717c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
67727c478bd9Sstevel@tonic-gate 	    "hubd_delete_child: port=%d, dip=0x%p usba_device=0x%p",
6773112116d8Sfb209375 	    port, (void *)child_dip, (void *)usba_device);
67747c478bd9Sstevel@tonic-gate 
67757c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
67767c478bd9Sstevel@tonic-gate 	if (child_dip) {
67777c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
67787c478bd9Sstevel@tonic-gate 		    "hubd_delete_child:\n\t"
67797c478bd9Sstevel@tonic-gate 		    "dip = 0x%p (%s) at port %d",
6780112116d8Sfb209375 		    (void *)child_dip, ddi_node_name(child_dip), port);
67817c478bd9Sstevel@tonic-gate 
678235f36846Ssl147100 		if (usba_device) {
678335f36846Ssl147100 			usba_hubdi_incr_power_budget(hubd->h_dip, usba_device);
678435f36846Ssl147100 		}
678535f36846Ssl147100 
6786993e3fafSRobert Mustacchi 
67877c478bd9Sstevel@tonic-gate 		rval = usba_destroy_child_devi(child_dip, flag);
67887c478bd9Sstevel@tonic-gate 
67897c478bd9Sstevel@tonic-gate 		if ((rval == USB_SUCCESS) && (flag & NDI_DEVI_REMOVE)) {
67907c478bd9Sstevel@tonic-gate 			/*
67917c478bd9Sstevel@tonic-gate 			 * if the child was still < DS_INITIALIZED
67927c478bd9Sstevel@tonic-gate 			 * then our bus_unconfig was not called and
67937c478bd9Sstevel@tonic-gate 			 * we have to zap the child here
67947c478bd9Sstevel@tonic-gate 			 */
67957c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
67967c478bd9Sstevel@tonic-gate 			if (hubd->h_children_dips[port] == child_dip) {
67977c478bd9Sstevel@tonic-gate 				usba_device_t *ud =
67987c478bd9Sstevel@tonic-gate 				    hubd->h_usba_devices[port];
67997c478bd9Sstevel@tonic-gate 				hubd->h_children_dips[port] = NULL;
68007c478bd9Sstevel@tonic-gate 				if (ud) {
68017c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
68027c478bd9Sstevel@tonic-gate 
68037c478bd9Sstevel@tonic-gate 					mutex_enter(&ud->usb_mutex);
68047c478bd9Sstevel@tonic-gate 					ud->usb_ref_count = 0;
68057c478bd9Sstevel@tonic-gate 					mutex_exit(&ud->usb_mutex);
68067c478bd9Sstevel@tonic-gate 
68077c478bd9Sstevel@tonic-gate 					usba_free_usba_device(ud);
68087c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
68097c478bd9Sstevel@tonic-gate 					hubd->h_usba_devices[port] = NULL;
68107c478bd9Sstevel@tonic-gate 				}
68117c478bd9Sstevel@tonic-gate 			}
68127c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
68137c478bd9Sstevel@tonic-gate 		}
68147c478bd9Sstevel@tonic-gate 	}
68157c478bd9Sstevel@tonic-gate 
68167c478bd9Sstevel@tonic-gate 	if ((rval != USB_SUCCESS) && retry) {
68177c478bd9Sstevel@tonic-gate 
68187c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(usba_device->usb_root_hub_dip);
68197c478bd9Sstevel@tonic-gate 	}
68207c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
68217c478bd9Sstevel@tonic-gate 
68227c478bd9Sstevel@tonic-gate 	return (rval);
68237c478bd9Sstevel@tonic-gate }
68247c478bd9Sstevel@tonic-gate 
68257c478bd9Sstevel@tonic-gate 
68267c478bd9Sstevel@tonic-gate /*
68277c478bd9Sstevel@tonic-gate  * hubd_free_usba_device:
68287c478bd9Sstevel@tonic-gate  *	free usb device structure unless it is associated with
68297c478bd9Sstevel@tonic-gate  *	the root hub which is handled differently
68307c478bd9Sstevel@tonic-gate  */
68317c478bd9Sstevel@tonic-gate static void
hubd_free_usba_device(hubd_t * hubd,usba_device_t * usba_device)68327c478bd9Sstevel@tonic-gate hubd_free_usba_device(hubd_t *hubd, usba_device_t *usba_device)
68337c478bd9Sstevel@tonic-gate {
68347c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
68357c478bd9Sstevel@tonic-gate 	    "hubd_free_usba_device: hubd=0x%p, usba_device=0x%p",
6836112116d8Sfb209375 	    (void *)hubd, (void *)usba_device);
68377c478bd9Sstevel@tonic-gate 
68387c478bd9Sstevel@tonic-gate 	if (usba_device && (usba_device->usb_addr != ROOT_HUB_ADDR)) {
68397c478bd9Sstevel@tonic-gate 		usb_port_t port = usba_device->usb_port;
68407c478bd9Sstevel@tonic-gate 		dev_info_t *dip = hubd->h_children_dips[port];
68417c478bd9Sstevel@tonic-gate 
68427c478bd9Sstevel@tonic-gate #ifdef DEBUG
68437c478bd9Sstevel@tonic-gate 		if (dip) {
68447c478bd9Sstevel@tonic-gate 			ASSERT(i_ddi_node_state(dip) < DS_INITIALIZED);
68457c478bd9Sstevel@tonic-gate 		}
68467c478bd9Sstevel@tonic-gate #endif
68477c478bd9Sstevel@tonic-gate 		port = usba_device->usb_port;
68487c478bd9Sstevel@tonic-gate 		hubd->h_usba_devices[port] = NULL;
68497c478bd9Sstevel@tonic-gate 
68507c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
68517c478bd9Sstevel@tonic-gate 		usba_free_usba_device(usba_device);
68527c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
68537c478bd9Sstevel@tonic-gate 	}
68547c478bd9Sstevel@tonic-gate }
68557c478bd9Sstevel@tonic-gate 
68567c478bd9Sstevel@tonic-gate 
68577c478bd9Sstevel@tonic-gate /*
68587c478bd9Sstevel@tonic-gate  * event support
68597c478bd9Sstevel@tonic-gate  *
68607c478bd9Sstevel@tonic-gate  * busctl event support
68617c478bd9Sstevel@tonic-gate  */
68627c478bd9Sstevel@tonic-gate static int
hubd_busop_get_eventcookie(dev_info_t * dip,dev_info_t * rdip,char * eventname,ddi_eventcookie_t * cookie)68637c478bd9Sstevel@tonic-gate hubd_busop_get_eventcookie(dev_info_t *dip,
68647c478bd9Sstevel@tonic-gate     dev_info_t	*rdip,
68657c478bd9Sstevel@tonic-gate     char		*eventname,
68667c478bd9Sstevel@tonic-gate     ddi_eventcookie_t *cookie)
68677c478bd9Sstevel@tonic-gate {
68687c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
68697c478bd9Sstevel@tonic-gate 
68707c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
68717c478bd9Sstevel@tonic-gate 	    "hubd_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
68727c478bd9Sstevel@tonic-gate 	    "event=%s", (void *)dip, (void *)rdip, eventname);
68737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
68747c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d)",
68757c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
68767c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
68777c478bd9Sstevel@tonic-gate 
68787c478bd9Sstevel@tonic-gate 	/* return event cookie, iblock cookie, and level */
68797c478bd9Sstevel@tonic-gate 	return (ndi_event_retrieve_cookie(hubd->h_ndi_event_hdl,
68807c478bd9Sstevel@tonic-gate 	    rdip, eventname, cookie, NDI_EVENT_NOPASS));
68817c478bd9Sstevel@tonic-gate }
68827c478bd9Sstevel@tonic-gate 
68837c478bd9Sstevel@tonic-gate 
68847c478bd9Sstevel@tonic-gate static int
hubd_busop_add_eventcall(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,ddi_event_cb_f callback,void * arg,ddi_callback_id_t * cb_id)68857c478bd9Sstevel@tonic-gate hubd_busop_add_eventcall(dev_info_t *dip,
68867c478bd9Sstevel@tonic-gate     dev_info_t	*rdip,
68877c478bd9Sstevel@tonic-gate     ddi_eventcookie_t cookie,
6888*d5ebc493SDan Cross     ddi_event_cb_f callback,
68897c478bd9Sstevel@tonic-gate     void *arg, ddi_callback_id_t *cb_id)
68907c478bd9Sstevel@tonic-gate {
68917c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
68927c478bd9Sstevel@tonic-gate 	usb_port_t port = hubd_child_dip2port(hubd, rdip);
68937c478bd9Sstevel@tonic-gate 
68947c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
68957c478bd9Sstevel@tonic-gate 	    "hubd_busop_add_eventcall: dip=0x%p, rdip=0x%p "
68967c478bd9Sstevel@tonic-gate 	    "cookie=0x%p, cb=0x%p, arg=0x%p",
68977c478bd9Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
68987c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
68997c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d, event=%s)",
69007c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
69017c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
69027c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(hubd->h_ndi_event_hdl, cookie));
69037c478bd9Sstevel@tonic-gate 
69047c478bd9Sstevel@tonic-gate 	/* Set flag on children registering events */
69057c478bd9Sstevel@tonic-gate 	switch (ndi_event_cookie_to_tag(hubd->h_ndi_event_hdl, cookie)) {
69067c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
69077c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
69087c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
69097c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
69107c478bd9Sstevel@tonic-gate 
69117c478bd9Sstevel@tonic-gate 		break;
69127c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
69137c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
69147c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
69157c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
69167c478bd9Sstevel@tonic-gate 
69177c478bd9Sstevel@tonic-gate 		break;
69187c478bd9Sstevel@tonic-gate 	default:
69197c478bd9Sstevel@tonic-gate 
69207c478bd9Sstevel@tonic-gate 		break;
69217c478bd9Sstevel@tonic-gate 	}
69227c478bd9Sstevel@tonic-gate 
69237c478bd9Sstevel@tonic-gate 	/* add callback to our event set */
69247c478bd9Sstevel@tonic-gate 	return (ndi_event_add_callback(hubd->h_ndi_event_hdl,
69257c478bd9Sstevel@tonic-gate 	    rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
69267c478bd9Sstevel@tonic-gate }
69277c478bd9Sstevel@tonic-gate 
69287c478bd9Sstevel@tonic-gate 
69297c478bd9Sstevel@tonic-gate static int
hubd_busop_remove_eventcall(dev_info_t * dip,ddi_callback_id_t cb_id)69307c478bd9Sstevel@tonic-gate hubd_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
69317c478bd9Sstevel@tonic-gate {
69327c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
69337c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *id = (ndi_event_callbacks_t *)cb_id;
69347c478bd9Sstevel@tonic-gate 
69357c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
69367c478bd9Sstevel@tonic-gate 	    "hubd_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
6937112116d8Sfb209375 	    "cookie=0x%p", (void *)dip, (void *)id->ndi_evtcb_dip,
6938112116d8Sfb209375 	    (void *)id->ndi_evtcb_cookie);
69397c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
69407c478bd9Sstevel@tonic-gate 	    "(dip=%s%d, rdip=%s%d, event=%s)",
69417c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
69427c478bd9Sstevel@tonic-gate 	    ddi_driver_name(id->ndi_evtcb_dip),
69437c478bd9Sstevel@tonic-gate 	    ddi_get_instance(id->ndi_evtcb_dip),
69447c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(hubd->h_ndi_event_hdl,
69457c478bd9Sstevel@tonic-gate 	    id->ndi_evtcb_cookie));
69467c478bd9Sstevel@tonic-gate 
69477c478bd9Sstevel@tonic-gate 	/* remove event registration from our event set */
69487c478bd9Sstevel@tonic-gate 	return (ndi_event_remove_callback(hubd->h_ndi_event_hdl, cb_id));
69497c478bd9Sstevel@tonic-gate }
69507c478bd9Sstevel@tonic-gate 
69517c478bd9Sstevel@tonic-gate 
69527c478bd9Sstevel@tonic-gate /*
69537c478bd9Sstevel@tonic-gate  * event distribution
69547c478bd9Sstevel@tonic-gate  *
69557c478bd9Sstevel@tonic-gate  * hubd_do_callback:
69567c478bd9Sstevel@tonic-gate  *	Post this event to the specified child
69577c478bd9Sstevel@tonic-gate  */
69587c478bd9Sstevel@tonic-gate static void
hubd_do_callback(hubd_t * hubd,dev_info_t * cdip,ddi_eventcookie_t cookie)69597c478bd9Sstevel@tonic-gate hubd_do_callback(hubd_t *hubd, dev_info_t *cdip, ddi_eventcookie_t cookie)
69607c478bd9Sstevel@tonic-gate {
69617c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
69627c478bd9Sstevel@tonic-gate 	    "hubd_do_callback");
69637c478bd9Sstevel@tonic-gate 
69647c478bd9Sstevel@tonic-gate 	(void) ndi_event_do_callback(hubd->h_ndi_event_hdl, cdip, cookie, NULL);
69657c478bd9Sstevel@tonic-gate }
69667c478bd9Sstevel@tonic-gate 
69677c478bd9Sstevel@tonic-gate 
69687c478bd9Sstevel@tonic-gate /*
69697c478bd9Sstevel@tonic-gate  * hubd_run_callbacks:
69707c478bd9Sstevel@tonic-gate  *	Send this event to all children
69717c478bd9Sstevel@tonic-gate  */
69727c478bd9Sstevel@tonic-gate static void
hubd_run_callbacks(hubd_t * hubd,usba_event_t type)69737c478bd9Sstevel@tonic-gate hubd_run_callbacks(hubd_t *hubd, usba_event_t type)
69747c478bd9Sstevel@tonic-gate {
69757c478bd9Sstevel@tonic-gate 	usb_port_t	port;
69767c478bd9Sstevel@tonic-gate 
69777c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
69787c478bd9Sstevel@tonic-gate 	    "hubd_run_callbacks");
69797c478bd9Sstevel@tonic-gate 
69807c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
6981993e3fafSRobert Mustacchi 	for (port = 1; port <= hubd->h_nports; port++) {
69827c478bd9Sstevel@tonic-gate 		/*
69837c478bd9Sstevel@tonic-gate 		 * the childen_dips list may have dips that have been
69847c478bd9Sstevel@tonic-gate 		 * already deallocated. we only get a post_detach notification
69857c478bd9Sstevel@tonic-gate 		 * but not a destroy notification
69867c478bd9Sstevel@tonic-gate 		 */
69877c478bd9Sstevel@tonic-gate 		if (hubd->h_children_dips[port]) {
69887c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
69897c478bd9Sstevel@tonic-gate 			hubd_post_event(hubd, port, type);
69907c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
69917c478bd9Sstevel@tonic-gate 		}
69927c478bd9Sstevel@tonic-gate 	}
69937c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
69947c478bd9Sstevel@tonic-gate }
69957c478bd9Sstevel@tonic-gate 
69967c478bd9Sstevel@tonic-gate 
69977c478bd9Sstevel@tonic-gate /*
69987c478bd9Sstevel@tonic-gate  * hubd_post_event
69997c478bd9Sstevel@tonic-gate  *	post event to a child on the port depending on the type
70007c478bd9Sstevel@tonic-gate  */
70017c478bd9Sstevel@tonic-gate static void
hubd_post_event(hubd_t * hubd,usb_port_t port,usba_event_t type)70027c478bd9Sstevel@tonic-gate hubd_post_event(hubd_t *hubd, usb_port_t port, usba_event_t type)
70037c478bd9Sstevel@tonic-gate {
70047c478bd9Sstevel@tonic-gate 	int	rval;
70057c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
70067c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
70077c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie, rm_cookie, suspend_cookie;
70087c478bd9Sstevel@tonic-gate 
70097c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
70107c478bd9Sstevel@tonic-gate 	    "hubd_post_event: port=%d event=%s", port,
70117c478bd9Sstevel@tonic-gate 	    ndi_event_tag_to_name(hubd->h_ndi_event_hdl, type));
70127c478bd9Sstevel@tonic-gate 
70137c478bd9Sstevel@tonic-gate 	cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl, type);
70147c478bd9Sstevel@tonic-gate 	rm_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
70157c478bd9Sstevel@tonic-gate 	    USBA_EVENT_TAG_HOT_REMOVAL);
70167c478bd9Sstevel@tonic-gate 	suspend_cookie = ndi_event_tag_to_cookie(hubd->h_ndi_event_hdl,
70177c478bd9Sstevel@tonic-gate 	    USBA_EVENT_TAG_PRE_SUSPEND);
70187c478bd9Sstevel@tonic-gate 
70197c478bd9Sstevel@tonic-gate 	/*
70207c478bd9Sstevel@tonic-gate 	 * Hotplug daemon may be attaching a driver that may be registering
70217c478bd9Sstevel@tonic-gate 	 * event callbacks. So it already has got the device tree lock and
70227c478bd9Sstevel@tonic-gate 	 * event handle mutex. So to prevent a deadlock while posting events,
70237c478bd9Sstevel@tonic-gate 	 * we grab and release the locks in the same order.
70247c478bd9Sstevel@tonic-gate 	 */
70257c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
70267c478bd9Sstevel@tonic-gate 	dip = hubd->h_children_dips[port];
70277c478bd9Sstevel@tonic-gate 	usba_device = hubd->h_usba_devices[port];
70287c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
70297c478bd9Sstevel@tonic-gate 
70307c478bd9Sstevel@tonic-gate 	switch (type) {
70317c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
70327c478bd9Sstevel@tonic-gate 		/* Clear the registered event flag */
70337c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
70347c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_DISCONNECT;
70357c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
70367c478bd9Sstevel@tonic-gate 
70377c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
70387c478bd9Sstevel@tonic-gate 		usba_persistent_pipe_close(usba_device);
70397c478bd9Sstevel@tonic-gate 
70407c478bd9Sstevel@tonic-gate 		/*
70417c478bd9Sstevel@tonic-gate 		 * Mark the dip for deletion only after the driver has
70427c478bd9Sstevel@tonic-gate 		 * seen the disconnect event to prevent cleanup thread
70437c478bd9Sstevel@tonic-gate 		 * from stepping in between.
70447c478bd9Sstevel@tonic-gate 		 */
704516747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
70467c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_REMOVED(dip);
704716747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
70487c478bd9Sstevel@tonic-gate 
70497c478bd9Sstevel@tonic-gate 		break;
70507c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
70517c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
70527c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] &= ~HUBD_CHILD_EVENT_PRESUSPEND;
70537c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
70547c478bd9Sstevel@tonic-gate 
70557c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
70567c478bd9Sstevel@tonic-gate 		/*
70577c478bd9Sstevel@tonic-gate 		 * persistent pipe close for this event is taken care by the
70587c478bd9Sstevel@tonic-gate 		 * caller after verfying that all children can suspend
70597c478bd9Sstevel@tonic-gate 		 */
70607c478bd9Sstevel@tonic-gate 
70617c478bd9Sstevel@tonic-gate 		break;
70627c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_INSERTION:
70637c478bd9Sstevel@tonic-gate 		/*
70647c478bd9Sstevel@tonic-gate 		 * Check if this child has missed the disconnect event before
70657c478bd9Sstevel@tonic-gate 		 * it registered for event callbacks
70667c478bd9Sstevel@tonic-gate 		 */
70677c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
70687c478bd9Sstevel@tonic-gate 		if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_DISCONNECT) {
70697c478bd9Sstevel@tonic-gate 			/* clear the flag and post disconnect event */
70707c478bd9Sstevel@tonic-gate 			hubd->h_child_events[port] &=
70717c478bd9Sstevel@tonic-gate 			    ~HUBD_CHILD_EVENT_DISCONNECT;
70727c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
70737c478bd9Sstevel@tonic-gate 			hubd_do_callback(hubd, dip, rm_cookie);
70747c478bd9Sstevel@tonic-gate 			usba_persistent_pipe_close(usba_device);
70757c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
70767c478bd9Sstevel@tonic-gate 		}
70777c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
70787c478bd9Sstevel@tonic-gate 
70797c478bd9Sstevel@tonic-gate 		/*
70807c478bd9Sstevel@tonic-gate 		 * Mark the dip as reinserted to prevent cleanup thread
70817c478bd9Sstevel@tonic-gate 		 * from stepping in.
70827c478bd9Sstevel@tonic-gate 		 */
708316747f41Scth 		mutex_enter(&(DEVI(dip)->devi_lock));
70847c478bd9Sstevel@tonic-gate 		DEVI_SET_DEVICE_REINSERTED(dip);
708516747f41Scth 		mutex_exit(&(DEVI(dip)->devi_lock));
70867c478bd9Sstevel@tonic-gate 
70877c478bd9Sstevel@tonic-gate 		rval = usba_persistent_pipe_open(usba_device);
70887c478bd9Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
7089d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
70907c478bd9Sstevel@tonic-gate 			    hubd->h_log_handle,
70917c478bd9Sstevel@tonic-gate 			    "failed to reopen all pipes on reconnect");
70927c478bd9Sstevel@tonic-gate 		}
70937c478bd9Sstevel@tonic-gate 
70947c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
70957c478bd9Sstevel@tonic-gate 
70967c478bd9Sstevel@tonic-gate 		/*
70977c478bd9Sstevel@tonic-gate 		 * We might see a connect event only if hotplug thread for
70987c478bd9Sstevel@tonic-gate 		 * disconnect event don't run in time.
70997c478bd9Sstevel@tonic-gate 		 * Set the flag again, so we don't miss posting a
71007c478bd9Sstevel@tonic-gate 		 * disconnect event.
71017c478bd9Sstevel@tonic-gate 		 */
71027c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
71037c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_DISCONNECT;
71047c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
71057c478bd9Sstevel@tonic-gate 
71067c478bd9Sstevel@tonic-gate 		break;
71077c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_POST_RESUME:
71087c478bd9Sstevel@tonic-gate 		/*
71097c478bd9Sstevel@tonic-gate 		 * Check if this child has missed the pre-suspend event before
71107c478bd9Sstevel@tonic-gate 		 * it registered for event callbacks
71117c478bd9Sstevel@tonic-gate 		 */
71127c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
71137c478bd9Sstevel@tonic-gate 		if (hubd->h_child_events[port] & HUBD_CHILD_EVENT_PRESUSPEND) {
71147c478bd9Sstevel@tonic-gate 			/* clear the flag and post pre_suspend event */
71157c478bd9Sstevel@tonic-gate 			hubd->h_port_state[port] &=
71167c478bd9Sstevel@tonic-gate 			    ~HUBD_CHILD_EVENT_PRESUSPEND;
71177c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
71187c478bd9Sstevel@tonic-gate 			hubd_do_callback(hubd, dip, suspend_cookie);
71197c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
71207c478bd9Sstevel@tonic-gate 		}
71217c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
71227c478bd9Sstevel@tonic-gate 
71237c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
71247c478bd9Sstevel@tonic-gate 		usba_device->usb_no_cpr = 0;
71257c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
71267c478bd9Sstevel@tonic-gate 
71277c478bd9Sstevel@tonic-gate 		/*
71287c478bd9Sstevel@tonic-gate 		 * Since the pipe has already been opened by hub
71297c478bd9Sstevel@tonic-gate 		 * at DDI_RESUME time, there is no need for a
71307c478bd9Sstevel@tonic-gate 		 * persistent pipe open
71317c478bd9Sstevel@tonic-gate 		 */
71327c478bd9Sstevel@tonic-gate 		hubd_do_callback(hubd, dip, cookie);
71337c478bd9Sstevel@tonic-gate 
71347c478bd9Sstevel@tonic-gate 		/*
71357c478bd9Sstevel@tonic-gate 		 * Set the flag again, so we don't miss posting a
71367c478bd9Sstevel@tonic-gate 		 * pre-suspend event. This enforces a tighter
71377c478bd9Sstevel@tonic-gate 		 * dev_state model.
71387c478bd9Sstevel@tonic-gate 		 */
71397c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
71407c478bd9Sstevel@tonic-gate 		hubd->h_child_events[port] |= HUBD_CHILD_EVENT_PRESUSPEND;
71417c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
71427c478bd9Sstevel@tonic-gate 		break;
71437c478bd9Sstevel@tonic-gate 	}
71447c478bd9Sstevel@tonic-gate }
71457c478bd9Sstevel@tonic-gate 
71467c478bd9Sstevel@tonic-gate 
71477c478bd9Sstevel@tonic-gate /*
71487c478bd9Sstevel@tonic-gate  * handling of events coming from above
71497c478bd9Sstevel@tonic-gate  */
71507c478bd9Sstevel@tonic-gate static int
hubd_disconnect_event_cb(dev_info_t * dip)71517c478bd9Sstevel@tonic-gate hubd_disconnect_event_cb(dev_info_t *dip)
71527c478bd9Sstevel@tonic-gate {
71537c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = (hubd_t *)hubd_get_soft_state(dip);
71547c478bd9Sstevel@tonic-gate 	usb_port_t	port, nports;
71557c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_dev;
71567c478bd9Sstevel@tonic-gate 	usba_event_t	tag = USBA_EVENT_TAG_HOT_REMOVAL;
71577c478bd9Sstevel@tonic-gate 
71587c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
71597c478bd9Sstevel@tonic-gate 	    "hubd_disconnect_event_cb: tag=%d", tag);
71607c478bd9Sstevel@tonic-gate 
71613fe80ca4SDan Cross 	ndi_devi_enter(dip);
71627c478bd9Sstevel@tonic-gate 
71637c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
71647c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
71657c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
71667c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
71677c478bd9Sstevel@tonic-gate 		hubd->h_dev_state = USB_DEV_DISCONNECTED;
71687c478bd9Sstevel@tonic-gate 		/* stop polling on the interrupt pipe */
71697c478bd9Sstevel@tonic-gate 		hubd_stop_polling(hubd);
71707c478bd9Sstevel@tonic-gate 
71717c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
71727c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
71737c478bd9Sstevel@tonic-gate 		/* we remain in this state */
71747c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
71757c478bd9Sstevel@tonic-gate 		hubd_run_callbacks(hubd, tag);
71767c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
71777c478bd9Sstevel@tonic-gate 
71787c478bd9Sstevel@tonic-gate 		/* close all the open pipes of our children */
7179993e3fafSRobert Mustacchi 		nports = hubd->h_nports;
71807c478bd9Sstevel@tonic-gate 		for (port = 1; port <= nports; port++) {
71817c478bd9Sstevel@tonic-gate 			usba_dev = hubd->h_usba_devices[port];
71827c478bd9Sstevel@tonic-gate 			if (usba_dev != NULL) {
71837c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
71847c478bd9Sstevel@tonic-gate 				usba_persistent_pipe_close(usba_dev);
71857c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
71867c478bd9Sstevel@tonic-gate 			}
71877c478bd9Sstevel@tonic-gate 		}
71887c478bd9Sstevel@tonic-gate 
71897c478bd9Sstevel@tonic-gate 		break;
71907c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
71917c478bd9Sstevel@tonic-gate 		/* avoid passing multiple disconnects to children */
71927c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
71937c478bd9Sstevel@tonic-gate 		    "hubd_disconnect_event_cb: Already disconnected");
71947c478bd9Sstevel@tonic-gate 
71957c478bd9Sstevel@tonic-gate 		break;
71967c478bd9Sstevel@tonic-gate 	default:
71977c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
71987c478bd9Sstevel@tonic-gate 		    "hubd_disconnect_event_cb: Illegal devstate=%d",
71997c478bd9Sstevel@tonic-gate 		    hubd->h_dev_state);
72007c478bd9Sstevel@tonic-gate 
72017c478bd9Sstevel@tonic-gate 		break;
72027c478bd9Sstevel@tonic-gate 	}
72037c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
72047c478bd9Sstevel@tonic-gate 
72053fe80ca4SDan Cross 	ndi_devi_exit(dip);
72067c478bd9Sstevel@tonic-gate 
72077c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
72087c478bd9Sstevel@tonic-gate }
72097c478bd9Sstevel@tonic-gate 
72107c478bd9Sstevel@tonic-gate 
72117c478bd9Sstevel@tonic-gate static int
hubd_reconnect_event_cb(dev_info_t * dip)72127c478bd9Sstevel@tonic-gate hubd_reconnect_event_cb(dev_info_t *dip)
72137c478bd9Sstevel@tonic-gate {
72143fe80ca4SDan Cross 	int	rval;
72157c478bd9Sstevel@tonic-gate 
72163fe80ca4SDan Cross 	ndi_devi_enter(dip);
72177c478bd9Sstevel@tonic-gate 	rval = hubd_restore_state_cb(dip);
72183fe80ca4SDan Cross 	ndi_devi_exit(dip);
72197c478bd9Sstevel@tonic-gate 
72207c478bd9Sstevel@tonic-gate 	return (rval);
72217c478bd9Sstevel@tonic-gate }
72227c478bd9Sstevel@tonic-gate 
72237c478bd9Sstevel@tonic-gate 
72247c478bd9Sstevel@tonic-gate /*
72257c478bd9Sstevel@tonic-gate  * hubd_pre_suspend_event_cb
72267c478bd9Sstevel@tonic-gate  *	propogate event for binary compatibility of old drivers
72277c478bd9Sstevel@tonic-gate  */
72287c478bd9Sstevel@tonic-gate static int
hubd_pre_suspend_event_cb(dev_info_t * dip)72297c478bd9Sstevel@tonic-gate hubd_pre_suspend_event_cb(dev_info_t *dip)
72307c478bd9Sstevel@tonic-gate {
72317c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
72327c478bd9Sstevel@tonic-gate 
72337c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
72347c478bd9Sstevel@tonic-gate 	    "hubd_pre_suspend_event_cb");
72357c478bd9Sstevel@tonic-gate 
72367c478bd9Sstevel@tonic-gate 	/* disable hotplug thread */
72377c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
72387c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread++;
72397c478bd9Sstevel@tonic-gate 	hubd_stop_polling(hubd);
72407c478bd9Sstevel@tonic-gate 
72417c478bd9Sstevel@tonic-gate 	/* keep PM out till we see a cpr resume */
72427c478bd9Sstevel@tonic-gate 	(void) hubd_pm_busy_component(hubd, hubd->h_dip, 0);
72437c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
72447c478bd9Sstevel@tonic-gate 
72453fe80ca4SDan Cross 	ndi_devi_enter(dip);
72467c478bd9Sstevel@tonic-gate 	hubd_run_callbacks(hubd, USBA_EVENT_TAG_PRE_SUSPEND);
72473fe80ca4SDan Cross 	ndi_devi_exit(dip);
72487c478bd9Sstevel@tonic-gate 
72497c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
72507c478bd9Sstevel@tonic-gate }
72517c478bd9Sstevel@tonic-gate 
72527c478bd9Sstevel@tonic-gate 
72537c478bd9Sstevel@tonic-gate /*
72547c478bd9Sstevel@tonic-gate  * hubd_post_resume_event_cb
72557c478bd9Sstevel@tonic-gate  *	propogate event for binary compatibility of old drivers
72567c478bd9Sstevel@tonic-gate  */
72577c478bd9Sstevel@tonic-gate static int
hubd_post_resume_event_cb(dev_info_t * dip)72587c478bd9Sstevel@tonic-gate hubd_post_resume_event_cb(dev_info_t *dip)
72597c478bd9Sstevel@tonic-gate {
72607c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
72617c478bd9Sstevel@tonic-gate 
72627c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
72637c478bd9Sstevel@tonic-gate 	    "hubd_post_resume_event_cb");
72647c478bd9Sstevel@tonic-gate 
72653fe80ca4SDan Cross 	ndi_devi_enter(dip);
72667c478bd9Sstevel@tonic-gate 	hubd_run_callbacks(hubd, USBA_EVENT_TAG_POST_RESUME);
72673fe80ca4SDan Cross 	ndi_devi_exit(dip);
72687c478bd9Sstevel@tonic-gate 
72697c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
72707c478bd9Sstevel@tonic-gate 
72717c478bd9Sstevel@tonic-gate 	/* enable PM */
72727c478bd9Sstevel@tonic-gate 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
72737c478bd9Sstevel@tonic-gate 
72747c478bd9Sstevel@tonic-gate 	/* allow hotplug thread */
72757c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
72767c478bd9Sstevel@tonic-gate 
72777c478bd9Sstevel@tonic-gate 	/* start polling */
72787c478bd9Sstevel@tonic-gate 	hubd_start_polling(hubd, 0);
72797c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
72807c478bd9Sstevel@tonic-gate 
72817c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
72827c478bd9Sstevel@tonic-gate }
72837c478bd9Sstevel@tonic-gate 
72847c478bd9Sstevel@tonic-gate 
72857c478bd9Sstevel@tonic-gate /*
72867c478bd9Sstevel@tonic-gate  * hubd_cpr_suspend
72877c478bd9Sstevel@tonic-gate  *	save the current state of the driver/device
72887c478bd9Sstevel@tonic-gate  */
72897c478bd9Sstevel@tonic-gate static int
hubd_cpr_suspend(hubd_t * hubd)72907c478bd9Sstevel@tonic-gate hubd_cpr_suspend(hubd_t *hubd)
72917c478bd9Sstevel@tonic-gate {
72927c478bd9Sstevel@tonic-gate 	usb_port_t	port, nports;
72937c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_dev;
72947c478bd9Sstevel@tonic-gate 	uchar_t		no_cpr = 0;
72957c478bd9Sstevel@tonic-gate 	int		rval = USB_FAILURE;
72967c478bd9Sstevel@tonic-gate 
72977c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
72987c478bd9Sstevel@tonic-gate 	    "hubd_cpr_suspend: Begin");
72997c478bd9Sstevel@tonic-gate 
73007c478bd9Sstevel@tonic-gate 	/* Make sure device is powered up to save state. */
73017c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
73027c478bd9Sstevel@tonic-gate 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
73037c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
73047c478bd9Sstevel@tonic-gate 
73057c478bd9Sstevel@tonic-gate 	/* bring the device to full power */
73067c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
73077c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
73087c478bd9Sstevel@tonic-gate 
73097c478bd9Sstevel@tonic-gate 	switch (hubd->h_dev_state) {
73107c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
73117c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
73127c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
73137c478bd9Sstevel@tonic-gate 		/* find out if all our children have been quiesced */
7314993e3fafSRobert Mustacchi 		nports = hubd->h_nports;
73157c478bd9Sstevel@tonic-gate 		for (port = 1; (no_cpr == 0) && (port <= nports); port++) {
73167c478bd9Sstevel@tonic-gate 			usba_dev = hubd->h_usba_devices[port];
73177c478bd9Sstevel@tonic-gate 			if (usba_dev != NULL) {
73187c478bd9Sstevel@tonic-gate 				mutex_enter(&usba_dev->usb_mutex);
73197c478bd9Sstevel@tonic-gate 				no_cpr += usba_dev->usb_no_cpr;
73207c478bd9Sstevel@tonic-gate 				mutex_exit(&usba_dev->usb_mutex);
73217c478bd9Sstevel@tonic-gate 			}
73227c478bd9Sstevel@tonic-gate 		}
73237c478bd9Sstevel@tonic-gate 		if (no_cpr > 0) {
73247c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
73257c478bd9Sstevel@tonic-gate 			    "Children busy - can't checkpoint");
73267c478bd9Sstevel@tonic-gate 			/* remain in same state to fail checkpoint */
73277c478bd9Sstevel@tonic-gate 
73287c478bd9Sstevel@tonic-gate 			break;
73297c478bd9Sstevel@tonic-gate 		} else {
73307c478bd9Sstevel@tonic-gate 			/*
73317c478bd9Sstevel@tonic-gate 			 * do not suspend if our hotplug thread
73327c478bd9Sstevel@tonic-gate 			 * or the deathrow thread is active
73337c478bd9Sstevel@tonic-gate 			 */
73347c478bd9Sstevel@tonic-gate 			if ((hubd->h_hotplug_thread > 1) ||
73357c478bd9Sstevel@tonic-gate 			    (hubd->h_cleanup_active == B_TRUE)) {
73367c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG,
73377c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
73387c478bd9Sstevel@tonic-gate 				    "hotplug thread active  - can't cpr");
73397c478bd9Sstevel@tonic-gate 				/* remain in same state to fail checkpoint */
73407c478bd9Sstevel@tonic-gate 
73417c478bd9Sstevel@tonic-gate 				break;
73427c478bd9Sstevel@tonic-gate 			}
73437c478bd9Sstevel@tonic-gate 
73447c478bd9Sstevel@tonic-gate 			/* quiesce ourselves now */
73457c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
73467c478bd9Sstevel@tonic-gate 
73477c478bd9Sstevel@tonic-gate 			/* close all the open pipes of our children */
73487c478bd9Sstevel@tonic-gate 			for (port = 1; port <= nports; port++) {
73497c478bd9Sstevel@tonic-gate 				usba_dev = hubd->h_usba_devices[port];
73507c478bd9Sstevel@tonic-gate 				if (usba_dev != NULL) {
73517c478bd9Sstevel@tonic-gate 					mutex_exit(HUBD_MUTEX(hubd));
73527c478bd9Sstevel@tonic-gate 					usba_persistent_pipe_close(usba_dev);
73536f6c7d2bSVincent Wang 					if (hubd_suspend_port(hubd, port)) {
73546f6c7d2bSVincent Wang 						USB_DPRINTF_L0(
73556f6c7d2bSVincent Wang 						    DPRINT_MASK_HOTPLUG,
73566f6c7d2bSVincent Wang 						    hubd->h_log_handle,
73576f6c7d2bSVincent Wang 						    "suspending port %d failed",
73586f6c7d2bSVincent Wang 						    port);
73596f6c7d2bSVincent Wang 					}
73607c478bd9Sstevel@tonic-gate 					mutex_enter(HUBD_MUTEX(hubd));
73617c478bd9Sstevel@tonic-gate 				}
73626f6c7d2bSVincent Wang 
73637c478bd9Sstevel@tonic-gate 			}
73646f6c7d2bSVincent Wang 			hubd->h_dev_state = USB_DEV_SUSPENDED;
73657c478bd9Sstevel@tonic-gate 
73667c478bd9Sstevel@tonic-gate 			/*
73677c478bd9Sstevel@tonic-gate 			 * if we are the root hub, we close our pipes
73687c478bd9Sstevel@tonic-gate 			 * ourselves.
73697c478bd9Sstevel@tonic-gate 			 */
73707c478bd9Sstevel@tonic-gate 			if (usba_is_root_hub(hubd->h_dip)) {
73717c478bd9Sstevel@tonic-gate 				mutex_exit(HUBD_MUTEX(hubd));
73727c478bd9Sstevel@tonic-gate 				usba_persistent_pipe_close(
73737c478bd9Sstevel@tonic-gate 				    usba_get_usba_device(hubd->h_dip));
73747c478bd9Sstevel@tonic-gate 				mutex_enter(HUBD_MUTEX(hubd));
73757c478bd9Sstevel@tonic-gate 			}
73767c478bd9Sstevel@tonic-gate 			rval = USB_SUCCESS;
73777c478bd9Sstevel@tonic-gate 
73787c478bd9Sstevel@tonic-gate 			break;
73797c478bd9Sstevel@tonic-gate 		}
73807c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
73817c478bd9Sstevel@tonic-gate 	default:
73827c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
73837c478bd9Sstevel@tonic-gate 		    "hubd_cpr_suspend: Illegal dev state=%d",
73847c478bd9Sstevel@tonic-gate 		    hubd->h_dev_state);
73857c478bd9Sstevel@tonic-gate 
73867c478bd9Sstevel@tonic-gate 		break;
73877c478bd9Sstevel@tonic-gate 	}
73887c478bd9Sstevel@tonic-gate 
73897c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, hubd->h_dip, 0);
73907c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
73917c478bd9Sstevel@tonic-gate 
73927c478bd9Sstevel@tonic-gate 	return (rval);
73937c478bd9Sstevel@tonic-gate }
73947c478bd9Sstevel@tonic-gate 
73957c478bd9Sstevel@tonic-gate static void
hubd_cpr_resume(dev_info_t * dip)73967c478bd9Sstevel@tonic-gate hubd_cpr_resume(dev_info_t *dip)
73977c478bd9Sstevel@tonic-gate {
73983fe80ca4SDan Cross 	int	rval;
73997c478bd9Sstevel@tonic-gate 
74003fe80ca4SDan Cross 	ndi_devi_enter(dip);
74017c478bd9Sstevel@tonic-gate 	/*
74027c478bd9Sstevel@tonic-gate 	 * if we are the root hub, we open our pipes
74037c478bd9Sstevel@tonic-gate 	 * ourselves.
74047c478bd9Sstevel@tonic-gate 	 */
74057c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
74067c478bd9Sstevel@tonic-gate 		rval = usba_persistent_pipe_open(
74077c478bd9Sstevel@tonic-gate 		    usba_get_usba_device(dip));
74087c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
74097c478bd9Sstevel@tonic-gate 	}
74107c478bd9Sstevel@tonic-gate 	(void) hubd_restore_state_cb(dip);
74113fe80ca4SDan Cross 	ndi_devi_exit(dip);
74127c478bd9Sstevel@tonic-gate }
74137c478bd9Sstevel@tonic-gate 
74147c478bd9Sstevel@tonic-gate 
74157c478bd9Sstevel@tonic-gate /*
74167c478bd9Sstevel@tonic-gate  * hubd_restore_state_cb
74177c478bd9Sstevel@tonic-gate  *	Event callback to restore device state
74187c478bd9Sstevel@tonic-gate  */
74197c478bd9Sstevel@tonic-gate static int
hubd_restore_state_cb(dev_info_t * dip)74207c478bd9Sstevel@tonic-gate hubd_restore_state_cb(dev_info_t *dip)
74217c478bd9Sstevel@tonic-gate {
74227c478bd9Sstevel@tonic-gate 	hubd_t	*hubd = (hubd_t *)hubd_get_soft_state(dip);
74237c478bd9Sstevel@tonic-gate 
74247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
74257c478bd9Sstevel@tonic-gate 	    "hubd_restore_state_cb: Begin");
74267c478bd9Sstevel@tonic-gate 
74277c478bd9Sstevel@tonic-gate 	/* restore the state of this device */
74287c478bd9Sstevel@tonic-gate 	hubd_restore_device_state(dip, hubd);
74297c478bd9Sstevel@tonic-gate 
74307c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
74317c478bd9Sstevel@tonic-gate }
74327c478bd9Sstevel@tonic-gate 
74337c478bd9Sstevel@tonic-gate 
74347c478bd9Sstevel@tonic-gate /*
74357c478bd9Sstevel@tonic-gate  * registering for events
74367c478bd9Sstevel@tonic-gate  */
74377c478bd9Sstevel@tonic-gate static int
hubd_register_events(hubd_t * hubd)74387c478bd9Sstevel@tonic-gate hubd_register_events(hubd_t *hubd)
74397c478bd9Sstevel@tonic-gate {
74407c478bd9Sstevel@tonic-gate 	int		rval = USB_SUCCESS;
74417c478bd9Sstevel@tonic-gate 
74427c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(hubd->h_dip)) {
74437c478bd9Sstevel@tonic-gate 		hubd_register_cpr_callback(hubd);
74447c478bd9Sstevel@tonic-gate 	} else {
74457c478bd9Sstevel@tonic-gate 		rval = usb_register_event_cbs(hubd->h_dip, &hubd_events, 0);
74467c478bd9Sstevel@tonic-gate 	}
74477c478bd9Sstevel@tonic-gate 
74487c478bd9Sstevel@tonic-gate 	return (rval);
74497c478bd9Sstevel@tonic-gate }
74507c478bd9Sstevel@tonic-gate 
74517c478bd9Sstevel@tonic-gate 
74527c478bd9Sstevel@tonic-gate /*
74537c478bd9Sstevel@tonic-gate  * hubd cpr callback related functions
74547c478bd9Sstevel@tonic-gate  *
74557c478bd9Sstevel@tonic-gate  * hubd_cpr_post_user_callb:
74567c478bd9Sstevel@tonic-gate  *	This function is called during checkpoint & resume -
74577c478bd9Sstevel@tonic-gate  *		1. after user threads are stopped during checkpoint
74587c478bd9Sstevel@tonic-gate  *		2. after kernel threads are resumed during resume
74597c478bd9Sstevel@tonic-gate  */
74607c478bd9Sstevel@tonic-gate /* ARGSUSED */
74617c478bd9Sstevel@tonic-gate static boolean_t
hubd_cpr_post_user_callb(void * arg,int code)74627c478bd9Sstevel@tonic-gate hubd_cpr_post_user_callb(void *arg, int code)
74637c478bd9Sstevel@tonic-gate {
74647c478bd9Sstevel@tonic-gate 	hubd_cpr_t	*cpr_cb = (hubd_cpr_t *)arg;
74657c478bd9Sstevel@tonic-gate 	hubd_t		*hubd = cpr_cb->statep;
74667c478bd9Sstevel@tonic-gate 	int		retry = 0;
74677c478bd9Sstevel@tonic-gate 
74687c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
74697c478bd9Sstevel@tonic-gate 	    "hubd_cpr_post_user_callb");
74707c478bd9Sstevel@tonic-gate 
74717c478bd9Sstevel@tonic-gate 	switch (code) {
74727c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_CHKPT:
74737c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
74747c478bd9Sstevel@tonic-gate 		    "hubd_cpr_post_user_callb: CB_CODE_CPR_CHKPT");
74757c478bd9Sstevel@tonic-gate 
74767c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
74777c478bd9Sstevel@tonic-gate 
74787c478bd9Sstevel@tonic-gate 		/* turn off deathrow thread */
74797c478bd9Sstevel@tonic-gate 		hubd->h_cleanup_enabled = B_FALSE;
74807c478bd9Sstevel@tonic-gate 
74817c478bd9Sstevel@tonic-gate 		/* give up if deathrow thread doesn't exit */
74827c478bd9Sstevel@tonic-gate 		while ((hubd->h_cleanup_active == B_TRUE) && (retry++ < 3)) {
74837c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
74847c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(hubd_dip_cleanup_delay));
74857c478bd9Sstevel@tonic-gate 
74867c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_EVENTS, hubd->h_log_handle,
74877c478bd9Sstevel@tonic-gate 			    "hubd_cpr_post_user_callb, waiting for "
74887c478bd9Sstevel@tonic-gate 			    "deathrow thread to exit");
74897c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
74907c478bd9Sstevel@tonic-gate 		}
74917c478bd9Sstevel@tonic-gate 
74927c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
74937c478bd9Sstevel@tonic-gate 
74947c478bd9Sstevel@tonic-gate 		/* save the state of the device */
74957c478bd9Sstevel@tonic-gate 		(void) hubd_pre_suspend_event_cb(hubd->h_dip);
74967c478bd9Sstevel@tonic-gate 
74977c478bd9Sstevel@tonic-gate 		return (B_TRUE);
74987c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_RESUME:
74997c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_EVENTS, hubd->h_log_handle,
75007c478bd9Sstevel@tonic-gate 		    "hubd_cpr_post_user_callb: CB_CODE_CPR_RESUME");
75017c478bd9Sstevel@tonic-gate 
75027c478bd9Sstevel@tonic-gate 		/* restore the state of the device */
75037c478bd9Sstevel@tonic-gate 		(void) hubd_post_resume_event_cb(hubd->h_dip);
75047c478bd9Sstevel@tonic-gate 
75057c478bd9Sstevel@tonic-gate 		/* turn on deathrow thread */
75067c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
75077c478bd9Sstevel@tonic-gate 		hubd->h_cleanup_enabled = B_TRUE;
75087c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
75097c478bd9Sstevel@tonic-gate 
75107c478bd9Sstevel@tonic-gate 		hubd_schedule_cleanup(hubd->h_usba_device->usb_root_hub_dip);
75117c478bd9Sstevel@tonic-gate 
75127c478bd9Sstevel@tonic-gate 		return (B_TRUE);
75137c478bd9Sstevel@tonic-gate 	default:
75147c478bd9Sstevel@tonic-gate 
75157c478bd9Sstevel@tonic-gate 		return (B_FALSE);
75167c478bd9Sstevel@tonic-gate 	}
75177c478bd9Sstevel@tonic-gate 
75187c478bd9Sstevel@tonic-gate }
75197c478bd9Sstevel@tonic-gate 
75207c478bd9Sstevel@tonic-gate 
75217c478bd9Sstevel@tonic-gate /* register callback with cpr framework */
75227c478bd9Sstevel@tonic-gate void
hubd_register_cpr_callback(hubd_t * hubd)75237c478bd9Sstevel@tonic-gate hubd_register_cpr_callback(hubd_t *hubd)
75247c478bd9Sstevel@tonic-gate {
75257c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
75267c478bd9Sstevel@tonic-gate 	    "hubd_register_cpr_callback");
75277c478bd9Sstevel@tonic-gate 
75287c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
75297c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb =
75307c478bd9Sstevel@tonic-gate 	    (hubd_cpr_t *)kmem_zalloc(sizeof (hubd_cpr_t), KM_SLEEP);
75317c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
75327c478bd9Sstevel@tonic-gate 	mutex_init(&hubd->h_cpr_cb->lockp, NULL, MUTEX_DRIVER,
75337c478bd9Sstevel@tonic-gate 	    hubd->h_dev_data->dev_iblock_cookie);
75347c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->statep = hubd;
75357c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->cpr.cc_lockp = &hubd->h_cpr_cb->lockp;
75367c478bd9Sstevel@tonic-gate 	hubd->h_cpr_cb->cpr.cc_id = callb_add(hubd_cpr_post_user_callb,
75377c478bd9Sstevel@tonic-gate 	    (void *)hubd->h_cpr_cb, CB_CL_CPR_POST_USER, "hubd");
75387c478bd9Sstevel@tonic-gate }
75397c478bd9Sstevel@tonic-gate 
75407c478bd9Sstevel@tonic-gate 
75417c478bd9Sstevel@tonic-gate /* unregister callback with cpr framework */
75427c478bd9Sstevel@tonic-gate void
hubd_unregister_cpr_callback(hubd_t * hubd)75437c478bd9Sstevel@tonic-gate hubd_unregister_cpr_callback(hubd_t *hubd)
75447c478bd9Sstevel@tonic-gate {
75457c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, hubd->h_log_handle,
75467c478bd9Sstevel@tonic-gate 	    "hubd_unregister_cpr_callback");
75477c478bd9Sstevel@tonic-gate 
75487c478bd9Sstevel@tonic-gate 	if (hubd->h_cpr_cb) {
75497c478bd9Sstevel@tonic-gate 		(void) callb_delete(hubd->h_cpr_cb->cpr.cc_id);
75507c478bd9Sstevel@tonic-gate 		mutex_destroy(&hubd->h_cpr_cb->lockp);
75517c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
75527c478bd9Sstevel@tonic-gate 		kmem_free(hubd->h_cpr_cb, sizeof (hubd_cpr_t));
75537c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
75547c478bd9Sstevel@tonic-gate 	}
75557c478bd9Sstevel@tonic-gate }
75567c478bd9Sstevel@tonic-gate 
75577c478bd9Sstevel@tonic-gate 
75587c478bd9Sstevel@tonic-gate /*
75597c478bd9Sstevel@tonic-gate  * Power management
75607c478bd9Sstevel@tonic-gate  *
75617c478bd9Sstevel@tonic-gate  * create the pm components required for power management
75627c478bd9Sstevel@tonic-gate  */
75637c478bd9Sstevel@tonic-gate static void
hubd_create_pm_components(dev_info_t * dip,hubd_t * hubd)75647c478bd9Sstevel@tonic-gate hubd_create_pm_components(dev_info_t *dip, hubd_t *hubd)
75657c478bd9Sstevel@tonic-gate {
75667c478bd9Sstevel@tonic-gate 	hub_power_t	*hubpm;
75677c478bd9Sstevel@tonic-gate 
75687c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
75697c478bd9Sstevel@tonic-gate 	    "hubd_create_pm_components: Begin");
75707c478bd9Sstevel@tonic-gate 
75717c478bd9Sstevel@tonic-gate 	/* Allocate the state structure */
75727c478bd9Sstevel@tonic-gate 	hubpm = kmem_zalloc(sizeof (hub_power_t), KM_SLEEP);
75737c478bd9Sstevel@tonic-gate 
75747c478bd9Sstevel@tonic-gate 	hubd->h_hubpm = hubpm;
75757c478bd9Sstevel@tonic-gate 	hubpm->hubp_hubd = hubd;
75767c478bd9Sstevel@tonic-gate 	hubpm->hubp_pm_capabilities = 0;
75777c478bd9Sstevel@tonic-gate 	hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
7578e5815e7aSJosef 'Jeff' Sipek 	hubpm->hubp_time_at_full_power = gethrtime();
7579e5815e7aSJosef 'Jeff' Sipek 	hubpm->hubp_min_pm_threshold = hubdi_min_pm_threshold * NANOSEC;
75807c478bd9Sstevel@tonic-gate 
75817c478bd9Sstevel@tonic-gate 	/* alloc memory to save power states of children */
75827c478bd9Sstevel@tonic-gate 	hubpm->hubp_child_pwrstate = (uint8_t *)
75837c478bd9Sstevel@tonic-gate 	    kmem_zalloc(MAX_PORTS + 1, KM_SLEEP);
75847c478bd9Sstevel@tonic-gate 
75857c478bd9Sstevel@tonic-gate 	/*
75867c478bd9Sstevel@tonic-gate 	 * if the enable remote wakeup fails
75877c478bd9Sstevel@tonic-gate 	 * we still want to enable
75887c478bd9Sstevel@tonic-gate 	 * parent notification so we can PM the children
75897c478bd9Sstevel@tonic-gate 	 */
75907c478bd9Sstevel@tonic-gate 	usb_enable_parent_notification(dip);
75917c478bd9Sstevel@tonic-gate 
75927c478bd9Sstevel@tonic-gate 	if (usb_handle_remote_wakeup(dip,
75937c478bd9Sstevel@tonic-gate 	    USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
75947c478bd9Sstevel@tonic-gate 		uint_t		pwr_states;
75957c478bd9Sstevel@tonic-gate 
75967c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, hubd->h_log_handle,
75977c478bd9Sstevel@tonic-gate 		    "hubd_create_pm_components: "
75987c478bd9Sstevel@tonic-gate 		    "Remote Wakeup Enabled");
75997c478bd9Sstevel@tonic-gate 
76007c478bd9Sstevel@tonic-gate 		if (usb_create_pm_components(dip, &pwr_states) ==
76017c478bd9Sstevel@tonic-gate 		    USB_SUCCESS) {
76027c478bd9Sstevel@tonic-gate 			mutex_enter(HUBD_MUTEX(hubd));
76037c478bd9Sstevel@tonic-gate 			hubpm->hubp_wakeup_enabled = 1;
76047c478bd9Sstevel@tonic-gate 			hubpm->hubp_pwr_states = (uint8_t)pwr_states;
76057c478bd9Sstevel@tonic-gate 
76067c478bd9Sstevel@tonic-gate 			/* we are busy now till end of the attach */
76077c478bd9Sstevel@tonic-gate 			hubd_pm_busy_component(hubd, dip, 0);
76087c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
76097c478bd9Sstevel@tonic-gate 
76107c478bd9Sstevel@tonic-gate 			/* bring the device to full power */
76117c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0,
76127c478bd9Sstevel@tonic-gate 			    USB_DEV_OS_FULL_PWR);
76137c478bd9Sstevel@tonic-gate 		}
76147c478bd9Sstevel@tonic-gate 	}
76157c478bd9Sstevel@tonic-gate 
76167c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, hubd->h_log_handle,
76177c478bd9Sstevel@tonic-gate 	    "hubd_create_pm_components: END");
76187c478bd9Sstevel@tonic-gate }
76197c478bd9Sstevel@tonic-gate 
76207c478bd9Sstevel@tonic-gate 
76217c478bd9Sstevel@tonic-gate /*
76227c478bd9Sstevel@tonic-gate  * Attachment point management
76237c478bd9Sstevel@tonic-gate  */
76247c478bd9Sstevel@tonic-gate /* ARGSUSED */
76257c478bd9Sstevel@tonic-gate int
usba_hubdi_open(dev_info_t * dip,dev_t * devp,int flags,int otyp,cred_t * credp)76267c478bd9Sstevel@tonic-gate usba_hubdi_open(dev_info_t *dip, dev_t *devp, int flags, int otyp,
76277c478bd9Sstevel@tonic-gate     cred_t *credp)
76287c478bd9Sstevel@tonic-gate {
76297c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
76307c478bd9Sstevel@tonic-gate 
76317c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
76327c478bd9Sstevel@tonic-gate 		return (EINVAL);
76337c478bd9Sstevel@tonic-gate 
76347c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
76357c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
76367c478bd9Sstevel@tonic-gate 		return (ENXIO);
76377c478bd9Sstevel@tonic-gate 	}
76387c478bd9Sstevel@tonic-gate 
76397c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
76407c478bd9Sstevel@tonic-gate 	    "hubd_open:");
76417c478bd9Sstevel@tonic-gate 
76427c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
76437c478bd9Sstevel@tonic-gate 	if ((flags & FEXCL) && (hubd->h_softstate & HUBD_SS_ISOPEN)) {
76447c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
76457c478bd9Sstevel@tonic-gate 
76467c478bd9Sstevel@tonic-gate 		return (EBUSY);
76477c478bd9Sstevel@tonic-gate 	}
76487c478bd9Sstevel@tonic-gate 
76497c478bd9Sstevel@tonic-gate 	hubd->h_softstate |= HUBD_SS_ISOPEN;
76507c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
76517c478bd9Sstevel@tonic-gate 
76527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "opened");
76537c478bd9Sstevel@tonic-gate 
76547c478bd9Sstevel@tonic-gate 	return (0);
76557c478bd9Sstevel@tonic-gate }
76567c478bd9Sstevel@tonic-gate 
76577c478bd9Sstevel@tonic-gate 
76587c478bd9Sstevel@tonic-gate /* ARGSUSED */
76597c478bd9Sstevel@tonic-gate int
usba_hubdi_close(dev_info_t * dip,dev_t dev,int flag,int otyp,cred_t * credp)76607c478bd9Sstevel@tonic-gate usba_hubdi_close(dev_info_t *dip, dev_t dev, int flag, int otyp,
76617c478bd9Sstevel@tonic-gate     cred_t *credp)
76627c478bd9Sstevel@tonic-gate {
76637c478bd9Sstevel@tonic-gate 	hubd_t *hubd;
76647c478bd9Sstevel@tonic-gate 
76657c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
76667c478bd9Sstevel@tonic-gate 		return (EINVAL);
76677c478bd9Sstevel@tonic-gate 	}
76687c478bd9Sstevel@tonic-gate 
76697c478bd9Sstevel@tonic-gate 	hubd = hubd_get_soft_state(dip);
76707c478bd9Sstevel@tonic-gate 
76717c478bd9Sstevel@tonic-gate 	if (hubd == NULL) {
76727c478bd9Sstevel@tonic-gate 		return (ENXIO);
76737c478bd9Sstevel@tonic-gate 	}
76747c478bd9Sstevel@tonic-gate 
76757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "hubd_close:");
76767c478bd9Sstevel@tonic-gate 
76777c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
76787c478bd9Sstevel@tonic-gate 	hubd->h_softstate &= ~HUBD_SS_ISOPEN;
76797c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
76807c478bd9Sstevel@tonic-gate 
76817c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle, "closed");
76827c478bd9Sstevel@tonic-gate 
76837c478bd9Sstevel@tonic-gate 	return (0);
76847c478bd9Sstevel@tonic-gate }
76857c478bd9Sstevel@tonic-gate 
76867c478bd9Sstevel@tonic-gate 
76877c478bd9Sstevel@tonic-gate /*
76887c478bd9Sstevel@tonic-gate  * hubd_ioctl: cfgadm controls
76897c478bd9Sstevel@tonic-gate  */
76907c478bd9Sstevel@tonic-gate /* ARGSUSED */
76917c478bd9Sstevel@tonic-gate int
usba_hubdi_ioctl(dev_info_t * self,dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)76927c478bd9Sstevel@tonic-gate usba_hubdi_ioctl(dev_info_t *self, dev_t dev, int cmd, intptr_t arg,
76937c478bd9Sstevel@tonic-gate     int mode, cred_t *credp, int *rvalp)
76947c478bd9Sstevel@tonic-gate {
76957c478bd9Sstevel@tonic-gate 	int			rv = 0;
76967c478bd9Sstevel@tonic-gate 	char			*msg;	/* for messages */
76977c478bd9Sstevel@tonic-gate 	hubd_t			*hubd;
76987c478bd9Sstevel@tonic-gate 	usb_port_t		port = 0;
76997c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
77007c478bd9Sstevel@tonic-gate 	dev_info_t		*rh_dip;
77017c478bd9Sstevel@tonic-gate 	devctl_ap_state_t	ap_state;
77027c478bd9Sstevel@tonic-gate 	struct devctl_iocdata	*dcp = NULL;
77037c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	prev_pipe_state = 0;
77047c478bd9Sstevel@tonic-gate 
77057c478bd9Sstevel@tonic-gate 	if ((hubd = hubd_get_soft_state(self)) == NULL) {
77067c478bd9Sstevel@tonic-gate 
77077c478bd9Sstevel@tonic-gate 		return (ENXIO);
77087c478bd9Sstevel@tonic-gate 	}
77097c478bd9Sstevel@tonic-gate 
77107c478bd9Sstevel@tonic-gate 	rh_dip = hubd->h_usba_device->usb_root_hub_dip;
77117c478bd9Sstevel@tonic-gate 
77127c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
77137c478bd9Sstevel@tonic-gate 	    "usba_hubdi_ioctl: "
77147c478bd9Sstevel@tonic-gate 	    "cmd=%x, arg=%lx, mode=%x, cred=%p, rval=%p dev=0x%lx",
7715112116d8Sfb209375 	    cmd, arg, mode, (void *)credp, (void *)rvalp, dev);
77167c478bd9Sstevel@tonic-gate 
77177c478bd9Sstevel@tonic-gate 	/* read devctl ioctl data */
77187c478bd9Sstevel@tonic-gate 	if ((cmd != DEVCTL_AP_CONTROL) &&
77197c478bd9Sstevel@tonic-gate 	    (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)) {
77207c478bd9Sstevel@tonic-gate 
77217c478bd9Sstevel@tonic-gate 		return (EFAULT);
77227c478bd9Sstevel@tonic-gate 	}
77237c478bd9Sstevel@tonic-gate 
77247c478bd9Sstevel@tonic-gate 	/*
77257c478bd9Sstevel@tonic-gate 	 * make sure the hub is connected before trying any
77267c478bd9Sstevel@tonic-gate 	 * of the following operations:
77277c478bd9Sstevel@tonic-gate 	 * configure, connect, disconnect
77287c478bd9Sstevel@tonic-gate 	 */
77297c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
77307c478bd9Sstevel@tonic-gate 
77317c478bd9Sstevel@tonic-gate 	switch (cmd) {
77327c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
77337c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
77347c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
77357c478bd9Sstevel@tonic-gate 		if (hubd->h_dev_state == USB_DEV_DISCONNECTED) {
77367c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
77377c478bd9Sstevel@tonic-gate 			    "hubd: already gone");
77387c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
77397c478bd9Sstevel@tonic-gate 			if (dcp) {
77407c478bd9Sstevel@tonic-gate 				ndi_dc_freehdl(dcp);
77417c478bd9Sstevel@tonic-gate 			}
77427c478bd9Sstevel@tonic-gate 
77437c478bd9Sstevel@tonic-gate 			return (EIO);
77447c478bd9Sstevel@tonic-gate 		}
77457c478bd9Sstevel@tonic-gate 
77467c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
77477c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
77487c478bd9Sstevel@tonic-gate 		if ((port = hubd_get_port_num(hubd, dcp)) == 0) {
77497c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
77507c478bd9Sstevel@tonic-gate 			    "hubd: bad port");
77517c478bd9Sstevel@tonic-gate 			mutex_exit(HUBD_MUTEX(hubd));
77527c478bd9Sstevel@tonic-gate 			if (dcp) {
77537c478bd9Sstevel@tonic-gate 				ndi_dc_freehdl(dcp);
77547c478bd9Sstevel@tonic-gate 			}
77557c478bd9Sstevel@tonic-gate 
77567c478bd9Sstevel@tonic-gate 			return (EINVAL);
77577c478bd9Sstevel@tonic-gate 		}
77587c478bd9Sstevel@tonic-gate 		break;
77597c478bd9Sstevel@tonic-gate 
77607c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
77617c478bd9Sstevel@tonic-gate 
77627c478bd9Sstevel@tonic-gate 		break;
77637c478bd9Sstevel@tonic-gate 	default:
77647c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
77657c478bd9Sstevel@tonic-gate 		if (dcp) {
77667c478bd9Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
77677c478bd9Sstevel@tonic-gate 		}
77687c478bd9Sstevel@tonic-gate 
77697c478bd9Sstevel@tonic-gate 		return (ENOTTY);
77707c478bd9Sstevel@tonic-gate 	}
77717c478bd9Sstevel@tonic-gate 
77727c478bd9Sstevel@tonic-gate 	/* should not happen, just in case */
77737c478bd9Sstevel@tonic-gate 	if (hubd->h_dev_state == USB_DEV_SUSPENDED) {
77747c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
77757c478bd9Sstevel@tonic-gate 		if (dcp) {
77767c478bd9Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
77777c478bd9Sstevel@tonic-gate 		}
77787c478bd9Sstevel@tonic-gate 
77797c478bd9Sstevel@tonic-gate 		return (EIO);
77807c478bd9Sstevel@tonic-gate 	}
77817c478bd9Sstevel@tonic-gate 
7782ffcd51f3Slg150142 	if (hubd->h_reset_port[port]) {
7783ffcd51f3Slg150142 		USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
7784ffcd51f3Slg150142 		    "This port is resetting, just return");
7785ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
7786ffcd51f3Slg150142 		if (dcp) {
7787ffcd51f3Slg150142 			ndi_dc_freehdl(dcp);
7788ffcd51f3Slg150142 		}
7789ffcd51f3Slg150142 
7790ffcd51f3Slg150142 		return (EIO);
7791ffcd51f3Slg150142 	}
7792ffcd51f3Slg150142 
77937c478bd9Sstevel@tonic-gate 	hubd_pm_busy_component(hubd, hubd->h_dip, 0);
77947c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
77957c478bd9Sstevel@tonic-gate 
77967c478bd9Sstevel@tonic-gate 	/* go full power */
77977c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(hubd->h_dip, 0, USB_DEV_OS_FULL_PWR);
77987c478bd9Sstevel@tonic-gate 
77993fe80ca4SDan Cross 	ndi_devi_enter(ddi_get_parent(rh_dip));
78003fe80ca4SDan Cross 	ndi_devi_enter(rh_dip);
78013fe80ca4SDan Cross 	ndi_devi_enter(hubd->h_dip);
78027c478bd9Sstevel@tonic-gate 
78037c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
78047c478bd9Sstevel@tonic-gate 
7805c0f24e5bSlg150142 	hubd->h_hotplug_thread++;
7806c0f24e5bSlg150142 
7807c0f24e5bSlg150142 	/* stop polling if it was active */
78087c478bd9Sstevel@tonic-gate 	if (hubd->h_ep1_ph) {
78097c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
78107c478bd9Sstevel@tonic-gate 		(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
78117c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP);
78127c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
78137c478bd9Sstevel@tonic-gate 
78147c478bd9Sstevel@tonic-gate 		if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
78157c478bd9Sstevel@tonic-gate 			hubd_stop_polling(hubd);
78167c478bd9Sstevel@tonic-gate 		}
78177c478bd9Sstevel@tonic-gate 	}
78187c478bd9Sstevel@tonic-gate 
78197c478bd9Sstevel@tonic-gate 	switch (cmd) {
78207c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
78217c478bd9Sstevel@tonic-gate 		if (hubd_delete_child(hubd, port,
78227c478bd9Sstevel@tonic-gate 		    NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS) {
78237c478bd9Sstevel@tonic-gate 			rv = EIO;
78247c478bd9Sstevel@tonic-gate 		}
78257c478bd9Sstevel@tonic-gate 
78267c478bd9Sstevel@tonic-gate 		break;
78277c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
78287c478bd9Sstevel@tonic-gate 		if (hubd_delete_child(hubd, port,
78297c478bd9Sstevel@tonic-gate 		    NDI_UNCONFIG, B_FALSE) != USB_SUCCESS) {
78307c478bd9Sstevel@tonic-gate 			rv = EIO;
78317c478bd9Sstevel@tonic-gate 		}
78327c478bd9Sstevel@tonic-gate 
78337c478bd9Sstevel@tonic-gate 		break;
78347c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
78357c478bd9Sstevel@tonic-gate 		/* toggle port */
78367c478bd9Sstevel@tonic-gate 		if (hubd_toggle_port(hubd, port) != USB_SUCCESS) {
78377c478bd9Sstevel@tonic-gate 			rv = EIO;
78387c478bd9Sstevel@tonic-gate 
78397c478bd9Sstevel@tonic-gate 			break;
78407c478bd9Sstevel@tonic-gate 		}
78417c478bd9Sstevel@tonic-gate 
78427c478bd9Sstevel@tonic-gate 		(void) hubd_handle_port_connect(hubd, port);
78437c478bd9Sstevel@tonic-gate 		child_dip = hubd_get_child_dip(hubd, port);
78447c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
78457c478bd9Sstevel@tonic-gate 
78463fe80ca4SDan Cross 		ndi_devi_exit(hubd->h_dip);
78473fe80ca4SDan Cross 		ndi_devi_exit(rh_dip);
78483fe80ca4SDan Cross 		ndi_devi_exit(ddi_get_parent(rh_dip));
7849e4c316c4SVincent Wang 		if (child_dip == NULL) {
78507c478bd9Sstevel@tonic-gate 			rv = EIO;
7851e4c316c4SVincent Wang 		} else {
7852e4c316c4SVincent Wang 			ndi_hold_devi(child_dip);
7853e4c316c4SVincent Wang 			if (ndi_devi_online(child_dip, 0) != NDI_SUCCESS)
7854e4c316c4SVincent Wang 				rv = EIO;
7855e4c316c4SVincent Wang 			ndi_rele_devi(child_dip);
78567c478bd9Sstevel@tonic-gate 		}
78573fe80ca4SDan Cross 		ndi_devi_enter(ddi_get_parent(rh_dip));
78583fe80ca4SDan Cross 		ndi_devi_enter(rh_dip);
78593fe80ca4SDan Cross 		ndi_devi_enter(hubd->h_dip);
78607c478bd9Sstevel@tonic-gate 
78617c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
78627c478bd9Sstevel@tonic-gate 
78637c478bd9Sstevel@tonic-gate 		break;
78647c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
78657c478bd9Sstevel@tonic-gate 		switch (hubd_cfgadm_state(hubd, port)) {
78667c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_DISCONNECTED:
78677c478bd9Sstevel@tonic-gate 			/* port previously 'disconnected' by cfgadm */
78687c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_DISCONNECTED;
78697c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
78707c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
78717c478bd9Sstevel@tonic-gate 
78727c478bd9Sstevel@tonic-gate 			break;
78737c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_UNCONFIGURED:
78747c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_CONNECTED;
78757c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
78767c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
78777c478bd9Sstevel@tonic-gate 
78787c478bd9Sstevel@tonic-gate 			break;
78797c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_CONFIGURED:
78807c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_CONNECTED;
78817c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
78827c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
78837c478bd9Sstevel@tonic-gate 
78847c478bd9Sstevel@tonic-gate 			break;
78857c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_STILL_REFERENCED:
78867c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_EMPTY;
78877c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_CONFIGURED;
78887c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_UNUSABLE;
78897c478bd9Sstevel@tonic-gate 
78907c478bd9Sstevel@tonic-gate 			break;
78917c478bd9Sstevel@tonic-gate 		case HUBD_CFGADM_EMPTY:
78927c478bd9Sstevel@tonic-gate 		default:
78937c478bd9Sstevel@tonic-gate 			ap_state.ap_rstate = AP_RSTATE_EMPTY;
78947c478bd9Sstevel@tonic-gate 			ap_state.ap_ostate = AP_OSTATE_UNCONFIGURED;
78957c478bd9Sstevel@tonic-gate 			ap_state.ap_condition = AP_COND_OK;
78967c478bd9Sstevel@tonic-gate 
78977c478bd9Sstevel@tonic-gate 			break;
78987c478bd9Sstevel@tonic-gate 		}
78997c478bd9Sstevel@tonic-gate 
79007c478bd9Sstevel@tonic-gate 		ap_state.ap_last_change = (time_t)-1;
79017c478bd9Sstevel@tonic-gate 		ap_state.ap_error_code = 0;
79027c478bd9Sstevel@tonic-gate 		ap_state.ap_in_transition = 0;
79037c478bd9Sstevel@tonic-gate 
79047c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
79057c478bd9Sstevel@tonic-gate 		    "DEVCTL_AP_GETSTATE: "
79067c478bd9Sstevel@tonic-gate 		    "ostate=0x%x, rstate=0x%x, condition=0x%x",
79077c478bd9Sstevel@tonic-gate 		    ap_state.ap_ostate,
79087c478bd9Sstevel@tonic-gate 		    ap_state.ap_rstate, ap_state.ap_condition);
79097c478bd9Sstevel@tonic-gate 
79107c478bd9Sstevel@tonic-gate 		/* copy the return-AP-state information to the user space */
79117c478bd9Sstevel@tonic-gate 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
79127c478bd9Sstevel@tonic-gate 			rv = EFAULT;
79137c478bd9Sstevel@tonic-gate 		}
79147c478bd9Sstevel@tonic-gate 
79157c478bd9Sstevel@tonic-gate 		break;
79167c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
79177c478bd9Sstevel@tonic-gate 	{
79187c478bd9Sstevel@tonic-gate 		/*
79197c478bd9Sstevel@tonic-gate 		 * Generic devctl for hardware-specific functionality.
79207c478bd9Sstevel@tonic-gate 		 * For list of sub-commands see hubd_impl.h
79217c478bd9Sstevel@tonic-gate 		 */
79227c478bd9Sstevel@tonic-gate 		hubd_ioctl_data_t	ioc;	/* for 64 byte copies */
79237c478bd9Sstevel@tonic-gate 
79247c478bd9Sstevel@tonic-gate 		/* copy user ioctl data in first */
79257c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
79267c478bd9Sstevel@tonic-gate 		if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
79277c478bd9Sstevel@tonic-gate 			hubd_ioctl_data_32_t ioc32;
79287c478bd9Sstevel@tonic-gate 
79297c478bd9Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, (void *)&ioc32,
79307c478bd9Sstevel@tonic-gate 			    sizeof (ioc32), mode) != 0) {
79317c478bd9Sstevel@tonic-gate 				rv = EFAULT;
79327c478bd9Sstevel@tonic-gate 
79337c478bd9Sstevel@tonic-gate 				break;
79347c478bd9Sstevel@tonic-gate 			}
79357c478bd9Sstevel@tonic-gate 			ioc.cmd		= (uint_t)ioc32.cmd;
79367c478bd9Sstevel@tonic-gate 			ioc.port	= (uint_t)ioc32.port;
79377c478bd9Sstevel@tonic-gate 			ioc.get_size	= (uint_t)ioc32.get_size;
79387c478bd9Sstevel@tonic-gate 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
79397c478bd9Sstevel@tonic-gate 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
79407c478bd9Sstevel@tonic-gate 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
79417c478bd9Sstevel@tonic-gate 		} else
79427c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
79437c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
79447c478bd9Sstevel@tonic-gate 		    mode) != 0) {
79457c478bd9Sstevel@tonic-gate 			rv = EFAULT;
79467c478bd9Sstevel@tonic-gate 
79477c478bd9Sstevel@tonic-gate 			break;
79487c478bd9Sstevel@tonic-gate 		}
79497c478bd9Sstevel@tonic-gate 
79507c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
79517c478bd9Sstevel@tonic-gate 		    "DEVCTL_AP_CONTROL: ioc: cmd=0x%x port=%d get_size=%d"
79527c478bd9Sstevel@tonic-gate 		    "\n\tbuf=0x%p, bufsiz=%d,  misc_arg=%d", ioc.cmd,
7953112116d8Sfb209375 		    ioc.port, ioc.get_size, (void *)ioc.buf, ioc.bufsiz,
7954112116d8Sfb209375 		    ioc.misc_arg);
79557c478bd9Sstevel@tonic-gate 
79567c478bd9Sstevel@tonic-gate 		/*
79577c478bd9Sstevel@tonic-gate 		 * To avoid BE/LE and 32/64 issues, a get_size always
79587c478bd9Sstevel@tonic-gate 		 * returns a 32-bit number.
79597c478bd9Sstevel@tonic-gate 		 */
79607c478bd9Sstevel@tonic-gate 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
79617c478bd9Sstevel@tonic-gate 			rv = EINVAL;
79627c478bd9Sstevel@tonic-gate 
79637c478bd9Sstevel@tonic-gate 			break;
79647c478bd9Sstevel@tonic-gate 		}
79657c478bd9Sstevel@tonic-gate 
79667c478bd9Sstevel@tonic-gate 		switch (ioc.cmd) {
79677c478bd9Sstevel@tonic-gate 		case USB_DESCR_TYPE_DEV:
79687c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_DEVICE_DESC";
79697c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
79707c478bd9Sstevel@tonic-gate 				/* uint32 so this works 32/64 */
79717c478bd9Sstevel@tonic-gate 				uint32_t size = sizeof (usb_dev_descr_t);
79727c478bd9Sstevel@tonic-gate 
79737c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size, ioc.buf,
79747c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
79757c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
79767c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
79777c478bd9Sstevel@tonic-gate 					    "%s: get_size copyout failed", msg);
79787c478bd9Sstevel@tonic-gate 					rv = EIO;
79797c478bd9Sstevel@tonic-gate 
79807c478bd9Sstevel@tonic-gate 					break;
79817c478bd9Sstevel@tonic-gate 				}
79827c478bd9Sstevel@tonic-gate 			} else {	/* send out the actual descr */
79837c478bd9Sstevel@tonic-gate 				usb_dev_descr_t *dev_descrp;
79847c478bd9Sstevel@tonic-gate 
79857c478bd9Sstevel@tonic-gate 				/* check child_dip */
79867c478bd9Sstevel@tonic-gate 				if ((child_dip = hubd_get_child_dip(hubd,
79877c478bd9Sstevel@tonic-gate 				    ioc.port)) == NULL) {
79887c478bd9Sstevel@tonic-gate 					rv = EINVAL;
79897c478bd9Sstevel@tonic-gate 
79907c478bd9Sstevel@tonic-gate 					break;
79917c478bd9Sstevel@tonic-gate 				}
79927c478bd9Sstevel@tonic-gate 
79937c478bd9Sstevel@tonic-gate 				dev_descrp = usb_get_dev_descr(child_dip);
79947c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != sizeof (*dev_descrp)) {
79957c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
79967c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
79977c478bd9Sstevel@tonic-gate 					    "%s: bufsize passed (%d) != sizeof "
79987c478bd9Sstevel@tonic-gate 					    "usba_device_descr_t (%d)", msg,
79997c478bd9Sstevel@tonic-gate 					    ioc.bufsiz, dev_descrp->bLength);
80007c478bd9Sstevel@tonic-gate 					rv = EINVAL;
80017c478bd9Sstevel@tonic-gate 
80027c478bd9Sstevel@tonic-gate 					break;
80037c478bd9Sstevel@tonic-gate 				}
80047c478bd9Sstevel@tonic-gate 
80057c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)dev_descrp,
80067c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
80077c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
80087c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
80097c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
80107c478bd9Sstevel@tonic-gate 					rv = EIO;
80117c478bd9Sstevel@tonic-gate 
80127c478bd9Sstevel@tonic-gate 					break;
80137c478bd9Sstevel@tonic-gate 				}
80147c478bd9Sstevel@tonic-gate 			}
80157c478bd9Sstevel@tonic-gate 			break;
80167c478bd9Sstevel@tonic-gate 		case USB_DESCR_TYPE_STRING:
80177c478bd9Sstevel@tonic-gate 		{
80187c478bd9Sstevel@tonic-gate 			char		*str;
80197c478bd9Sstevel@tonic-gate 			uint32_t	size;
80207c478bd9Sstevel@tonic-gate 			usba_device_t	*usba_device;
80217c478bd9Sstevel@tonic-gate 
80227c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_STRING_DESCR";
80237c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
80247c478bd9Sstevel@tonic-gate 			    "%s: string request: %d", msg, ioc.misc_arg);
80257c478bd9Sstevel@tonic-gate 
80267c478bd9Sstevel@tonic-gate 			/* recheck */
80277c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
80287c478bd9Sstevel@tonic-gate 			    NULL) {
80297c478bd9Sstevel@tonic-gate 				rv = EINVAL;
80307c478bd9Sstevel@tonic-gate 
80317c478bd9Sstevel@tonic-gate 				break;
80327c478bd9Sstevel@tonic-gate 			}
80337c478bd9Sstevel@tonic-gate 			usba_device = usba_get_usba_device(child_dip);
80347c478bd9Sstevel@tonic-gate 
80357c478bd9Sstevel@tonic-gate 			switch (ioc.misc_arg) {
80367c478bd9Sstevel@tonic-gate 			case HUBD_MFG_STR:
80377c478bd9Sstevel@tonic-gate 				str = usba_device->usb_mfg_str;
80387c478bd9Sstevel@tonic-gate 
80397c478bd9Sstevel@tonic-gate 				break;
80407c478bd9Sstevel@tonic-gate 			case HUBD_PRODUCT_STR:
80417c478bd9Sstevel@tonic-gate 				str = usba_device->usb_product_str;
80427c478bd9Sstevel@tonic-gate 
80437c478bd9Sstevel@tonic-gate 				break;
80447c478bd9Sstevel@tonic-gate 			case HUBD_SERIALNO_STR:
80457c478bd9Sstevel@tonic-gate 				str = usba_device->usb_serialno_str;
80467c478bd9Sstevel@tonic-gate 
80477c478bd9Sstevel@tonic-gate 				break;
80487c478bd9Sstevel@tonic-gate 			case HUBD_CFG_DESCR_STR:
80497c478bd9Sstevel@tonic-gate 				mutex_enter(&usba_device->usb_mutex);
80507c478bd9Sstevel@tonic-gate 				str = usba_device->usb_cfg_str_descr[
80517c478bd9Sstevel@tonic-gate 				    usba_device->usb_active_cfg_ndx];
80527c478bd9Sstevel@tonic-gate 				mutex_exit(&usba_device->usb_mutex);
80537c478bd9Sstevel@tonic-gate 
80547c478bd9Sstevel@tonic-gate 				break;
80557c478bd9Sstevel@tonic-gate 			default:
80567c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
80577c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
80587c478bd9Sstevel@tonic-gate 				    "%s: Invalid string request", msg);
80597c478bd9Sstevel@tonic-gate 				rv = EINVAL;
80607c478bd9Sstevel@tonic-gate 
80617c478bd9Sstevel@tonic-gate 				break;
80627c478bd9Sstevel@tonic-gate 			} /* end of switch */
80637c478bd9Sstevel@tonic-gate 
80647c478bd9Sstevel@tonic-gate 			if (rv != 0) {
80657c478bd9Sstevel@tonic-gate 
80667c478bd9Sstevel@tonic-gate 				break;
80677c478bd9Sstevel@tonic-gate 			}
80687c478bd9Sstevel@tonic-gate 
80697c478bd9Sstevel@tonic-gate 			size = (str != NULL) ? strlen(str) + 1 : 0;
80707c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
80717c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size, ioc.buf,
80727c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
80737c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
80747c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
80757c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
80767c478bd9Sstevel@tonic-gate 					rv = EIO;
80777c478bd9Sstevel@tonic-gate 
80787c478bd9Sstevel@tonic-gate 					break;
80797c478bd9Sstevel@tonic-gate 				}
80807c478bd9Sstevel@tonic-gate 			} else {
80817c478bd9Sstevel@tonic-gate 				if (size == 0) {
80827c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L3(DPRINT_MASK_CBOPS,
80837c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
80847c478bd9Sstevel@tonic-gate 					    "%s: String is NULL", msg);
80857c478bd9Sstevel@tonic-gate 					rv = EINVAL;
80867c478bd9Sstevel@tonic-gate 
80877c478bd9Sstevel@tonic-gate 					break;
80887c478bd9Sstevel@tonic-gate 				}
80897c478bd9Sstevel@tonic-gate 
80907c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
80917c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
80927c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
80937c478bd9Sstevel@tonic-gate 					    "%s: string buf size wrong", msg);
80947c478bd9Sstevel@tonic-gate 					rv = EINVAL;
80957c478bd9Sstevel@tonic-gate 
80967c478bd9Sstevel@tonic-gate 					break;
80977c478bd9Sstevel@tonic-gate 				}
80987c478bd9Sstevel@tonic-gate 
80997c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)str, ioc.buf,
81007c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
81017c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
81027c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
81037c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
81047c478bd9Sstevel@tonic-gate 					rv = EIO;
81057c478bd9Sstevel@tonic-gate 
81067c478bd9Sstevel@tonic-gate 					break;
81077c478bd9Sstevel@tonic-gate 				}
81087c478bd9Sstevel@tonic-gate 			}
81097c478bd9Sstevel@tonic-gate 			break;
81107c478bd9Sstevel@tonic-gate 		}
81117c478bd9Sstevel@tonic-gate 		case HUBD_GET_CFGADM_NAME:
81127c478bd9Sstevel@tonic-gate 		{
81137c478bd9Sstevel@tonic-gate 			uint32_t   name_len;
81147c478bd9Sstevel@tonic-gate 			const char *name;
81157c478bd9Sstevel@tonic-gate 
81167c478bd9Sstevel@tonic-gate 			/* recheck */
81177c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
81187c478bd9Sstevel@tonic-gate 			    NULL) {
81197c478bd9Sstevel@tonic-gate 				rv = EINVAL;
81207c478bd9Sstevel@tonic-gate 
81217c478bd9Sstevel@tonic-gate 				break;
81227c478bd9Sstevel@tonic-gate 			}
81237c478bd9Sstevel@tonic-gate 			name = ddi_node_name(child_dip);
81247c478bd9Sstevel@tonic-gate 			if (name == NULL) {
81257c478bd9Sstevel@tonic-gate 				name = "unsupported";
81267c478bd9Sstevel@tonic-gate 			}
81277c478bd9Sstevel@tonic-gate 			name_len = strlen(name) + 1;
81287c478bd9Sstevel@tonic-gate 
81297c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: HUBD_GET_CFGADM_NAME";
81307c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
81317c478bd9Sstevel@tonic-gate 			    "%s: name=%s name_len=%d", msg, name, name_len);
81327c478bd9Sstevel@tonic-gate 
81337c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
81347c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&name_len,
81357c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
81367c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
81377c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
81387c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed", msg);
81397c478bd9Sstevel@tonic-gate 					rv = EIO;
81407c478bd9Sstevel@tonic-gate 
81417c478bd9Sstevel@tonic-gate 					break;
81427c478bd9Sstevel@tonic-gate 				}
81437c478bd9Sstevel@tonic-gate 			} else {
81447c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != name_len) {
81457c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
81467c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
81477c478bd9Sstevel@tonic-gate 					    "%s: string buf length wrong", msg);
81487c478bd9Sstevel@tonic-gate 					rv = EINVAL;
81497c478bd9Sstevel@tonic-gate 
81507c478bd9Sstevel@tonic-gate 					break;
81517c478bd9Sstevel@tonic-gate 				}
81527c478bd9Sstevel@tonic-gate 
81537c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)name, ioc.buf,
81547c478bd9Sstevel@tonic-gate 				    ioc.bufsiz, mode) != 0) {
81557c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
81567c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
81577c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
81587c478bd9Sstevel@tonic-gate 					rv = EIO;
81597c478bd9Sstevel@tonic-gate 
81607c478bd9Sstevel@tonic-gate 					break;
81617c478bd9Sstevel@tonic-gate 				}
81627c478bd9Sstevel@tonic-gate 			}
81637c478bd9Sstevel@tonic-gate 
81647c478bd9Sstevel@tonic-gate 			break;
81657c478bd9Sstevel@tonic-gate 		}
81667c478bd9Sstevel@tonic-gate 
81677c478bd9Sstevel@tonic-gate 		/*
81687c478bd9Sstevel@tonic-gate 		 * Return the config index for the currently-configured
81697c478bd9Sstevel@tonic-gate 		 * configuration.
81707c478bd9Sstevel@tonic-gate 		 */
81717c478bd9Sstevel@tonic-gate 		case HUBD_GET_CURRENT_CONFIG:
81727c478bd9Sstevel@tonic-gate 		{
81737c478bd9Sstevel@tonic-gate 			uint_t		config_index;
81747c478bd9Sstevel@tonic-gate 			uint32_t	size = sizeof (config_index);
81757c478bd9Sstevel@tonic-gate 			usba_device_t	*usba_device;
81767c478bd9Sstevel@tonic-gate 
81777c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_CURRENT_CONFIG";
81787c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
81797c478bd9Sstevel@tonic-gate 			    "%s", msg);
81807c478bd9Sstevel@tonic-gate 
81817c478bd9Sstevel@tonic-gate 			/*
81827c478bd9Sstevel@tonic-gate 			 * Return the config index for the configuration
81837c478bd9Sstevel@tonic-gate 			 * currently in use.
81847c478bd9Sstevel@tonic-gate 			 * Recheck if child_dip exists
81857c478bd9Sstevel@tonic-gate 			 */
81867c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
81877c478bd9Sstevel@tonic-gate 			    NULL) {
81887c478bd9Sstevel@tonic-gate 				rv = EINVAL;
81897c478bd9Sstevel@tonic-gate 
81907c478bd9Sstevel@tonic-gate 				break;
81917c478bd9Sstevel@tonic-gate 			}
81927c478bd9Sstevel@tonic-gate 
81937c478bd9Sstevel@tonic-gate 			usba_device = usba_get_usba_device(child_dip);
81947c478bd9Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
81957c478bd9Sstevel@tonic-gate 			config_index = usba_device->usb_active_cfg_ndx;
81967c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
81977c478bd9Sstevel@tonic-gate 
81987c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
81997c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size,
82007c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
82017c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
82027c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
82037c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
82047c478bd9Sstevel@tonic-gate 					rv = EIO;
82057c478bd9Sstevel@tonic-gate 
82067c478bd9Sstevel@tonic-gate 					break;
82077c478bd9Sstevel@tonic-gate 				}
82087c478bd9Sstevel@tonic-gate 			} else {
82097c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
82107c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
82117c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
82127c478bd9Sstevel@tonic-gate 					    "%s: buffer size wrong", msg);
82137c478bd9Sstevel@tonic-gate 					rv = EINVAL;
82147c478bd9Sstevel@tonic-gate 
82157c478bd9Sstevel@tonic-gate 					break;
82167c478bd9Sstevel@tonic-gate 				}
82177c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&config_index,
82187c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
82197c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
82207c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
82217c478bd9Sstevel@tonic-gate 					    "%s: copyout failed", msg);
82227c478bd9Sstevel@tonic-gate 					rv = EIO;
82237c478bd9Sstevel@tonic-gate 				}
82247c478bd9Sstevel@tonic-gate 			}
82257c478bd9Sstevel@tonic-gate 
82267c478bd9Sstevel@tonic-gate 			break;
82277c478bd9Sstevel@tonic-gate 		}
82287c478bd9Sstevel@tonic-gate 		case HUBD_GET_DEVICE_PATH:
82297c478bd9Sstevel@tonic-gate 		{
82307c478bd9Sstevel@tonic-gate 			char		*path;
82317c478bd9Sstevel@tonic-gate 			uint32_t	size;
82327c478bd9Sstevel@tonic-gate 
82337c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: GET_DEVICE_PATH";
82347c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
82357c478bd9Sstevel@tonic-gate 			    "%s", msg);
82367c478bd9Sstevel@tonic-gate 
82377c478bd9Sstevel@tonic-gate 			/* Recheck if child_dip exists */
82387c478bd9Sstevel@tonic-gate 			if ((child_dip = hubd_get_child_dip(hubd, ioc.port)) ==
82397c478bd9Sstevel@tonic-gate 			    NULL) {
82407c478bd9Sstevel@tonic-gate 				rv = EINVAL;
82417c478bd9Sstevel@tonic-gate 
82427c478bd9Sstevel@tonic-gate 				break;
82437c478bd9Sstevel@tonic-gate 			}
82447c478bd9Sstevel@tonic-gate 
82457c478bd9Sstevel@tonic-gate 			/* ddi_pathname doesn't supply /devices, so we do. */
82467c478bd9Sstevel@tonic-gate 			path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
82477c478bd9Sstevel@tonic-gate 			(void) strcpy(path, "/devices");
82487c478bd9Sstevel@tonic-gate 			(void) ddi_pathname(child_dip, path + strlen(path));
82497c478bd9Sstevel@tonic-gate 			size = strlen(path) + 1;
82507c478bd9Sstevel@tonic-gate 
82517c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_CBOPS, hubd->h_log_handle,
82527c478bd9Sstevel@tonic-gate 			    "%s: device path=%s  size=%d", msg, path, size);
82537c478bd9Sstevel@tonic-gate 
82547c478bd9Sstevel@tonic-gate 			if (ioc.get_size) {
82557c478bd9Sstevel@tonic-gate 				if (ddi_copyout((void *)&size,
82567c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
82577c478bd9Sstevel@tonic-gate 
82587c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
82597c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
82607c478bd9Sstevel@tonic-gate 					    "%s: copyout of size failed.", msg);
82617c478bd9Sstevel@tonic-gate 					rv = EIO;
82627c478bd9Sstevel@tonic-gate 				}
82637c478bd9Sstevel@tonic-gate 			} else {
82647c478bd9Sstevel@tonic-gate 				if (ioc.bufsiz != size) {
82657c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
82667c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
82677c478bd9Sstevel@tonic-gate 					    "%s: buffer wrong size.", msg);
82687c478bd9Sstevel@tonic-gate 					rv = EINVAL;
82697c478bd9Sstevel@tonic-gate 				} else if (ddi_copyout((void *)path,
82707c478bd9Sstevel@tonic-gate 				    ioc.buf, ioc.bufsiz, mode) != 0) {
82717c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
82727c478bd9Sstevel@tonic-gate 					    hubd->h_log_handle,
82737c478bd9Sstevel@tonic-gate 					    "%s: copyout failed.", msg);
82747c478bd9Sstevel@tonic-gate 					rv = EIO;
82757c478bd9Sstevel@tonic-gate 				}
82767c478bd9Sstevel@tonic-gate 			}
82777c478bd9Sstevel@tonic-gate 			kmem_free(path, MAXPATHLEN);
82787c478bd9Sstevel@tonic-gate 
82797c478bd9Sstevel@tonic-gate 			break;
82807c478bd9Sstevel@tonic-gate 		}
82817c478bd9Sstevel@tonic-gate 		case HUBD_REFRESH_DEVDB:
82827c478bd9Sstevel@tonic-gate 			msg = "DEVCTL_AP_CONTROL: HUBD_REFRESH_DEVDB";
82837c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_CBOPS, hubd->h_log_handle,
82847c478bd9Sstevel@tonic-gate 			    "%s", msg);
82857c478bd9Sstevel@tonic-gate 
82867c478bd9Sstevel@tonic-gate 			if ((rv = usba_devdb_refresh()) != USB_SUCCESS) {
82877c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_CBOPS,
82887c478bd9Sstevel@tonic-gate 				    hubd->h_log_handle,
82897c478bd9Sstevel@tonic-gate 				    "%s: Failed: %d", msg, rv);
82907c478bd9Sstevel@tonic-gate 				rv = EIO;
82917c478bd9Sstevel@tonic-gate 			}
82927c478bd9Sstevel@tonic-gate 
82937c478bd9Sstevel@tonic-gate 			break;
82947c478bd9Sstevel@tonic-gate 		default:
82957c478bd9Sstevel@tonic-gate 			rv = ENOTSUP;
82967c478bd9Sstevel@tonic-gate 		}	/* end switch */
82977c478bd9Sstevel@tonic-gate 
82987c478bd9Sstevel@tonic-gate 		break;
82997c478bd9Sstevel@tonic-gate 	}
83007c478bd9Sstevel@tonic-gate 
83017c478bd9Sstevel@tonic-gate 	default:
83027c478bd9Sstevel@tonic-gate 		rv = ENOTTY;
83037c478bd9Sstevel@tonic-gate 	}
83047c478bd9Sstevel@tonic-gate 
83057c478bd9Sstevel@tonic-gate 	if (dcp) {
83067c478bd9Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
83077c478bd9Sstevel@tonic-gate 	}
83087c478bd9Sstevel@tonic-gate 
83097c478bd9Sstevel@tonic-gate 	/* allow hotplug thread now */
83107c478bd9Sstevel@tonic-gate 	hubd->h_hotplug_thread--;
83117c478bd9Sstevel@tonic-gate 
83127c478bd9Sstevel@tonic-gate 	if ((hubd->h_dev_state == USB_DEV_ONLINE) &&
83137c478bd9Sstevel@tonic-gate 	    hubd->h_ep1_ph && (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
83147c478bd9Sstevel@tonic-gate 		hubd_start_polling(hubd, 0);
83157c478bd9Sstevel@tonic-gate 	}
83167c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
83177c478bd9Sstevel@tonic-gate 
83183fe80ca4SDan Cross 	ndi_devi_exit(hubd->h_dip);
83193fe80ca4SDan Cross 	ndi_devi_exit(rh_dip);
83203fe80ca4SDan Cross 	ndi_devi_exit(ddi_get_parent(rh_dip));
83217c478bd9Sstevel@tonic-gate 
83227c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
83237c478bd9Sstevel@tonic-gate 	hubd_pm_idle_component(hubd, hubd->h_dip, 0);
83247c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
83257c478bd9Sstevel@tonic-gate 
83267c478bd9Sstevel@tonic-gate 	return (rv);
83277c478bd9Sstevel@tonic-gate }
83287c478bd9Sstevel@tonic-gate 
83297c478bd9Sstevel@tonic-gate 
83307c478bd9Sstevel@tonic-gate /*
83317c478bd9Sstevel@tonic-gate  * Helper func used only to help construct the names for the attachment point
83327c478bd9Sstevel@tonic-gate  * minor nodes.  Used only in usba_hubdi_attach.
83337c478bd9Sstevel@tonic-gate  * Returns whether it found ancestry or not (USB_SUCCESS if yes).
83347c478bd9Sstevel@tonic-gate  * ports between the root hub and the device represented by dip.
83357c478bd9Sstevel@tonic-gate  * E.g.,  "2.4.3.1" means this device is
83367c478bd9Sstevel@tonic-gate  *	plugged into port 1 of a hub that is
83377c478bd9Sstevel@tonic-gate  *	plugged into port 3 of a hub that is
83387c478bd9Sstevel@tonic-gate  *	plugged into port 4 of a hub that is
83397c478bd9Sstevel@tonic-gate  *	plugged into port 2 of the root hub.
83407c478bd9Sstevel@tonic-gate  * NOTE: Max ap_id path len is HUBD_APID_NAMELEN (32 chars), which is
83417c478bd9Sstevel@tonic-gate  * more than sufficient (as hubs are a max 6 levels deep, port needs 3
83427c478bd9Sstevel@tonic-gate  * chars plus NULL each)
83437c478bd9Sstevel@tonic-gate  */
8344ff0e937bSRaymond Chen void
hubd_get_ancestry_str(hubd_t * hubd)83457c478bd9Sstevel@tonic-gate hubd_get_ancestry_str(hubd_t *hubd)
83467c478bd9Sstevel@tonic-gate {
8347ff0e937bSRaymond Chen 	char		ap_name[HUBD_APID_NAMELEN];
8348ff0e937bSRaymond Chen 	dev_info_t	*pdip;
8349ff0e937bSRaymond Chen 	hubd_t		*phubd;
8350ff0e937bSRaymond Chen 	usb_port_t	port;
83517c478bd9Sstevel@tonic-gate 
83527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
8353112116d8Sfb209375 	    "hubd_get_ancestry_str: hubd=0x%p", (void *)hubd);
83547c478bd9Sstevel@tonic-gate 
8355ff0e937bSRaymond Chen 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
83567c478bd9Sstevel@tonic-gate 
83577c478bd9Sstevel@tonic-gate 	/*
8358ff0e937bSRaymond Chen 	 * The function is extended to support wire adapter class
8359ff0e937bSRaymond Chen 	 * devices introduced by WUSB spec. The node name is no
8360ff0e937bSRaymond Chen 	 * longer "hub" only.
8361ff0e937bSRaymond Chen 	 * Generate the ap_id str based on the parent and child
8362ff0e937bSRaymond Chen 	 * relationship instead of retrieving it from the hub
8363ff0e937bSRaymond Chen 	 * device path, which simplifies the algorithm.
83647c478bd9Sstevel@tonic-gate 	 */
8365ff0e937bSRaymond Chen 	if (usba_is_root_hub(hubd->h_dip)) {
8366ff0e937bSRaymond Chen 		hubd->h_ancestry_str[0] = '\0';
8367ff0e937bSRaymond Chen 	} else {
8368ff0e937bSRaymond Chen 		port = hubd->h_usba_device->usb_port;
8369ff0e937bSRaymond Chen 		mutex_exit(HUBD_MUTEX(hubd));
8370ff0e937bSRaymond Chen 
8371ff0e937bSRaymond Chen 		pdip = ddi_get_parent(hubd->h_dip);
8372ff0e937bSRaymond Chen 		/*
8373ff0e937bSRaymond Chen 		 * The parent of wire adapter device might be usb_mid.
8374ff0e937bSRaymond Chen 		 * Need to look further up for hub device
8375ff0e937bSRaymond Chen 		 */
8376ff0e937bSRaymond Chen 		if (strcmp(ddi_driver_name(pdip), "usb_mid") == 0) {
8377ff0e937bSRaymond Chen 			pdip = ddi_get_parent(pdip);
8378ff0e937bSRaymond Chen 			ASSERT(pdip != NULL);
83797c478bd9Sstevel@tonic-gate 		}
83807c478bd9Sstevel@tonic-gate 
8381ff0e937bSRaymond Chen 		phubd = hubd_get_soft_state(pdip);
83827c478bd9Sstevel@tonic-gate 
8383ff0e937bSRaymond Chen 		mutex_enter(HUBD_MUTEX(phubd));
8384ff0e937bSRaymond Chen 		(void) snprintf(ap_name, HUBD_APID_NAMELEN, "%s%d",
8385ff0e937bSRaymond Chen 		    phubd->h_ancestry_str, port);
8386ff0e937bSRaymond Chen 		mutex_exit(HUBD_MUTEX(phubd));
83877c478bd9Sstevel@tonic-gate 
8388ff0e937bSRaymond Chen 		mutex_enter(HUBD_MUTEX(hubd));
8389ff0e937bSRaymond Chen 		(void) strcpy(hubd->h_ancestry_str, ap_name);
83907c478bd9Sstevel@tonic-gate 		(void) strcat(hubd->h_ancestry_str, ".");
83917c478bd9Sstevel@tonic-gate 	}
83927c478bd9Sstevel@tonic-gate }
83937c478bd9Sstevel@tonic-gate 
83947c478bd9Sstevel@tonic-gate 
83957c478bd9Sstevel@tonic-gate /* Get which port to operate on.  */
83967c478bd9Sstevel@tonic-gate static usb_port_t
hubd_get_port_num(hubd_t * hubd,struct devctl_iocdata * dcp)83977c478bd9Sstevel@tonic-gate hubd_get_port_num(hubd_t *hubd, struct devctl_iocdata *dcp)
83987c478bd9Sstevel@tonic-gate {
83997c478bd9Sstevel@tonic-gate 	int32_t port;
84007c478bd9Sstevel@tonic-gate 
84017c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
84027c478bd9Sstevel@tonic-gate 
84037c478bd9Sstevel@tonic-gate 	/* Get which port to operate on.  */
84047c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
84057c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_CBOPS, hubd->h_log_handle,
84067c478bd9Sstevel@tonic-gate 		    "hubd_get_port_num: port lookup failed");
84077c478bd9Sstevel@tonic-gate 		port = 0;
84087c478bd9Sstevel@tonic-gate 	}
84097c478bd9Sstevel@tonic-gate 
84107c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8411112116d8Sfb209375 	    "hubd_get_port_num: hubd=0x%p, port=%d", (void *)hubd, port);
84127c478bd9Sstevel@tonic-gate 
84137c478bd9Sstevel@tonic-gate 	return ((usb_port_t)port);
84147c478bd9Sstevel@tonic-gate }
84157c478bd9Sstevel@tonic-gate 
84167c478bd9Sstevel@tonic-gate 
84177c478bd9Sstevel@tonic-gate /* check if child still exists */
84187c478bd9Sstevel@tonic-gate static dev_info_t *
hubd_get_child_dip(hubd_t * hubd,usb_port_t port)84197c478bd9Sstevel@tonic-gate hubd_get_child_dip(hubd_t *hubd, usb_port_t port)
84207c478bd9Sstevel@tonic-gate {
84217c478bd9Sstevel@tonic-gate 	dev_info_t *child_dip = hubd->h_children_dips[port];
84227c478bd9Sstevel@tonic-gate 
84237c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8424112116d8Sfb209375 	    "hubd_get_child_dip: hubd=0x%p, port=%d", (void *)hubd, port);
84257c478bd9Sstevel@tonic-gate 
84267c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
84277c478bd9Sstevel@tonic-gate 
84287c478bd9Sstevel@tonic-gate 	return (child_dip);
84297c478bd9Sstevel@tonic-gate }
84307c478bd9Sstevel@tonic-gate 
84317c478bd9Sstevel@tonic-gate 
84327c478bd9Sstevel@tonic-gate /*
84337c478bd9Sstevel@tonic-gate  * hubd_cfgadm_state:
84347c478bd9Sstevel@tonic-gate  *
84357c478bd9Sstevel@tonic-gate  *	child_dip list		port_state		cfgadm_state
84367c478bd9Sstevel@tonic-gate  *	--------------		----------		------------
84377c478bd9Sstevel@tonic-gate  *	!= NULL			connected		configured or
84387c478bd9Sstevel@tonic-gate  *							unconfigured
84397c478bd9Sstevel@tonic-gate  *	!= NULL			not connected		disconnect but
84407c478bd9Sstevel@tonic-gate  *							busy/still referenced
84417c478bd9Sstevel@tonic-gate  *	NULL			connected		logically disconnected
84427c478bd9Sstevel@tonic-gate  *	NULL			not connected		empty
84437c478bd9Sstevel@tonic-gate  */
84447c478bd9Sstevel@tonic-gate static uint_t
hubd_cfgadm_state(hubd_t * hubd,usb_port_t port)84457c478bd9Sstevel@tonic-gate hubd_cfgadm_state(hubd_t *hubd, usb_port_t port)
84467c478bd9Sstevel@tonic-gate {
84477c478bd9Sstevel@tonic-gate 	uint_t		state;
84487c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip = hubd_get_child_dip(hubd, port);
84497c478bd9Sstevel@tonic-gate 
84507c478bd9Sstevel@tonic-gate 	if (child_dip) {
84517c478bd9Sstevel@tonic-gate 		if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
84527c478bd9Sstevel@tonic-gate 			/*
84537c478bd9Sstevel@tonic-gate 			 * connected,  now check if driver exists
84547c478bd9Sstevel@tonic-gate 			 */
84557c478bd9Sstevel@tonic-gate 			if (DEVI_IS_DEVICE_OFFLINE(child_dip) ||
8456737d277aScth 			    !i_ddi_devi_attached(child_dip)) {
84577c478bd9Sstevel@tonic-gate 				state = HUBD_CFGADM_UNCONFIGURED;
84587c478bd9Sstevel@tonic-gate 			} else {
84597c478bd9Sstevel@tonic-gate 				state = HUBD_CFGADM_CONFIGURED;
84607c478bd9Sstevel@tonic-gate 			}
84617c478bd9Sstevel@tonic-gate 		} else {
84627c478bd9Sstevel@tonic-gate 			/*
84637c478bd9Sstevel@tonic-gate 			 * this means that the dip is around for
84647c478bd9Sstevel@tonic-gate 			 * a device that is still referenced but
84657c478bd9Sstevel@tonic-gate 			 * has been yanked out. So the cfgadm info
84667c478bd9Sstevel@tonic-gate 			 * for this state should be EMPTY (port empty)
84677c478bd9Sstevel@tonic-gate 			 * and CONFIGURED (dip still valid).
84687c478bd9Sstevel@tonic-gate 			 */
84697c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_STILL_REFERENCED;
84707c478bd9Sstevel@tonic-gate 		}
84717c478bd9Sstevel@tonic-gate 	} else {
84727c478bd9Sstevel@tonic-gate 		/* connected but no child dip */
84737c478bd9Sstevel@tonic-gate 		if (hubd->h_port_state[port] & PORT_STATUS_CCS) {
84747c478bd9Sstevel@tonic-gate 			/* logically disconnected */
84757c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_DISCONNECTED;
84767c478bd9Sstevel@tonic-gate 		} else {
84777c478bd9Sstevel@tonic-gate 			/* physically disconnected */
84787c478bd9Sstevel@tonic-gate 			state = HUBD_CFGADM_EMPTY;
84797c478bd9Sstevel@tonic-gate 		}
84807c478bd9Sstevel@tonic-gate 	}
84817c478bd9Sstevel@tonic-gate 
84827c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
84837c478bd9Sstevel@tonic-gate 	    "hubd_cfgadm_state: hubd=0x%p, port=%d state=0x%x",
8484112116d8Sfb209375 	    (void *)hubd, port, state);
84857c478bd9Sstevel@tonic-gate 
84867c478bd9Sstevel@tonic-gate 	return (state);
84877c478bd9Sstevel@tonic-gate }
84887c478bd9Sstevel@tonic-gate 
84897c478bd9Sstevel@tonic-gate 
84907c478bd9Sstevel@tonic-gate /*
84917c478bd9Sstevel@tonic-gate  * hubd_toggle_port:
84927c478bd9Sstevel@tonic-gate  */
84937c478bd9Sstevel@tonic-gate static int
hubd_toggle_port(hubd_t * hubd,usb_port_t port)84947c478bd9Sstevel@tonic-gate hubd_toggle_port(hubd_t *hubd, usb_port_t port)
84957c478bd9Sstevel@tonic-gate {
84967c478bd9Sstevel@tonic-gate 	int		wait;
84977c478bd9Sstevel@tonic-gate 	uint_t		retry;
84987c478bd9Sstevel@tonic-gate 	uint16_t	status;
84997c478bd9Sstevel@tonic-gate 	uint16_t	change;
85007c478bd9Sstevel@tonic-gate 
85017c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS,  hubd->h_log_handle,
8502112116d8Sfb209375 	    "hubd_toggle_port: hubd=0x%p, port=%d", (void *)hubd, port);
85037c478bd9Sstevel@tonic-gate 
85047c478bd9Sstevel@tonic-gate 	if ((hubd_disable_port_power(hubd, port)) != USB_SUCCESS) {
85057c478bd9Sstevel@tonic-gate 
85067c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
85077c478bd9Sstevel@tonic-gate 	}
85087c478bd9Sstevel@tonic-gate 
85097c478bd9Sstevel@tonic-gate 	/*
85107c478bd9Sstevel@tonic-gate 	 * see hubd_enable_all_port_power() which
85117c478bd9Sstevel@tonic-gate 	 * requires longer delay for hubs.
85127c478bd9Sstevel@tonic-gate 	 */
85137c478bd9Sstevel@tonic-gate 	mutex_exit(HUBD_MUTEX(hubd));
85147c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(hubd_device_delay / 10));
85157c478bd9Sstevel@tonic-gate 	mutex_enter(HUBD_MUTEX(hubd));
85167c478bd9Sstevel@tonic-gate 
85177c478bd9Sstevel@tonic-gate 	/*
85187c478bd9Sstevel@tonic-gate 	 * According to section 11.11 of USB, for hubs with no power
85197c478bd9Sstevel@tonic-gate 	 * switches, bPwrOn2PwrGood is zero. But we wait for some
85207c478bd9Sstevel@tonic-gate 	 * arbitrary time to enable power to become stable.
85217c478bd9Sstevel@tonic-gate 	 *
85227c478bd9Sstevel@tonic-gate 	 * If an hub supports port power swicthing, we need to wait
8523993e3fafSRobert Mustacchi 	 * at least 20ms before accesing corresonding usb port. Note
8524993e3fafSRobert Mustacchi 	 * this member is stored in the h_power_good member.
85257c478bd9Sstevel@tonic-gate 	 */
8526993e3fafSRobert Mustacchi 	if ((hubd->h_hub_chars & HUB_CHARS_NO_POWER_SWITCHING) ||
8527993e3fafSRobert Mustacchi 	    (hubd->h_power_good == 0)) {
85287c478bd9Sstevel@tonic-gate 		wait = hubd_device_delay / 10;
85297c478bd9Sstevel@tonic-gate 	} else {
85307c478bd9Sstevel@tonic-gate 		wait = max(HUB_DEFAULT_POPG,
8531993e3fafSRobert Mustacchi 		    hubd->h_power_good) * 2 * 1000;
85327c478bd9Sstevel@tonic-gate 	}
85337c478bd9Sstevel@tonic-gate 
85347c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_PORT, hubd->h_log_handle,
85357c478bd9Sstevel@tonic-gate 	    "hubd_toggle_port: popg=%d wait=%d",
8536993e3fafSRobert Mustacchi 	    hubd->h_power_good, wait);
85377c478bd9Sstevel@tonic-gate 
85387c478bd9Sstevel@tonic-gate 	retry = 0;
85397c478bd9Sstevel@tonic-gate 
85407c478bd9Sstevel@tonic-gate 	do {
85417c478bd9Sstevel@tonic-gate 		(void) hubd_enable_port_power(hubd, port);
85427c478bd9Sstevel@tonic-gate 
85437c478bd9Sstevel@tonic-gate 		mutex_exit(HUBD_MUTEX(hubd));
85447c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(wait));
85457c478bd9Sstevel@tonic-gate 		mutex_enter(HUBD_MUTEX(hubd));
85467c478bd9Sstevel@tonic-gate 
85477c478bd9Sstevel@tonic-gate 		/* Get port status */
85487c478bd9Sstevel@tonic-gate 		(void) hubd_determine_port_status(hubd, port,
8549993e3fafSRobert Mustacchi 		    &status, &change, NULL, 0);
85507c478bd9Sstevel@tonic-gate 
85517c478bd9Sstevel@tonic-gate 		/* For retry if any, use some extra delay */
85527c478bd9Sstevel@tonic-gate 		wait = max(wait, hubd_device_delay / 10);
85537c478bd9Sstevel@tonic-gate 
85547c478bd9Sstevel@tonic-gate 		retry++;
85557c478bd9Sstevel@tonic-gate 
85567c478bd9Sstevel@tonic-gate 	} while ((!(status & PORT_STATUS_PPS)) && (retry < HUBD_PORT_RETRY));
85577c478bd9Sstevel@tonic-gate 
85587c478bd9Sstevel@tonic-gate 	/* Print warning message if port has no power */
85597c478bd9Sstevel@tonic-gate 	if (!(status & PORT_STATUS_PPS)) {
85607c478bd9Sstevel@tonic-gate 
8561d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_PORT, hubd->h_log_handle,
85627c478bd9Sstevel@tonic-gate 		    "hubd_toggle_port: port %d power-on failed, "
85637c478bd9Sstevel@tonic-gate 		    "port status 0x%x", port, status);
85647c478bd9Sstevel@tonic-gate 
85657c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
85667c478bd9Sstevel@tonic-gate 	}
85677c478bd9Sstevel@tonic-gate 
85687c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
85697c478bd9Sstevel@tonic-gate }
857035f36846Ssl147100 
857135f36846Ssl147100 
857235f36846Ssl147100 /*
857335f36846Ssl147100  * hubd_init_power_budget:
857435f36846Ssl147100  *	Init power budget variables in hubd structure. According
857535f36846Ssl147100  *	to USB spec, the power budget rules are:
857635f36846Ssl147100  *	1. local-powered hubs including root-hubs can supply
857735f36846Ssl147100  *	   500mA to each port at maximum
857835f36846Ssl147100  *	2. two bus-powered hubs are not allowed to concatenate
857935f36846Ssl147100  *	3. bus-powered hubs can supply 100mA to each port at
858035f36846Ssl147100  *	   maximum, and the power consumed by all downstream
858135f36846Ssl147100  *	   ports and the hub itself cannot exceed the max power
858235f36846Ssl147100  *	   supplied by the upstream port, i.e., 500mA
858335f36846Ssl147100  *	The routine is only called during hub attach time
858435f36846Ssl147100  */
858535f36846Ssl147100 static int
hubd_init_power_budget(hubd_t * hubd)858635f36846Ssl147100 hubd_init_power_budget(hubd_t *hubd)
858735f36846Ssl147100 {
858835f36846Ssl147100 	uint16_t	status = 0;
858935f36846Ssl147100 	usba_device_t	*hubd_ud = NULL;
859035f36846Ssl147100 	size_t		size;
859135f36846Ssl147100 	usb_cfg_descr_t	cfg_descr;
859235f36846Ssl147100 	dev_info_t	*pdip = NULL;
859335f36846Ssl147100 	hubd_t		*phubd = NULL;
859435f36846Ssl147100 
859535f36846Ssl147100 	if (hubd->h_ignore_pwr_budget) {
859635f36846Ssl147100 
859735f36846Ssl147100 		return (USB_SUCCESS);
859835f36846Ssl147100 	}
859935f36846Ssl147100 
860035f36846Ssl147100 	USB_DPRINTF_L4(DPRINT_MASK_HUB, hubd->h_log_handle,
860135f36846Ssl147100 	    "hubd_init_power_budget:");
860235f36846Ssl147100 
860335f36846Ssl147100 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
860435f36846Ssl147100 	ASSERT(hubd->h_default_pipe != 0);
860535f36846Ssl147100 	mutex_exit(HUBD_MUTEX(hubd));
860635f36846Ssl147100 
860735f36846Ssl147100 	/* get device status */
860835f36846Ssl147100 	if ((usb_get_status(hubd->h_dip, hubd->h_default_pipe,
860935f36846Ssl147100 	    HUB_GET_DEVICE_STATUS_TYPE,
861035f36846Ssl147100 	    0, &status, 0)) != USB_SUCCESS) {
861135f36846Ssl147100 		mutex_enter(HUBD_MUTEX(hubd));
861235f36846Ssl147100 
861335f36846Ssl147100 		return (USB_FAILURE);
861435f36846Ssl147100 	}
861535f36846Ssl147100 
861635f36846Ssl147100 	hubd_ud = usba_get_usba_device(hubd->h_dip);
861735f36846Ssl147100 
861835f36846Ssl147100 	size = usb_parse_cfg_descr(hubd_ud->usb_cfg, hubd_ud->usb_cfg_length,
861935f36846Ssl147100 	    &cfg_descr, USB_CFG_DESCR_SIZE);
862035f36846Ssl147100 
862135f36846Ssl147100 	if (size != USB_CFG_DESCR_SIZE) {
862235f36846Ssl147100 		USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
862335f36846Ssl147100 		    "get hub configuration descriptor failed");
862435f36846Ssl147100 		mutex_enter(HUBD_MUTEX(hubd));
862535f36846Ssl147100 
862635f36846Ssl147100 		return (USB_FAILURE);
862735f36846Ssl147100 	}
862835f36846Ssl147100 
862935f36846Ssl147100 	mutex_enter(HUBD_MUTEX(hubd));
863035f36846Ssl147100 
863135f36846Ssl147100 	hubd->h_local_pwr_capable = (cfg_descr.bmAttributes &
863235f36846Ssl147100 	    USB_CFG_ATTR_SELFPWR);
863335f36846Ssl147100 
863435f36846Ssl147100 	if (hubd->h_local_pwr_capable) {
863535f36846Ssl147100 		USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
863635f36846Ssl147100 		    "hub is capable of local power");
863735f36846Ssl147100 	}
863835f36846Ssl147100 
863935f36846Ssl147100 	hubd->h_local_pwr_on = (status &
864035f36846Ssl147100 	    USB_DEV_SLF_PWRD_STATUS) && hubd->h_local_pwr_capable;
864135f36846Ssl147100 
864235f36846Ssl147100 	if (hubd->h_local_pwr_on) {
864335f36846Ssl147100 		USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
864435f36846Ssl147100 		    "hub is local-powered");
864535f36846Ssl147100 
864635f36846Ssl147100 		hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
864735f36846Ssl147100 		    USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
864835f36846Ssl147100 	} else {
864935f36846Ssl147100 		hubd->h_pwr_limit = (USB_PWR_UNIT_LOAD *
865035f36846Ssl147100 		    USB_LOW_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
865135f36846Ssl147100 
865235f36846Ssl147100 		hubd->h_pwr_left = (USB_PWR_UNIT_LOAD *
865335f36846Ssl147100 		    USB_HIGH_PWR_VALUE) / USB_CFG_DESCR_PWR_UNIT;
865435f36846Ssl147100 
865535f36846Ssl147100 		ASSERT(!usba_is_root_hub(hubd->h_dip));
865635f36846Ssl147100 
865735f36846Ssl147100 		if (!usba_is_root_hub(hubd->h_dip)) {
865835f36846Ssl147100 			/*
865935f36846Ssl147100 			 * two bus-powered hubs are not
866035f36846Ssl147100 			 * allowed to be concatenated
866135f36846Ssl147100 			 */
866235f36846Ssl147100 			mutex_exit(HUBD_MUTEX(hubd));
866335f36846Ssl147100 
866435f36846Ssl147100 			pdip = ddi_get_parent(hubd->h_dip);
866535f36846Ssl147100 			phubd = hubd_get_soft_state(pdip);
866635f36846Ssl147100 			ASSERT(phubd != NULL);
866735f36846Ssl147100 
866835f36846Ssl147100 			if (!phubd->h_ignore_pwr_budget) {
866935f36846Ssl147100 				mutex_enter(HUBD_MUTEX(phubd));
867035f36846Ssl147100 				if (phubd->h_local_pwr_on == B_FALSE) {
86718668df41Slg150142 					USB_DPRINTF_L1(DPRINT_MASK_HUB,
867235f36846Ssl147100 					    hubd->h_log_handle,
867335f36846Ssl147100 					    "two bus-powered hubs cannot "
867435f36846Ssl147100 					    "be concatenated");
867535f36846Ssl147100 
867635f36846Ssl147100 					mutex_exit(HUBD_MUTEX(phubd));
867735f36846Ssl147100 					mutex_enter(HUBD_MUTEX(hubd));
867835f36846Ssl147100 
867935f36846Ssl147100 					return (USB_FAILURE);
868035f36846Ssl147100 				}
868135f36846Ssl147100 				mutex_exit(HUBD_MUTEX(phubd));
868235f36846Ssl147100 			}
868335f36846Ssl147100 
868435f36846Ssl147100 			mutex_enter(HUBD_MUTEX(hubd));
868535f36846Ssl147100 
868635f36846Ssl147100 			USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
868735f36846Ssl147100 			    "hub is bus-powered");
868835f36846Ssl147100 		} else {
868935f36846Ssl147100 			USB_DPRINTF_L3(DPRINT_MASK_HUB, hubd->h_log_handle,
869035f36846Ssl147100 			    "root-hub must be local-powered");
869135f36846Ssl147100 		}
869235f36846Ssl147100 
869335f36846Ssl147100 		/*
869435f36846Ssl147100 		 * Subtract the power consumed by the hub itself
869535f36846Ssl147100 		 * and get the power that can be supplied to
869635f36846Ssl147100 		 * downstream ports
869735f36846Ssl147100 		 */
8698993e3fafSRobert Mustacchi 		hubd->h_pwr_left -= hubd->h_current / USB_CFG_DESCR_PWR_UNIT;
869935f36846Ssl147100 		if (hubd->h_pwr_left < 0) {
870035f36846Ssl147100 			USB_DPRINTF_L2(DPRINT_MASK_HUB, hubd->h_log_handle,
870135f36846Ssl147100 			    "hubd->h_pwr_left is less than bHubContrCurrent, "
870235f36846Ssl147100 			    "should fail");
870335f36846Ssl147100 
870435f36846Ssl147100 			return (USB_FAILURE);
870535f36846Ssl147100 		}
870635f36846Ssl147100 	}
870735f36846Ssl147100 
870835f36846Ssl147100 	return (USB_SUCCESS);
870935f36846Ssl147100 }
871035f36846Ssl147100 
871135f36846Ssl147100 
871235f36846Ssl147100 /*
871335f36846Ssl147100  * usba_hubdi_check_power_budget:
871435f36846Ssl147100  *	Check if the hub has enough power budget to allow a
871535f36846Ssl147100  *	child device to select a configuration of config_index.
871635f36846Ssl147100  */
871735f36846Ssl147100 int
usba_hubdi_check_power_budget(dev_info_t * dip,usba_device_t * child_ud,uint_t config_index)871835f36846Ssl147100 usba_hubdi_check_power_budget(dev_info_t *dip, usba_device_t *child_ud,
871935f36846Ssl147100     uint_t config_index)
872035f36846Ssl147100 {
872135f36846Ssl147100 	int16_t		pwr_left, pwr_limit, pwr_required;
872235f36846Ssl147100 	size_t		size;
872335f36846Ssl147100 	usb_cfg_descr_t cfg_descr;
872435f36846Ssl147100 	hubd_t		*hubd;
872535f36846Ssl147100 
872635f36846Ssl147100 	if ((hubd = hubd_get_soft_state(dip)) == NULL) {
872735f36846Ssl147100 
872835f36846Ssl147100 		return (USB_FAILURE);
872935f36846Ssl147100 	}
873035f36846Ssl147100 
873135f36846Ssl147100 	if (hubd->h_ignore_pwr_budget) {
873235f36846Ssl147100 
873335f36846Ssl147100 		return (USB_SUCCESS);
873435f36846Ssl147100 	}
873535f36846Ssl147100 
873635f36846Ssl147100 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
873735f36846Ssl147100 	    "usba_hubdi_check_power_budget: "
8738112116d8Sfb209375 	    "dip=0x%p child_ud=0x%p conf_index=%d", (void *)dip,
8739112116d8Sfb209375 	    (void *)child_ud, config_index);
874035f36846Ssl147100 
874135f36846Ssl147100 	mutex_enter(HUBD_MUTEX(hubd));
874235f36846Ssl147100 	pwr_limit = hubd->h_pwr_limit;
874335f36846Ssl147100 	if (hubd->h_local_pwr_on == B_FALSE) {
874435f36846Ssl147100 		pwr_left = hubd->h_pwr_left;
874535f36846Ssl147100 		pwr_limit = (pwr_limit <= pwr_left) ? pwr_limit : pwr_left;
874635f36846Ssl147100 	}
874735f36846Ssl147100 	mutex_exit(HUBD_MUTEX(hubd));
874835f36846Ssl147100 
874935f36846Ssl147100 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
875035f36846Ssl147100 	    "usba_hubdi_check_power_budget: "
875135f36846Ssl147100 	    "available power is %dmA", pwr_limit * USB_CFG_DESCR_PWR_UNIT);
875235f36846Ssl147100 
875335f36846Ssl147100 	size = usb_parse_cfg_descr(
875435f36846Ssl147100 	    child_ud->usb_cfg_array[config_index], USB_CFG_DESCR_SIZE,
875535f36846Ssl147100 	    &cfg_descr, USB_CFG_DESCR_SIZE);
875635f36846Ssl147100 
875735f36846Ssl147100 	if (size != USB_CFG_DESCR_SIZE) {
875835f36846Ssl147100 		USB_DPRINTF_L2(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
875935f36846Ssl147100 		    "get hub configuration descriptor failed");
876035f36846Ssl147100 
876135f36846Ssl147100 		return (USB_FAILURE);
876235f36846Ssl147100 	}
876335f36846Ssl147100 
876435f36846Ssl147100 	pwr_required = cfg_descr.bMaxPower;
876535f36846Ssl147100 
876635f36846Ssl147100 	USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
876735f36846Ssl147100 	    "usba_hubdi_check_power_budget: "
876835f36846Ssl147100 	    "child bmAttributes=0x%x bMaxPower=%d "
876935f36846Ssl147100 	    "with config_index=%d", cfg_descr.bmAttributes,
877035f36846Ssl147100 	    pwr_required, config_index);
877135f36846Ssl147100 
877235f36846Ssl147100 	if (pwr_required > pwr_limit) {
87738668df41Slg150142 		USB_DPRINTF_L1(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
877435f36846Ssl147100 		    "configuration %d for device %s %s at port %d "
877535f36846Ssl147100 		    "exceeds power available for this port, please "
877635f36846Ssl147100 		    "re-insert your device into another hub port which "
877735f36846Ssl147100 		    "has enough power",
877835f36846Ssl147100 		    config_index,
877935f36846Ssl147100 		    child_ud->usb_mfg_str,
878035f36846Ssl147100 		    child_ud->usb_product_str,
878135f36846Ssl147100 		    child_ud->usb_port);
878235f36846Ssl147100 
878335f36846Ssl147100 		return (USB_FAILURE);
878435f36846Ssl147100 	}
878535f36846Ssl147100 
878635f36846Ssl147100 	return (USB_SUCCESS);
878735f36846Ssl147100 }
878835f36846Ssl147100 
878935f36846Ssl147100 
879035f36846Ssl147100 /*
879135f36846Ssl147100  * usba_hubdi_incr_power_budget:
879235f36846Ssl147100  *	Increase the hub power budget value when a child device
879335f36846Ssl147100  *	is removed from a bus-powered hub port.
879435f36846Ssl147100  */
879535f36846Ssl147100 void
usba_hubdi_incr_power_budget(dev_info_t * dip,usba_device_t * child_ud)879635f36846Ssl147100 usba_hubdi_incr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
879735f36846Ssl147100 {
879835f36846Ssl147100 	uint16_t	pwr_value;
879935f36846Ssl147100 	hubd_t		*hubd = hubd_get_soft_state(dip);
880035f36846Ssl147100 
880135f36846Ssl147100 	ASSERT(hubd != NULL);
880235f36846Ssl147100 
880335f36846Ssl147100 	if (hubd->h_ignore_pwr_budget) {
880435f36846Ssl147100 
880535f36846Ssl147100 		return;
880635f36846Ssl147100 	}
880735f36846Ssl147100 
880835f36846Ssl147100 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
880935f36846Ssl147100 	    "usba_hubdi_incr_power_budget: "
8810112116d8Sfb209375 	    "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
881135f36846Ssl147100 
881235f36846Ssl147100 	mutex_enter(HUBD_MUTEX(hubd));
881335f36846Ssl147100 	if (hubd->h_local_pwr_on == B_TRUE) {
881435f36846Ssl147100 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
881535f36846Ssl147100 		    "usba_hubdi_incr_power_budget: "
881635f36846Ssl147100 		    "hub is local powered");
881735f36846Ssl147100 		mutex_exit(HUBD_MUTEX(hubd));
881835f36846Ssl147100 
881935f36846Ssl147100 		return;
882035f36846Ssl147100 	}
882135f36846Ssl147100 	mutex_exit(HUBD_MUTEX(hubd));
882235f36846Ssl147100 
882335f36846Ssl147100 	mutex_enter(&child_ud->usb_mutex);
882435f36846Ssl147100 	if (child_ud->usb_pwr_from_hub == 0) {
882535f36846Ssl147100 		mutex_exit(&child_ud->usb_mutex);
882635f36846Ssl147100 
882735f36846Ssl147100 		return;
882835f36846Ssl147100 	}
882935f36846Ssl147100 	pwr_value = child_ud->usb_pwr_from_hub;
883035f36846Ssl147100 	mutex_exit(&child_ud->usb_mutex);
883135f36846Ssl147100 
883235f36846Ssl147100 	mutex_enter(HUBD_MUTEX(hubd));
883335f36846Ssl147100 	hubd->h_pwr_left += pwr_value;
883435f36846Ssl147100 
883535f36846Ssl147100 	USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
883635f36846Ssl147100 	    "usba_hubdi_incr_power_budget: "
883735f36846Ssl147100 	    "available power is %dmA, increased by %dmA",
883835f36846Ssl147100 	    hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
883935f36846Ssl147100 	    pwr_value * USB_CFG_DESCR_PWR_UNIT);
884035f36846Ssl147100 
884135f36846Ssl147100 	mutex_exit(HUBD_MUTEX(hubd));
884235f36846Ssl147100 
884335f36846Ssl147100 	mutex_enter(&child_ud->usb_mutex);
884435f36846Ssl147100 	child_ud->usb_pwr_from_hub = 0;
884535f36846Ssl147100 	mutex_exit(&child_ud->usb_mutex);
884635f36846Ssl147100 }
884735f36846Ssl147100 
884835f36846Ssl147100 
884935f36846Ssl147100 /*
885035f36846Ssl147100  * usba_hubdi_decr_power_budget:
885135f36846Ssl147100  *	Decrease the hub power budget value when a child device
885235f36846Ssl147100  *	is inserted to a bus-powered hub port.
885335f36846Ssl147100  */
885435f36846Ssl147100 void
usba_hubdi_decr_power_budget(dev_info_t * dip,usba_device_t * child_ud)885535f36846Ssl147100 usba_hubdi_decr_power_budget(dev_info_t *dip, usba_device_t *child_ud)
885635f36846Ssl147100 {
885735f36846Ssl147100 	uint16_t	pwr_value;
885835f36846Ssl147100 	size_t		size;
885935f36846Ssl147100 	usb_cfg_descr_t	cfg_descr;
886035f36846Ssl147100 	hubd_t		*hubd = hubd_get_soft_state(dip);
886135f36846Ssl147100 
886235f36846Ssl147100 	ASSERT(hubd != NULL);
886335f36846Ssl147100 
886435f36846Ssl147100 	if (hubd->h_ignore_pwr_budget) {
886535f36846Ssl147100 
886635f36846Ssl147100 		return;
886735f36846Ssl147100 	}
886835f36846Ssl147100 
886935f36846Ssl147100 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, hubd->h_log_handle,
887035f36846Ssl147100 	    "usba_hubdi_decr_power_budget: "
8871112116d8Sfb209375 	    "dip=0x%p child_ud=0x%p", (void *)dip, (void *)child_ud);
887235f36846Ssl147100 
887335f36846Ssl147100 	mutex_enter(HUBD_MUTEX(hubd));
887435f36846Ssl147100 	if (hubd->h_local_pwr_on == B_TRUE) {
887535f36846Ssl147100 		USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
887635f36846Ssl147100 		    "usba_hubdi_decr_power_budget: "
887735f36846Ssl147100 		    "hub is local powered");
887835f36846Ssl147100 		mutex_exit(HUBD_MUTEX(hubd));
887935f36846Ssl147100 
888035f36846Ssl147100 		return;
888135f36846Ssl147100 	}
888235f36846Ssl147100 	mutex_exit(HUBD_MUTEX(hubd));
888335f36846Ssl147100 
888435f36846Ssl147100 	mutex_enter(&child_ud->usb_mutex);
888535f36846Ssl147100 	if (child_ud->usb_pwr_from_hub > 0) {
888635f36846Ssl147100 		mutex_exit(&child_ud->usb_mutex);
888735f36846Ssl147100 
888835f36846Ssl147100 		return;
888935f36846Ssl147100 	}
889035f36846Ssl147100 	mutex_exit(&child_ud->usb_mutex);
889135f36846Ssl147100 
889235f36846Ssl147100 	size = usb_parse_cfg_descr(
889335f36846Ssl147100 	    child_ud->usb_cfg, child_ud->usb_cfg_length,
889435f36846Ssl147100 	    &cfg_descr, USB_CFG_DESCR_SIZE);
889535f36846Ssl147100 	ASSERT(size == USB_CFG_DESCR_SIZE);
889635f36846Ssl147100 
889735f36846Ssl147100 	mutex_enter(HUBD_MUTEX(hubd));
889835f36846Ssl147100 	pwr_value = cfg_descr.bMaxPower;
889935f36846Ssl147100 	hubd->h_pwr_left -= pwr_value;
890035f36846Ssl147100 	ASSERT(hubd->h_pwr_left >= 0);
890135f36846Ssl147100 
890235f36846Ssl147100 	USB_DPRINTF_L3(DPRINT_MASK_ATTA, hubd->h_log_handle,
890335f36846Ssl147100 	    "usba_hubdi_decr_power_budget: "
890435f36846Ssl147100 	    "available power is %dmA, decreased by %dmA",
890535f36846Ssl147100 	    hubd->h_pwr_left * USB_CFG_DESCR_PWR_UNIT,
890635f36846Ssl147100 	    pwr_value * USB_CFG_DESCR_PWR_UNIT);
890735f36846Ssl147100 
890835f36846Ssl147100 	mutex_exit(HUBD_MUTEX(hubd));
890935f36846Ssl147100 
891035f36846Ssl147100 	mutex_enter(&child_ud->usb_mutex);
891135f36846Ssl147100 	child_ud->usb_pwr_from_hub = pwr_value;
891235f36846Ssl147100 	mutex_exit(&child_ud->usb_mutex);
891335f36846Ssl147100 }
8914ffcd51f3Slg150142 
8915ffcd51f3Slg150142 /*
8916ffcd51f3Slg150142  * hubd_wait_for_hotplug_exit:
8917ffcd51f3Slg150142  *	Waiting for the exit of the running hotplug thread or ioctl thread.
8918ffcd51f3Slg150142  */
8919ffcd51f3Slg150142 static int
hubd_wait_for_hotplug_exit(hubd_t * hubd)8920ffcd51f3Slg150142 hubd_wait_for_hotplug_exit(hubd_t *hubd)
8921ffcd51f3Slg150142 {
8922d3d50737SRafael Vanoni 	clock_t		until = drv_usectohz(1000000);
8923ffcd51f3Slg150142 	int		rval;
8924ffcd51f3Slg150142 
8925ffcd51f3Slg150142 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
8926ffcd51f3Slg150142 
8927ffcd51f3Slg150142 	if (hubd->h_hotplug_thread) {
8928ffcd51f3Slg150142 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8929ffcd51f3Slg150142 		    "waiting for hubd hotplug thread exit");
8930d3d50737SRafael Vanoni 		rval = cv_reltimedwait(&hubd->h_cv_hotplug_dev,
8931d3d50737SRafael Vanoni 		    &hubd->h_mutex, until, TR_CLOCK_TICK);
8932ffcd51f3Slg150142 
8933ffcd51f3Slg150142 		if ((rval <= 0) && (hubd->h_hotplug_thread)) {
8934ffcd51f3Slg150142 
8935ffcd51f3Slg150142 			return (USB_FAILURE);
8936ffcd51f3Slg150142 		}
8937ffcd51f3Slg150142 	}
8938ffcd51f3Slg150142 
8939ffcd51f3Slg150142 	return (USB_SUCCESS);
8940ffcd51f3Slg150142 }
8941ffcd51f3Slg150142 
8942ffcd51f3Slg150142 /*
8943ffcd51f3Slg150142  * hubd_reset_thread:
8944ffcd51f3Slg150142  *	handles the "USB_RESET_LVL_REATTACH" reset of usb device.
8945ffcd51f3Slg150142  *
8946ffcd51f3Slg150142  *	- delete the child (force detaching the device and its children)
8947ffcd51f3Slg150142  *	- reset the corresponding parent hub port
8948ffcd51f3Slg150142  *	- create the child (force re-attaching the device and its children)
8949ffcd51f3Slg150142  */
8950ffcd51f3Slg150142 static void
hubd_reset_thread(void * arg)8951ffcd51f3Slg150142 hubd_reset_thread(void *arg)
8952ffcd51f3Slg150142 {
8953ffcd51f3Slg150142 	hubd_reset_arg_t *hd_arg = (hubd_reset_arg_t *)arg;
8954ffcd51f3Slg150142 	hubd_t		*hubd = hd_arg->hubd;
8955ffcd51f3Slg150142 	uint16_t	reset_port = hd_arg->reset_port;
8956ffcd51f3Slg150142 	uint16_t	status, change;
8957ffcd51f3Slg150142 	hub_power_t	*hubpm;
8958ffcd51f3Slg150142 	dev_info_t	*hdip = hubd->h_dip;
8959ffcd51f3Slg150142 	dev_info_t	*rh_dip = hubd->h_usba_device->usb_root_hub_dip;
8960ffcd51f3Slg150142 	dev_info_t	*child_dip;
8961ffcd51f3Slg150142 	boolean_t	online_child = B_FALSE;
89623fe80ca4SDan Cross 	int		devinst;
8963ffcd51f3Slg150142 	char		*devname;
8964aa041649SRaymond Chen 	int		i = 0;
8965aa041649SRaymond Chen 	int		rval = USB_FAILURE;
8966ffcd51f3Slg150142 
8967ffcd51f3Slg150142 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8968ffcd51f3Slg150142 	    "hubd_reset_thread:  started, hubd_reset_port = 0x%x", reset_port);
8969ffcd51f3Slg150142 
8970ffcd51f3Slg150142 	kmem_free(arg, sizeof (hubd_reset_arg_t));
8971ffcd51f3Slg150142 
8972ffcd51f3Slg150142 	mutex_enter(HUBD_MUTEX(hubd));
8973ffcd51f3Slg150142 
8974ffcd51f3Slg150142 	child_dip = hubd->h_children_dips[reset_port];
8975ffcd51f3Slg150142 	ASSERT(child_dip != NULL);
8976ffcd51f3Slg150142 
8977ffcd51f3Slg150142 	devname = (char *)ddi_driver_name(child_dip);
8978ffcd51f3Slg150142 	devinst = ddi_get_instance(child_dip);
8979ffcd51f3Slg150142 
8980ffcd51f3Slg150142 	/* if our bus power entry point is active, quit the reset */
8981ffcd51f3Slg150142 	if (hubd->h_bus_pwr) {
8982ffcd51f3Slg150142 		USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
8983ffcd51f3Slg150142 		    "%s%d is under bus power management, cannot be reset. "
8984ffcd51f3Slg150142 		    "Please disconnect and reconnect this device.",
8985ffcd51f3Slg150142 		    devname, devinst);
8986ffcd51f3Slg150142 
8987ffcd51f3Slg150142 		goto Fail;
8988ffcd51f3Slg150142 	}
8989ffcd51f3Slg150142 
8990ffcd51f3Slg150142 	if (hubd_wait_for_hotplug_exit(hubd) == USB_FAILURE) {
8991ffcd51f3Slg150142 		/* we got woken up because of a timeout */
8992ffcd51f3Slg150142 		USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG,
8993ffcd51f3Slg150142 		    hubd->h_log_handle, "Time out when resetting the device"
8994ffcd51f3Slg150142 		    " %s%d. Please disconnect and reconnect this device.",
8995ffcd51f3Slg150142 		    devname, devinst);
8996ffcd51f3Slg150142 
8997ffcd51f3Slg150142 		goto Fail;
8998ffcd51f3Slg150142 	}
8999ffcd51f3Slg150142 
9000ffcd51f3Slg150142 	hubd->h_hotplug_thread++;
9001ffcd51f3Slg150142 
9002ffcd51f3Slg150142 	/* is this the root hub? */
9003ffcd51f3Slg150142 	if ((hdip == rh_dip) &&
9004ffcd51f3Slg150142 	    (hubd->h_dev_state == USB_DEV_PWRED_DOWN)) {
9005ffcd51f3Slg150142 		hubpm = hubd->h_hubpm;
9006ffcd51f3Slg150142 
9007ffcd51f3Slg150142 		/* mark the root hub as full power */
9008ffcd51f3Slg150142 		hubpm->hubp_current_power = USB_DEV_OS_FULL_PWR;
9009e5815e7aSJosef 'Jeff' Sipek 		hubpm->hubp_time_at_full_power = gethrtime();
9010ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
9011ffcd51f3Slg150142 
9012ffcd51f3Slg150142 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
9013ffcd51f3Slg150142 		    "hubd_reset_thread: call pm_power_has_changed");
9014ffcd51f3Slg150142 
9015ffcd51f3Slg150142 		(void) pm_power_has_changed(hdip, 0,
9016ffcd51f3Slg150142 		    USB_DEV_OS_FULL_PWR);
9017ffcd51f3Slg150142 
9018ffcd51f3Slg150142 		mutex_enter(HUBD_MUTEX(hubd));
9019ffcd51f3Slg150142 		hubd->h_dev_state = USB_DEV_ONLINE;
9020ffcd51f3Slg150142 	}
9021ffcd51f3Slg150142 
9022ffcd51f3Slg150142 	mutex_exit(HUBD_MUTEX(hubd));
9023ffcd51f3Slg150142 
9024ffcd51f3Slg150142 	/*
9025ffcd51f3Slg150142 	 * this ensures one reset activity per system at a time.
9026ffcd51f3Slg150142 	 * we enter the parent PCI node to have this serialization.
9027ffcd51f3Slg150142 	 * this also excludes ioctls and deathrow thread
9028ffcd51f3Slg150142 	 */
90293fe80ca4SDan Cross 	ndi_devi_enter(ddi_get_parent(rh_dip));
90303fe80ca4SDan Cross 	ndi_devi_enter(rh_dip);
9031ffcd51f3Slg150142 
9032ffcd51f3Slg150142 	/* exclude other threads */
90333fe80ca4SDan Cross 	ndi_devi_enter(hdip);
9034ffcd51f3Slg150142 	mutex_enter(HUBD_MUTEX(hubd));
9035ffcd51f3Slg150142 
9036ffcd51f3Slg150142 	/*
9037ffcd51f3Slg150142 	 * We need to make sure that the child is still online for a hotplug
9038ffcd51f3Slg150142 	 * thread could have inserted which detached the child.
9039ffcd51f3Slg150142 	 */
9040ffcd51f3Slg150142 	if (hubd->h_children_dips[reset_port]) {
9041ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
9042ffcd51f3Slg150142 		/* First disconnect the device */
9043ffcd51f3Slg150142 		hubd_post_event(hubd, reset_port, USBA_EVENT_TAG_HOT_REMOVAL);
9044aa041649SRaymond Chen 
9045aa041649SRaymond Chen 		/* delete cached dv_node's but drop locks first */
90463fe80ca4SDan Cross 		ndi_devi_exit(hdip);
90473fe80ca4SDan Cross 		ndi_devi_exit(rh_dip);
90483fe80ca4SDan Cross 		ndi_devi_exit(ddi_get_parent(rh_dip));
9049aa041649SRaymond Chen 
9050aa041649SRaymond Chen 		(void) devfs_clean(rh_dip, NULL, DV_CLEAN_FORCE);
9051aa041649SRaymond Chen 
9052aa041649SRaymond Chen 		/*
9053aa041649SRaymond Chen 		 * workaround only for storage device. When it's able to force
9054aa041649SRaymond Chen 		 * detach a driver, this code can be removed safely.
9055aa041649SRaymond Chen 		 *
9056aa041649SRaymond Chen 		 * If we're to reset storage device and the device is used, we
9057aa041649SRaymond Chen 		 * will wait at most extra 20s for applications to exit and
9058aa041649SRaymond Chen 		 * close the device. This is especially useful for HAL-based
9059aa041649SRaymond Chen 		 * applications.
9060aa041649SRaymond Chen 		 */
9061aa041649SRaymond Chen 		if ((strcmp(devname, "scsa2usb") == 0) &&
9062aa041649SRaymond Chen 		    DEVI(child_dip)->devi_ref != 0) {
9063aa041649SRaymond Chen 			while (i++ < hubdi_reset_delay) {
9064aa041649SRaymond Chen 				mutex_enter(HUBD_MUTEX(hubd));
9065aa041649SRaymond Chen 				rval = hubd_delete_child(hubd, reset_port,
9066aa041649SRaymond Chen 				    NDI_DEVI_REMOVE, B_FALSE);
9067aa041649SRaymond Chen 				mutex_exit(HUBD_MUTEX(hubd));
9068aa041649SRaymond Chen 				if (rval == USB_SUCCESS)
9069aa041649SRaymond Chen 					break;
9070aa041649SRaymond Chen 
9071aa041649SRaymond Chen 				delay(drv_usectohz(1000000)); /* 1s */
9072aa041649SRaymond Chen 			}
9073aa041649SRaymond Chen 		}
9074aa041649SRaymond Chen 
90753fe80ca4SDan Cross 		ndi_devi_enter(ddi_get_parent(rh_dip));
90763fe80ca4SDan Cross 		ndi_devi_enter(rh_dip);
90773fe80ca4SDan Cross 		ndi_devi_enter(hdip);
9078aa041649SRaymond Chen 
9079ffcd51f3Slg150142 		mutex_enter(HUBD_MUTEX(hubd));
9080ffcd51f3Slg150142 
9081ffcd51f3Slg150142 		/* Then force detaching the device */
9082aa041649SRaymond Chen 		if ((rval != USB_SUCCESS) && (hubd_delete_child(hubd,
9083aa041649SRaymond Chen 		    reset_port, NDI_DEVI_REMOVE, B_FALSE) != USB_SUCCESS)) {
9084ffcd51f3Slg150142 			USB_DPRINTF_L0(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
9085ffcd51f3Slg150142 			    "%s%d cannot be reset due to other applications "
9086ffcd51f3Slg150142 			    "are using it, please first close these "
9087ffcd51f3Slg150142 			    "applications, then disconnect and reconnect"
9088ffcd51f3Slg150142 			    "the device.", devname, devinst);
9089ffcd51f3Slg150142 
9090ffcd51f3Slg150142 			mutex_exit(HUBD_MUTEX(hubd));
9091ffcd51f3Slg150142 			/* post a re-connect event */
9092ffcd51f3Slg150142 			hubd_post_event(hubd, reset_port,
9093ffcd51f3Slg150142 			    USBA_EVENT_TAG_HOT_INSERTION);
9094ffcd51f3Slg150142 			mutex_enter(HUBD_MUTEX(hubd));
9095ffcd51f3Slg150142 		} else {
9096ffcd51f3Slg150142 			(void) hubd_determine_port_status(hubd, reset_port,
9097993e3fafSRobert Mustacchi 			    &status, &change, NULL, HUBD_ACK_ALL_CHANGES);
9098ffcd51f3Slg150142 
9099ffcd51f3Slg150142 			/* Reset the parent hubd port and create new child */
9100ffcd51f3Slg150142 			if (status & PORT_STATUS_CCS) {
9101ffcd51f3Slg150142 				online_child |=	(hubd_handle_port_connect(hubd,
9102ffcd51f3Slg150142 				    reset_port) == USB_SUCCESS);
9103ffcd51f3Slg150142 			}
9104ffcd51f3Slg150142 		}
9105ffcd51f3Slg150142 	}
9106ffcd51f3Slg150142 
9107ffcd51f3Slg150142 	/* release locks so we can do a devfs_clean */
9108ffcd51f3Slg150142 	mutex_exit(HUBD_MUTEX(hubd));
9109ffcd51f3Slg150142 
9110ffcd51f3Slg150142 	/* delete cached dv_node's but drop locks first */
91113fe80ca4SDan Cross 	ndi_devi_exit(hdip);
91123fe80ca4SDan Cross 	ndi_devi_exit(rh_dip);
91133fe80ca4SDan Cross 	ndi_devi_exit(ddi_get_parent(rh_dip));
9114ffcd51f3Slg150142 
9115ffcd51f3Slg150142 	(void) devfs_clean(rh_dip, NULL, 0);
9116ffcd51f3Slg150142 
9117ffcd51f3Slg150142 	/* now check if any children need onlining */
9118ffcd51f3Slg150142 	if (online_child) {
9119ffcd51f3Slg150142 		USB_DPRINTF_L3(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
9120ffcd51f3Slg150142 		    "hubd_reset_thread: onlining children");
9121ffcd51f3Slg150142 
9122ffcd51f3Slg150142 		(void) ndi_devi_online(hubd->h_dip, 0);
9123ffcd51f3Slg150142 	}
9124ffcd51f3Slg150142 
9125ffcd51f3Slg150142 	mutex_enter(HUBD_MUTEX(hubd));
9126ffcd51f3Slg150142 
9127ffcd51f3Slg150142 	/* allow hotplug thread now */
9128ffcd51f3Slg150142 	hubd->h_hotplug_thread--;
9129ffcd51f3Slg150142 Fail:
9130ffcd51f3Slg150142 	hubd_start_polling(hubd, 0);
9131ffcd51f3Slg150142 
9132ffcd51f3Slg150142 	/* mark this device as idle */
9133ffcd51f3Slg150142 	(void) hubd_pm_idle_component(hubd, hubd->h_dip, 0);
9134ffcd51f3Slg150142 
9135ffcd51f3Slg150142 	USB_DPRINTF_L4(DPRINT_MASK_HOTPLUG, hubd->h_log_handle,
9136ffcd51f3Slg150142 	    "hubd_reset_thread: exit, %d", hubd->h_hotplug_thread);
9137ffcd51f3Slg150142 
9138ffcd51f3Slg150142 	hubd->h_reset_port[reset_port] = B_FALSE;
9139ffcd51f3Slg150142 
9140ffcd51f3Slg150142 	mutex_exit(HUBD_MUTEX(hubd));
9141ffcd51f3Slg150142 
9142ffcd51f3Slg150142 	ndi_rele_devi(hdip);
9143ffcd51f3Slg150142 }
9144ffcd51f3Slg150142 
9145ffcd51f3Slg150142 /*
9146ffcd51f3Slg150142  * hubd_check_same_device:
9147ffcd51f3Slg150142  *	- open the default pipe of the device.
9148ffcd51f3Slg150142  *	- compare the old and new descriptors of the device.
9149ffcd51f3Slg150142  *	- close the default pipe.
9150ffcd51f3Slg150142  */
9151ffcd51f3Slg150142 static int
hubd_check_same_device(hubd_t * hubd,usb_port_t port)9152ffcd51f3Slg150142 hubd_check_same_device(hubd_t *hubd, usb_port_t port)
9153ffcd51f3Slg150142 {
9154ffcd51f3Slg150142 	dev_info_t		*dip = hubd->h_children_dips[port];
9155ffcd51f3Slg150142 	usb_pipe_handle_t	ph;
9156ffcd51f3Slg150142 	int			rval = USB_FAILURE;
9157ffcd51f3Slg150142 
9158ffcd51f3Slg150142 	ASSERT(mutex_owned(HUBD_MUTEX(hubd)));
9159ffcd51f3Slg150142 
9160ffcd51f3Slg150142 	mutex_exit(HUBD_MUTEX(hubd));
9161ffcd51f3Slg150142 	/* Open the default pipe to operate the device */
9162ffcd51f3Slg150142 	if (usb_pipe_open(dip, NULL, NULL,
9163ffcd51f3Slg150142 	    USB_FLAGS_SLEEP| USBA_FLAGS_PRIVILEGED,
9164ffcd51f3Slg150142 	    &ph) == USB_SUCCESS) {
9165ffcd51f3Slg150142 		/*
9166ffcd51f3Slg150142 		 * Check that if the device's descriptors are different
9167ffcd51f3Slg150142 		 * from the values saved before the port reset.
9168ffcd51f3Slg150142 		 */
9169ffcd51f3Slg150142 		rval = usb_check_same_device(dip,
9170ffcd51f3Slg150142 		    hubd->h_log_handle, USB_LOG_L0,
9171ffcd51f3Slg150142 		    DPRINT_MASK_ALL, USB_CHK_ALL, NULL);
9172ffcd51f3Slg150142 
9173ffcd51f3Slg150142 		usb_pipe_close(dip, ph, USB_FLAGS_SLEEP |
9174ffcd51f3Slg150142 		    USBA_FLAGS_PRIVILEGED, NULL, NULL);
9175ffcd51f3Slg150142 	}
9176ffcd51f3Slg150142 	mutex_enter(HUBD_MUTEX(hubd));
9177ffcd51f3Slg150142 
9178ffcd51f3Slg150142 	return (rval);
9179ffcd51f3Slg150142 }
9180ffcd51f3Slg150142 
9181ffcd51f3Slg150142 /*
9182ffcd51f3Slg150142  * usba_hubdi_reset_device
9183ffcd51f3Slg150142  *	Called by usb_reset_device to handle usb device reset.
9184ffcd51f3Slg150142  */
9185ffcd51f3Slg150142 int
usba_hubdi_reset_device(dev_info_t * dip,usb_dev_reset_lvl_t reset_level)9186ffcd51f3Slg150142 usba_hubdi_reset_device(dev_info_t *dip, usb_dev_reset_lvl_t reset_level)
9187ffcd51f3Slg150142 {
9188ffcd51f3Slg150142 	hubd_t			*hubd;
9189ffcd51f3Slg150142 	usb_port_t		port = 0;
9190ffcd51f3Slg150142 	dev_info_t		*hdip;
9191ffcd51f3Slg150142 	usb_pipe_state_t	prev_pipe_state = 0;
91920d2006e4SRobert Mustacchi 	usba_device_t		*usba_device = NULL;
9193ffcd51f3Slg150142 	hubd_reset_arg_t	*arg;
9194ffcd51f3Slg150142 	int			i, ph_open_cnt;
9195ffcd51f3Slg150142 	int			rval = USB_FAILURE;
9196ffcd51f3Slg150142 
9197ffcd51f3Slg150142 	if ((!dip) || usba_is_root_hub(dip)) {
9198aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
9199aa041649SRaymond Chen 		    "usba_hubdi_reset_device: NULL dip or root hub");
9200ffcd51f3Slg150142 
9201ffcd51f3Slg150142 		return (USB_INVALID_ARGS);
9202ffcd51f3Slg150142 	}
9203ffcd51f3Slg150142 
9204ffcd51f3Slg150142 	if (!usb_owns_device(dip)) {
9205aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
9206aa041649SRaymond Chen 		    "usba_hubdi_reset_device: Not owns the device");
9207ffcd51f3Slg150142 
9208ffcd51f3Slg150142 		return (USB_INVALID_PERM);
9209ffcd51f3Slg150142 	}
9210ffcd51f3Slg150142 
9211ffcd51f3Slg150142 	if ((reset_level != USB_RESET_LVL_REATTACH) &&
9212ffcd51f3Slg150142 	    (reset_level != USB_RESET_LVL_DEFAULT)) {
9213aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
9214aa041649SRaymond Chen 		    "usba_hubdi_reset_device: Unknown flags");
9215ffcd51f3Slg150142 
9216ffcd51f3Slg150142 		return (USB_INVALID_ARGS);
9217ffcd51f3Slg150142 	}
9218ffcd51f3Slg150142 
9219ffcd51f3Slg150142 	if ((hdip = ddi_get_parent(dip)) == NULL) {
9220aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
9221aa041649SRaymond Chen 		    "usba_hubdi_reset_device: fail to get parent hub");
9222ffcd51f3Slg150142 
9223ffcd51f3Slg150142 		return (USB_INVALID_ARGS);
9224ffcd51f3Slg150142 	}
9225ffcd51f3Slg150142 
9226ffcd51f3Slg150142 	if ((hubd = hubd_get_soft_state(hdip)) == NULL) {
9227aa041649SRaymond Chen 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubdi_log_handle,
9228aa041649SRaymond Chen 		    "usba_hubdi_reset_device: fail to get hub softstate");
9229ffcd51f3Slg150142 
9230ffcd51f3Slg150142 		return (USB_INVALID_ARGS);
9231ffcd51f3Slg150142 	}
9232ffcd51f3Slg150142 
9233ffcd51f3Slg150142 	mutex_enter(HUBD_MUTEX(hubd));
9234ffcd51f3Slg150142 
9235ffcd51f3Slg150142 	/* make sure the hub is connected before trying any kinds of reset. */
9236ffcd51f3Slg150142 	if ((hubd->h_dev_state == USB_DEV_DISCONNECTED) ||
9237ffcd51f3Slg150142 	    (hubd->h_dev_state == USB_DEV_SUSPENDED)) {
9238ffcd51f3Slg150142 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9239ffcd51f3Slg150142 		    "usb_reset_device: the state %d of the hub/roothub "
9240112116d8Sfb209375 		    "associated to the device 0x%p is incorrect",
9241112116d8Sfb209375 		    hubd->h_dev_state, (void *)dip);
9242ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
9243ffcd51f3Slg150142 
9244ffcd51f3Slg150142 		return (USB_INVALID_ARGS);
9245ffcd51f3Slg150142 	}
9246ffcd51f3Slg150142 
9247ffcd51f3Slg150142 	mutex_exit(HUBD_MUTEX(hubd));
9248ffcd51f3Slg150142 
9249ffcd51f3Slg150142 	port = hubd_child_dip2port(hubd, dip);
9250ffcd51f3Slg150142 
9251ffcd51f3Slg150142 	mutex_enter(HUBD_MUTEX(hubd));
9252ffcd51f3Slg150142 
9253ffcd51f3Slg150142 	if (hubd->h_reset_port[port]) {
9254ffcd51f3Slg150142 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9255ffcd51f3Slg150142 		    "usb_reset_device: the corresponding port is resetting");
9256ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
9257ffcd51f3Slg150142 
9258ffcd51f3Slg150142 		return (USB_SUCCESS);
9259ffcd51f3Slg150142 	}
9260ffcd51f3Slg150142 
9261ffcd51f3Slg150142 	/*
9262ffcd51f3Slg150142 	 * For Default reset, client drivers should first close all the pipes
9263ffcd51f3Slg150142 	 * except default pipe before calling the function, also should not
9264ffcd51f3Slg150142 	 * call the function during interrupt context.
9265ffcd51f3Slg150142 	 */
9266ffcd51f3Slg150142 	if (reset_level == USB_RESET_LVL_DEFAULT) {
9267ffcd51f3Slg150142 		usba_device = hubd->h_usba_devices[port];
9268ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
9269ffcd51f3Slg150142 
9270ffcd51f3Slg150142 		if (servicing_interrupt()) {
9271ffcd51f3Slg150142 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9272ffcd51f3Slg150142 			    "usb_reset_device: during interrput context, quit");
9273ffcd51f3Slg150142 
9274ffcd51f3Slg150142 			return (USB_INVALID_CONTEXT);
9275ffcd51f3Slg150142 		}
9276ffcd51f3Slg150142 		/* Check if all the pipes have been closed */
9277ffcd51f3Slg150142 		for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) {
9278ffcd51f3Slg150142 			if (usba_device->usb_ph_list[i].usba_ph_data) {
9279ffcd51f3Slg150142 				ph_open_cnt++;
9280ffcd51f3Slg150142 				break;
9281ffcd51f3Slg150142 			}
9282ffcd51f3Slg150142 		}
9283ffcd51f3Slg150142 		if (ph_open_cnt) {
9284ffcd51f3Slg150142 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9285ffcd51f3Slg150142 			    "usb_reset_device: %d pipes are still open",
9286ffcd51f3Slg150142 			    ph_open_cnt);
9287ffcd51f3Slg150142 
9288ffcd51f3Slg150142 			return (USB_BUSY);
9289ffcd51f3Slg150142 		}
9290ffcd51f3Slg150142 		mutex_enter(HUBD_MUTEX(hubd));
9291ffcd51f3Slg150142 	}
9292ffcd51f3Slg150142 
9293ffcd51f3Slg150142 	/* Don't perform reset while the device is detaching */
9294ffcd51f3Slg150142 	if (hubd->h_port_state[port] & HUBD_CHILD_DETACHING) {
9295ffcd51f3Slg150142 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9296ffcd51f3Slg150142 		    "usb_reset_device: the device is detaching, "
9297ffcd51f3Slg150142 		    "cannot be reset");
9298ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
9299ffcd51f3Slg150142 
9300ffcd51f3Slg150142 		return (USB_FAILURE);
9301ffcd51f3Slg150142 	}
9302ffcd51f3Slg150142 
9303ffcd51f3Slg150142 	hubd->h_reset_port[port] = B_TRUE;
9304ffcd51f3Slg150142 	hdip = hubd->h_dip;
9305ffcd51f3Slg150142 	mutex_exit(HUBD_MUTEX(hubd));
9306ffcd51f3Slg150142 
9307ffcd51f3Slg150142 	/* Don't allow hub detached during the reset */
9308ffcd51f3Slg150142 	ndi_hold_devi(hdip);
9309ffcd51f3Slg150142 
9310ffcd51f3Slg150142 	mutex_enter(HUBD_MUTEX(hubd));
9311ffcd51f3Slg150142 	hubd_pm_busy_component(hubd, hdip, 0);
9312ffcd51f3Slg150142 	mutex_exit(HUBD_MUTEX(hubd));
9313ffcd51f3Slg150142 	/* go full power */
9314ffcd51f3Slg150142 	(void) pm_raise_power(hdip, 0, USB_DEV_OS_FULL_PWR);
9315ffcd51f3Slg150142 	mutex_enter(HUBD_MUTEX(hubd));
9316ffcd51f3Slg150142 
9317ffcd51f3Slg150142 	hubd->h_hotplug_thread++;
9318ffcd51f3Slg150142 
9319ffcd51f3Slg150142 	/* stop polling if it was active */
9320ffcd51f3Slg150142 	if (hubd->h_ep1_ph) {
9321ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
9322ffcd51f3Slg150142 		(void) usb_pipe_get_state(hubd->h_ep1_ph, &prev_pipe_state,
9323ffcd51f3Slg150142 		    USB_FLAGS_SLEEP);
9324ffcd51f3Slg150142 		mutex_enter(HUBD_MUTEX(hubd));
9325ffcd51f3Slg150142 
9326ffcd51f3Slg150142 		if (prev_pipe_state == USB_PIPE_STATE_ACTIVE) {
9327ffcd51f3Slg150142 			hubd_stop_polling(hubd);
9328ffcd51f3Slg150142 		}
9329ffcd51f3Slg150142 	}
9330ffcd51f3Slg150142 
9331ffcd51f3Slg150142 	switch (reset_level) {
9332ffcd51f3Slg150142 	case USB_RESET_LVL_REATTACH:
9333ffcd51f3Slg150142 		mutex_exit(HUBD_MUTEX(hubd));
9334ffcd51f3Slg150142 		arg = (hubd_reset_arg_t *)kmem_zalloc(
9335ffcd51f3Slg150142 		    sizeof (hubd_reset_arg_t), KM_SLEEP);
9336ffcd51f3Slg150142 		arg->hubd = hubd;
9337ffcd51f3Slg150142 		arg->reset_port = port;
9338ffcd51f3Slg150142 		mutex_enter(HUBD_MUTEX(hubd));
9339ffcd51f3Slg150142 
9340ffcd51f3Slg150142 		if ((rval = usb_async_req(hdip, hubd_reset_thread,
9341ffcd51f3Slg150142 		    (void *)arg, 0)) == USB_SUCCESS) {
9342ffcd51f3Slg150142 			hubd->h_hotplug_thread--;
9343ffcd51f3Slg150142 			mutex_exit(HUBD_MUTEX(hubd));
9344ffcd51f3Slg150142 
9345ffcd51f3Slg150142 			return (USB_SUCCESS);
9346ffcd51f3Slg150142 		} else {
9347ffcd51f3Slg150142 			USB_DPRINTF_L2(DPRINT_MASK_ATTA, hubd->h_log_handle,
9348ffcd51f3Slg150142 			    "Cannot create reset thread, the device %s%d failed"
9349ffcd51f3Slg150142 			    " to reset", ddi_driver_name(dip),
9350ffcd51f3Slg150142 			    ddi_get_instance(dip));
9351ffcd51f3Slg150142 
9352ffcd51f3Slg150142 			kmem_free(arg, sizeof (hubd_reset_arg_t));
9353ffcd51f3Slg150142 		}
9354ffcd51f3Slg150142 
9355ffcd51f3Slg150142 		break;
9356ffcd51f3Slg150142 	case USB_RESET_LVL_DEFAULT:
9357ffcd51f3Slg150142 		/*
9358ffcd51f3Slg150142 		 * Reset hub port and then recover device's address, set back
9359ffcd51f3Slg150142 		 * device's configuration, hubd_handle_port_connect() will
9360ffcd51f3Slg150142 		 * handle errors happened during this process.
9361ffcd51f3Slg150142 		 */
9362ffcd51f3Slg150142 		if ((rval = hubd_handle_port_connect(hubd, port))
9363ffcd51f3Slg150142 		    == USB_SUCCESS) {
9364ffcd51f3Slg150142 			mutex_exit(HUBD_MUTEX(hubd));
9365ffcd51f3Slg150142 			/* re-open the default pipe */
93660d2006e4SRobert Mustacchi 			ASSERT3P(usba_device, !=, NULL);
9367ffcd51f3Slg150142 			rval = usba_persistent_pipe_open(usba_device);
9368ffcd51f3Slg150142 			mutex_enter(HUBD_MUTEX(hubd));
9369ffcd51f3Slg150142 			if (rval != USB_SUCCESS) {
9370ffcd51f3Slg150142 				USB_DPRINTF_L2(DPRINT_MASK_ATTA,
9371ffcd51f3Slg150142 				    hubd->h_log_handle, "failed to reopen "
9372ffcd51f3Slg150142 				    "default pipe after reset, disable hub"
9373ffcd51f3Slg150142 				    "port for %s%d", ddi_driver_name(dip),
9374ffcd51f3Slg150142 				    ddi_get_instance(dip));
9375ffcd51f3Slg150142 				/*
9376ffcd51f3Slg150142 				 * Disable port to set out a hotplug thread
9377ffcd51f3Slg150142 				 * which will handle errors.
9378ffcd51f3Slg150142 				 */
9379ffcd51f3Slg150142 				(void) hubd_disable_port(hubd, port);
9380ffcd51f3Slg150142 			}
9381ffcd51f3Slg150142 		}
9382ffcd51f3Slg150142 
9383ffcd51f3Slg150142 		break;
9384ffcd51f3Slg150142 	default:
9385ffcd51f3Slg150142 
9386ffcd51f3Slg150142 		break;
9387ffcd51f3Slg150142 	}
9388ffcd51f3Slg150142 
9389ffcd51f3Slg150142 	/* allow hotplug thread now */
9390ffcd51f3Slg150142 	hubd->h_hotplug_thread--;
9391ffcd51f3Slg150142 
9392ffcd51f3Slg150142 	if ((hubd->h_dev_state == USB_DEV_ONLINE) && hubd->h_ep1_ph &&
9393ffcd51f3Slg150142 	    (prev_pipe_state == USB_PIPE_STATE_ACTIVE)) {
9394ffcd51f3Slg150142 		hubd_start_polling(hubd, 0);
9395ffcd51f3Slg150142 	}
9396ffcd51f3Slg150142 
9397ffcd51f3Slg150142 	hubd_pm_idle_component(hubd, hdip, 0);
9398ffcd51f3Slg150142 
9399ffcd51f3Slg150142 	/* Clear reset mark for the port. */
9400ffcd51f3Slg150142 	hubd->h_reset_port[port] = B_FALSE;
9401ffcd51f3Slg150142 
9402ffcd51f3Slg150142 	mutex_exit(HUBD_MUTEX(hubd));
9403ffcd51f3Slg150142 
9404ffcd51f3Slg150142 	ndi_rele_devi(hdip);
9405ffcd51f3Slg150142 
9406ffcd51f3Slg150142 	return (rval);
9407ffcd51f3Slg150142 }
9408