xref: /netbsd-src/sys/dev/usb/ucom.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: ucom.c,v 1.110 2015/11/08 21:05:01 joerg 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * This code is very heavily based on the 16550 driver, com.c.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ucom.c,v 1.110 2015/11/08 21:05:01 joerg Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/ioctl.h>
43 #include <sys/conf.h>
44 #include <sys/tty.h>
45 #include <sys/file.h>
46 #include <sys/select.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/device.h>
50 #include <sys/poll.h>
51 #include <sys/queue.h>
52 #include <sys/kauth.h>
53 #include <sys/timepps.h>
54 #include <sys/rndsource.h>
55 
56 #include <dev/usb/usb.h>
57 
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbdevs.h>
61 #include <dev/usb/usb_quirks.h>
62 
63 #include <dev/usb/ucomvar.h>
64 
65 #include "ucom.h"
66 
67 #include "locators.h"
68 
69 #if NUCOM > 0
70 
71 #ifdef UCOM_DEBUG
72 #define DPRINTFN(n, x)	if (ucomdebug > (n)) printf x
73 int ucomdebug = 0;
74 #else
75 #define DPRINTFN(n, x)
76 #endif
77 #define DPRINTF(x) DPRINTFN(0, x)
78 
79 #define	UCOMCALLUNIT_MASK	TTCALLUNIT_MASK
80 #define	UCOMUNIT_MASK		TTUNIT_MASK
81 #define	UCOMDIALOUT_MASK	TTDIALOUT_MASK
82 
83 #define	UCOMCALLUNIT(x)		TTCALLUNIT(x)
84 #define	UCOMUNIT(x)		TTUNIT(x)
85 #define	UCOMDIALOUT(x)		TTDIALOUT(x)
86 
87 /*
88  * XXX: We can submit multiple input/output buffers to the usb stack
89  * to improve throughput, but the usb stack is too lame to deal with this
90  * in a number of places.
91  */
92 #define	UCOM_IN_BUFFS	1
93 #define	UCOM_OUT_BUFFS	1
94 
95 struct ucom_buffer {
96 	SIMPLEQ_ENTRY(ucom_buffer) ub_link;
97 	usbd_xfer_handle ub_xfer;
98 	u_char *ub_data;
99 	u_int ub_len;
100 	u_int ub_index;
101 };
102 
103 struct ucom_softc {
104 	device_t		sc_dev;		/* base device */
105 
106 	usbd_device_handle	sc_udev;	/* USB device */
107 
108 	usbd_interface_handle	sc_iface;	/* data interface */
109 
110 	int			sc_bulkin_no;	/* bulk in endpoint address */
111 	usbd_pipe_handle	sc_bulkin_pipe;	/* bulk in pipe */
112 	u_int			sc_ibufsize;	/* read buffer size */
113 	u_int			sc_ibufsizepad;	/* read buffer size padded */
114 	struct ucom_buffer	sc_ibuff[UCOM_IN_BUFFS];
115 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_empty;
116 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_full;
117 
118 	int			sc_bulkout_no;	/* bulk out endpoint address */
119 	usbd_pipe_handle	sc_bulkout_pipe;/* bulk out pipe */
120 	u_int			sc_obufsize;	/* write buffer size */
121 	u_int			sc_opkthdrlen;	/* header length of */
122 	struct ucom_buffer	sc_obuff[UCOM_OUT_BUFFS];
123 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_free;
124 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_full;
125 
126 	void			*sc_si;
127 
128 	const struct ucom_methods *sc_methods;
129 	void                    *sc_parent;
130 	int			sc_portno;
131 
132 	struct tty		*sc_tty;	/* our tty */
133 	u_char			sc_lsr;
134 	u_char			sc_msr;
135 	u_char			sc_mcr;
136 	volatile u_char		sc_rx_stopped;
137 	u_char			sc_rx_unblock;
138 	u_char			sc_tx_stopped;
139 	int			sc_swflags;
140 
141 	u_char			sc_opening;	/* lock during open */
142 	int			sc_refcnt;
143 	u_char			sc_dying;	/* disconnecting */
144 
145 	struct pps_state	sc_pps_state;	/* pps state */
146 
147 	krndsource_t	sc_rndsource;	/* random source */
148 };
149 
150 dev_type_open(ucomopen);
151 dev_type_close(ucomclose);
152 dev_type_read(ucomread);
153 dev_type_write(ucomwrite);
154 dev_type_ioctl(ucomioctl);
155 dev_type_stop(ucomstop);
156 dev_type_tty(ucomtty);
157 dev_type_poll(ucompoll);
158 
159 const struct cdevsw ucom_cdevsw = {
160 	.d_open = ucomopen,
161 	.d_close = ucomclose,
162 	.d_read = ucomread,
163 	.d_write = ucomwrite,
164 	.d_ioctl = ucomioctl,
165 	.d_stop = ucomstop,
166 	.d_tty = ucomtty,
167 	.d_poll = ucompoll,
168 	.d_mmap = nommap,
169 	.d_kqfilter = ttykqfilter,
170 	.d_discard = nodiscard,
171 	.d_flag = D_TTY
172 };
173 
174 static void	ucom_cleanup(struct ucom_softc *);
175 static int	ucomparam(struct tty *, struct termios *);
176 static int	ucomhwiflow(struct tty *, int);
177 static void	ucomstart(struct tty *);
178 static void	ucom_shutdown(struct ucom_softc *);
179 static int	ucom_do_ioctl(struct ucom_softc *, u_long, void *,
180 			      int, struct lwp *);
181 static void	ucom_dtr(struct ucom_softc *, int);
182 static void	ucom_rts(struct ucom_softc *, int);
183 static void	ucom_break(struct ucom_softc *, int);
184 static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
185 static int	ucom_to_tiocm(struct ucom_softc *);
186 
187 static void	ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
188 static void	ucom_submit_write(struct ucom_softc *, struct ucom_buffer *);
189 static void	ucom_write_status(struct ucom_softc *, struct ucom_buffer *,
190 			usbd_status);
191 
192 static void	ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
193 static void	ucom_read_complete(struct ucom_softc *);
194 static usbd_status ucomsubmitread(struct ucom_softc *, struct ucom_buffer *);
195 static void	ucom_softintr(void *);
196 
197 int ucom_match(device_t, cfdata_t, void *);
198 void ucom_attach(device_t, device_t, void *);
199 int ucom_detach(device_t, int);
200 int ucom_activate(device_t, enum devact);
201 extern struct cfdriver ucom_cd;
202 CFATTACH_DECL_NEW(ucom, sizeof(struct ucom_softc), ucom_match, ucom_attach,
203     ucom_detach, ucom_activate);
204 
205 int
206 ucom_match(device_t parent, cfdata_t match, void *aux)
207 {
208 	return (1);
209 }
210 
211 void
212 ucom_attach(device_t parent, device_t self, void *aux)
213 {
214 	struct ucom_softc *sc = device_private(self);
215 	struct ucom_attach_args *uca = aux;
216 	struct tty *tp;
217 
218 	if (uca->info != NULL)
219 		aprint_normal(": %s", uca->info);
220 	aprint_normal("\n");
221 
222 	prop_dictionary_set_int32(device_properties(self), "port", uca->portno);
223 
224 	sc->sc_dev = self;
225 	sc->sc_udev = uca->device;
226 	sc->sc_iface = uca->iface;
227 	sc->sc_bulkout_no = uca->bulkout;
228 	sc->sc_bulkin_no = uca->bulkin;
229 	sc->sc_ibufsize = uca->ibufsize;
230 	sc->sc_ibufsizepad = uca->ibufsizepad;
231 	sc->sc_obufsize = uca->obufsize;
232 	sc->sc_opkthdrlen = uca->opkthdrlen;
233 	sc->sc_methods = uca->methods;
234 	sc->sc_parent = uca->arg;
235 	sc->sc_portno = uca->portno;
236 
237 	sc->sc_lsr = 0;
238 	sc->sc_msr = 0;
239 	sc->sc_mcr = 0;
240 	sc->sc_tx_stopped = 0;
241 	sc->sc_swflags = 0;
242 	sc->sc_opening = 0;
243 	sc->sc_refcnt = 0;
244 	sc->sc_dying = 0;
245 
246 	sc->sc_si = softint_establish(SOFTINT_NET, ucom_softintr, sc);
247 
248 	tp = tty_alloc();
249 	tp->t_oproc = ucomstart;
250 	tp->t_param = ucomparam;
251 	tp->t_hwiflow = ucomhwiflow;
252 	sc->sc_tty = tp;
253 
254 	DPRINTF(("ucom_attach: tty_attach %p\n", tp));
255 	tty_attach(tp);
256 
257 	rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
258 			  RND_TYPE_TTY, RND_FLAG_DEFAULT);
259 
260 	if (!pmf_device_register(self, NULL, NULL))
261 		aprint_error_dev(self, "couldn't establish power handler\n");
262 	return;
263 }
264 
265 int
266 ucom_detach(device_t self, int flags)
267 {
268 	struct ucom_softc *sc = device_private(self);
269 	struct tty *tp = sc->sc_tty;
270 	int maj, mn;
271 	int s, i;
272 
273 	DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p, pipe=%d,%d\n",
274 		 sc, flags, tp, sc->sc_bulkin_no, sc->sc_bulkout_no));
275 
276 	sc->sc_dying = 1;
277 	pmf_device_deregister(self);
278 
279 	if (sc->sc_bulkin_pipe != NULL)
280 		usbd_abort_pipe(sc->sc_bulkin_pipe);
281 	if (sc->sc_bulkout_pipe != NULL)
282 		usbd_abort_pipe(sc->sc_bulkout_pipe);
283 
284 	s = splusb();
285 	if (--sc->sc_refcnt >= 0) {
286 		/* Wake up anyone waiting */
287 		if (tp != NULL) {
288 			mutex_spin_enter(&tty_lock);
289 			CLR(tp->t_state, TS_CARR_ON);
290 			CLR(tp->t_cflag, CLOCAL | MDMBUF);
291 			ttyflush(tp, FREAD|FWRITE);
292 			mutex_spin_exit(&tty_lock);
293 		}
294 		/* Wait for processes to go away. */
295 		usb_detach_waitold(sc->sc_dev);
296 	}
297 
298 	softint_disestablish(sc->sc_si);
299 	splx(s);
300 
301 	/* locate the major number */
302 	maj = cdevsw_lookup_major(&ucom_cdevsw);
303 
304 	/* Nuke the vnodes for any open instances. */
305 	mn = device_unit(self);
306 	DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
307 	vdevgone(maj, mn, mn, VCHR);
308 	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
309 	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
310 
311 	/* Detach and free the tty. */
312 	if (tp != NULL) {
313 		tty_detach(tp);
314 		tty_free(tp);
315 		sc->sc_tty = NULL;
316 	}
317 
318 	for (i = 0; i < UCOM_IN_BUFFS; i++) {
319 		if (sc->sc_ibuff[i].ub_xfer != NULL)
320 			usbd_free_xfer(sc->sc_ibuff[i].ub_xfer);
321 	}
322 
323 	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
324 		if (sc->sc_obuff[i].ub_xfer != NULL)
325 			usbd_free_xfer(sc->sc_obuff[i].ub_xfer);
326 	}
327 
328 	/* Detach the random source */
329 	rnd_detach_source(&sc->sc_rndsource);
330 
331 	return (0);
332 }
333 
334 int
335 ucom_activate(device_t self, enum devact act)
336 {
337 	struct ucom_softc *sc = device_private(self);
338 
339 	DPRINTFN(5,("ucom_activate: %d\n", act));
340 
341 	switch (act) {
342 	case DVACT_DEACTIVATE:
343 		sc->sc_dying = 1;
344 		return 0;
345 	default:
346 		return EOPNOTSUPP;
347 	}
348 }
349 
350 void
351 ucom_shutdown(struct ucom_softc *sc)
352 {
353 	struct tty *tp = sc->sc_tty;
354 
355 	DPRINTF(("ucom_shutdown\n"));
356 	/*
357 	 * Hang up if necessary.  Wait a bit, so the other side has time to
358 	 * notice even if we immediately open the port again.
359 	 */
360 	if (ISSET(tp->t_cflag, HUPCL)) {
361 		ucom_dtr(sc, 0);
362 		(void)tsleep(sc, TTIPRI, ttclos, hz);
363 	}
364 }
365 
366 int
367 ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
368 {
369 	int unit = UCOMUNIT(dev);
370 	usbd_status err;
371 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
372 	struct ucom_buffer *ub;
373 	struct tty *tp;
374 	int s, i;
375 	int error;
376 
377 	if (sc == NULL)
378 		return (ENXIO);
379 
380 	if (sc->sc_dying)
381 		return (EIO);
382 
383 	if (!device_is_active(sc->sc_dev))
384 		return (ENXIO);
385 
386 	tp = sc->sc_tty;
387 
388 	DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
389 
390 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
391 		return (EBUSY);
392 
393 	s = spltty();
394 
395 	/*
396 	 * Do the following iff this is a first open.
397 	 */
398 	while (sc->sc_opening)
399 		tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
400 
401 	if (sc->sc_dying) {
402 		splx(s);
403 		return (EIO);
404 	}
405 	sc->sc_opening = 1;
406 
407 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
408 		struct termios t;
409 
410 		tp->t_dev = dev;
411 
412 		if (sc->sc_methods->ucom_open != NULL) {
413 			error = sc->sc_methods->ucom_open(sc->sc_parent,
414 							  sc->sc_portno);
415 			if (error) {
416 				ucom_cleanup(sc);
417 				sc->sc_opening = 0;
418 				wakeup(&sc->sc_opening);
419 				splx(s);
420 				return (error);
421 			}
422 		}
423 
424 		ucom_status_change(sc);
425 
426 		/* Clear PPS capture state on first open. */
427 		mutex_spin_enter(&timecounter_lock);
428 		memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
429 		sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
430 		pps_init(&sc->sc_pps_state);
431 		mutex_spin_exit(&timecounter_lock);
432 
433 		/*
434 		 * Initialize the termios status to the defaults.  Add in the
435 		 * sticky bits from TIOCSFLAGS.
436 		 */
437 		t.c_ispeed = 0;
438 		t.c_ospeed = TTYDEF_SPEED;
439 		t.c_cflag = TTYDEF_CFLAG;
440 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
441 			SET(t.c_cflag, CLOCAL);
442 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
443 			SET(t.c_cflag, CRTSCTS);
444 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
445 			SET(t.c_cflag, MDMBUF);
446 		/* Make sure ucomparam() will do something. */
447 		tp->t_ospeed = 0;
448 		(void) ucomparam(tp, &t);
449 		tp->t_iflag = TTYDEF_IFLAG;
450 		tp->t_oflag = TTYDEF_OFLAG;
451 		tp->t_lflag = TTYDEF_LFLAG;
452 		ttychars(tp);
453 		ttsetwater(tp);
454 
455 		/*
456 		 * Turn on DTR.  We must always do this, even if carrier is not
457 		 * present, because otherwise we'd have to use TIOCSDTR
458 		 * immediately after setting CLOCAL, which applications do not
459 		 * expect.  We always assert DTR while the device is open
460 		 * unless explicitly requested to deassert it.  Ditto RTS.
461 		 */
462 		ucom_dtr(sc, 1);
463 		ucom_rts(sc, 1);
464 
465 		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
466 			 sc->sc_bulkin_no, sc->sc_bulkout_no));
467 
468 		/* Open the bulk pipes */
469 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no,
470 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
471 		if (err) {
472 			DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
473 				 device_xname(sc->sc_dev), sc->sc_bulkin_no,
474 				 usbd_errstr(err)));
475 			error = EIO;
476 			goto fail_0;
477 		}
478 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
479 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
480 		if (err) {
481 			DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
482 				 device_xname(sc->sc_dev), sc->sc_bulkout_no,
483 				 usbd_errstr(err)));
484 			error = EIO;
485 			goto fail_1;
486 		}
487 
488 		sc->sc_rx_unblock = 0;
489 		sc->sc_rx_stopped = 0;
490 		sc->sc_tx_stopped = 0;
491 
492 		memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff));
493 		memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff));
494 
495 		SIMPLEQ_INIT(&sc->sc_ibuff_empty);
496 		SIMPLEQ_INIT(&sc->sc_ibuff_full);
497 		SIMPLEQ_INIT(&sc->sc_obuff_free);
498 		SIMPLEQ_INIT(&sc->sc_obuff_full);
499 
500 		/* Allocate input buffers */
501 		for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
502 		    ub++) {
503 			ub->ub_xfer = usbd_alloc_xfer(sc->sc_udev);
504 			if (ub->ub_xfer == NULL) {
505 				error = ENOMEM;
506 				goto fail_2;
507 			}
508 			ub->ub_data = usbd_alloc_buffer(ub->ub_xfer,
509 			    sc->sc_ibufsizepad);
510 			if (ub->ub_data == NULL) {
511 				error = ENOMEM;
512 				goto fail_2;
513 			}
514 
515 			if (ucomsubmitread(sc, ub) != USBD_NORMAL_COMPLETION) {
516 				error = EIO;
517 				goto fail_2;
518 			}
519 		}
520 
521 		for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
522 		    ub++) {
523 			ub->ub_xfer = usbd_alloc_xfer(sc->sc_udev);
524 			if (ub->ub_xfer == NULL) {
525 				error = ENOMEM;
526 				goto fail_2;
527 			}
528 			ub->ub_data = usbd_alloc_buffer(ub->ub_xfer,
529 			    sc->sc_obufsize);
530 			if (ub->ub_data == NULL) {
531 				error = ENOMEM;
532 				goto fail_2;
533 			}
534 
535 			SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
536 		}
537 
538 	}
539 	sc->sc_opening = 0;
540 	wakeup(&sc->sc_opening);
541 	splx(s);
542 
543 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
544 	if (error)
545 		goto bad;
546 
547 	error = (*tp->t_linesw->l_open)(dev, tp);
548 	if (error)
549 		goto bad;
550 
551 	return (0);
552 
553 fail_2:
554 	usbd_abort_pipe(sc->sc_bulkin_pipe);
555 	for (i = 0; i < UCOM_IN_BUFFS; i++) {
556 		if (sc->sc_ibuff[i].ub_xfer != NULL) {
557 			usbd_free_xfer(sc->sc_ibuff[i].ub_xfer);
558 			sc->sc_ibuff[i].ub_xfer = NULL;
559 			sc->sc_ibuff[i].ub_data = NULL;
560 		}
561 	}
562 	usbd_abort_pipe(sc->sc_bulkout_pipe);
563 	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
564 		if (sc->sc_obuff[i].ub_xfer != NULL) {
565 			usbd_free_xfer(sc->sc_obuff[i].ub_xfer);
566 			sc->sc_obuff[i].ub_xfer = NULL;
567 			sc->sc_obuff[i].ub_data = NULL;
568 		}
569 	}
570 
571 	usbd_close_pipe(sc->sc_bulkout_pipe);
572 	sc->sc_bulkout_pipe = NULL;
573 fail_1:
574 	usbd_close_pipe(sc->sc_bulkin_pipe);
575 	sc->sc_bulkin_pipe = NULL;
576 fail_0:
577 	sc->sc_opening = 0;
578 	wakeup(&sc->sc_opening);
579 	splx(s);
580 	return (error);
581 
582 bad:
583 	s = spltty();
584 	CLR(tp->t_state, TS_BUSY);
585 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
586 		/*
587 		 * We failed to open the device, and nobody else had it opened.
588 		 * Clean up the state as appropriate.
589 		 */
590 		ucom_cleanup(sc);
591 	}
592 	splx(s);
593 
594 	return (error);
595 }
596 
597 int
598 ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
599 {
600 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
601 	struct tty *tp;
602 	int s;
603 
604 	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
605 
606 	if (sc == NULL)
607 		return 0;
608 
609 	tp = sc->sc_tty;
610 
611 	if (!ISSET(tp->t_state, TS_ISOPEN))
612 		return (0);
613 
614 	s = spltty();
615 	sc->sc_refcnt++;
616 
617 	(*tp->t_linesw->l_close)(tp, flag);
618 	ttyclose(tp);
619 
620 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
621 		/*
622 		 * Although we got a last close, the device may still be in
623 		 * use; e.g. if this was the dialout node, and there are still
624 		 * processes waiting for carrier on the non-dialout node.
625 		 */
626 		ucom_cleanup(sc);
627 	}
628 
629 	if (sc->sc_methods->ucom_close != NULL)
630 		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
631 
632 	if (--sc->sc_refcnt < 0)
633 		usb_detach_wakeupold(sc->sc_dev);
634 	splx(s);
635 
636 	return (0);
637 }
638 
639 int
640 ucomread(dev_t dev, struct uio *uio, int flag)
641 {
642 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
643 	struct tty *tp;
644 	int error;
645 
646 	if (sc == NULL || sc->sc_dying)
647 		return (EIO);
648 
649 	tp = sc->sc_tty;
650 
651 	sc->sc_refcnt++;
652 	error = ((*tp->t_linesw->l_read)(tp, uio, flag));
653 	if (--sc->sc_refcnt < 0)
654 		usb_detach_wakeupold(sc->sc_dev);
655 	return (error);
656 }
657 
658 int
659 ucomwrite(dev_t dev, struct uio *uio, int flag)
660 {
661 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
662 	struct tty *tp;
663 	int error;
664 
665 	if (sc == NULL || sc->sc_dying)
666 		return (EIO);
667 
668 	tp = sc->sc_tty;
669 
670 	sc->sc_refcnt++;
671 	error = ((*tp->t_linesw->l_write)(tp, uio, flag));
672 	if (--sc->sc_refcnt < 0)
673 		usb_detach_wakeupold(sc->sc_dev);
674 	return (error);
675 }
676 
677 int
678 ucompoll(dev_t dev, int events, struct lwp *l)
679 {
680 	struct ucom_softc *sc;
681 	struct tty *tp;
682 	int revents;
683 
684 	sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
685 	if (sc == NULL || sc->sc_dying)
686 		return (POLLHUP);
687 
688 	tp = sc->sc_tty;
689 
690 	sc->sc_refcnt++;
691 	revents = ((*tp->t_linesw->l_poll)(tp, events, l));
692 	if (--sc->sc_refcnt < 0)
693 		usb_detach_wakeupold(sc->sc_dev);
694 	return (revents);
695 }
696 
697 struct tty *
698 ucomtty(dev_t dev)
699 {
700 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
701 
702 	return ((sc != NULL) ? sc->sc_tty : NULL);
703 }
704 
705 int
706 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
707 {
708 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
709 	int error;
710 
711 	if (sc == NULL || sc->sc_dying)
712 		return (EIO);
713 
714 	sc->sc_refcnt++;
715 	error = ucom_do_ioctl(sc, cmd, data, flag, l);
716 	if (--sc->sc_refcnt < 0)
717 		usb_detach_wakeupold(sc->sc_dev);
718 	return (error);
719 }
720 
721 static int
722 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
723 	      int flag, struct lwp *l)
724 {
725 	struct tty *tp = sc->sc_tty;
726 	int error;
727 	int s;
728 
729 	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
730 
731 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
732 	if (error != EPASSTHROUGH)
733 		return (error);
734 
735 	error = ttioctl(tp, cmd, data, flag, l);
736 	if (error != EPASSTHROUGH)
737 		return (error);
738 
739 	if (sc->sc_methods->ucom_ioctl != NULL) {
740 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
741 			    sc->sc_portno, cmd, data, flag, l->l_proc);
742 		if (error != EPASSTHROUGH)
743 			return (error);
744 	}
745 
746 	error = 0;
747 
748 	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
749 	s = spltty();
750 
751 	switch (cmd) {
752 	case TIOCSBRK:
753 		ucom_break(sc, 1);
754 		break;
755 
756 	case TIOCCBRK:
757 		ucom_break(sc, 0);
758 		break;
759 
760 	case TIOCSDTR:
761 		ucom_dtr(sc, 1);
762 		break;
763 
764 	case TIOCCDTR:
765 		ucom_dtr(sc, 0);
766 		break;
767 
768 	case TIOCGFLAGS:
769 		*(int *)data = sc->sc_swflags;
770 		break;
771 
772 	case TIOCSFLAGS:
773 		error = kauth_authorize_device_tty(l->l_cred,
774 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
775 		if (error)
776 			break;
777 		sc->sc_swflags = *(int *)data;
778 		break;
779 
780 	case TIOCMSET:
781 	case TIOCMBIS:
782 	case TIOCMBIC:
783 		tiocm_to_ucom(sc, cmd, *(int *)data);
784 		break;
785 
786 	case TIOCMGET:
787 		*(int *)data = ucom_to_tiocm(sc);
788 		break;
789 
790 	case PPS_IOC_CREATE:
791 	case PPS_IOC_DESTROY:
792 	case PPS_IOC_GETPARAMS:
793 	case PPS_IOC_SETPARAMS:
794 	case PPS_IOC_GETCAP:
795 	case PPS_IOC_FETCH:
796 #ifdef PPS_SYNC
797 	case PPS_IOC_KCBIND:
798 #endif
799 		mutex_spin_enter(&timecounter_lock);
800 		error = pps_ioctl(cmd, data, &sc->sc_pps_state);
801 		mutex_spin_exit(&timecounter_lock);
802 		break;
803 
804 	default:
805 		error = EPASSTHROUGH;
806 		break;
807 	}
808 
809 	splx(s);
810 
811 	return (error);
812 }
813 
814 static void
815 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
816 {
817 	u_char combits;
818 
819 	combits = 0;
820 	if (ISSET(ttybits, TIOCM_DTR))
821 		SET(combits, UMCR_DTR);
822 	if (ISSET(ttybits, TIOCM_RTS))
823 		SET(combits, UMCR_RTS);
824 
825 	switch (how) {
826 	case TIOCMBIC:
827 		CLR(sc->sc_mcr, combits);
828 		break;
829 
830 	case TIOCMBIS:
831 		SET(sc->sc_mcr, combits);
832 		break;
833 
834 	case TIOCMSET:
835 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
836 		SET(sc->sc_mcr, combits);
837 		break;
838 	}
839 
840 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
841 		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
842 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
843 		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
844 }
845 
846 static int
847 ucom_to_tiocm(struct ucom_softc *sc)
848 {
849 	u_char combits;
850 	int ttybits = 0;
851 
852 	combits = sc->sc_mcr;
853 	if (ISSET(combits, UMCR_DTR))
854 		SET(ttybits, TIOCM_DTR);
855 	if (ISSET(combits, UMCR_RTS))
856 		SET(ttybits, TIOCM_RTS);
857 
858 	combits = sc->sc_msr;
859 	if (ISSET(combits, UMSR_DCD))
860 		SET(ttybits, TIOCM_CD);
861 	if (ISSET(combits, UMSR_CTS))
862 		SET(ttybits, TIOCM_CTS);
863 	if (ISSET(combits, UMSR_DSR))
864 		SET(ttybits, TIOCM_DSR);
865 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
866 		SET(ttybits, TIOCM_RI);
867 
868 #if 0
869 XXX;
870 	if (sc->sc_ier != 0)
871 		SET(ttybits, TIOCM_LE);
872 #endif
873 
874 	return (ttybits);
875 }
876 
877 static void
878 ucom_break(struct ucom_softc *sc, int onoff)
879 {
880 	DPRINTF(("ucom_break: onoff=%d\n", onoff));
881 
882 	if (sc->sc_methods->ucom_set != NULL)
883 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
884 		    UCOM_SET_BREAK, onoff);
885 }
886 
887 static void
888 ucom_dtr(struct ucom_softc *sc, int onoff)
889 {
890 	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
891 
892 	if (sc->sc_methods->ucom_set != NULL)
893 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
894 		    UCOM_SET_DTR, onoff);
895 }
896 
897 static void
898 ucom_rts(struct ucom_softc *sc, int onoff)
899 {
900 	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
901 
902 	if (sc->sc_methods->ucom_set != NULL)
903 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
904 		    UCOM_SET_RTS, onoff);
905 }
906 
907 void
908 ucom_status_change(struct ucom_softc *sc)
909 {
910 	struct tty *tp = sc->sc_tty;
911 	u_char old_msr;
912 
913 	if (sc->sc_methods->ucom_get_status != NULL) {
914 		old_msr = sc->sc_msr;
915 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
916 		    &sc->sc_lsr, &sc->sc_msr);
917 		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
918 			mutex_spin_enter(&timecounter_lock);
919 			pps_capture(&sc->sc_pps_state);
920 			pps_event(&sc->sc_pps_state,
921 			    (sc->sc_msr & UMSR_DCD) ?
922 			    PPS_CAPTUREASSERT :
923 			    PPS_CAPTURECLEAR);
924 			mutex_spin_exit(&timecounter_lock);
925 
926 			(*tp->t_linesw->l_modem)(tp,
927 			    ISSET(sc->sc_msr, UMSR_DCD));
928 		}
929 	} else {
930 		sc->sc_lsr = 0;
931 		/* Assume DCD is present, if we have no chance to check it. */
932 		sc->sc_msr = UMSR_DCD;
933 	}
934 }
935 
936 static int
937 ucomparam(struct tty *tp, struct termios *t)
938 {
939 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
940 	    UCOMUNIT(tp->t_dev));
941 	int error;
942 
943 	if (sc == NULL || sc->sc_dying)
944 		return (EIO);
945 
946 	/* Check requested parameters. */
947 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
948 		return (EINVAL);
949 
950 	/*
951 	 * For the console, always force CLOCAL and !HUPCL, so that the port
952 	 * is always active.
953 	 */
954 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
955 		SET(t->c_cflag, CLOCAL);
956 		CLR(t->c_cflag, HUPCL);
957 	}
958 
959 	/*
960 	 * If there were no changes, don't do anything.  This avoids dropping
961 	 * input and improves performance when all we did was frob things like
962 	 * VMIN and VTIME.
963 	 */
964 	if (tp->t_ospeed == t->c_ospeed &&
965 	    tp->t_cflag == t->c_cflag)
966 		return (0);
967 
968 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
969 
970 	/* And copy to tty. */
971 	tp->t_ispeed = 0;
972 	tp->t_ospeed = t->c_ospeed;
973 	tp->t_cflag = t->c_cflag;
974 
975 	if (sc->sc_methods->ucom_param != NULL) {
976 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
977 			    t);
978 		if (error)
979 			return (error);
980 	}
981 
982 	/* XXX worry about CHWFLOW */
983 
984 	/*
985 	 * Update the tty layer's idea of the carrier bit, in case we changed
986 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
987 	 * explicit request.
988 	 */
989 	DPRINTF(("ucomparam: l_modem\n"));
990 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
991 
992 #if 0
993 XXX what if the hardware is not open
994 	if (!ISSET(t->c_cflag, CHWFLOW)) {
995 		if (sc->sc_tx_stopped) {
996 			sc->sc_tx_stopped = 0;
997 			ucomstart(tp);
998 		}
999 	}
1000 #endif
1001 
1002 	return (0);
1003 }
1004 
1005 static int
1006 ucomhwiflow(struct tty *tp, int block)
1007 {
1008 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
1009 	    UCOMUNIT(tp->t_dev));
1010 	int old;
1011 
1012 	if (sc == NULL)
1013 		return (0);
1014 
1015 	old = sc->sc_rx_stopped;
1016 	sc->sc_rx_stopped = (u_char)block;
1017 
1018 	if (old && !block) {
1019 		int s = splusb();
1020 		sc->sc_rx_unblock = 1;
1021 		softint_schedule(sc->sc_si);
1022 		splx(s);
1023 	}
1024 
1025 	return (1);
1026 }
1027 
1028 static void
1029 ucomstart(struct tty *tp)
1030 {
1031 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
1032 	    UCOMUNIT(tp->t_dev));
1033 	struct ucom_buffer *ub;
1034 	int s;
1035 	u_char *data;
1036 	int cnt;
1037 
1038 	if (sc == NULL || sc->sc_dying)
1039 		return;
1040 
1041 	s = spltty();
1042 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1043 		goto out;
1044 	if (sc->sc_tx_stopped)
1045 		goto out;
1046 
1047 	if (!ttypull(tp))
1048 		goto out;
1049 
1050 	/* Grab the first contiguous region of buffer space. */
1051 	data = tp->t_outq.c_cf;
1052 	cnt = ndqb(&tp->t_outq, 0);
1053 
1054 	if (cnt == 0)
1055 		goto out;
1056 
1057 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
1058 	if (ub == NULL) {
1059 		SET(tp->t_state, TS_BUSY);
1060 		goto out;
1061 	}
1062 	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
1063 
1064 	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
1065 		SET(tp->t_state, TS_BUSY);
1066 
1067 	if (cnt > sc->sc_obufsize)
1068 		cnt = sc->sc_obufsize;
1069 
1070 	if (sc->sc_methods->ucom_write != NULL)
1071 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
1072 					   ub->ub_data, data, &cnt);
1073 	else
1074 		memcpy(ub->ub_data, data, cnt);
1075 
1076 	ub->ub_len = cnt;
1077 	ub->ub_index = 0;
1078 
1079 	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
1080 
1081 	softint_schedule(sc->sc_si);
1082 
1083  out:
1084 	splx(s);
1085 }
1086 
1087 void
1088 ucomstop(struct tty *tp, int flag)
1089 {
1090 #if 0
1091 	/*struct ucom_softc *sc =
1092 	    device_lookup_private(&ucom_cd, UCOMUNIT(tp->t_dev));*/
1093 	int s;
1094 
1095 	s = spltty();
1096 	if (ISSET(tp->t_state, TS_BUSY)) {
1097 		/* sc->sc_tx_stopped = 1; */
1098 		if (!ISSET(tp->t_state, TS_TTSTOP))
1099 			SET(tp->t_state, TS_FLUSH);
1100 	}
1101 	splx(s);
1102 #endif
1103 }
1104 
1105 static void
1106 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
1107     usbd_status err)
1108 {
1109 	struct tty *tp = sc->sc_tty;
1110 	uint32_t cc = ub->ub_len;
1111 
1112 	switch (err) {
1113 	case USBD_IN_PROGRESS:
1114 		ub->ub_index = ub->ub_len;
1115 		break;
1116 	case USBD_STALLED:
1117 		ub->ub_index = 0;
1118 		softint_schedule(sc->sc_si);
1119 		break;
1120 	case USBD_NORMAL_COMPLETION:
1121 		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
1122 		rnd_add_uint32(&sc->sc_rndsource, cc);
1123 		/*FALLTHROUGH*/
1124 	default:
1125 		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
1126 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
1127 		cc -= sc->sc_opkthdrlen;
1128 
1129 		mutex_spin_enter(&tty_lock);
1130 		CLR(tp->t_state, TS_BUSY);
1131 		if (ISSET(tp->t_state, TS_FLUSH))
1132 			CLR(tp->t_state, TS_FLUSH);
1133 		else
1134 			ndflush(&tp->t_outq, cc);
1135 		mutex_spin_exit(&tty_lock);
1136 
1137 		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
1138 		    !sc->sc_dying) {
1139 			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
1140 				ucom_submit_write(sc, ub);
1141 
1142 			(*tp->t_linesw->l_start)(tp);
1143 		}
1144 		break;
1145 	}
1146 }
1147 
1148 /* Call at spltty() */
1149 static void
1150 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
1151 {
1152 
1153 	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkout_pipe,
1154 	    (usbd_private_handle)sc, ub->ub_data, ub->ub_len,
1155 	    USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
1156 
1157 	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
1158 }
1159 
1160 static void
1161 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1162 {
1163 	struct ucom_softc *sc = (struct ucom_softc *)p;
1164 	int s;
1165 
1166 	s = spltty();
1167 
1168 	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
1169 
1170 	splx(s);
1171 }
1172 
1173 static void
1174 ucom_softintr(void *arg)
1175 {
1176 	struct ucom_softc *sc = arg;
1177 	struct tty *tp = sc->sc_tty;
1178 	struct ucom_buffer *ub;
1179 	int s;
1180 
1181 	if (!ISSET(tp->t_state, TS_ISOPEN))
1182 		return;
1183 
1184 	s = spltty();
1185 
1186 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
1187 
1188 	if (ub != NULL && ub->ub_index == 0)
1189 		ucom_submit_write(sc, ub);
1190 
1191 	if (sc->sc_rx_unblock)
1192 		ucom_read_complete(sc);
1193 
1194 	splx(s);
1195 }
1196 
1197 static void
1198 ucom_read_complete(struct ucom_softc *sc)
1199 {
1200 	int (*rint)(int, struct tty *);
1201 	struct ucom_buffer *ub;
1202 	struct tty *tp;
1203 	int s;
1204 
1205 	tp = sc->sc_tty;
1206 	rint = tp->t_linesw->l_rint;
1207 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
1208 
1209 	while (ub != NULL && !sc->sc_rx_stopped) {
1210 
1211 		s = spltty();
1212 
1213 		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
1214 			/* Give characters to tty layer. */
1215 			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
1216 				/* Overflow: drop remainder */
1217 				ub->ub_index = ub->ub_len;
1218 			} else
1219 				ub->ub_index++;
1220 		}
1221 
1222 		splx(s);
1223 
1224 		if (ub->ub_index == ub->ub_len) {
1225 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
1226 
1227 			ucomsubmitread(sc, ub);
1228 
1229 			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
1230 		}
1231 	}
1232 
1233 	sc->sc_rx_unblock = (ub != NULL);
1234 }
1235 
1236 static usbd_status
1237 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
1238 {
1239 	usbd_status err;
1240 
1241 	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkin_pipe,
1242 	    (usbd_private_handle)sc, ub->ub_data, sc->sc_ibufsize,
1243 	    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, ucomreadcb);
1244 
1245 	if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
1246 		/* XXX: Recover from this, please! */
1247 		printf("ucomsubmitread: err=%s\n", usbd_errstr(err));
1248 		return (err);
1249 	}
1250 
1251 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
1252 
1253 	return (USBD_NORMAL_COMPLETION);
1254 }
1255 
1256 static void
1257 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1258 {
1259 	struct ucom_softc *sc = (struct ucom_softc *)p;
1260 	struct tty *tp = sc->sc_tty;
1261 	struct ucom_buffer *ub;
1262 	u_int32_t cc;
1263 	u_char *cp;
1264 	int s;
1265 
1266 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
1267 	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
1268 
1269 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
1270 	    sc->sc_dying) {
1271 		DPRINTF(("ucomreadcb: dying\n"));
1272 		ub->ub_index = ub->ub_len = 0;
1273 		/* Send something to wake upper layer */
1274 		s = spltty();
1275 		if (status != USBD_CANCELLED) {
1276 			(tp->t_linesw->l_rint)('\n', tp);
1277 			mutex_spin_enter(&tty_lock);	/* XXX */
1278 			ttwakeup(tp);
1279 			mutex_spin_exit(&tty_lock);	/* XXX */
1280 		}
1281 		splx(s);
1282 		return;
1283 	}
1284 
1285 	if (status == USBD_STALLED) {
1286 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1287 		ucomsubmitread(sc, ub);
1288 		return;
1289 	}
1290 
1291 	if (status != USBD_NORMAL_COMPLETION) {
1292 		printf("ucomreadcb: wonky status=%s\n", usbd_errstr(status));
1293 		return;
1294 	}
1295 
1296 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
1297 
1298 #ifdef UCOM_DEBUG
1299 	/* This is triggered by uslsa(4) occasionally. */
1300 	if ((ucomdebug > 0) && (cc == 0)) {
1301 		device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
1302 	}
1303 #endif
1304 
1305 	KDASSERT(cp == ub->ub_data);
1306 
1307 	rnd_add_uint32(&sc->sc_rndsource, cc);
1308 
1309 	if (sc->sc_opening) {
1310 		ucomsubmitread(sc, ub);
1311 		return;
1312 	}
1313 
1314 	if (sc->sc_methods->ucom_read != NULL) {
1315 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
1316 		    &cp, &cc);
1317 		ub->ub_index = (u_int)(cp - ub->ub_data);
1318 	} else
1319 		ub->ub_index = 0;
1320 
1321 	ub->ub_len = cc;
1322 
1323 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
1324 
1325 	ucom_read_complete(sc);
1326 }
1327 
1328 static void
1329 ucom_cleanup(struct ucom_softc *sc)
1330 {
1331 	struct ucom_buffer *ub;
1332 
1333 	DPRINTF(("ucom_cleanup: closing pipes\n"));
1334 
1335 	ucom_shutdown(sc);
1336 	if (sc->sc_bulkin_pipe != NULL) {
1337 		usbd_abort_pipe(sc->sc_bulkin_pipe);
1338 		usbd_close_pipe(sc->sc_bulkin_pipe);
1339 		sc->sc_bulkin_pipe = NULL;
1340 	}
1341 	if (sc->sc_bulkout_pipe != NULL) {
1342 		usbd_abort_pipe(sc->sc_bulkout_pipe);
1343 		usbd_close_pipe(sc->sc_bulkout_pipe);
1344 		sc->sc_bulkout_pipe = NULL;
1345 	}
1346 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS]; ub++) {
1347 		if (ub->ub_xfer != NULL) {
1348 			usbd_free_xfer(ub->ub_xfer);
1349 			ub->ub_xfer = NULL;
1350 			ub->ub_data = NULL;
1351 		}
1352 	}
1353 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS]; ub++){
1354 		if (ub->ub_xfer != NULL) {
1355 			usbd_free_xfer(ub->ub_xfer);
1356 			ub->ub_xfer = NULL;
1357 			ub->ub_data = NULL;
1358 		}
1359 	}
1360 }
1361 
1362 #endif /* NUCOM > 0 */
1363 
1364 int
1365 ucomprint(void *aux, const char *pnp)
1366 {
1367 	struct ucom_attach_args *uca = aux;
1368 
1369 	if (pnp)
1370 		aprint_normal("ucom at %s", pnp);
1371 	if (uca->portno != UCOM_UNK_PORTNO)
1372 		aprint_normal(" portno %d", uca->portno);
1373 	return (UNCONF);
1374 }
1375 
1376 int
1377 ucomsubmatch(device_t parent, cfdata_t cf,
1378 	     const int *ldesc, void *aux)
1379 {
1380 	struct ucom_attach_args *uca = aux;
1381 
1382 	if (uca->portno != UCOM_UNK_PORTNO &&
1383 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
1384 	    cf->cf_loc[UCOMBUSCF_PORTNO] != uca->portno)
1385 		return (0);
1386 	return (config_match(parent, cf, aux));
1387 }
1388