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