2 * QEMU USB audio device
5 * H. Peter Anvin <hpa@linux.intel.com>
6 * Gerd Hoffmann <kraxel@redhat.com>
8 * lousely based on usb net device code which is:
10 * Copyright (c) 2006 Thomas Sailer
11 * Copyright (c) 2008 Andrzej Zaborowski
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 #include "qemu/osdep.h"
33 #include "qemu-common.h"
35 #include "hw/usb/desc.h"
37 #include "audio/audio.h"
39 #define USBAUDIO_VENDOR_NUM 0x46f4 /* CRC16() of "QEMU" */
40 #define USBAUDIO_PRODUCT_NUM 0x0002
42 #define DEV_CONFIG_VALUE 1 /* The one and only */
44 /* Descriptor subtypes for AC interfaces */
45 #define DST_AC_HEADER 1
46 #define DST_AC_INPUT_TERMINAL 2
47 #define DST_AC_OUTPUT_TERMINAL 3
48 #define DST_AC_FEATURE_UNIT 6
49 /* Descriptor subtypes for AS interfaces */
50 #define DST_AS_GENERAL 1
51 #define DST_AS_FORMAT_TYPE 2
52 /* Descriptor subtypes for endpoints */
53 #define DST_EP_GENERAL 1
55 enum usb_audio_strings
{
61 STRING_USBAUDIO_CONTROL
,
62 STRING_INPUT_TERMINAL
,
64 STRING_OUTPUT_TERMINAL
,
69 static const USBDescStrings usb_audio_stringtable
= {
70 [STRING_MANUFACTURER
] = "QEMU",
71 [STRING_PRODUCT
] = "QEMU USB Audio",
72 [STRING_SERIALNUMBER
] = "1",
73 [STRING_CONFIG
] = "Audio Configuration",
74 [STRING_USBAUDIO_CONTROL
] = "Audio Device",
75 [STRING_INPUT_TERMINAL
] = "Audio Output Pipe",
76 [STRING_FEATURE_UNIT
] = "Audio Output Volume Control",
77 [STRING_OUTPUT_TERMINAL
] = "Audio Output Terminal",
78 [STRING_NULL_STREAM
] = "Audio Output - Disabled",
79 [STRING_REAL_STREAM
] = "Audio Output - 48 kHz Stereo",
82 #define U16(x) ((x) & 0xff), (((x) >> 8) & 0xff)
83 #define U24(x) U16(x), (((x) >> 16) & 0xff)
84 #define U32(x) U24(x), (((x) >> 24) & 0xff)
87 * A Basic Audio Device uses these specific values
89 #define USBAUDIO_PACKET_SIZE 192
90 #define USBAUDIO_SAMPLE_RATE 48000
91 #define USBAUDIO_PACKET_INTERVAL 1
93 static const USBDescIface desc_iface
[] = {
95 .bInterfaceNumber
= 0,
97 .bInterfaceClass
= USB_CLASS_AUDIO
,
98 .bInterfaceSubClass
= USB_SUBCLASS_AUDIO_CONTROL
,
99 .bInterfaceProtocol
= 0x04,
100 .iInterface
= STRING_USBAUDIO_CONTROL
,
102 .descs
= (USBDescOther
[]) {
104 /* Headphone Class-Specific AC Interface Header Descriptor */
105 .data
= (uint8_t[]) {
106 0x09, /* u8 bLength */
107 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
108 DST_AC_HEADER
, /* u8 bDescriptorSubtype */
109 U16(0x0100), /* u16 bcdADC */
110 U16(0x2b), /* u16 wTotalLength */
111 0x01, /* u8 bInCollection */
112 0x01, /* u8 baInterfaceNr */
115 /* Generic Stereo Input Terminal ID1 Descriptor */
116 .data
= (uint8_t[]) {
117 0x0c, /* u8 bLength */
118 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
119 DST_AC_INPUT_TERMINAL
, /* u8 bDescriptorSubtype */
120 0x01, /* u8 bTerminalID */
121 U16(0x0101), /* u16 wTerminalType */
122 0x00, /* u8 bAssocTerminal */
123 0x02, /* u16 bNrChannels */
124 U16(0x0003), /* u16 wChannelConfig */
125 0x00, /* u8 iChannelNames */
126 STRING_INPUT_TERMINAL
, /* u8 iTerminal */
129 /* Generic Stereo Feature Unit ID2 Descriptor */
130 .data
= (uint8_t[]) {
131 0x0d, /* u8 bLength */
132 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
133 DST_AC_FEATURE_UNIT
, /* u8 bDescriptorSubtype */
134 0x02, /* u8 bUnitID */
135 0x01, /* u8 bSourceID */
136 0x02, /* u8 bControlSize */
137 U16(0x0001), /* u16 bmaControls(0) */
138 U16(0x0002), /* u16 bmaControls(1) */
139 U16(0x0002), /* u16 bmaControls(2) */
140 STRING_FEATURE_UNIT
, /* u8 iFeature */
143 /* Headphone Ouptut Terminal ID3 Descriptor */
144 .data
= (uint8_t[]) {
145 0x09, /* u8 bLength */
146 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
147 DST_AC_OUTPUT_TERMINAL
, /* u8 bDescriptorSubtype */
148 0x03, /* u8 bUnitID */
149 U16(0x0301), /* u16 wTerminalType (SPK) */
150 0x00, /* u8 bAssocTerminal */
151 0x02, /* u8 bSourceID */
152 STRING_OUTPUT_TERMINAL
, /* u8 iTerminal */
157 .bInterfaceNumber
= 1,
158 .bAlternateSetting
= 0,
160 .bInterfaceClass
= USB_CLASS_AUDIO
,
161 .bInterfaceSubClass
= USB_SUBCLASS_AUDIO_STREAMING
,
162 .iInterface
= STRING_NULL_STREAM
,
164 .bInterfaceNumber
= 1,
165 .bAlternateSetting
= 1,
167 .bInterfaceClass
= USB_CLASS_AUDIO
,
168 .bInterfaceSubClass
= USB_SUBCLASS_AUDIO_STREAMING
,
169 .iInterface
= STRING_REAL_STREAM
,
171 .descs
= (USBDescOther
[]) {
173 /* Headphone Class-specific AS General Interface Descriptor */
174 .data
= (uint8_t[]) {
175 0x07, /* u8 bLength */
176 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
177 DST_AS_GENERAL
, /* u8 bDescriptorSubtype */
178 0x01, /* u8 bTerminalLink */
179 0x00, /* u8 bDelay */
180 0x01, 0x00, /* u16 wFormatTag */
183 /* Headphone Type I Format Type Descriptor */
184 .data
= (uint8_t[]) {
185 0x0b, /* u8 bLength */
186 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
187 DST_AS_FORMAT_TYPE
, /* u8 bDescriptorSubtype */
188 0x01, /* u8 bFormatType */
189 0x02, /* u8 bNrChannels */
190 0x02, /* u8 bSubFrameSize */
191 0x10, /* u8 bBitResolution */
192 0x01, /* u8 bSamFreqType */
193 U24(USBAUDIO_SAMPLE_RATE
), /* u24 tSamFreq */
197 .eps
= (USBDescEndpoint
[]) {
199 .bEndpointAddress
= USB_DIR_OUT
| 0x01,
200 .bmAttributes
= 0x0d,
201 .wMaxPacketSize
= USBAUDIO_PACKET_SIZE
,
204 /* Stereo Headphone Class-specific
205 AS Audio Data Endpoint Descriptor */
206 .extra
= (uint8_t[]) {
207 0x07, /* u8 bLength */
208 USB_DT_CS_ENDPOINT
, /* u8 bDescriptorType */
209 DST_EP_GENERAL
, /* u8 bDescriptorSubtype */
210 0x00, /* u8 bmAttributes */
211 0x00, /* u8 bLockDelayUnits */
212 U16(0x0000), /* u16 wLockDelay */
219 static const USBDescDevice desc_device
= {
221 .bMaxPacketSize0
= 64,
222 .bNumConfigurations
= 1,
223 .confs
= (USBDescConfig
[]) {
226 .bConfigurationValue
= DEV_CONFIG_VALUE
,
227 .iConfiguration
= STRING_CONFIG
,
228 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
230 .nif
= ARRAY_SIZE(desc_iface
),
236 static const USBDesc desc_audio
= {
238 .idVendor
= USBAUDIO_VENDOR_NUM
,
239 .idProduct
= USBAUDIO_PRODUCT_NUM
,
241 .iManufacturer
= STRING_MANUFACTURER
,
242 .iProduct
= STRING_PRODUCT
,
243 .iSerialNumber
= STRING_SERIALNUMBER
,
245 .full
= &desc_device
,
246 .str
= usb_audio_stringtable
,
250 * A USB audio device supports an arbitrary number of alternate
251 * interface settings for each interface. Each corresponds to a block
252 * diagram of parameterized blocks. This can thus refer to things like
253 * number of channels, data rates, or in fact completely different
254 * block diagrams. Alternative setting 0 is always the null block diagram,
255 * which is used by a disabled device.
257 enum usb_audio_altset
{
258 ALTSET_OFF
= 0x00, /* No endpoint */
259 ALTSET_ON
= 0x01, /* Single endpoint */
263 * Class-specific control requests
265 #define CR_SET_CUR 0x01
266 #define CR_GET_CUR 0x81
267 #define CR_SET_MIN 0x02
268 #define CR_GET_MIN 0x82
269 #define CR_SET_MAX 0x03
270 #define CR_GET_MAX 0x83
271 #define CR_SET_RES 0x04
272 #define CR_GET_RES 0x84
273 #define CR_SET_MEM 0x05
274 #define CR_GET_MEM 0x85
275 #define CR_GET_STAT 0xff
278 * Feature Unit Control Selectors
280 #define MUTE_CONTROL 0x01
281 #define VOLUME_CONTROL 0x02
282 #define BASS_CONTROL 0x03
283 #define MID_CONTROL 0x04
284 #define TREBLE_CONTROL 0x05
285 #define GRAPHIC_EQUALIZER_CONTROL 0x06
286 #define AUTOMATIC_GAIN_CONTROL 0x07
287 #define DELAY_CONTROL 0x08
288 #define BASS_BOOST_CONTROL 0x09
289 #define LOUDNESS_CONTROL 0x0a
302 static void streambuf_init(struct streambuf
*buf
, uint32_t size
)
305 buf
->size
= size
- (size
% USBAUDIO_PACKET_SIZE
);
306 buf
->data
= g_malloc(buf
->size
);
311 static void streambuf_fini(struct streambuf
*buf
)
317 static int streambuf_put(struct streambuf
*buf
, USBPacket
*p
)
319 uint32_t free
= buf
->size
- (buf
->prod
- buf
->cons
);
324 assert(free
>= USBAUDIO_PACKET_SIZE
);
325 usb_packet_copy(p
, buf
->data
+ (buf
->prod
% buf
->size
),
326 USBAUDIO_PACKET_SIZE
);
327 buf
->prod
+= USBAUDIO_PACKET_SIZE
;
328 return USBAUDIO_PACKET_SIZE
;
331 static uint8_t *streambuf_get(struct streambuf
*buf
)
333 uint32_t used
= buf
->prod
- buf
->cons
;
339 assert(used
>= USBAUDIO_PACKET_SIZE
);
340 data
= buf
->data
+ (buf
->cons
% buf
->size
);
341 buf
->cons
+= USBAUDIO_PACKET_SIZE
;
345 typedef struct USBAudioState
{
346 /* qemu interfaces */
352 enum usb_audio_altset altset
;
353 struct audsettings as
;
357 struct streambuf buf
;
365 #define TYPE_USB_AUDIO "usb-audio"
366 #define USB_AUDIO(obj) OBJECT_CHECK(USBAudioState, (obj), TYPE_USB_AUDIO)
368 static void output_callback(void *opaque
, int avail
)
370 USBAudioState
*s
= opaque
;
374 if (avail
< USBAUDIO_PACKET_SIZE
) {
377 data
= streambuf_get(&s
->out
.buf
);
381 AUD_write(s
->out
.voice
, data
, USBAUDIO_PACKET_SIZE
);
382 avail
-= USBAUDIO_PACKET_SIZE
;
386 static int usb_audio_set_output_altset(USBAudioState
*s
, int altset
)
390 streambuf_init(&s
->out
.buf
, s
->buffer
);
391 AUD_set_active_out(s
->out
.voice
, false);
394 AUD_set_active_out(s
->out
.voice
, true);
401 fprintf(stderr
, "usb-audio: set interface %d\n", altset
);
403 s
->out
.altset
= altset
;
408 * Note: we arbitrarily map the volume control range onto -inf..+8 dB
410 #define ATTRIB_ID(cs, attrib, idif) \
411 (((cs) << 24) | ((attrib) << 16) | (idif))
413 static int usb_audio_get_control(USBAudioState
*s
, uint8_t attrib
,
414 uint16_t cscn
, uint16_t idif
,
415 int length
, uint8_t *data
)
417 uint8_t cs
= cscn
>> 8;
418 uint8_t cn
= cscn
- 1; /* -1 for the non-present master control */
419 uint32_t aid
= ATTRIB_ID(cs
, attrib
, idif
);
420 int ret
= USB_RET_STALL
;
423 case ATTRIB_ID(MUTE_CONTROL
, CR_GET_CUR
, 0x0200):
424 data
[0] = s
->out
.mute
;
427 case ATTRIB_ID(VOLUME_CONTROL
, CR_GET_CUR
, 0x0200):
429 uint16_t vol
= (s
->out
.vol
[cn
] * 0x8800 + 127) / 255 + 0x8000;
435 case ATTRIB_ID(VOLUME_CONTROL
, CR_GET_MIN
, 0x0200):
442 case ATTRIB_ID(VOLUME_CONTROL
, CR_GET_MAX
, 0x0200):
449 case ATTRIB_ID(VOLUME_CONTROL
, CR_GET_RES
, 0x0200):
460 static int usb_audio_set_control(USBAudioState
*s
, uint8_t attrib
,
461 uint16_t cscn
, uint16_t idif
,
462 int length
, uint8_t *data
)
464 uint8_t cs
= cscn
>> 8;
465 uint8_t cn
= cscn
- 1; /* -1 for the non-present master control */
466 uint32_t aid
= ATTRIB_ID(cs
, attrib
, idif
);
467 int ret
= USB_RET_STALL
;
468 bool set_vol
= false;
471 case ATTRIB_ID(MUTE_CONTROL
, CR_SET_CUR
, 0x0200):
472 s
->out
.mute
= data
[0] & 1;
476 case ATTRIB_ID(VOLUME_CONTROL
, CR_SET_CUR
, 0x0200):
478 uint16_t vol
= data
[0] + (data
[1] << 8);
481 fprintf(stderr
, "usb-audio: vol %04x\n", (uint16_t)vol
);
485 vol
= (vol
* 255 + 0x4400) / 0x8800;
490 s
->out
.vol
[cn
] = vol
;
499 fprintf(stderr
, "usb-audio: mute %d, lvol %3d, rvol %3d\n",
500 s
->out
.mute
, s
->out
.vol
[0], s
->out
.vol
[1]);
502 AUD_set_volume_out(s
->out
.voice
, s
->out
.mute
,
503 s
->out
.vol
[0], s
->out
.vol
[1]);
509 static void usb_audio_handle_control(USBDevice
*dev
, USBPacket
*p
,
510 int request
, int value
, int index
,
511 int length
, uint8_t *data
)
513 USBAudioState
*s
= USB_AUDIO(dev
);
517 fprintf(stderr
, "usb-audio: control transaction: "
518 "request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
519 request
, value
, index
, length
);
522 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
528 case ClassInterfaceRequest
| CR_GET_CUR
:
529 case ClassInterfaceRequest
| CR_GET_MIN
:
530 case ClassInterfaceRequest
| CR_GET_MAX
:
531 case ClassInterfaceRequest
| CR_GET_RES
:
532 ret
= usb_audio_get_control(s
, request
& 0xff, value
, index
,
536 fprintf(stderr
, "usb-audio: fail: get control\n");
540 p
->actual_length
= ret
;
543 case ClassInterfaceOutRequest
| CR_SET_CUR
:
544 case ClassInterfaceOutRequest
| CR_SET_MIN
:
545 case ClassInterfaceOutRequest
| CR_SET_MAX
:
546 case ClassInterfaceOutRequest
| CR_SET_RES
:
547 ret
= usb_audio_set_control(s
, request
& 0xff, value
, index
,
551 fprintf(stderr
, "usb-audio: fail: set control\n");
560 fprintf(stderr
, "usb-audio: failed control transaction: "
561 "request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
562 request
, value
, index
, length
);
564 p
->status
= USB_RET_STALL
;
569 static void usb_audio_set_interface(USBDevice
*dev
, int iface
,
572 USBAudioState
*s
= USB_AUDIO(dev
);
575 usb_audio_set_output_altset(s
, value
);
579 static void usb_audio_handle_reset(USBDevice
*dev
)
581 USBAudioState
*s
= USB_AUDIO(dev
);
584 fprintf(stderr
, "usb-audio: reset\n");
586 usb_audio_set_output_altset(s
, ALTSET_OFF
);
589 static void usb_audio_handle_dataout(USBAudioState
*s
, USBPacket
*p
)
591 if (s
->out
.altset
== ALTSET_OFF
) {
592 p
->status
= USB_RET_STALL
;
596 streambuf_put(&s
->out
.buf
, p
);
597 if (p
->actual_length
< p
->iov
.size
&& s
->debug
> 1) {
598 fprintf(stderr
, "usb-audio: output overrun (%zd bytes)\n",
599 p
->iov
.size
- p
->actual_length
);
603 static void usb_audio_handle_data(USBDevice
*dev
, USBPacket
*p
)
605 USBAudioState
*s
= (USBAudioState
*) dev
;
607 if (p
->pid
== USB_TOKEN_OUT
&& p
->ep
->nr
== 1) {
608 usb_audio_handle_dataout(s
, p
);
612 p
->status
= USB_RET_STALL
;
614 fprintf(stderr
, "usb-audio: failed data transaction: "
615 "pid 0x%x ep 0x%x len 0x%zx\n",
616 p
->pid
, p
->ep
->nr
, p
->iov
.size
);
620 static void usb_audio_unrealize(USBDevice
*dev
, Error
**errp
)
622 USBAudioState
*s
= USB_AUDIO(dev
);
625 fprintf(stderr
, "usb-audio: destroy\n");
628 usb_audio_set_output_altset(s
, ALTSET_OFF
);
629 AUD_close_out(&s
->card
, s
->out
.voice
);
630 AUD_remove_card(&s
->card
);
632 streambuf_fini(&s
->out
.buf
);
635 static void usb_audio_realize(USBDevice
*dev
, Error
**errp
)
637 USBAudioState
*s
= USB_AUDIO(dev
);
639 usb_desc_create_serial(dev
);
642 AUD_register_card(TYPE_USB_AUDIO
, &s
->card
);
644 s
->out
.altset
= ALTSET_OFF
;
646 s
->out
.vol
[0] = 240; /* 0 dB */
647 s
->out
.vol
[1] = 240; /* 0 dB */
648 s
->out
.as
.freq
= USBAUDIO_SAMPLE_RATE
;
649 s
->out
.as
.nchannels
= 2;
650 s
->out
.as
.fmt
= AUD_FMT_S16
;
651 s
->out
.as
.endianness
= 0;
652 streambuf_init(&s
->out
.buf
, s
->buffer
);
654 s
->out
.voice
= AUD_open_out(&s
->card
, s
->out
.voice
, TYPE_USB_AUDIO
,
655 s
, output_callback
, &s
->out
.as
);
656 AUD_set_volume_out(s
->out
.voice
, s
->out
.mute
, s
->out
.vol
[0], s
->out
.vol
[1]);
657 AUD_set_active_out(s
->out
.voice
, 0);
660 static const VMStateDescription vmstate_usb_audio
= {
661 .name
= TYPE_USB_AUDIO
,
665 static Property usb_audio_properties
[] = {
666 DEFINE_PROP_UINT32("debug", USBAudioState
, debug
, 0),
667 DEFINE_PROP_UINT32("buffer", USBAudioState
, buffer
,
668 32 * USBAUDIO_PACKET_SIZE
),
669 DEFINE_PROP_END_OF_LIST(),
672 static void usb_audio_class_init(ObjectClass
*klass
, void *data
)
674 DeviceClass
*dc
= DEVICE_CLASS(klass
);
675 USBDeviceClass
*k
= USB_DEVICE_CLASS(klass
);
677 dc
->vmsd
= &vmstate_usb_audio
;
678 dc
->props
= usb_audio_properties
;
679 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
680 k
->product_desc
= "QEMU USB Audio Interface";
681 k
->usb_desc
= &desc_audio
;
682 k
->realize
= usb_audio_realize
;
683 k
->handle_reset
= usb_audio_handle_reset
;
684 k
->handle_control
= usb_audio_handle_control
;
685 k
->handle_data
= usb_audio_handle_data
;
686 k
->unrealize
= usb_audio_unrealize
;
687 k
->set_interface
= usb_audio_set_interface
;
690 static const TypeInfo usb_audio_info
= {
691 .name
= TYPE_USB_AUDIO
,
692 .parent
= TYPE_USB_DEVICE
,
693 .instance_size
= sizeof(USBAudioState
),
694 .class_init
= usb_audio_class_init
,
697 static void usb_audio_register_types(void)
699 type_register_static(&usb_audio_info
);
700 usb_legacy_register(TYPE_USB_AUDIO
, "audio", NULL
);
703 type_init(usb_audio_register_types
)