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