xref: /netbsd-src/sys/dev/usb/umcs.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /* $NetBSD: umcs.c,v 1.8 2014/08/23 21:37:56 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@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.8 2014/08/23 21:37:56 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 	usbd_interface_handle sc_iface; /* the usb interface */
89 	usbd_device_handle sc_udev;	/* the usb device */
90 	usbd_pipe_handle 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 *sc, uint8_t reg, uint8_t *data);
103 static int umcs7840_set_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t data);
104 static int umcs7840_get_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t *data);
105 static int umcs7840_set_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t data);
106 static int umcs7840_calc_baudrate(uint32_t rate, uint16_t *divisor, uint8_t *clk);
107 static void umcs7840_dtr(struct umcs7840_softc *sc, int portno, bool onoff);
108 static void umcs7840_rts(struct umcs7840_softc *sc, int portno, bool onoff);
109 static void umcs7840_break(struct umcs7840_softc *sc, int portno, bool onoff);
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(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status);
115 static void umcs7840_change_task(void *arg);
116 static int umcs7840_activate(device_t, enum devact);
117 static void umcs7840_childdet(device_t, device_t);
118 
119 static void umcs7840_get_status(void *, int, u_char *, u_char *);
120 static void umcs7840_set(void *, int, int, int);
121 static int umcs7840_param(void *, int, struct termios *);
122 static int umcs7840_port_open(void *sc, int portno);
123 static void umcs7840_port_close(void *sc, int portno);
124 
125 struct ucom_methods umcs7840_methods = {
126 	.ucom_get_status = umcs7840_get_status,
127 	.ucom_set = umcs7840_set,
128 	.ucom_param = umcs7840_param,
129 	.ucom_open = umcs7840_port_open,
130 	.ucom_close = umcs7840_port_close,
131 };
132 
133 static const struct usb_devno umcs7840_devs[] = {
134 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7703 },
135 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7810 },
136 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7820 },
137 	{ USB_VENDOR_MOSCHIP,		USB_PRODUCT_MOSCHIP_MCS7840 },
138 	{ USB_VENDOR_ATEN,		USB_PRODUCT_ATEN_UC2324 }
139 };
140 #define umcs7840_lookup(v, p) usb_lookup(umcs7840_devs, v, p)
141 
142 CFATTACH_DECL2_NEW(umcs, sizeof(struct umcs7840_softc), umcs7840_match,
143     umcs7840_attach, umcs7840_detach, umcs7840_activate, NULL,
144     umcs7840_childdet);
145 
146 static inline int
147 umcs7840_reg_sp(int phyport)
148 {
149 	KASSERT(phyport >= 0 && phyport < 4);
150 	switch (phyport) {
151 	default:
152 	case 0:	return MCS7840_DEV_REG_SP1;
153 	case 1:	return MCS7840_DEV_REG_SP2;
154 	case 2:	return MCS7840_DEV_REG_SP3;
155 	case 3:	return MCS7840_DEV_REG_SP4;
156 	}
157 }
158 
159 static inline int
160 umcs7840_reg_ctrl(int phyport)
161 {
162 	KASSERT(phyport >= 0 && phyport < 4);
163 	switch (phyport) {
164 	default:
165 	case 0:	return MCS7840_DEV_REG_CONTROL1;
166 	case 1:	return MCS7840_DEV_REG_CONTROL2;
167 	case 2:	return MCS7840_DEV_REG_CONTROL3;
168 	case 3:	return MCS7840_DEV_REG_CONTROL4;
169 	}
170 }
171 
172 static int
173 umcs7840_match(device_t dev, cfdata_t match, void *aux)
174 {
175 	struct usb_attach_arg *uaa = aux;
176 
177 	return (umcs7840_lookup(uaa->vendor, uaa->product) != NULL ?
178 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
179 }
180 
181 static void
182 umcs7840_attach(device_t parent, device_t self, void * aux)
183 {
184 	struct umcs7840_softc *sc = device_private(self);
185 	struct usb_attach_arg *uaa = aux;
186 	usbd_device_handle dev = uaa->device;
187 	usb_interface_descriptor_t *id;
188 	usb_endpoint_descriptor_t *ed;
189 	char *devinfop;
190 	struct ucom_attach_args uca;
191 	int error, i, intr_addr;
192 	uint8_t data;
193 
194 	sc->sc_dev = self;
195 	sc->sc_udev = uaa->device;
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 		return;
200 	}
201 
202 	/* get the first interface handle */
203 	error = usbd_device2interface_handle(sc->sc_udev, MCS7840_IFACE_INDEX,
204 	    &sc->sc_iface);
205 	if (error != 0) {
206 		aprint_error(": could not get interface handle\n");
207 		return;
208 	}
209 
210 	/*
211 	 * Get number of ports
212 	 * Documentation (full datasheet) says, that number of ports is
213 	 * set as MCS7840_DEV_MODE_SELECT24S bit in MODE R/Only
214 	 * register. But vendor driver uses these undocumented
215 	 * register & bit.
216 	 *
217 	 * Experiments show, that MODE register can have `0'
218 	 * (4 ports) bit on 2-port device, so use vendor driver's way.
219 	 *
220 	 * Also, see notes in header file for these constants.
221 	 */
222 	umcs7840_get_reg(sc, MCS7840_DEV_REG_GPIO, &data);
223 	if (data & MCS7840_DEV_GPIO_4PORTS) {
224 		sc->sc_numports = 4;
225 		/* physical port no are : 0, 1, 2, 3 */
226 	} else {
227 		if (uaa->product == USB_PRODUCT_MOSCHIP_MCS7810)
228 			sc->sc_numports = 1;
229 		else {
230 			sc->sc_numports = 2;
231 			/* physical port no are : 0 and 2 */
232 		}
233 	}
234 	devinfop = usbd_devinfo_alloc(dev, 0);
235 	aprint_normal(": %s\n", devinfop);
236 	usbd_devinfo_free(devinfop);
237 	aprint_verbose_dev(self, "found %d active ports\n", sc->sc_numports);
238 
239 	if (!umcs7840_get_reg(sc, MCS7840_DEV_REG_MODE, &data)) {
240 		aprint_verbose_dev(self, "On-die confguration: RST: active %s, "
241 		    "HRD: %s, PLL: %s, POR: %s, Ports: %s, EEPROM write %s, "
242 		    "IrDA is %savailable\n",
243 		    (data & MCS7840_DEV_MODE_RESET) ? "low" : "high",
244 		    (data & MCS7840_DEV_MODE_SER_PRSNT) ? "yes" : "no",
245 		    (data & MCS7840_DEV_MODE_PLLBYPASS) ? "bypassed" : "avail",
246 		    (data & MCS7840_DEV_MODE_PORBYPASS) ? "bypassed" : "avail",
247 		    (data & MCS7840_DEV_MODE_SELECT24S) ? "2" : "4",
248 		    (data & MCS7840_DEV_MODE_EEPROMWR) ? "enabled" : "disabled",
249 		    (data & MCS7840_DEV_MODE_IRDA) ? "" : "not ");
250 	}
251 
252 	/*
253 	 * Set up the interrupt pipe
254 	 */
255 	id = usbd_get_interface_descriptor(sc->sc_iface);
256 	intr_addr = -1;
257 	for (i = 0 ; i < id->bNumEndpoints ; i++) {
258 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
259 		if (ed == NULL) continue;
260 		if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN
261 		    || UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT)
262 			continue;
263 		sc->sc_intr_buflen = UGETW(ed->wMaxPacketSize);
264 		intr_addr = ed->bEndpointAddress;
265 		break;
266 	}
267 	if (intr_addr < 0) {
268 		aprint_error_dev(self, "interrupt pipe not found\n");
269 		return;
270 	}
271 	sc->sc_intr_buf = kmem_alloc(sc->sc_intr_buflen, KM_SLEEP);
272 
273 	error = usbd_open_pipe_intr(sc->sc_iface, intr_addr,
274 		    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
275 		    sc->sc_intr_buflen, umcs7840_intr, 100);
276 	if (error) {
277 		aprint_error_dev(self, "cannot open interrupt pipe "
278 		    "(addr %d)\n", intr_addr);
279 		return;
280 	}
281 
282 	usb_init_task(&sc->sc_change_task, umcs7840_change_task, sc,
283 	    USB_TASKQ_MPSAFE);
284 
285 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
286 	    sc->sc_dev);
287 
288 	memset(&uca, 0, sizeof uca);
289 	uca.ibufsize = 256;
290 	uca.obufsize = 256;
291 	uca.ibufsizepad = 256;
292 	uca.opkthdrlen = 0;
293 	uca.device = sc->sc_udev;
294 	uca.iface = sc->sc_iface;
295 	uca.methods = &umcs7840_methods;
296 	uca.arg = sc;
297 
298 	for (i = 0; i < sc->sc_numports; i++) {
299 		uca.bulkin = uca.bulkout = -1;
300 
301 		/*
302 		 * On four port cards, endpoints are 0/1 for first,
303 		 * 2/3 for second, ...
304 		 * On two port cards, they are 0/1 for first, 4/5 for second.
305 		 * On single port, just 0/1 will be used.
306 		 */
307 		int phyport = i * (sc->sc_numports == 2 ? 2 : 1);
308 
309 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
310 			phyport*2);
311 		if (ed == NULL) {
312 			aprint_error_dev(self,
313 			    "no bulk in endpoint found for %d\n", i);
314 			return;
315 		}
316 		uca.bulkin = ed->bEndpointAddress;
317 
318 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
319 			phyport*2 + 1);
320 		if (ed == NULL) {
321 			aprint_error_dev(self,
322 			    "no bulk out endpoint found for %d\n", i);
323 			return;
324 		}
325 		uca.bulkout = ed->bEndpointAddress;
326 		uca.portno = i;
327 		DPRINTF(("port %d physical port %d bulk-in %d bulk-out %d\n",
328 		    i, phyport, uca.bulkin, uca.bulkout));
329 
330 		sc->sc_ports[i].sc_port_phys = phyport;
331 		sc->sc_ports[i].sc_port_ucom =
332 		    config_found_sm_loc(self, "ucombus", NULL, &uca,
333 					    ucomprint, ucomsubmatch);
334 	}
335 }
336 
337 static int
338 umcs7840_get_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t *data)
339 {
340 	usb_device_request_t req;
341 	int err;
342 
343 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
344 	req.bRequest = MCS7840_RDREQ;
345 	USETW(req.wValue, 0);
346 	USETW(req.wIndex, reg);
347 	USETW(req.wLength, UMCS7840_READ_LENGTH);
348 
349 	err = usbd_do_request(sc->sc_udev, &req, data);
350 	if (err)
351 		aprint_normal_dev(sc->sc_dev,
352 		    "Reading register %d failed: %s\n", reg, usbd_errstr(err));
353 	return err;
354 }
355 
356 static int
357 umcs7840_set_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t data)
358 {
359 	usb_device_request_t req;
360 	int err;
361 
362 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
363 	req.bRequest = MCS7840_WRREQ;
364 	USETW(req.wValue, data);
365 	USETW(req.wIndex, reg);
366 	USETW(req.wLength, 0);
367 
368 	err = usbd_do_request(sc->sc_udev, &req, 0);
369 	if (err)
370 		aprint_normal_dev(sc->sc_dev, "Writing register %d failed: %s\n", reg, usbd_errstr(err));
371 
372 	return err;
373 }
374 
375 static int
376 umcs7840_get_UART_reg(struct umcs7840_softc *sc, uint8_t portno,
377 	uint8_t reg, uint8_t *data)
378 {
379 	usb_device_request_t req;
380 	uint16_t wVal;
381 	int err;
382 
383 	/* portno is port number */
384 	wVal = ((uint16_t)(portno + 1)) << 8;
385 
386 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
387 	req.bRequest = MCS7840_RDREQ;
388 	USETW(req.wValue, wVal);
389 	USETW(req.wIndex, reg);
390 	USETW(req.wLength, UMCS7840_READ_LENGTH);
391 
392 	err = usbd_do_request(sc->sc_udev, &req, data);
393 	if (err)
394 		aprint_normal_dev(sc->sc_dev, "Reading UART %d register %d failed: %s\n", portno, reg, usbd_errstr(err));
395 	return err;
396 }
397 
398 static int
399 umcs7840_set_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t data)
400 {
401 	usb_device_request_t req;
402 	int err;
403 	uint16_t wVal;
404 
405 	/* portno is the physical port number */
406 	wVal = ((uint16_t)(portno + 1)) << 8 | data;
407 
408 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
409 	req.bRequest = MCS7840_WRREQ;
410 	USETW(req.wValue, wVal);
411 	USETW(req.wIndex, reg);
412 	USETW(req.wLength, 0);
413 
414 	err = usbd_do_request(sc->sc_udev, &req, NULL);
415 	if (err)
416 		aprint_error_dev(sc->sc_dev,
417 		    "Writing UART %d register %d failed: %s\n",
418 		    portno, reg, usbd_errstr(err));
419 	return err;
420 }
421 
422 static int
423 umcs7840_set_baudrate(struct umcs7840_softc *sc, uint8_t portno,
424 	uint32_t rate)
425 {
426 	int err;
427 	uint16_t divisor;
428 	uint8_t clk;
429 	uint8_t data;
430 	uint8_t physport = sc->sc_ports[portno].sc_port_phys;
431 	int spreg = umcs7840_reg_sp(physport);
432 
433 	if (umcs7840_calc_baudrate(rate, &divisor, &clk)) {
434 		DPRINTF(("Port %d bad speed: %d\n", portno, rate));
435 		return (-1);
436 	}
437 	if (divisor == 0 || (clk & MCS7840_DEV_SPx_CLOCK_MASK) != clk) {
438 		DPRINTF(("Port %d bad speed calculation: %d\n", portno,
439 		    rate));
440 		return (-1);
441 	}
442 	DPRINTF(("Port %d set speed: %d (%02x / %d)\n", portno, rate, clk, divisor));
443 
444 	/* Set clock source for standard BAUD frequences */
445 	err = umcs7840_get_reg(sc, spreg, &data);
446 	if (err)
447 		return err;
448 	data &= MCS7840_DEV_SPx_CLOCK_MASK;
449 	data |= clk;
450 	err = umcs7840_set_reg(sc, spreg, data);
451 	if (err)
452 		return err;
453 
454 	/* Set divider */
455 	sc->sc_ports[portno].sc_port_lcr |= MCS7840_UART_LCR_DIVISORS;
456 	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
457 	if (err)
458 		return err;
459 
460 	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLL, (uint8_t)(divisor & 0xff));
461 	if (err)
462 		return err;
463 	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLM, (uint8_t)((divisor >> 8) & 0xff));
464 	if (err)
465 		return err;
466 
467 	/* Turn off access to DLL/DLM registers of UART */
468 	sc->sc_ports[portno].sc_port_lcr &= ~MCS7840_UART_LCR_DIVISORS;
469 	err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
470 	if (err)
471 		return err;
472 	return (0);
473 }
474 
475 static int
476 umcs7840_calc_baudrate(uint32_t rate, uint16_t *divisor, uint8_t *clk)
477 {
478 	/* Maximum speeds for standard frequences, when PLL is not used */
479 	static const uint32_t umcs7840_baudrate_divisors[] =
480 	    {0, 115200, 230400, 403200, 460800, 806400, 921600,
481 	     1572864, 3145728,};
482 	static const uint8_t umcs7840_baudrate_divisors_len =
483 	     __arraycount(umcs7840_baudrate_divisors);
484 	uint8_t i = 0;
485 
486 	if (rate > umcs7840_baudrate_divisors[umcs7840_baudrate_divisors_len - 1])
487 		return (-1);
488 
489 	for (i = 0; i < umcs7840_baudrate_divisors_len - 1
490 	     && !(rate > umcs7840_baudrate_divisors[i]
491 	     && rate <= umcs7840_baudrate_divisors[i + 1]); ++i);
492 	*divisor = umcs7840_baudrate_divisors[i + 1] / rate;
493 	/* 0x00 .. 0x70 */
494 	*clk = i << MCS7840_DEV_SPx_CLOCK_SHIFT;
495 	return (0);
496 }
497 
498 static int
499 umcs7840_detach(device_t self, int flags)
500 {
501 	struct umcs7840_softc *sc = device_private(self);
502 	int rv = 0, i;
503 
504 	sc->sc_dying = true;
505 
506 	/* close interrupt pipe */
507 	if (sc->sc_intr_pipe != NULL) {
508 		rv = usbd_abort_pipe(sc->sc_intr_pipe);
509 		if (rv)
510 			aprint_error_dev(sc->sc_dev,
511 			    "abort interrupt pipe failed: %s\n",
512 			    usbd_errstr(rv));
513 		rv = usbd_close_pipe(sc->sc_intr_pipe);
514 		if (rv)
515 			aprint_error_dev(sc->sc_dev,
516 			    "failed to close interrupt pipe: %s\n",
517 			    usbd_errstr(rv));
518 		kmem_free(sc->sc_intr_buf, sc->sc_intr_buflen);
519 		sc->sc_intr_pipe = NULL;
520 	}
521 	usb_rem_task(sc->sc_udev, &sc->sc_change_task);
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 			if (rv)
529 				break;
530 		}
531 	}
532 
533 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
534 			   sc->sc_dev);
535 
536 	return rv;
537 }
538 
539 int
540 umcs7840_activate(device_t self, enum devact act)
541 {
542 	struct umcs7840_softc *sc = device_private(self);
543 
544 	switch (act) {
545 	case DVACT_DEACTIVATE:
546 		sc->sc_dying = true;
547 		return 0;
548 	default:
549 		return EOPNOTSUPP;
550 	}
551 }
552 
553 static void
554 umcs7840_childdet(device_t self, device_t child)
555 {
556 	struct umcs7840_softc *sc = device_private(self);
557 	int i;
558 
559 	for (i = 0; i < sc->sc_numports; i++) {
560 		if (child == sc->sc_ports[i].sc_port_ucom) {
561 			sc->sc_ports[i].sc_port_ucom = NULL;
562 			return;
563 		}
564 	}
565 }
566 
567 static void
568 umcs7840_get_status(void *self, int portno, u_char *lsr, u_char *msr)
569 {
570 	struct umcs7840_softc *sc = self;
571 	uint8_t pn = sc->sc_ports[portno].sc_port_phys;
572 	uint8_t	hw_lsr = 0;	/* local line status register */
573 	uint8_t	hw_msr = 0;	/* local modem status register */
574 
575 	if (sc->sc_dying)
576 		return;
577 
578 	/* Read LSR & MSR */
579 	umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LSR, &hw_lsr);
580 	umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_MSR, &hw_msr);
581 
582 	*lsr = hw_lsr;
583 	*msr = hw_msr;
584 }
585 
586 static void
587 umcs7840_set(void *self, int portno, int reg, int onoff)
588 {
589 	struct umcs7840_softc *sc = self;
590 
591 	if (sc->sc_dying)
592 		return;
593 
594 	switch (reg) {
595 	case UCOM_SET_DTR:
596 		umcs7840_dtr(sc, portno, onoff);
597 		break;
598 	case UCOM_SET_RTS:
599 		umcs7840_rts(sc, portno, onoff);
600 		break;
601 	case UCOM_SET_BREAK:
602 		umcs7840_break(sc, portno, onoff);
603 		break;
604 	default:
605 		break;
606 	}
607 }
608 
609 static int
610 umcs7840_param(void *self, int portno, struct termios *t)
611 {
612 	struct umcs7840_softc *sc = self;
613 	int pn = sc->sc_ports[portno].sc_port_phys;
614 	uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
615 	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
616 
617 	if (t->c_cflag & CSTOPB) {
618 		lcr |= MCS7840_UART_LCR_STOPB2;
619 	} else {
620 		lcr |= MCS7840_UART_LCR_STOPB1;
621 	}
622 
623 	lcr &= ~MCS7840_UART_LCR_PARITYMASK;
624 	if (t->c_cflag & PARENB) {
625 		lcr |= MCS7840_UART_LCR_PARITYON;
626 		if (t->c_cflag & PARODD) {
627 			lcr = MCS7840_UART_LCR_PARITYODD;
628 		} else {
629 			lcr = MCS7840_UART_LCR_PARITYEVEN;
630 		}
631 	} else {
632 		lcr &= ~MCS7840_UART_LCR_PARITYON;
633 	}
634 
635 	lcr &= ~MCS7840_UART_LCR_DATALENMASK;
636 	switch (t->c_cflag & CSIZE) {
637 	case CS5:
638 		lcr |= MCS7840_UART_LCR_DATALEN5;
639 		break;
640 	case CS6:
641 		lcr |= MCS7840_UART_LCR_DATALEN6;
642 		break;
643 	case CS7:
644 		lcr |= MCS7840_UART_LCR_DATALEN7;
645 		break;
646 	case CS8:
647 		lcr |= MCS7840_UART_LCR_DATALEN8;
648 		break;
649 	}
650 
651 	if (t->c_cflag & CRTSCTS)
652 		mcr |= MCS7840_UART_MCR_CTSRTS;
653 	else
654 		mcr &= ~MCS7840_UART_MCR_CTSRTS;
655 
656 	if (t->c_cflag & CLOCAL)
657 		mcr &= ~MCS7840_UART_MCR_DTRDSR;
658 	else
659 		mcr |= MCS7840_UART_MCR_DTRDSR;
660 
661 	sc->sc_ports[portno].sc_port_lcr = lcr;
662 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
663 	    sc->sc_ports[pn].sc_port_lcr);
664 
665 	sc->sc_ports[portno].sc_port_mcr = mcr;
666 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
667 	    sc->sc_ports[pn].sc_port_mcr);
668 
669 	if (umcs7840_set_baudrate(sc, portno, t->c_ospeed))
670 		return EIO;
671 
672 	return 0;
673 }
674 
675 static void
676 umcs7840_dtr(struct umcs7840_softc *sc, int portno, bool onoff)
677 {
678 	int pn = sc->sc_ports[portno].sc_port_phys;
679 	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
680 
681 	if (onoff)
682 		mcr |= MCS7840_UART_MCR_DTR;
683 	else
684 		mcr &= ~MCS7840_UART_MCR_DTR;
685 
686 	sc->sc_ports[portno].sc_port_mcr = mcr;
687 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
688 	    sc->sc_ports[pn].sc_port_mcr);
689 }
690 
691 static void
692 umcs7840_rts(struct umcs7840_softc *sc, int portno, bool onoff)
693 {
694 	int pn = sc->sc_ports[portno].sc_port_phys;
695 	uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
696 
697 	if (onoff)
698 		mcr |= MCS7840_UART_MCR_RTS;
699 	else
700 		mcr &= ~MCS7840_UART_MCR_RTS;
701 
702 	sc->sc_ports[portno].sc_port_mcr = mcr;
703 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
704 	    sc->sc_ports[pn].sc_port_mcr);
705 }
706 
707 static void
708 umcs7840_break(struct umcs7840_softc *sc, int portno, bool onoff)
709 {
710 	int pn = sc->sc_ports[portno].sc_port_phys;
711 	uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
712 
713 	if (onoff)
714 		lcr |= MCS7840_UART_LCR_BREAK;
715 	else
716 		lcr &= ~MCS7840_UART_LCR_BREAK;
717 
718 	sc->sc_ports[portno].sc_port_lcr = lcr;
719 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
720 	    sc->sc_ports[pn].sc_port_lcr);
721 }
722 
723 static int
724 umcs7840_port_open(void *self, int portno)
725 {
726 	struct umcs7840_softc *sc = self;
727 	int pn = sc->sc_ports[portno].sc_port_phys;
728 	int spreg = umcs7840_reg_sp(pn);
729 	int ctrlreg = umcs7840_reg_ctrl(pn);
730 	uint8_t data;
731 
732 	if (sc->sc_dying)
733 		return EIO;
734 
735 	/* If it very first open, finish global configuration */
736 	if (!sc->sc_init_done) {
737 		if (umcs7840_get_reg(sc, MCS7840_DEV_REG_CONTROL1, &data))
738 			return EIO;
739 		data |= MCS7840_DEV_CONTROL1_DRIVER_DONE;
740 		if (umcs7840_set_reg(sc, MCS7840_DEV_REG_CONTROL1, data))
741 			return EIO;
742 		sc->sc_init_done = 1;
743 	}
744 
745 	/* Toggle reset bit on-off */
746 	if (umcs7840_get_reg(sc, spreg, &data))
747 		return EIO;
748 	data |= MCS7840_DEV_SPx_UART_RESET;
749 	if (umcs7840_set_reg(sc, spreg, data))
750 		return EIO;
751 	data &= ~MCS7840_DEV_SPx_UART_RESET;
752 	if (umcs7840_set_reg(sc, spreg, data))
753 		return EIO;
754 
755 	/* Set RS-232 mode */
756 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_SCRATCHPAD,
757 	    MCS7840_UART_SCRATCHPAD_RS232))
758 		return EIO;
759 
760 	/* Disable RX on time of initialization */
761 	if (umcs7840_get_reg(sc, ctrlreg, &data))
762 		return EIO;
763 	data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
764 	if (umcs7840_set_reg(sc, ctrlreg, data))
765 		return EIO;
766 
767 	/* Disable all interrupts */
768 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0))
769 		return EIO;
770 
771 	/* Reset FIFO -- documented */
772 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR, 0))
773 		return EIO;
774 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR,
775 	    MCS7840_UART_FCR_ENABLE | MCS7840_UART_FCR_FLUSHRHR |
776 	    MCS7840_UART_FCR_FLUSHTHR | MCS7840_UART_FCR_RTL_1_14))
777 		return EIO;
778 
779 	/* Set 8 bit, no parity, 1 stop bit -- documented */
780 	sc->sc_ports[pn].sc_port_lcr =
781 	    MCS7840_UART_LCR_DATALEN8 | MCS7840_UART_LCR_STOPB1;
782 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
783 	    sc->sc_ports[pn].sc_port_lcr))
784 		return EIO;
785 
786 	/*
787 	 * Enable DTR/RTS on modem control, enable modem interrupts --
788 	 * documented
789 	 */
790 	sc->sc_ports[pn].sc_port_mcr = MCS7840_UART_MCR_DTR
791 	    | MCS7840_UART_MCR_RTS | MCS7840_UART_MCR_IE;
792 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
793 	    sc->sc_ports[pn].sc_port_mcr))
794 		return EIO;
795 
796 	/* Clearing Bulkin and Bulkout FIFO */
797 	if (umcs7840_get_reg(sc, spreg, &data))
798 		return EIO;
799 	data |= MCS7840_DEV_SPx_RESET_OUT_FIFO | MCS7840_DEV_SPx_RESET_IN_FIFO;
800 	if (umcs7840_set_reg(sc, spreg, data))
801 		return EIO;
802 	data &= ~(MCS7840_DEV_SPx_RESET_OUT_FIFO
803 	    | MCS7840_DEV_SPx_RESET_IN_FIFO);
804 	if (umcs7840_set_reg(sc, spreg, data))
805 		return EIO;
806 
807 	/* Set speed 9600 */
808 	if (umcs7840_set_baudrate(sc, portno, 9600))
809 		return EIO;
810 
811 
812 	/* Finally enable all interrupts -- documented */
813 	/*
814 	 * Copied from vendor driver, I don't know why we should read LCR
815 	 * here
816 	 */
817 	if (umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
818 	    &sc->sc_ports[pn].sc_port_lcr))
819 		return EIO;
820 	if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER,
821 	    MCS7840_UART_IER_RXSTAT | MCS7840_UART_IER_MODEM))
822 		return EIO;
823 
824 	/* Enable RX */
825 	if (umcs7840_get_reg(sc, ctrlreg, &data))
826 		return EIO;
827 	data &= ~MCS7840_DEV_CONTROLx_RX_DISABLE;
828 	if (umcs7840_set_reg(sc, ctrlreg, data))
829 		return EIO;
830 	return 0;
831 }
832 
833 static void
834 umcs7840_port_close(void *self, int portno)
835 {
836 	struct umcs7840_softc *sc = self;
837 	int pn = sc->sc_ports[portno].sc_port_phys;
838 	int ctrlreg = umcs7840_reg_ctrl(pn);
839 	uint8_t data;
840 
841 	if (sc->sc_dying)
842 		return;
843 
844 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR, 0);
845 	umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0);
846 
847 	/* Disable RX */
848 	if (umcs7840_get_reg(sc, ctrlreg, &data))
849 		return;
850 	data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
851 	if (umcs7840_set_reg(sc, ctrlreg, data))
852 		return;
853 }
854 
855 static void
856 umcs7840_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
857     usbd_status status)
858 {
859 	struct umcs7840_softc *sc = priv;
860 	u_char *buf = sc->sc_intr_buf;
861 	int actlen;
862 	int subunit;
863 
864 	if (status == USBD_NOT_STARTED || status == USBD_CANCELLED
865 	    || status == USBD_IOERROR)
866 		return;
867 
868 	if (status != USBD_NORMAL_COMPLETION) {
869 		aprint_error_dev(sc->sc_dev,
870 		    "umcs7840_intr: abnormal status: %s\n",
871 		    usbd_errstr(status));
872 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
873 		return;
874 	}
875 
876 	usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
877 	if (actlen == 5 || actlen == 13) {
878 		uint32_t change_mask = 0;
879 		/* Check status of all ports */
880 		for (subunit = 0; subunit < sc->sc_numports; subunit++) {
881 			uint8_t pn = sc->sc_ports[subunit].sc_port_phys;
882 			if (buf[pn] & MCS7840_UART_ISR_NOPENDING)
883 				continue;
884 			DPRINTF(("Port %d has pending interrupt: %02x "
885 			    "(FIFO: %02x)\n", pn,
886 			    buf[pn] & MCS7840_UART_ISR_INTMASK,
887 			    buf[pn] & (~MCS7840_UART_ISR_INTMASK)));
888 			switch (buf[pn] & MCS7840_UART_ISR_INTMASK) {
889 			case MCS7840_UART_ISR_RXERR:
890 			case MCS7840_UART_ISR_RXHASDATA:
891 			case MCS7840_UART_ISR_RXTIMEOUT:
892 			case MCS7840_UART_ISR_MSCHANGE:
893 				change_mask |= (1U << subunit);
894 				break;
895 			default:
896 				/* Do nothing */
897 				break;
898 			}
899 		}
900 
901 		if (change_mask != 0) {
902 			atomic_or_32(&sc->sc_change_mask, change_mask);
903 			usb_add_task(sc->sc_udev, &sc->sc_change_task,
904 			    USB_TASKQ_DRIVER);
905 		}
906 	} else {
907 		aprint_error_dev(sc->sc_dev,
908 		   "Invalid interrupt data length %d", actlen);
909 	}
910 }
911 
912 static void
913 umcs7840_change_task(void *arg)
914 {
915 	struct umcs7840_softc *sc = arg;
916 	uint32_t change_mask;
917 	int i;
918 
919 	change_mask = atomic_swap_32(&sc->sc_change_mask, 0);
920 	for (i = 0; i < sc->sc_numports; i++) {
921 		if (ISSET(change_mask, (1U << i)))
922 			ucom_status_change(device_private(
923 			    sc->sc_ports[i].sc_port_ucom));
924 	}
925 }
926