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