17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4c8a46c1Syq  * Common Development and Distribution License (the "License").
6*4c8a46c1Syq  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*4c8a46c1Syq  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Universal Host Controller Driver (UHCI)
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * The UHCI driver is a driver which interfaces to the Universal
327c478bd9Sstevel@tonic-gate  * Serial Bus Driver (USBA) and the Host Controller (HC). The interface to
337c478bd9Sstevel@tonic-gate  * the Host Controller is defined by the Universal Host Controller Interface.
347c478bd9Sstevel@tonic-gate  * This file contains the code for HCDI entry points.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/uhci/uhcid.h>
377c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/uhci/uhcitgt.h>
387c478bd9Sstevel@tonic-gate #include <sys/usb/hcd/uhci/uhciutil.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* function prototypes */
417c478bd9Sstevel@tonic-gate static int	uhci_pipe_send_isoc_data(uhci_state_t *uhcip,
427c478bd9Sstevel@tonic-gate 			usba_pipe_handle_data_t *ph, usb_isoc_req_t *isoc_req,
437c478bd9Sstevel@tonic-gate 			usb_flags_t usb_flags);
447c478bd9Sstevel@tonic-gate static int	uhci_send_intr_data(uhci_state_t *uhcip,
457c478bd9Sstevel@tonic-gate 			usba_pipe_handle_data_t	*pipe_handle,
467c478bd9Sstevel@tonic-gate 			usb_intr_req_t		*req,
477c478bd9Sstevel@tonic-gate 			usb_flags_t		flags);
487c478bd9Sstevel@tonic-gate static int	uhci_start_periodic_pipe_polling(uhci_state_t *uhcip,
497c478bd9Sstevel@tonic-gate 			usba_pipe_handle_data_t	*ph,
507c478bd9Sstevel@tonic-gate 			usb_opaque_t		reqp,
517c478bd9Sstevel@tonic-gate 			usb_flags_t		flags);
527c478bd9Sstevel@tonic-gate static int	uhci_stop_periodic_pipe_polling(uhci_state_t *uhcip,
537c478bd9Sstevel@tonic-gate 			usba_pipe_handle_data_t	*ph,
547c478bd9Sstevel@tonic-gate 			usb_flags_t		flags);
557c478bd9Sstevel@tonic-gate static void	uhci_update_intr_td_data_toggle(uhci_state_t *uhcip,
567c478bd9Sstevel@tonic-gate 			uhci_pipe_private_t *pp);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* Maximum bulk transfer size */
604610e4a0Sfrits int uhci_bulk_transfer_size = UHCI_BULK_MAX_XFER_SIZE;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_open:
647c478bd9Sstevel@tonic-gate  *	Member of HCD Ops structure and called during client specific pipe open
657c478bd9Sstevel@tonic-gate  *	Add the pipe to the data structure representing the device and allocate
667c478bd9Sstevel@tonic-gate  *	bandwidth for the pipe if it is a interrupt or isochronous endpoint.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate int
697c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_open(usba_pipe_handle_data_t *ph, usb_flags_t flags)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	uint_t			node = 0;
727c478bd9Sstevel@tonic-gate 	usb_addr_t		usb_addr;
737c478bd9Sstevel@tonic-gate 	uhci_state_t		*uhcip;
747c478bd9Sstevel@tonic-gate 	uhci_pipe_private_t	*pp;
757c478bd9Sstevel@tonic-gate 	int			error = USB_SUCCESS;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	ASSERT(ph);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	usb_addr = ph->p_usba_device->usb_addr;
807c478bd9Sstevel@tonic-gate 	uhcip = uhci_obtain_state(ph->p_usba_device->usb_root_hub_dip);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
837c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_open: addr = 0x%x, ep%d", usb_addr,
847c478bd9Sstevel@tonic-gate 	    ph->p_ep.bEndpointAddress & USB_EP_NUM_MASK);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	sema_p(&uhcip->uhci_ocsem);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * Return failure immediately for any other pipe open on the root hub
907c478bd9Sstevel@tonic-gate 	 * except control or interrupt pipe.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	if (usb_addr == ROOT_HUB_ADDR) {
937c478bd9Sstevel@tonic-gate 		switch (UHCI_XFER_TYPE(&ph->p_ep)) {
947c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_CONTROL:
957c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
967c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_open: Root hub control pipe");
977c478bd9Sstevel@tonic-gate 			break;
987c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_INTR:
997c478bd9Sstevel@tonic-gate 			ASSERT(UHCI_XFER_DIR(&ph->p_ep) == USB_EP_DIR_IN);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 			mutex_enter(&uhcip->uhci_int_mutex);
1027c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_intr_pipe_handle = ph;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 			/*
1057c478bd9Sstevel@tonic-gate 			 * Set the state of the root hub interrupt
1067c478bd9Sstevel@tonic-gate 			 * pipe as IDLE.
1077c478bd9Sstevel@tonic-gate 			 */
1087c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_pipe_state =
1097c478bd9Sstevel@tonic-gate 						UHCI_PIPE_STATE_IDLE;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 			ASSERT(uhcip->uhci_root_hub.rh_client_intr_req == NULL);
1127c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_client_intr_req = NULL;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 			ASSERT(uhcip->uhci_root_hub.rh_curr_intr_reqp == NULL);
1157c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_curr_intr_reqp = NULL;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
1187c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_open: Root hub interrupt "
1197c478bd9Sstevel@tonic-gate 			    "pipe open succeeded");
1207c478bd9Sstevel@tonic-gate 			mutex_exit(&uhcip->uhci_int_mutex);
1217c478bd9Sstevel@tonic-gate 			sema_v(&uhcip->uhci_ocsem);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 			return (USB_SUCCESS);
1247c478bd9Sstevel@tonic-gate 		default:
1257c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
1267c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_open: Root hub pipe open failed");
1277c478bd9Sstevel@tonic-gate 			sema_v(&uhcip->uhci_ocsem);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * A portion of the bandwidth is reserved for the non-periodic
1357c478bd9Sstevel@tonic-gate 	 * transfers  i.e control and bulk transfers in each  of one
1367c478bd9Sstevel@tonic-gate 	 * mill second frame period & usually it will be 10% of frame
1377c478bd9Sstevel@tonic-gate 	 * period. Hence there is no need to check for the available
1387c478bd9Sstevel@tonic-gate 	 * bandwidth before adding the control or bulk endpoints.
1397c478bd9Sstevel@tonic-gate 	 *
1407c478bd9Sstevel@tonic-gate 	 * There is a need to check for the available bandwidth before
1417c478bd9Sstevel@tonic-gate 	 * adding the periodic transfers i.e interrupt & isochronous, since
1427c478bd9Sstevel@tonic-gate 	 * all these periodic transfers are guaranteed transfers. Usually,
1437c478bd9Sstevel@tonic-gate 	 * 90% of the total frame time is reserved for periodic transfers.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	if (UHCI_PERIODIC_ENDPOINT(&ph->p_ep)) {
1467c478bd9Sstevel@tonic-gate 		/* Zero Max Packet size endpoints are not supported */
1477c478bd9Sstevel@tonic-gate 		if (ph->p_ep.wMaxPacketSize == 0) {
1487c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
1497c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_open: Zero length packet");
1507c478bd9Sstevel@tonic-gate 			sema_v(&uhcip->uhci_ocsem);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		mutex_enter(&uhcip->uhci_int_mutex);
1567c478bd9Sstevel@tonic-gate 		mutex_enter(&ph->p_mutex);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		error = uhci_allocate_bandwidth(uhcip, ph, &node);
1597c478bd9Sstevel@tonic-gate 		if (error != USB_SUCCESS) {
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
1627c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_open: Bandwidth allocation failed");
1637c478bd9Sstevel@tonic-gate 			mutex_exit(&ph->p_mutex);
1647c478bd9Sstevel@tonic-gate 			mutex_exit(&uhcip->uhci_int_mutex);
1657c478bd9Sstevel@tonic-gate 			sema_v(&uhcip->uhci_ocsem);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 			return (error);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		mutex_exit(&ph->p_mutex);
1717c478bd9Sstevel@tonic-gate 		mutex_exit(&uhcip->uhci_int_mutex);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/* Create the HCD pipe private structure */
1757c478bd9Sstevel@tonic-gate 	pp = kmem_zalloc(sizeof (uhci_pipe_private_t),
1767c478bd9Sstevel@tonic-gate 	    (flags & USB_FLAGS_SLEEP) ? KM_SLEEP : KM_NOSLEEP);
1777c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
1787c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
1797c478bd9Sstevel@tonic-gate 		    "uhci_hcdi_pipe_open: pp allocation failure");
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		if (UHCI_PERIODIC_ENDPOINT(&ph->p_ep)) {
1827c478bd9Sstevel@tonic-gate 			mutex_enter(&uhcip->uhci_int_mutex);
1837c478bd9Sstevel@tonic-gate 			uhci_deallocate_bandwidth(uhcip, ph);
1847c478bd9Sstevel@tonic-gate 			mutex_exit(&uhcip->uhci_int_mutex);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		sema_v(&uhcip->uhci_ocsem);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		return (USB_NO_RESOURCES);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
1927c478bd9Sstevel@tonic-gate 	pp->pp_node = node;	/* Store the node in the interrupt lattice */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* Initialize frame number */
1957c478bd9Sstevel@tonic-gate 	pp->pp_frame_num = INVALID_FRNUM;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* Set the state of pipe as IDLE */
1987c478bd9Sstevel@tonic-gate 	pp->pp_state = UHCI_PIPE_STATE_IDLE;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* Store a pointer to the pipe handle */
2017c478bd9Sstevel@tonic-gate 	pp->pp_pipe_handle = ph;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* Store the pointer in the pipe handle */
2047c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
2057c478bd9Sstevel@tonic-gate 	ph->p_hcd_private = (usb_opaque_t)pp;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/* Store a copy of the pipe policy */
2087c478bd9Sstevel@tonic-gate 	bcopy(&ph->p_policy, &pp->pp_policy, sizeof (usb_pipe_policy_t));
2097c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* don't check for ROOT_HUB here anymore */
2127c478bd9Sstevel@tonic-gate 	if (UHCI_XFER_TYPE(&ph->p_ep) != USB_EP_ATTR_ISOCH) {
2137c478bd9Sstevel@tonic-gate 		/* Allocate the host controller endpoint descriptor */
2147c478bd9Sstevel@tonic-gate 		pp->pp_qh = uhci_alloc_queue_head(uhcip);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		if (pp->pp_qh == NULL) {
2177c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_LISTS, uhcip->uhci_log_hdl,
2187c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_open: QH allocation failed");
2197c478bd9Sstevel@tonic-gate 
220*4c8a46c1Syq 			if (UHCI_PERIODIC_ENDPOINT(&ph->p_ep)) {
221*4c8a46c1Syq 				uhci_deallocate_bandwidth(uhcip, ph);
222*4c8a46c1Syq 			}
223*4c8a46c1Syq 
2247c478bd9Sstevel@tonic-gate 			mutex_enter(&ph->p_mutex);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 			/*
2277c478bd9Sstevel@tonic-gate 			 * Deallocate the hcd private portion
2287c478bd9Sstevel@tonic-gate 			 * of the pipe handle.
2297c478bd9Sstevel@tonic-gate 			 */
2307c478bd9Sstevel@tonic-gate 			kmem_free(ph->p_hcd_private,
2317c478bd9Sstevel@tonic-gate 				sizeof (uhci_pipe_private_t));
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 			/*
2347c478bd9Sstevel@tonic-gate 			 * Set the private structure in the
2357c478bd9Sstevel@tonic-gate 			 * pipe handle equal to NULL.
2367c478bd9Sstevel@tonic-gate 			 */
2377c478bd9Sstevel@tonic-gate 			ph->p_hcd_private = NULL;
2387c478bd9Sstevel@tonic-gate 			mutex_exit(&ph->p_mutex);
2397c478bd9Sstevel@tonic-gate 			mutex_exit(&uhcip->uhci_int_mutex);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 			sema_v(&uhcip->uhci_ocsem);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 			return (USB_NO_RESOURCES);
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		/*
2477c478bd9Sstevel@tonic-gate 		 * Insert the endpoint onto the host controller's
2487c478bd9Sstevel@tonic-gate 		 * appropriate endpoint list. The host controller
2497c478bd9Sstevel@tonic-gate 		 * will not schedule this endpoint until there are
2507c478bd9Sstevel@tonic-gate 		 * any TD's to process.
2517c478bd9Sstevel@tonic-gate 		 */
2527c478bd9Sstevel@tonic-gate 		uhci_insert_qh(uhcip, ph);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Restore the data toggle from usb device structure.
2577c478bd9Sstevel@tonic-gate 	 */
258*4c8a46c1Syq 	if (((ph->p_ep.bmAttributes) & USB_EP_ATTR_MASK) == USB_EP_ATTR_INTR ||
259*4c8a46c1Syq 	    ((ph->p_ep.bmAttributes) & USB_EP_ATTR_MASK) == USB_EP_ATTR_BULK) {
2607c478bd9Sstevel@tonic-gate 		mutex_enter(&ph->p_mutex);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		pp->pp_data_toggle = usba_hcdi_get_data_toggle(
2637c478bd9Sstevel@tonic-gate 			ph->p_usba_device, ph->p_ep.bEndpointAddress);
2647c478bd9Sstevel@tonic-gate 		mutex_exit(&ph->p_mutex);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
2687c478bd9Sstevel@tonic-gate 	sema_v(&uhcip->uhci_ocsem);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
2717c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_open: ph = 0x%p", ph);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_close:
2797c478bd9Sstevel@tonic-gate  *	Member of HCD Ops structure and called during the client specific pipe
2807c478bd9Sstevel@tonic-gate  *	close. Remove the pipe to the data structure representing the device
2817c478bd9Sstevel@tonic-gate  *	deallocate bandwidth for the pipe if it is an intr or isoch endpoint.
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate int
2847c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_close(usba_pipe_handle_data_t *ph, usb_flags_t usb_flags)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	usb_addr_t		usb_addr;
2877c478bd9Sstevel@tonic-gate 	uhci_state_t		*uhcip;
2887c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
2897c478bd9Sstevel@tonic-gate 	uhci_pipe_private_t	*pp;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	uhcip = uhci_obtain_state(ph->p_usba_device->usb_root_hub_dip);
2927c478bd9Sstevel@tonic-gate 	pp = (uhci_pipe_private_t *)ph->p_hcd_private;
2937c478bd9Sstevel@tonic-gate 	usb_addr = ph->p_usba_device->usb_addr;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
2967c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_close: addr = 0x%x, ep%d, flags = 0x%x", usb_addr,
2977c478bd9Sstevel@tonic-gate 	    eptd->bEndpointAddress, usb_flags);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	sema_p(&uhcip->uhci_ocsem);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * Check whether the pipe is a root hub
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 	if (usb_addr == ROOT_HUB_ADDR) {
3077c478bd9Sstevel@tonic-gate 		switch (UHCI_XFER_TYPE(eptd)) {
3087c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_CONTROL:
3097c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
3107c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_close: Root hub control pipe "
3117c478bd9Sstevel@tonic-gate 			    "close succeeded");
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 			break;
3147c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_INTR:
3157c478bd9Sstevel@tonic-gate 			ASSERT((eptd->bEndpointAddress &
3167c478bd9Sstevel@tonic-gate 				USB_EP_NUM_MASK) == 1);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			ASSERT(uhcip->uhci_root_hub.rh_pipe_state ==
3197c478bd9Sstevel@tonic-gate 					UHCI_PIPE_STATE_ACTIVE);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 			/* Do interrupt pipe cleanup */
3227c478bd9Sstevel@tonic-gate 			uhci_root_hub_intr_pipe_cleanup(uhcip,
3237c478bd9Sstevel@tonic-gate 						USB_CR_PIPE_CLOSING);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 			ASSERT(uhcip->uhci_root_hub.rh_pipe_state ==
3267c478bd9Sstevel@tonic-gate 					UHCI_PIPE_STATE_IDLE);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_intr_pipe_handle = NULL;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
3317c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_close: Root hub interrupt "
3327c478bd9Sstevel@tonic-gate 			    "pipe close succeeded");
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_pipe_state =
3357c478bd9Sstevel@tonic-gate 					UHCI_PIPE_STATE_IDLE;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 			mutex_exit(&uhcip->uhci_int_mutex);
3387c478bd9Sstevel@tonic-gate 			sema_v(&uhcip->uhci_ocsem);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 			return (USB_SUCCESS);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 	} else {
3437c478bd9Sstevel@tonic-gate 		/*
3447c478bd9Sstevel@tonic-gate 		 * Stop all the transactions if it is not the root hub.
3457c478bd9Sstevel@tonic-gate 		 */
3467c478bd9Sstevel@tonic-gate 		if (UHCI_XFER_TYPE(eptd) == USB_EP_ATTR_INTR) {
3477c478bd9Sstevel@tonic-gate 			/*
3487c478bd9Sstevel@tonic-gate 			 * Stop polling on the pipe to prevent any subsequently
3497c478bd9Sstevel@tonic-gate 			 * queued tds (while we're waiting for SOF, below)
3507c478bd9Sstevel@tonic-gate 			 * from being executed
3517c478bd9Sstevel@tonic-gate 			 */
3527c478bd9Sstevel@tonic-gate 			pp->pp_state = UHCI_PIPE_STATE_IDLE;
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		/* Disable all outstanding tds */
3567c478bd9Sstevel@tonic-gate 		uhci_modify_td_active_bits(uhcip, pp);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		/* Prevent this queue from being executed */
3597c478bd9Sstevel@tonic-gate 		if (UHCI_XFER_TYPE(eptd) != USB_EP_ATTR_ISOCH) {
3607c478bd9Sstevel@tonic-gate 			UHCI_SET_TERMINATE_BIT(pp->pp_qh->element_ptr);
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		/* Wait for the next start of frame */
3647c478bd9Sstevel@tonic-gate 		(void) uhci_wait_for_sof(uhcip);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		ASSERT(eptd != NULL);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		switch (UHCI_XFER_TYPE(eptd)) {
3697c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_INTR:
3707c478bd9Sstevel@tonic-gate 			uhci_update_intr_td_data_toggle(uhcip, pp);
3717c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
3727c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_CONTROL:
3737c478bd9Sstevel@tonic-gate 			uhci_remove_tds_tws(uhcip, ph);
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_BULK:
3767c478bd9Sstevel@tonic-gate 			SetQH32(uhcip, pp->pp_qh->element_ptr,
3777c478bd9Sstevel@tonic-gate 			    TD_PADDR(pp->pp_qh->td_tailp));
3787c478bd9Sstevel@tonic-gate 			uhci_remove_bulk_tds_tws(uhcip, pp, UHCI_IN_CLOSE);
3797c478bd9Sstevel@tonic-gate 			uhci_save_data_toggle(pp);
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_ISOCH:
3827c478bd9Sstevel@tonic-gate 			uhci_remove_isoc_tds_tws(uhcip, pp);
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 		default:
3857c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
3867c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_close: Unknown xfer type");
3877c478bd9Sstevel@tonic-gate 			break;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		/*
3917c478bd9Sstevel@tonic-gate 		 * Remove the endoint descriptor from Host Controller's
3927c478bd9Sstevel@tonic-gate 		 * appropriate endpoint list. Isochronous pipes dont have
3937c478bd9Sstevel@tonic-gate 		 * any queue heads attached to it.
3947c478bd9Sstevel@tonic-gate 		 */
3957c478bd9Sstevel@tonic-gate 		if (UHCI_XFER_TYPE(eptd) != USB_EP_ATTR_ISOCH) {
3967c478bd9Sstevel@tonic-gate 			uhci_remove_qh(uhcip, pp);
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		/*
4007c478bd9Sstevel@tonic-gate 		 * Do the callback for the original client
4017c478bd9Sstevel@tonic-gate 		 * periodic IN request.
4027c478bd9Sstevel@tonic-gate 		 */
4037c478bd9Sstevel@tonic-gate 		if (pp->pp_client_periodic_in_reqp) {
4047c478bd9Sstevel@tonic-gate 			uhci_hcdi_callback(uhcip, pp, ph, NULL,
4057c478bd9Sstevel@tonic-gate 			    USB_CR_PIPE_CLOSING);
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		/* Deallocate bandwidth */
4097c478bd9Sstevel@tonic-gate 		if (UHCI_PERIODIC_ENDPOINT(eptd)) {
4107c478bd9Sstevel@tonic-gate 			mutex_enter(&ph->p_mutex);
4117c478bd9Sstevel@tonic-gate 			uhci_deallocate_bandwidth(uhcip, ph);
4127c478bd9Sstevel@tonic-gate 			mutex_exit(&ph->p_mutex);
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/* Deallocate the hcd private portion of the pipe handle.  */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	mutex_enter(&ph->p_mutex);
4197c478bd9Sstevel@tonic-gate 	kmem_free(ph->p_hcd_private, sizeof (uhci_pipe_private_t));
4207c478bd9Sstevel@tonic-gate 	ph->p_hcd_private = NULL;
4217c478bd9Sstevel@tonic-gate 	mutex_exit(&ph->p_mutex);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
4247c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_close: ph = 0x%p", ph);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
4277c478bd9Sstevel@tonic-gate 	sema_v(&uhcip->uhci_ocsem);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_reset:
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate int
4377c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_reset(usba_pipe_handle_data_t *ph, usb_flags_t usb_flags)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	uhci_state_t		*uhcip = uhci_obtain_state(
4407c478bd9Sstevel@tonic-gate 					ph->p_usba_device->usb_root_hub_dip);
4417c478bd9Sstevel@tonic-gate 	uhci_pipe_private_t	*pp = (uhci_pipe_private_t *)ph->p_hcd_private;
4427c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
4457c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_reset: usb_flags = 0x%x", usb_flags);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/*
4487c478bd9Sstevel@tonic-gate 	 * Return failure immediately for any other pipe reset on the root
4497c478bd9Sstevel@tonic-gate 	 * hub except control or interrupt pipe.
4507c478bd9Sstevel@tonic-gate 	 */
4517c478bd9Sstevel@tonic-gate 	if (ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) {
4527c478bd9Sstevel@tonic-gate 		switch (UHCI_XFER_TYPE(&ph->p_ep)) {
4537c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_CONTROL:
4547c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
4557c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_reset: Pipe reset for root"
4567c478bd9Sstevel@tonic-gate 			    "hub control pipe successful");
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 			break;
4597c478bd9Sstevel@tonic-gate 		case USB_EP_ATTR_INTR:
4607c478bd9Sstevel@tonic-gate 			mutex_enter(&uhcip->uhci_int_mutex);
4617c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_pipe_state =
4627c478bd9Sstevel@tonic-gate 							UHCI_PIPE_STATE_IDLE;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 			/* Do interrupt pipe cleanup */
4657c478bd9Sstevel@tonic-gate 			uhci_root_hub_intr_pipe_cleanup(uhcip,
4667c478bd9Sstevel@tonic-gate 						USB_CR_PIPE_RESET);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
4697c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_reset: Pipe reset for "
4707c478bd9Sstevel@tonic-gate 			    "root hub interrupt pipe successful");
4717c478bd9Sstevel@tonic-gate 			mutex_exit(&uhcip->uhci_int_mutex);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 			break;
4747c478bd9Sstevel@tonic-gate 		default:
4757c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
4767c478bd9Sstevel@tonic-gate 			    "uhci_hcdi_pipe_close: Root hub pipe reset failed");
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 			return (USB_FAILURE);
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/*
4877c478bd9Sstevel@tonic-gate 	 * Set the active bit in to INACTIVE for all the remaining TD's of
4887c478bd9Sstevel@tonic-gate 	 * this end point.  Set the active bit for the dummy td. This will
4897c478bd9Sstevel@tonic-gate 	 * generate an interrupt at the end of the frame.  After receiving
4907c478bd9Sstevel@tonic-gate 	 * the interrupt, it is safe to to manipulate the lattice.
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	uhci_modify_td_active_bits(uhcip, pp);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/* Initialize the element pointer */
4957c478bd9Sstevel@tonic-gate 	if (UHCI_XFER_TYPE(eptd) != USB_EP_ATTR_ISOCH) {
4967c478bd9Sstevel@tonic-gate 		UHCI_SET_TERMINATE_BIT(pp->pp_qh->element_ptr);
4977c478bd9Sstevel@tonic-gate 		SetQH32(uhcip, pp->pp_qh->element_ptr,
4987c478bd9Sstevel@tonic-gate 		    TD_PADDR(pp->pp_qh->td_tailp));
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	(void) uhci_wait_for_sof(uhcip);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * Save the data toggle and clear the pipe.
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	switch (UHCI_XFER_TYPE(eptd)) {
5077c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_CONTROL:
5087c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_INTR:
5097c478bd9Sstevel@tonic-gate 		uhci_remove_tds_tws(uhcip, ph);
5107c478bd9Sstevel@tonic-gate 		break;
5117c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_BULK:
5127c478bd9Sstevel@tonic-gate 		SetQH32(uhcip, pp->pp_qh->element_ptr,
5137c478bd9Sstevel@tonic-gate 		    TD_PADDR(pp->pp_qh->td_tailp));
5147c478bd9Sstevel@tonic-gate 		uhci_remove_bulk_tds_tws(uhcip, pp, UHCI_IN_RESET);
5157c478bd9Sstevel@tonic-gate 		break;
5167c478bd9Sstevel@tonic-gate 	case USB_EP_ATTR_ISOCH:
5177c478bd9Sstevel@tonic-gate 		uhci_remove_isoc_tds_tws(uhcip, pp);
5187c478bd9Sstevel@tonic-gate 		break;
5197c478bd9Sstevel@tonic-gate 	default:
5207c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
5217c478bd9Sstevel@tonic-gate 		    "uhci_hcdi_pipe_reset: Unknown xfer type");
5227c478bd9Sstevel@tonic-gate 		break;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * Do the callback for the original client
5277c478bd9Sstevel@tonic-gate 	 * periodic IN request.
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	if (pp->pp_client_periodic_in_reqp) {
5307c478bd9Sstevel@tonic-gate 		uhci_hcdi_callback(uhcip, pp, ph, NULL, USB_CR_PIPE_RESET);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/*
5347c478bd9Sstevel@tonic-gate 	 * Since the endpoint is stripped of Transfer Descriptors (TD),
5357c478bd9Sstevel@tonic-gate 	 * reset the state of the periodic pipe to IDLE.
5367c478bd9Sstevel@tonic-gate 	 */
5377c478bd9Sstevel@tonic-gate 	pp->pp_state = UHCI_PIPE_STATE_IDLE;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_ctrl_xfer:
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate int
5497c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_ctrl_xfer(
5507c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
5517c478bd9Sstevel@tonic-gate 	usb_ctrl_req_t		*ctrl_reqp,
5527c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	uhci_state_t *uhcip = uhci_obtain_state(
5557c478bd9Sstevel@tonic-gate 				ph->p_usba_device->usb_root_hub_dip);
5567c478bd9Sstevel@tonic-gate 	uhci_pipe_private_t *pp = (uhci_pipe_private_t *)ph->p_hcd_private;
5577c478bd9Sstevel@tonic-gate 	int error = USB_SUCCESS;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
5607c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_ctrl_xfer: req=0x%p, ph=0x%p, flags=0x%x",
5617c478bd9Sstevel@tonic-gate 	    ctrl_reqp, ph, flags);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	ASSERT(pp->pp_state == UHCI_PIPE_STATE_IDLE);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/*
5687c478bd9Sstevel@tonic-gate 	 * Check and handle root hub control request.
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 	if (ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) {
5717c478bd9Sstevel@tonic-gate 		error = uhci_handle_root_hub_request(uhcip, ph, ctrl_reqp);
5727c478bd9Sstevel@tonic-gate 		mutex_exit(&uhcip->uhci_int_mutex);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		return (error);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/* Insert the td's on the endpoint */
5787c478bd9Sstevel@tonic-gate 	if ((error = uhci_insert_ctrl_td(uhcip, ph, ctrl_reqp, flags)) !=
5797c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
5807c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
5817c478bd9Sstevel@tonic-gate 		    "uhci_hcdi_pipe_ctrl_xfer: No resources");
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	return (error);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_bulk_xfer:
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate int
5937c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_bulk_xfer(usba_pipe_handle_data_t *pipe_handle,
5947c478bd9Sstevel@tonic-gate     usb_bulk_req_t *bulk_reqp, usb_flags_t usb_flags)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	int		error;
5977c478bd9Sstevel@tonic-gate 	uhci_state_t	*uhcip;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	uhcip = uhci_obtain_state(pipe_handle->p_usba_device->usb_root_hub_dip);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
6027c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_bulk_xfer: Flags = 0x%x", usb_flags);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/* Check the size of bulk request */
6057c478bd9Sstevel@tonic-gate 	if (bulk_reqp->bulk_len > UHCI_BULK_MAX_XFER_SIZE) {
6067c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
6077c478bd9Sstevel@tonic-gate 		    "uhci_hcdi_pipe_bulk_xfer: req size 0x%x is more than 0x%x",
6087c478bd9Sstevel@tonic-gate 		    bulk_reqp->bulk_len, UHCI_BULK_MAX_XFER_SIZE);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	/* Add the TD into the Host Controller's bulk list */
6167c478bd9Sstevel@tonic-gate 	if ((error = uhci_insert_bulk_td(uhcip, pipe_handle, bulk_reqp,
6177c478bd9Sstevel@tonic-gate 	    usb_flags)) != USB_SUCCESS) {
6187c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
6197c478bd9Sstevel@tonic-gate 		    "uhci_hcdi_pipe_bulk_xfer: uhci_insert_bulk_td failed");
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	return (error);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  * uhci_hcdi_bulk_transfer_size:
6297c478bd9Sstevel@tonic-gate  *	Return maximum bulk transfer size
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate int
6327c478bd9Sstevel@tonic-gate uhci_hcdi_bulk_transfer_size(
6337c478bd9Sstevel@tonic-gate 	usba_device_t	*usba_device,
6347c478bd9Sstevel@tonic-gate 	size_t		*size)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	uhci_state_t *uhcip = uhci_obtain_state(usba_device->usb_root_hub_dip);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
6397c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_bulk_transfer_size:");
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	*size = uhci_bulk_transfer_size;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate /*
6487c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_intr_xfer:
6497c478bd9Sstevel@tonic-gate  */
6507c478bd9Sstevel@tonic-gate int
6517c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_intr_xfer(
6527c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
6537c478bd9Sstevel@tonic-gate 	usb_intr_req_t		*req,
6547c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	uhci_state_t	*uhcip = uhci_obtain_state(
6577c478bd9Sstevel@tonic-gate 					ph->p_usba_device->usb_root_hub_dip);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
6607c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_intr_xfer: req=0x%p, uf=0x%x", req, flags);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	if (UHCI_XFER_DIR(&ph->p_ep) == USB_EP_DIR_IN) {
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		return (uhci_start_periodic_pipe_polling(uhcip, ph,
6657c478bd9Sstevel@tonic-gate 					(usb_opaque_t)req, flags));
6667c478bd9Sstevel@tonic-gate 	} else {
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		return (uhci_send_intr_data(uhcip, ph, req, flags));
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * uhci_send_intr_data():
6757c478bd9Sstevel@tonic-gate  *	send data to interrupt out pipe
6767c478bd9Sstevel@tonic-gate  */
6777c478bd9Sstevel@tonic-gate static int
6787c478bd9Sstevel@tonic-gate uhci_send_intr_data(
6797c478bd9Sstevel@tonic-gate 	uhci_state_t		*uhcip,
6807c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*pipe_handle,
6817c478bd9Sstevel@tonic-gate 	usb_intr_req_t		*req,
6827c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	int	rval = USB_SUCCESS;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, uhcip->uhci_log_hdl,
6877c478bd9Sstevel@tonic-gate 	    "uhci_send_intr_data:");
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/* Add the TD into the Host Controller's interrupt list */
6927c478bd9Sstevel@tonic-gate 	if ((rval = uhci_insert_intr_td(uhcip, pipe_handle, req, flags)) !=
6937c478bd9Sstevel@tonic-gate 	    USB_SUCCESS) {
6947c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
6957c478bd9Sstevel@tonic-gate 		    "uhci_send_intr_data: No resources");
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	return (rval);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_stop_intr_polling()
7057c478bd9Sstevel@tonic-gate  */
7067c478bd9Sstevel@tonic-gate int
7077c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_stop_intr_polling(
7087c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t *pipe_handle,
7097c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	uhci_state_t *uhcip =
7127c478bd9Sstevel@tonic-gate 		uhci_obtain_state(pipe_handle->p_usba_device->usb_root_hub_dip);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, uhcip->uhci_log_hdl,
7157c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_stop_intr_polling: ph = 0x%p fl = 0x%x",
7167c478bd9Sstevel@tonic-gate 	    (void *)pipe_handle, flags);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	return (uhci_stop_periodic_pipe_polling(uhcip, pipe_handle, flags));
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate  * uhci_hcdi_get_current_frame_number
7247c478bd9Sstevel@tonic-gate  *	Returns the current frame number
7257c478bd9Sstevel@tonic-gate  */
7267c478bd9Sstevel@tonic-gate usb_frame_number_t
7277c478bd9Sstevel@tonic-gate uhci_hcdi_get_current_frame_number(usba_device_t *usba_device)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	uhci_state_t *uhcip = uhci_obtain_state(usba_device->usb_root_hub_dip);
7307c478bd9Sstevel@tonic-gate 	usb_frame_number_t frame_number;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
7337c478bd9Sstevel@tonic-gate 	frame_number = uhci_get_sw_frame_number(uhcip);
7347c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
7377c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_get_current_frame_number: %llx", frame_number);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	return (frame_number);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate /*
7447c478bd9Sstevel@tonic-gate  * uhci_hcdi_get_max_isoc_pkts
7457c478bd9Sstevel@tonic-gate  *	Returns the maximum number of isoc packets per USB Isoch request
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate uint_t
7487c478bd9Sstevel@tonic-gate uhci_hcdi_get_max_isoc_pkts(usba_device_t *usba_device)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	uhci_state_t *uhcip = uhci_obtain_state(usba_device->usb_root_hub_dip);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
7537c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_get_max_isoc_pkts: 0x%x", UHCI_MAX_ISOC_PKTS);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	return (UHCI_MAX_ISOC_PKTS);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate /*
7607c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_isoc_xfer:
7617c478bd9Sstevel@tonic-gate  */
7627c478bd9Sstevel@tonic-gate int
7637c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_isoc_xfer(
7647c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
7657c478bd9Sstevel@tonic-gate 	usb_isoc_req_t		*isoc_reqp,
7667c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	uhci_state_t	*uhcip;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	uhcip = uhci_obtain_state(ph->p_usba_device->usb_root_hub_dip);
7717c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
7727c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_isoc_xfer: req=0x%p, uf=0x%x", isoc_reqp, flags);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (UHCI_XFER_DIR(&ph->p_ep) == USB_EP_DIR_IN) {
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		return (uhci_start_periodic_pipe_polling(uhcip, ph,
7777c478bd9Sstevel@tonic-gate 					(usb_opaque_t)isoc_reqp, flags));
7787c478bd9Sstevel@tonic-gate 	} else {
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		return (uhci_pipe_send_isoc_data(uhcip, ph, isoc_reqp, flags));
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate /*
7867c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_stop_isoc_polling()
7877c478bd9Sstevel@tonic-gate  */
7887c478bd9Sstevel@tonic-gate int
7897c478bd9Sstevel@tonic-gate uhci_hcdi_pipe_stop_isoc_polling(
7907c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
7917c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	uhci_state_t *uhcip =
7947c478bd9Sstevel@tonic-gate 		uhci_obtain_state(ph->p_usba_device->usb_root_hub_dip);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_LISTS, uhcip->uhci_log_hdl,
7977c478bd9Sstevel@tonic-gate 	    "uhci_hcdi_pipe_stop_isoc_polling: ph = 0x%p fl = 0x%x",
7987c478bd9Sstevel@tonic-gate 	    (void *)ph, flags);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	return (uhci_stop_periodic_pipe_polling(uhcip, ph, flags));
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate /*
8057c478bd9Sstevel@tonic-gate  * uhci_start_periodic_pipe_polling:
8067c478bd9Sstevel@tonic-gate  */
8077c478bd9Sstevel@tonic-gate static int
8087c478bd9Sstevel@tonic-gate uhci_start_periodic_pipe_polling(
8097c478bd9Sstevel@tonic-gate 	uhci_state_t		*uhcip,
8107c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
8117c478bd9Sstevel@tonic-gate 	usb_opaque_t		in_reqp,
8127c478bd9Sstevel@tonic-gate 	usb_flags_t		flags)
8137c478bd9Sstevel@tonic-gate {
8147c478bd9Sstevel@tonic-gate 	int			n, num_tds;
8157c478bd9Sstevel@tonic-gate 	int			error = USB_SUCCESS;
8167c478bd9Sstevel@tonic-gate 	usb_intr_req_t		*intr_reqp = (usb_intr_req_t *)in_reqp;
8177c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
8187c478bd9Sstevel@tonic-gate 	uhci_pipe_private_t	*pp = (uhci_pipe_private_t *)ph->p_hcd_private;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
8217c478bd9Sstevel@tonic-gate 	    "uhci_start_periodic_pipe_polling: flags: 0x%x, ep%d",
8227c478bd9Sstevel@tonic-gate 	    flags, eptd->bEndpointAddress);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	if (ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) {
8277c478bd9Sstevel@tonic-gate 		uint_t	pipe_state = uhcip->uhci_root_hub.rh_pipe_state;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		ASSERT(pipe_state == UHCI_PIPE_STATE_IDLE);
8307c478bd9Sstevel@tonic-gate 		ASSERT(UHCI_XFER_DIR(eptd) == USB_EP_DIR_IN);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		/* ONE_XFER not supported */
8337c478bd9Sstevel@tonic-gate 		ASSERT((intr_reqp->intr_attributes &
8347c478bd9Sstevel@tonic-gate 		    USB_ATTRS_ONE_XFER) == 0);
8357c478bd9Sstevel@tonic-gate 		ASSERT(uhcip->uhci_root_hub.rh_client_intr_req == NULL);
8367c478bd9Sstevel@tonic-gate 		uhcip->uhci_root_hub.rh_client_intr_req = intr_reqp;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		if ((error = uhci_root_hub_allocate_intr_pipe_resource(
8397c478bd9Sstevel@tonic-gate 		    uhcip, flags)) != USB_SUCCESS) {
8407c478bd9Sstevel@tonic-gate 			/* reset the client interrupt request pointer */
8417c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_client_intr_req = NULL;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 			mutex_exit(&uhcip->uhci_int_mutex);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			return (error);
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 		uhcip->uhci_root_hub.rh_pipe_state = USB_PIPE_STATE_ACTIVE;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
8517c478bd9Sstevel@tonic-gate 		    "uhci_start_periodic_pipe_polling: "
8527c478bd9Sstevel@tonic-gate 		    "Start intr polling for root hub successful");
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		/* check if we need to send the reset data up? */
8557c478bd9Sstevel@tonic-gate 		if (uhcip->uhci_root_hub.rh_status) {
8567c478bd9Sstevel@tonic-gate 			uhci_root_hub_reset_occurred(uhcip,
8577c478bd9Sstevel@tonic-gate 			    uhcip->uhci_root_hub.rh_status - 1);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_status = 0;
8607c478bd9Sstevel@tonic-gate 		}
8617c478bd9Sstevel@tonic-gate 		mutex_exit(&uhcip->uhci_int_mutex);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		return (error);
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/* save the original client's periodic IN request */
8677c478bd9Sstevel@tonic-gate 	pp->pp_client_periodic_in_reqp = in_reqp;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	ASSERT(pp->pp_state != UHCI_PIPE_STATE_ACTIVE);
8707c478bd9Sstevel@tonic-gate 	/*
8717c478bd9Sstevel@tonic-gate 	 *
8727c478bd9Sstevel@tonic-gate 	 * This pipe is uninitialized. If it is an isoc
8737c478bd9Sstevel@tonic-gate 	 * receive request, insert four times the same
8747c478bd9Sstevel@tonic-gate 	 * request so that we do not lose any frames.
8757c478bd9Sstevel@tonic-gate 	 */
8767c478bd9Sstevel@tonic-gate 	if (UHCI_XFER_TYPE(eptd) == USB_EP_ATTR_ISOCH) {
8777c478bd9Sstevel@tonic-gate 		for (n = 0; n < 5; n++) {
8787c478bd9Sstevel@tonic-gate 			if ((error = uhci_start_isoc_receive_polling(
8797c478bd9Sstevel@tonic-gate 			    uhcip, ph, NULL, flags)) != USB_SUCCESS) {
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(PRINT_MASK_INTR,
8827c478bd9Sstevel@tonic-gate 				    uhcip->uhci_log_hdl,
8837c478bd9Sstevel@tonic-gate 				    "uhci_start_periodic_pipe_polling: "
8847c478bd9Sstevel@tonic-gate 				    "Start isoc polling failed %d", n);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 				pp->pp_client_periodic_in_reqp = NULL;
8877c478bd9Sstevel@tonic-gate 				mutex_exit(&uhcip->uhci_int_mutex);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 				return (error);
8907c478bd9Sstevel@tonic-gate 			}
8917c478bd9Sstevel@tonic-gate 		}
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if (UHCI_XFER_TYPE(eptd) == USB_EP_ATTR_INTR) {
8957c478bd9Sstevel@tonic-gate 		if ((pp->pp_node < POLLING_FREQ_7MS) &&
8967c478bd9Sstevel@tonic-gate 		    (!(intr_reqp->intr_attributes & USB_ATTRS_ONE_XFER))) {
8977c478bd9Sstevel@tonic-gate 			num_tds = 5;
8987c478bd9Sstevel@tonic-gate 		} else {
8997c478bd9Sstevel@tonic-gate 			num_tds = 1;
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		/*
9037c478bd9Sstevel@tonic-gate 		 * This pipe is uninitialized.
9047c478bd9Sstevel@tonic-gate 		 * Insert a TD on the interrupt ED.
9057c478bd9Sstevel@tonic-gate 		 */
9067c478bd9Sstevel@tonic-gate 		for (n = 0; n < num_tds; n++) {
9077c478bd9Sstevel@tonic-gate 			if ((error = uhci_insert_intr_td(uhcip, ph, NULL,
9087c478bd9Sstevel@tonic-gate 			    flags)) != USB_SUCCESS) {
9097c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(PRINT_MASK_INTR,
9107c478bd9Sstevel@tonic-gate 				    uhcip->uhci_log_hdl,
9117c478bd9Sstevel@tonic-gate 				    "uhci_start_periodic_pipe_polling: "
9127c478bd9Sstevel@tonic-gate 				    "Start polling failed");
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 				pp->pp_client_periodic_in_reqp = NULL;
9157c478bd9Sstevel@tonic-gate 				mutex_exit(&uhcip->uhci_int_mutex);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 				return (error);
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	pp->pp_state = UHCI_PIPE_STATE_ACTIVE;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	return (error);
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate  * uhci_hcdi_periodic_pipe_stop_polling:
9327c478bd9Sstevel@tonic-gate  */
9337c478bd9Sstevel@tonic-gate static int
9347c478bd9Sstevel@tonic-gate uhci_stop_periodic_pipe_polling(uhci_state_t *uhcip,
9357c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t  *ph, usb_flags_t flags)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	uhci_pipe_private_t	*pp = (uhci_pipe_private_t *)ph->p_hcd_private;
9387c478bd9Sstevel@tonic-gate 	usb_ep_descr_t		*eptd = &ph->p_ep;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
9417c478bd9Sstevel@tonic-gate 	    "uhci_stop_periodic_pipe_polling: flags = 0x%x", flags);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	if (ph->p_usba_device->usb_addr == ROOT_HUB_ADDR) {
9447c478bd9Sstevel@tonic-gate 		ASSERT(UHCI_XFER_DIR(eptd) == USB_EP_DIR_IN);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		mutex_enter(&uhcip->uhci_int_mutex);
9477c478bd9Sstevel@tonic-gate 		if (uhcip->uhci_root_hub.rh_pipe_state ==
9487c478bd9Sstevel@tonic-gate 		    UHCI_PIPE_STATE_ACTIVE) {
9497c478bd9Sstevel@tonic-gate 			uhcip->uhci_root_hub.rh_pipe_state =
9507c478bd9Sstevel@tonic-gate 				UHCI_PIPE_STATE_IDLE;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 			/* Do interrupt pipe cleanup */
9537c478bd9Sstevel@tonic-gate 			uhci_root_hub_intr_pipe_cleanup(uhcip,
9547c478bd9Sstevel@tonic-gate 						USB_CR_STOPPED_POLLING);
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
9577c478bd9Sstevel@tonic-gate 			    "uhci_stop_periodic_pipe_polling: Stop intr "
9587c478bd9Sstevel@tonic-gate 			    "polling for root hub successful");
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		} else {
9617c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_INTR, uhcip->uhci_log_hdl,
9627c478bd9Sstevel@tonic-gate 			    "uhci_stop_periodic_pipe_polling: "
9637c478bd9Sstevel@tonic-gate 			    "Intr polling for root hub is already stopped");
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 		mutex_exit(&uhcip->uhci_int_mutex);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	if (pp->pp_state != UHCI_PIPE_STATE_ACTIVE) {
9737c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_INTR, uhcip->uhci_log_hdl,
9747c478bd9Sstevel@tonic-gate 		    "uhci_stop_periodic_pipe_polling: Polling already stopped");
9757c478bd9Sstevel@tonic-gate 		mutex_exit(&uhcip->uhci_int_mutex);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	/*
9817c478bd9Sstevel@tonic-gate 	 * Set the terminate bits in all the tds in the queue and
9827c478bd9Sstevel@tonic-gate 	 * in the element_ptr.
9837c478bd9Sstevel@tonic-gate 	 * Do not deallocate the bandwidth or tear down the DMA
9847c478bd9Sstevel@tonic-gate 	 */
9857c478bd9Sstevel@tonic-gate 	uhci_modify_td_active_bits(uhcip, pp);
9867c478bd9Sstevel@tonic-gate 	(void) uhci_wait_for_sof(uhcip);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if (UHCI_XFER_TYPE(eptd) == USB_EP_ATTR_ISOCH) {
9897c478bd9Sstevel@tonic-gate 		uhci_remove_isoc_tds_tws(uhcip, pp);
9907c478bd9Sstevel@tonic-gate 		pp->pp_state = UHCI_PIPE_STATE_IDLE;
9917c478bd9Sstevel@tonic-gate 	} else {
9927c478bd9Sstevel@tonic-gate 		UHCI_SET_TERMINATE_BIT(pp->pp_qh->element_ptr);
9937c478bd9Sstevel@tonic-gate 		uhci_update_intr_td_data_toggle(uhcip, pp);
9947c478bd9Sstevel@tonic-gate 		SetQH32(uhcip, pp->pp_qh->element_ptr,
9957c478bd9Sstevel@tonic-gate 		    TD_PADDR(pp->pp_qh->td_tailp));
9967c478bd9Sstevel@tonic-gate 		uhci_remove_tds_tws(uhcip, ph);
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	pp->pp_state = UHCI_PIPE_STATE_IDLE;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (pp->pp_client_periodic_in_reqp) {
10027c478bd9Sstevel@tonic-gate 		uhci_hcdi_callback(uhcip, pp, ph, NULL, USB_CR_STOPPED_POLLING);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate /*
10117c478bd9Sstevel@tonic-gate  * uhci_hcdi_pipe_send_isoc_data:
10127c478bd9Sstevel@tonic-gate  *	Handles the isoc write request.
10137c478bd9Sstevel@tonic-gate  */
10147c478bd9Sstevel@tonic-gate static int
10157c478bd9Sstevel@tonic-gate uhci_pipe_send_isoc_data(
10167c478bd9Sstevel@tonic-gate 	uhci_state_t		*uhcip,
10177c478bd9Sstevel@tonic-gate 	usba_pipe_handle_data_t	*ph,
10187c478bd9Sstevel@tonic-gate 	usb_isoc_req_t		*isoc_req,
10197c478bd9Sstevel@tonic-gate 	usb_flags_t		usb_flags)
10207c478bd9Sstevel@tonic-gate {
10217c478bd9Sstevel@tonic-gate 	int			error;
10227c478bd9Sstevel@tonic-gate 	size_t			max_isoc_xfer_sz, length;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
10257c478bd9Sstevel@tonic-gate 	    "uhci_pipe_send_isoc_data: isoc_req = %p flags = %x",
10267c478bd9Sstevel@tonic-gate 	    isoc_req, usb_flags);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	ASSERT(isoc_req->isoc_pkts_count < UHCI_MAX_ISOC_PKTS);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/* Calculate the maximum isochronous transfer size */
10317c478bd9Sstevel@tonic-gate 	max_isoc_xfer_sz = UHCI_MAX_ISOC_PKTS * ph->p_ep.wMaxPacketSize;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	/* Check the size of isochronous request */
10347c478bd9Sstevel@tonic-gate 	ASSERT(isoc_req->isoc_data != NULL);
10357c478bd9Sstevel@tonic-gate 	length = isoc_req->isoc_data->b_wptr - isoc_req->isoc_data->b_rptr;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if (length > max_isoc_xfer_sz) {
10387c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
10397c478bd9Sstevel@tonic-gate 		    "uhci_pipe_send_isoc_data: Maximum isoc request size %lx "
10407c478bd9Sstevel@tonic-gate 		    "Given isoc request size %lx", max_isoc_xfer_sz, length);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 		return (USB_INVALID_REQUEST);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	/*
10477c478bd9Sstevel@tonic-gate 	 * Check whether we can insert these tds?
10487c478bd9Sstevel@tonic-gate 	 * At any point of time, we can insert maximum of 1024 isoc td's,
10497c478bd9Sstevel@tonic-gate 	 * size of frame list table.
10507c478bd9Sstevel@tonic-gate 	 */
10517c478bd9Sstevel@tonic-gate 	if (isoc_req->isoc_pkts_count > UHCI_MAX_ISOC_PKTS) {
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ISOC, uhcip->uhci_log_hdl,
10547c478bd9Sstevel@tonic-gate 		    "uhci_pipe_send_isoc_data: request too big");
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 		return (USB_INVALID_REQUEST);
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/* Add the TD into the Host Controller's isoc list */
10607c478bd9Sstevel@tonic-gate 	mutex_enter(&uhcip->uhci_int_mutex);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	if ((error = uhci_insert_isoc_td(uhcip, ph, isoc_req,
10637c478bd9Sstevel@tonic-gate 	    length, usb_flags)) != USB_SUCCESS) {
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ISOC, uhcip->uhci_log_hdl,
10667c478bd9Sstevel@tonic-gate 		    "uhci_pipe_send_isoc_data: Unable to insert the isoc_req,"
10677c478bd9Sstevel@tonic-gate 		    "Error = %d", error);
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 	mutex_exit(&uhcip->uhci_int_mutex);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	return (error);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate /*
10767c478bd9Sstevel@tonic-gate  * uhci_update_intr_td_data_toggle
10777c478bd9Sstevel@tonic-gate  *	Update the data toggle and save in the usba_device structure
10787c478bd9Sstevel@tonic-gate  */
10797c478bd9Sstevel@tonic-gate static void
10807c478bd9Sstevel@tonic-gate uhci_update_intr_td_data_toggle(uhci_state_t *uhcip, uhci_pipe_private_t *pp)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate 	uint32_t	paddr_tail, element_ptr;
10837c478bd9Sstevel@tonic-gate 	uhci_td_t	*next_td;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	/* Find the next td that would have been executed */
10867c478bd9Sstevel@tonic-gate 	element_ptr = GetQH32(uhcip, pp->pp_qh->element_ptr) &
10877c478bd9Sstevel@tonic-gate 						QH_ELEMENT_PTR_MASK;
10887c478bd9Sstevel@tonic-gate 	next_td = TD_VADDR(element_ptr);
10897c478bd9Sstevel@tonic-gate 	paddr_tail = TD_PADDR(pp->pp_qh->td_tailp);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/*
10927c478bd9Sstevel@tonic-gate 	 * If element_ptr points to the dummy td, then the data toggle in
10937c478bd9Sstevel@tonic-gate 	 * pp_data_toggle is correct. Otherwise update the data toggle in
10947c478bd9Sstevel@tonic-gate 	 * the pipe private
10957c478bd9Sstevel@tonic-gate 	 */
10967c478bd9Sstevel@tonic-gate 	if (element_ptr != paddr_tail) {
10977c478bd9Sstevel@tonic-gate 		pp->pp_data_toggle = GetTD_dtogg(uhcip, next_td);
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_HCDI, uhcip->uhci_log_hdl,
11017c478bd9Sstevel@tonic-gate 	    "uhci_update_intr_td_data_toggle: "
11027c478bd9Sstevel@tonic-gate 	    "pp %p toggle %x element ptr %x ptail %x",
11037c478bd9Sstevel@tonic-gate 	    pp, pp->pp_data_toggle, element_ptr, paddr_tail);
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	uhci_save_data_toggle(pp);
11067c478bd9Sstevel@tonic-gate }
1107