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