xref: /openbsd-src/sys/dev/usb/ucom.c (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1 /*	$OpenBSD: ucom.c,v 1.70 2020/03/08 19:24:15 claudio Exp $ */
2 /*	$NetBSD: ucom.c,v 1.49 2003/01/01 00:10:25 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*
34  * This code is very heavily based on the 16550 driver, com.c.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/rwlock.h>
41 #include <sys/ioctl.h>
42 #include <sys/conf.h>
43 #include <sys/tty.h>
44 #include <sys/fcntl.h>
45 #include <sys/selinfo.h>
46 #include <sys/vnode.h>
47 #include <sys/device.h>
48 #include <sys/poll.h>
49 
50 #include <dev/usb/usb.h>
51 
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
54 #include <dev/usb/uhidev.h>
55 #include <dev/usb/usbdevs.h>
56 
57 #include <dev/usb/ucomvar.h>
58 
59 #include "ucom.h"
60 
61 #if NUCOM > 0
62 
63 #ifdef UCOM_DEBUG
64 #define DPRINTFN(n, x)	do { if (ucomdebug > (n)) printf x; } while (0)
65 int ucomdebug = 0;
66 #else
67 #define DPRINTFN(n, x)
68 #endif
69 #define DPRINTF(x) DPRINTFN(0, x)
70 
71 #define	UCOMUNIT_MASK		0x7f
72 #define	UCOMCUA_MASK		0x80
73 
74 #define LINESW(tp, func)	(linesw[(tp)->t_line].func)
75 
76 #define	UCOMUNIT(x)		(minor(x) & UCOMUNIT_MASK)
77 #define	UCOMCUA(x)		(minor(x) & UCOMCUA_MASK)
78 
79 struct ucom_softc {
80 	struct device		sc_dev;		/* base device */
81 
82 	struct usbd_device	*sc_uparent;	/* USB device */
83 	struct uhidev_softc	*sc_uhidev;	/* hid device (if deeper) */
84 
85 	struct usbd_interface	*sc_iface;	/* data interface */
86 
87 	int			sc_bulkin_no;	/* bulk in endpoint address */
88 	struct usbd_pipe	*sc_bulkin_pipe;/* bulk in pipe */
89 	struct usbd_xfer	*sc_ixfer;	/* read request */
90 	u_char			*sc_ibuf;	/* read buffer */
91 	u_int			sc_ibufsize;	/* read buffer size */
92 	u_int			sc_ibufsizepad;	/* read buffer size padded */
93 
94 	int			sc_bulkout_no;	/* bulk out endpoint address */
95 	struct usbd_pipe	*sc_bulkout_pipe;/* bulk out pipe */
96 	struct usbd_xfer	*sc_oxfer;	/* write request */
97 	u_char			*sc_obuf;	/* write buffer */
98 	u_int			sc_obufsize;	/* write buffer size */
99 	u_int			sc_opkthdrlen;	/* header length of
100 						 * output packet */
101 
102 	struct usbd_pipe	*sc_ipipe;	/* hid interrupt input pipe */
103 	struct usbd_pipe	*sc_opipe;	/* hid interrupt pipe */
104 
105 	struct ucom_methods     *sc_methods;
106 	void                    *sc_parent;
107 	int			sc_portno;
108 
109 	struct tty		*sc_tty;	/* our tty */
110 	u_char			sc_lsr;
111 	u_char			sc_msr;
112 	u_char			sc_mcr;
113 	u_char			sc_tx_stopped;
114 	int			sc_swflags;
115 
116 	u_char			sc_cua;
117 
118 	struct rwlock		sc_lock;	/* lock during open */
119 	int			sc_open;
120 	int			sc_refcnt;
121 };
122 
123 void	ucom_cleanup(struct ucom_softc *);
124 void	ucom_hwiflow(struct ucom_softc *);
125 int	ucomparam(struct tty *, struct termios *);
126 void	ucomstart(struct tty *);
127 void	ucom_shutdown(struct ucom_softc *);
128 int	ucom_do_open(dev_t, int, int, struct proc *);
129 int	ucom_do_ioctl(struct ucom_softc *, u_long, caddr_t, int, struct proc *);
130 int	ucom_do_close(struct ucom_softc *, int, int , struct proc *);
131 void	ucom_dtr(struct ucom_softc *, int);
132 void	ucom_rts(struct ucom_softc *, int);
133 void	ucom_break(struct ucom_softc *, int);
134 usbd_status ucomstartread(struct ucom_softc *);
135 void	ucomreadcb(struct usbd_xfer *, void *, usbd_status);
136 void	ucomwritecb(struct usbd_xfer *, void *, usbd_status);
137 void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
138 int	ucom_to_tiocm(struct ucom_softc *);
139 void	ucom_lock(struct ucom_softc *);
140 void	ucom_unlock(struct ucom_softc *);
141 
142 int ucom_match(struct device *, void *, void *);
143 void ucom_attach(struct device *, struct device *, void *);
144 int ucom_detach(struct device *, int);
145 
146 struct cfdriver ucom_cd = {
147 	NULL, "ucom", DV_TTY
148 };
149 
150 const struct cfattach ucom_ca = {
151 	sizeof(struct ucom_softc),
152 	ucom_match,
153 	ucom_attach,
154 	ucom_detach,
155 };
156 
157 void
158 ucom_lock(struct ucom_softc *sc)
159 {
160 	rw_enter_write(&sc->sc_lock);
161 }
162 
163 void
164 ucom_unlock(struct ucom_softc *sc)
165 {
166 	rw_exit_write(&sc->sc_lock);
167 }
168 
169 int
170 ucom_match(struct device *parent, void *match, void *aux)
171 {
172 	return (1);
173 }
174 
175 void
176 ucom_attach(struct device *parent, struct device *self, void *aux)
177 {
178 	struct ucom_softc *sc = (struct ucom_softc *)self;
179 	struct ucom_attach_args *uca = aux;
180 	struct tty *tp;
181 
182 	if (uca->info != NULL)
183 		printf(", %s", uca->info);
184 	printf("\n");
185 
186 	sc->sc_uparent = uca->device;
187 	sc->sc_iface = uca->iface;
188 	sc->sc_bulkout_no = uca->bulkout;
189 	sc->sc_bulkin_no = uca->bulkin;
190 	sc->sc_uhidev = uca->uhidev;
191 	sc->sc_ibufsize = uca->ibufsize;
192 	sc->sc_ibufsizepad = uca->ibufsizepad;
193 	sc->sc_obufsize = uca->obufsize;
194 	sc->sc_opkthdrlen = uca->opkthdrlen;
195 	sc->sc_methods = uca->methods;
196 	sc->sc_parent = uca->arg;
197 	sc->sc_portno = uca->portno;
198 
199 	tp = ttymalloc(1000000);
200 	tp->t_oproc = ucomstart;
201 	tp->t_param = ucomparam;
202 	sc->sc_tty = tp;
203 	sc->sc_cua = 0;
204 
205 	rw_init(&sc->sc_lock, "ucomlk");
206 }
207 
208 int
209 ucom_detach(struct device *self, int flags)
210 {
211 	struct ucom_softc *sc = (struct ucom_softc *)self;
212 	struct tty *tp = sc->sc_tty;
213 	int maj, mn;
214 	int s;
215 
216 	DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p, pipe=%d,%d\n",
217 		 sc, flags, tp, sc->sc_bulkin_no, sc->sc_bulkout_no));
218 
219 	if (sc->sc_bulkin_pipe != NULL) {
220 		usbd_abort_pipe(sc->sc_bulkin_pipe);
221 		usbd_close_pipe(sc->sc_bulkin_pipe);
222 		sc->sc_bulkin_pipe = NULL;
223 	}
224 	if (sc->sc_bulkout_pipe != NULL) {
225 		usbd_abort_pipe(sc->sc_bulkout_pipe);
226 		usbd_close_pipe(sc->sc_bulkout_pipe);
227 		sc->sc_bulkout_pipe = NULL;
228 	}
229 	if (sc->sc_ixfer != NULL) {
230 		if (sc->sc_bulkin_no != -1) {
231 			usbd_free_buffer(sc->sc_ixfer);
232 			sc->sc_ibuf = NULL;
233 			usbd_free_xfer(sc->sc_ixfer);
234 		}
235 		sc->sc_ixfer = NULL;
236 	}
237 	if (sc->sc_oxfer != NULL) {
238 		usbd_free_buffer(sc->sc_oxfer);
239 		sc->sc_obuf = NULL;
240 		if (sc->sc_bulkin_no != -1)
241 			usbd_free_xfer(sc->sc_oxfer);
242 		sc->sc_oxfer = NULL;
243 	}
244 
245 	s = splusb();
246 	if (--sc->sc_refcnt >= 0) {
247 		/* Wake up anyone waiting */
248 		if (tp != NULL) {
249 			CLR(tp->t_state, TS_CARR_ON);
250 			CLR(tp->t_cflag, CLOCAL | MDMBUF);
251 			ttyflush(tp, FREAD|FWRITE);
252 		}
253 		usb_detach_wait(&sc->sc_dev);
254 	}
255 	splx(s);
256 
257 	/* locate the major number */
258 	for (maj = 0; maj < nchrdev; maj++)
259 		if (cdevsw[maj].d_open == ucomopen)
260 			break;
261 
262 	/* Nuke the vnodes for any open instances. */
263 	mn = self->dv_unit;
264 	DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
265 	vdevgone(maj, mn, mn, VCHR);
266 	vdevgone(maj, mn | UCOMCUA_MASK, mn | UCOMCUA_MASK, VCHR);
267 
268 	/* Detach and free the tty. */
269 	if (tp != NULL) {
270 		(*LINESW(tp, l_close))(tp, FNONBLOCK, curproc);
271 		s = spltty();
272 		CLR(tp->t_state, TS_BUSY | TS_FLUSH);
273 		ttyclose(tp);
274 		splx(s);
275 		ttyfree(tp);
276 		sc->sc_tty = NULL;
277 	}
278 
279 	return (0);
280 }
281 
282 void
283 ucom_shutdown(struct ucom_softc *sc)
284 {
285 	struct tty *tp = sc->sc_tty;
286 
287 	DPRINTF(("ucom_shutdown\n"));
288 	/*
289 	 * Hang up if necessary.  Wait a bit, so the other side has time to
290 	 * notice even if we immediately open the port again.
291 	 */
292 	if (ISSET(tp->t_cflag, HUPCL)) {
293 		ucom_dtr(sc, 0);
294 		ucom_rts(sc, 0);
295 		tsleep_nsec(sc, TTIPRI, ttclos, SEC_TO_NSEC(1));
296 	}
297 }
298 
299 int
300 ucomopen(dev_t dev, int flag, int mode, struct proc *p)
301 {
302 	int unit = UCOMUNIT(dev);
303 	struct ucom_softc *sc;
304 	int error;
305 
306 	if (unit >= ucom_cd.cd_ndevs)
307 		return (ENXIO);
308 	sc = ucom_cd.cd_devs[unit];
309 	if (sc == NULL)
310 		return (ENXIO);
311 
312 	if (usbd_is_dying(sc->sc_uparent))
313 		return (EIO);
314 
315 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
316 		return (ENXIO);
317 
318 	sc->sc_refcnt++;
319 	error = ucom_do_open(dev, flag, mode, p);
320 	if (--sc->sc_refcnt < 0)
321 		usb_detach_wakeup(&sc->sc_dev);
322 
323 	return (error);
324 }
325 
326 int
327 ucom_do_open(dev_t dev, int flag, int mode, struct proc *p)
328 {
329 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
330 	usbd_status err;
331 	struct tty *tp;
332 	struct termios t;
333 	int error, s;
334 
335 	/* open the pipes if this is the first open */
336 	ucom_lock(sc);
337 	s = splusb();
338 	if (sc->sc_open == 0) {
339 		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
340 		    sc->sc_bulkin_no, sc->sc_bulkout_no));
341 		DPRINTF(("ucomopen: hid %p pipes in=%p out=%p\n",
342 		    sc->sc_uhidev, sc->sc_ipipe, sc->sc_opipe));
343 
344 		if (sc->sc_bulkin_no != -1) {
345 
346 			/* Open the bulk pipes */
347 			err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
348 			    &sc->sc_bulkin_pipe);
349 			if (err) {
350 				DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
351 				    sc->sc_dev.dv_xname, sc->sc_bulkin_no,
352 				    usbd_errstr(err)));
353 				error = EIO;
354 				goto fail_0;
355 			}
356 			err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
357 			    USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
358 			if (err) {
359 				DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
360 				    sc->sc_dev.dv_xname, sc->sc_bulkout_no,
361 				    usbd_errstr(err)));
362 				error = EIO;
363 				goto fail_1;
364 			}
365 
366 			/* Allocate a request and an input buffer and start reading. */
367 			sc->sc_ixfer = usbd_alloc_xfer(sc->sc_uparent);
368 			if (sc->sc_ixfer == NULL) {
369 				error = ENOMEM;
370 				goto fail_2;
371 			}
372 
373 			sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
374 			    sc->sc_ibufsizepad);
375 			if (sc->sc_ibuf == NULL) {
376 				error = ENOMEM;
377 				goto fail_2;
378 			}
379 
380 			sc->sc_oxfer = usbd_alloc_xfer(sc->sc_uparent);
381 			if (sc->sc_oxfer == NULL) {
382 				error = ENOMEM;
383 				goto fail_3;
384 			}
385 		} else {
386 			/*
387 			 * input/output pipes and xfers already allocated
388 			 * as is the input buffer.
389 			 */
390 			sc->sc_ipipe = sc->sc_uhidev->sc_ipipe;
391 			sc->sc_ixfer = sc->sc_uhidev->sc_ixfer;
392 			sc->sc_opipe = sc->sc_uhidev->sc_opipe;
393 			sc->sc_oxfer = sc->sc_uhidev->sc_oxfer;
394 		}
395 
396 		sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
397 		    sc->sc_obufsize + sc->sc_opkthdrlen);
398 		if (sc->sc_obuf == NULL) {
399 			error = ENOMEM;
400 			goto fail_4;
401 		}
402 
403 		if (sc->sc_methods->ucom_open != NULL) {
404 			error = sc->sc_methods->ucom_open(sc->sc_parent,
405 			    sc->sc_portno);
406 			if (error) {
407 				ucom_cleanup(sc);
408 				splx(s);
409 				ucom_unlock(sc);
410 				return (error);
411 			}
412 		}
413 
414 		ucom_status_change(sc);
415 
416 		ucomstartread(sc);
417 		sc->sc_open = 1;
418 	}
419 	splx(s);
420 	s = spltty();
421 	ucom_unlock(sc);
422 	tp = sc->sc_tty;
423 	splx(s);
424 
425 	DPRINTF(("ucomopen: unit=%d, tp=%p\n", UCOMUNIT(dev), tp));
426 
427 	tp->t_dev = dev;
428 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
429 		SET(tp->t_state, TS_WOPEN);
430 		ttychars(tp);
431 
432 		/*
433 		 * Initialize the termios status to the defaults.  Add in the
434 		 * sticky bits from TIOCSFLAGS.
435 		 */
436 		t.c_ispeed = 0;
437 		t.c_ospeed = TTYDEF_SPEED;
438 		t.c_cflag = TTYDEF_CFLAG;
439 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
440 			SET(t.c_cflag, CLOCAL);
441 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
442 			SET(t.c_cflag, CRTSCTS);
443 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
444 			SET(t.c_cflag, MDMBUF);
445 
446 		/* Make sure ucomparam() will do something. */
447 		tp->t_ospeed = 0;
448 		(void) ucomparam(tp, &t);
449 		tp->t_iflag = TTYDEF_IFLAG;
450 		tp->t_oflag = TTYDEF_OFLAG;
451 		tp->t_lflag = TTYDEF_LFLAG;
452 
453 		s = spltty();
454 		ttsetwater(tp);
455 
456 		/*
457 		 * Turn on DTR.  We must always do this, even if carrier is not
458 		 * present, because otherwise we'd have to use TIOCSDTR
459 		 * immediately after setting CLOCAL, which applications do not
460 		 * expect.  We always assert DTR while the device is open
461 		 * unless explicitly requested to deassert it.
462 		 */
463 		ucom_dtr(sc, 1);
464 		/* When not using CRTSCTS, RTS follows DTR. */
465 		if (!ISSET(t.c_cflag, CRTSCTS))
466 			ucom_rts(sc, 1);
467 
468 		/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
469 		ucom_hwiflow(sc);
470 
471 		if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || UCOMCUA(dev) ||
472 		    ISSET(sc->sc_msr, UMSR_DCD) || ISSET(tp->t_cflag, MDMBUF))
473 			SET(tp->t_state, TS_CARR_ON);
474 		else
475 			CLR(tp->t_state, TS_CARR_ON);
476 	} else if (ISSET(tp->t_state, TS_XCLUDE) && suser(p) != 0)
477 		return (EBUSY);
478 	else
479 		s = spltty();
480 
481 	if (UCOMCUA(dev)) {
482 		if (ISSET(tp->t_state, TS_ISOPEN)) {
483 			/* Someone is already dialed in */
484 			splx(s);
485 			return (EBUSY);
486 		}
487 		sc->sc_cua = 1;
488 	} else {
489 		/* tty (not cua) device, wait for carrier */
490 		if (ISSET(flag, O_NONBLOCK)) {
491 			if (sc->sc_cua) {
492 				splx(s);
493 				return (EBUSY);
494 			}
495 		} else {
496 			while (sc->sc_cua || (!ISSET(tp->t_cflag, CLOCAL) &&
497 			    !ISSET(tp->t_state, TS_CARR_ON))) {
498 				SET(tp->t_state, TS_WOPEN);
499 				error = ttysleep(tp, &tp->t_rawq,
500 				    TTIPRI | PCATCH, ttopen);
501 
502 				if (usbd_is_dying(sc->sc_uparent)) {
503 					splx(s);
504 					return (EIO);
505 				}
506 
507 				/*
508 				 * If TS_WOPEN has been reset, that means the
509 				 * cua device has been closed.  We don't want
510 				 * to fail in that case, so just go around
511 				 * again.
512 				 */
513 				if (error && ISSET(tp->t_state, TS_WOPEN)) {
514 					CLR(tp->t_state, TS_WOPEN);
515 					splx(s);
516 					goto bad;
517 				}
518 			}
519 		}
520 	}
521 	splx(s);
522 
523 	error = (*LINESW(tp, l_open))(dev, tp, p);
524 	if (error)
525 		goto bad;
526 
527 	return (0);
528 
529 fail_4:
530 	if (sc->sc_bulkin_no != -1)
531 		usbd_free_xfer(sc->sc_oxfer);
532 	sc->sc_oxfer = NULL;
533 fail_3:
534 	usbd_free_xfer(sc->sc_ixfer);
535 	sc->sc_ixfer = NULL;
536 fail_2:
537 	usbd_close_pipe(sc->sc_bulkout_pipe);
538 	sc->sc_bulkout_pipe = NULL;
539 fail_1:
540 	usbd_close_pipe(sc->sc_bulkin_pipe);
541 	sc->sc_bulkin_pipe = NULL;
542 fail_0:
543 	splx(s);
544 	ucom_unlock(sc);
545 	return (error);
546 
547 bad:
548 	ucom_lock(sc);
549 	ucom_cleanup(sc);
550 	ucom_unlock(sc);
551 
552 	return (error);
553 }
554 
555 int
556 ucomclose(dev_t dev, int flag, int mode, struct proc *p)
557 {
558 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
559 	int error;
560 
561 	if (sc == NULL || usbd_is_dying(sc->sc_uparent))
562 		return (EIO);
563 
564 	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
565 
566 	sc->sc_refcnt++;
567 	error = ucom_do_close(sc, flag, mode, p);
568 	if (--sc->sc_refcnt < 0)
569 		usb_detach_wakeup(&sc->sc_dev);
570 
571 	return (error);
572 }
573 
574 int
575 ucom_do_close(struct ucom_softc *sc, int flag, int mode, struct proc *p)
576 {
577 	struct tty *tp = sc->sc_tty;
578 	int s;
579 
580 	if (!ISSET(tp->t_state, TS_ISOPEN))
581 		return (0);
582 
583 	ucom_lock(sc);
584 
585 	(*LINESW(tp, l_close))(tp, flag, p);
586 	s = spltty();
587 	CLR(tp->t_state, TS_BUSY | TS_FLUSH);
588 	sc->sc_cua = 0;
589 	ttyclose(tp);
590 	splx(s);
591 	ucom_cleanup(sc);
592 
593 	if (sc->sc_methods->ucom_close != NULL)
594 		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
595 
596 	ucom_unlock(sc);
597 
598 	return (0);
599 }
600 
601 int
602 ucomread(dev_t dev, struct uio *uio, int flag)
603 {
604 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
605 	struct tty *tp;
606 	int error;
607 
608 	if (sc == NULL || usbd_is_dying(sc->sc_uparent))
609 		return (EIO);
610 
611 	sc->sc_refcnt++;
612 	tp = sc->sc_tty;
613 	error = (*LINESW(tp, l_read))(tp, uio, flag);
614 	if (--sc->sc_refcnt < 0)
615 		usb_detach_wakeup(&sc->sc_dev);
616 	return (error);
617 }
618 
619 int
620 ucomwrite(dev_t dev, struct uio *uio, int flag)
621 {
622 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
623 	struct tty *tp;
624 	int error;
625 
626 	if (sc == NULL || usbd_is_dying(sc->sc_uparent))
627 		return (EIO);
628 
629 	sc->sc_refcnt++;
630 	tp = sc->sc_tty;
631 	error = (*LINESW(tp, l_write))(tp, uio, flag);
632 	if (--sc->sc_refcnt < 0)
633 		usb_detach_wakeup(&sc->sc_dev);
634 	return (error);
635 }
636 
637 struct tty *
638 ucomtty(dev_t dev)
639 {
640 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
641 
642 	/*
643 	 * Return a pointer to our tty even if the device is dying
644 	 * in order to properly close it in the detach routine.
645 	 */
646 	if (sc == NULL)
647 		return (NULL);
648 
649 	return (sc->sc_tty);
650 }
651 
652 int
653 ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
654 {
655 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
656 	int error;
657 
658 	if (sc == NULL || usbd_is_dying(sc->sc_uparent))
659 		return (EIO);
660 
661 	sc->sc_refcnt++;
662 	error = ucom_do_ioctl(sc, cmd, data, flag, p);
663 	if (--sc->sc_refcnt < 0)
664 		usb_detach_wakeup(&sc->sc_dev);
665 	return (error);
666 }
667 
668 int
669 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, caddr_t data,
670 	      int flag, struct proc *p)
671 {
672 	struct tty *tp = sc->sc_tty;
673 	int error;
674 	int s;
675 
676 	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
677 
678 	error = (*LINESW(tp, l_ioctl))(tp, cmd, data, flag, p);
679 	if (error >= 0)
680 		return (error);
681 
682 	error = ttioctl(tp, cmd, data, flag, p);
683 	if (error >= 0)
684 		return (error);
685 
686 	if (sc->sc_methods->ucom_ioctl != NULL) {
687 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
688 			    sc->sc_portno, cmd, data, flag, p);
689 		if (error != ENOTTY)
690 			return (error);
691 	}
692 
693 	error = 0;
694 
695 	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
696 	s = spltty();
697 
698 	switch (cmd) {
699 	case TIOCSBRK:
700 		ucom_break(sc, 1);
701 		break;
702 
703 	case TIOCCBRK:
704 		ucom_break(sc, 0);
705 		break;
706 
707 	case TIOCSDTR:
708 		ucom_dtr(sc, 1);
709 		break;
710 
711 	case TIOCCDTR:
712 		ucom_dtr(sc, 0);
713 		break;
714 
715 	case TIOCGFLAGS:
716 		*(int *)data = sc->sc_swflags;
717 		break;
718 
719 	case TIOCSFLAGS:
720 		error = suser(p);
721 		if (error)
722 			break;
723 		sc->sc_swflags = *(int *)data;
724 		break;
725 
726 	case TIOCMSET:
727 	case TIOCMBIS:
728 	case TIOCMBIC:
729 		tiocm_to_ucom(sc, cmd, *(int *)data);
730 		break;
731 
732 	case TIOCMGET:
733 		*(int *)data = ucom_to_tiocm(sc);
734 		break;
735 
736 	default:
737 		error = ENOTTY;
738 		break;
739 	}
740 
741 	splx(s);
742 
743 	return (error);
744 }
745 
746 void
747 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
748 {
749 	u_char combits;
750 
751 	combits = 0;
752 	if (ISSET(ttybits, TIOCM_DTR))
753 		SET(combits, UMCR_DTR);
754 	if (ISSET(ttybits, TIOCM_RTS))
755 		SET(combits, UMCR_RTS);
756 
757 	switch (how) {
758 	case TIOCMBIC:
759 		CLR(sc->sc_mcr, combits);
760 		break;
761 
762 	case TIOCMBIS:
763 		SET(sc->sc_mcr, combits);
764 		break;
765 
766 	case TIOCMSET:
767 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
768 		SET(sc->sc_mcr, combits);
769 		break;
770 	}
771 
772 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
773 		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
774 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
775 		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
776 }
777 
778 int
779 ucom_to_tiocm(struct ucom_softc *sc)
780 {
781 	u_char combits;
782 	int ttybits = 0;
783 
784 	combits = sc->sc_mcr;
785 	if (ISSET(combits, UMCR_DTR))
786 		SET(ttybits, TIOCM_DTR);
787 	if (ISSET(combits, UMCR_RTS))
788 		SET(ttybits, TIOCM_RTS);
789 
790 	combits = sc->sc_msr;
791 	if (ISSET(combits, UMSR_DCD))
792 		SET(ttybits, TIOCM_CD);
793 	if (ISSET(combits, UMSR_CTS))
794 		SET(ttybits, TIOCM_CTS);
795 	if (ISSET(combits, UMSR_DSR))
796 		SET(ttybits, TIOCM_DSR);
797 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
798 		SET(ttybits, TIOCM_RI);
799 
800 #if 0
801 XXX;
802 	if (sc->sc_ier != 0)
803 		SET(ttybits, TIOCM_LE);
804 #endif
805 
806 	return (ttybits);
807 }
808 
809 void
810 ucom_break(struct ucom_softc *sc, int onoff)
811 {
812 	DPRINTF(("ucom_break: onoff=%d\n", onoff));
813 
814 	if (sc->sc_methods->ucom_set != NULL)
815 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
816 		    UCOM_SET_BREAK, onoff);
817 }
818 
819 void
820 ucom_dtr(struct ucom_softc *sc, int onoff)
821 {
822 	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
823 
824 	if (sc->sc_methods->ucom_set != NULL)
825 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
826 		    UCOM_SET_DTR, onoff);
827 }
828 
829 void
830 ucom_rts(struct ucom_softc *sc, int onoff)
831 {
832 	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
833 
834 	if (sc->sc_methods->ucom_set != NULL)
835 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
836 		    UCOM_SET_RTS, onoff);
837 }
838 
839 void
840 ucom_status_change(struct ucom_softc *sc)
841 {
842 	struct tty *tp = sc->sc_tty;
843 	u_char old_msr;
844 
845 	if (sc->sc_methods->ucom_get_status != NULL) {
846 		old_msr = sc->sc_msr;
847 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
848 		    &sc->sc_lsr, &sc->sc_msr);
849 
850 		ttytstamp(tp, old_msr & UMSR_CTS, sc->sc_msr & UMSR_CTS,
851 		    old_msr & UMSR_DCD, sc->sc_msr & UMSR_DCD);
852 
853 		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD))
854 			(*LINESW(tp, l_modem))(tp,
855 			    ISSET(sc->sc_msr, UMSR_DCD));
856 	} else {
857 		sc->sc_lsr = 0;
858 		sc->sc_msr = 0;
859 	}
860 }
861 
862 int
863 ucomparam(struct tty *tp, struct termios *t)
864 {
865 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
866 	int error;
867 
868 	if (sc == NULL || usbd_is_dying(sc->sc_uparent))
869 		return (EIO);
870 
871 	/* Check requested parameters. */
872 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
873 		return (EINVAL);
874 
875 	/*
876 	 * For the console, always force CLOCAL and !HUPCL, so that the port
877 	 * is always active.
878 	 */
879 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
880 		SET(t->c_cflag, CLOCAL);
881 		CLR(t->c_cflag, HUPCL);
882 	}
883 
884 	/*
885 	 * If there were no changes, don't do anything.  This avoids dropping
886 	 * input and improves performance when all we did was frob things like
887 	 * VMIN and VTIME.
888 	 */
889 	if (tp->t_ospeed == t->c_ospeed &&
890 	    tp->t_cflag == t->c_cflag)
891 		return (0);
892 
893 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
894 
895 	/* And copy to tty. */
896 	tp->t_ispeed = 0;
897 	tp->t_ospeed = t->c_ospeed;
898 	tp->t_cflag = t->c_cflag;
899 
900 	/*
901 	 * When not using CRTSCTS, RTS follows DTR.
902 	 * This assumes that the ucom_param() call will enable these signals
903 	 * for real.
904 	 */
905 	if (!ISSET(t->c_cflag, CRTSCTS))
906 		sc->sc_mcr = UMCR_DTR | UMCR_RTS;
907 	else
908 		sc->sc_mcr = UMCR_DTR;
909 
910 	if (sc->sc_methods->ucom_param != NULL) {
911 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
912 			    t);
913 		if (error)
914 			return (error);
915 	}
916 
917 	/* XXX worry about CHWFLOW */
918 
919 	/*
920 	 * Update the tty layer's idea of the carrier bit, in case we changed
921 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
922 	 * explicit request.
923 	 */
924 	DPRINTF(("ucomparam: l_modem\n"));
925 	(void) (*LINESW(tp, l_modem))(tp, 1 /* XXX carrier */ );
926 
927 #if 0
928 XXX what if the hardware is not open
929 	if (!ISSET(t->c_cflag, CHWFLOW)) {
930 		if (sc->sc_tx_stopped) {
931 			sc->sc_tx_stopped = 0;
932 			ucomstart(tp);
933 		}
934 	}
935 #endif
936 
937 	return (0);
938 }
939 
940 /*
941  * (un)block input via hw flowcontrol
942  */
943 void
944 ucom_hwiflow(struct ucom_softc *sc)
945 {
946 	DPRINTF(("ucom_hwiflow:\n"));
947 #if 0
948 XXX
949 	bus_space_tag_t iot = sc->sc_iot;
950 	bus_space_handle_t ioh = sc->sc_ioh;
951 
952 	if (sc->sc_mcr_rts == 0)
953 		return;
954 
955 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
956 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
957 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
958 	} else {
959 		SET(sc->sc_mcr, sc->sc_mcr_rts);
960 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
961 	}
962 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
963 #endif
964 }
965 
966 void
967 ucomstart(struct tty *tp)
968 {
969 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
970 	usbd_status err;
971 	int s;
972 	u_char *data;
973 	int cnt;
974 
975 	if (sc == NULL || usbd_is_dying(sc->sc_uparent))
976 		return;
977 
978 	s = spltty();
979 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
980 		DPRINTFN(4,("ucomstart: no go, state=0x%x\n", tp->t_state));
981 		goto out;
982 	}
983 	if (sc->sc_tx_stopped)
984 		goto out;
985 
986 	ttwakeupwr(tp);
987 	if (tp->t_outq.c_cc == 0)
988 		goto out;
989 
990 	/* Grab the first contiguous region of buffer space. */
991 	data = tp->t_outq.c_cf;
992 	cnt = ndqb(&tp->t_outq, 0);
993 
994 	if (cnt == 0) {
995 		DPRINTF(("ucomstart: cnt==0\n"));
996 		goto out;
997 	}
998 
999 	SET(tp->t_state, TS_BUSY);
1000 
1001 	if (cnt > sc->sc_obufsize) {
1002 		DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
1003 		cnt = sc->sc_obufsize;
1004 	}
1005 	if (sc->sc_methods->ucom_write != NULL)
1006 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
1007 					   sc->sc_obuf, data, &cnt);
1008 	else
1009 		memcpy(sc->sc_obuf, data, cnt);
1010 
1011 	DPRINTFN(4,("ucomstart: %d chars\n", cnt));
1012 #ifdef DIAGNOSTIC
1013 	if (sc->sc_oxfer == NULL) {
1014 		printf("ucomstart: null oxfer\n");
1015 		goto out;
1016 	}
1017 #endif
1018 	if (sc->sc_bulkout_pipe != NULL) {
1019 		usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
1020 		    (void *)sc, sc->sc_obuf, cnt,
1021 		    USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
1022 	} else {
1023 		usbd_setup_xfer(sc->sc_oxfer, sc->sc_opipe,
1024 		    (void *)sc, sc->sc_obuf, cnt,
1025 		    USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
1026 	}
1027 	/* What can we do on error? */
1028 	err = usbd_transfer(sc->sc_oxfer);
1029 #ifdef DIAGNOSTIC
1030 	if (err != USBD_IN_PROGRESS)
1031 		printf("ucomstart: err=%s\n", usbd_errstr(err));
1032 #endif
1033 
1034 out:
1035 	splx(s);
1036 }
1037 
1038 int
1039 ucomstop(struct tty *tp, int flag)
1040 {
1041 	DPRINTF(("ucomstop: flag=%d\n", flag));
1042 #if 0
1043 	/*struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];*/
1044 	int s;
1045 
1046 	s = spltty();
1047 	if (ISSET(tp->t_state, TS_BUSY)) {
1048 		DPRINTF(("ucomstop: XXX\n"));
1049 		/* sc->sc_tx_stopped = 1; */
1050 		if (!ISSET(tp->t_state, TS_TTSTOP))
1051 			SET(tp->t_state, TS_FLUSH);
1052 	}
1053 	splx(s);
1054 #endif
1055 	return (0);
1056 }
1057 
1058 void
1059 ucomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
1060 {
1061 	struct ucom_softc *sc = (struct ucom_softc *)p;
1062 	struct tty *tp = sc->sc_tty;
1063 	u_int32_t cc;
1064 	int s;
1065 
1066 	DPRINTFN(5,("ucomwritecb: %p %p status=%d\n", xfer, p, status));
1067 
1068 	if (status == USBD_CANCELLED || usbd_is_dying(sc->sc_uparent))
1069 		goto error;
1070 
1071 	if (sc->sc_bulkin_pipe != NULL) {
1072 		if (status) {
1073 			usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1074 			/* XXX we should restart after some delay. */
1075 			goto error;
1076 		}
1077 		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
1078 	} else {
1079 		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
1080 		// XXX above gives me wrong cc, no?
1081 	}
1082 
1083 	DPRINTFN(5,("ucomwritecb: cc=%d\n", cc));
1084 	/* convert from USB bytes to tty bytes */
1085 	cc -= sc->sc_opkthdrlen;
1086 
1087 	s = spltty();
1088 	CLR(tp->t_state, TS_BUSY);
1089 	if (ISSET(tp->t_state, TS_FLUSH))
1090 		CLR(tp->t_state, TS_FLUSH);
1091 	else
1092 		ndflush(&tp->t_outq, cc);
1093 	(*LINESW(tp, l_start))(tp);
1094 	splx(s);
1095 	return;
1096 
1097 error:
1098 	s = spltty();
1099 	CLR(tp->t_state, TS_BUSY);
1100 	splx(s);
1101 }
1102 
1103 usbd_status
1104 ucomstartread(struct ucom_softc *sc)
1105 {
1106 	usbd_status err;
1107 
1108 	DPRINTFN(5,("ucomstartread: start\n"));
1109 #ifdef DIAGNOSTIC
1110 	if (sc->sc_ixfer == NULL) {
1111 		DPRINTF(("ucomstartread: null ixfer\n"));
1112 		return (USBD_INVAL);
1113 	}
1114 #endif
1115 
1116 	if (sc->sc_bulkin_pipe != NULL) {
1117 		usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1118 			(void *)sc,
1119 			sc->sc_ibuf, sc->sc_ibufsize,
1120 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
1121 			USBD_NO_TIMEOUT, ucomreadcb);
1122 		err = usbd_transfer(sc->sc_ixfer);
1123 		if (err != USBD_IN_PROGRESS) {
1124 			DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err)));
1125 			return (err);
1126 		}
1127 	}
1128 
1129 	return (USBD_NORMAL_COMPLETION);
1130 }
1131 
1132 void
1133 ucomreadcb(struct usbd_xfer *xfer, void *p, usbd_status status)
1134 {
1135 	struct ucom_softc *sc = (struct ucom_softc *)p;
1136 	struct tty *tp = sc->sc_tty;
1137 	int (*rint)(int c, struct tty *tp) = LINESW(tp, l_rint);
1138 	usbd_status err;
1139 	u_int32_t cc;
1140 	u_char *cp;
1141 	int s;
1142 
1143 	DPRINTFN(5,("ucomreadcb: status=%d\n", status));
1144 
1145 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
1146 	    usbd_is_dying(sc->sc_uparent)) {
1147 		DPRINTF(("ucomreadcb: dying\n"));
1148 		/* Send something to wake upper layer */
1149 		s = spltty();
1150 		(*rint)('\n', tp);
1151 		ttwakeup(tp);
1152 		splx(s);
1153 		return;
1154 	}
1155 
1156 	if (status) {
1157 		if (sc->sc_bulkin_pipe != NULL) {
1158 			usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1159 			/* XXX we should restart after some delay. */
1160 			return;
1161 		}
1162 	}
1163 
1164 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
1165 	DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp));
1166 	if (sc->sc_methods->ucom_read != NULL)
1167 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
1168 					  &cp, &cc);
1169 
1170 	s = spltty();
1171 	/* Give characters to tty layer. */
1172 	while (cc-- > 0) {
1173 		DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp));
1174 		if ((*rint)(*cp++, tp) == -1) {
1175 			/* XXX what should we do? */
1176 			printf("%s: lost %d chars\n", sc->sc_dev.dv_xname,
1177 			       cc);
1178 			break;
1179 		}
1180 	}
1181 	splx(s);
1182 
1183 	err = ucomstartread(sc);
1184 	if (err) {
1185 		printf("%s: read start failed\n", sc->sc_dev.dv_xname);
1186 		/* XXX what should we dow now? */
1187 	}
1188 }
1189 
1190 void
1191 ucom_cleanup(struct ucom_softc *sc)
1192 {
1193 	DPRINTF(("ucom_cleanup: closing pipes\n"));
1194 
1195 	sc->sc_open = 0;
1196 
1197 	ucom_shutdown(sc);
1198 	if (sc->sc_bulkin_pipe != NULL) {
1199 		usbd_abort_pipe(sc->sc_bulkin_pipe);
1200 		usbd_close_pipe(sc->sc_bulkin_pipe);
1201 		sc->sc_bulkin_pipe = NULL;
1202 	}
1203 	if (sc->sc_bulkout_pipe != NULL) {
1204 		usbd_abort_pipe(sc->sc_bulkout_pipe);
1205 		usbd_close_pipe(sc->sc_bulkout_pipe);
1206 		sc->sc_bulkout_pipe = NULL;
1207 	}
1208 	if (sc->sc_ixfer != NULL) {
1209 		if (sc->sc_bulkin_no != -1) {
1210 			usbd_free_buffer(sc->sc_ixfer);
1211 			sc->sc_ibuf = NULL;
1212 			usbd_free_xfer(sc->sc_ixfer);
1213 		}
1214 		sc->sc_ixfer = NULL;
1215 	}
1216 	if (sc->sc_oxfer != NULL) {
1217 		usbd_free_buffer(sc->sc_oxfer);
1218 		sc->sc_obuf = NULL;
1219 		if (sc->sc_bulkin_no != -1)
1220 			usbd_free_xfer(sc->sc_oxfer);
1221 		sc->sc_oxfer = NULL;
1222 	}
1223 }
1224 
1225 #endif /* NUCOM > 0 */
1226 
1227 int
1228 ucomprint(void *aux, const char *pnp)
1229 {
1230 	struct ucom_attach_args *uca = aux;
1231 
1232 	if (pnp)
1233 		printf("ucom at %s", pnp);
1234 	if (uca->portno != UCOM_UNK_PORTNO)
1235 		printf(" portno %d", uca->portno);
1236 	return (UNCONF);
1237 }
1238 
1239 int
1240 ucomsubmatch(struct device *parent, void *match, void *aux)
1241 {
1242         struct ucom_attach_args *uca = aux;
1243         struct cfdata *cf = match;
1244 
1245 	if (uca->portno != UCOM_UNK_PORTNO &&
1246 	    cf->ucomcf_portno != UCOM_UNK_PORTNO &&
1247 	    cf->ucomcf_portno != uca->portno)
1248 		return (0);
1249 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1250 }
1251