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