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
|
/*
* Timer Interface - main file
* Copyright (c) 1998-2001 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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 <string.h>
#define __USE_GNU
#include <fcntl.h>
#include <sys/ioctl.h>
#include "timer_local.h"
#ifndef PIC
/* entry for static linking */
const char *_snd_module_timer_hw = "";
#endif
#define SNDRV_FILE_TIMER ALSA_DEVICE_DIRECTORY "timer"
#define SNDRV_TIMER_VERSION_MAX SNDRV_PROTOCOL_VERSION(2, 0, 5)
#define SNDRV_TIMER_IOCTL_STATUS_OLD _IOW('T', 0x14, struct sndrv_timer_status)
enum {
SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
};
static int snd_timer_hw_close(snd_timer_t *handle)
{
snd_timer_t *tmr = handle;
int res;
if (!tmr)
return -EINVAL;
res = close(tmr->poll_fd) < 0 ? -errno : 0;
return res;
}
static int snd_timer_hw_nonblock(snd_timer_t *timer, int nonblock)
{
long flags;
assert(timer);
if ((flags = fcntl(timer->poll_fd, F_GETFL)) < 0)
return -errno;
if (nonblock)
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
if (fcntl(timer->poll_fd, F_SETFL, flags) < 0)
return -errno;
return 0;
}
static int snd_timer_hw_async(snd_timer_t *timer, int sig, pid_t pid)
{
long flags;
int fd;
assert(timer);
fd = timer->poll_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_timer_hw_info(snd_timer_t *handle, snd_timer_info_t * info)
{
snd_timer_t *tmr;
tmr = handle;
if (!tmr || !info)
return -EINVAL;
if (ioctl(tmr->poll_fd, SNDRV_TIMER_IOCTL_INFO, info) < 0)
return -errno;
return 0;
}
static int snd_timer_hw_params(snd_timer_t *handle, snd_timer_params_t * params)
{
snd_timer_t *tmr;
tmr = handle;
if (!tmr || !params)
return -EINVAL;
if (ioctl(tmr->poll_fd, SNDRV_TIMER_IOCTL_PARAMS, params) < 0)
return -errno;
return 0;
}
static int snd_timer_hw_status(snd_timer_t *handle, snd_timer_status_t * status)
{
snd_timer_t *tmr;
int cmd;
tmr = handle;
if (!tmr || !status)
return -EINVAL;
if (tmr->version < SNDRV_PROTOCOL_VERSION(2, 0, 1))
cmd = SNDRV_TIMER_IOCTL_STATUS_OLD;
else
cmd = SNDRV_TIMER_IOCTL_STATUS;
if (ioctl(tmr->poll_fd, cmd, status) < 0)
return -errno;
return 0;
}
static int snd_timer_hw_start(snd_timer_t *handle)
{
snd_timer_t *tmr;
unsigned int cmd;
tmr = handle;
if (!tmr)
return -EINVAL;
if (tmr->version < SNDRV_PROTOCOL_VERSION(2, 0, 4))
cmd = SNDRV_TIMER_IOCTL_START_OLD;
else
cmd = SNDRV_TIMER_IOCTL_START;
if (ioctl(tmr->poll_fd, cmd) < 0)
return -errno;
return 0;
}
static int snd_timer_hw_stop(snd_timer_t *handle)
{
snd_timer_t *tmr;
unsigned int cmd;
tmr = handle;
if (!tmr)
return -EINVAL;
if (tmr->version < SNDRV_PROTOCOL_VERSION(2, 0, 4))
cmd = SNDRV_TIMER_IOCTL_STOP_OLD;
else
cmd = SNDRV_TIMER_IOCTL_STOP;
if (ioctl(tmr->poll_fd, cmd) < 0)
return -errno;
return 0;
}
static int snd_timer_hw_continue(snd_timer_t *handle)
{
snd_timer_t *tmr;
unsigned int cmd;
tmr = handle;
if (!tmr)
return -EINVAL;
if (tmr->version < SNDRV_PROTOCOL_VERSION(2, 0, 4))
cmd = SNDRV_TIMER_IOCTL_CONTINUE_OLD;
else
cmd = SNDRV_TIMER_IOCTL_CONTINUE;
if (ioctl(tmr->poll_fd, cmd) < 0)
return -errno;
return 0;
}
static ssize_t snd_timer_hw_read(snd_timer_t *handle, void *buffer, size_t size)
{
snd_timer_t *tmr;
ssize_t result;
tmr = handle;
if (!tmr || (!buffer && size > 0))
return -EINVAL;
result = read(tmr->poll_fd, buffer, size);
if (result < 0)
return -errno;
return result;
}
static snd_timer_ops_t snd_timer_hw_ops = {
.close = snd_timer_hw_close,
.nonblock = snd_timer_hw_nonblock,
.async = snd_timer_hw_async,
.info = snd_timer_hw_info,
.params = snd_timer_hw_params,
.status = snd_timer_hw_status,
.rt_start = snd_timer_hw_start,
.rt_stop = snd_timer_hw_stop,
.rt_continue = snd_timer_hw_continue,
.read = snd_timer_hw_read,
};
int snd_timer_hw_open(snd_timer_t **handle, const char *name, int dev_class, int dev_sclass, int card, int device, int subdevice, int mode)
{
int fd, ver, tmode, ret;
snd_timer_t *tmr;
struct sndrv_timer_select sel;
*handle = NULL;
tmode = O_RDONLY;
if (mode & SND_TIMER_OPEN_NONBLOCK)
tmode |= O_NONBLOCK;
fd = snd_open_device(SNDRV_FILE_TIMER, tmode);
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");
ret = -errno;
close(fd);
return ret;
}
#endif
if (ioctl(fd, SNDRV_TIMER_IOCTL_PVERSION, &ver) < 0) {
ret = -errno;
close(fd);
return ret;
}
if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_TIMER_VERSION_MAX)) {
close(fd);
return -SND_ERROR_INCOMPATIBLE_VERSION;
}
if (mode & SND_TIMER_OPEN_TREAD) {
int arg = 1;
if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 3)) {
ret = -ENOTTY;
goto __no_tread;
}
if (ioctl(fd, SNDRV_TIMER_IOCTL_TREAD, &arg) < 0) {
ret = -errno;
close(fd);
__no_tread:
SNDMSG("extended read is not supported (SNDRV_TIMER_IOCTL_TREAD)");
return ret;
}
}
memset(&sel, 0, sizeof(sel));
sel.id.dev_class = dev_class;
sel.id.dev_sclass = dev_sclass;
sel.id.card = card;
sel.id.device = device;
sel.id.subdevice = subdevice;
if (ioctl(fd, SNDRV_TIMER_IOCTL_SELECT, &sel) < 0) {
ret = -errno;
close(fd);
return ret;
}
tmr = (snd_timer_t *) calloc(1, sizeof(snd_timer_t));
if (tmr == NULL) {
close(fd);
return -ENOMEM;
}
tmr->type = SND_TIMER_TYPE_HW;
tmr->version = ver;
tmr->mode = tmode;
tmr->name = strdup(name);
tmr->poll_fd = fd;
tmr->ops = &snd_timer_hw_ops;
INIT_LIST_HEAD(&tmr->async_handlers);
*handle = tmr;
return 0;
}
int _snd_timer_hw_open(snd_timer_t **timer, char *name,
snd_config_t *root ATTRIBUTE_UNUSED,
snd_config_t *conf, int mode)
{
snd_config_iterator_t i, next;
long dev_class = SND_TIMER_CLASS_GLOBAL, dev_sclass = SND_TIMER_SCLASS_NONE;
long card = 0, device = 0, subdevice = 0;
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, "class") == 0) {
err = snd_config_get_integer(n, &dev_class);
if (err < 0)
return err;
continue;
}
if (strcmp(id, "sclass") == 0) {
err = snd_config_get_integer(n, &dev_sclass);
if (err < 0)
return err;
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;
}
if (strcmp(id, "device") == 0) {
err = snd_config_get_integer(n, &device);
if (err < 0)
return err;
continue;
}
if (strcmp(id, "subdevice") == 0) {
err = snd_config_get_integer(n, &subdevice);
if (err < 0)
return err;
continue;
}
SNDERR("Unexpected field %s", id);
return -EINVAL;
}
if (card < 0)
return -EINVAL;
return snd_timer_hw_open(timer, name, dev_class, dev_sclass, card, device, subdevice, mode);
}
SND_DLSYM_BUILD_VERSION(_snd_timer_hw_open, SND_TIMER_DLSYM_VERSION);
|