1 /* $NetBSD: uslsa.c,v 1.6 2008/04/05 16:35:35 cegger Exp $ */ 2 3 /* 4 * Copyright (c) 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jonathan A. Kollasch <jakllsch@kollasch.net>. Craig Shelley's Linux 9 * driver provided invaluable documentation. 10 * 11 * This code is derived from ugensa.c, software contributed to 12 * The NetBSD Foundation by Roland C. Dowdeswell <elric@netbsd.org>. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the NetBSD 25 * Foundation, Inc. and its contributors. 26 * 4. Neither the name of The NetBSD Foundation nor the names of its 27 * contributors may be used to endorse or promote products derived 28 * from this software without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 * POSSIBILITY OF SUCH DAMAGE. 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.6 2008/04/05 16:35:35 cegger Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/device.h> 50 #include <sys/conf.h> 51 #include <sys/tty.h> 52 53 #include <dev/usb/usb.h> 54 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbdevs.h> 58 59 #include <dev/usb/ucomvar.h> 60 61 #ifdef DEBUG 62 #define USLSA_DEBUG 63 #endif 64 65 #ifdef USLSA_DEBUG 66 #define DPRINTF(x) if (uslsadebug) printf x 67 #define DPRINTFN(n,x) if (uslsadebug>(n)) printf x 68 int uslsadebug = 0; 69 #else 70 #define DPRINTF(x) 71 #define DPRINTFN(n,x) 72 #endif 73 74 #define USLSA_REQUEST_SET 0x41 75 #define USLSA_REQUEST_GET 0xc1 76 77 #define USLSA_REQ_SET_STATE 0x00 78 79 #define USLSA_REQ_SET_BPS 0x01 80 #define USLSA_REQ_GET_BPS 0x02 81 82 #define USLSA_REQ_SET_DPS 0x03 83 #define USLSA_REQ_GET_DPS 0x04 84 85 #define USLSA_REQ_SET_BREAK 0x05 86 #define USLSA_REQ_GET_BREAK 0x06 87 88 #define USLSA_REQ_SET_FLOW 0x07 89 #define USLSA_REQ_GET_FLOW 0x08 90 91 #define USLSA_REQ_SET_MODEM 0x13 92 #define USLSA_REQ_GET_MODEM 0x14 93 94 #define USLSA_REQ_SET_MISC 0x19 95 #define USLSA_REQ_GET_MISC 0x20 96 97 #define USLSA_STATE_DISABLE 0x0000 98 #define USLSA_STATE_ENABLE 0x0001 99 100 #define USLSA_BPS(b) (3686400/b) 101 102 #define USLSA_DPS_DATA_MASK 0x0f00 103 #define USLSA_DPS_DATA_FIVE 0x0500 104 #define USLSA_DPS_DATA_SIX 0x0600 105 #define USLSA_DPS_DATA_SEVEN 0x0700 106 #define USLSA_DPS_DATA_EIGHT 0x0800 107 #define USLSA_DPS_DATA_NINE 0x0900 108 109 #define USLSA_DPS_PARITY_MASK 0x00f0 110 #define USLSA_DPS_PARITY_SPACE 0x0040 111 #define USLSA_DPS_PARITY_MARK 0x0030 112 #define USLSA_DPS_PARITY_EVEN 0x0020 113 #define USLSA_DPS_PARITY_ODD 0x0010 114 #define USLSA_DPS_PARITY_NONE 0x0000 115 116 #define USLSA_DPS_STOP_MASK 0x000f 117 #define USLSA_DPS_STOP_TWO 0x0002 118 #define USLSA_DPS_STOP_ONE_FIVE 0x0001 119 #define USLSA_DPS_STOP_ONE 0x0000 120 121 #define USLSA_BREAK_DISABLE 0x0001 122 #define USLSA_BREAK_ENABLE 0x0000 123 124 #define USLSA_FLOW_SET_RTS 0x0200 125 #define USLSA_FLOW_SET_DTR 0x0100 126 #define USLSA_FLOW_MSR_MASK 0x00f0 127 #define USLSA_FLOW_MSR_DCD 0x0080 128 #define USLSA_FLOW_MSR_RI 0x0040 129 #define USLSA_FLOW_MSR_DSR 0x0020 130 #define USLSA_FLOW_MSR_CTS 0x0010 131 #define USLSA_FLOW_RTS 0x0002 132 #define USLSA_FLOW_DTR 0x0001 133 134 struct uslsa_softc { 135 USBBASEDEVICE sc_dev; /* base device */ 136 usbd_device_handle sc_udev; /* device */ 137 usbd_interface_handle sc_iface; /* interface */ 138 139 device_t sc_subdev; /* ucom device */ 140 141 u_char sc_dying; /* disconnecting */ 142 143 u_char sc_lsr; /* local status register */ 144 u_char sc_msr; /* uslsa status register */ 145 }; 146 147 static void uslsa_get_status(void *sc, int, u_char *, u_char *); 148 static void uslsa_set(void *, int, int, int); 149 static int uslsa_param(void *, int, struct termios *); 150 static int uslsa_open(void *, int); 151 static void uslsa_close(void *, int); 152 153 static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t); 154 static void uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t); 155 156 struct ucom_methods uslsa_methods = { 157 uslsa_get_status, 158 uslsa_set, 159 uslsa_param, 160 NULL, 161 uslsa_open, 162 uslsa_close, 163 NULL, 164 NULL, 165 }; 166 167 #define USLSA_CONFIG_INDEX 0 168 #define USLSA_IFACE_INDEX 0 169 #define USLSA_BUFSIZE 256 170 171 static const struct usb_devno uslsa_devs[] = { 172 { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER }, 173 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD }, 174 { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B }, 175 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP }, 176 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 }, 177 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREECONT }, 178 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DESKTOPMOBILE }, 179 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_IPLINK1220 }, 180 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP }, 181 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG }, 182 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN }, 183 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU }, 184 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_1 }, 185 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 }, 186 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUNNTO }, 187 { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE }, 188 { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 } 189 }; 190 #define uslsa_lookup(v, p) usb_lookup(uslsa_devs, v, p) 191 192 int uslsa_match(device_t, struct cfdata *, void *); 193 void uslsa_attach(device_t, device_t, void *); 194 void uslsa_childdet(device_t, device_t); 195 int uslsa_detach(device_t, int); 196 int uslsa_activate(device_t, enum devact); 197 extern struct cfdriver uslsa_cd; 198 CFATTACH_DECL2(uslsa, sizeof(struct uslsa_softc), uslsa_match, 199 uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet); 200 201 USB_MATCH(uslsa) 202 { 203 USB_MATCH_START(uslsa, uaa); 204 205 return (uslsa_lookup(uaa->vendor, uaa->product) != NULL ? 206 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 207 } 208 209 USB_ATTACH(uslsa) 210 { 211 USB_ATTACH_START(uslsa, sc, uaa); 212 usbd_device_handle dev = uaa->device; 213 usbd_interface_handle iface; 214 usb_interface_descriptor_t *id; 215 usb_endpoint_descriptor_t *ed; 216 char *devinfop; 217 const char *devname; 218 usbd_status err; 219 struct ucom_attach_args uca; 220 int i; 221 222 devname = USBDEVNAME(sc->sc_dev); 223 224 DPRINTFN(10, ("\nuslsa_attach: sc=%p\n", sc)); 225 226 /* Move the device into the configured state. */ 227 err = usbd_set_config_index(dev, USLSA_CONFIG_INDEX, 1); 228 if (err) { 229 printf("\n%s: failed to set configuration, err=%s\n", 230 devname, usbd_errstr(err)); 231 goto bad; 232 } 233 234 err = usbd_device2interface_handle(dev, USLSA_IFACE_INDEX, &iface); 235 if (err) { 236 printf("\n%s: failed to get interface, err=%s\n", 237 devname, usbd_errstr(err)); 238 goto bad; 239 } 240 241 devinfop = usbd_devinfo_alloc(dev, 0); 242 USB_ATTACH_SETUP; 243 printf("%s: %s\n", devname, devinfop); 244 usbd_devinfo_free(devinfop); 245 246 id = usbd_get_interface_descriptor(iface); 247 248 sc->sc_udev = dev; 249 sc->sc_iface = iface; 250 251 uca.info = "Silicon Labs CP210x"; 252 uca.portno = UCOM_UNK_PORTNO; 253 uca.ibufsize = USLSA_BUFSIZE; 254 uca.obufsize = USLSA_BUFSIZE; 255 uca.ibufsizepad = USLSA_BUFSIZE; 256 uca.opkthdrlen = 0; 257 uca.device = dev; 258 uca.iface = iface; 259 uca.methods = &uslsa_methods; 260 uca.arg = sc; 261 262 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 263 USBDEV(sc->sc_dev)); 264 265 uca.bulkin = uca.bulkout = -1; 266 for (i = 0; i < id->bNumEndpoints; i++) { 267 int addr, dir, attr; 268 269 ed = usbd_interface2endpoint_descriptor(iface, i); 270 if (ed == NULL) { 271 printf("%s: could not read endpoint descriptor" 272 ": %s\n", devname, usbd_errstr(err)); 273 goto bad; 274 } 275 addr = ed->bEndpointAddress; 276 dir = UE_GET_DIR(ed->bEndpointAddress); 277 attr = ed->bmAttributes & UE_XFERTYPE; 278 if (dir == UE_DIR_IN && attr == UE_BULK) 279 uca.bulkin = addr; 280 else if (dir == UE_DIR_OUT && attr == UE_BULK) 281 uca.bulkout = addr; 282 else 283 printf("%s: unexpected endpoint\n", devname); 284 } 285 if (uca.bulkin == -1) { 286 printf("%s: Could not find data bulk in\n", 287 USBDEVNAME(sc->sc_dev)); 288 goto bad; 289 } 290 if (uca.bulkout == -1) { 291 printf("%s: Could not find data bulk out\n", 292 USBDEVNAME(sc->sc_dev)); 293 goto bad; 294 } 295 296 DPRINTF(("uslsa: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout)); 297 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca, 298 ucomprint, ucomsubmatch); 299 300 USB_ATTACH_SUCCESS_RETURN; 301 302 bad: 303 DPRINTF(("uslsa_attach: ATTACH ERROR\n")); 304 sc->sc_dying = 1; 305 USB_ATTACH_ERROR_RETURN; 306 } 307 308 int 309 uslsa_activate(device_t self, enum devact act) 310 { 311 struct uslsa_softc *sc = device_private(self); 312 int rv = 0; 313 314 switch (act) { 315 case DVACT_ACTIVATE: 316 return (EOPNOTSUPP); 317 break; 318 319 case DVACT_DEACTIVATE: 320 sc->sc_dying = 1; 321 if (sc->sc_subdev) 322 rv = config_deactivate(sc->sc_subdev); 323 break; 324 } 325 return (rv); 326 } 327 328 void 329 uslsa_childdet(device_t self, device_t child) 330 { 331 struct uslsa_softc *sc = device_private(self); 332 333 KASSERT(sc->sc_subdev == child); 334 sc->sc_subdev = NULL; 335 } 336 337 USB_DETACH(uslsa) 338 { 339 USB_DETACH_START(uslsa, sc); 340 int rv = 0; 341 342 DPRINTF(("uslsa_detach: sc=%p flags=%d\n", sc, flags)); 343 344 sc->sc_dying = 1; 345 346 if (sc->sc_subdev != NULL) 347 rv = config_detach(sc->sc_subdev, flags); 348 349 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 350 USBDEV(sc->sc_dev)); 351 352 return (rv); 353 } 354 355 static void 356 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 357 { 358 struct uslsa_softc *sc; 359 usb_device_request_t req; 360 usbd_status err; 361 int actlen; 362 uint16_t flowreg; 363 364 sc = vsc; 365 366 DPRINTF(("uslsa_get_status:\n")); 367 368 req.bmRequestType = USLSA_REQUEST_GET; 369 req.bRequest = USLSA_REQ_GET_FLOW; 370 USETW(req.wValue, 0); 371 USETW(req.wIndex, 0); 372 USETW(req.wLength, sizeof(flowreg)); 373 374 err = usbd_do_request_flags(sc->sc_udev, &req, &flowreg, 375 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 376 if (err) 377 printf("%s: uslsa_get_status: %s\n", 378 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 379 380 DPRINTF(("uslsa_get_status: flowreg=0x%x\n", flowreg)); 381 382 sc->sc_msr = (u_char)(USLSA_FLOW_MSR_MASK & flowreg); 383 384 if (lsr != NULL) 385 *lsr = sc->sc_lsr; 386 if (msr != NULL) 387 *msr = sc->sc_msr; 388 } 389 390 static void 391 uslsa_set(void *vsc, int portno, int reg, int onoff) 392 { 393 struct uslsa_softc *sc; 394 395 sc = vsc; 396 397 DPRINTF(("uslsa_set: sc=%p, port=%d reg=%d onoff=%d\n", sc, portno, 398 reg, onoff)); 399 400 switch (reg) { 401 case UCOM_SET_DTR: 402 if (uslsa_request_set(sc, USLSA_REQ_SET_FLOW, 403 (onoff ? (USLSA_FLOW_DTR | USLSA_FLOW_SET_DTR) : 404 USLSA_FLOW_SET_DTR))) 405 printf("%s: uslsa_set_dtr failed\n", 406 USBDEVNAME(sc->sc_dev)); 407 break; 408 case UCOM_SET_RTS: 409 if (uslsa_request_set(sc, USLSA_REQ_SET_FLOW, 410 (onoff ? (USLSA_FLOW_RTS | USLSA_FLOW_SET_RTS) : 411 USLSA_FLOW_SET_RTS))) 412 printf("%s: uslsa_set_rts failed\n", 413 USBDEVNAME(sc->sc_dev)); 414 break; 415 case UCOM_SET_BREAK: 416 if (uslsa_request_set(sc, USLSA_REQ_SET_BREAK, 417 (onoff ? USLSA_BREAK_ENABLE : 418 USLSA_BREAK_DISABLE))) 419 printf("%s: uslsa_set_break failed\n", 420 USBDEVNAME(sc->sc_dev)); 421 break; 422 default: 423 break; 424 } 425 } 426 427 static int 428 uslsa_param(void *vsc, int portno, struct termios * t) 429 { 430 struct uslsa_softc *sc; 431 uint16_t data; 432 433 sc = vsc; 434 435 DPRINTF(("uslsa_param: sc=%p\n", sc)); 436 437 switch (t->c_ospeed) { 438 case B600: 439 case B1200: 440 case B2400: 441 case B4800: 442 case B9600: 443 case B19200: 444 case B38400: 445 case B57600: 446 case B115200: 447 case B230400: 448 case B460800: 449 case B921600: 450 data = USLSA_BPS(t->c_ospeed); 451 break; 452 default: 453 printf("%s: uslsa_param: unsupported data rate, " 454 "forcing default of 115200bps\n", 455 USBDEVNAME(sc->sc_dev)); 456 data = USLSA_BPS(B115200); 457 }; 458 459 if (uslsa_request_set(sc, USLSA_REQ_SET_BPS, data)) 460 printf("%s: uslsa_param: setting data rate failed\n", 461 USBDEVNAME(sc->sc_dev)); 462 463 data = 0; 464 465 if (ISSET(t->c_cflag, CSTOPB)) 466 data |= USLSA_DPS_STOP_TWO; 467 else 468 data |= USLSA_DPS_STOP_ONE; 469 470 if (ISSET(t->c_cflag, PARENB)) { 471 if (ISSET(t->c_cflag, PARODD)) 472 data |= USLSA_DPS_PARITY_ODD; 473 else 474 data |= USLSA_DPS_PARITY_EVEN; 475 } else 476 data |= USLSA_DPS_PARITY_NONE; 477 478 switch (ISSET(t->c_cflag, CSIZE)) { 479 case CS5: 480 data |= USLSA_DPS_DATA_FIVE; 481 break; 482 case CS6: 483 data |= USLSA_DPS_DATA_SIX; 484 break; 485 case CS7: 486 data |= USLSA_DPS_DATA_SEVEN; 487 break; 488 case CS8: 489 data |= USLSA_DPS_DATA_EIGHT; 490 break; 491 } 492 493 DPRINTF(("uslsa_param: setting DPS register to 0x%x\n", data)); 494 if (uslsa_request_set(sc, USLSA_REQ_SET_DPS, data)) 495 printf("%s: setting DPS register failed: invalid argument\n", 496 USBDEVNAME(sc->sc_dev)); 497 498 uslsa_set_flow(sc, t->c_cflag, t->c_iflag); 499 500 return 0; 501 } 502 503 504 static int 505 uslsa_open(void *vsc, int portno) 506 { 507 struct uslsa_softc *sc; 508 509 sc = vsc; 510 511 DPRINTF(("uslsa_open: sc=%p\n", sc)); 512 513 if (sc->sc_dying) 514 return (EIO); 515 516 if (uslsa_request_set(sc, USLSA_REQ_SET_STATE, USLSA_STATE_ENABLE)) 517 return (EIO); 518 519 return 0; 520 } 521 522 void 523 uslsa_close(void *vsc, int portno) 524 { 525 struct uslsa_softc *sc; 526 527 sc = vsc; 528 529 if (sc->sc_dying) 530 return; 531 532 DPRINTF(("uslsa_close: sc=%p\n", sc)); 533 534 if (uslsa_request_set(sc, USLSA_REQ_SET_STATE, 535 USLSA_STATE_DISABLE)) 536 printf("%s: disable-on-close failed\n", 537 USBDEVNAME(sc->sc_dev)); 538 } 539 540 /* 541 * uslsa_request_set(), wrapper for doing sets with usbd_do_request() 542 */ 543 static int 544 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value) 545 { 546 usb_device_request_t req; 547 usbd_status err; 548 549 req.bmRequestType = USLSA_REQUEST_SET; 550 req.bRequest = request; 551 USETW(req.wValue, value); 552 USETW(req.wIndex, 0); 553 USETW(req.wLength, 0); 554 555 err = usbd_do_request(sc->sc_udev, &req, 0); 556 if (err) 557 printf("%s: uslsa_request: %s\n", 558 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 559 return err; 560 } 561 562 /* 563 * uslsa_set_flow() does some magic to set up hardware flow control 564 */ 565 static void 566 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag) 567 { 568 uint8_t mysterydata[16]; 569 usb_device_request_t req; 570 usbd_status err; 571 572 DPRINTF(("uslsa_set_flow: cflag = 0x%x, iflag = 0x%x\n", 573 cflag, iflag)); 574 575 req.bmRequestType = USLSA_REQUEST_GET; 576 req.bRequest = USLSA_REQ_GET_MODEM; 577 USETW(req.wValue, 0); 578 USETW(req.wIndex, 0); 579 USETW(req.wLength, 16); 580 581 err = usbd_do_request(sc->sc_udev, &req, mysterydata); 582 if (err) 583 printf("%s: uslsa_set_flow: %s\n", 584 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 585 586 if (ISSET(cflag, CRTSCTS)) { 587 mysterydata[0] &= ~0x7b; 588 mysterydata[0] |= 0x09; 589 mysterydata[4] = 0x80; 590 } else { 591 mysterydata[0] &= ~0x7b; 592 mysterydata[0] |= 0x01; 593 mysterydata[4] = 0x40; 594 } 595 596 req.bmRequestType = USLSA_REQUEST_SET; 597 req.bRequest = USLSA_REQ_SET_MODEM; 598 USETW(req.wValue, 0); 599 USETW(req.wIndex, 0); 600 USETW(req.wLength, 16); 601 602 err = usbd_do_request(sc->sc_udev, &req, mysterydata); 603 if (err) 604 printf("%s: uslsa_set_flow: %s\n", 605 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 606 } 607