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