xref: /illumos-gate/usr/src/uts/common/io/usb/hcd/ehci/ehci.c (revision fcf3ce44)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 /*
28  * EHCI Host Controller Driver (EHCI)
29  *
30  * The EHCI driver is a software driver which interfaces to the Universal
31  * Serial Bus layer (USBA) and the Host Controller (HC). The interface to
32  * the Host Controller is defined by the EHCI Host Controller Interface.
33  *
34  * This file contains code for Auto-configuration and HCDI entry points.
35  *
36  * NOTE:
37  *
38  * Currently EHCI driver does not support the following features
39  *
40  * - Alternate QTD for short xfer condition is only used in Bulk xfers.
41  * - Frame Span Traversal Nodes (FSTN).
42  * - Bandwidth allocation scheme needs to be updated for FSTN and USB2.0
43  *   or High speed hub with multiple TT implementation. Currently bandwidth
44  *   allocation scheme assumes one TT per USB2.0 or High speed hub.
45  * - 64 bit addressing capability.
46  * - Programmable periodic frame list size like 256, 512, 1024.
47  *   It supports only 1024 periodic frame list size.
48  */
49 
50 #include <sys/usb/hcd/ehci/ehcid.h>
51 #include <sys/usb/hcd/ehci/ehci_xfer.h>
52 #include <sys/usb/hcd/ehci/ehci_intr.h>
53 #include <sys/usb/hcd/ehci/ehci_util.h>
54 #include <sys/usb/hcd/ehci/ehci_isoch.h>
55 
56 /* Pointer to the state structure */
57 void *ehci_statep;
58 
59 /* Number of instances */
60 #define	EHCI_INSTS	1
61 
62 /* Debugging information */
63 uint_t ehci_errmask	= (uint_t)PRINT_MASK_ALL;
64 uint_t ehci_errlevel	= USB_LOG_L2;
65 uint_t ehci_instance_debug = (uint_t)-1;
66 
67 /*
68  * Tunable to ensure host controller goes off even if a keyboard is attached.
69  */
70 int force_ehci_off = 1;
71 
72 /* Enable all workarounds for VIA VT62x2 */
73 uint_t ehci_vt62x2_workaround = EHCI_VIA_WORKAROUNDS;
74 
75 /*
76  * EHCI Auto-configuration entry points.
77  *
78  * Device operations (dev_ops) entries function prototypes.
79  *
80  * We use the hub cbops since all nexus ioctl operations defined so far will
81  * be executed by the root hub. The following are the Host Controller Driver
82  * (HCD) entry points.
83  *
84  * the open/close/ioctl functions call the corresponding usba_hubdi_*
85  * calls after looking up the dip thru the dev_t.
86  */
87 static int	ehci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
88 static int	ehci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
89 static int	ehci_reset(dev_info_t *dip, ddi_reset_cmd_t cmd);
90 static int	ehci_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
91 				void *arg, void **result);
92 
93 static int	ehci_open(dev_t	*devp, int flags, int otyp, cred_t *credp);
94 static int	ehci_close(dev_t dev, int flag, int otyp, cred_t *credp);
95 static int	ehci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
96     cred_t *credp, int *rvalp);
97 
98 int		usba_hubdi_root_hub_power(dev_info_t *dip, int comp, int level);
99 static int	ehci_quiesce(dev_info_t *dip);
100 
101 static struct cb_ops ehci_cb_ops = {
102 	ehci_open,			/* EHCI */
103 	ehci_close,			/* Close */
104 	nodev,				/* Strategy */
105 	nodev,				/* Print */
106 	nodev,				/* Dump */
107 	nodev,				/* Read */
108 	nodev,				/* Write */
109 	ehci_ioctl,			/* Ioctl */
110 	nodev,				/* Devmap */
111 	nodev,				/* Mmap */
112 	nodev,				/* Segmap */
113 	nochpoll,			/* Poll */
114 	ddi_prop_op,			/* cb_prop_op */
115 	NULL,				/* Streamtab */
116 	D_NEW | D_MP | D_HOTPLUG	/* Driver compatibility flag */
117 };
118 
119 static struct dev_ops ehci_ops = {
120 	DEVO_REV,			/* Devo_rev */
121 	0,				/* Refcnt */
122 	ehci_info,			/* Info */
123 	nulldev,			/* Identify */
124 	nulldev,			/* Probe */
125 	ehci_attach,			/* Attach */
126 	ehci_detach,			/* Detach */
127 	ehci_reset,			/* Reset */
128 	&ehci_cb_ops,			/* Driver operations */
129 	&usba_hubdi_busops,		/* Bus operations */
130 	usba_hubdi_root_hub_power,	/* Power */
131 	ehci_quiesce			/* Quiesce */
132 };
133 
134 /*
135  * The USBA library must be loaded for this driver.
136  */
137 static struct modldrv modldrv = {
138 	&mod_driverops, 	/* Type of module. This one is a driver */
139 	"USB EHCI Driver", /* Name of the module. */
140 	&ehci_ops,		/* Driver ops */
141 };
142 
143 static struct modlinkage modlinkage = {
144 	MODREV_1, (void *)&modldrv, NULL
145 };
146 
147 
148 int
149 _init(void)
150 {
151 	int error;
152 
153 	/* Initialize the soft state structures */
154 	if ((error = ddi_soft_state_init(&ehci_statep, sizeof (ehci_state_t),
155 	    EHCI_INSTS)) != 0) {
156 		return (error);
157 	}
158 
159 	/* Install the loadable module */
160 	if ((error = mod_install(&modlinkage)) != 0) {
161 		ddi_soft_state_fini(&ehci_statep);
162 	}
163 
164 	return (error);
165 }
166 
167 
168 int
169 _info(struct modinfo *modinfop)
170 {
171 	return (mod_info(&modlinkage, modinfop));
172 }
173 
174 
175 int
176 _fini(void)
177 {
178 	int error;
179 
180 	if ((error = mod_remove(&modlinkage)) == 0) {
181 
182 		/* Release per module resources */
183 		ddi_soft_state_fini(&ehci_statep);
184 	}
185 
186 	return (error);
187 }
188 
189 
190 /*
191  * EHCI Auto configuration entry points.
192  */
193 
194 /*
195  * ehci_attach:
196  *
197  * Description: Attach entry point is called by the Kernel.
198  *		Allocates resources for each EHCI host controller instance.
199  *		Initializes the EHCI Host Controller.
200  *
201  * Return     : DDI_SUCCESS / DDI_FAILURE.
202  */
203 static int
204 ehci_attach(dev_info_t		*dip,
205 	ddi_attach_cmd_t	cmd)
206 {
207 	int			instance;
208 	ehci_state_t		*ehcip = NULL;
209 	usba_hcdi_register_args_t hcdi_args;
210 
211 	switch (cmd) {
212 	case DDI_ATTACH:
213 		break;
214 	case DDI_RESUME:
215 		ehcip = ehci_obtain_state(dip);
216 
217 		return (ehci_cpr_resume(ehcip));
218 	default:
219 		return (DDI_FAILURE);
220 	}
221 
222 	/* Get the instance and create soft state */
223 	instance = ddi_get_instance(dip);
224 
225 	if (ddi_soft_state_zalloc(ehci_statep, instance) != 0) {
226 
227 		return (DDI_FAILURE);
228 	}
229 
230 	ehcip = ddi_get_soft_state(ehci_statep, instance);
231 	if (ehcip == NULL) {
232 
233 		return (DDI_FAILURE);
234 	}
235 
236 	ehcip->ehci_flags = EHCI_ATTACH;
237 
238 	ehcip->ehci_log_hdl = usb_alloc_log_hdl(dip, "ehci", &ehci_errlevel,
239 	    &ehci_errmask, &ehci_instance_debug, 0);
240 
241 	ehcip->ehci_flags |= EHCI_ZALLOC;
242 
243 	/* Set host controller soft state to initialization */
244 	ehcip->ehci_hc_soft_state = EHCI_CTLR_INIT_STATE;
245 
246 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
247 	    "ehcip = 0x%p", (void *)ehcip);
248 
249 	/* Save the dip and instance */
250 	ehcip->ehci_dip = dip;
251 	ehcip->ehci_instance = instance;
252 
253 	/* Map the registers */
254 	if (ehci_map_regs(ehcip) != DDI_SUCCESS) {
255 		(void) ehci_cleanup(ehcip);
256 
257 		return (DDI_FAILURE);
258 	}
259 
260 	/* Get the ehci chip vendor and device id */
261 	ehcip->ehci_vendor_id = pci_config_get16(
262 	    ehcip->ehci_config_handle, PCI_CONF_VENID);
263 	ehcip->ehci_device_id = pci_config_get16(
264 	    ehcip->ehci_config_handle, PCI_CONF_DEVID);
265 	ehcip->ehci_rev_id = pci_config_get8(
266 	    ehcip->ehci_config_handle, PCI_CONF_REVID);
267 
268 	/* Initialize the DMA attributes */
269 	ehci_set_dma_attributes(ehcip);
270 
271 	/* Initialize kstat structures */
272 	ehci_create_stats(ehcip);
273 
274 	/* Create the qtd and qh pools */
275 	if (ehci_allocate_pools(ehcip) != DDI_SUCCESS) {
276 		(void) ehci_cleanup(ehcip);
277 
278 		return (DDI_FAILURE);
279 	}
280 
281 	/* Initialize the isochronous resources */
282 	if (ehci_isoc_init(ehcip) != DDI_SUCCESS) {
283 		(void) ehci_cleanup(ehcip);
284 
285 		return (DDI_FAILURE);
286 	}
287 
288 	/* Register interrupts */
289 	if (ehci_register_intrs_and_init_mutex(ehcip) != DDI_SUCCESS) {
290 		(void) ehci_cleanup(ehcip);
291 
292 		return (DDI_FAILURE);
293 	}
294 
295 	mutex_enter(&ehcip->ehci_int_mutex);
296 
297 	/* Initialize the controller */
298 	if (ehci_init_ctlr(ehcip, EHCI_NORMAL_INITIALIZATION) != DDI_SUCCESS) {
299 		mutex_exit(&ehcip->ehci_int_mutex);
300 		(void) ehci_cleanup(ehcip);
301 
302 		return (DDI_FAILURE);
303 	}
304 
305 	/*
306 	 * At this point, the hardware will be okay.
307 	 * Initialize the usba_hcdi structure
308 	 */
309 	ehcip->ehci_hcdi_ops = ehci_alloc_hcdi_ops(ehcip);
310 
311 	mutex_exit(&ehcip->ehci_int_mutex);
312 
313 	/*
314 	 * Make this HCD instance known to USBA
315 	 * (dma_attr must be passed for USBA busctl's)
316 	 */
317 	hcdi_args.usba_hcdi_register_version = HCDI_REGISTER_VERSION;
318 	hcdi_args.usba_hcdi_register_dip = dip;
319 	hcdi_args.usba_hcdi_register_ops = ehcip->ehci_hcdi_ops;
320 	hcdi_args.usba_hcdi_register_dma_attr = &ehcip->ehci_dma_attr;
321 
322 	/*
323 	 * Priority and iblock_cookie are one and the same
324 	 * (However, retaining hcdi_soft_iblock_cookie for now
325 	 * assigning it w/ priority. In future all iblock_cookie
326 	 * could just go)
327 	 */
328 	hcdi_args.usba_hcdi_register_iblock_cookie =
329 	    (ddi_iblock_cookie_t)(uintptr_t)ehcip->ehci_intr_pri;
330 
331 	if (usba_hcdi_register(&hcdi_args, 0) != DDI_SUCCESS) {
332 		(void) ehci_cleanup(ehcip);
333 
334 		return (DDI_FAILURE);
335 	}
336 
337 	ehcip->ehci_flags |= EHCI_USBAREG;
338 
339 	mutex_enter(&ehcip->ehci_int_mutex);
340 
341 	if ((ehci_init_root_hub(ehcip)) != USB_SUCCESS) {
342 		mutex_exit(&ehcip->ehci_int_mutex);
343 		(void) ehci_cleanup(ehcip);
344 
345 		return (DDI_FAILURE);
346 	}
347 
348 	mutex_exit(&ehcip->ehci_int_mutex);
349 
350 	/* Finally load the root hub driver */
351 	if (ehci_load_root_hub_driver(ehcip) != USB_SUCCESS) {
352 		(void) ehci_cleanup(ehcip);
353 
354 		return (DDI_FAILURE);
355 	}
356 	ehcip->ehci_flags |= EHCI_RHREG;
357 
358 	/* Display information in the banner */
359 	ddi_report_dev(dip);
360 
361 	mutex_enter(&ehcip->ehci_int_mutex);
362 
363 	/* Reset the ehci initialization flag */
364 	ehcip->ehci_flags &= ~EHCI_ATTACH;
365 
366 	/* Print the Host Control's Operational registers */
367 	ehci_print_caps(ehcip);
368 	ehci_print_regs(ehcip);
369 
370 	(void) pci_report_pmcap(dip, PCI_PM_IDLESPEED, (void *)4000);
371 
372 	mutex_exit(&ehcip->ehci_int_mutex);
373 
374 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl,
375 	    "ehci_attach: dip = 0x%p done", (void *)dip);
376 
377 	return (DDI_SUCCESS);
378 }
379 
380 
381 /*
382  * ehci_detach:
383  *
384  * Description: Detach entry point is called by the Kernel.
385  *		Deallocates all resource allocated.
386  *		Unregisters the interrupt handler.
387  *
388  * Return     : DDI_SUCCESS / DDI_FAILURE
389  */
390 int
391 ehci_detach(dev_info_t		*dip,
392 	ddi_detach_cmd_t	cmd)
393 {
394 	ehci_state_t		*ehcip = ehci_obtain_state(dip);
395 
396 	USB_DPRINTF_L4(PRINT_MASK_ATTA, ehcip->ehci_log_hdl, "ehci_detach:");
397 
398 	switch (cmd) {
399 	case DDI_DETACH:
400 
401 		return (ehci_cleanup(ehcip));
402 	case DDI_SUSPEND:
403 
404 		return (ehci_cpr_suspend(ehcip));
405 	default:
406 
407 		return (DDI_FAILURE);
408 	}
409 }
410 
411 /*
412  * ehci_reset:
413  *
414  * Description:	Reset entry point - called by the Kernel
415  *		on the way down.
416  *		Toshiba Tecra laptop has been observed to hang
417  *		on soft reboot. The resetting ehci on the way
418  *		down solves the problem.
419  *
420  * Return	: DDI_SUCCESS / DDI_FAILURE
421  */
422 /* ARGSUSED */
423 static int
424 ehci_reset(dev_info_t *dip, ddi_reset_cmd_t cmd)
425 {
426 #if defined(__sparc)
427 	/*
428 	 * Don't reset the host controller on SPARC, for OBP needs Solaris
429 	 * to continue to provide keyboard support after shutdown of SPARC,
430 	 * or the keyboard connected to a USB 2.0 port will not work after
431 	 * that. The incomplete reset problem on Toshiba Tecra laptop is
432 	 * specific to Tecra laptop or BIOS, not present on SPARC. The SPARC
433 	 * OBP guarantees good reset behavior during startup.
434 	 */
435 	return (DDI_SUCCESS);
436 #else
437 	ehci_state_t		*ehcip = ehci_obtain_state(dip);
438 
439 	mutex_enter(&ehcip->ehci_int_mutex);
440 
441 	/*
442 	 * To reset the host controller, the HCRESET bit should be set to one.
443 	 * Software should not set this bit to a one when the HCHalted bit in
444 	 * the USBSTS register is a zero. Attempting to reset an actively
445 	 * running host controller will result in undefined behavior.
446 	 * see EHCI SPEC. for more information.
447 	 */
448 	if (!(Get_OpReg(ehci_status) & EHCI_STS_HOST_CTRL_HALTED)) {
449 
450 		/* Stop the EHCI host controller */
451 		Set_OpReg(ehci_command,
452 		    Get_OpReg(ehci_command) & ~EHCI_CMD_HOST_CTRL_RUN);
453 		/*
454 		 * When this bit is set to 0, the Host Controller completes the
455 		 * current and any actively pipelined transactions on the USB
456 		 * and then halts. The Host Controller must halt within 16
457 		 * micro-frames after software clears the Run bit.
458 		 * The HC Halted bit in the status register indicates when the
459 		 * Host Controller has finished its pending pipelined
460 		 * transactions and has entered the stopped state.
461 		 */
462 		drv_usecwait(EHCI_RESET_TIMEWAIT);
463 	}
464 
465 	/* Reset the EHCI host controller */
466 	Set_OpReg(ehci_command,
467 	    Get_OpReg(ehci_command) | EHCI_CMD_HOST_CTRL_RESET);
468 
469 	mutex_exit(&ehcip->ehci_int_mutex);
470 
471 	return (DDI_SUCCESS);
472 #endif
473 }
474 
475 /*
476  * quiesce(9E) entry point.
477  *
478  * This function is called when the system is single-threaded at high
479  * PIL with preemption disabled. Therefore, this function must not be
480  * blocked.
481  *
482  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
483  * DDI_FAILURE indicates an error condition and should almost never happen.
484  */
485 static int
486 ehci_quiesce(dev_info_t *dip)
487 {
488 	ehci_state_t		*ehcip = ehci_obtain_state(dip);
489 
490 	if (ehcip == NULL)
491 		return (DDI_FAILURE);
492 
493 	/*
494 	 * To reset the host controller, the HCRESET bit should be set to one.
495 	 * Software should not set this bit to a one when the HCHalted bit in
496 	 * the USBSTS register is a zero. Attempting to reset an actively
497 	 * running host controller will result in undefined behavior.
498 	 * see EHCI SPEC. for more information.
499 	 */
500 	if (!(Get_OpReg(ehci_status) & EHCI_STS_HOST_CTRL_HALTED)) {
501 
502 		/* Stop the EHCI host controller */
503 		Set_OpReg(ehci_command,
504 		    Get_OpReg(ehci_command) & ~EHCI_CMD_HOST_CTRL_RUN);
505 		/*
506 		 * When this bit is set to 0, the Host Controller completes the
507 		 * current and any actively pipelined transactions on the USB
508 		 * and then halts. The Host Controller must halt within 16
509 		 * micro-frames after software clears the Run bit.
510 		 * The HC Halted bit in the status register indicates when the
511 		 * Host Controller has finished its pending pipelined
512 		 * transactions and has entered the stopped state.
513 		 */
514 		drv_usecwait(EHCI_RESET_TIMEWAIT);
515 	}
516 
517 	/* Reset the EHCI host controller */
518 	Set_OpReg(ehci_command,
519 	    Get_OpReg(ehci_command) | EHCI_CMD_HOST_CTRL_RESET);
520 
521 	return (DDI_SUCCESS);
522 }
523 
524 
525 /*
526  * ehci_info:
527  */
528 /* ARGSUSED */
529 static int
530 ehci_info(dev_info_t		*dip,
531 	ddi_info_cmd_t		infocmd,
532 	void			*arg,
533 	void			**result)
534 {
535 	dev_t			dev;
536 	ehci_state_t		*ehcip;
537 	int			instance;
538 	int			error = DDI_FAILURE;
539 
540 	switch (infocmd) {
541 	case DDI_INFO_DEVT2DEVINFO:
542 		dev = (dev_t)arg;
543 		instance = EHCI_UNIT(dev);
544 		ehcip = ddi_get_soft_state(ehci_statep, instance);
545 		if (ehcip != NULL) {
546 			*result = (void *)ehcip->ehci_dip;
547 			if (*result != NULL) {
548 				error = DDI_SUCCESS;
549 			}
550 		} else {
551 			*result = NULL;
552 		}
553 
554 		break;
555 	case DDI_INFO_DEVT2INSTANCE:
556 		dev = (dev_t)arg;
557 		instance = EHCI_UNIT(dev);
558 		*result = (void *)(uintptr_t)instance;
559 		error = DDI_SUCCESS;
560 		break;
561 	default:
562 		break;
563 	}
564 
565 	return (error);
566 }
567 
568 
569 /*
570  * EHCI CB_OPS entry points.
571  */
572 static dev_info_t *
573 ehci_get_dip(dev_t	dev)
574 {
575 	int		instance = EHCI_UNIT(dev);
576 	ehci_state_t	*ehcip = ddi_get_soft_state(ehci_statep, instance);
577 
578 	if (ehcip) {
579 
580 		return (ehcip->ehci_dip);
581 	} else {
582 
583 		return (NULL);
584 	}
585 }
586 
587 
588 static int
589 ehci_open(dev_t		*devp,
590 	int		flags,
591 	int		otyp,
592 	cred_t		*credp)
593 {
594 	dev_info_t	*dip = ehci_get_dip(*devp);
595 
596 	return (usba_hubdi_open(dip, devp, flags, otyp, credp));
597 }
598 
599 
600 static int
601 ehci_close(dev_t	dev,
602 	int		flag,
603 	int		otyp,
604 	cred_t		*credp)
605 {
606 	dev_info_t	*dip = ehci_get_dip(dev);
607 
608 	return (usba_hubdi_close(dip, dev, flag, otyp, credp));
609 }
610 
611 
612 static int
613 ehci_ioctl(dev_t	dev,
614 	int		cmd,
615 	intptr_t	arg,
616 	int		mode,
617 	cred_t		*credp,
618 	int		*rvalp)
619 {
620 	dev_info_t	*dip = ehci_get_dip(dev);
621 
622 	return (usba_hubdi_ioctl(dip,
623 	    dev, cmd, arg, mode, credp, rvalp));
624 }
625 
626 /*
627  * EHCI Interrupt Handler entry point.
628  */
629 
630 /*
631  * ehci_intr:
632  *
633  * EHCI (EHCI) interrupt handling routine.
634  */
635 uint_t
636 ehci_intr(caddr_t arg1, caddr_t arg2)
637 {
638 	uint_t			intr;
639 	ehci_state_t		*ehcip = (void *)arg1;
640 
641 	USB_DPRINTF_L4(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
642 	    "ehci_intr: Interrupt occurred, arg1 0x%p arg2 0x%p",
643 	    (void *)arg1, (void *)arg2);
644 
645 	/* Get the ehci global mutex */
646 	mutex_enter(&ehcip->ehci_int_mutex);
647 
648 	/* Any interrupt is not handled for the suspended device. */
649 	if (ehcip->ehci_hc_soft_state == EHCI_CTLR_SUSPEND_STATE) {
650 		mutex_exit(&ehcip->ehci_int_mutex);
651 
652 		return (DDI_INTR_UNCLAIMED);
653 	}
654 
655 	/*
656 	 * Now process the actual ehci interrupt events  that caused
657 	 * invocation of this ehci interrupt handler.
658 	 */
659 	intr = (Get_OpReg(ehci_status) & Get_OpReg(ehci_interrupt));
660 
661 	/* Update kstat values */
662 	ehci_do_intrs_stats(ehcip, intr);
663 
664 	/*
665 	 * We could have gotten a spurious interrupts. If so, do not
666 	 * claim it.  This is quite  possible on some  architectures
667 	 * where more than one PCI slots share the IRQs.  If so, the
668 	 * associated driver's interrupt routine may get called even
669 	 * if the interrupt is not meant for them.
670 	 *
671 	 * By unclaiming the interrupt, the other driver gets chance
672 	 * to service its interrupt.
673 	 */
674 	if (!intr) {
675 		mutex_exit(&ehcip->ehci_int_mutex);
676 
677 		return (DDI_INTR_UNCLAIMED);
678 	}
679 
680 	/* Acknowledge the interrupt */
681 	Set_OpReg(ehci_status, intr);
682 
683 	if (ehcip->ehci_hc_soft_state == EHCI_CTLR_ERROR_STATE) {
684 		mutex_exit(&ehcip->ehci_int_mutex);
685 
686 		return (DDI_INTR_CLAIMED);
687 	}
688 
689 	USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
690 	    "Interrupt status 0x%x", intr);
691 
692 	/*
693 	 * If necessary broadcast that an interrupt has occured.  This
694 	 * is only necessary during controller init.
695 	 */
696 	if (ehcip->ehci_flags & EHCI_CV_INTR) {
697 		ehcip->ehci_flags &= ~EHCI_CV_INTR;
698 		cv_broadcast(&ehcip->ehci_async_schedule_advance_cv);
699 	}
700 
701 	/* Check for Frame List Rollover */
702 	if (intr & EHCI_INTR_FRAME_LIST_ROLLOVER) {
703 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
704 		    "ehci_intr: Frame List Rollover");
705 
706 		ehci_handle_frame_list_rollover(ehcip);
707 
708 		/* VIA VT6202 looses EHCI_INTR_USB interrupts, workaround. */
709 		if ((ehcip->ehci_vendor_id == PCI_VENDOR_VIA) &&
710 		    (ehci_vt62x2_workaround & EHCI_VIA_LOST_INTERRUPTS)) {
711 			ehcip->ehci_missed_intr_sts |= EHCI_INTR_USB;
712 		}
713 	}
714 
715 	/* Check for Advance on Asynchronous Schedule */
716 	if (intr & EHCI_INTR_ASYNC_ADVANCE) {
717 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
718 		    "ehci_intr: Asynchronous Schedule Advance Notification");
719 
720 		/* Disable async list advance interrupt */
721 		Set_OpReg(ehci_interrupt,
722 		    (Get_OpReg(ehci_interrupt) & ~EHCI_INTR_ASYNC_ADVANCE));
723 
724 		/*
725 		 * Call cv_broadcast on every this interrupt to wakeup
726 		 * all the threads that are waiting the async list advance
727 		 * event.
728 		 */
729 		cv_broadcast(&ehcip->ehci_async_schedule_advance_cv);
730 	}
731 
732 	/* Always process completed itds */
733 	ehci_traverse_active_isoc_list(ehcip);
734 
735 	/*
736 	 * Check for any USB transaction completion notification. Also
737 	 * process any missed USB transaction completion interrupts.
738 	 */
739 	if ((intr & EHCI_INTR_USB) || (intr & EHCI_INTR_USB_ERROR) ||
740 	    (ehcip->ehci_missed_intr_sts & EHCI_INTR_USB) ||
741 	    (ehcip->ehci_missed_intr_sts & EHCI_INTR_USB_ERROR)) {
742 
743 		USB_DPRINTF_L3(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
744 		    "ehci_intr: USB Transaction Completion Notification");
745 
746 		/* Clear missed interrupts */
747 		if (ehcip->ehci_missed_intr_sts) {
748 			ehcip->ehci_missed_intr_sts = 0;
749 		}
750 
751 		/* Process completed qtds */
752 		ehci_traverse_active_qtd_list(ehcip);
753 	}
754 
755 	/* Process endpoint reclamation list */
756 	if (ehcip->ehci_reclaim_list) {
757 		ehci_handle_endpoint_reclaimation(ehcip);
758 	}
759 
760 	/* Check for Host System Error */
761 	if (intr & EHCI_INTR_HOST_SYSTEM_ERROR) {
762 		USB_DPRINTF_L2(PRINT_MASK_INTR, ehcip->ehci_log_hdl,
763 		    "ehci_intr: Unrecoverable error");
764 
765 		ehci_handle_ue(ehcip);
766 	}
767 
768 	/*
769 	 * Read interrupt status register to make sure that any PIO
770 	 * store to clear the ISR has made it on the PCI bus before
771 	 * returning from its interrupt handler.
772 	 */
773 	(void) Get_OpReg(ehci_status);
774 
775 	/* Release the ehci global mutex */
776 	mutex_exit(&ehcip->ehci_int_mutex);
777 
778 	USB_DPRINTF_L4(PRINT_MASK_INTR,  ehcip->ehci_log_hdl,
779 	    "Interrupt handling completed");
780 
781 	return (DDI_INTR_CLAIMED);
782 }
783 
784 
785 /*
786  * EHCI HCDI entry points
787  *
788  * The Host Controller Driver Interfaces (HCDI) are the software interfaces
789  * between the Universal Serial Bus Layer (USBA) and the Host Controller
790  * Driver (HCD). The HCDI interfaces or entry points are subject to change.
791  */
792 
793 /*
794  * ehci_hcdi_pipe_open:
795  *
796  * Member of HCD Ops structure and called during client specific pipe open
797  * Add the pipe to the data structure representing the device and allocate
798  * bandwidth for the pipe if it is a interrupt or isochronous endpoint.
799  */
800 int
801 ehci_hcdi_pipe_open(
802 	usba_pipe_handle_data_t	*ph,
803 	usb_flags_t		flags)
804 {
805 	ehci_state_t		*ehcip = ehci_obtain_state(
806 	    ph->p_usba_device->usb_root_hub_dip);
807 	usb_ep_descr_t		*epdt = &ph->p_ep;
808 	int			rval, error = USB_SUCCESS;
809 	int			kmflag = (flags & USB_FLAGS_SLEEP) ?
810 	    KM_SLEEP : KM_NOSLEEP;
811 	uchar_t			smask = 0;
812 	uchar_t			cmask = 0;
813 	uint_t			pnode = 0;
814 	ehci_pipe_private_t	*pp;
815 
816 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
817 	    "ehci_hcdi_pipe_open: addr = 0x%x, ep%d",
818 	    ph->p_usba_device->usb_addr,
819 	    epdt->bEndpointAddress & USB_EP_NUM_MASK);
820 
821 	mutex_enter(&ehcip->ehci_int_mutex);
822 	rval = ehci_state_is_operational(ehcip);
823 	mutex_exit(&ehcip->ehci_int_mutex);
824 
825 	if (rval != USB_SUCCESS) {
826 
827 		return (rval);
828 	}
829 
830 	/*
831 	 * Check and handle root hub pipe open.
832 	 */
833 	if (ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) {
834 
835 		mutex_enter(&ehcip->ehci_int_mutex);
836 		error = ehci_handle_root_hub_pipe_open(ph, flags);
837 		mutex_exit(&ehcip->ehci_int_mutex);
838 
839 		return (error);
840 	}
841 
842 	/*
843 	 * Opening of other pipes excluding root hub pipe are
844 	 * handled below. Check whether pipe is already opened.
845 	 */
846 	if (ph->p_hcd_private) {
847 		USB_DPRINTF_L2(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
848 		    "ehci_hcdi_pipe_open: Pipe is already opened");
849 
850 		return (USB_FAILURE);
851 	}
852 
853 	/*
854 	 * A portion of the bandwidth is reserved for the non-periodic
855 	 * transfers, i.e control and bulk transfers in each of one
856 	 * millisecond frame period & usually it will be 20% of frame
857 	 * period. Hence there is no need to check for the available
858 	 * bandwidth before adding the control or bulk endpoints.
859 	 *
860 	 * There is a need to check for the available bandwidth before
861 	 * adding the periodic transfers, i.e interrupt & isochronous,
862 	 * since all these periodic transfers are guaranteed transfers.
863 	 * Usually 80% of the total frame time is reserved for periodic
864 	 * transfers.
865 	 */
866 	if (EHCI_PERIODIC_ENDPOINT(epdt)) {
867 
868 		mutex_enter(&ehcip->ehci_int_mutex);
869 		mutex_enter(&ph->p_mutex);
870 
871 		error = ehci_allocate_bandwidth(ehcip,
872 		    ph, &pnode, &smask, &cmask);
873 
874 		if (error != USB_SUCCESS) {
875 
876 			USB_DPRINTF_L2(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
877 			    "ehci_hcdi_pipe_open: Bandwidth allocation failed");
878 
879 			mutex_exit(&ph->p_mutex);
880 			mutex_exit(&ehcip->ehci_int_mutex);
881 
882 			return (error);
883 		}
884 
885 		mutex_exit(&ph->p_mutex);
886 		mutex_exit(&ehcip->ehci_int_mutex);
887 	}
888 
889 	/* Create the HCD pipe private structure */
890 	pp = kmem_zalloc(sizeof (ehci_pipe_private_t), kmflag);
891 
892 	/*
893 	 * Return failure if ehci pipe private
894 	 * structure allocation fails.
895 	 */
896 	if (pp == NULL) {
897 
898 		mutex_enter(&ehcip->ehci_int_mutex);
899 
900 		/* Deallocate bandwidth */
901 		if (EHCI_PERIODIC_ENDPOINT(epdt)) {
902 
903 			mutex_enter(&ph->p_mutex);
904 			ehci_deallocate_bandwidth(ehcip,
905 			    ph, pnode, smask, cmask);
906 			mutex_exit(&ph->p_mutex);
907 		}
908 
909 		mutex_exit(&ehcip->ehci_int_mutex);
910 
911 		return (USB_NO_RESOURCES);
912 	}
913 
914 	mutex_enter(&ehcip->ehci_int_mutex);
915 
916 	/* Save periodic nodes */
917 	pp->pp_pnode = pnode;
918 
919 	/* Save start and complete split mask values */
920 	pp->pp_smask = smask;
921 	pp->pp_cmask = cmask;
922 
923 	/* Create prototype for xfer completion condition variable */
924 	cv_init(&pp->pp_xfer_cmpl_cv, NULL, CV_DRIVER, NULL);
925 
926 	/* Set the state of pipe as idle */
927 	pp->pp_state = EHCI_PIPE_STATE_IDLE;
928 
929 	/* Store a pointer to the pipe handle */
930 	pp->pp_pipe_handle = ph;
931 
932 	mutex_enter(&ph->p_mutex);
933 
934 	/* Store the pointer in the pipe handle */
935 	ph->p_hcd_private = (usb_opaque_t)pp;
936 
937 	/* Store a copy of the pipe policy */
938 	bcopy(&ph->p_policy, &pp->pp_policy, sizeof (usb_pipe_policy_t));
939 
940 	mutex_exit(&ph->p_mutex);
941 
942 	/* Allocate the host controller endpoint descriptor */
943 	pp->pp_qh = ehci_alloc_qh(ehcip, ph, NULL);
944 
945 	/* Initialize the halting flag */
946 	pp->pp_halt_state = EHCI_HALT_STATE_FREE;
947 
948 	/* Create prototype for halt completion condition variable */
949 	cv_init(&pp->pp_halt_cmpl_cv, NULL, CV_DRIVER, NULL);
950 
951 	/* Isoch does not use QH, so ignore this */
952 	if ((pp->pp_qh == NULL) && !(EHCI_ISOC_ENDPOINT(epdt))) {
953 		ASSERT(pp->pp_qh == NULL);
954 
955 		USB_DPRINTF_L2(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
956 		    "ehci_hcdi_pipe_open: QH allocation failed");
957 
958 		mutex_enter(&ph->p_mutex);
959 
960 		/* Deallocate bandwidth */
961 		if (EHCI_PERIODIC_ENDPOINT(epdt)) {
962 
963 			ehci_deallocate_bandwidth(ehcip,
964 			    ph, pnode, smask, cmask);
965 		}
966 
967 		/* Destroy the xfer completion condition variable */
968 		cv_destroy(&pp->pp_xfer_cmpl_cv);
969 
970 		/*
971 		 * Deallocate the hcd private portion
972 		 * of the pipe handle.
973 		 */
974 		kmem_free(ph->p_hcd_private, sizeof (ehci_pipe_private_t));
975 
976 		/*
977 		 * Set the private structure in the
978 		 * pipe handle equal to NULL.
979 		 */
980 		ph->p_hcd_private = NULL;
981 
982 		mutex_exit(&ph->p_mutex);
983 		mutex_exit(&ehcip->ehci_int_mutex);
984 
985 		return (USB_NO_RESOURCES);
986 	}
987 
988 	/*
989 	 * Isoch does not use QH so no need to
990 	 * restore data toggle or insert QH
991 	 */
992 	if (!(EHCI_ISOC_ENDPOINT(epdt))) {
993 		/* Restore the data toggle information */
994 		ehci_restore_data_toggle(ehcip, ph);
995 	}
996 
997 	/*
998 	 * Insert the endpoint onto the host controller's
999 	 * appropriate endpoint list. The host controller
1000 	 * will not schedule this endpoint and will not have
1001 	 * any QTD's to process.  It will also update the pipe count.
1002 	 */
1003 	ehci_insert_qh(ehcip, ph);
1004 
1005 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1006 	    "ehci_hcdi_pipe_open: ph = 0x%p", (void *)ph);
1007 
1008 	ehcip->ehci_open_pipe_count++;
1009 
1010 	mutex_exit(&ehcip->ehci_int_mutex);
1011 
1012 	return (USB_SUCCESS);
1013 }
1014 
1015 
1016 /*
1017  * ehci_hcdi_pipe_close:
1018  *
1019  * Member of HCD Ops structure and called during the client  specific pipe
1020  * close. Remove the pipe and the data structure representing the device.
1021  * Deallocate  bandwidth for the pipe if it is a interrupt or isochronous
1022  * endpoint.
1023  */
1024 /* ARGSUSED */
1025 int
1026 ehci_hcdi_pipe_close(
1027 	usba_pipe_handle_data_t	*ph,
1028 	usb_flags_t		flags)
1029 {
1030 	ehci_state_t		*ehcip = ehci_obtain_state(
1031 	    ph->p_usba_device->usb_root_hub_dip);
1032 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
1033 	usb_ep_descr_t		*eptd = &ph->p_ep;
1034 	int			error = USB_SUCCESS;
1035 
1036 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1037 	    "ehci_hcdi_pipe_close: addr = 0x%x, ep%d",
1038 	    ph->p_usba_device->usb_addr,
1039 	    eptd->bEndpointAddress & USB_EP_NUM_MASK);
1040 
1041 	/* Check and handle root hub pipe close */
1042 	if (ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) {
1043 
1044 		mutex_enter(&ehcip->ehci_int_mutex);
1045 		error = ehci_handle_root_hub_pipe_close(ph);
1046 		mutex_exit(&ehcip->ehci_int_mutex);
1047 
1048 		return (error);
1049 	}
1050 
1051 	ASSERT(ph->p_hcd_private != NULL);
1052 
1053 	mutex_enter(&ehcip->ehci_int_mutex);
1054 
1055 	/* Set pipe state to pipe close */
1056 	pp->pp_state = EHCI_PIPE_STATE_CLOSE;
1057 
1058 	ehci_pipe_cleanup(ehcip, ph);
1059 
1060 	/*
1061 	 * Remove the endpoint descriptor from Host
1062 	 * Controller's appropriate endpoint list.
1063 	 */
1064 	ehci_remove_qh(ehcip, pp, B_TRUE);
1065 
1066 	/* Deallocate bandwidth */
1067 	if (EHCI_PERIODIC_ENDPOINT(eptd)) {
1068 
1069 		mutex_enter(&ph->p_mutex);
1070 		ehci_deallocate_bandwidth(ehcip, ph, pp->pp_pnode,
1071 		    pp->pp_smask, pp->pp_cmask);
1072 		mutex_exit(&ph->p_mutex);
1073 	}
1074 
1075 	mutex_enter(&ph->p_mutex);
1076 
1077 	/* Destroy the xfer completion condition variable */
1078 	cv_destroy(&pp->pp_xfer_cmpl_cv);
1079 
1080 
1081 	/* Destory halt completion condition variable */
1082 	cv_destroy(&pp->pp_halt_cmpl_cv);
1083 
1084 	/*
1085 	 * Deallocate the hcd private portion
1086 	 * of the pipe handle.
1087 	 */
1088 	kmem_free(ph->p_hcd_private, sizeof (ehci_pipe_private_t));
1089 	ph->p_hcd_private = NULL;
1090 
1091 	mutex_exit(&ph->p_mutex);
1092 
1093 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1094 	    "ehci_hcdi_pipe_close: ph = 0x%p", (void *)ph);
1095 
1096 	ehcip->ehci_open_pipe_count--;
1097 
1098 	mutex_exit(&ehcip->ehci_int_mutex);
1099 
1100 	return (error);
1101 }
1102 
1103 
1104 /*
1105  * ehci_hcdi_pipe_reset:
1106  */
1107 /* ARGSUSED */
1108 int
1109 ehci_hcdi_pipe_reset(
1110 	usba_pipe_handle_data_t	*ph,
1111 	usb_flags_t		usb_flags)
1112 {
1113 	ehci_state_t		*ehcip = ehci_obtain_state(
1114 	    ph->p_usba_device->usb_root_hub_dip);
1115 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
1116 	int			error = USB_SUCCESS;
1117 
1118 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1119 	    "ehci_hcdi_pipe_reset:");
1120 
1121 	/*
1122 	 * Check and handle root hub pipe reset.
1123 	 */
1124 	if (ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) {
1125 
1126 		error = ehci_handle_root_hub_pipe_reset(ph, usb_flags);
1127 		return (error);
1128 	}
1129 
1130 	mutex_enter(&ehcip->ehci_int_mutex);
1131 
1132 	/* Set pipe state to pipe reset */
1133 	pp->pp_state = EHCI_PIPE_STATE_RESET;
1134 
1135 	ehci_pipe_cleanup(ehcip, ph);
1136 
1137 	mutex_exit(&ehcip->ehci_int_mutex);
1138 
1139 	return (error);
1140 }
1141 
1142 /*
1143  * ehci_hcdi_pipe_ctrl_xfer:
1144  */
1145 int
1146 ehci_hcdi_pipe_ctrl_xfer(
1147 	usba_pipe_handle_data_t	*ph,
1148 	usb_ctrl_req_t		*ctrl_reqp,
1149 	usb_flags_t		usb_flags)
1150 {
1151 	ehci_state_t		*ehcip = ehci_obtain_state(
1152 	    ph->p_usba_device->usb_root_hub_dip);
1153 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
1154 	int			rval;
1155 	int			error = USB_SUCCESS;
1156 	ehci_trans_wrapper_t	*tw;
1157 
1158 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1159 	    "ehci_hcdi_pipe_ctrl_xfer: ph = 0x%p reqp = 0x%p flags = %x",
1160 	    (void *)ph, (void *)ctrl_reqp, usb_flags);
1161 
1162 	mutex_enter(&ehcip->ehci_int_mutex);
1163 	rval = ehci_state_is_operational(ehcip);
1164 	mutex_exit(&ehcip->ehci_int_mutex);
1165 
1166 	if (rval != USB_SUCCESS) {
1167 
1168 		return (rval);
1169 	}
1170 
1171 	/*
1172 	 * Check and handle root hub control request.
1173 	 */
1174 	if (ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) {
1175 
1176 		error = ehci_handle_root_hub_request(ehcip, ph, ctrl_reqp);
1177 
1178 		return (error);
1179 	}
1180 
1181 	mutex_enter(&ehcip->ehci_int_mutex);
1182 
1183 	/*
1184 	 *  Check whether pipe is in halted state.
1185 	 */
1186 	if (pp->pp_state == EHCI_PIPE_STATE_ERROR) {
1187 
1188 		USB_DPRINTF_L2(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1189 		    "ehci_hcdi_pipe_ctrl_xfer: "
1190 		    "Pipe is in error state, need pipe reset to continue");
1191 
1192 		mutex_exit(&ehcip->ehci_int_mutex);
1193 
1194 		return (USB_FAILURE);
1195 	}
1196 
1197 	/* Allocate a transfer wrapper */
1198 	if ((tw = ehci_allocate_ctrl_resources(ehcip, pp, ctrl_reqp,
1199 	    usb_flags)) == NULL) {
1200 
1201 		error = USB_NO_RESOURCES;
1202 	} else {
1203 		/* Insert the qtd's on the endpoint */
1204 		ehci_insert_ctrl_req(ehcip, ph, ctrl_reqp, tw, usb_flags);
1205 	}
1206 
1207 	mutex_exit(&ehcip->ehci_int_mutex);
1208 
1209 	return (error);
1210 }
1211 
1212 
1213 /*
1214  * ehci_hcdi_bulk_transfer_size:
1215  *
1216  * Return maximum bulk transfer size
1217  */
1218 
1219 /* ARGSUSED */
1220 int
1221 ehci_hcdi_bulk_transfer_size(
1222 	usba_device_t	*usba_device,
1223 	size_t		*size)
1224 {
1225 	ehci_state_t	*ehcip = ehci_obtain_state(
1226 	    usba_device->usb_root_hub_dip);
1227 	int		rval;
1228 
1229 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1230 	    "ehci_hcdi_bulk_transfer_size:");
1231 
1232 	mutex_enter(&ehcip->ehci_int_mutex);
1233 	rval = ehci_state_is_operational(ehcip);
1234 	mutex_exit(&ehcip->ehci_int_mutex);
1235 
1236 	if (rval != USB_SUCCESS) {
1237 
1238 		return (rval);
1239 	}
1240 
1241 	/* VIA VT6202 may not handle bigger xfers well, workaround. */
1242 	if ((ehcip->ehci_vendor_id == PCI_VENDOR_VIA) &&
1243 	    (ehci_vt62x2_workaround & EHCI_VIA_REDUCED_MAX_BULK_XFER_SIZE)) {
1244 		*size = EHCI_VIA_MAX_BULK_XFER_SIZE;
1245 	} else {
1246 		*size = EHCI_MAX_BULK_XFER_SIZE;
1247 	}
1248 
1249 	return (USB_SUCCESS);
1250 }
1251 
1252 
1253 /*
1254  * ehci_hcdi_pipe_bulk_xfer:
1255  */
1256 int
1257 ehci_hcdi_pipe_bulk_xfer(
1258 	usba_pipe_handle_data_t	*ph,
1259 	usb_bulk_req_t		*bulk_reqp,
1260 	usb_flags_t		usb_flags)
1261 {
1262 	ehci_state_t		*ehcip = ehci_obtain_state(
1263 	    ph->p_usba_device->usb_root_hub_dip);
1264 	ehci_pipe_private_t	*pp = (ehci_pipe_private_t *)ph->p_hcd_private;
1265 	int			rval, error = USB_SUCCESS;
1266 	ehci_trans_wrapper_t	*tw;
1267 
1268 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1269 	    "ehci_hcdi_pipe_bulk_xfer: ph = 0x%p reqp = 0x%p flags = %x",
1270 	    (void *)ph, (void *)bulk_reqp, usb_flags);
1271 
1272 	mutex_enter(&ehcip->ehci_int_mutex);
1273 	rval = ehci_state_is_operational(ehcip);
1274 
1275 	if (rval != USB_SUCCESS) {
1276 		mutex_exit(&ehcip->ehci_int_mutex);
1277 
1278 		return (rval);
1279 	}
1280 
1281 	/*
1282 	 *  Check whether pipe is in halted state.
1283 	 */
1284 	if (pp->pp_state == EHCI_PIPE_STATE_ERROR) {
1285 
1286 		USB_DPRINTF_L2(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1287 		    "ehci_hcdi_pipe_bulk_xfer:"
1288 		    "Pipe is in error state, need pipe reset to continue");
1289 
1290 		mutex_exit(&ehcip->ehci_int_mutex);
1291 
1292 		return (USB_FAILURE);
1293 	}
1294 
1295 	/* Allocate a transfer wrapper */
1296 	if ((tw = ehci_allocate_bulk_resources(ehcip, pp, bulk_reqp,
1297 	    usb_flags)) == NULL) {
1298 
1299 		error = USB_NO_RESOURCES;
1300 	} else {
1301 		/* Add the QTD into the Host Controller's bulk list */
1302 		ehci_insert_bulk_req(ehcip, ph, bulk_reqp, tw, usb_flags);
1303 	}
1304 
1305 	mutex_exit(&ehcip->ehci_int_mutex);
1306 
1307 	return (error);
1308 }
1309 
1310 
1311 /*
1312  * ehci_hcdi_pipe_intr_xfer:
1313  */
1314 int
1315 ehci_hcdi_pipe_intr_xfer(
1316 	usba_pipe_handle_data_t	*ph,
1317 	usb_intr_req_t		*intr_reqp,
1318 	usb_flags_t		usb_flags)
1319 {
1320 	ehci_state_t		*ehcip = ehci_obtain_state(
1321 	    ph->p_usba_device->usb_root_hub_dip);
1322 	int			pipe_dir, rval, error = USB_SUCCESS;
1323 	ehci_trans_wrapper_t	*tw;
1324 
1325 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1326 	    "ehci_hcdi_pipe_intr_xfer: ph = 0x%p reqp = 0x%p flags = %x",
1327 	    (void *)ph, (void *)intr_reqp, usb_flags);
1328 
1329 	mutex_enter(&ehcip->ehci_int_mutex);
1330 	rval = ehci_state_is_operational(ehcip);
1331 
1332 	if (rval != USB_SUCCESS) {
1333 		mutex_exit(&ehcip->ehci_int_mutex);
1334 
1335 		return (rval);
1336 	}
1337 
1338 	/* Get the pipe direction */
1339 	pipe_dir = ph->p_ep.bEndpointAddress & USB_EP_DIR_MASK;
1340 
1341 	if (pipe_dir == USB_EP_DIR_IN) {
1342 		error = ehci_start_periodic_pipe_polling(ehcip, ph,
1343 		    (usb_opaque_t)intr_reqp, usb_flags);
1344 	} else {
1345 		/* Allocate transaction resources */
1346 		if ((tw = ehci_allocate_intr_resources(ehcip, ph,
1347 		    intr_reqp, usb_flags)) == NULL) {
1348 
1349 			error = USB_NO_RESOURCES;
1350 		} else {
1351 			ehci_insert_intr_req(ehcip,
1352 			    (ehci_pipe_private_t *)ph->p_hcd_private,
1353 			    tw, usb_flags);
1354 		}
1355 	}
1356 
1357 	mutex_exit(&ehcip->ehci_int_mutex);
1358 
1359 	return (error);
1360 }
1361 
1362 /*
1363  * ehci_hcdi_pipe_stop_intr_polling()
1364  */
1365 int
1366 ehci_hcdi_pipe_stop_intr_polling(
1367 	usba_pipe_handle_data_t	*ph,
1368 	usb_flags_t		flags)
1369 {
1370 	ehci_state_t		*ehcip = ehci_obtain_state(
1371 	    ph->p_usba_device->usb_root_hub_dip);
1372 	int			error = USB_SUCCESS;
1373 
1374 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1375 	    "ehci_hcdi_pipe_stop_intr_polling: ph = 0x%p fl = 0x%x",
1376 	    (void *)ph, flags);
1377 
1378 	mutex_enter(&ehcip->ehci_int_mutex);
1379 
1380 	error = ehci_stop_periodic_pipe_polling(ehcip, ph, flags);
1381 
1382 	mutex_exit(&ehcip->ehci_int_mutex);
1383 
1384 	return (error);
1385 }
1386 
1387 
1388 /*
1389  * ehci_hcdi_get_current_frame_number:
1390  *
1391  * Get the current usb frame number.
1392  * Return whether the request is handled successfully.
1393  */
1394 int
1395 ehci_hcdi_get_current_frame_number(
1396 	usba_device_t		*usba_device,
1397 	usb_frame_number_t	*frame_number)
1398 {
1399 	ehci_state_t		*ehcip = ehci_obtain_state(
1400 	    usba_device->usb_root_hub_dip);
1401 	int			rval;
1402 
1403 	ehcip = ehci_obtain_state(usba_device->usb_root_hub_dip);
1404 
1405 	mutex_enter(&ehcip->ehci_int_mutex);
1406 	rval = ehci_state_is_operational(ehcip);
1407 
1408 	if (rval != USB_SUCCESS) {
1409 		mutex_exit(&ehcip->ehci_int_mutex);
1410 
1411 		return (rval);
1412 	}
1413 
1414 	*frame_number = ehci_get_current_frame_number(ehcip);
1415 
1416 	mutex_exit(&ehcip->ehci_int_mutex);
1417 
1418 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1419 	    "ehci_hcdi_get_current_frame_number: "
1420 	    "Current frame number 0x%llx", (unsigned long long)(*frame_number));
1421 
1422 	return (rval);
1423 }
1424 
1425 
1426 /*
1427  * ehci_hcdi_get_max_isoc_pkts:
1428  *
1429  * Get maximum isochronous packets per usb isochronous request.
1430  * Return whether the request is handled successfully.
1431  */
1432 int
1433 ehci_hcdi_get_max_isoc_pkts(
1434 	usba_device_t	*usba_device,
1435 	uint_t		*max_isoc_pkts_per_request)
1436 {
1437 	ehci_state_t		*ehcip = ehci_obtain_state(
1438 	    usba_device->usb_root_hub_dip);
1439 	int			rval;
1440 
1441 	mutex_enter(&ehcip->ehci_int_mutex);
1442 	rval = ehci_state_is_operational(ehcip);
1443 	mutex_exit(&ehcip->ehci_int_mutex);
1444 
1445 	if (rval != USB_SUCCESS) {
1446 
1447 		return (rval);
1448 	}
1449 
1450 	*max_isoc_pkts_per_request = EHCI_MAX_ISOC_PKTS_PER_XFER;
1451 
1452 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1453 	    "ehci_hcdi_get_max_isoc_pkts: maximum isochronous"
1454 	    "packets per usb isochronous request = 0x%x",
1455 	    *max_isoc_pkts_per_request);
1456 
1457 	return (rval);
1458 }
1459 
1460 
1461 /*
1462  * ehci_hcdi_pipe_isoc_xfer:
1463  */
1464 int
1465 ehci_hcdi_pipe_isoc_xfer(
1466 	usba_pipe_handle_data_t	*ph,
1467 	usb_isoc_req_t		*isoc_reqp,
1468 	usb_flags_t		usb_flags)
1469 {
1470 	ehci_state_t		*ehcip = ehci_obtain_state(
1471 	    ph->p_usba_device->usb_root_hub_dip);
1472 
1473 	int			pipe_dir, rval;
1474 	ehci_isoc_xwrapper_t	*itw;
1475 
1476 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1477 	    "ehci_hcdi_pipe_isoc_xfer: ph = 0x%p reqp = 0x%p flags = 0x%x",
1478 	    (void *)ph, (void *)isoc_reqp, usb_flags);
1479 
1480 	mutex_enter(&ehcip->ehci_int_mutex);
1481 	rval = ehci_state_is_operational(ehcip);
1482 
1483 	if (rval != USB_SUCCESS) {
1484 		mutex_exit(&ehcip->ehci_int_mutex);
1485 
1486 		return (rval);
1487 	}
1488 
1489 	/* Get the isochronous pipe direction */
1490 	pipe_dir = ph->p_ep.bEndpointAddress & USB_EP_DIR_MASK;
1491 
1492 	if (pipe_dir == USB_EP_DIR_IN) {
1493 		rval = ehci_start_periodic_pipe_polling(ehcip, ph,
1494 		    (usb_opaque_t)isoc_reqp, usb_flags);
1495 	} else {
1496 		/* Allocate transaction resources */
1497 		if ((itw = ehci_allocate_isoc_resources(ehcip, ph,
1498 		    isoc_reqp, usb_flags)) == NULL) {
1499 			rval = USB_NO_RESOURCES;
1500 		} else {
1501 			rval = ehci_insert_isoc_req(ehcip,
1502 			    (ehci_pipe_private_t *)ph->p_hcd_private,
1503 			    itw, usb_flags);
1504 		}
1505 	}
1506 
1507 	mutex_exit(&ehcip->ehci_int_mutex);
1508 
1509 	return (rval);
1510 }
1511 
1512 
1513 /*
1514  * ehci_hcdi_pipe_stop_isoc_polling()
1515  */
1516 /*ARGSUSED*/
1517 int
1518 ehci_hcdi_pipe_stop_isoc_polling(
1519 	usba_pipe_handle_data_t	*ph,
1520 	usb_flags_t		flags)
1521 {
1522 	ehci_state_t		*ehcip = ehci_obtain_state(
1523 	    ph->p_usba_device->usb_root_hub_dip);
1524 	int			rval;
1525 
1526 	USB_DPRINTF_L4(PRINT_MASK_HCDI, ehcip->ehci_log_hdl,
1527 	    "ehci_hcdi_pipe_stop_isoc_polling: ph = 0x%p fl = 0x%x",
1528 	    (void *)ph, flags);
1529 
1530 	mutex_enter(&ehcip->ehci_int_mutex);
1531 	rval = ehci_state_is_operational(ehcip);
1532 
1533 	if (rval != USB_SUCCESS) {
1534 		mutex_exit(&ehcip->ehci_int_mutex);
1535 
1536 		return (rval);
1537 	}
1538 
1539 	rval = ehci_stop_periodic_pipe_polling(ehcip, ph, flags);
1540 
1541 	mutex_exit(&ehcip->ehci_int_mutex);
1542 
1543 	return (rval);
1544 }
1545