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