irframe_tty.c revision 1.45 1 /* $NetBSD: irframe_tty.c,v 1.45 2007/11/07 15:56:18 ad 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.45 2007/11/07 15:56:18 ad Exp $");
52
53 #include <sys/param.h>
54 #include <sys/proc.h>
55 #include <sys/ioctl.h>
56 #include <sys/tty.h>
57 #include <sys/kernel.h>
58 #include <sys/lock.h>
59 #include <sys/malloc.h>
60 #include <sys/conf.h>
61 #include <sys/systm.h>
62 #include <sys/device.h>
63 #include <sys/file.h>
64 #include <sys/vnode.h>
65 #include <sys/poll.h>
66 #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 struct lock 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", __FUNCTION__));
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", __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__, sc));
300
301 ttyflush(tp, FREAD | FWRITE);
302
303 sc->sc_dongle = DONGLE_NONE;
304 sc->sc_dongle_private = 0;
305
306 splx(s);
307
308 return (0);
309 }
310
311 /*
312 * Line specific close routine, called from device close routine
313 * and from ttioctl.
314 * Detach the tty from the irframe unit.
315 * Mimics part of ttyclose().
316 */
317 int
318 irframetclose(struct tty *tp, int flag)
319 {
320 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
321 int s;
322 struct cfdata *cfdata;
323
324 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
325
326 s = spltty();
327 ttyflush(tp, FREAD | FWRITE);
328 ttyldisc_release(tp->t_linesw);
329 tp->t_linesw = ttyldisc_default();
330 if (sc != NULL) {
331 tp->t_sc = NULL;
332 printf("%s detached from tty%02d\n", sc->sc_irp.sc_dev.dv_xname,
333 minor(tp->t_dev));
334
335 if (sc->sc_tp == tp) {
336 cfdata = sc->sc_irp.sc_dev.dv_cfdata;
337 config_detach(&sc->sc_irp.sc_dev, 0);
338 free(cfdata, M_DEVBUF);
339 }
340 }
341 splx(s);
342 return (0);
343 }
344
345 /*
346 * Line specific (tty) ioctl routine.
347 * This discipline requires that tty device drivers call
348 * the line specific l_ioctl routine from their ioctl routines.
349 */
350 /* ARGSUSED */
351 int
352 irframetioctl(struct tty *tp, u_long cmd, void *data, int flag,
353 struct lwp *l)
354 {
355 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
356 int error;
357 int d;
358
359 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
360
361 if (sc == NULL || tp != sc->sc_tp)
362 return (EPASSTHROUGH);
363
364 error = 0;
365 switch (cmd) {
366 case IRFRAMETTY_GET_DEVICE:
367 *(int *)data = device_unit(&sc->sc_irp.sc_dev);
368 break;
369 case IRFRAMETTY_GET_DONGLE:
370 *(int *)data = sc->sc_dongle;
371 break;
372 case IRFRAMETTY_SET_DONGLE:
373 d = *(int *)data;
374 if (d < 0 || d >= DONGLE_MAX)
375 return (EINVAL);
376 sc->sc_dongle = d;
377 break;
378 default:
379 error = EPASSTHROUGH;
380 break;
381 }
382
383 return (error);
384 }
385
386 /*
387 * Start output on async tty interface.
388 */
389 int
390 irframetstart(struct tty *tp)
391 {
392 /*struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;*/
393 int s;
394
395 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
396
397 s = spltty();
398 if (tp->t_oproc != NULL)
399 (*tp->t_oproc)(tp);
400 splx(s);
401
402 return (0);
403 }
404
405 void
406 irt_frame(struct irframet_softc *sc, u_char *tbuf, u_int len)
407 {
408 DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
409 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
410
411 if (sc->sc_inbuf == NULL) /* XXX happens if device is closed? */
412 return;
413 if (sc->sc_nframes >= MAXFRAMES) {
414 #ifdef IRFRAMET_DEBUG
415 printf("%s: dropped frame\n", __FUNCTION__);
416 #endif
417 return;
418 }
419 if (sc->sc_frames[sc->sc_framei].buf == NULL)
420 return;
421 memcpy(sc->sc_frames[sc->sc_framei].buf, tbuf, len);
422 sc->sc_frames[sc->sc_framei].len = len;
423 sc->sc_framei = (sc->sc_framei+1) % MAXFRAMES;
424 sc->sc_nframes++;
425 if (sc->sc_state & IRT_RSLP) {
426 sc->sc_state &= ~IRT_RSLP;
427 DPRINTF(("%s: waking up reader\n", __FUNCTION__));
428 wakeup(sc->sc_frames);
429 }
430 selnotify(&sc->sc_rsel, 0);
431 }
432
433 void
434 irt_timeout(void *v)
435 {
436 struct irframet_softc *sc = v;
437
438 #ifdef IRFRAMET_DEBUG
439 if (sc->sc_framestate != FRAME_OUTSIDE)
440 printf("%s: input frame timeout\n", __FUNCTION__);
441 #endif
442 sc->sc_framestate = FRAME_OUTSIDE;
443 }
444
445 int
446 irframetinput(int c, struct tty *tp)
447 {
448 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
449
450 c &= 0xff;
451
452 #if IRFRAMET_DEBUG
453 if (irframetdebug > 1)
454 DPRINTF(("%s: tp=%p c=0x%02x\n", __FUNCTION__, tp, c));
455 #endif
456
457 if (sc == NULL || tp != (struct tty *)sc->sc_tp)
458 return (0);
459
460 if (sc->sc_inbuf == NULL)
461 return (0);
462
463 switch (c) {
464 case SIR_BOF:
465 DPRINTF(("%s: BOF\n", __FUNCTION__));
466 sc->sc_framestate = FRAME_INSIDE;
467 sc->sc_inchars = 0;
468 sc->sc_inFCS = INITFCS;
469 break;
470 case SIR_EOF:
471 DPRINTF(("%s: EOF state=%d inchars=%d fcs=0x%04x\n",
472 __FUNCTION__,
473 sc->sc_framestate, sc->sc_inchars, sc->sc_inFCS));
474 if (sc->sc_framestate == FRAME_INSIDE &&
475 sc->sc_inchars >= 4 && sc->sc_inFCS == GOODFCS) {
476 irt_frame(sc, sc->sc_inbuf, sc->sc_inchars - 2);
477 } else if (sc->sc_framestate != FRAME_OUTSIDE) {
478 #ifdef IRFRAMET_DEBUG
479 printf("%s: malformed input frame\n", __FUNCTION__);
480 #endif
481 }
482 sc->sc_framestate = FRAME_OUTSIDE;
483 break;
484 case SIR_CE:
485 DPRINTF(("%s: CE\n", __FUNCTION__));
486 if (sc->sc_framestate == FRAME_INSIDE)
487 sc->sc_framestate = FRAME_ESCAPE;
488 break;
489 default:
490 #if IRFRAMET_DEBUG
491 if (irframetdebug > 1)
492 DPRINTF(("%s: c=0x%02x, inchar=%d state=%d\n", __FUNCTION__, c,
493 sc->sc_inchars, sc->sc_state));
494 #endif
495 if (sc->sc_framestate != FRAME_OUTSIDE) {
496 if (sc->sc_framestate == FRAME_ESCAPE) {
497 sc->sc_framestate = FRAME_INSIDE;
498 c ^= SIR_ESC_BIT;
499 }
500 if (sc->sc_inchars < sc->sc_params.maxsize + 2) {
501 sc->sc_inbuf[sc->sc_inchars++] = c;
502 sc->sc_inFCS = updateFCS(sc->sc_inFCS, c);
503 } else {
504 sc->sc_framestate = FRAME_OUTSIDE;
505 #ifdef IRFRAMET_DEBUG
506 printf("%s: input frame overrun\n",
507 __FUNCTION__);
508 #endif
509 }
510 }
511 break;
512 }
513
514 #if 1
515 if (sc->sc_framestate != FRAME_OUTSIDE) {
516 callout_reset(&sc->sc_timeout, hz/20, irt_timeout, sc);
517 }
518 #endif
519
520 return (0);
521 }
522
523
524 /*** irframe methods ***/
525
526 int
527 irframet_open(void *h, int flag, int mode,
528 struct lwp *l)
529 {
530 struct tty *tp = h;
531 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
532
533 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
534
535 sc->sc_params.speed = 0;
536 sc->sc_params.ebofs = IRDA_DEFAULT_EBOFS;
537 sc->sc_params.maxsize = 0;
538 sc->sc_framestate = FRAME_OUTSIDE;
539 sc->sc_nframes = 0;
540 sc->sc_framei = 0;
541 sc->sc_frameo = 0;
542 callout_init(&sc->sc_timeout, 0);
543 lockinit(&sc->sc_wr_lk, PZERO, "irfrtl", 0, 0);
544
545 return (0);
546 }
547
548 int
549 irframet_close(void *h, int flag, int mode,
550 struct lwp *l)
551 {
552 struct tty *tp = h;
553 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
554 int i, s;
555
556 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
557
558 callout_stop(&sc->sc_timeout);
559 s = splir();
560 if (sc->sc_inbuf != NULL) {
561 free(sc->sc_inbuf, M_DEVBUF);
562 sc->sc_inbuf = NULL;
563 }
564 for (i = 0; i < MAXFRAMES; i++) {
565 if (sc->sc_frames[i].buf != NULL) {
566 free(sc->sc_frames[i].buf, M_DEVBUF);
567 sc->sc_frames[i].buf = NULL;
568 }
569 }
570 splx(s);
571
572 return (0);
573 }
574
575 int
576 irframet_read(void *h, struct uio *uio, int flag)
577 {
578 struct tty *tp = h;
579 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
580 int error = 0;
581 int s;
582
583 DPRINTF(("%s: resid=%zd, iovcnt=%d, offset=%ld\n",
584 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
585 (long)uio->uio_offset));
586 DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
587 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
588
589
590 s = splir();
591 while (sc->sc_nframes == 0) {
592 if (flag & IO_NDELAY) {
593 splx(s);
594 return (EWOULDBLOCK);
595 }
596 sc->sc_state |= IRT_RSLP;
597 DPRINTF(("%s: sleep\n", __FUNCTION__));
598 error = tsleep(sc->sc_frames, PZERO | PCATCH, "irtrd", 0);
599 DPRINTF(("%s: woke, error=%d\n", __FUNCTION__, error));
600 if (error) {
601 sc->sc_state &= ~IRT_RSLP;
602 break;
603 }
604 }
605
606 /* Do just one frame transfer per read */
607 if (!error) {
608 if (uio->uio_resid < sc->sc_frames[sc->sc_frameo].len) {
609 DPRINTF(("%s: uio buffer smaller than frame size "
610 "(%zd < %d)\n", __FUNCTION__, uio->uio_resid,
611 sc->sc_frames[sc->sc_frameo].len));
612 error = EINVAL;
613 } else {
614 DPRINTF(("%s: moving %d bytes\n", __FUNCTION__,
615 sc->sc_frames[sc->sc_frameo].len));
616 error = uiomove(sc->sc_frames[sc->sc_frameo].buf,
617 sc->sc_frames[sc->sc_frameo].len, uio);
618 DPRINTF(("%s: error=%d\n", __FUNCTION__, error));
619 }
620 sc->sc_frameo = (sc->sc_frameo+1) % MAXFRAMES;
621 sc->sc_nframes--;
622 }
623 splx(s);
624
625 return (error);
626 }
627
628 int
629 irt_putc(struct tty *tp, int c)
630 {
631 int error;
632
633 #if IRFRAMET_DEBUG
634 if (irframetdebug > 3)
635 DPRINTF(("%s: tp=%p c=0x%02x cc=%d\n", __FUNCTION__, tp, c,
636 tp->t_outq.c_cc));
637 #endif
638 if (tp->t_outq.c_cc > tp->t_hiwat) {
639 irframetstart(tp);
640 mutex_spin_enter(&tty_lock);
641 /*
642 * This can only occur if FLUSHO is set in t_lflag,
643 * or if ttstart/oproc is synchronous (or very fast).
644 */
645 if (tp->t_outq.c_cc <= tp->t_hiwat) {
646 mutex_spin_exit(&tty_lock);
647 goto go;
648 }
649 SET(tp->t_state, TS_ASLEEP);
650 error = ttysleep(tp, &tp->t_outq.c_cv, true, 0);
651 mutex_spin_exit(&tty_lock);
652 if (error)
653 return (error);
654 }
655 go:
656 if (putc(c, &tp->t_outq) < 0) {
657 printf("irframe: putc failed\n");
658 return (EIO);
659 }
660 return (0);
661 }
662
663 int
664 irframet_write(void *h, struct uio *uio, int flag)
665 {
666 struct tty *tp = h;
667 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
668 int n;
669
670 DPRINTF(("%s: resid=%zd, iovcnt=%d, offset=%ld\n",
671 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
672 (long)uio->uio_offset));
673
674 n = irda_sir_frame(sc->sc_buffer, sizeof(sc->sc_buffer), uio,
675 sc->sc_params.ebofs);
676 if (n < 0) {
677 #ifdef IRFRAMET_DEBUG
678 printf("%s: irda_sir_frame() error=%d\n", __FUNCTION__, -n);
679 #endif
680 return (-n);
681 }
682 return (irt_write_frame(tp, sc->sc_buffer, n));
683 }
684
685 int
686 irt_write_frame(struct tty *tp, u_int8_t *tbuf, size_t len)
687 {
688 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
689 int error, i;
690
691 DPRINTF(("%s: tp=%p len=%zd\n", __FUNCTION__, tp, len));
692
693 lockmgr(&sc->sc_wr_lk, LK_EXCLUSIVE, NULL);
694 error = 0;
695 for (i = 0; !error && i < len; i++)
696 error = irt_putc(tp, tbuf[i]);
697 lockmgr(&sc->sc_wr_lk, LK_RELEASE, NULL);
698
699 irframetstart(tp);
700
701 DPRINTF(("%s: done, error=%d\n", __FUNCTION__, error));
702
703 return (error);
704 }
705
706 int
707 irframet_poll(void *h, int events, struct lwp *l)
708 {
709 struct tty *tp = h;
710 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
711 int revents = 0;
712 int s;
713
714 DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
715
716 s = splir();
717 /* XXX is this a good check? */
718 if (events & (POLLOUT | POLLWRNORM))
719 if (tp->t_outq.c_cc <= tp->t_lowat)
720 revents |= events & (POLLOUT | POLLWRNORM);
721
722 if (events & (POLLIN | POLLRDNORM)) {
723 if (sc->sc_nframes > 0) {
724 DPRINTF(("%s: have data\n", __FUNCTION__));
725 revents |= events & (POLLIN | POLLRDNORM);
726 } else {
727 DPRINTF(("%s: recording select\n", __FUNCTION__));
728 selrecord(l, &sc->sc_rsel);
729 }
730 }
731 splx(s);
732
733 return (revents);
734 }
735
736 static void
737 filt_irframetrdetach(struct knote *kn)
738 {
739 struct tty *tp = kn->kn_hook;
740 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
741 int s;
742
743 s = splir();
744 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
745 splx(s);
746 }
747
748 static int
749 filt_irframetread(struct knote *kn, long hint)
750 {
751 struct tty *tp = kn->kn_hook;
752 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
753
754 kn->kn_data = sc->sc_nframes;
755 return (kn->kn_data > 0);
756 }
757
758 static void
759 filt_irframetwdetach(struct knote *kn)
760 {
761 struct tty *tp = kn->kn_hook;
762 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
763 int s;
764
765 s = splir();
766 SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
767 splx(s);
768 }
769
770 static int
771 filt_irframetwrite(struct knote *kn, long hint)
772 {
773 struct tty *tp = kn->kn_hook;
774
775 /* XXX double-check this */
776
777 if (tp->t_outq.c_cc <= tp->t_lowat) {
778 kn->kn_data = tp->t_lowat - tp->t_outq.c_cc;
779 return (1);
780 }
781
782 kn->kn_data = 0;
783 return (0);
784 }
785
786 static const struct filterops irframetread_filtops =
787 { 1, NULL, filt_irframetrdetach, filt_irframetread };
788 static const struct filterops irframetwrite_filtops =
789 { 1, NULL, filt_irframetwdetach, filt_irframetwrite };
790
791 int
792 irframet_kqfilter(void *h, struct knote *kn)
793 {
794 struct tty *tp = h;
795 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
796 struct klist *klist;
797 int s;
798
799 switch (kn->kn_filter) {
800 case EVFILT_READ:
801 klist = &sc->sc_rsel.sel_klist;
802 kn->kn_fop = &irframetread_filtops;
803 break;
804 case EVFILT_WRITE:
805 klist = &sc->sc_wsel.sel_klist;
806 kn->kn_fop = &irframetwrite_filtops;
807 break;
808 default:
809 return (1);
810 }
811
812 kn->kn_hook = tp;
813
814 s = splir();
815 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
816 splx(s);
817
818 return (0);
819 }
820
821 int
822 irframet_set_params(void *h, struct irda_params *p)
823 {
824 struct tty *tp = h;
825 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
826 int i;
827
828 DPRINTF(("%s: tp=%p speed=%d ebofs=%d maxsize=%d\n",
829 __FUNCTION__, tp, p->speed, p->ebofs, p->maxsize));
830
831 if (p->speed != sc->sc_params.speed) {
832 /* Checked in irframe.c */
833 lockmgr(&sc->sc_wr_lk, LK_EXCLUSIVE, NULL);
834 irt_dongles[sc->sc_dongle].setspeed(tp, p->speed);
835 lockmgr(&sc->sc_wr_lk, LK_RELEASE, NULL);
836 sc->sc_params.speed = p->speed;
837 }
838
839 /* Max size checked in irframe.c */
840 sc->sc_params.ebofs = p->ebofs;
841 /* Max size checked in irframe.c */
842 if (sc->sc_params.maxsize != p->maxsize) {
843 sc->sc_params.maxsize = p->maxsize;
844 if (sc->sc_inbuf != NULL)
845 free(sc->sc_inbuf, M_DEVBUF);
846 for (i = 0; i < MAXFRAMES; i++)
847 if (sc->sc_frames[i].buf != NULL)
848 free(sc->sc_frames[i].buf, M_DEVBUF);
849 if (sc->sc_params.maxsize != 0) {
850 sc->sc_inbuf = malloc(sc->sc_params.maxsize+2,
851 M_DEVBUF, M_WAITOK);
852 for (i = 0; i < MAXFRAMES; i++)
853 sc->sc_frames[i].buf =
854 malloc(sc->sc_params.maxsize,
855 M_DEVBUF, M_WAITOK);
856 } else {
857 sc->sc_inbuf = NULL;
858 for (i = 0; i < MAXFRAMES; i++)
859 sc->sc_frames[i].buf = NULL;
860 }
861 }
862 sc->sc_framestate = FRAME_OUTSIDE;
863
864 return (0);
865 }
866
867 int
868 irframet_get_speeds(void *h, int *speeds)
869 {
870 struct tty *tp = h;
871 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
872
873 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
874
875 if (sc == NULL) /* during attach */
876 *speeds = IRDA_SPEEDS_SIR;
877 else
878 *speeds = irt_dongles[sc->sc_dongle].speedmask;
879 return (0);
880 }
881
882 int
883 irframet_get_turnarounds(void *h, int *turnarounds)
884 {
885 struct tty *tp = h;
886 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
887
888 DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
889
890 *turnarounds = irt_dongles[sc->sc_dongle].turnmask;
891 return (0);
892 }
893
894 void
895 irt_ioctl(struct tty *tp, u_long cmd, void *arg)
896 {
897 const struct cdevsw *cdev;
898 int error;
899 dev_t dev;
900
901 dev = tp->t_dev;
902 cdev = cdevsw_lookup(dev);
903 if (cdev != NULL)
904 error = (*cdev->d_ioctl)(dev, cmd, arg, 0, curlwp);
905 else
906 error = ENXIO;
907 #ifdef DIAGNOSTIC
908 if (error)
909 printf("irt_ioctl: cmd=0x%08lx error=%d\n", cmd, error);
910 #endif
911 }
912
913 void
914 irt_setspeed(struct tty *tp, u_int speed)
915 {
916 struct termios tt;
917
918 irt_ioctl(tp, TIOCGETA, &tt);
919 tt.c_ispeed = tt.c_ospeed = speed;
920 tt.c_cflag &= ~HUPCL;
921 tt.c_cflag |= CLOCAL;
922 irt_ioctl(tp, TIOCSETAF, &tt);
923 }
924
925 void
926 irt_setline(struct tty *tp, u_int line)
927 {
928 int mline;
929
930 irt_ioctl(tp, TIOCMGET, &mline);
931 mline &= ~(TIOCM_DTR | TIOCM_RTS);
932 mline |= line;
933 irt_ioctl(tp, TIOCMSET, (void *)&mline);
934 }
935
936 void
937 irt_delay(struct tty *tp, u_int ms)
938 {
939 if (cold)
940 delay(ms * 1000);
941 else
942 tsleep(&irt_delay, PZERO, "irtdly", ms * hz / 1000 + 1);
943
944 }
945
946 /**********************************************************************
947 * No dongle
948 **********************************************************************/
949 void
950 irts_none(struct tty *tp, u_int speed)
951 {
952 irt_setspeed(tp, speed);
953 }
954
955 /**********************************************************************
956 * Tekram
957 **********************************************************************/
958 #define TEKRAM_PW 0x10
959
960 #define TEKRAM_115200 (TEKRAM_PW|0x00)
961 #define TEKRAM_57600 (TEKRAM_PW|0x01)
962 #define TEKRAM_38400 (TEKRAM_PW|0x02)
963 #define TEKRAM_19200 (TEKRAM_PW|0x03)
964 #define TEKRAM_9600 (TEKRAM_PW|0x04)
965 #define TEKRAM_2400 (TEKRAM_PW|0x08)
966
967 #define TEKRAM_TV (TEKRAM_PW|0x05)
968
969 void
970 irts_tekram(struct tty *tp, u_int speed)
971 {
972 int s;
973
974 irt_setspeed(tp, 9600);
975 irt_setline(tp, 0);
976 irt_delay(tp, 50);
977
978 irt_setline(tp, TIOCM_RTS);
979 irt_delay(tp, 1);
980
981 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
982 irt_delay(tp, 1); /* 50 us */
983
984 irt_setline(tp, TIOCM_DTR);
985 irt_delay(tp, 1); /* 7 us */
986
987 switch(speed) {
988 case 115200: s = TEKRAM_115200; break;
989 case 57600: s = TEKRAM_57600; break;
990 case 38400: s = TEKRAM_38400; break;
991 case 19200: s = TEKRAM_19200; break;
992 case 2400: s = TEKRAM_2400; break;
993 default: s = TEKRAM_9600; break;
994 }
995 irt_putc(tp, s);
996 irframetstart(tp);
997
998 irt_delay(tp, 100);
999
1000 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1001 if (speed != 9600)
1002 irt_setspeed(tp, speed);
1003 irt_delay(tp, 1); /* 50 us */
1004 }
1005
1006 /**********************************************************************
1007 * Jeteye
1008 **********************************************************************/
1009 void
1010 irts_jeteye(struct tty *tp, u_int speed)
1011 {
1012 switch (speed) {
1013 case 19200:
1014 irt_setline(tp, TIOCM_DTR);
1015 break;
1016 case 115200:
1017 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1018 break;
1019 default: /*9600*/
1020 irt_setline(tp, TIOCM_RTS);
1021 break;
1022 }
1023 irt_setspeed(tp, speed);
1024 }
1025
1026 /**********************************************************************
1027 * Actisys
1028 **********************************************************************/
1029 void
1030 irts_actisys(struct tty *tp, u_int speed)
1031 {
1032 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
1033 int pulses;
1034
1035 irt_setspeed(tp, speed);
1036
1037 switch(speed) {
1038 case 19200: pulses=1; break;
1039 case 57600: pulses=2; break;
1040 case 115200: pulses=3; break;
1041 case 38400: pulses=4; break;
1042 default: /* 9600 */ pulses=0; break;
1043 }
1044
1045 if (sc->sc_dongle_private == 0) {
1046 sc->sc_dongle_private = 1;
1047 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1048 /*
1049 * Must wait at least 50ms after initial
1050 * power on to charge internal capacitor
1051 */
1052 irt_delay(tp, 50);
1053 }
1054 irt_setline(tp, TIOCM_RTS);
1055 delay(2);
1056 for (;;) {
1057 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1058 delay(2);
1059 if (--pulses <= 0)
1060 break;
1061 irt_setline(tp, TIOCM_DTR);
1062 delay(2);
1063 }
1064 }
1065
1066 /**********************************************************************
1067 * Litelink
1068 **********************************************************************/
1069 void
1070 irts_litelink(struct tty *tp, u_int speed)
1071 {
1072 struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
1073 int pulses;
1074
1075 irt_setspeed(tp, speed);
1076
1077 switch(speed) {
1078 case 57600: pulses=1; break;
1079 case 38400: pulses=2; break;
1080 case 19200: pulses=3; break;
1081 case 9600: pulses=4; break;
1082 default: /* 115200 */ pulses=0; break;
1083 }
1084
1085 if (sc->sc_dongle_private == 0) {
1086 sc->sc_dongle_private = 1;
1087 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1088 }
1089 irt_setline(tp, TIOCM_RTS);
1090 irt_delay(tp, 1); /* 15 us */;
1091 for (;;) {
1092 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1093 irt_delay(tp, 1); /* 15 us */;
1094 if (--pulses <= 0)
1095 break;
1096 irt_setline(tp, TIOCM_DTR);
1097 irt_delay(tp, 1); /* 15 us */;
1098 }
1099 }
1100
1101 /**********************************************************************
1102 * Girbil
1103 **********************************************************************/
1104 /* Control register 1 */
1105 #define GIRBIL_TXEN 0x01 /* Enable transmitter */
1106 #define GIRBIL_RXEN 0x02 /* Enable receiver */
1107 #define GIRBIL_ECAN 0x04 /* Cancel self emmited data */
1108 #define GIRBIL_ECHO 0x08 /* Echo control characters */
1109
1110 /* LED Current Register */
1111 #define GIRBIL_HIGH 0x20
1112 #define GIRBIL_MEDIUM 0x21
1113 #define GIRBIL_LOW 0x22
1114
1115 /* Baud register */
1116 #define GIRBIL_2400 0x30
1117 #define GIRBIL_4800 0x31
1118 #define GIRBIL_9600 0x32
1119 #define GIRBIL_19200 0x33
1120 #define GIRBIL_38400 0x34
1121 #define GIRBIL_57600 0x35
1122 #define GIRBIL_115200 0x36
1123
1124 /* Mode register */
1125 #define GIRBIL_IRDA 0x40
1126 #define GIRBIL_ASK 0x41
1127
1128 /* Control register 2 */
1129 #define GIRBIL_LOAD 0x51 /* Load the new baud rate value */
1130
1131 void
1132 irts_girbil(struct tty *tp, u_int speed)
1133 {
1134 int s;
1135
1136 irt_setspeed(tp, 9600);
1137 irt_setline(tp, TIOCM_DTR);
1138 irt_delay(tp, 5);
1139 irt_setline(tp, TIOCM_RTS);
1140 irt_delay(tp, 20);
1141 switch(speed) {
1142 case 115200: s = GIRBIL_115200; break;
1143 case 57600: s = GIRBIL_57600; break;
1144 case 38400: s = GIRBIL_38400; break;
1145 case 19200: s = GIRBIL_19200; break;
1146 case 4800: s = GIRBIL_4800; break;
1147 case 2400: s = GIRBIL_2400; break;
1148 default: s = GIRBIL_9600; break;
1149 }
1150 irt_putc(tp, GIRBIL_TXEN|GIRBIL_RXEN);
1151 irt_putc(tp, s);
1152 irt_putc(tp, GIRBIL_LOAD);
1153 irframetstart(tp);
1154 irt_delay(tp, 100);
1155 irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
1156 if (speed != 9600)
1157 irt_setspeed(tp, speed);
1158 }
1159