virtio-snd.c 43 KB

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