xen_snd_front_cfg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 <xen/xenbus.h>
  10. #include <xen/interface/io/sndif.h>
  11. #include "xen_snd_front.h"
  12. #include "xen_snd_front_cfg.h"
  13. /* Maximum number of supported streams. */
  14. #define VSND_MAX_STREAM 8
  15. struct cfg_hw_sample_rate {
  16. const char *name;
  17. unsigned int mask;
  18. unsigned int value;
  19. };
  20. static const struct cfg_hw_sample_rate CFG_HW_SUPPORTED_RATES[] = {
  21. { .name = "5512", .mask = SNDRV_PCM_RATE_5512, .value = 5512 },
  22. { .name = "8000", .mask = SNDRV_PCM_RATE_8000, .value = 8000 },
  23. { .name = "11025", .mask = SNDRV_PCM_RATE_11025, .value = 11025 },
  24. { .name = "16000", .mask = SNDRV_PCM_RATE_16000, .value = 16000 },
  25. { .name = "22050", .mask = SNDRV_PCM_RATE_22050, .value = 22050 },
  26. { .name = "32000", .mask = SNDRV_PCM_RATE_32000, .value = 32000 },
  27. { .name = "44100", .mask = SNDRV_PCM_RATE_44100, .value = 44100 },
  28. { .name = "48000", .mask = SNDRV_PCM_RATE_48000, .value = 48000 },
  29. { .name = "64000", .mask = SNDRV_PCM_RATE_64000, .value = 64000 },
  30. { .name = "96000", .mask = SNDRV_PCM_RATE_96000, .value = 96000 },
  31. { .name = "176400", .mask = SNDRV_PCM_RATE_176400, .value = 176400 },
  32. { .name = "192000", .mask = SNDRV_PCM_RATE_192000, .value = 192000 },
  33. };
  34. struct cfg_hw_sample_format {
  35. const char *name;
  36. u64 mask;
  37. };
  38. static const struct cfg_hw_sample_format CFG_HW_SUPPORTED_FORMATS[] = {
  39. {
  40. .name = XENSND_PCM_FORMAT_U8_STR,
  41. .mask = SNDRV_PCM_FMTBIT_U8
  42. },
  43. {
  44. .name = XENSND_PCM_FORMAT_S8_STR,
  45. .mask = SNDRV_PCM_FMTBIT_S8
  46. },
  47. {
  48. .name = XENSND_PCM_FORMAT_U16_LE_STR,
  49. .mask = SNDRV_PCM_FMTBIT_U16_LE
  50. },
  51. {
  52. .name = XENSND_PCM_FORMAT_U16_BE_STR,
  53. .mask = SNDRV_PCM_FMTBIT_U16_BE
  54. },
  55. {
  56. .name = XENSND_PCM_FORMAT_S16_LE_STR,
  57. .mask = SNDRV_PCM_FMTBIT_S16_LE
  58. },
  59. {
  60. .name = XENSND_PCM_FORMAT_S16_BE_STR,
  61. .mask = SNDRV_PCM_FMTBIT_S16_BE
  62. },
  63. {
  64. .name = XENSND_PCM_FORMAT_U24_LE_STR,
  65. .mask = SNDRV_PCM_FMTBIT_U24_LE
  66. },
  67. {
  68. .name = XENSND_PCM_FORMAT_U24_BE_STR,
  69. .mask = SNDRV_PCM_FMTBIT_U24_BE
  70. },
  71. {
  72. .name = XENSND_PCM_FORMAT_S24_LE_STR,
  73. .mask = SNDRV_PCM_FMTBIT_S24_LE
  74. },
  75. {
  76. .name = XENSND_PCM_FORMAT_S24_BE_STR,
  77. .mask = SNDRV_PCM_FMTBIT_S24_BE
  78. },
  79. {
  80. .name = XENSND_PCM_FORMAT_U32_LE_STR,
  81. .mask = SNDRV_PCM_FMTBIT_U32_LE
  82. },
  83. {
  84. .name = XENSND_PCM_FORMAT_U32_BE_STR,
  85. .mask = SNDRV_PCM_FMTBIT_U32_BE
  86. },
  87. {
  88. .name = XENSND_PCM_FORMAT_S32_LE_STR,
  89. .mask = SNDRV_PCM_FMTBIT_S32_LE
  90. },
  91. {
  92. .name = XENSND_PCM_FORMAT_S32_BE_STR,
  93. .mask = SNDRV_PCM_FMTBIT_S32_BE
  94. },
  95. {
  96. .name = XENSND_PCM_FORMAT_A_LAW_STR,
  97. .mask = SNDRV_PCM_FMTBIT_A_LAW
  98. },
  99. {
  100. .name = XENSND_PCM_FORMAT_MU_LAW_STR,
  101. .mask = SNDRV_PCM_FMTBIT_MU_LAW
  102. },
  103. {
  104. .name = XENSND_PCM_FORMAT_F32_LE_STR,
  105. .mask = SNDRV_PCM_FMTBIT_FLOAT_LE
  106. },
  107. {
  108. .name = XENSND_PCM_FORMAT_F32_BE_STR,
  109. .mask = SNDRV_PCM_FMTBIT_FLOAT_BE
  110. },
  111. {
  112. .name = XENSND_PCM_FORMAT_F64_LE_STR,
  113. .mask = SNDRV_PCM_FMTBIT_FLOAT64_LE
  114. },
  115. {
  116. .name = XENSND_PCM_FORMAT_F64_BE_STR,
  117. .mask = SNDRV_PCM_FMTBIT_FLOAT64_BE
  118. },
  119. {
  120. .name = XENSND_PCM_FORMAT_IEC958_SUBFRAME_LE_STR,
  121. .mask = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  122. },
  123. {
  124. .name = XENSND_PCM_FORMAT_IEC958_SUBFRAME_BE_STR,
  125. .mask = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
  126. },
  127. {
  128. .name = XENSND_PCM_FORMAT_IMA_ADPCM_STR,
  129. .mask = SNDRV_PCM_FMTBIT_IMA_ADPCM
  130. },
  131. {
  132. .name = XENSND_PCM_FORMAT_MPEG_STR,
  133. .mask = SNDRV_PCM_FMTBIT_MPEG
  134. },
  135. {
  136. .name = XENSND_PCM_FORMAT_GSM_STR,
  137. .mask = SNDRV_PCM_FMTBIT_GSM
  138. },
  139. };
  140. static void cfg_hw_rates(char *list, unsigned int len,
  141. const char *path, struct snd_pcm_hardware *pcm_hw)
  142. {
  143. char *cur_rate;
  144. unsigned int cur_mask;
  145. unsigned int cur_value;
  146. unsigned int rates;
  147. unsigned int rate_min;
  148. unsigned int rate_max;
  149. int i;
  150. rates = 0;
  151. rate_min = -1;
  152. rate_max = 0;
  153. while ((cur_rate = strsep(&list, XENSND_LIST_SEPARATOR))) {
  154. for (i = 0; i < ARRAY_SIZE(CFG_HW_SUPPORTED_RATES); i++)
  155. if (!strncasecmp(cur_rate,
  156. CFG_HW_SUPPORTED_RATES[i].name,
  157. XENSND_SAMPLE_RATE_MAX_LEN)) {
  158. cur_mask = CFG_HW_SUPPORTED_RATES[i].mask;
  159. cur_value = CFG_HW_SUPPORTED_RATES[i].value;
  160. rates |= cur_mask;
  161. if (rate_min > cur_value)
  162. rate_min = cur_value;
  163. if (rate_max < cur_value)
  164. rate_max = cur_value;
  165. }
  166. }
  167. if (rates) {
  168. pcm_hw->rates = rates;
  169. pcm_hw->rate_min = rate_min;
  170. pcm_hw->rate_max = rate_max;
  171. }
  172. }
  173. static void cfg_formats(char *list, unsigned int len,
  174. const char *path, struct snd_pcm_hardware *pcm_hw)
  175. {
  176. u64 formats;
  177. char *cur_format;
  178. int i;
  179. formats = 0;
  180. while ((cur_format = strsep(&list, XENSND_LIST_SEPARATOR))) {
  181. for (i = 0; i < ARRAY_SIZE(CFG_HW_SUPPORTED_FORMATS); i++)
  182. if (!strncasecmp(cur_format,
  183. CFG_HW_SUPPORTED_FORMATS[i].name,
  184. XENSND_SAMPLE_FORMAT_MAX_LEN))
  185. formats |= CFG_HW_SUPPORTED_FORMATS[i].mask;
  186. }
  187. if (formats)
  188. pcm_hw->formats = formats;
  189. }
  190. #define MAX_BUFFER_SIZE (64 * 1024)
  191. #define MIN_PERIOD_SIZE 64
  192. #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
  193. #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | \
  194. SNDRV_PCM_FMTBIT_S16_LE)
  195. #define USE_RATE (SNDRV_PCM_RATE_CONTINUOUS | \
  196. SNDRV_PCM_RATE_8000_48000)
  197. #define USE_RATE_MIN 5512
  198. #define USE_RATE_MAX 48000
  199. #define USE_CHANNELS_MIN 1
  200. #define USE_CHANNELS_MAX 2
  201. #define USE_PERIODS_MIN 2
  202. #define USE_PERIODS_MAX (MAX_BUFFER_SIZE / MIN_PERIOD_SIZE)
  203. static const struct snd_pcm_hardware SND_DRV_PCM_HW_DEFAULT = {
  204. .info = (SNDRV_PCM_INFO_MMAP |
  205. SNDRV_PCM_INFO_INTERLEAVED |
  206. SNDRV_PCM_INFO_RESUME |
  207. SNDRV_PCM_INFO_MMAP_VALID),
  208. .formats = USE_FORMATS,
  209. .rates = USE_RATE,
  210. .rate_min = USE_RATE_MIN,
  211. .rate_max = USE_RATE_MAX,
  212. .channels_min = USE_CHANNELS_MIN,
  213. .channels_max = USE_CHANNELS_MAX,
  214. .buffer_bytes_max = MAX_BUFFER_SIZE,
  215. .period_bytes_min = MIN_PERIOD_SIZE,
  216. .period_bytes_max = MAX_PERIOD_SIZE,
  217. .periods_min = USE_PERIODS_MIN,
  218. .periods_max = USE_PERIODS_MAX,
  219. .fifo_size = 0,
  220. };
  221. static void cfg_read_pcm_hw(const char *path,
  222. struct snd_pcm_hardware *parent_pcm_hw,
  223. struct snd_pcm_hardware *pcm_hw)
  224. {
  225. char *list;
  226. int val;
  227. size_t buf_sz;
  228. unsigned int len;
  229. /* Inherit parent's PCM HW and read overrides from XenStore. */
  230. if (parent_pcm_hw)
  231. *pcm_hw = *parent_pcm_hw;
  232. else
  233. *pcm_hw = SND_DRV_PCM_HW_DEFAULT;
  234. val = xenbus_read_unsigned(path, XENSND_FIELD_CHANNELS_MIN, 0);
  235. if (val)
  236. pcm_hw->channels_min = val;
  237. val = xenbus_read_unsigned(path, XENSND_FIELD_CHANNELS_MAX, 0);
  238. if (val)
  239. pcm_hw->channels_max = val;
  240. list = xenbus_read(XBT_NIL, path, XENSND_FIELD_SAMPLE_RATES, &len);
  241. if (!IS_ERR(list)) {
  242. cfg_hw_rates(list, len, path, pcm_hw);
  243. kfree(list);
  244. }
  245. list = xenbus_read(XBT_NIL, path, XENSND_FIELD_SAMPLE_FORMATS, &len);
  246. if (!IS_ERR(list)) {
  247. cfg_formats(list, len, path, pcm_hw);
  248. kfree(list);
  249. }
  250. buf_sz = xenbus_read_unsigned(path, XENSND_FIELD_BUFFER_SIZE, 0);
  251. if (buf_sz)
  252. pcm_hw->buffer_bytes_max = buf_sz;
  253. /* Update configuration to match new values. */
  254. if (pcm_hw->channels_min > pcm_hw->channels_max)
  255. pcm_hw->channels_min = pcm_hw->channels_max;
  256. if (pcm_hw->rate_min > pcm_hw->rate_max)
  257. pcm_hw->rate_min = pcm_hw->rate_max;
  258. pcm_hw->period_bytes_max = pcm_hw->buffer_bytes_max;
  259. pcm_hw->periods_max = pcm_hw->period_bytes_max /
  260. pcm_hw->period_bytes_min;
  261. }
  262. static int cfg_get_stream_type(const char *path, int index,
  263. int *num_pb, int *num_cap)
  264. {
  265. char *str = NULL;
  266. char *stream_path;
  267. int ret;
  268. *num_pb = 0;
  269. *num_cap = 0;
  270. stream_path = kasprintf(GFP_KERNEL, "%s/%d", path, index);
  271. if (!stream_path) {
  272. ret = -ENOMEM;
  273. goto fail;
  274. }
  275. str = xenbus_read(XBT_NIL, stream_path, XENSND_FIELD_TYPE, NULL);
  276. if (IS_ERR(str)) {
  277. ret = PTR_ERR(str);
  278. str = NULL;
  279. goto fail;
  280. }
  281. if (!strncasecmp(str, XENSND_STREAM_TYPE_PLAYBACK,
  282. sizeof(XENSND_STREAM_TYPE_PLAYBACK))) {
  283. (*num_pb)++;
  284. } else if (!strncasecmp(str, XENSND_STREAM_TYPE_CAPTURE,
  285. sizeof(XENSND_STREAM_TYPE_CAPTURE))) {
  286. (*num_cap)++;
  287. } else {
  288. ret = -EINVAL;
  289. goto fail;
  290. }
  291. ret = 0;
  292. fail:
  293. kfree(stream_path);
  294. kfree(str);
  295. return ret;
  296. }
  297. static int cfg_stream(struct xen_snd_front_info *front_info,
  298. struct xen_front_cfg_pcm_instance *pcm_instance,
  299. const char *path, int index, int *cur_pb, int *cur_cap,
  300. int *stream_cnt)
  301. {
  302. char *str = NULL;
  303. char *stream_path;
  304. struct xen_front_cfg_stream *stream;
  305. int ret;
  306. stream_path = devm_kasprintf(&front_info->xb_dev->dev,
  307. GFP_KERNEL, "%s/%d", path, index);
  308. if (!stream_path) {
  309. ret = -ENOMEM;
  310. goto fail;
  311. }
  312. str = xenbus_read(XBT_NIL, stream_path, XENSND_FIELD_TYPE, NULL);
  313. if (IS_ERR(str)) {
  314. ret = PTR_ERR(str);
  315. str = NULL;
  316. goto fail;
  317. }
  318. if (!strncasecmp(str, XENSND_STREAM_TYPE_PLAYBACK,
  319. sizeof(XENSND_STREAM_TYPE_PLAYBACK))) {
  320. stream = &pcm_instance->streams_pb[(*cur_pb)++];
  321. } else if (!strncasecmp(str, XENSND_STREAM_TYPE_CAPTURE,
  322. sizeof(XENSND_STREAM_TYPE_CAPTURE))) {
  323. stream = &pcm_instance->streams_cap[(*cur_cap)++];
  324. } else {
  325. ret = -EINVAL;
  326. goto fail;
  327. }
  328. /* Get next stream index. */
  329. stream->index = (*stream_cnt)++;
  330. stream->xenstore_path = stream_path;
  331. /*
  332. * Check XenStore if PCM HW configuration exists for this stream
  333. * and update if so, e.g. we inherit all values from device's PCM HW,
  334. * but can still override some of the values for the stream.
  335. */
  336. cfg_read_pcm_hw(stream->xenstore_path,
  337. &pcm_instance->pcm_hw, &stream->pcm_hw);
  338. ret = 0;
  339. fail:
  340. kfree(str);
  341. return ret;
  342. }
  343. static int cfg_device(struct xen_snd_front_info *front_info,
  344. struct xen_front_cfg_pcm_instance *pcm_instance,
  345. struct snd_pcm_hardware *parent_pcm_hw,
  346. const char *path, int node_index, int *stream_cnt)
  347. {
  348. char *str;
  349. char *device_path;
  350. int ret, i, num_streams;
  351. int num_pb, num_cap;
  352. int cur_pb, cur_cap;
  353. char node[3];
  354. device_path = kasprintf(GFP_KERNEL, "%s/%d", path, node_index);
  355. if (!device_path)
  356. return -ENOMEM;
  357. str = xenbus_read(XBT_NIL, device_path, XENSND_FIELD_DEVICE_NAME, NULL);
  358. if (!IS_ERR(str)) {
  359. strlcpy(pcm_instance->name, str, sizeof(pcm_instance->name));
  360. kfree(str);
  361. }
  362. pcm_instance->device_id = node_index;
  363. /*
  364. * Check XenStore if PCM HW configuration exists for this device
  365. * and update if so, e.g. we inherit all values from card's PCM HW,
  366. * but can still override some of the values for the device.
  367. */
  368. cfg_read_pcm_hw(device_path, parent_pcm_hw, &pcm_instance->pcm_hw);
  369. /* Find out how many streams were configured in Xen store. */
  370. num_streams = 0;
  371. do {
  372. snprintf(node, sizeof(node), "%d", num_streams);
  373. if (!xenbus_exists(XBT_NIL, device_path, node))
  374. break;
  375. num_streams++;
  376. } while (num_streams < VSND_MAX_STREAM);
  377. pcm_instance->num_streams_pb = 0;
  378. pcm_instance->num_streams_cap = 0;
  379. /* Get number of playback and capture streams. */
  380. for (i = 0; i < num_streams; i++) {
  381. ret = cfg_get_stream_type(device_path, i, &num_pb, &num_cap);
  382. if (ret < 0)
  383. goto fail;
  384. pcm_instance->num_streams_pb += num_pb;
  385. pcm_instance->num_streams_cap += num_cap;
  386. }
  387. if (pcm_instance->num_streams_pb) {
  388. pcm_instance->streams_pb =
  389. devm_kcalloc(&front_info->xb_dev->dev,
  390. pcm_instance->num_streams_pb,
  391. sizeof(struct xen_front_cfg_stream),
  392. GFP_KERNEL);
  393. if (!pcm_instance->streams_pb) {
  394. ret = -ENOMEM;
  395. goto fail;
  396. }
  397. }
  398. if (pcm_instance->num_streams_cap) {
  399. pcm_instance->streams_cap =
  400. devm_kcalloc(&front_info->xb_dev->dev,
  401. pcm_instance->num_streams_cap,
  402. sizeof(struct xen_front_cfg_stream),
  403. GFP_KERNEL);
  404. if (!pcm_instance->streams_cap) {
  405. ret = -ENOMEM;
  406. goto fail;
  407. }
  408. }
  409. cur_pb = 0;
  410. cur_cap = 0;
  411. for (i = 0; i < num_streams; i++) {
  412. ret = cfg_stream(front_info, pcm_instance, device_path, i,
  413. &cur_pb, &cur_cap, stream_cnt);
  414. if (ret < 0)
  415. goto fail;
  416. }
  417. ret = 0;
  418. fail:
  419. kfree(device_path);
  420. return ret;
  421. }
  422. int xen_snd_front_cfg_card(struct xen_snd_front_info *front_info,
  423. int *stream_cnt)
  424. {
  425. struct xenbus_device *xb_dev = front_info->xb_dev;
  426. struct xen_front_cfg_card *cfg = &front_info->cfg;
  427. int ret, num_devices, i;
  428. char node[3];
  429. *stream_cnt = 0;
  430. num_devices = 0;
  431. do {
  432. snprintf(node, sizeof(node), "%d", num_devices);
  433. if (!xenbus_exists(XBT_NIL, xb_dev->nodename, node))
  434. break;
  435. num_devices++;
  436. } while (num_devices < SNDRV_PCM_DEVICES);
  437. if (!num_devices) {
  438. dev_warn(&xb_dev->dev,
  439. "No devices configured for sound card at %s\n",
  440. xb_dev->nodename);
  441. return -ENODEV;
  442. }
  443. /* Start from default PCM HW configuration for the card. */
  444. cfg_read_pcm_hw(xb_dev->nodename, NULL, &cfg->pcm_hw);
  445. cfg->pcm_instances =
  446. devm_kcalloc(&front_info->xb_dev->dev, num_devices,
  447. sizeof(struct xen_front_cfg_pcm_instance),
  448. GFP_KERNEL);
  449. if (!cfg->pcm_instances)
  450. return -ENOMEM;
  451. for (i = 0; i < num_devices; i++) {
  452. ret = cfg_device(front_info, &cfg->pcm_instances[i],
  453. &cfg->pcm_hw, xb_dev->nodename, i, stream_cnt);
  454. if (ret < 0)
  455. return ret;
  456. }
  457. cfg->num_pcm_instances = num_devices;
  458. return 0;
  459. }