blob: 4fcc8549e4a646beffa1c33ab4545516c0c35a99 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/smp_lock.h>
26#include <linux/slab.h>
27#include <linux/time.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/moduleparam.h>
Paulo Marques543537b2005-06-23 00:09:02 -070030#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/timer.h>
33#include <sound/control.h>
34#include <sound/info.h>
35#include <sound/minors.h>
36#include <sound/initval.h>
37#include <linux/kmod.h>
38#ifdef CONFIG_KERNELD
39#include <linux/kerneld.h>
40#endif
41
42#if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
43#define DEFAULT_TIMER_LIMIT 3
44#elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
45#define DEFAULT_TIMER_LIMIT 2
46#else
47#define DEFAULT_TIMER_LIMIT 1
48#endif
49
50static int timer_limit = DEFAULT_TIMER_LIMIT;
51MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
52MODULE_DESCRIPTION("ALSA timer interface");
53MODULE_LICENSE("GPL");
54module_param(timer_limit, int, 0444);
55MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
56
Takashi Iwai53d2f742005-11-17 13:56:05 +010057struct snd_timer_user {
58 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020059 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned long ticks;
61 unsigned long overrun;
62 int qhead;
63 int qtail;
64 int qused;
65 int queue_size;
Takashi Iwai53d2f742005-11-17 13:56:05 +010066 struct snd_timer_read *queue;
67 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 spinlock_t qlock;
69 unsigned long last_resolution;
70 unsigned int filter;
71 struct timespec tstamp; /* trigger tstamp */
72 wait_queue_head_t qchange_sleep;
73 struct fasync_struct *fasync;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010074 struct mutex tread_sem;
Takashi Iwai53d2f742005-11-17 13:56:05 +010075};
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/* list of timers */
78static LIST_HEAD(snd_timer_list);
79
80/* list of slave instances */
81static LIST_HEAD(snd_timer_slave_list);
82
83/* lock for slave active lists */
84static DEFINE_SPINLOCK(slave_active_lock);
85
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010086static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Takashi Iwai53d2f742005-11-17 13:56:05 +010088static int snd_timer_free(struct snd_timer *timer);
89static int snd_timer_dev_free(struct snd_device *device);
90static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020091static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Takashi Iwai53d2f742005-11-17 13:56:05 +010093static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * create a timer instance with the given owner string.
97 * when timer is not NULL, increments the module counter
98 */
Takashi Iwai53d2f742005-11-17 13:56:05 +010099static struct snd_timer_instance *snd_timer_instance_new(char *owner,
100 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100102 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200103 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (timeri == NULL)
105 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700106 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (! timeri->owner) {
108 kfree(timeri);
109 return NULL;
110 }
111 INIT_LIST_HEAD(&timeri->open_list);
112 INIT_LIST_HEAD(&timeri->active_list);
113 INIT_LIST_HEAD(&timeri->ack_list);
114 INIT_LIST_HEAD(&timeri->slave_list_head);
115 INIT_LIST_HEAD(&timeri->slave_active_head);
116
117 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200118 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 kfree(timeri->owner);
120 kfree(timeri);
121 return NULL;
122 }
123
124 return timeri;
125}
126
127/*
128 * find a timer instance from the given timer id
129 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100130static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100132 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct list_head *p;
134
135 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100136 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 if (timer->tmr_class != tid->dev_class)
139 continue;
140 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
141 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
142 (timer->card == NULL ||
143 timer->card->number != tid->card))
144 continue;
145 if (timer->tmr_device != tid->device)
146 continue;
147 if (timer->tmr_subdevice != tid->subdevice)
148 continue;
149 return timer;
150 }
151 return NULL;
152}
153
154#ifdef CONFIG_KMOD
155
Takashi Iwai53d2f742005-11-17 13:56:05 +0100156static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 if (! current->fs->root)
159 return;
160 switch (tid->dev_class) {
161 case SNDRV_TIMER_CLASS_GLOBAL:
162 if (tid->device < timer_limit)
163 request_module("snd-timer-%i", tid->device);
164 break;
165 case SNDRV_TIMER_CLASS_CARD:
166 case SNDRV_TIMER_CLASS_PCM:
167 if (tid->card < snd_ecards_limit)
168 request_module("snd-card-%i", tid->card);
169 break;
170 default:
171 break;
172 }
173}
174
175#endif
176
177/*
178 * look for a master instance matching with the slave id of the given slave.
179 * when found, relink the open_link of the slave.
180 *
181 * call this with register_mutex down.
182 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100183static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100185 struct snd_timer *timer;
186 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct list_head *p, *q;
188
189 /* FIXME: it's really dumb to look up all entries.. */
190 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100191 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 list_for_each(q, &timer->open_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100193 master = list_entry(q, struct snd_timer_instance, open_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (slave->slave_class == master->slave_class &&
195 slave->slave_id == master->slave_id) {
196 list_del(&slave->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200197 list_add_tail(&slave->open_list,
198 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_lock_irq(&slave_active_lock);
200 slave->master = master;
201 slave->timer = master->timer;
202 spin_unlock_irq(&slave_active_lock);
203 return;
204 }
205 }
206 }
207}
208
209/*
210 * look for slave instances matching with the slave id of the given master.
211 * when found, relink the open_link of slaves.
212 *
213 * call this with register_mutex down.
214 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100215static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100217 struct snd_timer_instance *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct list_head *p, *n;
219
220 /* check all pending slaves */
221 list_for_each_safe(p, n, &snd_timer_slave_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100222 slave = list_entry(p, struct snd_timer_instance, open_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (slave->slave_class == master->slave_class &&
224 slave->slave_id == master->slave_id) {
225 list_del(p);
226 list_add_tail(p, &master->slave_list_head);
227 spin_lock_irq(&slave_active_lock);
228 slave->master = master;
229 slave->timer = master->timer;
230 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200231 list_add_tail(&slave->active_list,
232 &master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 spin_unlock_irq(&slave_active_lock);
234 }
235 }
236}
237
238/*
239 * open a timer instance
240 * when opening a master, the slave id must be here given.
241 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100242int snd_timer_open(struct snd_timer_instance **ti,
243 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned int slave_id)
245{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100246 struct snd_timer *timer;
247 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
250 /* open a slave instance */
251 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
252 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
253 snd_printd("invalid slave class %i\n", tid->dev_sclass);
254 return -EINVAL;
255 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100256 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200258 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100259 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200260 return -ENOMEM;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 timeri->slave_class = tid->dev_sclass;
263 timeri->slave_id = tid->device;
264 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
265 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
266 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100267 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *ti = timeri;
269 return 0;
270 }
271
272 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100273 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 timer = snd_timer_find(tid);
275#ifdef CONFIG_KMOD
276 if (timer == NULL) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100277 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100279 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 timer = snd_timer_find(tid);
281 }
282#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200283 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100284 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -ENODEV;
286 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200287 if (!list_empty(&timer->open_list_head)) {
288 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100289 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200290 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100291 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200292 return -EBUSY;
293 }
294 }
295 timeri = snd_timer_instance_new(owner, timer);
296 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100297 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200298 return -ENOMEM;
299 }
300 timeri->slave_class = tid->dev_sclass;
301 timeri->slave_id = slave_id;
302 if (list_empty(&timer->open_list_head) && timer->hw.open)
303 timer->hw.open(timer);
304 list_add_tail(&timeri->open_list, &timer->open_list_head);
305 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100306 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 *ti = timeri;
308 return 0;
309}
310
Takashi Iwai53d2f742005-11-17 13:56:05 +0100311static int _snd_timer_stop(struct snd_timer_instance *timeri,
312 int keep_flag, int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314/*
315 * close a timer instance
316 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100317int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100319 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct list_head *p, *n;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100321 struct snd_timer_instance *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 snd_assert(timeri != NULL, return -ENXIO);
324
325 /* force to stop the timer */
326 snd_timer_stop(timeri);
327
328 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
329 /* wait, until the active callback is finished */
330 spin_lock_irq(&slave_active_lock);
331 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
332 spin_unlock_irq(&slave_active_lock);
333 udelay(10);
334 spin_lock_irq(&slave_active_lock);
335 }
336 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100337 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 list_del(&timeri->open_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100339 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 } else {
341 timer = timeri->timer;
342 /* wait, until the active callback is finished */
343 spin_lock_irq(&timer->lock);
344 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
345 spin_unlock_irq(&timer->lock);
346 udelay(10);
347 spin_lock_irq(&timer->lock);
348 }
349 spin_unlock_irq(&timer->lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100350 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 list_del(&timeri->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200352 if (timer && list_empty(&timer->open_list_head) &&
353 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 timer->hw.close(timer);
355 /* remove slave links */
356 list_for_each_safe(p, n, &timeri->slave_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100357 slave = list_entry(p, struct snd_timer_instance, open_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 spin_lock_irq(&slave_active_lock);
359 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
360 list_del(p);
361 list_add_tail(p, &snd_timer_slave_list);
362 slave->master = NULL;
363 slave->timer = NULL;
364 spin_unlock_irq(&slave_active_lock);
365 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100366 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 if (timeri->private_free)
369 timeri->private_free(timeri);
370 kfree(timeri->owner);
371 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200372 if (timer)
373 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
Takashi Iwai53d2f742005-11-17 13:56:05 +0100377unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100379 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (timeri == NULL)
382 return 0;
383 if ((timer = timeri->timer) != NULL) {
384 if (timer->hw.c_resolution)
385 return timer->hw.c_resolution(timer);
386 return timer->hw.resolution;
387 }
388 return 0;
389}
390
Takashi Iwai53d2f742005-11-17 13:56:05 +0100391static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100393 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned long flags;
395 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100396 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct list_head *n;
398 struct timespec tstamp;
399
Takashi Iwai07799e72005-10-10 11:49:49 +0200400 getnstimeofday(&tstamp);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200401 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
402 event <= SNDRV_TIMER_EVENT_PAUSE, return);
403 if (event == SNDRV_TIMER_EVENT_START ||
404 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 resolution = snd_timer_resolution(ti);
406 if (ti->ccallback)
407 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
408 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
409 return;
410 timer = ti->timer;
411 if (timer == NULL)
412 return;
413 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
414 return;
415 spin_lock_irqsave(&timer->lock, flags);
416 list_for_each(n, &ti->slave_active_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100417 ts = list_entry(n, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (ts->ccallback)
419 ts->ccallback(ti, event + 100, &tstamp, resolution);
420 }
421 spin_unlock_irqrestore(&timer->lock, flags);
422}
423
Takashi Iwai53d2f742005-11-17 13:56:05 +0100424static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200425 unsigned long sticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 list_del(&timeri->active_list);
428 list_add_tail(&timeri->active_list, &timer->active_list_head);
429 if (timer->running) {
430 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
431 goto __start_now;
432 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
433 timeri->flags |= SNDRV_TIMER_IFLG_START;
434 return 1; /* delayed start */
435 } else {
436 timer->sticks = sticks;
437 timer->hw.start(timer);
438 __start_now:
439 timer->running++;
440 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
441 return 0;
442 }
443}
444
Takashi Iwai53d2f742005-11-17 13:56:05 +0100445static int snd_timer_start_slave(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 unsigned long flags;
448
449 spin_lock_irqsave(&slave_active_lock, flags);
450 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
451 if (timeri->master)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200452 list_add_tail(&timeri->active_list,
453 &timeri->master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 spin_unlock_irqrestore(&slave_active_lock, flags);
455 return 1; /* delayed start */
456}
457
458/*
459 * start the timer instance
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200460 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100461int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100463 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int result = -EINVAL;
465 unsigned long flags;
466
467 if (timeri == NULL || ticks < 1)
468 return -EINVAL;
469 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
470 result = snd_timer_start_slave(timeri);
471 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
472 return result;
473 }
474 timer = timeri->timer;
475 if (timer == NULL)
476 return -EINVAL;
477 spin_lock_irqsave(&timer->lock, flags);
478 timeri->ticks = timeri->cticks = ticks;
479 timeri->pticks = 0;
480 result = snd_timer_start1(timer, timeri, ticks);
481 spin_unlock_irqrestore(&timer->lock, flags);
482 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
483 return result;
484}
485
Takashi Iwai53d2f742005-11-17 13:56:05 +0100486static int _snd_timer_stop(struct snd_timer_instance * timeri,
487 int keep_flag, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100489 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 unsigned long flags;
491
492 snd_assert(timeri != NULL, return -ENXIO);
493
494 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
495 if (!keep_flag) {
496 spin_lock_irqsave(&slave_active_lock, flags);
497 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
498 spin_unlock_irqrestore(&slave_active_lock, flags);
499 }
500 goto __end;
501 }
502 timer = timeri->timer;
503 if (!timer)
504 return -EINVAL;
505 spin_lock_irqsave(&timer->lock, flags);
506 list_del_init(&timeri->ack_list);
507 list_del_init(&timeri->active_list);
508 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
509 !(--timer->running)) {
510 timer->hw.stop(timer);
511 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
512 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
513 snd_timer_reschedule(timer, 0);
514 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
515 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
516 timer->hw.start(timer);
517 }
518 }
519 }
520 if (!keep_flag)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200521 timeri->flags &=
522 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 spin_unlock_irqrestore(&timer->lock, flags);
524 __end:
525 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
526 snd_timer_notify1(timeri, event);
527 return 0;
528}
529
530/*
531 * stop the timer instance.
532 *
533 * do not call this from the timer callback!
534 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100535int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100537 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 unsigned long flags;
539 int err;
540
541 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
542 if (err < 0)
543 return err;
544 timer = timeri->timer;
545 spin_lock_irqsave(&timer->lock, flags);
546 timeri->cticks = timeri->ticks;
547 timeri->pticks = 0;
548 spin_unlock_irqrestore(&timer->lock, flags);
549 return 0;
550}
551
552/*
553 * start again.. the tick is kept.
554 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100555int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100557 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int result = -EINVAL;
559 unsigned long flags;
560
561 if (timeri == NULL)
562 return result;
563 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
564 return snd_timer_start_slave(timeri);
565 timer = timeri->timer;
566 if (! timer)
567 return -EINVAL;
568 spin_lock_irqsave(&timer->lock, flags);
569 if (!timeri->cticks)
570 timeri->cticks = 1;
571 timeri->pticks = 0;
572 result = snd_timer_start1(timer, timeri, timer->sticks);
573 spin_unlock_irqrestore(&timer->lock, flags);
574 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
575 return result;
576}
577
578/*
579 * pause.. remember the ticks left
580 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100581int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
584}
585
586/*
587 * reschedule the timer
588 *
589 * start pending instances and check the scheduling ticks.
590 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
591 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100592static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100594 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 unsigned long ticks = ~0UL;
596 struct list_head *p;
597
598 list_for_each(p, &timer->active_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100599 ti = list_entry(p, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (ti->flags & SNDRV_TIMER_IFLG_START) {
601 ti->flags &= ~SNDRV_TIMER_IFLG_START;
602 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
603 timer->running++;
604 }
605 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
606 if (ticks > ti->cticks)
607 ticks = ti->cticks;
608 }
609 }
610 if (ticks == ~0UL) {
611 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
612 return;
613 }
614 if (ticks > timer->hw.ticks)
615 ticks = timer->hw.ticks;
616 if (ticks_left != ticks)
617 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
618 timer->sticks = ticks;
619}
620
621/*
622 * timer tasklet
623 *
624 */
625static void snd_timer_tasklet(unsigned long arg)
626{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100627 struct snd_timer *timer = (struct snd_timer *) arg;
628 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct list_head *p;
630 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200631 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Takashi Iwai2999ff52006-07-05 17:16:58 +0200633 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* now process all callbacks */
635 while (!list_empty(&timer->sack_list_head)) {
636 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100637 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* remove from ack_list and make empty */
640 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ticks = ti->pticks;
643 ti->pticks = 0;
644 resolution = ti->resolution;
645
646 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
647 spin_unlock(&timer->lock);
648 if (ti->callback)
649 ti->callback(ti, resolution, ticks);
650 spin_lock(&timer->lock);
651 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
652 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200653 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/*
657 * timer interrupt
658 *
659 * ticks_left is usually equal to timer->sticks.
660 *
661 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100662void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100664 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unsigned long resolution, ticks;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200666 struct list_head *p, *q, *n, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100667 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int use_tasklet = 0;
669
670 if (timer == NULL)
671 return;
672
Takashi Iwaib32425a2005-11-18 18:52:14 +0100673 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 /* remember the current resolution */
676 if (timer->hw.c_resolution)
677 resolution = timer->hw.c_resolution(timer);
678 else
679 resolution = timer->hw.resolution;
680
681 /* loop for all active instances
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200682 * Here we cannot use list_for_each because the active_list of a
683 * processed instance is relinked to done_list_head before the callback
684 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
686 list_for_each_safe(p, n, &timer->active_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100687 ti = list_entry(p, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
689 continue;
690 ti->pticks += ticks_left;
691 ti->resolution = resolution;
692 if (ti->cticks < ticks_left)
693 ti->cticks = 0;
694 else
695 ti->cticks -= ticks_left;
696 if (ti->cticks) /* not expired */
697 continue;
698 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
699 ti->cticks = ti->ticks;
700 } else {
701 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
702 if (--timer->running)
703 list_del(p);
704 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200705 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
706 (ti->flags & SNDRV_TIMER_IFLG_FAST))
707 ack_list_head = &timer->ack_list_head;
708 else
709 ack_list_head = &timer->sack_list_head;
710 if (list_empty(&ti->ack_list))
711 list_add_tail(&ti->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 list_for_each(q, &ti->slave_active_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100713 ts = list_entry(q, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ts->pticks = ti->pticks;
715 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200716 if (list_empty(&ts->ack_list))
717 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 }
720 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
721 snd_timer_reschedule(timer, ticks_left);
722 if (timer->running) {
723 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
724 timer->hw.stop(timer);
725 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
726 }
727 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
728 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
729 /* restart timer */
730 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
731 timer->hw.start(timer);
732 }
733 } else {
734 timer->hw.stop(timer);
735 }
736
737 /* now process all fast callbacks */
738 while (!list_empty(&timer->ack_list_head)) {
739 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100740 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* remove from ack_list and make empty */
743 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 ticks = ti->pticks;
746 ti->pticks = 0;
747
748 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
749 spin_unlock(&timer->lock);
750 if (ti->callback)
751 ti->callback(ti, resolution, ticks);
752 spin_lock(&timer->lock);
753 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
754 }
755
756 /* do we have any slow callbacks? */
757 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100758 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (use_tasklet)
761 tasklet_hi_schedule(&timer->task_queue);
762}
763
764/*
765
766 */
767
Takashi Iwai53d2f742005-11-17 13:56:05 +0100768int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
769 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100771 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100773 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 .dev_free = snd_timer_dev_free,
775 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200776 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 };
778
779 snd_assert(tid != NULL, return -EINVAL);
780 snd_assert(rtimer != NULL, return -EINVAL);
781 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200782 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100783 if (timer == NULL) {
784 snd_printk(KERN_ERR "timer: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 timer->tmr_class = tid->dev_class;
788 timer->card = card;
789 timer->tmr_device = tid->device;
790 timer->tmr_subdevice = tid->subdevice;
791 if (id)
792 strlcpy(timer->id, id, sizeof(timer->id));
793 INIT_LIST_HEAD(&timer->device_list);
794 INIT_LIST_HEAD(&timer->open_list_head);
795 INIT_LIST_HEAD(&timer->active_list_head);
796 INIT_LIST_HEAD(&timer->ack_list_head);
797 INIT_LIST_HEAD(&timer->sack_list_head);
798 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200799 tasklet_init(&timer->task_queue, snd_timer_tasklet,
800 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200802 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200803 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
804 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 snd_timer_free(timer);
806 return err;
807 }
808 }
809 *rtimer = timer;
810 return 0;
811}
812
Takashi Iwai53d2f742005-11-17 13:56:05 +0100813static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 snd_assert(timer != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +0200816
817 mutex_lock(&register_mutex);
818 if (! list_empty(&timer->open_list_head)) {
819 struct list_head *p, *n;
820 struct snd_timer_instance *ti;
821 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
822 list_for_each_safe(p, n, &timer->open_list_head) {
823 list_del_init(p);
824 ti = list_entry(p, struct snd_timer_instance, open_list);
825 ti->timer = NULL;
826 }
827 }
828 list_del(&timer->device_list);
829 mutex_unlock(&register_mutex);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (timer->private_free)
832 timer->private_free(timer);
833 kfree(timer);
834 return 0;
835}
836
Takashi Iwai53d2f742005-11-17 13:56:05 +0100837static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100839 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return snd_timer_free(timer);
841}
842
Takashi Iwai53d2f742005-11-17 13:56:05 +0100843static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100845 struct snd_timer *timer = dev->device_data;
846 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct list_head *p;
848
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200849 snd_assert(timer != NULL && timer->hw.start != NULL &&
850 timer->hw.stop != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
852 !timer->hw.resolution && timer->hw.c_resolution == NULL)
853 return -EINVAL;
854
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100855 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100857 timer1 = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (timer1->tmr_class > timer->tmr_class)
859 break;
860 if (timer1->tmr_class < timer->tmr_class)
861 continue;
862 if (timer1->card && timer->card) {
863 if (timer1->card->number > timer->card->number)
864 break;
865 if (timer1->card->number < timer->card->number)
866 continue;
867 }
868 if (timer1->tmr_device > timer->tmr_device)
869 break;
870 if (timer1->tmr_device < timer->tmr_device)
871 continue;
872 if (timer1->tmr_subdevice > timer->tmr_subdevice)
873 break;
874 if (timer1->tmr_subdevice < timer->tmr_subdevice)
875 continue;
876 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100877 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return -EBUSY;
879 }
880 list_add_tail(&timer->device_list, p);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100881 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return 0;
883}
884
Takashi Iwaic4614822006-06-23 14:38:23 +0200885static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100887 struct snd_timer *timer = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +0200888 mutex_lock(&register_mutex);
889 list_del_init(&timer->device_list);
890 mutex_unlock(&register_mutex);
891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Takashi Iwai53d2f742005-11-17 13:56:05 +0100894void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 unsigned long flags;
897 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100898 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct list_head *p, *n;
900
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200901 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
902 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200903 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
904 event <= SNDRV_TIMER_EVENT_MRESUME, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200906 if (event == SNDRV_TIMER_EVENT_MSTART ||
907 event == SNDRV_TIMER_EVENT_MCONTINUE ||
908 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (timer->hw.c_resolution)
910 resolution = timer->hw.c_resolution(timer);
911 else
912 resolution = timer->hw.resolution;
913 }
914 list_for_each(p, &timer->active_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100915 ti = list_entry(p, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (ti->ccallback)
917 ti->ccallback(ti, event, tstamp, resolution);
918 list_for_each(n, &ti->slave_active_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +0100919 ts = list_entry(n, struct snd_timer_instance, active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (ts->ccallback)
921 ts->ccallback(ts, event, tstamp, resolution);
922 }
923 }
924 spin_unlock_irqrestore(&timer->lock, flags);
925}
926
927/*
928 * exported functions for global timers
929 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100930int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100932 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
935 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
936 tid.card = -1;
937 tid.device = device;
938 tid.subdevice = 0;
939 return snd_timer_new(NULL, id, &tid, rtimer);
940}
941
Takashi Iwai53d2f742005-11-17 13:56:05 +0100942int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 return snd_timer_free(timer);
945}
946
Takashi Iwai53d2f742005-11-17 13:56:05 +0100947int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100949 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 memset(&dev, 0, sizeof(dev));
952 dev.device_data = timer;
953 return snd_timer_dev_register(&dev);
954}
955
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200956/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * System timer
958 */
959
960struct snd_timer_system_private {
961 struct timer_list tlist;
962 struct timer * timer;
963 unsigned long last_expires;
964 unsigned long last_jiffies;
965 unsigned long correction;
966};
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968static void snd_timer_s_function(unsigned long data)
969{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100970 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 struct snd_timer_system_private *priv = timer->private_data;
972 unsigned long jiff = jiffies;
973 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +0200974 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
976}
977
Takashi Iwai53d2f742005-11-17 13:56:05 +0100978static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 struct snd_timer_system_private *priv;
981 unsigned long njiff;
982
983 priv = (struct snd_timer_system_private *) timer->private_data;
984 njiff = (priv->last_jiffies = jiffies);
985 if (priv->correction > timer->sticks - 1) {
986 priv->correction -= timer->sticks - 1;
987 njiff++;
988 } else {
989 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +0200990 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 priv->last_expires = priv->tlist.expires = njiff;
993 add_timer(&priv->tlist);
994 return 0;
995}
996
Takashi Iwai53d2f742005-11-17 13:56:05 +0100997static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct snd_timer_system_private *priv;
1000 unsigned long jiff;
1001
1002 priv = (struct snd_timer_system_private *) timer->private_data;
1003 del_timer(&priv->tlist);
1004 jiff = jiffies;
1005 if (time_before(jiff, priv->last_expires))
1006 timer->sticks = priv->last_expires - jiff;
1007 else
1008 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001009 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return 0;
1011}
1012
Takashi Iwai53d2f742005-11-17 13:56:05 +01001013static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1016 .resolution = 1000000000L / HZ,
1017 .ticks = 10000000L,
1018 .start = snd_timer_s_start,
1019 .stop = snd_timer_s_stop
1020};
1021
Takashi Iwai53d2f742005-11-17 13:56:05 +01001022static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 kfree(timer->private_data);
1025}
1026
1027static int snd_timer_register_system(void)
1028{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001029 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct snd_timer_system_private *priv;
1031 int err;
1032
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001033 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1034 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return err;
1036 strcpy(timer->name, "system timer");
1037 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001038 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (priv == NULL) {
1040 snd_timer_free(timer);
1041 return -ENOMEM;
1042 }
1043 init_timer(&priv->tlist);
1044 priv->tlist.function = snd_timer_s_function;
1045 priv->tlist.data = (unsigned long) timer;
1046 timer->private_data = priv;
1047 timer->private_free = snd_timer_free_system;
1048 return snd_timer_global_register(timer);
1049}
1050
Takashi Iwaie28563c2005-12-01 10:42:42 +01001051#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * Info interface
1054 */
1055
Takashi Iwai53d2f742005-11-17 13:56:05 +01001056static void snd_timer_proc_read(struct snd_info_entry *entry,
1057 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001059 struct snd_timer *timer;
1060 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct list_head *p, *q;
1062
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001063 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001065 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 switch (timer->tmr_class) {
1067 case SNDRV_TIMER_CLASS_GLOBAL:
1068 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1069 break;
1070 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001071 snd_iprintf(buffer, "C%i-%i: ",
1072 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 break;
1074 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001075 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1076 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 break;
1078 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001079 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1080 timer->card ? timer->card->number : -1,
1081 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 snd_iprintf(buffer, "%s :", timer->name);
1084 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001085 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1086 timer->hw.resolution / 1000,
1087 timer->hw.resolution % 1000,
1088 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1090 snd_iprintf(buffer, " SLAVE");
1091 snd_iprintf(buffer, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 list_for_each(q, &timer->open_list_head) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001093 ti = list_entry(q, struct snd_timer_instance, open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001094 snd_iprintf(buffer, " Client %s : %s\n",
1095 ti->owner ? ti->owner : "unknown",
1096 ti->flags & (SNDRV_TIMER_IFLG_START |
1097 SNDRV_TIMER_IFLG_RUNNING)
1098 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001101 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001104static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001105
1106static void __init snd_timer_proc_init(void)
1107{
1108 struct snd_info_entry *entry;
1109
1110 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1111 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001112 entry->c.text.read = snd_timer_proc_read;
1113 if (snd_info_register(entry) < 0) {
1114 snd_info_free_entry(entry);
1115 entry = NULL;
1116 }
1117 }
1118 snd_timer_proc_entry = entry;
1119}
1120
1121static void __exit snd_timer_proc_done(void)
1122{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001123 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001124}
1125#else /* !CONFIG_PROC_FS */
1126#define snd_timer_proc_init()
1127#define snd_timer_proc_done()
1128#endif
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * USER SPACE interface
1132 */
1133
Takashi Iwai53d2f742005-11-17 13:56:05 +01001134static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 unsigned long resolution,
1136 unsigned long ticks)
1137{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001138 struct snd_timer_user *tu = timeri->callback_data;
1139 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 spin_lock(&tu->qlock);
1143 if (tu->qused > 0) {
1144 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1145 r = &tu->queue[prev];
1146 if (r->resolution == resolution) {
1147 r->ticks += ticks;
1148 goto __wake;
1149 }
1150 }
1151 if (tu->qused >= tu->queue_size) {
1152 tu->overrun++;
1153 } else {
1154 r = &tu->queue[tu->qtail++];
1155 tu->qtail %= tu->queue_size;
1156 r->resolution = resolution;
1157 r->ticks = ticks;
1158 tu->qused++;
1159 }
1160 __wake:
1161 spin_unlock(&tu->qlock);
1162 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1163 wake_up(&tu->qchange_sleep);
1164}
1165
Takashi Iwai53d2f742005-11-17 13:56:05 +01001166static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1167 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 if (tu->qused >= tu->queue_size) {
1170 tu->overrun++;
1171 } else {
1172 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1173 tu->qtail %= tu->queue_size;
1174 tu->qused++;
1175 }
1176}
1177
Takashi Iwai53d2f742005-11-17 13:56:05 +01001178static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1179 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct timespec *tstamp,
1181 unsigned long resolution)
1182{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001183 struct snd_timer_user *tu = timeri->callback_data;
1184 struct snd_timer_tread r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001186 if (event >= SNDRV_TIMER_EVENT_START &&
1187 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 tu->tstamp = *tstamp;
1189 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1190 return;
1191 r1.event = event;
1192 r1.tstamp = *tstamp;
1193 r1.val = resolution;
1194 spin_lock(&tu->qlock);
1195 snd_timer_user_append_to_tqueue(tu, &r1);
1196 spin_unlock(&tu->qlock);
1197 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1198 wake_up(&tu->qchange_sleep);
1199}
1200
Takashi Iwai53d2f742005-11-17 13:56:05 +01001201static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 unsigned long resolution,
1203 unsigned long ticks)
1204{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001205 struct snd_timer_user *tu = timeri->callback_data;
1206 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 struct timespec tstamp;
1208 int prev, append = 0;
1209
Takashi Iwai07799e72005-10-10 11:49:49 +02001210 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001212 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1213 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 spin_unlock(&tu->qlock);
1215 return;
1216 }
1217 if (tu->last_resolution != resolution || ticks > 0)
Takashi Iwai07799e72005-10-10 11:49:49 +02001218 getnstimeofday(&tstamp);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001219 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1220 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1222 r1.tstamp = tstamp;
1223 r1.val = resolution;
1224 snd_timer_user_append_to_tqueue(tu, &r1);
1225 tu->last_resolution = resolution;
1226 append++;
1227 }
1228 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1229 goto __wake;
1230 if (ticks == 0)
1231 goto __wake;
1232 if (tu->qused > 0) {
1233 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1234 r = &tu->tqueue[prev];
1235 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1236 r->tstamp = tstamp;
1237 r->val += ticks;
1238 append++;
1239 goto __wake;
1240 }
1241 }
1242 r1.event = SNDRV_TIMER_EVENT_TICK;
1243 r1.tstamp = tstamp;
1244 r1.val = ticks;
1245 snd_timer_user_append_to_tqueue(tu, &r1);
1246 append++;
1247 __wake:
1248 spin_unlock(&tu->qlock);
1249 if (append == 0)
1250 return;
1251 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1252 wake_up(&tu->qchange_sleep);
1253}
1254
1255static int snd_timer_user_open(struct inode *inode, struct file *file)
1256{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001257 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001258
Takashi Iwaica2c0962005-09-09 14:20:23 +02001259 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (tu == NULL)
1261 return -ENOMEM;
1262 spin_lock_init(&tu->qlock);
1263 init_waitqueue_head(&tu->qchange_sleep);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001264 mutex_init(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 tu->ticks = 1;
1266 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001267 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001268 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (tu->queue == NULL) {
1270 kfree(tu);
1271 return -ENOMEM;
1272 }
1273 file->private_data = tu;
1274 return 0;
1275}
1276
1277static int snd_timer_user_release(struct inode *inode, struct file *file)
1278{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001279 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (file->private_data) {
1282 tu = file->private_data;
1283 file->private_data = NULL;
1284 fasync_helper(-1, file, 0, &tu->fasync);
1285 if (tu->timeri)
1286 snd_timer_close(tu->timeri);
1287 kfree(tu->queue);
1288 kfree(tu->tqueue);
1289 kfree(tu);
1290 }
1291 return 0;
1292}
1293
Takashi Iwai53d2f742005-11-17 13:56:05 +01001294static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1297 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1298 id->card = -1;
1299 id->device = -1;
1300 id->subdevice = -1;
1301}
1302
Takashi Iwai53d2f742005-11-17 13:56:05 +01001303static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 id->dev_class = timer->tmr_class;
1306 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1307 id->card = timer->card ? timer->card->number : -1;
1308 id->device = timer->tmr_device;
1309 id->subdevice = timer->tmr_subdevice;
1310}
1311
Takashi Iwai53d2f742005-11-17 13:56:05 +01001312static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001314 struct snd_timer_id id;
1315 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (copy_from_user(&id, _tid, sizeof(id)))
1319 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001320 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (id.dev_class < 0) { /* first item */
1322 if (list_empty(&snd_timer_list))
1323 snd_timer_user_zero_id(&id);
1324 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001325 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001326 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 snd_timer_user_copy_id(&id, timer);
1328 }
1329 } else {
1330 switch (id.dev_class) {
1331 case SNDRV_TIMER_CLASS_GLOBAL:
1332 id.device = id.device < 0 ? 0 : id.device + 1;
1333 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001334 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1336 snd_timer_user_copy_id(&id, timer);
1337 break;
1338 }
1339 if (timer->tmr_device >= id.device) {
1340 snd_timer_user_copy_id(&id, timer);
1341 break;
1342 }
1343 }
1344 if (p == &snd_timer_list)
1345 snd_timer_user_zero_id(&id);
1346 break;
1347 case SNDRV_TIMER_CLASS_CARD:
1348 case SNDRV_TIMER_CLASS_PCM:
1349 if (id.card < 0) {
1350 id.card = 0;
1351 } else {
1352 if (id.card < 0) {
1353 id.card = 0;
1354 } else {
1355 if (id.device < 0) {
1356 id.device = 0;
1357 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001358 if (id.subdevice < 0) {
1359 id.subdevice = 0;
1360 } else {
1361 id.subdevice++;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364 }
1365 }
1366 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001367 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (timer->tmr_class > id.dev_class) {
1369 snd_timer_user_copy_id(&id, timer);
1370 break;
1371 }
1372 if (timer->tmr_class < id.dev_class)
1373 continue;
1374 if (timer->card->number > id.card) {
1375 snd_timer_user_copy_id(&id, timer);
1376 break;
1377 }
1378 if (timer->card->number < id.card)
1379 continue;
1380 if (timer->tmr_device > id.device) {
1381 snd_timer_user_copy_id(&id, timer);
1382 break;
1383 }
1384 if (timer->tmr_device < id.device)
1385 continue;
1386 if (timer->tmr_subdevice > id.subdevice) {
1387 snd_timer_user_copy_id(&id, timer);
1388 break;
1389 }
1390 if (timer->tmr_subdevice < id.subdevice)
1391 continue;
1392 snd_timer_user_copy_id(&id, timer);
1393 break;
1394 }
1395 if (p == &snd_timer_list)
1396 snd_timer_user_zero_id(&id);
1397 break;
1398 default:
1399 snd_timer_user_zero_id(&id);
1400 }
1401 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001402 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1404 return -EFAULT;
1405 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001408static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001409 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001411 struct snd_timer_ginfo *ginfo;
1412 struct snd_timer_id tid;
1413 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 struct list_head *p;
1415 int err = 0;
1416
1417 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1418 if (! ginfo)
1419 return -ENOMEM;
1420 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1421 kfree(ginfo);
1422 return -EFAULT;
1423 }
1424 tid = ginfo->tid;
1425 memset(ginfo, 0, sizeof(*ginfo));
1426 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001427 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 t = snd_timer_find(&tid);
1429 if (t != NULL) {
1430 ginfo->card = t->card ? t->card->number : -1;
1431 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1432 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1433 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1434 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1435 ginfo->resolution = t->hw.resolution;
1436 if (t->hw.resolution_min > 0) {
1437 ginfo->resolution_min = t->hw.resolution_min;
1438 ginfo->resolution_max = t->hw.resolution_max;
1439 }
1440 list_for_each(p, &t->open_list_head) {
1441 ginfo->clients++;
1442 }
1443 } else {
1444 err = -ENODEV;
1445 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001446 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1448 err = -EFAULT;
1449 kfree(ginfo);
1450 return err;
1451}
1452
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001453static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001454 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001456 struct snd_timer_gparams gparams;
1457 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 int err;
1459
1460 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1461 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001462 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001464 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001466 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001468 if (!list_empty(&t->open_list_head)) {
1469 err = -EBUSY;
1470 goto _error;
1471 }
1472 if (!t->hw.set_period) {
1473 err = -ENOSYS;
1474 goto _error;
1475 }
1476 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1477_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001478 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return err;
1480}
1481
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001482static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001483 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001485 struct snd_timer_gstatus gstatus;
1486 struct snd_timer_id tid;
1487 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 int err = 0;
1489
1490 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1491 return -EFAULT;
1492 tid = gstatus.tid;
1493 memset(&gstatus, 0, sizeof(gstatus));
1494 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001495 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 t = snd_timer_find(&tid);
1497 if (t != NULL) {
1498 if (t->hw.c_resolution)
1499 gstatus.resolution = t->hw.c_resolution(t);
1500 else
1501 gstatus.resolution = t->hw.resolution;
1502 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001503 t->hw.precise_resolution(t, &gstatus.resolution_num,
1504 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 } else {
1506 gstatus.resolution_num = gstatus.resolution;
1507 gstatus.resolution_den = 1000000000uL;
1508 }
1509 } else {
1510 err = -ENODEV;
1511 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001512 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1514 err = -EFAULT;
1515 return err;
1516}
1517
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001518static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001519 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001521 struct snd_timer_user *tu;
1522 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001524 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 tu = file->private_data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001527 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001528 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001530 tu->timeri = NULL;
1531 }
1532 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1533 err = -EFAULT;
1534 goto __err;
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 sprintf(str, "application %i", current->pid);
1537 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1538 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001539 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1540 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001541 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Jesper Juhl4d572772005-05-30 17:30:32 +02001543 kfree(tu->queue);
1544 tu->queue = NULL;
1545 kfree(tu->tqueue);
1546 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001548 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001549 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001550 if (tu->tqueue == NULL)
1551 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001553 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001554 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001555 if (tu->queue == NULL)
1556 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001558
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001559 if (err < 0) {
1560 snd_timer_close(tu->timeri);
1561 tu->timeri = NULL;
1562 } else {
1563 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001564 tu->timeri->callback = tu->tread
1565 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001566 tu->timeri->ccallback = snd_timer_user_ccallback;
1567 tu->timeri->callback_data = (void *)tu;
1568 }
1569
1570 __err:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001571 mutex_unlock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001575static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001576 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001578 struct snd_timer_user *tu;
1579 struct snd_timer_info *info;
1580 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 int err = 0;
1582
1583 tu = file->private_data;
1584 snd_assert(tu->timeri != NULL, return -ENXIO);
1585 t = tu->timeri->timer;
1586 snd_assert(t != NULL, return -ENXIO);
1587
Takashi Iwaica2c0962005-09-09 14:20:23 +02001588 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (! info)
1590 return -ENOMEM;
1591 info->card = t->card ? t->card->number : -1;
1592 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1593 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1594 strlcpy(info->id, t->id, sizeof(info->id));
1595 strlcpy(info->name, t->name, sizeof(info->name));
1596 info->resolution = t->hw.resolution;
1597 if (copy_to_user(_info, info, sizeof(*_info)))
1598 err = -EFAULT;
1599 kfree(info);
1600 return err;
1601}
1602
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001603static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001604 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001606 struct snd_timer_user *tu;
1607 struct snd_timer_params params;
1608 struct snd_timer *t;
1609 struct snd_timer_read *tr;
1610 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 tu = file->private_data;
1614 snd_assert(tu->timeri != NULL, return -ENXIO);
1615 t = tu->timeri->timer;
1616 snd_assert(t != NULL, return -ENXIO);
1617 if (copy_from_user(&params, _params, sizeof(params)))
1618 return -EFAULT;
1619 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1620 err = -EINVAL;
1621 goto _end;
1622 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001623 if (params.queue_size > 0 &&
1624 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 err = -EINVAL;
1626 goto _end;
1627 }
1628 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1629 (1<<SNDRV_TIMER_EVENT_TICK)|
1630 (1<<SNDRV_TIMER_EVENT_START)|
1631 (1<<SNDRV_TIMER_EVENT_STOP)|
1632 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1633 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001634 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1635 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 (1<<SNDRV_TIMER_EVENT_MSTART)|
1637 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1638 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001639 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1640 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001641 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 err = -EINVAL;
1643 goto _end;
1644 }
1645 snd_timer_stop(tu->timeri);
1646 spin_lock_irq(&t->lock);
1647 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1648 SNDRV_TIMER_IFLG_EXCLUSIVE|
1649 SNDRV_TIMER_IFLG_EARLY_EVENT);
1650 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1651 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1652 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1653 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1654 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1655 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1656 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001657 if (params.queue_size > 0 &&
1658 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001660 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1661 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (ttr) {
1663 kfree(tu->tqueue);
1664 tu->queue_size = params.queue_size;
1665 tu->tqueue = ttr;
1666 }
1667 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001668 tr = kmalloc(params.queue_size * sizeof(*tr),
1669 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (tr) {
1671 kfree(tu->queue);
1672 tu->queue_size = params.queue_size;
1673 tu->queue = tr;
1674 }
1675 }
1676 }
1677 tu->qhead = tu->qtail = tu->qused = 0;
1678 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1679 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001680 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 tread.event = SNDRV_TIMER_EVENT_EARLY;
1682 tread.tstamp.tv_sec = 0;
1683 tread.tstamp.tv_nsec = 0;
1684 tread.val = 0;
1685 snd_timer_user_append_to_tqueue(tu, &tread);
1686 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001687 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 r->resolution = 0;
1689 r->ticks = 0;
1690 tu->qused++;
1691 tu->qtail++;
1692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694 tu->filter = params.filter;
1695 tu->ticks = params.ticks;
1696 err = 0;
1697 _end:
1698 if (copy_to_user(_params, &params, sizeof(params)))
1699 return -EFAULT;
1700 return err;
1701}
1702
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001703static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001704 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001706 struct snd_timer_user *tu;
1707 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 tu = file->private_data;
1710 snd_assert(tu->timeri != NULL, return -ENXIO);
1711 memset(&status, 0, sizeof(status));
1712 status.tstamp = tu->tstamp;
1713 status.resolution = snd_timer_resolution(tu->timeri);
1714 status.lost = tu->timeri->lost;
1715 status.overrun = tu->overrun;
1716 spin_lock_irq(&tu->qlock);
1717 status.queue = tu->qused;
1718 spin_unlock_irq(&tu->qlock);
1719 if (copy_to_user(_status, &status, sizeof(status)))
1720 return -EFAULT;
1721 return 0;
1722}
1723
1724static int snd_timer_user_start(struct file *file)
1725{
1726 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001727 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 tu = file->private_data;
1730 snd_assert(tu->timeri != NULL, return -ENXIO);
1731 snd_timer_stop(tu->timeri);
1732 tu->timeri->lost = 0;
1733 tu->last_resolution = 0;
1734 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1735}
1736
1737static int snd_timer_user_stop(struct file *file)
1738{
1739 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001740 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 tu = file->private_data;
1743 snd_assert(tu->timeri != NULL, return -ENXIO);
1744 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1745}
1746
1747static int snd_timer_user_continue(struct file *file)
1748{
1749 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001750 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 tu = file->private_data;
1753 snd_assert(tu->timeri != NULL, return -ENXIO);
1754 tu->timeri->lost = 0;
1755 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1756}
1757
Takashi Iwai15790a62005-05-15 15:04:14 +02001758static int snd_timer_user_pause(struct file *file)
1759{
1760 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001761 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001762
Takashi Iwai15790a62005-05-15 15:04:14 +02001763 tu = file->private_data;
1764 snd_assert(tu->timeri != NULL, return -ENXIO);
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001765 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001766}
1767
Takashi Iwai8c50b372005-05-15 15:43:54 +02001768enum {
1769 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1770 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1771 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1772 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1773};
1774
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001775static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1776 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001778 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 void __user *argp = (void __user *)arg;
1780 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 tu = file->private_data;
1783 switch (cmd) {
1784 case SNDRV_TIMER_IOCTL_PVERSION:
1785 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1786 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1787 return snd_timer_user_next_device(argp);
1788 case SNDRV_TIMER_IOCTL_TREAD:
1789 {
1790 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001791
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001792 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001793 if (tu->timeri) { /* too late */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001794 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001796 }
1797 if (get_user(xarg, p)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001798 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 tu->tread = xarg ? 1 : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001802 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return 0;
1804 }
1805 case SNDRV_TIMER_IOCTL_GINFO:
1806 return snd_timer_user_ginfo(file, argp);
1807 case SNDRV_TIMER_IOCTL_GPARAMS:
1808 return snd_timer_user_gparams(file, argp);
1809 case SNDRV_TIMER_IOCTL_GSTATUS:
1810 return snd_timer_user_gstatus(file, argp);
1811 case SNDRV_TIMER_IOCTL_SELECT:
1812 return snd_timer_user_tselect(file, argp);
1813 case SNDRV_TIMER_IOCTL_INFO:
1814 return snd_timer_user_info(file, argp);
1815 case SNDRV_TIMER_IOCTL_PARAMS:
1816 return snd_timer_user_params(file, argp);
1817 case SNDRV_TIMER_IOCTL_STATUS:
1818 return snd_timer_user_status(file, argp);
1819 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001820 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return snd_timer_user_start(file);
1822 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001823 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return snd_timer_user_stop(file);
1825 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001826 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001828 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001829 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001830 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832 return -ENOTTY;
1833}
1834
1835static int snd_timer_user_fasync(int fd, struct file * file, int on)
1836{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001837 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 tu = file->private_data;
1841 err = fasync_helper(fd, file, on, &tu->fasync);
1842 if (err < 0)
1843 return err;
1844 return 0;
1845}
1846
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001847static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1848 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001850 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 long result = 0, unit;
1852 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001855 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 spin_lock_irq(&tu->qlock);
1857 while ((long)count - result >= unit) {
1858 while (!tu->qused) {
1859 wait_queue_t wait;
1860
1861 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1862 err = -EAGAIN;
1863 break;
1864 }
1865
1866 set_current_state(TASK_INTERRUPTIBLE);
1867 init_waitqueue_entry(&wait, current);
1868 add_wait_queue(&tu->qchange_sleep, &wait);
1869
1870 spin_unlock_irq(&tu->qlock);
1871 schedule();
1872 spin_lock_irq(&tu->qlock);
1873
1874 remove_wait_queue(&tu->qchange_sleep, &wait);
1875
1876 if (signal_pending(current)) {
1877 err = -ERESTARTSYS;
1878 break;
1879 }
1880 }
1881
1882 spin_unlock_irq(&tu->qlock);
1883 if (err < 0)
1884 goto _error;
1885
1886 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001887 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001888 sizeof(struct snd_timer_tread))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 err = -EFAULT;
1890 goto _error;
1891 }
1892 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001893 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001894 sizeof(struct snd_timer_read))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 err = -EFAULT;
1896 goto _error;
1897 }
1898 }
1899
1900 tu->qhead %= tu->queue_size;
1901
1902 result += unit;
1903 buffer += unit;
1904
1905 spin_lock_irq(&tu->qlock);
1906 tu->qused--;
1907 }
1908 spin_unlock_irq(&tu->qlock);
1909 _error:
1910 return result > 0 ? result : err;
1911}
1912
1913static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1914{
1915 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001916 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 tu = file->private_data;
1919
1920 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 mask = 0;
1923 if (tu->qused)
1924 mask |= POLLIN | POLLRDNORM;
1925
1926 return mask;
1927}
1928
1929#ifdef CONFIG_COMPAT
1930#include "timer_compat.c"
1931#else
1932#define snd_timer_user_ioctl_compat NULL
1933#endif
1934
1935static struct file_operations snd_timer_f_ops =
1936{
1937 .owner = THIS_MODULE,
1938 .read = snd_timer_user_read,
1939 .open = snd_timer_user_open,
1940 .release = snd_timer_user_release,
1941 .poll = snd_timer_user_poll,
1942 .unlocked_ioctl = snd_timer_user_ioctl,
1943 .compat_ioctl = snd_timer_user_ioctl_compat,
1944 .fasync = snd_timer_user_fasync,
1945};
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947/*
1948 * ENTRY functions
1949 */
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951static int __init alsa_timer_init(void)
1952{
1953 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001956 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1957 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if ((err = snd_timer_register_system()) < 0)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001961 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1962 err);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001963 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1964 &snd_timer_f_ops, NULL, "timer")) < 0)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001965 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1966 err);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001967 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 0;
1969}
1970
1971static void __exit alsa_timer_exit(void)
1972{
1973 struct list_head *p, *n;
1974
1975 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1976 /* unregister the system timer */
1977 list_for_each_safe(p, n, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001978 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
Takashi Iwaic4614822006-06-23 14:38:23 +02001979 snd_timer_free(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Takashi Iwaie28563c2005-12-01 10:42:42 +01001981 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982#ifdef SNDRV_OSS_INFO_DEV_TIMERS
1983 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1984#endif
1985}
1986
1987module_init(alsa_timer_init)
1988module_exit(alsa_timer_exit)
1989
1990EXPORT_SYMBOL(snd_timer_open);
1991EXPORT_SYMBOL(snd_timer_close);
1992EXPORT_SYMBOL(snd_timer_resolution);
1993EXPORT_SYMBOL(snd_timer_start);
1994EXPORT_SYMBOL(snd_timer_stop);
1995EXPORT_SYMBOL(snd_timer_continue);
1996EXPORT_SYMBOL(snd_timer_pause);
1997EXPORT_SYMBOL(snd_timer_new);
1998EXPORT_SYMBOL(snd_timer_notify);
1999EXPORT_SYMBOL(snd_timer_global_new);
2000EXPORT_SYMBOL(snd_timer_global_free);
2001EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002EXPORT_SYMBOL(snd_timer_interrupt);