]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/core/timer.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-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
e1ac631c
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
b50b0dc1
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;
b50b0dc1 259 struct device *card_dev_to_put = NULL;
9b7d869e 260 int err;
6b172a85 261
2ec7970c 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);
2ec7970c
TI
269 err = -EINVAL;
270 goto unlock;
1da177e4 271 }
e1ac631c
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) {
2ec7970c
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);
e1ac631c 285 num_slaves++;
9b7d869e
TI
286 err = snd_timer_check_slave(timeri);
287 if (err < 0) {
b50b0dc1 288 snd_timer_close_locked(timeri, &card_dev_to_put);
9b7d869e
TI
289 timeri = NULL;
290 }
2ec7970c 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) {
2ec7970c
TI
305 err = -ENODEV;
306 goto unlock;
1da177e4 307 }
2fd43d11 308 if (!list_empty(&timer->open_list_head)) {
021de099
TI
309 struct snd_timer_instance *t =
310 list_entry(timer->open_list_head.next,
53d2f744 311 struct snd_timer_instance, open_list);
021de099 312 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
2ec7970c 313 err = -EBUSY;
2ec7970c 314 goto unlock;
2fd43d11
CL
315 }
316 }
9b7d869e 317 if (timer->num_instances >= timer->max_instances) {
2ec7970c
TI
318 err = -EBUSY;
319 goto unlock;
9b7d869e 320 }
2fd43d11
CL
321 timeri = snd_timer_instance_new(owner, timer);
322 if (!timeri) {
2ec7970c
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) {
2ec7970c 333 err = timer->hw.open(timer);
8ddc0563
VN
334 if (err) {
335 kfree(timeri->owner);
336 kfree(timeri);
2ec7970c 337 timeri = NULL;
8ddc0563
VN
338
339 if (timer->card)
b50b0dc1 340 card_dev_to_put = &timer->card->card_dev;
8ddc0563 341 module_put(timer->module);
2ec7970c 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) {
b50b0dc1 350 snd_timer_close_locked(timeri, &card_dev_to_put);
9b7d869e
TI
351 timeri = NULL;
352 }
2ec7970c
TI
353
354 unlock:
1a60d4c5 355 mutex_unlock(&register_mutex);
b50b0dc1
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 */
b50b0dc1
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);
e1ac631c
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)
b50b0dc1 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{
b50b0dc1 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);
b50b0dc1 442 err = snd_timer_close_locked(timeri, &card_dev_to_put);
9b7d869e 443 mutex_unlock(&register_mutex);
b50b0dc1
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 :
837603d5 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 :
837603d5 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;
38e9a80f 1093 struct snd_timer *snd_timer;
1da177e4
LT
1094 unsigned long last_expires;
1095 unsigned long last_jiffies;
1096 unsigned long correction;
1097};
1098
38e9a80f 1099static void snd_timer_s_function(struct timer_list *t)
1da177e4 1100{
38e9a80f
KC
1101 struct snd_timer_system_private *priv = from_timer(priv, t,
1102 tlist);
1103 struct snd_timer *timer = priv->snd_timer;
1da177e4
LT
1104 unsigned long jiff = jiffies;
1105 if (time_after(jiff, priv->last_expires))
6ed5eff0 1106 priv->correction += (long)jiff - (long)priv->last_expires;
1da177e4
LT
1107 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1108}
1109
53d2f744 1110static int snd_timer_s_start(struct snd_timer * timer)
1da177e4
LT
1111{
1112 struct snd_timer_system_private *priv;
1113 unsigned long njiff;
1114
1115 priv = (struct snd_timer_system_private *) timer->private_data;
1116 njiff = (priv->last_jiffies = jiffies);
1117 if (priv->correction > timer->sticks - 1) {
1118 priv->correction -= timer->sticks - 1;
1119 njiff++;
1120 } else {
1121 njiff += timer->sticks - priv->correction;
17f48ec3 1122 priv->correction = 0;
1da177e4 1123 }
4a07083e
TI
1124 priv->last_expires = njiff;
1125 mod_timer(&priv->tlist, njiff);
1da177e4
LT
1126 return 0;
1127}
1128
53d2f744 1129static int snd_timer_s_stop(struct snd_timer * timer)
1da177e4
LT
1130{
1131 struct snd_timer_system_private *priv;
1132 unsigned long jiff;
1133
1134 priv = (struct snd_timer_system_private *) timer->private_data;
1135 del_timer(&priv->tlist);
1136 jiff = jiffies;
1137 if (time_before(jiff, priv->last_expires))
1138 timer->sticks = priv->last_expires - jiff;
1139 else
1140 timer->sticks = 1;
de2696d8 1141 priv->correction = 0;
1da177e4
LT
1142 return 0;
1143}
1144
f146357f
TI
1145static int snd_timer_s_close(struct snd_timer *timer)
1146{
1147 struct snd_timer_system_private *priv;
1148
1149 priv = (struct snd_timer_system_private *)timer->private_data;
1150 del_timer_sync(&priv->tlist);
1151 return 0;
1152}
1153
53d2f744 1154static struct snd_timer_hardware snd_timer_system =
1da177e4
LT
1155{
1156 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1157 .resolution = 1000000000L / HZ,
1158 .ticks = 10000000L,
f146357f 1159 .close = snd_timer_s_close,
1da177e4
LT
1160 .start = snd_timer_s_start,
1161 .stop = snd_timer_s_stop
1162};
1163
53d2f744 1164static void snd_timer_free_system(struct snd_timer *timer)
1da177e4
LT
1165{
1166 kfree(timer->private_data);
1167}
1168
1169static int snd_timer_register_system(void)
1170{
53d2f744 1171 struct snd_timer *timer;
1da177e4
LT
1172 struct snd_timer_system_private *priv;
1173 int err;
1174
6b172a85
CL
1175 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1176 if (err < 0)
1da177e4
LT
1177 return err;
1178 strcpy(timer->name, "system timer");
1179 timer->hw = snd_timer_system;
ca2c0966 1180 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1da177e4
LT
1181 if (priv == NULL) {
1182 snd_timer_free(timer);
1183 return -ENOMEM;
1184 }
38e9a80f
KC
1185 priv->snd_timer = timer;
1186 timer_setup(&priv->tlist, snd_timer_s_function, 0);
1da177e4
LT
1187 timer->private_data = priv;
1188 timer->private_free = snd_timer_free_system;
1189 return snd_timer_global_register(timer);
1190}
1191
cd6a6503 1192#ifdef CONFIG_SND_PROC_FS
1da177e4
LT
1193/*
1194 * Info interface
1195 */
1196
53d2f744
TI
1197static void snd_timer_proc_read(struct snd_info_entry *entry,
1198 struct snd_info_buffer *buffer)
1da177e4 1199{
53d2f744
TI
1200 struct snd_timer *timer;
1201 struct snd_timer_instance *ti;
1da177e4 1202
1a60d4c5 1203 mutex_lock(&register_mutex);
9244b2c3 1204 list_for_each_entry(timer, &snd_timer_list, device_list) {
230323da
TI
1205 if (timer->card && timer->card->shutdown)
1206 continue;
1da177e4
LT
1207 switch (timer->tmr_class) {
1208 case SNDRV_TIMER_CLASS_GLOBAL:
1209 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1210 break;
1211 case SNDRV_TIMER_CLASS_CARD:
6b172a85
CL
1212 snd_iprintf(buffer, "C%i-%i: ",
1213 timer->card->number, timer->tmr_device);
1da177e4
LT
1214 break;
1215 case SNDRV_TIMER_CLASS_PCM:
6b172a85
CL
1216 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1217 timer->tmr_device, timer->tmr_subdevice);
1da177e4
LT
1218 break;
1219 default:
6b172a85
CL
1220 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1221 timer->card ? timer->card->number : -1,
1222 timer->tmr_device, timer->tmr_subdevice);
1da177e4
LT
1223 }
1224 snd_iprintf(buffer, "%s :", timer->name);
1225 if (timer->hw.resolution)
6b172a85
CL
1226 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1227 timer->hw.resolution / 1000,
1228 timer->hw.resolution % 1000,
1229 timer->hw.ticks);
1da177e4
LT
1230 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1231 snd_iprintf(buffer, " SLAVE");
1232 snd_iprintf(buffer, "\n");
9244b2c3 1233 list_for_each_entry(ti, &timer->open_list_head, open_list)
6b172a85
CL
1234 snd_iprintf(buffer, " Client %s : %s\n",
1235 ti->owner ? ti->owner : "unknown",
1236 ti->flags & (SNDRV_TIMER_IFLG_START |
1237 SNDRV_TIMER_IFLG_RUNNING)
1238 ? "running" : "stopped");
1da177e4 1239 }
1a60d4c5 1240 mutex_unlock(&register_mutex);
1da177e4
LT
1241}
1242
6581f4e7 1243static struct snd_info_entry *snd_timer_proc_entry;
e28563cc
TI
1244
1245static void __init snd_timer_proc_init(void)
1246{
1247 struct snd_info_entry *entry;
1248
1249 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1250 if (entry != NULL) {
e28563cc
TI
1251 entry->c.text.read = snd_timer_proc_read;
1252 if (snd_info_register(entry) < 0) {
1253 snd_info_free_entry(entry);
1254 entry = NULL;
1255 }
1256 }
1257 snd_timer_proc_entry = entry;
1258}
1259
1260static void __exit snd_timer_proc_done(void)
1261{
746d4a02 1262 snd_info_free_entry(snd_timer_proc_entry);
e28563cc 1263}
cd6a6503 1264#else /* !CONFIG_SND_PROC_FS */
e28563cc
TI
1265#define snd_timer_proc_init()
1266#define snd_timer_proc_done()
1267#endif
1268
1da177e4
LT
1269/*
1270 * USER SPACE interface
1271 */
1272
53d2f744 1273static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1da177e4
LT
1274 unsigned long resolution,
1275 unsigned long ticks)
1276{
53d2f744
TI
1277 struct snd_timer_user *tu = timeri->callback_data;
1278 struct snd_timer_read *r;
1da177e4 1279 int prev;
6b172a85 1280
1da177e4
LT
1281 spin_lock(&tu->qlock);
1282 if (tu->qused > 0) {
1283 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1284 r = &tu->queue[prev];
1285 if (r->resolution == resolution) {
1286 r->ticks += ticks;
1287 goto __wake;
1288 }
1289 }
1290 if (tu->qused >= tu->queue_size) {
1291 tu->overrun++;
1292 } else {
1293 r = &tu->queue[tu->qtail++];
1294 tu->qtail %= tu->queue_size;
1295 r->resolution = resolution;
1296 r->ticks = ticks;
1297 tu->qused++;
1298 }
1299 __wake:
1300 spin_unlock(&tu->qlock);
1301 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1302 wake_up(&tu->qchange_sleep);
1303}
1304
53d2f744
TI
1305static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1306 struct snd_timer_tread *tread)
1da177e4
LT
1307{
1308 if (tu->qused >= tu->queue_size) {
1309 tu->overrun++;
1310 } else {
1311 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1312 tu->qtail %= tu->queue_size;
1313 tu->qused++;
1314 }
1315}
1316
53d2f744
TI
1317static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1318 int event,
1da177e4
LT
1319 struct timespec *tstamp,
1320 unsigned long resolution)
1321{
53d2f744
TI
1322 struct snd_timer_user *tu = timeri->callback_data;
1323 struct snd_timer_tread r1;
bfe70783 1324 unsigned long flags;
1da177e4 1325
6b172a85
CL
1326 if (event >= SNDRV_TIMER_EVENT_START &&
1327 event <= SNDRV_TIMER_EVENT_PAUSE)
1da177e4
LT
1328 tu->tstamp = *tstamp;
1329 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1330 return;
9a47e9cf 1331 memset(&r1, 0, sizeof(r1));
1da177e4
LT
1332 r1.event = event;
1333 r1.tstamp = *tstamp;
1334 r1.val = resolution;
bfe70783 1335 spin_lock_irqsave(&tu->qlock, flags);
1da177e4 1336 snd_timer_user_append_to_tqueue(tu, &r1);
bfe70783 1337 spin_unlock_irqrestore(&tu->qlock, flags);
1da177e4
LT
1338 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1339 wake_up(&tu->qchange_sleep);
1340}
1341
40ed9444
TI
1342static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1343{
1344 struct snd_timer_user *tu = timeri->callback_data;
1345
1346 tu->disconnected = true;
1347 wake_up(&tu->qchange_sleep);
1348}
1349
53d2f744 1350static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1da177e4
LT
1351 unsigned long resolution,
1352 unsigned long ticks)
1353{
53d2f744
TI
1354 struct snd_timer_user *tu = timeri->callback_data;
1355 struct snd_timer_tread *r, r1;
1da177e4
LT
1356 struct timespec tstamp;
1357 int prev, append = 0;
1358
a8c006aa 1359 memset(&r1, 0, sizeof(r1));
07799e75 1360 memset(&tstamp, 0, sizeof(tstamp));
1da177e4 1361 spin_lock(&tu->qlock);
6b172a85
CL
1362 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1363 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1da177e4
LT
1364 spin_unlock(&tu->qlock);
1365 return;
1366 }
b751eef1
JK
1367 if (tu->last_resolution != resolution || ticks > 0) {
1368 if (timer_tstamp_monotonic)
26204e04 1369 ktime_get_ts(&tstamp);
b751eef1
JK
1370 else
1371 getnstimeofday(&tstamp);
1372 }
6b172a85
CL
1373 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1374 tu->last_resolution != resolution) {
1da177e4
LT
1375 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1376 r1.tstamp = tstamp;
1377 r1.val = resolution;
1378 snd_timer_user_append_to_tqueue(tu, &r1);
1379 tu->last_resolution = resolution;
1380 append++;
1381 }
1382 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1383 goto __wake;
1384 if (ticks == 0)
1385 goto __wake;
1386 if (tu->qused > 0) {
1387 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1388 r = &tu->tqueue[prev];
1389 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1390 r->tstamp = tstamp;
1391 r->val += ticks;
1392 append++;
1393 goto __wake;
1394 }
1395 }
1396 r1.event = SNDRV_TIMER_EVENT_TICK;
1397 r1.tstamp = tstamp;
1398 r1.val = ticks;
1399 snd_timer_user_append_to_tqueue(tu, &r1);
1400 append++;
1401 __wake:
1402 spin_unlock(&tu->qlock);
1403 if (append == 0)
1404 return;
1405 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1406 wake_up(&tu->qchange_sleep);
1407}
1408
890e2cb5
TI
1409static int realloc_user_queue(struct snd_timer_user *tu, int size)
1410{
1411 struct snd_timer_read *queue = NULL;
1412 struct snd_timer_tread *tqueue = NULL;
1413
1414 if (tu->tread) {
1415 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1416 if (!tqueue)
1417 return -ENOMEM;
1418 } else {
1419 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1420 if (!queue)
1421 return -ENOMEM;
1422 }
1423
1424 spin_lock_irq(&tu->qlock);
1425 kfree(tu->queue);
1426 kfree(tu->tqueue);
1427 tu->queue_size = size;
1428 tu->queue = queue;
1429 tu->tqueue = tqueue;
1430 tu->qhead = tu->qtail = tu->qused = 0;
1431 spin_unlock_irq(&tu->qlock);
1432
1433 return 0;
1434}
1435
1da177e4
LT
1436static int snd_timer_user_open(struct inode *inode, struct file *file)
1437{
53d2f744 1438 struct snd_timer_user *tu;
02f4865f
TI
1439 int err;
1440
1441 err = nonseekable_open(inode, file);
1442 if (err < 0)
1443 return err;
6b172a85 1444
ca2c0966 1445 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1da177e4
LT
1446 if (tu == NULL)
1447 return -ENOMEM;
1448 spin_lock_init(&tu->qlock);
1449 init_waitqueue_head(&tu->qchange_sleep);
af368027 1450 mutex_init(&tu->ioctl_lock);
1da177e4 1451 tu->ticks = 1;
890e2cb5 1452 if (realloc_user_queue(tu, 128) < 0) {
1da177e4
LT
1453 kfree(tu);
1454 return -ENOMEM;
1455 }
1456 file->private_data = tu;
1457 return 0;
1458}
1459
1460static int snd_timer_user_release(struct inode *inode, struct file *file)
1461{
53d2f744 1462 struct snd_timer_user *tu;
1da177e4
LT
1463
1464 if (file->private_data) {
1465 tu = file->private_data;
1466 file->private_data = NULL;
af368027 1467 mutex_lock(&tu->ioctl_lock);
1da177e4
LT
1468 if (tu->timeri)
1469 snd_timer_close(tu->timeri);
af368027 1470 mutex_unlock(&tu->ioctl_lock);
1da177e4
LT
1471 kfree(tu->queue);
1472 kfree(tu->tqueue);
1473 kfree(tu);
1474 }
1475 return 0;
1476}
1477
53d2f744 1478static void snd_timer_user_zero_id(struct snd_timer_id *id)
1da177e4
LT
1479{
1480 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1481 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1482 id->card = -1;
1483 id->device = -1;
1484 id->subdevice = -1;
1485}
1486
53d2f744 1487static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1da177e4
LT
1488{
1489 id->dev_class = timer->tmr_class;
1490 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1491 id->card = timer->card ? timer->card->number : -1;
1492 id->device = timer->tmr_device;
1493 id->subdevice = timer->tmr_subdevice;
1494}
1495
53d2f744 1496static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1da177e4 1497{
53d2f744
TI
1498 struct snd_timer_id id;
1499 struct snd_timer *timer;
1da177e4 1500 struct list_head *p;
6b172a85 1501
1da177e4
LT
1502 if (copy_from_user(&id, _tid, sizeof(id)))
1503 return -EFAULT;
1a60d4c5 1504 mutex_lock(&register_mutex);
1da177e4
LT
1505 if (id.dev_class < 0) { /* first item */
1506 if (list_empty(&snd_timer_list))
1507 snd_timer_user_zero_id(&id);
1508 else {
9dfba380 1509 timer = list_entry(snd_timer_list.next,
53d2f744 1510 struct snd_timer, device_list);
1da177e4
LT
1511 snd_timer_user_copy_id(&id, timer);
1512 }
1513 } else {
1514 switch (id.dev_class) {
1515 case SNDRV_TIMER_CLASS_GLOBAL:
1516 id.device = id.device < 0 ? 0 : id.device + 1;
1517 list_for_each(p, &snd_timer_list) {
53d2f744 1518 timer = list_entry(p, struct snd_timer, device_list);
1da177e4
LT
1519 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1520 snd_timer_user_copy_id(&id, timer);
1521 break;
1522 }
1523 if (timer->tmr_device >= id.device) {
1524 snd_timer_user_copy_id(&id, timer);
1525 break;
1526 }
1527 }
1528 if (p == &snd_timer_list)
1529 snd_timer_user_zero_id(&id);
1530 break;
1531 case SNDRV_TIMER_CLASS_CARD:
1532 case SNDRV_TIMER_CLASS_PCM:
1533 if (id.card < 0) {
1534 id.card = 0;
1535 } else {
e8ed6820
DC
1536 if (id.device < 0) {
1537 id.device = 0;
1da177e4 1538 } else {
e8ed6820
DC
1539 if (id.subdevice < 0)
1540 id.subdevice = 0;
f8b37fd0 1541 else if (id.subdevice < INT_MAX)
e8ed6820 1542 id.subdevice++;
1da177e4
LT
1543 }
1544 }
1545 list_for_each(p, &snd_timer_list) {
53d2f744 1546 timer = list_entry(p, struct snd_timer, device_list);
1da177e4
LT
1547 if (timer->tmr_class > id.dev_class) {
1548 snd_timer_user_copy_id(&id, timer);
1549 break;
1550 }
1551 if (timer->tmr_class < id.dev_class)
1552 continue;
1553 if (timer->card->number > id.card) {
1554 snd_timer_user_copy_id(&id, timer);
1555 break;
1556 }
1557 if (timer->card->number < id.card)
1558 continue;
1559 if (timer->tmr_device > id.device) {
1560 snd_timer_user_copy_id(&id, timer);
1561 break;
1562 }
1563 if (timer->tmr_device < id.device)
1564 continue;
1565 if (timer->tmr_subdevice > id.subdevice) {
1566 snd_timer_user_copy_id(&id, timer);
1567 break;
1568 }
1569 if (timer->tmr_subdevice < id.subdevice)
1570 continue;
1571 snd_timer_user_copy_id(&id, timer);
1572 break;
1573 }
1574 if (p == &snd_timer_list)
1575 snd_timer_user_zero_id(&id);
1576 break;
1577 default:
1578 snd_timer_user_zero_id(&id);
1579 }
1580 }
1a60d4c5 1581 mutex_unlock(&register_mutex);
1da177e4
LT
1582 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1583 return -EFAULT;
1584 return 0;
6b172a85 1585}
1da177e4 1586
6b172a85 1587static int snd_timer_user_ginfo(struct file *file,
53d2f744 1588 struct snd_timer_ginfo __user *_ginfo)
1da177e4 1589{
53d2f744
TI
1590 struct snd_timer_ginfo *ginfo;
1591 struct snd_timer_id tid;
1592 struct snd_timer *t;
1da177e4
LT
1593 struct list_head *p;
1594 int err = 0;
1595
ef44a1ec
LZ
1596 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1597 if (IS_ERR(ginfo))
1598 return PTR_ERR(ginfo);
1599
1da177e4
LT
1600 tid = ginfo->tid;
1601 memset(ginfo, 0, sizeof(*ginfo));
1602 ginfo->tid = tid;
1a60d4c5 1603 mutex_lock(&register_mutex);
1da177e4
LT
1604 t = snd_timer_find(&tid);
1605 if (t != NULL) {
1606 ginfo->card = t->card ? t->card->number : -1;
1607 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1608 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1609 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1610 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1611 ginfo->resolution = t->hw.resolution;
1612 if (t->hw.resolution_min > 0) {
1613 ginfo->resolution_min = t->hw.resolution_min;
1614 ginfo->resolution_max = t->hw.resolution_max;
1615 }
1616 list_for_each(p, &t->open_list_head) {
1617 ginfo->clients++;
1618 }
1619 } else {
1620 err = -ENODEV;
1621 }
1a60d4c5 1622 mutex_unlock(&register_mutex);
1da177e4
LT
1623 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1624 err = -EFAULT;
1625 kfree(ginfo);
1626 return err;
1627}
1628
91d2178e 1629static int timer_set_gparams(struct snd_timer_gparams *gparams)
1da177e4 1630{
53d2f744 1631 struct snd_timer *t;
1da177e4
LT
1632 int err;
1633
1a60d4c5 1634 mutex_lock(&register_mutex);
91d2178e 1635 t = snd_timer_find(&gparams->tid);
6b172a85 1636 if (!t) {
1da177e4 1637 err = -ENODEV;
6b172a85
CL
1638 goto _error;
1639 }
1640 if (!list_empty(&t->open_list_head)) {
1641 err = -EBUSY;
1642 goto _error;
1643 }
1644 if (!t->hw.set_period) {
1645 err = -ENOSYS;
1646 goto _error;
1da177e4 1647 }
91d2178e 1648 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
6b172a85 1649_error:
1a60d4c5 1650 mutex_unlock(&register_mutex);
1da177e4
LT
1651 return err;
1652}
1653
91d2178e
TS
1654static int snd_timer_user_gparams(struct file *file,
1655 struct snd_timer_gparams __user *_gparams)
1656{
1657 struct snd_timer_gparams gparams;
1658
1659 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1660 return -EFAULT;
1661 return timer_set_gparams(&gparams);
1662}
1663
6b172a85 1664static int snd_timer_user_gstatus(struct file *file,
53d2f744 1665 struct snd_timer_gstatus __user *_gstatus)
1da177e4 1666{
53d2f744
TI
1667 struct snd_timer_gstatus gstatus;
1668 struct snd_timer_id tid;
1669 struct snd_timer *t;
1da177e4
LT
1670 int err = 0;
1671
1672 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1673 return -EFAULT;
1674 tid = gstatus.tid;
1675 memset(&gstatus, 0, sizeof(gstatus));
1676 gstatus.tid = tid;
1a60d4c5 1677 mutex_lock(&register_mutex);
1da177e4
LT
1678 t = snd_timer_find(&tid);
1679 if (t != NULL) {
1680 if (t->hw.c_resolution)
1681 gstatus.resolution = t->hw.c_resolution(t);
1682 else
1683 gstatus.resolution = t->hw.resolution;
1684 if (t->hw.precise_resolution) {
6b172a85
CL
1685 t->hw.precise_resolution(t, &gstatus.resolution_num,
1686 &gstatus.resolution_den);
1da177e4
LT
1687 } else {
1688 gstatus.resolution_num = gstatus.resolution;
1689 gstatus.resolution_den = 1000000000uL;
1690 }
1691 } else {
1692 err = -ENODEV;
1693 }
1a60d4c5 1694 mutex_unlock(&register_mutex);
1da177e4
LT
1695 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1696 err = -EFAULT;
1697 return err;
1698}
1699
6b172a85 1700static int snd_timer_user_tselect(struct file *file,
53d2f744 1701 struct snd_timer_select __user *_tselect)
1da177e4 1702{
53d2f744
TI
1703 struct snd_timer_user *tu;
1704 struct snd_timer_select tselect;
1da177e4 1705 char str[32];
c1935b4d 1706 int err = 0;
6b172a85 1707
1da177e4 1708 tu = file->private_data;
c1935b4d 1709 if (tu->timeri) {
1da177e4 1710 snd_timer_close(tu->timeri);
c1935b4d
JK
1711 tu->timeri = NULL;
1712 }
1713 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1714 err = -EFAULT;
1715 goto __err;
1716 }
1da177e4
LT
1717 sprintf(str, "application %i", current->pid);
1718 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1719 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
6b172a85
CL
1720 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1721 if (err < 0)
c1935b4d 1722 goto __err;
1da177e4 1723
890e2cb5
TI
1724 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1725 tu->timeri->callback = tu->tread
6b172a85 1726 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
890e2cb5
TI
1727 tu->timeri->ccallback = snd_timer_user_ccallback;
1728 tu->timeri->callback_data = (void *)tu;
1729 tu->timeri->disconnect = snd_timer_user_disconnect;
c1935b4d
JK
1730
1731 __err:
c1935b4d 1732 return err;
1da177e4
LT
1733}
1734
6b172a85 1735static int snd_timer_user_info(struct file *file,
53d2f744 1736 struct snd_timer_info __user *_info)
1da177e4 1737{
53d2f744
TI
1738 struct snd_timer_user *tu;
1739 struct snd_timer_info *info;
1740 struct snd_timer *t;
1da177e4
LT
1741 int err = 0;
1742
1743 tu = file->private_data;
7c64ec34
CL
1744 if (!tu->timeri)
1745 return -EBADFD;
1da177e4 1746 t = tu->timeri->timer;
7c64ec34
CL
1747 if (!t)
1748 return -EBADFD;
1da177e4 1749
ca2c0966 1750 info = kzalloc(sizeof(*info), GFP_KERNEL);
1da177e4
LT
1751 if (! info)
1752 return -ENOMEM;
1753 info->card = t->card ? t->card->number : -1;
1754 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1755 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1756 strlcpy(info->id, t->id, sizeof(info->id));
1757 strlcpy(info->name, t->name, sizeof(info->name));
1758 info->resolution = t->hw.resolution;
1759 if (copy_to_user(_info, info, sizeof(*_info)))
1760 err = -EFAULT;
1761 kfree(info);
1762 return err;
1763}
1764
6b172a85 1765static int snd_timer_user_params(struct file *file,
53d2f744 1766 struct snd_timer_params __user *_params)
1da177e4 1767{
53d2f744
TI
1768 struct snd_timer_user *tu;
1769 struct snd_timer_params params;
1770 struct snd_timer *t;
1da177e4 1771 int err;
6b172a85 1772
1da177e4 1773 tu = file->private_data;
7c64ec34
CL
1774 if (!tu->timeri)
1775 return -EBADFD;
1da177e4 1776 t = tu->timeri->timer;
7c64ec34
CL
1777 if (!t)
1778 return -EBADFD;
1da177e4
LT
1779 if (copy_from_user(&params, _params, sizeof(params)))
1780 return -EFAULT;
71321eb3
TI
1781 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1782 u64 resolution;
1783
1784 if (params.ticks < 1) {
1785 err = -EINVAL;
1786 goto _end;
1787 }
1788
1789 /* Don't allow resolution less than 1ms */
1790 resolution = snd_timer_resolution(tu->timeri);
1791 resolution *= params.ticks;
1792 if (resolution < 1000000) {
1793 err = -EINVAL;
1794 goto _end;
1795 }
1da177e4 1796 }
6b172a85
CL
1797 if (params.queue_size > 0 &&
1798 (params.queue_size < 32 || params.queue_size > 1024)) {
1da177e4
LT
1799 err = -EINVAL;
1800 goto _end;
1801 }
1802 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1803 (1<<SNDRV_TIMER_EVENT_TICK)|
1804 (1<<SNDRV_TIMER_EVENT_START)|
1805 (1<<SNDRV_TIMER_EVENT_STOP)|
1806 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1807 (1<<SNDRV_TIMER_EVENT_PAUSE)|
a501dfa3
JK
1808 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1809 (1<<SNDRV_TIMER_EVENT_RESUME)|
1da177e4
LT
1810 (1<<SNDRV_TIMER_EVENT_MSTART)|
1811 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1812 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
65d11d95
JK
1813 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1814 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
a501dfa3 1815 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1da177e4
LT
1816 err = -EINVAL;
1817 goto _end;
1818 }
1819 snd_timer_stop(tu->timeri);
1820 spin_lock_irq(&t->lock);
1821 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1822 SNDRV_TIMER_IFLG_EXCLUSIVE|
1823 SNDRV_TIMER_IFLG_EARLY_EVENT);
1824 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1825 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1826 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1827 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1828 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1829 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1830 spin_unlock_irq(&t->lock);
6b172a85
CL
1831 if (params.queue_size > 0 &&
1832 (unsigned int)tu->queue_size != params.queue_size) {
890e2cb5
TI
1833 err = realloc_user_queue(tu, params.queue_size);
1834 if (err < 0)
1835 goto _end;
1da177e4 1836 }
d7f910bf 1837 spin_lock_irq(&tu->qlock);
1da177e4
LT
1838 tu->qhead = tu->qtail = tu->qused = 0;
1839 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1840 if (tu->tread) {
53d2f744 1841 struct snd_timer_tread tread;
cec8f96e 1842 memset(&tread, 0, sizeof(tread));
1da177e4
LT
1843 tread.event = SNDRV_TIMER_EVENT_EARLY;
1844 tread.tstamp.tv_sec = 0;
1845 tread.tstamp.tv_nsec = 0;
1846 tread.val = 0;
1847 snd_timer_user_append_to_tqueue(tu, &tread);
1848 } else {
53d2f744 1849 struct snd_timer_read *r = &tu->queue[0];
1da177e4
LT
1850 r->resolution = 0;
1851 r->ticks = 0;
1852 tu->qused++;
1853 tu->qtail++;
1854 }
1da177e4
LT
1855 }
1856 tu->filter = params.filter;
1857 tu->ticks = params.ticks;
d7f910bf 1858 spin_unlock_irq(&tu->qlock);
1da177e4
LT
1859 err = 0;
1860 _end:
1861 if (copy_to_user(_params, &params, sizeof(params)))
1862 return -EFAULT;
1863 return err;
1864}
1865
6b172a85 1866static int snd_timer_user_status(struct file *file,
53d2f744 1867 struct snd_timer_status __user *_status)
1da177e4 1868{
53d2f744
TI
1869 struct snd_timer_user *tu;
1870 struct snd_timer_status status;
6b172a85 1871
1da177e4 1872 tu = file->private_data;
7c64ec34
CL
1873 if (!tu->timeri)
1874 return -EBADFD;
1da177e4
LT
1875 memset(&status, 0, sizeof(status));
1876 status.tstamp = tu->tstamp;
1877 status.resolution = snd_timer_resolution(tu->timeri);
1878 status.lost = tu->timeri->lost;
1879 status.overrun = tu->overrun;
1880 spin_lock_irq(&tu->qlock);
1881 status.queue = tu->qused;
1882 spin_unlock_irq(&tu->qlock);
1883 if (copy_to_user(_status, &status, sizeof(status)))
1884 return -EFAULT;
1885 return 0;
1886}
1887
1888static int snd_timer_user_start(struct file *file)
1889{
1890 int err;
53d2f744 1891 struct snd_timer_user *tu;
6b172a85 1892
1da177e4 1893 tu = file->private_data;
7c64ec34
CL
1894 if (!tu->timeri)
1895 return -EBADFD;
1da177e4
LT
1896 snd_timer_stop(tu->timeri);
1897 tu->timeri->lost = 0;
1898 tu->last_resolution = 0;
1899 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1900}
1901
1902static int snd_timer_user_stop(struct file *file)
1903{
1904 int err;
53d2f744 1905 struct snd_timer_user *tu;
6b172a85 1906
1da177e4 1907 tu = file->private_data;
7c64ec34
CL
1908 if (!tu->timeri)
1909 return -EBADFD;
1da177e4
LT
1910 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1911}
1912
1913static int snd_timer_user_continue(struct file *file)
1914{
1915 int err;
53d2f744 1916 struct snd_timer_user *tu;
6b172a85 1917
1da177e4 1918 tu = file->private_data;
7c64ec34
CL
1919 if (!tu->timeri)
1920 return -EBADFD;
9f8a7658
TI
1921 /* start timer instead of continue if it's not used before */
1922 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1923 return snd_timer_user_start(file);
1da177e4
LT
1924 tu->timeri->lost = 0;
1925 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1926}
1927
15790a6b
TI
1928static int snd_timer_user_pause(struct file *file)
1929{
1930 int err;
53d2f744 1931 struct snd_timer_user *tu;
6b172a85 1932
15790a6b 1933 tu = file->private_data;
7c64ec34
CL
1934 if (!tu->timeri)
1935 return -EBADFD;
d138b445 1936 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
15790a6b
TI
1937}
1938
8c50b37c
TI
1939enum {
1940 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1941 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1942 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1943 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1944};
1945
af368027 1946static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
6b172a85 1947 unsigned long arg)
1da177e4 1948{
53d2f744 1949 struct snd_timer_user *tu;
1da177e4
LT
1950 void __user *argp = (void __user *)arg;
1951 int __user *p = argp;
6b172a85 1952
1da177e4
LT
1953 tu = file->private_data;
1954 switch (cmd) {
1955 case SNDRV_TIMER_IOCTL_PVERSION:
1956 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1957 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1958 return snd_timer_user_next_device(argp);
1959 case SNDRV_TIMER_IOCTL_TREAD:
1960 {
890e2cb5 1961 int xarg, old_tread;
6b172a85 1962
af368027 1963 if (tu->timeri) /* too late */
1da177e4 1964 return -EBUSY;
af368027 1965 if (get_user(xarg, p))
1da177e4 1966 return -EFAULT;
890e2cb5 1967 old_tread = tu->tread;
1da177e4 1968 tu->tread = xarg ? 1 : 0;
890e2cb5
TI
1969 if (tu->tread != old_tread &&
1970 realloc_user_queue(tu, tu->queue_size) < 0) {
1971 tu->tread = old_tread;
1972 return -ENOMEM;
1973 }
1da177e4
LT
1974 return 0;
1975 }
1976 case SNDRV_TIMER_IOCTL_GINFO:
1977 return snd_timer_user_ginfo(file, argp);
1978 case SNDRV_TIMER_IOCTL_GPARAMS:
1979 return snd_timer_user_gparams(file, argp);
1980 case SNDRV_TIMER_IOCTL_GSTATUS:
1981 return snd_timer_user_gstatus(file, argp);
1982 case SNDRV_TIMER_IOCTL_SELECT:
1983 return snd_timer_user_tselect(file, argp);
1984 case SNDRV_TIMER_IOCTL_INFO:
1985 return snd_timer_user_info(file, argp);
1986 case SNDRV_TIMER_IOCTL_PARAMS:
1987 return snd_timer_user_params(file, argp);
1988 case SNDRV_TIMER_IOCTL_STATUS:
1989 return snd_timer_user_status(file, argp);
1990 case SNDRV_TIMER_IOCTL_START:
8c50b37c 1991 case SNDRV_TIMER_IOCTL_START_OLD:
1da177e4
LT
1992 return snd_timer_user_start(file);
1993 case SNDRV_TIMER_IOCTL_STOP:
8c50b37c 1994 case SNDRV_TIMER_IOCTL_STOP_OLD:
1da177e4
LT
1995 return snd_timer_user_stop(file);
1996 case SNDRV_TIMER_IOCTL_CONTINUE:
8c50b37c 1997 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1da177e4 1998 return snd_timer_user_continue(file);
15790a6b 1999 case SNDRV_TIMER_IOCTL_PAUSE:
8c50b37c 2000 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
15790a6b 2001 return snd_timer_user_pause(file);
1da177e4
LT
2002 }
2003 return -ENOTTY;
2004}
2005
af368027
TI
2006static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2007 unsigned long arg)
2008{
2009 struct snd_timer_user *tu = file->private_data;
2010 long ret;
2011
2012 mutex_lock(&tu->ioctl_lock);
2013 ret = __snd_timer_user_ioctl(file, cmd, arg);
2014 mutex_unlock(&tu->ioctl_lock);
2015 return ret;
2016}
2017
1da177e4
LT
2018static int snd_timer_user_fasync(int fd, struct file * file, int on)
2019{
53d2f744 2020 struct snd_timer_user *tu;
6b172a85 2021
1da177e4 2022 tu = file->private_data;
60aa4924 2023 return fasync_helper(fd, file, on, &tu->fasync);
1da177e4
LT
2024}
2025
6b172a85
CL
2026static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2027 size_t count, loff_t *offset)
1da177e4 2028{
53d2f744 2029 struct snd_timer_user *tu;
1da177e4 2030 long result = 0, unit;
4dff5c7b 2031 int qhead;
1da177e4 2032 int err = 0;
6b172a85 2033
1da177e4 2034 tu = file->private_data;
53d2f744 2035 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
d11662f4 2036 mutex_lock(&tu->ioctl_lock);
1da177e4
LT
2037 spin_lock_irq(&tu->qlock);
2038 while ((long)count - result >= unit) {
2039 while (!tu->qused) {
ac6424b9 2040 wait_queue_entry_t wait;
1da177e4
LT
2041
2042 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2043 err = -EAGAIN;
4dff5c7b 2044 goto _error;
1da177e4
LT
2045 }
2046
2047 set_current_state(TASK_INTERRUPTIBLE);
2048 init_waitqueue_entry(&wait, current);
2049 add_wait_queue(&tu->qchange_sleep, &wait);
2050
2051 spin_unlock_irq(&tu->qlock);
d11662f4 2052 mutex_unlock(&tu->ioctl_lock);
1da177e4 2053 schedule();
d11662f4 2054 mutex_lock(&tu->ioctl_lock);
1da177e4
LT
2055 spin_lock_irq(&tu->qlock);
2056
2057 remove_wait_queue(&tu->qchange_sleep, &wait);
2058
230323da
TI
2059 if (tu->disconnected) {
2060 err = -ENODEV;
4dff5c7b 2061 goto _error;
230323da 2062 }
1da177e4
LT
2063 if (signal_pending(current)) {
2064 err = -ERESTARTSYS;
4dff5c7b 2065 goto _error;
1da177e4
LT
2066 }
2067 }
2068
4dff5c7b
TI
2069 qhead = tu->qhead++;
2070 tu->qhead %= tu->queue_size;
3fa6993f 2071 tu->qused--;
1da177e4 2072 spin_unlock_irq(&tu->qlock);
1da177e4
LT
2073
2074 if (tu->tread) {
4dff5c7b
TI
2075 if (copy_to_user(buffer, &tu->tqueue[qhead],
2076 sizeof(struct snd_timer_tread)))
1da177e4 2077 err = -EFAULT;
1da177e4 2078 } else {
4dff5c7b
TI
2079 if (copy_to_user(buffer, &tu->queue[qhead],
2080 sizeof(struct snd_timer_read)))
1da177e4 2081 err = -EFAULT;
1da177e4
LT
2082 }
2083
1da177e4 2084 spin_lock_irq(&tu->qlock);
4dff5c7b
TI
2085 if (err < 0)
2086 goto _error;
2087 result += unit;
2088 buffer += unit;
1da177e4 2089 }
1da177e4 2090 _error:
4dff5c7b 2091 spin_unlock_irq(&tu->qlock);
d11662f4 2092 mutex_unlock(&tu->ioctl_lock);
1da177e4
LT
2093 return result > 0 ? result : err;
2094}
2095
2096static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2097{
2098 unsigned int mask;
53d2f744 2099 struct snd_timer_user *tu;
1da177e4
LT
2100
2101 tu = file->private_data;
2102
2103 poll_wait(file, &tu->qchange_sleep, wait);
6b172a85 2104
1da177e4 2105 mask = 0;
d7f910bf 2106 spin_lock_irq(&tu->qlock);
1da177e4
LT
2107 if (tu->qused)
2108 mask |= POLLIN | POLLRDNORM;
230323da
TI
2109 if (tu->disconnected)
2110 mask |= POLLERR;
d7f910bf 2111 spin_unlock_irq(&tu->qlock);
1da177e4
LT
2112
2113 return mask;
2114}
2115
2116#ifdef CONFIG_COMPAT
2117#include "timer_compat.c"
2118#else
2119#define snd_timer_user_ioctl_compat NULL
2120#endif
2121
9c2e08c5 2122static const struct file_operations snd_timer_f_ops =
1da177e4
LT
2123{
2124 .owner = THIS_MODULE,
2125 .read = snd_timer_user_read,
2126 .open = snd_timer_user_open,
2127 .release = snd_timer_user_release,
02f4865f 2128 .llseek = no_llseek,
1da177e4
LT
2129 .poll = snd_timer_user_poll,
2130 .unlocked_ioctl = snd_timer_user_ioctl,
2131 .compat_ioctl = snd_timer_user_ioctl_compat,
2132 .fasync = snd_timer_user_fasync,
2133};
2134
7c35860d
TI
2135/* unregister the system timer */
2136static void snd_timer_free_all(void)
2137{
2138 struct snd_timer *timer, *n;
2139
2140 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2141 snd_timer_free(timer);
2142}
2143
89da061f
TI
2144static struct device timer_dev;
2145
1da177e4
LT
2146/*
2147 * ENTRY functions
2148 */
2149
1da177e4
LT
2150static int __init alsa_timer_init(void)
2151{
2152 int err;
1da177e4 2153
89da061f
TI
2154 snd_device_initialize(&timer_dev, NULL);
2155 dev_set_name(&timer_dev, "timer");
2156
1da177e4 2157#ifdef SNDRV_OSS_INFO_DEV_TIMERS
6b172a85
CL
2158 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2159 "system timer");
1da177e4 2160#endif
e28563cc 2161
7c35860d
TI
2162 err = snd_timer_register_system();
2163 if (err < 0) {
cf74dcf3 2164 pr_err("ALSA: unable to register system timer (%i)\n", err);
1ae0e4ce 2165 goto put_timer;
7c35860d
TI
2166 }
2167
40a4b263
TI
2168 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2169 &snd_timer_f_ops, NULL, &timer_dev);
7c35860d 2170 if (err < 0) {
cf74dcf3 2171 pr_err("ALSA: unable to register timer device (%i)\n", err);
7c35860d 2172 snd_timer_free_all();
1ae0e4ce 2173 goto put_timer;
7c35860d
TI
2174 }
2175
e28563cc 2176 snd_timer_proc_init();
1da177e4 2177 return 0;
1ae0e4ce
ME
2178
2179put_timer:
2180 put_device(&timer_dev);
2181 return err;
1da177e4
LT
2182}
2183
2184static void __exit alsa_timer_exit(void)
2185{
40a4b263 2186 snd_unregister_device(&timer_dev);
7c35860d 2187 snd_timer_free_all();
89da061f 2188 put_device(&timer_dev);
e28563cc 2189 snd_timer_proc_done();
1da177e4
LT
2190#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2191 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2192#endif
2193}
2194
2195module_init(alsa_timer_init)
2196module_exit(alsa_timer_exit)