1 /* $OpenBSD: ucycom.c,v 1.34 2016/01/09 04:14:42 jcs Exp $ */ 2 /* $NetBSD: ucycom.c,v 1.3 2005/08/05 07:27:47 skrll Exp $ */ 3 4 /* 5 * Copyright (c) 2005 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Nick Hudson 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /* 33 * This code is based on the ucom driver. 34 */ 35 36 /* 37 * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to 38 * RS232 bridges. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/device.h> 47 #include <sys/tty.h> 48 #include <sys/file.h> 49 50 #include <dev/usb/usb.h> 51 #include <dev/usb/usbhid.h> 52 53 #include <dev/usb/usbdi.h> 54 #include <dev/usb/usbdi_util.h> 55 #include <dev/usb/usbdevs.h> 56 #include <dev/usb/uhidev.h> 57 58 #include <dev/usb/ucomvar.h> 59 60 #ifdef UCYCOM_DEBUG 61 #define DPRINTF(x) if (ucycomdebug) printf x 62 #define DPRINTFN(n, x) if (ucycomdebug > (n)) printf x 63 int ucycomdebug = 200; 64 #else 65 #define DPRINTF(x) 66 #define DPRINTFN(n,x) 67 #endif 68 69 /* Configuration Byte */ 70 #define UCYCOM_RESET 0x80 71 #define UCYCOM_PARITY_TYPE_MASK 0x20 72 #define UCYCOM_PARITY_ODD 0x20 73 #define UCYCOM_PARITY_EVEN 0x00 74 #define UCYCOM_PARITY_MASK 0x10 75 #define UCYCOM_PARITY_ON 0x10 76 #define UCYCOM_PARITY_OFF 0x00 77 #define UCYCOM_STOP_MASK 0x08 78 #define UCYCOM_STOP_BITS_2 0x08 79 #define UCYCOM_STOP_BITS_1 0x00 80 #define UCYCOM_DATA_MASK 0x03 81 #define UCYCOM_DATA_BITS_8 0x03 82 #define UCYCOM_DATA_BITS_7 0x02 83 #define UCYCOM_DATA_BITS_6 0x01 84 #define UCYCOM_DATA_BITS_5 0x00 85 86 /* Modem (Input) status byte */ 87 #define UCYCOM_RI 0x80 88 #define UCYCOM_DCD 0x40 89 #define UCYCOM_DSR 0x20 90 #define UCYCOM_CTS 0x10 91 #define UCYCOM_ERROR 0x08 92 #define UCYCOM_LMASK 0x07 93 94 /* Modem (Output) control byte */ 95 #define UCYCOM_DTR 0x20 96 #define UCYCOM_RTS 0x10 97 #define UCYCOM_ORESET 0x08 98 99 struct ucycom_softc { 100 struct uhidev sc_hdev; 101 struct usbd_device *sc_udev; 102 103 /* uhidev parameters */ 104 size_t sc_flen; /* feature report length */ 105 size_t sc_ilen; /* input report length */ 106 size_t sc_olen; /* output report length */ 107 108 uint8_t *sc_obuf; 109 110 uint8_t *sc_ibuf; 111 uint32_t sc_icnt; 112 113 /* settings */ 114 uint32_t sc_baud; 115 uint8_t sc_cfg; /* Data format */ 116 uint8_t sc_mcr; /* Modem control */ 117 uint8_t sc_msr; /* Modem status */ 118 uint8_t sc_newmsr; /* from HID intr */ 119 int sc_swflags; 120 121 struct device *sc_subdev; 122 }; 123 124 /* Callback routines */ 125 void ucycom_set(void *, int, int, int); 126 int ucycom_param(void *, int, struct termios *); 127 void ucycom_get_status(void *, int, u_char *, u_char *); 128 int ucycom_open(void *, int); 129 void ucycom_close(void *, int); 130 void ucycom_write(void *, int, u_char *, u_char *, u_int32_t *); 131 void ucycom_read(void *, int, u_char **, u_int32_t *); 132 133 struct ucom_methods ucycom_methods = { 134 NULL, /* ucycom_get_status, */ 135 ucycom_set, 136 ucycom_param, 137 NULL, 138 ucycom_open, 139 ucycom_close, 140 ucycom_read, 141 ucycom_write, 142 }; 143 144 void ucycom_intr(struct uhidev *, void *, u_int); 145 146 const struct usb_devno ucycom_devs[] = { 147 { USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 }, 148 { USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMUSB }, 149 { USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMLT20 }, 150 }; 151 152 int ucycom_match(struct device *, void *, void *); 153 void ucycom_attach(struct device *, struct device *, void *); 154 int ucycom_detach(struct device *, int); 155 156 struct cfdriver ucycom_cd = { 157 NULL, "ucycom", DV_DULL 158 }; 159 160 const struct cfattach ucycom_ca = { 161 sizeof(struct ucycom_softc), ucycom_match, ucycom_attach, ucycom_detach 162 }; 163 164 int 165 ucycom_match(struct device *parent, void *match, void *aux) 166 { 167 struct uhidev_attach_arg *uha = aux; 168 169 if (uha->reportid == UHIDEV_CLAIM_ALLREPORTID) 170 return (UMATCH_NONE); 171 172 return (usb_lookup(ucycom_devs, uha->uaa->vendor, uha->uaa->product) != NULL ? 173 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 174 } 175 176 void 177 ucycom_attach(struct device *parent, struct device *self, void *aux) 178 { 179 struct ucycom_softc *sc = (struct ucycom_softc *)self; 180 struct usb_attach_arg *uaa = aux; 181 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa; 182 struct usbd_device *dev = uha->parent->sc_udev; 183 struct ucom_attach_args uca; 184 int size, repid, err; 185 void *desc; 186 187 sc->sc_hdev.sc_intr = ucycom_intr; 188 sc->sc_hdev.sc_parent = uha->parent; 189 sc->sc_hdev.sc_report_id = uha->reportid; 190 191 uhidev_get_report_desc(uha->parent, &desc, &size); 192 repid = uha->reportid; 193 sc->sc_ilen = hid_report_size(desc, size, hid_input, repid); 194 sc->sc_olen = hid_report_size(desc, size, hid_output, repid); 195 sc->sc_flen = hid_report_size(desc, size, hid_feature, repid); 196 197 DPRINTF(("ucycom_open: olen %d ilen %d flen %d\n", sc->sc_ilen, 198 sc->sc_olen, sc->sc_flen)); 199 200 printf("\n"); 201 202 sc->sc_udev = dev; 203 204 sc->sc_msr = sc->sc_mcr = 0; 205 206 err = uhidev_open(&sc->sc_hdev); 207 if (err) { 208 DPRINTF(("ucycom_open: uhidev_open %d\n", err)); 209 return; 210 } 211 212 DPRINTF(("ucycom attach: sc %p opipe %p ipipe %p report_id %d\n", 213 sc, sc->sc_hdev.sc_parent->sc_opipe, sc->sc_hdev.sc_parent->sc_ipipe, 214 uha->reportid)); 215 216 /* bulkin, bulkout set above */ 217 bzero(&uca, sizeof uca); 218 uca.bulkin = uca.bulkout = -1; 219 uca.uhidev = sc->sc_hdev.sc_parent; 220 uca.ibufsize = sc->sc_ilen - 1; 221 uca.obufsize = sc->sc_olen - 1; 222 uca.ibufsizepad = 1; 223 uca.opkthdrlen = 0; 224 uca.device = uaa->device; 225 uca.iface = uaa->iface; 226 uca.methods = &ucycom_methods; 227 uca.arg = sc; 228 uca.info = NULL; 229 230 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 231 DPRINTF(("ucycom_attach: complete %p\n", sc->sc_subdev)); 232 } 233 234 void 235 ucycom_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 236 { 237 struct ucycom_softc *sc = addr; 238 239 DPRINTF(("ucycom_get_status:\n")); 240 241 #if 0 242 if (lsr != NULL) 243 *lsr = sc->sc_lsr; 244 #endif 245 if (msr != NULL) 246 *msr = sc->sc_msr; 247 } 248 249 int 250 ucycom_open(void *addr, int portno) 251 { 252 struct ucycom_softc *sc = addr; 253 struct termios t; 254 int err; 255 256 DPRINTF(("ucycom_open: complete\n")); 257 258 if (usbd_is_dying(sc->sc_udev)) 259 return (EIO); 260 261 /* Allocate an output report buffer */ 262 sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK | M_ZERO); 263 264 /* Allocate an input report buffer */ 265 sc->sc_ibuf = malloc(sc->sc_ilen, M_USBDEV, M_WAITOK); 266 267 DPRINTF(("ucycom_open: sc->sc_ibuf=%p sc->sc_obuf=%p \n", 268 sc->sc_ibuf, sc->sc_obuf)); 269 270 t.c_ospeed = 9600; 271 t.c_cflag = CSTOPB | CS8; 272 (void)ucycom_param(sc, portno, &t); 273 274 sc->sc_mcr = UCYCOM_DTR | UCYCOM_RTS; 275 sc->sc_obuf[0] = sc->sc_mcr; 276 err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen); 277 if (err) { 278 DPRINTF(("ucycom_open: set RTS err=%d\n", err)); 279 return (EIO); 280 } 281 282 return (0); 283 } 284 285 void 286 ucycom_close(void *addr, int portno) 287 { 288 struct ucycom_softc *sc = addr; 289 int s; 290 291 if (usbd_is_dying(sc->sc_udev)) 292 return; 293 294 s = splusb(); 295 if (sc->sc_obuf != NULL) { 296 free(sc->sc_obuf, M_USBDEV, 0); 297 sc->sc_obuf = NULL; 298 } 299 if (sc->sc_ibuf != NULL) { 300 free(sc->sc_ibuf, M_USBDEV, 0); 301 sc->sc_ibuf = NULL; 302 } 303 splx(s); 304 } 305 306 void 307 ucycom_read(void *addr, int portno, u_char **ptr, u_int32_t *count) 308 { 309 struct ucycom_softc *sc = addr; 310 311 if (sc->sc_newmsr ^ sc->sc_msr) { 312 DPRINTF(("ucycom_read: msr %d new %d\n", 313 sc->sc_msr, sc->sc_newmsr)); 314 sc->sc_msr = sc->sc_newmsr; 315 ucom_status_change((struct ucom_softc *)sc->sc_subdev); 316 } 317 318 DPRINTF(("ucycom_read: buf %p chars %d\n", sc->sc_ibuf, sc->sc_icnt)); 319 *ptr = sc->sc_ibuf; 320 *count = sc->sc_icnt; 321 } 322 323 void 324 ucycom_write(void *addr, int portno, u_char *to, u_char *data, u_int32_t *cnt) 325 { 326 struct ucycom_softc *sc = addr; 327 u_int32_t len; 328 #ifdef UCYCOM_DEBUG 329 u_int32_t want = *cnt; 330 #endif 331 332 /* 333 * The 8 byte output report uses byte 0 for control and byte 334 * count. 335 * 336 * The 32 byte output report uses byte 0 for control. Byte 1 337 * is used for byte count. 338 */ 339 len = sc->sc_olen; 340 memset(to, 0, len); 341 switch (sc->sc_olen) { 342 case 8: 343 to[0] = *cnt | sc->sc_mcr; 344 memcpy(&to[1], data, *cnt); 345 DPRINTF(("ucycomstart(8): to[0] = %d | %d = %d\n", 346 *cnt, sc->sc_mcr, to[0])); 347 break; 348 349 case 32: 350 to[0] = sc->sc_mcr; 351 to[1] = *cnt; 352 memcpy(&to[2], data, *cnt); 353 DPRINTF(("ucycomstart(32): to[0] = %d\nto[1] = %d\n", 354 to[0], to[1])); 355 break; 356 } 357 358 #ifdef UCYCOM_DEBUG 359 if (ucycomdebug > 5) { 360 int i; 361 362 if (len != 0) { 363 DPRINTF(("ucycomstart: to[0..%d) =", len-1)); 364 for (i = 0; i < len; i++) 365 DPRINTF((" %02x", to[i])); 366 DPRINTF(("\n")); 367 } 368 } 369 #endif 370 *cnt = len; 371 372 DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", want, len)); 373 } 374 375 int 376 ucycom_param(void *addr, int portno, struct termios *t) 377 { 378 struct ucycom_softc *sc = addr; 379 uint8_t report[5]; 380 uint32_t baud = 0; 381 uint8_t cfg; 382 383 if (usbd_is_dying(sc->sc_udev)) 384 return (EIO); 385 386 switch (t->c_ospeed) { 387 case 600: 388 case 1200: 389 case 2400: 390 case 4800: 391 case 9600: 392 case 19200: 393 case 38400: 394 case 57600: 395 #if 0 396 /* 397 * Stock chips only support standard baud rates in the 600 - 57600 398 * range, but higher rates can be achieved using custom firmware. 399 */ 400 case 115200: 401 case 153600: 402 case 192000: 403 #endif 404 baud = t->c_ospeed; 405 break; 406 default: 407 return (EINVAL); 408 } 409 410 if (t->c_cflag & CIGNORE) { 411 cfg = sc->sc_cfg; 412 } else { 413 cfg = 0; 414 switch (t->c_cflag & CSIZE) { 415 case CS8: 416 cfg |= UCYCOM_DATA_BITS_8; 417 break; 418 case CS7: 419 cfg |= UCYCOM_DATA_BITS_7; 420 break; 421 case CS6: 422 cfg |= UCYCOM_DATA_BITS_6; 423 break; 424 case CS5: 425 cfg |= UCYCOM_DATA_BITS_5; 426 break; 427 default: 428 return (EINVAL); 429 } 430 cfg |= ISSET(t->c_cflag, CSTOPB) ? 431 UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1; 432 cfg |= ISSET(t->c_cflag, PARENB) ? 433 UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF; 434 cfg |= ISSET(t->c_cflag, PARODD) ? 435 UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN; 436 } 437 438 DPRINTF(("ucycom_param: setting %d baud, %d-%c-%d (%d)\n", baud, 439 5 + (cfg & UCYCOM_DATA_MASK), 440 (cfg & UCYCOM_PARITY_MASK) ? 441 ((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N', 442 (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg)); 443 444 report[0] = baud & 0xff; 445 report[1] = (baud >> 8) & 0xff; 446 report[2] = (baud >> 16) & 0xff; 447 report[3] = (baud >> 24) & 0xff; 448 report[4] = cfg; 449 if (uhidev_set_report(sc->sc_hdev.sc_parent, UHID_FEATURE_REPORT, 450 sc->sc_hdev.sc_report_id, report, sc->sc_flen) != sc->sc_flen) 451 return EIO; 452 sc->sc_baud = baud; 453 return (0); 454 } 455 456 void 457 ucycom_intr(struct uhidev *addr, void *ibuf, u_int len) 458 { 459 extern void ucomreadcb(struct usbd_xfer *, void *, usbd_status); 460 struct ucycom_softc *sc = (struct ucycom_softc *)addr; 461 uint8_t *cp = ibuf; 462 int n, st, s; 463 464 /* not accepting data anymore.. */ 465 if (sc->sc_ibuf == NULL) 466 return; 467 468 /* We understand 8 byte and 32 byte input records */ 469 switch (len) { 470 case 8: 471 n = cp[0] & UCYCOM_LMASK; 472 st = cp[0] & ~UCYCOM_LMASK; 473 cp++; 474 break; 475 476 case 32: 477 st = cp[0]; 478 n = cp[1]; 479 cp += 2; 480 break; 481 482 default: 483 DPRINTFN(3,("ucycom_intr: Unknown input report length\n")); 484 return; 485 } 486 487 #ifdef UCYCOM_DEBUG 488 if (ucycomdebug > 5) { 489 u_int32_t i; 490 491 if (n != 0) { 492 DPRINTF(("ucycom_intr: ibuf[0..%d) =", n)); 493 for (i = 0; i < n; i++) 494 DPRINTF((" %02x", cp[i])); 495 DPRINTF(("\n")); 496 } 497 } 498 #endif 499 500 if (n > 0 || st != sc->sc_msr) { 501 s = spltty(); 502 sc->sc_newmsr = st; 503 bcopy(cp, sc->sc_ibuf, n); 504 sc->sc_icnt = n; 505 ucomreadcb(addr->sc_parent->sc_ixfer, sc->sc_subdev, 506 USBD_NORMAL_COMPLETION); 507 splx(s); 508 } 509 } 510 511 void 512 ucycom_set(void *addr, int portno, int reg, int onoff) 513 { 514 struct ucycom_softc *sc = addr; 515 int err; 516 517 switch (reg) { 518 case UCOM_SET_DTR: 519 if (onoff) 520 SET(sc->sc_mcr, UCYCOM_DTR); 521 else 522 CLR(sc->sc_mcr, UCYCOM_DTR); 523 break; 524 case UCOM_SET_RTS: 525 if (onoff) 526 SET(sc->sc_mcr, UCYCOM_RTS); 527 else 528 CLR(sc->sc_mcr, UCYCOM_RTS); 529 break; 530 case UCOM_SET_BREAK: 531 break; 532 } 533 534 memset(sc->sc_obuf, 0, sc->sc_olen); 535 sc->sc_obuf[0] = sc->sc_mcr; 536 537 err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen); 538 if (err) 539 DPRINTF(("ucycom_set_status: err=%d\n", err)); 540 } 541 542 int 543 ucycom_detach(struct device *self, int flags) 544 { 545 struct ucycom_softc *sc = (struct ucycom_softc *)self; 546 547 DPRINTF(("ucycom_detach: sc=%p flags=%d\n", sc, flags)); 548 if (sc->sc_subdev != NULL) { 549 config_detach(sc->sc_subdev, flags); 550 sc->sc_subdev = NULL; 551 } 552 553 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) 554 uhidev_close(&sc->sc_hdev); 555 556 return (0); 557 } 558