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