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