2
0

dev-audio.c 38 KB

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