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