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