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