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