2010-09-11 01:47:56 +04:00
|
|
|
/*
|
|
|
|
* QEMU USB audio device
|
|
|
|
*
|
|
|
|
* written by:
|
|
|
|
* H. Peter Anvin <hpa@linux.intel.com>
|
|
|
|
* Gerd Hoffmann <kraxel@redhat.com>
|
|
|
|
*
|
|
|
|
* lousely based on usb net device code which is:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Thomas Sailer
|
|
|
|
* Copyright (c) 2008 Andrzej Zaborowski
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:12 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2012-03-07 17:55:18 +04:00
|
|
|
#include "hw/usb.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2018-05-03 22:50:48 +03:00
|
|
|
#include "desc.h"
|
2010-09-11 01:47:56 +04:00
|
|
|
#include "audio/audio.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2010-09-11 01:47:56 +04:00
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
static void usb_audio_reinit(USBDevice *dev, unsigned channels);
|
|
|
|
|
2010-09-11 01:47:56 +04:00
|
|
|
#define USBAUDIO_VENDOR_NUM 0x46f4 /* CRC16() of "QEMU" */
|
|
|
|
#define USBAUDIO_PRODUCT_NUM 0x0002
|
|
|
|
|
|
|
|
#define DEV_CONFIG_VALUE 1 /* The one and only */
|
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
#define USBAUDIO_MAX_CHANNELS(s) (s->multi ? 8 : 2)
|
|
|
|
|
2010-09-11 01:47:56 +04:00
|
|
|
/* Descriptor subtypes for AC interfaces */
|
|
|
|
#define DST_AC_HEADER 1
|
|
|
|
#define DST_AC_INPUT_TERMINAL 2
|
|
|
|
#define DST_AC_OUTPUT_TERMINAL 3
|
|
|
|
#define DST_AC_FEATURE_UNIT 6
|
|
|
|
/* Descriptor subtypes for AS interfaces */
|
|
|
|
#define DST_AS_GENERAL 1
|
|
|
|
#define DST_AS_FORMAT_TYPE 2
|
|
|
|
/* Descriptor subtypes for endpoints */
|
|
|
|
#define DST_EP_GENERAL 1
|
|
|
|
|
|
|
|
enum usb_audio_strings {
|
|
|
|
STRING_NULL,
|
|
|
|
STRING_MANUFACTURER,
|
|
|
|
STRING_PRODUCT,
|
|
|
|
STRING_SERIALNUMBER,
|
|
|
|
STRING_CONFIG,
|
|
|
|
STRING_USBAUDIO_CONTROL,
|
|
|
|
STRING_INPUT_TERMINAL,
|
|
|
|
STRING_FEATURE_UNIT,
|
|
|
|
STRING_OUTPUT_TERMINAL,
|
|
|
|
STRING_NULL_STREAM,
|
|
|
|
STRING_REAL_STREAM,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescStrings usb_audio_stringtable = {
|
|
|
|
[STRING_MANUFACTURER] = "QEMU",
|
|
|
|
[STRING_PRODUCT] = "QEMU USB Audio",
|
|
|
|
[STRING_SERIALNUMBER] = "1",
|
|
|
|
[STRING_CONFIG] = "Audio Configuration",
|
|
|
|
[STRING_USBAUDIO_CONTROL] = "Audio Device",
|
|
|
|
[STRING_INPUT_TERMINAL] = "Audio Output Pipe",
|
|
|
|
[STRING_FEATURE_UNIT] = "Audio Output Volume Control",
|
|
|
|
[STRING_OUTPUT_TERMINAL] = "Audio Output Terminal",
|
|
|
|
[STRING_NULL_STREAM] = "Audio Output - Disabled",
|
|
|
|
[STRING_REAL_STREAM] = "Audio Output - 48 kHz Stereo",
|
|
|
|
};
|
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
/*
|
|
|
|
* A USB audio device supports an arbitrary number of alternate
|
|
|
|
* interface settings for each interface. Each corresponds to a block
|
|
|
|
* diagram of parameterized blocks. This can thus refer to things like
|
|
|
|
* number of channels, data rates, or in fact completely different
|
|
|
|
* block diagrams. Alternative setting 0 is always the null block diagram,
|
|
|
|
* which is used by a disabled device.
|
|
|
|
*/
|
|
|
|
enum usb_audio_altset {
|
|
|
|
ALTSET_OFF = 0x00, /* No endpoint */
|
|
|
|
ALTSET_STEREO = 0x01, /* Single endpoint */
|
|
|
|
ALTSET_51 = 0x02,
|
|
|
|
ALTSET_71 = 0x03,
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned altset_channels[] = {
|
|
|
|
[ALTSET_STEREO] = 2,
|
|
|
|
[ALTSET_51] = 6,
|
|
|
|
[ALTSET_71] = 8,
|
|
|
|
};
|
|
|
|
|
2010-09-11 01:47:56 +04:00
|
|
|
#define U16(x) ((x) & 0xff), (((x) >> 8) & 0xff)
|
|
|
|
#define U24(x) U16(x), (((x) >> 16) & 0xff)
|
|
|
|
#define U32(x) U24(x), (((x) >> 24) & 0xff)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A Basic Audio Device uses these specific values
|
|
|
|
*/
|
2019-10-13 22:58:05 +03:00
|
|
|
#define USBAUDIO_PACKET_SIZE_BASE 96
|
|
|
|
#define USBAUDIO_PACKET_SIZE(channels) (USBAUDIO_PACKET_SIZE_BASE * channels)
|
2010-09-11 01:47:56 +04:00
|
|
|
#define USBAUDIO_SAMPLE_RATE 48000
|
|
|
|
#define USBAUDIO_PACKET_INTERVAL 1
|
|
|
|
|
|
|
|
static const USBDescIface desc_iface[] = {
|
|
|
|
{
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 0,
|
|
|
|
.bInterfaceClass = USB_CLASS_AUDIO,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL,
|
|
|
|
.bInterfaceProtocol = 0x04,
|
|
|
|
.iInterface = STRING_USBAUDIO_CONTROL,
|
|
|
|
.ndesc = 4,
|
|
|
|
.descs = (USBDescOther[]) {
|
|
|
|
{
|
|
|
|
/* Headphone Class-Specific AC Interface Header Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x09, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AC_HEADER, /* u8 bDescriptorSubtype */
|
|
|
|
U16(0x0100), /* u16 bcdADC */
|
|
|
|
U16(0x2b), /* u16 wTotalLength */
|
|
|
|
0x01, /* u8 bInCollection */
|
|
|
|
0x01, /* u8 baInterfaceNr */
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
/* Generic Stereo Input Terminal ID1 Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x0c, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AC_INPUT_TERMINAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bTerminalID */
|
|
|
|
U16(0x0101), /* u16 wTerminalType */
|
|
|
|
0x00, /* u8 bAssocTerminal */
|
2019-10-13 22:58:05 +03:00
|
|
|
0x02, /* u8 bNrChannels */
|
2010-09-11 01:47:56 +04:00
|
|
|
U16(0x0003), /* u16 wChannelConfig */
|
|
|
|
0x00, /* u8 iChannelNames */
|
|
|
|
STRING_INPUT_TERMINAL, /* u8 iTerminal */
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
/* Generic Stereo Feature Unit ID2 Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x0d, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AC_FEATURE_UNIT, /* u8 bDescriptorSubtype */
|
|
|
|
0x02, /* u8 bUnitID */
|
|
|
|
0x01, /* u8 bSourceID */
|
|
|
|
0x02, /* u8 bControlSize */
|
|
|
|
U16(0x0001), /* u16 bmaControls(0) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(1) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(2) */
|
|
|
|
STRING_FEATURE_UNIT, /* u8 iFeature */
|
|
|
|
}
|
|
|
|
},{
|
2021-07-30 04:27:20 +03:00
|
|
|
/* Headphone Output Terminal ID3 Descriptor */
|
2010-09-11 01:47:56 +04:00
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x09, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AC_OUTPUT_TERMINAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x03, /* u8 bUnitID */
|
|
|
|
U16(0x0301), /* u16 wTerminalType (SPK) */
|
|
|
|
0x00, /* u8 bAssocTerminal */
|
|
|
|
0x02, /* u8 bSourceID */
|
|
|
|
STRING_OUTPUT_TERMINAL, /* u8 iTerminal */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},{
|
|
|
|
.bInterfaceNumber = 1,
|
2019-10-13 22:58:05 +03:00
|
|
|
.bAlternateSetting = ALTSET_OFF,
|
2010-09-11 01:47:56 +04:00
|
|
|
.bNumEndpoints = 0,
|
|
|
|
.bInterfaceClass = USB_CLASS_AUDIO,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
|
|
|
|
.iInterface = STRING_NULL_STREAM,
|
|
|
|
},{
|
|
|
|
.bInterfaceNumber = 1,
|
2019-10-13 22:58:05 +03:00
|
|
|
.bAlternateSetting = ALTSET_STEREO,
|
2010-09-11 01:47:56 +04:00
|
|
|
.bNumEndpoints = 1,
|
|
|
|
.bInterfaceClass = USB_CLASS_AUDIO,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
|
|
|
|
.iInterface = STRING_REAL_STREAM,
|
|
|
|
.ndesc = 2,
|
|
|
|
.descs = (USBDescOther[]) {
|
|
|
|
{
|
|
|
|
/* Headphone Class-specific AS General Interface Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x07, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AS_GENERAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bTerminalLink */
|
|
|
|
0x00, /* u8 bDelay */
|
|
|
|
0x01, 0x00, /* u16 wFormatTag */
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
/* Headphone Type I Format Type Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x0b, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AS_FORMAT_TYPE, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bFormatType */
|
|
|
|
0x02, /* u8 bNrChannels */
|
|
|
|
0x02, /* u8 bSubFrameSize */
|
|
|
|
0x10, /* u8 bBitResolution */
|
|
|
|
0x01, /* u8 bSamFreqType */
|
|
|
|
U24(USBAUDIO_SAMPLE_RATE), /* u24 tSamFreq */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | 0x01,
|
|
|
|
.bmAttributes = 0x0d,
|
2019-10-13 22:58:05 +03:00
|
|
|
.wMaxPacketSize = USBAUDIO_PACKET_SIZE(2),
|
2010-09-11 01:47:56 +04:00
|
|
|
.bInterval = 1,
|
|
|
|
.is_audio = 1,
|
|
|
|
/* Stereo Headphone Class-specific
|
|
|
|
AS Audio Data Endpoint Descriptor */
|
|
|
|
.extra = (uint8_t[]) {
|
|
|
|
0x07, /* u8 bLength */
|
|
|
|
USB_DT_CS_ENDPOINT, /* u8 bDescriptorType */
|
|
|
|
DST_EP_GENERAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x00, /* u8 bmAttributes */
|
|
|
|
0x00, /* u8 bLockDelayUnits */
|
|
|
|
U16(0x0000), /* u16 wLockDelay */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescDevice desc_device = {
|
2012-08-28 18:43:34 +04:00
|
|
|
.bcdUSB = 0x0100,
|
2010-09-11 01:47:56 +04:00
|
|
|
.bMaxPacketSize0 = 64,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
.confs = (USBDescConfig[]) {
|
|
|
|
{
|
|
|
|
.bNumInterfaces = 2,
|
|
|
|
.bConfigurationValue = DEV_CONFIG_VALUE,
|
|
|
|
.iConfiguration = STRING_CONFIG,
|
2013-12-16 11:42:49 +04:00
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
|
2010-09-11 01:47:56 +04:00
|
|
|
.bMaxPower = 0x32,
|
|
|
|
.nif = ARRAY_SIZE(desc_iface),
|
|
|
|
.ifs = desc_iface,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDesc desc_audio = {
|
|
|
|
.id = {
|
|
|
|
.idVendor = USBAUDIO_VENDOR_NUM,
|
|
|
|
.idProduct = USBAUDIO_PRODUCT_NUM,
|
|
|
|
.bcdDevice = 0,
|
|
|
|
.iManufacturer = STRING_MANUFACTURER,
|
|
|
|
.iProduct = STRING_PRODUCT,
|
|
|
|
.iSerialNumber = STRING_SERIALNUMBER,
|
|
|
|
},
|
|
|
|
.full = &desc_device,
|
|
|
|
.str = usb_audio_stringtable,
|
|
|
|
};
|
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
/* multi channel compatible desc */
|
|
|
|
|
|
|
|
static const USBDescIface desc_iface_multi[] = {
|
|
|
|
{
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 0,
|
|
|
|
.bInterfaceClass = USB_CLASS_AUDIO,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL,
|
|
|
|
.bInterfaceProtocol = 0x04,
|
|
|
|
.iInterface = STRING_USBAUDIO_CONTROL,
|
|
|
|
.ndesc = 4,
|
|
|
|
.descs = (USBDescOther[]) {
|
|
|
|
{
|
|
|
|
/* Headphone Class-Specific AC Interface Header Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x09, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AC_HEADER, /* u8 bDescriptorSubtype */
|
|
|
|
U16(0x0100), /* u16 bcdADC */
|
|
|
|
U16(0x38), /* u16 wTotalLength */
|
|
|
|
0x01, /* u8 bInCollection */
|
|
|
|
0x01, /* u8 baInterfaceNr */
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
/* Generic Stereo Input Terminal ID1 Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x0c, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AC_INPUT_TERMINAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bTerminalID */
|
|
|
|
U16(0x0101), /* u16 wTerminalType */
|
|
|
|
0x00, /* u8 bAssocTerminal */
|
|
|
|
0x08, /* u8 bNrChannels */
|
|
|
|
U16(0x063f), /* u16 wChannelConfig */
|
|
|
|
0x00, /* u8 iChannelNames */
|
|
|
|
STRING_INPUT_TERMINAL, /* u8 iTerminal */
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
/* Generic Stereo Feature Unit ID2 Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x19, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AC_FEATURE_UNIT, /* u8 bDescriptorSubtype */
|
|
|
|
0x02, /* u8 bUnitID */
|
|
|
|
0x01, /* u8 bSourceID */
|
|
|
|
0x02, /* u8 bControlSize */
|
|
|
|
U16(0x0001), /* u16 bmaControls(0) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(1) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(2) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(3) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(4) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(5) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(6) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(7) */
|
|
|
|
U16(0x0002), /* u16 bmaControls(8) */
|
|
|
|
STRING_FEATURE_UNIT, /* u8 iFeature */
|
|
|
|
}
|
|
|
|
},{
|
2021-07-30 04:27:20 +03:00
|
|
|
/* Headphone Output Terminal ID3 Descriptor */
|
2019-10-13 22:58:05 +03:00
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x09, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AC_OUTPUT_TERMINAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x03, /* u8 bUnitID */
|
|
|
|
U16(0x0301), /* u16 wTerminalType (SPK) */
|
|
|
|
0x00, /* u8 bAssocTerminal */
|
|
|
|
0x02, /* u8 bSourceID */
|
|
|
|
STRING_OUTPUT_TERMINAL, /* u8 iTerminal */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},{
|
|
|
|
.bInterfaceNumber = 1,
|
|
|
|
.bAlternateSetting = ALTSET_OFF,
|
|
|
|
.bNumEndpoints = 0,
|
|
|
|
.bInterfaceClass = USB_CLASS_AUDIO,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
|
|
|
|
.iInterface = STRING_NULL_STREAM,
|
|
|
|
},{
|
|
|
|
.bInterfaceNumber = 1,
|
|
|
|
.bAlternateSetting = ALTSET_STEREO,
|
|
|
|
.bNumEndpoints = 1,
|
|
|
|
.bInterfaceClass = USB_CLASS_AUDIO,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
|
|
|
|
.iInterface = STRING_REAL_STREAM,
|
|
|
|
.ndesc = 2,
|
|
|
|
.descs = (USBDescOther[]) {
|
|
|
|
{
|
|
|
|
/* Headphone Class-specific AS General Interface Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x07, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AS_GENERAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bTerminalLink */
|
|
|
|
0x00, /* u8 bDelay */
|
|
|
|
0x01, 0x00, /* u16 wFormatTag */
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
/* Headphone Type I Format Type Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x0b, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AS_FORMAT_TYPE, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bFormatType */
|
|
|
|
0x02, /* u8 bNrChannels */
|
|
|
|
0x02, /* u8 bSubFrameSize */
|
|
|
|
0x10, /* u8 bBitResolution */
|
|
|
|
0x01, /* u8 bSamFreqType */
|
|
|
|
U24(USBAUDIO_SAMPLE_RATE), /* u24 tSamFreq */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | 0x01,
|
|
|
|
.bmAttributes = 0x0d,
|
|
|
|
.wMaxPacketSize = USBAUDIO_PACKET_SIZE(2),
|
|
|
|
.bInterval = 1,
|
|
|
|
.is_audio = 1,
|
|
|
|
/* Stereo Headphone Class-specific
|
|
|
|
AS Audio Data Endpoint Descriptor */
|
|
|
|
.extra = (uint8_t[]) {
|
|
|
|
0x07, /* u8 bLength */
|
|
|
|
USB_DT_CS_ENDPOINT, /* u8 bDescriptorType */
|
|
|
|
DST_EP_GENERAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x00, /* u8 bmAttributes */
|
|
|
|
0x00, /* u8 bLockDelayUnits */
|
|
|
|
U16(0x0000), /* u16 wLockDelay */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
.bInterfaceNumber = 1,
|
|
|
|
.bAlternateSetting = ALTSET_51,
|
|
|
|
.bNumEndpoints = 1,
|
|
|
|
.bInterfaceClass = USB_CLASS_AUDIO,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
|
|
|
|
.iInterface = STRING_REAL_STREAM,
|
|
|
|
.ndesc = 2,
|
|
|
|
.descs = (USBDescOther[]) {
|
|
|
|
{
|
|
|
|
/* Headphone Class-specific AS General Interface Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x07, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AS_GENERAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bTerminalLink */
|
|
|
|
0x00, /* u8 bDelay */
|
|
|
|
0x01, 0x00, /* u16 wFormatTag */
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
/* Headphone Type I Format Type Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x0b, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AS_FORMAT_TYPE, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bFormatType */
|
|
|
|
0x06, /* u8 bNrChannels */
|
|
|
|
0x02, /* u8 bSubFrameSize */
|
|
|
|
0x10, /* u8 bBitResolution */
|
|
|
|
0x01, /* u8 bSamFreqType */
|
|
|
|
U24(USBAUDIO_SAMPLE_RATE), /* u24 tSamFreq */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | 0x01,
|
|
|
|
.bmAttributes = 0x0d,
|
|
|
|
.wMaxPacketSize = USBAUDIO_PACKET_SIZE(6),
|
|
|
|
.bInterval = 1,
|
|
|
|
.is_audio = 1,
|
|
|
|
/* Stereo Headphone Class-specific
|
|
|
|
AS Audio Data Endpoint Descriptor */
|
|
|
|
.extra = (uint8_t[]) {
|
|
|
|
0x07, /* u8 bLength */
|
|
|
|
USB_DT_CS_ENDPOINT, /* u8 bDescriptorType */
|
|
|
|
DST_EP_GENERAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x00, /* u8 bmAttributes */
|
|
|
|
0x00, /* u8 bLockDelayUnits */
|
|
|
|
U16(0x0000), /* u16 wLockDelay */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
.bInterfaceNumber = 1,
|
|
|
|
.bAlternateSetting = ALTSET_71,
|
|
|
|
.bNumEndpoints = 1,
|
|
|
|
.bInterfaceClass = USB_CLASS_AUDIO,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
|
|
|
|
.iInterface = STRING_REAL_STREAM,
|
|
|
|
.ndesc = 2,
|
|
|
|
.descs = (USBDescOther[]) {
|
|
|
|
{
|
|
|
|
/* Headphone Class-specific AS General Interface Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x07, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AS_GENERAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bTerminalLink */
|
|
|
|
0x00, /* u8 bDelay */
|
|
|
|
0x01, 0x00, /* u16 wFormatTag */
|
|
|
|
}
|
|
|
|
},{
|
|
|
|
/* Headphone Type I Format Type Descriptor */
|
|
|
|
.data = (uint8_t[]) {
|
|
|
|
0x0b, /* u8 bLength */
|
|
|
|
USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
|
|
|
|
DST_AS_FORMAT_TYPE, /* u8 bDescriptorSubtype */
|
|
|
|
0x01, /* u8 bFormatType */
|
|
|
|
0x08, /* u8 bNrChannels */
|
|
|
|
0x02, /* u8 bSubFrameSize */
|
|
|
|
0x10, /* u8 bBitResolution */
|
|
|
|
0x01, /* u8 bSamFreqType */
|
|
|
|
U24(USBAUDIO_SAMPLE_RATE), /* u24 tSamFreq */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | 0x01,
|
|
|
|
.bmAttributes = 0x0d,
|
|
|
|
.wMaxPacketSize = USBAUDIO_PACKET_SIZE(8),
|
|
|
|
.bInterval = 1,
|
|
|
|
.is_audio = 1,
|
|
|
|
/* Stereo Headphone Class-specific
|
|
|
|
AS Audio Data Endpoint Descriptor */
|
|
|
|
.extra = (uint8_t[]) {
|
|
|
|
0x07, /* u8 bLength */
|
|
|
|
USB_DT_CS_ENDPOINT, /* u8 bDescriptorType */
|
|
|
|
DST_EP_GENERAL, /* u8 bDescriptorSubtype */
|
|
|
|
0x00, /* u8 bmAttributes */
|
|
|
|
0x00, /* u8 bLockDelayUnits */
|
|
|
|
U16(0x0000), /* u16 wLockDelay */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescDevice desc_device_multi = {
|
|
|
|
.bcdUSB = 0x0100,
|
|
|
|
.bMaxPacketSize0 = 64,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
.confs = (USBDescConfig[]) {
|
|
|
|
{
|
|
|
|
.bNumInterfaces = 2,
|
|
|
|
.bConfigurationValue = DEV_CONFIG_VALUE,
|
|
|
|
.iConfiguration = STRING_CONFIG,
|
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
|
|
|
|
.bMaxPower = 0x32,
|
|
|
|
.nif = ARRAY_SIZE(desc_iface_multi),
|
|
|
|
.ifs = desc_iface_multi,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDesc desc_audio_multi = {
|
|
|
|
.id = {
|
|
|
|
.idVendor = USBAUDIO_VENDOR_NUM,
|
|
|
|
.idProduct = USBAUDIO_PRODUCT_NUM,
|
|
|
|
.bcdDevice = 0,
|
|
|
|
.iManufacturer = STRING_MANUFACTURER,
|
|
|
|
.iProduct = STRING_PRODUCT,
|
|
|
|
.iSerialNumber = STRING_SERIALNUMBER,
|
|
|
|
},
|
|
|
|
.full = &desc_device_multi,
|
|
|
|
.str = usb_audio_stringtable,
|
2010-09-11 01:47:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class-specific control requests
|
|
|
|
*/
|
|
|
|
#define CR_SET_CUR 0x01
|
|
|
|
#define CR_GET_CUR 0x81
|
|
|
|
#define CR_SET_MIN 0x02
|
|
|
|
#define CR_GET_MIN 0x82
|
|
|
|
#define CR_SET_MAX 0x03
|
|
|
|
#define CR_GET_MAX 0x83
|
|
|
|
#define CR_SET_RES 0x04
|
|
|
|
#define CR_GET_RES 0x84
|
|
|
|
#define CR_SET_MEM 0x05
|
|
|
|
#define CR_GET_MEM 0x85
|
|
|
|
#define CR_GET_STAT 0xff
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Feature Unit Control Selectors
|
|
|
|
*/
|
|
|
|
#define MUTE_CONTROL 0x01
|
|
|
|
#define VOLUME_CONTROL 0x02
|
|
|
|
#define BASS_CONTROL 0x03
|
|
|
|
#define MID_CONTROL 0x04
|
|
|
|
#define TREBLE_CONTROL 0x05
|
|
|
|
#define GRAPHIC_EQUALIZER_CONTROL 0x06
|
|
|
|
#define AUTOMATIC_GAIN_CONTROL 0x07
|
|
|
|
#define DELAY_CONTROL 0x08
|
|
|
|
#define BASS_BOOST_CONTROL 0x09
|
|
|
|
#define LOUDNESS_CONTROL 0x0a
|
|
|
|
|
|
|
|
/*
|
|
|
|
* buffering
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct streambuf {
|
|
|
|
uint8_t *data;
|
2019-10-13 22:58:06 +03:00
|
|
|
size_t size;
|
|
|
|
uint64_t prod;
|
|
|
|
uint64_t cons;
|
2010-09-11 01:47:56 +04:00
|
|
|
};
|
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
static void streambuf_init(struct streambuf *buf, uint32_t size,
|
|
|
|
uint32_t channels)
|
2010-09-11 01:47:56 +04:00
|
|
|
{
|
|
|
|
g_free(buf->data);
|
2019-10-13 22:58:05 +03:00
|
|
|
buf->size = size - (size % USBAUDIO_PACKET_SIZE(channels));
|
2010-09-11 01:47:56 +04:00
|
|
|
buf->data = g_malloc(buf->size);
|
|
|
|
buf->prod = 0;
|
|
|
|
buf->cons = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void streambuf_fini(struct streambuf *buf)
|
|
|
|
{
|
|
|
|
g_free(buf->data);
|
|
|
|
buf->data = NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)
|
2010-09-11 01:47:56 +04:00
|
|
|
{
|
2019-10-13 22:58:06 +03:00
|
|
|
int64_t free = buf->size - (buf->prod - buf->cons);
|
2010-09-11 01:47:56 +04:00
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
if (free < USBAUDIO_PACKET_SIZE(channels)) {
|
2010-09-11 01:47:56 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2019-10-13 22:58:05 +03:00
|
|
|
if (p->iov.size != USBAUDIO_PACKET_SIZE(channels)) {
|
2018-12-11 10:26:49 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2019-10-13 22:58:04 +03:00
|
|
|
|
2019-10-13 22:58:06 +03:00
|
|
|
/* can happen if prod overflows */
|
|
|
|
assert(buf->prod % USBAUDIO_PACKET_SIZE(channels) == 0);
|
2010-09-11 01:47:56 +04:00
|
|
|
usb_packet_copy(p, buf->data + (buf->prod % buf->size),
|
2019-10-13 22:58:05 +03:00
|
|
|
USBAUDIO_PACKET_SIZE(channels));
|
|
|
|
buf->prod += USBAUDIO_PACKET_SIZE(channels);
|
|
|
|
return USBAUDIO_PACKET_SIZE(channels);
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
|
|
|
|
2019-10-13 22:58:04 +03:00
|
|
|
static uint8_t *streambuf_get(struct streambuf *buf, size_t *len)
|
2010-09-11 01:47:56 +04:00
|
|
|
{
|
2019-10-13 22:58:06 +03:00
|
|
|
int64_t used = buf->prod - buf->cons;
|
2010-09-11 01:47:56 +04:00
|
|
|
uint8_t *data;
|
|
|
|
|
2019-10-13 22:58:06 +03:00
|
|
|
if (used <= 0) {
|
2019-10-13 22:58:04 +03:00
|
|
|
*len = 0;
|
2010-09-11 01:47:56 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
data = buf->data + (buf->cons % buf->size);
|
2019-10-13 22:58:04 +03:00
|
|
|
*len = MIN(buf->prod - buf->cons,
|
|
|
|
buf->size - (buf->cons % buf->size));
|
2010-09-11 01:47:56 +04:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct USBAudioState {
|
2010-09-11 01:47:56 +04:00
|
|
|
/* qemu interfaces */
|
|
|
|
USBDevice dev;
|
|
|
|
QEMUSoundCard card;
|
|
|
|
|
|
|
|
/* state */
|
|
|
|
struct {
|
|
|
|
enum usb_audio_altset altset;
|
|
|
|
struct audsettings as;
|
|
|
|
SWVoiceOut *voice;
|
2019-10-13 22:58:05 +03:00
|
|
|
Volume vol;
|
2010-09-11 01:47:56 +04:00
|
|
|
struct streambuf buf;
|
2019-10-13 22:58:05 +03:00
|
|
|
uint32_t channels;
|
2010-09-11 01:47:56 +04:00
|
|
|
} out;
|
|
|
|
|
|
|
|
/* properties */
|
|
|
|
uint32_t debug;
|
2019-10-13 22:58:05 +03:00
|
|
|
uint32_t buffer_user, buffer;
|
|
|
|
bool multi;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2010-09-11 01:47:56 +04:00
|
|
|
|
2015-05-06 15:55:24 +03:00
|
|
|
#define TYPE_USB_AUDIO "usb-audio"
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(USBAudioState, USB_AUDIO)
|
2015-05-06 15:55:24 +03:00
|
|
|
|
2010-09-11 01:47:56 +04:00
|
|
|
static void output_callback(void *opaque, int avail)
|
|
|
|
{
|
|
|
|
USBAudioState *s = opaque;
|
|
|
|
uint8_t *data;
|
|
|
|
|
2019-10-13 22:58:04 +03:00
|
|
|
while (avail) {
|
|
|
|
size_t written, len;
|
|
|
|
|
|
|
|
data = streambuf_get(&s->out.buf, &len);
|
|
|
|
if (!data) {
|
2010-09-11 01:47:56 +04:00
|
|
|
return;
|
|
|
|
}
|
2019-10-13 22:58:04 +03:00
|
|
|
|
|
|
|
written = AUD_write(s->out.voice, data, len);
|
|
|
|
avail -= written;
|
|
|
|
s->out.buf.cons += written;
|
|
|
|
|
|
|
|
if (written < len) {
|
2010-09-11 01:47:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_audio_set_output_altset(USBAudioState *s, int altset)
|
|
|
|
{
|
|
|
|
switch (altset) {
|
|
|
|
case ALTSET_OFF:
|
|
|
|
AUD_set_active_out(s->out.voice, false);
|
|
|
|
break;
|
2019-10-13 22:58:05 +03:00
|
|
|
case ALTSET_STEREO:
|
|
|
|
case ALTSET_51:
|
|
|
|
case ALTSET_71:
|
|
|
|
if (s->out.channels != altset_channels[altset]) {
|
|
|
|
usb_audio_reinit(USB_DEVICE(s), altset_channels[altset]);
|
|
|
|
}
|
|
|
|
streambuf_init(&s->out.buf, s->buffer, s->out.channels);
|
2010-09-11 01:47:56 +04:00
|
|
|
AUD_set_active_out(s->out.voice, true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->debug) {
|
|
|
|
fprintf(stderr, "usb-audio: set interface %d\n", altset);
|
|
|
|
}
|
|
|
|
s->out.altset = altset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: we arbitrarily map the volume control range onto -inf..+8 dB
|
|
|
|
*/
|
|
|
|
#define ATTRIB_ID(cs, attrib, idif) \
|
|
|
|
(((cs) << 24) | ((attrib) << 16) | (idif))
|
|
|
|
|
|
|
|
static int usb_audio_get_control(USBAudioState *s, uint8_t attrib,
|
|
|
|
uint16_t cscn, uint16_t idif,
|
|
|
|
int length, uint8_t *data)
|
|
|
|
{
|
|
|
|
uint8_t cs = cscn >> 8;
|
|
|
|
uint8_t cn = cscn - 1; /* -1 for the non-present master control */
|
|
|
|
uint32_t aid = ATTRIB_ID(cs, attrib, idif);
|
|
|
|
int ret = USB_RET_STALL;
|
|
|
|
|
|
|
|
switch (aid) {
|
|
|
|
case ATTRIB_ID(MUTE_CONTROL, CR_GET_CUR, 0x0200):
|
2019-10-13 22:58:05 +03:00
|
|
|
data[0] = s->out.vol.mute;
|
2010-09-11 01:47:56 +04:00
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case ATTRIB_ID(VOLUME_CONTROL, CR_GET_CUR, 0x0200):
|
2019-10-13 22:58:05 +03:00
|
|
|
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
|
|
|
|
uint16_t vol = (s->out.vol.vol[cn] * 0x8800 + 127) / 255 + 0x8000;
|
2010-09-11 01:47:56 +04:00
|
|
|
data[0] = vol;
|
|
|
|
data[1] = vol >> 8;
|
|
|
|
ret = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATTRIB_ID(VOLUME_CONTROL, CR_GET_MIN, 0x0200):
|
2019-10-13 22:58:05 +03:00
|
|
|
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
|
2010-09-11 01:47:56 +04:00
|
|
|
data[0] = 0x01;
|
|
|
|
data[1] = 0x80;
|
|
|
|
ret = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATTRIB_ID(VOLUME_CONTROL, CR_GET_MAX, 0x0200):
|
2019-10-13 22:58:05 +03:00
|
|
|
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
|
2010-09-11 01:47:56 +04:00
|
|
|
data[0] = 0x00;
|
|
|
|
data[1] = 0x08;
|
|
|
|
ret = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ATTRIB_ID(VOLUME_CONTROL, CR_GET_RES, 0x0200):
|
2019-10-13 22:58:05 +03:00
|
|
|
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
|
2010-09-11 01:47:56 +04:00
|
|
|
data[0] = 0x88;
|
|
|
|
data[1] = 0x00;
|
|
|
|
ret = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
static int usb_audio_set_control(USBAudioState *s, uint8_t attrib,
|
|
|
|
uint16_t cscn, uint16_t idif,
|
|
|
|
int length, uint8_t *data)
|
|
|
|
{
|
|
|
|
uint8_t cs = cscn >> 8;
|
|
|
|
uint8_t cn = cscn - 1; /* -1 for the non-present master control */
|
|
|
|
uint32_t aid = ATTRIB_ID(cs, attrib, idif);
|
|
|
|
int ret = USB_RET_STALL;
|
|
|
|
bool set_vol = false;
|
|
|
|
|
|
|
|
switch (aid) {
|
|
|
|
case ATTRIB_ID(MUTE_CONTROL, CR_SET_CUR, 0x0200):
|
2019-10-13 22:58:05 +03:00
|
|
|
s->out.vol.mute = data[0] & 1;
|
2010-09-11 01:47:56 +04:00
|
|
|
set_vol = true;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case ATTRIB_ID(VOLUME_CONTROL, CR_SET_CUR, 0x0200):
|
2019-10-13 22:58:05 +03:00
|
|
|
if (cn < USBAUDIO_MAX_CHANNELS(s)) {
|
2010-09-11 01:47:56 +04:00
|
|
|
uint16_t vol = data[0] + (data[1] << 8);
|
|
|
|
|
|
|
|
if (s->debug) {
|
2019-10-13 22:58:05 +03:00
|
|
|
fprintf(stderr, "usb-audio: cn %d vol %04x\n", cn,
|
|
|
|
(uint16_t)vol);
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
vol -= 0x8000;
|
|
|
|
vol = (vol * 255 + 0x4400) / 0x8800;
|
|
|
|
if (vol > 255) {
|
|
|
|
vol = 255;
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
s->out.vol.vol[cn] = vol;
|
2010-09-11 01:47:56 +04:00
|
|
|
set_vol = true;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (set_vol) {
|
|
|
|
if (s->debug) {
|
2019-10-13 22:58:05 +03:00
|
|
|
int i;
|
|
|
|
fprintf(stderr, "usb-audio: mute %d", s->out.vol.mute);
|
|
|
|
for (i = 0; i < USBAUDIO_MAX_CHANNELS(s); ++i) {
|
|
|
|
fprintf(stderr, ", vol[%d] %3d", i, s->out.vol.vol[i]);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
2019-10-13 22:58:05 +03:00
|
|
|
audio_set_volume_out(s->out.voice, &s->out.vol);
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
static void usb_audio_handle_control(USBDevice *dev, USBPacket *p,
|
2010-09-11 01:47:56 +04:00
|
|
|
int request, int value, int index,
|
|
|
|
int length, uint8_t *data)
|
|
|
|
{
|
2015-05-06 15:55:24 +03:00
|
|
|
USBAudioState *s = USB_AUDIO(dev);
|
2010-09-11 01:47:56 +04:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (s->debug) {
|
|
|
|
fprintf(stderr, "usb-audio: control transaction: "
|
|
|
|
"request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
|
|
|
|
request, value, index, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
|
|
|
|
if (ret >= 0) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
return;
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case ClassInterfaceRequest | CR_GET_CUR:
|
|
|
|
case ClassInterfaceRequest | CR_GET_MIN:
|
|
|
|
case ClassInterfaceRequest | CR_GET_MAX:
|
|
|
|
case ClassInterfaceRequest | CR_GET_RES:
|
|
|
|
ret = usb_audio_get_control(s, request & 0xff, value, index,
|
|
|
|
length, data);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (s->debug) {
|
|
|
|
fprintf(stderr, "usb-audio: fail: get control\n");
|
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->actual_length = ret;
|
2010-09-11 01:47:56 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ClassInterfaceOutRequest | CR_SET_CUR:
|
|
|
|
case ClassInterfaceOutRequest | CR_SET_MIN:
|
|
|
|
case ClassInterfaceOutRequest | CR_SET_MAX:
|
|
|
|
case ClassInterfaceOutRequest | CR_SET_RES:
|
|
|
|
ret = usb_audio_set_control(s, request & 0xff, value, index,
|
|
|
|
length, data);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (s->debug) {
|
|
|
|
fprintf(stderr, "usb-audio: fail: set control\n");
|
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fail:
|
|
|
|
if (s->debug) {
|
|
|
|
fprintf(stderr, "usb-audio: failed control transaction: "
|
|
|
|
"request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
|
|
|
|
request, value, index, length);
|
|
|
|
}
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_STALL;
|
2010-09-11 01:47:56 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_audio_set_interface(USBDevice *dev, int iface,
|
|
|
|
int old, int value)
|
|
|
|
{
|
2015-05-06 15:55:24 +03:00
|
|
|
USBAudioState *s = USB_AUDIO(dev);
|
2010-09-11 01:47:56 +04:00
|
|
|
|
|
|
|
if (iface == 1) {
|
|
|
|
usb_audio_set_output_altset(s, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_audio_handle_reset(USBDevice *dev)
|
|
|
|
{
|
2015-05-06 15:55:24 +03:00
|
|
|
USBAudioState *s = USB_AUDIO(dev);
|
2010-09-11 01:47:56 +04:00
|
|
|
|
|
|
|
if (s->debug) {
|
|
|
|
fprintf(stderr, "usb-audio: reset\n");
|
|
|
|
}
|
|
|
|
usb_audio_set_output_altset(s, ALTSET_OFF);
|
|
|
|
}
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
static void usb_audio_handle_dataout(USBAudioState *s, USBPacket *p)
|
2010-09-11 01:47:56 +04:00
|
|
|
{
|
|
|
|
if (s->out.altset == ALTSET_OFF) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_STALL;
|
|
|
|
return;
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
streambuf_put(&s->out.buf, p, s->out.channels);
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
if (p->actual_length < p->iov.size && s->debug > 1) {
|
2010-09-11 01:47:56 +04:00
|
|
|
fprintf(stderr, "usb-audio: output overrun (%zd bytes)\n",
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->iov.size - p->actual_length);
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
static void usb_audio_handle_data(USBDevice *dev, USBPacket *p)
|
2010-09-11 01:47:56 +04:00
|
|
|
{
|
|
|
|
USBAudioState *s = (USBAudioState *) dev;
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
if (p->pid == USB_TOKEN_OUT && p->ep->nr == 1) {
|
|
|
|
usb_audio_handle_dataout(s, p);
|
|
|
|
return;
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
|
|
|
|
p->status = USB_RET_STALL;
|
|
|
|
if (s->debug) {
|
2010-09-11 01:47:56 +04:00
|
|
|
fprintf(stderr, "usb-audio: failed data transaction: "
|
|
|
|
"pid 0x%x ep 0x%x len 0x%zx\n",
|
2012-01-12 16:23:01 +04:00
|
|
|
p->pid, p->ep->nr, p->iov.size);
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 18:29:24 +03:00
|
|
|
static void usb_audio_unrealize(USBDevice *dev)
|
2010-09-11 01:47:56 +04:00
|
|
|
{
|
2015-05-06 15:55:24 +03:00
|
|
|
USBAudioState *s = USB_AUDIO(dev);
|
2010-09-11 01:47:56 +04:00
|
|
|
|
|
|
|
if (s->debug) {
|
|
|
|
fprintf(stderr, "usb-audio: destroy\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_audio_set_output_altset(s, ALTSET_OFF);
|
|
|
|
AUD_close_out(&s->card, s->out.voice);
|
|
|
|
AUD_remove_card(&s->card);
|
|
|
|
|
|
|
|
streambuf_fini(&s->out.buf);
|
|
|
|
}
|
|
|
|
|
2014-09-19 10:48:38 +04:00
|
|
|
static void usb_audio_realize(USBDevice *dev, Error **errp)
|
2010-09-11 01:47:56 +04:00
|
|
|
{
|
2015-05-06 15:55:24 +03:00
|
|
|
USBAudioState *s = USB_AUDIO(dev);
|
2019-10-13 22:58:05 +03:00
|
|
|
int i;
|
|
|
|
|
2023-10-02 17:27:57 +03:00
|
|
|
if (!AUD_register_card(TYPE_USB_AUDIO, &s->card, errp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:58:05 +03:00
|
|
|
dev->usb_desc = s->multi ? &desc_audio_multi : &desc_audio;
|
2010-09-11 01:47:56 +04:00
|
|
|
|
2012-04-20 14:33:30 +04:00
|
|
|
usb_desc_create_serial(dev);
|
2010-09-11 01:47:56 +04:00
|
|
|
usb_desc_init(dev);
|
|
|
|
s->dev.opaque = s;
|
|
|
|
|
|
|
|
s->out.altset = ALTSET_OFF;
|
2019-10-13 22:58:05 +03:00
|
|
|
s->out.vol.mute = false;
|
|
|
|
for (i = 0; i < USBAUDIO_MAX_CHANNELS(s); ++i) {
|
|
|
|
s->out.vol.vol[i] = 240; /* 0 dB */
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_audio_reinit(dev, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_audio_reinit(USBDevice *dev, unsigned channels)
|
|
|
|
{
|
|
|
|
USBAudioState *s = USB_AUDIO(dev);
|
|
|
|
|
|
|
|
s->out.channels = channels;
|
|
|
|
if (!s->buffer_user) {
|
|
|
|
s->buffer = 32 * USBAUDIO_PACKET_SIZE(s->out.channels);
|
|
|
|
} else {
|
|
|
|
s->buffer = s->buffer_user;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->out.vol.channels = s->out.channels;
|
2010-09-11 01:47:56 +04:00
|
|
|
s->out.as.freq = USBAUDIO_SAMPLE_RATE;
|
2019-10-13 22:58:05 +03:00
|
|
|
s->out.as.nchannels = s->out.channels;
|
2019-03-09 01:34:13 +03:00
|
|
|
s->out.as.fmt = AUDIO_FORMAT_S16;
|
2010-09-11 01:47:56 +04:00
|
|
|
s->out.as.endianness = 0;
|
2019-10-13 22:58:05 +03:00
|
|
|
streambuf_init(&s->out.buf, s->buffer, s->out.channels);
|
2010-09-11 01:47:56 +04:00
|
|
|
|
2015-05-06 15:55:24 +03:00
|
|
|
s->out.voice = AUD_open_out(&s->card, s->out.voice, TYPE_USB_AUDIO,
|
2010-09-11 01:47:56 +04:00
|
|
|
s, output_callback, &s->out.as);
|
2019-10-13 22:58:05 +03:00
|
|
|
audio_set_volume_out(s->out.voice, &s->out.vol);
|
2010-09-11 01:47:56 +04:00
|
|
|
AUD_set_active_out(s->out.voice, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_usb_audio = {
|
2015-05-06 15:55:24 +03:00
|
|
|
.name = TYPE_USB_AUDIO,
|
2010-09-11 01:47:56 +04:00
|
|
|
.unmigratable = 1,
|
|
|
|
};
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static Property usb_audio_properties[] = {
|
2019-08-19 02:06:49 +03:00
|
|
|
DEFINE_AUDIO_PROPERTIES(USBAudioState, card),
|
2011-12-08 07:34:16 +04:00
|
|
|
DEFINE_PROP_UINT32("debug", USBAudioState, debug, 0),
|
2019-10-13 22:58:05 +03:00
|
|
|
DEFINE_PROP_UINT32("buffer", USBAudioState, buffer_user, 0),
|
|
|
|
DEFINE_PROP_BOOL("multi", USBAudioState, multi, false),
|
2011-12-08 07:34:16 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2011-12-16 00:53:10 +04:00
|
|
|
static void usb_audio_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-16 00:53:10 +04:00
|
|
|
USBDeviceClass *k = USB_DEVICE_CLASS(klass);
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_usb_audio;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, usb_audio_properties);
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
|
2011-12-16 00:53:10 +04:00
|
|
|
k->product_desc = "QEMU USB Audio Interface";
|
2014-09-19 10:48:38 +04:00
|
|
|
k->realize = usb_audio_realize;
|
2011-12-16 00:53:10 +04:00
|
|
|
k->handle_reset = usb_audio_handle_reset;
|
|
|
|
k->handle_control = usb_audio_handle_control;
|
|
|
|
k->handle_data = usb_audio_handle_data;
|
2017-02-21 17:14:45 +03:00
|
|
|
k->unrealize = usb_audio_unrealize;
|
2011-12-16 00:53:10 +04:00
|
|
|
k->set_interface = usb_audio_set_interface;
|
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo usb_audio_info = {
|
2015-05-06 15:55:24 +03:00
|
|
|
.name = TYPE_USB_AUDIO,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_USB_DEVICE,
|
|
|
|
.instance_size = sizeof(USBAudioState),
|
|
|
|
.class_init = usb_audio_class_init,
|
2010-09-11 01:47:56 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void usb_audio_register_types(void)
|
2010-09-11 01:47:56 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&usb_audio_info);
|
2010-09-11 01:47:56 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(usb_audio_register_types)
|