xref: /illumos-gate/usr/src/uts/common/io/usb/usba/usbai.c (revision 602e131c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ffcd51f3Slg  * Common Development and Distribution License (the "License").
6ffcd51f3Slg  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
206f6c7d2bSVincent Wang  */
216f6c7d2bSVincent Wang /*
226f6c7d2bSVincent Wang  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230d2006e4SRobert Mustacchi  * Copyright 2019, Joyent, Inc.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * USBA: Solaris USB Architecture support
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * all functions exposed to client drivers  have prefix usb_ while all USBA
317c478bd9Sstevel@tonic-gate  * internal functions or functions exposed to HCD or hubd only have prefix
327c478bd9Sstevel@tonic-gate  * usba_
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * this file contains initializations, logging/tracing support and PM
357c478bd9Sstevel@tonic-gate  * support
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate #define	USBA_FRAMEWORK
387c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
39d29f5a71Szhigang lu - Sun Microsystems - Beijing China #include <sys/strsun.h>
407c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
414610e4a0Sfrits #include <sys/usb/usba/hcdi_impl.h>
427c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba10.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * print buffer protected by mutex for debug stuff. the mutex also
467c478bd9Sstevel@tonic-gate  * ensures serializing debug messages
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate static kmutex_t	usba_print_mutex;
497c478bd9Sstevel@tonic-gate static char usba_print_buf[USBA_PRINT_BUF_LEN];
507c478bd9Sstevel@tonic-gate kmutex_t usbai_mutex;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * debug stuff
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate usb_log_handle_t	usbai_log_handle;
564610e4a0Sfrits uint_t			usbai_errlevel = USB_LOG_L4;
574610e4a0Sfrits uint_t			usbai_errmask = (uint_t)-1;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	USBA_DEBUG_SIZE_EXTRA_ALLOC	8
607c478bd9Sstevel@tonic-gate #ifdef	DEBUG
614610e4a0Sfrits #define	USBA_DEBUG_BUF_SIZE \
624610e4a0Sfrits 			(0x40000 -  USBA_DEBUG_SIZE_EXTRA_ALLOC)
637c478bd9Sstevel@tonic-gate #else
644610e4a0Sfrits #define	USBA_DEBUG_BUF_SIZE \
654610e4a0Sfrits 			(0x4000 -  USBA_DEBUG_SIZE_EXTRA_ALLOC)
667c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	USBA_POWER_STR_SIZE		40
697c478bd9Sstevel@tonic-gate 
704610e4a0Sfrits int	usba_suppress_dprintf;		/* Suppress debug printing */
714610e4a0Sfrits int	usba_clear_debug_buf_flag;	/* clear debug buf */
724610e4a0Sfrits int	usba_buffer_dprintf = 1;	/* Use a debug print buffer */
734610e4a0Sfrits int	usba_timestamp_dprintf = 0;	/* get time stamps in trace */
744610e4a0Sfrits int	usba_debug_buf_size = USBA_DEBUG_BUF_SIZE;	/* Size of debug buf */
754610e4a0Sfrits int	usba_debug_chatty;		/* L1 msg on console */
764610e4a0Sfrits 
774610e4a0Sfrits static char *usba_debug_buf = NULL;	/* The debug buf */
787c478bd9Sstevel@tonic-gate static char *usba_buf_sptr, *usba_buf_eptr;
794610e4a0Sfrits static hrtime_t usba_last_timestamp;	/* last time stamp in trace */
807c478bd9Sstevel@tonic-gate 
81*602e131cSToomas Soome usb_dev_cap_t usb_cap;
82*602e131cSToomas Soome 
837c478bd9Sstevel@tonic-gate /* USBA framework initializations */
847c478bd9Sstevel@tonic-gate void
usba_usbai_initialization()857c478bd9Sstevel@tonic-gate usba_usbai_initialization()
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	usbai_log_handle = usb_alloc_log_hdl(NULL, "usbai", &usbai_errlevel,
88ffcd51f3Slg 	    &usbai_errmask, NULL, 0);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
917c478bd9Sstevel@tonic-gate 	    "usba_usbai_initialization");
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	mutex_init(&usba_print_mutex, NULL, MUTEX_DRIVER, NULL);
947c478bd9Sstevel@tonic-gate 	mutex_init(&usbai_mutex, NULL, MUTEX_DRIVER, NULL);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* USBA framework destroys */
997c478bd9Sstevel@tonic-gate void
usba_usbai_destroy()1007c478bd9Sstevel@tonic-gate usba_usbai_destroy()
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1037c478bd9Sstevel@tonic-gate 	    "usba_usbai_destroy");
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	mutex_destroy(&usba_print_mutex);
1067c478bd9Sstevel@tonic-gate 	mutex_destroy(&usbai_mutex);
1077c478bd9Sstevel@tonic-gate 	if (usba_debug_buf) {
1087c478bd9Sstevel@tonic-gate 		kmem_free(usba_debug_buf,
1097c478bd9Sstevel@tonic-gate 		    usba_debug_buf_size + USBA_DEBUG_SIZE_EXTRA_ALLOC);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	usb_free_log_hdl(usbai_log_handle);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * debug, log, and console message handling
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate usb_log_handle_t
usb_alloc_log_hdl(dev_info_t * dip,char * name,uint_t * errlevel,uint_t * mask,uint_t * instance_filter,usb_flags_t flags)1207c478bd9Sstevel@tonic-gate usb_alloc_log_hdl(dev_info_t *dip, char *name,
1210d2006e4SRobert Mustacchi     uint_t *errlevel, uint_t *mask, uint_t *instance_filter,
1220d2006e4SRobert Mustacchi     usb_flags_t flags)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	usba_log_handle_impl_t	*hdl;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	USBA_CHECK_CONTEXT();
1277c478bd9Sstevel@tonic-gate 	hdl = kmem_zalloc(sizeof (*hdl), KM_SLEEP);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	hdl->lh_dip = dip;
1307c478bd9Sstevel@tonic-gate 	if (dip && (name == NULL)) {
1317c478bd9Sstevel@tonic-gate 		hdl->lh_name = (char *)ddi_driver_name(dip);
1327c478bd9Sstevel@tonic-gate 	} else {
1337c478bd9Sstevel@tonic-gate 		hdl->lh_name = name;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	hdl->lh_errlevel = errlevel;
1367c478bd9Sstevel@tonic-gate 	hdl->lh_mask = mask;
1377c478bd9Sstevel@tonic-gate 	hdl->lh_instance_filter = instance_filter;
1387c478bd9Sstevel@tonic-gate 	hdl->lh_flags = flags;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #ifdef __lock_lint
1417c478bd9Sstevel@tonic-gate 	(void) usb_alloc_log_handle(dip, name, errlevel, mask,
1427c478bd9Sstevel@tonic-gate 	    instance_filter, 0, flags);
1437c478bd9Sstevel@tonic-gate 	usb_free_log_handle(NULL);
1447c478bd9Sstevel@tonic-gate #endif
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return ((usb_log_handle_t)hdl);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1517c478bd9Sstevel@tonic-gate usb_log_handle_t
usb_alloc_log_handle(dev_info_t * dip,char * name,uint_t * errlevel,uint_t * mask,uint_t * instance_filter,uint_t reserved,usb_flags_t flags)1527c478bd9Sstevel@tonic-gate usb_alloc_log_handle(dev_info_t *dip, char *name,
1530d2006e4SRobert Mustacchi     uint_t *errlevel, uint_t *mask, uint_t *instance_filter,
1540d2006e4SRobert Mustacchi     uint_t reserved, usb_flags_t flags)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	return (usb_alloc_log_hdl(dip, name, errlevel, mask,
1577c478bd9Sstevel@tonic-gate 	    instance_filter, flags));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate void
usb_free_log_handle(usb_log_handle_t handle)1617c478bd9Sstevel@tonic-gate usb_free_log_handle(usb_log_handle_t handle)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	if (handle) {
1647c478bd9Sstevel@tonic-gate 		kmem_free(handle, sizeof (usba_log_handle_impl_t));
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate void
usb_free_log_hdl(usb_log_handle_t handle)1697c478bd9Sstevel@tonic-gate usb_free_log_hdl(usb_log_handle_t handle)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	if (handle) {
1727c478bd9Sstevel@tonic-gate 		kmem_free(handle, sizeof (usba_log_handle_impl_t));
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static void
usba_clear_dprint_buf()1787c478bd9Sstevel@tonic-gate usba_clear_dprint_buf()
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	if (usba_debug_buf) {
1817c478bd9Sstevel@tonic-gate 		usba_buf_sptr = usba_debug_buf;
1827c478bd9Sstevel@tonic-gate 		usba_buf_eptr = usba_debug_buf + usba_debug_buf_size;
1837c478bd9Sstevel@tonic-gate 		bzero(usba_debug_buf, usba_debug_buf_size +
184ffcd51f3Slg 		    USBA_DEBUG_SIZE_EXTRA_ALLOC);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate #ifdef DEBUG
1907c478bd9Sstevel@tonic-gate char *
usba_dbuf_tail(uint_t lines)1917c478bd9Sstevel@tonic-gate usba_dbuf_tail(uint_t lines)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	int	count;
1947c478bd9Sstevel@tonic-gate 	char	*r = NULL;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_print_mutex);
1977c478bd9Sstevel@tonic-gate 	if (usba_debug_buf) {
1987c478bd9Sstevel@tonic-gate 		count = 0;
1997c478bd9Sstevel@tonic-gate 		r = usba_buf_sptr;
2007c478bd9Sstevel@tonic-gate 		while ((count < lines) && (r > usba_debug_buf)) {
2017c478bd9Sstevel@tonic-gate 			if (*r == '\n') {
2027c478bd9Sstevel@tonic-gate 				count++;
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 			r--;
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_print_mutex);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	return (r);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static void usb_vprintf(dev_info_t *, int, char *, char *, va_list)
2157c478bd9Sstevel@tonic-gate 	__KVPRINTFLIKE(4);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static void
usb_vprintf(dev_info_t * dip,int level,char * label,char * fmt,va_list ap)2187c478bd9Sstevel@tonic-gate usb_vprintf(dev_info_t *dip, int level, char *label, char *fmt, va_list ap)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	size_t len;
2210d2006e4SRobert Mustacchi 	int instance = 0;
2227c478bd9Sstevel@tonic-gate 	char driver_name[USBA_DRVNAME_LEN];
2237c478bd9Sstevel@tonic-gate 	char *msg_ptr;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (usba_suppress_dprintf) {
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		return;
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	*driver_name = '\0';
2317c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_print_mutex);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * Check if we have a valid buf size?
2357c478bd9Sstevel@tonic-gate 	 * Suppress logging to usb_buffer if so.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	if (usba_debug_buf_size <= 0) {
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		usba_buffer_dprintf = 0;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * if there is label and dip, use <driver name><instance>:
2447c478bd9Sstevel@tonic-gate 	 * otherwise just use the label
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate 	if (dip) {
2477c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
2487c478bd9Sstevel@tonic-gate 		(void) snprintf(driver_name, USBA_DRVNAME_LEN,
2497c478bd9Sstevel@tonic-gate 		    "%s%d", ddi_driver_name(dip), instance);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (label == (char *)NULL) {
2537c478bd9Sstevel@tonic-gate 		len = snprintf(usba_print_buf, USBA_PRINT_BUF_LEN, "\t");
2547c478bd9Sstevel@tonic-gate 	} else if (usba_timestamp_dprintf) {
2557c478bd9Sstevel@tonic-gate 		hrtime_t t = gethrtime();
2567c478bd9Sstevel@tonic-gate 		hrtime_t elapsed = (t - usba_last_timestamp)/1000;
2577c478bd9Sstevel@tonic-gate 		usba_last_timestamp = t;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		if (dip) {
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 			len = snprintf(usba_print_buf, USBA_PRINT_BUF_LEN,
2627c478bd9Sstevel@tonic-gate 			    "+%lld->%p: %s%d: ", elapsed,
2637c478bd9Sstevel@tonic-gate 			    (void *)curthread, label, instance);
2647c478bd9Sstevel@tonic-gate 		} else {
2657c478bd9Sstevel@tonic-gate 			len = snprintf(usba_print_buf, USBA_PRINT_BUF_LEN,
2667c478bd9Sstevel@tonic-gate 			    "+%lld->%p: %s: ", elapsed,
2677c478bd9Sstevel@tonic-gate 			    (void *)curthread, label);
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 	} else {
2707c478bd9Sstevel@tonic-gate 		if (dip) {
2717c478bd9Sstevel@tonic-gate 			len = snprintf(usba_print_buf, USBA_PRINT_BUF_LEN,
2727c478bd9Sstevel@tonic-gate 			    "%s%d:\t", label, instance);
2737c478bd9Sstevel@tonic-gate 		} else {
2747c478bd9Sstevel@tonic-gate 			len = snprintf(usba_print_buf, USBA_PRINT_BUF_LEN,
2757c478bd9Sstevel@tonic-gate 			    "%s:\t", label);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	msg_ptr = usba_print_buf + len;
2817c478bd9Sstevel@tonic-gate 	(void) vsnprintf(msg_ptr, USBA_PRINT_BUF_LEN - len - 2, fmt, ap);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	len = min(strlen(usba_print_buf), USBA_PRINT_BUF_LEN - 2);
2847c478bd9Sstevel@tonic-gate 	usba_print_buf[len++] = '\n';
2857c478bd9Sstevel@tonic-gate 	usba_print_buf[len] = '\0';
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * stuff the message in the debug buf
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	if (usba_buffer_dprintf) {
2917c478bd9Sstevel@tonic-gate 		if (usba_debug_buf == NULL) {
2927c478bd9Sstevel@tonic-gate 			usba_debug_buf = kmem_alloc(
2937c478bd9Sstevel@tonic-gate 			    usba_debug_buf_size + USBA_DEBUG_SIZE_EXTRA_ALLOC,
2947c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
2957c478bd9Sstevel@tonic-gate 			usba_clear_dprint_buf();
2967c478bd9Sstevel@tonic-gate 		} else if (usba_clear_debug_buf_flag) {
2977c478bd9Sstevel@tonic-gate 			usba_clear_dprint_buf();
2987c478bd9Sstevel@tonic-gate 			usba_clear_debug_buf_flag = 0;
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		/*
3027c478bd9Sstevel@tonic-gate 		 * overwrite >>>> that might be over the end of the
3037c478bd9Sstevel@tonic-gate 		 * the buffer
3047c478bd9Sstevel@tonic-gate 		 */
3057c478bd9Sstevel@tonic-gate 		*(usba_debug_buf + usba_debug_buf_size) = '\0';
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		if ((usba_buf_sptr + len) > usba_buf_eptr) {
308d29f5a71Szhigang lu - Sun Microsystems - Beijing China 			size_t left = _PTRDIFF(usba_buf_eptr, usba_buf_sptr);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 			bcopy(usba_print_buf, usba_buf_sptr, left);
3117c478bd9Sstevel@tonic-gate 			bcopy((caddr_t)usba_print_buf + left,
312ffcd51f3Slg 			    usba_debug_buf, len - left);
3137c478bd9Sstevel@tonic-gate 			usba_buf_sptr = usba_debug_buf + len - left;
3147c478bd9Sstevel@tonic-gate 		} else {
3157c478bd9Sstevel@tonic-gate 			bcopy(usba_print_buf, usba_buf_sptr, len);
3167c478bd9Sstevel@tonic-gate 			usba_buf_sptr += len;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 		/* add marker */
3197c478bd9Sstevel@tonic-gate 		(void) sprintf(usba_buf_sptr, ">>>>");
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * L4-L2 message may go to the log buf if not logged in usba_debug_buf
3247c478bd9Sstevel@tonic-gate 	 * L1 messages will go to the log buf in non-debug kernels and
3257c478bd9Sstevel@tonic-gate 	 * to console and log buf in debug kernels if usba_debug_chatty
3267c478bd9Sstevel@tonic-gate 	 * has been set
3277c478bd9Sstevel@tonic-gate 	 * L0 messages are warnings and will go to console and log buf and
3287c478bd9Sstevel@tonic-gate 	 * include the pathname, if available
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	switch (level) {
3327c478bd9Sstevel@tonic-gate 	case USB_LOG_L4:
3337c478bd9Sstevel@tonic-gate 	case USB_LOG_L3:
3347c478bd9Sstevel@tonic-gate 	case USB_LOG_L2:
3357c478bd9Sstevel@tonic-gate 		if (!usba_buffer_dprintf) {
3367c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "^%s", usba_print_buf);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 		break;
3397c478bd9Sstevel@tonic-gate 	case USB_LOG_L1:
3407c478bd9Sstevel@tonic-gate 		if (dip) {
3417c478bd9Sstevel@tonic-gate 			char *pathname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
3427c478bd9Sstevel@tonic-gate 			if (pathname) {
3437c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
3447c478bd9Sstevel@tonic-gate 				    usba_debug_chatty ?
3457c478bd9Sstevel@tonic-gate 				    "%s (%s): %s" : "?%s (%s): %s",
3467c478bd9Sstevel@tonic-gate 				    ddi_pathname(dip, pathname),
3477c478bd9Sstevel@tonic-gate 				    driver_name, msg_ptr);
3487c478bd9Sstevel@tonic-gate 				kmem_free(pathname, MAXPATHLEN);
3497c478bd9Sstevel@tonic-gate 			} else {
3507c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
3517c478bd9Sstevel@tonic-gate 				    usba_debug_chatty ?
3527c478bd9Sstevel@tonic-gate 				    "%s" : "?%s", usba_print_buf);
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 		} else {
3557c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
3567c478bd9Sstevel@tonic-gate 			    usba_debug_chatty ? "%s" : "?%s",
3577c478bd9Sstevel@tonic-gate 			    usba_print_buf);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 		break;
3607c478bd9Sstevel@tonic-gate 	case USB_LOG_L0:
3617c478bd9Sstevel@tonic-gate 		/* Strip the "\n" added earlier */
3627c478bd9Sstevel@tonic-gate 		if (usba_print_buf[len - 1] == '\n') {
3637c478bd9Sstevel@tonic-gate 			usba_print_buf[len - 1] = '\0';
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		if (msg_ptr[len - 1] == '\n') {
3667c478bd9Sstevel@tonic-gate 			msg_ptr[len - 1] = '\0';
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 		if (dip) {
3697c478bd9Sstevel@tonic-gate 			char *pathname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
3707c478bd9Sstevel@tonic-gate 			if (pathname) {
3717c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s (%s): %s",
3727c478bd9Sstevel@tonic-gate 				    ddi_pathname(dip, pathname),
3737c478bd9Sstevel@tonic-gate 				    driver_name, msg_ptr);
3747c478bd9Sstevel@tonic-gate 				kmem_free(pathname, MAXPATHLEN);
3757c478bd9Sstevel@tonic-gate 			} else {
3767c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, usba_print_buf);
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 		} else {
3797c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, usba_print_buf);
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		break;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_print_mutex);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate int
3887c478bd9Sstevel@tonic-gate usba_vlog(usb_log_handle_t, uint_t, uint_t, char *, va_list)
3890d2006e4SRobert Mustacchi     __KVPRINTFLIKE(4);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /* When usba10_calls.c goes away, this function can be made static again. */
3927c478bd9Sstevel@tonic-gate int
usba_vlog(usb_log_handle_t handle,uint_t level,uint_t mask,char * fmt,va_list ap)3937c478bd9Sstevel@tonic-gate usba_vlog(usb_log_handle_t handle, uint_t level, uint_t mask,
3947c478bd9Sstevel@tonic-gate     char *fmt, va_list ap)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	usba_log_handle_impl_t *hdl = (usba_log_handle_impl_t *)handle;
3977c478bd9Sstevel@tonic-gate 	char *label;
3987c478bd9Sstevel@tonic-gate 	uint_t hdl_errlevel, hdl_mask, hdl_instance_filter;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/* if there is no handle, use usba as label */
4017c478bd9Sstevel@tonic-gate 	if (hdl == NULL) {
4027c478bd9Sstevel@tonic-gate 		usb_vprintf(NULL, level, "usba", fmt, ap);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/* look up the filters and set defaults */
4087c478bd9Sstevel@tonic-gate 	if (hdl->lh_errlevel) {
4097c478bd9Sstevel@tonic-gate 		hdl_errlevel = *(hdl->lh_errlevel);
4107c478bd9Sstevel@tonic-gate 	} else {
4117c478bd9Sstevel@tonic-gate 		hdl_errlevel = 0;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (hdl->lh_mask) {
4157c478bd9Sstevel@tonic-gate 		hdl_mask = *(hdl->lh_mask);
4167c478bd9Sstevel@tonic-gate 	} else {
4177c478bd9Sstevel@tonic-gate 		hdl_mask = (uint_t)-1;
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (hdl->lh_instance_filter) {
4217c478bd9Sstevel@tonic-gate 		hdl_instance_filter = *(hdl->lh_instance_filter);
4227c478bd9Sstevel@tonic-gate 	} else {
4237c478bd9Sstevel@tonic-gate 		hdl_instance_filter = (uint_t)-1;
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/* if threshold is lower or mask doesn't match, we are done */
4277c478bd9Sstevel@tonic-gate 	if ((level > hdl_errlevel) || ((mask & hdl_mask) == 0)) {
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * if we have a dip, and it is not a warning, check
4347c478bd9Sstevel@tonic-gate 	 * the instance number
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	if (hdl->lh_dip && (level > USB_LOG_L0)) {
4377c478bd9Sstevel@tonic-gate 		if ((hdl_instance_filter != (uint_t)-1) &&
4387c478bd9Sstevel@tonic-gate 		    (ddi_get_instance(hdl->lh_dip) != hdl_instance_filter)) {
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	label = hdl->lh_name;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	usb_vprintf(hdl->lh_dip, level, label, fmt, ap);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate void
usb_dprintf4(uint_t mask,usb_log_handle_t handle,char * fmt,...)4537c478bd9Sstevel@tonic-gate usb_dprintf4(uint_t mask, usb_log_handle_t handle, char *fmt, ...)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	va_list ap;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
4587c478bd9Sstevel@tonic-gate 	(void) usba_vlog(handle, USB_LOG_L4, mask, fmt, ap);
4597c478bd9Sstevel@tonic-gate 	va_end(ap);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate void
usb_dprintf3(uint_t mask,usb_log_handle_t handle,char * fmt,...)4647c478bd9Sstevel@tonic-gate usb_dprintf3(uint_t mask, usb_log_handle_t handle, char *fmt, ...)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	va_list ap;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
4697c478bd9Sstevel@tonic-gate 	(void) usba_vlog(handle, USB_LOG_L3, mask, fmt, ap);
4707c478bd9Sstevel@tonic-gate 	va_end(ap);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate void
usb_dprintf2(uint_t mask,usb_log_handle_t handle,char * fmt,...)4757c478bd9Sstevel@tonic-gate usb_dprintf2(uint_t mask, usb_log_handle_t handle, char *fmt, ...)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	va_list ap;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
4807c478bd9Sstevel@tonic-gate 	(void) usba_vlog(handle, USB_LOG_L2, mask, fmt, ap);
4817c478bd9Sstevel@tonic-gate 	va_end(ap);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate void
usb_dprintf1(uint_t mask,usb_log_handle_t handle,char * fmt,...)4867c478bd9Sstevel@tonic-gate usb_dprintf1(uint_t mask, usb_log_handle_t handle, char *fmt, ...)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	va_list ap;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
4917c478bd9Sstevel@tonic-gate 	(void) usba_vlog(handle, USB_LOG_L1, mask, fmt, ap);
4927c478bd9Sstevel@tonic-gate 	va_end(ap);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate void
usb_dprintf0(uint_t mask,usb_log_handle_t handle,char * fmt,...)4977c478bd9Sstevel@tonic-gate usb_dprintf0(uint_t mask, usb_log_handle_t handle, char *fmt, ...)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	va_list ap;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
5027c478bd9Sstevel@tonic-gate 	(void) usba_vlog(handle, USB_LOG_L0, mask, fmt, ap);
5037c478bd9Sstevel@tonic-gate 	va_end(ap);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate int
usb_log(usb_log_handle_t handle,uint_t level,uint_t mask,char * fmt,...)5087c478bd9Sstevel@tonic-gate usb_log(usb_log_handle_t handle, uint_t level, uint_t mask, char *fmt, ...)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	va_list	ap;
5117c478bd9Sstevel@tonic-gate 	int rval;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
5147c478bd9Sstevel@tonic-gate 	rval = usba_vlog(handle, level, mask, fmt, ap);
5157c478bd9Sstevel@tonic-gate 	va_end(ap);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	return (rval);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * Provide a default configuration power descriptor
5237c478bd9Sstevel@tonic-gate  */
5247c478bd9Sstevel@tonic-gate usba_cfg_pwr_descr_t	default_cfg_power = {
5257c478bd9Sstevel@tonic-gate 	18,	/* bLength */
5267c478bd9Sstevel@tonic-gate 	USBA_DESCR_TYPE_CFG_PWR_1_1, /* bDescriptorType */
5277c478bd9Sstevel@tonic-gate 	0,	/* SelfPowerConsumedD0_l */
5287c478bd9Sstevel@tonic-gate 	0,	/* SelfPowerConsumedD0_h */
5297c478bd9Sstevel@tonic-gate 	0,	/* bPowerSummaryId */
5307c478bd9Sstevel@tonic-gate 	0,	/* bBusPowerSavingD1 */
5317c478bd9Sstevel@tonic-gate 	0,	/* bSelfPowerSavingD1 */
5327c478bd9Sstevel@tonic-gate 	0,	/* bBusPowerSavingD2 */
5337c478bd9Sstevel@tonic-gate 	0,	/* bSelfPowerSavingD2 */
5347c478bd9Sstevel@tonic-gate 	100,	/* bBusPowerSavingD3 */
5357c478bd9Sstevel@tonic-gate 	100,	/* bSelfPowerSavingD3 */
5367c478bd9Sstevel@tonic-gate 	0,	/* TransitionTimeFromD1 */
5377c478bd9Sstevel@tonic-gate 	0,	/* TransitionTimeFromD2 */
5387c478bd9Sstevel@tonic-gate 	10,	/* TransitionTimeFromD3 1 Second */
5397c478bd9Sstevel@tonic-gate };
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate  * Provide a default interface power descriptor
5447c478bd9Sstevel@tonic-gate  */
5457c478bd9Sstevel@tonic-gate usba_if_pwr_descr_t default_if_power = {
5467c478bd9Sstevel@tonic-gate 	15,	/* bLength */
5477c478bd9Sstevel@tonic-gate 	USBA_DESCR_TYPE_IF_PWR_1_1, /* bDescriptorType */
5487c478bd9Sstevel@tonic-gate 	8,	/* bmCapabilitiesFlags */
5497c478bd9Sstevel@tonic-gate 	0,	/* bBusPowerSavingD1 */
5507c478bd9Sstevel@tonic-gate 	0,	/* bSelfPowerSavingD1 */
5517c478bd9Sstevel@tonic-gate 	0,	/* bBusPowerSavingD2 */
5527c478bd9Sstevel@tonic-gate 	0,	/* bSelfPowerSavingD2 */
5537c478bd9Sstevel@tonic-gate 	100,	/* bBusPowerSavingD3 */
5547c478bd9Sstevel@tonic-gate 	100,	/* bSelfPowerSavingD3 */
5557c478bd9Sstevel@tonic-gate 	0,	/* TransitionTimeFromD1 */
5567c478bd9Sstevel@tonic-gate 	0,	/* TransitionTimeFromD2 */
5577c478bd9Sstevel@tonic-gate 	10,	/* TransitionTimeFromD3 1 Second */
5587c478bd9Sstevel@tonic-gate };
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate static void
usba_async_req_raise_power(void * arg)5627c478bd9Sstevel@tonic-gate usba_async_req_raise_power(void *arg)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	usba_pm_req_t *pmrq = (usba_pm_req_t *)arg;
5657c478bd9Sstevel@tonic-gate 	int rval;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/*
5687c478bd9Sstevel@tonic-gate 	 * To eliminate race condition between the call to power entry
5697c478bd9Sstevel@tonic-gate 	 * point and our call to raise power level, we first mark the
5707c478bd9Sstevel@tonic-gate 	 * component busy and later idle
5717c478bd9Sstevel@tonic-gate 	 */
5727c478bd9Sstevel@tonic-gate 	(void) pm_busy_component(pmrq->dip, pmrq->comp);
5737c478bd9Sstevel@tonic-gate 	rval = pm_raise_power(pmrq->dip, pmrq->comp, pmrq->level);
5747c478bd9Sstevel@tonic-gate 	(void) pm_idle_component(pmrq->dip, pmrq->comp);
5757c478bd9Sstevel@tonic-gate 	pmrq->cb(pmrq->arg, rval);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/* We are done with pmrq. Free it now */
5787c478bd9Sstevel@tonic-gate 	kmem_free(pmrq, sizeof (usba_pm_req_t));
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate /* usb function to perform async pm_request_power_change */
5837c478bd9Sstevel@tonic-gate int
usb_req_raise_power(dev_info_t * dip,int comp,int level,void (* callback)(void *,int),void * arg,usb_flags_t flags)5847c478bd9Sstevel@tonic-gate usb_req_raise_power(dev_info_t *dip, int comp, int level,
5850d2006e4SRobert Mustacchi     void (*callback)(void *, int), void *arg, usb_flags_t flags)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	usba_pm_req_t *pmrq;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (flags & USB_FLAGS_SLEEP) {
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		return (pm_raise_power(dip, comp, level));
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if ((pmrq = kmem_alloc(sizeof (usba_pm_req_t), KM_NOSLEEP)) ==
5957c478bd9Sstevel@tonic-gate 	    NULL) {
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	pmrq->dip = dip;
6017c478bd9Sstevel@tonic-gate 	pmrq->comp = comp;
6027c478bd9Sstevel@tonic-gate 	pmrq->level = level;
6037c478bd9Sstevel@tonic-gate 	pmrq->cb = callback;
6047c478bd9Sstevel@tonic-gate 	pmrq->arg = arg;
6057c478bd9Sstevel@tonic-gate 	pmrq->flags = flags;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (usb_async_req(dip, usba_async_req_raise_power,
6087c478bd9Sstevel@tonic-gate 	    (void *)pmrq, USB_FLAGS_NOSLEEP | USB_FLAGS_NOQUEUE) !=
6097c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
6107c478bd9Sstevel@tonic-gate 		kmem_free(pmrq, sizeof (usba_pm_req_t));
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate static void
usba_async_req_lower_power(void * arg)6207c478bd9Sstevel@tonic-gate usba_async_req_lower_power(void *arg)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	usba_pm_req_t *pmrq = (usba_pm_req_t *)arg;
6237c478bd9Sstevel@tonic-gate 	int rval;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * To eliminate race condition between the call to power entry
6277c478bd9Sstevel@tonic-gate 	 * point and our call to lower power level, we call idle component
6287c478bd9Sstevel@tonic-gate 	 * to push ahead the PM timestamp
6297c478bd9Sstevel@tonic-gate 	 */
6307c478bd9Sstevel@tonic-gate 	(void) pm_idle_component(pmrq->dip, pmrq->comp);
6317c478bd9Sstevel@tonic-gate 	rval = pm_lower_power(pmrq->dip, pmrq->comp, pmrq->level);
6327c478bd9Sstevel@tonic-gate 	pmrq->cb(pmrq->arg, rval);
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate /* usb function to perform async pm_request_power_change */
6377c478bd9Sstevel@tonic-gate int
usb_req_lower_power(dev_info_t * dip,int comp,int level,void (* callback)(void *,int),void * arg,usb_flags_t flags)6387c478bd9Sstevel@tonic-gate usb_req_lower_power(dev_info_t *dip, int comp, int level,
6390d2006e4SRobert Mustacchi     void (*callback)(void *, int), void *arg, usb_flags_t flags)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	usba_pm_req_t *pmrq;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	if (flags & USB_FLAGS_SLEEP) {
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		return (pm_lower_power(dip, comp, level));
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if ((pmrq = kmem_alloc(sizeof (usba_pm_req_t), KM_NOSLEEP)) ==
6497c478bd9Sstevel@tonic-gate 	    NULL) {
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	pmrq->dip = dip;
6557c478bd9Sstevel@tonic-gate 	pmrq->comp = comp;
6567c478bd9Sstevel@tonic-gate 	pmrq->level = level;
6577c478bd9Sstevel@tonic-gate 	pmrq->cb = callback;
6587c478bd9Sstevel@tonic-gate 	pmrq->arg = arg;
6597c478bd9Sstevel@tonic-gate 	pmrq->flags = flags;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (usb_async_req(dip, usba_async_req_lower_power,
6627c478bd9Sstevel@tonic-gate 	    (void *)pmrq, USB_FLAGS_NOSLEEP | USB_FLAGS_NOQUEUE) !=
6637c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
6647c478bd9Sstevel@tonic-gate 		kmem_free(pmrq, sizeof (usba_pm_req_t));
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /* function to see if pm is enabled for this device */
6747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6757c478bd9Sstevel@tonic-gate int
usb_is_pm_enabled(dev_info_t * dip)6767c478bd9Sstevel@tonic-gate usb_is_pm_enabled(dev_info_t *dip)
6777c478bd9Sstevel@tonic-gate {
6786f6c7d2bSVincent Wang 	/*
6796f6c7d2bSVincent Wang 	 * At this point we should assume that all devices
6806f6c7d2bSVincent Wang 	 * are capable of supporting PM
6816f6c7d2bSVincent Wang 	 */
6826f6c7d2bSVincent Wang 	return (USB_SUCCESS);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate  * usba_handle_device_remote_wakeup:
6887c478bd9Sstevel@tonic-gate  *	internal function to enable/disable remote wakeup in the device
6897c478bd9Sstevel@tonic-gate  *	or interface
6907c478bd9Sstevel@tonic-gate  */
6917c478bd9Sstevel@tonic-gate static int
usba_handle_device_remote_wakeup(dev_info_t * dip,int cmd)6927c478bd9Sstevel@tonic-gate usba_handle_device_remote_wakeup(dev_info_t *dip, int cmd)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	int		rval;
695*602e131cSToomas Soome 	uint8_t		bmRequest = USB_DEV_REQ_HOST_TO_DEV;
6967c478bd9Sstevel@tonic-gate 	uchar_t		bRequest;
6977c478bd9Sstevel@tonic-gate 	uint16_t	wIndex = 0;
6987c478bd9Sstevel@tonic-gate 	usb_cr_t	completion_reason = 0;
6997c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	cb_flags;
7007c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t ph;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBA, usbai_log_handle,
703112116d8Sfb 	    "usba_handle_device_remote_wakeup: dip = 0x%p", (void *)dip);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	USBA_CHECK_CONTEXT();
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/* get the default pipe */
7087c478bd9Sstevel@tonic-gate 	ph = usba_get_dflt_pipe_handle(dip);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/* do we own the device? */
7117c478bd9Sstevel@tonic-gate 	if (usb_owns_device(dip)) {
7127c478bd9Sstevel@tonic-gate 		bmRequest |= USB_DEV_REQ_RCPT_DEV;
7137c478bd9Sstevel@tonic-gate 	} else {
7147c478bd9Sstevel@tonic-gate 		bmRequest |= USB_DEV_REQ_RCPT_IF;
7157c478bd9Sstevel@tonic-gate 		wIndex = usba_get_ifno(dip);
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 	bRequest = ((cmd == USB_REMOTE_WAKEUP_ENABLE) ? USB_REQ_SET_FEATURE :
7187c478bd9Sstevel@tonic-gate 	    USB_REQ_CLEAR_FEATURE);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if ((rval = usb_pipe_sync_ctrl_xfer(dip, ph,
7217c478bd9Sstevel@tonic-gate 	    bmRequest,			/* bmRequest */
7227c478bd9Sstevel@tonic-gate 	    bRequest,			/* bRequest */
7237c478bd9Sstevel@tonic-gate 	    USB_DEV_REMOTE_WAKEUP,	/* wValue */
7247c478bd9Sstevel@tonic-gate 	    wIndex,			/* wIndex */
7257c478bd9Sstevel@tonic-gate 	    0,				/* wLength */
7267c478bd9Sstevel@tonic-gate 	    NULL, 0,
7277c478bd9Sstevel@tonic-gate 	    &completion_reason,
7287c478bd9Sstevel@tonic-gate 	    &cb_flags, USB_FLAGS_SLEEP)) != USB_SUCCESS) {
7297c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
7307c478bd9Sstevel@tonic-gate 		    "Set/ClearFeature (RemoteWakep) failed: "
7317c478bd9Sstevel@tonic-gate 		    "rval = %d, cmd = %d, cr = 0x%x cb = 0x%x",
7327c478bd9Sstevel@tonic-gate 		    rval, cmd, completion_reason, cb_flags);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	return (rval);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate void
usb_enable_parent_notification(dev_info_t * dip)7407c478bd9Sstevel@tonic-gate usb_enable_parent_notification(dev_info_t *dip)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	USBA_CHECK_CONTEXT();
7437c478bd9Sstevel@tonic-gate 	(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
7447c478bd9Sstevel@tonic-gate 	    "pm-want-child-notification?");
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate  * usb_handle_remote_wakeup:
7507c478bd9Sstevel@tonic-gate  *	check if device supports remote wakeup and, if so, enable/disable
7517c478bd9Sstevel@tonic-gate  *	remote wake up in the device depending upon the command
7527c478bd9Sstevel@tonic-gate  */
7537c478bd9Sstevel@tonic-gate int
usb_handle_remote_wakeup(dev_info_t * dip,int cmd)7547c478bd9Sstevel@tonic-gate usb_handle_remote_wakeup(dev_info_t *dip, int cmd)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t	cfg_descr;
757*602e131cSToomas Soome 	uchar_t		*usb_cfg;	/* buf for config descriptor */
7587c478bd9Sstevel@tonic-gate 	size_t		cfg_length;
7597c478bd9Sstevel@tonic-gate 	int		rval;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	USBA_CHECK_CONTEXT();
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/* Obtain the raw configuration descriptor */
7647c478bd9Sstevel@tonic-gate 	usb_cfg = usb_get_raw_cfg_data(dip, &cfg_length);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	/* get configuration descriptor, must succeed */
7677c478bd9Sstevel@tonic-gate 	rval = usb_parse_cfg_descr(usb_cfg, cfg_length,
7687c478bd9Sstevel@tonic-gate 	    &cfg_descr, USB_CFG_DESCR_SIZE);
7697c478bd9Sstevel@tonic-gate 	ASSERT(rval == USB_CFG_DESCR_SIZE);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	/*
7727c478bd9Sstevel@tonic-gate 	 * If the device supports remote wakeup, and PM is enabled,
7737c478bd9Sstevel@tonic-gate 	 * we enable remote wakeup in the device
7747c478bd9Sstevel@tonic-gate 	 */
7757c478bd9Sstevel@tonic-gate 	if ((usb_is_pm_enabled(dip) == USB_SUCCESS) &&
7767c478bd9Sstevel@tonic-gate 	    (cfg_descr.bmAttributes & USB_CFG_ATTR_REMOTE_WAKEUP)) {
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		rval = usba_handle_device_remote_wakeup(dip, cmd);
7797c478bd9Sstevel@tonic-gate 	} else {
7807c478bd9Sstevel@tonic-gate 		rval = USB_FAILURE;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	return (rval);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * usb_create_pm_components:
7897c478bd9Sstevel@tonic-gate  *	map descriptor into  pm properties
7907c478bd9Sstevel@tonic-gate  */
7917c478bd9Sstevel@tonic-gate int
usb_create_pm_components(dev_info_t * dip,uint_t * pwr_states)7927c478bd9Sstevel@tonic-gate usb_create_pm_components(dev_info_t *dip, uint_t *pwr_states)
7937c478bd9Sstevel@tonic-gate {
794*602e131cSToomas Soome 	uchar_t			*usb_cfg;	/* buf for config descriptor */
7957c478bd9Sstevel@tonic-gate 	usb_cfg_descr_t		cfg_descr;
7967c478bd9Sstevel@tonic-gate 	size_t			cfg_length;
7977c478bd9Sstevel@tonic-gate 	usba_cfg_pwr_descr_t	confpwr_descr;
7987c478bd9Sstevel@tonic-gate 	usba_if_pwr_descr_t	ifpwr_descr;
799*602e131cSToomas Soome 	uint8_t			cfg_attrib;
8007c478bd9Sstevel@tonic-gate 	int			i, lvl, rval;
8017c478bd9Sstevel@tonic-gate 	int			n_prop = 0;
802*602e131cSToomas Soome 	uint8_t			*ptr;
8037c478bd9Sstevel@tonic-gate 	char			*drvname;
8047c478bd9Sstevel@tonic-gate 	char			str[USBA_POWER_STR_SIZE];
8057c478bd9Sstevel@tonic-gate 	char			*pm_comp[USBA_N_PMCOMP];
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	USBA_CHECK_CONTEXT();
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if (usb_is_pm_enabled(dip) != USB_SUCCESS) {
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/* Obtain the raw configuration descriptor */
8157c478bd9Sstevel@tonic-gate 	usb_cfg = usb_get_raw_cfg_data(dip, &cfg_length);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	/* get configuration descriptor, must succceed */
8187c478bd9Sstevel@tonic-gate 	rval = usb_parse_cfg_descr(usb_cfg, cfg_length,
8197c478bd9Sstevel@tonic-gate 	    &cfg_descr, USB_CFG_DESCR_SIZE);
8207c478bd9Sstevel@tonic-gate 	ASSERT(rval == USB_CFG_DESCR_SIZE);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	cfg_attrib = cfg_descr.bmAttributes;
8237c478bd9Sstevel@tonic-gate 	*pwr_states = 0;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
8267c478bd9Sstevel@tonic-gate 	 * Now start creating the pm-components strings
8277c478bd9Sstevel@tonic-gate 	 */
8287c478bd9Sstevel@tonic-gate 	drvname = (char *)ddi_driver_name(dip);
8297c478bd9Sstevel@tonic-gate 	(void) snprintf(str, USBA_POWER_STR_SIZE, "NAME= %s%d Power",
8307c478bd9Sstevel@tonic-gate 	    drvname, ddi_get_instance(dip));
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	pm_comp[n_prop] = kmem_zalloc(strlen(str) + 1, KM_SLEEP);
8337c478bd9Sstevel@tonic-gate 	(void) strcpy(pm_comp[n_prop++], str);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/*
8367c478bd9Sstevel@tonic-gate 	 * if the device is bus powered we look at the bBusPowerSavingDx
8377c478bd9Sstevel@tonic-gate 	 * fields else we look at bSelfPowerSavingDx fields.
8387c478bd9Sstevel@tonic-gate 	 * OS and USB power states are numerically reversed,
8397c478bd9Sstevel@tonic-gate 	 *
8407c478bd9Sstevel@tonic-gate 	 * Here is the mapping :-
8417c478bd9Sstevel@tonic-gate 	 *	OS State	USB State
8427c478bd9Sstevel@tonic-gate 	 *	0		D3	(minimal or no power)
8437c478bd9Sstevel@tonic-gate 	 *	1		D2
8447c478bd9Sstevel@tonic-gate 	 *	2		D1
8457c478bd9Sstevel@tonic-gate 	 *	3		D0	(Full power)
8467c478bd9Sstevel@tonic-gate 	 *
8477c478bd9Sstevel@tonic-gate 	 * if we own the whole device, we look at the config pwr descr
8487c478bd9Sstevel@tonic-gate 	 * else at the interface pwr descr.
8497c478bd9Sstevel@tonic-gate 	 */
8507c478bd9Sstevel@tonic-gate 	if (usb_owns_device(dip)) {
8517c478bd9Sstevel@tonic-gate 		/* Parse the configuration power descriptor */
8527c478bd9Sstevel@tonic-gate 		rval = usba_parse_cfg_pwr_descr(usb_cfg, cfg_length,
8537c478bd9Sstevel@tonic-gate 		    &confpwr_descr, USBA_CFG_PWR_DESCR_SIZE);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		if (rval != USBA_CFG_PWR_DESCR_SIZE) {
8567c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8577c478bd9Sstevel@tonic-gate 			    "usb_create_pm_components: "
8587c478bd9Sstevel@tonic-gate 			    "usb_parse_cfg_pwr_descr returns length of %d, "
8597c478bd9Sstevel@tonic-gate 			    "expecting %d", rval, USBA_CFG_PWR_DESCR_SIZE);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 		if (cfg_attrib & USB_CFG_ATTR_SELFPWR) {
8657c478bd9Sstevel@tonic-gate 			ptr = &confpwr_descr.bSelfPowerSavingD3;
8667c478bd9Sstevel@tonic-gate 		} else {
8677c478bd9Sstevel@tonic-gate 			ptr = &confpwr_descr.bBusPowerSavingD3;
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 	} else {
8707c478bd9Sstevel@tonic-gate 		/* Parse the interface power descriptor */
8717c478bd9Sstevel@tonic-gate 		rval = usba_parse_if_pwr_descr(usb_cfg,
872ffcd51f3Slg 		    cfg_length,
873ffcd51f3Slg 		    usba_get_ifno(dip),	/* interface index */
874ffcd51f3Slg 		    0,			/* XXXX alt interface index */
875ffcd51f3Slg 		    &ifpwr_descr,
876ffcd51f3Slg 		    USBA_IF_PWR_DESCR_SIZE);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		if (rval != USBA_IF_PWR_DESCR_SIZE) {
8797c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8807c478bd9Sstevel@tonic-gate 			    "usb_create_pm_components: "
8817c478bd9Sstevel@tonic-gate 			    "usb_parse_if_pwr_descr "
8827c478bd9Sstevel@tonic-gate 			    "returns length of %d, "
8837c478bd9Sstevel@tonic-gate 			    "expecting %d", rval, USBA_CFG_PWR_DESCR_SIZE);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		if (cfg_attrib & USB_CFG_ATTR_SELFPWR) {
8897c478bd9Sstevel@tonic-gate 			ptr =  &ifpwr_descr.bSelfPowerSavingD3;
8907c478bd9Sstevel@tonic-gate 		} else {
8917c478bd9Sstevel@tonic-gate 			ptr =  &ifpwr_descr.bBusPowerSavingD3;
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	/* walk thru levels and create prop level=name strings */
8967c478bd9Sstevel@tonic-gate 	for (lvl = USB_DEV_OS_PWR_0; lvl <= USB_DEV_OS_PWR_3; lvl++) {
8977c478bd9Sstevel@tonic-gate 		if (*ptr || (lvl == USB_DEV_OS_PWR_3)) {
8987c478bd9Sstevel@tonic-gate 			(void) snprintf(str, USBA_POWER_STR_SIZE,
8997c478bd9Sstevel@tonic-gate 			    "%d=USB D%d State",
9007c478bd9Sstevel@tonic-gate 			    lvl, USB_DEV_OS_PWR2USB_PWR(lvl));
9017c478bd9Sstevel@tonic-gate 			pm_comp[n_prop] = kmem_zalloc(strlen(str) + 1,
902ffcd51f3Slg 			    KM_SLEEP);
9037c478bd9Sstevel@tonic-gate 			(void) strcpy(pm_comp[n_prop++], str);
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 			*pwr_states |= USB_DEV_PWRMASK(lvl);
9067c478bd9Sstevel@tonic-gate 		}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		ptr -= 2; /* skip to the next power state */
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBAI, usbai_log_handle,
9127c478bd9Sstevel@tonic-gate 	    "usb_create_pm_components: pwr_states: %x", *pwr_states);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/* now create the actual components */
9157c478bd9Sstevel@tonic-gate 	rval = ddi_prop_update_string_array(DDI_DEV_T_NONE, dip,
916ffcd51f3Slg 	    "pm-components", pm_comp, n_prop);
9177c478bd9Sstevel@tonic-gate 	if (rval == DDI_PROP_SUCCESS) {
9187c478bd9Sstevel@tonic-gate 		rval = USB_SUCCESS;
9197c478bd9Sstevel@tonic-gate 	} else {
9207c478bd9Sstevel@tonic-gate 		rval = USB_FAILURE;
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/* display & delete properties */
9247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBAI, usbai_log_handle,
9257c478bd9Sstevel@tonic-gate 	    "usb_create_pm_components: The properties are:");
9267c478bd9Sstevel@tonic-gate 	for (i = 0; i < n_prop; i++) {
9277c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBAI, usbai_log_handle,
9287c478bd9Sstevel@tonic-gate 		    "\t%s", pm_comp[i]);
9297c478bd9Sstevel@tonic-gate 		kmem_free(pm_comp[i], strlen(pm_comp[i]) + 1);
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	return (rval);
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate  * Generic Functions to set the power level of any usb device
9387c478bd9Sstevel@tonic-gate  *
9397c478bd9Sstevel@tonic-gate  * Since OS and USB power states are numerically reverse,
9407c478bd9Sstevel@tonic-gate  * Here is the mapping :-
9417c478bd9Sstevel@tonic-gate  *	OS State	USB State
9427c478bd9Sstevel@tonic-gate  *	0		D3	(minimal or no power)
9437c478bd9Sstevel@tonic-gate  *	1		D2
9447c478bd9Sstevel@tonic-gate  *	2		D1
9457c478bd9Sstevel@tonic-gate  *	3		D0	(Full power)
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate /* set device power level to 0 (full power) */
9497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9507c478bd9Sstevel@tonic-gate int
usb_set_device_pwrlvl0(dev_info_t * dip)9517c478bd9Sstevel@tonic-gate usb_set_device_pwrlvl0(dev_info_t *dip)
9527c478bd9Sstevel@tonic-gate {
9537c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
9547c478bd9Sstevel@tonic-gate 	    "usb_set_device_pwrlvl0 : Not Yet Implemented");
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /* set device power level to 1	*/
9617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9627c478bd9Sstevel@tonic-gate int
usb_set_device_pwrlvl1(dev_info_t * dip)9637c478bd9Sstevel@tonic-gate usb_set_device_pwrlvl1(dev_info_t *dip)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
9667c478bd9Sstevel@tonic-gate 	    "usb_set_device_pwrlvl1 : Not Yet Implemented");
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
9697c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate /* set device power level to 2	*/
9737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9747c478bd9Sstevel@tonic-gate int
usb_set_device_pwrlvl2(dev_info_t * dip)9757c478bd9Sstevel@tonic-gate usb_set_device_pwrlvl2(dev_info_t *dip)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
9787c478bd9Sstevel@tonic-gate 	    "usb_set_device_pwrlvl2 : Not Yet Implemented");
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate /* set device power level to 3	*/
9857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9867c478bd9Sstevel@tonic-gate int
usb_set_device_pwrlvl3(dev_info_t * dip)9877c478bd9Sstevel@tonic-gate usb_set_device_pwrlvl3(dev_info_t *dip)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
9907c478bd9Sstevel@tonic-gate 	    "usb_set_device_pwrlvl3 : Not Yet Implemented");
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate /*
9977c478bd9Sstevel@tonic-gate  * USB event management
9987c478bd9Sstevel@tonic-gate  */
9997c478bd9Sstevel@tonic-gate typedef void (*peh_t)(dev_info_t *, ddi_eventcookie_t, void *, void *);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate /*
10037c478bd9Sstevel@tonic-gate  * usb_register_hotplug_cbs:
10047c478bd9Sstevel@tonic-gate  *	Register to get callbacks for hotplug events
10057c478bd9Sstevel@tonic-gate  */
10067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10077c478bd9Sstevel@tonic-gate int
usb_register_hotplug_cbs(dev_info_t * dip,int (* disconnect_event_handler)(dev_info_t *),int (* reconnect_event_handler)(dev_info_t *))10087c478bd9Sstevel@tonic-gate usb_register_hotplug_cbs(dev_info_t *dip,
10097c478bd9Sstevel@tonic-gate     int (*disconnect_event_handler)(dev_info_t *),
10107c478bd9Sstevel@tonic-gate     int (*reconnect_event_handler)(dev_info_t *))
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
10137c478bd9Sstevel@tonic-gate 	usba_evdata_t		*evdata;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (disconnect_event_handler == NULL) ||
10167c478bd9Sstevel@tonic-gate 	    (reconnect_event_handler == NULL)) {
10177c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
10187c478bd9Sstevel@tonic-gate 		    "usb_register_hotplug_cbs: Bad argument(s)");
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
10247c478bd9Sstevel@tonic-gate 	    "usb_register_hotplug_cbs: entry");
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/*
10277c478bd9Sstevel@tonic-gate 	 * The event list searches by ddi_get_eventcookie calls below, go
10287c478bd9Sstevel@tonic-gate 	 * through hubd and so do not apply to host controllers.
10297c478bd9Sstevel@tonic-gate 	 */
10307c478bd9Sstevel@tonic-gate 	ASSERT(!usba_is_root_hub(dip));
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
10337c478bd9Sstevel@tonic-gate 	evdata = usba_get_evdata(dip);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	if (usba_device->rm_cookie == NULL) {
10367c478bd9Sstevel@tonic-gate 		if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
10377c478bd9Sstevel@tonic-gate 		    &usba_device->rm_cookie) != DDI_SUCCESS) {
10387c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
10397c478bd9Sstevel@tonic-gate 			    "usb_register_hotplug_cbs: get rm cookie failed");
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 			goto fail;
10427c478bd9Sstevel@tonic-gate 		}
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 	if (ddi_add_event_handler(dip, usba_device->rm_cookie,
10453fcf12aaSToomas Soome 	    (peh_t)(uintptr_t)disconnect_event_handler,
10467c478bd9Sstevel@tonic-gate 	    NULL, &evdata->ev_rm_cb_id) != DDI_SUCCESS) {
10477c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
10487c478bd9Sstevel@tonic-gate 		    "usb_register_hotplug_cbs: add disconnect handler failed");
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		goto fail;
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if (usba_device->ins_cookie == NULL) {
10547c478bd9Sstevel@tonic-gate 		if (ddi_get_eventcookie(dip, DDI_DEVI_INSERT_EVENT,
10557c478bd9Sstevel@tonic-gate 		    &usba_device->ins_cookie) != DDI_SUCCESS) {
10567c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
10577c478bd9Sstevel@tonic-gate 			    "usb_register_hotplug_cbs: get ins cookie failed");
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 			goto fail;
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 	if (ddi_add_event_handler(dip, usba_device->ins_cookie,
10633fcf12aaSToomas Soome 	    (peh_t)(uintptr_t)reconnect_event_handler,
10647c478bd9Sstevel@tonic-gate 	    NULL, &evdata->ev_ins_cb_id) != DDI_SUCCESS) {
10657c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
10667c478bd9Sstevel@tonic-gate 		    "usb_register_hotplug_cbs: add reconnect handler failed");
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 		goto fail;
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
10727c478bd9Sstevel@tonic-gate 	usba_device->usb_client_flags[usba_get_ifno(dip)] |=
1073ffcd51f3Slg 	    USBA_CLIENT_FLAG_EV_CBS;
10747c478bd9Sstevel@tonic-gate 	usba_device->usb_client_ev_cb_list->dip = dip;
10757c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate fail:
10807c478bd9Sstevel@tonic-gate 	usb_unregister_hotplug_cbs(dip);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate /*
10887c478bd9Sstevel@tonic-gate  * usb_unregister_hotplug_cbs:
10897c478bd9Sstevel@tonic-gate  *	Unregister hotplug callbacks
10907c478bd9Sstevel@tonic-gate  */
10917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10927c478bd9Sstevel@tonic-gate void
usb_unregister_hotplug_cbs(dev_info_t * dip)10937c478bd9Sstevel@tonic-gate usb_unregister_hotplug_cbs(dev_info_t *dip)
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate 	usb_unregister_event_cbs(dip, NULL);
10967c478bd9Sstevel@tonic-gate }
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * usb_register_event_cbs:
11017c478bd9Sstevel@tonic-gate  *	Register to get callbacks for USB events
11027c478bd9Sstevel@tonic-gate  */
11037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11047c478bd9Sstevel@tonic-gate int
usb_register_event_cbs(dev_info_t * dip,usb_event_t * usb_evdata,usb_flags_t flags)11057c478bd9Sstevel@tonic-gate usb_register_event_cbs(dev_info_t *dip, usb_event_t *usb_evdata,
11060d2006e4SRobert Mustacchi     usb_flags_t flags)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
11097c478bd9Sstevel@tonic-gate 	usba_evdata_t		*evdata;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (usb_evdata == NULL)) {
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	/*
11177c478bd9Sstevel@tonic-gate 	 * The event list searches by ddi_get_eventcookie calls below, go
11187c478bd9Sstevel@tonic-gate 	 * through hubd and so do not apply to host controllers.
11197c478bd9Sstevel@tonic-gate 	 */
11207c478bd9Sstevel@tonic-gate 	ASSERT(!usba_is_root_hub(dip));
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
11237c478bd9Sstevel@tonic-gate 	evdata = usba_get_evdata(dip);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	if (usb_evdata->disconnect_event_handler != NULL) {
11267c478bd9Sstevel@tonic-gate 		if (usba_device->rm_cookie == NULL) {
11277c478bd9Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
11287c478bd9Sstevel@tonic-gate 			    &usba_device->rm_cookie) != DDI_SUCCESS) {
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 				goto fail;
11317c478bd9Sstevel@tonic-gate 			}
11327c478bd9Sstevel@tonic-gate 		}
11337c478bd9Sstevel@tonic-gate 		if (ddi_add_event_handler(dip, usba_device->rm_cookie,
11343fcf12aaSToomas Soome 		    (peh_t)(uintptr_t)usb_evdata->disconnect_event_handler,
11357c478bd9Sstevel@tonic-gate 		    NULL, &evdata->ev_rm_cb_id) != DDI_SUCCESS) {
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 			goto fail;
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 	if (usb_evdata->reconnect_event_handler != NULL) {
11417c478bd9Sstevel@tonic-gate 		if (usba_device->ins_cookie == NULL) {
11427c478bd9Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, DDI_DEVI_INSERT_EVENT,
11437c478bd9Sstevel@tonic-gate 			    &usba_device->ins_cookie) != DDI_SUCCESS) {
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 				goto fail;
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 		}
11487c478bd9Sstevel@tonic-gate 		if (ddi_add_event_handler(dip, usba_device->ins_cookie,
11493fcf12aaSToomas Soome 		    (peh_t)(uintptr_t)usb_evdata->reconnect_event_handler,
11507c478bd9Sstevel@tonic-gate 		    NULL, &evdata->ev_ins_cb_id) != DDI_SUCCESS) {
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 			goto fail;
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate 	if (usb_evdata->post_resume_event_handler != NULL) {
11567c478bd9Sstevel@tonic-gate 		if (usba_device->resume_cookie == NULL) {
11577c478bd9Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, USBA_POST_RESUME_EVENT,
11587c478bd9Sstevel@tonic-gate 			    &usba_device->resume_cookie) != DDI_SUCCESS) {
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 				goto fail;
11617c478bd9Sstevel@tonic-gate 			}
11627c478bd9Sstevel@tonic-gate 		}
11637c478bd9Sstevel@tonic-gate 		if (ddi_add_event_handler(dip, usba_device->resume_cookie,
11643fcf12aaSToomas Soome 		    (peh_t)(uintptr_t)usb_evdata->post_resume_event_handler,
11657c478bd9Sstevel@tonic-gate 		    NULL, &evdata->ev_resume_cb_id) != DDI_SUCCESS) {
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 			goto fail;
11687c478bd9Sstevel@tonic-gate 		}
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 	if (usb_evdata->pre_suspend_event_handler != NULL) {
11717c478bd9Sstevel@tonic-gate 		if (usba_device->suspend_cookie == NULL) {
11727c478bd9Sstevel@tonic-gate 			if (ddi_get_eventcookie(dip, USBA_PRE_SUSPEND_EVENT,
11737c478bd9Sstevel@tonic-gate 			    &usba_device->suspend_cookie) != DDI_SUCCESS) {
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 				goto fail;
11767c478bd9Sstevel@tonic-gate 			}
11777c478bd9Sstevel@tonic-gate 		}
11787c478bd9Sstevel@tonic-gate 		if (ddi_add_event_handler(dip, usba_device->suspend_cookie,
11793fcf12aaSToomas Soome 		    (peh_t)(uintptr_t)usb_evdata->pre_suspend_event_handler,
11807c478bd9Sstevel@tonic-gate 		    NULL, &evdata->ev_suspend_cb_id) != DDI_SUCCESS) {
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 			goto fail;
11837c478bd9Sstevel@tonic-gate 		}
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
11877c478bd9Sstevel@tonic-gate 	usba_device->usb_client_flags[usba_get_ifno(dip)] |=
1188ffcd51f3Slg 	    USBA_CLIENT_FLAG_EV_CBS;
11897c478bd9Sstevel@tonic-gate 	usba_device->usb_client_ev_cb_list->dip = dip;
11907c478bd9Sstevel@tonic-gate 	usba_device->usb_client_ev_cb_list->ev_data = usb_evdata;
11917c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate fail:
11967c478bd9Sstevel@tonic-gate 	usb_unregister_event_cbs(dip, usb_evdata);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	return (USB_FAILURE);
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate /*
12047c478bd9Sstevel@tonic-gate  * usb_unregister_event_cbs:
12057c478bd9Sstevel@tonic-gate  *	Unregister all event callbacks
12067c478bd9Sstevel@tonic-gate  */
12077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12087c478bd9Sstevel@tonic-gate void
usb_unregister_event_cbs(dev_info_t * dip,usb_event_t * usb_evdata)12097c478bd9Sstevel@tonic-gate usb_unregister_event_cbs(dev_info_t *dip, usb_event_t *usb_evdata)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	usba_evdata_t		*evdata;
12127c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device = usba_get_usba_device(dip);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	evdata = usba_get_evdata(dip);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	if (evdata->ev_rm_cb_id != NULL) {
12177c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(evdata->ev_rm_cb_id);
12187c478bd9Sstevel@tonic-gate 		evdata->ev_rm_cb_id = NULL;
12197c478bd9Sstevel@tonic-gate 	}
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	if (evdata->ev_ins_cb_id != NULL) {
12227c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(evdata->ev_ins_cb_id);
12237c478bd9Sstevel@tonic-gate 		evdata->ev_ins_cb_id = NULL;
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	if (evdata->ev_suspend_cb_id != NULL) {
12277c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(evdata->ev_suspend_cb_id);
12287c478bd9Sstevel@tonic-gate 		evdata->ev_suspend_cb_id = NULL;
12297c478bd9Sstevel@tonic-gate 	}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	if (evdata->ev_resume_cb_id != NULL) {
12327c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(evdata->ev_resume_cb_id);
12337c478bd9Sstevel@tonic-gate 		evdata->ev_resume_cb_id = NULL;
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
12377c478bd9Sstevel@tonic-gate 	usba_device->usb_client_flags[usba_get_ifno(dip)] &=
1238ffcd51f3Slg 	    ~USBA_CLIENT_FLAG_EV_CBS;
12397c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
12407c478bd9Sstevel@tonic-gate }
1241ffcd51f3Slg 
1242ffcd51f3Slg int
usb_reset_device(dev_info_t * dip,usb_dev_reset_lvl_t reset_level)1243ffcd51f3Slg usb_reset_device(dev_info_t *dip, usb_dev_reset_lvl_t reset_level)
1244ffcd51f3Slg {
1245ffcd51f3Slg 	return (usba_hubdi_reset_device(dip, reset_level));
1246ffcd51f3Slg }
1247a7df97baSStrony Zhang - Solaris China Team 
1248a7df97baSStrony Zhang - Solaris China Team /*
1249a7df97baSStrony Zhang - Solaris China Team  * usb device driver registration
1250a7df97baSStrony Zhang - Solaris China Team  */
1251a7df97baSStrony Zhang - Solaris China Team int
usb_register_dev_driver(dev_info_t * dip,usb_dev_driver_callback_t cb)1252a7df97baSStrony Zhang - Solaris China Team usb_register_dev_driver(dev_info_t *dip, usb_dev_driver_callback_t cb)
1253a7df97baSStrony Zhang - Solaris China Team {
1254a7df97baSStrony Zhang - Solaris China Team 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1255a7df97baSStrony Zhang - Solaris China Team 	    "usb_register_dev_driver: register the specified driver "
1256a7df97baSStrony Zhang - Solaris China Team 	    "in usba: dip = 0x%p", (void *)dip);
1257a7df97baSStrony Zhang - Solaris China Team 
1258a7df97baSStrony Zhang - Solaris China Team 	if (cb != NULL) {
1259a7df97baSStrony Zhang - Solaris China Team 		usb_cap.dip = dip;
1260a7df97baSStrony Zhang - Solaris China Team 		usb_cap.usba_dev_driver_cb = cb;
1261a7df97baSStrony Zhang - Solaris China Team 
1262a7df97baSStrony Zhang - Solaris China Team 		return (USB_SUCCESS);
1263a7df97baSStrony Zhang - Solaris China Team 	}
1264a7df97baSStrony Zhang - Solaris China Team 
1265a7df97baSStrony Zhang - Solaris China Team 	return (USB_FAILURE);
1266a7df97baSStrony Zhang - Solaris China Team }
1267a7df97baSStrony Zhang - Solaris China Team 
1268a7df97baSStrony Zhang - Solaris China Team /*
1269a7df97baSStrony Zhang - Solaris China Team  * usb device driver unregistration
1270a7df97baSStrony Zhang - Solaris China Team  */
1271a7df97baSStrony Zhang - Solaris China Team void
usb_unregister_dev_driver(dev_info_t * dip)1272a7df97baSStrony Zhang - Solaris China Team usb_unregister_dev_driver(dev_info_t *dip)
1273a7df97baSStrony Zhang - Solaris China Team {
1274a7df97baSStrony Zhang - Solaris China Team 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1275a7df97baSStrony Zhang - Solaris China Team 	    "usb_unregister_dev_driver: unregister the registered "
1276a7df97baSStrony Zhang - Solaris China Team 	    "driver: dip =0x%p", (void *)dip);
1277a7df97baSStrony Zhang - Solaris China Team 
1278a7df97baSStrony Zhang - Solaris China Team 	ASSERT(dip == usb_cap.dip);
1279a7df97baSStrony Zhang - Solaris China Team 	usb_cap.dip = NULL;
1280a7df97baSStrony Zhang - Solaris China Team 	usb_cap.usba_dev_driver_cb = NULL;
1281a7df97baSStrony Zhang - Solaris China Team }
1282