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
5112116d8Sfb  * Common Development and Distribution License (the "License").
6112116d8Sfb  * 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
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ff0e937bSRaymond Chen  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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 all USBAI pipe management
357c478bd9Sstevel@tonic-gate  *	usb_pipe_open()
367c478bd9Sstevel@tonic-gate  *	usb_pipe_close()
377c478bd9Sstevel@tonic-gate  *	usb_pipe_set_private()
387c478bd9Sstevel@tonic-gate  *	usb_pipe_get_private()
397c478bd9Sstevel@tonic-gate  *	usb_pipe_abort()
407c478bd9Sstevel@tonic-gate  *	usb_pipe_reset()
417c478bd9Sstevel@tonic-gate  *	usb_pipe_drain_reqs()
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate #define	USBA_FRAMEWORK
447c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
457c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
467c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate extern	pri_t	maxclsyspri;
497c478bd9Sstevel@tonic-gate extern	pri_t	minclsyspri;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* function prototypes */
527c478bd9Sstevel@tonic-gate static	void	usba_pipe_do_async_func_thread(void *arg);
537c478bd9Sstevel@tonic-gate static	int	usba_pipe_sync_close(dev_info_t *, usba_ph_impl_t *,
547c478bd9Sstevel@tonic-gate 			usba_pipe_async_req_t *, usb_flags_t);
557c478bd9Sstevel@tonic-gate static	int	usba_pipe_sync_reset(dev_info_t *, usba_ph_impl_t *,
567c478bd9Sstevel@tonic-gate 			usba_pipe_async_req_t *, usb_flags_t);
577c478bd9Sstevel@tonic-gate static	int	usba_pipe_sync_drain_reqs(dev_info_t *, usba_ph_impl_t *,
587c478bd9Sstevel@tonic-gate 			usba_pipe_async_req_t *, usb_flags_t);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* local tunables */
614610e4a0Sfrits int	usba_drain_timeout = 1000;	/* in ms */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* return the default pipe for this device */
647c478bd9Sstevel@tonic-gate usb_pipe_handle_t
657c478bd9Sstevel@tonic-gate usba_get_dflt_pipe_handle(dev_info_t *dip)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
687c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle = NULL;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (dip) {
717c478bd9Sstevel@tonic-gate 		usba_device = usba_get_usba_device(dip);
727c478bd9Sstevel@tonic-gate 		if (usba_device) {
737c478bd9Sstevel@tonic-gate 			pipe_handle =
747c478bd9Sstevel@tonic-gate 			    (usb_pipe_handle_t)&usba_device->usb_ph_list[0];
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	return (pipe_handle);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* return dip owner of pipe_handle */
837c478bd9Sstevel@tonic-gate dev_info_t *
847c478bd9Sstevel@tonic-gate usba_get_dip(usb_pipe_handle_t pipe_handle)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = (usba_ph_impl_t *)pipe_handle;
877c478bd9Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (ph_impl) {
907c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
917c478bd9Sstevel@tonic-gate 		dip = ph_impl->usba_ph_dip;
927c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	return (dip);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate usb_pipe_handle_t
1007c478bd9Sstevel@tonic-gate usba_usbdev_to_dflt_pipe_handle(usba_device_t *usba_device)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle = NULL;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if ((usba_device) &&
1057c478bd9Sstevel@tonic-gate 	    (usba_device->usb_ph_list[0].usba_ph_data != NULL)) {
1067c478bd9Sstevel@tonic-gate 		pipe_handle = (usb_pipe_handle_t)&usba_device->usb_ph_list[0];
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	return (pipe_handle);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate usba_pipe_handle_data_t *
1147c478bd9Sstevel@tonic-gate usba_get_ph_data(usb_pipe_handle_t pipe_handle)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = (usba_ph_impl_t *)pipe_handle;
1177c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = NULL;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (ph_impl) {
1207c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
1217c478bd9Sstevel@tonic-gate 		ASSERT(ph_impl->usba_ph_ref_count >= 0);
1227c478bd9Sstevel@tonic-gate 		ph_data = ph_impl->usba_ph_data;
1237c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	return (ph_data);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate usb_pipe_handle_t
1317c478bd9Sstevel@tonic-gate usba_get_pipe_handle(usba_pipe_handle_data_t *ph_data)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t ph = NULL;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (ph_data) {
1367c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
1377c478bd9Sstevel@tonic-gate 		ASSERT(ph_data->p_req_count >= 0);
1387c478bd9Sstevel@tonic-gate 		ph = (usb_pipe_handle_t)ph_data->p_ph_impl;
1397c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	return (ph);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * opaque to pipe handle impl translation with incr of ref count. The caller
1487c478bd9Sstevel@tonic-gate  * must release ph_data when done. Increment the ref count ensures that
1497c478bd9Sstevel@tonic-gate  * the ph_data will not be freed underneath us.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate usba_pipe_handle_data_t *
1527c478bd9Sstevel@tonic-gate usba_hold_ph_data(usb_pipe_handle_t pipe_handle)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = (usba_ph_impl_t *)pipe_handle;
1557c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = NULL;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (ph_impl) {
1587c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		switch (ph_impl->usba_ph_state) {
1617c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_IDLE:
1627c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_ACTIVE:
1637c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_ERROR:
1647c478bd9Sstevel@tonic-gate 			ph_data = ph_impl->usba_ph_data;
1657c478bd9Sstevel@tonic-gate 			ph_impl->usba_ph_ref_count++;
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_CLOSED:
1687c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_CLOSING:
1697c478bd9Sstevel@tonic-gate 		default:
1707c478bd9Sstevel@tonic-gate 			break;
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1747c478bd9Sstevel@tonic-gate 		    "usba_hold_ph_data: ph_impl=0x%p state=%d ref=%d",
175112116d8Sfb 		    (void *)ph_impl, ph_impl->usba_ph_state,
1767c478bd9Sstevel@tonic-gate 		    ph_impl->usba_ph_ref_count);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	return (ph_data);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate void
1867c478bd9Sstevel@tonic-gate usba_release_ph_data(usba_ph_impl_t *ph_impl)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	if (ph_impl) {
1897c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1927c478bd9Sstevel@tonic-gate 		    "usba_release_ph_data: "
1937c478bd9Sstevel@tonic-gate 		    "ph_impl=0x%p state=%d ref=%d",
194112116d8Sfb 		    (void *)ph_impl, ph_impl->usba_ph_state,
1957c478bd9Sstevel@tonic-gate 		    ph_impl->usba_ph_ref_count);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate #ifndef __lock_lint
1987c478bd9Sstevel@tonic-gate 		if (ph_impl->usba_ph_data) {
1997c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
2007c478bd9Sstevel@tonic-gate 			    "usba_release_ph_data: req_count=%d",
2017c478bd9Sstevel@tonic-gate 			    ph_impl->usba_ph_data->p_req_count);
2027c478bd9Sstevel@tonic-gate 			ASSERT(ph_impl->usba_ph_data->p_req_count >= 0);
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate #endif
2057c478bd9Sstevel@tonic-gate 		ph_impl->usba_ph_ref_count--;
2067c478bd9Sstevel@tonic-gate 		ASSERT(ph_impl->usba_ph_ref_count >= 0);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * get pipe state from ph_data
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate usb_pipe_state_t
2177c478bd9Sstevel@tonic-gate usba_get_ph_state(usba_pipe_handle_data_t *ph_data)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
2207c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	pipe_state;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ph_data->p_mutex));
2237c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
2247c478bd9Sstevel@tonic-gate 	pipe_state = ph_impl->usba_ph_state;
2257c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	return (pipe_state);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * get ref_count from ph_data
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate int
2357c478bd9Sstevel@tonic-gate usba_get_ph_ref_count(usba_pipe_handle_data_t *ph_data)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
2387c478bd9Sstevel@tonic-gate 	int			ref_count;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
2417c478bd9Sstevel@tonic-gate 	ref_count = ph_impl->usba_ph_ref_count;
2427c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	return (ref_count);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * new pipe state
2507c478bd9Sstevel@tonic-gate  * We need to hold both pipe mutex and ph_impl mutex
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate void
2537c478bd9Sstevel@tonic-gate usba_pipe_new_state(usba_pipe_handle_data_t *ph_data, usb_pipe_state_t state)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	usba_ph_impl_t *ph_impl = ph_data->p_ph_impl;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ph_data->p_mutex));
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
2607c478bd9Sstevel@tonic-gate 	ASSERT(ph_data->p_req_count >= 0);
2617c478bd9Sstevel@tonic-gate 	ASSERT(ph_impl->usba_ph_ref_count >= 0);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
2647c478bd9Sstevel@tonic-gate 	    "usba_pipe_new_state: "
2657c478bd9Sstevel@tonic-gate 	    "ph_data=0x%p old=%s new=%s ref=%d req=%d",
266112116d8Sfb 	    (void *)ph_data, usb_str_pipe_state(ph_impl->usba_ph_state),
2677c478bd9Sstevel@tonic-gate 	    usb_str_pipe_state(state),
2687c478bd9Sstevel@tonic-gate 	    ph_impl->usba_ph_ref_count, ph_data->p_req_count);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	switch (ph_impl->usba_ph_state) {
2717c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_IDLE:
2727c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_ACTIVE:
2737c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_ERROR:
2747c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_CLOSED:
2757c478bd9Sstevel@tonic-gate 		ph_impl->usba_ph_state = state;
2767c478bd9Sstevel@tonic-gate 		break;
2777c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_CLOSING:
2787c478bd9Sstevel@tonic-gate 	default:
2797c478bd9Sstevel@tonic-gate 		break;
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * async function execution support
2877c478bd9Sstevel@tonic-gate  * Arguments:
2887c478bd9Sstevel@tonic-gate  *	dip		- devinfo pointer
2897c478bd9Sstevel@tonic-gate  *	sync_func	- function to be executed
2907c478bd9Sstevel@tonic-gate  *	ph_impl		- impl pipehandle
2917c478bd9Sstevel@tonic-gate  *	arg		- opaque arg
2927c478bd9Sstevel@tonic-gate  *	usb_flags	- none
2937c478bd9Sstevel@tonic-gate  *	callback	- function to be called on completion, may be NULL
2947c478bd9Sstevel@tonic-gate  *	callback_arg	- argument for callback function
2957c478bd9Sstevel@tonic-gate  *
2967c478bd9Sstevel@tonic-gate  * Note: The caller must do a hold on ph_data
2977c478bd9Sstevel@tonic-gate  *	We sleep for memory resources and taskq_dispatch which will ensure
2987c478bd9Sstevel@tonic-gate  *	that this function succeeds
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate int
3017c478bd9Sstevel@tonic-gate usba_pipe_setup_func_call(
3027c478bd9Sstevel@tonic-gate 	dev_info_t	*dip,
3037c478bd9Sstevel@tonic-gate 	int		(*sync_func)(dev_info_t *,
3047c478bd9Sstevel@tonic-gate 			    usba_ph_impl_t *, usba_pipe_async_req_t *,
3057c478bd9Sstevel@tonic-gate 			    usb_flags_t),
3067c478bd9Sstevel@tonic-gate 	usba_ph_impl_t *ph_impl,
3077c478bd9Sstevel@tonic-gate 	usb_opaque_t	arg,
3087c478bd9Sstevel@tonic-gate 	usb_flags_t	usb_flags,
3097c478bd9Sstevel@tonic-gate 	void		(*callback)(usb_pipe_handle_t,
3107c478bd9Sstevel@tonic-gate 			    usb_opaque_t, int, usb_cb_flags_t),
3117c478bd9Sstevel@tonic-gate 	usb_opaque_t	callback_arg)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	usba_pipe_async_req_t	*request;
3147c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle = (usb_pipe_handle_t)ph_impl;
3157c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = ph_impl->usba_ph_data;
3167c478bd9Sstevel@tonic-gate 	int			rval = USB_SUCCESS;
3177c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		callback_flags;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBAI, usbai_log_handle,
3207c478bd9Sstevel@tonic-gate 	    "usba_pipe_setup_func_call: ph_impl=0x%p, func=0x%p",
321112116d8Sfb 	    (void *)ph_impl, (void *)sync_func);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (((usb_flags & USB_FLAGS_SLEEP) == 0) && (callback == NULL)) {
3247c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
325d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
3267c478bd9Sstevel@tonic-gate 		    "usba_pipe_setup_func_call: async request with "
3277c478bd9Sstevel@tonic-gate 		    "no callback");
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		return (USB_INVALID_ARGS);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	request = kmem_zalloc(sizeof (usba_pipe_async_req_t), KM_SLEEP);
3337c478bd9Sstevel@tonic-gate 	request->dip		= dip;
3347c478bd9Sstevel@tonic-gate 	request->ph_impl	= ph_impl;
3357c478bd9Sstevel@tonic-gate 	request->arg		= arg;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * OR in sleep flag. regardless of calling sync_func directly
3397c478bd9Sstevel@tonic-gate 	 * or in a new thread, we will always wait for completion
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	request->usb_flags	= usb_flags | USB_FLAGS_SLEEP;
3427c478bd9Sstevel@tonic-gate 	request->sync_func	= sync_func;
3437c478bd9Sstevel@tonic-gate 	request->callback	= callback;
3447c478bd9Sstevel@tonic-gate 	request->callback_arg	= callback_arg;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (usb_flags & USB_FLAGS_SLEEP) {
3477c478bd9Sstevel@tonic-gate 		rval = sync_func(dip, ph_impl, request, usb_flags);
3487c478bd9Sstevel@tonic-gate 		kmem_free(request, sizeof (usba_pipe_async_req_t));
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	} else if (usba_async_ph_req(ph_data,
3517c478bd9Sstevel@tonic-gate 	    usba_pipe_do_async_func_thread,
3527c478bd9Sstevel@tonic-gate 	    (void *)request, USB_FLAGS_SLEEP) != USB_SUCCESS) {
3537c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
3547c478bd9Sstevel@tonic-gate 		    "usb_async_req failed: ph_impl=0x%p, func=0x%p",
355112116d8Sfb 		    (void *)ph_impl, (void *)sync_func);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		if (callback) {
3587c478bd9Sstevel@tonic-gate 			callback_flags =
3597c478bd9Sstevel@tonic-gate 			    usba_check_intr_context(USB_CB_ASYNC_REQ_FAILED);
3607c478bd9Sstevel@tonic-gate 			callback(pipe_handle, callback_arg, USB_FAILURE,
3617c478bd9Sstevel@tonic-gate 			    callback_flags);
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		kmem_free(request, sizeof (usba_pipe_async_req_t));
3657c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	return (rval);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * taskq thread function to execute function synchronously
3747c478bd9Sstevel@tonic-gate  * Note: caller must have done a hold on ph_data
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate static void
3777c478bd9Sstevel@tonic-gate usba_pipe_do_async_func_thread(void *arg)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	usba_pipe_async_req_t	*request = (usba_pipe_async_req_t *)arg;
3807c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = request->ph_impl;
3817c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle = (usb_pipe_handle_t)ph_impl;
3827c478bd9Sstevel@tonic-gate 	int			rval;
3837c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags = USB_CB_NO_INFO;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if ((rval = request->sync_func(request->dip, ph_impl,
3867c478bd9Sstevel@tonic-gate 	    request, request->usb_flags | USB_FLAGS_SLEEP)) !=
3877c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
3887c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
3897c478bd9Sstevel@tonic-gate 		    "sync func failed (%d)", rval);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (request->callback) {
3937c478bd9Sstevel@tonic-gate 		request->callback(pipe_handle, request->callback_arg, rval,
3947c478bd9Sstevel@tonic-gate 		    cb_flags);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	kmem_free(request, sizeof (usba_pipe_async_req_t));
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate  * default endpoint descriptor and pipe policy
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate usb_ep_descr_t	usba_default_ep_descr =
4057c478bd9Sstevel@tonic-gate 	{7, 5, 0, USB_EP_ATTR_CONTROL, 8, 0};
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /* set some meaningful defaults */
4087c478bd9Sstevel@tonic-gate static usb_pipe_policy_t usba_default_ep_pipe_policy = {3};
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * usb_get_ep_index: create an index from endpoint address that can
4137c478bd9Sstevel@tonic-gate  * be used to index into endpoint pipe lists
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate uchar_t
4167c478bd9Sstevel@tonic-gate usb_get_ep_index(uint8_t ep_addr)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	return ((ep_addr & USB_EP_NUM_MASK) +
4197c478bd9Sstevel@tonic-gate 	    ((ep_addr & USB_EP_DIR_MASK) ? 16 : 0));
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * pipe management
4257c478bd9Sstevel@tonic-gate  *	utility functions to init and destroy a pipehandle
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate static int
4287c478bd9Sstevel@tonic-gate usba_init_pipe_handle(dev_info_t *dip,
4297c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device,
4307c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*ep,
4317c478bd9Sstevel@tonic-gate 	usb_pipe_policy_t	*pipe_policy,
4327c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4357c478bd9Sstevel@tonic-gate 	unsigned int def_instance = instance;
4367c478bd9Sstevel@tonic-gate 	static unsigned int anon_instance = 0;
4377c478bd9Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = ph_impl->usba_ph_data;
4407c478bd9Sstevel@tonic-gate 	ddi_iblock_cookie_t	iblock_cookie =
4417c478bd9Sstevel@tonic-gate 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)->
4427c478bd9Sstevel@tonic-gate 	    hcdi_iblock_cookie;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
4457c478bd9Sstevel@tonic-gate 	    "usba_init_pipe_handle: "
446112116d8Sfb 	    "usba_device=0x%p ep=0x%x", (void *)usba_device,
447112116d8Sfb 	    ep->bEndpointAddress);
4487c478bd9Sstevel@tonic-gate 	mutex_init(&ph_data->p_mutex, NULL, MUTEX_DRIVER, iblock_cookie);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* just to keep warlock happy, there is no contention yet */
4517c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
4527c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	ASSERT(pipe_policy->pp_max_async_reqs);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (instance != -1) {
4577c478bd9Sstevel@tonic-gate 		(void) snprintf(tq_name, sizeof (tq_name),
4587c478bd9Sstevel@tonic-gate 		    "USB_%s_%x_pipehndl_tq_%d",
4597c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ep->bEndpointAddress, instance);
4607c478bd9Sstevel@tonic-gate 	} else {
4617c478bd9Sstevel@tonic-gate 		def_instance = atomic_add_32_nv(&anon_instance, 1);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		(void) snprintf(tq_name, sizeof (tq_name),
4647c478bd9Sstevel@tonic-gate 		    "USB_%s_%x_pipehndl_tq_%d_",
4657c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ep->bEndpointAddress, def_instance);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	ph_data->p_taskq = taskq_create(tq_name,
469112116d8Sfb 	    pipe_policy->pp_max_async_reqs + 1,
470112116d8Sfb 	    ((ep->bmAttributes & USB_EP_ATTR_MASK) ==
471112116d8Sfb 	    USB_EP_ATTR_ISOCH) ?
472112116d8Sfb 	    (maxclsyspri - 5) : minclsyspri,
473112116d8Sfb 	    2 * (pipe_policy->pp_max_async_reqs + 1),
474112116d8Sfb 	    8 * (pipe_policy->pp_max_async_reqs + 1),
475112116d8Sfb 	    TASKQ_PREPOPULATE);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * Create a shared taskq.
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 	if (ph_data->p_spec_flag & USBA_PH_FLAG_TQ_SHARE) {
4817c478bd9Sstevel@tonic-gate 		int iface = usb_get_if_number(dip);
4827c478bd9Sstevel@tonic-gate 		if (iface < 0) {
4837c478bd9Sstevel@tonic-gate 			/* we own the device, use first entry */
4847c478bd9Sstevel@tonic-gate 			iface = 0;
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		if (instance != -1) {
4887c478bd9Sstevel@tonic-gate 			(void) snprintf(tq_name, sizeof (tq_name),
4897c478bd9Sstevel@tonic-gate 			    "USB_%s_%x_shared_tq_%d",
4907c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ep->bEndpointAddress,
4917c478bd9Sstevel@tonic-gate 			    instance);
4927c478bd9Sstevel@tonic-gate 		} else {
4937c478bd9Sstevel@tonic-gate 			(void) snprintf(tq_name, sizeof (tq_name),
4947c478bd9Sstevel@tonic-gate 			    "USB_%s_%x_shared_tq_%d_",
4957c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ep->bEndpointAddress,
4967c478bd9Sstevel@tonic-gate 			    def_instance);
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		if (usba_device->usb_shared_taskq_ref_count[iface] == 0) {
5007c478bd9Sstevel@tonic-gate 			usba_device->usb_shared_taskq[iface] =
5017c478bd9Sstevel@tonic-gate 			    taskq_create(tq_name,
5027c478bd9Sstevel@tonic-gate 			    1,				/* Number threads. */
5037c478bd9Sstevel@tonic-gate 			    maxclsyspri - 5,		/* Priority */
5047c478bd9Sstevel@tonic-gate 			    1,				/* minalloc */
5057c478bd9Sstevel@tonic-gate 			    USBA_N_ENDPOINTS + 4,	/* maxalloc */
5067c478bd9Sstevel@tonic-gate 			    TASKQ_PREPOPULATE);
5077c478bd9Sstevel@tonic-gate 			ASSERT(usba_device->usb_shared_taskq[iface] != NULL);
5087c478bd9Sstevel@tonic-gate 		}
5097c478bd9Sstevel@tonic-gate 		usba_device->usb_shared_taskq_ref_count[iface]++;
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	ph_data->p_dip		= dip;
5137c478bd9Sstevel@tonic-gate 	ph_data->p_usba_device	= usba_device;
5147c478bd9Sstevel@tonic-gate 	ph_data->p_ep		= *ep;
5157c478bd9Sstevel@tonic-gate 	ph_data->p_ph_impl	= ph_impl;
5167c478bd9Sstevel@tonic-gate 	if ((ep->bmAttributes & USB_EP_ATTR_MASK) ==
5177c478bd9Sstevel@tonic-gate 	    USB_EP_ATTR_ISOCH) {
5187c478bd9Sstevel@tonic-gate 		ph_data->p_spec_flag |= USBA_PH_FLAG_USE_SOFT_INTR;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* fix up the MaxPacketSize if it is the default endpoint descr */
5227c478bd9Sstevel@tonic-gate 	if ((ep == &usba_default_ep_descr) && usba_device) {
523*ff0e937bSRaymond Chen 		uint16_t	maxpktsize;
524*ff0e937bSRaymond Chen 
525*ff0e937bSRaymond Chen 		maxpktsize = usba_device->usb_dev_descr->bMaxPacketSize0;
526*ff0e937bSRaymond Chen 		if (usba_device->usb_is_wireless) {
527*ff0e937bSRaymond Chen 			/*
528*ff0e937bSRaymond Chen 			 * according to wusb 1.0 spec 4.8.1, the host must
529*ff0e937bSRaymond Chen 			 * assume a wMaxPacketSize of 512 for the default
530*ff0e937bSRaymond Chen 			 * control pipe of a wusb device
531*ff0e937bSRaymond Chen 			 */
532*ff0e937bSRaymond Chen 			maxpktsize = 0x200;
533*ff0e937bSRaymond Chen 		}
5347c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBAI, usbai_log_handle,
5357c478bd9Sstevel@tonic-gate 		    "adjusting max packet size from %d to %d",
536*ff0e937bSRaymond Chen 		    ph_data->p_ep.wMaxPacketSize, maxpktsize);
5377c478bd9Sstevel@tonic-gate 
538*ff0e937bSRaymond Chen 		ph_data->p_ep.wMaxPacketSize = maxpktsize;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	/* now update usba_ph_impl structure */
5427c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
5437c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_dip = dip;
5447c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_ep = ph_data->p_ep;
5457c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_policy = ph_data->p_policy = *pipe_policy;
5467c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	usba_init_list(&ph_data->p_queue, (usb_opaque_t)ph_data, iblock_cookie);
5497c478bd9Sstevel@tonic-gate 	usba_init_list(&ph_data->p_cb_queue, (usb_opaque_t)ph_data,
550112116d8Sfb 	    iblock_cookie);
5517c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
5527c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate static void
5597c478bd9Sstevel@tonic-gate usba_taskq_destroy(void *arg)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	taskq_destroy((taskq_t *)arg);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate static void
5667c478bd9Sstevel@tonic-gate usba_destroy_pipe_handle(usba_pipe_handle_data_t *ph_data)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
5697c478bd9Sstevel@tonic-gate 	int			timeout;
5707c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
573112116d8Sfb 	    "usba_destroy_pipe_handle: ph_data=0x%p", (void *)ph_data);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
5767c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/* check for all activity to drain */
5797c478bd9Sstevel@tonic-gate 	for (timeout = 0; timeout < usba_drain_timeout; timeout++) {
5807c478bd9Sstevel@tonic-gate 		if ((ph_impl->usba_ph_ref_count <= 1) &&
5817c478bd9Sstevel@tonic-gate 		    (ph_data->p_req_count == 0)) {
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 			break;
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
5867c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
5877c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(1000));
5887c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
5897c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * set state to closed here so any other thread
5947c478bd9Sstevel@tonic-gate 	 * that is waiting for the CLOSED state will
5957c478bd9Sstevel@tonic-gate 	 * continue. Otherwise, taskq_destroy might deadlock
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_data = NULL;
5987c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_ref_count = 0;
5997c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_state = USB_PIPE_STATE_CLOSED;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (ph_data->p_taskq) {
6027c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
6037c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
6047c478bd9Sstevel@tonic-gate 		if (taskq_member(ph_data->p_taskq, curthread)) {
6057c478bd9Sstevel@tonic-gate 			/*
6067c478bd9Sstevel@tonic-gate 			 * use system taskq to destroy ph's taskq to avoid
6077c478bd9Sstevel@tonic-gate 			 * deadlock
6087c478bd9Sstevel@tonic-gate 			 */
6097c478bd9Sstevel@tonic-gate 			(void) taskq_dispatch(system_taskq,
6107c478bd9Sstevel@tonic-gate 			    usba_taskq_destroy, ph_data->p_taskq, TQ_SLEEP);
6117c478bd9Sstevel@tonic-gate 		} else {
6127c478bd9Sstevel@tonic-gate 			taskq_destroy(ph_data->p_taskq);
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 	} else {
6157c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
6167c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	usba_device = ph_data->p_usba_device;
6207c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
6217c478bd9Sstevel@tonic-gate 	if (ph_data->p_spec_flag & USBA_PH_FLAG_TQ_SHARE) {
6227c478bd9Sstevel@tonic-gate 		int iface = usb_get_if_number(ph_data->p_dip);
6237c478bd9Sstevel@tonic-gate 		if (iface < 0) {
6247c478bd9Sstevel@tonic-gate 			/* we own the device, use the first entry */
6257c478bd9Sstevel@tonic-gate 			iface = 0;
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
6287c478bd9Sstevel@tonic-gate 		if (--usba_device->usb_shared_taskq_ref_count[iface] == 0) {
6297c478bd9Sstevel@tonic-gate 			ph_data->p_spec_flag &= ~USBA_PH_FLAG_TQ_SHARE;
6307c478bd9Sstevel@tonic-gate 			if (taskq_member(usba_device->usb_shared_taskq[iface],
6317c478bd9Sstevel@tonic-gate 			    curthread)) {
6327c478bd9Sstevel@tonic-gate 				(void) taskq_dispatch(
6337c478bd9Sstevel@tonic-gate 				    system_taskq,
6347c478bd9Sstevel@tonic-gate 				    usba_taskq_destroy,
6357c478bd9Sstevel@tonic-gate 				    usba_device->usb_shared_taskq[iface],
6367c478bd9Sstevel@tonic-gate 				    TQ_SLEEP);
6377c478bd9Sstevel@tonic-gate 			} else {
6387c478bd9Sstevel@tonic-gate 				taskq_destroy(
6397c478bd9Sstevel@tonic-gate 				    usba_device->usb_shared_taskq[iface]);
6407c478bd9Sstevel@tonic-gate 			}
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
648112116d8Sfb 	    "usba_destroy_pipe_handle: destroying ph_data=0x%p",
649112116d8Sfb 	    (void *)ph_data);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	usba_destroy_list(&ph_data->p_queue);
6527c478bd9Sstevel@tonic-gate 	usba_destroy_list(&ph_data->p_cb_queue);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/* destroy mutexes */
6557c478bd9Sstevel@tonic-gate 	mutex_destroy(&ph_data->p_mutex);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	kmem_free(ph_data, sizeof (usba_pipe_handle_data_t));
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * usba_drain_cbs:
6637c478bd9Sstevel@tonic-gate  *	Drain the request callbacks on the pipe handle
6647c478bd9Sstevel@tonic-gate  */
6657c478bd9Sstevel@tonic-gate int
6667c478bd9Sstevel@tonic-gate usba_drain_cbs(usba_pipe_handle_data_t *ph_data, usb_cb_flags_t cb_flags,
6677c478bd9Sstevel@tonic-gate 	usb_cr_t cr)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp;
6707c478bd9Sstevel@tonic-gate 	int			flush_requests = 1;
6717c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
6727c478bd9Sstevel@tonic-gate 	int			timeout;
6737c478bd9Sstevel@tonic-gate 	int			rval = USB_SUCCESS;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ph_data->p_mutex));
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
6787c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
6797c478bd9Sstevel@tonic-gate 	    "usba_drain_cbs: ph_data=0x%p ref=%d req=%d cb=0x%x cr=%d",
680112116d8Sfb 	    (void *)ph_data, ph_impl->usba_ph_ref_count, ph_data->p_req_count,
6817c478bd9Sstevel@tonic-gate 	    cb_flags, cr);
6827c478bd9Sstevel@tonic-gate 	ASSERT(ph_data->p_req_count >= 0);
6837c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if (ph_data->p_dip) {
6867c478bd9Sstevel@tonic-gate 		if (USBA_IS_DEFAULT_PIPE(ph_data)) {
6877c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_USBAI,
6887c478bd9Sstevel@tonic-gate 			    usbai_log_handle,
6897c478bd9Sstevel@tonic-gate 			    "no flushing on default pipe!");
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 			flush_requests = 0;
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if (flush_requests) {
6967c478bd9Sstevel@tonic-gate 		/* flush all requests in the pipehandle queue */
6977c478bd9Sstevel@tonic-gate 		while ((req_wrp = (usba_req_wrapper_t *)
6987c478bd9Sstevel@tonic-gate 		    usba_rm_first_pvt_from_list(&ph_data->p_queue)) != NULL) {
6997c478bd9Sstevel@tonic-gate 			mutex_exit(&ph_data->p_mutex);
7007c478bd9Sstevel@tonic-gate 			usba_do_req_exc_cb(req_wrp, cr, cb_flags);
7017c478bd9Sstevel@tonic-gate 			mutex_enter(&ph_data->p_mutex);
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * wait for any callbacks in progress but don't wait for
7077c478bd9Sstevel@tonic-gate 	 * for queued requests on the default pipe
7087c478bd9Sstevel@tonic-gate 	 */
7097c478bd9Sstevel@tonic-gate 	for (timeout = 0; (timeout < usba_drain_timeout) &&
7107c478bd9Sstevel@tonic-gate 	    (ph_data->p_req_count >
7117c478bd9Sstevel@tonic-gate 	    usba_list_entry_count(&ph_data->p_queue));
7127c478bd9Sstevel@tonic-gate 	    timeout++) {
7137c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
7147c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(1000));
7157c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
7197c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
7207c478bd9Sstevel@tonic-gate 	    "usba_drain_cbs done: ph_data=0x%p ref=%d req=%d",
721112116d8Sfb 	    (void *)ph_data, ph_impl->usba_ph_ref_count, ph_data->p_req_count);
7227c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (timeout == usba_drain_timeout) {
7257c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
7267c478bd9Sstevel@tonic-gate 		    "draining callbacks timed out!");
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		rval = USB_FAILURE;
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	return (rval);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate  * usb_pipe_open():
7377c478bd9Sstevel@tonic-gate  *
7387c478bd9Sstevel@tonic-gate  * Before using any pipe including the default pipe, it should be opened
7397c478bd9Sstevel@tonic-gate  * using usb_pipe_open(). On a successful open, a pipe handle is returned
7407c478bd9Sstevel@tonic-gate  * for use in other usb_pipe_*() functions
7417c478bd9Sstevel@tonic-gate  *
7427c478bd9Sstevel@tonic-gate  * The default pipe can only be opened by the hub driver
7437c478bd9Sstevel@tonic-gate  *
7447c478bd9Sstevel@tonic-gate  * The bandwidth has been allocated and guaranteed on successful
7457c478bd9Sstevel@tonic-gate  * opening of an isoc/intr pipes.
7467c478bd9Sstevel@tonic-gate  *
7477c478bd9Sstevel@tonic-gate  * Only the default pipe can be shared. all other control pipes
7487c478bd9Sstevel@tonic-gate  * are excusively opened by default.
7497c478bd9Sstevel@tonic-gate  * A pipe policy and endpoint descriptor must always be provided
7507c478bd9Sstevel@tonic-gate  * except for default pipe
7517c478bd9Sstevel@tonic-gate  *
7527c478bd9Sstevel@tonic-gate  * Arguments:
7537c478bd9Sstevel@tonic-gate  *	dip		- devinfo ptr
7547c478bd9Sstevel@tonic-gate  *	ep		- endpoint descriptor pointer
7557c478bd9Sstevel@tonic-gate  *	pipe_policy	- pointer to pipe policy which provides hints on how
7567c478bd9Sstevel@tonic-gate  *			  the pipe will be used.
7577c478bd9Sstevel@tonic-gate  *	flags		- USB_FLAGS_SLEEP wait for resources
7587c478bd9Sstevel@tonic-gate  *			  to become available
7597c478bd9Sstevel@tonic-gate  *	pipe_handle	- a pipe handle pointer. On a successful open,
7607c478bd9Sstevel@tonic-gate  *			  a pipe_handle is returned in this pointer.
7617c478bd9Sstevel@tonic-gate  *
7627c478bd9Sstevel@tonic-gate  * Return values:
7637c478bd9Sstevel@tonic-gate  *	USB_SUCCESS	 - open succeeded
7647c478bd9Sstevel@tonic-gate  *	USB_FAILURE	 - unspecified open failure or pipe is already open
7657c478bd9Sstevel@tonic-gate  *	USB_NO_RESOURCES - no resources were available to complete the open
7667c478bd9Sstevel@tonic-gate  *	USB_NO_BANDWIDTH - no bandwidth available (isoc/intr pipes)
7677c478bd9Sstevel@tonic-gate  *	USB_*		 - refer to usbai.h
7687c478bd9Sstevel@tonic-gate  */
7697c478bd9Sstevel@tonic-gate int
7707c478bd9Sstevel@tonic-gate usb_pipe_open(
7717c478bd9Sstevel@tonic-gate 	dev_info_t		*dip,
7727c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*ep,
7737c478bd9Sstevel@tonic-gate 	usb_pipe_policy_t	*pipe_policy,
7747c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags,
7757c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	*pipe_handle)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
7787c478bd9Sstevel@tonic-gate 	int			rval;
7797c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data;
7807c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl;
7817c478bd9Sstevel@tonic-gate 	uchar_t			ep_index;
7827c478bd9Sstevel@tonic-gate 	int			kmflag;
7837c478bd9Sstevel@tonic-gate 	size_t			size;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
7867c478bd9Sstevel@tonic-gate 	    "usb_pipe_open:\n\t"
7877c478bd9Sstevel@tonic-gate 	    "dip=0x%p ep=0x%p pp=0x%p uf=0x%x ph=0x%p",
788112116d8Sfb 	    (void *)dip, (void *)ep, (void *)pipe_policy, usb_flags,
789112116d8Sfb 	    (void *)pipe_handle);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (pipe_handle == NULL)) {
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		return (USB_INVALID_ARGS);
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (servicing_interrupt() && (usb_flags & USB_FLAGS_SLEEP)) {
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		return (USB_INVALID_CONTEXT);
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if ((ep != NULL) && (pipe_policy == NULL)) {
8037c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8047c478bd9Sstevel@tonic-gate 		    "usb_pipe_open: null pipe policy");
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		return (USB_INVALID_ARGS);
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	/* is the device still connected? */
8107c478bd9Sstevel@tonic-gate 	if ((ep != NULL) & DEVI_IS_DEVICE_REMOVED(dip)) {
8117c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8127c478bd9Sstevel@tonic-gate 		    "usb_pipe_open: device has been removed");
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/*
8197c478bd9Sstevel@tonic-gate 	 * if a null endpoint pointer was passed, use the default
8207c478bd9Sstevel@tonic-gate 	 * endpoint descriptor
8217c478bd9Sstevel@tonic-gate 	 */
8227c478bd9Sstevel@tonic-gate 	if (ep == NULL) {
8237c478bd9Sstevel@tonic-gate 		if ((usb_flags & USBA_FLAGS_PRIVILEGED) == 0) {
8247c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8257c478bd9Sstevel@tonic-gate 			    "usb_pipe_open: not allowed to open def pipe");
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 			return (USB_INVALID_PERM);
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		ep = &usba_default_ep_descr;
8317c478bd9Sstevel@tonic-gate 		pipe_policy = &usba_default_ep_pipe_policy;
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (usb_flags & USB_FLAGS_SERIALIZED_CB) {
8357c478bd9Sstevel@tonic-gate 		if (((ep->bmAttributes & USB_EP_ATTR_MASK) ==
8367c478bd9Sstevel@tonic-gate 		    USB_EP_ATTR_CONTROL) ||
8377c478bd9Sstevel@tonic-gate 		    ((ep->bmAttributes & USB_EP_ATTR_MASK) ==
8387c478bd9Sstevel@tonic-gate 		    USB_EP_ATTR_ISOCH)) {
8397c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8407c478bd9Sstevel@tonic-gate 			    "usb_pipe_open: shared taskq not allowed with "
8417c478bd9Sstevel@tonic-gate 			    "ctrl or isoch pipe");
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 			return (USB_INVALID_ARGS);
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	kmflag	= (usb_flags & USB_FLAGS_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
8487c478bd9Sstevel@tonic-gate 	size	= sizeof (usba_pipe_handle_data_t);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if ((ph_data = kmem_zalloc(size, kmflag)) == NULL) {
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	/* check if pipe is already open and if so fail */
8567c478bd9Sstevel@tonic-gate 	ep_index = usb_get_ep_index(ep->bEndpointAddress);
8577c478bd9Sstevel@tonic-gate 	ph_impl = &usba_device->usb_ph_list[ep_index];
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
8607c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	if (ph_impl->usba_ph_data) {
8637c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8647c478bd9Sstevel@tonic-gate 		    "usb_pipe_open: pipe to ep %d already open", ep_index);
8657c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
8667c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
8677c478bd9Sstevel@tonic-gate 		kmem_free(ph_data, size);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		return (USB_BUSY);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_data = ph_data;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
8757c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	if (usb_flags & USB_FLAGS_SERIALIZED_CB) {
8787c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
8797c478bd9Sstevel@tonic-gate 		ph_data->p_spec_flag |= USBA_PH_FLAG_TQ_SHARE;
8807c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * allocate and initialize the pipe handle
8857c478bd9Sstevel@tonic-gate 	 */
8867c478bd9Sstevel@tonic-gate 	if ((rval = usba_init_pipe_handle(dip, usba_device,
8877c478bd9Sstevel@tonic-gate 	    ep, pipe_policy, ph_impl)) != USB_SUCCESS) {
8887c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8897c478bd9Sstevel@tonic-gate 		    "usb_pipe_open: pipe init failed (%d)", rval);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		return (rval);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 	ph_data = ph_impl->usba_ph_data;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	/*
8967c478bd9Sstevel@tonic-gate 	 * ask the hcd to open the pipe
8977c478bd9Sstevel@tonic-gate 	 */
8987c478bd9Sstevel@tonic-gate 	if ((rval = usba_device->usb_hcdi_ops->usba_hcdi_pipe_open(ph_data,
8997c478bd9Sstevel@tonic-gate 	    usb_flags)) != USB_SUCCESS) {
9007c478bd9Sstevel@tonic-gate 		usba_destroy_pipe_handle(ph_data);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		*pipe_handle = NULL;
9037c478bd9Sstevel@tonic-gate 	} else {
9047c478bd9Sstevel@tonic-gate 		*pipe_handle = (usb_pipe_handle_t)ph_impl;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 		/* set the pipe state after a successful hcd open */
9077c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
9087c478bd9Sstevel@tonic-gate 		usba_pipe_new_state(ph_data, USB_PIPE_STATE_IDLE);
9097c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
913112116d8Sfb 	    "usb_pipe_open: ph_impl=0x%p (0x%p)",
914112116d8Sfb 	    (void *)ph_impl, (void *)ph_data);
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	return (rval);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate  * usb_pipe_close/sync_close:
9227c478bd9Sstevel@tonic-gate  *
9237c478bd9Sstevel@tonic-gate  * Close a pipe and release all resources and free the pipe_handle.
9247c478bd9Sstevel@tonic-gate  * Automatic polling, if active,  will be terminated
9257c478bd9Sstevel@tonic-gate  *
9267c478bd9Sstevel@tonic-gate  * Arguments:
9277c478bd9Sstevel@tonic-gate  *	dip		- devinfo ptr
9287c478bd9Sstevel@tonic-gate  *	pipehandle	- pointer to pipehandle. The pipehandle will be
9297c478bd9Sstevel@tonic-gate  *			  zeroed on successful completion
9307c478bd9Sstevel@tonic-gate  *	flags		- USB_FLAGS_SLEEP:
9317c478bd9Sstevel@tonic-gate  *				wait for resources, pipe
9327c478bd9Sstevel@tonic-gate  *				to become free, all callbacks completed
9337c478bd9Sstevel@tonic-gate  *	callback	- If USB_FLAGS_SLEEP has not been specified, a
9347c478bd9Sstevel@tonic-gate  *			  callback will be performed.
9357c478bd9Sstevel@tonic-gate  *	callback_arg	- the first argument of the callback. Note that
9367c478bd9Sstevel@tonic-gate  *			  the pipehandle will be zeroed and not passed
9377c478bd9Sstevel@tonic-gate  *
9387c478bd9Sstevel@tonic-gate  * Notes:
9397c478bd9Sstevel@tonic-gate  * Pipe close will always succeed regardless whether USB_FLAGS_SLEEP has been
9407c478bd9Sstevel@tonic-gate  * specified or not.
9417c478bd9Sstevel@tonic-gate  * An async close will always succeed if the hint in the pipe policy
9427c478bd9Sstevel@tonic-gate  * has been correct about the max number of async taskq requests required.
9437c478bd9Sstevel@tonic-gate  * If there are really no resources, the pipe handle will be linked into
9447c478bd9Sstevel@tonic-gate  * a garbage pipe list and periodically checked by USBA until it can be
9457c478bd9Sstevel@tonic-gate  * closed. This may cause a hang in the detach of the driver.
9467c478bd9Sstevel@tonic-gate  * USBA will prevent the client from submitting more requests to a pipe
9477c478bd9Sstevel@tonic-gate  * that is being closed
9487c478bd9Sstevel@tonic-gate  * Subsequent usb_pipe_close() requests on the same pipe to USBA will
9497c478bd9Sstevel@tonic-gate  * wait for the previous close(s) to finish.
9507c478bd9Sstevel@tonic-gate  *
9517c478bd9Sstevel@tonic-gate  * Note that once we start closing a pipe, we cannot go back anymore
9527c478bd9Sstevel@tonic-gate  * to a normal pipe state
9537c478bd9Sstevel@tonic-gate  */
9547c478bd9Sstevel@tonic-gate void
9557c478bd9Sstevel@tonic-gate usb_pipe_close(dev_info_t	*dip,
9567c478bd9Sstevel@tonic-gate 		usb_pipe_handle_t pipe_handle,
9577c478bd9Sstevel@tonic-gate 		usb_flags_t	usb_flags,
9587c478bd9Sstevel@tonic-gate 		void		(*callback)(
9597c478bd9Sstevel@tonic-gate 				    usb_pipe_handle_t	pipe_handle,
9607c478bd9Sstevel@tonic-gate 				    usb_opaque_t	arg,
9617c478bd9Sstevel@tonic-gate 				    int			rval,
9627c478bd9Sstevel@tonic-gate 				    usb_cb_flags_t	flags),
9637c478bd9Sstevel@tonic-gate 		usb_opaque_t	callback_arg)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data;
9667c478bd9Sstevel@tonic-gate 	usba_ph_impl_t	*ph_impl = (usba_ph_impl_t *)pipe_handle;
9677c478bd9Sstevel@tonic-gate 	usb_cb_flags_t	callback_flags;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
970112116d8Sfb 	    "usb_pipe_close: ph=0x%p", (void *)pipe_handle);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	callback_flags = usba_check_intr_context(USB_CB_NO_INFO);
9737c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (pipe_handle == NULL)) {
9747c478bd9Sstevel@tonic-gate 		if (callback) {
9757c478bd9Sstevel@tonic-gate 			callback(pipe_handle, callback_arg,
9767c478bd9Sstevel@tonic-gate 			    USB_INVALID_ARGS, callback_flags);
9777c478bd9Sstevel@tonic-gate 		} else {
978d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBAI,
9797c478bd9Sstevel@tonic-gate 			    usbai_log_handle,
9807c478bd9Sstevel@tonic-gate 			    "usb_pipe_close: invalid arguments");
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		return;
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	if ((usb_flags & USBA_FLAGS_PRIVILEGED) == 0) {
9877c478bd9Sstevel@tonic-gate 		/*
9887c478bd9Sstevel@tonic-gate 		 * It is the client driver doing the pipe close,
9897c478bd9Sstevel@tonic-gate 		 * the pipe is no longer persistent then.
9907c478bd9Sstevel@tonic-gate 		 */
9917c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
9927c478bd9Sstevel@tonic-gate 		ph_impl->usba_ph_flags &= ~USBA_PH_DATA_PERSISTENT;
9937c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	if (servicing_interrupt() && (usb_flags & USB_FLAGS_SLEEP)) {
9977c478bd9Sstevel@tonic-gate 		if (callback) {
9987c478bd9Sstevel@tonic-gate 			callback(pipe_handle, callback_arg,
9997c478bd9Sstevel@tonic-gate 			    USB_INVALID_CONTEXT, callback_flags);
10007c478bd9Sstevel@tonic-gate 		} else {
1001d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBAI,
10027c478bd9Sstevel@tonic-gate 			    usbai_log_handle,
10037c478bd9Sstevel@tonic-gate 			    "usb_pipe_close: invalid context");
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 		return;
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if ((ph_data = usba_hold_ph_data(pipe_handle)) == NULL) {
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 		/* hold pipehandle anyways since we will decrement later */
10127c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
10137c478bd9Sstevel@tonic-gate 		ph_impl->usba_ph_ref_count++;
10147c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 		(void) usba_pipe_setup_func_call(dip, usba_pipe_sync_close,
10177c478bd9Sstevel@tonic-gate 		    ph_impl, NULL, usb_flags, callback, callback_arg);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		return;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	if (USBA_IS_DEFAULT_PIPE(ph_data) &&
10257c478bd9Sstevel@tonic-gate 	    ((usb_flags & USBA_FLAGS_PRIVILEGED) == 0)) {
1026d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
10277c478bd9Sstevel@tonic-gate 		    "usb_pipe_close: not allowed to close def pipe");
10287c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		if (callback) {
10337c478bd9Sstevel@tonic-gate 			callback(pipe_handle, callback_arg,
10347c478bd9Sstevel@tonic-gate 			    USB_INVALID_PIPE, callback_flags);
10357c478bd9Sstevel@tonic-gate 		} else {
1036d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBAI,
10377c478bd9Sstevel@tonic-gate 			    usbai_log_handle,
10387c478bd9Sstevel@tonic-gate 			    "usb_pipe_close: invalid pipe");
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 		return;
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	(void) usba_pipe_setup_func_call(dip, usba_pipe_sync_close,
10477c478bd9Sstevel@tonic-gate 	    ph_impl, NULL, usb_flags, callback, callback_arg);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10527c478bd9Sstevel@tonic-gate static int
10537c478bd9Sstevel@tonic-gate usba_pipe_sync_close(dev_info_t *dip, usba_ph_impl_t *ph_impl,
10547c478bd9Sstevel@tonic-gate 	usba_pipe_async_req_t *request, usb_flags_t usb_flags)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
10577c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = usba_get_ph_data(
1058112116d8Sfb 	    (usb_pipe_handle_t)ph_impl);
10597c478bd9Sstevel@tonic-gate 	int			attribute;
10607c478bd9Sstevel@tonic-gate 	uchar_t			dir;
10617c478bd9Sstevel@tonic-gate 	int			timeout;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	if (ph_impl == NULL) {
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
10697c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
10707c478bd9Sstevel@tonic-gate 	    "usba_pipe_sync_close: dip=0x%p ph_data=0x%p state=%d ref=%d",
1071112116d8Sfb 	    (void *)dip, (void *)ph_data, ph_impl->usba_ph_state,
1072112116d8Sfb 	    ph_impl->usba_ph_ref_count);
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	/*
10757c478bd9Sstevel@tonic-gate 	 * if another thread opens the pipe again, this loop could
10767c478bd9Sstevel@tonic-gate 	 * be truly forever
10777c478bd9Sstevel@tonic-gate 	 */
10787c478bd9Sstevel@tonic-gate 	if ((ph_data == NULL) ||
10797c478bd9Sstevel@tonic-gate 	    (ph_impl->usba_ph_state == USB_PIPE_STATE_CLOSING) ||
10807c478bd9Sstevel@tonic-gate 	    (ph_impl->usba_ph_state == USB_PIPE_STATE_CLOSED)) {
10817c478bd9Sstevel@tonic-gate 		/* wait forever till really closed */
10827c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
10837c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 		while (usba_get_ph_data((usb_pipe_handle_t)ph_impl)) {
10867c478bd9Sstevel@tonic-gate 			delay(1);
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_state = USB_PIPE_STATE_CLOSING;
10927c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
10957c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	attribute = ph_data->p_ep.bmAttributes & USB_EP_ATTR_MASK;
10987c478bd9Sstevel@tonic-gate 	dir = ph_data->p_ep.bEndpointAddress & USB_EP_DIR_MASK;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	usba_device = ph_data->p_usba_device;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	/*
11037c478bd9Sstevel@tonic-gate 	 * For control and bulk, we will drain till ref_count <= 1 and
11047c478bd9Sstevel@tonic-gate 	 * req_count == 0 but for isoc and intr IN, we can only wait
11057c478bd9Sstevel@tonic-gate 	 * till the ref_count === 1 as the req_count will never go to 0
11067c478bd9Sstevel@tonic-gate 	 */
11077c478bd9Sstevel@tonic-gate 	for (timeout = 0; timeout < usba_drain_timeout; timeout++) {
11087c478bd9Sstevel@tonic-gate 		switch (attribute) {
11097c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_CONTROL:
11107c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_BULK:
11117c478bd9Sstevel@tonic-gate 			if ((ph_data->p_req_count == 0) &&
11127c478bd9Sstevel@tonic-gate 			    (ph_impl->usba_ph_ref_count <= 1)) {
11137c478bd9Sstevel@tonic-gate 				goto done;
11147c478bd9Sstevel@tonic-gate 			}
11157c478bd9Sstevel@tonic-gate 			break;
11167c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_INTR:
11177c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_ISOCH:
11187c478bd9Sstevel@tonic-gate 			if (dir == USB_EP_DIR_IN) {
11197c478bd9Sstevel@tonic-gate 				if (ph_impl->usba_ph_ref_count <= 1) {
11207c478bd9Sstevel@tonic-gate 					goto done;
11217c478bd9Sstevel@tonic-gate 				}
11227c478bd9Sstevel@tonic-gate 			} else if ((ph_data->p_req_count == 0) &&
11237c478bd9Sstevel@tonic-gate 			    (ph_impl->usba_ph_ref_count <= 1)) {
11247c478bd9Sstevel@tonic-gate 				goto done;
11257c478bd9Sstevel@tonic-gate 			}
11267c478bd9Sstevel@tonic-gate 			break;
11277c478bd9Sstevel@tonic-gate 		}
11287c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
11297c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
11307c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(1000));
11317c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
11327c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate done:
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
11377c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	if (timeout >= usba_drain_timeout) {
11407c478bd9Sstevel@tonic-gate 		int draining_succeeded;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
11437c478bd9Sstevel@tonic-gate 		    "timeout on draining requests, resetting pipe 0x%p",
1144112116d8Sfb 		    (void *)ph_impl);
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 		(void) usba_device->usb_hcdi_ops->usba_hcdi_pipe_reset(ph_data,
11477c478bd9Sstevel@tonic-gate 		    USB_FLAGS_SLEEP);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
11507c478bd9Sstevel@tonic-gate 		draining_succeeded = usba_drain_cbs(ph_data, USB_CB_RESET_PIPE,
1151112116d8Sfb 		    USB_CR_PIPE_RESET);
11527c478bd9Sstevel@tonic-gate 		/* this MUST have succeeded */
11537c478bd9Sstevel@tonic-gate 		ASSERT(draining_succeeded == USB_SUCCESS);
11547c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
11577c478bd9Sstevel@tonic-gate 		    "draining requests done");
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if (usba_device->usb_hcdi_ops->usba_hcdi_pipe_close(ph_data,
11617c478bd9Sstevel@tonic-gate 	    usb_flags) != USB_SUCCESS) {
1162d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
11637c478bd9Sstevel@tonic-gate 		    "usba_pipe_sync_close: hcd close failed");
11647c478bd9Sstevel@tonic-gate 		/* carry on regardless! */
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	usba_destroy_pipe_handle(ph_data);
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate /*
11747c478bd9Sstevel@tonic-gate  * usb_pipe_set_private:
11757c478bd9Sstevel@tonic-gate  *	set private client date in the pipe handle
11767c478bd9Sstevel@tonic-gate  */
11777c478bd9Sstevel@tonic-gate int
11787c478bd9Sstevel@tonic-gate usb_pipe_set_private(usb_pipe_handle_t	pipe_handle, usb_opaque_t data)
11797c478bd9Sstevel@tonic-gate {
11807c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = usba_hold_ph_data(pipe_handle);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
11837c478bd9Sstevel@tonic-gate 	    "usb_pipe_set_private: ");
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	if (ph_data == NULL) {
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 		return (USB_INVALID_PIPE);
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate 	if (USBA_IS_DEFAULT_PIPE(ph_data)) {
11907c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_data->p_ph_impl);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		return (USB_INVALID_PERM);
11937c478bd9Sstevel@tonic-gate 	}
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
11967c478bd9Sstevel@tonic-gate 	ph_data->p_client_private = data;
11977c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	usba_release_ph_data(ph_data->p_ph_impl);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate /*
12067c478bd9Sstevel@tonic-gate  * usb_pipe_get_private:
12077c478bd9Sstevel@tonic-gate  *	get private client date from the pipe handle
12087c478bd9Sstevel@tonic-gate  */
12097c478bd9Sstevel@tonic-gate usb_opaque_t
12107c478bd9Sstevel@tonic-gate usb_pipe_get_private(usb_pipe_handle_t	pipe_handle)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = usba_hold_ph_data(pipe_handle);
12137c478bd9Sstevel@tonic-gate 	usb_opaque_t		data;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
12167c478bd9Sstevel@tonic-gate 	    "usb_pipe_get_private:");
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	if (ph_data == NULL) {
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		return (NULL);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
12247c478bd9Sstevel@tonic-gate 	data = ph_data->p_client_private;
12257c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	usba_release_ph_data(ph_data->p_ph_impl);
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	return (data);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate /*
12347c478bd9Sstevel@tonic-gate  * usb_pipe_reset
12357c478bd9Sstevel@tonic-gate  * Arguments:
12367c478bd9Sstevel@tonic-gate  *	dip		- devinfo pointer
12377c478bd9Sstevel@tonic-gate  *	pipe_handle	- opaque pipe handle
12387c478bd9Sstevel@tonic-gate  * Returns:
12397c478bd9Sstevel@tonic-gate  *	USB_SUCCESS	- pipe successfully reset or request queued
12407c478bd9Sstevel@tonic-gate  *	USB_FAILURE	- undetermined failure
12417c478bd9Sstevel@tonic-gate  *	USB_INVALID_PIPE - pipe is invalid or already closed
12427c478bd9Sstevel@tonic-gate  */
12437c478bd9Sstevel@tonic-gate void
12447c478bd9Sstevel@tonic-gate usb_pipe_reset(dev_info_t		*dip,
12457c478bd9Sstevel@tonic-gate 		usb_pipe_handle_t	pipe_handle,
12467c478bd9Sstevel@tonic-gate 		usb_flags_t		usb_flags,
12477c478bd9Sstevel@tonic-gate 		void			(*callback)(
12487c478bd9Sstevel@tonic-gate 					    usb_pipe_handle_t	ph,
12497c478bd9Sstevel@tonic-gate 					    usb_opaque_t	arg,
12507c478bd9Sstevel@tonic-gate 					    int			rval,
12517c478bd9Sstevel@tonic-gate 					    usb_cb_flags_t	flags),
12527c478bd9Sstevel@tonic-gate 		usb_opaque_t		callback_arg)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = (usba_ph_impl_t *)pipe_handle;
12557c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = usba_hold_ph_data(pipe_handle);
12567c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		callback_flags;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
12597c478bd9Sstevel@tonic-gate 	    "usb_pipe_reset: dip=0x%p ph=0x%p uf=0x%x",
1260112116d8Sfb 	    (void *)dip, (void *)pipe_handle, usb_flags);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	callback_flags = usba_check_intr_context(USB_CB_NO_INFO);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (ph_data == NULL)) {
12657c478bd9Sstevel@tonic-gate 		if (callback) {
12667c478bd9Sstevel@tonic-gate 			callback(pipe_handle, callback_arg,
12677c478bd9Sstevel@tonic-gate 			    USB_INVALID_ARGS, callback_flags);
12687c478bd9Sstevel@tonic-gate 		} else {
1269d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBAI,
12707c478bd9Sstevel@tonic-gate 			    usbai_log_handle,
12717c478bd9Sstevel@tonic-gate 			    "usb_pipe_reset: invalid arguments");
12727c478bd9Sstevel@tonic-gate 		}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 		return;
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 	if (servicing_interrupt() && (usb_flags & USB_FLAGS_SLEEP)) {
12797c478bd9Sstevel@tonic-gate 		if (callback) {
12807c478bd9Sstevel@tonic-gate 			callback(pipe_handle, callback_arg,
12817c478bd9Sstevel@tonic-gate 			    USB_INVALID_CONTEXT, callback_flags);
12827c478bd9Sstevel@tonic-gate 		} else {
1283d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBAI,
12847c478bd9Sstevel@tonic-gate 			    usbai_log_handle,
12857c478bd9Sstevel@tonic-gate 			    "usb_pipe_reset: invalid context");
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		return;
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/* is this the default pipe? */
12967c478bd9Sstevel@tonic-gate 	if (USBA_IS_DEFAULT_PIPE(ph_data)) {
12977c478bd9Sstevel@tonic-gate 		if ((usb_flags & USBA_FLAGS_PRIVILEGED) == 0) {
1298d291d9f2Sfrits 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
12997c478bd9Sstevel@tonic-gate 			    "usb_pipe_reset: not allowed to reset def pipe");
13007c478bd9Sstevel@tonic-gate 			mutex_exit(&ph_data->p_mutex);
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 			if (callback) {
13037c478bd9Sstevel@tonic-gate 				callback(pipe_handle, callback_arg,
13047c478bd9Sstevel@tonic-gate 				    USB_INVALID_PIPE, callback_flags);
13057c478bd9Sstevel@tonic-gate 			} else {
1306d291d9f2Sfrits 				USB_DPRINTF_L2(DPRINT_MASK_USBAI,
13077c478bd9Sstevel@tonic-gate 				    usbai_log_handle,
13087c478bd9Sstevel@tonic-gate 				    "usb_pipe_reset: invalid pipe");
13097c478bd9Sstevel@tonic-gate 			}
13107c478bd9Sstevel@tonic-gate 			usba_release_ph_data(ph_impl);
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 			return;
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	(void) usba_pipe_setup_func_call(dip,
13187c478bd9Sstevel@tonic-gate 	    usba_pipe_sync_reset, ph_impl, NULL, usb_flags, callback,
13197c478bd9Sstevel@tonic-gate 	    callback_arg);
13207c478bd9Sstevel@tonic-gate }
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13247c478bd9Sstevel@tonic-gate int
13257c478bd9Sstevel@tonic-gate usba_pipe_sync_reset(dev_info_t	*dip,
13267c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl,
13277c478bd9Sstevel@tonic-gate 	usba_pipe_async_req_t	*request,
13287c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags)
13297c478bd9Sstevel@tonic-gate {
13307c478bd9Sstevel@tonic-gate 	int rval, draining_succeeded;
13317c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = usba_get_ph_data((usb_pipe_handle_t)
1332112116d8Sfb 	    ph_impl);
13337c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
13367c478bd9Sstevel@tonic-gate 	    "usba_pipe_sync_reset: dip=0x%p ph_data=0x%p uf=0x%x",
1337112116d8Sfb 	    (void *)dip, (void *)ph_data, usb_flags);
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
13407c478bd9Sstevel@tonic-gate 	usba_device = ph_data->p_usba_device;
13417c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	rval = usba_device->usb_hcdi_ops->usba_hcdi_pipe_reset(ph_data,
1344112116d8Sfb 	    usb_flags);
13457c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/*
13487c478bd9Sstevel@tonic-gate 	 * The host controller has stopped polling of the endpoint.
13497c478bd9Sstevel@tonic-gate 	 */
13507c478bd9Sstevel@tonic-gate 	draining_succeeded = usba_drain_cbs(ph_data, USB_CB_RESET_PIPE,
1351112116d8Sfb 	    USB_CR_PIPE_RESET);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	/* this MUST have succeeded */
13547c478bd9Sstevel@tonic-gate 	ASSERT(draining_succeeded == USB_SUCCESS);
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	usba_pipe_new_state(ph_data, USB_PIPE_STATE_IDLE);
13577c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	/*
13607c478bd9Sstevel@tonic-gate 	 * if there are requests still queued on the default pipe,
13617c478bd9Sstevel@tonic-gate 	 * start them now
13627c478bd9Sstevel@tonic-gate 	 */
13637c478bd9Sstevel@tonic-gate 	usba_start_next_req(ph_data);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	usba_release_ph_data(ph_impl);
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	return (rval);
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate /*
13727c478bd9Sstevel@tonic-gate  * usba_pipe_clear:
13737c478bd9Sstevel@tonic-gate  *	call hcd to clear pipe but don't wait for draining
13747c478bd9Sstevel@tonic-gate  */
13757c478bd9Sstevel@tonic-gate void
13767c478bd9Sstevel@tonic-gate usba_pipe_clear(usb_pipe_handle_t pipe_handle)
13777c478bd9Sstevel@tonic-gate {
13787c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = usba_get_ph_data(pipe_handle);
13797c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
13807c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp;
13817c478bd9Sstevel@tonic-gate 	int			flush_requests = 1;
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1384112116d8Sfb 	    "usba_pipe_clear: ph_data=0x%p", (void *)ph_data);
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	if (ph_data == NULL) {
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 		return;
13897c478bd9Sstevel@tonic-gate 	}
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
13927c478bd9Sstevel@tonic-gate 	if (USBA_PIPE_CLOSING(usba_get_ph_state(ph_data))) {
13937c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 		return;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	usba_device = ph_data->p_usba_device;
13987c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	(void) usba_device->usb_hcdi_ops->usba_hcdi_pipe_reset(ph_data,
1401112116d8Sfb 	    USB_FLAGS_SLEEP);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
14047c478bd9Sstevel@tonic-gate 	if (ph_data->p_dip) {
14057c478bd9Sstevel@tonic-gate 		if (USBA_IS_DEFAULT_PIPE(ph_data)) {
14067c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_USBAI,
14077c478bd9Sstevel@tonic-gate 			    usbai_log_handle,
14087c478bd9Sstevel@tonic-gate 			    "no flushing on default pipe!");
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 			flush_requests = 0;
14117c478bd9Sstevel@tonic-gate 		}
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	if (flush_requests) {
14157c478bd9Sstevel@tonic-gate 		/* flush all requests in the pipehandle queue */
14167c478bd9Sstevel@tonic-gate 		while ((req_wrp = (usba_req_wrapper_t *)
14177c478bd9Sstevel@tonic-gate 		    usba_rm_first_pvt_from_list(&ph_data->p_queue)) != NULL) {
14187c478bd9Sstevel@tonic-gate 			mutex_exit(&ph_data->p_mutex);
14197c478bd9Sstevel@tonic-gate 			usba_do_req_exc_cb(req_wrp, USB_CR_FLUSHED,
1420112116d8Sfb 			    USB_CB_RESET_PIPE);
14217c478bd9Sstevel@tonic-gate 			mutex_enter(&ph_data->p_mutex);
14227c478bd9Sstevel@tonic-gate 		}
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	usba_pipe_new_state(ph_data, USB_PIPE_STATE_IDLE);
14267c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate /*
14317c478bd9Sstevel@tonic-gate  *
14327c478bd9Sstevel@tonic-gate  * usb_pipe_drain_reqs
14337c478bd9Sstevel@tonic-gate  *	this function blocks until there are no more requests
14347c478bd9Sstevel@tonic-gate  *	owned by this dip on the pipe
14357c478bd9Sstevel@tonic-gate  *
14367c478bd9Sstevel@tonic-gate  * Arguments:
14377c478bd9Sstevel@tonic-gate  *	dip		- devinfo pointer
14387c478bd9Sstevel@tonic-gate  *	pipe_handle	- opaque pipe handle
14397c478bd9Sstevel@tonic-gate  *	timeout 	- timeout in seconds
14407c478bd9Sstevel@tonic-gate  *	flags		- USB_FLAGS_SLEEP:
14417c478bd9Sstevel@tonic-gate  *				wait for completion.
14427c478bd9Sstevel@tonic-gate  *	cb		- if USB_FLAGS_SLEEP has not been specified
14437c478bd9Sstevel@tonic-gate  *			  this callback function will be called on
14447c478bd9Sstevel@tonic-gate  *			  completion. This callback may be NULL
14457c478bd9Sstevel@tonic-gate  *			  and no notification of completion will then
14467c478bd9Sstevel@tonic-gate  *			  be provided.
14477c478bd9Sstevel@tonic-gate  *	cb_arg		- 2nd argument to callback function.
14487c478bd9Sstevel@tonic-gate  *
14497c478bd9Sstevel@tonic-gate  * callback and callback_arg should be NULL if USB_FLAGS_SLEEP has
14507c478bd9Sstevel@tonic-gate  * been specified
14517c478bd9Sstevel@tonic-gate  *
14527c478bd9Sstevel@tonic-gate  * Returns:
14537c478bd9Sstevel@tonic-gate  *	USB_SUCCESS	- pipe successfully reset or request queued
14547c478bd9Sstevel@tonic-gate  *	USB_FAILURE	- timeout
14557c478bd9Sstevel@tonic-gate  *	USB_*		- refer to usbai.h
14567c478bd9Sstevel@tonic-gate  */
14577c478bd9Sstevel@tonic-gate int
14587c478bd9Sstevel@tonic-gate usb_pipe_drain_reqs(dev_info_t	*dip,
14597c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle,
14607c478bd9Sstevel@tonic-gate 	uint_t			time,
14617c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags,
14627c478bd9Sstevel@tonic-gate 	void			(*cb)(
14637c478bd9Sstevel@tonic-gate 				    usb_pipe_handle_t	ph,
14647c478bd9Sstevel@tonic-gate 				    usb_opaque_t	arg,   /* cb arg */
14657c478bd9Sstevel@tonic-gate 				    int			rval,
14667c478bd9Sstevel@tonic-gate 				    usb_cb_flags_t	flags),
14677c478bd9Sstevel@tonic-gate 	usb_opaque_t		cb_arg)
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = (usba_ph_impl_t *)pipe_handle;
14707c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = usba_hold_ph_data(pipe_handle);
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
14737c478bd9Sstevel@tonic-gate 	    "usb_pipe_drain_reqs: dip=0x%p ph_data=0x%p tm=%d uf=0x%x",
1474112116d8Sfb 	    (void *)dip, (void *)ph_data, time, usb_flags);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	if (ph_data == NULL) {
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 		return (USB_INVALID_PIPE);
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
14817c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		return (USB_INVALID_ARGS);
14847c478bd9Sstevel@tonic-gate 	}
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	if ((usb_flags & USB_FLAGS_SLEEP) && servicing_interrupt()) {
14877c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 		return (USB_INVALID_CONTEXT);
14907c478bd9Sstevel@tonic-gate 	}
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	(void) usba_pipe_setup_func_call(dip, usba_pipe_sync_drain_reqs,
14937c478bd9Sstevel@tonic-gate 	    ph_impl, (usb_opaque_t)((uintptr_t)time), usb_flags, cb, cb_arg);
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate /*
15007c478bd9Sstevel@tonic-gate  * usba_pipe_sync_drain_reqs
15017c478bd9Sstevel@tonic-gate  *	this function blocks until there are no more requests
15027c478bd9Sstevel@tonic-gate  *	owned by this dip on the pipe
15037c478bd9Sstevel@tonic-gate  *
15047c478bd9Sstevel@tonic-gate  * Arguments:
15057c478bd9Sstevel@tonic-gate  *	dip		- devinfo pointer
15067c478bd9Sstevel@tonic-gate  *	ph_impl		- pipe impl handle
15077c478bd9Sstevel@tonic-gate  *	timeout		- timeout in seconds
15087c478bd9Sstevel@tonic-gate  * Returns:
15097c478bd9Sstevel@tonic-gate  *	USB_SUCCESS	- pipe successfully reset or request queued
15107c478bd9Sstevel@tonic-gate  *	USB_FAILURE	- timeout
15117c478bd9Sstevel@tonic-gate  *	USB_*		- see usbai.h
15127c478bd9Sstevel@tonic-gate  */
15137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15147c478bd9Sstevel@tonic-gate int
15157c478bd9Sstevel@tonic-gate usba_pipe_sync_drain_reqs(dev_info_t	*dip,
15167c478bd9Sstevel@tonic-gate 		usba_ph_impl_t		*ph_impl,
15177c478bd9Sstevel@tonic-gate 		usba_pipe_async_req_t	*request,
15187c478bd9Sstevel@tonic-gate 		usb_flags_t		usb_flags)
15197c478bd9Sstevel@tonic-gate {
15207c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = usba_get_ph_data((usb_pipe_handle_t)
1521112116d8Sfb 	    ph_impl);
15227c478bd9Sstevel@tonic-gate 	int		i;
15237c478bd9Sstevel@tonic-gate 	int		timeout = 100 * (int)((uintptr_t)(request->arg));
15247c478bd9Sstevel@tonic-gate 						/* delay will be 10 ms */
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
15297c478bd9Sstevel@tonic-gate 	    "usba_pipe_sync_drain_reqs: "
15307c478bd9Sstevel@tonic-gate 	    "dip=0x%p ph_data=0x%p timeout=%d ref=%d req=%d",
1531112116d8Sfb 	    (void *)dip, (void *)ph_data, timeout,
1532112116d8Sfb 	    usba_get_ph_ref_count(ph_data),
15337c478bd9Sstevel@tonic-gate 	    ph_data->p_req_count);
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	ASSERT(ph_data->p_req_count >= 0);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	/*
15387c478bd9Sstevel@tonic-gate 	 * for default pipe, we need to check the active request
15397c478bd9Sstevel@tonic-gate 	 * and the queue
15407c478bd9Sstevel@tonic-gate 	 * Note that a pipe reset on the default pipe doesn't flush
15417c478bd9Sstevel@tonic-gate 	 * the queue
15427c478bd9Sstevel@tonic-gate 	 * for all other pipes we just check ref and req count since
15437c478bd9Sstevel@tonic-gate 	 * these pipes are unshared
15447c478bd9Sstevel@tonic-gate 	 */
15457c478bd9Sstevel@tonic-gate 	if (USBA_IS_DEFAULT_PIPE(ph_data)) {
15467c478bd9Sstevel@tonic-gate 		for (i = 0; (i < timeout) || (request->arg == 0); i++) {
15477c478bd9Sstevel@tonic-gate 			usba_list_entry_t *next, *tmpnext;
15487c478bd9Sstevel@tonic-gate 			usba_req_wrapper_t *req_wrp = (usba_req_wrapper_t *)
1549112116d8Sfb 			    ph_data->p_active_cntrl_req_wrp;
15507c478bd9Sstevel@tonic-gate 			int found = 0;
15517c478bd9Sstevel@tonic-gate 			int count = 0;
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 			/* active_req_wrp is only for control pipes */
15547c478bd9Sstevel@tonic-gate 			if ((req_wrp == NULL) || (req_wrp->wr_dip != dip)) {
15557c478bd9Sstevel@tonic-gate 				/* walk the queue */
15567c478bd9Sstevel@tonic-gate 				mutex_enter(&ph_data->p_queue.list_mutex);
15577c478bd9Sstevel@tonic-gate 				next = ph_data->p_queue.next;
15587c478bd9Sstevel@tonic-gate 				while (next != NULL) {
15597c478bd9Sstevel@tonic-gate 					mutex_enter(&next->list_mutex);
15607c478bd9Sstevel@tonic-gate 					req_wrp = (usba_req_wrapper_t *)
1561112116d8Sfb 					    next->private;
15627c478bd9Sstevel@tonic-gate 					found = (req_wrp->wr_dip == dip);
15637c478bd9Sstevel@tonic-gate 					if (found) {
15647c478bd9Sstevel@tonic-gate 						mutex_exit(&next->list_mutex);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 						break;
15677c478bd9Sstevel@tonic-gate 					}
15687c478bd9Sstevel@tonic-gate 					tmpnext = next->next;
15697c478bd9Sstevel@tonic-gate 					mutex_exit(&next->list_mutex);
15707c478bd9Sstevel@tonic-gate 					next = tmpnext;
15717c478bd9Sstevel@tonic-gate 					count++;
15727c478bd9Sstevel@tonic-gate 				}
15737c478bd9Sstevel@tonic-gate 				mutex_exit(&ph_data->p_queue.list_mutex);
15747c478bd9Sstevel@tonic-gate 				if (found == 0) {
15757c478bd9Sstevel@tonic-gate 					break;
15767c478bd9Sstevel@tonic-gate 				}
15777c478bd9Sstevel@tonic-gate 			}
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
15807c478bd9Sstevel@tonic-gate 			    "usb_pipe_sync_drain_reqs: "
15817c478bd9Sstevel@tonic-gate 			    "cnt=%d active_req_wrp=0x%p",
1582112116d8Sfb 			    count, (void *)ph_data->p_active_cntrl_req_wrp);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 			mutex_exit(&ph_data->p_mutex);
15857c478bd9Sstevel@tonic-gate 			delay(drv_usectohz(10000));
15867c478bd9Sstevel@tonic-gate 			mutex_enter(&ph_data->p_mutex);
15877c478bd9Sstevel@tonic-gate 		}
15887c478bd9Sstevel@tonic-gate 	} else {
15897c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_ph_impl->usba_ph_mutex);
15907c478bd9Sstevel@tonic-gate 		for (i = 0; (i < timeout) || (request->arg == 0); i++) {
15917c478bd9Sstevel@tonic-gate 			ASSERT(ph_data->p_req_count >= 0);
15927c478bd9Sstevel@tonic-gate 			if (ph_data->p_req_count ||
15937c478bd9Sstevel@tonic-gate 			    (ph_data->p_ph_impl->usba_ph_ref_count > 1)) {
15947c478bd9Sstevel@tonic-gate 				mutex_exit(&ph_data->p_ph_impl->usba_ph_mutex);
15957c478bd9Sstevel@tonic-gate 				mutex_exit(&ph_data->p_mutex);
15967c478bd9Sstevel@tonic-gate 				delay(drv_usectohz(10000));
15977c478bd9Sstevel@tonic-gate 				mutex_enter(&ph_data->p_mutex);
15987c478bd9Sstevel@tonic-gate 				mutex_enter(&ph_data->p_ph_impl->usba_ph_mutex);
15997c478bd9Sstevel@tonic-gate 			} else {
16007c478bd9Sstevel@tonic-gate 				break;
16017c478bd9Sstevel@tonic-gate 			}
16027c478bd9Sstevel@tonic-gate 		}
16037c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_ph_impl->usba_ph_mutex);
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
16077c478bd9Sstevel@tonic-gate 	    "usb_pipe_sync_drain_reqs: timeout=%d active_req_wrp=0x%p req=%d",
1608112116d8Sfb 	    i, (void *)ph_data->p_active_cntrl_req_wrp, ph_data->p_req_count);
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	usba_release_ph_data(ph_impl);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	return (i >= timeout ? USB_FAILURE : USB_SUCCESS);
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate /*
16197c478bd9Sstevel@tonic-gate  * usba_persistent_pipe_open
16207c478bd9Sstevel@tonic-gate  *	Open all the pipes marked persistent for this device
16217c478bd9Sstevel@tonic-gate  */
16227c478bd9Sstevel@tonic-gate int
16237c478bd9Sstevel@tonic-gate usba_persistent_pipe_open(usba_device_t *usba_device)
16247c478bd9Sstevel@tonic-gate {
16257c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl;
16267c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle;
16277c478bd9Sstevel@tonic-gate 	int			i;
16287c478bd9Sstevel@tonic-gate 	int			rval = USB_SUCCESS;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1631112116d8Sfb 	    "usba_persistent_pipe_open: usba_device=0x%p", (void *)usba_device);
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	if (usba_device != NULL) {
16347c478bd9Sstevel@tonic-gate 		/* default pipe is the first one to be opened */
16357c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
16367c478bd9Sstevel@tonic-gate 		for (i = 0; (rval == USB_SUCCESS) &&
16377c478bd9Sstevel@tonic-gate 		    (i < USBA_N_ENDPOINTS); i++) {
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 			ph_impl = &usba_device->usb_ph_list[i];
16407c478bd9Sstevel@tonic-gate 			mutex_enter(&ph_impl->usba_ph_mutex);
16417c478bd9Sstevel@tonic-gate 			if (ph_impl->usba_ph_flags & USBA_PH_DATA_PERSISTENT) {
16427c478bd9Sstevel@tonic-gate 				ph_impl->usba_ph_flags &=
1643112116d8Sfb 				    ~USBA_PH_DATA_PERSISTENT;
16447c478bd9Sstevel@tonic-gate 				mutex_exit(&ph_impl->usba_ph_mutex);
16457c478bd9Sstevel@tonic-gate 				mutex_exit(&usba_device->usb_mutex);
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 				rval = usb_pipe_open(ph_impl->usba_ph_dip,
16487c478bd9Sstevel@tonic-gate 				    &ph_impl->usba_ph_ep,
16497c478bd9Sstevel@tonic-gate 				    &ph_impl->usba_ph_policy,
16507c478bd9Sstevel@tonic-gate 				    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED,
16517c478bd9Sstevel@tonic-gate 				    &pipe_handle);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(DPRINT_MASK_USBAI,
16547c478bd9Sstevel@tonic-gate 				    usbai_log_handle,
16557c478bd9Sstevel@tonic-gate 				    "usba_persistent_pipe_open: "
16567c478bd9Sstevel@tonic-gate 				    "ep_index=%d, rval=%d", i, rval);
16577c478bd9Sstevel@tonic-gate 				mutex_enter(&usba_device->usb_mutex);
16587c478bd9Sstevel@tonic-gate 				mutex_enter(&ph_impl->usba_ph_mutex);
16597c478bd9Sstevel@tonic-gate 			}
16607c478bd9Sstevel@tonic-gate 			mutex_exit(&ph_impl->usba_ph_mutex);
16617c478bd9Sstevel@tonic-gate 		}
16627c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	return (rval);
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate /*
16707c478bd9Sstevel@tonic-gate  * usba_persistent_pipe_close
16717c478bd9Sstevel@tonic-gate  *	Close all pipes of this device and mark them persistent
16727c478bd9Sstevel@tonic-gate  */
16737c478bd9Sstevel@tonic-gate void
16747c478bd9Sstevel@tonic-gate usba_persistent_pipe_close(usba_device_t *usba_device)
16757c478bd9Sstevel@tonic-gate {
16767c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl;
16777c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle;
16787c478bd9Sstevel@tonic-gate 	int			i;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1681112116d8Sfb 	    "usba_persistent_pipe_close: usba_device=0x%p",
1682112116d8Sfb 	    (void *)usba_device);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	if (usba_device != NULL) {
16857c478bd9Sstevel@tonic-gate 		/* default pipe is the last one to be closed */
16867c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		for (i = (USBA_N_ENDPOINTS - 1); i >= 0; i--) {
16897c478bd9Sstevel@tonic-gate 			ph_impl = &usba_device->usb_ph_list[i];
16907c478bd9Sstevel@tonic-gate 			if (ph_impl->usba_ph_data != NULL) {
16917c478bd9Sstevel@tonic-gate 				mutex_enter(&ph_impl->usba_ph_mutex);
16927c478bd9Sstevel@tonic-gate 				ph_impl->usba_ph_flags |=
1693112116d8Sfb 				    USBA_PH_DATA_PERSISTENT;
16947c478bd9Sstevel@tonic-gate 				mutex_exit(&ph_impl->usba_ph_mutex);
16957c478bd9Sstevel@tonic-gate 				mutex_exit(&usba_device->usb_mutex);
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 				pipe_handle = (usb_pipe_handle_t)ph_impl;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 				usb_pipe_close(ph_impl->usba_ph_dip,
17007c478bd9Sstevel@tonic-gate 				    pipe_handle,
17017c478bd9Sstevel@tonic-gate 				    USB_FLAGS_SLEEP | USBA_FLAGS_PRIVILEGED,
17027c478bd9Sstevel@tonic-gate 				    NULL, NULL);
17037c478bd9Sstevel@tonic-gate 				mutex_enter(&usba_device->usb_mutex);
17047c478bd9Sstevel@tonic-gate 				ASSERT(ph_impl->usba_ph_data == NULL);
17057c478bd9Sstevel@tonic-gate 			}
17067c478bd9Sstevel@tonic-gate 		}
17077c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate }
1710