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