xref: /freebsd/sys/dev/usb/serial/uvscom.c (revision 4c8945a06b01a5c8122cdeb402af36bb46a06acc)
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 SYSCTL_NODE(_hw_usb, OID_AUTO, uvscom, CTLFLAG_RW, 0, "USB uvscom");
74 SYSCTL_INT(_hw_usb_uvscom, OID_AUTO, debug, CTLFLAG_RW,
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 
167 static usb_callback_t uvscom_write_callback;
168 static usb_callback_t uvscom_read_callback;
169 static usb_callback_t uvscom_intr_callback;
170 
171 static void	uvscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
172 static void	uvscom_cfg_set_rts(struct ucom_softc *, uint8_t);
173 static void	uvscom_cfg_set_break(struct ucom_softc *, uint8_t);
174 static int	uvscom_pre_param(struct ucom_softc *, struct termios *);
175 static void	uvscom_cfg_param(struct ucom_softc *, struct termios *);
176 static int	uvscom_pre_open(struct ucom_softc *);
177 static void	uvscom_cfg_open(struct ucom_softc *);
178 static void	uvscom_cfg_close(struct ucom_softc *);
179 static void	uvscom_start_read(struct ucom_softc *);
180 static void	uvscom_stop_read(struct ucom_softc *);
181 static void	uvscom_start_write(struct ucom_softc *);
182 static void	uvscom_stop_write(struct ucom_softc *);
183 static void	uvscom_cfg_get_status(struct ucom_softc *, uint8_t *,
184 		    uint8_t *);
185 static void	uvscom_cfg_write(struct uvscom_softc *, uint8_t, uint16_t);
186 static uint16_t	uvscom_cfg_read_status(struct uvscom_softc *);
187 static void	uvscom_poll(struct ucom_softc *ucom);
188 
189 static const struct usb_config uvscom_config[UVSCOM_N_TRANSFER] = {
190 
191 	[UVSCOM_BULK_DT_WR] = {
192 		.type = UE_BULK,
193 		.endpoint = UE_ADDR_ANY,
194 		.direction = UE_DIR_OUT,
195 		.bufsize = UVSCOM_BULK_BUF_SIZE,
196 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
197 		.callback = &uvscom_write_callback,
198 	},
199 
200 	[UVSCOM_BULK_DT_RD] = {
201 		.type = UE_BULK,
202 		.endpoint = UE_ADDR_ANY,
203 		.direction = UE_DIR_IN,
204 		.bufsize = UVSCOM_BULK_BUF_SIZE,
205 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
206 		.callback = &uvscom_read_callback,
207 	},
208 
209 	[UVSCOM_INTR_DT_RD] = {
210 		.type = UE_INTERRUPT,
211 		.endpoint = UE_ADDR_ANY,
212 		.direction = UE_DIR_IN,
213 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
214 		.bufsize = 0,	/* use wMaxPacketSize */
215 		.callback = &uvscom_intr_callback,
216 	},
217 };
218 
219 static const struct ucom_callback uvscom_callback = {
220 	.ucom_cfg_get_status = &uvscom_cfg_get_status,
221 	.ucom_cfg_set_dtr = &uvscom_cfg_set_dtr,
222 	.ucom_cfg_set_rts = &uvscom_cfg_set_rts,
223 	.ucom_cfg_set_break = &uvscom_cfg_set_break,
224 	.ucom_cfg_param = &uvscom_cfg_param,
225 	.ucom_cfg_open = &uvscom_cfg_open,
226 	.ucom_cfg_close = &uvscom_cfg_close,
227 	.ucom_pre_open = &uvscom_pre_open,
228 	.ucom_pre_param = &uvscom_pre_param,
229 	.ucom_start_read = &uvscom_start_read,
230 	.ucom_stop_read = &uvscom_stop_read,
231 	.ucom_start_write = &uvscom_start_write,
232 	.ucom_stop_write = &uvscom_stop_write,
233 	.ucom_poll = &uvscom_poll,
234 };
235 
236 static const struct usb_device_id uvscom_devs[] = {
237 	/* SUNTAC U-Cable type A4 */
238 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4, 0)},
239 	/* SUNTAC U-Cable type D2 */
240 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L, 0)},
241 	/* SUNTAC Ir-Trinity */
242 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U, 0)},
243 	/* SUNTAC U-Cable type P1 */
244 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1, 0)},
245 	/* SUNTAC Slipper U */
246 	{USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U, 0)},
247 };
248 
249 static device_method_t uvscom_methods[] = {
250 	DEVMETHOD(device_probe, uvscom_probe),
251 	DEVMETHOD(device_attach, uvscom_attach),
252 	DEVMETHOD(device_detach, uvscom_detach),
253 	{0, 0}
254 };
255 
256 static devclass_t uvscom_devclass;
257 
258 static driver_t uvscom_driver = {
259 	.name = "uvscom",
260 	.methods = uvscom_methods,
261 	.size = sizeof(struct uvscom_softc),
262 };
263 
264 DRIVER_MODULE(uvscom, uhub, uvscom_driver, uvscom_devclass, NULL, 0);
265 MODULE_DEPEND(uvscom, ucom, 1, 1, 1);
266 MODULE_DEPEND(uvscom, usb, 1, 1, 1);
267 MODULE_VERSION(uvscom, UVSCOM_MODVER);
268 
269 static int
270 uvscom_probe(device_t dev)
271 {
272 	struct usb_attach_arg *uaa = device_get_ivars(dev);
273 
274 	if (uaa->usb_mode != USB_MODE_HOST) {
275 		return (ENXIO);
276 	}
277 	if (uaa->info.bConfigIndex != UVSCOM_CONFIG_INDEX) {
278 		return (ENXIO);
279 	}
280 	if (uaa->info.bIfaceIndex != UVSCOM_IFACE_INDEX) {
281 		return (ENXIO);
282 	}
283 	return (usbd_lookup_id_by_uaa(uvscom_devs, sizeof(uvscom_devs), uaa));
284 }
285 
286 static int
287 uvscom_attach(device_t dev)
288 {
289 	struct usb_attach_arg *uaa = device_get_ivars(dev);
290 	struct uvscom_softc *sc = device_get_softc(dev);
291 	int error;
292 
293 	device_set_usb_desc(dev);
294 	mtx_init(&sc->sc_mtx, "uvscom", NULL, MTX_DEF);
295 
296 	sc->sc_udev = uaa->device;
297 
298 	DPRINTF("sc=%p\n", sc);
299 
300 	sc->sc_iface_no = uaa->info.bIfaceNum;
301 	sc->sc_iface_index = UVSCOM_IFACE_INDEX;
302 
303 	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
304 	    sc->sc_xfer, uvscom_config, UVSCOM_N_TRANSFER, sc, &sc->sc_mtx);
305 
306 	if (error) {
307 		DPRINTF("could not allocate all USB transfers!\n");
308 		goto detach;
309 	}
310 	sc->sc_line = UVSCOM_LINE_INIT;
311 
312 	/* clear stall at first run */
313 	mtx_lock(&sc->sc_mtx);
314 	usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
315 	usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
316 	mtx_unlock(&sc->sc_mtx);
317 
318 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
319 	    &uvscom_callback, &sc->sc_mtx);
320 	if (error) {
321 		goto detach;
322 	}
323 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
324 
325 	/* start interrupt pipe */
326 	mtx_lock(&sc->sc_mtx);
327 	usbd_transfer_start(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
328 	mtx_unlock(&sc->sc_mtx);
329 
330 	return (0);
331 
332 detach:
333 	uvscom_detach(dev);
334 	return (ENXIO);
335 }
336 
337 static int
338 uvscom_detach(device_t dev)
339 {
340 	struct uvscom_softc *sc = device_get_softc(dev);
341 
342 	DPRINTF("sc=%p\n", sc);
343 
344 	/* stop interrupt pipe */
345 
346 	if (sc->sc_xfer[UVSCOM_INTR_DT_RD])
347 		usbd_transfer_stop(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
348 
349 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
350 	usbd_transfer_unsetup(sc->sc_xfer, UVSCOM_N_TRANSFER);
351 	mtx_destroy(&sc->sc_mtx);
352 
353 	return (0);
354 }
355 
356 static void
357 uvscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
358 {
359 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
360 	struct usb_page_cache *pc;
361 	uint32_t actlen;
362 
363 	switch (USB_GET_STATE(xfer)) {
364 	case USB_ST_SETUP:
365 	case USB_ST_TRANSFERRED:
366 tr_setup:
367 		pc = usbd_xfer_get_frame(xfer, 0);
368 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
369 		    UVSCOM_BULK_BUF_SIZE, &actlen)) {
370 
371 			usbd_xfer_set_frame_len(xfer, 0, actlen);
372 			usbd_transfer_submit(xfer);
373 		}
374 		return;
375 
376 	default:			/* Error */
377 		if (error != USB_ERR_CANCELLED) {
378 			/* try to clear stall first */
379 			usbd_xfer_set_stall(xfer);
380 			goto tr_setup;
381 		}
382 		return;
383 	}
384 }
385 
386 static void
387 uvscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
388 {
389 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
390 	struct usb_page_cache *pc;
391 	int actlen;
392 
393 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
394 
395 	switch (USB_GET_STATE(xfer)) {
396 	case USB_ST_TRANSFERRED:
397 		pc = usbd_xfer_get_frame(xfer, 0);
398 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
399 
400 	case USB_ST_SETUP:
401 tr_setup:
402 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
403 		usbd_transfer_submit(xfer);
404 		return;
405 
406 	default:			/* Error */
407 		if (error != USB_ERR_CANCELLED) {
408 			/* try to clear stall first */
409 			usbd_xfer_set_stall(xfer);
410 			goto tr_setup;
411 		}
412 		return;
413 	}
414 }
415 
416 static void
417 uvscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
418 {
419 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
420 	struct usb_page_cache *pc;
421 	uint8_t buf[2];
422 	int actlen;
423 
424 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
425 
426 	switch (USB_GET_STATE(xfer)) {
427 	case USB_ST_TRANSFERRED:
428 		if (actlen >= 2) {
429 
430 			pc = usbd_xfer_get_frame(xfer, 0);
431 			usbd_copy_out(pc, 0, buf, sizeof(buf));
432 
433 			sc->sc_lsr = 0;
434 			sc->sc_msr = 0;
435 			sc->sc_unit_status = buf[1];
436 
437 			if (buf[0] & UVSCOM_TXRDY) {
438 				sc->sc_lsr |= ULSR_TXRDY;
439 			}
440 			if (buf[0] & UVSCOM_RXRDY) {
441 				sc->sc_lsr |= ULSR_RXRDY;
442 			}
443 			if (buf[1] & UVSCOM_CTS) {
444 				sc->sc_msr |= SER_CTS;
445 			}
446 			if (buf[1] & UVSCOM_DSR) {
447 				sc->sc_msr |= SER_DSR;
448 			}
449 			if (buf[1] & UVSCOM_DCD) {
450 				sc->sc_msr |= SER_DCD;
451 			}
452 			/*
453 			 * the UCOM layer will ignore this call if the TTY
454 			 * device is closed!
455 			 */
456 			ucom_status_change(&sc->sc_ucom);
457 		}
458 	case USB_ST_SETUP:
459 tr_setup:
460 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
461 		usbd_transfer_submit(xfer);
462 		return;
463 
464 	default:			/* Error */
465 		if (error != USB_ERR_CANCELLED) {
466 			/* try to clear stall first */
467 			usbd_xfer_set_stall(xfer);
468 			goto tr_setup;
469 		}
470 		return;
471 	}
472 }
473 
474 static void
475 uvscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
476 {
477 	struct uvscom_softc *sc = ucom->sc_parent;
478 
479 	DPRINTF("onoff = %d\n", onoff);
480 
481 	if (onoff)
482 		sc->sc_line |= UVSCOM_DTR;
483 	else
484 		sc->sc_line &= ~UVSCOM_DTR;
485 
486 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
487 }
488 
489 static void
490 uvscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
491 {
492 	struct uvscom_softc *sc = ucom->sc_parent;
493 
494 	DPRINTF("onoff = %d\n", onoff);
495 
496 	if (onoff)
497 		sc->sc_line |= UVSCOM_RTS;
498 	else
499 		sc->sc_line &= ~UVSCOM_RTS;
500 
501 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
502 }
503 
504 static void
505 uvscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
506 {
507 	struct uvscom_softc *sc = ucom->sc_parent;
508 
509 	DPRINTF("onoff = %d\n", onoff);
510 
511 	if (onoff)
512 		sc->sc_line |= UVSCOM_BREAK;
513 	else
514 		sc->sc_line &= ~UVSCOM_BREAK;
515 
516 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
517 }
518 
519 static int
520 uvscom_pre_param(struct ucom_softc *ucom, struct termios *t)
521 {
522 	switch (t->c_ospeed) {
523 		case B150:
524 		case B300:
525 		case B600:
526 		case B1200:
527 		case B2400:
528 		case B4800:
529 		case B9600:
530 		case B19200:
531 		case B38400:
532 		case B57600:
533 		case B115200:
534 		default:
535 		return (EINVAL);
536 	}
537 	return (0);
538 }
539 
540 static void
541 uvscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
542 {
543 	struct uvscom_softc *sc = ucom->sc_parent;
544 	uint16_t value;
545 
546 	DPRINTF("\n");
547 
548 	switch (t->c_ospeed) {
549 	case B150:
550 		value = UVSCOM_SPEED_150BPS;
551 		break;
552 	case B300:
553 		value = UVSCOM_SPEED_300BPS;
554 		break;
555 	case B600:
556 		value = UVSCOM_SPEED_600BPS;
557 		break;
558 	case B1200:
559 		value = UVSCOM_SPEED_1200BPS;
560 		break;
561 	case B2400:
562 		value = UVSCOM_SPEED_2400BPS;
563 		break;
564 	case B4800:
565 		value = UVSCOM_SPEED_4800BPS;
566 		break;
567 	case B9600:
568 		value = UVSCOM_SPEED_9600BPS;
569 		break;
570 	case B19200:
571 		value = UVSCOM_SPEED_19200BPS;
572 		break;
573 	case B38400:
574 		value = UVSCOM_SPEED_38400BPS;
575 		break;
576 	case B57600:
577 		value = UVSCOM_SPEED_57600BPS;
578 		break;
579 	case B115200:
580 		value = UVSCOM_SPEED_115200BPS;
581 		break;
582 	default:
583 		return;
584 	}
585 
586 	uvscom_cfg_write(sc, UVSCOM_SET_SPEED, value);
587 
588 	value = 0;
589 
590 	if (t->c_cflag & CSTOPB) {
591 		value |= UVSCOM_STOP_BIT_2;
592 	}
593 	if (t->c_cflag & PARENB) {
594 		if (t->c_cflag & PARODD) {
595 			value |= UVSCOM_PARITY_ODD;
596 		} else {
597 			value |= UVSCOM_PARITY_EVEN;
598 		}
599 	} else {
600 		value |= UVSCOM_PARITY_NONE;
601 	}
602 
603 	switch (t->c_cflag & CSIZE) {
604 	case CS5:
605 		value |= UVSCOM_DATA_BIT_5;
606 		break;
607 	case CS6:
608 		value |= UVSCOM_DATA_BIT_6;
609 		break;
610 	case CS7:
611 		value |= UVSCOM_DATA_BIT_7;
612 		break;
613 	default:
614 	case CS8:
615 		value |= UVSCOM_DATA_BIT_8;
616 		break;
617 	}
618 
619 	uvscom_cfg_write(sc, UVSCOM_SET_PARAM, value);
620 }
621 
622 static int
623 uvscom_pre_open(struct ucom_softc *ucom)
624 {
625 	struct uvscom_softc *sc = ucom->sc_parent;
626 
627 	DPRINTF("sc = %p\n", sc);
628 
629 	/* check if PC card was inserted */
630 
631 	if (sc->sc_unit_status & UVSCOM_NOCARD) {
632 		DPRINTF("no PC card!\n");
633 		return (ENXIO);
634 	}
635 	return (0);
636 }
637 
638 static void
639 uvscom_cfg_open(struct ucom_softc *ucom)
640 {
641 	struct uvscom_softc *sc = ucom->sc_parent;
642 
643 	DPRINTF("sc = %p\n", sc);
644 
645 	uvscom_cfg_read_status(sc);
646 }
647 
648 static void
649 uvscom_cfg_close(struct ucom_softc *ucom)
650 {
651 	struct uvscom_softc *sc = ucom->sc_parent;
652 
653 	DPRINTF("sc=%p\n", sc);
654 
655 	uvscom_cfg_write(sc, UVSCOM_SHUTDOWN, 0);
656 }
657 
658 static void
659 uvscom_start_read(struct ucom_softc *ucom)
660 {
661 	struct uvscom_softc *sc = ucom->sc_parent;
662 
663 	usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
664 }
665 
666 static void
667 uvscom_stop_read(struct ucom_softc *ucom)
668 {
669 	struct uvscom_softc *sc = ucom->sc_parent;
670 
671 	usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
672 }
673 
674 static void
675 uvscom_start_write(struct ucom_softc *ucom)
676 {
677 	struct uvscom_softc *sc = ucom->sc_parent;
678 
679 	usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
680 }
681 
682 static void
683 uvscom_stop_write(struct ucom_softc *ucom)
684 {
685 	struct uvscom_softc *sc = ucom->sc_parent;
686 
687 	usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
688 }
689 
690 static void
691 uvscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
692 {
693 	struct uvscom_softc *sc = ucom->sc_parent;
694 
695 	*lsr = sc->sc_lsr;
696 	*msr = sc->sc_msr;
697 }
698 
699 static void
700 uvscom_cfg_write(struct uvscom_softc *sc, uint8_t index, uint16_t value)
701 {
702 	struct usb_device_request req;
703 	usb_error_t err;
704 
705 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
706 	req.bRequest = index;
707 	USETW(req.wValue, value);
708 	USETW(req.wIndex, 0);
709 	USETW(req.wLength, 0);
710 
711 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
712 	    &req, NULL, 0, 1000);
713 	if (err) {
714 		DPRINTFN(0, "device request failed, err=%s "
715 		    "(ignored)\n", usbd_errstr(err));
716 	}
717 }
718 
719 static uint16_t
720 uvscom_cfg_read_status(struct uvscom_softc *sc)
721 {
722 	struct usb_device_request req;
723 	usb_error_t err;
724 	uint8_t data[2];
725 
726 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
727 	req.bRequest = UVSCOM_READ_STATUS;
728 	USETW(req.wValue, 0);
729 	USETW(req.wIndex, 0);
730 	USETW(req.wLength, 2);
731 
732 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
733 	    &req, data, 0, 1000);
734 	if (err) {
735 		DPRINTFN(0, "device request failed, err=%s "
736 		    "(ignored)\n", usbd_errstr(err));
737 	}
738 	return (data[0] | (data[1] << 8));
739 }
740 
741 static void
742 uvscom_poll(struct ucom_softc *ucom)
743 {
744 	struct uvscom_softc *sc = ucom->sc_parent;
745 	usbd_transfer_poll(sc->sc_xfer, UVSCOM_N_TRANSFER);
746 }
747