1 /*- 2 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 /* 27 * USB audio specs: http://www.usb.org/developers/devclass_docs/audio10.pdf 28 * http://www.usb.org/developers/devclass_docs/frmts10.pdf 29 * http://www.usb.org/developers/devclass_docs/termt10.pdf 30 */ 31 32 #include <sys/param.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/stdint.h> 36 #include <sys/stddef.h> 37 #include <sys/queue.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/bus.h> 41 #include <sys/linker_set.h> 42 #include <sys/module.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/condvar.h> 46 #include <sys/sysctl.h> 47 #include <sys/sx.h> 48 #include <sys/unistd.h> 49 #include <sys/callout.h> 50 #include <sys/malloc.h> 51 #include <sys/priv.h> 52 53 #include <dev/usb/usb.h> 54 #include <dev/usb/usb_cdc.h> 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbhid.h> 58 #include "usb_if.h" 59 60 #define USB_DEBUG_VAR g_audio_debug 61 #include <dev/usb/usb_debug.h> 62 63 #include <dev/usb/gadget/g_audio.h> 64 65 enum { 66 G_AUDIO_ISOC0_RD, 67 G_AUDIO_ISOC1_RD, 68 G_AUDIO_ISOC0_WR, 69 G_AUDIO_ISOC1_WR, 70 G_AUDIO_N_TRANSFER, 71 }; 72 73 struct g_audio_softc { 74 struct mtx sc_mtx; 75 struct usb_callout sc_callout; 76 struct usb_callout sc_watchdog; 77 struct usb_xfer *sc_xfer[G_AUDIO_N_TRANSFER]; 78 79 int sc_mode; 80 int sc_pattern_len; 81 int sc_throughput; 82 int sc_tx_interval; 83 int sc_state; 84 int sc_noise_rem; 85 86 int8_t sc_pattern[G_AUDIO_MAX_STRLEN]; 87 88 uint16_t sc_data_len[2][G_AUDIO_FRAMES]; 89 90 int16_t sc_data_buf[2][G_AUDIO_BUFSIZE / 2]; 91 92 uint8_t sc_volume_setting[32]; 93 uint8_t sc_volume_limit[32]; 94 uint8_t sc_sample_rate[32]; 95 }; 96 97 static SYSCTL_NODE(_hw_usb, OID_AUTO, g_audio, CTLFLAG_RW, 0, "USB audio gadget"); 98 99 #ifdef USB_DEBUG 100 static int g_audio_debug = 0; 101 102 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, debug, CTLFLAG_RWTUN, 103 &g_audio_debug, 0, "Debug level"); 104 #endif 105 106 static int g_audio_mode = 0; 107 108 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, mode, CTLFLAG_RWTUN, 109 &g_audio_mode, 0, "Mode selection"); 110 111 static int g_audio_pattern_interval = 1000; 112 113 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, pattern_interval, CTLFLAG_RWTUN, 114 &g_audio_pattern_interval, 0, "Pattern interval in milliseconds"); 115 116 static char g_audio_pattern_data[G_AUDIO_MAX_STRLEN]; 117 118 SYSCTL_STRING(_hw_usb_g_audio, OID_AUTO, pattern, CTLFLAG_RW, 119 &g_audio_pattern_data, sizeof(g_audio_pattern_data), "Data pattern"); 120 121 static int g_audio_throughput; 122 123 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, throughput, CTLFLAG_RD, 124 &g_audio_throughput, sizeof(g_audio_throughput), "Throughput in bytes per second"); 125 126 static device_probe_t g_audio_probe; 127 static device_attach_t g_audio_attach; 128 static device_detach_t g_audio_detach; 129 static usb_handle_request_t g_audio_handle_request; 130 131 static usb_callback_t g_audio_isoc_read_callback; 132 static usb_callback_t g_audio_isoc_write_callback; 133 134 static devclass_t g_audio_devclass; 135 136 static void g_audio_watchdog(void *arg); 137 static void g_audio_timeout(void *arg); 138 139 static device_method_t g_audio_methods[] = { 140 /* USB interface */ 141 DEVMETHOD(usb_handle_request, g_audio_handle_request), 142 143 /* Device interface */ 144 DEVMETHOD(device_probe, g_audio_probe), 145 DEVMETHOD(device_attach, g_audio_attach), 146 DEVMETHOD(device_detach, g_audio_detach), 147 148 DEVMETHOD_END 149 }; 150 151 static driver_t g_audio_driver = { 152 .name = "g_audio", 153 .methods = g_audio_methods, 154 .size = sizeof(struct g_audio_softc), 155 }; 156 157 DRIVER_MODULE(g_audio, uhub, g_audio_driver, g_audio_devclass, 0, 0); 158 MODULE_DEPEND(g_audio, usb, 1, 1, 1); 159 160 static const struct usb_config g_audio_config[G_AUDIO_N_TRANSFER] = { 161 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 383 static int32_t 384 g_noise(struct g_audio_softc *sc) 385 { 386 uint32_t temp; 387 const uint32_t prime = 0xFFFF1D; 388 389 if (sc->sc_noise_rem & 1) { 390 sc->sc_noise_rem += prime; 391 } 392 sc->sc_noise_rem /= 2; 393 394 temp = sc->sc_noise_rem; 395 396 /* unsigned to signed conversion */ 397 398 temp ^= 0x800000; 399 if (temp & 0x800000) { 400 temp |= (-0x800000); 401 } 402 return temp; 403 } 404 405 static void 406 g_audio_make_samples(struct g_audio_softc *sc, int16_t *ptr, int samples) 407 { 408 int i; 409 int j; 410 411 for (i = 0; i != samples; i++) { 412 413 j = g_noise(sc); 414 415 if ((sc->sc_state < 0) || (sc->sc_state >= sc->sc_pattern_len)) 416 sc->sc_state = 0; 417 418 if (sc->sc_pattern_len != 0) { 419 j = (j * sc->sc_pattern[sc->sc_state]) >> 16; 420 sc->sc_state++; 421 } 422 *ptr++ = j / 256; 423 *ptr++ = j / 256; 424 } 425 } 426 427 static void 428 g_audio_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error) 429 { 430 struct g_audio_softc *sc = usbd_xfer_softc(xfer); 431 int actlen; 432 int aframes; 433 int nr = (xfer == sc->sc_xfer[G_AUDIO_ISOC0_WR]) ? 0 : 1; 434 int16_t *ptr; 435 int i; 436 437 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 438 439 DPRINTF("st=%d aframes=%d actlen=%d bytes\n", 440 USB_GET_STATE(xfer), aframes, actlen); 441 442 switch (USB_GET_STATE(xfer)) { 443 case USB_ST_TRANSFERRED: 444 445 sc->sc_throughput += actlen; 446 447 if (sc->sc_mode == G_AUDIO_MODE_LOOP) 448 break; /* sync with RX */ 449 450 case USB_ST_SETUP: 451 tr_setup: 452 453 ptr = sc->sc_data_buf[nr]; 454 455 if (sc->sc_mode == G_AUDIO_MODE_PATTERN) { 456 457 for (i = 0; i != G_AUDIO_FRAMES; i++) { 458 459 usbd_xfer_set_frame_data(xfer, i, ptr, sc->sc_data_len[nr][i]); 460 461 g_audio_make_samples(sc, ptr, (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2); 462 463 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2; 464 } 465 } else if (sc->sc_mode == G_AUDIO_MODE_LOOP) { 466 467 for (i = 0; i != G_AUDIO_FRAMES; i++) { 468 469 usbd_xfer_set_frame_data(xfer, i, ptr, sc->sc_data_len[nr][i] & ~3); 470 471 g_audio_make_samples(sc, ptr, sc->sc_data_len[nr][i] / 4); 472 473 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2; 474 } 475 } 476 break; 477 478 default: /* Error */ 479 DPRINTF("error=%s\n", usbd_errstr(error)); 480 481 if (error != USB_ERR_CANCELLED) { 482 /* try to clear stall first */ 483 usbd_xfer_set_stall(xfer); 484 goto tr_setup; 485 } 486 break; 487 } 488 } 489 490 static void 491 g_audio_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error) 492 { 493 struct g_audio_softc *sc = usbd_xfer_softc(xfer); 494 int actlen; 495 int aframes; 496 int nr = (xfer == sc->sc_xfer[G_AUDIO_ISOC0_RD]) ? 0 : 1; 497 int16_t *ptr; 498 int i; 499 500 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 501 502 DPRINTF("st=%d aframes=%d actlen=%d bytes\n", 503 USB_GET_STATE(xfer), aframes, actlen); 504 505 switch (USB_GET_STATE(xfer)) { 506 case USB_ST_TRANSFERRED: 507 508 sc->sc_throughput += actlen; 509 510 for (i = 0; i != G_AUDIO_FRAMES; i++) { 511 sc->sc_data_len[nr][i] = usbd_xfer_frame_len(xfer, i); 512 } 513 514 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC0_WR]); 515 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC1_WR]); 516 517 break; 518 519 case USB_ST_SETUP: 520 tr_setup: 521 ptr = sc->sc_data_buf[nr]; 522 523 for (i = 0; i != G_AUDIO_FRAMES; i++) { 524 525 usbd_xfer_set_frame_data(xfer, i, ptr, 526 G_AUDIO_BUFSIZE / G_AUDIO_FRAMES); 527 528 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2; 529 } 530 531 usbd_transfer_submit(xfer); 532 break; 533 534 default: /* Error */ 535 DPRINTF("error=%s\n", usbd_errstr(error)); 536 537 if (error != USB_ERR_CANCELLED) { 538 /* try to clear stall first */ 539 usbd_xfer_set_stall(xfer); 540 goto tr_setup; 541 } 542 break; 543 } 544 } 545 546 547 static int 548 g_audio_handle_request(device_t dev, 549 const void *preq, void **pptr, uint16_t *plen, 550 uint16_t offset, uint8_t *pstate) 551 { 552 struct g_audio_softc *sc = device_get_softc(dev); 553 const struct usb_device_request *req = preq; 554 uint8_t is_complete = *pstate; 555 556 if (!is_complete) { 557 if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) && 558 (req->bRequest == 0x82 /* get min */ )) { 559 560 if (offset == 0) { 561 USETW(sc->sc_volume_limit, 0); 562 *plen = 2; 563 *pptr = &sc->sc_volume_limit; 564 } else { 565 *plen = 0; 566 } 567 return (0); 568 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) && 569 (req->bRequest == 0x83 /* get max */ )) { 570 571 if (offset == 0) { 572 USETW(sc->sc_volume_limit, 0x2000); 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 == 0x84 /* get residue */ )) { 581 582 if (offset == 0) { 583 USETW(sc->sc_volume_limit, 1); 584 *plen = 2; 585 *pptr = &sc->sc_volume_limit; 586 } else { 587 *plen = 0; 588 } 589 return (0); 590 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) && 591 (req->bRequest == 0x81 /* get value */ )) { 592 593 if (offset == 0) { 594 USETW(sc->sc_volume_setting, 0x2000); 595 *plen = sizeof(sc->sc_volume_setting); 596 *pptr = &sc->sc_volume_setting; 597 } else { 598 *plen = 0; 599 } 600 return (0); 601 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 602 (req->bRequest == 0x01 /* set value */ )) { 603 604 if (offset == 0) { 605 *plen = sizeof(sc->sc_volume_setting); 606 *pptr = &sc->sc_volume_setting; 607 } else { 608 *plen = 0; 609 } 610 return (0); 611 } else if ((req->bmRequestType == UT_WRITE_CLASS_ENDPOINT) && 612 (req->bRequest == 0x01 /* set value */ )) { 613 614 if (offset == 0) { 615 *plen = sizeof(sc->sc_sample_rate); 616 *pptr = &sc->sc_sample_rate; 617 } else { 618 *plen = 0; 619 } 620 return (0); 621 } 622 } 623 return (ENXIO); /* use builtin handler */ 624 } 625