xref: /netbsd-src/sys/dev/usb/ucom.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: ucom.c,v 1.38 2001/05/02 10:32:11 scw 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 #if defined(__NetBSD__)
56 #include "rnd.h"
57 #if NRND > 0
58 #include <sys/rnd.h>
59 #endif
60 #endif
61 
62 #include <dev/usb/usb.h>
63 
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdevs.h>
67 #include <dev/usb/usb_quirks.h>
68 
69 #include <dev/usb/ucomvar.h>
70 
71 #include "ucom.h"
72 
73 #if NUCOM > 0
74 
75 #ifdef UCOM_DEBUG
76 #define DPRINTFN(n, x)	if (ucomdebug > (n)) logprintf x
77 int ucomdebug = 0;
78 #else
79 #define DPRINTFN(n, x)
80 #endif
81 #define DPRINTF(x) DPRINTFN(0, x)
82 
83 #define	UCOMUNIT_MASK		0x3ffff
84 #define	UCOMDIALOUT_MASK	0x80000
85 #define	UCOMCALLUNIT_MASK	0x40000
86 
87 #define	UCOMUNIT(x)		(minor(x) & UCOMUNIT_MASK)
88 #define	UCOMDIALOUT(x)		(minor(x) & UCOMDIALOUT_MASK)
89 #define	UCOMCALLUNIT(x)		(minor(x) & UCOMCALLUNIT_MASK)
90 
91 struct ucom_softc {
92 	USBBASEDEVICE		sc_dev;		/* base device */
93 
94 	usbd_device_handle	sc_udev;	/* USB device */
95 
96 	usbd_interface_handle	sc_iface;	/* data interface */
97 
98 	int			sc_bulkin_no;	/* bulk in endpoint address */
99 	usbd_pipe_handle	sc_bulkin_pipe;	/* bulk in pipe */
100 	usbd_xfer_handle	sc_ixfer;	/* read request */
101 	u_char			*sc_ibuf;	/* read buffer */
102 	u_int			sc_ibufsize;	/* read buffer size */
103 	u_int			sc_ibufsizepad;	/* read buffer size padded */
104 
105 	int			sc_bulkout_no;	/* bulk out endpoint address */
106 	usbd_pipe_handle	sc_bulkout_pipe;/* bulk out pipe */
107 	usbd_xfer_handle	sc_oxfer;	/* write request */
108 	u_char			*sc_obuf;	/* write buffer */
109 	u_int			sc_obufsize;	/* write buffer size */
110 	u_int			sc_opkthdrlen;	/* header length of
111 						 * output packet */
112 
113 	struct ucom_methods     *sc_methods;
114 	void                    *sc_parent;
115 	int			sc_portno;
116 
117 	struct tty		*sc_tty;	/* our tty */
118 	u_char			sc_lsr;
119 	u_char			sc_msr;
120 	u_char			sc_mcr;
121 	u_char			sc_tx_stopped;
122 	int			sc_swflags;
123 
124 	u_char			sc_opening;	/* lock during open */
125 	int			sc_refcnt;
126 	u_char			sc_dying;	/* disconnecting */
127 
128 #if defined(__NetBSD__) && NRND > 0
129 	rndsource_element_t	sc_rndsource;	/* random source */
130 #endif
131 };
132 
133 cdev_decl(ucom);
134 
135 Static void	ucom_cleanup(struct ucom_softc *);
136 Static void	ucom_hwiflow(struct ucom_softc *);
137 Static int	ucomparam(struct tty *, struct termios *);
138 Static void	ucomstart(struct tty *);
139 Static void	ucom_shutdown(struct ucom_softc *);
140 Static int	ucom_do_ioctl(struct ucom_softc *, u_long, caddr_t,
141 			      int, struct proc *);
142 Static void	ucom_dtr(struct ucom_softc *, int);
143 Static void	ucom_rts(struct ucom_softc *, int);
144 Static void	ucom_break(struct ucom_softc *, int);
145 Static usbd_status ucomstartread(struct ucom_softc *);
146 Static void	ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
147 Static void	ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
148 Static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
149 Static int	ucom_to_tiocm(struct ucom_softc *);
150 
151 USB_DECLARE_DRIVER(ucom);
152 
153 USB_MATCH(ucom)
154 {
155 	return (1);
156 }
157 
158 USB_ATTACH(ucom)
159 {
160 	struct ucom_softc *sc = (struct ucom_softc *)self;
161 	struct ucom_attach_args *uca = aux;
162 	struct tty *tp;
163 
164 	if (uca->portno != UCOM_UNK_PORTNO)
165 		printf(": portno %d", uca->portno);
166 	if (uca->info != NULL)
167 		printf(", %s", uca->info);
168 	printf("\n");
169 
170 	sc->sc_udev = uca->device;
171 	sc->sc_iface = uca->iface;
172 	sc->sc_bulkout_no = uca->bulkout;
173 	sc->sc_bulkin_no = uca->bulkin;
174 	sc->sc_ibufsize = uca->ibufsize;
175 	sc->sc_ibufsizepad = uca->ibufsizepad;
176 	sc->sc_obufsize = uca->obufsize;
177 	sc->sc_opkthdrlen = uca->opkthdrlen;
178 	sc->sc_methods = uca->methods;
179 	sc->sc_parent = uca->arg;
180 	sc->sc_portno = uca->portno;
181 
182 	tp = ttymalloc();
183 	tp->t_oproc = ucomstart;
184 	tp->t_param = ucomparam;
185 	sc->sc_tty = tp;
186 
187 	DPRINTF(("ucom_attach: tty_attach %p\n", tp));
188 	tty_attach(tp);
189 
190 #if defined(__NetBSD__) && NRND > 0
191 	rnd_attach_source(&sc->sc_rndsource, USBDEVNAME(sc->sc_dev),
192 			  RND_TYPE_TTY, 0);
193 #endif
194 
195 	USB_ATTACH_SUCCESS_RETURN;
196 }
197 
198 USB_DETACH(ucom)
199 {
200 	struct ucom_softc *sc = (struct ucom_softc *)self;
201 	struct tty *tp = sc->sc_tty;
202 	int maj, mn;
203 	int s;
204 
205 	DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p, pipe=%d,%d\n",
206 		 sc, flags, tp, sc->sc_bulkin_no, sc->sc_bulkout_no));
207 
208 	sc->sc_dying = 1;
209 
210 	if (sc->sc_bulkin_pipe != NULL)
211 		usbd_abort_pipe(sc->sc_bulkin_pipe);
212 	if (sc->sc_bulkout_pipe != NULL)
213 		usbd_abort_pipe(sc->sc_bulkout_pipe);
214 
215 	s = splusb();
216 	if (--sc->sc_refcnt >= 0) {
217 		/* Wake up anyone waiting */
218 		if (tp != NULL) {
219 			CLR(tp->t_state, TS_CARR_ON);
220 			CLR(tp->t_cflag, CLOCAL | MDMBUF);
221 			ttyflush(tp, FREAD|FWRITE);
222 		}
223 		/* Wait for processes to go away. */
224 		usb_detach_wait(USBDEV(sc->sc_dev));
225 	}
226 	splx(s);
227 
228 	/* locate the major number */
229 	for (maj = 0; maj < nchrdev; maj++)
230 		if (cdevsw[maj].d_open == ucomopen)
231 			break;
232 
233 	/* Nuke the vnodes for any open instances. */
234 	mn = self->dv_unit;
235 	DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
236 	vdevgone(maj, mn, mn, VCHR);
237 	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
238 	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
239 
240 	/* Detach and free the tty. */
241 	if (tp != NULL) {
242 		tty_detach(tp);
243 		ttyfree(tp);
244 		sc->sc_tty = NULL;
245 	}
246 
247 	/* Detach the random source */
248 #if defined(__NetBSD__) && NRND > 0
249 	rnd_detach_source(&sc->sc_rndsource);
250 #endif
251 
252 	return (0);
253 }
254 
255 int
256 ucom_activate(device_ptr_t self, enum devact act)
257 {
258 	struct ucom_softc *sc = (struct ucom_softc *)self;
259 
260 	DPRINTFN(5,("ucom_activate: %d\n", act));
261 
262 	switch (act) {
263 	case DVACT_ACTIVATE:
264 		return (EOPNOTSUPP);
265 		break;
266 
267 	case DVACT_DEACTIVATE:
268 		sc->sc_dying = 1;
269 		break;
270 	}
271 	return (0);
272 }
273 
274 void
275 ucom_shutdown(struct ucom_softc *sc)
276 {
277 	struct tty *tp = sc->sc_tty;
278 
279 	DPRINTF(("ucom_shutdown\n"));
280 	/*
281 	 * Hang up if necessary.  Wait a bit, so the other side has time to
282 	 * notice even if we immediately open the port again.
283 	 */
284 	if (ISSET(tp->t_cflag, HUPCL)) {
285 		ucom_dtr(sc, 0);
286 		(void)tsleep(sc, TTIPRI, ttclos, hz);
287 	}
288 }
289 
290 int
291 ucomopen(dev_t dev, int flag, int mode, struct proc *p)
292 {
293 	int unit = UCOMUNIT(dev);
294 	usbd_status err;
295 	struct ucom_softc *sc;
296 	struct tty *tp;
297 	int s;
298 	int error;
299 
300 	if (unit >= ucom_cd.cd_ndevs)
301 		return (ENXIO);
302 	sc = ucom_cd.cd_devs[unit];
303 	if (sc == NULL)
304 		return (ENXIO);
305 
306 	if (sc->sc_dying)
307 		return (EIO);
308 
309 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
310 		return (ENXIO);
311 
312 	tp = sc->sc_tty;
313 
314 	DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
315 
316 	if (ISSET(tp->t_state, TS_ISOPEN) &&
317 	    ISSET(tp->t_state, TS_XCLUDE) &&
318 	    p->p_ucred->cr_uid != 0)
319 		return (EBUSY);
320 
321 	s = spltty();
322 
323 	/*
324 	 * Do the following iff this is a first open.
325 	 */
326 	while (sc->sc_opening)
327 		tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
328 
329 	if (sc->sc_dying) {
330 		splx(s);
331 		return (EIO);
332 	}
333 	sc->sc_opening = 1;
334 
335 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
336 		struct termios t;
337 
338 		tp->t_dev = dev;
339 
340 		if (sc->sc_methods->ucom_open != NULL) {
341 			error = sc->sc_methods->ucom_open(sc->sc_parent,
342 							  sc->sc_portno);
343 			if (error) {
344 				ucom_cleanup(sc);
345 				sc->sc_opening = 0;
346 				wakeup(&sc->sc_opening);
347 				splx(s);
348 				return (error);
349 			}
350 		}
351 
352 		ucom_status_change(sc);
353 
354 		/*
355 		 * Initialize the termios status to the defaults.  Add in the
356 		 * sticky bits from TIOCSFLAGS.
357 		 */
358 		t.c_ispeed = 0;
359 		t.c_ospeed = TTYDEF_SPEED;
360 		t.c_cflag = TTYDEF_CFLAG;
361 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
362 			SET(t.c_cflag, CLOCAL);
363 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
364 			SET(t.c_cflag, CRTSCTS);
365 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
366 			SET(t.c_cflag, MDMBUF);
367 		/* Make sure ucomparam() will do something. */
368 		tp->t_ospeed = 0;
369 		(void) ucomparam(tp, &t);
370 		tp->t_iflag = TTYDEF_IFLAG;
371 		tp->t_oflag = TTYDEF_OFLAG;
372 		tp->t_lflag = TTYDEF_LFLAG;
373 		ttychars(tp);
374 		ttsetwater(tp);
375 
376 		/*
377 		 * Turn on DTR.  We must always do this, even if carrier is not
378 		 * present, because otherwise we'd have to use TIOCSDTR
379 		 * immediately after setting CLOCAL, which applications do not
380 		 * expect.  We always assert DTR while the device is open
381 		 * unless explicitly requested to deassert it.
382 		 */
383 		ucom_dtr(sc, 1);
384 
385 		/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
386 		ucom_hwiflow(sc);
387 
388 		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
389 			 sc->sc_bulkin_no, sc->sc_bulkout_no));
390 
391 		/* Open the bulk pipes */
392 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
393 				     &sc->sc_bulkin_pipe);
394 		if (err) {
395 			DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
396 				 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
397 				 usbd_errstr(err)));
398 			error = EIO;
399 			goto fail_0;
400 		}
401 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
402 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
403 		if (err) {
404 			DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
405 				 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
406 				 usbd_errstr(err)));
407 			error = EIO;
408 			goto fail_1;
409 		}
410 
411 		/* Allocate a request and an input buffer and start reading. */
412 		sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
413 		if (sc->sc_ixfer == NULL) {
414 			error = ENOMEM;
415 			goto fail_2;
416 		}
417 
418 		sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
419 						sc->sc_ibufsizepad);
420 		if (sc->sc_ibuf == NULL) {
421 			error = ENOMEM;
422 			goto fail_3;
423 		}
424 
425 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
426 		if (sc->sc_oxfer == NULL) {
427 			error = ENOMEM;
428 			goto fail_3;
429 		}
430 
431 		sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
432 						sc->sc_obufsize +
433 						sc->sc_opkthdrlen);
434 		if (sc->sc_obuf == NULL) {
435 			error = ENOMEM;
436 			goto fail_4;
437 		}
438 
439 		ucomstartread(sc);
440 	}
441 	sc->sc_opening = 0;
442 	wakeup(&sc->sc_opening);
443 	splx(s);
444 
445 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
446 	if (error)
447 		goto bad;
448 
449 	error = (*tp->t_linesw->l_open)(dev, tp);
450 	if (error)
451 		goto bad;
452 
453 	return (0);
454 
455 fail_4:
456 	usbd_free_xfer(sc->sc_oxfer);
457 	sc->sc_oxfer = NULL;
458 fail_3:
459 	usbd_free_xfer(sc->sc_ixfer);
460 	sc->sc_ixfer = NULL;
461 fail_2:
462 	usbd_close_pipe(sc->sc_bulkout_pipe);
463 	sc->sc_bulkout_pipe = NULL;
464 fail_1:
465 	usbd_close_pipe(sc->sc_bulkin_pipe);
466 	sc->sc_bulkin_pipe = NULL;
467 fail_0:
468 	sc->sc_opening = 0;
469 	wakeup(&sc->sc_opening);
470 	splx(s);
471 	return (error);
472 
473 bad:
474 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
475 		/*
476 		 * We failed to open the device, and nobody else had it opened.
477 		 * Clean up the state as appropriate.
478 		 */
479 		ucom_cleanup(sc);
480 	}
481 
482 	return (error);
483 }
484 
485 int
486 ucomclose(dev_t dev, int flag, int mode, struct proc *p)
487 {
488 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
489 	struct tty *tp = sc->sc_tty;
490 
491 	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
492 	if (!ISSET(tp->t_state, TS_ISOPEN))
493 		return (0);
494 
495 	sc->sc_refcnt++;
496 
497 	(*tp->t_linesw->l_close)(tp, flag);
498 	ttyclose(tp);
499 
500 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
501 		/*
502 		 * Although we got a last close, the device may still be in
503 		 * use; e.g. if this was the dialout node, and there are still
504 		 * processes waiting for carrier on the non-dialout node.
505 		 */
506 		ucom_cleanup(sc);
507 	}
508 
509 	if (sc->sc_methods->ucom_close != NULL)
510 		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
511 
512 	if (--sc->sc_refcnt < 0)
513 		usb_detach_wakeup(USBDEV(sc->sc_dev));
514 
515 	return (0);
516 }
517 
518 int
519 ucomread(dev_t dev, struct uio *uio, int flag)
520 {
521 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
522 	struct tty *tp = sc->sc_tty;
523 	int error;
524 
525 	if (sc->sc_dying)
526 		return (EIO);
527 
528 	sc->sc_refcnt++;
529 	error = ((*tp->t_linesw->l_read)(tp, uio, flag));
530 	if (--sc->sc_refcnt < 0)
531 		usb_detach_wakeup(USBDEV(sc->sc_dev));
532 	return (error);
533 }
534 
535 int
536 ucomwrite(dev_t dev, struct uio *uio, int flag)
537 {
538 	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
539 	struct tty *tp = sc->sc_tty;
540 	int error;
541 
542 	if (sc->sc_dying)
543 		return (EIO);
544 
545 	sc->sc_refcnt++;
546 	error = ((*tp->t_linesw->l_write)(tp, uio, flag));
547 	if (--sc->sc_refcnt < 0)
548 		usb_detach_wakeup(USBDEV(sc->sc_dev));
549 	return (error);
550 }
551 
552 int
553 ucompoll(dev, events, p)
554 	dev_t dev;
555 	int events;
556 	struct proc *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, struct proc *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, struct proc *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 >= 0)
609 		return (error);
610 
611 	error = ttioctl(tp, cmd, data, flag, p);
612 	if (error >= 0)
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 >= 0)
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 = ENOTTY;
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 		return;
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 		return;
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 }
1005 
1006 Static usbd_status
1007 ucomstartread(struct ucom_softc *sc)
1008 {
1009 	usbd_status err;
1010 
1011 	DPRINTFN(5,("ucomstartread: start\n"));
1012 	usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1013 			(usbd_private_handle)sc,
1014 			sc->sc_ibuf, sc->sc_ibufsize,
1015 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
1016 			USBD_NO_TIMEOUT, ucomreadcb);
1017 	err = usbd_transfer(sc->sc_ixfer);
1018 	if (err != USBD_IN_PROGRESS) {
1019 		DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err)));
1020 		return (err);
1021 	}
1022 	return (USBD_NORMAL_COMPLETION);
1023 }
1024 
1025 Static void
1026 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1027 {
1028 	struct ucom_softc *sc = (struct ucom_softc *)p;
1029 	struct tty *tp = sc->sc_tty;
1030 	int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
1031 	usbd_status err;
1032 	u_int32_t cc;
1033 	u_char *cp;
1034 	int s;
1035 
1036 	DPRINTFN(5,("ucomreadcb: status=%d\n", status));
1037 
1038 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
1039 	    sc->sc_dying) {
1040 		DPRINTF(("ucomreadcb: dying\n"));
1041 		/* Send something to wake upper layer */
1042 		s = spltty();
1043 		(*rint)('\n', tp);
1044 		ttwakeup(tp);
1045 		splx(s);
1046 		return;
1047 	}
1048 
1049 	if (status) {
1050 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1051 		/* XXX we should restart after some delay. */
1052 		return;
1053 	}
1054 
1055 	usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
1056 #if defined(__NetBSD__) && NRND > 0
1057 	rnd_add_uint32(&sc->sc_rndsource, cc);
1058 #endif
1059 	DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp));
1060 	if (sc->sc_methods->ucom_read != NULL)
1061 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
1062 					  &cp, &cc);
1063 
1064 	s = spltty();
1065 	/* Give characters to tty layer. */
1066 	while (cc-- > 0) {
1067 		DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp));
1068 		if ((*rint)(*cp++, tp) == -1) {
1069 			/* XXX what should we do? */
1070 			printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1071 			       cc);
1072 			break;
1073 		}
1074 	}
1075 	splx(s);
1076 
1077 	err = ucomstartread(sc);
1078 	if (err) {
1079 		printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1080 		/* XXX what should we dow now? */
1081 	}
1082 }
1083 
1084 Static void
1085 ucom_cleanup(struct ucom_softc *sc)
1086 {
1087 	DPRINTF(("ucom_cleanup: closing pipes\n"));
1088 
1089 	ucom_shutdown(sc);
1090 	if (sc->sc_bulkin_pipe != NULL) {
1091 		usbd_abort_pipe(sc->sc_bulkin_pipe);
1092 		usbd_close_pipe(sc->sc_bulkin_pipe);
1093 		sc->sc_bulkin_pipe = NULL;
1094 	}
1095 	if (sc->sc_bulkout_pipe != NULL) {
1096 		usbd_abort_pipe(sc->sc_bulkout_pipe);
1097 		usbd_close_pipe(sc->sc_bulkout_pipe);
1098 		sc->sc_bulkout_pipe = NULL;
1099 	}
1100 	if (sc->sc_ixfer != NULL) {
1101 		usbd_free_xfer(sc->sc_ixfer);
1102 		sc->sc_ixfer = NULL;
1103 	}
1104 	if (sc->sc_oxfer != NULL) {
1105 		usbd_free_xfer(sc->sc_oxfer);
1106 		sc->sc_oxfer = NULL;
1107 	}
1108 }
1109 
1110 #endif /* NUCOM > 0 */
1111 
1112 int
1113 ucomprint(void *aux, const char *pnp)
1114 {
1115 	struct ucom_attach_args *uca = aux;
1116 
1117 	if (pnp)
1118 		printf("ucom at %s", pnp);
1119 	if (uca->portno != UCOM_UNK_PORTNO)
1120 		printf(" portno %d", uca->portno);
1121 	return (UNCONF);
1122 }
1123 
1124 int
1125 ucomsubmatch(struct device *parent, struct cfdata *cf, void *aux)
1126 {
1127 	struct ucom_attach_args *uca = aux;
1128 
1129 	if (uca->portno != UCOM_UNK_PORTNO &&
1130 	    cf->ucomcf_portno != UCOM_UNK_PORTNO &&
1131 	    cf->ucomcf_portno != uca->portno)
1132 		return (0);
1133 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1134 }
1135