1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf 30 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf 31 * http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip 32 */ 33 34 #include <sys/param.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/stdint.h> 38 #include <sys/stddef.h> 39 #include <sys/queue.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/bus.h> 43 #include <sys/linker_set.h> 44 #include <sys/module.h> 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/condvar.h> 48 #include <sys/sysctl.h> 49 #include <sys/sx.h> 50 #include <sys/unistd.h> 51 #include <sys/callout.h> 52 #include <sys/malloc.h> 53 #include <sys/priv.h> 54 55 #include <dev/usb/usb.h> 56 #include <dev/usb/usb_cdc.h> 57 #include <dev/usb/usbdi.h> 58 #include <dev/usb/usbdi_util.h> 59 #include <dev/usb/usbhid.h> 60 #include "usb_if.h" 61 62 #define USB_DEBUG_VAR g_modem_debug 63 #include <dev/usb/usb_debug.h> 64 65 #include <dev/usb/gadget/g_modem.h> 66 67 enum { 68 G_MODEM_INTR_DT, 69 G_MODEM_BULK_RD, 70 G_MODEM_BULK_WR, 71 G_MODEM_N_TRANSFER, 72 }; 73 74 struct g_modem_softc { 75 struct mtx sc_mtx; 76 struct usb_callout sc_callout; 77 struct usb_callout sc_watchdog; 78 struct usb_xfer *sc_xfer[G_MODEM_N_TRANSFER]; 79 80 int sc_mode; 81 int sc_tx_busy; 82 int sc_pattern_len; 83 int sc_throughput; 84 int sc_tx_interval; 85 86 char sc_pattern[G_MODEM_MAX_STRLEN]; 87 88 uint16_t sc_data_len; 89 90 uint8_t sc_data_buf[G_MODEM_BUFSIZE]; 91 uint8_t sc_line_coding[32]; 92 uint8_t sc_abstract_state[32]; 93 }; 94 95 static SYSCTL_NODE(_hw_usb, OID_AUTO, g_modem, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 96 "USB modem gadget"); 97 98 #ifdef USB_DEBUG 99 static int g_modem_debug = 0; 100 101 SYSCTL_INT(_hw_usb_g_modem, OID_AUTO, debug, CTLFLAG_RWTUN, 102 &g_modem_debug, 0, "Debug level"); 103 #endif 104 105 static int g_modem_mode = 0; 106 107 SYSCTL_INT(_hw_usb_g_modem, OID_AUTO, mode, CTLFLAG_RWTUN, 108 &g_modem_mode, 0, "Mode selection"); 109 110 static int g_modem_pattern_interval = 1000; 111 112 SYSCTL_INT(_hw_usb_g_modem, OID_AUTO, pattern_interval, CTLFLAG_RWTUN, 113 &g_modem_pattern_interval, 0, "Pattern interval in milliseconds"); 114 115 static char g_modem_pattern_data[G_MODEM_MAX_STRLEN]; 116 117 SYSCTL_STRING(_hw_usb_g_modem, OID_AUTO, pattern, CTLFLAG_RW, 118 &g_modem_pattern_data, sizeof(g_modem_pattern_data), "Data pattern"); 119 120 static int g_modem_throughput; 121 122 SYSCTL_INT(_hw_usb_g_modem, OID_AUTO, throughput, CTLFLAG_RD, 123 &g_modem_throughput, sizeof(g_modem_throughput), "Throughput in bytes per second"); 124 125 static device_probe_t g_modem_probe; 126 static device_attach_t g_modem_attach; 127 static device_detach_t g_modem_detach; 128 static usb_handle_request_t g_modem_handle_request; 129 static usb_callback_t g_modem_intr_callback; 130 static usb_callback_t g_modem_bulk_read_callback; 131 static usb_callback_t g_modem_bulk_write_callback; 132 133 static void g_modem_timeout(void *arg); 134 135 static devclass_t g_modem_devclass; 136 137 static device_method_t g_modem_methods[] = { 138 /* USB interface */ 139 DEVMETHOD(usb_handle_request, g_modem_handle_request), 140 141 /* Device interface */ 142 DEVMETHOD(device_probe, g_modem_probe), 143 DEVMETHOD(device_attach, g_modem_attach), 144 DEVMETHOD(device_detach, g_modem_detach), 145 146 DEVMETHOD_END 147 }; 148 149 static driver_t g_modem_driver = { 150 .name = "g_modem", 151 .methods = g_modem_methods, 152 .size = sizeof(struct g_modem_softc), 153 }; 154 155 DRIVER_MODULE(g_modem, uhub, g_modem_driver, g_modem_devclass, 0, 0); 156 MODULE_DEPEND(g_modem, usb, 1, 1, 1); 157 158 static const struct usb_config g_modem_config[G_MODEM_N_TRANSFER] = { 159 160 [G_MODEM_INTR_DT] = { 161 .type = UE_INTERRUPT, 162 .endpoint = UE_ADDR_ANY, 163 .direction = UE_DIR_TX, 164 .flags = {.ext_buffer = 1,.pipe_bof = 1,}, 165 .bufsize = 0, /* use wMaxPacketSize */ 166 .callback = &g_modem_intr_callback, 167 .frames = 1, 168 .usb_mode = USB_MODE_DEVICE, 169 .if_index = 0, 170 }, 171 172 [G_MODEM_BULK_RD] = { 173 .type = UE_BULK, 174 .endpoint = UE_ADDR_ANY, 175 .direction = UE_DIR_RX, 176 .flags = {.ext_buffer = 1,.pipe_bof = 1,.short_xfer_ok = 1,}, 177 .bufsize = G_MODEM_BUFSIZE, 178 .callback = &g_modem_bulk_read_callback, 179 .frames = 1, 180 .usb_mode = USB_MODE_DEVICE, 181 .if_index = 1, 182 }, 183 184 [G_MODEM_BULK_WR] = { 185 .type = UE_BULK, 186 .endpoint = UE_ADDR_ANY, 187 .direction = UE_DIR_TX, 188 .flags = {.ext_buffer = 1,.pipe_bof = 1,}, 189 .bufsize = G_MODEM_BUFSIZE, 190 .callback = &g_modem_bulk_write_callback, 191 .frames = 1, 192 .usb_mode = USB_MODE_DEVICE, 193 .if_index = 1, 194 }, 195 }; 196 197 static void 198 g_modem_timeout_reset(struct g_modem_softc *sc) 199 { 200 int i = g_modem_pattern_interval; 201 202 sc->sc_tx_interval = i; 203 204 if (i <= 0) 205 i = 1; 206 else if (i > 1023) 207 i = 1023; 208 209 i = USB_MS_TO_TICKS(i); 210 211 usb_callout_reset(&sc->sc_callout, i, &g_modem_timeout, sc); 212 } 213 214 static void 215 g_modem_timeout(void *arg) 216 { 217 struct g_modem_softc *sc = arg; 218 219 sc->sc_mode = g_modem_mode; 220 221 memcpy(sc->sc_pattern, g_modem_pattern_data, sizeof(sc->sc_pattern)); 222 223 sc->sc_pattern[G_MODEM_MAX_STRLEN - 1] = 0; 224 225 sc->sc_pattern_len = strlen(sc->sc_pattern); 226 227 DPRINTFN(11, "Timeout %p\n", sc->sc_xfer[G_MODEM_INTR_DT]); 228 229 usbd_transfer_start(sc->sc_xfer[G_MODEM_BULK_WR]); 230 usbd_transfer_start(sc->sc_xfer[G_MODEM_BULK_RD]); 231 232 g_modem_timeout_reset(sc); 233 } 234 235 static void g_modem_watchdog(void *arg); 236 237 static void 238 g_modem_watchdog_reset(struct g_modem_softc *sc) 239 { 240 usb_callout_reset(&sc->sc_watchdog, hz, &g_modem_watchdog, sc); 241 } 242 243 static void 244 g_modem_watchdog(void *arg) 245 { 246 struct g_modem_softc *sc = arg; 247 int i; 248 249 i = sc->sc_throughput; 250 251 sc->sc_throughput = 0; 252 253 g_modem_throughput = i; 254 255 g_modem_watchdog_reset(sc); 256 } 257 258 static int 259 g_modem_probe(device_t dev) 260 { 261 struct usb_attach_arg *uaa = device_get_ivars(dev); 262 263 DPRINTFN(11, "\n"); 264 265 if (uaa->usb_mode != USB_MODE_DEVICE) 266 return (ENXIO); 267 268 if ((uaa->info.bInterfaceClass == UICLASS_CDC) && 269 (uaa->info.bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL) && 270 (uaa->info.bInterfaceProtocol == UIPROTO_CDC_AT)) 271 return (0); 272 273 return (ENXIO); 274 } 275 276 static int 277 g_modem_attach(device_t dev) 278 { 279 struct g_modem_softc *sc = device_get_softc(dev); 280 struct usb_attach_arg *uaa = device_get_ivars(dev); 281 int error; 282 uint8_t iface_index[2]; 283 284 DPRINTFN(11, "\n"); 285 286 device_set_usb_desc(dev); 287 288 mtx_init(&sc->sc_mtx, "g_modem", NULL, MTX_DEF); 289 290 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); 291 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 292 293 sc->sc_mode = G_MODEM_MODE_SILENT; 294 295 iface_index[0] = uaa->info.bIfaceIndex; 296 iface_index[1] = uaa->info.bIfaceIndex + 1; 297 298 error = usbd_transfer_setup(uaa->device, 299 iface_index, sc->sc_xfer, g_modem_config, 300 G_MODEM_N_TRANSFER, sc, &sc->sc_mtx); 301 302 if (error) { 303 DPRINTF("error=%s\n", usbd_errstr(error)); 304 goto detach; 305 } 306 usbd_set_parent_iface(uaa->device, iface_index[1], iface_index[0]); 307 308 mtx_lock(&sc->sc_mtx); 309 g_modem_timeout_reset(sc); 310 g_modem_watchdog_reset(sc); 311 mtx_unlock(&sc->sc_mtx); 312 313 return (0); /* success */ 314 315 detach: 316 g_modem_detach(dev); 317 318 return (ENXIO); /* error */ 319 } 320 321 static int 322 g_modem_detach(device_t dev) 323 { 324 struct g_modem_softc *sc = device_get_softc(dev); 325 326 DPRINTF("\n"); 327 328 mtx_lock(&sc->sc_mtx); 329 usb_callout_stop(&sc->sc_callout); 330 usb_callout_stop(&sc->sc_watchdog); 331 mtx_unlock(&sc->sc_mtx); 332 333 usbd_transfer_unsetup(sc->sc_xfer, G_MODEM_N_TRANSFER); 334 335 usb_callout_drain(&sc->sc_callout); 336 usb_callout_drain(&sc->sc_watchdog); 337 338 mtx_destroy(&sc->sc_mtx); 339 340 return (0); 341 } 342 343 static void 344 g_modem_intr_callback(struct usb_xfer *xfer, usb_error_t error) 345 { 346 int actlen; 347 int aframes; 348 349 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 350 351 DPRINTF("st=%d aframes=%d actlen=%d bytes\n", 352 USB_GET_STATE(xfer), aframes, actlen); 353 354 switch (USB_GET_STATE(xfer)) { 355 case USB_ST_TRANSFERRED: 356 break; 357 358 case USB_ST_SETUP: 359 tr_setup: 360 break; 361 362 default: /* Error */ 363 DPRINTF("error=%s\n", usbd_errstr(error)); 364 365 if (error != USB_ERR_CANCELLED) { 366 /* try to clear stall first */ 367 usbd_xfer_set_stall(xfer); 368 goto tr_setup; 369 } 370 break; 371 } 372 } 373 374 static void 375 g_modem_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 376 { 377 struct g_modem_softc *sc = usbd_xfer_softc(xfer); 378 int actlen; 379 int aframes; 380 int mod; 381 int x; 382 int max; 383 384 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 385 386 DPRINTF("st=%d aframes=%d actlen=%d bytes\n", 387 USB_GET_STATE(xfer), aframes, actlen); 388 389 switch (USB_GET_STATE(xfer)) { 390 case USB_ST_TRANSFERRED: 391 392 sc->sc_tx_busy = 0; 393 sc->sc_throughput += actlen; 394 395 if (sc->sc_mode == G_MODEM_MODE_LOOP) { 396 /* start loop */ 397 usbd_transfer_start(sc->sc_xfer[G_MODEM_BULK_RD]); 398 break; 399 } else if ((sc->sc_mode == G_MODEM_MODE_PATTERN) && (sc->sc_tx_interval != 0)) { 400 /* wait for next timeout */ 401 break; 402 } 403 case USB_ST_SETUP: 404 tr_setup: 405 if (sc->sc_mode == G_MODEM_MODE_PATTERN) { 406 407 mod = sc->sc_pattern_len; 408 max = sc->sc_tx_interval ? mod : G_MODEM_BUFSIZE; 409 410 if (mod == 0) { 411 for (x = 0; x != max; x++) 412 sc->sc_data_buf[x] = x % 255; 413 } else { 414 for (x = 0; x != max; x++) 415 sc->sc_data_buf[x] = sc->sc_pattern[x % mod]; 416 } 417 418 usbd_xfer_set_frame_data(xfer, 0, sc->sc_data_buf, max); 419 usbd_xfer_set_interval(xfer, 0); 420 usbd_xfer_set_frames(xfer, 1); 421 usbd_transfer_submit(xfer); 422 423 } else if (sc->sc_mode == G_MODEM_MODE_LOOP) { 424 425 if (sc->sc_tx_busy == 0) 426 break; 427 428 x = sc->sc_tx_interval; 429 430 if (x < 0) 431 x = 0; 432 else if (x > 256) 433 x = 256; 434 435 usbd_xfer_set_frame_data(xfer, 0, sc->sc_data_buf, sc->sc_data_len); 436 usbd_xfer_set_interval(xfer, x); 437 usbd_xfer_set_frames(xfer, 1); 438 usbd_transfer_submit(xfer); 439 } else { 440 sc->sc_tx_busy = 0; 441 } 442 break; 443 444 default: /* Error */ 445 DPRINTF("error=%s\n", usbd_errstr(error)); 446 447 if (error != USB_ERR_CANCELLED) { 448 /* try to clear stall first */ 449 usbd_xfer_set_stall(xfer); 450 goto tr_setup; 451 } 452 break; 453 } 454 } 455 456 static void 457 g_modem_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 458 { 459 struct g_modem_softc *sc = usbd_xfer_softc(xfer); 460 int actlen; 461 int aframes; 462 463 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 464 465 DPRINTF("st=%d aframes=%d actlen=%d bytes\n", 466 USB_GET_STATE(xfer), aframes, actlen); 467 468 switch (USB_GET_STATE(xfer)) { 469 case USB_ST_TRANSFERRED: 470 471 sc->sc_throughput += actlen; 472 473 if (sc->sc_mode == G_MODEM_MODE_LOOP) { 474 sc->sc_tx_busy = 1; 475 sc->sc_data_len = actlen; 476 usbd_transfer_start(sc->sc_xfer[G_MODEM_BULK_WR]); 477 break; 478 } 479 480 case USB_ST_SETUP: 481 tr_setup: 482 if ((sc->sc_mode == G_MODEM_MODE_SILENT) || 483 (sc->sc_tx_busy != 0)) 484 break; 485 486 usbd_xfer_set_frame_data(xfer, 0, sc->sc_data_buf, G_MODEM_BUFSIZE); 487 usbd_xfer_set_frames(xfer, 1); 488 usbd_transfer_submit(xfer); 489 break; 490 491 default: /* Error */ 492 DPRINTF("error=%s\n", usbd_errstr(error)); 493 494 if (error != USB_ERR_CANCELLED) { 495 /* try to clear stall first */ 496 usbd_xfer_set_stall(xfer); 497 goto tr_setup; 498 } 499 break; 500 } 501 } 502 503 504 static int 505 g_modem_handle_request(device_t dev, 506 const void *preq, void **pptr, uint16_t *plen, 507 uint16_t offset, uint8_t *pstate) 508 { 509 struct g_modem_softc *sc = device_get_softc(dev); 510 const struct usb_device_request *req = preq; 511 uint8_t is_complete = *pstate; 512 513 if (!is_complete) { 514 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 515 (req->bRequest == UCDC_SET_LINE_CODING) && 516 (req->wValue[0] == 0x00) && 517 (req->wValue[1] == 0x00)) { 518 519 if (offset == 0) { 520 *plen = sizeof(sc->sc_line_coding); 521 *pptr = &sc->sc_line_coding; 522 } else { 523 *plen = 0; 524 } 525 return (0); 526 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 527 (req->bRequest == UCDC_SET_COMM_FEATURE)) { 528 529 if (offset == 0) { 530 *plen = sizeof(sc->sc_abstract_state); 531 *pptr = &sc->sc_abstract_state; 532 } else { 533 *plen = 0; 534 } 535 return (0); 536 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 537 (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) { 538 *plen = 0; 539 return (0); 540 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 541 (req->bRequest == UCDC_SEND_BREAK)) { 542 *plen = 0; 543 return (0); 544 } 545 } 546 return (ENXIO); /* use builtin handler */ 547 } 548