irframe_tty.c revision 1.37 1 /* $NetBSD: irframe_tty.c,v 1.37 2006/10/01 19:28:43 elad 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.37 2006/10/01 19:28:43 elad 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, struct lwp *l)
475 {
476 struct tty *tp = h;
477 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
478
479 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
480
481 sc->sc_params.speed = 0;
482 sc->sc_params.ebofs = IRDA_DEFAULT_EBOFS;
483 sc->sc_params.maxsize = 0;
484 sc->sc_framestate = FRAME_OUTSIDE;
485 sc->sc_nframes = 0;
486 sc->sc_framei = 0;
487 sc->sc_frameo = 0;
488 callout_init(&sc->sc_timeout);
489 lockinit(&sc->sc_wr_lk, PZERO, "irfrtl", 0, 0);
490
491 return (0);
492 }
493
494 int
495 irframet_close(void *h, int flag, int mode, struct lwp *l)
496 {
497 struct tty *tp = h;
498 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
499 int i, s;
500
501 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
502
503 callout_stop(&sc->sc_timeout);
504 s = splir();
505 if (sc->sc_inbuf != NULL) {
506 free(sc->sc_inbuf, M_DEVBUF);
507 sc->sc_inbuf = NULL;
508 }
509 for (i = 0; i < MAXFRAMES; i++) {
510 if (sc->sc_frames[i].buf != NULL) {
511 free(sc->sc_frames[i].buf, M_DEVBUF);
512 sc->sc_frames[i].buf = NULL;
513 }
514 }
515 splx(s);
516
517 return (0);
518 }
519
520 int
521 irframet_read(void *h, struct uio *uio, int flag)
522 {
523 struct tty *tp = h;
524 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
525 int error = 0;
526 int s;
527
528 DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
529 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
530 (long)uio->uio_offset));
531 DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
532 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
533
534
535 s = splir();
536 while (sc->sc_nframes == 0) {
537 if (flag & IO_NDELAY) {
538 splx(s);
539 return (EWOULDBLOCK);
540 }
541 sc->sc_state |= IRT_RSLP;
542 DPRINTF(("%s: sleep\n", __FUNCTION__));
543 error = tsleep(sc->sc_frames, PZERO | PCATCH, "irtrd", 0);
544 DPRINTF(("%s: woke, error=%d\n", __FUNCTION__, error));
545 if (error) {
546 sc->sc_state &= ~IRT_RSLP;
547 break;
548 }
549 }
550
551 /* Do just one frame transfer per read */
552 if (!error) {
553 if (uio->uio_resid < sc->sc_frames[sc->sc_frameo].len) {
554 DPRINTF(("%s: uio buffer smaller than frame size "
555 "(%d < %d)\n", __FUNCTION__, uio->uio_resid,
556 sc->sc_frames[sc->sc_frameo].len));
557 error = EINVAL;
558 } else {
559 DPRINTF(("%s: moving %d bytes\n", __FUNCTION__,
560 sc->sc_frames[sc->sc_frameo].len));
561 error = uiomove(sc->sc_frames[sc->sc_frameo].buf,
562 sc->sc_frames[sc->sc_frameo].len, uio);
563 DPRINTF(("%s: error=%d\n", __FUNCTION__, error));
564 }
565 sc->sc_frameo = (sc->sc_frameo+1) % MAXFRAMES;
566 sc->sc_nframes--;
567 }
568 splx(s);
569
570 return (error);
571 }
572
573 int
574 irt_putc(struct tty *tp, int c)
575 {
576 int s;
577 int error;
578
579 #if IRFRAMET_DEBUG
580 if (irframetdebug > 3)
581 DPRINTF(("%s: tp=%p c=0x%02x cc=%d\n", __FUNCTION__, tp, c,
582 tp->t_outq.c_cc));
583 #endif
584 if (tp->t_outq.c_cc > tp->t_hiwat) {
585 irframetstart(tp);
586 s = spltty();
587 /*
588 * This can only occur if FLUSHO is set in t_lflag,
589 * or if ttstart/oproc is synchronous (or very fast).
590 */
591 if (tp->t_outq.c_cc <= tp->t_hiwat) {
592 splx(s);
593 goto go;
594 }
595 SET(tp->t_state, TS_ASLEEP);
596 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
597 splx(s);
598 if (error)
599 return (error);
600 }
601 go:
602 if (putc(c, &tp->t_outq) < 0) {
603 printf("irframe: putc failed\n");
604 return (EIO);
605 }
606 return (0);
607 }
608
609 int
610 irframet_write(void *h, struct uio *uio, int flag)
611 {
612 struct tty *tp = h;
613 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
614 int n;
615
616 DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
617 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
618 (long)uio->uio_offset));
619
620 n = irda_sir_frame(sc->sc_buffer, sizeof(sc->sc_buffer), uio,
621 sc->sc_params.ebofs);
622 if (n < 0) {
623 #ifdef IRFRAMET_DEBUG
624 printf("%s: irda_sir_frame() error=%d\n", __FUNCTION__, -n);
625 #endif
626 return (-n);
627 }
628 return (irt_write_frame(tp, sc->sc_buffer, n));
629 }
630
631 int
632 irt_write_frame(struct tty *tp, u_int8_t *tbuf, size_t len)
633 {
634 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
635 int error, i;
636
637 DPRINTF(("%s: tp=%p len=%d\n", __FUNCTION__, tp, len));
638
639 lockmgr(&sc->sc_wr_lk, LK_EXCLUSIVE, NULL);
640 error = 0;
641 for (i = 0; !error && i < len; i++)
642 error = irt_putc(tp, tbuf[i]);
643 lockmgr(&sc->sc_wr_lk, LK_RELEASE, NULL);
644
645 irframetstart(tp);
646
647 DPRINTF(("%s: done, error=%d\n", __FUNCTION__, error));
648
649 return (error);
650 }
651
652 int
653 irframet_poll(void *h, int events, struct lwp *l)
654 {
655 struct tty *tp = h;
656 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
657 int revents = 0;
658 int s;
659
660 DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
661
662 s = splir();
663 /* XXX is this a good check? */
664 if (events & (POLLOUT | POLLWRNORM))
665 if (tp->t_outq.c_cc <= tp->t_lowat)
666 revents |= events & (POLLOUT | POLLWRNORM);
667
668 if (events & (POLLIN | POLLRDNORM)) {
669 if (sc->sc_nframes > 0) {
670 DPRINTF(("%s: have data\n", __FUNCTION__));
671 revents |= events & (POLLIN | POLLRDNORM);
672 } else {
673 DPRINTF(("%s: recording select\n", __FUNCTION__));
674 selrecord(l, &sc->sc_rsel);
675 }
676 }
677 splx(s);
678
679 return (revents);
680 }
681
682 static void
683 filt_irframetrdetach(struct knote *kn)
684 {
685 struct tty *tp = kn->kn_hook;
686 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
687 int s;
688
689 s = splir();
690 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
691 splx(s);
692 }
693
694 static int
695 filt_irframetread(struct knote *kn, long hint)
696 {
697 struct tty *tp = kn->kn_hook;
698 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
699
700 kn->kn_data = sc->sc_nframes;
701 return (kn->kn_data > 0);
702 }
703
704 static void
705 filt_irframetwdetach(struct knote *kn)
706 {
707 struct tty *tp = kn->kn_hook;
708 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
709 int s;
710
711 s = splir();
712 SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
713 splx(s);
714 }
715
716 static int
717 filt_irframetwrite(struct knote *kn, long hint)
718 {
719 struct tty *tp = kn->kn_hook;
720
721 /* XXX double-check this */
722
723 if (tp->t_outq.c_cc <= tp->t_lowat) {
724 kn->kn_data = tp->t_lowat - tp->t_outq.c_cc;
725 return (1);
726 }
727
728 kn->kn_data = 0;
729 return (0);
730 }
731
732 static const struct filterops irframetread_filtops =
733 { 1, NULL, filt_irframetrdetach, filt_irframetread };
734 static const struct filterops irframetwrite_filtops =
735 { 1, NULL, filt_irframetwdetach, filt_irframetwrite };
736
737 int
738 irframet_kqfilter(void *h, struct knote *kn)
739 {
740 struct tty *tp = h;
741 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
742 struct klist *klist;
743 int s;
744
745 switch (kn->kn_filter) {
746 case EVFILT_READ:
747 klist = &sc->sc_rsel.sel_klist;
748 kn->kn_fop = &irframetread_filtops;
749 break;
750 case EVFILT_WRITE:
751 klist = &sc->sc_wsel.sel_klist;
752 kn->kn_fop = &irframetwrite_filtops;
753 break;
754 default:
755 return (1);
756 }
757
758 kn->kn_hook = tp;
759
760 s = splir();
761 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
762 splx(s);
763
764 return (0);
765 }
766
767 int
768 irframet_set_params(void *h, struct irda_params *p)
769 {
770 struct tty *tp = h;
771 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
772 int i;
773
774 DPRINTF(("%s: tp=%p speed=%d ebofs=%d maxsize=%d\n",
775 __FUNCTION__, tp, p->speed, p->ebofs, p->maxsize));
776
777 if (p->speed != sc->sc_params.speed) {
778 /* Checked in irframe.c */
779 lockmgr(&sc->sc_wr_lk, LK_EXCLUSIVE, NULL);
780 irt_dongles[sc->sc_dongle].setspeed(tp, p->speed);
781 lockmgr(&sc->sc_wr_lk, LK_RELEASE, NULL);
782 sc->sc_params.speed = p->speed;
783 }
784
785 /* Max size checked in irframe.c */
786 sc->sc_params.ebofs = p->ebofs;
787 /* Max size checked in irframe.c */
788 if (sc->sc_params.maxsize != p->maxsize) {
789 sc->sc_params.maxsize = p->maxsize;
790 if (sc->sc_inbuf != NULL)
791 free(sc->sc_inbuf, M_DEVBUF);
792 for (i = 0; i < MAXFRAMES; i++)
793 if (sc->sc_frames[i].buf != NULL)
794 free(sc->sc_frames[i].buf, M_DEVBUF);
795 if (sc->sc_params.maxsize != 0) {
796 sc->sc_inbuf = malloc(sc->sc_params.maxsize+2,
797 M_DEVBUF, M_WAITOK);
798 for (i = 0; i < MAXFRAMES; i++)
799 sc->sc_frames[i].buf =
800 malloc(sc->sc_params.maxsize,
801 M_DEVBUF, M_WAITOK);
802 } else {
803 sc->sc_inbuf = NULL;
804 for (i = 0; i < MAXFRAMES; i++)
805 sc->sc_frames[i].buf = NULL;
806 }
807 }
808 sc->sc_framestate = FRAME_OUTSIDE;
809
810 return (0);
811 }
812
813 int
814 irframet_get_speeds(void *h, int *speeds)
815 {
816 struct tty *tp = h;
817 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
818
819 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
820
821 if (sc == NULL) /* during attach */
822 *speeds = IRDA_SPEEDS_SIR;
823 else
824 *speeds = irt_dongles[sc->sc_dongle].speedmask;
825 return (0);
826 }
827
828 int
829 irframet_get_turnarounds(void *h, int *turnarounds)
830 {
831 struct tty *tp = h;
832 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
833
834 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
835
836 *turnarounds = irt_dongles[sc->sc_dongle].turnmask;
837 return (0);
838 }
839
840 void
841 irt_ioctl(struct tty *tp, u_long cmd, void *arg)
842 {
843 const struct cdevsw *cdev;
844 int error;
845 dev_t dev;
846
847 dev = tp->t_dev;
848 cdev = cdevsw_lookup(dev);
849 if (cdev != NULL)
850 error = (*cdev->d_ioctl)(dev, cmd, arg, 0, curlwp);
851 else
852 error = ENXIO;
853 #ifdef DIAGNOSTIC
854 if (error)
855 printf("irt_ioctl: cmd=0x%08lx error=%d\n", cmd, error);
856 #endif
857 }
858
859 void
860 irt_setspeed(struct tty *tp, u_int speed)
861 {
862 struct termios tt;
863
864 irt_ioctl(tp, TIOCGETA, &tt);
865 tt.c_ispeed = tt.c_ospeed = speed;
866 tt.c_cflag &= ~HUPCL;
867 tt.c_cflag |= CLOCAL;
868 irt_ioctl(tp, TIOCSETAF, &tt);
869 }
870
871 void
872 irt_setline(struct tty *tp, u_int line)
873 {
874 int mline;
875
876 irt_ioctl(tp, TIOCMGET, &mline);
877 mline &= ~(TIOCM_DTR | TIOCM_RTS);
878 mline |= line;
879 irt_ioctl(tp, TIOCMSET, (caddr_t)&mline);
880 }
881
882 void
883 irt_delay(struct tty *tp, u_int ms)
884 {
885 if (cold)
886 delay(ms * 1000);
887 else
888 tsleep(&irt_delay, PZERO, "irtdly", ms * hz / 1000 + 1);
889
890 }
891
892 /**********************************************************************
893 * No dongle
894 **********************************************************************/
895 void
896 irts_none(struct tty *tp, u_int speed)
897 {
898 irt_setspeed(tp, speed);
899 }
900
901 /**********************************************************************
902 * Tekram
903 **********************************************************************/
904 #define TEKRAM_PW 0x10
905
906 #define TEKRAM_115200 (TEKRAM_PW|0x00)
907 #define TEKRAM_57600 (TEKRAM_PW|0x01)
908 #define TEKRAM_38400 (TEKRAM_PW|0x02)
909 #define TEKRAM_19200 (TEKRAM_PW|0x03)
910 #define TEKRAM_9600 (TEKRAM_PW|0x04)
911 #define TEKRAM_2400 (TEKRAM_PW|0x08)
912
913 #define TEKRAM_TV (TEKRAM_PW|0x05)
914
915 void
916 irts_tekram(struct tty *tp, u_int speed)
917 {
918 int s;
919
920 irt_setspeed(tp, 9600);
921 irt_setline(tp, 0);
922 irt_delay(tp, 50);
923
924 irt_setline(tp, TIOCM_RTS);
925 irt_delay(tp, 1);
926
927 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
928 irt_delay(tp, 1); /* 50 us */
929
930 irt_setline(tp, TIOCM_DTR);
931 irt_delay(tp, 1); /* 7 us */
932
933 switch(speed) {
934 case 115200: s = TEKRAM_115200; break;
935 case 57600: s = TEKRAM_57600; break;
936 case 38400: s = TEKRAM_38400; break;
937 case 19200: s = TEKRAM_19200; break;
938 case 2400: s = TEKRAM_2400; break;
939 default: s = TEKRAM_9600; break;
940 }
941 irt_putc(tp, s);
942 irframetstart(tp);
943
944 irt_delay(tp, 100);
945
946 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
947 if (speed != 9600)
948 irt_setspeed(tp, speed);
949 irt_delay(tp, 1); /* 50 us */
950 }
951
952 /**********************************************************************
953 * Jeteye
954 **********************************************************************/
955 void
956 irts_jeteye(struct tty *tp, u_int speed)
957 {
958 switch (speed) {
959 case 19200:
960 irt_setline(tp, TIOCM_DTR);
961 break;
962 case 115200:
963 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
964 break;
965 default: /*9600*/
966 irt_setline(tp, TIOCM_RTS);
967 break;
968 }
969 irt_setspeed(tp, speed);
970 }
971
972 /**********************************************************************
973 * Actisys
974 **********************************************************************/
975 void
976 irts_actisys(struct tty *tp, u_int speed)
977 {
978 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
979 int pulses;
980
981 irt_setspeed(tp, speed);
982
983 switch(speed) {
984 case 19200: pulses=1; break;
985 case 57600: pulses=2; break;
986 case 115200: pulses=3; break;
987 case 38400: pulses=4; break;
988 default: /* 9600 */ pulses=0; break;
989 }
990
991 if (sc->sc_dongle_private == 0) {
992 sc->sc_dongle_private = 1;
993 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
994 /*
995 * Must wait at least 50ms after initial
996 * power on to charge internal capacitor
997 */
998 irt_delay(tp, 50);
999 }
1000 irt_setline(tp, TIOCM_RTS);
1001 delay(2);
1002 for (;;) {
1003 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1004 delay(2);
1005 if (--pulses <= 0)
1006 break;
1007 irt_setline(tp, TIOCM_DTR);
1008 delay(2);
1009 }
1010 }
1011
1012 /**********************************************************************
1013 * Litelink
1014 **********************************************************************/
1015 void
1016 irts_litelink(struct tty *tp, u_int speed)
1017 {
1018 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
1019 int pulses;
1020
1021 irt_setspeed(tp, speed);
1022
1023 switch(speed) {
1024 case 57600: pulses=1; break;
1025 case 38400: pulses=2; break;
1026 case 19200: pulses=3; break;
1027 case 9600: pulses=4; break;
1028 default: /* 115200 */ pulses=0; break;
1029 }
1030
1031 if (sc->sc_dongle_private == 0) {
1032 sc->sc_dongle_private = 1;
1033 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1034 }
1035 irt_setline(tp, TIOCM_RTS);
1036 irt_delay(tp, 1); /* 15 us */;
1037 for (;;) {
1038 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1039 irt_delay(tp, 1); /* 15 us */;
1040 if (--pulses <= 0)
1041 break;
1042 irt_setline(tp, TIOCM_DTR);
1043 irt_delay(tp, 1); /* 15 us */;
1044 }
1045 }
1046
1047 /**********************************************************************
1048 * Girbil
1049 **********************************************************************/
1050 /* Control register 1 */
1051 #define GIRBIL_TXEN 0x01 /* Enable transmitter */
1052 #define GIRBIL_RXEN 0x02 /* Enable receiver */
1053 #define GIRBIL_ECAN 0x04 /* Cancel self emmited data */
1054 #define GIRBIL_ECHO 0x08 /* Echo control characters */
1055
1056 /* LED Current Register */
1057 #define GIRBIL_HIGH 0x20
1058 #define GIRBIL_MEDIUM 0x21
1059 #define GIRBIL_LOW 0x22
1060
1061 /* Baud register */
1062 #define GIRBIL_2400 0x30
1063 #define GIRBIL_4800 0x31
1064 #define GIRBIL_9600 0x32
1065 #define GIRBIL_19200 0x33
1066 #define GIRBIL_38400 0x34
1067 #define GIRBIL_57600 0x35
1068 #define GIRBIL_115200 0x36
1069
1070 /* Mode register */
1071 #define GIRBIL_IRDA 0x40
1072 #define GIRBIL_ASK 0x41
1073
1074 /* Control register 2 */
1075 #define GIRBIL_LOAD 0x51 /* Load the new baud rate value */
1076
1077 void
1078 irts_girbil(struct tty *tp, u_int speed)
1079 {
1080 int s;
1081
1082 irt_setspeed(tp, 9600);
1083 irt_setline(tp, TIOCM_DTR);
1084 irt_delay(tp, 5);
1085 irt_setline(tp, TIOCM_RTS);
1086 irt_delay(tp, 20);
1087 switch(speed) {
1088 case 115200: s = GIRBIL_115200; break;
1089 case 57600: s = GIRBIL_57600; break;
1090 case 38400: s = GIRBIL_38400; break;
1091 case 19200: s = GIRBIL_19200; break;
1092 case 4800: s = GIRBIL_4800; break;
1093 case 2400: s = GIRBIL_2400; break;
1094 default: s = GIRBIL_9600; break;
1095 }
1096 irt_putc(tp, GIRBIL_TXEN|GIRBIL_RXEN);
1097 irt_putc(tp, s);
1098 irt_putc(tp, GIRBIL_LOAD);
1099 irframetstart(tp);
1100 irt_delay(tp, 100);
1101 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1102 if (speed != 9600)
1103 irt_setspeed(tp, speed);
1104 }
1105