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