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