xref: /freebsd/sys/dev/usb/serial/uvscom.c (revision 1f474190)
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 	[UVSCOM_BULK_DT_WR] = {
196 		.type = UE_BULK,
197 		.endpoint = UE_ADDR_ANY,
198 		.direction = UE_DIR_OUT,
199 		.bufsize = UVSCOM_BULK_BUF_SIZE,
200 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
201 		.callback = &uvscom_write_callback,
202 	},
203 
204 	[UVSCOM_BULK_DT_RD] = {
205 		.type = UE_BULK,
206 		.endpoint = UE_ADDR_ANY,
207 		.direction = UE_DIR_IN,
208 		.bufsize = UVSCOM_BULK_BUF_SIZE,
209 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
210 		.callback = &uvscom_read_callback,
211 	},
212 
213 	[UVSCOM_INTR_DT_RD] = {
214 		.type = UE_INTERRUPT,
215 		.endpoint = UE_ADDR_ANY,
216 		.direction = UE_DIR_IN,
217 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
218 		.bufsize = 0,	/* use wMaxPacketSize */
219 		.callback = &uvscom_intr_callback,
220 	},
221 };
222 
223 static const struct ucom_callback uvscom_callback = {
224 	.ucom_cfg_get_status = &uvscom_cfg_get_status,
225 	.ucom_cfg_set_dtr = &uvscom_cfg_set_dtr,
226 	.ucom_cfg_set_rts = &uvscom_cfg_set_rts,
227 	.ucom_cfg_set_break = &uvscom_cfg_set_break,
228 	.ucom_cfg_param = &uvscom_cfg_param,
229 	.ucom_cfg_open = &uvscom_cfg_open,
230 	.ucom_cfg_close = &uvscom_cfg_close,
231 	.ucom_pre_open = &uvscom_pre_open,
232 	.ucom_pre_param = &uvscom_pre_param,
233 	.ucom_start_read = &uvscom_start_read,
234 	.ucom_stop_read = &uvscom_stop_read,
235 	.ucom_start_write = &uvscom_start_write,
236 	.ucom_stop_write = &uvscom_stop_write,
237 	.ucom_poll = &uvscom_poll,
238 	.ucom_free = &uvscom_free,
239 };
240 
241 static const STRUCT_USB_HOST_ID uvscom_devs[] = {
242 	/* SUNTAC U-Cable type A4 */
243 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4, 0)},
244 	/* SUNTAC U-Cable type D2 */
245 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L, 0)},
246 	/* SUNTAC Ir-Trinity */
247 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U, 0)},
248 	/* SUNTAC U-Cable type P1 */
249 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1, 0)},
250 	/* SUNTAC Slipper U */
251 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U, 0)},
252 };
253 
254 static device_method_t uvscom_methods[] = {
255 	DEVMETHOD(device_probe, uvscom_probe),
256 	DEVMETHOD(device_attach, uvscom_attach),
257 	DEVMETHOD(device_detach, uvscom_detach),
258 	DEVMETHOD_END
259 };
260 
261 static devclass_t uvscom_devclass;
262 
263 static driver_t uvscom_driver = {
264 	.name = "uvscom",
265 	.methods = uvscom_methods,
266 	.size = sizeof(struct uvscom_softc),
267 };
268 
269 DRIVER_MODULE(uvscom, uhub, uvscom_driver, uvscom_devclass, NULL, 0);
270 MODULE_DEPEND(uvscom, ucom, 1, 1, 1);
271 MODULE_DEPEND(uvscom, usb, 1, 1, 1);
272 MODULE_VERSION(uvscom, UVSCOM_MODVER);
273 USB_PNP_HOST_INFO(uvscom_devs);
274 
275 static int
276 uvscom_probe(device_t dev)
277 {
278 	struct usb_attach_arg *uaa = device_get_ivars(dev);
279 
280 	if (uaa->usb_mode != USB_MODE_HOST) {
281 		return (ENXIO);
282 	}
283 	if (uaa->info.bConfigIndex != UVSCOM_CONFIG_INDEX) {
284 		return (ENXIO);
285 	}
286 	if (uaa->info.bIfaceIndex != UVSCOM_IFACE_INDEX) {
287 		return (ENXIO);
288 	}
289 	return (usbd_lookup_id_by_uaa(uvscom_devs, sizeof(uvscom_devs), uaa));
290 }
291 
292 static int
293 uvscom_attach(device_t dev)
294 {
295 	struct usb_attach_arg *uaa = device_get_ivars(dev);
296 	struct uvscom_softc *sc = device_get_softc(dev);
297 	int error;
298 
299 	device_set_usb_desc(dev);
300 	mtx_init(&sc->sc_mtx, "uvscom", NULL, MTX_DEF);
301 	ucom_ref(&sc->sc_super_ucom);
302 
303 	sc->sc_udev = uaa->device;
304 
305 	DPRINTF("sc=%p\n", sc);
306 
307 	sc->sc_iface_no = uaa->info.bIfaceNum;
308 	sc->sc_iface_index = UVSCOM_IFACE_INDEX;
309 
310 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
311 	    sc->sc_xfer, uvscom_config, UVSCOM_N_TRANSFER, sc, &sc->sc_mtx);
312 
313 	if (error) {
314 		DPRINTF("could not allocate all USB transfers!\n");
315 		goto detach;
316 	}
317 	sc->sc_line = UVSCOM_LINE_INIT;
318 
319 	/* clear stall at first run */
320 	mtx_lock(&sc->sc_mtx);
321 	usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
322 	usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
323 	mtx_unlock(&sc->sc_mtx);
324 
325 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
326 	    &uvscom_callback, &sc->sc_mtx);
327 	if (error) {
328 		goto detach;
329 	}
330 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
331 
332 	/* start interrupt pipe */
333 	mtx_lock(&sc->sc_mtx);
334 	usbd_transfer_start(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
335 	mtx_unlock(&sc->sc_mtx);
336 
337 	return (0);
338 
339 detach:
340 	uvscom_detach(dev);
341 	return (ENXIO);
342 }
343 
344 static int
345 uvscom_detach(device_t dev)
346 {
347 	struct uvscom_softc *sc = device_get_softc(dev);
348 
349 	DPRINTF("sc=%p\n", sc);
350 
351 	/* stop interrupt pipe */
352 
353 	if (sc->sc_xfer[UVSCOM_INTR_DT_RD])
354 		usbd_transfer_stop(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
355 
356 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
357 	usbd_transfer_unsetup(sc->sc_xfer, UVSCOM_N_TRANSFER);
358 
359 	device_claim_softc(dev);
360 
361 	uvscom_free_softc(sc);
362 
363 	return (0);
364 }
365 
366 UCOM_UNLOAD_DRAIN(uvscom);
367 
368 static void
369 uvscom_free_softc(struct uvscom_softc *sc)
370 {
371 	if (ucom_unref(&sc->sc_super_ucom)) {
372 		mtx_destroy(&sc->sc_mtx);
373 		device_free_softc(sc);
374 	}
375 }
376 
377 static void
378 uvscom_free(struct ucom_softc *ucom)
379 {
380 	uvscom_free_softc(ucom->sc_parent);
381 }
382 
383 static void
384 uvscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
385 {
386 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
387 	struct usb_page_cache *pc;
388 	uint32_t actlen;
389 
390 	switch (USB_GET_STATE(xfer)) {
391 	case USB_ST_SETUP:
392 	case USB_ST_TRANSFERRED:
393 tr_setup:
394 		pc = usbd_xfer_get_frame(xfer, 0);
395 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
396 		    UVSCOM_BULK_BUF_SIZE, &actlen)) {
397 			usbd_xfer_set_frame_len(xfer, 0, actlen);
398 			usbd_transfer_submit(xfer);
399 		}
400 		return;
401 
402 	default:			/* Error */
403 		if (error != USB_ERR_CANCELLED) {
404 			/* try to clear stall first */
405 			usbd_xfer_set_stall(xfer);
406 			goto tr_setup;
407 		}
408 		return;
409 	}
410 }
411 
412 static void
413 uvscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
414 {
415 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
416 	struct usb_page_cache *pc;
417 	int actlen;
418 
419 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
420 
421 	switch (USB_GET_STATE(xfer)) {
422 	case USB_ST_TRANSFERRED:
423 		pc = usbd_xfer_get_frame(xfer, 0);
424 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
425 
426 	case USB_ST_SETUP:
427 tr_setup:
428 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
429 		usbd_transfer_submit(xfer);
430 		return;
431 
432 	default:			/* Error */
433 		if (error != USB_ERR_CANCELLED) {
434 			/* try to clear stall first */
435 			usbd_xfer_set_stall(xfer);
436 			goto tr_setup;
437 		}
438 		return;
439 	}
440 }
441 
442 static void
443 uvscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
444 {
445 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
446 	struct usb_page_cache *pc;
447 	uint8_t buf[2];
448 	int actlen;
449 
450 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
451 
452 	switch (USB_GET_STATE(xfer)) {
453 	case USB_ST_TRANSFERRED:
454 		if (actlen >= 2) {
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