blob: 18d43a0378500b2a5c3ed25c4268c2da9efd27be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/smp_lock.h>
26#include <linux/slab.h>
27#include <linux/time.h>
28#include <linux/moduleparam.h>
Paulo Marques543537b2005-06-23 00:09:02 -070029#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/timer.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/minors.h>
35#include <sound/initval.h>
36#include <linux/kmod.h>
37#ifdef CONFIG_KERNELD
38#include <linux/kerneld.h>
39#endif
40
41#if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
42#define DEFAULT_TIMER_LIMIT 3
43#elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
44#define DEFAULT_TIMER_LIMIT 2
45#else
46#define DEFAULT_TIMER_LIMIT 1
47#endif
48
49static int timer_limit = DEFAULT_TIMER_LIMIT;
50MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
51MODULE_DESCRIPTION("ALSA timer interface");
52MODULE_LICENSE("GPL");
53module_param(timer_limit, int, 0444);
54MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
55
Takashi Iwai53d2f742005-11-17 13:56:05 +010056struct snd_timer_user {
57 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020058 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned long ticks;
60 unsigned long overrun;
61 int qhead;
62 int qtail;
63 int qused;
64 int queue_size;
Takashi Iwai53d2f742005-11-17 13:56:05 +010065 struct snd_timer_read *queue;
66 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 spinlock_t qlock;
68 unsigned long last_resolution;
69 unsigned int filter;
70 struct timespec tstamp; /* trigger tstamp */
71 wait_queue_head_t qchange_sleep;
72 struct fasync_struct *fasync;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +020073 struct semaphore tread_sem;
Takashi Iwai53d2f742005-11-17 13:56:05 +010074};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* list of timers */
77static LIST_HEAD(snd_timer_list);
78
79/* list of slave instances */
80static LIST_HEAD(snd_timer_slave_list);
81
82/* lock for slave active lists */
83static DEFINE_SPINLOCK(slave_active_lock);
84
85static DECLARE_MUTEX(register_mutex);
86
Takashi Iwai53d2f742005-11-17 13:56:05 +010087static int snd_timer_free(struct snd_timer *timer);
88static int snd_timer_dev_free(struct snd_device *device);
89static int snd_timer_dev_register(struct snd_device *device);
90static int snd_timer_dev_unregister(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Takashi Iwai53d2f742005-11-17 13:56:05 +010092static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * create a timer instance with the given owner string.
96 * when timer is not NULL, increments the module counter
97 */
Takashi Iwai53d2f742005-11-17 13:56:05 +010098static struct snd_timer_instance *snd_timer_instance_new(char *owner,
99 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100101 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200102 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (timeri == NULL)
104 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700105 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (! timeri->owner) {
107 kfree(timeri);
108 return NULL;
109 }
110 INIT_LIST_HEAD(&timeri->open_list);
111 INIT_LIST_HEAD(&timeri->active_list);
112 INIT_LIST_HEAD(&timeri->ack_list);
113 INIT_LIST_HEAD(&timeri->slave_list_head);
114 INIT_LIST_HEAD(&timeri->slave_active_head);
115
116 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200117 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 kfree(timeri->owner);
119 kfree(timeri);
120 return NULL;
121 }
122
123 return timeri;
124}
125
126/*
127 * find a timer instance from the given timer id
128 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100129static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100131 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct list_head *p;
133
134 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100135 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (timer->tmr_class != tid->dev_class)
138 continue;
139 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
140 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
141 (timer->card == NULL ||
142 timer->card->number != tid->card))
143 continue;
144 if (timer->tmr_device != tid->device)
145 continue;
146 if (timer->tmr_subdevice != tid->subdevice)
147 continue;
148 return timer;
149 }
150 return NULL;
151}
152
153#ifdef CONFIG_KMOD
154
Takashi Iwai53d2f742005-11-17 13:56:05 +0100155static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 if (! current->fs->root)
158 return;
159 switch (tid->dev_class) {
160 case SNDRV_TIMER_CLASS_GLOBAL:
161 if (tid->device < timer_limit)
162 request_module("snd-timer-%i", tid->device);
163 break;
164 case SNDRV_TIMER_CLASS_CARD:
165 case SNDRV_TIMER_CLASS_PCM:
166 if (tid->card < snd_ecards_limit)
167 request_module("snd-card-%i", tid->card);
168 break;
169 default:
170 break;
171 }
172}
173
174#endif
175
176/*
177 * look for a master instance matching with the slave id of the given slave.
178 * when found, relink the open_link of the slave.
179 *
180 * call this with register_mutex down.
181 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100182static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100184 struct snd_timer *timer;
185 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct list_head *p, *q;
187
188 /* FIXME: it's really dumb to look up all entries.. */
189 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100190 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 list_for_each(q, &timer->open_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100192 master = list_entry(q, struct snd_timer_instance, open_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (slave->slave_class == master->slave_class &&
194 slave->slave_id == master->slave_id) {
195 list_del(&slave->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200196 list_add_tail(&slave->open_list,
197 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 spin_lock_irq(&slave_active_lock);
199 slave->master = master;
200 slave->timer = master->timer;
201 spin_unlock_irq(&slave_active_lock);
202 return;
203 }
204 }
205 }
206}
207
208/*
209 * look for slave instances matching with the slave id of the given master.
210 * when found, relink the open_link of slaves.
211 *
212 * call this with register_mutex down.
213 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100214static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100216 struct snd_timer_instance *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct list_head *p, *n;
218
219 /* check all pending slaves */
220 list_for_each_safe(p, n, &snd_timer_slave_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100221 slave = list_entry(p, struct snd_timer_instance, open_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (slave->slave_class == master->slave_class &&
223 slave->slave_id == master->slave_id) {
224 list_del(p);
225 list_add_tail(p, &master->slave_list_head);
226 spin_lock_irq(&slave_active_lock);
227 slave->master = master;
228 slave->timer = master->timer;
229 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200230 list_add_tail(&slave->active_list,
231 &master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_unlock_irq(&slave_active_lock);
233 }
234 }
235}
236
237/*
238 * open a timer instance
239 * when opening a master, the slave id must be here given.
240 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100241int snd_timer_open(struct snd_timer_instance **ti,
242 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned int slave_id)
244{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100245 struct snd_timer *timer;
246 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
249 /* open a slave instance */
250 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
251 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
252 snd_printd("invalid slave class %i\n", tid->dev_sclass);
253 return -EINVAL;
254 }
255 down(&register_mutex);
256 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200257 if (!timeri) {
258 up(&register_mutex);
259 return -ENOMEM;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 timeri->slave_class = tid->dev_sclass;
262 timeri->slave_id = tid->device;
263 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
264 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
265 snd_timer_check_slave(timeri);
266 up(&register_mutex);
267 *ti = timeri;
268 return 0;
269 }
270
271 /* open a master instance */
272 down(&register_mutex);
273 timer = snd_timer_find(tid);
274#ifdef CONFIG_KMOD
275 if (timer == NULL) {
276 up(&register_mutex);
277 snd_timer_request(tid);
278 down(&register_mutex);
279 timer = snd_timer_find(tid);
280 }
281#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200282 if (!timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 up(&register_mutex);
284 return -ENODEV;
285 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200286 if (!list_empty(&timer->open_list_head)) {
287 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100288 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200289 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
290 up(&register_mutex);
291 return -EBUSY;
292 }
293 }
294 timeri = snd_timer_instance_new(owner, timer);
295 if (!timeri) {
296 up(&register_mutex);
297 return -ENOMEM;
298 }
299 timeri->slave_class = tid->dev_sclass;
300 timeri->slave_id = slave_id;
301 if (list_empty(&timer->open_list_head) && timer->hw.open)
302 timer->hw.open(timer);
303 list_add_tail(&timeri->open_list, &timer->open_list_head);
304 snd_timer_check_master(timeri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 up(&register_mutex);
306 *ti = timeri;
307 return 0;
308}
309
Takashi Iwai53d2f742005-11-17 13:56:05 +0100310static int _snd_timer_stop(struct snd_timer_instance *timeri,
311 int keep_flag, int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313/*
314 * close a timer instance
315 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100316int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100318 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct list_head *p, *n;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100320 struct snd_timer_instance *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 snd_assert(timeri != NULL, return -ENXIO);
323
324 /* force to stop the timer */
325 snd_timer_stop(timeri);
326
327 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
328 /* wait, until the active callback is finished */
329 spin_lock_irq(&slave_active_lock);
330 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
331 spin_unlock_irq(&slave_active_lock);
332 udelay(10);
333 spin_lock_irq(&slave_active_lock);
334 }
335 spin_unlock_irq(&slave_active_lock);
336 down(&register_mutex);
337 list_del(&timeri->open_list);
338 up(&register_mutex);
339 } else {
340 timer = timeri->timer;
341 /* wait, until the active callback is finished */
342 spin_lock_irq(&timer->lock);
343 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
344 spin_unlock_irq(&timer->lock);
345 udelay(10);
346 spin_lock_irq(&timer->lock);
347 }
348 spin_unlock_irq(&timer->lock);
349 down(&register_mutex);
350 list_del(&timeri->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200351 if (timer && list_empty(&timer->open_list_head) &&
352 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 timer->hw.close(timer);
354 /* remove slave links */
355 list_for_each_safe(p, n, &timeri->slave_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100356 slave = list_entry(p, struct snd_timer_instance, open_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 spin_lock_irq(&slave_active_lock);
358 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
359 list_del(p);
360 list_add_tail(p, &snd_timer_slave_list);
361 slave->master = NULL;
362 slave->timer = NULL;
363 spin_unlock_irq(&slave_active_lock);
364 }
365 up(&register_mutex);
366 }
367 if (timeri->private_free)
368 timeri->private_free(timeri);
369 kfree(timeri->owner);
370 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200371 if (timer)
372 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
Takashi Iwai53d2f742005-11-17 13:56:05 +0100376unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100378 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (timeri == NULL)
381 return 0;
382 if ((timer = timeri->timer) != NULL) {
383 if (timer->hw.c_resolution)
384 return timer->hw.c_resolution(timer);
385 return timer->hw.resolution;
386 }
387 return 0;
388}
389
Takashi Iwai53d2f742005-11-17 13:56:05 +0100390static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100392 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned long flags;
394 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100395 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct list_head *n;
397 struct timespec tstamp;
398
Takashi Iwai07799e72005-10-10 11:49:49 +0200399 getnstimeofday(&tstamp);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200400 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
401 event <= SNDRV_TIMER_EVENT_PAUSE, return);
402 if (event == SNDRV_TIMER_EVENT_START ||
403 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 resolution = snd_timer_resolution(ti);
405 if (ti->ccallback)
406 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
407 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
408 return;
409 timer = ti->timer;
410 if (timer == NULL)
411 return;
412 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
413 return;
414 spin_lock_irqsave(&timer->lock, flags);
415 list_for_each(n, &ti->slave_active_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100416 ts = list_entry(n, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (ts->ccallback)
418 ts->ccallback(ti, event + 100, &tstamp, resolution);
419 }
420 spin_unlock_irqrestore(&timer->lock, flags);
421}
422
Takashi Iwai53d2f742005-11-17 13:56:05 +0100423static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200424 unsigned long sticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 list_del(&timeri->active_list);
427 list_add_tail(&timeri->active_list, &timer->active_list_head);
428 if (timer->running) {
429 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
430 goto __start_now;
431 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
432 timeri->flags |= SNDRV_TIMER_IFLG_START;
433 return 1; /* delayed start */
434 } else {
435 timer->sticks = sticks;
436 timer->hw.start(timer);
437 __start_now:
438 timer->running++;
439 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
440 return 0;
441 }
442}
443
Takashi Iwai53d2f742005-11-17 13:56:05 +0100444static int snd_timer_start_slave(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 unsigned long flags;
447
448 spin_lock_irqsave(&slave_active_lock, flags);
449 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
450 if (timeri->master)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200451 list_add_tail(&timeri->active_list,
452 &timeri->master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 spin_unlock_irqrestore(&slave_active_lock, flags);
454 return 1; /* delayed start */
455}
456
457/*
458 * start the timer instance
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200459 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100460int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100462 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int result = -EINVAL;
464 unsigned long flags;
465
466 if (timeri == NULL || ticks < 1)
467 return -EINVAL;
468 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
469 result = snd_timer_start_slave(timeri);
470 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
471 return result;
472 }
473 timer = timeri->timer;
474 if (timer == NULL)
475 return -EINVAL;
476 spin_lock_irqsave(&timer->lock, flags);
477 timeri->ticks = timeri->cticks = ticks;
478 timeri->pticks = 0;
479 result = snd_timer_start1(timer, timeri, ticks);
480 spin_unlock_irqrestore(&timer->lock, flags);
481 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
482 return result;
483}
484
Takashi Iwai53d2f742005-11-17 13:56:05 +0100485static int _snd_timer_stop(struct snd_timer_instance * timeri,
486 int keep_flag, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100488 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned long flags;
490
491 snd_assert(timeri != NULL, return -ENXIO);
492
493 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
494 if (!keep_flag) {
495 spin_lock_irqsave(&slave_active_lock, flags);
496 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
497 spin_unlock_irqrestore(&slave_active_lock, flags);
498 }
499 goto __end;
500 }
501 timer = timeri->timer;
502 if (!timer)
503 return -EINVAL;
504 spin_lock_irqsave(&timer->lock, flags);
505 list_del_init(&timeri->ack_list);
506 list_del_init(&timeri->active_list);
507 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
508 !(--timer->running)) {
509 timer->hw.stop(timer);
510 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
511 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
512 snd_timer_reschedule(timer, 0);
513 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
514 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
515 timer->hw.start(timer);
516 }
517 }
518 }
519 if (!keep_flag)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200520 timeri->flags &=
521 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 spin_unlock_irqrestore(&timer->lock, flags);
523 __end:
524 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
525 snd_timer_notify1(timeri, event);
526 return 0;
527}
528
529/*
530 * stop the timer instance.
531 *
532 * do not call this from the timer callback!
533 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100534int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100536 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 unsigned long flags;
538 int err;
539
540 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
541 if (err < 0)
542 return err;
543 timer = timeri->timer;
544 spin_lock_irqsave(&timer->lock, flags);
545 timeri->cticks = timeri->ticks;
546 timeri->pticks = 0;
547 spin_unlock_irqrestore(&timer->lock, flags);
548 return 0;
549}
550
551/*
552 * start again.. the tick is kept.
553 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100554int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100556 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int result = -EINVAL;
558 unsigned long flags;
559
560 if (timeri == NULL)
561 return result;
562 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
563 return snd_timer_start_slave(timeri);
564 timer = timeri->timer;
565 if (! timer)
566 return -EINVAL;
567 spin_lock_irqsave(&timer->lock, flags);
568 if (!timeri->cticks)
569 timeri->cticks = 1;
570 timeri->pticks = 0;
571 result = snd_timer_start1(timer, timeri, timer->sticks);
572 spin_unlock_irqrestore(&timer->lock, flags);
573 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
574 return result;
575}
576
577/*
578 * pause.. remember the ticks left
579 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100580int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
583}
584
585/*
586 * reschedule the timer
587 *
588 * start pending instances and check the scheduling ticks.
589 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
590 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100591static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100593 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 unsigned long ticks = ~0UL;
595 struct list_head *p;
596
597 list_for_each(p, &timer->active_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100598 ti = list_entry(p, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (ti->flags & SNDRV_TIMER_IFLG_START) {
600 ti->flags &= ~SNDRV_TIMER_IFLG_START;
601 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
602 timer->running++;
603 }
604 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
605 if (ticks > ti->cticks)
606 ticks = ti->cticks;
607 }
608 }
609 if (ticks == ~0UL) {
610 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
611 return;
612 }
613 if (ticks > timer->hw.ticks)
614 ticks = timer->hw.ticks;
615 if (ticks_left != ticks)
616 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
617 timer->sticks = ticks;
618}
619
620/*
621 * timer tasklet
622 *
623 */
624static void snd_timer_tasklet(unsigned long arg)
625{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100626 struct snd_timer *timer = (struct snd_timer *) arg;
627 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct list_head *p;
629 unsigned long resolution, ticks;
630
631 spin_lock(&timer->lock);
632 /* now process all callbacks */
633 while (!list_empty(&timer->sack_list_head)) {
634 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100635 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* remove from ack_list and make empty */
638 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ticks = ti->pticks;
641 ti->pticks = 0;
642 resolution = ti->resolution;
643
644 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
645 spin_unlock(&timer->lock);
646 if (ti->callback)
647 ti->callback(ti, resolution, ticks);
648 spin_lock(&timer->lock);
649 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
650 }
651 spin_unlock(&timer->lock);
652}
653
654/*
655 * timer interrupt
656 *
657 * ticks_left is usually equal to timer->sticks.
658 *
659 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100660void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100662 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 unsigned long resolution, ticks;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200664 struct list_head *p, *q, *n, *ack_list_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int use_tasklet = 0;
666
667 if (timer == NULL)
668 return;
669
670 spin_lock(&timer->lock);
671
672 /* remember the current resolution */
673 if (timer->hw.c_resolution)
674 resolution = timer->hw.c_resolution(timer);
675 else
676 resolution = timer->hw.resolution;
677
678 /* loop for all active instances
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200679 * Here we cannot use list_for_each because the active_list of a
680 * processed instance is relinked to done_list_head before the callback
681 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
683 list_for_each_safe(p, n, &timer->active_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100684 ti = list_entry(p, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
686 continue;
687 ti->pticks += ticks_left;
688 ti->resolution = resolution;
689 if (ti->cticks < ticks_left)
690 ti->cticks = 0;
691 else
692 ti->cticks -= ticks_left;
693 if (ti->cticks) /* not expired */
694 continue;
695 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
696 ti->cticks = ti->ticks;
697 } else {
698 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
699 if (--timer->running)
700 list_del(p);
701 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200702 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
703 (ti->flags & SNDRV_TIMER_IFLG_FAST))
704 ack_list_head = &timer->ack_list_head;
705 else
706 ack_list_head = &timer->sack_list_head;
707 if (list_empty(&ti->ack_list))
708 list_add_tail(&ti->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 list_for_each(q, &ti->slave_active_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100710 ts = list_entry(q, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 ts->pticks = ti->pticks;
712 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200713 if (list_empty(&ts->ack_list))
714 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 }
717 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
718 snd_timer_reschedule(timer, ticks_left);
719 if (timer->running) {
720 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
721 timer->hw.stop(timer);
722 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
723 }
724 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
725 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
726 /* restart timer */
727 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
728 timer->hw.start(timer);
729 }
730 } else {
731 timer->hw.stop(timer);
732 }
733
734 /* now process all fast callbacks */
735 while (!list_empty(&timer->ack_list_head)) {
736 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100737 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* remove from ack_list and make empty */
740 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ticks = ti->pticks;
743 ti->pticks = 0;
744
745 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
746 spin_unlock(&timer->lock);
747 if (ti->callback)
748 ti->callback(ti, resolution, ticks);
749 spin_lock(&timer->lock);
750 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
751 }
752
753 /* do we have any slow callbacks? */
754 use_tasklet = !list_empty(&timer->sack_list_head);
755 spin_unlock(&timer->lock);
756
757 if (use_tasklet)
758 tasklet_hi_schedule(&timer->task_queue);
759}
760
761/*
762
763 */
764
Takashi Iwai53d2f742005-11-17 13:56:05 +0100765int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
766 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100768 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100770 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 .dev_free = snd_timer_dev_free,
772 .dev_register = snd_timer_dev_register,
773 .dev_unregister = snd_timer_dev_unregister
774 };
775
776 snd_assert(tid != NULL, return -EINVAL);
777 snd_assert(rtimer != NULL, return -EINVAL);
778 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200779 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (timer == NULL)
781 return -ENOMEM;
782 timer->tmr_class = tid->dev_class;
783 timer->card = card;
784 timer->tmr_device = tid->device;
785 timer->tmr_subdevice = tid->subdevice;
786 if (id)
787 strlcpy(timer->id, id, sizeof(timer->id));
788 INIT_LIST_HEAD(&timer->device_list);
789 INIT_LIST_HEAD(&timer->open_list_head);
790 INIT_LIST_HEAD(&timer->active_list_head);
791 INIT_LIST_HEAD(&timer->ack_list_head);
792 INIT_LIST_HEAD(&timer->sack_list_head);
793 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200794 tasklet_init(&timer->task_queue, snd_timer_tasklet,
795 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200797 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200798 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
799 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 snd_timer_free(timer);
801 return err;
802 }
803 }
804 *rtimer = timer;
805 return 0;
806}
807
Takashi Iwai53d2f742005-11-17 13:56:05 +0100808static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 snd_assert(timer != NULL, return -ENXIO);
811 if (timer->private_free)
812 timer->private_free(timer);
813 kfree(timer);
814 return 0;
815}
816
Takashi Iwai53d2f742005-11-17 13:56:05 +0100817static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100819 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return snd_timer_free(timer);
821}
822
Takashi Iwai53d2f742005-11-17 13:56:05 +0100823static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100825 struct snd_timer *timer = dev->device_data;
826 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct list_head *p;
828
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200829 snd_assert(timer != NULL && timer->hw.start != NULL &&
830 timer->hw.stop != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
832 !timer->hw.resolution && timer->hw.c_resolution == NULL)
833 return -EINVAL;
834
835 down(&register_mutex);
836 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100837 timer1 = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (timer1->tmr_class > timer->tmr_class)
839 break;
840 if (timer1->tmr_class < timer->tmr_class)
841 continue;
842 if (timer1->card && timer->card) {
843 if (timer1->card->number > timer->card->number)
844 break;
845 if (timer1->card->number < timer->card->number)
846 continue;
847 }
848 if (timer1->tmr_device > timer->tmr_device)
849 break;
850 if (timer1->tmr_device < timer->tmr_device)
851 continue;
852 if (timer1->tmr_subdevice > timer->tmr_subdevice)
853 break;
854 if (timer1->tmr_subdevice < timer->tmr_subdevice)
855 continue;
856 /* conflicts.. */
857 up(&register_mutex);
858 return -EBUSY;
859 }
860 list_add_tail(&timer->device_list, p);
861 up(&register_mutex);
862 return 0;
863}
864
Takashi Iwai53d2f742005-11-17 13:56:05 +0100865static int snd_timer_unregister(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct list_head *p, *n;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100868 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 snd_assert(timer != NULL, return -ENXIO);
871 down(&register_mutex);
872 if (! list_empty(&timer->open_list_head)) {
873 snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
874 list_for_each_safe(p, n, &timer->open_list_head) {
875 list_del_init(p);
Takashi Iwai53d2f742005-11-17 13:56:05 +0100876 ti = list_entry(p, struct snd_timer_instance, open_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ti->timer = NULL;
878 }
879 }
880 list_del(&timer->device_list);
881 up(&register_mutex);
882 return snd_timer_free(timer);
883}
884
Takashi Iwai53d2f742005-11-17 13:56:05 +0100885static int snd_timer_dev_unregister(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100887 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return snd_timer_unregister(timer);
889}
890
Takashi Iwai53d2f742005-11-17 13:56:05 +0100891void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 unsigned long flags;
894 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100895 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 struct list_head *p, *n;
897
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200898 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
899 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200900 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
901 event <= SNDRV_TIMER_EVENT_MRESUME, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200903 if (event == SNDRV_TIMER_EVENT_MSTART ||
904 event == SNDRV_TIMER_EVENT_MCONTINUE ||
905 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (timer->hw.c_resolution)
907 resolution = timer->hw.c_resolution(timer);
908 else
909 resolution = timer->hw.resolution;
910 }
911 list_for_each(p, &timer->active_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100912 ti = list_entry(p, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (ti->ccallback)
914 ti->ccallback(ti, event, tstamp, resolution);
915 list_for_each(n, &ti->slave_active_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100916 ts = list_entry(n, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (ts->ccallback)
918 ts->ccallback(ts, event, tstamp, resolution);
919 }
920 }
921 spin_unlock_irqrestore(&timer->lock, flags);
922}
923
924/*
925 * exported functions for global timers
926 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100927int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100929 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
932 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
933 tid.card = -1;
934 tid.device = device;
935 tid.subdevice = 0;
936 return snd_timer_new(NULL, id, &tid, rtimer);
937}
938
Takashi Iwai53d2f742005-11-17 13:56:05 +0100939int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 return snd_timer_free(timer);
942}
943
Takashi Iwai53d2f742005-11-17 13:56:05 +0100944int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100946 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 memset(&dev, 0, sizeof(dev));
949 dev.device_data = timer;
950 return snd_timer_dev_register(&dev);
951}
952
Takashi Iwai53d2f742005-11-17 13:56:05 +0100953int snd_timer_global_unregister(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 return snd_timer_unregister(timer);
956}
957
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200958/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * System timer
960 */
961
962struct snd_timer_system_private {
963 struct timer_list tlist;
964 struct timer * timer;
965 unsigned long last_expires;
966 unsigned long last_jiffies;
967 unsigned long correction;
968};
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static void snd_timer_s_function(unsigned long data)
971{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100972 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct snd_timer_system_private *priv = timer->private_data;
974 unsigned long jiff = jiffies;
975 if (time_after(jiff, priv->last_expires))
976 priv->correction = (long)jiff - (long)priv->last_expires;
977 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
978}
979
Takashi Iwai53d2f742005-11-17 13:56:05 +0100980static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 struct snd_timer_system_private *priv;
983 unsigned long njiff;
984
985 priv = (struct snd_timer_system_private *) timer->private_data;
986 njiff = (priv->last_jiffies = jiffies);
987 if (priv->correction > timer->sticks - 1) {
988 priv->correction -= timer->sticks - 1;
989 njiff++;
990 } else {
991 njiff += timer->sticks - priv->correction;
992 priv->correction -= timer->sticks;
993 }
994 priv->last_expires = priv->tlist.expires = njiff;
995 add_timer(&priv->tlist);
996 return 0;
997}
998
Takashi Iwai53d2f742005-11-17 13:56:05 +0100999static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 struct snd_timer_system_private *priv;
1002 unsigned long jiff;
1003
1004 priv = (struct snd_timer_system_private *) timer->private_data;
1005 del_timer(&priv->tlist);
1006 jiff = jiffies;
1007 if (time_before(jiff, priv->last_expires))
1008 timer->sticks = priv->last_expires - jiff;
1009 else
1010 timer->sticks = 1;
1011 return 0;
1012}
1013
Takashi Iwai53d2f742005-11-17 13:56:05 +01001014static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1017 .resolution = 1000000000L / HZ,
1018 .ticks = 10000000L,
1019 .start = snd_timer_s_start,
1020 .stop = snd_timer_s_stop
1021};
1022
Takashi Iwai53d2f742005-11-17 13:56:05 +01001023static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 kfree(timer->private_data);
1026}
1027
1028static int snd_timer_register_system(void)
1029{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001030 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct snd_timer_system_private *priv;
1032 int err;
1033
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001034 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1035 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return err;
1037 strcpy(timer->name, "system timer");
1038 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001039 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (priv == NULL) {
1041 snd_timer_free(timer);
1042 return -ENOMEM;
1043 }
1044 init_timer(&priv->tlist);
1045 priv->tlist.function = snd_timer_s_function;
1046 priv->tlist.data = (unsigned long) timer;
1047 timer->private_data = priv;
1048 timer->private_free = snd_timer_free_system;
1049 return snd_timer_global_register(timer);
1050}
1051
1052/*
1053 * Info interface
1054 */
1055
Takashi Iwai53d2f742005-11-17 13:56:05 +01001056static void snd_timer_proc_read(struct snd_info_entry *entry,
1057 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 unsigned long flags;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001060 struct snd_timer *timer;
1061 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct list_head *p, *q;
1063
1064 down(&register_mutex);
1065 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001066 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 switch (timer->tmr_class) {
1068 case SNDRV_TIMER_CLASS_GLOBAL:
1069 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1070 break;
1071 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001072 snd_iprintf(buffer, "C%i-%i: ",
1073 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 break;
1075 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001076 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1077 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 break;
1079 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001080 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1081 timer->card ? timer->card->number : -1,
1082 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 snd_iprintf(buffer, "%s :", timer->name);
1085 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001086 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1087 timer->hw.resolution / 1000,
1088 timer->hw.resolution % 1000,
1089 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1091 snd_iprintf(buffer, " SLAVE");
1092 snd_iprintf(buffer, "\n");
1093 spin_lock_irqsave(&timer->lock, flags);
1094 list_for_each(q, &timer->open_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001095 ti = list_entry(q, struct snd_timer_instance, open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001096 snd_iprintf(buffer, " Client %s : %s\n",
1097 ti->owner ? ti->owner : "unknown",
1098 ti->flags & (SNDRV_TIMER_IFLG_START |
1099 SNDRV_TIMER_IFLG_RUNNING)
1100 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102 spin_unlock_irqrestore(&timer->lock, flags);
1103 }
1104 up(&register_mutex);
1105}
1106
1107/*
1108 * USER SPACE interface
1109 */
1110
Takashi Iwai53d2f742005-11-17 13:56:05 +01001111static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 unsigned long resolution,
1113 unsigned long ticks)
1114{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001115 struct snd_timer_user *tu = timeri->callback_data;
1116 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 spin_lock(&tu->qlock);
1120 if (tu->qused > 0) {
1121 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1122 r = &tu->queue[prev];
1123 if (r->resolution == resolution) {
1124 r->ticks += ticks;
1125 goto __wake;
1126 }
1127 }
1128 if (tu->qused >= tu->queue_size) {
1129 tu->overrun++;
1130 } else {
1131 r = &tu->queue[tu->qtail++];
1132 tu->qtail %= tu->queue_size;
1133 r->resolution = resolution;
1134 r->ticks = ticks;
1135 tu->qused++;
1136 }
1137 __wake:
1138 spin_unlock(&tu->qlock);
1139 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1140 wake_up(&tu->qchange_sleep);
1141}
1142
Takashi Iwai53d2f742005-11-17 13:56:05 +01001143static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1144 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 if (tu->qused >= tu->queue_size) {
1147 tu->overrun++;
1148 } else {
1149 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1150 tu->qtail %= tu->queue_size;
1151 tu->qused++;
1152 }
1153}
1154
Takashi Iwai53d2f742005-11-17 13:56:05 +01001155static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1156 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 struct timespec *tstamp,
1158 unsigned long resolution)
1159{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001160 struct snd_timer_user *tu = timeri->callback_data;
1161 struct snd_timer_tread r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001163 if (event >= SNDRV_TIMER_EVENT_START &&
1164 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 tu->tstamp = *tstamp;
1166 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1167 return;
1168 r1.event = event;
1169 r1.tstamp = *tstamp;
1170 r1.val = resolution;
1171 spin_lock(&tu->qlock);
1172 snd_timer_user_append_to_tqueue(tu, &r1);
1173 spin_unlock(&tu->qlock);
1174 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1175 wake_up(&tu->qchange_sleep);
1176}
1177
Takashi Iwai53d2f742005-11-17 13:56:05 +01001178static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 unsigned long resolution,
1180 unsigned long ticks)
1181{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001182 struct snd_timer_user *tu = timeri->callback_data;
1183 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct timespec tstamp;
1185 int prev, append = 0;
1186
Takashi Iwai07799e72005-10-10 11:49:49 +02001187 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001189 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1190 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 spin_unlock(&tu->qlock);
1192 return;
1193 }
1194 if (tu->last_resolution != resolution || ticks > 0)
Takashi Iwai07799e72005-10-10 11:49:49 +02001195 getnstimeofday(&tstamp);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001196 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1197 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1199 r1.tstamp = tstamp;
1200 r1.val = resolution;
1201 snd_timer_user_append_to_tqueue(tu, &r1);
1202 tu->last_resolution = resolution;
1203 append++;
1204 }
1205 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1206 goto __wake;
1207 if (ticks == 0)
1208 goto __wake;
1209 if (tu->qused > 0) {
1210 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1211 r = &tu->tqueue[prev];
1212 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1213 r->tstamp = tstamp;
1214 r->val += ticks;
1215 append++;
1216 goto __wake;
1217 }
1218 }
1219 r1.event = SNDRV_TIMER_EVENT_TICK;
1220 r1.tstamp = tstamp;
1221 r1.val = ticks;
1222 snd_timer_user_append_to_tqueue(tu, &r1);
1223 append++;
1224 __wake:
1225 spin_unlock(&tu->qlock);
1226 if (append == 0)
1227 return;
1228 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1229 wake_up(&tu->qchange_sleep);
1230}
1231
1232static int snd_timer_user_open(struct inode *inode, struct file *file)
1233{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001234 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001235
Takashi Iwaica2c0962005-09-09 14:20:23 +02001236 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (tu == NULL)
1238 return -ENOMEM;
1239 spin_lock_init(&tu->qlock);
1240 init_waitqueue_head(&tu->qchange_sleep);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001241 init_MUTEX(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 tu->ticks = 1;
1243 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001244 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001245 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (tu->queue == NULL) {
1247 kfree(tu);
1248 return -ENOMEM;
1249 }
1250 file->private_data = tu;
1251 return 0;
1252}
1253
1254static int snd_timer_user_release(struct inode *inode, struct file *file)
1255{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001256 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if (file->private_data) {
1259 tu = file->private_data;
1260 file->private_data = NULL;
1261 fasync_helper(-1, file, 0, &tu->fasync);
1262 if (tu->timeri)
1263 snd_timer_close(tu->timeri);
1264 kfree(tu->queue);
1265 kfree(tu->tqueue);
1266 kfree(tu);
1267 }
1268 return 0;
1269}
1270
Takashi Iwai53d2f742005-11-17 13:56:05 +01001271static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1274 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1275 id->card = -1;
1276 id->device = -1;
1277 id->subdevice = -1;
1278}
1279
Takashi Iwai53d2f742005-11-17 13:56:05 +01001280static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 id->dev_class = timer->tmr_class;
1283 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1284 id->card = timer->card ? timer->card->number : -1;
1285 id->device = timer->tmr_device;
1286 id->subdevice = timer->tmr_subdevice;
1287}
1288
Takashi Iwai53d2f742005-11-17 13:56:05 +01001289static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001291 struct snd_timer_id id;
1292 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (copy_from_user(&id, _tid, sizeof(id)))
1296 return -EFAULT;
1297 down(&register_mutex);
1298 if (id.dev_class < 0) { /* first item */
1299 if (list_empty(&snd_timer_list))
1300 snd_timer_user_zero_id(&id);
1301 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001302 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001303 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 snd_timer_user_copy_id(&id, timer);
1305 }
1306 } else {
1307 switch (id.dev_class) {
1308 case SNDRV_TIMER_CLASS_GLOBAL:
1309 id.device = id.device < 0 ? 0 : id.device + 1;
1310 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001311 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1313 snd_timer_user_copy_id(&id, timer);
1314 break;
1315 }
1316 if (timer->tmr_device >= id.device) {
1317 snd_timer_user_copy_id(&id, timer);
1318 break;
1319 }
1320 }
1321 if (p == &snd_timer_list)
1322 snd_timer_user_zero_id(&id);
1323 break;
1324 case SNDRV_TIMER_CLASS_CARD:
1325 case SNDRV_TIMER_CLASS_PCM:
1326 if (id.card < 0) {
1327 id.card = 0;
1328 } else {
1329 if (id.card < 0) {
1330 id.card = 0;
1331 } else {
1332 if (id.device < 0) {
1333 id.device = 0;
1334 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001335 if (id.subdevice < 0) {
1336 id.subdevice = 0;
1337 } else {
1338 id.subdevice++;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 }
1342 }
1343 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001344 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (timer->tmr_class > id.dev_class) {
1346 snd_timer_user_copy_id(&id, timer);
1347 break;
1348 }
1349 if (timer->tmr_class < id.dev_class)
1350 continue;
1351 if (timer->card->number > id.card) {
1352 snd_timer_user_copy_id(&id, timer);
1353 break;
1354 }
1355 if (timer->card->number < id.card)
1356 continue;
1357 if (timer->tmr_device > id.device) {
1358 snd_timer_user_copy_id(&id, timer);
1359 break;
1360 }
1361 if (timer->tmr_device < id.device)
1362 continue;
1363 if (timer->tmr_subdevice > id.subdevice) {
1364 snd_timer_user_copy_id(&id, timer);
1365 break;
1366 }
1367 if (timer->tmr_subdevice < id.subdevice)
1368 continue;
1369 snd_timer_user_copy_id(&id, timer);
1370 break;
1371 }
1372 if (p == &snd_timer_list)
1373 snd_timer_user_zero_id(&id);
1374 break;
1375 default:
1376 snd_timer_user_zero_id(&id);
1377 }
1378 }
1379 up(&register_mutex);
1380 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1381 return -EFAULT;
1382 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001385static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001386 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001388 struct snd_timer_ginfo *ginfo;
1389 struct snd_timer_id tid;
1390 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct list_head *p;
1392 int err = 0;
1393
1394 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1395 if (! ginfo)
1396 return -ENOMEM;
1397 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1398 kfree(ginfo);
1399 return -EFAULT;
1400 }
1401 tid = ginfo->tid;
1402 memset(ginfo, 0, sizeof(*ginfo));
1403 ginfo->tid = tid;
1404 down(&register_mutex);
1405 t = snd_timer_find(&tid);
1406 if (t != NULL) {
1407 ginfo->card = t->card ? t->card->number : -1;
1408 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1409 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1410 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1411 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1412 ginfo->resolution = t->hw.resolution;
1413 if (t->hw.resolution_min > 0) {
1414 ginfo->resolution_min = t->hw.resolution_min;
1415 ginfo->resolution_max = t->hw.resolution_max;
1416 }
1417 list_for_each(p, &t->open_list_head) {
1418 ginfo->clients++;
1419 }
1420 } else {
1421 err = -ENODEV;
1422 }
1423 up(&register_mutex);
1424 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1425 err = -EFAULT;
1426 kfree(ginfo);
1427 return err;
1428}
1429
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001430static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001431 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001433 struct snd_timer_gparams gparams;
1434 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int err;
1436
1437 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1438 return -EFAULT;
1439 down(&register_mutex);
1440 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001441 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001443 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001445 if (!list_empty(&t->open_list_head)) {
1446 err = -EBUSY;
1447 goto _error;
1448 }
1449 if (!t->hw.set_period) {
1450 err = -ENOSYS;
1451 goto _error;
1452 }
1453 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1454_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 up(&register_mutex);
1456 return err;
1457}
1458
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001459static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001460 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001462 struct snd_timer_gstatus gstatus;
1463 struct snd_timer_id tid;
1464 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int err = 0;
1466
1467 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1468 return -EFAULT;
1469 tid = gstatus.tid;
1470 memset(&gstatus, 0, sizeof(gstatus));
1471 gstatus.tid = tid;
1472 down(&register_mutex);
1473 t = snd_timer_find(&tid);
1474 if (t != NULL) {
1475 if (t->hw.c_resolution)
1476 gstatus.resolution = t->hw.c_resolution(t);
1477 else
1478 gstatus.resolution = t->hw.resolution;
1479 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001480 t->hw.precise_resolution(t, &gstatus.resolution_num,
1481 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 } else {
1483 gstatus.resolution_num = gstatus.resolution;
1484 gstatus.resolution_den = 1000000000uL;
1485 }
1486 } else {
1487 err = -ENODEV;
1488 }
1489 up(&register_mutex);
1490 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1491 err = -EFAULT;
1492 return err;
1493}
1494
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001495static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001496 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001498 struct snd_timer_user *tu;
1499 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001501 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 tu = file->private_data;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001504 down(&tu->tread_sem);
1505 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001507 tu->timeri = NULL;
1508 }
1509 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1510 err = -EFAULT;
1511 goto __err;
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 sprintf(str, "application %i", current->pid);
1514 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1515 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001516 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1517 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001518 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Jesper Juhl4d572772005-05-30 17:30:32 +02001520 kfree(tu->queue);
1521 tu->queue = NULL;
1522 kfree(tu->tqueue);
1523 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001525 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001526 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001527 if (tu->tqueue == NULL)
1528 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001530 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001531 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001532 if (tu->queue == NULL)
1533 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001535
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001536 if (err < 0) {
1537 snd_timer_close(tu->timeri);
1538 tu->timeri = NULL;
1539 } else {
1540 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001541 tu->timeri->callback = tu->tread
1542 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001543 tu->timeri->ccallback = snd_timer_user_ccallback;
1544 tu->timeri->callback_data = (void *)tu;
1545 }
1546
1547 __err:
1548 up(&tu->tread_sem);
1549 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001552static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001553 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001555 struct snd_timer_user *tu;
1556 struct snd_timer_info *info;
1557 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int err = 0;
1559
1560 tu = file->private_data;
1561 snd_assert(tu->timeri != NULL, return -ENXIO);
1562 t = tu->timeri->timer;
1563 snd_assert(t != NULL, return -ENXIO);
1564
Takashi Iwaica2c0962005-09-09 14:20:23 +02001565 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (! info)
1567 return -ENOMEM;
1568 info->card = t->card ? t->card->number : -1;
1569 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1570 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1571 strlcpy(info->id, t->id, sizeof(info->id));
1572 strlcpy(info->name, t->name, sizeof(info->name));
1573 info->resolution = t->hw.resolution;
1574 if (copy_to_user(_info, info, sizeof(*_info)))
1575 err = -EFAULT;
1576 kfree(info);
1577 return err;
1578}
1579
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001580static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001581 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001583 struct snd_timer_user *tu;
1584 struct snd_timer_params params;
1585 struct snd_timer *t;
1586 struct snd_timer_read *tr;
1587 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 tu = file->private_data;
1591 snd_assert(tu->timeri != NULL, return -ENXIO);
1592 t = tu->timeri->timer;
1593 snd_assert(t != NULL, return -ENXIO);
1594 if (copy_from_user(&params, _params, sizeof(params)))
1595 return -EFAULT;
1596 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1597 err = -EINVAL;
1598 goto _end;
1599 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001600 if (params.queue_size > 0 &&
1601 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 err = -EINVAL;
1603 goto _end;
1604 }
1605 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1606 (1<<SNDRV_TIMER_EVENT_TICK)|
1607 (1<<SNDRV_TIMER_EVENT_START)|
1608 (1<<SNDRV_TIMER_EVENT_STOP)|
1609 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1610 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001611 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1612 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 (1<<SNDRV_TIMER_EVENT_MSTART)|
1614 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1615 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001616 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1617 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001618 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 err = -EINVAL;
1620 goto _end;
1621 }
1622 snd_timer_stop(tu->timeri);
1623 spin_lock_irq(&t->lock);
1624 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1625 SNDRV_TIMER_IFLG_EXCLUSIVE|
1626 SNDRV_TIMER_IFLG_EARLY_EVENT);
1627 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1628 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1629 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1630 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1631 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1632 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1633 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001634 if (params.queue_size > 0 &&
1635 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001637 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1638 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (ttr) {
1640 kfree(tu->tqueue);
1641 tu->queue_size = params.queue_size;
1642 tu->tqueue = ttr;
1643 }
1644 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001645 tr = kmalloc(params.queue_size * sizeof(*tr),
1646 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (tr) {
1648 kfree(tu->queue);
1649 tu->queue_size = params.queue_size;
1650 tu->queue = tr;
1651 }
1652 }
1653 }
1654 tu->qhead = tu->qtail = tu->qused = 0;
1655 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1656 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001657 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 tread.event = SNDRV_TIMER_EVENT_EARLY;
1659 tread.tstamp.tv_sec = 0;
1660 tread.tstamp.tv_nsec = 0;
1661 tread.val = 0;
1662 snd_timer_user_append_to_tqueue(tu, &tread);
1663 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001664 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 r->resolution = 0;
1666 r->ticks = 0;
1667 tu->qused++;
1668 tu->qtail++;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671 tu->filter = params.filter;
1672 tu->ticks = params.ticks;
1673 err = 0;
1674 _end:
1675 if (copy_to_user(_params, &params, sizeof(params)))
1676 return -EFAULT;
1677 return err;
1678}
1679
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001680static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001681 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001683 struct snd_timer_user *tu;
1684 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 tu = file->private_data;
1687 snd_assert(tu->timeri != NULL, return -ENXIO);
1688 memset(&status, 0, sizeof(status));
1689 status.tstamp = tu->tstamp;
1690 status.resolution = snd_timer_resolution(tu->timeri);
1691 status.lost = tu->timeri->lost;
1692 status.overrun = tu->overrun;
1693 spin_lock_irq(&tu->qlock);
1694 status.queue = tu->qused;
1695 spin_unlock_irq(&tu->qlock);
1696 if (copy_to_user(_status, &status, sizeof(status)))
1697 return -EFAULT;
1698 return 0;
1699}
1700
1701static int snd_timer_user_start(struct file *file)
1702{
1703 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001704 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 tu = file->private_data;
1707 snd_assert(tu->timeri != NULL, return -ENXIO);
1708 snd_timer_stop(tu->timeri);
1709 tu->timeri->lost = 0;
1710 tu->last_resolution = 0;
1711 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1712}
1713
1714static int snd_timer_user_stop(struct file *file)
1715{
1716 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001717 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 tu = file->private_data;
1720 snd_assert(tu->timeri != NULL, return -ENXIO);
1721 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1722}
1723
1724static int snd_timer_user_continue(struct file *file)
1725{
1726 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001727 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 tu = file->private_data;
1730 snd_assert(tu->timeri != NULL, return -ENXIO);
1731 tu->timeri->lost = 0;
1732 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1733}
1734
Takashi Iwai15790a62005-05-15 15:04:14 +02001735static int snd_timer_user_pause(struct file *file)
1736{
1737 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001738 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001739
Takashi Iwai15790a62005-05-15 15:04:14 +02001740 tu = file->private_data;
1741 snd_assert(tu->timeri != NULL, return -ENXIO);
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001742 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001743}
1744
Takashi Iwai8c50b372005-05-15 15:43:54 +02001745enum {
1746 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1747 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1748 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1749 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1750};
1751
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001752static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1753 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001755 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 void __user *argp = (void __user *)arg;
1757 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 tu = file->private_data;
1760 switch (cmd) {
1761 case SNDRV_TIMER_IOCTL_PVERSION:
1762 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1763 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1764 return snd_timer_user_next_device(argp);
1765 case SNDRV_TIMER_IOCTL_TREAD:
1766 {
1767 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001768
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001769 down(&tu->tread_sem);
1770 if (tu->timeri) { /* too late */
1771 up(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001773 }
1774 if (get_user(xarg, p)) {
1775 up(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 tu->tread = xarg ? 1 : 0;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001779 up(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781 }
1782 case SNDRV_TIMER_IOCTL_GINFO:
1783 return snd_timer_user_ginfo(file, argp);
1784 case SNDRV_TIMER_IOCTL_GPARAMS:
1785 return snd_timer_user_gparams(file, argp);
1786 case SNDRV_TIMER_IOCTL_GSTATUS:
1787 return snd_timer_user_gstatus(file, argp);
1788 case SNDRV_TIMER_IOCTL_SELECT:
1789 return snd_timer_user_tselect(file, argp);
1790 case SNDRV_TIMER_IOCTL_INFO:
1791 return snd_timer_user_info(file, argp);
1792 case SNDRV_TIMER_IOCTL_PARAMS:
1793 return snd_timer_user_params(file, argp);
1794 case SNDRV_TIMER_IOCTL_STATUS:
1795 return snd_timer_user_status(file, argp);
1796 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001797 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return snd_timer_user_start(file);
1799 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001800 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return snd_timer_user_stop(file);
1802 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001803 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001805 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001806 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001807 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809 return -ENOTTY;
1810}
1811
1812static int snd_timer_user_fasync(int fd, struct file * file, int on)
1813{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001814 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 tu = file->private_data;
1818 err = fasync_helper(fd, file, on, &tu->fasync);
1819 if (err < 0)
1820 return err;
1821 return 0;
1822}
1823
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001824static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1825 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001827 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 long result = 0, unit;
1829 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001832 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 spin_lock_irq(&tu->qlock);
1834 while ((long)count - result >= unit) {
1835 while (!tu->qused) {
1836 wait_queue_t wait;
1837
1838 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1839 err = -EAGAIN;
1840 break;
1841 }
1842
1843 set_current_state(TASK_INTERRUPTIBLE);
1844 init_waitqueue_entry(&wait, current);
1845 add_wait_queue(&tu->qchange_sleep, &wait);
1846
1847 spin_unlock_irq(&tu->qlock);
1848 schedule();
1849 spin_lock_irq(&tu->qlock);
1850
1851 remove_wait_queue(&tu->qchange_sleep, &wait);
1852
1853 if (signal_pending(current)) {
1854 err = -ERESTARTSYS;
1855 break;
1856 }
1857 }
1858
1859 spin_unlock_irq(&tu->qlock);
1860 if (err < 0)
1861 goto _error;
1862
1863 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001864 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001865 sizeof(struct snd_timer_tread))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 err = -EFAULT;
1867 goto _error;
1868 }
1869 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001870 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001871 sizeof(struct snd_timer_read))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 err = -EFAULT;
1873 goto _error;
1874 }
1875 }
1876
1877 tu->qhead %= tu->queue_size;
1878
1879 result += unit;
1880 buffer += unit;
1881
1882 spin_lock_irq(&tu->qlock);
1883 tu->qused--;
1884 }
1885 spin_unlock_irq(&tu->qlock);
1886 _error:
1887 return result > 0 ? result : err;
1888}
1889
1890static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1891{
1892 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001893 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 tu = file->private_data;
1896
1897 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 mask = 0;
1900 if (tu->qused)
1901 mask |= POLLIN | POLLRDNORM;
1902
1903 return mask;
1904}
1905
1906#ifdef CONFIG_COMPAT
1907#include "timer_compat.c"
1908#else
1909#define snd_timer_user_ioctl_compat NULL
1910#endif
1911
1912static struct file_operations snd_timer_f_ops =
1913{
1914 .owner = THIS_MODULE,
1915 .read = snd_timer_user_read,
1916 .open = snd_timer_user_open,
1917 .release = snd_timer_user_release,
1918 .poll = snd_timer_user_poll,
1919 .unlocked_ioctl = snd_timer_user_ioctl,
1920 .compat_ioctl = snd_timer_user_ioctl_compat,
1921 .fasync = snd_timer_user_fasync,
1922};
1923
Takashi Iwai53d2f742005-11-17 13:56:05 +01001924static struct snd_minor snd_timer_reg =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 .comment = "timer",
1927 .f_ops = &snd_timer_f_ops,
1928};
1929
1930/*
1931 * ENTRY functions
1932 */
1933
Takashi Iwai53d2f742005-11-17 13:56:05 +01001934static struct snd_info_entry *snd_timer_proc_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936static int __init alsa_timer_init(void)
1937{
1938 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001939 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001942 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1943 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944#endif
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001945 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1946 if (entry != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 entry->c.text.read_size = SNDRV_TIMER_DEVICES * 128;
1948 entry->c.text.read = snd_timer_proc_read;
1949 if (snd_info_register(entry) < 0) {
1950 snd_info_free_entry(entry);
1951 entry = NULL;
1952 }
1953 }
1954 snd_timer_proc_entry = entry;
1955 if ((err = snd_timer_register_system()) < 0)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001956 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1957 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER,
1959 NULL, 0, &snd_timer_reg, "timer"))<0)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001960 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1961 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return 0;
1963}
1964
1965static void __exit alsa_timer_exit(void)
1966{
1967 struct list_head *p, *n;
1968
1969 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1970 /* unregister the system timer */
1971 list_for_each_safe(p, n, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001972 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 snd_timer_unregister(timer);
1974 }
1975 if (snd_timer_proc_entry) {
1976 snd_info_unregister(snd_timer_proc_entry);
1977 snd_timer_proc_entry = NULL;
1978 }
1979#ifdef SNDRV_OSS_INFO_DEV_TIMERS
1980 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1981#endif
1982}
1983
1984module_init(alsa_timer_init)
1985module_exit(alsa_timer_exit)
1986
1987EXPORT_SYMBOL(snd_timer_open);
1988EXPORT_SYMBOL(snd_timer_close);
1989EXPORT_SYMBOL(snd_timer_resolution);
1990EXPORT_SYMBOL(snd_timer_start);
1991EXPORT_SYMBOL(snd_timer_stop);
1992EXPORT_SYMBOL(snd_timer_continue);
1993EXPORT_SYMBOL(snd_timer_pause);
1994EXPORT_SYMBOL(snd_timer_new);
1995EXPORT_SYMBOL(snd_timer_notify);
1996EXPORT_SYMBOL(snd_timer_global_new);
1997EXPORT_SYMBOL(snd_timer_global_free);
1998EXPORT_SYMBOL(snd_timer_global_register);
1999EXPORT_SYMBOL(snd_timer_global_unregister);
2000EXPORT_SYMBOL(snd_timer_interrupt);