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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * usb multi interface and common class driver
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  *	this driver attempts to attach each interface to a driver
327c478bd9Sstevel@tonic-gate  *	and may eventually handle common class features such as
337c478bd9Sstevel@tonic-gate  *	shared endpoints
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if defined(lint) && !defined(DEBUG)
377c478bd9Sstevel@tonic-gate #define	DEBUG	1
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
407c478bd9Sstevel@tonic-gate #include <sys/usb/usba.h>
417c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_types.h>
427c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_ugen.h>
447c478bd9Sstevel@tonic-gate #include <sys/usb/usb_mid/usb_midvar.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate void usba_free_evdata(usba_evdata_t *);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* Debugging support */
49*4610e4a0Sfrits uint_t usb_mid_errlevel = USB_LOG_L4;
50*4610e4a0Sfrits uint_t usb_mid_errmask = (uint_t)DPRINT_MASK_ALL;
51*4610e4a0Sfrits uint_t usb_mid_instance_debug = (uint_t)-1;
52*4610e4a0Sfrits uint_t usb_mid_bus_config_debug = 0;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_errlevel))
557c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_errmask))
567c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_instance_debug))
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
597c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
607c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", usb_pipe_policy))
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Hotplug support
647c478bd9Sstevel@tonic-gate  * Leaf ops (hotplug controls for client devices)
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static int usb_mid_open(dev_t *, int, int, cred_t *);
677c478bd9Sstevel@tonic-gate static int usb_mid_close(dev_t, int, int, cred_t *);
687c478bd9Sstevel@tonic-gate static int usb_mid_read(dev_t, struct uio *, cred_t *);
697c478bd9Sstevel@tonic-gate static int usb_mid_write(dev_t, struct uio *, cred_t *);
707c478bd9Sstevel@tonic-gate static int usb_mid_poll(dev_t, short, int,  short *,
717c478bd9Sstevel@tonic-gate 					struct pollhead **);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static struct cb_ops usb_mid_cb_ops = {
747c478bd9Sstevel@tonic-gate 	usb_mid_open,
757c478bd9Sstevel@tonic-gate 	usb_mid_close,
767c478bd9Sstevel@tonic-gate 	nodev,		/* strategy */
777c478bd9Sstevel@tonic-gate 	nodev,		/* print */
787c478bd9Sstevel@tonic-gate 	nodev,		/* dump */
797c478bd9Sstevel@tonic-gate 	usb_mid_read,	/* read */
807c478bd9Sstevel@tonic-gate 	usb_mid_write,	/* write */
817c478bd9Sstevel@tonic-gate 	nodev,
827c478bd9Sstevel@tonic-gate 	nodev,		/* devmap */
837c478bd9Sstevel@tonic-gate 	nodev,		/* mmap */
847c478bd9Sstevel@tonic-gate 	nodev,		/* segmap */
857c478bd9Sstevel@tonic-gate 	usb_mid_poll,	/* poll */
867c478bd9Sstevel@tonic-gate 	ddi_prop_op,	/* prop_op */
877c478bd9Sstevel@tonic-gate 	NULL,
887c478bd9Sstevel@tonic-gate 	D_MP
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int usb_mid_busop_get_eventcookie(dev_info_t *dip,
927c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
937c478bd9Sstevel@tonic-gate 			char *eventname,
947c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t *cookie);
957c478bd9Sstevel@tonic-gate static int usb_mid_busop_add_eventcall(dev_info_t *dip,
967c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
977c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t cookie,
987c478bd9Sstevel@tonic-gate 			void (*callback)(dev_info_t *dip,
997c478bd9Sstevel@tonic-gate 				ddi_eventcookie_t cookie, void *arg,
1007c478bd9Sstevel@tonic-gate 				void *bus_impldata),
1017c478bd9Sstevel@tonic-gate 			void *arg, ddi_callback_id_t *cb_id);
1027c478bd9Sstevel@tonic-gate static int usb_mid_busop_remove_eventcall(dev_info_t *dip,
1037c478bd9Sstevel@tonic-gate 			ddi_callback_id_t cb_id);
1047c478bd9Sstevel@tonic-gate static int usb_mid_busop_post_event(dev_info_t *dip,
1057c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
1067c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t cookie,
1077c478bd9Sstevel@tonic-gate 			void *bus_impldata);
1087c478bd9Sstevel@tonic-gate static int usb_mid_bus_config(dev_info_t *dip,
1097c478bd9Sstevel@tonic-gate 			uint_t flag,
1107c478bd9Sstevel@tonic-gate 			ddi_bus_config_op_t op,
1117c478bd9Sstevel@tonic-gate 			void *arg,
1127c478bd9Sstevel@tonic-gate 			dev_info_t **child);
1137c478bd9Sstevel@tonic-gate static int usb_mid_bus_unconfig(dev_info_t *dip,
1147c478bd9Sstevel@tonic-gate 			uint_t flag,
1157c478bd9Sstevel@tonic-gate 			ddi_bus_config_op_t op,
1167c478bd9Sstevel@tonic-gate 			void *arg);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * autoconfiguration data and routines.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static int	usb_mid_info(dev_info_t *, ddi_info_cmd_t,
1237c478bd9Sstevel@tonic-gate 				void *, void **);
1247c478bd9Sstevel@tonic-gate static int	usb_mid_attach(dev_info_t *, ddi_attach_cmd_t);
1257c478bd9Sstevel@tonic-gate static int	usb_mid_detach(dev_info_t *, ddi_detach_cmd_t);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* other routines */
1287c478bd9Sstevel@tonic-gate static void usb_mid_create_pm_components(dev_info_t *, usb_mid_t *);
1297c478bd9Sstevel@tonic-gate static int usb_mid_bus_ctl(dev_info_t *, dev_info_t	*,
1307c478bd9Sstevel@tonic-gate 				ddi_ctl_enum_t, void *, void *);
1317c478bd9Sstevel@tonic-gate static int usb_mid_power(dev_info_t *, int, int);
1327c478bd9Sstevel@tonic-gate static int usb_mid_restore_device_state(dev_info_t *, usb_mid_t *);
1337c478bd9Sstevel@tonic-gate static usb_mid_t  *usb_mid_obtain_state(dev_info_t *);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Busops vector
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static struct bus_ops usb_mid_busops = {
1397c478bd9Sstevel@tonic-gate 	BUSO_REV,
1407c478bd9Sstevel@tonic-gate 	nullbusmap,			/* bus_map */
1417c478bd9Sstevel@tonic-gate 	NULL,				/* bus_get_intrspec */
1427c478bd9Sstevel@tonic-gate 	NULL,				/* bus_add_intrspec */
1437c478bd9Sstevel@tonic-gate 	NULL,				/* bus_remove_intrspec */
1447c478bd9Sstevel@tonic-gate 	NULL,				/* XXXX bus_map_fault */
1457c478bd9Sstevel@tonic-gate 	ddi_dma_map,			/* bus_dma_map */
1467c478bd9Sstevel@tonic-gate 	ddi_dma_allochdl,
1477c478bd9Sstevel@tonic-gate 	ddi_dma_freehdl,
1487c478bd9Sstevel@tonic-gate 	ddi_dma_bindhdl,
1497c478bd9Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1507c478bd9Sstevel@tonic-gate 	ddi_dma_flush,
1517c478bd9Sstevel@tonic-gate 	ddi_dma_win,
1527c478bd9Sstevel@tonic-gate 	ddi_dma_mctl,			/* bus_dma_ctl */
1537c478bd9Sstevel@tonic-gate 	usb_mid_bus_ctl,		/* bus_ctl */
1547c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,		/* bus_prop_op */
1557c478bd9Sstevel@tonic-gate 	usb_mid_busop_get_eventcookie,
1567c478bd9Sstevel@tonic-gate 	usb_mid_busop_add_eventcall,
1577c478bd9Sstevel@tonic-gate 	usb_mid_busop_remove_eventcall,
1587c478bd9Sstevel@tonic-gate 	usb_mid_busop_post_event,	/* bus_post_event */
1597c478bd9Sstevel@tonic-gate 	NULL,				/* bus_intr_ctl */
1607c478bd9Sstevel@tonic-gate 	usb_mid_bus_config,		/* bus_config */
1617c478bd9Sstevel@tonic-gate 	usb_mid_bus_unconfig,		/* bus_unconfig */
1627c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_init */
1637c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_fini */
1647c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_enter */
1657c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_exit */
1667c478bd9Sstevel@tonic-gate 	NULL				/* bus_power */
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate static struct dev_ops usb_mid_ops = {
1717c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1727c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
1737c478bd9Sstevel@tonic-gate 	usb_mid_info,		/* info */
1747c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1757c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1767c478bd9Sstevel@tonic-gate 	usb_mid_attach,		/* attach */
1777c478bd9Sstevel@tonic-gate 	usb_mid_detach,		/* detach */
1787c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1797c478bd9Sstevel@tonic-gate 	&usb_mid_cb_ops,	/* driver operations */
1807c478bd9Sstevel@tonic-gate 	&usb_mid_busops,	/* bus operations */
1817c478bd9Sstevel@tonic-gate 	usb_mid_power		/* power */
1827c478bd9Sstevel@tonic-gate };
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1857c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module. This one is a driver */
1867c478bd9Sstevel@tonic-gate 	"USB Multi Interface Driver %I%", /* Name of the module. */
1877c478bd9Sstevel@tonic-gate 	&usb_mid_ops,	/* driver ops */
1887c478bd9Sstevel@tonic-gate };
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1917c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1927c478bd9Sstevel@tonic-gate };
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #define	USB_MID_INITIAL_SOFT_SPACE 4
1957c478bd9Sstevel@tonic-gate static	void	*usb_mid_statep;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * prototypes
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate static void usb_mid_create_children(usb_mid_t *usb_mid);
2027c478bd9Sstevel@tonic-gate static int usb_mid_cleanup(dev_info_t *dip, usb_mid_t	*usb_mid);
2037c478bd9Sstevel@tonic-gate static void usb_mid_register_events(usb_mid_t *usb_mid);
2047c478bd9Sstevel@tonic-gate static void usb_mid_unregister_events(usb_mid_t *usb_mid);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /* usbai private */
2077c478bd9Sstevel@tonic-gate char	*usba_get_mfg_prod_sn_str(dev_info_t  *dip,
2087c478bd9Sstevel@tonic-gate 					char *buffer, int buflen);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * event definition
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate static ndi_event_definition_t usb_mid_ndi_event_defs[] = {
2147c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
2157c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2167c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
2177c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2187c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
2197c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2207c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
2217c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL}
2227c478bd9Sstevel@tonic-gate };
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate #define	USB_MID_N_NDI_EVENTS \
2257c478bd9Sstevel@tonic-gate 	(sizeof (usb_mid_ndi_event_defs) / sizeof (ndi_event_definition_t))
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static	ndi_event_set_t usb_mid_ndi_events = {
2287c478bd9Sstevel@tonic-gate 	NDI_EVENTS_REV1, USB_MID_N_NDI_EVENTS, usb_mid_ndi_event_defs};
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * standard driver entry points
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate int
2357c478bd9Sstevel@tonic-gate _init(void)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	int rval;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	rval = ddi_soft_state_init(&usb_mid_statep, sizeof (struct usb_mid),
2407c478bd9Sstevel@tonic-gate 	    USB_MID_INITIAL_SOFT_SPACE);
2417c478bd9Sstevel@tonic-gate 	if (rval != 0) {
2427c478bd9Sstevel@tonic-gate 		return (rval);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if ((rval = mod_install(&modlinkage)) != 0) {
2467c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&usb_mid_statep);
2477c478bd9Sstevel@tonic-gate 		return (rval);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	return (rval);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate int
2557c478bd9Sstevel@tonic-gate _fini(void)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	int	rval;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	rval = mod_remove(&modlinkage);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (rval) {
2627c478bd9Sstevel@tonic-gate 		return (rval);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&usb_mid_statep);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	return (rval);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate int
2727c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2797c478bd9Sstevel@tonic-gate static int
2807c478bd9Sstevel@tonic-gate usb_mid_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid;
2837c478bd9Sstevel@tonic-gate 	int		instance =
2847c478bd9Sstevel@tonic-gate 			USB_MID_MINOR_TO_INSTANCE(getminor((dev_t)arg));
2857c478bd9Sstevel@tonic-gate 	int		error = DDI_FAILURE;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2887c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2897c478bd9Sstevel@tonic-gate 		if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
2907c478bd9Sstevel@tonic-gate 		    instance)) != NULL) {
2917c478bd9Sstevel@tonic-gate 			*result = (void *)usb_mid->mi_dip;
2927c478bd9Sstevel@tonic-gate 			if (*result != NULL) {
2937c478bd9Sstevel@tonic-gate 				error = DDI_SUCCESS;
2947c478bd9Sstevel@tonic-gate 			}
2957c478bd9Sstevel@tonic-gate 		} else {
2967c478bd9Sstevel@tonic-gate 			*result = NULL;
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		break;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3017c478bd9Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
3027c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
3037c478bd9Sstevel@tonic-gate 		break;
3047c478bd9Sstevel@tonic-gate 	default:
3057c478bd9Sstevel@tonic-gate 		break;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (error);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * child  post attach/detach notification
3147c478bd9Sstevel@tonic-gate  */
3157c478bd9Sstevel@tonic-gate static void
3167c478bd9Sstevel@tonic-gate usb_mid_post_attach(usb_mid_t *usb_mid, uint8_t ifno, struct attachspec *as)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3197c478bd9Sstevel@tonic-gate 	    "usb_mid_post_attach: ifno = %d result = %d", ifno, as->result);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	/* if child successfully attached, set power */
3227c478bd9Sstevel@tonic-gate 	if (as->result == DDI_SUCCESS) {
3237c478bd9Sstevel@tonic-gate 		/*
3247c478bd9Sstevel@tonic-gate 		 * Check if the child created wants to be power managed.
3257c478bd9Sstevel@tonic-gate 		 * If yes, the childs power level gets automatically tracked
3267c478bd9Sstevel@tonic-gate 		 * by DDI_CTLOPS_POWER busctl.
3277c478bd9Sstevel@tonic-gate 		 * If no, we set power of the new child by default
3287c478bd9Sstevel@tonic-gate 		 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
3297c478bd9Sstevel@tonic-gate 		 */
3307c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
3317c478bd9Sstevel@tonic-gate 		usb_mid->mi_attach_count++;
3327c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate static void
3387c478bd9Sstevel@tonic-gate usb_mid_post_detach(usb_mid_t *usb_mid, uint8_t ifno, struct detachspec *ds)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3417c478bd9Sstevel@tonic-gate 	    "usb_mid_post_detach: ifno = %d result = %d", ifno, ds->result);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * if the device is successfully detached,
3457c478bd9Sstevel@tonic-gate 	 * mark component as idle
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	if (ds->result == DDI_SUCCESS) {
3487c478bd9Sstevel@tonic-gate 		usba_device_t *usba_device =
3497c478bd9Sstevel@tonic-gate 				usba_get_usba_device(usb_mid->mi_dip);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		/* check for leaks except when where is a ugen open */
3547c478bd9Sstevel@tonic-gate 		if ((ds->cmd == DDI_DETACH) &&
3557c478bd9Sstevel@tonic-gate 		    (--usb_mid->mi_attach_count == 0) && usba_device &&
3567c478bd9Sstevel@tonic-gate 		    (usb_mid->mi_ugen_open_count == 0)) {
3577c478bd9Sstevel@tonic-gate 			usba_check_for_leaks(usba_device);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * bus ctl support. we handle notifications here and the
3667c478bd9Sstevel@tonic-gate  * rest goes up to root hub/hcd
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3697c478bd9Sstevel@tonic-gate static int
3707c478bd9Sstevel@tonic-gate usb_mid_bus_ctl(dev_info_t *dip,
3717c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
3727c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t	op,
3737c478bd9Sstevel@tonic-gate 	void		*arg,
3747c478bd9Sstevel@tonic-gate 	void		*result)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
3777c478bd9Sstevel@tonic-gate 	dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
3787c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid;
3797c478bd9Sstevel@tonic-gate 	struct attachspec *as;
3807c478bd9Sstevel@tonic-gate 	struct detachspec *ds;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	usb_mid = usb_mid_obtain_state(dip);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3857c478bd9Sstevel@tonic-gate 	    "usb_mid_bus_ctl:\n\t"
3867c478bd9Sstevel@tonic-gate 	    "dip = 0x%p, rdip = 0x%p, op = 0x%x, arg = 0x%p",
3877c478bd9Sstevel@tonic-gate 	    dip, rdip, op, arg);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	switch (op) {
3907c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
3917c478bd9Sstevel@tonic-gate 		as = (struct attachspec *)arg;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		switch (as->when) {
3947c478bd9Sstevel@tonic-gate 		case DDI_PRE :
3957c478bd9Sstevel@tonic-gate 			/* nothing to do basically */
3967c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3977c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_ATTACH");
3987c478bd9Sstevel@tonic-gate 			break;
3997c478bd9Sstevel@tonic-gate 		case DDI_POST :
4007c478bd9Sstevel@tonic-gate 			usb_mid_post_attach(usb_mid, usba_get_ifno(rdip),
4017c478bd9Sstevel@tonic-gate 			    (struct attachspec *)arg);
4027c478bd9Sstevel@tonic-gate 			break;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		break;
4067c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
4077c478bd9Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		switch (ds->when) {
4107c478bd9Sstevel@tonic-gate 		case DDI_PRE :
4117c478bd9Sstevel@tonic-gate 			/* nothing to do basically */
4127c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
4137c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_DETACH");
4147c478bd9Sstevel@tonic-gate 			break;
4157c478bd9Sstevel@tonic-gate 		case DDI_POST :
4167c478bd9Sstevel@tonic-gate 			usb_mid_post_detach(usb_mid, usba_get_ifno(rdip),
4177c478bd9Sstevel@tonic-gate 			    (struct detachspec *)arg);
4187c478bd9Sstevel@tonic-gate 			break;
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		break;
4227c478bd9Sstevel@tonic-gate 	default:
4237c478bd9Sstevel@tonic-gate 		/* pass to root hub to handle */
4247c478bd9Sstevel@tonic-gate 		return (usba_bus_ctl(root_hub_dip, rdip, op, arg, result));
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate  * bus enumeration entry points
4337c478bd9Sstevel@tonic-gate  */
4347c478bd9Sstevel@tonic-gate static int
4357c478bd9Sstevel@tonic-gate usb_mid_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
4367c478bd9Sstevel@tonic-gate     void *arg, dev_info_t **child)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	int		rval, circ;
4397c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid = usb_mid_obtain_state(dip);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
4427c478bd9Sstevel@tonic-gate 	    "usb_mid_bus_config: op=%d", op);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (usb_mid_bus_config_debug) {
4457c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* enumerate each interface below us */
4517c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
4527c478bd9Sstevel@tonic-gate 	usb_mid_create_children(usb_mid);
4537c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
4567c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	return (rval);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate static int
4637c478bd9Sstevel@tonic-gate usb_mid_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
4647c478bd9Sstevel@tonic-gate     void *arg)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip, *mdip;
4697c478bd9Sstevel@tonic-gate 	int		interface, circular_count;
4707c478bd9Sstevel@tonic-gate 	int		 rval = NDI_SUCCESS;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
4737c478bd9Sstevel@tonic-gate 	    "usb_mid_bus_unconfig: op=%d", op);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (usb_mid_bus_config_debug) {
4767c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * first offline and if offlining successful, then
4817c478bd9Sstevel@tonic-gate 	 * remove children
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (op == BUS_UNCONFIG_ALL) {
4847c478bd9Sstevel@tonic-gate 		flag &= ~(NDI_DEVI_REMOVE | NDI_UNCONFIG);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circular_count);
4887c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (op == BUS_UNCONFIG_ALL && rval == NDI_SUCCESS &&
4917c478bd9Sstevel@tonic-gate 	    (flag & NDI_AUTODETACH) == 0) {
4927c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_REMOVE;
4937c478bd9Sstevel@tonic-gate 		rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* update children's list */
4977c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
4987c478bd9Sstevel@tonic-gate 	for (interface = 0; usb_mid->mi_children_dips &&
4997c478bd9Sstevel@tonic-gate 	    (interface < usb_mid->mi_n_ifs); interface++) {
5007c478bd9Sstevel@tonic-gate 		mdip = usb_mid->mi_children_dips[interface];
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		/* now search if this dip still exists */
5037c478bd9Sstevel@tonic-gate 		for (cdip = ddi_get_child(dip); cdip && (cdip != mdip);
5047c478bd9Sstevel@tonic-gate 		    cdip = ddi_get_next_sibling(cdip));
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		if (cdip != mdip) {
5077c478bd9Sstevel@tonic-gate 			/* we lost the dip on this interface */
5087c478bd9Sstevel@tonic-gate 			usb_mid->mi_children_dips[interface] = NULL;
5097c478bd9Sstevel@tonic-gate 		} else if (cdip) {
5107c478bd9Sstevel@tonic-gate 			/*
5117c478bd9Sstevel@tonic-gate 			 * keep in DS_INITALIZED to prevent parent
5127c478bd9Sstevel@tonic-gate 			 * from detaching
5137c478bd9Sstevel@tonic-gate 			 */
5147c478bd9Sstevel@tonic-gate 			(void) ddi_initchild(ddi_get_parent(cdip), cdip);
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circular_count);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
5227c478bd9Sstevel@tonic-gate 	    "usb_mid_bus_config: rval=%d", rval);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	return (rval);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * functions to handle power transition for OS levels 0 -> 3
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate static int
5317c478bd9Sstevel@tonic-gate usb_mid_pwrlvl0(usb_mid_t *usb_mid)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	int	rval;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	switch (usb_mid->mi_dev_state) {
5367c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
5377c478bd9Sstevel@tonic-gate 		/* Issue USB D3 command to the device here */
5387c478bd9Sstevel@tonic-gate 		rval = usb_set_device_pwrlvl3(usb_mid->mi_dip);
5397c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		usb_mid->mi_dev_state = USB_DEV_PWRED_DOWN;
5427c478bd9Sstevel@tonic-gate 		usb_mid->mi_pm->mip_current_power =
5437c478bd9Sstevel@tonic-gate 					USB_DEV_OS_PWR_OFF;
5447c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
5457c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
5467c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
5477c478bd9Sstevel@tonic-gate 		/* allow a disconnected/cpr'ed device to go to low pwr */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
5507c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
5517c478bd9Sstevel@tonic-gate 	default:
5527c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /* ARGSUSED */
5587c478bd9Sstevel@tonic-gate static int
5597c478bd9Sstevel@tonic-gate usb_mid_pwrlvl1(usb_mid_t *usb_mid)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	int	rval;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/* Issue USB D2 command to the device here */
5647c478bd9Sstevel@tonic-gate 	rval = usb_set_device_pwrlvl2(usb_mid->mi_dip);
5657c478bd9Sstevel@tonic-gate 	ASSERT(rval == USB_SUCCESS);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /* ARGSUSED */
5727c478bd9Sstevel@tonic-gate static int
5737c478bd9Sstevel@tonic-gate usb_mid_pwrlvl2(usb_mid_t *usb_mid)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	int	rval;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/* Issue USB D1 command to the device here */
5787c478bd9Sstevel@tonic-gate 	rval = usb_set_device_pwrlvl1(usb_mid->mi_dip);
5797c478bd9Sstevel@tonic-gate 	ASSERT(rval == USB_SUCCESS);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate static int
5867c478bd9Sstevel@tonic-gate usb_mid_pwrlvl3(usb_mid_t *usb_mid)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	int	rval;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	switch (usb_mid->mi_dev_state) {
5917c478bd9Sstevel@tonic-gate 	case USB_DEV_PWRED_DOWN:
5927c478bd9Sstevel@tonic-gate 		/* Issue USB D0 command to the device here */
5937c478bd9Sstevel@tonic-gate 		rval = usb_set_device_pwrlvl0(usb_mid->mi_dip);
5947c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		usb_mid->mi_dev_state = USB_DEV_ONLINE;
5977c478bd9Sstevel@tonic-gate 		usb_mid->mi_pm->mip_current_power = USB_DEV_OS_FULL_PWR;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
6007c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
6017c478bd9Sstevel@tonic-gate 		/* we are already in full power */
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
6047c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
6057c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
6067c478bd9Sstevel@tonic-gate 		/* allow a disconnected/cpr'ed device to go to low power */
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
6097c478bd9Sstevel@tonic-gate 	default:
6107c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
6117c478bd9Sstevel@tonic-gate 		    "usb_mid_pwrlvl3: Illegal state (%s)",
6127c478bd9Sstevel@tonic-gate 		    usb_str_dev_state(usb_mid->mi_dev_state));
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate /* power entry point */
6207c478bd9Sstevel@tonic-gate /* ARGSUSED */
6217c478bd9Sstevel@tonic-gate static int
6227c478bd9Sstevel@tonic-gate usb_mid_power(dev_info_t *dip, int comp, int level)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid;
6257c478bd9Sstevel@tonic-gate 	usb_mid_power_t	*midpm;
6267c478bd9Sstevel@tonic-gate 	int		rval = DDI_FAILURE;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	usb_mid =  usb_mid_obtain_state(dip);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
6317c478bd9Sstevel@tonic-gate 	    "usb_mid_power: Begin: usb_mid = %p, level = %d", usb_mid, level);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
6347c478bd9Sstevel@tonic-gate 	midpm = usb_mid->mi_pm;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* check if we are transitioning to a legal power level */
6377c478bd9Sstevel@tonic-gate 	if (USB_DEV_PWRSTATE_OK(midpm->mip_pwr_states, level)) {
6387c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
6397c478bd9Sstevel@tonic-gate 		    "usb_mid_power: illegal power level = %d "
6407c478bd9Sstevel@tonic-gate 		    "mip_pwr_states = %x", level, midpm->mip_pwr_states);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		return (rval);
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	switch (level) {
6487c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_OFF:
6497c478bd9Sstevel@tonic-gate 		rval = usb_mid_pwrlvl0(usb_mid);
6507c478bd9Sstevel@tonic-gate 		break;
6517c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_1:
6527c478bd9Sstevel@tonic-gate 		rval = usb_mid_pwrlvl1(usb_mid);
6537c478bd9Sstevel@tonic-gate 		break;
6547c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_PWR_2:
6557c478bd9Sstevel@tonic-gate 		rval = usb_mid_pwrlvl2(usb_mid);
6567c478bd9Sstevel@tonic-gate 		break;
6577c478bd9Sstevel@tonic-gate 	case USB_DEV_OS_FULL_PWR:
6587c478bd9Sstevel@tonic-gate 		rval = usb_mid_pwrlvl3(usb_mid);
6597c478bd9Sstevel@tonic-gate 		break;
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate  * attach/resume entry point
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate static int
6727c478bd9Sstevel@tonic-gate usb_mid_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	int		instance = ddi_get_instance(dip);
6757c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid = NULL;
6767c478bd9Sstevel@tonic-gate 	uint_t		n_ifs;
6777c478bd9Sstevel@tonic-gate 	size_t		size;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	switch (cmd) {
6807c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		break;
6837c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
6847c478bd9Sstevel@tonic-gate 		usb_mid = (usb_mid_t *)ddi_get_soft_state(usb_mid_statep,
6857c478bd9Sstevel@tonic-gate 		    instance);
6867c478bd9Sstevel@tonic-gate 		(void) usb_mid_restore_device_state(dip, usb_mid);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		if (usb_mid->mi_ugen_hdl) {
6897c478bd9Sstevel@tonic-gate 			(void) usb_ugen_attach(usb_mid->mi_ugen_hdl,
6907c478bd9Sstevel@tonic-gate 						DDI_RESUME);
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6947c478bd9Sstevel@tonic-gate 	default:
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/*
7007c478bd9Sstevel@tonic-gate 	 * Attach:
7017c478bd9Sstevel@tonic-gate 	 *
7027c478bd9Sstevel@tonic-gate 	 * Allocate soft state and initialize
7037c478bd9Sstevel@tonic-gate 	 */
7047c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(usb_mid_statep, instance) != DDI_SUCCESS) {
7057c478bd9Sstevel@tonic-gate 		goto fail;
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	usb_mid = ddi_get_soft_state(usb_mid_statep, instance);
7097c478bd9Sstevel@tonic-gate 	if (usb_mid == NULL) {
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		goto fail;
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/* allocate handle for logging of messages */
7157c478bd9Sstevel@tonic-gate 	usb_mid->mi_log_handle = usb_alloc_log_hdl(dip, "mid",
7167c478bd9Sstevel@tonic-gate 				&usb_mid_errlevel,
7177c478bd9Sstevel@tonic-gate 				&usb_mid_errmask, &usb_mid_instance_debug,
7187c478bd9Sstevel@tonic-gate 				0);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	usb_mid->mi_usba_device = usba_get_usba_device(dip);
7217c478bd9Sstevel@tonic-gate 	usb_mid->mi_dip	= dip;
7227c478bd9Sstevel@tonic-gate 	usb_mid->mi_instance = instance;
7237c478bd9Sstevel@tonic-gate 	usb_mid->mi_n_ifs = usb_mid->mi_usba_device->usb_n_ifs;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/* attach client driver to USBA */
7267c478bd9Sstevel@tonic-gate 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
7277c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7287c478bd9Sstevel@tonic-gate 		    "usb_client_attach failed");
7297c478bd9Sstevel@tonic-gate 		goto fail;
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 	if (usb_get_dev_data(dip, &usb_mid->mi_dev_data, USB_PARSE_LVL_NONE,
7327c478bd9Sstevel@tonic-gate 	    0) != USB_SUCCESS) {
7337c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7347c478bd9Sstevel@tonic-gate 		    "usb_get_dev_data failed");
7357c478bd9Sstevel@tonic-gate 		goto fail;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	mutex_init(&usb_mid->mi_mutex, NULL, MUTEX_DRIVER,
7397c478bd9Sstevel@tonic-gate 			usb_mid->mi_dev_data->dev_iblock_cookie);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	usb_free_dev_data(dip, usb_mid->mi_dev_data);
7427c478bd9Sstevel@tonic-gate 	usb_mid->mi_dev_data = NULL;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_LOCK_INIT;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "usb_mid", S_IFCHR,
7477c478bd9Sstevel@tonic-gate 	    instance << USB_MID_MINOR_INSTANCE_SHIFT,
7487c478bd9Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
7497c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7507c478bd9Sstevel@tonic-gate 		    "cannot create devctl minor node");
7517c478bd9Sstevel@tonic-gate 		goto fail;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_MINOR_NODE_CREATED;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * allocate array for keeping track of child dips
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate 	n_ifs = usb_mid->mi_n_ifs;
7607c478bd9Sstevel@tonic-gate 	usb_mid->mi_cd_list_length = size = (sizeof (dev_info_t *)) * n_ifs;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	usb_mid->mi_children_dips = kmem_zalloc(size, KM_SLEEP);
7637c478bd9Sstevel@tonic-gate 	usb_mid->mi_child_events = kmem_zalloc(sizeof (uint8_t) * n_ifs,
7647c478bd9Sstevel@tonic-gate 								KM_SLEEP);
7657c478bd9Sstevel@tonic-gate 	/*
7667c478bd9Sstevel@tonic-gate 	 * Event handling: definition and registration
7677c478bd9Sstevel@tonic-gate 	 * get event handle for events that we have defined
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 	(void) ndi_event_alloc_hdl(dip, 0, &usb_mid->mi_ndi_event_hdl,
7707c478bd9Sstevel@tonic-gate 								NDI_SLEEP);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	/* bind event set to the handle */
7737c478bd9Sstevel@tonic-gate 	if (ndi_event_bind_set(usb_mid->mi_ndi_event_hdl, &usb_mid_ndi_events,
7747c478bd9Sstevel@tonic-gate 	    NDI_SLEEP)) {
7757c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7767c478bd9Sstevel@tonic-gate 		    "usb_mid_attach: binding event set failed");
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		goto fail;
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	usb_mid->mi_dev_state = USB_DEV_ONLINE;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * now create components to power manage this device
7857c478bd9Sstevel@tonic-gate 	 * before attaching children
7867c478bd9Sstevel@tonic-gate 	 */
7877c478bd9Sstevel@tonic-gate 	usb_mid_create_pm_components(dip, usb_mid);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/* event registration for events from our parent */
7907c478bd9Sstevel@tonic-gate 	usb_mid_register_events(usb_mid);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_EVENTS_REGISTERED;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate fail:
7997c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L1(DPRINT_MASK_ATTA, NULL, "usb_mid%d cannot attach",
8007c478bd9Sstevel@tonic-gate 	    instance);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if (usb_mid) {
8037c478bd9Sstevel@tonic-gate 		(void) usb_mid_cleanup(dip, usb_mid);
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /* detach or suspend this instance */
8117c478bd9Sstevel@tonic-gate static int
8127c478bd9Sstevel@tonic-gate usb_mid_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8137c478bd9Sstevel@tonic-gate {
8147c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid = usb_mid_obtain_state(dip);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
8177c478bd9Sstevel@tonic-gate 	    "usb_mid_detach: cmd = 0x%x", cmd);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	switch (cmd) {
8207c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 		return (usb_mid_cleanup(dip, usb_mid));
8237c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
8247c478bd9Sstevel@tonic-gate 		/* nothing to do */
8257c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
8267c478bd9Sstevel@tonic-gate 		usb_mid->mi_dev_state = USB_DEV_SUSPENDED;
8277c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		if (usb_mid->mi_ugen_hdl) {
8307c478bd9Sstevel@tonic-gate 			int rval = usb_ugen_detach(usb_mid->mi_ugen_hdl,
8317c478bd9Sstevel@tonic-gate 						DDI_SUSPEND);
8327c478bd9Sstevel@tonic-gate 			return (rval == USB_SUCCESS ? DDI_SUCCESS :
8337c478bd9Sstevel@tonic-gate 						DDI_FAILURE);
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8377c478bd9Sstevel@tonic-gate 	default:
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	_NOTE(NOT_REACHED)
8437c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /*
8487c478bd9Sstevel@tonic-gate  * usb_mid_cleanup:
8497c478bd9Sstevel@tonic-gate  *	cleanup usb_mid and deallocate. this function is called for
8507c478bd9Sstevel@tonic-gate  *	handling attach failures and detaching including dynamic
8517c478bd9Sstevel@tonic-gate  *	reconfiguration
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8547c478bd9Sstevel@tonic-gate static int
8557c478bd9Sstevel@tonic-gate usb_mid_cleanup(dev_info_t *dip, usb_mid_t *usb_mid)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	usb_mid_power_t	*midpm;
8587c478bd9Sstevel@tonic-gate 	int		rval;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
8617c478bd9Sstevel@tonic-gate 	    "usb_mid_cleanup:");
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if ((usb_mid->mi_init_state & USB_MID_LOCK_INIT) == 0) {
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		goto done;
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/*
8697c478bd9Sstevel@tonic-gate 	 * deallocate events, if events are still registered
8707c478bd9Sstevel@tonic-gate 	 * (ie. children still attached) then we have to fail the detach
8717c478bd9Sstevel@tonic-gate 	 */
8727c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_ndi_event_hdl &&
8737c478bd9Sstevel@tonic-gate 	    (ndi_event_free_hdl(usb_mid->mi_ndi_event_hdl) != NDI_SUCCESS)) {
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
8767c478bd9Sstevel@tonic-gate 		    "usb_mid_cleanup: ndi_event_free_hdl failed");
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	/*
8827c478bd9Sstevel@tonic-gate 	 * Disable the event callbacks, after this point, event
8837c478bd9Sstevel@tonic-gate 	 * callbacks will never get called. Note we shouldn't hold
8847c478bd9Sstevel@tonic-gate 	 * mutex while unregistering events because there may be a
8857c478bd9Sstevel@tonic-gate 	 * competing event callback thread. Event callbacks are done
8867c478bd9Sstevel@tonic-gate 	 * with ndi mutex held and this can cause a potential deadlock.
8877c478bd9Sstevel@tonic-gate 	 * Note that cleanup can't fail after deregistration of events.
8887c478bd9Sstevel@tonic-gate 	 */
8897c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_init_state & USB_MID_EVENTS_REGISTERED) {
8907c478bd9Sstevel@tonic-gate 		usb_mid_unregister_events(usb_mid);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	midpm = usb_mid->mi_pm;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	if ((midpm) && (usb_mid->mi_dev_state != USB_DEV_DISCONNECTED)) {
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		(void) pm_busy_component(dip, 0);
9027c478bd9Sstevel@tonic-gate 		if (midpm->mip_wakeup_enabled) {
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 			/* First bring the device to full power */
9057c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 			rval = usb_handle_remote_wakeup(dip,
9087c478bd9Sstevel@tonic-gate 			    USB_REMOTE_WAKEUP_DISABLE);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 			if (rval != DDI_SUCCESS) {
9117c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_EVENTS,
9127c478bd9Sstevel@tonic-gate 				    usb_mid->mi_log_handle,
9137c478bd9Sstevel@tonic-gate 				    "usb_cleanup: disable remote "
9147c478bd9Sstevel@tonic-gate 				    "wakeup failed, rval=%d", rval);
9157c478bd9Sstevel@tonic-gate 			}
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 		(void) pm_lower_power(usb_mid->mi_dip, 0, USB_DEV_OS_PWR_OFF);
9197c478bd9Sstevel@tonic-gate 		(void) pm_idle_component(dip, 0);
9207c478bd9Sstevel@tonic-gate 	} else {
9217c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	if (midpm) {
9257c478bd9Sstevel@tonic-gate 		kmem_free(midpm, sizeof (usb_mid_power_t));
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/* free children list */
9297c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_children_dips) {
9307c478bd9Sstevel@tonic-gate 		kmem_free(usb_mid->mi_children_dips,
9317c478bd9Sstevel@tonic-gate 					usb_mid->mi_cd_list_length);
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_child_events) {
9357c478bd9Sstevel@tonic-gate 		kmem_free(usb_mid->mi_child_events, sizeof (uint8_t) *
9367c478bd9Sstevel@tonic-gate 		    usb_mid->mi_n_ifs);
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_init_state & USB_MID_MINOR_NODE_CREATED) {
9407c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	mutex_destroy(&usb_mid->mi_mutex);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate done:
9467c478bd9Sstevel@tonic-gate 	usb_client_detach(dip, usb_mid->mi_dev_data);
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_ugen_hdl) {
9497c478bd9Sstevel@tonic-gate 		(void) usb_ugen_detach(usb_mid->mi_ugen_hdl, DDI_DETACH);
9507c478bd9Sstevel@tonic-gate 		usb_ugen_release_hdl(usb_mid->mi_ugen_hdl);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(usb_mid->mi_log_handle);
9547c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(usb_mid_statep, ddi_get_instance(dip));
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate int
9637c478bd9Sstevel@tonic-gate usb_mid_devi_bind_driver(usb_mid_t *usb_mid, dev_info_t *dip)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate 	char	*name;
9667c478bd9Sstevel@tonic-gate 	uint8_t if_num = usba_get_ifno(dip);
9677c478bd9Sstevel@tonic-gate 	int	rval = USB_SUCCESS;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
9707c478bd9Sstevel@tonic-gate 	    "usb_mid_devi_bind_driver: dip = 0x%p, if_num = 0x%x", dip, if_num);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	/* bind device to the driver */
9757c478bd9Sstevel@tonic-gate 	if (ndi_devi_bind_driver(dip, 0) != NDI_SUCCESS) {
9767c478bd9Sstevel@tonic-gate 		/* if we fail to bind report an error */
9777c478bd9Sstevel@tonic-gate 		(void) usba_get_mfg_prod_sn_str(dip, name, MAXNAMELEN);
9787c478bd9Sstevel@tonic-gate 		if (name[0] != '\0') {
9797c478bd9Sstevel@tonic-gate 			if (!usb_owns_device(dip)) {
9807c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L1(DPRINT_MASK_ATTA,
9817c478bd9Sstevel@tonic-gate 				    usb_mid->mi_log_handle,
9827c478bd9Sstevel@tonic-gate 				    "no driver found for "
9837c478bd9Sstevel@tonic-gate 				    "interface %d (nodename: '%s') of %s",
9847c478bd9Sstevel@tonic-gate 				    if_num, ddi_node_name(dip), name);
9857c478bd9Sstevel@tonic-gate 			} else {
9867c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L1(DPRINT_MASK_ATTA,
9877c478bd9Sstevel@tonic-gate 				    usb_mid->mi_log_handle,
9887c478bd9Sstevel@tonic-gate 				    "no driver found for device %s", name);
9897c478bd9Sstevel@tonic-gate 			}
9907c478bd9Sstevel@tonic-gate 		} else {
9917c478bd9Sstevel@tonic-gate 			(void) ddi_pathname(dip, name);
9927c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_ATTA,
9937c478bd9Sstevel@tonic-gate 			    usb_mid->mi_log_handle,
9947c478bd9Sstevel@tonic-gate 			    "no driver found for device %s", name);
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		rval = USB_FAILURE;
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	return (rval);
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate static void
10067c478bd9Sstevel@tonic-gate usb_mid_ugen_attach(usb_mid_t *usb_mid, boolean_t remove_children)
10077c478bd9Sstevel@tonic-gate {
10087c478bd9Sstevel@tonic-gate 	_NOTE(NO_COMPETING_THREADS_NOW);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_ugen_hdl == NULL) {
10117c478bd9Sstevel@tonic-gate 		usb_ugen_info_t usb_ugen_info;
10127c478bd9Sstevel@tonic-gate 		int		rval;
10137c478bd9Sstevel@tonic-gate 		usb_ugen_hdl_t	hdl;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
10167c478bd9Sstevel@tonic-gate 			"usb_mid_ugen_attach: get handle");
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 		bzero(&usb_ugen_info, sizeof (usb_ugen_info));
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_flags = (remove_children ?
10217c478bd9Sstevel@tonic-gate 				USB_UGEN_REMOVE_CHILDREN : 0);
10227c478bd9Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_minor_node_ugen_bits_mask =
10237c478bd9Sstevel@tonic-gate 				(dev_t)USB_MID_MINOR_UGEN_BITS_MASK;
10247c478bd9Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_minor_node_instance_mask =
10257c478bd9Sstevel@tonic-gate 				(dev_t)~USB_MID_MINOR_UGEN_BITS_MASK;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
10287c478bd9Sstevel@tonic-gate 		hdl = usb_ugen_get_hdl(usb_mid->mi_dip,
10297c478bd9Sstevel@tonic-gate 						&usb_ugen_info);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 		if ((rval = usb_ugen_attach(hdl, DDI_ATTACH)) != USB_SUCCESS) {
10327c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
10337c478bd9Sstevel@tonic-gate 			    "failed to create ugen support (%d)", rval);
10347c478bd9Sstevel@tonic-gate 			usb_ugen_release_hdl(hdl);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
10377c478bd9Sstevel@tonic-gate 		} else {
10387c478bd9Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
10397c478bd9Sstevel@tonic-gate 			usb_mid->mi_ugen_hdl = hdl;
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	_NOTE(COMPETING_THREADS_NOW);
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate  * usb_mid_create_children:
10497c478bd9Sstevel@tonic-gate  */
10507c478bd9Sstevel@tonic-gate static void
10517c478bd9Sstevel@tonic-gate usb_mid_create_children(usb_mid_t *usb_mid)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
10547c478bd9Sstevel@tonic-gate 	uint_t			n_ifs;
10557c478bd9Sstevel@tonic-gate 	uint_t			i;
10567c478bd9Sstevel@tonic-gate 	dev_info_t		*cdip;
10577c478bd9Sstevel@tonic-gate 	uint_t			ugen_bound = 0;
10587c478bd9Sstevel@tonic-gate 	uint_t			bound_children = 0;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(usb_mid->mi_dip);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
10637c478bd9Sstevel@tonic-gate 	    "usb_mid_attach_child_drivers: port = %d, address = %d",
10647c478bd9Sstevel@tonic-gate 	    usba_device->usb_port, usba_device->usb_addr);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_removed_children) {
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 			return;
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	n_ifs = usb_mid->mi_n_ifs;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
10747c478bd9Sstevel@tonic-gate 	    "usb_mid_create_children: #interfaces = %d", n_ifs);
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	/*
10777c478bd9Sstevel@tonic-gate 	 * create all children if not already present
10787c478bd9Sstevel@tonic-gate 	 */
10797c478bd9Sstevel@tonic-gate 	for (i = 0; i < n_ifs; i++) {
10807c478bd9Sstevel@tonic-gate 		if (usb_mid->mi_children_dips[i] != NULL) {
1081*4610e4a0Sfrits 			if (i_ddi_node_state(
1082*4610e4a0Sfrits 				usb_mid->mi_children_dips[i]) >=
1083*4610e4a0Sfrits 				DS_BOUND) {
1084*4610e4a0Sfrits 					bound_children++;
1085*4610e4a0Sfrits 			}
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 			continue;
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
10917c478bd9Sstevel@tonic-gate 		cdip = usba_ready_interface_node(usb_mid->mi_dip, i);
10927c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
10937c478bd9Sstevel@tonic-gate 		if (cdip != NULL) {
10947c478bd9Sstevel@tonic-gate 			if (usb_mid_devi_bind_driver(usb_mid, cdip) ==
10957c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
10967c478bd9Sstevel@tonic-gate 				bound_children++;
10977c478bd9Sstevel@tonic-gate 				if (strcmp(ddi_driver_name(cdip),
10987c478bd9Sstevel@tonic-gate 				    "ugen") == 0) {
10997c478bd9Sstevel@tonic-gate 					ugen_bound++;
11007c478bd9Sstevel@tonic-gate 				}
11017c478bd9Sstevel@tonic-gate 			}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 			usb_mid->mi_children_dips[i] = cdip;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	usb_mid->mi_removed_children = (bound_children ? B_FALSE : B_TRUE);
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	/*
11117c478bd9Sstevel@tonic-gate 	 * if there are no ugen interface children, create ugen support at
11127c478bd9Sstevel@tonic-gate 	 * device level, use a separate thread because we may be at interrupt
11137c478bd9Sstevel@tonic-gate 	 * level
11147c478bd9Sstevel@tonic-gate 	 */
11157c478bd9Sstevel@tonic-gate 	if ((ugen_bound == 0) && (usb_mid->mi_ugen_hdl == NULL)) {
11167c478bd9Sstevel@tonic-gate 		/*
11177c478bd9Sstevel@tonic-gate 		 * we only need to remove the children if there are
11187c478bd9Sstevel@tonic-gate 		 * multiple configurations which would fail if there
11197c478bd9Sstevel@tonic-gate 		 * are child interfaces
11207c478bd9Sstevel@tonic-gate 		 */
11217c478bd9Sstevel@tonic-gate 		if ((usb_mid->mi_removed_children == B_FALSE) &&
11227c478bd9Sstevel@tonic-gate 		    (usba_device->usb_n_cfgs > 1)) {
11237c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_ATTA,
11247c478bd9Sstevel@tonic-gate 			    usb_mid->mi_log_handle,
11257c478bd9Sstevel@tonic-gate 			    "can't support ugen for multiple "
11267c478bd9Sstevel@tonic-gate 			    "configurations devices that have attached "
11277c478bd9Sstevel@tonic-gate 			    "child interface drivers");
11287c478bd9Sstevel@tonic-gate 		} else {
11297c478bd9Sstevel@tonic-gate 			usb_mid_ugen_attach(usb_mid,
11307c478bd9Sstevel@tonic-gate 			    usb_mid->mi_removed_children);
11317c478bd9Sstevel@tonic-gate 		}
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate /*
11377c478bd9Sstevel@tonic-gate  * event support
11387c478bd9Sstevel@tonic-gate  */
11397c478bd9Sstevel@tonic-gate static int
11407c478bd9Sstevel@tonic-gate usb_mid_busop_get_eventcookie(dev_info_t *dip,
11417c478bd9Sstevel@tonic-gate 	dev_info_t *rdip, char *eventname, ddi_eventcookie_t *cookie)
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11467c478bd9Sstevel@tonic-gate 	    "usb_mid_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
11477c478bd9Sstevel@tonic-gate 	    "event=%s", (void *)dip, (void *)rdip, eventname);
11487c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11497c478bd9Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d)",
11507c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
11517c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	/* return event cookie, iblock cookie, and level */
11547c478bd9Sstevel@tonic-gate 	return (ndi_event_retrieve_cookie(usb_mid->mi_ndi_event_hdl,
11557c478bd9Sstevel@tonic-gate 		rdip, eventname, cookie, NDI_EVENT_NOPASS));
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate static int
11607c478bd9Sstevel@tonic-gate usb_mid_busop_add_eventcall(dev_info_t *dip,
11617c478bd9Sstevel@tonic-gate 	dev_info_t *rdip,
11627c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
11637c478bd9Sstevel@tonic-gate 	void (*callback)(dev_info_t *dip,
11647c478bd9Sstevel@tonic-gate 	    ddi_eventcookie_t cookie, void *arg,
11657c478bd9Sstevel@tonic-gate 	    void *bus_impldata),
11667c478bd9Sstevel@tonic-gate 	void *arg, ddi_callback_id_t *cb_id)
11677c478bd9Sstevel@tonic-gate {
11687c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
11697c478bd9Sstevel@tonic-gate 	int	ifno = usba_get_ifno(rdip);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11727c478bd9Sstevel@tonic-gate 	    "usb_mid_busop_add_eventcall: dip=0x%p, rdip=0x%p "
11737c478bd9Sstevel@tonic-gate 	    "cookie=0x%p, cb=0x%p, arg=0x%p",
11747c478bd9Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
11757c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11767c478bd9Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
11777c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
11787c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
11797c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl, cookie));
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/* Set flag on children registering events */
11827c478bd9Sstevel@tonic-gate 	switch (ndi_event_cookie_to_tag(usb_mid->mi_ndi_event_hdl, cookie)) {
11837c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
11847c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
11857c478bd9Sstevel@tonic-gate 		usb_mid->mi_child_events[ifno] |=
11867c478bd9Sstevel@tonic-gate 		    USB_MID_CHILD_EVENT_DISCONNECT;
11877c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 		break;
11907c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
11917c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
11927c478bd9Sstevel@tonic-gate 		usb_mid->mi_child_events[ifno] |=
11937c478bd9Sstevel@tonic-gate 		    USB_MID_CHILD_EVENT_PRESUSPEND;
11947c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 		break;
11977c478bd9Sstevel@tonic-gate 	default:
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		break;
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 	/* add callback (perform registration) */
12027c478bd9Sstevel@tonic-gate 	return (ndi_event_add_callback(usb_mid->mi_ndi_event_hdl,
12037c478bd9Sstevel@tonic-gate 		rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate static int
12087c478bd9Sstevel@tonic-gate usb_mid_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
12097c478bd9Sstevel@tonic-gate {
12107c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
12117c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	ASSERT(cb);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
12167c478bd9Sstevel@tonic-gate 	    "usb_mid_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
12177c478bd9Sstevel@tonic-gate 	    "cookie=0x%p", (void *)dip, cb->ndi_evtcb_dip,
12187c478bd9Sstevel@tonic-gate 	    cb->ndi_evtcb_cookie);
12197c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
12207c478bd9Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
12217c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
12227c478bd9Sstevel@tonic-gate 	    ddi_driver_name(cb->ndi_evtcb_dip),
12237c478bd9Sstevel@tonic-gate 	    ddi_get_instance(cb->ndi_evtcb_dip),
12247c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl,
12257c478bd9Sstevel@tonic-gate 	    cb->ndi_evtcb_cookie));
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	/* remove event registration from our event set */
12287c478bd9Sstevel@tonic-gate 	return (ndi_event_remove_callback(usb_mid->mi_ndi_event_hdl, cb_id));
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate static int
12337c478bd9Sstevel@tonic-gate usb_mid_busop_post_event(dev_info_t *dip,
12347c478bd9Sstevel@tonic-gate 	dev_info_t *rdip,
12357c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
12367c478bd9Sstevel@tonic-gate 	void *bus_impldata)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
12417c478bd9Sstevel@tonic-gate 	    "usb_mid_busop_post_event: dip=0x%p, rdip=0x%p "
12427c478bd9Sstevel@tonic-gate 	    "cookie=0x%p, impl=0x%p",
12437c478bd9Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, bus_impldata);
12447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
12457c478bd9Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
12467c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
12477c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
12487c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl, cookie));
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/* post event to all children registered for this event */
12517c478bd9Sstevel@tonic-gate 	return (ndi_event_run_callbacks(usb_mid->mi_ndi_event_hdl, rdip,
12527c478bd9Sstevel@tonic-gate 		    cookie, bus_impldata));
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate /*
12577c478bd9Sstevel@tonic-gate  * usb_mid_restore_device_state
12587c478bd9Sstevel@tonic-gate  *	set the original configuration of the device
12597c478bd9Sstevel@tonic-gate  */
12607c478bd9Sstevel@tonic-gate static int
12617c478bd9Sstevel@tonic-gate usb_mid_restore_device_state(dev_info_t *dip, usb_mid_t *usb_mid)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	usb_mid_power_t		*midpm;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
12667c478bd9Sstevel@tonic-gate 	    "usb_mid_restore_device_state: usb_mid = %p", usb_mid);
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
12697c478bd9Sstevel@tonic-gate 	midpm = usb_mid->mi_pm;
12707c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	/* First bring the device to full power */
12737c478bd9Sstevel@tonic-gate 	(void) pm_busy_component(dip, 0);
12747c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	if (usb_check_same_device(dip, usb_mid->mi_log_handle, USB_LOG_L0,
12777c478bd9Sstevel@tonic-gate 	    DPRINT_MASK_EVENTS, USB_CHK_VIDPID, NULL) != USB_SUCCESS) {
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 		/* change the device state from suspended to disconnected */
12807c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
12817c478bd9Sstevel@tonic-gate 		usb_mid->mi_dev_state = USB_DEV_DISCONNECTED;
12827c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
12837c478bd9Sstevel@tonic-gate 		(void) pm_idle_component(dip, 0);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
12867c478bd9Sstevel@tonic-gate 	}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * if the device had remote wakeup earlier,
12907c478bd9Sstevel@tonic-gate 	 * enable it again
12917c478bd9Sstevel@tonic-gate 	 */
12927c478bd9Sstevel@tonic-gate 	if (midpm->mip_wakeup_enabled) {
12937c478bd9Sstevel@tonic-gate 		(void) usb_handle_remote_wakeup(usb_mid->mi_dip,
12947c478bd9Sstevel@tonic-gate 		    USB_REMOTE_WAKEUP_ENABLE);
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
12987c478bd9Sstevel@tonic-gate 	usb_mid->mi_dev_state = USB_DEV_ONLINE;
12997c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	(void) pm_idle_component(dip, 0);
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate /*
13087c478bd9Sstevel@tonic-gate  * usb_mid_event_cb()
13097c478bd9Sstevel@tonic-gate  *	handle disconnect and connect events
13107c478bd9Sstevel@tonic-gate  */
13117c478bd9Sstevel@tonic-gate static void
13127c478bd9Sstevel@tonic-gate usb_mid_event_cb(dev_info_t *dip, ddi_eventcookie_t cookie,
13137c478bd9Sstevel@tonic-gate 	void *arg, void *bus_impldata)
13147c478bd9Sstevel@tonic-gate {
13157c478bd9Sstevel@tonic-gate 	int		i, tag;
13167c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid = usb_mid_obtain_state(dip);
13177c478bd9Sstevel@tonic-gate 	dev_info_t	*child_dip;
13187c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t rm_cookie, ins_cookie, suspend_cookie, resume_cookie;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
13217c478bd9Sstevel@tonic-gate 	    "usb_mid_event_cb: dip=0x%p, cookie=0x%p, "
13227c478bd9Sstevel@tonic-gate 	    "arg=0x%p, impl=0x%p",
13237c478bd9Sstevel@tonic-gate 	    (void *)dip, (void *)cookie, arg, bus_impldata);
13247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
13257c478bd9Sstevel@tonic-gate 	    "(dip=%s%d event=%s)",
13267c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
13277c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl, cookie));
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	tag = NDI_EVENT_TAG(cookie);
13307c478bd9Sstevel@tonic-gate 	rm_cookie = ndi_event_tag_to_cookie(
13317c478bd9Sstevel@tonic-gate 	    usb_mid->mi_ndi_event_hdl, USBA_EVENT_TAG_HOT_REMOVAL);
13327c478bd9Sstevel@tonic-gate 	suspend_cookie = ndi_event_tag_to_cookie(
13337c478bd9Sstevel@tonic-gate 	    usb_mid->mi_ndi_event_hdl, USBA_EVENT_TAG_PRE_SUSPEND);
13347c478bd9Sstevel@tonic-gate 	ins_cookie = ndi_event_tag_to_cookie(
13357c478bd9Sstevel@tonic-gate 	    usb_mid->mi_ndi_event_hdl, USBA_EVENT_TAG_HOT_INSERTION);
13367c478bd9Sstevel@tonic-gate 	resume_cookie = ndi_event_tag_to_cookie(
13377c478bd9Sstevel@tonic-gate 	    usb_mid->mi_ndi_event_hdl, USBA_EVENT_TAG_POST_RESUME);
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
13407c478bd9Sstevel@tonic-gate 	switch (tag) {
13417c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
13427c478bd9Sstevel@tonic-gate 		if (usb_mid->mi_dev_state == USB_DEV_DISCONNECTED) {
13437c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_EVENTS,
13447c478bd9Sstevel@tonic-gate 			    usb_mid->mi_log_handle,
13457c478bd9Sstevel@tonic-gate 			    "usb_mid_event_cb: Device already disconnected");
13467c478bd9Sstevel@tonic-gate 		} else {
13477c478bd9Sstevel@tonic-gate 			/* we are disconnected so set our state now */
13487c478bd9Sstevel@tonic-gate 			usb_mid->mi_dev_state = USB_DEV_DISCONNECTED;
13497c478bd9Sstevel@tonic-gate 			for (i = 0; i < usb_mid->mi_n_ifs; i++) {
13507c478bd9Sstevel@tonic-gate 				usb_mid->mi_child_events[i] &= ~
13517c478bd9Sstevel@tonic-gate 				    USB_MID_CHILD_EVENT_DISCONNECT;
13527c478bd9Sstevel@tonic-gate 			}
13537c478bd9Sstevel@tonic-gate 			mutex_exit(&usb_mid->mi_mutex);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 			/* pass disconnect event to all the children */
13567c478bd9Sstevel@tonic-gate 			(void) ndi_event_run_callbacks(
13577c478bd9Sstevel@tonic-gate 			    usb_mid->mi_ndi_event_hdl, NULL,
13587c478bd9Sstevel@tonic-gate 			    rm_cookie, bus_impldata);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 			if (usb_mid->mi_ugen_hdl) {
13617c478bd9Sstevel@tonic-gate 				(void) usb_ugen_disconnect_ev_cb(
13627c478bd9Sstevel@tonic-gate 						usb_mid->mi_ugen_hdl);
13637c478bd9Sstevel@tonic-gate 			}
13647c478bd9Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 		break;
13677c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
13687c478bd9Sstevel@tonic-gate 		/* set our state *after* suspending children */
13697c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 		/* pass pre_suspend event to all the children */
13727c478bd9Sstevel@tonic-gate 		(void) ndi_event_run_callbacks(usb_mid->mi_ndi_event_hdl,
13737c478bd9Sstevel@tonic-gate 		    NULL, suspend_cookie, bus_impldata);
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
13767c478bd9Sstevel@tonic-gate 		for (i = 0; i < usb_mid->mi_n_ifs; i++) {
13777c478bd9Sstevel@tonic-gate 			usb_mid->mi_child_events[i] &= ~
13787c478bd9Sstevel@tonic-gate 			    USB_MID_CHILD_EVENT_PRESUSPEND;
13797c478bd9Sstevel@tonic-gate 		}
13807c478bd9Sstevel@tonic-gate 		break;
13817c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_INSERTION:
13827c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
13837c478bd9Sstevel@tonic-gate 		if (usb_mid_restore_device_state(dip, usb_mid) == USB_SUCCESS) {
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 			/*
13867c478bd9Sstevel@tonic-gate 			 * Check to see if this child has missed the disconnect
13877c478bd9Sstevel@tonic-gate 			 * event before it registered for event cb
13887c478bd9Sstevel@tonic-gate 			 */
13897c478bd9Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
13907c478bd9Sstevel@tonic-gate 			for (i = 0; i < usb_mid->mi_n_ifs; i++) {
13917c478bd9Sstevel@tonic-gate 				if (usb_mid->mi_child_events[i] &
13927c478bd9Sstevel@tonic-gate 				    USB_MID_CHILD_EVENT_DISCONNECT) {
13937c478bd9Sstevel@tonic-gate 					usb_mid->mi_child_events[i] &=
13947c478bd9Sstevel@tonic-gate 					    ~USB_MID_CHILD_EVENT_DISCONNECT;
13957c478bd9Sstevel@tonic-gate 					child_dip =
13967c478bd9Sstevel@tonic-gate 					    usb_mid->mi_children_dips[i];
13977c478bd9Sstevel@tonic-gate 					mutex_exit(&usb_mid->mi_mutex);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 					/* post the missed disconnect */
14007c478bd9Sstevel@tonic-gate 					(void) ndi_event_do_callback(
14017c478bd9Sstevel@tonic-gate 					    usb_mid->mi_ndi_event_hdl,
14027c478bd9Sstevel@tonic-gate 					    child_dip,
14037c478bd9Sstevel@tonic-gate 					    rm_cookie,
14047c478bd9Sstevel@tonic-gate 					    bus_impldata);
14057c478bd9Sstevel@tonic-gate 					mutex_enter(&usb_mid->mi_mutex);
14067c478bd9Sstevel@tonic-gate 				}
14077c478bd9Sstevel@tonic-gate 			}
14087c478bd9Sstevel@tonic-gate 			mutex_exit(&usb_mid->mi_mutex);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 			/* pass reconnect event to all the children */
14117c478bd9Sstevel@tonic-gate 			(void) ndi_event_run_callbacks(
14127c478bd9Sstevel@tonic-gate 			    usb_mid->mi_ndi_event_hdl, NULL,
14137c478bd9Sstevel@tonic-gate 			    ins_cookie, bus_impldata);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 			if (usb_mid->mi_ugen_hdl) {
14167c478bd9Sstevel@tonic-gate 				(void) usb_ugen_reconnect_ev_cb(
14177c478bd9Sstevel@tonic-gate 						usb_mid->mi_ugen_hdl);
14187c478bd9Sstevel@tonic-gate 			}
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
14217c478bd9Sstevel@tonic-gate 		break;
14227c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_POST_RESUME:
14237c478bd9Sstevel@tonic-gate 		/*
14247c478bd9Sstevel@tonic-gate 		 * Check to see if this child has missed the pre-suspend
14257c478bd9Sstevel@tonic-gate 		 * event before it registered for event cb
14267c478bd9Sstevel@tonic-gate 		 */
14277c478bd9Sstevel@tonic-gate 		for (i = 0; i < usb_mid->mi_n_ifs; i++) {
14287c478bd9Sstevel@tonic-gate 			if (usb_mid->mi_child_events[i] &
14297c478bd9Sstevel@tonic-gate 			    USB_MID_CHILD_EVENT_PRESUSPEND) {
14307c478bd9Sstevel@tonic-gate 				usb_mid->mi_child_events[i] &=
14317c478bd9Sstevel@tonic-gate 				    ~USB_MID_CHILD_EVENT_PRESUSPEND;
14327c478bd9Sstevel@tonic-gate 				child_dip = usb_mid->mi_children_dips[i];
14337c478bd9Sstevel@tonic-gate 				mutex_exit(&usb_mid->mi_mutex);
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 				/* post the missed pre-suspend event */
14367c478bd9Sstevel@tonic-gate 				(void) ndi_event_do_callback(
14377c478bd9Sstevel@tonic-gate 				    usb_mid->mi_ndi_event_hdl,
14387c478bd9Sstevel@tonic-gate 				    child_dip, suspend_cookie,
14397c478bd9Sstevel@tonic-gate 				    bus_impldata);
14407c478bd9Sstevel@tonic-gate 				mutex_enter(&usb_mid->mi_mutex);
14417c478bd9Sstevel@tonic-gate 			}
14427c478bd9Sstevel@tonic-gate 		}
14437c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 		/* pass post_resume event to all the children */
14467c478bd9Sstevel@tonic-gate 		(void) ndi_event_run_callbacks(usb_mid->mi_ndi_event_hdl,
14477c478bd9Sstevel@tonic-gate 		    NULL, resume_cookie, bus_impldata);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
14507c478bd9Sstevel@tonic-gate 		break;
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate }
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate /*
14587c478bd9Sstevel@tonic-gate  * register and unregister for events from our parent
14597c478bd9Sstevel@tonic-gate  *
14607c478bd9Sstevel@tonic-gate  * Note: usb_mid doesn't use the cookie fields in usba_device structure.
14617c478bd9Sstevel@tonic-gate  *	They are used/shared by children of usb_mid.
14627c478bd9Sstevel@tonic-gate  */
14637c478bd9Sstevel@tonic-gate static void
14647c478bd9Sstevel@tonic-gate usb_mid_register_events(usb_mid_t *usb_mid)
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	int rval;
14677c478bd9Sstevel@tonic-gate 	usba_evdata_t *evdata;
14687c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
14717c478bd9Sstevel@tonic-gate 	    "usb_mid_register_events:");
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	evdata = usba_get_evdata(usb_mid->mi_dip);
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	/* get event cookie, discard level and icookie for now */
14767c478bd9Sstevel@tonic-gate 	rval = ddi_get_eventcookie(usb_mid->mi_dip, DDI_DEVI_REMOVE_EVENT,
14777c478bd9Sstevel@tonic-gate 		&cookie);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
14807c478bd9Sstevel@tonic-gate 		rval = ddi_add_event_handler(usb_mid->mi_dip,
14817c478bd9Sstevel@tonic-gate 		    cookie, usb_mid_event_cb, NULL, &evdata->ev_rm_cb_id);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		if (rval != DDI_SUCCESS) {
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 			goto fail;
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 	}
14887c478bd9Sstevel@tonic-gate 	rval = ddi_get_eventcookie(usb_mid->mi_dip, DDI_DEVI_INSERT_EVENT,
14897c478bd9Sstevel@tonic-gate 		&cookie);
14907c478bd9Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
14917c478bd9Sstevel@tonic-gate 		rval = ddi_add_event_handler(usb_mid->mi_dip,
14927c478bd9Sstevel@tonic-gate 		    cookie, usb_mid_event_cb, NULL, &evdata->ev_ins_cb_id);
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 		if (rval != DDI_SUCCESS) {
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 			goto fail;
14977c478bd9Sstevel@tonic-gate 		}
14987c478bd9Sstevel@tonic-gate 	}
14997c478bd9Sstevel@tonic-gate 	rval = ddi_get_eventcookie(usb_mid->mi_dip, USBA_PRE_SUSPEND_EVENT,
15007c478bd9Sstevel@tonic-gate 		&cookie);
15017c478bd9Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
15027c478bd9Sstevel@tonic-gate 		rval = ddi_add_event_handler(usb_mid->mi_dip,
15037c478bd9Sstevel@tonic-gate 		    cookie, usb_mid_event_cb, NULL, &evdata->ev_suspend_cb_id);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 		if (rval != DDI_SUCCESS) {
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 			goto fail;
15087c478bd9Sstevel@tonic-gate 		}
15097c478bd9Sstevel@tonic-gate 	}
15107c478bd9Sstevel@tonic-gate 	rval = ddi_get_eventcookie(usb_mid->mi_dip, USBA_POST_RESUME_EVENT,
15117c478bd9Sstevel@tonic-gate 		&cookie);
15127c478bd9Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
15137c478bd9Sstevel@tonic-gate 		rval = ddi_add_event_handler(usb_mid->mi_dip,
15147c478bd9Sstevel@tonic-gate 		    cookie, usb_mid_event_cb, NULL,
15157c478bd9Sstevel@tonic-gate 		    &evdata->ev_resume_cb_id);
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 		if (rval != DDI_SUCCESS) {
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 			goto fail;
15207c478bd9Sstevel@tonic-gate 		}
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	return;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate fail:
15277c478bd9Sstevel@tonic-gate 	usb_mid_unregister_events(usb_mid);
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate }
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate static void
15337c478bd9Sstevel@tonic-gate usb_mid_unregister_events(usb_mid_t *usb_mid)
15347c478bd9Sstevel@tonic-gate {
15357c478bd9Sstevel@tonic-gate 	int rval;
15367c478bd9Sstevel@tonic-gate 	usba_evdata_t *evdata;
15377c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(usb_mid->mi_dip);
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
15407c478bd9Sstevel@tonic-gate 	    "usb_mid_unregister_events:");
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	evdata = usba_get_evdata(usb_mid->mi_dip);
15437c478bd9Sstevel@tonic-gate 	if (evdata) {
15447c478bd9Sstevel@tonic-gate 		if (evdata->ev_rm_cb_id) {
15457c478bd9Sstevel@tonic-gate 			rval = ddi_remove_event_handler(evdata->ev_rm_cb_id);
15467c478bd9Sstevel@tonic-gate 			ASSERT(rval == DDI_SUCCESS);
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 		if (evdata->ev_ins_cb_id) {
15507c478bd9Sstevel@tonic-gate 			rval = ddi_remove_event_handler(evdata->ev_ins_cb_id);
15517c478bd9Sstevel@tonic-gate 			ASSERT(rval == DDI_SUCCESS);
15527c478bd9Sstevel@tonic-gate 		}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 		if (evdata->ev_resume_cb_id) {
15557c478bd9Sstevel@tonic-gate 			rval =
15567c478bd9Sstevel@tonic-gate 			    ddi_remove_event_handler(evdata->ev_resume_cb_id);
15577c478bd9Sstevel@tonic-gate 			ASSERT(rval == DDI_SUCCESS);
15587c478bd9Sstevel@tonic-gate 		}
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 		if (evdata->ev_suspend_cb_id) {
15617c478bd9Sstevel@tonic-gate 			rval =
15627c478bd9Sstevel@tonic-gate 			    ddi_remove_event_handler(evdata->ev_suspend_cb_id);
15637c478bd9Sstevel@tonic-gate 			ASSERT(rval == DDI_SUCCESS);
15647c478bd9Sstevel@tonic-gate 		}
15657c478bd9Sstevel@tonic-gate 	}
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/* clear event data for children, required for cfgmadm unconfigure */
15687c478bd9Sstevel@tonic-gate 	usba_free_evdata(usba_device->usb_evdata);
15697c478bd9Sstevel@tonic-gate 	usba_device->usb_evdata = NULL;
15707c478bd9Sstevel@tonic-gate 	usba_device->rm_cookie = NULL;
15717c478bd9Sstevel@tonic-gate 	usba_device->ins_cookie = NULL;
15727c478bd9Sstevel@tonic-gate 	usba_device->suspend_cookie = NULL;
15737c478bd9Sstevel@tonic-gate 	usba_device->resume_cookie = NULL;
15747c478bd9Sstevel@tonic-gate }
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate /*
15787c478bd9Sstevel@tonic-gate  * create the pm components required for power management
15797c478bd9Sstevel@tonic-gate  */
15807c478bd9Sstevel@tonic-gate static void
15817c478bd9Sstevel@tonic-gate usb_mid_create_pm_components(dev_info_t *dip, usb_mid_t *usb_mid)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	usb_mid_power_t	*midpm;
15847c478bd9Sstevel@tonic-gate 	uint_t		pwr_states;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
15877c478bd9Sstevel@tonic-gate 	    "usb_mid_create_pm_components: Begin");
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	/* Allocate the PM state structure */
15907c478bd9Sstevel@tonic-gate 	midpm = kmem_zalloc(sizeof (usb_mid_power_t), KM_SLEEP);
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
15937c478bd9Sstevel@tonic-gate 	usb_mid->mi_pm = midpm;
15947c478bd9Sstevel@tonic-gate 	midpm->mip_usb_mid = usb_mid;
15957c478bd9Sstevel@tonic-gate 	midpm->mip_pm_capabilities = 0; /* XXXX should this be 0?? */
15967c478bd9Sstevel@tonic-gate 	midpm->mip_current_power = USB_DEV_OS_FULL_PWR;
15977c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	/*
16007c478bd9Sstevel@tonic-gate 	 * By not enabling parental notification, PM enforces
16017c478bd9Sstevel@tonic-gate 	 * "strict parental dependency" meaning, usb_mid won't
16027c478bd9Sstevel@tonic-gate 	 * power off until any of its children are in full power.
16037c478bd9Sstevel@tonic-gate 	 */
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	/*
16067c478bd9Sstevel@tonic-gate 	 * there are 3 scenarios:
16077c478bd9Sstevel@tonic-gate 	 * 1. a well behaved device should have remote wakeup
16087c478bd9Sstevel@tonic-gate 	 * at interface and device level. If the interface
16097c478bd9Sstevel@tonic-gate 	 * wakes up, usb_mid will wake up
16107c478bd9Sstevel@tonic-gate 	 * 2. if the device doesn't have remote wake up and
16117c478bd9Sstevel@tonic-gate 	 * the interface has, PM will still work, ie.
16127c478bd9Sstevel@tonic-gate 	 * the interfaces wakes up and usb_mid wakes up
16137c478bd9Sstevel@tonic-gate 	 * 3. if neither the interface nor device has remote
16147c478bd9Sstevel@tonic-gate 	 * wakeup, the interface will wake up when it is opened
16157c478bd9Sstevel@tonic-gate 	 * and goes to sleep after being closed for a while
16167c478bd9Sstevel@tonic-gate 	 * In this case usb_mid should also go to sleep shortly
16177c478bd9Sstevel@tonic-gate 	 * thereafter
16187c478bd9Sstevel@tonic-gate 	 * In all scenarios it doesn't really matter whether
16197c478bd9Sstevel@tonic-gate 	 * remote wakeup at the device level is enabled or not
16207c478bd9Sstevel@tonic-gate 	 * but we do it anyways
16217c478bd9Sstevel@tonic-gate 	 */
16227c478bd9Sstevel@tonic-gate 	if (usb_handle_remote_wakeup(dip, USB_REMOTE_WAKEUP_ENABLE) ==
16237c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
16247c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PM, usb_mid->mi_log_handle,
16257c478bd9Sstevel@tonic-gate 		    "usb_mid_create_pm_components: "
16267c478bd9Sstevel@tonic-gate 		    "Remote Wakeup Enabled");
16277c478bd9Sstevel@tonic-gate 		midpm->mip_wakeup_enabled = 1;
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	if (usb_create_pm_components(dip, &pwr_states) ==
16317c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
16327c478bd9Sstevel@tonic-gate 		midpm->mip_pwr_states = (uint8_t)pwr_states;
16337c478bd9Sstevel@tonic-gate 		(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
16347c478bd9Sstevel@tonic-gate 	}
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
16377c478bd9Sstevel@tonic-gate 	    "usb_mid_create_pm_components: End");
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate /*
16427c478bd9Sstevel@tonic-gate  * usb_mid_obtain_state:
16437c478bd9Sstevel@tonic-gate  */
16447c478bd9Sstevel@tonic-gate usb_mid_t *
16457c478bd9Sstevel@tonic-gate usb_mid_obtain_state(dev_info_t *dip)
16467c478bd9Sstevel@tonic-gate {
16477c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
16487c478bd9Sstevel@tonic-gate 	usb_mid_t *statep = ddi_get_soft_state(usb_mid_statep, instance);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	ASSERT(statep != NULL);
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	return (statep);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate /*
16577c478bd9Sstevel@tonic-gate  * ugen support
16587c478bd9Sstevel@tonic-gate  */
16597c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
16607c478bd9Sstevel@tonic-gate static int
16617c478bd9Sstevel@tonic-gate usb_mid_open(dev_t *devp, int flags, int otyp, cred_t *credp)
16627c478bd9Sstevel@tonic-gate {
16637c478bd9Sstevel@tonic-gate 	struct usb_mid *usb_mid;
16647c478bd9Sstevel@tonic-gate 	int	rval;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
16677c478bd9Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(*devp)))) == NULL) {
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 		return (ENXIO);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, usb_mid->mi_log_handle,
16737c478bd9Sstevel@tonic-gate 	    "usb_mid_open: usb_mid = 0x%p *devp = 0x%lx", usb_mid, *devp);
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	/* First bring the device to full power */
16767c478bd9Sstevel@tonic-gate 	(void) pm_busy_component(usb_mid->mi_dip, 0);
16777c478bd9Sstevel@tonic-gate 	(void) pm_raise_power(usb_mid->mi_dip, 0, USB_DEV_OS_FULL_PWR);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	rval = usb_ugen_open(usb_mid->mi_ugen_hdl, devp, flags, otyp,
16817c478bd9Sstevel@tonic-gate 								credp);
16827c478bd9Sstevel@tonic-gate 	if (rval) {
16837c478bd9Sstevel@tonic-gate 		(void) pm_idle_component(usb_mid->mi_dip, 0);
16847c478bd9Sstevel@tonic-gate 	} else {
16857c478bd9Sstevel@tonic-gate 		/*
16867c478bd9Sstevel@tonic-gate 		 * since all ugen opens are exclusive we can count the
16877c478bd9Sstevel@tonic-gate 		 * opens
16887c478bd9Sstevel@tonic-gate 		 */
16897c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
16907c478bd9Sstevel@tonic-gate 		usb_mid->mi_ugen_open_count++;
16917c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	return (rval);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate /* ARGSUSED */
16997c478bd9Sstevel@tonic-gate static int
17007c478bd9Sstevel@tonic-gate usb_mid_close(dev_t dev, int flag, int otyp, cred_t *credp)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	struct usb_mid *usb_mid;
17037c478bd9Sstevel@tonic-gate 	int rval;
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
17067c478bd9Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(dev)))) == NULL) {
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 		return (ENXIO);
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	rval = usb_ugen_close(usb_mid->mi_ugen_hdl, dev, flag, otyp,
17127c478bd9Sstevel@tonic-gate 								credp);
17137c478bd9Sstevel@tonic-gate 	if (rval == 0) {
17147c478bd9Sstevel@tonic-gate 		(void) pm_idle_component(usb_mid->mi_dip, 0);
17157c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
17167c478bd9Sstevel@tonic-gate 		usb_mid->mi_ugen_open_count--;
17177c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
17187c478bd9Sstevel@tonic-gate 	}
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	return (rval);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate static int
17257c478bd9Sstevel@tonic-gate usb_mid_read(dev_t dev, struct uio *uio, cred_t *credp)
17267c478bd9Sstevel@tonic-gate {
17277c478bd9Sstevel@tonic-gate 	struct usb_mid *usb_mid;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
17307c478bd9Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(dev)))) == NULL) {
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 		return (ENXIO);
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	return (usb_ugen_read(usb_mid->mi_ugen_hdl, dev, uio, credp));
17367c478bd9Sstevel@tonic-gate }
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate static int
17407c478bd9Sstevel@tonic-gate usb_mid_write(dev_t dev, struct uio *uio, cred_t *credp)
17417c478bd9Sstevel@tonic-gate {
17427c478bd9Sstevel@tonic-gate 	struct usb_mid *usb_mid;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
17457c478bd9Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(dev)))) == NULL) {
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 		return (ENXIO);
17487c478bd9Sstevel@tonic-gate 	}
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	return (usb_ugen_write(usb_mid->mi_ugen_hdl, dev, uio, credp));
17517c478bd9Sstevel@tonic-gate }
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate static int
17557c478bd9Sstevel@tonic-gate usb_mid_poll(dev_t dev, short events, int anyyet,  short *reventsp,
17567c478bd9Sstevel@tonic-gate     struct pollhead **phpp)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate 	struct usb_mid *usb_mid;
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
17617c478bd9Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(dev)))) == NULL) {
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 		return (ENXIO);
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	return (usb_ugen_poll(usb_mid->mi_ugen_hdl, dev, events,
17677c478bd9Sstevel@tonic-gate 						anyyet, reventsp, phpp));
17687c478bd9Sstevel@tonic-gate }
1769