aboutsummaryrefslogtreecommitdiffstats
path: root/alsa.c
blob: a899bb11757ff04d1cf80ab3f1f9a5eb67ea78a6 (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
// 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"

static struct snd_card *card;

/**
 * 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 < coreinfo.playback.devices; i++) {
		CHK_ERR(snd_pcm_new(card,
				    coreinfo.playback.config[i].devicename, i,
				    1, 0, &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, coreinfo.playback.config[i].devicename);
	}

	// Allocate Capture PCM instances
	for (i = 0; i < coreinfo.capture.devices; i++) {
		CHK_ERR(snd_pcm_new(card, coreinfo.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, coreinfo.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_devconfig **devconfig,
			    struct avirt_alsa_devconfig *userconfig,
			    unsigned int numdevices)
{
	if (numdevices == 0)
		return 0;

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

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

	return 0;
}

struct avirt_alsa_group *avirt_alsa_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;
	}
}

/**
 * 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_devconfig *config, int direction,
			     unsigned int numdevices)
{
	struct avirt_alsa_group *group;

	group = avirt_alsa_get_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)
{
	static struct snd_device_ops device_ops;

	// 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");

	// Create new sound device
	CHK_ERR_V((snd_device_new(card, SNDRV_DEV_LOWLEVEL, &coreinfo,
				  &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(card);
	snd_card_free(card);
	CHK_NULL(coreinfo.playback.config);
	kfree(coreinfo.playback.config);
	CHK_NULL(coreinfo.capture.config);
	kfree(coreinfo.capture.config);

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