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