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 * USB audio specs: http://www.usb.org/developers/devclass_docs/audio10.pdf 30 * http://www.usb.org/developers/devclass_docs/frmts10.pdf 31 * http://www.usb.org/developers/devclass_docs/termt10.pdf 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_audio_debug 63 #include <dev/usb/usb_debug.h> 64 65 #include <dev/usb/gadget/g_audio.h> 66 67 enum { 68 G_AUDIO_ISOC0_RD, 69 G_AUDIO_ISOC1_RD, 70 G_AUDIO_ISOC0_WR, 71 G_AUDIO_ISOC1_WR, 72 G_AUDIO_N_TRANSFER, 73 }; 74 75 struct g_audio_softc { 76 struct mtx sc_mtx; 77 struct usb_callout sc_callout; 78 struct usb_callout sc_watchdog; 79 struct usb_xfer *sc_xfer[G_AUDIO_N_TRANSFER]; 80 81 int sc_mode; 82 int sc_pattern_len; 83 int sc_throughput; 84 int sc_tx_interval; 85 int sc_state; 86 int sc_noise_rem; 87 88 int8_t sc_pattern[G_AUDIO_MAX_STRLEN]; 89 90 uint16_t sc_data_len[2][G_AUDIO_FRAMES]; 91 92 int16_t sc_data_buf[2][G_AUDIO_BUFSIZE / 2]; 93 94 uint8_t sc_volume_setting[32]; 95 uint8_t sc_volume_limit[32]; 96 uint8_t sc_sample_rate[32]; 97 }; 98 99 static SYSCTL_NODE(_hw_usb, OID_AUTO, g_audio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 100 "USB audio gadget"); 101 102 #ifdef USB_DEBUG 103 static int g_audio_debug = 0; 104 105 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, debug, CTLFLAG_RWTUN, 106 &g_audio_debug, 0, "Debug level"); 107 #endif 108 109 static int g_audio_mode = 0; 110 111 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, mode, CTLFLAG_RWTUN, 112 &g_audio_mode, 0, "Mode selection"); 113 114 static int g_audio_pattern_interval = 1000; 115 116 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, pattern_interval, CTLFLAG_RWTUN, 117 &g_audio_pattern_interval, 0, "Pattern interval in milliseconds"); 118 119 static char g_audio_pattern_data[G_AUDIO_MAX_STRLEN]; 120 121 SYSCTL_STRING(_hw_usb_g_audio, OID_AUTO, pattern, CTLFLAG_RW, 122 &g_audio_pattern_data, sizeof(g_audio_pattern_data), "Data pattern"); 123 124 static int g_audio_throughput; 125 126 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, throughput, CTLFLAG_RD, 127 &g_audio_throughput, sizeof(g_audio_throughput), "Throughput in bytes per second"); 128 129 static device_probe_t g_audio_probe; 130 static device_attach_t g_audio_attach; 131 static device_detach_t g_audio_detach; 132 static usb_handle_request_t g_audio_handle_request; 133 134 static usb_callback_t g_audio_isoc_read_callback; 135 static usb_callback_t g_audio_isoc_write_callback; 136 137 static devclass_t g_audio_devclass; 138 139 static void g_audio_watchdog(void *arg); 140 static void g_audio_timeout(void *arg); 141 142 static device_method_t g_audio_methods[] = { 143 /* USB interface */ 144 DEVMETHOD(usb_handle_request, g_audio_handle_request), 145 146 /* Device interface */ 147 DEVMETHOD(device_probe, g_audio_probe), 148 DEVMETHOD(device_attach, g_audio_attach), 149 DEVMETHOD(device_detach, g_audio_detach), 150 151 DEVMETHOD_END 152 }; 153 154 static driver_t g_audio_driver = { 155 .name = "g_audio", 156 .methods = g_audio_methods, 157 .size = sizeof(struct g_audio_softc), 158 }; 159 160 DRIVER_MODULE(g_audio, uhub, g_audio_driver, g_audio_devclass, 0, 0); 161 MODULE_DEPEND(g_audio, usb, 1, 1, 1); 162 163 static const struct usb_config g_audio_config[G_AUDIO_N_TRANSFER] = { 164 165 [G_AUDIO_ISOC0_RD] = { 166 .type = UE_ISOCHRONOUS, 167 .endpoint = UE_ADDR_ANY, 168 .direction = UE_DIR_RX, 169 .flags = {.ext_buffer = 1,.pipe_bof = 1,.short_xfer_ok = 1,}, 170 .bufsize = G_AUDIO_BUFSIZE, 171 .callback = &g_audio_isoc_read_callback, 172 .frames = G_AUDIO_FRAMES, 173 .usb_mode = USB_MODE_DEVICE, 174 .if_index = 1, 175 }, 176 177 [G_AUDIO_ISOC1_RD] = { 178 .type = UE_ISOCHRONOUS, 179 .endpoint = UE_ADDR_ANY, 180 .direction = UE_DIR_RX, 181 .flags = {.ext_buffer = 1,.pipe_bof = 1,.short_xfer_ok = 1,}, 182 .bufsize = G_AUDIO_BUFSIZE, 183 .callback = &g_audio_isoc_read_callback, 184 .frames = G_AUDIO_FRAMES, 185 .usb_mode = USB_MODE_DEVICE, 186 .if_index = 1, 187 }, 188 189 [G_AUDIO_ISOC0_WR] = { 190 .type = UE_ISOCHRONOUS, 191 .endpoint = UE_ADDR_ANY, 192 .direction = UE_DIR_TX, 193 .flags = {.ext_buffer = 1,.pipe_bof = 1,}, 194 .bufsize = G_AUDIO_BUFSIZE, 195 .callback = &g_audio_isoc_write_callback, 196 .frames = G_AUDIO_FRAMES, 197 .usb_mode = USB_MODE_DEVICE, 198 .if_index = 2, 199 }, 200 201 [G_AUDIO_ISOC1_WR] = { 202 .type = UE_ISOCHRONOUS, 203 .endpoint = UE_ADDR_ANY, 204 .direction = UE_DIR_TX, 205 .flags = {.ext_buffer = 1,.pipe_bof = 1,}, 206 .bufsize = G_AUDIO_BUFSIZE, 207 .callback = &g_audio_isoc_write_callback, 208 .frames = G_AUDIO_FRAMES, 209 .usb_mode = USB_MODE_DEVICE, 210 .if_index = 2, 211 }, 212 }; 213 214 static void 215 g_audio_timeout_reset(struct g_audio_softc *sc) 216 { 217 int i = g_audio_pattern_interval; 218 219 sc->sc_tx_interval = i; 220 221 if (i <= 0) 222 i = 1; 223 else if (i > 1023) 224 i = 1023; 225 226 i = USB_MS_TO_TICKS(i); 227 228 usb_callout_reset(&sc->sc_callout, i, &g_audio_timeout, sc); 229 } 230 231 static void 232 g_audio_timeout(void *arg) 233 { 234 struct g_audio_softc *sc = arg; 235 236 sc->sc_mode = g_audio_mode; 237 238 memcpy(sc->sc_pattern, g_audio_pattern_data, sizeof(sc->sc_pattern)); 239 240 sc->sc_pattern[G_AUDIO_MAX_STRLEN - 1] = 0; 241 242 sc->sc_pattern_len = strlen(sc->sc_pattern); 243 244 if (sc->sc_mode != G_AUDIO_MODE_LOOP) { 245 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC0_WR]); 246 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC1_WR]); 247 } 248 g_audio_timeout_reset(sc); 249 } 250 251 static void 252 g_audio_watchdog_reset(struct g_audio_softc *sc) 253 { 254 usb_callout_reset(&sc->sc_watchdog, hz, &g_audio_watchdog, sc); 255 } 256 257 static void 258 g_audio_watchdog(void *arg) 259 { 260 struct g_audio_softc *sc = arg; 261 int i; 262 263 i = sc->sc_throughput; 264 265 sc->sc_throughput = 0; 266 267 g_audio_throughput = i; 268 269 g_audio_watchdog_reset(sc); 270 } 271 272 static int 273 g_audio_probe(device_t dev) 274 { 275 struct usb_attach_arg *uaa = device_get_ivars(dev); 276 277 DPRINTFN(11, "\n"); 278 279 if (uaa->usb_mode != USB_MODE_DEVICE) 280 return (ENXIO); 281 282 if ((uaa->info.bInterfaceClass == UICLASS_AUDIO) && 283 (uaa->info.bInterfaceSubClass == UISUBCLASS_AUDIOCONTROL)) 284 return (0); 285 286 return (ENXIO); 287 } 288 289 static int 290 g_audio_attach(device_t dev) 291 { 292 struct g_audio_softc *sc = device_get_softc(dev); 293 struct usb_attach_arg *uaa = device_get_ivars(dev); 294 int error; 295 int i; 296 uint8_t iface_index[3]; 297 298 DPRINTFN(11, "\n"); 299 300 device_set_usb_desc(dev); 301 302 mtx_init(&sc->sc_mtx, "g_audio", NULL, MTX_DEF); 303 304 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); 305 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 306 307 sc->sc_mode = G_AUDIO_MODE_SILENT; 308 309 sc->sc_noise_rem = 1; 310 311 for (i = 0; i != G_AUDIO_FRAMES; i++) { 312 sc->sc_data_len[0][i] = G_AUDIO_BUFSIZE / G_AUDIO_FRAMES; 313 sc->sc_data_len[1][i] = G_AUDIO_BUFSIZE / G_AUDIO_FRAMES; 314 } 315 316 iface_index[0] = uaa->info.bIfaceIndex; 317 iface_index[1] = uaa->info.bIfaceIndex + 1; 318 iface_index[2] = uaa->info.bIfaceIndex + 2; 319 320 error = usbd_set_alt_interface_index(uaa->device, iface_index[1], 1); 321 if (error) { 322 DPRINTF("alt iface setting error=%s\n", usbd_errstr(error)); 323 goto detach; 324 } 325 error = usbd_set_alt_interface_index(uaa->device, iface_index[2], 1); 326 if (error) { 327 DPRINTF("alt iface setting error=%s\n", usbd_errstr(error)); 328 goto detach; 329 } 330 error = usbd_transfer_setup(uaa->device, 331 iface_index, sc->sc_xfer, g_audio_config, 332 G_AUDIO_N_TRANSFER, sc, &sc->sc_mtx); 333 334 if (error) { 335 DPRINTF("error=%s\n", usbd_errstr(error)); 336 goto detach; 337 } 338 usbd_set_parent_iface(uaa->device, iface_index[1], iface_index[0]); 339 usbd_set_parent_iface(uaa->device, iface_index[2], iface_index[0]); 340 341 mtx_lock(&sc->sc_mtx); 342 343 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC0_RD]); 344 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC1_RD]); 345 346 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC0_WR]); 347 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC1_WR]); 348 349 g_audio_timeout_reset(sc); 350 351 g_audio_watchdog_reset(sc); 352 353 mtx_unlock(&sc->sc_mtx); 354 355 return (0); /* success */ 356 357 detach: 358 g_audio_detach(dev); 359 360 return (ENXIO); /* error */ 361 } 362 363 static int 364 g_audio_detach(device_t dev) 365 { 366 struct g_audio_softc *sc = device_get_softc(dev); 367 368 DPRINTF("\n"); 369 370 mtx_lock(&sc->sc_mtx); 371 usb_callout_stop(&sc->sc_callout); 372 usb_callout_stop(&sc->sc_watchdog); 373 mtx_unlock(&sc->sc_mtx); 374 375 usbd_transfer_unsetup(sc->sc_xfer, G_AUDIO_N_TRANSFER); 376 377 usb_callout_drain(&sc->sc_callout); 378 usb_callout_drain(&sc->sc_watchdog); 379 380 mtx_destroy(&sc->sc_mtx); 381 382 return (0); 383 } 384 385 386 static int32_t 387 g_noise(struct g_audio_softc *sc) 388 { 389 uint32_t temp; 390 const uint32_t prime = 0xFFFF1D; 391 392 if (sc->sc_noise_rem & 1) { 393 sc->sc_noise_rem += prime; 394 } 395 sc->sc_noise_rem /= 2; 396 397 temp = sc->sc_noise_rem; 398 399 /* unsigned to signed conversion */ 400 401 temp ^= 0x800000; 402 if (temp & 0x800000) { 403 temp |= (-0x800000); 404 } 405 return temp; 406 } 407 408 static void 409 g_audio_make_samples(struct g_audio_softc *sc, int16_t *ptr, int samples) 410 { 411 int i; 412 int j; 413 414 for (i = 0; i != samples; i++) { 415 416 j = g_noise(sc); 417 418 if ((sc->sc_state < 0) || (sc->sc_state >= sc->sc_pattern_len)) 419 sc->sc_state = 0; 420 421 if (sc->sc_pattern_len != 0) { 422 j = (j * sc->sc_pattern[sc->sc_state]) >> 16; 423 sc->sc_state++; 424 } 425 *ptr++ = j / 256; 426 *ptr++ = j / 256; 427 } 428 } 429 430 static void 431 g_audio_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error) 432 { 433 struct g_audio_softc *sc = usbd_xfer_softc(xfer); 434 int actlen; 435 int aframes; 436 int nr = (xfer == sc->sc_xfer[G_AUDIO_ISOC0_WR]) ? 0 : 1; 437 int16_t *ptr; 438 int i; 439 440 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 441 442 DPRINTF("st=%d aframes=%d actlen=%d bytes\n", 443 USB_GET_STATE(xfer), aframes, actlen); 444 445 switch (USB_GET_STATE(xfer)) { 446 case USB_ST_TRANSFERRED: 447 448 sc->sc_throughput += actlen; 449 450 if (sc->sc_mode == G_AUDIO_MODE_LOOP) 451 break; /* sync with RX */ 452 453 case USB_ST_SETUP: 454 tr_setup: 455 456 ptr = sc->sc_data_buf[nr]; 457 458 if (sc->sc_mode == G_AUDIO_MODE_PATTERN) { 459 460 for (i = 0; i != G_AUDIO_FRAMES; i++) { 461 462 usbd_xfer_set_frame_data(xfer, i, ptr, sc->sc_data_len[nr][i]); 463 464 g_audio_make_samples(sc, ptr, (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2); 465 466 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2; 467 } 468 } else if (sc->sc_mode == G_AUDIO_MODE_LOOP) { 469 470 for (i = 0; i != G_AUDIO_FRAMES; i++) { 471 472 usbd_xfer_set_frame_data(xfer, i, ptr, sc->sc_data_len[nr][i] & ~3); 473 474 g_audio_make_samples(sc, ptr, sc->sc_data_len[nr][i] / 4); 475 476 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2; 477 } 478 } 479 break; 480 481 default: /* Error */ 482 DPRINTF("error=%s\n", usbd_errstr(error)); 483 484 if (error != USB_ERR_CANCELLED) { 485 /* try to clear stall first */ 486 usbd_xfer_set_stall(xfer); 487 goto tr_setup; 488 } 489 break; 490 } 491 } 492 493 static void 494 g_audio_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error) 495 { 496 struct g_audio_softc *sc = usbd_xfer_softc(xfer); 497 int actlen; 498 int aframes; 499 int nr = (xfer == sc->sc_xfer[G_AUDIO_ISOC0_RD]) ? 0 : 1; 500 int16_t *ptr; 501 int i; 502 503 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 504 505 DPRINTF("st=%d aframes=%d actlen=%d bytes\n", 506 USB_GET_STATE(xfer), aframes, actlen); 507 508 switch (USB_GET_STATE(xfer)) { 509 case USB_ST_TRANSFERRED: 510 511 sc->sc_throughput += actlen; 512 513 for (i = 0; i != G_AUDIO_FRAMES; i++) { 514 sc->sc_data_len[nr][i] = usbd_xfer_frame_len(xfer, i); 515 } 516 517 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC0_WR]); 518 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC1_WR]); 519 520 break; 521 522 case USB_ST_SETUP: 523 tr_setup: 524 ptr = sc->sc_data_buf[nr]; 525 526 for (i = 0; i != G_AUDIO_FRAMES; i++) { 527 528 usbd_xfer_set_frame_data(xfer, i, ptr, 529 G_AUDIO_BUFSIZE / G_AUDIO_FRAMES); 530 531 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2; 532 } 533 534 usbd_transfer_submit(xfer); 535 break; 536 537 default: /* Error */ 538 DPRINTF("error=%s\n", usbd_errstr(error)); 539 540 if (error != USB_ERR_CANCELLED) { 541 /* try to clear stall first */ 542 usbd_xfer_set_stall(xfer); 543 goto tr_setup; 544 } 545 break; 546 } 547 } 548 549 550 static int 551 g_audio_handle_request(device_t dev, 552 const void *preq, void **pptr, uint16_t *plen, 553 uint16_t offset, uint8_t *pstate) 554 { 555 struct g_audio_softc *sc = device_get_softc(dev); 556 const struct usb_device_request *req = preq; 557 uint8_t is_complete = *pstate; 558 559 if (!is_complete) { 560 if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) && 561 (req->bRequest == 0x82 /* get min */ )) { 562 563 if (offset == 0) { 564 USETW(sc->sc_volume_limit, 0); 565 *plen = 2; 566 *pptr = &sc->sc_volume_limit; 567 } else { 568 *plen = 0; 569 } 570 return (0); 571 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) && 572 (req->bRequest == 0x83 /* get max */ )) { 573 574 if (offset == 0) { 575 USETW(sc->sc_volume_limit, 0x2000); 576 *plen = 2; 577 *pptr = &sc->sc_volume_limit; 578 } else { 579 *plen = 0; 580 } 581 return (0); 582 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) && 583 (req->bRequest == 0x84 /* get residue */ )) { 584 585 if (offset == 0) { 586 USETW(sc->sc_volume_limit, 1); 587 *plen = 2; 588 *pptr = &sc->sc_volume_limit; 589 } else { 590 *plen = 0; 591 } 592 return (0); 593 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) && 594 (req->bRequest == 0x81 /* get value */ )) { 595 596 if (offset == 0) { 597 USETW(sc->sc_volume_setting, 0x2000); 598 *plen = sizeof(sc->sc_volume_setting); 599 *pptr = &sc->sc_volume_setting; 600 } else { 601 *plen = 0; 602 } 603 return (0); 604 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 605 (req->bRequest == 0x01 /* set value */ )) { 606 607 if (offset == 0) { 608 *plen = sizeof(sc->sc_volume_setting); 609 *pptr = &sc->sc_volume_setting; 610 } else { 611 *plen = 0; 612 } 613 return (0); 614 } else if ((req->bmRequestType == UT_WRITE_CLASS_ENDPOINT) && 615 (req->bRequest == 0x01 /* set value */ )) { 616 617 if (offset == 0) { 618 *plen = sizeof(sc->sc_sample_rate); 619 *pptr = &sc->sc_sample_rate; 620 } else { 621 *plen = 0; 622 } 623 return (0); 624 } 625 } 626 return (ENXIO); /* use builtin handler */ 627 } 628