virtio-snd.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /*
  2. * VIRTIO Sound Device conforming to
  3. *
  4. * "Virtual I/O Device (VIRTIO) Version 1.2
  5. * Committee Specification Draft 01
  6. * 09 May 2022"
  7. *
  8. * <https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-52900014>
  9. *
  10. * Copyright (c) 2023 Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org>
  11. * Copyright (C) 2019 OpenSynergy GmbH
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2 or
  14. * (at your option) any later version. See the COPYING file in the
  15. * top-level directory.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "qemu/iov.h"
  19. #include "qemu/log.h"
  20. #include "qemu/error-report.h"
  21. #include "qemu/lockable.h"
  22. #include "sysemu/runstate.h"
  23. #include "trace.h"
  24. #include "qapi/error.h"
  25. #include "hw/audio/virtio-snd.h"
  26. #define VIRTIO_SOUND_VM_VERSION 1
  27. #define VIRTIO_SOUND_JACK_DEFAULT 0
  28. #define VIRTIO_SOUND_STREAM_DEFAULT 2
  29. #define VIRTIO_SOUND_CHMAP_DEFAULT 0
  30. #define VIRTIO_SOUND_HDA_FN_NID 0
  31. static void virtio_snd_pcm_out_cb(void *data, int available);
  32. static void virtio_snd_process_cmdq(VirtIOSound *s);
  33. static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream);
  34. static void virtio_snd_pcm_in_cb(void *data, int available);
  35. static void virtio_snd_unrealize(DeviceState *dev);
  36. static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
  37. | BIT(VIRTIO_SND_PCM_FMT_U8)
  38. | BIT(VIRTIO_SND_PCM_FMT_S16)
  39. | BIT(VIRTIO_SND_PCM_FMT_U16)
  40. | BIT(VIRTIO_SND_PCM_FMT_S32)
  41. | BIT(VIRTIO_SND_PCM_FMT_U32)
  42. | BIT(VIRTIO_SND_PCM_FMT_FLOAT);
  43. static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512)
  44. | BIT(VIRTIO_SND_PCM_RATE_8000)
  45. | BIT(VIRTIO_SND_PCM_RATE_11025)
  46. | BIT(VIRTIO_SND_PCM_RATE_16000)
  47. | BIT(VIRTIO_SND_PCM_RATE_22050)
  48. | BIT(VIRTIO_SND_PCM_RATE_32000)
  49. | BIT(VIRTIO_SND_PCM_RATE_44100)
  50. | BIT(VIRTIO_SND_PCM_RATE_48000)
  51. | BIT(VIRTIO_SND_PCM_RATE_64000)
  52. | BIT(VIRTIO_SND_PCM_RATE_88200)
  53. | BIT(VIRTIO_SND_PCM_RATE_96000)
  54. | BIT(VIRTIO_SND_PCM_RATE_176400)
  55. | BIT(VIRTIO_SND_PCM_RATE_192000)
  56. | BIT(VIRTIO_SND_PCM_RATE_384000);
  57. static const VMStateDescription vmstate_virtio_snd_device = {
  58. .name = TYPE_VIRTIO_SND,
  59. .version_id = VIRTIO_SOUND_VM_VERSION,
  60. .minimum_version_id = VIRTIO_SOUND_VM_VERSION,
  61. };
  62. static const VMStateDescription vmstate_virtio_snd = {
  63. .name = TYPE_VIRTIO_SND,
  64. .unmigratable = 1,
  65. .minimum_version_id = VIRTIO_SOUND_VM_VERSION,
  66. .version_id = VIRTIO_SOUND_VM_VERSION,
  67. .fields = (const VMStateField[]) {
  68. VMSTATE_VIRTIO_DEVICE,
  69. VMSTATE_END_OF_LIST()
  70. },
  71. };
  72. static Property virtio_snd_properties[] = {
  73. DEFINE_AUDIO_PROPERTIES(VirtIOSound, card),
  74. DEFINE_PROP_UINT32("jacks", VirtIOSound, snd_conf.jacks,
  75. VIRTIO_SOUND_JACK_DEFAULT),
  76. DEFINE_PROP_UINT32("streams", VirtIOSound, snd_conf.streams,
  77. VIRTIO_SOUND_STREAM_DEFAULT),
  78. DEFINE_PROP_UINT32("chmaps", VirtIOSound, snd_conf.chmaps,
  79. VIRTIO_SOUND_CHMAP_DEFAULT),
  80. DEFINE_PROP_END_OF_LIST(),
  81. };
  82. static void
  83. virtio_snd_get_config(VirtIODevice *vdev, uint8_t *config)
  84. {
  85. VirtIOSound *s = VIRTIO_SND(vdev);
  86. virtio_snd_config *sndconfig =
  87. (virtio_snd_config *)config;
  88. trace_virtio_snd_get_config(vdev,
  89. s->snd_conf.jacks,
  90. s->snd_conf.streams,
  91. s->snd_conf.chmaps);
  92. memcpy(sndconfig, &s->snd_conf, sizeof(s->snd_conf));
  93. cpu_to_le32s(&sndconfig->jacks);
  94. cpu_to_le32s(&sndconfig->streams);
  95. cpu_to_le32s(&sndconfig->chmaps);
  96. }
  97. static void
  98. virtio_snd_pcm_buffer_free(VirtIOSoundPCMBuffer *buffer)
  99. {
  100. g_free(buffer->elem);
  101. g_free(buffer);
  102. }
  103. static void
  104. virtio_snd_ctrl_cmd_free(virtio_snd_ctrl_command *cmd)
  105. {
  106. g_free(cmd->elem);
  107. g_free(cmd);
  108. }
  109. /*
  110. * Get a specific stream from the virtio sound card device.
  111. * Returns NULL if @stream_id is invalid or not allocated.
  112. *
  113. * @s: VirtIOSound device
  114. * @stream_id: stream id
  115. */
  116. static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s,
  117. uint32_t stream_id)
  118. {
  119. return stream_id >= s->snd_conf.streams ? NULL :
  120. s->pcm->streams[stream_id];
  121. }
  122. /*
  123. * Get params for a specific stream.
  124. *
  125. * @s: VirtIOSound device
  126. * @stream_id: stream id
  127. */
  128. static virtio_snd_pcm_set_params *virtio_snd_pcm_get_params(VirtIOSound *s,
  129. uint32_t stream_id)
  130. {
  131. return stream_id >= s->snd_conf.streams ? NULL
  132. : &s->pcm->pcm_params[stream_id];
  133. }
  134. /*
  135. * Handle the VIRTIO_SND_R_PCM_INFO request.
  136. * The function writes the info structs to the request element.
  137. *
  138. * @s: VirtIOSound device
  139. * @cmd: The request command queue element from VirtIOSound cmdq field
  140. */
  141. static void virtio_snd_handle_pcm_info(VirtIOSound *s,
  142. virtio_snd_ctrl_command *cmd)
  143. {
  144. uint32_t stream_id, start_id, count, size;
  145. virtio_snd_pcm_info val;
  146. virtio_snd_query_info req;
  147. VirtIOSoundPCMStream *stream = NULL;
  148. g_autofree virtio_snd_pcm_info *pcm_info = NULL;
  149. size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
  150. cmd->elem->out_num,
  151. 0,
  152. &req,
  153. sizeof(virtio_snd_query_info));
  154. if (msg_sz != sizeof(virtio_snd_query_info)) {
  155. /*
  156. * TODO: do we need to set DEVICE_NEEDS_RESET?
  157. */
  158. qemu_log_mask(LOG_GUEST_ERROR,
  159. "%s: virtio-snd command size incorrect %zu vs \
  160. %zu\n", __func__, msg_sz, sizeof(virtio_snd_query_info));
  161. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  162. return;
  163. }
  164. start_id = le32_to_cpu(req.start_id);
  165. count = le32_to_cpu(req.count);
  166. size = le32_to_cpu(req.size);
  167. if (iov_size(cmd->elem->in_sg, cmd->elem->in_num) <
  168. sizeof(virtio_snd_hdr) + size * count) {
  169. /*
  170. * TODO: do we need to set DEVICE_NEEDS_RESET?
  171. */
  172. error_report("pcm info: buffer too small, got: %zu, needed: %zu",
  173. iov_size(cmd->elem->in_sg, cmd->elem->in_num),
  174. sizeof(virtio_snd_pcm_info));
  175. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  176. return;
  177. }
  178. pcm_info = g_new0(virtio_snd_pcm_info, count);
  179. for (uint32_t i = 0; i < count; i++) {
  180. stream_id = i + start_id;
  181. trace_virtio_snd_handle_pcm_info(stream_id);
  182. stream = virtio_snd_pcm_get_stream(s, stream_id);
  183. if (!stream) {
  184. error_report("Invalid stream id: %"PRIu32, stream_id);
  185. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  186. return;
  187. }
  188. val = stream->info;
  189. val.hdr.hda_fn_nid = cpu_to_le32(val.hdr.hda_fn_nid);
  190. val.features = cpu_to_le32(val.features);
  191. val.formats = cpu_to_le64(val.formats);
  192. val.rates = cpu_to_le64(val.rates);
  193. /*
  194. * 5.14.6.6.2.1 Device Requirements: Stream Information The device MUST
  195. * NOT set undefined feature, format, rate and direction values. The
  196. * device MUST initialize the padding bytes to 0.
  197. */
  198. pcm_info[i] = val;
  199. memset(&pcm_info[i].padding, 0, 5);
  200. }
  201. cmd->payload_size = sizeof(virtio_snd_pcm_info) * count;
  202. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
  203. iov_from_buf(cmd->elem->in_sg,
  204. cmd->elem->in_num,
  205. sizeof(virtio_snd_hdr),
  206. pcm_info,
  207. cmd->payload_size);
  208. }
  209. /*
  210. * Set the given stream params.
  211. * Called by both virtio_snd_handle_pcm_set_params and during device
  212. * initialization.
  213. * Returns the response status code. (VIRTIO_SND_S_*).
  214. *
  215. * @s: VirtIOSound device
  216. * @params: The PCM params as defined in the virtio specification
  217. */
  218. static
  219. uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,
  220. uint32_t stream_id,
  221. virtio_snd_pcm_set_params *params)
  222. {
  223. virtio_snd_pcm_set_params *st_params;
  224. if (stream_id >= s->snd_conf.streams || s->pcm->pcm_params == NULL) {
  225. /*
  226. * TODO: do we need to set DEVICE_NEEDS_RESET?
  227. */
  228. virtio_error(VIRTIO_DEVICE(s), "Streams have not been initialized.\n");
  229. return cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  230. }
  231. st_params = virtio_snd_pcm_get_params(s, stream_id);
  232. if (params->channels < 1 || params->channels > AUDIO_MAX_CHANNELS) {
  233. error_report("Number of channels is not supported.");
  234. return cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
  235. }
  236. if (params->format >= sizeof(supported_formats) * BITS_PER_BYTE ||
  237. !(supported_formats & BIT(params->format))) {
  238. error_report("Stream format is not supported.");
  239. return cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
  240. }
  241. if (params->rate >= sizeof(supported_rates) * BITS_PER_BYTE ||
  242. !(supported_rates & BIT(params->rate))) {
  243. error_report("Stream rate is not supported.");
  244. return cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
  245. }
  246. st_params->buffer_bytes = le32_to_cpu(params->buffer_bytes);
  247. st_params->period_bytes = le32_to_cpu(params->period_bytes);
  248. st_params->features = le32_to_cpu(params->features);
  249. /* the following are uint8_t, so there's no need to bswap the values. */
  250. st_params->channels = params->channels;
  251. st_params->format = params->format;
  252. st_params->rate = params->rate;
  253. return cpu_to_le32(VIRTIO_SND_S_OK);
  254. }
  255. /*
  256. * Handles the VIRTIO_SND_R_PCM_SET_PARAMS request.
  257. *
  258. * @s: VirtIOSound device
  259. * @cmd: The request command queue element from VirtIOSound cmdq field
  260. */
  261. static void virtio_snd_handle_pcm_set_params(VirtIOSound *s,
  262. virtio_snd_ctrl_command *cmd)
  263. {
  264. virtio_snd_pcm_set_params req = { 0 };
  265. uint32_t stream_id;
  266. size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
  267. cmd->elem->out_num,
  268. 0,
  269. &req,
  270. sizeof(virtio_snd_pcm_set_params));
  271. if (msg_sz != sizeof(virtio_snd_pcm_set_params)) {
  272. /*
  273. * TODO: do we need to set DEVICE_NEEDS_RESET?
  274. */
  275. qemu_log_mask(LOG_GUEST_ERROR,
  276. "%s: virtio-snd command size incorrect %zu vs \
  277. %zu\n", __func__, msg_sz, sizeof(virtio_snd_pcm_set_params));
  278. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  279. return;
  280. }
  281. stream_id = le32_to_cpu(req.hdr.stream_id);
  282. trace_virtio_snd_handle_pcm_set_params(stream_id);
  283. cmd->resp.code = virtio_snd_set_pcm_params(s, stream_id, &req);
  284. }
  285. /*
  286. * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_*
  287. */
  288. static AudioFormat virtio_snd_get_qemu_format(uint32_t format)
  289. {
  290. #define CASE(FMT) \
  291. case VIRTIO_SND_PCM_FMT_##FMT: \
  292. return AUDIO_FORMAT_##FMT;
  293. switch (format) {
  294. CASE(U8)
  295. CASE(S8)
  296. CASE(U16)
  297. CASE(S16)
  298. CASE(U32)
  299. CASE(S32)
  300. case VIRTIO_SND_PCM_FMT_FLOAT:
  301. return AUDIO_FORMAT_F32;
  302. default:
  303. g_assert_not_reached();
  304. }
  305. #undef CASE
  306. }
  307. /*
  308. * Get a QEMU Audiosystem compatible frequency value from a
  309. * VIRTIO_SND_PCM_RATE_*
  310. */
  311. static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
  312. {
  313. #define CASE(RATE) \
  314. case VIRTIO_SND_PCM_RATE_##RATE: \
  315. return RATE;
  316. switch (rate) {
  317. CASE(5512)
  318. CASE(8000)
  319. CASE(11025)
  320. CASE(16000)
  321. CASE(22050)
  322. CASE(32000)
  323. CASE(44100)
  324. CASE(48000)
  325. CASE(64000)
  326. CASE(88200)
  327. CASE(96000)
  328. CASE(176400)
  329. CASE(192000)
  330. CASE(384000)
  331. default:
  332. g_assert_not_reached();
  333. }
  334. #undef CASE
  335. }
  336. /*
  337. * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
  338. * params.
  339. */
  340. static void virtio_snd_get_qemu_audsettings(audsettings *as,
  341. virtio_snd_pcm_set_params *params)
  342. {
  343. as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
  344. as->fmt = virtio_snd_get_qemu_format(params->format);
  345. as->freq = virtio_snd_get_qemu_freq(params->rate);
  346. as->endianness = 0; /* Conforming to VIRTIO 1.0: always little endian. */
  347. }
  348. /*
  349. * Close a stream and free all its resources.
  350. *
  351. * @stream: VirtIOSoundPCMStream *stream
  352. */
  353. static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
  354. {
  355. if (stream) {
  356. virtio_snd_pcm_flush(stream);
  357. if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
  358. AUD_close_out(&stream->pcm->snd->card, stream->voice.out);
  359. stream->voice.out = NULL;
  360. } else if (stream->info.direction == VIRTIO_SND_D_INPUT) {
  361. AUD_close_in(&stream->pcm->snd->card, stream->voice.in);
  362. stream->voice.in = NULL;
  363. }
  364. }
  365. }
  366. /*
  367. * Prepares a VirtIOSound card stream.
  368. * Returns the response status code. (VIRTIO_SND_S_*).
  369. *
  370. * @s: VirtIOSound device
  371. * @stream_id: stream id
  372. */
  373. static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
  374. {
  375. audsettings as;
  376. virtio_snd_pcm_set_params *params;
  377. VirtIOSoundPCMStream *stream;
  378. if (s->pcm->streams == NULL ||
  379. s->pcm->pcm_params == NULL ||
  380. stream_id >= s->snd_conf.streams) {
  381. return cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  382. }
  383. params = virtio_snd_pcm_get_params(s, stream_id);
  384. if (params == NULL) {
  385. return cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  386. }
  387. stream = virtio_snd_pcm_get_stream(s, stream_id);
  388. if (stream == NULL) {
  389. stream = g_new0(VirtIOSoundPCMStream, 1);
  390. stream->active = false;
  391. stream->id = stream_id;
  392. stream->pcm = s->pcm;
  393. stream->s = s;
  394. qemu_mutex_init(&stream->queue_mutex);
  395. QSIMPLEQ_INIT(&stream->queue);
  396. /*
  397. * stream_id >= s->snd_conf.streams was checked before so this is
  398. * in-bounds
  399. */
  400. s->pcm->streams[stream_id] = stream;
  401. }
  402. virtio_snd_get_qemu_audsettings(&as, params);
  403. stream->info.direction = stream_id < s->snd_conf.streams / 2 +
  404. (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
  405. stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
  406. stream->info.features = 0;
  407. stream->info.channels_min = 1;
  408. stream->info.channels_max = as.nchannels;
  409. stream->info.formats = supported_formats;
  410. stream->info.rates = supported_rates;
  411. stream->params = *params;
  412. stream->positions[0] = VIRTIO_SND_CHMAP_FL;
  413. stream->positions[1] = VIRTIO_SND_CHMAP_FR;
  414. stream->as = as;
  415. if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
  416. stream->voice.out = AUD_open_out(&s->card,
  417. stream->voice.out,
  418. "virtio-sound.out",
  419. stream,
  420. virtio_snd_pcm_out_cb,
  421. &as);
  422. AUD_set_volume_out(stream->voice.out, 0, 255, 255);
  423. } else {
  424. stream->voice.in = AUD_open_in(&s->card,
  425. stream->voice.in,
  426. "virtio-sound.in",
  427. stream,
  428. virtio_snd_pcm_in_cb,
  429. &as);
  430. AUD_set_volume_in(stream->voice.in, 0, 255, 255);
  431. }
  432. return cpu_to_le32(VIRTIO_SND_S_OK);
  433. }
  434. static const char *print_code(uint32_t code)
  435. {
  436. #define CASE(CODE) \
  437. case VIRTIO_SND_R_##CODE: \
  438. return "VIRTIO_SND_R_"#CODE
  439. switch (code) {
  440. CASE(JACK_INFO);
  441. CASE(JACK_REMAP);
  442. CASE(PCM_INFO);
  443. CASE(PCM_SET_PARAMS);
  444. CASE(PCM_PREPARE);
  445. CASE(PCM_RELEASE);
  446. CASE(PCM_START);
  447. CASE(PCM_STOP);
  448. CASE(CHMAP_INFO);
  449. default:
  450. return "invalid code";
  451. }
  452. #undef CASE
  453. };
  454. /*
  455. * Handles VIRTIO_SND_R_PCM_PREPARE.
  456. *
  457. * @s: VirtIOSound device
  458. * @cmd: The request command queue element from VirtIOSound cmdq field
  459. */
  460. static void virtio_snd_handle_pcm_prepare(VirtIOSound *s,
  461. virtio_snd_ctrl_command *cmd)
  462. {
  463. uint32_t stream_id;
  464. size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
  465. cmd->elem->out_num,
  466. sizeof(virtio_snd_hdr),
  467. &stream_id,
  468. sizeof(stream_id));
  469. stream_id = le32_to_cpu(stream_id);
  470. cmd->resp.code = msg_sz == sizeof(stream_id)
  471. ? virtio_snd_pcm_prepare(s, stream_id)
  472. : cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  473. }
  474. /*
  475. * Handles VIRTIO_SND_R_PCM_START.
  476. *
  477. * @s: VirtIOSound device
  478. * @cmd: The request command queue element from VirtIOSound cmdq field
  479. * @start: whether to start or stop the device
  480. */
  481. static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s,
  482. virtio_snd_ctrl_command *cmd,
  483. bool start)
  484. {
  485. VirtIOSoundPCMStream *stream;
  486. virtio_snd_pcm_hdr req;
  487. uint32_t stream_id;
  488. size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
  489. cmd->elem->out_num,
  490. 0,
  491. &req,
  492. sizeof(virtio_snd_pcm_hdr));
  493. if (msg_sz != sizeof(virtio_snd_pcm_hdr)) {
  494. qemu_log_mask(LOG_GUEST_ERROR,
  495. "%s: virtio-snd command size incorrect %zu vs \
  496. %zu\n", __func__, msg_sz, sizeof(virtio_snd_pcm_hdr));
  497. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  498. return;
  499. }
  500. stream_id = le32_to_cpu(req.stream_id);
  501. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
  502. trace_virtio_snd_handle_pcm_start_stop(start ? "VIRTIO_SND_R_PCM_START" :
  503. "VIRTIO_SND_R_PCM_STOP", stream_id);
  504. stream = virtio_snd_pcm_get_stream(s, stream_id);
  505. if (stream) {
  506. WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
  507. stream->active = start;
  508. }
  509. if (stream->info.direction == VIRTIO_SND_D_OUTPUT) {
  510. AUD_set_active_out(stream->voice.out, start);
  511. } else {
  512. AUD_set_active_in(stream->voice.in, start);
  513. }
  514. } else {
  515. error_report("Invalid stream id: %"PRIu32, stream_id);
  516. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  517. return;
  518. }
  519. stream->active = start;
  520. }
  521. /*
  522. * Returns the number of I/O messages that are being processed.
  523. *
  524. * @stream: VirtIOSoundPCMStream
  525. */
  526. static size_t virtio_snd_pcm_get_io_msgs_count(VirtIOSoundPCMStream *stream)
  527. {
  528. VirtIOSoundPCMBuffer *buffer, *next;
  529. size_t count = 0;
  530. WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
  531. QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) {
  532. count += 1;
  533. }
  534. }
  535. return count;
  536. }
  537. /*
  538. * Handles VIRTIO_SND_R_PCM_RELEASE.
  539. *
  540. * @s: VirtIOSound device
  541. * @cmd: The request command queue element from VirtIOSound cmdq field
  542. */
  543. static void virtio_snd_handle_pcm_release(VirtIOSound *s,
  544. virtio_snd_ctrl_command *cmd)
  545. {
  546. uint32_t stream_id;
  547. VirtIOSoundPCMStream *stream;
  548. size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
  549. cmd->elem->out_num,
  550. sizeof(virtio_snd_hdr),
  551. &stream_id,
  552. sizeof(stream_id));
  553. if (msg_sz != sizeof(stream_id)) {
  554. /*
  555. * TODO: do we need to set DEVICE_NEEDS_RESET?
  556. */
  557. qemu_log_mask(LOG_GUEST_ERROR,
  558. "%s: virtio-snd command size incorrect %zu vs \
  559. %zu\n", __func__, msg_sz, sizeof(stream_id));
  560. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  561. return;
  562. }
  563. stream_id = le32_to_cpu(stream_id);
  564. trace_virtio_snd_handle_pcm_release(stream_id);
  565. stream = virtio_snd_pcm_get_stream(s, stream_id);
  566. if (stream == NULL) {
  567. /*
  568. * TODO: do we need to set DEVICE_NEEDS_RESET?
  569. */
  570. error_report("already released stream %"PRIu32, stream_id);
  571. virtio_error(VIRTIO_DEVICE(s),
  572. "already released stream %"PRIu32,
  573. stream_id);
  574. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  575. return;
  576. }
  577. if (virtio_snd_pcm_get_io_msgs_count(stream)) {
  578. /*
  579. * virtio-v1.2-csd01, 5.14.6.6.5.1,
  580. * Device Requirements: Stream Release
  581. *
  582. * - The device MUST complete all pending I/O messages for the
  583. * specified stream ID.
  584. * - The device MUST NOT complete the control request while there
  585. * are pending I/O messages for the specified stream ID.
  586. */
  587. trace_virtio_snd_pcm_stream_flush(stream_id);
  588. virtio_snd_pcm_flush(stream);
  589. }
  590. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
  591. }
  592. /*
  593. * The actual processing done in virtio_snd_process_cmdq().
  594. *
  595. * @s: VirtIOSound device
  596. * @cmd: control command request
  597. */
  598. static inline void
  599. process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd)
  600. {
  601. uint32_t code;
  602. size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
  603. cmd->elem->out_num,
  604. 0,
  605. &cmd->ctrl,
  606. sizeof(virtio_snd_hdr));
  607. if (msg_sz != sizeof(virtio_snd_hdr)) {
  608. /*
  609. * TODO: do we need to set DEVICE_NEEDS_RESET?
  610. */
  611. qemu_log_mask(LOG_GUEST_ERROR,
  612. "%s: virtio-snd command size incorrect %zu vs \
  613. %zu\n", __func__, msg_sz, sizeof(virtio_snd_hdr));
  614. return;
  615. }
  616. code = le32_to_cpu(cmd->ctrl.code);
  617. trace_virtio_snd_handle_code(code, print_code(code));
  618. switch (code) {
  619. case VIRTIO_SND_R_JACK_INFO:
  620. case VIRTIO_SND_R_JACK_REMAP:
  621. qemu_log_mask(LOG_UNIMP,
  622. "virtio_snd: jack functionality is unimplemented.\n");
  623. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
  624. break;
  625. case VIRTIO_SND_R_PCM_INFO:
  626. virtio_snd_handle_pcm_info(s, cmd);
  627. break;
  628. case VIRTIO_SND_R_PCM_START:
  629. virtio_snd_handle_pcm_start_stop(s, cmd, true);
  630. break;
  631. case VIRTIO_SND_R_PCM_STOP:
  632. virtio_snd_handle_pcm_start_stop(s, cmd, false);
  633. break;
  634. case VIRTIO_SND_R_PCM_SET_PARAMS:
  635. virtio_snd_handle_pcm_set_params(s, cmd);
  636. break;
  637. case VIRTIO_SND_R_PCM_PREPARE:
  638. virtio_snd_handle_pcm_prepare(s, cmd);
  639. break;
  640. case VIRTIO_SND_R_PCM_RELEASE:
  641. virtio_snd_handle_pcm_release(s, cmd);
  642. break;
  643. case VIRTIO_SND_R_CHMAP_INFO:
  644. qemu_log_mask(LOG_UNIMP,
  645. "virtio_snd: chmap info functionality is unimplemented.\n");
  646. trace_virtio_snd_handle_chmap_info();
  647. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
  648. break;
  649. default:
  650. /* error */
  651. error_report("virtio snd header not recognized: %"PRIu32, code);
  652. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  653. }
  654. iov_from_buf(cmd->elem->in_sg,
  655. cmd->elem->in_num,
  656. 0,
  657. &cmd->resp,
  658. sizeof(virtio_snd_hdr));
  659. virtqueue_push(cmd->vq, cmd->elem,
  660. sizeof(virtio_snd_hdr) + cmd->payload_size);
  661. virtio_notify(VIRTIO_DEVICE(s), cmd->vq);
  662. }
  663. /*
  664. * Consume all elements in command queue.
  665. *
  666. * @s: VirtIOSound device
  667. */
  668. static void virtio_snd_process_cmdq(VirtIOSound *s)
  669. {
  670. virtio_snd_ctrl_command *cmd;
  671. if (unlikely(qatomic_read(&s->processing_cmdq))) {
  672. return;
  673. }
  674. WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) {
  675. qatomic_set(&s->processing_cmdq, true);
  676. while (!QTAILQ_EMPTY(&s->cmdq)) {
  677. cmd = QTAILQ_FIRST(&s->cmdq);
  678. /* process command */
  679. process_cmd(s, cmd);
  680. QTAILQ_REMOVE(&s->cmdq, cmd, next);
  681. virtio_snd_ctrl_cmd_free(cmd);
  682. }
  683. qatomic_set(&s->processing_cmdq, false);
  684. }
  685. }
  686. /*
  687. * The control message handler. Pops an element from the control virtqueue,
  688. * and stores them to VirtIOSound's cmdq queue and finally calls
  689. * virtio_snd_process_cmdq() for processing.
  690. *
  691. * @vdev: VirtIOSound device
  692. * @vq: Control virtqueue
  693. */
  694. static void virtio_snd_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
  695. {
  696. VirtIOSound *s = VIRTIO_SND(vdev);
  697. VirtQueueElement *elem;
  698. virtio_snd_ctrl_command *cmd;
  699. trace_virtio_snd_handle_ctrl(vdev, vq);
  700. if (!virtio_queue_ready(vq)) {
  701. return;
  702. }
  703. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  704. while (elem) {
  705. cmd = g_new0(virtio_snd_ctrl_command, 1);
  706. cmd->elem = elem;
  707. cmd->vq = vq;
  708. cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_OK);
  709. /* implicit cmd->payload_size = 0; */
  710. QTAILQ_INSERT_TAIL(&s->cmdq, cmd, next);
  711. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  712. }
  713. virtio_snd_process_cmdq(s);
  714. }
  715. /*
  716. * The event virtqueue handler.
  717. * Not implemented yet.
  718. *
  719. * @vdev: VirtIOSound device
  720. * @vq: event vq
  721. */
  722. static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq)
  723. {
  724. qemu_log_mask(LOG_UNIMP, "virtio_snd: event queue is unimplemented.\n");
  725. trace_virtio_snd_handle_event();
  726. }
  727. /*
  728. * Must only be called if vsnd->invalid is not empty.
  729. */
  730. static inline void empty_invalid_queue(VirtIODevice *vdev, VirtQueue *vq)
  731. {
  732. VirtIOSoundPCMBuffer *buffer = NULL;
  733. virtio_snd_pcm_status resp = { 0 };
  734. VirtIOSound *vsnd = VIRTIO_SND(vdev);
  735. g_assert(!QSIMPLEQ_EMPTY(&vsnd->invalid));
  736. while (!QSIMPLEQ_EMPTY(&vsnd->invalid)) {
  737. buffer = QSIMPLEQ_FIRST(&vsnd->invalid);
  738. /* If buffer->vq != vq, our logic is fundamentally wrong, so bail out */
  739. g_assert(buffer->vq == vq);
  740. resp.status = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
  741. iov_from_buf(buffer->elem->in_sg,
  742. buffer->elem->in_num,
  743. 0,
  744. &resp,
  745. sizeof(virtio_snd_pcm_status));
  746. virtqueue_push(vq,
  747. buffer->elem,
  748. sizeof(virtio_snd_pcm_status));
  749. QSIMPLEQ_REMOVE_HEAD(&vsnd->invalid, entry);
  750. virtio_snd_pcm_buffer_free(buffer);
  751. }
  752. /* Notify vq about virtio_snd_pcm_status responses. */
  753. virtio_notify(vdev, vq);
  754. }
  755. /*
  756. * The tx virtqueue handler. Makes the buffers available to their respective
  757. * streams for consumption.
  758. *
  759. * @vdev: VirtIOSound device
  760. * @vq: tx virtqueue
  761. */
  762. static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)
  763. {
  764. VirtIOSound *vsnd = VIRTIO_SND(vdev);
  765. VirtIOSoundPCMBuffer *buffer;
  766. VirtQueueElement *elem;
  767. size_t msg_sz, size;
  768. virtio_snd_pcm_xfer hdr;
  769. uint32_t stream_id;
  770. /*
  771. * If any of the I/O messages are invalid, put them in vsnd->invalid and
  772. * return them after the for loop.
  773. */
  774. bool must_empty_invalid_queue = false;
  775. if (!virtio_queue_ready(vq)) {
  776. return;
  777. }
  778. trace_virtio_snd_handle_tx_xfer();
  779. for (;;) {
  780. VirtIOSoundPCMStream *stream;
  781. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  782. if (!elem) {
  783. break;
  784. }
  785. /* get the message hdr object */
  786. msg_sz = iov_to_buf(elem->out_sg,
  787. elem->out_num,
  788. 0,
  789. &hdr,
  790. sizeof(virtio_snd_pcm_xfer));
  791. if (msg_sz != sizeof(virtio_snd_pcm_xfer)) {
  792. goto tx_err;
  793. }
  794. stream_id = le32_to_cpu(hdr.stream_id);
  795. if (stream_id >= vsnd->snd_conf.streams
  796. || vsnd->pcm->streams[stream_id] == NULL) {
  797. goto tx_err;
  798. }
  799. stream = vsnd->pcm->streams[stream_id];
  800. if (stream->info.direction != VIRTIO_SND_D_OUTPUT) {
  801. goto tx_err;
  802. }
  803. WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
  804. size = iov_size(elem->out_sg, elem->out_num) - msg_sz;
  805. buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
  806. buffer->elem = elem;
  807. buffer->populated = false;
  808. buffer->vq = vq;
  809. buffer->size = size;
  810. buffer->offset = 0;
  811. QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
  812. }
  813. continue;
  814. tx_err:
  815. must_empty_invalid_queue = true;
  816. buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer));
  817. buffer->elem = elem;
  818. buffer->vq = vq;
  819. QSIMPLEQ_INSERT_TAIL(&vsnd->invalid, buffer, entry);
  820. }
  821. if (must_empty_invalid_queue) {
  822. empty_invalid_queue(vdev, vq);
  823. }
  824. }
  825. /*
  826. * The rx virtqueue handler. Makes the buffers available to their respective
  827. * streams for consumption.
  828. *
  829. * @vdev: VirtIOSound device
  830. * @vq: rx virtqueue
  831. */
  832. static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq)
  833. {
  834. VirtIOSound *vsnd = VIRTIO_SND(vdev);
  835. VirtIOSoundPCMBuffer *buffer;
  836. VirtQueueElement *elem;
  837. size_t msg_sz, size;
  838. virtio_snd_pcm_xfer hdr;
  839. uint32_t stream_id;
  840. /*
  841. * if any of the I/O messages are invalid, put them in vsnd->invalid and
  842. * return them after the for loop.
  843. */
  844. bool must_empty_invalid_queue = false;
  845. if (!virtio_queue_ready(vq)) {
  846. return;
  847. }
  848. trace_virtio_snd_handle_rx_xfer();
  849. for (;;) {
  850. VirtIOSoundPCMStream *stream;
  851. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  852. if (!elem) {
  853. break;
  854. }
  855. /* get the message hdr object */
  856. msg_sz = iov_to_buf(elem->out_sg,
  857. elem->out_num,
  858. 0,
  859. &hdr,
  860. sizeof(virtio_snd_pcm_xfer));
  861. if (msg_sz != sizeof(virtio_snd_pcm_xfer)) {
  862. goto rx_err;
  863. }
  864. stream_id = le32_to_cpu(hdr.stream_id);
  865. if (stream_id >= vsnd->snd_conf.streams
  866. || !vsnd->pcm->streams[stream_id]) {
  867. goto rx_err;
  868. }
  869. stream = vsnd->pcm->streams[stream_id];
  870. if (stream == NULL || stream->info.direction != VIRTIO_SND_D_INPUT) {
  871. goto rx_err;
  872. }
  873. WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
  874. size = iov_size(elem->in_sg, elem->in_num) -
  875. sizeof(virtio_snd_pcm_status);
  876. buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size);
  877. buffer->elem = elem;
  878. buffer->vq = vq;
  879. buffer->size = 0;
  880. buffer->offset = 0;
  881. QSIMPLEQ_INSERT_TAIL(&stream->queue, buffer, entry);
  882. }
  883. continue;
  884. rx_err:
  885. must_empty_invalid_queue = true;
  886. buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer));
  887. buffer->elem = elem;
  888. buffer->vq = vq;
  889. QSIMPLEQ_INSERT_TAIL(&vsnd->invalid, buffer, entry);
  890. }
  891. if (must_empty_invalid_queue) {
  892. empty_invalid_queue(vdev, vq);
  893. }
  894. }
  895. static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
  896. Error **errp)
  897. {
  898. /*
  899. * virtio-v1.2-csd01, 5.14.3,
  900. * Feature Bits
  901. * None currently defined.
  902. */
  903. VirtIOSound *s = VIRTIO_SND(vdev);
  904. features |= s->features;
  905. trace_virtio_snd_get_features(vdev, features);
  906. return features;
  907. }
  908. static void
  909. virtio_snd_vm_state_change(void *opaque, bool running,
  910. RunState state)
  911. {
  912. if (running) {
  913. trace_virtio_snd_vm_state_running();
  914. } else {
  915. trace_virtio_snd_vm_state_stopped();
  916. }
  917. }
  918. static void virtio_snd_realize(DeviceState *dev, Error **errp)
  919. {
  920. ERRP_GUARD();
  921. VirtIOSound *vsnd = VIRTIO_SND(dev);
  922. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  923. virtio_snd_pcm_set_params default_params = { 0 };
  924. uint32_t status;
  925. trace_virtio_snd_realize(vsnd);
  926. /* check number of jacks and streams */
  927. if (vsnd->snd_conf.jacks > 8) {
  928. error_setg(errp,
  929. "Invalid number of jacks: %"PRIu32,
  930. vsnd->snd_conf.jacks);
  931. return;
  932. }
  933. if (vsnd->snd_conf.streams < 1 || vsnd->snd_conf.streams > 10) {
  934. error_setg(errp,
  935. "Invalid number of streams: %"PRIu32,
  936. vsnd->snd_conf.streams);
  937. return;
  938. }
  939. if (vsnd->snd_conf.chmaps > VIRTIO_SND_CHMAP_MAX_SIZE) {
  940. error_setg(errp,
  941. "Invalid number of channel maps: %"PRIu32,
  942. vsnd->snd_conf.chmaps);
  943. return;
  944. }
  945. if (!AUD_register_card("virtio-sound", &vsnd->card, errp)) {
  946. return;
  947. }
  948. vsnd->vmstate =
  949. qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd);
  950. vsnd->pcm = g_new0(VirtIOSoundPCM, 1);
  951. vsnd->pcm->snd = vsnd;
  952. vsnd->pcm->streams =
  953. g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams);
  954. vsnd->pcm->pcm_params =
  955. g_new0(virtio_snd_pcm_set_params, vsnd->snd_conf.streams);
  956. virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config));
  957. virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1);
  958. /* set default params for all streams */
  959. default_params.features = 0;
  960. default_params.buffer_bytes = cpu_to_le32(8192);
  961. default_params.period_bytes = cpu_to_le32(2048);
  962. default_params.channels = 2;
  963. default_params.format = VIRTIO_SND_PCM_FMT_S16;
  964. default_params.rate = VIRTIO_SND_PCM_RATE_48000;
  965. vsnd->queues[VIRTIO_SND_VQ_CONTROL] =
  966. virtio_add_queue(vdev, 64, virtio_snd_handle_ctrl);
  967. vsnd->queues[VIRTIO_SND_VQ_EVENT] =
  968. virtio_add_queue(vdev, 64, virtio_snd_handle_event);
  969. vsnd->queues[VIRTIO_SND_VQ_TX] =
  970. virtio_add_queue(vdev, 64, virtio_snd_handle_tx_xfer);
  971. vsnd->queues[VIRTIO_SND_VQ_RX] =
  972. virtio_add_queue(vdev, 64, virtio_snd_handle_rx_xfer);
  973. qemu_mutex_init(&vsnd->cmdq_mutex);
  974. QTAILQ_INIT(&vsnd->cmdq);
  975. QSIMPLEQ_INIT(&vsnd->invalid);
  976. for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
  977. status = virtio_snd_set_pcm_params(vsnd, i, &default_params);
  978. if (status != cpu_to_le32(VIRTIO_SND_S_OK)) {
  979. error_setg(errp,
  980. "Can't initialize stream params, device responded with %s.",
  981. print_code(status));
  982. goto error_cleanup;
  983. }
  984. status = virtio_snd_pcm_prepare(vsnd, i);
  985. if (status != cpu_to_le32(VIRTIO_SND_S_OK)) {
  986. error_setg(errp,
  987. "Can't prepare streams, device responded with %s.",
  988. print_code(status));
  989. goto error_cleanup;
  990. }
  991. }
  992. return;
  993. error_cleanup:
  994. virtio_snd_unrealize(dev);
  995. }
  996. static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,
  997. VirtIOSoundPCMBuffer *buffer)
  998. {
  999. virtio_snd_pcm_status resp = { 0 };
  1000. resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
  1001. resp.latency_bytes = cpu_to_le32((uint32_t)buffer->size);
  1002. iov_from_buf(buffer->elem->in_sg,
  1003. buffer->elem->in_num,
  1004. 0,
  1005. &resp,
  1006. sizeof(virtio_snd_pcm_status));
  1007. virtqueue_push(buffer->vq,
  1008. buffer->elem,
  1009. sizeof(virtio_snd_pcm_status));
  1010. virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
  1011. QSIMPLEQ_REMOVE(&stream->queue,
  1012. buffer,
  1013. VirtIOSoundPCMBuffer,
  1014. entry);
  1015. virtio_snd_pcm_buffer_free(buffer);
  1016. }
  1017. /*
  1018. * AUD_* output callback.
  1019. *
  1020. * @data: VirtIOSoundPCMStream stream
  1021. * @available: number of bytes that can be written with AUD_write()
  1022. */
  1023. static void virtio_snd_pcm_out_cb(void *data, int available)
  1024. {
  1025. VirtIOSoundPCMStream *stream = data;
  1026. VirtIOSoundPCMBuffer *buffer;
  1027. size_t size;
  1028. WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
  1029. while (!QSIMPLEQ_EMPTY(&stream->queue)) {
  1030. buffer = QSIMPLEQ_FIRST(&stream->queue);
  1031. if (!virtio_queue_ready(buffer->vq)) {
  1032. return;
  1033. }
  1034. if (!stream->active) {
  1035. /* Stream has stopped, so do not perform AUD_write. */
  1036. return_tx_buffer(stream, buffer);
  1037. continue;
  1038. }
  1039. if (!buffer->populated) {
  1040. iov_to_buf(buffer->elem->out_sg,
  1041. buffer->elem->out_num,
  1042. sizeof(virtio_snd_pcm_xfer),
  1043. buffer->data,
  1044. buffer->size);
  1045. buffer->populated = true;
  1046. }
  1047. for (;;) {
  1048. size = AUD_write(stream->voice.out,
  1049. buffer->data + buffer->offset,
  1050. MIN(buffer->size, available));
  1051. assert(size <= MIN(buffer->size, available));
  1052. if (size == 0) {
  1053. /* break out of both loops */
  1054. available = 0;
  1055. break;
  1056. }
  1057. buffer->size -= size;
  1058. buffer->offset += size;
  1059. available -= size;
  1060. if (buffer->size < 1) {
  1061. return_tx_buffer(stream, buffer);
  1062. break;
  1063. }
  1064. if (!available) {
  1065. break;
  1066. }
  1067. }
  1068. if (!available) {
  1069. break;
  1070. }
  1071. }
  1072. }
  1073. }
  1074. /*
  1075. * Flush all buffer data from this input stream's queue into the driver's
  1076. * virtual queue.
  1077. *
  1078. * @stream: VirtIOSoundPCMStream *stream
  1079. */
  1080. static inline void return_rx_buffer(VirtIOSoundPCMStream *stream,
  1081. VirtIOSoundPCMBuffer *buffer)
  1082. {
  1083. virtio_snd_pcm_status resp = { 0 };
  1084. resp.status = cpu_to_le32(VIRTIO_SND_S_OK);
  1085. resp.latency_bytes = 0;
  1086. /* Copy data -if any- to guest */
  1087. iov_from_buf(buffer->elem->in_sg,
  1088. buffer->elem->in_num,
  1089. 0,
  1090. buffer->data,
  1091. buffer->size);
  1092. iov_from_buf(buffer->elem->in_sg,
  1093. buffer->elem->in_num,
  1094. buffer->size,
  1095. &resp,
  1096. sizeof(virtio_snd_pcm_status));
  1097. virtqueue_push(buffer->vq,
  1098. buffer->elem,
  1099. sizeof(virtio_snd_pcm_status) + buffer->size);
  1100. virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq);
  1101. QSIMPLEQ_REMOVE(&stream->queue,
  1102. buffer,
  1103. VirtIOSoundPCMBuffer,
  1104. entry);
  1105. virtio_snd_pcm_buffer_free(buffer);
  1106. }
  1107. /*
  1108. * AUD_* input callback.
  1109. *
  1110. * @data: VirtIOSoundPCMStream stream
  1111. * @available: number of bytes that can be read with AUD_read()
  1112. */
  1113. static void virtio_snd_pcm_in_cb(void *data, int available)
  1114. {
  1115. VirtIOSoundPCMStream *stream = data;
  1116. VirtIOSoundPCMBuffer *buffer;
  1117. size_t size, max_size;
  1118. WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
  1119. while (!QSIMPLEQ_EMPTY(&stream->queue)) {
  1120. buffer = QSIMPLEQ_FIRST(&stream->queue);
  1121. if (!virtio_queue_ready(buffer->vq)) {
  1122. return;
  1123. }
  1124. if (!stream->active) {
  1125. /* Stream has stopped, so do not perform AUD_read. */
  1126. return_rx_buffer(stream, buffer);
  1127. continue;
  1128. }
  1129. max_size = iov_size(buffer->elem->in_sg, buffer->elem->in_num);
  1130. for (;;) {
  1131. if (buffer->size >= max_size) {
  1132. return_rx_buffer(stream, buffer);
  1133. break;
  1134. }
  1135. size = AUD_read(stream->voice.in,
  1136. buffer->data + buffer->size,
  1137. MIN(available, (stream->params.period_bytes -
  1138. buffer->size)));
  1139. if (!size) {
  1140. available = 0;
  1141. break;
  1142. }
  1143. buffer->size += size;
  1144. available -= size;
  1145. if (buffer->size >= stream->params.period_bytes) {
  1146. return_rx_buffer(stream, buffer);
  1147. break;
  1148. }
  1149. if (!available) {
  1150. break;
  1151. }
  1152. }
  1153. if (!available) {
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. }
  1159. /*
  1160. * Flush all buffer data from this output stream's queue into the driver's
  1161. * virtual queue.
  1162. *
  1163. * @stream: VirtIOSoundPCMStream *stream
  1164. */
  1165. static inline void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream)
  1166. {
  1167. VirtIOSoundPCMBuffer *buffer;
  1168. void (*cb)(VirtIOSoundPCMStream *, VirtIOSoundPCMBuffer *) =
  1169. (stream->info.direction == VIRTIO_SND_D_OUTPUT) ? return_tx_buffer :
  1170. return_rx_buffer;
  1171. WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
  1172. while (!QSIMPLEQ_EMPTY(&stream->queue)) {
  1173. buffer = QSIMPLEQ_FIRST(&stream->queue);
  1174. cb(stream, buffer);
  1175. }
  1176. }
  1177. }
  1178. static void virtio_snd_unrealize(DeviceState *dev)
  1179. {
  1180. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  1181. VirtIOSound *vsnd = VIRTIO_SND(dev);
  1182. VirtIOSoundPCMStream *stream;
  1183. qemu_del_vm_change_state_handler(vsnd->vmstate);
  1184. trace_virtio_snd_unrealize(vsnd);
  1185. if (vsnd->pcm) {
  1186. if (vsnd->pcm->streams) {
  1187. for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
  1188. stream = vsnd->pcm->streams[i];
  1189. if (stream) {
  1190. virtio_snd_process_cmdq(stream->s);
  1191. virtio_snd_pcm_close(stream);
  1192. qemu_mutex_destroy(&stream->queue_mutex);
  1193. g_free(stream);
  1194. }
  1195. }
  1196. g_free(vsnd->pcm->streams);
  1197. }
  1198. g_free(vsnd->pcm->pcm_params);
  1199. g_free(vsnd->pcm);
  1200. vsnd->pcm = NULL;
  1201. }
  1202. AUD_remove_card(&vsnd->card);
  1203. qemu_mutex_destroy(&vsnd->cmdq_mutex);
  1204. virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_CONTROL]);
  1205. virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_EVENT]);
  1206. virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_TX]);
  1207. virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_RX]);
  1208. virtio_cleanup(vdev);
  1209. }
  1210. static void virtio_snd_reset(VirtIODevice *vdev)
  1211. {
  1212. VirtIOSound *vsnd = VIRTIO_SND(vdev);
  1213. virtio_snd_ctrl_command *cmd;
  1214. /*
  1215. * Sanity check that the invalid buffer message queue is emptied at the end
  1216. * of every virtio_snd_handle_tx_xfer/virtio_snd_handle_rx_xfer call, and
  1217. * must be empty otherwise.
  1218. */
  1219. g_assert(QSIMPLEQ_EMPTY(&vsnd->invalid));
  1220. WITH_QEMU_LOCK_GUARD(&vsnd->cmdq_mutex) {
  1221. while (!QTAILQ_EMPTY(&vsnd->cmdq)) {
  1222. cmd = QTAILQ_FIRST(&vsnd->cmdq);
  1223. QTAILQ_REMOVE(&vsnd->cmdq, cmd, next);
  1224. virtio_snd_ctrl_cmd_free(cmd);
  1225. }
  1226. }
  1227. }
  1228. static void virtio_snd_class_init(ObjectClass *klass, void *data)
  1229. {
  1230. DeviceClass *dc = DEVICE_CLASS(klass);
  1231. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  1232. set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
  1233. device_class_set_props(dc, virtio_snd_properties);
  1234. dc->vmsd = &vmstate_virtio_snd;
  1235. vdc->vmsd = &vmstate_virtio_snd_device;
  1236. vdc->realize = virtio_snd_realize;
  1237. vdc->unrealize = virtio_snd_unrealize;
  1238. vdc->get_config = virtio_snd_get_config;
  1239. vdc->get_features = get_features;
  1240. vdc->reset = virtio_snd_reset;
  1241. vdc->legacy_features = 0;
  1242. }
  1243. static const TypeInfo virtio_snd_types[] = {
  1244. {
  1245. .name = TYPE_VIRTIO_SND,
  1246. .parent = TYPE_VIRTIO_DEVICE,
  1247. .instance_size = sizeof(VirtIOSound),
  1248. .class_init = virtio_snd_class_init,
  1249. }
  1250. };
  1251. DEFINE_TYPES(virtio_snd_types)