uchcom.c revision 184736
1/* $NetBSD: uchcom.c,v 1.1 2007/09/03 17:57:37 tshiozak Exp $ */ 2 3/*- 4 * Copyright (c) 2007, Takanori Watanabe 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29/* 30 * Copyright (c) 2007 The NetBSD Foundation, Inc. 31 * All rights reserved. 32 * 33 * This code is derived from software contributed to The NetBSD Foundation 34 * by Takuya SHIOZAKI (tshiozak@netbsd.org). 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by the NetBSD 47 * Foundation, Inc. and its contributors. 48 * 4. Neither the name of The NetBSD Foundation nor the names of its 49 * contributors may be used to endorse or promote products derived 50 * from this software without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 53 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 62 * POSSIBILITY OF SUCH DAMAGE. 63 */ 64 65#include <sys/cdefs.h> 66__FBSDID("$FreeBSD: head/sys/dev/usb2/serial/uchcom2.c 184736 2008-11-06 17:26:12Z imp $"); 67 68/* 69 * driver for WinChipHead CH341/340, the worst USB-serial chip in the world. 70 */ 71 72#include <dev/usb2/include/usb2_devid.h> 73#include <dev/usb2/include/usb2_standard.h> 74#include <dev/usb2/include/usb2_mfunc.h> 75#include <dev/usb2/include/usb2_error.h> 76#include <dev/usb2/include/usb2_cdc.h> 77#include <dev/usb2/include/usb2_ioctl.h> 78 79#define USB_DEBUG_VAR uchcom_debug 80 81#include <dev/usb2/core/usb2_core.h> 82#include <dev/usb2/core/usb2_debug.h> 83#include <dev/usb2/core/usb2_process.h> 84#include <dev/usb2/core/usb2_config_td.h> 85#include <dev/usb2/core/usb2_request.h> 86#include <dev/usb2/core/usb2_lookup.h> 87#include <dev/usb2/core/usb2_util.h> 88#include <dev/usb2/core/usb2_busdma.h> 89 90#include <dev/usb2/serial/usb2_serial.h> 91 92#if USB_DEBUG 93static int uchcom_debug = 0; 94 95SYSCTL_NODE(_hw_usb2, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom"); 96SYSCTL_INT(_hw_usb2_uchcom, OID_AUTO, debug, CTLFLAG_RW, 97 &uchcom_debug, 0, "uchcom debug level"); 98#endif 99 100#define UCHCOM_IFACE_INDEX 0 101#define UCHCOM_CONFIG_INDEX 0 102 103#define UCHCOM_REV_CH340 0x0250 104#define UCHCOM_INPUT_BUF_SIZE 8 105 106#define UCHCOM_REQ_GET_VERSION 0x5F 107#define UCHCOM_REQ_READ_REG 0x95 108#define UCHCOM_REQ_WRITE_REG 0x9A 109#define UCHCOM_REQ_RESET 0xA1 110#define UCHCOM_REQ_SET_DTRRTS 0xA4 111 112#define UCHCOM_REG_STAT1 0x06 113#define UCHCOM_REG_STAT2 0x07 114#define UCHCOM_REG_BPS_PRE 0x12 115#define UCHCOM_REG_BPS_DIV 0x13 116#define UCHCOM_REG_BPS_MOD 0x14 117#define UCHCOM_REG_BPS_PAD 0x0F 118#define UCHCOM_REG_BREAK1 0x05 119#define UCHCOM_REG_BREAK2 0x18 120#define UCHCOM_REG_LCR1 0x18 121#define UCHCOM_REG_LCR2 0x25 122 123#define UCHCOM_VER_20 0x20 124 125#define UCHCOM_BASE_UNKNOWN 0 126#define UCHCOM_BPS_MOD_BASE 20000000 127#define UCHCOM_BPS_MOD_BASE_OFS 1100 128 129#define UCHCOM_DTR_MASK 0x20 130#define UCHCOM_RTS_MASK 0x40 131 132#define UCHCOM_BRK1_MASK 0x01 133#define UCHCOM_BRK2_MASK 0x40 134 135#define UCHCOM_LCR1_MASK 0xAF 136#define UCHCOM_LCR2_MASK 0x07 137#define UCHCOM_LCR1_PARENB 0x80 138#define UCHCOM_LCR2_PAREVEN 0x07 139#define UCHCOM_LCR2_PARODD 0x06 140#define UCHCOM_LCR2_PARMARK 0x05 141#define UCHCOM_LCR2_PARSPACE 0x04 142 143#define UCHCOM_INTR_STAT1 0x02 144#define UCHCOM_INTR_STAT2 0x03 145#define UCHCOM_INTR_LEAST 4 146 147#define UCHCOM_BULK_BUF_SIZE 1024 /* bytes */ 148#define UCHCOM_N_TRANSFER 6 /* units */ 149 150struct uchcom_softc { 151 struct usb2_com_super_softc sc_super_ucom; 152 struct usb2_com_softc sc_ucom; 153 154 struct usb2_xfer *sc_xfer[UCHCOM_N_TRANSFER]; 155 struct usb2_device *sc_udev; 156 157 uint8_t sc_dtr; /* local copy */ 158 uint8_t sc_rts; /* local copy */ 159 uint8_t sc_version; 160 uint8_t sc_msr; 161 uint8_t sc_lsr; /* local status register */ 162 uint8_t sc_flag; 163#define UCHCOM_FLAG_INTR_STALL 0x01 164#define UCHCOM_FLAG_READ_STALL 0x02 165#define UCHCOM_FLAG_WRITE_STALL 0x04 166}; 167 168struct uchcom_divider { 169 uint8_t dv_prescaler; 170 uint8_t dv_div; 171 uint8_t dv_mod; 172}; 173 174struct uchcom_divider_record { 175 uint32_t dvr_high; 176 uint32_t dvr_low; 177 uint32_t dvr_base_clock; 178 struct uchcom_divider dvr_divider; 179}; 180 181static const struct uchcom_divider_record dividers[] = 182{ 183 {307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}}, 184 {921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}}, 185 {2999999, 23530, 6000000, {3, 0, 0}}, 186 {23529, 2942, 750000, {2, 0, 0}}, 187 {2941, 368, 93750, {1, 0, 0}}, 188 {367, 1, 11719, {0, 0, 0}}, 189}; 190 191#define NUM_DIVIDERS (sizeof (dividers) / sizeof (dividers[0])) 192 193static const struct usb2_device_id uchcom_devs[] = { 194 {USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)}, 195}; 196 197/* protypes */ 198 199static int uchcom_ioctl(struct usb2_com_softc *ucom, uint32_t cmd, caddr_t data, int flag, struct thread *td); 200static int uchcom_pre_param(struct usb2_com_softc *ucom, struct termios *t); 201static void uchcom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr); 202static void uchcom_cfg_param(struct usb2_com_softc *ucom, struct termios *t); 203static void uchcom_cfg_set_break(struct usb2_com_softc *sc, uint8_t onoff); 204static void uchcom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff); 205static void uchcom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff); 206static void uchcom_start_read(struct usb2_com_softc *ucom); 207static void uchcom_start_write(struct usb2_com_softc *ucom); 208static void uchcom_stop_read(struct usb2_com_softc *ucom); 209static void uchcom_stop_write(struct usb2_com_softc *ucom); 210 211static void uchcom_update_version(struct uchcom_softc *sc); 212static void uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur); 213static void uchcom_update_status(struct uchcom_softc *sc); 214static void uchcom_set_dtrrts(struct uchcom_softc *sc); 215static int uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate); 216static void uchcom_set_dte_rate(struct uchcom_softc *sc, uint32_t rate); 217static void uchcom_set_line_control(struct uchcom_softc *sc, tcflag_t cflag); 218static void uchcom_clear_chip(struct uchcom_softc *sc); 219static void uchcom_reset_chip(struct uchcom_softc *sc); 220 221static device_probe_t uchcom_probe; 222static device_attach_t uchcom_attach; 223static device_detach_t uchcom_detach; 224 225static usb2_callback_t uchcom_intr_callback; 226static usb2_callback_t uchcom_intr_clear_stall_callback; 227static usb2_callback_t uchcom_write_callback; 228static usb2_callback_t uchcom_write_clear_stall_callback; 229static usb2_callback_t uchcom_read_callback; 230static usb2_callback_t uchcom_read_clear_stall_callback; 231 232static const struct usb2_config uchcom_config_data[UCHCOM_N_TRANSFER] = { 233 234 [0] = { 235 .type = UE_BULK, 236 .endpoint = UE_ADDR_ANY, 237 .direction = UE_DIR_OUT, 238 .mh.bufsize = UCHCOM_BULK_BUF_SIZE, 239 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 240 .mh.callback = &uchcom_write_callback, 241 }, 242 243 [1] = { 244 .type = UE_BULK, 245 .endpoint = UE_ADDR_ANY, 246 .direction = UE_DIR_IN, 247 .mh.bufsize = UCHCOM_BULK_BUF_SIZE, 248 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 249 .mh.callback = &uchcom_read_callback, 250 }, 251 252 [2] = { 253 .type = UE_CONTROL, 254 .endpoint = 0x00, /* Control pipe */ 255 .direction = UE_DIR_ANY, 256 .mh.bufsize = sizeof(struct usb2_device_request), 257 .mh.callback = &uchcom_write_clear_stall_callback, 258 .mh.timeout = 1000, /* 1 second */ 259 .mh.interval = 50, /* 50ms */ 260 }, 261 262 [3] = { 263 .type = UE_CONTROL, 264 .endpoint = 0x00, /* Control pipe */ 265 .direction = UE_DIR_ANY, 266 .mh.bufsize = sizeof(struct usb2_device_request), 267 .mh.callback = &uchcom_read_clear_stall_callback, 268 .mh.timeout = 1000, /* 1 second */ 269 .mh.interval = 50, /* 50ms */ 270 }, 271 272 [4] = { 273 .type = UE_INTERRUPT, 274 .endpoint = UE_ADDR_ANY, 275 .direction = UE_DIR_IN, 276 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 277 .mh.bufsize = 0, /* use wMaxPacketSize */ 278 .mh.callback = &uchcom_intr_callback, 279 }, 280 281 [5] = { 282 .type = UE_CONTROL, 283 .endpoint = 0x00, /* Control pipe */ 284 .direction = UE_DIR_ANY, 285 .mh.bufsize = sizeof(struct usb2_device_request), 286 .mh.callback = &uchcom_intr_clear_stall_callback, 287 .mh.timeout = 1000, /* 1 second */ 288 .mh.interval = 50, /* 50ms */ 289 }, 290}; 291 292struct usb2_com_callback uchcom_callback = { 293 .usb2_com_cfg_get_status = &uchcom_cfg_get_status, 294 .usb2_com_cfg_set_dtr = &uchcom_cfg_set_dtr, 295 .usb2_com_cfg_set_rts = &uchcom_cfg_set_rts, 296 .usb2_com_cfg_set_break = &uchcom_cfg_set_break, 297 .usb2_com_cfg_param = &uchcom_cfg_param, 298 .usb2_com_pre_param = &uchcom_pre_param, 299 .usb2_com_ioctl = &uchcom_ioctl, 300 .usb2_com_start_read = &uchcom_start_read, 301 .usb2_com_stop_read = &uchcom_stop_read, 302 .usb2_com_start_write = &uchcom_start_write, 303 .usb2_com_stop_write = &uchcom_stop_write, 304}; 305 306/* ---------------------------------------------------------------------- 307 * driver entry points 308 */ 309 310static int 311uchcom_probe(device_t dev) 312{ 313 struct usb2_attach_arg *uaa = device_get_ivars(dev); 314 315 DPRINTFN(11, "\n"); 316 317 if (uaa->usb2_mode != USB_MODE_HOST) { 318 return (ENXIO); 319 } 320 if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) { 321 return (ENXIO); 322 } 323 if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) { 324 return (ENXIO); 325 } 326 return (usb2_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa)); 327} 328 329static int 330uchcom_attach(device_t dev) 331{ 332 struct uchcom_softc *sc = device_get_softc(dev); 333 struct usb2_attach_arg *uaa = device_get_ivars(dev); 334 int error; 335 uint8_t iface_index; 336 337 DPRINTFN(11, "\n"); 338 339 if (sc == NULL) { 340 return (ENOMEM); 341 } 342 device_set_usb2_desc(dev); 343 344 sc->sc_udev = uaa->device; 345 346 switch (uaa->info.bcdDevice) { 347 case UCHCOM_REV_CH340: 348 device_printf(dev, "CH340 detected\n"); 349 break; 350 default: 351 device_printf(dev, "CH341 detected\n"); 352 break; 353 } 354 355 iface_index = UCHCOM_IFACE_INDEX; 356 error = usb2_transfer_setup(uaa->device, 357 &iface_index, sc->sc_xfer, uchcom_config_data, 358 UCHCOM_N_TRANSFER, sc, &Giant); 359 360 if (error) { 361 DPRINTF("one or more missing USB endpoints, " 362 "error=%s\n", usb2_errstr(error)); 363 goto detach; 364 } 365 /* 366 * Do the initialization during attach so that the system does not 367 * sleep during open: 368 */ 369 uchcom_update_version(sc); 370 uchcom_clear_chip(sc); 371 uchcom_reset_chip(sc); 372 uchcom_update_status(sc); 373 374 sc->sc_dtr = 1; 375 sc->sc_rts = 1; 376 377 /* clear stall at first run */ 378 sc->sc_flag |= (UCHCOM_FLAG_READ_STALL | 379 UCHCOM_FLAG_WRITE_STALL); 380 381 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 382 &uchcom_callback, &Giant); 383 if (error) { 384 goto detach; 385 } 386 return (0); 387 388detach: 389 uchcom_detach(dev); 390 return (ENXIO); 391} 392 393static int 394uchcom_detach(device_t dev) 395{ 396 struct uchcom_softc *sc = device_get_softc(dev); 397 398 DPRINTFN(11, "\n"); 399 400 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1); 401 402 usb2_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER); 403 404 return (0); 405} 406 407/* ---------------------------------------------------------------------- 408 * low level i/o 409 */ 410 411static void 412uchcom_do_request(struct uchcom_softc *sc, 413 struct usb2_device_request *req, void *data) 414{ 415 uint16_t length; 416 uint16_t actlen; 417 usb2_error_t err; 418 419 length = UGETW(req->wLength); 420 actlen = 0; 421 422 if (usb2_com_cfg_is_gone(&sc->sc_ucom)) { 423 goto done; 424 } 425 err = usb2_do_request_flags(sc->sc_udev, &Giant, req, 426 data, USB_SHORT_XFER_OK, &actlen, 1000); 427 428 if (err) { 429 DPRINTFN(0, "device request failed, err=%s " 430 "(ignored)\n", usb2_errstr(err)); 431 } 432done: 433 if (length != actlen) { 434 if (req->bmRequestType & UT_READ) { 435 bzero(USB_ADD_BYTES(data, actlen), length - actlen); 436 } 437 } 438 return; 439} 440 441static void 442uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno, 443 uint16_t value, uint16_t index) 444{ 445 struct usb2_device_request req; 446 447 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 448 req.bRequest = reqno; 449 USETW(req.wValue, value); 450 USETW(req.wIndex, index); 451 USETW(req.wLength, 0); 452 453 uchcom_do_request(sc, &req, NULL); 454 return; 455} 456 457static void 458uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno, 459 uint16_t value, uint16_t index, void *buf, uint16_t buflen) 460{ 461 struct usb2_device_request req; 462 463 req.bmRequestType = UT_READ_VENDOR_DEVICE; 464 req.bRequest = reqno; 465 USETW(req.wValue, value); 466 USETW(req.wIndex, index); 467 USETW(req.wLength, buflen); 468 469 uchcom_do_request(sc, &req, buf); 470 return; 471} 472 473static void 474uchcom_write_reg(struct uchcom_softc *sc, 475 uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2) 476{ 477 DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n", 478 (unsigned)reg1, (unsigned)val1, 479 (unsigned)reg2, (unsigned)val2); 480 uchcom_ctrl_write( 481 sc, UCHCOM_REQ_WRITE_REG, 482 reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8)); 483 return; 484} 485 486static void 487uchcom_read_reg(struct uchcom_softc *sc, 488 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2) 489{ 490 uint8_t buf[UCHCOM_INPUT_BUF_SIZE]; 491 492 uchcom_ctrl_read( 493 sc, UCHCOM_REQ_READ_REG, 494 reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf)); 495 496 DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n", 497 (unsigned)reg1, (unsigned)buf[0], 498 (unsigned)reg2, (unsigned)buf[1]); 499 500 if (rval1) 501 *rval1 = buf[0]; 502 if (rval2) 503 *rval2 = buf[1]; 504 505 return; 506} 507 508static void 509uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver) 510{ 511 uint8_t buf[UCHCOM_INPUT_BUF_SIZE]; 512 513 uchcom_ctrl_read( 514 sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf)); 515 516 if (rver) 517 *rver = buf[0]; 518 519 return; 520} 521 522static void 523uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval) 524{ 525 uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL); 526 return; 527} 528 529static void 530uchcom_set_dtrrts_10(struct uchcom_softc *sc, uint8_t val) 531{ 532 uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val); 533 return; 534} 535 536static void 537uchcom_set_dtrrts_20(struct uchcom_softc *sc, uint8_t val) 538{ 539 uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0); 540 return; 541} 542 543 544/* ---------------------------------------------------------------------- 545 * middle layer 546 */ 547 548static void 549uchcom_update_version(struct uchcom_softc *sc) 550{ 551 uchcom_get_version(sc, &sc->sc_version); 552 return; 553} 554 555static void 556uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur) 557{ 558 sc->sc_dtr = !(cur & UCHCOM_DTR_MASK); 559 sc->sc_rts = !(cur & UCHCOM_RTS_MASK); 560 561 cur = ~cur & 0x0F; 562 sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur); 563} 564 565static void 566uchcom_update_status(struct uchcom_softc *sc) 567{ 568 uint8_t cur; 569 570 uchcom_get_status(sc, &cur); 571 uchcom_convert_status(sc, cur); 572 return; 573} 574 575 576static void 577uchcom_set_dtrrts(struct uchcom_softc *sc) 578{ 579 uint8_t val = 0; 580 581 if (sc->sc_dtr) 582 val |= UCHCOM_DTR_MASK; 583 if (sc->sc_rts) 584 val |= UCHCOM_RTS_MASK; 585 586 if (sc->sc_version < UCHCOM_VER_20) 587 uchcom_set_dtrrts_10(sc, ~val); 588 else 589 uchcom_set_dtrrts_20(sc, ~val); 590 591 return; 592} 593 594static void 595uchcom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff) 596{ 597 struct uchcom_softc *sc = ucom->sc_parent; 598 uint8_t brk1; 599 uint8_t brk2; 600 601 uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2); 602 if (onoff) { 603 /* on - clear bits */ 604 brk1 &= ~UCHCOM_BRK1_MASK; 605 brk2 &= ~UCHCOM_BRK2_MASK; 606 } else { 607 /* off - set bits */ 608 brk1 |= UCHCOM_BRK1_MASK; 609 brk2 |= UCHCOM_BRK2_MASK; 610 } 611 uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2); 612 613 return; 614} 615 616static int 617uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate) 618{ 619 const struct uchcom_divider_record *rp; 620 uint32_t div; 621 uint32_t rem; 622 uint32_t mod; 623 uint8_t i; 624 625 /* find record */ 626 for (i = 0; i != NUM_DIVIDERS; i++) { 627 if (dividers[i].dvr_high >= rate && 628 dividers[i].dvr_low <= rate) { 629 rp = ÷rs[i]; 630 goto found; 631 } 632 } 633 return (-1); 634 635found: 636 dp->dv_prescaler = rp->dvr_divider.dv_prescaler; 637 if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN) 638 dp->dv_div = rp->dvr_divider.dv_div; 639 else { 640 div = rp->dvr_base_clock / rate; 641 rem = rp->dvr_base_clock % rate; 642 if (div == 0 || div >= 0xFF) 643 return (-1); 644 if ((rem << 1) >= rate) 645 div += 1; 646 dp->dv_div = (uint8_t)-div; 647 } 648 649 mod = UCHCOM_BPS_MOD_BASE / rate + UCHCOM_BPS_MOD_BASE_OFS; 650 mod = mod + mod / 2; 651 652 dp->dv_mod = mod / 0x100; 653 654 return (0); 655} 656 657static void 658uchcom_set_dte_rate(struct uchcom_softc *sc, uint32_t rate) 659{ 660 struct uchcom_divider dv; 661 662 if (uchcom_calc_divider_settings(&dv, rate)) 663 return; 664 665 uchcom_write_reg(sc, 666 UCHCOM_REG_BPS_PRE, dv.dv_prescaler, 667 UCHCOM_REG_BPS_DIV, dv.dv_div); 668 uchcom_write_reg(sc, 669 UCHCOM_REG_BPS_MOD, dv.dv_mod, 670 UCHCOM_REG_BPS_PAD, 0); 671 return; 672} 673 674static void 675uchcom_set_line_control(struct uchcom_softc *sc, tcflag_t cflag) 676{ 677 uint8_t lcr1 = 0; 678 uint8_t lcr2 = 0; 679 680 uchcom_read_reg(sc, UCHCOM_REG_LCR1, &lcr1, UCHCOM_REG_LCR2, &lcr2); 681 682 lcr1 &= ~UCHCOM_LCR1_MASK; 683 lcr2 &= ~UCHCOM_LCR2_MASK; 684 685 /* 686 * XXX: it is difficult to handle the line control appropriately: 687 * - CS8, !CSTOPB and any parity mode seems ok, but 688 * - the chip doesn't have the function to calculate parity 689 * in !CS8 mode. 690 * - it is unclear that the chip supports CS5,6 mode. 691 * - it is unclear how to handle stop bits. 692 */ 693 694 if (cflag & PARENB) { 695 lcr1 |= UCHCOM_LCR1_PARENB; 696 if (cflag & PARODD) 697 lcr2 |= UCHCOM_LCR2_PARODD; 698 else 699 lcr2 |= UCHCOM_LCR2_PAREVEN; 700 } 701 uchcom_write_reg(sc, UCHCOM_REG_LCR1, lcr1, UCHCOM_REG_LCR2, lcr2); 702 703 return; 704} 705 706static void 707uchcom_clear_chip(struct uchcom_softc *sc) 708{ 709 DPRINTF("\n"); 710 uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0); 711 return; 712} 713 714static void 715uchcom_reset_chip(struct uchcom_softc *sc) 716{ 717 uint16_t val; 718 uint16_t idx; 719 uint8_t lcr1; 720 uint8_t lcr2; 721 uint8_t pre; 722 uint8_t div; 723 uint8_t mod; 724 725 uchcom_read_reg(sc, UCHCOM_REG_LCR1, &lcr1, UCHCOM_REG_LCR2, &lcr2); 726 uchcom_read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div); 727 uchcom_read_reg(sc, UCHCOM_REG_BPS_MOD, &mod, UCHCOM_REG_BPS_PAD, NULL); 728 729 val = 0; 730 idx = 0; 731 val |= (uint16_t)(lcr1 & 0xF0) << 8; 732 val |= 0x01; 733 val |= (uint16_t)(lcr2 & 0x0F) << 8; 734 val |= 0x02; 735 idx |= pre & 0x07; 736 val |= 0x04; 737 idx |= (uint16_t)div << 8; 738 val |= 0x08; 739 idx |= mod & 0xF8; 740 val |= 0x10; 741 742 DPRINTF("reset v=0x%04X, i=0x%04X\n", val, idx); 743 744 uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, val, idx); 745 746 return; 747} 748 749/* ---------------------------------------------------------------------- 750 * methods for ucom 751 */ 752static void 753uchcom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr) 754{ 755 struct uchcom_softc *sc = ucom->sc_parent; 756 757 DPRINTF("\n"); 758 759 *lsr = sc->sc_lsr; 760 *msr = sc->sc_msr; 761 return; 762} 763 764static int 765uchcom_ioctl(struct usb2_com_softc *ucom, uint32_t cmd, caddr_t data, int flag, 766 struct thread *td) 767{ 768 return (ENOTTY); 769} 770 771static void 772uchcom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff) 773{ 774 struct uchcom_softc *sc = ucom->sc_parent; 775 776 DPRINTF("onoff = %d\n", onoff); 777 778 sc->sc_dtr = onoff; 779 uchcom_set_dtrrts(sc); 780 return; 781} 782 783static void 784uchcom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff) 785{ 786 struct uchcom_softc *sc = ucom->sc_parent; 787 788 DPRINTF("onoff = %d\n", onoff); 789 790 sc->sc_rts = onoff; 791 uchcom_set_dtrrts(sc); 792 return; 793} 794 795static int 796uchcom_pre_param(struct usb2_com_softc *ucom, struct termios *t) 797{ 798 struct uchcom_divider dv; 799 800 switch (t->c_cflag & CSIZE) { 801 case CS5: 802 case CS6: 803 case CS7: 804 return (EIO); 805 default: 806 break; 807 } 808 809 if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) { 810 return (EIO); 811 } 812 return (0); /* success */ 813} 814 815static void 816uchcom_cfg_param(struct usb2_com_softc *ucom, struct termios *t) 817{ 818 struct uchcom_softc *sc = ucom->sc_parent; 819 820 uchcom_set_line_control(sc, t->c_cflag); 821 uchcom_set_dte_rate(sc, t->c_ospeed); 822 return; 823} 824 825static void 826uchcom_start_read(struct usb2_com_softc *ucom) 827{ 828 struct uchcom_softc *sc = ucom->sc_parent; 829 830 /* start interrupt endpoint */ 831 usb2_transfer_start(sc->sc_xfer[4]); 832 833 /* start read endpoint */ 834 usb2_transfer_start(sc->sc_xfer[1]); 835 return; 836} 837 838static void 839uchcom_stop_read(struct usb2_com_softc *ucom) 840{ 841 struct uchcom_softc *sc = ucom->sc_parent; 842 843 /* stop interrupt endpoint */ 844 usb2_transfer_stop(sc->sc_xfer[4]); 845 846 /* stop read endpoint */ 847 usb2_transfer_stop(sc->sc_xfer[3]); 848 usb2_transfer_stop(sc->sc_xfer[1]); 849 return; 850} 851 852static void 853uchcom_start_write(struct usb2_com_softc *ucom) 854{ 855 struct uchcom_softc *sc = ucom->sc_parent; 856 857 usb2_transfer_start(sc->sc_xfer[0]); 858 return; 859} 860 861static void 862uchcom_stop_write(struct usb2_com_softc *ucom) 863{ 864 struct uchcom_softc *sc = ucom->sc_parent; 865 866 usb2_transfer_stop(sc->sc_xfer[2]); 867 usb2_transfer_stop(sc->sc_xfer[0]); 868 return; 869} 870 871/* ---------------------------------------------------------------------- 872 * callback when the modem status is changed. 873 */ 874static void 875uchcom_intr_callback(struct usb2_xfer *xfer) 876{ 877 struct uchcom_softc *sc = xfer->priv_sc; 878 uint8_t buf[UCHCOM_INTR_LEAST]; 879 880 switch (USB_GET_STATE(xfer)) { 881 case USB_ST_TRANSFERRED: 882 883 DPRINTF("actlen = %u\n", xfer->actlen); 884 885 if (xfer->actlen >= UCHCOM_INTR_LEAST) { 886 usb2_copy_out(xfer->frbuffers, 0, buf, 887 UCHCOM_INTR_LEAST); 888 889 DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n", 890 (unsigned)buf[0], (unsigned)buf[1], 891 (unsigned)buf[2], (unsigned)buf[3]); 892 893 uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]); 894 usb2_com_status_change(&sc->sc_ucom); 895 } 896 case USB_ST_SETUP: 897 if (sc->sc_flag & UCHCOM_FLAG_INTR_STALL) { 898 usb2_transfer_start(sc->sc_xfer[5]); 899 } else { 900 xfer->frlengths[0] = xfer->max_data_length; 901 usb2_start_hardware(xfer); 902 } 903 break; 904 905 default: /* Error */ 906 if (xfer->error != USB_ERR_CANCELLED) { 907 sc->sc_flag |= UCHCOM_FLAG_INTR_STALL; 908 usb2_transfer_start(sc->sc_xfer[5]); 909 } 910 break; 911 } 912 return; 913} 914 915static void 916uchcom_intr_clear_stall_callback(struct usb2_xfer *xfer) 917{ 918 struct uchcom_softc *sc = xfer->priv_sc; 919 struct usb2_xfer *xfer_other = sc->sc_xfer[4]; 920 921 if (usb2_clear_stall_callback(xfer, xfer_other)) { 922 DPRINTF("stall cleared\n"); 923 sc->sc_flag &= ~UCHCOM_FLAG_INTR_STALL; 924 usb2_transfer_start(xfer_other); 925 } 926 return; 927} 928 929static void 930uchcom_write_callback(struct usb2_xfer *xfer) 931{ 932 struct uchcom_softc *sc = xfer->priv_sc; 933 uint32_t actlen; 934 935 switch (USB_GET_STATE(xfer)) { 936 case USB_ST_SETUP: 937 case USB_ST_TRANSFERRED: 938 if (sc->sc_flag & UCHCOM_FLAG_WRITE_STALL) { 939 usb2_transfer_start(sc->sc_xfer[2]); 940 return; 941 } 942 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0, 943 UCHCOM_BULK_BUF_SIZE, &actlen)) { 944 945 DPRINTF("actlen = %d\n", actlen); 946 947 xfer->frlengths[0] = actlen; 948 usb2_start_hardware(xfer); 949 } 950 return; 951 952 default: /* Error */ 953 if (xfer->error != USB_ERR_CANCELLED) { 954 sc->sc_flag |= UCHCOM_FLAG_WRITE_STALL; 955 usb2_transfer_start(sc->sc_xfer[2]); 956 } 957 return; 958 959 } 960} 961 962static void 963uchcom_write_clear_stall_callback(struct usb2_xfer *xfer) 964{ 965 struct uchcom_softc *sc = xfer->priv_sc; 966 struct usb2_xfer *xfer_other = sc->sc_xfer[0]; 967 968 if (usb2_clear_stall_callback(xfer, xfer_other)) { 969 DPRINTF("stall cleared\n"); 970 sc->sc_flag &= ~UCHCOM_FLAG_WRITE_STALL; 971 usb2_transfer_start(xfer_other); 972 } 973 return; 974} 975 976static void 977uchcom_read_callback(struct usb2_xfer *xfer) 978{ 979 struct uchcom_softc *sc = xfer->priv_sc; 980 981 switch (USB_GET_STATE(xfer)) { 982 case USB_ST_TRANSFERRED: 983 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen); 984 985 case USB_ST_SETUP: 986 if (sc->sc_flag & UCHCOM_FLAG_READ_STALL) { 987 usb2_transfer_start(sc->sc_xfer[3]); 988 } else { 989 xfer->frlengths[0] = xfer->max_data_length; 990 usb2_start_hardware(xfer); 991 } 992 return; 993 994 default: /* Error */ 995 if (xfer->error != USB_ERR_CANCELLED) { 996 sc->sc_flag |= UCHCOM_FLAG_READ_STALL; 997 usb2_transfer_start(sc->sc_xfer[3]); 998 } 999 return; 1000 1001 } 1002} 1003 1004static void 1005uchcom_read_clear_stall_callback(struct usb2_xfer *xfer) 1006{ 1007 struct uchcom_softc *sc = xfer->priv_sc; 1008 struct usb2_xfer *xfer_other = sc->sc_xfer[1]; 1009 1010 if (usb2_clear_stall_callback(xfer, xfer_other)) { 1011 DPRINTF("stall cleared\n"); 1012 sc->sc_flag &= ~UCHCOM_FLAG_READ_STALL; 1013 usb2_transfer_start(xfer_other); 1014 } 1015 return; 1016} 1017 1018static device_method_t uchcom_methods[] = { 1019 /* Device interface */ 1020 DEVMETHOD(device_probe, uchcom_probe), 1021 DEVMETHOD(device_attach, uchcom_attach), 1022 DEVMETHOD(device_detach, uchcom_detach), 1023 1024 {0, 0} 1025}; 1026 1027static driver_t uchcom_driver = { 1028 "ucom", 1029 uchcom_methods, 1030 sizeof(struct uchcom_softc) 1031}; 1032 1033static devclass_t uchcom_devclass; 1034 1035DRIVER_MODULE(uchcom, ushub, uchcom_driver, uchcom_devclass, NULL, 0); 1036MODULE_DEPEND(uchcom, usb2_serial, 1, 1, 1); 1037MODULE_DEPEND(uchcom, usb2_core, 1, 1, 1); 1038