xref: /freebsd/sys/dev/usb/serial/umoscom.c (revision c697fb7f)
1 /* $FreeBSD$ */
2 /*	$OpenBSD: umoscom.c,v 1.2 2006/10/26 06:02:43 jsg Exp $	*/
3 
4 /*
5  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/stdint.h>
21 #include <sys/stddef.h>
22 #include <sys/param.h>
23 #include <sys/queue.h>
24 #include <sys/types.h>
25 #include <sys/systm.h>
26 #include <sys/kernel.h>
27 #include <sys/bus.h>
28 #include <sys/module.h>
29 #include <sys/lock.h>
30 #include <sys/mutex.h>
31 #include <sys/condvar.h>
32 #include <sys/sysctl.h>
33 #include <sys/sx.h>
34 #include <sys/unistd.h>
35 #include <sys/callout.h>
36 #include <sys/malloc.h>
37 #include <sys/priv.h>
38 
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41 #include <dev/usb/usbdi_util.h>
42 #include "usbdevs.h"
43 
44 #define	USB_DEBUG_VAR umoscom_debug
45 #include <dev/usb/usb_debug.h>
46 #include <dev/usb/usb_process.h>
47 
48 #include <dev/usb/serial/usb_serial.h>
49 
50 #ifdef USB_DEBUG
51 static int umoscom_debug = 0;
52 
53 static SYSCTL_NODE(_hw_usb, OID_AUTO, umoscom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
54     "USB umoscom");
55 SYSCTL_INT(_hw_usb_umoscom, OID_AUTO, debug, CTLFLAG_RWTUN,
56     &umoscom_debug, 0, "Debug level");
57 #endif
58 
59 #define	UMOSCOM_BUFSIZE	       1024	/* bytes */
60 
61 #define	UMOSCOM_CONFIG_INDEX	0
62 #define	UMOSCOM_IFACE_INDEX	0
63 
64 /* interrupt packet */
65 #define	UMOSCOM_IIR_RLS		0x06
66 #define	UMOSCOM_IIR_RDA		0x04
67 #define	UMOSCOM_IIR_CTI		0x0c
68 #define	UMOSCOM_IIR_THR		0x02
69 #define	UMOSCOM_IIR_MS		0x00
70 
71 /* registers */
72 #define	UMOSCOM_READ		0x0d
73 #define	UMOSCOM_WRITE		0x0e
74 #define	UMOSCOM_UART_REG	0x0300
75 #define	UMOSCOM_VEND_REG	0x0000
76 
77 #define	UMOSCOM_TXBUF		0x00	/* Write */
78 #define	UMOSCOM_RXBUF		0x00	/* Read */
79 #define	UMOSCOM_INT		0x01
80 #define	UMOSCOM_FIFO		0x02	/* Write */
81 #define	UMOSCOM_ISR		0x02	/* Read */
82 #define	UMOSCOM_LCR		0x03
83 #define	UMOSCOM_MCR		0x04
84 #define	UMOSCOM_LSR		0x05
85 #define	UMOSCOM_MSR		0x06
86 #define	UMOSCOM_SCRATCH		0x07
87 #define	UMOSCOM_DIV_LO		0x08
88 #define	UMOSCOM_DIV_HI		0x09
89 #define	UMOSCOM_EFR		0x0a
90 #define	UMOSCOM_XON1		0x0b
91 #define	UMOSCOM_XON2		0x0c
92 #define	UMOSCOM_XOFF1		0x0d
93 #define	UMOSCOM_XOFF2		0x0e
94 
95 #define	UMOSCOM_BAUDLO		0x00
96 #define	UMOSCOM_BAUDHI		0x01
97 
98 #define	UMOSCOM_INT_RXEN	0x01
99 #define	UMOSCOM_INT_TXEN	0x02
100 #define	UMOSCOM_INT_RSEN	0x04
101 #define	UMOSCOM_INT_MDMEM	0x08
102 #define	UMOSCOM_INT_SLEEP	0x10
103 #define	UMOSCOM_INT_XOFF	0x20
104 #define	UMOSCOM_INT_RTS		0x40
105 
106 #define	UMOSCOM_FIFO_EN		0x01
107 #define	UMOSCOM_FIFO_RXCLR	0x02
108 #define	UMOSCOM_FIFO_TXCLR	0x04
109 #define	UMOSCOM_FIFO_DMA_BLK	0x08
110 #define	UMOSCOM_FIFO_TXLVL_MASK	0x30
111 #define	UMOSCOM_FIFO_TXLVL_8	0x00
112 #define	UMOSCOM_FIFO_TXLVL_16	0x10
113 #define	UMOSCOM_FIFO_TXLVL_32	0x20
114 #define	UMOSCOM_FIFO_TXLVL_56	0x30
115 #define	UMOSCOM_FIFO_RXLVL_MASK	0xc0
116 #define	UMOSCOM_FIFO_RXLVL_8	0x00
117 #define	UMOSCOM_FIFO_RXLVL_16	0x40
118 #define	UMOSCOM_FIFO_RXLVL_56	0x80
119 #define	UMOSCOM_FIFO_RXLVL_80	0xc0
120 
121 #define	UMOSCOM_ISR_MDM		0x00
122 #define	UMOSCOM_ISR_NONE	0x01
123 #define	UMOSCOM_ISR_TX		0x02
124 #define	UMOSCOM_ISR_RX		0x04
125 #define	UMOSCOM_ISR_LINE	0x06
126 #define	UMOSCOM_ISR_RXTIMEOUT	0x0c
127 #define	UMOSCOM_ISR_RX_XOFF	0x10
128 #define	UMOSCOM_ISR_RTSCTS	0x20
129 #define	UMOSCOM_ISR_FIFOEN	0xc0
130 
131 #define	UMOSCOM_LCR_DBITS(x)	((x) - 5)
132 #define	UMOSCOM_LCR_STOP_BITS_1	0x00
133 #define	UMOSCOM_LCR_STOP_BITS_2	0x04	/* 2 if 6-8 bits/char or 1.5 if 5 */
134 #define	UMOSCOM_LCR_PARITY_NONE	0x00
135 #define	UMOSCOM_LCR_PARITY_ODD	0x08
136 #define	UMOSCOM_LCR_PARITY_EVEN	0x18
137 #define	UMOSCOM_LCR_BREAK	0x40
138 #define	UMOSCOM_LCR_DIVLATCH_EN	0x80
139 
140 #define	UMOSCOM_MCR_DTR		0x01
141 #define	UMOSCOM_MCR_RTS		0x02
142 #define	UMOSCOM_MCR_LOOP	0x04
143 #define	UMOSCOM_MCR_INTEN	0x08
144 #define	UMOSCOM_MCR_LOOPBACK	0x10
145 #define	UMOSCOM_MCR_XONANY	0x20
146 #define	UMOSCOM_MCR_IRDA_EN	0x40
147 #define	UMOSCOM_MCR_BAUD_DIV4	0x80
148 
149 #define	UMOSCOM_LSR_RXDATA	0x01
150 #define	UMOSCOM_LSR_RXOVER	0x02
151 #define	UMOSCOM_LSR_RXPAR_ERR	0x04
152 #define	UMOSCOM_LSR_RXFRM_ERR	0x08
153 #define	UMOSCOM_LSR_RXBREAK	0x10
154 #define	UMOSCOM_LSR_TXEMPTY	0x20
155 #define	UMOSCOM_LSR_TXALLEMPTY	0x40
156 #define	UMOSCOM_LSR_TXFIFO_ERR	0x80
157 
158 #define	UMOSCOM_MSR_CTS_CHG	0x01
159 #define	UMOSCOM_MSR_DSR_CHG	0x02
160 #define	UMOSCOM_MSR_RI_CHG	0x04
161 #define	UMOSCOM_MSR_CD_CHG	0x08
162 #define	UMOSCOM_MSR_CTS		0x10
163 #define	UMOSCOM_MSR_RTS		0x20
164 #define	UMOSCOM_MSR_RI		0x40
165 #define	UMOSCOM_MSR_CD		0x80
166 
167 #define	UMOSCOM_BAUD_REF	115200
168 
169 enum {
170 	UMOSCOM_BULK_DT_WR,
171 	UMOSCOM_BULK_DT_RD,
172 	UMOSCOM_INTR_DT_RD,
173 	UMOSCOM_N_TRANSFER,
174 };
175 
176 struct umoscom_softc {
177 	struct ucom_super_softc sc_super_ucom;
178 	struct ucom_softc sc_ucom;
179 
180 	struct usb_xfer *sc_xfer[UMOSCOM_N_TRANSFER];
181 	struct usb_device *sc_udev;
182 	struct mtx sc_mtx;
183 
184 	uint8_t	sc_mcr;
185 	uint8_t	sc_lcr;
186 };
187 
188 /* prototypes */
189 
190 static device_probe_t umoscom_probe;
191 static device_attach_t umoscom_attach;
192 static device_detach_t umoscom_detach;
193 static void umoscom_free_softc(struct umoscom_softc *);
194 
195 static usb_callback_t umoscom_write_callback;
196 static usb_callback_t umoscom_read_callback;
197 static usb_callback_t umoscom_intr_callback;
198 
199 static void	umoscom_free(struct ucom_softc *);
200 static void	umoscom_cfg_open(struct ucom_softc *);
201 static void	umoscom_cfg_close(struct ucom_softc *);
202 static void	umoscom_cfg_set_break(struct ucom_softc *, uint8_t);
203 static void	umoscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
204 static void	umoscom_cfg_set_rts(struct ucom_softc *, uint8_t);
205 static int	umoscom_pre_param(struct ucom_softc *, struct termios *);
206 static void	umoscom_cfg_param(struct ucom_softc *, struct termios *);
207 static void	umoscom_cfg_get_status(struct ucom_softc *, uint8_t *,
208 		    uint8_t *);
209 static void	umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
210 static uint8_t	umoscom_cfg_read(struct umoscom_softc *, uint16_t);
211 static void	umoscom_start_read(struct ucom_softc *);
212 static void	umoscom_stop_read(struct ucom_softc *);
213 static void	umoscom_start_write(struct ucom_softc *);
214 static void	umoscom_stop_write(struct ucom_softc *);
215 static void	umoscom_poll(struct ucom_softc *ucom);
216 
217 static const struct usb_config umoscom_config_data[UMOSCOM_N_TRANSFER] = {
218 
219 	[UMOSCOM_BULK_DT_WR] = {
220 		.type = UE_BULK,
221 		.endpoint = UE_ADDR_ANY,
222 		.direction = UE_DIR_OUT,
223 		.bufsize = UMOSCOM_BUFSIZE,
224 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
225 		.callback = &umoscom_write_callback,
226 	},
227 
228 	[UMOSCOM_BULK_DT_RD] = {
229 		.type = UE_BULK,
230 		.endpoint = UE_ADDR_ANY,
231 		.direction = UE_DIR_IN,
232 		.bufsize = UMOSCOM_BUFSIZE,
233 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
234 		.callback = &umoscom_read_callback,
235 	},
236 
237 	[UMOSCOM_INTR_DT_RD] = {
238 		.type = UE_INTERRUPT,
239 		.endpoint = UE_ADDR_ANY,
240 		.direction = UE_DIR_IN,
241 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
242 		.bufsize = 0,	/* use wMaxPacketSize */
243 		.callback = &umoscom_intr_callback,
244 	},
245 };
246 
247 static const struct ucom_callback umoscom_callback = {
248 	/* configuration callbacks */
249 	.ucom_cfg_get_status = &umoscom_cfg_get_status,
250 	.ucom_cfg_set_dtr = &umoscom_cfg_set_dtr,
251 	.ucom_cfg_set_rts = &umoscom_cfg_set_rts,
252 	.ucom_cfg_set_break = &umoscom_cfg_set_break,
253 	.ucom_cfg_param = &umoscom_cfg_param,
254 	.ucom_cfg_open = &umoscom_cfg_open,
255 	.ucom_cfg_close = &umoscom_cfg_close,
256 
257 	/* other callbacks */
258 	.ucom_pre_param = &umoscom_pre_param,
259 	.ucom_start_read = &umoscom_start_read,
260 	.ucom_stop_read = &umoscom_stop_read,
261 	.ucom_start_write = &umoscom_start_write,
262 	.ucom_stop_write = &umoscom_stop_write,
263 	.ucom_poll = &umoscom_poll,
264 	.ucom_free = &umoscom_free,
265 };
266 
267 static device_method_t umoscom_methods[] = {
268 	DEVMETHOD(device_probe, umoscom_probe),
269 	DEVMETHOD(device_attach, umoscom_attach),
270 	DEVMETHOD(device_detach, umoscom_detach),
271 	DEVMETHOD_END
272 };
273 
274 static devclass_t umoscom_devclass;
275 
276 static driver_t umoscom_driver = {
277 	.name = "umoscom",
278 	.methods = umoscom_methods,
279 	.size = sizeof(struct umoscom_softc),
280 };
281 
282 static const STRUCT_USB_HOST_ID umoscom_devs[] = {
283 	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
284 };
285 
286 DRIVER_MODULE(umoscom, uhub, umoscom_driver, umoscom_devclass, NULL, 0);
287 MODULE_DEPEND(umoscom, ucom, 1, 1, 1);
288 MODULE_DEPEND(umoscom, usb, 1, 1, 1);
289 MODULE_VERSION(umoscom, 1);
290 USB_PNP_HOST_INFO(umoscom_devs);
291 
292 static int
293 umoscom_probe(device_t dev)
294 {
295 	struct usb_attach_arg *uaa = device_get_ivars(dev);
296 
297 	if (uaa->usb_mode != USB_MODE_HOST) {
298 		return (ENXIO);
299 	}
300 	if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) {
301 		return (ENXIO);
302 	}
303 	if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
304 		return (ENXIO);
305 	}
306 	return (usbd_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
307 }
308 
309 static int
310 umoscom_attach(device_t dev)
311 {
312 	struct usb_attach_arg *uaa = device_get_ivars(dev);
313 	struct umoscom_softc *sc = device_get_softc(dev);
314 	int error;
315 	uint8_t iface_index;
316 
317 	sc->sc_udev = uaa->device;
318 	sc->sc_mcr = 0x08;		/* enable interrupts */
319 
320 	/* XXX the device doesn't provide any ID string, so set a static one */
321 	device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
322 	device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");
323 
324 	mtx_init(&sc->sc_mtx, "umoscom", NULL, MTX_DEF);
325 	ucom_ref(&sc->sc_super_ucom);
326 
327 	iface_index = UMOSCOM_IFACE_INDEX;
328 	error = usbd_transfer_setup(uaa->device, &iface_index,
329 	    sc->sc_xfer, umoscom_config_data,
330 	    UMOSCOM_N_TRANSFER, sc, &sc->sc_mtx);
331 
332 	if (error) {
333 		goto detach;
334 	}
335 	/* clear stall at first run */
336 	mtx_lock(&sc->sc_mtx);
337 	usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
338 	usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
339 	mtx_unlock(&sc->sc_mtx);
340 
341 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
342 	    &umoscom_callback, &sc->sc_mtx);
343 	if (error) {
344 		goto detach;
345 	}
346 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
347 
348 	return (0);
349 
350 detach:
351 	device_printf(dev, "attach error: %s\n", usbd_errstr(error));
352 	umoscom_detach(dev);
353 	return (ENXIO);
354 }
355 
356 static int
357 umoscom_detach(device_t dev)
358 {
359 	struct umoscom_softc *sc = device_get_softc(dev);
360 
361 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
362 	usbd_transfer_unsetup(sc->sc_xfer, UMOSCOM_N_TRANSFER);
363 
364 	device_claim_softc(dev);
365 
366 	umoscom_free_softc(sc);
367 
368 	return (0);
369 }
370 
371 UCOM_UNLOAD_DRAIN(umoscom);
372 
373 static void
374 umoscom_free_softc(struct umoscom_softc *sc)
375 {
376 	if (ucom_unref(&sc->sc_super_ucom)) {
377 		mtx_destroy(&sc->sc_mtx);
378 		device_free_softc(sc);
379 	}
380 }
381 
382 static void
383 umoscom_free(struct ucom_softc *ucom)
384 {
385 	umoscom_free_softc(ucom->sc_parent);
386 }
387 
388 static void
389 umoscom_cfg_open(struct ucom_softc *ucom)
390 {
391 	struct umoscom_softc *sc = ucom->sc_parent;
392 
393 	DPRINTF("\n");
394 
395 	/* Purge FIFOs or odd things happen */
396 	umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);
397 
398 	/* Enable FIFO */
399 	umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
400 	    UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
401 	    UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
402 	    UMOSCOM_UART_REG);
403 
404 	/* Enable Interrupt Registers */
405 	umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);
406 
407 	/* Magic */
408 	umoscom_cfg_write(sc, 0x01, 0x08);
409 
410 	/* Magic */
411 	umoscom_cfg_write(sc, 0x00, 0x02);
412 }
413 
414 static void
415 umoscom_cfg_close(struct ucom_softc *ucom)
416 {
417 	return;
418 }
419 
420 static void
421 umoscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
422 {
423 	struct umoscom_softc *sc = ucom->sc_parent;
424 	uint16_t val;
425 
426 	val = sc->sc_lcr;
427 	if (onoff)
428 		val |= UMOSCOM_LCR_BREAK;
429 
430 	umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
431 }
432 
433 static void
434 umoscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
435 {
436 	struct umoscom_softc *sc = ucom->sc_parent;
437 
438 	if (onoff)
439 		sc->sc_mcr |= UMOSCOM_MCR_DTR;
440 	else
441 		sc->sc_mcr &= ~UMOSCOM_MCR_DTR;
442 
443 	umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
444 }
445 
446 static void
447 umoscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
448 {
449 	struct umoscom_softc *sc = ucom->sc_parent;
450 
451 	if (onoff)
452 		sc->sc_mcr |= UMOSCOM_MCR_RTS;
453 	else
454 		sc->sc_mcr &= ~UMOSCOM_MCR_RTS;
455 
456 	umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
457 }
458 
459 static int
460 umoscom_pre_param(struct ucom_softc *ucom, struct termios *t)
461 {
462 	if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
463 		return (EINVAL);
464 
465 	return (0);
466 }
467 
468 static void
469 umoscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
470 {
471 	struct umoscom_softc *sc = ucom->sc_parent;
472 	uint16_t data;
473 
474 	DPRINTF("speed=%d\n", t->c_ospeed);
475 
476 	data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);
477 
478 	if (data == 0) {
479 		DPRINTF("invalid baud rate!\n");
480 		return;
481 	}
482 	umoscom_cfg_write(sc, UMOSCOM_LCR,
483 	    UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);
484 
485 	umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
486 	    (data & 0xFF) | UMOSCOM_UART_REG);
487 
488 	umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
489 	    ((data >> 8) & 0xFF) | UMOSCOM_UART_REG);
490 
491 	if (t->c_cflag & CSTOPB)
492 		data = UMOSCOM_LCR_STOP_BITS_2;
493 	else
494 		data = UMOSCOM_LCR_STOP_BITS_1;
495 
496 	if (t->c_cflag & PARENB) {
497 		if (t->c_cflag & PARODD)
498 			data |= UMOSCOM_LCR_PARITY_ODD;
499 		else
500 			data |= UMOSCOM_LCR_PARITY_EVEN;
501 	} else
502 		data |= UMOSCOM_LCR_PARITY_NONE;
503 
504 	switch (t->c_cflag & CSIZE) {
505 	case CS5:
506 		data |= UMOSCOM_LCR_DBITS(5);
507 		break;
508 	case CS6:
509 		data |= UMOSCOM_LCR_DBITS(6);
510 		break;
511 	case CS7:
512 		data |= UMOSCOM_LCR_DBITS(7);
513 		break;
514 	case CS8:
515 		data |= UMOSCOM_LCR_DBITS(8);
516 		break;
517 	}
518 
519 	sc->sc_lcr = data;
520 	umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
521 }
522 
523 static void
524 umoscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
525 {
526 	struct umoscom_softc *sc = ucom->sc_parent;
527 	uint8_t msr;
528 
529 	DPRINTFN(5, "\n");
530 
531 	/*
532 	 * Read status registers.  MSR bits need translation from ns16550 to
533 	 * SER_* values.  LSR bits are ns16550 in hardware and ucom.
534 	 */
535 
536 	*p_lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
537 	msr = umoscom_cfg_read(sc, UMOSCOM_MSR);
538 
539 	/* translate bits */
540 
541 	if (msr & UMOSCOM_MSR_CTS)
542 		*p_msr |= SER_CTS;
543 
544 	if (msr & UMOSCOM_MSR_CD)
545 		*p_msr |= SER_DCD;
546 
547 	if (msr & UMOSCOM_MSR_RI)
548 		*p_msr |= SER_RI;
549 
550 	if (msr & UMOSCOM_MSR_RTS)
551 		*p_msr |= SER_DSR;
552 }
553 
554 static void
555 umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
556 {
557 	struct usb_device_request req;
558 
559 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
560 	req.bRequest = UMOSCOM_WRITE;
561 	USETW(req.wValue, val);
562 	USETW(req.wIndex, reg);
563 	USETW(req.wLength, 0);
564 
565 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
566 	    &req, NULL, 0, 1000);
567 }
568 
569 static uint8_t
570 umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
571 {
572 	struct usb_device_request req;
573 	uint8_t val;
574 
575 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
576 	req.bRequest = UMOSCOM_READ;
577 	USETW(req.wValue, 0);
578 	USETW(req.wIndex, reg);
579 	USETW(req.wLength, 1);
580 
581 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
582 	    &req, &val, 0, 1000);
583 
584 	DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);
585 
586 	return (val);
587 }
588 
589 static void
590 umoscom_start_read(struct ucom_softc *ucom)
591 {
592 	struct umoscom_softc *sc = ucom->sc_parent;
593 
594 #if 0
595 	/* start interrupt endpoint */
596 	usbd_transfer_start(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
597 #endif
598 	/* start read endpoint */
599 	usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
600 }
601 
602 static void
603 umoscom_stop_read(struct ucom_softc *ucom)
604 {
605 	struct umoscom_softc *sc = ucom->sc_parent;
606 
607 	/* stop interrupt transfer */
608 	usbd_transfer_stop(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
609 
610 	/* stop read endpoint */
611 	usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
612 }
613 
614 static void
615 umoscom_start_write(struct ucom_softc *ucom)
616 {
617 	struct umoscom_softc *sc = ucom->sc_parent;
618 
619 	usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
620 }
621 
622 static void
623 umoscom_stop_write(struct ucom_softc *ucom)
624 {
625 	struct umoscom_softc *sc = ucom->sc_parent;
626 
627 	usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
628 }
629 
630 static void
631 umoscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
632 {
633 	struct umoscom_softc *sc = usbd_xfer_softc(xfer);
634 	struct usb_page_cache *pc;
635 	uint32_t actlen;
636 
637 	switch (USB_GET_STATE(xfer)) {
638 	case USB_ST_SETUP:
639 	case USB_ST_TRANSFERRED:
640 tr_setup:
641 		DPRINTF("\n");
642 
643 		pc = usbd_xfer_get_frame(xfer, 0);
644 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
645 		    UMOSCOM_BUFSIZE, &actlen)) {
646 
647 			usbd_xfer_set_frame_len(xfer, 0, actlen);
648 			usbd_transfer_submit(xfer);
649 		}
650 		return;
651 
652 	default:			/* Error */
653 		if (error != USB_ERR_CANCELLED) {
654 			DPRINTFN(0, "transfer failed\n");
655 			/* try to clear stall first */
656 			usbd_xfer_set_stall(xfer);
657 			goto tr_setup;
658 		}
659 		return;
660 	}
661 }
662 
663 static void
664 umoscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
665 {
666 	struct umoscom_softc *sc = usbd_xfer_softc(xfer);
667 	struct usb_page_cache *pc;
668 	int actlen;
669 
670 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
671 
672 	switch (USB_GET_STATE(xfer)) {
673 	case USB_ST_TRANSFERRED:
674 		DPRINTF("got %d bytes\n", actlen);
675 		pc = usbd_xfer_get_frame(xfer, 0);
676 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
677 
678 	case USB_ST_SETUP:
679 tr_setup:
680 		DPRINTF("\n");
681 
682 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
683 		usbd_transfer_submit(xfer);
684 		return;
685 
686 	default:			/* Error */
687 		if (error != USB_ERR_CANCELLED) {
688 			DPRINTFN(0, "transfer failed\n");
689 			/* try to clear stall first */
690 			usbd_xfer_set_stall(xfer);
691 			goto tr_setup;
692 		}
693 		return;
694 	}
695 }
696 
697 static void
698 umoscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
699 {
700 	struct umoscom_softc *sc = usbd_xfer_softc(xfer);
701 	int actlen;
702 
703 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
704 
705 	switch (USB_GET_STATE(xfer)) {
706 	case USB_ST_TRANSFERRED:
707 		if (actlen < 2) {
708 			DPRINTF("too short message\n");
709 			goto tr_setup;
710 		}
711 		ucom_status_change(&sc->sc_ucom);
712 
713 	case USB_ST_SETUP:
714 tr_setup:
715 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
716 		usbd_transfer_submit(xfer);
717 		return;
718 
719 	default:			/* Error */
720 		if (error != USB_ERR_CANCELLED) {
721 			DPRINTFN(0, "transfer failed\n");
722 			/* try to clear stall first */
723 			usbd_xfer_set_stall(xfer);
724 			goto tr_setup;
725 		}
726 		return;
727 	}
728 }
729 
730 static void
731 umoscom_poll(struct ucom_softc *ucom)
732 {
733 	struct umoscom_softc *sc = ucom->sc_parent;
734 	usbd_transfer_poll(sc->sc_xfer, UMOSCOM_N_TRANSFER);
735 }
736