xref: /onnv-gate/usr/src/uts/common/io/usb/usba/usba.c (revision 9094:77f003128c4d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52257Slg150142  * Common Development and Distribution License (the "License").
62257Slg150142  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9094SVincent.Wang@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * USBA: Solaris USB Architecture support
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate #define	USBA_FRAMEWORK
310Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
320Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
330Sstevel@tonic-gate #include <sys/usb/hubd/hub.h>
340Sstevel@tonic-gate #include <sys/fs/dv_node.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate static int usba_str_startcmp(char *, char *);
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * USBA private variables and tunables
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate static kmutex_t	usba_mutex;
420Sstevel@tonic-gate 
43*9094SVincent.Wang@Sun.COM /* mutex to protect usba_root_hubs */
44*9094SVincent.Wang@Sun.COM static kmutex_t usba_hub_mutex;
45*9094SVincent.Wang@Sun.COM 
46*9094SVincent.Wang@Sun.COM typedef struct usba_root_hub_ent {
47*9094SVincent.Wang@Sun.COM 	dev_info_t *dip;
48*9094SVincent.Wang@Sun.COM 	struct usba_root_hub_ent *next;
49*9094SVincent.Wang@Sun.COM }usba_root_hub_ent_t;
50*9094SVincent.Wang@Sun.COM 
51*9094SVincent.Wang@Sun.COM static usba_root_hub_ent_t *usba_root_hubs = NULL;
52*9094SVincent.Wang@Sun.COM 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * ddivs forced binding:
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *    usbc usbc_xhubs usbc_xaddress  node name
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *	0	x	x	class name or "device"
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *	1	0	0	ddivs_usbc
610Sstevel@tonic-gate  *	1	0	>1	ddivs_usbc except device
620Sstevel@tonic-gate  *				at usbc_xaddress
630Sstevel@tonic-gate  *	1	1	0	ddivs_usbc except hubs
640Sstevel@tonic-gate  *	1	1	>1	ddivs_usbc except hubs and
650Sstevel@tonic-gate  *				device at usbc_xaddress
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate uint_t usba_ddivs_usbc;
680Sstevel@tonic-gate uint_t usba_ddivs_usbc_xhubs;
690Sstevel@tonic-gate uint_t usba_ddivs_usbc_xaddress;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate uint_t usba_ugen_force_binding;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * compatible name handling
750Sstevel@tonic-gate  */
768382SRaymond.Chen@Sun.COM /* allowing for 15 compat names, plus one force bind name */
778382SRaymond.Chen@Sun.COM #define	USBA_MAX_COMPAT_NAMES		16
780Sstevel@tonic-gate #define	USBA_MAX_COMPAT_NAME_LEN	64
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* double linked list for usba_devices */
810Sstevel@tonic-gate usba_list_entry_t	usba_device_list;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate _NOTE(MUTEX_PROTECTS_DATA(usba_mutex, usba_device_list))
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * modload support
870Sstevel@tonic-gate  */
887767SJohn.Beck@Sun.COM 
897767SJohn.Beck@Sun.COM static struct modlmisc modlmisc	= {
900Sstevel@tonic-gate 	&mod_miscops,	/* Type	of module */
913341Sgc161489 	"USBA: USB Architecture 2.0 1.66"
920Sstevel@tonic-gate };
930Sstevel@tonic-gate 
947767SJohn.Beck@Sun.COM static struct modlinkage modlinkage = {
950Sstevel@tonic-gate 	MODREV_1, (void	*)&modlmisc, NULL
960Sstevel@tonic-gate };
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static usb_log_handle_t	usba_log_handle;
100880Sfrits uint_t		usba_errlevel = USB_LOG_L4;
101880Sfrits uint_t		usba_errmask = (uint_t)-1;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate extern usb_log_handle_t	hubdi_log_handle;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate int
1060Sstevel@tonic-gate _init(void)
1070Sstevel@tonic-gate {
1083630Slg150142 	int rval;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/*
1110Sstevel@tonic-gate 	 * usbai providing log support needs to be init'ed first
1120Sstevel@tonic-gate 	 * and destroyed last
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 	usba_usbai_initialization();
1150Sstevel@tonic-gate 	usba_usba_initialization();
1160Sstevel@tonic-gate 	usba_usbai_register_initialization();
1170Sstevel@tonic-gate 	usba_hcdi_initialization();
1180Sstevel@tonic-gate 	usba_hubdi_initialization();
1190Sstevel@tonic-gate 	usba_devdb_initialization();
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if ((rval = mod_install(&modlinkage)) != 0) {
1220Sstevel@tonic-gate 		usba_devdb_destroy();
1230Sstevel@tonic-gate 		usba_hubdi_destroy();
1240Sstevel@tonic-gate 		usba_hcdi_destroy();
1250Sstevel@tonic-gate 		usba_usbai_register_destroy();
1260Sstevel@tonic-gate 		usba_usba_destroy();
1270Sstevel@tonic-gate 		usba_usbai_destroy();
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	return (rval);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate int
1340Sstevel@tonic-gate _fini()
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	int rval;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if ((rval = mod_remove(&modlinkage)) == 0) {
1390Sstevel@tonic-gate 		usba_devdb_destroy();
1400Sstevel@tonic-gate 		usba_hubdi_destroy();
1410Sstevel@tonic-gate 		usba_hcdi_destroy();
1420Sstevel@tonic-gate 		usba_usbai_register_destroy();
1430Sstevel@tonic-gate 		usba_usba_destroy();
1440Sstevel@tonic-gate 		usba_usbai_destroy();
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	return (rval);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate int
1510Sstevel@tonic-gate _info(struct modinfo *modinfop)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1563341Sgc161489 boolean_t
1573341Sgc161489 usba_owns_ia(dev_info_t *dip)
1583341Sgc161489 {
1593341Sgc161489 	int if_count = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1603341Sgc161489 	    "interface-count", 0);
1613341Sgc161489 
1623341Sgc161489 	return ((if_count) ? B_TRUE : B_FALSE);
1633341Sgc161489 }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * common bus ctl for hcd, usb_mid, and hubd
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate int
1690Sstevel@tonic-gate usba_bus_ctl(dev_info_t	*dip,
1700Sstevel@tonic-gate 	dev_info_t		*rdip,
1710Sstevel@tonic-gate 	ddi_ctl_enum_t		op,
1720Sstevel@tonic-gate 	void			*arg,
1730Sstevel@tonic-gate 	void			*result)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	dev_info_t		*child_dip = (dev_info_t *)arg;
1760Sstevel@tonic-gate 	usba_device_t		*usba_device;
1770Sstevel@tonic-gate 	usba_hcdi_t		*usba_hcdi;
1780Sstevel@tonic-gate 	usba_hcdi_ops_t		*usba_hcdi_ops;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, hubdi_log_handle,
1810Sstevel@tonic-gate 	    "usba_bus_ctl: %s%d %s%d op=%d", ddi_node_name(rdip),
1820Sstevel@tonic-gate 	    ddi_get_instance(rdip), ddi_node_name(dip),
1830Sstevel@tonic-gate 	    ddi_get_instance(dip), op);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	switch (op) {
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
1880Sstevel@tonic-gate 	{
1890Sstevel@tonic-gate 		char *name, compat_name[64], *speed;
1900Sstevel@tonic-gate 		usba_device_t	*hub_usba_device;
1910Sstevel@tonic-gate 		dev_info_t	*hubdip;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		usba_device = usba_get_usba_device(rdip);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		/* find the parent hub */
1960Sstevel@tonic-gate 		hubdip = ddi_get_parent(rdip);
1970Sstevel@tonic-gate 		while ((strcmp(ddi_driver_name(hubdip), "hubd") != 0) &&
1980Sstevel@tonic-gate 		    !(usba_is_root_hub(hubdip))) {
1990Sstevel@tonic-gate 			hubdip = ddi_get_parent(hubdip);
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		hub_usba_device = usba_get_usba_device(hubdip);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		if (usba_device) {
2050Sstevel@tonic-gate 			if (usb_owns_device(rdip)) {
2060Sstevel@tonic-gate 				(void) snprintf(compat_name,
2070Sstevel@tonic-gate 				    sizeof (compat_name),
2080Sstevel@tonic-gate 				    "usb%x,%x",
2090Sstevel@tonic-gate 				    usba_device->usb_dev_descr->idVendor,
2100Sstevel@tonic-gate 				    usba_device->usb_dev_descr->idProduct);
2113341Sgc161489 			} else if (usba_owns_ia(rdip)) {
2123341Sgc161489 				(void) snprintf(compat_name,
2133341Sgc161489 				    sizeof (compat_name),
2143341Sgc161489 				    "usbia%x,%x.config%x.%x",
2153341Sgc161489 				    usba_device->usb_dev_descr->idVendor,
2163341Sgc161489 				    usba_device->usb_dev_descr->idProduct,
2173341Sgc161489 				    usba_device->usb_cfg_value,
2183341Sgc161489 				    usb_get_if_number(rdip));
2190Sstevel@tonic-gate 			} else {
2200Sstevel@tonic-gate 				(void) snprintf(compat_name,
2210Sstevel@tonic-gate 				    sizeof (compat_name),
2220Sstevel@tonic-gate 				    "usbif%x,%x.config%x.%x",
2230Sstevel@tonic-gate 				    usba_device->usb_dev_descr->idVendor,
2240Sstevel@tonic-gate 				    usba_device->usb_dev_descr->idProduct,
2250Sstevel@tonic-gate 				    usba_device->usb_cfg_value,
2260Sstevel@tonic-gate 				    usb_get_if_number(rdip));
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 			switch (usba_device->usb_port_status) {
2290Sstevel@tonic-gate 			case USBA_HIGH_SPEED_DEV:
2300Sstevel@tonic-gate 				speed = "hi speed (USB 2.x)";
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 				break;
2330Sstevel@tonic-gate 			case USBA_LOW_SPEED_DEV:
2340Sstevel@tonic-gate 				speed = "low speed (USB 1.x)";
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 				break;
2370Sstevel@tonic-gate 			case USBA_FULL_SPEED_DEV:
2380Sstevel@tonic-gate 			default:
2390Sstevel@tonic-gate 				speed = "full speed (USB 1.x)";
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 				break;
2420Sstevel@tonic-gate 			}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 			cmn_err(CE_CONT,
2450Sstevel@tonic-gate 			    "?USB %x.%x %s (%s) operating at %s on "
2460Sstevel@tonic-gate 			    "USB %x.%x %s hub: "
2470Sstevel@tonic-gate 			    "%s@%s, %s%d at bus address %d\n",
2480Sstevel@tonic-gate 			    (usba_device->usb_dev_descr->bcdUSB & 0xff00) >> 8,
2490Sstevel@tonic-gate 			    usba_device->usb_dev_descr->bcdUSB & 0xff,
2503341Sgc161489 			    (usb_owns_device(rdip) ? "device" :
2513341Sgc161489 			    ((usba_owns_ia(rdip) ? "interface-association" :
2523341Sgc161489 			    "interface"))),
2530Sstevel@tonic-gate 			    compat_name, speed,
2540Sstevel@tonic-gate 			    (hub_usba_device->usb_dev_descr->bcdUSB &
2550Sstevel@tonic-gate 			    0xff00) >> 8,
2560Sstevel@tonic-gate 			    hub_usba_device->usb_dev_descr->bcdUSB & 0xff,
2570Sstevel@tonic-gate 			    usba_is_root_hub(hubdip) ? "root" : "external",
2580Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_name_addr(rdip),
2590Sstevel@tonic-gate 			    ddi_driver_name(rdip),
2600Sstevel@tonic-gate 			    ddi_get_instance(rdip), usba_device->usb_addr);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 			name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2630Sstevel@tonic-gate 			(void) usba_get_mfg_prod_sn_str(rdip, name, MAXNAMELEN);
2640Sstevel@tonic-gate 			if (name[0] != '\0') {
2650Sstevel@tonic-gate 				cmn_err(CE_CONT, "?\t%s\n", name);
2660Sstevel@tonic-gate 			}
2670Sstevel@tonic-gate 			kmem_free(name, MAXNAMELEN);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 		} else { /* harden USBA against this case; if it happens */
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 			cmn_err(CE_CONT,
2720Sstevel@tonic-gate 			    "?USB-device: %s@%s, %s%d\n",
2730Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_name_addr(rdip),
2740Sstevel@tonic-gate 			    ddi_driver_name(rdip), ddi_get_instance(rdip));
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		return (DDI_SUCCESS);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
2810Sstevel@tonic-gate 	{
2820Sstevel@tonic-gate 		int			usb_addr;
2830Sstevel@tonic-gate 		uint_t			n;
2840Sstevel@tonic-gate 		char			name[32];
2850Sstevel@tonic-gate 		int			*data;
2860Sstevel@tonic-gate 		int			rval;
2870Sstevel@tonic-gate 		int			len = sizeof (usb_addr);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		usba_hcdi	= usba_hcdi_get_hcdi(dip);
2900Sstevel@tonic-gate 		usba_hcdi_ops	= usba_hcdi->hcdi_ops;
2910Sstevel@tonic-gate 		ASSERT(usba_hcdi_ops != NULL);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		/*
2940Sstevel@tonic-gate 		 * as long as the dip exists, it should have
2950Sstevel@tonic-gate 		 * usba_device structure associated with it
2960Sstevel@tonic-gate 		 */
2970Sstevel@tonic-gate 		usba_device = usba_get_usba_device(child_dip);
2980Sstevel@tonic-gate 		if (usba_device == NULL) {
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
3010Sstevel@tonic-gate 			    "usba_bus_ctl: DDI_NOT_WELL_FORMED (%s (0x%p))",
3020Sstevel@tonic-gate 			    ddi_node_name(child_dip), (void *)child_dip);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		/* the dip should have an address and reg property */
3080Sstevel@tonic-gate 		if (ddi_prop_op(DDI_DEV_T_NONE, child_dip, PROP_LEN_AND_VAL_BUF,
3090Sstevel@tonic-gate 		    DDI_PROP_DONTPASS |	DDI_PROP_CANSLEEP, "assigned-address",
3100Sstevel@tonic-gate 		    (caddr_t)&usb_addr,	&len) != DDI_SUCCESS) {
3110Sstevel@tonic-gate 
312978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
3130Sstevel@tonic-gate 			    "usba_bus_ctl:\n\t"
3140Sstevel@tonic-gate 			    "%s%d %s%d op=%d rdip = 0x%p dip = 0x%p",
3150Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_instance(rdip),
3160Sstevel@tonic-gate 			    ddi_node_name(dip), ddi_get_instance(dip), op,
3176898Sfb209375 			    (void *)rdip, (void *)dip);
3180Sstevel@tonic-gate 
319978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
3200Sstevel@tonic-gate 			    "usba_bus_ctl: DDI_NOT_WELL_FORMED (%s (0x%p))",
3210Sstevel@tonic-gate 			    ddi_node_name(child_dip), (void *)child_dip);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
3240Sstevel@tonic-gate 		}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 		if ((rval = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child_dip,
3270Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg",
3280Sstevel@tonic-gate 		    &data, &n)) != DDI_SUCCESS) {
3290Sstevel@tonic-gate 
330978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA, hubdi_log_handle,
3310Sstevel@tonic-gate 			    "usba_bus_ctl: %d, DDI_NOT_WELL_FORMED", rval);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
3340Sstevel@tonic-gate 		}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		/*
3380Sstevel@tonic-gate 		 * if the configuration is 1, the unit address is
3390Sstevel@tonic-gate 		 * just the interface number
3400Sstevel@tonic-gate 		 */
3410Sstevel@tonic-gate 		if ((n == 1) || ((n > 1) && (data[1] == 1))) {
3420Sstevel@tonic-gate 			(void) sprintf(name, "%x", data[0]);
3430Sstevel@tonic-gate 		} else {
3440Sstevel@tonic-gate 			(void) sprintf(name, "%x,%x", data[0], data[1]);
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA,
3480Sstevel@tonic-gate 		    hubdi_log_handle, "usba_bus_ctl: name = %s", name);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		ddi_prop_free(data);
3510Sstevel@tonic-gate 		ddi_set_name_addr(child_dip, name);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		/*
3540Sstevel@tonic-gate 		 * increment the reference count for each child using this
3550Sstevel@tonic-gate 		 * usba_device structure
3560Sstevel@tonic-gate 		 */
3570Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
3580Sstevel@tonic-gate 		usba_device->usb_ref_count++;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, hubdi_log_handle,
3610Sstevel@tonic-gate 		    "usba_bus_ctl: init usba_device = 0x%p ref_count = %d",
3620Sstevel@tonic-gate 		    (void *)usba_device, usba_device->usb_ref_count);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 		return (DDI_SUCCESS);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
3700Sstevel@tonic-gate 	{
3710Sstevel@tonic-gate 		usba_device = usba_get_usba_device(child_dip);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		if (usba_device != NULL) {
3740Sstevel@tonic-gate 			/*
3750Sstevel@tonic-gate 			 * decrement the reference count for each child
3760Sstevel@tonic-gate 			 * using this  usba_device structure
3770Sstevel@tonic-gate 			 */
3780Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
3790Sstevel@tonic-gate 			usba_device->usb_ref_count--;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_USBA, hubdi_log_handle,
3820Sstevel@tonic-gate 			    "usba_hcdi_bus_ctl: uninit usba_device=0x%p "
3830Sstevel@tonic-gate 			    "ref_count=%d",
3846898Sfb209375 			    (void *)usba_device, usba_device->usb_ref_count);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 		ddi_set_name_addr(child_dip, NULL);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		return (DDI_SUCCESS);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
3940Sstevel@tonic-gate 		/* Do nothing */
3950Sstevel@tonic-gate 		return (DDI_SUCCESS);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * These ops correspond	to functions that "shouldn't" be called
3990Sstevel@tonic-gate 	 * by a	USB client driver.  So	we whine when we're called.
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	case DDI_CTLOPS_DMAPMAPC:
4020Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTINT:
4030Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
4040Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
4050Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
4060Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
4070Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
4080Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
4090Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:
4100Sstevel@tonic-gate 		cmn_err(CE_CONT, "%s%d:	invalid	op (%d)	from %s%d",
4116112Sqz150045 		    ddi_node_name(dip), ddi_get_instance(dip),
4126112Sqz150045 		    op, ddi_node_name(rdip), ddi_get_instance(rdip));
4130Sstevel@tonic-gate 		return (DDI_FAILURE);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	/*
4160Sstevel@tonic-gate 	 * Everything else (e.g. PTOB/BTOP/BTOPR requests) we pass up
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 	default:
4190Sstevel@tonic-gate 		return (ddi_ctlops(dip,	rdip, op, arg, result));
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate  * initialize and destroy USBA module
4260Sstevel@tonic-gate  */
4270Sstevel@tonic-gate void
4280Sstevel@tonic-gate usba_usba_initialization()
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	usba_log_handle = usb_alloc_log_hdl(NULL, "usba", &usba_errlevel,
4316112Sqz150045 	    &usba_errmask, NULL, 0);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA,
4340Sstevel@tonic-gate 	    usba_log_handle, "usba_usba_initialization");
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	mutex_init(&usba_mutex, NULL, MUTEX_DRIVER, NULL);
437*9094SVincent.Wang@Sun.COM 	mutex_init(&usba_hub_mutex, NULL, MUTEX_DRIVER, NULL);
4380Sstevel@tonic-gate 	usba_init_list(&usba_device_list, NULL, NULL);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate void
4430Sstevel@tonic-gate usba_usba_destroy()
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle, "usba_usba_destroy");
4460Sstevel@tonic-gate 
447*9094SVincent.Wang@Sun.COM 	mutex_destroy(&usba_hub_mutex);
4480Sstevel@tonic-gate 	mutex_destroy(&usba_mutex);
4490Sstevel@tonic-gate 	usba_destroy_list(&usba_device_list);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	usb_free_log_hdl(usba_log_handle);
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate  * usba_set_usb_address:
4570Sstevel@tonic-gate  *	set usb address in usba_device structure
4580Sstevel@tonic-gate  */
4590Sstevel@tonic-gate int
4600Sstevel@tonic-gate usba_set_usb_address(usba_device_t *usba_device)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	usb_addr_t address;
4630Sstevel@tonic-gate 	uchar_t s = 8;
4640Sstevel@tonic-gate 	usba_hcdi_t *hcdi;
4650Sstevel@tonic-gate 	char *usb_address_in_use;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	mutex_enter(&hcdi->hcdi_mutex);
4720Sstevel@tonic-gate 	usb_address_in_use = hcdi->hcdi_usb_address_in_use;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	for (address = ROOT_HUB_ADDR + 1;
4750Sstevel@tonic-gate 	    address <= USBA_MAX_ADDRESS; address++) {
4760Sstevel@tonic-gate 		if (usb_address_in_use[address/s] & (1 << (address % s))) {
4770Sstevel@tonic-gate 			continue;
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 		usb_address_in_use[address/s] |= (1 << (address % s));
4800Sstevel@tonic-gate 		hcdi->hcdi_device_count++;
4810Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->hcdi_device_count.value.ui64++;
4820Sstevel@tonic-gate 		mutex_exit(&hcdi->hcdi_mutex);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
4850Sstevel@tonic-gate 		    "usba_set_usb_address: %d", address);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		usba_device->usb_addr = address;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 		return (USB_SUCCESS);
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	usba_device->usb_addr = 0;
4950Sstevel@tonic-gate 
496978Sfrits 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
4970Sstevel@tonic-gate 	    "no usb address available");
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	mutex_exit(&hcdi->hcdi_mutex);
5000Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	return (USB_FAILURE);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate  * usba_unset_usb_address:
5080Sstevel@tonic-gate  *	unset usb_address in usba_device structure
5090Sstevel@tonic-gate  */
5100Sstevel@tonic-gate void
5110Sstevel@tonic-gate usba_unset_usb_address(usba_device_t *usba_device)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	usb_addr_t address;
5140Sstevel@tonic-gate 	usba_hcdi_t *hcdi;
5150Sstevel@tonic-gate 	uchar_t s = 8;
5160Sstevel@tonic-gate 	char *usb_address_in_use;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
5190Sstevel@tonic-gate 	address = usba_device->usb_addr;
5200Sstevel@tonic-gate 	hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (address > ROOT_HUB_ADDR) {
5230Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
5240Sstevel@tonic-gate 		    "usba_unset_usb_address: address=%d", address);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 		mutex_enter(&hcdi->hcdi_mutex);
5270Sstevel@tonic-gate 		usb_address_in_use = hcdi->hcdi_usb_address_in_use;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		ASSERT(usb_address_in_use[address/s] & (1 << (address % s)));
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 		usb_address_in_use[address/s] &= ~(1 << (address % s));
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		hcdi->hcdi_device_count--;
5340Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->hcdi_device_count.value.ui64--;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		mutex_exit(&hcdi->hcdi_mutex);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		usba_device->usb_addr = 0;
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate struct usba_evdata *
5450Sstevel@tonic-gate usba_get_evdata(dev_info_t *dip)
5460Sstevel@tonic-gate {
5470Sstevel@tonic-gate 	usba_evdata_t *evdata;
5480Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	/* called when dip attaches */
5510Sstevel@tonic-gate 	ASSERT(usba_device != NULL);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
5540Sstevel@tonic-gate 	evdata = usba_device->usb_evdata;
5550Sstevel@tonic-gate 	while (evdata) {
5560Sstevel@tonic-gate 		if (evdata->ev_dip == dip) {
5570Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 			return (evdata);
5600Sstevel@tonic-gate 		}
5610Sstevel@tonic-gate 		evdata = evdata->ev_next;
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	evdata = kmem_zalloc(sizeof (usba_evdata_t), KM_SLEEP);
5650Sstevel@tonic-gate 	evdata->ev_dip = dip;
5660Sstevel@tonic-gate 	evdata->ev_next = usba_device->usb_evdata;
5670Sstevel@tonic-gate 	usba_device->usb_evdata = evdata;
5680Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	return (evdata);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate  * allocate a usb device structure and link it in the list
5760Sstevel@tonic-gate  */
5770Sstevel@tonic-gate usba_device_t *
5780Sstevel@tonic-gate usba_alloc_usba_device(dev_info_t *root_hub_dip)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	usba_device_t	*usba_device;
5810Sstevel@tonic-gate 	int		ep_idx;
5820Sstevel@tonic-gate 	ddi_iblock_cookie_t iblock_cookie =
5830Sstevel@tonic-gate 	    usba_hcdi_get_hcdi(root_hub_dip)->hcdi_iblock_cookie;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * create a new usba_device structure
5870Sstevel@tonic-gate 	 */
5880Sstevel@tonic-gate 	usba_device = kmem_zalloc(sizeof (usba_device_t), KM_SLEEP);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/*
5910Sstevel@tonic-gate 	 * initialize usba_device
5920Sstevel@tonic-gate 	 */
5930Sstevel@tonic-gate 	mutex_init(&usba_device->usb_mutex, NULL, MUTEX_DRIVER,
5946112Sqz150045 	    iblock_cookie);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	usba_init_list(&usba_device->usb_device_list, (usb_opaque_t)usba_device,
5976112Sqz150045 	    iblock_cookie);
5980Sstevel@tonic-gate 	usba_init_list(&usba_device->usb_allocated, (usb_opaque_t)usba_device,
5996112Sqz150045 	    iblock_cookie);
6000Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
6010Sstevel@tonic-gate 	usba_device->usb_root_hub_dip = root_hub_dip;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	/*
6040Sstevel@tonic-gate 	 * add to list of usba_devices
6050Sstevel@tonic-gate 	 */
6060Sstevel@tonic-gate 	usba_add_to_list(&usba_device_list, &usba_device->usb_device_list);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	/* init mutex in each usba_ph_impl structure */
6090Sstevel@tonic-gate 	for (ep_idx = 0; ep_idx < USBA_N_ENDPOINTS; ep_idx++) {
6100Sstevel@tonic-gate 		mutex_init(&usba_device->usb_ph_list[ep_idx].usba_ph_mutex,
6110Sstevel@tonic-gate 		    NULL, MUTEX_DRIVER, iblock_cookie);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
6150Sstevel@tonic-gate 	    "allocated usba_device 0x%p", (void *)usba_device);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	return (usba_device);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /* free NDI event data associated with usba_device */
6240Sstevel@tonic-gate void
6250Sstevel@tonic-gate usba_free_evdata(usba_evdata_t *evdata)
6260Sstevel@tonic-gate {
6270Sstevel@tonic-gate 	usba_evdata_t *next;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	while (evdata) {
6300Sstevel@tonic-gate 		next = evdata->ev_next;
6310Sstevel@tonic-gate 		kmem_free(evdata, sizeof (usba_evdata_t));
6320Sstevel@tonic-gate 		evdata = next;
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate  * free usb device structure
6390Sstevel@tonic-gate  */
6400Sstevel@tonic-gate void
6410Sstevel@tonic-gate usba_free_usba_device(usba_device_t *usba_device)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 	int			i, ep_idx;
6440Sstevel@tonic-gate 	usb_pipe_handle_t	def_ph;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	if (usba_device == NULL) {
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		return;
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
6520Sstevel@tonic-gate 	if (usba_device->usb_ref_count) {
6530Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		return;
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
6590Sstevel@tonic-gate 	    "usba_free_usba_device 0x%p, address=0x%x, ref cnt=%d",
6600Sstevel@tonic-gate 	    (void *)usba_device, usba_device->usb_addr,
6610Sstevel@tonic-gate 	    usba_device->usb_ref_count);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	usba_free_evdata(usba_device->usb_evdata);
6640Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	def_ph = usba_usbdev_to_dflt_pipe_handle(usba_device);
6670Sstevel@tonic-gate 	if (def_ph != NULL) {
6680Sstevel@tonic-gate 		usba_pipe_handle_data_t	*ph_data = usba_get_ph_data(def_ph);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 		if (ph_data) {
6710Sstevel@tonic-gate 			usb_pipe_close(ph_data->p_dip, def_ph,
6720Sstevel@tonic-gate 			    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED,
6730Sstevel@tonic-gate 			    NULL, NULL);
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	mutex_enter(&usba_mutex);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/* destroy mutex in each usba_ph_impl structure */
6800Sstevel@tonic-gate 	for (ep_idx = 0; ep_idx < USBA_N_ENDPOINTS; ep_idx++) {
6810Sstevel@tonic-gate 		mutex_destroy(&usba_device->usb_ph_list[ep_idx].usba_ph_mutex);
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	(void) usba_rm_from_list(&usba_device_list,
6856112Sqz150045 	    &usba_device->usb_device_list);
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	mutex_exit(&usba_mutex);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	usba_destroy_list(&usba_device->usb_device_list);
6900Sstevel@tonic-gate 	usba_destroy_list(&usba_device->usb_allocated);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
6930Sstevel@tonic-gate 	    "deallocating usba_device = 0x%p, address = 0x%x",
6940Sstevel@tonic-gate 	    (void *)usba_device, usba_device->usb_addr);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * ohci allocates descriptors for root hub so we can't
6980Sstevel@tonic-gate 	 * deallocate these here
6990Sstevel@tonic-gate 	 */
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (usba_device->usb_addr != ROOT_HUB_ADDR) {
7020Sstevel@tonic-gate 		if (usba_device->usb_cfg_array) {
7030Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
7040Sstevel@tonic-gate 			    "deallocating usb_config_array: 0x%p",
7056898Sfb209375 			    (void *)usba_device->usb_cfg_array);
7060Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
7070Sstevel@tonic-gate 			for (i = 0;
7080Sstevel@tonic-gate 			    i < usba_device->usb_dev_descr->bNumConfigurations;
7090Sstevel@tonic-gate 			    i++) {
7100Sstevel@tonic-gate 				if (usba_device->usb_cfg_array[i]) {
7110Sstevel@tonic-gate 					kmem_free(
7120Sstevel@tonic-gate 					    usba_device->usb_cfg_array[i],
7130Sstevel@tonic-gate 					    usba_device->usb_cfg_array_len[i]);
7140Sstevel@tonic-gate 				}
7150Sstevel@tonic-gate 			}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 			/* free the array pointers */
7180Sstevel@tonic-gate 			kmem_free(usba_device->usb_cfg_array,
7190Sstevel@tonic-gate 			    usba_device->usb_cfg_array_length);
7200Sstevel@tonic-gate 			kmem_free(usba_device->usb_cfg_array_len,
7210Sstevel@tonic-gate 			    usba_device->usb_cfg_array_len_length);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		if (usba_device->usb_cfg_str_descr) {
7270Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
7280Sstevel@tonic-gate 			    "deallocating usb_cfg_str_descr: 0x%p",
7296898Sfb209375 			    (void *)usba_device->usb_cfg_str_descr);
7300Sstevel@tonic-gate 			for (i = 0;
7310Sstevel@tonic-gate 			    i < usba_device->usb_dev_descr->bNumConfigurations;
7320Sstevel@tonic-gate 			    i++) {
7330Sstevel@tonic-gate 				if (usba_device->usb_cfg_str_descr[i]) {
7340Sstevel@tonic-gate 					kmem_free(
7350Sstevel@tonic-gate 					    usba_device->usb_cfg_str_descr[i],
7360Sstevel@tonic-gate 					    strlen(usba_device->
7376112Sqz150045 					    usb_cfg_str_descr[i]) + 1);
7380Sstevel@tonic-gate 				}
7390Sstevel@tonic-gate 			}
7400Sstevel@tonic-gate 			/* free the array pointers */
7410Sstevel@tonic-gate 			kmem_free(usba_device->usb_cfg_str_descr,
7420Sstevel@tonic-gate 			    sizeof (uchar_t *) * usba_device->usb_n_cfgs);
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 		if (usba_device->usb_dev_descr) {
7460Sstevel@tonic-gate 			kmem_free(usba_device->usb_dev_descr,
7476112Sqz150045 			    sizeof (usb_dev_descr_t));
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 		if (usba_device->usb_mfg_str) {
7510Sstevel@tonic-gate 			kmem_free(usba_device->usb_mfg_str,
7526112Sqz150045 			    strlen(usba_device->usb_mfg_str) + 1);
7530Sstevel@tonic-gate 		}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 		if (usba_device->usb_product_str) {
7560Sstevel@tonic-gate 			kmem_free(usba_device->usb_product_str,
7576112Sqz150045 			    strlen(usba_device->usb_product_str) + 1);
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		if (usba_device->usb_serialno_str) {
7610Sstevel@tonic-gate 			kmem_free(usba_device->usb_serialno_str,
7626112Sqz150045 			    strlen(usba_device->usb_serialno_str) + 1);
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		usba_unset_usb_address(usba_device);
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate #ifndef __lock_lint
7690Sstevel@tonic-gate 	ASSERT(usba_device->usb_client_dev_data_list.cddl_next == NULL);
7700Sstevel@tonic-gate #endif
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (usba_device->usb_client_flags) {
7730Sstevel@tonic-gate #ifndef __lock_lint
7740Sstevel@tonic-gate 		int i;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 		for (i = 0; i < usba_device->usb_n_ifs; i++) {
7770Sstevel@tonic-gate 			ASSERT(usba_device->usb_client_flags[i] == 0);
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate #endif
7800Sstevel@tonic-gate 		kmem_free(usba_device->usb_client_flags,
7810Sstevel@tonic-gate 		    usba_device->usb_n_ifs * USBA_CLIENT_FLAG_SIZE);
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	if (usba_device->usb_client_attach_list) {
7860Sstevel@tonic-gate 		kmem_free(usba_device->usb_client_attach_list,
7870Sstevel@tonic-gate 		    usba_device->usb_n_ifs *
7880Sstevel@tonic-gate 		    sizeof (*usba_device->usb_client_attach_list));
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	if (usba_device->usb_client_ev_cb_list) {
7910Sstevel@tonic-gate 		kmem_free(usba_device->usb_client_ev_cb_list,
7920Sstevel@tonic-gate 		    usba_device->usb_n_ifs *
7930Sstevel@tonic-gate 		    sizeof (*usba_device->usb_client_ev_cb_list));
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/*
7970Sstevel@tonic-gate 	 * finally ready to destroy the structure
7980Sstevel@tonic-gate 	 */
7990Sstevel@tonic-gate 	mutex_destroy(&usba_device->usb_mutex);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	kmem_free((caddr_t)usba_device, sizeof (usba_device_t));
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /* clear the data toggle for all endpoints on this device */
8060Sstevel@tonic-gate void
8070Sstevel@tonic-gate usba_clear_data_toggle(usba_device_t *usba_device)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	int	i;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	if (usba_device != NULL) {
8120Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
8130Sstevel@tonic-gate 		for (i = 0; i < USBA_N_ENDPOINTS; i++) {
8140Sstevel@tonic-gate 			usba_device->usb_ph_list[i].usba_ph_flags &=
8150Sstevel@tonic-gate 			    ~USBA_PH_DATA_TOGGLE;
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate /*
8230Sstevel@tonic-gate  * usba_create_child_devi():
8240Sstevel@tonic-gate  *	create a child devinfo node, usba_device, attach properties.
8250Sstevel@tonic-gate  *	the usba_device structure is shared between all interfaces
8260Sstevel@tonic-gate  */
8270Sstevel@tonic-gate int
8280Sstevel@tonic-gate usba_create_child_devi(dev_info_t	*dip,
8290Sstevel@tonic-gate 		char			*node_name,
8300Sstevel@tonic-gate 		usba_hcdi_ops_t		*usba_hcdi_ops,
8310Sstevel@tonic-gate 		dev_info_t		*usb_root_hub_dip,
8320Sstevel@tonic-gate 		usb_port_status_t	port_status,
8330Sstevel@tonic-gate 		usba_device_t		*usba_device,
8340Sstevel@tonic-gate 		dev_info_t		**child_dip)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	int rval = USB_FAILURE;
8370Sstevel@tonic-gate 	int usba_device_allocated = 0;
8380Sstevel@tonic-gate 	usb_addr_t	address;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
8410Sstevel@tonic-gate 	    "usba_create_child_devi: %s usba_device=0x%p "
8420Sstevel@tonic-gate 	    "port status=0x%x", node_name,
8430Sstevel@tonic-gate 	    (void *)usba_device, port_status);
8440Sstevel@tonic-gate 
845789Sahrens 	ndi_devi_alloc_sleep(dip, node_name, (pnode_t)DEVI_SID_NODEID,
8466112Sqz150045 	    child_dip);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
8496898Sfb209375 	    "child dip=0x%p", (void *)*child_dip);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	if (usba_device == NULL) {
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 		usba_device = usba_alloc_usba_device(usb_root_hub_dip);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		/* grab the mutex to keep warlock happy */
8560Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
8570Sstevel@tonic-gate 		usba_device->usb_hcdi_ops	= usba_hcdi_ops;
8580Sstevel@tonic-gate 		usba_device->usb_port_status	= port_status;
8590Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 		usba_device_allocated++;
8620Sstevel@tonic-gate 	} else {
8630Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
8640Sstevel@tonic-gate 		if (usba_hcdi_ops) {
8650Sstevel@tonic-gate 			ASSERT(usba_device->usb_hcdi_ops == usba_hcdi_ops);
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate 		if (usb_root_hub_dip) {
8680Sstevel@tonic-gate 			ASSERT(usba_device->usb_root_hub_dip ==
8696112Sqz150045 			    usb_root_hub_dip);
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		usba_device->usb_port_status	= port_status;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	if (usba_device->usb_addr == 0) {
8780Sstevel@tonic-gate 		if (usba_set_usb_address(usba_device) == USB_FAILURE) {
8790Sstevel@tonic-gate 			address = 0;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
8826112Sqz150045 			    "cannot set usb address for dip=0x%p",
8836898Sfb209375 			    (void *)*child_dip);
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 			goto fail;
8860Sstevel@tonic-gate 		}
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 	address = usba_device->usb_addr;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	/* attach properties */
8910Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, *child_dip,
8926112Sqz150045 	    "assigned-address", address);
8930Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
8940Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
8956112Sqz150045 		    "cannot set usb address property for dip=0x%p",
8966898Sfb209375 		    (void *)*child_dip);
8970Sstevel@tonic-gate 		rval = USB_FAILURE;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 		goto fail;
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	/*
9030Sstevel@tonic-gate 	 * store the usba_device point in the dip
9040Sstevel@tonic-gate 	 */
9050Sstevel@tonic-gate 	usba_set_usba_device(*child_dip, usba_device);
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
9080Sstevel@tonic-gate 	    "usba_create_child_devi: devi=0x%p (%s) ud=0x%p",
9096898Sfb209375 	    (void *)*child_dip, ddi_driver_name(*child_dip),
9106898Sfb209375 	    (void *)usba_device);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	return (USB_SUCCESS);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate fail:
9150Sstevel@tonic-gate 	if (*child_dip) {
9160Sstevel@tonic-gate 		int rval = usba_destroy_child_devi(*child_dip, NDI_DEVI_REMOVE);
9170Sstevel@tonic-gate 		ASSERT(rval == USB_SUCCESS);
9180Sstevel@tonic-gate 		*child_dip = NULL;
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	if (usba_device_allocated) {
9220Sstevel@tonic-gate 		usba_free_usba_device(usba_device);
9230Sstevel@tonic-gate 	} else if (address && usba_device) {
9240Sstevel@tonic-gate 		usba_unset_usb_address(usba_device);
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 
927978Sfrits 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
9280Sstevel@tonic-gate 	    "usba_create_child_devi failed: rval=%d", rval);
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	return (rval);
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate int
9350Sstevel@tonic-gate usba_destroy_child_devi(dev_info_t *dip, uint_t flag)
9360Sstevel@tonic-gate {
9370Sstevel@tonic-gate 	usba_device_t	*usba_device;
9380Sstevel@tonic-gate 	int		rval = NDI_SUCCESS;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
9410Sstevel@tonic-gate 	    "usba_destroy_child_devi: %s%d (0x%p)",
9426898Sfb209375 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip);
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * if the child hasn't been bound yet, we can just
9480Sstevel@tonic-gate 	 * free the dip
9490Sstevel@tonic-gate 	 */
9500Sstevel@tonic-gate 	if (i_ddi_node_state(dip) < DS_INITIALIZED) {
9510Sstevel@tonic-gate 		/*
9520Sstevel@tonic-gate 		 * do not call ndi_devi_free() since it might
9530Sstevel@tonic-gate 		 * deadlock
9540Sstevel@tonic-gate 		 */
9550Sstevel@tonic-gate 		rval = ddi_remove_child(dip, 0);
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	} else {
9580Sstevel@tonic-gate 		char *devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
9590Sstevel@tonic-gate 		dev_info_t *pdip = ddi_get_parent(dip);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		(void) ddi_deviname(dip, devnm);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
9640Sstevel@tonic-gate 		    "usba_destroy_child_devi:\n\t"
9656898Sfb209375 		    "offlining dip 0x%p usba_device=0x%p (%s)", (void *)dip,
9660Sstevel@tonic-gate 		    (void *)usba_device, devnm);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		(void) devfs_clean(pdip, NULL, DV_CLEAN_FORCE);
9690Sstevel@tonic-gate 		rval =	ndi_devi_unconfig_one(pdip, devnm + 1, NULL,
9700Sstevel@tonic-gate 		    flag | NDI_UNCONFIG | NDI_DEVI_OFFLINE);
9710Sstevel@tonic-gate 		if (rval != NDI_SUCCESS) {
9720Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
9730Sstevel@tonic-gate 			    " ndi_devi_unconfig_one %s%d failed (%d)",
9740Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
9750Sstevel@tonic-gate 			    rval);
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate 		kmem_free(devnm, MAXNAMELEN + 1);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
9810Sstevel@tonic-gate 	    "usba_destroy_child_devi: rval=%d", rval);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	return (rval == NDI_SUCCESS ? USB_SUCCESS : USB_FAILURE);
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate /*
9880Sstevel@tonic-gate  * list management
9890Sstevel@tonic-gate  */
9900Sstevel@tonic-gate void
9910Sstevel@tonic-gate usba_init_list(usba_list_entry_t *element, usb_opaque_t private,
9920Sstevel@tonic-gate 	ddi_iblock_cookie_t	iblock_cookie)
9930Sstevel@tonic-gate {
9940Sstevel@tonic-gate 	mutex_init(&element->list_mutex, NULL, MUTEX_DRIVER,
9956112Sqz150045 	    iblock_cookie);
9960Sstevel@tonic-gate 	mutex_enter(&element->list_mutex);
9970Sstevel@tonic-gate 	element->private = private;
9980Sstevel@tonic-gate 	mutex_exit(&element->list_mutex);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate void
10030Sstevel@tonic-gate usba_destroy_list(usba_list_entry_t *head)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
10060Sstevel@tonic-gate 	ASSERT(head->next == NULL);
10070Sstevel@tonic-gate 	ASSERT(head->prev == NULL);
10080Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	mutex_destroy(&head->list_mutex);
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate void
10150Sstevel@tonic-gate usba_add_to_list(usba_list_entry_t *head, usba_list_entry_t *element)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	usba_list_entry_t *next;
10180Sstevel@tonic-gate 	int		remaining;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
10210Sstevel@tonic-gate 	mutex_enter(&element->list_mutex);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	remaining = head->count;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	/* check if it is not in another list */
10260Sstevel@tonic-gate 	ASSERT(element->next == NULL);
10270Sstevel@tonic-gate 	ASSERT(element->prev == NULL);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate #ifdef DEBUG
10300Sstevel@tonic-gate 	/*
10310Sstevel@tonic-gate 	 * only verify the list when not in interrupt context, we
10320Sstevel@tonic-gate 	 * have to trust the HCD
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	if (!servicing_interrupt()) {
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 		/* check if not already in this list */
10370Sstevel@tonic-gate 		for (next = head->next; (next != NULL);
10380Sstevel@tonic-gate 		    next = next->next) {
10390Sstevel@tonic-gate 			if (next == element) {
10400Sstevel@tonic-gate 				USB_DPRINTF_L0(DPRINT_MASK_USBA,
10410Sstevel@tonic-gate 				    usba_log_handle,
10420Sstevel@tonic-gate 				    "Attempt to corrupt USB list at 0x%p",
10430Sstevel@tonic-gate 				    (void *)head);
10440Sstevel@tonic-gate 				ASSERT(next == element);
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 				goto done;
10470Sstevel@tonic-gate 			}
10480Sstevel@tonic-gate 			remaining--;
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 			/*
10510Sstevel@tonic-gate 			 * Detect incorrect circ links or found
10520Sstevel@tonic-gate 			 * unexpected elements.
10530Sstevel@tonic-gate 			 */
10540Sstevel@tonic-gate 			if ((next->next && (remaining == 0)) ||
10550Sstevel@tonic-gate 			    ((next->next == NULL) && remaining)) {
10560Sstevel@tonic-gate 				panic("Corrupted USB list at 0x%p",
10570Sstevel@tonic-gate 				    (void *)head);
10580Sstevel@tonic-gate 				/*NOTREACHED*/
10590Sstevel@tonic-gate 			}
10600Sstevel@tonic-gate 		}
10610Sstevel@tonic-gate 	}
10620Sstevel@tonic-gate #endif
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	if (head->next == NULL) {
10650Sstevel@tonic-gate 		head->prev = head->next = element;
10660Sstevel@tonic-gate 	} else {
10670Sstevel@tonic-gate 		/* add to tail */
10680Sstevel@tonic-gate 		head->prev->next = element;
10690Sstevel@tonic-gate 		element->prev = head->prev;
10700Sstevel@tonic-gate 		head->prev = element;
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	head->count++;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
10760Sstevel@tonic-gate 	    "usba_add_to_list: head=0x%p element=0x%p count=%d",
10776898Sfb209375 	    (void *)head, (void *)element, head->count);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate done:
10800Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
10810Sstevel@tonic-gate 	mutex_exit(&element->list_mutex);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate int
10860Sstevel@tonic-gate usba_rm_from_list(usba_list_entry_t *head, usba_list_entry_t *element)
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate 	usba_list_entry_t *e;
10890Sstevel@tonic-gate 	int		found = 0;
10900Sstevel@tonic-gate 	int		remaining;
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	/* find the element in the list first */
10930Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
10960Sstevel@tonic-gate 	    "usba_rm_from_list: head=0x%p element=0x%p count=%d",
10976898Sfb209375 	    (void *)head, (void *)element, head->count);
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	remaining = head->count;
11000Sstevel@tonic-gate 	e = head->next;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	while (e) {
11030Sstevel@tonic-gate 		if (e == element) {
11040Sstevel@tonic-gate 			found++;
11050Sstevel@tonic-gate 			break;
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate 		e = e->next;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 		remaining--;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		/* Detect incorrect circ links or found unexpected elements. */
11120Sstevel@tonic-gate 		if ((e && (remaining == 0)) ||
11130Sstevel@tonic-gate 		    ((e == NULL) && (remaining))) {
11140Sstevel@tonic-gate 			panic("Corrupted USB list at 0x%p", (void *)head);
11150Sstevel@tonic-gate 			/*NOTREACHED*/
11160Sstevel@tonic-gate 		}
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	if (!found) {
11200Sstevel@tonic-gate 		mutex_exit(&head->list_mutex);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		return (USB_FAILURE);
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	/* now remove the element */
11260Sstevel@tonic-gate 	mutex_enter(&element->list_mutex);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	if (element->next) {
11290Sstevel@tonic-gate 		element->next->prev = element->prev;
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 	if (element->prev) {
11320Sstevel@tonic-gate 		element->prev->next = element->next;
11330Sstevel@tonic-gate 	}
11340Sstevel@tonic-gate 	if (head->next == element) {
11350Sstevel@tonic-gate 		head->next = element->next;
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate 	if (head->prev == element) {
11380Sstevel@tonic-gate 		head->prev = element->prev;
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	element->prev = element->next = NULL;
11420Sstevel@tonic-gate 	if (head->next == NULL) {
11430Sstevel@tonic-gate 		ASSERT(head->prev == NULL);
11440Sstevel@tonic-gate 	} else {
11450Sstevel@tonic-gate 		ASSERT(head->next->prev == NULL);
11460Sstevel@tonic-gate 	}
11470Sstevel@tonic-gate 	if (head->prev == NULL) {
11480Sstevel@tonic-gate 		ASSERT(head->next == NULL);
11490Sstevel@tonic-gate 	} else {
11500Sstevel@tonic-gate 		ASSERT(head->prev->next == NULL);
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	head->count--;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
11560Sstevel@tonic-gate 	    "usba_rm_from_list success: head=0x%p element=0x%p cnt=%d",
11576898Sfb209375 	    (void *)head, (void *)element, head->count);
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	mutex_exit(&element->list_mutex);
11600Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	return (USB_SUCCESS);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate usba_list_entry_t *
11670Sstevel@tonic-gate usba_rm_first_from_list(usba_list_entry_t *head)
11680Sstevel@tonic-gate {
11690Sstevel@tonic-gate 	usba_list_entry_t *element = NULL;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	if (head) {
11720Sstevel@tonic-gate 		mutex_enter(&head->list_mutex);
11730Sstevel@tonic-gate 		element = head->next;
11740Sstevel@tonic-gate 		if (element) {
11750Sstevel@tonic-gate 			/* now remove the element */
11760Sstevel@tonic-gate 			mutex_enter(&element->list_mutex);
11770Sstevel@tonic-gate 			head->next = element->next;
11780Sstevel@tonic-gate 			if (head->next) {
11790Sstevel@tonic-gate 				head->next->prev = NULL;
11800Sstevel@tonic-gate 			}
11810Sstevel@tonic-gate 			if (head->prev == element) {
11820Sstevel@tonic-gate 				head->prev = element->next;
11830Sstevel@tonic-gate 			}
11840Sstevel@tonic-gate 			element->prev = element->next = NULL;
11850Sstevel@tonic-gate 			mutex_exit(&element->list_mutex);
11860Sstevel@tonic-gate 			head->count--;
11870Sstevel@tonic-gate 		}
11880Sstevel@tonic-gate 		if (head->next == NULL) {
11890Sstevel@tonic-gate 			ASSERT(head->prev == NULL);
11900Sstevel@tonic-gate 		} else {
11910Sstevel@tonic-gate 			ASSERT(head->next->prev == NULL);
11920Sstevel@tonic-gate 		}
11930Sstevel@tonic-gate 		if (head->prev == NULL) {
11940Sstevel@tonic-gate 			ASSERT(head->next == NULL);
11950Sstevel@tonic-gate 		} else {
11960Sstevel@tonic-gate 			ASSERT(head->prev->next == NULL);
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
11990Sstevel@tonic-gate 		    "usba_rm_first_from_list: head=0x%p el=0x%p cnt=%d",
12006898Sfb209375 		    (void *)head, (void *)element, head->count);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		mutex_exit(&head->list_mutex);
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	return (element);
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate usb_opaque_t
12100Sstevel@tonic-gate usba_rm_first_pvt_from_list(usba_list_entry_t *head)
12110Sstevel@tonic-gate {
12120Sstevel@tonic-gate 	usba_list_entry_t *element = usba_rm_first_from_list(head);
12130Sstevel@tonic-gate 	usb_opaque_t private = NULL;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	if (element) {
12160Sstevel@tonic-gate 		mutex_enter(&element->list_mutex);
12170Sstevel@tonic-gate 		private = element->private;
12180Sstevel@tonic-gate 		mutex_exit(&element->list_mutex);
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	return (private);
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate /*
12260Sstevel@tonic-gate  * move list to new list and zero original list
12270Sstevel@tonic-gate  */
12280Sstevel@tonic-gate void
12290Sstevel@tonic-gate usba_move_list(usba_list_entry_t *head, usba_list_entry_t *new,
12300Sstevel@tonic-gate 	ddi_iblock_cookie_t iblock_cookie)
12310Sstevel@tonic-gate {
12320Sstevel@tonic-gate 	usba_init_list(new, NULL, iblock_cookie);
12330Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
12340Sstevel@tonic-gate 	mutex_enter(&new->list_mutex);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	new->next = head->next;
12370Sstevel@tonic-gate 	new->prev = head->prev;
12380Sstevel@tonic-gate 	new->count = head->count;
12390Sstevel@tonic-gate 	new->private = head->private;
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	head->next = NULL;
12420Sstevel@tonic-gate 	head->prev = NULL;
12430Sstevel@tonic-gate 	head->count = 0;
12440Sstevel@tonic-gate 	head->private = NULL;
12450Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
12460Sstevel@tonic-gate 	mutex_exit(&new->list_mutex);
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate int
12510Sstevel@tonic-gate usba_check_in_list(usba_list_entry_t *head, usba_list_entry_t *element)
12520Sstevel@tonic-gate {
12530Sstevel@tonic-gate 	int		rval = USB_FAILURE;
12540Sstevel@tonic-gate 	int		remaining;
12550Sstevel@tonic-gate 	usba_list_entry_t *next;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
12580Sstevel@tonic-gate 	remaining = head->count;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	mutex_enter(&element->list_mutex);
12610Sstevel@tonic-gate 	for (next = head->next; next != NULL; next = next->next) {
12620Sstevel@tonic-gate 		if (next == element) {
12630Sstevel@tonic-gate 			rval = USB_SUCCESS;
12640Sstevel@tonic-gate 			break;
12650Sstevel@tonic-gate 		}
12660Sstevel@tonic-gate 		remaining--;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 		/* Detect incorrect circ links or found unexpected elements. */
12690Sstevel@tonic-gate 		if ((next->next && (remaining == 0)) ||
12700Sstevel@tonic-gate 		    ((next->next == NULL) && remaining)) {
12710Sstevel@tonic-gate 			panic("Corrupted USB list at 0x%p", (void *)head);
12720Sstevel@tonic-gate 			/*NOTREACHED*/
12730Sstevel@tonic-gate 		}
12740Sstevel@tonic-gate 	}
12750Sstevel@tonic-gate 	mutex_exit(&element->list_mutex);
12760Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	return (rval);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate int
12830Sstevel@tonic-gate usba_list_entry_leaks(usba_list_entry_t *head, char *what)
12840Sstevel@tonic-gate {
12850Sstevel@tonic-gate 	int		count = 0;
12860Sstevel@tonic-gate 	int		remaining;
12870Sstevel@tonic-gate 	usba_list_entry_t *next;
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
12900Sstevel@tonic-gate 	remaining = head->count;
12910Sstevel@tonic-gate 	for (next = head->next; next != NULL; next = next->next) {
12920Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
12936898Sfb209375 		    "leaking %s 0x%p", what, (void *)next->private);
12940Sstevel@tonic-gate 		count++;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 		remaining--;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 		/* Detect incorrect circ links or found unexpected elements. */
12990Sstevel@tonic-gate 		if ((next->next && (remaining == 0)) ||
13000Sstevel@tonic-gate 		    ((next->next == NULL) && remaining)) {
13010Sstevel@tonic-gate 			panic("Corrupted USB list at 0x%p", (void *)head);
13020Sstevel@tonic-gate 			/*NOTREACHED*/
13030Sstevel@tonic-gate 		}
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate 	ASSERT(count == head->count);
13060Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	if (count) {
13090Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
13100Sstevel@tonic-gate 		    "usba_list_entry_count: leaking %d", count);
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	return (count);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate int
13180Sstevel@tonic-gate usba_list_entry_count(usba_list_entry_t *head)
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate 	int count;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	mutex_enter(&head->list_mutex);
13230Sstevel@tonic-gate 	count = head->count;
13240Sstevel@tonic-gate 	mutex_exit(&head->list_mutex);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	return (count);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
1329*9094SVincent.Wang@Sun.COM /* add a new root hub to the usba_root_hubs list */
1330*9094SVincent.Wang@Sun.COM 
1331*9094SVincent.Wang@Sun.COM void
1332*9094SVincent.Wang@Sun.COM usba_add_root_hub(dev_info_t *dip)
1333*9094SVincent.Wang@Sun.COM {
1334*9094SVincent.Wang@Sun.COM 	usba_root_hub_ent_t *hub;
1335*9094SVincent.Wang@Sun.COM 
1336*9094SVincent.Wang@Sun.COM 	hub = (usba_root_hub_ent_t *)
1337*9094SVincent.Wang@Sun.COM 	    kmem_zalloc(sizeof (usba_root_hub_ent_t), KM_SLEEP);
1338*9094SVincent.Wang@Sun.COM 
1339*9094SVincent.Wang@Sun.COM 	mutex_enter(&usba_hub_mutex);
1340*9094SVincent.Wang@Sun.COM 	hub->dip = dip;
1341*9094SVincent.Wang@Sun.COM 	hub->next = usba_root_hubs;
1342*9094SVincent.Wang@Sun.COM 	usba_root_hubs = hub;
1343*9094SVincent.Wang@Sun.COM 	mutex_exit(&usba_hub_mutex);
1344*9094SVincent.Wang@Sun.COM }
1345*9094SVincent.Wang@Sun.COM 
1346*9094SVincent.Wang@Sun.COM /* remove a root hub from the usba_root_hubs list */
1347*9094SVincent.Wang@Sun.COM 
1348*9094SVincent.Wang@Sun.COM void
1349*9094SVincent.Wang@Sun.COM usba_rem_root_hub(dev_info_t *dip)
1350*9094SVincent.Wang@Sun.COM {
1351*9094SVincent.Wang@Sun.COM 	usba_root_hub_ent_t **hubp, *hub;
1352*9094SVincent.Wang@Sun.COM 
1353*9094SVincent.Wang@Sun.COM 	mutex_enter(&usba_hub_mutex);
1354*9094SVincent.Wang@Sun.COM 	hubp = &usba_root_hubs;
1355*9094SVincent.Wang@Sun.COM 	while (*hubp) {
1356*9094SVincent.Wang@Sun.COM 		if ((*hubp)->dip == dip) {
1357*9094SVincent.Wang@Sun.COM 			hub = *hubp;
1358*9094SVincent.Wang@Sun.COM 			*hubp = hub->next;
1359*9094SVincent.Wang@Sun.COM 			kmem_free(hub, sizeof (struct usba_root_hub_ent));
1360*9094SVincent.Wang@Sun.COM 			mutex_exit(&usba_hub_mutex);
1361*9094SVincent.Wang@Sun.COM 
1362*9094SVincent.Wang@Sun.COM 			return;
1363*9094SVincent.Wang@Sun.COM 		}
1364*9094SVincent.Wang@Sun.COM 		hubp = &(*hubp)->next;
1365*9094SVincent.Wang@Sun.COM 	}
1366*9094SVincent.Wang@Sun.COM 	mutex_exit(&usba_hub_mutex);
1367*9094SVincent.Wang@Sun.COM }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate /*
1370*9094SVincent.Wang@Sun.COM  * check whether this dip is the root hub. Any root hub known by
1371*9094SVincent.Wang@Sun.COM  * usba is recorded in the linked list pointed to by usba_root_hubs
13720Sstevel@tonic-gate  */
13730Sstevel@tonic-gate int
13740Sstevel@tonic-gate usba_is_root_hub(dev_info_t *dip)
13750Sstevel@tonic-gate {
1376*9094SVincent.Wang@Sun.COM 	usba_root_hub_ent_t *hub;
1377*9094SVincent.Wang@Sun.COM 
1378*9094SVincent.Wang@Sun.COM 	mutex_enter(&usba_hub_mutex);
1379*9094SVincent.Wang@Sun.COM 	hub = usba_root_hubs;
1380*9094SVincent.Wang@Sun.COM 	while (hub) {
1381*9094SVincent.Wang@Sun.COM 		if (hub->dip == dip) {
1382*9094SVincent.Wang@Sun.COM 			mutex_exit(&usba_hub_mutex);
1383*9094SVincent.Wang@Sun.COM 
1384*9094SVincent.Wang@Sun.COM 			return (1);
1385*9094SVincent.Wang@Sun.COM 		}
1386*9094SVincent.Wang@Sun.COM 		hub = hub->next;
13870Sstevel@tonic-gate 	}
1388*9094SVincent.Wang@Sun.COM 	mutex_exit(&usba_hub_mutex);
1389*9094SVincent.Wang@Sun.COM 
13900Sstevel@tonic-gate 	return (0);
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate /*
13950Sstevel@tonic-gate  * get and store usba_device pointer in the devi
13960Sstevel@tonic-gate  */
13970Sstevel@tonic-gate usba_device_t *
13980Sstevel@tonic-gate usba_get_usba_device(dev_info_t *dip)
13990Sstevel@tonic-gate {
14000Sstevel@tonic-gate 	/*
14010Sstevel@tonic-gate 	 * we cannot use parent_data in the usb node because its
14020Sstevel@tonic-gate 	 * bus parent (eg. PCI nexus driver) uses this data
14030Sstevel@tonic-gate 	 *
14040Sstevel@tonic-gate 	 * we cannot use driver data in the other usb nodes since
14050Sstevel@tonic-gate 	 * usb drivers may need to use this
14060Sstevel@tonic-gate 	 */
14070Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
14080Sstevel@tonic-gate 		usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 		return (hcdi->hcdi_usba_device);
14110Sstevel@tonic-gate 	} else {
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 		return (ddi_get_parent_data(dip));
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate /*
14190Sstevel@tonic-gate  * Retrieve the usba_device pointer from the dev without checking for
14200Sstevel@tonic-gate  * the root hub first.	This function is only used in polled mode.
14210Sstevel@tonic-gate  */
14220Sstevel@tonic-gate usba_device_t *
14230Sstevel@tonic-gate usba_polled_get_usba_device(dev_info_t *dip)
14240Sstevel@tonic-gate {
14250Sstevel@tonic-gate 	/*
14260Sstevel@tonic-gate 	 * Don't call usba_is_root_hub() to find out if this is
14270Sstevel@tonic-gate 	 * the root hub  usba_is_root_hub() calls into the DDI
14280Sstevel@tonic-gate 	 * where there are locking issues. The dip sent in during
14290Sstevel@tonic-gate 	 * polled mode will never be the root hub, so just get
14300Sstevel@tonic-gate 	 * the usba_device pointer from the dip.
14310Sstevel@tonic-gate 	 */
14320Sstevel@tonic-gate 	return (ddi_get_parent_data(dip));
14330Sstevel@tonic-gate }
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate void
14370Sstevel@tonic-gate usba_set_usba_device(dev_info_t *dip, usba_device_t *usba_device)
14380Sstevel@tonic-gate {
14390Sstevel@tonic-gate 	if (usba_is_root_hub(dip)) {
14400Sstevel@tonic-gate 		usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
14410Sstevel@tonic-gate 		/* no locking is needed here */
14420Sstevel@tonic-gate 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(hcdi->hcdi_usba_device))
14430Sstevel@tonic-gate 		hcdi->hcdi_usba_device = usba_device;
14440Sstevel@tonic-gate 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(hcdi->hcdi_usba_device))
14450Sstevel@tonic-gate 	} else {
14460Sstevel@tonic-gate 		ddi_set_parent_data(dip, usba_device);
14470Sstevel@tonic-gate 	}
14480Sstevel@tonic-gate }
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate /*
14520Sstevel@tonic-gate  * usba_set_node_name() according to class, subclass, and protocol
14530Sstevel@tonic-gate  * following the 1275 USB binding tables.
14540Sstevel@tonic-gate  */
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate /* device node table, refer to section 3.2.2.1 of 1275 binding */
14570Sstevel@tonic-gate static node_name_entry_t device_node_name_table[] = {
14580Sstevel@tonic-gate { USB_CLASS_COMM,	DONTCARE,	DONTCARE,	"communications" },
14590Sstevel@tonic-gate { USB_CLASS_HUB,	DONTCARE,	DONTCARE,	"hub" },
14603341Sgc161489 { USB_CLASS_DIAG,	DONTCARE,	DONTCARE,	"diagnostics" },
14613341Sgc161489 { USB_CLASS_MISC,	DONTCARE,	DONTCARE,	"miscellaneous" },
14620Sstevel@tonic-gate { DONTCARE,		DONTCARE,	DONTCARE,	"device" }
14630Sstevel@tonic-gate };
14640Sstevel@tonic-gate 
14653341Sgc161489 /* interface-association node table */
14663341Sgc161489 static node_name_entry_t ia_node_name_table[] = {
14673341Sgc161489 { USB_CLASS_AUDIO,	DONTCARE,	DONTCARE, "audio" },
14683341Sgc161489 { USB_CLASS_VIDEO,	DONTCARE,	DONTCARE, "video" },
14693341Sgc161489 { USB_CLASS_WIRELESS,	USB_SUBCLS_WUSB_2, USB_PROTO_WUSB_DWA,
14703341Sgc161489 						"device-wire-adaptor" },
14713341Sgc161489 { USB_CLASS_WIRELESS,	DONTCARE,	DONTCARE, "wireless-controller" },
14723341Sgc161489 { DONTCARE,		DONTCARE,	DONTCARE, "interface-association" }
14733341Sgc161489 };
14743341Sgc161489 
14750Sstevel@tonic-gate /* interface node table, refer to section 3.3.2.1 */
14760Sstevel@tonic-gate static node_name_entry_t if_node_name_table[] = {
14770Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_CONTROL, DONTCARE,	"sound-control" },
14780Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_STREAMING, DONTCARE, "sound" },
14790Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_MIDI_STREAMING, DONTCARE, "midi" },
14800Sstevel@tonic-gate { USB_CLASS_AUDIO, DONTCARE,		DONTCARE,	"sound" },
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_DIRECT_LINE,	DONTCARE, "line" },
14830Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ABSTRCT_CTRL,	DONTCARE, "modem" },
14840Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_PHONE_CTRL, DONTCARE, "telephone" },
14850Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_MULTCNL_ISDN, DONTCARE, "isdn" },
14860Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ISDN,		DONTCARE, "isdn" },
14870Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ETHERNET,	DONTCARE, "ethernet" },
14880Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ATM_NETWORK, DONTCARE, "atm-network" },
14893341Sgc161489 { USB_CLASS_COMM, DONTCARE,		DONTCARE,	"communications" },
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_KEYBOARD,	"keyboard" },
14920Sstevel@tonic-gate { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_MOUSE,	"mouse" },
14930Sstevel@tonic-gate { USB_CLASS_HID,	DONTCARE,	DONTCARE,	"input" },
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate { USB_CLASS_HUB,	DONTCARE,	DONTCARE,	"hub" },
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate { USB_CLASS_PHYSICAL,	DONTCARE,	DONTCARE,	"physical" },
14980Sstevel@tonic-gate 
14993341Sgc161489 { USB_CLASS_IMAGE,	DONTCARE,	DONTCARE,	"image" },
15003341Sgc161489 
15010Sstevel@tonic-gate { USB_CLASS_PRINTER,	DONTCARE,	DONTCARE,	"printer" },
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate { USB_CLASS_MASS_STORAGE, DONTCARE,	DONTCARE,	"storage" },
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate { USB_CLASS_CDC_DATA,	DONTCARE,	DONTCARE,	"data" },
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate { USB_CLASS_SECURITY,	DONTCARE,	DONTCARE,	"security" },
15080Sstevel@tonic-gate 
15093341Sgc161489 { USB_CLASS_VIDEO, USB_SUBCLS_VIDEO_CONTROL, DONTCARE,	"video-control" },
15103341Sgc161489 { USB_CLASS_VIDEO, USB_SUBCLS_VIDEO_STREAM,  DONTCARE,	"video-stream" },
15113341Sgc161489 { USB_CLASS_VIDEO,	DONTCARE,	DONTCARE,	"video" },
15123341Sgc161489 
15130Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_FIRMWARE, DONTCARE, "firmware" },
15140Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_IRDA,	DONTCARE, "IrDa" },
15150Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_TEST,	DONTCARE, "test" },
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate { DONTCARE,		DONTCARE,	DONTCARE,	"interface" },
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate };
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate /* combined node table, refer to section 3.4.2.1 */
15220Sstevel@tonic-gate static node_name_entry_t combined_node_name_table[] = {
15230Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_CONTROL, DONTCARE,	"sound-control" },
15240Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_STREAMING, DONTCARE, "sound" },
15250Sstevel@tonic-gate { USB_CLASS_AUDIO, USB_SUBCLS_AUD_MIDI_STREAMING, DONTCARE, "midi" },
15260Sstevel@tonic-gate { USB_CLASS_AUDIO, DONTCARE,		DONTCARE,	"sound" },
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_DIRECT_LINE,	DONTCARE, "line" },
15290Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ABSTRCT_CTRL,	DONTCARE, "modem" },
15300Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_PHONE_CTRL, DONTCARE, "telephone" },
15310Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_MULTCNL_ISDN, DONTCARE, "isdn" },
15320Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ISDN,		DONTCARE, "isdn" },
15330Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ETHERNET,	DONTCARE, "ethernet" },
15340Sstevel@tonic-gate { USB_CLASS_COMM, USB_SUBCLS_CDCC_ATM_NETWORK, DONTCARE, "atm-network" },
15353341Sgc161489 { USB_CLASS_COMM, DONTCARE,		DONTCARE,	"communications" },
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_KEYBOARD, "keyboard" },
15380Sstevel@tonic-gate { USB_CLASS_HID, USB_SUBCLS_HID_1, USB_PROTO_HID_MOUSE,	"mouse" },
15390Sstevel@tonic-gate { USB_CLASS_HID,	DONTCARE,	DONTCARE,	"input" },
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate { USB_CLASS_PHYSICAL,	DONTCARE,	DONTCARE,	"physical" },
15420Sstevel@tonic-gate 
15433341Sgc161489 { USB_CLASS_IMAGE,	DONTCARE,	DONTCARE,	"image" },
15443341Sgc161489 
15450Sstevel@tonic-gate { USB_CLASS_PRINTER,	DONTCARE,	DONTCARE,	"printer" },
15460Sstevel@tonic-gate 
15473341Sgc161489 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_RBC_T10,	DONTCARE, "storage" },
15483341Sgc161489 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_SFF8020I,	DONTCARE, "cdrom" },
15493341Sgc161489 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_QIC_157,	DONTCARE, "tape" },
15503341Sgc161489 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_UFI,		DONTCARE, "floppy" },
15513341Sgc161489 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_SFF8070I,	DONTCARE, "storage" },
15523341Sgc161489 { USB_CLASS_MASS_STORAGE, USB_SUBCLS_MS_SCSI,		DONTCARE, "storage" },
15530Sstevel@tonic-gate { USB_CLASS_MASS_STORAGE, DONTCARE,	DONTCARE,	"storage" },
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate { USB_CLASS_CDC_DATA,	DONTCARE,	DONTCARE,	"data" },
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate { USB_CLASS_SECURITY,	DONTCARE,	DONTCARE,	"security" },
15580Sstevel@tonic-gate 
15593341Sgc161489 { USB_CLASS_VIDEO, USB_SUBCLS_VIDEO_CONTROL, DONTCARE,	"video-control" },
15603341Sgc161489 { USB_CLASS_VIDEO, USB_SUBCLS_VIDEO_STREAM,  DONTCARE,	"video-stream" },
15613341Sgc161489 { USB_CLASS_VIDEO,	DONTCARE,	DONTCARE,	"video" },
15623341Sgc161489 
15630Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_FIRMWARE, DONTCARE, "firmware" },
15640Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_IRDA,	DONTCARE, "IrDa" },
15650Sstevel@tonic-gate { USB_CLASS_APP,	USB_SUBCLS_APP_TEST,	DONTCARE, "test" },
15660Sstevel@tonic-gate 
15673341Sgc161489 { USB_CLASS_COMM,	DONTCARE,	DONTCARE,	"communications" },
15680Sstevel@tonic-gate { USB_CLASS_HUB,	DONTCARE,	DONTCARE,	"hub" },
15693341Sgc161489 { USB_CLASS_DIAG,	DONTCARE,	DONTCARE,	"diagnostics" },
15703341Sgc161489 { USB_CLASS_MISC,	DONTCARE,	DONTCARE,	"miscellaneous" },
15713341Sgc161489 { DONTCARE,		DONTCARE,	DONTCARE,	"device" }
15720Sstevel@tonic-gate };
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate static size_t device_node_name_table_size =
15750Sstevel@tonic-gate 	sizeof (device_node_name_table)/sizeof (struct node_name_entry);
15763341Sgc161489 static size_t ia_node_name_table_size =
15773341Sgc161489 	sizeof (ia_node_name_table)/sizeof (struct node_name_entry);
15780Sstevel@tonic-gate static size_t if_node_name_table_size =
15790Sstevel@tonic-gate 	sizeof (if_node_name_table)/sizeof (struct node_name_entry);
15800Sstevel@tonic-gate static size_t combined_node_name_table_size =
15810Sstevel@tonic-gate 	sizeof (combined_node_name_table)/sizeof (struct node_name_entry);
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate static void
15850Sstevel@tonic-gate usba_set_node_name(dev_info_t *dip, uint8_t class, uint8_t subclass,
15860Sstevel@tonic-gate     uint8_t protocol, uint_t flag)
15870Sstevel@tonic-gate {
15880Sstevel@tonic-gate 	int i;
15890Sstevel@tonic-gate 	size_t size;
15900Sstevel@tonic-gate 	node_name_entry_t *node_name_table;
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	switch (flag) {
15933341Sgc161489 	/* interface share node names with interface-association */
15943341Sgc161489 	case FLAG_INTERFACE_ASSOCIATION_NODE:
15953341Sgc161489 		node_name_table = ia_node_name_table;
15963341Sgc161489 		size = ia_node_name_table_size;
15973341Sgc161489 		break;
15980Sstevel@tonic-gate 	case FLAG_INTERFACE_NODE:
15990Sstevel@tonic-gate 		node_name_table = if_node_name_table;
16000Sstevel@tonic-gate 		size = if_node_name_table_size;
16010Sstevel@tonic-gate 		break;
16020Sstevel@tonic-gate 	case FLAG_DEVICE_NODE:
16030Sstevel@tonic-gate 		node_name_table = device_node_name_table;
16040Sstevel@tonic-gate 		size = device_node_name_table_size;
16050Sstevel@tonic-gate 		break;
16060Sstevel@tonic-gate 	case FLAG_COMBINED_NODE:
16070Sstevel@tonic-gate 		node_name_table = combined_node_name_table;
16080Sstevel@tonic-gate 		size = combined_node_name_table_size;
16090Sstevel@tonic-gate 		break;
16100Sstevel@tonic-gate 	default:
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 		return;
16130Sstevel@tonic-gate 	}
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
16160Sstevel@tonic-gate 		int16_t c = node_name_table[i].class;
16170Sstevel@tonic-gate 		int16_t s = node_name_table[i].subclass;
16180Sstevel@tonic-gate 		int16_t p = node_name_table[i].protocol;
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 		if (((c == DONTCARE) || (c == class)) &&
16210Sstevel@tonic-gate 		    ((s == DONTCARE) || (s == subclass)) &&
16220Sstevel@tonic-gate 		    ((p == DONTCARE) || (p == protocol))) {
16230Sstevel@tonic-gate 			char *name = node_name_table[i].name;
16243341Sgc161489 
16250Sstevel@tonic-gate 			(void) ndi_devi_set_nodename(dip, name, 0);
16260Sstevel@tonic-gate 			break;
16270Sstevel@tonic-gate 		}
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate }
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate #ifdef DEBUG
16330Sstevel@tonic-gate /*
16340Sstevel@tonic-gate  * walk the children of the parent of this devi and compare the
16350Sstevel@tonic-gate  * name and  reg property of each child. If there is a match
16360Sstevel@tonic-gate  * return this node
16370Sstevel@tonic-gate  */
16380Sstevel@tonic-gate static dev_info_t *
16390Sstevel@tonic-gate usba_find_existing_node(dev_info_t *odip)
16400Sstevel@tonic-gate {
16410Sstevel@tonic-gate 	dev_info_t *ndip, *child, *pdip;
16420Sstevel@tonic-gate 	int	*odata, *ndata;
16430Sstevel@tonic-gate 	uint_t	n_odata, n_ndata;
16440Sstevel@tonic-gate 	int	circular;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	pdip = ddi_get_parent(odip);
16470Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
16480Sstevel@tonic-gate 	    odip, DDI_PROP_DONTPASS, "reg",
16490Sstevel@tonic-gate 	    &odata, &n_odata) != DDI_SUCCESS) {
1650978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
16510Sstevel@tonic-gate 		    "usba_find_existing_node: "
16520Sstevel@tonic-gate 		    "%s: DDI_NOT_WELL_FORMED", ddi_driver_name(odip));
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 		return (NULL);
16550Sstevel@tonic-gate 	}
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circular);
16580Sstevel@tonic-gate 	ndip = (dev_info_t *)(DEVI(pdip)->devi_child);
16590Sstevel@tonic-gate 	while ((child = ndip) != NULL) {
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 		ndip = (dev_info_t *)(DEVI(child)->devi_sibling);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 		if (child == odip) {
16640Sstevel@tonic-gate 			continue;
16650Sstevel@tonic-gate 		}
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		if (strcmp(DEVI(child)->devi_node_name,
16680Sstevel@tonic-gate 		    DEVI(odip)->devi_node_name)) {
16690Sstevel@tonic-gate 			continue;
16700Sstevel@tonic-gate 		}
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
16730Sstevel@tonic-gate 		    child, DDI_PROP_DONTPASS, "reg",
16740Sstevel@tonic-gate 		    &ndata, &n_ndata) != DDI_SUCCESS) {
16750Sstevel@tonic-gate 
1676978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_HCDI, usba_log_handle,
16770Sstevel@tonic-gate 			    "usba_find_existing_node: "
16780Sstevel@tonic-gate 			    "%s DDI_NOT_WELL_FORMED", ddi_driver_name(child));
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 		} else if (n_ndata && n_odata && (bcmp(odata, ndata,
16810Sstevel@tonic-gate 		    max(n_odata, n_ndata) * sizeof (int)) == 0)) {
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HCDI, usba_log_handle,
16840Sstevel@tonic-gate 			    "usba_find_existing_node: found %s%d (%p)",
16850Sstevel@tonic-gate 			    ddi_driver_name(child),
16860Sstevel@tonic-gate 			    ddi_get_instance(child), (void *)child);
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 			USB_DPRINTF_L3(DPRINT_MASK_HCDI, usba_log_handle,
16890Sstevel@tonic-gate 			    "usba_find_existing_node: "
16900Sstevel@tonic-gate 			    "reg: %x %x %x - %x %x %x",
16910Sstevel@tonic-gate 			    n_odata, odata[0], odata[1],
16920Sstevel@tonic-gate 			    n_ndata, ndata[0], ndata[1]);
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 			ddi_prop_free(ndata);
16950Sstevel@tonic-gate 			break;
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 		} else {
16980Sstevel@tonic-gate 			ddi_prop_free(ndata);
16990Sstevel@tonic-gate 		}
17000Sstevel@tonic-gate 	}
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	ndi_devi_exit(pdip, circular);
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	ddi_prop_free(odata);
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	return (child);
17070Sstevel@tonic-gate }
17080Sstevel@tonic-gate #endif
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate /* change all unprintable characters to spaces */
17110Sstevel@tonic-gate static void
17120Sstevel@tonic-gate usba_filter_string(char *instr, char *outstr)
17130Sstevel@tonic-gate {
17140Sstevel@tonic-gate 	while (*instr) {
17150Sstevel@tonic-gate 		if ((*instr >= ' ') && (*instr <= '~')) {
17160Sstevel@tonic-gate 			*outstr = *instr;
17170Sstevel@tonic-gate 		} else {
17180Sstevel@tonic-gate 			*outstr = ' ';
17190Sstevel@tonic-gate 		}
17200Sstevel@tonic-gate 		outstr++;
17210Sstevel@tonic-gate 		instr++;
17220Sstevel@tonic-gate 	}
17230Sstevel@tonic-gate 	*outstr = '\0';
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate /*
17280Sstevel@tonic-gate  * lookup ugen binding specified in property in
17290Sstevel@tonic-gate  * hcd.conf files
17300Sstevel@tonic-gate  */
17310Sstevel@tonic-gate int
17320Sstevel@tonic-gate usba_get_ugen_binding(dev_info_t *dip)
17330Sstevel@tonic-gate {
17340Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(dip);
17350Sstevel@tonic-gate 	usba_hcdi_t	*hcdi =
17366112Sqz150045 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	return (hcdi->hcdi_ugen_default_binding);
17390Sstevel@tonic-gate }
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate /*
17430Sstevel@tonic-gate  * driver binding support at device level
17440Sstevel@tonic-gate  */
17450Sstevel@tonic-gate dev_info_t *
17460Sstevel@tonic-gate usba_ready_device_node(dev_info_t *child_dip)
17470Sstevel@tonic-gate {
17480Sstevel@tonic-gate 	int		rval, i;
17490Sstevel@tonic-gate 	int		n = 0;
17500Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(child_dip);
17510Sstevel@tonic-gate 	usb_dev_descr_t	*usb_dev_descr;
17520Sstevel@tonic-gate 	uint_t		n_cfgs;	/* number of configs */
17530Sstevel@tonic-gate 	uint_t		n_ifs;	/* number of interfaces */
17540Sstevel@tonic-gate 	uint_t		port;
17550Sstevel@tonic-gate 	size_t		usb_config_length;
17560Sstevel@tonic-gate 	uchar_t 	*usb_config;
17570Sstevel@tonic-gate 	int		reg[1];
17580Sstevel@tonic-gate 	usb_addr_t	address = usb_get_addr(child_dip);
17590Sstevel@tonic-gate 	usb_if_descr_t	if_descr;
17600Sstevel@tonic-gate 	size_t		size;
17610Sstevel@tonic-gate 	int		combined_node = 0;
17620Sstevel@tonic-gate 	int		is_hub;
17630Sstevel@tonic-gate 	char		*devprop_str;
17640Sstevel@tonic-gate 	char		*force_bind = NULL;
17653630Slg150142 	char		*usba_name_buf = NULL;
17663630Slg150142 	char		*usba_name[USBA_MAX_COMPAT_NAMES];
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	usb_config = usb_get_raw_cfg_data(child_dip, &usb_config_length);
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
17710Sstevel@tonic-gate 	mutex_enter(&usba_mutex);
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
17740Sstevel@tonic-gate 	    "usba_ready_device_node: child=0x%p", (void *)child_dip);
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	port = usba_device->usb_port;
17770Sstevel@tonic-gate 	usb_dev_descr = usba_device->usb_dev_descr;
17780Sstevel@tonic-gate 	n_cfgs = usba_device->usb_n_cfgs;
17790Sstevel@tonic-gate 	n_ifs = usba_device->usb_n_ifs;
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	if (address != ROOT_HUB_ADDR) {
17830Sstevel@tonic-gate 		size = usb_parse_if_descr(
17846112Sqz150045 		    usb_config,
17856112Sqz150045 		    usb_config_length,
17866112Sqz150045 		    0,		/* interface index */
17876112Sqz150045 		    0,		/* alt interface index */
17886112Sqz150045 		    &if_descr,
17896112Sqz150045 		    USB_IF_DESCR_SIZE);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 		if (size != USB_IF_DESCR_SIZE) {
1792978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
17930Sstevel@tonic-gate 			    "parsing interface: "
17940Sstevel@tonic-gate 			    "size (%lu) != USB_IF_DESCR_SIZE (%d)",
17950Sstevel@tonic-gate 			    size, USB_IF_DESCR_SIZE);
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 			mutex_exit(&usba_mutex);
17980Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 			return (child_dip);
18010Sstevel@tonic-gate 		}
18020Sstevel@tonic-gate 	} else {
18030Sstevel@tonic-gate 		/* fake an interface descriptor for the root hub */
18040Sstevel@tonic-gate 		bzero(&if_descr, sizeof (if_descr));
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 		if_descr.bInterfaceClass = USB_CLASS_HUB;
18070Sstevel@tonic-gate 	}
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	reg[0] = port;
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	mutex_exit(&usba_mutex);
18120Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	rval = ndi_prop_update_int_array(
18156112Sqz150045 	    DDI_DEV_T_NONE, child_dip, "reg", reg, 1);
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
1818978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
18190Sstevel@tonic-gate 		    "usba_ready_device_node: property update failed");
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 		return (child_dip);
18220Sstevel@tonic-gate 	}
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	combined_node = ((n_cfgs == 1) && (n_ifs == 1) &&
18250Sstevel@tonic-gate 	    ((usb_dev_descr->bDeviceClass == USB_CLASS_HUB) ||
18260Sstevel@tonic-gate 	    (usb_dev_descr->bDeviceClass == 0)));
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	is_hub = (if_descr.bInterfaceClass == USB_CLASS_HUB) ||
18296112Sqz150045 	    (usb_dev_descr->bDeviceClass == USB_CLASS_HUB);
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	/* set node name */
18320Sstevel@tonic-gate 	if (combined_node) {
18330Sstevel@tonic-gate 		usba_set_node_name(child_dip,
18340Sstevel@tonic-gate 		    if_descr.bInterfaceClass,
18350Sstevel@tonic-gate 		    if_descr.bInterfaceSubClass,
18360Sstevel@tonic-gate 		    if_descr.bInterfaceProtocol,
18370Sstevel@tonic-gate 		    FLAG_COMBINED_NODE);
18380Sstevel@tonic-gate 	} else {
18390Sstevel@tonic-gate 		usba_set_node_name(child_dip,
18400Sstevel@tonic-gate 		    usb_dev_descr->bDeviceClass,
18410Sstevel@tonic-gate 		    usb_dev_descr->bDeviceSubClass,
18420Sstevel@tonic-gate 		    usb_dev_descr->bDeviceProtocol,
18430Sstevel@tonic-gate 		    FLAG_DEVICE_NODE);
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	/*
18470Sstevel@tonic-gate 	 * check force binding rules
18480Sstevel@tonic-gate 	 */
18490Sstevel@tonic-gate 	if ((address != ROOT_HUB_ADDR) && usba_ddivs_usbc &&
18500Sstevel@tonic-gate 	    (address != usba_ddivs_usbc_xaddress) &&
18510Sstevel@tonic-gate 	    (!(usba_ddivs_usbc_xhubs && is_hub))) {
18520Sstevel@tonic-gate 		force_bind = "ddivs_usbc";
18530Sstevel@tonic-gate 		(void) ndi_devi_set_nodename(child_dip, "ddivs_usbc", 0);
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	} else if (usba_device->usb_preferred_driver) {
18560Sstevel@tonic-gate 		force_bind = usba_device->usb_preferred_driver;
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	} else if ((address != ROOT_HUB_ADDR) &&
18590Sstevel@tonic-gate 	    ((usba_ugen_force_binding == USBA_UGEN_DEVICE_BINDING) ||
18600Sstevel@tonic-gate 	    ((usba_ugen_force_binding == USBA_UGEN_INTERFACE_BINDING) &&
18610Sstevel@tonic-gate 	    combined_node)) && (!is_hub)) {
18620Sstevel@tonic-gate 		force_bind = "ugen";
18630Sstevel@tonic-gate 	}
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate #ifdef DEBUG
18660Sstevel@tonic-gate 	/*
18670Sstevel@tonic-gate 	 * check whether there is another dip with this name and address
18680Sstevel@tonic-gate 	 * If the dip contains usba_device, it is held by the previous
18690Sstevel@tonic-gate 	 * round of configuration.
18700Sstevel@tonic-gate 	 */
18710Sstevel@tonic-gate 	ASSERT(usba_find_existing_node(child_dip) == NULL);
18720Sstevel@tonic-gate #endif
18733630Slg150142 
18743630Slg150142 	usba_name_buf = kmem_zalloc(USBA_MAX_COMPAT_NAMES *
18753630Slg150142 	    USBA_MAX_COMPAT_NAME_LEN, KM_SLEEP);
18763630Slg150142 
18773630Slg150142 	for (i = 0; i < USBA_MAX_COMPAT_NAMES; i++) {
18783630Slg150142 		usba_name[i] = usba_name_buf + (i * USBA_MAX_COMPAT_NAME_LEN);
18793630Slg150142 	}
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	if (force_bind) {
18820Sstevel@tonic-gate 		(void) ndi_devi_set_nodename(child_dip, force_bind, 0);
18830Sstevel@tonic-gate 		(void) strncpy(usba_name[n++], force_bind,
18846112Sqz150045 		    USBA_MAX_COMPAT_NAME_LEN);
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	/* create compatible names */
18880Sstevel@tonic-gate 	if (combined_node) {
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 		/* 1. usbVID,PID.REV */
18910Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
18926112Sqz150045 		    "usb%x,%x.%x",
18936112Sqz150045 		    usb_dev_descr->idVendor,
18946112Sqz150045 		    usb_dev_descr->idProduct,
18956112Sqz150045 		    usb_dev_descr->bcdDevice);
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 		/* 2. usbVID,PID */
18980Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
18996112Sqz150045 		    "usb%x,%x",
19006112Sqz150045 		    usb_dev_descr->idVendor,
19016112Sqz150045 		    usb_dev_descr->idProduct);
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 		if (usb_dev_descr->bDeviceClass != 0) {
19040Sstevel@tonic-gate 			/* 3. usbVID,classDC.DSC.DPROTO */
19050Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19066112Sqz150045 			    "usb%x,class%x.%x.%x",
19076112Sqz150045 			    usb_dev_descr->idVendor,
19086112Sqz150045 			    usb_dev_descr->bDeviceClass,
19096112Sqz150045 			    usb_dev_descr->bDeviceSubClass,
19106112Sqz150045 			    usb_dev_descr->bDeviceProtocol);
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 			/* 4. usbVID,classDC.DSC */
19130Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19146112Sqz150045 			    "usb%x,class%x.%x",
19156112Sqz150045 			    usb_dev_descr->idVendor,
19166112Sqz150045 			    usb_dev_descr->bDeviceClass,
19176112Sqz150045 			    usb_dev_descr->bDeviceSubClass);
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 			/* 5. usbVID,classDC */
19200Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19216112Sqz150045 			    "usb%x,class%x",
19226112Sqz150045 			    usb_dev_descr->idVendor,
19236112Sqz150045 			    usb_dev_descr->bDeviceClass);
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 			/* 6. usb,classDC.DSC.DPROTO */
19260Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19276112Sqz150045 			    "usb,class%x.%x.%x",
19286112Sqz150045 			    usb_dev_descr->bDeviceClass,
19296112Sqz150045 			    usb_dev_descr->bDeviceSubClass,
19306112Sqz150045 			    usb_dev_descr->bDeviceProtocol);
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 			/* 7. usb,classDC.DSC */
19330Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19346112Sqz150045 			    "usb,class%x.%x",
19356112Sqz150045 			    usb_dev_descr->bDeviceClass,
19366112Sqz150045 			    usb_dev_descr->bDeviceSubClass);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 			/* 8. usb,classDC */
19390Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19406112Sqz150045 			    "usb,class%x",
19416112Sqz150045 			    usb_dev_descr->bDeviceClass);
19420Sstevel@tonic-gate 		}
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 		if (if_descr.bInterfaceClass != 0) {
19450Sstevel@tonic-gate 			/* 9. usbifVID,classIC.ISC.IPROTO */
19460Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19476112Sqz150045 			    "usbif%x,class%x.%x.%x",
19486112Sqz150045 			    usb_dev_descr->idVendor,
19496112Sqz150045 			    if_descr.bInterfaceClass,
19506112Sqz150045 			    if_descr.bInterfaceSubClass,
19516112Sqz150045 			    if_descr.bInterfaceProtocol);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 			/* 10. usbifVID,classIC.ISC */
19540Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19556112Sqz150045 			    "usbif%x,class%x.%x",
19566112Sqz150045 			    usb_dev_descr->idVendor,
19576112Sqz150045 			    if_descr.bInterfaceClass,
19586112Sqz150045 			    if_descr.bInterfaceSubClass);
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 			/* 11. usbifVID,classIC */
19610Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19626112Sqz150045 			    "usbif%x,class%x",
19636112Sqz150045 			    usb_dev_descr->idVendor,
19646112Sqz150045 			    if_descr.bInterfaceClass);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 			/* 12. usbif,classIC.ISC.IPROTO */
19670Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19686112Sqz150045 			    "usbif,class%x.%x.%x",
19696112Sqz150045 			    if_descr.bInterfaceClass,
19706112Sqz150045 			    if_descr.bInterfaceSubClass,
19716112Sqz150045 			    if_descr.bInterfaceProtocol);
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 			/* 13. usbif,classIC.ISC */
19740Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19756112Sqz150045 			    "usbif,class%x.%x",
19766112Sqz150045 			    if_descr.bInterfaceClass,
19776112Sqz150045 			    if_descr.bInterfaceSubClass);
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 			/* 14. usbif,classIC */
19800Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19816112Sqz150045 			    "usbif,class%x",
19826112Sqz150045 			    if_descr.bInterfaceClass);
19830Sstevel@tonic-gate 		}
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 		/* 15. ugen or usb_mid */
19860Sstevel@tonic-gate 		if (usba_get_ugen_binding(child_dip) ==
19870Sstevel@tonic-gate 		    USBA_UGEN_DEVICE_BINDING) {
19880Sstevel@tonic-gate 			(void) sprintf(usba_name[n++], "ugen");
19890Sstevel@tonic-gate 		} else {
19900Sstevel@tonic-gate 			(void) sprintf(usba_name[n++], "usb,device");
19910Sstevel@tonic-gate 		}
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	} else {
19940Sstevel@tonic-gate 		if (n_cfgs > 1) {
19950Sstevel@tonic-gate 			/* 1. usbVID,PID.REV.configCN */
19960Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
19976112Sqz150045 			    "usb%x,%x.%x.config%x",
19986112Sqz150045 			    usb_dev_descr->idVendor,
19996112Sqz150045 			    usb_dev_descr->idProduct,
20006112Sqz150045 			    usb_dev_descr->bcdDevice,
20016112Sqz150045 			    usba_device->usb_cfg_value);
20020Sstevel@tonic-gate 		}
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 		/* 2. usbVID,PID.REV */
20050Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
20066112Sqz150045 		    "usb%x,%x.%x",
20076112Sqz150045 		    usb_dev_descr->idVendor,
20086112Sqz150045 		    usb_dev_descr->idProduct,
20096112Sqz150045 		    usb_dev_descr->bcdDevice);
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 		/* 3. usbVID,PID.configCN */
20120Sstevel@tonic-gate 		if (n_cfgs > 1) {
20130Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
20146112Sqz150045 			    "usb%x,%x.%x",
20156112Sqz150045 			    usb_dev_descr->idVendor,
20166112Sqz150045 			    usb_dev_descr->idProduct,
20176112Sqz150045 			    usba_device->usb_cfg_value);
20180Sstevel@tonic-gate 		}
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 		/* 4. usbVID,PID */
20210Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
20226112Sqz150045 		    "usb%x,%x",
20236112Sqz150045 		    usb_dev_descr->idVendor,
20246112Sqz150045 		    usb_dev_descr->idProduct);
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 		if (usb_dev_descr->bDeviceClass != 0) {
20270Sstevel@tonic-gate 			/* 5. usbVID,classDC.DSC.DPROTO */
20280Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
20296112Sqz150045 			    "usb%x,class%x.%x.%x",
20306112Sqz150045 			    usb_dev_descr->idVendor,
20316112Sqz150045 			    usb_dev_descr->bDeviceClass,
20326112Sqz150045 			    usb_dev_descr->bDeviceSubClass,
20336112Sqz150045 			    usb_dev_descr->bDeviceProtocol);
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 			/* 6. usbVID,classDC.DSC */
20360Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
20376112Sqz150045 			    "usb%x.class%x.%x",
20386112Sqz150045 			    usb_dev_descr->idVendor,
20396112Sqz150045 			    usb_dev_descr->bDeviceClass,
20406112Sqz150045 			    usb_dev_descr->bDeviceSubClass);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 			/* 7. usbVID,classDC */
20430Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
20446112Sqz150045 			    "usb%x.class%x",
20456112Sqz150045 			    usb_dev_descr->idVendor,
20466112Sqz150045 			    usb_dev_descr->bDeviceClass);
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 			/* 8. usb,classDC.DSC.DPROTO */
20490Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
20506112Sqz150045 			    "usb,class%x.%x.%x",
20516112Sqz150045 			    usb_dev_descr->bDeviceClass,
20526112Sqz150045 			    usb_dev_descr->bDeviceSubClass,
20536112Sqz150045 			    usb_dev_descr->bDeviceProtocol);
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 			/* 9. usb,classDC.DSC */
20560Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
20576112Sqz150045 			    "usb,class%x.%x",
20586112Sqz150045 			    usb_dev_descr->bDeviceClass,
20596112Sqz150045 			    usb_dev_descr->bDeviceSubClass);
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 			/* 10. usb,classDC */
20620Sstevel@tonic-gate 			(void) sprintf(usba_name[n++],
20636112Sqz150045 			    "usb,class%x",
20646112Sqz150045 			    usb_dev_descr->bDeviceClass);
20650Sstevel@tonic-gate 		}
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 		if (usba_get_ugen_binding(child_dip) ==
20680Sstevel@tonic-gate 		    USBA_UGEN_DEVICE_BINDING) {
20690Sstevel@tonic-gate 			/* 11. ugen */
20700Sstevel@tonic-gate 			(void) sprintf(usba_name[n++], "ugen");
20710Sstevel@tonic-gate 		} else {
20720Sstevel@tonic-gate 			/* 11. usb,device */
20730Sstevel@tonic-gate 			(void) sprintf(usba_name[n++], "usb,device");
20740Sstevel@tonic-gate 		}
20750Sstevel@tonic-gate 	}
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	for (i = 0; i < n; i += 2) {
20780Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
20793630Slg150142 		    "compatible name:\t%s\t%s", usba_name[i],
20803630Slg150142 		    (((i+1) < n)? usba_name[i+1] : ""));
20810Sstevel@tonic-gate 	}
20823630Slg150142 
20833630Slg150142 	rval = ndi_prop_update_string_array(DDI_DEV_T_NONE, child_dip,
20843630Slg150142 	    "compatible", (char **)usba_name, n);
20853630Slg150142 
20863630Slg150142 	kmem_free(usba_name_buf, USBA_MAX_COMPAT_NAMES *
20873630Slg150142 	    USBA_MAX_COMPAT_NAME_LEN);
20883630Slg150142 
20893630Slg150142 	if (rval != DDI_PROP_SUCCESS) {
20903630Slg150142 
20913630Slg150142 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
20923630Slg150142 		    "usba_ready_device_node: property update failed");
20933630Slg150142 
20943630Slg150142 		return (child_dip);
20950Sstevel@tonic-gate 	}
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	/* update the address property */
20980Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
20996112Sqz150045 	    "assigned-address", usba_device->usb_addr);
21000Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
2101978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21020Sstevel@tonic-gate 		    "usba_ready_device_node: address update failed");
21030Sstevel@tonic-gate 	}
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	/* update the usb device properties (PSARC/2000/454) */
21060Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
21070Sstevel@tonic-gate 	    "usb-vendor-id", usb_dev_descr->idVendor);
21080Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
2109978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21100Sstevel@tonic-gate 		    "usba_ready_device_node: usb-vendor-id update failed");
21110Sstevel@tonic-gate 	}
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
21140Sstevel@tonic-gate 	    "usb-product-id", usb_dev_descr->idProduct);
21150Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
2116978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21170Sstevel@tonic-gate 		    "usba_ready_device_node: usb-product-id update failed");
21180Sstevel@tonic-gate 	}
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
21210Sstevel@tonic-gate 	    "usb-revision-id", usb_dev_descr->bcdDevice);
21220Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
2123978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21240Sstevel@tonic-gate 		    "usba_ready_device_node: usb-revision-id update failed");
21250Sstevel@tonic-gate 	}
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
21280Sstevel@tonic-gate 	    "usb-num-configs", usb_dev_descr->bNumConfigurations);
21290Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
2130978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21310Sstevel@tonic-gate 		    "usba_ready_device_node: usb-num-configs update failed");
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
21350Sstevel@tonic-gate 	    "usb-release", usb_dev_descr->bcdUSB);
21360Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
2137978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21380Sstevel@tonic-gate 		    "usba_ready_device_node: usb-release update failed");
21390Sstevel@tonic-gate 	}
21400Sstevel@tonic-gate 
21416112Sqz150045 	rval = ndi_prop_update_byte_array(DDI_DEV_T_NONE, child_dip,
21426112Sqz150045 	    "usb-dev-descriptor", (uchar_t *)usb_dev_descr,
21436112Sqz150045 	    sizeof (usb_dev_descr_t));
21446112Sqz150045 	if (rval != DDI_PROP_SUCCESS) {
21456112Sqz150045 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21466112Sqz150045 		    "usba_ready_device_node: usb-descriptor update failed");
21476112Sqz150045 	}
21486112Sqz150045 
21496112Sqz150045 	rval = ndi_prop_update_byte_array(DDI_DEV_T_NONE, child_dip,
21506112Sqz150045 	    "usb-raw-cfg-descriptors", usb_config, usb_config_length);
21516112Sqz150045 	if (rval != DDI_PROP_SUCCESS) {
21526112Sqz150045 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21536112Sqz150045 		    "usba_ready_device_node: usb-raw-cfg-descriptors update "
21546112Sqz150045 		    "failed");
21556112Sqz150045 	}
21566112Sqz150045 
21570Sstevel@tonic-gate 	devprop_str = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	if (usba_device->usb_serialno_str) {
21600Sstevel@tonic-gate 		usba_filter_string(usba_device->usb_serialno_str, devprop_str);
21610Sstevel@tonic-gate 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
21620Sstevel@tonic-gate 		    "usb-serialno", devprop_str);
21630Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
2164978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21650Sstevel@tonic-gate 			    "usba_ready_device_node: "
21660Sstevel@tonic-gate 			    "usb-serialno update failed");
21670Sstevel@tonic-gate 		}
21680Sstevel@tonic-gate 	}
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 	if (usba_device->usb_mfg_str) {
21710Sstevel@tonic-gate 		usba_filter_string(usba_device->usb_mfg_str, devprop_str);
21720Sstevel@tonic-gate 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
21730Sstevel@tonic-gate 		    "usb-vendor-name", devprop_str);
21740Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
2175978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21760Sstevel@tonic-gate 			    "usba_ready_device_node: "
21770Sstevel@tonic-gate 			    "usb-vendor-name update failed");
21780Sstevel@tonic-gate 		}
21790Sstevel@tonic-gate 	}
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	if (usba_device->usb_product_str) {
21820Sstevel@tonic-gate 		usba_filter_string(usba_device->usb_product_str, devprop_str);
21830Sstevel@tonic-gate 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, child_dip,
21840Sstevel@tonic-gate 		    "usb-product-name", devprop_str);
21850Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
2186978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
21870Sstevel@tonic-gate 			    "usba_ready_device_node: "
21880Sstevel@tonic-gate 			    "usb-product-name update failed");
21890Sstevel@tonic-gate 		}
21900Sstevel@tonic-gate 	}
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	kmem_free(devprop_str, USB_MAXSTRINGLEN);
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 	if (!combined_node) {
21950Sstevel@tonic-gate 		/* update the configuration property */
21960Sstevel@tonic-gate 		rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
21976112Sqz150045 		    "configuration#", usba_device->usb_cfg_value);
21980Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
21990Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
22000Sstevel@tonic-gate 			    "usba_ready_device_node: "
22010Sstevel@tonic-gate 			    "config prop update failed");
22020Sstevel@tonic-gate 		}
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	if (usba_device->usb_port_status == USBA_LOW_SPEED_DEV) {
22060Sstevel@tonic-gate 		/* create boolean property */
22070Sstevel@tonic-gate 		rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, child_dip,
22086112Sqz150045 		    "low-speed");
22090Sstevel@tonic-gate 		if (rval != DDI_PROP_SUCCESS) {
2210978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
22110Sstevel@tonic-gate 			    "usba_ready_device_node: "
22120Sstevel@tonic-gate 			    "low speed prop update failed");
22130Sstevel@tonic-gate 		}
22140Sstevel@tonic-gate 	}
22150Sstevel@tonic-gate 
22166112Sqz150045 	if (usba_device->usb_port_status == USBA_HIGH_SPEED_DEV) {
22176112Sqz150045 		/* create boolean property */
22186112Sqz150045 		rval = ndi_prop_create_boolean(DDI_DEV_T_NONE, child_dip,
22196112Sqz150045 		    "high-speed");
22206112Sqz150045 		if (rval != DDI_PROP_SUCCESS) {
22216112Sqz150045 			USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
22226112Sqz150045 			    "usba_ready_device_node: "
22236112Sqz150045 			    "high speed prop update failed");
22246112Sqz150045 		}
22256112Sqz150045 	}
22266112Sqz150045 
22270Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
22280Sstevel@tonic-gate 	    "%s%d at port %d: %s, dip=0x%p",
22290Sstevel@tonic-gate 	    ddi_node_name(ddi_get_parent(child_dip)),
22300Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(child_dip)),
22316898Sfb209375 	    port, ddi_node_name(child_dip), (void *)child_dip);
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 	usba_set_usba_device(child_dip, usba_device);
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 	ASSERT(!mutex_owned(&(usba_get_usba_device(child_dip)->usb_mutex)));
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	return (child_dip);
22380Sstevel@tonic-gate }
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate /*
22423341Sgc161489  * driver binding at interface association level. the first arg is the parent
22433341Sgc161489  * dip. if_count returns amount of interfaces which are associated within
22443341Sgc161489  * this interface-association that starts from first_if.
22453341Sgc161489  */
22463341Sgc161489 /*ARGSUSED*/
22473341Sgc161489 dev_info_t *
22483341Sgc161489 usba_ready_interface_association_node(dev_info_t	*dip,
22493341Sgc161489 					uint_t		first_if,
22503341Sgc161489 					uint_t		*if_count)
22513341Sgc161489 {
22523341Sgc161489 	dev_info_t		*child_dip = NULL;
22533341Sgc161489 	usba_device_t		*child_ud = usba_get_usba_device(dip);
22543341Sgc161489 	usb_dev_descr_t		*usb_dev_descr;
22553341Sgc161489 	size_t			usb_cfg_length;
22563341Sgc161489 	uchar_t			*usb_cfg;
22573341Sgc161489 	usb_ia_descr_t		ia_descr;
22583341Sgc161489 	int			i, n, rval;
22593341Sgc161489 	int			reg[2];
22603341Sgc161489 	size_t			size;
22613341Sgc161489 	usb_port_status_t	port_status;
22623341Sgc161489 	char			*force_bind = NULL;
22633630Slg150142 	char			*usba_name_buf = NULL;
22643630Slg150142 	char			*usba_name[USBA_MAX_COMPAT_NAMES];
22653341Sgc161489 
22663341Sgc161489 	usb_cfg = usb_get_raw_cfg_data(dip, &usb_cfg_length);
22673341Sgc161489 
22683341Sgc161489 	mutex_enter(&child_ud->usb_mutex);
22693341Sgc161489 
22703341Sgc161489 	usb_dev_descr = child_ud->usb_dev_descr;
22713341Sgc161489 
22723341Sgc161489 	/*
22733341Sgc161489 	 * for each interface association, determine all compatible names
22743341Sgc161489 	 */
22753341Sgc161489 	USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
22763341Sgc161489 	    "usba_ready_ia_node: "
22773341Sgc161489 	    "port %d, interface = %d, port_status = %x",
22783341Sgc161489 	    child_ud->usb_port, first_if, child_ud->usb_port_status);
22793341Sgc161489 
22803341Sgc161489 	/* Parse the interface descriptor */
22813341Sgc161489 	size = usb_parse_ia_descr(
22826112Sqz150045 	    usb_cfg,
22836112Sqz150045 	    usb_cfg_length,
22846112Sqz150045 	    first_if,	/* interface index */
22856112Sqz150045 	    &ia_descr,
22866112Sqz150045 	    USB_IA_DESCR_SIZE);
22873341Sgc161489 
22883341Sgc161489 	*if_count = 1;
22893341Sgc161489 	if (size != USB_IA_DESCR_SIZE) {
22903341Sgc161489 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
22913341Sgc161489 		    "parsing ia: size (%lu) != USB_IA_DESCR_SIZE (%d)",
22923341Sgc161489 		    size, USB_IA_DESCR_SIZE);
22933341Sgc161489 		mutex_exit(&child_ud->usb_mutex);
22943341Sgc161489 
22953341Sgc161489 		return (NULL);
22963341Sgc161489 	}
22973341Sgc161489 
22983341Sgc161489 	port_status = child_ud->usb_port_status;
22993341Sgc161489 
23003341Sgc161489 	/* create reg property */
23013341Sgc161489 	reg[0] = first_if;
23023341Sgc161489 	reg[1] = child_ud->usb_cfg_value;
23033341Sgc161489 
23043341Sgc161489 	mutex_exit(&child_ud->usb_mutex);
23053341Sgc161489 
23063341Sgc161489 	/* clone this dip */
23073341Sgc161489 	rval =	usba_create_child_devi(dip,
23086112Sqz150045 	    "interface-association",
23096112Sqz150045 	    NULL,		/* usba_hcdi ops */
23106112Sqz150045 	    NULL,		/* root hub dip */
23116112Sqz150045 	    port_status,	/* port status */
23126112Sqz150045 	    child_ud,	/* share this usba_device */
23136112Sqz150045 	    &child_dip);
23143341Sgc161489 
23153341Sgc161489 	if (rval != USB_SUCCESS) {
23163341Sgc161489 
23173341Sgc161489 		goto fail;
23183341Sgc161489 	}
23193341Sgc161489 
23203341Sgc161489 	rval = ndi_prop_update_int_array(
23216112Sqz150045 	    DDI_DEV_T_NONE, child_dip, "reg", reg, 2);
23223341Sgc161489 
23233341Sgc161489 	if (rval != DDI_PROP_SUCCESS) {
23243341Sgc161489 
23253341Sgc161489 		goto fail;
23263341Sgc161489 	}
23273341Sgc161489 
23283341Sgc161489 	usba_set_node_name(child_dip, ia_descr.bFunctionClass,
23293341Sgc161489 	    ia_descr.bFunctionSubClass, ia_descr.bFunctionProtocol,
23303341Sgc161489 	    FLAG_INTERFACE_ASSOCIATION_NODE);
23313341Sgc161489 
23323341Sgc161489 	/* check force binding */
23333341Sgc161489 	if (usba_ugen_force_binding ==
23343341Sgc161489 	    USBA_UGEN_INTERFACE_ASSOCIATION_BINDING) {
23353341Sgc161489 		force_bind = "ugen";
23363341Sgc161489 	}
23373341Sgc161489 
23383341Sgc161489 	/*
23393341Sgc161489 	 * check whether there is another dip with this name and address
23403341Sgc161489 	 */
23413341Sgc161489 	ASSERT(usba_find_existing_node(child_dip) == NULL);
23423341Sgc161489 
23433630Slg150142 	usba_name_buf = kmem_zalloc(USBA_MAX_COMPAT_NAMES *
23443630Slg150142 	    USBA_MAX_COMPAT_NAME_LEN, KM_SLEEP);
23453630Slg150142 
23463630Slg150142 	for (i = 0; i < USBA_MAX_COMPAT_NAMES; i++) {
23473630Slg150142 		usba_name[i] = usba_name_buf + (i * USBA_MAX_COMPAT_NAME_LEN);
23483630Slg150142 	}
23493630Slg150142 
23503341Sgc161489 	n = 0;
23513341Sgc161489 
23523341Sgc161489 	if (force_bind) {
23533341Sgc161489 		(void) ndi_devi_set_nodename(child_dip, force_bind, 0);
23543341Sgc161489 		(void) strncpy(usba_name[n++], force_bind,
23556112Sqz150045 		    USBA_MAX_COMPAT_NAME_LEN);
23563341Sgc161489 	}
23573341Sgc161489 
23583341Sgc161489 	/* 1) usbiaVID,PID.REV.configCN.FN */
23593341Sgc161489 	(void) sprintf(usba_name[n++],
23606112Sqz150045 	    "usbia%x,%x.%x.config%x.%x",
23616112Sqz150045 	    usb_dev_descr->idVendor,
23626112Sqz150045 	    usb_dev_descr->idProduct,
23636112Sqz150045 	    usb_dev_descr->bcdDevice,
23646112Sqz150045 	    child_ud->usb_cfg_value,
23656112Sqz150045 	    first_if);
23663341Sgc161489 
23673341Sgc161489 	/* 2) usbiaVID,PID.configCN.FN */
23683341Sgc161489 	(void) sprintf(usba_name[n++],
23696112Sqz150045 	    "usbia%x,%x.config%x.%x",
23706112Sqz150045 	    usb_dev_descr->idVendor,
23716112Sqz150045 	    usb_dev_descr->idProduct,
23726112Sqz150045 	    child_ud->usb_cfg_value,
23736112Sqz150045 	    first_if);
23743341Sgc161489 
23753341Sgc161489 
23763341Sgc161489 	if (ia_descr.bFunctionClass) {
23773341Sgc161489 		/* 3) usbiaVID,classFC.FSC.FPROTO */
23783341Sgc161489 		(void) sprintf(usba_name[n++],
23796112Sqz150045 		    "usbia%x,class%x.%x.%x",
23806112Sqz150045 		    usb_dev_descr->idVendor,
23816112Sqz150045 		    ia_descr.bFunctionClass,
23826112Sqz150045 		    ia_descr.bFunctionSubClass,
23836112Sqz150045 		    ia_descr.bFunctionProtocol);
23843341Sgc161489 
23853341Sgc161489 		/* 4) usbiaVID,classFC.FSC */
23863341Sgc161489 		(void) sprintf(usba_name[n++],
23876112Sqz150045 		    "usbia%x,class%x.%x",
23886112Sqz150045 		    usb_dev_descr->idVendor,
23896112Sqz150045 		    ia_descr.bFunctionClass,
23906112Sqz150045 		    ia_descr.bFunctionSubClass);
23913341Sgc161489 
23923341Sgc161489 		/* 5) usbiaVID,classFC */
23933341Sgc161489 		(void) sprintf(usba_name[n++],
23946112Sqz150045 		    "usbia%x,class%x",
23956112Sqz150045 		    usb_dev_descr->idVendor,
23966112Sqz150045 		    ia_descr.bFunctionClass);
23973341Sgc161489 
23983341Sgc161489 		/* 6) usbia,classFC.FSC.FPROTO */
23993341Sgc161489 		(void) sprintf(usba_name[n++],
24006112Sqz150045 		    "usbia,class%x.%x.%x",
24016112Sqz150045 		    ia_descr.bFunctionClass,
24026112Sqz150045 		    ia_descr.bFunctionSubClass,
24036112Sqz150045 		    ia_descr.bFunctionProtocol);
24043341Sgc161489 
24053341Sgc161489 		/* 7) usbia,classFC.FSC */
24063341Sgc161489 		(void) sprintf(usba_name[n++],
24076112Sqz150045 		    "usbia,class%x.%x",
24086112Sqz150045 		    ia_descr.bFunctionClass,
24096112Sqz150045 		    ia_descr.bFunctionSubClass);
24103341Sgc161489 
24113341Sgc161489 		/* 8) usbia,classFC */
24123341Sgc161489 		(void) sprintf(usba_name[n++],
24136112Sqz150045 		    "usbia,class%x",
24146112Sqz150045 		    ia_descr.bFunctionClass);
24153341Sgc161489 	}
24163341Sgc161489 
24173341Sgc161489 	if (usba_get_ugen_binding(child_dip) ==
24183341Sgc161489 	    USBA_UGEN_INTERFACE_ASSOCIATION_BINDING) {
24193341Sgc161489 		/* 9) ugen */
24203341Sgc161489 		(void) sprintf(usba_name[n++], "ugen");
24213341Sgc161489 	} else {
24223341Sgc161489 
24233341Sgc161489 		(void) sprintf(usba_name[n++], "usb,ia");
24243341Sgc161489 	}
24253341Sgc161489 
24263341Sgc161489 	for (i = 0; i < n; i += 2) {
24273341Sgc161489 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
24283630Slg150142 		    "compatible name:\t%s\t%s", usba_name[i],
24293630Slg150142 		    (((i+1) < n)? usba_name[i+1] : ""));
24303341Sgc161489 	}
24313341Sgc161489 
24323341Sgc161489 	/* create compatible property */
24333630Slg150142 	rval = ndi_prop_update_string_array(DDI_DEV_T_NONE, child_dip,
24343630Slg150142 	    "compatible", (char **)usba_name, n);
24353630Slg150142 
24363630Slg150142 	kmem_free(usba_name_buf, USBA_MAX_COMPAT_NAMES *
24373630Slg150142 	    USBA_MAX_COMPAT_NAME_LEN);
24383630Slg150142 
24393630Slg150142 	if (rval != DDI_PROP_SUCCESS) {
24403630Slg150142 
24413630Slg150142 		goto fail;
24423341Sgc161489 	}
24433341Sgc161489 
24443341Sgc161489 	/* update the address property */
24453341Sgc161489 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
24463341Sgc161489 	    "assigned-address", child_ud->usb_addr);
24473341Sgc161489 	if (rval != DDI_PROP_SUCCESS) {
24483341Sgc161489 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
24493341Sgc161489 		    "usba_ready_interface_node: address update failed");
24503341Sgc161489 	}
24513341Sgc161489 
24523341Sgc161489 	/* create property with first interface number */
24533341Sgc161489 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
24543341Sgc161489 	    "interface", ia_descr.bFirstInterface);
24553341Sgc161489 
24563341Sgc161489 	if (rval != DDI_PROP_SUCCESS) {
24573341Sgc161489 
24583341Sgc161489 		goto fail;
24593341Sgc161489 	}
24603341Sgc161489 
24613341Sgc161489 	/* create property with the count of interfaces in this ia */
24623341Sgc161489 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
24633341Sgc161489 	    "interface-count", ia_descr.bInterfaceCount);
24643341Sgc161489 
24653341Sgc161489 	if (rval != DDI_PROP_SUCCESS) {
24663341Sgc161489 
24673341Sgc161489 		goto fail;
24683341Sgc161489 	}
24693341Sgc161489 
24703341Sgc161489 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
24713341Sgc161489 	    "%s%d port %d: %s, dip = 0x%p",
24723341Sgc161489 	    ddi_node_name(ddi_get_parent(dip)),
24733341Sgc161489 	    ddi_get_instance(ddi_get_parent(dip)),
24746898Sfb209375 	    child_ud->usb_port, ddi_node_name(child_dip), (void *)child_dip);
24753341Sgc161489 
24763341Sgc161489 	*if_count = ia_descr.bInterfaceCount;
24773341Sgc161489 	usba_set_usba_device(child_dip, child_ud);
24783341Sgc161489 	ASSERT(!mutex_owned(&(usba_get_usba_device(child_dip)->usb_mutex)));
24793341Sgc161489 
24803341Sgc161489 	return (child_dip);
24813341Sgc161489 
24823341Sgc161489 fail:
24833341Sgc161489 	(void) usba_destroy_child_devi(child_dip, NDI_DEVI_REMOVE);
24843341Sgc161489 
24853341Sgc161489 	return (NULL);
24863341Sgc161489 }
24873341Sgc161489 
24883341Sgc161489 
24893341Sgc161489 /*
24900Sstevel@tonic-gate  * driver binding at interface level, the first arg will be the
24910Sstevel@tonic-gate  * the parent dip
24920Sstevel@tonic-gate  */
24930Sstevel@tonic-gate /*ARGSUSED*/
24940Sstevel@tonic-gate dev_info_t *
24950Sstevel@tonic-gate usba_ready_interface_node(dev_info_t *dip, uint_t intf)
24960Sstevel@tonic-gate {
24970Sstevel@tonic-gate 	dev_info_t		*child_dip = NULL;
24980Sstevel@tonic-gate 	usba_device_t		*child_ud = usba_get_usba_device(dip);
24990Sstevel@tonic-gate 	usb_dev_descr_t	*usb_dev_descr;
25000Sstevel@tonic-gate 	size_t			usb_cfg_length;
25010Sstevel@tonic-gate 	uchar_t 		*usb_cfg;
25020Sstevel@tonic-gate 	usb_if_descr_t	if_descr;
25030Sstevel@tonic-gate 	int			i, n, rval;
25040Sstevel@tonic-gate 	int			reg[2];
25050Sstevel@tonic-gate 	size_t			size;
25060Sstevel@tonic-gate 	usb_port_status_t	port_status;
25070Sstevel@tonic-gate 	char			*force_bind = NULL;
25083630Slg150142 	char			*usba_name_buf = NULL;
25093630Slg150142 	char			*usba_name[USBA_MAX_COMPAT_NAMES];
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 	usb_cfg = usb_get_raw_cfg_data(dip, &usb_cfg_length);
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 	mutex_enter(&child_ud->usb_mutex);
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	usb_dev_descr = child_ud->usb_dev_descr;
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	/*
25180Sstevel@tonic-gate 	 * for each interface, determine all compatible names
25190Sstevel@tonic-gate 	 */
25200Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
25210Sstevel@tonic-gate 	    "usba_ready_interface_node: "
25220Sstevel@tonic-gate 	    "port %d, interface = %d port status = %x",
25230Sstevel@tonic-gate 	    child_ud->usb_port, intf, child_ud->usb_port_status);
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	/* Parse the interface descriptor */
25260Sstevel@tonic-gate 	size = usb_parse_if_descr(
25276112Sqz150045 	    usb_cfg,
25286112Sqz150045 	    usb_cfg_length,
25296112Sqz150045 	    intf,		/* interface index */
25306112Sqz150045 	    0,		/* alt interface index */
25316112Sqz150045 	    &if_descr,
25326112Sqz150045 	    USB_IF_DESCR_SIZE);
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 	if (size != USB_IF_DESCR_SIZE) {
2535978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
25360Sstevel@tonic-gate 		    "parsing interface: size (%lu) != USB_IF_DESCR_SIZE (%d)",
25370Sstevel@tonic-gate 		    size, USB_IF_DESCR_SIZE);
25380Sstevel@tonic-gate 		mutex_exit(&child_ud->usb_mutex);
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 		return (NULL);
25410Sstevel@tonic-gate 	}
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate 	port_status = child_ud->usb_port_status;
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	/* create reg property */
25460Sstevel@tonic-gate 	reg[0] = intf;
25470Sstevel@tonic-gate 	reg[1] = child_ud->usb_cfg_value;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	mutex_exit(&child_ud->usb_mutex);
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	/* clone this dip */
25520Sstevel@tonic-gate 	rval =	usba_create_child_devi(dip,
25536112Sqz150045 	    "interface",
25546112Sqz150045 	    NULL,		/* usba_hcdi ops */
25556112Sqz150045 	    NULL,		/* root hub dip */
25566112Sqz150045 	    port_status,	/* port status */
25576112Sqz150045 	    child_ud,	/* share this usba_device */
25586112Sqz150045 	    &child_dip);
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 	if (rval != USB_SUCCESS) {
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 		goto fail;
25630Sstevel@tonic-gate 	}
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	rval = ndi_prop_update_int_array(
25666112Sqz150045 	    DDI_DEV_T_NONE, child_dip, "reg", reg, 2);
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 		goto fail;
25710Sstevel@tonic-gate 	}
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	usba_set_node_name(child_dip, if_descr.bInterfaceClass,
25740Sstevel@tonic-gate 	    if_descr.bInterfaceSubClass, if_descr.bInterfaceProtocol,
25750Sstevel@tonic-gate 	    FLAG_INTERFACE_NODE);
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	/* check force binding */
25780Sstevel@tonic-gate 	if (usba_ugen_force_binding == USBA_UGEN_INTERFACE_BINDING) {
25790Sstevel@tonic-gate 		force_bind = "ugen";
25800Sstevel@tonic-gate 	}
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	/*
25830Sstevel@tonic-gate 	 * check whether there is another dip with this name and address
25840Sstevel@tonic-gate 	 */
25850Sstevel@tonic-gate 	ASSERT(usba_find_existing_node(child_dip) == NULL);
25860Sstevel@tonic-gate 
25873630Slg150142 	usba_name_buf = kmem_zalloc(USBA_MAX_COMPAT_NAMES *
25883630Slg150142 	    USBA_MAX_COMPAT_NAME_LEN, KM_SLEEP);
25893630Slg150142 
25903630Slg150142 	for (i = 0; i < USBA_MAX_COMPAT_NAMES; i++) {
25913630Slg150142 		usba_name[i] = usba_name_buf + (i * USBA_MAX_COMPAT_NAME_LEN);
25923630Slg150142 	}
25933630Slg150142 
25940Sstevel@tonic-gate 	n = 0;
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	if (force_bind) {
25970Sstevel@tonic-gate 		(void) ndi_devi_set_nodename(child_dip, force_bind, 0);
25980Sstevel@tonic-gate 		(void) strncpy(usba_name[n++], force_bind,
25996112Sqz150045 		    USBA_MAX_COMPAT_NAME_LEN);
26000Sstevel@tonic-gate 	}
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	/* 1) usbifVID,PID.REV.configCN.IN */
26030Sstevel@tonic-gate 	(void) sprintf(usba_name[n++],
26046112Sqz150045 	    "usbif%x,%x.%x.config%x.%x",
26056112Sqz150045 	    usb_dev_descr->idVendor,
26066112Sqz150045 	    usb_dev_descr->idProduct,
26076112Sqz150045 	    usb_dev_descr->bcdDevice,
26086112Sqz150045 	    child_ud->usb_cfg_value,
26096112Sqz150045 	    intf);
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	/* 2) usbifVID,PID.configCN.IN */
26120Sstevel@tonic-gate 	(void) sprintf(usba_name[n++],
26136112Sqz150045 	    "usbif%x,%x.config%x.%x",
26146112Sqz150045 	    usb_dev_descr->idVendor,
26156112Sqz150045 	    usb_dev_descr->idProduct,
26166112Sqz150045 	    child_ud->usb_cfg_value,
26176112Sqz150045 	    intf);
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 	if (if_descr.bInterfaceClass) {
26210Sstevel@tonic-gate 		/* 3) usbifVID,classIC.ISC.IPROTO */
26220Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
26236112Sqz150045 		    "usbif%x,class%x.%x.%x",
26246112Sqz150045 		    usb_dev_descr->idVendor,
26256112Sqz150045 		    if_descr.bInterfaceClass,
26266112Sqz150045 		    if_descr.bInterfaceSubClass,
26276112Sqz150045 		    if_descr.bInterfaceProtocol);
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 		/* 4) usbifVID,classIC.ISC */
26300Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
26316112Sqz150045 		    "usbif%x,class%x.%x",
26326112Sqz150045 		    usb_dev_descr->idVendor,
26336112Sqz150045 		    if_descr.bInterfaceClass,
26346112Sqz150045 		    if_descr.bInterfaceSubClass);
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 		/* 5) usbifVID,classIC */
26370Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
26386112Sqz150045 		    "usbif%x,class%x",
26396112Sqz150045 		    usb_dev_descr->idVendor,
26406112Sqz150045 		    if_descr.bInterfaceClass);
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 		/* 6) usbif,classIC.ISC.IPROTO */
26430Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
26446112Sqz150045 		    "usbif,class%x.%x.%x",
26456112Sqz150045 		    if_descr.bInterfaceClass,
26466112Sqz150045 		    if_descr.bInterfaceSubClass,
26476112Sqz150045 		    if_descr.bInterfaceProtocol);
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 		/* 7) usbif,classIC.ISC */
26500Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
26516112Sqz150045 		    "usbif,class%x.%x",
26526112Sqz150045 		    if_descr.bInterfaceClass,
26536112Sqz150045 		    if_descr.bInterfaceSubClass);
26540Sstevel@tonic-gate 
26550Sstevel@tonic-gate 		/* 8) usbif,classIC */
26560Sstevel@tonic-gate 		(void) sprintf(usba_name[n++],
26576112Sqz150045 		    "usbif,class%x",
26586112Sqz150045 		    if_descr.bInterfaceClass);
26590Sstevel@tonic-gate 	}
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 	if (usba_get_ugen_binding(child_dip) ==
26620Sstevel@tonic-gate 	    USBA_UGEN_INTERFACE_BINDING) {
26630Sstevel@tonic-gate 		/* 9) ugen */
26640Sstevel@tonic-gate 		(void) sprintf(usba_name[n++], "ugen");
26650Sstevel@tonic-gate 	}
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	for (i = 0; i < n; i += 2) {
26680Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
26693630Slg150142 		    "compatible name:\t%s\t%s", usba_name[i],
26703630Slg150142 		    (((i+1) < n)? usba_name[i+1] : ""));
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	/* create compatible property */
26743630Slg150142 	rval = ndi_prop_update_string_array(DDI_DEV_T_NONE, child_dip,
26753630Slg150142 	    "compatible", (char **)usba_name, n);
26763630Slg150142 
26773630Slg150142 	kmem_free(usba_name_buf, USBA_MAX_COMPAT_NAMES *
26783630Slg150142 	    USBA_MAX_COMPAT_NAME_LEN);
26793630Slg150142 
26803630Slg150142 	if (rval != DDI_PROP_SUCCESS) {
26813630Slg150142 
26823630Slg150142 		goto fail;
26830Sstevel@tonic-gate 	}
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	/* update the address property */
26860Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
26876112Sqz150045 	    "assigned-address", child_ud->usb_addr);
26880Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
2689978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
26900Sstevel@tonic-gate 		    "usba_ready_interface_node: address update failed");
26910Sstevel@tonic-gate 	}
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 	/* create property with if number */
26940Sstevel@tonic-gate 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, child_dip,
26956112Sqz150045 	    "interface", intf);
26960Sstevel@tonic-gate 
26970Sstevel@tonic-gate 	if (rval != DDI_PROP_SUCCESS) {
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 		goto fail;
27000Sstevel@tonic-gate 	}
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
27030Sstevel@tonic-gate 	    "%s%d port %d: %s, dip = 0x%p",
27040Sstevel@tonic-gate 	    ddi_node_name(ddi_get_parent(dip)),
27050Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(dip)),
27066898Sfb209375 	    child_ud->usb_port, ddi_node_name(child_dip), (void *)child_dip);
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 	usba_set_usba_device(child_dip, child_ud);
27090Sstevel@tonic-gate 	ASSERT(!mutex_owned(&(usba_get_usba_device(child_dip)->usb_mutex)));
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	return (child_dip);
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate fail:
27140Sstevel@tonic-gate 	(void) usba_destroy_child_devi(child_dip, NDI_DEVI_REMOVE);
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	return (NULL);
27170Sstevel@tonic-gate }
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate /*
27210Sstevel@tonic-gate  * retrieve string descriptors for manufacturer, vendor and serial
27220Sstevel@tonic-gate  * number
27230Sstevel@tonic-gate  */
27240Sstevel@tonic-gate void
27250Sstevel@tonic-gate usba_get_dev_string_descrs(dev_info_t *dip, usba_device_t *ud)
27260Sstevel@tonic-gate {
27270Sstevel@tonic-gate 	char	*tmpbuf, *str;
27280Sstevel@tonic-gate 	int	l;
27290Sstevel@tonic-gate 	usb_dev_descr_t *usb_dev_descr = ud->usb_dev_descr;
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
27330Sstevel@tonic-gate 	    "usba_get_usb_string_descr: m=%d, p=%d, s=%d",
27340Sstevel@tonic-gate 	    usb_dev_descr->iManufacturer,
27350Sstevel@tonic-gate 	    usb_dev_descr->iProduct,
27360Sstevel@tonic-gate 	    usb_dev_descr->iSerialNumber);
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	tmpbuf = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	/* fetch manufacturer string */
27410Sstevel@tonic-gate 	if ((ud->usb_mfg_str == NULL) && usb_dev_descr->iManufacturer &&
27420Sstevel@tonic-gate 	    (usb_get_string_descr(dip, USB_LANG_ID,
27436112Sqz150045 	    usb_dev_descr->iManufacturer, tmpbuf, USB_MAXSTRINGLEN) ==
27446112Sqz150045 	    USB_SUCCESS)) {
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 		l = strlen(tmpbuf);
27470Sstevel@tonic-gate 		if (l > 0) {
27480Sstevel@tonic-gate 			str = kmem_zalloc(l + 1, KM_SLEEP);
27490Sstevel@tonic-gate 			mutex_enter(&ud->usb_mutex);
27500Sstevel@tonic-gate 			ud->usb_mfg_str = str;
27510Sstevel@tonic-gate 			(void) strcpy(ud->usb_mfg_str, tmpbuf);
27520Sstevel@tonic-gate 			mutex_exit(&ud->usb_mutex);
27530Sstevel@tonic-gate 		}
27540Sstevel@tonic-gate 	}
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 	/* fetch product string */
27570Sstevel@tonic-gate 	if ((ud->usb_product_str == NULL) && usb_dev_descr->iProduct &&
27580Sstevel@tonic-gate 	    (usb_get_string_descr(dip, USB_LANG_ID, usb_dev_descr->iProduct,
27590Sstevel@tonic-gate 	    tmpbuf, USB_MAXSTRINGLEN) ==
27606112Sqz150045 	    USB_SUCCESS)) {
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 		l = strlen(tmpbuf);
27630Sstevel@tonic-gate 		if (l > 0) {
27640Sstevel@tonic-gate 			str = kmem_zalloc(l + 1, KM_SLEEP);
27650Sstevel@tonic-gate 			mutex_enter(&ud->usb_mutex);
27660Sstevel@tonic-gate 			ud->usb_product_str = str;
27670Sstevel@tonic-gate 			(void) strcpy(ud->usb_product_str, tmpbuf);
27680Sstevel@tonic-gate 			mutex_exit(&ud->usb_mutex);
27690Sstevel@tonic-gate 		}
27700Sstevel@tonic-gate 	}
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate 	/* fetch device serial number string */
27730Sstevel@tonic-gate 	if ((ud->usb_serialno_str == NULL) && usb_dev_descr->iSerialNumber &&
27740Sstevel@tonic-gate 	    (usb_get_string_descr(dip, USB_LANG_ID,
27756112Sqz150045 	    usb_dev_descr->iSerialNumber, tmpbuf, USB_MAXSTRINGLEN) ==
27766112Sqz150045 	    USB_SUCCESS)) {
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 		l = strlen(tmpbuf);
27790Sstevel@tonic-gate 		if (l > 0) {
27800Sstevel@tonic-gate 			str = kmem_zalloc(l + 1, KM_SLEEP);
27810Sstevel@tonic-gate 			mutex_enter(&ud->usb_mutex);
27820Sstevel@tonic-gate 			ud->usb_serialno_str = str;
27830Sstevel@tonic-gate 			(void) strcpy(ud->usb_serialno_str, tmpbuf);
27840Sstevel@tonic-gate 			mutex_exit(&ud->usb_mutex);
27850Sstevel@tonic-gate 		}
27860Sstevel@tonic-gate 	}
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 	kmem_free(tmpbuf, USB_MAXSTRINGLEN);
27890Sstevel@tonic-gate }
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate /*
27930Sstevel@tonic-gate  * usba_str_startcmp:
27940Sstevel@tonic-gate  *	Return the number of characters duplicated from the beginning of the
27950Sstevel@tonic-gate  *	string.  Return -1 if a complete duplicate.
27960Sstevel@tonic-gate  *
27970Sstevel@tonic-gate  * Arguments:
27980Sstevel@tonic-gate  *	Two strings to compare.
27990Sstevel@tonic-gate  */
28000Sstevel@tonic-gate static int usba_str_startcmp(char *first, char *second)
28010Sstevel@tonic-gate {
28020Sstevel@tonic-gate 	int num_same_chars = 0;
28030Sstevel@tonic-gate 	while (*first == *second++) {
28040Sstevel@tonic-gate 		if (*first++ == '\0') {
28050Sstevel@tonic-gate 			return (-1);
28060Sstevel@tonic-gate 		}
28070Sstevel@tonic-gate 		num_same_chars++;
28080Sstevel@tonic-gate 	}
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	return (num_same_chars);
28110Sstevel@tonic-gate }
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate /*
28150Sstevel@tonic-gate  * usba_get_mfg_prod_sn_str:
28160Sstevel@tonic-gate  *	Return a string containing mfg, product, serial number strings.
28170Sstevel@tonic-gate  *	Remove duplicates if some strings are the same.
28180Sstevel@tonic-gate  *
28190Sstevel@tonic-gate  * Arguments:
28200Sstevel@tonic-gate  *	dip	- pointer to dev info
28210Sstevel@tonic-gate  *	buffer	- Where string is returned
28220Sstevel@tonic-gate  *	buflen	- Length of buffer
28230Sstevel@tonic-gate  *
28240Sstevel@tonic-gate  * Returns:
28250Sstevel@tonic-gate  *	Same as second arg.
28260Sstevel@tonic-gate  */
28270Sstevel@tonic-gate char *
28280Sstevel@tonic-gate usba_get_mfg_prod_sn_str(
28290Sstevel@tonic-gate     dev_info_t	*dip,
28300Sstevel@tonic-gate     char	*buffer,
28310Sstevel@tonic-gate     int		buflen)
28320Sstevel@tonic-gate {
28330Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
28340Sstevel@tonic-gate 	int return_len = 0;
28350Sstevel@tonic-gate 	int len = 0;
28360Sstevel@tonic-gate 	int duplen;
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 	buffer[0] = '\0';
28390Sstevel@tonic-gate 	buffer[buflen-1] = '\0';
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	if ((usba_device->usb_mfg_str) &&
28420Sstevel@tonic-gate 	    ((len = strlen(usba_device->usb_mfg_str)) != 0)) {
28430Sstevel@tonic-gate 		(void) strncpy(buffer, usba_device->usb_mfg_str, buflen - 1);
28440Sstevel@tonic-gate 		return_len = min(buflen - 1, len);
28450Sstevel@tonic-gate 	}
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	/* Product string exists to append. */
28480Sstevel@tonic-gate 	if ((usba_device->usb_product_str) &&
28490Sstevel@tonic-gate 	    ((len = strlen(usba_device->usb_product_str)) != 0)) {
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate 		/* Append only parts of string that don't match mfg string. */
28520Sstevel@tonic-gate 		duplen = usba_str_startcmp(buffer,
28536112Sqz150045 		    usba_device->usb_product_str);
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 		if (duplen != -1) {		/* Not a complete match. */
28560Sstevel@tonic-gate 			if (return_len > 0) {
28570Sstevel@tonic-gate 				buffer[return_len++] = ' ';
28580Sstevel@tonic-gate 			}
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 			/* Skip over the dup part of the concat'ed string. */
28610Sstevel@tonic-gate 			len -= duplen;
28620Sstevel@tonic-gate 			(void) strncpy(&buffer[return_len],
28630Sstevel@tonic-gate 			    &usba_device->usb_product_str[duplen],
28640Sstevel@tonic-gate 			    buflen - return_len - 1);
28650Sstevel@tonic-gate 			return_len = min(buflen - 1, return_len + len);
28660Sstevel@tonic-gate 		}
28670Sstevel@tonic-gate 	}
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate 	if ((usba_device->usb_serialno_str) &&
28700Sstevel@tonic-gate 	    ((len = strlen(usba_device->usb_serialno_str)) != 0)) {
28710Sstevel@tonic-gate 		if (return_len > 0) {
28720Sstevel@tonic-gate 			buffer[return_len++] = ' ';
28730Sstevel@tonic-gate 		}
28740Sstevel@tonic-gate 		(void) strncpy(&buffer[return_len],
28756112Sqz150045 		    usba_device->usb_serialno_str,
28766112Sqz150045 		    buflen - return_len - 1);
28770Sstevel@tonic-gate 	}
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 	return (buffer);
28800Sstevel@tonic-gate }
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate /*
28840Sstevel@tonic-gate  * USB enumeration statistic functions
28850Sstevel@tonic-gate  */
28860Sstevel@tonic-gate 
28870Sstevel@tonic-gate /*
28880Sstevel@tonic-gate  * Increments the hotplug statistics based on flags.
28890Sstevel@tonic-gate  */
28900Sstevel@tonic-gate void
28910Sstevel@tonic-gate usba_update_hotplug_stats(dev_info_t *dip, usb_flags_t flags)
28920Sstevel@tonic-gate {
28930Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(dip);
28940Sstevel@tonic-gate 	usba_hcdi_t	*hcdi =
28956112Sqz150045 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate 	mutex_enter(&hcdi->hcdi_mutex);
28980Sstevel@tonic-gate 	if (flags & USBA_TOTAL_HOTPLUG_SUCCESS) {
28990Sstevel@tonic-gate 		hcdi->hcdi_total_hotplug_success++;
29000Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->
29010Sstevel@tonic-gate 		    hcdi_hotplug_total_success.value.ui64++;
29020Sstevel@tonic-gate 	}
29030Sstevel@tonic-gate 	if (flags & USBA_HOTPLUG_SUCCESS) {
29040Sstevel@tonic-gate 		hcdi->hcdi_hotplug_success++;
29050Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->
29060Sstevel@tonic-gate 		    hcdi_hotplug_success.value.ui64++;
29070Sstevel@tonic-gate 	}
29080Sstevel@tonic-gate 	if (flags & USBA_TOTAL_HOTPLUG_FAILURE) {
29090Sstevel@tonic-gate 		hcdi->hcdi_total_hotplug_failure++;
29100Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->
29110Sstevel@tonic-gate 		    hcdi_hotplug_total_failure.value.ui64++;
29120Sstevel@tonic-gate 	}
29130Sstevel@tonic-gate 	if (flags & USBA_HOTPLUG_FAILURE) {
29140Sstevel@tonic-gate 		hcdi->hcdi_hotplug_failure++;
29150Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS_DATA(hcdi)->
29160Sstevel@tonic-gate 		    hcdi_hotplug_failure.value.ui64++;
29170Sstevel@tonic-gate 	}
29180Sstevel@tonic-gate 	mutex_exit(&hcdi->hcdi_mutex);
29190Sstevel@tonic-gate }
29200Sstevel@tonic-gate 
29210Sstevel@tonic-gate 
29220Sstevel@tonic-gate /*
29230Sstevel@tonic-gate  * Retrieve the current enumeration statistics
29240Sstevel@tonic-gate  */
29250Sstevel@tonic-gate void
29260Sstevel@tonic-gate usba_get_hotplug_stats(dev_info_t *dip, ulong_t *total_success,
29270Sstevel@tonic-gate     ulong_t *success, ulong_t *total_failure, ulong_t *failure,
29280Sstevel@tonic-gate     uchar_t *device_count)
29290Sstevel@tonic-gate {
29300Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(dip);
29310Sstevel@tonic-gate 	usba_hcdi_t	*hcdi =
29326112Sqz150045 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate 	mutex_enter(&hcdi->hcdi_mutex);
29350Sstevel@tonic-gate 	*total_success = hcdi->hcdi_total_hotplug_success;
29360Sstevel@tonic-gate 	*success = hcdi->hcdi_hotplug_success;
29370Sstevel@tonic-gate 	*total_failure = hcdi->hcdi_total_hotplug_failure;
29380Sstevel@tonic-gate 	*failure = hcdi->hcdi_hotplug_failure;
29390Sstevel@tonic-gate 	*device_count = hcdi->hcdi_device_count;
29400Sstevel@tonic-gate 	mutex_exit(&hcdi->hcdi_mutex);
29410Sstevel@tonic-gate }
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate /*
29450Sstevel@tonic-gate  * Reset the resetable hotplug stats
29460Sstevel@tonic-gate  */
29470Sstevel@tonic-gate void
29480Sstevel@tonic-gate usba_reset_hotplug_stats(dev_info_t *dip)
29490Sstevel@tonic-gate {
29500Sstevel@tonic-gate 	usba_device_t	*usba_device = usba_get_usba_device(dip);
29510Sstevel@tonic-gate 	usba_hcdi_t	*hcdi =
29526112Sqz150045 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
29530Sstevel@tonic-gate 	hcdi_hotplug_stats_t *hsp;
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 	mutex_enter(&hcdi->hcdi_mutex);
29560Sstevel@tonic-gate 	hcdi->hcdi_hotplug_success = 0;
29570Sstevel@tonic-gate 	hcdi->hcdi_hotplug_failure = 0;
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 	hsp = HCDI_HOTPLUG_STATS_DATA(hcdi);
29600Sstevel@tonic-gate 	hsp->hcdi_hotplug_success.value.ui64 = 0;
29610Sstevel@tonic-gate 	hsp->hcdi_hotplug_failure.value.ui64 = 0;
29620Sstevel@tonic-gate 	mutex_exit(&hcdi->hcdi_mutex);
29630Sstevel@tonic-gate }
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate /*
29670Sstevel@tonic-gate  * usba_bind_driver():
29680Sstevel@tonic-gate  *	This function calls ndi_devi_bind_driver() which tries to
29690Sstevel@tonic-gate  *	bind a driver to the device.  If the driver binding fails
29700Sstevel@tonic-gate  *	we get an rval of NDI_UNBOUD and report an error to the
29710Sstevel@tonic-gate  *	syslog that the driver failed binding.
29720Sstevel@tonic-gate  *	If rval is something other than NDI_UNBOUND we report an
29730Sstevel@tonic-gate  *	error to the console.
29740Sstevel@tonic-gate  *
29750Sstevel@tonic-gate  *	This function returns USB_SUCCESS if no errors were
29760Sstevel@tonic-gate  *	encountered while binding.
29770Sstevel@tonic-gate  */
29780Sstevel@tonic-gate int
29790Sstevel@tonic-gate usba_bind_driver(dev_info_t *dip)
29800Sstevel@tonic-gate {
29810Sstevel@tonic-gate 	int	rval;
29820Sstevel@tonic-gate 	char	*name;
29830Sstevel@tonic-gate 	uint8_t if_num = usba_get_ifno(dip);
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
29866898Sfb209375 	    "usba_bind_driver: dip = 0x%p, if_num = 0x%x", (void *)dip, if_num);
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	/* bind device to the driver */
29910Sstevel@tonic-gate 	if ((rval = ndi_devi_bind_driver(dip, 0)) != NDI_SUCCESS) {
29920Sstevel@tonic-gate 		/* if we fail to bind report an error */
29930Sstevel@tonic-gate 		(void) usba_get_mfg_prod_sn_str(dip, name, MAXNAMELEN);
29940Sstevel@tonic-gate 		if (name[0] != '\0') {
29950Sstevel@tonic-gate 			if (!usb_owns_device(dip)) {
29960Sstevel@tonic-gate 				USB_DPRINTF_L1(DPRINT_MASK_USBA,
29970Sstevel@tonic-gate 				    usba_log_handle,
29980Sstevel@tonic-gate 				    "no driver found for "
29990Sstevel@tonic-gate 				    "interface %d (nodename: '%s') of %s",
30000Sstevel@tonic-gate 				    if_num, ddi_node_name(dip), name);
30010Sstevel@tonic-gate 			} else {
30020Sstevel@tonic-gate 				USB_DPRINTF_L1(DPRINT_MASK_USBA,
30030Sstevel@tonic-gate 				    usba_log_handle,
30040Sstevel@tonic-gate 				    "no driver found for device %s", name);
30050Sstevel@tonic-gate 			}
30060Sstevel@tonic-gate 		} else {
30070Sstevel@tonic-gate 			(void) ddi_pathname(dip, name);
30080Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_USBA,
30090Sstevel@tonic-gate 			    usba_log_handle,
30100Sstevel@tonic-gate 			    "no driver found for device %s", name);
30110Sstevel@tonic-gate 		}
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate 		kmem_free(name, MAXNAMELEN);
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 		return (USB_FAILURE);
30160Sstevel@tonic-gate 	}
30170Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 	return ((rval == NDI_SUCCESS) ? USB_SUCCESS : USB_FAILURE);
30200Sstevel@tonic-gate }
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate /*
30240Sstevel@tonic-gate  * usba_get_hc_dma_attr:
30250Sstevel@tonic-gate  *	function returning dma attributes of the HCD
30260Sstevel@tonic-gate  *
30270Sstevel@tonic-gate  * Arguments:
30280Sstevel@tonic-gate  *	dip	- pointer to devinfo of the client
30290Sstevel@tonic-gate  *
30300Sstevel@tonic-gate  * Return Values:
30310Sstevel@tonic-gate  *	hcdi_dma_attr
30320Sstevel@tonic-gate  */
30330Sstevel@tonic-gate ddi_dma_attr_t *
30340Sstevel@tonic-gate usba_get_hc_dma_attr(dev_info_t *dip)
30350Sstevel@tonic-gate {
30360Sstevel@tonic-gate 	usba_device_t *usba_device = usba_get_usba_device(dip);
30370Sstevel@tonic-gate 	usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip);
30380Sstevel@tonic-gate 
30390Sstevel@tonic-gate 	return (hcdi->hcdi_dma_attr);
30400Sstevel@tonic-gate }
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate /*
30440Sstevel@tonic-gate  * usba_check_for_leaks:
30450Sstevel@tonic-gate  *	check usba_device structure for leaks
30460Sstevel@tonic-gate  *
30470Sstevel@tonic-gate  * Arguments:
30480Sstevel@tonic-gate  *	usba_device	- usba_device structure pointer
30490Sstevel@tonic-gate  */
30500Sstevel@tonic-gate void
30510Sstevel@tonic-gate usba_check_for_leaks(usba_device_t *usba_device)
30520Sstevel@tonic-gate {
30530Sstevel@tonic-gate 	int i, ph_open_cnt, req_wrp_leaks, iface;
30540Sstevel@tonic-gate 	int leaks = 0;
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usba_log_handle,
30570Sstevel@tonic-gate 	    "usba_check_for_leaks: %s%d usba_device=0x%p",
30580Sstevel@tonic-gate 	    ddi_driver_name(usba_device->usb_dip),
30596898Sfb209375 	    ddi_get_instance(usba_device->usb_dip), (void *)usba_device);
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	/*
30620Sstevel@tonic-gate 	 * default pipe is still open
30630Sstevel@tonic-gate 	 * all other pipes should be closed
30640Sstevel@tonic-gate 	 */
30650Sstevel@tonic-gate 	for (ph_open_cnt = 0, i = 1; i < USBA_N_ENDPOINTS; i++) {
30660Sstevel@tonic-gate 		usba_ph_impl_t *ph_impl =
30670Sstevel@tonic-gate 		    &usba_device->usb_ph_list[i];
30680Sstevel@tonic-gate 		if (ph_impl->usba_ph_data) {
3069978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA,
30700Sstevel@tonic-gate 			    usba_log_handle,
30710Sstevel@tonic-gate 			    "%s%d: leaking pipehandle=0x%p (0x%p) ep_addr=0x%x",
30720Sstevel@tonic-gate 			    ddi_driver_name(ph_impl->usba_ph_data->p_dip),
30730Sstevel@tonic-gate 			    ddi_get_instance(ph_impl->usba_ph_data->p_dip),
30746898Sfb209375 			    (void *)ph_impl,
30756898Sfb209375 			    (void *)ph_impl->usba_ph_data,
30760Sstevel@tonic-gate 			    ph_impl->usba_ph_ep.bEndpointAddress);
30770Sstevel@tonic-gate 			ph_open_cnt++;
30780Sstevel@tonic-gate 			leaks++;
30790Sstevel@tonic-gate #ifndef DEBUG
30800Sstevel@tonic-gate 			usb_pipe_close(ph_impl->usba_ph_data->p_dip,
30810Sstevel@tonic-gate 			    (usb_pipe_handle_t)ph_impl, USB_FLAGS_SLEEP,
30820Sstevel@tonic-gate 			    NULL, NULL);
30830Sstevel@tonic-gate #endif
30840Sstevel@tonic-gate 		}
30850Sstevel@tonic-gate 	}
30860Sstevel@tonic-gate 	req_wrp_leaks =  usba_list_entry_leaks(&usba_device->
30870Sstevel@tonic-gate 	    usb_allocated, "request wrappers");
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate 	ASSERT(ph_open_cnt == 0);
30900Sstevel@tonic-gate 	ASSERT(req_wrp_leaks == 0);
30910Sstevel@tonic-gate 
30920Sstevel@tonic-gate 	if (req_wrp_leaks) {
30930Sstevel@tonic-gate 		usba_list_entry_t *entry;
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate 		while ((entry = usba_rm_first_from_list(
30960Sstevel@tonic-gate 		    &usba_device->usb_allocated)) != NULL) {
30970Sstevel@tonic-gate 			usba_req_wrapper_t *wrp;
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 			mutex_enter(&entry->list_mutex);
31000Sstevel@tonic-gate 			wrp = (usba_req_wrapper_t *)entry->private;
31010Sstevel@tonic-gate 			mutex_exit(&entry->list_mutex);
31020Sstevel@tonic-gate 			leaks++;
31030Sstevel@tonic-gate 
3104978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA,
31050Sstevel@tonic-gate 			    usba_log_handle,
31060Sstevel@tonic-gate 			    "%s%d: leaking request 0x%p",
31070Sstevel@tonic-gate 			    ddi_driver_name(wrp->wr_dip),
31080Sstevel@tonic-gate 			    ddi_get_instance(wrp->wr_dip),
31096898Sfb209375 			    (void *)wrp->wr_req);
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate 			/*
31120Sstevel@tonic-gate 			 * put it back, usba_req_wrapper_free
31130Sstevel@tonic-gate 			 * expects it on the list
31140Sstevel@tonic-gate 			 */
31150Sstevel@tonic-gate 			usba_add_to_list(&usba_device->usb_allocated,
31160Sstevel@tonic-gate 			    &wrp->wr_allocated_list);
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate 			usba_req_wrapper_free(wrp);
31190Sstevel@tonic-gate 		}
31200Sstevel@tonic-gate 	}
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
31230Sstevel@tonic-gate 	for (iface = 0; iface < usba_device->usb_n_ifs; iface++) {
31240Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBA, usba_log_handle,
31250Sstevel@tonic-gate 		    "usba_check_for_leaks: if=%d client_flags=0x%x",
31260Sstevel@tonic-gate 		    iface, usba_device->usb_client_flags[iface]);
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 		if (usba_device->usb_client_flags[iface] &
31290Sstevel@tonic-gate 		    USBA_CLIENT_FLAG_DEV_DATA) {
31300Sstevel@tonic-gate 			usb_client_dev_data_list_t *entry =
31310Sstevel@tonic-gate 			    usba_device->usb_client_dev_data_list.cddl_next;
31320Sstevel@tonic-gate 			usb_client_dev_data_list_t *next;
31330Sstevel@tonic-gate 			usb_client_dev_data_t *dev_data;
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 			while (entry) {
31360Sstevel@tonic-gate 				dev_info_t *dip = entry->cddl_dip;
31370Sstevel@tonic-gate 				next = entry->cddl_next;
31380Sstevel@tonic-gate 				dev_data = entry->cddl_dev_data;
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 
31411333Scth 				if (!i_ddi_devi_attached(dip)) {
3142978Sfrits 					USB_DPRINTF_L2(DPRINT_MASK_USBA,
31430Sstevel@tonic-gate 					    usba_log_handle,
31440Sstevel@tonic-gate 					    "%s%d: leaking dev_data 0x%p",
31450Sstevel@tonic-gate 					    ddi_driver_name(dip),
31460Sstevel@tonic-gate 					    ddi_get_instance(dip),
31470Sstevel@tonic-gate 					    (void *)dev_data);
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 					leaks++;
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 					mutex_exit(&usba_device->usb_mutex);
31520Sstevel@tonic-gate 					usb_free_dev_data(dip, dev_data);
31530Sstevel@tonic-gate 					mutex_enter(&usba_device->usb_mutex);
31540Sstevel@tonic-gate 				}
31550Sstevel@tonic-gate 
31560Sstevel@tonic-gate 				entry = next;
31570Sstevel@tonic-gate 			}
31580Sstevel@tonic-gate 		}
31590Sstevel@tonic-gate 		if (usba_device->usb_client_flags[iface] &
31600Sstevel@tonic-gate 		    USBA_CLIENT_FLAG_ATTACH) {
31610Sstevel@tonic-gate 			dev_info_t *dip = usba_device->
31626112Sqz150045 			    usb_client_attach_list[iface].dip;
31630Sstevel@tonic-gate 
3164978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA,
31650Sstevel@tonic-gate 			    usba_log_handle,
31660Sstevel@tonic-gate 			    "%s%d: did no usb_client_detach",
31670Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
31680Sstevel@tonic-gate 			leaks++;
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
31710Sstevel@tonic-gate 			usb_client_detach(dip, NULL);
31720Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
31730Sstevel@tonic-gate 
31740Sstevel@tonic-gate 			usba_device->
31750Sstevel@tonic-gate 			    usb_client_attach_list[iface].dip = NULL;
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 			usba_device->usb_client_flags[iface] &=
31780Sstevel@tonic-gate 			    ~USBA_CLIENT_FLAG_ATTACH;
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 		}
31810Sstevel@tonic-gate 		if (usba_device->usb_client_flags[iface] &
31820Sstevel@tonic-gate 		    USBA_CLIENT_FLAG_EV_CBS) {
31830Sstevel@tonic-gate 			dev_info_t *dip =
31840Sstevel@tonic-gate 			    usba_device->usb_client_ev_cb_list[iface].
31856112Sqz150045 			    dip;
31860Sstevel@tonic-gate 			usb_event_t *ev_data =
31870Sstevel@tonic-gate 			    usba_device->usb_client_ev_cb_list[iface].
31886112Sqz150045 			    ev_data;
31890Sstevel@tonic-gate 
3190978Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBA,
31910Sstevel@tonic-gate 			    usba_log_handle,
31920Sstevel@tonic-gate 			    "%s%d: did no usb_unregister_event_cbs",
31930Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
31940Sstevel@tonic-gate 			leaks++;
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate 			mutex_exit(&usba_device->usb_mutex);
31970Sstevel@tonic-gate 			usb_unregister_event_cbs(dip, ev_data);
31980Sstevel@tonic-gate 			mutex_enter(&usba_device->usb_mutex);
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate 			usba_device->usb_client_ev_cb_list[iface].
32016112Sqz150045 			    dip = NULL;
32020Sstevel@tonic-gate 			usba_device->usb_client_ev_cb_list[iface].
32036112Sqz150045 			    ev_data = NULL;
32040Sstevel@tonic-gate 			usba_device->usb_client_flags[iface] &=
32050Sstevel@tonic-gate 			    ~USBA_CLIENT_FLAG_EV_CBS;
32060Sstevel@tonic-gate 		}
32070Sstevel@tonic-gate 	}
32080Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 	if (leaks) {
3211978Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBA, usba_log_handle,
32120Sstevel@tonic-gate 		    "all %d leaks fixed", leaks);
32130Sstevel@tonic-gate 	}
32140Sstevel@tonic-gate }
3215