xref: /netbsd/sys/dev/ir/irframe_tty.c (revision 43926d73)
1 /*	$NetBSD: irframe_tty.c,v 1.67 2022/10/26 23:45:43 riastradh 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.67 2022/10/26 23:45:43 riastradh 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
irframettyattach(int n)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
irframet_attach(device_t parent,device_t self,void * aux)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
irframet_detach(device_t dev,int flags)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
irframetopen(dev_t dev,struct tty * tp)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 	ttylock(tp);
317 	ttyflush(tp, FREAD | FWRITE);
318 	ttyunlock(tp);
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
irframetclose(struct tty * tp,int flag)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 	ttylock(tp);
345 	ttyflush(tp, FREAD | FWRITE);
346 	ttyunlock(tp);	 /* 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
irframetioctl(struct tty * tp,u_long cmd,void * data,int flag,struct lwp * l)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 	/*
381 	 * XXX
382 	 * This function can be called without KERNEL_LOCK when caller's
383 	 * struct cdevsw is set D_MPSAFE. Is KERNEL_LOCK required?
384 	 */
385 
386 	if (sc == NULL || tp != sc->sc_tp)
387 		return (EPASSTHROUGH);
388 
389 	error = 0;
390 	switch (cmd) {
391 	case IRFRAMETTY_GET_DEVICE:
392 		*(int *)data = device_unit(sc->sc_irp.sc_dev);
393 		break;
394 	case IRFRAMETTY_GET_DONGLE:
395 		*(int *)data = sc->sc_dongle;
396 		break;
397 	case IRFRAMETTY_SET_DONGLE:
398 		d = *(int *)data;
399 		if (d < 0 || d >= DONGLE_MAX)
400 			return (EINVAL);
401 		sc->sc_dongle = d;
402 		break;
403 	default:
404 		error = EPASSTHROUGH;
405 		break;
406 	}
407 
408 	return (error);
409 }
410 
411 /*
412  * Start output on async tty interface.
413  */
414 int
irframetstart(struct tty * tp)415 irframetstart(struct tty *tp)
416 {
417 	/*struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;*/
418 	int s;
419 
420 	DPRINTF(("%s: tp=%p\n", __func__, tp));
421 
422 	s = spltty();
423 	if (tp->t_oproc != NULL)
424 		(*tp->t_oproc)(tp);
425 	splx(s);
426 
427 	return (0);
428 }
429 
430 static void
irt_buffer(struct irframet_softc * sc,u_int maxsize)431 irt_buffer(struct irframet_softc *sc, u_int maxsize)
432 {
433 	int i;
434 
435 	DPRINTF(("%s: sc=%p, maxsize=%u\n", __func__, sc, maxsize));
436 
437 	if (sc->sc_params.maxsize != maxsize) {
438 		sc->sc_params.maxsize = maxsize;
439 		if (sc->sc_inbuf != NULL)
440 			free(sc->sc_inbuf, M_DEVBUF);
441 		for (i = 0; i < MAXFRAMES; i++)
442 			if (sc->sc_frames[i].buf != NULL)
443 				free(sc->sc_frames[i].buf, M_DEVBUF);
444 		if (sc->sc_params.maxsize != 0) {
445 			sc->sc_inbuf = malloc(sc->sc_params.maxsize+2,
446 					      M_DEVBUF, M_WAITOK);
447 			for (i = 0; i < MAXFRAMES; i++)
448 				sc->sc_frames[i].buf =
449 					malloc(sc->sc_params.maxsize,
450 					       M_DEVBUF, M_WAITOK);
451 		} else {
452 			sc->sc_inbuf = NULL;
453 			for (i = 0; i < MAXFRAMES; i++)
454 				sc->sc_frames[i].buf = NULL;
455 		}
456 	}
457 }
458 
459 void
irt_frame(struct irframet_softc * sc,u_char * tbuf,u_int len)460 irt_frame(struct irframet_softc *sc, u_char *tbuf, u_int len)
461 {
462 	DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
463 		 __func__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
464 
465 	if (sc->sc_inbuf == NULL) /* XXX happens if device is closed? */
466 		return;
467 	if (sc->sc_nframes >= MAXFRAMES) {
468 #ifdef IRFRAMET_DEBUG
469 		printf("%s: dropped frame\n", __func__);
470 #endif
471 		return;
472 	}
473 	if (sc->sc_frames[sc->sc_framei].buf == NULL)
474 		return;
475 	memcpy(sc->sc_frames[sc->sc_framei].buf, tbuf, len);
476 	sc->sc_frames[sc->sc_framei].len = len;
477 	sc->sc_framei = (sc->sc_framei+1) % MAXFRAMES;
478 	sc->sc_nframes++;
479 	if (sc->sc_state & IRT_RSLP) {
480 		sc->sc_state &= ~IRT_RSLP;
481 		DPRINTF(("%s: waking up reader\n", __func__));
482 		wakeup(sc->sc_frames);
483 	}
484 	selnotify(&sc->sc_rsel, 0, 0);
485 }
486 
487 void
irt_timeout(void * v)488 irt_timeout(void *v)
489 {
490 	struct irframet_softc *sc = v;
491 
492 #ifdef IRFRAMET_DEBUG
493 	if (sc->sc_framestate != FRAME_OUTSIDE)
494 		printf("%s: input frame timeout\n", __func__);
495 #endif
496 	sc->sc_framestate = FRAME_OUTSIDE;
497 }
498 
499 int
irframetinput(int c,struct tty * tp)500 irframetinput(int c, struct tty *tp)
501 {
502 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
503 
504 	c &= 0xff;
505 
506 #if IRFRAMET_DEBUG
507 	if (irframetdebug > 1)
508 		DPRINTF(("%s: tp=%p c=0x%02x\n", __func__, tp, c));
509 #endif
510 
511 	if (sc == NULL || tp != (struct tty *)sc->sc_tp)
512 		return (0);
513 
514 	if (sc->sc_inbuf == NULL)
515 		return (0);
516 
517 	switch (c) {
518 	case SIR_BOF:
519 		DPRINTF(("%s: BOF\n", __func__));
520 		sc->sc_framestate = FRAME_INSIDE;
521 		sc->sc_inchars = 0;
522 		sc->sc_inFCS = INITFCS;
523 		break;
524 	case SIR_EOF:
525 		DPRINTF(("%s: EOF state=%d inchars=%d fcs=0x%04x\n",
526 			 __func__,
527 			 sc->sc_framestate, sc->sc_inchars, sc->sc_inFCS));
528 		if (sc->sc_framestate == FRAME_INSIDE &&
529 		    sc->sc_inchars >= 4 && sc->sc_inFCS == GOODFCS) {
530 			irt_frame(sc, sc->sc_inbuf, sc->sc_inchars - 2);
531 		} else if (sc->sc_framestate != FRAME_OUTSIDE) {
532 #ifdef IRFRAMET_DEBUG
533 			printf("%s: malformed input frame\n", __func__);
534 #endif
535 		}
536 		sc->sc_framestate = FRAME_OUTSIDE;
537 		break;
538 	case SIR_CE:
539 		DPRINTF(("%s: CE\n", __func__));
540 		if (sc->sc_framestate == FRAME_INSIDE)
541 			sc->sc_framestate = FRAME_ESCAPE;
542 		break;
543 	default:
544 #if IRFRAMET_DEBUG
545 	if (irframetdebug > 1)
546 		DPRINTF(("%s: c=0x%02x, inchar=%d state=%d\n", __func__, c,
547 			 sc->sc_inchars, sc->sc_state));
548 #endif
549 		if (sc->sc_framestate != FRAME_OUTSIDE) {
550 			if (sc->sc_framestate == FRAME_ESCAPE) {
551 				sc->sc_framestate = FRAME_INSIDE;
552 				c ^= SIR_ESC_BIT;
553 			}
554 			if (sc->sc_inchars < sc->sc_params.maxsize + 2) {
555 				sc->sc_inbuf[sc->sc_inchars++] = c;
556 				sc->sc_inFCS = updateFCS(sc->sc_inFCS, c);
557 			} else {
558 				sc->sc_framestate = FRAME_OUTSIDE;
559 #ifdef IRFRAMET_DEBUG
560 				printf("%s: input frame overrun\n",
561 				       __func__);
562 #endif
563 			}
564 		}
565 		break;
566 	}
567 
568 #if 1
569 	if (sc->sc_framestate != FRAME_OUTSIDE) {
570 		callout_reset(&sc->sc_timeout, hz/20, irt_timeout, sc);
571 	}
572 #endif
573 
574 	return (0);
575 }
576 
577 
578 /*** irframe methods ***/
579 
580 int
irframet_open(void * h,int flag,int mode,struct lwp * l)581 irframet_open(void *h, int flag, int mode,
582     struct lwp *l)
583 {
584 	struct tty *tp = h;
585 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
586 
587 	DPRINTF(("%s: tp=%p\n", __func__, tp));
588 
589 	sc->sc_params.speed = 0;
590 	sc->sc_params.ebofs = IRDA_DEFAULT_EBOFS;
591 	sc->sc_params.maxsize = 0;
592 	sc->sc_framestate = FRAME_OUTSIDE;
593 	sc->sc_nframes = 0;
594 	sc->sc_framei = 0;
595 	sc->sc_frameo = 0;
596 
597 	return (0);
598 }
599 
600 int
irframet_close(void * h,int flag,int mode,struct lwp * l)601 irframet_close(void *h, int flag, int mode,
602     struct lwp *l)
603 {
604 	struct tty *tp = h;
605 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
606 	int s;
607 
608 	DPRINTF(("%s: tp=%p\n", __func__, tp));
609 
610 	/* line discipline was closed */
611 	if (sc == NULL)
612 		return (0);
613 
614 	callout_stop(&sc->sc_timeout);
615 	s = splir();
616 	irt_buffer(sc, 0);
617 	splx(s);
618 
619 	return (0);
620 }
621 
622 int
irframet_read(void * h,struct uio * uio,int flag)623 irframet_read(void *h, struct uio *uio, int flag)
624 {
625 	struct tty *tp = h;
626 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
627 	int error = 0;
628 	int s;
629 
630 	DPRINTF(("%s: resid=%zd, iovcnt=%d, offset=%ld\n",
631 		 __func__, uio->uio_resid, uio->uio_iovcnt,
632 		 (long)uio->uio_offset));
633 	DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
634 		 __func__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
635 
636 
637 	s = splir();
638 	while (sc->sc_nframes == 0) {
639 		if (flag & IO_NDELAY) {
640 			splx(s);
641 			return (EWOULDBLOCK);
642 		}
643 		sc->sc_state |= IRT_RSLP;
644 		DPRINTF(("%s: sleep\n", __func__));
645 		error = tsleep(sc->sc_frames, PZERO | PCATCH, "irtrd", 0);
646 		DPRINTF(("%s: woke, error=%d\n", __func__, error));
647 		if (error) {
648 			sc->sc_state &= ~IRT_RSLP;
649 			break;
650 		}
651 	}
652 
653 	/* Do just one frame transfer per read */
654 	if (!error) {
655 		if (uio->uio_resid < sc->sc_frames[sc->sc_frameo].len) {
656 			DPRINTF(("%s: uio buffer smaller than frame size "
657 				 "(%zd < %d)\n", __func__, uio->uio_resid,
658 				 sc->sc_frames[sc->sc_frameo].len));
659 			error = EINVAL;
660 		} else {
661 			DPRINTF(("%s: moving %d bytes\n", __func__,
662 				 sc->sc_frames[sc->sc_frameo].len));
663 			error = uiomove(sc->sc_frames[sc->sc_frameo].buf,
664 					sc->sc_frames[sc->sc_frameo].len, uio);
665 			DPRINTF(("%s: error=%d\n", __func__, error));
666 		}
667 		sc->sc_frameo = (sc->sc_frameo+1) % MAXFRAMES;
668 		sc->sc_nframes--;
669 	}
670 	splx(s);
671 
672 	return (error);
673 }
674 
675 int
irt_putc(struct tty * tp,int c)676 irt_putc(struct tty *tp, int c)
677 {
678 	int error;
679 
680 #if IRFRAMET_DEBUG
681 	if (irframetdebug > 3)
682 		DPRINTF(("%s: tp=%p c=0x%02x cc=%d\n", __func__, tp, c,
683 			 tp->t_outq.c_cc));
684 #endif
685 	if (tp->t_outq.c_cc > tp->t_hiwat) {
686 		irframetstart(tp);
687 		ttylock(tp);
688 		/*
689 		 * This can only occur if FLUSHO is set in t_lflag,
690 		 * or if ttstart/oproc is synchronous (or very fast).
691 		 */
692 		if (tp->t_outq.c_cc <= tp->t_hiwat) {
693 			ttyunlock(tp);
694 			goto go;
695 		}
696 		error = ttysleep(tp, &tp->t_outcv, true, 0);
697 		ttyunlock(tp);
698 		if (error)
699 			return (error);
700 	}
701  go:
702 	if (putc(c, &tp->t_outq) < 0) {
703 		printf("irframe: putc failed\n");
704 		return (EIO);
705 	}
706 	return (0);
707 }
708 
709 int
irframet_write(void * h,struct uio * uio,int flag)710 irframet_write(void *h, struct uio *uio, int flag)
711 {
712 	struct tty *tp = h;
713 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
714 	int n;
715 
716 	DPRINTF(("%s: resid=%zd, iovcnt=%d, offset=%ld\n",
717 		 __func__, uio->uio_resid, uio->uio_iovcnt,
718 		 (long)uio->uio_offset));
719 
720 	n = irda_sir_frame(sc->sc_buffer, sizeof(sc->sc_buffer), uio,
721 	    sc->sc_params.ebofs);
722 	if (n < 0) {
723 #ifdef IRFRAMET_DEBUG
724 		printf("%s: irda_sir_frame() error=%d\n", __func__, -n);
725 #endif
726 		return (-n);
727 	}
728 	return (irt_write_frame(tp, sc->sc_buffer, n));
729 }
730 
731 int
irt_write_frame(struct tty * tp,u_int8_t * tbuf,size_t len)732 irt_write_frame(struct tty *tp, u_int8_t *tbuf, size_t len)
733 {
734 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
735 	int error, i;
736 
737 	DPRINTF(("%s: tp=%p len=%zd\n", __func__, tp, len));
738 
739 	mutex_enter(&sc->sc_wr_lk);
740 	error = 0;
741 	for (i = 0; !error && i < len; i++)
742 		error = irt_putc(tp, tbuf[i]);
743 	mutex_exit(&sc->sc_wr_lk);
744 
745 	irframetstart(tp);
746 
747 	DPRINTF(("%s: done, error=%d\n", __func__, error));
748 
749 	return (error);
750 }
751 
752 int
irframet_poll(void * h,int events,struct lwp * l)753 irframet_poll(void *h, int events, struct lwp *l)
754 {
755 	struct tty *tp = h;
756 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
757 	int revents = 0;
758 	int s;
759 
760 	DPRINTF(("%s: sc=%p\n", __func__, sc));
761 
762 	s = splir();
763 	/* XXX is this a good check? */
764 	if (events & (POLLOUT | POLLWRNORM))
765 		if (tp->t_outq.c_cc <= tp->t_lowat)
766 			revents |= events & (POLLOUT | POLLWRNORM);
767 
768 	if (events & (POLLIN | POLLRDNORM)) {
769 		if (sc->sc_nframes > 0) {
770 			DPRINTF(("%s: have data\n", __func__));
771 			revents |= events & (POLLIN | POLLRDNORM);
772 		} else {
773 			DPRINTF(("%s: recording select\n", __func__));
774 			selrecord(l, &sc->sc_rsel);
775 		}
776 	}
777 	splx(s);
778 
779 	return (revents);
780 }
781 
782 static void
filt_irframetrdetach(struct knote * kn)783 filt_irframetrdetach(struct knote *kn)
784 {
785 	struct tty *tp = kn->kn_hook;
786 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
787 	int s;
788 
789 	s = splir();
790 	selremove_knote(&sc->sc_rsel, kn);
791 	splx(s);
792 }
793 
794 static int
filt_irframetread(struct knote * kn,long hint)795 filt_irframetread(struct knote *kn, long hint)
796 {
797 	struct tty *tp = kn->kn_hook;
798 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
799 
800 	kn->kn_data = sc->sc_nframes;
801 	return (kn->kn_data > 0);
802 }
803 
804 static void
filt_irframetwdetach(struct knote * kn)805 filt_irframetwdetach(struct knote *kn)
806 {
807 	struct tty *tp = kn->kn_hook;
808 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
809 	int s;
810 
811 	s = splir();
812 	selremove_knote(&sc->sc_wsel, kn);
813 	splx(s);
814 }
815 
816 static int
filt_irframetwrite(struct knote * kn,long hint)817 filt_irframetwrite(struct knote *kn, long hint)
818 {
819 	struct tty *tp = kn->kn_hook;
820 
821 	/* XXX double-check this */
822 
823 	if (tp->t_outq.c_cc <= tp->t_lowat) {
824 		kn->kn_data = tp->t_lowat - tp->t_outq.c_cc;
825 		return (1);
826 	}
827 
828 	kn->kn_data = 0;
829 	return (0);
830 }
831 
832 static const struct filterops irframetread_filtops = {
833 	.f_flags = FILTEROP_ISFD,
834 	.f_attach = NULL,
835 	.f_detach = filt_irframetrdetach,
836 	.f_event = filt_irframetread,
837 };
838 
839 static const struct filterops irframetwrite_filtops = {
840 	.f_flags = FILTEROP_ISFD,
841 	.f_attach = NULL,
842 	.f_detach = filt_irframetwdetach,
843 	.f_event = filt_irframetwrite,
844 };
845 
846 int
irframet_kqfilter(void * h,struct knote * kn)847 irframet_kqfilter(void *h, struct knote *kn)
848 {
849 	struct tty *tp = h;
850 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
851 	struct selinfo *sip;
852 	int s;
853 
854 	switch (kn->kn_filter) {
855 	case EVFILT_READ:
856 		sip = &sc->sc_rsel;
857 		kn->kn_fop = &irframetread_filtops;
858 		break;
859 	case EVFILT_WRITE:
860 		sip = &sc->sc_wsel;
861 		kn->kn_fop = &irframetwrite_filtops;
862 		break;
863 	default:
864 		return (EINVAL);
865 	}
866 
867 	kn->kn_hook = tp;
868 
869 	s = splir();
870 	selrecord_knote(sip, kn);
871 	splx(s);
872 
873 	return (0);
874 }
875 
876 int
irframet_set_params(void * h,struct irda_params * p)877 irframet_set_params(void *h, struct irda_params *p)
878 {
879 	struct tty *tp = h;
880 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
881 
882 	DPRINTF(("%s: tp=%p speed=%d ebofs=%d maxsize=%d\n",
883 		 __func__, tp, p->speed, p->ebofs, p->maxsize));
884 
885 	if (p->speed != sc->sc_params.speed) {
886 		/* Checked in irframe.c */
887 		mutex_enter(&sc->sc_wr_lk);
888 		irt_dongles[sc->sc_dongle].setspeed(tp, p->speed);
889 		mutex_exit(&sc->sc_wr_lk);
890 		sc->sc_params.speed = p->speed;
891 	}
892 
893 	/* Max size checked in irframe.c */
894 	sc->sc_params.ebofs = p->ebofs;
895 	irt_buffer(sc, p->maxsize);
896 	sc->sc_framestate = FRAME_OUTSIDE;
897 
898 	return (0);
899 }
900 
901 int
irframet_get_speeds(void * h,int * speeds)902 irframet_get_speeds(void *h, int *speeds)
903 {
904 	struct tty *tp = h;
905 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
906 
907 	DPRINTF(("%s: tp=%p\n", __func__, tp));
908 
909 	if (sc == NULL)		/* during attach */
910 		*speeds = IRDA_SPEEDS_SIR;
911 	else
912 		*speeds = irt_dongles[sc->sc_dongle].speedmask;
913 	return (0);
914 }
915 
916 int
irframet_get_turnarounds(void * h,int * turnarounds)917 irframet_get_turnarounds(void *h, int *turnarounds)
918 {
919 	struct tty *tp = h;
920 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
921 
922 	DPRINTF(("%s: tp=%p\n", __func__, tp));
923 
924 	*turnarounds = irt_dongles[sc->sc_dongle].turnmask;
925 	return (0);
926 }
927 
928 void
irt_ioctl(struct tty * tp,u_long cmd,void * arg)929 irt_ioctl(struct tty *tp, u_long cmd, void *arg)
930 {
931 	const struct cdevsw *cdev;
932 	int error __diagused;
933 	dev_t dev;
934 
935 	dev = tp->t_dev;
936 	cdev = cdevsw_lookup(dev);
937 	if (cdev != NULL)
938 		error = (*cdev->d_ioctl)(dev, cmd, arg, 0, curlwp);
939 	else
940 		error = ENXIO;
941 #ifdef DIAGNOSTIC
942 	if (error)
943 		printf("irt_ioctl: cmd=0x%08lx error=%d\n", cmd, error);
944 #endif
945 }
946 
947 void
irt_setspeed(struct tty * tp,u_int speed)948 irt_setspeed(struct tty *tp, u_int speed)
949 {
950 	struct termios tt;
951 
952 	irt_ioctl(tp, TIOCGETA,  &tt);
953 	tt.c_ispeed = tt.c_ospeed = speed;
954 	tt.c_cflag &= ~HUPCL;
955 	tt.c_cflag |= CLOCAL;
956 	irt_ioctl(tp, TIOCSETAF, &tt);
957 }
958 
959 void
irt_setline(struct tty * tp,u_int line)960 irt_setline(struct tty *tp, u_int line)
961 {
962 	int mline;
963 
964 	irt_ioctl(tp, TIOCMGET, &mline);
965 	mline &= ~(TIOCM_DTR | TIOCM_RTS);
966 	mline |= line;
967 	irt_ioctl(tp, TIOCMSET, (void *)&mline);
968 }
969 
970 void
irt_delay(struct tty * tp,u_int ms)971 irt_delay(struct tty *tp, u_int ms)
972 {
973 	if (cold)
974 		delay(ms * 1000);
975 	else
976 		tsleep(&irt_delay, PZERO, "irtdly", ms * hz / 1000 + 1);
977 
978 }
979 
980 /**********************************************************************
981  * No dongle
982  **********************************************************************/
983 void
irts_none(struct tty * tp,u_int speed)984 irts_none(struct tty *tp, u_int speed)
985 {
986 	irt_setspeed(tp, speed);
987 }
988 
989 /**********************************************************************
990  * Tekram
991  **********************************************************************/
992 #define TEKRAM_PW     0x10
993 
994 #define TEKRAM_115200 (TEKRAM_PW|0x00)
995 #define TEKRAM_57600  (TEKRAM_PW|0x01)
996 #define TEKRAM_38400  (TEKRAM_PW|0x02)
997 #define TEKRAM_19200  (TEKRAM_PW|0x03)
998 #define TEKRAM_9600   (TEKRAM_PW|0x04)
999 #define TEKRAM_2400   (TEKRAM_PW|0x08)
1000 
1001 #define TEKRAM_TV     (TEKRAM_PW|0x05)
1002 
1003 void
irts_tekram(struct tty * tp,u_int speed)1004 irts_tekram(struct tty *tp, u_int speed)
1005 {
1006 	int s;
1007 
1008 	irt_setspeed(tp, 9600);
1009 	irt_setline(tp, 0);
1010 	irt_delay(tp, 50);
1011 
1012 	irt_setline(tp, TIOCM_RTS);
1013 	irt_delay(tp, 1);
1014 
1015 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1016 	irt_delay(tp, 1);	/* 50 us */
1017 
1018 	irt_setline(tp, TIOCM_DTR);
1019 	irt_delay(tp, 1);	/* 7 us */
1020 
1021 	switch(speed) {
1022 	case 115200: s = TEKRAM_115200; break;
1023 	case 57600:  s = TEKRAM_57600; break;
1024 	case 38400:  s = TEKRAM_38400; break;
1025 	case 19200:  s = TEKRAM_19200; break;
1026 	case 2400:   s = TEKRAM_2400; break;
1027 	default:     s = TEKRAM_9600; break;
1028 	}
1029 	irt_putc(tp, s);
1030 	irframetstart(tp);
1031 
1032 	irt_delay(tp, 100);
1033 
1034 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1035 	if (speed != 9600)
1036 		irt_setspeed(tp, speed);
1037 	irt_delay(tp, 1);	/* 50 us */
1038 }
1039 
1040 /**********************************************************************
1041  * Jeteye
1042  **********************************************************************/
1043 void
irts_jeteye(struct tty * tp,u_int speed)1044 irts_jeteye(struct tty *tp, u_int speed)
1045 {
1046 	switch (speed) {
1047 	case 19200:
1048 		irt_setline(tp, TIOCM_DTR);
1049 		break;
1050 	case 115200:
1051 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1052 		break;
1053 	default: /*9600*/
1054 		irt_setline(tp, TIOCM_RTS);
1055 		break;
1056 	}
1057 	irt_setspeed(tp, speed);
1058 }
1059 
1060 /**********************************************************************
1061  * Actisys
1062  **********************************************************************/
1063 void
irts_actisys(struct tty * tp,u_int speed)1064 irts_actisys(struct tty *tp, u_int speed)
1065 {
1066 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
1067 	int pulses;
1068 
1069 	irt_setspeed(tp, speed);
1070 
1071 	switch(speed) {
1072 	case 19200:  pulses=1; break;
1073 	case 57600:  pulses=2; break;
1074 	case 115200: pulses=3; break;
1075 	case 38400:  pulses=4; break;
1076 	default: /* 9600 */ pulses=0; break;
1077 	}
1078 
1079 	if (sc->sc_dongle_private == 0) {
1080 		sc->sc_dongle_private = 1;
1081 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1082 		/*
1083 		 * Must wait at least 50ms after initial
1084 		 * power on to charge internal capacitor
1085 		 */
1086 		irt_delay(tp, 50);
1087 	}
1088 	irt_setline(tp, TIOCM_RTS);
1089 	delay(2);
1090 	for (;;) {
1091 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1092 		delay(2);
1093 		if (--pulses <= 0)
1094 			break;
1095 		irt_setline(tp, TIOCM_DTR);
1096 		delay(2);
1097 	}
1098 }
1099 
1100 /**********************************************************************
1101  * Litelink
1102  **********************************************************************/
1103 void
irts_litelink(struct tty * tp,u_int speed)1104 irts_litelink(struct tty *tp, u_int speed)
1105 {
1106 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
1107 	int pulses;
1108 
1109 	irt_setspeed(tp, speed);
1110 
1111 	switch(speed) {
1112 	case 57600:  pulses=1; break;
1113 	case 38400:  pulses=2; break;
1114 	case 19200:  pulses=3; break;
1115 	case 9600:   pulses=4; break;
1116 	default: /* 115200 */ pulses=0; break;
1117 	}
1118 
1119 	if (sc->sc_dongle_private == 0) {
1120 		sc->sc_dongle_private = 1;
1121 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1122 	}
1123 	irt_setline(tp, TIOCM_RTS);
1124 	irt_delay(tp, 1); /* 15 us */;
1125 	for (;;) {
1126 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1127 		irt_delay(tp, 1); /* 15 us */;
1128 		if (--pulses <= 0)
1129 			break;
1130 		irt_setline(tp, TIOCM_DTR);
1131 		irt_delay(tp, 1); /* 15 us */;
1132 	}
1133 }
1134 
1135 /**********************************************************************
1136  * Girbil
1137  **********************************************************************/
1138 /* Control register 1 */
1139 #define GIRBIL_TXEN      0x01 /* Enable transmitter */
1140 #define GIRBIL_RXEN      0x02 /* Enable receiver */
1141 #define GIRBIL_ECAN      0x04 /* Cancel self emitted data */
1142 #define GIRBIL_ECHO      0x08 /* Echo control characters */
1143 
1144 /* LED Current Register */
1145 #define GIRBIL_HIGH      0x20
1146 #define GIRBIL_MEDIUM    0x21
1147 #define GIRBIL_LOW       0x22
1148 
1149 /* Baud register */
1150 #define GIRBIL_2400      0x30
1151 #define GIRBIL_4800      0x31
1152 #define GIRBIL_9600      0x32
1153 #define GIRBIL_19200     0x33
1154 #define GIRBIL_38400     0x34
1155 #define GIRBIL_57600     0x35
1156 #define GIRBIL_115200    0x36
1157 
1158 /* Mode register */
1159 #define GIRBIL_IRDA      0x40
1160 #define GIRBIL_ASK       0x41
1161 
1162 /* Control register 2 */
1163 #define GIRBIL_LOAD      0x51 /* Load the new baud rate value */
1164 
1165 void
irts_girbil(struct tty * tp,u_int speed)1166 irts_girbil(struct tty *tp, u_int speed)
1167 {
1168 	int s;
1169 
1170 	irt_setspeed(tp, 9600);
1171 	irt_setline(tp, TIOCM_DTR);
1172 	irt_delay(tp, 5);
1173 	irt_setline(tp, TIOCM_RTS);
1174 	irt_delay(tp, 20);
1175 	switch(speed) {
1176 	case 115200: s = GIRBIL_115200; break;
1177 	case 57600:  s = GIRBIL_57600; break;
1178 	case 38400:  s = GIRBIL_38400; break;
1179 	case 19200:  s = GIRBIL_19200; break;
1180 	case 4800:   s = GIRBIL_4800; break;
1181 	case 2400:   s = GIRBIL_2400; break;
1182 	default:     s = GIRBIL_9600; break;
1183 	}
1184 	irt_putc(tp, GIRBIL_TXEN|GIRBIL_RXEN);
1185 	irt_putc(tp, s);
1186 	irt_putc(tp, GIRBIL_LOAD);
1187 	irframetstart(tp);
1188 	irt_delay(tp, 100);
1189 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1190 	if (speed != 9600)
1191 		irt_setspeed(tp, speed);
1192 }
1193