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