1 /* $NetBSD: uchcom.c,v 1.8 2009/09/23 19:07:19 plunky 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 Takuya SHIOZAKI (tshiozak@netbsd.org). 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: uchcom.c,v 1.8 2009/09/23 19:07:19 plunky Exp $"); 34 35 /* 36 * driver for WinChipHead CH341/340, the worst USB-serial chip in the world. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/ioctl.h> 44 #include <sys/conf.h> 45 #include <sys/tty.h> 46 #include <sys/file.h> 47 #include <sys/select.h> 48 #include <sys/proc.h> 49 #include <sys/device.h> 50 #include <sys/poll.h> 51 #include <sys/workqueue.h> 52 53 #include <dev/usb/usb.h> 54 #include <dev/usb/usbcdc.h> 55 56 #include <dev/usb/usbdi.h> 57 #include <dev/usb/usbdi_util.h> 58 #include <dev/usb/usbdevs.h> 59 #include <dev/usb/usb_quirks.h> 60 61 #include <dev/usb/ucomvar.h> 62 63 #ifdef UCHCOM_DEBUG 64 #define DPRINTFN(n, x) if (uchcomdebug > (n)) logprintf x 65 int uchcomdebug = 0; 66 #else 67 #define DPRINTFN(n, x) 68 #endif 69 #define DPRINTF(x) DPRINTFN(0, x) 70 71 #define UCHCOM_IFACE_INDEX 0 72 #define UCHCOM_CONFIG_INDEX 0 73 74 #define UCHCOM_REV_CH340 0x0250 75 #define UCHCOM_INPUT_BUF_SIZE 8 76 77 #define UCHCOM_REQ_GET_VERSION 0x5F 78 #define UCHCOM_REQ_READ_REG 0x95 79 #define UCHCOM_REQ_WRITE_REG 0x9A 80 #define UCHCOM_REQ_RESET 0xA1 81 #define UCHCOM_REQ_SET_DTRRTS 0xA4 82 83 #define UCHCOM_REG_STAT1 0x06 84 #define UCHCOM_REG_STAT2 0x07 85 #define UCHCOM_REG_BPS_PRE 0x12 86 #define UCHCOM_REG_BPS_DIV 0x13 87 #define UCHCOM_REG_BPS_MOD 0x14 88 #define UCHCOM_REG_BPS_PAD 0x0F 89 #define UCHCOM_REG_BREAK1 0x05 90 #define UCHCOM_REG_BREAK2 0x18 91 #define UCHCOM_REG_LCR1 0x18 92 #define UCHCOM_REG_LCR2 0x25 93 94 #define UCHCOM_VER_20 0x20 95 96 #define UCHCOM_BASE_UNKNOWN 0 97 #define UCHCOM_BPS_MOD_BASE 20000000 98 #define UCHCOM_BPS_MOD_BASE_OFS 1100 99 100 #define UCHCOM_DTR_MASK 0x20 101 #define UCHCOM_RTS_MASK 0x40 102 103 #define UCHCOM_BRK1_MASK 0x01 104 #define UCHCOM_BRK2_MASK 0x40 105 106 #define UCHCOM_LCR1_MASK 0xAF 107 #define UCHCOM_LCR2_MASK 0x07 108 #define UCHCOM_LCR1_PARENB 0x80 109 #define UCHCOM_LCR2_PAREVEN 0x07 110 #define UCHCOM_LCR2_PARODD 0x06 111 #define UCHCOM_LCR2_PARMARK 0x05 112 #define UCHCOM_LCR2_PARSPACE 0x04 113 114 #define UCHCOM_INTR_STAT1 0x02 115 #define UCHCOM_INTR_STAT2 0x03 116 #define UCHCOM_INTR_LEAST 4 117 118 #define UCHCOMIBUFSIZE 256 119 #define UCHCOMOBUFSIZE 256 120 121 struct uchcom_softc 122 { 123 USBBASEDEVICE sc_dev; 124 usbd_device_handle sc_udev; 125 device_ptr_t sc_subdev; 126 usbd_interface_handle sc_iface; 127 int sc_dying; 128 /* */ 129 int sc_intr_endpoint; 130 int sc_intr_size; 131 usbd_pipe_handle sc_intr_pipe; 132 u_char *sc_intr_buf; 133 /* */ 134 uint8_t sc_version; 135 int sc_dtr; 136 int sc_rts; 137 u_char sc_lsr; 138 u_char sc_msr; 139 int sc_lcr1; 140 int sc_lcr2; 141 }; 142 143 struct uchcom_endpoints 144 { 145 int ep_bulkin; 146 int ep_bulkout; 147 int ep_intr; 148 int ep_intr_size; 149 }; 150 151 struct uchcom_divider 152 { 153 uint8_t dv_prescaler; 154 uint8_t dv_div; 155 uint8_t dv_mod; 156 }; 157 158 struct uchcom_divider_record 159 { 160 uint32_t dvr_high; 161 uint32_t dvr_low; 162 uint32_t dvr_base_clock; 163 struct uchcom_divider dvr_divider; 164 }; 165 166 static const struct uchcom_divider_record dividers[] = 167 { 168 { 307200, 307200, UCHCOM_BASE_UNKNOWN, { 7, 0xD9, 0 } }, 169 { 921600, 921600, UCHCOM_BASE_UNKNOWN, { 7, 0xF3, 0 } }, 170 { 2999999, 23530, 6000000, { 3, 0, 0 } }, 171 { 23529, 2942, 750000, { 2, 0, 0 } }, 172 { 2941, 368, 93750, { 1, 0, 0 } }, 173 { 367, 1, 11719, { 0, 0, 0 } }, 174 }; 175 #define NUM_DIVIDERS (sizeof (dividers) / sizeof (dividers[0])) 176 177 static const struct usb_devno uchcom_devs[] = { 178 { USB_VENDOR_WINCHIPHEAD, USB_PRODUCT_WINCHIPHEAD_CH341SER }, 179 { USB_VENDOR_WINCHIPHEAD2, USB_PRODUCT_WINCHIPHEAD2_CH341 }, 180 }; 181 #define uchcom_lookup(v, p) usb_lookup(uchcom_devs, v, p) 182 183 Static void uchcom_get_status(void *, int, u_char *, u_char *); 184 Static void uchcom_set(void *, int, int, int); 185 Static int uchcom_param(void *, int, struct termios *); 186 Static int uchcom_open(void *, int); 187 Static void uchcom_close(void *, int); 188 Static void uchcom_intr(usbd_xfer_handle, usbd_private_handle, 189 usbd_status); 190 191 static int set_config(struct uchcom_softc *); 192 static int find_ifaces(struct uchcom_softc *, usbd_interface_handle *); 193 static int find_endpoints(struct uchcom_softc *, 194 struct uchcom_endpoints *); 195 static void close_intr_pipe(struct uchcom_softc *); 196 197 198 struct ucom_methods uchcom_methods = { 199 .ucom_get_status = uchcom_get_status, 200 .ucom_set = uchcom_set, 201 .ucom_param = uchcom_param, 202 .ucom_ioctl = NULL, 203 .ucom_open = uchcom_open, 204 .ucom_close = uchcom_close, 205 .ucom_read = NULL, 206 .ucom_write = NULL, 207 }; 208 209 int uchcom_match(device_t, cfdata_t, void *); 210 void uchcom_attach(device_t, device_t, void *); 211 void uchcom_childdet(device_t, device_t); 212 int uchcom_detach(device_t, int); 213 int uchcom_activate(device_t, enum devact); 214 215 extern struct cfdriver uchcom_cd; 216 217 CFATTACH_DECL2_NEW(uchcom, 218 sizeof(struct uchcom_softc), 219 uchcom_match, 220 uchcom_attach, 221 uchcom_detach, 222 uchcom_activate, 223 NULL, 224 uchcom_childdet); 225 226 /* ---------------------------------------------------------------------- 227 * driver entry points 228 */ 229 230 USB_MATCH(uchcom) 231 { 232 USB_MATCH_START(uchcom, uaa); 233 234 return (uchcom_lookup(uaa->vendor, uaa->product) != NULL ? 235 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 236 } 237 238 USB_ATTACH(uchcom) 239 { 240 USB_ATTACH_START(uchcom, sc, uaa); 241 usbd_device_handle dev = uaa->device; 242 char *devinfop; 243 struct uchcom_endpoints endpoints; 244 struct ucom_attach_args uca; 245 246 aprint_naive("\n"); 247 aprint_normal("\n"); 248 249 devinfop = usbd_devinfo_alloc(dev, 0); 250 aprint_normal_dev(self, "%s\n", devinfop); 251 usbd_devinfo_free(devinfop); 252 253 sc->sc_dev = self; 254 sc->sc_udev = dev; 255 sc->sc_dying = 0; 256 sc->sc_dtr = sc->sc_rts = -1; 257 sc->sc_lsr = sc->sc_msr = 0; 258 259 DPRINTF(("\n\nuchcom attach: sc=%p\n", sc)); 260 261 if (set_config(sc)) 262 goto failed; 263 264 switch (uaa->release) { 265 case UCHCOM_REV_CH340: 266 aprint_normal_dev(self, "CH340 detected\n"); 267 break; 268 default: 269 aprint_normal_dev(self, "CH341 detected\n"); 270 break; 271 } 272 273 if (find_ifaces(sc, &sc->sc_iface)) 274 goto failed; 275 276 if (find_endpoints(sc, &endpoints)) 277 goto failed; 278 279 sc->sc_intr_endpoint = endpoints.ep_intr; 280 sc->sc_intr_size = endpoints.ep_intr_size; 281 282 /* setup ucom layer */ 283 uca.portno = UCOM_UNK_PORTNO; 284 uca.bulkin = endpoints.ep_bulkin; 285 uca.bulkout = endpoints.ep_bulkout; 286 uca.ibufsize = UCHCOMIBUFSIZE; 287 uca.obufsize = UCHCOMOBUFSIZE; 288 uca.ibufsizepad = UCHCOMIBUFSIZE; 289 uca.opkthdrlen = 0; 290 uca.device = dev; 291 uca.iface = sc->sc_iface; 292 uca.methods = &uchcom_methods; 293 uca.arg = sc; 294 uca.info = NULL; 295 296 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 297 USBDEV(sc->sc_dev)); 298 299 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca, 300 ucomprint, ucomsubmatch); 301 302 USB_ATTACH_SUCCESS_RETURN; 303 304 failed: 305 sc->sc_dying = 1; 306 USB_ATTACH_ERROR_RETURN; 307 } 308 309 void 310 uchcom_childdet(device_t self, device_t child) 311 { 312 struct uchcom_softc *sc = device_private(self); 313 314 KASSERT(sc->sc_subdev == child); 315 sc->sc_subdev = NULL; 316 } 317 318 USB_DETACH(uchcom) 319 { 320 USB_DETACH_START(uchcom, sc); 321 int rv = 0; 322 323 DPRINTF(("uchcom_detach: sc=%p flags=%d\n", sc, flags)); 324 325 close_intr_pipe(sc); 326 327 sc->sc_dying = 1; 328 329 if (sc->sc_subdev != NULL) 330 rv = config_detach(sc->sc_subdev, flags); 331 332 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 333 USBDEV(sc->sc_dev)); 334 335 return rv; 336 } 337 338 int 339 uchcom_activate(device_t self, enum devact act) 340 { 341 struct uchcom_softc *sc = device_private(self); 342 int rv = 0; 343 344 switch (act) { 345 case DVACT_ACTIVATE: 346 rv = EOPNOTSUPP; 347 break; 348 case DVACT_DEACTIVATE: 349 close_intr_pipe(sc); 350 sc->sc_dying = 1; 351 if (sc->sc_subdev != NULL) 352 rv = config_deactivate(sc->sc_subdev); 353 break; 354 } 355 return rv; 356 } 357 358 static int 359 set_config(struct uchcom_softc *sc) 360 { 361 usbd_status err; 362 363 err = usbd_set_config_index(sc->sc_udev, UCHCOM_CONFIG_INDEX, 1); 364 if (err) { 365 aprint_error_dev(sc->sc_dev, 366 "failed to set configuration: %s\n", usbd_errstr(err)); 367 return -1; 368 } 369 370 return 0; 371 } 372 373 static int 374 find_ifaces(struct uchcom_softc *sc, usbd_interface_handle *riface) 375 { 376 usbd_status err; 377 378 err = usbd_device2interface_handle(sc->sc_udev, UCHCOM_IFACE_INDEX, 379 riface); 380 if (err) { 381 aprint_error("\n%s: failed to get interface: %s\n", 382 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 383 return -1; 384 } 385 386 return 0; 387 } 388 389 static int 390 find_endpoints(struct uchcom_softc *sc, struct uchcom_endpoints *endpoints) 391 { 392 int i, bin=-1, bout=-1, intr=-1, isize=0; 393 usb_interface_descriptor_t *id; 394 usb_endpoint_descriptor_t *ed; 395 396 id = usbd_get_interface_descriptor(sc->sc_iface); 397 398 for (i = 0; i < id->bNumEndpoints; i++) { 399 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 400 if (ed == NULL) { 401 aprint_error_dev(sc->sc_dev, 402 "no endpoint descriptor for %d\n", i); 403 return -1; 404 } 405 406 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 407 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 408 intr = ed->bEndpointAddress; 409 isize = UGETW(ed->wMaxPacketSize); 410 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 411 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 412 bin = ed->bEndpointAddress; 413 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 414 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 415 bout = ed->bEndpointAddress; 416 } 417 } 418 419 if (intr == -1 || bin == -1 || bout == -1) { 420 if (intr == -1) { 421 aprint_error_dev(sc->sc_dev, 422 "no interrupt end point\n"); 423 } 424 if (bin == -1) { 425 aprint_error_dev(sc->sc_dev, 426 "no data bulk in end point\n"); 427 } 428 if (bout == -1) { 429 aprint_error_dev(sc->sc_dev, 430 "no data bulk out end point\n"); 431 } 432 return -1; 433 } 434 if (isize < UCHCOM_INTR_LEAST) { 435 aprint_error_dev(sc->sc_dev, "intr pipe is too short\n"); 436 return -1; 437 } 438 439 DPRINTF(("%s: bulkin=%d, bulkout=%d, intr=%d, isize=%d\n", 440 USBDEVNAME(sc->sc_dev), bin, bout, intr, isize)); 441 442 endpoints->ep_intr = intr; 443 endpoints->ep_intr_size = isize; 444 endpoints->ep_bulkin = bin; 445 endpoints->ep_bulkout = bout; 446 447 return 0; 448 } 449 450 451 /* ---------------------------------------------------------------------- 452 * low level i/o 453 */ 454 455 static __inline usbd_status 456 generic_control_out(struct uchcom_softc *sc, uint8_t reqno, 457 uint16_t value, uint16_t index) 458 { 459 usb_device_request_t req; 460 461 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 462 req.bRequest = reqno; 463 USETW(req.wValue, value); 464 USETW(req.wIndex, index); 465 USETW(req.wLength, 0); 466 467 return usbd_do_request(sc->sc_udev, &req, 0); 468 } 469 470 static __inline usbd_status 471 generic_control_in(struct uchcom_softc *sc, uint8_t reqno, 472 uint16_t value, uint16_t index, void *buf, int buflen, 473 int *actlen) 474 { 475 usb_device_request_t req; 476 477 req.bmRequestType = UT_READ_VENDOR_DEVICE; 478 req.bRequest = reqno; 479 USETW(req.wValue, value); 480 USETW(req.wIndex, index); 481 USETW(req.wLength, (uint16_t)buflen); 482 483 return usbd_do_request_flags(sc->sc_udev, &req, buf, 484 USBD_SHORT_XFER_OK, actlen, 485 USBD_DEFAULT_TIMEOUT); 486 } 487 488 static __inline usbd_status 489 write_reg(struct uchcom_softc *sc, 490 uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2) 491 { 492 DPRINTF(("uchcom: write reg 0x%02X<-0x%02X, 0x%02X<-0x%02X\n", 493 (unsigned)reg1, (unsigned)val1, 494 (unsigned)reg2, (unsigned)val2)); 495 return generic_control_out( 496 sc, UCHCOM_REQ_WRITE_REG, 497 reg1|((uint16_t)reg2<<8), val1|((uint16_t)val2<<8)); 498 } 499 500 static __inline usbd_status 501 read_reg(struct uchcom_softc *sc, 502 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2) 503 { 504 uint8_t buf[UCHCOM_INPUT_BUF_SIZE]; 505 usbd_status err; 506 int actin; 507 508 err = generic_control_in( 509 sc, UCHCOM_REQ_READ_REG, 510 reg1|((uint16_t)reg2<<8), 0, buf, sizeof buf, &actin); 511 if (err) 512 return err; 513 514 DPRINTF(("uchcom: read reg 0x%02X->0x%02X, 0x%02X->0x%02X\n", 515 (unsigned)reg1, (unsigned)buf[0], 516 (unsigned)reg2, (unsigned)buf[1])); 517 518 if (rval1) *rval1 = buf[0]; 519 if (rval2) *rval2 = buf[1]; 520 521 return USBD_NORMAL_COMPLETION; 522 } 523 524 static __inline usbd_status 525 get_version(struct uchcom_softc *sc, uint8_t *rver) 526 { 527 uint8_t buf[UCHCOM_INPUT_BUF_SIZE]; 528 usbd_status err; 529 int actin; 530 531 err = generic_control_in( 532 sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof buf, &actin); 533 if (err) 534 return err; 535 536 if (rver) *rver = buf[0]; 537 538 return USBD_NORMAL_COMPLETION; 539 } 540 541 static __inline usbd_status 542 get_status(struct uchcom_softc *sc, uint8_t *rval) 543 { 544 return read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL); 545 } 546 547 static __inline usbd_status 548 set_dtrrts_10(struct uchcom_softc *sc, uint8_t val) 549 { 550 return write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val); 551 } 552 553 static __inline usbd_status 554 set_dtrrts_20(struct uchcom_softc *sc, uint8_t val) 555 { 556 return generic_control_out(sc, UCHCOM_REQ_SET_DTRRTS, val, 0); 557 } 558 559 560 /* ---------------------------------------------------------------------- 561 * middle layer 562 */ 563 564 static int 565 update_version(struct uchcom_softc *sc) 566 { 567 usbd_status err; 568 569 err = get_version(sc, &sc->sc_version); 570 if (err) { 571 aprint_error_dev(sc->sc_dev, "cannot get version: %s\n", 572 usbd_errstr(err)); 573 return EIO; 574 } 575 576 return 0; 577 } 578 579 static void 580 convert_status(struct uchcom_softc *sc, uint8_t cur) 581 { 582 sc->sc_dtr = !(cur & UCHCOM_DTR_MASK); 583 sc->sc_rts = !(cur & UCHCOM_RTS_MASK); 584 585 cur = ~cur & 0x0F; 586 sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur); 587 } 588 589 static int 590 update_status(struct uchcom_softc *sc) 591 { 592 usbd_status err; 593 uint8_t cur; 594 595 err = get_status(sc, &cur); 596 if (err) { 597 aprint_error_dev(sc->sc_dev, 598 "cannot update status: %s\n", usbd_errstr(err)); 599 return EIO; 600 } 601 convert_status(sc, cur); 602 603 return 0; 604 } 605 606 607 static int 608 set_dtrrts(struct uchcom_softc *sc, int dtr, int rts) 609 { 610 usbd_status err; 611 uint8_t val = 0; 612 613 if (dtr) val |= UCHCOM_DTR_MASK; 614 if (rts) val |= UCHCOM_RTS_MASK; 615 616 if (sc->sc_version < UCHCOM_VER_20) 617 err = set_dtrrts_10(sc, ~val); 618 else 619 err = set_dtrrts_20(sc, ~val); 620 621 if (err) { 622 aprint_error_dev(sc->sc_dev, "cannot set DTR/RTS: %s\n", 623 usbd_errstr(err)); 624 return EIO; 625 } 626 627 return 0; 628 } 629 630 static int 631 set_break(struct uchcom_softc *sc, int onoff) 632 { 633 usbd_status err; 634 uint8_t brk1, brk2; 635 636 err = read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2); 637 if (err) 638 return EIO; 639 if (onoff) { 640 /* on - clear bits */ 641 brk1 &= ~UCHCOM_BRK1_MASK; 642 brk2 &= ~UCHCOM_BRK2_MASK; 643 } else { 644 /* off - set bits */ 645 brk1 |= UCHCOM_BRK1_MASK; 646 brk2 |= UCHCOM_BRK2_MASK; 647 } 648 err = write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2); 649 if (err) 650 return EIO; 651 652 return 0; 653 } 654 655 static int 656 calc_divider_settings(struct uchcom_divider *dp, uint32_t rate) 657 { 658 int i; 659 const struct uchcom_divider_record *rp; 660 uint32_t div, rem, mod; 661 662 /* find record */ 663 for (i=0; i<NUM_DIVIDERS; i++) { 664 if (dividers[i].dvr_high >= rate && 665 dividers[i].dvr_low <= rate) { 666 rp = ÷rs[i]; 667 goto found; 668 } 669 } 670 return -1; 671 672 found: 673 dp->dv_prescaler = rp->dvr_divider.dv_prescaler; 674 if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN) 675 dp->dv_div = rp->dvr_divider.dv_div; 676 else { 677 div = rp->dvr_base_clock / rate; 678 rem = rp->dvr_base_clock % rate; 679 if (div==0 || div>=0xFF) 680 return -1; 681 if ((rem<<1) >= rate) 682 div += 1; 683 dp->dv_div = (uint8_t)-div; 684 } 685 686 mod = UCHCOM_BPS_MOD_BASE/rate + UCHCOM_BPS_MOD_BASE_OFS; 687 mod = mod + mod/2; 688 689 dp->dv_mod = mod / 0x100; 690 691 return 0; 692 } 693 694 static int 695 set_dte_rate(struct uchcom_softc *sc, uint32_t rate) 696 { 697 usbd_status err; 698 struct uchcom_divider dv; 699 700 if (calc_divider_settings(&dv, rate)) 701 return EINVAL; 702 703 if ((err = write_reg(sc, 704 UCHCOM_REG_BPS_PRE, dv.dv_prescaler, 705 UCHCOM_REG_BPS_DIV, dv.dv_div)) || 706 (err = write_reg(sc, 707 UCHCOM_REG_BPS_MOD, dv.dv_mod, 708 UCHCOM_REG_BPS_PAD, 0))) { 709 aprint_error_dev(sc->sc_dev, "cannot set DTE rate: %s\n", 710 usbd_errstr(err)); 711 return EIO; 712 } 713 714 return 0; 715 } 716 717 static int 718 set_line_control(struct uchcom_softc *sc, tcflag_t cflag) 719 { 720 usbd_status err; 721 uint8_t lcr1val = 0, lcr2val = 0; 722 723 err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val); 724 if (err) { 725 aprint_error_dev(sc->sc_dev, "cannot get LCR: %s\n", 726 usbd_errstr(err)); 727 return EIO; 728 } 729 730 lcr1val &= ~UCHCOM_LCR1_MASK; 731 lcr2val &= ~UCHCOM_LCR2_MASK; 732 733 /* 734 * XXX: it is difficult to handle the line control appropriately: 735 * - CS8, !CSTOPB and any parity mode seems ok, but 736 * - the chip doesn't have the function to calculate parity 737 * in !CS8 mode. 738 * - it is unclear that the chip supports CS5,6 mode. 739 * - it is unclear how to handle stop bits. 740 */ 741 742 switch (ISSET(cflag, CSIZE)) { 743 case CS5: 744 case CS6: 745 case CS7: 746 return EINVAL; 747 case CS8: 748 break; 749 } 750 751 if (ISSET(cflag, PARENB)) { 752 lcr1val |= UCHCOM_LCR1_PARENB; 753 if (ISSET(cflag, PARODD)) 754 lcr2val |= UCHCOM_LCR2_PARODD; 755 else 756 lcr2val |= UCHCOM_LCR2_PAREVEN; 757 } 758 759 err = write_reg(sc, UCHCOM_REG_LCR1, lcr1val, UCHCOM_REG_LCR2, lcr2val); 760 if (err) { 761 aprint_error_dev(sc->sc_dev, "cannot set LCR: %s\n", 762 usbd_errstr(err)); 763 return EIO; 764 } 765 766 return 0; 767 } 768 769 static int 770 clear_chip(struct uchcom_softc *sc) 771 { 772 usbd_status err; 773 774 DPRINTF(("%s: clear\n", USBDEVNAME(sc->sc_dev))); 775 err = generic_control_out(sc, UCHCOM_REQ_RESET, 0, 0); 776 if (err) { 777 aprint_error_dev(sc->sc_dev, "cannot clear: %s\n", 778 usbd_errstr(err)); 779 return EIO; 780 } 781 782 return 0; 783 } 784 785 static int 786 reset_chip(struct uchcom_softc *sc) 787 { 788 usbd_status err; 789 uint8_t lcr1val, lcr2val, pre, div, mod; 790 uint16_t val=0, idx=0; 791 792 err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val); 793 if (err) 794 goto failed; 795 796 err = read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div); 797 if (err) 798 goto failed; 799 800 err = read_reg(sc, UCHCOM_REG_BPS_MOD, &mod, UCHCOM_REG_BPS_PAD, NULL); 801 if (err) 802 goto failed; 803 804 val |= (uint16_t)(lcr1val&0xF0) << 8; 805 val |= 0x01; 806 val |= (uint16_t)(lcr2val&0x0F) << 8; 807 val |= 0x02; 808 idx |= pre & 0x07; 809 val |= 0x04; 810 idx |= (uint16_t)div << 8; 811 val |= 0x08; 812 idx |= mod & 0xF8; 813 val |= 0x10; 814 815 DPRINTF(("%s: reset v=0x%04X, i=0x%04X\n", 816 USBDEVNAME(sc->sc_dev), val, idx)); 817 818 err = generic_control_out(sc, UCHCOM_REQ_RESET, val, idx); 819 if (err) 820 goto failed; 821 822 return 0; 823 824 failed: 825 printf("%s: cannot reset: %s\n", 826 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 827 return EIO; 828 } 829 830 static int 831 setup_comm(struct uchcom_softc *sc) 832 { 833 int ret; 834 835 ret = update_version(sc); 836 if (ret) 837 return ret; 838 839 ret = clear_chip(sc); 840 if (ret) 841 return ret; 842 843 ret = set_dte_rate(sc, TTYDEF_SPEED); 844 if (ret) 845 return ret; 846 847 ret = set_line_control(sc, CS8); 848 if (ret) 849 return ret; 850 851 ret = update_status(sc); 852 if (ret) 853 return ret; 854 855 ret = reset_chip(sc); 856 if (ret) 857 return ret; 858 859 ret = set_dte_rate(sc, TTYDEF_SPEED); /* XXX */ 860 if (ret) 861 return ret; 862 863 sc->sc_dtr = sc->sc_rts = 1; 864 ret = set_dtrrts(sc, sc->sc_dtr, sc->sc_rts); 865 if (ret) 866 return ret; 867 868 return 0; 869 } 870 871 static int 872 setup_intr_pipe(struct uchcom_softc *sc) 873 { 874 usbd_status err; 875 876 if (sc->sc_intr_endpoint != -1 && sc->sc_intr_pipe == NULL) { 877 sc->sc_intr_buf = malloc(sc->sc_intr_size, M_USBDEV, M_WAITOK); 878 err = usbd_open_pipe_intr(sc->sc_iface, 879 sc->sc_intr_endpoint, 880 USBD_SHORT_XFER_OK, 881 &sc->sc_intr_pipe, sc, 882 sc->sc_intr_buf, 883 sc->sc_intr_size, 884 uchcom_intr, USBD_DEFAULT_INTERVAL); 885 if (err) { 886 aprint_error_dev(sc->sc_dev, 887 "cannot open interrupt pipe: %s\n", 888 usbd_errstr(err)); 889 return EIO; 890 } 891 } 892 return 0; 893 } 894 895 static void 896 close_intr_pipe(struct uchcom_softc *sc) 897 { 898 usbd_status err; 899 900 if (sc->sc_dying) 901 return; 902 903 if (sc->sc_intr_pipe != NULL) { 904 err = usbd_abort_pipe(sc->sc_intr_pipe); 905 if (err) 906 aprint_error_dev(sc->sc_dev, 907 "abort interrupt pipe failed: %s\n", 908 usbd_errstr(err)); 909 err = usbd_close_pipe(sc->sc_intr_pipe); 910 if (err) 911 aprint_error_dev(sc->sc_dev, 912 "close interrupt pipe failed: %s\n", 913 usbd_errstr(err)); 914 free(sc->sc_intr_buf, M_USBDEV); 915 sc->sc_intr_pipe = NULL; 916 } 917 } 918 919 920 /* ---------------------------------------------------------------------- 921 * methods for ucom 922 */ 923 void 924 uchcom_get_status(void *arg, int portno, u_char *rlsr, u_char *rmsr) 925 { 926 struct uchcom_softc *sc = arg; 927 928 if (sc->sc_dying) 929 return; 930 931 *rlsr = sc->sc_lsr; 932 *rmsr = sc->sc_msr; 933 } 934 935 void 936 uchcom_set(void *arg, int portno, int reg, int onoff) 937 { 938 struct uchcom_softc *sc = arg; 939 940 if (sc->sc_dying) 941 return; 942 943 switch (reg) { 944 case UCOM_SET_DTR: 945 sc->sc_dtr = !!onoff; 946 set_dtrrts(sc, sc->sc_dtr, sc->sc_rts); 947 break; 948 case UCOM_SET_RTS: 949 sc->sc_rts = !!onoff; 950 set_dtrrts(sc, sc->sc_dtr, sc->sc_rts); 951 break; 952 case UCOM_SET_BREAK: 953 set_break(sc, onoff); 954 break; 955 } 956 } 957 958 int 959 uchcom_param(void *arg, int portno, struct termios *t) 960 { 961 struct uchcom_softc *sc = arg; 962 int ret; 963 964 if (sc->sc_dying) 965 return 0; 966 967 ret = set_line_control(sc, t->c_cflag); 968 if (ret) 969 return ret; 970 971 ret = set_dte_rate(sc, t->c_ospeed); 972 if (ret) 973 return ret; 974 975 return 0; 976 } 977 978 int 979 uchcom_open(void *arg, int portno) 980 { 981 int ret; 982 struct uchcom_softc *sc = arg; 983 984 if (sc->sc_dying) 985 return EIO; 986 987 ret = setup_intr_pipe(sc); 988 if (ret) 989 return ret; 990 991 ret = setup_comm(sc); 992 if (ret) 993 return ret; 994 995 return 0; 996 } 997 998 void 999 uchcom_close(void *arg, int portno) 1000 { 1001 struct uchcom_softc *sc = arg; 1002 1003 if (sc->sc_dying) 1004 return; 1005 1006 close_intr_pipe(sc); 1007 } 1008 1009 1010 /* ---------------------------------------------------------------------- 1011 * callback when the modem status is changed. 1012 */ 1013 void 1014 uchcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, 1015 usbd_status status) 1016 { 1017 struct uchcom_softc *sc = priv; 1018 u_char *buf = sc->sc_intr_buf; 1019 1020 if (sc->sc_dying) 1021 return; 1022 1023 if (status != USBD_NORMAL_COMPLETION) { 1024 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1025 return; 1026 1027 DPRINTF(("%s: abnormal status: %s\n", 1028 USBDEVNAME(sc->sc_dev), usbd_errstr(status))); 1029 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 1030 return; 1031 } 1032 DPRINTF(("%s: intr: 0x%02X 0x%02X 0x%02X 0x%02X " 1033 "0x%02X 0x%02X 0x%02X 0x%02X\n", 1034 USBDEVNAME(sc->sc_dev), 1035 (unsigned)buf[0], (unsigned)buf[1], 1036 (unsigned)buf[2], (unsigned)buf[3], 1037 (unsigned)buf[4], (unsigned)buf[5], 1038 (unsigned)buf[6], (unsigned)buf[7])); 1039 1040 convert_status(sc, buf[UCHCOM_INTR_STAT1]); 1041 ucom_status_change(device_private(sc->sc_subdev)); 1042 } 1043