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