xen_snd_front_alsa.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Xen para-virtual sound device
  4. *
  5. * Copyright (C) 2016-2018 EPAM Systems Inc.
  6. *
  7. * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
  8. */
  9. #include <linux/platform_device.h>
  10. #include <sound/core.h>
  11. #include <sound/pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include <xen/xenbus.h>
  14. #include <xen/xen-front-pgdir-shbuf.h>
  15. #include "xen_snd_front.h"
  16. #include "xen_snd_front_alsa.h"
  17. #include "xen_snd_front_cfg.h"
  18. #include "xen_snd_front_evtchnl.h"
  19. struct xen_snd_front_pcm_stream_info {
  20. struct xen_snd_front_info *front_info;
  21. struct xen_snd_front_evtchnl_pair *evt_pair;
  22. /* This is the shared buffer with its backing storage. */
  23. struct xen_front_pgdir_shbuf shbuf;
  24. u8 *buffer;
  25. size_t buffer_sz;
  26. int num_pages;
  27. struct page **pages;
  28. int index;
  29. bool is_open;
  30. struct snd_pcm_hardware pcm_hw;
  31. /* Number of processed frames as reported by the backend. */
  32. snd_pcm_uframes_t be_cur_frame;
  33. /* Current HW pointer to be reported via .period callback. */
  34. atomic_t hw_ptr;
  35. /* Modulo of the number of processed frames - for period detection. */
  36. u32 out_frames;
  37. };
  38. struct xen_snd_front_pcm_instance_info {
  39. struct xen_snd_front_card_info *card_info;
  40. struct snd_pcm *pcm;
  41. struct snd_pcm_hardware pcm_hw;
  42. int num_pcm_streams_pb;
  43. struct xen_snd_front_pcm_stream_info *streams_pb;
  44. int num_pcm_streams_cap;
  45. struct xen_snd_front_pcm_stream_info *streams_cap;
  46. };
  47. struct xen_snd_front_card_info {
  48. struct xen_snd_front_info *front_info;
  49. struct snd_card *card;
  50. struct snd_pcm_hardware pcm_hw;
  51. int num_pcm_instances;
  52. struct xen_snd_front_pcm_instance_info *pcm_instances;
  53. };
  54. struct alsa_sndif_sample_format {
  55. u8 sndif;
  56. snd_pcm_format_t alsa;
  57. };
  58. struct alsa_sndif_hw_param {
  59. u8 sndif;
  60. snd_pcm_hw_param_t alsa;
  61. };
  62. static const struct alsa_sndif_sample_format ALSA_SNDIF_FORMATS[] = {
  63. {
  64. .sndif = XENSND_PCM_FORMAT_U8,
  65. .alsa = SNDRV_PCM_FORMAT_U8
  66. },
  67. {
  68. .sndif = XENSND_PCM_FORMAT_S8,
  69. .alsa = SNDRV_PCM_FORMAT_S8
  70. },
  71. {
  72. .sndif = XENSND_PCM_FORMAT_U16_LE,
  73. .alsa = SNDRV_PCM_FORMAT_U16_LE
  74. },
  75. {
  76. .sndif = XENSND_PCM_FORMAT_U16_BE,
  77. .alsa = SNDRV_PCM_FORMAT_U16_BE
  78. },
  79. {
  80. .sndif = XENSND_PCM_FORMAT_S16_LE,
  81. .alsa = SNDRV_PCM_FORMAT_S16_LE
  82. },
  83. {
  84. .sndif = XENSND_PCM_FORMAT_S16_BE,
  85. .alsa = SNDRV_PCM_FORMAT_S16_BE
  86. },
  87. {
  88. .sndif = XENSND_PCM_FORMAT_U24_LE,
  89. .alsa = SNDRV_PCM_FORMAT_U24_LE
  90. },
  91. {
  92. .sndif = XENSND_PCM_FORMAT_U24_BE,
  93. .alsa = SNDRV_PCM_FORMAT_U24_BE
  94. },
  95. {
  96. .sndif = XENSND_PCM_FORMAT_S24_LE,
  97. .alsa = SNDRV_PCM_FORMAT_S24_LE
  98. },
  99. {
  100. .sndif = XENSND_PCM_FORMAT_S24_BE,
  101. .alsa = SNDRV_PCM_FORMAT_S24_BE
  102. },
  103. {
  104. .sndif = XENSND_PCM_FORMAT_U32_LE,
  105. .alsa = SNDRV_PCM_FORMAT_U32_LE
  106. },
  107. {
  108. .sndif = XENSND_PCM_FORMAT_U32_BE,
  109. .alsa = SNDRV_PCM_FORMAT_U32_BE
  110. },
  111. {
  112. .sndif = XENSND_PCM_FORMAT_S32_LE,
  113. .alsa = SNDRV_PCM_FORMAT_S32_LE
  114. },
  115. {
  116. .sndif = XENSND_PCM_FORMAT_S32_BE,
  117. .alsa = SNDRV_PCM_FORMAT_S32_BE
  118. },
  119. {
  120. .sndif = XENSND_PCM_FORMAT_A_LAW,
  121. .alsa = SNDRV_PCM_FORMAT_A_LAW
  122. },
  123. {
  124. .sndif = XENSND_PCM_FORMAT_MU_LAW,
  125. .alsa = SNDRV_PCM_FORMAT_MU_LAW
  126. },
  127. {
  128. .sndif = XENSND_PCM_FORMAT_F32_LE,
  129. .alsa = SNDRV_PCM_FORMAT_FLOAT_LE
  130. },
  131. {
  132. .sndif = XENSND_PCM_FORMAT_F32_BE,
  133. .alsa = SNDRV_PCM_FORMAT_FLOAT_BE
  134. },
  135. {
  136. .sndif = XENSND_PCM_FORMAT_F64_LE,
  137. .alsa = SNDRV_PCM_FORMAT_FLOAT64_LE
  138. },
  139. {
  140. .sndif = XENSND_PCM_FORMAT_F64_BE,
  141. .alsa = SNDRV_PCM_FORMAT_FLOAT64_BE
  142. },
  143. {
  144. .sndif = XENSND_PCM_FORMAT_IEC958_SUBFRAME_LE,
  145. .alsa = SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE
  146. },
  147. {
  148. .sndif = XENSND_PCM_FORMAT_IEC958_SUBFRAME_BE,
  149. .alsa = SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE
  150. },
  151. {
  152. .sndif = XENSND_PCM_FORMAT_IMA_ADPCM,
  153. .alsa = SNDRV_PCM_FORMAT_IMA_ADPCM
  154. },
  155. {
  156. .sndif = XENSND_PCM_FORMAT_MPEG,
  157. .alsa = SNDRV_PCM_FORMAT_MPEG
  158. },
  159. {
  160. .sndif = XENSND_PCM_FORMAT_GSM,
  161. .alsa = SNDRV_PCM_FORMAT_GSM
  162. },
  163. };
  164. static int to_sndif_format(snd_pcm_format_t format)
  165. {
  166. int i;
  167. for (i = 0; i < ARRAY_SIZE(ALSA_SNDIF_FORMATS); i++)
  168. if (ALSA_SNDIF_FORMATS[i].alsa == format)
  169. return ALSA_SNDIF_FORMATS[i].sndif;
  170. return -EINVAL;
  171. }
  172. static u64 to_sndif_formats_mask(u64 alsa_formats)
  173. {
  174. u64 mask;
  175. int i;
  176. mask = 0;
  177. for (i = 0; i < ARRAY_SIZE(ALSA_SNDIF_FORMATS); i++)
  178. if (pcm_format_to_bits(ALSA_SNDIF_FORMATS[i].alsa) & alsa_formats)
  179. mask |= BIT_ULL(ALSA_SNDIF_FORMATS[i].sndif);
  180. return mask;
  181. }
  182. static u64 to_alsa_formats_mask(u64 sndif_formats)
  183. {
  184. u64 mask;
  185. int i;
  186. mask = 0;
  187. for (i = 0; i < ARRAY_SIZE(ALSA_SNDIF_FORMATS); i++)
  188. if (BIT_ULL(ALSA_SNDIF_FORMATS[i].sndif) & sndif_formats)
  189. mask |= pcm_format_to_bits(ALSA_SNDIF_FORMATS[i].alsa);
  190. return mask;
  191. }
  192. static void stream_clear(struct xen_snd_front_pcm_stream_info *stream)
  193. {
  194. stream->is_open = false;
  195. stream->be_cur_frame = 0;
  196. stream->out_frames = 0;
  197. atomic_set(&stream->hw_ptr, 0);
  198. xen_snd_front_evtchnl_pair_clear(stream->evt_pair);
  199. memset(&stream->shbuf, 0, sizeof(stream->shbuf));
  200. stream->buffer = NULL;
  201. stream->buffer_sz = 0;
  202. stream->pages = NULL;
  203. stream->num_pages = 0;
  204. }
  205. static void stream_free(struct xen_snd_front_pcm_stream_info *stream)
  206. {
  207. xen_front_pgdir_shbuf_unmap(&stream->shbuf);
  208. xen_front_pgdir_shbuf_free(&stream->shbuf);
  209. if (stream->buffer)
  210. free_pages_exact(stream->buffer, stream->buffer_sz);
  211. kfree(stream->pages);
  212. stream_clear(stream);
  213. }
  214. static struct xen_snd_front_pcm_stream_info *
  215. stream_get(struct snd_pcm_substream *substream)
  216. {
  217. struct xen_snd_front_pcm_instance_info *pcm_instance =
  218. snd_pcm_substream_chip(substream);
  219. struct xen_snd_front_pcm_stream_info *stream;
  220. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  221. stream = &pcm_instance->streams_pb[substream->number];
  222. else
  223. stream = &pcm_instance->streams_cap[substream->number];
  224. return stream;
  225. }
  226. static int alsa_hw_rule(struct snd_pcm_hw_params *params,
  227. struct snd_pcm_hw_rule *rule)
  228. {
  229. struct xen_snd_front_pcm_stream_info *stream = rule->private;
  230. struct device *dev = &stream->front_info->xb_dev->dev;
  231. struct snd_mask *formats =
  232. hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  233. struct snd_interval *rates =
  234. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  235. struct snd_interval *channels =
  236. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  237. struct snd_interval *period =
  238. hw_param_interval(params,
  239. SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  240. struct snd_interval *buffer =
  241. hw_param_interval(params,
  242. SNDRV_PCM_HW_PARAM_BUFFER_SIZE);
  243. struct xensnd_query_hw_param req;
  244. struct xensnd_query_hw_param resp;
  245. struct snd_interval interval;
  246. struct snd_mask mask;
  247. u64 sndif_formats;
  248. int changed, ret;
  249. /* Collect all the values we need for the query. */
  250. req.formats = to_sndif_formats_mask((u64)formats->bits[0] |
  251. (u64)(formats->bits[1]) << 32);
  252. req.rates.min = rates->min;
  253. req.rates.max = rates->max;
  254. req.channels.min = channels->min;
  255. req.channels.max = channels->max;
  256. req.buffer.min = buffer->min;
  257. req.buffer.max = buffer->max;
  258. req.period.min = period->min;
  259. req.period.max = period->max;
  260. ret = xen_snd_front_stream_query_hw_param(&stream->evt_pair->req,
  261. &req, &resp);
  262. if (ret < 0) {
  263. /* Check if this is due to backend communication error. */
  264. if (ret == -EIO || ret == -ETIMEDOUT)
  265. dev_err(dev, "Failed to query ALSA HW parameters\n");
  266. return ret;
  267. }
  268. /* Refine HW parameters after the query. */
  269. changed = 0;
  270. sndif_formats = to_alsa_formats_mask(resp.formats);
  271. snd_mask_none(&mask);
  272. mask.bits[0] = (u32)sndif_formats;
  273. mask.bits[1] = (u32)(sndif_formats >> 32);
  274. ret = snd_mask_refine(formats, &mask);
  275. if (ret < 0)
  276. return ret;
  277. changed |= ret;
  278. interval.openmin = 0;
  279. interval.openmax = 0;
  280. interval.integer = 1;
  281. interval.min = resp.rates.min;
  282. interval.max = resp.rates.max;
  283. ret = snd_interval_refine(rates, &interval);
  284. if (ret < 0)
  285. return ret;
  286. changed |= ret;
  287. interval.min = resp.channels.min;
  288. interval.max = resp.channels.max;
  289. ret = snd_interval_refine(channels, &interval);
  290. if (ret < 0)
  291. return ret;
  292. changed |= ret;
  293. interval.min = resp.buffer.min;
  294. interval.max = resp.buffer.max;
  295. ret = snd_interval_refine(buffer, &interval);
  296. if (ret < 0)
  297. return ret;
  298. changed |= ret;
  299. interval.min = resp.period.min;
  300. interval.max = resp.period.max;
  301. ret = snd_interval_refine(period, &interval);
  302. if (ret < 0)
  303. return ret;
  304. changed |= ret;
  305. return changed;
  306. }
  307. static int alsa_open(struct snd_pcm_substream *substream)
  308. {
  309. struct xen_snd_front_pcm_instance_info *pcm_instance =
  310. snd_pcm_substream_chip(substream);
  311. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  312. struct snd_pcm_runtime *runtime = substream->runtime;
  313. struct xen_snd_front_info *front_info =
  314. pcm_instance->card_info->front_info;
  315. struct device *dev = &front_info->xb_dev->dev;
  316. int ret;
  317. /*
  318. * Return our HW properties: override defaults with those configured
  319. * via XenStore.
  320. */
  321. runtime->hw = stream->pcm_hw;
  322. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
  323. SNDRV_PCM_INFO_MMAP_VALID |
  324. SNDRV_PCM_INFO_DOUBLE |
  325. SNDRV_PCM_INFO_BATCH |
  326. SNDRV_PCM_INFO_NONINTERLEAVED |
  327. SNDRV_PCM_INFO_RESUME |
  328. SNDRV_PCM_INFO_PAUSE);
  329. runtime->hw.info |= SNDRV_PCM_INFO_INTERLEAVED;
  330. stream->evt_pair = &front_info->evt_pairs[stream->index];
  331. stream->front_info = front_info;
  332. stream->evt_pair->evt.u.evt.substream = substream;
  333. stream_clear(stream);
  334. xen_snd_front_evtchnl_pair_set_connected(stream->evt_pair, true);
  335. ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  336. alsa_hw_rule, stream,
  337. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  338. if (ret) {
  339. dev_err(dev, "Failed to add HW rule for SNDRV_PCM_HW_PARAM_FORMAT\n");
  340. return ret;
  341. }
  342. ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  343. alsa_hw_rule, stream,
  344. SNDRV_PCM_HW_PARAM_RATE, -1);
  345. if (ret) {
  346. dev_err(dev, "Failed to add HW rule for SNDRV_PCM_HW_PARAM_RATE\n");
  347. return ret;
  348. }
  349. ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  350. alsa_hw_rule, stream,
  351. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  352. if (ret) {
  353. dev_err(dev, "Failed to add HW rule for SNDRV_PCM_HW_PARAM_CHANNELS\n");
  354. return ret;
  355. }
  356. ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  357. alsa_hw_rule, stream,
  358. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  359. if (ret) {
  360. dev_err(dev, "Failed to add HW rule for SNDRV_PCM_HW_PARAM_PERIOD_SIZE\n");
  361. return ret;
  362. }
  363. ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  364. alsa_hw_rule, stream,
  365. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
  366. if (ret) {
  367. dev_err(dev, "Failed to add HW rule for SNDRV_PCM_HW_PARAM_BUFFER_SIZE\n");
  368. return ret;
  369. }
  370. return 0;
  371. }
  372. static int alsa_close(struct snd_pcm_substream *substream)
  373. {
  374. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  375. xen_snd_front_evtchnl_pair_set_connected(stream->evt_pair, false);
  376. return 0;
  377. }
  378. static int shbuf_setup_backstore(struct xen_snd_front_pcm_stream_info *stream,
  379. size_t buffer_sz)
  380. {
  381. int i;
  382. stream->buffer = alloc_pages_exact(buffer_sz, GFP_KERNEL);
  383. if (!stream->buffer)
  384. return -ENOMEM;
  385. stream->buffer_sz = buffer_sz;
  386. stream->num_pages = DIV_ROUND_UP(stream->buffer_sz, PAGE_SIZE);
  387. stream->pages = kcalloc(stream->num_pages, sizeof(struct page *),
  388. GFP_KERNEL);
  389. if (!stream->pages)
  390. return -ENOMEM;
  391. for (i = 0; i < stream->num_pages; i++)
  392. stream->pages[i] = virt_to_page(stream->buffer + i * PAGE_SIZE);
  393. return 0;
  394. }
  395. static int alsa_hw_params(struct snd_pcm_substream *substream,
  396. struct snd_pcm_hw_params *params)
  397. {
  398. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  399. struct xen_snd_front_info *front_info = stream->front_info;
  400. struct xen_front_pgdir_shbuf_cfg buf_cfg;
  401. int ret;
  402. /*
  403. * This callback may be called multiple times,
  404. * so free the previously allocated shared buffer if any.
  405. */
  406. stream_free(stream);
  407. ret = shbuf_setup_backstore(stream, params_buffer_bytes(params));
  408. if (ret < 0)
  409. goto fail;
  410. memset(&buf_cfg, 0, sizeof(buf_cfg));
  411. buf_cfg.xb_dev = front_info->xb_dev;
  412. buf_cfg.pgdir = &stream->shbuf;
  413. buf_cfg.num_pages = stream->num_pages;
  414. buf_cfg.pages = stream->pages;
  415. ret = xen_front_pgdir_shbuf_alloc(&buf_cfg);
  416. if (ret < 0)
  417. goto fail;
  418. ret = xen_front_pgdir_shbuf_map(&stream->shbuf);
  419. if (ret < 0)
  420. goto fail;
  421. return 0;
  422. fail:
  423. stream_free(stream);
  424. dev_err(&front_info->xb_dev->dev,
  425. "Failed to allocate buffers for stream with index %d\n",
  426. stream->index);
  427. return ret;
  428. }
  429. static int alsa_hw_free(struct snd_pcm_substream *substream)
  430. {
  431. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  432. int ret;
  433. ret = xen_snd_front_stream_close(&stream->evt_pair->req);
  434. stream_free(stream);
  435. return ret;
  436. }
  437. static int alsa_prepare(struct snd_pcm_substream *substream)
  438. {
  439. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  440. if (!stream->is_open) {
  441. struct snd_pcm_runtime *runtime = substream->runtime;
  442. u8 sndif_format;
  443. int ret;
  444. ret = to_sndif_format(runtime->format);
  445. if (ret < 0) {
  446. dev_err(&stream->front_info->xb_dev->dev,
  447. "Unsupported sample format: %d\n",
  448. runtime->format);
  449. return ret;
  450. }
  451. sndif_format = ret;
  452. ret = xen_snd_front_stream_prepare(&stream->evt_pair->req,
  453. &stream->shbuf,
  454. sndif_format,
  455. runtime->channels,
  456. runtime->rate,
  457. snd_pcm_lib_buffer_bytes(substream),
  458. snd_pcm_lib_period_bytes(substream));
  459. if (ret < 0)
  460. return ret;
  461. stream->is_open = true;
  462. }
  463. return 0;
  464. }
  465. static int alsa_trigger(struct snd_pcm_substream *substream, int cmd)
  466. {
  467. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  468. int type;
  469. switch (cmd) {
  470. case SNDRV_PCM_TRIGGER_START:
  471. type = XENSND_OP_TRIGGER_START;
  472. break;
  473. case SNDRV_PCM_TRIGGER_RESUME:
  474. type = XENSND_OP_TRIGGER_RESUME;
  475. break;
  476. case SNDRV_PCM_TRIGGER_STOP:
  477. type = XENSND_OP_TRIGGER_STOP;
  478. break;
  479. case SNDRV_PCM_TRIGGER_SUSPEND:
  480. type = XENSND_OP_TRIGGER_PAUSE;
  481. break;
  482. default:
  483. return -EINVAL;
  484. }
  485. return xen_snd_front_stream_trigger(&stream->evt_pair->req, type);
  486. }
  487. void xen_snd_front_alsa_handle_cur_pos(struct xen_snd_front_evtchnl *evtchnl,
  488. u64 pos_bytes)
  489. {
  490. struct snd_pcm_substream *substream = evtchnl->u.evt.substream;
  491. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  492. snd_pcm_uframes_t delta, new_hw_ptr, cur_frame;
  493. cur_frame = bytes_to_frames(substream->runtime, pos_bytes);
  494. delta = cur_frame - stream->be_cur_frame;
  495. stream->be_cur_frame = cur_frame;
  496. new_hw_ptr = (snd_pcm_uframes_t)atomic_read(&stream->hw_ptr);
  497. new_hw_ptr = (new_hw_ptr + delta) % substream->runtime->buffer_size;
  498. atomic_set(&stream->hw_ptr, (int)new_hw_ptr);
  499. stream->out_frames += delta;
  500. if (stream->out_frames > substream->runtime->period_size) {
  501. stream->out_frames %= substream->runtime->period_size;
  502. snd_pcm_period_elapsed(substream);
  503. }
  504. }
  505. static snd_pcm_uframes_t alsa_pointer(struct snd_pcm_substream *substream)
  506. {
  507. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  508. return (snd_pcm_uframes_t)atomic_read(&stream->hw_ptr);
  509. }
  510. static int alsa_pb_copy_user(struct snd_pcm_substream *substream,
  511. int channel, unsigned long pos, void __user *src,
  512. unsigned long count)
  513. {
  514. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  515. if (unlikely(pos + count > stream->buffer_sz))
  516. return -EINVAL;
  517. if (copy_from_user(stream->buffer + pos, src, count))
  518. return -EFAULT;
  519. return xen_snd_front_stream_write(&stream->evt_pair->req, pos, count);
  520. }
  521. static int alsa_pb_copy_kernel(struct snd_pcm_substream *substream,
  522. int channel, unsigned long pos, void *src,
  523. unsigned long count)
  524. {
  525. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  526. if (unlikely(pos + count > stream->buffer_sz))
  527. return -EINVAL;
  528. memcpy(stream->buffer + pos, src, count);
  529. return xen_snd_front_stream_write(&stream->evt_pair->req, pos, count);
  530. }
  531. static int alsa_cap_copy_user(struct snd_pcm_substream *substream,
  532. int channel, unsigned long pos, void __user *dst,
  533. unsigned long count)
  534. {
  535. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  536. int ret;
  537. if (unlikely(pos + count > stream->buffer_sz))
  538. return -EINVAL;
  539. ret = xen_snd_front_stream_read(&stream->evt_pair->req, pos, count);
  540. if (ret < 0)
  541. return ret;
  542. return copy_to_user(dst, stream->buffer + pos, count) ?
  543. -EFAULT : 0;
  544. }
  545. static int alsa_cap_copy_kernel(struct snd_pcm_substream *substream,
  546. int channel, unsigned long pos, void *dst,
  547. unsigned long count)
  548. {
  549. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  550. int ret;
  551. if (unlikely(pos + count > stream->buffer_sz))
  552. return -EINVAL;
  553. ret = xen_snd_front_stream_read(&stream->evt_pair->req, pos, count);
  554. if (ret < 0)
  555. return ret;
  556. memcpy(dst, stream->buffer + pos, count);
  557. return 0;
  558. }
  559. static int alsa_pb_fill_silence(struct snd_pcm_substream *substream,
  560. int channel, unsigned long pos,
  561. unsigned long count)
  562. {
  563. struct xen_snd_front_pcm_stream_info *stream = stream_get(substream);
  564. if (unlikely(pos + count > stream->buffer_sz))
  565. return -EINVAL;
  566. memset(stream->buffer + pos, 0, count);
  567. return xen_snd_front_stream_write(&stream->evt_pair->req, pos, count);
  568. }
  569. /*
  570. * FIXME: The mmaped data transfer is asynchronous and there is no
  571. * ack signal from user-space when it is done. This is the
  572. * reason it is not implemented in the PV driver as we do need
  573. * to know when the buffer can be transferred to the backend.
  574. */
  575. static const struct snd_pcm_ops snd_drv_alsa_playback_ops = {
  576. .open = alsa_open,
  577. .close = alsa_close,
  578. .hw_params = alsa_hw_params,
  579. .hw_free = alsa_hw_free,
  580. .prepare = alsa_prepare,
  581. .trigger = alsa_trigger,
  582. .pointer = alsa_pointer,
  583. .copy_user = alsa_pb_copy_user,
  584. .copy_kernel = alsa_pb_copy_kernel,
  585. .fill_silence = alsa_pb_fill_silence,
  586. };
  587. static const struct snd_pcm_ops snd_drv_alsa_capture_ops = {
  588. .open = alsa_open,
  589. .close = alsa_close,
  590. .hw_params = alsa_hw_params,
  591. .hw_free = alsa_hw_free,
  592. .prepare = alsa_prepare,
  593. .trigger = alsa_trigger,
  594. .pointer = alsa_pointer,
  595. .copy_user = alsa_cap_copy_user,
  596. .copy_kernel = alsa_cap_copy_kernel,
  597. };
  598. static int new_pcm_instance(struct xen_snd_front_card_info *card_info,
  599. struct xen_front_cfg_pcm_instance *instance_cfg,
  600. struct xen_snd_front_pcm_instance_info *pcm_instance_info)
  601. {
  602. struct snd_pcm *pcm;
  603. int ret, i;
  604. dev_dbg(&card_info->front_info->xb_dev->dev,
  605. "New PCM device \"%s\" with id %d playback %d capture %d",
  606. instance_cfg->name,
  607. instance_cfg->device_id,
  608. instance_cfg->num_streams_pb,
  609. instance_cfg->num_streams_cap);
  610. pcm_instance_info->card_info = card_info;
  611. pcm_instance_info->pcm_hw = instance_cfg->pcm_hw;
  612. if (instance_cfg->num_streams_pb) {
  613. pcm_instance_info->streams_pb =
  614. devm_kcalloc(&card_info->card->card_dev,
  615. instance_cfg->num_streams_pb,
  616. sizeof(struct xen_snd_front_pcm_stream_info),
  617. GFP_KERNEL);
  618. if (!pcm_instance_info->streams_pb)
  619. return -ENOMEM;
  620. }
  621. if (instance_cfg->num_streams_cap) {
  622. pcm_instance_info->streams_cap =
  623. devm_kcalloc(&card_info->card->card_dev,
  624. instance_cfg->num_streams_cap,
  625. sizeof(struct xen_snd_front_pcm_stream_info),
  626. GFP_KERNEL);
  627. if (!pcm_instance_info->streams_cap)
  628. return -ENOMEM;
  629. }
  630. pcm_instance_info->num_pcm_streams_pb =
  631. instance_cfg->num_streams_pb;
  632. pcm_instance_info->num_pcm_streams_cap =
  633. instance_cfg->num_streams_cap;
  634. for (i = 0; i < pcm_instance_info->num_pcm_streams_pb; i++) {
  635. pcm_instance_info->streams_pb[i].pcm_hw =
  636. instance_cfg->streams_pb[i].pcm_hw;
  637. pcm_instance_info->streams_pb[i].index =
  638. instance_cfg->streams_pb[i].index;
  639. }
  640. for (i = 0; i < pcm_instance_info->num_pcm_streams_cap; i++) {
  641. pcm_instance_info->streams_cap[i].pcm_hw =
  642. instance_cfg->streams_cap[i].pcm_hw;
  643. pcm_instance_info->streams_cap[i].index =
  644. instance_cfg->streams_cap[i].index;
  645. }
  646. ret = snd_pcm_new(card_info->card, instance_cfg->name,
  647. instance_cfg->device_id,
  648. instance_cfg->num_streams_pb,
  649. instance_cfg->num_streams_cap,
  650. &pcm);
  651. if (ret < 0)
  652. return ret;
  653. pcm->private_data = pcm_instance_info;
  654. pcm->info_flags = 0;
  655. /* we want to handle all PCM operations in non-atomic context */
  656. pcm->nonatomic = true;
  657. strncpy(pcm->name, "Virtual card PCM", sizeof(pcm->name));
  658. if (instance_cfg->num_streams_pb)
  659. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  660. &snd_drv_alsa_playback_ops);
  661. if (instance_cfg->num_streams_cap)
  662. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  663. &snd_drv_alsa_capture_ops);
  664. pcm_instance_info->pcm = pcm;
  665. return 0;
  666. }
  667. int xen_snd_front_alsa_init(struct xen_snd_front_info *front_info)
  668. {
  669. struct device *dev = &front_info->xb_dev->dev;
  670. struct xen_front_cfg_card *cfg = &front_info->cfg;
  671. struct xen_snd_front_card_info *card_info;
  672. struct snd_card *card;
  673. int ret, i;
  674. dev_dbg(dev, "Creating virtual sound card\n");
  675. ret = snd_card_new(dev, 0, XENSND_DRIVER_NAME, THIS_MODULE,
  676. sizeof(struct xen_snd_front_card_info), &card);
  677. if (ret < 0)
  678. return ret;
  679. card_info = card->private_data;
  680. card_info->front_info = front_info;
  681. front_info->card_info = card_info;
  682. card_info->card = card;
  683. card_info->pcm_instances =
  684. devm_kcalloc(dev, cfg->num_pcm_instances,
  685. sizeof(struct xen_snd_front_pcm_instance_info),
  686. GFP_KERNEL);
  687. if (!card_info->pcm_instances) {
  688. ret = -ENOMEM;
  689. goto fail;
  690. }
  691. card_info->num_pcm_instances = cfg->num_pcm_instances;
  692. card_info->pcm_hw = cfg->pcm_hw;
  693. for (i = 0; i < cfg->num_pcm_instances; i++) {
  694. ret = new_pcm_instance(card_info, &cfg->pcm_instances[i],
  695. &card_info->pcm_instances[i]);
  696. if (ret < 0)
  697. goto fail;
  698. }
  699. strncpy(card->driver, XENSND_DRIVER_NAME, sizeof(card->driver));
  700. strncpy(card->shortname, cfg->name_short, sizeof(card->shortname));
  701. strncpy(card->longname, cfg->name_long, sizeof(card->longname));
  702. ret = snd_card_register(card);
  703. if (ret < 0)
  704. goto fail;
  705. return 0;
  706. fail:
  707. snd_card_free(card);
  708. return ret;
  709. }
  710. void xen_snd_front_alsa_fini(struct xen_snd_front_info *front_info)
  711. {
  712. struct xen_snd_front_card_info *card_info;
  713. struct snd_card *card;
  714. card_info = front_info->card_info;
  715. if (!card_info)
  716. return;
  717. card = card_info->card;
  718. if (!card)
  719. return;
  720. dev_dbg(&front_info->xb_dev->dev, "Removing virtual sound card %d\n",
  721. card->number);
  722. snd_card_free(card);
  723. /* Card_info will be freed when destroying front_info->xb_dev->dev. */
  724. card_info->card = NULL;
  725. }