1 /* $NetBSD: ucycom.c,v 1.16 2006/11/16 01:33:26 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Nick Hudson 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 /* 39 * This code is based on the ucom driver. 40 */ 41 42 /* 43 * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to 44 * RS232 bridges. 45 */ 46 47 #include <sys/cdefs.h> 48 __RCSID("$NetBSD: ucycom.c,v 1.16 2006/11/16 01:33:26 christos Exp $"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/conf.h> 53 #include <sys/kernel.h> 54 #include <sys/malloc.h> 55 #include <sys/device.h> 56 #include <sys/sysctl.h> 57 #include <sys/tty.h> 58 #include <sys/file.h> 59 #include <sys/vnode.h> 60 #include <sys/kauth.h> 61 62 #include <dev/usb/usb.h> 63 #include <dev/usb/usbhid.h> 64 65 #include <dev/usb/usbdi.h> 66 #include <dev/usb/usbdi_util.h> 67 #include <dev/usb/usbdevs.h> 68 #include <dev/usb/uhidev.h> 69 #include <dev/usb/hid.h> 70 71 #include "ioconf.h" 72 73 #ifdef UCYCOM_DEBUG 74 #define DPRINTF(x) if (ucycomdebug) logprintf x 75 #define DPRINTFN(n, x) if (ucycomdebug > (n)) logprintf x 76 int ucycomdebug = 0; 77 #else 78 #define DPRINTF(x) 79 #define DPRINTFN(n,x) 80 #endif 81 82 83 #define UCYCOMUNIT_MASK 0x3ffff 84 #define UCYCOMDIALOUT_MASK 0x80000 85 #define UCYCOMCALLUNIT_MASK 0x40000 86 87 #define UCYCOMUNIT(x) (minor(x) & UCYCOMUNIT_MASK) 88 #define UCYCOMDIALOUT(x) (minor(x) & UCYCOMDIALOUT_MASK) 89 #define UCYCOMCALLUNIT(x) (minor(x) & UCYCOMCALLUNIT_MASK) 90 91 /* Configuration Byte */ 92 #define UCYCOM_RESET 0x80 93 #define UCYCOM_PARITY_TYPE_MASK 0x20 94 #define UCYCOM_PARITY_ODD 0x20 95 #define UCYCOM_PARITY_EVEN 0x00 96 #define UCYCOM_PARITY_MASK 0x10 97 #define UCYCOM_PARITY_ON 0x10 98 #define UCYCOM_PARITY_OFF 0x00 99 #define UCYCOM_STOP_MASK 0x08 100 #define UCYCOM_STOP_BITS_2 0x08 101 #define UCYCOM_STOP_BITS_1 0x00 102 #define UCYCOM_DATA_MASK 0x03 103 #define UCYCOM_DATA_BITS_8 0x03 104 #define UCYCOM_DATA_BITS_7 0x02 105 #define UCYCOM_DATA_BITS_6 0x01 106 #define UCYCOM_DATA_BITS_5 0x00 107 108 /* Modem (Input) status byte */ 109 #define UCYCOM_RI 0x80 110 #define UCYCOM_DCD 0x40 111 #define UCYCOM_DSR 0x20 112 #define UCYCOM_CTS 0x10 113 #define UCYCOM_ERROR 0x08 114 #define UCYCOM_LMASK 0x07 115 116 /* Modem (Output) control byte */ 117 #define UCYCOM_DTR 0x20 118 #define UCYCOM_RTS 0x10 119 #define UCYCOM_ORESET 0x08 120 121 struct ucycom_softc { 122 struct uhidev sc_hdev; 123 124 struct tty *sc_tty; 125 126 /* uhidev parameters */ 127 size_t sc_flen; /* feature report length */ 128 size_t sc_ilen; /* input report length */ 129 size_t sc_olen; /* output report length */ 130 131 uint8_t *sc_obuf; 132 133 /* settings */ 134 uint32_t sc_baud; 135 uint8_t sc_cfg; /* Data format */ 136 uint8_t sc_mcr; /* Modem control */ 137 uint8_t sc_msr; /* Modem status */ 138 int sc_swflags; 139 140 /* flags */ 141 char sc_dying; 142 }; 143 144 dev_type_open(ucycomopen); 145 dev_type_close(ucycomclose); 146 dev_type_read(ucycomread); 147 dev_type_write(ucycomwrite); 148 dev_type_ioctl(ucycomioctl); 149 dev_type_stop(ucycomstop); 150 dev_type_tty(ucycomtty); 151 dev_type_poll(ucycompoll); 152 153 const struct cdevsw ucycom_cdevsw = { 154 ucycomopen, ucycomclose, ucycomread, ucycomwrite, ucycomioctl, 155 ucycomstop, ucycomtty, ucycompoll, nommap, ttykqfilter, D_TTY 156 }; 157 158 Static int ucycomparam(struct tty *, struct termios *); 159 Static void ucycomstart(struct tty *); 160 Static void ucycom_intr(struct uhidev *, void *, u_int); 161 Static int ucycom_configure(struct ucycom_softc *, uint32_t, uint8_t); 162 Static void tiocm_to_ucycom(struct ucycom_softc *, u_long, int); 163 Static int ucycom_to_tiocm(struct ucycom_softc *); 164 Static void ucycom_set_status(struct ucycom_softc *); 165 Static void ucycom_dtr(struct ucycom_softc *, int); 166 #if 0 167 Static void ucycom_rts(struct ucycom_softc *, int); 168 #endif 169 Static void ucycom_cleanup(struct ucycom_softc *sc); 170 171 #ifdef UCYCOM_DEBUG 172 Static void ucycom_get_cfg(struct ucycom_softc *); 173 #endif 174 175 Static const struct usb_devno ucycom_devs[] = { 176 { USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 }, 177 { USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE }, 178 }; 179 #define ucycom_lookup(v, p) usb_lookup(ucycom_devs, v, p) 180 181 USB_DECLARE_DRIVER(ucycom); 182 183 int 184 ucycom_match(struct device *parent, struct cfdata *match, 185 void *aux) 186 { 187 struct uhidev_attach_arg *uha = aux; 188 189 return (ucycom_lookup(uha->uaa->vendor, uha->uaa->product) != NULL ? 190 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 191 } 192 193 void 194 ucycom_attach(struct device *parent, struct device *self, void *aux) 195 { 196 struct ucycom_softc *sc = (struct ucycom_softc *)self; 197 struct uhidev_attach_arg *uha = aux; 198 int size, repid; 199 void *desc; 200 201 sc->sc_hdev.sc_intr = ucycom_intr; 202 sc->sc_hdev.sc_parent = uha->parent; 203 sc->sc_hdev.sc_report_id = uha->reportid; 204 205 uhidev_get_report_desc(uha->parent, &desc, &size); 206 repid = uha->reportid; 207 sc->sc_ilen = hid_report_size(desc, size, hid_input, repid); 208 sc->sc_olen = hid_report_size(desc, size, hid_output, repid); 209 sc->sc_flen = hid_report_size(desc, size, hid_feature, repid); 210 211 sc->sc_msr = sc->sc_mcr = 0; 212 213 /* set up tty */ 214 sc->sc_tty = ttymalloc(); 215 sc->sc_tty->t_sc = sc; 216 sc->sc_tty->t_oproc = ucycomstart; 217 sc->sc_tty->t_param = ucycomparam; 218 219 tty_attach(sc->sc_tty); 220 221 /* Nothing interesting to report */ 222 printf("\n"); 223 224 } 225 226 227 int 228 ucycom_detach(struct device *self, int flags) 229 { 230 struct ucycom_softc *sc = (struct ucycom_softc *)self; 231 struct tty *tp = sc->sc_tty; 232 int maj, mn; 233 int s; 234 235 DPRINTF(("ucycom_detach: sc=%p flags=%d tp=%p\n", sc, flags, tp)); 236 237 sc->sc_dying = 1; 238 239 s = splusb(); 240 if (tp != NULL) { 241 CLR(tp->t_state, TS_CARR_ON); 242 CLR(tp->t_cflag, CLOCAL | MDMBUF); 243 ttyflush(tp, FREAD|FWRITE); 244 } 245 /* Wait for processes to go away. */ 246 usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev)); 247 splx(s); 248 249 /* locate the major number */ 250 maj = cdevsw_lookup_major(&ucycom_cdevsw); 251 252 /* Nuke the vnodes for any open instances. */ 253 mn = device_unit(self); 254 255 DPRINTFN(2, ("ucycom_detach: maj=%d mn=%d\n", maj, mn)); 256 vdevgone(maj, mn, mn, VCHR); 257 vdevgone(maj, mn | UCYCOMDIALOUT_MASK, mn | UCYCOMDIALOUT_MASK, VCHR); 258 vdevgone(maj, mn | UCYCOMCALLUNIT_MASK, mn | UCYCOMCALLUNIT_MASK, VCHR); 259 260 /* Detach and free the tty. */ 261 if (tp != NULL) { 262 DPRINTF(("ucycom_detach: tty_detach %p\n", tp)); 263 tty_detach(tp); 264 ttyfree(tp); 265 sc->sc_tty = NULL; 266 } 267 268 return 0; 269 } 270 271 int 272 ucycom_activate(device_ptr_t self, enum devact act) 273 { 274 struct ucycom_softc *sc = (struct ucycom_softc *)self; 275 276 DPRINTFN(5,("ucycom_activate: %d\n", act)); 277 278 switch (act) { 279 case DVACT_ACTIVATE: 280 return (EOPNOTSUPP); 281 282 case DVACT_DEACTIVATE: 283 sc->sc_dying = 1; 284 break; 285 } 286 return (0); 287 } 288 289 #if 0 290 void 291 ucycom_shutdown(struct ucycom_softc *sc) 292 { 293 struct tty *tp = sc->sc_tty; 294 295 DPRINTF(("ucycom_shutdown\n")); 296 /* 297 * Hang up if necessary. Wait a bit, so the other side has time to 298 * notice even if we immediately open the port again. 299 */ 300 if (ISSET(tp->t_cflag, HUPCL)) { 301 ucycom_dtr(sc, 0); 302 (void)tsleep(sc, TTIPRI, ttclos, hz); 303 } 304 } 305 #endif 306 307 int 308 ucycomopen(dev_t dev, int flag, int mode, struct lwp *l) 309 { 310 int unit = UCYCOMUNIT(dev); 311 struct ucycom_softc *sc; 312 struct tty *tp; 313 int s, err; 314 315 DPRINTF(("ucycomopen: unit=%d\n", unit)); 316 317 if (unit >= ucycom_cd.cd_ndevs) 318 return (ENXIO); 319 sc = ucycom_cd.cd_devs[unit]; 320 321 DPRINTF(("ucycomopen: sc=%p\n", sc)); 322 323 if (sc == NULL) 324 return (ENXIO); 325 326 if (sc->sc_dying) 327 return (EIO); 328 329 if (!device_is_active(&sc->sc_hdev.sc_dev)) 330 return (ENXIO); 331 332 tp = sc->sc_tty; 333 334 DPRINTF(("ucycomopen: tp=%p\n", tp)); 335 336 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 337 return (EBUSY); 338 339 s = spltty(); 340 341 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 342 struct termios t; 343 344 tp->t_dev = dev; 345 346 err = uhidev_open(&sc->sc_hdev); 347 if (err) { 348 /* Any cleanup? */ 349 splx(s); 350 return (err); 351 } 352 353 /* 354 * Initialize the termios status to the defaults. Add in the 355 * sticky bits from TIOCSFLAGS. 356 */ 357 t.c_ispeed = 0; 358 t.c_ospeed = TTYDEF_SPEED; 359 t.c_cflag = TTYDEF_CFLAG; 360 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 361 SET(t.c_cflag, CLOCAL); 362 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 363 SET(t.c_cflag, CRTSCTS); 364 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 365 SET(t.c_cflag, MDMBUF); 366 367 tp->t_ospeed = 0; 368 (void) ucycomparam(tp, &t); 369 tp->t_iflag = TTYDEF_IFLAG; 370 tp->t_oflag = TTYDEF_OFLAG; 371 tp->t_lflag = TTYDEF_LFLAG; 372 ttychars(tp); 373 ttsetwater(tp); 374 375 /* Allocate an output report buffer */ 376 sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK); 377 378 DPRINTF(("ucycomopen: sc->sc_obuf=%p\n", sc->sc_obuf)); 379 380 #if 0 381 /* XXX Don't do this as for some reason trying to do an interrupt 382 * XXX out transfer at this point means everything gets stuck!?! 383 */ 384 /* 385 * Turn on DTR. We must always do this, even if carrier is not 386 * present, because otherwise we'd have to use TIOCSDTR 387 * immediately after setting CLOCAL, which applications do not 388 * expect. We always assert DTR while the device is open 389 * unless explicitly requested to deassert it. 390 */ 391 ucycom_dtr(sc, 1); 392 #endif 393 394 #if 0 395 /* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/ 396 ucycom_hwiflow(sc); 397 #endif 398 399 } 400 splx(s); 401 402 err = ttyopen(tp, UCYCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 403 if (err) 404 goto bad; 405 406 err = (*tp->t_linesw->l_open)(dev, tp); 407 if (err) 408 goto bad; 409 410 return (0); 411 412 bad: 413 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 414 /* 415 * We failed to open the device, and nobody else had it opened. 416 * Clean up the state as appropriate. 417 */ 418 ucycom_cleanup(sc); 419 } 420 421 return (err); 422 423 } 424 425 426 int 427 ucycomclose(dev_t dev, int flag, int mode, struct lwp *l) 428 { 429 struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)]; 430 struct tty *tp = sc->sc_tty; 431 432 DPRINTF(("ucycomclose: unit=%d\n", UCYCOMUNIT(dev))); 433 if (!ISSET(tp->t_state, TS_ISOPEN)) 434 return (0); 435 436 (*tp->t_linesw->l_close)(tp, flag); 437 ttyclose(tp); 438 439 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 440 /* 441 * Although we got a last close, the device may still be in 442 * use; e.g. if this was the dialout node, and there are still 443 * processes waiting for carrier on the non-dialout node. 444 */ 445 ucycom_cleanup(sc); 446 } 447 448 return (0); 449 } 450 451 Static void 452 ucycomstart(struct tty *tp) 453 { 454 struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(tp->t_dev)]; 455 u_char *data; 456 int cnt, len, err, s; 457 458 if (sc->sc_dying) 459 return; 460 461 s = spltty(); 462 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) { 463 DPRINTFN(4,("ucycomstart: no go, state=0x%x\n", tp->t_state)); 464 goto out; 465 } 466 467 #if 0 468 /* HW FLOW CTL */ 469 if (sc->sc_tx_stopped) 470 goto out; 471 #endif 472 473 if (tp->t_outq.c_cc <= tp->t_lowat) { 474 if (ISSET(tp->t_state, TS_ASLEEP)) { 475 CLR(tp->t_state, TS_ASLEEP); 476 wakeup(&tp->t_outq); 477 } 478 selwakeup(&tp->t_wsel); 479 if (tp->t_outq.c_cc == 0) 480 goto out; 481 } 482 483 /* Grab the first contiguous region of buffer space. */ 484 data = tp->t_outq.c_cf; 485 cnt = ndqb(&tp->t_outq, 0); 486 487 if (cnt == 0) { 488 DPRINTF(("ucycomstart: cnt == 0\n")); 489 goto out; 490 } 491 492 SET(tp->t_state, TS_BUSY); 493 494 /* 495 * The 8 byte output report uses byte 0 for control and byte 496 * count. 497 * 498 * The 32 byte output report uses byte 0 for control. Byte 1 499 * is used for byte count. 500 */ 501 memset(sc->sc_obuf, 0, sc->sc_olen); 502 len = cnt; 503 switch (sc->sc_olen) { 504 case 8: 505 if (cnt > sc->sc_olen - 1) { 506 DPRINTF(("ucycomstart(8): big buffer %d chars\n", len)); 507 len = sc->sc_olen - 1; 508 } 509 510 memcpy(sc->sc_obuf + 1, data, len); 511 sc->sc_obuf[0] = len | sc->sc_mcr; 512 513 DPRINTF(("ucycomstart(8): sc->sc_obuf[0] = %d | %d = %d\n", len, sc->sc_mcr, sc->sc_obuf[0])); 514 #ifdef UCYCOM_DEBUG 515 if (ucycomdebug > 10) { 516 u_int32_t i; 517 u_int8_t *d = data; 518 519 DPRINTF(("ucycomstart(8): data =")); 520 for (i = 0; i < len; i++) 521 DPRINTF((" %02x", d[i])); 522 DPRINTF(("\n")); 523 } 524 #endif 525 break; 526 527 case 32: 528 if (cnt > sc->sc_olen - 2) { 529 DPRINTF(("ucycomstart(32): big buffer %d chars\n", len)); 530 len = sc->sc_olen - 2; 531 } 532 533 memcpy(sc->sc_obuf + 2, data, len); 534 sc->sc_obuf[0] = sc->sc_mcr; 535 sc->sc_obuf[1] = len; 536 DPRINTF(("ucycomstart(32): sc->sc_obuf[0] = %d\nsc->sc_obuf[1] = %d\n", sc->sc_obuf[0], sc->sc_obuf[1])); 537 #ifdef UCYCOM_DEBUG 538 if (ucycomdebug > 10) { 539 u_int32_t i; 540 u_int8_t *d = data; 541 542 DPRINTF(("ucycomstart(32): data =")); 543 for (i = 0; i < len; i++) 544 DPRINTF((" %02x", d[i])); 545 DPRINTF(("\n")); 546 } 547 #endif 548 break; 549 550 default: 551 DPRINTFN(2,("ucycomstart: unknown output report size (%zd)\n", 552 sc->sc_olen)); 553 goto out; 554 } 555 splx(s); 556 557 #ifdef UCYCOM_DEBUG 558 if (ucycomdebug > 5) { 559 int i; 560 561 if (len != 0) { 562 DPRINTF(("ucycomstart: sc->sc_obuf[0..%zd) =", sc->sc_olen)); 563 for (i = 0; i < sc->sc_olen; i++) 564 DPRINTF((" %02x", sc->sc_obuf[i])); 565 DPRINTF(("\n")); 566 } 567 } 568 #endif 569 err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen); 570 571 if (err) { 572 DPRINTF(("ucycomstart: error doing uhidev_write = %d\n", err)); 573 } 574 575 #ifdef UCYCOM_DEBUG 576 ucycom_get_cfg(sc); 577 #endif 578 DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", cnt, len)); 579 580 s = spltty(); 581 CLR(tp->t_state, TS_BUSY); 582 if (ISSET(tp->t_state, TS_FLUSH)) 583 CLR(tp->t_state, TS_FLUSH); 584 else 585 ndflush(&tp->t_outq, len); 586 (*tp->t_linesw->l_start)(tp); 587 588 out: 589 splx(s); 590 } 591 592 Static int 593 ucycomparam(struct tty *tp, struct termios *t) 594 { 595 struct ucycom_softc *sc = tp->t_sc; 596 uint32_t baud; 597 uint8_t cfg; 598 int err; 599 600 if (t->c_ospeed < 0) { 601 DPRINTF(("ucycomparam: c_ospeed < 0\n")); 602 return (EINVAL); 603 } 604 605 /* Check requested parameters. */ 606 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 607 return (EINVAL); 608 609 /* 610 * For the console, always force CLOCAL and !HUPCL, so that the port 611 * is always active. 612 */ 613 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) { 614 SET(t->c_cflag, CLOCAL); 615 CLR(t->c_cflag, HUPCL); 616 } 617 618 /* 619 * If there were no changes, don't do anything. This avoids dropping 620 * input and improves performance when all we did was frob things like 621 * VMIN and VTIME. 622 */ 623 if (tp->t_ospeed == t->c_ospeed && 624 tp->t_cflag == t->c_cflag) 625 return (0); 626 627 /* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */ 628 629 /* And copy to tty. */ 630 tp->t_ispeed = 0; 631 tp->t_ospeed = t->c_ospeed; 632 tp->t_cflag = t->c_cflag; 633 634 baud = t->c_ispeed; 635 DPRINTF(("ucycomparam: baud=%d\n", baud)); 636 637 if (t->c_cflag & CIGNORE) { 638 cfg = sc->sc_cfg; 639 } else { 640 cfg = 0; 641 switch (t->c_cflag & CSIZE) { 642 case CS8: 643 cfg |= UCYCOM_DATA_BITS_8; 644 break; 645 case CS7: 646 cfg |= UCYCOM_DATA_BITS_7; 647 break; 648 case CS6: 649 cfg |= UCYCOM_DATA_BITS_6; 650 break; 651 case CS5: 652 cfg |= UCYCOM_DATA_BITS_5; 653 break; 654 default: 655 return (EINVAL); 656 } 657 cfg |= ISSET(t->c_cflag, CSTOPB) ? 658 UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1; 659 cfg |= ISSET(t->c_cflag, PARENB) ? 660 UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF; 661 cfg |= ISSET(t->c_cflag, PARODD) ? 662 UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN; 663 } 664 665 /* 666 * Update the tty layer's idea of the carrier bit, in case we changed 667 * CLOCAL or MDMBUF. We don't hang up here; we only do that by 668 * explicit request. 669 */ 670 DPRINTF(("ucycomparam: l_modem\n")); 671 (void) (*tp->t_linesw->l_modem)(tp, 1 /* XXX carrier */ ); 672 673 err = ucycom_configure(sc, baud, cfg); 674 return (err); 675 } 676 677 void 678 ucycomstop(struct tty *tp, int flag) 679 { 680 DPRINTF(("ucycomstop: flag=%d\n", flag)); 681 } 682 683 int 684 ucycomread(dev_t dev, struct uio *uio, int flag) 685 { 686 struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)]; 687 struct tty *tp = sc->sc_tty; 688 int err; 689 690 DPRINTF(("ucycomread: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio, flag)); 691 if (sc->sc_dying) 692 return (EIO); 693 694 err = ((*tp->t_linesw->l_read)(tp, uio, flag)); 695 return (err); 696 } 697 698 699 int 700 ucycomwrite(dev_t dev, struct uio *uio, int flag) 701 { 702 struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)]; 703 struct tty *tp = sc->sc_tty; 704 int err; 705 706 DPRINTF(("ucycomwrite: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio, flag)); 707 if (sc->sc_dying) 708 return (EIO); 709 710 err = ((*tp->t_linesw->l_write)(tp, uio, flag)); 711 return (err); 712 } 713 714 struct tty * 715 ucycomtty(dev_t dev) 716 { 717 struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)]; 718 struct tty *tp = sc->sc_tty; 719 720 DPRINTF(("ucycomtty: sc=%p, tp=%p\n", sc, tp)); 721 722 return (tp); 723 } 724 725 int 726 ucycomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l) 727 { 728 struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)]; 729 struct tty *tp = sc->sc_tty; 730 int err; 731 int s; 732 733 if (sc->sc_dying) 734 return (EIO); 735 736 DPRINTF(("ucycomioctl: sc=%p, tp=%p, data=%p\n", sc, tp, data)); 737 738 err = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 739 if (err != EPASSTHROUGH) 740 return (err); 741 742 err = ttioctl(tp, cmd, data, flag, l); 743 if (err != EPASSTHROUGH) 744 return (err); 745 746 err = 0; 747 748 DPRINTF(("ucycomioctl: our cmd=0x%08lx\n", cmd)); 749 s = spltty(); 750 751 switch (cmd) { 752 /* case TIOCSBRK: 753 ucycom_break(sc, 1); 754 break; 755 756 case TIOCCBRK: 757 ucycom_break(sc, 0); 758 break; 759 */ 760 case TIOCSDTR: 761 ucycom_dtr(sc, 1); 762 break; 763 764 case TIOCCDTR: 765 ucycom_dtr(sc, 0); 766 break; 767 768 case TIOCGFLAGS: 769 *(int *)data = sc->sc_swflags; 770 break; 771 772 case TIOCSFLAGS: 773 err = kauth_authorize_device_tty(l->l_cred, 774 KAUTH_DEVICE_TTY_PRIVSET, tp); 775 if (err) 776 break; 777 sc->sc_swflags = *(int *)data; 778 break; 779 780 case TIOCMSET: 781 case TIOCMBIS: 782 case TIOCMBIC: 783 tiocm_to_ucycom(sc, cmd, *(int *)data); 784 break; 785 786 case TIOCMGET: 787 *(int *)data = ucycom_to_tiocm(sc); 788 break; 789 790 default: 791 err = EPASSTHROUGH; 792 break; 793 } 794 795 splx(s); 796 797 return (err); 798 } 799 800 int 801 ucycompoll(dev_t dev, int events, struct lwp *l) 802 { 803 struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)]; 804 struct tty *tp = sc->sc_tty; 805 int err; 806 807 DPRINTF(("ucycompoll: sc=%p, tp=%p, events=%d, lwp=%p\n", sc, tp, events, l)); 808 809 if (sc->sc_dying) 810 return (EIO); 811 812 err = ((*tp->t_linesw->l_poll)(tp, events, l)); 813 return (err); 814 } 815 816 Static int 817 ucycom_configure(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg) 818 { 819 uint8_t report[5]; 820 int err; 821 822 switch (baud) { 823 case 600: 824 case 1200: 825 case 2400: 826 case 4800: 827 case 9600: 828 case 19200: 829 case 38400: 830 case 57600: 831 #if 0 832 /* 833 * Stock chips only support standard baud rates in the 600 - 57600 834 * range, but higher rates can be achieved using custom firmware. 835 */ 836 case 115200: 837 case 153600: 838 case 192000: 839 #endif 840 break; 841 default: 842 return (EINVAL); 843 } 844 845 DPRINTF(("ucycom_configure: setting %d baud, %d-%c-%d (%d)\n", baud, 846 5 + (cfg & UCYCOM_DATA_MASK), 847 (cfg & UCYCOM_PARITY_MASK) ? 848 ((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N', 849 (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg)); 850 851 report[0] = baud & 0xff; 852 report[1] = (baud >> 8) & 0xff; 853 report[2] = (baud >> 16) & 0xff; 854 report[3] = (baud >> 24) & 0xff; 855 report[4] = cfg; 856 err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT, 857 report, sc->sc_flen); 858 if (err != 0) { 859 DPRINTF(("%s\n", usbd_errstr(err))); 860 return EIO; 861 } 862 sc->sc_baud = baud; 863 sc->sc_cfg = cfg; 864 865 return 0; 866 } 867 868 Static void 869 ucycom_intr(struct uhidev *addr, void *ibuf, u_int len) 870 { 871 struct ucycom_softc *sc = (struct ucycom_softc *)addr; 872 struct tty *tp = sc->sc_tty; 873 int (*rint)(int , struct tty *) = tp->t_linesw->l_rint; 874 uint8_t *cp = ibuf; 875 int s, n, st, chg; 876 877 /* We understand 8 byte and 32 byte input records */ 878 switch (len) { 879 case 8: 880 n = cp[0] & UCYCOM_LMASK; 881 st = cp[0] & ~UCYCOM_LMASK; 882 cp++; 883 break; 884 885 case 32: 886 st = cp[0]; 887 n = cp[1]; 888 cp += 2; 889 break; 890 891 default: 892 DPRINTFN(3,("ucycom_intr: Unknown input report length\n")); 893 return; 894 } 895 896 #ifdef UCYCOM_DEBUG 897 if (ucycomdebug > 5) { 898 u_int32_t i; 899 900 if (n != 0) { 901 DPRINTF(("ucycom_intr: ibuf[0..%d) =", n)); 902 for (i = 0; i < n; i++) 903 DPRINTF((" %02x", cp[i])); 904 DPRINTF(("\n")); 905 } 906 } 907 #endif 908 s = spltty(); 909 910 /* Give characters to tty layer. */ 911 while (n-- > 0) { 912 DPRINTFN(7,("ucycom_intr: char=0x%02x\n", *cp)); 913 if ((*rint)(*cp++, tp) == -1) { 914 /* XXX what should we do? */ 915 printf("%s: lost a character\n", USBDEVNAME(sc->sc_hdev.sc_dev)); 916 break; 917 } 918 } 919 splx(s); 920 chg = st ^ sc->sc_msr; 921 sc->sc_msr = st; 922 if (ISSET(chg, UCYCOM_DCD)) 923 (*tp->t_linesw->l_modem)(tp, 924 ISSET(sc->sc_msr, UCYCOM_DCD)); 925 926 } 927 928 Static void 929 tiocm_to_ucycom(struct ucycom_softc *sc, u_long how, int ttybits) 930 { 931 u_char combits; 932 u_char before = sc->sc_mcr; 933 934 combits = 0; 935 if (ISSET(ttybits, TIOCM_DTR)) 936 SET(combits, UCYCOM_DTR); 937 if (ISSET(ttybits, TIOCM_RTS)) 938 SET(combits, UCYCOM_RTS); 939 940 switch (how) { 941 case TIOCMBIC: 942 CLR(sc->sc_mcr, combits); 943 break; 944 945 case TIOCMBIS: 946 SET(sc->sc_mcr, combits); 947 break; 948 949 case TIOCMSET: 950 CLR(sc->sc_mcr, UCYCOM_DTR | UCYCOM_RTS); 951 SET(sc->sc_mcr, combits); 952 break; 953 } 954 if (before ^ sc->sc_mcr) { 955 DPRINTF(("tiocm_to_ucycom: something has changed\n")); 956 ucycom_set_status(sc); 957 } 958 } 959 960 Static int 961 ucycom_to_tiocm(struct ucycom_softc *sc) 962 { 963 u_char combits; 964 int ttybits = 0; 965 966 combits = sc->sc_mcr; 967 if (ISSET(combits, UCYCOM_DTR)) 968 SET(ttybits, TIOCM_DTR); 969 if (ISSET(combits, UCYCOM_RTS)) 970 SET(ttybits, TIOCM_RTS); 971 972 combits = sc->sc_msr; 973 if (ISSET(combits, UCYCOM_DCD)) 974 SET(ttybits, TIOCM_CD); 975 if (ISSET(combits, UCYCOM_CTS)) 976 SET(ttybits, TIOCM_CTS); 977 if (ISSET(combits, UCYCOM_DSR)) 978 SET(ttybits, TIOCM_DSR); 979 if (ISSET(combits, UCYCOM_RI)) 980 SET(ttybits, TIOCM_RI); 981 982 return (ttybits); 983 } 984 985 Static void 986 ucycom_dtr(struct ucycom_softc *sc, int set) 987 { 988 uint8_t old; 989 990 old = sc->sc_mcr; 991 if (set) 992 SET(sc->sc_mcr, UCYCOM_DTR); 993 else 994 CLR(sc->sc_mcr, UCYCOM_DTR); 995 996 if (old ^ sc->sc_mcr) 997 ucycom_set_status(sc); 998 } 999 1000 #if 0 1001 Static void 1002 ucycom_rts(struct ucycom_softc *sc, int set) 1003 { 1004 uint8_t old; 1005 1006 old = sc->sc_msr; 1007 if (set) 1008 SET(sc->sc_mcr, UCYCOM_RTS); 1009 else 1010 CLR(sc->sc_mcr, UCYCOM_RTS); 1011 1012 if (old ^ sc->sc_mcr) 1013 ucycom_set_status(sc); 1014 } 1015 #endif 1016 1017 Static void 1018 ucycom_set_status(struct ucycom_softc *sc) 1019 { 1020 int err; 1021 1022 if (sc->sc_olen != 8 && sc->sc_olen != 32) { 1023 DPRINTFN(2,("ucycom_set_status: unknown output report size (%zd)\n", 1024 sc->sc_olen)); 1025 return; 1026 } 1027 1028 DPRINTF(("ucycom_set_status: %d\n", sc->sc_mcr)); 1029 1030 memset(sc->sc_obuf, 0, sc->sc_olen); 1031 sc->sc_obuf[0] = sc->sc_mcr; 1032 1033 err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen); 1034 if (err) { 1035 DPRINTF(("ucycom_set_status: err=%d\n", err)); 1036 } 1037 } 1038 1039 #ifdef UCYCOM_DEBUG 1040 Static void 1041 ucycom_get_cfg(struct ucycom_softc *sc) 1042 { 1043 int err, cfg, baud; 1044 uint8_t report[5]; 1045 1046 err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT, 1047 report, sc->sc_flen); 1048 cfg = report[4]; 1049 baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) + report[0]; 1050 DPRINTF(("ucycom_configure: device reports %d baud, %d-%c-%d (%d)\n", baud, 1051 5 + (cfg & UCYCOM_DATA_MASK), 1052 (cfg & UCYCOM_PARITY_MASK) ? 1053 ((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N', 1054 (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg)); 1055 } 1056 #endif 1057 1058 Static void 1059 ucycom_cleanup(struct ucycom_softc *sc) 1060 { 1061 DPRINTF(("ucycom_cleanup: closing uhidev\n")); 1062 1063 if (sc->sc_obuf !=NULL) 1064 free (sc->sc_obuf, M_USBDEV); 1065 uhidev_close(&sc->sc_hdev); 1066 } 1067