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"
39 #include "qom/object.h"
41 static void usb_audio_reinit(USBDevice
*dev
, unsigned channels
);
43 #define USBAUDIO_VENDOR_NUM 0x46f4 /* CRC16() of "QEMU" */
44 #define USBAUDIO_PRODUCT_NUM 0x0002
46 #define DEV_CONFIG_VALUE 1 /* The one and only */
48 #define USBAUDIO_MAX_CHANNELS(s) (s->multi ? 8 : 2)
50 /* Descriptor subtypes for AC interfaces */
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
55 /* Descriptor subtypes for AS interfaces */
56 #define DST_AS_GENERAL 1
57 #define DST_AS_FORMAT_TYPE 2
58 /* Descriptor subtypes for endpoints */
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",
89 * A USB audio device supports an arbitrary number of alternate
90 * interface settings for each interface. Each corresponds to a block
91 * diagram of parameterized blocks. This can thus refer to things like
92 * number of channels, data rates, or in fact completely different
93 * block diagrams. Alternative setting 0 is always the null block diagram,
94 * which is used by a disabled device.
96 enum usb_audio_altset
{
97 ALTSET_OFF
= 0x00, /* No endpoint */
98 ALTSET_STEREO
= 0x01, /* Single endpoint */
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)
114 * A Basic Audio Device uses these specific values
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
[]) {
131 /* Headphone Class-Specific AC Interface Header Descriptor */
132 .data
= (uint8_t[]) {
133 0x09, /* u8 bLength */
134 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
135 DST_AC_HEADER
, /* u8 bDescriptorSubtype */
136 U16(0x0100), /* u16 bcdADC */
137 U16(0x2b), /* u16 wTotalLength */
138 0x01, /* u8 bInCollection */
139 0x01, /* u8 baInterfaceNr */
142 /* Generic Stereo Input Terminal ID1 Descriptor */
143 .data
= (uint8_t[]) {
144 0x0c, /* u8 bLength */
145 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
146 DST_AC_INPUT_TERMINAL
, /* u8 bDescriptorSubtype */
147 0x01, /* u8 bTerminalID */
148 U16(0x0101), /* u16 wTerminalType */
149 0x00, /* u8 bAssocTerminal */
150 0x02, /* u8 bNrChannels */
151 U16(0x0003), /* u16 wChannelConfig */
152 0x00, /* u8 iChannelNames */
153 STRING_INPUT_TERMINAL
, /* u8 iTerminal */
156 /* Generic Stereo Feature Unit ID2 Descriptor */
157 .data
= (uint8_t[]) {
158 0x0d, /* u8 bLength */
159 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
160 DST_AC_FEATURE_UNIT
, /* u8 bDescriptorSubtype */
161 0x02, /* u8 bUnitID */
162 0x01, /* u8 bSourceID */
163 0x02, /* u8 bControlSize */
164 U16(0x0001), /* u16 bmaControls(0) */
165 U16(0x0002), /* u16 bmaControls(1) */
166 U16(0x0002), /* u16 bmaControls(2) */
167 STRING_FEATURE_UNIT
, /* u8 iFeature */
170 /* Headphone Output Terminal ID3 Descriptor */
171 .data
= (uint8_t[]) {
172 0x09, /* u8 bLength */
173 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
174 DST_AC_OUTPUT_TERMINAL
, /* u8 bDescriptorSubtype */
175 0x03, /* u8 bUnitID */
176 U16(0x0301), /* u16 wTerminalType (SPK) */
177 0x00, /* u8 bAssocTerminal */
178 0x02, /* u8 bSourceID */
179 STRING_OUTPUT_TERMINAL
, /* u8 iTerminal */
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
[]) {
200 /* Headphone Class-specific AS General Interface Descriptor */
201 .data
= (uint8_t[]) {
202 0x07, /* u8 bLength */
203 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
204 DST_AS_GENERAL
, /* u8 bDescriptorSubtype */
205 0x01, /* u8 bTerminalLink */
206 0x00, /* u8 bDelay */
207 0x01, 0x00, /* u16 wFormatTag */
210 /* Headphone Type I Format Type Descriptor */
211 .data
= (uint8_t[]) {
212 0x0b, /* u8 bLength */
213 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
214 DST_AS_FORMAT_TYPE
, /* u8 bDescriptorSubtype */
215 0x01, /* u8 bFormatType */
216 0x02, /* u8 bNrChannels */
217 0x02, /* u8 bSubFrameSize */
218 0x10, /* u8 bBitResolution */
219 0x01, /* u8 bSamFreqType */
220 U24(USBAUDIO_SAMPLE_RATE
), /* u24 tSamFreq */
224 .eps
= (USBDescEndpoint
[]) {
226 .bEndpointAddress
= USB_DIR_OUT
| 0x01,
227 .bmAttributes
= 0x0d,
228 .wMaxPacketSize
= USBAUDIO_PACKET_SIZE(2),
231 /* Stereo Headphone Class-specific
232 AS Audio Data Endpoint Descriptor */
233 .extra
= (uint8_t[]) {
234 0x07, /* u8 bLength */
235 USB_DT_CS_ENDPOINT
, /* u8 bDescriptorType */
236 DST_EP_GENERAL
, /* u8 bDescriptorSubtype */
237 0x00, /* u8 bmAttributes */
238 0x00, /* u8 bLockDelayUnits */
239 U16(0x0000), /* u16 wLockDelay */
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
,
276 /* multi channel compatible desc */
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
[]) {
288 /* Headphone Class-Specific AC Interface Header Descriptor */
289 .data
= (uint8_t[]) {
290 0x09, /* u8 bLength */
291 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
292 DST_AC_HEADER
, /* u8 bDescriptorSubtype */
293 U16(0x0100), /* u16 bcdADC */
294 U16(0x37), /* u16 wTotalLength */
295 0x01, /* u8 bInCollection */
296 0x01, /* u8 baInterfaceNr */
299 /* Generic Stereo Input Terminal ID1 Descriptor */
300 .data
= (uint8_t[]) {
301 0x0c, /* u8 bLength */
302 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
303 DST_AC_INPUT_TERMINAL
, /* u8 bDescriptorSubtype */
304 0x01, /* u8 bTerminalID */
305 U16(0x0101), /* u16 wTerminalType */
306 0x00, /* u8 bAssocTerminal */
307 0x08, /* u8 bNrChannels */
308 U16(0x063f), /* u16 wChannelConfig */
309 0x00, /* u8 iChannelNames */
310 STRING_INPUT_TERMINAL
, /* u8 iTerminal */
313 /* Generic Stereo Feature Unit ID2 Descriptor */
314 .data
= (uint8_t[]) {
315 0x19, /* u8 bLength */
316 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
317 DST_AC_FEATURE_UNIT
, /* u8 bDescriptorSubtype */
318 0x02, /* u8 bUnitID */
319 0x01, /* u8 bSourceID */
320 0x02, /* u8 bControlSize */
321 U16(0x0001), /* u16 bmaControls(0) */
322 U16(0x0002), /* u16 bmaControls(1) */
323 U16(0x0002), /* u16 bmaControls(2) */
324 U16(0x0002), /* u16 bmaControls(3) */
325 U16(0x0002), /* u16 bmaControls(4) */
326 U16(0x0002), /* u16 bmaControls(5) */
327 U16(0x0002), /* u16 bmaControls(6) */
328 U16(0x0002), /* u16 bmaControls(7) */
329 U16(0x0002), /* u16 bmaControls(8) */
330 STRING_FEATURE_UNIT
, /* u8 iFeature */
333 /* Headphone Output Terminal ID3 Descriptor */
334 .data
= (uint8_t[]) {
335 0x09, /* u8 bLength */
336 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
337 DST_AC_OUTPUT_TERMINAL
, /* u8 bDescriptorSubtype */
338 0x03, /* u8 bUnitID */
339 U16(0x0301), /* u16 wTerminalType (SPK) */
340 0x00, /* u8 bAssocTerminal */
341 0x02, /* u8 bSourceID */
342 STRING_OUTPUT_TERMINAL
, /* u8 iTerminal */
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
[]) {
363 /* Headphone Class-specific AS General Interface Descriptor */
364 .data
= (uint8_t[]) {
365 0x07, /* u8 bLength */
366 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
367 DST_AS_GENERAL
, /* u8 bDescriptorSubtype */
368 0x01, /* u8 bTerminalLink */
369 0x00, /* u8 bDelay */
370 0x01, 0x00, /* u16 wFormatTag */
373 /* Headphone Type I Format Type Descriptor */
374 .data
= (uint8_t[]) {
375 0x0b, /* u8 bLength */
376 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
377 DST_AS_FORMAT_TYPE
, /* u8 bDescriptorSubtype */
378 0x01, /* u8 bFormatType */
379 0x02, /* u8 bNrChannels */
380 0x02, /* u8 bSubFrameSize */
381 0x10, /* u8 bBitResolution */
382 0x01, /* u8 bSamFreqType */
383 U24(USBAUDIO_SAMPLE_RATE
), /* u24 tSamFreq */
387 .eps
= (USBDescEndpoint
[]) {
389 .bEndpointAddress
= USB_DIR_OUT
| 0x01,
390 .bmAttributes
= 0x0d,
391 .wMaxPacketSize
= USBAUDIO_PACKET_SIZE(2),
394 /* Stereo Headphone Class-specific
395 AS Audio Data Endpoint Descriptor */
396 .extra
= (uint8_t[]) {
397 0x07, /* u8 bLength */
398 USB_DT_CS_ENDPOINT
, /* u8 bDescriptorType */
399 DST_EP_GENERAL
, /* u8 bDescriptorSubtype */
400 0x00, /* u8 bmAttributes */
401 0x00, /* u8 bLockDelayUnits */
402 U16(0x0000), /* u16 wLockDelay */
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
[]) {
416 /* Headphone Class-specific AS General Interface Descriptor */
417 .data
= (uint8_t[]) {
418 0x07, /* u8 bLength */
419 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
420 DST_AS_GENERAL
, /* u8 bDescriptorSubtype */
421 0x01, /* u8 bTerminalLink */
422 0x00, /* u8 bDelay */
423 0x01, 0x00, /* u16 wFormatTag */
426 /* Headphone Type I Format Type Descriptor */
427 .data
= (uint8_t[]) {
428 0x0b, /* u8 bLength */
429 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
430 DST_AS_FORMAT_TYPE
, /* u8 bDescriptorSubtype */
431 0x01, /* u8 bFormatType */
432 0x06, /* u8 bNrChannels */
433 0x02, /* u8 bSubFrameSize */
434 0x10, /* u8 bBitResolution */
435 0x01, /* u8 bSamFreqType */
436 U24(USBAUDIO_SAMPLE_RATE
), /* u24 tSamFreq */
440 .eps
= (USBDescEndpoint
[]) {
442 .bEndpointAddress
= USB_DIR_OUT
| 0x01,
443 .bmAttributes
= 0x0d,
444 .wMaxPacketSize
= USBAUDIO_PACKET_SIZE(6),
447 /* Stereo Headphone Class-specific
448 AS Audio Data Endpoint Descriptor */
449 .extra
= (uint8_t[]) {
450 0x07, /* u8 bLength */
451 USB_DT_CS_ENDPOINT
, /* u8 bDescriptorType */
452 DST_EP_GENERAL
, /* u8 bDescriptorSubtype */
453 0x00, /* u8 bmAttributes */
454 0x00, /* u8 bLockDelayUnits */
455 U16(0x0000), /* u16 wLockDelay */
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
[]) {
469 /* Headphone Class-specific AS General Interface Descriptor */
470 .data
= (uint8_t[]) {
471 0x07, /* u8 bLength */
472 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
473 DST_AS_GENERAL
, /* u8 bDescriptorSubtype */
474 0x01, /* u8 bTerminalLink */
475 0x00, /* u8 bDelay */
476 0x01, 0x00, /* u16 wFormatTag */
479 /* Headphone Type I Format Type Descriptor */
480 .data
= (uint8_t[]) {
481 0x0b, /* u8 bLength */
482 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
483 DST_AS_FORMAT_TYPE
, /* u8 bDescriptorSubtype */
484 0x01, /* u8 bFormatType */
485 0x08, /* u8 bNrChannels */
486 0x02, /* u8 bSubFrameSize */
487 0x10, /* u8 bBitResolution */
488 0x01, /* u8 bSamFreqType */
489 U24(USBAUDIO_SAMPLE_RATE
), /* u24 tSamFreq */
493 .eps
= (USBDescEndpoint
[]) {
495 .bEndpointAddress
= USB_DIR_OUT
| 0x01,
496 .bmAttributes
= 0x0d,
497 .wMaxPacketSize
= USBAUDIO_PACKET_SIZE(8),
500 /* Stereo Headphone Class-specific
501 AS Audio Data Endpoint Descriptor */
502 .extra
= (uint8_t[]) {
503 0x07, /* u8 bLength */
504 USB_DT_CS_ENDPOINT
, /* u8 bDescriptorType */
505 DST_EP_GENERAL
, /* u8 bDescriptorSubtype */
506 0x00, /* u8 bmAttributes */
507 0x00, /* u8 bLockDelayUnits */
508 U16(0x0000), /* u16 wLockDelay */
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
,
546 * Class-specific control requests
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
561 * Feature Unit Control Selectors
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
)) {
612 /* can happen if prod overflows */
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
{
636 /* qemu interfaces */
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
;
709 * Note: we arbitrarily map the volume control range onto -inf..+8 dB
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; /* -1 for the non-present master control */
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; /* -1 for the non-present master control */
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; /* 0 dB */
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
)