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