xref: /onnv-gate/usr/src/uts/common/io/usb/usb_mid/usb_mid.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52840Scarlsonj  * Common Development and Distribution License (the "License").
62840Scarlsonj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
202840Scarlsonj  */
212840Scarlsonj /*
226898Sfb209375  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * usb multi interface and common class driver
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  *	this driver attempts to attach each interface to a driver
310Sstevel@tonic-gate  *	and may eventually handle common class features such as
320Sstevel@tonic-gate  *	shared endpoints
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #if defined(lint) && !defined(DEBUG)
360Sstevel@tonic-gate #define	DEBUG	1
370Sstevel@tonic-gate #endif
380Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
390Sstevel@tonic-gate #include <sys/usb/usba.h>
400Sstevel@tonic-gate #include <sys/usb/usba/usba_types.h>
410Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
420Sstevel@tonic-gate #include <sys/usb/usba/usba_ugen.h>
430Sstevel@tonic-gate #include <sys/usb/usb_mid/usb_midvar.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate void usba_free_evdata(usba_evdata_t *);
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /* Debugging support */
48880Sfrits uint_t usb_mid_errlevel = USB_LOG_L4;
49880Sfrits uint_t usb_mid_errmask = (uint_t)DPRINT_MASK_ALL;
50880Sfrits uint_t usb_mid_instance_debug = (uint_t)-1;
51880Sfrits uint_t usb_mid_bus_config_debug = 0;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_errlevel))
540Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_errmask))
550Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_instance_debug))
560Sstevel@tonic-gate 
570Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
580Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
590Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", usb_pipe_policy))
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Hotplug support
630Sstevel@tonic-gate  * Leaf ops (hotplug controls for client devices)
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static int usb_mid_open(dev_t *, int, int, cred_t *);
660Sstevel@tonic-gate static int usb_mid_close(dev_t, int, int, cred_t *);
670Sstevel@tonic-gate static int usb_mid_read(dev_t, struct uio *, cred_t *);
680Sstevel@tonic-gate static int usb_mid_write(dev_t, struct uio *, cred_t *);
690Sstevel@tonic-gate static int usb_mid_poll(dev_t, short, int,  short *,
700Sstevel@tonic-gate 					struct pollhead **);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static struct cb_ops usb_mid_cb_ops = {
730Sstevel@tonic-gate 	usb_mid_open,
740Sstevel@tonic-gate 	usb_mid_close,
750Sstevel@tonic-gate 	nodev,		/* strategy */
760Sstevel@tonic-gate 	nodev,		/* print */
770Sstevel@tonic-gate 	nodev,		/* dump */
780Sstevel@tonic-gate 	usb_mid_read,	/* read */
790Sstevel@tonic-gate 	usb_mid_write,	/* write */
800Sstevel@tonic-gate 	nodev,
810Sstevel@tonic-gate 	nodev,		/* devmap */
820Sstevel@tonic-gate 	nodev,		/* mmap */
830Sstevel@tonic-gate 	nodev,		/* segmap */
840Sstevel@tonic-gate 	usb_mid_poll,	/* poll */
850Sstevel@tonic-gate 	ddi_prop_op,	/* prop_op */
860Sstevel@tonic-gate 	NULL,
870Sstevel@tonic-gate 	D_MP
880Sstevel@tonic-gate };
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static int usb_mid_busop_get_eventcookie(dev_info_t *dip,
910Sstevel@tonic-gate 			dev_info_t *rdip,
920Sstevel@tonic-gate 			char *eventname,
930Sstevel@tonic-gate 			ddi_eventcookie_t *cookie);
940Sstevel@tonic-gate static int usb_mid_busop_add_eventcall(dev_info_t *dip,
950Sstevel@tonic-gate 			dev_info_t *rdip,
960Sstevel@tonic-gate 			ddi_eventcookie_t cookie,
970Sstevel@tonic-gate 			void (*callback)(dev_info_t *dip,
980Sstevel@tonic-gate 				ddi_eventcookie_t cookie, void *arg,
990Sstevel@tonic-gate 				void *bus_impldata),
1000Sstevel@tonic-gate 			void *arg, ddi_callback_id_t *cb_id);
1010Sstevel@tonic-gate static int usb_mid_busop_remove_eventcall(dev_info_t *dip,
1020Sstevel@tonic-gate 			ddi_callback_id_t cb_id);
1030Sstevel@tonic-gate static int usb_mid_busop_post_event(dev_info_t *dip,
1040Sstevel@tonic-gate 			dev_info_t *rdip,
1050Sstevel@tonic-gate 			ddi_eventcookie_t cookie,
1060Sstevel@tonic-gate 			void *bus_impldata);
1070Sstevel@tonic-gate static int usb_mid_bus_config(dev_info_t *dip,
1080Sstevel@tonic-gate 			uint_t flag,
1090Sstevel@tonic-gate 			ddi_bus_config_op_t op,
1100Sstevel@tonic-gate 			void *arg,
1110Sstevel@tonic-gate 			dev_info_t **child);
1120Sstevel@tonic-gate static int usb_mid_bus_unconfig(dev_info_t *dip,
1130Sstevel@tonic-gate 			uint_t flag,
1140Sstevel@tonic-gate 			ddi_bus_config_op_t op,
1150Sstevel@tonic-gate 			void *arg);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * autoconfiguration data and routines.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate static int	usb_mid_info(dev_info_t *, ddi_info_cmd_t,
1220Sstevel@tonic-gate 				void *, void **);
1230Sstevel@tonic-gate static int	usb_mid_attach(dev_info_t *, ddi_attach_cmd_t);
1240Sstevel@tonic-gate static int	usb_mid_detach(dev_info_t *, ddi_detach_cmd_t);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /* other routines */
1270Sstevel@tonic-gate static void usb_mid_create_pm_components(dev_info_t *, usb_mid_t *);
1280Sstevel@tonic-gate static int usb_mid_bus_ctl(dev_info_t *, dev_info_t	*,
1290Sstevel@tonic-gate 				ddi_ctl_enum_t, void *, void *);
1300Sstevel@tonic-gate static int usb_mid_power(dev_info_t *, int, int);
1310Sstevel@tonic-gate static int usb_mid_restore_device_state(dev_info_t *, usb_mid_t *);
1320Sstevel@tonic-gate static usb_mid_t  *usb_mid_obtain_state(dev_info_t *);
1333341Sgc161489 static void usb_mid_event_cb(dev_info_t *, ddi_eventcookie_t, void *, void *);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * Busops vector
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate static struct bus_ops usb_mid_busops = {
1390Sstevel@tonic-gate 	BUSO_REV,
1400Sstevel@tonic-gate 	nullbusmap,			/* bus_map */
1410Sstevel@tonic-gate 	NULL,				/* bus_get_intrspec */
1420Sstevel@tonic-gate 	NULL,				/* bus_add_intrspec */
1430Sstevel@tonic-gate 	NULL,				/* bus_remove_intrspec */
1440Sstevel@tonic-gate 	NULL,				/* XXXX bus_map_fault */
1450Sstevel@tonic-gate 	ddi_dma_map,			/* bus_dma_map */
1460Sstevel@tonic-gate 	ddi_dma_allochdl,
1470Sstevel@tonic-gate 	ddi_dma_freehdl,
1480Sstevel@tonic-gate 	ddi_dma_bindhdl,
1490Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1500Sstevel@tonic-gate 	ddi_dma_flush,
1510Sstevel@tonic-gate 	ddi_dma_win,
1520Sstevel@tonic-gate 	ddi_dma_mctl,			/* bus_dma_ctl */
1530Sstevel@tonic-gate 	usb_mid_bus_ctl,		/* bus_ctl */
1540Sstevel@tonic-gate 	ddi_bus_prop_op,		/* bus_prop_op */
1550Sstevel@tonic-gate 	usb_mid_busop_get_eventcookie,
1560Sstevel@tonic-gate 	usb_mid_busop_add_eventcall,
1570Sstevel@tonic-gate 	usb_mid_busop_remove_eventcall,
1580Sstevel@tonic-gate 	usb_mid_busop_post_event,	/* bus_post_event */
1590Sstevel@tonic-gate 	NULL,				/* bus_intr_ctl */
1600Sstevel@tonic-gate 	usb_mid_bus_config,		/* bus_config */
1610Sstevel@tonic-gate 	usb_mid_bus_unconfig,		/* bus_unconfig */
1620Sstevel@tonic-gate 	NULL,				/* bus_fm_init */
1630Sstevel@tonic-gate 	NULL,				/* bus_fm_fini */
1640Sstevel@tonic-gate 	NULL,				/* bus_fm_access_enter */
1650Sstevel@tonic-gate 	NULL,				/* bus_fm_access_exit */
1660Sstevel@tonic-gate 	NULL				/* bus_power */
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static struct dev_ops usb_mid_ops = {
1710Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1720Sstevel@tonic-gate 	0,			/* refcnt  */
1730Sstevel@tonic-gate 	usb_mid_info,		/* info */
1740Sstevel@tonic-gate 	nulldev,		/* identify */
1750Sstevel@tonic-gate 	nulldev,		/* probe */
1760Sstevel@tonic-gate 	usb_mid_attach,		/* attach */
1770Sstevel@tonic-gate 	usb_mid_detach,		/* detach */
1780Sstevel@tonic-gate 	nodev,			/* reset */
1790Sstevel@tonic-gate 	&usb_mid_cb_ops,	/* driver operations */
1800Sstevel@tonic-gate 	&usb_mid_busops,	/* bus operations */
181*7656SSherry.Moore@Sun.COM 	usb_mid_power,		/* power */
182*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1830Sstevel@tonic-gate };
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate static struct modldrv modldrv = {
1860Sstevel@tonic-gate 	&mod_driverops, /* Type of module. This one is a driver */
1877425SGongtian.Zhao@Sun.COM 	"USB Multi Interface Driver", /* Name of the module. */
1880Sstevel@tonic-gate 	&usb_mid_ops,	/* driver ops */
1890Sstevel@tonic-gate };
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static struct modlinkage modlinkage = {
1920Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1930Sstevel@tonic-gate };
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate #define	USB_MID_INITIAL_SOFT_SPACE 4
1960Sstevel@tonic-gate static	void	*usb_mid_statep;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * prototypes
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate static void usb_mid_create_children(usb_mid_t *usb_mid);
2030Sstevel@tonic-gate static int usb_mid_cleanup(dev_info_t *dip, usb_mid_t	*usb_mid);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * event definition
2070Sstevel@tonic-gate  */
2080Sstevel@tonic-gate static ndi_event_definition_t usb_mid_ndi_event_defs[] = {
2090Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
2100Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2110Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
2120Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2130Sstevel@tonic-gate 	{USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
2140Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2150Sstevel@tonic-gate 	{USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
2160Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL}
2170Sstevel@tonic-gate };
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate #define	USB_MID_N_NDI_EVENTS \
2200Sstevel@tonic-gate 	(sizeof (usb_mid_ndi_event_defs) / sizeof (ndi_event_definition_t))
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate static	ndi_event_set_t usb_mid_ndi_events = {
2230Sstevel@tonic-gate 	NDI_EVENTS_REV1, USB_MID_N_NDI_EVENTS, usb_mid_ndi_event_defs};
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * standard driver entry points
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate int
_init(void)2300Sstevel@tonic-gate _init(void)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	int rval;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	rval = ddi_soft_state_init(&usb_mid_statep, sizeof (struct usb_mid),
2350Sstevel@tonic-gate 	    USB_MID_INITIAL_SOFT_SPACE);
2360Sstevel@tonic-gate 	if (rval != 0) {
2370Sstevel@tonic-gate 		return (rval);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if ((rval = mod_install(&modlinkage)) != 0) {
2410Sstevel@tonic-gate 		ddi_soft_state_fini(&usb_mid_statep);
2420Sstevel@tonic-gate 		return (rval);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	return (rval);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate int
_fini(void)2500Sstevel@tonic-gate _fini(void)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	int	rval;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	rval = mod_remove(&modlinkage);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (rval) {
2570Sstevel@tonic-gate 		return (rval);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	ddi_soft_state_fini(&usb_mid_statep);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	return (rval);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2670Sstevel@tonic-gate _info(struct modinfo *modinfop)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*ARGSUSED*/
2740Sstevel@tonic-gate static int
usb_mid_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2750Sstevel@tonic-gate usb_mid_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	usb_mid_t	*usb_mid;
2780Sstevel@tonic-gate 	int		instance =
2796898Sfb209375 	    USB_MID_MINOR_TO_INSTANCE(getminor((dev_t)arg));
2800Sstevel@tonic-gate 	int		error = DDI_FAILURE;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	switch (infocmd) {
2830Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2840Sstevel@tonic-gate 		if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
2850Sstevel@tonic-gate 		    instance)) != NULL) {
2860Sstevel@tonic-gate 			*result = (void *)usb_mid->mi_dip;
2870Sstevel@tonic-gate 			if (*result != NULL) {
2880Sstevel@tonic-gate 				error = DDI_SUCCESS;
2890Sstevel@tonic-gate 			}
2900Sstevel@tonic-gate 		} else {
2910Sstevel@tonic-gate 			*result = NULL;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 		break;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2960Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
2970Sstevel@tonic-gate 		error = DDI_SUCCESS;
2980Sstevel@tonic-gate 		break;
2990Sstevel@tonic-gate 	default:
3000Sstevel@tonic-gate 		break;
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	return (error);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * child  post attach/detach notification
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate static void
usb_mid_post_attach(usb_mid_t * usb_mid,uint8_t ifno,struct attachspec * as)3110Sstevel@tonic-gate usb_mid_post_attach(usb_mid_t *usb_mid, uint8_t ifno, struct attachspec *as)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3140Sstevel@tonic-gate 	    "usb_mid_post_attach: ifno = %d result = %d", ifno, as->result);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/* if child successfully attached, set power */
3170Sstevel@tonic-gate 	if (as->result == DDI_SUCCESS) {
3180Sstevel@tonic-gate 		/*
3190Sstevel@tonic-gate 		 * Check if the child created wants to be power managed.
3200Sstevel@tonic-gate 		 * If yes, the childs power level gets automatically tracked
3210Sstevel@tonic-gate 		 * by DDI_CTLOPS_POWER busctl.
3220Sstevel@tonic-gate 		 * If no, we set power of the new child by default
3230Sstevel@tonic-gate 		 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
3240Sstevel@tonic-gate 		 */
3250Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
3260Sstevel@tonic-gate 		usb_mid->mi_attach_count++;
3270Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static void
usb_mid_post_detach(usb_mid_t * usb_mid,uint8_t ifno,struct detachspec * ds)3330Sstevel@tonic-gate usb_mid_post_detach(usb_mid_t *usb_mid, uint8_t ifno, struct detachspec *ds)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3360Sstevel@tonic-gate 	    "usb_mid_post_detach: ifno = %d result = %d", ifno, ds->result);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/*
3390Sstevel@tonic-gate 	 * if the device is successfully detached,
3400Sstevel@tonic-gate 	 * mark component as idle
3410Sstevel@tonic-gate 	 */
3420Sstevel@tonic-gate 	if (ds->result == DDI_SUCCESS) {
3430Sstevel@tonic-gate 		usba_device_t *usba_device =
3446898Sfb209375 		    usba_get_usba_device(usb_mid->mi_dip);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		/* check for leaks except when where is a ugen open */
3490Sstevel@tonic-gate 		if ((ds->cmd == DDI_DETACH) &&
3500Sstevel@tonic-gate 		    (--usb_mid->mi_attach_count == 0) && usba_device &&
3510Sstevel@tonic-gate 		    (usb_mid->mi_ugen_open_count == 0)) {
3520Sstevel@tonic-gate 			usba_check_for_leaks(usba_device);
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate  * bus ctl support. we handle notifications here and the
3610Sstevel@tonic-gate  * rest goes up to root hub/hcd
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate /*ARGSUSED*/
3640Sstevel@tonic-gate static int
usb_mid_bus_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t op,void * arg,void * result)3650Sstevel@tonic-gate usb_mid_bus_ctl(dev_info_t *dip,
3660Sstevel@tonic-gate 	dev_info_t	*rdip,
3670Sstevel@tonic-gate 	ddi_ctl_enum_t	op,
3680Sstevel@tonic-gate 	void		*arg,
3690Sstevel@tonic-gate 	void		*result)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
3720Sstevel@tonic-gate 	dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
3730Sstevel@tonic-gate 	usb_mid_t  *usb_mid;
3740Sstevel@tonic-gate 	struct attachspec *as;
3750Sstevel@tonic-gate 	struct detachspec *ds;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	usb_mid = usb_mid_obtain_state(dip);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3800Sstevel@tonic-gate 	    "usb_mid_bus_ctl:\n\t"
3810Sstevel@tonic-gate 	    "dip = 0x%p, rdip = 0x%p, op = 0x%x, arg = 0x%p",
3826898Sfb209375 	    (void *)dip, (void *)rdip, op, arg);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	switch (op) {
3850Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
3860Sstevel@tonic-gate 		as = (struct attachspec *)arg;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		switch (as->when) {
3890Sstevel@tonic-gate 		case DDI_PRE :
3900Sstevel@tonic-gate 			/* nothing to do basically */
3910Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3920Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_ATTACH");
3930Sstevel@tonic-gate 			break;
3940Sstevel@tonic-gate 		case DDI_POST :
3950Sstevel@tonic-gate 			usb_mid_post_attach(usb_mid, usba_get_ifno(rdip),
3960Sstevel@tonic-gate 			    (struct attachspec *)arg);
3970Sstevel@tonic-gate 			break;
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		break;
4010Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
4020Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 		switch (ds->when) {
4050Sstevel@tonic-gate 		case DDI_PRE :
4060Sstevel@tonic-gate 			/* nothing to do basically */
4070Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
4080Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_DETACH");
4090Sstevel@tonic-gate 			break;
4100Sstevel@tonic-gate 		case DDI_POST :
4110Sstevel@tonic-gate 			usb_mid_post_detach(usb_mid, usba_get_ifno(rdip),
4120Sstevel@tonic-gate 			    (struct detachspec *)arg);
4130Sstevel@tonic-gate 			break;
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 		break;
4170Sstevel@tonic-gate 	default:
4180Sstevel@tonic-gate 		/* pass to root hub to handle */
4190Sstevel@tonic-gate 		return (usba_bus_ctl(root_hub_dip, rdip, op, arg, result));
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	return (DDI_SUCCESS);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate  * bus enumeration entry points
4280Sstevel@tonic-gate  */
4290Sstevel@tonic-gate static int
usb_mid_bus_config(dev_info_t * dip,uint_t flag,ddi_bus_config_op_t op,void * arg,dev_info_t ** child)4300Sstevel@tonic-gate usb_mid_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
4310Sstevel@tonic-gate     void *arg, dev_info_t **child)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	int		rval, circ;
4340Sstevel@tonic-gate 	usb_mid_t	*usb_mid = usb_mid_obtain_state(dip);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
4370Sstevel@tonic-gate 	    "usb_mid_bus_config: op=%d", op);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (usb_mid_bus_config_debug) {
4400Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/* enumerate each interface below us */
4460Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
4470Sstevel@tonic-gate 	usb_mid_create_children(usb_mid);
4480Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
4510Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	return (rval);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate static int
usb_mid_bus_unconfig(dev_info_t * dip,uint_t flag,ddi_bus_config_op_t op,void * arg)4580Sstevel@tonic-gate usb_mid_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
4590Sstevel@tonic-gate     void *arg)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	dev_info_t	*cdip, *mdip;
4640Sstevel@tonic-gate 	int		interface, circular_count;
4653341Sgc161489 	int		rval = NDI_SUCCESS;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
4680Sstevel@tonic-gate 	    "usb_mid_bus_unconfig: op=%d", op);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (usb_mid_bus_config_debug) {
4710Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * first offline and if offlining successful, then
4760Sstevel@tonic-gate 	 * remove children
4770Sstevel@tonic-gate 	 */
4780Sstevel@tonic-gate 	if (op == BUS_UNCONFIG_ALL) {
4790Sstevel@tonic-gate 		flag &= ~(NDI_DEVI_REMOVE | NDI_UNCONFIG);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	ndi_devi_enter(dip, &circular_count);
4830Sstevel@tonic-gate 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	if (op == BUS_UNCONFIG_ALL && rval == NDI_SUCCESS &&
4860Sstevel@tonic-gate 	    (flag & NDI_AUTODETACH) == 0) {
4870Sstevel@tonic-gate 		flag |= NDI_DEVI_REMOVE;
4880Sstevel@tonic-gate 		rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/* update children's list */
4920Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
4930Sstevel@tonic-gate 	for (interface = 0; usb_mid->mi_children_dips &&
4943341Sgc161489 	    (interface < usb_mid->mi_n_ifs) &&
4953341Sgc161489 	    (usb_mid->mi_children_ifs[interface]); interface++) {
4960Sstevel@tonic-gate 		mdip = usb_mid->mi_children_dips[interface];
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		/* now search if this dip still exists */
4996898Sfb209375 		for (cdip = ddi_get_child(dip); cdip && (cdip != mdip); )
5006898Sfb209375 			cdip = ddi_get_next_sibling(cdip);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		if (cdip != mdip) {
5030Sstevel@tonic-gate 			/* we lost the dip on this interface */
5040Sstevel@tonic-gate 			usb_mid->mi_children_dips[interface] = NULL;
5050Sstevel@tonic-gate 		} else if (cdip) {
5060Sstevel@tonic-gate 			/*
5070Sstevel@tonic-gate 			 * keep in DS_INITALIZED to prevent parent
5080Sstevel@tonic-gate 			 * from detaching
5090Sstevel@tonic-gate 			 */
5100Sstevel@tonic-gate 			(void) ddi_initchild(ddi_get_parent(cdip), cdip);
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	ndi_devi_exit(dip, circular_count);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
5180Sstevel@tonic-gate 	    "usb_mid_bus_config: rval=%d", rval);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	return (rval);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /* power entry point */
5250Sstevel@tonic-gate /* ARGSUSED */
5260Sstevel@tonic-gate static int
usb_mid_power(dev_info_t * dip,int comp,int level)5270Sstevel@tonic-gate usb_mid_power(dev_info_t *dip, int comp, int level)
5280Sstevel@tonic-gate {
5293341Sgc161489 	usb_mid_t		*usb_mid;
5303341Sgc161489 	usb_common_power_t	*midpm;
5313341Sgc161489 	int			rval = DDI_FAILURE;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	usb_mid =  usb_mid_obtain_state(dip);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
5366898Sfb209375 	    "usb_mid_power: Begin: usb_mid = %p, level = %d",
5376898Sfb209375 	    (void *)usb_mid, level);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
5400Sstevel@tonic-gate 	midpm = usb_mid->mi_pm;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/* check if we are transitioning to a legal power level */
5433341Sgc161489 	if (USB_DEV_PWRSTATE_OK(midpm->uc_pwr_states, level)) {
5440Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
5450Sstevel@tonic-gate 		    "usb_mid_power: illegal power level = %d "
5463341Sgc161489 		    "uc_pwr_states = %x", level, midpm->uc_pwr_states);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		return (rval);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5533528Sgc161489 	rval = usba_common_power(dip, &(midpm->uc_current_power),
5543528Sgc161489 	    &(usb_mid->mi_dev_state), level);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
5570Sstevel@tonic-gate 
5583341Sgc161489 	return (rval);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate  * attach/resume entry point
5640Sstevel@tonic-gate  */
5650Sstevel@tonic-gate static int
usb_mid_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)5660Sstevel@tonic-gate usb_mid_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate 	int		instance = ddi_get_instance(dip);
5690Sstevel@tonic-gate 	usb_mid_t	*usb_mid = NULL;
5703341Sgc161489 	uint_t		n_ifs, i;
5710Sstevel@tonic-gate 	size_t		size;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	switch (cmd) {
5740Sstevel@tonic-gate 	case DDI_ATTACH:
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		break;
5770Sstevel@tonic-gate 	case DDI_RESUME:
5780Sstevel@tonic-gate 		usb_mid = (usb_mid_t *)ddi_get_soft_state(usb_mid_statep,
5790Sstevel@tonic-gate 		    instance);
5800Sstevel@tonic-gate 		(void) usb_mid_restore_device_state(dip, usb_mid);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 		if (usb_mid->mi_ugen_hdl) {
5830Sstevel@tonic-gate 			(void) usb_ugen_attach(usb_mid->mi_ugen_hdl,
5846898Sfb209375 			    DDI_RESUME);
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 		return (DDI_SUCCESS);
5880Sstevel@tonic-gate 	default:
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		return (DDI_FAILURE);
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * Attach:
5950Sstevel@tonic-gate 	 *
5960Sstevel@tonic-gate 	 * Allocate soft state and initialize
5970Sstevel@tonic-gate 	 */
5980Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(usb_mid_statep, instance) != DDI_SUCCESS) {
5990Sstevel@tonic-gate 		goto fail;
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	usb_mid = ddi_get_soft_state(usb_mid_statep, instance);
6030Sstevel@tonic-gate 	if (usb_mid == NULL) {
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		goto fail;
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	/* allocate handle for logging of messages */
6090Sstevel@tonic-gate 	usb_mid->mi_log_handle = usb_alloc_log_hdl(dip, "mid",
6106898Sfb209375 	    &usb_mid_errlevel,
6116898Sfb209375 	    &usb_mid_errmask, &usb_mid_instance_debug,
6126898Sfb209375 	    0);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	usb_mid->mi_usba_device = usba_get_usba_device(dip);
6150Sstevel@tonic-gate 	usb_mid->mi_dip	= dip;
6160Sstevel@tonic-gate 	usb_mid->mi_instance = instance;
6170Sstevel@tonic-gate 	usb_mid->mi_n_ifs = usb_mid->mi_usba_device->usb_n_ifs;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/* attach client driver to USBA */
6200Sstevel@tonic-gate 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
6210Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
6220Sstevel@tonic-gate 		    "usb_client_attach failed");
6230Sstevel@tonic-gate 		goto fail;
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 	if (usb_get_dev_data(dip, &usb_mid->mi_dev_data, USB_PARSE_LVL_NONE,
6260Sstevel@tonic-gate 	    0) != USB_SUCCESS) {
6270Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
6280Sstevel@tonic-gate 		    "usb_get_dev_data failed");
6290Sstevel@tonic-gate 		goto fail;
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	mutex_init(&usb_mid->mi_mutex, NULL, MUTEX_DRIVER,
6336898Sfb209375 	    usb_mid->mi_dev_data->dev_iblock_cookie);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	usb_free_dev_data(dip, usb_mid->mi_dev_data);
6360Sstevel@tonic-gate 	usb_mid->mi_dev_data = NULL;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_LOCK_INIT;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "usb_mid", S_IFCHR,
6410Sstevel@tonic-gate 	    instance << USB_MID_MINOR_INSTANCE_SHIFT,
6420Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
643978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
6440Sstevel@tonic-gate 		    "cannot create devctl minor node");
6450Sstevel@tonic-gate 		goto fail;
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_MINOR_NODE_CREATED;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	/*
6510Sstevel@tonic-gate 	 * allocate array for keeping track of child dips
6520Sstevel@tonic-gate 	 */
6530Sstevel@tonic-gate 	n_ifs = usb_mid->mi_n_ifs;
6540Sstevel@tonic-gate 	usb_mid->mi_cd_list_length = size = (sizeof (dev_info_t *)) * n_ifs;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	usb_mid->mi_children_dips = kmem_zalloc(size, KM_SLEEP);
6570Sstevel@tonic-gate 	usb_mid->mi_child_events = kmem_zalloc(sizeof (uint8_t) * n_ifs,
6586898Sfb209375 	    KM_SLEEP);
6593341Sgc161489 	usb_mid->mi_children_ifs = kmem_zalloc(sizeof (uint_t) * n_ifs,
6606898Sfb209375 	    KM_SLEEP);
6613341Sgc161489 	for (i = 0; i < n_ifs; i++) {
6623341Sgc161489 		usb_mid->mi_children_ifs[i] = 1;
6633341Sgc161489 	}
6643341Sgc161489 
6650Sstevel@tonic-gate 	/*
6660Sstevel@tonic-gate 	 * Event handling: definition and registration
6670Sstevel@tonic-gate 	 * get event handle for events that we have defined
6680Sstevel@tonic-gate 	 */
6690Sstevel@tonic-gate 	(void) ndi_event_alloc_hdl(dip, 0, &usb_mid->mi_ndi_event_hdl,
6706898Sfb209375 	    NDI_SLEEP);
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	/* bind event set to the handle */
6730Sstevel@tonic-gate 	if (ndi_event_bind_set(usb_mid->mi_ndi_event_hdl, &usb_mid_ndi_events,
6740Sstevel@tonic-gate 	    NDI_SLEEP)) {
6750Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
6760Sstevel@tonic-gate 		    "usb_mid_attach: binding event set failed");
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		goto fail;
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	usb_mid->mi_dev_state = USB_DEV_ONLINE;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/*
6840Sstevel@tonic-gate 	 * now create components to power manage this device
6850Sstevel@tonic-gate 	 * before attaching children
6860Sstevel@tonic-gate 	 */
6870Sstevel@tonic-gate 	usb_mid_create_pm_components(dip, usb_mid);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/* event registration for events from our parent */
6903341Sgc161489 	usba_common_register_events(usb_mid->mi_dip, 1, usb_mid_event_cb);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_EVENTS_REGISTERED;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	ddi_report_dev(dip);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	return (DDI_SUCCESS);
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate fail:
699978Sfrits 	USB_DPRINTF_L2(DPRINT_MASK_ATTA, NULL, "usb_mid%d cannot attach",
7000Sstevel@tonic-gate 	    instance);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (usb_mid) {
7030Sstevel@tonic-gate 		(void) usb_mid_cleanup(dip, usb_mid);
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	return (DDI_FAILURE);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate /* detach or suspend this instance */
7110Sstevel@tonic-gate static int
usb_mid_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)7120Sstevel@tonic-gate usb_mid_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	usb_mid_t	*usb_mid = usb_mid_obtain_state(dip);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7170Sstevel@tonic-gate 	    "usb_mid_detach: cmd = 0x%x", cmd);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	switch (cmd) {
7200Sstevel@tonic-gate 	case DDI_DETACH:
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		return (usb_mid_cleanup(dip, usb_mid));
7230Sstevel@tonic-gate 	case DDI_SUSPEND:
7240Sstevel@tonic-gate 		/* nothing to do */
7250Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
7260Sstevel@tonic-gate 		usb_mid->mi_dev_state = USB_DEV_SUSPENDED;
7270Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		if (usb_mid->mi_ugen_hdl) {
7300Sstevel@tonic-gate 			int rval = usb_ugen_detach(usb_mid->mi_ugen_hdl,
7316898Sfb209375 			    DDI_SUSPEND);
7320Sstevel@tonic-gate 			return (rval == USB_SUCCESS ? DDI_SUCCESS :
7336898Sfb209375 			    DDI_FAILURE);
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		return (DDI_SUCCESS);
7370Sstevel@tonic-gate 	default:
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 		return (DDI_FAILURE);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	_NOTE(NOT_REACHED)
7430Sstevel@tonic-gate 	/* NOTREACHED */
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate /*
7470Sstevel@tonic-gate  * usb_mid_cleanup:
7480Sstevel@tonic-gate  *	cleanup usb_mid and deallocate. this function is called for
7490Sstevel@tonic-gate  *	handling attach failures and detaching including dynamic
7500Sstevel@tonic-gate  *	reconfiguration
7510Sstevel@tonic-gate  */
7520Sstevel@tonic-gate /*ARGSUSED*/
7530Sstevel@tonic-gate static int
usb_mid_cleanup(dev_info_t * dip,usb_mid_t * usb_mid)7540Sstevel@tonic-gate usb_mid_cleanup(dev_info_t *dip, usb_mid_t *usb_mid)
7550Sstevel@tonic-gate {
7563341Sgc161489 	usb_common_power_t	*midpm;
7570Sstevel@tonic-gate 	int		rval;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7600Sstevel@tonic-gate 	    "usb_mid_cleanup:");
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if ((usb_mid->mi_init_state & USB_MID_LOCK_INIT) == 0) {
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		goto done;
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	/*
7680Sstevel@tonic-gate 	 * deallocate events, if events are still registered
7690Sstevel@tonic-gate 	 * (ie. children still attached) then we have to fail the detach
7700Sstevel@tonic-gate 	 */
7710Sstevel@tonic-gate 	if (usb_mid->mi_ndi_event_hdl &&
7720Sstevel@tonic-gate 	    (ndi_event_free_hdl(usb_mid->mi_ndi_event_hdl) != NDI_SUCCESS)) {
7730Sstevel@tonic-gate 
774978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7750Sstevel@tonic-gate 		    "usb_mid_cleanup: ndi_event_free_hdl failed");
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 		return (DDI_FAILURE);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/*
7810Sstevel@tonic-gate 	 * Disable the event callbacks, after this point, event
7820Sstevel@tonic-gate 	 * callbacks will never get called. Note we shouldn't hold
7830Sstevel@tonic-gate 	 * mutex while unregistering events because there may be a
7840Sstevel@tonic-gate 	 * competing event callback thread. Event callbacks are done
7850Sstevel@tonic-gate 	 * with ndi mutex held and this can cause a potential deadlock.
7860Sstevel@tonic-gate 	 * Note that cleanup can't fail after deregistration of events.
7870Sstevel@tonic-gate 	 */
7880Sstevel@tonic-gate 	if (usb_mid->mi_init_state & USB_MID_EVENTS_REGISTERED) {
7893341Sgc161489 		usba_common_unregister_events(usb_mid->mi_dip, 1);
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	midpm = usb_mid->mi_pm;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	if ((midpm) && (usb_mid->mi_dev_state != USB_DEV_DISCONNECTED)) {
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 		(void) pm_busy_component(dip, 0);
8013341Sgc161489 		if (midpm->uc_wakeup_enabled) {
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 			/* First bring the device to full power */
8040Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 			rval = usb_handle_remote_wakeup(dip,
8070Sstevel@tonic-gate 			    USB_REMOTE_WAKEUP_DISABLE);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 			if (rval != DDI_SUCCESS) {
8100Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_EVENTS,
8110Sstevel@tonic-gate 				    usb_mid->mi_log_handle,
8120Sstevel@tonic-gate 				    "usb_cleanup: disable remote "
8130Sstevel@tonic-gate 				    "wakeup failed, rval=%d", rval);
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 		(void) pm_lower_power(usb_mid->mi_dip, 0, USB_DEV_OS_PWR_OFF);
8180Sstevel@tonic-gate 		(void) pm_idle_component(dip, 0);
8190Sstevel@tonic-gate 	} else {
8200Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	if (midpm) {
8243341Sgc161489 		kmem_free(midpm, sizeof (usb_common_power_t));
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/* free children list */
8280Sstevel@tonic-gate 	if (usb_mid->mi_children_dips) {
8290Sstevel@tonic-gate 		kmem_free(usb_mid->mi_children_dips,
8306898Sfb209375 		    usb_mid->mi_cd_list_length);
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	if (usb_mid->mi_child_events) {
8340Sstevel@tonic-gate 		kmem_free(usb_mid->mi_child_events, sizeof (uint8_t) *
8350Sstevel@tonic-gate 		    usb_mid->mi_n_ifs);
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 
8383341Sgc161489 	if (usb_mid->mi_children_ifs) {
8393341Sgc161489 		kmem_free(usb_mid->mi_children_ifs, sizeof (uint_t) *
8403341Sgc161489 		    usb_mid->mi_n_ifs);
8413341Sgc161489 	}
8423341Sgc161489 
8430Sstevel@tonic-gate 	if (usb_mid->mi_init_state & USB_MID_MINOR_NODE_CREATED) {
8440Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	mutex_destroy(&usb_mid->mi_mutex);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate done:
8500Sstevel@tonic-gate 	usb_client_detach(dip, usb_mid->mi_dev_data);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	if (usb_mid->mi_ugen_hdl) {
8530Sstevel@tonic-gate 		(void) usb_ugen_detach(usb_mid->mi_ugen_hdl, DDI_DETACH);
8540Sstevel@tonic-gate 		usb_ugen_release_hdl(usb_mid->mi_ugen_hdl);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	usb_free_log_hdl(usb_mid->mi_log_handle);
8580Sstevel@tonic-gate 	ddi_soft_state_free(usb_mid_statep, ddi_get_instance(dip));
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	return (DDI_SUCCESS);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate static void
usb_mid_ugen_attach(usb_mid_t * usb_mid,boolean_t remove_children)8670Sstevel@tonic-gate usb_mid_ugen_attach(usb_mid_t *usb_mid, boolean_t remove_children)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	_NOTE(NO_COMPETING_THREADS_NOW);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if (usb_mid->mi_ugen_hdl == NULL) {
8720Sstevel@tonic-gate 		usb_ugen_info_t usb_ugen_info;
8730Sstevel@tonic-gate 		int		rval;
8740Sstevel@tonic-gate 		usb_ugen_hdl_t	hdl;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
8776898Sfb209375 		    "usb_mid_ugen_attach: get handle");
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 		bzero(&usb_ugen_info, sizeof (usb_ugen_info));
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_flags = (remove_children ?
8826898Sfb209375 		    USB_UGEN_REMOVE_CHILDREN : 0);
8830Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_minor_node_ugen_bits_mask =
8846898Sfb209375 		    (dev_t)USB_MID_MINOR_UGEN_BITS_MASK;
8850Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_minor_node_instance_mask =
8866898Sfb209375 		    (dev_t)~USB_MID_MINOR_UGEN_BITS_MASK;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
8890Sstevel@tonic-gate 		hdl = usb_ugen_get_hdl(usb_mid->mi_dip,
8906898Sfb209375 		    &usb_ugen_info);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 		if ((rval = usb_ugen_attach(hdl, DDI_ATTACH)) != USB_SUCCESS) {
8930Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
8940Sstevel@tonic-gate 			    "failed to create ugen support (%d)", rval);
8950Sstevel@tonic-gate 			usb_ugen_release_hdl(hdl);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
8980Sstevel@tonic-gate 		} else {
8990Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
9000Sstevel@tonic-gate 			usb_mid->mi_ugen_hdl = hdl;
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9042840Scarlsonj #ifndef lint
9050Sstevel@tonic-gate 	_NOTE(COMPETING_THREADS_NOW);
9062840Scarlsonj #endif
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate /*
9110Sstevel@tonic-gate  * usb_mid_create_children:
9120Sstevel@tonic-gate  */
9130Sstevel@tonic-gate static void
usb_mid_create_children(usb_mid_t * usb_mid)9140Sstevel@tonic-gate usb_mid_create_children(usb_mid_t *usb_mid)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	usba_device_t		*usba_device;
9173341Sgc161489 	uint_t			n_ifs, if_count;
9183341Sgc161489 	uint_t			i, j;
9193341Sgc161489 	dev_info_t		*cdip, *ia_dip;
9200Sstevel@tonic-gate 	uint_t			ugen_bound = 0;
9210Sstevel@tonic-gate 	uint_t			bound_children = 0;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	usba_device = usba_get_usba_device(usb_mid->mi_dip);
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
9260Sstevel@tonic-gate 	    "usb_mid_attach_child_drivers: port = %d, address = %d",
9270Sstevel@tonic-gate 	    usba_device->usb_port, usba_device->usb_addr);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if (usb_mid->mi_removed_children) {
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 			return;
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	n_ifs = usb_mid->mi_n_ifs;
9353341Sgc161489 	if_count = 1;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
9380Sstevel@tonic-gate 	    "usb_mid_create_children: #interfaces = %d", n_ifs);
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	/*
9410Sstevel@tonic-gate 	 * create all children if not already present
9420Sstevel@tonic-gate 	 */
9433341Sgc161489 	for (i = 0; i < n_ifs; i += if_count) {
9443341Sgc161489 
9453341Sgc161489 		/* ignore since this if is included by an ia */
9463341Sgc161489 		if (usb_mid->mi_children_ifs[i] == 0) {
9473341Sgc161489 
9483341Sgc161489 			continue;
9493341Sgc161489 		}
9503341Sgc161489 
9510Sstevel@tonic-gate 		if (usb_mid->mi_children_dips[i] != NULL) {
952880Sfrits 			if (i_ddi_node_state(
9536898Sfb209375 			    usb_mid->mi_children_dips[i]) >=
9546898Sfb209375 			    DS_BOUND) {
955880Sfrits 					bound_children++;
956880Sfrits 			}
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 			continue;
9590Sstevel@tonic-gate 		}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
9623341Sgc161489 		ia_dip = usba_ready_interface_association_node(usb_mid->mi_dip,
9633341Sgc161489 		    i, &if_count);
9643341Sgc161489 
9653341Sgc161489 		if (ia_dip != NULL) {
9663341Sgc161489 			if (usba_bind_driver(ia_dip) == USB_SUCCESS) {
9673341Sgc161489 				bound_children++;
9683341Sgc161489 				if (strcmp(ddi_driver_name(ia_dip),
9693341Sgc161489 				    "ugen") == 0) {
9703341Sgc161489 					ugen_bound++;
9713341Sgc161489 				}
9723341Sgc161489 			}
9733341Sgc161489 
9743341Sgc161489 			/*
9753341Sgc161489 			 * IA node owns if_count interfaces.
9763341Sgc161489 			 * The rest interfaces own none.
9773341Sgc161489 			 */
9783341Sgc161489 			mutex_enter(&usb_mid->mi_mutex);
9793341Sgc161489 			usb_mid->mi_children_dips[i] = ia_dip;
9803341Sgc161489 			usb_mid->mi_children_ifs[i] = if_count;
9813341Sgc161489 			for (j = i + 1; j < i + if_count; j++) {
9823341Sgc161489 				usb_mid->mi_children_ifs[j] = 0;
9833341Sgc161489 			}
9843341Sgc161489 
9853341Sgc161489 			continue;
9863341Sgc161489 		}
9873341Sgc161489 
9880Sstevel@tonic-gate 		cdip = usba_ready_interface_node(usb_mid->mi_dip, i);
9893341Sgc161489 
9900Sstevel@tonic-gate 		if (cdip != NULL) {
9913341Sgc161489 			if (usba_bind_driver(cdip) ==
9920Sstevel@tonic-gate 			    USB_SUCCESS) {
9930Sstevel@tonic-gate 				bound_children++;
9940Sstevel@tonic-gate 				if (strcmp(ddi_driver_name(cdip),
9950Sstevel@tonic-gate 				    "ugen") == 0) {
9960Sstevel@tonic-gate 					ugen_bound++;
9970Sstevel@tonic-gate 				}
9980Sstevel@tonic-gate 			}
9990Sstevel@tonic-gate 
10003341Sgc161489 			/*
10013341Sgc161489 			 * interface node owns 1 interface always.
10023341Sgc161489 			 */
10033341Sgc161489 			mutex_enter(&usb_mid->mi_mutex);
10040Sstevel@tonic-gate 			usb_mid->mi_children_dips[i] = cdip;
10053341Sgc161489 			usb_mid->mi_children_ifs[i] = 1;
10063341Sgc161489 			mutex_exit(&usb_mid->mi_mutex);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 		}
10093341Sgc161489 
10103341Sgc161489 		mutex_enter(&usb_mid->mi_mutex);
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	usb_mid->mi_removed_children = (bound_children ? B_FALSE : B_TRUE);
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	/*
10160Sstevel@tonic-gate 	 * if there are no ugen interface children, create ugen support at
10170Sstevel@tonic-gate 	 * device level, use a separate thread because we may be at interrupt
10180Sstevel@tonic-gate 	 * level
10190Sstevel@tonic-gate 	 */
10200Sstevel@tonic-gate 	if ((ugen_bound == 0) && (usb_mid->mi_ugen_hdl == NULL)) {
10210Sstevel@tonic-gate 		/*
10220Sstevel@tonic-gate 		 * we only need to remove the children if there are
10230Sstevel@tonic-gate 		 * multiple configurations which would fail if there
10240Sstevel@tonic-gate 		 * are child interfaces
10250Sstevel@tonic-gate 		 */
10260Sstevel@tonic-gate 		if ((usb_mid->mi_removed_children == B_FALSE) &&
10270Sstevel@tonic-gate 		    (usba_device->usb_n_cfgs > 1)) {
10280Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_ATTA,
10290Sstevel@tonic-gate 			    usb_mid->mi_log_handle,
10300Sstevel@tonic-gate 			    "can't support ugen for multiple "
10310Sstevel@tonic-gate 			    "configurations devices that have attached "
10320Sstevel@tonic-gate 			    "child interface drivers");
10330Sstevel@tonic-gate 		} else {
10340Sstevel@tonic-gate 			usb_mid_ugen_attach(usb_mid,
10350Sstevel@tonic-gate 			    usb_mid->mi_removed_children);
10360Sstevel@tonic-gate 		}
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate /*
10420Sstevel@tonic-gate  * event support
10430Sstevel@tonic-gate  */
10440Sstevel@tonic-gate static int
usb_mid_busop_get_eventcookie(dev_info_t * dip,dev_info_t * rdip,char * eventname,ddi_eventcookie_t * cookie)10450Sstevel@tonic-gate usb_mid_busop_get_eventcookie(dev_info_t *dip,
10460Sstevel@tonic-gate 	dev_info_t *rdip, char *eventname, ddi_eventcookie_t *cookie)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
10510Sstevel@tonic-gate 	    "usb_mid_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
10520Sstevel@tonic-gate 	    "event=%s", (void *)dip, (void *)rdip, eventname);
10530Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
10540Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d)",
10550Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
10560Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	/* return event cookie, iblock cookie, and level */
10590Sstevel@tonic-gate 	return (ndi_event_retrieve_cookie(usb_mid->mi_ndi_event_hdl,
10606898Sfb209375 	    rdip, eventname, cookie, NDI_EVENT_NOPASS));
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate static int
usb_mid_busop_add_eventcall(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void (* callback)(dev_info_t * dip,ddi_eventcookie_t cookie,void * arg,void * bus_impldata),void * arg,ddi_callback_id_t * cb_id)10650Sstevel@tonic-gate usb_mid_busop_add_eventcall(dev_info_t *dip,
10660Sstevel@tonic-gate 	dev_info_t *rdip,
10670Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
10680Sstevel@tonic-gate 	void (*callback)(dev_info_t *dip,
10690Sstevel@tonic-gate 	    ddi_eventcookie_t cookie, void *arg,
10700Sstevel@tonic-gate 	    void *bus_impldata),
10710Sstevel@tonic-gate 	void *arg, ddi_callback_id_t *cb_id)
10720Sstevel@tonic-gate {
10730Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
10740Sstevel@tonic-gate 	int	ifno = usba_get_ifno(rdip);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
10770Sstevel@tonic-gate 	    "usb_mid_busop_add_eventcall: dip=0x%p, rdip=0x%p "
10780Sstevel@tonic-gate 	    "cookie=0x%p, cb=0x%p, arg=0x%p",
10790Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
10800Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
10810Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
10820Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
10830Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
10840Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl, cookie));
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/* Set flag on children registering events */
10870Sstevel@tonic-gate 	switch (ndi_event_cookie_to_tag(usb_mid->mi_ndi_event_hdl, cookie)) {
10880Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
10890Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
10900Sstevel@tonic-gate 		usb_mid->mi_child_events[ifno] |=
10910Sstevel@tonic-gate 		    USB_MID_CHILD_EVENT_DISCONNECT;
10920Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 		break;
10950Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
10960Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
10970Sstevel@tonic-gate 		usb_mid->mi_child_events[ifno] |=
10980Sstevel@tonic-gate 		    USB_MID_CHILD_EVENT_PRESUSPEND;
10990Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 		break;
11020Sstevel@tonic-gate 	default:
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 		break;
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 	/* add callback (perform registration) */
11070Sstevel@tonic-gate 	return (ndi_event_add_callback(usb_mid->mi_ndi_event_hdl,
11086898Sfb209375 	    rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate static int
usb_mid_busop_remove_eventcall(dev_info_t * dip,ddi_callback_id_t cb_id)11130Sstevel@tonic-gate usb_mid_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
11140Sstevel@tonic-gate {
11150Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
11160Sstevel@tonic-gate 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	ASSERT(cb);
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11210Sstevel@tonic-gate 	    "usb_mid_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
11226898Sfb209375 	    "cookie=0x%p", (void *)dip, (void *)cb->ndi_evtcb_dip,
11236898Sfb209375 	    (void *)cb->ndi_evtcb_cookie);
11240Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11250Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
11260Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
11270Sstevel@tonic-gate 	    ddi_driver_name(cb->ndi_evtcb_dip),
11280Sstevel@tonic-gate 	    ddi_get_instance(cb->ndi_evtcb_dip),
11290Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl,
11300Sstevel@tonic-gate 	    cb->ndi_evtcb_cookie));
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	/* remove event registration from our event set */
11330Sstevel@tonic-gate 	return (ndi_event_remove_callback(usb_mid->mi_ndi_event_hdl, cb_id));
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate static int
usb_mid_busop_post_event(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void * bus_impldata)11380Sstevel@tonic-gate usb_mid_busop_post_event(dev_info_t *dip,
11390Sstevel@tonic-gate 	dev_info_t *rdip,
11400Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
11410Sstevel@tonic-gate 	void *bus_impldata)
11420Sstevel@tonic-gate {
11430Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11460Sstevel@tonic-gate 	    "usb_mid_busop_post_event: dip=0x%p, rdip=0x%p "
11470Sstevel@tonic-gate 	    "cookie=0x%p, impl=0x%p",
11480Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, bus_impldata);
11490Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11500Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
11510Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
11520Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
11530Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl, cookie));
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	/* post event to all children registered for this event */
11560Sstevel@tonic-gate 	return (ndi_event_run_callbacks(usb_mid->mi_ndi_event_hdl, rdip,
11576898Sfb209375 	    cookie, bus_impldata));
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate /*
11620Sstevel@tonic-gate  * usb_mid_restore_device_state
11630Sstevel@tonic-gate  *	set the original configuration of the device
11640Sstevel@tonic-gate  */
11650Sstevel@tonic-gate static int
usb_mid_restore_device_state(dev_info_t * dip,usb_mid_t * usb_mid)11660Sstevel@tonic-gate usb_mid_restore_device_state(dev_info_t *dip, usb_mid_t *usb_mid)
11670Sstevel@tonic-gate {
11683341Sgc161489 	usb_common_power_t		*midpm;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11716898Sfb209375 	    "usb_mid_restore_device_state: usb_mid = %p", (void *)usb_mid);
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
11740Sstevel@tonic-gate 	midpm = usb_mid->mi_pm;
11750Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	/* First bring the device to full power */
11780Sstevel@tonic-gate 	(void) pm_busy_component(dip, 0);
11790Sstevel@tonic-gate 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	if (usb_check_same_device(dip, usb_mid->mi_log_handle, USB_LOG_L0,
11820Sstevel@tonic-gate 	    DPRINT_MASK_EVENTS, USB_CHK_VIDPID, NULL) != USB_SUCCESS) {
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 		/* change the device state from suspended to disconnected */
11850Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
11860Sstevel@tonic-gate 		usb_mid->mi_dev_state = USB_DEV_DISCONNECTED;
11870Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
11880Sstevel@tonic-gate 		(void) pm_idle_component(dip, 0);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		return (USB_FAILURE);
11910Sstevel@tonic-gate 	}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	/*
11940Sstevel@tonic-gate 	 * if the device had remote wakeup earlier,
11950Sstevel@tonic-gate 	 * enable it again
11960Sstevel@tonic-gate 	 */
11973341Sgc161489 	if (midpm->uc_wakeup_enabled) {
11980Sstevel@tonic-gate 		(void) usb_handle_remote_wakeup(usb_mid->mi_dip,
11990Sstevel@tonic-gate 		    USB_REMOTE_WAKEUP_ENABLE);
12000Sstevel@tonic-gate 	}
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
12030Sstevel@tonic-gate 	usb_mid->mi_dev_state = USB_DEV_ONLINE;
12040Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	(void) pm_idle_component(dip, 0);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	return (USB_SUCCESS);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate /*
12130Sstevel@tonic-gate  * usb_mid_event_cb()
12140Sstevel@tonic-gate  *	handle disconnect and connect events
12150Sstevel@tonic-gate  */
12160Sstevel@tonic-gate static void
usb_mid_event_cb(dev_info_t * dip,ddi_eventcookie_t cookie,void * arg,void * bus_impldata)12170Sstevel@tonic-gate usb_mid_event_cb(dev_info_t *dip, ddi_eventcookie_t cookie,
12180Sstevel@tonic-gate 	void *arg, void *bus_impldata)
12190Sstevel@tonic-gate {
12200Sstevel@tonic-gate 	int		i, tag;
12210Sstevel@tonic-gate 	usb_mid_t	*usb_mid = usb_mid_obtain_state(dip);
12220Sstevel@tonic-gate 	dev_info_t	*child_dip;
12230Sstevel@tonic-gate 	ddi_eventcookie_t rm_cookie, ins_cookie, suspend_cookie, resume_cookie;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
12260Sstevel@tonic-gate 	    "usb_mid_event_cb: dip=0x%p, cookie=0x%p, "
12270Sstevel@tonic-gate 	    "arg=0x%p, impl=0x%p",
12280Sstevel@tonic-gate 	    (void *)dip, (void *)cookie, arg, bus_impldata);
12290Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
12300Sstevel@tonic-gate 	    "(dip=%s%d event=%s)",
12310Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
12320Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl, cookie));
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	tag = NDI_EVENT_TAG(cookie);
12350Sstevel@tonic-gate 	rm_cookie = ndi_event_tag_to_cookie(
12360Sstevel@tonic-gate 	    usb_mid->mi_ndi_event_hdl, USBA_EVENT_TAG_HOT_REMOVAL);
12370Sstevel@tonic-gate 	suspend_cookie = ndi_event_tag_to_cookie(
12380Sstevel@tonic-gate 	    usb_mid->mi_ndi_event_hdl, USBA_EVENT_TAG_PRE_SUSPEND);
12390Sstevel@tonic-gate 	ins_cookie = ndi_event_tag_to_cookie(
12400Sstevel@tonic-gate 	    usb_mid->mi_ndi_event_hdl, USBA_EVENT_TAG_HOT_INSERTION);
12410Sstevel@tonic-gate 	resume_cookie = ndi_event_tag_to_cookie(
12420Sstevel@tonic-gate 	    usb_mid->mi_ndi_event_hdl, USBA_EVENT_TAG_POST_RESUME);
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
12450Sstevel@tonic-gate 	switch (tag) {
12460Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
12470Sstevel@tonic-gate 		if (usb_mid->mi_dev_state == USB_DEV_DISCONNECTED) {
12480Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_EVENTS,
12490Sstevel@tonic-gate 			    usb_mid->mi_log_handle,
12500Sstevel@tonic-gate 			    "usb_mid_event_cb: Device already disconnected");
12510Sstevel@tonic-gate 		} else {
12520Sstevel@tonic-gate 			/* we are disconnected so set our state now */
12530Sstevel@tonic-gate 			usb_mid->mi_dev_state = USB_DEV_DISCONNECTED;
12540Sstevel@tonic-gate 			for (i = 0; i < usb_mid->mi_n_ifs; i++) {
12550Sstevel@tonic-gate 				usb_mid->mi_child_events[i] &= ~
12560Sstevel@tonic-gate 				    USB_MID_CHILD_EVENT_DISCONNECT;
12570Sstevel@tonic-gate 			}
12580Sstevel@tonic-gate 			mutex_exit(&usb_mid->mi_mutex);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 			/* pass disconnect event to all the children */
12610Sstevel@tonic-gate 			(void) ndi_event_run_callbacks(
12620Sstevel@tonic-gate 			    usb_mid->mi_ndi_event_hdl, NULL,
12630Sstevel@tonic-gate 			    rm_cookie, bus_impldata);
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 			if (usb_mid->mi_ugen_hdl) {
12660Sstevel@tonic-gate 				(void) usb_ugen_disconnect_ev_cb(
12676898Sfb209375 				    usb_mid->mi_ugen_hdl);
12680Sstevel@tonic-gate 			}
12690Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
12700Sstevel@tonic-gate 		}
12710Sstevel@tonic-gate 		break;
12720Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
12730Sstevel@tonic-gate 		/* set our state *after* suspending children */
12740Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		/* pass pre_suspend event to all the children */
12770Sstevel@tonic-gate 		(void) ndi_event_run_callbacks(usb_mid->mi_ndi_event_hdl,
12780Sstevel@tonic-gate 		    NULL, suspend_cookie, bus_impldata);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
12810Sstevel@tonic-gate 		for (i = 0; i < usb_mid->mi_n_ifs; i++) {
12820Sstevel@tonic-gate 			usb_mid->mi_child_events[i] &= ~
12830Sstevel@tonic-gate 			    USB_MID_CHILD_EVENT_PRESUSPEND;
12840Sstevel@tonic-gate 		}
12850Sstevel@tonic-gate 		break;
12860Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_INSERTION:
12870Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
12880Sstevel@tonic-gate 		if (usb_mid_restore_device_state(dip, usb_mid) == USB_SUCCESS) {
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 			/*
12910Sstevel@tonic-gate 			 * Check to see if this child has missed the disconnect
12920Sstevel@tonic-gate 			 * event before it registered for event cb
12930Sstevel@tonic-gate 			 */
12940Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
12950Sstevel@tonic-gate 			for (i = 0; i < usb_mid->mi_n_ifs; i++) {
12963341Sgc161489 				if ((usb_mid->mi_child_events[i] &
12973341Sgc161489 				    USB_MID_CHILD_EVENT_DISCONNECT) &&
12983341Sgc161489 				    usb_mid->mi_children_ifs[i]) {
12990Sstevel@tonic-gate 					usb_mid->mi_child_events[i] &=
13000Sstevel@tonic-gate 					    ~USB_MID_CHILD_EVENT_DISCONNECT;
13010Sstevel@tonic-gate 					child_dip =
13020Sstevel@tonic-gate 					    usb_mid->mi_children_dips[i];
13030Sstevel@tonic-gate 					mutex_exit(&usb_mid->mi_mutex);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 					/* post the missed disconnect */
13060Sstevel@tonic-gate 					(void) ndi_event_do_callback(
13070Sstevel@tonic-gate 					    usb_mid->mi_ndi_event_hdl,
13080Sstevel@tonic-gate 					    child_dip,
13090Sstevel@tonic-gate 					    rm_cookie,
13100Sstevel@tonic-gate 					    bus_impldata);
13110Sstevel@tonic-gate 					mutex_enter(&usb_mid->mi_mutex);
13120Sstevel@tonic-gate 				}
13130Sstevel@tonic-gate 			}
13140Sstevel@tonic-gate 			mutex_exit(&usb_mid->mi_mutex);
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 			/* pass reconnect event to all the children */
13170Sstevel@tonic-gate 			(void) ndi_event_run_callbacks(
13180Sstevel@tonic-gate 			    usb_mid->mi_ndi_event_hdl, NULL,
13190Sstevel@tonic-gate 			    ins_cookie, bus_impldata);
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 			if (usb_mid->mi_ugen_hdl) {
13220Sstevel@tonic-gate 				(void) usb_ugen_reconnect_ev_cb(
13236898Sfb209375 				    usb_mid->mi_ugen_hdl);
13240Sstevel@tonic-gate 			}
13250Sstevel@tonic-gate 		}
13260Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
13270Sstevel@tonic-gate 		break;
13280Sstevel@tonic-gate 	case USBA_EVENT_TAG_POST_RESUME:
13290Sstevel@tonic-gate 		/*
13300Sstevel@tonic-gate 		 * Check to see if this child has missed the pre-suspend
13310Sstevel@tonic-gate 		 * event before it registered for event cb
13320Sstevel@tonic-gate 		 */
13330Sstevel@tonic-gate 		for (i = 0; i < usb_mid->mi_n_ifs; i++) {
13343341Sgc161489 			if ((usb_mid->mi_child_events[i] &
13353341Sgc161489 			    USB_MID_CHILD_EVENT_PRESUSPEND) &&
13363341Sgc161489 			    usb_mid->mi_children_ifs[i]) {
13370Sstevel@tonic-gate 				usb_mid->mi_child_events[i] &=
13380Sstevel@tonic-gate 				    ~USB_MID_CHILD_EVENT_PRESUSPEND;
13390Sstevel@tonic-gate 				child_dip = usb_mid->mi_children_dips[i];
13400Sstevel@tonic-gate 				mutex_exit(&usb_mid->mi_mutex);
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 				/* post the missed pre-suspend event */
13430Sstevel@tonic-gate 				(void) ndi_event_do_callback(
13440Sstevel@tonic-gate 				    usb_mid->mi_ndi_event_hdl,
13450Sstevel@tonic-gate 				    child_dip, suspend_cookie,
13460Sstevel@tonic-gate 				    bus_impldata);
13470Sstevel@tonic-gate 				mutex_enter(&usb_mid->mi_mutex);
13480Sstevel@tonic-gate 			}
13490Sstevel@tonic-gate 		}
13500Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 		/* pass post_resume event to all the children */
13530Sstevel@tonic-gate 		(void) ndi_event_run_callbacks(usb_mid->mi_ndi_event_hdl,
13540Sstevel@tonic-gate 		    NULL, resume_cookie, bus_impldata);
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
13570Sstevel@tonic-gate 		break;
13580Sstevel@tonic-gate 	}
13590Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate /*
13650Sstevel@tonic-gate  * create the pm components required for power management
13660Sstevel@tonic-gate  */
13670Sstevel@tonic-gate static void
usb_mid_create_pm_components(dev_info_t * dip,usb_mid_t * usb_mid)13680Sstevel@tonic-gate usb_mid_create_pm_components(dev_info_t *dip, usb_mid_t *usb_mid)
13690Sstevel@tonic-gate {
13703341Sgc161489 	usb_common_power_t	*midpm;
13710Sstevel@tonic-gate 	uint_t		pwr_states;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
13740Sstevel@tonic-gate 	    "usb_mid_create_pm_components: Begin");
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	/* Allocate the PM state structure */
13773341Sgc161489 	midpm = kmem_zalloc(sizeof (usb_common_power_t), KM_SLEEP);
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
13800Sstevel@tonic-gate 	usb_mid->mi_pm = midpm;
13813341Sgc161489 	midpm->uc_usb_statep = usb_mid;
13823341Sgc161489 	midpm->uc_pm_capabilities = 0; /* XXXX should this be 0?? */
13833341Sgc161489 	midpm->uc_current_power = USB_DEV_OS_FULL_PWR;
13840Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	/*
13870Sstevel@tonic-gate 	 * By not enabling parental notification, PM enforces
13880Sstevel@tonic-gate 	 * "strict parental dependency" meaning, usb_mid won't
13890Sstevel@tonic-gate 	 * power off until any of its children are in full power.
13900Sstevel@tonic-gate 	 */
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	/*
13930Sstevel@tonic-gate 	 * there are 3 scenarios:
13940Sstevel@tonic-gate 	 * 1. a well behaved device should have remote wakeup
13950Sstevel@tonic-gate 	 * at interface and device level. If the interface
13960Sstevel@tonic-gate 	 * wakes up, usb_mid will wake up
13970Sstevel@tonic-gate 	 * 2. if the device doesn't have remote wake up and
13980Sstevel@tonic-gate 	 * the interface has, PM will still work, ie.
13990Sstevel@tonic-gate 	 * the interfaces wakes up and usb_mid wakes up
14000Sstevel@tonic-gate 	 * 3. if neither the interface nor device has remote
14010Sstevel@tonic-gate 	 * wakeup, the interface will wake up when it is opened
14020Sstevel@tonic-gate 	 * and goes to sleep after being closed for a while
14030Sstevel@tonic-gate 	 * In this case usb_mid should also go to sleep shortly
14040Sstevel@tonic-gate 	 * thereafter
14050Sstevel@tonic-gate 	 * In all scenarios it doesn't really matter whether
14060Sstevel@tonic-gate 	 * remote wakeup at the device level is enabled or not
14070Sstevel@tonic-gate 	 * but we do it anyways
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	if (usb_handle_remote_wakeup(dip, USB_REMOTE_WAKEUP_ENABLE) ==
14100Sstevel@tonic-gate 	    USB_SUCCESS) {
14110Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_PM, usb_mid->mi_log_handle,
14120Sstevel@tonic-gate 		    "usb_mid_create_pm_components: "
14130Sstevel@tonic-gate 		    "Remote Wakeup Enabled");
14143341Sgc161489 		midpm->uc_wakeup_enabled = 1;
14150Sstevel@tonic-gate 	}
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	if (usb_create_pm_components(dip, &pwr_states) ==
14180Sstevel@tonic-gate 	    USB_SUCCESS) {
14193341Sgc161489 		midpm->uc_pwr_states = (uint8_t)pwr_states;
14200Sstevel@tonic-gate 		(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
14240Sstevel@tonic-gate 	    "usb_mid_create_pm_components: End");
14250Sstevel@tonic-gate }
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate /*
14290Sstevel@tonic-gate  * usb_mid_obtain_state:
14300Sstevel@tonic-gate  */
14310Sstevel@tonic-gate usb_mid_t *
usb_mid_obtain_state(dev_info_t * dip)14320Sstevel@tonic-gate usb_mid_obtain_state(dev_info_t *dip)
14330Sstevel@tonic-gate {
14340Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
14350Sstevel@tonic-gate 	usb_mid_t *statep = ddi_get_soft_state(usb_mid_statep, instance);
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	ASSERT(statep != NULL);
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	return (statep);
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate /*
14440Sstevel@tonic-gate  * ugen support
14450Sstevel@tonic-gate  */
14460Sstevel@tonic-gate /* ARGSUSED3 */
14470Sstevel@tonic-gate static int
usb_mid_open(dev_t * devp,int flags,int otyp,cred_t * credp)14480Sstevel@tonic-gate usb_mid_open(dev_t *devp, int flags, int otyp, cred_t *credp)
14490Sstevel@tonic-gate {
14500Sstevel@tonic-gate 	struct usb_mid *usb_mid;
14510Sstevel@tonic-gate 	int	rval;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
14540Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(*devp)))) == NULL) {
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 		return (ENXIO);
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_CBOPS, usb_mid->mi_log_handle,
14606898Sfb209375 	    "usb_mid_open: usb_mid = 0x%p *devp = 0x%lx",
14616898Sfb209375 	    (void *)usb_mid, *devp);
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	/* First bring the device to full power */
14640Sstevel@tonic-gate 	(void) pm_busy_component(usb_mid->mi_dip, 0);
14650Sstevel@tonic-gate 	(void) pm_raise_power(usb_mid->mi_dip, 0, USB_DEV_OS_FULL_PWR);
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	rval = usb_ugen_open(usb_mid->mi_ugen_hdl, devp, flags, otyp,
14696898Sfb209375 	    credp);
14700Sstevel@tonic-gate 	if (rval) {
14710Sstevel@tonic-gate 		(void) pm_idle_component(usb_mid->mi_dip, 0);
14720Sstevel@tonic-gate 	} else {
14730Sstevel@tonic-gate 		/*
14740Sstevel@tonic-gate 		 * since all ugen opens are exclusive we can count the
14750Sstevel@tonic-gate 		 * opens
14760Sstevel@tonic-gate 		 */
14770Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
14780Sstevel@tonic-gate 		usb_mid->mi_ugen_open_count++;
14790Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	return (rval);
14830Sstevel@tonic-gate }
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate /* ARGSUSED */
14870Sstevel@tonic-gate static int
usb_mid_close(dev_t dev,int flag,int otyp,cred_t * credp)14880Sstevel@tonic-gate usb_mid_close(dev_t dev, int flag, int otyp, cred_t *credp)
14890Sstevel@tonic-gate {
14900Sstevel@tonic-gate 	struct usb_mid *usb_mid;
14910Sstevel@tonic-gate 	int rval;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
14940Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(dev)))) == NULL) {
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 		return (ENXIO);
14970Sstevel@tonic-gate 	}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	rval = usb_ugen_close(usb_mid->mi_ugen_hdl, dev, flag, otyp,
15006898Sfb209375 	    credp);
15010Sstevel@tonic-gate 	if (rval == 0) {
15020Sstevel@tonic-gate 		(void) pm_idle_component(usb_mid->mi_dip, 0);
15030Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
15040Sstevel@tonic-gate 		usb_mid->mi_ugen_open_count--;
15050Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	return (rval);
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate static int
usb_mid_read(dev_t dev,struct uio * uio,cred_t * credp)15130Sstevel@tonic-gate usb_mid_read(dev_t dev, struct uio *uio, cred_t *credp)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate 	struct usb_mid *usb_mid;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
15180Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(dev)))) == NULL) {
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		return (ENXIO);
15210Sstevel@tonic-gate 	}
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	return (usb_ugen_read(usb_mid->mi_ugen_hdl, dev, uio, credp));
15240Sstevel@tonic-gate }
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate static int
usb_mid_write(dev_t dev,struct uio * uio,cred_t * credp)15280Sstevel@tonic-gate usb_mid_write(dev_t dev, struct uio *uio, cred_t *credp)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate 	struct usb_mid *usb_mid;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
15330Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(dev)))) == NULL) {
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 		return (ENXIO);
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	return (usb_ugen_write(usb_mid->mi_ugen_hdl, dev, uio, credp));
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate static int
usb_mid_poll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)15430Sstevel@tonic-gate usb_mid_poll(dev_t dev, short events, int anyyet,  short *reventsp,
15440Sstevel@tonic-gate     struct pollhead **phpp)
15450Sstevel@tonic-gate {
15460Sstevel@tonic-gate 	struct usb_mid *usb_mid;
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
15490Sstevel@tonic-gate 	    USB_MID_MINOR_TO_INSTANCE(getminor(dev)))) == NULL) {
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 		return (ENXIO);
15520Sstevel@tonic-gate 	}
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	return (usb_ugen_poll(usb_mid->mi_ugen_hdl, dev, events,
15556898Sfb209375 	    anyyet, reventsp, phpp));
15560Sstevel@tonic-gate }
1557