xref: /freebsd/sys/dev/usb/serial/umoscom.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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 "usbdevs.h"
21 #include <dev/usb/usb.h>
22 #include <dev/usb/usb_mfunc.h>
23 #include <dev/usb/usb_error.h>
24 #include <dev/usb/usb_cdc.h>
25 
26 #define	USB_DEBUG_VAR umoscom_debug
27 
28 #include <dev/usb/usb_core.h>
29 #include <dev/usb/usb_debug.h>
30 #include <dev/usb/usb_process.h>
31 #include <dev/usb/usb_request.h>
32 #include <dev/usb/usb_lookup.h>
33 #include <dev/usb/usb_util.h>
34 #include <dev/usb/usb_busdma.h>
35 
36 #include <dev/usb/serial/usb_serial.h>
37 
38 #if USB_DEBUG
39 static int umoscom_debug = 0;
40 
41 SYSCTL_NODE(_hw_usb, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom");
42 SYSCTL_INT(_hw_usb_umoscom, OID_AUTO, debug, CTLFLAG_RW,
43     &umoscom_debug, 0, "Debug level");
44 #endif
45 
46 #define	UMOSCOM_BUFSIZE	       1024	/* bytes */
47 
48 #define	UMOSCOM_CONFIG_INDEX	0
49 #define	UMOSCOM_IFACE_INDEX	0
50 
51 /* interrupt packet */
52 #define	UMOSCOM_IIR_RLS		0x06
53 #define	UMOSCOM_IIR_RDA		0x04
54 #define	UMOSCOM_IIR_CTI		0x0c
55 #define	UMOSCOM_IIR_THR		0x02
56 #define	UMOSCOM_IIR_MS		0x00
57 
58 /* registers */
59 #define	UMOSCOM_READ		0x0d
60 #define	UMOSCOM_WRITE		0x0e
61 #define	UMOSCOM_UART_REG	0x0300
62 #define	UMOSCOM_VEND_REG	0x0000
63 
64 #define	UMOSCOM_TXBUF		0x00	/* Write */
65 #define	UMOSCOM_RXBUF		0x00	/* Read */
66 #define	UMOSCOM_INT		0x01
67 #define	UMOSCOM_FIFO		0x02	/* Write */
68 #define	UMOSCOM_ISR		0x02	/* Read */
69 #define	UMOSCOM_LCR		0x03
70 #define	UMOSCOM_MCR		0x04
71 #define	UMOSCOM_LSR		0x05
72 #define	UMOSCOM_MSR		0x06
73 #define	UMOSCOM_SCRATCH		0x07
74 #define	UMOSCOM_DIV_LO		0x08
75 #define	UMOSCOM_DIV_HI		0x09
76 #define	UMOSCOM_EFR		0x0a
77 #define	UMOSCOM_XON1		0x0b
78 #define	UMOSCOM_XON2		0x0c
79 #define	UMOSCOM_XOFF1		0x0d
80 #define	UMOSCOM_XOFF2		0x0e
81 
82 #define	UMOSCOM_BAUDLO		0x00
83 #define	UMOSCOM_BAUDHI		0x01
84 
85 #define	UMOSCOM_INT_RXEN	0x01
86 #define	UMOSCOM_INT_TXEN	0x02
87 #define	UMOSCOM_INT_RSEN	0x04
88 #define	UMOSCOM_INT_MDMEM	0x08
89 #define	UMOSCOM_INT_SLEEP	0x10
90 #define	UMOSCOM_INT_XOFF	0x20
91 #define	UMOSCOM_INT_RTS		0x40
92 
93 #define	UMOSCOM_FIFO_EN		0x01
94 #define	UMOSCOM_FIFO_RXCLR	0x02
95 #define	UMOSCOM_FIFO_TXCLR	0x04
96 #define	UMOSCOM_FIFO_DMA_BLK	0x08
97 #define	UMOSCOM_FIFO_TXLVL_MASK	0x30
98 #define	UMOSCOM_FIFO_TXLVL_8	0x00
99 #define	UMOSCOM_FIFO_TXLVL_16	0x10
100 #define	UMOSCOM_FIFO_TXLVL_32	0x20
101 #define	UMOSCOM_FIFO_TXLVL_56	0x30
102 #define	UMOSCOM_FIFO_RXLVL_MASK	0xc0
103 #define	UMOSCOM_FIFO_RXLVL_8	0x00
104 #define	UMOSCOM_FIFO_RXLVL_16	0x40
105 #define	UMOSCOM_FIFO_RXLVL_56	0x80
106 #define	UMOSCOM_FIFO_RXLVL_80	0xc0
107 
108 #define	UMOSCOM_ISR_MDM		0x00
109 #define	UMOSCOM_ISR_NONE	0x01
110 #define	UMOSCOM_ISR_TX		0x02
111 #define	UMOSCOM_ISR_RX		0x04
112 #define	UMOSCOM_ISR_LINE	0x06
113 #define	UMOSCOM_ISR_RXTIMEOUT	0x0c
114 #define	UMOSCOM_ISR_RX_XOFF	0x10
115 #define	UMOSCOM_ISR_RTSCTS	0x20
116 #define	UMOSCOM_ISR_FIFOEN	0xc0
117 
118 #define	UMOSCOM_LCR_DBITS(x)	((x) - 5)
119 #define	UMOSCOM_LCR_STOP_BITS_1	0x00
120 #define	UMOSCOM_LCR_STOP_BITS_2	0x04	/* 2 if 6-8 bits/char or 1.5 if 5 */
121 #define	UMOSCOM_LCR_PARITY_NONE	0x00
122 #define	UMOSCOM_LCR_PARITY_ODD	0x08
123 #define	UMOSCOM_LCR_PARITY_EVEN	0x18
124 #define	UMOSCOM_LCR_BREAK	0x40
125 #define	UMOSCOM_LCR_DIVLATCH_EN	0x80
126 
127 #define	UMOSCOM_MCR_DTR		0x01
128 #define	UMOSCOM_MCR_RTS		0x02
129 #define	UMOSCOM_MCR_LOOP	0x04
130 #define	UMOSCOM_MCR_INTEN	0x08
131 #define	UMOSCOM_MCR_LOOPBACK	0x10
132 #define	UMOSCOM_MCR_XONANY	0x20
133 #define	UMOSCOM_MCR_IRDA_EN	0x40
134 #define	UMOSCOM_MCR_BAUD_DIV4	0x80
135 
136 #define	UMOSCOM_LSR_RXDATA	0x01
137 #define	UMOSCOM_LSR_RXOVER	0x02
138 #define	UMOSCOM_LSR_RXPAR_ERR	0x04
139 #define	UMOSCOM_LSR_RXFRM_ERR	0x08
140 #define	UMOSCOM_LSR_RXBREAK	0x10
141 #define	UMOSCOM_LSR_TXEMPTY	0x20
142 #define	UMOSCOM_LSR_TXALLEMPTY	0x40
143 #define	UMOSCOM_LSR_TXFIFO_ERR	0x80
144 
145 #define	UMOSCOM_MSR_CTS_CHG	0x01
146 #define	UMOSCOM_MSR_DSR_CHG	0x02
147 #define	UMOSCOM_MSR_RI_CHG	0x04
148 #define	UMOSCOM_MSR_CD_CHG	0x08
149 #define	UMOSCOM_MSR_CTS		0x10
150 #define	UMOSCOM_MSR_RTS		0x20
151 #define	UMOSCOM_MSR_RI		0x40
152 #define	UMOSCOM_MSR_CD		0x80
153 
154 #define	UMOSCOM_BAUD_REF	115200
155 
156 enum {
157 	UMOSCOM_BULK_DT_WR,
158 	UMOSCOM_BULK_DT_RD,
159 	UMOSCOM_INTR_DT_RD,
160 	UMOSCOM_N_TRANSFER,
161 };
162 
163 struct umoscom_softc {
164 	struct ucom_super_softc sc_super_ucom;
165 	struct ucom_softc sc_ucom;
166 
167 	struct usb_xfer *sc_xfer[UMOSCOM_N_TRANSFER];
168 	struct usb_device *sc_udev;
169 	struct mtx sc_mtx;
170 
171 	uint8_t	sc_mcr;
172 	uint8_t	sc_lcr;
173 };
174 
175 /* prototypes */
176 
177 static device_probe_t umoscom_probe;
178 static device_attach_t umoscom_attach;
179 static device_detach_t umoscom_detach;
180 
181 static usb_callback_t umoscom_write_callback;
182 static usb_callback_t umoscom_read_callback;
183 static usb_callback_t umoscom_intr_callback;
184 
185 static void	umoscom_cfg_open(struct ucom_softc *);
186 static void	umoscom_cfg_close(struct ucom_softc *);
187 static void	umoscom_cfg_set_break(struct ucom_softc *, uint8_t);
188 static void	umoscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
189 static void	umoscom_cfg_set_rts(struct ucom_softc *, uint8_t);
190 static int	umoscom_pre_param(struct ucom_softc *, struct termios *);
191 static void	umoscom_cfg_param(struct ucom_softc *, struct termios *);
192 static void	umoscom_cfg_get_status(struct ucom_softc *, uint8_t *,
193 		    uint8_t *);
194 static void	umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
195 static uint8_t	umoscom_cfg_read(struct umoscom_softc *, uint16_t);
196 static void	umoscom_start_read(struct ucom_softc *);
197 static void	umoscom_stop_read(struct ucom_softc *);
198 static void	umoscom_start_write(struct ucom_softc *);
199 static void	umoscom_stop_write(struct ucom_softc *);
200 
201 static const struct usb_config umoscom_config_data[UMOSCOM_N_TRANSFER] = {
202 
203 	[UMOSCOM_BULK_DT_WR] = {
204 		.type = UE_BULK,
205 		.endpoint = UE_ADDR_ANY,
206 		.direction = UE_DIR_OUT,
207 		.bufsize = UMOSCOM_BUFSIZE,
208 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
209 		.callback = &umoscom_write_callback,
210 	},
211 
212 	[UMOSCOM_BULK_DT_RD] = {
213 		.type = UE_BULK,
214 		.endpoint = UE_ADDR_ANY,
215 		.direction = UE_DIR_IN,
216 		.bufsize = UMOSCOM_BUFSIZE,
217 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
218 		.callback = &umoscom_read_callback,
219 	},
220 
221 	[UMOSCOM_INTR_DT_RD] = {
222 		.type = UE_INTERRUPT,
223 		.endpoint = UE_ADDR_ANY,
224 		.direction = UE_DIR_IN,
225 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
226 		.bufsize = 0,	/* use wMaxPacketSize */
227 		.callback = &umoscom_intr_callback,
228 	},
229 };
230 
231 static const struct ucom_callback umoscom_callback = {
232 	/* configuration callbacks */
233 	.usb2_com_cfg_get_status = &umoscom_cfg_get_status,
234 	.usb2_com_cfg_set_dtr = &umoscom_cfg_set_dtr,
235 	.usb2_com_cfg_set_rts = &umoscom_cfg_set_rts,
236 	.usb2_com_cfg_set_break = &umoscom_cfg_set_break,
237 	.usb2_com_cfg_param = &umoscom_cfg_param,
238 	.usb2_com_cfg_open = &umoscom_cfg_open,
239 	.usb2_com_cfg_close = &umoscom_cfg_close,
240 
241 	/* other callbacks */
242 	.usb2_com_pre_param = &umoscom_pre_param,
243 	.usb2_com_start_read = &umoscom_start_read,
244 	.usb2_com_stop_read = &umoscom_stop_read,
245 	.usb2_com_start_write = &umoscom_start_write,
246 	.usb2_com_stop_write = &umoscom_stop_write,
247 };
248 
249 static device_method_t umoscom_methods[] = {
250 	DEVMETHOD(device_probe, umoscom_probe),
251 	DEVMETHOD(device_attach, umoscom_attach),
252 	DEVMETHOD(device_detach, umoscom_detach),
253 	{0, 0}
254 };
255 
256 static devclass_t umoscom_devclass;
257 
258 static driver_t umoscom_driver = {
259 	.name = "umoscom",
260 	.methods = umoscom_methods,
261 	.size = sizeof(struct umoscom_softc),
262 };
263 
264 DRIVER_MODULE(umoscom, uhub, umoscom_driver, umoscom_devclass, NULL, 0);
265 MODULE_DEPEND(umoscom, ucom, 1, 1, 1);
266 MODULE_DEPEND(umoscom, usb, 1, 1, 1);
267 
268 static const struct usb_device_id umoscom_devs[] = {
269 	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
270 };
271 
272 static int
273 umoscom_probe(device_t dev)
274 {
275 	struct usb_attach_arg *uaa = device_get_ivars(dev);
276 
277 	if (uaa->usb_mode != USB_MODE_HOST) {
278 		return (ENXIO);
279 	}
280 	if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) {
281 		return (ENXIO);
282 	}
283 	if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
284 		return (ENXIO);
285 	}
286 	return (usb2_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
287 }
288 
289 static int
290 umoscom_attach(device_t dev)
291 {
292 	struct usb_attach_arg *uaa = device_get_ivars(dev);
293 	struct umoscom_softc *sc = device_get_softc(dev);
294 	int error;
295 	uint8_t iface_index;
296 
297 	sc->sc_udev = uaa->device;
298 	sc->sc_mcr = 0x08;		/* enable interrupts */
299 
300 	/* XXX the device doesn't provide any ID string, so set a static one */
301 	device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
302 	device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");
303 
304 	mtx_init(&sc->sc_mtx, "umoscom", NULL, MTX_DEF);
305 
306 	iface_index = UMOSCOM_IFACE_INDEX;
307 	error = usb2_transfer_setup(uaa->device, &iface_index,
308 	    sc->sc_xfer, umoscom_config_data,
309 	    UMOSCOM_N_TRANSFER, sc, &sc->sc_mtx);
310 
311 	if (error) {
312 		goto detach;
313 	}
314 	/* clear stall at first run */
315 	mtx_lock(&sc->sc_mtx);
316 	usb2_transfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
317 	usb2_transfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
318 	mtx_unlock(&sc->sc_mtx);
319 
320 	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
321 	    &umoscom_callback, &sc->sc_mtx);
322 	if (error) {
323 		goto detach;
324 	}
325 	return (0);
326 
327 detach:
328 	device_printf(dev, "attach error: %s\n", usb2_errstr(error));
329 	umoscom_detach(dev);
330 	return (ENXIO);
331 }
332 
333 static int
334 umoscom_detach(device_t dev)
335 {
336 	struct umoscom_softc *sc = device_get_softc(dev);
337 
338 	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
339 	usb2_transfer_unsetup(sc->sc_xfer, UMOSCOM_N_TRANSFER);
340 	mtx_destroy(&sc->sc_mtx);
341 
342 	return (0);
343 }
344 
345 static void
346 umoscom_cfg_open(struct ucom_softc *ucom)
347 {
348 	struct umoscom_softc *sc = ucom->sc_parent;
349 
350 	DPRINTF("\n");
351 
352 	/* Purge FIFOs or odd things happen */
353 	umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);
354 
355 	/* Enable FIFO */
356 	umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
357 	    UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
358 	    UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
359 	    UMOSCOM_UART_REG);
360 
361 	/* Enable Interrupt Registers */
362 	umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);
363 
364 	/* Magic */
365 	umoscom_cfg_write(sc, 0x01, 0x08);
366 
367 	/* Magic */
368 	umoscom_cfg_write(sc, 0x00, 0x02);
369 }
370 
371 static void
372 umoscom_cfg_close(struct ucom_softc *ucom)
373 {
374 	return;
375 }
376 
377 static void
378 umoscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
379 {
380 	struct umoscom_softc *sc = ucom->sc_parent;
381 	uint16_t val;
382 
383 	val = sc->sc_lcr;
384 	if (onoff)
385 		val |= UMOSCOM_LCR_BREAK;
386 
387 	umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
388 }
389 
390 static void
391 umoscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
392 {
393 	struct umoscom_softc *sc = ucom->sc_parent;
394 
395 	if (onoff)
396 		sc->sc_mcr |= UMOSCOM_MCR_DTR;
397 	else
398 		sc->sc_mcr &= ~UMOSCOM_MCR_DTR;
399 
400 	umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
401 }
402 
403 static void
404 umoscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
405 {
406 	struct umoscom_softc *sc = ucom->sc_parent;
407 
408 	if (onoff)
409 		sc->sc_mcr |= UMOSCOM_MCR_RTS;
410 	else
411 		sc->sc_mcr &= ~UMOSCOM_MCR_RTS;
412 
413 	umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
414 }
415 
416 static int
417 umoscom_pre_param(struct ucom_softc *ucom, struct termios *t)
418 {
419 	if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
420 		return (EINVAL);
421 
422 	return (0);
423 }
424 
425 static void
426 umoscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
427 {
428 	struct umoscom_softc *sc = ucom->sc_parent;
429 	uint16_t data;
430 
431 	DPRINTF("speed=%d\n", t->c_ospeed);
432 
433 	data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);
434 
435 	if (data == 0) {
436 		DPRINTF("invalid baud rate!\n");
437 		return;
438 	}
439 	umoscom_cfg_write(sc, UMOSCOM_LCR,
440 	    UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);
441 
442 	umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
443 	    (data & 0xFF) | UMOSCOM_UART_REG);
444 
445 	umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
446 	    ((data >> 8) & 0xFF) | UMOSCOM_UART_REG);
447 
448 	if (t->c_cflag & CSTOPB)
449 		data = UMOSCOM_LCR_STOP_BITS_2;
450 	else
451 		data = UMOSCOM_LCR_STOP_BITS_1;
452 
453 	if (t->c_cflag & PARENB) {
454 		if (t->c_cflag & PARODD)
455 			data |= UMOSCOM_LCR_PARITY_ODD;
456 		else
457 			data |= UMOSCOM_LCR_PARITY_EVEN;
458 	} else
459 		data |= UMOSCOM_LCR_PARITY_NONE;
460 
461 	switch (t->c_cflag & CSIZE) {
462 	case CS5:
463 		data |= UMOSCOM_LCR_DBITS(5);
464 		break;
465 	case CS6:
466 		data |= UMOSCOM_LCR_DBITS(6);
467 		break;
468 	case CS7:
469 		data |= UMOSCOM_LCR_DBITS(7);
470 		break;
471 	case CS8:
472 		data |= UMOSCOM_LCR_DBITS(8);
473 		break;
474 	}
475 
476 	sc->sc_lcr = data;
477 	umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
478 }
479 
480 static void
481 umoscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
482 {
483 	struct umoscom_softc *sc = ucom->sc_parent;
484 	uint8_t lsr;
485 	uint8_t msr;
486 
487 	DPRINTFN(5, "\n");
488 
489 	/* read status registers */
490 
491 	lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
492 	msr = umoscom_cfg_read(sc, UMOSCOM_MSR);
493 
494 	/* translate bits */
495 
496 	if (msr & UMOSCOM_MSR_CTS)
497 		*p_msr |= SER_CTS;
498 
499 	if (msr & UMOSCOM_MSR_CD)
500 		*p_msr |= SER_DCD;
501 
502 	if (msr & UMOSCOM_MSR_RI)
503 		*p_msr |= SER_RI;
504 
505 	if (msr & UMOSCOM_MSR_RTS)
506 		*p_msr |= SER_DSR;
507 }
508 
509 static void
510 umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
511 {
512 	struct usb_device_request req;
513 
514 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
515 	req.bRequest = UMOSCOM_WRITE;
516 	USETW(req.wValue, val);
517 	USETW(req.wIndex, reg);
518 	USETW(req.wLength, 0);
519 
520 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
521 	    &req, NULL, 0, 1000);
522 }
523 
524 static uint8_t
525 umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
526 {
527 	struct usb_device_request req;
528 	uint8_t val;
529 
530 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
531 	req.bRequest = UMOSCOM_READ;
532 	USETW(req.wValue, 0);
533 	USETW(req.wIndex, reg);
534 	USETW(req.wLength, 1);
535 
536 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
537 	    &req, &val, 0, 1000);
538 
539 	DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);
540 
541 	return (val);
542 }
543 
544 static void
545 umoscom_start_read(struct ucom_softc *ucom)
546 {
547 	struct umoscom_softc *sc = ucom->sc_parent;
548 
549 #if 0
550 	/* start interrupt endpoint */
551 	usb2_transfer_start(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
552 #endif
553 	/* start read endpoint */
554 	usb2_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
555 }
556 
557 static void
558 umoscom_stop_read(struct ucom_softc *ucom)
559 {
560 	struct umoscom_softc *sc = ucom->sc_parent;
561 
562 	/* stop interrupt transfer */
563 	usb2_transfer_stop(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
564 
565 	/* stop read endpoint */
566 	usb2_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
567 }
568 
569 static void
570 umoscom_start_write(struct ucom_softc *ucom)
571 {
572 	struct umoscom_softc *sc = ucom->sc_parent;
573 
574 	usb2_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
575 }
576 
577 static void
578 umoscom_stop_write(struct ucom_softc *ucom)
579 {
580 	struct umoscom_softc *sc = ucom->sc_parent;
581 
582 	usb2_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
583 }
584 
585 static void
586 umoscom_write_callback(struct usb_xfer *xfer)
587 {
588 	struct umoscom_softc *sc = xfer->priv_sc;
589 	uint32_t actlen;
590 
591 	switch (USB_GET_STATE(xfer)) {
592 	case USB_ST_SETUP:
593 	case USB_ST_TRANSFERRED:
594 tr_setup:
595 		DPRINTF("\n");
596 
597 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
598 		    UMOSCOM_BUFSIZE, &actlen)) {
599 
600 			xfer->frlengths[0] = actlen;
601 			usb2_start_hardware(xfer);
602 		}
603 		return;
604 
605 	default:			/* Error */
606 		if (xfer->error != USB_ERR_CANCELLED) {
607 			DPRINTFN(0, "transfer failed\n");
608 			/* try to clear stall first */
609 			xfer->flags.stall_pipe = 1;
610 			goto tr_setup;
611 		}
612 		return;
613 	}
614 }
615 
616 static void
617 umoscom_read_callback(struct usb_xfer *xfer)
618 {
619 	struct umoscom_softc *sc = xfer->priv_sc;
620 
621 	switch (USB_GET_STATE(xfer)) {
622 	case USB_ST_TRANSFERRED:
623 		DPRINTF("got %d bytes\n", xfer->actlen);
624 		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
625 
626 	case USB_ST_SETUP:
627 tr_setup:
628 		DPRINTF("\n");
629 
630 		xfer->frlengths[0] = xfer->max_data_length;
631 		usb2_start_hardware(xfer);
632 		return;
633 
634 	default:			/* Error */
635 		if (xfer->error != USB_ERR_CANCELLED) {
636 			DPRINTFN(0, "transfer failed\n");
637 			/* try to clear stall first */
638 			xfer->flags.stall_pipe = 1;
639 			goto tr_setup;
640 		}
641 		return;
642 	}
643 }
644 
645 static void
646 umoscom_intr_callback(struct usb_xfer *xfer)
647 {
648 	struct umoscom_softc *sc = xfer->priv_sc;
649 
650 	switch (USB_GET_STATE(xfer)) {
651 	case USB_ST_TRANSFERRED:
652 		if (xfer->actlen < 2) {
653 			DPRINTF("too short message\n");
654 			goto tr_setup;
655 		}
656 		usb2_com_status_change(&sc->sc_ucom);
657 
658 	case USB_ST_SETUP:
659 tr_setup:
660 		xfer->frlengths[0] = xfer->max_data_length;
661 		usb2_start_hardware(xfer);
662 		return;
663 
664 	default:			/* Error */
665 		if (xfer->error != USB_ERR_CANCELLED) {
666 			DPRINTFN(0, "transfer failed\n");
667 			/* try to clear stall first */
668 			xfer->flags.stall_pipe = 1;
669 			goto tr_setup;
670 		}
671 		return;
672 	}
673 }
674