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/module.h"
34 #include "hw/qdev-properties.h"
36 #include "migration/vmstate.h"
38 #include "audio/audio.h"
40 #define USBAUDIO_VENDOR_NUM 0x46f4 /* CRC16() of "QEMU" */
41 #define USBAUDIO_PRODUCT_NUM 0x0002
43 #define DEV_CONFIG_VALUE 1 /* The one and only */
45 /* Descriptor subtypes for AC interfaces */
46 #define DST_AC_HEADER 1
47 #define DST_AC_INPUT_TERMINAL 2
48 #define DST_AC_OUTPUT_TERMINAL 3
49 #define DST_AC_FEATURE_UNIT 6
50 /* Descriptor subtypes for AS interfaces */
51 #define DST_AS_GENERAL 1
52 #define DST_AS_FORMAT_TYPE 2
53 /* Descriptor subtypes for endpoints */
54 #define DST_EP_GENERAL 1
56 enum usb_audio_strings
{
62 STRING_USBAUDIO_CONTROL
,
63 STRING_INPUT_TERMINAL
,
65 STRING_OUTPUT_TERMINAL
,
70 static const USBDescStrings usb_audio_stringtable
= {
71 [STRING_MANUFACTURER
] = "QEMU",
72 [STRING_PRODUCT
] = "QEMU USB Audio",
73 [STRING_SERIALNUMBER
] = "1",
74 [STRING_CONFIG
] = "Audio Configuration",
75 [STRING_USBAUDIO_CONTROL
] = "Audio Device",
76 [STRING_INPUT_TERMINAL
] = "Audio Output Pipe",
77 [STRING_FEATURE_UNIT
] = "Audio Output Volume Control",
78 [STRING_OUTPUT_TERMINAL
] = "Audio Output Terminal",
79 [STRING_NULL_STREAM
] = "Audio Output - Disabled",
80 [STRING_REAL_STREAM
] = "Audio Output - 48 kHz Stereo",
83 #define U16(x) ((x) & 0xff), (((x) >> 8) & 0xff)
84 #define U24(x) U16(x), (((x) >> 16) & 0xff)
85 #define U32(x) U24(x), (((x) >> 24) & 0xff)
88 * A Basic Audio Device uses these specific values
90 #define USBAUDIO_PACKET_SIZE 192
91 #define USBAUDIO_SAMPLE_RATE 48000
92 #define USBAUDIO_PACKET_INTERVAL 1
94 static const USBDescIface desc_iface
[] = {
96 .bInterfaceNumber
= 0,
98 .bInterfaceClass
= USB_CLASS_AUDIO
,
99 .bInterfaceSubClass
= USB_SUBCLASS_AUDIO_CONTROL
,
100 .bInterfaceProtocol
= 0x04,
101 .iInterface
= STRING_USBAUDIO_CONTROL
,
103 .descs
= (USBDescOther
[]) {
105 /* Headphone Class-Specific AC Interface Header Descriptor */
106 .data
= (uint8_t[]) {
107 0x09, /* u8 bLength */
108 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
109 DST_AC_HEADER
, /* u8 bDescriptorSubtype */
110 U16(0x0100), /* u16 bcdADC */
111 U16(0x2b), /* u16 wTotalLength */
112 0x01, /* u8 bInCollection */
113 0x01, /* u8 baInterfaceNr */
116 /* Generic Stereo Input Terminal ID1 Descriptor */
117 .data
= (uint8_t[]) {
118 0x0c, /* u8 bLength */
119 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
120 DST_AC_INPUT_TERMINAL
, /* u8 bDescriptorSubtype */
121 0x01, /* u8 bTerminalID */
122 U16(0x0101), /* u16 wTerminalType */
123 0x00, /* u8 bAssocTerminal */
124 0x02, /* u16 bNrChannels */
125 U16(0x0003), /* u16 wChannelConfig */
126 0x00, /* u8 iChannelNames */
127 STRING_INPUT_TERMINAL
, /* u8 iTerminal */
130 /* Generic Stereo Feature Unit ID2 Descriptor */
131 .data
= (uint8_t[]) {
132 0x0d, /* u8 bLength */
133 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
134 DST_AC_FEATURE_UNIT
, /* u8 bDescriptorSubtype */
135 0x02, /* u8 bUnitID */
136 0x01, /* u8 bSourceID */
137 0x02, /* u8 bControlSize */
138 U16(0x0001), /* u16 bmaControls(0) */
139 U16(0x0002), /* u16 bmaControls(1) */
140 U16(0x0002), /* u16 bmaControls(2) */
141 STRING_FEATURE_UNIT
, /* u8 iFeature */
144 /* Headphone Ouptut Terminal ID3 Descriptor */
145 .data
= (uint8_t[]) {
146 0x09, /* u8 bLength */
147 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
148 DST_AC_OUTPUT_TERMINAL
, /* u8 bDescriptorSubtype */
149 0x03, /* u8 bUnitID */
150 U16(0x0301), /* u16 wTerminalType (SPK) */
151 0x00, /* u8 bAssocTerminal */
152 0x02, /* u8 bSourceID */
153 STRING_OUTPUT_TERMINAL
, /* u8 iTerminal */
158 .bInterfaceNumber
= 1,
159 .bAlternateSetting
= 0,
161 .bInterfaceClass
= USB_CLASS_AUDIO
,
162 .bInterfaceSubClass
= USB_SUBCLASS_AUDIO_STREAMING
,
163 .iInterface
= STRING_NULL_STREAM
,
165 .bInterfaceNumber
= 1,
166 .bAlternateSetting
= 1,
168 .bInterfaceClass
= USB_CLASS_AUDIO
,
169 .bInterfaceSubClass
= USB_SUBCLASS_AUDIO_STREAMING
,
170 .iInterface
= STRING_REAL_STREAM
,
172 .descs
= (USBDescOther
[]) {
174 /* Headphone Class-specific AS General Interface Descriptor */
175 .data
= (uint8_t[]) {
176 0x07, /* u8 bLength */
177 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
178 DST_AS_GENERAL
, /* u8 bDescriptorSubtype */
179 0x01, /* u8 bTerminalLink */
180 0x00, /* u8 bDelay */
181 0x01, 0x00, /* u16 wFormatTag */
184 /* Headphone Type I Format Type Descriptor */
185 .data
= (uint8_t[]) {
186 0x0b, /* u8 bLength */
187 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
188 DST_AS_FORMAT_TYPE
, /* u8 bDescriptorSubtype */
189 0x01, /* u8 bFormatType */
190 0x02, /* u8 bNrChannels */
191 0x02, /* u8 bSubFrameSize */
192 0x10, /* u8 bBitResolution */
193 0x01, /* u8 bSamFreqType */
194 U24(USBAUDIO_SAMPLE_RATE
), /* u24 tSamFreq */
198 .eps
= (USBDescEndpoint
[]) {
200 .bEndpointAddress
= USB_DIR_OUT
| 0x01,
201 .bmAttributes
= 0x0d,
202 .wMaxPacketSize
= USBAUDIO_PACKET_SIZE
,
205 /* Stereo Headphone Class-specific
206 AS Audio Data Endpoint Descriptor */
207 .extra
= (uint8_t[]) {
208 0x07, /* u8 bLength */
209 USB_DT_CS_ENDPOINT
, /* u8 bDescriptorType */
210 DST_EP_GENERAL
, /* u8 bDescriptorSubtype */
211 0x00, /* u8 bmAttributes */
212 0x00, /* u8 bLockDelayUnits */
213 U16(0x0000), /* u16 wLockDelay */
220 static const USBDescDevice desc_device
= {
222 .bMaxPacketSize0
= 64,
223 .bNumConfigurations
= 1,
224 .confs
= (USBDescConfig
[]) {
227 .bConfigurationValue
= DEV_CONFIG_VALUE
,
228 .iConfiguration
= STRING_CONFIG
,
229 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
231 .nif
= ARRAY_SIZE(desc_iface
),
237 static const USBDesc desc_audio
= {
239 .idVendor
= USBAUDIO_VENDOR_NUM
,
240 .idProduct
= USBAUDIO_PRODUCT_NUM
,
242 .iManufacturer
= STRING_MANUFACTURER
,
243 .iProduct
= STRING_PRODUCT
,
244 .iSerialNumber
= STRING_SERIALNUMBER
,
246 .full
= &desc_device
,
247 .str
= usb_audio_stringtable
,
251 * A USB audio device supports an arbitrary number of alternate
252 * interface settings for each interface. Each corresponds to a block
253 * diagram of parameterized blocks. This can thus refer to things like
254 * number of channels, data rates, or in fact completely different
255 * block diagrams. Alternative setting 0 is always the null block diagram,
256 * which is used by a disabled device.
258 enum usb_audio_altset
{
259 ALTSET_OFF
= 0x00, /* No endpoint */
260 ALTSET_ON
= 0x01, /* Single endpoint */
264 * Class-specific control requests
266 #define CR_SET_CUR 0x01
267 #define CR_GET_CUR 0x81
268 #define CR_SET_MIN 0x02
269 #define CR_GET_MIN 0x82
270 #define CR_SET_MAX 0x03
271 #define CR_GET_MAX 0x83
272 #define CR_SET_RES 0x04
273 #define CR_GET_RES 0x84
274 #define CR_SET_MEM 0x05
275 #define CR_GET_MEM 0x85
276 #define CR_GET_STAT 0xff
279 * Feature Unit Control Selectors
281 #define MUTE_CONTROL 0x01
282 #define VOLUME_CONTROL 0x02
283 #define BASS_CONTROL 0x03
284 #define MID_CONTROL 0x04
285 #define TREBLE_CONTROL 0x05
286 #define GRAPHIC_EQUALIZER_CONTROL 0x06
287 #define AUTOMATIC_GAIN_CONTROL 0x07
288 #define DELAY_CONTROL 0x08
289 #define BASS_BOOST_CONTROL 0x09
290 #define LOUDNESS_CONTROL 0x0a
303 static void streambuf_init(struct streambuf
*buf
, uint32_t size
)
306 buf
->size
= size
- (size
% USBAUDIO_PACKET_SIZE
);
307 buf
->data
= g_malloc(buf
->size
);
312 static void streambuf_fini(struct streambuf
*buf
)
318 static int streambuf_put(struct streambuf
*buf
, USBPacket
*p
)
320 uint32_t free
= buf
->size
- (buf
->prod
- buf
->cons
);
325 if (p
->iov
.size
!= USBAUDIO_PACKET_SIZE
) {
328 assert(free
>= USBAUDIO_PACKET_SIZE
);
329 usb_packet_copy(p
, buf
->data
+ (buf
->prod
% buf
->size
),
330 USBAUDIO_PACKET_SIZE
);
331 buf
->prod
+= USBAUDIO_PACKET_SIZE
;
332 return USBAUDIO_PACKET_SIZE
;
335 static uint8_t *streambuf_get(struct streambuf
*buf
)
337 uint32_t used
= buf
->prod
- buf
->cons
;
343 assert(used
>= USBAUDIO_PACKET_SIZE
);
344 data
= buf
->data
+ (buf
->cons
% buf
->size
);
345 buf
->cons
+= USBAUDIO_PACKET_SIZE
;
349 typedef struct USBAudioState
{
350 /* qemu interfaces */
356 enum usb_audio_altset altset
;
357 struct audsettings as
;
361 struct streambuf buf
;
369 #define TYPE_USB_AUDIO "usb-audio"
370 #define USB_AUDIO(obj) OBJECT_CHECK(USBAudioState, (obj), TYPE_USB_AUDIO)
372 static void output_callback(void *opaque
, int avail
)
374 USBAudioState
*s
= opaque
;
378 if (avail
< USBAUDIO_PACKET_SIZE
) {
381 data
= streambuf_get(&s
->out
.buf
);
385 AUD_write(s
->out
.voice
, data
, USBAUDIO_PACKET_SIZE
);
386 avail
-= USBAUDIO_PACKET_SIZE
;
390 static int usb_audio_set_output_altset(USBAudioState
*s
, int altset
)
394 streambuf_init(&s
->out
.buf
, s
->buffer
);
395 AUD_set_active_out(s
->out
.voice
, false);
398 AUD_set_active_out(s
->out
.voice
, true);
405 fprintf(stderr
, "usb-audio: set interface %d\n", altset
);
407 s
->out
.altset
= altset
;
412 * Note: we arbitrarily map the volume control range onto -inf..+8 dB
414 #define ATTRIB_ID(cs, attrib, idif) \
415 (((cs) << 24) | ((attrib) << 16) | (idif))
417 static int usb_audio_get_control(USBAudioState
*s
, uint8_t attrib
,
418 uint16_t cscn
, uint16_t idif
,
419 int length
, uint8_t *data
)
421 uint8_t cs
= cscn
>> 8;
422 uint8_t cn
= cscn
- 1; /* -1 for the non-present master control */
423 uint32_t aid
= ATTRIB_ID(cs
, attrib
, idif
);
424 int ret
= USB_RET_STALL
;
427 case ATTRIB_ID(MUTE_CONTROL
, CR_GET_CUR
, 0x0200):
428 data
[0] = s
->out
.mute
;
431 case ATTRIB_ID(VOLUME_CONTROL
, CR_GET_CUR
, 0x0200):
433 uint16_t vol
= (s
->out
.vol
[cn
] * 0x8800 + 127) / 255 + 0x8000;
439 case ATTRIB_ID(VOLUME_CONTROL
, CR_GET_MIN
, 0x0200):
446 case ATTRIB_ID(VOLUME_CONTROL
, CR_GET_MAX
, 0x0200):
453 case ATTRIB_ID(VOLUME_CONTROL
, CR_GET_RES
, 0x0200):
464 static int usb_audio_set_control(USBAudioState
*s
, uint8_t attrib
,
465 uint16_t cscn
, uint16_t idif
,
466 int length
, uint8_t *data
)
468 uint8_t cs
= cscn
>> 8;
469 uint8_t cn
= cscn
- 1; /* -1 for the non-present master control */
470 uint32_t aid
= ATTRIB_ID(cs
, attrib
, idif
);
471 int ret
= USB_RET_STALL
;
472 bool set_vol
= false;
475 case ATTRIB_ID(MUTE_CONTROL
, CR_SET_CUR
, 0x0200):
476 s
->out
.mute
= data
[0] & 1;
480 case ATTRIB_ID(VOLUME_CONTROL
, CR_SET_CUR
, 0x0200):
482 uint16_t vol
= data
[0] + (data
[1] << 8);
485 fprintf(stderr
, "usb-audio: vol %04x\n", (uint16_t)vol
);
489 vol
= (vol
* 255 + 0x4400) / 0x8800;
494 s
->out
.vol
[cn
] = vol
;
503 fprintf(stderr
, "usb-audio: mute %d, lvol %3d, rvol %3d\n",
504 s
->out
.mute
, s
->out
.vol
[0], s
->out
.vol
[1]);
506 AUD_set_volume_out(s
->out
.voice
, s
->out
.mute
,
507 s
->out
.vol
[0], s
->out
.vol
[1]);
513 static void usb_audio_handle_control(USBDevice
*dev
, USBPacket
*p
,
514 int request
, int value
, int index
,
515 int length
, uint8_t *data
)
517 USBAudioState
*s
= USB_AUDIO(dev
);
521 fprintf(stderr
, "usb-audio: control transaction: "
522 "request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
523 request
, value
, index
, length
);
526 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
532 case ClassInterfaceRequest
| CR_GET_CUR
:
533 case ClassInterfaceRequest
| CR_GET_MIN
:
534 case ClassInterfaceRequest
| CR_GET_MAX
:
535 case ClassInterfaceRequest
| CR_GET_RES
:
536 ret
= usb_audio_get_control(s
, request
& 0xff, value
, index
,
540 fprintf(stderr
, "usb-audio: fail: get control\n");
544 p
->actual_length
= ret
;
547 case ClassInterfaceOutRequest
| CR_SET_CUR
:
548 case ClassInterfaceOutRequest
| CR_SET_MIN
:
549 case ClassInterfaceOutRequest
| CR_SET_MAX
:
550 case ClassInterfaceOutRequest
| CR_SET_RES
:
551 ret
= usb_audio_set_control(s
, request
& 0xff, value
, index
,
555 fprintf(stderr
, "usb-audio: fail: set control\n");
564 fprintf(stderr
, "usb-audio: failed control transaction: "
565 "request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
566 request
, value
, index
, length
);
568 p
->status
= USB_RET_STALL
;
573 static void usb_audio_set_interface(USBDevice
*dev
, int iface
,
576 USBAudioState
*s
= USB_AUDIO(dev
);
579 usb_audio_set_output_altset(s
, value
);
583 static void usb_audio_handle_reset(USBDevice
*dev
)
585 USBAudioState
*s
= USB_AUDIO(dev
);
588 fprintf(stderr
, "usb-audio: reset\n");
590 usb_audio_set_output_altset(s
, ALTSET_OFF
);
593 static void usb_audio_handle_dataout(USBAudioState
*s
, USBPacket
*p
)
595 if (s
->out
.altset
== ALTSET_OFF
) {
596 p
->status
= USB_RET_STALL
;
600 streambuf_put(&s
->out
.buf
, p
);
601 if (p
->actual_length
< p
->iov
.size
&& s
->debug
> 1) {
602 fprintf(stderr
, "usb-audio: output overrun (%zd bytes)\n",
603 p
->iov
.size
- p
->actual_length
);
607 static void usb_audio_handle_data(USBDevice
*dev
, USBPacket
*p
)
609 USBAudioState
*s
= (USBAudioState
*) dev
;
611 if (p
->pid
== USB_TOKEN_OUT
&& p
->ep
->nr
== 1) {
612 usb_audio_handle_dataout(s
, p
);
616 p
->status
= USB_RET_STALL
;
618 fprintf(stderr
, "usb-audio: failed data transaction: "
619 "pid 0x%x ep 0x%x len 0x%zx\n",
620 p
->pid
, p
->ep
->nr
, p
->iov
.size
);
624 static void usb_audio_unrealize(USBDevice
*dev
, Error
**errp
)
626 USBAudioState
*s
= USB_AUDIO(dev
);
629 fprintf(stderr
, "usb-audio: destroy\n");
632 usb_audio_set_output_altset(s
, ALTSET_OFF
);
633 AUD_close_out(&s
->card
, s
->out
.voice
);
634 AUD_remove_card(&s
->card
);
636 streambuf_fini(&s
->out
.buf
);
639 static void usb_audio_realize(USBDevice
*dev
, Error
**errp
)
641 USBAudioState
*s
= USB_AUDIO(dev
);
643 usb_desc_create_serial(dev
);
646 AUD_register_card(TYPE_USB_AUDIO
, &s
->card
);
648 s
->out
.altset
= ALTSET_OFF
;
650 s
->out
.vol
[0] = 240; /* 0 dB */
651 s
->out
.vol
[1] = 240; /* 0 dB */
652 s
->out
.as
.freq
= USBAUDIO_SAMPLE_RATE
;
653 s
->out
.as
.nchannels
= 2;
654 s
->out
.as
.fmt
= AUDIO_FORMAT_S16
;
655 s
->out
.as
.endianness
= 0;
656 streambuf_init(&s
->out
.buf
, s
->buffer
);
658 s
->out
.voice
= AUD_open_out(&s
->card
, s
->out
.voice
, TYPE_USB_AUDIO
,
659 s
, output_callback
, &s
->out
.as
);
660 AUD_set_volume_out(s
->out
.voice
, s
->out
.mute
, s
->out
.vol
[0], s
->out
.vol
[1]);
661 AUD_set_active_out(s
->out
.voice
, 0);
664 static const VMStateDescription vmstate_usb_audio
= {
665 .name
= TYPE_USB_AUDIO
,
669 static Property usb_audio_properties
[] = {
670 DEFINE_AUDIO_PROPERTIES(USBAudioState
, card
),
671 DEFINE_PROP_UINT32("debug", USBAudioState
, debug
, 0),
672 DEFINE_PROP_UINT32("buffer", USBAudioState
, buffer
,
673 32 * USBAUDIO_PACKET_SIZE
),
674 DEFINE_PROP_END_OF_LIST(),
677 static void usb_audio_class_init(ObjectClass
*klass
, void *data
)
679 DeviceClass
*dc
= DEVICE_CLASS(klass
);
680 USBDeviceClass
*k
= USB_DEVICE_CLASS(klass
);
682 dc
->vmsd
= &vmstate_usb_audio
;
683 dc
->props
= usb_audio_properties
;
684 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
685 k
->product_desc
= "QEMU USB Audio Interface";
686 k
->usb_desc
= &desc_audio
;
687 k
->realize
= usb_audio_realize
;
688 k
->handle_reset
= usb_audio_handle_reset
;
689 k
->handle_control
= usb_audio_handle_control
;
690 k
->handle_data
= usb_audio_handle_data
;
691 k
->unrealize
= usb_audio_unrealize
;
692 k
->set_interface
= usb_audio_set_interface
;
695 static const TypeInfo usb_audio_info
= {
696 .name
= TYPE_USB_AUDIO
,
697 .parent
= TYPE_USB_DEVICE
,
698 .instance_size
= sizeof(USBAudioState
),
699 .class_init
= usb_audio_class_init
,
702 static void usb_audio_register_types(void)
704 type_register_static(&usb_audio_info
);
705 usb_legacy_register(TYPE_USB_AUDIO
, "audio", NULL
);
708 type_init(usb_audio_register_types
)