xref: /freebsd/sys/dev/usb/serial/uvscom.c (revision 93e779a26c651610ac6e7986d67ecc9ed2cadcbf)
1 /*	$NetBSD: usb/uvscom.c,v 1.1 2002/03/19 15:08:42 augustss Exp $	*/
2 
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5 
6 /*-
7  * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  */
32 
33 /*
34  * uvscom: SUNTAC Slipper U VS-10U driver.
35  * Slipper U is a PC Card to USB converter for data communication card
36  * adapter.  It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in,
37  * P-in m@ater and various data communication card adapters.
38  */
39 
40 #include <sys/stdint.h>
41 #include <sys/stddef.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bus.h>
48 #include <sys/module.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/sx.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
57 #include <sys/priv.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62 #include "usbdevs.h"
63 
64 #define	USB_DEBUG_VAR uvscom_debug
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_process.h>
67 
68 #include <dev/usb/serial/usb_serial.h>
69 
70 #ifdef USB_DEBUG
71 static int uvscom_debug = 0;
72 
73 static SYSCTL_NODE(_hw_usb, OID_AUTO, uvscom, CTLFLAG_RW, 0, "USB uvscom");
74 SYSCTL_INT(_hw_usb_uvscom, OID_AUTO, debug, CTLFLAG_RWTUN,
75     &uvscom_debug, 0, "Debug level");
76 #endif
77 
78 #define	UVSCOM_MODVER		1	/* module version */
79 
80 #define	UVSCOM_CONFIG_INDEX	0
81 #define	UVSCOM_IFACE_INDEX	0
82 
83 /* Request */
84 #define	UVSCOM_SET_SPEED	0x10
85 #define	UVSCOM_LINE_CTL		0x11
86 #define	UVSCOM_SET_PARAM	0x12
87 #define	UVSCOM_READ_STATUS	0xd0
88 #define	UVSCOM_SHUTDOWN		0xe0
89 
90 /* UVSCOM_SET_SPEED parameters */
91 #define	UVSCOM_SPEED_150BPS	0x00
92 #define	UVSCOM_SPEED_300BPS	0x01
93 #define	UVSCOM_SPEED_600BPS	0x02
94 #define	UVSCOM_SPEED_1200BPS	0x03
95 #define	UVSCOM_SPEED_2400BPS	0x04
96 #define	UVSCOM_SPEED_4800BPS	0x05
97 #define	UVSCOM_SPEED_9600BPS	0x06
98 #define	UVSCOM_SPEED_19200BPS	0x07
99 #define	UVSCOM_SPEED_38400BPS	0x08
100 #define	UVSCOM_SPEED_57600BPS	0x09
101 #define	UVSCOM_SPEED_115200BPS	0x0a
102 
103 /* UVSCOM_LINE_CTL parameters */
104 #define	UVSCOM_BREAK		0x40
105 #define	UVSCOM_RTS		0x02
106 #define	UVSCOM_DTR		0x01
107 #define	UVSCOM_LINE_INIT	0x08
108 
109 /* UVSCOM_SET_PARAM parameters */
110 #define	UVSCOM_DATA_MASK	0x03
111 #define	UVSCOM_DATA_BIT_8	0x03
112 #define	UVSCOM_DATA_BIT_7	0x02
113 #define	UVSCOM_DATA_BIT_6	0x01
114 #define	UVSCOM_DATA_BIT_5	0x00
115 
116 #define	UVSCOM_STOP_MASK	0x04
117 #define	UVSCOM_STOP_BIT_2	0x04
118 #define	UVSCOM_STOP_BIT_1	0x00
119 
120 #define	UVSCOM_PARITY_MASK	0x18
121 #define	UVSCOM_PARITY_EVEN	0x18
122 #define	UVSCOM_PARITY_ODD	0x08
123 #define	UVSCOM_PARITY_NONE	0x00
124 
125 /* Status bits */
126 #define	UVSCOM_TXRDY		0x04
127 #define	UVSCOM_RXRDY		0x01
128 
129 #define	UVSCOM_DCD		0x08
130 #define	UVSCOM_NOCARD		0x04
131 #define	UVSCOM_DSR		0x02
132 #define	UVSCOM_CTS		0x01
133 #define	UVSCOM_USTAT_MASK	(UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
134 
135 #define	UVSCOM_BULK_BUF_SIZE	1024	/* bytes */
136 
137 enum {
138 	UVSCOM_BULK_DT_WR,
139 	UVSCOM_BULK_DT_RD,
140 	UVSCOM_INTR_DT_RD,
141 	UVSCOM_N_TRANSFER,
142 };
143 
144 struct uvscom_softc {
145 	struct ucom_super_softc sc_super_ucom;
146 	struct ucom_softc sc_ucom;
147 
148 	struct usb_xfer *sc_xfer[UVSCOM_N_TRANSFER];
149 	struct usb_device *sc_udev;
150 	struct mtx sc_mtx;
151 
152 	uint16_t sc_line;		/* line control register */
153 
154 	uint8_t	sc_iface_no;		/* interface number */
155 	uint8_t	sc_iface_index;		/* interface index */
156 	uint8_t	sc_lsr;			/* local status register */
157 	uint8_t	sc_msr;			/* uvscom status register */
158 	uint8_t	sc_unit_status;		/* unit status */
159 };
160 
161 /* prototypes */
162 
163 static device_probe_t uvscom_probe;
164 static device_attach_t uvscom_attach;
165 static device_detach_t uvscom_detach;
166 static void uvscom_free_softc(struct uvscom_softc *);
167 
168 static usb_callback_t uvscom_write_callback;
169 static usb_callback_t uvscom_read_callback;
170 static usb_callback_t uvscom_intr_callback;
171 
172 static void	uvscom_free(struct ucom_softc *);
173 static void	uvscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
174 static void	uvscom_cfg_set_rts(struct ucom_softc *, uint8_t);
175 static void	uvscom_cfg_set_break(struct ucom_softc *, uint8_t);
176 static int	uvscom_pre_param(struct ucom_softc *, struct termios *);
177 static void	uvscom_cfg_param(struct ucom_softc *, struct termios *);
178 static int	uvscom_pre_open(struct ucom_softc *);
179 static void	uvscom_cfg_open(struct ucom_softc *);
180 static void	uvscom_cfg_close(struct ucom_softc *);
181 static void	uvscom_start_read(struct ucom_softc *);
182 static void	uvscom_stop_read(struct ucom_softc *);
183 static void	uvscom_start_write(struct ucom_softc *);
184 static void	uvscom_stop_write(struct ucom_softc *);
185 static void	uvscom_cfg_get_status(struct ucom_softc *, uint8_t *,
186 		    uint8_t *);
187 static void	uvscom_cfg_write(struct uvscom_softc *, uint8_t, uint16_t);
188 static uint16_t	uvscom_cfg_read_status(struct uvscom_softc *);
189 static void	uvscom_poll(struct ucom_softc *ucom);
190 
191 static const struct usb_config uvscom_config[UVSCOM_N_TRANSFER] = {
192 
193 	[UVSCOM_BULK_DT_WR] = {
194 		.type = UE_BULK,
195 		.endpoint = UE_ADDR_ANY,
196 		.direction = UE_DIR_OUT,
197 		.bufsize = UVSCOM_BULK_BUF_SIZE,
198 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
199 		.callback = &uvscom_write_callback,
200 	},
201 
202 	[UVSCOM_BULK_DT_RD] = {
203 		.type = UE_BULK,
204 		.endpoint = UE_ADDR_ANY,
205 		.direction = UE_DIR_IN,
206 		.bufsize = UVSCOM_BULK_BUF_SIZE,
207 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
208 		.callback = &uvscom_read_callback,
209 	},
210 
211 	[UVSCOM_INTR_DT_RD] = {
212 		.type = UE_INTERRUPT,
213 		.endpoint = UE_ADDR_ANY,
214 		.direction = UE_DIR_IN,
215 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
216 		.bufsize = 0,	/* use wMaxPacketSize */
217 		.callback = &uvscom_intr_callback,
218 	},
219 };
220 
221 static const struct ucom_callback uvscom_callback = {
222 	.ucom_cfg_get_status = &uvscom_cfg_get_status,
223 	.ucom_cfg_set_dtr = &uvscom_cfg_set_dtr,
224 	.ucom_cfg_set_rts = &uvscom_cfg_set_rts,
225 	.ucom_cfg_set_break = &uvscom_cfg_set_break,
226 	.ucom_cfg_param = &uvscom_cfg_param,
227 	.ucom_cfg_open = &uvscom_cfg_open,
228 	.ucom_cfg_close = &uvscom_cfg_close,
229 	.ucom_pre_open = &uvscom_pre_open,
230 	.ucom_pre_param = &uvscom_pre_param,
231 	.ucom_start_read = &uvscom_start_read,
232 	.ucom_stop_read = &uvscom_stop_read,
233 	.ucom_start_write = &uvscom_start_write,
234 	.ucom_stop_write = &uvscom_stop_write,
235 	.ucom_poll = &uvscom_poll,
236 	.ucom_free = &uvscom_free,
237 };
238 
239 static const STRUCT_USB_HOST_ID uvscom_devs[] = {
240 	/* SUNTAC U-Cable type A4 */
241 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4, 0)},
242 	/* SUNTAC U-Cable type D2 */
243 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L, 0)},
244 	/* SUNTAC Ir-Trinity */
245 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U, 0)},
246 	/* SUNTAC U-Cable type P1 */
247 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1, 0)},
248 	/* SUNTAC Slipper U */
249 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U, 0)},
250 };
251 
252 static device_method_t uvscom_methods[] = {
253 	DEVMETHOD(device_probe, uvscom_probe),
254 	DEVMETHOD(device_attach, uvscom_attach),
255 	DEVMETHOD(device_detach, uvscom_detach),
256 	DEVMETHOD_END
257 };
258 
259 static devclass_t uvscom_devclass;
260 
261 static driver_t uvscom_driver = {
262 	.name = "uvscom",
263 	.methods = uvscom_methods,
264 	.size = sizeof(struct uvscom_softc),
265 };
266 
267 DRIVER_MODULE(uvscom, uhub, uvscom_driver, uvscom_devclass, NULL, 0);
268 MODULE_DEPEND(uvscom, ucom, 1, 1, 1);
269 MODULE_DEPEND(uvscom, usb, 1, 1, 1);
270 MODULE_VERSION(uvscom, UVSCOM_MODVER);
271 
272 static int
273 uvscom_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 != UVSCOM_CONFIG_INDEX) {
281 		return (ENXIO);
282 	}
283 	if (uaa->info.bIfaceIndex != UVSCOM_IFACE_INDEX) {
284 		return (ENXIO);
285 	}
286 	return (usbd_lookup_id_by_uaa(uvscom_devs, sizeof(uvscom_devs), uaa));
287 }
288 
289 static int
290 uvscom_attach(device_t dev)
291 {
292 	struct usb_attach_arg *uaa = device_get_ivars(dev);
293 	struct uvscom_softc *sc = device_get_softc(dev);
294 	int error;
295 
296 	device_set_usb_desc(dev);
297 	mtx_init(&sc->sc_mtx, "uvscom", NULL, MTX_DEF);
298 	ucom_ref(&sc->sc_super_ucom);
299 
300 	sc->sc_udev = uaa->device;
301 
302 	DPRINTF("sc=%p\n", sc);
303 
304 	sc->sc_iface_no = uaa->info.bIfaceNum;
305 	sc->sc_iface_index = UVSCOM_IFACE_INDEX;
306 
307 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
308 	    sc->sc_xfer, uvscom_config, UVSCOM_N_TRANSFER, sc, &sc->sc_mtx);
309 
310 	if (error) {
311 		DPRINTF("could not allocate all USB transfers!\n");
312 		goto detach;
313 	}
314 	sc->sc_line = UVSCOM_LINE_INIT;
315 
316 	/* clear stall at first run */
317 	mtx_lock(&sc->sc_mtx);
318 	usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
319 	usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
320 	mtx_unlock(&sc->sc_mtx);
321 
322 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
323 	    &uvscom_callback, &sc->sc_mtx);
324 	if (error) {
325 		goto detach;
326 	}
327 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
328 
329 	/* start interrupt pipe */
330 	mtx_lock(&sc->sc_mtx);
331 	usbd_transfer_start(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
332 	mtx_unlock(&sc->sc_mtx);
333 
334 	return (0);
335 
336 detach:
337 	uvscom_detach(dev);
338 	return (ENXIO);
339 }
340 
341 static int
342 uvscom_detach(device_t dev)
343 {
344 	struct uvscom_softc *sc = device_get_softc(dev);
345 
346 	DPRINTF("sc=%p\n", sc);
347 
348 	/* stop interrupt pipe */
349 
350 	if (sc->sc_xfer[UVSCOM_INTR_DT_RD])
351 		usbd_transfer_stop(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
352 
353 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
354 	usbd_transfer_unsetup(sc->sc_xfer, UVSCOM_N_TRANSFER);
355 
356 	device_claim_softc(dev);
357 
358 	uvscom_free_softc(sc);
359 
360 	return (0);
361 }
362 
363 UCOM_UNLOAD_DRAIN(uvscom);
364 
365 static void
366 uvscom_free_softc(struct uvscom_softc *sc)
367 {
368 	if (ucom_unref(&sc->sc_super_ucom)) {
369 		mtx_destroy(&sc->sc_mtx);
370 		device_free_softc(sc);
371 	}
372 }
373 
374 static void
375 uvscom_free(struct ucom_softc *ucom)
376 {
377 	uvscom_free_softc(ucom->sc_parent);
378 }
379 
380 static void
381 uvscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
382 {
383 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
384 	struct usb_page_cache *pc;
385 	uint32_t actlen;
386 
387 	switch (USB_GET_STATE(xfer)) {
388 	case USB_ST_SETUP:
389 	case USB_ST_TRANSFERRED:
390 tr_setup:
391 		pc = usbd_xfer_get_frame(xfer, 0);
392 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
393 		    UVSCOM_BULK_BUF_SIZE, &actlen)) {
394 
395 			usbd_xfer_set_frame_len(xfer, 0, actlen);
396 			usbd_transfer_submit(xfer);
397 		}
398 		return;
399 
400 	default:			/* Error */
401 		if (error != USB_ERR_CANCELLED) {
402 			/* try to clear stall first */
403 			usbd_xfer_set_stall(xfer);
404 			goto tr_setup;
405 		}
406 		return;
407 	}
408 }
409 
410 static void
411 uvscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
412 {
413 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
414 	struct usb_page_cache *pc;
415 	int actlen;
416 
417 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
418 
419 	switch (USB_GET_STATE(xfer)) {
420 	case USB_ST_TRANSFERRED:
421 		pc = usbd_xfer_get_frame(xfer, 0);
422 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
423 
424 	case USB_ST_SETUP:
425 tr_setup:
426 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
427 		usbd_transfer_submit(xfer);
428 		return;
429 
430 	default:			/* Error */
431 		if (error != USB_ERR_CANCELLED) {
432 			/* try to clear stall first */
433 			usbd_xfer_set_stall(xfer);
434 			goto tr_setup;
435 		}
436 		return;
437 	}
438 }
439 
440 static void
441 uvscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
442 {
443 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
444 	struct usb_page_cache *pc;
445 	uint8_t buf[2];
446 	int actlen;
447 
448 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
449 
450 	switch (USB_GET_STATE(xfer)) {
451 	case USB_ST_TRANSFERRED:
452 		if (actlen >= 2) {
453 
454 			pc = usbd_xfer_get_frame(xfer, 0);
455 			usbd_copy_out(pc, 0, buf, sizeof(buf));
456 
457 			sc->sc_lsr = 0;
458 			sc->sc_msr = 0;
459 			sc->sc_unit_status = buf[1];
460 
461 			if (buf[0] & UVSCOM_TXRDY) {
462 				sc->sc_lsr |= ULSR_TXRDY;
463 			}
464 			if (buf[0] & UVSCOM_RXRDY) {
465 				sc->sc_lsr |= ULSR_RXRDY;
466 			}
467 			if (buf[1] & UVSCOM_CTS) {
468 				sc->sc_msr |= SER_CTS;
469 			}
470 			if (buf[1] & UVSCOM_DSR) {
471 				sc->sc_msr |= SER_DSR;
472 			}
473 			if (buf[1] & UVSCOM_DCD) {
474 				sc->sc_msr |= SER_DCD;
475 			}
476 			/*
477 			 * the UCOM layer will ignore this call if the TTY
478 			 * device is closed!
479 			 */
480 			ucom_status_change(&sc->sc_ucom);
481 		}
482 	case USB_ST_SETUP:
483 tr_setup:
484 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
485 		usbd_transfer_submit(xfer);
486 		return;
487 
488 	default:			/* Error */
489 		if (error != USB_ERR_CANCELLED) {
490 			/* try to clear stall first */
491 			usbd_xfer_set_stall(xfer);
492 			goto tr_setup;
493 		}
494 		return;
495 	}
496 }
497 
498 static void
499 uvscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
500 {
501 	struct uvscom_softc *sc = ucom->sc_parent;
502 
503 	DPRINTF("onoff = %d\n", onoff);
504 
505 	if (onoff)
506 		sc->sc_line |= UVSCOM_DTR;
507 	else
508 		sc->sc_line &= ~UVSCOM_DTR;
509 
510 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
511 }
512 
513 static void
514 uvscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
515 {
516 	struct uvscom_softc *sc = ucom->sc_parent;
517 
518 	DPRINTF("onoff = %d\n", onoff);
519 
520 	if (onoff)
521 		sc->sc_line |= UVSCOM_RTS;
522 	else
523 		sc->sc_line &= ~UVSCOM_RTS;
524 
525 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
526 }
527 
528 static void
529 uvscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
530 {
531 	struct uvscom_softc *sc = ucom->sc_parent;
532 
533 	DPRINTF("onoff = %d\n", onoff);
534 
535 	if (onoff)
536 		sc->sc_line |= UVSCOM_BREAK;
537 	else
538 		sc->sc_line &= ~UVSCOM_BREAK;
539 
540 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
541 }
542 
543 static int
544 uvscom_pre_param(struct ucom_softc *ucom, struct termios *t)
545 {
546 	switch (t->c_ospeed) {
547 		case B150:
548 		case B300:
549 		case B600:
550 		case B1200:
551 		case B2400:
552 		case B4800:
553 		case B9600:
554 		case B19200:
555 		case B38400:
556 		case B57600:
557 		case B115200:
558 		default:
559 		return (EINVAL);
560 	}
561 	return (0);
562 }
563 
564 static void
565 uvscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
566 {
567 	struct uvscom_softc *sc = ucom->sc_parent;
568 	uint16_t value;
569 
570 	DPRINTF("\n");
571 
572 	switch (t->c_ospeed) {
573 	case B150:
574 		value = UVSCOM_SPEED_150BPS;
575 		break;
576 	case B300:
577 		value = UVSCOM_SPEED_300BPS;
578 		break;
579 	case B600:
580 		value = UVSCOM_SPEED_600BPS;
581 		break;
582 	case B1200:
583 		value = UVSCOM_SPEED_1200BPS;
584 		break;
585 	case B2400:
586 		value = UVSCOM_SPEED_2400BPS;
587 		break;
588 	case B4800:
589 		value = UVSCOM_SPEED_4800BPS;
590 		break;
591 	case B9600:
592 		value = UVSCOM_SPEED_9600BPS;
593 		break;
594 	case B19200:
595 		value = UVSCOM_SPEED_19200BPS;
596 		break;
597 	case B38400:
598 		value = UVSCOM_SPEED_38400BPS;
599 		break;
600 	case B57600:
601 		value = UVSCOM_SPEED_57600BPS;
602 		break;
603 	case B115200:
604 		value = UVSCOM_SPEED_115200BPS;
605 		break;
606 	default:
607 		return;
608 	}
609 
610 	uvscom_cfg_write(sc, UVSCOM_SET_SPEED, value);
611 
612 	value = 0;
613 
614 	if (t->c_cflag & CSTOPB) {
615 		value |= UVSCOM_STOP_BIT_2;
616 	}
617 	if (t->c_cflag & PARENB) {
618 		if (t->c_cflag & PARODD) {
619 			value |= UVSCOM_PARITY_ODD;
620 		} else {
621 			value |= UVSCOM_PARITY_EVEN;
622 		}
623 	} else {
624 		value |= UVSCOM_PARITY_NONE;
625 	}
626 
627 	switch (t->c_cflag & CSIZE) {
628 	case CS5:
629 		value |= UVSCOM_DATA_BIT_5;
630 		break;
631 	case CS6:
632 		value |= UVSCOM_DATA_BIT_6;
633 		break;
634 	case CS7:
635 		value |= UVSCOM_DATA_BIT_7;
636 		break;
637 	default:
638 	case CS8:
639 		value |= UVSCOM_DATA_BIT_8;
640 		break;
641 	}
642 
643 	uvscom_cfg_write(sc, UVSCOM_SET_PARAM, value);
644 }
645 
646 static int
647 uvscom_pre_open(struct ucom_softc *ucom)
648 {
649 	struct uvscom_softc *sc = ucom->sc_parent;
650 
651 	DPRINTF("sc = %p\n", sc);
652 
653 	/* check if PC card was inserted */
654 
655 	if (sc->sc_unit_status & UVSCOM_NOCARD) {
656 		DPRINTF("no PC card!\n");
657 		return (ENXIO);
658 	}
659 	return (0);
660 }
661 
662 static void
663 uvscom_cfg_open(struct ucom_softc *ucom)
664 {
665 	struct uvscom_softc *sc = ucom->sc_parent;
666 
667 	DPRINTF("sc = %p\n", sc);
668 
669 	uvscom_cfg_read_status(sc);
670 }
671 
672 static void
673 uvscom_cfg_close(struct ucom_softc *ucom)
674 {
675 	struct uvscom_softc *sc = ucom->sc_parent;
676 
677 	DPRINTF("sc=%p\n", sc);
678 
679 	uvscom_cfg_write(sc, UVSCOM_SHUTDOWN, 0);
680 }
681 
682 static void
683 uvscom_start_read(struct ucom_softc *ucom)
684 {
685 	struct uvscom_softc *sc = ucom->sc_parent;
686 
687 	usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
688 }
689 
690 static void
691 uvscom_stop_read(struct ucom_softc *ucom)
692 {
693 	struct uvscom_softc *sc = ucom->sc_parent;
694 
695 	usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
696 }
697 
698 static void
699 uvscom_start_write(struct ucom_softc *ucom)
700 {
701 	struct uvscom_softc *sc = ucom->sc_parent;
702 
703 	usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
704 }
705 
706 static void
707 uvscom_stop_write(struct ucom_softc *ucom)
708 {
709 	struct uvscom_softc *sc = ucom->sc_parent;
710 
711 	usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
712 }
713 
714 static void
715 uvscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
716 {
717 	struct uvscom_softc *sc = ucom->sc_parent;
718 
719 	*lsr = sc->sc_lsr;
720 	*msr = sc->sc_msr;
721 }
722 
723 static void
724 uvscom_cfg_write(struct uvscom_softc *sc, uint8_t index, uint16_t value)
725 {
726 	struct usb_device_request req;
727 	usb_error_t err;
728 
729 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
730 	req.bRequest = index;
731 	USETW(req.wValue, value);
732 	USETW(req.wIndex, 0);
733 	USETW(req.wLength, 0);
734 
735 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
736 	    &req, NULL, 0, 1000);
737 	if (err) {
738 		DPRINTFN(0, "device request failed, err=%s "
739 		    "(ignored)\n", usbd_errstr(err));
740 	}
741 }
742 
743 static uint16_t
744 uvscom_cfg_read_status(struct uvscom_softc *sc)
745 {
746 	struct usb_device_request req;
747 	usb_error_t err;
748 	uint8_t data[2];
749 
750 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
751 	req.bRequest = UVSCOM_READ_STATUS;
752 	USETW(req.wValue, 0);
753 	USETW(req.wIndex, 0);
754 	USETW(req.wLength, 2);
755 
756 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
757 	    &req, data, 0, 1000);
758 	if (err) {
759 		DPRINTFN(0, "device request failed, err=%s "
760 		    "(ignored)\n", usbd_errstr(err));
761 	}
762 	return (data[0] | (data[1] << 8));
763 }
764 
765 static void
766 uvscom_poll(struct ucom_softc *ucom)
767 {
768 	struct uvscom_softc *sc = ucom->sc_parent;
769 	usbd_transfer_poll(sc->sc_xfer, UVSCOM_N_TRANSFER);
770 }
771