2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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
34 #include <sys/param.h>
35 __FBSDID("$FreeBSD$");
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>
43 #include <sys/linker_set.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
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>
62 #define USB_DEBUG_VAR g_audio_debug
63 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/gadget/g_audio.h>
75 struct g_audio_softc {
77 struct usb_callout sc_callout;
78 struct usb_callout sc_watchdog;
79 struct usb_xfer *sc_xfer[G_AUDIO_N_TRANSFER];
88 int8_t sc_pattern[G_AUDIO_MAX_STRLEN];
90 uint16_t sc_data_len[2][G_AUDIO_FRAMES];
92 int16_t sc_data_buf[2][G_AUDIO_BUFSIZE / 2];
94 uint8_t sc_volume_setting[32];
95 uint8_t sc_volume_limit[32];
96 uint8_t sc_sample_rate[32];
99 static SYSCTL_NODE(_hw_usb, OID_AUTO, g_audio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
103 static int g_audio_debug = 0;
105 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, debug, CTLFLAG_RWTUN,
106 &g_audio_debug, 0, "Debug level");
109 static int g_audio_mode = 0;
111 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, mode, CTLFLAG_RWTUN,
112 &g_audio_mode, 0, "Mode selection");
114 static int g_audio_pattern_interval = 1000;
116 SYSCTL_INT(_hw_usb_g_audio, OID_AUTO, pattern_interval, CTLFLAG_RWTUN,
117 &g_audio_pattern_interval, 0, "Pattern interval in milliseconds");
119 static char g_audio_pattern_data[G_AUDIO_MAX_STRLEN];
121 SYSCTL_STRING(_hw_usb_g_audio, OID_AUTO, pattern, CTLFLAG_RW,
122 &g_audio_pattern_data, sizeof(g_audio_pattern_data), "Data pattern");
124 static int g_audio_throughput;
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");
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;
134 static usb_callback_t g_audio_isoc_read_callback;
135 static usb_callback_t g_audio_isoc_write_callback;
137 static void g_audio_watchdog(void *arg);
138 static void g_audio_timeout(void *arg);
140 static device_method_t g_audio_methods[] = {
142 DEVMETHOD(usb_handle_request, g_audio_handle_request),
144 /* Device interface */
145 DEVMETHOD(device_probe, g_audio_probe),
146 DEVMETHOD(device_attach, g_audio_attach),
147 DEVMETHOD(device_detach, g_audio_detach),
152 static driver_t g_audio_driver = {
154 .methods = g_audio_methods,
155 .size = sizeof(struct g_audio_softc),
158 DRIVER_MODULE(g_audio, uhub, g_audio_driver, 0, 0);
159 MODULE_DEPEND(g_audio, usb, 1, 1, 1);
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,
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,
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,
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,
212 g_audio_timeout_reset(struct g_audio_softc *sc)
214 int i = g_audio_pattern_interval;
216 sc->sc_tx_interval = i;
223 i = USB_MS_TO_TICKS(i);
225 usb_callout_reset(&sc->sc_callout, i, &g_audio_timeout, sc);
229 g_audio_timeout(void *arg)
231 struct g_audio_softc *sc = arg;
233 sc->sc_mode = g_audio_mode;
235 memcpy(sc->sc_pattern, g_audio_pattern_data, sizeof(sc->sc_pattern));
237 sc->sc_pattern[G_AUDIO_MAX_STRLEN - 1] = 0;
239 sc->sc_pattern_len = strlen(sc->sc_pattern);
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]);
245 g_audio_timeout_reset(sc);
249 g_audio_watchdog_reset(struct g_audio_softc *sc)
251 usb_callout_reset(&sc->sc_watchdog, hz, &g_audio_watchdog, sc);
255 g_audio_watchdog(void *arg)
257 struct g_audio_softc *sc = arg;
260 i = sc->sc_throughput;
262 sc->sc_throughput = 0;
264 g_audio_throughput = i;
266 g_audio_watchdog_reset(sc);
270 g_audio_probe(device_t dev)
272 struct usb_attach_arg *uaa = device_get_ivars(dev);
276 if (uaa->usb_mode != USB_MODE_DEVICE)
279 if ((uaa->info.bInterfaceClass == UICLASS_AUDIO) &&
280 (uaa->info.bInterfaceSubClass == UISUBCLASS_AUDIOCONTROL))
287 g_audio_attach(device_t dev)
289 struct g_audio_softc *sc = device_get_softc(dev);
290 struct usb_attach_arg *uaa = device_get_ivars(dev);
293 uint8_t iface_index[3];
297 device_set_usb_desc(dev);
299 mtx_init(&sc->sc_mtx, "g_audio", NULL, MTX_DEF);
301 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
302 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
304 sc->sc_mode = G_AUDIO_MODE_SILENT;
306 sc->sc_noise_rem = 1;
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;
313 iface_index[0] = uaa->info.bIfaceIndex;
314 iface_index[1] = uaa->info.bIfaceIndex + 1;
315 iface_index[2] = uaa->info.bIfaceIndex + 2;
317 error = usbd_set_alt_interface_index(uaa->device, iface_index[1], 1);
319 DPRINTF("alt iface setting error=%s\n", usbd_errstr(error));
322 error = usbd_set_alt_interface_index(uaa->device, iface_index[2], 1);
324 DPRINTF("alt iface setting error=%s\n", usbd_errstr(error));
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);
332 DPRINTF("error=%s\n", usbd_errstr(error));
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]);
338 mtx_lock(&sc->sc_mtx);
340 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC0_RD]);
341 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC1_RD]);
343 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC0_WR]);
344 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC1_WR]);
346 g_audio_timeout_reset(sc);
348 g_audio_watchdog_reset(sc);
350 mtx_unlock(&sc->sc_mtx);
352 return (0); /* success */
357 return (ENXIO); /* error */
361 g_audio_detach(device_t dev)
363 struct g_audio_softc *sc = device_get_softc(dev);
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);
372 usbd_transfer_unsetup(sc->sc_xfer, G_AUDIO_N_TRANSFER);
374 usb_callout_drain(&sc->sc_callout);
375 usb_callout_drain(&sc->sc_watchdog);
377 mtx_destroy(&sc->sc_mtx);
383 g_noise(struct g_audio_softc *sc)
386 const uint32_t prime = 0xFFFF1D;
388 if (sc->sc_noise_rem & 1) {
389 sc->sc_noise_rem += prime;
391 sc->sc_noise_rem /= 2;
393 temp = sc->sc_noise_rem;
395 /* unsigned to signed conversion */
398 if (temp & 0x800000) {
405 g_audio_make_samples(struct g_audio_softc *sc, int16_t *ptr, int samples)
410 for (i = 0; i != samples; i++) {
413 if ((sc->sc_state < 0) || (sc->sc_state >= sc->sc_pattern_len))
416 if (sc->sc_pattern_len != 0) {
417 j = (j * sc->sc_pattern[sc->sc_state]) >> 16;
426 g_audio_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error)
428 struct g_audio_softc *sc = usbd_xfer_softc(xfer);
431 int nr = (xfer == sc->sc_xfer[G_AUDIO_ISOC0_WR]) ? 0 : 1;
435 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
437 DPRINTF("st=%d aframes=%d actlen=%d bytes\n",
438 USB_GET_STATE(xfer), aframes, actlen);
440 switch (USB_GET_STATE(xfer)) {
441 case USB_ST_TRANSFERRED:
443 sc->sc_throughput += actlen;
445 if (sc->sc_mode == G_AUDIO_MODE_LOOP)
446 break; /* sync with RX */
451 ptr = sc->sc_data_buf[nr];
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]);
457 g_audio_make_samples(sc, ptr, (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2);
459 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2;
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);
465 g_audio_make_samples(sc, ptr, sc->sc_data_len[nr][i] / 4);
467 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2;
473 DPRINTF("error=%s\n", usbd_errstr(error));
475 if (error != USB_ERR_CANCELLED) {
476 /* try to clear stall first */
477 usbd_xfer_set_stall(xfer);
485 g_audio_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error)
487 struct g_audio_softc *sc = usbd_xfer_softc(xfer);
490 int nr = (xfer == sc->sc_xfer[G_AUDIO_ISOC0_RD]) ? 0 : 1;
494 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
496 DPRINTF("st=%d aframes=%d actlen=%d bytes\n",
497 USB_GET_STATE(xfer), aframes, actlen);
499 switch (USB_GET_STATE(xfer)) {
500 case USB_ST_TRANSFERRED:
502 sc->sc_throughput += actlen;
504 for (i = 0; i != G_AUDIO_FRAMES; i++) {
505 sc->sc_data_len[nr][i] = usbd_xfer_frame_len(xfer, i);
508 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC0_WR]);
509 usbd_transfer_start(sc->sc_xfer[G_AUDIO_ISOC1_WR]);
515 ptr = sc->sc_data_buf[nr];
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);
521 ptr += (G_AUDIO_BUFSIZE / G_AUDIO_FRAMES) / 2;
524 usbd_transfer_submit(xfer);
528 DPRINTF("error=%s\n", usbd_errstr(error));
530 if (error != USB_ERR_CANCELLED) {
531 /* try to clear stall first */
532 usbd_xfer_set_stall(xfer);
540 g_audio_handle_request(device_t dev,
541 const void *preq, void **pptr, uint16_t *plen,
542 uint16_t offset, uint8_t *pstate)
544 struct g_audio_softc *sc = device_get_softc(dev);
545 const struct usb_device_request *req = preq;
546 uint8_t is_complete = *pstate;
549 if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
550 (req->bRequest == 0x82 /* get min */ )) {
552 USETW(sc->sc_volume_limit, 0);
554 *pptr = &sc->sc_volume_limit;
559 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
560 (req->bRequest == 0x83 /* get max */ )) {
562 USETW(sc->sc_volume_limit, 0x2000);
564 *pptr = &sc->sc_volume_limit;
569 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
570 (req->bRequest == 0x84 /* get residue */ )) {
572 USETW(sc->sc_volume_limit, 1);
574 *pptr = &sc->sc_volume_limit;
579 } else if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
580 (req->bRequest == 0x81 /* get value */ )) {
582 USETW(sc->sc_volume_setting, 0x2000);
583 *plen = sizeof(sc->sc_volume_setting);
584 *pptr = &sc->sc_volume_setting;
589 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
590 (req->bRequest == 0x01 /* set value */ )) {
592 *plen = sizeof(sc->sc_volume_setting);
593 *pptr = &sc->sc_volume_setting;
598 } else if ((req->bmRequestType == UT_WRITE_CLASS_ENDPOINT) &&
599 (req->bRequest == 0x01 /* set value */ )) {
601 *plen = sizeof(sc->sc_sample_rate);
602 *pptr = &sc->sc_sample_rate;
609 return (ENXIO); /* use builtin handler */