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