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
50a05e705Slc  * Common Development and Distribution License (the "License").
60a05e705Slc  * 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  *
21112116d8Sfb  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2348bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26a5eb7107SBryan Cantrill /*
27*0d2006e4SRobert Mustacchi  * Copyright 2019 Joyent, Inc.
28a5eb7107SBryan Cantrill  */
29a5eb7107SBryan Cantrill 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * UGEN: USB Generic Driver support code
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * This code provides entry points called by the ugen driver or other
347c478bd9Sstevel@tonic-gate  * drivers that want to export a ugen interface
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * The "Universal Generic Driver"  (UGEN) for USB devices provides interfaces
377c478bd9Sstevel@tonic-gate  * to  talk to	USB  devices.  This is	very  useful for  Point of Sale sale
387c478bd9Sstevel@tonic-gate  * devices and other simple  devices like  USB	scanner, USB palm  pilot.
397c478bd9Sstevel@tonic-gate  * The UGEN provides a system call interface to USB  devices  enabling
4048bbca81SDaniel Hoffman  * a USB device vendor to write an application for their
417c478bd9Sstevel@tonic-gate  * device instead of  writing a driver. This facilitates the vendor to write
427c478bd9Sstevel@tonic-gate  * device management s/w quickly in userland.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * UGEN supports read/write/poll entry points. An application can be written
457c478bd9Sstevel@tonic-gate  * using  read/write/aioread/aiowrite/poll  system calls to communicate
467c478bd9Sstevel@tonic-gate  * with the device.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * XXX Theory of Operations
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
517c478bd9Sstevel@tonic-gate #include <sys/usb/usba.h>
527c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
53d29f5a71Szhigang lu - Sun Microsystems - Beijing China #include <sys/strsun.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include "sys/usb/clients/ugen/usb_ugen.h"
567c478bd9Sstevel@tonic-gate #include "sys/usb/usba/usba_ugen.h"
577c478bd9Sstevel@tonic-gate #include "sys/usb/usba/usba_ugend.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* Debugging information */
604610e4a0Sfrits uint_t	ugen_errmask		= (uint_t)UGEN_PRINT_ALL;
614610e4a0Sfrits uint_t	ugen_errlevel		= USB_LOG_L4;
624610e4a0Sfrits uint_t	ugen_instance_debug	= (uint_t)-1;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* default endpoint descriptor */
657c478bd9Sstevel@tonic-gate static usb_ep_descr_t  ugen_default_ep_descr =
667c478bd9Sstevel@tonic-gate 	{7, 5, 0, USB_EP_ATTR_CONTROL, 8, 0};
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* tunables */
694610e4a0Sfrits int	ugen_busy_loop		= 60;	/* secs */
704610e4a0Sfrits int	ugen_ctrl_timeout	= 10;
714610e4a0Sfrits int	ugen_bulk_timeout	= 10;
724610e4a0Sfrits int	ugen_intr_timeout	= 10;
734610e4a0Sfrits int	ugen_enable_pm		= 0;
740a05e705Slc int	ugen_isoc_buf_limit	= 1000;	/* ms */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* local function prototypes */
787c478bd9Sstevel@tonic-gate static int	ugen_cleanup(ugen_state_t *);
797c478bd9Sstevel@tonic-gate static int	ugen_cpr_suspend(ugen_state_t *);
807c478bd9Sstevel@tonic-gate static void	ugen_cpr_resume(ugen_state_t *);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static void	ugen_restore_state(ugen_state_t *);
837c478bd9Sstevel@tonic-gate static int	ugen_check_open_flags(ugen_state_t *, dev_t, int);
847c478bd9Sstevel@tonic-gate static int	ugen_strategy(struct buf *);
857c478bd9Sstevel@tonic-gate static void	ugen_minphys(struct buf *);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static void	ugen_pm_init(ugen_state_t *);
887c478bd9Sstevel@tonic-gate static void	ugen_pm_destroy(ugen_state_t *);
897c478bd9Sstevel@tonic-gate static void	ugen_pm_busy_component(ugen_state_t *);
907c478bd9Sstevel@tonic-gate static void	ugen_pm_idle_component(ugen_state_t *);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* endpoint xfer and status management */
937c478bd9Sstevel@tonic-gate static int	ugen_epxs_init(ugen_state_t *);
947c478bd9Sstevel@tonic-gate static void	ugen_epxs_destroy(ugen_state_t *);
957c478bd9Sstevel@tonic-gate static int	ugen_epxs_data_init(ugen_state_t *, usb_ep_data_t *,
967c478bd9Sstevel@tonic-gate 					uchar_t, uchar_t, uchar_t, uchar_t);
977c478bd9Sstevel@tonic-gate static void	ugen_epxs_data_destroy(ugen_state_t *, ugen_ep_t *);
987c478bd9Sstevel@tonic-gate static int	ugen_epxs_minor_nodes_create(ugen_state_t *,
997c478bd9Sstevel@tonic-gate 					usb_ep_descr_t *, uchar_t,
1007c478bd9Sstevel@tonic-gate 					uchar_t, uchar_t, uchar_t);
1017c478bd9Sstevel@tonic-gate static int	ugen_epxs_check_open_nodes(ugen_state_t *);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static int	ugen_epx_open(ugen_state_t *, dev_t, int);
1047c478bd9Sstevel@tonic-gate static void	ugen_epx_close(ugen_state_t *, dev_t, int);
1057c478bd9Sstevel@tonic-gate static void	ugen_epx_shutdown(ugen_state_t *);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int	ugen_epx_open_pipe(ugen_state_t *, ugen_ep_t *, int);
1087c478bd9Sstevel@tonic-gate static void	ugen_epx_close_pipe(ugen_state_t *, ugen_ep_t *);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static int	ugen_epx_req(ugen_state_t *, struct buf *);
1117c478bd9Sstevel@tonic-gate static int	ugen_epx_ctrl_req(ugen_state_t *, ugen_ep_t *,
1127c478bd9Sstevel@tonic-gate 					struct buf *, boolean_t *);
1137c478bd9Sstevel@tonic-gate static void	ugen_epx_ctrl_req_cb(usb_pipe_handle_t, usb_ctrl_req_t *);
1147c478bd9Sstevel@tonic-gate static int	ugen_epx_bulk_req(ugen_state_t *, ugen_ep_t *,
1157c478bd9Sstevel@tonic-gate 					struct buf *, boolean_t *);
1167c478bd9Sstevel@tonic-gate static void	ugen_epx_bulk_req_cb(usb_pipe_handle_t, usb_bulk_req_t *);
1177c478bd9Sstevel@tonic-gate static int	ugen_epx_intr_IN_req(ugen_state_t *, ugen_ep_t *,
1187c478bd9Sstevel@tonic-gate 					struct buf *, boolean_t *);
1197c478bd9Sstevel@tonic-gate static int	ugen_epx_intr_IN_start_polling(ugen_state_t *, ugen_ep_t *);
1207c478bd9Sstevel@tonic-gate static void	ugen_epx_intr_IN_stop_polling(ugen_state_t *, ugen_ep_t *);
1217c478bd9Sstevel@tonic-gate static void	ugen_epx_intr_IN_req_cb(usb_pipe_handle_t, usb_intr_req_t *);
1227c478bd9Sstevel@tonic-gate static int	ugen_epx_intr_OUT_req(ugen_state_t *, ugen_ep_t *,
1237c478bd9Sstevel@tonic-gate 					struct buf *, boolean_t *);
1247c478bd9Sstevel@tonic-gate static void	ugen_epx_intr_OUT_req_cb(usb_pipe_handle_t, usb_intr_req_t *);
1250a05e705Slc static int	ugen_epx_isoc_IN_req(ugen_state_t *, ugen_ep_t *,
1260a05e705Slc 					struct buf *, boolean_t *);
1270a05e705Slc static int	ugen_epx_isoc_IN_start_polling(ugen_state_t *, ugen_ep_t *);
1280a05e705Slc static void	ugen_epx_isoc_IN_stop_polling(ugen_state_t *, ugen_ep_t *);
1290a05e705Slc static void	ugen_epx_isoc_IN_req_cb(usb_pipe_handle_t, usb_isoc_req_t *);
1300a05e705Slc static int	ugen_epx_isoc_OUT_req(ugen_state_t *, ugen_ep_t *,
1310a05e705Slc 					struct buf *, boolean_t *);
1320a05e705Slc static void	ugen_epx_isoc_OUT_req_cb(usb_pipe_handle_t, usb_isoc_req_t *);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static int	ugen_eps_open(ugen_state_t *, dev_t, int);
1357c478bd9Sstevel@tonic-gate static void	ugen_eps_close(ugen_state_t *, dev_t, int);
1367c478bd9Sstevel@tonic-gate static int	ugen_eps_req(ugen_state_t *, struct buf *);
1377c478bd9Sstevel@tonic-gate static void	ugen_update_ep_descr(ugen_state_t *, ugen_ep_t *);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /* device status management */
1407c478bd9Sstevel@tonic-gate static int	ugen_ds_init(ugen_state_t *);
1417c478bd9Sstevel@tonic-gate static void	ugen_ds_destroy(ugen_state_t *);
1427c478bd9Sstevel@tonic-gate static int	ugen_ds_open(ugen_state_t *, dev_t, int);
1437c478bd9Sstevel@tonic-gate static void	ugen_ds_close(ugen_state_t *, dev_t, int);
1447c478bd9Sstevel@tonic-gate static int	ugen_ds_req(ugen_state_t *, struct buf *);
1457c478bd9Sstevel@tonic-gate static void	ugen_ds_change(ugen_state_t *);
1467c478bd9Sstevel@tonic-gate static int	ugen_ds_minor_nodes_create(ugen_state_t *);
1477c478bd9Sstevel@tonic-gate static void	ugen_ds_poll_wakeup(ugen_state_t *);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /* utility functions */
1507c478bd9Sstevel@tonic-gate static int	ugen_minor_index_create(ugen_state_t *, ugen_minor_t);
1517c478bd9Sstevel@tonic-gate static ugen_minor_t ugen_devt2minor(ugen_state_t *, dev_t);
1527c478bd9Sstevel@tonic-gate static void	ugen_minor_node_table_create(ugen_state_t *);
1537c478bd9Sstevel@tonic-gate static void	ugen_minor_node_table_destroy(ugen_state_t *);
1547c478bd9Sstevel@tonic-gate static void	ugen_minor_node_table_shrink(ugen_state_t *);
1557c478bd9Sstevel@tonic-gate static int	ugen_cr2lcstat(int);
1567c478bd9Sstevel@tonic-gate static void	ugen_check_mask(uint_t, uint_t *, uint_t *);
15796603521Sfrits static int	ugen_is_valid_minor_node(ugen_state_t *, dev_t);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static kmutex_t	ugen_devt_list_mutex;
1607c478bd9Sstevel@tonic-gate static ugen_devt_list_entry_t ugen_devt_list;
1617c478bd9Sstevel@tonic-gate static ugen_devt_cache_entry_t ugen_devt_cache[UGEN_DEVT_CACHE_SIZE];
1627c478bd9Sstevel@tonic-gate static uint_t	ugen_devt_cache_index;
1637c478bd9Sstevel@tonic-gate static void	ugen_store_devt(ugen_state_t *, minor_t);
1647c478bd9Sstevel@tonic-gate static ugen_state_t *ugen_devt2state(dev_t);
1657c478bd9Sstevel@tonic-gate static void	ugen_free_devt(ugen_state_t *);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * usb_ugen entry points
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * usb_ugen_get_hdl:
1717c478bd9Sstevel@tonic-gate  *	allocate and initialize handle
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate usb_ugen_hdl_t
usb_ugen_get_hdl(dev_info_t * dip,usb_ugen_info_t * usb_ugen_info)1747c478bd9Sstevel@tonic-gate usb_ugen_get_hdl(dev_info_t *dip, usb_ugen_info_t *usb_ugen_info)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*hdl = kmem_zalloc(sizeof (*hdl), KM_SLEEP);
1777c478bd9Sstevel@tonic-gate 	ugen_state_t		*ugenp = kmem_zalloc(sizeof (ugen_state_t),
1780a05e705Slc 	    KM_SLEEP);
1797c478bd9Sstevel@tonic-gate 	uint_t			len, shift, limit;
1807c478bd9Sstevel@tonic-gate 	int			rval;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	hdl->hdl_ugenp = ugenp;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* masks may not overlap */
1857c478bd9Sstevel@tonic-gate 	if (usb_ugen_info->usb_ugen_minor_node_ugen_bits_mask &
1867c478bd9Sstevel@tonic-gate 	    usb_ugen_info->usb_ugen_minor_node_instance_mask) {
1877c478bd9Sstevel@tonic-gate 		usb_ugen_release_hdl((usb_ugen_hdl_t)hdl);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		return (NULL);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if ((rval = usb_get_dev_data(dip, &ugenp->ug_dev_data,
1937c478bd9Sstevel@tonic-gate 	    usb_owns_device(dip) ? USB_PARSE_LVL_ALL : USB_PARSE_LVL_IF,
1947c478bd9Sstevel@tonic-gate 	    0)) != USB_SUCCESS) {
1957c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
1967c478bd9Sstevel@tonic-gate 		    "usb_ugen_attach: usb_get_dev_data failed, rval=%d", rval);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		return (NULL);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/* Initialize state structure for this instance */
2027c478bd9Sstevel@tonic-gate 	mutex_init(&ugenp->ug_mutex, NULL, MUTEX_DRIVER,
2030a05e705Slc 	    ugenp->ug_dev_data->dev_iblock_cookie);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
2067c478bd9Sstevel@tonic-gate 	ugenp->ug_dip		= dip;
2077c478bd9Sstevel@tonic-gate 	ugenp->ug_instance	= ddi_get_instance(dip);
2087c478bd9Sstevel@tonic-gate 	ugenp->ug_hdl		= hdl;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* Allocate a log handle for debug/error messages */
2117c478bd9Sstevel@tonic-gate 	if (strcmp(ddi_driver_name(dip), "ugen") != 0) {
2127c478bd9Sstevel@tonic-gate 		char	*name;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		len = strlen(ddi_driver_name(dip)) + sizeof ("_ugen") + 1;
2157c478bd9Sstevel@tonic-gate 		name = kmem_alloc(len, KM_SLEEP);
2167c478bd9Sstevel@tonic-gate 		(void) snprintf(name, len, "%s_ugen", ddi_driver_name(dip));
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		ugenp->ug_log_hdl = usb_alloc_log_hdl(dip, name, &ugen_errlevel,
2190a05e705Slc 		    &ugen_errmask, &ugen_instance_debug, 0);
2207c478bd9Sstevel@tonic-gate 		hdl->hdl_log_name = name;
2217c478bd9Sstevel@tonic-gate 		hdl->hdl_log_name_length = len;
2227c478bd9Sstevel@tonic-gate 	} else {
2237c478bd9Sstevel@tonic-gate 		ugenp->ug_log_hdl = usb_alloc_log_hdl(dip, "ugen",
2240a05e705Slc 		    &ugen_errlevel,
2250a05e705Slc 		    &ugen_errmask, &ugen_instance_debug, 0);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	hdl->hdl_dip = dip;
2297c478bd9Sstevel@tonic-gate 	hdl->hdl_flags = usb_ugen_info->usb_ugen_flags;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	ugen_check_mask(usb_ugen_info->usb_ugen_minor_node_ugen_bits_mask,
2320a05e705Slc 	    &shift, &limit);
2337c478bd9Sstevel@tonic-gate 	if (limit == 0) {
2347c478bd9Sstevel@tonic-gate 		usb_ugen_release_hdl((usb_ugen_hdl_t)hdl);
2357c478bd9Sstevel@tonic-gate 		mutex_exit(&ugenp->ug_mutex);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		return (NULL);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 	hdl->hdl_minor_node_ugen_bits_mask = usb_ugen_info->
2400a05e705Slc 	    usb_ugen_minor_node_ugen_bits_mask;
2417c478bd9Sstevel@tonic-gate 	hdl->hdl_minor_node_ugen_bits_shift = shift;
2427c478bd9Sstevel@tonic-gate 	hdl->hdl_minor_node_ugen_bits_limit = limit;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	ugen_check_mask(usb_ugen_info->usb_ugen_minor_node_instance_mask,
2450a05e705Slc 	    &shift, &limit);
2467c478bd9Sstevel@tonic-gate 	if (limit == 0) {
2477c478bd9Sstevel@tonic-gate 		usb_ugen_release_hdl((usb_ugen_hdl_t)hdl);
2487c478bd9Sstevel@tonic-gate 		mutex_exit(&ugenp->ug_mutex);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		return (NULL);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	hdl->hdl_minor_node_instance_mask = usb_ugen_info->
2540a05e705Slc 	    usb_ugen_minor_node_instance_mask;
2557c478bd9Sstevel@tonic-gate 	hdl->hdl_minor_node_instance_shift = shift;
2567c478bd9Sstevel@tonic-gate 	hdl->hdl_minor_node_instance_limit = limit;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
2597c478bd9Sstevel@tonic-gate 	    "usb_ugen_get_hdl: instance shift=%d instance limit=%d",
2607c478bd9Sstevel@tonic-gate 	    hdl->hdl_minor_node_instance_shift,
2617c478bd9Sstevel@tonic-gate 	    hdl->hdl_minor_node_instance_limit);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
2647c478bd9Sstevel@tonic-gate 	    "usb_ugen_get_hdl: bits shift=%d bits limit=%d",
2657c478bd9Sstevel@tonic-gate 	    hdl->hdl_minor_node_ugen_bits_shift,
2667c478bd9Sstevel@tonic-gate 	    hdl->hdl_minor_node_ugen_bits_limit);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	mutex_exit(&ugenp->ug_mutex);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	return ((usb_ugen_hdl_t)hdl);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * usb_ugen_release_hdl:
2767c478bd9Sstevel@tonic-gate  *	deallocate a handle
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate void
usb_ugen_release_hdl(usb_ugen_hdl_t usb_ugen_hdl)2797c478bd9Sstevel@tonic-gate usb_ugen_release_hdl(usb_ugen_hdl_t usb_ugen_hdl)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
2820a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (usb_ugen_hdl_impl) {
2857c478bd9Sstevel@tonic-gate 		ugen_state_t *ugenp = usb_ugen_hdl_impl->hdl_ugenp;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		if (ugenp) {
2887c478bd9Sstevel@tonic-gate 			mutex_destroy(&ugenp->ug_mutex);
2897c478bd9Sstevel@tonic-gate 			usb_free_log_hdl(ugenp->ug_log_hdl);
2907c478bd9Sstevel@tonic-gate 			usb_free_dev_data(usb_ugen_hdl_impl->hdl_dip,
2910a05e705Slc 			    ugenp->ug_dev_data);
2927c478bd9Sstevel@tonic-gate 			kmem_free(ugenp, sizeof (*ugenp));
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		if (usb_ugen_hdl_impl->hdl_log_name) {
2957c478bd9Sstevel@tonic-gate 			kmem_free(usb_ugen_hdl_impl->hdl_log_name,
2960a05e705Slc 			    usb_ugen_hdl_impl->hdl_log_name_length);
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		kmem_free(usb_ugen_hdl_impl, sizeof (*usb_ugen_hdl_impl));
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * usb_ugen_attach()
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate int
usb_ugen_attach(usb_ugen_hdl_t usb_ugen_hdl,ddi_attach_cmd_t cmd)3077c478bd9Sstevel@tonic-gate usb_ugen_attach(usb_ugen_hdl_t usb_ugen_hdl, ddi_attach_cmd_t cmd)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
3100a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
3117c478bd9Sstevel@tonic-gate 	ugen_state_t		*ugenp;
3127c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if (usb_ugen_hdl == NULL) {
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	ugenp = usb_ugen_hdl_impl->hdl_ugenp;
3207c478bd9Sstevel@tonic-gate 	dip = usb_ugen_hdl_impl->hdl_dip;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
3247c478bd9Sstevel@tonic-gate 	    "usb_ugen_attach: cmd=%d", cmd);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	switch (cmd) {
3277c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		break;
3307c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
3317c478bd9Sstevel@tonic-gate 		ugen_cpr_resume(ugenp);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		return (USB_SUCCESS);
3347c478bd9Sstevel@tonic-gate 	default:
3357c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(UGEN_PRINT_ATTA, NULL,
3367c478bd9Sstevel@tonic-gate 		    "usb_ugen_attach: unknown command");
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
3427c478bd9Sstevel@tonic-gate 	ugenp->ug_ser_cookie =
3437c478bd9Sstevel@tonic-gate 	    usb_init_serialization(dip, USB_INIT_SER_CHECK_SAME_THREAD);
3447c478bd9Sstevel@tonic-gate 	ugenp->ug_cleanup_flags |= UGEN_INIT_LOCKS;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/* Get maximum bulk transfer size supported by the HCD */
3477c478bd9Sstevel@tonic-gate 	if (usb_pipe_get_max_bulk_transfer_size(dip,
3487c478bd9Sstevel@tonic-gate 	    &ugenp->ug_max_bulk_xfer_sz) != USB_SUCCESS) {
3497c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
3507c478bd9Sstevel@tonic-gate 		    "usb_ugen_attach: Getting max bulk xfer sz failed");
3517c478bd9Sstevel@tonic-gate 		mutex_exit(&ugenp->ug_mutex);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		goto fail;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* table for mapping 48 bit minor codes to 9 bit index (for ugen) */
3577c478bd9Sstevel@tonic-gate 	ugen_minor_node_table_create(ugenp);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/* prepare device status node handling */
3607c478bd9Sstevel@tonic-gate 	if (ugen_ds_init(ugenp) != USB_SUCCESS) {
3617c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
3627c478bd9Sstevel@tonic-gate 		    "usb_ugen_attach: preparing dev status failed");
3637c478bd9Sstevel@tonic-gate 		mutex_exit(&ugenp->ug_mutex);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		goto fail;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/* prepare all available xfer and status endpoints nodes */
3697c478bd9Sstevel@tonic-gate 	if (ugen_epxs_init(ugenp) != USB_SUCCESS) {
3707c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
3717c478bd9Sstevel@tonic-gate 		    "usb_ugen_attach: preparing endpoints failed");
3727c478bd9Sstevel@tonic-gate 		mutex_exit(&ugenp->ug_mutex);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		goto fail;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/* reduce table size if not all entries are used */
3787c478bd9Sstevel@tonic-gate 	ugen_minor_node_table_shrink(ugenp);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/* we are ready to go */
3817c478bd9Sstevel@tonic-gate 	ugenp->ug_dev_state = USB_DEV_ONLINE;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	mutex_exit(&ugenp->ug_mutex);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/* prepare PM */
3867c478bd9Sstevel@tonic-gate 	if (ugenp->ug_hdl->hdl_flags & USB_UGEN_ENABLE_PM) {
3877c478bd9Sstevel@tonic-gate 		ugen_pm_init(ugenp);
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	/*
3917c478bd9Sstevel@tonic-gate 	 * if ugen driver, kill all child nodes otherwise set cfg fails
3927c478bd9Sstevel@tonic-gate 	 * if requested
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	if (usb_owns_device(dip) &&
3957c478bd9Sstevel@tonic-gate 	    (usb_ugen_hdl_impl->hdl_flags & USB_UGEN_REMOVE_CHILDREN)) {
3967c478bd9Sstevel@tonic-gate 		dev_info_t *cdip;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		/* save cfgidx so we can restore on detach */
3997c478bd9Sstevel@tonic-gate 		mutex_enter(&ugenp->ug_mutex);
4007c478bd9Sstevel@tonic-gate 		ugenp->ug_initial_cfgidx = usb_get_current_cfgidx(dip);
4017c478bd9Sstevel@tonic-gate 		mutex_exit(&ugenp->ug_mutex);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		for (cdip = ddi_get_child(dip); cdip; ) {
4047c478bd9Sstevel@tonic-gate 			dev_info_t *next = ddi_get_next_sibling(cdip);
4057c478bd9Sstevel@tonic-gate 			(void) ddi_remove_child(cdip, 0);
4067c478bd9Sstevel@tonic-gate 			cdip = next;
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4117c478bd9Sstevel@tonic-gate fail:
412*0d2006e4SRobert Mustacchi 	USB_DPRINTF_L2(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
413*0d2006e4SRobert Mustacchi 	    "attach fail");
414*0d2006e4SRobert Mustacchi 	(void) ugen_cleanup(ugenp);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * usb_ugen_detach()
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate int
usb_ugen_detach(usb_ugen_hdl_t usb_ugen_hdl,ddi_detach_cmd_t cmd)4247c478bd9Sstevel@tonic-gate usb_ugen_detach(usb_ugen_hdl_t usb_ugen_hdl, ddi_detach_cmd_t cmd)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
4270a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
4287c478bd9Sstevel@tonic-gate 	int			rval = USB_FAILURE;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (usb_ugen_hdl) {
4317c478bd9Sstevel@tonic-gate 		ugen_state_t *ugenp = usb_ugen_hdl_impl->hdl_ugenp;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(UGEN_PRINT_ATTA, ugenp->ug_log_hdl,
4347c478bd9Sstevel@tonic-gate 		    "usb_ugen_detach cmd %d", cmd);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		switch (cmd) {
4377c478bd9Sstevel@tonic-gate 		case DDI_DETACH:
4387c478bd9Sstevel@tonic-gate 			rval = ugen_cleanup(ugenp);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 			break;
4417c478bd9Sstevel@tonic-gate 		case DDI_SUSPEND:
4427c478bd9Sstevel@tonic-gate 			rval = ugen_cpr_suspend(ugenp);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 			break;
4457c478bd9Sstevel@tonic-gate 		default:
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 			break;
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	return (rval);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate  * ugen_cleanup()
4577c478bd9Sstevel@tonic-gate  */
4587c478bd9Sstevel@tonic-gate static int
ugen_cleanup(ugen_state_t * ugenp)4597c478bd9Sstevel@tonic-gate ugen_cleanup(ugen_state_t *ugenp)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	dev_info_t *dip = ugenp->ug_dip;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_ATTA, ugenp->ug_log_hdl, "ugen_cleanup");
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (ugenp->ug_cleanup_flags & UGEN_INIT_LOCKS) {
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		/* shutdown all endpoints */
4687c478bd9Sstevel@tonic-gate 		ugen_epx_shutdown(ugenp);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		/*
4717c478bd9Sstevel@tonic-gate 		 * At this point, no new activity can be initiated.
4727c478bd9Sstevel@tonic-gate 		 * The driver has disabled hotplug callbacks.
4737c478bd9Sstevel@tonic-gate 		 * The Solaris framework has disabled
4747c478bd9Sstevel@tonic-gate 		 * new opens on a device being detached, and does not
4757c478bd9Sstevel@tonic-gate 		 * allow detaching an open device. PM should power
4767c478bd9Sstevel@tonic-gate 		 * down while we are detaching
4777c478bd9Sstevel@tonic-gate 		 *
4787c478bd9Sstevel@tonic-gate 		 * The following ensures that any other driver
4797c478bd9Sstevel@tonic-gate 		 * activity must have drained (paranoia)
4807c478bd9Sstevel@tonic-gate 		 */
4817c478bd9Sstevel@tonic-gate 		(void) usb_serialize_access(ugenp->ug_ser_cookie,
4820a05e705Slc 		    USB_WAIT, 0);
4837c478bd9Sstevel@tonic-gate 		usb_release_access(ugenp->ug_ser_cookie);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		mutex_enter(&ugenp->ug_mutex);
4867c478bd9Sstevel@tonic-gate 		ASSERT(ugenp->ug_open_count == 0);
4877c478bd9Sstevel@tonic-gate 		ASSERT(ugenp->ug_pending_cmds == 0);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		/* dismantle in reverse order */
4907c478bd9Sstevel@tonic-gate 		ugen_pm_destroy(ugenp);
4917c478bd9Sstevel@tonic-gate 		ugen_epxs_destroy(ugenp);
4927c478bd9Sstevel@tonic-gate 		ugen_ds_destroy(ugenp);
4937c478bd9Sstevel@tonic-gate 		ugen_minor_node_table_destroy(ugenp);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		/* restore to initial configuration */
4977c478bd9Sstevel@tonic-gate 		if (usb_owns_device(dip) &&
4987c478bd9Sstevel@tonic-gate 		    (ugenp->ug_dev_state != USB_DEV_DISCONNECTED)) {
4997c478bd9Sstevel@tonic-gate 			int idx = ugenp->ug_initial_cfgidx;
5007c478bd9Sstevel@tonic-gate 			mutex_exit(&ugenp->ug_mutex);
5017c478bd9Sstevel@tonic-gate 			(void) usb_set_cfg(dip, idx,
5027c478bd9Sstevel@tonic-gate 			    USB_FLAGS_SLEEP, NULL, NULL);
5037c478bd9Sstevel@tonic-gate 		} else {
5047c478bd9Sstevel@tonic-gate 			mutex_exit(&ugenp->ug_mutex);
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		usb_fini_serialization(ugenp->ug_ser_cookie);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all(dip);
5117c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	ugen_free_devt(ugenp);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate  * ugen_cpr_suspend
5217c478bd9Sstevel@tonic-gate  */
5227c478bd9Sstevel@tonic-gate static int
ugen_cpr_suspend(ugen_state_t * ugenp)5237c478bd9Sstevel@tonic-gate ugen_cpr_suspend(ugen_state_t *ugenp)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	int		rval = USB_FAILURE;
5267c478bd9Sstevel@tonic-gate 	int		i;
5277c478bd9Sstevel@tonic-gate 	int		prev_state;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_CPR, ugenp->ug_log_hdl,
5307c478bd9Sstevel@tonic-gate 	    "ugen_cpr_suspend:");
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
5337c478bd9Sstevel@tonic-gate 	switch (ugenp->ug_dev_state) {
5347c478bd9Sstevel@tonic-gate 	case USB_DEV_ONLINE:
5357c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
5367c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(UGEN_PRINT_CPR, ugenp->ug_log_hdl,
5377c478bd9Sstevel@tonic-gate 		    "ugen_cpr_suspend:");
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		prev_state = ugenp->ug_dev_state;
5407c478bd9Sstevel@tonic-gate 		ugenp->ug_dev_state = USB_DEV_SUSPENDED;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		if (ugenp->ug_open_count) {
5437c478bd9Sstevel@tonic-gate 			/* drain outstanding cmds */
5447c478bd9Sstevel@tonic-gate 			for (i = 0; i < ugen_busy_loop; i++) {
5457c478bd9Sstevel@tonic-gate 				if (ugenp->ug_pending_cmds == 0) {
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 					break;
5487c478bd9Sstevel@tonic-gate 				}
5497c478bd9Sstevel@tonic-gate 				mutex_exit(&ugenp->ug_mutex);
5507c478bd9Sstevel@tonic-gate 				delay(drv_usectohz(100000));
5517c478bd9Sstevel@tonic-gate 				mutex_enter(&ugenp->ug_mutex);
5527c478bd9Sstevel@tonic-gate 			}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 			/* if still outstanding cmds, fail suspend */
5557c478bd9Sstevel@tonic-gate 			if (ugenp->ug_pending_cmds) {
5567c478bd9Sstevel@tonic-gate 				ugenp->ug_dev_state = prev_state;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L2(UGEN_PRINT_CPR,
5597c478bd9Sstevel@tonic-gate 				    ugenp->ug_log_hdl,
5607c478bd9Sstevel@tonic-gate 				    "ugen_cpr_suspend: pending %d",
5617c478bd9Sstevel@tonic-gate 				    ugenp->ug_pending_cmds);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 				rval =	USB_FAILURE;
5647c478bd9Sstevel@tonic-gate 				break;
5657c478bd9Sstevel@tonic-gate 			}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 			mutex_exit(&ugenp->ug_mutex);
5687c478bd9Sstevel@tonic-gate 			(void) usb_serialize_access(ugenp->ug_ser_cookie,
5690a05e705Slc 			    USB_WAIT, 0);
5707c478bd9Sstevel@tonic-gate 			/* close all pipes */
5717c478bd9Sstevel@tonic-gate 			ugen_epx_shutdown(ugenp);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 			usb_release_access(ugenp->ug_ser_cookie);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 			mutex_enter(&ugenp->ug_mutex);
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		/* wakeup devstat reads and polls */
5797c478bd9Sstevel@tonic-gate 		ugen_ds_change(ugenp);
5807c478bd9Sstevel@tonic-gate 		ugen_ds_poll_wakeup(ugenp);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		rval = USB_SUCCESS;
5837c478bd9Sstevel@tonic-gate 		break;
5847c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
5857c478bd9Sstevel@tonic-gate 	case USB_UGEN_DEV_UNAVAILABLE_RESUME:
5867c478bd9Sstevel@tonic-gate 	case USB_UGEN_DEV_UNAVAILABLE_RECONNECT:
5877c478bd9Sstevel@tonic-gate 	default:
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		break;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	mutex_exit(&ugenp->ug_mutex);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	return (rval);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate  * ugen_cpr_resume
5987c478bd9Sstevel@tonic-gate  */
5997c478bd9Sstevel@tonic-gate static void
ugen_cpr_resume(ugen_state_t * ugenp)6007c478bd9Sstevel@tonic-gate ugen_cpr_resume(ugen_state_t *ugenp)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_CPR, ugenp->ug_log_hdl,
6037c478bd9Sstevel@tonic-gate 	    "ugen_cpr_resume:");
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	ugen_restore_state(ugenp);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  * usb_ugen_disconnect_ev_cb:
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate int
usb_ugen_disconnect_ev_cb(usb_ugen_hdl_t usb_ugen_hdl)6127c478bd9Sstevel@tonic-gate usb_ugen_disconnect_ev_cb(usb_ugen_hdl_t usb_ugen_hdl)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
6150a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
6167c478bd9Sstevel@tonic-gate 	ugen_state_t		*ugenp;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (usb_ugen_hdl_impl == NULL) {
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		return (USB_FAILURE);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	ugenp = usb_ugen_hdl_impl->hdl_ugenp;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_HOTPLUG, ugenp->ug_log_hdl,
6267c478bd9Sstevel@tonic-gate 	    "usb_ugen_disconnect_ev_cb:");
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/* get exclusive access */
6297c478bd9Sstevel@tonic-gate 	(void) usb_serialize_access(ugenp->ug_ser_cookie, USB_WAIT, 0);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
6327c478bd9Sstevel@tonic-gate 	ugenp->ug_dev_state = USB_DEV_DISCONNECTED;
6337c478bd9Sstevel@tonic-gate 	if (ugenp->ug_open_count) {
6347c478bd9Sstevel@tonic-gate 		mutex_exit(&ugenp->ug_mutex);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		/* close all pipes */
6377c478bd9Sstevel@tonic-gate 		(void) ugen_epx_shutdown(ugenp);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		mutex_enter(&ugenp->ug_mutex);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/* wakeup devstat reads and polls */
6447c478bd9Sstevel@tonic-gate 	ugen_ds_change(ugenp);
6457c478bd9Sstevel@tonic-gate 	ugen_ds_poll_wakeup(ugenp);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	mutex_exit(&ugenp->ug_mutex);
6487c478bd9Sstevel@tonic-gate 	usb_release_access(ugenp->ug_ser_cookie);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate  * usb_ugen_reconnect_ev_cb:
6567c478bd9Sstevel@tonic-gate  */
6577c478bd9Sstevel@tonic-gate int
usb_ugen_reconnect_ev_cb(usb_ugen_hdl_t usb_ugen_hdl)6587c478bd9Sstevel@tonic-gate usb_ugen_reconnect_ev_cb(usb_ugen_hdl_t usb_ugen_hdl)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
6610a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
6627c478bd9Sstevel@tonic-gate 	ugen_state_t		*ugenp = usb_ugen_hdl_impl->hdl_ugenp;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_HOTPLUG, ugenp->ug_log_hdl,
6657c478bd9Sstevel@tonic-gate 	    "usb_ugen_reconnect_ev_cb:");
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	ugen_restore_state(ugenp);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	return (USB_SUCCESS);
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * ugen_restore_state:
6757c478bd9Sstevel@tonic-gate  *	Check for same device; if a different device is attached, set
6767c478bd9Sstevel@tonic-gate  *	the device status to disconnected.
6777c478bd9Sstevel@tonic-gate  *	If we were open, then set to UNAVAILABLE until all endpoints have
6787c478bd9Sstevel@tonic-gate  *	be closed.
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate static void
ugen_restore_state(ugen_state_t * ugenp)6817c478bd9Sstevel@tonic-gate ugen_restore_state(ugen_state_t *ugenp)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	dev_info_t *dip = ugenp->ug_dip;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_HOTPLUG, ugenp->ug_log_hdl,
6867c478bd9Sstevel@tonic-gate 	    "ugen_restore_state");
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/* first raise power */
6897c478bd9Sstevel@tonic-gate 	if (ugenp->ug_hdl->hdl_flags & USB_UGEN_ENABLE_PM) {
6907c478bd9Sstevel@tonic-gate 		ugen_pm_busy_component(ugenp);
6917c478bd9Sstevel@tonic-gate 		(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/* Check if we are talking to the same device */
6957c478bd9Sstevel@tonic-gate 	if (usb_check_same_device(dip, ugenp->ug_log_hdl,
6967c478bd9Sstevel@tonic-gate 	    USB_LOG_L0, UGEN_PRINT_HOTPLUG, USB_CHK_ALL, NULL) ==
6977c478bd9Sstevel@tonic-gate 	    USB_FAILURE) {
6987c478bd9Sstevel@tonic-gate 		mutex_enter(&ugenp->ug_mutex);
6997c478bd9Sstevel@tonic-gate 		ugenp->ug_dev_state = USB_DEV_DISCONNECTED;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		/* wakeup devstat reads and polls */
7027c478bd9Sstevel@tonic-gate 		ugen_ds_change(ugenp);
7037c478bd9Sstevel@tonic-gate 		ugen_ds_poll_wakeup(ugenp);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		mutex_exit(&ugenp->ug_mutex);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if (ugenp->ug_hdl->hdl_flags & USB_UGEN_ENABLE_PM) {
7087c478bd9Sstevel@tonic-gate 			ugen_pm_idle_component(ugenp);
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		return;
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/*
7157c478bd9Sstevel@tonic-gate 	 * get exclusive access, we don't want to change state in the
7167c478bd9Sstevel@tonic-gate 	 * middle of some other actions
7177c478bd9Sstevel@tonic-gate 	 */
7187c478bd9Sstevel@tonic-gate 	(void) usb_serialize_access(ugenp->ug_ser_cookie, USB_WAIT, 0);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
7217c478bd9Sstevel@tonic-gate 	switch (ugenp->ug_dev_state) {
7227c478bd9Sstevel@tonic-gate 	case USB_DEV_DISCONNECTED:
7237c478bd9Sstevel@tonic-gate 		ugenp->ug_dev_state = (ugenp->ug_open_count == 0) ?
7247c478bd9Sstevel@tonic-gate 		    USB_DEV_ONLINE : USB_UGEN_DEV_UNAVAILABLE_RECONNECT;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		break;
7277c478bd9Sstevel@tonic-gate 	case USB_DEV_SUSPENDED:
7287c478bd9Sstevel@tonic-gate 		ugenp->ug_dev_state = (ugenp->ug_open_count == 0) ?
7297c478bd9Sstevel@tonic-gate 		    USB_DEV_ONLINE : USB_UGEN_DEV_UNAVAILABLE_RESUME;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		break;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_HOTPLUG, ugenp->ug_log_hdl,
7347c478bd9Sstevel@tonic-gate 	    "ugen_restore_state: state=%d, opencount=%d",
7357c478bd9Sstevel@tonic-gate 	    ugenp->ug_dev_state, ugenp->ug_open_count);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/* wakeup devstat reads and polls */
7387c478bd9Sstevel@tonic-gate 	ugen_ds_change(ugenp);
7397c478bd9Sstevel@tonic-gate 	ugen_ds_poll_wakeup(ugenp);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	mutex_exit(&ugenp->ug_mutex);
7427c478bd9Sstevel@tonic-gate 	usb_release_access(ugenp->ug_ser_cookie);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (ugenp->ug_hdl->hdl_flags & USB_UGEN_ENABLE_PM) {
7457c478bd9Sstevel@tonic-gate 		ugen_pm_idle_component(ugenp);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate /*
7517c478bd9Sstevel@tonic-gate  * usb_ugen_open:
7527c478bd9Sstevel@tonic-gate  */
7537c478bd9Sstevel@tonic-gate /* ARGSUSED */
7547c478bd9Sstevel@tonic-gate int
usb_ugen_open(usb_ugen_hdl_t usb_ugen_hdl,dev_t * devp,int flag,int sflag,cred_t * cr)7557c478bd9Sstevel@tonic-gate usb_ugen_open(usb_ugen_hdl_t usb_ugen_hdl, dev_t *devp, int flag, int sflag,
7567c478bd9Sstevel@tonic-gate     cred_t *cr)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
7590a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
7607c478bd9Sstevel@tonic-gate 	ugen_state_t		*ugenp;
7617c478bd9Sstevel@tonic-gate 	int			rval;
7627c478bd9Sstevel@tonic-gate 	int			minor_node_type;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	if (usb_ugen_hdl == NULL) {
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		return (EINVAL);
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	ugenp = usb_ugen_hdl_impl->hdl_ugenp;
77096603521Sfrits 
77196603521Sfrits 	if (ugen_is_valid_minor_node(ugenp, *devp) != USB_SUCCESS) {
77296603521Sfrits 
77396603521Sfrits 		return (EINVAL);
77496603521Sfrits 	}
77596603521Sfrits 
7767c478bd9Sstevel@tonic-gate 	minor_node_type = UGEN_MINOR_TYPE(ugenp, *devp);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_CBOPS, ugenp->ug_log_hdl,
7797c478bd9Sstevel@tonic-gate 	    "usb_ugen_open: minor=%u", getminor(*devp));
7807c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(UGEN_PRINT_CBOPS, ugenp->ug_log_hdl,
7817c478bd9Sstevel@tonic-gate 	    "cfgval=%" PRIu64 " cfgidx=%" PRIu64 " if=%" PRIu64
7827c478bd9Sstevel@tonic-gate 	    " alt=%" PRIu64 " epidx=%" PRIu64 " type=0x%" PRIx64,
7837c478bd9Sstevel@tonic-gate 	    UGEN_MINOR_CFGVAL(ugenp, *devp), UGEN_MINOR_CFGIDX(ugenp, *devp),
7847c478bd9Sstevel@tonic-gate 	    UGEN_MINOR_IF(ugenp, *devp), UGEN_MINOR_ALT(ugenp, *devp),
7857c478bd9Sstevel@tonic-gate 	    UGEN_MINOR_EPIDX(ugenp, *devp), UGEN_MINOR_TYPE(ugenp, *devp));
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/* first check for legal open flags */
7887c478bd9Sstevel@tonic-gate 	if ((rval = ugen_check_open_flags(ugenp, *devp, flag)) != 0) {
7897c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(UGEN_PRINT_CBOPS, ugenp->ug_log_hdl,
7907c478bd9Sstevel@tonic-gate 		    "usb_ugen_open: check failed, rval=%d", rval);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		return (rval);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	/* exclude other threads including other opens */
7967c478bd9Sstevel@tonic-gate 	if (usb_serialize_access(ugenp->ug_ser_cookie,
7977c478bd9Sstevel@tonic-gate 	    USB_WAIT_SIG, 0) <= 0) {
7987c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(UGEN_PRINT_CBOPS, ugenp->ug_log_hdl,
7997c478bd9Sstevel@tonic-gate 		    "usb_ugen_open: interrupted");
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		return (EINTR);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* always allow open of dev stat node */
8077c478bd9Sstevel@tonic-gate 	if (minor_node_type != UGEN_MINOR_DEV_STAT_NODE) {
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		/* if we are not online or powered down, fail open */
8107c478bd9Sstevel@tonic-gate 		switch (ugenp->ug_dev_state) {
8117c478bd9Sstevel@tonic-gate 		case USB_DEV_ONLINE:
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 			break;
8147c478bd9Sstevel@tonic-gate 		case USB_DEV_DISCONNECTED:
8157c478bd9Sstevel@tonic-gate 			rval = ENODEV;
8167c478bd9Sstevel@tonic-gate 			mutex_exit(&ugenp->ug_mutex);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 			goto done;
8197c478bd9Sstevel@tonic-gate 		case USB_DEV_SUSPENDED:
8207c478bd9Sstevel@tonic-gate 		case USB_UGEN_DEV_UNAVAILABLE_RESUME:
8217c478bd9Sstevel@tonic-gate 		case USB_UGEN_DEV_UNAVAILABLE_RECONNECT:
8227c478bd9Sstevel@tonic-gate 		default:
8237c478bd9Sstevel@tonic-gate 			rval = EBADF;
8247c478bd9Sstevel@tonic-gate 			mutex_exit(&ugenp->ug_mutex);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 			goto done;
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	mutex_exit(&ugenp->ug_mutex);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/* open node depending on type */
8327c478bd9Sstevel@tonic-gate 	switch (minor_node_type) {
8337c478bd9Sstevel@tonic-gate 	case UGEN_MINOR_EP_XFER_NODE:
8347c478bd9Sstevel@tonic-gate 		if (ugenp->ug_hdl->hdl_flags & USB_UGEN_ENABLE_PM) {
8357c478bd9Sstevel@tonic-gate 			ugen_pm_busy_component(ugenp);
8367c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(ugenp->ug_dip, 0,
8370a05e705Slc 			    USB_DEV_OS_FULL_PWR);
8387c478bd9Sstevel@tonic-gate 		}
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		rval = ugen_epx_open(ugenp, *devp, flag);
8417c478bd9Sstevel@tonic-gate 		if (rval == 0) {
8427c478bd9Sstevel@tonic-gate 			mutex_enter(&ugenp->ug_mutex);
8437c478bd9Sstevel@tonic-gate 			ugenp->ug_open_count++;
8447c478bd9Sstevel@tonic-gate 			mutex_exit(&ugenp->ug_mutex);
8457c478bd9Sstevel@tonic-gate 		} else {
8467c478bd9Sstevel@tonic-gate 			if (ugenp->ug_hdl->hdl_flags &
8477c478bd9Sstevel@tonic-gate 			    USB_UGEN_ENABLE_PM) {
8487c478bd9Sstevel@tonic-gate 				ugen_pm_idle_component(ugenp);
8497c478bd9Sstevel@tonic-gate 			}
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 		break;
8537c478bd9Sstevel@tonic-gate 	case UGEN_MINOR_EP_STAT_NODE:
8547c478bd9Sstevel@tonic-gate 		rval = ugen_eps_open(ugenp, *devp, flag);
8557c478bd9Sstevel@tonic-gate 		if (rval == 0) {
8567c478bd9Sstevel@tonic-gate 			mutex_enter(&ugenp->ug_mutex);
8577c478bd9Sstevel@tonic-gate 			ugenp->ug_open_count++;
8587c478bd9Sstevel@tonic-gate 			mutex_exit(&ugenp->ug_mutex);
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		break;
8627c478bd9Sstevel@tonic-gate 	case UGEN_MINOR_DEV_STAT_NODE:
8637c478bd9Sstevel@tonic-gate 		rval = ugen_ds_open(ugenp, *devp, flag);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		break;
8667c478bd9Sstevel@tonic-gate 	default:
8677c478bd9Sstevel@tonic-gate 		rval = EINVAL;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		break;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate done:
8727c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_CBOPS, ugenp->ug_log_hdl,
8757c478bd9Sstevel@tonic-gate 	    "usb_ugen_open: minor=0x%x rval=%d state=%d cnt=%d",
8767c478bd9Sstevel@tonic-gate 	    getminor(*devp), rval, ugenp->ug_dev_state,
8777c478bd9Sstevel@tonic-gate 	    ugenp->ug_open_count);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	mutex_exit(&ugenp->ug_mutex);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	usb_release_access(ugenp->ug_ser_cookie);
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	return (rval);
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate /*
8887c478bd9Sstevel@tonic-gate  * usb_ugen_close()
8897c478bd9Sstevel@tonic-gate  */
8907c478bd9Sstevel@tonic-gate /* ARGSUSED */
8917c478bd9Sstevel@tonic-gate int
usb_ugen_close(usb_ugen_hdl_t usb_ugen_hdl,dev_t dev,int flag,int otype,cred_t * cr)8927c478bd9Sstevel@tonic-gate usb_ugen_close(usb_ugen_hdl_t usb_ugen_hdl, dev_t dev, int flag, int otype,
8937c478bd9Sstevel@tonic-gate     cred_t *cr)
8947c478bd9Sstevel@tonic-gate {
8957c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
8960a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
8977c478bd9Sstevel@tonic-gate 	ugen_state_t		*ugenp;
8987c478bd9Sstevel@tonic-gate 	int			minor_node_type;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	if (usb_ugen_hdl == NULL) {
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		return (EINVAL);
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	ugenp = usb_ugen_hdl_impl->hdl_ugenp;
90696603521Sfrits 	if (ugen_is_valid_minor_node(ugenp, dev) != USB_SUCCESS) {
90796603521Sfrits 
90896603521Sfrits 		return (EINVAL);
90996603521Sfrits 	}
91096603521Sfrits 
9117c478bd9Sstevel@tonic-gate 	minor_node_type = UGEN_MINOR_TYPE(ugenp, dev);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_CBOPS, ugenp->ug_log_hdl,
9147c478bd9Sstevel@tonic-gate 	    "usb_ugen_close: minor=0x%x", getminor(dev));
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	/* exclude other threads, including other opens */
9177c478bd9Sstevel@tonic-gate 	if (usb_serialize_access(ugenp->ug_ser_cookie,
9187c478bd9Sstevel@tonic-gate 	    USB_WAIT_SIG, 0) <= 0) {
9197c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(UGEN_PRINT_CBOPS, ugenp->ug_log_hdl,
9207c478bd9Sstevel@tonic-gate 		    "usb_ugen_close: interrupted");
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		return (EINTR);
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/* close node depending on type */
9267c478bd9Sstevel@tonic-gate 	switch (minor_node_type) {
9277c478bd9Sstevel@tonic-gate 	case UGEN_MINOR_EP_XFER_NODE:
9287c478bd9Sstevel@tonic-gate 		ugen_epx_close(ugenp, dev, flag);
9297c478bd9Sstevel@tonic-gate 		if (ugenp->ug_hdl->hdl_flags & USB_UGEN_ENABLE_PM) {
9307c478bd9Sstevel@tonic-gate 			ugen_pm_idle_component(ugenp);
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		break;
9347c478bd9Sstevel@tonic-gate 	case UGEN_MINOR_EP_STAT_NODE:
9357c478bd9Sstevel@tonic-gate 		ugen_eps_close(ugenp, dev, flag);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		break;
9387c478bd9Sstevel@tonic-gate 	case UGEN_MINOR_DEV_STAT_NODE:
9397c478bd9Sstevel@tonic-gate 		ugen_ds_close(ugenp, dev, flag);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		break;
9427c478bd9Sstevel@tonic-gate 	default:
9437c478bd9Sstevel@tonic-gate 		usb_release_access(ugenp->ug_ser_cookie);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		return (EINVAL);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
9497c478bd9Sstevel@tonic-gate 	if (minor_node_type != UGEN_MINOR_DEV_STAT_NODE) {
9507c478bd9Sstevel@tonic-gate 		ASSERT(ugenp->ug_open_count > 0);
9517c478bd9Sstevel@tonic-gate 		if ((--ugenp->ug_open_count == 0) &&
9527c478bd9Sstevel@tonic-gate 		    ((ugenp->ug_dev_state == USB_UGEN_DEV_UNAVAILABLE_RESUME) ||
9537c478bd9Sstevel@tonic-gate 		    (ugenp->ug_dev_state ==
9547c478bd9Sstevel@tonic-gate 		    USB_UGEN_DEV_UNAVAILABLE_RECONNECT))) {
9557c478bd9Sstevel@tonic-gate 			ugenp->ug_dev_state = USB_DEV_ONLINE;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 			/* wakeup devstat reads and polls */
9587c478bd9Sstevel@tonic-gate 			ugen_ds_change(ugenp);
9597c478bd9Sstevel@tonic-gate 			ugen_ds_poll_wakeup(ugenp);
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_CBOPS, ugenp->ug_log_hdl,
9647c478bd9Sstevel@tonic-gate 	    "usb_ugen_close: minor=0x%x state=%d cnt=%d",
9657c478bd9Sstevel@tonic-gate 	    getminor(dev), ugenp->ug_dev_state, ugenp->ug_open_count);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	if (ugenp->ug_open_count == 0) {
9687c478bd9Sstevel@tonic-gate 		ASSERT(ugen_epxs_check_open_nodes(ugenp) == USB_FAILURE);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	mutex_exit(&ugenp->ug_mutex);
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	usb_release_access(ugenp->ug_ser_cookie);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	return (0);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate /*
9807c478bd9Sstevel@tonic-gate  * usb_ugen_read/write()
9817c478bd9Sstevel@tonic-gate  */
9827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9837c478bd9Sstevel@tonic-gate int
usb_ugen_read(usb_ugen_hdl_t usb_ugen_hdl,dev_t dev,struct uio * uiop,cred_t * credp)9847c478bd9Sstevel@tonic-gate usb_ugen_read(usb_ugen_hdl_t usb_ugen_hdl, dev_t dev, struct uio *uiop,
9857c478bd9Sstevel@tonic-gate     cred_t *credp)
9867c478bd9Sstevel@tonic-gate {
98796603521Sfrits 	ugen_state_t		*ugenp;
98896603521Sfrits 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
9890a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
99096603521Sfrits 
99196603521Sfrits 	if (usb_ugen_hdl == NULL) {
99296603521Sfrits 
99396603521Sfrits 		return (EINVAL);
99496603521Sfrits 	}
99596603521Sfrits 	ugenp = usb_ugen_hdl_impl->hdl_ugenp;
99696603521Sfrits 
99796603521Sfrits 	if (ugen_is_valid_minor_node(ugenp, dev) != USB_SUCCESS) {
99896603521Sfrits 
99996603521Sfrits 		return (EINVAL);
100096603521Sfrits 	}
100196603521Sfrits 
10027c478bd9Sstevel@tonic-gate 	return (physio(ugen_strategy,
10037c478bd9Sstevel@tonic-gate 	    (struct buf *)0, dev, B_READ, ugen_minphys, uiop));
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10087c478bd9Sstevel@tonic-gate int
usb_ugen_write(usb_ugen_hdl_t usb_ugen_hdl,dev_t dev,struct uio * uiop,cred_t * credp)10097c478bd9Sstevel@tonic-gate usb_ugen_write(usb_ugen_hdl_t usb_ugen_hdl, dev_t dev, struct uio *uiop,
10107c478bd9Sstevel@tonic-gate     cred_t *credp)
10117c478bd9Sstevel@tonic-gate {
101296603521Sfrits 	ugen_state_t		*ugenp;
101396603521Sfrits 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
10140a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
101596603521Sfrits 
101696603521Sfrits 	if (usb_ugen_hdl == NULL) {
101796603521Sfrits 
101896603521Sfrits 		return (EINVAL);
101996603521Sfrits 	}
102096603521Sfrits 	ugenp = usb_ugen_hdl_impl->hdl_ugenp;
102196603521Sfrits 
102296603521Sfrits 	if (ugen_is_valid_minor_node(ugenp, dev) != USB_SUCCESS) {
102396603521Sfrits 
102496603521Sfrits 		return (EINVAL);
102596603521Sfrits 	}
102696603521Sfrits 
10277c478bd9Sstevel@tonic-gate 	return (physio(ugen_strategy,
10287c478bd9Sstevel@tonic-gate 	    (struct buf *)0, dev, B_WRITE, ugen_minphys, uiop));
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate /*
10337c478bd9Sstevel@tonic-gate  * usb_ugen_poll
10347c478bd9Sstevel@tonic-gate  */
10357c478bd9Sstevel@tonic-gate int
usb_ugen_poll(usb_ugen_hdl_t usb_ugen_hdl,dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)10367c478bd9Sstevel@tonic-gate usb_ugen_poll(usb_ugen_hdl_t usb_ugen_hdl, dev_t dev, short events,
10377c478bd9Sstevel@tonic-gate     int anyyet,  short *reventsp, struct pollhead **phpp)
10387c478bd9Sstevel@tonic-gate {
10397c478bd9Sstevel@tonic-gate 	usb_ugen_hdl_impl_t	*usb_ugen_hdl_impl =
10400a05e705Slc 	    (usb_ugen_hdl_impl_t *)usb_ugen_hdl;
10417c478bd9Sstevel@tonic-gate 	ugen_state_t		*ugenp;
10427c478bd9Sstevel@tonic-gate 	int			minor_node_type;
10437c478bd9Sstevel@tonic-gate 	uint_t			ep_index;
10447c478bd9Sstevel@tonic-gate 	ugen_ep_t		*epp;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	if (usb_ugen_hdl == NULL) {
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 		return (EINVAL);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	ugenp = usb_ugen_hdl_impl->hdl_ugenp;
105296603521Sfrits 	if (ugen_is_valid_minor_node(ugenp, dev) != USB_SUCCESS) {
105396603521Sfrits 
105496603521Sfrits 		return (EINVAL);
105596603521Sfrits 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	minor_node_type = UGEN_MINOR_TYPE(ugenp, dev);
10587c478bd9Sstevel@tonic-gate 	ep_index	= UGEN_MINOR_EPIDX(ugenp, dev);
10597c478bd9Sstevel@tonic-gate 	epp		= &ugenp->ug_ep[ep_index];
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	mutex_enter(&ugenp->ug_mutex);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(UGEN_PRINT_POLL, ugenp->ug_log_hdl,
10647c478bd9Sstevel@tonic-gate 	    "usb_ugen_poll: "
10657c478bd9Sstevel@tonic-gate 	    "dev=0x%lx events=0x%x anyyet=0x%x rev=0x%p type=%d "
10667c478bd9Sstevel@tonic-gate 	    "devstat=0x%x devstate=0x%x",
10677c478bd9Sstevel@tonic-gate 	    dev, events, anyyet, (void *)reventsp, minor_node_type,
10687c478bd9Sstevel@tonic-gate 	    ugenp->ug_ds.dev_stat, ugenp->ug_ds.dev_state);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	*reventsp = 0;
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	if (ugenp->ug_dev_state == USB_DEV_ONLINE) {
10737c478bd9Sstevel@tonic-gate 		switch (minor_node_type) {
10747c478bd9Sstevel@tonic-gate 		case UGEN_MINOR_EP_XFER_NODE:
10757c478bd9Sstevel@tonic-gate 			/* if interrupt IN ep and there is data, set POLLIN */
10767c478bd9Sstevel@tonic-gate 			if ((UGEN_XFER_TYPE(epp) == USB_EP_ATTR_INTR) &&
10777c478bd9Sstevel@tonic-gate 			    (UGEN_XFER_DIR(epp) & USB_EP_DIR_IN)) {
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 				/*
10807c478bd9Sstevel@tonic-gate 				 * if we are not polling, force another
10817c478bd9Sstevel@tonic-gate 				 * read to kick off polling
10827c478bd9Sstevel@tonic-gate 				 */
10837c478bd9Sstevel@tonic-gate 				mutex_enter(&epp->ep_mutex);
10847c478bd9Sstevel@tonic-gate 				if ((epp->ep_data) ||
10857c478bd9Sstevel@tonic-gate 				    ((epp->ep_state &
10867c478bd9Sstevel@tonic-gate 				    UGEN_EP_STATE_INTR_IN_POLLING_ON) == 0)) {
10877c478bd9Sstevel@tonic-gate 					*reventsp |= POLLIN;
1088a5eb7107SBryan Cantrill 				}
1089a5eb7107SBryan Cantrill 
1090a5eb7107SBryan Cantrill 				if ((!*reventsp && !anyyet) ||
1091a5eb7107SBryan Cantrill 				    (events & POLLET)) {
10927c478bd9Sstevel@tonic-gate 					*phpp = &epp->ep_pollhead;
10937c478bd9Sstevel@tonic-gate 					epp->ep_state |=
10947c478bd9Sstevel@tonic-gate 					    UGEN_EP_STATE_INTR_IN_POLL_PENDING;
10957c478bd9Sstevel@tonic-gate 				}
10967c478bd9Sstevel@tonic-gate 				mutex_exit(&epp->ep_mutex);
10970a05e705Slc 
10980a05e705Slc 			} else if ((UGEN_XFER_TYPE(epp) == USB_EP_ATTR_ISOCH) &&
10990a05e705Slc 			    (UGEN_XFER_DIR(epp) & USB_EP_DIR_IN)) {
11000a05e705Slc 
11010a05e705Slc 				/*
11020a05e705Slc 				 * if we are not polling, force another
11030a05e705Slc 				 * read to kick off polling
11040a05e705Slc 				 */
11050a05e705Slc 				mutex_enter(&epp->ep_mutex);
11060a05e705Slc 				if ((epp->ep_data) ||
11070a05e705Slc 				    ((epp->ep_state &
11080a05e705Slc 				    UGEN_EP_STATE_ISOC_IN_POLLING_ON) == 0)) {
11090a05e705Slc 					*reventsp |= POLLIN;
1110a5eb7107SBryan Cantrill 				}
1111a5eb7107SBryan Cantrill 
1112a5eb7107SBryan Cantrill 				if ((!*reventsp && !anyyet) ||
1113a5eb7107SBryan Cantrill 				    (events & POLLET)) {
11140a05e705Slc 					*phpp = &epp->ep_pollhead;
11150a05e705Slc 					epp->ep_state |=
1116