1 /* $FreeBSD$ */ 2 /* $OpenBSD: umoscom.c,v 1.2 2006/10/26 06:02:43 jsg Exp $ */ 3 4 /* 5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/stdint.h> 21 #include <sys/stddef.h> 22 #include <sys/param.h> 23 #include <sys/queue.h> 24 #include <sys/types.h> 25 #include <sys/systm.h> 26 #include <sys/kernel.h> 27 #include <sys/bus.h> 28 #include <sys/linker_set.h> 29 #include <sys/module.h> 30 #include <sys/lock.h> 31 #include <sys/mutex.h> 32 #include <sys/condvar.h> 33 #include <sys/sysctl.h> 34 #include <sys/sx.h> 35 #include <sys/unistd.h> 36 #include <sys/callout.h> 37 #include <sys/malloc.h> 38 #include <sys/priv.h> 39 40 #include <dev/usb/usb.h> 41 #include <dev/usb/usbdi.h> 42 #include <dev/usb/usbdi_util.h> 43 #include "usbdevs.h" 44 45 #define USB_DEBUG_VAR umoscom_debug 46 #include <dev/usb/usb_debug.h> 47 #include <dev/usb/usb_process.h> 48 49 #include <dev/usb/serial/usb_serial.h> 50 51 #ifdef USB_DEBUG 52 static int umoscom_debug = 0; 53 54 SYSCTL_NODE(_hw_usb, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom"); 55 SYSCTL_INT(_hw_usb_umoscom, OID_AUTO, debug, CTLFLAG_RW, 56 &umoscom_debug, 0, "Debug level"); 57 #endif 58 59 #define UMOSCOM_BUFSIZE 1024 /* bytes */ 60 61 #define UMOSCOM_CONFIG_INDEX 0 62 #define UMOSCOM_IFACE_INDEX 0 63 64 /* interrupt packet */ 65 #define UMOSCOM_IIR_RLS 0x06 66 #define UMOSCOM_IIR_RDA 0x04 67 #define UMOSCOM_IIR_CTI 0x0c 68 #define UMOSCOM_IIR_THR 0x02 69 #define UMOSCOM_IIR_MS 0x00 70 71 /* registers */ 72 #define UMOSCOM_READ 0x0d 73 #define UMOSCOM_WRITE 0x0e 74 #define UMOSCOM_UART_REG 0x0300 75 #define UMOSCOM_VEND_REG 0x0000 76 77 #define UMOSCOM_TXBUF 0x00 /* Write */ 78 #define UMOSCOM_RXBUF 0x00 /* Read */ 79 #define UMOSCOM_INT 0x01 80 #define UMOSCOM_FIFO 0x02 /* Write */ 81 #define UMOSCOM_ISR 0x02 /* Read */ 82 #define UMOSCOM_LCR 0x03 83 #define UMOSCOM_MCR 0x04 84 #define UMOSCOM_LSR 0x05 85 #define UMOSCOM_MSR 0x06 86 #define UMOSCOM_SCRATCH 0x07 87 #define UMOSCOM_DIV_LO 0x08 88 #define UMOSCOM_DIV_HI 0x09 89 #define UMOSCOM_EFR 0x0a 90 #define UMOSCOM_XON1 0x0b 91 #define UMOSCOM_XON2 0x0c 92 #define UMOSCOM_XOFF1 0x0d 93 #define UMOSCOM_XOFF2 0x0e 94 95 #define UMOSCOM_BAUDLO 0x00 96 #define UMOSCOM_BAUDHI 0x01 97 98 #define UMOSCOM_INT_RXEN 0x01 99 #define UMOSCOM_INT_TXEN 0x02 100 #define UMOSCOM_INT_RSEN 0x04 101 #define UMOSCOM_INT_MDMEM 0x08 102 #define UMOSCOM_INT_SLEEP 0x10 103 #define UMOSCOM_INT_XOFF 0x20 104 #define UMOSCOM_INT_RTS 0x40 105 106 #define UMOSCOM_FIFO_EN 0x01 107 #define UMOSCOM_FIFO_RXCLR 0x02 108 #define UMOSCOM_FIFO_TXCLR 0x04 109 #define UMOSCOM_FIFO_DMA_BLK 0x08 110 #define UMOSCOM_FIFO_TXLVL_MASK 0x30 111 #define UMOSCOM_FIFO_TXLVL_8 0x00 112 #define UMOSCOM_FIFO_TXLVL_16 0x10 113 #define UMOSCOM_FIFO_TXLVL_32 0x20 114 #define UMOSCOM_FIFO_TXLVL_56 0x30 115 #define UMOSCOM_FIFO_RXLVL_MASK 0xc0 116 #define UMOSCOM_FIFO_RXLVL_8 0x00 117 #define UMOSCOM_FIFO_RXLVL_16 0x40 118 #define UMOSCOM_FIFO_RXLVL_56 0x80 119 #define UMOSCOM_FIFO_RXLVL_80 0xc0 120 121 #define UMOSCOM_ISR_MDM 0x00 122 #define UMOSCOM_ISR_NONE 0x01 123 #define UMOSCOM_ISR_TX 0x02 124 #define UMOSCOM_ISR_RX 0x04 125 #define UMOSCOM_ISR_LINE 0x06 126 #define UMOSCOM_ISR_RXTIMEOUT 0x0c 127 #define UMOSCOM_ISR_RX_XOFF 0x10 128 #define UMOSCOM_ISR_RTSCTS 0x20 129 #define UMOSCOM_ISR_FIFOEN 0xc0 130 131 #define UMOSCOM_LCR_DBITS(x) ((x) - 5) 132 #define UMOSCOM_LCR_STOP_BITS_1 0x00 133 #define UMOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */ 134 #define UMOSCOM_LCR_PARITY_NONE 0x00 135 #define UMOSCOM_LCR_PARITY_ODD 0x08 136 #define UMOSCOM_LCR_PARITY_EVEN 0x18 137 #define UMOSCOM_LCR_BREAK 0x40 138 #define UMOSCOM_LCR_DIVLATCH_EN 0x80 139 140 #define UMOSCOM_MCR_DTR 0x01 141 #define UMOSCOM_MCR_RTS 0x02 142 #define UMOSCOM_MCR_LOOP 0x04 143 #define UMOSCOM_MCR_INTEN 0x08 144 #define UMOSCOM_MCR_LOOPBACK 0x10 145 #define UMOSCOM_MCR_XONANY 0x20 146 #define UMOSCOM_MCR_IRDA_EN 0x40 147 #define UMOSCOM_MCR_BAUD_DIV4 0x80 148 149 #define UMOSCOM_LSR_RXDATA 0x01 150 #define UMOSCOM_LSR_RXOVER 0x02 151 #define UMOSCOM_LSR_RXPAR_ERR 0x04 152 #define UMOSCOM_LSR_RXFRM_ERR 0x08 153 #define UMOSCOM_LSR_RXBREAK 0x10 154 #define UMOSCOM_LSR_TXEMPTY 0x20 155 #define UMOSCOM_LSR_TXALLEMPTY 0x40 156 #define UMOSCOM_LSR_TXFIFO_ERR 0x80 157 158 #define UMOSCOM_MSR_CTS_CHG 0x01 159 #define UMOSCOM_MSR_DSR_CHG 0x02 160 #define UMOSCOM_MSR_RI_CHG 0x04 161 #define UMOSCOM_MSR_CD_CHG 0x08 162 #define UMOSCOM_MSR_CTS 0x10 163 #define UMOSCOM_MSR_RTS 0x20 164 #define UMOSCOM_MSR_RI 0x40 165 #define UMOSCOM_MSR_CD 0x80 166 167 #define UMOSCOM_BAUD_REF 115200 168 169 enum { 170 UMOSCOM_BULK_DT_WR, 171 UMOSCOM_BULK_DT_RD, 172 UMOSCOM_INTR_DT_RD, 173 UMOSCOM_N_TRANSFER, 174 }; 175 176 struct umoscom_softc { 177 struct ucom_super_softc sc_super_ucom; 178 struct ucom_softc sc_ucom; 179 180 struct usb_xfer *sc_xfer[UMOSCOM_N_TRANSFER]; 181 struct usb_device *sc_udev; 182 struct mtx sc_mtx; 183 184 uint8_t sc_mcr; 185 uint8_t sc_lcr; 186 }; 187 188 /* prototypes */ 189 190 static device_probe_t umoscom_probe; 191 static device_attach_t umoscom_attach; 192 static device_detach_t umoscom_detach; 193 194 static usb_callback_t umoscom_write_callback; 195 static usb_callback_t umoscom_read_callback; 196 static usb_callback_t umoscom_intr_callback; 197 198 static void umoscom_cfg_open(struct ucom_softc *); 199 static void umoscom_cfg_close(struct ucom_softc *); 200 static void umoscom_cfg_set_break(struct ucom_softc *, uint8_t); 201 static void umoscom_cfg_set_dtr(struct ucom_softc *, uint8_t); 202 static void umoscom_cfg_set_rts(struct ucom_softc *, uint8_t); 203 static int umoscom_pre_param(struct ucom_softc *, struct termios *); 204 static void umoscom_cfg_param(struct ucom_softc *, struct termios *); 205 static void umoscom_cfg_get_status(struct ucom_softc *, uint8_t *, 206 uint8_t *); 207 static void umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t); 208 static uint8_t umoscom_cfg_read(struct umoscom_softc *, uint16_t); 209 static void umoscom_start_read(struct ucom_softc *); 210 static void umoscom_stop_read(struct ucom_softc *); 211 static void umoscom_start_write(struct ucom_softc *); 212 static void umoscom_stop_write(struct ucom_softc *); 213 static void umoscom_poll(struct ucom_softc *ucom); 214 215 static const struct usb_config umoscom_config_data[UMOSCOM_N_TRANSFER] = { 216 217 [UMOSCOM_BULK_DT_WR] = { 218 .type = UE_BULK, 219 .endpoint = UE_ADDR_ANY, 220 .direction = UE_DIR_OUT, 221 .bufsize = UMOSCOM_BUFSIZE, 222 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 223 .callback = &umoscom_write_callback, 224 }, 225 226 [UMOSCOM_BULK_DT_RD] = { 227 .type = UE_BULK, 228 .endpoint = UE_ADDR_ANY, 229 .direction = UE_DIR_IN, 230 .bufsize = UMOSCOM_BUFSIZE, 231 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 232 .callback = &umoscom_read_callback, 233 }, 234 235 [UMOSCOM_INTR_DT_RD] = { 236 .type = UE_INTERRUPT, 237 .endpoint = UE_ADDR_ANY, 238 .direction = UE_DIR_IN, 239 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 240 .bufsize = 0, /* use wMaxPacketSize */ 241 .callback = &umoscom_intr_callback, 242 }, 243 }; 244 245 static const struct ucom_callback umoscom_callback = { 246 /* configuration callbacks */ 247 .ucom_cfg_get_status = &umoscom_cfg_get_status, 248 .ucom_cfg_set_dtr = &umoscom_cfg_set_dtr, 249 .ucom_cfg_set_rts = &umoscom_cfg_set_rts, 250 .ucom_cfg_set_break = &umoscom_cfg_set_break, 251 .ucom_cfg_param = &umoscom_cfg_param, 252 .ucom_cfg_open = &umoscom_cfg_open, 253 .ucom_cfg_close = &umoscom_cfg_close, 254 255 /* other callbacks */ 256 .ucom_pre_param = &umoscom_pre_param, 257 .ucom_start_read = &umoscom_start_read, 258 .ucom_stop_read = &umoscom_stop_read, 259 .ucom_start_write = &umoscom_start_write, 260 .ucom_stop_write = &umoscom_stop_write, 261 .ucom_poll = &umoscom_poll, 262 }; 263 264 static device_method_t umoscom_methods[] = { 265 DEVMETHOD(device_probe, umoscom_probe), 266 DEVMETHOD(device_attach, umoscom_attach), 267 DEVMETHOD(device_detach, umoscom_detach), 268 {0, 0} 269 }; 270 271 static devclass_t umoscom_devclass; 272 273 static driver_t umoscom_driver = { 274 .name = "umoscom", 275 .methods = umoscom_methods, 276 .size = sizeof(struct umoscom_softc), 277 }; 278 279 DRIVER_MODULE(umoscom, uhub, umoscom_driver, umoscom_devclass, NULL, 0); 280 MODULE_DEPEND(umoscom, ucom, 1, 1, 1); 281 MODULE_DEPEND(umoscom, usb, 1, 1, 1); 282 MODULE_VERSION(umoscom, 1); 283 284 static const struct usb_device_id umoscom_devs[] = { 285 {USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)} 286 }; 287 288 static int 289 umoscom_probe(device_t dev) 290 { 291 struct usb_attach_arg *uaa = device_get_ivars(dev); 292 293 if (uaa->usb_mode != USB_MODE_HOST) { 294 return (ENXIO); 295 } 296 if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) { 297 return (ENXIO); 298 } 299 if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) { 300 return (ENXIO); 301 } 302 return (usbd_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa)); 303 } 304 305 static int 306 umoscom_attach(device_t dev) 307 { 308 struct usb_attach_arg *uaa = device_get_ivars(dev); 309 struct umoscom_softc *sc = device_get_softc(dev); 310 int error; 311 uint8_t iface_index; 312 313 sc->sc_udev = uaa->device; 314 sc->sc_mcr = 0x08; /* enable interrupts */ 315 316 /* XXX the device doesn't provide any ID string, so set a static one */ 317 device_set_desc(dev, "MOSCHIP USB Serial Port Adapter"); 318 device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n"); 319 320 mtx_init(&sc->sc_mtx, "umoscom", NULL, MTX_DEF); 321 322 iface_index = UMOSCOM_IFACE_INDEX; 323 error = usbd_transfer_setup(uaa->device, &iface_index, 324 sc->sc_xfer, umoscom_config_data, 325 UMOSCOM_N_TRANSFER, sc, &sc->sc_mtx); 326 327 if (error) { 328 goto detach; 329 } 330 /* clear stall at first run */ 331 mtx_lock(&sc->sc_mtx); 332 usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_WR]); 333 usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_RD]); 334 mtx_unlock(&sc->sc_mtx); 335 336 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 337 &umoscom_callback, &sc->sc_mtx); 338 if (error) { 339 goto detach; 340 } 341 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 342 343 return (0); 344 345 detach: 346 device_printf(dev, "attach error: %s\n", usbd_errstr(error)); 347 umoscom_detach(dev); 348 return (ENXIO); 349 } 350 351 static int 352 umoscom_detach(device_t dev) 353 { 354 struct umoscom_softc *sc = device_get_softc(dev); 355 356 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 357 usbd_transfer_unsetup(sc->sc_xfer, UMOSCOM_N_TRANSFER); 358 mtx_destroy(&sc->sc_mtx); 359 360 return (0); 361 } 362 363 static void 364 umoscom_cfg_open(struct ucom_softc *ucom) 365 { 366 struct umoscom_softc *sc = ucom->sc_parent; 367 368 DPRINTF("\n"); 369 370 /* Purge FIFOs or odd things happen */ 371 umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG); 372 373 /* Enable FIFO */ 374 umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN | 375 UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR | 376 UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK | 377 UMOSCOM_UART_REG); 378 379 /* Enable Interrupt Registers */ 380 umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG); 381 382 /* Magic */ 383 umoscom_cfg_write(sc, 0x01, 0x08); 384 385 /* Magic */ 386 umoscom_cfg_write(sc, 0x00, 0x02); 387 } 388 389 static void 390 umoscom_cfg_close(struct ucom_softc *ucom) 391 { 392 return; 393 } 394 395 static void 396 umoscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 397 { 398 struct umoscom_softc *sc = ucom->sc_parent; 399 uint16_t val; 400 401 val = sc->sc_lcr; 402 if (onoff) 403 val |= UMOSCOM_LCR_BREAK; 404 405 umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG); 406 } 407 408 static void 409 umoscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 410 { 411 struct umoscom_softc *sc = ucom->sc_parent; 412 413 if (onoff) 414 sc->sc_mcr |= UMOSCOM_MCR_DTR; 415 else 416 sc->sc_mcr &= ~UMOSCOM_MCR_DTR; 417 418 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG); 419 } 420 421 static void 422 umoscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 423 { 424 struct umoscom_softc *sc = ucom->sc_parent; 425 426 if (onoff) 427 sc->sc_mcr |= UMOSCOM_MCR_RTS; 428 else 429 sc->sc_mcr &= ~UMOSCOM_MCR_RTS; 430 431 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG); 432 } 433 434 static int 435 umoscom_pre_param(struct ucom_softc *ucom, struct termios *t) 436 { 437 if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200)) 438 return (EINVAL); 439 440 return (0); 441 } 442 443 static void 444 umoscom_cfg_param(struct ucom_softc *ucom, struct termios *t) 445 { 446 struct umoscom_softc *sc = ucom->sc_parent; 447 uint16_t data; 448 449 DPRINTF("speed=%d\n", t->c_ospeed); 450 451 data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed); 452 453 if (data == 0) { 454 DPRINTF("invalid baud rate!\n"); 455 return; 456 } 457 umoscom_cfg_write(sc, UMOSCOM_LCR, 458 UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG); 459 460 umoscom_cfg_write(sc, UMOSCOM_BAUDLO, 461 (data & 0xFF) | UMOSCOM_UART_REG); 462 463 umoscom_cfg_write(sc, UMOSCOM_BAUDHI, 464 ((data >> 8) & 0xFF) | UMOSCOM_UART_REG); 465 466 if (t->c_cflag & CSTOPB) 467 data = UMOSCOM_LCR_STOP_BITS_2; 468 else 469 data = UMOSCOM_LCR_STOP_BITS_1; 470 471 if (t->c_cflag & PARENB) { 472 if (t->c_cflag & PARODD) 473 data |= UMOSCOM_LCR_PARITY_ODD; 474 else 475 data |= UMOSCOM_LCR_PARITY_EVEN; 476 } else 477 data |= UMOSCOM_LCR_PARITY_NONE; 478 479 switch (t->c_cflag & CSIZE) { 480 case CS5: 481 data |= UMOSCOM_LCR_DBITS(5); 482 break; 483 case CS6: 484 data |= UMOSCOM_LCR_DBITS(6); 485 break; 486 case CS7: 487 data |= UMOSCOM_LCR_DBITS(7); 488 break; 489 case CS8: 490 data |= UMOSCOM_LCR_DBITS(8); 491 break; 492 } 493 494 sc->sc_lcr = data; 495 umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG); 496 } 497 498 static void 499 umoscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr) 500 { 501 struct umoscom_softc *sc = ucom->sc_parent; 502 uint8_t lsr; 503 uint8_t msr; 504 505 DPRINTFN(5, "\n"); 506 507 /* read status registers */ 508 509 lsr = umoscom_cfg_read(sc, UMOSCOM_LSR); 510 msr = umoscom_cfg_read(sc, UMOSCOM_MSR); 511 512 /* translate bits */ 513 514 if (msr & UMOSCOM_MSR_CTS) 515 *p_msr |= SER_CTS; 516 517 if (msr & UMOSCOM_MSR_CD) 518 *p_msr |= SER_DCD; 519 520 if (msr & UMOSCOM_MSR_RI) 521 *p_msr |= SER_RI; 522 523 if (msr & UMOSCOM_MSR_RTS) 524 *p_msr |= SER_DSR; 525 } 526 527 static void 528 umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val) 529 { 530 struct usb_device_request req; 531 532 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 533 req.bRequest = UMOSCOM_WRITE; 534 USETW(req.wValue, val); 535 USETW(req.wIndex, reg); 536 USETW(req.wLength, 0); 537 538 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 539 &req, NULL, 0, 1000); 540 } 541 542 static uint8_t 543 umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg) 544 { 545 struct usb_device_request req; 546 uint8_t val; 547 548 req.bmRequestType = UT_READ_VENDOR_DEVICE; 549 req.bRequest = UMOSCOM_READ; 550 USETW(req.wValue, 0); 551 USETW(req.wIndex, reg); 552 USETW(req.wLength, 1); 553 554 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 555 &req, &val, 0, 1000); 556 557 DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val); 558 559 return (val); 560 } 561 562 static void 563 umoscom_start_read(struct ucom_softc *ucom) 564 { 565 struct umoscom_softc *sc = ucom->sc_parent; 566 567 #if 0 568 /* start interrupt endpoint */ 569 usbd_transfer_start(sc->sc_xfer[UMOSCOM_INTR_DT_RD]); 570 #endif 571 /* start read endpoint */ 572 usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_RD]); 573 } 574 575 static void 576 umoscom_stop_read(struct ucom_softc *ucom) 577 { 578 struct umoscom_softc *sc = ucom->sc_parent; 579 580 /* stop interrupt transfer */ 581 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_INTR_DT_RD]); 582 583 /* stop read endpoint */ 584 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_RD]); 585 } 586 587 static void 588 umoscom_start_write(struct ucom_softc *ucom) 589 { 590 struct umoscom_softc *sc = ucom->sc_parent; 591 592 usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_WR]); 593 } 594 595 static void 596 umoscom_stop_write(struct ucom_softc *ucom) 597 { 598 struct umoscom_softc *sc = ucom->sc_parent; 599 600 usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_WR]); 601 } 602 603 static void 604 umoscom_write_callback(struct usb_xfer *xfer, usb_error_t error) 605 { 606 struct umoscom_softc *sc = usbd_xfer_softc(xfer); 607 struct usb_page_cache *pc; 608 uint32_t actlen; 609 610 switch (USB_GET_STATE(xfer)) { 611 case USB_ST_SETUP: 612 case USB_ST_TRANSFERRED: 613 tr_setup: 614 DPRINTF("\n"); 615 616 pc = usbd_xfer_get_frame(xfer, 0); 617 if (ucom_get_data(&sc->sc_ucom, pc, 0, 618 UMOSCOM_BUFSIZE, &actlen)) { 619 620 usbd_xfer_set_frame_len(xfer, 0, actlen); 621 usbd_transfer_submit(xfer); 622 } 623 return; 624 625 default: /* Error */ 626 if (error != USB_ERR_CANCELLED) { 627 DPRINTFN(0, "transfer failed\n"); 628 /* try to clear stall first */ 629 usbd_xfer_set_stall(xfer); 630 goto tr_setup; 631 } 632 return; 633 } 634 } 635 636 static void 637 umoscom_read_callback(struct usb_xfer *xfer, usb_error_t error) 638 { 639 struct umoscom_softc *sc = usbd_xfer_softc(xfer); 640 struct usb_page_cache *pc; 641 int actlen; 642 643 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 644 645 switch (USB_GET_STATE(xfer)) { 646 case USB_ST_TRANSFERRED: 647 DPRINTF("got %d bytes\n", actlen); 648 pc = usbd_xfer_get_frame(xfer, 0); 649 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 650 651 case USB_ST_SETUP: 652 tr_setup: 653 DPRINTF("\n"); 654 655 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 656 usbd_transfer_submit(xfer); 657 return; 658 659 default: /* Error */ 660 if (error != USB_ERR_CANCELLED) { 661 DPRINTFN(0, "transfer failed\n"); 662 /* try to clear stall first */ 663 usbd_xfer_set_stall(xfer); 664 goto tr_setup; 665 } 666 return; 667 } 668 } 669 670 static void 671 umoscom_intr_callback(struct usb_xfer *xfer, usb_error_t error) 672 { 673 struct umoscom_softc *sc = usbd_xfer_softc(xfer); 674 int actlen; 675 676 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 677 678 switch (USB_GET_STATE(xfer)) { 679 case USB_ST_TRANSFERRED: 680 if (actlen < 2) { 681 DPRINTF("too short message\n"); 682 goto tr_setup; 683 } 684 ucom_status_change(&sc->sc_ucom); 685 686 case USB_ST_SETUP: 687 tr_setup: 688 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 689 usbd_transfer_submit(xfer); 690 return; 691 692 default: /* Error */ 693 if (error != USB_ERR_CANCELLED) { 694 DPRINTFN(0, "transfer failed\n"); 695 /* try to clear stall first */ 696 usbd_xfer_set_stall(xfer); 697 goto tr_setup; 698 } 699 return; 700 } 701 } 702 703 static void 704 umoscom_poll(struct ucom_softc *ucom) 705 { 706 struct umoscom_softc *sc = ucom->sc_parent; 707 usbd_transfer_poll(sc->sc_xfer, UMOSCOM_N_TRANSFER); 708 } 709