xref: /illumos-gate/usr/src/uts/common/io/usb/usba/usba.c (revision fa9e4066)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * USBA: Solaris USB Architecture support
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate #define	USBA_FRAMEWORK
337c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
347c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
357c478bd9Sstevel@tonic-gate #include <sys/usb/hubd/hub.h>
367c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static int usba_str_startcmp(char *, char *);
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * USBA private variables and tunables
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate static kmutex_t	usba_mutex;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * ddivs forced binding:
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  *    usbc usbc_xhubs usbc_xaddress  node name
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  *	0	x	x	class name or "device"
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *	1	0	0	ddivs_usbc
537c478bd9Sstevel@tonic-gate  *	1	0	>1	ddivs_usbc except device
547c478bd9Sstevel@tonic-gate  *				at usbc_xaddress
557c478bd9Sstevel@tonic-gate  *	1	1	0	ddivs_usbc except hubs
567c478bd9Sstevel@tonic-gate  *	1	1	>1	ddivs_usbc except hubs and
577c478bd9Sstevel@tonic-gate  *				device at usbc_xaddress
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate uint_t usba_ddivs_usbc;
607c478bd9Sstevel@tonic-gate uint_t usba_ddivs_usbc_xhubs;
617c478bd9Sstevel@tonic-gate uint_t usba_ddivs_usbc_xaddress;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate uint_t usba_ugen_force_binding;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * compatible name handling
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define	USBA_MAX_COMPAT_NAMES		15
697c478bd9Sstevel@tonic-gate #define	USBA_MAX_COMPAT_NAME_LEN	64
707c478bd9Sstevel@tonic-gate static char	usba_name[USBA_MAX_COMPAT_NAMES][USBA_MAX_COMPAT_NAME_LEN];
717c478bd9Sstevel@tonic-gate static char	*usba_compatible[USBA_MAX_COMPAT_NAMES];
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate _NOTE(MUTEX_PROTECTS_DATA(usba_mutex, usba_name usba_compatible))
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* double linked list for usba_devices */
767c478bd9Sstevel@tonic-gate usba_list_entry_t	usba_device_list;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate _NOTE(MUTEX_PROTECTS_DATA(usba_mutex, usba_device_list))
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * modload support
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate struct modlmisc modlmisc	= {
867c478bd9Sstevel@tonic-gate 	&mod_miscops,	/* Type	of module */
877c478bd9Sstevel@tonic-gate 	"USBA: USB Architecture 2.0 %I%"
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate struct modlinkage modlinkage = {
917c478bd9Sstevel@tonic-gate 	MODREV_1, (void	*)&modlmisc, NULL
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static usb_log_handle_t	usba_log_handle;
967c478bd9Sstevel@tonic-gate static uint_t		usba_errlevel = USB_LOG_L4;
977c478bd9Sstevel@tonic-gate static uint_t		usba_errmask = (uint_t)-1;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate extern usb_log_handle_t	hubdi_log_handle;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate int
1027c478bd9Sstevel@tonic-gate _init(void)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	int i, rval;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/*
1077c478bd9Sstevel@tonic-gate 	 * usbai providing log support needs to be init'ed first
1087c478bd9Sstevel@tonic-gate 	 * and destroyed last
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	usba_usbai_initialization();
1117c478bd9Sstevel@tonic-gate 	usba_usba_initialization();
1127c478bd9Sstevel@tonic-gate 	usba_usbai_register_initialization();
1137c478bd9Sstevel@tonic-gate 	usba_hcdi_initialization();
1147c478bd9Sstevel@tonic-gate 	usba_hubdi_initialization();
1157c478bd9Sstevel@tonic-gate 	usba_devdb_initialization();
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if ((rval = mod_install(&modlinkage)) != 0) {
1187c478bd9Sstevel@tonic-gate 		usba_devdb_destroy();
1197c478bd9Sstevel@tonic-gate 		usba_hubdi_destroy();
1207c478bd9Sstevel@tonic-gate 		usba_hcdi_destroy();
1217c478bd9Sstevel@tonic-gate 		usba_usbai_register_destroy();
1227c478bd9Sstevel@tonic-gate 		usba_usba_destroy();
1237c478bd9Sstevel@tonic-gate 		usba_usbai_destroy();
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	for (i = 0; i < USBA_MAX_COMPAT_NAMES; i++) {
1277c478bd9Sstevel@tonic-gate 		usba_compatible[i] = usba_name[i];
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	return (rval);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate int
1347c478bd9Sstevel@tonic-gate _fini()
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	int rval;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if ((rval = mod_remove(&modlinkage)) == 0) {
1397c478bd9Sstevel@tonic-gate 		usba_devdb_destroy();
1407c478bd9Sstevel@tonic-gate 		usba_hubdi_destroy();
1417c478bd9Sstevel@tonic-gate 		usba_hcdi_destroy();
1427c478bd9Sstevel@tonic-gate 		usba_usbai_register_destroy();
1437c478bd9Sstevel@tonic-gate 		usba_usba_destroy();
1447c478bd9Sstevel@tonic-gate 		usba_usbai_destroy();
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return (rval);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate int
1517c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * common bus ctl for hcd, usb_mid, and hubd
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate int
1617c478bd9Sstevel@tonic-gate usba_bus_ctl(dev_info_t	*dip,
1627c478bd9Sstevel@tonic-gate 	dev_info_t		*rdip,
1637c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t		op,
1647c478bd9Sstevel@tonic-gate 	void			*arg,
1657c478bd9Sstevel@tonic-gate 	void			*result)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = (dev_info_t *)arg;
1687c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
1697c478bd9Sstevel@tonic-gate 	usba_hcdi_t		*usba_hcdi;
1707c478bd9Sstevel@tonic-gate 	usba_hcdi_ops_t		*usba_hcdi_ops;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, hubdi_log_handle,
1737c478bd9Sstevel@tonic-gate 	    "usba_bus_ctl: %s%d %s%d op=%d", ddi_node_name(rdip),
1747c478bd9Sstevel@tonic-gate 	    ddi_get_instance(rdip), ddi_node_name(dip),
1757c478bd9Sstevel@tonic-gate 	    ddi_get_instance(dip), op);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	switch (op) {
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
1807c478bd9Sstevel@tonic-gate 	{
1817c478bd9Sstevel@tonic-gate 		char *name, compat_name[64], *speed;
1827c478bd9Sstevel@tonic-gate 		usba_device_t	*hub_usba_device;
1837c478bd9Sstevel@tonic-gate 		dev_info_t	*hubdip;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		usba_device = usba_get_usba_device(rdip);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		/* find the parent hub */
1887c478bd9Sstevel@tonic-gate 		hubdip = ddi_get_parent(rdip);
1897c478bd9Sstevel@tonic-gate 		while ((strcmp(ddi_driver_name(hubdip), "hubd") != 0) &&
1907c478bd9Sstevel@tonic-gate 		    !(usba_is_root_hub(hubdip))) {
1917c478bd9Sstevel@tonic-gate 			hubdip = ddi_get_parent(hubdip);
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		hub_usba_device = usba_get_usba_device(hubdip);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		if (usba_device) {
1977c478bd9Sstevel@tonic-gate 			if (usb_owns_device(rdip)) {
1987c478bd9Sstevel@tonic-gate 				(void) snprintf(compat_name,
1997c478bd9Sstevel@tonic-gate 				    sizeof (compat_name),
2007c478bd9Sstevel@tonic-gate 				    "usb%x,%x",
2017c478bd9Sstevel@tonic-gate 				    usba_device->usb_dev_descr->idVendor,
2027c478bd9Sstevel@tonic-gate 				    usba_device->usb_dev_descr->idProduct);
2037c478bd9Sstevel@tonic-gate 			} else {
2047c478bd9Sstevel@tonic-gate 				(void) snprintf(compat_name,
2057c478bd9Sstevel@tonic-gate 				    sizeof (compat_name),
2067c478bd9Sstevel@tonic-gate 				    "usbif%x,%x.config%x.%x",
2077c478bd9Sstevel@tonic-gate 				    usba_device->usb_dev_descr->idVendor,
2087c478bd9Sstevel@tonic-gate 				    usba_device->usb_dev_descr->idProduct,
2097c478bd9Sstevel@tonic-gate 				    usba_device->usb_cfg_value,
2107c478bd9Sstevel@tonic-gate 				    usb_get_if_number(rdip));
2117c478bd9Sstevel@tonic-gate 			}
2127c478bd9Sstevel@tonic-gate 			switch (usba_device->usb_port_status) {
2137c478bd9Sstevel@tonic-gate 			case USBA_HIGH_SPEED_DEV:
2147c478bd9Sstevel@tonic-gate 				speed = "hi speed (USB 2.x)";
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 				break;
2177c478bd9Sstevel@tonic-gate 			case USBA_LOW_SPEED_DEV:
2187c478bd9Sstevel@tonic-gate 				speed = "low speed (USB 1.x)";
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 				break;
2217c478bd9Sstevel@tonic-gate 			case USBA_FULL_SPEED_DEV:
2227c478bd9Sstevel@tonic-gate 			default:
2237c478bd9Sstevel@tonic-gate 				speed = "full speed (USB 1.x)";
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 				break;
2267c478bd9Sstevel@tonic-gate 			}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
2297c478bd9Sstevel@tonic-gate 			    "?USB %x.%x %s (%s) operating at %s on "
2307c478bd9Sstevel@tonic-gate 			    "USB %x.%x %s hub: "
2317c478bd9Sstevel@tonic-gate 			    "%s@%s, %s%d at bus address %d\n",
2327c478bd9Sstevel@tonic-gate 			    (usba_device->usb_dev_descr->bcdUSB & 0xff00) >> 8,
2337c478bd9Sstevel@tonic-gate 			    usba_device->usb_dev_descr->bcdUSB & 0xff,
2347c478bd9Sstevel@tonic-gate 			    (usb_owns_device(rdip) ? "device" : "interface"),
2357c478bd9Sstevel@tonic-gate 			    compat_name, speed,
2367c478bd9Sstevel@tonic-gate 			    (hub_usba_device->usb_dev_descr->bcdUSB &
2377c478bd9Sstevel@tonic-gate 			    0xff00) >> 8,
2387c478bd9Sstevel@tonic-gate 			    hub_usba_device->usb_dev_descr->bcdUSB & 0xff,
2397c478bd9Sstevel@tonic-gate 			    usba_is_root_hub(hubdip) ? "root" : "external",
2407c478bd9Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_name_addr(rdip),
2417c478bd9Sstevel@tonic-gate 			    ddi_driver_name(rdip),
2427c478bd9Sstevel@tonic-gate 			    ddi_get_instance(rdip), usba_device->usb_addr);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 			name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2457c478bd9Sstevel@tonic-gate 			(void) usba_get_mfg_prod_sn_str(rdip, name, MAXNAMELEN);
2467c478bd9Sstevel@tonic-gate 			if (name[0] != '\0') {
2477c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "?\t%s\n", name);
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 			kmem_free(name, MAXNAMELEN);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		} else { /* harden USBA against this case; if it happens */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
2547c478bd9Sstevel@tonic-gate 			    "?USB-device: %s@%s, %s%d\n",
2557c478bd9Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_name_addr(rdip),
2567c478bd9Sstevel@tonic-gate 			    ddi_driver_name(rdip), ddi_get_instance(rdip));
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
2637c478bd9Sstevel@tonic-gate 	{
2647c478bd9Sstevel@tonic-gate 		int			usb_addr;
2657c478bd9Sstevel@tonic-gate 		uint_t			n;
2667c478bd9Sstevel@tonic-gate 		char			name[32];
2677c478bd9Sstevel@tonic-gate 		int			*data;
2687c478bd9Sstevel@tonic-gate 		int			rval;
2697c478bd9Sstevel@tonic-gate 		int			len = sizeof (usb_addr);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		usba_hcdi	= usba_hcdi_get_hcdi(dip);
2727c478bd9Sstevel@tonic-gate 		usba_hcdi_ops	= usba_hcdi->hcdi_ops;
2737c478bd9Sstevel@tonic-gate 		ASSERT(usba_hcdi_ops != NULL);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		/*
2767c478bd9Sstevel@tonic-gate 		 * as long as the dip exists, it should have
2777c478bd9Sstevel@tonic-gate 		 * usba_device structure associated with it
2787c478bd9Sstevel@tonic-gate 		 */
2797c478bd9Sstevel@tonic-gate 		usba_device = usba_get_usba_device(child_dip);
2807c478bd9Sstevel@tonic-gate 		if (usba_device == NULL) {
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
2837c478bd9Sstevel@tonic-gate 			    "usba_bus_ctl: DDI_NOT_WELL_FORMED (%s (0x%p))",
2847c478bd9Sstevel@tonic-gate 			    ddi_node_name(child_dip), (void *)child_dip);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		/* the dip should have an address and reg property */
2907c478bd9Sstevel@tonic-gate 		if (ddi_prop_op(DDI_DEV_T_NONE, child_dip, PROP_LEN_AND_VAL_BUF,
2917c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS |	DDI_PROP_CANSLEEP, "assigned-address",
2927c478bd9Sstevel@tonic-gate 		    (caddr_t)&usb_addr,	&len) != DDI_SUCCESS) {
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, hubdi_log_handle,
2957c478bd9Sstevel@tonic-gate 			    "usba_bus_ctl:\n\t"
2967c478bd9Sstevel@tonic-gate 			    "%s%d %s%d op=%d rdip = 0x%p dip = 0x%p",
2977c478bd9Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_instance(rdip),
2987c478bd9Sstevel@tonic-gate 			    ddi_node_name(dip), ddi_get_instance(dip), op,
2997c478bd9Sstevel@tonic-gate 			    rdip, dip);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, hubdi_log_handle,
3027c478bd9Sstevel@tonic-gate 			    "usba_bus_ctl: DDI_NOT_WELL_FORMED (%s (0x%p))",
3037c478bd9Sstevel@tonic-gate 			    ddi_node_name(child_dip), (void *)child_dip);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		if ((rval = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child_dip,
3097c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg",
3107c478bd9Sstevel@tonic-gate 		    &data, &n)) != DDI_SUCCESS) {
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, hubdi_log_handle,
3137c478bd9Sstevel@tonic-gate 			    "usba_bus_ctl: %d, DDI_NOT_WELL_FORMED", rval);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
3167c478bd9Sstevel@tonic-gate 		}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		/*
3207c478bd9Sstevel@tonic-gate 		 * if the configuration is 1, the unit address is
3217c478bd9Sstevel@tonic-gate 		 * just the interface number
3227c478bd9Sstevel@tonic-gate 		 */
3237c478bd9Sstevel@tonic-gate 		if ((n == 1) || ((n > 1) && (data[1] == 1))) {
3247c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "%x", data[0]);
3257c478bd9Sstevel@tonic-gate 		} else {
3267c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "%x,%x", data[0], data[1]);
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA,
3307c478bd9Sstevel@tonic-gate 		    hubdi_log_handle, "usba_bus_ctl: name = %s", name);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		ddi_prop_free(data);
3337c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child_dip, name);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		/*
3367c478bd9Sstevel@tonic-gate 		 * increment the reference count for each child using this
3377c478bd9Sstevel@tonic-gate 		 * usba_device structure
3387c478bd9Sstevel@tonic-gate 		 */
3397c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
3407c478bd9Sstevel@tonic-gate 		usba_device->usb_ref_count++;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, hubdi_log_handle,
3437c478bd9Sstevel@tonic-gate 		    "usba_bus_ctl: init usba_device = 0x%p ref_count = %d",
3447c478bd9Sstevel@tonic-gate 		    (void *)usba_device, usba_device->usb_ref_count);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
3527c478bd9Sstevel@tonic-gate 	{
3537c478bd9Sstevel@tonic-gate 		usba_device = usba_get_usba_device(child_dip);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		if (usba_device != NULL) {
3567c478bd9Sstevel@tonic-gate 			/*
3577c478bd9Sstevel@tonic-gate 			 * decrement the reference count for each child
3587c478bd9Sstevel@tonic-gate 			 * using this  usba_device structure
3597c478bd9Sstevel@tonic-gate 			 */
3607c478bd9Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
3617c478bd9Sstevel@tonic-gate 			usba_device->usb_ref_count--;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_USBA, hubdi_log_handle,
3647c478bd9Sstevel@tonic-gate 			    "usba_hcdi_bus_ctl: uninit usba_device=0x%p "
3657c478bd9Sstevel@tonic-gate 			    "ref_count=%d",
3667c478bd9Sstevel@tonic-gate 			    usba_device, usba_device->usb_ref_count);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child_dip, NULL);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
3767c478bd9Sstevel@tonic-gate 		/* Do nothing */
3777c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * These ops correspond	to functions that "shouldn't" be called
3817c478bd9Sstevel@tonic-gate 	 * by a	USB client driver.  So	we whine when we're called.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_DMAPMAPC:
3847c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTINT:
3857c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
3867c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
3877c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
3887c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
3897c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
3907c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
3917c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:
3927c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d:	invalid	op (%d)	from %s%d",
3937c478bd9Sstevel@tonic-gate 			ddi_node_name(dip), ddi_get_instance(dip),
3947c478bd9Sstevel@tonic-gate 			op, ddi_node_name(rdip), ddi_get_instance(rdip));
3957c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	default:
4017c478bd9Sstevel@tonic-gate 		return (ddi_ctlops(dip,	rdip, op, arg, result));
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate  * initialize and destroy USBA module
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate void
4107c478bd9Sstevel@tonic-gate usba_usba_initialization()
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	usba_log_handle = usb_alloc_log_hdl(NULL, "usba", &usba_errlevel,
4137c478bd9Sstevel@tonic-gate 				&usba_errmask, NULL, 0);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA,
4167c478bd9Sstevel@tonic-gate 	    usba_log_handle, "usba_usba_initialization");
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	mutex_init(&usba_mutex, NULL, MUTEX_DRIVER, NULL);
4197c478bd9Sstevel@tonic-gate 	usba_init_list(&usba_device_list, NULL, NULL);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate void
4247c478bd9Sstevel@tonic-gate usba_usba_destroy()
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle, "usba_usba_destroy");
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	mutex_destroy(&usba_mutex);
4297c478bd9Sstevel@tonic-gate 	usba_destroy_list(&usba_device_list);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(usba_log_handle);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate /*
4367c478bd9Sstevel@tonic-gate  * usba_set_usb_address:
4377c478bd9Sstevel@tonic-gate  *	set usb address in usba_device structure
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate int
4407c478bd9Sstevel@tonic-gate usba_set_usb_address(usba_device_t *usba_device)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	usb_addr_t address;
4437c478bd9Sstevel@tonic-gate 	uchar_t s = 8;
4447c478bd9Sstevel@tonic-gate 	usba_hcdi_t *hcdi;
4457c478bd9Sstevel@tonic-gate 	char *usb_address_in_use;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	mutex_enter(&hcdi->hcdi_mutex);
4527c478bd9Sstevel@tonic-gate 	usb_address_in_use = hcdi->hcdi_usb_address_in_use;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	for (address = ROOT_HUB_ADDR + 1;
4557c478bd9Sstevel@tonic-gate 	    address <= USBA_MAX_ADDRESS; address++) {
4567c478bd9Sstevel@tonic-gate 		if (usb_address_in_use[address/s] & (1 << (address % s))) {
4577c478bd9Sstevel@tonic-gate 			continue;
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		usb_address_in_use[address/s] |= (1 << (address % s));
4607c478bd9Sstevel@tonic-gate 		hcdi->hcdi_device_count++;
4617c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->hcdi_device_count.value.ui64++;
4627c478bd9Sstevel@tonic-gate 		mutex_exit(&hcdi->hcdi_mutex);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
4657c478bd9Sstevel@tonic-gate 		    "usba_set_usb_address: %d", address);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		usba_device->usb_addr = address;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	usba_device->usb_addr = 0;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
4777c478bd9Sstevel@tonic-gate 	    "no usb address available");
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	mutex_exit(&hcdi->hcdi_mutex);
4807c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * usba_unset_usb_address:
4887c478bd9Sstevel@tonic-gate  *	unset usb_address in usba_device structure
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate void
4917c478bd9Sstevel@tonic-gate usba_unset_usb_address(usba_device_t *usba_device)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	usb_addr_t address;
4947c478bd9Sstevel@tonic-gate 	usba_hcdi_t *hcdi;
4957c478bd9Sstevel@tonic-gate 	uchar_t s = 8;
4967c478bd9Sstevel@tonic-gate 	char *usb_address_in_use;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
4997c478bd9Sstevel@tonic-gate 	address = usba_device->usb_addr;
5007c478bd9Sstevel@tonic-gate 	hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (address > ROOT_HUB_ADDR) {
5037c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
5047c478bd9Sstevel@tonic-gate 		    "usba_unset_usb_address: address=%d", address);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		mutex_enter(&hcdi->hcdi_mutex);
5077c478bd9Sstevel@tonic-gate 		usb_address_in_use = hcdi->hcdi_usb_address_in_use;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		ASSERT(usb_address_in_use[address/s] & (1 << (address % s)));
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		usb_address_in_use[address/s] &= ~(1 << (address % s));
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		hcdi->hcdi_device_count--;
5147c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->hcdi_device_count.value.ui64--;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		mutex_exit(&hcdi->hcdi_mutex);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		usba_device->usb_addr = 0;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate struct usba_evdata *
5257c478bd9Sstevel@tonic-gate usba_get_evdata(dev_info_t *dip)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	usba_evdata_t *evdata;
5287c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/* called when dip attaches */
5317c478bd9Sstevel@tonic-gate 	ASSERT(usba_device != NULL);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
5347c478bd9Sstevel@tonic-gate 	evdata = usba_device->usb_evdata;
5357c478bd9Sstevel@tonic-gate 	while (evdata) {
5367c478bd9Sstevel@tonic-gate 		if (evdata->ev_dip == dip) {
5377c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 			return (evdata);
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 		evdata = evdata->ev_next;
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	evdata = kmem_zalloc(sizeof (usba_evdata_t), KM_SLEEP);
5457c478bd9Sstevel@tonic-gate 	evdata->ev_dip = dip;
5467c478bd9Sstevel@tonic-gate 	evdata->ev_next = usba_device->usb_evdata;
5477c478bd9Sstevel@tonic-gate 	usba_device->usb_evdata = evdata;
5487c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	return (evdata);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * allocate a usb device structure and link it in the list
5567c478bd9Sstevel@tonic-gate  */
5577c478bd9Sstevel@tonic-gate usba_device_t *
5587c478bd9Sstevel@tonic-gate usba_alloc_usba_device(dev_info_t *root_hub_dip)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
5617c478bd9Sstevel@tonic-gate 	int		ep_idx;
5627c478bd9Sstevel@tonic-gate 	ddi_iblock_cookie_t iblock_cookie =
5637c478bd9Sstevel@tonic-gate 	    usba_hcdi_get_hcdi(root_hub_dip)->hcdi_iblock_cookie;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 * create a new usba_device structure
5677c478bd9Sstevel@tonic-gate 	 */
5687c478bd9Sstevel@tonic-gate 	usba_device = kmem_zalloc(sizeof (usba_device_t), KM_SLEEP);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/*
5717c478bd9Sstevel@tonic-gate 	 * initialize usba_device
5727c478bd9Sstevel@tonic-gate 	 */
5737c478bd9Sstevel@tonic-gate 	mutex_init(&usba_device->usb_mutex, NULL, MUTEX_DRIVER,
5747c478bd9Sstevel@tonic-gate 							iblock_cookie);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	usba_init_list(&usba_device->usb_device_list, (usb_opaque_t)usba_device,
5777c478bd9Sstevel@tonic-gate 							iblock_cookie);
5787c478bd9Sstevel@tonic-gate 	usba_init_list(&usba_device->usb_allocated, (usb_opaque_t)usba_device,
5797c478bd9Sstevel@tonic-gate 							iblock_cookie);
5807c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
5817c478bd9Sstevel@tonic-gate 	usba_device->usb_root_hub_dip = root_hub_dip;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * add to list of usba_devices
5857c478bd9Sstevel@tonic-gate 	 */
5867c478bd9Sstevel@tonic-gate 	usba_add_to_list(&usba_device_list, &usba_device->usb_device_list);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/* init mutex in each usba_ph_impl structure */
5897c478bd9Sstevel@tonic-gate 	for (ep_idx = 0; ep_idx < USBA_N_ENDPOINTS; ep_idx++) {
5907c478bd9Sstevel@tonic-gate 		mutex_init(&usba_device->usb_ph_list[ep_idx].usba_ph_mutex,
5917c478bd9Sstevel@tonic-gate 		    NULL, MUTEX_DRIVER, iblock_cookie);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
5957c478bd9Sstevel@tonic-gate 	    "allocated usba_device 0x%p", (void *)usba_device);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	return (usba_device);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate /* free NDI event data associated with usba_device */
6047c478bd9Sstevel@tonic-gate void
6057c478bd9Sstevel@tonic-gate usba_free_evdata(usba_evdata_t *evdata)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	usba_evdata_t *next;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	while (evdata) {
6107c478bd9Sstevel@tonic-gate 		next = evdata->ev_next;
6117c478bd9Sstevel@tonic-gate 		kmem_free(evdata, sizeof (usba_evdata_t));
6127c478bd9Sstevel@tonic-gate 		evdata = next;
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate /*
6187c478bd9Sstevel@tonic-gate  * free usb device structure
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate void
6217c478bd9Sstevel@tonic-gate usba_free_usba_device(usba_device_t *usba_device)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	int			i, ep_idx;
6247c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	def_ph;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if (usba_device == NULL) {
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		return;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
6327c478bd9Sstevel@tonic-gate 	if (usba_device->usb_ref_count) {
6337c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 		return;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
6397c478bd9Sstevel@tonic-gate 	    "usba_free_usba_device 0x%p, address=0x%x, ref cnt=%d",
6407c478bd9Sstevel@tonic-gate 	    (void *)usba_device, usba_device->usb_addr,
6417c478bd9Sstevel@tonic-gate 	    usba_device->usb_ref_count);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	usba_free_evdata(usba_device->usb_evdata);
6447c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	def_ph = usba_usbdev_to_dflt_pipe_handle(usba_device);
6477c478bd9Sstevel@tonic-gate 	if (def_ph != NULL) {
6487c478bd9Sstevel@tonic-gate 		usba_pipe_handle_data_t	*ph_data = usba_get_ph_data(def_ph);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		if (ph_data) {
6517c478bd9Sstevel@tonic-gate 			usb_pipe_close(ph_data->p_dip, def_ph,
6527c478bd9Sstevel@tonic-gate 			    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED,
6537c478bd9Sstevel@tonic-gate 			    NULL, NULL);
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_mutex);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/* destroy mutex in each usba_ph_impl structure */
6607c478bd9Sstevel@tonic-gate 	for (ep_idx = 0; ep_idx < USBA_N_ENDPOINTS; ep_idx++) {
6617c478bd9Sstevel@tonic-gate 		mutex_destroy(&usba_device->usb_ph_list[ep_idx].usba_ph_mutex);
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	(void) usba_rm_from_list(&usba_device_list,
6657c478bd9Sstevel@tonic-gate 				&usba_device->usb_device_list);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_mutex);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	usba_destroy_list(&usba_device->usb_device_list);
6707c478bd9Sstevel@tonic-gate 	usba_destroy_list(&usba_device->usb_allocated);
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
6737c478bd9Sstevel@tonic-gate 	    "deallocating usba_device = 0x%p, address = 0x%x",
6747c478bd9Sstevel@tonic-gate 	    (void *)usba_device, usba_device->usb_addr);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/*
6777c478bd9Sstevel@tonic-gate 	 * ohci allocates descriptors for root hub so we can't
6787c478bd9Sstevel@tonic-gate 	 * deallocate these here
6797c478bd9Sstevel@tonic-gate 	 */
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if (usba_device->usb_addr != ROOT_HUB_ADDR) {
6827c478bd9Sstevel@tonic-gate 		if (usba_device->usb_cfg_array) {
6837c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
6847c478bd9Sstevel@tonic-gate 			    "deallocating usb_config_array: 0x%p",
6857c478bd9Sstevel@tonic-gate 			    usba_device->usb_cfg_array);
6867c478bd9Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
6877c478bd9Sstevel@tonic-gate 			for (i = 0;
6887c478bd9Sstevel@tonic-gate 			    i < usba_device->usb_dev_descr->bNumConfigurations;
6897c478bd9Sstevel@tonic-gate 			    i++) {
6907c478bd9Sstevel@tonic-gate 				if (usba_device->usb_cfg_array[i]) {
6917c478bd9Sstevel@tonic-gate 					kmem_free(
6927c478bd9Sstevel@tonic-gate 					    usba_device->usb_cfg_array[i],
6937c478bd9Sstevel@tonic-gate 					    usba_device->usb_cfg_array_len[i]);
6947c478bd9Sstevel@tonic-gate 				}
6957c478bd9Sstevel@tonic-gate 			}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 			/* free the array pointers */
6987c478bd9Sstevel@tonic-gate 			kmem_free(usba_device->usb_cfg_array,
6997c478bd9Sstevel@tonic-gate 			    usba_device->usb_cfg_array_length);
7007c478bd9Sstevel@tonic-gate 			kmem_free(usba_device->usb_cfg_array_len,
7017c478bd9Sstevel@tonic-gate 			    usba_device->usb_cfg_array_len_length);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		if (usba_device->usb_cfg_str_descr) {
7077c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
7087c478bd9Sstevel@tonic-gate 			    "deallocating usb_cfg_str_descr: 0x%p",
7097c478bd9Sstevel@tonic-gate 			    usba_device->usb_cfg_str_descr);
7107c478bd9Sstevel@tonic-gate 			for (i = 0;
7117c478bd9Sstevel@tonic-gate 			    i < usba_device->usb_dev_descr->bNumConfigurations;
7127c478bd9Sstevel@tonic-gate 			    i++) {
7137c478bd9Sstevel@tonic-gate 				if (usba_device->usb_cfg_str_descr[i]) {
7147c478bd9Sstevel@tonic-gate 					kmem_free(
7157c478bd9Sstevel@tonic-gate 					    usba_device->usb_cfg_str_descr[i],
7167c478bd9Sstevel@tonic-gate 					    strlen(usba_device->
7177c478bd9Sstevel@tonic-gate 						usb_cfg_str_descr[i]) + 1);
7187c478bd9Sstevel@tonic-gate 				}
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 			/* free the array pointers */
7217c478bd9Sstevel@tonic-gate 			kmem_free(usba_device->usb_cfg_str_descr,
7227c478bd9Sstevel@tonic-gate 			    sizeof (uchar_t *) * usba_device->usb_n_cfgs);
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		if (usba_device->usb_dev_descr) {
7267c478bd9Sstevel@tonic-gate 			kmem_free(usba_device->usb_dev_descr,
7277c478bd9Sstevel@tonic-gate 				    sizeof (usb_dev_descr_t));
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		if (usba_device->usb_mfg_str) {
7317c478bd9Sstevel@tonic-gate 			kmem_free(usba_device->usb_mfg_str,
7327c478bd9Sstevel@tonic-gate 				strlen(usba_device->usb_mfg_str) + 1);
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		if (usba_device->usb_product_str) {
7367c478bd9Sstevel@tonic-gate 			kmem_free(usba_device->usb_product_str,
7377c478bd9Sstevel@tonic-gate 				strlen(usba_device->usb_product_str) + 1);
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		if (usba_device->usb_serialno_str) {
7417c478bd9Sstevel@tonic-gate 			kmem_free(usba_device->usb_serialno_str,
7427c478bd9Sstevel@tonic-gate 				strlen(usba_device->usb_serialno_str) + 1);
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 		usba_unset_usb_address(usba_device);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate #ifndef __lock_lint
7497c478bd9Sstevel@tonic-gate 	ASSERT(usba_device->usb_client_dev_data_list.cddl_next == NULL);
7507c478bd9Sstevel@tonic-gate #endif
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if (usba_device->usb_client_flags) {
7537c478bd9Sstevel@tonic-gate #ifndef __lock_lint
7547c478bd9Sstevel@tonic-gate 		int i;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		for (i = 0; i < usba_device->usb_n_ifs; i++) {
7577c478bd9Sstevel@tonic-gate 			ASSERT(usba_device->usb_client_flags[i] == 0);
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate #endif
7607c478bd9Sstevel@tonic-gate 		kmem_free(usba_device->usb_client_flags,
7617c478bd9Sstevel@tonic-gate 		    usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if (usba_device->usb_client_attach_list) {
7667c478bd9Sstevel@tonic-gate 		kmem_free(usba_device->usb_client_attach_list,
7677c478bd9Sstevel@tonic-gate 		    usba_device->usb_n_ifs *
7687c478bd9Sstevel@tonic-gate 		    sizeof (*usba_device->usb_client_attach_list));
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	if (usba_device->usb_client_ev_cb_list) {
7717c478bd9Sstevel@tonic-gate 		kmem_free(usba_device->usb_client_ev_cb_list,
7727c478bd9Sstevel@tonic-gate 		    usba_device->usb_n_ifs *
7737c478bd9Sstevel@tonic-gate 		    sizeof (*usba_device->usb_client_ev_cb_list));
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * finally ready to destroy the structure
7787c478bd9Sstevel@tonic-gate 	 */
7797c478bd9Sstevel@tonic-gate 	mutex_destroy(&usba_device->usb_mutex);
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	kmem_free((caddr_t)usba_device, sizeof (usba_device_t));
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate /* clear the data toggle for all endpoints on this device */
7867c478bd9Sstevel@tonic-gate void
7877c478bd9Sstevel@tonic-gate usba_clear_data_toggle(usba_device_t *usba_device)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate 	int	i;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	if (usba_device != NULL) {
7927c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
7937c478bd9Sstevel@tonic-gate 		for (i = 0; i < USBA_N_ENDPOINTS; i++) {
7947c478bd9Sstevel@tonic-gate 			usba_device->usb_ph_list[i].usba_ph_flags &=
7957c478bd9Sstevel@tonic-gate 			    ~USBA_PH_DATA_TOGGLE;
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate  * usba_create_child_devi():
8047c478bd9Sstevel@tonic-gate  *	create a child devinfo node, usba_device, attach properties.
8057c478bd9Sstevel@tonic-gate  *	the usba_device structure is shared between all interfaces
8067c478bd9Sstevel@tonic-gate  */
8077c478bd9Sstevel@tonic-gate int
8087c478bd9Sstevel@tonic-gate usba_create_child_devi(dev_info_t	*dip,
8097c478bd9Sstevel@tonic-gate 		char			*node_name,
8107c478bd9Sstevel@tonic-gate 		usba_hcdi_ops_t		*usba_hcdi_ops,
8117c478bd9Sstevel@tonic-gate 		dev_info_t		*usb_root_hub_dip,
8127c478bd9Sstevel@tonic-gate 		usb_port_status_t	port_status,
8137c478bd9Sstevel@tonic-gate 		usba_device_t		*usba_device,
8147c478bd9Sstevel@tonic-gate 		dev_info_t		**child_dip)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	int rval = USB_FAILURE;
8177c478bd9Sstevel@tonic-gate 	int usba_device_allocated = 0;
8187c478bd9Sstevel@tonic-gate 	usb_addr_t	address;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
8217c478bd9Sstevel@tonic-gate 	    "usba_create_child_devi: %s usba_device=0x%p "
8227c478bd9Sstevel@tonic-gate 	    "port status=0x%x", node_name,
8237c478bd9Sstevel@tonic-gate 	    (void *)usba_device, port_status);
8247c478bd9Sstevel@tonic-gate 
825*fa9e4066Sahrens 	ndi_devi_alloc_sleep(dip, node_name, (pnode_t)DEVI_SID_NODEID,
8267c478bd9Sstevel@tonic-gate 				child_dip);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
8297c478bd9Sstevel@tonic-gate 	    "child dip=0x%p", *child_dip);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if (usba_device == NULL) {
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		usba_device = usba_alloc_usba_device(usb_root_hub_dip);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		/* grab the mutex to keep warlock happy */
8367c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
8377c478bd9Sstevel@tonic-gate 		usba_device->usb_hcdi_ops	= usba_hcdi_ops;
8387c478bd9Sstevel@tonic-gate 		usba_device->usb_port_status	= port_status;
8397c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		usba_device_allocated++;
8427c478bd9Sstevel@tonic-gate 	} else {
8437c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
8447c478bd9Sstevel@tonic-gate 		if (usba_hcdi_ops) {
8457c478bd9Sstevel@tonic-gate 			ASSERT(usba_device->usb_hcdi_ops == usba_hcdi_ops);
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 		if (usb_root_hub_dip) {
8487c478bd9Sstevel@tonic-gate 			ASSERT(usba_device->usb_root_hub_dip ==
8497c478bd9Sstevel@tonic-gate 						usb_root_hub_dip);
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 		usba_device->usb_port_status	= port_status;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if (usba_device->usb_addr == 0) {
8587c478bd9Sstevel@tonic-gate 		if (usba_set_usb_address(usba_device) == USB_FAILURE) {
8597c478bd9Sstevel@tonic-gate 			address = 0;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
8627c478bd9Sstevel@tonic-gate 				"cannot set usb address for dip=0x%p",
8637c478bd9Sstevel@tonic-gate 				*child_dip);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 			goto fail;
8667c478bd9Sstevel@tonic-gate 		}
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 	address = usba_device->usb_addr;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	/* attach properties */
8717c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, *child_dip,
8727c478bd9Sstevel@tonic-gate 		"assigned-address", address);
8737c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
8747c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
8757c478bd9Sstevel@tonic-gate 			"cannot set usb address property for dip=0x%p",
8767c478bd9Sstevel@tonic-gate 			*child_dip);
8777c478bd9Sstevel@tonic-gate 		rval = USB_FAILURE;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		goto fail;
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/*
8837c478bd9Sstevel@tonic-gate 	 * store the usba_device point in the dip
8847c478bd9Sstevel@tonic-gate 	 */
8857c478bd9Sstevel@tonic-gate 	usba_set_usba_device(*child_dip, usba_device);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
8887c478bd9Sstevel@tonic-gate 	    "usba_create_child_devi: devi=0x%p (%s) ud=0x%p",
8897c478bd9Sstevel@tonic-gate 		*child_dip, ddi_driver_name(*child_dip), usba_device);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate fail:
8947c478bd9Sstevel@tonic-gate 	if (*child_dip) {
8957c478bd9Sstevel@tonic-gate 		int rval = usba_destroy_child_devi(*child_dip, NDI_DEVI_REMOVE);
8967c478bd9Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
8977c478bd9Sstevel@tonic-gate 		*child_dip = NULL;
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	if (usba_device_allocated) {
9017c478bd9Sstevel@tonic-gate 		usba_free_usba_device(usba_device);
9027c478bd9Sstevel@tonic-gate 	} else if (address && usba_device) {
9037c478bd9Sstevel@tonic-gate 		usba_unset_usb_address(usba_device);
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
9077c478bd9Sstevel@tonic-gate 	    "usba_create_child_devi failed: rval=%d", rval);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	return (rval);
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate int
9147c478bd9Sstevel@tonic-gate usba_destroy_child_devi(dev_info_t *dip, uint_t flag)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device;
9177c478bd9Sstevel@tonic-gate 	int		rval = NDI_SUCCESS;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
9207c478bd9Sstevel@tonic-gate 	    "usba_destroy_child_devi: %s%d (0x%p)",
9217c478bd9Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), dip);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * if the child hasn't been bound yet, we can just
9277c478bd9Sstevel@tonic-gate 	 * free the dip
9287c478bd9Sstevel@tonic-gate 	 */
9297c478bd9Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_INITIALIZED) {
9307c478bd9Sstevel@tonic-gate 		/*
9317c478bd9Sstevel@tonic-gate 		 * do not call ndi_devi_free() since it might
9327c478bd9Sstevel@tonic-gate 		 * deadlock
9337c478bd9Sstevel@tonic-gate 		 */
9347c478bd9Sstevel@tonic-gate 		rval = ddi_remove_child(dip, 0);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	} else {
9377c478bd9Sstevel@tonic-gate 		char *devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
9387c478bd9Sstevel@tonic-gate 		dev_info_t *pdip = ddi_get_parent(dip);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		(void) ddi_deviname(dip, devnm);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
9437c478bd9Sstevel@tonic-gate 		    "usba_destroy_child_devi:\n\t"
9447c478bd9Sstevel@tonic-gate 		    "offlining dip 0x%p usba_device=0x%p (%s)", dip,
9457c478bd9Sstevel@tonic-gate 		    (void *)usba_device, devnm);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, DV_CLEAN_FORCE);
9487c478bd9Sstevel@tonic-gate 		rval =	ndi_devi_unconfig_one(pdip, devnm + 1, NULL,
9497c478bd9Sstevel@tonic-gate 		    flag | NDI_UNCONFIG | NDI_DEVI_OFFLINE);
9507c478bd9Sstevel@tonic-gate 		if (rval != NDI_SUCCESS) {
9517c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
9527c478bd9Sstevel@tonic-gate 			    " ndi_devi_unconfig_one %s%d failed (%d)",
9537c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
9547c478bd9Sstevel@tonic-gate 			    rval);
9557c478bd9Sstevel@tonic-gate 		}
9567c478bd9Sstevel@tonic-gate 		kmem_free(devnm, MAXNAMELEN + 1);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
9607c478bd9Sstevel@tonic-gate 	    "usba_destroy_child_devi: rval=%d", rval);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	return (rval == NDI_SUCCESS ? USB_SUCCESS : USB_FAILURE);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate /*
9677c478bd9Sstevel@tonic-gate  * list management
9687c478bd9Sstevel@tonic-gate  */
9697c478bd9Sstevel@tonic-gate void
9707c478bd9Sstevel@tonic-gate usba_init_list(usba_list_entry_t *element, usb_opaque_t private,
9717c478bd9Sstevel@tonic-gate 	ddi_iblock_cookie_t	iblock_cookie)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	mutex_init(&element->list_mutex, NULL, MUTEX_DRIVER,
9747c478bd9Sstevel@tonic-gate 						iblock_cookie);
9757c478bd9Sstevel@tonic-gate 	mutex_enter(&element->list_mutex);
9767c478bd9Sstevel@tonic-gate 	element->private = private;
9777c478bd9Sstevel@tonic-gate 	mutex_exit(&element->list_mutex);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate void
9827c478bd9Sstevel@tonic-gate usba_destroy_list(usba_list_entry_t *head)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
9857c478bd9Sstevel@tonic-gate 	ASSERT(head->next == NULL);
9867c478bd9Sstevel@tonic-gate 	ASSERT(head->prev == NULL);
9877c478bd9Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	mutex_destroy(&head->list_mutex);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate void
9947c478bd9Sstevel@tonic-gate usba_add_to_list(usba_list_entry_t *head, usba_list_entry_t *element)
9957c478bd9Sstevel@tonic-gate {
9967c478bd9Sstevel@tonic-gate 	usba_list_entry_t *next;
9977c478bd9Sstevel@tonic-gate 	int		remaining;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
10007c478bd9Sstevel@tonic-gate 	mutex_enter(&element->list_mutex);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	remaining = head->count;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	/* check if it is not in another list */
10057c478bd9Sstevel@tonic-gate 	ASSERT(element->next == NULL);
10067c478bd9Sstevel@tonic-gate 	ASSERT(element->prev == NULL);
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate #ifdef DEBUG
10097c478bd9Sstevel@tonic-gate 	/*
10107c478bd9Sstevel@tonic-gate 	 * only verify the list when not in interrupt context, we
10117c478bd9Sstevel@tonic-gate 	 * have to trust the HCD
10127c478bd9Sstevel@tonic-gate 	 */
10137c478bd9Sstevel@tonic-gate 	if (!servicing_interrupt()) {
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		/* check if not already in this list */
10167c478bd9Sstevel@tonic-gate 		for (next = head->next; (next != NULL);
10177c478bd9Sstevel@tonic-gate 		    next = next->next) {
10187c478bd9Sstevel@tonic-gate 			if (next == element) {
10197c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L0(DPRINT_MASK_USBA,
10207c478bd9Sstevel@tonic-gate 				    usba_log_handle,
10217c478bd9Sstevel@tonic-gate 				    "Attempt to corrupt USB list at 0x%p",
10227c478bd9Sstevel@tonic-gate 				    (void *)head);
10237c478bd9Sstevel@tonic-gate 				ASSERT(next == element);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 				goto done;
10267c478bd9Sstevel@tonic-gate 			}
10277c478bd9Sstevel@tonic-gate 			remaining--;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 			/*
10307c478bd9Sstevel@tonic-gate 			 * Detect incorrect circ links or found
10317c478bd9Sstevel@tonic-gate 			 * unexpected elements.
10327c478bd9Sstevel@tonic-gate 			 */
10337c478bd9Sstevel@tonic-gate 			if ((next->next && (remaining == 0)) ||
10347c478bd9Sstevel@tonic-gate 			    ((next->next == NULL) && remaining)) {
10357c478bd9Sstevel@tonic-gate 				panic("Corrupted USB list at 0x%p",
10367c478bd9Sstevel@tonic-gate 				    (void *)head);
10377c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
10387c478bd9Sstevel@tonic-gate 			}
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate #endif
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if (head->next == NULL) {
10447c478bd9Sstevel@tonic-gate 		head->prev = head->next = element;
10457c478bd9Sstevel@tonic-gate 	} else {
10467c478bd9Sstevel@tonic-gate 		/* add to tail */
10477c478bd9Sstevel@tonic-gate 		head->prev->next = element;
10487c478bd9Sstevel@tonic-gate 		element->prev = head->prev;
10497c478bd9Sstevel@tonic-gate 		head->prev = element;
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	head->count++;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
10557c478bd9Sstevel@tonic-gate 	    "usba_add_to_list: head=0x%p element=0x%p count=%d",
10567c478bd9Sstevel@tonic-gate 	    head, element, head->count);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate done:
10597c478bd9Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
10607c478bd9Sstevel@tonic-gate 	mutex_exit(&element->list_mutex);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate int
10657c478bd9Sstevel@tonic-gate usba_rm_from_list(usba_list_entry_t *head, usba_list_entry_t *element)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	usba_list_entry_t *e;
10687c478bd9Sstevel@tonic-gate 	int		found = 0;
10697c478bd9Sstevel@tonic-gate 	int		remaining;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	/* find the element in the list first */
10727c478bd9Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
10757c478bd9Sstevel@tonic-gate 	    "usba_rm_from_list: head=0x%p element=0x%p count=%d",
10767c478bd9Sstevel@tonic-gate 	    head, element, head->count);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	remaining = head->count;
10797c478bd9Sstevel@tonic-gate 	e = head->next;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	while (e) {
10827c478bd9Sstevel@tonic-gate 		if (e == element) {
10837c478bd9Sstevel@tonic-gate 			found++;
10847c478bd9Sstevel@tonic-gate 			break;
10857c478bd9Sstevel@tonic-gate 		}
10867c478bd9Sstevel@tonic-gate 		e = e->next;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 		remaining--;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		/* Detect incorrect circ links or found unexpected elements. */
10917c478bd9Sstevel@tonic-gate 		if ((e && (remaining == 0)) ||
10927c478bd9Sstevel@tonic-gate 		    ((e == NULL) && (remaining))) {
10937c478bd9Sstevel@tonic-gate 			panic("Corrupted USB list at 0x%p", (void *)head);
10947c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
10957c478bd9Sstevel@tonic-gate 		}
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	if (!found) {
10997c478bd9Sstevel@tonic-gate 		mutex_exit(&head->list_mutex);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	/* now remove the element */
11057c478bd9Sstevel@tonic-gate 	mutex_enter(&element->list_mutex);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	if (element->next) {
11087c478bd9Sstevel@tonic-gate 		element->next->prev = element->prev;
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 	if (element->prev) {
11117c478bd9Sstevel@tonic-gate 		element->prev->next = element->next;
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 	if (head->next == element) {
11147c478bd9Sstevel@tonic-gate 		head->next = element->next;
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 	if (head->prev == element) {
11177c478bd9Sstevel@tonic-gate 		head->prev = element->prev;
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	element->prev = element->next = NULL;
11217c478bd9Sstevel@tonic-gate 	if (head->next == NULL) {
11227c478bd9Sstevel@tonic-gate 		ASSERT(head->prev == NULL);
11237c478bd9Sstevel@tonic-gate 	} else {
11247c478bd9Sstevel@tonic-gate 		ASSERT(head->next->prev == NULL);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 	if (head->prev == NULL) {
11277c478bd9Sstevel@tonic-gate 		ASSERT(head->next == NULL);
11287c478bd9Sstevel@tonic-gate 	} else {
11297c478bd9Sstevel@tonic-gate 		ASSERT(head->prev->next == NULL);
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	head->count--;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
11357c478bd9Sstevel@tonic-gate 	    "usba_rm_from_list success: head=0x%p element=0x%p cnt=%d",
11367c478bd9Sstevel@tonic-gate 	    head, element, head->count);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	mutex_exit(&element->list_mutex);
11397c478bd9Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate usba_list_entry_t *
11467c478bd9Sstevel@tonic-gate usba_rm_first_from_list(usba_list_entry_t *head)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	usba_list_entry_t *element = NULL;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	if (head) {
11517c478bd9Sstevel@tonic-gate 		mutex_enter(&head->list_mutex);
11527c478bd9Sstevel@tonic-gate 		element = head->next;
11537c478bd9Sstevel@tonic-gate 		if (element) {
11547c478bd9Sstevel@tonic-gate 			/* now remove the element */
11557c478bd9Sstevel@tonic-gate 			mutex_enter(&element->list_mutex);
11567c478bd9Sstevel@tonic-gate 			head->next = element->next;
11577c478bd9Sstevel@tonic-gate 			if (head->next) {
11587c478bd9Sstevel@tonic-gate 				head->next->prev = NULL;
11597c478bd9Sstevel@tonic-gate 			}
11607c478bd9Sstevel@tonic-gate 			if (head->prev == element) {
11617c478bd9Sstevel@tonic-gate 				head->prev = element->next;
11627c478bd9Sstevel@tonic-gate 			}
11637c478bd9Sstevel@tonic-gate 			element->prev = element->next = NULL;
11647c478bd9Sstevel@tonic-gate 			mutex_exit(&element->list_mutex);
11657c478bd9Sstevel@tonic-gate 			head->count--;
11667c478bd9Sstevel@tonic-gate 		}
11677c478bd9Sstevel@tonic-gate 		if (head->next == NULL) {
11687c478bd9Sstevel@tonic-gate 			ASSERT(head->prev == NULL);
11697c478bd9Sstevel@tonic-gate 		} else {
11707c478bd9Sstevel@tonic-gate 			ASSERT(head->next->prev == NULL);
11717c478bd9Sstevel@tonic-gate 		}
11727c478bd9Sstevel@tonic-gate 		if (head->prev == NULL) {
11737c478bd9Sstevel@tonic-gate 			ASSERT(head->next == NULL);
11747c478bd9Sstevel@tonic-gate 		} else {
11757c478bd9Sstevel@tonic-gate 			ASSERT(head->prev->next == NULL);
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
11787c478bd9Sstevel@tonic-gate 		    "usba_rm_first_from_list: head=0x%p el=0x%p cnt=%d",
11797c478bd9Sstevel@tonic-gate 		    head, element, head->count);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		mutex_exit(&head->list_mutex);
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	return (element);
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate usb_opaque_t
11897c478bd9Sstevel@tonic-gate usba_rm_first_pvt_from_list(usba_list_entry_t *head)
11907c478bd9Sstevel@tonic-gate {
11917c478bd9Sstevel@tonic-gate 	usba_list_entry_t *element = usba_rm_first_from_list(head);
11927c478bd9Sstevel@tonic-gate 	usb_opaque_t private = NULL;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	if (element) {
11957c478bd9Sstevel@tonic-gate 		mutex_enter(&element->list_mutex);
11967c478bd9Sstevel@tonic-gate 		private = element->private;
11977c478bd9Sstevel@tonic-gate 		mutex_exit(&element->list_mutex);
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	return (private);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate /*
12057c478bd9Sstevel@tonic-gate  * move list to new list and zero original list
12067c478bd9Sstevel@tonic-gate  */
12077c478bd9Sstevel@tonic-gate void
12087c478bd9Sstevel@tonic-gate usba_move_list(usba_list_entry_t *head, usba_list_entry_t *new,
12097c478bd9Sstevel@tonic-gate 	ddi_iblock_cookie_t iblock_cookie)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	usba_init_list(new, NULL, iblock_cookie);
12127c478bd9Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
12137c478bd9Sstevel@tonic-gate 	mutex_enter(&new->list_mutex);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	new->next = head->next;
12167c478bd9Sstevel@tonic-gate 	new->prev = head->prev;
12177c478bd9Sstevel@tonic-gate 	new->count = head->count;
12187c478bd9Sstevel@tonic-gate 	new->private = head->private;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	head->next = NULL;
12217c478bd9Sstevel@tonic-gate 	head->prev = NULL;
12227c478bd9Sstevel@tonic-gate 	head->count = 0;
12237c478bd9Sstevel@tonic-gate 	head->private = NULL;
12247c478bd9Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
12257c478bd9Sstevel@tonic-gate 	mutex_exit(&new->list_mutex);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate int
12307c478bd9Sstevel@tonic-gate usba_check_in_list(usba_list_entry_t *head, usba_list_entry_t *element)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	int		rval = USB_FAILURE;
12337c478bd9Sstevel@tonic-gate 	int		remaining;
12347c478bd9Sstevel@tonic-gate 	usba_list_entry_t *next;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
12377c478bd9Sstevel@tonic-gate 	remaining = head->count;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	mutex_enter(&element->list_mutex);
12407c478bd9Sstevel@tonic-gate 	for (next = head->next; next != NULL; next = next->next) {
12417c478bd9Sstevel@tonic-gate 		if (next == element) {
12427c478bd9Sstevel@tonic-gate 			rval = USB_SUCCESS;
12437c478bd9Sstevel@tonic-gate 			break;
12447c478bd9Sstevel@tonic-gate 		}
12457c478bd9Sstevel@tonic-gate 		remaining--;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 		/* Detect incorrect circ links or found unexpected elements. */
12487c478bd9Sstevel@tonic-gate 		if ((next->next && (remaining == 0)) ||
12497c478bd9Sstevel@tonic-gate 		    ((next->next == NULL) && remaining)) {
12507c478bd9Sstevel@tonic-gate 			panic("Corrupted USB list at 0x%p", (void *)head);
12517c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
12527c478bd9Sstevel@tonic-gate 		}
12537c478bd9Sstevel@tonic-gate 	}
12547c478bd9Sstevel@tonic-gate 	mutex_exit(&element->list_mutex);
12557c478bd9Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	return (rval);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate int
12627c478bd9Sstevel@tonic-gate usba_list_entry_leaks(usba_list_entry_t *head, char *what)
12637c478bd9Sstevel@tonic-gate {
12647c478bd9Sstevel@tonic-gate 	int		count = 0;
12657c478bd9Sstevel@tonic-gate 	int		remaining;
12667c478bd9Sstevel@tonic-gate 	usba_list_entry_t *next;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
12697c478bd9Sstevel@tonic-gate 	remaining = head->count;
12707c478bd9Sstevel@tonic-gate 	for (next = head->next; next != NULL; next = next->next) {
12717c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
12727c478bd9Sstevel@tonic-gate 		    "leaking %s 0x%p", what, next->private);
12737c478bd9Sstevel@tonic-gate 		count++;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 		remaining--;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		/* Detect incorrect circ links or found unexpected elements. */
12787c478bd9Sstevel@tonic-gate 		if ((next->next && (remaining == 0)) ||
12797c478bd9Sstevel@tonic-gate 		    ((next->next == NULL) && remaining)) {
12807c478bd9Sstevel@tonic-gate 			panic("Corrupted USB list at 0x%p", (void *)head);
12817c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
12827c478bd9Sstevel@tonic-gate 		}
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 	ASSERT(count == head->count);
12857c478bd9Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if (count) {
12887c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
12897c478bd9Sstevel@tonic-gate 		    "usba_list_entry_count: leaking %d", count);
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	return (count);
12937c478bd9Sstevel@tonic-gate }
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate int
12977c478bd9Sstevel@tonic-gate usba_list_entry_count(usba_list_entry_t *head)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate 	int count;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
13027c478bd9Sstevel@tonic-gate 	count = head->count;
13037c478bd9Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	return (count);
13067c478bd9Sstevel@tonic-gate }
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate /*
13107c478bd9Sstevel@tonic-gate  * check whether this dip is the root hub. instead of doing a
13117c478bd9Sstevel@tonic-gate  * strcmp on the node name we could also check the address
13127c478bd9Sstevel@tonic-gate  */
13137c478bd9Sstevel@tonic-gate int
13147c478bd9Sstevel@tonic-gate usba_is_root_hub(dev_info_t *dip)
13157c478bd9Sstevel@tonic-gate {
13167c478bd9Sstevel@tonic-gate 	if (dip) {
13177c478bd9Sstevel@tonic-gate 		return (ddi_prop_exists(DDI_DEV_T_ANY, dip,
13187c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS|DDI_PROP_NOTPROM, "root-hub"));
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	return (0);
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate /*
13257c478bd9Sstevel@tonic-gate  * get and store usba_device pointer in the devi
13267c478bd9Sstevel@tonic-gate  */
13277c478bd9Sstevel@tonic-gate usba_device_t *
13287c478bd9Sstevel@tonic-gate usba_get_usba_device(dev_info_t *dip)
13297c478bd9Sstevel@tonic-gate {
13307c478bd9Sstevel@tonic-gate 	/*
13317c478bd9Sstevel@tonic-gate 	 * we cannot use parent_data in the usb node because its
13327c478bd9Sstevel@tonic-gate 	 * bus parent (eg. PCI nexus driver) uses this data
13337c478bd9Sstevel@tonic-gate 	 *
13347c478bd9Sstevel@tonic-gate 	 * we cannot use driver data in the other usb nodes since
13357c478bd9Sstevel@tonic-gate 	 * usb drivers may need to use this
13367c478bd9Sstevel@tonic-gate 	 */
13377c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
13387c478bd9Sstevel@tonic-gate 		usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 		return (hcdi->hcdi_usba_device);
13417c478bd9Sstevel@tonic-gate 	} else {
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		return (ddi_get_parent_data(dip));
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate }
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate /*
13497c478bd9Sstevel@tonic-gate  * Retrieve the usba_device pointer from the dev without checking for
13507c478bd9Sstevel@tonic-gate  * the root hub first.	This function is only used in polled mode.
13517c478bd9Sstevel@tonic-gate  */
13527c478bd9Sstevel@tonic-gate usba_device_t *
13537c478bd9Sstevel@tonic-gate usba_polled_get_usba_device(dev_info_t *dip)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	/*
13567c478bd9Sstevel@tonic-gate 	 * Don't call usba_is_root_hub() to find out if this is
13577c478bd9Sstevel@tonic-gate 	 * the root hub  usba_is_root_hub() calls into the DDI
13587c478bd9Sstevel@tonic-gate 	 * where there are locking issues. The dip sent in during
13597c478bd9Sstevel@tonic-gate 	 * polled mode will never be the root hub, so just get
13607c478bd9Sstevel@tonic-gate 	 * the usba_device pointer from the dip.
13617c478bd9Sstevel@tonic-gate 	 */
13627c478bd9Sstevel@tonic-gate 	return (ddi_get_parent_data(dip));
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate void
13677c478bd9Sstevel@tonic-gate usba_set_usba_device(dev_info_t *dip, usba_device_t *usba_device)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
13707c478bd9Sstevel@tonic-gate 		usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
13717c478bd9Sstevel@tonic-gate 		/* no locking is needed here */
13727c478bd9Sstevel@tonic-gate 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(hcdi->hcdi_usba_device))
13737c478bd9Sstevel@tonic-gate 		hcdi->hcdi_usba_device = usba_device;
13747c478bd9Sstevel@tonic-gate 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(hcdi->hcdi_usba_device))
13757c478bd9Sstevel@tonic-gate 	} else {
13767c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(dip, usba_device);
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate /*
13827c478bd9Sstevel@tonic-gate  * usba_set_node_name() according to class, subclass, and protocol
13837c478bd9Sstevel@tonic-gate  * following the 1275 USB binding tables.
13847c478bd9Sstevel@tonic-gate  */
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate /* device node table, refer to section 3.2.2.1 of 1275 binding */
13877c478bd9Sstevel@tonic-gate static node_name_entry_t device_node_name_table[] = {
13887c478bd9Sstevel@tonic-gate { USB_CLASS_COMM,	DONTCARE,	DONTCARE,	"communications" },
13897c478bd9Sstevel@tonic-gate { USB_CLASS_HUB,	DONTCARE,	DONTCARE,	"hub" },
13907c478bd9Sstevel@tonic-gate { DONTCARE,		DONTCARE,	DONTCARE,	"device" }
13917c478bd9Sstevel@tonic-gate };
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate /* interface node table, refer to section 3.3.2.1 */
13947c478bd9Sstevel@tonic-gate static node_name_entry_t if_node_name_table[] = {
13957c478bd9Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_CONTROL, DONTCARE,	"sound-control" },
13967c478bd9Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_STREAMING, DONTCARE, "sound" },
13977c478bd9Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_MIDI_STREAMING, DONTCARE, "midi" },
13987c478bd9Sstevel@tonic-gate { USB_CLASS_AUDIO, DONTCARE,		DONTCARE,	"sound" },
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_DIRECT_LINE,	DONTCARE, "line" },
14017c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ABSTRCT_CTRL,	DONTCARE, "modem" },
14027c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_PHONE_CTRL, DONTCARE, "telephone" },
14037c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_MULTCNL_ISDN, DONTCARE, "isdn" },
14047c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ISDN,		DONTCARE, "isdn" },
14057c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ETHERNET,	DONTCARE, "ethernet" },
14067c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ATM_NETWORK, DONTCARE, "atm-network" },
14077c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, DONTCARE,		DONTCARE,	"control" },
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_KEYBOARD,	"keyboard" },
14107c478bd9Sstevel@tonic-gate { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_MOUSE,	"mouse" },
14117c478bd9Sstevel@tonic-gate { USB_CLASS_HID,	DONTCARE,	DONTCARE,	"input" },
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate { USB_CLASS_HUB,	DONTCARE,	DONTCARE,	"hub" },
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate { USB_CLASS_PHYSICAL,	DONTCARE,	DONTCARE,	"physical" },
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate { USB_CLASS_PRINTER,	DONTCARE,	DONTCARE,	"printer" },
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate { USB_CLASS_MASS_STORAGE, DONTCARE,	DONTCARE,	"storage" },
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate { USB_CLASS_CDC_DATA,	DONTCARE,	DONTCARE,	"data" },
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate { USB_CLASS_SECURITY,	DONTCARE,	DONTCARE,	"security" },
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_FIRMWARE, DONTCARE, "firmware" },
14267c478bd9Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_IRDA,	DONTCARE, "IrDa" },
14277c478bd9Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_TEST,	DONTCARE, "test" },
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate { DONTCARE,		DONTCARE,	DONTCARE,	"interface" },
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate };
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate /* combined node table, refer to section 3.4.2.1 */
14347c478bd9Sstevel@tonic-gate static node_name_entry_t combined_node_name_table[] = {
14357c478bd9Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_CONTROL, DONTCARE,	"sound-control" },
14367c478bd9Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_STREAMING, DONTCARE, "sound" },
14377c478bd9Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_MIDI_STREAMING, DONTCARE, "midi" },
14387c478bd9Sstevel@tonic-gate { USB_CLASS_AUDIO, DONTCARE,		DONTCARE,	"sound" },
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_DIRECT_LINE,	DONTCARE, "line" },
14417c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ABSTRCT_CTRL,	DONTCARE, "modem" },
14427c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_PHONE_CTRL, DONTCARE, "telephone" },
14437c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_MULTCNL_ISDN, DONTCARE, "isdn" },
14447c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ISDN,		DONTCARE, "isdn" },
14457c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ETHERNET,	DONTCARE, "ethernet" },
14467c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ATM_NETWORK, DONTCARE, "atm-network" },
14477c478bd9Sstevel@tonic-gate { USB_CLASS_COMM, DONTCARE,		DONTCARE,	"control" },
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_KEYBOARD, "keyboard" },
14507c478bd9Sstevel@tonic-gate { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_MOUSE,	"mouse" },
14517c478bd9Sstevel@tonic-gate { USB_CLASS_HID,	DONTCARE,	DONTCARE,	"input" },
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate { USB_CLASS_PHYSICAL,	DONTCARE,	DONTCARE,	"physical" },
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate { USB_CLASS_PRINTER,	DONTCARE,	DONTCARE,	"printer" },
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate { USB_CLASS_MASS_STORAGE, DONTCARE,	DONTCARE,	"storage" },
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate { USB_CLASS_CDC_DATA,	DONTCARE,	DONTCARE,	"data" },
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate { USB_CLASS_SECURITY,	DONTCARE,	DONTCARE,	"security" },
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_FIRMWARE, DONTCARE, "firmware" },
14647c478bd9Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_IRDA,	DONTCARE, "IrDa" },
14657c478bd9Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_TEST,	DONTCARE, "test" },
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate { USB_CLASS_HUB,	DONTCARE,	DONTCARE,	"hub" },
14687c478bd9Sstevel@tonic-gate { USB_CLASS_COMM,	DONTCARE,	DONTCARE,	"communications" },
14697c478bd9Sstevel@tonic-gate { DONTCARE,		DONTCARE,	DONTCARE,	"device" },
14707c478bd9Sstevel@tonic-gate };
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate static size_t device_node_name_table_size =
14737c478bd9Sstevel@tonic-gate 	sizeof (device_node_name_table)/sizeof (struct node_name_entry);
14747c478bd9Sstevel@tonic-gate static size_t if_node_name_table_size =
14757c478bd9Sstevel@tonic-gate 	sizeof (if_node_name_table)/sizeof (struct node_name_entry);
14767c478bd9Sstevel@tonic-gate static size_t combined_node_name_table_size =
14777c478bd9Sstevel@tonic-gate 	sizeof (combined_node_name_table)/sizeof (struct node_name_entry);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate static void
14817c478bd9Sstevel@tonic-gate usba_set_node_name(dev_info_t *dip, uint8_t class, uint8_t subclass,
14827c478bd9Sstevel@tonic-gate     uint8_t protocol, uint_t flag)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate 	int i;
14857c478bd9Sstevel@tonic-gate 	size_t size;
14867c478bd9Sstevel@tonic-gate 	node_name_entry_t *node_name_table;
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	switch (flag) {
14897c478bd9Sstevel@tonic-gate 	case FLAG_INTERFACE_NODE:
14907c478bd9Sstevel@tonic-gate 		node_name_table = if_node_name_table;
14917c478bd9Sstevel@tonic-gate 		size = if_node_name_table_size;
14927c478bd9Sstevel@tonic-gate 		break;
14937c478bd9Sstevel@tonic-gate 	case FLAG_DEVICE_NODE:
14947c478bd9Sstevel@tonic-gate 		node_name_table = device_node_name_table;
14957c478bd9Sstevel@tonic-gate 		size = device_node_name_table_size;
14967c478bd9Sstevel@tonic-gate 		break;
14977c478bd9Sstevel@tonic-gate 	case FLAG_COMBINED_NODE:
14987c478bd9Sstevel@tonic-gate 		node_name_table = combined_node_name_table;
14997c478bd9Sstevel@tonic-gate 		size = combined_node_name_table_size;
15007c478bd9Sstevel@tonic-gate 		break;
15017c478bd9Sstevel@tonic-gate 	default:
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 		return;
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
15077c478bd9Sstevel@tonic-gate 		int16_t c = node_name_table[i].class;
15087c478bd9Sstevel@tonic-gate 		int16_t s = node_name_table[i].subclass;
15097c478bd9Sstevel@tonic-gate 		int16_t p = node_name_table[i].protocol;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 		if (((c == DONTCARE) || (c == class)) &&
15127c478bd9Sstevel@tonic-gate 		    ((s == DONTCARE) || (s == subclass)) &&
15137c478bd9Sstevel@tonic-gate 		    ((p == DONTCARE) || (p == protocol))) {
15147c478bd9Sstevel@tonic-gate 			char *name = node_name_table[i].name;
15157c478bd9Sstevel@tonic-gate 			(void) ndi_devi_set_nodename(dip, name, 0);
15167c478bd9Sstevel@tonic-gate 			break;
15177c478bd9Sstevel@tonic-gate 		}
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate }
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate #ifdef DEBUG
15237c478bd9Sstevel@tonic-gate /*
15247c478bd9Sstevel@tonic-gate  * walk the children of the parent of this devi and compare the
15257c478bd9Sstevel@tonic-gate  * name and  reg property of each child. If there is a match
15267c478bd9Sstevel@tonic-gate  * return this node
15277c478bd9Sstevel@tonic-gate  */
15287c478bd9Sstevel@tonic-gate static dev_info_t *
15297c478bd9Sstevel@tonic-gate usba_find_existing_node(dev_info_t *odip)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate 	dev_info_t *ndip, *child, *pdip;
15327c478bd9Sstevel@tonic-gate 	int	*odata, *ndata;
15337c478bd9Sstevel@tonic-gate 	uint_t	n_odata, n_ndata;
15347c478bd9Sstevel@tonic-gate 	int	circular;
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	pdip = ddi_get_parent(odip);
15377c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
15387c478bd9Sstevel@tonic-gate 	    odip, DDI_PROP_DONTPASS, "reg",
15397c478bd9Sstevel@tonic-gate 	    &odata, &n_odata) != DDI_SUCCESS) {
15407c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_HCDI, usba_log_handle,
15417c478bd9Sstevel@tonic-gate 		    "usba_find_existing_node: "
15427c478bd9Sstevel@tonic-gate 		    "%s: DDI_NOT_WELL_FORMED", ddi_driver_name(odip));
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 		return (NULL);
15457c478bd9Sstevel@tonic-gate 	}
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circular);
15487c478bd9Sstevel@tonic-gate 	ndip = (dev_info_t *)(DEVI(pdip)->devi_child);
15497c478bd9Sstevel@tonic-gate 	while ((child = ndip) != NULL) {
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 		ndip = (dev_info_t *)(DEVI(child)->devi_sibling);
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		if (child == odip) {
15547c478bd9Sstevel@tonic-gate 			continue;
15557c478bd9Sstevel@tonic-gate 		}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 		if (strcmp(DEVI(child)->devi_node_name,
15587c478bd9Sstevel@tonic-gate 		    DEVI(odip)->devi_node_name)) {
15597c478bd9Sstevel@tonic-gate 			continue;
15607c478bd9Sstevel@tonic-gate 		}
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
15637c478bd9Sstevel@tonic-gate 		    child, DDI_PROP_DONTPASS, "reg",
15647c478bd9Sstevel@tonic-gate 		    &ndata, &n_ndata) != DDI_SUCCESS) {
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_HCDI, usba_log_handle,
15677c478bd9Sstevel@tonic-gate 			    "usba_find_existing_node: "
15687c478bd9Sstevel@tonic-gate 			    "%s DDI_NOT_WELL_FORMED", ddi_driver_name(child));
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 		} else if (n_ndata && n_odata && (bcmp(odata, ndata,
15717c478bd9Sstevel@tonic-gate 		    max(n_odata, n_ndata) * sizeof (int)) == 0)) {
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HCDI, usba_log_handle,
15747c478bd9Sstevel@tonic-gate 			    "usba_find_existing_node: found %s%d (%p)",
15757c478bd9Sstevel@tonic-gate 			    ddi_driver_name(child),
15767c478bd9Sstevel@tonic-gate 			    ddi_get_instance(child), (void *)child);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HCDI, usba_log_handle,
15797c478bd9Sstevel@tonic-gate 			    "usba_find_existing_node: "
15807c478bd9Sstevel@tonic-gate 			    "reg: %x %x %x - %x %x %x",
15817c478bd9Sstevel@tonic-gate 			    n_odata, odata[0], odata[1],
15827c478bd9Sstevel@tonic-gate 			    n_ndata, ndata[0], ndata[1]);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 			ddi_prop_free(ndata);
15857c478bd9Sstevel@tonic-gate 			break;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		} else {
15887c478bd9Sstevel@tonic-gate 			ddi_prop_free(ndata);
15897c478bd9Sstevel@tonic-gate 		}
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circular);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	ddi_prop_free(odata);
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	return (child);
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate #endif
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate /* change all unprintable characters to spaces */
16017c478bd9Sstevel@tonic-gate static void
16027c478bd9Sstevel@tonic-gate usba_filter_string(char *instr, char *outstr)
16037c478bd9Sstevel@tonic-gate {
16047c478bd9Sstevel@tonic-gate 	while (*instr) {
16057c478bd9Sstevel@tonic-gate 		if ((*instr >= ' ') && (*instr <= '~')) {
16067c478bd9Sstevel@tonic-gate 			*outstr = *instr;
16077c478bd9Sstevel@tonic-gate 		} else {
16087c478bd9Sstevel@tonic-gate 			*outstr = ' ';
16097c478bd9Sstevel@tonic-gate 		}
16107c478bd9Sstevel@tonic-gate 		outstr++;
16117c478bd9Sstevel@tonic-gate 		instr++;
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 	*outstr = '\0';
16147c478bd9Sstevel@tonic-gate }
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate /*
16187c478bd9Sstevel@tonic-gate  * lookup ugen binding specified in property in
16197c478bd9Sstevel@tonic-gate  * hcd.conf files
16207c478bd9Sstevel@tonic-gate  */
16217c478bd9Sstevel@tonic-gate int
16227c478bd9Sstevel@tonic-gate usba_get_ugen_binding(dev_info_t *dip)
16237c478bd9Sstevel@tonic-gate {
16247c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(dip);
16257c478bd9Sstevel@tonic-gate 	usba_hcdi_t	*hcdi =
16267c478bd9Sstevel@tonic-gate 			    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	return (hcdi->hcdi_ugen_default_binding);
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate /*
16337c478bd9Sstevel@tonic-gate  * driver binding support at device level
16347c478bd9Sstevel@tonic-gate  */
16357c478bd9Sstevel@tonic-gate dev_info_t *
16367c478bd9Sstevel@tonic-gate usba_ready_device_node(dev_info_t *child_dip)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	int		rval, i;
16397c478bd9Sstevel@tonic-gate 	int		n = 0;
16407c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(child_dip);
16417c478bd9Sstevel@tonic-gate 	usb_dev_descr_t	*usb_dev_descr;
16427c478bd9Sstevel@tonic-gate 	uint_t		n_cfgs;	/* number of configs */
16437c478bd9Sstevel@tonic-gate 	uint_t		n_ifs;	/* number of interfaces */
16447c478bd9Sstevel@tonic-gate 	uint_t		port;
16457c478bd9Sstevel@tonic-gate 	size_t		usb_config_length;
16467c478bd9Sstevel@tonic-gate 	uchar_t 	*usb_config;
16477c478bd9Sstevel@tonic-gate 	int		reg[1];
16487c478bd9Sstevel@tonic-gate 	usb_addr_t	address = usb_get_addr(child_dip);
16497c478bd9Sstevel@tonic-gate 	usb_if_descr_t	if_descr;
16507c478bd9Sstevel@tonic-gate 	size_t		size;
16517c478bd9Sstevel@tonic-gate 	int		combined_node = 0;
16527c478bd9Sstevel@tonic-gate 	int		is_hub;
16537c478bd9Sstevel@tonic-gate 	char		*devprop_str;
16547c478bd9Sstevel@tonic-gate 	char		*force_bind = NULL;
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	usb_config = usb_get_raw_cfg_data(child_dip, &usb_config_length);
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
16597c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_mutex);
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
16627c478bd9Sstevel@tonic-gate 	    "usba_ready_device_node: child=0x%p", (void *)child_dip);
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	port = usba_device->usb_port;
16657c478bd9Sstevel@tonic-gate 	usb_dev_descr = usba_device->usb_dev_descr;
16667c478bd9Sstevel@tonic-gate 	n_cfgs = usba_device->usb_n_cfgs;
16677c478bd9Sstevel@tonic-gate 	n_ifs = usba_device->usb_n_ifs;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	if (address != ROOT_HUB_ADDR) {
16717c478bd9Sstevel@tonic-gate 		size = usb_parse_if_descr(
16727c478bd9Sstevel@tonic-gate 				usb_config,
16737c478bd9Sstevel@tonic-gate 				usb_config_length,
16747c478bd9Sstevel@tonic-gate 				0,		/* interface index */
16757c478bd9Sstevel@tonic-gate 				0,		/* alt interface index */
16767c478bd9Sstevel@tonic-gate 				&if_descr,
16777c478bd9Sstevel@tonic-gate 				USB_IF_DESCR_SIZE);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 		if (size != USB_IF_DESCR_SIZE) {
16807c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
16817c478bd9Sstevel@tonic-gate 			    "parsing interface: "
16827c478bd9Sstevel@tonic-gate 			    "size (%lu) != USB_IF_DESCR_SIZE (%d)",
16837c478bd9Sstevel@tonic-gate 			    size, USB_IF_DESCR_SIZE);
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_mutex);
16867c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 			return (child_dip);
16897c478bd9Sstevel@tonic-gate 		}
16907c478bd9Sstevel@tonic-gate 	} else {
16917c478bd9Sstevel@tonic-gate 		/* fake an interface descriptor for the root hub */
16927c478bd9Sstevel@tonic-gate 		bzero(&if_descr, sizeof (if_descr));
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 		if_descr.bInterfaceClass = USB_CLASS_HUB;
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	reg[0] = port;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_mutex);
17007c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int_array(
17037c478bd9Sstevel@tonic-gate 		DDI_DEV_T_NONE, child_dip, "reg", reg, 1);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
17067c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
17077c478bd9Sstevel@tonic-gate 		    "usba_ready_device_node: property update failed");
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 		return (child_dip);
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	combined_node = ((n_cfgs == 1) && (n_ifs == 1) &&
17137c478bd9Sstevel@tonic-gate 	    ((usb_dev_descr->bDeviceClass == USB_CLASS_HUB) ||
17147c478bd9Sstevel@tonic-gate 	    (usb_dev_descr->bDeviceClass == 0)));
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	is_hub = (if_descr.bInterfaceClass == USB_CLASS_HUB) ||
17177c478bd9Sstevel@tonic-gate 		(usb_dev_descr->bDeviceClass == USB_CLASS_HUB);
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	/* set node name */
17207c478bd9Sstevel@tonic-gate 	if (combined_node) {
17217c478bd9Sstevel@tonic-gate 		usba_set_node_name(child_dip,
17227c478bd9Sstevel@tonic-gate 		    if_descr.bInterfaceClass,
17237c478bd9Sstevel@tonic-gate 		    if_descr.bInterfaceSubClass,
17247c478bd9Sstevel@tonic-gate 		    if_descr.bInterfaceProtocol,
17257c478bd9Sstevel@tonic-gate 		    FLAG_COMBINED_NODE);
17267c478bd9Sstevel@tonic-gate 	} else {
17277c478bd9Sstevel@tonic-gate 		usba_set_node_name(child_dip,
17287c478bd9Sstevel@tonic-gate 		    usb_dev_descr->bDeviceClass,
17297c478bd9Sstevel@tonic-gate 		    usb_dev_descr->bDeviceSubClass,
17307c478bd9Sstevel@tonic-gate 		    usb_dev_descr->bDeviceProtocol,
17317c478bd9Sstevel@tonic-gate 		    FLAG_DEVICE_NODE);
17327c478bd9Sstevel@tonic-gate 	}
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * check force binding rules
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	if ((address != ROOT_HUB_ADDR) && usba_ddivs_usbc &&
17387c478bd9Sstevel@tonic-gate 	    (address != usba_ddivs_usbc_xaddress) &&
17397c478bd9Sstevel@tonic-gate 	    (!(usba_ddivs_usbc_xhubs && is_hub))) {
17407c478bd9Sstevel@tonic-gate 		force_bind = "ddivs_usbc";
17417c478bd9Sstevel@tonic-gate 		(void) ndi_devi_set_nodename(child_dip, "ddivs_usbc", 0);
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	} else if (usba_device->usb_preferred_driver) {
17447c478bd9Sstevel@tonic-gate 		force_bind = usba_device->usb_preferred_driver;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	} else if ((address != ROOT_HUB_ADDR) &&
17477c478bd9Sstevel@tonic-gate 	    ((usba_ugen_force_binding == USBA_UGEN_DEVICE_BINDING) ||
17487c478bd9Sstevel@tonic-gate 	    ((usba_ugen_force_binding == USBA_UGEN_INTERFACE_BINDING) &&
17497c478bd9Sstevel@tonic-gate 	    combined_node)) && (!is_hub)) {
17507c478bd9Sstevel@tonic-gate 		force_bind = "ugen";
17517c478bd9Sstevel@tonic-gate 	}
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate #ifdef DEBUG
17547c478bd9Sstevel@tonic-gate 	/*
17557c478bd9Sstevel@tonic-gate 	 * check whether there is another dip with this name and address
17567c478bd9Sstevel@tonic-gate 	 * If the dip contains usba_device, it is held by the previous
17577c478bd9Sstevel@tonic-gate 	 * round of configuration.
17587c478bd9Sstevel@tonic-gate 	 */
17597c478bd9Sstevel@tonic-gate 	ASSERT(usba_find_existing_node(child_dip) == NULL);
17607c478bd9Sstevel@tonic-gate #endif
17617c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_mutex);
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	if (force_bind) {
17647c478bd9Sstevel@tonic-gate 		(void) ndi_devi_set_nodename(child_dip, force_bind, 0);
17657c478bd9Sstevel@tonic-gate 		(void) strncpy(usba_name[n++], force_bind,
17667c478bd9Sstevel@tonic-gate 						USBA_MAX_COMPAT_NAME_LEN);
17677c478bd9Sstevel@tonic-gate 	}
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	/* create compatible names */
17707c478bd9Sstevel@tonic-gate 	if (combined_node) {
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 		/* 1. usbVID,PID.REV */
17737c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
17747c478bd9Sstevel@tonic-gate 			"usb%x,%x.%x",
17757c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
17767c478bd9Sstevel@tonic-gate 			usb_dev_descr->idProduct,
17777c478bd9Sstevel@tonic-gate 			usb_dev_descr->bcdDevice);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 		/* 2. usbVID,PID */
17807c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
17817c478bd9Sstevel@tonic-gate 			"usb%x,%x",
17827c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
17837c478bd9Sstevel@tonic-gate 			usb_dev_descr->idProduct);
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		if (usb_dev_descr->bDeviceClass != 0) {
17867c478bd9Sstevel@tonic-gate 			/* 3. usbVID,classDC.DSC.DPROTO */
17877c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
17887c478bd9Sstevel@tonic-gate 				"usb%x,class%x.%x.%x",
17897c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
17907c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass,
17917c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceSubClass,
17927c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceProtocol);
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 			/* 4. usbVID,classDC.DSC */
17957c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
17967c478bd9Sstevel@tonic-gate 				"usb%x,class%x.%x",
17977c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
17987c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass,
17997c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceSubClass);
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 			/* 5. usbVID,classDC */
18027c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18037c478bd9Sstevel@tonic-gate 				"usb%x,class%x",
18047c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
18057c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass);
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 			/* 6. usb,classDC.DSC.DPROTO */
18087c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18097c478bd9Sstevel@tonic-gate 				"usb,class%x.%x.%x",
18107c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass,
18117c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceSubClass,
18127c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceProtocol);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 			/* 7. usb,classDC.DSC */
18157c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18167c478bd9Sstevel@tonic-gate 				"usb,class%x.%x",
18177c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass,
18187c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceSubClass);
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 			/* 8. usb,classDC */
18217c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18227c478bd9Sstevel@tonic-gate 				"usb,class%x",
18237c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass);
18247c478bd9Sstevel@tonic-gate 		}
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 		if (if_descr.bInterfaceClass != 0) {
18277c478bd9Sstevel@tonic-gate 			/* 9. usbifVID,classIC.ISC.IPROTO */
18287c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18297c478bd9Sstevel@tonic-gate 				"usbif%x,class%x.%x.%x",
18307c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
18317c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceClass,
18327c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceSubClass,
18337c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceProtocol);
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 			/* 10. usbifVID,classIC.ISC */
18367c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18377c478bd9Sstevel@tonic-gate 				"usbif%x,class%x.%x",
18387c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
18397c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceClass,
18407c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceSubClass);
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 			/* 11. usbifVID,classIC */
18437c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18447c478bd9Sstevel@tonic-gate 				"usbif%x,class%x",
18457c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
18467c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceClass);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 			/* 12. usbif,classIC.ISC.IPROTO */
18497c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18507c478bd9Sstevel@tonic-gate 				"usbif,class%x.%x.%x",
18517c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceClass,
18527c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceSubClass,
18537c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceProtocol);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 			/* 13. usbif,classIC.ISC */
18567c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18577c478bd9Sstevel@tonic-gate 				"usbif,class%x.%x",
18587c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceClass,
18597c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceSubClass);
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 			/* 14. usbif,classIC */
18627c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18637c478bd9Sstevel@tonic-gate 				"usbif,class%x",
18647c478bd9Sstevel@tonic-gate 				if_descr.bInterfaceClass);
18657c478bd9Sstevel@tonic-gate 		}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 		/* 15. ugen or usb_mid */
18687c478bd9Sstevel@tonic-gate 		if (usba_get_ugen_binding(child_dip) ==
18697c478bd9Sstevel@tonic-gate 		    USBA_UGEN_DEVICE_BINDING) {
18707c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++], "ugen");
18717c478bd9Sstevel@tonic-gate 		} else {
18727c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++], "usb,device");
18737c478bd9Sstevel@tonic-gate 		}
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	} else {
18767c478bd9Sstevel@tonic-gate 		if (n_cfgs > 1) {
18777c478bd9Sstevel@tonic-gate 			/* 1. usbVID,PID.REV.configCN */
18787c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18797c478bd9Sstevel@tonic-gate 				"usb%x,%x.%x.config%x",
18807c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
18817c478bd9Sstevel@tonic-gate 				usb_dev_descr->idProduct,
18827c478bd9Sstevel@tonic-gate 				usb_dev_descr->bcdDevice,
18837c478bd9Sstevel@tonic-gate 				usba_device->usb_cfg_value);
18847c478bd9Sstevel@tonic-gate 		}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 		/* 2. usbVID,PID.REV */
18877c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
18887c478bd9Sstevel@tonic-gate 			"usb%x,%x.%x",
18897c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
18907c478bd9Sstevel@tonic-gate 			usb_dev_descr->idProduct,
18917c478bd9Sstevel@tonic-gate 			usb_dev_descr->bcdDevice);
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 		/* 3. usbVID,PID.configCN */
18947c478bd9Sstevel@tonic-gate 		if (n_cfgs > 1) {
18957c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
18967c478bd9Sstevel@tonic-gate 				"usb%x,%x.%x",
18977c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
18987c478bd9Sstevel@tonic-gate 				usb_dev_descr->idProduct,
18997c478bd9Sstevel@tonic-gate 				usba_device->usb_cfg_value);
19007c478bd9Sstevel@tonic-gate 		}
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 		/* 4. usbVID,PID */
19037c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
19047c478bd9Sstevel@tonic-gate 			"usb%x,%x",
19057c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
19067c478bd9Sstevel@tonic-gate 			usb_dev_descr->idProduct);
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 		if (usb_dev_descr->bDeviceClass != 0) {
19097c478bd9Sstevel@tonic-gate 			/* 5. usbVID,classDC.DSC.DPROTO */
19107c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19117c478bd9Sstevel@tonic-gate 				"usb%x,class%x.%x.%x",
19127c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
19137c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass,
19147c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceSubClass,
19157c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceProtocol);
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 			/* 6. usbVID,classDC.DSC */
19187c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19197c478bd9Sstevel@tonic-gate 				"usb%x.class%x.%x",
19207c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
19217c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass,
19227c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceSubClass);
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 			/* 7. usbVID,classDC */
19257c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19267c478bd9Sstevel@tonic-gate 				"usb%x.class%x",
19277c478bd9Sstevel@tonic-gate 				usb_dev_descr->idVendor,
19287c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass);
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 			/* 8. usb,classDC.DSC.DPROTO */
19317c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19327c478bd9Sstevel@tonic-gate 				"usb,class%x.%x.%x",
19337c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass,
19347c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceSubClass,
19357c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceProtocol);
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 			/* 9. usb,classDC.DSC */
19387c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19397c478bd9Sstevel@tonic-gate 				"usb,class%x.%x",
19407c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass,
19417c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceSubClass);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 			/* 10. usb,classDC */
19447c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19457c478bd9Sstevel@tonic-gate 				"usb,class%x",
19467c478bd9Sstevel@tonic-gate 				usb_dev_descr->bDeviceClass);
19477c478bd9Sstevel@tonic-gate 		}
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 		if (usba_get_ugen_binding(child_dip) ==
19507c478bd9Sstevel@tonic-gate 		    USBA_UGEN_DEVICE_BINDING) {
19517c478bd9Sstevel@tonic-gate 			/* 11. ugen */
19527c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++], "ugen");
19537c478bd9Sstevel@tonic-gate 		} else {
19547c478bd9Sstevel@tonic-gate 			/* 11. usb,device */
19557c478bd9Sstevel@tonic-gate 			(void) sprintf(usba_name[n++], "usb,device");
19567c478bd9Sstevel@tonic-gate 		}
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i += 2) {
19607c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
19617c478bd9Sstevel@tonic-gate 		    "compatible name:\t%s\t%s", usba_compatible[i],
19627c478bd9Sstevel@tonic-gate 		    (((i+1) < n)? usba_compatible[i+1] : ""));
19637c478bd9Sstevel@tonic-gate 	}
19647c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_mutex);
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	if (n) {
19677c478bd9Sstevel@tonic-gate 		rval = ndi_prop_update_string_array(
19687c478bd9Sstevel@tonic-gate 		    DDI_DEV_T_NONE, child_dip,
19697c478bd9Sstevel@tonic-gate 		    "compatible", (char **)usba_compatible, n);
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
19747c478bd9Sstevel@tonic-gate 			    "usba_ready_device_node: property update failed");
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 			return (child_dip);
19777c478bd9Sstevel@tonic-gate 		}
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	/* update the address property */
19817c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
19827c478bd9Sstevel@tonic-gate 			"assigned-address", usba_device->usb_addr);
19837c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
19847c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
19857c478bd9Sstevel@tonic-gate 		    "usba_ready_device_node: address update failed");
19867c478bd9Sstevel@tonic-gate 	}
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	/* update the address property */
19897c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
19907c478bd9Sstevel@tonic-gate 			"assigned-address", usba_device->usb_addr);
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
19937c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
19947c478bd9Sstevel@tonic-gate 		    "usba_ready_device_node: address update failed");
19957c478bd9Sstevel@tonic-gate 	}
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	/* update the usb device properties (PSARC/2000/454) */
19987c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
19997c478bd9Sstevel@tonic-gate 	    "usb-vendor-id", usb_dev_descr->idVendor);
20007c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
20017c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20027c478bd9Sstevel@tonic-gate 		    "usba_ready_device_node: usb-vendor-id update failed");
20037c478bd9Sstevel@tonic-gate 	}
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
20067c478bd9Sstevel@tonic-gate 	    "usb-product-id", usb_dev_descr->idProduct);
20077c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
20087c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20097c478bd9Sstevel@tonic-gate 		    "usba_ready_device_node: usb-product-id update failed");
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
20137c478bd9Sstevel@tonic-gate 	    "usb-revision-id", usb_dev_descr->bcdDevice);
20147c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
20157c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20167c478bd9Sstevel@tonic-gate 		    "usba_ready_device_node: usb-revision-id update failed");
20177c478bd9Sstevel@tonic-gate 	}
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
20207c478bd9Sstevel@tonic-gate 	    "usb-num-configs", usb_dev_descr->bNumConfigurations);
20217c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
20227c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20237c478bd9Sstevel@tonic-gate 		    "usba_ready_device_node: usb-num-configs update failed");
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
20277c478bd9Sstevel@tonic-gate 	    "usb-release", usb_dev_descr->bcdUSB);
20287c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
20297c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20307c478bd9Sstevel@tonic-gate 		    "usba_ready_device_node: usb-release update failed");
20317c478bd9Sstevel@tonic-gate 	}
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	devprop_str = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	if (usba_device->usb_serialno_str) {
20367c478bd9Sstevel@tonic-gate 		usba_filter_string(usba_device->usb_serialno_str, devprop_str);
20377c478bd9Sstevel@tonic-gate 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
20387c478bd9Sstevel@tonic-gate 		    "usb-serialno", devprop_str);
20397c478bd9Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
20407c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20417c478bd9Sstevel@tonic-gate 			    "usba_ready_device_node: "
20427c478bd9Sstevel@tonic-gate 			    "usb-serialno update failed");
20437c478bd9Sstevel@tonic-gate 		}
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	if (usba_device->usb_mfg_str) {
20477c478bd9Sstevel@tonic-gate 		usba_filter_string(usba_device->usb_mfg_str, devprop_str);
20487c478bd9Sstevel@tonic-gate 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
20497c478bd9Sstevel@tonic-gate 		    "usb-vendor-name", devprop_str);
20507c478bd9Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
20517c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20527c478bd9Sstevel@tonic-gate 			    "usba_ready_device_node: "
20537c478bd9Sstevel@tonic-gate 			    "usb-vendor-name update failed");
20547c478bd9Sstevel@tonic-gate 		}
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	if (usba_device->usb_product_str) {
20587c478bd9Sstevel@tonic-gate 		usba_filter_string(usba_device->usb_product_str, devprop_str);
20597c478bd9Sstevel@tonic-gate 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
20607c478bd9Sstevel@tonic-gate 		    "usb-product-name", devprop_str);
20617c478bd9Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
20627c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20637c478bd9Sstevel@tonic-gate 			    "usba_ready_device_node: "
20647c478bd9Sstevel@tonic-gate 			    "usb-product-name update failed");
20657c478bd9Sstevel@tonic-gate 		}
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	kmem_free(devprop_str, USB_MAXSTRINGLEN);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	if (!combined_node) {
20717c478bd9Sstevel@tonic-gate 		/* update the configuration property */
20727c478bd9Sstevel@tonic-gate 		rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
20737c478bd9Sstevel@tonic-gate 			"configuration#", usba_device->usb_cfg_value);
20747c478bd9Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
20757c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
20767c478bd9Sstevel@tonic-gate 			    "usba_ready_device_node: "
20777c478bd9Sstevel@tonic-gate 			    "config prop update failed");
20787c478bd9Sstevel@tonic-gate 		}
20797c478bd9Sstevel@tonic-gate 	}
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	if (usba_device->usb_port_status == USBA_LOW_SPEED_DEV) {
20827c478bd9Sstevel@tonic-gate 		/* create boolean property */
20837c478bd9Sstevel@tonic-gate 		rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, child_dip,
20847c478bd9Sstevel@tonic-gate 			"low-speed");
20857c478bd9Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
20867c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
20877c478bd9Sstevel@tonic-gate 			    "usba_ready_device_node: "
20887c478bd9Sstevel@tonic-gate 			    "low speed prop update failed");
20897c478bd9Sstevel@tonic-gate 		}
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
20937c478bd9Sstevel@tonic-gate 	    "%s%d at port %d: %s, dip=0x%p",
20947c478bd9Sstevel@tonic-gate 	    ddi_node_name(ddi_get_parent(child_dip)),
20957c478bd9Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(child_dip)),
20967c478bd9Sstevel@tonic-gate 	    port, ddi_node_name(child_dip), child_dip);
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	usba_set_usba_device(child_dip, usba_device);
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	ASSERT(!mutex_owned(&(usba_get_usba_device(child_dip)->usb_mutex)));
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	return (child_dip);
21037c478bd9Sstevel@tonic-gate }
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate /*
21077c478bd9Sstevel@tonic-gate  * driver binding at interface level, the first arg will be the
21087c478bd9Sstevel@tonic-gate  * the parent dip
21097c478bd9Sstevel@tonic-gate  */
21107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21117c478bd9Sstevel@tonic-gate dev_info_t *
21127c478bd9Sstevel@tonic-gate usba_ready_interface_node(dev_info_t *dip, uint_t intf)
21137c478bd9Sstevel@tonic-gate {
21147c478bd9Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
21157c478bd9Sstevel@tonic-gate 	usba_device_t		*child_ud = usba_get_usba_device(dip);
21167c478bd9Sstevel@tonic-gate 	usb_dev_descr_t	*usb_dev_descr;
21177c478bd9Sstevel@tonic-gate 	size_t			usb_cfg_length;
21187c478bd9Sstevel@tonic-gate 	uchar_t 		*usb_cfg;
21197c478bd9Sstevel@tonic-gate 	usb_if_descr_t	if_descr;
21207c478bd9Sstevel@tonic-gate 	int			i, n, rval;
21217c478bd9Sstevel@tonic-gate 	int			reg[2];
21227c478bd9Sstevel@tonic-gate 	size_t			size;
21237c478bd9Sstevel@tonic-gate 	usb_port_status_t	port_status;
21247c478bd9Sstevel@tonic-gate 	char			*force_bind = NULL;
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	usb_cfg = usb_get_raw_cfg_data(dip, &usb_cfg_length);
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	usb_dev_descr = child_ud->usb_dev_descr;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	/*
21337c478bd9Sstevel@tonic-gate 	 * for each interface, determine all compatible names
21347c478bd9Sstevel@tonic-gate 	 */
21357c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
21367c478bd9Sstevel@tonic-gate 	    "usba_ready_interface_node: "
21377c478bd9Sstevel@tonic-gate 	    "port %d, interface = %d port status = %x",
21387c478bd9Sstevel@tonic-gate 	    child_ud->usb_port, intf, child_ud->usb_port_status);
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	/* Parse the interface descriptor */
21417c478bd9Sstevel@tonic-gate 	size = usb_parse_if_descr(
21427c478bd9Sstevel@tonic-gate 			usb_cfg,
21437c478bd9Sstevel@tonic-gate 			usb_cfg_length,
21447c478bd9Sstevel@tonic-gate 			intf,		/* interface index */
21457c478bd9Sstevel@tonic-gate 			0,		/* alt interface index */
21467c478bd9Sstevel@tonic-gate 			&if_descr,
21477c478bd9Sstevel@tonic-gate 			USB_IF_DESCR_SIZE);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	if (size != USB_IF_DESCR_SIZE) {
21507c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
21517c478bd9Sstevel@tonic-gate 		    "parsing interface: size (%lu) != USB_IF_DESCR_SIZE (%d)",
21527c478bd9Sstevel@tonic-gate 		    size, USB_IF_DESCR_SIZE);
21537c478bd9Sstevel@tonic-gate 		mutex_exit(&child_ud->usb_mutex);
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 		return (NULL);
21567c478bd9Sstevel@tonic-gate 	}
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	port_status = child_ud->usb_port_status;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	/* create reg property */
21617c478bd9Sstevel@tonic-gate 	reg[0] = intf;
21627c478bd9Sstevel@tonic-gate 	reg[1] = child_ud->usb_cfg_value;
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	/* clone this dip */
21677c478bd9Sstevel@tonic-gate 	rval =	usba_create_child_devi(dip,
21687c478bd9Sstevel@tonic-gate 			"interface",
21697c478bd9Sstevel@tonic-gate 			NULL,		/* usba_hcdi ops */
21707c478bd9Sstevel@tonic-gate 			NULL,		/* root hub dip */
21717c478bd9Sstevel@tonic-gate 			port_status,	/* port status */
21727c478bd9Sstevel@tonic-gate 			child_ud,	/* share this usba_device */
21737c478bd9Sstevel@tonic-gate 			&child_dip);
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 		goto fail;
21787c478bd9Sstevel@tonic-gate 	}
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int_array(
21817c478bd9Sstevel@tonic-gate 		DDI_DEV_T_NONE, child_dip, "reg", reg, 2);
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 		goto fail;
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	usba_set_node_name(child_dip, if_descr.bInterfaceClass,
21897c478bd9Sstevel@tonic-gate 	    if_descr.bInterfaceSubClass, if_descr.bInterfaceProtocol,
21907c478bd9Sstevel@tonic-gate 	    FLAG_INTERFACE_NODE);
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	/* check force binding */
21937c478bd9Sstevel@tonic-gate 	if (usba_ugen_force_binding == USBA_UGEN_INTERFACE_BINDING) {
21947c478bd9Sstevel@tonic-gate 		force_bind = "ugen";
21957c478bd9Sstevel@tonic-gate 	}
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate #ifdef DEBUG
21987c478bd9Sstevel@tonic-gate 	/*
21997c478bd9Sstevel@tonic-gate 	 * check whether there is another dip with this name and address
22007c478bd9Sstevel@tonic-gate 	 */
22017c478bd9Sstevel@tonic-gate 	ASSERT(usba_find_existing_node(child_dip) == NULL);
22027c478bd9Sstevel@tonic-gate #endif
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_mutex);
22057c478bd9Sstevel@tonic-gate 	n = 0;
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	if (force_bind) {
22087c478bd9Sstevel@tonic-gate 		(void) ndi_devi_set_nodename(child_dip, force_bind, 0);
22097c478bd9Sstevel@tonic-gate 		(void) strncpy(usba_name[n++], force_bind,
22107c478bd9Sstevel@tonic-gate 						USBA_MAX_COMPAT_NAME_LEN);
22117c478bd9Sstevel@tonic-gate 	}
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 	/* 1) usbifVID,PID.REV.configCN.IN */
22147c478bd9Sstevel@tonic-gate 	(void) sprintf(usba_name[n++],
22157c478bd9Sstevel@tonic-gate 			"usbif%x,%x.%x.config%x.%x",
22167c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
22177c478bd9Sstevel@tonic-gate 			usb_dev_descr->idProduct,
22187c478bd9Sstevel@tonic-gate 			usb_dev_descr->bcdDevice,
22197c478bd9Sstevel@tonic-gate 			child_ud->usb_cfg_value,
22207c478bd9Sstevel@tonic-gate 			intf);
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	/* 2) usbifVID,PID.configCN.IN */
22237c478bd9Sstevel@tonic-gate 	(void) sprintf(usba_name[n++],
22247c478bd9Sstevel@tonic-gate 			"usbif%x,%x.config%x.%x",
22257c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
22267c478bd9Sstevel@tonic-gate 			usb_dev_descr->idProduct,
22277c478bd9Sstevel@tonic-gate 			child_ud->usb_cfg_value,
22287c478bd9Sstevel@tonic-gate 			intf);
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	if (if_descr.bInterfaceClass) {
22327c478bd9Sstevel@tonic-gate 		/* 3) usbifVID,classIC.ISC.IPROTO */
22337c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
22347c478bd9Sstevel@tonic-gate 			"usbif%x,class%x.%x.%x",
22357c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
22367c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceClass,
22377c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceSubClass,
22387c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceProtocol);
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 		/* 4) usbifVID,classIC.ISC */
22417c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
22427c478bd9Sstevel@tonic-gate 			"usbif%x,class%x.%x",
22437c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
22447c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceClass,
22457c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceSubClass);
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 		/* 5) usbifVID,classIC */
22487c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
22497c478bd9Sstevel@tonic-gate 			"usbif%x,class%x",
22507c478bd9Sstevel@tonic-gate 			usb_dev_descr->idVendor,
22517c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceClass);
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 		/* 6) usbif,classIC.ISC.IPROTO */
22547c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
22557c478bd9Sstevel@tonic-gate 			"usbif,class%x.%x.%x",
22567c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceClass,
22577c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceSubClass,
22587c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceProtocol);
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 		/* 7) usbif,classIC.ISC */
22617c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
22627c478bd9Sstevel@tonic-gate 			"usbif,class%x.%x",
22637c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceClass,
22647c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceSubClass);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 		/* 8) usbif,classIC */
22677c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
22687c478bd9Sstevel@tonic-gate 			"usbif,class%x",
22697c478bd9Sstevel@tonic-gate 			if_descr.bInterfaceClass);
22707c478bd9Sstevel@tonic-gate 	}
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	if (usba_get_ugen_binding(child_dip) ==
22737c478bd9Sstevel@tonic-gate 	    USBA_UGEN_INTERFACE_BINDING) {
22747c478bd9Sstevel@tonic-gate 		/* 9) ugen */
22757c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_name[n++], "ugen");
22767c478bd9Sstevel@tonic-gate 	}
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i += 2) {
22797c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
22807c478bd9Sstevel@tonic-gate 		    "compatible name:\t%s\t%s", usba_compatible[i],
22817c478bd9Sstevel@tonic-gate 		    (((i+1) < n)? usba_compatible[i+1] : ""));
22827c478bd9Sstevel@tonic-gate 	}
22837c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_mutex);
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	/* create compatible property */
22867c478bd9Sstevel@tonic-gate 	if (n) {
22877c478bd9Sstevel@tonic-gate 		rval = ndi_prop_update_string_array(
22887c478bd9Sstevel@tonic-gate 		    DDI_DEV_T_NONE, child_dip,
22897c478bd9Sstevel@tonic-gate 		    "compatible", (char **)usba_compatible,
22907c478bd9Sstevel@tonic-gate 		    n);
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 			goto fail;
22957c478bd9Sstevel@tonic-gate 		}
22967c478bd9Sstevel@tonic-gate 	}
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	/* update the address property */
22997c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
23007c478bd9Sstevel@tonic-gate 			"assigned-address", child_ud->usb_addr);
23017c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
23027c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
23037c478bd9Sstevel@tonic-gate 		    "usba_ready_interface_node: address update failed");
23047c478bd9Sstevel@tonic-gate 	}
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	/* create property with if number */
23077c478bd9Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
23087c478bd9Sstevel@tonic-gate 		"interface", intf);
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 		goto fail;
23137c478bd9Sstevel@tonic-gate 	}
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
23167c478bd9Sstevel@tonic-gate 	    "%s%d port %d: %s, dip = 0x%p",
23177c478bd9Sstevel@tonic-gate 	    ddi_node_name(ddi_get_parent(dip)),
23187c478bd9Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(dip)),
23197c478bd9Sstevel@tonic-gate 	    child_ud->usb_port, ddi_node_name(child_dip), child_dip);
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	usba_set_usba_device(child_dip, child_ud);
23227c478bd9Sstevel@tonic-gate 	ASSERT(!mutex_owned(&(usba_get_usba_device(child_dip)->usb_mutex)));
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	return (child_dip);
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate fail:
23277c478bd9Sstevel@tonic-gate 	(void) usba_destroy_child_devi(child_dip, NDI_DEVI_REMOVE);
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 	return (NULL);
23307c478bd9Sstevel@tonic-gate }
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate /*
23347c478bd9Sstevel@tonic-gate  * retrieve string descriptors for manufacturer, vendor and serial
23357c478bd9Sstevel@tonic-gate  * number
23367c478bd9Sstevel@tonic-gate  */
23377c478bd9Sstevel@tonic-gate void
23387c478bd9Sstevel@tonic-gate usba_get_dev_string_descrs(dev_info_t *dip, usba_device_t *ud)
23397c478bd9Sstevel@tonic-gate {
23407c478bd9Sstevel@tonic-gate 	char	*tmpbuf, *str;
23417c478bd9Sstevel@tonic-gate 	int	l;
23427c478bd9Sstevel@tonic-gate 	usb_dev_descr_t *usb_dev_descr = ud->usb_dev_descr;
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
23467c478bd9Sstevel@tonic-gate 	    "usba_get_usb_string_descr: m=%d, p=%d, s=%d",
23477c478bd9Sstevel@tonic-gate 	    usb_dev_descr->iManufacturer,
23487c478bd9Sstevel@tonic-gate 	    usb_dev_descr->iProduct,
23497c478bd9Sstevel@tonic-gate 	    usb_dev_descr->iSerialNumber);
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	/* fetch manufacturer string */
23547c478bd9Sstevel@tonic-gate 	if ((ud->usb_mfg_str == NULL) && usb_dev_descr->iManufacturer &&
23557c478bd9Sstevel@tonic-gate 	    (usb_get_string_descr(dip, USB_LANG_ID,
23567c478bd9Sstevel@tonic-gate 		usb_dev_descr->iManufacturer, tmpbuf, USB_MAXSTRINGLEN) ==
23577c478bd9Sstevel@tonic-gate 		USB_SUCCESS)) {
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 		l = strlen(tmpbuf);
23607c478bd9Sstevel@tonic-gate 		if (l > 0) {
23617c478bd9Sstevel@tonic-gate 			str = kmem_zalloc(l + 1, KM_SLEEP);
23627c478bd9Sstevel@tonic-gate 			mutex_enter(&ud->usb_mutex);
23637c478bd9Sstevel@tonic-gate 			ud->usb_mfg_str = str;
23647c478bd9Sstevel@tonic-gate 			(void) strcpy(ud->usb_mfg_str, tmpbuf);
23657c478bd9Sstevel@tonic-gate 			mutex_exit(&ud->usb_mutex);
23667c478bd9Sstevel@tonic-gate 		}
23677c478bd9Sstevel@tonic-gate 	}
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	/* fetch product string */
23707c478bd9Sstevel@tonic-gate 	if ((ud->usb_product_str == NULL) && usb_dev_descr->iProduct &&
23717c478bd9Sstevel@tonic-gate 	    (usb_get_string_descr(dip, USB_LANG_ID, usb_dev_descr->iProduct,
23727c478bd9Sstevel@tonic-gate 	    tmpbuf, USB_MAXSTRINGLEN) ==
23737c478bd9Sstevel@tonic-gate 		USB_SUCCESS)) {
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 		l = strlen(tmpbuf);
23767c478bd9Sstevel@tonic-gate 		if (l > 0) {
23777c478bd9Sstevel@tonic-gate 			str = kmem_zalloc(l + 1, KM_SLEEP);
23787c478bd9Sstevel@tonic-gate 			mutex_enter(&ud->usb_mutex);
23797c478bd9Sstevel@tonic-gate 			ud->usb_product_str = str;
23807c478bd9Sstevel@tonic-gate 			(void) strcpy(ud->usb_product_str, tmpbuf);
23817c478bd9Sstevel@tonic-gate 			mutex_exit(&ud->usb_mutex);
23827c478bd9Sstevel@tonic-gate 		}
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	/* fetch device serial number string */
23867c478bd9Sstevel@tonic-gate 	if ((ud->usb_serialno_str == NULL) && usb_dev_descr->iSerialNumber &&
23877c478bd9Sstevel@tonic-gate 	    (usb_get_string_descr(dip, USB_LANG_ID,
23887c478bd9Sstevel@tonic-gate 		usb_dev_descr->iSerialNumber, tmpbuf, USB_MAXSTRINGLEN) ==
23897c478bd9Sstevel@tonic-gate 		USB_SUCCESS)) {
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 		l = strlen(tmpbuf);
23927c478bd9Sstevel@tonic-gate 		if (l > 0) {
23937c478bd9Sstevel@tonic-gate 			str = kmem_zalloc(l + 1, KM_SLEEP);
23947c478bd9Sstevel@tonic-gate 			mutex_enter(&ud->usb_mutex);
23957c478bd9Sstevel@tonic-gate 			ud->usb_serialno_str = str;
23967c478bd9Sstevel@tonic-gate 			(void) strcpy(ud->usb_serialno_str, tmpbuf);
23977c478bd9Sstevel@tonic-gate 			mutex_exit(&ud->usb_mutex);
23987c478bd9Sstevel@tonic-gate 		}
23997c478bd9Sstevel@tonic-gate 	}
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	kmem_free(tmpbuf, USB_MAXSTRINGLEN);
24027c478bd9Sstevel@tonic-gate }
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate /*
24067c478bd9Sstevel@tonic-gate  * usba_str_startcmp:
24077c478bd9Sstevel@tonic-gate  *	Return the number of characters duplicated from the beginning of the
24087c478bd9Sstevel@tonic-gate  *	string.  Return -1 if a complete duplicate.
24097c478bd9Sstevel@tonic-gate  *
24107c478bd9Sstevel@tonic-gate  * Arguments:
24117c478bd9Sstevel@tonic-gate  *	Two strings to compare.
24127c478bd9Sstevel@tonic-gate  */
24137c478bd9Sstevel@tonic-gate static int usba_str_startcmp(char *first, char *second)
24147c478bd9Sstevel@tonic-gate {
24157c478bd9Sstevel@tonic-gate 	int num_same_chars = 0;
24167c478bd9Sstevel@tonic-gate 	while (*first == *second++) {
24177c478bd9Sstevel@tonic-gate 		if (*first++ == '\0') {
24187c478bd9Sstevel@tonic-gate 			return (-1);
24197c478bd9Sstevel@tonic-gate 		}
24207c478bd9Sstevel@tonic-gate 		num_same_chars++;
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	return (num_same_chars);
24247c478bd9Sstevel@tonic-gate }
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate /*
24287c478bd9Sstevel@tonic-gate  * usba_get_mfg_prod_sn_str:
24297c478bd9Sstevel@tonic-gate  *	Return a string containing mfg, product, serial number strings.
24307c478bd9Sstevel@tonic-gate  *	Remove duplicates if some strings are the same.
24317c478bd9Sstevel@tonic-gate  *
24327c478bd9Sstevel@tonic-gate  * Arguments:
24337c478bd9Sstevel@tonic-gate  *	dip	- pointer to dev info
24347c478bd9Sstevel@tonic-gate  *	buffer	- Where string is returned
24357c478bd9Sstevel@tonic-gate  *	buflen	- Length of buffer
24367c478bd9Sstevel@tonic-gate  *
24377c478bd9Sstevel@tonic-gate  * Returns:
24387c478bd9Sstevel@tonic-gate  *	Same as second arg.
24397c478bd9Sstevel@tonic-gate  */
24407c478bd9Sstevel@tonic-gate char *
24417c478bd9Sstevel@tonic-gate usba_get_mfg_prod_sn_str(
24427c478bd9Sstevel@tonic-gate     dev_info_t	*dip,
24437c478bd9Sstevel@tonic-gate     char	*buffer,
24447c478bd9Sstevel@tonic-gate     int		buflen)
24457c478bd9Sstevel@tonic-gate {
24467c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
24477c478bd9Sstevel@tonic-gate 	int return_len = 0;
24487c478bd9Sstevel@tonic-gate 	int len = 0;
24497c478bd9Sstevel@tonic-gate 	int duplen;
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	buffer[0] = '\0';
24527c478bd9Sstevel@tonic-gate 	buffer[buflen-1] = '\0';
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	if ((usba_device->usb_mfg_str) &&
24557c478bd9Sstevel@tonic-gate 	    ((len = strlen(usba_device->usb_mfg_str)) != 0)) {
24567c478bd9Sstevel@tonic-gate 		(void) strncpy(buffer, usba_device->usb_mfg_str, buflen - 1);
24577c478bd9Sstevel@tonic-gate 		return_len = min(buflen - 1, len);
24587c478bd9Sstevel@tonic-gate 	}
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	/* Product string exists to append. */
24617c478bd9Sstevel@tonic-gate 	if ((usba_device->usb_product_str) &&
24627c478bd9Sstevel@tonic-gate 	    ((len = strlen(usba_device->usb_product_str)) != 0)) {
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 		/* Append only parts of string that don't match mfg string. */
24657c478bd9Sstevel@tonic-gate 		duplen = usba_str_startcmp(buffer,
24667c478bd9Sstevel@tonic-gate 					usba_device->usb_product_str);
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 		if (duplen != -1) {		/* Not a complete match. */
24697c478bd9Sstevel@tonic-gate 			if (return_len > 0) {
24707c478bd9Sstevel@tonic-gate 				buffer[return_len++] = ' ';
24717c478bd9Sstevel@tonic-gate 			}
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 			/* Skip over the dup part of the concat'ed string. */
24747c478bd9Sstevel@tonic-gate 			len -= duplen;
24757c478bd9Sstevel@tonic-gate 			(void) strncpy(&buffer[return_len],
24767c478bd9Sstevel@tonic-gate 			    &usba_device->usb_product_str[duplen],
24777c478bd9Sstevel@tonic-gate 			    buflen - return_len - 1);
24787c478bd9Sstevel@tonic-gate 			return_len = min(buflen - 1, return_len + len);
24797c478bd9Sstevel@tonic-gate 		}
24807c478bd9Sstevel@tonic-gate 	}
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	if ((usba_device->usb_serialno_str) &&
24837c478bd9Sstevel@tonic-gate 	    ((len = strlen(usba_device->usb_serialno_str)) != 0)) {
24847c478bd9Sstevel@tonic-gate 		if (return_len > 0) {
24857c478bd9Sstevel@tonic-gate 			buffer[return_len++] = ' ';
24867c478bd9Sstevel@tonic-gate 		}
24877c478bd9Sstevel@tonic-gate 		(void) strncpy(&buffer[return_len],
24887c478bd9Sstevel@tonic-gate 				usba_device->usb_serialno_str,
24897c478bd9Sstevel@tonic-gate 				buflen - return_len - 1);
24907c478bd9Sstevel@tonic-gate 	}
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	return (buffer);
24937c478bd9Sstevel@tonic-gate }
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate /*
24977c478bd9Sstevel@tonic-gate  * USB enumeration statistic functions
24987c478bd9Sstevel@tonic-gate  */
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate /*
25017c478bd9Sstevel@tonic-gate  * Increments the hotplug statistics based on flags.
25027c478bd9Sstevel@tonic-gate  */
25037c478bd9Sstevel@tonic-gate void
25047c478bd9Sstevel@tonic-gate usba_update_hotplug_stats(dev_info_t *dip, usb_flags_t flags)
25057c478bd9Sstevel@tonic-gate {
25067c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(dip);
25077c478bd9Sstevel@tonic-gate 	usba_hcdi_t	*hcdi =
25087c478bd9Sstevel@tonic-gate 			    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	mutex_enter(&hcdi->hcdi_mutex);
25117c478bd9Sstevel@tonic-gate 	if (flags & USBA_TOTAL_HOTPLUG_SUCCESS) {
25127c478bd9Sstevel@tonic-gate 		hcdi->hcdi_total_hotplug_success++;
25137c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->
25147c478bd9Sstevel@tonic-gate 		    hcdi_hotplug_total_success.value.ui64++;
25157c478bd9Sstevel@tonic-gate 	}
25167c478bd9Sstevel@tonic-gate 	if (flags & USBA_HOTPLUG_SUCCESS) {
25177c478bd9Sstevel@tonic-gate 		hcdi->hcdi_hotplug_success++;
25187c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->
25197c478bd9Sstevel@tonic-gate 		    hcdi_hotplug_success.value.ui64++;
25207c478bd9Sstevel@tonic-gate 	}
25217c478bd9Sstevel@tonic-gate 	if (flags & USBA_TOTAL_HOTPLUG_FAILURE) {
25227c478bd9Sstevel@tonic-gate 		hcdi->hcdi_total_hotplug_failure++;
25237c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->
25247c478bd9Sstevel@tonic-gate 		    hcdi_hotplug_total_failure.value.ui64++;
25257c478bd9Sstevel@tonic-gate 	}
25267c478bd9Sstevel@tonic-gate 	if (flags & USBA_HOTPLUG_FAILURE) {
25277c478bd9Sstevel@tonic-gate 		hcdi->hcdi_hotplug_failure++;
25287c478bd9Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->
25297c478bd9Sstevel@tonic-gate 		    hcdi_hotplug_failure.value.ui64++;
25307c478bd9Sstevel@tonic-gate 	}
25317c478bd9Sstevel@tonic-gate 	mutex_exit(&hcdi->hcdi_mutex);
25327c478bd9Sstevel@tonic-gate }
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate /*
25367c478bd9Sstevel@tonic-gate  * Retrieve the current enumeration statistics
25377c478bd9Sstevel@tonic-gate  */
25387c478bd9Sstevel@tonic-gate void
25397c478bd9Sstevel@tonic-gate usba_get_hotplug_stats(dev_info_t *dip, ulong_t *total_success,
25407c478bd9Sstevel@tonic-gate     ulong_t *success, ulong_t *total_failure, ulong_t *failure,
25417c478bd9Sstevel@tonic-gate     uchar_t *device_count)
25427c478bd9Sstevel@tonic-gate {
25437c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(dip);
25447c478bd9Sstevel@tonic-gate 	usba_hcdi_t	*hcdi =
25457c478bd9Sstevel@tonic-gate 			    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	mutex_enter(&hcdi->hcdi_mutex);
25487c478bd9Sstevel@tonic-gate 	*total_success = hcdi->hcdi_total_hotplug_success;
25497c478bd9Sstevel@tonic-gate 	*success = hcdi->hcdi_hotplug_success;
25507c478bd9Sstevel@tonic-gate 	*total_failure = hcdi->hcdi_total_hotplug_failure;
25517c478bd9Sstevel@tonic-gate 	*failure = hcdi->hcdi_hotplug_failure;
25527c478bd9Sstevel@tonic-gate 	*device_count = hcdi->hcdi_device_count;
25537c478bd9Sstevel@tonic-gate 	mutex_exit(&hcdi->hcdi_mutex);
25547c478bd9Sstevel@tonic-gate }
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate /*
25587c478bd9Sstevel@tonic-gate  * Reset the resetable hotplug stats
25597c478bd9Sstevel@tonic-gate  */
25607c478bd9Sstevel@tonic-gate void
25617c478bd9Sstevel@tonic-gate usba_reset_hotplug_stats(dev_info_t *dip)
25627c478bd9Sstevel@tonic-gate {
25637c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(dip);
25647c478bd9Sstevel@tonic-gate 	usba_hcdi_t	*hcdi =
25657c478bd9Sstevel@tonic-gate 			    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
25667c478bd9Sstevel@tonic-gate 	hcdi_hotplug_stats_t *hsp;
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	mutex_enter(&hcdi->hcdi_mutex);
25697c478bd9Sstevel@tonic-gate 	hcdi->hcdi_hotplug_success = 0;
25707c478bd9Sstevel@tonic-gate 	hcdi->hcdi_hotplug_failure = 0;
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 	hsp = HCDI_HOTPLUG_STATS_DATA(hcdi);
25737c478bd9Sstevel@tonic-gate 	hsp->hcdi_hotplug_success.value.ui64 = 0;
25747c478bd9Sstevel@tonic-gate 	hsp->hcdi_hotplug_failure.value.ui64 = 0;
25757c478bd9Sstevel@tonic-gate 	mutex_exit(&hcdi->hcdi_mutex);
25767c478bd9Sstevel@tonic-gate }
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate /*
25807c478bd9Sstevel@tonic-gate  * usba_bind_driver():
25817c478bd9Sstevel@tonic-gate  *	This function calls ndi_devi_bind_driver() which tries to
25827c478bd9Sstevel@tonic-gate  *	bind a driver to the device.  If the driver binding fails
25837c478bd9Sstevel@tonic-gate  *	we get an rval of NDI_UNBOUD and report an error to the
25847c478bd9Sstevel@tonic-gate  *	syslog that the driver failed binding.
25857c478bd9Sstevel@tonic-gate  *	If rval is something other than NDI_UNBOUND we report an
25867c478bd9Sstevel@tonic-gate  *	error to the console.
25877c478bd9Sstevel@tonic-gate  *
25887c478bd9Sstevel@tonic-gate  *	This function returns USB_SUCCESS if no errors were
25897c478bd9Sstevel@tonic-gate  *	encountered while binding.
25907c478bd9Sstevel@tonic-gate  */
25917c478bd9Sstevel@tonic-gate int
25927c478bd9Sstevel@tonic-gate usba_bind_driver(dev_info_t *dip)
25937c478bd9Sstevel@tonic-gate {
25947c478bd9Sstevel@tonic-gate 	int	rval;
25957c478bd9Sstevel@tonic-gate 	char	*name;
25967c478bd9Sstevel@tonic-gate 	uint8_t if_num = usba_get_ifno(dip);
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
25997c478bd9Sstevel@tonic-gate 	    "usba_bind_driver: dip = 0x%p, if_num = 0x%x", dip, if_num);
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	/* bind device to the driver */
26047c478bd9Sstevel@tonic-gate 	if ((rval = ndi_devi_bind_driver(dip, 0)) != NDI_SUCCESS) {
26057c478bd9Sstevel@tonic-gate 		/* if we fail to bind report an error */
26067c478bd9Sstevel@tonic-gate 		(void) usba_get_mfg_prod_sn_str(dip, name, MAXNAMELEN);
26077c478bd9Sstevel@tonic-gate 		if (name[0] != '\0') {
26087c478bd9Sstevel@tonic-gate 			if (!usb_owns_device(dip)) {
26097c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L1(DPRINT_MASK_USBA,
26107c478bd9Sstevel@tonic-gate 				    usba_log_handle,
26117c478bd9Sstevel@tonic-gate 				    "no driver found for "
26127c478bd9Sstevel@tonic-gate 				    "interface %d (nodename: '%s') of %s",
26137c478bd9Sstevel@tonic-gate 				    if_num, ddi_node_name(dip), name);
26147c478bd9Sstevel@tonic-gate 			} else {
26157c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L1(DPRINT_MASK_USBA,
26167c478bd9Sstevel@tonic-gate 				    usba_log_handle,
26177c478bd9Sstevel@tonic-gate 				    "no driver found for device %s", name);
26187c478bd9Sstevel@tonic-gate 			}
26197c478bd9Sstevel@tonic-gate 		} else {
26207c478bd9Sstevel@tonic-gate 			(void) ddi_pathname(dip, name);
26217c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA,
26227c478bd9Sstevel@tonic-gate 			    usba_log_handle,
26237c478bd9Sstevel@tonic-gate 			    "no driver found for device %s", name);
26247c478bd9Sstevel@tonic-gate 		}
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 		kmem_free(name, MAXNAMELEN);
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
26297c478bd9Sstevel@tonic-gate 	}
26307c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	return ((rval == NDI_SUCCESS) ? USB_SUCCESS : USB_FAILURE);
26337c478bd9Sstevel@tonic-gate }
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate /*
26377c478bd9Sstevel@tonic-gate  * usba_get_hc_dma_attr:
26387c478bd9Sstevel@tonic-gate  *	function returning dma attributes of the HCD
26397c478bd9Sstevel@tonic-gate  *
26407c478bd9Sstevel@tonic-gate  * Arguments:
26417c478bd9Sstevel@tonic-gate  *	dip	- pointer to devinfo of the client
26427c478bd9Sstevel@tonic-gate  *
26437c478bd9Sstevel@tonic-gate  * Return Values:
26447c478bd9Sstevel@tonic-gate  *	hcdi_dma_attr
26457c478bd9Sstevel@tonic-gate  */
26467c478bd9Sstevel@tonic-gate ddi_dma_attr_t *
26477c478bd9Sstevel@tonic-gate usba_get_hc_dma_attr(dev_info_t *dip)
26487c478bd9Sstevel@tonic-gate {
26497c478bd9Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
26507c478bd9Sstevel@tonic-gate 	usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 	return (hcdi->hcdi_dma_attr);
26537c478bd9Sstevel@tonic-gate }
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate /*
26577c478bd9Sstevel@tonic-gate  * usba_check_for_leaks:
26587c478bd9Sstevel@tonic-gate  *	check usba_device structure for leaks
26597c478bd9Sstevel@tonic-gate  *
26607c478bd9Sstevel@tonic-gate  * Arguments:
26617c478bd9Sstevel@tonic-gate  *	usba_device	- usba_device structure pointer
26627c478bd9Sstevel@tonic-gate  */
26637c478bd9Sstevel@tonic-gate void
26647c478bd9Sstevel@tonic-gate usba_check_for_leaks(usba_device_t *usba_device)
26657c478bd9Sstevel@tonic-gate {
26667c478bd9Sstevel@tonic-gate 	int i, ph_open_cnt, req_wrp_leaks, iface;
26677c478bd9Sstevel@tonic-gate 	int leaks = 0;
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
26707c478bd9Sstevel@tonic-gate 	    "usba_check_for_leaks: %s%d usba_device=0x%p",
26717c478bd9Sstevel@tonic-gate 	    ddi_driver_name(usba_device->usb_dip),
26727c478bd9Sstevel@tonic-gate 	    ddi_get_instance(usba_device->usb_dip), usba_device);
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	/*
26757c478bd9Sstevel@tonic-gate 	 * default pipe is still open
26767c478bd9Sstevel@tonic-gate 	 * all other pipes should be closed
26777c478bd9Sstevel@tonic-gate 	 */
26787c478bd9Sstevel@tonic-gate 	for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) {
26797c478bd9Sstevel@tonic-gate 		usba_ph_impl_t *ph_impl =
26807c478bd9Sstevel@tonic-gate 		    &usba_device->usb_ph_list[i];
26817c478bd9Sstevel@tonic-gate 		if (ph_impl->usba_ph_data) {
26827c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA,
26837c478bd9Sstevel@tonic-gate 			    usba_log_handle,
26847c478bd9Sstevel@tonic-gate 			    "%s%d: leaking pipehandle=0x%p (0x%p) ep_addr=0x%x",
26857c478bd9Sstevel@tonic-gate 			    ddi_driver_name(ph_impl->usba_ph_data->p_dip),
26867c478bd9Sstevel@tonic-gate 			    ddi_get_instance(ph_impl->usba_ph_data->p_dip),
26877c478bd9Sstevel@tonic-gate 			    ph_impl,
26887c478bd9Sstevel@tonic-gate 			    ph_impl->usba_ph_data,
26897c478bd9Sstevel@tonic-gate 			    ph_impl->usba_ph_ep.bEndpointAddress);
26907c478bd9Sstevel@tonic-gate 			ph_open_cnt++;
26917c478bd9Sstevel@tonic-gate 			leaks++;
26927c478bd9Sstevel@tonic-gate #ifndef DEBUG
26937c478bd9Sstevel@tonic-gate 			usb_pipe_close(ph_impl->usba_ph_data->p_dip,
26947c478bd9Sstevel@tonic-gate 			    (usb_pipe_handle_t)ph_impl, USB_FLAGS_SLEEP,
26957c478bd9Sstevel@tonic-gate 			    NULL, NULL);
26967c478bd9Sstevel@tonic-gate #endif
26977c478bd9Sstevel@tonic-gate 		}
26987c478bd9Sstevel@tonic-gate 	}
26997c478bd9Sstevel@tonic-gate 	req_wrp_leaks =  usba_list_entry_leaks(&usba_device->
27007c478bd9Sstevel@tonic-gate 	    usb_allocated, "request wrappers");
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	ASSERT(ph_open_cnt == 0);
27037c478bd9Sstevel@tonic-gate 	ASSERT(req_wrp_leaks == 0);
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 	if (req_wrp_leaks) {
27067c478bd9Sstevel@tonic-gate 		usba_list_entry_t *entry;
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 		while ((entry = usba_rm_first_from_list(
27097c478bd9Sstevel@tonic-gate 		    &usba_device->usb_allocated)) != NULL) {
27107c478bd9Sstevel@tonic-gate 			usba_req_wrapper_t *wrp;
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 			mutex_enter(&entry->list_mutex);
27137c478bd9Sstevel@tonic-gate 			wrp = (usba_req_wrapper_t *)entry->private;
27147c478bd9Sstevel@tonic-gate 			mutex_exit(&entry->list_mutex);
27157c478bd9Sstevel@tonic-gate 			leaks++;
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA,
27187c478bd9Sstevel@tonic-gate 			    usba_log_handle,
27197c478bd9Sstevel@tonic-gate 			    "%s%d: leaking request 0x%p",
27207c478bd9Sstevel@tonic-gate 			    ddi_driver_name(wrp->wr_dip),
27217c478bd9Sstevel@tonic-gate 			    ddi_get_instance(wrp->wr_dip),
27227c478bd9Sstevel@tonic-gate 			    wrp->wr_req);
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 			/*
27257c478bd9Sstevel@tonic-gate 			 * put it back, usba_req_wrapper_free
27267c478bd9Sstevel@tonic-gate 			 * expects it on the list
27277c478bd9Sstevel@tonic-gate 			 */
27287c478bd9Sstevel@tonic-gate 			usba_add_to_list(&usba_device->usb_allocated,
27297c478bd9Sstevel@tonic-gate 			    &wrp->wr_allocated_list);
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate 			usba_req_wrapper_free(wrp);
27327c478bd9Sstevel@tonic-gate 		}
27337c478bd9Sstevel@tonic-gate 	}
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
27367c478bd9Sstevel@tonic-gate 	for (iface = 0; iface < usba_device->usb_n_ifs; iface++) {
27377c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
27387c478bd9Sstevel@tonic-gate 		    "usba_check_for_leaks: if=%d client_flags=0x%x",
27397c478bd9Sstevel@tonic-gate 		    iface, usba_device->usb_client_flags[iface]);
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 		if (usba_device->usb_client_flags[iface] &
27427c478bd9Sstevel@tonic-gate 		    USBA_CLIENT_FLAG_DEV_DATA) {
27437c478bd9Sstevel@tonic-gate 			usb_client_dev_data_list_t *entry =
27447c478bd9Sstevel@tonic-gate 			    usba_device->usb_client_dev_data_list.cddl_next;
27457c478bd9Sstevel@tonic-gate 			usb_client_dev_data_list_t *next;
27467c478bd9Sstevel@tonic-gate 			usb_client_dev_data_t *dev_data;
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 			while (entry) {
27497c478bd9Sstevel@tonic-gate 				dev_info_t *dip = entry->cddl_dip;
27507c478bd9Sstevel@tonic-gate 				next = entry->cddl_next;
27517c478bd9Sstevel@tonic-gate 				dev_data = entry->cddl_dev_data;
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 				if (i_ddi_node_state(dip) < DS_ATTACHED) {
27557c478bd9Sstevel@tonic-gate 					USB_DPRINTF_L1(DPRINT_MASK_USBA,
27567c478bd9Sstevel@tonic-gate 					    usba_log_handle,
27577c478bd9Sstevel@tonic-gate 					    "%s%d: leaking dev_data 0x%p",
27587c478bd9Sstevel@tonic-gate 					    ddi_driver_name(dip),
27597c478bd9Sstevel@tonic-gate 					    ddi_get_instance(dip),
27607c478bd9Sstevel@tonic-gate 					    (void *)dev_data);
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 					leaks++;
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 					mutex_exit(&usba_device->usb_mutex);
27657c478bd9Sstevel@tonic-gate 					usb_free_dev_data(dip, dev_data);
27667c478bd9Sstevel@tonic-gate 					mutex_enter(&usba_device->usb_mutex);
27677c478bd9Sstevel@tonic-gate 				}
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 				entry = next;
27707c478bd9Sstevel@tonic-gate 			}
27717c478bd9Sstevel@tonic-gate 		}
27727c478bd9Sstevel@tonic-gate 		if (usba_device->usb_client_flags[iface] &
27737c478bd9Sstevel@tonic-gate 		    USBA_CLIENT_FLAG_ATTACH) {
27747c478bd9Sstevel@tonic-gate 			dev_info_t *dip = usba_device->
27757c478bd9Sstevel@tonic-gate 				usb_client_attach_list[iface].dip;
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA,
27787c478bd9Sstevel@tonic-gate 			    usba_log_handle,
27797c478bd9Sstevel@tonic-gate 			    "%s%d: did no usb_client_detach",
27807c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
27817c478bd9Sstevel@tonic-gate 			leaks++;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
27847c478bd9Sstevel@tonic-gate 			usb_client_detach(dip, NULL);
27857c478bd9Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 			usba_device->
27887c478bd9Sstevel@tonic-gate 			    usb_client_attach_list[iface].dip = NULL;
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 			usba_device->usb_client_flags[iface] &=
27917c478bd9Sstevel@tonic-gate 			    ~USBA_CLIENT_FLAG_ATTACH;
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 		}
27947c478bd9Sstevel@tonic-gate 		if (usba_device->usb_client_flags[iface] &
27957c478bd9Sstevel@tonic-gate 		    USBA_CLIENT_FLAG_EV_CBS) {
27967c478bd9Sstevel@tonic-gate 			dev_info_t *dip =
27977c478bd9Sstevel@tonic-gate 			    usba_device->usb_client_ev_cb_list[iface].
27987c478bd9Sstevel@tonic-gate 							dip;
27997c478bd9Sstevel@tonic-gate 			usb_event_t *ev_data =
28007c478bd9Sstevel@tonic-gate 			    usba_device->usb_client_ev_cb_list[iface].
28017c478bd9Sstevel@tonic-gate 							ev_data;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA,
28047c478bd9Sstevel@tonic-gate 			    usba_log_handle,
28057c478bd9Sstevel@tonic-gate 			    "%s%d: did no usb_unregister_event_cbs",
28067c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
28077c478bd9Sstevel@tonic-gate 			leaks++;
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
28107c478bd9Sstevel@tonic-gate 			usb_unregister_event_cbs(dip, ev_data);
28117c478bd9Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 			usba_device->usb_client_ev_cb_list[iface].
28147c478bd9Sstevel@tonic-gate 					dip = NULL;
28157c478bd9Sstevel@tonic-gate 			usba_device->usb_client_ev_cb_list[iface].
28167c478bd9Sstevel@tonic-gate 					ev_data = NULL;
28177c478bd9Sstevel@tonic-gate 			usba_device->usb_client_flags[iface] &=
28187c478bd9Sstevel@tonic-gate 			    ~USBA_CLIENT_FLAG_EV_CBS;
28197c478bd9Sstevel@tonic-gate 		}
28207c478bd9Sstevel@tonic-gate 	}
28217c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	if (leaks) {
28247c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L1(DPRINT_MASK_USBA, usba_log_handle,
28257c478bd9Sstevel@tonic-gate 		    "all %d leaks fixed", leaks);
28267c478bd9Sstevel@tonic-gate 	}
28277c478bd9Sstevel@tonic-gate }
2828