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