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