blob: 7e5fe2d9166233b0874dc5a97c8f930c214e27c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Timers abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/time.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/moduleparam.h>
Paulo Marques543537b2005-06-23 00:09:02 -070029#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/timer.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/minors.h>
35#include <sound/initval.h>
36#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
39#define DEFAULT_TIMER_LIMIT 3
40#elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
41#define DEFAULT_TIMER_LIMIT 2
42#else
43#define DEFAULT_TIMER_LIMIT 1
44#endif
45
46static int timer_limit = DEFAULT_TIMER_LIMIT;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010047static int timer_tstamp_monotonic = 1;
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020048MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049MODULE_DESCRIPTION("ALSA timer interface");
50MODULE_LICENSE("GPL");
51module_param(timer_limit, int, 0444);
52MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010053module_param(timer_tstamp_monotonic, int, 0444);
54MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Takashi Iwai53d2f742005-11-17 13:56:05 +010056struct snd_timer_user {
57 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020058 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned long ticks;
60 unsigned long overrun;
61 int qhead;
62 int qtail;
63 int qused;
64 int queue_size;
Takashi Iwai53d2f742005-11-17 13:56:05 +010065 struct snd_timer_read *queue;
66 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 spinlock_t qlock;
68 unsigned long last_resolution;
69 unsigned int filter;
70 struct timespec tstamp; /* trigger tstamp */
71 wait_queue_head_t qchange_sleep;
72 struct fasync_struct *fasync;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010073 struct mutex tread_sem;
Takashi Iwai53d2f742005-11-17 13:56:05 +010074};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* list of timers */
77static LIST_HEAD(snd_timer_list);
78
79/* list of slave instances */
80static LIST_HEAD(snd_timer_slave_list);
81
82/* lock for slave active lists */
83static DEFINE_SPINLOCK(slave_active_lock);
84
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010085static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwai53d2f742005-11-17 13:56:05 +010087static int snd_timer_free(struct snd_timer *timer);
88static int snd_timer_dev_free(struct snd_device *device);
89static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020090static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Takashi Iwai53d2f742005-11-17 13:56:05 +010092static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * create a timer instance with the given owner string.
96 * when timer is not NULL, increments the module counter
97 */
Takashi Iwai53d2f742005-11-17 13:56:05 +010098static struct snd_timer_instance *snd_timer_instance_new(char *owner,
99 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100101 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200102 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (timeri == NULL)
104 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700105 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (! timeri->owner) {
107 kfree(timeri);
108 return NULL;
109 }
110 INIT_LIST_HEAD(&timeri->open_list);
111 INIT_LIST_HEAD(&timeri->active_list);
112 INIT_LIST_HEAD(&timeri->ack_list);
113 INIT_LIST_HEAD(&timeri->slave_list_head);
114 INIT_LIST_HEAD(&timeri->slave_active_head);
115
116 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200117 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 kfree(timeri->owner);
119 kfree(timeri);
120 return NULL;
121 }
122
123 return timeri;
124}
125
126/*
127 * find a timer instance from the given timer id
128 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100129static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100131 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Johannes Berg9244b2c2006-10-05 16:02:22 +0200133 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (timer->tmr_class != tid->dev_class)
135 continue;
136 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
137 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
138 (timer->card == NULL ||
139 timer->card->number != tid->card))
140 continue;
141 if (timer->tmr_device != tid->device)
142 continue;
143 if (timer->tmr_subdevice != tid->subdevice)
144 continue;
145 return timer;
146 }
147 return NULL;
148}
149
150#ifdef CONFIG_KMOD
151
Takashi Iwai53d2f742005-11-17 13:56:05 +0100152static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 if (! current->fs->root)
155 return;
156 switch (tid->dev_class) {
157 case SNDRV_TIMER_CLASS_GLOBAL:
158 if (tid->device < timer_limit)
159 request_module("snd-timer-%i", tid->device);
160 break;
161 case SNDRV_TIMER_CLASS_CARD:
162 case SNDRV_TIMER_CLASS_PCM:
163 if (tid->card < snd_ecards_limit)
164 request_module("snd-card-%i", tid->card);
165 break;
166 default:
167 break;
168 }
169}
170
171#endif
172
173/*
174 * look for a master instance matching with the slave id of the given slave.
175 * when found, relink the open_link of the slave.
176 *
177 * call this with register_mutex down.
178 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100179static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100181 struct snd_timer *timer;
182 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200185 list_for_each_entry(timer, &snd_timer_list, device_list) {
186 list_for_each_entry(master, &timer->open_list_head, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (slave->slave_class == master->slave_class &&
188 slave->slave_id == master->slave_id) {
189 list_del(&slave->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200190 list_add_tail(&slave->open_list,
191 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 spin_lock_irq(&slave_active_lock);
193 slave->master = master;
194 slave->timer = master->timer;
195 spin_unlock_irq(&slave_active_lock);
196 return;
197 }
198 }
199 }
200}
201
202/*
203 * look for slave instances matching with the slave id of the given master.
204 * when found, relink the open_link of slaves.
205 *
206 * call this with register_mutex down.
207 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100208static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200210 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200213 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (slave->slave_class == master->slave_class &&
215 slave->slave_id == master->slave_id) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200216 list_move_tail(&slave->open_list, &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 spin_lock_irq(&slave_active_lock);
218 slave->master = master;
219 slave->timer = master->timer;
220 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200221 list_add_tail(&slave->active_list,
222 &master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_unlock_irq(&slave_active_lock);
224 }
225 }
226}
227
228/*
229 * open a timer instance
230 * when opening a master, the slave id must be here given.
231 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100232int snd_timer_open(struct snd_timer_instance **ti,
233 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned int slave_id)
235{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100236 struct snd_timer *timer;
237 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
240 /* open a slave instance */
241 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
242 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
243 snd_printd("invalid slave class %i\n", tid->dev_sclass);
244 return -EINVAL;
245 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100246 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200248 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100249 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200250 return -ENOMEM;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 timeri->slave_class = tid->dev_sclass;
253 timeri->slave_id = tid->device;
254 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
255 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
256 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100257 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *ti = timeri;
259 return 0;
260 }
261
262 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100263 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 timer = snd_timer_find(tid);
265#ifdef CONFIG_KMOD
266 if (timer == NULL) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100267 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100269 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 timer = snd_timer_find(tid);
271 }
272#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200273 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100274 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -ENODEV;
276 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200277 if (!list_empty(&timer->open_list_head)) {
278 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100279 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200280 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100281 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200282 return -EBUSY;
283 }
284 }
285 timeri = snd_timer_instance_new(owner, timer);
286 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100287 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200288 return -ENOMEM;
289 }
290 timeri->slave_class = tid->dev_sclass;
291 timeri->slave_id = slave_id;
292 if (list_empty(&timer->open_list_head) && timer->hw.open)
293 timer->hw.open(timer);
294 list_add_tail(&timeri->open_list, &timer->open_list_head);
295 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100296 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 *ti = timeri;
298 return 0;
299}
300
Takashi Iwai53d2f742005-11-17 13:56:05 +0100301static int _snd_timer_stop(struct snd_timer_instance *timeri,
302 int keep_flag, int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304/*
305 * close a timer instance
306 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100307int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100309 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200310 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 snd_assert(timeri != NULL, return -ENXIO);
313
314 /* force to stop the timer */
315 snd_timer_stop(timeri);
316
317 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
318 /* wait, until the active callback is finished */
319 spin_lock_irq(&slave_active_lock);
320 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
321 spin_unlock_irq(&slave_active_lock);
322 udelay(10);
323 spin_lock_irq(&slave_active_lock);
324 }
325 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100326 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 list_del(&timeri->open_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100328 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 } else {
330 timer = timeri->timer;
331 /* wait, until the active callback is finished */
332 spin_lock_irq(&timer->lock);
333 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
334 spin_unlock_irq(&timer->lock);
335 udelay(10);
336 spin_lock_irq(&timer->lock);
337 }
338 spin_unlock_irq(&timer->lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100339 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 list_del(&timeri->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200341 if (timer && list_empty(&timer->open_list_head) &&
342 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 timer->hw.close(timer);
344 /* remove slave links */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200345 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
346 open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_lock_irq(&slave_active_lock);
348 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200349 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 slave->master = NULL;
351 slave->timer = NULL;
352 spin_unlock_irq(&slave_active_lock);
353 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100354 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 if (timeri->private_free)
357 timeri->private_free(timeri);
358 kfree(timeri->owner);
359 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200360 if (timer)
361 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363}
364
Takashi Iwai53d2f742005-11-17 13:56:05 +0100365unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100367 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (timeri == NULL)
370 return 0;
371 if ((timer = timeri->timer) != NULL) {
372 if (timer->hw.c_resolution)
373 return timer->hw.c_resolution(timer);
374 return timer->hw.resolution;
375 }
376 return 0;
377}
378
Takashi Iwai53d2f742005-11-17 13:56:05 +0100379static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100381 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned long flags;
383 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100384 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct timespec tstamp;
386
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100387 if (timer_tstamp_monotonic)
388 do_posix_clock_monotonic_gettime(&tstamp);
389 else
390 getnstimeofday(&tstamp);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200391 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
392 event <= SNDRV_TIMER_EVENT_PAUSE, return);
393 if (event == SNDRV_TIMER_EVENT_START ||
394 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 resolution = snd_timer_resolution(ti);
396 if (ti->ccallback)
397 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
398 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
399 return;
400 timer = ti->timer;
401 if (timer == NULL)
402 return;
403 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
404 return;
405 spin_lock_irqsave(&timer->lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200406 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (ts->ccallback)
408 ts->ccallback(ti, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 spin_unlock_irqrestore(&timer->lock, flags);
410}
411
Takashi Iwai53d2f742005-11-17 13:56:05 +0100412static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200413 unsigned long sticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 list_del(&timeri->active_list);
416 list_add_tail(&timeri->active_list, &timer->active_list_head);
417 if (timer->running) {
418 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
419 goto __start_now;
420 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
421 timeri->flags |= SNDRV_TIMER_IFLG_START;
422 return 1; /* delayed start */
423 } else {
424 timer->sticks = sticks;
425 timer->hw.start(timer);
426 __start_now:
427 timer->running++;
428 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
429 return 0;
430 }
431}
432
Takashi Iwai53d2f742005-11-17 13:56:05 +0100433static int snd_timer_start_slave(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 unsigned long flags;
436
437 spin_lock_irqsave(&slave_active_lock, flags);
438 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
439 if (timeri->master)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200440 list_add_tail(&timeri->active_list,
441 &timeri->master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 spin_unlock_irqrestore(&slave_active_lock, flags);
443 return 1; /* delayed start */
444}
445
446/*
447 * start the timer instance
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200448 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100449int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100451 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int result = -EINVAL;
453 unsigned long flags;
454
455 if (timeri == NULL || ticks < 1)
456 return -EINVAL;
457 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
458 result = snd_timer_start_slave(timeri);
459 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
460 return result;
461 }
462 timer = timeri->timer;
463 if (timer == NULL)
464 return -EINVAL;
465 spin_lock_irqsave(&timer->lock, flags);
466 timeri->ticks = timeri->cticks = ticks;
467 timeri->pticks = 0;
468 result = snd_timer_start1(timer, timeri, ticks);
469 spin_unlock_irqrestore(&timer->lock, flags);
470 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
471 return result;
472}
473
Takashi Iwai53d2f742005-11-17 13:56:05 +0100474static int _snd_timer_stop(struct snd_timer_instance * timeri,
475 int keep_flag, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100477 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned long flags;
479
480 snd_assert(timeri != NULL, return -ENXIO);
481
482 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
483 if (!keep_flag) {
484 spin_lock_irqsave(&slave_active_lock, flags);
485 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
486 spin_unlock_irqrestore(&slave_active_lock, flags);
487 }
488 goto __end;
489 }
490 timer = timeri->timer;
491 if (!timer)
492 return -EINVAL;
493 spin_lock_irqsave(&timer->lock, flags);
494 list_del_init(&timeri->ack_list);
495 list_del_init(&timeri->active_list);
496 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
497 !(--timer->running)) {
498 timer->hw.stop(timer);
499 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
500 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
501 snd_timer_reschedule(timer, 0);
502 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
503 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
504 timer->hw.start(timer);
505 }
506 }
507 }
508 if (!keep_flag)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200509 timeri->flags &=
510 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 spin_unlock_irqrestore(&timer->lock, flags);
512 __end:
513 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
514 snd_timer_notify1(timeri, event);
515 return 0;
516}
517
518/*
519 * stop the timer instance.
520 *
521 * do not call this from the timer callback!
522 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100523int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100525 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned long flags;
527 int err;
528
529 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
530 if (err < 0)
531 return err;
532 timer = timeri->timer;
533 spin_lock_irqsave(&timer->lock, flags);
534 timeri->cticks = timeri->ticks;
535 timeri->pticks = 0;
536 spin_unlock_irqrestore(&timer->lock, flags);
537 return 0;
538}
539
540/*
541 * start again.. the tick is kept.
542 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100543int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100545 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int result = -EINVAL;
547 unsigned long flags;
548
549 if (timeri == NULL)
550 return result;
551 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
552 return snd_timer_start_slave(timeri);
553 timer = timeri->timer;
554 if (! timer)
555 return -EINVAL;
556 spin_lock_irqsave(&timer->lock, flags);
557 if (!timeri->cticks)
558 timeri->cticks = 1;
559 timeri->pticks = 0;
560 result = snd_timer_start1(timer, timeri, timer->sticks);
561 spin_unlock_irqrestore(&timer->lock, flags);
562 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
563 return result;
564}
565
566/*
567 * pause.. remember the ticks left
568 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100569int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
572}
573
574/*
575 * reschedule the timer
576 *
577 * start pending instances and check the scheduling ticks.
578 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
579 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100580static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100582 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Johannes Berg9244b2c2006-10-05 16:02:22 +0200585 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (ti->flags & SNDRV_TIMER_IFLG_START) {
587 ti->flags &= ~SNDRV_TIMER_IFLG_START;
588 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
589 timer->running++;
590 }
591 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
592 if (ticks > ti->cticks)
593 ticks = ti->cticks;
594 }
595 }
596 if (ticks == ~0UL) {
597 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
598 return;
599 }
600 if (ticks > timer->hw.ticks)
601 ticks = timer->hw.ticks;
602 if (ticks_left != ticks)
603 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
604 timer->sticks = ticks;
605}
606
607/*
608 * timer tasklet
609 *
610 */
611static void snd_timer_tasklet(unsigned long arg)
612{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100613 struct snd_timer *timer = (struct snd_timer *) arg;
614 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct list_head *p;
616 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200617 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Takashi Iwai2999ff52006-07-05 17:16:58 +0200619 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* now process all callbacks */
621 while (!list_empty(&timer->sack_list_head)) {
622 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100623 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* remove from ack_list and make empty */
626 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ticks = ti->pticks;
629 ti->pticks = 0;
630 resolution = ti->resolution;
631
632 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
633 spin_unlock(&timer->lock);
634 if (ti->callback)
635 ti->callback(ti, resolution, ticks);
636 spin_lock(&timer->lock);
637 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
638 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200639 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/*
643 * timer interrupt
644 *
645 * ticks_left is usually equal to timer->sticks.
646 *
647 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100648void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200650 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200652 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100653 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int use_tasklet = 0;
655
656 if (timer == NULL)
657 return;
658
Takashi Iwaib32425a2005-11-18 18:52:14 +0100659 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* remember the current resolution */
662 if (timer->hw.c_resolution)
663 resolution = timer->hw.c_resolution(timer);
664 else
665 resolution = timer->hw.resolution;
666
667 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200668 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200669 * processed instance is relinked to done_list_head before the callback
670 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200672 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
673 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
675 continue;
676 ti->pticks += ticks_left;
677 ti->resolution = resolution;
678 if (ti->cticks < ticks_left)
679 ti->cticks = 0;
680 else
681 ti->cticks -= ticks_left;
682 if (ti->cticks) /* not expired */
683 continue;
684 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
685 ti->cticks = ti->ticks;
686 } else {
687 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
688 if (--timer->running)
Johannes Berg9244b2c2006-10-05 16:02:22 +0200689 list_del(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200691 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
692 (ti->flags & SNDRV_TIMER_IFLG_FAST))
693 ack_list_head = &timer->ack_list_head;
694 else
695 ack_list_head = &timer->sack_list_head;
696 if (list_empty(&ti->ack_list))
697 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200698 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 ts->pticks = ti->pticks;
700 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200701 if (list_empty(&ts->ack_list))
702 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 }
705 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200706 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (timer->running) {
708 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
709 timer->hw.stop(timer);
710 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
711 }
712 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
713 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
714 /* restart timer */
715 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
716 timer->hw.start(timer);
717 }
718 } else {
719 timer->hw.stop(timer);
720 }
721
722 /* now process all fast callbacks */
723 while (!list_empty(&timer->ack_list_head)) {
724 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100725 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* remove from ack_list and make empty */
728 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ticks = ti->pticks;
731 ti->pticks = 0;
732
733 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
734 spin_unlock(&timer->lock);
735 if (ti->callback)
736 ti->callback(ti, resolution, ticks);
737 spin_lock(&timer->lock);
738 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
739 }
740
741 /* do we have any slow callbacks? */
742 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100743 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (use_tasklet)
746 tasklet_hi_schedule(&timer->task_queue);
747}
748
749/*
750
751 */
752
Takashi Iwai53d2f742005-11-17 13:56:05 +0100753int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
754 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100756 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100758 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .dev_free = snd_timer_dev_free,
760 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200761 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 };
763
764 snd_assert(tid != NULL, return -EINVAL);
765 snd_assert(rtimer != NULL, return -EINVAL);
766 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200767 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100768 if (timer == NULL) {
769 snd_printk(KERN_ERR "timer: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 timer->tmr_class = tid->dev_class;
773 timer->card = card;
774 timer->tmr_device = tid->device;
775 timer->tmr_subdevice = tid->subdevice;
776 if (id)
777 strlcpy(timer->id, id, sizeof(timer->id));
778 INIT_LIST_HEAD(&timer->device_list);
779 INIT_LIST_HEAD(&timer->open_list_head);
780 INIT_LIST_HEAD(&timer->active_list_head);
781 INIT_LIST_HEAD(&timer->ack_list_head);
782 INIT_LIST_HEAD(&timer->sack_list_head);
783 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200784 tasklet_init(&timer->task_queue, snd_timer_tasklet,
785 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200787 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200788 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
789 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 snd_timer_free(timer);
791 return err;
792 }
793 }
794 *rtimer = timer;
795 return 0;
796}
797
Takashi Iwai53d2f742005-11-17 13:56:05 +0100798static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 snd_assert(timer != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +0200801
802 mutex_lock(&register_mutex);
803 if (! list_empty(&timer->open_list_head)) {
804 struct list_head *p, *n;
805 struct snd_timer_instance *ti;
806 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
807 list_for_each_safe(p, n, &timer->open_list_head) {
808 list_del_init(p);
809 ti = list_entry(p, struct snd_timer_instance, open_list);
810 ti->timer = NULL;
811 }
812 }
813 list_del(&timer->device_list);
814 mutex_unlock(&register_mutex);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (timer->private_free)
817 timer->private_free(timer);
818 kfree(timer);
819 return 0;
820}
821
Takashi Iwai53d2f742005-11-17 13:56:05 +0100822static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100824 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return snd_timer_free(timer);
826}
827
Takashi Iwai53d2f742005-11-17 13:56:05 +0100828static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100830 struct snd_timer *timer = dev->device_data;
831 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200833 snd_assert(timer != NULL && timer->hw.start != NULL &&
834 timer->hw.stop != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
836 !timer->hw.resolution && timer->hw.c_resolution == NULL)
837 return -EINVAL;
838
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100839 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200840 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (timer1->tmr_class > timer->tmr_class)
842 break;
843 if (timer1->tmr_class < timer->tmr_class)
844 continue;
845 if (timer1->card && timer->card) {
846 if (timer1->card->number > timer->card->number)
847 break;
848 if (timer1->card->number < timer->card->number)
849 continue;
850 }
851 if (timer1->tmr_device > timer->tmr_device)
852 break;
853 if (timer1->tmr_device < timer->tmr_device)
854 continue;
855 if (timer1->tmr_subdevice > timer->tmr_subdevice)
856 break;
857 if (timer1->tmr_subdevice < timer->tmr_subdevice)
858 continue;
859 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100860 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EBUSY;
862 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200863 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100864 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
Takashi Iwaic4614822006-06-23 14:38:23 +0200868static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100870 struct snd_timer *timer = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +0200871 mutex_lock(&register_mutex);
872 list_del_init(&timer->device_list);
873 mutex_unlock(&register_mutex);
874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Takashi Iwai53d2f742005-11-17 13:56:05 +0100877void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 unsigned long flags;
880 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100881 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200883 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
884 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200885 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
886 event <= SNDRV_TIMER_EVENT_MRESUME, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200888 if (event == SNDRV_TIMER_EVENT_MSTART ||
889 event == SNDRV_TIMER_EVENT_MCONTINUE ||
890 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (timer->hw.c_resolution)
892 resolution = timer->hw.c_resolution(timer);
893 else
894 resolution = timer->hw.resolution;
895 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200896 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (ti->ccallback)
898 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200899 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (ts->ccallback)
901 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 spin_unlock_irqrestore(&timer->lock, flags);
904}
905
906/*
907 * exported functions for global timers
908 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100909int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100911 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
914 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
915 tid.card = -1;
916 tid.device = device;
917 tid.subdevice = 0;
918 return snd_timer_new(NULL, id, &tid, rtimer);
919}
920
Takashi Iwai53d2f742005-11-17 13:56:05 +0100921int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 return snd_timer_free(timer);
924}
925
Takashi Iwai53d2f742005-11-17 13:56:05 +0100926int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100928 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 memset(&dev, 0, sizeof(dev));
931 dev.device_data = timer;
932 return snd_timer_dev_register(&dev);
933}
934
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200935/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * System timer
937 */
938
939struct snd_timer_system_private {
940 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 unsigned long last_expires;
942 unsigned long last_jiffies;
943 unsigned long correction;
944};
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static void snd_timer_s_function(unsigned long data)
947{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100948 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 struct snd_timer_system_private *priv = timer->private_data;
950 unsigned long jiff = jiffies;
951 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +0200952 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
954}
955
Takashi Iwai53d2f742005-11-17 13:56:05 +0100956static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct snd_timer_system_private *priv;
959 unsigned long njiff;
960
961 priv = (struct snd_timer_system_private *) timer->private_data;
962 njiff = (priv->last_jiffies = jiffies);
963 if (priv->correction > timer->sticks - 1) {
964 priv->correction -= timer->sticks - 1;
965 njiff++;
966 } else {
967 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +0200968 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 priv->last_expires = priv->tlist.expires = njiff;
971 add_timer(&priv->tlist);
972 return 0;
973}
974
Takashi Iwai53d2f742005-11-17 13:56:05 +0100975static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct snd_timer_system_private *priv;
978 unsigned long jiff;
979
980 priv = (struct snd_timer_system_private *) timer->private_data;
981 del_timer(&priv->tlist);
982 jiff = jiffies;
983 if (time_before(jiff, priv->last_expires))
984 timer->sticks = priv->last_expires - jiff;
985 else
986 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +0200987 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 0;
989}
990
Takashi Iwai53d2f742005-11-17 13:56:05 +0100991static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
994 .resolution = 1000000000L / HZ,
995 .ticks = 10000000L,
996 .start = snd_timer_s_start,
997 .stop = snd_timer_s_stop
998};
999
Takashi Iwai53d2f742005-11-17 13:56:05 +01001000static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 kfree(timer->private_data);
1003}
1004
1005static int snd_timer_register_system(void)
1006{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001007 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct snd_timer_system_private *priv;
1009 int err;
1010
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001011 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1012 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return err;
1014 strcpy(timer->name, "system timer");
1015 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001016 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (priv == NULL) {
1018 snd_timer_free(timer);
1019 return -ENOMEM;
1020 }
1021 init_timer(&priv->tlist);
1022 priv->tlist.function = snd_timer_s_function;
1023 priv->tlist.data = (unsigned long) timer;
1024 timer->private_data = priv;
1025 timer->private_free = snd_timer_free_system;
1026 return snd_timer_global_register(timer);
1027}
1028
Takashi Iwaie28563c2005-12-01 10:42:42 +01001029#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/*
1031 * Info interface
1032 */
1033
Takashi Iwai53d2f742005-11-17 13:56:05 +01001034static void snd_timer_proc_read(struct snd_info_entry *entry,
1035 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001037 struct snd_timer *timer;
1038 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001040 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001041 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 switch (timer->tmr_class) {
1043 case SNDRV_TIMER_CLASS_GLOBAL:
1044 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1045 break;
1046 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001047 snd_iprintf(buffer, "C%i-%i: ",
1048 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001051 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1052 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 break;
1054 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001055 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1056 timer->card ? timer->card->number : -1,
1057 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 snd_iprintf(buffer, "%s :", timer->name);
1060 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001061 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1062 timer->hw.resolution / 1000,
1063 timer->hw.resolution % 1000,
1064 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1066 snd_iprintf(buffer, " SLAVE");
1067 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001068 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001069 snd_iprintf(buffer, " Client %s : %s\n",
1070 ti->owner ? ti->owner : "unknown",
1071 ti->flags & (SNDRV_TIMER_IFLG_START |
1072 SNDRV_TIMER_IFLG_RUNNING)
1073 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001075 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001078static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001079
1080static void __init snd_timer_proc_init(void)
1081{
1082 struct snd_info_entry *entry;
1083
1084 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1085 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001086 entry->c.text.read = snd_timer_proc_read;
1087 if (snd_info_register(entry) < 0) {
1088 snd_info_free_entry(entry);
1089 entry = NULL;
1090 }
1091 }
1092 snd_timer_proc_entry = entry;
1093}
1094
1095static void __exit snd_timer_proc_done(void)
1096{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001097 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001098}
1099#else /* !CONFIG_PROC_FS */
1100#define snd_timer_proc_init()
1101#define snd_timer_proc_done()
1102#endif
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/*
1105 * USER SPACE interface
1106 */
1107
Takashi Iwai53d2f742005-11-17 13:56:05 +01001108static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 unsigned long resolution,
1110 unsigned long ticks)
1111{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001112 struct snd_timer_user *tu = timeri->callback_data;
1113 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 spin_lock(&tu->qlock);
1117 if (tu->qused > 0) {
1118 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1119 r = &tu->queue[prev];
1120 if (r->resolution == resolution) {
1121 r->ticks += ticks;
1122 goto __wake;
1123 }
1124 }
1125 if (tu->qused >= tu->queue_size) {
1126 tu->overrun++;
1127 } else {
1128 r = &tu->queue[tu->qtail++];
1129 tu->qtail %= tu->queue_size;
1130 r->resolution = resolution;
1131 r->ticks = ticks;
1132 tu->qused++;
1133 }
1134 __wake:
1135 spin_unlock(&tu->qlock);
1136 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1137 wake_up(&tu->qchange_sleep);
1138}
1139
Takashi Iwai53d2f742005-11-17 13:56:05 +01001140static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1141 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 if (tu->qused >= tu->queue_size) {
1144 tu->overrun++;
1145 } else {
1146 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1147 tu->qtail %= tu->queue_size;
1148 tu->qused++;
1149 }
1150}
1151
Takashi Iwai53d2f742005-11-17 13:56:05 +01001152static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1153 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct timespec *tstamp,
1155 unsigned long resolution)
1156{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001157 struct snd_timer_user *tu = timeri->callback_data;
1158 struct snd_timer_tread r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001160 if (event >= SNDRV_TIMER_EVENT_START &&
1161 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 tu->tstamp = *tstamp;
1163 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1164 return;
1165 r1.event = event;
1166 r1.tstamp = *tstamp;
1167 r1.val = resolution;
1168 spin_lock(&tu->qlock);
1169 snd_timer_user_append_to_tqueue(tu, &r1);
1170 spin_unlock(&tu->qlock);
1171 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1172 wake_up(&tu->qchange_sleep);
1173}
1174
Takashi Iwai53d2f742005-11-17 13:56:05 +01001175static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 unsigned long resolution,
1177 unsigned long ticks)
1178{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001179 struct snd_timer_user *tu = timeri->callback_data;
1180 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 struct timespec tstamp;
1182 int prev, append = 0;
1183
Takashi Iwai07799e72005-10-10 11:49:49 +02001184 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001186 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1187 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 spin_unlock(&tu->qlock);
1189 return;
1190 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001191 if (tu->last_resolution != resolution || ticks > 0) {
1192 if (timer_tstamp_monotonic)
1193 do_posix_clock_monotonic_gettime(&tstamp);
1194 else
1195 getnstimeofday(&tstamp);
1196 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001197 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1198 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1200 r1.tstamp = tstamp;
1201 r1.val = resolution;
1202 snd_timer_user_append_to_tqueue(tu, &r1);
1203 tu->last_resolution = resolution;
1204 append++;
1205 }
1206 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1207 goto __wake;
1208 if (ticks == 0)
1209 goto __wake;
1210 if (tu->qused > 0) {
1211 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1212 r = &tu->tqueue[prev];
1213 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1214 r->tstamp = tstamp;
1215 r->val += ticks;
1216 append++;
1217 goto __wake;
1218 }
1219 }
1220 r1.event = SNDRV_TIMER_EVENT_TICK;
1221 r1.tstamp = tstamp;
1222 r1.val = ticks;
1223 snd_timer_user_append_to_tqueue(tu, &r1);
1224 append++;
1225 __wake:
1226 spin_unlock(&tu->qlock);
1227 if (append == 0)
1228 return;
1229 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1230 wake_up(&tu->qchange_sleep);
1231}
1232
1233static int snd_timer_user_open(struct inode *inode, struct file *file)
1234{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001235 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001236
Takashi Iwaica2c0962005-09-09 14:20:23 +02001237 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (tu == NULL)
1239 return -ENOMEM;
1240 spin_lock_init(&tu->qlock);
1241 init_waitqueue_head(&tu->qchange_sleep);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001242 mutex_init(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 tu->ticks = 1;
1244 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001245 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001246 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (tu->queue == NULL) {
1248 kfree(tu);
1249 return -ENOMEM;
1250 }
1251 file->private_data = tu;
1252 return 0;
1253}
1254
1255static int snd_timer_user_release(struct inode *inode, struct file *file)
1256{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001257 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (file->private_data) {
1260 tu = file->private_data;
1261 file->private_data = NULL;
1262 fasync_helper(-1, file, 0, &tu->fasync);
1263 if (tu->timeri)
1264 snd_timer_close(tu->timeri);
1265 kfree(tu->queue);
1266 kfree(tu->tqueue);
1267 kfree(tu);
1268 }
1269 return 0;
1270}
1271
Takashi Iwai53d2f742005-11-17 13:56:05 +01001272static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1275 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1276 id->card = -1;
1277 id->device = -1;
1278 id->subdevice = -1;
1279}
1280
Takashi Iwai53d2f742005-11-17 13:56:05 +01001281static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 id->dev_class = timer->tmr_class;
1284 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1285 id->card = timer->card ? timer->card->number : -1;
1286 id->device = timer->tmr_device;
1287 id->subdevice = timer->tmr_subdevice;
1288}
1289
Takashi Iwai53d2f742005-11-17 13:56:05 +01001290static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001292 struct snd_timer_id id;
1293 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (copy_from_user(&id, _tid, sizeof(id)))
1297 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001298 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (id.dev_class < 0) { /* first item */
1300 if (list_empty(&snd_timer_list))
1301 snd_timer_user_zero_id(&id);
1302 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001303 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001304 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 snd_timer_user_copy_id(&id, timer);
1306 }
1307 } else {
1308 switch (id.dev_class) {
1309 case SNDRV_TIMER_CLASS_GLOBAL:
1310 id.device = id.device < 0 ? 0 : id.device + 1;
1311 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001312 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1314 snd_timer_user_copy_id(&id, timer);
1315 break;
1316 }
1317 if (timer->tmr_device >= id.device) {
1318 snd_timer_user_copy_id(&id, timer);
1319 break;
1320 }
1321 }
1322 if (p == &snd_timer_list)
1323 snd_timer_user_zero_id(&id);
1324 break;
1325 case SNDRV_TIMER_CLASS_CARD:
1326 case SNDRV_TIMER_CLASS_PCM:
1327 if (id.card < 0) {
1328 id.card = 0;
1329 } else {
1330 if (id.card < 0) {
1331 id.card = 0;
1332 } else {
1333 if (id.device < 0) {
1334 id.device = 0;
1335 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001336 if (id.subdevice < 0) {
1337 id.subdevice = 0;
1338 } else {
1339 id.subdevice++;
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 }
1343 }
1344 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001345 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (timer->tmr_class > id.dev_class) {
1347 snd_timer_user_copy_id(&id, timer);
1348 break;
1349 }
1350 if (timer->tmr_class < id.dev_class)
1351 continue;
1352 if (timer->card->number > id.card) {
1353 snd_timer_user_copy_id(&id, timer);
1354 break;
1355 }
1356 if (timer->card->number < id.card)
1357 continue;
1358 if (timer->tmr_device > id.device) {
1359 snd_timer_user_copy_id(&id, timer);
1360 break;
1361 }
1362 if (timer->tmr_device < id.device)
1363 continue;
1364 if (timer->tmr_subdevice > id.subdevice) {
1365 snd_timer_user_copy_id(&id, timer);
1366 break;
1367 }
1368 if (timer->tmr_subdevice < id.subdevice)
1369 continue;
1370 snd_timer_user_copy_id(&id, timer);
1371 break;
1372 }
1373 if (p == &snd_timer_list)
1374 snd_timer_user_zero_id(&id);
1375 break;
1376 default:
1377 snd_timer_user_zero_id(&id);
1378 }
1379 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001380 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1382 return -EFAULT;
1383 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001386static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001387 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001389 struct snd_timer_ginfo *ginfo;
1390 struct snd_timer_id tid;
1391 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 struct list_head *p;
1393 int err = 0;
1394
1395 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1396 if (! ginfo)
1397 return -ENOMEM;
1398 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1399 kfree(ginfo);
1400 return -EFAULT;
1401 }
1402 tid = ginfo->tid;
1403 memset(ginfo, 0, sizeof(*ginfo));
1404 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001405 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 t = snd_timer_find(&tid);
1407 if (t != NULL) {
1408 ginfo->card = t->card ? t->card->number : -1;
1409 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1410 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1411 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1412 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1413 ginfo->resolution = t->hw.resolution;
1414 if (t->hw.resolution_min > 0) {
1415 ginfo->resolution_min = t->hw.resolution_min;
1416 ginfo->resolution_max = t->hw.resolution_max;
1417 }
1418 list_for_each(p, &t->open_list_head) {
1419 ginfo->clients++;
1420 }
1421 } else {
1422 err = -ENODEV;
1423 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001424 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1426 err = -EFAULT;
1427 kfree(ginfo);
1428 return err;
1429}
1430
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001431static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001432 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001434 struct snd_timer_gparams gparams;
1435 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int err;
1437
1438 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1439 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001440 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001442 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001444 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001446 if (!list_empty(&t->open_list_head)) {
1447 err = -EBUSY;
1448 goto _error;
1449 }
1450 if (!t->hw.set_period) {
1451 err = -ENOSYS;
1452 goto _error;
1453 }
1454 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1455_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001456 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return err;
1458}
1459
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001460static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001461 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001463 struct snd_timer_gstatus gstatus;
1464 struct snd_timer_id tid;
1465 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 int err = 0;
1467
1468 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1469 return -EFAULT;
1470 tid = gstatus.tid;
1471 memset(&gstatus, 0, sizeof(gstatus));
1472 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001473 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 t = snd_timer_find(&tid);
1475 if (t != NULL) {
1476 if (t->hw.c_resolution)
1477 gstatus.resolution = t->hw.c_resolution(t);
1478 else
1479 gstatus.resolution = t->hw.resolution;
1480 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001481 t->hw.precise_resolution(t, &gstatus.resolution_num,
1482 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else {
1484 gstatus.resolution_num = gstatus.resolution;
1485 gstatus.resolution_den = 1000000000uL;
1486 }
1487 } else {
1488 err = -ENODEV;
1489 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001490 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1492 err = -EFAULT;
1493 return err;
1494}
1495
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001496static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001497 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001499 struct snd_timer_user *tu;
1500 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001502 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 tu = file->private_data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001505 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001506 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001508 tu->timeri = NULL;
1509 }
1510 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1511 err = -EFAULT;
1512 goto __err;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 sprintf(str, "application %i", current->pid);
1515 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1516 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001517 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1518 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001519 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Jesper Juhl4d572772005-05-30 17:30:32 +02001521 kfree(tu->queue);
1522 tu->queue = NULL;
1523 kfree(tu->tqueue);
1524 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001526 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001527 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001528 if (tu->tqueue == NULL)
1529 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001531 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001532 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001533 if (tu->queue == NULL)
1534 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001536
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001537 if (err < 0) {
1538 snd_timer_close(tu->timeri);
1539 tu->timeri = NULL;
1540 } else {
1541 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001542 tu->timeri->callback = tu->tread
1543 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001544 tu->timeri->ccallback = snd_timer_user_ccallback;
1545 tu->timeri->callback_data = (void *)tu;
1546 }
1547
1548 __err:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001549 mutex_unlock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001550 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001553static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001554 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001556 struct snd_timer_user *tu;
1557 struct snd_timer_info *info;
1558 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 int err = 0;
1560
1561 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001562 if (!tu->timeri)
1563 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001565 if (!t)
1566 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Takashi Iwaica2c0962005-09-09 14:20:23 +02001568 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (! info)
1570 return -ENOMEM;
1571 info->card = t->card ? t->card->number : -1;
1572 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1573 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1574 strlcpy(info->id, t->id, sizeof(info->id));
1575 strlcpy(info->name, t->name, sizeof(info->name));
1576 info->resolution = t->hw.resolution;
1577 if (copy_to_user(_info, info, sizeof(*_info)))
1578 err = -EFAULT;
1579 kfree(info);
1580 return err;
1581}
1582
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001583static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001584 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001586 struct snd_timer_user *tu;
1587 struct snd_timer_params params;
1588 struct snd_timer *t;
1589 struct snd_timer_read *tr;
1590 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001594 if (!tu->timeri)
1595 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001597 if (!t)
1598 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (copy_from_user(&params, _params, sizeof(params)))
1600 return -EFAULT;
1601 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1602 err = -EINVAL;
1603 goto _end;
1604 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001605 if (params.queue_size > 0 &&
1606 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 err = -EINVAL;
1608 goto _end;
1609 }
1610 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1611 (1<<SNDRV_TIMER_EVENT_TICK)|
1612 (1<<SNDRV_TIMER_EVENT_START)|
1613 (1<<SNDRV_TIMER_EVENT_STOP)|
1614 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1615 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001616 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1617 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 (1<<SNDRV_TIMER_EVENT_MSTART)|
1619 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1620 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001621 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1622 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001623 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 err = -EINVAL;
1625 goto _end;
1626 }
1627 snd_timer_stop(tu->timeri);
1628 spin_lock_irq(&t->lock);
1629 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1630 SNDRV_TIMER_IFLG_EXCLUSIVE|
1631 SNDRV_TIMER_IFLG_EARLY_EVENT);
1632 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1633 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1634 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1635 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1636 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1637 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1638 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001639 if (params.queue_size > 0 &&
1640 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001642 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1643 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (ttr) {
1645 kfree(tu->tqueue);
1646 tu->queue_size = params.queue_size;
1647 tu->tqueue = ttr;
1648 }
1649 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001650 tr = kmalloc(params.queue_size * sizeof(*tr),
1651 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (tr) {
1653 kfree(tu->queue);
1654 tu->queue_size = params.queue_size;
1655 tu->queue = tr;
1656 }
1657 }
1658 }
1659 tu->qhead = tu->qtail = tu->qused = 0;
1660 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1661 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001662 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 tread.event = SNDRV_TIMER_EVENT_EARLY;
1664 tread.tstamp.tv_sec = 0;
1665 tread.tstamp.tv_nsec = 0;
1666 tread.val = 0;
1667 snd_timer_user_append_to_tqueue(tu, &tread);
1668 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001669 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 r->resolution = 0;
1671 r->ticks = 0;
1672 tu->qused++;
1673 tu->qtail++;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 tu->filter = params.filter;
1677 tu->ticks = params.ticks;
1678 err = 0;
1679 _end:
1680 if (copy_to_user(_params, &params, sizeof(params)))
1681 return -EFAULT;
1682 return err;
1683}
1684
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001685static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001686 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001688 struct snd_timer_user *tu;
1689 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001692 if (!tu->timeri)
1693 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 memset(&status, 0, sizeof(status));
1695 status.tstamp = tu->tstamp;
1696 status.resolution = snd_timer_resolution(tu->timeri);
1697 status.lost = tu->timeri->lost;
1698 status.overrun = tu->overrun;
1699 spin_lock_irq(&tu->qlock);
1700 status.queue = tu->qused;
1701 spin_unlock_irq(&tu->qlock);
1702 if (copy_to_user(_status, &status, sizeof(status)))
1703 return -EFAULT;
1704 return 0;
1705}
1706
1707static int snd_timer_user_start(struct file *file)
1708{
1709 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001710 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001713 if (!tu->timeri)
1714 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 snd_timer_stop(tu->timeri);
1716 tu->timeri->lost = 0;
1717 tu->last_resolution = 0;
1718 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1719}
1720
1721static int snd_timer_user_stop(struct file *file)
1722{
1723 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001724 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001727 if (!tu->timeri)
1728 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1730}
1731
1732static int snd_timer_user_continue(struct file *file)
1733{
1734 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001735 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001738 if (!tu->timeri)
1739 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 tu->timeri->lost = 0;
1741 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1742}
1743
Takashi Iwai15790a62005-05-15 15:04:14 +02001744static int snd_timer_user_pause(struct file *file)
1745{
1746 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001747 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001748
Takashi Iwai15790a62005-05-15 15:04:14 +02001749 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001750 if (!tu->timeri)
1751 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001752 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001753}
1754
Takashi Iwai8c50b372005-05-15 15:43:54 +02001755enum {
1756 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1757 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1758 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1759 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1760};
1761
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001762static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1763 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001765 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 void __user *argp = (void __user *)arg;
1767 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 tu = file->private_data;
1770 switch (cmd) {
1771 case SNDRV_TIMER_IOCTL_PVERSION:
1772 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1773 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1774 return snd_timer_user_next_device(argp);
1775 case SNDRV_TIMER_IOCTL_TREAD:
1776 {
1777 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001778
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001779 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001780 if (tu->timeri) { /* too late */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001781 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001783 }
1784 if (get_user(xarg, p)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001785 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 tu->tread = xarg ? 1 : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001789 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791 }
1792 case SNDRV_TIMER_IOCTL_GINFO:
1793 return snd_timer_user_ginfo(file, argp);
1794 case SNDRV_TIMER_IOCTL_GPARAMS:
1795 return snd_timer_user_gparams(file, argp);
1796 case SNDRV_TIMER_IOCTL_GSTATUS:
1797 return snd_timer_user_gstatus(file, argp);
1798 case SNDRV_TIMER_IOCTL_SELECT:
1799 return snd_timer_user_tselect(file, argp);
1800 case SNDRV_TIMER_IOCTL_INFO:
1801 return snd_timer_user_info(file, argp);
1802 case SNDRV_TIMER_IOCTL_PARAMS:
1803 return snd_timer_user_params(file, argp);
1804 case SNDRV_TIMER_IOCTL_STATUS:
1805 return snd_timer_user_status(file, argp);
1806 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001807 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return snd_timer_user_start(file);
1809 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001810 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return snd_timer_user_stop(file);
1812 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001813 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001815 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001816 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001817 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 return -ENOTTY;
1820}
1821
1822static int snd_timer_user_fasync(int fd, struct file * file, int on)
1823{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001824 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 tu = file->private_data;
1828 err = fasync_helper(fd, file, on, &tu->fasync);
1829 if (err < 0)
1830 return err;
1831 return 0;
1832}
1833
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001834static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1835 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001837 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 long result = 0, unit;
1839 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001842 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 spin_lock_irq(&tu->qlock);
1844 while ((long)count - result >= unit) {
1845 while (!tu->qused) {
1846 wait_queue_t wait;
1847
1848 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1849 err = -EAGAIN;
1850 break;
1851 }
1852
1853 set_current_state(TASK_INTERRUPTIBLE);
1854 init_waitqueue_entry(&wait, current);
1855 add_wait_queue(&tu->qchange_sleep, &wait);
1856
1857 spin_unlock_irq(&tu->qlock);
1858 schedule();
1859 spin_lock_irq(&tu->qlock);
1860
1861 remove_wait_queue(&tu->qchange_sleep, &wait);
1862
1863 if (signal_pending(current)) {
1864 err = -ERESTARTSYS;
1865 break;
1866 }
1867 }
1868
1869 spin_unlock_irq(&tu->qlock);
1870 if (err < 0)
1871 goto _error;
1872
1873 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001874 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001875 sizeof(struct snd_timer_tread))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 err = -EFAULT;
1877 goto _error;
1878 }
1879 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001880 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001881 sizeof(struct snd_timer_read))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 err = -EFAULT;
1883 goto _error;
1884 }
1885 }
1886
1887 tu->qhead %= tu->queue_size;
1888
1889 result += unit;
1890 buffer += unit;
1891
1892 spin_lock_irq(&tu->qlock);
1893 tu->qused--;
1894 }
1895 spin_unlock_irq(&tu->qlock);
1896 _error:
1897 return result > 0 ? result : err;
1898}
1899
1900static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1901{
1902 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001903 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 tu = file->private_data;
1906
1907 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 mask = 0;
1910 if (tu->qused)
1911 mask |= POLLIN | POLLRDNORM;
1912
1913 return mask;
1914}
1915
1916#ifdef CONFIG_COMPAT
1917#include "timer_compat.c"
1918#else
1919#define snd_timer_user_ioctl_compat NULL
1920#endif
1921
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001922static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 .owner = THIS_MODULE,
1925 .read = snd_timer_user_read,
1926 .open = snd_timer_user_open,
1927 .release = snd_timer_user_release,
1928 .poll = snd_timer_user_poll,
1929 .unlocked_ioctl = snd_timer_user_ioctl,
1930 .compat_ioctl = snd_timer_user_ioctl_compat,
1931 .fasync = snd_timer_user_fasync,
1932};
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/*
1935 * ENTRY functions
1936 */
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938static int __init alsa_timer_init(void)
1939{
1940 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001943 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1944 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if ((err = snd_timer_register_system()) < 0)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001948 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1949 err);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001950 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1951 &snd_timer_f_ops, NULL, "timer")) < 0)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001952 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1953 err);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001954 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return 0;
1956}
1957
1958static void __exit alsa_timer_exit(void)
1959{
1960 struct list_head *p, *n;
1961
1962 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1963 /* unregister the system timer */
1964 list_for_each_safe(p, n, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001965 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
Takashi Iwaic4614822006-06-23 14:38:23 +02001966 snd_timer_free(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Takashi Iwaie28563c2005-12-01 10:42:42 +01001968 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969#ifdef SNDRV_OSS_INFO_DEV_TIMERS
1970 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1971#endif
1972}
1973
1974module_init(alsa_timer_init)
1975module_exit(alsa_timer_exit)
1976
1977EXPORT_SYMBOL(snd_timer_open);
1978EXPORT_SYMBOL(snd_timer_close);
1979EXPORT_SYMBOL(snd_timer_resolution);
1980EXPORT_SYMBOL(snd_timer_start);
1981EXPORT_SYMBOL(snd_timer_stop);
1982EXPORT_SYMBOL(snd_timer_continue);
1983EXPORT_SYMBOL(snd_timer_pause);
1984EXPORT_SYMBOL(snd_timer_new);
1985EXPORT_SYMBOL(snd_timer_notify);
1986EXPORT_SYMBOL(snd_timer_global_new);
1987EXPORT_SYMBOL(snd_timer_global_free);
1988EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989EXPORT_SYMBOL(snd_timer_interrupt);