summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/usb/serial/umoscom.c
blob: be45f51041fd3dcfec75a48e78b5be43ab8c2366 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include <machine/rtems-bsd-kernel-space.h>

/* $FreeBSD$ */
/*	$OpenBSD: umoscom.c,v 1.2 2006/10/26 06:02:43 jsg Exp $	*/

/*
 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/stdint.h>
#include <sys/stddef.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <rtems/bsd/local/usbdevs.h>

#define	USB_DEBUG_VAR umoscom_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>

#include <dev/usb/serial/usb_serial.h>

#ifdef USB_DEBUG
static int umoscom_debug = 0;

static SYSCTL_NODE(_hw_usb, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom");
SYSCTL_INT(_hw_usb_umoscom, OID_AUTO, debug, CTLFLAG_RWTUN,
    &umoscom_debug, 0, "Debug level");
#endif

#define	UMOSCOM_BUFSIZE	       1024	/* bytes */

#define	UMOSCOM_CONFIG_INDEX	0
#define	UMOSCOM_IFACE_INDEX	0

/* interrupt packet */
#define	UMOSCOM_IIR_RLS		0x06
#define	UMOSCOM_IIR_RDA		0x04
#define	UMOSCOM_IIR_CTI		0x0c
#define	UMOSCOM_IIR_THR		0x02
#define	UMOSCOM_IIR_MS		0x00

/* registers */
#define	UMOSCOM_READ		0x0d
#define	UMOSCOM_WRITE		0x0e
#define	UMOSCOM_UART_REG	0x0300
#define	UMOSCOM_VEND_REG	0x0000

#define	UMOSCOM_TXBUF		0x00	/* Write */
#define	UMOSCOM_RXBUF		0x00	/* Read */
#define	UMOSCOM_INT		0x01
#define	UMOSCOM_FIFO		0x02	/* Write */
#define	UMOSCOM_ISR		0x02	/* Read */
#define	UMOSCOM_LCR		0x03
#define	UMOSCOM_MCR		0x04
#define	UMOSCOM_LSR		0x05
#define	UMOSCOM_MSR		0x06
#define	UMOSCOM_SCRATCH		0x07
#define	UMOSCOM_DIV_LO		0x08
#define	UMOSCOM_DIV_HI		0x09
#define	UMOSCOM_EFR		0x0a
#define	UMOSCOM_XON1		0x0b
#define	UMOSCOM_XON2		0x0c
#define	UMOSCOM_XOFF1		0x0d
#define	UMOSCOM_XOFF2		0x0e

#define	UMOSCOM_BAUDLO		0x00
#define	UMOSCOM_BAUDHI		0x01

#define	UMOSCOM_INT_RXEN	0x01
#define	UMOSCOM_INT_TXEN	0x02
#define	UMOSCOM_INT_RSEN	0x04
#define	UMOSCOM_INT_MDMEM	0x08
#define	UMOSCOM_INT_SLEEP	0x10
#define	UMOSCOM_INT_XOFF	0x20
#define	UMOSCOM_INT_RTS		0x40

#define	UMOSCOM_FIFO_EN		0x01
#define	UMOSCOM_FIFO_RXCLR	0x02
#define	UMOSCOM_FIFO_TXCLR	0x04
#define	UMOSCOM_FIFO_DMA_BLK	0x08
#define	UMOSCOM_FIFO_TXLVL_MASK	0x30
#define	UMOSCOM_FIFO_TXLVL_8	0x00
#define	UMOSCOM_FIFO_TXLVL_16	0x10
#define	UMOSCOM_FIFO_TXLVL_32	0x20
#define	UMOSCOM_FIFO_TXLVL_56	0x30
#define	UMOSCOM_FIFO_RXLVL_MASK	0xc0
#define	UMOSCOM_FIFO_RXLVL_8	0x00
#define	UMOSCOM_FIFO_RXLVL_16	0x40
#define	UMOSCOM_FIFO_RXLVL_56	0x80
#define	UMOSCOM_FIFO_RXLVL_80	0xc0

#define	UMOSCOM_ISR_MDM		0x00
#define	UMOSCOM_ISR_NONE	0x01
#define	UMOSCOM_ISR_TX		0x02
#define	UMOSCOM_ISR_RX		0x04
#define	UMOSCOM_ISR_LINE	0x06
#define	UMOSCOM_ISR_RXTIMEOUT	0x0c
#define	UMOSCOM_ISR_RX_XOFF	0x10
#define	UMOSCOM_ISR_RTSCTS	0x20
#define	UMOSCOM_ISR_FIFOEN	0xc0

#define	UMOSCOM_LCR_DBITS(x)	((x) - 5)
#define	UMOSCOM_LCR_STOP_BITS_1	0x00
#define	UMOSCOM_LCR_STOP_BITS_2	0x04	/* 2 if 6-8 bits/char or 1.5 if 5 */
#define	UMOSCOM_LCR_PARITY_NONE	0x00
#define	UMOSCOM_LCR_PARITY_ODD	0x08
#define	UMOSCOM_LCR_PARITY_EVEN	0x18
#define	UMOSCOM_LCR_BREAK	0x40
#define	UMOSCOM_LCR_DIVLATCH_EN	0x80

#define	UMOSCOM_MCR_DTR		0x01
#define	UMOSCOM_MCR_RTS		0x02
#define	UMOSCOM_MCR_LOOP	0x04
#define	UMOSCOM_MCR_INTEN	0x08
#define	UMOSCOM_MCR_LOOPBACK	0x10
#define	UMOSCOM_MCR_XONANY	0x20
#define	UMOSCOM_MCR_IRDA_EN	0x40
#define	UMOSCOM_MCR_BAUD_DIV4	0x80

#define	UMOSCOM_LSR_RXDATA	0x01
#define	UMOSCOM_LSR_RXOVER	0x02
#define	UMOSCOM_LSR_RXPAR_ERR	0x04
#define	UMOSCOM_LSR_RXFRM_ERR	0x08
#define	UMOSCOM_LSR_RXBREAK	0x10
#define	UMOSCOM_LSR_TXEMPTY	0x20
#define	UMOSCOM_LSR_TXALLEMPTY	0x40
#define	UMOSCOM_LSR_TXFIFO_ERR	0x80

#define	UMOSCOM_MSR_CTS_CHG	0x01
#define	UMOSCOM_MSR_DSR_CHG	0x02
#define	UMOSCOM_MSR_RI_CHG	0x04
#define	UMOSCOM_MSR_CD_CHG	0x08
#define	UMOSCOM_MSR_CTS		0x10
#define	UMOSCOM_MSR_RTS		0x20
#define	UMOSCOM_MSR_RI		0x40
#define	UMOSCOM_MSR_CD		0x80

#define	UMOSCOM_BAUD_REF	115200

enum {
	UMOSCOM_BULK_DT_WR,
	UMOSCOM_BULK_DT_RD,
	UMOSCOM_INTR_DT_RD,
	UMOSCOM_N_TRANSFER,
};

struct umoscom_softc {
	struct ucom_super_softc sc_super_ucom;
	struct ucom_softc sc_ucom;

	struct usb_xfer *sc_xfer[UMOSCOM_N_TRANSFER];
	struct usb_device *sc_udev;
	struct mtx sc_mtx;

	uint8_t	sc_mcr;
	uint8_t	sc_lcr;
};

/* prototypes */

static device_probe_t umoscom_probe;
static device_attach_t umoscom_attach;
static device_detach_t umoscom_detach;
static void umoscom_free_softc(struct umoscom_softc *);

static usb_callback_t umoscom_write_callback;
static usb_callback_t umoscom_read_callback;
static usb_callback_t umoscom_intr_callback;

static void	umoscom_free(struct ucom_softc *);
static void	umoscom_cfg_open(struct ucom_softc *);
static void	umoscom_cfg_close(struct ucom_softc *);
static void	umoscom_cfg_set_break(struct ucom_softc *, uint8_t);
static void	umoscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
static void	umoscom_cfg_set_rts(struct ucom_softc *, uint8_t);
static int	umoscom_pre_param(struct ucom_softc *, struct termios *);
static void	umoscom_cfg_param(struct ucom_softc *, struct termios *);
static void	umoscom_cfg_get_status(struct ucom_softc *, uint8_t *,
		    uint8_t *);
static void	umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
static uint8_t	umoscom_cfg_read(struct umoscom_softc *, uint16_t);
static void	umoscom_start_read(struct ucom_softc *);
static void	umoscom_stop_read(struct ucom_softc *);
static void	umoscom_start_write(struct ucom_softc *);
static void	umoscom_stop_write(struct ucom_softc *);
static void	umoscom_poll(struct ucom_softc *ucom);

static const struct usb_config umoscom_config_data[UMOSCOM_N_TRANSFER] = {

	[UMOSCOM_BULK_DT_WR] = {
		.type = UE_BULK,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_OUT,
		.bufsize = UMOSCOM_BUFSIZE,
		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
		.callback = &umoscom_write_callback,
	},

	[UMOSCOM_BULK_DT_RD] = {
		.type = UE_BULK,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_IN,
		.bufsize = UMOSCOM_BUFSIZE,
		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
		.callback = &umoscom_read_callback,
	},

	[UMOSCOM_INTR_DT_RD] = {
		.type = UE_INTERRUPT,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_IN,
		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
		.bufsize = 0,	/* use wMaxPacketSize */
		.callback = &umoscom_intr_callback,
	},
};

static const struct ucom_callback umoscom_callback = {
	/* configuration callbacks */
	.ucom_cfg_get_status = &umoscom_cfg_get_status,
	.ucom_cfg_set_dtr = &umoscom_cfg_set_dtr,
	.ucom_cfg_set_rts = &umoscom_cfg_set_rts,
	.ucom_cfg_set_break = &umoscom_cfg_set_break,
	.ucom_cfg_param = &umoscom_cfg_param,
	.ucom_cfg_open = &umoscom_cfg_open,
	.ucom_cfg_close = &umoscom_cfg_close,

	/* other callbacks */
	.ucom_pre_param = &umoscom_pre_param,
	.ucom_start_read = &umoscom_start_read,
	.ucom_stop_read = &umoscom_stop_read,
	.ucom_start_write = &umoscom_start_write,
	.ucom_stop_write = &umoscom_stop_write,
	.ucom_poll = &umoscom_poll,
	.ucom_free = &umoscom_free,
};

static device_method_t umoscom_methods[] = {
	DEVMETHOD(device_probe, umoscom_probe),
	DEVMETHOD(device_attach, umoscom_attach),
	DEVMETHOD(device_detach, umoscom_detach),
	DEVMETHOD_END
};

static devclass_t umoscom_devclass;

static driver_t umoscom_driver = {
	.name = "umoscom",
	.methods = umoscom_methods,
	.size = sizeof(struct umoscom_softc),
};

static const STRUCT_USB_HOST_ID umoscom_devs[] = {
	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
};

DRIVER_MODULE(umoscom, uhub, umoscom_driver, umoscom_devclass, NULL, 0);
MODULE_DEPEND(umoscom, ucom, 1, 1, 1);
MODULE_DEPEND(umoscom, usb, 1, 1, 1);
MODULE_VERSION(umoscom, 1);
USB_PNP_HOST_INFO(umoscom_devs);

static int
umoscom_probe(device_t dev)
{
	struct usb_attach_arg *uaa = device_get_ivars(dev);

	if (uaa->usb_mode != USB_MODE_HOST) {
		return (ENXIO);
	}
	if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) {
		return (ENXIO);
	}
	if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
		return (ENXIO);
	}
	return (usbd_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
}

static int
umoscom_attach(device_t dev)
{
	struct usb_attach_arg *uaa = device_get_ivars(dev);
	struct umoscom_softc *sc = device_get_softc(dev);
	int error;
	uint8_t iface_index;

	sc->sc_udev = uaa->device;
	sc->sc_mcr = 0x08;		/* enable interrupts */

	/* XXX the device doesn't provide any ID string, so set a static one */
	device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
	device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");

	mtx_init(&sc->sc_mtx, "umoscom", NULL, MTX_DEF);
	ucom_ref(&sc->sc_super_ucom);

	iface_index = UMOSCOM_IFACE_INDEX;
	error = usbd_transfer_setup(uaa->device, &iface_index,
	    sc->sc_xfer, umoscom_config_data,
	    UMOSCOM_N_TRANSFER, sc, &sc->sc_mtx);

	if (error) {
		goto detach;
	}
	/* clear stall at first run */
	mtx_lock(&sc->sc_mtx);
	usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
	usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
	mtx_unlock(&sc->sc_mtx);

	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
	    &umoscom_callback, &sc->sc_mtx);
	if (error) {
		goto detach;
	}
	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);

	return (0);

detach:
	device_printf(dev, "attach error: %s\n", usbd_errstr(error));
	umoscom_detach(dev);
	return (ENXIO);
}

static int
umoscom_detach(device_t dev)
{
	struct umoscom_softc *sc = device_get_softc(dev);

	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
	usbd_transfer_unsetup(sc->sc_xfer, UMOSCOM_N_TRANSFER);

	device_claim_softc(dev);

	umoscom_free_softc(sc);

	return (0);
}

UCOM_UNLOAD_DRAIN(umoscom);

static void
umoscom_free_softc(struct umoscom_softc *sc)
{
	if (ucom_unref(&sc->sc_super_ucom)) {
		mtx_destroy(&sc->sc_mtx);
		device_free_softc(sc);
	}
}

static void
umoscom_free(struct ucom_softc *ucom)
{
	umoscom_free_softc(ucom->sc_parent);
}

static void
umoscom_cfg_open(struct ucom_softc *ucom)
{
	struct umoscom_softc *sc = ucom->sc_parent;

	DPRINTF("\n");

	/* Purge FIFOs or odd things happen */
	umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);

	/* Enable FIFO */
	umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
	    UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
	    UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
	    UMOSCOM_UART_REG);

	/* Enable Interrupt Registers */
	umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);

	/* Magic */
	umoscom_cfg_write(sc, 0x01, 0x08);

	/* Magic */
	umoscom_cfg_write(sc, 0x00, 0x02);
}

static void
umoscom_cfg_close(struct ucom_softc *ucom)
{
	return;
}

static void
umoscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
{
	struct umoscom_softc *sc = ucom->sc_parent;
	uint16_t val;

	val = sc->sc_lcr;
	if (onoff)
		val |= UMOSCOM_LCR_BREAK;

	umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
}

static void
umoscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
	struct umoscom_softc *sc = ucom->sc_parent;

	if (onoff)
		sc->sc_mcr |= UMOSCOM_MCR_DTR;
	else
		sc->sc_mcr &= ~UMOSCOM_MCR_DTR;

	umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
}

static void
umoscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
	struct umoscom_softc *sc = ucom->sc_parent;

	if (onoff)
		sc->sc_mcr |= UMOSCOM_MCR_RTS;
	else
		sc->sc_mcr &= ~UMOSCOM_MCR_RTS;

	umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
}

static int
umoscom_pre_param(struct ucom_softc *ucom, struct termios *t)
{
	if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
		return (EINVAL);

	return (0);
}

static void
umoscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
{
	struct umoscom_softc *sc = ucom->sc_parent;
	uint16_t data;

	DPRINTF("speed=%d\n", t->c_ospeed);

	data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);

	if (data == 0) {
		DPRINTF("invalid baud rate!\n");
		return;
	}
	umoscom_cfg_write(sc, UMOSCOM_LCR,
	    UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);

	umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
	    (data & 0xFF) | UMOSCOM_UART_REG);

	umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
	    ((data >> 8) & 0xFF) | UMOSCOM_UART_REG);

	if (t->c_cflag & CSTOPB)
		data = UMOSCOM_LCR_STOP_BITS_2;
	else
		data = UMOSCOM_LCR_STOP_BITS_1;

	if (t->c_cflag & PARENB) {
		if (t->c_cflag & PARODD)
			data |= UMOSCOM_LCR_PARITY_ODD;
		else
			data |= UMOSCOM_LCR_PARITY_EVEN;
	} else
		data |= UMOSCOM_LCR_PARITY_NONE;

	switch (t->c_cflag & CSIZE) {
	case CS5:
		data |= UMOSCOM_LCR_DBITS(5);
		break;
	case CS6:
		data |= UMOSCOM_LCR_DBITS(6);
		break;
	case CS7:
		data |= UMOSCOM_LCR_DBITS(7);
		break;
	case CS8:
		data |= UMOSCOM_LCR_DBITS(8);
		break;
	}

	sc->sc_lcr = data;
	umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
}

static void
umoscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
{
	struct umoscom_softc *sc = ucom->sc_parent;
	uint8_t msr;

	DPRINTFN(5, "\n");

	/*
	 * Read status registers.  MSR bits need translation from ns16550 to
	 * SER_* values.  LSR bits are ns16550 in hardware and ucom.
	 */

	*p_lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
	msr = umoscom_cfg_read(sc, UMOSCOM_MSR);

	/* translate bits */

	if (msr & UMOSCOM_MSR_CTS)
		*p_msr |= SER_CTS;

	if (msr & UMOSCOM_MSR_CD)
		*p_msr |= SER_DCD;

	if (msr & UMOSCOM_MSR_RI)
		*p_msr |= SER_RI;

	if (msr & UMOSCOM_MSR_RTS)
		*p_msr |= SER_DSR;
}

static void
umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
{
	struct usb_device_request req;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = UMOSCOM_WRITE;
	USETW(req.wValue, val);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 0);

	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
	    &req, NULL, 0, 1000);
}

static uint8_t
umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
{
	struct usb_device_request req;
	uint8_t val;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = UMOSCOM_READ;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 1);

	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
	    &req, &val, 0, 1000);

	DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);

	return (val);
}

static void
umoscom_start_read(struct ucom_softc *ucom)
{
	struct umoscom_softc *sc = ucom->sc_parent;

#if 0
	/* start interrupt endpoint */
	usbd_transfer_start(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
#endif
	/* start read endpoint */
	usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
}

static void
umoscom_stop_read(struct ucom_softc *ucom)
{
	struct umoscom_softc *sc = ucom->sc_parent;

	/* stop interrupt transfer */
	usbd_transfer_stop(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);

	/* stop read endpoint */
	usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
}

static void
umoscom_start_write(struct ucom_softc *ucom)
{
	struct umoscom_softc *sc = ucom->sc_parent;

	usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
}

static void
umoscom_stop_write(struct ucom_softc *ucom)
{
	struct umoscom_softc *sc = ucom->sc_parent;

	usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
}

static void
umoscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
	struct umoscom_softc *sc = usbd_xfer_softc(xfer);
	struct usb_page_cache *pc;
	uint32_t actlen;

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_SETUP:
	case USB_ST_TRANSFERRED:
tr_setup:
		DPRINTF("\n");

		pc = usbd_xfer_get_frame(xfer, 0);
		if (ucom_get_data(&sc->sc_ucom, pc, 0,
		    UMOSCOM_BUFSIZE, &actlen)) {

			usbd_xfer_set_frame_len(xfer, 0, actlen);
			usbd_transfer_submit(xfer);
		}
		return;

	default:			/* Error */
		if (error != USB_ERR_CANCELLED) {
			DPRINTFN(0, "transfer failed\n");
			/* try to clear stall first */
			usbd_xfer_set_stall(xfer);
			goto tr_setup;
		}
		return;
	}
}

static void
umoscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
	struct umoscom_softc *sc = usbd_xfer_softc(xfer);
	struct usb_page_cache *pc;
	int actlen;

	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:
		DPRINTF("got %d bytes\n", actlen);
		pc = usbd_xfer_get_frame(xfer, 0);
		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);

	case USB_ST_SETUP:
tr_setup:
		DPRINTF("\n");

		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
		usbd_transfer_submit(xfer);
		return;

	default:			/* Error */
		if (error != USB_ERR_CANCELLED) {
			DPRINTFN(0, "transfer failed\n");
			/* try to clear stall first */
			usbd_xfer_set_stall(xfer);
			goto tr_setup;
		}
		return;
	}
}

static void
umoscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
{
	struct umoscom_softc *sc = usbd_xfer_softc(xfer);
	int actlen;

	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:
		if (actlen < 2) {
			DPRINTF("too short message\n");
			goto tr_setup;
		}
		ucom_status_change(&sc->sc_ucom);

	case USB_ST_SETUP:
tr_setup:
		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
		usbd_transfer_submit(xfer);
		return;

	default:			/* Error */
		if (error != USB_ERR_CANCELLED) {
			DPRINTFN(0, "transfer failed\n");
			/* try to clear stall first */
			usbd_xfer_set_stall(xfer);
			goto tr_setup;
		}
		return;
	}
}

static void
umoscom_poll(struct ucom_softc *ucom)
{
	struct umoscom_softc *sc = ucom->sc_parent;
	usbd_transfer_poll(sc->sc_xfer, UMOSCOM_N_TRANSFER);
}