2
0

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. #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. .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 Output 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. .iInterface = STRING_USBAUDIO_CONTROL,
  268. .ndesc = 4,
  269. .descs = (USBDescOther[]) {
  270. {
  271. /* Headphone Class-Specific AC Interface Header Descriptor */
  272. .data = (uint8_t[]) {
  273. 0x09, /* u8 bLength */
  274. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  275. DST_AC_HEADER, /* u8 bDescriptorSubtype */
  276. U16(0x0100), /* u16 bcdADC */
  277. U16(0x37), /* u16 wTotalLength */
  278. 0x01, /* u8 bInCollection */
  279. 0x01, /* u8 baInterfaceNr */
  280. }
  281. },{
  282. /* Generic Stereo Input Terminal ID1 Descriptor */
  283. .data = (uint8_t[]) {
  284. 0x0c, /* u8 bLength */
  285. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  286. DST_AC_INPUT_TERMINAL, /* u8 bDescriptorSubtype */
  287. 0x01, /* u8 bTerminalID */
  288. U16(0x0101), /* u16 wTerminalType */
  289. 0x00, /* u8 bAssocTerminal */
  290. 0x08, /* u8 bNrChannels */
  291. U16(0x063f), /* u16 wChannelConfig */
  292. 0x00, /* u8 iChannelNames */
  293. STRING_INPUT_TERMINAL, /* u8 iTerminal */
  294. }
  295. },{
  296. /* Generic Stereo Feature Unit ID2 Descriptor */
  297. .data = (uint8_t[]) {
  298. 0x19, /* u8 bLength */
  299. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  300. DST_AC_FEATURE_UNIT, /* u8 bDescriptorSubtype */
  301. 0x02, /* u8 bUnitID */
  302. 0x01, /* u8 bSourceID */
  303. 0x02, /* u8 bControlSize */
  304. U16(0x0001), /* u16 bmaControls(0) */
  305. U16(0x0002), /* u16 bmaControls(1) */
  306. U16(0x0002), /* u16 bmaControls(2) */
  307. U16(0x0002), /* u16 bmaControls(3) */
  308. U16(0x0002), /* u16 bmaControls(4) */
  309. U16(0x0002), /* u16 bmaControls(5) */
  310. U16(0x0002), /* u16 bmaControls(6) */
  311. U16(0x0002), /* u16 bmaControls(7) */
  312. U16(0x0002), /* u16 bmaControls(8) */
  313. STRING_FEATURE_UNIT, /* u8 iFeature */
  314. }
  315. },{
  316. /* Headphone Output Terminal ID3 Descriptor */
  317. .data = (uint8_t[]) {
  318. 0x09, /* u8 bLength */
  319. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  320. DST_AC_OUTPUT_TERMINAL, /* u8 bDescriptorSubtype */
  321. 0x03, /* u8 bUnitID */
  322. U16(0x0301), /* u16 wTerminalType (SPK) */
  323. 0x00, /* u8 bAssocTerminal */
  324. 0x02, /* u8 bSourceID */
  325. STRING_OUTPUT_TERMINAL, /* u8 iTerminal */
  326. }
  327. }
  328. },
  329. },{
  330. .bInterfaceNumber = 1,
  331. .bAlternateSetting = ALTSET_OFF,
  332. .bNumEndpoints = 0,
  333. .bInterfaceClass = USB_CLASS_AUDIO,
  334. .bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
  335. .iInterface = STRING_NULL_STREAM,
  336. },{
  337. .bInterfaceNumber = 1,
  338. .bAlternateSetting = ALTSET_STEREO,
  339. .bNumEndpoints = 1,
  340. .bInterfaceClass = USB_CLASS_AUDIO,
  341. .bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
  342. .iInterface = STRING_REAL_STREAM,
  343. .ndesc = 2,
  344. .descs = (USBDescOther[]) {
  345. {
  346. /* Headphone Class-specific AS General Interface Descriptor */
  347. .data = (uint8_t[]) {
  348. 0x07, /* u8 bLength */
  349. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  350. DST_AS_GENERAL, /* u8 bDescriptorSubtype */
  351. 0x01, /* u8 bTerminalLink */
  352. 0x00, /* u8 bDelay */
  353. 0x01, 0x00, /* u16 wFormatTag */
  354. }
  355. },{
  356. /* Headphone Type I Format Type Descriptor */
  357. .data = (uint8_t[]) {
  358. 0x0b, /* u8 bLength */
  359. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  360. DST_AS_FORMAT_TYPE, /* u8 bDescriptorSubtype */
  361. 0x01, /* u8 bFormatType */
  362. 0x02, /* u8 bNrChannels */
  363. 0x02, /* u8 bSubFrameSize */
  364. 0x10, /* u8 bBitResolution */
  365. 0x01, /* u8 bSamFreqType */
  366. U24(USBAUDIO_SAMPLE_RATE), /* u24 tSamFreq */
  367. }
  368. }
  369. },
  370. .eps = (USBDescEndpoint[]) {
  371. {
  372. .bEndpointAddress = USB_DIR_OUT | 0x01,
  373. .bmAttributes = 0x0d,
  374. .wMaxPacketSize = USBAUDIO_PACKET_SIZE(2),
  375. .bInterval = 1,
  376. .is_audio = 1,
  377. /* Stereo Headphone Class-specific
  378. AS Audio Data Endpoint Descriptor */
  379. .extra = (uint8_t[]) {
  380. 0x07, /* u8 bLength */
  381. USB_DT_CS_ENDPOINT, /* u8 bDescriptorType */
  382. DST_EP_GENERAL, /* u8 bDescriptorSubtype */
  383. 0x00, /* u8 bmAttributes */
  384. 0x00, /* u8 bLockDelayUnits */
  385. U16(0x0000), /* u16 wLockDelay */
  386. },
  387. },
  388. }
  389. },{
  390. .bInterfaceNumber = 1,
  391. .bAlternateSetting = ALTSET_51,
  392. .bNumEndpoints = 1,
  393. .bInterfaceClass = USB_CLASS_AUDIO,
  394. .bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
  395. .iInterface = STRING_REAL_STREAM,
  396. .ndesc = 2,
  397. .descs = (USBDescOther[]) {
  398. {
  399. /* Headphone Class-specific AS General Interface Descriptor */
  400. .data = (uint8_t[]) {
  401. 0x07, /* u8 bLength */
  402. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  403. DST_AS_GENERAL, /* u8 bDescriptorSubtype */
  404. 0x01, /* u8 bTerminalLink */
  405. 0x00, /* u8 bDelay */
  406. 0x01, 0x00, /* u16 wFormatTag */
  407. }
  408. },{
  409. /* Headphone Type I Format Type Descriptor */
  410. .data = (uint8_t[]) {
  411. 0x0b, /* u8 bLength */
  412. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  413. DST_AS_FORMAT_TYPE, /* u8 bDescriptorSubtype */
  414. 0x01, /* u8 bFormatType */
  415. 0x06, /* u8 bNrChannels */
  416. 0x02, /* u8 bSubFrameSize */
  417. 0x10, /* u8 bBitResolution */
  418. 0x01, /* u8 bSamFreqType */
  419. U24(USBAUDIO_SAMPLE_RATE), /* u24 tSamFreq */
  420. }
  421. }
  422. },
  423. .eps = (USBDescEndpoint[]) {
  424. {
  425. .bEndpointAddress = USB_DIR_OUT | 0x01,
  426. .bmAttributes = 0x0d,
  427. .wMaxPacketSize = USBAUDIO_PACKET_SIZE(6),
  428. .bInterval = 1,
  429. .is_audio = 1,
  430. /* Stereo Headphone Class-specific
  431. AS Audio Data Endpoint Descriptor */
  432. .extra = (uint8_t[]) {
  433. 0x07, /* u8 bLength */
  434. USB_DT_CS_ENDPOINT, /* u8 bDescriptorType */
  435. DST_EP_GENERAL, /* u8 bDescriptorSubtype */
  436. 0x00, /* u8 bmAttributes */
  437. 0x00, /* u8 bLockDelayUnits */
  438. U16(0x0000), /* u16 wLockDelay */
  439. },
  440. },
  441. }
  442. },{
  443. .bInterfaceNumber = 1,
  444. .bAlternateSetting = ALTSET_71,
  445. .bNumEndpoints = 1,
  446. .bInterfaceClass = USB_CLASS_AUDIO,
  447. .bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING,
  448. .iInterface = STRING_REAL_STREAM,
  449. .ndesc = 2,
  450. .descs = (USBDescOther[]) {
  451. {
  452. /* Headphone Class-specific AS General Interface Descriptor */
  453. .data = (uint8_t[]) {
  454. 0x07, /* u8 bLength */
  455. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  456. DST_AS_GENERAL, /* u8 bDescriptorSubtype */
  457. 0x01, /* u8 bTerminalLink */
  458. 0x00, /* u8 bDelay */
  459. 0x01, 0x00, /* u16 wFormatTag */
  460. }
  461. },{
  462. /* Headphone Type I Format Type Descriptor */
  463. .data = (uint8_t[]) {
  464. 0x0b, /* u8 bLength */
  465. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  466. DST_AS_FORMAT_TYPE, /* u8 bDescriptorSubtype */
  467. 0x01, /* u8 bFormatType */
  468. 0x08, /* u8 bNrChannels */
  469. 0x02, /* u8 bSubFrameSize */
  470. 0x10, /* u8 bBitResolution */
  471. 0x01, /* u8 bSamFreqType */
  472. U24(USBAUDIO_SAMPLE_RATE), /* u24 tSamFreq */
  473. }
  474. }
  475. },
  476. .eps = (USBDescEndpoint[]) {
  477. {
  478. .bEndpointAddress = USB_DIR_OUT | 0x01,
  479. .bmAttributes = 0x0d,
  480. .wMaxPacketSize = USBAUDIO_PACKET_SIZE(8),
  481. .bInterval = 1,
  482. .is_audio = 1,
  483. /* Stereo Headphone Class-specific
  484. AS Audio Data Endpoint Descriptor */
  485. .extra = (uint8_t[]) {
  486. 0x07, /* u8 bLength */
  487. USB_DT_CS_ENDPOINT, /* u8 bDescriptorType */
  488. DST_EP_GENERAL, /* u8 bDescriptorSubtype */
  489. 0x00, /* u8 bmAttributes */
  490. 0x00, /* u8 bLockDelayUnits */
  491. U16(0x0000), /* u16 wLockDelay */
  492. },
  493. },
  494. }
  495. }
  496. };
  497. static const USBDescDevice desc_device_multi = {
  498. .bcdUSB = 0x0100,
  499. .bMaxPacketSize0 = 64,
  500. .bNumConfigurations = 1,
  501. .confs = (USBDescConfig[]) {
  502. {
  503. .bNumInterfaces = 2,
  504. .bConfigurationValue = DEV_CONFIG_VALUE,
  505. .iConfiguration = STRING_CONFIG,
  506. .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
  507. .bMaxPower = 0x32,
  508. .nif = ARRAY_SIZE(desc_iface_multi),
  509. .ifs = desc_iface_multi,
  510. }
  511. },
  512. };
  513. static const USBDesc desc_audio_multi = {
  514. .id = {
  515. .idVendor = USBAUDIO_VENDOR_NUM,
  516. .idProduct = USBAUDIO_PRODUCT_NUM,
  517. .bcdDevice = 0,
  518. .iManufacturer = STRING_MANUFACTURER,
  519. .iProduct = STRING_PRODUCT,
  520. .iSerialNumber = STRING_SERIALNUMBER,
  521. },
  522. .full = &desc_device_multi,
  523. .str = usb_audio_stringtable,
  524. };
  525. /*
  526. * Class-specific control requests
  527. */
  528. #define CR_SET_CUR 0x01
  529. #define CR_GET_CUR 0x81
  530. #define CR_SET_MIN 0x02
  531. #define CR_GET_MIN 0x82
  532. #define CR_SET_MAX 0x03
  533. #define CR_GET_MAX 0x83
  534. #define CR_SET_RES 0x04
  535. #define CR_GET_RES 0x84
  536. #define CR_SET_MEM 0x05
  537. #define CR_GET_MEM 0x85
  538. #define CR_GET_STAT 0xff
  539. /*
  540. * Feature Unit Control Selectors
  541. */
  542. #define MUTE_CONTROL 0x01
  543. #define VOLUME_CONTROL 0x02
  544. #define BASS_CONTROL 0x03
  545. #define MID_CONTROL 0x04
  546. #define TREBLE_CONTROL 0x05
  547. #define GRAPHIC_EQUALIZER_CONTROL 0x06
  548. #define AUTOMATIC_GAIN_CONTROL 0x07
  549. #define DELAY_CONTROL 0x08
  550. #define BASS_BOOST_CONTROL 0x09
  551. #define LOUDNESS_CONTROL 0x0a
  552. /*
  553. * buffering
  554. */
  555. struct streambuf {
  556. uint8_t *data;
  557. size_t size;
  558. uint64_t prod;
  559. uint64_t cons;
  560. };
  561. static void streambuf_init(struct streambuf *buf, uint32_t size,
  562. uint32_t channels)
  563. {
  564. g_free(buf->data);
  565. buf->size = size - (size % USBAUDIO_PACKET_SIZE(channels));
  566. buf->data = g_malloc(buf->size);
  567. buf->prod = 0;
  568. buf->cons = 0;
  569. }
  570. static void streambuf_fini(struct streambuf *buf)
  571. {
  572. g_free(buf->data);
  573. buf->data = NULL;
  574. }
  575. static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)
  576. {
  577. int64_t free = buf->size - (buf->prod - buf->cons);
  578. if (free < USBAUDIO_PACKET_SIZE(channels)) {
  579. return 0;
  580. }
  581. if (p->iov.size != USBAUDIO_PACKET_SIZE(channels)) {
  582. return 0;
  583. }
  584. /* can happen if prod overflows */
  585. assert(buf->prod % USBAUDIO_PACKET_SIZE(channels) == 0);
  586. usb_packet_copy(p, buf->data + (buf->prod % buf->size),
  587. USBAUDIO_PACKET_SIZE(channels));
  588. buf->prod += USBAUDIO_PACKET_SIZE(channels);
  589. return USBAUDIO_PACKET_SIZE(channels);
  590. }
  591. static uint8_t *streambuf_get(struct streambuf *buf, size_t *len)
  592. {
  593. int64_t used = buf->prod - buf->cons;
  594. uint8_t *data;
  595. if (used <= 0) {
  596. *len = 0;
  597. return NULL;
  598. }
  599. data = buf->data + (buf->cons % buf->size);
  600. *len = MIN(buf->prod - buf->cons,
  601. buf->size - (buf->cons % buf->size));
  602. return data;
  603. }
  604. struct USBAudioState {
  605. /* qemu interfaces */
  606. USBDevice dev;
  607. QEMUSoundCard card;
  608. /* state */
  609. struct {
  610. enum usb_audio_altset altset;
  611. struct audsettings as;
  612. SWVoiceOut *voice;
  613. Volume vol;
  614. struct streambuf buf;
  615. uint32_t channels;
  616. } out;
  617. /* properties */
  618. uint32_t debug;
  619. uint32_t buffer_user, buffer;
  620. bool multi;
  621. };
  622. #define TYPE_USB_AUDIO "usb-audio"
  623. OBJECT_DECLARE_SIMPLE_TYPE(USBAudioState, USB_AUDIO)
  624. static void output_callback(void *opaque, int avail)
  625. {
  626. USBAudioState *s = opaque;
  627. uint8_t *data;
  628. while (avail) {
  629. size_t written, len;
  630. data = streambuf_get(&s->out.buf, &len);
  631. if (!data) {
  632. return;
  633. }
  634. written = AUD_write(s->out.voice, data, len);
  635. avail -= written;
  636. s->out.buf.cons += written;
  637. if (written < len) {
  638. return;
  639. }
  640. }
  641. }
  642. static int usb_audio_set_output_altset(USBAudioState *s, int altset)
  643. {
  644. switch (altset) {
  645. case ALTSET_OFF:
  646. AUD_set_active_out(s->out.voice, false);
  647. break;
  648. case ALTSET_STEREO:
  649. case ALTSET_51:
  650. case ALTSET_71:
  651. if (s->out.channels != altset_channels[altset]) {
  652. usb_audio_reinit(USB_DEVICE(s), altset_channels[altset]);
  653. }
  654. streambuf_init(&s->out.buf, s->buffer, s->out.channels);
  655. AUD_set_active_out(s->out.voice, true);
  656. break;
  657. default:
  658. return -1;
  659. }
  660. if (s->debug) {
  661. fprintf(stderr, "usb-audio: set interface %d\n", altset);
  662. }
  663. s->out.altset = altset;
  664. return 0;
  665. }
  666. /*
  667. * Note: we arbitrarily map the volume control range onto -inf..+8 dB
  668. */
  669. #define ATTRIB_ID(cs, attrib, idif) \
  670. (((cs) << 24) | ((attrib) << 16) | (idif))
  671. static int usb_audio_get_control(USBAudioState *s, uint8_t attrib,
  672. uint16_t cscn, uint16_t idif,
  673. int length, uint8_t *data)
  674. {
  675. uint8_t cs = cscn >> 8;
  676. uint8_t cn = cscn - 1; /* -1 for the non-present master control */
  677. uint32_t aid = ATTRIB_ID(cs, attrib, idif);
  678. int ret = USB_RET_STALL;
  679. switch (aid) {
  680. case ATTRIB_ID(MUTE_CONTROL, CR_GET_CUR, 0x0200):
  681. data[0] = s->out.vol.mute;
  682. ret = 1;
  683. break;
  684. case ATTRIB_ID(VOLUME_CONTROL, CR_GET_CUR, 0x0200):
  685. if (cn < USBAUDIO_MAX_CHANNELS(s)) {
  686. uint16_t vol = (s->out.vol.vol[cn] * 0x8800 + 127) / 255 + 0x8000;
  687. data[0] = vol;
  688. data[1] = vol >> 8;
  689. ret = 2;
  690. }
  691. break;
  692. case ATTRIB_ID(VOLUME_CONTROL, CR_GET_MIN, 0x0200):
  693. if (cn < USBAUDIO_MAX_CHANNELS(s)) {
  694. data[0] = 0x01;
  695. data[1] = 0x80;
  696. ret = 2;
  697. }
  698. break;
  699. case ATTRIB_ID(VOLUME_CONTROL, CR_GET_MAX, 0x0200):
  700. if (cn < USBAUDIO_MAX_CHANNELS(s)) {
  701. data[0] = 0x00;
  702. data[1] = 0x08;
  703. ret = 2;
  704. }
  705. break;
  706. case ATTRIB_ID(VOLUME_CONTROL, CR_GET_RES, 0x0200):
  707. if (cn < USBAUDIO_MAX_CHANNELS(s)) {
  708. data[0] = 0x88;
  709. data[1] = 0x00;
  710. ret = 2;
  711. }
  712. break;
  713. }
  714. return ret;
  715. }
  716. static int usb_audio_set_control(USBAudioState *s, uint8_t attrib,
  717. uint16_t cscn, uint16_t idif,
  718. int length, uint8_t *data)
  719. {
  720. uint8_t cs = cscn >> 8;
  721. uint8_t cn = cscn - 1; /* -1 for the non-present master control */
  722. uint32_t aid = ATTRIB_ID(cs, attrib, idif);
  723. int ret = USB_RET_STALL;
  724. bool set_vol = false;
  725. switch (aid) {
  726. case ATTRIB_ID(MUTE_CONTROL, CR_SET_CUR, 0x0200):
  727. s->out.vol.mute = data[0] & 1;
  728. set_vol = true;
  729. ret = 0;
  730. break;
  731. case ATTRIB_ID(VOLUME_CONTROL, CR_SET_CUR, 0x0200):
  732. if (cn < USBAUDIO_MAX_CHANNELS(s)) {
  733. uint16_t vol = data[0] + (data[1] << 8);
  734. if (s->debug) {
  735. fprintf(stderr, "usb-audio: cn %d vol %04x\n", cn,
  736. (uint16_t)vol);
  737. }
  738. vol -= 0x8000;
  739. vol = (vol * 255 + 0x4400) / 0x8800;
  740. if (vol > 255) {
  741. vol = 255;
  742. }
  743. s->out.vol.vol[cn] = vol;
  744. set_vol = true;
  745. ret = 0;
  746. }
  747. break;
  748. }
  749. if (set_vol) {
  750. if (s->debug) {
  751. int i;
  752. fprintf(stderr, "usb-audio: mute %d", s->out.vol.mute);
  753. for (i = 0; i < USBAUDIO_MAX_CHANNELS(s); ++i) {
  754. fprintf(stderr, ", vol[%d] %3d", i, s->out.vol.vol[i]);
  755. }
  756. fprintf(stderr, "\n");
  757. }
  758. audio_set_volume_out(s->out.voice, &s->out.vol);
  759. }
  760. return ret;
  761. }
  762. static void usb_audio_handle_control(USBDevice *dev, USBPacket *p,
  763. int request, int value, int index,
  764. int length, uint8_t *data)
  765. {
  766. USBAudioState *s = USB_AUDIO(dev);
  767. int ret = 0;
  768. if (s->debug) {
  769. fprintf(stderr, "usb-audio: control transaction: "
  770. "request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
  771. request, value, index, length);
  772. }
  773. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  774. if (ret >= 0) {
  775. return;
  776. }
  777. switch (request) {
  778. case ClassInterfaceRequest | CR_GET_CUR:
  779. case ClassInterfaceRequest | CR_GET_MIN:
  780. case ClassInterfaceRequest | CR_GET_MAX:
  781. case ClassInterfaceRequest | CR_GET_RES:
  782. ret = usb_audio_get_control(s, request & 0xff, value, index,
  783. length, data);
  784. if (ret < 0) {
  785. if (s->debug) {
  786. fprintf(stderr, "usb-audio: fail: get control\n");
  787. }
  788. goto fail;
  789. }
  790. p->actual_length = ret;
  791. break;
  792. case ClassInterfaceOutRequest | CR_SET_CUR:
  793. case ClassInterfaceOutRequest | CR_SET_MIN:
  794. case ClassInterfaceOutRequest | CR_SET_MAX:
  795. case ClassInterfaceOutRequest | CR_SET_RES:
  796. ret = usb_audio_set_control(s, request & 0xff, value, index,
  797. length, data);
  798. if (ret < 0) {
  799. if (s->debug) {
  800. fprintf(stderr, "usb-audio: fail: set control\n");
  801. }
  802. goto fail;
  803. }
  804. break;
  805. default:
  806. fail:
  807. if (s->debug) {
  808. fprintf(stderr, "usb-audio: failed control transaction: "
  809. "request 0x%04x value 0x%04x index 0x%04x length 0x%04x\n",
  810. request, value, index, length);
  811. }
  812. p->status = USB_RET_STALL;
  813. break;
  814. }
  815. }
  816. static void usb_audio_set_interface(USBDevice *dev, int iface,
  817. int old, int value)
  818. {
  819. USBAudioState *s = USB_AUDIO(dev);
  820. if (iface == 1) {
  821. usb_audio_set_output_altset(s, value);
  822. }
  823. }
  824. static void usb_audio_handle_reset(USBDevice *dev)
  825. {
  826. USBAudioState *s = USB_AUDIO(dev);
  827. if (s->debug) {
  828. fprintf(stderr, "usb-audio: reset\n");
  829. }
  830. usb_audio_set_output_altset(s, ALTSET_OFF);
  831. }
  832. static void usb_audio_handle_dataout(USBAudioState *s, USBPacket *p)
  833. {
  834. if (s->out.altset == ALTSET_OFF) {
  835. p->status = USB_RET_STALL;
  836. return;
  837. }
  838. streambuf_put(&s->out.buf, p, s->out.channels);
  839. if (p->actual_length < p->iov.size && s->debug > 1) {
  840. fprintf(stderr, "usb-audio: output overrun (%zd bytes)\n",
  841. p->iov.size - p->actual_length);
  842. }
  843. }
  844. static void usb_audio_handle_data(USBDevice *dev, USBPacket *p)
  845. {
  846. USBAudioState *s = (USBAudioState *) dev;
  847. if (p->pid == USB_TOKEN_OUT && p->ep->nr == 1) {
  848. usb_audio_handle_dataout(s, p);
  849. return;
  850. }
  851. p->status = USB_RET_STALL;
  852. if (s->debug) {
  853. fprintf(stderr, "usb-audio: failed data transaction: "
  854. "pid 0x%x ep 0x%x len 0x%zx\n",
  855. p->pid, p->ep->nr, p->iov.size);
  856. }
  857. }
  858. static void usb_audio_unrealize(USBDevice *dev)
  859. {
  860. USBAudioState *s = USB_AUDIO(dev);
  861. if (s->debug) {
  862. fprintf(stderr, "usb-audio: destroy\n");
  863. }
  864. usb_audio_set_output_altset(s, ALTSET_OFF);
  865. AUD_close_out(&s->card, s->out.voice);
  866. AUD_remove_card(&s->card);
  867. streambuf_fini(&s->out.buf);
  868. }
  869. static void usb_audio_realize(USBDevice *dev, Error **errp)
  870. {
  871. USBAudioState *s = USB_AUDIO(dev);
  872. int i;
  873. if (!AUD_register_card(TYPE_USB_AUDIO, &s->card, errp)) {
  874. return;
  875. }
  876. dev->usb_desc = s->multi ? &desc_audio_multi : &desc_audio;
  877. usb_desc_create_serial(dev);
  878. usb_desc_init(dev);
  879. s->dev.opaque = s;
  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 const 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. };
  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. }
  942. type_init(usb_audio_register_types)