]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/dev-audio.c
hw/net: Clean up includes
[mirror_qemu.git] / hw / usb / dev-audio.c
CommitLineData
b870472d
PA
1/*
2 * QEMU USB audio device
3 *
4 * written by:
5 * H. Peter Anvin <hpa@linux.intel.com>
6 * Gerd Hoffmann <kraxel@redhat.com>
7 *
8 * lousely based on usb net device code which is:
9 *
10 * Copyright (c) 2006 Thomas Sailer
11 * Copyright (c) 2008 Andrzej Zaborowski
12 *
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:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
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
29 * THE SOFTWARE.
30 */
31
32#include "qemu-common.h"
f1ae32a1
GH
33#include "hw/usb.h"
34#include "hw/usb/desc.h"
35#include "hw/hw.h"
b870472d
PA
36#include "audio/audio.h"
37
38#define USBAUDIO_VENDOR_NUM 0x46f4 /* CRC16() of "QEMU" */
39#define USBAUDIO_PRODUCT_NUM 0x0002
40
41#define DEV_CONFIG_VALUE 1 /* The one and only */
42
43/* Descriptor subtypes for AC interfaces */
44#define DST_AC_HEADER 1
45#define DST_AC_INPUT_TERMINAL 2
46#define DST_AC_OUTPUT_TERMINAL 3
47#define DST_AC_FEATURE_UNIT 6
48/* Descriptor subtypes for AS interfaces */
49#define DST_AS_GENERAL 1
50#define DST_AS_FORMAT_TYPE 2
51/* Descriptor subtypes for endpoints */
52#define DST_EP_GENERAL 1
53
54enum usb_audio_strings {
55 STRING_NULL,
56 STRING_MANUFACTURER,
57 STRING_PRODUCT,
58 STRING_SERIALNUMBER,
59 STRING_CONFIG,
60 STRING_USBAUDIO_CONTROL,
61 STRING_INPUT_TERMINAL,
62 STRING_FEATURE_UNIT,
63 STRING_OUTPUT_TERMINAL,
64 STRING_NULL_STREAM,
65 STRING_REAL_STREAM,
66};
67
68static const USBDescStrings usb_audio_stringtable = {
69 [STRING_MANUFACTURER] = "QEMU",
70 [STRING_PRODUCT] = "QEMU USB Audio",
71 [STRING_SERIALNUMBER] = "1",
72 [STRING_CONFIG] = "Audio Configuration",
73 [STRING_USBAUDIO_CONTROL] = "Audio Device",
74 [STRING_INPUT_TERMINAL] = "Audio Output Pipe",
75 [STRING_FEATURE_UNIT] = "Audio Output Volume Control",
76 [STRING_OUTPUT_TERMINAL] = "Audio Output Terminal",
77 [STRING_NULL_STREAM] = "Audio Output - Disabled",
78 [STRING_REAL_STREAM] = "Audio Output - 48 kHz Stereo",
79};
80
81#define U16(x) ((x) & 0xff), (((x) >> 8) & 0xff)
82#define U24(x) U16(x), (((x) >> 16) & 0xff)
83#define U32(x) U24(x), (((x) >> 24) & 0xff)
84
85/*
86 * A Basic Audio Device uses these specific values
87 */
88#define USBAUDIO_PACKET_SIZE 192
89#define USBAUDIO_SAMPLE_RATE 48000
90#define USBAUDIO_PACKET_INTERVAL 1
91
92static const USBDescIface desc_iface[] = {
93 {
94 .bInterfaceNumber = 0,
95 .bNumEndpoints = 0,
96 .bInterfaceClass = USB_CLASS_AUDIO,
97 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL,
98 .bInterfaceProtocol = 0x04,
99 .iInterface = STRING_USBAUDIO_CONTROL,
100 .ndesc = 4,
101 .descs = (USBDescOther[]) {
102 {
103 /* Headphone Class-Specific AC Interface Header Descriptor */
104 .data = (uint8_t[]) {
105 0x09, /* u8 bLength */
106 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
107 DST_AC_HEADER, /* u8 bDescriptorSubtype */
108 U16(0x0100), /* u16 bcdADC */
109 U16(0x2b), /* u16 wTotalLength */
110 0x01, /* u8 bInCollection */
111 0x01, /* u8 baInterfaceNr */
112 }
113 },{
114 /* Generic Stereo Input Terminal ID1 Descriptor */
115 .data = (uint8_t[]) {
116 0x0c, /* u8 bLength */
117 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
118 DST_AC_INPUT_TERMINAL, /* u8 bDescriptorSubtype */
119 0x01, /* u8 bTerminalID */
120 U16(0x0101), /* u16 wTerminalType */
121 0x00, /* u8 bAssocTerminal */
122 0x02, /* u16 bNrChannels */
123 U16(0x0003), /* u16 wChannelConfig */
124 0x00, /* u8 iChannelNames */
125 STRING_INPUT_TERMINAL, /* u8 iTerminal */
126 }
127 },{
128 /* Generic Stereo Feature Unit ID2 Descriptor */
129 .data = (uint8_t[]) {
130 0x0d, /* u8 bLength */
131 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
132 DST_AC_FEATURE_UNIT, /* u8 bDescriptorSubtype */
133 0x02, /* u8 bUnitID */
134 0x01, /* u8 bSourceID */
135 0x02, /* u8 bControlSize */
136 U16(0x0001), /* u16 bmaControls(0) */
137 U16(0x0002), /* u16 bmaControls(1) */
138 U16(0x0002), /* u16 bmaControls(2) */
139 STRING_FEATURE_UNIT, /* u8 iFeature */
140 }
141 },{
142 /* Headphone Ouptut Terminal ID3 Descriptor */
143 .data = (uint8_t[]) {
144 0x09, /* u8 bLength */
145 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
146 DST_AC_OUTPUT_TERMINAL, /* u8 bDescriptorSubtype */
147 0x03, /* u8 bUnitID */
148 U16(0x0301), /* u16 wTerminalType (SPK) */
149 0x00, /* u8 bAssocTerminal */
150 0x02, /* u8 bSourceID */
151 STRING_OUTPUT_TERMINAL, /* u8 iTerminal */
152 }
153 }
154 },
155 },{
156 .bInterfaceNumber = 1,
157 .bAlternateSetting = 0,
158 .bNumEndpoints = 0,
159 .bInterfaceClass = USB_CLASS_AUDIO,
160 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
161 .iInterface = STRING_NULL_STREAM,
162 },{
163 .bInterfaceNumber = 1,
164 .bAlternateSetting = 1,
165 .bNumEndpoints = 1,
166 .bInterfaceClass = USB_CLASS_AUDIO,
167 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
168 .iInterface = STRING_REAL_STREAM,
169 .ndesc = 2,
170 .descs = (USBDescOther[]) {
171 {
172 /* Headphone Class-specific AS General Interface Descriptor */
173 .data = (uint8_t[]) {
174 0x07, /* u8 bLength */
175 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
176 DST_AS_GENERAL, /* u8 bDescriptorSubtype */
177 0x01, /* u8 bTerminalLink */
178 0x00, /* u8 bDelay */
179 0x01, 0x00, /* u16 wFormatTag */
180 }
181 },{
182 /* Headphone Type I Format Type Descriptor */
183 .data = (uint8_t[]) {
184 0x0b, /* u8 bLength */
185 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
186 DST_AS_FORMAT_TYPE, /* u8 bDescriptorSubtype */
187 0x01, /* u8 bFormatType */
188 0x02, /* u8 bNrChannels */
189 0x02, /* u8 bSubFrameSize */
190 0x10, /* u8 bBitResolution */
191 0x01, /* u8 bSamFreqType */
192 U24(USBAUDIO_SAMPLE_RATE), /* u24 tSamFreq */
193 }
194 }
195 },
196 .eps = (USBDescEndpoint[]) {
197 {
198 .bEndpointAddress = USB_DIR_OUT | 0x01,
199 .bmAttributes = 0x0d,
200 .wMaxPacketSize = USBAUDIO_PACKET_SIZE,
201 .bInterval = 1,
202 .is_audio = 1,
203 /* Stereo Headphone Class-specific
204 AS Audio Data Endpoint Descriptor */
205 .extra = (uint8_t[]) {
206 0x07, /* u8 bLength */
207 USB_DT_CS_ENDPOINT, /* u8 bDescriptorType */
208 DST_EP_GENERAL, /* u8 bDescriptorSubtype */
209 0x00, /* u8 bmAttributes */
210 0x00, /* u8 bLockDelayUnits */
211 U16(0x0000), /* u16 wLockDelay */
212 },
213 },
214 }
215 }
216};
217
218static const USBDescDevice desc_device = {
2bbd086c 219 .bcdUSB = 0x0100,
b870472d
PA
220 .bMaxPacketSize0 = 64,
221 .bNumConfigurations = 1,
222 .confs = (USBDescConfig[]) {
223 {
224 .bNumInterfaces = 2,
225 .bConfigurationValue = DEV_CONFIG_VALUE,
226 .iConfiguration = STRING_CONFIG,
bd93976a 227 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
b870472d
PA
228 .bMaxPower = 0x32,
229 .nif = ARRAY_SIZE(desc_iface),
230 .ifs = desc_iface,
231 },
232 },
233};
234
235static const USBDesc desc_audio = {
236 .id = {
237 .idVendor = USBAUDIO_VENDOR_NUM,
238 .idProduct = USBAUDIO_PRODUCT_NUM,
239 .bcdDevice = 0,
240 .iManufacturer = STRING_MANUFACTURER,
241 .iProduct = STRING_PRODUCT,
242 .iSerialNumber = STRING_SERIALNUMBER,
243 },
244 .full = &desc_device,
245 .str = usb_audio_stringtable,
246};
247
248/*
249 * A USB audio device supports an arbitrary number of alternate
250 * interface settings for each interface. Each corresponds to a block
251 * diagram of parameterized blocks. This can thus refer to things like
252 * number of channels, data rates, or in fact completely different
253 * block diagrams. Alternative setting 0 is always the null block diagram,
254 * which is used by a disabled device.
255 */
256enum usb_audio_altset {
257 ALTSET_OFF = 0x00, /* No endpoint */
258 ALTSET_ON = 0x01, /* Single endpoint */
259};
260
261/*
262 * Class-specific control requests
263 */
264#define CR_SET_CUR 0x01
265#define CR_GET_CUR 0x81
266#define CR_SET_MIN 0x02
267#define CR_GET_MIN 0x82
268#define CR_SET_MAX 0x03
269#define CR_GET_MAX 0x83
270#define CR_SET_RES 0x04
271#define CR_GET_RES 0x84
272#define CR_SET_MEM 0x05
273#define CR_GET_MEM 0x85
274#define CR_GET_STAT 0xff
275
276/*
277 * Feature Unit Control Selectors
278 */
279#define MUTE_CONTROL 0x01
280#define VOLUME_CONTROL 0x02
281#define BASS_CONTROL 0x03
282#define MID_CONTROL 0x04
283#define TREBLE_CONTROL 0x05
284#define GRAPHIC_EQUALIZER_CONTROL 0x06
285#define AUTOMATIC_GAIN_CONTROL 0x07
286#define DELAY_CONTROL 0x08
287#define BASS_BOOST_CONTROL 0x09
288#define LOUDNESS_CONTROL 0x0a
289
290/*
291 * buffering
292 */
293
294struct streambuf {
295 uint8_t *data;
296 uint32_t size;
297 uint32_t prod;
298 uint32_t cons;
299};
300
301static void streambuf_init(struct streambuf *buf, uint32_t size)
302{
303 g_free(buf->data);
304 buf->size = size - (size % USBAUDIO_PACKET_SIZE);
305 buf->data = g_malloc(buf->size);
306 buf->prod = 0;
307 buf->cons = 0;
308}
309
310static void streambuf_fini(struct streambuf *buf)
311{
312 g_free(buf->data);
313 buf->data = NULL;
314}
315
316static int streambuf_put(struct streambuf *buf, USBPacket *p)
317{
318 uint32_t free = buf->size - (buf->prod - buf->cons);
319
320 if (!free) {
321 return 0;
322 }
323 assert(free >= USBAUDIO_PACKET_SIZE);
324 usb_packet_copy(p, buf->data + (buf->prod % buf->size),
325 USBAUDIO_PACKET_SIZE);
326 buf->prod += USBAUDIO_PACKET_SIZE;
327 return USBAUDIO_PACKET_SIZE;
328}
329
330static uint8_t *streambuf_get(struct streambuf *buf)
331{
332 uint32_t used = buf->prod - buf->cons;
333 uint8_t *data;
334
335 if (!used) {
336 return NULL;
337 }
338 assert(used >= USBAUDIO_PACKET_SIZE);
339 data = buf->data + (buf->cons % buf->size);
340 buf->cons += USBAUDIO_PACKET_SIZE;
341 return data;
342}
343
344typedef struct USBAudioState {
345 /* qemu interfaces */
346 USBDevice dev;
347 QEMUSoundCard card;
348
349 /* state */
350 struct {
351 enum usb_audio_altset altset;
352 struct audsettings as;
353 SWVoiceOut *voice;
354 bool mute;
355 uint8_t vol[2];
356 struct streambuf buf;
357 } out;
358
359 /* properties */
360 uint32_t debug;
361 uint32_t buffer;
362} USBAudioState;
363
0389a0b1
GA
364#define TYPE_USB_AUDIO "usb-audio"
365#define USB_AUDIO(obj) OBJECT_CHECK(USBAudioState, (obj), TYPE_USB_AUDIO)
366
b870472d
PA
367static void output_callback(void *opaque, int avail)
368{
369 USBAudioState *s = opaque;
370 uint8_t *data;
371
372 for (;;) {
373 if (avail < USBAUDIO_PACKET_SIZE) {
374 return;
375 }
376 data = streambuf_get(&s->out.buf);
d0657b2a 377 if (!data) {
b870472d
PA
378 return;
379 }
380 AUD_write(s->out.voice, data, USBAUDIO_PACKET_SIZE);
381 avail -= USBAUDIO_PACKET_SIZE;
382 }
383}
384
385static int usb_audio_set_output_altset(USBAudioState *s, int altset)
386{
387 switch (altset) {
388 case ALTSET_OFF:
389 streambuf_init(&s->out.buf, s->buffer);
390 AUD_set_active_out(s->out.voice, false);
391 break;
392 case ALTSET_ON:
393 AUD_set_active_out(s->out.voice, true);
394 break;
395 default:
396 return -1;
397 }
398
399 if (s->debug) {
400 fprintf(stderr, "usb-audio: set interface %d\n", altset);
401 }
402 s->out.altset = altset;
403 return 0;
404}
405
406/*
407 * Note: we arbitrarily map the volume control range onto -inf..+8 dB
408 */
409#define ATTRIB_ID(cs, attrib, idif) \
410 (((cs) << 24) | ((attrib) << 16) | (idif))
411
412static int usb_audio_get_control(USBAudioState *s, uint8_t attrib,
413 uint16_t cscn, uint16_t idif,
414 int length, uint8_t *data)
415{
416 uint8_t cs = cscn >> 8;
417 uint8_t cn = cscn - 1; /* -1 for the non-present master control */
418 uint32_t aid = ATTRIB_ID(cs, attrib, idif);
419 int ret = USB_RET_STALL;
420
421 switch (aid) {
422 case ATTRIB_ID(MUTE_CONTROL, CR_GET_CUR, 0x0200):
423 data[0] = s->out.mute;
424 ret = 1;
425 break;
426 case ATTRIB_ID(VOLUME_CONTROL, CR_GET_CUR, 0x0200):
427 if (cn < 2) {
428 uint16_t vol = (s->out.vol[cn] * 0x8800 + 127) / 255 + 0x8000;
429 data[0] = vol;
430 data[1] = vol >> 8;
431 ret = 2;
432 }
433 break;
434 case ATTRIB_ID(VOLUME_CONTROL, CR_GET_MIN, 0x0200):
435 if (cn < 2) {
436 data[0] = 0x01;
437 data[1] = 0x80;
438 ret = 2;
439 }
440 break;
441 case ATTRIB_ID(VOLUME_CONTROL, CR_GET_MAX, 0x0200):
442 if (cn < 2) {
443 data[0] = 0x00;
444 data[1] = 0x08;
445 ret = 2;
446 }
447 break;
448 case ATTRIB_ID(VOLUME_CONTROL, CR_GET_RES, 0x0200):
449 if (cn < 2) {
450 data[0] = 0x88;
451 data[1] = 0x00;
452 ret = 2;
453 }
454 break;
455 }
456
457 return ret;
458}
459static int usb_audio_set_control(USBAudioState *s, uint8_t attrib,
460 uint16_t cscn, uint16_t idif,
461 int length, uint8_t *data)
462{
463 uint8_t cs = cscn >> 8;
464 uint8_t cn = cscn - 1; /* -1 for the non-present master control */
465 uint32_t aid = ATTRIB_ID(cs, attrib, idif);
466 int ret = USB_RET_STALL;
467 bool set_vol = false;
468
469 switch (aid) {
470 case ATTRIB_ID(MUTE_CONTROL, CR_SET_CUR, 0x0200):
471 s->out.mute = data[0] & 1;
472 set_vol = true;
473 ret = 0;
474 break;
475 case ATTRIB_ID(VOLUME_CONTROL, CR_SET_CUR, 0x0200):
476 if (cn < 2) {
477 uint16_t vol = data[0] + (data[1] << 8);
478
479 if (s->debug) {
480 fprintf(stderr, "usb-audio: vol %04x\n", (uint16_t)vol);
481 }
482
483 vol -= 0x8000;
484 vol = (vol * 255 + 0x4400) / 0x8800;
485 if (vol > 255) {
486 vol = 255;
487 }
488
489 s->out.vol[cn] = vol;
490 set_vol = true;
491 ret = 0;
492 }
493 break;
494 }
495
496 if (set_vol) {
497 if (s->debug) {
498 fprintf(stderr, "usb-audio: mute %d, lvol %3d, rvol %3d\n",
499 s->out.mute, s->out.vol[0], s->out.vol[1]);
500 }
501 AUD_set_volume_out(s->out.voice, s->out.mute,
502 s->out.vol[0], s->out.vol[1]);
503 }
504
505 return ret;
506}
507
9a77a0f5 508static void usb_audio_handle_control(USBDevice *dev, USBPacket *p,
b870472d
PA
509 int request, int value, int index,
510 int length, uint8_t *data)
511{
0389a0b1 512 USBAudioState *s = USB_AUDIO(dev);
b870472d
PA
513 int ret = 0;
514
515 if (s->debug) {
516 fprintf(stderr, "usb-audio: control transaction: "
517 "request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
518 request, value, index, length);
519 }
520
521 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
522 if (ret >= 0) {
9a77a0f5 523 return;
b870472d
PA
524 }
525
526 switch (request) {
527 case ClassInterfaceRequest | CR_GET_CUR:
528 case ClassInterfaceRequest | CR_GET_MIN:
529 case ClassInterfaceRequest | CR_GET_MAX:
530 case ClassInterfaceRequest | CR_GET_RES:
531 ret = usb_audio_get_control(s, request & 0xff, value, index,
532 length, data);
533 if (ret < 0) {
534 if (s->debug) {
535 fprintf(stderr, "usb-audio: fail: get control\n");
536 }
537 goto fail;
538 }
9a77a0f5 539 p->actual_length = ret;
b870472d
PA
540 break;
541
542 case ClassInterfaceOutRequest | CR_SET_CUR:
543 case ClassInterfaceOutRequest | CR_SET_MIN:
544 case ClassInterfaceOutRequest | CR_SET_MAX:
545 case ClassInterfaceOutRequest | CR_SET_RES:
546 ret = usb_audio_set_control(s, request & 0xff, value, index,
547 length, data);
548 if (ret < 0) {
549 if (s->debug) {
550 fprintf(stderr, "usb-audio: fail: set control\n");
551 }
552 goto fail;
553 }
554 break;
555
556 default:
557fail:
558 if (s->debug) {
559 fprintf(stderr, "usb-audio: failed control transaction: "
560 "request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
561 request, value, index, length);
562 }
9a77a0f5 563 p->status = USB_RET_STALL;
b870472d
PA
564 break;
565 }
b870472d
PA
566}
567
568static void usb_audio_set_interface(USBDevice *dev, int iface,
569 int old, int value)
570{
0389a0b1 571 USBAudioState *s = USB_AUDIO(dev);
b870472d
PA
572
573 if (iface == 1) {
574 usb_audio_set_output_altset(s, value);
575 }
576}
577
578static void usb_audio_handle_reset(USBDevice *dev)
579{
0389a0b1 580 USBAudioState *s = USB_AUDIO(dev);
b870472d
PA
581
582 if (s->debug) {
583 fprintf(stderr, "usb-audio: reset\n");
584 }
585 usb_audio_set_output_altset(s, ALTSET_OFF);
586}
587
9a77a0f5 588static void usb_audio_handle_dataout(USBAudioState *s, USBPacket *p)
b870472d 589{
b870472d 590 if (s->out.altset == ALTSET_OFF) {
9a77a0f5
HG
591 p->status = USB_RET_STALL;
592 return;
b870472d
PA
593 }
594
9a77a0f5
HG
595 streambuf_put(&s->out.buf, p);
596 if (p->actual_length < p->iov.size && s->debug > 1) {
b870472d 597 fprintf(stderr, "usb-audio: output overrun (%zd bytes)\n",
9a77a0f5 598 p->iov.size - p->actual_length);
b870472d 599 }
b870472d
PA
600}
601
9a77a0f5 602static void usb_audio_handle_data(USBDevice *dev, USBPacket *p)
b870472d
PA
603{
604 USBAudioState *s = (USBAudioState *) dev;
b870472d 605
9a77a0f5
HG
606 if (p->pid == USB_TOKEN_OUT && p->ep->nr == 1) {
607 usb_audio_handle_dataout(s, p);
608 return;
b870472d 609 }
9a77a0f5
HG
610
611 p->status = USB_RET_STALL;
612 if (s->debug) {
b870472d
PA
613 fprintf(stderr, "usb-audio: failed data transaction: "
614 "pid 0x%x ep 0x%x len 0x%zx\n",
079d0b7f 615 p->pid, p->ep->nr, p->iov.size);
b870472d 616 }
b870472d
PA
617}
618
619static void usb_audio_handle_destroy(USBDevice *dev)
620{
0389a0b1 621 USBAudioState *s = USB_AUDIO(dev);
b870472d
PA
622
623 if (s->debug) {
624 fprintf(stderr, "usb-audio: destroy\n");
625 }
626
627 usb_audio_set_output_altset(s, ALTSET_OFF);
628 AUD_close_out(&s->card, s->out.voice);
629 AUD_remove_card(&s->card);
630
631 streambuf_fini(&s->out.buf);
632}
633
5450eeaa 634static void usb_audio_realize(USBDevice *dev, Error **errp)
b870472d 635{
0389a0b1 636 USBAudioState *s = USB_AUDIO(dev);
b870472d 637
9d55d1ad 638 usb_desc_create_serial(dev);
b870472d
PA
639 usb_desc_init(dev);
640 s->dev.opaque = s;
0389a0b1 641 AUD_register_card(TYPE_USB_AUDIO, &s->card);
b870472d
PA
642
643 s->out.altset = ALTSET_OFF;
644 s->out.mute = false;
645 s->out.vol[0] = 240; /* 0 dB */
646 s->out.vol[1] = 240; /* 0 dB */
647 s->out.as.freq = USBAUDIO_SAMPLE_RATE;
648 s->out.as.nchannels = 2;
649 s->out.as.fmt = AUD_FMT_S16;
650 s->out.as.endianness = 0;
651 streambuf_init(&s->out.buf, s->buffer);
652
0389a0b1 653 s->out.voice = AUD_open_out(&s->card, s->out.voice, TYPE_USB_AUDIO,
b870472d
PA
654 s, output_callback, &s->out.as);
655 AUD_set_volume_out(s->out.voice, s->out.mute, s->out.vol[0], s->out.vol[1]);
656 AUD_set_active_out(s->out.voice, 0);
b870472d
PA
657}
658
659static const VMStateDescription vmstate_usb_audio = {
0389a0b1 660 .name = TYPE_USB_AUDIO,
b870472d
PA
661 .unmigratable = 1,
662};
663
39bffca2
AL
664static Property usb_audio_properties[] = {
665 DEFINE_PROP_UINT32("debug", USBAudioState, debug, 0),
666 DEFINE_PROP_UINT32("buffer", USBAudioState, buffer,
37bc43f7 667 32 * USBAUDIO_PACKET_SIZE),
39bffca2
AL
668 DEFINE_PROP_END_OF_LIST(),
669};
670
62aed765
AL
671static void usb_audio_class_init(ObjectClass *klass, void *data)
672{
39bffca2 673 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
674 USBDeviceClass *k = USB_DEVICE_CLASS(klass);
675
39bffca2
AL
676 dc->vmsd = &vmstate_usb_audio;
677 dc->props = usb_audio_properties;
125ee0ed 678 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
62aed765
AL
679 k->product_desc = "QEMU USB Audio Interface";
680 k->usb_desc = &desc_audio;
5450eeaa 681 k->realize = usb_audio_realize;
62aed765
AL
682 k->handle_reset = usb_audio_handle_reset;
683 k->handle_control = usb_audio_handle_control;
684 k->handle_data = usb_audio_handle_data;
685 k->handle_destroy = usb_audio_handle_destroy;
686 k->set_interface = usb_audio_set_interface;
687}
688
8c43a6f0 689static const TypeInfo usb_audio_info = {
0389a0b1 690 .name = TYPE_USB_AUDIO,
39bffca2
AL
691 .parent = TYPE_USB_DEVICE,
692 .instance_size = sizeof(USBAudioState),
693 .class_init = usb_audio_class_init,
b870472d
PA
694};
695
83f7d43a 696static void usb_audio_register_types(void)
b870472d 697{
39bffca2 698 type_register_static(&usb_audio_info);
0389a0b1 699 usb_legacy_register(TYPE_USB_AUDIO, "audio", NULL);
b870472d
PA
700}
701
83f7d43a 702type_init(usb_audio_register_types)