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