irframe_tty.c revision 1.9 1 1.9 augustss /* $NetBSD: irframe_tty.c,v 1.9 2001/12/05 14:50:14 augustss Exp $ */
2 1.5 augustss
3 1.5 augustss /*
4 1.5 augustss * TODO
5 1.5 augustss * Implement dongle support.
6 1.5 augustss * Test!!!
7 1.5 augustss * Get rid of MAX_IRDA_FRAME
8 1.5 augustss */
9 1.1 augustss
10 1.1 augustss /*
11 1.1 augustss * Copyright (c) 2001 The NetBSD Foundation, Inc.
12 1.1 augustss * All rights reserved.
13 1.1 augustss *
14 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
15 1.6 augustss * by Lennart Augustsson (lennart (at) augustsson.net) and Tommy Bohlin
16 1.6 augustss * (tommy (at) gatespace.com).
17 1.1 augustss *
18 1.1 augustss * Redistribution and use in source and binary forms, with or without
19 1.1 augustss * modification, are permitted provided that the following conditions
20 1.1 augustss * are met:
21 1.1 augustss * 1. Redistributions of source code must retain the above copyright
22 1.1 augustss * notice, this list of conditions and the following disclaimer.
23 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
24 1.1 augustss * notice, this list of conditions and the following disclaimer in the
25 1.1 augustss * documentation and/or other materials provided with the distribution.
26 1.1 augustss * 3. All advertising materials mentioning features or use of this software
27 1.1 augustss * must display the following acknowledgement:
28 1.1 augustss * This product includes software developed by the NetBSD
29 1.1 augustss * Foundation, Inc. and its contributors.
30 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
31 1.1 augustss * contributors may be used to endorse or promote products derived
32 1.1 augustss * from this software without specific prior written permission.
33 1.1 augustss *
34 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
45 1.1 augustss */
46 1.1 augustss
47 1.1 augustss /*
48 1.1 augustss * Loosely based on ppp_tty.c.
49 1.9 augustss * Framing and dongle handling written by Tommy Bohlin.
50 1.1 augustss */
51 1.1 augustss
52 1.1 augustss #include <sys/param.h>
53 1.1 augustss #include <sys/proc.h>
54 1.1 augustss #include <sys/ioctl.h>
55 1.1 augustss #include <sys/tty.h>
56 1.1 augustss #include <sys/kernel.h>
57 1.2 augustss #include <sys/malloc.h>
58 1.1 augustss #include <sys/conf.h>
59 1.1 augustss #include <sys/systm.h>
60 1.1 augustss #include <sys/device.h>
61 1.1 augustss #include <sys/file.h>
62 1.2 augustss #include <sys/vnode.h>
63 1.1 augustss #include <sys/poll.h>
64 1.1 augustss
65 1.1 augustss #include <dev/ir/ir.h>
66 1.9 augustss #include <dev/ir/sir.h>
67 1.1 augustss #include <dev/ir/irdaio.h>
68 1.1 augustss #include <dev/ir/irframevar.h>
69 1.1 augustss
70 1.1 augustss /* Macros to clear/set/test flags. */
71 1.1 augustss #define SET(t, f) (t) |= (f)
72 1.2 augustss #define CLR(t, f) (t) &= ~(f)
73 1.1 augustss #define ISSET(t, f) ((t) & (f))
74 1.1 augustss
75 1.1 augustss #ifdef IRFRAMET_DEBUG
76 1.1 augustss #define DPRINTF(x) if (irframetdebug) printf x
77 1.2 augustss #define Static
78 1.8 augustss int irframetdebug = 0;
79 1.1 augustss #else
80 1.1 augustss #define DPRINTF(x)
81 1.2 augustss #define Static static
82 1.1 augustss #endif
83 1.1 augustss
84 1.1 augustss /*****/
85 1.1 augustss
86 1.1 augustss #define MAX_IRDA_FRAME 5000 /* XXX what is it? */
87 1.1 augustss
88 1.2 augustss struct frame {
89 1.2 augustss u_char *buf;
90 1.2 augustss u_int len;
91 1.2 augustss };
92 1.2 augustss #define MAXFRAMES 4
93 1.2 augustss
94 1.1 augustss struct irframet_softc {
95 1.1 augustss struct irframe_softc sc_irp;
96 1.1 augustss struct tty *sc_tp;
97 1.2 augustss
98 1.5 augustss int sc_dongle;
99 1.5 augustss
100 1.2 augustss int sc_state;
101 1.2 augustss #define IRT_RSLP 0x01 /* waiting for data (read) */
102 1.2 augustss #if 0
103 1.2 augustss #define IRT_WSLP 0x02 /* waiting for data (write) */
104 1.2 augustss #define IRT_CLOSING 0x04 /* waiting for output to drain */
105 1.2 augustss #endif
106 1.2 augustss
107 1.1 augustss int sc_ebofs;
108 1.4 augustss int sc_speed;
109 1.2 augustss
110 1.2 augustss u_char* sc_inbuf;
111 1.2 augustss int sc_maxsize;
112 1.2 augustss int sc_framestate;
113 1.2 augustss #define FRAME_OUTSIDE 0
114 1.2 augustss #define FRAME_INSIDE 1
115 1.2 augustss #define FRAME_ESCAPE 2
116 1.2 augustss int sc_inchars;
117 1.2 augustss int sc_inFCS;
118 1.2 augustss struct callout sc_timeout;
119 1.2 augustss
120 1.2 augustss u_int sc_nframes;
121 1.2 augustss u_int sc_framei;
122 1.2 augustss u_int sc_frameo;
123 1.2 augustss struct frame sc_frames[MAXFRAMES];
124 1.2 augustss struct selinfo sc_rsel;
125 1.1 augustss };
126 1.1 augustss
127 1.1 augustss /* line discipline methods */
128 1.1 augustss int irframetopen(dev_t dev, struct tty *tp);
129 1.1 augustss int irframetclose(struct tty *tp, int flag);
130 1.1 augustss int irframetioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
131 1.5 augustss struct proc *);
132 1.1 augustss int irframetinput(int c, struct tty *tp);
133 1.1 augustss int irframetstart(struct tty *tp);
134 1.1 augustss
135 1.2 augustss /* pseudo device init */
136 1.2 augustss void irframettyattach(int);
137 1.2 augustss
138 1.1 augustss /* irframe methods */
139 1.2 augustss Static int irframet_open(void *h, int flag, int mode, struct proc *p);
140 1.2 augustss Static int irframet_close(void *h, int flag, int mode, struct proc *p);
141 1.2 augustss Static int irframet_read(void *h, struct uio *uio, int flag);
142 1.2 augustss Static int irframet_write(void *h, struct uio *uio, int flag);
143 1.2 augustss Static int irframet_poll(void *h, int events, struct proc *p);
144 1.2 augustss Static int irframet_set_params(void *h, struct irda_params *params);
145 1.2 augustss Static int irframet_get_speeds(void *h, int *speeds);
146 1.2 augustss Static int irframet_get_turnarounds(void *h, int *times);
147 1.2 augustss
148 1.2 augustss /* internal */
149 1.9 augustss Static int irt_write_frame(void *h, u_int8_t *buf, size_t len);
150 1.2 augustss Static int irt_putc(int c, struct tty *tp);
151 1.2 augustss Static void irt_frame(struct irframet_softc *sc, u_char *buf, u_int len);
152 1.2 augustss Static void irt_timeout(void *v);
153 1.1 augustss
154 1.2 augustss Static struct irframe_methods irframet_methods = {
155 1.1 augustss irframet_open, irframet_close, irframet_read, irframet_write,
156 1.2 augustss irframet_poll, irframet_set_params,
157 1.2 augustss irframet_get_speeds, irframet_get_turnarounds
158 1.1 augustss };
159 1.1 augustss
160 1.1 augustss void
161 1.1 augustss irframettyattach(int n)
162 1.1 augustss {
163 1.1 augustss }
164 1.1 augustss
165 1.1 augustss /*
166 1.1 augustss * Line specific open routine for async tty devices.
167 1.1 augustss * Attach the given tty to the first available irframe unit.
168 1.1 augustss * Called from device open routine or ttioctl.
169 1.1 augustss */
170 1.1 augustss /* ARGSUSED */
171 1.1 augustss int
172 1.1 augustss irframetopen(dev_t dev, struct tty *tp)
173 1.1 augustss {
174 1.1 augustss struct proc *p = curproc; /* XXX */
175 1.1 augustss struct irframet_softc *sc;
176 1.1 augustss int error, s;
177 1.1 augustss
178 1.1 augustss DPRINTF(("%s\n", __FUNCTION__));
179 1.1 augustss
180 1.1 augustss if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
181 1.1 augustss return (error);
182 1.1 augustss
183 1.1 augustss s = spltty();
184 1.1 augustss
185 1.7 augustss DPRINTF(("%s: linesw=%p disc=%s\n", __FUNCTION__, tp->t_linesw,
186 1.7 augustss tp->t_linesw->l_name));
187 1.7 augustss if (strcmp(tp->t_linesw->l_name, "irframe") == 0) { /* XXX */
188 1.1 augustss sc = (struct irframet_softc *)tp->t_sc;
189 1.1 augustss DPRINTF(("%s: sc=%p sc_tp=%p\n", __FUNCTION__, sc, sc->sc_tp));
190 1.1 augustss if (sc != NULL) {
191 1.1 augustss splx(s);
192 1.1 augustss return (EBUSY);
193 1.1 augustss }
194 1.1 augustss }
195 1.1 augustss
196 1.1 augustss tp->t_sc = irframe_alloc(sizeof (struct irframet_softc),
197 1.1 augustss &irframet_methods, tp);
198 1.1 augustss sc = (struct irframet_softc *)tp->t_sc;
199 1.1 augustss sc->sc_tp = tp;
200 1.7 augustss printf("%s attached at tty%02d\n", sc->sc_irp.sc_dev.dv_xname,
201 1.7 augustss minor(tp->t_dev));
202 1.1 augustss
203 1.1 augustss DPRINTF(("%s: set sc=%p\n", __FUNCTION__, sc));
204 1.1 augustss
205 1.1 augustss ttyflush(tp, FREAD | FWRITE);
206 1.1 augustss
207 1.5 augustss sc->sc_dongle = DONGLE_NONE;
208 1.5 augustss
209 1.1 augustss splx(s);
210 1.2 augustss
211 1.1 augustss return (0);
212 1.1 augustss }
213 1.1 augustss
214 1.1 augustss /*
215 1.1 augustss * Line specific close routine, called from device close routine
216 1.1 augustss * and from ttioctl.
217 1.1 augustss * Detach the tty from the irframe unit.
218 1.1 augustss * Mimics part of ttyclose().
219 1.1 augustss */
220 1.1 augustss int
221 1.1 augustss irframetclose(struct tty *tp, int flag)
222 1.1 augustss {
223 1.1 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
224 1.1 augustss int s;
225 1.1 augustss
226 1.1 augustss DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
227 1.1 augustss
228 1.1 augustss s = spltty();
229 1.1 augustss ttyflush(tp, FREAD | FWRITE);
230 1.1 augustss tp->t_linesw = linesw[0]; /* default line discipline */
231 1.1 augustss if (sc != NULL) {
232 1.1 augustss tp->t_sc = NULL;
233 1.2 augustss printf("%s detached from tty%02d\n", sc->sc_irp.sc_dev.dv_xname,
234 1.1 augustss minor(tp->t_dev));
235 1.1 augustss
236 1.1 augustss if (sc->sc_tp == tp)
237 1.1 augustss irframe_dealloc(&sc->sc_irp.sc_dev);
238 1.1 augustss }
239 1.1 augustss splx(s);
240 1.1 augustss return (0);
241 1.1 augustss }
242 1.1 augustss
243 1.1 augustss /*
244 1.1 augustss * Line specific (tty) ioctl routine.
245 1.1 augustss * This discipline requires that tty device drivers call
246 1.1 augustss * the line specific l_ioctl routine from their ioctl routines.
247 1.1 augustss */
248 1.1 augustss /* ARGSUSED */
249 1.1 augustss int
250 1.1 augustss irframetioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
251 1.1 augustss struct proc *p)
252 1.1 augustss {
253 1.1 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
254 1.1 augustss int error;
255 1.1 augustss
256 1.1 augustss DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
257 1.1 augustss
258 1.1 augustss if (sc == NULL || tp != sc->sc_tp)
259 1.1 augustss return (-1);
260 1.1 augustss
261 1.1 augustss error = 0;
262 1.1 augustss switch (cmd) {
263 1.5 augustss case IRFRAMETTY_GET_DEVICE:
264 1.5 augustss *(int *)data = sc->sc_irp.sc_dev.dv_unit;
265 1.5 augustss break;
266 1.5 augustss case IRFRAMETTY_GET_DONGLE:
267 1.5 augustss *(int *)data = sc->sc_dongle;
268 1.5 augustss break;
269 1.5 augustss case IRFRAMETTY_SET_DONGLE:
270 1.5 augustss sc->sc_dongle = *(int *)data;
271 1.5 augustss break;
272 1.1 augustss default:
273 1.1 augustss error = EINVAL;
274 1.1 augustss break;
275 1.1 augustss }
276 1.1 augustss
277 1.1 augustss return (error);
278 1.1 augustss }
279 1.1 augustss
280 1.1 augustss /*
281 1.9 augustss * Start output on async tty interface.
282 1.1 augustss * Called at spltty or higher.
283 1.1 augustss */
284 1.1 augustss int
285 1.1 augustss irframetstart(struct tty *tp)
286 1.1 augustss {
287 1.1 augustss /*struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;*/
288 1.1 augustss
289 1.1 augustss DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
290 1.1 augustss
291 1.1 augustss if (tp->t_oproc != NULL)
292 1.1 augustss (*tp->t_oproc)(tp);
293 1.1 augustss
294 1.1 augustss return (0);
295 1.1 augustss }
296 1.1 augustss
297 1.2 augustss void
298 1.2 augustss irt_frame(struct irframet_softc *sc, u_char *buf, u_int len)
299 1.2 augustss {
300 1.7 augustss DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
301 1.7 augustss __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
302 1.7 augustss
303 1.2 augustss if (sc->sc_nframes >= MAXFRAMES) {
304 1.2 augustss #ifdef IRFRAMET_DEBUG
305 1.2 augustss printf("%s: dropped frame\n", __FUNCTION__);
306 1.2 augustss #endif
307 1.2 augustss return;
308 1.2 augustss }
309 1.2 augustss if (sc->sc_frames[sc->sc_framei].buf == NULL)
310 1.2 augustss return;
311 1.2 augustss memcpy(sc->sc_frames[sc->sc_framei].buf, buf, len);
312 1.2 augustss sc->sc_frames[sc->sc_framei].len = len;
313 1.2 augustss sc->sc_framei = (sc->sc_framei+1) % MAXFRAMES;
314 1.2 augustss sc->sc_nframes++;
315 1.2 augustss if (sc->sc_state & IRT_RSLP) {
316 1.2 augustss sc->sc_state &= ~IRT_RSLP;
317 1.2 augustss DPRINTF(("%s: waking up reader\n", __FUNCTION__));
318 1.2 augustss wakeup(sc->sc_frames);
319 1.2 augustss }
320 1.2 augustss selwakeup(&sc->sc_rsel);
321 1.2 augustss }
322 1.2 augustss
323 1.2 augustss void
324 1.2 augustss irt_timeout(void *v)
325 1.2 augustss {
326 1.2 augustss struct irframet_softc *sc = v;
327 1.2 augustss
328 1.2 augustss #ifdef IRFRAMET_DEBUG
329 1.2 augustss if (sc->sc_framestate != FRAME_OUTSIDE)
330 1.2 augustss printf("%s: input frame timeout\n", __FUNCTION__);
331 1.2 augustss #endif
332 1.2 augustss sc->sc_framestate = FRAME_OUTSIDE;
333 1.2 augustss }
334 1.2 augustss
335 1.1 augustss int
336 1.1 augustss irframetinput(int c, struct tty *tp)
337 1.1 augustss {
338 1.1 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
339 1.1 augustss
340 1.7 augustss c &= 0xff;
341 1.7 augustss
342 1.7 augustss #if IRFRAMET_DEBUG
343 1.7 augustss if (irframetdebug > 1)
344 1.7 augustss DPRINTF(("%s: tp=%p c=0x%02x\n", __FUNCTION__, tp, c));
345 1.7 augustss #endif
346 1.1 augustss
347 1.1 augustss if (sc == NULL || tp != (struct tty *)sc->sc_tp)
348 1.1 augustss return (0);
349 1.1 augustss
350 1.2 augustss if (sc->sc_inbuf == NULL)
351 1.2 augustss return (0);
352 1.1 augustss
353 1.2 augustss switch (c) {
354 1.2 augustss case SIR_BOF:
355 1.7 augustss DPRINTF(("%s: BOF\n", __FUNCTION__));
356 1.2 augustss sc->sc_framestate = FRAME_INSIDE;
357 1.2 augustss sc->sc_inchars = 0;
358 1.2 augustss sc->sc_inFCS = INITFCS;
359 1.2 augustss break;
360 1.2 augustss case SIR_EOF:
361 1.7 augustss DPRINTF(("%s: EOF state=%d inchars=%d fcs=0x%04x\n",
362 1.7 augustss __FUNCTION__,
363 1.7 augustss sc->sc_framestate, sc->sc_inchars, sc->sc_inFCS));
364 1.2 augustss if (sc->sc_framestate == FRAME_INSIDE &&
365 1.2 augustss sc->sc_inchars >= 4 && sc->sc_inFCS == GOODFCS) {
366 1.2 augustss irt_frame(sc, sc->sc_inbuf, sc->sc_inchars - 2);
367 1.2 augustss } else if (sc->sc_framestate != FRAME_OUTSIDE) {
368 1.2 augustss #ifdef IRFRAMET_DEBUG
369 1.2 augustss printf("%s: malformed input frame\n", __FUNCTION__);
370 1.2 augustss #endif
371 1.2 augustss }
372 1.2 augustss sc->sc_framestate = FRAME_OUTSIDE;
373 1.2 augustss break;
374 1.2 augustss case SIR_CE:
375 1.7 augustss DPRINTF(("%s: CE\n", __FUNCTION__));
376 1.2 augustss if (sc->sc_framestate == FRAME_INSIDE)
377 1.2 augustss sc->sc_framestate = FRAME_ESCAPE;
378 1.2 augustss break;
379 1.2 augustss default:
380 1.8 augustss #if IRFRAMET_DEBUG
381 1.8 augustss if (irframetdebug > 1)
382 1.7 augustss DPRINTF(("%s: c=0x%02x, inchar=%d state=%d\n", __FUNCTION__, c,
383 1.7 augustss sc->sc_inchars, sc->sc_state));
384 1.8 augustss #endif
385 1.2 augustss if (sc->sc_framestate != FRAME_OUTSIDE) {
386 1.2 augustss if (sc->sc_framestate == FRAME_ESCAPE) {
387 1.2 augustss sc->sc_framestate = FRAME_INSIDE;
388 1.2 augustss c ^= SIR_ESC_BIT;
389 1.2 augustss }
390 1.2 augustss if (sc->sc_inchars < sc->sc_maxsize + 2) {
391 1.2 augustss sc->sc_inbuf[sc->sc_inchars++] = c;
392 1.2 augustss sc->sc_inFCS = updateFCS(sc->sc_inFCS, c);
393 1.2 augustss } else {
394 1.2 augustss sc->sc_framestate = FRAME_OUTSIDE;
395 1.2 augustss #ifdef IRFRAMET_DEBUG
396 1.2 augustss printf("%s: input frame overrun\n",
397 1.2 augustss __FUNCTION__);
398 1.2 augustss #endif
399 1.2 augustss }
400 1.2 augustss }
401 1.2 augustss break;
402 1.1 augustss }
403 1.1 augustss
404 1.7 augustss #if 1
405 1.2 augustss if (sc->sc_framestate != FRAME_OUTSIDE) {
406 1.7 augustss callout_reset(&sc->sc_timeout, hz/20, irt_timeout, sc);
407 1.1 augustss }
408 1.7 augustss #endif
409 1.1 augustss
410 1.2 augustss return (0);
411 1.1 augustss }
412 1.1 augustss
413 1.1 augustss
414 1.1 augustss /*** irframe methods ***/
415 1.1 augustss
416 1.1 augustss int
417 1.1 augustss irframet_open(void *h, int flag, int mode, struct proc *p)
418 1.1 augustss {
419 1.1 augustss struct tty *tp = h;
420 1.3 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
421 1.1 augustss
422 1.1 augustss DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
423 1.3 augustss
424 1.4 augustss sc->sc_speed = 0;
425 1.3 augustss sc->sc_ebofs = IRDA_DEFAULT_EBOFS;
426 1.3 augustss sc->sc_maxsize = 0;
427 1.3 augustss sc->sc_framestate = FRAME_OUTSIDE;
428 1.3 augustss sc->sc_nframes = 0;
429 1.3 augustss sc->sc_framei = 0;
430 1.3 augustss sc->sc_frameo = 0;
431 1.3 augustss callout_init(&sc->sc_timeout);
432 1.3 augustss
433 1.1 augustss return (0);
434 1.1 augustss }
435 1.1 augustss
436 1.1 augustss int
437 1.1 augustss irframet_close(void *h, int flag, int mode, struct proc *p)
438 1.1 augustss {
439 1.1 augustss struct tty *tp = h;
440 1.3 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
441 1.3 augustss int i, s;
442 1.1 augustss
443 1.1 augustss DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
444 1.3 augustss
445 1.3 augustss callout_stop(&sc->sc_timeout);
446 1.3 augustss s = splir();
447 1.3 augustss if (sc->sc_inbuf != NULL) {
448 1.3 augustss free(sc->sc_inbuf, M_DEVBUF);
449 1.3 augustss sc->sc_inbuf = NULL;
450 1.3 augustss }
451 1.3 augustss for (i = 0; i < MAXFRAMES; i++) {
452 1.3 augustss if (sc->sc_frames[i].buf != NULL) {
453 1.3 augustss free(sc->sc_frames[i].buf, M_DEVBUF);
454 1.3 augustss sc->sc_frames[i].buf = NULL;
455 1.3 augustss }
456 1.3 augustss }
457 1.3 augustss splx(s);
458 1.3 augustss
459 1.1 augustss return (0);
460 1.1 augustss }
461 1.1 augustss
462 1.1 augustss int
463 1.1 augustss irframet_read(void *h, struct uio *uio, int flag)
464 1.1 augustss {
465 1.1 augustss struct tty *tp = h;
466 1.2 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
467 1.2 augustss int error = 0;
468 1.2 augustss int s;
469 1.1 augustss
470 1.2 augustss DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
471 1.2 augustss __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
472 1.2 augustss (long)uio->uio_offset));
473 1.7 augustss DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
474 1.7 augustss __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
475 1.7 augustss
476 1.2 augustss
477 1.2 augustss s = splir();
478 1.2 augustss while (sc->sc_nframes == 0) {
479 1.2 augustss if (flag & IO_NDELAY) {
480 1.2 augustss splx(s);
481 1.2 augustss return (EWOULDBLOCK);
482 1.2 augustss }
483 1.2 augustss sc->sc_state |= IRT_RSLP;
484 1.2 augustss DPRINTF(("%s: sleep\n", __FUNCTION__));
485 1.2 augustss error = tsleep(sc->sc_frames, PZERO | PCATCH, "irtrd", 0);
486 1.2 augustss DPRINTF(("%s: woke, error=%d\n", __FUNCTION__, error));
487 1.2 augustss if (error) {
488 1.2 augustss sc->sc_state &= ~IRT_RSLP;
489 1.2 augustss break;
490 1.2 augustss }
491 1.2 augustss }
492 1.2 augustss
493 1.2 augustss /* Do just one frame transfer per read */
494 1.2 augustss if (!error) {
495 1.2 augustss if (uio->uio_resid < sc->sc_frames[sc->sc_frameo].len) {
496 1.2 augustss DPRINTF(("%s: uio buffer smaller than frame size (%d < %d)\n", __FUNCTION__, uio->uio_resid, sc->sc_frames[sc->sc_frameo].len));
497 1.2 augustss error = EINVAL;
498 1.2 augustss } else {
499 1.2 augustss DPRINTF(("%s: moving %d bytes\n", __FUNCTION__,
500 1.2 augustss sc->sc_frames[sc->sc_frameo].len));
501 1.7 augustss error = uiomove(sc->sc_frames[sc->sc_frameo].buf,
502 1.2 augustss sc->sc_frames[sc->sc_frameo].len, uio);
503 1.7 augustss DPRINTF(("%s: error=%d\n", __FUNCTION__, error));
504 1.2 augustss }
505 1.7 augustss sc->sc_frameo = (sc->sc_frameo+1) % MAXFRAMES;
506 1.2 augustss sc->sc_nframes--;
507 1.2 augustss }
508 1.2 augustss splx(s);
509 1.2 augustss
510 1.2 augustss return (error);
511 1.1 augustss }
512 1.1 augustss
513 1.2 augustss int
514 1.2 augustss irt_putc(int c, struct tty *tp)
515 1.1 augustss {
516 1.1 augustss int s;
517 1.1 augustss int error;
518 1.1 augustss
519 1.9 augustss #if IRFRAMET_DEBUG
520 1.9 augustss if (irframetdebug > 3)
521 1.9 augustss DPRINTF(("%s: tp=%p c=0x%02x cc=%d\n", __FUNCTION__, tp, c,
522 1.9 augustss tp->t_outq.c_cc));
523 1.9 augustss #endif
524 1.1 augustss if (tp->t_outq.c_cc > tp->t_hiwat) {
525 1.1 augustss irframetstart(tp);
526 1.1 augustss s = spltty();
527 1.1 augustss /*
528 1.1 augustss * This can only occur if FLUSHO is set in t_lflag,
529 1.1 augustss * or if ttstart/oproc is synchronous (or very fast).
530 1.1 augustss */
531 1.1 augustss if (tp->t_outq.c_cc <= tp->t_hiwat) {
532 1.1 augustss splx(s);
533 1.1 augustss goto go;
534 1.1 augustss }
535 1.1 augustss SET(tp->t_state, TS_ASLEEP);
536 1.1 augustss error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
537 1.1 augustss splx(s);
538 1.1 augustss if (error)
539 1.1 augustss return (error);
540 1.1 augustss }
541 1.1 augustss go:
542 1.8 augustss if (putc(c, &tp->t_outq) < 0) {
543 1.1 augustss printf("irframe: putc failed\n");
544 1.1 augustss return (EIO);
545 1.1 augustss }
546 1.1 augustss return (0);
547 1.1 augustss }
548 1.1 augustss
549 1.2 augustss int
550 1.1 augustss irframet_write(void *h, struct uio *uio, int flag)
551 1.1 augustss {
552 1.9 augustss struct tty *tp = h;
553 1.9 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
554 1.1 augustss u_int8_t buf[MAX_IRDA_FRAME];
555 1.9 augustss int n;
556 1.1 augustss
557 1.7 augustss DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
558 1.7 augustss __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
559 1.7 augustss (long)uio->uio_offset));
560 1.1 augustss
561 1.9 augustss n = irda_sir_frame(buf, MAX_IRDA_FRAME, uio, sc->sc_ebofs);
562 1.9 augustss if (n < 0)
563 1.9 augustss return (-n);
564 1.9 augustss return (irt_write_frame(h, buf, n));
565 1.1 augustss }
566 1.1 augustss
567 1.1 augustss int
568 1.9 augustss irt_write_frame(void *h, u_int8_t *buf, size_t len)
569 1.1 augustss {
570 1.1 augustss struct tty *tp = h;
571 1.9 augustss int error, i;
572 1.1 augustss
573 1.1 augustss DPRINTF(("%s: tp=%p len=%d\n", __FUNCTION__, tp, len));
574 1.1 augustss
575 1.1 augustss error = 0;
576 1.9 augustss for (i = 0; !error && i < len; i++)
577 1.9 augustss error = irt_putc(buf[i], tp);
578 1.1 augustss
579 1.1 augustss irframetstart(tp);
580 1.1 augustss
581 1.9 augustss DPRINTF(("%s: done, error=%d\n", __FUNCTION__, error));
582 1.7 augustss
583 1.1 augustss return (error);
584 1.1 augustss }
585 1.1 augustss
586 1.1 augustss int
587 1.1 augustss irframet_poll(void *h, int events, struct proc *p)
588 1.1 augustss {
589 1.2 augustss struct tty *tp = h;
590 1.2 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
591 1.1 augustss int revents = 0;
592 1.1 augustss int s;
593 1.1 augustss
594 1.1 augustss DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
595 1.1 augustss
596 1.1 augustss s = splir();
597 1.9 augustss /* XXX is this a good check? */
598 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
599 1.9 augustss if (tp->t_outq.c_cc <= tp->t_lowat)
600 1.9 augustss revents |= events & (POLLOUT | POLLWRNORM);
601 1.9 augustss
602 1.1 augustss if (events & (POLLIN | POLLRDNORM)) {
603 1.2 augustss if (sc->sc_nframes > 0) {
604 1.1 augustss DPRINTF(("%s: have data\n", __FUNCTION__));
605 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
606 1.1 augustss } else {
607 1.7 augustss DPRINTF(("%s: recording select\n", __FUNCTION__));
608 1.1 augustss selrecord(p, &sc->sc_rsel);
609 1.1 augustss }
610 1.1 augustss }
611 1.1 augustss splx(s);
612 1.1 augustss
613 1.1 augustss return (revents);
614 1.1 augustss }
615 1.1 augustss
616 1.1 augustss int
617 1.1 augustss irframet_set_params(void *h, struct irda_params *p)
618 1.1 augustss {
619 1.1 augustss struct tty *tp = h;
620 1.3 augustss struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
621 1.3 augustss struct termios tt;
622 1.2 augustss int i;
623 1.1 augustss
624 1.7 augustss DPRINTF(("%s: tp=%p speed=%d ebofs=%d maxsize=%d\n",
625 1.7 augustss __FUNCTION__, tp, p->speed, p->ebofs, p->maxsize));
626 1.1 augustss
627 1.4 augustss if (p->speed != sc->sc_speed) {
628 1.4 augustss switch (p->speed) {
629 1.4 augustss case 2400:
630 1.4 augustss case 9600:
631 1.4 augustss case 19200:
632 1.4 augustss case 38400:
633 1.4 augustss case 57600:
634 1.4 augustss case 115200:
635 1.4 augustss break;
636 1.4 augustss default: return (EINVAL);
637 1.4 augustss }
638 1.4 augustss ttioctl(tp, TIOCGETA, (caddr_t)&tt, 0, curproc);
639 1.4 augustss sc->sc_speed = tt.c_ispeed = tt.c_ospeed = p->speed;
640 1.4 augustss ttioctl(tp, TIOCSETAF, (caddr_t)&tt, 0, curproc);
641 1.3 augustss }
642 1.3 augustss
643 1.1 augustss sc->sc_ebofs = p->ebofs;
644 1.2 augustss if (sc->sc_maxsize != p->maxsize) {
645 1.2 augustss sc->sc_maxsize = p->maxsize;
646 1.2 augustss if (sc->sc_inbuf != NULL)
647 1.2 augustss free(sc->sc_inbuf, M_DEVBUF);
648 1.2 augustss for (i = 0; i < MAXFRAMES; i++)
649 1.2 augustss if (sc->sc_frames[i].buf != NULL)
650 1.2 augustss free(sc->sc_frames[i].buf, M_DEVBUF);
651 1.2 augustss if (sc->sc_maxsize != 0) {
652 1.2 augustss sc->sc_inbuf = malloc(sc->sc_maxsize+2, M_DEVBUF,
653 1.2 augustss M_WAITOK);
654 1.2 augustss for (i = 0; i < MAXFRAMES; i++)
655 1.2 augustss sc->sc_frames[i].buf = malloc(sc->sc_maxsize,
656 1.2 augustss M_DEVBUF, M_WAITOK);
657 1.2 augustss } else {
658 1.2 augustss sc->sc_inbuf = NULL;
659 1.2 augustss for (i = 0; i < MAXFRAMES; i++)
660 1.2 augustss sc->sc_frames[i].buf = NULL;
661 1.2 augustss }
662 1.2 augustss }
663 1.2 augustss sc->sc_framestate = FRAME_OUTSIDE;
664 1.1 augustss
665 1.1 augustss return (0);
666 1.1 augustss }
667 1.1 augustss
668 1.1 augustss int
669 1.1 augustss irframet_get_speeds(void *h, int *speeds)
670 1.1 augustss {
671 1.1 augustss struct tty *tp = h;
672 1.1 augustss
673 1.1 augustss tp = tp;
674 1.1 augustss DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
675 1.1 augustss *speeds = IRDA_SPEEDS_SIR;
676 1.1 augustss return (0);
677 1.1 augustss }
678 1.1 augustss
679 1.1 augustss int
680 1.1 augustss irframet_get_turnarounds(void *h, int *turnarounds)
681 1.1 augustss {
682 1.1 augustss struct tty *tp = h;
683 1.1 augustss
684 1.1 augustss tp = tp;
685 1.1 augustss DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
686 1.1 augustss *turnarounds = IRDA_TURNT_10000;
687 1.1 augustss return (0);
688 1.1 augustss }
689