summaryrefslogtreecommitdiffstats
path: root/configfs.c
blob: 5668cb338b77777b52f70fec49327047670fa660 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// SPDX-License-Identifier: GPL-2.0
/*
 * AVIRT - ALSA Virtual Soundcard
 *
 * Copyright (c) 2010-2018 Fiberdyne Systems Pty Ltd
 *
 * configfs.c - AVIRT configfs support
 */

#include <linux/slab.h>

#include "core.h"

#define D_LOGNAME "configfs"

#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)

/**
 * Defines the configfs direction 'show' callback for a stream or route
 */
#define CFG_SND_AVIRT_DIRECTION_RO(type)                                       \
	static ssize_t cfg_snd_avirt_##type##_direction_show(                  \
		struct config_item *item, char *page)                          \
	{                                                                      \
		ssize_t count;                                                 \
		struct snd_avirt_##type *s =                                   \
			snd_avirt_##type##_from_config_item(item);             \
		count = sprintf(page, "%d\n", s->direction);                   \
		return count;                                                  \
	}                                                                      \
	CONFIGFS_ATTR_RO(cfg_snd_avirt_##type##_, direction)

/**
 * Defines the configfs channels 'show'/'store' callbacks for a stream or route
 */
#define CFG_SND_AVIRT_CHANNELS(type)                                           \
	static ssize_t cfg_snd_avirt_##type##_channels_show(                   \
		struct config_item *item, char *page)                          \
	{                                                                      \
		ssize_t count;                                                 \
		struct snd_avirt_##type *s =                                   \
			snd_avirt_##type##_from_config_item(item);             \
		count = sprintf(page, "%d\n", s->channels);                    \
		return count;                                                  \
	}                                                                      \
	static ssize_t cfg_snd_avirt_##type##_channels_store(                  \
		struct config_item *item, const char *page, size_t count)      \
	{                                                                      \
		int err;                                                       \
		struct snd_avirt_##type *s =                                   \
			snd_avirt_##type##_from_config_item(item);             \
		unsigned long tmp;                                             \
		char *p = (char *)page;                                        \
		err = kstrtoul(p, 10, &tmp);                                   \
		if (err < 0)                                                   \
			return err;                                            \
		if ((tmp > INT_MAX) || (tmp == 0))                             \
			return -ERANGE;                                        \
		s->channels = tmp;                                             \
		return count;                                                  \
	}                                                                      \
	CONFIGFS_ATTR(cfg_snd_avirt_##type##_, channels);

CFG_SND_AVIRT_DIRECTION_RO(stream);
CFG_SND_AVIRT_CHANNELS(stream);
CFG_SND_AVIRT_DIRECTION_RO(route);
CFG_SND_AVIRT_CHANNELS(route);

static ssize_t cfg_snd_avirt_stream_map_show(struct config_item *item,
					     char *page)
{
	struct snd_avirt_stream *stream =
		snd_avirt_stream_from_config_item(item);

	return sprintf(page, "%s\n", stream->map);
}

static ssize_t cfg_snd_avirt_stream_map_store(struct config_item *item,
					      const char *page, size_t count)
{
	char *split;
	struct snd_avirt_stream *stream =
		snd_avirt_stream_from_config_item(item);

	split = strsep((char **)&page, "\n");
	snd_avirt_stream_set_map(stream, split);

	return count;
}
CONFIGFS_ATTR(cfg_snd_avirt_stream_, map);

static struct configfs_attribute *cfg_snd_avirt_stream_attrs[] = {
	&cfg_snd_avirt_stream_attr_channels,
	&cfg_snd_avirt_stream_attr_map,
	&cfg_snd_avirt_stream_attr_direction,
	NULL,
};

static ssize_t cfg_snd_avirt_route_to_ap_show(struct config_item *item,
					      char *page)
{
	struct snd_avirt_route *route = snd_avirt_route_from_config_item(item);

	if (route)
		if (route->to_ap)
			return sprintf(page, "%s\n", route->to_ap->uid);

	return sprintf(page, "\n");
}

static ssize_t cfg_snd_avirt_route_to_ap_store(struct config_item *item,
					       const char *page, size_t count)
{
	char *uid_ap;
	struct snd_avirt_route *route = snd_avirt_route_from_config_item(item);
	struct snd_avirt_audiopath *to_ap;

	uid_ap = strsep((char **)&page, "\n");
	to_ap = snd_avirt_audiopath_get(uid_ap);
	if (!to_ap) {
		D_ERRORK("Audio Path '%s' does not exist!", uid_ap);
		D_ERRORK("Cannot set 'route'->'to_ap'");
		return count;
	}

	route->to_ap = to_ap;
	if (route->from_ap) {
		route->from_ap->route_to_ap = to_ap;
		route->to_ap->route_from_ap = route->from_ap;
	}

	return count;
}
CONFIGFS_ATTR(cfg_snd_avirt_route_, to_ap);

static ssize_t cfg_snd_avirt_route_from_ap_show(struct config_item *item,
						char *page)
{
	struct snd_avirt_route *route = snd_avirt_route_from_config_item(item);

	if (route)
		if (route->from_ap)
			return sprintf(page, "%s\n", route->from_ap->uid);

	return sprintf(page, "\n");
}

static ssize_t cfg_snd_avirt_route_from_ap_store(struct config_item *item,
						 const char *page, size_t count)
{
	char *uid_ap;
	struct snd_avirt_route *route = snd_avirt_route_from_config_item(item);
	struct snd_avirt_audiopath *from_ap;

	uid_ap = strsep((char **)&page, "\n");
	from_ap = snd_avirt_audiopath_get(uid_ap);
	if (!from_ap) {
		D_ERRORK("Audio Path '%s' does not exist!", uid_ap);
		D_ERRORK("Cannot set 'route'->'from_ap'");
		return count;
	}

	route->from_ap = from_ap;
	if (route->to_ap) {
		route->to_ap->route_from_ap = from_ap;
		route->from_ap->route_to_ap = route->to_ap;
	}

	return count;
}
CONFIGFS_ATTR(cfg_snd_avirt_route_, from_ap);

static struct configfs_attribute *cfg_snd_avirt_route_attrs[] = {
	&cfg_snd_avirt_route_attr_channels,
	&cfg_snd_avirt_route_attr_direction,
	&cfg_snd_avirt_route_attr_from_ap,
	&cfg_snd_avirt_route_attr_to_ap,
	NULL,
};

static void cfg_snd_avirt_stream_release(struct config_item *item)
{
	struct snd_avirt_stream *stream =
		snd_avirt_stream_from_config_item(item);
	if (!stream) {
		D_ERRORK("Cannot release stream!");
		return;
	}

	D_INFOK("Release stream: %s", stream->name);
	kfree(stream->pcm_ops);
	kfree(stream);
}

static void cfg_snd_avirt_route_release(struct config_item *item)
{
	struct snd_avirt_route *route = snd_avirt_route_from_config_item(item);
	if (!route) {
		D_ERRORK("Cannot release route!");
		return;
	}

	D_INFOK("Release route: %s", route->name);
	kfree(route);
}

static struct configfs_item_operations cfg_snd_avirt_stream_ops = {
	.release = cfg_snd_avirt_stream_release,
};

static struct configfs_item_operations cfg_snd_avirt_route_ops = {
	.release = cfg_snd_avirt_route_release,
};

static struct config_item_type cfg_snd_avirt_stream_type = {
	.ct_item_ops = &cfg_snd_avirt_stream_ops,
	.ct_attrs = cfg_snd_avirt_stream_attrs,
	.ct_owner = THIS_MODULE,
};

static struct config_item_type cfg_snd_avirt_route_type = {
	.ct_item_ops = &cfg_snd_avirt_route_ops,
	.ct_attrs = cfg_snd_avirt_route_attrs,
	.ct_owner = THIS_MODULE,
};

static struct config_item *
cfg_snd_avirt_stream_make_item(struct config_group *group, const char *name)
{
	char *split;
	int direction;
	struct snd_avirt_stream *stream;

	// Get prefix (playback_ or capture_)
	split = strsep((char **)&name, "_");
	if (!split) {
		D_ERRORK("Stream name: '%s' invalid!", split);
		D_ERRORK("Must begin with playback_ * or capture_ *");
		return ERR_PTR(-EINVAL);
	}
	if (!strcmp(split, "playback")) {
		direction = SNDRV_PCM_STREAM_PLAYBACK;
	} else if (!strcmp(split, "capture")) {
		direction = SNDRV_PCM_STREAM_CAPTURE;
	} else {
		D_ERRORK("Stream name: '%s' invalid!", split);
		D_ERRORK("Must begin with playback_ * or capture_ ");
		return ERR_PTR(-EINVAL);
	}

	// Get stream name, and create PCM for stream
	split = strsep((char **)&name, "\n");
	stream = snd_avirt_stream_create(split, direction);
	if (IS_ERR(stream))
		return ERR_PTR(PTR_ERR(stream));

	config_item_init_type_name(&stream->item, name,
				   &cfg_snd_avirt_stream_type);

	D_INFOK("Make stream: %s", stream->name);

	return &stream->item;
}

static struct config_item *
cfg_snd_avirt_route_make_item(struct config_group *group, const char *name)
{
	char *split;
	int direction;
	struct snd_avirt_route *route;

	// Get prefix (playback_ or capture_)
	split = strsep((char **)&name, "_");
	if (!split) {
		D_ERRORK("Route name: '%s' invalid!", split);
		D_ERRORK("Must begin with playback_ * or capture_ *");
		return ERR_PTR(-EINVAL);
	}
	if (!strcmp(split, "playback")) {
		direction = SNDRV_PCM_STREAM_PLAYBACK;
	} else if (!strcmp(split, "capture")) {
		direction = SNDRV_PCM_STREAM_CAPTURE;
	} else {
		D_ERRORK("Route name: '%s' invalid!", split);
		D_ERRORK("Must begin with playback_ * or capture_ ");
		return ERR_PTR(-EINVAL);
	}

	// Get route name, and create route
	split = strsep((char **)&name, "\n");
	route = snd_avirt_route_create(split, direction);
	if (IS_ERR(route))
		return ERR_PTR(PTR_ERR(route));

	config_item_init_type_name(&route->item, name,
				   &cfg_snd_avirt_route_type);

	D_INFOK("Make route: %s", route->name);

	return &route->item;
}

static ssize_t cfg_snd_avirt_stream_group_sealed_show(struct config_item *item,
						      char *page)
{
	return snprintf(page, PAGE_SIZE, "%d\n", snd_avirt_streams_sealed());
}

static ssize_t cfg_snd_avirt_stream_group_sealed_store(struct config_item *item,
						       const char *page,
						       size_t count)
{
	unsigned long tmp;
	char *p = (char *)page;

	CHK_ERR(kstrtoul(p, 10, &tmp));

	if (tmp != 1) {
		D_ERRORK("streams can only be sealed, not unsealed!");
		return -ERANGE;
	}

	snd_avirt_streams_seal();

	return count;
}
CONFIGFS_ATTR(cfg_snd_avirt_stream_group_, sealed);

static struct configfs_attribute *cfg_snd_avirt_stream_group_attrs[] = {
	&cfg_snd_avirt_stream_group_attr_sealed,
	NULL,
};

static struct configfs_group_operations cfg_snd_avirt_stream_group_ops = {
	.make_item = cfg_snd_avirt_stream_make_item
};

static struct configfs_group_operations cfg_snd_avirt_route_group_ops = {
	.make_item = cfg_snd_avirt_route_make_item
};

static struct config_item_type cfg_stream_group_type = {
	.ct_group_ops = &cfg_snd_avirt_stream_group_ops,
	.ct_attrs = cfg_snd_avirt_stream_group_attrs,
	.ct_owner = THIS_MODULE
};

static struct config_item_type cfg_route_group_type = {
	.ct_group_ops = &cfg_snd_avirt_route_group_ops,
	.ct_attrs = NULL,
	.ct_owner = THIS_MODULE
};

static struct config_item_type cfg_avirt_group_type = {
	.ct_owner = THIS_MODULE,
};

static struct configfs_subsystem cfg_subsys = {
	.su_group =
		{
			.cg_item =
				{
					.ci_namebuf = "snd-avirt",
					.ci_type = &cfg_avirt_group_type,
				},
		},
};

int __init snd_avirt_configfs_init(struct snd_avirt_core *core)
{
	int err;

	config_group_init(&cfg_subsys.su_group);
	mutex_init(&cfg_subsys.su_mutex);
	err = configfs_register_subsystem(&cfg_subsys);
	if (err) {
		D_ERRORK("Cannot register configfs subsys!");
		return err;
	}

	/* Create streams default group */
	core->stream_group = configfs_register_default_group(
		&cfg_subsys.su_group, "streams", &cfg_stream_group_type);
	if (IS_ERR(core->stream_group)) {
		err = PTR_ERR(core->stream_group);
		D_ERRORK("Cannot register configfs default group 'streams'!");
		goto exit_configfs;
	}

	/* Create routes default group */
	core->route_group = configfs_register_default_group(
		&cfg_subsys.su_group, "routes", &cfg_route_group_type);
	if (IS_ERR(core->route_group)) {
		err = PTR_ERR(core->route_group);
		D_ERRORK("Cannot register configfs default group 'routes'!");
		goto exit_configfs;
	}

	return 0;

exit_configfs:
	configfs_unregister_subsystem(&cfg_subsys);

	return err;
}

void __exit snd_avirt_configfs_exit(struct snd_avirt_core *core)
{
	configfs_unregister_default_group(core->stream_group);
	configfs_unregister_subsystem(&cfg_subsys);
}