32
#include "qemu/osdep.h"
33
#include "qemu/module.h"
34
#include "hw/qdev-properties.h"
36
#include "migration/vmstate.h"
38
#include "audio/audio.h"
39
#include "qom/object.h"
41
static void usb_audio_reinit(USBDevice *dev, unsigned channels);
43
#define USBAUDIO_VENDOR_NUM 0x46f4
44
#define USBAUDIO_PRODUCT_NUM 0x0002
46
#define DEV_CONFIG_VALUE 1
48
#define USBAUDIO_MAX_CHANNELS(s) (s->multi ? 8 : 2)
51
#define DST_AC_HEADER 1
52
#define DST_AC_INPUT_TERMINAL 2
53
#define DST_AC_OUTPUT_TERMINAL 3
54
#define DST_AC_FEATURE_UNIT 6
56
#define DST_AS_GENERAL 1
57
#define DST_AS_FORMAT_TYPE 2
59
#define DST_EP_GENERAL 1
61
enum usb_audio_strings {
67
STRING_USBAUDIO_CONTROL,
68
STRING_INPUT_TERMINAL,
70
STRING_OUTPUT_TERMINAL,
75
static const USBDescStrings usb_audio_stringtable = {
76
[STRING_MANUFACTURER] = "QEMU",
77
[STRING_PRODUCT] = "QEMU USB Audio",
78
[STRING_SERIALNUMBER] = "1",
79
[STRING_CONFIG] = "Audio Configuration",
80
[STRING_USBAUDIO_CONTROL] = "Audio Device",
81
[STRING_INPUT_TERMINAL] = "Audio Output Pipe",
82
[STRING_FEATURE_UNIT] = "Audio Output Volume Control",
83
[STRING_OUTPUT_TERMINAL] = "Audio Output Terminal",
84
[STRING_NULL_STREAM] = "Audio Output - Disabled",
85
[STRING_REAL_STREAM] = "Audio Output - 48 kHz Stereo",
96
enum usb_audio_altset {
103
static unsigned altset_channels[] = {
109
#define U16(x) ((x) & 0xff), (((x) >> 8) & 0xff)
110
#define U24(x) U16(x), (((x) >> 16) & 0xff)
111
#define U32(x) U24(x), (((x) >> 24) & 0xff)
116
#define USBAUDIO_PACKET_SIZE_BASE 96
117
#define USBAUDIO_PACKET_SIZE(channels) (USBAUDIO_PACKET_SIZE_BASE * channels)
118
#define USBAUDIO_SAMPLE_RATE 48000
119
#define USBAUDIO_PACKET_INTERVAL 1
121
static const USBDescIface desc_iface[] = {
123
.bInterfaceNumber = 0,
125
.bInterfaceClass = USB_CLASS_AUDIO,
126
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL,
127
.iInterface = STRING_USBAUDIO_CONTROL,
129
.descs = (USBDescOther[]) {
132
.data = (uint8_t[]) {
143
.data = (uint8_t[]) {
146
DST_AC_INPUT_TERMINAL,
153
STRING_INPUT_TERMINAL,
157
.data = (uint8_t[]) {
171
.data = (uint8_t[]) {
174
DST_AC_OUTPUT_TERMINAL,
179
STRING_OUTPUT_TERMINAL,
184
.bInterfaceNumber = 1,
185
.bAlternateSetting = ALTSET_OFF,
187
.bInterfaceClass = USB_CLASS_AUDIO,
188
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
189
.iInterface = STRING_NULL_STREAM,
191
.bInterfaceNumber = 1,
192
.bAlternateSetting = ALTSET_STEREO,
194
.bInterfaceClass = USB_CLASS_AUDIO,
195
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
196
.iInterface = STRING_REAL_STREAM,
198
.descs = (USBDescOther[]) {
201
.data = (uint8_t[]) {
211
.data = (uint8_t[]) {
220
U24(USBAUDIO_SAMPLE_RATE),
224
.eps = (USBDescEndpoint[]) {
226
.bEndpointAddress = USB_DIR_OUT | 0x01,
227
.bmAttributes = 0x0d,
228
.wMaxPacketSize = USBAUDIO_PACKET_SIZE(2),
233
.extra = (uint8_t[]) {
246
static const USBDescDevice desc_device = {
248
.bMaxPacketSize0 = 64,
249
.bNumConfigurations = 1,
250
.confs = (USBDescConfig[]) {
253
.bConfigurationValue = DEV_CONFIG_VALUE,
254
.iConfiguration = STRING_CONFIG,
255
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
257
.nif = ARRAY_SIZE(desc_iface),
263
static const USBDesc desc_audio = {
265
.idVendor = USBAUDIO_VENDOR_NUM,
266
.idProduct = USBAUDIO_PRODUCT_NUM,
268
.iManufacturer = STRING_MANUFACTURER,
269
.iProduct = STRING_PRODUCT,
270
.iSerialNumber = STRING_SERIALNUMBER,
272
.full = &desc_device,
273
.str = usb_audio_stringtable,
278
static const USBDescIface desc_iface_multi[] = {
280
.bInterfaceNumber = 0,
282
.bInterfaceClass = USB_CLASS_AUDIO,
283
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL,
284
.iInterface = STRING_USBAUDIO_CONTROL,
286
.descs = (USBDescOther[]) {
289
.data = (uint8_t[]) {
300
.data = (uint8_t[]) {
303
DST_AC_INPUT_TERMINAL,
310
STRING_INPUT_TERMINAL,
314
.data = (uint8_t[]) {
334
.data = (uint8_t[]) {
337
DST_AC_OUTPUT_TERMINAL,
342
STRING_OUTPUT_TERMINAL,
347
.bInterfaceNumber = 1,
348
.bAlternateSetting = ALTSET_OFF,
350
.bInterfaceClass = USB_CLASS_AUDIO,
351
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
352
.iInterface = STRING_NULL_STREAM,
354
.bInterfaceNumber = 1,
355
.bAlternateSetting = ALTSET_STEREO,
357
.bInterfaceClass = USB_CLASS_AUDIO,
358
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
359
.iInterface = STRING_REAL_STREAM,
361
.descs = (USBDescOther[]) {
364
.data = (uint8_t[]) {
374
.data = (uint8_t[]) {
383
U24(USBAUDIO_SAMPLE_RATE),
387
.eps = (USBDescEndpoint[]) {
389
.bEndpointAddress = USB_DIR_OUT | 0x01,
390
.bmAttributes = 0x0d,
391
.wMaxPacketSize = USBAUDIO_PACKET_SIZE(2),
396
.extra = (uint8_t[]) {
407
.bInterfaceNumber = 1,
408
.bAlternateSetting = ALTSET_51,
410
.bInterfaceClass = USB_CLASS_AUDIO,
411
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
412
.iInterface = STRING_REAL_STREAM,
414
.descs = (USBDescOther[]) {
417
.data = (uint8_t[]) {
427
.data = (uint8_t[]) {
436
U24(USBAUDIO_SAMPLE_RATE),
440
.eps = (USBDescEndpoint[]) {
442
.bEndpointAddress = USB_DIR_OUT | 0x01,
443
.bmAttributes = 0x0d,
444
.wMaxPacketSize = USBAUDIO_PACKET_SIZE(6),
449
.extra = (uint8_t[]) {
460
.bInterfaceNumber = 1,
461
.bAlternateSetting = ALTSET_71,
463
.bInterfaceClass = USB_CLASS_AUDIO,
464
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
465
.iInterface = STRING_REAL_STREAM,
467
.descs = (USBDescOther[]) {
470
.data = (uint8_t[]) {
480
.data = (uint8_t[]) {
489
U24(USBAUDIO_SAMPLE_RATE),
493
.eps = (USBDescEndpoint[]) {
495
.bEndpointAddress = USB_DIR_OUT | 0x01,
496
.bmAttributes = 0x0d,
497
.wMaxPacketSize = USBAUDIO_PACKET_SIZE(8),
502
.extra = (uint8_t[]) {
515
static const USBDescDevice desc_device_multi = {
517
.bMaxPacketSize0 = 64,
518
.bNumConfigurations = 1,
519
.confs = (USBDescConfig[]) {
522
.bConfigurationValue = DEV_CONFIG_VALUE,
523
.iConfiguration = STRING_CONFIG,
524
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
526
.nif = ARRAY_SIZE(desc_iface_multi),
527
.ifs = desc_iface_multi,
532
static const USBDesc desc_audio_multi = {
534
.idVendor = USBAUDIO_VENDOR_NUM,
535
.idProduct = USBAUDIO_PRODUCT_NUM,
537
.iManufacturer = STRING_MANUFACTURER,
538
.iProduct = STRING_PRODUCT,
539
.iSerialNumber = STRING_SERIALNUMBER,
541
.full = &desc_device_multi,
542
.str = usb_audio_stringtable,
548
#define CR_SET_CUR 0x01
549
#define CR_GET_CUR 0x81
550
#define CR_SET_MIN 0x02
551
#define CR_GET_MIN 0x82
552
#define CR_SET_MAX 0x03
553
#define CR_GET_MAX 0x83
554
#define CR_SET_RES 0x04
555
#define CR_GET_RES 0x84
556
#define CR_SET_MEM 0x05
557
#define CR_GET_MEM 0x85
558
#define CR_GET_STAT 0xff
563
#define MUTE_CONTROL 0x01
564
#define VOLUME_CONTROL 0x02
565
#define BASS_CONTROL 0x03
566
#define MID_CONTROL 0x04
567
#define TREBLE_CONTROL 0x05
568
#define GRAPHIC_EQUALIZER_CONTROL 0x06
569
#define AUTOMATIC_GAIN_CONTROL 0x07
570
#define DELAY_CONTROL 0x08
571
#define BASS_BOOST_CONTROL 0x09
572
#define LOUDNESS_CONTROL 0x0a
585
static void streambuf_init(struct streambuf *buf, uint32_t size,
589
buf->size = size - (size % USBAUDIO_PACKET_SIZE(channels));
590
buf->data = g_malloc(buf->size);
595
static void streambuf_fini(struct streambuf *buf)
601
static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)
603
int64_t free = buf->size - (buf->prod - buf->cons);
605
if (free < USBAUDIO_PACKET_SIZE(channels)) {
608
if (p->iov.size != USBAUDIO_PACKET_SIZE(channels)) {
613
assert(buf->prod % USBAUDIO_PACKET_SIZE(channels) == 0);
614
usb_packet_copy(p, buf->data + (buf->prod % buf->size),
615
USBAUDIO_PACKET_SIZE(channels));
616
buf->prod += USBAUDIO_PACKET_SIZE(channels);
617
return USBAUDIO_PACKET_SIZE(channels);
620
static uint8_t *streambuf_get(struct streambuf *buf, size_t *len)
622
int64_t used = buf->prod - buf->cons;
629
data = buf->data + (buf->cons % buf->size);
630
*len = MIN(buf->prod - buf->cons,
631
buf->size - (buf->cons % buf->size));
635
struct USBAudioState {
642
enum usb_audio_altset altset;
643
struct audsettings as;
646
struct streambuf buf;
652
uint32_t buffer_user, buffer;
656
#define TYPE_USB_AUDIO "usb-audio"
657
OBJECT_DECLARE_SIMPLE_TYPE(USBAudioState, USB_AUDIO)
659
static void output_callback(void *opaque, int avail)
661
USBAudioState *s = opaque;
667
data = streambuf_get(&s->out.buf, &len);
672
written = AUD_write(s->out.voice, data, len);
674
s->out.buf.cons += written;
682
static int usb_audio_set_output_altset(USBAudioState *s, int altset)
686
AUD_set_active_out(s->out.voice, false);
691
if (s->out.channels != altset_channels[altset]) {
692
usb_audio_reinit(USB_DEVICE(s), altset_channels[altset]);
694
streambuf_init(&s->out.buf, s->buffer, s->out.channels);
695
AUD_set_active_out(s->out.voice, true);
702
fprintf(stderr, "usb-audio: set interface %d\n", altset);
704
s->out.altset = altset;
711
#define ATTRIB_ID(cs, attrib, idif) \
712
(((cs) << 24) | ((attrib) << 16) | (idif))
714
static int usb_audio_get_control(USBAudioState *s, uint8_t attrib,
715
uint16_t cscn, uint16_t idif,
716
int length, uint8_t *data)
718
uint8_t cs = cscn >> 8;
719
uint8_t cn = cscn - 1;
720
uint32_t aid = ATTRIB_ID(cs, attrib, idif);
721
int ret = USB_RET_STALL;
724
case ATTRIB_ID(MUTE_CONTROL, CR_GET_CUR, 0x0200):
725
data[0] = s->out.vol.mute;
728
case ATTRIB_ID(VOLUME_CONTROL, CR_GET_CUR, 0x0200):
729
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
730
uint16_t vol = (s->out.vol.vol[cn] * 0x8800 + 127) / 255 + 0x8000;
736
case ATTRIB_ID(VOLUME_CONTROL, CR_GET_MIN, 0x0200):
737
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
743
case ATTRIB_ID(VOLUME_CONTROL, CR_GET_MAX, 0x0200):
744
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
750
case ATTRIB_ID(VOLUME_CONTROL, CR_GET_RES, 0x0200):
751
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
761
static int usb_audio_set_control(USBAudioState *s, uint8_t attrib,
762
uint16_t cscn, uint16_t idif,
763
int length, uint8_t *data)
765
uint8_t cs = cscn >> 8;
766
uint8_t cn = cscn - 1;
767
uint32_t aid = ATTRIB_ID(cs, attrib, idif);
768
int ret = USB_RET_STALL;
769
bool set_vol = false;
772
case ATTRIB_ID(MUTE_CONTROL, CR_SET_CUR, 0x0200):
773
s->out.vol.mute = data[0] & 1;
777
case ATTRIB_ID(VOLUME_CONTROL, CR_SET_CUR, 0x0200):
778
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
779
uint16_t vol = data[0] + (data[1] << 8);
782
fprintf(stderr, "usb-audio: cn %d vol %04x\n", cn,
787
vol = (vol * 255 + 0x4400) / 0x8800;
792
s->out.vol.vol[cn] = vol;
802
fprintf(stderr, "usb-audio: mute %d", s->out.vol.mute);
803
for (i = 0; i < USBAUDIO_MAX_CHANNELS(s); ++i) {
804
fprintf(stderr, ", vol[%d] %3d", i, s->out.vol.vol[i]);
806
fprintf(stderr, "\n");
808
audio_set_volume_out(s->out.voice, &s->out.vol);
814
static void usb_audio_handle_control(USBDevice *dev, USBPacket *p,
815
int request, int value, int index,
816
int length, uint8_t *data)
818
USBAudioState *s = USB_AUDIO(dev);
822
fprintf(stderr, "usb-audio: control transaction: "
823
"request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
824
request, value, index, length);
827
ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
833
case ClassInterfaceRequest | CR_GET_CUR:
834
case ClassInterfaceRequest | CR_GET_MIN:
835
case ClassInterfaceRequest | CR_GET_MAX:
836
case ClassInterfaceRequest | CR_GET_RES:
837
ret = usb_audio_get_control(s, request & 0xff, value, index,
841
fprintf(stderr, "usb-audio: fail: get control\n");
845
p->actual_length = ret;
848
case ClassInterfaceOutRequest | CR_SET_CUR:
849
case ClassInterfaceOutRequest | CR_SET_MIN:
850
case ClassInterfaceOutRequest | CR_SET_MAX:
851
case ClassInterfaceOutRequest | CR_SET_RES:
852
ret = usb_audio_set_control(s, request & 0xff, value, index,
856
fprintf(stderr, "usb-audio: fail: set control\n");
865
fprintf(stderr, "usb-audio: failed control transaction: "
866
"request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
867
request, value, index, length);
869
p->status = USB_RET_STALL;
874
static void usb_audio_set_interface(USBDevice *dev, int iface,
877
USBAudioState *s = USB_AUDIO(dev);
880
usb_audio_set_output_altset(s, value);
884
static void usb_audio_handle_reset(USBDevice *dev)
886
USBAudioState *s = USB_AUDIO(dev);
889
fprintf(stderr, "usb-audio: reset\n");
891
usb_audio_set_output_altset(s, ALTSET_OFF);
894
static void usb_audio_handle_dataout(USBAudioState *s, USBPacket *p)
896
if (s->out.altset == ALTSET_OFF) {
897
p->status = USB_RET_STALL;
901
streambuf_put(&s->out.buf, p, s->out.channels);
902
if (p->actual_length < p->iov.size && s->debug > 1) {
903
fprintf(stderr, "usb-audio: output overrun (%zd bytes)\n",
904
p->iov.size - p->actual_length);
908
static void usb_audio_handle_data(USBDevice *dev, USBPacket *p)
910
USBAudioState *s = (USBAudioState *) dev;
912
if (p->pid == USB_TOKEN_OUT && p->ep->nr == 1) {
913
usb_audio_handle_dataout(s, p);
917
p->status = USB_RET_STALL;
919
fprintf(stderr, "usb-audio: failed data transaction: "
920
"pid 0x%x ep 0x%x len 0x%zx\n",
921
p->pid, p->ep->nr, p->iov.size);
925
static void usb_audio_unrealize(USBDevice *dev)
927
USBAudioState *s = USB_AUDIO(dev);
930
fprintf(stderr, "usb-audio: destroy\n");
933
usb_audio_set_output_altset(s, ALTSET_OFF);
934
AUD_close_out(&s->card, s->out.voice);
935
AUD_remove_card(&s->card);
937
streambuf_fini(&s->out.buf);
940
static void usb_audio_realize(USBDevice *dev, Error **errp)
942
USBAudioState *s = USB_AUDIO(dev);
945
if (!AUD_register_card(TYPE_USB_AUDIO, &s->card, errp)) {
949
dev->usb_desc = s->multi ? &desc_audio_multi : &desc_audio;
951
usb_desc_create_serial(dev);
955
s->out.altset = ALTSET_OFF;
956
s->out.vol.mute = false;
957
for (i = 0; i < USBAUDIO_MAX_CHANNELS(s); ++i) {
958
s->out.vol.vol[i] = 240;
961
usb_audio_reinit(dev, 2);
964
static void usb_audio_reinit(USBDevice *dev, unsigned channels)
966
USBAudioState *s = USB_AUDIO(dev);
968
s->out.channels = channels;
969
if (!s->buffer_user) {
970
s->buffer = 32 * USBAUDIO_PACKET_SIZE(s->out.channels);
972
s->buffer = s->buffer_user;
975
s->out.vol.channels = s->out.channels;
976
s->out.as.freq = USBAUDIO_SAMPLE_RATE;
977
s->out.as.nchannels = s->out.channels;
978
s->out.as.fmt = AUDIO_FORMAT_S16;
979
s->out.as.endianness = 0;
980
streambuf_init(&s->out.buf, s->buffer, s->out.channels);
982
s->out.voice = AUD_open_out(&s->card, s->out.voice, TYPE_USB_AUDIO,
983
s, output_callback, &s->out.as);
984
audio_set_volume_out(s->out.voice, &s->out.vol);
985
AUD_set_active_out(s->out.voice, 0);
988
static const VMStateDescription vmstate_usb_audio = {
989
.name = TYPE_USB_AUDIO,
993
static Property usb_audio_properties[] = {
994
DEFINE_AUDIO_PROPERTIES(USBAudioState, card),
995
DEFINE_PROP_UINT32("debug", USBAudioState, debug, 0),
996
DEFINE_PROP_UINT32("buffer", USBAudioState, buffer_user, 0),
997
DEFINE_PROP_BOOL("multi", USBAudioState, multi, false),
998
DEFINE_PROP_END_OF_LIST(),
1001
static void usb_audio_class_init(ObjectClass *klass, void *data)
1003
DeviceClass *dc = DEVICE_CLASS(klass);
1004
USBDeviceClass *k = USB_DEVICE_CLASS(klass);
1006
dc->vmsd = &vmstate_usb_audio;
1007
device_class_set_props(dc, usb_audio_properties);
1008
set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
1009
k->product_desc = "QEMU USB Audio Interface";
1010
k->realize = usb_audio_realize;
1011
k->handle_reset = usb_audio_handle_reset;
1012
k->handle_control = usb_audio_handle_control;
1013
k->handle_data = usb_audio_handle_data;
1014
k->unrealize = usb_audio_unrealize;
1015
k->set_interface = usb_audio_set_interface;
1018
static const TypeInfo usb_audio_info = {
1019
.name = TYPE_USB_AUDIO,
1020
.parent = TYPE_USB_DEVICE,
1021
.instance_size = sizeof(USBAudioState),
1022
.class_init = usb_audio_class_init,
1025
static void usb_audio_register_types(void)
1027
type_register_static(&usb_audio_info);
1030
type_init(usb_audio_register_types)