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