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