xref: /freebsd/sys/dev/usb/serial/uvscom.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
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 USB_PNP_HOST_INFO(uvscom_devs);
272 
273 static int
274 uvscom_probe(device_t dev)
275 {
276 	struct usb_attach_arg *uaa = device_get_ivars(dev);
277 
278 	if (uaa->usb_mode != USB_MODE_HOST) {
279 		return (ENXIO);
280 	}
281 	if (uaa->info.bConfigIndex != UVSCOM_CONFIG_INDEX) {
282 		return (ENXIO);
283 	}
284 	if (uaa->info.bIfaceIndex != UVSCOM_IFACE_INDEX) {
285 		return (ENXIO);
286 	}
287 	return (usbd_lookup_id_by_uaa(uvscom_devs, sizeof(uvscom_devs), uaa));
288 }
289 
290 static int
291 uvscom_attach(device_t dev)
292 {
293 	struct usb_attach_arg *uaa = device_get_ivars(dev);
294 	struct uvscom_softc *sc = device_get_softc(dev);
295 	int error;
296 
297 	device_set_usb_desc(dev);
298 	mtx_init(&sc->sc_mtx, "uvscom", NULL, MTX_DEF);
299 	ucom_ref(&sc->sc_super_ucom);
300 
301 	sc->sc_udev = uaa->device;
302 
303 	DPRINTF("sc=%p\n", sc);
304 
305 	sc->sc_iface_no = uaa->info.bIfaceNum;
306 	sc->sc_iface_index = UVSCOM_IFACE_INDEX;
307 
308 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
309 	    sc->sc_xfer, uvscom_config, UVSCOM_N_TRANSFER, sc, &sc->sc_mtx);
310 
311 	if (error) {
312 		DPRINTF("could not allocate all USB transfers!\n");
313 		goto detach;
314 	}
315 	sc->sc_line = UVSCOM_LINE_INIT;
316 
317 	/* clear stall at first run */
318 	mtx_lock(&sc->sc_mtx);
319 	usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
320 	usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
321 	mtx_unlock(&sc->sc_mtx);
322 
323 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
324 	    &uvscom_callback, &sc->sc_mtx);
325 	if (error) {
326 		goto detach;
327 	}
328 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
329 
330 	/* start interrupt pipe */
331 	mtx_lock(&sc->sc_mtx);
332 	usbd_transfer_start(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
333 	mtx_unlock(&sc->sc_mtx);
334 
335 	return (0);
336 
337 detach:
338 	uvscom_detach(dev);
339 	return (ENXIO);
340 }
341 
342 static int
343 uvscom_detach(device_t dev)
344 {
345 	struct uvscom_softc *sc = device_get_softc(dev);
346 
347 	DPRINTF("sc=%p\n", sc);
348 
349 	/* stop interrupt pipe */
350 
351 	if (sc->sc_xfer[UVSCOM_INTR_DT_RD])
352 		usbd_transfer_stop(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
353 
354 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
355 	usbd_transfer_unsetup(sc->sc_xfer, UVSCOM_N_TRANSFER);
356 
357 	device_claim_softc(dev);
358 
359 	uvscom_free_softc(sc);
360 
361 	return (0);
362 }
363 
364 UCOM_UNLOAD_DRAIN(uvscom);
365 
366 static void
367 uvscom_free_softc(struct uvscom_softc *sc)
368 {
369 	if (ucom_unref(&sc->sc_super_ucom)) {
370 		mtx_destroy(&sc->sc_mtx);
371 		device_free_softc(sc);
372 	}
373 }
374 
375 static void
376 uvscom_free(struct ucom_softc *ucom)
377 {
378 	uvscom_free_softc(ucom->sc_parent);
379 }
380 
381 static void
382 uvscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
383 {
384 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
385 	struct usb_page_cache *pc;
386 	uint32_t actlen;
387 
388 	switch (USB_GET_STATE(xfer)) {
389 	case USB_ST_SETUP:
390 	case USB_ST_TRANSFERRED:
391 tr_setup:
392 		pc = usbd_xfer_get_frame(xfer, 0);
393 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
394 		    UVSCOM_BULK_BUF_SIZE, &actlen)) {
395 
396 			usbd_xfer_set_frame_len(xfer, 0, actlen);
397 			usbd_transfer_submit(xfer);
398 		}
399 		return;
400 
401 	default:			/* Error */
402 		if (error != USB_ERR_CANCELLED) {
403 			/* try to clear stall first */
404 			usbd_xfer_set_stall(xfer);
405 			goto tr_setup;
406 		}
407 		return;
408 	}
409 }
410 
411 static void
412 uvscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
413 {
414 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
415 	struct usb_page_cache *pc;
416 	int actlen;
417 
418 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
419 
420 	switch (USB_GET_STATE(xfer)) {
421 	case USB_ST_TRANSFERRED:
422 		pc = usbd_xfer_get_frame(xfer, 0);
423 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
424 
425 	case USB_ST_SETUP:
426 tr_setup:
427 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
428 		usbd_transfer_submit(xfer);
429 		return;
430 
431 	default:			/* Error */
432 		if (error != USB_ERR_CANCELLED) {
433 			/* try to clear stall first */
434 			usbd_xfer_set_stall(xfer);
435 			goto tr_setup;
436 		}
437 		return;
438 	}
439 }
440 
441 static void
442 uvscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
443 {
444 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
445 	struct usb_page_cache *pc;
446 	uint8_t buf[2];
447 	int actlen;
448 
449 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
450 
451 	switch (USB_GET_STATE(xfer)) {
452 	case USB_ST_TRANSFERRED:
453 		if (actlen >= 2) {
454 
455 			pc = usbd_xfer_get_frame(xfer, 0);
456 			usbd_copy_out(pc, 0, buf, sizeof(buf));
457 
458 			sc->sc_lsr = 0;
459 			sc->sc_msr = 0;
460 			sc->sc_unit_status = buf[1];
461 
462 			if (buf[0] & UVSCOM_TXRDY) {
463 				sc->sc_lsr |= ULSR_TXRDY;
464 			}
465 			if (buf[0] & UVSCOM_RXRDY) {
466 				sc->sc_lsr |= ULSR_RXRDY;
467 			}
468 			if (buf[1] & UVSCOM_CTS) {
469 				sc->sc_msr |= SER_CTS;
470 			}
471 			if (buf[1] & UVSCOM_DSR) {
472 				sc->sc_msr |= SER_DSR;
473 			}
474 			if (buf[1] & UVSCOM_DCD) {
475 				sc->sc_msr |= SER_DCD;
476 			}
477 			/*
478 			 * the UCOM layer will ignore this call if the TTY
479 			 * device is closed!
480 			 */
481 			ucom_status_change(&sc->sc_ucom);
482 		}
483 	case USB_ST_SETUP:
484 tr_setup:
485 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
486 		usbd_transfer_submit(xfer);
487 		return;
488 
489 	default:			/* Error */
490 		if (error != USB_ERR_CANCELLED) {
491 			/* try to clear stall first */
492 			usbd_xfer_set_stall(xfer);
493 			goto tr_setup;
494 		}
495 		return;
496 	}
497 }
498 
499 static void
500 uvscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
501 {
502 	struct uvscom_softc *sc = ucom->sc_parent;
503 
504 	DPRINTF("onoff = %d\n", onoff);
505 
506 	if (onoff)
507 		sc->sc_line |= UVSCOM_DTR;
508 	else
509 		sc->sc_line &= ~UVSCOM_DTR;
510 
511 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
512 }
513 
514 static void
515 uvscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
516 {
517 	struct uvscom_softc *sc = ucom->sc_parent;
518 
519 	DPRINTF("onoff = %d\n", onoff);
520 
521 	if (onoff)
522 		sc->sc_line |= UVSCOM_RTS;
523 	else
524 		sc->sc_line &= ~UVSCOM_RTS;
525 
526 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
527 }
528 
529 static void
530 uvscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
531 {
532 	struct uvscom_softc *sc = ucom->sc_parent;
533 
534 	DPRINTF("onoff = %d\n", onoff);
535 
536 	if (onoff)
537 		sc->sc_line |= UVSCOM_BREAK;
538 	else
539 		sc->sc_line &= ~UVSCOM_BREAK;
540 
541 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
542 }
543 
544 static int
545 uvscom_pre_param(struct ucom_softc *ucom, struct termios *t)
546 {
547 	switch (t->c_ospeed) {
548 		case B150:
549 		case B300:
550 		case B600:
551 		case B1200:
552 		case B2400:
553 		case B4800:
554 		case B9600:
555 		case B19200:
556 		case B38400:
557 		case B57600:
558 		case B115200:
559 		default:
560 		return (EINVAL);
561 	}
562 	return (0);
563 }
564 
565 static void
566 uvscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
567 {
568 	struct uvscom_softc *sc = ucom->sc_parent;
569 	uint16_t value;
570 
571 	DPRINTF("\n");
572 
573 	switch (t->c_ospeed) {
574 	case B150:
575 		value = UVSCOM_SPEED_150BPS;
576 		break;
577 	case B300:
578 		value = UVSCOM_SPEED_300BPS;
579 		break;
580 	case B600:
581 		value = UVSCOM_SPEED_600BPS;
582 		break;
583 	case B1200:
584 		value = UVSCOM_SPEED_1200BPS;
585 		break;
586 	case B2400:
587 		value = UVSCOM_SPEED_2400BPS;
588 		break;
589 	case B4800:
590 		value = UVSCOM_SPEED_4800BPS;
591 		break;
592 	case B9600:
593 		value = UVSCOM_SPEED_9600BPS;
594 		break;
595 	case B19200:
596 		value = UVSCOM_SPEED_19200BPS;
597 		break;
598 	case B38400:
599 		value = UVSCOM_SPEED_38400BPS;
600 		break;
601 	case B57600:
602 		value = UVSCOM_SPEED_57600BPS;
603 		break;
604 	case B115200:
605 		value = UVSCOM_SPEED_115200BPS;
606 		break;
607 	default:
608 		return;
609 	}
610 
611 	uvscom_cfg_write(sc, UVSCOM_SET_SPEED, value);
612 
613 	value = 0;
614 
615 	if (t->c_cflag & CSTOPB) {
616 		value |= UVSCOM_STOP_BIT_2;
617 	}
618 	if (t->c_cflag & PARENB) {
619 		if (t->c_cflag & PARODD) {
620 			value |= UVSCOM_PARITY_ODD;
621 		} else {
622 			value |= UVSCOM_PARITY_EVEN;
623 		}
624 	} else {
625 		value |= UVSCOM_PARITY_NONE;
626 	}
627 
628 	switch (t->c_cflag & CSIZE) {
629 	case CS5:
630 		value |= UVSCOM_DATA_BIT_5;
631 		break;
632 	case CS6:
633 		value |= UVSCOM_DATA_BIT_6;
634 		break;
635 	case CS7:
636 		value |= UVSCOM_DATA_BIT_7;
637 		break;
638 	default:
639 	case CS8:
640 		value |= UVSCOM_DATA_BIT_8;
641 		break;
642 	}
643 
644 	uvscom_cfg_write(sc, UVSCOM_SET_PARAM, value);
645 }
646 
647 static int
648 uvscom_pre_open(struct ucom_softc *ucom)
649 {
650 	struct uvscom_softc *sc = ucom->sc_parent;
651 
652 	DPRINTF("sc = %p\n", sc);
653 
654 	/* check if PC card was inserted */
655 
656 	if (sc->sc_unit_status & UVSCOM_NOCARD) {
657 		DPRINTF("no PC card!\n");
658 		return (ENXIO);
659 	}
660 	return (0);
661 }
662 
663 static void
664 uvscom_cfg_open(struct ucom_softc *ucom)
665 {
666 	struct uvscom_softc *sc = ucom->sc_parent;
667 
668 	DPRINTF("sc = %p\n", sc);
669 
670 	uvscom_cfg_read_status(sc);
671 }
672 
673 static void
674 uvscom_cfg_close(struct ucom_softc *ucom)
675 {
676 	struct uvscom_softc *sc = ucom->sc_parent;
677 
678 	DPRINTF("sc=%p\n", sc);
679 
680 	uvscom_cfg_write(sc, UVSCOM_SHUTDOWN, 0);
681 }
682 
683 static void
684 uvscom_start_read(struct ucom_softc *ucom)
685 {
686 	struct uvscom_softc *sc = ucom->sc_parent;
687 
688 	usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
689 }
690 
691 static void
692 uvscom_stop_read(struct ucom_softc *ucom)
693 {
694 	struct uvscom_softc *sc = ucom->sc_parent;
695 
696 	usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
697 }
698 
699 static void
700 uvscom_start_write(struct ucom_softc *ucom)
701 {
702 	struct uvscom_softc *sc = ucom->sc_parent;
703 
704 	usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
705 }
706 
707 static void
708 uvscom_stop_write(struct ucom_softc *ucom)
709 {
710 	struct uvscom_softc *sc = ucom->sc_parent;
711 
712 	usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
713 }
714 
715 static void
716 uvscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
717 {
718 	struct uvscom_softc *sc = ucom->sc_parent;
719 
720 	*lsr = sc->sc_lsr;
721 	*msr = sc->sc_msr;
722 }
723 
724 static void
725 uvscom_cfg_write(struct uvscom_softc *sc, uint8_t index, uint16_t value)
726 {
727 	struct usb_device_request req;
728 	usb_error_t err;
729 
730 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
731 	req.bRequest = index;
732 	USETW(req.wValue, value);
733 	USETW(req.wIndex, 0);
734 	USETW(req.wLength, 0);
735 
736 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
737 	    &req, NULL, 0, 1000);
738 	if (err) {
739 		DPRINTFN(0, "device request failed, err=%s "
740 		    "(ignored)\n", usbd_errstr(err));
741 	}
742 }
743 
744 static uint16_t
745 uvscom_cfg_read_status(struct uvscom_softc *sc)
746 {
747 	struct usb_device_request req;
748 	usb_error_t err;
749 	uint8_t data[2];
750 
751 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
752 	req.bRequest = UVSCOM_READ_STATUS;
753 	USETW(req.wValue, 0);
754 	USETW(req.wIndex, 0);
755 	USETW(req.wLength, 2);
756 
757 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
758 	    &req, data, 0, 1000);
759 	if (err) {
760 		DPRINTFN(0, "device request failed, err=%s "
761 		    "(ignored)\n", usbd_errstr(err));
762 	}
763 	return (data[0] | (data[1] << 8));
764 }
765 
766 static void
767 uvscom_poll(struct ucom_softc *ucom)
768 {
769 	struct uvscom_softc *sc = ucom->sc_parent;
770 	usbd_transfer_poll(sc->sc_xfer, UVSCOM_N_TRANSFER);
771 }
772