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