xref: /netbsd-src/sys/dev/usb/uvscom.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: uvscom.c,v 1.31 2016/11/25 12:56:29 skrll Exp $	*/
2 /*-
3  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/usb/uvscom.c,v 1.1 2002/03/18 18:23:39 joe Exp $
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/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: uvscom.c,v 1.31 2016/11/25 12:56:29 skrll Exp $");
39 
40 #ifdef _KERNEL_OPT
41 #include "opt_usb.h"
42 #endif
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/kmem.h>
48 #include <sys/fcntl.h>
49 #include <sys/conf.h>
50 #include <sys/tty.h>
51 #include <sys/file.h>
52 #include <sys/ioctl.h>
53 #include <sys/device.h>
54 #include <sys/proc.h>
55 #include <sys/poll.h>
56 
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbcdc.h>
59 
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62 #include <dev/usb/usbdevs.h>
63 #include <dev/usb/usb_quirks.h>
64 
65 #include <dev/usb/ucomvar.h>
66 
67 #ifdef UVSCOM_DEBUG
68 static int	uvscomdebug = 1;
69 
70 #define DPRINTFN(n, x)  do { \
71 				if (uvscomdebug > (n)) \
72 					printf x; \
73 			} while (0)
74 #else
75 #define DPRINTFN(n, x)
76 #endif
77 #define DPRINTF(x) DPRINTFN(0, x)
78 
79 #define	UVSCOM_CONFIG_INDEX	0
80 #define	UVSCOM_IFACE_INDEX	0
81 
82 #define UVSCOM_INTR_INTERVAL	100	/* mS */
83 
84 #define UVSCOM_UNIT_WAIT	5
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 #if 0
126 #define UVSCOM_PARITY_UNK	0x10
127 #endif
128 #define UVSCOM_PARITY_ODD	0x08
129 #define UVSCOM_PARITY_NONE	0x00
130 
131 /* Status bits */
132 #define UVSCOM_TXRDY		0x04
133 #define UVSCOM_RXRDY		0x01
134 
135 #define UVSCOM_DCD		0x08
136 #define UVSCOM_NOCARD		0x04
137 #define UVSCOM_DSR		0x02
138 #define UVSCOM_CTS		0x01
139 #define UVSCOM_USTAT_MASK	(UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
140 
141 struct	uvscom_softc {
142 	device_t		sc_dev;		/* base device */
143 	struct usbd_device *	sc_udev;	/* USB device */
144 	struct usbd_interface *	sc_iface;	/* interface */
145 	int			sc_iface_number;/* interface number */
146 
147 	struct usbd_interface *	sc_intr_iface;	/* interrupt interface */
148 	int			sc_intr_number;	/* interrupt number */
149 	struct usbd_pipe *	sc_intr_pipe;	/* interrupt pipe */
150 	u_char			*sc_intr_buf;	/* interrupt buffer */
151 	int			sc_isize;
152 
153 	u_char			sc_dtr;		/* current DTR state */
154 	u_char			sc_rts;		/* current RTS state */
155 
156 	u_char			sc_lsr;		/* Local status register */
157 	u_char			sc_msr;		/* uvscom status register */
158 
159 	uint16_t		sc_lcr;		/* Line control */
160 	u_char			sc_usr;		/* unit status */
161 
162 	device_t		sc_subdev;	/* ucom device */
163 	u_char			sc_dying;	/* disconnecting */
164 };
165 
166 /*
167  * These are the maximum number of bytes transferred per frame.
168  * The output buffer size cannot be increased due to the size encoding.
169  */
170 #define UVSCOMIBUFSIZE 512
171 #define UVSCOMOBUFSIZE 64
172 
173 Static	usbd_status uvscom_readstat(struct uvscom_softc *);
174 Static	usbd_status uvscom_shutdown(struct uvscom_softc *);
175 Static	usbd_status uvscom_reset(struct uvscom_softc *);
176 Static	usbd_status uvscom_set_line_coding(struct uvscom_softc *,
177 					   uint16_t, uint16_t);
178 Static	usbd_status uvscom_set_line(struct uvscom_softc *, uint16_t);
179 Static	usbd_status uvscom_set_crtscts(struct uvscom_softc *);
180 Static	void uvscom_get_status(void *, int, u_char *, u_char *);
181 Static	void uvscom_dtr(struct uvscom_softc *, int);
182 Static	void uvscom_rts(struct uvscom_softc *, int);
183 Static	void uvscom_break(struct uvscom_softc *, int);
184 
185 Static	void uvscom_set(void *, int, int, int);
186 Static	void uvscom_intr(struct usbd_xfer *, void *, usbd_status);
187 Static	int  uvscom_param(void *, int, struct termios *);
188 Static	int  uvscom_open(void *, int);
189 Static	void uvscom_close(void *, int);
190 
191 struct ucom_methods uvscom_methods = {
192 	.ucom_get_status = uvscom_get_status,
193 	.ucom_set = uvscom_set,
194 	.ucom_param = uvscom_param,
195 	.ucom_ioctl = NULL,		/* TODO */
196 	.ucom_open = uvscom_open,
197 	.ucom_close = uvscom_close,
198 	.ucom_read = NULL,
199 	.ucom_write = NULL
200 };
201 
202 static const struct usb_devno uvscom_devs [] = {
203 	/* SUNTAC U-Cable type A4 */
204 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4 },
205 	/* SUNTAC U-Cable type D2 */
206 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L },
207 	/* SUNTAC U-Cable type P1 */
208 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1 },
209 	/* SUNTAC Slipper U  */
210 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U },
211 	/* SUNTAC Ir-Trinity */
212 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U },
213 };
214 #define uvscom_lookup(v, p) usb_lookup(uvscom_devs, v, p)
215 
216 int uvscom_match(device_t, cfdata_t, void *);
217 void uvscom_attach(device_t, device_t, void *);
218 void uvscom_childdet(device_t, device_t);
219 int uvscom_detach(device_t, int);
220 int uvscom_activate(device_t, enum devact);
221 extern struct cfdriver uvscom_cd;
222 CFATTACH_DECL2_NEW(uvscom, sizeof(struct uvscom_softc), uvscom_match,
223     uvscom_attach, uvscom_detach, uvscom_activate, NULL, uvscom_childdet);
224 
225 int
226 uvscom_match(device_t parent, cfdata_t match, void *aux)
227 {
228 	struct usb_attach_arg *uaa = aux;
229 
230 	return uvscom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
231 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
232 }
233 
234 void
235 uvscom_attach(device_t parent, device_t self, void *aux)
236 {
237 	struct uvscom_softc *sc = device_private(self);
238 	struct usb_attach_arg *uaa = aux;
239 	struct usbd_device *dev = uaa->uaa_device;
240 	usb_config_descriptor_t *cdesc;
241 	usb_interface_descriptor_t *id;
242 	usb_endpoint_descriptor_t *ed;
243 	char *devinfop;
244 	usbd_status err;
245 	int i;
246 	struct ucom_attach_args ucaa;
247 
248 	aprint_naive("\n");
249 	aprint_normal("\n");
250 
251 	devinfop = usbd_devinfo_alloc(dev, 0);
252 	aprint_normal_dev(self, "%s\n", devinfop);
253 	usbd_devinfo_free(devinfop);
254 
255 	sc->sc_dev = self;
256         sc->sc_udev = dev;
257 
258 	DPRINTF(("uvscom attach: sc = %p\n", sc));
259 
260 	/* initialize endpoints */
261 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
262 	sc->sc_intr_number = -1;
263 	sc->sc_intr_pipe = NULL;
264 
265 	/* Move the device into the configured state. */
266 	err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
267 	if (err) {
268 		aprint_error_dev(self, "failed to set configuration, err=%s\n",
269 		    usbd_errstr(err));
270 		sc->sc_dying = 1;
271 		return;
272 	}
273 
274 	/* get the config descriptor */
275 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
276 
277 	if (cdesc == NULL) {
278 		aprint_error_dev(self,
279 		    "failed to get configuration descriptor\n");
280 		sc->sc_dying = 1;
281 		return;
282 	}
283 
284 	/* get the common interface */
285 	err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
286 					   &sc->sc_iface);
287 	if (err) {
288 		aprint_error_dev(self, "failed to get interface, err=%s\n",
289 		    usbd_errstr(err));
290 		sc->sc_dying = 1;
291 		return;
292 	}
293 
294 	id = usbd_get_interface_descriptor(sc->sc_iface);
295 	sc->sc_iface_number = id->bInterfaceNumber;
296 
297 	/* Find endpoints */
298 	for (i = 0; i < id->bNumEndpoints; i++) {
299 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
300 		if (ed == NULL) {
301 			aprint_error_dev(self,
302 			    "no endpoint descriptor for %d\n", i);
303 			sc->sc_dying = 1;
304 			return;
305 		}
306 
307 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
308 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
309 			ucaa.ucaa_bulkin = ed->bEndpointAddress;
310 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
311 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
312 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
313 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
314 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
315 			sc->sc_intr_number = ed->bEndpointAddress;
316 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
317 		}
318 	}
319 
320 	if (ucaa.ucaa_bulkin == -1) {
321 		aprint_error_dev(self, "Could not find data bulk in\n");
322 		sc->sc_dying = 1;
323 		return;
324 	}
325 	if (ucaa.ucaa_bulkout == -1) {
326 		aprint_error_dev(self, "Could not find data bulk out\n");
327 		sc->sc_dying = 1;
328 		return;
329 	}
330 	if (sc->sc_intr_number == -1) {
331 		aprint_error_dev(self, "Could not find interrupt in\n");
332 		sc->sc_dying = 1;
333 		return;
334 	}
335 
336 	sc->sc_dtr = sc->sc_rts = 0;
337 	sc->sc_lcr = UVSCOM_LINE_INIT;
338 
339 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
340 	/* ucaa_bulkin, ucaa_bulkout set above */
341 	ucaa.ucaa_ibufsize = UVSCOMIBUFSIZE;
342 	ucaa.ucaa_obufsize = UVSCOMOBUFSIZE;
343 	ucaa.ucaa_ibufsizepad = UVSCOMIBUFSIZE;
344 	ucaa.ucaa_opkthdrlen = 0;
345 	ucaa.ucaa_device = dev;
346 	ucaa.ucaa_iface = sc->sc_iface;
347 	ucaa.ucaa_methods = &uvscom_methods;
348 	ucaa.ucaa_arg = sc;
349 	ucaa.ucaa_info = NULL;
350 
351 	err = uvscom_reset(sc);
352 
353 	if (err) {
354 		aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err));
355 		sc->sc_dying = 1;
356 		return;
357 	}
358 
359 	DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
360 		 ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number));
361 
362 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
363 
364 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
365 		ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number ));
366 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
367 					    ucomprint, ucomsubmatch);
368 
369 	return;
370 }
371 
372 void
373 uvscom_childdet(device_t self, device_t child)
374 {
375 	struct uvscom_softc *sc = device_private(self);
376 
377 	KASSERT(sc->sc_subdev == child);
378 	sc->sc_subdev = NULL;
379 }
380 
381 int
382 uvscom_detach(device_t self, int flags)
383 {
384 	struct uvscom_softc *sc = device_private(self);
385 	int rv = 0;
386 
387 	DPRINTF(("uvscom_detach: sc = %p\n", sc));
388 
389 	sc->sc_dying = 1;
390 
391 	if (sc->sc_intr_pipe != NULL) {
392 		usbd_abort_pipe(sc->sc_intr_pipe);
393 		usbd_close_pipe(sc->sc_intr_pipe);
394 		kmem_free(sc->sc_intr_buf, sc->sc_isize);
395 		sc->sc_intr_pipe = NULL;
396 	}
397 
398 	sc->sc_dying = 1;
399 	if (sc->sc_subdev != NULL)
400 		rv = config_detach(sc->sc_subdev, flags);
401 
402 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
403 
404 	return rv;
405 }
406 
407 int
408 uvscom_activate(device_t self, enum devact act)
409 {
410 	struct uvscom_softc *sc = device_private(self);
411 
412 	switch (act) {
413 	case DVACT_DEACTIVATE:
414 		sc->sc_dying = 1;
415 		return 0;
416 	default:
417 		return EOPNOTSUPP;
418 	}
419 }
420 
421 Static usbd_status
422 uvscom_readstat(struct uvscom_softc *sc)
423 {
424 	usb_device_request_t req;
425 	usbd_status err;
426 	uint16_t r;
427 
428 	DPRINTF(("%s: send readstat\n", device_xname(sc->sc_dev)));
429 
430 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
431 	req.bRequest = UVSCOM_READ_STATUS;
432 	USETW(req.wValue, 0);
433 	USETW(req.wIndex, 0);
434 	USETW(req.wLength, 2);
435 
436 	err = usbd_do_request(sc->sc_udev, &req, &r);
437 	if (err) {
438 		aprint_error_dev(sc->sc_dev, "uvscom_readstat: %s\n",
439 		    usbd_errstr(err));
440 		return err;
441 	}
442 
443 	DPRINTF(("%s: uvscom_readstat: r = %d\n",
444 		 device_xname(sc->sc_dev), r));
445 
446 	return USBD_NORMAL_COMPLETION;
447 }
448 
449 Static usbd_status
450 uvscom_shutdown(struct uvscom_softc *sc)
451 {
452 	usb_device_request_t req;
453 	usbd_status err;
454 
455 	DPRINTF(("%s: send shutdown\n", device_xname(sc->sc_dev)));
456 
457 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
458 	req.bRequest = UVSCOM_SHUTDOWN;
459 	USETW(req.wValue, 0);
460 	USETW(req.wIndex, 0);
461 	USETW(req.wLength, 0);
462 
463 	err = usbd_do_request(sc->sc_udev, &req, NULL);
464 	if (err) {
465 		aprint_error_dev(sc->sc_dev, "uvscom_shutdown: %s\n",
466 		   usbd_errstr(err));
467 		return err;
468 	}
469 
470 	return USBD_NORMAL_COMPLETION;
471 }
472 
473 Static usbd_status
474 uvscom_reset(struct uvscom_softc *sc)
475 {
476 	DPRINTF(("%s: uvscom_reset\n", device_xname(sc->sc_dev)));
477 
478 	return USBD_NORMAL_COMPLETION;
479 }
480 
481 Static usbd_status
482 uvscom_set_crtscts(struct uvscom_softc *sc)
483 {
484 	DPRINTF(("%s: uvscom_set_crtscts\n", device_xname(sc->sc_dev)));
485 
486 	return USBD_NORMAL_COMPLETION;
487 }
488 
489 Static usbd_status
490 uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
491 {
492 	usb_device_request_t req;
493 	usbd_status err;
494 
495 	DPRINTF(("%s: uvscom_set_line: %04x\n",
496 		 device_xname(sc->sc_dev), line));
497 
498 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
499 	req.bRequest = UVSCOM_LINE_CTL;
500 	USETW(req.wValue, line);
501 	USETW(req.wIndex, 0);
502 	USETW(req.wLength, 0);
503 
504 	err = usbd_do_request(sc->sc_udev, &req, NULL);
505 	if (err) {
506 		aprint_error_dev(sc->sc_dev, "uvscom_set_line: %s\n",
507 		    usbd_errstr(err));
508 		return err;
509 	}
510 
511 	return USBD_NORMAL_COMPLETION;
512 }
513 
514 Static usbd_status
515 uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
516 {
517 	usb_device_request_t req;
518 	usbd_status err;
519 
520 	DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
521 		 device_xname(sc->sc_dev), lsp, ls));
522 
523 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
524 	req.bRequest = UVSCOM_SET_SPEED;
525 	USETW(req.wValue, lsp);
526 	USETW(req.wIndex, 0);
527 	USETW(req.wLength, 0);
528 
529 	err = usbd_do_request(sc->sc_udev, &req, NULL);
530 	if (err) {
531 		aprint_error_dev(sc->sc_dev, "uvscom_set_line_coding: %s\n",
532 		   usbd_errstr(err));
533 		return err;
534 	}
535 
536 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
537 	req.bRequest = UVSCOM_SET_PARAM;
538 	USETW(req.wValue, ls);
539 	USETW(req.wIndex, 0);
540 	USETW(req.wLength, 0);
541 
542 	err = usbd_do_request(sc->sc_udev, &req, NULL);
543 	if (err) {
544 		aprint_error_dev(sc->sc_dev, "uvscom_set_line_coding: %s\n",
545 		   usbd_errstr(err));
546 		return err;
547 	}
548 
549 	return USBD_NORMAL_COMPLETION;
550 }
551 
552 Static void
553 uvscom_dtr(struct uvscom_softc *sc, int onoff)
554 {
555 	DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
556 		 device_xname(sc->sc_dev), onoff));
557 
558 	if (sc->sc_dtr == onoff)
559 		return;			/* no change */
560 
561 	sc->sc_dtr = onoff;
562 
563 	if (onoff)
564 		SET(sc->sc_lcr, UVSCOM_DTR);
565 	else
566 		CLR(sc->sc_lcr, UVSCOM_DTR);
567 
568 	uvscom_set_line(sc, sc->sc_lcr);
569 }
570 
571 Static void
572 uvscom_rts(struct uvscom_softc *sc, int onoff)
573 {
574 	DPRINTF(("%s: uvscom_rts: onoff = %d\n",
575 		 device_xname(sc->sc_dev), onoff));
576 
577 	if (sc->sc_rts == onoff)
578 		return;			/* no change */
579 
580 	sc->sc_rts = onoff;
581 
582 	if (onoff)
583 		SET(sc->sc_lcr, UVSCOM_RTS);
584 	else
585 		CLR(sc->sc_lcr, UVSCOM_RTS);
586 
587 	uvscom_set_line(sc, sc->sc_lcr);
588 }
589 
590 Static void
591 uvscom_break(struct uvscom_softc *sc, int onoff)
592 {
593 	DPRINTF(("%s: uvscom_break: onoff = %d\n",
594 		 device_xname(sc->sc_dev), onoff));
595 
596 	if (onoff)
597 		uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
598 }
599 
600 Static void
601 uvscom_set(void *addr, int portno, int reg, int onoff)
602 {
603 	struct uvscom_softc *sc = addr;
604 
605 	switch (reg) {
606 	case UCOM_SET_DTR:
607 		uvscom_dtr(sc, onoff);
608 		break;
609 	case UCOM_SET_RTS:
610 		uvscom_rts(sc, onoff);
611 		break;
612 	case UCOM_SET_BREAK:
613 		uvscom_break(sc, onoff);
614 		break;
615 	default:
616 		break;
617 	}
618 }
619 
620 Static int
621 uvscom_param(void *addr, int portno, struct termios *t)
622 {
623 	struct uvscom_softc *sc = addr;
624 	usbd_status err;
625 	uint16_t lsp;
626 	uint16_t ls;
627 
628 	DPRINTF(("%s: uvscom_param: sc = %p\n",
629 		 device_xname(sc->sc_dev), sc));
630 
631 	ls = 0;
632 
633 	switch (t->c_ospeed) {
634 	case B150:
635 		lsp = UVSCOM_SPEED_150BPS;
636 		break;
637 	case B300:
638 		lsp = UVSCOM_SPEED_300BPS;
639 		break;
640 	case B600:
641 		lsp = UVSCOM_SPEED_600BPS;
642 		break;
643 	case B1200:
644 		lsp = UVSCOM_SPEED_1200BPS;
645 		break;
646 	case B2400:
647 		lsp = UVSCOM_SPEED_2400BPS;
648 		break;
649 	case B4800:
650 		lsp = UVSCOM_SPEED_4800BPS;
651 		break;
652 	case B9600:
653 		lsp = UVSCOM_SPEED_9600BPS;
654 		break;
655 	case B19200:
656 		lsp = UVSCOM_SPEED_19200BPS;
657 		break;
658 	case B38400:
659 		lsp = UVSCOM_SPEED_38400BPS;
660 		break;
661 	case B57600:
662 		lsp = UVSCOM_SPEED_57600BPS;
663 		break;
664 	case B115200:
665 		lsp = UVSCOM_SPEED_115200BPS;
666 		break;
667 	default:
668 		return EIO;
669 	}
670 
671 	if (ISSET(t->c_cflag, CSTOPB))
672 		SET(ls, UVSCOM_STOP_BIT_2);
673 	else
674 		SET(ls, UVSCOM_STOP_BIT_1);
675 
676 	if (ISSET(t->c_cflag, PARENB)) {
677 		if (ISSET(t->c_cflag, PARODD))
678 			SET(ls, UVSCOM_PARITY_ODD);
679 		else
680 			SET(ls, UVSCOM_PARITY_EVEN);
681 	} else
682 		SET(ls, UVSCOM_PARITY_NONE);
683 
684 	switch (ISSET(t->c_cflag, CSIZE)) {
685 	case CS5:
686 		SET(ls, UVSCOM_DATA_BIT_5);
687 		break;
688 	case CS6:
689 		SET(ls, UVSCOM_DATA_BIT_6);
690 		break;
691 	case CS7:
692 		SET(ls, UVSCOM_DATA_BIT_7);
693 		break;
694 	case CS8:
695 		SET(ls, UVSCOM_DATA_BIT_8);
696 		break;
697 	default:
698 		return EIO;
699 	}
700 
701 	err = uvscom_set_line_coding(sc, lsp, ls);
702 	if (err)
703 		return EIO;
704 
705 	if (ISSET(t->c_cflag, CRTSCTS)) {
706 		err = uvscom_set_crtscts(sc);
707 		if (err)
708 			return EIO;
709 	}
710 
711 	return 0;
712 }
713 
714 Static int
715 uvscom_open(void *addr, int portno)
716 {
717 	struct uvscom_softc *sc = addr;
718 	int err;
719 	int i;
720 
721 	if (sc->sc_dying)
722 		return EIO;
723 
724 	DPRINTF(("uvscom_open: sc = %p\n", sc));
725 
726 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
727 		DPRINTF(("uvscom_open: open interrupt pipe.\n"));
728 
729 		sc->sc_usr = 0;		/* clear unit status */
730 
731 		err = uvscom_readstat(sc);
732 		if (err) {
733 			DPRINTF(("%s: uvscom_open: readstat faild\n",
734 				 device_xname(sc->sc_dev)));
735 			return EIO;
736 		}
737 
738 		sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
739 		err = usbd_open_pipe_intr(sc->sc_iface,
740 					  sc->sc_intr_number,
741 					  USBD_SHORT_XFER_OK,
742 					  &sc->sc_intr_pipe,
743 					  sc,
744 					  sc->sc_intr_buf,
745 					  sc->sc_isize,
746 					  uvscom_intr,
747 					  UVSCOM_INTR_INTERVAL);
748 		if (err) {
749 			aprint_error_dev(sc->sc_dev,
750 			    "cannot open interrupt pipe (addr %d)\n",
751 				 sc->sc_intr_number);
752 			return EIO;
753 		}
754 	} else {
755 		DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
756 	}
757 
758 	if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
759 		/* unit is not ready */
760 
761 		for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
762 			tsleep(&err, TTIPRI, "uvsop", hz);	/* XXX */
763 			if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
764 				break;
765 		}
766 		if (i == 0) {
767 			DPRINTF(("%s: unit is not ready\n",
768 				 device_xname(sc->sc_dev)));
769 			return EIO;
770 		}
771 
772 		/* check PC card was inserted */
773 		if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
774 			DPRINTF(("%s: no card\n",
775 				 device_xname(sc->sc_dev)));
776 			return EIO;
777 		}
778 	}
779 
780 	return 0;
781 }
782 
783 Static void
784 uvscom_close(void *addr, int portno)
785 {
786 	struct uvscom_softc *sc = addr;
787 	int err;
788 
789 	if (sc->sc_dying)
790 		return;
791 
792 	DPRINTF(("uvscom_close: close\n"));
793 
794 	uvscom_shutdown(sc);
795 
796 	if (sc->sc_intr_pipe != NULL) {
797 		err = usbd_abort_pipe(sc->sc_intr_pipe);
798 		if (err)
799 			aprint_error_dev(sc->sc_dev,
800 			    "abort interrupt pipe failed: %s\n",
801 			    usbd_errstr(err));
802 		err = usbd_close_pipe(sc->sc_intr_pipe);
803 		if (err)
804 			aprint_error_dev(sc->sc_dev,
805 			    "lose interrupt pipe failed: %s\n",
806 			    usbd_errstr(err));
807 		kmem_free(sc->sc_intr_buf, sc->sc_isize);
808 		sc->sc_intr_pipe = NULL;
809 	}
810 }
811 
812 Static void
813 uvscom_intr(struct usbd_xfer *xfer, void *priv,
814     usbd_status status)
815 {
816 	struct uvscom_softc *sc = priv;
817 	u_char *buf = sc->sc_intr_buf;
818 	u_char pstatus;
819 
820 	if (sc->sc_dying)
821 		return;
822 
823 	if (status != USBD_NORMAL_COMPLETION) {
824 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
825 			return;
826 
827 		aprint_error_dev(sc->sc_dev,
828 		    "uvscom_intr: abnormal status: %s\n",
829 		    usbd_errstr(status));
830 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
831 		return;
832 	}
833 
834 	DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
835 		 device_xname(sc->sc_dev), buf[0], buf[1]));
836 
837 	sc->sc_lsr = sc->sc_msr = 0;
838 	sc->sc_usr = buf[1];
839 
840 	pstatus = buf[0];
841 	if (ISSET(pstatus, UVSCOM_TXRDY))
842 		SET(sc->sc_lsr, ULSR_TXRDY);
843 	if (ISSET(pstatus, UVSCOM_RXRDY))
844 		SET(sc->sc_lsr, ULSR_RXRDY);
845 
846 	pstatus = buf[1];
847 	if (ISSET(pstatus, UVSCOM_CTS))
848 		SET(sc->sc_msr, UMSR_CTS);
849 	if (ISSET(pstatus, UVSCOM_DSR))
850 		SET(sc->sc_msr, UMSR_DSR);
851 	if (ISSET(pstatus, UVSCOM_DCD))
852 		SET(sc->sc_msr, UMSR_DCD);
853 
854 	ucom_status_change(device_private(sc->sc_subdev));
855 }
856 
857 Static void
858 uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
859 {
860 	struct uvscom_softc *sc = addr;
861 
862 	if (lsr != NULL)
863 		*lsr = sc->sc_lsr;
864 	if (msr != NULL)
865 		*msr = sc->sc_msr;
866 }
867 
868