1 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 * Xen para-virtual sound device
6 * Copyright (C) 2016-2018 EPAM Systems Inc.
8 * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
11 #include <xen/xenbus.h>
13 #include <xen/interface/io/sndif.h>
15 #include "xen_snd_front.h"
16 #include "xen_snd_front_cfg.h"
18 /* Maximum number of supported streams. */
19 #define VSND_MAX_STREAM 8
21 struct cfg_hw_sample_rate {
27 static const struct cfg_hw_sample_rate CFG_HW_SUPPORTED_RATES[] = {
28 { .name = "5512", .mask = SNDRV_PCM_RATE_5512, .value = 5512 },
29 { .name = "8000", .mask = SNDRV_PCM_RATE_8000, .value = 8000 },
30 { .name = "11025", .mask = SNDRV_PCM_RATE_11025, .value = 11025 },
31 { .name = "16000", .mask = SNDRV_PCM_RATE_16000, .value = 16000 },
32 { .name = "22050", .mask = SNDRV_PCM_RATE_22050, .value = 22050 },
33 { .name = "32000", .mask = SNDRV_PCM_RATE_32000, .value = 32000 },
34 { .name = "44100", .mask = SNDRV_PCM_RATE_44100, .value = 44100 },
35 { .name = "48000", .mask = SNDRV_PCM_RATE_48000, .value = 48000 },
36 { .name = "64000", .mask = SNDRV_PCM_RATE_64000, .value = 64000 },
37 { .name = "96000", .mask = SNDRV_PCM_RATE_96000, .value = 96000 },
38 { .name = "176400", .mask = SNDRV_PCM_RATE_176400, .value = 176400 },
39 { .name = "192000", .mask = SNDRV_PCM_RATE_192000, .value = 192000 },
42 struct cfg_hw_sample_format {
47 static const struct cfg_hw_sample_format CFG_HW_SUPPORTED_FORMATS[] = {
49 .name = XENSND_PCM_FORMAT_U8_STR,
50 .mask = SNDRV_PCM_FMTBIT_U8
53 .name = XENSND_PCM_FORMAT_S8_STR,
54 .mask = SNDRV_PCM_FMTBIT_S8
57 .name = XENSND_PCM_FORMAT_U16_LE_STR,
58 .mask = SNDRV_PCM_FMTBIT_U16_LE
61 .name = XENSND_PCM_FORMAT_U16_BE_STR,
62 .mask = SNDRV_PCM_FMTBIT_U16_BE
65 .name = XENSND_PCM_FORMAT_S16_LE_STR,
66 .mask = SNDRV_PCM_FMTBIT_S16_LE
69 .name = XENSND_PCM_FORMAT_S16_BE_STR,
70 .mask = SNDRV_PCM_FMTBIT_S16_BE
73 .name = XENSND_PCM_FORMAT_U24_LE_STR,
74 .mask = SNDRV_PCM_FMTBIT_U24_LE
77 .name = XENSND_PCM_FORMAT_U24_BE_STR,
78 .mask = SNDRV_PCM_FMTBIT_U24_BE
81 .name = XENSND_PCM_FORMAT_S24_LE_STR,
82 .mask = SNDRV_PCM_FMTBIT_S24_LE
85 .name = XENSND_PCM_FORMAT_S24_BE_STR,
86 .mask = SNDRV_PCM_FMTBIT_S24_BE
89 .name = XENSND_PCM_FORMAT_U32_LE_STR,
90 .mask = SNDRV_PCM_FMTBIT_U32_LE
93 .name = XENSND_PCM_FORMAT_U32_BE_STR,
94 .mask = SNDRV_PCM_FMTBIT_U32_BE
97 .name = XENSND_PCM_FORMAT_S32_LE_STR,
98 .mask = SNDRV_PCM_FMTBIT_S32_LE
101 .name = XENSND_PCM_FORMAT_S32_BE_STR,
102 .mask = SNDRV_PCM_FMTBIT_S32_BE
105 .name = XENSND_PCM_FORMAT_A_LAW_STR,
106 .mask = SNDRV_PCM_FMTBIT_A_LAW
109 .name = XENSND_PCM_FORMAT_MU_LAW_STR,
110 .mask = SNDRV_PCM_FMTBIT_MU_LAW
113 .name = XENSND_PCM_FORMAT_F32_LE_STR,
114 .mask = SNDRV_PCM_FMTBIT_FLOAT_LE
117 .name = XENSND_PCM_FORMAT_F32_BE_STR,
118 .mask = SNDRV_PCM_FMTBIT_FLOAT_BE
121 .name = XENSND_PCM_FORMAT_F64_LE_STR,
122 .mask = SNDRV_PCM_FMTBIT_FLOAT64_LE
125 .name = XENSND_PCM_FORMAT_F64_BE_STR,
126 .mask = SNDRV_PCM_FMTBIT_FLOAT64_BE
129 .name = XENSND_PCM_FORMAT_IEC958_SUBFRAME_LE_STR,
130 .mask = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
133 .name = XENSND_PCM_FORMAT_IEC958_SUBFRAME_BE_STR,
134 .mask = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
137 .name = XENSND_PCM_FORMAT_IMA_ADPCM_STR,
138 .mask = SNDRV_PCM_FMTBIT_IMA_ADPCM
141 .name = XENSND_PCM_FORMAT_MPEG_STR,
142 .mask = SNDRV_PCM_FMTBIT_MPEG
145 .name = XENSND_PCM_FORMAT_GSM_STR,
146 .mask = SNDRV_PCM_FMTBIT_GSM
150 static void cfg_hw_rates(char *list, unsigned int len,
151 const char *path, struct snd_pcm_hardware *pcm_hw)
154 unsigned int cur_mask;
155 unsigned int cur_value;
157 unsigned int rate_min;
158 unsigned int rate_max;
164 while ((cur_rate = strsep(&list, XENSND_LIST_SEPARATOR))) {
165 for (i = 0; i < ARRAY_SIZE(CFG_HW_SUPPORTED_RATES); i++)
166 if (!strncasecmp(cur_rate,
167 CFG_HW_SUPPORTED_RATES[i].name,
168 XENSND_SAMPLE_RATE_MAX_LEN)) {
169 cur_mask = CFG_HW_SUPPORTED_RATES[i].mask;
170 cur_value = CFG_HW_SUPPORTED_RATES[i].value;
172 if (rate_min > cur_value)
173 rate_min = cur_value;
174 if (rate_max < cur_value)
175 rate_max = cur_value;
180 pcm_hw->rates = rates;
181 pcm_hw->rate_min = rate_min;
182 pcm_hw->rate_max = rate_max;
186 static void cfg_formats(char *list, unsigned int len,
187 const char *path, struct snd_pcm_hardware *pcm_hw)
194 while ((cur_format = strsep(&list, XENSND_LIST_SEPARATOR))) {
195 for (i = 0; i < ARRAY_SIZE(CFG_HW_SUPPORTED_FORMATS); i++)
196 if (!strncasecmp(cur_format,
197 CFG_HW_SUPPORTED_FORMATS[i].name,
198 XENSND_SAMPLE_FORMAT_MAX_LEN))
199 formats |= CFG_HW_SUPPORTED_FORMATS[i].mask;
203 pcm_hw->formats = formats;
206 #define MAX_BUFFER_SIZE (64 * 1024)
207 #define MIN_PERIOD_SIZE 64
208 #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
209 #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | \
210 SNDRV_PCM_FMTBIT_S16_LE)
211 #define USE_RATE (SNDRV_PCM_RATE_CONTINUOUS | \
212 SNDRV_PCM_RATE_8000_48000)
213 #define USE_RATE_MIN 5512
214 #define USE_RATE_MAX 48000
215 #define USE_CHANNELS_MIN 1
216 #define USE_CHANNELS_MAX 2
217 #define USE_PERIODS_MIN 2
218 #define USE_PERIODS_MAX (MAX_BUFFER_SIZE / MIN_PERIOD_SIZE)
220 static const struct snd_pcm_hardware SND_DRV_PCM_HW_DEFAULT = {
221 .info = (SNDRV_PCM_INFO_MMAP |
222 SNDRV_PCM_INFO_INTERLEAVED |
223 SNDRV_PCM_INFO_RESUME |
224 SNDRV_PCM_INFO_MMAP_VALID),
225 .formats = USE_FORMATS,
227 .rate_min = USE_RATE_MIN,
228 .rate_max = USE_RATE_MAX,
229 .channels_min = USE_CHANNELS_MIN,
230 .channels_max = USE_CHANNELS_MAX,
231 .buffer_bytes_max = MAX_BUFFER_SIZE,
232 .period_bytes_min = MIN_PERIOD_SIZE,
233 .period_bytes_max = MAX_PERIOD_SIZE,
234 .periods_min = USE_PERIODS_MIN,
235 .periods_max = USE_PERIODS_MAX,
239 static void cfg_read_pcm_hw(const char *path,
240 struct snd_pcm_hardware *parent_pcm_hw,
241 struct snd_pcm_hardware *pcm_hw)
248 /* Inherit parent's PCM HW and read overrides from XenStore. */
250 *pcm_hw = *parent_pcm_hw;
252 *pcm_hw = SND_DRV_PCM_HW_DEFAULT;
254 val = xenbus_read_unsigned(path, XENSND_FIELD_CHANNELS_MIN, 0);
256 pcm_hw->channels_min = val;
258 val = xenbus_read_unsigned(path, XENSND_FIELD_CHANNELS_MAX, 0);
260 pcm_hw->channels_max = val;
262 list = xenbus_read(XBT_NIL, path, XENSND_FIELD_SAMPLE_RATES, &len);
264 cfg_hw_rates(list, len, path, pcm_hw);
268 list = xenbus_read(XBT_NIL, path, XENSND_FIELD_SAMPLE_FORMATS, &len);
270 cfg_formats(list, len, path, pcm_hw);
274 buf_sz = xenbus_read_unsigned(path, XENSND_FIELD_BUFFER_SIZE, 0);
276 pcm_hw->buffer_bytes_max = buf_sz;
278 /* Update configuration to match new values. */
279 if (pcm_hw->channels_min > pcm_hw->channels_max)
280 pcm_hw->channels_min = pcm_hw->channels_max;
282 if (pcm_hw->rate_min > pcm_hw->rate_max)
283 pcm_hw->rate_min = pcm_hw->rate_max;
285 pcm_hw->period_bytes_max = pcm_hw->buffer_bytes_max;
287 pcm_hw->periods_max = pcm_hw->period_bytes_max /
288 pcm_hw->period_bytes_min;
291 static int cfg_get_stream_type(const char *path, int index,
292 int *num_pb, int *num_cap)
300 stream_path = kasprintf(GFP_KERNEL, "%s/%d", path, index);
306 str = xenbus_read(XBT_NIL, stream_path, XENSND_FIELD_TYPE, NULL);
313 if (!strncasecmp(str, XENSND_STREAM_TYPE_PLAYBACK,
314 sizeof(XENSND_STREAM_TYPE_PLAYBACK))) {
316 } else if (!strncasecmp(str, XENSND_STREAM_TYPE_CAPTURE,
317 sizeof(XENSND_STREAM_TYPE_CAPTURE))) {
331 static int cfg_stream(struct xen_snd_front_info *front_info,
332 struct xen_front_cfg_pcm_instance *pcm_instance,
333 const char *path, int index, int *cur_pb, int *cur_cap,
338 struct xen_front_cfg_stream *stream;
341 stream_path = devm_kasprintf(&front_info->xb_dev->dev,
342 GFP_KERNEL, "%s/%d", path, index);
348 str = xenbus_read(XBT_NIL, stream_path, XENSND_FIELD_TYPE, NULL);
355 if (!strncasecmp(str, XENSND_STREAM_TYPE_PLAYBACK,
356 sizeof(XENSND_STREAM_TYPE_PLAYBACK))) {
357 stream = &pcm_instance->streams_pb[(*cur_pb)++];
358 } else if (!strncasecmp(str, XENSND_STREAM_TYPE_CAPTURE,
359 sizeof(XENSND_STREAM_TYPE_CAPTURE))) {
360 stream = &pcm_instance->streams_cap[(*cur_cap)++];
366 /* Get next stream index. */
367 stream->index = (*stream_cnt)++;
368 stream->xenstore_path = stream_path;
370 * Check XenStore if PCM HW configuration exists for this stream
371 * and update if so, e.g. we inherit all values from device's PCM HW,
372 * but can still override some of the values for the stream.
374 cfg_read_pcm_hw(stream->xenstore_path,
375 &pcm_instance->pcm_hw, &stream->pcm_hw);
383 static int cfg_device(struct xen_snd_front_info *front_info,
384 struct xen_front_cfg_pcm_instance *pcm_instance,
385 struct snd_pcm_hardware *parent_pcm_hw,
386 const char *path, int node_index, int *stream_cnt)
390 int ret, i, num_streams;
395 device_path = kasprintf(GFP_KERNEL, "%s/%d", path, node_index);
399 str = xenbus_read(XBT_NIL, device_path, XENSND_FIELD_DEVICE_NAME, NULL);
401 strscpy(pcm_instance->name, str, sizeof(pcm_instance->name));
405 pcm_instance->device_id = node_index;
408 * Check XenStore if PCM HW configuration exists for this device
409 * and update if so, e.g. we inherit all values from card's PCM HW,
410 * but can still override some of the values for the device.
412 cfg_read_pcm_hw(device_path, parent_pcm_hw, &pcm_instance->pcm_hw);
414 /* Find out how many streams were configured in Xen store. */
417 snprintf(node, sizeof(node), "%d", num_streams);
418 if (!xenbus_exists(XBT_NIL, device_path, node))
422 } while (num_streams < VSND_MAX_STREAM);
424 pcm_instance->num_streams_pb = 0;
425 pcm_instance->num_streams_cap = 0;
426 /* Get number of playback and capture streams. */
427 for (i = 0; i < num_streams; i++) {
428 ret = cfg_get_stream_type(device_path, i, &num_pb, &num_cap);
432 pcm_instance->num_streams_pb += num_pb;
433 pcm_instance->num_streams_cap += num_cap;
436 if (pcm_instance->num_streams_pb) {
437 pcm_instance->streams_pb =
438 devm_kcalloc(&front_info->xb_dev->dev,
439 pcm_instance->num_streams_pb,
440 sizeof(struct xen_front_cfg_stream),
442 if (!pcm_instance->streams_pb) {
448 if (pcm_instance->num_streams_cap) {
449 pcm_instance->streams_cap =
450 devm_kcalloc(&front_info->xb_dev->dev,
451 pcm_instance->num_streams_cap,
452 sizeof(struct xen_front_cfg_stream),
454 if (!pcm_instance->streams_cap) {
462 for (i = 0; i < num_streams; i++) {
463 ret = cfg_stream(front_info, pcm_instance, device_path, i,
464 &cur_pb, &cur_cap, stream_cnt);
475 int xen_snd_front_cfg_card(struct xen_snd_front_info *front_info,
478 struct xenbus_device *xb_dev = front_info->xb_dev;
479 struct xen_front_cfg_card *cfg = &front_info->cfg;
480 int ret, num_devices, i;
486 scnprintf(node, sizeof(node), "%d", num_devices);
487 if (!xenbus_exists(XBT_NIL, xb_dev->nodename, node))
491 } while (num_devices < SNDRV_PCM_DEVICES);
494 dev_warn(&xb_dev->dev,
495 "No devices configured for sound card at %s\n",
500 /* Start from default PCM HW configuration for the card. */
501 cfg_read_pcm_hw(xb_dev->nodename, NULL, &cfg->pcm_hw);
504 devm_kcalloc(&front_info->xb_dev->dev, num_devices,
505 sizeof(struct xen_front_cfg_pcm_instance),
507 if (!cfg->pcm_instances)
510 for (i = 0; i < num_devices; i++) {
511 ret = cfg_device(front_info, &cfg->pcm_instances[i],
512 &cfg->pcm_hw, xb_dev->nodename, i, stream_cnt);
516 cfg->num_pcm_instances = num_devices;