Home | History | Annotate | Line # | Download | only in usb
uslsa.c revision 1.25.4.2
      1  1.25.4.2    martin /* $NetBSD: uslsa.c,v 1.25.4.2 2020/04/08 14:08:14 martin 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.25.4.2    martin __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.25.4.2 2020/04/08 14:08:14 martin 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.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUNNTO },
    142       1.1    dogcow         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
    143      1.25  jakllsch         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 },
    144      1.24  jakllsch 	{ USB_VENDOR_WMR,		USB_PRODUCT_WMR_RIGBLASTER },
    145       1.1    dogcow };
    146       1.1    dogcow 
    147      1.14  jakllsch static int uslsa_match(device_t, cfdata_t, void *);
    148      1.14  jakllsch static void uslsa_attach(device_t, device_t, void *);
    149      1.14  jakllsch static void uslsa_childdet(device_t, device_t);
    150      1.14  jakllsch static int uslsa_detach(device_t, int);
    151      1.14  jakllsch 
    152       1.8      cube CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
    153  1.25.4.1  christos     uslsa_attach, uslsa_detach, NULL, NULL, uslsa_childdet);
    154       1.1    dogcow 
    155      1.17  jakllsch static int
    156      1.12    dyoung uslsa_match(device_t parent, cfdata_t match, void *aux)
    157       1.1    dogcow {
    158      1.20     skrll 	const struct usbif_attach_arg *uiaa = aux;
    159      1.17  jakllsch 
    160      1.21   msaitoh 	if (usb_lookup(uslsa_devs, uiaa->uiaa_vendor, uiaa->uiaa_product)
    161      1.21   msaitoh 	    != NULL)
    162      1.17  jakllsch 		return UMATCH_VENDOR_PRODUCT;
    163      1.21   msaitoh 	else
    164      1.17  jakllsch 		return UMATCH_NONE;
    165       1.1    dogcow }
    166       1.1    dogcow 
    167      1.17  jakllsch static void
    168      1.12    dyoung uslsa_attach(device_t parent, device_t self, void *aux)
    169       1.1    dogcow {
    170      1.17  jakllsch 	struct uslsa_softc *sc;
    171      1.20     skrll 	const struct usbif_attach_arg *uiaa = aux;
    172      1.17  jakllsch 	const usb_interface_descriptor_t *id;
    173      1.17  jakllsch 	const usb_endpoint_descriptor_t *ed;
    174       1.1    dogcow 	char *devinfop;
    175      1.20     skrll 	struct ucom_attach_args ucaa;
    176       1.1    dogcow 	int i;
    177       1.1    dogcow 
    178      1.17  jakllsch 	sc = device_private(self);
    179      1.17  jakllsch 
    180       1.8      cube 	sc->sc_dev = self;
    181      1.20     skrll 	sc->sc_udev = uiaa->uiaa_device;
    182      1.20     skrll 	sc->sc_iface = uiaa->uiaa_iface;
    183  1.25.4.1  christos 	sc->sc_dying = false;
    184       1.1    dogcow 
    185      1.10    plunky 	aprint_naive("\n");
    186      1.10    plunky 	aprint_normal("\n");
    187      1.10    plunky 
    188      1.17  jakllsch 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    189      1.10    plunky 	aprint_normal_dev(self, "%s\n", devinfop);
    190      1.10    plunky 	usbd_devinfo_free(devinfop);
    191      1.10    plunky 
    192      1.17  jakllsch 	id = usbd_get_interface_descriptor(sc->sc_iface);
    193       1.1    dogcow 
    194      1.17  jakllsch 	sc->sc_ifnum = id->bInterfaceNumber;
    195       1.1    dogcow 
    196      1.20     skrll 	ucaa.ucaa_info = "Silicon Labs CP210x";
    197      1.20     skrll 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    198      1.20     skrll 	ucaa.ucaa_ibufsize = USLSA_BUFSIZE;
    199      1.20     skrll 	ucaa.ucaa_obufsize = USLSA_BUFSIZE;
    200      1.20     skrll 	ucaa.ucaa_ibufsizepad = USLSA_BUFSIZE;
    201      1.20     skrll 	ucaa.ucaa_opkthdrlen = 0;
    202      1.20     skrll 	ucaa.ucaa_device = sc->sc_udev;
    203      1.20     skrll 	ucaa.ucaa_iface = sc->sc_iface;
    204      1.20     skrll 	ucaa.ucaa_methods = &uslsa_methods;
    205      1.20     skrll 	ucaa.ucaa_arg = sc;
    206       1.1    dogcow 
    207      1.21   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    208       1.1    dogcow 
    209      1.20     skrll 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    210       1.1    dogcow 	for (i = 0; i < id->bNumEndpoints; i++) {
    211       1.1    dogcow 		int addr, dir, attr;
    212       1.1    dogcow 
    213      1.17  jakllsch 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    214       1.1    dogcow 		if (ed == NULL) {
    215       1.8      cube 			aprint_error_dev(self,
    216      1.17  jakllsch 			    "could not read endpoint descriptor\n");
    217      1.17  jakllsch 			sc->sc_dying = true;
    218      1.17  jakllsch 			return;
    219       1.1    dogcow 		}
    220       1.1    dogcow 		addr = ed->bEndpointAddress;
    221       1.1    dogcow 		dir = UE_GET_DIR(ed->bEndpointAddress);
    222       1.1    dogcow 		attr = ed->bmAttributes & UE_XFERTYPE;
    223      1.17  jakllsch 		if (dir == UE_DIR_IN && attr == UE_BULK) {
    224      1.20     skrll 			ucaa.ucaa_bulkin = addr;
    225      1.17  jakllsch 		} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
    226      1.20     skrll 			ucaa.ucaa_bulkout = addr;
    227      1.17  jakllsch 		} else {
    228       1.8      cube 			aprint_error_dev(self, "unexpected endpoint\n");
    229      1.17  jakllsch 		}
    230       1.1    dogcow 	}
    231      1.17  jakllsch 	aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n",
    232      1.20     skrll 		ucaa.ucaa_bulkin, ucaa.ucaa_bulkout);
    233      1.20     skrll 	if ((ucaa.ucaa_bulkin == -1) || (ucaa.ucaa_bulkout == -1)) {
    234      1.17  jakllsch 		aprint_error_dev(self, "could not find endpoints\n");
    235      1.17  jakllsch 		sc->sc_dying = true;
    236      1.17  jakllsch 		return;
    237       1.1    dogcow 	}
    238       1.1    dogcow 
    239      1.20     skrll 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    240       1.1    dogcow 	                                    ucomprint, ucomsubmatch);
    241       1.1    dogcow 
    242      1.23      maya 	if (!pmf_device_register(self, NULL, NULL))
    243      1.23      maya 		aprint_error_dev(self, "couldn't establish power handler\n");
    244      1.23      maya 
    245      1.12    dyoung 	return;
    246       1.1    dogcow }
    247       1.1    dogcow 
    248      1.14  jakllsch static void
    249       1.5    dyoung uslsa_childdet(device_t self, device_t child)
    250       1.5    dyoung {
    251       1.5    dyoung 	struct uslsa_softc *sc = device_private(self);
    252       1.5    dyoung 
    253       1.5    dyoung 	KASSERT(sc->sc_subdev == child);
    254       1.5    dyoung 	sc->sc_subdev = NULL;
    255       1.5    dyoung }
    256       1.5    dyoung 
    257      1.17  jakllsch static int
    258      1.12    dyoung uslsa_detach(device_t self, int flags)
    259       1.1    dogcow {
    260      1.12    dyoung 	struct uslsa_softc *sc = device_private(self);
    261       1.1    dogcow 	int rv = 0;
    262       1.1    dogcow 
    263      1.17  jakllsch 	DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags));
    264       1.1    dogcow 
    265      1.17  jakllsch 	sc->sc_dying = true;
    266       1.1    dogcow 
    267      1.17  jakllsch 	if (sc->sc_subdev != NULL) {
    268       1.1    dogcow 		rv = config_detach(sc->sc_subdev, flags);
    269  1.25.4.1  christos 		sc->sc_subdev = NULL;
    270      1.17  jakllsch 	}
    271       1.1    dogcow 
    272      1.21   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    273       1.1    dogcow 
    274  1.25.4.1  christos 	return rv;
    275       1.1    dogcow }
    276       1.1    dogcow 
    277      1.17  jakllsch static int
    278      1.17  jakllsch uslsa_usbd_errno(usbd_status status)
    279      1.17  jakllsch {
    280      1.17  jakllsch 	switch (status) {
    281      1.17  jakllsch 	case USBD_NORMAL_COMPLETION:
    282      1.17  jakllsch 		return 0;
    283      1.17  jakllsch 	case USBD_STALLED:
    284      1.17  jakllsch 		return EINVAL;
    285      1.17  jakllsch 	default:
    286      1.17  jakllsch 		return EIO;
    287      1.17  jakllsch 	}
    288      1.17  jakllsch }
    289      1.17  jakllsch 
    290       1.1    dogcow static void
    291       1.1    dogcow uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    292       1.1    dogcow {
    293       1.1    dogcow 	struct uslsa_softc *sc;
    294       1.1    dogcow 	usb_device_request_t req;
    295      1.17  jakllsch 	usbd_status status;
    296      1.17  jakllsch 	uint8_t mdmsts;
    297       1.1    dogcow 
    298       1.1    dogcow 	sc = vsc;
    299       1.1    dogcow 
    300      1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno));
    301      1.17  jakllsch 
    302  1.25.4.1  christos 	if (sc->sc_dying)
    303      1.17  jakllsch 		return;
    304       1.1    dogcow 
    305       1.9  jakllsch 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    306      1.17  jakllsch 	req.bRequest = SLSA_R_GET_MDMSTS;
    307       1.1    dogcow 	USETW(req.wValue, 0);
    308      1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    309      1.17  jakllsch 	USETW(req.wLength, SLSA_RL_GET_MDMSTS);
    310      1.17  jakllsch 
    311      1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &mdmsts);
    312      1.17  jakllsch 	if (status != USBD_NORMAL_COMPLETION) {
    313      1.17  jakllsch 		device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n",
    314      1.17  jakllsch 		    __func__, usbd_errstr(status));
    315      1.17  jakllsch 		return;
    316      1.17  jakllsch 	}
    317      1.17  jakllsch 
    318      1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts));
    319      1.17  jakllsch 
    320  1.25.4.1  christos 	*lsr = 0;
    321       1.1    dogcow 
    322  1.25.4.1  christos 	*msr  = ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0;
    323  1.25.4.1  christos 	*msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0;
    324  1.25.4.1  christos 	*msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0;
    325  1.25.4.1  christos 	*msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0;
    326       1.1    dogcow }
    327       1.1    dogcow 
    328       1.1    dogcow static void
    329       1.1    dogcow uslsa_set(void *vsc, int portno, int reg, int onoff)
    330       1.1    dogcow {
    331       1.1    dogcow 	struct uslsa_softc *sc;
    332       1.1    dogcow 
    333       1.1    dogcow 	sc = vsc;
    334       1.1    dogcow 
    335      1.21   msaitoh 	DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno,
    336      1.21   msaitoh 	    reg, onoff));
    337      1.17  jakllsch 
    338  1.25.4.1  christos 	if (sc->sc_dying)
    339      1.17  jakllsch 		return;
    340       1.1    dogcow 
    341       1.1    dogcow 	switch (reg) {
    342       1.1    dogcow 	case UCOM_SET_DTR:
    343      1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_MHS,
    344      1.17  jakllsch 		    SLSA_RV_SET_MHS_DTR_MASK |
    345      1.17  jakllsch 		    (onoff ? SLSA_RV_SET_MHS_DTR : 0))) {
    346      1.17  jakllsch 			device_printf(sc->sc_dev, "SET_MHS/DTR failed\n");
    347      1.17  jakllsch 		}
    348       1.1    dogcow 		break;
    349       1.1    dogcow 	case UCOM_SET_RTS:
    350      1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_MHS,
    351      1.17  jakllsch 		    SLSA_RV_SET_MHS_RTS_MASK |
    352      1.17  jakllsch 		    (onoff ? SLSA_RV_SET_MHS_RTS : 0))) {
    353      1.17  jakllsch 			device_printf(sc->sc_dev, "SET_MHS/RTS failed\n");
    354      1.17  jakllsch 		}
    355       1.1    dogcow 		break;
    356       1.1    dogcow 	case UCOM_SET_BREAK:
    357      1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_BREAK,
    358      1.17  jakllsch 		    (onoff ? SLSA_RV_SET_BREAK_ENABLE :
    359      1.17  jakllsch 		     SLSA_RV_SET_BREAK_DISABLE))) {
    360      1.17  jakllsch 			device_printf(sc->sc_dev, "SET_BREAK failed\n");
    361      1.17  jakllsch 		}
    362       1.1    dogcow 		break;
    363       1.1    dogcow 	default:
    364       1.1    dogcow 		break;
    365       1.1    dogcow 	}
    366       1.1    dogcow }
    367       1.1    dogcow 
    368       1.1    dogcow static int
    369      1.17  jakllsch uslsa_param(void *vsc, int portno, struct termios *t)
    370       1.1    dogcow {
    371       1.1    dogcow 	struct uslsa_softc *sc;
    372      1.19   reinoud 	usb_device_request_t req;
    373      1.19   reinoud 	usbd_status status;
    374      1.19   reinoud 	uint16_t value;
    375      1.19   reinoud 	uint32_t baud;
    376      1.17  jakllsch 	int ret;
    377       1.1    dogcow 
    378       1.1    dogcow 	sc = vsc;
    379       1.1    dogcow 
    380      1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t));
    381      1.17  jakllsch 
    382  1.25.4.1  christos 	if (sc->sc_dying)
    383      1.17  jakllsch 		return EIO;
    384      1.17  jakllsch 
    385      1.19   reinoud 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    386      1.19   reinoud 	req.bRequest = SLSA_R_SET_BAUDRATE;
    387      1.19   reinoud 	USETW(req.wValue, 0);
    388      1.19   reinoud 	USETW(req.wIndex, sc->sc_ifnum);
    389      1.19   reinoud 	USETW(req.wLength, 4);
    390      1.17  jakllsch 
    391      1.19   reinoud 	baud = t->c_ospeed;
    392      1.19   reinoud 	status = usbd_do_request(sc->sc_udev, &req, &baud);
    393      1.19   reinoud 	if (status != USBD_NORMAL_COMPLETION) {
    394      1.19   reinoud 		/* fallback method for devices that don't know SET_BAUDRATE */
    395      1.19   reinoud 		/* hope we calculate it right */
    396      1.19   reinoud 		device_printf(sc->sc_dev, "%s: set baudrate %d, failed %s,"
    397      1.19   reinoud 				" using set bauddiv\n",
    398      1.19   reinoud 		    __func__, baud, usbd_errstr(status));
    399      1.19   reinoud 
    400      1.19   reinoud 		value = SLSA_RV_BAUDDIV(t->c_ospeed);
    401      1.21   msaitoh 		if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value))
    402      1.21   msaitoh 		    != 0) {
    403      1.19   reinoud 			device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n",
    404      1.19   reinoud 			       __func__);
    405      1.19   reinoud 			return ret;
    406      1.19   reinoud 		}
    407      1.17  jakllsch 	}
    408      1.17  jakllsch 
    409      1.17  jakllsch 	value = 0;
    410       1.1    dogcow 
    411      1.17  jakllsch 	if (ISSET(t->c_cflag, CSTOPB)) {
    412      1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_STOP_2;
    413      1.17  jakllsch 	} else {
    414      1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_STOP_1;
    415      1.17  jakllsch 	}
    416       1.1    dogcow 
    417       1.1    dogcow 	if (ISSET(t->c_cflag, PARENB)) {
    418      1.17  jakllsch 		if (ISSET(t->c_cflag, PARODD)) {
    419      1.17  jakllsch 			value |= SLSA_RV_LINE_CTL_PARITY_ODD;
    420      1.17  jakllsch 		} else {
    421      1.17  jakllsch 			value |= SLSA_RV_LINE_CTL_PARITY_EVEN;
    422      1.17  jakllsch 		}
    423      1.17  jakllsch 	} else {
    424      1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_PARITY_NONE;
    425      1.17  jakllsch 	}
    426       1.1    dogcow 
    427       1.1    dogcow 	switch (ISSET(t->c_cflag, CSIZE)) {
    428       1.1    dogcow 	case CS5:
    429      1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_5;
    430       1.1    dogcow 		break;
    431       1.1    dogcow 	case CS6:
    432      1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_6;
    433       1.1    dogcow 		break;
    434       1.1    dogcow 	case CS7:
    435      1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_7;
    436       1.1    dogcow 		break;
    437       1.1    dogcow 	case CS8:
    438      1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_8;
    439       1.1    dogcow 		break;
    440       1.1    dogcow 	}
    441       1.1    dogcow 
    442  1.25.4.2    martin 	DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to %#x\n",
    443      1.17  jakllsch 	    __func__, value));
    444      1.17  jakllsch 	if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) {
    445      1.17  jakllsch 		device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
    446      1.17  jakllsch 		return ret;
    447      1.17  jakllsch 	}
    448      1.17  jakllsch 
    449      1.17  jakllsch 	if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) {
    450      1.17  jakllsch 		device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
    451      1.17  jakllsch 	}
    452      1.17  jakllsch 
    453      1.17  jakllsch 	return ret;
    454      1.17  jakllsch }
    455      1.17  jakllsch 
    456      1.17  jakllsch static int
    457      1.17  jakllsch uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p)
    458      1.17  jakllsch {
    459      1.17  jakllsch 	struct uslsa_softc *sc;
    460       1.1    dogcow 
    461      1.17  jakllsch 	sc = vsc;
    462      1.17  jakllsch 
    463  1.25.4.1  christos 	if (sc->sc_dying)
    464  1.25.4.1  christos 		return EIO;
    465  1.25.4.1  christos 
    466      1.17  jakllsch 	switch (cmd) {
    467      1.17  jakllsch 	case TIOCMGET:
    468      1.17  jakllsch 		ucom_status_change(device_private(sc->sc_subdev));
    469      1.17  jakllsch 		return EPASSTHROUGH;
    470      1.17  jakllsch 	default:
    471      1.17  jakllsch 		return EPASSTHROUGH;
    472      1.17  jakllsch 	}
    473       1.1    dogcow 
    474       1.1    dogcow 	return 0;
    475       1.1    dogcow }
    476       1.1    dogcow 
    477       1.1    dogcow static int
    478       1.1    dogcow uslsa_open(void *vsc, int portno)
    479       1.1    dogcow {
    480       1.1    dogcow 	struct uslsa_softc *sc;
    481       1.1    dogcow 
    482       1.1    dogcow 	sc = vsc;
    483       1.1    dogcow 
    484      1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
    485       1.1    dogcow 
    486  1.25.4.1  christos 	if (sc->sc_dying)
    487      1.17  jakllsch 		return EIO;
    488       1.1    dogcow 
    489      1.17  jakllsch 	return uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
    490      1.17  jakllsch 	    SLSA_RV_IFC_ENABLE_ENABLE);
    491       1.1    dogcow }
    492       1.1    dogcow 
    493      1.14  jakllsch static void
    494       1.1    dogcow uslsa_close(void *vsc, int portno)
    495       1.1    dogcow {
    496       1.1    dogcow 	struct uslsa_softc *sc;
    497       1.1    dogcow 
    498       1.1    dogcow 	sc = vsc;
    499       1.1    dogcow 
    500      1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
    501      1.17  jakllsch 
    502  1.25.4.1  christos 	if (sc->sc_dying)
    503       1.1    dogcow 		return;
    504       1.1    dogcow 
    505  1.25.4.1  christos 	uslsa_request_set(sc, SLSA_R_IFC_ENABLE, SLSA_RV_IFC_ENABLE_DISABLE);
    506       1.1    dogcow }
    507       1.1    dogcow 
    508       1.1    dogcow static int
    509       1.1    dogcow uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
    510       1.1    dogcow {
    511       1.1    dogcow 	usb_device_request_t req;
    512      1.17  jakllsch 	usbd_status status;
    513       1.1    dogcow 
    514       1.9  jakllsch 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    515       1.1    dogcow 	req.bRequest = request;
    516       1.1    dogcow 	USETW(req.wValue, value);
    517      1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    518       1.1    dogcow 	USETW(req.wLength, 0);
    519       1.1    dogcow 
    520      1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, NULL);
    521      1.17  jakllsch 
    522      1.17  jakllsch 	return uslsa_usbd_errno(status);
    523       1.1    dogcow }
    524       1.1    dogcow 
    525      1.17  jakllsch static int
    526       1.1    dogcow uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
    527       1.1    dogcow {
    528      1.17  jakllsch 	struct slsa_fcs fcs;
    529       1.1    dogcow 	usb_device_request_t req;
    530      1.17  jakllsch 	uint32_t ulControlHandshake;
    531      1.17  jakllsch 	uint32_t ulFlowReplace;
    532      1.17  jakllsch 	usbd_status status;
    533       1.1    dogcow 
    534      1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag));
    535       1.1    dogcow 
    536       1.9  jakllsch 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    537      1.17  jakllsch 	req.bRequest = SLSA_R_GET_FLOW;
    538       1.1    dogcow 	USETW(req.wValue, 0);
    539      1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    540      1.17  jakllsch 	USETW(req.wLength, SLSA_RL_GET_FLOW);
    541      1.17  jakllsch 
    542      1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &fcs);
    543      1.17  jakllsch 	if (status != USBD_NORMAL_COMPLETION) {
    544      1.17  jakllsch 		device_printf(sc->sc_dev, "%s: GET_FLOW %s\n",
    545      1.17  jakllsch 			__func__, usbd_errstr(status));
    546      1.17  jakllsch 		return uslsa_usbd_errno(status);
    547      1.17  jakllsch 	}
    548       1.1    dogcow 
    549      1.17  jakllsch 	ulControlHandshake = le32toh(fcs.ulControlHandshake);
    550      1.17  jakllsch 	ulFlowReplace = le32toh(fcs.ulFlowReplace);
    551       1.1    dogcow 
    552       1.1    dogcow 	if (ISSET(cflag, CRTSCTS)) {
    553      1.17  jakllsch 		ulControlHandshake =
    554      1.17  jakllsch 		    SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK);
    555      1.17  jakllsch 		ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK);
    556       1.1    dogcow 	} else {
    557      1.17  jakllsch 		ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK);
    558      1.17  jakllsch 		ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK);
    559       1.1    dogcow 	}
    560       1.1    dogcow 
    561      1.17  jakllsch 	fcs.ulControlHandshake = htole32(ulControlHandshake);
    562      1.17  jakllsch 	fcs.ulFlowReplace = htole32(ulFlowReplace);
    563      1.17  jakllsch 
    564       1.9  jakllsch 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    565      1.17  jakllsch 	req.bRequest = SLSA_R_SET_FLOW;
    566       1.1    dogcow 	USETW(req.wValue, 0);
    567      1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    568      1.17  jakllsch 	USETW(req.wLength, SLSA_RL_SET_FLOW);
    569      1.17  jakllsch 
    570      1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &fcs);
    571       1.1    dogcow 
    572      1.17  jakllsch 	return uslsa_usbd_errno(status);
    573       1.1    dogcow }
    574