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