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