Home | History | Annotate | Line # | Download | only in usb
umcs.c revision 1.7.6.1.4.1
      1  1.7.6.1.4.1     skrll /* $NetBSD: umcs.c,v 1.7.6.1.4.1 2016/09/06 20:33:09 skrll Exp $ */
      2          1.1    martin /* $FreeBSD: head/sys/dev/usb/serial/umcs.c 260559 2014-01-12 11:44:28Z hselasky $ */
      3          1.1    martin 
      4          1.1    martin /*-
      5          1.1    martin  * Copyright (c) 2010 Lev Serebryakov <lev (at) FreeBSD.org>.
      6          1.1    martin  * All rights reserved.
      7          1.1    martin  *
      8          1.1    martin  * Redistribution and use in source and binary forms, with or without
      9          1.1    martin  * modification, are permitted provided that the following conditions
     10          1.1    martin  * are met:
     11          1.1    martin  * 1. Redistributions of source code must retain the above copyright
     12          1.1    martin  *    notice, this list of conditions and the following disclaimer.
     13          1.1    martin  * 2. Redistributions in binary form must reproduce the above copyright
     14          1.1    martin  *    notice, this list of conditions and the following disclaimer in the
     15          1.1    martin  *    documentation and/or other materials provided with the distribution.
     16          1.1    martin  *
     17          1.1    martin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18          1.1    martin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19          1.1    martin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20          1.1    martin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21          1.1    martin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22          1.1    martin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23          1.1    martin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24          1.1    martin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25          1.1    martin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26          1.1    martin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27          1.1    martin  * SUCH DAMAGE.
     28          1.1    martin  */
     29          1.1    martin 
     30          1.1    martin /*
     31          1.1    martin  * This driver supports several multiport USB-to-RS232 serial adapters driven
     32          1.1    martin  * by MosChip mos7820 and mos7840, bridge chips.
     33          1.1    martin  * The adapters are sold under many different brand names.
     34          1.1    martin  *
     35          1.1    martin  * Datasheets are available at MosChip www site at
     36          1.1    martin  * http://www.moschip.com.  The datasheets don't contain full
     37          1.1    martin  * programming information for the chip.
     38          1.1    martin  *
     39          1.1    martin  * It is nornal to have only two enabled ports in devices, based on
     40          1.1    martin  * quad-port mos7840.
     41          1.1    martin  *
     42          1.1    martin  */
     43          1.1    martin #include <sys/cdefs.h>
     44  1.7.6.1.4.1     skrll __KERNEL_RCSID(0, "$NetBSD: umcs.c,v 1.7.6.1.4.1 2016/09/06 20:33:09 skrll Exp $");
     45          1.1    martin 
     46          1.1    martin #include <sys/param.h>
     47          1.1    martin #include <sys/systm.h>
     48          1.1    martin #include <sys/atomic.h>
     49          1.1    martin #include <sys/kernel.h>
     50          1.1    martin #include <sys/conf.h>
     51          1.1    martin #include <sys/tty.h>
     52          1.1    martin #include <sys/device.h>
     53          1.3    martin #include <sys/kmem.h>
     54          1.1    martin 
     55          1.1    martin #include <dev/usb/usb.h>
     56          1.1    martin #include <dev/usb/usbdi.h>
     57          1.1    martin #include <dev/usb/usbdi_util.h>
     58          1.1    martin #include <dev/usb/usbdevs.h>
     59          1.1    martin 
     60          1.1    martin #include <dev/usb/usbdevs.h>
     61          1.1    martin #include <dev/usb/ucomvar.h>
     62          1.1    martin 
     63          1.1    martin #include "umcs.h"
     64          1.1    martin 
     65          1.1    martin #if 0
     66          1.1    martin #define	DPRINTF(ARG)	printf ARG
     67          1.1    martin #else
     68          1.1    martin #define	DPRINTF(ARG)
     69          1.1    martin #endif
     70          1.1    martin 
     71          1.1    martin /*
     72          1.1    martin  * Two-port devices (both with 7820 chip and 7840 chip configured as two-port)
     73          1.1    martin  * have ports 0 and 2, with ports 1 and 3 omitted.
     74          1.1    martin  * So, PHYSICAL port numbers on two-port device will be 0 and 2.
     75          1.1    martin  *
     76          1.1    martin  * We use an array of the following struct, indexed by ucom port index,
     77          1.1    martin  * and include the physical port number in it.
     78          1.1    martin  */
     79          1.1    martin struct umcs7840_softc_oneport {
     80          1.1    martin 	device_t sc_port_ucom;		/* ucom subdevice */
     81          1.1    martin 	unsigned int sc_port_phys;	/* physical port number */
     82          1.1    martin 	uint8_t	sc_port_lcr;		/* local line control register */
     83          1.1    martin 	uint8_t	sc_port_mcr;		/* local modem control register */
     84          1.1    martin };
     85          1.1    martin 
     86          1.1    martin struct umcs7840_softc {
     87          1.1    martin 	device_t sc_dev;		/* ourself */
     88  1.7.6.1.4.1     skrll 	struct usbd_interface *sc_iface; /* the usb interface */
     89  1.7.6.1.4.1     skrll 	struct usbd_device *sc_udev;	/* the usb device */
     90  1.7.6.1.4.1     skrll 	struct usbd_pipe *sc_intr_pipe;	/* interrupt pipe */
     91          1.1    martin 	uint8_t *sc_intr_buf;		/* buffer for interrupt xfer */
     92          1.3    martin 	unsigned int sc_intr_buflen;	/* size of buffer */
     93          1.5    martin 	struct usb_task sc_change_task;	/* async status changes */
     94          1.6  riastrad 	volatile uint32_t sc_change_mask;	/* mask of port changes */
     95          1.1    martin 	struct umcs7840_softc_oneport sc_ports[UMCS7840_MAX_PORTS];
     96          1.1    martin 					/* data for each port */
     97          1.1    martin 	uint8_t	sc_numports;		/* number of ports (subunits) */
     98          1.1    martin 	bool sc_init_done;		/* special one time init in open */
     99          1.1    martin 	bool sc_dying;			/* we have been deactivated */
    100          1.1    martin };
    101          1.1    martin 
    102  1.7.6.1.4.1     skrll static int umcs7840_get_reg(struct umcs7840_softc *, uint8_t, uint8_t *);
    103  1.7.6.1.4.1     skrll static int umcs7840_set_reg(struct umcs7840_softc *, uint8_t, uint8_t);
    104  1.7.6.1.4.1     skrll static int umcs7840_get_UART_reg(struct umcs7840_softc *, uint8_t, uint8_t, uint8_t *);
    105  1.7.6.1.4.1     skrll static int umcs7840_set_UART_reg(struct umcs7840_softc *, uint8_t, uint8_t, uint8_t );
    106  1.7.6.1.4.1     skrll static int umcs7840_calc_baudrate(uint32_t, uint16_t *, uint8_t *);
    107  1.7.6.1.4.1     skrll static void umcs7840_dtr(struct umcs7840_softc *, int, bool);
    108  1.7.6.1.4.1     skrll static void umcs7840_rts(struct umcs7840_softc *, int, bool);
    109  1.7.6.1.4.1     skrll static void umcs7840_break(struct umcs7840_softc *, int, bool );
    110          1.1    martin 
    111          1.1    martin static int umcs7840_match(device_t, cfdata_t, void *);
    112          1.1    martin static void umcs7840_attach(device_t, device_t, void *);
    113          1.1    martin static int umcs7840_detach(device_t, int);
    114  1.7.6.1.4.1     skrll static void umcs7840_intr(struct usbd_xfer *, void *, usbd_status);
    115          1.5    martin static void umcs7840_change_task(void *arg);
    116  1.7.6.1.4.1     skrll static int umcs7840_activate(device_t, enum devact);
    117          1.1    martin static void umcs7840_childdet(device_t, device_t);
    118          1.1    martin 
    119          1.1    martin static void umcs7840_get_status(void *, int, u_char *, u_char *);
    120          1.1    martin static void umcs7840_set(void *, int, int, int);
    121          1.1    martin static int umcs7840_param(void *, int, struct termios *);
    122  1.7.6.1.4.1     skrll static int umcs7840_port_open(void *, int);
    123  1.7.6.1.4.1     skrll static void umcs7840_port_close(void *, int);
    124          1.1    martin 
    125          1.1    martin struct ucom_methods umcs7840_methods = {
    126          1.2    martin 	.ucom_get_status = umcs7840_get_status,
    127          1.2    martin 	.ucom_set = umcs7840_set,
    128          1.2    martin 	.ucom_param = umcs7840_param,
    129          1.2    martin 	.ucom_open = umcs7840_port_open,
    130          1.2    martin 	.ucom_close = umcs7840_port_close,
    131          1.1    martin };
    132          1.1    martin 
    133          1.1    martin static const struct usb_devno umcs7840_devs[] = {
    134          1.1    martin 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7703 },
    135          1.1    martin 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7810 },
    136          1.1    martin 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7820 },
    137          1.1    martin 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7840 },
    138          1.1    martin 	{ USB_VENDOR_ATEN,		USB_PRODUCT_ATEN_UC2324 }
    139          1.1    martin };
    140          1.1    martin #define umcs7840_lookup(v, p) usb_lookup(umcs7840_devs, v, p)
    141          1.1    martin 
    142          1.1    martin CFATTACH_DECL2_NEW(umcs, sizeof(struct umcs7840_softc), umcs7840_match,
    143          1.1    martin     umcs7840_attach, umcs7840_detach, umcs7840_activate, NULL,
    144          1.1    martin     umcs7840_childdet);
    145          1.1    martin 
    146          1.1    martin static inline int
    147          1.1    martin umcs7840_reg_sp(int phyport)
    148          1.1    martin {
    149          1.1    martin 	KASSERT(phyport >= 0 && phyport < 4);
    150          1.1    martin 	switch (phyport) {
    151          1.1    martin 	default:
    152          1.1    martin 	case 0:	return MCS7840_DEV_REG_SP1;
    153          1.1    martin 	case 1:	return MCS7840_DEV_REG_SP2;
    154          1.1    martin 	case 2:	return MCS7840_DEV_REG_SP3;
    155          1.1    martin 	case 3:	return MCS7840_DEV_REG_SP4;
    156          1.1    martin 	}
    157          1.1    martin }
    158          1.1    martin 
    159          1.1    martin static inline int
    160          1.1    martin umcs7840_reg_ctrl(int phyport)
    161          1.1    martin {
    162          1.1    martin 	KASSERT(phyport >= 0 && phyport < 4);
    163          1.1    martin 	switch (phyport) {
    164          1.1    martin 	default:
    165          1.1    martin 	case 0:	return MCS7840_DEV_REG_CONTROL1;
    166          1.1    martin 	case 1:	return MCS7840_DEV_REG_CONTROL2;
    167          1.1    martin 	case 2:	return MCS7840_DEV_REG_CONTROL3;
    168          1.1    martin 	case 3:	return MCS7840_DEV_REG_CONTROL4;
    169          1.1    martin 	}
    170          1.1    martin }
    171          1.1    martin 
    172          1.1    martin static int
    173          1.1    martin umcs7840_match(device_t dev, cfdata_t match, void *aux)
    174          1.1    martin {
    175          1.1    martin 	struct usb_attach_arg *uaa = aux;
    176          1.1    martin 
    177  1.7.6.1.4.1     skrll 	return umcs7840_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    178  1.7.6.1.4.1     skrll 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    179          1.1    martin }
    180          1.1    martin 
    181          1.1    martin static void
    182  1.7.6.1.4.1     skrll umcs7840_attach(device_t parent, device_t self, void *aux)
    183          1.1    martin {
    184          1.1    martin 	struct umcs7840_softc *sc = device_private(self);
    185          1.1    martin 	struct usb_attach_arg *uaa = aux;
    186  1.7.6.1.4.1     skrll 	struct usbd_device *dev = uaa->uaa_device;
    187          1.1    martin 	usb_interface_descriptor_t *id;
    188          1.1    martin 	usb_endpoint_descriptor_t *ed;
    189          1.1    martin 	char *devinfop;
    190  1.7.6.1.4.1     skrll 	struct ucom_attach_args ucaa;
    191          1.1    martin 	int error, i, intr_addr;
    192          1.1    martin 	uint8_t data;
    193          1.1    martin 
    194          1.1    martin 	sc->sc_dev = self;
    195  1.7.6.1.4.1     skrll 	sc->sc_udev = uaa->uaa_device;
    196          1.1    martin 
    197          1.1    martin 	if (usbd_set_config_index(sc->sc_udev, MCS7840_CONFIG_INDEX, 1) != 0) {
    198          1.1    martin 		aprint_error(": could not set configuration no\n");
    199          1.1    martin 		return;
    200          1.1    martin 	}
    201          1.1    martin 
    202          1.1    martin 	/* get the first interface handle */
    203          1.1    martin 	error = usbd_device2interface_handle(sc->sc_udev, MCS7840_IFACE_INDEX,
    204          1.1    martin 	    &sc->sc_iface);
    205          1.1    martin 	if (error != 0) {
    206          1.1    martin 		aprint_error(": could not get interface handle\n");
    207          1.1    martin 		return;
    208          1.1    martin 	}
    209          1.1    martin 
    210          1.1    martin 	/*
    211          1.1    martin 	 * Get number of ports
    212          1.1    martin 	 * Documentation (full datasheet) says, that number of ports is
    213          1.1    martin 	 * set as MCS7840_DEV_MODE_SELECT24S bit in MODE R/Only
    214          1.1    martin 	 * register. But vendor driver uses these undocumented
    215          1.1    martin 	 * register & bit.
    216          1.1    martin 	 *
    217          1.1    martin 	 * Experiments show, that MODE register can have `0'
    218          1.1    martin 	 * (4 ports) bit on 2-port device, so use vendor driver's way.
    219          1.1    martin 	 *
    220          1.1    martin 	 * Also, see notes in header file for these constants.
    221          1.1    martin 	 */
    222          1.1    martin 	umcs7840_get_reg(sc, MCS7840_DEV_REG_GPIO, &data);
    223          1.1    martin 	if (data & MCS7840_DEV_GPIO_4PORTS) {
    224          1.1    martin 		sc->sc_numports = 4;
    225          1.1    martin 		/* physical port no are : 0, 1, 2, 3 */
    226          1.1    martin 	} else {
    227  1.7.6.1.4.1     skrll 		if (uaa->uaa_product == USB_PRODUCT_MOSCHIP_MCS7810)
    228          1.1    martin 			sc->sc_numports = 1;
    229          1.1    martin 		else {
    230          1.1    martin 			sc->sc_numports = 2;
    231          1.1    martin 			/* physical port no are : 0 and 2 */
    232          1.1    martin 		}
    233          1.1    martin 	}
    234          1.1    martin 	devinfop = usbd_devinfo_alloc(dev, 0);
    235          1.1    martin 	aprint_normal(": %s\n", devinfop);
    236          1.1    martin 	usbd_devinfo_free(devinfop);
    237          1.1    martin 	aprint_verbose_dev(self, "found %d active ports\n", sc->sc_numports);
    238          1.1    martin 
    239          1.1    martin 	if (!umcs7840_get_reg(sc, MCS7840_DEV_REG_MODE, &data)) {
    240          1.1    martin 		aprint_verbose_dev(self, "On-die confguration: RST: active %s, "
    241          1.1    martin 		    "HRD: %s, PLL: %s, POR: %s, Ports: %s, EEPROM write %s, "
    242          1.1    martin 		    "IrDA is %savailable\n",
    243          1.1    martin 		    (data & MCS7840_DEV_MODE_RESET) ? "low" : "high",
    244          1.1    martin 		    (data & MCS7840_DEV_MODE_SER_PRSNT) ? "yes" : "no",
    245          1.1    martin 		    (data & MCS7840_DEV_MODE_PLLBYPASS) ? "bypassed" : "avail",
    246          1.1    martin 		    (data & MCS7840_DEV_MODE_PORBYPASS) ? "bypassed" : "avail",
    247          1.1    martin 		    (data & MCS7840_DEV_MODE_SELECT24S) ? "2" : "4",
    248          1.1    martin 		    (data & MCS7840_DEV_MODE_EEPROMWR) ? "enabled" : "disabled",
    249          1.1    martin 		    (data & MCS7840_DEV_MODE_IRDA) ? "" : "not ");
    250          1.1    martin 	}
    251          1.1    martin 
    252          1.1    martin 	/*
    253          1.1    martin 	 * Set up the interrupt pipe
    254          1.1    martin 	 */
    255          1.1    martin 	id = usbd_get_interface_descriptor(sc->sc_iface);
    256          1.1    martin 	intr_addr = -1;
    257          1.1    martin 	for (i = 0 ; i < id->bNumEndpoints ; i++) {
    258          1.1    martin 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    259          1.1    martin 		if (ed == NULL) continue;
    260          1.1    martin 		if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN
    261          1.1    martin 		    || UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT)
    262          1.1    martin 			continue;
    263          1.3    martin 		sc->sc_intr_buflen = UGETW(ed->wMaxPacketSize);
    264          1.1    martin 		intr_addr = ed->bEndpointAddress;
    265          1.1    martin 		break;
    266          1.1    martin 	}
    267          1.1    martin 	if (intr_addr < 0) {
    268          1.1    martin 		aprint_error_dev(self, "interrupt pipe not found\n");
    269          1.1    martin 		return;
    270          1.1    martin 	}
    271          1.3    martin 	sc->sc_intr_buf = kmem_alloc(sc->sc_intr_buflen, KM_SLEEP);
    272          1.1    martin 
    273          1.1    martin 	error = usbd_open_pipe_intr(sc->sc_iface, intr_addr,
    274          1.1    martin 		    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
    275          1.3    martin 		    sc->sc_intr_buflen, umcs7840_intr, 100);
    276          1.1    martin 	if (error) {
    277          1.1    martin 		aprint_error_dev(self, "cannot open interrupt pipe "
    278          1.1    martin 		    "(addr %d)\n", intr_addr);
    279          1.1    martin 		return;
    280          1.1    martin 	}
    281          1.1    martin 
    282          1.5    martin 	usb_init_task(&sc->sc_change_task, umcs7840_change_task, sc,
    283          1.5    martin 	    USB_TASKQ_MPSAFE);
    284          1.1    martin 
    285  1.7.6.1.4.1     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    286          1.1    martin 
    287  1.7.6.1.4.1     skrll 	memset(&ucaa, 0, sizeof(ucaa));
    288  1.7.6.1.4.1     skrll 	ucaa.ucaa_ibufsize = 256;
    289  1.7.6.1.4.1     skrll 	ucaa.ucaa_obufsize = 256;
    290  1.7.6.1.4.1     skrll 	ucaa.ucaa_ibufsizepad = 256;
    291  1.7.6.1.4.1     skrll 	ucaa.ucaa_opkthdrlen = 0;
    292  1.7.6.1.4.1     skrll 	ucaa.ucaa_device = sc->sc_udev;
    293  1.7.6.1.4.1     skrll 	ucaa.ucaa_iface = sc->sc_iface;
    294  1.7.6.1.4.1     skrll 	ucaa.ucaa_methods = &umcs7840_methods;
    295  1.7.6.1.4.1     skrll 	ucaa.ucaa_arg = sc;
    296          1.1    martin 
    297          1.1    martin 	for (i = 0; i < sc->sc_numports; i++) {
    298  1.7.6.1.4.1     skrll 		ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    299          1.1    martin 
    300          1.1    martin 		/*
    301          1.1    martin 		 * On four port cards, endpoints are 0/1 for first,
    302          1.1    martin 		 * 2/3 for second, ...
    303          1.1    martin 		 * On two port cards, they are 0/1 for first, 4/5 for second.
    304          1.1    martin 		 * On single port, just 0/1 will be used.
    305          1.1    martin 		 */
    306          1.1    martin 		int phyport = i * (sc->sc_numports == 2 ? 2 : 1);
    307          1.1    martin 
    308          1.1    martin 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
    309          1.1    martin 			phyport*2);
    310          1.1    martin 		if (ed == NULL) {
    311          1.1    martin 			aprint_error_dev(self,
    312          1.1    martin 			    "no bulk in endpoint found for %d\n", i);
    313          1.1    martin 			return;
    314          1.1    martin 		}
    315  1.7.6.1.4.1     skrll 		ucaa.ucaa_bulkin = ed->bEndpointAddress;
    316          1.1    martin 
    317          1.1    martin 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
    318          1.1    martin 			phyport*2 + 1);
    319          1.1    martin 		if (ed == NULL) {
    320          1.1    martin 			aprint_error_dev(self,
    321          1.1    martin 			    "no bulk out endpoint found for %d\n", i);
    322          1.1    martin 			return;
    323          1.1    martin 		}
    324  1.7.6.1.4.1     skrll 		ucaa.ucaa_bulkout = ed->bEndpointAddress;
    325  1.7.6.1.4.1     skrll 		ucaa.ucaa_portno = i;
    326          1.1    martin 		DPRINTF(("port %d physical port %d bulk-in %d bulk-out %d\n",
    327  1.7.6.1.4.1     skrll 		    i, phyport, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout));
    328          1.1    martin 
    329          1.1    martin 		sc->sc_ports[i].sc_port_phys = phyport;
    330          1.1    martin 		sc->sc_ports[i].sc_port_ucom =
    331  1.7.6.1.4.1     skrll 		    config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    332          1.1    martin 					    ucomprint, ucomsubmatch);
    333          1.1    martin 	}
    334          1.1    martin }
    335          1.1    martin 
    336          1.1    martin static int
    337          1.1    martin umcs7840_get_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t *data)
    338          1.1    martin {
    339          1.1    martin 	usb_device_request_t req;
    340          1.1    martin 	int err;
    341          1.1    martin 
    342          1.1    martin 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    343          1.1    martin 	req.bRequest = MCS7840_RDREQ;
    344          1.1    martin 	USETW(req.wValue, 0);
    345          1.1    martin 	USETW(req.wIndex, reg);
    346          1.1    martin 	USETW(req.wLength, UMCS7840_READ_LENGTH);
    347          1.1    martin 
    348          1.1    martin 	err = usbd_do_request(sc->sc_udev, &req, data);
    349          1.1    martin 	if (err)
    350          1.1    martin 		aprint_normal_dev(sc->sc_dev,
    351          1.1    martin 		    "Reading register %d failed: %s\n", reg, usbd_errstr(err));
    352          1.1    martin 	return err;
    353          1.1    martin }
    354          1.1    martin 
    355          1.1    martin static int
    356          1.1    martin umcs7840_set_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t data)
    357          1.1    martin {
    358          1.1    martin 	usb_device_request_t req;
    359          1.1    martin 	int err;
    360          1.1    martin 
    361          1.1    martin 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    362          1.1    martin 	req.bRequest = MCS7840_WRREQ;
    363          1.1    martin 	USETW(req.wValue, data);
    364          1.1    martin 	USETW(req.wIndex, reg);
    365          1.1    martin 	USETW(req.wLength, 0);
    366          1.1    martin 
    367          1.1    martin 	err = usbd_do_request(sc->sc_udev, &req, 0);
    368          1.1    martin 	if (err)
    369          1.1    martin 		aprint_normal_dev(sc->sc_dev, "Writing register %d failed: %s\n", reg, usbd_errstr(err));
    370          1.1    martin 
    371          1.1    martin 	return err;
    372          1.1    martin }
    373          1.1    martin 
    374          1.1    martin static int
    375          1.1    martin umcs7840_get_UART_reg(struct umcs7840_softc *sc, uint8_t portno,
    376          1.1    martin 	uint8_t reg, uint8_t *data)
    377          1.1    martin {
    378          1.1    martin 	usb_device_request_t req;
    379          1.1    martin 	uint16_t wVal;
    380          1.1    martin 	int err;
    381          1.1    martin 
    382          1.1    martin 	/* portno is port number */
    383          1.1    martin 	wVal = ((uint16_t)(portno + 1)) << 8;
    384          1.1    martin 
    385          1.1    martin 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    386          1.1    martin 	req.bRequest = MCS7840_RDREQ;
    387          1.1    martin 	USETW(req.wValue, wVal);
    388          1.1    martin 	USETW(req.wIndex, reg);
    389          1.1    martin 	USETW(req.wLength, UMCS7840_READ_LENGTH);
    390          1.1    martin 
    391          1.1    martin 	err = usbd_do_request(sc->sc_udev, &req, data);
    392          1.1    martin 	if (err)
    393          1.1    martin 		aprint_normal_dev(sc->sc_dev, "Reading UART %d register %d failed: %s\n", portno, reg, usbd_errstr(err));
    394          1.1    martin 	return err;
    395          1.1    martin }
    396          1.1    martin 
    397          1.1    martin static int
    398          1.1    martin umcs7840_set_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t data)
    399          1.1    martin {
    400          1.1    martin 	usb_device_request_t req;
    401          1.1    martin 	int err;
    402          1.1    martin 	uint16_t wVal;
    403          1.1    martin 
    404          1.1    martin 	/* portno is the physical port number */
    405          1.1    martin 	wVal = ((uint16_t)(portno + 1)) << 8 | data;
    406          1.1    martin 
    407          1.1    martin 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    408          1.1    martin 	req.bRequest = MCS7840_WRREQ;
    409          1.1    martin 	USETW(req.wValue, wVal);
    410          1.1    martin 	USETW(req.wIndex, reg);
    411          1.1    martin 	USETW(req.wLength, 0);
    412          1.1    martin 
    413          1.1    martin 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    414          1.1    martin 	if (err)
    415          1.3    martin 		aprint_error_dev(sc->sc_dev,
    416          1.3    martin 		    "Writing UART %d register %d failed: %s\n",
    417          1.3    martin 		    portno, reg, usbd_errstr(err));
    418          1.1    martin 	return err;
    419          1.1    martin }
    420          1.1    martin 
    421          1.1    martin static int
    422      1.7.6.1       riz umcs7840_set_baudrate(struct umcs7840_softc *sc, uint8_t portno,
    423      1.7.6.1       riz 	uint32_t rate)
    424          1.1    martin {
    425          1.1    martin 	int err;
    426          1.1    martin 	uint16_t divisor;
    427          1.1    martin 	uint8_t clk;
    428          1.1    martin 	uint8_t data;
    429          1.1    martin 	uint8_t physport = sc->sc_ports[portno].sc_port_phys;
    430          1.1    martin 	int spreg = umcs7840_reg_sp(physport);
    431          1.1    martin 
    432          1.1    martin 	if (umcs7840_calc_baudrate(rate, &divisor, &clk)) {
    433          1.1    martin 		DPRINTF(("Port %d bad speed: %d\n", portno, rate));
    434  1.7.6.1.4.1     skrll 		return -1;
    435          1.1    martin 	}
    436          1.1    martin 	if (divisor == 0 || (clk & MCS7840_DEV_SPx_CLOCK_MASK) != clk) {
    437      1.7.6.1       riz 		DPRINTF(("Port %d bad speed calculation: %d\n", portno,
    438      1.7.6.1       riz 		    rate));
    439  1.7.6.1.4.1     skrll 		return -1;
    440          1.1    martin 	}
    441          1.1    martin 	DPRINTF(("Port %d set speed: %d (%02x / %d)\n", portno, rate, clk, divisor));
    442          1.1    martin 
    443          1.1    martin 	/* Set clock source for standard BAUD frequences */
    444          1.1    martin 	err = umcs7840_get_reg(sc, spreg, &data);
    445          1.1    martin 	if (err)
    446          1.1    martin 		return err;
    447          1.1    martin 	data &= MCS7840_DEV_SPx_CLOCK_MASK;
    448          1.1    martin 	data |= clk;
    449          1.1    martin 	err = umcs7840_set_reg(sc, spreg, data);
    450          1.1    martin 	if (err)
    451          1.1    martin 		return err;
    452          1.1    martin 
    453          1.1    martin 	/* Set divider */
    454          1.1    martin 	sc->sc_ports[portno].sc_port_lcr |= MCS7840_UART_LCR_DIVISORS;
    455          1.1    martin 	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
    456          1.1    martin 	if (err)
    457          1.1    martin 		return err;
    458          1.1    martin 
    459          1.1    martin 	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLL, (uint8_t)(divisor & 0xff));
    460          1.1    martin 	if (err)
    461          1.1    martin 		return err;
    462          1.1    martin 	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLM, (uint8_t)((divisor >> 8) & 0xff));
    463          1.1    martin 	if (err)
    464          1.1    martin 		return err;
    465          1.1    martin 
    466          1.1    martin 	/* Turn off access to DLL/DLM registers of UART */
    467          1.1    martin 	sc->sc_ports[portno].sc_port_lcr &= ~MCS7840_UART_LCR_DIVISORS;
    468          1.1    martin 	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
    469          1.1    martin 	if (err)
    470          1.1    martin 		return err;
    471  1.7.6.1.4.1     skrll 	return 0;
    472          1.1    martin }
    473          1.1    martin 
    474          1.1    martin static int
    475          1.1    martin umcs7840_calc_baudrate(uint32_t rate, uint16_t *divisor, uint8_t *clk)
    476          1.1    martin {
    477          1.1    martin 	/* Maximum speeds for standard frequences, when PLL is not used */
    478          1.1    martin 	static const uint32_t umcs7840_baudrate_divisors[] =
    479          1.1    martin 	    {0, 115200, 230400, 403200, 460800, 806400, 921600,
    480          1.1    martin 	     1572864, 3145728,};
    481          1.1    martin 	static const uint8_t umcs7840_baudrate_divisors_len =
    482          1.1    martin 	     __arraycount(umcs7840_baudrate_divisors);
    483          1.1    martin 	uint8_t i = 0;
    484          1.1    martin 
    485          1.1    martin 	if (rate > umcs7840_baudrate_divisors[umcs7840_baudrate_divisors_len - 1])
    486  1.7.6.1.4.1     skrll 		return -1;
    487          1.1    martin 
    488          1.1    martin 	for (i = 0; i < umcs7840_baudrate_divisors_len - 1
    489          1.1    martin 	     && !(rate > umcs7840_baudrate_divisors[i]
    490          1.1    martin 	     && rate <= umcs7840_baudrate_divisors[i + 1]); ++i);
    491          1.1    martin 	*divisor = umcs7840_baudrate_divisors[i + 1] / rate;
    492          1.1    martin 	/* 0x00 .. 0x70 */
    493          1.1    martin 	*clk = i << MCS7840_DEV_SPx_CLOCK_SHIFT;
    494  1.7.6.1.4.1     skrll 	return 0;
    495          1.1    martin }
    496          1.1    martin 
    497  1.7.6.1.4.1     skrll static int
    498          1.1    martin umcs7840_detach(device_t self, int flags)
    499          1.1    martin {
    500          1.1    martin 	struct umcs7840_softc *sc = device_private(self);
    501          1.1    martin 	int rv = 0, i;
    502          1.1    martin 
    503          1.4    martin 	sc->sc_dying = true;
    504          1.3    martin 
    505          1.1    martin 	/* close interrupt pipe */
    506          1.1    martin 	if (sc->sc_intr_pipe != NULL) {
    507          1.1    martin 		rv = usbd_abort_pipe(sc->sc_intr_pipe);
    508          1.1    martin 		if (rv)
    509          1.1    martin 			aprint_error_dev(sc->sc_dev,
    510          1.1    martin 			    "abort interrupt pipe failed: %s\n",
    511          1.1    martin 			    usbd_errstr(rv));
    512          1.1    martin 		rv = usbd_close_pipe(sc->sc_intr_pipe);
    513          1.1    martin 		if (rv)
    514          1.1    martin 			aprint_error_dev(sc->sc_dev,
    515          1.3    martin 			    "failed to close interrupt pipe: %s\n",
    516          1.1    martin 			    usbd_errstr(rv));
    517          1.3    martin 		kmem_free(sc->sc_intr_buf, sc->sc_intr_buflen);
    518          1.1    martin 		sc->sc_intr_pipe = NULL;
    519          1.1    martin 	}
    520          1.5    martin 	usb_rem_task(sc->sc_udev, &sc->sc_change_task);
    521          1.1    martin 
    522          1.4    martin 	/* detach children */
    523          1.4    martin 	for (i = 0; i < sc->sc_numports; i++) {
    524          1.4    martin 		if (sc->sc_ports[i].sc_port_ucom) {
    525          1.4    martin 			rv = config_detach(sc->sc_ports[i].sc_port_ucom,
    526          1.4    martin 			    flags);
    527          1.4    martin 			if (rv)
    528          1.4    martin 				break;
    529          1.4    martin 		}
    530          1.4    martin 	}
    531          1.4    martin 
    532  1.7.6.1.4.1     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    533          1.1    martin 
    534          1.1    martin 	return rv;
    535          1.1    martin }
    536          1.1    martin 
    537          1.1    martin int
    538          1.1    martin umcs7840_activate(device_t self, enum devact act)
    539          1.1    martin {
    540          1.1    martin 	struct umcs7840_softc *sc = device_private(self);
    541          1.1    martin 
    542          1.1    martin 	switch (act) {
    543          1.1    martin 	case DVACT_DEACTIVATE:
    544          1.1    martin 		sc->sc_dying = true;
    545          1.1    martin 		return 0;
    546          1.1    martin 	default:
    547          1.1    martin 		return EOPNOTSUPP;
    548          1.1    martin 	}
    549          1.1    martin }
    550          1.1    martin 
    551          1.1    martin static void
    552          1.1    martin umcs7840_childdet(device_t self, device_t child)
    553          1.1    martin {
    554          1.1    martin 	struct umcs7840_softc *sc = device_private(self);
    555          1.1    martin 	int i;
    556          1.1    martin 
    557          1.1    martin 	for (i = 0; i < sc->sc_numports; i++) {
    558          1.1    martin 		if (child == sc->sc_ports[i].sc_port_ucom) {
    559          1.1    martin 			sc->sc_ports[i].sc_port_ucom = NULL;
    560          1.1    martin 			return;
    561          1.1    martin 		}
    562          1.1    martin 	}
    563          1.1    martin }
    564          1.1    martin 
    565          1.1    martin static void
    566          1.1    martin umcs7840_get_status(void *self, int portno, u_char *lsr, u_char *msr)
    567          1.1    martin {
    568          1.1    martin 	struct umcs7840_softc *sc = self;
    569          1.1    martin 	uint8_t pn = sc->sc_ports[portno].sc_port_phys;
    570          1.1    martin 	uint8_t	hw_lsr = 0;	/* local line status register */
    571          1.1    martin 	uint8_t	hw_msr = 0;	/* local modem status register */
    572          1.1    martin 
    573          1.4    martin 	if (sc->sc_dying)
    574          1.4    martin 		return;
    575          1.4    martin 
    576          1.1    martin 	/* Read LSR & MSR */
    577          1.1    martin 	umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LSR, &hw_lsr);
    578          1.1    martin 	umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_MSR, &hw_msr);
    579          1.1    martin 
    580          1.1    martin 	*lsr = hw_lsr;
    581          1.1    martin 	*msr = hw_msr;
    582          1.1    martin }
    583          1.1    martin 
    584          1.1    martin static void
    585          1.1    martin umcs7840_set(void *self, int portno, int reg, int onoff)
    586          1.1    martin {
    587          1.1    martin 	struct umcs7840_softc *sc = self;
    588          1.1    martin 
    589          1.1    martin 	if (sc->sc_dying)
    590          1.1    martin 		return;
    591          1.1    martin 
    592          1.1    martin 	switch (reg) {
    593          1.1    martin 	case UCOM_SET_DTR:
    594      1.7.6.1       riz 		umcs7840_dtr(sc, portno, onoff);
    595          1.1    martin 		break;
    596          1.1    martin 	case UCOM_SET_RTS:
    597      1.7.6.1       riz 		umcs7840_rts(sc, portno, onoff);
    598          1.1    martin 		break;
    599          1.1    martin 	case UCOM_SET_BREAK:
    600      1.7.6.1       riz 		umcs7840_break(sc, portno, onoff);
    601          1.1    martin 		break;
    602          1.1    martin 	default:
    603          1.1    martin 		break;
    604          1.1    martin 	}
    605          1.1    martin }
    606          1.1    martin 
    607          1.1    martin static int
    608          1.1    martin umcs7840_param(void *self, int portno, struct termios *t)
    609          1.1    martin {
    610          1.1    martin 	struct umcs7840_softc *sc = self;
    611          1.1    martin 	int pn = sc->sc_ports[portno].sc_port_phys;
    612          1.1    martin 	uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
    613          1.1    martin 	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
    614          1.1    martin 
    615          1.1    martin 	if (t->c_cflag & CSTOPB) {
    616          1.1    martin 		lcr |= MCS7840_UART_LCR_STOPB2;
    617          1.1    martin 	} else {
    618          1.1    martin 		lcr |= MCS7840_UART_LCR_STOPB1;
    619          1.1    martin 	}
    620          1.1    martin 
    621          1.1    martin 	lcr &= ~MCS7840_UART_LCR_PARITYMASK;
    622          1.1    martin 	if (t->c_cflag & PARENB) {
    623          1.1    martin 		lcr |= MCS7840_UART_LCR_PARITYON;
    624          1.1    martin 		if (t->c_cflag & PARODD) {
    625          1.1    martin 			lcr = MCS7840_UART_LCR_PARITYODD;
    626          1.1    martin 		} else {
    627          1.1    martin 			lcr = MCS7840_UART_LCR_PARITYEVEN;
    628          1.1    martin 		}
    629          1.1    martin 	} else {
    630          1.1    martin 		lcr &= ~MCS7840_UART_LCR_PARITYON;
    631          1.1    martin 	}
    632          1.1    martin 
    633          1.1    martin 	lcr &= ~MCS7840_UART_LCR_DATALENMASK;
    634          1.1    martin 	switch (t->c_cflag & CSIZE) {
    635          1.1    martin 	case CS5:
    636          1.1    martin 		lcr |= MCS7840_UART_LCR_DATALEN5;
    637          1.1    martin 		break;
    638          1.1    martin 	case CS6:
    639          1.1    martin 		lcr |= MCS7840_UART_LCR_DATALEN6;
    640          1.1    martin 		break;
    641          1.1    martin 	case CS7:
    642          1.1    martin 		lcr |= MCS7840_UART_LCR_DATALEN7;
    643          1.1    martin 		break;
    644          1.1    martin 	case CS8:
    645          1.1    martin 		lcr |= MCS7840_UART_LCR_DATALEN8;
    646          1.1    martin 		break;
    647          1.1    martin 	}
    648          1.1    martin 
    649          1.1    martin 	if (t->c_cflag & CRTSCTS)
    650          1.1    martin 		mcr |= MCS7840_UART_MCR_CTSRTS;
    651          1.1    martin 	else
    652          1.1    martin 		mcr &= ~MCS7840_UART_MCR_CTSRTS;
    653          1.1    martin 
    654          1.1    martin 	if (t->c_cflag & CLOCAL)
    655          1.1    martin 		mcr &= ~MCS7840_UART_MCR_DTRDSR;
    656          1.1    martin 	else
    657          1.1    martin 		mcr |= MCS7840_UART_MCR_DTRDSR;
    658          1.1    martin 
    659          1.1    martin 	sc->sc_ports[portno].sc_port_lcr = lcr;
    660          1.1    martin 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
    661          1.1    martin 	    sc->sc_ports[pn].sc_port_lcr);
    662          1.1    martin 
    663          1.1    martin 	sc->sc_ports[portno].sc_port_mcr = mcr;
    664          1.1    martin 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
    665          1.1    martin 	    sc->sc_ports[pn].sc_port_mcr);
    666          1.1    martin 
    667      1.7.6.1       riz 	if (umcs7840_set_baudrate(sc, portno, t->c_ospeed))
    668          1.1    martin 		return EIO;
    669          1.1    martin 
    670          1.1    martin 	return 0;
    671          1.1    martin }
    672          1.1    martin 
    673          1.1    martin static void
    674          1.1    martin umcs7840_dtr(struct umcs7840_softc *sc, int portno, bool onoff)
    675          1.1    martin {
    676          1.1    martin 	int pn = sc->sc_ports[portno].sc_port_phys;
    677          1.1    martin 	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
    678          1.1    martin 
    679          1.1    martin 	if (onoff)
    680          1.1    martin 		mcr |= MCS7840_UART_MCR_DTR;
    681          1.1    martin 	else
    682          1.1    martin 		mcr &= ~MCS7840_UART_MCR_DTR;
    683          1.1    martin 
    684          1.1    martin 	sc->sc_ports[portno].sc_port_mcr = mcr;
    685          1.1    martin 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
    686          1.1    martin 	    sc->sc_ports[pn].sc_port_mcr);
    687          1.1    martin }
    688          1.1    martin 
    689          1.1    martin static void
    690          1.1    martin umcs7840_rts(struct umcs7840_softc *sc, int portno, bool onoff)
    691          1.1    martin {
    692          1.1    martin 	int pn = sc->sc_ports[portno].sc_port_phys;
    693          1.1    martin 	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
    694          1.1    martin 
    695          1.1    martin 	if (onoff)
    696          1.1    martin 		mcr |= MCS7840_UART_MCR_RTS;
    697          1.1    martin 	else
    698          1.1    martin 		mcr &= ~MCS7840_UART_MCR_RTS;
    699          1.1    martin 
    700          1.1    martin 	sc->sc_ports[portno].sc_port_mcr = mcr;
    701          1.1    martin 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
    702          1.1    martin 	    sc->sc_ports[pn].sc_port_mcr);
    703          1.1    martin }
    704          1.1    martin 
    705          1.1    martin static void
    706          1.1    martin umcs7840_break(struct umcs7840_softc *sc, int portno, bool onoff)
    707          1.1    martin {
    708          1.1    martin 	int pn = sc->sc_ports[portno].sc_port_phys;
    709          1.1    martin 	uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
    710          1.1    martin 
    711          1.1    martin 	if (onoff)
    712          1.1    martin 		lcr |= MCS7840_UART_LCR_BREAK;
    713          1.1    martin 	else
    714          1.1    martin 		lcr &= ~MCS7840_UART_LCR_BREAK;
    715          1.1    martin 
    716          1.1    martin 	sc->sc_ports[portno].sc_port_lcr = lcr;
    717          1.1    martin 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
    718          1.1    martin 	    sc->sc_ports[pn].sc_port_lcr);
    719          1.1    martin }
    720          1.1    martin 
    721          1.1    martin static int
    722          1.1    martin umcs7840_port_open(void *self, int portno)
    723          1.1    martin {
    724          1.1    martin 	struct umcs7840_softc *sc = self;
    725          1.1    martin 	int pn = sc->sc_ports[portno].sc_port_phys;
    726          1.1    martin 	int spreg = umcs7840_reg_sp(pn);
    727          1.1    martin 	int ctrlreg = umcs7840_reg_ctrl(pn);
    728          1.1    martin 	uint8_t data;
    729          1.1    martin 
    730          1.1    martin 	if (sc->sc_dying)
    731          1.1    martin 		return EIO;
    732          1.1    martin 
    733          1.1    martin 	/* If it very first open, finish global configuration */
    734          1.1    martin 	if (!sc->sc_init_done) {
    735          1.1    martin 		if (umcs7840_get_reg(sc, MCS7840_DEV_REG_CONTROL1, &data))
    736          1.1    martin 			return EIO;
    737          1.1    martin 		data |= MCS7840_DEV_CONTROL1_DRIVER_DONE;
    738          1.1    martin 		if (umcs7840_set_reg(sc, MCS7840_DEV_REG_CONTROL1, data))
    739          1.1    martin 			return EIO;
    740          1.1    martin 		sc->sc_init_done = 1;
    741          1.1    martin 	}
    742          1.1    martin 
    743          1.1    martin 	/* Toggle reset bit on-off */
    744          1.1    martin 	if (umcs7840_get_reg(sc, spreg, &data))
    745          1.1    martin 		return EIO;
    746          1.1    martin 	data |= MCS7840_DEV_SPx_UART_RESET;
    747          1.1    martin 	if (umcs7840_set_reg(sc, spreg, data))
    748          1.1    martin 		return EIO;
    749          1.1    martin 	data &= ~MCS7840_DEV_SPx_UART_RESET;
    750          1.1    martin 	if (umcs7840_set_reg(sc, spreg, data))
    751          1.1    martin 		return EIO;
    752          1.1    martin 
    753          1.1    martin 	/* Set RS-232 mode */
    754          1.1    martin 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_SCRATCHPAD,
    755          1.1    martin 	    MCS7840_UART_SCRATCHPAD_RS232))
    756          1.1    martin 		return EIO;
    757          1.1    martin 
    758          1.1    martin 	/* Disable RX on time of initialization */
    759          1.1    martin 	if (umcs7840_get_reg(sc, ctrlreg, &data))
    760          1.1    martin 		return EIO;
    761          1.1    martin 	data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
    762          1.1    martin 	if (umcs7840_set_reg(sc, ctrlreg, data))
    763          1.1    martin 		return EIO;
    764          1.1    martin 
    765          1.1    martin 	/* Disable all interrupts */
    766          1.1    martin 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0))
    767          1.1    martin 		return EIO;
    768          1.1    martin 
    769          1.1    martin 	/* Reset FIFO -- documented */
    770          1.1    martin 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR, 0))
    771          1.1    martin 		return EIO;
    772          1.1    martin 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR,
    773          1.1    martin 	    MCS7840_UART_FCR_ENABLE | MCS7840_UART_FCR_FLUSHRHR |
    774          1.1    martin 	    MCS7840_UART_FCR_FLUSHTHR | MCS7840_UART_FCR_RTL_1_14))
    775          1.1    martin 		return EIO;
    776          1.1    martin 
    777          1.1    martin 	/* Set 8 bit, no parity, 1 stop bit -- documented */
    778          1.1    martin 	sc->sc_ports[pn].sc_port_lcr =
    779          1.1    martin 	    MCS7840_UART_LCR_DATALEN8 | MCS7840_UART_LCR_STOPB1;
    780          1.1    martin 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
    781          1.1    martin 	    sc->sc_ports[pn].sc_port_lcr))
    782          1.1    martin 		return EIO;
    783          1.1    martin 
    784          1.1    martin 	/*
    785          1.1    martin 	 * Enable DTR/RTS on modem control, enable modem interrupts --
    786          1.1    martin 	 * documented
    787          1.1    martin 	 */
    788          1.1    martin 	sc->sc_ports[pn].sc_port_mcr = MCS7840_UART_MCR_DTR
    789          1.1    martin 	    | MCS7840_UART_MCR_RTS | MCS7840_UART_MCR_IE;
    790          1.1    martin 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
    791          1.1    martin 	    sc->sc_ports[pn].sc_port_mcr))
    792          1.1    martin 		return EIO;
    793          1.1    martin 
    794          1.1    martin 	/* Clearing Bulkin and Bulkout FIFO */
    795          1.1    martin 	if (umcs7840_get_reg(sc, spreg, &data))
    796          1.1    martin 		return EIO;
    797          1.1    martin 	data |= MCS7840_DEV_SPx_RESET_OUT_FIFO | MCS7840_DEV_SPx_RESET_IN_FIFO;
    798          1.1    martin 	if (umcs7840_set_reg(sc, spreg, data))
    799          1.1    martin 		return EIO;
    800          1.1    martin 	data &= ~(MCS7840_DEV_SPx_RESET_OUT_FIFO
    801          1.1    martin 	    | MCS7840_DEV_SPx_RESET_IN_FIFO);
    802          1.1    martin 	if (umcs7840_set_reg(sc, spreg, data))
    803          1.1    martin 		return EIO;
    804          1.1    martin 
    805          1.1    martin 	/* Set speed 9600 */
    806      1.7.6.1       riz 	if (umcs7840_set_baudrate(sc, portno, 9600))
    807          1.1    martin 		return EIO;
    808          1.1    martin 
    809          1.1    martin 
    810          1.1    martin 	/* Finally enable all interrupts -- documented */
    811          1.1    martin 	/*
    812          1.1    martin 	 * Copied from vendor driver, I don't know why we should read LCR
    813          1.1    martin 	 * here
    814          1.1    martin 	 */
    815          1.1    martin 	if (umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
    816          1.1    martin 	    &sc->sc_ports[pn].sc_port_lcr))
    817          1.1    martin 		return EIO;
    818          1.1    martin 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER,
    819          1.1    martin 	    MCS7840_UART_IER_RXSTAT | MCS7840_UART_IER_MODEM))
    820          1.1    martin 		return EIO;
    821          1.1    martin 
    822          1.1    martin 	/* Enable RX */
    823          1.1    martin 	if (umcs7840_get_reg(sc, ctrlreg, &data))
    824          1.1    martin 		return EIO;
    825          1.1    martin 	data &= ~MCS7840_DEV_CONTROLx_RX_DISABLE;
    826          1.1    martin 	if (umcs7840_set_reg(sc, ctrlreg, data))
    827          1.1    martin 		return EIO;
    828          1.1    martin 	return 0;
    829          1.1    martin }
    830          1.1    martin 
    831          1.1    martin static void
    832          1.1    martin umcs7840_port_close(void *self, int portno)
    833          1.1    martin {
    834          1.1    martin 	struct umcs7840_softc *sc = self;
    835          1.1    martin 	int pn = sc->sc_ports[portno].sc_port_phys;
    836          1.1    martin 	int ctrlreg = umcs7840_reg_ctrl(pn);
    837          1.1    martin 	uint8_t data;
    838          1.1    martin 
    839          1.4    martin 	if (sc->sc_dying)
    840          1.4    martin 		return;
    841          1.4    martin 
    842          1.1    martin 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR, 0);
    843          1.1    martin 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0);
    844          1.1    martin 
    845          1.1    martin 	/* Disable RX */
    846          1.1    martin 	if (umcs7840_get_reg(sc, ctrlreg, &data))
    847          1.1    martin 		return;
    848          1.1    martin 	data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
    849          1.1    martin 	if (umcs7840_set_reg(sc, ctrlreg, data))
    850          1.1    martin 		return;
    851          1.1    martin }
    852          1.1    martin 
    853          1.1    martin static void
    854  1.7.6.1.4.1     skrll umcs7840_intr(struct usbd_xfer *xfer, void *priv,
    855          1.1    martin     usbd_status status)
    856          1.1    martin {
    857          1.1    martin 	struct umcs7840_softc *sc = priv;
    858          1.1    martin 	u_char *buf = sc->sc_intr_buf;
    859          1.1    martin 	int actlen;
    860          1.5    martin 	int subunit;
    861          1.1    martin 
    862          1.4    martin 	if (status == USBD_NOT_STARTED || status == USBD_CANCELLED
    863          1.4    martin 	    || status == USBD_IOERROR)
    864          1.1    martin 		return;
    865          1.1    martin 
    866          1.1    martin 	if (status != USBD_NORMAL_COMPLETION) {
    867          1.1    martin 		aprint_error_dev(sc->sc_dev,
    868          1.1    martin 		    "umcs7840_intr: abnormal status: %s\n",
    869          1.1    martin 		    usbd_errstr(status));
    870          1.1    martin 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    871          1.1    martin 		return;
    872          1.1    martin 	}
    873          1.1    martin 
    874          1.1    martin 	usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
    875          1.1    martin 	if (actlen == 5 || actlen == 13) {
    876          1.6  riastrad 		uint32_t change_mask = 0;
    877          1.1    martin 		/* Check status of all ports */
    878          1.1    martin 		for (subunit = 0; subunit < sc->sc_numports; subunit++) {
    879          1.1    martin 			uint8_t pn = sc->sc_ports[subunit].sc_port_phys;
    880          1.1    martin 			if (buf[pn] & MCS7840_UART_ISR_NOPENDING)
    881          1.1    martin 				continue;
    882          1.1    martin 			DPRINTF(("Port %d has pending interrupt: %02x "
    883          1.1    martin 			    "(FIFO: %02x)\n", pn,
    884          1.1    martin 			    buf[pn] & MCS7840_UART_ISR_INTMASK,
    885          1.1    martin 			    buf[pn] & (~MCS7840_UART_ISR_INTMASK)));
    886          1.1    martin 			switch (buf[pn] & MCS7840_UART_ISR_INTMASK) {
    887          1.1    martin 			case MCS7840_UART_ISR_RXERR:
    888          1.1    martin 			case MCS7840_UART_ISR_RXHASDATA:
    889          1.1    martin 			case MCS7840_UART_ISR_RXTIMEOUT:
    890          1.1    martin 			case MCS7840_UART_ISR_MSCHANGE:
    891          1.6  riastrad 				change_mask |= (1U << subunit);
    892          1.1    martin 				break;
    893          1.1    martin 			default:
    894          1.1    martin 				/* Do nothing */
    895          1.1    martin 				break;
    896          1.1    martin 			}
    897          1.1    martin 		}
    898          1.5    martin 
    899          1.6  riastrad 		if (change_mask != 0) {
    900          1.6  riastrad 			atomic_or_32(&sc->sc_change_mask, change_mask);
    901          1.6  riastrad 			usb_add_task(sc->sc_udev, &sc->sc_change_task,
    902          1.6  riastrad 			    USB_TASKQ_DRIVER);
    903          1.6  riastrad 		}
    904          1.1    martin 	} else {
    905          1.3    martin 		aprint_error_dev(sc->sc_dev,
    906          1.3    martin 		   "Invalid interrupt data length %d", actlen);
    907          1.1    martin 	}
    908          1.1    martin }
    909          1.1    martin 
    910          1.1    martin static void
    911          1.5    martin umcs7840_change_task(void *arg)
    912          1.1    martin {
    913          1.1    martin 	struct umcs7840_softc *sc = arg;
    914          1.6  riastrad 	uint32_t change_mask;
    915          1.1    martin 	int i;
    916          1.1    martin 
    917          1.6  riastrad 	change_mask = atomic_swap_32(&sc->sc_change_mask, 0);
    918          1.1    martin 	for (i = 0; i < sc->sc_numports; i++) {
    919          1.6  riastrad 		if (ISSET(change_mask, (1U << i)))
    920          1.1    martin 			ucom_status_change(device_private(
    921          1.1    martin 			    sc->sc_ports[i].sc_port_ucom));
    922          1.1    martin 	}
    923          1.1    martin }
    924