summaryrefslogtreecommitdiff
path: root/src/seq/seq_hw.c
blob: a66376650bbe54a2e68e5c41778417248713ee85 (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
/*
 *  Sequencer Interface - main file
 *  Copyright (c) 2000 by Jaroslav Kysela <perex@suse.cz>
 *                        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 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 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <fcntl.h>
#include <sys/ioctl.h>
#include "seq_local.h"

#define SNDRV_FILE_SEQ		"/dev/snd/seq"
#define SNDRV_FILE_ALOADSEQ	"/dev/aloadSEQ"
#define SNDRV_SEQ_VERSION_MAX	SNDRV_PROTOCOL_VERSION(1, 0, 0)

typedef struct {
	int fd;
} snd_seq_hw_t;

static int snd_seq_hw_close(snd_seq_t *seq)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (close(hw->fd)) {
		SYSERR("close failed\n");
		return -errno;
	}
	free(hw);
	return 0;
}

static int snd_seq_hw_nonblock(snd_seq_t *seq, int nonblock)
{
	snd_seq_hw_t *hw = seq->private_data;
	long flags;

	if ((flags = fcntl(hw->fd, F_GETFL)) < 0) {
		SYSERR("F_GETFL failed");
		return -errno;
	}
	if (nonblock)
		flags |= O_NONBLOCK;
	else
		flags &= ~O_NONBLOCK;
	if (fcntl(hw->fd, F_SETFL, flags) < 0) {
		SYSERR("F_SETFL for O_NONBLOCK failed");
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_client_id(snd_seq_t *seq)
{
	snd_seq_hw_t *hw = seq->private_data;
	int client;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_CLIENT_ID, &client) < 0) {
		SYSERR("SNDRV_SEQ_IOCTL_CLIENT_ID failed");
		return -errno;
	}
	return client;
}

static int snd_seq_hw_system_info(snd_seq_t *seq, snd_seq_system_info_t * info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SYSTEM_INFO, info) < 0) {
		SYSERR("SNDRV_SEQ_IOCTL_SYSTEM_INFO failed");
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_client_info(snd_seq_t *seq, snd_seq_client_info_t * info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_CLIENT_INFO failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_set_client_info(snd_seq_t *seq, snd_seq_client_info_t * info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_SET_CLIENT_INFO failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_create_port(snd_seq_t *seq, snd_seq_port_info_t * port)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_CREATE_PORT, port) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_CREATE_PORT failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_delete_port(snd_seq_t *seq, snd_seq_port_info_t * port)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_DELETE_PORT, port) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_DELETE_PORT failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_port_info(snd_seq_t *seq, snd_seq_port_info_t * info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_PORT_INFO, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_PORT_INFO failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_set_port_info(snd_seq_t *seq, snd_seq_port_info_t * info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SET_PORT_INFO, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_SET_PORT_INFO failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_port_subscription(snd_seq_t *seq, snd_seq_port_subscribe_t * sub)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, sub) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_subscribe_port(snd_seq_t *seq, snd_seq_port_subscribe_t * sub)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, sub) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_unsubscribe_port(snd_seq_t *seq, snd_seq_port_subscribe_t * sub)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, sub) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_query_port_subscribers(snd_seq_t *seq, snd_seq_query_subscribe_t * subs)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_QUERY_SUBS, subs) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_QUERY_SUBS failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_queue_status(snd_seq_t *seq, snd_seq_queue_status_t * status)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, status) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_queue_tempo(snd_seq_t *seq, snd_seq_queue_tempo_t * tempo)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, tempo) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_set_queue_tempo(snd_seq_t *seq, snd_seq_queue_tempo_t * tempo)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, tempo) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_queue_timer(snd_seq_t *seq, snd_seq_queue_timer_t * timer)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, timer) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_set_queue_timer(snd_seq_t *seq, snd_seq_queue_timer_t * timer)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, timer) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_queue_client(snd_seq_t *seq, snd_seq_queue_client_t * info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_set_queue_client(snd_seq_t *seq, snd_seq_queue_client_t * info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_create_queue(snd_seq_t *seq, snd_seq_queue_info_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_CREATE_QUEUE, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_CREATE_QUEUE failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_delete_queue(snd_seq_t *seq, snd_seq_queue_info_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_DELETE_QUEUE, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_DELETE_QUEUE failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_queue_info(snd_seq_t *seq, snd_seq_queue_info_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_QUEUE_INFO failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_set_queue_info(snd_seq_t *seq, snd_seq_queue_info_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_SET_QUEUE_INFO failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_named_queue(snd_seq_t *seq, snd_seq_queue_info_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE failed");*/
		return -errno;
	}
	return 0;
}

static ssize_t snd_seq_hw_write(snd_seq_t *seq, void *buf, size_t len)
{
	snd_seq_hw_t *hw = seq->private_data;
	ssize_t result = write(hw->fd, buf, len);
	if (result < 0)
		return -errno;
	return result;
}

static ssize_t snd_seq_hw_read(snd_seq_t *seq, void *buf, size_t len)
{
	snd_seq_hw_t *hw = seq->private_data;
	ssize_t result = read(hw->fd, buf, len);
	if (result < 0)
		return -errno;
	return result;
}

static int snd_seq_hw_remove_events(snd_seq_t *seq, snd_seq_remove_events_t *rmp)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_REMOVE_EVENTS, rmp) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_REMOVE_EVENTS failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_get_client_pool(snd_seq_t *seq, snd_seq_client_pool_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_GET_CLIENT_POOL failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_set_client_pool(snd_seq_t *seq, snd_seq_client_pool_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_SET_CLIENT_POOL failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_query_next_client(snd_seq_t *seq, snd_seq_client_info_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT failed");*/
		return -errno;
	}
	return 0;
}

static int snd_seq_hw_query_next_port(snd_seq_t *seq, snd_seq_port_info_t *info)
{
	snd_seq_hw_t *hw = seq->private_data;
	if (ioctl(hw->fd, SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, info) < 0) {
		/*SYSERR("SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT failed");*/
		return -errno;
	}
	return 0;
}

snd_seq_ops_t snd_seq_hw_ops = {
	close: snd_seq_hw_close,
	nonblock: snd_seq_hw_nonblock,
	system_info: snd_seq_hw_system_info,
	get_client_info: snd_seq_hw_get_client_info,
	set_client_info: snd_seq_hw_set_client_info,
	create_port: snd_seq_hw_create_port,
	delete_port: snd_seq_hw_delete_port,
	get_port_info: snd_seq_hw_get_port_info,
	set_port_info: snd_seq_hw_set_port_info,
	get_port_subscription: snd_seq_hw_get_port_subscription,
	subscribe_port: snd_seq_hw_subscribe_port,
	unsubscribe_port: snd_seq_hw_unsubscribe_port,
	query_port_subscribers: snd_seq_hw_query_port_subscribers,
	get_queue_status: snd_seq_hw_get_queue_status,
	get_queue_tempo: snd_seq_hw_get_queue_tempo,
	set_queue_tempo: snd_seq_hw_set_queue_tempo,
	get_queue_timer: snd_seq_hw_get_queue_timer,
	set_queue_timer: snd_seq_hw_set_queue_timer,
	get_queue_client: snd_seq_hw_get_queue_client,
	set_queue_client: snd_seq_hw_set_queue_client,
	create_queue: snd_seq_hw_create_queue,
	delete_queue: snd_seq_hw_delete_queue,
	get_queue_info: snd_seq_hw_get_queue_info,
	set_queue_info: snd_seq_hw_set_queue_info,
	get_named_queue: snd_seq_hw_get_named_queue,
	write: snd_seq_hw_write,
	read: snd_seq_hw_read,
	remove_events: snd_seq_hw_remove_events,
	get_client_pool: snd_seq_hw_get_client_pool,
	set_client_pool: snd_seq_hw_set_client_pool,
	query_next_client: snd_seq_hw_query_next_client,
	query_next_port: snd_seq_hw_query_next_port,
};

int snd_seq_hw_open(snd_seq_t **handle, const char *name, int streams, int mode)
{
	int fd, ver, client, fmode;
	char filename[32];
	snd_seq_t *seq;
	snd_seq_hw_t *hw;

	*handle = NULL;

	switch (streams) {
	case SND_SEQ_OPEN_OUTPUT:
		fmode = O_WRONLY;
		break;
	case SND_SEQ_OPEN_INPUT:
		fmode = O_RDONLY;
		break;
	case SND_SEQ_OPEN_DUPLEX:
		fmode = O_RDWR;
		break;
	default:
		assert(0);
		return -EINVAL;
	}
	
	if (mode & SND_SEQ_NONBLOCK)
		fmode |= O_NONBLOCK;

	sprintf(filename, SNDRV_FILE_SEQ);
	if ((fd = open(filename, fmode)) < 0) {
		close(open(SNDRV_FILE_ALOADSEQ, O_RDWR));
		if ((fd = open(filename, fmode)) < 0) {
			SYSERR("open %s failed", filename);
			return -errno;
		}
	}
	if (ioctl(fd, SNDRV_SEQ_IOCTL_PVERSION, &ver) < 0) {
		SYSERR("SNDRV_SEQ_IOCTL_PVERSION failed");
		close(fd);
		return -errno;
	}
	if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_SEQ_VERSION_MAX)) {
		close(fd);
		return -SND_ERROR_INCOMPATIBLE_VERSION;
	}
	hw = calloc(1, sizeof(snd_seq_hw_t));
	if (hw == NULL) {
		close(fd);
		return -ENOMEM;
	}

	seq = calloc(1, sizeof(snd_seq_t));
	if (seq == NULL) {
		free(hw);
		close(fd);
		return -ENOMEM;
	}
	hw->fd = fd;
	if (streams & SND_SEQ_OPEN_OUTPUT) {
		seq->obuf = (char *) malloc(seq->obufsize = SND_SEQ_OBUF_SIZE);
		if (!seq->obuf) {
			free(hw);
			free(seq);
			close(fd);
			return -ENOMEM;
		}
	}
	if (streams & SND_SEQ_OPEN_INPUT) {
		seq->ibuf = (snd_seq_event_t *) calloc(sizeof(snd_seq_event_t), seq->ibufsize = SND_SEQ_IBUF_SIZE);
		if (!seq->ibuf) {
			free(seq->ibuf);
			free(hw);
			free(seq);
			close(fd);
			return -ENOMEM;
		}
	}
	if (name)
		seq->name = strdup(name);
	seq->type = SND_SEQ_TYPE_HW;
	seq->streams = streams;
	seq->mode = mode;
	seq->tmpbuf = NULL;
	seq->tmpbufsize = 0;
	seq->poll_fd = fd;
	seq->ops = &snd_seq_hw_ops;
	seq->private_data = hw;
	client = snd_seq_hw_client_id(seq);
	if (client < 0) {
		snd_seq_close(seq);
		return client;
	} else
		seq->client = client;
	*handle = seq;
	return 0;
}

SND_DLSYM_BUILD_VERSION(_snd_seq_hw_open, SND_SEQ_DLSYM_VERSION);
int _snd_seq_hw_open(snd_seq_t **handlep, char *name,
		     snd_config_t *root ATTRIBUTE_UNUSED, snd_config_t *conf,
		     int streams, int mode)
{
	snd_config_iterator_t i, next;
	snd_config_for_each(i, next, conf) {
		snd_config_t *n = snd_config_iterator_entry(i);
		const char *id = snd_config_get_id(n);
		if (strcmp(id, "comment") == 0)
			continue;
		if (strcmp(id, "type") == 0)
			continue;
		return -EINVAL;
	}
	return snd_seq_hw_open(handlep, name, streams, mode);
}