summaryrefslogtreecommitdiffstats
path: root/alsa.c
blob: 045624bab3aa8ca54eea4d083fa40e5b9c54de59 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * ALSA Virtual Soundcard
 *
 * alsa.c - ALSA PCM driver for virtual ALSA card
 *
 * Copyright (C) 2010-2018 Fiberdyne Systems Pty Ltd
 */

#include <linux/slab.h>
#include <sound/core.h>
#include <sound/initval.h>

#include "alsa.h"
#include "core.h"

static struct avirt_alsa_driver *_driver = NULL;

extern struct snd_pcm_ops pcm_ops;

/**
 * pcm_constructor - Constructs the ALSA PCM middle devices for this driver
 * @card: The snd_card struct to construct the devices for
 * @return 0 on success or error code otherwise
 */
static int pcm_constructor(struct snd_card *card)
{
	struct snd_pcm *pcm;
	int i;

	// Allocate Playback PCM instances
	for (i = 0; i < _driver->playback.devices; i++) {
		CHK_ERR(snd_pcm_new(card,
				    _driver->playback.config[i].devicename, i,
				    1, 1, &pcm));

		/** Register driver callbacks */
		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops);

		pcm->info_flags = 0;
		strcpy(pcm->name, _driver->playback.config[i].devicename);
	}

	// Allocate Capture PCM instances
	for (i = 0; i < _driver->capture.devices; i++) {
		CHK_ERR(snd_pcm_new(card, _driver->capture.config[i].devicename,
				    i, 0, 1, &pcm));

		/** Register driver callbacks */
		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops);

		pcm->info_flags = 0;
		strcpy(pcm->name, _driver->capture.config[i].devicename);
	}

	return 0;
}

/**
 * alloc_dev_config - Allocates memory for ALSA device configuration
 * @return: 0 on success or error code otherwise
 */
static int alloc_dev_config(struct avirt_alsa_dev_config **devconfig,
			    struct avirt_alsa_dev_config *userconfig,
			    unsigned numdevices)
{
	if (numdevices == 0)
		return 0;

	*devconfig = kzalloc(sizeof(struct avirt_alsa_dev_config) * numdevices,
			     GFP_KERNEL);
	if (!(*devconfig))
		return -ENOMEM;

	memcpy(*devconfig, userconfig,
	       sizeof(struct avirt_alsa_dev_config) * numdevices);

	return 0;
}

struct avirt_alsa_dev_group *avirt_alsa_get_dev_group(int direction)
{
	if (!_driver) {
		pr_err("[%s] _driver is NULL", __func__);
		return NULL;
	}

	switch (direction) {
	case SNDRV_PCM_STREAM_PLAYBACK:
	case SNDRV_PCM_STREAM_CAPTURE:
		return &_driver->playback;
	default:
		pr_err("[%s] Direction must be SNDRV_PCM_STREAM_XXX!",
		       __func__);
		return NULL;
	}
}

/**
 * avirt_alsa_init - Initializes the ALSA driver
 * @return: 0 on success or error code otherwise
 */
int avirt_alsa_init()
{
	// Allocate memory for the driver
	_driver = kzalloc(sizeof(struct avirt_alsa_driver), GFP_KERNEL);
	if (!_driver)
		return -ENOMEM;

	return 0;
}

/**
 * avirt_alsa_configure_pcm- Configure the PCM device
 * @config: Device configuration structure array
 * @direction: Direction of PCM (SNDRV_PCM_STREAM_XXX)
 * @numdevices: Number of devices (array length)
 * @return: 0 on success or error code otherwise
 */
int avirt_alsa_configure_pcm(struct avirt_alsa_dev_config *config,
			     int direction, unsigned numdevices)
{
	struct avirt_alsa_dev_group *group;

	group = avirt_alsa_get_dev_group(direction);
	CHK_NULL(group);

	CHK_ERR(alloc_dev_config(&group->config, config, numdevices));

	group->devices = numdevices;

	return 0;
}

/**
 * avirt_alsa_register - Registers the ALSA driver
 * @devptr: Platform driver device
 * @return: 0 on success or error code otherwise
 */
int avirt_alsa_register(struct platform_device *devptr)
{
	struct snd_card *card;
	static struct snd_device_ops device_ops;

	if (!_driver)
		return -EFAULT;

	// Create the card instance
	CHK_ERR_V(snd_card_new(&devptr->dev, SNDRV_DEFAULT_IDX1, "avirt",
			       THIS_MODULE, 0, &card),
		  "Failed to create sound card");

	strcpy(card->driver, "avirt-alsa-device");
	strcpy(card->shortname, "avirt");
	strcpy(card->longname, "A virtual sound card driver for ALSA");
	_driver->card = card;

	// Create new sound device
	CHK_ERR_V((snd_device_new(card, SNDRV_DEV_LOWLEVEL, _driver,
				  &device_ops)),
		  "Failed to create sound device");

	CHK_ERR((pcm_constructor(card)));

	/** Register with the ALSA framework */
	CHK_ERR_V(snd_card_register(card), "Device registration failed");

	return 0;
}

/**
 * avirt_alsa_deregister - Deregisters the ALSA driver
 * @devptr: Platform driver device
 * @return: 0 on success or error code otherwise
 */
int avirt_alsa_deregister(void)
{
	CHK_NULL(_driver->card);
	snd_card_free(_driver->card);
	CHK_NULL(_driver->playback.config);
	kfree(_driver->playback.config);
	CHK_NULL(_driver->capture.config);
	kfree(_driver->capture.config);
	CHK_NULL(_driver);
	kfree(_driver);

	return 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;
}