xref: /freebsd/sys/dev/usb/serial/uvscom.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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 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, NULL, NULL);
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 			usbd_xfer_set_frame_len(xfer, 0, actlen);
396 			usbd_transfer_submit(xfer);
397 		}
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_read_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 	int actlen;
416 
417 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
418 
419 	switch (USB_GET_STATE(xfer)) {
420 	case USB_ST_TRANSFERRED:
421 		pc = usbd_xfer_get_frame(xfer, 0);
422 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
423 
424 	case USB_ST_SETUP:
425 tr_setup:
426 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
427 		usbd_transfer_submit(xfer);
428 		return;
429 
430 	default:			/* Error */
431 		if (error != USB_ERR_CANCELLED) {
432 			/* try to clear stall first */
433 			usbd_xfer_set_stall(xfer);
434 			goto tr_setup;
435 		}
436 		return;
437 	}
438 }
439 
440 static void
441 uvscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
442 {
443 	struct uvscom_softc *sc = usbd_xfer_softc(xfer);
444 	struct usb_page_cache *pc;
445 	uint8_t buf[2];
446 	int actlen;
447 
448 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
449 
450 	switch (USB_GET_STATE(xfer)) {
451 	case USB_ST_TRANSFERRED:
452 		if (actlen >= 2) {
453 			pc = usbd_xfer_get_frame(xfer, 0);
454 			usbd_copy_out(pc, 0, buf, sizeof(buf));
455 
456 			sc->sc_lsr = 0;
457 			sc->sc_msr = 0;
458 			sc->sc_unit_status = buf[1];
459 
460 			if (buf[0] & UVSCOM_TXRDY) {
461 				sc->sc_lsr |= ULSR_TXRDY;
462 			}
463 			if (buf[0] & UVSCOM_RXRDY) {
464 				sc->sc_lsr |= ULSR_RXRDY;
465 			}
466 			if (buf[1] & UVSCOM_CTS) {
467 				sc->sc_msr |= SER_CTS;
468 			}
469 			if (buf[1] & UVSCOM_DSR) {
470 				sc->sc_msr |= SER_DSR;
471 			}
472 			if (buf[1] & UVSCOM_DCD) {
473 				sc->sc_msr |= SER_DCD;
474 			}
475 			/*
476 			 * the UCOM layer will ignore this call if the TTY
477 			 * device is closed!
478 			 */
479 			ucom_status_change(&sc->sc_ucom);
480 		}
481 	case USB_ST_SETUP:
482 tr_setup:
483 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
484 		usbd_transfer_submit(xfer);
485 		return;
486 
487 	default:			/* Error */
488 		if (error != USB_ERR_CANCELLED) {
489 			/* try to clear stall first */
490 			usbd_xfer_set_stall(xfer);
491 			goto tr_setup;
492 		}
493 		return;
494 	}
495 }
496 
497 static void
498 uvscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
499 {
500 	struct uvscom_softc *sc = ucom->sc_parent;
501 
502 	DPRINTF("onoff = %d\n", onoff);
503 
504 	if (onoff)
505 		sc->sc_line |= UVSCOM_DTR;
506 	else
507 		sc->sc_line &= ~UVSCOM_DTR;
508 
509 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
510 }
511 
512 static void
513 uvscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
514 {
515 	struct uvscom_softc *sc = ucom->sc_parent;
516 
517 	DPRINTF("onoff = %d\n", onoff);
518 
519 	if (onoff)
520 		sc->sc_line |= UVSCOM_RTS;
521 	else
522 		sc->sc_line &= ~UVSCOM_RTS;
523 
524 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
525 }
526 
527 static void
528 uvscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
529 {
530 	struct uvscom_softc *sc = ucom->sc_parent;
531 
532 	DPRINTF("onoff = %d\n", onoff);
533 
534 	if (onoff)
535 		sc->sc_line |= UVSCOM_BREAK;
536 	else
537 		sc->sc_line &= ~UVSCOM_BREAK;
538 
539 	uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
540 }
541 
542 static int
543 uvscom_pre_param(struct ucom_softc *ucom, struct termios *t)
544 {
545 	switch (t->c_ospeed) {
546 		case B150:
547 		case B300:
548 		case B600:
549 		case B1200:
550 		case B2400:
551 		case B4800:
552 		case B9600:
553 		case B19200:
554 		case B38400:
555 		case B57600:
556 		case B115200:
557 		default:
558 		return (EINVAL);
559 	}
560 	return (0);
561 }
562 
563 static void
564 uvscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
565 {
566 	struct uvscom_softc *sc = ucom->sc_parent;
567 	uint16_t value;
568 
569 	DPRINTF("\n");
570 
571 	switch (t->c_ospeed) {
572 	case B150:
573 		value = UVSCOM_SPEED_150BPS;
574 		break;
575 	case B300:
576 		value = UVSCOM_SPEED_300BPS;
577 		break;
578 	case B600:
579 		value = UVSCOM_SPEED_600BPS;
580 		break;
581 	case B1200:
582 		value = UVSCOM_SPEED_1200BPS;
583 		break;
584 	case B2400:
585 		value = UVSCOM_SPEED_2400BPS;
586 		break;
587 	case B4800:
588 		value = UVSCOM_SPEED_4800BPS;
589 		break;
590 	case B9600:
591 		value = UVSCOM_SPEED_9600BPS;
592 		break;
593 	case B19200:
594 		value = UVSCOM_SPEED_19200BPS;
595 		break;
596 	case B38400:
597 		value = UVSCOM_SPEED_38400BPS;
598 		break;
599 	case B57600:
600 		value = UVSCOM_SPEED_57600BPS;
601 		break;
602 	case B115200:
603 		value = UVSCOM_SPEED_115200BPS;
604 		break;
605 	default:
606 		return;
607 	}
608 
609 	uvscom_cfg_write(sc, UVSCOM_SET_SPEED, value);
610 
611 	value = 0;
612 
613 	if (t->c_cflag & CSTOPB) {
614 		value |= UVSCOM_STOP_BIT_2;
615 	}
616 	if (t->c_cflag & PARENB) {
617 		if (t->c_cflag & PARODD) {
618 			value |= UVSCOM_PARITY_ODD;
619 		} else {
620 			value |= UVSCOM_PARITY_EVEN;
621 		}
622 	} else {
623 		value |= UVSCOM_PARITY_NONE;
624 	}
625 
626 	switch (t->c_cflag & CSIZE) {
627 	case CS5:
628 		value |= UVSCOM_DATA_BIT_5;
629 		break;
630 	case CS6:
631 		value |= UVSCOM_DATA_BIT_6;
632 		break;
633 	case CS7:
634 		value |= UVSCOM_DATA_BIT_7;
635 		break;
636 	default:
637 	case CS8:
638 		value |= UVSCOM_DATA_BIT_8;
639 		break;
640 	}
641 
642 	uvscom_cfg_write(sc, UVSCOM_SET_PARAM, value);
643 }
644 
645 static int
646 uvscom_pre_open(struct ucom_softc *ucom)
647 {
648 	struct uvscom_softc *sc = ucom->sc_parent;
649 
650 	DPRINTF("sc = %p\n", sc);
651 
652 	/* check if PC card was inserted */
653 
654 	if (sc->sc_unit_status & UVSCOM_NOCARD) {
655 		DPRINTF("no PC card!\n");
656 		return (ENXIO);
657 	}
658 	return (0);
659 }
660 
661 static void
662 uvscom_cfg_open(struct ucom_softc *ucom)
663 {
664 	struct uvscom_softc *sc = ucom->sc_parent;
665 
666 	DPRINTF("sc = %p\n", sc);
667 
668 	uvscom_cfg_read_status(sc);
669 }
670 
671 static void
672 uvscom_cfg_close(struct ucom_softc *ucom)
673 {
674 	struct uvscom_softc *sc = ucom->sc_parent;
675 
676 	DPRINTF("sc=%p\n", sc);
677 
678 	uvscom_cfg_write(sc, UVSCOM_SHUTDOWN, 0);
679 }
680 
681 static void
682 uvscom_start_read(struct ucom_softc *ucom)
683 {
684 	struct uvscom_softc *sc = ucom->sc_parent;
685 
686 	usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
687 }
688 
689 static void
690 uvscom_stop_read(struct ucom_softc *ucom)
691 {
692 	struct uvscom_softc *sc = ucom->sc_parent;
693 
694 	usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
695 }
696 
697 static void
698 uvscom_start_write(struct ucom_softc *ucom)
699 {
700 	struct uvscom_softc *sc = ucom->sc_parent;
701 
702 	usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
703 }
704 
705 static void
706 uvscom_stop_write(struct ucom_softc *ucom)
707 {
708 	struct uvscom_softc *sc = ucom->sc_parent;
709 
710 	usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
711 }
712 
713 static void
714 uvscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
715 {
716 	struct uvscom_softc *sc = ucom->sc_parent;
717 
718 	*lsr = sc->sc_lsr;
719 	*msr = sc->sc_msr;
720 }
721 
722 static void
723 uvscom_cfg_write(struct uvscom_softc *sc, uint8_t index, uint16_t value)
724 {
725 	struct usb_device_request req;
726 	usb_error_t err;
727 
728 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
729 	req.bRequest = index;
730 	USETW(req.wValue, value);
731 	USETW(req.wIndex, 0);
732 	USETW(req.wLength, 0);
733 
734 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
735 	    &req, NULL, 0, 1000);
736 	if (err) {
737 		DPRINTFN(0, "device request failed, err=%s "
738 		    "(ignored)\n", usbd_errstr(err));
739 	}
740 }
741 
742 static uint16_t
743 uvscom_cfg_read_status(struct uvscom_softc *sc)
744 {
745 	struct usb_device_request req;
746 	usb_error_t err;
747 	uint8_t data[2];
748 
749 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
750 	req.bRequest = UVSCOM_READ_STATUS;
751 	USETW(req.wValue, 0);
752 	USETW(req.wIndex, 0);
753 	USETW(req.wLength, 2);
754 
755 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
756 	    &req, data, 0, 1000);
757 	if (err) {
758 		DPRINTFN(0, "device request failed, err=%s "
759 		    "(ignored)\n", usbd_errstr(err));
760 	}
761 	return (data[0] | (data[1] << 8));
762 }
763 
764 static void
765 uvscom_poll(struct ucom_softc *ucom)
766 {
767 	struct uvscom_softc *sc = ucom->sc_parent;
768 	usbd_transfer_poll(sc->sc_xfer, UVSCOM_N_TRANSFER);
769 }
770