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