irframe_tty.c revision 1.18 1 /* $NetBSD: irframe_tty.c,v 1.18 2001/12/20 11:30:13 augustss 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/param.h>
51 #include <sys/proc.h>
52 #include <sys/ioctl.h>
53 #include <sys/tty.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/conf.h>
58 #include <sys/systm.h>
59 #include <sys/device.h>
60 #include <sys/file.h>
61 #include <sys/vnode.h>
62 #include <sys/poll.h>
63
64 #include <dev/ir/ir.h>
65 #include <dev/ir/sir.h>
66 #include <dev/ir/irdaio.h>
67 #include <dev/ir/irframevar.h>
68
69 /* Macros to clear/set/test flags. */
70 #define SET(t, f) (t) |= (f)
71 #define CLR(t, f) (t) &= ~(f)
72 #define ISSET(t, f) ((t) & (f))
73
74 #ifdef IRFRAMET_DEBUG
75 #define DPRINTF(x) if (irframetdebug) printf x
76 #define Static
77 int irframetdebug = 0;
78 #else
79 #define DPRINTF(x)
80 #define Static static
81 #endif
82
83 /*****/
84
85 /* Max size with framing. */
86 #define MAX_IRDA_FRAME (2*IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + 4)
87
88 struct frame {
89 u_char *buf;
90 u_int len;
91 };
92 #define MAXFRAMES 8
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 struct lock sc_wr_lk;
108
109 struct irda_params sc_params;
110
111 u_char* sc_inbuf;
112 int sc_framestate;
113 #define FRAME_OUTSIDE 0
114 #define FRAME_INSIDE 1
115 #define FRAME_ESCAPE 2
116 int sc_inchars;
117 int sc_inFCS;
118 struct callout sc_timeout;
119
120 u_int sc_nframes;
121 u_int sc_framei;
122 u_int sc_frameo;
123 struct frame sc_frames[MAXFRAMES];
124 struct selinfo sc_rsel;
125 };
126
127 /* line discipline methods */
128 int irframetopen(dev_t dev, struct tty *tp);
129 int irframetclose(struct tty *tp, int flag);
130 int irframetioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
131 struct proc *);
132 int irframetinput(int c, struct tty *tp);
133 int irframetstart(struct tty *tp);
134
135 /* pseudo device init */
136 void irframettyattach(int);
137
138 /* irframe methods */
139 Static int irframet_open(void *h, int flag, int mode, struct proc *p);
140 Static int irframet_close(void *h, int flag, int mode, struct proc *p);
141 Static int irframet_read(void *h, struct uio *uio, int flag);
142 Static int irframet_write(void *h, struct uio *uio, int flag);
143 Static int irframet_poll(void *h, int events, struct proc *p);
144 Static int irframet_set_params(void *h, struct irda_params *params);
145 Static int irframet_get_speeds(void *h, int *speeds);
146 Static int irframet_get_turnarounds(void *h, int *times);
147
148 /* internal */
149 Static int irt_write_frame(struct tty *tp, u_int8_t *buf, size_t len);
150 Static int irt_putc(struct tty *tp, int c);
151 Static void irt_frame(struct irframet_softc *sc, u_char *buf, u_int len);
152 Static void irt_timeout(void *v);
153 Static void irt_ioctl(struct tty *tp, u_long cmd, void *arg);
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 = -1;
309 break;
310 }
311
312 return (error);
313 }
314
315 /*
316 * Start output on async tty interface.
317 */
318 int
319 irframetstart(struct tty *tp)
320 {
321 /*struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;*/
322 int s;
323
324 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
325
326 s = spltty();
327 if (tp->t_oproc != NULL)
328 (*tp->t_oproc)(tp);
329 splx(s);
330
331 return (0);
332 }
333
334 void
335 irt_frame(struct irframet_softc *sc, u_char *buf, u_int len)
336 {
337 DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
338 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
339
340 if (sc->sc_nframes >= MAXFRAMES) {
341 #ifdef IRFRAMET_DEBUG
342 printf("%s: dropped frame\n", __FUNCTION__);
343 #endif
344 return;
345 }
346 if (sc->sc_frames[sc->sc_framei].buf == NULL)
347 return;
348 memcpy(sc->sc_frames[sc->sc_framei].buf, buf, len);
349 sc->sc_frames[sc->sc_framei].len = len;
350 sc->sc_framei = (sc->sc_framei+1) % MAXFRAMES;
351 sc->sc_nframes++;
352 if (sc->sc_state & IRT_RSLP) {
353 sc->sc_state &= ~IRT_RSLP;
354 DPRINTF(("%s: waking up reader\n", __FUNCTION__));
355 wakeup(sc->sc_frames);
356 }
357 selwakeup(&sc->sc_rsel);
358 }
359
360 void
361 irt_timeout(void *v)
362 {
363 struct irframet_softc *sc = v;
364
365 #ifdef IRFRAMET_DEBUG
366 if (sc->sc_framestate != FRAME_OUTSIDE)
367 printf("%s: input frame timeout\n", __FUNCTION__);
368 #endif
369 sc->sc_framestate = FRAME_OUTSIDE;
370 }
371
372 int
373 irframetinput(int c, struct tty *tp)
374 {
375 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
376
377 c &= 0xff;
378
379 #if IRFRAMET_DEBUG
380 if (irframetdebug > 1)
381 DPRINTF(("%s: tp=%p c=0x%02x\n", __FUNCTION__, tp, c));
382 #endif
383
384 if (sc == NULL || tp != (struct tty *)sc->sc_tp)
385 return (0);
386
387 if (sc->sc_inbuf == NULL)
388 return (0);
389
390 switch (c) {
391 case SIR_BOF:
392 DPRINTF(("%s: BOF\n", __FUNCTION__));
393 sc->sc_framestate = FRAME_INSIDE;
394 sc->sc_inchars = 0;
395 sc->sc_inFCS = INITFCS;
396 break;
397 case SIR_EOF:
398 DPRINTF(("%s: EOF state=%d inchars=%d fcs=0x%04x\n",
399 __FUNCTION__,
400 sc->sc_framestate, sc->sc_inchars, sc->sc_inFCS));
401 if (sc->sc_framestate == FRAME_INSIDE &&
402 sc->sc_inchars >= 4 && sc->sc_inFCS == GOODFCS) {
403 irt_frame(sc, sc->sc_inbuf, sc->sc_inchars - 2);
404 } else if (sc->sc_framestate != FRAME_OUTSIDE) {
405 #ifdef IRFRAMET_DEBUG
406 printf("%s: malformed input frame\n", __FUNCTION__);
407 #endif
408 }
409 sc->sc_framestate = FRAME_OUTSIDE;
410 break;
411 case SIR_CE:
412 DPRINTF(("%s: CE\n", __FUNCTION__));
413 if (sc->sc_framestate == FRAME_INSIDE)
414 sc->sc_framestate = FRAME_ESCAPE;
415 break;
416 default:
417 #if IRFRAMET_DEBUG
418 if (irframetdebug > 1)
419 DPRINTF(("%s: c=0x%02x, inchar=%d state=%d\n", __FUNCTION__, c,
420 sc->sc_inchars, sc->sc_state));
421 #endif
422 if (sc->sc_framestate != FRAME_OUTSIDE) {
423 if (sc->sc_framestate == FRAME_ESCAPE) {
424 sc->sc_framestate = FRAME_INSIDE;
425 c ^= SIR_ESC_BIT;
426 }
427 if (sc->sc_inchars < sc->sc_params.maxsize + 2) {
428 sc->sc_inbuf[sc->sc_inchars++] = c;
429 sc->sc_inFCS = updateFCS(sc->sc_inFCS, c);
430 } else {
431 sc->sc_framestate = FRAME_OUTSIDE;
432 #ifdef IRFRAMET_DEBUG
433 printf("%s: input frame overrun\n",
434 __FUNCTION__);
435 #endif
436 }
437 }
438 break;
439 }
440
441 #if 1
442 if (sc->sc_framestate != FRAME_OUTSIDE) {
443 callout_reset(&sc->sc_timeout, hz/20, irt_timeout, sc);
444 }
445 #endif
446
447 return (0);
448 }
449
450
451 /*** irframe methods ***/
452
453 int
454 irframet_open(void *h, int flag, int mode, struct proc *p)
455 {
456 struct tty *tp = h;
457 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
458
459 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
460
461 sc->sc_params.speed = 0;
462 sc->sc_params.ebofs = IRDA_DEFAULT_EBOFS;
463 sc->sc_params.maxsize = 0;
464 sc->sc_framestate = FRAME_OUTSIDE;
465 sc->sc_nframes = 0;
466 sc->sc_framei = 0;
467 sc->sc_frameo = 0;
468 callout_init(&sc->sc_timeout);
469 lockinit(&sc->sc_wr_lk, PZERO, "irfrtl", 0, 0);
470
471 return (0);
472 }
473
474 int
475 irframet_close(void *h, int flag, int mode, struct proc *p)
476 {
477 struct tty *tp = h;
478 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
479 int i, s;
480
481 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
482
483 callout_stop(&sc->sc_timeout);
484 s = splir();
485 if (sc->sc_inbuf != NULL) {
486 free(sc->sc_inbuf, M_DEVBUF);
487 sc->sc_inbuf = NULL;
488 }
489 for (i = 0; i < MAXFRAMES; i++) {
490 if (sc->sc_frames[i].buf != NULL) {
491 free(sc->sc_frames[i].buf, M_DEVBUF);
492 sc->sc_frames[i].buf = NULL;
493 }
494 }
495 splx(s);
496
497 return (0);
498 }
499
500 int
501 irframet_read(void *h, struct uio *uio, int flag)
502 {
503 struct tty *tp = h;
504 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
505 int error = 0;
506 int s;
507
508 DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
509 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
510 (long)uio->uio_offset));
511 DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
512 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
513
514
515 s = splir();
516 while (sc->sc_nframes == 0) {
517 if (flag & IO_NDELAY) {
518 splx(s);
519 return (EWOULDBLOCK);
520 }
521 sc->sc_state |= IRT_RSLP;
522 DPRINTF(("%s: sleep\n", __FUNCTION__));
523 error = tsleep(sc->sc_frames, PZERO | PCATCH, "irtrd", 0);
524 DPRINTF(("%s: woke, error=%d\n", __FUNCTION__, error));
525 if (error) {
526 sc->sc_state &= ~IRT_RSLP;
527 break;
528 }
529 }
530
531 /* Do just one frame transfer per read */
532 if (!error) {
533 if (uio->uio_resid < sc->sc_frames[sc->sc_frameo].len) {
534 DPRINTF(("%s: uio buffer smaller than frame size "
535 "(%d < %d)\n", __FUNCTION__, uio->uio_resid,
536 sc->sc_frames[sc->sc_frameo].len));
537 error = EINVAL;
538 } else {
539 DPRINTF(("%s: moving %d bytes\n", __FUNCTION__,
540 sc->sc_frames[sc->sc_frameo].len));
541 error = uiomove(sc->sc_frames[sc->sc_frameo].buf,
542 sc->sc_frames[sc->sc_frameo].len, uio);
543 DPRINTF(("%s: error=%d\n", __FUNCTION__, error));
544 }
545 sc->sc_frameo = (sc->sc_frameo+1) % MAXFRAMES;
546 sc->sc_nframes--;
547 }
548 splx(s);
549
550 return (error);
551 }
552
553 int
554 irt_putc(struct tty *tp, int c)
555 {
556 int s;
557 int error;
558
559 #if IRFRAMET_DEBUG
560 if (irframetdebug > 3)
561 DPRINTF(("%s: tp=%p c=0x%02x cc=%d\n", __FUNCTION__, tp, c,
562 tp->t_outq.c_cc));
563 #endif
564 if (tp->t_outq.c_cc > tp->t_hiwat) {
565 irframetstart(tp);
566 s = spltty();
567 /*
568 * This can only occur if FLUSHO is set in t_lflag,
569 * or if ttstart/oproc is synchronous (or very fast).
570 */
571 if (tp->t_outq.c_cc <= tp->t_hiwat) {
572 splx(s);
573 goto go;
574 }
575 SET(tp->t_state, TS_ASLEEP);
576 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
577 splx(s);
578 if (error)
579 return (error);
580 }
581 go:
582 if (putc(c, &tp->t_outq) < 0) {
583 printf("irframe: putc failed\n");
584 return (EIO);
585 }
586 return (0);
587 }
588
589 int
590 irframet_write(void *h, struct uio *uio, int flag)
591 {
592 struct tty *tp = h;
593 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
594 u_int8_t buf[MAX_IRDA_FRAME];
595 int n;
596
597 DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
598 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
599 (long)uio->uio_offset));
600
601 n = irda_sir_frame(buf, MAX_IRDA_FRAME, uio, sc->sc_params.ebofs);
602 if (n < 0) {
603 #ifdef IRFRAMET_DEBUG
604 printf("%s: irda_sir_frame() error=%d\n", __FUNCTION__, -n);
605 #endif
606 return (-n);
607 }
608 return (irt_write_frame(tp, buf, n));
609 }
610
611 int
612 irt_write_frame(struct tty *tp, u_int8_t *buf, size_t len)
613 {
614 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
615 int error, i;
616
617 DPRINTF(("%s: tp=%p len=%d\n", __FUNCTION__, tp, len));
618
619 lockmgr(&sc->sc_wr_lk, LK_EXCLUSIVE, NULL);
620 error = 0;
621 for (i = 0; !error && i < len; i++)
622 error = irt_putc(tp, buf[i]);
623 lockmgr(&sc->sc_wr_lk, LK_RELEASE, NULL);
624
625 irframetstart(tp);
626
627 DPRINTF(("%s: done, error=%d\n", __FUNCTION__, error));
628
629 return (error);
630 }
631
632 int
633 irframet_poll(void *h, int events, struct proc *p)
634 {
635 struct tty *tp = h;
636 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
637 int revents = 0;
638 int s;
639
640 DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
641
642 s = splir();
643 /* XXX is this a good check? */
644 if (events & (POLLOUT | POLLWRNORM))
645 if (tp->t_outq.c_cc <= tp->t_lowat)
646 revents |= events & (POLLOUT | POLLWRNORM);
647
648 if (events & (POLLIN | POLLRDNORM)) {
649 if (sc->sc_nframes > 0) {
650 DPRINTF(("%s: have data\n", __FUNCTION__));
651 revents |= events & (POLLIN | POLLRDNORM);
652 } else {
653 DPRINTF(("%s: recording select\n", __FUNCTION__));
654 selrecord(p, &sc->sc_rsel);
655 }
656 }
657 splx(s);
658
659 return (revents);
660 }
661
662 int
663 irframet_set_params(void *h, struct irda_params *p)
664 {
665 struct tty *tp = h;
666 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
667 int i;
668
669 DPRINTF(("%s: tp=%p speed=%d ebofs=%d maxsize=%d\n",
670 __FUNCTION__, tp, p->speed, p->ebofs, p->maxsize));
671
672 if (p->speed != sc->sc_params.speed) {
673 /* Checked in irframe.c */
674 lockmgr(&sc->sc_wr_lk, LK_EXCLUSIVE, NULL);
675 irt_dongles[sc->sc_dongle].setspeed(tp, p->speed);
676 lockmgr(&sc->sc_wr_lk, LK_RELEASE, NULL);
677 sc->sc_params.speed = p->speed;
678 }
679
680 /* Max size checked in irframe.c */
681 sc->sc_params.ebofs = p->ebofs;
682 /* Max size checked in irframe.c */
683 if (sc->sc_params.maxsize != p->maxsize) {
684 sc->sc_params.maxsize = p->maxsize;
685 if (sc->sc_inbuf != NULL)
686 free(sc->sc_inbuf, M_DEVBUF);
687 for (i = 0; i < MAXFRAMES; i++)
688 if (sc->sc_frames[i].buf != NULL)
689 free(sc->sc_frames[i].buf, M_DEVBUF);
690 if (sc->sc_params.maxsize != 0) {
691 sc->sc_inbuf = malloc(sc->sc_params.maxsize+2,
692 M_DEVBUF, M_WAITOK);
693 for (i = 0; i < MAXFRAMES; i++)
694 sc->sc_frames[i].buf =
695 malloc(sc->sc_params.maxsize,
696 M_DEVBUF, M_WAITOK);
697 } else {
698 sc->sc_inbuf = NULL;
699 for (i = 0; i < MAXFRAMES; i++)
700 sc->sc_frames[i].buf = NULL;
701 }
702 }
703 sc->sc_framestate = FRAME_OUTSIDE;
704
705 return (0);
706 }
707
708 int
709 irframet_get_speeds(void *h, int *speeds)
710 {
711 struct tty *tp = h;
712 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
713
714 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
715
716 if (sc == NULL) /* during attach */
717 *speeds = IRDA_SPEEDS_SIR;
718 else
719 *speeds = irt_dongles[sc->sc_dongle].speedmask;
720 return (0);
721 }
722
723 int
724 irframet_get_turnarounds(void *h, int *turnarounds)
725 {
726 struct tty *tp = h;
727 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
728
729 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
730
731 *turnarounds = irt_dongles[sc->sc_dongle].turnmask;
732 return (0);
733 }
734
735 void
736 irt_ioctl(struct tty *tp, u_long cmd, void *arg)
737 {
738 int error;
739 dev_t dev;
740
741 dev = tp->t_dev;
742 error = cdevsw[major(dev)].d_ioctl(dev, cmd, arg, 0, curproc);
743 #ifdef DIAGNOSTIC
744 if (error)
745 printf("irt_ioctl: cmd=0x%08lx error=%d\n", cmd, error);
746 #endif
747 }
748
749 void
750 irt_setspeed(struct tty *tp, u_int speed)
751 {
752 struct termios tt;
753
754 irt_ioctl(tp, TIOCGETA, &tt);
755 tt.c_ispeed = tt.c_ospeed = speed;
756 tt.c_cflag &= ~HUPCL;
757 tt.c_cflag |= CLOCAL;
758 irt_ioctl(tp, TIOCSETAF, &tt);
759 }
760
761 void
762 irt_setline(struct tty *tp, u_int line)
763 {
764 int mline;
765
766 irt_ioctl(tp, TIOCMGET, &mline);
767 mline &= ~(TIOCM_DTR | TIOCM_RTS);
768 mline |= line;
769 irt_ioctl(tp, TIOCMSET, (caddr_t)&mline);
770 }
771
772 void
773 irt_delay(struct tty *tp, u_int ms)
774 {
775 if (cold)
776 delay(ms * 1000);
777 else
778 tsleep(&irt_delay, PZERO, "irtdly", ms * hz / 1000 + 1);
779
780 }
781
782 /**********************************************************************
783 * No dongle
784 **********************************************************************/
785 void
786 irts_none(struct tty *tp, u_int speed)
787 {
788 irt_setspeed(tp, speed);
789 }
790
791 /**********************************************************************
792 * Tekram
793 **********************************************************************/
794 #define TEKRAM_PW 0x10
795
796 #define TEKRAM_115200 (TEKRAM_PW|0x00)
797 #define TEKRAM_57600 (TEKRAM_PW|0x01)
798 #define TEKRAM_38400 (TEKRAM_PW|0x02)
799 #define TEKRAM_19200 (TEKRAM_PW|0x03)
800 #define TEKRAM_9600 (TEKRAM_PW|0x04)
801 #define TEKRAM_2400 (TEKRAM_PW|0x08)
802
803 #define TEKRAM_TV (TEKRAM_PW|0x05)
804
805 void
806 irts_tekram(struct tty *tp, u_int speed)
807 {
808 int s;
809
810 irt_setspeed(tp, 9600);
811 irt_setline(tp, 0);
812 irt_delay(tp, 50);
813
814 irt_setline(tp, TIOCM_RTS);
815 irt_delay(tp, 1);
816
817 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
818 irt_delay(tp, 1); /* 50 us */
819
820 irt_setline(tp, TIOCM_DTR);
821 irt_delay(tp, 1); /* 7 us */
822
823 switch(speed) {
824 case 115200: s = TEKRAM_115200; break;
825 case 57600: s = TEKRAM_57600; break;
826 case 38400: s = TEKRAM_38400; break;
827 case 19200: s = TEKRAM_19200; break;
828 case 2400: s = TEKRAM_2400; break;
829 default: s = TEKRAM_9600; break;
830 }
831 irt_putc(tp, s);
832 irframetstart(tp);
833
834 irt_delay(tp, 100);
835
836 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
837 if (speed != 9600)
838 irt_setspeed(tp, speed);
839 irt_delay(tp, 1); /* 50 us */
840 }
841
842 /**********************************************************************
843 * Jeteye
844 **********************************************************************/
845 void
846 irts_jeteye(struct tty *tp, u_int speed)
847 {
848 switch (speed) {
849 case 19200:
850 irt_setline(tp, TIOCM_DTR);
851 break;
852 case 115200:
853 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
854 break;
855 default: /*9600*/
856 irt_setline(tp, TIOCM_RTS);
857 break;
858 }
859 irt_setspeed(tp, speed);
860 }
861
862 /**********************************************************************
863 * Actisys
864 **********************************************************************/
865 void
866 irts_actisys(struct tty *tp, u_int speed)
867 {
868 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
869 int pulses;
870
871 irt_setspeed(tp, speed);
872
873 switch(speed) {
874 case 19200: pulses=1; break;
875 case 57600: pulses=2; break;
876 case 115200: pulses=3; break;
877 case 38400: pulses=4; break;
878 default: /* 9600 */ pulses=0; break;
879 }
880
881 if (sc->sc_dongle_private == 0) {
882 sc->sc_dongle_private = 1;
883 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
884 /*
885 * Must wait at least 50ms after initial
886 * power on to charge internal capacitor
887 */
888 irt_delay(tp, 50);
889 }
890 irt_setline(tp, TIOCM_RTS);
891 delay(2);
892 for (;;) {
893 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
894 delay(2);
895 if (--pulses <= 0)
896 break;
897 irt_setline(tp, TIOCM_DTR);
898 delay(2);
899 }
900 }
901
902 /**********************************************************************
903 * Litelink
904 **********************************************************************/
905 void
906 irts_litelink(struct tty *tp, u_int speed)
907 {
908 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
909 int pulses;
910
911 irt_setspeed(tp, speed);
912
913 switch(speed) {
914 case 57600: pulses=1; break;
915 case 38400: pulses=2; break;
916 case 19200: pulses=3; break;
917 case 9600: pulses=4; break;
918 default: /* 115200 */ pulses=0; break;
919 }
920
921 if (sc->sc_dongle_private == 0) {
922 sc->sc_dongle_private = 1;
923 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
924 }
925 irt_setline(tp, TIOCM_RTS);
926 irt_delay(tp, 1); /* 15 us */;
927 for (;;) {
928 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
929 irt_delay(tp, 1); /* 15 us */;
930 if (--pulses <= 0)
931 break;
932 irt_setline(tp, TIOCM_DTR);
933 irt_delay(tp, 1); /* 15 us */;
934 }
935 }
936
937 /**********************************************************************
938 * Girbil
939 **********************************************************************/
940 /* Control register 1 */
941 #define GIRBIL_TXEN 0x01 /* Enable transmitter */
942 #define GIRBIL_RXEN 0x02 /* Enable receiver */
943 #define GIRBIL_ECAN 0x04 /* Cancel self emmited data */
944 #define GIRBIL_ECHO 0x08 /* Echo control characters */
945
946 /* LED Current Register */
947 #define GIRBIL_HIGH 0x20
948 #define GIRBIL_MEDIUM 0x21
949 #define GIRBIL_LOW 0x22
950
951 /* Baud register */
952 #define GIRBIL_2400 0x30
953 #define GIRBIL_4800 0x31
954 #define GIRBIL_9600 0x32
955 #define GIRBIL_19200 0x33
956 #define GIRBIL_38400 0x34
957 #define GIRBIL_57600 0x35
958 #define GIRBIL_115200 0x36
959
960 /* Mode register */
961 #define GIRBIL_IRDA 0x40
962 #define GIRBIL_ASK 0x41
963
964 /* Control register 2 */
965 #define GIRBIL_LOAD 0x51 /* Load the new baud rate value */
966
967 void
968 irts_girbil(struct tty *tp, u_int speed)
969 {
970 int s;
971
972 irt_setspeed(tp, 9600);
973 irt_setline(tp, TIOCM_DTR);
974 irt_delay(tp, 5);
975 irt_setline(tp, TIOCM_RTS);
976 irt_delay(tp, 20);
977 switch(speed) {
978 case 115200: s = GIRBIL_115200; break;
979 case 57600: s = GIRBIL_57600; break;
980 case 38400: s = GIRBIL_38400; break;
981 case 19200: s = GIRBIL_19200; break;
982 case 4800: s = GIRBIL_4800; break;
983 case 2400: s = GIRBIL_2400; break;
984 default: s = GIRBIL_9600; break;
985 }
986 irt_putc(tp, GIRBIL_TXEN|GIRBIL_RXEN);
987 irt_putc(tp, s);
988 irt_putc(tp, GIRBIL_LOAD);
989 irframetstart(tp);
990 irt_delay(tp, 100);
991 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
992 if (speed != 9600)
993 irt_setspeed(tp, speed);
994 }
995