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