ubser.c revision 185950
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 185950 2008-12-11 23:17:48Z thompsa $"); 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 *, struct termios *); 152static void ubser_cfg_set_break(struct usb2_com_softc *, uint8_t); 153static void ubser_cfg_get_status(struct usb2_com_softc *, uint8_t *, 154 uint8_t *); 155static void ubser_start_read(struct usb2_com_softc *); 156static void ubser_stop_read(struct usb2_com_softc *); 157static void ubser_start_write(struct usb2_com_softc *); 158static void ubser_stop_write(struct usb2_com_softc *); 159 160static const struct usb2_config ubser_config[UBSER_TR_MAX] = { 161 162 [UBSER_TR_DT_WRITE] = { 163 .type = UE_BULK, 164 .endpoint = UE_ADDR_ANY, 165 .direction = UE_DIR_OUT, 166 .mh.bufsize = 0, /* use wMaxPacketSize */ 167 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 168 .mh.callback = &ubser_write_callback, 169 }, 170 171 [UBSER_TR_DT_READ] = { 172 .type = UE_BULK, 173 .endpoint = UE_ADDR_ANY, 174 .direction = UE_DIR_IN, 175 .mh.bufsize = 0, /* use wMaxPacketSize */ 176 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 177 .mh.callback = &ubser_read_callback, 178 }, 179 180 [UBSER_TR_CS_WRITE] = { 181 .type = UE_CONTROL, 182 .endpoint = 0x00, /* Control pipe */ 183 .direction = UE_DIR_ANY, 184 .mh.bufsize = sizeof(struct usb2_device_request), 185 .mh.flags = {}, 186 .mh.callback = &ubser_write_clear_stall_callback, 187 .mh.timeout = 1000, /* 1 second */ 188 .mh.interval = 50, /* 50ms */ 189 }, 190 191 [UBSER_TR_CS_READ] = { 192 .type = UE_CONTROL, 193 .endpoint = 0x00, /* Control pipe */ 194 .direction = UE_DIR_ANY, 195 .mh.bufsize = sizeof(struct usb2_device_request), 196 .mh.flags = {}, 197 .mh.callback = &ubser_read_clear_stall_callback, 198 .mh.timeout = 1000, /* 1 second */ 199 .mh.interval = 50, /* 50ms */ 200 }, 201}; 202 203static const struct usb2_com_callback ubser_callback = { 204 .usb2_com_cfg_set_break = &ubser_cfg_set_break, 205 .usb2_com_cfg_get_status = &ubser_cfg_get_status, 206 .usb2_com_pre_param = &ubser_pre_param, 207 .usb2_com_start_read = &ubser_start_read, 208 .usb2_com_stop_read = &ubser_stop_read, 209 .usb2_com_start_write = &ubser_start_write, 210 .usb2_com_stop_write = &ubser_stop_write, 211}; 212 213static device_method_t ubser_methods[] = { 214 DEVMETHOD(device_probe, ubser_probe), 215 DEVMETHOD(device_attach, ubser_attach), 216 DEVMETHOD(device_detach, ubser_detach), 217 {0, 0} 218}; 219 220static devclass_t ubser_devclass; 221 222static driver_t ubser_driver = { 223 .name = "ubser", 224 .methods = ubser_methods, 225 .size = sizeof(struct ubser_softc), 226}; 227 228DRIVER_MODULE(ubser, ushub, ubser_driver, ubser_devclass, NULL, 0); 229MODULE_DEPEND(ubser, usb2_serial, 1, 1, 1); 230MODULE_DEPEND(ubser, usb2_core, 1, 1, 1); 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} 412 413static void 414ubser_write_clear_stall_callback(struct usb2_xfer *xfer) 415{ 416 struct ubser_softc *sc = xfer->priv_sc; 417 struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_TR_DT_WRITE]; 418 419 if (usb2_clear_stall_callback(xfer, xfer_other)) { 420 DPRINTF("stall cleared\n"); 421 sc->sc_flags &= ~UBSER_FLAG_WRITE_STALL; 422 usb2_transfer_start(xfer_other); 423 } 424} 425 426static void 427ubser_write_callback(struct usb2_xfer *xfer) 428{ 429 struct ubser_softc *sc = xfer->priv_sc; 430 uint8_t buf[1]; 431 uint8_t first_unit = sc->sc_curr_tx_unit; 432 uint32_t actlen; 433 434 switch (USB_GET_STATE(xfer)) { 435 case USB_ST_SETUP: 436 case USB_ST_TRANSFERRED: 437 if (sc->sc_flags & UBSER_FLAG_WRITE_STALL) { 438 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_WRITE]); 439 return; 440 } 441 do { 442 if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit, 443 xfer->frbuffers, 1, sc->sc_tx_size - 1, 444 &actlen)) { 445 446 buf[0] = sc->sc_curr_tx_unit; 447 448 usb2_copy_in(xfer->frbuffers, 0, buf, 1); 449 450 xfer->frlengths[0] = actlen + 1; 451 usb2_start_hardware(xfer); 452 453 ubser_inc_tx_unit(sc); /* round robin */ 454 455 break; 456 } 457 ubser_inc_tx_unit(sc); 458 459 } while (sc->sc_curr_tx_unit != first_unit); 460 461 return; 462 463 default: /* Error */ 464 if (xfer->error != USB_ERR_CANCELLED) { 465 sc->sc_flags |= UBSER_FLAG_WRITE_STALL; 466 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_WRITE]); 467 } 468 return; 469 470 } 471} 472 473static void 474ubser_read_clear_stall_callback(struct usb2_xfer *xfer) 475{ 476 struct ubser_softc *sc = xfer->priv_sc; 477 struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_TR_DT_READ]; 478 479 if (usb2_clear_stall_callback(xfer, xfer_other)) { 480 DPRINTF("stall cleared\n"); 481 sc->sc_flags &= ~UBSER_FLAG_READ_STALL; 482 usb2_transfer_start(xfer_other); 483 } 484} 485 486static void 487ubser_read_callback(struct usb2_xfer *xfer) 488{ 489 struct ubser_softc *sc = xfer->priv_sc; 490 uint8_t buf[1]; 491 492 switch (USB_GET_STATE(xfer)) { 493 case USB_ST_TRANSFERRED: 494 if (xfer->actlen < 1) { 495 DPRINTF("invalid actlen=0!\n"); 496 goto tr_setup; 497 } 498 usb2_copy_out(xfer->frbuffers, 0, buf, 1); 499 500 if (buf[0] >= sc->sc_numser) { 501 DPRINTF("invalid serial number!\n"); 502 goto tr_setup; 503 } 504 usb2_com_put_data(sc->sc_ucom + buf[0], 505 xfer->frbuffers, 1, xfer->actlen - 1); 506 507 case USB_ST_SETUP: 508tr_setup: 509 if (sc->sc_flags & UBSER_FLAG_READ_STALL) { 510 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_READ]); 511 } else { 512 xfer->frlengths[0] = xfer->max_data_length; 513 usb2_start_hardware(xfer); 514 } 515 return; 516 517 default: /* Error */ 518 if (xfer->error != USB_ERR_CANCELLED) { 519 sc->sc_flags |= UBSER_FLAG_READ_STALL; 520 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_READ]); 521 } 522 return; 523 524 } 525} 526 527static void 528ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff) 529{ 530 struct ubser_softc *sc = ucom->sc_parent; 531 uint8_t x = ucom->sc_portno; 532 struct usb2_device_request req; 533 usb2_error_t err; 534 535 if (onoff) { 536 537 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 538 req.bRequest = VENDOR_SET_BREAK; 539 req.wValue[0] = x; 540 req.wValue[1] = 0; 541 req.wIndex[0] = sc->sc_iface_no; 542 req.wIndex[1] = 0; 543 USETW(req.wLength, 0); 544 545 err = usb2_do_request_flags 546 (sc->sc_udev, &Giant, &req, NULL, 0, NULL, 1000); 547 548 if (err) { 549 DPRINTFN(0, "send break failed, error=%s\n", 550 usb2_errstr(err)); 551 } 552 } 553} 554 555static void 556ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr) 557{ 558 /* fake status bits */ 559 *lsr = 0; 560 *msr = SER_DCD; 561} 562 563static void 564ubser_start_read(struct usb2_com_softc *ucom) 565{ 566 struct ubser_softc *sc = ucom->sc_parent; 567 568 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_READ]); 569} 570 571static void 572ubser_stop_read(struct usb2_com_softc *ucom) 573{ 574 struct ubser_softc *sc = ucom->sc_parent; 575 576 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_CS_READ]); 577 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_DT_READ]); 578} 579 580static void 581ubser_start_write(struct usb2_com_softc *ucom) 582{ 583 struct ubser_softc *sc = ucom->sc_parent; 584 585 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_WRITE]); 586} 587 588static void 589ubser_stop_write(struct usb2_com_softc *ucom) 590{ 591 struct ubser_softc *sc = ucom->sc_parent; 592 593 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_CS_WRITE]); 594 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_DT_WRITE]); 595} 596