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