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