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