aboutsummaryrefslogtreecommitdiffstats
path: root/alsa-pcm.c
blob: 21287ee5b16def875d131411a2f873c6b952b155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// SPDX-License-Identifier: GPL-2.0
/*
 * ALSA Virtual Soundcard
 *
 * alsa-pcm.c - AVIRT ALSA PCM interface
 *
 * Copyright (C) 2010-2018 Fiberdyne Systems Pty Ltd
 */

#include "alsa-pcm.h"
#include "utils.h"

#define DO_AUDIOPATH_CB(ap, callback, substream, ...)                \
	do {                                                         \
		if (ap->pcm_ops->callback) {                         \
			return ap->pcm_ops->callback((substream),    \
						     ##__VA_ARGS__); \
		}                                                    \
	} while (0)

/**
 * pcm_buff_complete_cb - PCM buffer complete callback
 * @substreamid: pointer to ALSA PCM substream
 * @return 0 on success or error code otherwise
 *
 * This should be called from a child Audio Path once it has finished processing
 * the pcm buffer
 */
int pcm_buff_complete_cb(struct snd_pcm_substream *substream)
{
	// Notify ALSA middle layer of the elapsed period boundary
	snd_pcm_period_elapsed(substream);

	return 0;
}

static struct avirt_stream_group *avirt_stream_get_group(int direction)
{
	switch (direction) {
	case SNDRV_PCM_STREAM_PLAYBACK:
		return &coreinfo.playback;
	case SNDRV_PCM_STREAM_CAPTURE:
		return &coreinfo.capture;
	default:
		pr_err("[%s] Direction must be SNDRV_PCM_STREAM_XXX!",
		       __func__);
		return NULL;
	}
}

/*******************************************************************************
 * ALSA PCM Callbacks
 ******************************************************************************/
/**
 * pcm_open - Implements 'open' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * This is called when an ALSA PCM substream is opened. The substream device is
 * configured here.
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_open(struct snd_pcm_substream *substream)
{
	struct avirt_audiopath *audiopath;
	struct avirt_stream_group *group;
	struct snd_pcm_hardware *hw;
	unsigned int bytes_per_sample = 0, blocksize = 0, chans = 0;

	char *uid = "ap_fddsp"; // TD MF: Make this dynamic!
	audiopath = avirt_audiopath_get(uid);
	CHK_NULL_V(audiopath, "Cannot find Audio Path uid: '%s'!", uid);
	substream->private_data = audiopath;

	blocksize = audiopath->blocksize;

	// Copy the hw params from the audiopath to the pcm
	hw = &substream->runtime->hw;
	memcpy(hw, audiopath->hw, sizeof(struct snd_pcm_hardware));
	pr_info("%s %d %d", __func__, blocksize, hw->periods_max);

	if (hw->formats == SNDRV_PCM_FMTBIT_S16_LE) {
		bytes_per_sample = 2;
	} else {
		pr_err("[%s] PCM only supports SNDRV_PCM_FMTBIT_S16_LE",
		       __func__);
		return -EINVAL;
	}

	// Get device group (playback/capture)
	group = avirt_stream_get_group(substream->stream);
	CHK_NULL(group);

	// Check if substream id is valid
	pr_info("%d substream is < %d", substream->pcm->device, group->devices);
	if (substream->pcm->device >= group->devices) {
		pr_err("%s %d substream id is invalid expecting %d", __func__,
		       substream->pcm->device, group->devices);
		return -1;
	}

	// Setup remaining hw properties
	chans = group->streams[substream->pcm->device].channels;
	hw->channels_min = chans;
	hw->channels_max = chans;
	hw->buffer_bytes_max =
		blocksize * hw->periods_max * bytes_per_sample * chans;
	hw->period_bytes_min = blocksize * bytes_per_sample * chans;
	hw->period_bytes_max = blocksize * bytes_per_sample * chans;

	// Do additional Audio Path 'open' callback
	DO_AUDIOPATH_CB(audiopath, open, substream);

	return 0;
}

/**
 * pcm_close - Implements 'close' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * This is called when a PCM substream is closed.
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_close(struct snd_pcm_substream *substream)
{
	DINFO(AP_LOGNAME, "");
	// Do additional Audio Path 'close' callback
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			close, substream);

	return 0;
}

/**
 * pcm_hw_params - Implements 'hw_params' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @hw_params: contains the hardware parameters for the PCM
 *
 * This is called when the hardware parameters are set, including buffer size,
 * the period size, the format, etc. The buffer allocation should be done here.
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_hw_params(struct snd_pcm_substream *substream,
			 struct snd_pcm_hw_params *hw_params)
{
	int channels, err;
	size_t bufsz;
	struct avirt_audiopath *audiopath;
	struct avirt_stream_group *group;

	group = avirt_stream_get_group(substream->stream);
	CHK_NULL(group);

	channels = group->streams[substream->pcm->device].channels;

	if ((params_channels(hw_params) > channels) ||
	    (params_channels(hw_params) < channels)) {
		pr_err("Requested number of channels not supported.\n");
		return -EINVAL;
	}

	audiopath = ((struct avirt_audiopath *)substream->private_data);
	bufsz = params_buffer_bytes(hw_params) * audiopath->hw->periods_max;

	err = snd_pcm_lib_alloc_vmalloc_buffer(substream, bufsz);
	if (err <= 0) {
		pr_err("pcm: buffer allocation failed (%d)\n", err);
		return err;
	}

	// Do additional Audio Path 'hw_params' callback
	DO_AUDIOPATH_CB(hw_params, substream, hw_params);

	return 0;
}

/**
 * pcm_hw_free - Implements 'hw_free' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * Release the resources allocated via 'hw_params'.
 * This function is always called before the 'close' callback .
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_hw_free(struct snd_pcm_substream *substream)
{
	DINFO(AP_LOGNAME, "");
	CHK_ERR(snd_pcm_lib_free_vmalloc_buffer(substream));

	// Do additional Audio Path 'hw_free' callback
	DO_AUDIOPATH_CB(hw_free, substream);

	return 0;
}

/**
 * pcm_prepare - Implements 'prepare' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * The format rate, sample rate, etc., can be set here. This callback can be
 * called many times at each setup. This function is also used to handle overrun
 * and underrun conditions when we try and resync the DMA (if we're using DMA).
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_prepare(struct snd_pcm_substream *substream)
{
	// Do additional Audio Path 'prepare' callback
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			prepare, substream);

	return 0;
}

/**
 * pcm_trigger - Implements 'trigger' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @cmd: action to be performed (start or stop)
 *
 * This is called when the PCM is started, stopped or paused. The action
 * indicated action is specified in the second argument, SNDRV_PCM_TRIGGER_XXX
 *
 * Returns 0 on success or error code otherwise.
 */
static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		break;
	default:
		pr_err("trigger must be START or STOP");
		return -EINVAL;
	}

	// Do additional Audio Path 'trigger' callback
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			trigger, substream, cmd);

	return 0;
}

/**
 * pcm_pointer - Implements 'pointer' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * This gets called when the user space needs a DMA buffer index. IO errors will
 * be generated if the index does not increment, or drives beyond the frame
 * threshold of the buffer itself.
 *
 * Returns the current hardware buffer frame index.
 */
static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
{
	// Do additional Audio Path 'pointer' callback
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			pointer, substream);

	return 0;
}

/**
 * pcm_pointer - Implements 'get_time_info' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @system_ts
 * @audio_ts
 * @audio_tstamp_config
 * @audio_tstamp_report
 *
 * Generic way to get system timestamp and audio timestamp info
 *
 * Returns 0 on success or error code otherwise
 */
static int pcm_get_time_info(
	struct snd_pcm_substream *substream, struct timespec *system_ts,
	struct timespec *audio_ts,
	struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
	struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
{
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			get_time_info, substream, system_ts, audio_ts,
			audio_tstamp_config, audio_tstamp_report);

	return 0;
}

/**
 * pcm_copy_user - Implements 'copy_user' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @channel:
 * @pos: The offset in the DMA
 * @src: Audio PCM data from the user space
 * @count:
 *
 * This is where we need to copy user audio PCM data into the sound driver
 *
 * Returns 0 on success or error code otherwise.
 *
 */
static int pcm_copy_user(struct snd_pcm_substream *substream, int channel,
			 snd_pcm_uframes_t pos, void __user *src,
			 snd_pcm_uframes_t count)
{
	//struct snd_pcm_runtime *runtime;
	//int offset;

	//runtime = substream->runtime;
	//offset = frames_to_bytes(runtime, pos);

	// Do additional Audio Path 'copy_user' callback
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			copy_user, substream, channel, pos, src, count);

	return 0;
}

/**
 * pcm_copy_kernel - Implements 'copy_kernel' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 * @channel:
 * @pos: The offset in the DMA
 * @src: Audio PCM data from the user space
 * @count:
 *
 * This is where we need to copy kernel audio PCM data into the sound driver
 *
 * Returns 0 on success or error code otherwise.
 *
 */
static int pcm_copy_kernel(struct snd_pcm_substream *substream, int channel,
			   unsigned long pos, void *buf, unsigned long count)
{
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			copy_kernel, substream, channel, pos, buf, count);

	return 0;
}

/**
 * pcm_ack - Implements 'ack' callback for PCM middle layer
 * @substream: pointer to ALSA PCM substream
 *
 * This is where we need to ack
 *
 * Returns 0 on success or error code otherwise.
 *
 */
static int pcm_ack(struct snd_pcm_substream *substream)
{
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			ack, substream);

	return 0;
}

static int pcm_silence(struct snd_pcm_substream *substream, int channel,
		       snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
{
	DO_AUDIOPATH_CB(((struct avirt_audiopath *)substream->private_data),
			fill_silence, substream, channel, pos, count);

	return 0;
}

struct snd_pcm_ops pcm_ops = {
	.open = pcm_open,
	.close = pcm_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = pcm_hw_params,
	.hw_free = pcm_hw_free,
	.prepare = pcm_prepare,
	.trigger = pcm_trigger,
	.pointer = pcm_pointer,
	.get_time_info = pcm_get_time_info,
	.fill_silence = pcm_silence,
	.copy_user = pcm_copy_user,
	.copy_kernel = pcm_copy_kernel,
	.page = snd_pcm_lib_get_vmalloc_page,
	.ack = pcm_ack,
};