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