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$"); 74 75 /* 76 * BWCT serial adapter driver 77 */ 78 79 #include <dev/usb/usb.h> 80 #include <dev/usb/usb_mfunc.h> 81 #include <dev/usb/usb_error.h> 82 #include <dev/usb/usb_cdc.h> 83 #include <dev/usb/usb_defs.h> 84 85 #define USB_DEBUG_VAR ubser_debug 86 87 #include <dev/usb/usb_core.h> 88 #include <dev/usb/usb_debug.h> 89 #include <dev/usb/usb_process.h> 90 #include <dev/usb/usb_request.h> 91 #include <dev/usb/usb_lookup.h> 92 #include <dev/usb/usb_util.h> 93 #include <dev/usb/usb_busdma.h> 94 #include <dev/usb/usb_device.h> 95 96 #include <dev/usb/serial/usb_serial.h> 97 98 #define UBSER_UNIT_MAX 32 99 100 /* Vendor Interface Requests */ 101 #define VENDOR_GET_NUMSER 0x01 102 #define VENDOR_SET_BREAK 0x02 103 #define VENDOR_CLEAR_BREAK 0x03 104 105 #if USB_DEBUG 106 static int ubser_debug = 0; 107 108 SYSCTL_NODE(_hw_usb2, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser"); 109 SYSCTL_INT(_hw_usb2_ubser, OID_AUTO, debug, CTLFLAG_RW, 110 &ubser_debug, 0, "ubser debug level"); 111 #endif 112 113 enum { 114 UBSER_BULK_DT_WR, 115 UBSER_BULK_DT_RD, 116 UBSER_N_TRANSFER, 117 }; 118 119 struct ubser_softc { 120 struct usb2_com_super_softc sc_super_ucom; 121 struct usb2_com_softc sc_ucom[UBSER_UNIT_MAX]; 122 123 struct usb2_xfer *sc_xfer[UBSER_N_TRANSFER]; 124 struct usb2_device *sc_udev; 125 126 uint16_t sc_tx_size; 127 128 uint8_t sc_numser; 129 uint8_t sc_iface_no; 130 uint8_t sc_iface_index; 131 uint8_t sc_curr_tx_unit; 132 uint8_t sc_name[16]; 133 }; 134 135 /* prototypes */ 136 137 static device_probe_t ubser_probe; 138 static device_attach_t ubser_attach; 139 static device_detach_t ubser_detach; 140 141 static usb2_callback_t ubser_write_callback; 142 static usb2_callback_t ubser_read_callback; 143 144 static int ubser_pre_param(struct usb2_com_softc *, struct termios *); 145 static void ubser_cfg_set_break(struct usb2_com_softc *, uint8_t); 146 static void ubser_cfg_get_status(struct usb2_com_softc *, uint8_t *, 147 uint8_t *); 148 static void ubser_start_read(struct usb2_com_softc *); 149 static void ubser_stop_read(struct usb2_com_softc *); 150 static void ubser_start_write(struct usb2_com_softc *); 151 static void ubser_stop_write(struct usb2_com_softc *); 152 153 static const struct usb2_config ubser_config[UBSER_N_TRANSFER] = { 154 155 [UBSER_BULK_DT_WR] = { 156 .type = UE_BULK, 157 .endpoint = UE_ADDR_ANY, 158 .direction = UE_DIR_OUT, 159 .mh.bufsize = 0, /* use wMaxPacketSize */ 160 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 161 .mh.callback = &ubser_write_callback, 162 }, 163 164 [UBSER_BULK_DT_RD] = { 165 .type = UE_BULK, 166 .endpoint = UE_ADDR_ANY, 167 .direction = UE_DIR_IN, 168 .mh.bufsize = 0, /* use wMaxPacketSize */ 169 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 170 .mh.callback = &ubser_read_callback, 171 }, 172 }; 173 174 static const struct usb2_com_callback ubser_callback = { 175 .usb2_com_cfg_set_break = &ubser_cfg_set_break, 176 .usb2_com_cfg_get_status = &ubser_cfg_get_status, 177 .usb2_com_pre_param = &ubser_pre_param, 178 .usb2_com_start_read = &ubser_start_read, 179 .usb2_com_stop_read = &ubser_stop_read, 180 .usb2_com_start_write = &ubser_start_write, 181 .usb2_com_stop_write = &ubser_stop_write, 182 }; 183 184 static device_method_t ubser_methods[] = { 185 DEVMETHOD(device_probe, ubser_probe), 186 DEVMETHOD(device_attach, ubser_attach), 187 DEVMETHOD(device_detach, ubser_detach), 188 {0, 0} 189 }; 190 191 static devclass_t ubser_devclass; 192 193 static driver_t ubser_driver = { 194 .name = "ubser", 195 .methods = ubser_methods, 196 .size = sizeof(struct ubser_softc), 197 }; 198 199 DRIVER_MODULE(ubser, ushub, ubser_driver, ubser_devclass, NULL, 0); 200 MODULE_DEPEND(ubser, ucom, 1, 1, 1); 201 MODULE_DEPEND(ubser, usb, 1, 1, 1); 202 203 static int 204 ubser_probe(device_t dev) 205 { 206 struct usb2_attach_arg *uaa = device_get_ivars(dev); 207 208 if (uaa->usb2_mode != USB_MODE_HOST) { 209 return (ENXIO); 210 } 211 /* check if this is a BWCT vendor specific ubser interface */ 212 if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) && 213 (uaa->info.bInterfaceClass == 0xff) && 214 (uaa->info.bInterfaceSubClass == 0x00)) 215 return (0); 216 217 return (ENXIO); 218 } 219 220 static int 221 ubser_attach(device_t dev) 222 { 223 struct usb2_attach_arg *uaa = device_get_ivars(dev); 224 struct ubser_softc *sc = device_get_softc(dev); 225 struct usb2_device_request req; 226 uint8_t n; 227 int error; 228 229 device_set_usb2_desc(dev); 230 231 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s", 232 device_get_nameunit(dev)); 233 234 sc->sc_iface_no = uaa->info.bIfaceNum; 235 sc->sc_iface_index = uaa->info.bIfaceIndex; 236 sc->sc_udev = uaa->device; 237 238 /* get number of serials */ 239 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 240 req.bRequest = VENDOR_GET_NUMSER; 241 USETW(req.wValue, 0); 242 req.wIndex[0] = sc->sc_iface_no; 243 req.wIndex[1] = 0; 244 USETW(req.wLength, 1); 245 error = usb2_do_request_flags 246 (uaa->device, &Giant, &req, &sc->sc_numser, 247 0, NULL, USB_DEFAULT_TIMEOUT); 248 249 if (error || (sc->sc_numser == 0)) { 250 device_printf(dev, "failed to get number " 251 "of serial ports: %s\n", 252 usb2_errstr(error)); 253 goto detach; 254 } 255 if (sc->sc_numser > UBSER_UNIT_MAX) 256 sc->sc_numser = UBSER_UNIT_MAX; 257 258 device_printf(dev, "found %i serials\n", sc->sc_numser); 259 260 error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index, 261 sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &Giant); 262 if (error) { 263 goto detach; 264 } 265 sc->sc_tx_size = sc->sc_xfer[UBSER_BULK_DT_WR]->max_data_length; 266 267 if (sc->sc_tx_size == 0) { 268 DPRINTFN(0, "invalid tx_size!\n"); 269 goto detach; 270 } 271 /* initialize port numbers */ 272 273 for (n = 0; n < sc->sc_numser; n++) { 274 sc->sc_ucom[n].sc_portno = n; 275 } 276 277 error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom, 278 sc->sc_numser, sc, &ubser_callback, &Giant); 279 if (error) { 280 goto detach; 281 } 282 mtx_lock(&Giant); 283 284 usb2_transfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]); 285 usb2_transfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]); 286 287 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]); 288 289 mtx_unlock(&Giant); 290 291 return (0); /* success */ 292 293 detach: 294 ubser_detach(dev); 295 return (ENXIO); /* failure */ 296 } 297 298 static int 299 ubser_detach(device_t dev) 300 { 301 struct ubser_softc *sc = device_get_softc(dev); 302 303 DPRINTF("\n"); 304 305 usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser); 306 307 usb2_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER); 308 309 return (0); 310 } 311 312 static int 313 ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t) 314 { 315 DPRINTF("\n"); 316 317 /* 318 * The firmware on our devices can only do 8n1@9600bps 319 * without handshake. 320 * We refuse to accept other configurations. 321 */ 322 323 /* ensure 9600bps */ 324 switch (t->c_ospeed) { 325 case 9600: 326 break; 327 default: 328 return (EINVAL); 329 } 330 331 /* 2 stop bits not possible */ 332 if (t->c_cflag & CSTOPB) 333 return (EINVAL); 334 335 /* XXX parity handling not possible with current firmware */ 336 if (t->c_cflag & PARENB) 337 return (EINVAL); 338 339 /* we can only do 8 data bits */ 340 switch (t->c_cflag & CSIZE) { 341 case CS8: 342 break; 343 default: 344 return (EINVAL); 345 } 346 347 /* we can't do any kind of hardware handshaking */ 348 if ((t->c_cflag & 349 (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0) 350 return (EINVAL); 351 352 /* 353 * XXX xon/xoff not supported by the firmware! 354 * This is handled within FreeBSD only and may overflow buffers 355 * because of delayed reaction due to device buffering. 356 */ 357 358 return (0); 359 } 360 361 static __inline void 362 ubser_inc_tx_unit(struct ubser_softc *sc) 363 { 364 sc->sc_curr_tx_unit++; 365 if (sc->sc_curr_tx_unit >= sc->sc_numser) { 366 sc->sc_curr_tx_unit = 0; 367 } 368 } 369 370 static void 371 ubser_write_callback(struct usb2_xfer *xfer) 372 { 373 struct ubser_softc *sc = xfer->priv_sc; 374 uint8_t buf[1]; 375 uint8_t first_unit = sc->sc_curr_tx_unit; 376 uint32_t actlen; 377 378 switch (USB_GET_STATE(xfer)) { 379 case USB_ST_SETUP: 380 case USB_ST_TRANSFERRED: 381 tr_setup: 382 do { 383 if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit, 384 xfer->frbuffers, 1, sc->sc_tx_size - 1, 385 &actlen)) { 386 387 buf[0] = sc->sc_curr_tx_unit; 388 389 usb2_copy_in(xfer->frbuffers, 0, buf, 1); 390 391 xfer->frlengths[0] = actlen + 1; 392 usb2_start_hardware(xfer); 393 394 ubser_inc_tx_unit(sc); /* round robin */ 395 396 break; 397 } 398 ubser_inc_tx_unit(sc); 399 400 } while (sc->sc_curr_tx_unit != first_unit); 401 402 return; 403 404 default: /* Error */ 405 if (xfer->error != USB_ERR_CANCELLED) { 406 /* try to clear stall first */ 407 xfer->flags.stall_pipe = 1; 408 goto tr_setup; 409 } 410 return; 411 412 } 413 } 414 415 static void 416 ubser_read_callback(struct usb2_xfer *xfer) 417 { 418 struct ubser_softc *sc = xfer->priv_sc; 419 uint8_t buf[1]; 420 421 switch (USB_GET_STATE(xfer)) { 422 case USB_ST_TRANSFERRED: 423 if (xfer->actlen < 1) { 424 DPRINTF("invalid actlen=0!\n"); 425 goto tr_setup; 426 } 427 usb2_copy_out(xfer->frbuffers, 0, buf, 1); 428 429 if (buf[0] >= sc->sc_numser) { 430 DPRINTF("invalid serial number!\n"); 431 goto tr_setup; 432 } 433 usb2_com_put_data(sc->sc_ucom + buf[0], 434 xfer->frbuffers, 1, xfer->actlen - 1); 435 436 case USB_ST_SETUP: 437 tr_setup: 438 xfer->frlengths[0] = xfer->max_data_length; 439 usb2_start_hardware(xfer); 440 return; 441 442 default: /* Error */ 443 if (xfer->error != USB_ERR_CANCELLED) { 444 /* try to clear stall first */ 445 xfer->flags.stall_pipe = 1; 446 goto tr_setup; 447 } 448 return; 449 450 } 451 } 452 453 static void 454 ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff) 455 { 456 struct ubser_softc *sc = ucom->sc_parent; 457 uint8_t x = ucom->sc_portno; 458 struct usb2_device_request req; 459 usb2_error_t err; 460 461 if (onoff) { 462 463 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 464 req.bRequest = VENDOR_SET_BREAK; 465 req.wValue[0] = x; 466 req.wValue[1] = 0; 467 req.wIndex[0] = sc->sc_iface_no; 468 req.wIndex[1] = 0; 469 USETW(req.wLength, 0); 470 471 err = usb2_com_cfg_do_request(sc->sc_udev, ucom, 472 &req, NULL, 0, 1000); 473 if (err) { 474 DPRINTFN(0, "send break failed, error=%s\n", 475 usb2_errstr(err)); 476 } 477 } 478 } 479 480 static void 481 ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr) 482 { 483 /* fake status bits */ 484 *lsr = 0; 485 *msr = SER_DCD; 486 } 487 488 static void 489 ubser_start_read(struct usb2_com_softc *ucom) 490 { 491 struct ubser_softc *sc = ucom->sc_parent; 492 493 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]); 494 } 495 496 static void 497 ubser_stop_read(struct usb2_com_softc *ucom) 498 { 499 struct ubser_softc *sc = ucom->sc_parent; 500 501 usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]); 502 } 503 504 static void 505 ubser_start_write(struct usb2_com_softc *ucom) 506 { 507 struct ubser_softc *sc = ucom->sc_parent; 508 509 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]); 510 } 511 512 static void 513 ubser_stop_write(struct usb2_com_softc *ucom) 514 { 515 struct ubser_softc *sc = ucom->sc_parent; 516 517 usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]); 518 } 519