ucom.c revision 1.103
1/*	$NetBSD: ucom.c,v 1.103 2014/03/16 05:20:29 dholland 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.103 2014/03/16 05:20:29 dholland 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
72int 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
94struct 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
102struct 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
147dev_type_open(ucomopen);
148dev_type_close(ucomclose);
149dev_type_read(ucomread);
150dev_type_write(ucomwrite);
151dev_type_ioctl(ucomioctl);
152dev_type_stop(ucomstop);
153dev_type_tty(ucomtty);
154dev_type_poll(ucompoll);
155
156const 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
170static void	ucom_cleanup(struct ucom_softc *);
171static int	ucomparam(struct tty *, struct termios *);
172static int	ucomhwiflow(struct tty *, int);
173static void	ucomstart(struct tty *);
174static void	ucom_shutdown(struct ucom_softc *);
175static int	ucom_do_ioctl(struct ucom_softc *, u_long, void *,
176			      int, struct lwp *);
177static void	ucom_dtr(struct ucom_softc *, int);
178static void	ucom_rts(struct ucom_softc *, int);
179static void	ucom_break(struct ucom_softc *, int);
180static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
181static int	ucom_to_tiocm(struct ucom_softc *);
182
183static void	ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
184static void	ucom_submit_write(struct ucom_softc *, struct ucom_buffer *);
185static void	ucom_write_status(struct ucom_softc *, struct ucom_buffer *,
186			usbd_status);
187
188static void	ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
189static void	ucom_read_complete(struct ucom_softc *);
190static usbd_status ucomsubmitread(struct ucom_softc *, struct ucom_buffer *);
191static void	ucom_softintr(void *);
192
193int ucom_match(device_t, cfdata_t, void *);
194void ucom_attach(device_t, device_t, void *);
195int ucom_detach(device_t, int);
196int ucom_activate(device_t, enum devact);
197extern struct cfdriver ucom_cd;
198CFATTACH_DECL_NEW(ucom, sizeof(struct ucom_softc), ucom_match, ucom_attach,
199    ucom_detach, ucom_activate);
200
201int
202ucom_match(device_t parent, cfdata_t match, void *aux)
203{
204	return (1);
205}
206
207void
208ucom_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
259int
260ucom_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
328int
329ucom_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
344void
345ucom_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
360int
361ucomopen(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	/* XXX This is a hopefully temporary stopgap for kern/42848. */
372	if ((flag & (FREAD|FWRITE)) != (FREAD|FWRITE))
373		return (EINVAL);
374
375	if (sc == NULL)
376		return (ENXIO);
377
378	if (sc->sc_dying)
379		return (EIO);
380
381	if (!device_is_active(sc->sc_dev))
382		return (ENXIO);
383
384	tp = sc->sc_tty;
385
386	DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
387
388	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
389		return (EBUSY);
390
391	s = spltty();
392
393	/*
394	 * Do the following iff this is a first open.
395	 */
396	while (sc->sc_opening)
397		tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
398
399	if (sc->sc_dying) {
400		splx(s);
401		return (EIO);
402	}
403	sc->sc_opening = 1;
404
405	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
406		struct termios t;
407
408		tp->t_dev = dev;
409
410		if (sc->sc_methods->ucom_open != NULL) {
411			error = sc->sc_methods->ucom_open(sc->sc_parent,
412							  sc->sc_portno);
413			if (error) {
414				ucom_cleanup(sc);
415				sc->sc_opening = 0;
416				wakeup(&sc->sc_opening);
417				splx(s);
418				return (error);
419			}
420		}
421
422		ucom_status_change(sc);
423
424		/*
425		 * Initialize the termios status to the defaults.  Add in the
426		 * sticky bits from TIOCSFLAGS.
427		 */
428		t.c_ispeed = 0;
429		t.c_ospeed = TTYDEF_SPEED;
430		t.c_cflag = TTYDEF_CFLAG;
431		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
432			SET(t.c_cflag, CLOCAL);
433		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
434			SET(t.c_cflag, CRTSCTS);
435		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
436			SET(t.c_cflag, MDMBUF);
437		/* Make sure ucomparam() will do something. */
438		tp->t_ospeed = 0;
439		(void) ucomparam(tp, &t);
440		tp->t_iflag = TTYDEF_IFLAG;
441		tp->t_oflag = TTYDEF_OFLAG;
442		tp->t_lflag = TTYDEF_LFLAG;
443		ttychars(tp);
444		ttsetwater(tp);
445
446		/*
447		 * Turn on DTR.  We must always do this, even if carrier is not
448		 * present, because otherwise we'd have to use TIOCSDTR
449		 * immediately after setting CLOCAL, which applications do not
450		 * expect.  We always assert DTR while the device is open
451		 * unless explicitly requested to deassert it.  Ditto RTS.
452		 */
453		ucom_dtr(sc, 1);
454		ucom_rts(sc, 1);
455
456		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
457			 sc->sc_bulkin_no, sc->sc_bulkout_no));
458
459		/* Open the bulk pipes */
460		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no,
461				     USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
462		if (err) {
463			DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
464				 device_xname(sc->sc_dev), sc->sc_bulkin_no,
465				 usbd_errstr(err)));
466			error = EIO;
467			goto fail_0;
468		}
469		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
470				     USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
471		if (err) {
472			DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
473				 device_xname(sc->sc_dev), sc->sc_bulkout_no,
474				 usbd_errstr(err)));
475			error = EIO;
476			goto fail_1;
477		}
478
479		sc->sc_rx_unblock = 0;
480		sc->sc_rx_stopped = 0;
481		sc->sc_tx_stopped = 0;
482
483		memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff));
484		memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff));
485
486		SIMPLEQ_INIT(&sc->sc_ibuff_empty);
487		SIMPLEQ_INIT(&sc->sc_ibuff_full);
488		SIMPLEQ_INIT(&sc->sc_obuff_free);
489		SIMPLEQ_INIT(&sc->sc_obuff_full);
490
491		/* Allocate input buffers */
492		for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
493		    ub++) {
494			ub->ub_xfer = usbd_alloc_xfer(sc->sc_udev);
495			if (ub->ub_xfer == NULL) {
496				error = ENOMEM;
497				goto fail_2;
498			}
499			ub->ub_data = usbd_alloc_buffer(ub->ub_xfer,
500			    sc->sc_ibufsizepad);
501			if (ub->ub_data == NULL) {
502				error = ENOMEM;
503				goto fail_2;
504			}
505
506			if (ucomsubmitread(sc, ub) != USBD_NORMAL_COMPLETION) {
507				error = EIO;
508				goto fail_2;
509			}
510		}
511
512		for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
513		    ub++) {
514			ub->ub_xfer = usbd_alloc_xfer(sc->sc_udev);
515			if (ub->ub_xfer == NULL) {
516				error = ENOMEM;
517				goto fail_2;
518			}
519			ub->ub_data = usbd_alloc_buffer(ub->ub_xfer,
520			    sc->sc_obufsize);
521			if (ub->ub_data == NULL) {
522				error = ENOMEM;
523				goto fail_2;
524			}
525
526			SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
527		}
528
529	}
530	sc->sc_opening = 0;
531	wakeup(&sc->sc_opening);
532	splx(s);
533
534	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
535	if (error)
536		goto bad;
537
538	error = (*tp->t_linesw->l_open)(dev, tp);
539	if (error)
540		goto bad;
541
542	return (0);
543
544fail_2:
545	usbd_abort_pipe(sc->sc_bulkin_pipe);
546	for (i = 0; i < UCOM_IN_BUFFS; i++) {
547		if (sc->sc_ibuff[i].ub_xfer != NULL) {
548			usbd_free_xfer(sc->sc_ibuff[i].ub_xfer);
549			sc->sc_ibuff[i].ub_xfer = NULL;
550			sc->sc_ibuff[i].ub_data = NULL;
551		}
552	}
553	usbd_abort_pipe(sc->sc_bulkout_pipe);
554	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
555		if (sc->sc_obuff[i].ub_xfer != NULL) {
556			usbd_free_xfer(sc->sc_obuff[i].ub_xfer);
557			sc->sc_obuff[i].ub_xfer = NULL;
558			sc->sc_obuff[i].ub_data = NULL;
559		}
560	}
561
562	usbd_close_pipe(sc->sc_bulkout_pipe);
563	sc->sc_bulkout_pipe = NULL;
564fail_1:
565	usbd_close_pipe(sc->sc_bulkin_pipe);
566	sc->sc_bulkin_pipe = NULL;
567fail_0:
568	sc->sc_opening = 0;
569	wakeup(&sc->sc_opening);
570	splx(s);
571	return (error);
572
573bad:
574	s = spltty();
575	CLR(tp->t_state, TS_BUSY);
576	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
577		/*
578		 * We failed to open the device, and nobody else had it opened.
579		 * Clean up the state as appropriate.
580		 */
581		ucom_cleanup(sc);
582	}
583	splx(s);
584
585	return (error);
586}
587
588int
589ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
590{
591	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
592	struct tty *tp;
593	int s;
594
595	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
596
597	if (sc == NULL)
598		return 0;
599
600	tp = sc->sc_tty;
601
602	if (!ISSET(tp->t_state, TS_ISOPEN))
603		return (0);
604
605	s = spltty();
606	sc->sc_refcnt++;
607
608	(*tp->t_linesw->l_close)(tp, flag);
609	ttyclose(tp);
610
611	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
612		/*
613		 * Although we got a last close, the device may still be in
614		 * use; e.g. if this was the dialout node, and there are still
615		 * processes waiting for carrier on the non-dialout node.
616		 */
617		ucom_cleanup(sc);
618	}
619
620	if (sc->sc_methods->ucom_close != NULL)
621		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
622
623	if (--sc->sc_refcnt < 0)
624		usb_detach_wakeupold(sc->sc_dev);
625	splx(s);
626
627	return (0);
628}
629
630int
631ucomread(dev_t dev, struct uio *uio, int flag)
632{
633	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
634	struct tty *tp;
635	int error;
636
637	if (sc == NULL || sc->sc_dying)
638		return (EIO);
639
640	tp = sc->sc_tty;
641
642	sc->sc_refcnt++;
643	error = ((*tp->t_linesw->l_read)(tp, uio, flag));
644	if (--sc->sc_refcnt < 0)
645		usb_detach_wakeupold(sc->sc_dev);
646	return (error);
647}
648
649int
650ucomwrite(dev_t dev, struct uio *uio, int flag)
651{
652	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
653	struct tty *tp;
654	int error;
655
656	if (sc == NULL || sc->sc_dying)
657		return (EIO);
658
659	tp = sc->sc_tty;
660
661	sc->sc_refcnt++;
662	error = ((*tp->t_linesw->l_write)(tp, uio, flag));
663	if (--sc->sc_refcnt < 0)
664		usb_detach_wakeupold(sc->sc_dev);
665	return (error);
666}
667
668int
669ucompoll(dev_t dev, int events, struct lwp *l)
670{
671	struct ucom_softc *sc;
672	struct tty *tp;
673	int revents;
674
675	sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
676	if (sc == NULL || sc->sc_dying)
677		return (POLLHUP);
678
679	tp = sc->sc_tty;
680
681	sc->sc_refcnt++;
682	revents = ((*tp->t_linesw->l_poll)(tp, events, l));
683	if (--sc->sc_refcnt < 0)
684		usb_detach_wakeupold(sc->sc_dev);
685	return (revents);
686}
687
688struct tty *
689ucomtty(dev_t dev)
690{
691	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
692
693	return ((sc != NULL) ? sc->sc_tty : NULL);
694}
695
696int
697ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
698{
699	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
700	int error;
701
702	if (sc == NULL || sc->sc_dying)
703		return (EIO);
704
705	sc->sc_refcnt++;
706	error = ucom_do_ioctl(sc, cmd, data, flag, l);
707	if (--sc->sc_refcnt < 0)
708		usb_detach_wakeupold(sc->sc_dev);
709	return (error);
710}
711
712static int
713ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
714	      int flag, struct lwp *l)
715{
716	struct tty *tp = sc->sc_tty;
717	int error;
718	int s;
719
720	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
721
722	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
723	if (error != EPASSTHROUGH)
724		return (error);
725
726	error = ttioctl(tp, cmd, data, flag, l);
727	if (error != EPASSTHROUGH)
728		return (error);
729
730	if (sc->sc_methods->ucom_ioctl != NULL) {
731		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
732			    sc->sc_portno, cmd, data, flag, l->l_proc);
733		if (error != EPASSTHROUGH)
734			return (error);
735	}
736
737	error = 0;
738
739	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
740	s = spltty();
741
742	switch (cmd) {
743	case TIOCSBRK:
744		ucom_break(sc, 1);
745		break;
746
747	case TIOCCBRK:
748		ucom_break(sc, 0);
749		break;
750
751	case TIOCSDTR:
752		ucom_dtr(sc, 1);
753		break;
754
755	case TIOCCDTR:
756		ucom_dtr(sc, 0);
757		break;
758
759	case TIOCGFLAGS:
760		*(int *)data = sc->sc_swflags;
761		break;
762
763	case TIOCSFLAGS:
764		error = kauth_authorize_device_tty(l->l_cred,
765		    KAUTH_DEVICE_TTY_PRIVSET, tp);
766		if (error)
767			break;
768		sc->sc_swflags = *(int *)data;
769		break;
770
771	case TIOCMSET:
772	case TIOCMBIS:
773	case TIOCMBIC:
774		tiocm_to_ucom(sc, cmd, *(int *)data);
775		break;
776
777	case TIOCMGET:
778		*(int *)data = ucom_to_tiocm(sc);
779		break;
780
781	default:
782		error = EPASSTHROUGH;
783		break;
784	}
785
786	splx(s);
787
788	return (error);
789}
790
791static void
792tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
793{
794	u_char combits;
795
796	combits = 0;
797	if (ISSET(ttybits, TIOCM_DTR))
798		SET(combits, UMCR_DTR);
799	if (ISSET(ttybits, TIOCM_RTS))
800		SET(combits, UMCR_RTS);
801
802	switch (how) {
803	case TIOCMBIC:
804		CLR(sc->sc_mcr, combits);
805		break;
806
807	case TIOCMBIS:
808		SET(sc->sc_mcr, combits);
809		break;
810
811	case TIOCMSET:
812		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
813		SET(sc->sc_mcr, combits);
814		break;
815	}
816
817	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
818		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
819	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
820		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
821}
822
823static int
824ucom_to_tiocm(struct ucom_softc *sc)
825{
826	u_char combits;
827	int ttybits = 0;
828
829	combits = sc->sc_mcr;
830	if (ISSET(combits, UMCR_DTR))
831		SET(ttybits, TIOCM_DTR);
832	if (ISSET(combits, UMCR_RTS))
833		SET(ttybits, TIOCM_RTS);
834
835	combits = sc->sc_msr;
836	if (ISSET(combits, UMSR_DCD))
837		SET(ttybits, TIOCM_CD);
838	if (ISSET(combits, UMSR_CTS))
839		SET(ttybits, TIOCM_CTS);
840	if (ISSET(combits, UMSR_DSR))
841		SET(ttybits, TIOCM_DSR);
842	if (ISSET(combits, UMSR_RI | UMSR_TERI))
843		SET(ttybits, TIOCM_RI);
844
845#if 0
846XXX;
847	if (sc->sc_ier != 0)
848		SET(ttybits, TIOCM_LE);
849#endif
850
851	return (ttybits);
852}
853
854static void
855ucom_break(struct ucom_softc *sc, int onoff)
856{
857	DPRINTF(("ucom_break: onoff=%d\n", onoff));
858
859	if (sc->sc_methods->ucom_set != NULL)
860		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
861		    UCOM_SET_BREAK, onoff);
862}
863
864static void
865ucom_dtr(struct ucom_softc *sc, int onoff)
866{
867	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
868
869	if (sc->sc_methods->ucom_set != NULL)
870		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
871		    UCOM_SET_DTR, onoff);
872}
873
874static void
875ucom_rts(struct ucom_softc *sc, int onoff)
876{
877	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
878
879	if (sc->sc_methods->ucom_set != NULL)
880		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
881		    UCOM_SET_RTS, onoff);
882}
883
884void
885ucom_status_change(struct ucom_softc *sc)
886{
887	struct tty *tp = sc->sc_tty;
888	u_char old_msr;
889
890	if (sc->sc_methods->ucom_get_status != NULL) {
891		old_msr = sc->sc_msr;
892		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
893		    &sc->sc_lsr, &sc->sc_msr);
894		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD))
895			(*tp->t_linesw->l_modem)(tp,
896			    ISSET(sc->sc_msr, UMSR_DCD));
897	} else {
898		sc->sc_lsr = 0;
899		/* Assume DCD is present, if we have no chance to check it. */
900		sc->sc_msr = UMSR_DCD;
901	}
902}
903
904static int
905ucomparam(struct tty *tp, struct termios *t)
906{
907	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
908	    UCOMUNIT(tp->t_dev));
909	int error;
910
911	if (sc == NULL || sc->sc_dying)
912		return (EIO);
913
914	/* Check requested parameters. */
915	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
916		return (EINVAL);
917
918	/*
919	 * For the console, always force CLOCAL and !HUPCL, so that the port
920	 * is always active.
921	 */
922	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
923		SET(t->c_cflag, CLOCAL);
924		CLR(t->c_cflag, HUPCL);
925	}
926
927	/*
928	 * If there were no changes, don't do anything.  This avoids dropping
929	 * input and improves performance when all we did was frob things like
930	 * VMIN and VTIME.
931	 */
932	if (tp->t_ospeed == t->c_ospeed &&
933	    tp->t_cflag == t->c_cflag)
934		return (0);
935
936	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
937
938	/* And copy to tty. */
939	tp->t_ispeed = 0;
940	tp->t_ospeed = t->c_ospeed;
941	tp->t_cflag = t->c_cflag;
942
943	if (sc->sc_methods->ucom_param != NULL) {
944		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
945			    t);
946		if (error)
947			return (error);
948	}
949
950	/* XXX worry about CHWFLOW */
951
952	/*
953	 * Update the tty layer's idea of the carrier bit, in case we changed
954	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
955	 * explicit request.
956	 */
957	DPRINTF(("ucomparam: l_modem\n"));
958	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
959
960#if 0
961XXX what if the hardware is not open
962	if (!ISSET(t->c_cflag, CHWFLOW)) {
963		if (sc->sc_tx_stopped) {
964			sc->sc_tx_stopped = 0;
965			ucomstart(tp);
966		}
967	}
968#endif
969
970	return (0);
971}
972
973static int
974ucomhwiflow(struct tty *tp, int block)
975{
976	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
977	    UCOMUNIT(tp->t_dev));
978	int old;
979
980	if (sc == NULL)
981		return (0);
982
983	old = sc->sc_rx_stopped;
984	sc->sc_rx_stopped = (u_char)block;
985
986	if (old && !block) {
987		int s = splusb();
988		sc->sc_rx_unblock = 1;
989		softint_schedule(sc->sc_si);
990		splx(s);
991	}
992
993	return (1);
994}
995
996static void
997ucomstart(struct tty *tp)
998{
999	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
1000	    UCOMUNIT(tp->t_dev));
1001	struct ucom_buffer *ub;
1002	int s;
1003	u_char *data;
1004	int cnt;
1005
1006	if (sc == NULL || sc->sc_dying)
1007		return;
1008
1009	s = spltty();
1010	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1011		goto out;
1012	if (sc->sc_tx_stopped)
1013		goto out;
1014
1015	if (!ttypull(tp))
1016		goto out;
1017
1018	/* Grab the first contiguous region of buffer space. */
1019	data = tp->t_outq.c_cf;
1020	cnt = ndqb(&tp->t_outq, 0);
1021
1022	if (cnt == 0)
1023		goto out;
1024
1025	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
1026	if (ub == NULL) {
1027		SET(tp->t_state, TS_BUSY);
1028		goto out;
1029	}
1030	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
1031
1032	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
1033		SET(tp->t_state, TS_BUSY);
1034
1035	if (cnt > sc->sc_obufsize)
1036		cnt = sc->sc_obufsize;
1037
1038	if (sc->sc_methods->ucom_write != NULL)
1039		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
1040					   ub->ub_data, data, &cnt);
1041	else
1042		memcpy(ub->ub_data, data, cnt);
1043
1044	ub->ub_len = cnt;
1045	ub->ub_index = 0;
1046
1047	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
1048
1049	softint_schedule(sc->sc_si);
1050
1051 out:
1052	splx(s);
1053}
1054
1055void
1056ucomstop(struct tty *tp, int flag)
1057{
1058#if 0
1059	/*struct ucom_softc *sc =
1060	    device_lookup_private(&ucom_cd, UCOMUNIT(tp->t_dev));*/
1061	int s;
1062
1063	s = spltty();
1064	if (ISSET(tp->t_state, TS_BUSY)) {
1065		/* sc->sc_tx_stopped = 1; */
1066		if (!ISSET(tp->t_state, TS_TTSTOP))
1067			SET(tp->t_state, TS_FLUSH);
1068	}
1069	splx(s);
1070#endif
1071}
1072
1073static void
1074ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
1075    usbd_status err)
1076{
1077	struct tty *tp = sc->sc_tty;
1078	uint32_t cc = ub->ub_len;
1079
1080	switch (err) {
1081	case USBD_IN_PROGRESS:
1082		ub->ub_index = ub->ub_len;
1083		break;
1084	case USBD_STALLED:
1085		ub->ub_index = 0;
1086		softint_schedule(sc->sc_si);
1087		break;
1088	case USBD_NORMAL_COMPLETION:
1089		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
1090		rnd_add_uint32(&sc->sc_rndsource, cc);
1091		/*FALLTHROUGH*/
1092	default:
1093		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
1094		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
1095		cc -= sc->sc_opkthdrlen;
1096
1097		mutex_spin_enter(&tty_lock);
1098		CLR(tp->t_state, TS_BUSY);
1099		if (ISSET(tp->t_state, TS_FLUSH))
1100			CLR(tp->t_state, TS_FLUSH);
1101		else
1102			ndflush(&tp->t_outq, cc);
1103		mutex_spin_exit(&tty_lock);
1104
1105		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
1106		    !sc->sc_dying) {
1107			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
1108				ucom_submit_write(sc, ub);
1109
1110			(*tp->t_linesw->l_start)(tp);
1111		}
1112		break;
1113	}
1114}
1115
1116/* Call at spltty() */
1117static void
1118ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
1119{
1120
1121	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkout_pipe,
1122	    (usbd_private_handle)sc, ub->ub_data, ub->ub_len,
1123	    USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
1124
1125	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
1126}
1127
1128static void
1129ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1130{
1131	struct ucom_softc *sc = (struct ucom_softc *)p;
1132	int s;
1133
1134	s = spltty();
1135
1136	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
1137
1138	splx(s);
1139}
1140
1141static void
1142ucom_softintr(void *arg)
1143{
1144	struct ucom_softc *sc = arg;
1145	struct tty *tp = sc->sc_tty;
1146	struct ucom_buffer *ub;
1147	int s;
1148
1149	if (!ISSET(tp->t_state, TS_ISOPEN))
1150		return;
1151
1152	s = spltty();
1153
1154	ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
1155
1156	if (ub != NULL && ub->ub_index == 0)
1157		ucom_submit_write(sc, ub);
1158
1159	if (sc->sc_rx_unblock)
1160		ucom_read_complete(sc);
1161
1162	splx(s);
1163}
1164
1165static void
1166ucom_read_complete(struct ucom_softc *sc)
1167{
1168	int (*rint)(int, struct tty *);
1169	struct ucom_buffer *ub;
1170	struct tty *tp;
1171	int s;
1172
1173	tp = sc->sc_tty;
1174	rint = tp->t_linesw->l_rint;
1175	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
1176
1177	while (ub != NULL && !sc->sc_rx_stopped) {
1178
1179		s = spltty();
1180
1181		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
1182			/* Give characters to tty layer. */
1183			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
1184				/* Overflow: drop remainder */
1185				ub->ub_index = ub->ub_len;
1186			} else
1187				ub->ub_index++;
1188		}
1189
1190		splx(s);
1191
1192		if (ub->ub_index == ub->ub_len) {
1193			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
1194
1195			ucomsubmitread(sc, ub);
1196
1197			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
1198		}
1199	}
1200
1201	sc->sc_rx_unblock = (ub != NULL);
1202}
1203
1204static usbd_status
1205ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
1206{
1207	usbd_status err;
1208
1209	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkin_pipe,
1210	    (usbd_private_handle)sc, ub->ub_data, sc->sc_ibufsize,
1211	    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, ucomreadcb);
1212
1213	if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
1214		/* XXX: Recover from this, please! */
1215		printf("ucomsubmitread: err=%s\n", usbd_errstr(err));
1216		return (err);
1217	}
1218
1219	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
1220
1221	return (USBD_NORMAL_COMPLETION);
1222}
1223
1224static void
1225ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1226{
1227	struct ucom_softc *sc = (struct ucom_softc *)p;
1228	struct tty *tp = sc->sc_tty;
1229	struct ucom_buffer *ub;
1230	u_int32_t cc;
1231	u_char *cp;
1232	int s;
1233
1234	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
1235	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
1236
1237	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
1238	    sc->sc_dying) {
1239		DPRINTF(("ucomreadcb: dying\n"));
1240		ub->ub_index = ub->ub_len = 0;
1241		/* Send something to wake upper layer */
1242		s = spltty();
1243		if (status != USBD_CANCELLED) {
1244			(tp->t_linesw->l_rint)('\n', tp);
1245			mutex_spin_enter(&tty_lock);	/* XXX */
1246			ttwakeup(tp);
1247			mutex_spin_exit(&tty_lock);	/* XXX */
1248		}
1249		splx(s);
1250		return;
1251	}
1252
1253	if (status == USBD_STALLED) {
1254		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1255		ucomsubmitread(sc, ub);
1256		return;
1257	}
1258
1259	if (status != USBD_NORMAL_COMPLETION) {
1260		printf("ucomreadcb: wonky status=%s\n", usbd_errstr(status));
1261		return;
1262	}
1263
1264	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
1265
1266#ifdef UCOM_DEBUG
1267	/* This is triggered by uslsa(4) occasionally. */
1268	if ((ucomdebug > 0) && (cc == 0)) {
1269		device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
1270	}
1271#endif
1272
1273	KDASSERT(cp == ub->ub_data);
1274
1275	rnd_add_uint32(&sc->sc_rndsource, cc);
1276
1277	if (sc->sc_opening) {
1278		ucomsubmitread(sc, ub);
1279		return;
1280	}
1281
1282	if (sc->sc_methods->ucom_read != NULL) {
1283		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
1284		    &cp, &cc);
1285		ub->ub_index = (u_int)(cp - ub->ub_data);
1286	} else
1287		ub->ub_index = 0;
1288
1289	ub->ub_len = cc;
1290
1291	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
1292
1293	ucom_read_complete(sc);
1294}
1295
1296static void
1297ucom_cleanup(struct ucom_softc *sc)
1298{
1299	struct ucom_buffer *ub;
1300
1301	DPRINTF(("ucom_cleanup: closing pipes\n"));
1302
1303	ucom_shutdown(sc);
1304	if (sc->sc_bulkin_pipe != NULL) {
1305		usbd_abort_pipe(sc->sc_bulkin_pipe);
1306		usbd_close_pipe(sc->sc_bulkin_pipe);
1307		sc->sc_bulkin_pipe = NULL;
1308	}
1309	if (sc->sc_bulkout_pipe != NULL) {
1310		usbd_abort_pipe(sc->sc_bulkout_pipe);
1311		usbd_close_pipe(sc->sc_bulkout_pipe);
1312		sc->sc_bulkout_pipe = NULL;
1313	}
1314	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS]; ub++) {
1315		if (ub->ub_xfer != NULL) {
1316			usbd_free_xfer(ub->ub_xfer);
1317			ub->ub_xfer = NULL;
1318			ub->ub_data = NULL;
1319		}
1320	}
1321	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS]; ub++){
1322		if (ub->ub_xfer != NULL) {
1323			usbd_free_xfer(ub->ub_xfer);
1324			ub->ub_xfer = NULL;
1325			ub->ub_data = NULL;
1326		}
1327	}
1328}
1329
1330#endif /* NUCOM > 0 */
1331
1332int
1333ucomprint(void *aux, const char *pnp)
1334{
1335	struct ucom_attach_args *uca = aux;
1336
1337	if (pnp)
1338		aprint_normal("ucom at %s", pnp);
1339	if (uca->portno != UCOM_UNK_PORTNO)
1340		aprint_normal(" portno %d", uca->portno);
1341	return (UNCONF);
1342}
1343
1344int
1345ucomsubmatch(device_t parent, cfdata_t cf,
1346	     const int *ldesc, void *aux)
1347{
1348	struct ucom_attach_args *uca = aux;
1349
1350	if (uca->portno != UCOM_UNK_PORTNO &&
1351	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
1352	    cf->cf_loc[UCOMBUSCF_PORTNO] != uca->portno)
1353		return (0);
1354	return (config_match(parent, cf, aux));
1355}
1356