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
5055d7c80Scarlsonj  * Common Development and Distribution License (the "License").
6055d7c80Scarlsonj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
20055d7c80Scarlsonj  */
21055d7c80Scarlsonj /*
22112116d8Sfb  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25*cd21e7c5SGarrett D'Amore /*
26*cd21e7c5SGarrett D'Amore  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
27*cd21e7c5SGarrett D'Amore  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * usb multi interface and common class driver
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *	this driver attempts to attach each interface to a driver
347c478bd9Sstevel@tonic-gate  *	and may eventually handle common class features such as
357c478bd9Sstevel@tonic-gate  *	shared endpoints
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #if defined(lint) && !defined(DEBUG)
397c478bd9Sstevel@tonic-gate #define	DEBUG	1
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
427c478bd9Sstevel@tonic-gate #include <sys/usb/usba.h>
437c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_types.h>
447c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
457c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_ugen.h>
467c478bd9Sstevel@tonic-gate #include <sys/usb/usb_mid/usb_midvar.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate void usba_free_evdata(usba_evdata_t *);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* Debugging support */
514610e4a0Sfrits uint_t usb_mid_errlevel = USB_LOG_L4;
524610e4a0Sfrits uint_t usb_mid_errmask = (uint_t)DPRINT_MASK_ALL;
534610e4a0Sfrits uint_t usb_mid_instance_debug = (uint_t)-1;
544610e4a0Sfrits uint_t usb_mid_bus_config_debug = 0;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_errlevel))
577c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_errmask))
587c478bd9Sstevel@tonic-gate _NOTE(DATA_READABLE_WITHOUT_LOCK(usb_mid_instance_debug))
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", msgb))
617c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", dev_info))
627c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("unique", usb_pipe_policy))
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Hotplug support
667c478bd9Sstevel@tonic-gate  * Leaf ops (hotplug controls for client devices)
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate static int usb_mid_open(dev_t *, int, int, cred_t *);
697c478bd9Sstevel@tonic-gate static int usb_mid_close(dev_t, int, int, cred_t *);
707c478bd9Sstevel@tonic-gate static int usb_mid_read(dev_t, struct uio *, cred_t *);
717c478bd9Sstevel@tonic-gate static int usb_mid_write(dev_t, struct uio *, cred_t *);
727c478bd9Sstevel@tonic-gate static int usb_mid_poll(dev_t, short, int,  short *,
737c478bd9Sstevel@tonic-gate 					struct pollhead **);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static struct cb_ops usb_mid_cb_ops = {
767c478bd9Sstevel@tonic-gate 	usb_mid_open,
777c478bd9Sstevel@tonic-gate 	usb_mid_close,
787c478bd9Sstevel@tonic-gate 	nodev,		/* strategy */
797c478bd9Sstevel@tonic-gate 	nodev,		/* print */
807c478bd9Sstevel@tonic-gate 	nodev,		/* dump */
817c478bd9Sstevel@tonic-gate 	usb_mid_read,	/* read */
827c478bd9Sstevel@tonic-gate 	usb_mid_write,	/* write */
837c478bd9Sstevel@tonic-gate 	nodev,
847c478bd9Sstevel@tonic-gate 	nodev,		/* devmap */
857c478bd9Sstevel@tonic-gate 	nodev,		/* mmap */
867c478bd9Sstevel@tonic-gate 	nodev,		/* segmap */
877c478bd9Sstevel@tonic-gate 	usb_mid_poll,	/* poll */
887c478bd9Sstevel@tonic-gate 	ddi_prop_op,	/* prop_op */
897c478bd9Sstevel@tonic-gate 	NULL,
907c478bd9Sstevel@tonic-gate 	D_MP
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static int usb_mid_busop_get_eventcookie(dev_info_t *dip,
947c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
957c478bd9Sstevel@tonic-gate 			char *eventname,
967c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t *cookie);
977c478bd9Sstevel@tonic-gate static int usb_mid_busop_add_eventcall(dev_info_t *dip,
987c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
997c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t cookie,
1007c478bd9Sstevel@tonic-gate 			void (*callback)(dev_info_t *dip,
1017c478bd9Sstevel@tonic-gate 				ddi_eventcookie_t cookie, void *arg,
1027c478bd9Sstevel@tonic-gate 				void *bus_impldata),
1037c478bd9Sstevel@tonic-gate 			void *arg, ddi_callback_id_t *cb_id);
1047c478bd9Sstevel@tonic-gate static int usb_mid_busop_remove_eventcall(dev_info_t *dip,
1057c478bd9Sstevel@tonic-gate 			ddi_callback_id_t cb_id);
1067c478bd9Sstevel@tonic-gate static int usb_mid_busop_post_event(dev_info_t *dip,
1077c478bd9Sstevel@tonic-gate 			dev_info_t *rdip,
1087c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t cookie,
1097c478bd9Sstevel@tonic-gate 			void *bus_impldata);
1107c478bd9Sstevel@tonic-gate static int usb_mid_bus_config(dev_info_t *dip,
1117c478bd9Sstevel@tonic-gate 			uint_t flag,
1127c478bd9Sstevel@tonic-gate 			ddi_bus_config_op_t op,
1137c478bd9Sstevel@tonic-gate 			void *arg,
1147c478bd9Sstevel@tonic-gate 			dev_info_t **child);
1157c478bd9Sstevel@tonic-gate static int usb_mid_bus_unconfig(dev_info_t *dip,
1167c478bd9Sstevel@tonic-gate 			uint_t flag,
1177c478bd9Sstevel@tonic-gate 			ddi_bus_config_op_t op,
1187c478bd9Sstevel@tonic-gate 			void *arg);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * autoconfiguration data and routines.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate static int	usb_mid_info(dev_info_t *, ddi_info_cmd_t,
1257c478bd9Sstevel@tonic-gate 				void *, void **);
1267c478bd9Sstevel@tonic-gate static int	usb_mid_attach(dev_info_t *, ddi_attach_cmd_t);
1277c478bd9Sstevel@tonic-gate static int	usb_mid_detach(dev_info_t *, ddi_detach_cmd_t);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /* other routines */
1307c478bd9Sstevel@tonic-gate static void usb_mid_create_pm_components(dev_info_t *, usb_mid_t *);
1317c478bd9Sstevel@tonic-gate static int usb_mid_bus_ctl(dev_info_t *, dev_info_t	*,
1327c478bd9Sstevel@tonic-gate 				ddi_ctl_enum_t, void *, void *);
1337c478bd9Sstevel@tonic-gate static int usb_mid_power(dev_info_t *, int, int);
1347c478bd9Sstevel@tonic-gate static int usb_mid_restore_device_state(dev_info_t *, usb_mid_t *);
1357c478bd9Sstevel@tonic-gate static usb_mid_t  *usb_mid_obtain_state(dev_info_t *);
136d73ae94eSgc static void usb_mid_event_cb(dev_info_t *, ddi_eventcookie_t, void *, void *);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Busops vector
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate static struct bus_ops usb_mid_busops = {
1427c478bd9Sstevel@tonic-gate 	BUSO_REV,
1437c478bd9Sstevel@tonic-gate 	nullbusmap,			/* bus_map */
1447c478bd9Sstevel@tonic-gate 	NULL,				/* bus_get_intrspec */
1457c478bd9Sstevel@tonic-gate 	NULL,				/* bus_add_intrspec */
1467c478bd9Sstevel@tonic-gate 	NULL,				/* bus_remove_intrspec */
1477c478bd9Sstevel@tonic-gate 	NULL,				/* XXXX bus_map_fault */
148*cd21e7c5SGarrett D'Amore 	NULL,				/* bus_dma_map */
1497c478bd9Sstevel@tonic-gate 	ddi_dma_allochdl,
1507c478bd9Sstevel@tonic-gate 	ddi_dma_freehdl,
1517c478bd9Sstevel@tonic-gate 	ddi_dma_bindhdl,
1527c478bd9Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1537c478bd9Sstevel@tonic-gate 	ddi_dma_flush,
1547c478bd9Sstevel@tonic-gate 	ddi_dma_win,
1557c478bd9Sstevel@tonic-gate 	ddi_dma_mctl,			/* bus_dma_ctl */
1567c478bd9Sstevel@tonic-gate 	usb_mid_bus_ctl,		/* bus_ctl */
1577c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,		/* bus_prop_op */
1587c478bd9Sstevel@tonic-gate 	usb_mid_busop_get_eventcookie,
1597c478bd9Sstevel@tonic-gate 	usb_mid_busop_add_eventcall,
1607c478bd9Sstevel@tonic-gate 	usb_mid_busop_remove_eventcall,
1617c478bd9Sstevel@tonic-gate 	usb_mid_busop_post_event,	/* bus_post_event */
1627c478bd9Sstevel@tonic-gate 	NULL,				/* bus_intr_ctl */
1637c478bd9Sstevel@tonic-gate 	usb_mid_bus_config,		/* bus_config */
1647c478bd9Sstevel@tonic-gate 	usb_mid_bus_unconfig,		/* bus_unconfig */
1657c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_init */
1667c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_fini */
1677c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_enter */
1687c478bd9Sstevel@tonic-gate 	NULL,				/* bus_fm_access_exit */
1697c478bd9Sstevel@tonic-gate 	NULL				/* bus_power */
1707c478bd9Sstevel@tonic-gate };
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static struct dev_ops usb_mid_ops = {
1747c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1757c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
1767c478bd9Sstevel@tonic-gate 	usb_mid_info,		/* info */
1777c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1787c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1797c478bd9Sstevel@tonic-gate 	usb_mid_attach,		/* attach */
1807c478bd9Sstevel@tonic-gate 	usb_mid_detach,		/* detach */
1817c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1827c478bd9Sstevel@tonic-gate 	&usb_mid_cb_ops,	/* driver operations */
1837c478bd9Sstevel@tonic-gate 	&usb_mid_busops,	/* bus operations */
18419397407SSherry Moore 	usb_mid_power,		/* power */
18519397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1867c478bd9Sstevel@tonic-gate };
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1897c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module. This one is a driver */
19077e51571Sgongtian zhao - Sun Microsystems - Beijing China 	"USB Multi Interface Driver", /* Name of the module. */
1917c478bd9Sstevel@tonic-gate 	&usb_mid_ops,	/* driver ops */
1927c478bd9Sstevel@tonic-gate };
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1957c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1967c478bd9Sstevel@tonic-gate };
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate #define	USB_MID_INITIAL_SOFT_SPACE 4
1997c478bd9Sstevel@tonic-gate static	void	*usb_mid_statep;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * prototypes
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate static void usb_mid_create_children(usb_mid_t *usb_mid);
2067c478bd9Sstevel@tonic-gate static int usb_mid_cleanup(dev_info_t *dip, usb_mid_t	*usb_mid);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * event definition
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate static ndi_event_definition_t usb_mid_ndi_event_defs[] = {
2127c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_REMOVAL, DDI_DEVI_REMOVE_EVENT, EPL_KERNEL,
2137c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2147c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_HOT_INSERTION, DDI_DEVI_INSERT_EVENT, EPL_KERNEL,
2157c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2167c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_POST_RESUME, USBA_POST_RESUME_EVENT, EPL_KERNEL,
2177c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL},
2187c478bd9Sstevel@tonic-gate 	{USBA_EVENT_TAG_PRE_SUSPEND, USBA_PRE_SUSPEND_EVENT, EPL_KERNEL,
2197c478bd9Sstevel@tonic-gate 						NDI_EVENT_POST_TO_ALL}
2207c478bd9Sstevel@tonic-gate };
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #define	USB_MID_N_NDI_EVENTS \
2237c478bd9Sstevel@tonic-gate 	(sizeof (usb_mid_ndi_event_defs) / sizeof (ndi_event_definition_t))
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static	ndi_event_set_t usb_mid_ndi_events = {
2267c478bd9Sstevel@tonic-gate 	NDI_EVENTS_REV1, USB_MID_N_NDI_EVENTS, usb_mid_ndi_event_defs};
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * standard driver entry points
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate int
_init(void)2337c478bd9Sstevel@tonic-gate _init(void)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	int rval;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	rval = ddi_soft_state_init(&usb_mid_statep, sizeof (struct usb_mid),
2387c478bd9Sstevel@tonic-gate 	    USB_MID_INITIAL_SOFT_SPACE);
2397c478bd9Sstevel@tonic-gate 	if (rval != 0) {
2407c478bd9Sstevel@tonic-gate 		return (rval);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if ((rval = mod_install(&modlinkage)) != 0) {
2447c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&usb_mid_statep);
2457c478bd9Sstevel@tonic-gate 		return (rval);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (rval);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate int
_fini(void)2537c478bd9Sstevel@tonic-gate _fini(void)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	int	rval;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	rval = mod_remove(&modlinkage);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (rval) {
2607c478bd9Sstevel@tonic-gate 		return (rval);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&usb_mid_statep);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	return (rval);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2707c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2777c478bd9Sstevel@tonic-gate static int
usb_mid_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2787c478bd9Sstevel@tonic-gate usb_mid_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid;
2817c478bd9Sstevel@tonic-gate 	int		instance =
282112116d8Sfb 	    USB_MID_MINOR_TO_INSTANCE(getminor((dev_t)arg));
2837c478bd9Sstevel@tonic-gate 	int		error = DDI_FAILURE;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2867c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2877c478bd9Sstevel@tonic-gate 		if ((usb_mid = ddi_get_soft_state(usb_mid_statep,
2887c478bd9Sstevel@tonic-gate 		    instance)) != NULL) {
2897c478bd9Sstevel@tonic-gate 			*result = (void *)usb_mid->mi_dip;
2907c478bd9Sstevel@tonic-gate 			if (*result != NULL) {
2917c478bd9Sstevel@tonic-gate 				error = DDI_SUCCESS;
2927c478bd9Sstevel@tonic-gate 			}
2937c478bd9Sstevel@tonic-gate 		} else {
2947c478bd9Sstevel@tonic-gate 			*result = NULL;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 		break;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2997c478bd9Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
3007c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
3017c478bd9Sstevel@tonic-gate 		break;
3027c478bd9Sstevel@tonic-gate 	default:
3037c478bd9Sstevel@tonic-gate 		break;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	return (error);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * child  post attach/detach notification
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate static void
usb_mid_post_attach(usb_mid_t * usb_mid,uint8_t ifno,struct attachspec * as)3147c478bd9Sstevel@tonic-gate usb_mid_post_attach(usb_mid_t *usb_mid, uint8_t ifno, struct attachspec *as)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3177c478bd9Sstevel@tonic-gate 	    "usb_mid_post_attach: ifno = %d result = %d", ifno, as->result);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* if child successfully attached, set power */
3207c478bd9Sstevel@tonic-gate 	if (as->result == DDI_SUCCESS) {
3217c478bd9Sstevel@tonic-gate 		/*
3227c478bd9Sstevel@tonic-gate 		 * Check if the child created wants to be power managed.
3237c478bd9Sstevel@tonic-gate 		 * If yes, the childs power level gets automatically tracked
3247c478bd9Sstevel@tonic-gate 		 * by DDI_CTLOPS_POWER busctl.
3257c478bd9Sstevel@tonic-gate 		 * If no, we set power of the new child by default
3267c478bd9Sstevel@tonic-gate 		 * to USB_DEV_OS_FULL_PWR. Because we should never suspend.
3277c478bd9Sstevel@tonic-gate 		 */
3287c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
3297c478bd9Sstevel@tonic-gate 		usb_mid->mi_attach_count++;
3307c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static void
usb_mid_post_detach(usb_mid_t * usb_mid,uint8_t ifno,struct detachspec * ds)3367c478bd9Sstevel@tonic-gate usb_mid_post_detach(usb_mid_t *usb_mid, uint8_t ifno, struct detachspec *ds)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3397c478bd9Sstevel@tonic-gate 	    "usb_mid_post_detach: ifno = %d result = %d", ifno, ds->result);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * if the device is successfully detached,
3437c478bd9Sstevel@tonic-gate 	 * mark component as idle
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	if (ds->result == DDI_SUCCESS) {
3467c478bd9Sstevel@tonic-gate 		usba_device_t *usba_device =
347112116d8Sfb 		    usba_get_usba_device(usb_mid->mi_dip);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		/* check for leaks except when where is a ugen open */
3527c478bd9Sstevel@tonic-gate 		if ((ds->cmd == DDI_DETACH) &&
3537c478bd9Sstevel@tonic-gate 		    (--usb_mid->mi_attach_count == 0) && usba_device &&
3547c478bd9Sstevel@tonic-gate 		    (usb_mid->mi_ugen_open_count == 0)) {
3557c478bd9Sstevel@tonic-gate 			usba_check_for_leaks(usba_device);
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * bus ctl support. we handle notifications here and the
3647c478bd9Sstevel@tonic-gate  * rest goes up to root hub/hcd
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3677c478bd9Sstevel@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)3687c478bd9Sstevel@tonic-gate usb_mid_bus_ctl(dev_info_t *dip,
3697c478bd9Sstevel@tonic-gate 	dev_info_t	*rdip,
3707c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t	op,
3717c478bd9Sstevel@tonic-gate 	void		*arg,
3727c478bd9Sstevel@tonic-gate 	void		*result)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	usba_device_t *hub_usba_device = usba_get_usba_device(rdip);
3757c478bd9Sstevel@tonic-gate 	dev_info_t *root_hub_dip = hub_usba_device->usb_root_hub_dip;
3767c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid;
3777c478bd9Sstevel@tonic-gate 	struct attachspec *as;
3787c478bd9Sstevel@tonic-gate 	struct detachspec *ds;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	usb_mid = usb_mid_obtain_state(dip);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3837c478bd9Sstevel@tonic-gate 	    "usb_mid_bus_ctl:\n\t"
3847c478bd9Sstevel@tonic-gate 	    "dip = 0x%p, rdip = 0x%p, op = 0x%x, arg = 0x%p",
385112116d8Sfb 	    (void *)dip, (void *)rdip, op, arg);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	switch (op) {
3887c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
3897c478bd9Sstevel@tonic-gate 		as = (struct attachspec *)arg;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		switch (as->when) {
3927c478bd9Sstevel@tonic-gate 		case DDI_PRE :
3937c478bd9Sstevel@tonic-gate 			/* nothing to do basically */
3947c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
3957c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_ATTACH");
3967c478bd9Sstevel@tonic-gate 			break;
3977c478bd9Sstevel@tonic-gate 		case DDI_POST :
3987c478bd9Sstevel@tonic-gate 			usb_mid_post_attach(usb_mid, usba_get_ifno(rdip),
3997c478bd9Sstevel@tonic-gate 			    (struct attachspec *)arg);
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		break;
4047c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
4057c478bd9Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		switch (ds->when) {
4087c478bd9Sstevel@tonic-gate 		case DDI_PRE :
4097c478bd9Sstevel@tonic-gate 			/* nothing to do basically */
4107c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
4117c478bd9Sstevel@tonic-gate 			    "DDI_PRE DDI_CTLOPS_DETACH");
4127c478bd9Sstevel@tonic-gate 			break;
4137c478bd9Sstevel@tonic-gate 		case DDI_POST :
4147c478bd9Sstevel@tonic-gate 			usb_mid_post_detach(usb_mid, usba_get_ifno(rdip),
4157c478bd9Sstevel@tonic-gate 			    (struct detachspec *)arg);
4167c478bd9Sstevel@tonic-gate 			break;
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		break;
4207c478bd9Sstevel@tonic-gate 	default:
4217c478bd9Sstevel@tonic-gate 		/* pass to root hub to handle */
4227c478bd9Sstevel@tonic-gate 		return (usba_bus_ctl(root_hub_dip, rdip, op, arg, result));
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate  * bus enumeration entry points
4317c478bd9Sstevel@tonic-gate  */
4327c478bd9Sstevel@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)4337c478bd9Sstevel@tonic-gate usb_mid_bus_config(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
4347c478bd9Sstevel@tonic-gate     void *arg, dev_info_t **child)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	int		rval, circ;
4377c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid = usb_mid_obtain_state(dip);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
4407c478bd9Sstevel@tonic-gate 	    "usb_mid_bus_config: op=%d", op);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if (usb_mid_bus_config_debug) {
4437c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circ);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/* enumerate each interface below us */
4497c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
4507c478bd9Sstevel@tonic-gate 	usb_mid_create_children(usb_mid);
4517c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_config(dip, flag, op, arg, child, 0);
4547c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circ);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (rval);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate static int
usb_mid_bus_unconfig(dev_info_t * dip,uint_t flag,ddi_bus_config_op_t op,void * arg)4617c478bd9Sstevel@tonic-gate usb_mid_bus_unconfig(dev_info_t *dip, uint_t flag, ddi_bus_config_op_t op,
4627c478bd9Sstevel@tonic-gate     void *arg)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip, *mdip;
4677c478bd9Sstevel@tonic-gate 	int		interface, circular_count;
468d73ae94eSgc 	int		rval = NDI_SUCCESS;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
4717c478bd9Sstevel@tonic-gate 	    "usb_mid_bus_unconfig: op=%d", op);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if (usb_mid_bus_config_debug) {
4747c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_DEBUG;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * first offline and if offlining successful, then
4797c478bd9Sstevel@tonic-gate 	 * remove children
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	if (op == BUS_UNCONFIG_ALL) {
4827c478bd9Sstevel@tonic-gate 		flag &= ~(NDI_DEVI_REMOVE | NDI_UNCONFIG);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	ndi_devi_enter(dip, &circular_count);
4867c478bd9Sstevel@tonic-gate 	rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (op == BUS_UNCONFIG_ALL && rval == NDI_SUCCESS &&
4897c478bd9Sstevel@tonic-gate 	    (flag & NDI_AUTODETACH) == 0) {
4907c478bd9Sstevel@tonic-gate 		flag |= NDI_DEVI_REMOVE;
4917c478bd9Sstevel@tonic-gate 		rval = ndi_busop_bus_unconfig(dip, flag, op, arg);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/* update children's list */
4957c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
4967c478bd9Sstevel@tonic-gate 	for (interface = 0; usb_mid->mi_children_dips &&
497d73ae94eSgc 	    (interface < usb_mid->mi_n_ifs) &&
498d73ae94eSgc 	    (usb_mid->mi_children_ifs[interface]); interface++) {
4997c478bd9Sstevel@tonic-gate 		mdip = usb_mid->mi_children_dips[interface];
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		/* now search if this dip still exists */
502112116d8Sfb 		for (cdip = ddi_get_child(dip); cdip && (cdip != mdip); )
503112116d8Sfb 			cdip = ddi_get_next_sibling(cdip);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		if (cdip != mdip) {
5067c478bd9Sstevel@tonic-gate 			/* we lost the dip on this interface */
5077c478bd9Sstevel@tonic-gate 			usb_mid->mi_children_dips[interface] = NULL;
5087c478bd9Sstevel@tonic-gate 		} else if (cdip) {
5097c478bd9Sstevel@tonic-gate 			/*
5107c478bd9Sstevel@tonic-gate 			 * keep in DS_INITALIZED to prevent parent
5117c478bd9Sstevel@tonic-gate 			 * from detaching
5127c478bd9Sstevel@tonic-gate 			 */
5137c478bd9Sstevel@tonic-gate 			(void) ddi_initchild(ddi_get_parent(cdip), cdip);
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	ndi_devi_exit(dip, circular_count);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ALL, usb_mid->mi_log_handle,
5217c478bd9Sstevel@tonic-gate 	    "usb_mid_bus_config: rval=%d", rval);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	return (rval);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /* power entry point */
5287c478bd9Sstevel@tonic-gate /* ARGSUSED */
5297c478bd9Sstevel@tonic-gate static int
usb_mid_power(dev_info_t * dip,int comp,int level)5307c478bd9Sstevel@tonic-gate usb_mid_power(dev_info_t *dip, int comp, int level)
5317c478bd9Sstevel@tonic-gate {
532d73ae94eSgc 	usb_mid_t		*usb_mid;
533d73ae94eSgc 	usb_common_power_t	*midpm;
534d73ae94eSgc 	int			rval = DDI_FAILURE;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	usb_mid =  usb_mid_obtain_state(dip);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_PM, usb_mid->mi_log_handle,
539112116d8Sfb 	    "usb_mid_power: Begin: usb_mid = %p, level = %d",
540112116d8Sfb 	    (void *)usb_mid, level);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
5437c478bd9Sstevel@tonic-gate 	midpm = usb_mid->mi_pm;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/* check if we are transitioning to a legal power level */
546d73ae94eSgc 	if (USB_DEV_PWRSTATE_OK(midpm->uc_pwr_states, level)) {
5477c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_PM, usb_mid->mi_log_handle,
5487c478bd9Sstevel@tonic-gate 		    "usb_mid_power: illegal power level = %d "
549d73ae94eSgc 		    "uc_pwr_states = %x", level, midpm->uc_pwr_states);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 		return (rval);
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 
556036aa261Sgc 	rval = usba_common_power(dip, &(midpm->uc_current_power),
557036aa261Sgc 	    &(usb_mid->mi_dev_state), level);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	mutex_exit(&usb_mid->mi_mutex);
5607c478bd9Sstevel@tonic-gate 
561d73ae94eSgc 	return (rval);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * attach/resume entry point
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate static int
usb_mid_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)5697c478bd9Sstevel@tonic-gate usb_mid_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	int		instance = ddi_get_instance(dip);
5727c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid = NULL;
573d73ae94eSgc 	uint_t		n_ifs, i;
5747c478bd9Sstevel@tonic-gate 	size_t		size;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	switch (cmd) {
5777c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		break;
5807c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
5817c478bd9Sstevel@tonic-gate 		usb_mid = (usb_mid_t *)ddi_get_soft_state(usb_mid_statep,
5827c478bd9Sstevel@tonic-gate 		    instance);
5837c478bd9Sstevel@tonic-gate 		(void) usb_mid_restore_device_state(dip, usb_mid);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		if (usb_mid->mi_ugen_hdl) {
5867c478bd9Sstevel@tonic-gate 			(void) usb_ugen_attach(usb_mid->mi_ugen_hdl,
587112116d8Sfb 			    DDI_RESUME);
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5917c478bd9Sstevel@tonic-gate 	default:
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	/*
5977c478bd9Sstevel@tonic-gate 	 * Attach:
5987c478bd9Sstevel@tonic-gate 	 *
5997c478bd9Sstevel@tonic-gate 	 * Allocate soft state and initialize
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(usb_mid_statep, instance) != DDI_SUCCESS) {
6027c478bd9Sstevel@tonic-gate 		goto fail;
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	usb_mid = ddi_get_soft_state(usb_mid_statep, instance);
6067c478bd9Sstevel@tonic-gate 	if (usb_mid == NULL) {
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		goto fail;
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/* allocate handle for logging of messages */
6127c478bd9Sstevel@tonic-gate 	usb_mid->mi_log_handle = usb_alloc_log_hdl(dip, "mid",
613112116d8Sfb 	    &usb_mid_errlevel,
614112116d8Sfb 	    &usb_mid_errmask, &usb_mid_instance_debug,
615112116d8Sfb 	    0);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	usb_mid->mi_usba_device = usba_get_usba_device(dip);
6187c478bd9Sstevel@tonic-gate 	usb_mid->mi_dip	= dip;
6197c478bd9Sstevel@tonic-gate 	usb_mid->mi_instance = instance;
6207c478bd9Sstevel@tonic-gate 	usb_mid->mi_n_ifs = usb_mid->mi_usba_device->usb_n_ifs;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/* attach client driver to USBA */
6237c478bd9Sstevel@tonic-gate 	if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) {
6247c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
6257c478bd9Sstevel@tonic-gate 		    "usb_client_attach failed");
6267c478bd9Sstevel@tonic-gate 		goto fail;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	if (usb_get_dev_data(dip, &usb_mid->mi_dev_data, USB_PARSE_LVL_NONE,
6297c478bd9Sstevel@tonic-gate 	    0) != USB_SUCCESS) {
6307c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
6317c478bd9Sstevel@tonic-gate 		    "usb_get_dev_data failed");
6327c478bd9Sstevel@tonic-gate 		goto fail;
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	mutex_init(&usb_mid->mi_mutex, NULL, MUTEX_DRIVER,
636112116d8Sfb 	    usb_mid->mi_dev_data->dev_iblock_cookie);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	usb_free_dev_data(dip, usb_mid->mi_dev_data);
6397c478bd9Sstevel@tonic-gate 	usb_mid->mi_dev_data = NULL;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_LOCK_INIT;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "usb_mid", S_IFCHR,
6447c478bd9Sstevel@tonic-gate 	    instance << USB_MID_MINOR_INSTANCE_SHIFT,
6457c478bd9Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
646d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
6477c478bd9Sstevel@tonic-gate 		    "cannot create devctl minor node");
6487c478bd9Sstevel@tonic-gate 		goto fail;
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_MINOR_NODE_CREATED;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * allocate array for keeping track of child dips
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	n_ifs = usb_mid->mi_n_ifs;
6577c478bd9Sstevel@tonic-gate 	usb_mid->mi_cd_list_length = size = (sizeof (dev_info_t *)) * n_ifs;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	usb_mid->mi_children_dips = kmem_zalloc(size, KM_SLEEP);
6607c478bd9Sstevel@tonic-gate 	usb_mid->mi_child_events = kmem_zalloc(sizeof (uint8_t) * n_ifs,
661112116d8Sfb 	    KM_SLEEP);
662d73ae94eSgc 	usb_mid->mi_children_ifs = kmem_zalloc(sizeof (uint_t) * n_ifs,
663112116d8Sfb 	    KM_SLEEP);
664d73ae94eSgc 	for (i = 0; i < n_ifs; i++) {
665d73ae94eSgc 		usb_mid->mi_children_ifs[i] = 1;
666d73ae94eSgc 	}
667d73ae94eSgc 
6687c478bd9Sstevel@tonic-gate 	/*
6697c478bd9Sstevel@tonic-gate 	 * Event handling: definition and registration
6707c478bd9Sstevel@tonic-gate 	 * get event handle for events that we have defined
6717c478bd9Sstevel@tonic-gate 	 */
6727c478bd9Sstevel@tonic-gate 	(void) ndi_event_alloc_hdl(dip, 0, &usb_mid->mi_ndi_event_hdl,
673112116d8Sfb 	    NDI_SLEEP);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	/* bind event set to the handle */
6767c478bd9Sstevel@tonic-gate 	if (ndi_event_bind_set(usb_mid->mi_ndi_event_hdl, &usb_mid_ndi_events,
6777c478bd9Sstevel@tonic-gate 	    NDI_SLEEP)) {
6787c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
6797c478bd9Sstevel@tonic-gate 		    "usb_mid_attach: binding event set failed");
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		goto fail;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	usb_mid->mi_dev_state = USB_DEV_ONLINE;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * now create components to power manage this device
6887c478bd9Sstevel@tonic-gate 	 * before attaching children
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	usb_mid_create_pm_components(dip, usb_mid);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/* event registration for events from our parent */
693d73ae94eSgc 	usba_common_register_events(usb_mid->mi_dip, 1, usb_mid_event_cb);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	usb_mid->mi_init_state |= USB_MID_EVENTS_REGISTERED;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate fail:
702d291d9f2Sfrits 	USB_DPRINTF_L2(DPRINT_MASK_ATTA, NULL, "usb_mid%d cannot attach",
7037c478bd9Sstevel@tonic-gate 	    instance);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	if (usb_mid) {
7067c478bd9Sstevel@tonic-gate 		(void) usb_mid_cleanup(dip, usb_mid);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /* detach or suspend this instance */
7147c478bd9Sstevel@tonic-gate static int
usb_mid_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)7157c478bd9Sstevel@tonic-gate usb_mid_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	usb_mid_t	*usb_mid = usb_mid_obtain_state(dip);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7207c478bd9Sstevel@tonic-gate 	    "usb_mid_detach: cmd = 0x%x", cmd);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	switch (cmd) {
7237c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		return (usb_mid_cleanup(dip, usb_mid));
7267c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
7277c478bd9Sstevel@tonic-gate 		/* nothing to do */
7287c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
7297c478bd9Sstevel@tonic-gate 		usb_mid->mi_dev_state = USB_DEV_SUSPENDED;
7307c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		if (usb_mid->mi_ugen_hdl) {
7337c478bd9Sstevel@tonic-gate 			int rval = usb_ugen_detach(usb_mid->mi_ugen_hdl,
734112116d8Sfb 			    DDI_SUSPEND);
7357c478bd9Sstevel@tonic-gate 			return (rval == USB_SUCCESS ? DDI_SUCCESS :
736112116d8Sfb 			    DDI_FAILURE);
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7407c478bd9Sstevel@tonic-gate 	default:
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	_NOTE(NOT_REACHED)
7467c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate  * usb_mid_cleanup:
7517c478bd9Sstevel@tonic-gate  *	cleanup usb_mid and deallocate. this function is called for
7527c478bd9Sstevel@tonic-gate  *	handling attach failures and detaching including dynamic
7537c478bd9Sstevel@tonic-gate  *	reconfiguration
7547c478bd9Sstevel@tonic-gate  */
7557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7567c478bd9Sstevel@tonic-gate static int
usb_mid_cleanup(dev_info_t * dip,usb_mid_t * usb_mid)7577c478bd9Sstevel@tonic-gate usb_mid_cleanup(dev_info_t *dip, usb_mid_t *usb_mid)
7587c478bd9Sstevel@tonic-gate {
759d73ae94eSgc 	usb_common_power_t	*midpm;
7607c478bd9Sstevel@tonic-gate 	int		rval;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7637c478bd9Sstevel@tonic-gate 	    "usb_mid_cleanup:");
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if ((usb_mid->mi_init_state & USB_MID_LOCK_INIT) == 0) {
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		goto done;
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/*
7717c478bd9Sstevel@tonic-gate 	 * deallocate events, if events are still registered
7727c478bd9Sstevel@tonic-gate 	 * (ie. children still attached) then we have to fail the detach
7737c478bd9Sstevel@tonic-gate 	 */
7747c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_ndi_event_hdl &&
7757c478bd9Sstevel@tonic-gate 	    (ndi_event_free_hdl(usb_mid->mi_ndi_event_hdl) != NDI_SUCCESS)) {
7767c478bd9Sstevel@tonic-gate 
777d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
7787c478bd9Sstevel@tonic-gate 		    "usb_mid_cleanup: ndi_event_free_hdl failed");
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * Disable the event callbacks, after this point, event
7857c478bd9Sstevel@tonic-gate 	 * callbacks will never get called. Note we shouldn't hold
7867c478bd9Sstevel@tonic-gate 	 * mutex while unregistering events because there may be a
7877c478bd9Sstevel@tonic-gate 	 * competing event callback thread. Event callbacks are done
7887c478bd9Sstevel@tonic-gate 	 * with ndi mutex held and this can cause a potential deadlock.
7897c478bd9Sstevel@tonic-gate 	 * Note that cleanup can't fail after deregistration of events.
7907c478bd9Sstevel@tonic-gate 	 */
7917c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_init_state & USB_MID_EVENTS_REGISTERED) {
792d73ae94eSgc 		usba_common_unregister_events(usb_mid->mi_dip, 1);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	midpm = usb_mid->mi_pm;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	mutex_enter(&usb_mid->mi_mutex);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	if ((midpm) && (usb_mid->mi_dev_state != USB_DEV_DISCONNECTED)) {
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		(void) pm_busy_component(dip, 0);
804d73ae94eSgc 		if (midpm->uc_wakeup_enabled) {
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 			/* First bring the device to full power */
8077c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 			rval = usb_handle_remote_wakeup(dip,
8107c478bd9Sstevel@tonic-gate 			    USB_REMOTE_WAKEUP_DISABLE);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 			if (rval != DDI_SUCCESS) {
8137c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_EVENTS,
8147c478bd9Sstevel@tonic-gate 				    usb_mid->mi_log_handle,
8157c478bd9Sstevel@tonic-gate 				    "usb_cleanup: disable remote "
8167c478bd9Sstevel@tonic-gate 				    "wakeup failed, rval=%d", rval);
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		(void) pm_lower_power(usb_mid->mi_dip, 0, USB_DEV_OS_PWR_OFF);
8217c478bd9Sstevel@tonic-gate 		(void) pm_idle_component(dip, 0);
8227c478bd9Sstevel@tonic-gate 	} else {
8237c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	if (midpm) {
827d73ae94eSgc 		kmem_free(midpm, sizeof (usb_common_power_t));
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	/* free children list */
8317c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_children_dips) {
8327c478bd9Sstevel@tonic-gate 		kmem_free(usb_mid->mi_children_dips,
833112116d8Sfb 		    usb_mid->mi_cd_list_length);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_child_events) {
8377c478bd9Sstevel@tonic-gate 		kmem_free(usb_mid->mi_child_events, sizeof (uint8_t) *
8387c478bd9Sstevel@tonic-gate 		    usb_mid->mi_n_ifs);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
841d73ae94eSgc 	if (usb_mid->mi_children_ifs) {
842d73ae94eSgc 		kmem_free(usb_mid->mi_children_ifs, sizeof (uint_t) *
843d73ae94eSgc 		    usb_mid->mi_n_ifs);
844d73ae94eSgc 	}
845d73ae94eSgc 
8467c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_init_state & USB_MID_MINOR_NODE_CREATED) {
8477c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	mutex_destroy(&usb_mid->mi_mutex);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate done:
8537c478bd9Sstevel@tonic-gate 	usb_client_detach(dip, usb_mid->mi_dev_data);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_ugen_hdl) {
8567c478bd9Sstevel@tonic-gate 		(void) usb_ugen_detach(usb_mid->mi_ugen_hdl, DDI_DETACH);
8577c478bd9Sstevel@tonic-gate 		usb_ugen_release_hdl(usb_mid->mi_ugen_hdl);
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(usb_mid->mi_log_handle);
8617c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(usb_mid_statep, ddi_get_instance(dip));
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate static void
usb_mid_ugen_attach(usb_mid_t * usb_mid,boolean_t remove_children)8707c478bd9Sstevel@tonic-gate usb_mid_ugen_attach(usb_mid_t *usb_mid, boolean_t remove_children)
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate 	_NOTE(NO_COMPETING_THREADS_NOW);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_ugen_hdl == NULL) {
8757c478bd9Sstevel@tonic-gate 		usb_ugen_info_t usb_ugen_info;
8767c478bd9Sstevel@tonic-gate 		int		rval;
8777c478bd9Sstevel@tonic-gate 		usb_ugen_hdl_t	hdl;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
880112116d8Sfb 		    "usb_mid_ugen_attach: get handle");
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		bzero(&usb_ugen_info, sizeof (usb_ugen_info));
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_flags = (remove_children ?
885112116d8Sfb 		    USB_UGEN_REMOVE_CHILDREN : 0);
8867c478bd9Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_minor_node_ugen_bits_mask =
887112116d8Sfb 		    (dev_t)USB_MID_MINOR_UGEN_BITS_MASK;
8887c478bd9Sstevel@tonic-gate 		usb_ugen_info.usb_ugen_minor_node_instance_mask =
889112116d8Sfb 		    (dev_t)~USB_MID_MINOR_UGEN_BITS_MASK;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
8927c478bd9Sstevel@tonic-gate 		hdl = usb_ugen_get_hdl(usb_mid->mi_dip,
893112116d8Sfb 		    &usb_ugen_info);
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 		if ((rval = usb_ugen_attach(hdl, DDI_ATTACH)) != USB_SUCCESS) {
8967c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
8977c478bd9Sstevel@tonic-gate 			    "failed to create ugen support (%d)", rval);
8987c478bd9Sstevel@tonic-gate 			usb_ugen_release_hdl(hdl);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
9017c478bd9Sstevel@tonic-gate 		} else {
9027c478bd9Sstevel@tonic-gate 			mutex_enter(&usb_mid->mi_mutex);
9037c478bd9Sstevel@tonic-gate 			usb_mid->mi_ugen_hdl = hdl;
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
907055d7c80Scarlsonj #ifndef lint
9087c478bd9Sstevel@tonic-gate 	_NOTE(COMPETING_THREADS_NOW);
909055d7c80Scarlsonj #endif
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate /*
9147c478bd9Sstevel@tonic-gate  * usb_mid_create_children:
9157c478bd9Sstevel@tonic-gate  */
9167c478bd9Sstevel@tonic-gate static void
usb_mid_create_children(usb_mid_t * usb_mid)9177c478bd9Sstevel@tonic-gate usb_mid_create_children(usb_mid_t *usb_mid)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
920d73ae94eSgc 	uint_t			n_ifs, if_count;
921d73ae94eSgc 	uint_t			i, j;
922d73ae94eSgc 	dev_info_t		*cdip, *ia_dip;
9237c478bd9Sstevel@tonic-gate 	uint_t			ugen_bound = 0;
9247c478bd9Sstevel@tonic-gate 	uint_t			bound_children = 0;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(usb_mid->mi_dip);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
9297c478bd9Sstevel@tonic-gate 	    "usb_mid_attach_child_drivers: port = %d, address = %d",
9307c478bd9Sstevel@tonic-gate 	    usba_device->usb_port, usba_device->usb_addr);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	if (usb_mid->mi_removed_children) {
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 			return;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	n_ifs = usb_mid->mi_n_ifs;
938d73ae94eSgc 	if_count = 1;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_ATTA, usb_mid->mi_log_handle,
9417c478bd9Sstevel@tonic-gate 	    "usb_mid_create_children: #interfaces = %d", n_ifs);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	/*
9447c478bd9Sstevel@tonic-gate 	 * create all children if not already present
9457c478bd9Sstevel@tonic-gate 	 */
946d73ae94eSgc 	for (i = 0; i < n_ifs; i += if_count) {
947d73ae94eSgc 
948d73ae94eSgc 		/* ignore since this if is included by an ia */
949d73ae94eSgc 		if (usb_mid->mi_children_ifs[i] == 0) {
950d73ae94eSgc 
951d73ae94eSgc 			continue;
952d73ae94eSgc 		}
953d73ae94eSgc 
9547c478bd9Sstevel@tonic-gate 		if (usb_mid->mi_children_dips[i] != NULL) {
9554610e4a0Sfrits 			if (i_ddi_node_state(
956112116d8Sfb 			    usb_mid->mi_children_dips[i]) >=
957112116d8Sfb 			    DS_BOUND) {
9584610e4a0Sfrits 					bound_children++;
9594610e4a0Sfrits 			}
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 			continue;
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
965d73ae94eSgc 		ia_dip = usba_ready_interface_association_node(usb_mid->mi_dip,
966d73ae94eSgc 		    i, &if_count);
967d73ae94eSgc 
968d73ae94eSgc 		if (ia_dip != NULL) {
969d73ae94eSgc 			if (usba_bind_driver(ia_dip) == USB_SUCCESS) {
970d73ae94eSgc 				bound_children++;
971d73ae94eSgc 				if (strcmp(ddi_driver_name(ia_dip),
972d73ae94eSgc 				    "ugen") == 0) {
973d73ae94eSgc 					ugen_bound++;
974d73ae94eSgc 				}
975d73ae94eSgc 			}
976d73ae94eSgc 
977d73ae94eSgc 			/*
978d73ae94eSgc 			 * IA node owns if_count interfaces.
979d73ae94eSgc 			 * The rest interfaces own none.
980d73ae94eSgc 			 */
981d73ae94eSgc 			mutex_enter(&usb_mid->mi_mutex);
982d73ae94eSgc 			usb_mid->mi_children_dips[i] = ia_dip;
983d73ae94eSgc 			usb_mid->mi_children_ifs[i] = if_count;
984d73ae94eSgc 			for (j = i + 1; j < i + if_count; j++) {
985d73ae94eSgc 				usb_mid->mi_children_ifs[j] = 0;
986d73ae94eSgc 			}
987d73ae94eSgc 
988d73ae94eSgc 			continue;
989d73ae94eSgc 		}
990d73ae94eSgc 
9917c478bd9Sstevel@tonic-gate 		cdip = usba_ready_interface_node(usb_mid->mi_dip, i);
992d73ae94eSgc 
9937c478bd9Sstevel@tonic-gate 		if (cdip != NULL) {
994d73ae94eSgc 			if (usba_bind_driver(cdip) ==
9957c478bd9Sstevel@tonic-gate 			    USB_SUCCESS) {
9967c478bd9Sstevel@tonic-gate 				bound_children++;
9977c478bd9Sstevel@tonic-gate 				if (strcmp(ddi_driver_name(cdip),
9987c478bd9Sstevel@tonic-gate 				    "ugen") == 0) {
9997c478bd9Sstevel@tonic-gate 					ugen_bound++;
10007c478bd9Sstevel@tonic-gate 				}
10017c478bd9Sstevel@tonic-gate 			}
10027c478bd9Sstevel@tonic-gate 
1003d73ae94eSgc 			/*
1004d73ae94eSgc 			 * interface node owns 1 interface always.
1005d73ae94eSgc 			 */
1006d73ae94eSgc 			mutex_enter(&usb_mid->mi_mutex);
10077c478bd9Sstevel@tonic-gate 			usb_mid->mi_children_dips[i] = cdip;
1008d73ae94eSgc 			usb_mid->mi_children_ifs[i] = 1;
1009d73ae94eSgc 			mutex_exit(&usb_mid->mi_mutex);
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 		}
1012d73ae94eSgc 
1013d73ae94eSgc 		mutex_enter(&usb_mid->mi_mutex);
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	usb_mid->mi_removed_children = (bound_children ? B_FALSE : B_TRUE);
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	/*
10197c478bd9Sstevel@tonic-gate 	 * if there are no ugen interface children, create ugen support at
10207c478bd9Sstevel@tonic-gate 	 * device level, use a separate thread because we may be at interrupt
10217c478bd9Sstevel@tonic-gate 	 * level
10227c478bd9Sstevel@tonic-gate 	 */
10237c478bd9Sstevel@tonic-gate 	if ((ugen_bound == 0) && (usb_mid->mi_ugen_hdl == NULL)) {
10247c478bd9Sstevel@tonic-gate 		/*
10257c478bd9Sstevel@tonic-gate 		 * we only need to remove the children if there are
10267c478bd9Sstevel@tonic-gate 		 * multiple configurations which would fail if there
10277c478bd9Sstevel@tonic-gate 		 * are child interfaces
10287c478bd9Sstevel@tonic-gate 		 */
10297c478bd9Sstevel@tonic-gate 		if ((usb_mid->mi_removed_children == B_FALSE) &&
10307c478bd9Sstevel@tonic-gate 		    (usba_device->usb_n_cfgs > 1)) {
10317c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_ATTA,
10327c478bd9Sstevel@tonic-gate 			    usb_mid->mi_log_handle,
10337c478bd9Sstevel@tonic-gate 			    "can't support ugen for multiple "
10347c478bd9Sstevel@tonic-gate 			    "configurations devices that have attached "
10357c478bd9Sstevel@tonic-gate 			    "child interface drivers");
10367c478bd9Sstevel@tonic-gate 		} else {
10377c478bd9Sstevel@tonic-gate 			usb_mid_ugen_attach(usb_mid,
10387c478bd9Sstevel@tonic-gate 			    usb_mid->mi_removed_children);
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * event support
10467c478bd9Sstevel@tonic-gate  */
10477c478bd9Sstevel@tonic-gate static int
usb_mid_busop_get_eventcookie(dev_info_t * dip,dev_info_t * rdip,char * eventname,ddi_eventcookie_t * cookie)10487c478bd9Sstevel@tonic-gate usb_mid_busop_get_eventcookie(dev_info_t *dip,
10497c478bd9Sstevel@tonic-gate 	dev_info_t *rdip, char *eventname, ddi_eventcookie_t *cookie)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
10547c478bd9Sstevel@tonic-gate 	    "usb_mid_busop_get_eventcookie: dip=0x%p, rdip=0x%p, "
10557c478bd9Sstevel@tonic-gate 	    "event=%s", (void *)dip, (void *)rdip, eventname);
10567c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
10577c478bd9Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d)",
10587c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
10597c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	/* return event cookie, iblock cookie, and level */
10627c478bd9Sstevel@tonic-gate 	return (ndi_event_retrieve_cookie(usb_mid->mi_ndi_event_hdl,
1063112116d8Sfb 	    rdip, eventname, cookie, NDI_EVENT_NOPASS));
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@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)10687c478bd9Sstevel@tonic-gate usb_mid_busop_add_eventcall(dev_info_t *dip,
10697c478bd9Sstevel@tonic-gate 	dev_info_t *rdip,
10707c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
10717c478bd9Sstevel@tonic-gate 	void (*callback)(dev_info_t *dip,
10727c478bd9Sstevel@tonic-gate 	    ddi_eventcookie_t cookie, void *arg,
10737c478bd9Sstevel@tonic-gate 	    void *bus_impldata),
10747c478bd9Sstevel@tonic-gate 	void *arg, ddi_callback_id_t *cb_id)
10757c478bd9Sstevel@tonic-gate {
10767c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
10777c478bd9Sstevel@tonic-gate 	int	ifno = usba_get_ifno(rdip);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
10807c478bd9Sstevel@tonic-gate 	    "usb_mid_busop_add_eventcall: dip=0x%p, rdip=0x%p "
10817c478bd9Sstevel@tonic-gate 	    "cookie=0x%p, cb=0x%p, arg=0x%p",
10827c478bd9Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, (void *)callback, arg);
10837c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
10847c478bd9Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
10857c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
10867c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
10877c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl, cookie));
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/* Set flag on children registering events */
10907c478bd9Sstevel@tonic-gate 	switch (ndi_event_cookie_to_tag(usb_mid->mi_ndi_event_hdl, cookie)) {
10917c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_HOT_REMOVAL:
10927c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
10937c478bd9Sstevel@tonic-gate 		usb_mid->mi_child_events[ifno] |=
10947c478bd9Sstevel@tonic-gate 		    USB_MID_CHILD_EVENT_DISCONNECT;
10957c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 		break;
10987c478bd9Sstevel@tonic-gate 	case USBA_EVENT_TAG_PRE_SUSPEND:
10997c478bd9Sstevel@tonic-gate 		mutex_enter(&usb_mid->mi_mutex);
11007c478bd9Sstevel@tonic-gate 		usb_mid->mi_child_events[ifno] |=
11017c478bd9Sstevel@tonic-gate 		    USB_MID_CHILD_EVENT_PRESUSPEND;
11027c478bd9Sstevel@tonic-gate 		mutex_exit(&usb_mid->mi_mutex);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 		break;
11057c478bd9Sstevel@tonic-gate 	default:
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		break;
11087c478bd9Sstevel@tonic-gate 	}
11097c478bd9Sstevel@tonic-gate 	/* add callback (perform registration) */
11107c478bd9Sstevel@tonic-gate 	return (ndi_event_add_callback(usb_mid->mi_ndi_event_hdl,
1111112116d8Sfb 	    rdip, cookie, callback, arg, NDI_SLEEP, cb_id));
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate static int
usb_mid_busop_remove_eventcall(dev_info_t * dip,ddi_callback_id_t cb_id)11167c478bd9Sstevel@tonic-gate usb_mid_busop_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
11197c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	ASSERT(cb);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11247c478bd9Sstevel@tonic-gate 	    "usb_mid_busop_remove_eventcall: dip=0x%p, rdip=0x%p "
1125112116d8Sfb 	    "cookie=0x%p", (void *)dip, (void *)cb->ndi_evtcb_dip,
1126112116d8Sfb 	    (void *)cb->ndi_evtcb_cookie);
11277c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11287c478bd9Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
11297c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
11307c478bd9Sstevel@tonic-gate 	    ddi_driver_name(cb->ndi_evtcb_dip),
11317c478bd9Sstevel@tonic-gate 	    ddi_get_instance(cb->ndi_evtcb_dip),
11327c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(usb_mid->mi_ndi_event_hdl,
11337c478bd9Sstevel@tonic-gate 	    cb->ndi_evtcb_cookie));
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	/* remove event registration from our event set */
11367c478bd9Sstevel@tonic-gate 	return (ndi_event_remove_callback(usb_mid->mi_ndi_event_hdl, cb_id));
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate static int
usb_mid_busop_post_event(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void * bus_impldata)11417c478bd9Sstevel@tonic-gate usb_mid_busop_post_event(dev_info_t *dip,
11427c478bd9Sstevel@tonic-gate 	dev_info_t *rdip,
11437c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
11447c478bd9Sstevel@tonic-gate 	void *bus_impldata)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	usb_mid_t  *usb_mid = usb_mid_obtain_state(dip);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11497c478bd9Sstevel@tonic-gate 	    "usb_mid_busop_post_event: dip=0x%p, rdip=0x%p "
11507c478bd9Sstevel@tonic-gate 	    "cookie=0x%p, impl=0x%p",
11517c478bd9Sstevel@tonic-gate 	    (void *)dip, (void *)rdip, (void *)cookie, bus_impldata);
11527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_EVENTS, usb_mid->mi_log_handle,
11537c478bd9Sstevel@tonic-gate 	    "(dip=%s%d rdip=%s%d event=%s)",
11547c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip),
11557c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
11567c478bd9Sstevel@tonic-gate 	    ndi_event_cookie_to_name(