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