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