Home | History | Annotate | Line # | Download | only in usb
uslsa.c revision 1.17
      1  1.17  jakllsch /* $NetBSD: uslsa.c,v 1.17 2012/01/14 21:06:01 jakllsch 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.17  jakllsch __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.17 2012/01/14 21:06:01 jakllsch Exp $");
     62   1.2   xtraeme 
     63   1.1    dogcow #include <sys/param.h>
     64   1.1    dogcow #include <sys/systm.h>
     65   1.1    dogcow #include <sys/kernel.h>
     66   1.1    dogcow #include <sys/device.h>
     67   1.1    dogcow #include <sys/conf.h>
     68   1.1    dogcow #include <sys/tty.h>
     69   1.1    dogcow 
     70   1.1    dogcow #include <dev/usb/usb.h>
     71   1.1    dogcow 
     72   1.1    dogcow #include <dev/usb/usbdi.h>
     73   1.1    dogcow #include <dev/usb/usbdi_util.h>
     74   1.1    dogcow #include <dev/usb/usbdevs.h>
     75   1.1    dogcow 
     76   1.1    dogcow #include <dev/usb/ucomvar.h>
     77   1.1    dogcow 
     78  1.17  jakllsch #include <dev/usb/uslsareg.h>
     79  1.17  jakllsch 
     80  1.17  jakllsch #include <fs/unicode.h>
     81   1.1    dogcow 
     82   1.1    dogcow #ifdef USLSA_DEBUG
     83  1.17  jakllsch #define DPRINTF(x)	if (uslsadebug) device_printf x
     84   1.1    dogcow int uslsadebug = 0;
     85   1.1    dogcow #else
     86   1.1    dogcow #define DPRINTF(x)
     87   1.1    dogcow #endif
     88   1.1    dogcow 
     89   1.1    dogcow struct uslsa_softc {
     90  1.12    dyoung 	device_t		sc_dev;		/* base device */
     91  1.17  jakllsch 	device_t		sc_subdev;	/* ucom device */
     92  1.17  jakllsch 	usbd_device_handle	sc_udev;	/* usb device */
     93   1.1    dogcow 	usbd_interface_handle	sc_iface;	/* interface */
     94  1.17  jakllsch 	uint8_t			sc_ifnum;	/* interface number */
     95  1.17  jakllsch 	bool			sc_dying;	/* disconnecting */
     96   1.1    dogcow };
     97   1.1    dogcow 
     98   1.1    dogcow static void uslsa_get_status(void *sc, int, u_char *, u_char *);
     99   1.1    dogcow static void uslsa_set(void *, int, int, int);
    100   1.1    dogcow static int uslsa_param(void *, int, struct termios *);
    101  1.17  jakllsch static int uslsa_ioctl(void *, int, u_long, void *, int, proc_t *);
    102  1.17  jakllsch 
    103   1.1    dogcow static int uslsa_open(void *, int);
    104   1.1    dogcow static void uslsa_close(void *, int);
    105   1.1    dogcow 
    106  1.17  jakllsch static int uslsa_usbd_errno(usbd_status);
    107   1.1    dogcow static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t);
    108  1.17  jakllsch static int uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t);
    109  1.17  jakllsch 
    110  1.17  jakllsch static void uslsa_props(struct uslsa_softc *);
    111   1.1    dogcow 
    112  1.14  jakllsch static const struct ucom_methods uslsa_methods = {
    113   1.1    dogcow 	uslsa_get_status,
    114   1.1    dogcow 	uslsa_set,
    115   1.1    dogcow 	uslsa_param,
    116  1.17  jakllsch 	uslsa_ioctl,
    117   1.1    dogcow 	uslsa_open,
    118   1.1    dogcow 	uslsa_close,
    119   1.1    dogcow 	NULL,
    120   1.1    dogcow 	NULL,
    121   1.1    dogcow };
    122   1.1    dogcow 
    123   1.1    dogcow #define USLSA_CONFIG_INDEX	0
    124   1.1    dogcow #define USLSA_IFACE_INDEX	0
    125   1.1    dogcow #define USLSA_BUFSIZE		256
    126   1.1    dogcow 
    127   1.1    dogcow static const struct usb_devno uslsa_devs[] = {
    128   1.1    dogcow         { USB_VENDOR_BALTECH,           USB_PRODUCT_BALTECH_CARDREADER },
    129   1.1    dogcow         { USB_VENDOR_DYNASTREAM,        USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
    130   1.1    dogcow         { USB_VENDOR_JABLOTRON,         USB_PRODUCT_JABLOTRON_PC60B },
    131   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_ARGUSISP },
    132   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CRUMB128 },
    133   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DEGREECONT },
    134   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_DESKTOPMOBILE },
    135   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_IPLINK1220 },
    136   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_HARP },
    137   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
    138   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_LIPOWSKY_LIN },
    139   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_POLOLU },
    140   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_1 },
    141   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_CP210X_2 },
    142   1.1    dogcow         { USB_VENDOR_SILABS,            USB_PRODUCT_SILABS_SUNNTO },
    143   1.1    dogcow         { USB_VENDOR_SILABS2,           USB_PRODUCT_SILABS2_DCU11CLONE },
    144   1.1    dogcow         { USB_VENDOR_USI,               USB_PRODUCT_USI_MC60 }
    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 static int uslsa_activate(device_t, enum devact);
    152  1.14  jakllsch 
    153   1.8      cube CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
    154   1.5    dyoung     uslsa_attach, uslsa_detach, uslsa_activate, 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.17  jakllsch 	const struct usbif_attach_arg *uaa;
    160  1.17  jakllsch 
    161  1.17  jakllsch 	uaa = aux;
    162   1.1    dogcow 
    163  1.17  jakllsch 	if (usb_lookup(uslsa_devs, uaa->vendor, uaa->product) != NULL) {
    164  1.17  jakllsch 		return UMATCH_VENDOR_PRODUCT;
    165  1.17  jakllsch 	} else {
    166  1.17  jakllsch 		return UMATCH_NONE;
    167  1.17  jakllsch 	}
    168   1.1    dogcow }
    169   1.1    dogcow 
    170  1.17  jakllsch static void
    171  1.12    dyoung uslsa_attach(device_t parent, device_t self, void *aux)
    172   1.1    dogcow {
    173  1.17  jakllsch 	struct uslsa_softc *sc;
    174  1.17  jakllsch 	const struct usbif_attach_arg *uaa;
    175  1.17  jakllsch 	const usb_interface_descriptor_t *id;
    176  1.17  jakllsch 	const usb_endpoint_descriptor_t *ed;
    177   1.1    dogcow 	char *devinfop;
    178   1.1    dogcow 	struct ucom_attach_args uca;
    179   1.1    dogcow 	int i;
    180   1.1    dogcow 
    181  1.17  jakllsch 	sc = device_private(self);
    182  1.17  jakllsch 	uaa = aux;
    183  1.17  jakllsch 
    184   1.8      cube 	sc->sc_dev = self;
    185  1.17  jakllsch 	sc->sc_udev = uaa->device;
    186  1.17  jakllsch 	sc->sc_iface = uaa->iface;
    187   1.1    dogcow 
    188  1.10    plunky 	aprint_naive("\n");
    189  1.10    plunky 	aprint_normal("\n");
    190  1.10    plunky 
    191  1.17  jakllsch 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    192  1.10    plunky 	aprint_normal_dev(self, "%s\n", devinfop);
    193  1.10    plunky 	usbd_devinfo_free(devinfop);
    194  1.10    plunky 
    195  1.17  jakllsch 	id = usbd_get_interface_descriptor(sc->sc_iface);
    196   1.1    dogcow 
    197  1.17  jakllsch 	sc->sc_ifnum = id->bInterfaceNumber;
    198   1.1    dogcow 
    199   1.1    dogcow 	uca.info = "Silicon Labs CP210x";
    200   1.3   nathanw 	uca.portno = UCOM_UNK_PORTNO;
    201   1.1    dogcow 	uca.ibufsize = USLSA_BUFSIZE;
    202   1.1    dogcow 	uca.obufsize = USLSA_BUFSIZE;
    203   1.1    dogcow 	uca.ibufsizepad = USLSA_BUFSIZE;
    204   1.1    dogcow 	uca.opkthdrlen = 0;
    205  1.17  jakllsch 	uca.device = sc->sc_udev;
    206  1.17  jakllsch 	uca.iface = sc->sc_iface;
    207   1.1    dogcow 	uca.methods = &uslsa_methods;
    208   1.1    dogcow 	uca.arg = sc;
    209   1.1    dogcow 
    210   1.1    dogcow 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    211  1.12    dyoung 	                   sc->sc_dev);
    212   1.1    dogcow 
    213   1.1    dogcow 	uca.bulkin = uca.bulkout = -1;
    214   1.1    dogcow 	for (i = 0; i < id->bNumEndpoints; i++) {
    215   1.1    dogcow 		int addr, dir, attr;
    216   1.1    dogcow 
    217  1.17  jakllsch 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    218   1.1    dogcow 		if (ed == NULL) {
    219   1.8      cube 			aprint_error_dev(self,
    220  1.17  jakllsch 			    "could not read endpoint descriptor\n");
    221  1.17  jakllsch 			sc->sc_dying = true;
    222  1.17  jakllsch 			return;
    223   1.1    dogcow 		}
    224   1.1    dogcow 		addr = ed->bEndpointAddress;
    225   1.1    dogcow 		dir = UE_GET_DIR(ed->bEndpointAddress);
    226   1.1    dogcow 		attr = ed->bmAttributes & UE_XFERTYPE;
    227  1.17  jakllsch 		if (dir == UE_DIR_IN && attr == UE_BULK) {
    228   1.1    dogcow 			uca.bulkin = addr;
    229  1.17  jakllsch 		} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
    230   1.1    dogcow 			uca.bulkout = addr;
    231  1.17  jakllsch 		} else {
    232   1.8      cube 			aprint_error_dev(self, "unexpected endpoint\n");
    233  1.17  jakllsch 		}
    234   1.1    dogcow 	}
    235  1.17  jakllsch 	aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n",
    236  1.17  jakllsch 		uca.bulkin, uca.bulkout);
    237  1.17  jakllsch 	if ((uca.bulkin == -1) || (uca.bulkout == -1)) {
    238  1.17  jakllsch 		aprint_error_dev(self, "could not find endpoints\n");
    239  1.17  jakllsch 		sc->sc_dying = true;
    240  1.17  jakllsch 		return;
    241   1.1    dogcow 	}
    242   1.1    dogcow 
    243   1.1    dogcow 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    244   1.1    dogcow 	                                    ucomprint, ucomsubmatch);
    245   1.1    dogcow 
    246  1.12    dyoung 	return;
    247   1.1    dogcow }
    248   1.1    dogcow 
    249  1.14  jakllsch static int
    250   1.5    dyoung uslsa_activate(device_t self, enum devact act)
    251   1.1    dogcow {
    252   1.5    dyoung 	struct uslsa_softc *sc = device_private(self);
    253   1.1    dogcow 
    254   1.1    dogcow 	switch (act) {
    255   1.1    dogcow 	case DVACT_DEACTIVATE:
    256  1.17  jakllsch 		sc->sc_dying = true;
    257  1.11    dyoung 		return 0;
    258  1.11    dyoung 	default:
    259  1.11    dyoung 		return EOPNOTSUPP;
    260   1.1    dogcow 	}
    261   1.1    dogcow }
    262   1.1    dogcow 
    263  1.14  jakllsch static void
    264   1.5    dyoung uslsa_childdet(device_t self, device_t child)
    265   1.5    dyoung {
    266   1.5    dyoung 	struct uslsa_softc *sc = device_private(self);
    267   1.5    dyoung 
    268   1.5    dyoung 	KASSERT(sc->sc_subdev == child);
    269   1.5    dyoung 	sc->sc_subdev = NULL;
    270   1.5    dyoung }
    271   1.5    dyoung 
    272  1.17  jakllsch static int
    273  1.12    dyoung uslsa_detach(device_t self, int flags)
    274   1.1    dogcow {
    275  1.12    dyoung 	struct uslsa_softc *sc = device_private(self);
    276   1.1    dogcow 	int rv = 0;
    277   1.1    dogcow 
    278  1.17  jakllsch 	DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags));
    279   1.1    dogcow 
    280  1.17  jakllsch 	sc->sc_dying = true;
    281   1.1    dogcow 
    282  1.17  jakllsch 	if (sc->sc_subdev != NULL) {
    283   1.1    dogcow 		rv = config_detach(sc->sc_subdev, flags);
    284  1.17  jakllsch 	}
    285   1.1    dogcow 
    286   1.1    dogcow 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    287  1.12    dyoung 	                   sc->sc_dev);
    288   1.1    dogcow 
    289   1.1    dogcow 	return (rv);
    290   1.1    dogcow }
    291   1.1    dogcow 
    292  1.17  jakllsch static int
    293  1.17  jakllsch uslsa_usbd_errno(usbd_status status)
    294  1.17  jakllsch {
    295  1.17  jakllsch 	switch (status) {
    296  1.17  jakllsch 	case USBD_NORMAL_COMPLETION:
    297  1.17  jakllsch 		return 0;
    298  1.17  jakllsch 	case USBD_STALLED:
    299  1.17  jakllsch 		return EINVAL;
    300  1.17  jakllsch 	default:
    301  1.17  jakllsch 		return EIO;
    302  1.17  jakllsch 	}
    303  1.17  jakllsch }
    304  1.17  jakllsch 
    305   1.1    dogcow static void
    306   1.1    dogcow uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    307   1.1    dogcow {
    308   1.1    dogcow 	struct uslsa_softc *sc;
    309   1.1    dogcow 	usb_device_request_t req;
    310  1.17  jakllsch 	usbd_status status;
    311  1.17  jakllsch 	uint8_t mdmsts;
    312   1.1    dogcow 
    313   1.1    dogcow 	sc = vsc;
    314   1.1    dogcow 
    315  1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno));
    316  1.17  jakllsch 
    317  1.17  jakllsch 	if (sc->sc_dying) {
    318  1.17  jakllsch 		return;
    319  1.17  jakllsch 	}
    320   1.1    dogcow 
    321   1.9  jakllsch 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    322  1.17  jakllsch 	req.bRequest = SLSA_R_GET_MDMSTS;
    323   1.1    dogcow 	USETW(req.wValue, 0);
    324  1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    325  1.17  jakllsch 	USETW(req.wLength, SLSA_RL_GET_MDMSTS);
    326  1.17  jakllsch 
    327  1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &mdmsts);
    328  1.17  jakllsch 	if (status != USBD_NORMAL_COMPLETION) {
    329  1.17  jakllsch 		device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n",
    330  1.17  jakllsch 		    __func__, usbd_errstr(status));
    331  1.17  jakllsch 		return;
    332  1.17  jakllsch 	}
    333  1.17  jakllsch 
    334  1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts));
    335  1.17  jakllsch 
    336  1.17  jakllsch 	if (lsr != NULL) {
    337  1.17  jakllsch 		*lsr = 0;
    338  1.17  jakllsch 	}
    339   1.1    dogcow 
    340  1.17  jakllsch 	if (msr != NULL) {
    341  1.17  jakllsch 		*msr = 0;
    342  1.17  jakllsch 		*msr |= ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0;
    343  1.17  jakllsch 		*msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0;
    344  1.17  jakllsch 		*msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0;
    345  1.17  jakllsch 		*msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0;
    346  1.17  jakllsch 	}
    347   1.1    dogcow }
    348   1.1    dogcow 
    349   1.1    dogcow static void
    350   1.1    dogcow uslsa_set(void *vsc, int portno, int reg, int onoff)
    351   1.1    dogcow {
    352   1.1    dogcow 	struct uslsa_softc *sc;
    353   1.1    dogcow 
    354   1.1    dogcow 	sc = vsc;
    355   1.1    dogcow 
    356  1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno, reg, onoff));
    357  1.17  jakllsch 
    358  1.17  jakllsch 	if (sc->sc_dying) {
    359  1.17  jakllsch 		return;
    360  1.17  jakllsch 	}
    361   1.1    dogcow 
    362   1.1    dogcow 	switch (reg) {
    363   1.1    dogcow 	case UCOM_SET_DTR:
    364  1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_MHS,
    365  1.17  jakllsch 		    SLSA_RV_SET_MHS_DTR_MASK |
    366  1.17  jakllsch 		    (onoff ? SLSA_RV_SET_MHS_DTR : 0))) {
    367  1.17  jakllsch 			device_printf(sc->sc_dev, "SET_MHS/DTR failed\n");
    368  1.17  jakllsch 		}
    369   1.1    dogcow 		break;
    370   1.1    dogcow 	case UCOM_SET_RTS:
    371  1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_MHS,
    372  1.17  jakllsch 		    SLSA_RV_SET_MHS_RTS_MASK |
    373  1.17  jakllsch 		    (onoff ? SLSA_RV_SET_MHS_RTS : 0))) {
    374  1.17  jakllsch 			device_printf(sc->sc_dev, "SET_MHS/RTS failed\n");
    375  1.17  jakllsch 		}
    376   1.1    dogcow 		break;
    377   1.1    dogcow 	case UCOM_SET_BREAK:
    378  1.17  jakllsch 		if (uslsa_request_set(sc, SLSA_R_SET_BREAK,
    379  1.17  jakllsch 		    (onoff ? SLSA_RV_SET_BREAK_ENABLE :
    380  1.17  jakllsch 		     SLSA_RV_SET_BREAK_DISABLE))) {
    381  1.17  jakllsch 			device_printf(sc->sc_dev, "SET_BREAK failed\n");
    382  1.17  jakllsch 		}
    383   1.1    dogcow 		break;
    384   1.1    dogcow 	default:
    385   1.1    dogcow 		break;
    386   1.1    dogcow 	}
    387   1.1    dogcow }
    388   1.1    dogcow 
    389   1.1    dogcow static int
    390  1.17  jakllsch uslsa_param(void *vsc, int portno, struct termios *t)
    391   1.1    dogcow {
    392   1.1    dogcow 	struct uslsa_softc *sc;
    393  1.17  jakllsch 	int ret;
    394  1.17  jakllsch 	uint16_t value;
    395   1.1    dogcow 
    396   1.1    dogcow 	sc = vsc;
    397   1.1    dogcow 
    398  1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t));
    399  1.17  jakllsch 
    400  1.17  jakllsch 	if (sc->sc_dying) {
    401  1.17  jakllsch 		return EIO;
    402  1.17  jakllsch 	}
    403  1.17  jakllsch 
    404  1.17  jakllsch 	value = SLSA_RV_BAUDDIV(t->c_ospeed);
    405  1.17  jakllsch 
    406  1.17  jakllsch 	if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value)) != 0) {
    407  1.17  jakllsch 		device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n",
    408  1.17  jakllsch 		       __func__);
    409  1.17  jakllsch 		return ret;
    410  1.17  jakllsch 	}
    411  1.17  jakllsch 
    412  1.17  jakllsch 	value = 0;
    413   1.1    dogcow 
    414  1.17  jakllsch 	if (ISSET(t->c_cflag, CSTOPB)) {
    415  1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_STOP_2;
    416  1.17  jakllsch 	} else {
    417  1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_STOP_1;
    418  1.17  jakllsch 	}
    419   1.1    dogcow 
    420   1.1    dogcow 	if (ISSET(t->c_cflag, PARENB)) {
    421  1.17  jakllsch 		if (ISSET(t->c_cflag, PARODD)) {
    422  1.17  jakllsch 			value |= SLSA_RV_LINE_CTL_PARITY_ODD;
    423  1.17  jakllsch 		} else {
    424  1.17  jakllsch 			value |= SLSA_RV_LINE_CTL_PARITY_EVEN;
    425  1.17  jakllsch 		}
    426  1.17  jakllsch 	} else {
    427  1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_PARITY_NONE;
    428  1.17  jakllsch 	}
    429   1.1    dogcow 
    430   1.1    dogcow 	switch (ISSET(t->c_cflag, CSIZE)) {
    431   1.1    dogcow 	case CS5:
    432  1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_5;
    433   1.1    dogcow 		break;
    434   1.1    dogcow 	case CS6:
    435  1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_6;
    436   1.1    dogcow 		break;
    437   1.1    dogcow 	case CS7:
    438  1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_7;
    439   1.1    dogcow 		break;
    440   1.1    dogcow 	case CS8:
    441  1.17  jakllsch 		value |= SLSA_RV_LINE_CTL_LEN_8;
    442   1.1    dogcow 		break;
    443   1.1    dogcow 	}
    444   1.1    dogcow 
    445  1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to 0x%x\n",
    446  1.17  jakllsch 	    __func__, value));
    447  1.17  jakllsch 	if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) {
    448  1.17  jakllsch 		device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
    449  1.17  jakllsch 		return ret;
    450  1.17  jakllsch 	}
    451  1.17  jakllsch 
    452  1.17  jakllsch 	if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) {
    453  1.17  jakllsch 		device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
    454  1.17  jakllsch 	}
    455  1.17  jakllsch 
    456  1.17  jakllsch 	return ret;
    457  1.17  jakllsch }
    458  1.17  jakllsch 
    459  1.17  jakllsch static int
    460  1.17  jakllsch uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p)
    461  1.17  jakllsch {
    462  1.17  jakllsch 	struct uslsa_softc *sc;
    463   1.1    dogcow 
    464  1.17  jakllsch 	sc = vsc;
    465  1.17  jakllsch 
    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.17  jakllsch 	if (sc->sc_dying) {
    487  1.17  jakllsch 		return EIO;
    488  1.17  jakllsch 	}
    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.17  jakllsch 	if (sc->sc_dying) {
    504   1.1    dogcow 		return;
    505  1.17  jakllsch 	}
    506   1.1    dogcow 
    507  1.17  jakllsch 	(void)uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
    508  1.17  jakllsch 	    SLSA_RV_IFC_ENABLE_DISABLE);
    509   1.1    dogcow }
    510   1.1    dogcow 
    511   1.1    dogcow static int
    512   1.1    dogcow uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
    513   1.1    dogcow {
    514   1.1    dogcow 	usb_device_request_t req;
    515  1.17  jakllsch 	usbd_status status;
    516   1.1    dogcow 
    517   1.9  jakllsch 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    518   1.1    dogcow 	req.bRequest = request;
    519   1.1    dogcow 	USETW(req.wValue, value);
    520  1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    521   1.1    dogcow 	USETW(req.wLength, 0);
    522   1.1    dogcow 
    523  1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, NULL);
    524  1.17  jakllsch 
    525  1.17  jakllsch 	return uslsa_usbd_errno(status);
    526   1.1    dogcow }
    527   1.1    dogcow 
    528  1.17  jakllsch static int
    529   1.1    dogcow uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
    530   1.1    dogcow {
    531  1.17  jakllsch 	struct slsa_fcs fcs;
    532   1.1    dogcow 	usb_device_request_t req;
    533  1.17  jakllsch 	uint32_t ulControlHandshake;
    534  1.17  jakllsch 	uint32_t ulFlowReplace;
    535  1.17  jakllsch 	usbd_status status;
    536   1.1    dogcow 
    537  1.17  jakllsch 	DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag));
    538   1.1    dogcow 
    539   1.9  jakllsch 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    540  1.17  jakllsch 	req.bRequest = SLSA_R_GET_FLOW;
    541   1.1    dogcow 	USETW(req.wValue, 0);
    542  1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    543  1.17  jakllsch 	USETW(req.wLength, SLSA_RL_GET_FLOW);
    544  1.17  jakllsch 
    545  1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &fcs);
    546  1.17  jakllsch 	if (status != USBD_NORMAL_COMPLETION) {
    547  1.17  jakllsch 		device_printf(sc->sc_dev, "%s: GET_FLOW %s\n",
    548  1.17  jakllsch 			__func__, usbd_errstr(status));
    549  1.17  jakllsch 		return uslsa_usbd_errno(status);
    550  1.17  jakllsch 	}
    551   1.1    dogcow 
    552  1.17  jakllsch 	ulControlHandshake = le32toh(fcs.ulControlHandshake);
    553  1.17  jakllsch 	ulFlowReplace = le32toh(fcs.ulFlowReplace);
    554   1.1    dogcow 
    555   1.1    dogcow 	if (ISSET(cflag, CRTSCTS)) {
    556  1.17  jakllsch 		ulControlHandshake =
    557  1.17  jakllsch 		    SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK);
    558  1.17  jakllsch 		ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK);
    559   1.1    dogcow 	} else {
    560  1.17  jakllsch 		ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK);
    561  1.17  jakllsch 		ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK);
    562   1.1    dogcow 	}
    563   1.1    dogcow 
    564  1.17  jakllsch 	fcs.ulControlHandshake = htole32(ulControlHandshake);
    565  1.17  jakllsch 	fcs.ulFlowReplace = htole32(ulFlowReplace);
    566  1.17  jakllsch 
    567   1.9  jakllsch 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
    568  1.17  jakllsch 	req.bRequest = SLSA_R_SET_FLOW;
    569   1.1    dogcow 	USETW(req.wValue, 0);
    570  1.17  jakllsch 	USETW(req.wIndex, sc->sc_ifnum);
    571  1.17  jakllsch 	USETW(req.wLength, SLSA_RL_SET_FLOW);
    572  1.17  jakllsch 
    573  1.17  jakllsch 	status = usbd_do_request(sc->sc_udev, &req, &fcs);
    574   1.1    dogcow 
    575  1.17  jakllsch 	return uslsa_usbd_errno(status);
    576   1.1    dogcow }
    577