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