dev-audio.c 23 KB

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