1 /*- 2 * Copyright (c) 2004 Bernd Walter <ticso@FreeBSD.org> 3 * 4 * $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $ 5 * $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $ 6 * $Author: ticso $ 7 * $Rev: 1127 $ 8 */ 9 10 /*- 11 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>. 12 * All rights reserved. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 /*- 37 * Copyright (c) 2000 The NetBSD Foundation, Inc. 38 * All rights reserved. 39 * 40 * This code is derived from software contributed to The NetBSD Foundation 41 * by Lennart Augustsson (lennart@augustsson.net). 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 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 * BWCT serial adapter driver 70 */ 71 72 #include <sys/stdint.h> 73 #include <sys/stddef.h> 74 #include <sys/param.h> 75 #include <sys/queue.h> 76 #include <sys/types.h> 77 #include <sys/systm.h> 78 #include <sys/kernel.h> 79 #include <sys/bus.h> 80 #include <sys/module.h> 81 #include <sys/lock.h> 82 #include <sys/mutex.h> 83 #include <sys/condvar.h> 84 #include <sys/sysctl.h> 85 #include <sys/sx.h> 86 #include <sys/unistd.h> 87 #include <sys/callout.h> 88 #include <sys/malloc.h> 89 #include <sys/priv.h> 90 91 #include <dev/usb/usb.h> 92 #include <dev/usb/usbdi.h> 93 #include <dev/usb/usbdi_util.h> 94 #include "usbdevs.h" 95 96 #define USB_DEBUG_VAR ubser_debug 97 #include <dev/usb/usb_debug.h> 98 #include <dev/usb/usb_process.h> 99 100 #include <dev/usb/serial/usb_serial.h> 101 102 #define UBSER_UNIT_MAX 32 103 104 /* Vendor Interface Requests */ 105 #define VENDOR_GET_NUMSER 0x01 106 #define VENDOR_SET_BREAK 0x02 107 #define VENDOR_CLEAR_BREAK 0x03 108 109 #ifdef USB_DEBUG 110 static int ubser_debug = 0; 111 112 static SYSCTL_NODE(_hw_usb, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser"); 113 SYSCTL_INT(_hw_usb_ubser, OID_AUTO, debug, CTLFLAG_RW, 114 &ubser_debug, 0, "ubser debug level"); 115 #endif 116 117 enum { 118 UBSER_BULK_DT_WR, 119 UBSER_BULK_DT_RD, 120 UBSER_N_TRANSFER, 121 }; 122 123 struct ubser_softc { 124 struct ucom_super_softc sc_super_ucom; 125 struct ucom_softc sc_ucom[UBSER_UNIT_MAX]; 126 127 struct usb_xfer *sc_xfer[UBSER_N_TRANSFER]; 128 struct usb_device *sc_udev; 129 struct mtx sc_mtx; 130 131 uint16_t sc_tx_size; 132 133 uint8_t sc_numser; 134 uint8_t sc_iface_no; 135 uint8_t sc_iface_index; 136 uint8_t sc_curr_tx_unit; 137 }; 138 139 /* prototypes */ 140 141 static device_probe_t ubser_probe; 142 static device_attach_t ubser_attach; 143 static device_detach_t ubser_detach; 144 static void ubser_free_softc(struct ubser_softc *); 145 146 static usb_callback_t ubser_write_callback; 147 static usb_callback_t ubser_read_callback; 148 149 static void ubser_free(struct ucom_softc *); 150 static int ubser_pre_param(struct ucom_softc *, struct termios *); 151 static void ubser_cfg_set_break(struct ucom_softc *, uint8_t); 152 static void ubser_cfg_get_status(struct ucom_softc *, uint8_t *, 153 uint8_t *); 154 static void ubser_start_read(struct ucom_softc *); 155 static void ubser_stop_read(struct ucom_softc *); 156 static void ubser_start_write(struct ucom_softc *); 157 static void ubser_stop_write(struct ucom_softc *); 158 static void ubser_poll(struct ucom_softc *ucom); 159 160 static const struct usb_config ubser_config[UBSER_N_TRANSFER] = { 161 162 [UBSER_BULK_DT_WR] = { 163 .type = UE_BULK, 164 .endpoint = UE_ADDR_ANY, 165 .direction = UE_DIR_OUT, 166 .bufsize = 0, /* use wMaxPacketSize */ 167 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 168 .callback = &ubser_write_callback, 169 }, 170 171 [UBSER_BULK_DT_RD] = { 172 .type = UE_BULK, 173 .endpoint = UE_ADDR_ANY, 174 .direction = UE_DIR_IN, 175 .bufsize = 0, /* use wMaxPacketSize */ 176 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 177 .callback = &ubser_read_callback, 178 }, 179 }; 180 181 static const struct ucom_callback ubser_callback = { 182 .ucom_cfg_set_break = &ubser_cfg_set_break, 183 .ucom_cfg_get_status = &ubser_cfg_get_status, 184 .ucom_pre_param = &ubser_pre_param, 185 .ucom_start_read = &ubser_start_read, 186 .ucom_stop_read = &ubser_stop_read, 187 .ucom_start_write = &ubser_start_write, 188 .ucom_stop_write = &ubser_stop_write, 189 .ucom_poll = &ubser_poll, 190 .ucom_free = &ubser_free, 191 }; 192 193 static device_method_t ubser_methods[] = { 194 DEVMETHOD(device_probe, ubser_probe), 195 DEVMETHOD(device_attach, ubser_attach), 196 DEVMETHOD(device_detach, ubser_detach), 197 DEVMETHOD_END 198 }; 199 200 static devclass_t ubser_devclass; 201 202 static driver_t ubser_driver = { 203 .name = "ubser", 204 .methods = ubser_methods, 205 .size = sizeof(struct ubser_softc), 206 }; 207 208 DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, NULL, 0); 209 MODULE_DEPEND(ubser, ucom, 1, 1, 1); 210 MODULE_DEPEND(ubser, usb, 1, 1, 1); 211 MODULE_VERSION(ubser, 1); 212 213 static int 214 ubser_probe(device_t dev) 215 { 216 struct usb_attach_arg *uaa = device_get_ivars(dev); 217 218 if (uaa->usb_mode != USB_MODE_HOST) { 219 return (ENXIO); 220 } 221 /* check if this is a BWCT vendor specific ubser interface */ 222 if ((strcmp(usb_get_manufacturer(uaa->device), "BWCT") == 0) && 223 (uaa->info.bInterfaceClass == 0xff) && 224 (uaa->info.bInterfaceSubClass == 0x00)) 225 return (0); 226 227 return (ENXIO); 228 } 229 230 static int 231 ubser_attach(device_t dev) 232 { 233 struct usb_attach_arg *uaa = device_get_ivars(dev); 234 struct ubser_softc *sc = device_get_softc(dev); 235 struct usb_device_request req; 236 uint8_t n; 237 int error; 238 239 device_set_usb_desc(dev); 240 mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF); 241 ucom_ref(&sc->sc_super_ucom); 242 243 sc->sc_iface_no = uaa->info.bIfaceNum; 244 sc->sc_iface_index = uaa->info.bIfaceIndex; 245 sc->sc_udev = uaa->device; 246 247 /* get number of serials */ 248 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 249 req.bRequest = VENDOR_GET_NUMSER; 250 USETW(req.wValue, 0); 251 req.wIndex[0] = sc->sc_iface_no; 252 req.wIndex[1] = 0; 253 USETW(req.wLength, 1); 254 error = usbd_do_request_flags(uaa->device, NULL, 255 &req, &sc->sc_numser, 256 0, NULL, USB_DEFAULT_TIMEOUT); 257 258 if (error || (sc->sc_numser == 0)) { 259 device_printf(dev, "failed to get number " 260 "of serial ports: %s\n", 261 usbd_errstr(error)); 262 goto detach; 263 } 264 if (sc->sc_numser > UBSER_UNIT_MAX) 265 sc->sc_numser = UBSER_UNIT_MAX; 266 267 device_printf(dev, "found %i serials\n", sc->sc_numser); 268 269 error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index, 270 sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx); 271 if (error) { 272 goto detach; 273 } 274 sc->sc_tx_size = usbd_xfer_max_len(sc->sc_xfer[UBSER_BULK_DT_WR]); 275 276 if (sc->sc_tx_size == 0) { 277 DPRINTFN(0, "invalid tx_size\n"); 278 goto detach; 279 } 280 /* initialize port numbers */ 281 282 for (n = 0; n < sc->sc_numser; n++) { 283 sc->sc_ucom[n].sc_portno = n; 284 } 285 286 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 287 sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx); 288 if (error) { 289 goto detach; 290 } 291 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 292 293 mtx_lock(&sc->sc_mtx); 294 usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]); 295 usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]); 296 usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]); 297 mtx_unlock(&sc->sc_mtx); 298 299 return (0); /* success */ 300 301 detach: 302 ubser_detach(dev); 303 return (ENXIO); /* failure */ 304 } 305 306 static int 307 ubser_detach(device_t dev) 308 { 309 struct ubser_softc *sc = device_get_softc(dev); 310 311 DPRINTF("\n"); 312 313 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom); 314 usbd_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER); 315 316 device_claim_softc(dev); 317 318 ubser_free_softc(sc); 319 320 return (0); 321 } 322 323 UCOM_UNLOAD_DRAIN(ubser); 324 325 static void 326 ubser_free_softc(struct ubser_softc *sc) 327 { 328 if (ucom_unref(&sc->sc_super_ucom)) { 329 mtx_destroy(&sc->sc_mtx); 330 device_free_softc(sc); 331 } 332 } 333 334 static void 335 ubser_free(struct ucom_softc *ucom) 336 { 337 ubser_free_softc(ucom->sc_parent); 338 } 339 340 static int 341 ubser_pre_param(struct ucom_softc *ucom, struct termios *t) 342 { 343 DPRINTF("\n"); 344 345 /* 346 * The firmware on our devices can only do 8n1@9600bps 347 * without handshake. 348 * We refuse to accept other configurations. 349 */ 350 351 /* ensure 9600bps */ 352 switch (t->c_ospeed) { 353 case 9600: 354 break; 355 default: 356 return (EINVAL); 357 } 358 359 /* 2 stop bits not possible */ 360 if (t->c_cflag & CSTOPB) 361 return (EINVAL); 362 363 /* XXX parity handling not possible with current firmware */ 364 if (t->c_cflag & PARENB) 365 return (EINVAL); 366 367 /* we can only do 8 data bits */ 368 switch (t->c_cflag & CSIZE) { 369 case CS8: 370 break; 371 default: 372 return (EINVAL); 373 } 374 375 /* we can't do any kind of hardware handshaking */ 376 if ((t->c_cflag & 377 (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0) 378 return (EINVAL); 379 380 /* 381 * XXX xon/xoff not supported by the firmware! 382 * This is handled within FreeBSD only and may overflow buffers 383 * because of delayed reaction due to device buffering. 384 */ 385 386 return (0); 387 } 388 389 static __inline void 390 ubser_inc_tx_unit(struct ubser_softc *sc) 391 { 392 sc->sc_curr_tx_unit++; 393 if (sc->sc_curr_tx_unit >= sc->sc_numser) { 394 sc->sc_curr_tx_unit = 0; 395 } 396 } 397 398 static void 399 ubser_write_callback(struct usb_xfer *xfer, usb_error_t error) 400 { 401 struct ubser_softc *sc = usbd_xfer_softc(xfer); 402 struct usb_page_cache *pc; 403 uint8_t buf[1]; 404 uint8_t first_unit = sc->sc_curr_tx_unit; 405 uint32_t actlen; 406 407 switch (USB_GET_STATE(xfer)) { 408 case USB_ST_SETUP: 409 case USB_ST_TRANSFERRED: 410 tr_setup: 411 pc = usbd_xfer_get_frame(xfer, 0); 412 do { 413 if (ucom_get_data(sc->sc_ucom + sc->sc_curr_tx_unit, 414 pc, 1, sc->sc_tx_size - 1, 415 &actlen)) { 416 417 buf[0] = sc->sc_curr_tx_unit; 418 419 usbd_copy_in(pc, 0, buf, 1); 420 421 usbd_xfer_set_frame_len(xfer, 0, actlen + 1); 422 usbd_transfer_submit(xfer); 423 424 ubser_inc_tx_unit(sc); /* round robin */ 425 426 break; 427 } 428 ubser_inc_tx_unit(sc); 429 430 } while (sc->sc_curr_tx_unit != first_unit); 431 432 return; 433 434 default: /* Error */ 435 if (error != USB_ERR_CANCELLED) { 436 /* try to clear stall first */ 437 usbd_xfer_set_stall(xfer); 438 goto tr_setup; 439 } 440 return; 441 442 } 443 } 444 445 static void 446 ubser_read_callback(struct usb_xfer *xfer, usb_error_t error) 447 { 448 struct ubser_softc *sc = usbd_xfer_softc(xfer); 449 struct usb_page_cache *pc; 450 uint8_t buf[1]; 451 int actlen; 452 453 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 454 455 switch (USB_GET_STATE(xfer)) { 456 case USB_ST_TRANSFERRED: 457 if (actlen < 1) { 458 DPRINTF("invalid actlen=0!\n"); 459 goto tr_setup; 460 } 461 pc = usbd_xfer_get_frame(xfer, 0); 462 usbd_copy_out(pc, 0, buf, 1); 463 464 if (buf[0] >= sc->sc_numser) { 465 DPRINTF("invalid serial number!\n"); 466 goto tr_setup; 467 } 468 ucom_put_data(sc->sc_ucom + buf[0], pc, 1, actlen - 1); 469 470 case USB_ST_SETUP: 471 tr_setup: 472 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 473 usbd_transfer_submit(xfer); 474 return; 475 476 default: /* Error */ 477 if (error != USB_ERR_CANCELLED) { 478 /* try to clear stall first */ 479 usbd_xfer_set_stall(xfer); 480 goto tr_setup; 481 } 482 return; 483 484 } 485 } 486 487 static void 488 ubser_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 489 { 490 struct ubser_softc *sc = ucom->sc_parent; 491 uint8_t x = ucom->sc_portno; 492 struct usb_device_request req; 493 usb_error_t err; 494 495 if (onoff) { 496 497 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 498 req.bRequest = VENDOR_SET_BREAK; 499 req.wValue[0] = x; 500 req.wValue[1] = 0; 501 req.wIndex[0] = sc->sc_iface_no; 502 req.wIndex[1] = 0; 503 USETW(req.wLength, 0); 504 505 err = ucom_cfg_do_request(sc->sc_udev, ucom, 506 &req, NULL, 0, 1000); 507 if (err) { 508 DPRINTFN(0, "send break failed, error=%s\n", 509 usbd_errstr(err)); 510 } 511 } 512 } 513 514 static void 515 ubser_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 516 { 517 /* fake status bits */ 518 *lsr = 0; 519 *msr = SER_DCD; 520 } 521 522 static void 523 ubser_start_read(struct ucom_softc *ucom) 524 { 525 struct ubser_softc *sc = ucom->sc_parent; 526 527 usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]); 528 } 529 530 static void 531 ubser_stop_read(struct ucom_softc *ucom) 532 { 533 struct ubser_softc *sc = ucom->sc_parent; 534 535 usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]); 536 } 537 538 static void 539 ubser_start_write(struct ucom_softc *ucom) 540 { 541 struct ubser_softc *sc = ucom->sc_parent; 542 543 usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]); 544 } 545 546 static void 547 ubser_stop_write(struct ucom_softc *ucom) 548 { 549 struct ubser_softc *sc = ucom->sc_parent; 550 551 usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]); 552 } 553 554 static void 555 ubser_poll(struct ucom_softc *ucom) 556 { 557 struct ubser_softc *sc = ucom->sc_parent; 558 usbd_transfer_poll(sc->sc_xfer, UBSER_N_TRANSFER); 559 } 560