summaryrefslogtreecommitdiffstats
path: root/maemo/dsp-ctl.c
blob: 523c580603b14c61e2316a96728b6c40baf0b7d5 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/**
 * @file dsp-ctl.c
 * @brief CTL External plugin implementation.
 * <p>
 * Copyright (C) 2006 Nokia Corporation
 * <p>
 * Contact: Eduardo Bezerra Valentin <eduardo.valentin@indt.org.br>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 * */
#include <stdio.h>
#include <sys/ioctl.h>
#include <alsa/asoundlib.h>
#include <alsa/control_external.h>
#include <string.h>
#include "list.h"
#include "debug.h"
#include "dsp-protocol.h"
#include "constants.h"

#define PLAYBACK_VOLUME_CONTROL_NAME 	"PCM Playback Volume"
#define PLAYBACK_MUTE_CONTROL_NAME 	"PCM Playback Switch"
#define RECORDING_CONTROL_NAME		"Capture Switch"

/**
 * data structure to represent a dsp task device node.
 */
typedef struct {
	dsp_protocol_t *dsp_protocol;
	char *name;
	int channels;
	struct list_head list;
} control_list_t;

/**
 * data structure to represent this plugin information.
 */ 
typedef struct snd_ctl_dsp {
	snd_ctl_ext_t ext;
	int num_playbacks;
	int num_recordings;
	control_list_t **controls;
	control_list_t playback_devices;
	control_list_t recording_devices;
} snd_ctl_dsp_t;

static snd_ctl_dsp_t *free_ref;
/**
 * @param control_list control list to be freed.
 *
 * It passes through this control list and frees
 * all its nodes.
 * 
 * @return zero. success.
 */ 
static int free_control_list(control_list_t * control_list)
{
	struct list_head *pos, *q;
	control_list_t *tmp;
	list_for_each_safe(pos, q, &control_list->list) {
		tmp = list_entry(pos, control_list_t, list);
		list_del(pos);
		free(tmp->name);
		close(tmp->dsp_protocol->fd);
		dsp_protocol_destroy(&(tmp->dsp_protocol));
		free(tmp);
	}
	return 0;
}

/**
 * @param ext snd_ctl_ext_t structure.
 *
 * It is the close event handler for this plugin.
 * It frees all the allocated memory.
 * 
 * @return zero. success.
 */ 
static void dsp_ctl_close(snd_ctl_ext_t * ext)
{
	snd_ctl_dsp_t *dsp_ctl = ext->private_data;
	DENTER();
	free(dsp_ctl->controls);
	free_control_list(&(dsp_ctl->playback_devices));
	free_control_list(&(dsp_ctl->recording_devices));
//      free(dsp_ctl);
	DLEAVE(0);
}

/**
 * @param ext snd_ctl_ext_t structure.
 *
 * It returns number of controls to be used by this
 * plugin. It is based on number of recording and playback
 * device nodes configured to be handled by this plugin.
 * 
 * @return number of controls.
 */ 
static int dsp_ctl_elem_count(snd_ctl_ext_t * ext)
{
	snd_ctl_dsp_t *dsp_ctl = ext->private_data;
	int ret = 2 * dsp_ctl->num_playbacks + dsp_ctl->num_recordings;
	DENTER();
	DLEAVE(ret);
	return ret;
}

/**
 * @param ext snd_ctl_ext_t structure.
 * @param offset offset of current control.
 * @param id id of current control element.
 *
 * It sets name and index for a control based
 * of its offset.
 * 
 * @return zero. success.
 */ 
static int dsp_ctl_elem_list(snd_ctl_ext_t * ext, unsigned int offset,
			     snd_ctl_elem_id_t * id)
{
	snd_ctl_dsp_t *dsp_ctl = ext->private_data;
	int ret = 0;

	DENTER();
	snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
	if (offset < 2 * dsp_ctl->num_playbacks) {
		if (offset % 2 == 1)
			snd_ctl_elem_id_set_name(id,
						 PLAYBACK_MUTE_CONTROL_NAME);
		else
			snd_ctl_elem_id_set_name(id,
						 PLAYBACK_VOLUME_CONTROL_NAME);
		offset /= 2;
	} else {
		offset -= 2 * dsp_ctl->num_playbacks;
		snd_ctl_elem_id_set_name(id, RECORDING_CONTROL_NAME);
	}
	snd_ctl_elem_id_set_index(id, offset);
	DLEAVE(ret);
	return ret;
}

/**
 * @param ext snd_ctl_ext_t structure.
 * @param id control element id from alsa-lib
 *
 * It searches for an control element using 
 * its name and index. If this control can
 * be found, it returns a key to represent it
 * for future use. This key is an index of an
 * array of controls whose is composed of three
 * types of elements: PCM Volume, PCM Switch and
 * Capture Switch. This array is organized as 
 * follows:
 * pv0, ps0, pv1, ps1, ..., pv_n, ps_n, cs0, cs1, ..., cs_m
 *
 * where, pvi is the i-th PCM Volume Control
 *        psi is the i-th PCM Switch Control
 *        csi is the i-th Capture Switch Control
 *        n - is the number of Playback devices
 *        m - is the number of Recording devices
 *
 * @return if control is not found, returns SND_CTL_EXT_KEY_NOT_FOUND.
 * Otherwise, returns a key of control.
 */
static snd_ctl_ext_key_t dsp_ctl_find_elem(snd_ctl_ext_t * ext,
					   const snd_ctl_elem_id_t * id)
{
	snd_ctl_dsp_t *dsp_ctl = ext->private_data;
	snd_ctl_ext_key_t ret = SND_CTL_EXT_KEY_NOT_FOUND;
	int idx;
	const char *name;

	DENTER();
	idx = snd_ctl_elem_id_get_index(id);
	name = snd_ctl_elem_id_get_name(id);
	if (strcmp(PLAYBACK_VOLUME_CONTROL_NAME, name) == 0)
		ret = idx * 2;
	else if (strcmp(PLAYBACK_MUTE_CONTROL_NAME, name) == 0)
		ret = idx * 2 + 1;
	else
		ret = 2 * dsp_ctl->num_playbacks + idx;
	if (ret == SND_CTL_EXT_KEY_NOT_FOUND)
		DPRINT("Not Found name %s, index %d\n",
		       snd_ctl_elem_id_get_name(id), idx);
	DLEAVE((int)ret);
	return ret;
}

/**
 * @param ext snd_ctl_ext_t structure.
 * @param key current control key to be handled.
 * @param type type of this control.
 * @param acc access type of this control.
 * @param count number of channels of this control.
 *
 * It provides information about a control. 
 * Playback device node has an integer and a boolean
 * control. Recording has only boolean control.
 *
 * @return zero. success.
 */
static int dsp_ctl_get_attribute(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key,
				 int *type, unsigned int *acc,
				 unsigned int *count)
{
	snd_ctl_dsp_t *dsp_ctl = ext->private_data;
	int ret = 0;
	DENTER();

	if (key >= 2 * dsp_ctl->num_playbacks || key % 2 == 1)
		*type = SND_CTL_ELEM_TYPE_BOOLEAN;
	else
		*type = SND_CTL_ELEM_TYPE_INTEGER;

	*count = dsp_ctl->controls[key]->channels;
	*acc = SND_CTL_EXT_ACCESS_READWRITE;
	DLEAVE(ret);
	return ret;
}

/**
 * @param ext snd_ctl_ext_t structure.
 * @param key current control key to be handled.
 * @param imin minimum value of this integer control.
 * @param imax maximum value of this integer control.
 * @param istep steps value of this integer control.
 *
 * It provides information about integer control. It
 * consideres both boolean and integer controls.
 *
 * @return  zero. success.
 */
static int dsp_ctl_get_integer_info(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key,
				    long *imin, long *imax, long *istep)
{
	snd_ctl_dsp_t *dsp_ctl = ext->private_data;
	DENTER();
	*imin = 0;
	if (key >= 2 * dsp_ctl->num_playbacks || key % 2 == 1)
		*imax = 1;
	else
		*imax = 100;
	*istep = 0;
	DLEAVE(0);
	return 0;
}

/**
 * @param ext snd_ctl_ext_t structure.
 * @param key current control key to be handled.
 * @param value return value holder.
 *
 * It reads volume information from dsp task node and 
 * fills it into value to alsa-lib.
 *
 * @return zero. success.
 */
static int dsp_ctl_read_integer(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key,
				long *value)
{
	int ret = 0;
	unsigned char left, right;
	snd_ctl_dsp_t *dsp_ctl = ext->private_data;
	control_list_t *tmp = dsp_ctl->controls[key];

	DENTER();
	if (key >= 2 * dsp_ctl->num_playbacks || key % 2 == 1) {
		ret = dsp_protocol_get_mute(tmp->dsp_protocol);
		if (ret >= 0) {
			left = ret == 0 ? 1 : 0;
			right = left;
		}
	} else
		ret = dsp_protocol_get_volume(tmp->dsp_protocol, &left, &right);
	if (ret >= 0) {
		value[0] = left;
		if (tmp->channels == 2)
			value[1] = right;
	} else {
		value[0] = 0;
		if (tmp->channels == 2)
			value[1] = 0;
		ret = 0;
	}

	DLEAVE(ret);
	return ret;
}

/**
 * @param ext snd_ctl_ext_t structure.
 * @param key current control key to be handled.
 * @param value volume value to be written.
 *
 * It writes volume information to dsp task node from
 * value that comes from alsa-lib. It checks if value
 * coming from alsa-lib is different of value in dsp 
 * side.
 *
 * @return zero if not updated. one if updated.
 */
static int dsp_ctl_write_integer(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key,
				 long *value)
{
	int ret;
	unsigned char left, right;
	snd_ctl_dsp_t *dsp_ctl = ext->private_data;
	control_list_t *tmp = dsp_ctl->controls[key];

	DENTER();
	if (key >= 2 * dsp_ctl->num_playbacks || key % 2 == 1) {
		if ((ret = dsp_protocol_get_mute(tmp->dsp_protocol)) < 0)
			goto zero;
		left = ret == 0 ? 1 : 0;
		right = left;
	} else
	    if ((ret =
		 dsp_protocol_get_volume(tmp->dsp_protocol, &left, &right)) < 0)
		goto zero;

	if (tmp->channels == 2) {
		if (left == value[0] && right == value[1]) {
			ret = 0;
			goto out;
		}
		right = value[1];
		left = value[0];
	} else {
		if (left == value[0]) {
			ret = 0;
			goto out;
		}
		right = left = value[0];
	}

	if (key >= 2 * dsp_ctl->num_playbacks || key % 2 == 1) {
		if ((ret =
		     dsp_protocol_set_mute(tmp->dsp_protocol,
					   left == 0 ? 1 : 0)) < 0)
			goto zero;
	} else
	    if ((ret =
		 dsp_protocol_set_volume(tmp->dsp_protocol, left, right)) < 0)
		goto zero;
	ret = 1;
	goto out;
      zero:
	value[0] = 0;
	if (tmp->channels == 2)
		value[1] = 0;
	ret = 0;
      out:
	DLEAVE(ret);
	return ret;
}

/**
 * @param ext
 * @param id
 * @param event_mask
 *
 * @return -EIO
 */
static int dsp_ctl_read_event(snd_ctl_ext_t * ext, snd_ctl_elem_id_t * id,
			      unsigned int *event_mask)
{
	return -EIO;
}

/**
 * @param n configuration file parse tree. 
 * @param device_list list of device files to be filled.
 *
 * It searches for device file names in given configuration parse
 * tree. When one device file name is found, it is filled into device_list.
 *
 * @return zero if success, otherwise a negative error code.
 */
static int fill_control_list(snd_config_t * n, control_list_t * control_list)
{
	snd_config_iterator_t j, nextj;
	control_list_t *tmp;
	long idx = 0;
	DENTER();
	INIT_LIST_HEAD(&control_list->list);
	snd_config_for_each(j, nextj, n) {
		snd_config_t *s = snd_config_iterator_entry(j);
		const char *id_number;
		long k;
		if (snd_config_get_id(s, &id_number) < 0)
			continue;
		if (safe_strtol(id_number, &k) < 0) {
			SNDERR("id of field %s is not an integer", id_number);
			idx = -EINVAL;
			goto out;
		}
		if (k == idx) {
			idx++;
			/* add to available dsp task nodes */
			tmp = (control_list_t *) malloc(sizeof(control_list_t));
			if (snd_config_get_ascii(s, &(tmp->name)) < 0) {
				SNDERR("invalid ascii string for id %s\n",
				       id_number);
				idx = -EINVAL;
				goto out;
			}
			list_add(&(tmp->list), &(control_list->list));
		}
	}
      out:
	DLEAVE((int)idx);
	return idx;
}

/**
 * @param dsp_ctl snd_dsp_t structure.
 *
 * It probes all dsp tasks configured to be handled by this
 * plugin. It gets all needed information about volume controling.
 *
 * @return zero if success, otherwise a negative error code.
 */
static int dsp_ctl_probe_dsp_task(snd_ctl_dsp_t * dsp_ctl)
{
	int err = 0, i;
	control_list_t *tmp;
	struct list_head *lists[2] =
	    { &(dsp_ctl->playback_devices.list),
&(dsp_ctl->recording_devices.list) };
	DENTER();
	DPRINT("Probing dsp device nodes \n");
	for (i = 0; i < 2; i++) {
		list_for_each_entry(tmp, lists[i], list) {
			DPRINT("Trying to use %s\n", tmp->name);
			/* Initialise the dsp_protocol and create connection */
			if ((err =
			     dsp_protocol_create(&(tmp->dsp_protocol))) < 0)
				goto out;
			if ((tmp->channels =
			     dsp_protocol_probe_node(tmp->dsp_protocol,
						     tmp->name)) < 0) {
				DPRINT("%s is not available now\n", tmp->name);
				err = tmp->channels;
				close(tmp->dsp_protocol->fd);	//memory leak?!?
				goto out;
			}
		}
	}
	if (err < 0) {
		DPRINT("No valid dsp task nodes for now. Exiting.\n");
	}
      out:
	DLEAVE(err);
	return err;
}

/**
 * @param dsp_ctl snd_dsp_t structure.
 *
 * It fills an array of controls to represent PCM Volume,
 * PCM Switch and Capture Switch controls.
 *
 * @return zero if success, otherwise a negative error code.
 */
static int dsp_ctl_fill_controls(snd_ctl_dsp_t * dsp_ctl)
{
	int ret = 0;
	int i;
	int num_controls = 2 * dsp_ctl->num_playbacks + dsp_ctl->num_recordings;
	DENTER();
	control_list_t *tmp;
	dsp_ctl->controls = calloc(num_controls, sizeof(control_list_t *));
	if (dsp_ctl->controls == NULL) {
		ret = -ENOMEM;
		goto out;
	}
	i = 0;
	/* Each pcm task node should have a PCM Volume and PCM Switch control */
	list_for_each_entry(tmp, &(dsp_ctl->playback_devices.list), list) {
		dsp_ctl->controls[i] = tmp;
		dsp_ctl->controls[i + 1] = tmp;
		i += 2;
	}
	/* Each pcm_rec node should have a Capture Switch control */
	list_for_each_entry(tmp, &(dsp_ctl->recording_devices.list), list)
	    dsp_ctl->controls[i++] = tmp;

      out:
	DLEAVE(ret);
	return ret;
}

/**
 * Alsa-lib callback structure.
 */
static snd_ctl_ext_callback_t dsp_ctl_ext_callback = {
	.close = dsp_ctl_close,
	.elem_count = dsp_ctl_elem_count,
	.elem_list = dsp_ctl_elem_list,
	.find_elem = dsp_ctl_find_elem,
	.get_attribute = dsp_ctl_get_attribute,
	.get_integer_info = dsp_ctl_get_integer_info,
	.read_integer = dsp_ctl_read_integer,
	.write_integer = dsp_ctl_write_integer,
	.read_event = dsp_ctl_read_event,
};

/**
 * It initializes the alsa ctl plugin. It reads the parameters and creates the 
 * connection with the pcm device file.
 *
 * @return zero if success, otherwise a negative error code.
 */
SND_CTL_PLUGIN_DEFINE_FUNC(dsp_ctl)
{
	snd_config_iterator_t i, next;
	snd_ctl_dsp_t *dsp_ctl;
	int err;
	int ret;

	DENTER();
	/* Allocate the structure */
	dsp_ctl = calloc(1, sizeof(*dsp_ctl));
	if (dsp_ctl == NULL) {
		ret = -ENOMEM;
		goto out;
	}
	/* Read the configuration searching for configurated devices */
	snd_config_for_each(i, next, conf) {
		snd_config_t *n = snd_config_iterator_entry(i);
		const char *id;
		if (snd_config_get_id(n, &id) < 0)
			continue;
		if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
			continue;
		if (strcmp(id, "playback_devices") == 0) {
			if (snd_config_get_type(n) == SND_CONFIG_TYPE_COMPOUND){
				if ((dsp_ctl->num_playbacks =
				     fill_control_list(n,
						&(dsp_ctl->
						playback_devices))) < 0) {
					SNDERR("Could not fill control"
						" list for playback devices\n");
					err = -EINVAL;
					goto error;
				}
			} else {
				SNDERR("Invalid type for %s", id);
				err = -EINVAL;
				goto error;
			}
			continue;
		}
		if (strcmp(id, "recording_devices") == 0) {
			if (snd_config_get_type(n) == SND_CONFIG_TYPE_COMPOUND){
				if ((dsp_ctl->num_recordings =
				     fill_control_list(n,
						&(dsp_ctl->
						recording_devices))) < 0) {
					SNDERR("Could not fill string "
						"list for recording devices\n");
					err = -EINVAL;
					goto error;
				}
			} else {
				SNDERR("Invalid type for %s", id);
				err = -EINVAL;
				goto error;
			}
			continue;
		}
		SNDERR("Unknown field %s", id);
		err = -EINVAL;
		goto error;
	}

	if ((err = dsp_ctl_probe_dsp_task(dsp_ctl)) < 0)
		goto error;

	if ((err = dsp_ctl_fill_controls(dsp_ctl)) < 0)
		goto error;
	dsp_ctl->ext.version = SND_CTL_EXT_VERSION;
	dsp_ctl->ext.card_idx = 0;	/* FIXME */
	strcpy(dsp_ctl->ext.id, "ALSA-DSP-CTL");
	strcpy(dsp_ctl->ext.name, "Alsa-DSP external ctl plugin");
	strcpy(dsp_ctl->ext.longname, "External Control Alsa plugin for DSP");
	strcpy(dsp_ctl->ext.mixername, "ALSA-DSP plugin Mixer");
	dsp_ctl->ext.callback = &dsp_ctl_ext_callback;
	dsp_ctl->ext.private_data = dsp_ctl;
	free_ref = dsp_ctl;

	if ((err = snd_ctl_ext_create(&dsp_ctl->ext, name, mode)) < 0)
		goto error;

	*handlep = dsp_ctl->ext.handle;
	ret = 0;
	goto out;
      error:
	ret = err;
	free(dsp_ctl);
      out:
	DLEAVE(ret);
	return ret;
}
static void dsp_ctl_descructor(void) __attribute__ ((destructor));

static void dsp_ctl_descructor(void)
{
	DENTER();
	DPRINT("dsp ctl destructor\n");
	DPRINT("checking for memories leaks and releasing resources\n");
	if (free_ref) {
		if (free_ref->controls) 
			free(free_ref->controls);

		free_control_list(&(free_ref->playback_devices));

		free_control_list(&(free_ref->recording_devices));
		
		free(free_ref);
		free_ref = NULL;
	}
	DLEAVE(0);

}

SND_CTL_PLUGIN_SYMBOL(dsp_ctl);