Home | History | Annotate | Line # | Download | only in usb
uslsa.c revision 1.32.10.1
      1  1.32.10.1  perseant /* $NetBSD: uslsa.c,v 1.32.10.1 2025/08/02 05:57:07 perseant Exp $ */
      2        1.9  jakllsch 
      3        1.9  jakllsch /* from ugensa.c */
      4        1.2   xtraeme 
      5        1.1    dogcow /*
      6        1.9  jakllsch  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
      7        1.1    dogcow  * All rights reserved.
      8        1.1    dogcow  *
      9        1.1    dogcow  * This code is derived from software contributed to The NetBSD Foundation
     10        1.9  jakllsch  * by Roland C. Dowdeswell <elric (at) netbsd.org>.
     11        1.1    dogcow  *
     12        1.1    dogcow  * Redistribution and use in source and binary forms, with or without
     13        1.1    dogcow  * modification, are permitted provided that the following conditions
     14        1.1    dogcow  * are met:
     15        1.1    dogcow  * 1. Redistributions of source code must retain the above copyright
     16        1.1    dogcow  *    notice, this list of conditions and the following disclaimer.
     17        1.1    dogcow  * 2. Redistributions in binary form must reproduce the above copyright
     18        1.1    dogcow  *    notice, this list of conditions and the following disclaimer in the
     19        1.1    dogcow  *    documentation and/or other materials provided with the distribution.
     20        1.1    dogcow  *
     21        1.1    dogcow  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22        1.1    dogcow  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23        1.1    dogcow  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24        1.1    dogcow  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25        1.1    dogcow  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26        1.1    dogcow  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27        1.1    dogcow  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28        1.1    dogcow  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29        1.1    dogcow  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30        1.1    dogcow  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31        1.1    dogcow  * POSSIBILITY OF SUCH DAMAGE.
     32        1.1    dogcow  */
     33        1.1    dogcow 
     34        1.9  jakllsch /*
     35        1.9  jakllsch  * Copyright (c) 2007, 2009 Jonathan A. Kollasch.
     36        1.9  jakllsch  * All rights reserved.
     37        1.9  jakllsch  *
     38        1.9  jakllsch  * Redistribution and use in source and binary forms, with or without
     39        1.9  jakllsch  * modification, are permitted provided that the following conditions
     40        1.9  jakllsch  * are met:
     41        1.9  jakllsch  * 1. Redistributions of source code must retain the above copyright
     42        1.9  jakllsch  *    notice, this list of conditions and the following disclaimer.
     43        1.9  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     44        1.9  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     45        1.9  jakllsch  *    documentation and/or other materials provided with the distribution.
     46        1.9  jakllsch  *
     47        1.9  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48        1.9  jakllsch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49        1.9  jakllsch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50        1.9  jakllsch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51        1.9  jakllsch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52        1.9  jakllsch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53        1.9  jakllsch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54        1.9  jakllsch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55        1.9  jakllsch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56        1.9  jakllsch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57        1.9  jakllsch  *
     58        1.9  jakllsch  */
     59        1.9  jakllsch 
     60        1.2   xtraeme #include <sys/cdefs.h>
     61  1.32.10.1  perseant __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.32.10.1 2025/08/02 05:57:07 perseant Exp $");
     62       1.22     skrll 
     63       1.22     skrll #ifdef _KERNEL_OPT
     64       1.22     skrll #include "opt_usb.h"
     65       1.22     skrll #endif
     66        1.2   xtraeme 
     67        1.1    dogcow #include <sys/param.h>
     68        1.1    dogcow #include <sys/systm.h>
     69        1.1    dogcow #include <sys/kernel.h>
     70        1.1    dogcow #include <sys/device.h>
     71        1.1    dogcow #include <sys/conf.h>
     72        1.1    dogcow #include <sys/tty.h>
     73        1.1    dogcow 
     74        1.1    dogcow #include <dev/usb/usb.h>
     75        1.1    dogcow 
     76        1.1    dogcow #include <dev/usb/usbdi.h>
     77        1.1    dogcow #include <dev/usb/usbdi_util.h>
     78        1.1    dogcow #include <dev/usb/usbdevs.h>
     79        1.1    dogcow 
     80        1.1    dogcow #include <dev/usb/ucomvar.h>
     81        1.1    dogcow 
     82       1.17  jakllsch #include <dev/usb/uslsareg.h>
     83       1.17  jakllsch 
     84       1.17  jakllsch #include <fs/unicode.h>
     85        1.1    dogcow 
     86        1.1    dogcow #ifdef USLSA_DEBUG
     87       1.17  jakllsch #define DPRINTF(x)	if (uslsadebug) device_printf x
     88        1.1    dogcow int uslsadebug = 0;
     89        1.1    dogcow #else
     90        1.1    dogcow #define DPRINTF(x)
     91        1.1    dogcow #endif
     92        1.1    dogcow 
     93        1.1    dogcow struct uslsa_softc {
     94       1.12    dyoung 	device_t		sc_dev;		/* base device */
     95       1.17  jakllsch 	device_t		sc_subdev;	/* ucom device */
     96       1.20     skrll 	struct usbd_device *	sc_udev;	/* usb device */
     97       1.20     skrll 	struct usbd_interface *	sc_iface;	/* interface */
     98       1.17  jakllsch 	uint8_t			sc_ifnum;	/* interface number */
     99       1.17  jakllsch 	bool			sc_dying;	/* disconnecting */
    100        1.1    dogcow };
    101        1.1    dogcow 
    102        1.1    dogcow static void uslsa_get_status(void *sc, int, u_char *, u_char *);
    103        1.1    dogcow static void uslsa_set(void *, int, int, int);
    104        1.1    dogcow static int uslsa_param(void *, int, struct termios *);
    105       1.17  jakllsch static int uslsa_ioctl(void *, int, u_long, void *, int, proc_t *);
    106        1.1    dogcow static int uslsa_open(void *, int);
    107        1.1    dogcow static void uslsa_close(void *, int);
    108        1.1    dogcow 
    109       1.17  jakllsch static int uslsa_usbd_errno(usbd_status);
    110        1.1    dogcow static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t);
    111       1.17  jakllsch static int uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t);
    112       1.17  jakllsch 
    113       1.14  jakllsch static const struct ucom_methods uslsa_methods = {
    114       1.20     skrll 	.ucom_get_status = uslsa_get_status,
    115       1.20     skrll 	.ucom_set = uslsa_set,
    116       1.20     skrll 	.ucom_param = uslsa_param,
    117       1.20     skrll 	.ucom_ioctl = uslsa_ioctl,
    118       1.20     skrll 	.ucom_open = uslsa_open,
    119       1.20     skrll 	.ucom_close = uslsa_close,
    120        1.1    dogcow };
    121        1.1    dogcow 
    122        1.1    dogcow #define USLSA_CONFIG_INDEX	0
    123        1.1    dogcow #define USLSA_IFACE_INDEX	0
    124        1.1    dogcow #define USLSA_BUFSIZE		256
    125        1.1    dogcow 
    126        1.1    dogcow static const struct usb_devno uslsa_devs[] = {
    127        1.1    dogcow         { USB_VENDOR_BALTECH,           USB_PRODUCT_BALTECH_CARDREADER },
    128        1.1    dogcow         { USB_VENDOR_DYNASTREAM,        USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
    129        1.1    dogcow         { USB_VENDOR_JABLOTRON,         USB_PRODUCT_JABLOTRON_PC60B },
    130        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_ARGUSISP },
    131        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CRUMB128 },
    132        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DEGREECONT },
    133        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DESKTOPMOBILE },
    134        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_IPLINK1220 },
    135        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_HARP },
    136        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
    137        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_LIN },
    138        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_POLOLU },
    139        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_1 },
    140        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_2 },
    141  1.32.10.1  perseant         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_3 },
    142        1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUNNTO },
    143        1.1    dogcow         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
    144       1.25  jakllsch         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 },
    145       1.24  jakllsch 	{ USB_VENDOR_WMR,		USB_PRODUCT_WMR_RIGBLASTER },
    146        1.1    dogcow };
    147        1.1    dogcow 
    148       1.14  jakllsch static int uslsa_match(device_t, cfdata_t, void *);
    149       1.14  jakllsch static void uslsa_attach(device_t, device_t, void *);
    150       1.14  jakllsch static void uslsa_childdet(device_t, device_t);
    151       1.14  jakllsch static int uslsa_detach(device_t, int);
    152       1.14  jakllsch 
    153        1.8      cube CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
    154       1.28       mrg     uslsa_attach, uslsa_detach, NULL, NULL, uslsa_childdet);
    155        1.1    dogcow 
    156       1.17  jakllsch static int
    157       1.12    dyoung uslsa_match(device_t parent, cfdata_t match, void *aux)
    158        1.1    dogcow {
    159       1.20     skrll 	const struct usbif_attach_arg *uiaa = aux;
    160       1.17  jakllsch 
    161       1.21   msaitoh 	if (usb_lookup(uslsa_devs, uiaa->uiaa_vendor, uiaa->uiaa_product)
    162       1.21   msaitoh 	    != NULL)
    163       1.17  jakllsch 		return UMATCH_VENDOR_PRODUCT;
    164       1.21   msaitoh 	else
    165       1.17  jakllsch 		return UMATCH_NONE;
    166        1.1    dogcow }
    167        1.1    dogcow 
    168       1.17  jakllsch static void
    169       1.12    dyoung uslsa_attach(device_t parent, device_t self, void *aux)
    170        1.1    dogcow {
    171       1.17  jakllsch 	struct uslsa_softc *sc;
    172       1.20     skrll 	const struct usbif_attach_arg *uiaa = aux;
    173       1.17  jakllsch 	const usb_interface_descriptor_t *id;
    174       1.17  jakllsch 	const usb_endpoint_descriptor_t *ed;
    175        1.1    dogcow 	char *devinfop;
    176       1.20     skrll 	struct ucom_attach_args ucaa;
    177        1.1    dogcow 	int i;
    178        1.1    dogcow 
    179       1.17  jakllsch 	sc = device_private(self);
    180       1.17  jakllsch 
    181        1.8      cube 	sc->sc_dev = self;
    182       1.20     skrll 	sc->sc_udev = uiaa->uiaa_device;
    183       1.20     skrll 	sc->sc_iface = uiaa->uiaa_iface;
    184       1.28       mrg 	sc->sc_dying = false;
    185        1.1    dogcow 
    186       1.10    plunky 	aprint_naive("\n");
    187       1.10    plunky 	aprint_normal("\n");
    188       1.10    plunky 
    189       1.17  jakllsch 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    190       1.10    plunky 	aprint_normal_dev(self, "%s\n", devinfop);
    191       1.10    plunky 	usbd_devinfo_free(devinfop);
    192       1.10    plunky 
    193       1.17  jakllsch 	id = usbd_get_interface_descriptor(sc->sc_iface);
    194        1.1    dogcow 
    195       1.17  jakllsch 	sc->sc_ifnum = id->bInterfaceNumber;
    196        1.1    dogcow 
    197       1.20     skrll 	ucaa.ucaa_info = "Silicon Labs CP210x";
    198       1.20     skrll 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    199       1.20     skrll 	ucaa.ucaa_ibufsize = USLSA_BUFSIZE;
    200       1.20     skrll 	ucaa.ucaa_obufsize = USLSA_BUFSIZE;
    201       1.20     skrll 	ucaa.ucaa_ibufsizepad = USLSA_BUFSIZE;
    202       1.20     skrll 	ucaa.ucaa_opkthdrlen = 0;
    203       1.20     skrll 	ucaa.ucaa_device = sc->sc_udev;
    204       1.20     skrll 	ucaa.ucaa_iface = sc->sc_iface;
    205       1.20     skrll 	ucaa.ucaa_methods = &uslsa_methods;
    206       1.20     skrll 	ucaa.ucaa_arg = sc;
    207        1.1    dogcow 
    208       1.21   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    209        1.1    dogcow 
    210       1.20     skrll 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    211        1.1    dogcow 	for (i = 0; i < id->bNumEndpoints; i++) {
    212        1.1    dogcow 		int addr, dir, attr;
    213        1.1    dogcow 
    214       1.17  jakllsch 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    215        1.1    dogcow 		if (ed == NULL) {
    216        1.8      cube 			aprint_error_dev(self,
    217       1.17  jakllsch 			    "could not read endpoint descriptor\n");
    218       1.17  jakllsch 			sc->sc_dying = true;
    219       1.17  jakllsch 			return;
    220        1.1    dogcow 		}
    221        1.1    dogcow 		addr = ed->bEndpointAddress;
    222        1.1    dogcow 		dir = UE_GET_DIR(ed->bEndpointAddress);
    223        1.1    dogcow 		attr = ed->bmAttributes & UE_XFERTYPE;
    224       1.17  jakllsch 		if (dir == UE_DIR_IN && attr == UE_BULK) {
    225       1.20     skrll 			ucaa.ucaa_bulkin = addr;
    226       1.17  jakllsch 		} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
    227       1.20     skrll 			ucaa.ucaa_bulkout = addr;
    228       1.17  jakllsch 		} else {
    229        1.8      cube 			aprint_error_dev(self, "unexpected endpoint\n");
    230       1.17  jakllsch 		}
    231        1.1    dogcow 	}
    232       1.17  jakllsch 	aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n",
    233       1.20     skrll 		ucaa.ucaa_bulkin, ucaa.ucaa_bulkout);
    234       1.20     skrll 	if ((ucaa.ucaa_bulkin == -1) || (ucaa.ucaa_bulkout == -1)) {
    235       1.17  jakllsch 		aprint_error_dev(self, "could not find endpoints\n");
    236       1.17  jakllsch 		sc->sc_dying = true;
    237       1.17  jakllsch 		return;
    238        1.1    dogcow 	}
    239        1.1    dogcow 
    240       1.30   thorpej 	sc->sc_subdev = config_found(self, &ucaa, ucomprint,
    241       1.31   thorpej 	    CFARGS(.submatch = ucomsubmatch));
    242        1.1    dogcow 
    243       1.23      maya 	if (!pmf_device_register(self, NULL, NULL))
    244       1.23      maya 		aprint_error_dev(self, "couldn't establish power handler\n");
    245       1.23      maya 
    246       1.12    dyoung 	return;
    247        1.1    dogcow }
    248        1.1    dogcow 
    249       1.14  jakllsch static void
    250        1.5    dyoung uslsa_childdet(device_t self, device_t child)
    251        1.5    dyoung {
    252        1.5    dyoung 	struct uslsa_softc *sc = device_private(self);
    253        1.5    dyoung 
    254        1.5    dyoung 	KASSERT(sc->sc_subdev == child);
    255        1.5    dyoung 	sc->sc_subdev = NULL;
    256        1.5    dyoung }
    257        1.5    dyoung 
    258       1.17  jakllsch static int
    259       1.12    dyoung uslsa_detach(device_t self, int flags)
    260        1.1    dogcow {
    261       1.12    dyoung 	struct uslsa_softc *sc = device_private(self);
    262        1.1    dogcow 	int rv = 0;
    263        1.1    dogcow 
    264       1.17  jakllsch 	DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags));
    265        1.1    dogcow 
    266       1.17  jakllsch 	sc->sc_dying = true;
    267        1.1    dogcow 
    268       1.17  jakllsch 	if (sc->sc_subdev != NULL) {
    269        1.1    dogcow 		rv = config_detach(sc->sc_subdev, flags);
    270       1.28       mrg 		sc->sc_subdev = NULL;
    271       1.17  jakllsch 	}
    272        1.1    dogcow 
    273       1.21   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    274        1.1    dogcow 
    275       1.26     skrll 	return rv;
    276        1.1    dogcow }
    277        1.1    dogcow 
    278       1.17  jakllsch static int
    279       1.17  jakllsch uslsa_usbd_errno(usbd_status status)
    280       1.17  jakllsch {
    281       1.17  jakllsch 	switch (status) {
    282       1.17  jakllsch 	case USBD_NORMAL_COMPLETION:
    283       1.17  jakllsch 		return 0;
    284       1.17  jakllsch 	case USBD_STALLED:
    285       1.17  jakllsch 		return EINVAL;
    286       1.17  jakllsch 	default:
    287       1.17  jakllsch 		return EIO;
    288       1.17  jakllsch 	}
    289       1.17  jakllsch }
    290       1.17  jakllsch 
    291        1.1    dogcow static void
    292        1.1    dogcow uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    293        1.1    dogcow {
    294        1.1    dogcow 	struct uslsa_softc *sc;
    295        1.1    dogcow 	usb_device_request_t req;
    296       1.17  jakllsch 	usbd_status status;
    297       1.17  jakllsch 	uint8_t mdmsts;
    298        1.1    dogcow 
    299        1.1    dogcow 	sc = vsc;
    300        1.1    dogcow 
    301       1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno));
    302       1.17  jakllsch 
    303       1.28       mrg 	if (sc->sc_dying)
    304       1.17  jakllsch 		return;
    305        1.1    dogcow 
    306        1.9  jakllsch 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    307       1.17  jakllsch 	req.bRequest = SLSA_R_GET_MDMSTS;
    308        1.1    dogcow 	USETW(req.wValue, 0);
    309       1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    310       1.17  jakllsch 	USETW(req.wLength, SLSA_RL_GET_MDMSTS);
    311       1.17  jakllsch 
    312       1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &mdmsts);
    313       1.17  jakllsch 	if (status != USBD_NORMAL_COMPLETION) {
    314       1.17  jakllsch 		device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n",
    315       1.17  jakllsch 		    __func__, usbd_errstr(status));
    316       1.17  jakllsch 		return;
    317       1.17  jakllsch 	}
    318       1.17  jakllsch 
    319       1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts));
    320       1.17  jakllsch 
    321       1.27       mrg 	*lsr = 0;
    322        1.1    dogcow 
    323       1.27       mrg 	*msr  = ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0;
    324       1.27       mrg 	*msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0;
    325       1.27       mrg 	*msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0;
    326       1.27       mrg 	*msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0;
    327        1.1    dogcow }
    328        1.1    dogcow 
    329        1.1    dogcow static void
    330        1.1    dogcow uslsa_set(void *vsc, int portno, int reg, int onoff)
    331        1.1    dogcow {
    332        1.1    dogcow 	struct uslsa_softc *sc;
    333        1.1    dogcow 
    334        1.1    dogcow 	sc = vsc;
    335        1.1    dogcow 
    336       1.21   msaitoh 	DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno,
    337       1.21   msaitoh 	    reg, onoff));
    338       1.17  jakllsch 
    339       1.28       mrg 	if (sc->sc_dying)
    340       1.17  jakllsch 		return;
    341        1.1    dogcow 
    342        1.1    dogcow 	switch (reg) {
    343        1.1    dogcow 	case UCOM_SET_DTR:
    344       1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_MHS,
    345       1.17  jakllsch 		    SLSA_RV_SET_MHS_DTR_MASK |
    346       1.17  jakllsch 		    (onoff ? SLSA_RV_SET_MHS_DTR : 0))) {
    347       1.17  jakllsch 			device_printf(sc->sc_dev, "SET_MHS/DTR failed\n");
    348       1.17  jakllsch 		}
    349        1.1    dogcow 		break;
    350        1.1    dogcow 	case UCOM_SET_RTS:
    351       1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_MHS,
    352       1.17  jakllsch 		    SLSA_RV_SET_MHS_RTS_MASK |
    353       1.17  jakllsch 		    (onoff ? SLSA_RV_SET_MHS_RTS : 0))) {
    354       1.17  jakllsch 			device_printf(sc->sc_dev, "SET_MHS/RTS failed\n");
    355       1.17  jakllsch 		}
    356        1.1    dogcow 		break;
    357        1.1    dogcow 	case UCOM_SET_BREAK:
    358       1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_BREAK,
    359       1.17  jakllsch 		    (onoff ? SLSA_RV_SET_BREAK_ENABLE :
    360       1.17  jakllsch 		     SLSA_RV_SET_BREAK_DISABLE))) {
    361       1.17  jakllsch 			device_printf(sc->sc_dev, "SET_BREAK failed\n");
    362       1.17  jakllsch 		}
    363        1.1    dogcow 		break;
    364        1.1    dogcow 	default:
    365        1.1    dogcow 		break;
    366        1.1    dogcow 	}
    367        1.1    dogcow }
    368        1.1    dogcow 
    369        1.1    dogcow static int
    370       1.17  jakllsch uslsa_param(void *vsc, int portno, struct termios *t)
    371        1.1    dogcow {
    372        1.1    dogcow 	struct uslsa_softc *sc;
    373       1.19   reinoud 	usb_device_request_t req;
    374       1.19   reinoud 	usbd_status status;
    375       1.19   reinoud 	uint16_t value;
    376       1.19   reinoud 	uint32_t baud;
    377       1.17  jakllsch 	int ret;
    378        1.1    dogcow 
    379        1.1    dogcow 	sc = vsc;
    380        1.1    dogcow 
    381       1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t));
    382       1.17  jakllsch 
    383       1.28       mrg 	if (sc->sc_dying)
    384       1.17  jakllsch 		return EIO;
    385       1.17  jakllsch 
    386       1.19   reinoud 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    387       1.19   reinoud 	req.bRequest = SLSA_R_SET_BAUDRATE;
    388       1.19   reinoud 	USETW(req.wValue, 0);
    389       1.19   reinoud 	USETW(req.wIndex, sc->sc_ifnum);
    390       1.19   reinoud 	USETW(req.wLength, 4);
    391       1.17  jakllsch 
    392       1.32       rin 	baud = htole32(t->c_ospeed);
    393       1.19   reinoud 	status = usbd_do_request(sc->sc_udev, &req, &baud);
    394       1.19   reinoud 	if (status != USBD_NORMAL_COMPLETION) {
    395       1.19   reinoud 		/* fallback method for devices that don't know SET_BAUDRATE */
    396       1.19   reinoud 		/* hope we calculate it right */
    397       1.19   reinoud 		device_printf(sc->sc_dev, "%s: set baudrate %d, failed %s,"
    398       1.19   reinoud 				" using set bauddiv\n",
    399       1.19   reinoud 		    __func__, baud, usbd_errstr(status));
    400       1.19   reinoud 
    401       1.19   reinoud 		value = SLSA_RV_BAUDDIV(t->c_ospeed);
    402       1.21   msaitoh 		if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value))
    403       1.21   msaitoh 		    != 0) {
    404       1.19   reinoud 			device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n",
    405       1.19   reinoud 			       __func__);
    406       1.19   reinoud 			return ret;
    407       1.19   reinoud 		}
    408       1.17  jakllsch 	}
    409       1.17  jakllsch 
    410       1.17  jakllsch 	value = 0;
    411        1.1    dogcow 
    412       1.17  jakllsch 	if (ISSET(t->c_cflag, CSTOPB)) {
    413       1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_STOP_2;
    414       1.17  jakllsch 	} else {
    415       1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_STOP_1;
    416       1.17  jakllsch 	}
    417        1.1    dogcow 
    418        1.1    dogcow 	if (ISSET(t->c_cflag, PARENB)) {
    419       1.17  jakllsch 		if (ISSET(t->c_cflag, PARODD)) {
    420       1.17  jakllsch 			value |= SLSA_RV_LINE_CTL_PARITY_ODD;
    421       1.17  jakllsch 		} else {
    422       1.17  jakllsch 			value |= SLSA_RV_LINE_CTL_PARITY_EVEN;
    423       1.17  jakllsch 		}
    424       1.17  jakllsch 	} else {
    425       1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_PARITY_NONE;
    426       1.17  jakllsch 	}
    427        1.1    dogcow 
    428        1.1    dogcow 	switch (ISSET(t->c_cflag, CSIZE)) {
    429        1.1    dogcow 	case CS5:
    430       1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_5;
    431        1.1    dogcow 		break;
    432        1.1    dogcow 	case CS6:
    433       1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_6;
    434        1.1    dogcow 		break;
    435        1.1    dogcow 	case CS7:
    436       1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_7;
    437        1.1    dogcow 		break;
    438        1.1    dogcow 	case CS8:
    439       1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_8;
    440        1.1    dogcow 		break;
    441        1.1    dogcow 	}
    442        1.1    dogcow 
    443       1.29  christos 	DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to %#x\n",
    444       1.17  jakllsch 	    __func__, value));
    445       1.17  jakllsch 	if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) {
    446       1.17  jakllsch 		device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
    447       1.17  jakllsch 		return ret;
    448       1.17  jakllsch 	}
    449       1.17  jakllsch 
    450       1.17  jakllsch 	if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) {
    451       1.17  jakllsch 		device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
    452       1.17  jakllsch 	}
    453       1.17  jakllsch 
    454       1.17  jakllsch 	return ret;
    455       1.17  jakllsch }
    456       1.17  jakllsch 
    457       1.17  jakllsch static int
    458       1.17  jakllsch uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p)
    459       1.17  jakllsch {
    460       1.17  jakllsch 	struct uslsa_softc *sc;
    461        1.1    dogcow 
    462       1.17  jakllsch 	sc = vsc;
    463       1.17  jakllsch 
    464       1.28       mrg 	if (sc->sc_dying)
    465       1.28       mrg 		return EIO;
    466       1.28       mrg 
    467       1.17  jakllsch 	switch (cmd) {
    468       1.17  jakllsch 	case TIOCMGET:
    469       1.17  jakllsch 		ucom_status_change(device_private(sc->sc_subdev));
    470       1.17  jakllsch 		return EPASSTHROUGH;
    471       1.17  jakllsch 	default:
    472       1.17  jakllsch 		return EPASSTHROUGH;
    473       1.17  jakllsch 	}
    474        1.1    dogcow 
    475        1.1    dogcow 	return 0;
    476        1.1    dogcow }
    477        1.1    dogcow 
    478        1.1    dogcow static int
    479        1.1    dogcow uslsa_open(void *vsc, int portno)
    480        1.1    dogcow {
    481        1.1    dogcow 	struct uslsa_softc *sc;
    482        1.1    dogcow 
    483        1.1    dogcow 	sc = vsc;
    484        1.1    dogcow 
    485       1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
    486        1.1    dogcow 
    487       1.28       mrg 	if (sc->sc_dying)
    488       1.17  jakllsch 		return EIO;
    489        1.1    dogcow 
    490       1.17  jakllsch 	return uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
    491       1.17  jakllsch 	    SLSA_RV_IFC_ENABLE_ENABLE);
    492        1.1    dogcow }
    493        1.1    dogcow 
    494       1.14  jakllsch static void
    495        1.1    dogcow uslsa_close(void *vsc, int portno)
    496        1.1    dogcow {
    497        1.1    dogcow 	struct uslsa_softc *sc;
    498        1.1    dogcow 
    499        1.1    dogcow 	sc = vsc;
    500        1.1    dogcow 
    501       1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
    502       1.17  jakllsch 
    503       1.28       mrg 	if (sc->sc_dying)
    504        1.1    dogcow 		return;
    505        1.1    dogcow 
    506       1.28       mrg 	uslsa_request_set(sc, SLSA_R_IFC_ENABLE, SLSA_RV_IFC_ENABLE_DISABLE);
    507        1.1    dogcow }
    508        1.1    dogcow 
    509        1.1    dogcow static int
    510        1.1    dogcow uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
    511        1.1    dogcow {
    512        1.1    dogcow 	usb_device_request_t req;
    513       1.17  jakllsch 	usbd_status status;
    514        1.1    dogcow 
    515        1.9  jakllsch 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    516        1.1    dogcow 	req.bRequest = request;
    517        1.1    dogcow 	USETW(req.wValue, value);
    518       1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    519        1.1    dogcow 	USETW(req.wLength, 0);
    520        1.1    dogcow 
    521       1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, NULL);
    522       1.17  jakllsch 
    523       1.17  jakllsch 	return uslsa_usbd_errno(status);
    524        1.1    dogcow }
    525        1.1    dogcow 
    526       1.17  jakllsch static int
    527        1.1    dogcow uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
    528        1.1    dogcow {
    529       1.17  jakllsch 	struct slsa_fcs fcs;
    530        1.1    dogcow 	usb_device_request_t req;
    531       1.17  jakllsch 	uint32_t ulControlHandshake;
    532       1.17  jakllsch 	uint32_t ulFlowReplace;
    533       1.17  jakllsch 	usbd_status status;
    534        1.1    dogcow 
    535       1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag));
    536        1.1    dogcow 
    537        1.9  jakllsch 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    538       1.17  jakllsch 	req.bRequest = SLSA_R_GET_FLOW;
    539        1.1    dogcow 	USETW(req.wValue, 0);
    540       1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    541       1.17  jakllsch 	USETW(req.wLength, SLSA_RL_GET_FLOW);
    542       1.17  jakllsch 
    543       1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &fcs);
    544       1.17  jakllsch 	if (status != USBD_NORMAL_COMPLETION) {
    545       1.17  jakllsch 		device_printf(sc->sc_dev, "%s: GET_FLOW %s\n",
    546       1.17  jakllsch 			__func__, usbd_errstr(status));
    547       1.17  jakllsch 		return uslsa_usbd_errno(status);
    548       1.17  jakllsch 	}
    549        1.1    dogcow 
    550       1.17  jakllsch 	ulControlHandshake = le32toh(fcs.ulControlHandshake);
    551       1.17  jakllsch 	ulFlowReplace = le32toh(fcs.ulFlowReplace);
    552        1.1    dogcow 
    553        1.1    dogcow 	if (ISSET(cflag, CRTSCTS)) {
    554       1.17  jakllsch 		ulControlHandshake =
    555       1.17  jakllsch 		    SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK);
    556       1.17  jakllsch 		ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK);
    557        1.1    dogcow 	} else {
    558       1.17  jakllsch 		ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK);
    559       1.17  jakllsch 		ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK);
    560        1.1    dogcow 	}
    561        1.1    dogcow 
    562       1.17  jakllsch 	fcs.ulControlHandshake = htole32(ulControlHandshake);
    563       1.17  jakllsch 	fcs.ulFlowReplace = htole32(ulFlowReplace);
    564       1.17  jakllsch 
    565        1.9  jakllsch 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    566       1.17  jakllsch 	req.bRequest = SLSA_R_SET_FLOW;
    567        1.1    dogcow 	USETW(req.wValue, 0);
    568       1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    569       1.17  jakllsch 	USETW(req.wLength, SLSA_RL_SET_FLOW);
    570       1.17  jakllsch 
    571       1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &fcs);
    572        1.1    dogcow 
    573       1.17  jakllsch 	return uslsa_usbd_errno(status);
    574        1.1    dogcow }
    575