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