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