timer.c 54.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Timers abstract layer
3
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/delay.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/time.h>
26
#include <linux/mutex.h>
27
#include <linux/device.h>
28
#include <linux/module.h>
29
#include <linux/string.h>
30
#include <linux/sched/signal.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38
#include <sound/core.h>
#include <sound/timer.h>
#include <sound/control.h>
#include <sound/info.h>
#include <sound/minors.h>
#include <sound/initval.h>
#include <linux/kmod.h>

39 40 41
/* internal flags */
#define SNDRV_TIMER_IFLG_PAUSED		0x00010000

T
Takashi Iwai 已提交
42
#if IS_ENABLED(CONFIG_SND_HRTIMER)
43
#define DEFAULT_TIMER_LIMIT 4
L
Linus Torvalds 已提交
44 45 46 47 48
#else
#define DEFAULT_TIMER_LIMIT 1
#endif

static int timer_limit = DEFAULT_TIMER_LIMIT;
49
static int timer_tstamp_monotonic = 1;
50
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
L
Linus Torvalds 已提交
51 52 53 54
MODULE_DESCRIPTION("ALSA timer interface");
MODULE_LICENSE("GPL");
module_param(timer_limit, int, 0444);
MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
55 56
module_param(timer_tstamp_monotonic, int, 0444);
MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
L
Linus Torvalds 已提交
57

58 59 60
MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
MODULE_ALIAS("devname:snd/timer");

61 62
struct snd_timer_user {
	struct snd_timer_instance *timeri;
C
Clemens Ladisch 已提交
63
	int tread;		/* enhanced read with timestamps and events */
L
Linus Torvalds 已提交
64 65 66 67 68 69
	unsigned long ticks;
	unsigned long overrun;
	int qhead;
	int qtail;
	int qused;
	int queue_size;
70
	bool disconnected;
71 72
	struct snd_timer_read *queue;
	struct snd_timer_tread *tqueue;
L
Linus Torvalds 已提交
73 74 75 76 77 78
	spinlock_t qlock;
	unsigned long last_resolution;
	unsigned int filter;
	struct timespec tstamp;		/* trigger tstamp */
	wait_queue_head_t qchange_sleep;
	struct fasync_struct *fasync;
79
	struct mutex ioctl_lock;
80
};
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90

/* list of timers */
static LIST_HEAD(snd_timer_list);

/* list of slave instances */
static LIST_HEAD(snd_timer_slave_list);

/* lock for slave active lists */
static DEFINE_SPINLOCK(slave_active_lock);

91
static DEFINE_MUTEX(register_mutex);
L
Linus Torvalds 已提交
92

93 94 95
static int snd_timer_free(struct snd_timer *timer);
static int snd_timer_dev_free(struct snd_device *device);
static int snd_timer_dev_register(struct snd_device *device);
96
static int snd_timer_dev_disconnect(struct snd_device *device);
L
Linus Torvalds 已提交
97

98
static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
L
Linus Torvalds 已提交
99 100 101 102 103

/*
 * create a timer instance with the given owner string.
 * when timer is not NULL, increments the module counter
 */
104 105
static struct snd_timer_instance *snd_timer_instance_new(char *owner,
							 struct snd_timer *timer)
L
Linus Torvalds 已提交
106
{
107
	struct snd_timer_instance *timeri;
108
	timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
L
Linus Torvalds 已提交
109 110
	if (timeri == NULL)
		return NULL;
111
	timeri->owner = kstrdup(owner, GFP_KERNEL);
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122
	if (! timeri->owner) {
		kfree(timeri);
		return NULL;
	}
	INIT_LIST_HEAD(&timeri->open_list);
	INIT_LIST_HEAD(&timeri->active_list);
	INIT_LIST_HEAD(&timeri->ack_list);
	INIT_LIST_HEAD(&timeri->slave_list_head);
	INIT_LIST_HEAD(&timeri->slave_active_head);

	timeri->timer = timer;
123
	if (timer && !try_module_get(timer->module)) {
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131 132 133 134
		kfree(timeri->owner);
		kfree(timeri);
		return NULL;
	}

	return timeri;
}

/*
 * find a timer instance from the given timer id
 */
135
static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
L
Linus Torvalds 已提交
136
{
137
	struct snd_timer *timer = NULL;
L
Linus Torvalds 已提交
138

139
	list_for_each_entry(timer, &snd_timer_list, device_list) {
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		if (timer->tmr_class != tid->dev_class)
			continue;
		if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
		     timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
		    (timer->card == NULL ||
		     timer->card->number != tid->card))
			continue;
		if (timer->tmr_device != tid->device)
			continue;
		if (timer->tmr_subdevice != tid->subdevice)
			continue;
		return timer;
	}
	return NULL;
}

156
#ifdef CONFIG_MODULES
L
Linus Torvalds 已提交
157

158
static void snd_timer_request(struct snd_timer_id *tid)
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
{
	switch (tid->dev_class) {
	case SNDRV_TIMER_CLASS_GLOBAL:
		if (tid->device < timer_limit)
			request_module("snd-timer-%i", tid->device);
		break;
	case SNDRV_TIMER_CLASS_CARD:
	case SNDRV_TIMER_CLASS_PCM:
		if (tid->card < snd_ecards_limit)
			request_module("snd-card-%i", tid->card);
		break;
	default:
		break;
	}
}

#endif

/*
 * look for a master instance matching with the slave id of the given slave.
 * when found, relink the open_link of the slave.
 *
 * call this with register_mutex down.
 */
183
static int snd_timer_check_slave(struct snd_timer_instance *slave)
L
Linus Torvalds 已提交
184
{
185 186
	struct snd_timer *timer;
	struct snd_timer_instance *master;
L
Linus Torvalds 已提交
187 188

	/* FIXME: it's really dumb to look up all entries.. */
189 190
	list_for_each_entry(timer, &snd_timer_list, device_list) {
		list_for_each_entry(master, &timer->open_list_head, open_list) {
L
Linus Torvalds 已提交
191 192
			if (slave->slave_class == master->slave_class &&
			    slave->slave_id == master->slave_id) {
193 194 195
				if (master->timer->num_instances >=
				    master->timer->max_instances)
					return -EBUSY;
196 197
				list_move_tail(&slave->open_list,
					       &master->slave_list_head);
198
				master->timer->num_instances++;
L
Linus Torvalds 已提交
199 200 201 202
				spin_lock_irq(&slave_active_lock);
				slave->master = master;
				slave->timer = master->timer;
				spin_unlock_irq(&slave_active_lock);
203
				return 0;
L
Linus Torvalds 已提交
204 205 206
			}
		}
	}
207
	return 0;
L
Linus Torvalds 已提交
208 209 210 211 212 213 214 215
}

/*
 * look for slave instances matching with the slave id of the given master.
 * when found, relink the open_link of slaves.
 *
 * call this with register_mutex down.
 */
216
static int snd_timer_check_master(struct snd_timer_instance *master)
L
Linus Torvalds 已提交
217
{
218
	struct snd_timer_instance *slave, *tmp;
L
Linus Torvalds 已提交
219 220

	/* check all pending slaves */
221
	list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
L
Linus Torvalds 已提交
222 223
		if (slave->slave_class == master->slave_class &&
		    slave->slave_id == master->slave_id) {
224 225 226
			if (master->timer->num_instances >=
			    master->timer->max_instances)
				return -EBUSY;
227
			list_move_tail(&slave->open_list, &master->slave_list_head);
228
			master->timer->num_instances++;
L
Linus Torvalds 已提交
229
			spin_lock_irq(&slave_active_lock);
230
			spin_lock(&master->timer->lock);
L
Linus Torvalds 已提交
231 232 233
			slave->master = master;
			slave->timer = master->timer;
			if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
C
Clemens Ladisch 已提交
234 235
				list_add_tail(&slave->active_list,
					      &master->slave_active_head);
236
			spin_unlock(&master->timer->lock);
L
Linus Torvalds 已提交
237 238 239
			spin_unlock_irq(&slave_active_lock);
		}
	}
240
	return 0;
L
Linus Torvalds 已提交
241 242
}

243 244
static int snd_timer_close_locked(struct snd_timer_instance *timeri);

L
Linus Torvalds 已提交
245 246 247 248
/*
 * open a timer instance
 * when opening a master, the slave id must be here given.
 */
249 250
int snd_timer_open(struct snd_timer_instance **ti,
		   char *owner, struct snd_timer_id *tid,
L
Linus Torvalds 已提交
251 252
		   unsigned int slave_id)
{
253 254
	struct snd_timer *timer;
	struct snd_timer_instance *timeri = NULL;
255
	int err;
C
Clemens Ladisch 已提交
256

L
Linus Torvalds 已提交
257 258 259 260
	if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
		/* open a slave instance */
		if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
		    tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
261 262
			pr_debug("ALSA: timer: invalid slave class %i\n",
				 tid->dev_sclass);
L
Linus Torvalds 已提交
263 264
			return -EINVAL;
		}
265
		mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
266
		timeri = snd_timer_instance_new(owner, NULL);
267
		if (!timeri) {
268
			mutex_unlock(&register_mutex);
269 270
			return -ENOMEM;
		}
L
Linus Torvalds 已提交
271 272 273 274
		timeri->slave_class = tid->dev_sclass;
		timeri->slave_id = tid->device;
		timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
		list_add_tail(&timeri->open_list, &snd_timer_slave_list);
275 276 277 278 279
		err = snd_timer_check_slave(timeri);
		if (err < 0) {
			snd_timer_close_locked(timeri);
			timeri = NULL;
		}
280
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
281
		*ti = timeri;
282
		return err;
L
Linus Torvalds 已提交
283 284 285
	}

	/* open a master instance */
286
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
287
	timer = snd_timer_find(tid);
288 289
#ifdef CONFIG_MODULES
	if (!timer) {
290
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
291
		snd_timer_request(tid);
292
		mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
293 294 295
		timer = snd_timer_find(tid);
	}
#endif
296
	if (!timer) {
297
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
298 299
		return -ENODEV;
	}
300 301
	if (!list_empty(&timer->open_list_head)) {
		timeri = list_entry(timer->open_list_head.next,
302
				    struct snd_timer_instance, open_list);
303
		if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
304
			mutex_unlock(&register_mutex);
305 306 307
			return -EBUSY;
		}
	}
308 309 310 311
	if (timer->num_instances >= timer->max_instances) {
		mutex_unlock(&register_mutex);
		return -EBUSY;
	}
312 313
	timeri = snd_timer_instance_new(owner, timer);
	if (!timeri) {
314
		mutex_unlock(&register_mutex);
315 316
		return -ENOMEM;
	}
317 318 319
	/* take a card refcount for safe disconnection */
	if (timer->card)
		get_device(&timer->card->card_dev);
320 321
	timeri->slave_class = tid->dev_sclass;
	timeri->slave_id = slave_id;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

	if (list_empty(&timer->open_list_head) && timer->hw.open) {
		int err = timer->hw.open(timer);
		if (err) {
			kfree(timeri->owner);
			kfree(timeri);

			if (timer->card)
				put_device(&timer->card->card_dev);
			module_put(timer->module);
			mutex_unlock(&register_mutex);
			return err;
		}
	}

337
	list_add_tail(&timeri->open_list, &timer->open_list_head);
338 339 340 341 342 343
	timer->num_instances++;
	err = snd_timer_check_master(timeri);
	if (err < 0) {
		snd_timer_close_locked(timeri);
		timeri = NULL;
	}
344
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
345
	*ti = timeri;
346
	return err;
L
Linus Torvalds 已提交
347
}
348
EXPORT_SYMBOL(snd_timer_open);
L
Linus Torvalds 已提交
349 350 351

/*
 * close a timer instance
352
 * call this with register_mutex down.
L
Linus Torvalds 已提交
353
 */
354
static int snd_timer_close_locked(struct snd_timer_instance *timeri)
L
Linus Torvalds 已提交
355
{
356
	struct snd_timer *timer = NULL;
357
	struct snd_timer_instance *slave, *tmp;
L
Linus Torvalds 已提交
358

359 360
	list_del(&timeri->open_list);

L
Linus Torvalds 已提交
361 362 363
	/* force to stop the timer */
	snd_timer_stop(timeri);

364 365
	timer = timeri->timer;
	if (timer) {
366
		timer->num_instances--;
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374
		/* wait, until the active callback is finished */
		spin_lock_irq(&timer->lock);
		while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
			spin_unlock_irq(&timer->lock);
			udelay(10);
			spin_lock_irq(&timer->lock);
		}
		spin_unlock_irq(&timer->lock);
375

L
Linus Torvalds 已提交
376
		/* remove slave links */
377 378
		spin_lock_irq(&slave_active_lock);
		spin_lock(&timer->lock);
379 380 381
		list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
					 open_list) {
			list_move_tail(&slave->open_list, &snd_timer_slave_list);
382
			timer->num_instances--;
L
Linus Torvalds 已提交
383 384
			slave->master = NULL;
			slave->timer = NULL;
385 386
			list_del_init(&slave->ack_list);
			list_del_init(&slave->active_list);
L
Linus Torvalds 已提交
387
		}
388 389
		spin_unlock(&timer->lock);
		spin_unlock_irq(&slave_active_lock);
390 391 392 393

		/* slave doesn't need to release timer resources below */
		if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
			timer = NULL;
L
Linus Torvalds 已提交
394
	}
395

L
Linus Torvalds 已提交
396 397 398 399
	if (timeri->private_free)
		timeri->private_free(timeri);
	kfree(timeri->owner);
	kfree(timeri);
400 401 402 403 404 405 406

	if (timer) {
		if (list_empty(&timer->open_list_head) && timer->hw.close)
			timer->hw.close(timer);
		/* release a card refcount for safe disconnection */
		if (timer->card)
			put_device(&timer->card->card_dev);
407
		module_put(timer->module);
408 409
	}

L
Linus Torvalds 已提交
410 411
	return 0;
}
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

/*
 * close a timer instance
 */
int snd_timer_close(struct snd_timer_instance *timeri)
{
	int err;

	if (snd_BUG_ON(!timeri))
		return -ENXIO;

	mutex_lock(&register_mutex);
	err = snd_timer_close_locked(timeri);
	mutex_unlock(&register_mutex);
	return err;
}
428
EXPORT_SYMBOL(snd_timer_close);
L
Linus Torvalds 已提交
429

430 431 432 433 434 435 436 437
static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
{
	if (timer->hw.c_resolution)
		return timer->hw.c_resolution(timer);
	else
		return timer->hw.resolution;
}

438
unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
L
Linus Torvalds 已提交
439
{
440
	struct snd_timer * timer;
441 442
	unsigned long ret = 0;
	unsigned long flags;
L
Linus Torvalds 已提交
443 444 445

	if (timeri == NULL)
		return 0;
446
	timer = timeri->timer;
447 448 449 450 451 452
	if (timer) {
		spin_lock_irqsave(&timer->lock, flags);
		ret = snd_timer_hw_resolution(timer);
		spin_unlock_irqrestore(&timer->lock, flags);
	}
	return ret;
L
Linus Torvalds 已提交
453
}
454
EXPORT_SYMBOL(snd_timer_resolution);
L
Linus Torvalds 已提交
455

456
static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
L
Linus Torvalds 已提交
457
{
458
	struct snd_timer *timer = ti->timer;
L
Linus Torvalds 已提交
459
	unsigned long resolution = 0;
460
	struct snd_timer_instance *ts;
L
Linus Torvalds 已提交
461 462
	struct timespec tstamp;

463
	if (timer_tstamp_monotonic)
T
Thomas Gleixner 已提交
464
		ktime_get_ts(&tstamp);
465 466
	else
		getnstimeofday(&tstamp);
467 468 469
	if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
		       event > SNDRV_TIMER_EVENT_PAUSE))
		return;
470 471 472 473
	if (timer &&
	    (event == SNDRV_TIMER_EVENT_START ||
	     event == SNDRV_TIMER_EVENT_CONTINUE))
		resolution = snd_timer_hw_resolution(timer);
L
Linus Torvalds 已提交
474
	if (ti->ccallback)
475
		ti->ccallback(ti, event, &tstamp, resolution);
L
Linus Torvalds 已提交
476 477 478 479 480 481
	if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
		return;
	if (timer == NULL)
		return;
	if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
		return;
482
	list_for_each_entry(ts, &ti->slave_active_head, active_list)
L
Linus Torvalds 已提交
483
		if (ts->ccallback)
484
			ts->ccallback(ts, event + 100, &tstamp, resolution);
L
Linus Torvalds 已提交
485 486
}

487 488 489
/* start/continue a master timer */
static int snd_timer_start1(struct snd_timer_instance *timeri,
			    bool start, unsigned long ticks)
L
Linus Torvalds 已提交
490
{
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	struct snd_timer *timer;
	int result;
	unsigned long flags;

	timer = timeri->timer;
	if (!timer)
		return -EINVAL;

	spin_lock_irqsave(&timer->lock, flags);
	if (timer->card && timer->card->shutdown) {
		result = -ENODEV;
		goto unlock;
	}
	if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
			     SNDRV_TIMER_IFLG_START)) {
		result = -EBUSY;
		goto unlock;
	}

	if (start)
		timeri->ticks = timeri->cticks = ticks;
	else if (!timeri->cticks)
		timeri->cticks = 1;
	timeri->pticks = 0;

516
	list_move_tail(&timeri->active_list, &timer->active_list_head);
L
Linus Torvalds 已提交
517 518 519 520 521
	if (timer->running) {
		if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
			goto __start_now;
		timer->flags |= SNDRV_TIMER_FLG_RESCHED;
		timeri->flags |= SNDRV_TIMER_IFLG_START;
522
		result = 1; /* delayed start */
L
Linus Torvalds 已提交
523
	} else {
524 525
		if (start)
			timer->sticks = ticks;
L
Linus Torvalds 已提交
526 527 528 529
		timer->hw.start(timer);
	      __start_now:
		timer->running++;
		timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
530
		result = 0;
L
Linus Torvalds 已提交
531
	}
532 533 534 535 536
	snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
			  SNDRV_TIMER_EVENT_CONTINUE);
 unlock:
	spin_unlock_irqrestore(&timer->lock, flags);
	return result;
L
Linus Torvalds 已提交
537 538
}

539 540 541
/* start/continue a slave timer */
static int snd_timer_start_slave(struct snd_timer_instance *timeri,
				 bool start)
L
Linus Torvalds 已提交
542 543 544 545
{
	unsigned long flags;

	spin_lock_irqsave(&slave_active_lock, flags);
546 547 548 549
	if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
		spin_unlock_irqrestore(&slave_active_lock, flags);
		return -EBUSY;
	}
L
Linus Torvalds 已提交
550
	timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
551 552
	if (timeri->master && timeri->timer) {
		spin_lock(&timeri->timer->lock);
C
Clemens Ladisch 已提交
553 554
		list_add_tail(&timeri->active_list,
			      &timeri->master->slave_active_head);
555 556
		snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
				  SNDRV_TIMER_EVENT_CONTINUE);
557 558
		spin_unlock(&timeri->timer->lock);
	}
L
Linus Torvalds 已提交
559 560 561 562
	spin_unlock_irqrestore(&slave_active_lock, flags);
	return 1; /* delayed start */
}

563 564
/* stop/pause a master timer */
static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
L
Linus Torvalds 已提交
565
{
566
	struct snd_timer *timer;
567
	int result = 0;
L
Linus Torvalds 已提交
568 569 570 571 572 573
	unsigned long flags;

	timer = timeri->timer;
	if (!timer)
		return -EINVAL;
	spin_lock_irqsave(&timer->lock, flags);
574 575
	if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
			       SNDRV_TIMER_IFLG_START))) {
576 577
		result = -EBUSY;
		goto unlock;
578
	}
L
Linus Torvalds 已提交
579 580
	list_del_init(&timeri->ack_list);
	list_del_init(&timeri->active_list);
581 582 583 584 585
	if (timer->card && timer->card->shutdown)
		goto unlock;
	if (stop) {
		timeri->cticks = timeri->ticks;
		timeri->pticks = 0;
586
	}
L
Linus Torvalds 已提交
587 588 589 590 591 592 593 594 595 596 597 598
	if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
	    !(--timer->running)) {
		timer->hw.stop(timer);
		if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
			timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
			snd_timer_reschedule(timer, 0);
			if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
				timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
				timer->hw.start(timer);
			}
		}
	}
T
Takashi Iwai 已提交
599
	timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
600 601 602 603
	if (stop)
		timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
	else
		timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
604
	snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
605
			  SNDRV_TIMER_EVENT_PAUSE);
606
 unlock:
L
Linus Torvalds 已提交
607
	spin_unlock_irqrestore(&timer->lock, flags);
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
	return result;
}

/* stop/pause a slave timer */
static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
{
	unsigned long flags;

	spin_lock_irqsave(&slave_active_lock, flags);
	if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
		spin_unlock_irqrestore(&slave_active_lock, flags);
		return -EBUSY;
	}
	timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
	if (timeri->timer) {
		spin_lock(&timeri->timer->lock);
		list_del_init(&timeri->ack_list);
		list_del_init(&timeri->active_list);
		snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
627
				  SNDRV_TIMER_EVENT_PAUSE);
628 629 630
		spin_unlock(&timeri->timer->lock);
	}
	spin_unlock_irqrestore(&slave_active_lock, flags);
L
Linus Torvalds 已提交
631 632 633
	return 0;
}

634 635 636 637 638 639 640 641 642 643 644 645
/*
 *  start the timer instance
 */
int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
{
	if (timeri == NULL || ticks < 1)
		return -EINVAL;
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
		return snd_timer_start_slave(timeri, true);
	else
		return snd_timer_start1(timeri, true, ticks);
}
646
EXPORT_SYMBOL(snd_timer_start);
647

L
Linus Torvalds 已提交
648 649 650 651 652
/*
 * stop the timer instance.
 *
 * do not call this from the timer callback!
 */
653
int snd_timer_stop(struct snd_timer_instance *timeri)
L
Linus Torvalds 已提交
654
{
655 656 657 658
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
		return snd_timer_stop_slave(timeri, true);
	else
		return snd_timer_stop1(timeri, true);
L
Linus Torvalds 已提交
659
}
660
EXPORT_SYMBOL(snd_timer_stop);
L
Linus Torvalds 已提交
661 662 663 664

/*
 * start again..  the tick is kept.
 */
665
int snd_timer_continue(struct snd_timer_instance *timeri)
L
Linus Torvalds 已提交
666
{
667 668 669 670
	/* timer can continue only after pause */
	if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
		return -EINVAL;

L
Linus Torvalds 已提交
671
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
672 673 674
		return snd_timer_start_slave(timeri, false);
	else
		return snd_timer_start1(timeri, false, 0);
L
Linus Torvalds 已提交
675
}
676
EXPORT_SYMBOL(snd_timer_continue);
L
Linus Torvalds 已提交
677 678 679 680

/*
 * pause.. remember the ticks left
 */
681
int snd_timer_pause(struct snd_timer_instance * timeri)
L
Linus Torvalds 已提交
682
{
683 684 685 686
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
		return snd_timer_stop_slave(timeri, false);
	else
		return snd_timer_stop1(timeri, false);
L
Linus Torvalds 已提交
687
}
688
EXPORT_SYMBOL(snd_timer_pause);
L
Linus Torvalds 已提交
689 690 691 692 693 694 695

/*
 * reschedule the timer
 *
 * start pending instances and check the scheduling ticks.
 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
 */
696
static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
L
Linus Torvalds 已提交
697
{
698
	struct snd_timer_instance *ti;
L
Linus Torvalds 已提交
699 700
	unsigned long ticks = ~0UL;

701
	list_for_each_entry(ti, &timer->active_list_head, active_list) {
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
		if (ti->flags & SNDRV_TIMER_IFLG_START) {
			ti->flags &= ~SNDRV_TIMER_IFLG_START;
			ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
			timer->running++;
		}
		if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
			if (ticks > ti->cticks)
				ticks = ti->cticks;
		}
	}
	if (ticks == ~0UL) {
		timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
		return;
	}
	if (ticks > timer->hw.ticks)
		ticks = timer->hw.ticks;
	if (ticks_left != ticks)
		timer->flags |= SNDRV_TIMER_FLG_CHANGE;
	timer->sticks = ticks;
}

/*
 * timer tasklet
 *
 */
static void snd_timer_tasklet(unsigned long arg)
{
729 730
	struct snd_timer *timer = (struct snd_timer *) arg;
	struct snd_timer_instance *ti;
L
Linus Torvalds 已提交
731 732
	struct list_head *p;
	unsigned long resolution, ticks;
733
	unsigned long flags;
L
Linus Torvalds 已提交
734

735 736 737
	if (timer->card && timer->card->shutdown)
		return;

738
	spin_lock_irqsave(&timer->lock, flags);
L
Linus Torvalds 已提交
739 740 741
	/* now process all callbacks */
	while (!list_empty(&timer->sack_list_head)) {
		p = timer->sack_list_head.next;		/* get first item */
742
		ti = list_entry(p, struct snd_timer_instance, ack_list);
L
Linus Torvalds 已提交
743 744 745

		/* remove from ack_list and make empty */
		list_del_init(p);
C
Clemens Ladisch 已提交
746

L
Linus Torvalds 已提交
747 748 749 750 751 752 753 754 755 756 757
		ticks = ti->pticks;
		ti->pticks = 0;
		resolution = ti->resolution;

		ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
		spin_unlock(&timer->lock);
		if (ti->callback)
			ti->callback(ti, resolution, ticks);
		spin_lock(&timer->lock);
		ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
	}
758
	spin_unlock_irqrestore(&timer->lock, flags);
L
Linus Torvalds 已提交
759 760 761 762 763 764 765 766
}

/*
 * timer interrupt
 *
 * ticks_left is usually equal to timer->sticks.
 *
 */
767
void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
L
Linus Torvalds 已提交
768
{
769
	struct snd_timer_instance *ti, *ts, *tmp;
L
Linus Torvalds 已提交
770
	unsigned long resolution, ticks;
771
	struct list_head *p, *ack_list_head;
772
	unsigned long flags;
L
Linus Torvalds 已提交
773 774 775 776 777
	int use_tasklet = 0;

	if (timer == NULL)
		return;

778 779 780
	if (timer->card && timer->card->shutdown)
		return;

781
	spin_lock_irqsave(&timer->lock, flags);
L
Linus Torvalds 已提交
782 783

	/* remember the current resolution */
784
	resolution = snd_timer_hw_resolution(timer);
L
Linus Torvalds 已提交
785 786

	/* loop for all active instances
787
	 * Here we cannot use list_for_each_entry because the active_list of a
C
Clemens Ladisch 已提交
788 789
	 * processed instance is relinked to done_list_head before the callback
	 * is called.
L
Linus Torvalds 已提交
790
	 */
791 792
	list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
				 active_list) {
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806
		if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
			continue;
		ti->pticks += ticks_left;
		ti->resolution = resolution;
		if (ti->cticks < ticks_left)
			ti->cticks = 0;
		else
			ti->cticks -= ticks_left;
		if (ti->cticks) /* not expired */
			continue;
		if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
			ti->cticks = ti->ticks;
		} else {
			ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
807 808
			--timer->running;
			list_del_init(&ti->active_list);
L
Linus Torvalds 已提交
809
		}
C
Clemens Ladisch 已提交
810 811 812 813 814 815 816
		if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
		    (ti->flags & SNDRV_TIMER_IFLG_FAST))
			ack_list_head = &timer->ack_list_head;
		else
			ack_list_head = &timer->sack_list_head;
		if (list_empty(&ti->ack_list))
			list_add_tail(&ti->ack_list, ack_list_head);
817
		list_for_each_entry(ts, &ti->slave_active_head, active_list) {
L
Linus Torvalds 已提交
818 819
			ts->pticks = ti->pticks;
			ts->resolution = resolution;
C
Clemens Ladisch 已提交
820 821
			if (list_empty(&ts->ack_list))
				list_add_tail(&ts->ack_list, ack_list_head);
L
Linus Torvalds 已提交
822 823 824
		}
	}
	if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
825
		snd_timer_reschedule(timer, timer->sticks);
L
Linus Torvalds 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
	if (timer->running) {
		if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
			timer->hw.stop(timer);
			timer->flags |= SNDRV_TIMER_FLG_CHANGE;
		}
		if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
		    (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
			/* restart timer */
			timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
			timer->hw.start(timer);
		}
	} else {
		timer->hw.stop(timer);
	}

	/* now process all fast callbacks */
	while (!list_empty(&timer->ack_list_head)) {
		p = timer->ack_list_head.next;		/* get first item */
844
		ti = list_entry(p, struct snd_timer_instance, ack_list);
C
Clemens Ladisch 已提交
845

L
Linus Torvalds 已提交
846 847
		/* remove from ack_list and make empty */
		list_del_init(p);
C
Clemens Ladisch 已提交
848

L
Linus Torvalds 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861
		ticks = ti->pticks;
		ti->pticks = 0;

		ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
		spin_unlock(&timer->lock);
		if (ti->callback)
			ti->callback(ti, resolution, ticks);
		spin_lock(&timer->lock);
		ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
	}

	/* do we have any slow callbacks? */
	use_tasklet = !list_empty(&timer->sack_list_head);
862
	spin_unlock_irqrestore(&timer->lock, flags);
L
Linus Torvalds 已提交
863 864

	if (use_tasklet)
865
		tasklet_schedule(&timer->task_queue);
L
Linus Torvalds 已提交
866
}
867
EXPORT_SYMBOL(snd_timer_interrupt);
L
Linus Torvalds 已提交
868 869 870 871 872

/*

 */

873 874
int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
		  struct snd_timer **rtimer)
L
Linus Torvalds 已提交
875
{
876
	struct snd_timer *timer;
L
Linus Torvalds 已提交
877
	int err;
878
	static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
879 880
		.dev_free = snd_timer_dev_free,
		.dev_register = snd_timer_dev_register,
881
		.dev_disconnect = snd_timer_dev_disconnect,
L
Linus Torvalds 已提交
882 883
	};

884 885 886 887
	if (snd_BUG_ON(!tid))
		return -EINVAL;
	if (rtimer)
		*rtimer = NULL;
888
	timer = kzalloc(sizeof(*timer), GFP_KERNEL);
889
	if (!timer)
L
Linus Torvalds 已提交
890 891 892 893 894 895 896
		return -ENOMEM;
	timer->tmr_class = tid->dev_class;
	timer->card = card;
	timer->tmr_device = tid->device;
	timer->tmr_subdevice = tid->subdevice;
	if (id)
		strlcpy(timer->id, id, sizeof(timer->id));
897
	timer->sticks = 1;
L
Linus Torvalds 已提交
898 899 900 901 902 903
	INIT_LIST_HEAD(&timer->device_list);
	INIT_LIST_HEAD(&timer->open_list_head);
	INIT_LIST_HEAD(&timer->active_list_head);
	INIT_LIST_HEAD(&timer->ack_list_head);
	INIT_LIST_HEAD(&timer->sack_list_head);
	spin_lock_init(&timer->lock);
C
Clemens Ladisch 已提交
904 905
	tasklet_init(&timer->task_queue, snd_timer_tasklet,
		     (unsigned long)timer);
906
	timer->max_instances = 1000; /* default limit per timer */
L
Linus Torvalds 已提交
907
	if (card != NULL) {
908
		timer->module = card->module;
C
Clemens Ladisch 已提交
909 910
		err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
		if (err < 0) {
L
Linus Torvalds 已提交
911 912 913 914
			snd_timer_free(timer);
			return err;
		}
	}
915 916
	if (rtimer)
		*rtimer = timer;
L
Linus Torvalds 已提交
917 918
	return 0;
}
919
EXPORT_SYMBOL(snd_timer_new);
L
Linus Torvalds 已提交
920

921
static int snd_timer_free(struct snd_timer *timer)
L
Linus Torvalds 已提交
922
{
923 924
	if (!timer)
		return 0;
925 926 927 928 929

	mutex_lock(&register_mutex);
	if (! list_empty(&timer->open_list_head)) {
		struct list_head *p, *n;
		struct snd_timer_instance *ti;
930
		pr_warn("ALSA: timer %p is busy?\n", timer);
931 932 933 934 935 936 937 938 939
		list_for_each_safe(p, n, &timer->open_list_head) {
			list_del_init(p);
			ti = list_entry(p, struct snd_timer_instance, open_list);
			ti->timer = NULL;
		}
	}
	list_del(&timer->device_list);
	mutex_unlock(&register_mutex);

L
Linus Torvalds 已提交
940 941 942 943 944 945
	if (timer->private_free)
		timer->private_free(timer);
	kfree(timer);
	return 0;
}

946
static int snd_timer_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
947
{
948
	struct snd_timer *timer = device->device_data;
L
Linus Torvalds 已提交
949 950 951
	return snd_timer_free(timer);
}

952
static int snd_timer_dev_register(struct snd_device *dev)
L
Linus Torvalds 已提交
953
{
954 955
	struct snd_timer *timer = dev->device_data;
	struct snd_timer *timer1;
L
Linus Torvalds 已提交
956

957 958
	if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
		return -ENXIO;
L
Linus Torvalds 已提交
959 960 961 962
	if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
	    !timer->hw.resolution && timer->hw.c_resolution == NULL)
	    	return -EINVAL;

963
	mutex_lock(&register_mutex);
964
	list_for_each_entry(timer1, &snd_timer_list, device_list) {
L
Linus Torvalds 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
		if (timer1->tmr_class > timer->tmr_class)
			break;
		if (timer1->tmr_class < timer->tmr_class)
			continue;
		if (timer1->card && timer->card) {
			if (timer1->card->number > timer->card->number)
				break;
			if (timer1->card->number < timer->card->number)
				continue;
		}
		if (timer1->tmr_device > timer->tmr_device)
			break;
		if (timer1->tmr_device < timer->tmr_device)
			continue;
		if (timer1->tmr_subdevice > timer->tmr_subdevice)
			break;
		if (timer1->tmr_subdevice < timer->tmr_subdevice)
			continue;
		/* conflicts.. */
984
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
985 986
		return -EBUSY;
	}
987
	list_add_tail(&timer->device_list, &timer1->device_list);
988
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
989 990 991
	return 0;
}

992
static int snd_timer_dev_disconnect(struct snd_device *device)
L
Linus Torvalds 已提交
993
{
994
	struct snd_timer *timer = device->device_data;
995 996
	struct snd_timer_instance *ti;

997
	mutex_lock(&register_mutex);
998
	list_del_init(&timer->device_list);
999 1000
	/* wake up pending sleepers */
	list_for_each_entry(ti, &timer->open_list_head, open_list) {
1001 1002
		if (ti->disconnect)
			ti->disconnect(ti);
1003
	}
1004
	mutex_unlock(&register_mutex);
1005
	return 0;
L
Linus Torvalds 已提交
1006 1007
}

1008
void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
L
Linus Torvalds 已提交
1009 1010 1011
{
	unsigned long flags;
	unsigned long resolution = 0;
1012
	struct snd_timer_instance *ti, *ts;
L
Linus Torvalds 已提交
1013

1014 1015
	if (timer->card && timer->card->shutdown)
		return;
1016 1017
	if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
		return;
1018 1019 1020
	if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
		       event > SNDRV_TIMER_EVENT_MRESUME))
		return;
L
Linus Torvalds 已提交
1021
	spin_lock_irqsave(&timer->lock, flags);
1022 1023
	if (event == SNDRV_TIMER_EVENT_MSTART ||
	    event == SNDRV_TIMER_EVENT_MCONTINUE ||
1024 1025
	    event == SNDRV_TIMER_EVENT_MRESUME)
		resolution = snd_timer_hw_resolution(timer);
1026
	list_for_each_entry(ti, &timer->active_list_head, active_list) {
L
Linus Torvalds 已提交
1027 1028
		if (ti->ccallback)
			ti->ccallback(ti, event, tstamp, resolution);
1029
		list_for_each_entry(ts, &ti->slave_active_head, active_list)
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034
			if (ts->ccallback)
				ts->ccallback(ts, event, tstamp, resolution);
	}
	spin_unlock_irqrestore(&timer->lock, flags);
}
1035
EXPORT_SYMBOL(snd_timer_notify);
L
Linus Torvalds 已提交
1036 1037 1038 1039

/*
 * exported functions for global timers
 */
1040
int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
L
Linus Torvalds 已提交
1041
{
1042
	struct snd_timer_id tid;
C
Clemens Ladisch 已提交
1043

L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049 1050
	tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	tid.card = -1;
	tid.device = device;
	tid.subdevice = 0;
	return snd_timer_new(NULL, id, &tid, rtimer);
}
1051
EXPORT_SYMBOL(snd_timer_global_new);
L
Linus Torvalds 已提交
1052

1053
int snd_timer_global_free(struct snd_timer *timer)
L
Linus Torvalds 已提交
1054 1055 1056
{
	return snd_timer_free(timer);
}
1057
EXPORT_SYMBOL(snd_timer_global_free);
L
Linus Torvalds 已提交
1058

1059
int snd_timer_global_register(struct snd_timer *timer)
L
Linus Torvalds 已提交
1060
{
1061
	struct snd_device dev;
L
Linus Torvalds 已提交
1062 1063 1064 1065 1066

	memset(&dev, 0, sizeof(dev));
	dev.device_data = timer;
	return snd_timer_dev_register(&dev);
}
1067
EXPORT_SYMBOL(snd_timer_global_register);
L
Linus Torvalds 已提交
1068

C
Clemens Ladisch 已提交
1069
/*
L
Linus Torvalds 已提交
1070 1071 1072 1073 1074
 *  System timer
 */

struct snd_timer_system_private {
	struct timer_list tlist;
1075
	struct snd_timer *snd_timer;
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080
	unsigned long last_expires;
	unsigned long last_jiffies;
	unsigned long correction;
};

1081
static void snd_timer_s_function(struct timer_list *t)
L
Linus Torvalds 已提交
1082
{
1083 1084 1085
	struct snd_timer_system_private *priv = from_timer(priv, t,
								tlist);
	struct snd_timer *timer = priv->snd_timer;
L
Linus Torvalds 已提交
1086 1087
	unsigned long jiff = jiffies;
	if (time_after(jiff, priv->last_expires))
1088
		priv->correction += (long)jiff - (long)priv->last_expires;
L
Linus Torvalds 已提交
1089 1090 1091
	snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
}

1092
static int snd_timer_s_start(struct snd_timer * timer)
L
Linus Torvalds 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
{
	struct snd_timer_system_private *priv;
	unsigned long njiff;

	priv = (struct snd_timer_system_private *) timer->private_data;
	njiff = (priv->last_jiffies = jiffies);
	if (priv->correction > timer->sticks - 1) {
		priv->correction -= timer->sticks - 1;
		njiff++;
	} else {
		njiff += timer->sticks - priv->correction;
1104
		priv->correction = 0;
L
Linus Torvalds 已提交
1105
	}
1106 1107
	priv->last_expires = njiff;
	mod_timer(&priv->tlist, njiff);
L
Linus Torvalds 已提交
1108 1109 1110
	return 0;
}

1111
static int snd_timer_s_stop(struct snd_timer * timer)
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
{
	struct snd_timer_system_private *priv;
	unsigned long jiff;

	priv = (struct snd_timer_system_private *) timer->private_data;
	del_timer(&priv->tlist);
	jiff = jiffies;
	if (time_before(jiff, priv->last_expires))
		timer->sticks = priv->last_expires - jiff;
	else
		timer->sticks = 1;
1123
	priv->correction = 0;
L
Linus Torvalds 已提交
1124 1125 1126
	return 0;
}

1127 1128 1129 1130 1131 1132 1133 1134 1135
static int snd_timer_s_close(struct snd_timer *timer)
{
	struct snd_timer_system_private *priv;

	priv = (struct snd_timer_system_private *)timer->private_data;
	del_timer_sync(&priv->tlist);
	return 0;
}

1136
static struct snd_timer_hardware snd_timer_system =
L
Linus Torvalds 已提交
1137 1138 1139 1140
{
	.flags =	SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
	.resolution =	1000000000L / HZ,
	.ticks =	10000000L,
1141
	.close =	snd_timer_s_close,
L
Linus Torvalds 已提交
1142 1143 1144 1145
	.start =	snd_timer_s_start,
	.stop =		snd_timer_s_stop
};

1146
static void snd_timer_free_system(struct snd_timer *timer)
L
Linus Torvalds 已提交
1147 1148 1149 1150 1151 1152
{
	kfree(timer->private_data);
}

static int snd_timer_register_system(void)
{
1153
	struct snd_timer *timer;
L
Linus Torvalds 已提交
1154 1155 1156
	struct snd_timer_system_private *priv;
	int err;

C
Clemens Ladisch 已提交
1157 1158
	err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
	if (err < 0)
L
Linus Torvalds 已提交
1159 1160 1161
		return err;
	strcpy(timer->name, "system timer");
	timer->hw = snd_timer_system;
1162
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
L
Linus Torvalds 已提交
1163 1164 1165 1166
	if (priv == NULL) {
		snd_timer_free(timer);
		return -ENOMEM;
	}
1167 1168
	priv->snd_timer = timer;
	timer_setup(&priv->tlist, snd_timer_s_function, 0);
L
Linus Torvalds 已提交
1169 1170 1171 1172 1173
	timer->private_data = priv;
	timer->private_free = snd_timer_free_system;
	return snd_timer_global_register(timer);
}

1174
#ifdef CONFIG_SND_PROC_FS
L
Linus Torvalds 已提交
1175 1176 1177 1178
/*
 *  Info interface
 */

1179 1180
static void snd_timer_proc_read(struct snd_info_entry *entry,
				struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
1181
{
1182 1183
	struct snd_timer *timer;
	struct snd_timer_instance *ti;
L
Linus Torvalds 已提交
1184

1185
	mutex_lock(&register_mutex);
1186
	list_for_each_entry(timer, &snd_timer_list, device_list) {
1187 1188
		if (timer->card && timer->card->shutdown)
			continue;
L
Linus Torvalds 已提交
1189 1190 1191 1192 1193
		switch (timer->tmr_class) {
		case SNDRV_TIMER_CLASS_GLOBAL:
			snd_iprintf(buffer, "G%i: ", timer->tmr_device);
			break;
		case SNDRV_TIMER_CLASS_CARD:
C
Clemens Ladisch 已提交
1194 1195
			snd_iprintf(buffer, "C%i-%i: ",
				    timer->card->number, timer->tmr_device);
L
Linus Torvalds 已提交
1196 1197
			break;
		case SNDRV_TIMER_CLASS_PCM:
C
Clemens Ladisch 已提交
1198 1199
			snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
				    timer->tmr_device, timer->tmr_subdevice);
L
Linus Torvalds 已提交
1200 1201
			break;
		default:
C
Clemens Ladisch 已提交
1202 1203 1204
			snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
				    timer->card ? timer->card->number : -1,
				    timer->tmr_device, timer->tmr_subdevice);
L
Linus Torvalds 已提交
1205 1206 1207
		}
		snd_iprintf(buffer, "%s :", timer->name);
		if (timer->hw.resolution)
C
Clemens Ladisch 已提交
1208 1209 1210 1211
			snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
				    timer->hw.resolution / 1000,
				    timer->hw.resolution % 1000,
				    timer->hw.ticks);
L
Linus Torvalds 已提交
1212 1213 1214
		if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
			snd_iprintf(buffer, " SLAVE");
		snd_iprintf(buffer, "\n");
1215
		list_for_each_entry(ti, &timer->open_list_head, open_list)
C
Clemens Ladisch 已提交
1216 1217 1218 1219 1220
			snd_iprintf(buffer, "  Client %s : %s\n",
				    ti->owner ? ti->owner : "unknown",
				    ti->flags & (SNDRV_TIMER_IFLG_START |
						 SNDRV_TIMER_IFLG_RUNNING)
				    ? "running" : "stopped");
L
Linus Torvalds 已提交
1221
	}
1222
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1223 1224
}

1225
static struct snd_info_entry *snd_timer_proc_entry;
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

static void __init snd_timer_proc_init(void)
{
	struct snd_info_entry *entry;

	entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
	if (entry != NULL) {
		entry->c.text.read = snd_timer_proc_read;
		if (snd_info_register(entry) < 0) {
			snd_info_free_entry(entry);
			entry = NULL;
		}
	}
	snd_timer_proc_entry = entry;
}

static void __exit snd_timer_proc_done(void)
{
1244
	snd_info_free_entry(snd_timer_proc_entry);
1245
}
1246
#else /* !CONFIG_SND_PROC_FS */
1247 1248 1249 1250
#define snd_timer_proc_init()
#define snd_timer_proc_done()
#endif

L
Linus Torvalds 已提交
1251 1252 1253 1254
/*
 *  USER SPACE interface
 */

1255
static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
L
Linus Torvalds 已提交
1256 1257 1258
				     unsigned long resolution,
				     unsigned long ticks)
{
1259 1260
	struct snd_timer_user *tu = timeri->callback_data;
	struct snd_timer_read *r;
L
Linus Torvalds 已提交
1261
	int prev;
C
Clemens Ladisch 已提交
1262

L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	spin_lock(&tu->qlock);
	if (tu->qused > 0) {
		prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
		r = &tu->queue[prev];
		if (r->resolution == resolution) {
			r->ticks += ticks;
			goto __wake;
		}
	}
	if (tu->qused >= tu->queue_size) {
		tu->overrun++;
	} else {
		r = &tu->queue[tu->qtail++];
		tu->qtail %= tu->queue_size;
		r->resolution = resolution;
		r->ticks = ticks;
		tu->qused++;
	}
      __wake:
	spin_unlock(&tu->qlock);
	kill_fasync(&tu->fasync, SIGIO, POLL_IN);
	wake_up(&tu->qchange_sleep);
}

1287 1288
static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
					    struct snd_timer_tread *tread)
L
Linus Torvalds 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
{
	if (tu->qused >= tu->queue_size) {
		tu->overrun++;
	} else {
		memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
		tu->qtail %= tu->queue_size;
		tu->qused++;
	}
}

1299 1300
static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
				     int event,
L
Linus Torvalds 已提交
1301 1302 1303
				     struct timespec *tstamp,
				     unsigned long resolution)
{
1304 1305
	struct snd_timer_user *tu = timeri->callback_data;
	struct snd_timer_tread r1;
1306
	unsigned long flags;
L
Linus Torvalds 已提交
1307

C
Clemens Ladisch 已提交
1308 1309
	if (event >= SNDRV_TIMER_EVENT_START &&
	    event <= SNDRV_TIMER_EVENT_PAUSE)
L
Linus Torvalds 已提交
1310 1311 1312
		tu->tstamp = *tstamp;
	if ((tu->filter & (1 << event)) == 0 || !tu->tread)
		return;
1313
	memset(&r1, 0, sizeof(r1));
L
Linus Torvalds 已提交
1314 1315 1316
	r1.event = event;
	r1.tstamp = *tstamp;
	r1.val = resolution;
1317
	spin_lock_irqsave(&tu->qlock, flags);
L
Linus Torvalds 已提交
1318
	snd_timer_user_append_to_tqueue(tu, &r1);
1319
	spin_unlock_irqrestore(&tu->qlock, flags);
L
Linus Torvalds 已提交
1320 1321 1322 1323
	kill_fasync(&tu->fasync, SIGIO, POLL_IN);
	wake_up(&tu->qchange_sleep);
}

1324 1325 1326 1327 1328 1329 1330 1331
static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
{
	struct snd_timer_user *tu = timeri->callback_data;

	tu->disconnected = true;
	wake_up(&tu->qchange_sleep);
}

1332
static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
L
Linus Torvalds 已提交
1333 1334 1335
				      unsigned long resolution,
				      unsigned long ticks)
{
1336 1337
	struct snd_timer_user *tu = timeri->callback_data;
	struct snd_timer_tread *r, r1;
L
Linus Torvalds 已提交
1338 1339 1340
	struct timespec tstamp;
	int prev, append = 0;

1341
	memset(&r1, 0, sizeof(r1));
T
Takashi Iwai 已提交
1342
	memset(&tstamp, 0, sizeof(tstamp));
L
Linus Torvalds 已提交
1343
	spin_lock(&tu->qlock);
C
Clemens Ladisch 已提交
1344 1345
	if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
			   (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
L
Linus Torvalds 已提交
1346 1347 1348
		spin_unlock(&tu->qlock);
		return;
	}
1349 1350
	if (tu->last_resolution != resolution || ticks > 0) {
		if (timer_tstamp_monotonic)
T
Thomas Gleixner 已提交
1351
			ktime_get_ts(&tstamp);
1352 1353 1354
		else
			getnstimeofday(&tstamp);
	}
C
Clemens Ladisch 已提交
1355 1356
	if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
	    tu->last_resolution != resolution) {
L
Linus Torvalds 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
		r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
		r1.tstamp = tstamp;
		r1.val = resolution;
		snd_timer_user_append_to_tqueue(tu, &r1);
		tu->last_resolution = resolution;
		append++;
	}
	if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
		goto __wake;
	if (ticks == 0)
		goto __wake;
	if (tu->qused > 0) {
		prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
		r = &tu->tqueue[prev];
		if (r->event == SNDRV_TIMER_EVENT_TICK) {
			r->tstamp = tstamp;
			r->val += ticks;
			append++;
			goto __wake;
		}
	}
	r1.event = SNDRV_TIMER_EVENT_TICK;
	r1.tstamp = tstamp;
	r1.val = ticks;
	snd_timer_user_append_to_tqueue(tu, &r1);
	append++;
      __wake:
	spin_unlock(&tu->qlock);
	if (append == 0)
		return;
	kill_fasync(&tu->fasync, SIGIO, POLL_IN);
	wake_up(&tu->qchange_sleep);
}

1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
static int realloc_user_queue(struct snd_timer_user *tu, int size)
{
	struct snd_timer_read *queue = NULL;
	struct snd_timer_tread *tqueue = NULL;

	if (tu->tread) {
		tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
		if (!tqueue)
			return -ENOMEM;
	} else {
		queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
		if (!queue)
			return -ENOMEM;
	}

	spin_lock_irq(&tu->qlock);
	kfree(tu->queue);
	kfree(tu->tqueue);
	tu->queue_size = size;
	tu->queue = queue;
	tu->tqueue = tqueue;
	tu->qhead = tu->qtail = tu->qused = 0;
	spin_unlock_irq(&tu->qlock);

	return 0;
}

L
Linus Torvalds 已提交
1418 1419
static int snd_timer_user_open(struct inode *inode, struct file *file)
{
1420
	struct snd_timer_user *tu;
T
Takashi Iwai 已提交
1421 1422 1423 1424 1425
	int err;

	err = nonseekable_open(inode, file);
	if (err < 0)
		return err;
C
Clemens Ladisch 已提交
1426

1427
	tu = kzalloc(sizeof(*tu), GFP_KERNEL);
L
Linus Torvalds 已提交
1428 1429 1430 1431
	if (tu == NULL)
		return -ENOMEM;
	spin_lock_init(&tu->qlock);
	init_waitqueue_head(&tu->qchange_sleep);
1432
	mutex_init(&tu->ioctl_lock);
L
Linus Torvalds 已提交
1433
	tu->ticks = 1;
1434
	if (realloc_user_queue(tu, 128) < 0) {
L
Linus Torvalds 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443
		kfree(tu);
		return -ENOMEM;
	}
	file->private_data = tu;
	return 0;
}

static int snd_timer_user_release(struct inode *inode, struct file *file)
{
1444
	struct snd_timer_user *tu;
L
Linus Torvalds 已提交
1445 1446 1447 1448

	if (file->private_data) {
		tu = file->private_data;
		file->private_data = NULL;
1449
		mutex_lock(&tu->ioctl_lock);
L
Linus Torvalds 已提交
1450 1451
		if (tu->timeri)
			snd_timer_close(tu->timeri);
1452
		mutex_unlock(&tu->ioctl_lock);
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458 1459
		kfree(tu->queue);
		kfree(tu->tqueue);
		kfree(tu);
	}
	return 0;
}

1460
static void snd_timer_user_zero_id(struct snd_timer_id *id)
L
Linus Torvalds 已提交
1461 1462 1463 1464 1465 1466 1467 1468
{
	id->dev_class = SNDRV_TIMER_CLASS_NONE;
	id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	id->card = -1;
	id->device = -1;
	id->subdevice = -1;
}

1469
static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474 1475 1476 1477
{
	id->dev_class = timer->tmr_class;
	id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	id->card = timer->card ? timer->card->number : -1;
	id->device = timer->tmr_device;
	id->subdevice = timer->tmr_subdevice;
}

1478
static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
L
Linus Torvalds 已提交
1479
{
1480 1481
	struct snd_timer_id id;
	struct snd_timer *timer;
L
Linus Torvalds 已提交
1482
	struct list_head *p;
C
Clemens Ladisch 已提交
1483

L
Linus Torvalds 已提交
1484 1485
	if (copy_from_user(&id, _tid, sizeof(id)))
		return -EFAULT;
1486
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
1487 1488 1489 1490
	if (id.dev_class < 0) {		/* first item */
		if (list_empty(&snd_timer_list))
			snd_timer_user_zero_id(&id);
		else {
1491
			timer = list_entry(snd_timer_list.next,
1492
					   struct snd_timer, device_list);
L
Linus Torvalds 已提交
1493 1494 1495 1496 1497 1498 1499
			snd_timer_user_copy_id(&id, timer);
		}
	} else {
		switch (id.dev_class) {
		case SNDRV_TIMER_CLASS_GLOBAL:
			id.device = id.device < 0 ? 0 : id.device + 1;
			list_for_each(p, &snd_timer_list) {
1500
				timer = list_entry(p, struct snd_timer, device_list);
L
Linus Torvalds 已提交
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
				if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->tmr_device >= id.device) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
			}
			if (p == &snd_timer_list)
				snd_timer_user_zero_id(&id);
			break;
		case SNDRV_TIMER_CLASS_CARD:
		case SNDRV_TIMER_CLASS_PCM:
			if (id.card < 0) {
				id.card = 0;
			} else {
1518 1519
				if (id.device < 0) {
					id.device = 0;
L
Linus Torvalds 已提交
1520
				} else {
1521 1522 1523 1524
					if (id.subdevice < 0)
						id.subdevice = 0;
					else
						id.subdevice++;
L
Linus Torvalds 已提交
1525 1526 1527
				}
			}
			list_for_each(p, &snd_timer_list) {
1528
				timer = list_entry(p, struct snd_timer, device_list);
L
Linus Torvalds 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
				if (timer->tmr_class > id.dev_class) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->tmr_class < id.dev_class)
					continue;
				if (timer->card->number > id.card) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->card->number < id.card)
					continue;
				if (timer->tmr_device > id.device) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->tmr_device < id.device)
					continue;
				if (timer->tmr_subdevice > id.subdevice) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->tmr_subdevice < id.subdevice)
					continue;
				snd_timer_user_copy_id(&id, timer);
				break;
			}
			if (p == &snd_timer_list)
				snd_timer_user_zero_id(&id);
			break;
		default:
			snd_timer_user_zero_id(&id);
		}
	}
1563
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1564 1565 1566
	if (copy_to_user(_tid, &id, sizeof(*_tid)))
		return -EFAULT;
	return 0;
C
Clemens Ladisch 已提交
1567
}
L
Linus Torvalds 已提交
1568

C
Clemens Ladisch 已提交
1569
static int snd_timer_user_ginfo(struct file *file,
1570
				struct snd_timer_ginfo __user *_ginfo)
L
Linus Torvalds 已提交
1571
{
1572 1573 1574
	struct snd_timer_ginfo *ginfo;
	struct snd_timer_id tid;
	struct snd_timer *t;
L
Linus Torvalds 已提交
1575 1576 1577
	struct list_head *p;
	int err = 0;

L
Li Zefan 已提交
1578 1579 1580 1581
	ginfo = memdup_user(_ginfo, sizeof(*ginfo));
	if (IS_ERR(ginfo))
		return PTR_ERR(ginfo);

L
Linus Torvalds 已提交
1582 1583 1584
	tid = ginfo->tid;
	memset(ginfo, 0, sizeof(*ginfo));
	ginfo->tid = tid;
1585
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
	t = snd_timer_find(&tid);
	if (t != NULL) {
		ginfo->card = t->card ? t->card->number : -1;
		if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
			ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
		strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
		strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
		ginfo->resolution = t->hw.resolution;
		if (t->hw.resolution_min > 0) {
			ginfo->resolution_min = t->hw.resolution_min;
			ginfo->resolution_max = t->hw.resolution_max;
		}
		list_for_each(p, &t->open_list_head) {
			ginfo->clients++;
		}
	} else {
		err = -ENODEV;
	}
1604
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1605 1606 1607 1608 1609 1610
	if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
		err = -EFAULT;
	kfree(ginfo);
	return err;
}

1611
static int timer_set_gparams(struct snd_timer_gparams *gparams)
L
Linus Torvalds 已提交
1612
{
1613
	struct snd_timer *t;
L
Linus Torvalds 已提交
1614 1615
	int err;

1616
	mutex_lock(&register_mutex);
1617
	t = snd_timer_find(&gparams->tid);
C
Clemens Ladisch 已提交
1618
	if (!t) {
L
Linus Torvalds 已提交
1619
		err = -ENODEV;
C
Clemens Ladisch 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628
		goto _error;
	}
	if (!list_empty(&t->open_list_head)) {
		err = -EBUSY;
		goto _error;
	}
	if (!t->hw.set_period) {
		err = -ENOSYS;
		goto _error;
L
Linus Torvalds 已提交
1629
	}
1630
	err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
C
Clemens Ladisch 已提交
1631
_error:
1632
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1633 1634 1635
	return err;
}

1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
static int snd_timer_user_gparams(struct file *file,
				  struct snd_timer_gparams __user *_gparams)
{
	struct snd_timer_gparams gparams;

	if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
		return -EFAULT;
	return timer_set_gparams(&gparams);
}

C
Clemens Ladisch 已提交
1646
static int snd_timer_user_gstatus(struct file *file,
1647
				  struct snd_timer_gstatus __user *_gstatus)
L
Linus Torvalds 已提交
1648
{
1649 1650 1651
	struct snd_timer_gstatus gstatus;
	struct snd_timer_id tid;
	struct snd_timer *t;
L
Linus Torvalds 已提交
1652 1653 1654 1655 1656 1657 1658
	int err = 0;

	if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
		return -EFAULT;
	tid = gstatus.tid;
	memset(&gstatus, 0, sizeof(gstatus));
	gstatus.tid = tid;
1659
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
1660 1661
	t = snd_timer_find(&tid);
	if (t != NULL) {
1662
		spin_lock_irq(&t->lock);
1663
		gstatus.resolution = snd_timer_hw_resolution(t);
L
Linus Torvalds 已提交
1664
		if (t->hw.precise_resolution) {
C
Clemens Ladisch 已提交
1665 1666
			t->hw.precise_resolution(t, &gstatus.resolution_num,
						 &gstatus.resolution_den);
L
Linus Torvalds 已提交
1667 1668 1669 1670
		} else {
			gstatus.resolution_num = gstatus.resolution;
			gstatus.resolution_den = 1000000000uL;
		}
1671
		spin_unlock_irq(&t->lock);
L
Linus Torvalds 已提交
1672 1673 1674
	} else {
		err = -ENODEV;
	}
1675
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1676 1677 1678 1679 1680
	if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
		err = -EFAULT;
	return err;
}

C
Clemens Ladisch 已提交
1681
static int snd_timer_user_tselect(struct file *file,
1682
				  struct snd_timer_select __user *_tselect)
L
Linus Torvalds 已提交
1683
{
1684 1685
	struct snd_timer_user *tu;
	struct snd_timer_select tselect;
L
Linus Torvalds 已提交
1686
	char str[32];
1687
	int err = 0;
C
Clemens Ladisch 已提交
1688

L
Linus Torvalds 已提交
1689
	tu = file->private_data;
1690
	if (tu->timeri) {
L
Linus Torvalds 已提交
1691
		snd_timer_close(tu->timeri);
1692 1693 1694 1695 1696 1697
		tu->timeri = NULL;
	}
	if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
		err = -EFAULT;
		goto __err;
	}
L
Linus Torvalds 已提交
1698 1699 1700
	sprintf(str, "application %i", current->pid);
	if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
		tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
C
Clemens Ladisch 已提交
1701 1702
	err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
	if (err < 0)
1703
		goto __err;
L
Linus Torvalds 已提交
1704

1705 1706
	tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
	tu->timeri->callback = tu->tread
C
Clemens Ladisch 已提交
1707
			? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1708 1709 1710
	tu->timeri->ccallback = snd_timer_user_ccallback;
	tu->timeri->callback_data = (void *)tu;
	tu->timeri->disconnect = snd_timer_user_disconnect;
1711 1712 1713

      __err:
	return err;
L
Linus Torvalds 已提交
1714 1715
}

C
Clemens Ladisch 已提交
1716
static int snd_timer_user_info(struct file *file,
1717
			       struct snd_timer_info __user *_info)
L
Linus Torvalds 已提交
1718
{
1719 1720 1721
	struct snd_timer_user *tu;
	struct snd_timer_info *info;
	struct snd_timer *t;
L
Linus Torvalds 已提交
1722 1723 1724
	int err = 0;

	tu = file->private_data;
1725 1726
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1727
	t = tu->timeri->timer;
1728 1729
	if (!t)
		return -EBADFD;
L
Linus Torvalds 已提交
1730

1731
	info = kzalloc(sizeof(*info), GFP_KERNEL);
L
Linus Torvalds 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
	if (! info)
		return -ENOMEM;
	info->card = t->card ? t->card->number : -1;
	if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
		info->flags |= SNDRV_TIMER_FLG_SLAVE;
	strlcpy(info->id, t->id, sizeof(info->id));
	strlcpy(info->name, t->name, sizeof(info->name));
	info->resolution = t->hw.resolution;
	if (copy_to_user(_info, info, sizeof(*_info)))
		err = -EFAULT;
	kfree(info);
	return err;
}

C
Clemens Ladisch 已提交
1746
static int snd_timer_user_params(struct file *file,
1747
				 struct snd_timer_params __user *_params)
L
Linus Torvalds 已提交
1748
{
1749 1750 1751
	struct snd_timer_user *tu;
	struct snd_timer_params params;
	struct snd_timer *t;
L
Linus Torvalds 已提交
1752
	int err;
C
Clemens Ladisch 已提交
1753

L
Linus Torvalds 已提交
1754
	tu = file->private_data;
1755 1756
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1757
	t = tu->timeri->timer;
1758 1759
	if (!t)
		return -EBADFD;
L
Linus Torvalds 已提交
1760 1761
	if (copy_from_user(&params, _params, sizeof(params)))
		return -EFAULT;
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
	if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
		u64 resolution;

		if (params.ticks < 1) {
			err = -EINVAL;
			goto _end;
		}

		/* Don't allow resolution less than 1ms */
		resolution = snd_timer_resolution(tu->timeri);
		resolution *= params.ticks;
		if (resolution < 1000000) {
			err = -EINVAL;
			goto _end;
		}
L
Linus Torvalds 已提交
1777
	}
C
Clemens Ladisch 已提交
1778 1779
	if (params.queue_size > 0 &&
	    (params.queue_size < 32 || params.queue_size > 1024)) {
L
Linus Torvalds 已提交
1780 1781 1782 1783 1784 1785 1786 1787 1788
		err = -EINVAL;
		goto _end;
	}
	if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
			      (1<<SNDRV_TIMER_EVENT_TICK)|
			      (1<<SNDRV_TIMER_EVENT_START)|
			      (1<<SNDRV_TIMER_EVENT_STOP)|
			      (1<<SNDRV_TIMER_EVENT_CONTINUE)|
			      (1<<SNDRV_TIMER_EVENT_PAUSE)|
1789 1790
			      (1<<SNDRV_TIMER_EVENT_SUSPEND)|
			      (1<<SNDRV_TIMER_EVENT_RESUME)|
L
Linus Torvalds 已提交
1791 1792 1793
			      (1<<SNDRV_TIMER_EVENT_MSTART)|
			      (1<<SNDRV_TIMER_EVENT_MSTOP)|
			      (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1794 1795
			      (1<<SNDRV_TIMER_EVENT_MPAUSE)|
			      (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1796
			      (1<<SNDRV_TIMER_EVENT_MRESUME))) {
L
Linus Torvalds 已提交
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
		err = -EINVAL;
		goto _end;
	}
	snd_timer_stop(tu->timeri);
	spin_lock_irq(&t->lock);
	tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
			       SNDRV_TIMER_IFLG_EXCLUSIVE|
			       SNDRV_TIMER_IFLG_EARLY_EVENT);
	if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
		tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
	if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
		tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
	if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
		tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
	spin_unlock_irq(&t->lock);
C
Clemens Ladisch 已提交
1812 1813
	if (params.queue_size > 0 &&
	    (unsigned int)tu->queue_size != params.queue_size) {
1814 1815 1816
		err = realloc_user_queue(tu, params.queue_size);
		if (err < 0)
			goto _end;
L
Linus Torvalds 已提交
1817
	}
1818
	spin_lock_irq(&tu->qlock);
L
Linus Torvalds 已提交
1819 1820 1821
	tu->qhead = tu->qtail = tu->qused = 0;
	if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
		if (tu->tread) {
1822
			struct snd_timer_tread tread;
1823
			memset(&tread, 0, sizeof(tread));
L
Linus Torvalds 已提交
1824 1825 1826 1827 1828 1829
			tread.event = SNDRV_TIMER_EVENT_EARLY;
			tread.tstamp.tv_sec = 0;
			tread.tstamp.tv_nsec = 0;
			tread.val = 0;
			snd_timer_user_append_to_tqueue(tu, &tread);
		} else {
1830
			struct snd_timer_read *r = &tu->queue[0];
L
Linus Torvalds 已提交
1831 1832 1833 1834 1835 1836 1837 1838
			r->resolution = 0;
			r->ticks = 0;
			tu->qused++;
			tu->qtail++;
		}
	}
	tu->filter = params.filter;
	tu->ticks = params.ticks;
1839
	spin_unlock_irq(&tu->qlock);
L
Linus Torvalds 已提交
1840 1841 1842 1843 1844 1845 1846
	err = 0;
 _end:
	if (copy_to_user(_params, &params, sizeof(params)))
		return -EFAULT;
	return err;
}

C
Clemens Ladisch 已提交
1847
static int snd_timer_user_status(struct file *file,
1848
				 struct snd_timer_status __user *_status)
L
Linus Torvalds 已提交
1849
{
1850 1851
	struct snd_timer_user *tu;
	struct snd_timer_status status;
C
Clemens Ladisch 已提交
1852

L
Linus Torvalds 已提交
1853
	tu = file->private_data;
1854 1855
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
	memset(&status, 0, sizeof(status));
	status.tstamp = tu->tstamp;
	status.resolution = snd_timer_resolution(tu->timeri);
	status.lost = tu->timeri->lost;
	status.overrun = tu->overrun;
	spin_lock_irq(&tu->qlock);
	status.queue = tu->qused;
	spin_unlock_irq(&tu->qlock);
	if (copy_to_user(_status, &status, sizeof(status)))
		return -EFAULT;
	return 0;
}

static int snd_timer_user_start(struct file *file)
{
	int err;
1872
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1873

L
Linus Torvalds 已提交
1874
	tu = file->private_data;
1875 1876
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1877 1878 1879 1880 1881 1882 1883 1884 1885
	snd_timer_stop(tu->timeri);
	tu->timeri->lost = 0;
	tu->last_resolution = 0;
	return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
}

static int snd_timer_user_stop(struct file *file)
{
	int err;
1886
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1887

L
Linus Torvalds 已提交
1888
	tu = file->private_data;
1889 1890
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1891 1892 1893 1894 1895 1896
	return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
}

static int snd_timer_user_continue(struct file *file)
{
	int err;
1897
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1898

L
Linus Torvalds 已提交
1899
	tu = file->private_data;
1900 1901
	if (!tu->timeri)
		return -EBADFD;
1902 1903 1904
	/* start timer instead of continue if it's not used before */
	if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
		return snd_timer_user_start(file);
L
Linus Torvalds 已提交
1905 1906 1907 1908
	tu->timeri->lost = 0;
	return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
}

T
Takashi Iwai 已提交
1909 1910 1911
static int snd_timer_user_pause(struct file *file)
{
	int err;
1912
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1913

T
Takashi Iwai 已提交
1914
	tu = file->private_data;
1915 1916
	if (!tu->timeri)
		return -EBADFD;
1917
	return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
T
Takashi Iwai 已提交
1918 1919
}

1920 1921 1922 1923 1924 1925 1926
enum {
	SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
	SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
	SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
	SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
};

1927
static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
C
Clemens Ladisch 已提交
1928
				 unsigned long arg)
L
Linus Torvalds 已提交
1929
{
1930
	struct snd_timer_user *tu;
L
Linus Torvalds 已提交
1931 1932
	void __user *argp = (void __user *)arg;
	int __user *p = argp;
C
Clemens Ladisch 已提交
1933

L
Linus Torvalds 已提交
1934 1935 1936 1937 1938 1939 1940 1941
	tu = file->private_data;
	switch (cmd) {
	case SNDRV_TIMER_IOCTL_PVERSION:
		return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
	case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
		return snd_timer_user_next_device(argp);
	case SNDRV_TIMER_IOCTL_TREAD:
	{
1942
		int xarg, old_tread;
C
Clemens Ladisch 已提交
1943

1944
		if (tu->timeri)	/* too late */
L
Linus Torvalds 已提交
1945
			return -EBUSY;
1946
		if (get_user(xarg, p))
L
Linus Torvalds 已提交
1947
			return -EFAULT;
1948
		old_tread = tu->tread;
L
Linus Torvalds 已提交
1949
		tu->tread = xarg ? 1 : 0;
1950 1951 1952 1953 1954
		if (tu->tread != old_tread &&
		    realloc_user_queue(tu, tu->queue_size) < 0) {
			tu->tread = old_tread;
			return -ENOMEM;
		}
L
Linus Torvalds 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
		return 0;
	}
	case SNDRV_TIMER_IOCTL_GINFO:
		return snd_timer_user_ginfo(file, argp);
	case SNDRV_TIMER_IOCTL_GPARAMS:
		return snd_timer_user_gparams(file, argp);
	case SNDRV_TIMER_IOCTL_GSTATUS:
		return snd_timer_user_gstatus(file, argp);
	case SNDRV_TIMER_IOCTL_SELECT:
		return snd_timer_user_tselect(file, argp);
	case SNDRV_TIMER_IOCTL_INFO:
		return snd_timer_user_info(file, argp);
	case SNDRV_TIMER_IOCTL_PARAMS:
		return snd_timer_user_params(file, argp);
	case SNDRV_TIMER_IOCTL_STATUS:
		return snd_timer_user_status(file, argp);
	case SNDRV_TIMER_IOCTL_START:
1972
	case SNDRV_TIMER_IOCTL_START_OLD:
L
Linus Torvalds 已提交
1973 1974
		return snd_timer_user_start(file);
	case SNDRV_TIMER_IOCTL_STOP:
1975
	case SNDRV_TIMER_IOCTL_STOP_OLD:
L
Linus Torvalds 已提交
1976 1977
		return snd_timer_user_stop(file);
	case SNDRV_TIMER_IOCTL_CONTINUE:
1978
	case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
L
Linus Torvalds 已提交
1979
		return snd_timer_user_continue(file);
T
Takashi Iwai 已提交
1980
	case SNDRV_TIMER_IOCTL_PAUSE:
1981
	case SNDRV_TIMER_IOCTL_PAUSE_OLD:
T
Takashi Iwai 已提交
1982
		return snd_timer_user_pause(file);
L
Linus Torvalds 已提交
1983 1984 1985 1986
	}
	return -ENOTTY;
}

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
				 unsigned long arg)
{
	struct snd_timer_user *tu = file->private_data;
	long ret;

	mutex_lock(&tu->ioctl_lock);
	ret = __snd_timer_user_ioctl(file, cmd, arg);
	mutex_unlock(&tu->ioctl_lock);
	return ret;
}

L
Linus Torvalds 已提交
1999 2000
static int snd_timer_user_fasync(int fd, struct file * file, int on)
{
2001
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
2002

L
Linus Torvalds 已提交
2003
	tu = file->private_data;
2004
	return fasync_helper(fd, file, on, &tu->fasync);
L
Linus Torvalds 已提交
2005 2006
}

C
Clemens Ladisch 已提交
2007 2008
static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
				   size_t count, loff_t *offset)
L
Linus Torvalds 已提交
2009
{
2010
	struct snd_timer_user *tu;
L
Linus Torvalds 已提交
2011
	long result = 0, unit;
2012
	int qhead;
L
Linus Torvalds 已提交
2013
	int err = 0;
C
Clemens Ladisch 已提交
2014

L
Linus Torvalds 已提交
2015
	tu = file->private_data;
2016
	unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2017
	mutex_lock(&tu->ioctl_lock);
L
Linus Torvalds 已提交
2018 2019 2020
	spin_lock_irq(&tu->qlock);
	while ((long)count - result >= unit) {
		while (!tu->qused) {
2021
			wait_queue_entry_t wait;
L
Linus Torvalds 已提交
2022 2023 2024

			if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
				err = -EAGAIN;
2025
				goto _error;
L
Linus Torvalds 已提交
2026 2027 2028 2029 2030 2031 2032
			}

			set_current_state(TASK_INTERRUPTIBLE);
			init_waitqueue_entry(&wait, current);
			add_wait_queue(&tu->qchange_sleep, &wait);

			spin_unlock_irq(&tu->qlock);
2033
			mutex_unlock(&tu->ioctl_lock);
L
Linus Torvalds 已提交
2034
			schedule();
2035
			mutex_lock(&tu->ioctl_lock);
L
Linus Torvalds 已提交
2036 2037 2038 2039
			spin_lock_irq(&tu->qlock);

			remove_wait_queue(&tu->qchange_sleep, &wait);

2040 2041
			if (tu->disconnected) {
				err = -ENODEV;
2042
				goto _error;
2043
			}
L
Linus Torvalds 已提交
2044 2045
			if (signal_pending(current)) {
				err = -ERESTARTSYS;
2046
				goto _error;
L
Linus Torvalds 已提交
2047 2048 2049
			}
		}

2050 2051
		qhead = tu->qhead++;
		tu->qhead %= tu->queue_size;
2052
		tu->qused--;
L
Linus Torvalds 已提交
2053 2054 2055
		spin_unlock_irq(&tu->qlock);

		if (tu->tread) {
2056 2057
			if (copy_to_user(buffer, &tu->tqueue[qhead],
					 sizeof(struct snd_timer_tread)))
L
Linus Torvalds 已提交
2058 2059
				err = -EFAULT;
		} else {
2060 2061
			if (copy_to_user(buffer, &tu->queue[qhead],
					 sizeof(struct snd_timer_read)))
L
Linus Torvalds 已提交
2062 2063 2064 2065
				err = -EFAULT;
		}

		spin_lock_irq(&tu->qlock);
2066 2067 2068 2069
		if (err < 0)
			goto _error;
		result += unit;
		buffer += unit;
L
Linus Torvalds 已提交
2070 2071
	}
 _error:
2072
	spin_unlock_irq(&tu->qlock);
2073
	mutex_unlock(&tu->ioctl_lock);
L
Linus Torvalds 已提交
2074 2075 2076
	return result > 0 ? result : err;
}

A
Al Viro 已提交
2077
static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
L
Linus Torvalds 已提交
2078
{
A
Al Viro 已提交
2079
        __poll_t mask;
2080
        struct snd_timer_user *tu;
L
Linus Torvalds 已提交
2081 2082 2083 2084

        tu = file->private_data;

        poll_wait(file, &tu->qchange_sleep, wait);
C
Clemens Ladisch 已提交
2085

L
Linus Torvalds 已提交
2086
	mask = 0;
2087
	spin_lock_irq(&tu->qlock);
L
Linus Torvalds 已提交
2088
	if (tu->qused)
2089
		mask |= EPOLLIN | EPOLLRDNORM;
2090
	if (tu->disconnected)
2091
		mask |= EPOLLERR;
2092
	spin_unlock_irq(&tu->qlock);
L
Linus Torvalds 已提交
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102

	return mask;
}

#ifdef CONFIG_COMPAT
#include "timer_compat.c"
#else
#define snd_timer_user_ioctl_compat	NULL
#endif

2103
static const struct file_operations snd_timer_f_ops =
L
Linus Torvalds 已提交
2104 2105 2106 2107 2108
{
	.owner =	THIS_MODULE,
	.read =		snd_timer_user_read,
	.open =		snd_timer_user_open,
	.release =	snd_timer_user_release,
T
Takashi Iwai 已提交
2109
	.llseek =	no_llseek,
L
Linus Torvalds 已提交
2110 2111 2112 2113 2114 2115
	.poll =		snd_timer_user_poll,
	.unlocked_ioctl =	snd_timer_user_ioctl,
	.compat_ioctl =	snd_timer_user_ioctl_compat,
	.fasync = 	snd_timer_user_fasync,
};

2116 2117 2118 2119 2120 2121 2122 2123 2124
/* unregister the system timer */
static void snd_timer_free_all(void)
{
	struct snd_timer *timer, *n;

	list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
		snd_timer_free(timer);
}

2125 2126
static struct device timer_dev;

L
Linus Torvalds 已提交
2127 2128 2129 2130 2131 2132 2133 2134
/*
 *  ENTRY functions
 */

static int __init alsa_timer_init(void)
{
	int err;

2135 2136 2137
	snd_device_initialize(&timer_dev, NULL);
	dev_set_name(&timer_dev, "timer");

L
Linus Torvalds 已提交
2138
#ifdef SNDRV_OSS_INFO_DEV_TIMERS
C
Clemens Ladisch 已提交
2139 2140
	snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
			      "system timer");
L
Linus Torvalds 已提交
2141
#endif
2142

2143 2144
	err = snd_timer_register_system();
	if (err < 0) {
2145
		pr_err("ALSA: unable to register system timer (%i)\n", err);
2146
		goto put_timer;
2147 2148
	}

2149 2150
	err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
				  &snd_timer_f_ops, NULL, &timer_dev);
2151
	if (err < 0) {
2152
		pr_err("ALSA: unable to register timer device (%i)\n", err);
2153
		snd_timer_free_all();
2154
		goto put_timer;
2155 2156
	}

2157
	snd_timer_proc_init();
L
Linus Torvalds 已提交
2158
	return 0;
2159 2160 2161 2162

put_timer:
	put_device(&timer_dev);
	return err;
L
Linus Torvalds 已提交
2163 2164 2165 2166
}

static void __exit alsa_timer_exit(void)
{
2167
	snd_unregister_device(&timer_dev);
2168
	snd_timer_free_all();
2169
	put_device(&timer_dev);
2170
	snd_timer_proc_done();
L
Linus Torvalds 已提交
2171 2172 2173 2174 2175 2176 2177
#ifdef SNDRV_OSS_INFO_DEV_TIMERS
	snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
#endif
}

module_init(alsa_timer_init)
module_exit(alsa_timer_exit)