xref: /onnv-gate/usr/src/uts/common/io/usb/usba/hcdi.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  * USBA: Solaris USB Architecture support
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  * hcdi.c contains the code for client driver callbacks.  A host controller
32*0Sstevel@tonic-gate  * driver registers/unregisters with usba through usba_hcdi_register/unregister.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * When the transfer has finished, the host controller driver will call into
35*0Sstevel@tonic-gate  * usba with the result.  The call is usba_hcdi_cb().
36*0Sstevel@tonic-gate  *
37*0Sstevel@tonic-gate  * The callback queue is maintained in FIFO order.  usba_hcdi_cb
38*0Sstevel@tonic-gate  * adds to the queue, and hcdi_cb_thread takes the callbacks off the queue
39*0Sstevel@tonic-gate  * and executes them.
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate #define	USBA_FRAMEWORK
42*0Sstevel@tonic-gate #include <sys/usb/usba/usba_impl.h>
43*0Sstevel@tonic-gate #include <sys/usb/usba/hcdi_impl.h>
44*0Sstevel@tonic-gate #include <sys/kstat.h>
45*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /* function prototypes, XXXX use hcdi_ prefix?	*/
48*0Sstevel@tonic-gate static void usba_hcdi_create_stats(usba_hcdi_t *, int);
49*0Sstevel@tonic-gate static void usba_hcdi_update_error_stats(usba_hcdi_t *, usb_cr_t);
50*0Sstevel@tonic-gate static void usba_hcdi_destroy_stats(usba_hcdi_t *);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /* internal functions */
53*0Sstevel@tonic-gate static uint_t hcdi_soft_intr(caddr_t arg1, caddr_t arg2);
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static void hcdi_cb_thread(void *);
56*0Sstevel@tonic-gate static void hcdi_shared_cb_thread(void *);
57*0Sstevel@tonic-gate static void hcdi_do_cb(usba_pipe_handle_data_t *, usba_req_wrapper_t *,
58*0Sstevel@tonic-gate 							usba_hcdi_t *);
59*0Sstevel@tonic-gate static void hcdi_autoclearing(usba_req_wrapper_t *);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate /* private function from USBAI */
62*0Sstevel@tonic-gate void	usba_pipe_clear(usb_pipe_handle_t);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate /* for debug messages */
65*0Sstevel@tonic-gate static uint_t	hcdi_errmask	= (uint_t)DPRINT_MASK_ALL;
66*0Sstevel@tonic-gate static uint_t	hcdi_errlevel	= USB_LOG_L4;
67*0Sstevel@tonic-gate static uint_t	hcdi_instance_debug = (uint_t)-1;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate void
70*0Sstevel@tonic-gate usba_hcdi_initialization()
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate void
76*0Sstevel@tonic-gate usba_hcdi_destroy()
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate  * store hcdi structure in the dip
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate void
85*0Sstevel@tonic-gate usba_hcdi_set_hcdi(dev_info_t *dip, usba_hcdi_t *hcdi)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	ddi_set_driver_private(dip, hcdi);
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * retrieve hcdi structure from the dip
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate usba_hcdi_t *
95*0Sstevel@tonic-gate usba_hcdi_get_hcdi(dev_info_t *dip)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	return (ddi_get_driver_private(dip));
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * Called by an	HCD to attach an instance of the driver
102*0Sstevel@tonic-gate  *	make this instance known to USBA
103*0Sstevel@tonic-gate  *	the HCD	should initialize usba_hcdi structure prior
104*0Sstevel@tonic-gate  *	to calling this	interface
105*0Sstevel@tonic-gate  */
106*0Sstevel@tonic-gate int
107*0Sstevel@tonic-gate usba_hcdi_register(usba_hcdi_register_args_t *args, uint_t flags)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	char		*datap;
110*0Sstevel@tonic-gate 	uint_t		soft_prip;
111*0Sstevel@tonic-gate 	usba_hcdi_t	*hcdi = kmem_zalloc(sizeof (usba_hcdi_t), KM_SLEEP);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if (args->usba_hcdi_register_version != HCDI_REGISTER_VERS_0) {
114*0Sstevel@tonic-gate 		kmem_free(hcdi, sizeof (usba_hcdi_t));
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		return (USB_FAILURE);
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	hcdi->hcdi_dip = args->usba_hcdi_register_dip;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	/*
122*0Sstevel@tonic-gate 	 * Create a log_handle
123*0Sstevel@tonic-gate 	 */
124*0Sstevel@tonic-gate 	hcdi->hcdi_log_handle = usb_alloc_log_hdl(hcdi->hcdi_dip, NULL,
125*0Sstevel@tonic-gate 			&hcdi_errlevel, &hcdi_errmask, &hcdi_instance_debug,
126*0Sstevel@tonic-gate 			0);
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
129*0Sstevel@tonic-gate 	    "usba_hcdi_register: %s", ddi_node_name(hcdi->hcdi_dip));
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	/*
132*0Sstevel@tonic-gate 	 * Initialize the mutex.  Use the iblock cookie passed in
133*0Sstevel@tonic-gate 	 * by the host controller driver.
134*0Sstevel@tonic-gate 	 */
135*0Sstevel@tonic-gate 	mutex_init(&hcdi->hcdi_mutex, NULL, MUTEX_DRIVER,
136*0Sstevel@tonic-gate 			args->usba_hcdi_register_iblock_cookie);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/* add soft interrupt */
139*0Sstevel@tonic-gate 	if (ddi_intr_add_softint(hcdi->hcdi_dip, &hcdi->hcdi_softint_hdl,
140*0Sstevel@tonic-gate 	    DDI_INTR_SOFTPRI_MAX, hcdi_soft_intr, (caddr_t)hcdi) !=
141*0Sstevel@tonic-gate 	    DDI_SUCCESS) {
142*0Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
143*0Sstevel@tonic-gate 		    "usba_hcd_register: add soft interrupt failed");
144*0Sstevel@tonic-gate 		mutex_destroy(&hcdi->hcdi_mutex);
145*0Sstevel@tonic-gate 		usb_free_log_hdl(hcdi->hcdi_log_handle);
146*0Sstevel@tonic-gate 		kmem_free(hcdi, sizeof (usba_hcdi_t));
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		return (USB_FAILURE);
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (ddi_intr_get_softint_pri(hcdi->hcdi_softint_hdl, &soft_prip) !=
152*0Sstevel@tonic-gate 	    DDI_SUCCESS) {
153*0Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
154*0Sstevel@tonic-gate 		    "usba_hcd_register: get soft interrupt priority failed");
155*0Sstevel@tonic-gate 		(void) ddi_intr_remove_softint(hcdi->hcdi_softint_hdl);
156*0Sstevel@tonic-gate 		mutex_destroy(&hcdi->hcdi_mutex);
157*0Sstevel@tonic-gate 		usb_free_log_hdl(hcdi->hcdi_log_handle);
158*0Sstevel@tonic-gate 		kmem_free(hcdi, sizeof (usba_hcdi_t));
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 		return (USB_FAILURE);
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	/*
164*0Sstevel@tonic-gate 	 * Priority and iblock_cookie are one and the same
165*0Sstevel@tonic-gate 	 * (However, retaining hcdi_soft_iblock_cookie for now
166*0Sstevel@tonic-gate 	 * assigning it w/ priority. In future all iblock_cookie
167*0Sstevel@tonic-gate 	 * could just go)
168*0Sstevel@tonic-gate 	 */
169*0Sstevel@tonic-gate 	hcdi->hcdi_soft_iblock_cookie =
170*0Sstevel@tonic-gate 	    (ddi_iblock_cookie_t)(uint64_t)soft_prip;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	usba_init_list(&hcdi->hcdi_cb_queue, NULL, NULL);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	hcdi->hcdi_dma_attr	= args->usba_hcdi_register_dma_attr;
175*0Sstevel@tonic-gate 	hcdi->hcdi_flags	= flags;
176*0Sstevel@tonic-gate 	hcdi->hcdi_ops		= args->usba_hcdi_register_ops;
177*0Sstevel@tonic-gate 	hcdi->hcdi_iblock_cookie = args->usba_hcdi_register_iblock_cookie;
178*0Sstevel@tonic-gate 	usba_hcdi_create_stats(hcdi, ddi_get_instance(hcdi->hcdi_dip));
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	hcdi->hcdi_min_xfer	= hcdi->hcdi_dma_attr->dma_attr_minxfer;
181*0Sstevel@tonic-gate 	hcdi->hcdi_min_burst_size =
182*0Sstevel@tonic-gate 		(1<<(ddi_ffs(hcdi->hcdi_dma_attr->dma_attr_burstsizes)-1));
183*0Sstevel@tonic-gate 	hcdi->hcdi_max_burst_size =
184*0Sstevel@tonic-gate 		(1<<(ddi_fls(hcdi->hcdi_dma_attr->dma_attr_burstsizes)-1));
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	usba_hcdi_set_hcdi(hcdi->hcdi_dip, hcdi);
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY,
189*0Sstevel@tonic-gate 	    hcdi->hcdi_dip,
190*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "ugen-default-binding", &datap) ==
191*0Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
192*0Sstevel@tonic-gate 		if (strcmp(datap, "device") == 0) {
193*0Sstevel@tonic-gate 			hcdi->hcdi_ugen_default_binding =
194*0Sstevel@tonic-gate 			    USBA_UGEN_DEVICE_BINDING;
195*0Sstevel@tonic-gate 		} else if (strcmp(datap, "interface") == 0) {
196*0Sstevel@tonic-gate 			hcdi->hcdi_ugen_default_binding =
197*0Sstevel@tonic-gate 			    USBA_UGEN_INTERFACE_BINDING;
198*0Sstevel@tonic-gate 		} else {
199*0Sstevel@tonic-gate 			USB_DPRINTF_L1(DPRINT_MASK_HCDI,
200*0Sstevel@tonic-gate 			    hcdi->hcdi_log_handle,
201*0Sstevel@tonic-gate 			    "illegal value (%s) for "
202*0Sstevel@tonic-gate 			    "ugen_default_binding property",
203*0Sstevel@tonic-gate 			    datap);
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 		ddi_prop_free(datap);
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	return (USB_SUCCESS);
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate /*
213*0Sstevel@tonic-gate  * Called by an	HCD to detach an instance of the driver
214*0Sstevel@tonic-gate  */
215*0Sstevel@tonic-gate /*ARGSUSED*/
216*0Sstevel@tonic-gate void
217*0Sstevel@tonic-gate usba_hcdi_unregister(dev_info_t *dip)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	usba_hcdi_t *hcdi = usba_hcdi_get_hcdi(dip);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if (hcdi) {
222*0Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
223*0Sstevel@tonic-gate 		    "usba_hcdi_unregister: %s", ddi_node_name(dip));
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 		usba_hcdi_set_hcdi(dip, NULL);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 		mutex_destroy(&hcdi->hcdi_mutex);
228*0Sstevel@tonic-gate 		usba_hcdi_destroy_stats(hcdi);
229*0Sstevel@tonic-gate 		usb_free_log_hdl(hcdi->hcdi_log_handle);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 		/* Destroy the soft interrupt */
232*0Sstevel@tonic-gate 		(void) ddi_intr_remove_softint(hcdi->hcdi_softint_hdl);
233*0Sstevel@tonic-gate 		kmem_free(hcdi, sizeof (usba_hcdi_t));
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate  * alloc usba_hcdi_ops structure
240*0Sstevel@tonic-gate  *	called from the HCD attach routine
241*0Sstevel@tonic-gate  */
242*0Sstevel@tonic-gate usba_hcdi_ops_t *
243*0Sstevel@tonic-gate usba_alloc_hcdi_ops()
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate 	usba_hcdi_ops_t	*usba_hcdi_ops;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	usba_hcdi_ops = kmem_zalloc(sizeof (usba_hcdi_ops_t), KM_SLEEP);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	return (usba_hcdi_ops);
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate /*
254*0Sstevel@tonic-gate  * dealloc usba_hcdi_ops structure
255*0Sstevel@tonic-gate  */
256*0Sstevel@tonic-gate void
257*0Sstevel@tonic-gate usba_free_hcdi_ops(usba_hcdi_ops_t *hcdi_ops)
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate 	if (hcdi_ops) {
260*0Sstevel@tonic-gate 		kmem_free(hcdi_ops, sizeof (usba_hcdi_ops_t));
261*0Sstevel@tonic-gate 	}
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate /*
266*0Sstevel@tonic-gate  * Allocate the hotplug kstats structure
267*0Sstevel@tonic-gate  */
268*0Sstevel@tonic-gate void
269*0Sstevel@tonic-gate usba_hcdi_create_stats(usba_hcdi_t *hcdi, int instance)
270*0Sstevel@tonic-gate {
271*0Sstevel@tonic-gate 	char			kstatname[KSTAT_STRLEN];
272*0Sstevel@tonic-gate 	const char		*dname = ddi_driver_name(hcdi->hcdi_dip);
273*0Sstevel@tonic-gate 	hcdi_hotplug_stats_t	*hsp;
274*0Sstevel@tonic-gate 	hcdi_error_stats_t	*esp;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	if (HCDI_HOTPLUG_STATS(hcdi) == NULL) {
277*0Sstevel@tonic-gate 		(void) snprintf(kstatname, KSTAT_STRLEN, "%s%d,hotplug",
278*0Sstevel@tonic-gate 		    dname, instance);
279*0Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS(hcdi) = kstat_create("usba", instance,
280*0Sstevel@tonic-gate 		    kstatname, "usb_hotplug", KSTAT_TYPE_NAMED,
281*0Sstevel@tonic-gate 		    sizeof (hcdi_hotplug_stats_t) / sizeof (kstat_named_t),
282*0Sstevel@tonic-gate 		    KSTAT_FLAG_PERSISTENT);
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 		if (HCDI_HOTPLUG_STATS(hcdi) == NULL) {
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 			return;
287*0Sstevel@tonic-gate 		}
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 		hsp = HCDI_HOTPLUG_STATS_DATA(hcdi);
290*0Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_hotplug_total_success,
291*0Sstevel@tonic-gate 		    "Total Hotplug Successes", KSTAT_DATA_UINT64);
292*0Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_hotplug_success,
293*0Sstevel@tonic-gate 		    "Hotplug Successes", KSTAT_DATA_UINT64);
294*0Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_hotplug_total_failure,
295*0Sstevel@tonic-gate 		    "Hotplug Total Failures", KSTAT_DATA_UINT64);
296*0Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_hotplug_failure,
297*0Sstevel@tonic-gate 		    "Hotplug Failures", KSTAT_DATA_UINT64);
298*0Sstevel@tonic-gate 		kstat_named_init(&hsp->hcdi_device_count,
299*0Sstevel@tonic-gate 		    "Device Count", KSTAT_DATA_UINT64);
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS(hcdi)->ks_private = hcdi;
302*0Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS(hcdi)->ks_update = nulldev;
303*0Sstevel@tonic-gate 		kstat_install(HCDI_HOTPLUG_STATS(hcdi));
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	if (HCDI_ERROR_STATS(hcdi) == NULL) {
307*0Sstevel@tonic-gate 		(void) snprintf(kstatname, KSTAT_STRLEN, "%s%d,error",
308*0Sstevel@tonic-gate 		    dname, instance);
309*0Sstevel@tonic-gate 		HCDI_ERROR_STATS(hcdi) = kstat_create("usba", instance,
310*0Sstevel@tonic-gate 		    kstatname, "usb_errors", KSTAT_TYPE_NAMED,
311*0Sstevel@tonic-gate 		    sizeof (hcdi_error_stats_t) / sizeof (kstat_named_t),
312*0Sstevel@tonic-gate 		    KSTAT_FLAG_PERSISTENT);
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 		if (HCDI_ERROR_STATS(hcdi) == NULL) {
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 			return;
317*0Sstevel@tonic-gate 		}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		esp = HCDI_ERROR_STATS_DATA(hcdi);
320*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_crc, "CRC Errors", KSTAT_DATA_UINT64);
321*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_bitstuffing,
322*0Sstevel@tonic-gate 		    "Bit Stuffing Violations", KSTAT_DATA_UINT64);
323*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_data_toggle_mm,
324*0Sstevel@tonic-gate 		    "Data Toggle PID Errors", KSTAT_DATA_UINT64);
325*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_stall,
326*0Sstevel@tonic-gate 		    "Endpoint Stalls", KSTAT_DATA_UINT64);
327*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_dev_not_resp,
328*0Sstevel@tonic-gate 		    "Device Not Responding", KSTAT_DATA_UINT64);
329*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_pid_checkfailure,
330*0Sstevel@tonic-gate 		    "PID Check Bit Errors", KSTAT_DATA_UINT64);
331*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_unexp_pid,
332*0Sstevel@tonic-gate 		    "Invalid PID Errors", KSTAT_DATA_UINT64);
333*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_data_overrun,
334*0Sstevel@tonic-gate 		    "Data Overruns", KSTAT_DATA_UINT64);
335*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_data_underrun,
336*0Sstevel@tonic-gate 		    "Data Underruns", KSTAT_DATA_UINT64);
337*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_buffer_overrun,
338*0Sstevel@tonic-gate 		    "Buffer Overruns", KSTAT_DATA_UINT64);
339*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_buffer_underrun,
340*0Sstevel@tonic-gate 		    "Buffer Underruns", KSTAT_DATA_UINT64);
341*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_timeout,
342*0Sstevel@tonic-gate 		    "Command Timed Out", KSTAT_DATA_UINT64);
343*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_not_accessed,
344*0Sstevel@tonic-gate 		    "Not Accessed By Hardware", KSTAT_DATA_UINT64);
345*0Sstevel@tonic-gate 		kstat_named_init(&esp->cc_unspecified_err,
346*0Sstevel@tonic-gate 		    "Unspecified Error", KSTAT_DATA_UINT64);
347*0Sstevel@tonic-gate #ifdef	NOTYETNEEDED
348*0Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_failure,
349*0Sstevel@tonic-gate 		    "USB Failure", KSTAT_DATA_UINT64);
350*0Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_no_resources,
351*0Sstevel@tonic-gate 		    "No Resources", KSTAT_DATA_UINT64);
352*0Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_no_bandwidth,
353*0Sstevel@tonic-gate 		    "No Bandwidth", KSTAT_DATA_UINT64);
354*0Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_pipe_reserved,
355*0Sstevel@tonic-gate 		    "Pipe Reserved", KSTAT_DATA_UINT64);
356*0Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_pipe_unshareable,
357*0Sstevel@tonic-gate 		    "Pipe Unshareable", KSTAT_DATA_UINT64);
358*0Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_not_supported,
359*0Sstevel@tonic-gate 		    "Function Not Supported", KSTAT_DATA_UINT64);
360*0Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_pipe_error,
361*0Sstevel@tonic-gate 		    "Pipe Error", KSTAT_DATA_UINT64);
362*0Sstevel@tonic-gate 		kstat_named_init(&esp->hcdi_usb_pipe_busy,
363*0Sstevel@tonic-gate 		    "Pipe Busy", KSTAT_DATA_UINT64);
364*0Sstevel@tonic-gate #endif
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 		HCDI_ERROR_STATS(hcdi)->ks_private = hcdi;
367*0Sstevel@tonic-gate 		HCDI_ERROR_STATS(hcdi)->ks_update = nulldev;
368*0Sstevel@tonic-gate 		kstat_install(HCDI_ERROR_STATS(hcdi));
369*0Sstevel@tonic-gate 	}
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate /*
374*0Sstevel@tonic-gate  * Do actual error stats
375*0Sstevel@tonic-gate  */
376*0Sstevel@tonic-gate void
377*0Sstevel@tonic-gate usba_hcdi_update_error_stats(usba_hcdi_t *hcdi, usb_cr_t completion_reason)
378*0Sstevel@tonic-gate {
379*0Sstevel@tonic-gate 	if (HCDI_ERROR_STATS(hcdi) == NULL) {
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 		return;
382*0Sstevel@tonic-gate 	}
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	switch (completion_reason) {
385*0Sstevel@tonic-gate 	case USB_CR_OK:
386*0Sstevel@tonic-gate 		break;
387*0Sstevel@tonic-gate 	case USB_CR_CRC:
388*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_crc.value.ui64++;
389*0Sstevel@tonic-gate 		break;
390*0Sstevel@tonic-gate 	case USB_CR_BITSTUFFING:
391*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_bitstuffing.value.ui64++;
392*0Sstevel@tonic-gate 		break;
393*0Sstevel@tonic-gate 	case USB_CR_DATA_TOGGLE_MM:
394*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_data_toggle_mm.value.ui64++;
395*0Sstevel@tonic-gate 		break;
396*0Sstevel@tonic-gate 	case USB_CR_STALL:
397*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_stall.value.ui64++;
398*0Sstevel@tonic-gate 		break;
399*0Sstevel@tonic-gate 	case USB_CR_DEV_NOT_RESP:
400*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_dev_not_resp.value.ui64++;
401*0Sstevel@tonic-gate 		break;
402*0Sstevel@tonic-gate 	case USB_CR_PID_CHECKFAILURE:
403*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_pid_checkfailure.value.ui64++;
404*0Sstevel@tonic-gate 		break;
405*0Sstevel@tonic-gate 	case USB_CR_UNEXP_PID:
406*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_unexp_pid.value.ui64++;
407*0Sstevel@tonic-gate 		break;
408*0Sstevel@tonic-gate 	case USB_CR_DATA_OVERRUN:
409*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_data_overrun.value.ui64++;
410*0Sstevel@tonic-gate 		break;
411*0Sstevel@tonic-gate 	case USB_CR_DATA_UNDERRUN:
412*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_data_underrun.value.ui64++;
413*0Sstevel@tonic-gate 		break;
414*0Sstevel@tonic-gate 	case USB_CR_BUFFER_OVERRUN:
415*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_buffer_overrun.value.ui64++;
416*0Sstevel@tonic-gate 		break;
417*0Sstevel@tonic-gate 	case USB_CR_BUFFER_UNDERRUN:
418*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_buffer_underrun.value.ui64++;
419*0Sstevel@tonic-gate 		break;
420*0Sstevel@tonic-gate 	case USB_CR_TIMEOUT:
421*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_timeout.value.ui64++;
422*0Sstevel@tonic-gate 		break;
423*0Sstevel@tonic-gate 	case USB_CR_NOT_ACCESSED:
424*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_not_accessed.value.ui64++;
425*0Sstevel@tonic-gate 		break;
426*0Sstevel@tonic-gate 	case USB_CR_NO_RESOURCES:
427*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_no_resources.value.ui64++;
428*0Sstevel@tonic-gate 		break;
429*0Sstevel@tonic-gate 	case USB_CR_UNSPECIFIED_ERR:
430*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_unspecified_err.value.ui64++;
431*0Sstevel@tonic-gate 		break;
432*0Sstevel@tonic-gate 	case USB_CR_STOPPED_POLLING:
433*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_stopped_polling.value.ui64++;
434*0Sstevel@tonic-gate 		break;
435*0Sstevel@tonic-gate 	case USB_CR_PIPE_CLOSING:
436*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_pipe_closing.value.ui64++;
437*0Sstevel@tonic-gate 		break;
438*0Sstevel@tonic-gate 	case USB_CR_PIPE_RESET:
439*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_pipe_reset.value.ui64++;
440*0Sstevel@tonic-gate 		break;
441*0Sstevel@tonic-gate 	case USB_CR_NOT_SUPPORTED:
442*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_not_supported.value.ui64++;
443*0Sstevel@tonic-gate 		break;
444*0Sstevel@tonic-gate 	case USB_CR_FLUSHED:
445*0Sstevel@tonic-gate 		HCDI_ERROR_STATS_DATA(hcdi)->cc_flushed.value.ui64++;
446*0Sstevel@tonic-gate 		break;
447*0Sstevel@tonic-gate 	default:
448*0Sstevel@tonic-gate 		break;
449*0Sstevel@tonic-gate 	}
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate /*
454*0Sstevel@tonic-gate  * Destroy the hotplug kstats structure
455*0Sstevel@tonic-gate  */
456*0Sstevel@tonic-gate static void
457*0Sstevel@tonic-gate usba_hcdi_destroy_stats(usba_hcdi_t *hcdi)
458*0Sstevel@tonic-gate {
459*0Sstevel@tonic-gate 	if (HCDI_HOTPLUG_STATS(hcdi)) {
460*0Sstevel@tonic-gate 		kstat_delete(HCDI_HOTPLUG_STATS(hcdi));
461*0Sstevel@tonic-gate 		HCDI_HOTPLUG_STATS(hcdi) = NULL;
462*0Sstevel@tonic-gate 	}
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	if (HCDI_ERROR_STATS(hcdi)) {
465*0Sstevel@tonic-gate 		kstat_delete(HCDI_ERROR_STATS(hcdi));
466*0Sstevel@tonic-gate 		HCDI_ERROR_STATS(hcdi) = NULL;
467*0Sstevel@tonic-gate 	}
468*0Sstevel@tonic-gate }
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate /*
472*0Sstevel@tonic-gate  * HCD callback handling
473*0Sstevel@tonic-gate  */
474*0Sstevel@tonic-gate void
475*0Sstevel@tonic-gate usba_hcdi_cb(usba_pipe_handle_data_t *ph_data,
476*0Sstevel@tonic-gate 	usb_opaque_t	req,
477*0Sstevel@tonic-gate 	usb_cr_t	completion_reason)
478*0Sstevel@tonic-gate {
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	usba_device_t		*usba_device = ph_data->p_usba_device;
481*0Sstevel@tonic-gate 	usba_hcdi_t		*hcdi =	usba_hcdi_get_hcdi(
482*0Sstevel@tonic-gate 					usba_device->usb_root_hub_dip);
483*0Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp = USBA_REQ2WRP(req);
484*0Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph_data->p_ep;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate #ifdef DEBUG
489*0Sstevel@tonic-gate 	mutex_enter(&ph_data->p_ph_impl->usba_ph_mutex);
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
492*0Sstevel@tonic-gate 	    "usba_hcdi_cb: "
493*0Sstevel@tonic-gate 	    "ph_data=0x%p req=0x%p state=%d ref=%d cnt=%d cr=%d",
494*0Sstevel@tonic-gate 	    ph_data, req, ph_data->p_ph_impl->usba_ph_state,
495*0Sstevel@tonic-gate 	    ph_data->p_ph_impl->usba_ph_ref_count, ph_data->p_req_count,
496*0Sstevel@tonic-gate 	    completion_reason);
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	mutex_exit(&ph_data->p_ph_impl->usba_ph_mutex);
499*0Sstevel@tonic-gate #endif
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	/* Set the completion reason */
502*0Sstevel@tonic-gate 	switch (eptd->bmAttributes & USB_EP_ATTR_MASK) {
503*0Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
504*0Sstevel@tonic-gate 		((usb_ctrl_req_t *)req)->
505*0Sstevel@tonic-gate 		    ctrl_completion_reason = completion_reason;
506*0Sstevel@tonic-gate 		break;
507*0Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
508*0Sstevel@tonic-gate 		((usb_bulk_req_t *)req)->
509*0Sstevel@tonic-gate 		    bulk_completion_reason = completion_reason;
510*0Sstevel@tonic-gate 		break;
511*0Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
512*0Sstevel@tonic-gate 		((usb_intr_req_t *)req)->
513*0Sstevel@tonic-gate 		    intr_completion_reason = completion_reason;
514*0Sstevel@tonic-gate 		break;
515*0Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
516*0Sstevel@tonic-gate 		((usb_isoc_req_t *)req)->
517*0Sstevel@tonic-gate 		    isoc_completion_reason = completion_reason;
518*0Sstevel@tonic-gate 		break;
519*0Sstevel@tonic-gate 	}
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	/*
522*0Sstevel@tonic-gate 	 * exception callbacks will still go thru a taskq thread
523*0Sstevel@tonic-gate 	 * but should occur after the soft interrupt callback
524*0Sstevel@tonic-gate 	 * By design of periodic pipes, polling will stop on any
525*0Sstevel@tonic-gate 	 * exception
526*0Sstevel@tonic-gate 	 */
527*0Sstevel@tonic-gate 	if ((ph_data->p_spec_flag & USBA_PH_FLAG_USE_SOFT_INTR) &&
528*0Sstevel@tonic-gate 	    (completion_reason == USB_CR_OK)) {
529*0Sstevel@tonic-gate 		ph_data->p_soft_intr++;
530*0Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 		usba_add_to_list(&hcdi->hcdi_cb_queue, &req_wrp->wr_queue);
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 		if (ddi_intr_trigger_softint(hcdi->hcdi_softint_hdl, NULL) !=
535*0Sstevel@tonic-gate 		    DDI_SUCCESS)
536*0Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
537*0Sstevel@tonic-gate 			    "usba_hcdi_cb: ddi_intr_trigger_softint  failed");
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 		return;
540*0Sstevel@tonic-gate 	}
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	/*
543*0Sstevel@tonic-gate 	 * USBA_PH_FLAG_TQ_SHARE is for bulk and intr requests,
544*0Sstevel@tonic-gate 	 * USBA_PH_FLAG_USE_SOFT_INTR is only for isoch,
545*0Sstevel@tonic-gate 	 * so there are no conflicts.
546*0Sstevel@tonic-gate 	 */
547*0Sstevel@tonic-gate 	if (ph_data->p_spec_flag & USBA_PH_FLAG_TQ_SHARE) {
548*0Sstevel@tonic-gate 		int iface;
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
551*0Sstevel@tonic-gate 		iface = usb_get_if_number(ph_data->p_dip);
552*0Sstevel@tonic-gate 		if (iface < 0) {
553*0Sstevel@tonic-gate 			/* we own the device, use the first taskq */
554*0Sstevel@tonic-gate 			iface = 0;
555*0Sstevel@tonic-gate 		}
556*0Sstevel@tonic-gate 		if (taskq_dispatch(usba_device->usb_shared_taskq[iface],
557*0Sstevel@tonic-gate 		    hcdi_shared_cb_thread, req_wrp, TQ_NOSLEEP) ==
558*0Sstevel@tonic-gate 		    NULL) {
559*0Sstevel@tonic-gate 			usba_req_exc_cb(req_wrp,
560*0Sstevel@tonic-gate 			    USB_CR_NO_RESOURCES, USB_CB_ASYNC_REQ_FAILED);
561*0Sstevel@tonic-gate 		}
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 		return;
564*0Sstevel@tonic-gate 	}
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	/* Add the callback to the pipehandles callback list */
567*0Sstevel@tonic-gate 	usba_add_to_list(&ph_data->p_cb_queue, &req_wrp->wr_queue);
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	/* only dispatch if there is no thread running */
570*0Sstevel@tonic-gate 	if (ph_data->p_thread_id == 0) {
571*0Sstevel@tonic-gate 		if (usba_async_ph_req(ph_data, hcdi_cb_thread,
572*0Sstevel@tonic-gate 		    ph_data, USB_FLAGS_NOSLEEP) != USB_SUCCESS) {
573*0Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
574*0Sstevel@tonic-gate 			    "usba_hcdi_cb: taskq_dispatch failed");
575*0Sstevel@tonic-gate 			if (usba_rm_from_list(&ph_data->p_cb_queue,
576*0Sstevel@tonic-gate 			    &req_wrp->wr_queue) == USB_SUCCESS) {
577*0Sstevel@tonic-gate 				mutex_exit(&ph_data->p_mutex);
578*0Sstevel@tonic-gate 				usba_req_exc_cb(req_wrp,
579*0Sstevel@tonic-gate 				    USB_CR_NO_RESOURCES,
580*0Sstevel@tonic-gate 				    USB_CB_ASYNC_REQ_FAILED);
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 				return;
583*0Sstevel@tonic-gate 			}
584*0Sstevel@tonic-gate 		} else {
585*0Sstevel@tonic-gate 			ph_data->p_thread_id = (kthread_t *)1;
586*0Sstevel@tonic-gate 		}
587*0Sstevel@tonic-gate 	}
588*0Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
589*0Sstevel@tonic-gate }
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate /*
593*0Sstevel@tonic-gate  * thread to perform the callbacks
594*0Sstevel@tonic-gate  */
595*0Sstevel@tonic-gate static void
596*0Sstevel@tonic-gate hcdi_cb_thread(void *arg)
597*0Sstevel@tonic-gate {
598*0Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph_data =
599*0Sstevel@tonic-gate 				(usba_pipe_handle_data_t *)arg;
600*0Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
601*0Sstevel@tonic-gate 	usba_hcdi_t		*hcdi = usba_hcdi_get_hcdi(ph_data->
602*0Sstevel@tonic-gate 				p_usba_device->usb_root_hub_dip);
603*0Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp;
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
606*0Sstevel@tonic-gate 	ASSERT(ph_data->p_thread_id == (kthread_t *)1);
607*0Sstevel@tonic-gate 	ph_data->p_thread_id = curthread;
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 	/*
610*0Sstevel@tonic-gate 	 * hold the ph_data. we can't use usba_hold_ph_data() since
611*0Sstevel@tonic-gate 	 * it will return NULL if we are closing the pipe which would
612*0Sstevel@tonic-gate 	 * then leave all requests stuck in the cb_queue
613*0Sstevel@tonic-gate 	 */
614*0Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
615*0Sstevel@tonic-gate 	ph_impl->usba_ph_ref_count++;
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
618*0Sstevel@tonic-gate 	    "hcdi_cb_thread: ph_data=0x%p ref=%d", ph_data,
619*0Sstevel@tonic-gate 	    ph_impl->usba_ph_ref_count);
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 	/*
624*0Sstevel@tonic-gate 	 * wait till soft interrupt callbacks are taken care of
625*0Sstevel@tonic-gate 	 */
626*0Sstevel@tonic-gate 	while (ph_data->p_soft_intr) {
627*0Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
628*0Sstevel@tonic-gate 		delay(1);
629*0Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
630*0Sstevel@tonic-gate 	}
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	while ((req_wrp = (usba_req_wrapper_t *)
633*0Sstevel@tonic-gate 	    usba_rm_first_pvt_from_list(&ph_data->p_cb_queue)) != NULL) {
634*0Sstevel@tonic-gate 		hcdi_do_cb(ph_data, req_wrp, hcdi);
635*0Sstevel@tonic-gate 	}
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	ph_data->p_thread_id = 0;
638*0Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
641*0Sstevel@tonic-gate 	    "hcdi_cb_thread done: ph_data=0x%p", ph_data);
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	usba_release_ph_data(ph_impl);
644*0Sstevel@tonic-gate }
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate static void
648*0Sstevel@tonic-gate hcdi_do_cb(usba_pipe_handle_data_t *ph_data, usba_req_wrapper_t *req_wrp,
649*0Sstevel@tonic-gate     usba_hcdi_t *hcdi)
650*0Sstevel@tonic-gate {
651*0Sstevel@tonic-gate 	usb_cr_t		completion_reason;
652*0Sstevel@tonic-gate 	usb_req_attrs_t		attrs = req_wrp->wr_attrs;
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	switch (req_wrp->wr_ph_data->p_ep.bmAttributes &
655*0Sstevel@tonic-gate 	    USB_EP_ATTR_MASK) {
656*0Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
657*0Sstevel@tonic-gate 		completion_reason =
658*0Sstevel@tonic-gate 		    USBA_WRP2CTRL_REQ(req_wrp)->ctrl_completion_reason;
659*0Sstevel@tonic-gate 		break;
660*0Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
661*0Sstevel@tonic-gate 		completion_reason =
662*0Sstevel@tonic-gate 		    USBA_WRP2INTR_REQ(req_wrp)->intr_completion_reason;
663*0Sstevel@tonic-gate 		break;
664*0Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
665*0Sstevel@tonic-gate 		completion_reason =
666*0Sstevel@tonic-gate 		    USBA_WRP2BULK_REQ(req_wrp)->bulk_completion_reason;
667*0Sstevel@tonic-gate 		break;
668*0Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
669*0Sstevel@tonic-gate 		completion_reason =
670*0Sstevel@tonic-gate 		    USBA_WRP2ISOC_REQ(req_wrp)->isoc_completion_reason;
671*0Sstevel@tonic-gate 		break;
672*0Sstevel@tonic-gate 	}
673*0Sstevel@tonic-gate 	req_wrp->wr_cr = completion_reason;
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
676*0Sstevel@tonic-gate 	    "hcdi_do_cb: wrp=0x%p cr=0x%x", req_wrp, completion_reason);
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 	/*
679*0Sstevel@tonic-gate 	 * Normal callbacks:
680*0Sstevel@tonic-gate 	 */
681*0Sstevel@tonic-gate 	if (completion_reason == USB_CR_OK) {
682*0Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
683*0Sstevel@tonic-gate 		usba_req_normal_cb(req_wrp);
684*0Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
685*0Sstevel@tonic-gate 	} else {
686*0Sstevel@tonic-gate 		usb_pipe_state_t pipe_state;
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 		USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
689*0Sstevel@tonic-gate 		    "exception callback handling: attrs=0x%x", attrs);
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 		/*
692*0Sstevel@tonic-gate 		 * In exception callback handling, if we were
693*0Sstevel@tonic-gate 		 * not able to clear stall, we need to modify
694*0Sstevel@tonic-gate 		 * pipe state. Also if auto-clearing is not set
695*0Sstevel@tonic-gate 		 * pipe state needs to be modified.
696*0Sstevel@tonic-gate 		 */
697*0Sstevel@tonic-gate 		pipe_state = usba_get_ph_state(ph_data);
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 		if (!USBA_PIPE_CLOSING(pipe_state)) {
700*0Sstevel@tonic-gate 			switch (completion_reason) {
701*0Sstevel@tonic-gate 			case USB_CR_STOPPED_POLLING:
702*0Sstevel@tonic-gate 				if (pipe_state ==
703*0Sstevel@tonic-gate 				    USB_PIPE_STATE_ACTIVE) {
704*0Sstevel@tonic-gate 					usba_pipe_new_state(ph_data,
705*0Sstevel@tonic-gate 					    USB_PIPE_STATE_IDLE);
706*0Sstevel@tonic-gate 				}
707*0Sstevel@tonic-gate 				break;
708*0Sstevel@tonic-gate 			case USB_CR_NOT_SUPPORTED:
709*0Sstevel@tonic-gate 				usba_pipe_new_state(ph_data,
710*0Sstevel@tonic-gate 				    USB_PIPE_STATE_IDLE);
711*0Sstevel@tonic-gate 				break;
712*0Sstevel@tonic-gate 			case USB_CR_PIPE_RESET:
713*0Sstevel@tonic-gate 			case USB_CR_FLUSHED:
714*0Sstevel@tonic-gate 				break;
715*0Sstevel@tonic-gate 			default:
716*0Sstevel@tonic-gate 				usba_pipe_new_state(ph_data,
717*0Sstevel@tonic-gate 				    USB_PIPE_STATE_ERROR);
718*0Sstevel@tonic-gate 				break;
719*0Sstevel@tonic-gate 			}
720*0Sstevel@tonic-gate 		}
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 		pipe_state = usba_get_ph_state(ph_data);
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
725*0Sstevel@tonic-gate 		if (attrs & USB_ATTRS_PIPE_RESET) {
726*0Sstevel@tonic-gate 			if ((completion_reason != USB_CR_PIPE_RESET) &&
727*0Sstevel@tonic-gate 			    (pipe_state == USB_PIPE_STATE_ERROR)) {
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 				hcdi_autoclearing(req_wrp);
730*0Sstevel@tonic-gate 			}
731*0Sstevel@tonic-gate 		}
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 		usba_req_exc_cb(req_wrp, 0, 0);
734*0Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
735*0Sstevel@tonic-gate 	}
736*0Sstevel@tonic-gate 
737*0Sstevel@tonic-gate 	/* Update the hcdi error kstats */
738*0Sstevel@tonic-gate 	if (completion_reason) {
739*0Sstevel@tonic-gate 		mutex_enter(&hcdi->hcdi_mutex);
740*0Sstevel@tonic-gate 		usba_hcdi_update_error_stats(hcdi, completion_reason);
741*0Sstevel@tonic-gate 		mutex_exit(&hcdi->hcdi_mutex);
742*0Sstevel@tonic-gate 	}
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate 	/*
745*0Sstevel@tonic-gate 	 * Once the callback is finished, release the pipe handle
746*0Sstevel@tonic-gate 	 * we start the next request first to avoid that the
747*0Sstevel@tonic-gate 	 * pipe gets closed while starting the next request
748*0Sstevel@tonic-gate 	 */
749*0Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
750*0Sstevel@tonic-gate 	usba_start_next_req(ph_data);
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
753*0Sstevel@tonic-gate }
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate /*
757*0Sstevel@tonic-gate  * thread to perform callbacks on the shared queue
758*0Sstevel@tonic-gate  */
759*0Sstevel@tonic-gate static void
760*0Sstevel@tonic-gate hcdi_shared_cb_thread(void *arg)
761*0Sstevel@tonic-gate {
762*0Sstevel@tonic-gate 	usba_req_wrapper_t *req_wrp = (usba_req_wrapper_t *)arg;
763*0Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph_data = req_wrp->wr_ph_data;
764*0Sstevel@tonic-gate 	usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
765*0Sstevel@tonic-gate 	usba_hcdi_t		*hcdi = usba_hcdi_get_hcdi(ph_data->
766*0Sstevel@tonic-gate 				p_usba_device->usb_root_hub_dip);
767*0Sstevel@tonic-gate 	/*
768*0Sstevel@tonic-gate 	 * hold the ph_data. we can't use usba_hold_ph_data() since
769*0Sstevel@tonic-gate 	 * it will return NULL if we are closing the pipe which would
770*0Sstevel@tonic-gate 	 * then leave all requests stuck in the cb_queue
771*0Sstevel@tonic-gate 	 */
772*0Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
773*0Sstevel@tonic-gate 	ph_impl->usba_ph_ref_count++;
774*0Sstevel@tonic-gate 
775*0Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
776*0Sstevel@tonic-gate 	    "hcdi_shared_cb_thread: ph_data=0x%p ref=%d req=0x%p",
777*0Sstevel@tonic-gate 	    ph_data, ph_impl->usba_ph_ref_count, req_wrp);
778*0Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate 	/* do the callback */
781*0Sstevel@tonic-gate 	mutex_enter(&ph_data->p_mutex);
782*0Sstevel@tonic-gate 	hcdi_do_cb(ph_data, req_wrp, hcdi);
783*0Sstevel@tonic-gate 	mutex_exit(&ph_data->p_mutex);
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
786*0Sstevel@tonic-gate 	    "hcdi_cb_thread done: ph_data=0x%p", ph_data);
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	usba_release_ph_data(ph_impl);
789*0Sstevel@tonic-gate }
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate /*
793*0Sstevel@tonic-gate  * soft interrupt handler
794*0Sstevel@tonic-gate  */
795*0Sstevel@tonic-gate /*ARGSUSED*/
796*0Sstevel@tonic-gate static uint_t
797*0Sstevel@tonic-gate hcdi_soft_intr(caddr_t arg1, caddr_t arg2)
798*0Sstevel@tonic-gate {
799*0Sstevel@tonic-gate 	usba_hcdi_t		*hcdi = (usba_hcdi_t *)arg1;
800*0Sstevel@tonic-gate 	usba_req_wrapper_t	*req_wrp;
801*0Sstevel@tonic-gate 	int			count = 0;
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	while ((req_wrp = (usba_req_wrapper_t *)
804*0Sstevel@tonic-gate 	    usba_rm_first_pvt_from_list(&hcdi->hcdi_cb_queue)) != NULL) {
805*0Sstevel@tonic-gate 		usba_pipe_handle_data_t *ph_data = req_wrp->wr_ph_data;
806*0Sstevel@tonic-gate 		usba_ph_impl_t		*ph_impl = ph_data->p_ph_impl;
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 		/* hold the pipe */
809*0Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
810*0Sstevel@tonic-gate 		ph_impl->usba_ph_ref_count++;
811*0Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 		/* do the callback */
814*0Sstevel@tonic-gate 		usba_req_normal_cb(req_wrp);
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate 		/* decrement the soft interrupt count */
817*0Sstevel@tonic-gate 		mutex_enter(&ph_data->p_mutex);
818*0Sstevel@tonic-gate 		ph_data->p_soft_intr--;
819*0Sstevel@tonic-gate 		mutex_exit(&ph_data->p_mutex);
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 		/* release the pipe */
822*0Sstevel@tonic-gate 		mutex_enter(&ph_impl->usba_ph_mutex);
823*0Sstevel@tonic-gate 		ph_impl->usba_ph_ref_count--;
824*0Sstevel@tonic-gate 		mutex_exit(&ph_impl->usba_ph_mutex);
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 		count++;
827*0Sstevel@tonic-gate 	}
828*0Sstevel@tonic-gate 
829*0Sstevel@tonic-gate 	return (count == 0 ? DDI_INTR_UNCLAIMED : DDI_INTR_CLAIMED);
830*0Sstevel@tonic-gate }
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate /*
834*0Sstevel@tonic-gate  * hcdi_autoclearing:
835*0Sstevel@tonic-gate  *	This function is called under the taskq context. It
836*0Sstevel@tonic-gate  *	resets the pipe, and clears the stall, if necessary
837*0Sstevel@tonic-gate  */
838*0Sstevel@tonic-gate static void
839*0Sstevel@tonic-gate hcdi_autoclearing(usba_req_wrapper_t *req_wrp)
840*0Sstevel@tonic-gate {
841*0Sstevel@tonic-gate 	usb_cr_t		cr = req_wrp->wr_cr;
842*0Sstevel@tonic-gate 	usb_pipe_handle_t	pipe_handle, def_pipe_handle;
843*0Sstevel@tonic-gate 	usb_cr_t		completion_reason;
844*0Sstevel@tonic-gate 	usb_cb_flags_t		cb_flags;
845*0Sstevel@tonic-gate 	int			rval;
846*0Sstevel@tonic-gate 	usba_device_t		*usba_device =
847*0Sstevel@tonic-gate 				    req_wrp->wr_ph_data->p_usba_device;
848*0Sstevel@tonic-gate 	usba_hcdi_t		*hcdi = usba_hcdi_get_hcdi(
849*0Sstevel@tonic-gate 				    usba_device->usb_root_hub_dip);
850*0Sstevel@tonic-gate 	usb_req_attrs_t		attrs = req_wrp->wr_attrs;
851*0Sstevel@tonic-gate 
852*0Sstevel@tonic-gate 	USB_DPRINTF_L4(DPRINT_MASK_HCDI, hcdi->hcdi_log_handle,
853*0Sstevel@tonic-gate 	    "hcdi_autoclearing: wrp=0x%p", req_wrp);
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate 	pipe_handle = usba_get_pipe_handle(req_wrp->wr_ph_data);
856*0Sstevel@tonic-gate 	def_pipe_handle = usba_get_dflt_pipe_handle(req_wrp->wr_ph_data->p_dip);
857*0Sstevel@tonic-gate 
858*0Sstevel@tonic-gate 	/*
859*0Sstevel@tonic-gate 	 * first reset the pipe synchronously
860*0Sstevel@tonic-gate 	 */
861*0Sstevel@tonic-gate 	if ((attrs & USB_ATTRS_PIPE_RESET) == USB_ATTRS_PIPE_RESET) {
862*0Sstevel@tonic-gate 		usba_pipe_clear(pipe_handle);
863*0Sstevel@tonic-gate 		usba_req_set_cb_flags(req_wrp, USB_CB_RESET_PIPE);
864*0Sstevel@tonic-gate 	}
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate 	ASSERT(def_pipe_handle);
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	/* Do not clear if this request was a usb_get_status request */
869*0Sstevel@tonic-gate 	if ((pipe_handle == def_pipe_handle) &&
870*0Sstevel@tonic-gate 	    (USBA_WRP2CTRL_REQ(req_wrp)->ctrl_bRequest ==
871*0Sstevel@tonic-gate 	    USB_REQ_GET_STATUS)) {
872*0Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, hcdi->hcdi_log_handle,
873*0Sstevel@tonic-gate 		    "hcdi_autoclearing: usb_get_status failed, no clearing");
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 	/* if default pipe and stall no auto clearing */
876*0Sstevel@tonic-gate 	} else if ((pipe_handle == def_pipe_handle) && (cr == USB_CR_STALL)) {
877*0Sstevel@tonic-gate 		USB_DPRINTF_L2(DPRINT_MASK_USBAI, hcdi->hcdi_log_handle,
878*0Sstevel@tonic-gate 		    "hcdi_autoclearing: default pipe stalled, no clearing");
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 		usba_req_set_cb_flags(req_wrp, USB_CB_PROTOCOL_STALL);
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	/* else do auto clearing */
883*0Sstevel@tonic-gate 	} else if (((attrs & USB_ATTRS_AUTOCLEARING) ==
884*0Sstevel@tonic-gate 	    USB_ATTRS_AUTOCLEARING) && (cr == USB_CR_STALL)) {
885*0Sstevel@tonic-gate 		ushort_t status = 0;
886*0Sstevel@tonic-gate 
887*0Sstevel@tonic-gate 		rval = usb_get_status(req_wrp->wr_dip, def_pipe_handle,
888*0Sstevel@tonic-gate 		    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_RCPT_EP,
889*0Sstevel@tonic-gate 		    req_wrp->wr_ph_data->p_ep.bEndpointAddress,
890*0Sstevel@tonic-gate 		    &status, USB_FLAGS_SLEEP);
891*0Sstevel@tonic-gate 		if (rval != USB_SUCCESS) {
892*0Sstevel@tonic-gate 			USB_DPRINTF_L2(DPRINT_MASK_USBAI, hcdi->hcdi_log_handle,
893*0Sstevel@tonic-gate 			    "get status (STALL) failed: rval=%d", rval);
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate 			usba_pipe_clear(def_pipe_handle);
896*0Sstevel@tonic-gate 		}
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 		if ((rval != USB_SUCCESS) ||
899*0Sstevel@tonic-gate 		    (status & USB_EP_HALT_STATUS)) {
900*0Sstevel@tonic-gate 			usba_req_set_cb_flags(req_wrp, USB_CB_FUNCTIONAL_STALL);
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 			if ((rval = usb_pipe_sync_ctrl_xfer(
903*0Sstevel@tonic-gate 			    req_wrp->wr_dip, def_pipe_handle,
904*0Sstevel@tonic-gate 			    USB_DEV_REQ_HOST_TO_DEV |
905*0Sstevel@tonic-gate 			    USB_DEV_REQ_RCPT_EP,
906*0Sstevel@tonic-gate 			    USB_REQ_CLEAR_FEATURE,
907*0Sstevel@tonic-gate 			    0,
908*0Sstevel@tonic-gate 			    req_wrp->wr_ph_data->p_ep.bEndpointAddress,
909*0Sstevel@tonic-gate 			    0,
910*0Sstevel@tonic-gate 			    NULL, 0,
911*0Sstevel@tonic-gate 			    &completion_reason,
912*0Sstevel@tonic-gate 			    &cb_flags, USB_FLAGS_SLEEP)) != USB_SUCCESS) {
913*0Sstevel@tonic-gate 				USB_DPRINTF_L2(DPRINT_MASK_USBAI,
914*0Sstevel@tonic-gate 				    hcdi->hcdi_log_handle,
915*0Sstevel@tonic-gate 				    "auto clearing (STALL) failed: "
916*0Sstevel@tonic-gate 				    "rval=%d, cr=0x%x cb=0x%x",
917*0Sstevel@tonic-gate 				    rval, completion_reason, cb_flags);
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 				usba_pipe_clear(def_pipe_handle);
920*0Sstevel@tonic-gate 			} else {
921*0Sstevel@tonic-gate 				usba_req_set_cb_flags(req_wrp,
922*0Sstevel@tonic-gate 						USB_CB_STALL_CLEARED);
923*0Sstevel@tonic-gate 			}
924*0Sstevel@tonic-gate 		} else {
925*0Sstevel@tonic-gate 			usba_req_set_cb_flags(req_wrp, USB_CB_PROTOCOL_STALL);
926*0Sstevel@tonic-gate 		}
927*0Sstevel@tonic-gate 	}
928*0Sstevel@tonic-gate }
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate /*
932*0Sstevel@tonic-gate  * usba_hcdi_get_req_private:
933*0Sstevel@tonic-gate  *	This function is used to get the HCD private field
934*0Sstevel@tonic-gate  *	maintained by USBA. HCD calls this function.
935*0Sstevel@tonic-gate  *
936*0Sstevel@tonic-gate  * Arguments:
937*0Sstevel@tonic-gate  *	req		- pointer to usb_*_req_t
938*0Sstevel@tonic-gate  *
939*0Sstevel@tonic-gate  * Return Values:
940*0Sstevel@tonic-gate  *	wr_hcd_private field from wrapper
941*0Sstevel@tonic-gate  */
942*0Sstevel@tonic-gate usb_opaque_t
943*0Sstevel@tonic-gate usba_hcdi_get_req_private(usb_opaque_t req)
944*0Sstevel@tonic-gate {
945*0Sstevel@tonic-gate 	usba_req_wrapper_t *wrp = USBA_REQ2WRP(req);
946*0Sstevel@tonic-gate 
947*0Sstevel@tonic-gate 	return (wrp->wr_hcd_private);
948*0Sstevel@tonic-gate }
949*0Sstevel@tonic-gate 
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate /*
952*0Sstevel@tonic-gate  * usba_hcdi_set_req_private:
953*0Sstevel@tonic-gate  *	This function is used to set the HCD private field
954*0Sstevel@tonic-gate  *	maintained by USBA. HCD calls this function.
955*0Sstevel@tonic-gate  *
956*0Sstevel@tonic-gate  * Arguments:
957*0Sstevel@tonic-gate  *	req		- pointer to usb_*_req_t
958*0Sstevel@tonic-gate  *	hcd_private	- wr_hcd_private field from wrapper
959*0Sstevel@tonic-gate  */
960*0Sstevel@tonic-gate void
961*0Sstevel@tonic-gate usba_hcdi_set_req_private(usb_opaque_t req,
962*0Sstevel@tonic-gate 			usb_opaque_t	hcd_private)
963*0Sstevel@tonic-gate {
964*0Sstevel@tonic-gate 	usba_req_wrapper_t *wrp = USBA_REQ2WRP(req);
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate 	wrp->wr_hcd_private = hcd_private;
967*0Sstevel@tonic-gate }
968*0Sstevel@tonic-gate 
969*0Sstevel@tonic-gate 
970*0Sstevel@tonic-gate /* get data toggle information for this endpoint */
971*0Sstevel@tonic-gate uchar_t
972*0Sstevel@tonic-gate usba_hcdi_get_data_toggle(usba_device_t *usba_device, uint8_t ep_addr)
973*0Sstevel@tonic-gate {
974*0Sstevel@tonic-gate 	uchar_t		toggle;
975*0Sstevel@tonic-gate 	usba_ph_impl_t	*ph_impl;
976*0Sstevel@tonic-gate 	int		ep_index;
977*0Sstevel@tonic-gate 
978*0Sstevel@tonic-gate 	ep_index = usb_get_ep_index(ep_addr);
979*0Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
980*0Sstevel@tonic-gate 	ph_impl = &usba_device->usb_ph_list[ep_index];
981*0Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
982*0Sstevel@tonic-gate 	toggle = (uchar_t)(ph_impl->usba_ph_flags & USBA_PH_DATA_TOGGLE);
983*0Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
984*0Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
985*0Sstevel@tonic-gate 
986*0Sstevel@tonic-gate 	return (toggle);
987*0Sstevel@tonic-gate }
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate /* set data toggle information for this endpoint */
991*0Sstevel@tonic-gate void
992*0Sstevel@tonic-gate usba_hcdi_set_data_toggle(usba_device_t *usba_device, uint8_t ep_addr,
993*0Sstevel@tonic-gate     uchar_t toggle)
994*0Sstevel@tonic-gate {
995*0Sstevel@tonic-gate 	usba_ph_impl_t	*ph_impl;
996*0Sstevel@tonic-gate 	int		ep_index;
997*0Sstevel@tonic-gate 
998*0Sstevel@tonic-gate 	ep_index = usb_get_ep_index(ep_addr);
999*0Sstevel@tonic-gate 	mutex_enter(&usba_device->usb_mutex);
1000*0Sstevel@tonic-gate 	ph_impl = &usba_device->usb_ph_list[ep_index];
1001*0Sstevel@tonic-gate 	mutex_enter(&ph_impl->usba_ph_mutex);
1002*0Sstevel@tonic-gate 	ph_impl->usba_ph_flags &= ~USBA_PH_DATA_TOGGLE;
1003*0Sstevel@tonic-gate 	ph_impl->usba_ph_flags |= (USBA_PH_DATA_TOGGLE & toggle);
1004*0Sstevel@tonic-gate 	mutex_exit(&ph_impl->usba_ph_mutex);
1005*0Sstevel@tonic-gate 	mutex_exit(&usba_device->usb_mutex);
1006*0Sstevel@tonic-gate }
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate /* get pipe_handle_impl ptr for this ep */
1010*0Sstevel@tonic-gate usba_pipe_handle_data_t *
1011*0Sstevel@tonic-gate usba_hcdi_get_ph_data(usba_device_t *usba_device, uint8_t ep_addr)
1012*0Sstevel@tonic-gate {
1013*0Sstevel@tonic-gate 	return (usba_device->usb_ph_list[usb_get_ep_index(ep_addr)].
1014*0Sstevel@tonic-gate 							usba_ph_data);
1015*0Sstevel@tonic-gate }
1016