ucom.c revision 1.71
1/*	$NetBSD: ucom.c,v 1.71 2007/11/10 18:29:37 ad 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 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *        This product includes software developed by the NetBSD
22 *        Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39/*
40 * This code is very heavily based on the 16550 driver, com.c.
41 */
42
43#include <sys/cdefs.h>
44__KERNEL_RCSID(0, "$NetBSD: ucom.c,v 1.71 2007/11/10 18:29:37 ad Exp $");
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/ioctl.h>
50#include <sys/conf.h>
51#include <sys/tty.h>
52#include <sys/file.h>
53#include <sys/select.h>
54#include <sys/proc.h>
55#include <sys/vnode.h>
56#include <sys/device.h>
57#include <sys/poll.h>
58#include <sys/kauth.h>
59#if defined(__NetBSD__)
60#include "rnd.h"
61#if NRND > 0
62#include <sys/rnd.h>
63#endif
64#endif
65
66#include <dev/usb/usb.h>
67
68#include <dev/usb/usbdi.h>
69#include <dev/usb/usbdi_util.h>
70#include <dev/usb/usbdevs.h>
71#include <dev/usb/usb_quirks.h>
72
73#include <dev/usb/ucomvar.h>
74
75#include "ucom.h"
76
77#include "locators.h"
78
79#if NUCOM > 0
80
81#ifdef UCOM_DEBUG
82#define DPRINTFN(n, x)	if (ucomdebug > (n)) logprintf x
83int ucomdebug = 0;
84#else
85#define DPRINTFN(n, x)
86#endif
87#define DPRINTF(x) DPRINTFN(0, x)
88
89#define	UCOMUNIT_MASK		0x3ffff
90#define	UCOMDIALOUT_MASK	0x80000
91#define	UCOMCALLUNIT_MASK	0x40000
92
93#define	UCOMUNIT(x)		(minor(x) & UCOMUNIT_MASK)
94#define	UCOMDIALOUT(x)		(minor(x) & UCOMDIALOUT_MASK)
95#define	UCOMCALLUNIT(x)		(minor(x) & UCOMCALLUNIT_MASK)
96
97struct ucom_softc {
98	USBBASEDEVICE		sc_dev;		/* base device */
99
100	usbd_device_handle	sc_udev;	/* USB device */
101
102	usbd_interface_handle	sc_iface;	/* data interface */
103
104	int			sc_bulkin_no;	/* bulk in endpoint address */
105	usbd_pipe_handle	sc_bulkin_pipe;	/* bulk in pipe */
106	usbd_xfer_handle	sc_ixfer;	/* read request */
107	u_char			*sc_ibuf;	/* read buffer */
108	u_int			sc_ibufsize;	/* read buffer size */
109	u_int			sc_ibufsizepad;	/* read buffer size padded */
110
111	int			sc_bulkout_no;	/* bulk out endpoint address */
112	usbd_pipe_handle	sc_bulkout_pipe;/* bulk out pipe */
113	usbd_xfer_handle	sc_oxfer;	/* write request */
114	u_char			*sc_obuf;	/* write buffer */
115	u_int			sc_obufsize;	/* write buffer size */
116	u_int			sc_opkthdrlen;	/* header length of
117						 * output packet */
118
119	struct ucom_methods     *sc_methods;
120	void                    *sc_parent;
121	int			sc_portno;
122
123	struct tty		*sc_tty;	/* our tty */
124	u_char			sc_lsr;
125	u_char			sc_msr;
126	u_char			sc_mcr;
127	u_char			sc_tx_stopped;
128	int			sc_swflags;
129
130	u_char			sc_opening;	/* lock during open */
131	int			sc_refcnt;
132	u_char			sc_dying;	/* disconnecting */
133
134#if defined(__NetBSD__) && NRND > 0
135	rndsource_element_t	sc_rndsource;	/* random source */
136#endif
137};
138
139dev_type_open(ucomopen);
140dev_type_close(ucomclose);
141dev_type_read(ucomread);
142dev_type_write(ucomwrite);
143dev_type_ioctl(ucomioctl);
144dev_type_stop(ucomstop);
145dev_type_tty(ucomtty);
146dev_type_poll(ucompoll);
147
148const struct cdevsw ucom_cdevsw = {
149	ucomopen, ucomclose, ucomread, ucomwrite, ucomioctl,
150	ucomstop, ucomtty, ucompoll, nommap, ttykqfilter, D_TTY
151};
152
153Static void	ucom_cleanup(struct ucom_softc *);
154Static void	ucom_hwiflow(struct ucom_softc *);
155Static int	ucomparam(struct tty *, struct termios *);
156Static void	ucomstart(struct tty *);
157Static void	ucom_shutdown(struct ucom_softc *);
158Static int	ucom_do_ioctl(struct ucom_softc *, u_long, void *,
159			      int, struct lwp *);
160Static void	ucom_dtr(struct ucom_softc *, int);
161Static void	ucom_rts(struct ucom_softc *, int);
162Static void	ucom_break(struct ucom_softc *, int);
163Static usbd_status ucomstartread(struct ucom_softc *);
164Static void	ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
165Static void	ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
166Static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
167Static int	ucom_to_tiocm(struct ucom_softc *);
168
169USB_DECLARE_DRIVER(ucom);
170
171USB_MATCH(ucom)
172{
173	return (1);
174}
175
176USB_ATTACH(ucom)
177{
178	struct ucom_softc *sc = (struct ucom_softc *)self;
179	struct ucom_attach_args *uca = aux;
180	struct tty *tp;
181
182	if (uca->info != NULL)
183		printf(": %s", uca->info);
184	printf("\n");
185
186	sc->sc_udev = uca->device;
187	sc->sc_iface = uca->iface;
188	sc->sc_bulkout_no = uca->bulkout;
189	sc->sc_bulkin_no = uca->bulkin;
190	sc->sc_ibufsize = uca->ibufsize;
191	sc->sc_ibufsizepad = uca->ibufsizepad;
192	sc->sc_obufsize = uca->obufsize;
193	sc->sc_opkthdrlen = uca->opkthdrlen;
194	sc->sc_methods = uca->methods;
195	sc->sc_parent = uca->arg;
196	sc->sc_portno = uca->portno;
197
198	tp = ttymalloc();
199	tp->t_oproc = ucomstart;
200	tp->t_param = ucomparam;
201	sc->sc_tty = tp;
202
203	DPRINTF(("ucom_attach: tty_attach %p\n", tp));
204	tty_attach(tp);
205
206#if defined(__NetBSD__) && NRND > 0
207	rnd_attach_source(&sc->sc_rndsource, USBDEVNAME(sc->sc_dev),
208			  RND_TYPE_TTY, 0);
209#endif
210
211	USB_ATTACH_SUCCESS_RETURN;
212}
213
214USB_DETACH(ucom)
215{
216	struct ucom_softc *sc = (struct ucom_softc *)self;
217	struct tty *tp = sc->sc_tty;
218	int maj, mn;
219	int s;
220
221	DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p, pipe=%d,%d\n",
222		 sc, flags, tp, sc->sc_bulkin_no, sc->sc_bulkout_no));
223
224	sc->sc_dying = 1;
225
226	if (sc->sc_bulkin_pipe != NULL)
227		usbd_abort_pipe(sc->sc_bulkin_pipe);
228	if (sc->sc_bulkout_pipe != NULL)
229		usbd_abort_pipe(sc->sc_bulkout_pipe);
230
231	s = splusb();
232	if (--sc->sc_refcnt >= 0) {
233		/* Wake up anyone waiting */
234		if (tp != NULL) {
235			mutex_spin_enter(&tty_lock);
236			CLR(tp->t_state, TS_CARR_ON);
237			CLR(tp->t_cflag, CLOCAL | MDMBUF);
238			ttyflush(tp, FREAD|FWRITE);
239			mutex_spin_exit(&tty_lock);
240		}
241		/* Wait for processes to go away. */
242		usb_detach_wait(USBDEV(sc->sc_dev));
243	}
244	splx(s);
245
246	/* locate the major number */
247	maj = cdevsw_lookup_major(&ucom_cdevsw);
248
249	/* Nuke the vnodes for any open instances. */
250	mn = device_unit(self);
251	DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
252	vdevgone(maj, mn, mn, VCHR);
253	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
254	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
255
256	/* Detach and free the tty. */
257	if (tp != NULL) {
258		tty_detach(tp);
259		ttyfree(tp);
260		sc->sc_tty = NULL;
261	}
262
263	/* Detach the random source */
264#if defined(__NetBSD__) && NRND > 0
265	rnd_detach_source(&sc->sc_rndsource);
266#endif
267
268	return (0);
269}
270
271int
272ucom_activate(device_ptr_t self, enum devact act)
273{
274	struct ucom_softc *sc = (struct ucom_softc *)self;
275
276	DPRINTFN(5,("ucom_activate: %d\n", act));
277
278	switch (act) {
279	case DVACT_ACTIVATE:
280		return (EOPNOTSUPP);
281
282	case DVACT_DEACTIVATE:
283		sc->sc_dying = 1;
284		break;
285	}
286	return (0);
287}
288
289void
290ucom_shutdown(struct ucom_softc *sc)
291{
292	struct tty *tp = sc->sc_tty;
293
294	DPRINTF(("ucom_shutdown\n"));
295	/*
296	 * Hang up if necessary.  Wait a bit, so the other side has time to
297	 * notice even if we immediately open the port again.
298	 */
299	if (ISSET(tp->t_cflag, HUPCL)) {
300		ucom_dtr(sc, 0);
301		(void)tsleep(sc, TTIPRI, ttclos, hz);
302	}
303}
304
305int
306ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
307{
308	int unit = UCOMUNIT(dev);
309	usbd_status err;
310	struct ucom_softc *sc;
311	struct tty *tp;
312	int s;
313	int error;
314
315	if (unit >= ucom_cd.cd_ndevs)
316		return (ENXIO);
317	sc = ucom_cd.cd_devs[unit];
318	if (sc == NULL)
319		return (ENXIO);
320
321	if (sc->sc_dying)
322		return (EIO);
323
324	if (!device_is_active(&sc->sc_dev))
325		return (ENXIO);
326
327	tp = sc->sc_tty;
328
329	DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
330
331	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
332		return (EBUSY);
333
334	s = spltty();
335
336	/*
337	 * Do the following iff this is a first open.
338	 */
339	while (sc->sc_opening)
340		tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
341
342	if (sc->sc_dying) {
343		splx(s);
344		return (EIO);
345	}
346	sc->sc_opening = 1;
347
348	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
349		struct termios t;
350
351		tp->t_dev = dev;
352
353		if (sc->sc_methods->ucom_open != NULL) {
354			error = sc->sc_methods->ucom_open(sc->sc_parent,
355							  sc->sc_portno);
356			if (error) {
357				ucom_cleanup(sc);
358				sc->sc_opening = 0;
359				wakeup(&sc->sc_opening);
360				splx(s);
361				return (error);
362			}
363		}
364
365		ucom_status_change(sc);
366
367		/*
368		 * Initialize the termios status to the defaults.  Add in the
369		 * sticky bits from TIOCSFLAGS.
370		 */
371		t.c_ispeed = 0;
372		t.c_ospeed = TTYDEF_SPEED;
373		t.c_cflag = TTYDEF_CFLAG;
374		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
375			SET(t.c_cflag, CLOCAL);
376		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
377			SET(t.c_cflag, CRTSCTS);
378		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
379			SET(t.c_cflag, MDMBUF);
380		/* Make sure ucomparam() will do something. */
381		tp->t_ospeed = 0;
382		(void) ucomparam(tp, &t);
383		tp->t_iflag = TTYDEF_IFLAG;
384		tp->t_oflag = TTYDEF_OFLAG;
385		tp->t_lflag = TTYDEF_LFLAG;
386		ttychars(tp);
387		ttsetwater(tp);
388
389		/*
390		 * Turn on DTR.  We must always do this, even if carrier is not
391		 * present, because otherwise we'd have to use TIOCSDTR
392		 * immediately after setting CLOCAL, which applications do not
393		 * expect.  We always assert DTR while the device is open
394		 * unless explicitly requested to deassert it.  Ditto RTS.
395		 */
396		ucom_dtr(sc, 1);
397		ucom_rts(sc, 1);
398
399		/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
400		ucom_hwiflow(sc);
401
402		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
403			 sc->sc_bulkin_no, sc->sc_bulkout_no));
404
405		/* Open the bulk pipes */
406		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
407				     &sc->sc_bulkin_pipe);
408		if (err) {
409			DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
410				 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
411				 usbd_errstr(err)));
412			error = EIO;
413			goto fail_0;
414		}
415		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
416				     USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
417		if (err) {
418			DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
419				 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
420				 usbd_errstr(err)));
421			error = EIO;
422			goto fail_1;
423		}
424
425		/* Allocate a request and an input buffer and start reading. */
426		sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
427		if (sc->sc_ixfer == NULL) {
428			error = ENOMEM;
429			goto fail_2;
430		}
431
432		sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
433						sc->sc_ibufsizepad);
434		if (sc->sc_ibuf == NULL) {
435			error = ENOMEM;
436			goto fail_3;
437		}
438
439		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
440		if (sc->sc_oxfer == NULL) {
441			error = ENOMEM;
442			goto fail_3;
443		}
444
445		sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
446						sc->sc_obufsize +
447						sc->sc_opkthdrlen);
448		if (sc->sc_obuf == NULL) {
449			error = ENOMEM;
450			goto fail_4;
451		}
452
453		ucomstartread(sc);
454	}
455	sc->sc_opening = 0;
456	wakeup(&sc->sc_opening);
457	splx(s);
458
459	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
460	if (error)
461		goto bad;
462
463	error = (*tp->t_linesw->l_open)(dev, tp);
464	if (error)
465		goto bad;
466
467	return (0);
468
469fail_4:
470	usbd_free_xfer(sc->sc_oxfer);
471	sc->sc_oxfer = NULL;
472fail_3:
473	usbd_free_xfer(sc->sc_ixfer);
474	sc->sc_ixfer = NULL;
475fail_2:
476	usbd_close_pipe(sc->sc_bulkout_pipe);
477	sc->sc_bulkout_pipe = NULL;
478fail_1:
479	usbd_close_pipe(sc->sc_bulkin_pipe);
480	sc->sc_bulkin_pipe = NULL;
481fail_0:
482	sc->sc_opening = 0;
483	wakeup(&sc->sc_opening);
484	splx(s);
485	return (error);
486
487bad:
488	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
489		/*
490		 * We failed to open the device, and nobody else had it opened.
491		 * Clean up the state as appropriate.
492		 */
493		ucom_cleanup(sc);
494	}
495
496	return (error);
497}
498
499int
500ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
501{
502	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
503	struct tty *tp = sc->sc_tty;
504
505	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
506	if (!ISSET(tp->t_state, TS_ISOPEN))
507		return (0);
508
509	sc->sc_refcnt++;
510
511	(*tp->t_linesw->l_close)(tp, flag);
512	ttyclose(tp);
513
514	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
515		/*
516		 * Although we got a last close, the device may still be in
517		 * use; e.g. if this was the dialout node, and there are still
518		 * processes waiting for carrier on the non-dialout node.
519		 */
520		ucom_cleanup(sc);
521	}
522
523	if (sc->sc_methods->ucom_close != NULL)
524		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
525
526	if (--sc->sc_refcnt < 0)
527		usb_detach_wakeup(USBDEV(sc->sc_dev));
528
529	return (0);
530}
531
532int
533ucomread(dev_t dev, struct uio *uio, int flag)
534{
535	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
536	struct tty *tp = sc->sc_tty;
537	int error;
538
539	if (sc->sc_dying)
540		return (EIO);
541
542	sc->sc_refcnt++;
543	error = ((*tp->t_linesw->l_read)(tp, uio, flag));
544	if (--sc->sc_refcnt < 0)
545		usb_detach_wakeup(USBDEV(sc->sc_dev));
546	return (error);
547}
548
549int
550ucomwrite(dev_t dev, struct uio *uio, int flag)
551{
552	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
553	struct tty *tp = sc->sc_tty;
554	int error;
555
556	if (sc->sc_dying)
557		return (EIO);
558
559	sc->sc_refcnt++;
560	error = ((*tp->t_linesw->l_write)(tp, uio, flag));
561	if (--sc->sc_refcnt < 0)
562		usb_detach_wakeup(USBDEV(sc->sc_dev));
563	return (error);
564}
565
566int
567ucompoll(dev_t dev, int events, struct lwp *l)
568{
569	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
570	struct tty *tp = sc->sc_tty;
571	int revents;
572
573	if (sc->sc_dying)
574		return (POLLHUP);
575
576	sc->sc_refcnt++;
577	revents = ((*tp->t_linesw->l_poll)(tp, events, l));
578	if (--sc->sc_refcnt < 0)
579		usb_detach_wakeup(USBDEV(sc->sc_dev));
580	return (revents);
581}
582
583struct tty *
584ucomtty(dev_t dev)
585{
586	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
587	struct tty *tp = sc->sc_tty;
588
589	return (tp);
590}
591
592int
593ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
594{
595	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
596	int error;
597
598	sc->sc_refcnt++;
599	error = ucom_do_ioctl(sc, cmd, data, flag, l);
600	if (--sc->sc_refcnt < 0)
601		usb_detach_wakeup(USBDEV(sc->sc_dev));
602	return (error);
603}
604
605Static int
606ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
607	      int flag, struct lwp *l)
608{
609	struct tty *tp = sc->sc_tty;
610	int error;
611	int s;
612
613	if (sc->sc_dying)
614		return (EIO);
615
616	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
617
618	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
619	if (error != EPASSTHROUGH)
620		return (error);
621
622	error = ttioctl(tp, cmd, data, flag, l);
623	if (error != EPASSTHROUGH)
624		return (error);
625
626	if (sc->sc_methods->ucom_ioctl != NULL) {
627		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
628			    sc->sc_portno, cmd, data, flag, l->l_proc);
629		if (error != EPASSTHROUGH)
630			return (error);
631	}
632
633	error = 0;
634
635	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
636	s = spltty();
637
638	switch (cmd) {
639	case TIOCSBRK:
640		ucom_break(sc, 1);
641		break;
642
643	case TIOCCBRK:
644		ucom_break(sc, 0);
645		break;
646
647	case TIOCSDTR:
648		ucom_dtr(sc, 1);
649		break;
650
651	case TIOCCDTR:
652		ucom_dtr(sc, 0);
653		break;
654
655	case TIOCGFLAGS:
656		*(int *)data = sc->sc_swflags;
657		break;
658
659	case TIOCSFLAGS:
660		error = kauth_authorize_device_tty(l->l_cred,
661		    KAUTH_DEVICE_TTY_PRIVSET, tp);
662		if (error)
663			break;
664		sc->sc_swflags = *(int *)data;
665		break;
666
667	case TIOCMSET:
668	case TIOCMBIS:
669	case TIOCMBIC:
670		tiocm_to_ucom(sc, cmd, *(int *)data);
671		break;
672
673	case TIOCMGET:
674		*(int *)data = ucom_to_tiocm(sc);
675		break;
676
677	default:
678		error = EPASSTHROUGH;
679		break;
680	}
681
682	splx(s);
683
684	return (error);
685}
686
687Static void
688tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
689{
690	u_char combits;
691
692	combits = 0;
693	if (ISSET(ttybits, TIOCM_DTR))
694		SET(combits, UMCR_DTR);
695	if (ISSET(ttybits, TIOCM_RTS))
696		SET(combits, UMCR_RTS);
697
698	switch (how) {
699	case TIOCMBIC:
700		CLR(sc->sc_mcr, combits);
701		break;
702
703	case TIOCMBIS:
704		SET(sc->sc_mcr, combits);
705		break;
706
707	case TIOCMSET:
708		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
709		SET(sc->sc_mcr, combits);
710		break;
711	}
712
713	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
714		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
715	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
716		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
717}
718
719Static int
720ucom_to_tiocm(struct ucom_softc *sc)
721{
722	u_char combits;
723	int ttybits = 0;
724
725	combits = sc->sc_mcr;
726	if (ISSET(combits, UMCR_DTR))
727		SET(ttybits, TIOCM_DTR);
728	if (ISSET(combits, UMCR_RTS))
729		SET(ttybits, TIOCM_RTS);
730
731	combits = sc->sc_msr;
732	if (ISSET(combits, UMSR_DCD))
733		SET(ttybits, TIOCM_CD);
734	if (ISSET(combits, UMSR_CTS))
735		SET(ttybits, TIOCM_CTS);
736	if (ISSET(combits, UMSR_DSR))
737		SET(ttybits, TIOCM_DSR);
738	if (ISSET(combits, UMSR_RI | UMSR_TERI))
739		SET(ttybits, TIOCM_RI);
740
741#if 0
742XXX;
743	if (sc->sc_ier != 0)
744		SET(ttybits, TIOCM_LE);
745#endif
746
747	return (ttybits);
748}
749
750Static void
751ucom_break(sc, onoff)
752	struct ucom_softc *sc;
753	int onoff;
754{
755	DPRINTF(("ucom_break: onoff=%d\n", onoff));
756
757	if (sc->sc_methods->ucom_set != NULL)
758		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
759		    UCOM_SET_BREAK, onoff);
760}
761
762Static void
763ucom_dtr(struct ucom_softc *sc, int onoff)
764{
765	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
766
767	if (sc->sc_methods->ucom_set != NULL)
768		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
769		    UCOM_SET_DTR, onoff);
770}
771
772Static void
773ucom_rts(struct ucom_softc *sc, int onoff)
774{
775	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
776
777	if (sc->sc_methods->ucom_set != NULL)
778		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
779		    UCOM_SET_RTS, onoff);
780}
781
782void
783ucom_status_change(struct ucom_softc *sc)
784{
785	struct tty *tp = sc->sc_tty;
786	u_char old_msr;
787
788	if (sc->sc_methods->ucom_get_status != NULL) {
789		old_msr = sc->sc_msr;
790		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
791		    &sc->sc_lsr, &sc->sc_msr);
792		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD))
793			(*tp->t_linesw->l_modem)(tp,
794			    ISSET(sc->sc_msr, UMSR_DCD));
795	} else {
796		sc->sc_lsr = 0;
797		/* Assume DCD is present, if we have no chance to check it. */
798		sc->sc_msr = UMSR_DCD;
799	}
800}
801
802Static int
803ucomparam(struct tty *tp, struct termios *t)
804{
805	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
806	int error;
807
808	if (sc->sc_dying)
809		return (EIO);
810
811	/* Check requested parameters. */
812	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
813		return (EINVAL);
814
815	/*
816	 * For the console, always force CLOCAL and !HUPCL, so that the port
817	 * is always active.
818	 */
819	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
820		SET(t->c_cflag, CLOCAL);
821		CLR(t->c_cflag, HUPCL);
822	}
823
824	/*
825	 * If there were no changes, don't do anything.  This avoids dropping
826	 * input and improves performance when all we did was frob things like
827	 * VMIN and VTIME.
828	 */
829	if (tp->t_ospeed == t->c_ospeed &&
830	    tp->t_cflag == t->c_cflag)
831		return (0);
832
833	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
834
835	/* And copy to tty. */
836	tp->t_ispeed = 0;
837	tp->t_ospeed = t->c_ospeed;
838	tp->t_cflag = t->c_cflag;
839
840	if (sc->sc_methods->ucom_param != NULL) {
841		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
842			    t);
843		if (error)
844			return (error);
845	}
846
847	/* XXX worry about CHWFLOW */
848
849	/*
850	 * Update the tty layer's idea of the carrier bit, in case we changed
851	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
852	 * explicit request.
853	 */
854	DPRINTF(("ucomparam: l_modem\n"));
855	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
856
857#if 0
858XXX what if the hardware is not open
859	if (!ISSET(t->c_cflag, CHWFLOW)) {
860		if (sc->sc_tx_stopped) {
861			sc->sc_tx_stopped = 0;
862			ucomstart(tp);
863		}
864	}
865#endif
866
867	return (0);
868}
869
870/*
871 * (un)block input via hw flowcontrol
872 */
873Static void
874ucom_hwiflow(struct ucom_softc *sc)
875{
876	DPRINTF(("ucom_hwiflow:\n"));
877#if 0
878XXX
879	bus_space_tag_t iot = sc->sc_iot;
880	bus_space_handle_t ioh = sc->sc_ioh;
881
882	if (sc->sc_mcr_rts == 0)
883		return;
884
885	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
886		CLR(sc->sc_mcr, sc->sc_mcr_rts);
887		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
888	} else {
889		SET(sc->sc_mcr, sc->sc_mcr_rts);
890		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
891	}
892	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
893#endif
894}
895
896Static void
897ucomstart(struct tty *tp)
898{
899	struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
900	usbd_status err;
901	int s;
902	u_char *data;
903	int cnt;
904
905	if (sc->sc_dying)
906		return;
907
908	s = spltty();
909	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
910		DPRINTFN(4,("ucomstart: no go, state=0x%x\n", tp->t_state));
911		goto out;
912	}
913	if (sc->sc_tx_stopped)
914		goto out;
915
916	if (tp->t_outq.c_cc <= tp->t_lowat) {
917		if (ISSET(tp->t_state, TS_ASLEEP)) {
918			CLR(tp->t_state, TS_ASLEEP);
919			wakeup(&tp->t_outq);
920		}
921		selwakeup(&tp->t_wsel);
922		if (tp->t_outq.c_cc == 0)
923			goto out;
924	}
925
926	/* Grab the first contiguous region of buffer space. */
927	data = tp->t_outq.c_cf;
928	cnt = ndqb(&tp->t_outq, 0);
929
930	if (cnt == 0) {
931		DPRINTF(("ucomstart: cnt==0\n"));
932		goto out;
933	}
934
935	SET(tp->t_state, TS_BUSY);
936
937	if (cnt > sc->sc_obufsize) {
938		DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
939		cnt = sc->sc_obufsize;
940	}
941	if (sc->sc_methods->ucom_write != NULL)
942		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
943					   sc->sc_obuf, data, &cnt);
944	else
945		memcpy(sc->sc_obuf, data, cnt);
946
947	DPRINTFN(4,("ucomstart: %d chars\n", cnt));
948	usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
949			(usbd_private_handle)sc, sc->sc_obuf, cnt,
950			USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
951	/* What can we do on error? */
952	err = usbd_transfer(sc->sc_oxfer);
953#ifdef DIAGNOSTIC
954	if (err != USBD_IN_PROGRESS)
955		printf("ucomstart: err=%s\n", usbd_errstr(err));
956#endif
957
958out:
959	splx(s);
960}
961
962void
963ucomstop(struct tty *tp, int flag)
964{
965	DPRINTF(("ucomstop: flag=%d\n", flag));
966#if 0
967	/*struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];*/
968	int s;
969
970	s = spltty();
971	if (ISSET(tp->t_state, TS_BUSY)) {
972		DPRINTF(("ucomstop: XXX\n"));
973		/* sc->sc_tx_stopped = 1; */
974		if (!ISSET(tp->t_state, TS_TTSTOP))
975			SET(tp->t_state, TS_FLUSH);
976	}
977	splx(s);
978#endif
979}
980
981Static void
982ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
983{
984	struct ucom_softc *sc = (struct ucom_softc *)p;
985	struct tty *tp = sc->sc_tty;
986	u_int32_t cc;
987	int s;
988
989	DPRINTFN(5,("ucomwritecb: status=%d\n", status));
990
991	if (status == USBD_CANCELLED || sc->sc_dying)
992		goto error;
993
994	if (status) {
995		DPRINTF(("ucomwritecb: status=%d\n", status));
996		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
997		/* XXX we should restart after some delay. */
998		goto error;
999	}
1000
1001	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
1002#if defined(__NetBSD__) && NRND > 0
1003	rnd_add_uint32(&sc->sc_rndsource, cc);
1004#endif
1005	DPRINTFN(5,("ucomwritecb: cc=%d\n", cc));
1006	/* convert from USB bytes to tty bytes */
1007	cc -= sc->sc_opkthdrlen;
1008
1009	s = spltty();
1010	CLR(tp->t_state, TS_BUSY);
1011	if (ISSET(tp->t_state, TS_FLUSH))
1012		CLR(tp->t_state, TS_FLUSH);
1013	else
1014		ndflush(&tp->t_outq, cc);
1015	(*tp->t_linesw->l_start)(tp);
1016	splx(s);
1017	return;
1018
1019error:
1020	s = spltty();
1021	CLR(tp->t_state, TS_BUSY);
1022	splx(s);
1023}
1024
1025Static usbd_status
1026ucomstartread(struct ucom_softc *sc)
1027{
1028	usbd_status err;
1029
1030	DPRINTFN(5,("ucomstartread: start\n"));
1031	usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1032			(usbd_private_handle)sc,
1033			sc->sc_ibuf, sc->sc_ibufsize,
1034			USBD_SHORT_XFER_OK | USBD_NO_COPY,
1035			USBD_NO_TIMEOUT, ucomreadcb);
1036	err = usbd_transfer(sc->sc_ixfer);
1037	if (err != USBD_IN_PROGRESS) {
1038		DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err)));
1039		return (err);
1040	}
1041	return (USBD_NORMAL_COMPLETION);
1042}
1043
1044Static void
1045ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1046{
1047	struct ucom_softc *sc = (struct ucom_softc *)p;
1048	struct tty *tp = sc->sc_tty;
1049	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1050	usbd_status err;
1051	u_int32_t cc;
1052	u_char *cp;
1053	int s;
1054
1055	DPRINTFN(5,("ucomreadcb: status=%d\n", status));
1056
1057	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
1058	    sc->sc_dying) {
1059		DPRINTF(("ucomreadcb: dying\n"));
1060		/* Send something to wake upper layer */
1061		s = spltty();
1062		(*rint)('\n', tp);
1063		ttwakeup(tp);
1064		splx(s);
1065		return;
1066	}
1067
1068	if (status) {
1069		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1070		/* XXX we should restart after some delay. */
1071		return;
1072	}
1073
1074	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
1075#if defined(__NetBSD__) && NRND > 0
1076	rnd_add_uint32(&sc->sc_rndsource, cc);
1077#endif
1078	DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp));
1079	if (sc->sc_methods->ucom_read != NULL)
1080		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
1081					  &cp, &cc);
1082
1083	s = spltty();
1084	/* Give characters to tty layer. */
1085	while (cc-- > 0) {
1086		DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp));
1087		if ((*rint)(*cp++, tp) == -1) {
1088			/* XXX what should we do? */
1089			printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1090			       cc);
1091			break;
1092		}
1093	}
1094	splx(s);
1095
1096	err = ucomstartread(sc);
1097	if (err) {
1098		printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1099		/* XXX what should we dow now? */
1100	}
1101}
1102
1103Static void
1104ucom_cleanup(struct ucom_softc *sc)
1105{
1106	DPRINTF(("ucom_cleanup: closing pipes\n"));
1107
1108	ucom_shutdown(sc);
1109	if (sc->sc_bulkin_pipe != NULL) {
1110		usbd_abort_pipe(sc->sc_bulkin_pipe);
1111		usbd_close_pipe(sc->sc_bulkin_pipe);
1112		sc->sc_bulkin_pipe = NULL;
1113	}
1114	if (sc->sc_bulkout_pipe != NULL) {
1115		usbd_abort_pipe(sc->sc_bulkout_pipe);
1116		usbd_close_pipe(sc->sc_bulkout_pipe);
1117		sc->sc_bulkout_pipe = NULL;
1118	}
1119	if (sc->sc_ixfer != NULL) {
1120		usbd_free_xfer(sc->sc_ixfer);
1121		sc->sc_ixfer = NULL;
1122	}
1123	if (sc->sc_oxfer != NULL) {
1124		usbd_free_xfer(sc->sc_oxfer);
1125		sc->sc_oxfer = NULL;
1126	}
1127}
1128
1129#endif /* NUCOM > 0 */
1130
1131int
1132ucomprint(void *aux, const char *pnp)
1133{
1134	struct ucom_attach_args *uca = aux;
1135
1136	if (pnp)
1137		aprint_normal("ucom at %s", pnp);
1138	if (uca->portno != UCOM_UNK_PORTNO)
1139		aprint_normal(" portno %d", uca->portno);
1140	return (UNCONF);
1141}
1142
1143int
1144ucomsubmatch(struct device *parent, struct cfdata *cf,
1145	     const int *ldesc, void *aux)
1146{
1147	struct ucom_attach_args *uca = aux;
1148
1149	if (uca->portno != UCOM_UNK_PORTNO &&
1150	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
1151	    cf->cf_loc[UCOMBUSCF_PORTNO] != uca->portno)
1152		return (0);
1153	return (config_match(parent, cf, aux));
1154}
1155