dev-audio.c 38 KB

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