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 /*
22ff0e937bSRaymond Chen  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24e2c88f0cSGarrett D'Amore  *
25e2c88f0cSGarrett D'Amore  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
26*0d2006e4SRobert Mustacchi  * Copyright 2019 Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * USBA: Solaris USB Architecture support
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * all functions exposed to client drivers  have prefix usb_ while all USBA
347c478bd9Sstevel@tonic-gate  * internal functions or functions exposed to HCD or hubd only have prefix
357c478bd9Sstevel@tonic-gate  * usba_
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * this file contains all USBAI pipe management
387c478bd9Sstevel@tonic-gate  *	usb_pipe_open()
397c478bd9Sstevel@tonic-gate  *	usb_pipe_close()
407c478bd9Sstevel@tonic-gate  *	usb_pipe_set_private()
417c478bd9Sstevel@tonic-gate  *	usb_pipe_get_private()
427c478bd9Sstevel@tonic-gate  *	usb_pipe_abort()
437c478bd9Sstevel@tonic-gate  *	usb_pipe_reset()
447c478bd9Sstevel@tonic-gate  *	usb_pipe_drain_reqs()
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate #define	USBA_FRAMEWORK
477c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
487c478bd9Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
497c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate extern	pri_t	maxclsyspri;
527c478bd9Sstevel@tonic-gate extern	pri_t	minclsyspri;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* function prototypes */
557c478bd9Sstevel@tonic-gate static	void	usba_pipe_do_async_func_thread(void *arg);
567c478bd9Sstevel@tonic-gate static	int	usba_pipe_sync_close(dev_info_t *, usba_ph_impl_t *,
577c478bd9Sstevel@tonic-gate 			usba_pipe_async_req_t *, usb_flags_t);
587c478bd9Sstevel@tonic-gate static	int	usba_pipe_sync_reset(dev_info_t *, usba_ph_impl_t *,
597c478bd9Sstevel@tonic-gate 			usba_pipe_async_req_t *, usb_flags_t);
607c478bd9Sstevel@tonic-gate static	int	usba_pipe_sync_drain_reqs(dev_info_t *, usba_ph_impl_t *,
617c478bd9Sstevel@tonic-gate 			usba_pipe_async_req_t *, usb_flags_t);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* local tunables */
644610e4a0Sfrits int	usba_drain_timeout = 1000;	/* in ms */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* return the default pipe for this device */
677c478bd9Sstevel@tonic-gate usb_pipe_handle_t
usba_get_dflt_pipe_handle(dev_info_t * dip)687c478bd9Sstevel@tonic-gate usba_get_dflt_pipe_handle(dev_info_t *dip)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
717c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle = NULL;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	if (dip) {
747c478bd9Sstevel@tonic-gate 		usba_device = usba_get_usba_device(dip);
757c478bd9Sstevel@tonic-gate 		if (usba_device) {
767c478bd9Sstevel@tonic-gate 			pipe_handle =
777c478bd9Sstevel@tonic-gate 			    (usb_pipe_handle_t)&usba_device->usb_ph_list[0];
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	return (pipe_handle);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* return dip owner of pipe_handle */
867c478bd9Sstevel@tonic-gate dev_info_t *
usba_get_dip(usb_pipe_handle_t pipe_handle)877c478bd9Sstevel@tonic-gate usba_get_dip(usb_pipe_handle_t pipe_handle)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = (usba_ph_impl_t *)pipe_handle;
907c478bd9Sstevel@tonic-gate 	dev_info_t		*dip = NULL;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (ph_impl) {
937c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
947c478bd9Sstevel@tonic-gate 		dip = ph_impl->usba_ph_dip;
957c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	return (dip);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate usb_pipe_handle_t
usba_usbdev_to_dflt_pipe_handle(usba_device_t * usba_device)1037c478bd9Sstevel@tonic-gate usba_usbdev_to_dflt_pipe_handle(usba_device_t *usba_device)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle = NULL;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if ((usba_device) &&
1087c478bd9Sstevel@tonic-gate 	    (usba_device->usb_ph_list[0].usba_ph_data != NULL)) {
1097c478bd9Sstevel@tonic-gate 		pipe_handle = (usb_pipe_handle_t)&usba_device->usb_ph_list[0];
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	return (pipe_handle);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate usba_pipe_handle_data_t *
usba_get_ph_data(usb_pipe_handle_t pipe_handle)1177c478bd9Sstevel@tonic-gate usba_get_ph_data(usb_pipe_handle_t pipe_handle)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = (usba_ph_impl_t *)pipe_handle;
1207c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = NULL;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (ph_impl) {
1237c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
1247c478bd9Sstevel@tonic-gate 		ASSERT(ph_impl->usba_ph_ref_count >= 0);
1257c478bd9Sstevel@tonic-gate 		ph_data = ph_impl->usba_ph_data;
1267c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	return (ph_data);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate usb_pipe_handle_t
usba_get_pipe_handle(usba_pipe_handle_data_t * ph_data)1347c478bd9Sstevel@tonic-gate usba_get_pipe_handle(usba_pipe_handle_data_t *ph_data)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t ph = NULL;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (ph_data) {
1397c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
1407c478bd9Sstevel@tonic-gate 		ASSERT(ph_data->p_req_count >= 0);
1417c478bd9Sstevel@tonic-gate 		ph = (usb_pipe_handle_t)ph_data->p_ph_impl;
1427c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return (ph);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * opaque to pipe handle impl translation with incr of ref count. The caller
1517c478bd9Sstevel@tonic-gate  * must release ph_data when done. Increment the ref count ensures that
1527c478bd9Sstevel@tonic-gate  * the ph_data will not be freed underneath us.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate usba_pipe_handle_data_t *
usba_hold_ph_data(usb_pipe_handle_t pipe_handle)1557c478bd9Sstevel@tonic-gate usba_hold_ph_data(usb_pipe_handle_t pipe_handle)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = (usba_ph_impl_t *)pipe_handle;
1587c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = NULL;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (ph_impl) {
1617c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		switch (ph_impl->usba_ph_state) {
1647c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_IDLE:
1657c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_ACTIVE:
1667c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_ERROR:
1677c478bd9Sstevel@tonic-gate 			ph_data = ph_impl->usba_ph_data;
1687c478bd9Sstevel@tonic-gate 			ph_impl->usba_ph_ref_count++;
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_CLOSED:
1717c478bd9Sstevel@tonic-gate 		case USB_PIPE_STATE_CLOSING:
1727c478bd9Sstevel@tonic-gate 		default:
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1777c478bd9Sstevel@tonic-gate 		    "usba_hold_ph_data: ph_impl=0x%p state=%d ref=%d",
178112116d8Sfb 		    (void *)ph_impl, ph_impl->usba_ph_state,
1797c478bd9Sstevel@tonic-gate 		    ph_impl->usba_ph_ref_count);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return (ph_data);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate void
usba_release_ph_data(usba_ph_impl_t * ph_impl)1897c478bd9Sstevel@tonic-gate usba_release_ph_data(usba_ph_impl_t *ph_impl)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	if (ph_impl) {
1927c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
1957c478bd9Sstevel@tonic-gate 		    "usba_release_ph_data: "
1967c478bd9Sstevel@tonic-gate 		    "ph_impl=0x%p state=%d ref=%d",
197112116d8Sfb 		    (void *)ph_impl, ph_impl->usba_ph_state,
1987c478bd9Sstevel@tonic-gate 		    ph_impl->usba_ph_ref_count);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #ifndef __lock_lint
2017c478bd9Sstevel@tonic-gate 		if (ph_impl->usba_ph_data) {
2027c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
2037c478bd9Sstevel@tonic-gate 			    "usba_release_ph_data: req_count=%d",
2047c478bd9Sstevel@tonic-gate 			    ph_impl->usba_ph_data->p_req_count);
2057c478bd9Sstevel@tonic-gate 			ASSERT(ph_impl->usba_ph_data->p_req_count >= 0);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate #endif
2087c478bd9Sstevel@tonic-gate 		ph_impl->usba_ph_ref_count--;
2097c478bd9Sstevel@tonic-gate 		ASSERT(ph_impl->usba_ph_ref_count >= 0);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * get pipe state from ph_data
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate usb_pipe_state_t
usba_get_ph_state(usba_pipe_handle_data_t * ph_data)2207c478bd9Sstevel@tonic-gate usba_get_ph_state(usba_pipe_handle_data_t *ph_data)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
2237c478bd9Sstevel@tonic-gate 	usb_pipe_state_t	pipe_state;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ph_data->p_mutex));
2267c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
2277c478bd9Sstevel@tonic-gate 	pipe_state = ph_impl->usba_ph_state;
2287c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	return (pipe_state);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * get ref_count from ph_data
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate int
usba_get_ph_ref_count(usba_pipe_handle_data_t * ph_data)2387c478bd9Sstevel@tonic-gate usba_get_ph_ref_count(usba_pipe_handle_data_t *ph_data)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
2417c478bd9Sstevel@tonic-gate 	int			ref_count;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
2447c478bd9Sstevel@tonic-gate 	ref_count = ph_impl->usba_ph_ref_count;
2457c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	return (ref_count);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * new pipe state
2537c478bd9Sstevel@tonic-gate  * We need to hold both pipe mutex and ph_impl mutex
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate void
usba_pipe_new_state(usba_pipe_handle_data_t * ph_data,usb_pipe_state_t state)2567c478bd9Sstevel@tonic-gate usba_pipe_new_state(usba_pipe_handle_data_t *ph_data, usb_pipe_state_t state)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	usba_ph_impl_t *ph_impl = ph_data->p_ph_impl;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ph_data->p_mutex));
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
2637c478bd9Sstevel@tonic-gate 	ASSERT(ph_data->p_req_count >= 0);
2647c478bd9Sstevel@tonic-gate 	ASSERT(ph_impl->usba_ph_ref_count >= 0);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
2677c478bd9Sstevel@tonic-gate 	    "usba_pipe_new_state: "
2687c478bd9Sstevel@tonic-gate 	    "ph_data=0x%p old=%s new=%s ref=%d req=%d",
269112116d8Sfb 	    (void *)ph_data, usb_str_pipe_state(ph_impl->usba_ph_state),
2707c478bd9Sstevel@tonic-gate 	    usb_str_pipe_state(state),
2717c478bd9Sstevel@tonic-gate 	    ph_impl->usba_ph_ref_count, ph_data->p_req_count);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	switch (ph_impl->usba_ph_state) {
2747c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_IDLE:
2757c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_ACTIVE:
2767c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_ERROR:
2777c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_CLOSED:
2787c478bd9Sstevel@tonic-gate 		ph_impl->usba_ph_state = state;
2797c478bd9Sstevel@tonic-gate 		break;
2807c478bd9Sstevel@tonic-gate 	case USB_PIPE_STATE_CLOSING:
2817c478bd9Sstevel@tonic-gate 	default:
2827c478bd9Sstevel@tonic-gate 		break;
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * async function execution support
2907c478bd9Sstevel@tonic-gate  * Arguments:
2917c478bd9Sstevel@tonic-gate  *	dip		- devinfo pointer
2927c478bd9Sstevel@tonic-gate  *	sync_func	- function to be executed
2937c478bd9Sstevel@tonic-gate  *	ph_impl		- impl pipehandle
2947c478bd9Sstevel@tonic-gate  *	arg		- opaque arg
2957c478bd9Sstevel@tonic-gate  *	usb_flags	- none
2967c478bd9Sstevel@tonic-gate  *	callback	- function to be called on completion, may be NULL
2977c478bd9Sstevel@tonic-gate  *	callback_arg	- argument for callback function
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * Note: The caller must do a hold on ph_data
3007c478bd9Sstevel@tonic-gate  *	We sleep for memory resources and taskq_dispatch which will ensure
3017c478bd9Sstevel@tonic-gate  *	that this function succeeds
3027c478bd9Sstevel@tonic-gate  */
3037c478bd9Sstevel@tonic-gate int
usba_pipe_setup_func_call(dev_info_t * dip,int (* sync_func)(dev_info_t *,usba_ph_impl_t *,usba_pipe_async_req_t *,usb_flags_t),usba_ph_impl_t * ph_impl,usb_opaque_t arg,usb_flags_t usb_flags,void (* callback)(usb_pipe_handle_t,usb_opaque_t,int,usb_cb_flags_t),usb_opaque_t callback_arg)3047c478bd9Sstevel@tonic-gate usba_pipe_setup_func_call(
3057c478bd9Sstevel@tonic-gate 	dev_info_t	*dip,
3067c478bd9Sstevel@tonic-gate 	int		(*sync_func)(dev_info_t *,
3077c478bd9Sstevel@tonic-gate 			    usba_ph_impl_t *, usba_pipe_async_req_t *,
3087c478bd9Sstevel@tonic-gate 			    usb_flags_t),
3097c478bd9Sstevel@tonic-gate 	usba_ph_impl_t *ph_impl,
3107c478bd9Sstevel@tonic-gate 	usb_opaque_t	arg,
3117c478bd9Sstevel@tonic-gate 	usb_flags_t	usb_flags,
3127c478bd9Sstevel@tonic-gate 	void		(*callback)(usb_pipe_handle_t,
3137c478bd9Sstevel@tonic-gate 			    usb_opaque_t, int, usb_cb_flags_t),
3147c478bd9Sstevel@tonic-gate 	usb_opaque_t	callback_arg)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	usba_pipe_async_req_t	*request;
3177c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle = (usb_pipe_handle_t)ph_impl;
3187c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = ph_impl->usba_ph_data;
3197c478bd9Sstevel@tonic-gate 	int			rval = USB_SUCCESS;
3207c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		callback_flags;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(DPRINT_MASK_USBAI, usbai_log_handle,
3237c478bd9Sstevel@tonic-gate 	    "usba_pipe_setup_func_call: ph_impl=0x%p, func=0x%p",
324112116d8Sfb 	    (void *)ph_impl, (void *)sync_func);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (((usb_flags & USB_FLAGS_SLEEP) == 0) && (callback == NULL)) {
3277c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
328d291d9f2Sfrits 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
3297c478bd9Sstevel@tonic-gate 		    "usba_pipe_setup_func_call: async request with "
3307c478bd9Sstevel@tonic-gate 		    "no callback");
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		return (USB_INVALID_ARGS);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	request = kmem_zalloc(sizeof (usba_pipe_async_req_t), KM_SLEEP);
3367c478bd9Sstevel@tonic-gate 	request->dip		= dip;
3377c478bd9Sstevel@tonic-gate 	request->ph_impl	= ph_impl;
3387c478bd9Sstevel@tonic-gate 	request->arg		= arg;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * OR in sleep flag. regardless of calling sync_func directly
3427c478bd9Sstevel@tonic-gate 	 * or in a new thread, we will always wait for completion
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	request->usb_flags	= usb_flags | USB_FLAGS_SLEEP;
3457c478bd9Sstevel@tonic-gate 	request->sync_func	= sync_func;
3467c478bd9Sstevel@tonic-gate 	request->callback	= callback;
3477c478bd9Sstevel@tonic-gate 	request->callback_arg	= callback_arg;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (usb_flags & USB_FLAGS_SLEEP) {
3507c478bd9Sstevel@tonic-gate 		rval = sync_func(dip, ph_impl, request, usb_flags);
3517c478bd9Sstevel@tonic-gate 		kmem_free(request, sizeof (usba_pipe_async_req_t));
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	} else if (usba_async_ph_req(ph_data,
3547c478bd9Sstevel@tonic-gate 	    usba_pipe_do_async_func_thread,
3557c478bd9Sstevel@tonic-gate 	    (void *)request, USB_FLAGS_SLEEP) != USB_SUCCESS) {
3567c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
3577c478bd9Sstevel@tonic-gate 		    "usb_async_req failed: ph_impl=0x%p, func=0x%p",
358112116d8Sfb 		    (void *)ph_impl, (void *)sync_func);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		if (callback) {
3617c478bd9Sstevel@tonic-gate 			callback_flags =
3627c478bd9Sstevel@tonic-gate 			    usba_check_intr_context(USB_CB_ASYNC_REQ_FAILED);
3637c478bd9Sstevel@tonic-gate 			callback(pipe_handle, callback_arg, USB_FAILURE,
3647c478bd9Sstevel@tonic-gate 			    callback_flags);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		kmem_free(request, sizeof (usba_pipe_async_req_t));
3687c478bd9Sstevel@tonic-gate 		usba_release_ph_data(ph_impl);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	return (rval);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * taskq thread function to execute function synchronously
3777c478bd9Sstevel@tonic-gate  * Note: caller must have done a hold on ph_data
3787c478bd9Sstevel@tonic-gate  */
3797c478bd9Sstevel@tonic-gate static void
usba_pipe_do_async_func_thread(void * arg)3807c478bd9Sstevel@tonic-gate usba_pipe_do_async_func_thread(void *arg)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	usba_pipe_async_req_t	*request = (usba_pipe_async_req_t *)arg;
3837c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = request->ph_impl;
3847c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle = (usb_pipe_handle_t)ph_impl;
3857c478bd9Sstevel@tonic-gate 	int			rval;
3867c478bd9Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags = USB_CB_NO_INFO;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if ((rval = request->sync_func(request->dip, ph_impl,
3897c478bd9Sstevel@tonic-gate 	    request, request->usb_flags | USB_FLAGS_SLEEP)) !=
3907c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
3917c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
3927c478bd9Sstevel@tonic-gate 		    "sync func failed (%d)", rval);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if (request->callback) {
3967c478bd9Sstevel@tonic-gate 		request->callback(pipe_handle, request->callback_arg, rval,
3977c478bd9Sstevel@tonic-gate 		    cb_flags);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	kmem_free(request, sizeof (usba_pipe_async_req_t));
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * default endpoint descriptor and pipe policy
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate usb_ep_descr_t	usba_default_ep_descr =
4087c478bd9Sstevel@tonic-gate 	{7, 5, 0, USB_EP_ATTR_CONTROL, 8, 0};
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /* set some meaningful defaults */
4117c478bd9Sstevel@tonic-gate static usb_pipe_policy_t usba_default_ep_pipe_policy = {3};
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * usb_get_ep_index: create an index from endpoint address that can
4167c478bd9Sstevel@tonic-gate  * be used to index into endpoint pipe lists
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate uchar_t
usb_get_ep_index(uint8_t ep_addr)4197c478bd9Sstevel@tonic-gate usb_get_ep_index(uint8_t ep_addr)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	return ((ep_addr & USB_EP_NUM_MASK) +
4227c478bd9Sstevel@tonic-gate 	    ((ep_addr & USB_EP_DIR_MASK) ? 16 : 0));
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * pipe management
4287c478bd9Sstevel@tonic-gate  *	utility functions to init and destroy a pipehandle
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate static int
usba_init_pipe_handle(dev_info_t * dip,usba_device_t * usba_device,usb_ep_descr_t * ep,usb_ep_xdescr_t * ep_xdescr,usb_pipe_policy_t * pipe_policy,usba_ph_impl_t * ph_impl)4317c478bd9Sstevel@tonic-gate usba_init_pipe_handle(dev_info_t *dip,
4327c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device,
4337c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*ep,
434993e3fafSRobert Mustacchi 	usb_ep_xdescr_t		*ep_xdescr,
4357c478bd9Sstevel@tonic-gate 	usb_pipe_policy_t	*pipe_policy,
4367c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl)
4377c478bd9Sstevel@tonic-gate {
438993e3fafSRobert Mustacchi 	usb_ep_xdescr_t xep;
4397c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4407c478bd9Sstevel@tonic-gate 	unsigned int def_instance = instance;
4417c478bd9Sstevel@tonic-gate 	static unsigned int anon_instance = 0;
4427c478bd9Sstevel@tonic-gate 	char tq_name[TASKQ_NAMELEN];
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data = ph_impl->usba_ph_data;
4457c478bd9Sstevel@tonic-gate 	ddi_iblock_cookie_t	iblock_cookie =
4467c478bd9Sstevel@tonic-gate 	    usba_hcdi_get_hcdi(usba_device->usb_root_hub_dip)->
4477c478bd9Sstevel@tonic-gate 	    hcdi_iblock_cookie;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
4507c478bd9Sstevel@tonic-gate 	    "usba_init_pipe_handle: "
451112116d8Sfb 	    "usba_device=0x%p ep=0x%x", (void *)usba_device,
452112116d8Sfb 	    ep->bEndpointAddress);
4537c478bd9Sstevel@tonic-gate 	mutex_init(&ph_data->p_mutex, NULL, MUTEX_DRIVER, iblock_cookie);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* just to keep warlock happy, there is no contention yet */
4567c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
4577c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	ASSERT(pipe_policy->pp_max_async_reqs);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if (instance != -1) {
4627c478bd9Sstevel@tonic-gate 		(void) snprintf(tq_name, sizeof (tq_name),
4637c478bd9Sstevel@tonic-gate 		    "USB_%s_%x_pipehndl_tq_%d",
4647c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ep->bEndpointAddress, instance);
4657c478bd9Sstevel@tonic-gate 	} else {
4661a5e258fSJosef 'Jeff' Sipek 		def_instance = atomic_inc_32_nv(&anon_instance);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		(void) snprintf(tq_name, sizeof (tq_name),
4697c478bd9Sstevel@tonic-gate 		    "USB_%s_%x_pipehndl_tq_%d_",
4707c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ep->bEndpointAddress, def_instance);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	ph_data->p_taskq = taskq_create(tq_name,
474112116d8Sfb 	    pipe_policy->pp_max_async_reqs + 1,
475112116d8Sfb 	    ((ep->bmAttributes & USB_EP_ATTR_MASK) ==
476112116d8Sfb 	    USB_EP_ATTR_ISOCH) ?
477112116d8Sfb 	    (maxclsyspri - 5) : minclsyspri,
478112116d8Sfb 	    2 * (pipe_policy->pp_max_async_reqs + 1),
479112116d8Sfb 	    8 * (pipe_policy->pp_max_async_reqs + 1),
480112116d8Sfb 	    TASKQ_PREPOPULATE);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	/*
4837c478bd9Sstevel@tonic-gate 	 * Create a shared taskq.
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 	if (ph_data->p_spec_flag & USBA_PH_FLAG_TQ_SHARE) {
4867c478bd9Sstevel@tonic-gate 		int iface = usb_get_if_number(dip);
4877c478bd9Sstevel@tonic-gate 		if (iface < 0) {
4887c478bd9Sstevel@tonic-gate 			/* we own the device, use first entry */
4897c478bd9Sstevel@tonic-gate 			iface = 0;
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		if (instance != -1) {
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 			    instance);
4977c478bd9Sstevel@tonic-gate 		} else {
4987c478bd9Sstevel@tonic-gate 			(void) snprintf(tq_name, sizeof (tq_name),
4997c478bd9Sstevel@tonic-gate 			    "USB_%s_%x_shared_tq_%d_",
5007c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ep->bEndpointAddress,
5017c478bd9Sstevel@tonic-gate 			    def_instance);
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		if (usba_device->usb_shared_taskq_ref_count[iface] == 0) {
5057c478bd9Sstevel@tonic-gate 			usba_device->usb_shared_taskq[iface] =
5067c478bd9Sstevel@tonic-gate 			    taskq_create(tq_name,
5077c478bd9Sstevel@tonic-gate 			    1,				/* Number threads. */
5087c478bd9Sstevel@tonic-gate 			    maxclsyspri - 5,		/* Priority */
5097c478bd9Sstevel@tonic-gate 			    1,				/* minalloc */
5107c478bd9Sstevel@tonic-gate 			    USBA_N_ENDPOINTS + 4,	/* maxalloc */
5117c478bd9Sstevel@tonic-gate 			    TASKQ_PREPOPULATE);
5127c478bd9Sstevel@tonic-gate 			ASSERT(usba_device->usb_shared_taskq[iface] != NULL);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 		usba_device->usb_shared_taskq_ref_count[iface]++;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
517993e3fafSRobert Mustacchi 	/*
518993e3fafSRobert Mustacchi 	 * In the future, when we may have different versions of the extended
519993e3fafSRobert Mustacchi 	 * endpoint descriptor, they should be normalized to the current version
520993e3fafSRobert Mustacchi 	 * here such that all of the HCI drivers have a consistent view of the
521993e3fafSRobert Mustacchi 	 * world. The extended descriptor may be NULL if we are opening the
522993e3fafSRobert Mustacchi 	 * default control endpoint; however, we create a uniform view for the
523993e3fafSRobert Mustacchi 	 * HCI drivers.
524993e3fafSRobert Mustacchi 	 */
525993e3fafSRobert Mustacchi 	if (ep_xdescr == NULL) {
526993e3fafSRobert Mustacchi 		bzero(&xep, sizeof (usb_ep_xdescr_t));
527993e3fafSRobert Mustacchi 		xep.uex_version = USB_EP_XDESCR_CURRENT_VERSION;
528993e3fafSRobert Mustacchi 		xep.uex_ep = *ep;
529993e3fafSRobert Mustacchi 		ep_xdescr = &xep;
530993e3fafSRobert Mustacchi 	}
531993e3fafSRobert Mustacchi 
5327c478bd9Sstevel@tonic-gate 	ph_data->p_dip		= dip;
5337c478bd9Sstevel@tonic-gate 	ph_data->p_usba_device	= usba_device;
5347c478bd9Sstevel@tonic-gate 	ph_data->p_ep		= *ep;
535993e3fafSRobert Mustacchi 	ph_data->p_xep		= *ep_xdescr;
5367c478bd9Sstevel@tonic-gate 	ph_data->p_ph_impl	= ph_impl;
5377c478bd9Sstevel@tonic-gate 	if ((ep->bmAttributes & USB_EP_ATTR_MASK) ==
5387c478bd9Sstevel@tonic-gate 	    USB_EP_ATTR_ISOCH) {
5397c478bd9Sstevel@tonic-gate 		ph_data->p_spec_flag |= USBA_PH_FLAG_USE_SOFT_INTR;
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* fix up the MaxPacketSize if it is the default endpoint descr */
543*0d2006e4SRobert Mustacchi 	if (ep == &usba_default_ep_descr) {
544ff0e937bSRaymond Chen 		uint16_t	maxpktsize;
545ff0e937bSRaymond Chen 
546ff0e937bSRaymond Chen 		maxpktsize = usba_device->usb_dev_descr->bMaxPacketSize0;
5477c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(DPRINT_MASK_USBAI, usbai_log_handle,
5487c478bd9Sstevel@tonic-gate 		    "adjusting max packet size from %d to %d",
549ff0e937bSRaymond Chen 		    ph_data->p_ep.wMaxPacketSize, maxpktsize);
5507c478bd9Sstevel@tonic-gate 
551ff0e937bSRaymond Chen 		ph_data->p_ep.wMaxPacketSize = maxpktsize;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/* now update usba_ph_impl structure */
5557c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
5567c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_dip = dip;
5577c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_ep = ph_data->p_ep;
5587c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_policy = ph_data->p_policy = *pipe_policy;
5597c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	usba_init_list(&ph_data->p_queue, (usb_opaque_t)ph_data, iblock_cookie);
5627c478bd9Sstevel@tonic-gate 	usba_init_list(&ph_data->p_cb_queue, (usb_opaque_t)ph_data,
563112116d8Sfb 	    iblock_cookie);
5647c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
5657c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate static void
usba_taskq_destroy(void * arg)5727c478bd9Sstevel@tonic-gate usba_taskq_destroy(void *arg)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	taskq_destroy((taskq_t *)arg);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate static void
usba_destroy_pipe_handle(usba_pipe_handle_data_t * ph_data)5797c478bd9Sstevel@tonic-gate usba_destroy_pipe_handle(usba_pipe_handle_data_t *ph_data)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
5827c478bd9Sstevel@tonic-gate 	int			timeout;
5837c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
586112116d8Sfb 	    "usba_destroy_pipe_handle: ph_data=0x%p", (void *)ph_data);
5877c478bd9Sstevel@tonic-gate 
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 	/* check for all activity to drain */
5927c478bd9Sstevel@tonic-gate 	for (timeout = 0; timeout < usba_drain_timeout; timeout++) {
5937c478bd9Sstevel@tonic-gate 		if ((ph_impl->usba_ph_ref_count <= 1) &&
5947c478bd9Sstevel@tonic-gate 		    (ph_data->p_req_count == 0)) {
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 			break;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
5997c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
6007c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(1000));
6017c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
6027c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/*
6067c478bd9Sstevel@tonic-gate 	 * set state to closed here so any other thread
6077c478bd9Sstevel@tonic-gate 	 * that is waiting for the CLOSED state will
6087c478bd9Sstevel@tonic-gate 	 * continue. Otherwise, taskq_destroy might deadlock
6097c478bd9Sstevel@tonic-gate 	 */
6107c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_data = NULL;
6117c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_ref_count = 0;
6127c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_state = USB_PIPE_STATE_CLOSED;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (ph_data->p_taskq) {
6157c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
6167c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
6177c478bd9Sstevel@tonic-gate 		if (taskq_member(ph_data->p_taskq, curthread)) {
6187c478bd9Sstevel@tonic-gate 			/*
6197c478bd9Sstevel@tonic-gate 			 * use system taskq to destroy ph's taskq to avoid
6207c478bd9Sstevel@tonic-gate 			 * deadlock
6217c478bd9Sstevel@tonic-gate 			 */
6227c478bd9Sstevel@tonic-gate 			(void) taskq_dispatch(system_taskq,
6237c478bd9Sstevel@tonic-gate 			    usba_taskq_destroy, ph_data->p_taskq, TQ_SLEEP);
6247c478bd9Sstevel@tonic-gate 		} else {
6257c478bd9Sstevel@tonic-gate 			taskq_destroy(ph_data->p_taskq);
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 	} else {
6287c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
6297c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	usba_device = ph_data->p_usba_device;
6337c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
6347c478bd9Sstevel@tonic-gate 	if (ph_data->p_spec_flag & USBA_PH_FLAG_TQ_SHARE) {
6357c478bd9Sstevel@tonic-gate 		int iface = usb_get_if_number(ph_data->p_dip);
6367c478bd9Sstevel@tonic-gate 		if (iface < 0) {
6377c478bd9Sstevel@tonic-gate 			/* we own the device, use the first entry */
6387c478bd9Sstevel@tonic-gate 			iface = 0;
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 		mutex_enter(&usba_device->usb_mutex);
6417c478bd9Sstevel@tonic-gate 		if (--usba_device->usb_shared_taskq_ref_count[iface] == 0) {
6427c478bd9Sstevel@tonic-gate 			ph_data->p_spec_flag &= ~USBA_PH_FLAG_TQ_SHARE;
6437c478bd9Sstevel@tonic-gate 			if (taskq_member(usba_device->usb_shared_taskq[iface],
6447c478bd9Sstevel@tonic-gate 			    curthread)) {
6457c478bd9Sstevel@tonic-gate 				(void) taskq_dispatch(
6467c478bd9Sstevel@tonic-gate 				    system_taskq,
6477c478bd9Sstevel@tonic-gate 				    usba_taskq_destroy,
6487c478bd9Sstevel@tonic-gate 				    usba_device->usb_shared_taskq[iface],
6497c478bd9Sstevel@tonic-gate 				    TQ_SLEEP);
6507c478bd9Sstevel@tonic-gate 			} else {
6517c478bd9Sstevel@tonic-gate 				taskq_destroy(
6527c478bd9Sstevel@tonic-gate 				    usba_device->usb_shared_taskq[iface]);
6537c478bd9Sstevel@tonic-gate 			}
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
661112116d8Sfb 	    "usba_destroy_pipe_handle: destroying ph_data=0x%p",
662112116d8Sfb 	    (void *)ph_data);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	usba_destroy_list(&ph_data->p_queue);
6657c478bd9Sstevel@tonic-gate 	usba_destroy_list(&ph_data->p_cb_queue);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/* destroy mutexes */
6687c478bd9Sstevel@tonic-gate 	mutex_destroy(&ph_data->p_mutex);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	kmem_free(ph_data, sizeof (usba_pipe_handle_data_t));
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate /*
6757c478bd9Sstevel@tonic-gate  * usba_drain_cbs:
6767c478bd9Sstevel@tonic-gate  *	Drain the request callbacks on the pipe handle
6777c478bd9Sstevel@tonic-gate  */
6787c478bd9Sstevel@tonic-gate int
usba_drain_cbs(usba_pipe_handle_data_t * ph_data,usb_cb_flags_t cb_flags,usb_cr_t cr)6797c478bd9Sstevel@tonic-gate usba_drain_cbs(usba_pipe_handle_data_t *ph_data, usb_cb_flags_t cb_flags,
6807c478bd9Sstevel@tonic-gate 	usb_cr_t cr)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp;
6837c478bd9Sstevel@tonic-gate 	int			flush_requests = 1;
6847c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
6857c478bd9Sstevel@tonic-gate 	int			timeout;
6867c478bd9Sstevel@tonic-gate 	int			rval = USB_SUCCESS;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ph_data->p_mutex));
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
6917c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
6927c478bd9Sstevel@tonic-gate 	    "usba_drain_cbs: ph_data=0x%p ref=%d req=%d cb=0x%x cr=%d",
693112116d8Sfb 	    (void *)ph_data, ph_impl->usba_ph_ref_count, ph_data->p_req_count,
6947c478bd9Sstevel@tonic-gate 	    cb_flags, cr);
6957c478bd9Sstevel@tonic-gate 	ASSERT(ph_data->p_req_count >= 0);
6967c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (ph_data->p_dip) {
6997c478bd9Sstevel@tonic-gate 		if (USBA_IS_DEFAULT_PIPE(ph_data)) {
7007c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(DPRINT_MASK_USBAI,
7017c478bd9Sstevel@tonic-gate 			    usbai_log_handle,
7027c478bd9Sstevel@tonic-gate 			    "no flushing on default pipe!");
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 			flush_requests = 0;
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (flush_requests) {
7097c478bd9Sstevel@tonic-gate 		/* flush all requests in the pipehandle queue */
7107c478bd9Sstevel@tonic-gate 		while ((req_wrp = (usba_req_wrapper_t *)
7117c478bd9Sstevel@tonic-gate 		    usba_rm_first_pvt_from_list(&ph_data->p_queue)) != NULL) {
7127c478bd9Sstevel@tonic-gate 			mutex_exit(&ph_data->p_mutex);
7137c478bd9Sstevel@tonic-gate 			usba_do_req_exc_cb(req_wrp, cr, cb_flags);
7147c478bd9Sstevel@tonic-gate 			mutex_enter(&ph_data->p_mutex);
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * wait for any callbacks in progress but don't wait for
7207c478bd9Sstevel@tonic-gate 	 * for queued requests on the default pipe
7217c478bd9Sstevel@tonic-gate 	 */
7227c478bd9Sstevel@tonic-gate 	for (timeout = 0; (timeout < usba_drain_timeout) &&
7237c478bd9Sstevel@tonic-gate 	    (ph_data->p_req_count >
7247c478bd9Sstevel@tonic-gate 	    usba_list_entry_count(&ph_data->p_queue));
7257c478bd9Sstevel@tonic-gate 	    timeout++) {
7267c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
7277c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(1000));
7287c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
7327c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
7337c478bd9Sstevel@tonic-gate 	    "usba_drain_cbs done: ph_data=0x%p ref=%d req=%d",
734112116d8Sfb 	    (void *)ph_data, ph_impl->usba_ph_ref_count, ph_data->p_req_count);
7357c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	if (timeout == usba_drain_timeout) {
7387c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
7397c478bd9Sstevel@tonic-gate 		    "draining callbacks timed out!");
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		rval = USB_FAILURE;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	return (rval);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate  * usb_pipe_open():
7507c478bd9Sstevel@tonic-gate  *
7517c478bd9Sstevel@tonic-gate  * Before using any pipe including the default pipe, it should be opened
7527c478bd9Sstevel@tonic-gate  * using usb_pipe_open(). On a successful open, a pipe handle is returned
7537c478bd9Sstevel@tonic-gate  * for use in other usb_pipe_*() functions
7547c478bd9Sstevel@tonic-gate  *
7557c478bd9Sstevel@tonic-gate  * The default pipe can only be opened by the hub driver
7567c478bd9Sstevel@tonic-gate  *
7577c478bd9Sstevel@tonic-gate  * The bandwidth has been allocated and guaranteed on successful
7587c478bd9Sstevel@tonic-gate  * opening of an isoc/intr pipes.
7597c478bd9Sstevel@tonic-gate  *
7607c478bd9Sstevel@tonic-gate  * Only the default pipe can be shared. all other control pipes
7617c478bd9Sstevel@tonic-gate  * are excusively opened by default.
7627c478bd9Sstevel@tonic-gate  * A pipe policy and endpoint descriptor must always be provided
7637c478bd9Sstevel@tonic-gate  * except for default pipe
7647c478bd9Sstevel@tonic-gate  *
7657c478bd9Sstevel@tonic-gate  * Arguments:
7667c478bd9Sstevel@tonic-gate  *	dip		- devinfo ptr
7677c478bd9Sstevel@tonic-gate  *	ep		- endpoint descriptor pointer
7687c478bd9Sstevel@tonic-gate  *	pipe_policy	- pointer to pipe policy which provides hints on how
7697c478bd9Sstevel@tonic-gate  *			  the pipe will be used.
7707c478bd9Sstevel@tonic-gate  *	flags		- USB_FLAGS_SLEEP wait for resources
7717c478bd9Sstevel@tonic-gate  *			  to become available
7727c478bd9Sstevel@tonic-gate  *	pipe_handle	- a pipe handle pointer. On a successful open,
7737c478bd9Sstevel@tonic-gate  *			  a pipe_handle is returned in this pointer.
7747c478bd9Sstevel@tonic-gate  *
7757c478bd9Sstevel@tonic-gate  * Return values:
7767c478bd9Sstevel@tonic-gate  *	USB_SUCCESS	 - open succeeded
7777c478bd9Sstevel@tonic-gate  *	USB_FAILURE	 - unspecified open failure or pipe is already open
7787c478bd9Sstevel@tonic-gate  *	USB_NO_RESOURCES - no resources were available to complete the open
7797c478bd9Sstevel@tonic-gate  *	USB_NO_BANDWIDTH - no bandwidth available (isoc/intr pipes)
7807c478bd9Sstevel@tonic-gate  *	USB_*		 - refer to usbai.h
7817c478bd9Sstevel@tonic-gate  */
7827c478bd9Sstevel@tonic-gate int
usb_pipe_xopen(dev_info_t * dip,usb_ep_xdescr_t * ep_xdesc,usb_pipe_policy_t * pipe_policy,usb_flags_t usb_flags,usb_pipe_handle_t * pipe_handle)783993e3fafSRobert Mustacchi usb_pipe_xopen(
7847c478bd9Sstevel@tonic-gate 	dev_info_t		*dip,
785993e3fafSRobert Mustacchi 	usb_ep_xdescr_t		*ep_xdesc,
7867c478bd9Sstevel@tonic-gate 	usb_pipe_policy_t	*pipe_policy,
7877c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags,
7887c478bd9Sstevel@tonic-gate 	usb_pipe_handle_t	*pipe_handle)
7897c478bd9Sstevel@tonic-gate {
790993e3fafSRobert Mustacchi 	usb_ep_descr_t		*ep;
7917c478bd9Sstevel@tonic-gate 	usba_device_t		*usba_device;
7927c478bd9Sstevel@tonic-gate 	int			rval;
7937c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *ph_data;
7947c478bd9Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl;
7957c478bd9Sstevel@tonic-gate 	uchar_t			ep_index;
7967c478bd9Sstevel@tonic-gate 	int			kmflag;
7977c478bd9Sstevel@tonic-gate 	size_t			size;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
8007c478bd9Sstevel@tonic-gate 	    "usb_pipe_open:\n\t"
801993e3fafSRobert Mustacchi 	    "dip=0x%p ep_xdesc=0x%p pp=0x%p uf=0x%x ph=0x%p",
802993e3fafSRobert Mustacchi 	    (void *)dip, (void *)ep_xdesc, (void *)pipe_policy, usb_flags,
803112116d8Sfb 	    (void *)pipe_handle);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (pipe_handle == NULL)) {
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		return (USB_INVALID_ARGS);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
810993e3fafSRobert Mustacchi 	if ((ep_xdesc != NULL) &&
811993e3fafSRobert Mustacchi 	    ((ep_xdesc->uex_version != USB_EP_XDESCR_CURRENT_VERSION) ||
812993e3fafSRobert Mustacchi 	    ((ep_xdesc->uex_flags & ~USB_EP_XFLAGS_SS_COMP) != 0))) {
813993e3fafSRobert Mustacchi 
814993e3fafSRobert Mustacchi 		return (USB_INVALID_ARGS);
815993e3fafSRobert Mustacchi 	}
816993e3fafSRobert Mustacchi 
8177c478bd9Sstevel@tonic-gate 	if (servicing_interrupt() && (usb_flags & USB_FLAGS_SLEEP)) {
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		return (USB_INVALID_CONTEXT);
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 	usba_device = usba_get_usba_device(dip);
8227c478bd9Sstevel@tonic-gate 
823993e3fafSRobert Mustacchi 	/*
824993e3fafSRobert Mustacchi 	 * Check the device's speed. If we're being asked to open anything other
825993e3fafSRobert Mustacchi 	 * than the default endpoint and the device is superspeed or greater and
826993e3fafSRobert Mustacchi 	 * we only have a usb_ep_descr_t and not the full endpoint data, then
827993e3fafSRobert Mustacchi 	 * this was coming through usb_pipe_open() and we need to fail this
828993e3fafSRobert Mustacchi 	 * call.
829993e3fafSRobert Mustacchi 	 *
830993e3fafSRobert Mustacchi 	 * Some drivers technically cheat and open the default control endpoint
831993e3fafSRobert Mustacchi 	 * even though they're not supposed to. ugen appears to be the main
832993e3fafSRobert Mustacchi 	 * offender. To deal with this, we check to see if the endpoint
833993e3fafSRobert Mustacchi 	 * descriptor bcmps to our default and give them a break, since we don't
834993e3fafSRobert Mustacchi 	 * need extended info for default control endpoints.
835993e3fafSRobert Mustacchi 	 */
836993e3fafSRobert Mustacchi 	if (ep_xdesc != NULL && ep_xdesc->uex_flags == 0 &&
837993e3fafSRobert Mustacchi 	    bcmp(&ep_xdesc->uex_ep, &usba_default_ep_descr,
838993e3fafSRobert Mustacchi 	    sizeof (usb_ep_descr_t)) != 0 &&
839993e3fafSRobert Mustacchi 	    usba_device->usb_port_status >= USBA_SUPER_SPEED_DEV) {
840993e3fafSRobert Mustacchi 		const char *dname = ddi_driver_name(dip);
841993e3fafSRobert Mustacchi 		const char *prod, *mfg;
842993e3fafSRobert Mustacchi 
843993e3fafSRobert Mustacchi 		prod = usba_device->usb_product_str;
844993e3fafSRobert Mustacchi 		if (prod == NULL)
845993e3fafSRobert Mustacchi 			prod = "Unknown Device";
846993e3fafSRobert Mustacchi 		mfg = usba_device->usb_mfg_str;
847993e3fafSRobert Mustacchi 		if (mfg == NULL)
848993e3fafSRobert Mustacchi 			mfg = "Unknown Manufacturer";
849993e3fafSRobert Mustacchi 		cmn_err(CE_NOTE, "driver %s attempting to open non-default "
850993e3fafSRobert Mustacchi 		    "of a USB 3.0 or newer device through usb_pipe_open(). "
851993e3fafSRobert Mustacchi 		    "%s must be updated to use usb_pipe_xopen() to work with "
852993e3fafSRobert Mustacchi 		    "USB device %s %s.", dname, dname, mfg, prod);
853993e3fafSRobert Mustacchi 		return (USB_FAILURE);
854993e3fafSRobert Mustacchi 	}
855993e3fafSRobert Mustacchi 
856993e3fafSRobert Mustacchi 	if ((ep_xdesc != NULL) && (pipe_policy == NULL)) {
8577c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8587c478bd9Sstevel@tonic-gate 		    "usb_pipe_open: null pipe policy");
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		return (USB_INVALID_ARGS);
8617c478bd9Sstevel@tonic-gate 	}
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	/* is the device still connected? */
864993e3fafSRobert Mustacchi 	if ((ep_xdesc != NULL) & DEVI_IS_DEVICE_REMOVED(dip)) {
8657c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8667c478bd9Sstevel@tonic-gate 		    "usb_pipe_open: device has been removed");
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/*
8737c478bd9Sstevel@tonic-gate 	 * if a null endpoint pointer was passed, use the default
8747c478bd9Sstevel@tonic-gate 	 * endpoint descriptor
8757c478bd9Sstevel@tonic-gate 	 */
876993e3fafSRobert Mustacchi 	if (ep_xdesc == NULL) {
8777c478bd9Sstevel@tonic-gate 		if ((usb_flags & USBA_FLAGS_PRIVILEGED) == 0) {
8787c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8797c478bd9Sstevel@tonic-gate 			    "usb_pipe_open: not allowed to open def pipe");
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 			return (USB_INVALID_PERM);
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		ep = &usba_default_ep_descr;
8857c478bd9Sstevel@tonic-gate 		pipe_policy = &usba_default_ep_pipe_policy;
886993e3fafSRobert Mustacchi 	} else {
887993e3fafSRobert Mustacchi 		ep = &ep_xdesc->uex_ep;
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (usb_flags & USB_FLAGS_SERIALIZED_CB) {
8917c478bd9Sstevel@tonic-gate 		if (((ep->bmAttributes & USB_EP_ATTR_MASK) ==
8927c478bd9Sstevel@tonic-gate 		    USB_EP_ATTR_CONTROL) ||
8937c478bd9Sstevel@tonic-gate 		    ((ep->bmAttributes & USB_EP_ATTR_MASK) ==
8947c478bd9Sstevel@tonic-gate 		    USB_EP_ATTR_ISOCH)) {
8957c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
8967c478bd9Sstevel@tonic-gate 			    "usb_pipe_open: shared taskq not allowed with "
8977c478bd9Sstevel@tonic-gate 			    "ctrl or isoch pipe");
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 			return (USB_INVALID_ARGS);
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	kmflag	= (usb_flags & USB_FLAGS_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
9047c478bd9Sstevel@tonic-gate 	size	= sizeof (usba_pipe_handle_data_t);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if ((ph_data = kmem_zalloc(size, kmflag)) == NULL) {
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/* check if pipe is already open and if so fail */
9127c478bd9Sstevel@tonic-gate 	ep_index = usb_get_ep_index(ep->bEndpointAddress);
9137c478bd9Sstevel@tonic-gate 	ph_impl = &usba_device->usb_ph_list[ep_index];
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
9167c478bd9Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (ph_impl->usba_ph_data) {
9197c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
9207c478bd9Sstevel@tonic-gate 		    "usb_pipe_open: pipe to ep %d already open", ep_index);
9217c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
9227c478bd9Sstevel@tonic-gate 		mutex_exit(&usba_device->usb_mutex);
9237c478bd9Sstevel@tonic-gate 		kmem_free(ph_data, size);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		return (USB_BUSY);
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	ph_impl->usba_ph_data = ph_data;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
9317c478bd9Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	if (usb_flags & USB_FLAGS_SERIALIZED_CB) {
9347c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
9357c478bd9Sstevel@tonic-gate 		ph_data->p_spec_flag |= USBA_PH_FLAG_TQ_SHARE;
9367c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	/*
9407c478bd9Sstevel@tonic-gate 	 * allocate and initialize the pipe handle
9417c478bd9Sstevel@tonic-gate 	 */
9427c478bd9Sstevel@tonic-gate 	if ((rval = usba_init_pipe_handle(dip, usba_device,
943993e3fafSRobert Mustacchi 	    ep, ep_xdesc, pipe_policy, ph_impl)) != USB_SUCCESS) {
9447c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, usbai_log_handle,
9457c478bd9Sstevel@tonic-gate 		    "usb_pipe_open: pipe init failed (%d)", rval);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		return (rval);
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 	ph_data = ph_impl->usba_ph_data;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * ask the hcd to open the pipe
9537c478bd9Sstevel@tonic-gate 	 */
9547c478bd9Sstevel@tonic-gate 	if ((rval = usba_device->usb_hcdi_ops->usba_hcdi_pipe_open(ph_data,
9557c478bd9Sstevel@tonic-gate 	    usb_flags)) != USB_SUCCESS) {
9567c478bd9Sstevel@tonic-gate 		usba_destroy_pipe_handle(ph_data);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		*pipe_handle = NULL;
9597c478bd9Sstevel@tonic-gate 	} else {
9607c478bd9Sstevel@tonic-gate 		*pipe_handle = (usb_pipe_handle_t)ph_impl;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 		/* set the pipe state after a successful hcd open */
9637c478bd9Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
9647c478bd9Sstevel@tonic-gate 		usba_pipe_new_state(ph_data, USB_PIPE_STATE_IDLE);
9657c478bd9Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_USBAI, usbai_log_handle,
969112116d8Sfb 	    "usb_pipe_open: ph_impl=0x%p (0x%p)",
970112116d8Sfb 	    (void *)ph_impl, (void *)ph_data);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	return (rval);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
975993e3fafSRobert Mustacchi int
usb_pipe_open(dev_info_t * dip,usb_ep_descr_t * ep,usb_pipe_policy_t * pipe_policy,usb_flags_t usb_flags,usb_pipe_handle_t * pipe_handle)976993e3fafSRobert Mustacchi usb_pipe_open(
977993e3fafSRobert Mustacchi 	dev_info_t		*dip,
978993e3fafSRobert Mustacchi 	usb_ep_descr_t		*ep,
979993e3fafSRobert Mustacchi 	usb_pipe_policy_t	*pipe_policy,
980993e3fafSRobert Mustacchi 	usb_flags_t		usb_flags,
981993e3fafSRobert Mustacchi 	usb_pipe_handle_t	*pipe_handle)
982993e3fafSRobert Mustacchi {
983993e3fafSRobert Mustacchi 	usb_ep_xdescr_t xdesc, *xp = NULL;
984993e3fafSRobert Mustacchi 
985993e3fafSRobert Mustacchi 	/*
986993e3fafSRobert Mustacchi 	 * ep may be NULL if trying to open the default control endpoint.
987993e3fafSRobert Mustacchi 	 */
988993e3fafSRobert Mustacchi 	if (ep != NULL) {
989993e3fafSRobert Mustacchi 		bzero(&xdesc, sizeof (usb_ep_xdescr_t));
990993e3fafSRobert Mustacchi 		xdesc.uex_version = USB_EP_XDESCR_CURRENT_VERSION;
991993e3fafSRobert Mustacchi 		xdesc.uex_ep = *ep;
992993e3fafSRobert Mustacchi 		xp = &xdesc;
993993e3fafSRobert Mustacchi 	}
994993e3fafSRobert Mustacchi 
995993e3fafSRobert Mustacchi 	return (usb_pipe_xopen(dip, xp, pipe_policy, usb_flags,
996993e3fafSRobert Mustacchi 	    pipe_handle));
997993e3fafSRobert Mustacchi }
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate /*
10007c478bd9Sstevel@tonic-gate  * usb_pipe_close/sync_close:
10017c478bd9Sstevel@tonic-gate  *
10027c478bd9Sstevel@tonic-gate  * Close a pipe and release all resources and free the pipe_handle.
10037c478bd9Sstevel@tonic-gate  * Automatic polling, if active,  will be terminated
10047c478bd9Sstevel@tonic-gate  *
10057c478bd9Sstevel@tonic-gate  * Arguments:
10067c478bd9Sstevel@tonic-gate  *	dip		- devinfo ptr
10077c478bd9Sstevel@tonic-gate  *	pipehandle	- pointer to pipehandle. The pipehandle will be
10087c478bd9Sstevel@tonic-gate  *			  zeroed on successful completion
10097c478bd9Sstevel@tonic-gate  *	flags		- USB_FLAGS_SLEEP:
10107c478bd9Sstevel@tonic-gate  *				wait for resources, pipe
10117c478bd9Sstevel@tonic-gate  *				to become free, all callbacks completed
10127c478bd9Sstevel@tonic-gate  *	callback	- If USB_FLAGS_SLEEP has not been specified, a
10137c478bd9Sstevel@tonic-gate  *			  callback will be performed.
10147c478bd9Sstevel@tonic-gate  *	callback_arg	- the first argument of the callback. Note that
10157c478bd9Sstevel@tonic-gate  *			  the pipehandle will be zeroed and not passed
10167c478bd9Sstevel@tonic-gate  *
10177c478bd9Sstevel@tonic-gate  * Notes: