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