ALSA: timer: Fix race at concurrent reads
[pandora-kernel.git] / sound / core / timer.c
1 /*
2  *  Timers abstract layer
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/timer.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/minors.h>
34 #include <sound/initval.h>
35 #include <linux/kmod.h>
36
37 #if defined(CONFIG_SND_HRTIMER) || defined(CONFIG_SND_HRTIMER_MODULE)
38 #define DEFAULT_TIMER_LIMIT 4
39 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
40 #define DEFAULT_TIMER_LIMIT 2
41 #else
42 #define DEFAULT_TIMER_LIMIT 1
43 #endif
44
45 static int timer_limit = DEFAULT_TIMER_LIMIT;
46 static int timer_tstamp_monotonic = 1;
47 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
48 MODULE_DESCRIPTION("ALSA timer interface");
49 MODULE_LICENSE("GPL");
50 module_param(timer_limit, int, 0444);
51 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
52 module_param(timer_tstamp_monotonic, int, 0444);
53 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
54
55 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
56 MODULE_ALIAS("devname:snd/timer");
57
58 struct snd_timer_user {
59         struct snd_timer_instance *timeri;
60         int tread;              /* enhanced read with timestamps and events */
61         unsigned long ticks;
62         unsigned long overrun;
63         int qhead;
64         int qtail;
65         int qused;
66         int queue_size;
67         struct snd_timer_read *queue;
68         struct snd_timer_tread *tqueue;
69         spinlock_t qlock;
70         unsigned long last_resolution;
71         unsigned int filter;
72         struct timespec tstamp;         /* trigger tstamp */
73         wait_queue_head_t qchange_sleep;
74         struct fasync_struct *fasync;
75         struct mutex ioctl_lock;
76 };
77
78 /* list of timers */
79 static LIST_HEAD(snd_timer_list);
80
81 /* list of slave instances */
82 static LIST_HEAD(snd_timer_slave_list);
83
84 /* lock for slave active lists */
85 static DEFINE_SPINLOCK(slave_active_lock);
86
87 static DEFINE_MUTEX(register_mutex);
88
89 static int snd_timer_free(struct snd_timer *timer);
90 static int snd_timer_dev_free(struct snd_device *device);
91 static int snd_timer_dev_register(struct snd_device *device);
92 static int snd_timer_dev_disconnect(struct snd_device *device);
93
94 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
95
96 /*
97  * create a timer instance with the given owner string.
98  * when timer is not NULL, increments the module counter
99  */
100 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
101                                                          struct snd_timer *timer)
102 {
103         struct snd_timer_instance *timeri;
104         timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
105         if (timeri == NULL)
106                 return NULL;
107         timeri->owner = kstrdup(owner, GFP_KERNEL);
108         if (! timeri->owner) {
109                 kfree(timeri);
110                 return NULL;
111         }
112         INIT_LIST_HEAD(&timeri->open_list);
113         INIT_LIST_HEAD(&timeri->active_list);
114         INIT_LIST_HEAD(&timeri->ack_list);
115         INIT_LIST_HEAD(&timeri->slave_list_head);
116         INIT_LIST_HEAD(&timeri->slave_active_head);
117
118         timeri->timer = timer;
119         if (timer && !try_module_get(timer->module)) {
120                 kfree(timeri->owner);
121                 kfree(timeri);
122                 return NULL;
123         }
124
125         return timeri;
126 }
127
128 /*
129  * find a timer instance from the given timer id
130  */
131 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
132 {
133         struct snd_timer *timer = NULL;
134
135         list_for_each_entry(timer, &snd_timer_list, device_list) {
136                 if (timer->tmr_class != tid->dev_class)
137                         continue;
138                 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
139                      timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
140                     (timer->card == NULL ||
141                      timer->card->number != tid->card))
142                         continue;
143                 if (timer->tmr_device != tid->device)
144                         continue;
145                 if (timer->tmr_subdevice != tid->subdevice)
146                         continue;
147                 return timer;
148         }
149         return NULL;
150 }
151
152 #ifdef CONFIG_MODULES
153
154 static void snd_timer_request(struct snd_timer_id *tid)
155 {
156         switch (tid->dev_class) {
157         case SNDRV_TIMER_CLASS_GLOBAL:
158                 if (tid->device < timer_limit)
159                         request_module("snd-timer-%i", tid->device);
160                 break;
161         case SNDRV_TIMER_CLASS_CARD:
162         case SNDRV_TIMER_CLASS_PCM:
163                 if (tid->card < snd_ecards_limit)
164                         request_module("snd-card-%i", tid->card);
165                 break;
166         default:
167                 break;
168         }
169 }
170
171 #endif
172
173 /*
174  * look for a master instance matching with the slave id of the given slave.
175  * when found, relink the open_link of the slave.
176  *
177  * call this with register_mutex down.
178  */
179 static void snd_timer_check_slave(struct snd_timer_instance *slave)
180 {
181         struct snd_timer *timer;
182         struct snd_timer_instance *master;
183
184         /* FIXME: it's really dumb to look up all entries.. */
185         list_for_each_entry(timer, &snd_timer_list, device_list) {
186                 list_for_each_entry(master, &timer->open_list_head, open_list) {
187                         if (slave->slave_class == master->slave_class &&
188                             slave->slave_id == master->slave_id) {
189                                 list_move_tail(&slave->open_list,
190                                                &master->slave_list_head);
191                                 spin_lock_irq(&slave_active_lock);
192                                 slave->master = master;
193                                 slave->timer = master->timer;
194                                 spin_unlock_irq(&slave_active_lock);
195                                 return;
196                         }
197                 }
198         }
199 }
200
201 /*
202  * look for slave instances matching with the slave id of the given master.
203  * when found, relink the open_link of slaves.
204  *
205  * call this with register_mutex down.
206  */
207 static void snd_timer_check_master(struct snd_timer_instance *master)
208 {
209         struct snd_timer_instance *slave, *tmp;
210
211         /* check all pending slaves */
212         list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
213                 if (slave->slave_class == master->slave_class &&
214                     slave->slave_id == master->slave_id) {
215                         list_move_tail(&slave->open_list, &master->slave_list_head);
216                         spin_lock_irq(&slave_active_lock);
217                         spin_lock(&master->timer->lock);
218                         slave->master = master;
219                         slave->timer = master->timer;
220                         if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
221                                 list_add_tail(&slave->active_list,
222                                               &master->slave_active_head);
223                         spin_unlock(&master->timer->lock);
224                         spin_unlock_irq(&slave_active_lock);
225                 }
226         }
227 }
228
229 /*
230  * open a timer instance
231  * when opening a master, the slave id must be here given.
232  */
233 int snd_timer_open(struct snd_timer_instance **ti,
234                    char *owner, struct snd_timer_id *tid,
235                    unsigned int slave_id)
236 {
237         struct snd_timer *timer;
238         struct snd_timer_instance *timeri = NULL;
239
240         if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
241                 /* open a slave instance */
242                 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
243                     tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
244                         snd_printd("invalid slave class %i\n", tid->dev_sclass);
245                         return -EINVAL;
246                 }
247                 mutex_lock(&register_mutex);
248                 timeri = snd_timer_instance_new(owner, NULL);
249                 if (!timeri) {
250                         mutex_unlock(&register_mutex);
251                         return -ENOMEM;
252                 }
253                 timeri->slave_class = tid->dev_sclass;
254                 timeri->slave_id = tid->device;
255                 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
256                 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
257                 snd_timer_check_slave(timeri);
258                 mutex_unlock(&register_mutex);
259                 *ti = timeri;
260                 return 0;
261         }
262
263         /* open a master instance */
264         mutex_lock(&register_mutex);
265         timer = snd_timer_find(tid);
266 #ifdef CONFIG_MODULES
267         if (!timer) {
268                 mutex_unlock(&register_mutex);
269                 snd_timer_request(tid);
270                 mutex_lock(&register_mutex);
271                 timer = snd_timer_find(tid);
272         }
273 #endif
274         if (!timer) {
275                 mutex_unlock(&register_mutex);
276                 return -ENODEV;
277         }
278         if (!list_empty(&timer->open_list_head)) {
279                 timeri = list_entry(timer->open_list_head.next,
280                                     struct snd_timer_instance, open_list);
281                 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
282                         mutex_unlock(&register_mutex);
283                         return -EBUSY;
284                 }
285         }
286         timeri = snd_timer_instance_new(owner, timer);
287         if (!timeri) {
288                 mutex_unlock(&register_mutex);
289                 return -ENOMEM;
290         }
291         timeri->slave_class = tid->dev_sclass;
292         timeri->slave_id = slave_id;
293         if (list_empty(&timer->open_list_head) && timer->hw.open)
294                 timer->hw.open(timer);
295         list_add_tail(&timeri->open_list, &timer->open_list_head);
296         snd_timer_check_master(timeri);
297         mutex_unlock(&register_mutex);
298         *ti = timeri;
299         return 0;
300 }
301
302 static int _snd_timer_stop(struct snd_timer_instance *timeri,
303                            int keep_flag, int event);
304
305 /*
306  * close a timer instance
307  */
308 int snd_timer_close(struct snd_timer_instance *timeri)
309 {
310         struct snd_timer *timer = NULL;
311         struct snd_timer_instance *slave, *tmp;
312
313         if (snd_BUG_ON(!timeri))
314                 return -ENXIO;
315
316         /* force to stop the timer */
317         snd_timer_stop(timeri);
318
319         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
320                 /* wait, until the active callback is finished */
321                 spin_lock_irq(&slave_active_lock);
322                 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
323                         spin_unlock_irq(&slave_active_lock);
324                         udelay(10);
325                         spin_lock_irq(&slave_active_lock);
326                 }
327                 spin_unlock_irq(&slave_active_lock);
328                 mutex_lock(&register_mutex);
329                 list_del(&timeri->open_list);
330                 mutex_unlock(&register_mutex);
331         } else {
332                 timer = timeri->timer;
333                 if (snd_BUG_ON(!timer))
334                         goto out;
335                 /* wait, until the active callback is finished */
336                 spin_lock_irq(&timer->lock);
337                 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
338                         spin_unlock_irq(&timer->lock);
339                         udelay(10);
340                         spin_lock_irq(&timer->lock);
341                 }
342                 spin_unlock_irq(&timer->lock);
343                 mutex_lock(&register_mutex);
344                 list_del(&timeri->open_list);
345                 if (timer && list_empty(&timer->open_list_head) &&
346                     timer->hw.close)
347                         timer->hw.close(timer);
348                 /* remove slave links */
349                 spin_lock_irq(&slave_active_lock);
350                 spin_lock(&timer->lock);
351                 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
352                                          open_list) {
353                         list_move_tail(&slave->open_list, &snd_timer_slave_list);
354                         slave->master = NULL;
355                         slave->timer = NULL;
356                         list_del_init(&slave->ack_list);
357                         list_del_init(&slave->active_list);
358                 }
359                 spin_unlock(&timer->lock);
360                 spin_unlock_irq(&slave_active_lock);
361                 mutex_unlock(&register_mutex);
362         }
363  out:
364         if (timeri->private_free)
365                 timeri->private_free(timeri);
366         kfree(timeri->owner);
367         kfree(timeri);
368         if (timer)
369                 module_put(timer->module);
370         return 0;
371 }
372
373 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
374 {
375         struct snd_timer * timer;
376
377         if (timeri == NULL)
378                 return 0;
379         if ((timer = timeri->timer) != NULL) {
380                 if (timer->hw.c_resolution)
381                         return timer->hw.c_resolution(timer);
382                 return timer->hw.resolution;
383         }
384         return 0;
385 }
386
387 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
388 {
389         struct snd_timer *timer;
390         unsigned long flags;
391         unsigned long resolution = 0;
392         struct snd_timer_instance *ts;
393         struct timespec tstamp;
394
395         if (timer_tstamp_monotonic)
396                 do_posix_clock_monotonic_gettime(&tstamp);
397         else
398                 getnstimeofday(&tstamp);
399         if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
400                        event > SNDRV_TIMER_EVENT_PAUSE))
401                 return;
402         if (event == SNDRV_TIMER_EVENT_START ||
403             event == SNDRV_TIMER_EVENT_CONTINUE)
404                 resolution = snd_timer_resolution(ti);
405         if (ti->ccallback)
406                 ti->ccallback(ti, event, &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_entry(ts, &ti->slave_active_head, active_list)
416                 if (ts->ccallback)
417                         ts->ccallback(ts, event + 100, &tstamp, resolution);
418         spin_unlock_irqrestore(&timer->lock, flags);
419 }
420
421 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
422                             unsigned long sticks)
423 {
424         list_move_tail(&timeri->active_list, &timer->active_list_head);
425         if (timer->running) {
426                 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
427                         goto __start_now;
428                 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
429                 timeri->flags |= SNDRV_TIMER_IFLG_START;
430                 return 1;       /* delayed start */
431         } else {
432                 timer->sticks = sticks;
433                 timer->hw.start(timer);
434               __start_now:
435                 timer->running++;
436                 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
437                 return 0;
438         }
439 }
440
441 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
442 {
443         unsigned long flags;
444
445         spin_lock_irqsave(&slave_active_lock, flags);
446         if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
447                 spin_unlock_irqrestore(&slave_active_lock, flags);
448                 return -EBUSY;
449         }
450         timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
451         if (timeri->master && timeri->timer) {
452                 spin_lock(&timeri->timer->lock);
453                 list_add_tail(&timeri->active_list,
454                               &timeri->master->slave_active_head);
455                 spin_unlock(&timeri->timer->lock);
456         }
457         spin_unlock_irqrestore(&slave_active_lock, flags);
458         return 1; /* delayed start */
459 }
460
461 /*
462  *  start the timer instance
463  */
464 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
465 {
466         struct snd_timer *timer;
467         int result = -EINVAL;
468         unsigned long flags;
469
470         if (timeri == NULL || ticks < 1)
471                 return -EINVAL;
472         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
473                 result = snd_timer_start_slave(timeri);
474                 if (result >= 0)
475                         snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
476                 return result;
477         }
478         timer = timeri->timer;
479         if (timer == NULL)
480                 return -EINVAL;
481         spin_lock_irqsave(&timer->lock, flags);
482         if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
483                              SNDRV_TIMER_IFLG_START)) {
484                 result = -EBUSY;
485                 goto unlock;
486         }
487         timeri->ticks = timeri->cticks = ticks;
488         timeri->pticks = 0;
489         result = snd_timer_start1(timer, timeri, ticks);
490  unlock:
491         spin_unlock_irqrestore(&timer->lock, flags);
492         if (result >= 0)
493                 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
494         return result;
495 }
496
497 static int _snd_timer_stop(struct snd_timer_instance * timeri,
498                            int keep_flag, int event)
499 {
500         struct snd_timer *timer;
501         unsigned long flags;
502
503         if (snd_BUG_ON(!timeri))
504                 return -ENXIO;
505
506         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
507                 if (!keep_flag) {
508                         spin_lock_irqsave(&slave_active_lock, flags);
509                         if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
510                                 spin_unlock_irqrestore(&slave_active_lock, flags);
511                                 return -EBUSY;
512                         }
513                         if (timeri->timer)
514                                 spin_lock(&timeri->timer->lock);
515                         timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
516                         list_del_init(&timeri->ack_list);
517                         list_del_init(&timeri->active_list);
518                         if (timeri->timer)
519                                 spin_unlock(&timeri->timer->lock);
520                         spin_unlock_irqrestore(&slave_active_lock, flags);
521                 }
522                 goto __end;
523         }
524         timer = timeri->timer;
525         if (!timer)
526                 return -EINVAL;
527         spin_lock_irqsave(&timer->lock, flags);
528         if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
529                                SNDRV_TIMER_IFLG_START))) {
530                 spin_unlock_irqrestore(&timer->lock, flags);
531                 return -EBUSY;
532         }
533         list_del_init(&timeri->ack_list);
534         list_del_init(&timeri->active_list);
535         if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
536             !(--timer->running)) {
537                 timer->hw.stop(timer);
538                 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
539                         timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
540                         snd_timer_reschedule(timer, 0);
541                         if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
542                                 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
543                                 timer->hw.start(timer);
544                         }
545                 }
546         }
547         if (!keep_flag)
548                 timeri->flags &=
549                         ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
550         spin_unlock_irqrestore(&timer->lock, flags);
551       __end:
552         if (event != SNDRV_TIMER_EVENT_RESOLUTION)
553                 snd_timer_notify1(timeri, event);
554         return 0;
555 }
556
557 /*
558  * stop the timer instance.
559  *
560  * do not call this from the timer callback!
561  */
562 int snd_timer_stop(struct snd_timer_instance *timeri)
563 {
564         struct snd_timer *timer;
565         unsigned long flags;
566         int err;
567
568         err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
569         if (err < 0)
570                 return err;
571         timer = timeri->timer;
572         if (!timer)
573                 return -EINVAL;
574         spin_lock_irqsave(&timer->lock, flags);
575         timeri->cticks = timeri->ticks;
576         timeri->pticks = 0;
577         spin_unlock_irqrestore(&timer->lock, flags);
578         return 0;
579 }
580
581 /*
582  * start again..  the tick is kept.
583  */
584 int snd_timer_continue(struct snd_timer_instance *timeri)
585 {
586         struct snd_timer *timer;
587         int result = -EINVAL;
588         unsigned long flags;
589
590         if (timeri == NULL)
591                 return result;
592         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
593                 return snd_timer_start_slave(timeri);
594         timer = timeri->timer;
595         if (! timer)
596                 return -EINVAL;
597         spin_lock_irqsave(&timer->lock, flags);
598         if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
599                 result = -EBUSY;
600                 goto unlock;
601         }
602         if (!timeri->cticks)
603                 timeri->cticks = 1;
604         timeri->pticks = 0;
605         result = snd_timer_start1(timer, timeri, timer->sticks);
606  unlock:
607         spin_unlock_irqrestore(&timer->lock, flags);
608         snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
609         return result;
610 }
611
612 /*
613  * pause.. remember the ticks left
614  */
615 int snd_timer_pause(struct snd_timer_instance * timeri)
616 {
617         return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
618 }
619
620 /*
621  * reschedule the timer
622  *
623  * start pending instances and check the scheduling ticks.
624  * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
625  */
626 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
627 {
628         struct snd_timer_instance *ti;
629         unsigned long ticks = ~0UL;
630
631         list_for_each_entry(ti, &timer->active_list_head, active_list) {
632                 if (ti->flags & SNDRV_TIMER_IFLG_START) {
633                         ti->flags &= ~SNDRV_TIMER_IFLG_START;
634                         ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
635                         timer->running++;
636                 }
637                 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
638                         if (ticks > ti->cticks)
639                                 ticks = ti->cticks;
640                 }
641         }
642         if (ticks == ~0UL) {
643                 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
644                 return;
645         }
646         if (ticks > timer->hw.ticks)
647                 ticks = timer->hw.ticks;
648         if (ticks_left != ticks)
649                 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
650         timer->sticks = ticks;
651 }
652
653 /*
654  * timer tasklet
655  *
656  */
657 static void snd_timer_tasklet(unsigned long arg)
658 {
659         struct snd_timer *timer = (struct snd_timer *) arg;
660         struct snd_timer_instance *ti;
661         struct list_head *p;
662         unsigned long resolution, ticks;
663         unsigned long flags;
664
665         spin_lock_irqsave(&timer->lock, flags);
666         /* now process all callbacks */
667         while (!list_empty(&timer->sack_list_head)) {
668                 p = timer->sack_list_head.next;         /* get first item */
669                 ti = list_entry(p, struct snd_timer_instance, ack_list);
670
671                 /* remove from ack_list and make empty */
672                 list_del_init(p);
673
674                 ticks = ti->pticks;
675                 ti->pticks = 0;
676                 resolution = ti->resolution;
677
678                 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
679                 spin_unlock(&timer->lock);
680                 if (ti->callback)
681                         ti->callback(ti, resolution, ticks);
682                 spin_lock(&timer->lock);
683                 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
684         }
685         spin_unlock_irqrestore(&timer->lock, flags);
686 }
687
688 /*
689  * timer interrupt
690  *
691  * ticks_left is usually equal to timer->sticks.
692  *
693  */
694 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
695 {
696         struct snd_timer_instance *ti, *ts, *tmp;
697         unsigned long resolution, ticks;
698         struct list_head *p, *ack_list_head;
699         unsigned long flags;
700         int use_tasklet = 0;
701
702         if (timer == NULL)
703                 return;
704
705         spin_lock_irqsave(&timer->lock, flags);
706
707         /* remember the current resolution */
708         if (timer->hw.c_resolution)
709                 resolution = timer->hw.c_resolution(timer);
710         else
711                 resolution = timer->hw.resolution;
712
713         /* loop for all active instances
714          * Here we cannot use list_for_each_entry because the active_list of a
715          * processed instance is relinked to done_list_head before the callback
716          * is called.
717          */
718         list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
719                                  active_list) {
720                 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
721                         continue;
722                 ti->pticks += ticks_left;
723                 ti->resolution = resolution;
724                 if (ti->cticks < ticks_left)
725                         ti->cticks = 0;
726                 else
727                         ti->cticks -= ticks_left;
728                 if (ti->cticks) /* not expired */
729                         continue;
730                 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
731                         ti->cticks = ti->ticks;
732                 } else {
733                         ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
734                         --timer->running;
735                         list_del_init(&ti->active_list);
736                 }
737                 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
738                     (ti->flags & SNDRV_TIMER_IFLG_FAST))
739                         ack_list_head = &timer->ack_list_head;
740                 else
741                         ack_list_head = &timer->sack_list_head;
742                 if (list_empty(&ti->ack_list))
743                         list_add_tail(&ti->ack_list, ack_list_head);
744                 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
745                         ts->pticks = ti->pticks;
746                         ts->resolution = resolution;
747                         if (list_empty(&ts->ack_list))
748                                 list_add_tail(&ts->ack_list, ack_list_head);
749                 }
750         }
751         if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
752                 snd_timer_reschedule(timer, timer->sticks);
753         if (timer->running) {
754                 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
755                         timer->hw.stop(timer);
756                         timer->flags |= SNDRV_TIMER_FLG_CHANGE;
757                 }
758                 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
759                     (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
760                         /* restart timer */
761                         timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
762                         timer->hw.start(timer);
763                 }
764         } else {
765                 timer->hw.stop(timer);
766         }
767
768         /* now process all fast callbacks */
769         while (!list_empty(&timer->ack_list_head)) {
770                 p = timer->ack_list_head.next;          /* get first item */
771                 ti = list_entry(p, struct snd_timer_instance, ack_list);
772
773                 /* remove from ack_list and make empty */
774                 list_del_init(p);
775
776                 ticks = ti->pticks;
777                 ti->pticks = 0;
778
779                 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
780                 spin_unlock(&timer->lock);
781                 if (ti->callback)
782                         ti->callback(ti, resolution, ticks);
783                 spin_lock(&timer->lock);
784                 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
785         }
786
787         /* do we have any slow callbacks? */
788         use_tasklet = !list_empty(&timer->sack_list_head);
789         spin_unlock_irqrestore(&timer->lock, flags);
790
791         if (use_tasklet)
792                 tasklet_schedule(&timer->task_queue);
793 }
794
795 /*
796
797  */
798
799 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
800                   struct snd_timer **rtimer)
801 {
802         struct snd_timer *timer;
803         int err;
804         static struct snd_device_ops ops = {
805                 .dev_free = snd_timer_dev_free,
806                 .dev_register = snd_timer_dev_register,
807                 .dev_disconnect = snd_timer_dev_disconnect,
808         };
809
810         if (snd_BUG_ON(!tid))
811                 return -EINVAL;
812         if (rtimer)
813                 *rtimer = NULL;
814         timer = kzalloc(sizeof(*timer), GFP_KERNEL);
815         if (timer == NULL) {
816                 snd_printk(KERN_ERR "timer: cannot allocate\n");
817                 return -ENOMEM;
818         }
819         timer->tmr_class = tid->dev_class;
820         timer->card = card;
821         timer->tmr_device = tid->device;
822         timer->tmr_subdevice = tid->subdevice;
823         if (id)
824                 strlcpy(timer->id, id, sizeof(timer->id));
825         INIT_LIST_HEAD(&timer->device_list);
826         INIT_LIST_HEAD(&timer->open_list_head);
827         INIT_LIST_HEAD(&timer->active_list_head);
828         INIT_LIST_HEAD(&timer->ack_list_head);
829         INIT_LIST_HEAD(&timer->sack_list_head);
830         spin_lock_init(&timer->lock);
831         tasklet_init(&timer->task_queue, snd_timer_tasklet,
832                      (unsigned long)timer);
833         if (card != NULL) {
834                 timer->module = card->module;
835                 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
836                 if (err < 0) {
837                         snd_timer_free(timer);
838                         return err;
839                 }
840         }
841         if (rtimer)
842                 *rtimer = timer;
843         return 0;
844 }
845
846 static int snd_timer_free(struct snd_timer *timer)
847 {
848         if (!timer)
849                 return 0;
850
851         mutex_lock(&register_mutex);
852         if (! list_empty(&timer->open_list_head)) {
853                 struct list_head *p, *n;
854                 struct snd_timer_instance *ti;
855                 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
856                 list_for_each_safe(p, n, &timer->open_list_head) {
857                         list_del_init(p);
858                         ti = list_entry(p, struct snd_timer_instance, open_list);
859                         ti->timer = NULL;
860                 }
861         }
862         list_del(&timer->device_list);
863         mutex_unlock(&register_mutex);
864
865         if (timer->private_free)
866                 timer->private_free(timer);
867         kfree(timer);
868         return 0;
869 }
870
871 static int snd_timer_dev_free(struct snd_device *device)
872 {
873         struct snd_timer *timer = device->device_data;
874         return snd_timer_free(timer);
875 }
876
877 static int snd_timer_dev_register(struct snd_device *dev)
878 {
879         struct snd_timer *timer = dev->device_data;
880         struct snd_timer *timer1;
881
882         if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
883                 return -ENXIO;
884         if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
885             !timer->hw.resolution && timer->hw.c_resolution == NULL)
886                 return -EINVAL;
887
888         mutex_lock(&register_mutex);
889         list_for_each_entry(timer1, &snd_timer_list, device_list) {
890                 if (timer1->tmr_class > timer->tmr_class)
891                         break;
892                 if (timer1->tmr_class < timer->tmr_class)
893                         continue;
894                 if (timer1->card && timer->card) {
895                         if (timer1->card->number > timer->card->number)
896                                 break;
897                         if (timer1->card->number < timer->card->number)
898                                 continue;
899                 }
900                 if (timer1->tmr_device > timer->tmr_device)
901                         break;
902                 if (timer1->tmr_device < timer->tmr_device)
903                         continue;
904                 if (timer1->tmr_subdevice > timer->tmr_subdevice)
905                         break;
906                 if (timer1->tmr_subdevice < timer->tmr_subdevice)
907                         continue;
908                 /* conflicts.. */
909                 mutex_unlock(&register_mutex);
910                 return -EBUSY;
911         }
912         list_add_tail(&timer->device_list, &timer1->device_list);
913         mutex_unlock(&register_mutex);
914         return 0;
915 }
916
917 static int snd_timer_dev_disconnect(struct snd_device *device)
918 {
919         struct snd_timer *timer = device->device_data;
920         mutex_lock(&register_mutex);
921         list_del_init(&timer->device_list);
922         mutex_unlock(&register_mutex);
923         return 0;
924 }
925
926 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
927 {
928         unsigned long flags;
929         unsigned long resolution = 0;
930         struct snd_timer_instance *ti, *ts;
931
932         if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
933                 return;
934         if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
935                        event > SNDRV_TIMER_EVENT_MRESUME))
936                 return;
937         spin_lock_irqsave(&timer->lock, flags);
938         if (event == SNDRV_TIMER_EVENT_MSTART ||
939             event == SNDRV_TIMER_EVENT_MCONTINUE ||
940             event == SNDRV_TIMER_EVENT_MRESUME) {
941                 if (timer->hw.c_resolution)
942                         resolution = timer->hw.c_resolution(timer);
943                 else
944                         resolution = timer->hw.resolution;
945         }
946         list_for_each_entry(ti, &timer->active_list_head, active_list) {
947                 if (ti->ccallback)
948                         ti->ccallback(ti, event, tstamp, resolution);
949                 list_for_each_entry(ts, &ti->slave_active_head, active_list)
950                         if (ts->ccallback)
951                                 ts->ccallback(ts, event, tstamp, resolution);
952         }
953         spin_unlock_irqrestore(&timer->lock, flags);
954 }
955
956 /*
957  * exported functions for global timers
958  */
959 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
960 {
961         struct snd_timer_id tid;
962
963         tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
964         tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
965         tid.card = -1;
966         tid.device = device;
967         tid.subdevice = 0;
968         return snd_timer_new(NULL, id, &tid, rtimer);
969 }
970
971 int snd_timer_global_free(struct snd_timer *timer)
972 {
973         return snd_timer_free(timer);
974 }
975
976 int snd_timer_global_register(struct snd_timer *timer)
977 {
978         struct snd_device dev;
979
980         memset(&dev, 0, sizeof(dev));
981         dev.device_data = timer;
982         return snd_timer_dev_register(&dev);
983 }
984
985 /*
986  *  System timer
987  */
988
989 struct snd_timer_system_private {
990         struct timer_list tlist;
991         unsigned long last_expires;
992         unsigned long last_jiffies;
993         unsigned long correction;
994 };
995
996 static void snd_timer_s_function(unsigned long data)
997 {
998         struct snd_timer *timer = (struct snd_timer *)data;
999         struct snd_timer_system_private *priv = timer->private_data;
1000         unsigned long jiff = jiffies;
1001         if (time_after(jiff, priv->last_expires))
1002                 priv->correction += (long)jiff - (long)priv->last_expires;
1003         snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1004 }
1005
1006 static int snd_timer_s_start(struct snd_timer * timer)
1007 {
1008         struct snd_timer_system_private *priv;
1009         unsigned long njiff;
1010
1011         priv = (struct snd_timer_system_private *) timer->private_data;
1012         njiff = (priv->last_jiffies = jiffies);
1013         if (priv->correction > timer->sticks - 1) {
1014                 priv->correction -= timer->sticks - 1;
1015                 njiff++;
1016         } else {
1017                 njiff += timer->sticks - priv->correction;
1018                 priv->correction = 0;
1019         }
1020         priv->last_expires = priv->tlist.expires = njiff;
1021         add_timer(&priv->tlist);
1022         return 0;
1023 }
1024
1025 static int snd_timer_s_stop(struct snd_timer * timer)
1026 {
1027         struct snd_timer_system_private *priv;
1028         unsigned long jiff;
1029
1030         priv = (struct snd_timer_system_private *) timer->private_data;
1031         del_timer(&priv->tlist);
1032         jiff = jiffies;
1033         if (time_before(jiff, priv->last_expires))
1034                 timer->sticks = priv->last_expires - jiff;
1035         else
1036                 timer->sticks = 1;
1037         priv->correction = 0;
1038         return 0;
1039 }
1040
1041 static struct snd_timer_hardware snd_timer_system =
1042 {
1043         .flags =        SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1044         .resolution =   1000000000L / HZ,
1045         .ticks =        10000000L,
1046         .start =        snd_timer_s_start,
1047         .stop =         snd_timer_s_stop
1048 };
1049
1050 static void snd_timer_free_system(struct snd_timer *timer)
1051 {
1052         kfree(timer->private_data);
1053 }
1054
1055 static int snd_timer_register_system(void)
1056 {
1057         struct snd_timer *timer;
1058         struct snd_timer_system_private *priv;
1059         int err;
1060
1061         err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1062         if (err < 0)
1063                 return err;
1064         strcpy(timer->name, "system timer");
1065         timer->hw = snd_timer_system;
1066         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1067         if (priv == NULL) {
1068                 snd_timer_free(timer);
1069                 return -ENOMEM;
1070         }
1071         init_timer(&priv->tlist);
1072         priv->tlist.function = snd_timer_s_function;
1073         priv->tlist.data = (unsigned long) timer;
1074         timer->private_data = priv;
1075         timer->private_free = snd_timer_free_system;
1076         return snd_timer_global_register(timer);
1077 }
1078
1079 #ifdef CONFIG_PROC_FS
1080 /*
1081  *  Info interface
1082  */
1083
1084 static void snd_timer_proc_read(struct snd_info_entry *entry,
1085                                 struct snd_info_buffer *buffer)
1086 {
1087         struct snd_timer *timer;
1088         struct snd_timer_instance *ti;
1089
1090         mutex_lock(&register_mutex);
1091         list_for_each_entry(timer, &snd_timer_list, device_list) {
1092                 switch (timer->tmr_class) {
1093                 case SNDRV_TIMER_CLASS_GLOBAL:
1094                         snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1095                         break;
1096                 case SNDRV_TIMER_CLASS_CARD:
1097                         snd_iprintf(buffer, "C%i-%i: ",
1098                                     timer->card->number, timer->tmr_device);
1099                         break;
1100                 case SNDRV_TIMER_CLASS_PCM:
1101                         snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1102                                     timer->tmr_device, timer->tmr_subdevice);
1103                         break;
1104                 default:
1105                         snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1106                                     timer->card ? timer->card->number : -1,
1107                                     timer->tmr_device, timer->tmr_subdevice);
1108                 }
1109                 snd_iprintf(buffer, "%s :", timer->name);
1110                 if (timer->hw.resolution)
1111                         snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1112                                     timer->hw.resolution / 1000,
1113                                     timer->hw.resolution % 1000,
1114                                     timer->hw.ticks);
1115                 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1116                         snd_iprintf(buffer, " SLAVE");
1117                 snd_iprintf(buffer, "\n");
1118                 list_for_each_entry(ti, &timer->open_list_head, open_list)
1119                         snd_iprintf(buffer, "  Client %s : %s\n",
1120                                     ti->owner ? ti->owner : "unknown",
1121                                     ti->flags & (SNDRV_TIMER_IFLG_START |
1122                                                  SNDRV_TIMER_IFLG_RUNNING)
1123                                     ? "running" : "stopped");
1124         }
1125         mutex_unlock(&register_mutex);
1126 }
1127
1128 static struct snd_info_entry *snd_timer_proc_entry;
1129
1130 static void __init snd_timer_proc_init(void)
1131 {
1132         struct snd_info_entry *entry;
1133
1134         entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1135         if (entry != NULL) {
1136                 entry->c.text.read = snd_timer_proc_read;
1137                 if (snd_info_register(entry) < 0) {
1138                         snd_info_free_entry(entry);
1139                         entry = NULL;
1140                 }
1141         }
1142         snd_timer_proc_entry = entry;
1143 }
1144
1145 static void __exit snd_timer_proc_done(void)
1146 {
1147         snd_info_free_entry(snd_timer_proc_entry);
1148 }
1149 #else /* !CONFIG_PROC_FS */
1150 #define snd_timer_proc_init()
1151 #define snd_timer_proc_done()
1152 #endif
1153
1154 /*
1155  *  USER SPACE interface
1156  */
1157
1158 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1159                                      unsigned long resolution,
1160                                      unsigned long ticks)
1161 {
1162         struct snd_timer_user *tu = timeri->callback_data;
1163         struct snd_timer_read *r;
1164         int prev;
1165
1166         spin_lock(&tu->qlock);
1167         if (tu->qused > 0) {
1168                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1169                 r = &tu->queue[prev];
1170                 if (r->resolution == resolution) {
1171                         r->ticks += ticks;
1172                         goto __wake;
1173                 }
1174         }
1175         if (tu->qused >= tu->queue_size) {
1176                 tu->overrun++;
1177         } else {
1178                 r = &tu->queue[tu->qtail++];
1179                 tu->qtail %= tu->queue_size;
1180                 r->resolution = resolution;
1181                 r->ticks = ticks;
1182                 tu->qused++;
1183         }
1184       __wake:
1185         spin_unlock(&tu->qlock);
1186         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1187         wake_up(&tu->qchange_sleep);
1188 }
1189
1190 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1191                                             struct snd_timer_tread *tread)
1192 {
1193         if (tu->qused >= tu->queue_size) {
1194                 tu->overrun++;
1195         } else {
1196                 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1197                 tu->qtail %= tu->queue_size;
1198                 tu->qused++;
1199         }
1200 }
1201
1202 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1203                                      int event,
1204                                      struct timespec *tstamp,
1205                                      unsigned long resolution)
1206 {
1207         struct snd_timer_user *tu = timeri->callback_data;
1208         struct snd_timer_tread r1;
1209         unsigned long flags;
1210
1211         if (event >= SNDRV_TIMER_EVENT_START &&
1212             event <= SNDRV_TIMER_EVENT_PAUSE)
1213                 tu->tstamp = *tstamp;
1214         if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1215                 return;
1216         r1.event = event;
1217         r1.tstamp = *tstamp;
1218         r1.val = resolution;
1219         spin_lock_irqsave(&tu->qlock, flags);
1220         snd_timer_user_append_to_tqueue(tu, &r1);
1221         spin_unlock_irqrestore(&tu->qlock, flags);
1222         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1223         wake_up(&tu->qchange_sleep);
1224 }
1225
1226 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1227                                       unsigned long resolution,
1228                                       unsigned long ticks)
1229 {
1230         struct snd_timer_user *tu = timeri->callback_data;
1231         struct snd_timer_tread *r, r1;
1232         struct timespec tstamp;
1233         int prev, append = 0;
1234
1235         memset(&tstamp, 0, sizeof(tstamp));
1236         spin_lock(&tu->qlock);
1237         if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1238                            (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1239                 spin_unlock(&tu->qlock);
1240                 return;
1241         }
1242         if (tu->last_resolution != resolution || ticks > 0) {
1243                 if (timer_tstamp_monotonic)
1244                         do_posix_clock_monotonic_gettime(&tstamp);
1245                 else
1246                         getnstimeofday(&tstamp);
1247         }
1248         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1249             tu->last_resolution != resolution) {
1250                 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1251                 r1.tstamp = tstamp;
1252                 r1.val = resolution;
1253                 snd_timer_user_append_to_tqueue(tu, &r1);
1254                 tu->last_resolution = resolution;
1255                 append++;
1256         }
1257         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1258                 goto __wake;
1259         if (ticks == 0)
1260                 goto __wake;
1261         if (tu->qused > 0) {
1262                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1263                 r = &tu->tqueue[prev];
1264                 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1265                         r->tstamp = tstamp;
1266                         r->val += ticks;
1267                         append++;
1268                         goto __wake;
1269                 }
1270         }
1271         r1.event = SNDRV_TIMER_EVENT_TICK;
1272         r1.tstamp = tstamp;
1273         r1.val = ticks;
1274         snd_timer_user_append_to_tqueue(tu, &r1);
1275         append++;
1276       __wake:
1277         spin_unlock(&tu->qlock);
1278         if (append == 0)
1279                 return;
1280         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1281         wake_up(&tu->qchange_sleep);
1282 }
1283
1284 static int snd_timer_user_open(struct inode *inode, struct file *file)
1285 {
1286         struct snd_timer_user *tu;
1287         int err;
1288
1289         err = nonseekable_open(inode, file);
1290         if (err < 0)
1291                 return err;
1292
1293         tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1294         if (tu == NULL)
1295                 return -ENOMEM;
1296         spin_lock_init(&tu->qlock);
1297         init_waitqueue_head(&tu->qchange_sleep);
1298         mutex_init(&tu->ioctl_lock);
1299         tu->ticks = 1;
1300         tu->queue_size = 128;
1301         tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1302                             GFP_KERNEL);
1303         if (tu->queue == NULL) {
1304                 kfree(tu);
1305                 return -ENOMEM;
1306         }
1307         file->private_data = tu;
1308         return 0;
1309 }
1310
1311 static int snd_timer_user_release(struct inode *inode, struct file *file)
1312 {
1313         struct snd_timer_user *tu;
1314
1315         if (file->private_data) {
1316                 tu = file->private_data;
1317                 file->private_data = NULL;
1318                 mutex_lock(&tu->ioctl_lock);
1319                 if (tu->timeri)
1320                         snd_timer_close(tu->timeri);
1321                 mutex_unlock(&tu->ioctl_lock);
1322                 kfree(tu->queue);
1323                 kfree(tu->tqueue);
1324                 kfree(tu);
1325         }
1326         return 0;
1327 }
1328
1329 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1330 {
1331         id->dev_class = SNDRV_TIMER_CLASS_NONE;
1332         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1333         id->card = -1;
1334         id->device = -1;
1335         id->subdevice = -1;
1336 }
1337
1338 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1339 {
1340         id->dev_class = timer->tmr_class;
1341         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1342         id->card = timer->card ? timer->card->number : -1;
1343         id->device = timer->tmr_device;
1344         id->subdevice = timer->tmr_subdevice;
1345 }
1346
1347 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1348 {
1349         struct snd_timer_id id;
1350         struct snd_timer *timer;
1351         struct list_head *p;
1352
1353         if (copy_from_user(&id, _tid, sizeof(id)))
1354                 return -EFAULT;
1355         mutex_lock(&register_mutex);
1356         if (id.dev_class < 0) {         /* first item */
1357                 if (list_empty(&snd_timer_list))
1358                         snd_timer_user_zero_id(&id);
1359                 else {
1360                         timer = list_entry(snd_timer_list.next,
1361                                            struct snd_timer, device_list);
1362                         snd_timer_user_copy_id(&id, timer);
1363                 }
1364         } else {
1365                 switch (id.dev_class) {
1366                 case SNDRV_TIMER_CLASS_GLOBAL:
1367                         id.device = id.device < 0 ? 0 : id.device + 1;
1368                         list_for_each(p, &snd_timer_list) {
1369                                 timer = list_entry(p, struct snd_timer, device_list);
1370                                 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1371                                         snd_timer_user_copy_id(&id, timer);
1372                                         break;
1373                                 }
1374                                 if (timer->tmr_device >= id.device) {
1375                                         snd_timer_user_copy_id(&id, timer);
1376                                         break;
1377                                 }
1378                         }
1379                         if (p == &snd_timer_list)
1380                                 snd_timer_user_zero_id(&id);
1381                         break;
1382                 case SNDRV_TIMER_CLASS_CARD:
1383                 case SNDRV_TIMER_CLASS_PCM:
1384                         if (id.card < 0) {
1385                                 id.card = 0;
1386                         } else {
1387                                 if (id.card < 0) {
1388                                         id.card = 0;
1389                                 } else {
1390                                         if (id.device < 0) {
1391                                                 id.device = 0;
1392                                         } else {
1393                                                 if (id.subdevice < 0) {
1394                                                         id.subdevice = 0;
1395                                                 } else {
1396                                                         id.subdevice++;
1397                                                 }
1398                                         }
1399                                 }
1400                         }
1401                         list_for_each(p, &snd_timer_list) {
1402                                 timer = list_entry(p, struct snd_timer, device_list);
1403                                 if (timer->tmr_class > id.dev_class) {
1404                                         snd_timer_user_copy_id(&id, timer);
1405                                         break;
1406                                 }
1407                                 if (timer->tmr_class < id.dev_class)
1408                                         continue;
1409                                 if (timer->card->number > id.card) {
1410                                         snd_timer_user_copy_id(&id, timer);
1411                                         break;
1412                                 }
1413                                 if (timer->card->number < id.card)
1414                                         continue;
1415                                 if (timer->tmr_device > id.device) {
1416                                         snd_timer_user_copy_id(&id, timer);
1417                                         break;
1418                                 }
1419                                 if (timer->tmr_device < id.device)
1420                                         continue;
1421                                 if (timer->tmr_subdevice > id.subdevice) {
1422                                         snd_timer_user_copy_id(&id, timer);
1423                                         break;
1424                                 }
1425                                 if (timer->tmr_subdevice < id.subdevice)
1426                                         continue;
1427                                 snd_timer_user_copy_id(&id, timer);
1428                                 break;
1429                         }
1430                         if (p == &snd_timer_list)
1431                                 snd_timer_user_zero_id(&id);
1432                         break;
1433                 default:
1434                         snd_timer_user_zero_id(&id);
1435                 }
1436         }
1437         mutex_unlock(&register_mutex);
1438         if (copy_to_user(_tid, &id, sizeof(*_tid)))
1439                 return -EFAULT;
1440         return 0;
1441 }
1442
1443 static int snd_timer_user_ginfo(struct file *file,
1444                                 struct snd_timer_ginfo __user *_ginfo)
1445 {
1446         struct snd_timer_ginfo *ginfo;
1447         struct snd_timer_id tid;
1448         struct snd_timer *t;
1449         struct list_head *p;
1450         int err = 0;
1451
1452         ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1453         if (IS_ERR(ginfo))
1454                 return PTR_ERR(ginfo);
1455
1456         tid = ginfo->tid;
1457         memset(ginfo, 0, sizeof(*ginfo));
1458         ginfo->tid = tid;
1459         mutex_lock(&register_mutex);
1460         t = snd_timer_find(&tid);
1461         if (t != NULL) {
1462                 ginfo->card = t->card ? t->card->number : -1;
1463                 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1464                         ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1465                 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1466                 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1467                 ginfo->resolution = t->hw.resolution;
1468                 if (t->hw.resolution_min > 0) {
1469                         ginfo->resolution_min = t->hw.resolution_min;
1470                         ginfo->resolution_max = t->hw.resolution_max;
1471                 }
1472                 list_for_each(p, &t->open_list_head) {
1473                         ginfo->clients++;
1474                 }
1475         } else {
1476                 err = -ENODEV;
1477         }
1478         mutex_unlock(&register_mutex);
1479         if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1480                 err = -EFAULT;
1481         kfree(ginfo);
1482         return err;
1483 }
1484
1485 static int snd_timer_user_gparams(struct file *file,
1486                                   struct snd_timer_gparams __user *_gparams)
1487 {
1488         struct snd_timer_gparams gparams;
1489         struct snd_timer *t;
1490         int err;
1491
1492         if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1493                 return -EFAULT;
1494         mutex_lock(&register_mutex);
1495         t = snd_timer_find(&gparams.tid);
1496         if (!t) {
1497                 err = -ENODEV;
1498                 goto _error;
1499         }
1500         if (!list_empty(&t->open_list_head)) {
1501                 err = -EBUSY;
1502                 goto _error;
1503         }
1504         if (!t->hw.set_period) {
1505                 err = -ENOSYS;
1506                 goto _error;
1507         }
1508         err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1509 _error:
1510         mutex_unlock(&register_mutex);
1511         return err;
1512 }
1513
1514 static int snd_timer_user_gstatus(struct file *file,
1515                                   struct snd_timer_gstatus __user *_gstatus)
1516 {
1517         struct snd_timer_gstatus gstatus;
1518         struct snd_timer_id tid;
1519         struct snd_timer *t;
1520         int err = 0;
1521
1522         if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1523                 return -EFAULT;
1524         tid = gstatus.tid;
1525         memset(&gstatus, 0, sizeof(gstatus));
1526         gstatus.tid = tid;
1527         mutex_lock(&register_mutex);
1528         t = snd_timer_find(&tid);
1529         if (t != NULL) {
1530                 if (t->hw.c_resolution)
1531                         gstatus.resolution = t->hw.c_resolution(t);
1532                 else
1533                         gstatus.resolution = t->hw.resolution;
1534                 if (t->hw.precise_resolution) {
1535                         t->hw.precise_resolution(t, &gstatus.resolution_num,
1536                                                  &gstatus.resolution_den);
1537                 } else {
1538                         gstatus.resolution_num = gstatus.resolution;
1539                         gstatus.resolution_den = 1000000000uL;
1540                 }
1541         } else {
1542                 err = -ENODEV;
1543         }
1544         mutex_unlock(&register_mutex);
1545         if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1546                 err = -EFAULT;
1547         return err;
1548 }
1549
1550 static int snd_timer_user_tselect(struct file *file,
1551                                   struct snd_timer_select __user *_tselect)
1552 {
1553         struct snd_timer_user *tu;
1554         struct snd_timer_select tselect;
1555         char str[32];
1556         int err = 0;
1557
1558         tu = file->private_data;
1559         if (tu->timeri) {
1560                 snd_timer_close(tu->timeri);
1561                 tu->timeri = NULL;
1562         }
1563         if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1564                 err = -EFAULT;
1565                 goto __err;
1566         }
1567         sprintf(str, "application %i", current->pid);
1568         if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1569                 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1570         err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1571         if (err < 0)
1572                 goto __err;
1573
1574         kfree(tu->queue);
1575         tu->queue = NULL;
1576         kfree(tu->tqueue);
1577         tu->tqueue = NULL;
1578         if (tu->tread) {
1579                 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1580                                      GFP_KERNEL);
1581                 if (tu->tqueue == NULL)
1582                         err = -ENOMEM;
1583         } else {
1584                 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1585                                     GFP_KERNEL);
1586                 if (tu->queue == NULL)
1587                         err = -ENOMEM;
1588         }
1589
1590         if (err < 0) {
1591                 snd_timer_close(tu->timeri);
1592                 tu->timeri = NULL;
1593         } else {
1594                 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1595                 tu->timeri->callback = tu->tread
1596                         ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1597                 tu->timeri->ccallback = snd_timer_user_ccallback;
1598                 tu->timeri->callback_data = (void *)tu;
1599         }
1600
1601       __err:
1602         return err;
1603 }
1604
1605 static int snd_timer_user_info(struct file *file,
1606                                struct snd_timer_info __user *_info)
1607 {
1608         struct snd_timer_user *tu;
1609         struct snd_timer_info *info;
1610         struct snd_timer *t;
1611         int err = 0;
1612
1613         tu = file->private_data;
1614         if (!tu->timeri)
1615                 return -EBADFD;
1616         t = tu->timeri->timer;
1617         if (!t)
1618                 return -EBADFD;
1619
1620         info = kzalloc(sizeof(*info), GFP_KERNEL);
1621         if (! info)
1622                 return -ENOMEM;
1623         info->card = t->card ? t->card->number : -1;
1624         if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1625                 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1626         strlcpy(info->id, t->id, sizeof(info->id));
1627         strlcpy(info->name, t->name, sizeof(info->name));
1628         info->resolution = t->hw.resolution;
1629         if (copy_to_user(_info, info, sizeof(*_info)))
1630                 err = -EFAULT;
1631         kfree(info);
1632         return err;
1633 }
1634
1635 static int snd_timer_user_params(struct file *file,
1636                                  struct snd_timer_params __user *_params)
1637 {
1638         struct snd_timer_user *tu;
1639         struct snd_timer_params params;
1640         struct snd_timer *t;
1641         struct snd_timer_read *tr;
1642         struct snd_timer_tread *ttr;
1643         int err;
1644
1645         tu = file->private_data;
1646         if (!tu->timeri)
1647                 return -EBADFD;
1648         t = tu->timeri->timer;
1649         if (!t)
1650                 return -EBADFD;
1651         if (copy_from_user(&params, _params, sizeof(params)))
1652                 return -EFAULT;
1653         if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1654                 err = -EINVAL;
1655                 goto _end;
1656         }
1657         if (params.queue_size > 0 &&
1658             (params.queue_size < 32 || params.queue_size > 1024)) {
1659                 err = -EINVAL;
1660                 goto _end;
1661         }
1662         if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1663                               (1<<SNDRV_TIMER_EVENT_TICK)|
1664                               (1<<SNDRV_TIMER_EVENT_START)|
1665                               (1<<SNDRV_TIMER_EVENT_STOP)|
1666                               (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1667                               (1<<SNDRV_TIMER_EVENT_PAUSE)|
1668                               (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1669                               (1<<SNDRV_TIMER_EVENT_RESUME)|
1670                               (1<<SNDRV_TIMER_EVENT_MSTART)|
1671                               (1<<SNDRV_TIMER_EVENT_MSTOP)|
1672                               (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1673                               (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1674                               (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1675                               (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1676                 err = -EINVAL;
1677                 goto _end;
1678         }
1679         snd_timer_stop(tu->timeri);
1680         spin_lock_irq(&t->lock);
1681         tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1682                                SNDRV_TIMER_IFLG_EXCLUSIVE|
1683                                SNDRV_TIMER_IFLG_EARLY_EVENT);
1684         if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1685                 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1686         if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1687                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1688         if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1689                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1690         spin_unlock_irq(&t->lock);
1691         if (params.queue_size > 0 &&
1692             (unsigned int)tu->queue_size != params.queue_size) {
1693                 if (tu->tread) {
1694                         ttr = kmalloc(params.queue_size * sizeof(*ttr),
1695                                       GFP_KERNEL);
1696                         if (ttr) {
1697                                 kfree(tu->tqueue);
1698                                 tu->queue_size = params.queue_size;
1699                                 tu->tqueue = ttr;
1700                         }
1701                 } else {
1702                         tr = kmalloc(params.queue_size * sizeof(*tr),
1703                                      GFP_KERNEL);
1704                         if (tr) {
1705                                 kfree(tu->queue);
1706                                 tu->queue_size = params.queue_size;
1707                                 tu->queue = tr;
1708                         }
1709                 }
1710         }
1711         tu->qhead = tu->qtail = tu->qused = 0;
1712         if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1713                 if (tu->tread) {
1714                         struct snd_timer_tread tread;
1715                         tread.event = SNDRV_TIMER_EVENT_EARLY;
1716                         tread.tstamp.tv_sec = 0;
1717                         tread.tstamp.tv_nsec = 0;
1718                         tread.val = 0;
1719                         snd_timer_user_append_to_tqueue(tu, &tread);
1720                 } else {
1721                         struct snd_timer_read *r = &tu->queue[0];
1722                         r->resolution = 0;
1723                         r->ticks = 0;
1724                         tu->qused++;
1725                         tu->qtail++;
1726                 }
1727         }
1728         tu->filter = params.filter;
1729         tu->ticks = params.ticks;
1730         err = 0;
1731  _end:
1732         if (copy_to_user(_params, &params, sizeof(params)))
1733                 return -EFAULT;
1734         return err;
1735 }
1736
1737 static int snd_timer_user_status(struct file *file,
1738                                  struct snd_timer_status __user *_status)
1739 {
1740         struct snd_timer_user *tu;
1741         struct snd_timer_status status;
1742
1743         tu = file->private_data;
1744         if (!tu->timeri)
1745                 return -EBADFD;
1746         memset(&status, 0, sizeof(status));
1747         status.tstamp = tu->tstamp;
1748         status.resolution = snd_timer_resolution(tu->timeri);
1749         status.lost = tu->timeri->lost;
1750         status.overrun = tu->overrun;
1751         spin_lock_irq(&tu->qlock);
1752         status.queue = tu->qused;
1753         spin_unlock_irq(&tu->qlock);
1754         if (copy_to_user(_status, &status, sizeof(status)))
1755                 return -EFAULT;
1756         return 0;
1757 }
1758
1759 static int snd_timer_user_start(struct file *file)
1760 {
1761         int err;
1762         struct snd_timer_user *tu;
1763
1764         tu = file->private_data;
1765         if (!tu->timeri)
1766                 return -EBADFD;
1767         snd_timer_stop(tu->timeri);
1768         tu->timeri->lost = 0;
1769         tu->last_resolution = 0;
1770         return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1771 }
1772
1773 static int snd_timer_user_stop(struct file *file)
1774 {
1775         int err;
1776         struct snd_timer_user *tu;
1777
1778         tu = file->private_data;
1779         if (!tu->timeri)
1780                 return -EBADFD;
1781         return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1782 }
1783
1784 static int snd_timer_user_continue(struct file *file)
1785 {
1786         int err;
1787         struct snd_timer_user *tu;
1788
1789         tu = file->private_data;
1790         if (!tu->timeri)
1791                 return -EBADFD;
1792         tu->timeri->lost = 0;
1793         return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1794 }
1795
1796 static int snd_timer_user_pause(struct file *file)
1797 {
1798         int err;
1799         struct snd_timer_user *tu;
1800
1801         tu = file->private_data;
1802         if (!tu->timeri)
1803                 return -EBADFD;
1804         return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1805 }
1806
1807 enum {
1808         SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1809         SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1810         SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1811         SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1812 };
1813
1814 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1815                                  unsigned long arg)
1816 {
1817         struct snd_timer_user *tu;
1818         void __user *argp = (void __user *)arg;
1819         int __user *p = argp;
1820
1821         tu = file->private_data;
1822         switch (cmd) {
1823         case SNDRV_TIMER_IOCTL_PVERSION:
1824                 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1825         case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1826                 return snd_timer_user_next_device(argp);
1827         case SNDRV_TIMER_IOCTL_TREAD:
1828         {
1829                 int xarg;
1830
1831                 if (tu->timeri) /* too late */
1832                         return -EBUSY;
1833                 if (get_user(xarg, p))
1834                         return -EFAULT;
1835                 tu->tread = xarg ? 1 : 0;
1836                 return 0;
1837         }
1838         case SNDRV_TIMER_IOCTL_GINFO:
1839                 return snd_timer_user_ginfo(file, argp);
1840         case SNDRV_TIMER_IOCTL_GPARAMS:
1841                 return snd_timer_user_gparams(file, argp);
1842         case SNDRV_TIMER_IOCTL_GSTATUS:
1843                 return snd_timer_user_gstatus(file, argp);
1844         case SNDRV_TIMER_IOCTL_SELECT:
1845                 return snd_timer_user_tselect(file, argp);
1846         case SNDRV_TIMER_IOCTL_INFO:
1847                 return snd_timer_user_info(file, argp);
1848         case SNDRV_TIMER_IOCTL_PARAMS:
1849                 return snd_timer_user_params(file, argp);
1850         case SNDRV_TIMER_IOCTL_STATUS:
1851                 return snd_timer_user_status(file, argp);
1852         case SNDRV_TIMER_IOCTL_START:
1853         case SNDRV_TIMER_IOCTL_START_OLD:
1854                 return snd_timer_user_start(file);
1855         case SNDRV_TIMER_IOCTL_STOP:
1856         case SNDRV_TIMER_IOCTL_STOP_OLD:
1857                 return snd_timer_user_stop(file);
1858         case SNDRV_TIMER_IOCTL_CONTINUE:
1859         case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1860                 return snd_timer_user_continue(file);
1861         case SNDRV_TIMER_IOCTL_PAUSE:
1862         case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1863                 return snd_timer_user_pause(file);
1864         }
1865         return -ENOTTY;
1866 }
1867
1868 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1869                                  unsigned long arg)
1870 {
1871         struct snd_timer_user *tu = file->private_data;
1872         long ret;
1873
1874         mutex_lock(&tu->ioctl_lock);
1875         ret = __snd_timer_user_ioctl(file, cmd, arg);
1876         mutex_unlock(&tu->ioctl_lock);
1877         return ret;
1878 }
1879
1880 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1881 {
1882         struct snd_timer_user *tu;
1883
1884         tu = file->private_data;
1885         return fasync_helper(fd, file, on, &tu->fasync);
1886 }
1887
1888 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1889                                    size_t count, loff_t *offset)
1890 {
1891         struct snd_timer_user *tu;
1892         long result = 0, unit;
1893         int qhead;
1894         int err = 0;
1895
1896         tu = file->private_data;
1897         unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1898         spin_lock_irq(&tu->qlock);
1899         while ((long)count - result >= unit) {
1900                 while (!tu->qused) {
1901                         wait_queue_t wait;
1902
1903                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1904                                 err = -EAGAIN;
1905                                 goto _error;
1906                         }
1907
1908                         set_current_state(TASK_INTERRUPTIBLE);
1909                         init_waitqueue_entry(&wait, current);
1910                         add_wait_queue(&tu->qchange_sleep, &wait);
1911
1912                         spin_unlock_irq(&tu->qlock);
1913                         schedule();
1914                         spin_lock_irq(&tu->qlock);
1915
1916                         remove_wait_queue(&tu->qchange_sleep, &wait);
1917
1918                         if (signal_pending(current)) {
1919                                 err = -ERESTARTSYS;
1920                                 goto _error;
1921                         }
1922                 }
1923
1924                 qhead = tu->qhead++;
1925                 tu->qhead %= tu->queue_size;
1926                 spin_unlock_irq(&tu->qlock);
1927
1928                 if (tu->tread) {
1929                         if (copy_to_user(buffer, &tu->tqueue[qhead],
1930                                          sizeof(struct snd_timer_tread)))
1931                                 err = -EFAULT;
1932                 } else {
1933                         if (copy_to_user(buffer, &tu->queue[qhead],
1934                                          sizeof(struct snd_timer_read)))
1935                                 err = -EFAULT;
1936                 }
1937
1938                 spin_lock_irq(&tu->qlock);
1939                 tu->qused--;
1940                 if (err < 0)
1941                         goto _error;
1942                 result += unit;
1943                 buffer += unit;
1944         }
1945  _error:
1946         spin_unlock_irq(&tu->qlock);
1947         return result > 0 ? result : err;
1948 }
1949
1950 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1951 {
1952         unsigned int mask;
1953         struct snd_timer_user *tu;
1954
1955         tu = file->private_data;
1956
1957         poll_wait(file, &tu->qchange_sleep, wait);
1958
1959         mask = 0;
1960         if (tu->qused)
1961                 mask |= POLLIN | POLLRDNORM;
1962
1963         return mask;
1964 }
1965
1966 #ifdef CONFIG_COMPAT
1967 #include "timer_compat.c"
1968 #else
1969 #define snd_timer_user_ioctl_compat     NULL
1970 #endif
1971
1972 static const struct file_operations snd_timer_f_ops =
1973 {
1974         .owner =        THIS_MODULE,
1975         .read =         snd_timer_user_read,
1976         .open =         snd_timer_user_open,
1977         .release =      snd_timer_user_release,
1978         .llseek =       no_llseek,
1979         .poll =         snd_timer_user_poll,
1980         .unlocked_ioctl =       snd_timer_user_ioctl,
1981         .compat_ioctl = snd_timer_user_ioctl_compat,
1982         .fasync =       snd_timer_user_fasync,
1983 };
1984
1985 /*
1986  *  ENTRY functions
1987  */
1988
1989 static int __init alsa_timer_init(void)
1990 {
1991         int err;
1992
1993 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1994         snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1995                               "system timer");
1996 #endif
1997
1998         if ((err = snd_timer_register_system()) < 0)
1999                 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
2000                            err);
2001         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2002                                        &snd_timer_f_ops, NULL, "timer")) < 0)
2003                 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
2004                            err);
2005         snd_timer_proc_init();
2006         return 0;
2007 }
2008
2009 static void __exit alsa_timer_exit(void)
2010 {
2011         struct list_head *p, *n;
2012
2013         snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
2014         /* unregister the system timer */
2015         list_for_each_safe(p, n, &snd_timer_list) {
2016                 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
2017                 snd_timer_free(timer);
2018         }
2019         snd_timer_proc_done();
2020 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2021         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2022 #endif
2023 }
2024
2025 module_init(alsa_timer_init)
2026 module_exit(alsa_timer_exit)
2027
2028 EXPORT_SYMBOL(snd_timer_open);
2029 EXPORT_SYMBOL(snd_timer_close);
2030 EXPORT_SYMBOL(snd_timer_resolution);
2031 EXPORT_SYMBOL(snd_timer_start);
2032 EXPORT_SYMBOL(snd_timer_stop);
2033 EXPORT_SYMBOL(snd_timer_continue);
2034 EXPORT_SYMBOL(snd_timer_pause);
2035 EXPORT_SYMBOL(snd_timer_new);
2036 EXPORT_SYMBOL(snd_timer_notify);
2037 EXPORT_SYMBOL(snd_timer_global_new);
2038 EXPORT_SYMBOL(snd_timer_global_free);
2039 EXPORT_SYMBOL(snd_timer_global_register);
2040 EXPORT_SYMBOL(snd_timer_interrupt);