ubser.c revision 184610
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 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the NetBSD 54 * Foundation, Inc. and its contributors. 55 * 4. Neither the name of The NetBSD Foundation nor the names of its 56 * contributors may be used to endorse or promote products derived 57 * from this software without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 69 * POSSIBILITY OF SUCH DAMAGE. 70 */ 71 72#include <sys/cdefs.h> 73__FBSDID("$FreeBSD: head/sys/dev/usb2/serial/ubser2.c 184610 2008-11-04 02:31:03Z alfred $"); 74 75/* 76 * BWCT serial adapter driver 77 */ 78 79#include <dev/usb2/include/usb2_standard.h> 80#include <dev/usb2/include/usb2_mfunc.h> 81#include <dev/usb2/include/usb2_error.h> 82#include <dev/usb2/include/usb2_cdc.h> 83#include <dev/usb2/include/usb2_defs.h> 84 85#define USB_DEBUG_VAR ubser_debug 86 87#include <dev/usb2/core/usb2_core.h> 88#include <dev/usb2/core/usb2_debug.h> 89#include <dev/usb2/core/usb2_process.h> 90#include <dev/usb2/core/usb2_config_td.h> 91#include <dev/usb2/core/usb2_request.h> 92#include <dev/usb2/core/usb2_lookup.h> 93#include <dev/usb2/core/usb2_util.h> 94#include <dev/usb2/core/usb2_busdma.h> 95#include <dev/usb2/core/usb2_device.h> 96 97#include <dev/usb2/serial/usb2_serial.h> 98 99#define UBSER_UNIT_MAX 32 100 101/* Vendor Interface Requests */ 102#define VENDOR_GET_NUMSER 0x01 103#define VENDOR_SET_BREAK 0x02 104#define VENDOR_CLEAR_BREAK 0x03 105 106#if USB_DEBUG 107static int ubser_debug = 0; 108 109SYSCTL_NODE(_hw_usb2, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser"); 110SYSCTL_INT(_hw_usb2_ubser, OID_AUTO, debug, CTLFLAG_RW, 111 &ubser_debug, 0, "ubser debug level"); 112#endif 113 114#define UBSER_TR_DT_WRITE 0 115#define UBSER_TR_DT_READ 1 116#define UBSER_TR_CS_WRITE 2 117#define UBSER_TR_CS_READ 3 118#define UBSER_TR_MAX 4 119 120struct ubser_softc { 121 struct usb2_com_super_softc sc_super_ucom; 122 struct usb2_com_softc sc_ucom[UBSER_UNIT_MAX]; 123 124 struct usb2_xfer *sc_xfer[UBSER_TR_MAX]; 125 struct usb2_device *sc_udev; 126 127 uint16_t sc_tx_size; 128 129 uint8_t sc_numser; 130 uint8_t sc_flags; 131#define UBSER_FLAG_READ_STALL 0x01 132#define UBSER_FLAG_WRITE_STALL 0x02 133 134 uint8_t sc_iface_no; 135 uint8_t sc_iface_index; 136 uint8_t sc_curr_tx_unit; 137 uint8_t sc_name[16]; 138}; 139 140/* prototypes */ 141 142static device_probe_t ubser_probe; 143static device_attach_t ubser_attach; 144static device_detach_t ubser_detach; 145 146static usb2_callback_t ubser_write_clear_stall_callback; 147static usb2_callback_t ubser_write_callback; 148static usb2_callback_t ubser_read_clear_stall_callback; 149static usb2_callback_t ubser_read_callback; 150 151static int ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t); 152static void ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff); 153static void ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr); 154static void ubser_start_read(struct usb2_com_softc *ucom); 155static void ubser_stop_read(struct usb2_com_softc *ucom); 156static void ubser_start_write(struct usb2_com_softc *ucom); 157static void ubser_stop_write(struct usb2_com_softc *ucom); 158 159static const struct usb2_config ubser_config[UBSER_TR_MAX] = { 160 161 [UBSER_TR_DT_WRITE] = { 162 .type = UE_BULK, 163 .endpoint = UE_ADDR_ANY, 164 .direction = UE_DIR_OUT, 165 .mh.bufsize = 0, /* use wMaxPacketSize */ 166 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 167 .mh.callback = &ubser_write_callback, 168 }, 169 170 [UBSER_TR_DT_READ] = { 171 .type = UE_BULK, 172 .endpoint = UE_ADDR_ANY, 173 .direction = UE_DIR_IN, 174 .mh.bufsize = 0, /* use wMaxPacketSize */ 175 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 176 .mh.callback = &ubser_read_callback, 177 }, 178 179 [UBSER_TR_CS_WRITE] = { 180 .type = UE_CONTROL, 181 .endpoint = 0x00, /* Control pipe */ 182 .direction = UE_DIR_ANY, 183 .mh.bufsize = sizeof(struct usb2_device_request), 184 .mh.flags = {}, 185 .mh.callback = &ubser_write_clear_stall_callback, 186 .mh.timeout = 1000, /* 1 second */ 187 .mh.interval = 50, /* 50ms */ 188 }, 189 190 [UBSER_TR_CS_READ] = { 191 .type = UE_CONTROL, 192 .endpoint = 0x00, /* Control pipe */ 193 .direction = UE_DIR_ANY, 194 .mh.bufsize = sizeof(struct usb2_device_request), 195 .mh.flags = {}, 196 .mh.callback = &ubser_read_clear_stall_callback, 197 .mh.timeout = 1000, /* 1 second */ 198 .mh.interval = 50, /* 50ms */ 199 }, 200}; 201 202static const struct usb2_com_callback ubser_callback = { 203 .usb2_com_cfg_set_break = &ubser_cfg_set_break, 204 .usb2_com_cfg_get_status = &ubser_cfg_get_status, 205 .usb2_com_pre_param = &ubser_pre_param, 206 .usb2_com_start_read = &ubser_start_read, 207 .usb2_com_stop_read = &ubser_stop_read, 208 .usb2_com_start_write = &ubser_start_write, 209 .usb2_com_stop_write = &ubser_stop_write, 210}; 211 212static device_method_t ubser_methods[] = { 213 DEVMETHOD(device_probe, ubser_probe), 214 DEVMETHOD(device_attach, ubser_attach), 215 DEVMETHOD(device_detach, ubser_detach), 216 {0, 0} 217}; 218 219static devclass_t ubser_devclass; 220 221static driver_t ubser_driver = { 222 .name = "ubser", 223 .methods = ubser_methods, 224 .size = sizeof(struct ubser_softc), 225}; 226 227DRIVER_MODULE(ubser, ushub, ubser_driver, ubser_devclass, NULL, 0); 228MODULE_DEPEND(ubser, usb2_serial, 1, 1, 1); 229MODULE_DEPEND(ubser, usb2_core, 1, 1, 1); 230MODULE_DEPEND(ubser, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER); 231 232static int 233ubser_probe(device_t dev) 234{ 235 struct usb2_attach_arg *uaa = device_get_ivars(dev); 236 237 if (uaa->usb2_mode != USB_MODE_HOST) { 238 return (ENXIO); 239 } 240 /* check if this is a BWCT vendor specific ubser interface */ 241 if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) && 242 (uaa->info.bInterfaceClass == 0xff) && 243 (uaa->info.bInterfaceSubClass == 0x00)) 244 return (0); 245 246 return (ENXIO); 247} 248 249static int 250ubser_attach(device_t dev) 251{ 252 struct usb2_attach_arg *uaa = device_get_ivars(dev); 253 struct ubser_softc *sc = device_get_softc(dev); 254 struct usb2_device_request req; 255 uint8_t n; 256 int error; 257 258 if (sc == NULL) { 259 return (ENOMEM); 260 } 261 device_set_usb2_desc(dev); 262 263 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s", 264 device_get_nameunit(dev)); 265 266 sc->sc_iface_no = uaa->info.bIfaceNum; 267 sc->sc_iface_index = uaa->info.bIfaceIndex; 268 sc->sc_udev = uaa->device; 269 270 /* get number of serials */ 271 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 272 req.bRequest = VENDOR_GET_NUMSER; 273 USETW(req.wValue, 0); 274 req.wIndex[0] = sc->sc_iface_no; 275 req.wIndex[1] = 0; 276 USETW(req.wLength, 1); 277 error = usb2_do_request_flags 278 (uaa->device, &Giant, &req, &sc->sc_numser, 279 0, NULL, USB_DEFAULT_TIMEOUT); 280 281 if (error || (sc->sc_numser == 0)) { 282 device_printf(dev, "failed to get number " 283 "of serial ports: %s\n", 284 usb2_errstr(error)); 285 goto detach; 286 } 287 if (sc->sc_numser > UBSER_UNIT_MAX) 288 sc->sc_numser = UBSER_UNIT_MAX; 289 290 device_printf(dev, "found %i serials\n", sc->sc_numser); 291 292 error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index, 293 sc->sc_xfer, ubser_config, UBSER_TR_MAX, sc, &Giant); 294 if (error) { 295 goto detach; 296 } 297 sc->sc_tx_size = sc->sc_xfer[UBSER_TR_DT_WRITE]->max_data_length; 298 299 if (sc->sc_tx_size == 0) { 300 DPRINTFN(0, "invalid tx_size!\n"); 301 goto detach; 302 } 303 /* initialize port numbers */ 304 305 for (n = 0; n < sc->sc_numser; n++) { 306 sc->sc_ucom[n].sc_portno = n; 307 } 308 309 error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom, 310 sc->sc_numser, sc, &ubser_callback, &Giant); 311 if (error) { 312 goto detach; 313 } 314 mtx_lock(&Giant); 315 316 sc->sc_flags |= (UBSER_FLAG_READ_STALL | 317 UBSER_FLAG_WRITE_STALL); 318 319 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_READ]); 320 321 mtx_unlock(&Giant); 322 323 return (0); /* success */ 324 325detach: 326 ubser_detach(dev); 327 return (ENXIO); /* failure */ 328} 329 330static int 331ubser_detach(device_t dev) 332{ 333 struct ubser_softc *sc = device_get_softc(dev); 334 uint8_t n; 335 336 DPRINTF("\n"); 337 338 usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser); 339 340 /* 341 * need to stop all transfers atomically, hence when clear stall 342 * completes, it might start other transfers ! 343 */ 344 mtx_lock(&Giant); 345 for (n = 0; n < UBSER_TR_MAX; n++) { 346 usb2_transfer_stop(sc->sc_xfer[n]); 347 } 348 mtx_unlock(&Giant); 349 350 usb2_transfer_unsetup(sc->sc_xfer, UBSER_TR_MAX); 351 352 return (0); 353} 354 355static int 356ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t) 357{ 358 DPRINTF("\n"); 359 360 /* 361 * The firmware on our devices can only do 8n1@9600bps 362 * without handshake. 363 * We refuse to accept other configurations. 364 */ 365 366 /* ensure 9600bps */ 367 switch (t->c_ospeed) { 368 case 9600: 369 break; 370 default: 371 return (EINVAL); 372 } 373 374 /* 2 stop bits not possible */ 375 if (t->c_cflag & CSTOPB) 376 return (EINVAL); 377 378 /* XXX parity handling not possible with current firmware */ 379 if (t->c_cflag & PARENB) 380 return (EINVAL); 381 382 /* we can only do 8 data bits */ 383 switch (t->c_cflag & CSIZE) { 384 case CS8: 385 break; 386 default: 387 return (EINVAL); 388 } 389 390 /* we can't do any kind of hardware handshaking */ 391 if ((t->c_cflag & 392 (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0) 393 return (EINVAL); 394 395 /* 396 * XXX xon/xoff not supported by the firmware! 397 * This is handled within FreeBSD only and may overflow buffers 398 * because of delayed reaction due to device buffering. 399 */ 400 401 return (0); 402} 403 404static __inline void 405ubser_inc_tx_unit(struct ubser_softc *sc) 406{ 407 sc->sc_curr_tx_unit++; 408 if (sc->sc_curr_tx_unit >= sc->sc_numser) { 409 sc->sc_curr_tx_unit = 0; 410 } 411 return; 412} 413 414static void 415ubser_write_clear_stall_callback(struct usb2_xfer *xfer) 416{ 417 struct ubser_softc *sc = xfer->priv_sc; 418 struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_TR_DT_WRITE]; 419 420 if (usb2_clear_stall_callback(xfer, xfer_other)) { 421 DPRINTF("stall cleared\n"); 422 sc->sc_flags &= ~UBSER_FLAG_WRITE_STALL; 423 usb2_transfer_start(xfer_other); 424 } 425 return; 426} 427 428static void 429ubser_write_callback(struct usb2_xfer *xfer) 430{ 431 struct ubser_softc *sc = xfer->priv_sc; 432 uint8_t buf[1]; 433 uint8_t first_unit = sc->sc_curr_tx_unit; 434 uint32_t actlen; 435 436 switch (USB_GET_STATE(xfer)) { 437 case USB_ST_SETUP: 438 case USB_ST_TRANSFERRED: 439 if (sc->sc_flags & UBSER_FLAG_WRITE_STALL) { 440 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_WRITE]); 441 return; 442 } 443 do { 444 if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit, 445 xfer->frbuffers, 1, sc->sc_tx_size - 1, 446 &actlen)) { 447 448 buf[0] = sc->sc_curr_tx_unit; 449 450 usb2_copy_in(xfer->frbuffers, 0, buf, 1); 451 452 xfer->frlengths[0] = actlen + 1; 453 usb2_start_hardware(xfer); 454 455 ubser_inc_tx_unit(sc); /* round robin */ 456 457 break; 458 } 459 ubser_inc_tx_unit(sc); 460 461 } while (sc->sc_curr_tx_unit != first_unit); 462 463 return; 464 465 default: /* Error */ 466 if (xfer->error != USB_ERR_CANCELLED) { 467 sc->sc_flags |= UBSER_FLAG_WRITE_STALL; 468 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_WRITE]); 469 } 470 return; 471 472 } 473} 474 475static void 476ubser_read_clear_stall_callback(struct usb2_xfer *xfer) 477{ 478 struct ubser_softc *sc = xfer->priv_sc; 479 struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_TR_DT_READ]; 480 481 if (usb2_clear_stall_callback(xfer, xfer_other)) { 482 DPRINTF("stall cleared\n"); 483 sc->sc_flags &= ~UBSER_FLAG_READ_STALL; 484 usb2_transfer_start(xfer_other); 485 } 486 return; 487} 488 489static void 490ubser_read_callback(struct usb2_xfer *xfer) 491{ 492 struct ubser_softc *sc = xfer->priv_sc; 493 uint8_t buf[1]; 494 495 switch (USB_GET_STATE(xfer)) { 496 case USB_ST_TRANSFERRED: 497 if (xfer->actlen < 1) { 498 DPRINTF("invalid actlen=0!\n"); 499 goto tr_setup; 500 } 501 usb2_copy_out(xfer->frbuffers, 0, buf, 1); 502 503 if (buf[0] >= sc->sc_numser) { 504 DPRINTF("invalid serial number!\n"); 505 goto tr_setup; 506 } 507 usb2_com_put_data(sc->sc_ucom + buf[0], 508 xfer->frbuffers, 1, xfer->actlen - 1); 509 510 case USB_ST_SETUP: 511tr_setup: 512 if (sc->sc_flags & UBSER_FLAG_READ_STALL) { 513 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_READ]); 514 } else { 515 xfer->frlengths[0] = xfer->max_data_length; 516 usb2_start_hardware(xfer); 517 } 518 return; 519 520 default: /* Error */ 521 if (xfer->error != USB_ERR_CANCELLED) { 522 sc->sc_flags |= UBSER_FLAG_READ_STALL; 523 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_READ]); 524 } 525 return; 526 527 } 528} 529 530static void 531ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff) 532{ 533 struct ubser_softc *sc = ucom->sc_parent; 534 uint8_t x = ucom->sc_portno; 535 struct usb2_device_request req; 536 usb2_error_t err; 537 538 if (onoff) { 539 540 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 541 req.bRequest = VENDOR_SET_BREAK; 542 req.wValue[0] = x; 543 req.wValue[1] = 0; 544 req.wIndex[0] = sc->sc_iface_no; 545 req.wIndex[1] = 0; 546 USETW(req.wLength, 0); 547 548 err = usb2_do_request_flags 549 (sc->sc_udev, &Giant, &req, NULL, 0, NULL, 1000); 550 551 if (err) { 552 DPRINTFN(0, "send break failed, error=%s\n", 553 usb2_errstr(err)); 554 } 555 } 556 return; 557} 558 559static void 560ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr) 561{ 562 /* fake status bits */ 563 *lsr = 0; 564 *msr = SER_DCD; 565 return; 566} 567 568static void 569ubser_start_read(struct usb2_com_softc *ucom) 570{ 571 struct ubser_softc *sc = ucom->sc_parent; 572 573 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_READ]); 574 return; 575} 576 577static void 578ubser_stop_read(struct usb2_com_softc *ucom) 579{ 580 struct ubser_softc *sc = ucom->sc_parent; 581 582 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_CS_READ]); 583 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_DT_READ]); 584 return; 585} 586 587static void 588ubser_start_write(struct usb2_com_softc *ucom) 589{ 590 struct ubser_softc *sc = ucom->sc_parent; 591 592 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_WRITE]); 593 return; 594} 595 596static void 597ubser_stop_write(struct usb2_com_softc *ucom) 598{ 599 struct ubser_softc *sc = ucom->sc_parent; 600 601 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_CS_WRITE]); 602 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_DT_WRITE]); 603 return; 604} 605