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