xref: /netbsd-src/sys/dev/usb/ucom.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: ucom.c,v 1.24 2000/06/01 14:28:59 augustss Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 /*
40  * This code is very heavily based on the 16550 driver, com.c.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/ioctl.h>
47 #include <sys/conf.h>
48 #include <sys/tty.h>
49 #include <sys/file.h>
50 #include <sys/select.h>
51 #include <sys/proc.h>
52 #include <sys/vnode.h>
53 #include <sys/device.h>
54 #include <sys/poll.h>
55 
56 #include <dev/usb/usb.h>
57 
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbdevs.h>
61 #include <dev/usb/usb_quirks.h>
62 
63 #include <dev/usb/ucomvar.h>
64 
65 #include "ucom.h"
66 
67 #if NUCOM > 0
68 
69 #ifdef UCOM_DEBUG
70 #define DPRINTFN(n, x)	if (ucomdebug > (n)) logprintf x
71 int ucomdebug = 0;
72 #else
73 #define DPRINTFN(n, x)
74 #endif
75 #define DPRINTF(x) DPRINTFN(0, x)
76 
77 #define	UCOMUNIT_MASK		0x3ffff
78 #define	UCOMDIALOUT_MASK	0x80000
79 #define	UCOMCALLUNIT_MASK	0x40000
80 
81 #define	UCOMUNIT(x)		(minor(x) & UCOMUNIT_MASK)
82 #define	UCOMDIALOUT(x)		(minor(x) & UCOMDIALOUT_MASK)
83 #define	UCOMCALLUNIT(x)		(minor(x) & UCOMCALLUNIT_MASK)
84 
85 struct ucom_softc {
86 	USBBASEDEVICE		sc_dev;		/* base device */
87 
88 	usbd_device_handle	sc_udev;	/* USB device */
89 
90 	usbd_interface_handle	sc_iface;	/* data interface */
91 
92 	int			sc_bulkin_no;	/* bulk in endpoint address */
93 	usbd_pipe_handle	sc_bulkin_pipe;	/* bulk in pipe */
94 	usbd_xfer_handle	sc_ixfer;	/* read request */
95 	u_char			*sc_ibuf;	/* read buffer */
96 	u_int			sc_ibufsize;	/* read buffer size */
97 	u_int			sc_ibufsizepad;	/* read buffer size padded */
98 
99 	int			sc_bulkout_no;	/* bulk out endpoint address */
100 	usbd_pipe_handle	sc_bulkout_pipe;/* bulk out pipe */
101 	usbd_xfer_handle	sc_oxfer;	/* write request */
102 	u_char			*sc_obuf;	/* write buffer */
103 	u_int			sc_obufsize;	/* write buffer size */
104 	u_int			sc_obufsizepad;	/* write buffer size padded */
105 
106 	struct ucom_methods     *sc_methods;
107 	void                    *sc_parent;
108 	int			sc_portno;
109 
110 	struct tty		*sc_tty;	/* our tty */
111 	u_char			sc_lsr;
112 	u_char			sc_msr;
113 	u_char			sc_mcr;
114 	u_char			sc_tx_stopped;
115 	int			sc_swflags;
116 
117 	u_char			sc_opening;	/* lock during open */
118 	int			sc_refcnt;
119 	u_char			sc_dying;	/* disconnecting */
120 };
121 
122 cdev_decl(ucom);
123 
124 Static void	ucom_cleanup(struct ucom_softc *);
125 Static void	ucom_hwiflow(struct ucom_softc *);
126 Static int	ucomparam(struct tty *, struct termios *);
127 Static void	ucomstart(struct tty *);
128 Static void	ucom_shutdown(struct ucom_softc *);
129 Static int	ucom_do_ioctl(struct ucom_softc *, u_long, caddr_t,
130 			      int, struct proc *);
131 Static void	ucom_dtr(struct ucom_softc *, int);
132 Static void	ucom_rts(struct ucom_softc *, int);
133 Static void	ucom_break(struct ucom_softc *, int);
134 Static usbd_status ucomstartread(struct ucom_softc *);
135 Static void	ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
136 Static void	ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
137 Static void	tiocm_to_ucom(struct ucom_softc *, int, int);
138 Static int	ucom_to_tiocm(struct ucom_softc *);
139 
140 USB_DECLARE_DRIVER(ucom);
141 
142 USB_MATCH(ucom)
143 {
144 	return (1);
145 }
146 
147 USB_ATTACH(ucom)
148 {
149 	struct ucom_softc *sc = (struct ucom_softc *)self;
150 	struct ucom_attach_args *uca = aux;
151 	struct tty *tp;
152 
153 	if (uca->portno != UCOM_UNK_PORTNO)
154 		printf(": portno %d", uca->portno);
155 	printf("\n");
156 
157 	sc->sc_udev = uca->device;
158 	sc->sc_iface = uca->iface;
159 	sc->sc_bulkout_no = uca->bulkout;
160 	sc->sc_bulkin_no = uca->bulkin;
161 	sc->sc_ibufsize = uca->ibufsize;
162 	sc->sc_ibufsizepad = uca->ibufsizepad;
163 	sc->sc_obufsize = uca->obufsize;
164 	sc->sc_obufsizepad = uca->obufsizepad;
165 	sc->sc_methods = uca->methods;
166 	sc->sc_parent = uca->arg;
167 	sc->sc_portno = uca->portno;
168 
169 	tp = ttymalloc();
170 	tp->t_oproc = ucomstart;
171 	tp->t_param = ucomparam;
172 	sc->sc_tty = tp;
173 
174 	DPRINTF(("ucom_attach: tty_attach %p\n", tp));
175 	tty_attach(tp);
176 
177 	USB_ATTACH_SUCCESS_RETURN;
178 }
179 
180 USB_DETACH(ucom)
181 {
182 	struct ucom_softc *sc = (struct ucom_softc *)self;
183 	int maj, mn;
184 	int s;
185 
186 	DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p\n",
187 		 sc, flags, sc->sc_tty));
188 
189 	sc->sc_dying = 1;
190 
191 #ifdef DIAGNOSTIC
192 	if (sc->sc_tty == NULL) {
193 		DPRINTF(("ucom_detach: no tty\n"));
194 		return (0);
195 	}
196 #endif
197 
198 	s = splusb();
199 	if (--sc->sc_refcnt >= 0) {
200 		/* Wake everyone.. how? */
201 		/* Wait for processes to go away. */
202 		usb_detach_wait(USBDEV(sc->sc_dev));
203 	}
204 	splx(s);
205 
206 	/* locate the major number */
207 	for (maj = 0; maj < nchrdev; maj++)
208 		if (cdevsw[maj].d_open == ucomopen)
209 			break;
210 
211 	/* Nuke the vnodes for any open instances. */
212 	mn = self->dv_unit;
213 	DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
214 	vdevgone(maj, mn, mn, VCHR);
215 	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
216 	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
217 
218 	/* Detach and free the tty. */
219 	tty_detach(sc->sc_tty);
220 	ttyfree(sc->sc_tty);
221 	sc->sc_tty = 0;
222 
223 	return (0);
224 }
225 
226 #if defined(__NetBSD__) || defined(__OpenBSD__)
227 int
228 ucom_activate(device_ptr_t self, enum devact act)
229 {
230 	struct ucom_softc *sc = (struct ucom_softc *)self;
231 
232 	switch (act) {
233 	case DVACT_ACTIVATE:
234 		return (EOPNOTSUPP);
235 		break;
236 
237 	case DVACT_DEACTIVATE:
238 		sc->sc_dying = 1;
239 		break;
240 	}
241 	return (0);
242 }
243 #endif
244 
245 void
246 ucom_shutdown(struct ucom_softc *sc)
247 {
248 	struct tty *tp = sc->sc_tty;
249 
250 	DPRINTF(("ucom_shutdown\n"));
251 	/*
252 	 * Hang up if necessary.  Wait a bit, so the other side has time to
253 	 * notice even if we immediately open the port again.
254 	 */
255 	if (ISSET(tp->t_cflag, HUPCL)) {
256 		ucom_dtr(sc, 0);
257 		(void)tsleep(sc, TTIPRI, ttclos, hz);
258 	}
259 }
260 
261 int
262 ucomopen(dev_t dev, int flag, int mode, struct proc *p)
263 {
264 	int unit = UCOMUNIT(dev);
265 	usbd_status err;
266 	struct ucom_softc *sc;
267 	struct tty *tp;
268 	int s;
269 	int error;
270 
271 	if (unit >= ucom_cd.cd_ndevs)
272 		return (ENXIO);
273 	sc = ucom_cd.cd_devs[unit];
274 	if (sc == NULL)
275 		return (ENXIO);
276 
277 	if (sc->sc_dying)
278 		return (EIO);
279 
280 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
281 		return (ENXIO);
282 
283 	tp = sc->sc_tty;
284 
285 	DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
286 
287 	if (ISSET(tp->t_state, TS_ISOPEN) &&
288 	    ISSET(tp->t_state, TS_XCLUDE) &&
289 	    p->p_ucred->cr_uid != 0)
290 		return (EBUSY);
291 
292 	s = spltty();
293 
294 	/*
295 	 * Do the following iff this is a first open.
296 	 */
297 	while (sc->sc_opening)
298 		tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
299 
300 	if (sc->sc_dying) {
301 		splx(s);
302 		return (EIO);
303 	}
304 	sc->sc_opening = 1;
305 
306 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
307 		struct termios t;
308 
309 		tp->t_dev = dev;
310 
311 		if (sc->sc_methods->ucom_open != NULL) {
312 			error = sc->sc_methods->ucom_open(sc->sc_parent,
313 							  sc->sc_portno);
314 			if (error) {
315 				ucom_cleanup(sc);
316 				return (error);
317 			}
318 		}
319 
320 		ucom_status_change(sc);
321 
322 		/*
323 		 * Initialize the termios status to the defaults.  Add in the
324 		 * sticky bits from TIOCSFLAGS.
325 		 */
326 		t.c_ispeed = 0;
327 		t.c_ospeed = TTYDEF_SPEED;
328 		t.c_cflag = TTYDEF_CFLAG;
329 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
330 			SET(t.c_cflag, CLOCAL);
331 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
332 			SET(t.c_cflag, CRTSCTS);
333 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
334 			SET(t.c_cflag, MDMBUF);
335 		/* Make sure ucomparam() will do something. */
336 		tp->t_ospeed = 0;
337 		(void) ucomparam(tp, &t);
338 		tp->t_iflag = TTYDEF_IFLAG;
339 		tp->t_oflag = TTYDEF_OFLAG;
340 		tp->t_lflag = TTYDEF_LFLAG;
341 		ttychars(tp);
342 		ttsetwater(tp);
343 
344 		/*
345 		 * Turn on DTR.  We must always do this, even if carrier is not
346 		 * present, because otherwise we'd have to use TIOCSDTR
347 		 * immediately after setting CLOCAL, which applications do not
348 		 * expect.  We always assert DTR while the device is open
349 		 * unless explicitly requested to deassert it.
350 		 */
351 		ucom_dtr(sc, 1);
352 
353 		// XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
354 		ucom_hwiflow(sc);
355 
356 		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
357 			 sc->sc_bulkin_no, sc->sc_bulkout_no));
358 
359 		/* Open the bulk pipes */
360 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
361 				     &sc->sc_bulkin_pipe);
362 		if (err) {
363 			DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
364 				 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
365 				 usbd_errstr(err)));
366 			return (EIO);
367 		}
368 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
369 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
370 		if (err) {
371 			DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
372 				 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
373 				 usbd_errstr(err)));
374 			usbd_close_pipe(sc->sc_bulkin_pipe);
375 			return (EIO);
376 		}
377 
378 		/* Allocate a request and an input buffer and start reading. */
379 		sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
380 		if (sc->sc_ixfer == NULL) {
381 			usbd_close_pipe(sc->sc_bulkin_pipe);
382 			usbd_close_pipe(sc->sc_bulkout_pipe);
383 			return (ENOMEM);
384 		}
385 		sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
386 						sc->sc_ibufsizepad);
387 		if (sc->sc_ibuf == NULL) {
388 			usbd_free_xfer(sc->sc_ixfer);
389 			usbd_close_pipe(sc->sc_bulkin_pipe);
390 			usbd_close_pipe(sc->sc_bulkout_pipe);
391 			return (ENOMEM);
392 		}
393 
394 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
395 		if (sc->sc_oxfer == NULL) {
396 			usbd_free_xfer(sc->sc_ixfer);
397 			usbd_close_pipe(sc->sc_bulkin_pipe);
398 			usbd_close_pipe(sc->sc_bulkout_pipe);
399 			return (ENOMEM);
400 		}
401 		sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
402 						sc->sc_obufsizepad);
403 		if (sc->sc_obuf == NULL) {
404 			usbd_free_xfer(sc->sc_oxfer);
405 			usbd_free_xfer(sc->sc_ixfer);
406 			usbd_close_pipe(sc->sc_bulkin_pipe);
407 			usbd_close_pipe(sc->sc_bulkout_pipe);
408 			return (ENOMEM);
409 		}
410 
411 		ucomstartread(sc);
412 	}
413 	sc->sc_opening = 0;
414 	wakeup(&sc->sc_opening);
415 	splx(s);
416 
417 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
418 	if (error)
419 		goto bad;
420 
421 	error = (*linesw[tp->t_line].l_open)(dev, tp);
422 	if (error)
423 		goto bad;
424 
425 	return (0);
426 
427 bad:
428 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
429 		/*
430 		 * We failed to open the device, and nobody else had it opened.
431 		 * Clean up the state as appropriate.
432 		 */
433 		ucom_cleanup(sc);
434 	}
435 
436 	return (error);
437 }
438 
439 int
440 ucomclose(dev_t dev, int flag, int mode, struct proc *p)
441 {
442 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
443 	struct tty *tp = sc->sc_tty;
444 
445 	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
446 	if (!ISSET(tp->t_state, TS_ISOPEN))
447 		return (0);
448 
449 	sc->sc_refcnt++;
450 
451 	(*linesw[tp->t_line].l_close)(tp, flag);
452 	ttyclose(tp);
453 
454 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
455 		/*
456 		 * Although we got a last close, the device may still be in
457 		 * use; e.g. if this was the dialout node, and there are still
458 		 * processes waiting for carrier on the non-dialout node.
459 		 */
460 		ucom_cleanup(sc);
461 	}
462 
463 	if (sc->sc_methods->ucom_close != NULL)
464 		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
465 
466 	if (--sc->sc_refcnt < 0)
467 		usb_detach_wakeup(USBDEV(sc->sc_dev));
468 
469 	return (0);
470 }
471 
472 int
473 ucomread(dev_t dev, struct uio *uio, int flag)
474 {
475 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
476 	struct tty *tp = sc->sc_tty;
477 	int error;
478 
479 	if (sc->sc_dying)
480 		return (EIO);
481 
482 	sc->sc_refcnt++;
483 	error = ((*linesw[tp->t_line].l_read)(tp, uio, flag));
484 	if (--sc->sc_refcnt < 0)
485 		usb_detach_wakeup(USBDEV(sc->sc_dev));
486 	return (error);
487 }
488 
489 int
490 ucomwrite(dev_t dev, struct uio *uio, int flag)
491 {
492 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
493 	struct tty *tp = sc->sc_tty;
494 	int error;
495 
496 	if (sc->sc_dying)
497 		return (EIO);
498 
499 	sc->sc_refcnt++;
500 	error = ((*linesw[tp->t_line].l_write)(tp, uio, flag));
501 	if (--sc->sc_refcnt < 0)
502 		usb_detach_wakeup(USBDEV(sc->sc_dev));
503 	return (error);
504 }
505 
506 struct tty *
507 ucomtty(dev_t dev)
508 {
509 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
510 	struct tty *tp = sc->sc_tty;
511 
512 	return (tp);
513 }
514 
515 int
516 ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
517 {
518 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
519 	int error;
520 
521 	sc->sc_refcnt++;
522 	error = ucom_do_ioctl(sc, cmd, data, flag, p);
523 	if (--sc->sc_refcnt < 0)
524 		usb_detach_wakeup(USBDEV(sc->sc_dev));
525 	return (error);
526 }
527 
528 Static int
529 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, caddr_t data,
530 	      int flag, struct proc *p)
531 {
532 	struct tty *tp = sc->sc_tty;
533 	int error;
534 	int s;
535 
536 	if (sc->sc_dying)
537 		return (EIO);
538 
539 	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
540 
541 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
542 	if (error >= 0)
543 		return (error);
544 
545 	error = ttioctl(tp, cmd, data, flag, p);
546 	if (error >= 0)
547 		return (error);
548 
549 	if (sc->sc_methods->ucom_ioctl != NULL) {
550 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
551 			    sc->sc_portno, cmd, data, flag, p);
552 		if (error >= 0)
553 			return (error);
554 	}
555 
556 	error = 0;
557 
558 	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
559 	s = spltty();
560 
561 	switch (cmd) {
562 	case TIOCSBRK:
563 		ucom_break(sc, 1);
564 		break;
565 
566 	case TIOCCBRK:
567 		ucom_break(sc, 0);
568 		break;
569 
570 	case TIOCSDTR:
571 		ucom_dtr(sc, 1);
572 		break;
573 
574 	case TIOCCDTR:
575 		ucom_dtr(sc, 0);
576 		break;
577 
578 	case TIOCGFLAGS:
579 		*(int *)data = sc->sc_swflags;
580 		break;
581 
582 	case TIOCSFLAGS:
583 		error = suser(p->p_ucred, &p->p_acflag);
584 		if (error)
585 			break;
586 		sc->sc_swflags = *(int *)data;
587 		break;
588 
589 	case TIOCMSET:
590 	case TIOCMBIS:
591 	case TIOCMBIC:
592 		tiocm_to_ucom(sc, cmd, *(int *)data);
593 		break;
594 
595 	case TIOCMGET:
596 		*(int *)data = ucom_to_tiocm(sc);
597 		break;
598 
599 	default:
600 		error = ENOTTY;
601 		break;
602 	}
603 
604 	splx(s);
605 
606 	return (error);
607 }
608 
609 Static void
610 tiocm_to_ucom(struct ucom_softc *sc, int how, int ttybits)
611 {
612 	u_char combits;
613 
614 	combits = 0;
615 	if (ISSET(ttybits, TIOCM_DTR))
616 		SET(combits, UMCR_DTR);
617 	if (ISSET(ttybits, TIOCM_RTS))
618 		SET(combits, UMCR_RTS);
619 
620 	switch (how) {
621 	case TIOCMBIC:
622 		CLR(sc->sc_mcr, combits);
623 		break;
624 
625 	case TIOCMBIS:
626 		SET(sc->sc_mcr, combits);
627 		break;
628 
629 	case TIOCMSET:
630 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
631 		SET(sc->sc_mcr, combits);
632 		break;
633 	}
634 
635 	ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
636 	ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
637 }
638 
639 Static int
640 ucom_to_tiocm(struct ucom_softc *sc)
641 {
642 	u_char combits;
643 	int ttybits = 0;
644 
645 	combits = sc->sc_mcr;
646 	if (ISSET(combits, UMCR_DTR))
647 		SET(ttybits, TIOCM_DTR);
648 	if (ISSET(combits, UMCR_RTS))
649 		SET(ttybits, TIOCM_RTS);
650 
651 	combits = sc->sc_msr;
652 	if (ISSET(combits, UMSR_DCD))
653 		SET(ttybits, TIOCM_CD);
654 	if (ISSET(combits, UMSR_CTS))
655 		SET(ttybits, TIOCM_CTS);
656 	if (ISSET(combits, UMSR_DSR))
657 		SET(ttybits, TIOCM_DSR);
658 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
659 		SET(ttybits, TIOCM_RI);
660 
661 #if 0
662 XXX;
663 	if (sc->sc_ier != 0)
664 		SET(ttybits, TIOCM_LE);
665 #endif
666 
667 	return (ttybits);
668 }
669 
670 Static void
671 ucom_break(sc, onoff)
672 	struct ucom_softc *sc;
673 	int onoff;
674 {
675 	DPRINTF(("ucom_break: onoff=%d\n", onoff));
676 
677 	if (sc->sc_methods->ucom_set != NULL)
678 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
679 		    UCOM_SET_BREAK, onoff);
680 }
681 
682 Static void
683 ucom_dtr(struct ucom_softc *sc, int onoff)
684 {
685 	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
686 
687 	if (sc->sc_methods->ucom_set != NULL)
688 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
689 		    UCOM_SET_DTR, onoff);
690 }
691 
692 Static void
693 ucom_rts(struct ucom_softc *sc, int onoff)
694 {
695 	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
696 
697 	if (sc->sc_methods->ucom_set != NULL)
698 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
699 		    UCOM_SET_RTS, onoff);
700 }
701 
702 void
703 ucom_status_change(struct ucom_softc *sc)
704 {
705 	if (sc->sc_methods->ucom_get_status != NULL) {
706 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
707 		    &sc->sc_lsr, &sc->sc_msr);
708 	} else {
709 		sc->sc_lsr = 0;
710 		sc->sc_msr = 0;
711 	}
712 }
713 
714 Static int
715 ucomparam(struct tty *tp, struct termios *t)
716 {
717 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
718 	int error;
719 
720 	if (sc->sc_dying)
721 		return (EIO);
722 
723 	/* Check requested parameters. */
724 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
725 		return (EINVAL);
726 
727 	/*
728 	 * For the console, always force CLOCAL and !HUPCL, so that the port
729 	 * is always active.
730 	 */
731 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
732 		SET(t->c_cflag, CLOCAL);
733 		CLR(t->c_cflag, HUPCL);
734 	}
735 
736 	/*
737 	 * If there were no changes, don't do anything.  This avoids dropping
738 	 * input and improves performance when all we did was frob things like
739 	 * VMIN and VTIME.
740 	 */
741 	if (tp->t_ospeed == t->c_ospeed &&
742 	    tp->t_cflag == t->c_cflag)
743 		return (0);
744 
745 	//XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
746 
747 	/* And copy to tty. */
748 	tp->t_ispeed = 0;
749 	tp->t_ospeed = t->c_ospeed;
750 	tp->t_cflag = t->c_cflag;
751 
752 	if (sc->sc_methods->ucom_param != NULL) {
753 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
754 			    t);
755 		if (error)
756 			return (error);
757 	}
758 
759 	// XXX worry about CHWFLOW
760 
761 	/*
762 	 * Update the tty layer's idea of the carrier bit, in case we changed
763 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
764 	 * explicit request.
765 	 */
766 	DPRINTF(("ucomparam: l_modem\n"));
767 	(void) (*linesw[tp->t_line].l_modem)(tp, 1 /* XXX carrier */ );
768 
769 #if 0
770 XXX what if the hardware is not open
771 	if (!ISSET(t->c_cflag, CHWFLOW)) {
772 		if (sc->sc_tx_stopped) {
773 			sc->sc_tx_stopped = 0;
774 			ucomstart(tp);
775 		}
776 	}
777 #endif
778 
779 	return (0);
780 }
781 
782 /*
783  * (un)block input via hw flowcontrol
784  */
785 Static void
786 ucom_hwiflow(struct ucom_softc *sc)
787 {
788 	DPRINTF(("ucom_hwiflow:\n"));
789 #if 0
790 XXX
791 	bus_space_tag_t iot = sc->sc_iot;
792 	bus_space_handle_t ioh = sc->sc_ioh;
793 
794 	if (sc->sc_mcr_rts == 0)
795 		return;
796 
797 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
798 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
799 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
800 	} else {
801 		SET(sc->sc_mcr, sc->sc_mcr_rts);
802 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
803 	}
804 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
805 #endif
806 }
807 
808 Static void
809 ucomstart(struct tty *tp)
810 {
811 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
812 	usbd_status err;
813 	int s;
814 	u_char *data;
815 	int cnt;
816 
817 	if (sc->sc_dying)
818 		return;
819 
820 	s = spltty();
821 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
822 		DPRINTFN(4,("ucomstart: no go, state=0x%x\n", tp->t_state));
823 		goto out;
824 	}
825 	if (sc->sc_tx_stopped)
826 		goto out;
827 
828 	if (tp->t_outq.c_cc <= tp->t_lowat) {
829 		if (ISSET(tp->t_state, TS_ASLEEP)) {
830 			CLR(tp->t_state, TS_ASLEEP);
831 			wakeup(&tp->t_outq);
832 		}
833 		selwakeup(&tp->t_wsel);
834 		if (tp->t_outq.c_cc == 0)
835 			goto out;
836 	}
837 
838 	/* Grab the first contiguous region of buffer space. */
839 	data = tp->t_outq.c_cf;
840 	cnt = ndqb(&tp->t_outq, 0);
841 
842 	if (cnt == 0) {
843 		DPRINTF(("ucomstart: cnt==0\n"));
844 		goto out;
845 	}
846 
847 	SET(tp->t_state, TS_BUSY);
848 
849 	if (cnt > sc->sc_obufsize) {
850 		DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
851 		cnt = sc->sc_obufsize;
852 	}
853 	if (sc->sc_methods->ucom_write != NULL)
854 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
855 					   sc->sc_obuf, data, &cnt);
856 	else
857 		memcpy(sc->sc_obuf, data, cnt);
858 
859 	DPRINTFN(4,("ucomstart: %d chars\n", cnt));
860 	usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
861 			(usbd_private_handle)sc, sc->sc_obuf, cnt,
862 			USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
863 	/* What can we do on error? */
864 	err = usbd_transfer(sc->sc_oxfer);
865 #ifdef DIAGNOSTIC
866 	if (err != USBD_IN_PROGRESS)
867 		printf("ucomstart: err=%s\n", usbd_errstr(err));
868 #endif
869 
870 out:
871 	splx(s);
872 }
873 
874 void
875 ucomstop(struct tty *tp, int flag)
876 {
877 	DPRINTF(("ucomstop: flag=%d\n", flag));
878 #if 0
879 	/*struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];*/
880 	int s;
881 
882 	s = spltty();
883 	if (ISSET(tp->t_state, TS_BUSY)) {
884 		DPRINTF(("ucomstop: XXX\n"));
885 		/* sc->sc_tx_stopped = 1; */
886 		if (!ISSET(tp->t_state, TS_TTSTOP))
887 			SET(tp->t_state, TS_FLUSH);
888 	}
889 	splx(s);
890 #endif
891 }
892 
893 Static void
894 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
895 {
896 	struct ucom_softc *sc = (struct ucom_softc *)p;
897 	struct tty *tp = sc->sc_tty;
898 	u_int32_t cc;
899 	int s;
900 
901 	DPRINTFN(5,("ucomwritecb: status=%d\n", status));
902 
903 	if (status == USBD_CANCELLED)
904 		return;
905 
906 	if (status) {
907 		DPRINTF(("ucomwritecb: status=%d\n", status));
908 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
909 		/* XXX we should restart after some delay. */
910 		return;
911 	}
912 
913 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
914 	DPRINTFN(5,("ucomwritecb: cc=%d\n", cc));
915 
916 	s = spltty();
917 	CLR(tp->t_state, TS_BUSY);
918 	if (ISSET(tp->t_state, TS_FLUSH))
919 		CLR(tp->t_state, TS_FLUSH);
920 	else
921 		ndflush(&tp->t_outq, cc);
922 	(*linesw[tp->t_line].l_start)(tp);
923 	splx(s);
924 }
925 
926 Static usbd_status
927 ucomstartread(struct ucom_softc *sc)
928 {
929 	usbd_status err;
930 
931 	DPRINTFN(5,("ucomstartread: start\n"));
932 	usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
933 			(usbd_private_handle)sc,
934 			sc->sc_ibuf, sc->sc_ibufsize,
935 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
936 			USBD_NO_TIMEOUT, ucomreadcb);
937 	err = usbd_transfer(sc->sc_ixfer);
938 	if (err != USBD_IN_PROGRESS) {
939 		DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err)));
940 		return (err);
941 	}
942 	return (USBD_NORMAL_COMPLETION);
943 }
944 
945 Static void
946 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
947 {
948 	struct ucom_softc *sc = (struct ucom_softc *)p;
949 	struct tty *tp = sc->sc_tty;
950 	int (*rint)(int c, struct tty *tp) = linesw[tp->t_line].l_rint;
951 	usbd_status err;
952 	u_int32_t cc;
953 	u_char *cp;
954 	int s;
955 
956 	if (status == USBD_CANCELLED)
957 		return;
958 
959 	if (status) {
960 		DPRINTF(("ucomreadcb: status=%d\n", status));
961 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
962 		/* XXX we should restart after some delay. */
963 		return;
964 	}
965 
966 	usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
967 	DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp));
968 	if (sc->sc_methods->ucom_read != NULL)
969 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
970 					  &cp, &cc);
971 
972 	s = spltty();
973 	/* Give characters to tty layer. */
974 	while (cc-- > 0) {
975 		DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp));
976 		if ((*rint)(*cp++, tp) == -1) {
977 			/* XXX what should we do? */
978 			printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
979 			       cc);
980 			break;
981 		}
982 	}
983 	splx(s);
984 
985 	err = ucomstartread(sc);
986 	if (err) {
987 		printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
988 		/* XXX what should we dow now? */
989 	}
990 }
991 
992 Static void
993 ucom_cleanup(struct ucom_softc *sc)
994 {
995 	DPRINTF(("ucom_cleanup: closing pipes\n"));
996 
997 	ucom_shutdown(sc);
998 	usbd_abort_pipe(sc->sc_bulkin_pipe);
999 	usbd_close_pipe(sc->sc_bulkin_pipe);
1000 	usbd_abort_pipe(sc->sc_bulkout_pipe);
1001 	usbd_close_pipe(sc->sc_bulkout_pipe);
1002 	usbd_free_xfer(sc->sc_ixfer);
1003 	usbd_free_xfer(sc->sc_oxfer);
1004 }
1005 
1006 #endif /* NUCOM > 0 */
1007 
1008 int
1009 ucomprint(void *aux, const char *pnp)
1010 {
1011 
1012 	if (pnp)
1013 		printf("ucom at %s\n", pnp);
1014 	return (UNCONF);
1015 }
1016 
1017 int
1018 ucomsubmatch(struct device *parent, struct cfdata *cf, void *aux)
1019 {
1020 	struct ucom_attach_args *uca = aux;
1021 
1022 	if (uca->portno != UCOM_UNK_PORTNO &&
1023 	    cf->ucomcf_portno != UCOM_UNK_PORTNO &&
1024 	    cf->ucomcf_portno != uca->portno)
1025 		return (0);
1026 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1027 }
1028