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