xref: /netbsd-src/sys/dev/ir/irframe_tty.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: irframe_tty.c,v 1.62 2017/10/25 08:12:38 maya Exp $	*/
2 
3 /*
4  * TODO
5  *  Test dongle code.
6  */
7 
8 /*
9  * Copyright (c) 2001 The NetBSD Foundation, Inc.
10  * All rights reserved.
11  *
12  * This code is derived from software contributed to The NetBSD Foundation
13  * by Lennart Augustsson (lennart@augustsson.net) and Tommy Bohlin
14  * (tommy@gatespace.com).
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Loosely based on ppp_tty.c.
40  * Framing and dongle handling written by Tommy Bohlin.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: irframe_tty.c,v 1.62 2017/10/25 08:12:38 maya Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/proc.h>
48 #include <sys/ioctl.h>
49 #include <sys/tty.h>
50 #include <sys/kernel.h>
51 #include <sys/mutex.h>
52 #include <sys/malloc.h>
53 #include <sys/conf.h>
54 #include <sys/systm.h>
55 #include <sys/device.h>
56 #include <sys/file.h>
57 #include <sys/vnode.h>
58 #include <sys/poll.h>
59 #include <sys/kauth.h>
60 
61 #include <dev/ir/ir.h>
62 #include <dev/ir/sir.h>
63 #include <dev/ir/irdaio.h>
64 #include <dev/ir/irframevar.h>
65 
66 #include "ioconf.h"
67 
68 #ifdef IRFRAMET_DEBUG
69 #define DPRINTF(x)	if (irframetdebug) printf x
70 int irframetdebug = 0;
71 #else
72 #define DPRINTF(x)
73 #endif
74 
75 /*****/
76 
77 /* Max size with framing. */
78 #define MAX_IRDA_FRAME (2*IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + 4)
79 
80 struct irt_frame {
81 	u_char *buf;
82 	u_int len;
83 };
84 #define MAXFRAMES 8
85 
86 struct irframet_softc {
87 	struct irframe_softc sc_irp;
88 	struct tty *sc_tp;
89 
90 	int sc_dongle;
91 	int sc_dongle_private;
92 
93 	int sc_state;
94 #define	IRT_RSLP		0x01	/* waiting for data (read) */
95 #if 0
96 #define	IRT_WSLP		0x02	/* waiting for data (write) */
97 #define IRT_CLOSING		0x04	/* waiting for output to drain */
98 #endif
99 	kmutex_t sc_wr_lk;
100 
101 	struct irda_params sc_params;
102 
103 	u_char* sc_inbuf;
104 	int sc_framestate;
105 #define FRAME_OUTSIDE    0
106 #define FRAME_INSIDE     1
107 #define FRAME_ESCAPE     2
108 	int sc_inchars;
109 	int sc_inFCS;
110 	struct callout sc_timeout;
111 
112 	u_int sc_nframes;
113 	u_int sc_framei;
114 	u_int sc_frameo;
115 	struct irt_frame sc_frames[MAXFRAMES];
116 	u_int8_t sc_buffer[MAX_IRDA_FRAME];
117 	struct selinfo sc_rsel;
118 	/* XXXJRT Nothing selnotify's sc_wsel */
119 	struct selinfo sc_wsel;
120 };
121 
122 /* line discipline methods */
123 int	irframetopen(dev_t, struct tty *);
124 int	irframetclose(struct tty *, int);
125 int	irframetioctl(struct tty *, u_long, void *, int, struct lwp *);
126 int	irframetinput(int, struct tty *);
127 int	irframetstart(struct tty *);
128 
129 
130 /* irframe methods */
131 static int	irframet_open(void *, int, int, struct lwp *);
132 static int	irframet_close(void *, int, int, struct lwp *);
133 static int	irframet_read(void *, struct uio *, int);
134 static int	irframet_write(void *, struct uio *, int);
135 static int	irframet_poll(void *, int, struct lwp *);
136 static int	irframet_kqfilter(void *, struct knote *);
137 
138 static int	irframet_set_params(void *, struct irda_params *);
139 static int	irframet_get_speeds(void *, int *);
140 static int	irframet_get_turnarounds(void *, int *);
141 
142 /* internal */
143 static int	irt_write_frame(struct tty *, u_int8_t *, size_t);
144 static int	irt_putc(struct tty *, int);
145 static void	irt_frame(struct irframet_softc *, u_char *, u_int);
146 static void	irt_timeout(void *);
147 static void	irt_ioctl(struct tty *, u_long, void *);
148 static void	irt_setspeed(struct tty *, u_int);
149 static void	irt_setline(struct tty *, u_int);
150 static void	irt_delay(struct tty *, u_int);
151 static void	irt_buffer(struct irframet_softc *, u_int);
152 
153 static const struct irframe_methods irframet_methods = {
154 	irframet_open, irframet_close, irframet_read, irframet_write,
155 	irframet_poll, irframet_kqfilter, irframet_set_params,
156 	irframet_get_speeds, irframet_get_turnarounds
157 };
158 
159 static void irts_none(struct tty *, u_int);
160 static void irts_tekram(struct tty *, u_int);
161 static void irts_jeteye(struct tty *, u_int);
162 static void irts_actisys(struct tty *, u_int);
163 static void irts_litelink(struct tty *, u_int);
164 static void irts_girbil(struct tty *, u_int);
165 
166 #define NORMAL_SPEEDS (IRDA_SPEEDS_SIR & ~IRDA_SPEED_2400)
167 #define TURNT_POS (IRDA_TURNT_10000 | IRDA_TURNT_5000 | IRDA_TURNT_1000 | \
168 	IRDA_TURNT_500 | IRDA_TURNT_100 | IRDA_TURNT_50 | IRDA_TURNT_10)
169 static const struct dongle {
170 	void (*setspeed)(struct tty *, u_int);
171 	u_int speedmask;
172 	u_int turnmask;
173 } irt_dongles[DONGLE_MAX] = {
174 	/* Indexed by dongle number from irdaio.h */
175 	{ irts_none, IRDA_SPEEDS_SIR, IRDA_TURNT_10000 },
176 	{ irts_tekram, IRDA_SPEEDS_SIR, IRDA_TURNT_10000 },
177 	{ irts_jeteye, IRDA_SPEED_9600|IRDA_SPEED_19200|IRDA_SPEED_115200,
178 	  				IRDA_TURNT_10000 },
179 	{ irts_actisys, NORMAL_SPEEDS & ~IRDA_SPEED_38400, TURNT_POS },
180 	{ irts_actisys, NORMAL_SPEEDS, TURNT_POS },
181 	{ irts_litelink, NORMAL_SPEEDS, TURNT_POS },
182 	{ irts_girbil, IRDA_SPEEDS_SIR, IRDA_TURNT_10000 | IRDA_TURNT_5000 },
183 };
184 
185 static struct linesw irframet_disc = {
186 	.l_name = "irframe",
187 	.l_open = irframetopen,
188 	.l_close = irframetclose,
189 	.l_read = ttyerrio,
190 	.l_write = ttyerrio,
191 	.l_ioctl = irframetioctl,
192 	.l_rint = irframetinput,
193 	.l_start = irframetstart,
194 	.l_modem = ttymodem,
195 	.l_poll = ttyerrpoll
196 };
197 
198 /* glue to attach irframe device */
199 static void irframet_attach(device_t, device_t, void *);
200 static int irframet_detach(device_t, int);
201 
202 CFATTACH_DECL_NEW(irframet, sizeof(struct irframet_softc),
203 	NULL, irframet_attach, irframet_detach, NULL);
204 
205 void
206 irframettyattach(int n)
207 {
208 	extern struct cfdriver irframe_cd;
209 
210 	(void) ttyldisc_attach(&irframet_disc);
211 
212 	/* XXX might fail if "real" attachments have pulled this in */
213 	/* XXX should not be done here */
214 	config_cfdriver_attach(&irframe_cd);
215 
216 	config_cfattach_attach("irframe", &irframet_ca);
217 }
218 
219 static void
220 irframet_attach(device_t parent, device_t self, void *aux)
221 {
222 	struct irframet_softc *sc = device_private(self);
223 
224 	/* pseudo-device attachment does not print name */
225 	aprint_normal("%s", device_xname(self));
226 
227 	callout_init(&sc->sc_timeout, 0);
228 	mutex_init(&sc->sc_wr_lk, MUTEX_DEFAULT, IPL_NONE);
229 	selinit(&sc->sc_rsel);
230 	selinit(&sc->sc_wsel);
231 
232 #if 0 /* XXX can't do it yet because pseudo-devices don't get aux */
233 	struct ir_attach_args ia;
234 
235 	ia.ia_methods = &irframet_methods;
236 	ia.ia_handle = aux->xxx;
237 
238 	irframe_attach(parent, self, &ia);
239 #endif
240 }
241 
242 static int
243 irframet_detach(device_t dev, int flags)
244 {
245 	struct irframet_softc *sc = device_private(dev);
246 	int rc;
247 
248 	callout_halt(&sc->sc_timeout, NULL);
249 
250 	rc = irframe_detach(dev, flags);
251 
252 	callout_destroy(&sc->sc_timeout);
253 	mutex_destroy(&sc->sc_wr_lk);
254 	seldestroy(&sc->sc_wsel);
255 	seldestroy(&sc->sc_rsel);
256 
257 	return rc;
258 }
259 
260 /*
261  * Line specific open routine for async tty devices.
262  * Attach the given tty to the first available irframe unit.
263  * Called from device open routine or ttioctl.
264  */
265 /* ARGSUSED */
266 int
267 irframetopen(dev_t dev, struct tty *tp)
268 {
269 	struct lwp *l = curlwp;		/* XXX */
270 	struct irframet_softc *sc;
271 	int error, s;
272 	cfdata_t cfdata;
273 	struct ir_attach_args ia;
274 	device_t d;
275 
276 	DPRINTF(("%s\n", __func__));
277 
278 	if ((error = kauth_authorize_device_tty(l->l_cred,
279 		KAUTH_DEVICE_TTY_OPEN, tp)))
280 		return (error);
281 
282 	s = spltty();
283 
284 	DPRINTF(("%s: linesw=%p disc=%s\n", __func__, tp->t_linesw,
285 		 tp->t_linesw->l_name));
286 	if (tp->t_linesw == &irframet_disc) {
287 		sc = (struct irframet_softc *)tp->t_sc;
288 		DPRINTF(("%s: sc=%p sc_tp=%p\n", __func__, sc, sc->sc_tp));
289 		if (sc != NULL) {
290 			splx(s);
291 			return (EBUSY);
292 		}
293 	}
294 
295 	cfdata = malloc(sizeof(struct cfdata), M_DEVBUF, M_WAITOK);
296 	cfdata->cf_name = "irframe";
297 	cfdata->cf_atname = "irframet";
298 	cfdata->cf_fstate = FSTATE_STAR;
299 	cfdata->cf_unit = 0;
300 	d = config_attach_pseudo(cfdata);
301 	sc = device_private(d);
302 	sc->sc_irp.sc_dev = d;
303 
304 	/* XXX should be done in irframet_attach() */
305 	ia.ia_methods = &irframet_methods;
306 	ia.ia_handle = tp;
307 	irframe_attach(0, d, &ia);
308 
309 	tp->t_sc = sc;
310 	sc->sc_tp = tp;
311 	aprint_normal("%s attached at tty%02d\n", device_xname(d),
312 	    (int)minor(tp->t_dev));
313 
314 	DPRINTF(("%s: set sc=%p\n", __func__, sc));
315 
316 	mutex_spin_enter(&tty_lock);
317 	ttyflush(tp, FREAD | FWRITE);
318 	mutex_spin_exit(&tty_lock);
319 
320 	sc->sc_dongle = DONGLE_NONE;
321 	sc->sc_dongle_private = 0;
322 
323 	splx(s);
324 
325 	return (0);
326 }
327 
328 /*
329  * Line specific close routine, called from device close routine
330  * and from ttioctl.
331  * Detach the tty from the irframe unit.
332  * Mimics part of ttyclose().
333  */
334 int
335 irframetclose(struct tty *tp, int flag)
336 {
337 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
338 	int s;
339 	cfdata_t cfdata;
340 
341 	DPRINTF(("%s: tp=%p\n", __func__, tp));
342 
343 	s = spltty();
344 	mutex_spin_enter(&tty_lock);
345 	ttyflush(tp, FREAD | FWRITE);
346 	mutex_spin_exit(&tty_lock);	 /* XXX */
347 	ttyldisc_release(tp->t_linesw);
348 	tp->t_linesw = ttyldisc_default(); if (sc != NULL) {
349 		irt_buffer(sc, 0);
350 		tp->t_sc = NULL;
351 		aprint_normal("%s detached from tty%02d\n",
352 		    device_xname(sc->sc_irp.sc_dev), (int)minor(tp->t_dev));
353 
354 		if (sc->sc_tp == tp) {
355 			cfdata = device_cfdata(sc->sc_irp.sc_dev);
356 			config_detach(sc->sc_irp.sc_dev, 0);
357 			free(cfdata, M_DEVBUF);
358 		}
359 	}
360 	splx(s);
361 	return (0);
362 }
363 
364 /*
365  * Line specific (tty) ioctl routine.
366  * This discipline requires that tty device drivers call
367  * the line specific l_ioctl routine from their ioctl routines.
368  */
369 /* ARGSUSED */
370 int
371 irframetioctl(struct tty *tp, u_long cmd, void *data, int flag,
372     struct lwp *l)
373 {
374 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
375 	int error;
376 	int d;
377 
378 	DPRINTF(("%s: tp=%p\n", __func__, tp));
379 
380 	if (sc == NULL || tp != sc->sc_tp)
381 		return (EPASSTHROUGH);
382 
383 	error = 0;
384 	switch (cmd) {
385 	case IRFRAMETTY_GET_DEVICE:
386 		*(int *)data = device_unit(sc->sc_irp.sc_dev);
387 		break;
388 	case IRFRAMETTY_GET_DONGLE:
389 		*(int *)data = sc->sc_dongle;
390 		break;
391 	case IRFRAMETTY_SET_DONGLE:
392 		d = *(int *)data;
393 		if (d < 0 || d >= DONGLE_MAX)
394 			return (EINVAL);
395 		sc->sc_dongle = d;
396 		break;
397 	default:
398 		error = EPASSTHROUGH;
399 		break;
400 	}
401 
402 	return (error);
403 }
404 
405 /*
406  * Start output on async tty interface.
407  */
408 int
409 irframetstart(struct tty *tp)
410 {
411 	/*struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;*/
412 	int s;
413 
414 	DPRINTF(("%s: tp=%p\n", __func__, tp));
415 
416 	s = spltty();
417 	if (tp->t_oproc != NULL)
418 		(*tp->t_oproc)(tp);
419 	splx(s);
420 
421 	return (0);
422 }
423 
424 static void
425 irt_buffer(struct irframet_softc *sc, u_int maxsize)
426 {
427 	int i;
428 
429 	DPRINTF(("%s: sc=%p, maxsize=%u\n", __func__, sc, maxsize));
430 
431 	if (sc->sc_params.maxsize != maxsize) {
432 		sc->sc_params.maxsize = maxsize;
433 		if (sc->sc_inbuf != NULL)
434 			free(sc->sc_inbuf, M_DEVBUF);
435 		for (i = 0; i < MAXFRAMES; i++)
436 			if (sc->sc_frames[i].buf != NULL)
437 				free(sc->sc_frames[i].buf, M_DEVBUF);
438 		if (sc->sc_params.maxsize != 0) {
439 			sc->sc_inbuf = malloc(sc->sc_params.maxsize+2,
440 					      M_DEVBUF, M_WAITOK);
441 			for (i = 0; i < MAXFRAMES; i++)
442 				sc->sc_frames[i].buf =
443 					malloc(sc->sc_params.maxsize,
444 					       M_DEVBUF, M_WAITOK);
445 		} else {
446 			sc->sc_inbuf = NULL;
447 			for (i = 0; i < MAXFRAMES; i++)
448 				sc->sc_frames[i].buf = NULL;
449 		}
450 	}
451 }
452 
453 void
454 irt_frame(struct irframet_softc *sc, u_char *tbuf, u_int len)
455 {
456 	DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
457 		 __func__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
458 
459 	if (sc->sc_inbuf == NULL) /* XXX happens if device is closed? */
460 		return;
461 	if (sc->sc_nframes >= MAXFRAMES) {
462 #ifdef IRFRAMET_DEBUG
463 		printf("%s: dropped frame\n", __func__);
464 #endif
465 		return;
466 	}
467 	if (sc->sc_frames[sc->sc_framei].buf == NULL)
468 		return;
469 	memcpy(sc->sc_frames[sc->sc_framei].buf, tbuf, len);
470 	sc->sc_frames[sc->sc_framei].len = len;
471 	sc->sc_framei = (sc->sc_framei+1) % MAXFRAMES;
472 	sc->sc_nframes++;
473 	if (sc->sc_state & IRT_RSLP) {
474 		sc->sc_state &= ~IRT_RSLP;
475 		DPRINTF(("%s: waking up reader\n", __func__));
476 		wakeup(sc->sc_frames);
477 	}
478 	selnotify(&sc->sc_rsel, 0, 0);
479 }
480 
481 void
482 irt_timeout(void *v)
483 {
484 	struct irframet_softc *sc = v;
485 
486 #ifdef IRFRAMET_DEBUG
487 	if (sc->sc_framestate != FRAME_OUTSIDE)
488 		printf("%s: input frame timeout\n", __func__);
489 #endif
490 	sc->sc_framestate = FRAME_OUTSIDE;
491 }
492 
493 int
494 irframetinput(int c, struct tty *tp)
495 {
496 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
497 
498 	c &= 0xff;
499 
500 #if IRFRAMET_DEBUG
501 	if (irframetdebug > 1)
502 		DPRINTF(("%s: tp=%p c=0x%02x\n", __func__, tp, c));
503 #endif
504 
505 	if (sc == NULL || tp != (struct tty *)sc->sc_tp)
506 		return (0);
507 
508 	if (sc->sc_inbuf == NULL)
509 		return (0);
510 
511 	switch (c) {
512 	case SIR_BOF:
513 		DPRINTF(("%s: BOF\n", __func__));
514 		sc->sc_framestate = FRAME_INSIDE;
515 		sc->sc_inchars = 0;
516 		sc->sc_inFCS = INITFCS;
517 		break;
518 	case SIR_EOF:
519 		DPRINTF(("%s: EOF state=%d inchars=%d fcs=0x%04x\n",
520 			 __func__,
521 			 sc->sc_framestate, sc->sc_inchars, sc->sc_inFCS));
522 		if (sc->sc_framestate == FRAME_INSIDE &&
523 		    sc->sc_inchars >= 4 && sc->sc_inFCS == GOODFCS) {
524 			irt_frame(sc, sc->sc_inbuf, sc->sc_inchars - 2);
525 		} else if (sc->sc_framestate != FRAME_OUTSIDE) {
526 #ifdef IRFRAMET_DEBUG
527 			printf("%s: malformed input frame\n", __func__);
528 #endif
529 		}
530 		sc->sc_framestate = FRAME_OUTSIDE;
531 		break;
532 	case SIR_CE:
533 		DPRINTF(("%s: CE\n", __func__));
534 		if (sc->sc_framestate == FRAME_INSIDE)
535 			sc->sc_framestate = FRAME_ESCAPE;
536 		break;
537 	default:
538 #if IRFRAMET_DEBUG
539 	if (irframetdebug > 1)
540 		DPRINTF(("%s: c=0x%02x, inchar=%d state=%d\n", __func__, c,
541 			 sc->sc_inchars, sc->sc_state));
542 #endif
543 		if (sc->sc_framestate != FRAME_OUTSIDE) {
544 			if (sc->sc_framestate == FRAME_ESCAPE) {
545 				sc->sc_framestate = FRAME_INSIDE;
546 				c ^= SIR_ESC_BIT;
547 			}
548 			if (sc->sc_inchars < sc->sc_params.maxsize + 2) {
549 				sc->sc_inbuf[sc->sc_inchars++] = c;
550 				sc->sc_inFCS = updateFCS(sc->sc_inFCS, c);
551 			} else {
552 				sc->sc_framestate = FRAME_OUTSIDE;
553 #ifdef IRFRAMET_DEBUG
554 				printf("%s: input frame overrun\n",
555 				       __func__);
556 #endif
557 			}
558 		}
559 		break;
560 	}
561 
562 #if 1
563 	if (sc->sc_framestate != FRAME_OUTSIDE) {
564 		callout_reset(&sc->sc_timeout, hz/20, irt_timeout, sc);
565 	}
566 #endif
567 
568 	return (0);
569 }
570 
571 
572 /*** irframe methods ***/
573 
574 int
575 irframet_open(void *h, int flag, int mode,
576     struct lwp *l)
577 {
578 	struct tty *tp = h;
579 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
580 
581 	DPRINTF(("%s: tp=%p\n", __func__, tp));
582 
583 	sc->sc_params.speed = 0;
584 	sc->sc_params.ebofs = IRDA_DEFAULT_EBOFS;
585 	sc->sc_params.maxsize = 0;
586 	sc->sc_framestate = FRAME_OUTSIDE;
587 	sc->sc_nframes = 0;
588 	sc->sc_framei = 0;
589 	sc->sc_frameo = 0;
590 
591 	return (0);
592 }
593 
594 int
595 irframet_close(void *h, int flag, int mode,
596     struct lwp *l)
597 {
598 	struct tty *tp = h;
599 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
600 	int s;
601 
602 	DPRINTF(("%s: tp=%p\n", __func__, tp));
603 
604 	/* line discipline was closed */
605 	if (sc == NULL)
606 		return (0);
607 
608 	callout_stop(&sc->sc_timeout);
609 	s = splir();
610 	irt_buffer(sc, 0);
611 	splx(s);
612 
613 	return (0);
614 }
615 
616 int
617 irframet_read(void *h, struct uio *uio, int flag)
618 {
619 	struct tty *tp = h;
620 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
621 	int error = 0;
622 	int s;
623 
624 	DPRINTF(("%s: resid=%zd, iovcnt=%d, offset=%ld\n",
625 		 __func__, uio->uio_resid, uio->uio_iovcnt,
626 		 (long)uio->uio_offset));
627 	DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
628 		 __func__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
629 
630 
631 	s = splir();
632 	while (sc->sc_nframes == 0) {
633 		if (flag & IO_NDELAY) {
634 			splx(s);
635 			return (EWOULDBLOCK);
636 		}
637 		sc->sc_state |= IRT_RSLP;
638 		DPRINTF(("%s: sleep\n", __func__));
639 		error = tsleep(sc->sc_frames, PZERO | PCATCH, "irtrd", 0);
640 		DPRINTF(("%s: woke, error=%d\n", __func__, error));
641 		if (error) {
642 			sc->sc_state &= ~IRT_RSLP;
643 			break;
644 		}
645 	}
646 
647 	/* Do just one frame transfer per read */
648 	if (!error) {
649 		if (uio->uio_resid < sc->sc_frames[sc->sc_frameo].len) {
650 			DPRINTF(("%s: uio buffer smaller than frame size "
651 				 "(%zd < %d)\n", __func__, uio->uio_resid,
652 				 sc->sc_frames[sc->sc_frameo].len));
653 			error = EINVAL;
654 		} else {
655 			DPRINTF(("%s: moving %d bytes\n", __func__,
656 				 sc->sc_frames[sc->sc_frameo].len));
657 			error = uiomove(sc->sc_frames[sc->sc_frameo].buf,
658 					sc->sc_frames[sc->sc_frameo].len, uio);
659 			DPRINTF(("%s: error=%d\n", __func__, error));
660 		}
661 		sc->sc_frameo = (sc->sc_frameo+1) % MAXFRAMES;
662 		sc->sc_nframes--;
663 	}
664 	splx(s);
665 
666 	return (error);
667 }
668 
669 int
670 irt_putc(struct tty *tp, int c)
671 {
672 	int error;
673 
674 #if IRFRAMET_DEBUG
675 	if (irframetdebug > 3)
676 		DPRINTF(("%s: tp=%p c=0x%02x cc=%d\n", __func__, tp, c,
677 			 tp->t_outq.c_cc));
678 #endif
679 	if (tp->t_outq.c_cc > tp->t_hiwat) {
680 		irframetstart(tp);
681 		mutex_spin_enter(&tty_lock);
682 		/*
683 		 * This can only occur if FLUSHO is set in t_lflag,
684 		 * or if ttstart/oproc is synchronous (or very fast).
685 		 */
686 		if (tp->t_outq.c_cc <= tp->t_hiwat) {
687 			mutex_spin_exit(&tty_lock);
688 			goto go;
689 		}
690 		error = ttysleep(tp, &tp->t_outcv, true, 0);
691 		mutex_spin_exit(&tty_lock);
692 		if (error)
693 			return (error);
694 	}
695  go:
696 	if (putc(c, &tp->t_outq) < 0) {
697 		printf("irframe: putc failed\n");
698 		return (EIO);
699 	}
700 	return (0);
701 }
702 
703 int
704 irframet_write(void *h, struct uio *uio, int flag)
705 {
706 	struct tty *tp = h;
707 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
708 	int n;
709 
710 	DPRINTF(("%s: resid=%zd, iovcnt=%d, offset=%ld\n",
711 		 __func__, uio->uio_resid, uio->uio_iovcnt,
712 		 (long)uio->uio_offset));
713 
714 	n = irda_sir_frame(sc->sc_buffer, sizeof(sc->sc_buffer), uio,
715 	    sc->sc_params.ebofs);
716 	if (n < 0) {
717 #ifdef IRFRAMET_DEBUG
718 		printf("%s: irda_sir_frame() error=%d\n", __func__, -n);
719 #endif
720 		return (-n);
721 	}
722 	return (irt_write_frame(tp, sc->sc_buffer, n));
723 }
724 
725 int
726 irt_write_frame(struct tty *tp, u_int8_t *tbuf, size_t len)
727 {
728 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
729 	int error, i;
730 
731 	DPRINTF(("%s: tp=%p len=%zd\n", __func__, tp, len));
732 
733 	mutex_enter(&sc->sc_wr_lk);
734 	error = 0;
735 	for (i = 0; !error && i < len; i++)
736 		error = irt_putc(tp, tbuf[i]);
737 	mutex_exit(&sc->sc_wr_lk);
738 
739 	irframetstart(tp);
740 
741 	DPRINTF(("%s: done, error=%d\n", __func__, error));
742 
743 	return (error);
744 }
745 
746 int
747 irframet_poll(void *h, int events, struct lwp *l)
748 {
749 	struct tty *tp = h;
750 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
751 	int revents = 0;
752 	int s;
753 
754 	DPRINTF(("%s: sc=%p\n", __func__, sc));
755 
756 	s = splir();
757 	/* XXX is this a good check? */
758 	if (events & (POLLOUT | POLLWRNORM))
759 		if (tp->t_outq.c_cc <= tp->t_lowat)
760 			revents |= events & (POLLOUT | POLLWRNORM);
761 
762 	if (events & (POLLIN | POLLRDNORM)) {
763 		if (sc->sc_nframes > 0) {
764 			DPRINTF(("%s: have data\n", __func__));
765 			revents |= events & (POLLIN | POLLRDNORM);
766 		} else {
767 			DPRINTF(("%s: recording select\n", __func__));
768 			selrecord(l, &sc->sc_rsel);
769 		}
770 	}
771 	splx(s);
772 
773 	return (revents);
774 }
775 
776 static void
777 filt_irframetrdetach(struct knote *kn)
778 {
779 	struct tty *tp = kn->kn_hook;
780 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
781 	int s;
782 
783 	s = splir();
784 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
785 	splx(s);
786 }
787 
788 static int
789 filt_irframetread(struct knote *kn, long hint)
790 {
791 	struct tty *tp = kn->kn_hook;
792 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
793 
794 	kn->kn_data = sc->sc_nframes;
795 	return (kn->kn_data > 0);
796 }
797 
798 static void
799 filt_irframetwdetach(struct knote *kn)
800 {
801 	struct tty *tp = kn->kn_hook;
802 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
803 	int s;
804 
805 	s = splir();
806 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
807 	splx(s);
808 }
809 
810 static int
811 filt_irframetwrite(struct knote *kn, long hint)
812 {
813 	struct tty *tp = kn->kn_hook;
814 
815 	/* XXX double-check this */
816 
817 	if (tp->t_outq.c_cc <= tp->t_lowat) {
818 		kn->kn_data = tp->t_lowat - tp->t_outq.c_cc;
819 		return (1);
820 	}
821 
822 	kn->kn_data = 0;
823 	return (0);
824 }
825 
826 static const struct filterops irframetread_filtops = {
827 	.f_isfd = 1,
828 	.f_attach = NULL,
829 	.f_detach = filt_irframetrdetach,
830 	.f_event = filt_irframetread,
831 };
832 
833 static const struct filterops irframetwrite_filtops = {
834 	.f_isfd = 1,
835 	.f_attach = NULL,
836 	.f_detach = filt_irframetwdetach,
837 	.f_event = filt_irframetwrite,
838 };
839 
840 int
841 irframet_kqfilter(void *h, struct knote *kn)
842 {
843 	struct tty *tp = h;
844 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
845 	struct klist *klist;
846 	int s;
847 
848 	switch (kn->kn_filter) {
849 	case EVFILT_READ:
850 		klist = &sc->sc_rsel.sel_klist;
851 		kn->kn_fop = &irframetread_filtops;
852 		break;
853 	case EVFILT_WRITE:
854 		klist = &sc->sc_wsel.sel_klist;
855 		kn->kn_fop = &irframetwrite_filtops;
856 		break;
857 	default:
858 		return (EINVAL);
859 	}
860 
861 	kn->kn_hook = tp;
862 
863 	s = splir();
864 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
865 	splx(s);
866 
867 	return (0);
868 }
869 
870 int
871 irframet_set_params(void *h, struct irda_params *p)
872 {
873 	struct tty *tp = h;
874 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
875 
876 	DPRINTF(("%s: tp=%p speed=%d ebofs=%d maxsize=%d\n",
877 		 __func__, tp, p->speed, p->ebofs, p->maxsize));
878 
879 	if (p->speed != sc->sc_params.speed) {
880 		/* Checked in irframe.c */
881 		mutex_enter(&sc->sc_wr_lk);
882 		irt_dongles[sc->sc_dongle].setspeed(tp, p->speed);
883 		mutex_exit(&sc->sc_wr_lk);
884 		sc->sc_params.speed = p->speed;
885 	}
886 
887 	/* Max size checked in irframe.c */
888 	sc->sc_params.ebofs = p->ebofs;
889 	irt_buffer(sc, p->maxsize);
890 	sc->sc_framestate = FRAME_OUTSIDE;
891 
892 	return (0);
893 }
894 
895 int
896 irframet_get_speeds(void *h, int *speeds)
897 {
898 	struct tty *tp = h;
899 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
900 
901 	DPRINTF(("%s: tp=%p\n", __func__, tp));
902 
903 	if (sc == NULL)		/* during attach */
904 		*speeds = IRDA_SPEEDS_SIR;
905 	else
906 		*speeds = irt_dongles[sc->sc_dongle].speedmask;
907 	return (0);
908 }
909 
910 int
911 irframet_get_turnarounds(void *h, int *turnarounds)
912 {
913 	struct tty *tp = h;
914 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
915 
916 	DPRINTF(("%s: tp=%p\n", __func__, tp));
917 
918 	*turnarounds = irt_dongles[sc->sc_dongle].turnmask;
919 	return (0);
920 }
921 
922 void
923 irt_ioctl(struct tty *tp, u_long cmd, void *arg)
924 {
925 	const struct cdevsw *cdev;
926 	int error __diagused;
927 	dev_t dev;
928 
929 	dev = tp->t_dev;
930 	cdev = cdevsw_lookup(dev);
931 	if (cdev != NULL)
932 		error = (*cdev->d_ioctl)(dev, cmd, arg, 0, curlwp);
933 	else
934 		error = ENXIO;
935 #ifdef DIAGNOSTIC
936 	if (error)
937 		printf("irt_ioctl: cmd=0x%08lx error=%d\n", cmd, error);
938 #endif
939 }
940 
941 void
942 irt_setspeed(struct tty *tp, u_int speed)
943 {
944 	struct termios tt;
945 
946 	irt_ioctl(tp, TIOCGETA,  &tt);
947 	tt.c_ispeed = tt.c_ospeed = speed;
948 	tt.c_cflag &= ~HUPCL;
949 	tt.c_cflag |= CLOCAL;
950 	irt_ioctl(tp, TIOCSETAF, &tt);
951 }
952 
953 void
954 irt_setline(struct tty *tp, u_int line)
955 {
956 	int mline;
957 
958 	irt_ioctl(tp, TIOCMGET, &mline);
959 	mline &= ~(TIOCM_DTR | TIOCM_RTS);
960 	mline |= line;
961 	irt_ioctl(tp, TIOCMSET, (void *)&mline);
962 }
963 
964 void
965 irt_delay(struct tty *tp, u_int ms)
966 {
967 	if (cold)
968 		delay(ms * 1000);
969 	else
970 		tsleep(&irt_delay, PZERO, "irtdly", ms * hz / 1000 + 1);
971 
972 }
973 
974 /**********************************************************************
975  * No dongle
976  **********************************************************************/
977 void
978 irts_none(struct tty *tp, u_int speed)
979 {
980 	irt_setspeed(tp, speed);
981 }
982 
983 /**********************************************************************
984  * Tekram
985  **********************************************************************/
986 #define TEKRAM_PW     0x10
987 
988 #define TEKRAM_115200 (TEKRAM_PW|0x00)
989 #define TEKRAM_57600  (TEKRAM_PW|0x01)
990 #define TEKRAM_38400  (TEKRAM_PW|0x02)
991 #define TEKRAM_19200  (TEKRAM_PW|0x03)
992 #define TEKRAM_9600   (TEKRAM_PW|0x04)
993 #define TEKRAM_2400   (TEKRAM_PW|0x08)
994 
995 #define TEKRAM_TV     (TEKRAM_PW|0x05)
996 
997 void
998 irts_tekram(struct tty *tp, u_int speed)
999 {
1000 	int s;
1001 
1002 	irt_setspeed(tp, 9600);
1003 	irt_setline(tp, 0);
1004 	irt_delay(tp, 50);
1005 
1006 	irt_setline(tp, TIOCM_RTS);
1007 	irt_delay(tp, 1);
1008 
1009 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1010 	irt_delay(tp, 1);	/* 50 us */
1011 
1012 	irt_setline(tp, TIOCM_DTR);
1013 	irt_delay(tp, 1);	/* 7 us */
1014 
1015 	switch(speed) {
1016 	case 115200: s = TEKRAM_115200; break;
1017 	case 57600:  s = TEKRAM_57600; break;
1018 	case 38400:  s = TEKRAM_38400; break;
1019 	case 19200:  s = TEKRAM_19200; break;
1020 	case 2400:   s = TEKRAM_2400; break;
1021 	default:     s = TEKRAM_9600; break;
1022 	}
1023 	irt_putc(tp, s);
1024 	irframetstart(tp);
1025 
1026 	irt_delay(tp, 100);
1027 
1028 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1029 	if (speed != 9600)
1030 		irt_setspeed(tp, speed);
1031 	irt_delay(tp, 1);	/* 50 us */
1032 }
1033 
1034 /**********************************************************************
1035  * Jeteye
1036  **********************************************************************/
1037 void
1038 irts_jeteye(struct tty *tp, u_int speed)
1039 {
1040 	switch (speed) {
1041 	case 19200:
1042 		irt_setline(tp, TIOCM_DTR);
1043 		break;
1044 	case 115200:
1045 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1046 		break;
1047 	default: /*9600*/
1048 		irt_setline(tp, TIOCM_RTS);
1049 		break;
1050 	}
1051 	irt_setspeed(tp, speed);
1052 }
1053 
1054 /**********************************************************************
1055  * Actisys
1056  **********************************************************************/
1057 void
1058 irts_actisys(struct tty *tp, u_int speed)
1059 {
1060 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
1061 	int pulses;
1062 
1063 	irt_setspeed(tp, speed);
1064 
1065 	switch(speed) {
1066 	case 19200:  pulses=1; break;
1067 	case 57600:  pulses=2; break;
1068 	case 115200: pulses=3; break;
1069 	case 38400:  pulses=4; break;
1070 	default: /* 9600 */ pulses=0; break;
1071 	}
1072 
1073 	if (sc->sc_dongle_private == 0) {
1074 		sc->sc_dongle_private = 1;
1075 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1076 		/*
1077 		 * Must wait at least 50ms after initial
1078 		 * power on to charge internal capacitor
1079 		 */
1080 		irt_delay(tp, 50);
1081 	}
1082 	irt_setline(tp, TIOCM_RTS);
1083 	delay(2);
1084 	for (;;) {
1085 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1086 		delay(2);
1087 		if (--pulses <= 0)
1088 			break;
1089 		irt_setline(tp, TIOCM_DTR);
1090 		delay(2);
1091 	}
1092 }
1093 
1094 /**********************************************************************
1095  * Litelink
1096  **********************************************************************/
1097 void
1098 irts_litelink(struct tty *tp, u_int speed)
1099 {
1100 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
1101 	int pulses;
1102 
1103 	irt_setspeed(tp, speed);
1104 
1105 	switch(speed) {
1106 	case 57600:  pulses=1; break;
1107 	case 38400:  pulses=2; break;
1108 	case 19200:  pulses=3; break;
1109 	case 9600:   pulses=4; break;
1110 	default: /* 115200 */ pulses=0; break;
1111 	}
1112 
1113 	if (sc->sc_dongle_private == 0) {
1114 		sc->sc_dongle_private = 1;
1115 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1116 	}
1117 	irt_setline(tp, TIOCM_RTS);
1118 	irt_delay(tp, 1); /* 15 us */;
1119 	for (;;) {
1120 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1121 		irt_delay(tp, 1); /* 15 us */;
1122 		if (--pulses <= 0)
1123 			break;
1124 		irt_setline(tp, TIOCM_DTR);
1125 		irt_delay(tp, 1); /* 15 us */;
1126 	}
1127 }
1128 
1129 /**********************************************************************
1130  * Girbil
1131  **********************************************************************/
1132 /* Control register 1 */
1133 #define GIRBIL_TXEN      0x01 /* Enable transmitter */
1134 #define GIRBIL_RXEN      0x02 /* Enable receiver */
1135 #define GIRBIL_ECAN      0x04 /* Cancel self emmited data */
1136 #define GIRBIL_ECHO      0x08 /* Echo control characters */
1137 
1138 /* LED Current Register */
1139 #define GIRBIL_HIGH      0x20
1140 #define GIRBIL_MEDIUM    0x21
1141 #define GIRBIL_LOW       0x22
1142 
1143 /* Baud register */
1144 #define GIRBIL_2400      0x30
1145 #define GIRBIL_4800      0x31
1146 #define GIRBIL_9600      0x32
1147 #define GIRBIL_19200     0x33
1148 #define GIRBIL_38400     0x34
1149 #define GIRBIL_57600     0x35
1150 #define GIRBIL_115200    0x36
1151 
1152 /* Mode register */
1153 #define GIRBIL_IRDA      0x40
1154 #define GIRBIL_ASK       0x41
1155 
1156 /* Control register 2 */
1157 #define GIRBIL_LOAD      0x51 /* Load the new baud rate value */
1158 
1159 void
1160 irts_girbil(struct tty *tp, u_int speed)
1161 {
1162 	int s;
1163 
1164 	irt_setspeed(tp, 9600);
1165 	irt_setline(tp, TIOCM_DTR);
1166 	irt_delay(tp, 5);
1167 	irt_setline(tp, TIOCM_RTS);
1168 	irt_delay(tp, 20);
1169 	switch(speed) {
1170 	case 115200: s = GIRBIL_115200; break;
1171 	case 57600:  s = GIRBIL_57600; break;
1172 	case 38400:  s = GIRBIL_38400; break;
1173 	case 19200:  s = GIRBIL_19200; break;
1174 	case 4800:   s = GIRBIL_4800; break;
1175 	case 2400:   s = GIRBIL_2400; break;
1176 	default:     s = GIRBIL_9600; break;
1177 	}
1178 	irt_putc(tp, GIRBIL_TXEN|GIRBIL_RXEN);
1179 	irt_putc(tp, s);
1180 	irt_putc(tp, GIRBIL_LOAD);
1181 	irframetstart(tp);
1182 	irt_delay(tp, 100);
1183 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1184 	if (speed != 9600)
1185 		irt_setspeed(tp, speed);
1186 }
1187