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