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