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