aboutsummaryrefslogtreecommitdiffstats
path: root/pcm.c
blob: ff1b5fc4ee8409f724a3f7350b8734fcbe8d3a80 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * AVIRT - ALSA Virtual Soundcard
 *
 * Copyright (c) 2010-2018 Fiberdyne Systems Pty Ltd
 *
 * pcm.c - AVIRT PCM interface
 */

#include "core.h"

#define D_LOGNAME "pcm"

#define D_INFOK(fmt, args...) DINFO(D_LOGNAME, fmt, ##args)
#define D_PRINTK(fmt, args...) DDEBUG(D_LOGNAME, fmt, ##args)
#define D_ERRORK(fmt, args...) DERROR(D_LOGNAME, fmt, ##args)

/**
 * DO_AUDIOPATH_CB - Used to call into an Audio Path's ALSA PCM callback from
 * AVIRT, where AVIRT's own callback must be attended to first.
 */
#define DO_AUDIOPATH_CB(ap, callback, substream, ...)                          \
	((!substream->stream) ?                                                \
		 (((ap)->pcm_playback_ops->callback) ?                         \
			  (ap)->pcm_playback_ops->callback((substream),        \
							   ##__VA_ARGS__) :    \
			  0) :                                                 \
		 (((ap)->pcm_capture_ops->callback) ?                          \
			  (ap)->pcm_capture_ops->callback((substream),         \
							  ##__VA_ARGS__) :     \
			  0))

#define PRIVATE_DATA(substream)                                                \
	((struct snd_avirt_private_data *)substream->private_data)

/**
 * snd_avirt_pcm_period_elapsed - PCM buffer complete callback
 * @substreamid: pointer to ALSA PCM substream
 *
 * This should be called from a child Audio Path once it has finished
 * processing the pcm buffer
 */
void snd_avirt_pcm_period_elapsed(struct snd_pcm_substream *substream)
{
	// Notify ALSA middle layer of the elapsed period boundary
	snd_pcm_period_elapsed(substream);
}
EXPORT_SYMBOL_GPL(snd_avirt_pcm_period_elapsed);

/*******************************************************************************
 * 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 snd_avirt_private_data *avirt_private_data;
	struct snd_avirt_audiopath *audiopath;
	struct snd_avirt_stream *stream;
	struct snd_pcm_hardware *hw;
	unsigned int chans = 0;

	// Find the Audio Path mapped to this device
	stream = snd_avirt_stream_find_by_device(substream->pcm->device);
	if (IS_ERR_VALUE(stream) || !stream)
		return PTR_ERR(stream);
	audiopath = snd_avirt_audiopath_get(stream->map);
	CHK_NULL_V(audiopath, "Cannot find Audio Path uid: '%s'!", stream->map);

	// Set the private data's Audio Path
	avirt_private_data = substream->private_data;
	avirt_private_data->audiopath = audiopath;

	// Copy the hw params from the audiopath to the pcm
	hw = &substream->runtime->hw;
	memcpy(hw, audiopath->hw, sizeof(struct snd_pcm_hardware));

	// Setup remaining hw properties
	chans = stream->channels;
	hw->channels_min = chans;
	hw->channels_max = chans;

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

/**
 * 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)
{
	// Do additional Audio Path 'close' callback
	return DO_AUDIOPATH_CB(
		(struct snd_avirt_audiopath *)PRIVATE_DATA(substream)->audiopath,
		close, substream);
}

/**
 * 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 retval;

	/* Check supported channels */
	if ((params_channels(hw_params) <
	     substream->runtime->hw.channels_min) ||
	    (params_channels(hw_params) >
	     substream->runtime->hw.channels_max)) {
		D_ERRORK("Requested number of channels: %d not supported",
			 params_channels(hw_params));
		return -EINVAL;
	}

	retval = snd_pcm_lib_alloc_vmalloc_buffer(
		substream, params_buffer_bytes(hw_params));
	if (retval < 0)
		D_ERRORK("pcm: buffer allocation failed: %d", retval);

	return retval;
}

/**
 * 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)
{
	int err;

	// Do additional Audio Path 'hw_free' callback
	err = DO_AUDIOPATH_CB(
		(struct snd_avirt_audiopath *)PRIVATE_DATA(substream)->audiopath,
		hw_free, substream);
	if (err < 0)
		return err;

	return snd_pcm_lib_free_vmalloc_buffer(substream);
}

/**
 * Default PCM ops table. Selected PCM ops must pass through AVIRT before
 * calling back to their respective Audio Paths
 */
struct snd_pcm_ops pcm_ops_avirt = {
	.open = pcm_open,
	.close = pcm_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = pcm_hw_params,
	.hw_free = pcm_hw_free,
	.page = snd_pcm_lib_get_vmalloc_page,
};