1 /* $OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $ */ 2 3 #include <sys/cdefs.h> 4 __FBSDID("$FreeBSD$"); 5 6 /* 7 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22 /* 23 * Driver for Silicon Laboratories CP2101/CP2102/CP2103/CP2104/CP2105 24 * USB-Serial adapters. Based on datasheet AN571, publicly available from 25 * http://www.silabs.com/Support%20Documents/TechnicalDocs/AN571.pdf 26 */ 27 28 #include <sys/stdint.h> 29 #include <sys/stddef.h> 30 #include <sys/param.h> 31 #include <sys/queue.h> 32 #include <sys/types.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/bus.h> 36 #include <sys/module.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/condvar.h> 40 #include <sys/sysctl.h> 41 #include <sys/sx.h> 42 #include <sys/unistd.h> 43 #include <sys/callout.h> 44 #include <sys/malloc.h> 45 #include <sys/priv.h> 46 47 #include <dev/usb/usb.h> 48 #include <dev/usb/usbdi.h> 49 #include <dev/usb/usbdi_util.h> 50 #include <dev/usb/usb_ioctl.h> 51 #include "usbdevs.h" 52 53 #define USB_DEBUG_VAR uslcom_debug 54 #include <dev/usb/usb_debug.h> 55 #include <dev/usb/usb_process.h> 56 57 #include <dev/usb/serial/usb_serial.h> 58 59 #ifdef USB_DEBUG 60 static int uslcom_debug = 0; 61 62 static SYSCTL_NODE(_hw_usb, OID_AUTO, uslcom, CTLFLAG_RW, 0, "USB uslcom"); 63 SYSCTL_INT(_hw_usb_uslcom, OID_AUTO, debug, CTLFLAG_RW, 64 &uslcom_debug, 0, "Debug level"); 65 #endif 66 67 #define USLCOM_BULK_BUF_SIZE 1024 68 #define USLCOM_CONFIG_INDEX 0 69 70 /* Request types */ 71 #define USLCOM_WRITE 0x41 72 #define USLCOM_READ 0xc1 73 74 /* Request codes */ 75 #define USLCOM_IFC_ENABLE 0x00 76 #define USLCOM_SET_BAUDDIV 0x01 77 #define USLCOM_SET_LINE_CTL 0x03 78 #define USLCOM_SET_BREAK 0x05 79 #define USLCOM_SET_MHS 0x07 80 #define USLCOM_GET_MDMSTS 0x08 81 #define USLCOM_SET_FLOW 0x13 82 #define USLCOM_SET_BAUDRATE 0x1e 83 #define USLCOM_VENDOR_SPECIFIC 0xff 84 85 /* USLCOM_IFC_ENABLE values */ 86 #define USLCOM_IFC_ENABLE_DIS 0x00 87 #define USLCOM_IFC_ENABLE_EN 0x01 88 89 /* USLCOM_SET_MHS/USLCOM_GET_MDMSTS values */ 90 #define USLCOM_MHS_DTR_ON 0x0001 91 #define USLCOM_MHS_DTR_SET 0x0100 92 #define USLCOM_MHS_RTS_ON 0x0002 93 #define USLCOM_MHS_RTS_SET 0x0200 94 #define USLCOM_MHS_CTS 0x0010 95 #define USLCOM_MHS_DSR 0x0020 96 #define USLCOM_MHS_RI 0x0040 97 #define USLCOM_MHS_DCD 0x0080 98 99 /* USLCOM_SET_BAUDDIV values */ 100 #define USLCOM_BAUDDIV_REF 3686400 /* 3.6864 MHz */ 101 102 /* USLCOM_SET_LINE_CTL values */ 103 #define USLCOM_STOP_BITS_1 0x00 104 #define USLCOM_STOP_BITS_2 0x02 105 #define USLCOM_PARITY_NONE 0x00 106 #define USLCOM_PARITY_ODD 0x10 107 #define USLCOM_PARITY_EVEN 0x20 108 #define USLCOM_SET_DATA_BITS(x) ((x) << 8) 109 110 /* USLCOM_SET_BREAK values */ 111 #define USLCOM_SET_BREAK_OFF 0x00 112 #define USLCOM_SET_BREAK_ON 0x01 113 114 /* USLCOM_SET_FLOW values - 1st word */ 115 #define USLCOM_FLOW_DTR_ON 0x00000001 /* DTR static active */ 116 #define USLCOM_FLOW_CTS_HS 0x00000008 /* CTS handshake */ 117 /* USLCOM_SET_FLOW values - 2nd word */ 118 #define USLCOM_FLOW_RTS_ON 0x00000040 /* RTS static active */ 119 #define USLCOM_FLOW_RTS_HS 0x00000080 /* RTS handshake */ 120 121 /* USLCOM_VENDOR_SPECIFIC values */ 122 #define USLCOM_WRITE_LATCH 0x37E1 123 #define USLCOM_READ_LATCH 0x00C2 124 125 enum { 126 USLCOM_BULK_DT_WR, 127 USLCOM_BULK_DT_RD, 128 USLCOM_CTRL_DT_RD, 129 USLCOM_N_TRANSFER, 130 }; 131 132 struct uslcom_softc { 133 struct ucom_super_softc sc_super_ucom; 134 struct ucom_softc sc_ucom; 135 struct usb_callout sc_watchdog; 136 137 struct usb_xfer *sc_xfer[USLCOM_N_TRANSFER]; 138 struct usb_device *sc_udev; 139 struct mtx sc_mtx; 140 141 uint8_t sc_msr; 142 uint8_t sc_lsr; 143 uint8_t sc_iface_no; 144 }; 145 146 static device_probe_t uslcom_probe; 147 static device_attach_t uslcom_attach; 148 static device_detach_t uslcom_detach; 149 static void uslcom_free_softc(struct uslcom_softc *); 150 151 static usb_callback_t uslcom_write_callback; 152 static usb_callback_t uslcom_read_callback; 153 static usb_callback_t uslcom_control_callback; 154 155 static void uslcom_free(struct ucom_softc *); 156 static void uslcom_open(struct ucom_softc *); 157 static void uslcom_close(struct ucom_softc *); 158 static void uslcom_set_dtr(struct ucom_softc *, uint8_t); 159 static void uslcom_set_rts(struct ucom_softc *, uint8_t); 160 static void uslcom_set_break(struct ucom_softc *, uint8_t); 161 static int uslcom_ioctl(struct ucom_softc *, uint32_t, caddr_t, int, 162 struct thread *); 163 static int uslcom_pre_param(struct ucom_softc *, struct termios *); 164 static void uslcom_param(struct ucom_softc *, struct termios *); 165 static void uslcom_get_status(struct ucom_softc *, uint8_t *, uint8_t *); 166 static void uslcom_start_read(struct ucom_softc *); 167 static void uslcom_stop_read(struct ucom_softc *); 168 static void uslcom_start_write(struct ucom_softc *); 169 static void uslcom_stop_write(struct ucom_softc *); 170 static void uslcom_poll(struct ucom_softc *ucom); 171 172 static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = { 173 174 [USLCOM_BULK_DT_WR] = { 175 .type = UE_BULK, 176 .endpoint = UE_ADDR_ANY, 177 .direction = UE_DIR_OUT, 178 .bufsize = USLCOM_BULK_BUF_SIZE, 179 .flags = {.pipe_bof = 1,}, 180 .callback = &uslcom_write_callback, 181 }, 182 183 [USLCOM_BULK_DT_RD] = { 184 .type = UE_BULK, 185 .endpoint = UE_ADDR_ANY, 186 .direction = UE_DIR_IN, 187 .bufsize = USLCOM_BULK_BUF_SIZE, 188 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 189 .callback = &uslcom_read_callback, 190 }, 191 [USLCOM_CTRL_DT_RD] = { 192 .type = UE_CONTROL, 193 .endpoint = 0x00, 194 .direction = UE_DIR_ANY, 195 .bufsize = sizeof(struct usb_device_request) + 8, 196 .flags = {.pipe_bof = 1,}, 197 .callback = &uslcom_control_callback, 198 .timeout = 1000, /* 1 second timeout */ 199 }, 200 }; 201 202 static struct ucom_callback uslcom_callback = { 203 .ucom_cfg_open = &uslcom_open, 204 .ucom_cfg_close = &uslcom_close, 205 .ucom_cfg_get_status = &uslcom_get_status, 206 .ucom_cfg_set_dtr = &uslcom_set_dtr, 207 .ucom_cfg_set_rts = &uslcom_set_rts, 208 .ucom_cfg_set_break = &uslcom_set_break, 209 .ucom_ioctl = &uslcom_ioctl, 210 .ucom_cfg_param = &uslcom_param, 211 .ucom_pre_param = &uslcom_pre_param, 212 .ucom_start_read = &uslcom_start_read, 213 .ucom_stop_read = &uslcom_stop_read, 214 .ucom_start_write = &uslcom_start_write, 215 .ucom_stop_write = &uslcom_stop_write, 216 .ucom_poll = &uslcom_poll, 217 .ucom_free = &uslcom_free, 218 }; 219 220 static const STRUCT_USB_HOST_ID uslcom_devs[] = { 221 #define USLCOM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 222 USLCOM_DEV(BALTECH, CARDREADER), 223 USLCOM_DEV(CLIPSAL, 5000CT2), 224 USLCOM_DEV(CLIPSAL, 5500PACA), 225 USLCOM_DEV(CLIPSAL, 5500PCU), 226 USLCOM_DEV(CLIPSAL, 560884), 227 USLCOM_DEV(CLIPSAL, 5800PC), 228 USLCOM_DEV(CLIPSAL, C5000CT2), 229 USLCOM_DEV(CLIPSAL, L51xx), 230 USLCOM_DEV(DATAAPEX, MULTICOM), 231 USLCOM_DEV(DELL, DW700), 232 USLCOM_DEV(DIGIANSWER, ZIGBEE802154), 233 USLCOM_DEV(DYNASTREAM, ANTDEVBOARD), 234 USLCOM_DEV(DYNASTREAM, ANTDEVBOARD2), 235 USLCOM_DEV(DYNASTREAM, ANT2USB), 236 USLCOM_DEV(ELV, USBI2C), 237 USLCOM_DEV(FESTO, CMSP), 238 USLCOM_DEV(FESTO, CPX_USB), 239 USLCOM_DEV(FOXCONN, PIRELLI_DP_L10), 240 USLCOM_DEV(FOXCONN, TCOM_TC_300), 241 USLCOM_DEV(GEMALTO, PROXPU), 242 USLCOM_DEV(JABLOTRON, PC60B), 243 USLCOM_DEV(KAMSTRUP, OPTICALEYE), 244 USLCOM_DEV(KAMSTRUP, MBUS_250D), 245 USLCOM_DEV(LINKINSTRUMENTS, MSO19), 246 USLCOM_DEV(LINKINSTRUMENTS, MSO28), 247 USLCOM_DEV(LINKINSTRUMENTS, MSO28_2), 248 USLCOM_DEV(MEI, CASHFLOW_SC), 249 USLCOM_DEV(MEI, S2000), 250 USLCOM_DEV(OWEN, AC4), 251 USLCOM_DEV(PHILIPS, ACE1001), 252 USLCOM_DEV(PLX, CA42), 253 USLCOM_DEV(RENESAS, RX610), 254 USLCOM_DEV(SILABS, AC_SERV_CAN), 255 USLCOM_DEV(SILABS, AC_SERV_CIS), 256 USLCOM_DEV(SILABS, AC_SERV_IBUS), 257 USLCOM_DEV(SILABS, AC_SERV_OBD), 258 USLCOM_DEV(SILABS, AEROCOMM), 259 USLCOM_DEV(SILABS, AMBER_AMB2560), 260 USLCOM_DEV(SILABS, ARGUSISP), 261 USLCOM_DEV(SILABS, ARKHAM_DS101_A), 262 USLCOM_DEV(SILABS, ARKHAM_DS101_M), 263 USLCOM_DEV(SILABS, ARYGON_MIFARE), 264 USLCOM_DEV(SILABS, AVIT_USB_TTL), 265 USLCOM_DEV(SILABS, B_G_H3000), 266 USLCOM_DEV(SILABS, BALLUFF_RFID), 267 USLCOM_DEV(SILABS, BEI_VCP), 268 USLCOM_DEV(SILABS, BSM7DUSB), 269 USLCOM_DEV(SILABS, BURNSIDE), 270 USLCOM_DEV(SILABS, C2_EDGE_MODEM), 271 USLCOM_DEV(SILABS, CP2102), 272 USLCOM_DEV(SILABS, CP210X_2), 273 USLCOM_DEV(SILABS, CP210X_3), 274 USLCOM_DEV(SILABS, CP210X_4), 275 USLCOM_DEV(SILABS, CRUMB128), 276 USLCOM_DEV(SILABS, CYGNAL), 277 USLCOM_DEV(SILABS, CYGNAL_DEBUG), 278 USLCOM_DEV(SILABS, CYGNAL_GPS), 279 USLCOM_DEV(SILABS, DEGREE), 280 USLCOM_DEV(SILABS, DEKTEK_DTAPLUS), 281 USLCOM_DEV(SILABS, EMS_C1007), 282 USLCOM_DEV(SILABS, HAMLINKUSB), 283 USLCOM_DEV(SILABS, HELICOM), 284 USLCOM_DEV(SILABS, IMS_USB_RS422), 285 USLCOM_DEV(SILABS, INFINITY_MIC), 286 USLCOM_DEV(SILABS, INSYS_MODEM), 287 USLCOM_DEV(SILABS, IRZ_SG10), 288 USLCOM_DEV(SILABS, KYOCERA_GPS), 289 USLCOM_DEV(SILABS, LIPOWSKY_HARP), 290 USLCOM_DEV(SILABS, LIPOWSKY_JTAG), 291 USLCOM_DEV(SILABS, LIPOWSKY_LIN), 292 USLCOM_DEV(SILABS, MC35PU), 293 USLCOM_DEV(SILABS, MJS_TOSLINK), 294 USLCOM_DEV(SILABS, MSD_DASHHAWK), 295 USLCOM_DEV(SILABS, MULTIPLEX_RC), 296 USLCOM_DEV(SILABS, OPTRIS_MSPRO), 297 USLCOM_DEV(SILABS, POLOLU), 298 USLCOM_DEV(SILABS, PROCYON_AVS), 299 USLCOM_DEV(SILABS, SB_PARAMOUNT_ME), 300 USLCOM_DEV(SILABS, SUUNTO), 301 USLCOM_DEV(SILABS, TAMSMASTER), 302 USLCOM_DEV(SILABS, TELEGESIS_ETRX2), 303 USLCOM_DEV(SILABS, TRACIENT), 304 USLCOM_DEV(SILABS, TRAQMATE), 305 USLCOM_DEV(SILABS, USBCOUNT50), 306 USLCOM_DEV(SILABS, USBPULSE100), 307 USLCOM_DEV(SILABS, USBSCOPE50), 308 USLCOM_DEV(SILABS, USBWAVE12), 309 USLCOM_DEV(SILABS, VSTABI), 310 USLCOM_DEV(SILABS, WAVIT), 311 USLCOM_DEV(SILABS, WMRBATT), 312 USLCOM_DEV(SILABS, WMRRIGBLASTER), 313 USLCOM_DEV(SILABS, WMRRIGTALK), 314 USLCOM_DEV(SILABS, ZEPHYR_BIO), 315 USLCOM_DEV(SILABS2, DCU11CLONE), 316 USLCOM_DEV(SILABS3, GPRS_MODEM), 317 USLCOM_DEV(SILABS4, 100EU_MODEM), 318 USLCOM_DEV(SYNTECH, CYPHERLAB100), 319 USLCOM_DEV(USI, MC60), 320 USLCOM_DEV(VAISALA, CABLE), 321 USLCOM_DEV(WAGO, SERVICECABLE), 322 USLCOM_DEV(WAVESENSE, JAZZ), 323 USLCOM_DEV(WIENERPLEINBAUS, PL512), 324 USLCOM_DEV(WIENERPLEINBAUS, RCM), 325 USLCOM_DEV(WIENERPLEINBAUS, MPOD), 326 USLCOM_DEV(WIENERPLEINBAUS, CML), 327 #undef USLCOM_DEV 328 }; 329 330 static device_method_t uslcom_methods[] = { 331 DEVMETHOD(device_probe, uslcom_probe), 332 DEVMETHOD(device_attach, uslcom_attach), 333 DEVMETHOD(device_detach, uslcom_detach), 334 DEVMETHOD_END 335 }; 336 337 static devclass_t uslcom_devclass; 338 339 static driver_t uslcom_driver = { 340 .name = "uslcom", 341 .methods = uslcom_methods, 342 .size = sizeof(struct uslcom_softc), 343 }; 344 345 DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0); 346 MODULE_DEPEND(uslcom, ucom, 1, 1, 1); 347 MODULE_DEPEND(uslcom, usb, 1, 1, 1); 348 MODULE_VERSION(uslcom, 1); 349 350 static void 351 uslcom_watchdog(void *arg) 352 { 353 struct uslcom_softc *sc = arg; 354 355 mtx_assert(&sc->sc_mtx, MA_OWNED); 356 357 usbd_transfer_start(sc->sc_xfer[USLCOM_CTRL_DT_RD]); 358 359 usb_callout_reset(&sc->sc_watchdog, 360 hz / 4, &uslcom_watchdog, sc); 361 } 362 363 static int 364 uslcom_probe(device_t dev) 365 { 366 struct usb_attach_arg *uaa = device_get_ivars(dev); 367 368 DPRINTFN(11, "\n"); 369 370 if (uaa->usb_mode != USB_MODE_HOST) { 371 return (ENXIO); 372 } 373 if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) { 374 return (ENXIO); 375 } 376 return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa)); 377 } 378 379 static int 380 uslcom_attach(device_t dev) 381 { 382 struct usb_attach_arg *uaa = device_get_ivars(dev); 383 struct uslcom_softc *sc = device_get_softc(dev); 384 int error; 385 386 DPRINTFN(11, "\n"); 387 388 device_set_usb_desc(dev); 389 mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF); 390 ucom_ref(&sc->sc_super_ucom); 391 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 392 393 sc->sc_udev = uaa->device; 394 /* use the interface number from the USB interface descriptor */ 395 sc->sc_iface_no = uaa->info.bIfaceNum; 396 397 error = usbd_transfer_setup(uaa->device, 398 &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config, 399 USLCOM_N_TRANSFER, sc, &sc->sc_mtx); 400 if (error) { 401 DPRINTF("one or more missing USB endpoints, " 402 "error=%s\n", usbd_errstr(error)); 403 goto detach; 404 } 405 /* clear stall at first run */ 406 mtx_lock(&sc->sc_mtx); 407 usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]); 408 usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]); 409 mtx_unlock(&sc->sc_mtx); 410 411 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 412 &uslcom_callback, &sc->sc_mtx); 413 if (error) { 414 goto detach; 415 } 416 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 417 418 return (0); 419 420 detach: 421 uslcom_detach(dev); 422 return (ENXIO); 423 } 424 425 static int 426 uslcom_detach(device_t dev) 427 { 428 struct uslcom_softc *sc = device_get_softc(dev); 429 430 DPRINTF("sc=%p\n", sc); 431 432 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 433 usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER); 434 435 usb_callout_drain(&sc->sc_watchdog); 436 437 device_claim_softc(dev); 438 439 uslcom_free_softc(sc); 440 441 return (0); 442 } 443 444 UCOM_UNLOAD_DRAIN(uslcom); 445 446 static void 447 uslcom_free_softc(struct uslcom_softc *sc) 448 { 449 if (ucom_unref(&sc->sc_super_ucom)) { 450 mtx_destroy(&sc->sc_mtx); 451 device_free_softc(sc); 452 } 453 } 454 455 static void 456 uslcom_free(struct ucom_softc *ucom) 457 { 458 uslcom_free_softc(ucom->sc_parent); 459 } 460 461 static void 462 uslcom_open(struct ucom_softc *ucom) 463 { 464 struct uslcom_softc *sc = ucom->sc_parent; 465 struct usb_device_request req; 466 467 req.bmRequestType = USLCOM_WRITE; 468 req.bRequest = USLCOM_IFC_ENABLE; 469 USETW(req.wValue, USLCOM_IFC_ENABLE_EN); 470 USETW(req.wIndex, sc->sc_iface_no); 471 USETW(req.wLength, 0); 472 473 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 474 &req, NULL, 0, 1000)) { 475 DPRINTF("UART enable failed (ignored)\n"); 476 } 477 478 /* start polling status */ 479 uslcom_watchdog(sc); 480 } 481 482 static void 483 uslcom_close(struct ucom_softc *ucom) 484 { 485 struct uslcom_softc *sc = ucom->sc_parent; 486 struct usb_device_request req; 487 488 /* stop polling status */ 489 usb_callout_stop(&sc->sc_watchdog); 490 491 req.bmRequestType = USLCOM_WRITE; 492 req.bRequest = USLCOM_IFC_ENABLE; 493 USETW(req.wValue, USLCOM_IFC_ENABLE_DIS); 494 USETW(req.wIndex, sc->sc_iface_no); 495 USETW(req.wLength, 0); 496 497 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 498 &req, NULL, 0, 1000)) { 499 DPRINTF("UART disable failed (ignored)\n"); 500 } 501 } 502 503 static void 504 uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 505 { 506 struct uslcom_softc *sc = ucom->sc_parent; 507 struct usb_device_request req; 508 uint16_t ctl; 509 510 DPRINTF("onoff = %d\n", onoff); 511 512 ctl = onoff ? USLCOM_MHS_DTR_ON : 0; 513 ctl |= USLCOM_MHS_DTR_SET; 514 515 req.bmRequestType = USLCOM_WRITE; 516 req.bRequest = USLCOM_SET_MHS; 517 USETW(req.wValue, ctl); 518 USETW(req.wIndex, sc->sc_iface_no); 519 USETW(req.wLength, 0); 520 521 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 522 &req, NULL, 0, 1000)) { 523 DPRINTF("Setting DTR failed (ignored)\n"); 524 } 525 } 526 527 static void 528 uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff) 529 { 530 struct uslcom_softc *sc = ucom->sc_parent; 531 struct usb_device_request req; 532 uint16_t ctl; 533 534 DPRINTF("onoff = %d\n", onoff); 535 536 ctl = onoff ? USLCOM_MHS_RTS_ON : 0; 537 ctl |= USLCOM_MHS_RTS_SET; 538 539 req.bmRequestType = USLCOM_WRITE; 540 req.bRequest = USLCOM_SET_MHS; 541 USETW(req.wValue, ctl); 542 USETW(req.wIndex, sc->sc_iface_no); 543 USETW(req.wLength, 0); 544 545 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 546 &req, NULL, 0, 1000)) { 547 DPRINTF("Setting DTR failed (ignored)\n"); 548 } 549 } 550 551 static int 552 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t) 553 { 554 if (t->c_ospeed <= 0 || t->c_ospeed > 921600) 555 return (EINVAL); 556 return (0); 557 } 558 559 static void 560 uslcom_param(struct ucom_softc *ucom, struct termios *t) 561 { 562 struct uslcom_softc *sc = ucom->sc_parent; 563 struct usb_device_request req; 564 uint32_t baudrate, flowctrl[4]; 565 uint16_t data; 566 567 DPRINTF("\n"); 568 569 baudrate = t->c_ospeed; 570 req.bmRequestType = USLCOM_WRITE; 571 req.bRequest = USLCOM_SET_BAUDRATE; 572 USETW(req.wValue, 0); 573 USETW(req.wIndex, sc->sc_iface_no); 574 USETW(req.wLength, sizeof(baudrate)); 575 576 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 577 &req, &baudrate, 0, 1000)) { 578 DPRINTF("Set baudrate failed (ignored)\n"); 579 } 580 581 if (t->c_cflag & CSTOPB) 582 data = USLCOM_STOP_BITS_2; 583 else 584 data = USLCOM_STOP_BITS_1; 585 if (t->c_cflag & PARENB) { 586 if (t->c_cflag & PARODD) 587 data |= USLCOM_PARITY_ODD; 588 else 589 data |= USLCOM_PARITY_EVEN; 590 } else 591 data |= USLCOM_PARITY_NONE; 592 switch (t->c_cflag & CSIZE) { 593 case CS5: 594 data |= USLCOM_SET_DATA_BITS(5); 595 break; 596 case CS6: 597 data |= USLCOM_SET_DATA_BITS(6); 598 break; 599 case CS7: 600 data |= USLCOM_SET_DATA_BITS(7); 601 break; 602 case CS8: 603 data |= USLCOM_SET_DATA_BITS(8); 604 break; 605 } 606 607 req.bmRequestType = USLCOM_WRITE; 608 req.bRequest = USLCOM_SET_LINE_CTL; 609 USETW(req.wValue, data); 610 USETW(req.wIndex, sc->sc_iface_no); 611 USETW(req.wLength, 0); 612 613 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 614 &req, NULL, 0, 1000)) { 615 DPRINTF("Set format failed (ignored)\n"); 616 } 617 618 if (t->c_cflag & CRTSCTS) { 619 flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON | USLCOM_FLOW_CTS_HS); 620 flowctrl[1] = htole32(USLCOM_FLOW_RTS_HS); 621 } else { 622 flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON); 623 flowctrl[1] = htole32(USLCOM_FLOW_RTS_ON); 624 } 625 flowctrl[2] = 0; 626 flowctrl[3] = 0; 627 req.bmRequestType = USLCOM_WRITE; 628 req.bRequest = USLCOM_SET_FLOW; 629 USETW(req.wValue, 0); 630 USETW(req.wIndex, sc->sc_iface_no); 631 USETW(req.wLength, sizeof(flowctrl)); 632 633 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 634 &req, flowctrl, 0, 1000)) { 635 DPRINTF("Set flowcontrol failed (ignored)\n"); 636 } 637 } 638 639 static void 640 uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 641 { 642 struct uslcom_softc *sc = ucom->sc_parent; 643 644 DPRINTF("\n"); 645 646 *lsr = sc->sc_lsr; 647 *msr = sc->sc_msr; 648 } 649 650 static void 651 uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff) 652 { 653 struct uslcom_softc *sc = ucom->sc_parent; 654 struct usb_device_request req; 655 uint16_t brk = onoff ? USLCOM_SET_BREAK_ON : USLCOM_SET_BREAK_OFF; 656 657 req.bmRequestType = USLCOM_WRITE; 658 req.bRequest = USLCOM_SET_BREAK; 659 USETW(req.wValue, brk); 660 USETW(req.wIndex, sc->sc_iface_no); 661 USETW(req.wLength, 0); 662 663 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 664 &req, NULL, 0, 1000)) { 665 DPRINTF("Set BREAK failed (ignored)\n"); 666 } 667 } 668 669 static int 670 uslcom_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data, 671 int flag, struct thread *td) 672 { 673 struct uslcom_softc *sc = ucom->sc_parent; 674 struct usb_device_request req; 675 int error = 0; 676 uint8_t latch; 677 678 DPRINTF("cmd=0x%08x\n", cmd); 679 680 switch (cmd) { 681 case USB_GET_GPIO: 682 req.bmRequestType = USLCOM_READ; 683 req.bRequest = USLCOM_VENDOR_SPECIFIC; 684 USETW(req.wValue, USLCOM_READ_LATCH); 685 USETW(req.wIndex, 0); 686 USETW(req.wLength, sizeof(latch)); 687 688 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 689 &req, &latch, 0, 1000)) { 690 DPRINTF("Get LATCH failed\n"); 691 error = EIO; 692 } 693 *(int *)data = latch; 694 break; 695 696 case USB_SET_GPIO: 697 req.bmRequestType = USLCOM_WRITE; 698 req.bRequest = USLCOM_VENDOR_SPECIFIC; 699 USETW(req.wValue, USLCOM_WRITE_LATCH); 700 USETW(req.wIndex, (*(int *)data)); 701 USETW(req.wLength, 0); 702 703 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 704 &req, NULL, 0, 1000)) { 705 DPRINTF("Set LATCH failed\n"); 706 error = EIO; 707 } 708 break; 709 710 default: 711 DPRINTF("Unknown IOCTL\n"); 712 error = ENOIOCTL; 713 break; 714 } 715 return (error); 716 } 717 718 static void 719 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error) 720 { 721 struct uslcom_softc *sc = usbd_xfer_softc(xfer); 722 struct usb_page_cache *pc; 723 uint32_t actlen; 724 725 switch (USB_GET_STATE(xfer)) { 726 case USB_ST_SETUP: 727 case USB_ST_TRANSFERRED: 728 tr_setup: 729 pc = usbd_xfer_get_frame(xfer, 0); 730 if (ucom_get_data(&sc->sc_ucom, pc, 0, 731 USLCOM_BULK_BUF_SIZE, &actlen)) { 732 733 DPRINTF("actlen = %d\n", actlen); 734 735 usbd_xfer_set_frame_len(xfer, 0, actlen); 736 usbd_transfer_submit(xfer); 737 } 738 return; 739 740 default: /* Error */ 741 if (error != USB_ERR_CANCELLED) { 742 /* try to clear stall first */ 743 usbd_xfer_set_stall(xfer); 744 goto tr_setup; 745 } 746 return; 747 } 748 } 749 750 static void 751 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error) 752 { 753 struct uslcom_softc *sc = usbd_xfer_softc(xfer); 754 struct usb_page_cache *pc; 755 int actlen; 756 757 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 758 759 switch (USB_GET_STATE(xfer)) { 760 case USB_ST_TRANSFERRED: 761 pc = usbd_xfer_get_frame(xfer, 0); 762 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 763 764 case USB_ST_SETUP: 765 tr_setup: 766 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 767 usbd_transfer_submit(xfer); 768 return; 769 770 default: /* Error */ 771 if (error != USB_ERR_CANCELLED) { 772 /* try to clear stall first */ 773 usbd_xfer_set_stall(xfer); 774 goto tr_setup; 775 } 776 return; 777 } 778 } 779 780 static void 781 uslcom_control_callback(struct usb_xfer *xfer, usb_error_t error) 782 { 783 struct uslcom_softc *sc = usbd_xfer_softc(xfer); 784 struct usb_page_cache *pc; 785 struct usb_device_request req; 786 uint8_t msr = 0; 787 uint8_t buf; 788 789 switch (USB_GET_STATE(xfer)) { 790 case USB_ST_TRANSFERRED: 791 pc = usbd_xfer_get_frame(xfer, 1); 792 usbd_copy_out(pc, 0, &buf, sizeof(buf)); 793 if (buf & USLCOM_MHS_CTS) 794 msr |= SER_CTS; 795 if (buf & USLCOM_MHS_DSR) 796 msr |= SER_DSR; 797 if (buf & USLCOM_MHS_RI) 798 msr |= SER_RI; 799 if (buf & USLCOM_MHS_DCD) 800 msr |= SER_DCD; 801 802 if (msr != sc->sc_msr) { 803 DPRINTF("status change msr=0x%02x " 804 "(was 0x%02x)\n", msr, sc->sc_msr); 805 sc->sc_msr = msr; 806 ucom_status_change(&sc->sc_ucom); 807 } 808 break; 809 810 case USB_ST_SETUP: 811 req.bmRequestType = USLCOM_READ; 812 req.bRequest = USLCOM_GET_MDMSTS; 813 USETW(req.wValue, 0); 814 USETW(req.wIndex, sc->sc_iface_no); 815 USETW(req.wLength, sizeof(buf)); 816 817 usbd_xfer_set_frames(xfer, 2); 818 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 819 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf)); 820 821 pc = usbd_xfer_get_frame(xfer, 0); 822 usbd_copy_in(pc, 0, &req, sizeof(req)); 823 usbd_transfer_submit(xfer); 824 break; 825 826 default: /* error */ 827 if (error != USB_ERR_CANCELLED) 828 DPRINTF("error=%s\n", usbd_errstr(error)); 829 break; 830 } 831 } 832 833 static void 834 uslcom_start_read(struct ucom_softc *ucom) 835 { 836 struct uslcom_softc *sc = ucom->sc_parent; 837 838 /* start read endpoint */ 839 usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]); 840 } 841 842 static void 843 uslcom_stop_read(struct ucom_softc *ucom) 844 { 845 struct uslcom_softc *sc = ucom->sc_parent; 846 847 /* stop read endpoint */ 848 usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]); 849 } 850 851 static void 852 uslcom_start_write(struct ucom_softc *ucom) 853 { 854 struct uslcom_softc *sc = ucom->sc_parent; 855 856 usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]); 857 } 858 859 static void 860 uslcom_stop_write(struct ucom_softc *ucom) 861 { 862 struct uslcom_softc *sc = ucom->sc_parent; 863 864 usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]); 865 } 866 867 static void 868 uslcom_poll(struct ucom_softc *ucom) 869 { 870 struct uslcom_softc *sc = ucom->sc_parent; 871 usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER); 872 } 873