bce3fe051fbf066231be66327e3f5257c3d2d996
[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 = njiff;
1021         mod_timer(&priv->tlist, njiff);
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         memset(&r1, 0, sizeof(r1));
1217         r1.event = event;
1218         r1.tstamp = *tstamp;
1219         r1.val = resolution;
1220         spin_lock_irqsave(&tu->qlock, flags);
1221         snd_timer_user_append_to_tqueue(tu, &r1);
1222         spin_unlock_irqrestore(&tu->qlock, flags);
1223         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1224         wake_up(&tu->qchange_sleep);
1225 }
1226
1227 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1228                                       unsigned long resolution,
1229                                       unsigned long ticks)
1230 {
1231         struct snd_timer_user *tu = timeri->callback_data;
1232         struct snd_timer_tread *r, r1;
1233         struct timespec tstamp;
1234         int prev, append = 0;
1235
1236         memset(&tstamp, 0, sizeof(tstamp));
1237         spin_lock(&tu->qlock);
1238         if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1239                            (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1240                 spin_unlock(&tu->qlock);
1241                 return;
1242         }
1243         if (tu->last_resolution != resolution || ticks > 0) {
1244                 if (timer_tstamp_monotonic)
1245                         do_posix_clock_monotonic_gettime(&tstamp);
1246                 else
1247                         getnstimeofday(&tstamp);
1248         }
1249         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1250             tu->last_resolution != resolution) {
1251                 memset(&r1, 0, sizeof(r1));
1252                 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1253                 r1.tstamp = tstamp;
1254                 r1.val = resolution;
1255                 snd_timer_user_append_to_tqueue(tu, &r1);
1256                 tu->last_resolution = resolution;
1257                 append++;
1258         }
1259         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1260                 goto __wake;
1261         if (ticks == 0)
1262                 goto __wake;
1263         if (tu->qused > 0) {
1264                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1265                 r = &tu->tqueue[prev];
1266                 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1267                         r->tstamp = tstamp;
1268                         r->val += ticks;
1269                         append++;
1270                         goto __wake;
1271                 }
1272         }
1273         r1.event = SNDRV_TIMER_EVENT_TICK;
1274         r1.tstamp = tstamp;
1275         r1.val = ticks;
1276         snd_timer_user_append_to_tqueue(tu, &r1);
1277         append++;
1278       __wake:
1279         spin_unlock(&tu->qlock);
1280         if (append == 0)
1281                 return;
1282         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1283         wake_up(&tu->qchange_sleep);
1284 }
1285
1286 static int snd_timer_user_open(struct inode *inode, struct file *file)
1287 {
1288         struct snd_timer_user *tu;
1289         int err;
1290
1291         err = nonseekable_open(inode, file);
1292         if (err < 0)
1293                 return err;
1294
1295         tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1296         if (tu == NULL)
1297                 return -ENOMEM;
1298         spin_lock_init(&tu->qlock);
1299         init_waitqueue_head(&tu->qchange_sleep);
1300         mutex_init(&tu->ioctl_lock);
1301         tu->ticks = 1;
1302         tu->queue_size = 128;
1303         tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1304                             GFP_KERNEL);
1305         if (tu->queue == NULL) {
1306                 kfree(tu);
1307                 return -ENOMEM;
1308         }
1309         file->private_data = tu;
1310         return 0;
1311 }
1312
1313 static int snd_timer_user_release(struct inode *inode, struct file *file)
1314 {
1315         struct snd_timer_user *tu;
1316
1317         if (file->private_data) {
1318                 tu = file->private_data;
1319                 file->private_data = NULL;
1320                 mutex_lock(&tu->ioctl_lock);
1321                 if (tu->timeri)
1322                         snd_timer_close(tu->timeri);
1323                 mutex_unlock(&tu->ioctl_lock);
1324                 kfree(tu->queue);
1325                 kfree(tu->tqueue);
1326                 kfree(tu);
1327         }
1328         return 0;
1329 }
1330
1331 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1332 {
1333         id->dev_class = SNDRV_TIMER_CLASS_NONE;
1334         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1335         id->card = -1;
1336         id->device = -1;
1337         id->subdevice = -1;
1338 }
1339
1340 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1341 {
1342         id->dev_class = timer->tmr_class;
1343         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1344         id->card = timer->card ? timer->card->number : -1;
1345         id->device = timer->tmr_device;
1346         id->subdevice = timer->tmr_subdevice;
1347 }
1348
1349 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1350 {
1351         struct snd_timer_id id;
1352         struct snd_timer *timer;
1353         struct list_head *p;
1354
1355         if (copy_from_user(&id, _tid, sizeof(id)))
1356                 return -EFAULT;
1357         mutex_lock(&register_mutex);
1358         if (id.dev_class < 0) {         /* first item */
1359                 if (list_empty(&snd_timer_list))
1360                         snd_timer_user_zero_id(&id);
1361                 else {
1362                         timer = list_entry(snd_timer_list.next,
1363                                            struct snd_timer, device_list);
1364                         snd_timer_user_copy_id(&id, timer);
1365                 }
1366         } else {
1367                 switch (id.dev_class) {
1368                 case SNDRV_TIMER_CLASS_GLOBAL:
1369                         id.device = id.device < 0 ? 0 : id.device + 1;
1370                         list_for_each(p, &snd_timer_list) {
1371                                 timer = list_entry(p, struct snd_timer, device_list);
1372                                 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1373                                         snd_timer_user_copy_id(&id, timer);
1374                                         break;
1375                                 }
1376                                 if (timer->tmr_device >= id.device) {
1377                                         snd_timer_user_copy_id(&id, timer);
1378                                         break;
1379                                 }
1380                         }
1381                         if (p == &snd_timer_list)
1382                                 snd_timer_user_zero_id(&id);
1383                         break;
1384                 case SNDRV_TIMER_CLASS_CARD:
1385                 case SNDRV_TIMER_CLASS_PCM:
1386                         if (id.card < 0) {
1387                                 id.card = 0;
1388                         } else {
1389                                 if (id.card < 0) {
1390                                         id.card = 0;
1391                                 } else {
1392                                         if (id.device < 0) {
1393                                                 id.device = 0;
1394                                         } else {
1395                                                 if (id.subdevice < 0) {
1396                                                         id.subdevice = 0;
1397                                                 } else {
1398                                                         id.subdevice++;
1399                                                 }
1400                                         }
1401                                 }
1402                         }
1403                         list_for_each(p, &snd_timer_list) {
1404                                 timer = list_entry(p, struct snd_timer, device_list);
1405                                 if (timer->tmr_class > id.dev_class) {
1406                                         snd_timer_user_copy_id(&id, timer);
1407                                         break;
1408                                 }
1409                                 if (timer->tmr_class < id.dev_class)
1410                                         continue;
1411                                 if (timer->card->number > id.card) {
1412                                         snd_timer_user_copy_id(&id, timer);
1413                                         break;
1414                                 }
1415                                 if (timer->card->number < id.card)
1416                                         continue;
1417                                 if (timer->tmr_device > id.device) {
1418                                         snd_timer_user_copy_id(&id, timer);
1419                                         break;
1420                                 }
1421                                 if (timer->tmr_device < id.device)
1422                                         continue;
1423                                 if (timer->tmr_subdevice > id.subdevice) {
1424                                         snd_timer_user_copy_id(&id, timer);
1425                                         break;
1426                                 }
1427                                 if (timer->tmr_subdevice < id.subdevice)
1428                                         continue;
1429                                 snd_timer_user_copy_id(&id, timer);
1430                                 break;
1431                         }
1432                         if (p == &snd_timer_list)
1433                                 snd_timer_user_zero_id(&id);
1434                         break;
1435                 default:
1436                         snd_timer_user_zero_id(&id);
1437                 }
1438         }
1439         mutex_unlock(&register_mutex);
1440         if (copy_to_user(_tid, &id, sizeof(*_tid)))
1441                 return -EFAULT;
1442         return 0;
1443 }
1444
1445 static int snd_timer_user_ginfo(struct file *file,
1446                                 struct snd_timer_ginfo __user *_ginfo)
1447 {
1448         struct snd_timer_ginfo *ginfo;
1449         struct snd_timer_id tid;
1450         struct snd_timer *t;
1451         struct list_head *p;
1452         int err = 0;
1453
1454         ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1455         if (IS_ERR(ginfo))
1456                 return PTR_ERR(ginfo);
1457
1458         tid = ginfo->tid;
1459         memset(ginfo, 0, sizeof(*ginfo));
1460         ginfo->tid = tid;
1461         mutex_lock(&register_mutex);
1462         t = snd_timer_find(&tid);
1463         if (t != NULL) {
1464                 ginfo->card = t->card ? t->card->number : -1;
1465                 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1466                         ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1467                 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1468                 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1469                 ginfo->resolution = t->hw.resolution;
1470                 if (t->hw.resolution_min > 0) {
1471                         ginfo->resolution_min = t->hw.resolution_min;
1472                         ginfo->resolution_max = t->hw.resolution_max;
1473                 }
1474                 list_for_each(p, &t->open_list_head) {
1475                         ginfo->clients++;
1476                 }
1477         } else {
1478                 err = -ENODEV;
1479         }
1480         mutex_unlock(&register_mutex);
1481         if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1482                 err = -EFAULT;
1483         kfree(ginfo);
1484         return err;
1485 }
1486
1487 static int snd_timer_user_gparams(struct file *file,
1488                                   struct snd_timer_gparams __user *_gparams)
1489 {
1490         struct snd_timer_gparams gparams;
1491         struct snd_timer *t;
1492         int err;
1493
1494         if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1495                 return -EFAULT;
1496         mutex_lock(&register_mutex);
1497         t = snd_timer_find(&gparams.tid);
1498         if (!t) {
1499                 err = -ENODEV;
1500                 goto _error;
1501         }
1502         if (!list_empty(&t->open_list_head)) {
1503                 err = -EBUSY;
1504                 goto _error;
1505         }
1506         if (!t->hw.set_period) {
1507                 err = -ENOSYS;
1508                 goto _error;
1509         }
1510         err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1511 _error:
1512         mutex_unlock(&register_mutex);
1513         return err;
1514 }
1515
1516 static int snd_timer_user_gstatus(struct file *file,
1517                                   struct snd_timer_gstatus __user *_gstatus)
1518 {
1519         struct snd_timer_gstatus gstatus;
1520         struct snd_timer_id tid;
1521         struct snd_timer *t;
1522         int err = 0;
1523
1524         if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1525                 return -EFAULT;
1526         tid = gstatus.tid;
1527         memset(&gstatus, 0, sizeof(gstatus));
1528         gstatus.tid = tid;
1529         mutex_lock(&register_mutex);
1530         t = snd_timer_find(&tid);
1531         if (t != NULL) {
1532                 if (t->hw.c_resolution)
1533                         gstatus.resolution = t->hw.c_resolution(t);
1534                 else
1535                         gstatus.resolution = t->hw.resolution;
1536                 if (t->hw.precise_resolution) {
1537                         t->hw.precise_resolution(t, &gstatus.resolution_num,
1538                                                  &gstatus.resolution_den);
1539                 } else {
1540                         gstatus.resolution_num = gstatus.resolution;
1541                         gstatus.resolution_den = 1000000000uL;
1542                 }
1543         } else {
1544                 err = -ENODEV;
1545         }
1546         mutex_unlock(&register_mutex);
1547         if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1548                 err = -EFAULT;
1549         return err;
1550 }
1551
1552 static int snd_timer_user_tselect(struct file *file,
1553                                   struct snd_timer_select __user *_tselect)
1554 {
1555         struct snd_timer_user *tu;
1556         struct snd_timer_select tselect;
1557         char str[32];
1558         int err = 0;
1559
1560         tu = file->private_data;
1561         if (tu->timeri) {
1562                 snd_timer_close(tu->timeri);
1563                 tu->timeri = NULL;
1564         }
1565         if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1566                 err = -EFAULT;
1567                 goto __err;
1568         }
1569         sprintf(str, "application %i", current->pid);
1570         if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1571                 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1572         err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1573         if (err < 0)
1574                 goto __err;
1575
1576         kfree(tu->queue);
1577         tu->queue = NULL;
1578         kfree(tu->tqueue);
1579         tu->tqueue = NULL;
1580         if (tu->tread) {
1581                 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1582                                      GFP_KERNEL);
1583                 if (tu->tqueue == NULL)
1584                         err = -ENOMEM;
1585         } else {
1586                 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1587                                     GFP_KERNEL);
1588                 if (tu->queue == NULL)
1589                         err = -ENOMEM;
1590         }
1591
1592         if (err < 0) {
1593                 snd_timer_close(tu->timeri);
1594                 tu->timeri = NULL;
1595         } else {
1596                 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1597                 tu->timeri->callback = tu->tread
1598                         ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1599                 tu->timeri->ccallback = snd_timer_user_ccallback;
1600                 tu->timeri->callback_data = (void *)tu;
1601         }
1602
1603       __err:
1604         return err;
1605 }
1606
1607 static int snd_timer_user_info(struct file *file,
1608                                struct snd_timer_info __user *_info)
1609 {
1610         struct snd_timer_user *tu;
1611         struct snd_timer_info *info;
1612         struct snd_timer *t;
1613         int err = 0;
1614
1615         tu = file->private_data;
1616         if (!tu->timeri)
1617                 return -EBADFD;
1618         t = tu->timeri->timer;
1619         if (!t)
1620                 return -EBADFD;
1621
1622         info = kzalloc(sizeof(*info), GFP_KERNEL);
1623         if (! info)
1624                 return -ENOMEM;
1625         info->card = t->card ? t->card->number : -1;
1626         if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1627                 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1628         strlcpy(info->id, t->id, sizeof(info->id));
1629         strlcpy(info->name, t->name, sizeof(info->name));
1630         info->resolution = t->hw.resolution;
1631         if (copy_to_user(_info, info, sizeof(*_info)))
1632                 err = -EFAULT;
1633         kfree(info);
1634         return err;
1635 }
1636
1637 static int snd_timer_user_params(struct file *file,
1638                                  struct snd_timer_params __user *_params)
1639 {
1640         struct snd_timer_user *tu;
1641         struct snd_timer_params params;
1642         struct snd_timer *t;
1643         struct snd_timer_read *tr;
1644         struct snd_timer_tread *ttr;
1645         int err;
1646
1647         tu = file->private_data;
1648         if (!tu->timeri)
1649                 return -EBADFD;
1650         t = tu->timeri->timer;
1651         if (!t)
1652                 return -EBADFD;
1653         if (copy_from_user(&params, _params, sizeof(params)))
1654                 return -EFAULT;
1655         if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1656                 err = -EINVAL;
1657                 goto _end;
1658         }
1659         if (params.queue_size > 0 &&
1660             (params.queue_size < 32 || params.queue_size > 1024)) {
1661                 err = -EINVAL;
1662                 goto _end;
1663         }
1664         if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1665                               (1<<SNDRV_TIMER_EVENT_TICK)|
1666                               (1<<SNDRV_TIMER_EVENT_START)|
1667                               (1<<SNDRV_TIMER_EVENT_STOP)|
1668                               (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1669                               (1<<SNDRV_TIMER_EVENT_PAUSE)|
1670                               (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1671                               (1<<SNDRV_TIMER_EVENT_RESUME)|
1672                               (1<<SNDRV_TIMER_EVENT_MSTART)|
1673                               (1<<SNDRV_TIMER_EVENT_MSTOP)|
1674                               (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1675                               (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1676                               (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1677                               (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1678                 err = -EINVAL;
1679                 goto _end;
1680         }
1681         snd_timer_stop(tu->timeri);
1682         spin_lock_irq(&t->lock);
1683         tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1684                                SNDRV_TIMER_IFLG_EXCLUSIVE|
1685                                SNDRV_TIMER_IFLG_EARLY_EVENT);
1686         if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1687                 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1688         if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1689                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1690         if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1691                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1692         spin_unlock_irq(&t->lock);
1693         if (params.queue_size > 0 &&
1694             (unsigned int)tu->queue_size != params.queue_size) {
1695                 if (tu->tread) {
1696                         ttr = kmalloc(params.queue_size * sizeof(*ttr),
1697                                       GFP_KERNEL);
1698                         if (ttr) {
1699                                 kfree(tu->tqueue);
1700                                 tu->queue_size = params.queue_size;
1701                                 tu->tqueue = ttr;
1702                         }
1703                 } else {
1704                         tr = kmalloc(params.queue_size * sizeof(*tr),
1705                                      GFP_KERNEL);
1706                         if (tr) {
1707                                 kfree(tu->queue);
1708                                 tu->queue_size = params.queue_size;
1709                                 tu->queue = tr;
1710                         }
1711                 }
1712         }
1713         tu->qhead = tu->qtail = tu->qused = 0;
1714         if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1715                 if (tu->tread) {
1716                         struct snd_timer_tread tread;
1717                         memset(&tread, 0, sizeof(tread));
1718                         tread.event = SNDRV_TIMER_EVENT_EARLY;
1719                         tread.tstamp.tv_sec = 0;
1720                         tread.tstamp.tv_nsec = 0;
1721                         tread.val = 0;
1722                         snd_timer_user_append_to_tqueue(tu, &tread);
1723                 } else {
1724                         struct snd_timer_read *r = &tu->queue[0];
1725                         r->resolution = 0;
1726                         r->ticks = 0;
1727                         tu->qused++;
1728                         tu->qtail++;
1729                 }
1730         }
1731         tu->filter = params.filter;
1732         tu->ticks = params.ticks;
1733         err = 0;
1734  _end:
1735         if (copy_to_user(_params, &params, sizeof(params)))
1736                 return -EFAULT;
1737         return err;
1738 }
1739
1740 static int snd_timer_user_status(struct file *file,
1741                                  struct snd_timer_status __user *_status)
1742 {
1743         struct snd_timer_user *tu;
1744         struct snd_timer_status status;
1745
1746         tu = file->private_data;
1747         if (!tu->timeri)
1748                 return -EBADFD;
1749         memset(&status, 0, sizeof(status));
1750         status.tstamp = tu->tstamp;
1751         status.resolution = snd_timer_resolution(tu->timeri);
1752         status.lost = tu->timeri->lost;
1753         status.overrun = tu->overrun;
1754         spin_lock_irq(&tu->qlock);
1755         status.queue = tu->qused;
1756         spin_unlock_irq(&tu->qlock);
1757         if (copy_to_user(_status, &status, sizeof(status)))
1758                 return -EFAULT;
1759         return 0;
1760 }
1761
1762 static int snd_timer_user_start(struct file *file)
1763 {
1764         int err;
1765         struct snd_timer_user *tu;
1766
1767         tu = file->private_data;
1768         if (!tu->timeri)
1769                 return -EBADFD;
1770         snd_timer_stop(tu->timeri);
1771         tu->timeri->lost = 0;
1772         tu->last_resolution = 0;
1773         return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1774 }
1775
1776 static int snd_timer_user_stop(struct file *file)
1777 {
1778         int err;
1779         struct snd_timer_user *tu;
1780
1781         tu = file->private_data;
1782         if (!tu->timeri)
1783                 return -EBADFD;
1784         return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1785 }
1786
1787 static int snd_timer_user_continue(struct file *file)
1788 {
1789         int err;
1790         struct snd_timer_user *tu;
1791
1792         tu = file->private_data;
1793         if (!tu->timeri)
1794                 return -EBADFD;
1795         tu->timeri->lost = 0;
1796         return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1797 }
1798
1799 static int snd_timer_user_pause(struct file *file)
1800 {
1801         int err;
1802         struct snd_timer_user *tu;
1803
1804         tu = file->private_data;
1805         if (!tu->timeri)
1806                 return -EBADFD;
1807         return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1808 }
1809
1810 enum {
1811         SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1812         SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1813         SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1814         SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1815 };
1816
1817 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1818                                  unsigned long arg)
1819 {
1820         struct snd_timer_user *tu;
1821         void __user *argp = (void __user *)arg;
1822         int __user *p = argp;
1823
1824         tu = file->private_data;
1825         switch (cmd) {
1826         case SNDRV_TIMER_IOCTL_PVERSION:
1827                 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1828         case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1829                 return snd_timer_user_next_device(argp);
1830         case SNDRV_TIMER_IOCTL_TREAD:
1831         {
1832                 int xarg;
1833
1834                 if (tu->timeri) /* too late */
1835                         return -EBUSY;
1836                 if (get_user(xarg, p))
1837                         return -EFAULT;
1838                 tu->tread = xarg ? 1 : 0;
1839                 return 0;
1840         }
1841         case SNDRV_TIMER_IOCTL_GINFO:
1842                 return snd_timer_user_ginfo(file, argp);
1843         case SNDRV_TIMER_IOCTL_GPARAMS:
1844                 return snd_timer_user_gparams(file, argp);
1845         case SNDRV_TIMER_IOCTL_GSTATUS:
1846                 return snd_timer_user_gstatus(file, argp);
1847         case SNDRV_TIMER_IOCTL_SELECT:
1848                 return snd_timer_user_tselect(file, argp);
1849         case SNDRV_TIMER_IOCTL_INFO:
1850                 return snd_timer_user_info(file, argp);
1851         case SNDRV_TIMER_IOCTL_PARAMS:
1852                 return snd_timer_user_params(file, argp);
1853         case SNDRV_TIMER_IOCTL_STATUS:
1854                 return snd_timer_user_status(file, argp);
1855         case SNDRV_TIMER_IOCTL_START:
1856         case SNDRV_TIMER_IOCTL_START_OLD:
1857                 return snd_timer_user_start(file);
1858         case SNDRV_TIMER_IOCTL_STOP:
1859         case SNDRV_TIMER_IOCTL_STOP_OLD:
1860                 return snd_timer_user_stop(file);
1861         case SNDRV_TIMER_IOCTL_CONTINUE:
1862         case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1863                 return snd_timer_user_continue(file);
1864         case SNDRV_TIMER_IOCTL_PAUSE:
1865         case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1866                 return snd_timer_user_pause(file);
1867         }
1868         return -ENOTTY;
1869 }
1870
1871 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1872                                  unsigned long arg)
1873 {
1874         struct snd_timer_user *tu = file->private_data;
1875         long ret;
1876
1877         mutex_lock(&tu->ioctl_lock);
1878         ret = __snd_timer_user_ioctl(file, cmd, arg);
1879         mutex_unlock(&tu->ioctl_lock);
1880         return ret;
1881 }
1882
1883 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1884 {
1885         struct snd_timer_user *tu;
1886
1887         tu = file->private_data;
1888         return fasync_helper(fd, file, on, &tu->fasync);
1889 }
1890
1891 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1892                                    size_t count, loff_t *offset)
1893 {
1894         struct snd_timer_user *tu;
1895         long result = 0, unit;
1896         int qhead;
1897         int err = 0;
1898
1899         tu = file->private_data;
1900         unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1901         spin_lock_irq(&tu->qlock);
1902         while ((long)count - result >= unit) {
1903                 while (!tu->qused) {
1904                         wait_queue_t wait;
1905
1906                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1907                                 err = -EAGAIN;
1908                                 goto _error;
1909                         }
1910
1911                         set_current_state(TASK_INTERRUPTIBLE);
1912                         init_waitqueue_entry(&wait, current);
1913                         add_wait_queue(&tu->qchange_sleep, &wait);
1914
1915                         spin_unlock_irq(&tu->qlock);
1916                         schedule();
1917                         spin_lock_irq(&tu->qlock);
1918
1919                         remove_wait_queue(&tu->qchange_sleep, &wait);
1920
1921                         if (signal_pending(current)) {
1922                                 err = -ERESTARTSYS;
1923                                 goto _error;
1924                         }
1925                 }
1926
1927                 qhead = tu->qhead++;
1928                 tu->qhead %= tu->queue_size;
1929                 tu->qused--;
1930                 spin_unlock_irq(&tu->qlock);
1931
1932                 if (tu->tread) {
1933                         if (copy_to_user(buffer, &tu->tqueue[qhead],
1934                                          sizeof(struct snd_timer_tread)))
1935                                 err = -EFAULT;
1936                 } else {
1937                         if (copy_to_user(buffer, &tu->queue[qhead],
1938                                          sizeof(struct snd_timer_read)))
1939                                 err = -EFAULT;
1940                 }
1941
1942                 spin_lock_irq(&tu->qlock);
1943                 if (err < 0)
1944                         goto _error;
1945                 result += unit;
1946                 buffer += unit;
1947         }
1948  _error:
1949         spin_unlock_irq(&tu->qlock);
1950         return result > 0 ? result : err;
1951 }
1952
1953 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1954 {
1955         unsigned int mask;
1956         struct snd_timer_user *tu;
1957
1958         tu = file->private_data;
1959
1960         poll_wait(file, &tu->qchange_sleep, wait);
1961
1962         mask = 0;
1963         if (tu->qused)
1964                 mask |= POLLIN | POLLRDNORM;
1965
1966         return mask;
1967 }
1968
1969 #ifdef CONFIG_COMPAT
1970 #include "timer_compat.c"
1971 #else
1972 #define snd_timer_user_ioctl_compat     NULL
1973 #endif
1974
1975 static const struct file_operations snd_timer_f_ops =
1976 {
1977         .owner =        THIS_MODULE,
1978         .read =         snd_timer_user_read,
1979         .open =         snd_timer_user_open,
1980         .release =      snd_timer_user_release,
1981         .llseek =       no_llseek,
1982         .poll =         snd_timer_user_poll,
1983         .unlocked_ioctl =       snd_timer_user_ioctl,
1984         .compat_ioctl = snd_timer_user_ioctl_compat,
1985         .fasync =       snd_timer_user_fasync,
1986 };
1987
1988 /*
1989  *  ENTRY functions
1990  */
1991
1992 static int __init alsa_timer_init(void)
1993 {
1994         int err;
1995
1996 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1997         snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1998                               "system timer");
1999 #endif
2000
2001         if ((err = snd_timer_register_system()) < 0)
2002                 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
2003                            err);
2004         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2005                                        &snd_timer_f_ops, NULL, "timer")) < 0)
2006                 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
2007                            err);
2008         snd_timer_proc_init();
2009         return 0;
2010 }
2011
2012 static void __exit alsa_timer_exit(void)
2013 {
2014         struct list_head *p, *n;
2015
2016         snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
2017         /* unregister the system timer */
2018         list_for_each_safe(p, n, &snd_timer_list) {
2019                 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
2020                 snd_timer_free(timer);
2021         }
2022         snd_timer_proc_done();
2023 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2024         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2025 #endif
2026 }
2027
2028 module_init(alsa_timer_init)
2029 module_exit(alsa_timer_exit)
2030
2031 EXPORT_SYMBOL(snd_timer_open);
2032 EXPORT_SYMBOL(snd_timer_close);
2033 EXPORT_SYMBOL(snd_timer_resolution);
2034 EXPORT_SYMBOL(snd_timer_start);
2035 EXPORT_SYMBOL(snd_timer_stop);
2036 EXPORT_SYMBOL(snd_timer_continue);
2037 EXPORT_SYMBOL(snd_timer_pause);
2038 EXPORT_SYMBOL(snd_timer_new);
2039 EXPORT_SYMBOL(snd_timer_notify);
2040 EXPORT_SYMBOL(snd_timer_global_new);
2041 EXPORT_SYMBOL(snd_timer_global_free);
2042 EXPORT_SYMBOL(snd_timer_global_register);
2043 EXPORT_SYMBOL(snd_timer_interrupt);