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$"); 67 68 /* 69 * Driver for WinChipHead CH341/340, the worst USB-serial chip in the 70 * world. 71 */ 72 73 #include <sys/stdint.h> 74 #include <sys/stddef.h> 75 #include <sys/param.h> 76 #include <sys/queue.h> 77 #include <sys/types.h> 78 #include <sys/systm.h> 79 #include <sys/kernel.h> 80 #include <sys/bus.h> 81 #include <sys/linker_set.h> 82 #include <sys/module.h> 83 #include <sys/lock.h> 84 #include <sys/mutex.h> 85 #include <sys/condvar.h> 86 #include <sys/sysctl.h> 87 #include <sys/sx.h> 88 #include <sys/unistd.h> 89 #include <sys/callout.h> 90 #include <sys/malloc.h> 91 #include <sys/priv.h> 92 93 #include <dev/usb/usb.h> 94 #include <dev/usb/usbdi.h> 95 #include <dev/usb/usbdi_util.h> 96 #include "usbdevs.h" 97 98 #define USB_DEBUG_VAR uchcom_debug 99 #include <dev/usb/usb_debug.h> 100 #include <dev/usb/usb_process.h> 101 102 #include <dev/usb/serial/usb_serial.h> 103 104 #ifdef USB_DEBUG 105 static int uchcom_debug = 0; 106 107 SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom"); 108 SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RW, 109 &uchcom_debug, 0, "uchcom debug level"); 110 #endif 111 112 #define UCHCOM_IFACE_INDEX 0 113 #define UCHCOM_CONFIG_INDEX 0 114 115 #define UCHCOM_REV_CH340 0x0250 116 #define UCHCOM_INPUT_BUF_SIZE 8 117 118 #define UCHCOM_REQ_GET_VERSION 0x5F 119 #define UCHCOM_REQ_READ_REG 0x95 120 #define UCHCOM_REQ_WRITE_REG 0x9A 121 #define UCHCOM_REQ_RESET 0xA1 122 #define UCHCOM_REQ_SET_DTRRTS 0xA4 123 124 #define UCHCOM_REG_STAT1 0x06 125 #define UCHCOM_REG_STAT2 0x07 126 #define UCHCOM_REG_BPS_PRE 0x12 127 #define UCHCOM_REG_BPS_DIV 0x13 128 #define UCHCOM_REG_BPS_MOD 0x14 129 #define UCHCOM_REG_BPS_PAD 0x0F 130 #define UCHCOM_REG_BREAK1 0x05 131 #define UCHCOM_REG_BREAK2 0x18 132 #define UCHCOM_REG_LCR1 0x18 133 #define UCHCOM_REG_LCR2 0x25 134 135 #define UCHCOM_VER_20 0x20 136 137 #define UCHCOM_BASE_UNKNOWN 0 138 #define UCHCOM_BPS_MOD_BASE 20000000 139 #define UCHCOM_BPS_MOD_BASE_OFS 1100 140 141 #define UCHCOM_DTR_MASK 0x20 142 #define UCHCOM_RTS_MASK 0x40 143 144 #define UCHCOM_BRK1_MASK 0x01 145 #define UCHCOM_BRK2_MASK 0x40 146 147 #define UCHCOM_LCR1_MASK 0xAF 148 #define UCHCOM_LCR2_MASK 0x07 149 #define UCHCOM_LCR1_PARENB 0x80 150 #define UCHCOM_LCR2_PAREVEN 0x07 151 #define UCHCOM_LCR2_PARODD 0x06 152 #define UCHCOM_LCR2_PARMARK 0x05 153 #define UCHCOM_LCR2_PARSPACE 0x04 154 155 #define UCHCOM_INTR_STAT1 0x02 156 #define UCHCOM_INTR_STAT2 0x03 157 #define UCHCOM_INTR_LEAST 4 158 159 #define UCHCOM_BULK_BUF_SIZE 1024 /* bytes */ 160 161 enum { 162 UCHCOM_BULK_DT_WR, 163 UCHCOM_BULK_DT_RD, 164 UCHCOM_INTR_DT_RD, 165 UCHCOM_N_TRANSFER, 166 }; 167 168 struct uchcom_softc { 169 struct ucom_super_softc sc_super_ucom; 170 struct ucom_softc sc_ucom; 171 172 struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER]; 173 struct usb_device *sc_udev; 174 struct mtx sc_mtx; 175 176 uint8_t sc_dtr; /* local copy */ 177 uint8_t sc_rts; /* local copy */ 178 uint8_t sc_version; 179 uint8_t sc_msr; 180 uint8_t sc_lsr; /* local status register */ 181 }; 182 183 struct uchcom_divider { 184 uint8_t dv_prescaler; 185 uint8_t dv_div; 186 uint8_t dv_mod; 187 }; 188 189 struct uchcom_divider_record { 190 uint32_t dvr_high; 191 uint32_t dvr_low; 192 uint32_t dvr_base_clock; 193 struct uchcom_divider dvr_divider; 194 }; 195 196 static const struct uchcom_divider_record dividers[] = 197 { 198 {307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}}, 199 {921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}}, 200 {2999999, 23530, 6000000, {3, 0, 0}}, 201 {23529, 2942, 750000, {2, 0, 0}}, 202 {2941, 368, 93750, {1, 0, 0}}, 203 {367, 1, 11719, {0, 0, 0}}, 204 }; 205 206 #define NUM_DIVIDERS (sizeof (dividers) / sizeof (dividers[0])) 207 208 static const struct usb_device_id uchcom_devs[] = { 209 {USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)}, 210 {USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER, 0)}, 211 }; 212 213 /* protypes */ 214 215 static int uchcom_pre_param(struct ucom_softc *, struct termios *); 216 static void uchcom_cfg_get_status(struct ucom_softc *, uint8_t *, 217 uint8_t *); 218 static void uchcom_cfg_open(struct ucom_softc *ucom); 219 static void uchcom_cfg_param(struct ucom_softc *, struct termios *); 220 static void uchcom_cfg_set_break(struct ucom_softc *, uint8_t); 221 static void uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t); 222 static void uchcom_cfg_set_rts(struct ucom_softc *, uint8_t); 223 static void uchcom_start_read(struct ucom_softc *); 224 static void uchcom_start_write(struct ucom_softc *); 225 static void uchcom_stop_read(struct ucom_softc *); 226 static void uchcom_stop_write(struct ucom_softc *); 227 static void uchcom_update_version(struct uchcom_softc *); 228 static void uchcom_convert_status(struct uchcom_softc *, uint8_t); 229 static void uchcom_update_status(struct uchcom_softc *); 230 static void uchcom_set_dtr_rts(struct uchcom_softc *); 231 static int uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t); 232 static void uchcom_set_baudrate(struct uchcom_softc *, uint32_t); 233 static void uchcom_poll(struct ucom_softc *ucom); 234 235 static device_probe_t uchcom_probe; 236 static device_attach_t uchcom_attach; 237 static device_detach_t uchcom_detach; 238 239 static usb_callback_t uchcom_intr_callback; 240 static usb_callback_t uchcom_write_callback; 241 static usb_callback_t uchcom_read_callback; 242 243 static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = { 244 245 [UCHCOM_BULK_DT_WR] = { 246 .type = UE_BULK, 247 .endpoint = UE_ADDR_ANY, 248 .direction = UE_DIR_OUT, 249 .bufsize = UCHCOM_BULK_BUF_SIZE, 250 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 251 .callback = &uchcom_write_callback, 252 }, 253 254 [UCHCOM_BULK_DT_RD] = { 255 .type = UE_BULK, 256 .endpoint = UE_ADDR_ANY, 257 .direction = UE_DIR_IN, 258 .bufsize = UCHCOM_BULK_BUF_SIZE, 259 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 260 .callback = &uchcom_read_callback, 261 }, 262 263 [UCHCOM_INTR_DT_RD] = { 264 .type = UE_INTERRUPT, 265 .endpoint = UE_ADDR_ANY, 266 .direction = UE_DIR_IN, 267 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 268 .bufsize = 0, /* use wMaxPacketSize */ 269 .callback = &uchcom_intr_callback, 270 }, 271 }; 272 273 static struct ucom_callback uchcom_callback = { 274 .ucom_cfg_get_status = &uchcom_cfg_get_status, 275 .ucom_cfg_set_dtr = &uchcom_cfg_set_dtr, 276 .ucom_cfg_set_rts = &uchcom_cfg_set_rts, 277 .ucom_cfg_set_break = &uchcom_cfg_set_break, 278 .ucom_cfg_open = &uchcom_cfg_open, 279 .ucom_cfg_param = &uchcom_cfg_param, 280 .ucom_pre_param = &uchcom_pre_param, 281 .ucom_start_read = &uchcom_start_read, 282 .ucom_stop_read = &uchcom_stop_read, 283 .ucom_start_write = &uchcom_start_write, 284 .ucom_stop_write = &uchcom_stop_write, 285 .ucom_poll = &uchcom_poll, 286 }; 287 288 /* ---------------------------------------------------------------------- 289 * driver entry points 290 */ 291 292 static int 293 uchcom_probe(device_t dev) 294 { 295 struct usb_attach_arg *uaa = device_get_ivars(dev); 296 297 DPRINTFN(11, "\n"); 298 299 if (uaa->usb_mode != USB_MODE_HOST) { 300 return (ENXIO); 301 } 302 if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) { 303 return (ENXIO); 304 } 305 if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) { 306 return (ENXIO); 307 } 308 return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa)); 309 } 310 311 static int 312 uchcom_attach(device_t dev) 313 { 314 struct uchcom_softc *sc = device_get_softc(dev); 315 struct usb_attach_arg *uaa = device_get_ivars(dev); 316 int error; 317 uint8_t iface_index; 318 319 DPRINTFN(11, "\n"); 320 321 device_set_usb_desc(dev); 322 mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF); 323 324 sc->sc_udev = uaa->device; 325 326 switch (uaa->info.bcdDevice) { 327 case UCHCOM_REV_CH340: 328 device_printf(dev, "CH340 detected\n"); 329 break; 330 default: 331 device_printf(dev, "CH341 detected\n"); 332 break; 333 } 334 335 iface_index = UCHCOM_IFACE_INDEX; 336 error = usbd_transfer_setup(uaa->device, 337 &iface_index, sc->sc_xfer, uchcom_config_data, 338 UCHCOM_N_TRANSFER, sc, &sc->sc_mtx); 339 340 if (error) { 341 DPRINTF("one or more missing USB endpoints, " 342 "error=%s\n", usbd_errstr(error)); 343 goto detach; 344 } 345 346 /* clear stall at first run */ 347 mtx_lock(&sc->sc_mtx); 348 usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]); 349 usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]); 350 mtx_unlock(&sc->sc_mtx); 351 352 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 353 &uchcom_callback, &sc->sc_mtx); 354 if (error) { 355 goto detach; 356 } 357 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 358 359 return (0); 360 361 detach: 362 uchcom_detach(dev); 363 return (ENXIO); 364 } 365 366 static int 367 uchcom_detach(device_t dev) 368 { 369 struct uchcom_softc *sc = device_get_softc(dev); 370 371 DPRINTFN(11, "\n"); 372 373 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 374 usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER); 375 mtx_destroy(&sc->sc_mtx); 376 377 return (0); 378 } 379 380 /* ---------------------------------------------------------------------- 381 * low level i/o 382 */ 383 384 static void 385 uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno, 386 uint16_t value, uint16_t index) 387 { 388 struct usb_device_request req; 389 390 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 391 req.bRequest = reqno; 392 USETW(req.wValue, value); 393 USETW(req.wIndex, index); 394 USETW(req.wLength, 0); 395 396 ucom_cfg_do_request(sc->sc_udev, 397 &sc->sc_ucom, &req, NULL, 0, 1000); 398 } 399 400 static void 401 uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno, 402 uint16_t value, uint16_t index, void *buf, uint16_t buflen) 403 { 404 struct usb_device_request req; 405 406 req.bmRequestType = UT_READ_VENDOR_DEVICE; 407 req.bRequest = reqno; 408 USETW(req.wValue, value); 409 USETW(req.wIndex, index); 410 USETW(req.wLength, buflen); 411 412 ucom_cfg_do_request(sc->sc_udev, 413 &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000); 414 } 415 416 static void 417 uchcom_write_reg(struct uchcom_softc *sc, 418 uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2) 419 { 420 DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n", 421 (unsigned)reg1, (unsigned)val1, 422 (unsigned)reg2, (unsigned)val2); 423 uchcom_ctrl_write( 424 sc, UCHCOM_REQ_WRITE_REG, 425 reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8)); 426 } 427 428 static void 429 uchcom_read_reg(struct uchcom_softc *sc, 430 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2) 431 { 432 uint8_t buf[UCHCOM_INPUT_BUF_SIZE]; 433 434 uchcom_ctrl_read( 435 sc, UCHCOM_REQ_READ_REG, 436 reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf)); 437 438 DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n", 439 (unsigned)reg1, (unsigned)buf[0], 440 (unsigned)reg2, (unsigned)buf[1]); 441 442 if (rval1) 443 *rval1 = buf[0]; 444 if (rval2) 445 *rval2 = buf[1]; 446 } 447 448 static void 449 uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver) 450 { 451 uint8_t buf[UCHCOM_INPUT_BUF_SIZE]; 452 453 uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf)); 454 455 if (rver) 456 *rver = buf[0]; 457 } 458 459 static void 460 uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval) 461 { 462 uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL); 463 } 464 465 static void 466 uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val) 467 { 468 uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val); 469 } 470 471 static void 472 uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val) 473 { 474 uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0); 475 } 476 477 478 /* ---------------------------------------------------------------------- 479 * middle layer 480 */ 481 482 static void 483 uchcom_update_version(struct uchcom_softc *sc) 484 { 485 uchcom_get_version(sc, &sc->sc_version); 486 } 487 488 static void 489 uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur) 490 { 491 sc->sc_dtr = !(cur & UCHCOM_DTR_MASK); 492 sc->sc_rts = !(cur & UCHCOM_RTS_MASK); 493 494 cur = ~cur & 0x0F; 495 sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur); 496 } 497 498 static void 499 uchcom_update_status(struct uchcom_softc *sc) 500 { 501 uint8_t cur; 502 503 uchcom_get_status(sc, &cur); 504 uchcom_convert_status(sc, cur); 505 } 506 507 508 static void 509 uchcom_set_dtr_rts(struct uchcom_softc *sc) 510 { 511 uint8_t val = 0; 512 513 if (sc->sc_dtr) 514 val |= UCHCOM_DTR_MASK; 515 if (sc->sc_rts) 516 val |= UCHCOM_RTS_MASK; 517 518 if (sc->sc_version < UCHCOM_VER_20) 519 uchcom_set_dtr_rts_10(sc, ~val); 520 else 521 uchcom_set_dtr_rts_20(sc, ~val); 522 } 523 524 static void 525 uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 526 { 527 struct uchcom_softc *sc = ucom->sc_parent; 528 uint8_t brk1; 529 uint8_t brk2; 530 531 uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2); 532 if (onoff) { 533 /* on - clear bits */ 534 brk1 &= ~UCHCOM_BRK1_MASK; 535 brk2 &= ~UCHCOM_BRK2_MASK; 536 } else { 537 /* off - set bits */ 538 brk1 |= UCHCOM_BRK1_MASK; 539 brk2 |= UCHCOM_BRK2_MASK; 540 } 541 uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2); 542 } 543 544 static int 545 uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate) 546 { 547 const struct uchcom_divider_record *rp; 548 uint32_t div; 549 uint32_t rem; 550 uint32_t mod; 551 uint8_t i; 552 553 /* find record */ 554 for (i = 0; i != NUM_DIVIDERS; i++) { 555 if (dividers[i].dvr_high >= rate && 556 dividers[i].dvr_low <= rate) { 557 rp = ÷rs[i]; 558 goto found; 559 } 560 } 561 return (-1); 562 563 found: 564 dp->dv_prescaler = rp->dvr_divider.dv_prescaler; 565 if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN) 566 dp->dv_div = rp->dvr_divider.dv_div; 567 else { 568 div = rp->dvr_base_clock / rate; 569 rem = rp->dvr_base_clock % rate; 570 if (div == 0 || div >= 0xFF) 571 return (-1); 572 if ((rem << 1) >= rate) 573 div += 1; 574 dp->dv_div = (uint8_t)-div; 575 } 576 577 mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS; 578 mod = mod + (mod / 2); 579 580 dp->dv_mod = (mod + 0xFF) / 0x100; 581 582 return (0); 583 } 584 585 static void 586 uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate) 587 { 588 struct uchcom_divider dv; 589 590 if (uchcom_calc_divider_settings(&dv, rate)) 591 return; 592 593 uchcom_write_reg(sc, 594 UCHCOM_REG_BPS_PRE, dv.dv_prescaler, 595 UCHCOM_REG_BPS_DIV, dv.dv_div); 596 uchcom_write_reg(sc, 597 UCHCOM_REG_BPS_MOD, dv.dv_mod, 598 UCHCOM_REG_BPS_PAD, 0); 599 } 600 601 /* ---------------------------------------------------------------------- 602 * methods for ucom 603 */ 604 static void 605 uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 606 { 607 struct uchcom_softc *sc = ucom->sc_parent; 608 609 DPRINTF("\n"); 610 611 *lsr = sc->sc_lsr; 612 *msr = sc->sc_msr; 613 } 614 615 static void 616 uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 617 { 618 struct uchcom_softc *sc = ucom->sc_parent; 619 620 DPRINTF("onoff = %d\n", onoff); 621 622 sc->sc_dtr = onoff; 623 uchcom_set_dtr_rts(sc); 624 } 625 626 static void 627 uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 628 { 629 struct uchcom_softc *sc = ucom->sc_parent; 630 631 DPRINTF("onoff = %d\n", onoff); 632 633 sc->sc_rts = onoff; 634 uchcom_set_dtr_rts(sc); 635 } 636 637 static void 638 uchcom_cfg_open(struct ucom_softc *ucom) 639 { 640 struct uchcom_softc *sc = ucom->sc_parent; 641 642 DPRINTF("\n"); 643 644 uchcom_update_version(sc); 645 uchcom_update_status(sc); 646 } 647 648 static int 649 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t) 650 { 651 struct uchcom_divider dv; 652 653 switch (t->c_cflag & CSIZE) { 654 case CS8: 655 break; 656 default: 657 return (EIO); 658 } 659 660 if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) { 661 return (EIO); 662 } 663 return (0); /* success */ 664 } 665 666 static void 667 uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t) 668 { 669 struct uchcom_softc *sc = ucom->sc_parent; 670 671 uchcom_get_version(sc, 0); 672 uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0); 673 uchcom_set_baudrate(sc, t->c_ospeed); 674 uchcom_read_reg(sc, 0x18, 0, 0x25, 0); 675 uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00); 676 uchcom_update_status(sc); 677 uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a); 678 uchcom_set_baudrate(sc, t->c_ospeed); 679 uchcom_set_dtr_rts(sc); 680 uchcom_update_status(sc); 681 } 682 683 static void 684 uchcom_start_read(struct ucom_softc *ucom) 685 { 686 struct uchcom_softc *sc = ucom->sc_parent; 687 688 /* start interrupt endpoint */ 689 usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]); 690 691 /* start read endpoint */ 692 usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]); 693 } 694 695 static void 696 uchcom_stop_read(struct ucom_softc *ucom) 697 { 698 struct uchcom_softc *sc = ucom->sc_parent; 699 700 /* stop interrupt endpoint */ 701 usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]); 702 703 /* stop read endpoint */ 704 usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]); 705 } 706 707 static void 708 uchcom_start_write(struct ucom_softc *ucom) 709 { 710 struct uchcom_softc *sc = ucom->sc_parent; 711 712 usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]); 713 } 714 715 static void 716 uchcom_stop_write(struct ucom_softc *ucom) 717 { 718 struct uchcom_softc *sc = ucom->sc_parent; 719 720 usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]); 721 } 722 723 /* ---------------------------------------------------------------------- 724 * callback when the modem status is changed. 725 */ 726 static void 727 uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error) 728 { 729 struct uchcom_softc *sc = usbd_xfer_softc(xfer); 730 struct usb_page_cache *pc; 731 uint8_t buf[UCHCOM_INTR_LEAST]; 732 int actlen; 733 734 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 735 736 switch (USB_GET_STATE(xfer)) { 737 case USB_ST_TRANSFERRED: 738 739 DPRINTF("actlen = %u\n", actlen); 740 741 if (actlen >= UCHCOM_INTR_LEAST) { 742 pc = usbd_xfer_get_frame(xfer, 0); 743 usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST); 744 745 DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n", 746 (unsigned)buf[0], (unsigned)buf[1], 747 (unsigned)buf[2], (unsigned)buf[3]); 748 749 uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]); 750 ucom_status_change(&sc->sc_ucom); 751 } 752 case USB_ST_SETUP: 753 tr_setup: 754 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 755 usbd_transfer_submit(xfer); 756 break; 757 758 default: /* Error */ 759 if (error != USB_ERR_CANCELLED) { 760 /* try to clear stall first */ 761 usbd_xfer_set_stall(xfer); 762 goto tr_setup; 763 } 764 break; 765 } 766 } 767 768 static void 769 uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error) 770 { 771 struct uchcom_softc *sc = usbd_xfer_softc(xfer); 772 struct usb_page_cache *pc; 773 uint32_t actlen; 774 775 switch (USB_GET_STATE(xfer)) { 776 case USB_ST_SETUP: 777 case USB_ST_TRANSFERRED: 778 tr_setup: 779 pc = usbd_xfer_get_frame(xfer, 0); 780 if (ucom_get_data(&sc->sc_ucom, pc, 0, 781 usbd_xfer_max_len(xfer), &actlen)) { 782 783 DPRINTF("actlen = %d\n", actlen); 784 785 usbd_xfer_set_frame_len(xfer, 0, actlen); 786 usbd_transfer_submit(xfer); 787 } 788 break; 789 790 default: /* Error */ 791 if (error != USB_ERR_CANCELLED) { 792 /* try to clear stall first */ 793 usbd_xfer_set_stall(xfer); 794 goto tr_setup; 795 } 796 break; 797 } 798 } 799 800 static void 801 uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error) 802 { 803 struct uchcom_softc *sc = usbd_xfer_softc(xfer); 804 struct usb_page_cache *pc; 805 int actlen; 806 807 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 808 809 switch (USB_GET_STATE(xfer)) { 810 case USB_ST_TRANSFERRED: 811 812 if (actlen > 0) { 813 pc = usbd_xfer_get_frame(xfer, 0); 814 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 815 } 816 817 case USB_ST_SETUP: 818 tr_setup: 819 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 820 usbd_transfer_submit(xfer); 821 break; 822 823 default: /* Error */ 824 if (error != USB_ERR_CANCELLED) { 825 /* try to clear stall first */ 826 usbd_xfer_set_stall(xfer); 827 goto tr_setup; 828 } 829 break; 830 } 831 } 832 833 static void 834 uchcom_poll(struct ucom_softc *ucom) 835 { 836 struct uchcom_softc *sc = ucom->sc_parent; 837 usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER); 838 } 839 840 static device_method_t uchcom_methods[] = { 841 /* Device interface */ 842 DEVMETHOD(device_probe, uchcom_probe), 843 DEVMETHOD(device_attach, uchcom_attach), 844 DEVMETHOD(device_detach, uchcom_detach), 845 846 {0, 0} 847 }; 848 849 static driver_t uchcom_driver = { 850 "ucom", 851 uchcom_methods, 852 sizeof(struct uchcom_softc) 853 }; 854 855 static devclass_t uchcom_devclass; 856 857 DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0); 858 MODULE_DEPEND(uchcom, ucom, 1, 1, 1); 859 MODULE_DEPEND(uchcom, usb, 1, 1, 1); 860 MODULE_VERSION(uchcom, 1); 861