~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/timer.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp