summaryrefslogtreecommitdiff
path: root/src/control/control_hw.c
blob: e9a6be274e03c07d0c65686fd86aa799c1813123 (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
/*
 *  Control Interface - Hardware
 *  Copyright (c) 1998,1999,2000 by Jaroslav Kysela <perex@perex.cz>
 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
 *
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation; either version 2.1 of
 *   the License, or (at your option) any later version.
 *
 *   This program 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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser 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 <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "control_local.h"

#ifndef PIC
/* entry for static linking */
const char *_snd_module_control_hw = "";
#endif

#ifndef F_SETSIG
#define F_SETSIG 10
#endif

#ifndef DOC_HIDDEN
#define SNDRV_FILE_CONTROL	ALSA_DEVICE_DIRECTORY "controlC%i"
#define SNDRV_CTL_VERSION_MAX	SNDRV_PROTOCOL_VERSION(2, 0, 4)

typedef struct {
	int card;
	int fd;
	unsigned int protocol;
} snd_ctl_hw_t;
#endif /* DOC_HIDDEN */

static int snd_ctl_hw_close(snd_ctl_t *handle)
{
	snd_ctl_hw_t *hw = handle->private_data;
	int res;
	res = close(hw->fd) < 0 ? -errno : 0;
	free(hw);
	return res;
}

static int snd_ctl_hw_nonblock(snd_ctl_t *handle, int nonblock)
{
	snd_ctl_hw_t *hw = handle->private_data;
	long flags;
	int fd = hw->fd;
	if ((flags = fcntl(fd, F_GETFL)) < 0) {
		SYSERR("F_GETFL failed");
		return -errno;
	}
	if (nonblock)
		flags |= O_NONBLOCK;
	else
		flags &= ~O_NONBLOCK;
	if (fcntl(fd, F_SETFL, flags) < 0) {
		SYSERR("F_SETFL for O_NONBLOCK failed");
		return -errno;
	}
	return 0;
}

static int snd_ctl_hw_async(snd_ctl_t *ctl, int sig, pid_t pid)
{
	long flags;
	snd_ctl_hw_t *hw = ctl->private_data;
	int fd = hw->fd;

	if ((flags = fcntl(fd, F_GETFL)) < 0) {
		SYSERR("F_GETFL failed");
		return -errno;
	}
	if (sig >= 0)
		flags |= O_ASYNC;
	else
		flags &= ~O_ASYNC;
	if (fcntl(fd, F_SETFL, flags) < 0) {
		SYSERR("F_SETFL for O_ASYNC failed");
		return -errno;
	}
	if (sig < 0)
		return 0;
	if (fcntl(fd, F_SETSIG, (long)sig) < 0) {
		SYSERR("F_SETSIG failed");
		return -errno;
	}
	if (fcntl(fd, F_SETOWN, (long)pid) < 0) {
		SYSERR("F_SETOWN failed");
		return -errno;
	}
	return 0;
}

static int snd_ctl_hw_subscribe_events(snd_ctl_t *handle, int subscribe)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) {
		SYSERR("SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS failed");
		return -errno;
	}
	return 0;
}

static int snd_ctl_hw_card_info(snd_ctl_t *handle, snd_ctl_card_info_t *info)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_CARD_INFO, info) < 0) {
		SYSERR("SNDRV_CTL_IOCTL_CARD_INFO failed");
		return -errno;
	}
	return 0;
}

static int snd_ctl_hw_elem_list(snd_ctl_t *handle, snd_ctl_elem_list_t *list)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_LIST, list) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_info(snd_ctl_t *handle, snd_ctl_elem_info_t *info)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_INFO, info) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_add(snd_ctl_t *handle, snd_ctl_elem_info_t *info)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_ADD, info) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_replace(snd_ctl_t *handle, snd_ctl_elem_info_t *info)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_REPLACE, info) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_remove(snd_ctl_t *handle, snd_ctl_elem_id_t *id)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_REMOVE, id) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_read(snd_ctl_t *handle, snd_ctl_elem_value_t *control)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_READ, control) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_write(snd_ctl_t *handle, snd_ctl_elem_value_t *control)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, control) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_lock(snd_ctl_t *handle, snd_ctl_elem_id_t *id)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_LOCK, id) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_unlock(snd_ctl_t *handle, snd_ctl_elem_id_t *id)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_UNLOCK, id) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_elem_tlv(snd_ctl_t *handle, int op_flag,
			       unsigned int numid,
			       unsigned int *tlv, unsigned int tlv_size)
{
	int inum;
	snd_ctl_hw_t *hw = handle->private_data;
	struct sndrv_ctl_tlv *xtlv;
	
	/* we don't support TLV on protocol ver 2.0.3 or earlier */
	if (hw->protocol < SNDRV_PROTOCOL_VERSION(2, 0, 4))
		return -ENXIO;

	switch (op_flag) {
	case -1: inum = SNDRV_CTL_IOCTL_TLV_COMMAND; break;
 	case 0:	inum = SNDRV_CTL_IOCTL_TLV_READ; break;
	case 1:	inum = SNDRV_CTL_IOCTL_TLV_WRITE; break;
	default: return -EINVAL;
	}
	xtlv = malloc(sizeof(struct sndrv_ctl_tlv) + tlv_size);
	if (xtlv == NULL)
		return -ENOMEM; 
	xtlv->numid = numid;
	xtlv->length = tlv_size;
	memcpy(xtlv->tlv, tlv, tlv_size);
	if (ioctl(hw->fd, inum, xtlv) < 0) {
		free(xtlv);
		return -errno;
	}
	if (op_flag == 0) {
		if (xtlv->tlv[1] + 2 * sizeof(unsigned int) > tlv_size)
			return -EFAULT;
		memcpy(tlv, xtlv->tlv, xtlv->tlv[1] + 2 * sizeof(unsigned int));
	}
	free(xtlv);
	return 0;
}

static int snd_ctl_hw_hwdep_next_device(snd_ctl_t *handle, int * device)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE, device) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_hwdep_info(snd_ctl_t *handle, snd_hwdep_info_t * info)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_HWDEP_INFO, info) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_pcm_next_device(snd_ctl_t *handle, int * device)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE, device) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_pcm_info(snd_ctl_t *handle, snd_pcm_info_t * info)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_PCM_INFO, info) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_pcm_prefer_subdevice(snd_ctl_t *handle, int subdev)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE, &subdev) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_rawmidi_next_device(snd_ctl_t *handle, int * device)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE, device) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_rawmidi_info(snd_ctl_t *handle, snd_rawmidi_info_t * info)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_RAWMIDI_INFO, info) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_rawmidi_prefer_subdevice(snd_ctl_t *handle, int subdev)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE, &subdev) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_set_power_state(snd_ctl_t *handle, unsigned int state)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_POWER, &state) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_get_power_state(snd_ctl_t *handle, unsigned int *state)
{
	snd_ctl_hw_t *hw = handle->private_data;
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_POWER_STATE, state) < 0)
		return -errno;
	return 0;
}

static int snd_ctl_hw_read(snd_ctl_t *handle, snd_ctl_event_t *event)
{
	snd_ctl_hw_t *hw = handle->private_data;
	ssize_t res = read(hw->fd, event, sizeof(*event));
	if (res <= 0)
		return -errno;
	if (CHECK_SANITY(res != sizeof(*event))) {
		SNDMSG("snd_ctl_hw_read: read size error (req:%d, got:%d)\n",
		       sizeof(*event), res);
		return -EINVAL;
	}
	return 1;
}

static const snd_ctl_ops_t snd_ctl_hw_ops = {
	.close = snd_ctl_hw_close,
	.nonblock = snd_ctl_hw_nonblock,
	.async = snd_ctl_hw_async,
	.subscribe_events = snd_ctl_hw_subscribe_events,
	.card_info = snd_ctl_hw_card_info,
	.element_list = snd_ctl_hw_elem_list,
	.element_info = snd_ctl_hw_elem_info,
	.element_add = snd_ctl_hw_elem_add,
	.element_replace = snd_ctl_hw_elem_replace,
	.element_remove = snd_ctl_hw_elem_remove,
	.element_read = snd_ctl_hw_elem_read,
	.element_write = snd_ctl_hw_elem_write,
	.element_lock = snd_ctl_hw_elem_lock,
	.element_unlock = snd_ctl_hw_elem_unlock,
	.element_tlv = snd_ctl_hw_elem_tlv,
	.hwdep_next_device = snd_ctl_hw_hwdep_next_device,
	.hwdep_info = snd_ctl_hw_hwdep_info,
	.pcm_next_device = snd_ctl_hw_pcm_next_device,
	.pcm_info = snd_ctl_hw_pcm_info,
	.pcm_prefer_subdevice = snd_ctl_hw_pcm_prefer_subdevice,
	.rawmidi_next_device = snd_ctl_hw_rawmidi_next_device,
	.rawmidi_info = snd_ctl_hw_rawmidi_info,
	.rawmidi_prefer_subdevice = snd_ctl_hw_rawmidi_prefer_subdevice,
	.set_power_state = snd_ctl_hw_set_power_state,
	.get_power_state = snd_ctl_hw_get_power_state,
	.read = snd_ctl_hw_read,
};

int snd_ctl_hw_open(snd_ctl_t **handle, const char *name, int card, int mode)
{
	int fd, ver;
	char filename[sizeof(SNDRV_FILE_CONTROL) + 10];
	int fmode;
	snd_ctl_t *ctl;
	snd_ctl_hw_t *hw;
	int err;

	*handle = NULL;	

	if (CHECK_SANITY(card < 0 || card >= 32)) {
		SNDMSG("Invalid card index %d", card);
		return -EINVAL;
	}
	sprintf(filename, SNDRV_FILE_CONTROL, card);
	if (mode & SND_CTL_READONLY)
		fmode = O_RDONLY;
	else
		fmode = O_RDWR;
	if (mode & SND_CTL_NONBLOCK)
		fmode |= O_NONBLOCK;
	if (mode & SND_CTL_ASYNC)
		fmode |= O_ASYNC;
	fd = snd_open_device(filename, fmode);
	if (fd < 0) {
		snd_card_load(card);
		fd = snd_open_device(filename, fmode);
		if (fd < 0)
			return -errno;
	}
#if 0
	/*
	 * this is bogus, an application have to care about open filedescriptors
	 */
	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
		SYSERR("fcntl FD_CLOEXEC failed");
		err = -errno;
		close(fd);
		return err;
	}
#endif
	if (ioctl(fd, SNDRV_CTL_IOCTL_PVERSION, &ver) < 0) {
		err = -errno;
		close(fd);
		return err;
	}
	if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_CTL_VERSION_MAX)) {
		close(fd);
		return -SND_ERROR_INCOMPATIBLE_VERSION;
	}
	hw = calloc(1, sizeof(snd_ctl_hw_t));
	if (hw == NULL) {
		close(fd);
		return -ENOMEM;
	}
	hw->card = card;
	hw->fd = fd;
	hw->protocol = ver;

	err = snd_ctl_new(&ctl, SND_CTL_TYPE_HW, name);
	if (err < 0) {
		close(fd);
		free(hw);
	}
	ctl->ops = &snd_ctl_hw_ops;
	ctl->private_data = hw;
	ctl->poll_fd = fd;
	*handle = ctl;
	return 0;
}

int _snd_ctl_hw_open(snd_ctl_t **handlep, char *name, snd_config_t *root ATTRIBUTE_UNUSED, snd_config_t *conf, int mode)
{
	snd_config_iterator_t i, next;
	long card = -1;
	const char *str;
	int err;
	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)
			continue;
		if (strcmp(id, "type") == 0)
			continue;
		if (strcmp(id, "card") == 0) {
			err = snd_config_get_integer(n, &card);
			if (err < 0) {
				err = snd_config_get_string(n, &str);
				if (err < 0)
					return -EINVAL;
				card = snd_card_get_index(str);
				if (card < 0)
					return card;
			}
			continue;
		}
		return -EINVAL;
	}
	if (card < 0)
		return -EINVAL;
	return snd_ctl_hw_open(handlep, name, card, mode);
}
SND_DLSYM_BUILD_VERSION(_snd_ctl_hw_open, SND_CONTROL_DLSYM_VERSION);