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