blob: 942f36eb69466f4a4a91561996c811824c76dcff [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/time.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010026#include <linux/mutex.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050027#include <linux/device.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040028#include <linux/module.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
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010038#if IS_ENABLED(CONFIG_SND_HRTIMER)
Clemens Ladisch109fef92010-11-18 09:53:54 +010039#define DEFAULT_TIMER_LIMIT 4
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010040#elif IS_ENABLED(CONFIG_SND_RTCTIMER)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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
Kay Sievers03cfe6f2010-11-23 17:43:19 +010056MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
57MODULE_ALIAS("devname:snd/timer");
58
Takashi Iwai53d2f742005-11-17 13:56:05 +010059struct snd_timer_user {
60 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020061 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned long ticks;
63 unsigned long overrun;
64 int qhead;
65 int qtail;
66 int qused;
67 int queue_size;
Takashi Iwai53d2f742005-11-17 13:56:05 +010068 struct snd_timer_read *queue;
69 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 spinlock_t qlock;
71 unsigned long last_resolution;
72 unsigned int filter;
73 struct timespec tstamp; /* trigger tstamp */
74 wait_queue_head_t qchange_sleep;
75 struct fasync_struct *fasync;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010076 struct mutex tread_sem;
Takashi Iwai53d2f742005-11-17 13:56:05 +010077};
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* list of timers */
80static LIST_HEAD(snd_timer_list);
81
82/* list of slave instances */
83static LIST_HEAD(snd_timer_slave_list);
84
85/* lock for slave active lists */
86static DEFINE_SPINLOCK(slave_active_lock);
87
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010088static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwai53d2f742005-11-17 13:56:05 +010090static int snd_timer_free(struct snd_timer *timer);
91static int snd_timer_dev_free(struct snd_device *device);
92static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020093static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Takashi Iwai53d2f742005-11-17 13:56:05 +010095static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*
98 * create a timer instance with the given owner string.
99 * when timer is not NULL, increments the module counter
100 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100101static struct snd_timer_instance *snd_timer_instance_new(char *owner,
102 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100104 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200105 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (timeri == NULL)
107 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700108 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (! timeri->owner) {
110 kfree(timeri);
111 return NULL;
112 }
113 INIT_LIST_HEAD(&timeri->open_list);
114 INIT_LIST_HEAD(&timeri->active_list);
115 INIT_LIST_HEAD(&timeri->ack_list);
116 INIT_LIST_HEAD(&timeri->slave_list_head);
117 INIT_LIST_HEAD(&timeri->slave_active_head);
118
119 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200120 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 kfree(timeri->owner);
122 kfree(timeri);
123 return NULL;
124 }
125
126 return timeri;
127}
128
129/*
130 * find a timer instance from the given timer id
131 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100132static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100134 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Johannes Berg9244b2c2006-10-05 16:02:22 +0200136 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (timer->tmr_class != tid->dev_class)
138 continue;
139 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
140 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
141 (timer->card == NULL ||
142 timer->card->number != tid->card))
143 continue;
144 if (timer->tmr_device != tid->device)
145 continue;
146 if (timer->tmr_subdevice != tid->subdevice)
147 continue;
148 return timer;
149 }
150 return NULL;
151}
152
Johannes Bergee2da992008-07-09 10:28:41 +0200153#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Takashi Iwai53d2f742005-11-17 13:56:05 +0100155static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 switch (tid->dev_class) {
158 case SNDRV_TIMER_CLASS_GLOBAL:
159 if (tid->device < timer_limit)
160 request_module("snd-timer-%i", tid->device);
161 break;
162 case SNDRV_TIMER_CLASS_CARD:
163 case SNDRV_TIMER_CLASS_PCM:
164 if (tid->card < snd_ecards_limit)
165 request_module("snd-card-%i", tid->card);
166 break;
167 default:
168 break;
169 }
170}
171
172#endif
173
174/*
175 * look for a master instance matching with the slave id of the given slave.
176 * when found, relink the open_link of the slave.
177 *
178 * call this with register_mutex down.
179 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100180static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100182 struct snd_timer *timer;
183 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200186 list_for_each_entry(timer, &snd_timer_list, device_list) {
187 list_for_each_entry(master, &timer->open_list_head, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (slave->slave_class == master->slave_class &&
189 slave->slave_id == master->slave_id) {
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100190 list_move_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);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100218 spin_lock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 slave->master = master;
220 slave->timer = master->timer;
221 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200222 list_add_tail(&slave->active_list,
223 &master->slave_active_head);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100224 spin_unlock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 spin_unlock_irq(&slave_active_lock);
226 }
227 }
228}
229
230/*
231 * open a timer instance
232 * when opening a master, the slave id must be here given.
233 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100234int snd_timer_open(struct snd_timer_instance **ti,
235 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 unsigned int slave_id)
237{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100238 struct snd_timer *timer;
239 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
242 /* open a slave instance */
243 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
244 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100245 pr_debug("ALSA: timer: invalid slave class %i\n",
246 tid->dev_sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -EINVAL;
248 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100249 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200251 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100252 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200253 return -ENOMEM;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 timeri->slave_class = tid->dev_sclass;
256 timeri->slave_id = tid->device;
257 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
258 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
259 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100260 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *ti = timeri;
262 return 0;
263 }
264
265 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100266 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 timer = snd_timer_find(tid);
Johannes Bergee2da992008-07-09 10:28:41 +0200268#ifdef CONFIG_MODULES
269 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100270 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100272 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 timer = snd_timer_find(tid);
274 }
275#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200276 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100277 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -ENODEV;
279 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200280 if (!list_empty(&timer->open_list_head)) {
281 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100282 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200283 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100284 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200285 return -EBUSY;
286 }
287 }
288 timeri = snd_timer_instance_new(owner, timer);
289 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100290 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200291 return -ENOMEM;
292 }
293 timeri->slave_class = tid->dev_sclass;
294 timeri->slave_id = slave_id;
295 if (list_empty(&timer->open_list_head) && timer->hw.open)
296 timer->hw.open(timer);
297 list_add_tail(&timeri->open_list, &timer->open_list_head);
298 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100299 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 *ti = timeri;
301 return 0;
302}
303
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100304static int _snd_timer_stop(struct snd_timer_instance *timeri, int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/*
307 * close a timer instance
308 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100309int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100311 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200312 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Takashi Iwai00728892008-08-14 07:51:57 +0200314 if (snd_BUG_ON(!timeri))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200315 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* force to stop the timer */
318 snd_timer_stop(timeri);
319
320 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
321 /* wait, until the active callback is finished */
322 spin_lock_irq(&slave_active_lock);
323 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
324 spin_unlock_irq(&slave_active_lock);
325 udelay(10);
326 spin_lock_irq(&slave_active_lock);
327 }
328 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100329 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 list_del(&timeri->open_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100331 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 } else {
333 timer = timeri->timer;
Takashi Iwai94094c82011-08-08 12:28:22 +0200334 if (snd_BUG_ON(!timer))
335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* wait, until the active callback is finished */
337 spin_lock_irq(&timer->lock);
338 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
339 spin_unlock_irq(&timer->lock);
340 udelay(10);
341 spin_lock_irq(&timer->lock);
342 }
343 spin_unlock_irq(&timer->lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100344 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 list_del(&timeri->open_list);
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100346 if (list_empty(&timer->open_list_head) &&
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200347 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 timer->hw.close(timer);
349 /* remove slave links */
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100350 spin_lock_irq(&slave_active_lock);
351 spin_lock(&timer->lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200352 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
353 open_list) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200354 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 slave->master = NULL;
356 slave->timer = NULL;
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100357 list_del_init(&slave->ack_list);
358 list_del_init(&slave->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100360 spin_unlock(&timer->lock);
361 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100362 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Takashi Iwai94094c82011-08-08 12:28:22 +0200364 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (timeri->private_free)
366 timeri->private_free(timeri);
367 kfree(timeri->owner);
368 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200369 if (timer)
370 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372}
373
Takashi Iwai53d2f742005-11-17 13:56:05 +0100374unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100376 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (timeri == NULL)
379 return 0;
380 if ((timer = timeri->timer) != NULL) {
381 if (timer->hw.c_resolution)
382 return timer->hw.c_resolution(timer);
383 return timer->hw.resolution;
384 }
385 return 0;
386}
387
Takashi Iwai53d2f742005-11-17 13:56:05 +0100388static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100390 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned long flags;
392 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100393 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct timespec tstamp;
395
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100396 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +0000397 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100398 else
399 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200400 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
401 event > SNDRV_TIMER_EVENT_PAUSE))
402 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200403 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)
Jaroslav Kyselab30477d2010-03-03 11:05:55 +0100407 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 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);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200416 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (ts->ccallback)
418 ts->ccallback(ti, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 spin_unlock_irqrestore(&timer->lock, flags);
420}
421
Takashi Iwai53d2f742005-11-17 13:56:05 +0100422static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200423 unsigned long sticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100425 list_move_tail(&timeri->active_list, &timer->active_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (timer->running) {
427 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
428 goto __start_now;
429 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
430 timeri->flags |= SNDRV_TIMER_IFLG_START;
431 return 1; /* delayed start */
432 } else {
433 timer->sticks = sticks;
434 timer->hw.start(timer);
435 __start_now:
436 timer->running++;
437 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
438 return 0;
439 }
440}
441
Takashi Iwai53d2f742005-11-17 13:56:05 +0100442static int snd_timer_start_slave(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 unsigned long flags;
445
446 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100447 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
448 spin_unlock_irqrestore(&slave_active_lock, flags);
449 return -EBUSY;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100452 if (timeri->master && timeri->timer) {
453 spin_lock(&timeri->timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200454 list_add_tail(&timeri->active_list,
455 &timeri->master->slave_active_head);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100456 spin_unlock(&timeri->timer->lock);
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 spin_unlock_irqrestore(&slave_active_lock, flags);
459 return 1; /* delayed start */
460}
461
462/*
463 * start the timer instance
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200464 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100465int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100467 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int result = -EINVAL;
469 unsigned long flags;
470
471 if (timeri == NULL || ticks < 1)
472 return -EINVAL;
473 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
474 result = snd_timer_start_slave(timeri);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100475 if (result >= 0)
476 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return result;
478 }
479 timer = timeri->timer;
480 if (timer == NULL)
481 return -EINVAL;
482 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100483 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
484 SNDRV_TIMER_IFLG_START)) {
485 result = -EBUSY;
486 goto unlock;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 timeri->ticks = timeri->cticks = ticks;
489 timeri->pticks = 0;
490 result = snd_timer_start1(timer, timeri, ticks);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100491 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 spin_unlock_irqrestore(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100493 if (result >= 0)
494 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return result;
496}
497
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100498static int _snd_timer_stop(struct snd_timer_instance *timeri, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100500 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned long flags;
502
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200503 if (snd_BUG_ON(!timeri))
504 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100507 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100508 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
509 spin_unlock_irqrestore(&slave_active_lock, flags);
510 return -EBUSY;
511 }
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100512 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
513 list_del_init(&timeri->ack_list);
514 list_del_init(&timeri->active_list);
515 spin_unlock_irqrestore(&slave_active_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto __end;
517 }
518 timer = timeri->timer;
519 if (!timer)
520 return -EINVAL;
521 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100522 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
523 SNDRV_TIMER_IFLG_START))) {
524 spin_unlock_irqrestore(&timer->lock, flags);
525 return -EBUSY;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 list_del_init(&timeri->ack_list);
528 list_del_init(&timeri->active_list);
529 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
530 !(--timer->running)) {
531 timer->hw.stop(timer);
532 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
533 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
534 snd_timer_reschedule(timer, 0);
535 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
536 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
537 timer->hw.start(timer);
538 }
539 }
540 }
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100541 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 spin_unlock_irqrestore(&timer->lock, flags);
543 __end:
544 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
545 snd_timer_notify1(timeri, event);
546 return 0;
547}
548
549/*
550 * stop the timer instance.
551 *
552 * do not call this from the timer callback!
553 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100554int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100556 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 unsigned long flags;
558 int err;
559
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100560 err = _snd_timer_stop(timeri, SNDRV_TIMER_EVENT_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (err < 0)
562 return err;
563 timer = timeri->timer;
Takashi Iwai0584ffa2011-08-08 12:24:46 +0200564 if (!timer)
565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_lock_irqsave(&timer->lock, flags);
567 timeri->cticks = timeri->ticks;
568 timeri->pticks = 0;
569 spin_unlock_irqrestore(&timer->lock, flags);
570 return 0;
571}
572
573/*
574 * start again.. the tick is kept.
575 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100576int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100578 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int result = -EINVAL;
580 unsigned long flags;
581
582 if (timeri == NULL)
583 return result;
584 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
585 return snd_timer_start_slave(timeri);
586 timer = timeri->timer;
587 if (! timer)
588 return -EINVAL;
589 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100590 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
591 result = -EBUSY;
592 goto unlock;
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!timeri->cticks)
595 timeri->cticks = 1;
596 timeri->pticks = 0;
597 result = snd_timer_start1(timer, timeri, timer->sticks);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100598 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_unlock_irqrestore(&timer->lock, flags);
600 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
601 return result;
602}
603
604/*
605 * pause.. remember the ticks left
606 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100607int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100609 return _snd_timer_stop(timeri, SNDRV_TIMER_EVENT_PAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/*
613 * reschedule the timer
614 *
615 * start pending instances and check the scheduling ticks.
616 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
617 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100618static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100620 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Johannes Berg9244b2c2006-10-05 16:02:22 +0200623 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (ti->flags & SNDRV_TIMER_IFLG_START) {
625 ti->flags &= ~SNDRV_TIMER_IFLG_START;
626 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
627 timer->running++;
628 }
629 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
630 if (ticks > ti->cticks)
631 ticks = ti->cticks;
632 }
633 }
634 if (ticks == ~0UL) {
635 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
636 return;
637 }
638 if (ticks > timer->hw.ticks)
639 ticks = timer->hw.ticks;
640 if (ticks_left != ticks)
641 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
642 timer->sticks = ticks;
643}
644
645/*
646 * timer tasklet
647 *
648 */
649static void snd_timer_tasklet(unsigned long arg)
650{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100651 struct snd_timer *timer = (struct snd_timer *) arg;
652 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct list_head *p;
654 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200655 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Takashi Iwai2999ff52006-07-05 17:16:58 +0200657 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* now process all callbacks */
659 while (!list_empty(&timer->sack_list_head)) {
660 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100661 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /* remove from ack_list and make empty */
664 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 ticks = ti->pticks;
667 ti->pticks = 0;
668 resolution = ti->resolution;
669
670 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
671 spin_unlock(&timer->lock);
672 if (ti->callback)
673 ti->callback(ti, resolution, ticks);
674 spin_lock(&timer->lock);
675 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
676 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200677 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680/*
681 * timer interrupt
682 *
683 * ticks_left is usually equal to timer->sticks.
684 *
685 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100686void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200688 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200690 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100691 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int use_tasklet = 0;
693
694 if (timer == NULL)
695 return;
696
Takashi Iwaib32425a2005-11-18 18:52:14 +0100697 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 /* remember the current resolution */
700 if (timer->hw.c_resolution)
701 resolution = timer->hw.c_resolution(timer);
702 else
703 resolution = timer->hw.resolution;
704
705 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200706 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200707 * processed instance is relinked to done_list_head before the callback
708 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200710 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
711 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
713 continue;
714 ti->pticks += ticks_left;
715 ti->resolution = resolution;
716 if (ti->cticks < ticks_left)
717 ti->cticks = 0;
718 else
719 ti->cticks -= ticks_left;
720 if (ti->cticks) /* not expired */
721 continue;
722 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
723 ti->cticks = ti->ticks;
724 } else {
725 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai2562c292016-02-04 17:06:13 +0100726 --timer->running;
727 list_del_init(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200729 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
730 (ti->flags & SNDRV_TIMER_IFLG_FAST))
731 ack_list_head = &timer->ack_list_head;
732 else
733 ack_list_head = &timer->sack_list_head;
734 if (list_empty(&ti->ack_list))
735 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200736 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ts->pticks = ti->pticks;
738 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200739 if (list_empty(&ts->ack_list))
740 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742 }
743 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200744 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (timer->running) {
746 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
747 timer->hw.stop(timer);
748 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
749 }
750 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
751 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
752 /* restart timer */
753 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
754 timer->hw.start(timer);
755 }
756 } else {
757 timer->hw.stop(timer);
758 }
759
760 /* now process all fast callbacks */
761 while (!list_empty(&timer->ack_list_head)) {
762 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100763 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /* remove from ack_list and make empty */
766 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 ticks = ti->pticks;
769 ti->pticks = 0;
770
771 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
772 spin_unlock(&timer->lock);
773 if (ti->callback)
774 ti->callback(ti, resolution, ticks);
775 spin_lock(&timer->lock);
776 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
777 }
778
779 /* do we have any slow callbacks? */
780 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100781 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100784 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
788
789 */
790
Takashi Iwai53d2f742005-11-17 13:56:05 +0100791int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
792 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100794 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100796 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 .dev_free = snd_timer_dev_free,
798 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200799 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 };
801
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200802 if (snd_BUG_ON(!tid))
803 return -EINVAL;
804 if (rtimer)
805 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200806 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100807 if (timer == NULL) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100808 pr_err("ALSA: timer: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 timer->tmr_class = tid->dev_class;
812 timer->card = card;
813 timer->tmr_device = tid->device;
814 timer->tmr_subdevice = tid->subdevice;
815 if (id)
816 strlcpy(timer->id, id, sizeof(timer->id));
817 INIT_LIST_HEAD(&timer->device_list);
818 INIT_LIST_HEAD(&timer->open_list_head);
819 INIT_LIST_HEAD(&timer->active_list_head);
820 INIT_LIST_HEAD(&timer->ack_list_head);
821 INIT_LIST_HEAD(&timer->sack_list_head);
822 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200823 tasklet_init(&timer->task_queue, snd_timer_tasklet,
824 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200826 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200827 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
828 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 snd_timer_free(timer);
830 return err;
831 }
832 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200833 if (rtimer)
834 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 0;
836}
837
Takashi Iwai53d2f742005-11-17 13:56:05 +0100838static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200840 if (!timer)
841 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200842
843 mutex_lock(&register_mutex);
844 if (! list_empty(&timer->open_list_head)) {
845 struct list_head *p, *n;
846 struct snd_timer_instance *ti;
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100847 pr_warn("ALSA: timer %p is busy?\n", timer);
Takashi Iwaic4614822006-06-23 14:38:23 +0200848 list_for_each_safe(p, n, &timer->open_list_head) {
849 list_del_init(p);
850 ti = list_entry(p, struct snd_timer_instance, open_list);
851 ti->timer = NULL;
852 }
853 }
854 list_del(&timer->device_list);
855 mutex_unlock(&register_mutex);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (timer->private_free)
858 timer->private_free(timer);
859 kfree(timer);
860 return 0;
861}
862
Takashi Iwai53d2f742005-11-17 13:56:05 +0100863static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100865 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return snd_timer_free(timer);
867}
868
Takashi Iwai53d2f742005-11-17 13:56:05 +0100869static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100871 struct snd_timer *timer = dev->device_data;
872 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200874 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
875 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
877 !timer->hw.resolution && timer->hw.c_resolution == NULL)
878 return -EINVAL;
879
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100880 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200881 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (timer1->tmr_class > timer->tmr_class)
883 break;
884 if (timer1->tmr_class < timer->tmr_class)
885 continue;
886 if (timer1->card && timer->card) {
887 if (timer1->card->number > timer->card->number)
888 break;
889 if (timer1->card->number < timer->card->number)
890 continue;
891 }
892 if (timer1->tmr_device > timer->tmr_device)
893 break;
894 if (timer1->tmr_device < timer->tmr_device)
895 continue;
896 if (timer1->tmr_subdevice > timer->tmr_subdevice)
897 break;
898 if (timer1->tmr_subdevice < timer->tmr_subdevice)
899 continue;
900 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100901 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -EBUSY;
903 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200904 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100905 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
907}
908
Takashi Iwaic4614822006-06-23 14:38:23 +0200909static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100911 struct snd_timer *timer = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +0200912 mutex_lock(&register_mutex);
913 list_del_init(&timer->device_list);
914 mutex_unlock(&register_mutex);
915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Takashi Iwai53d2f742005-11-17 13:56:05 +0100918void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 unsigned long flags;
921 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100922 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200924 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
925 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200926 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
927 event > SNDRV_TIMER_EVENT_MRESUME))
928 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200930 if (event == SNDRV_TIMER_EVENT_MSTART ||
931 event == SNDRV_TIMER_EVENT_MCONTINUE ||
932 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (timer->hw.c_resolution)
934 resolution = timer->hw.c_resolution(timer);
935 else
936 resolution = timer->hw.resolution;
937 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200938 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (ti->ccallback)
940 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200941 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (ts->ccallback)
943 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 spin_unlock_irqrestore(&timer->lock, flags);
946}
947
948/*
949 * exported functions for global timers
950 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100951int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100953 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
956 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
957 tid.card = -1;
958 tid.device = device;
959 tid.subdevice = 0;
960 return snd_timer_new(NULL, id, &tid, rtimer);
961}
962
Takashi Iwai53d2f742005-11-17 13:56:05 +0100963int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 return snd_timer_free(timer);
966}
967
Takashi Iwai53d2f742005-11-17 13:56:05 +0100968int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100970 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 memset(&dev, 0, sizeof(dev));
973 dev.device_data = timer;
974 return snd_timer_dev_register(&dev);
975}
976
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200977/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * System timer
979 */
980
981struct snd_timer_system_private {
982 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 unsigned long last_expires;
984 unsigned long last_jiffies;
985 unsigned long correction;
986};
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static void snd_timer_s_function(unsigned long data)
989{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100990 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct snd_timer_system_private *priv = timer->private_data;
992 unsigned long jiff = jiffies;
993 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +0200994 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
996}
997
Takashi Iwai53d2f742005-11-17 13:56:05 +0100998static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct snd_timer_system_private *priv;
1001 unsigned long njiff;
1002
1003 priv = (struct snd_timer_system_private *) timer->private_data;
1004 njiff = (priv->last_jiffies = jiffies);
1005 if (priv->correction > timer->sticks - 1) {
1006 priv->correction -= timer->sticks - 1;
1007 njiff++;
1008 } else {
1009 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +02001010 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 priv->last_expires = priv->tlist.expires = njiff;
1013 add_timer(&priv->tlist);
1014 return 0;
1015}
1016
Takashi Iwai53d2f742005-11-17 13:56:05 +01001017static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct snd_timer_system_private *priv;
1020 unsigned long jiff;
1021
1022 priv = (struct snd_timer_system_private *) timer->private_data;
1023 del_timer(&priv->tlist);
1024 jiff = jiffies;
1025 if (time_before(jiff, priv->last_expires))
1026 timer->sticks = priv->last_expires - jiff;
1027 else
1028 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001029 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return 0;
1031}
1032
Takashi Iwai53d2f742005-11-17 13:56:05 +01001033static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1036 .resolution = 1000000000L / HZ,
1037 .ticks = 10000000L,
1038 .start = snd_timer_s_start,
1039 .stop = snd_timer_s_stop
1040};
1041
Takashi Iwai53d2f742005-11-17 13:56:05 +01001042static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 kfree(timer->private_data);
1045}
1046
1047static int snd_timer_register_system(void)
1048{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001049 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct snd_timer_system_private *priv;
1051 int err;
1052
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001053 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1054 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return err;
1056 strcpy(timer->name, "system timer");
1057 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001058 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (priv == NULL) {
1060 snd_timer_free(timer);
1061 return -ENOMEM;
1062 }
1063 init_timer(&priv->tlist);
1064 priv->tlist.function = snd_timer_s_function;
1065 priv->tlist.data = (unsigned long) timer;
1066 timer->private_data = priv;
1067 timer->private_free = snd_timer_free_system;
1068 return snd_timer_global_register(timer);
1069}
1070
Takashi Iwaie28563c2005-12-01 10:42:42 +01001071#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/*
1073 * Info interface
1074 */
1075
Takashi Iwai53d2f742005-11-17 13:56:05 +01001076static void snd_timer_proc_read(struct snd_info_entry *entry,
1077 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001079 struct snd_timer *timer;
1080 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001082 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001083 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 switch (timer->tmr_class) {
1085 case SNDRV_TIMER_CLASS_GLOBAL:
1086 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1087 break;
1088 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001089 snd_iprintf(buffer, "C%i-%i: ",
1090 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
1092 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001093 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1094 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 break;
1096 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001097 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1098 timer->card ? timer->card->number : -1,
1099 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101 snd_iprintf(buffer, "%s :", timer->name);
1102 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001103 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1104 timer->hw.resolution / 1000,
1105 timer->hw.resolution % 1000,
1106 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1108 snd_iprintf(buffer, " SLAVE");
1109 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001110 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001111 snd_iprintf(buffer, " Client %s : %s\n",
1112 ti->owner ? ti->owner : "unknown",
1113 ti->flags & (SNDRV_TIMER_IFLG_START |
1114 SNDRV_TIMER_IFLG_RUNNING)
1115 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001117 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001120static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001121
1122static void __init snd_timer_proc_init(void)
1123{
1124 struct snd_info_entry *entry;
1125
1126 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1127 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001128 entry->c.text.read = snd_timer_proc_read;
1129 if (snd_info_register(entry) < 0) {
1130 snd_info_free_entry(entry);
1131 entry = NULL;
1132 }
1133 }
1134 snd_timer_proc_entry = entry;
1135}
1136
1137static void __exit snd_timer_proc_done(void)
1138{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001139 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001140}
1141#else /* !CONFIG_PROC_FS */
1142#define snd_timer_proc_init()
1143#define snd_timer_proc_done()
1144#endif
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/*
1147 * USER SPACE interface
1148 */
1149
Takashi Iwai53d2f742005-11-17 13:56:05 +01001150static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 unsigned long resolution,
1152 unsigned long ticks)
1153{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001154 struct snd_timer_user *tu = timeri->callback_data;
1155 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 spin_lock(&tu->qlock);
1159 if (tu->qused > 0) {
1160 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1161 r = &tu->queue[prev];
1162 if (r->resolution == resolution) {
1163 r->ticks += ticks;
1164 goto __wake;
1165 }
1166 }
1167 if (tu->qused >= tu->queue_size) {
1168 tu->overrun++;
1169 } else {
1170 r = &tu->queue[tu->qtail++];
1171 tu->qtail %= tu->queue_size;
1172 r->resolution = resolution;
1173 r->ticks = ticks;
1174 tu->qused++;
1175 }
1176 __wake:
1177 spin_unlock(&tu->qlock);
1178 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1179 wake_up(&tu->qchange_sleep);
1180}
1181
Takashi Iwai53d2f742005-11-17 13:56:05 +01001182static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1183 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 if (tu->qused >= tu->queue_size) {
1186 tu->overrun++;
1187 } else {
1188 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1189 tu->qtail %= tu->queue_size;
1190 tu->qused++;
1191 }
1192}
1193
Takashi Iwai53d2f742005-11-17 13:56:05 +01001194static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1195 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct timespec *tstamp,
1197 unsigned long resolution)
1198{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001199 struct snd_timer_user *tu = timeri->callback_data;
1200 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001201 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001203 if (event >= SNDRV_TIMER_EVENT_START &&
1204 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 tu->tstamp = *tstamp;
1206 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1207 return;
1208 r1.event = event;
1209 r1.tstamp = *tstamp;
1210 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001211 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001213 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1215 wake_up(&tu->qchange_sleep);
1216}
1217
Takashi Iwai53d2f742005-11-17 13:56:05 +01001218static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 unsigned long resolution,
1220 unsigned long ticks)
1221{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001222 struct snd_timer_user *tu = timeri->callback_data;
1223 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct timespec tstamp;
1225 int prev, append = 0;
1226
Takashi Iwai07799e72005-10-10 11:49:49 +02001227 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001229 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1230 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 spin_unlock(&tu->qlock);
1232 return;
1233 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001234 if (tu->last_resolution != resolution || ticks > 0) {
1235 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +00001236 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001237 else
1238 getnstimeofday(&tstamp);
1239 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001240 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1241 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1243 r1.tstamp = tstamp;
1244 r1.val = resolution;
1245 snd_timer_user_append_to_tqueue(tu, &r1);
1246 tu->last_resolution = resolution;
1247 append++;
1248 }
1249 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1250 goto __wake;
1251 if (ticks == 0)
1252 goto __wake;
1253 if (tu->qused > 0) {
1254 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1255 r = &tu->tqueue[prev];
1256 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1257 r->tstamp = tstamp;
1258 r->val += ticks;
1259 append++;
1260 goto __wake;
1261 }
1262 }
1263 r1.event = SNDRV_TIMER_EVENT_TICK;
1264 r1.tstamp = tstamp;
1265 r1.val = ticks;
1266 snd_timer_user_append_to_tqueue(tu, &r1);
1267 append++;
1268 __wake:
1269 spin_unlock(&tu->qlock);
1270 if (append == 0)
1271 return;
1272 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1273 wake_up(&tu->qchange_sleep);
1274}
1275
1276static int snd_timer_user_open(struct inode *inode, struct file *file)
1277{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001278 struct snd_timer_user *tu;
Takashi Iwai02f48652010-04-13 11:49:04 +02001279 int err;
1280
1281 err = nonseekable_open(inode, file);
1282 if (err < 0)
1283 return err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001284
Takashi Iwaica2c0962005-09-09 14:20:23 +02001285 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (tu == NULL)
1287 return -ENOMEM;
1288 spin_lock_init(&tu->qlock);
1289 init_waitqueue_head(&tu->qchange_sleep);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001290 mutex_init(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 tu->ticks = 1;
1292 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001293 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001294 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (tu->queue == NULL) {
1296 kfree(tu);
1297 return -ENOMEM;
1298 }
1299 file->private_data = tu;
1300 return 0;
1301}
1302
1303static int snd_timer_user_release(struct inode *inode, struct file *file)
1304{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001305 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (file->private_data) {
1308 tu = file->private_data;
1309 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (tu->timeri)
1311 snd_timer_close(tu->timeri);
1312 kfree(tu->queue);
1313 kfree(tu->tqueue);
1314 kfree(tu);
1315 }
1316 return 0;
1317}
1318
Takashi Iwai53d2f742005-11-17 13:56:05 +01001319static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1322 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1323 id->card = -1;
1324 id->device = -1;
1325 id->subdevice = -1;
1326}
1327
Takashi Iwai53d2f742005-11-17 13:56:05 +01001328static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 id->dev_class = timer->tmr_class;
1331 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1332 id->card = timer->card ? timer->card->number : -1;
1333 id->device = timer->tmr_device;
1334 id->subdevice = timer->tmr_subdevice;
1335}
1336
Takashi Iwai53d2f742005-11-17 13:56:05 +01001337static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001339 struct snd_timer_id id;
1340 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (copy_from_user(&id, _tid, sizeof(id)))
1344 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001345 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (id.dev_class < 0) { /* first item */
1347 if (list_empty(&snd_timer_list))
1348 snd_timer_user_zero_id(&id);
1349 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001350 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001351 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 snd_timer_user_copy_id(&id, timer);
1353 }
1354 } else {
1355 switch (id.dev_class) {
1356 case SNDRV_TIMER_CLASS_GLOBAL:
1357 id.device = id.device < 0 ? 0 : id.device + 1;
1358 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001359 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1361 snd_timer_user_copy_id(&id, timer);
1362 break;
1363 }
1364 if (timer->tmr_device >= id.device) {
1365 snd_timer_user_copy_id(&id, timer);
1366 break;
1367 }
1368 }
1369 if (p == &snd_timer_list)
1370 snd_timer_user_zero_id(&id);
1371 break;
1372 case SNDRV_TIMER_CLASS_CARD:
1373 case SNDRV_TIMER_CLASS_PCM:
1374 if (id.card < 0) {
1375 id.card = 0;
1376 } else {
1377 if (id.card < 0) {
1378 id.card = 0;
1379 } else {
1380 if (id.device < 0) {
1381 id.device = 0;
1382 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001383 if (id.subdevice < 0) {
1384 id.subdevice = 0;
1385 } else {
1386 id.subdevice++;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 }
1390 }
1391 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001392 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (timer->tmr_class > id.dev_class) {
1394 snd_timer_user_copy_id(&id, timer);
1395 break;
1396 }
1397 if (timer->tmr_class < id.dev_class)
1398 continue;
1399 if (timer->card->number > id.card) {
1400 snd_timer_user_copy_id(&id, timer);
1401 break;
1402 }
1403 if (timer->card->number < id.card)
1404 continue;
1405 if (timer->tmr_device > id.device) {
1406 snd_timer_user_copy_id(&id, timer);
1407 break;
1408 }
1409 if (timer->tmr_device < id.device)
1410 continue;
1411 if (timer->tmr_subdevice > id.subdevice) {
1412 snd_timer_user_copy_id(&id, timer);
1413 break;
1414 }
1415 if (timer->tmr_subdevice < id.subdevice)
1416 continue;
1417 snd_timer_user_copy_id(&id, timer);
1418 break;
1419 }
1420 if (p == &snd_timer_list)
1421 snd_timer_user_zero_id(&id);
1422 break;
1423 default:
1424 snd_timer_user_zero_id(&id);
1425 }
1426 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001427 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1429 return -EFAULT;
1430 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001433static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001434 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001436 struct snd_timer_ginfo *ginfo;
1437 struct snd_timer_id tid;
1438 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct list_head *p;
1440 int err = 0;
1441
Li Zefanef44a1e2009-04-10 09:43:08 +08001442 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1443 if (IS_ERR(ginfo))
1444 return PTR_ERR(ginfo);
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 tid = ginfo->tid;
1447 memset(ginfo, 0, sizeof(*ginfo));
1448 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001449 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 t = snd_timer_find(&tid);
1451 if (t != NULL) {
1452 ginfo->card = t->card ? t->card->number : -1;
1453 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1454 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1455 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1456 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1457 ginfo->resolution = t->hw.resolution;
1458 if (t->hw.resolution_min > 0) {
1459 ginfo->resolution_min = t->hw.resolution_min;
1460 ginfo->resolution_max = t->hw.resolution_max;
1461 }
1462 list_for_each(p, &t->open_list_head) {
1463 ginfo->clients++;
1464 }
1465 } else {
1466 err = -ENODEV;
1467 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001468 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1470 err = -EFAULT;
1471 kfree(ginfo);
1472 return err;
1473}
1474
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001475static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001476 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001478 struct snd_timer_gparams gparams;
1479 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 int err;
1481
1482 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1483 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001484 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001486 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001488 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001490 if (!list_empty(&t->open_list_head)) {
1491 err = -EBUSY;
1492 goto _error;
1493 }
1494 if (!t->hw.set_period) {
1495 err = -ENOSYS;
1496 goto _error;
1497 }
1498 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1499_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001500 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return err;
1502}
1503
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001504static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001505 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001507 struct snd_timer_gstatus gstatus;
1508 struct snd_timer_id tid;
1509 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int err = 0;
1511
1512 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1513 return -EFAULT;
1514 tid = gstatus.tid;
1515 memset(&gstatus, 0, sizeof(gstatus));
1516 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001517 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 t = snd_timer_find(&tid);
1519 if (t != NULL) {
1520 if (t->hw.c_resolution)
1521 gstatus.resolution = t->hw.c_resolution(t);
1522 else
1523 gstatus.resolution = t->hw.resolution;
1524 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001525 t->hw.precise_resolution(t, &gstatus.resolution_num,
1526 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 } else {
1528 gstatus.resolution_num = gstatus.resolution;
1529 gstatus.resolution_den = 1000000000uL;
1530 }
1531 } else {
1532 err = -ENODEV;
1533 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001534 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1536 err = -EFAULT;
1537 return err;
1538}
1539
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001540static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001541 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001543 struct snd_timer_user *tu;
1544 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001546 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 tu = file->private_data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001549 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001550 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001552 tu->timeri = NULL;
1553 }
1554 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1555 err = -EFAULT;
1556 goto __err;
1557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 sprintf(str, "application %i", current->pid);
1559 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1560 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001561 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1562 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001563 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Jesper Juhl4d572772005-05-30 17:30:32 +02001565 kfree(tu->queue);
1566 tu->queue = NULL;
1567 kfree(tu->tqueue);
1568 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001570 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001571 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001572 if (tu->tqueue == NULL)
1573 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001575 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001576 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001577 if (tu->queue == NULL)
1578 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001580
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001581 if (err < 0) {
1582 snd_timer_close(tu->timeri);
1583 tu->timeri = NULL;
1584 } else {
1585 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001586 tu->timeri->callback = tu->tread
1587 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001588 tu->timeri->ccallback = snd_timer_user_ccallback;
1589 tu->timeri->callback_data = (void *)tu;
1590 }
1591
1592 __err:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001593 mutex_unlock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001597static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001598 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001600 struct snd_timer_user *tu;
1601 struct snd_timer_info *info;
1602 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int err = 0;
1604
1605 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001606 if (!tu->timeri)
1607 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001609 if (!t)
1610 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Takashi Iwaica2c0962005-09-09 14:20:23 +02001612 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (! info)
1614 return -ENOMEM;
1615 info->card = t->card ? t->card->number : -1;
1616 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1617 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1618 strlcpy(info->id, t->id, sizeof(info->id));
1619 strlcpy(info->name, t->name, sizeof(info->name));
1620 info->resolution = t->hw.resolution;
1621 if (copy_to_user(_info, info, sizeof(*_info)))
1622 err = -EFAULT;
1623 kfree(info);
1624 return err;
1625}
1626
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001627static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001628 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001630 struct snd_timer_user *tu;
1631 struct snd_timer_params params;
1632 struct snd_timer *t;
1633 struct snd_timer_read *tr;
1634 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001638 if (!tu->timeri)
1639 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001641 if (!t)
1642 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (copy_from_user(&params, _params, sizeof(params)))
1644 return -EFAULT;
1645 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1646 err = -EINVAL;
1647 goto _end;
1648 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001649 if (params.queue_size > 0 &&
1650 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 err = -EINVAL;
1652 goto _end;
1653 }
1654 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1655 (1<<SNDRV_TIMER_EVENT_TICK)|
1656 (1<<SNDRV_TIMER_EVENT_START)|
1657 (1<<SNDRV_TIMER_EVENT_STOP)|
1658 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1659 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001660 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1661 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 (1<<SNDRV_TIMER_EVENT_MSTART)|
1663 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1664 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001665 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1666 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001667 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 err = -EINVAL;
1669 goto _end;
1670 }
1671 snd_timer_stop(tu->timeri);
1672 spin_lock_irq(&t->lock);
1673 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1674 SNDRV_TIMER_IFLG_EXCLUSIVE|
1675 SNDRV_TIMER_IFLG_EARLY_EVENT);
1676 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1677 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1678 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1679 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1680 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1681 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1682 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001683 if (params.queue_size > 0 &&
1684 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001686 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1687 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (ttr) {
1689 kfree(tu->tqueue);
1690 tu->queue_size = params.queue_size;
1691 tu->tqueue = ttr;
1692 }
1693 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001694 tr = kmalloc(params.queue_size * sizeof(*tr),
1695 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (tr) {
1697 kfree(tu->queue);
1698 tu->queue_size = params.queue_size;
1699 tu->queue = tr;
1700 }
1701 }
1702 }
1703 tu->qhead = tu->qtail = tu->qused = 0;
1704 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1705 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001706 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 tread.event = SNDRV_TIMER_EVENT_EARLY;
1708 tread.tstamp.tv_sec = 0;
1709 tread.tstamp.tv_nsec = 0;
1710 tread.val = 0;
1711 snd_timer_user_append_to_tqueue(tu, &tread);
1712 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001713 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 r->resolution = 0;
1715 r->ticks = 0;
1716 tu->qused++;
1717 tu->qtail++;
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720 tu->filter = params.filter;
1721 tu->ticks = params.ticks;
1722 err = 0;
1723 _end:
1724 if (copy_to_user(_params, &params, sizeof(params)))
1725 return -EFAULT;
1726 return err;
1727}
1728
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001729static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001730 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001732 struct snd_timer_user *tu;
1733 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001736 if (!tu->timeri)
1737 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 memset(&status, 0, sizeof(status));
1739 status.tstamp = tu->tstamp;
1740 status.resolution = snd_timer_resolution(tu->timeri);
1741 status.lost = tu->timeri->lost;
1742 status.overrun = tu->overrun;
1743 spin_lock_irq(&tu->qlock);
1744 status.queue = tu->qused;
1745 spin_unlock_irq(&tu->qlock);
1746 if (copy_to_user(_status, &status, sizeof(status)))
1747 return -EFAULT;
1748 return 0;
1749}
1750
1751static int snd_timer_user_start(struct file *file)
1752{
1753 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001754 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001757 if (!tu->timeri)
1758 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 snd_timer_stop(tu->timeri);
1760 tu->timeri->lost = 0;
1761 tu->last_resolution = 0;
1762 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1763}
1764
1765static int snd_timer_user_stop(struct file *file)
1766{
1767 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001768 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001771 if (!tu->timeri)
1772 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1774}
1775
1776static int snd_timer_user_continue(struct file *file)
1777{
1778 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001779 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001782 if (!tu->timeri)
1783 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 tu->timeri->lost = 0;
1785 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1786}
1787
Takashi Iwai15790a62005-05-15 15:04:14 +02001788static int snd_timer_user_pause(struct file *file)
1789{
1790 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001791 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001792
Takashi Iwai15790a62005-05-15 15:04:14 +02001793 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001794 if (!tu->timeri)
1795 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001796 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001797}
1798
Takashi Iwai8c50b372005-05-15 15:43:54 +02001799enum {
1800 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1801 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1802 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1803 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1804};
1805
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001806static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1807 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001809 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 void __user *argp = (void __user *)arg;
1811 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 tu = file->private_data;
1814 switch (cmd) {
1815 case SNDRV_TIMER_IOCTL_PVERSION:
1816 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1817 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1818 return snd_timer_user_next_device(argp);
1819 case SNDRV_TIMER_IOCTL_TREAD:
1820 {
1821 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001822
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001823 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001824 if (tu->timeri) { /* too late */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001825 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001827 }
1828 if (get_user(xarg, p)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001829 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 tu->tread = xarg ? 1 : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001833 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return 0;
1835 }
1836 case SNDRV_TIMER_IOCTL_GINFO:
1837 return snd_timer_user_ginfo(file, argp);
1838 case SNDRV_TIMER_IOCTL_GPARAMS:
1839 return snd_timer_user_gparams(file, argp);
1840 case SNDRV_TIMER_IOCTL_GSTATUS:
1841 return snd_timer_user_gstatus(file, argp);
1842 case SNDRV_TIMER_IOCTL_SELECT:
1843 return snd_timer_user_tselect(file, argp);
1844 case SNDRV_TIMER_IOCTL_INFO:
1845 return snd_timer_user_info(file, argp);
1846 case SNDRV_TIMER_IOCTL_PARAMS:
1847 return snd_timer_user_params(file, argp);
1848 case SNDRV_TIMER_IOCTL_STATUS:
1849 return snd_timer_user_status(file, argp);
1850 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001851 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return snd_timer_user_start(file);
1853 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001854 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return snd_timer_user_stop(file);
1856 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001857 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001859 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001860 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001861 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 return -ENOTTY;
1864}
1865
1866static int snd_timer_user_fasync(int fd, struct file * file, int on)
1867{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001868 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001871 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001874static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1875 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001877 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 long result = 0, unit;
1879 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001882 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 spin_lock_irq(&tu->qlock);
1884 while ((long)count - result >= unit) {
1885 while (!tu->qused) {
1886 wait_queue_t wait;
1887
1888 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1889 err = -EAGAIN;
1890 break;
1891 }
1892
1893 set_current_state(TASK_INTERRUPTIBLE);
1894 init_waitqueue_entry(&wait, current);
1895 add_wait_queue(&tu->qchange_sleep, &wait);
1896
1897 spin_unlock_irq(&tu->qlock);
1898 schedule();
1899 spin_lock_irq(&tu->qlock);
1900
1901 remove_wait_queue(&tu->qchange_sleep, &wait);
1902
1903 if (signal_pending(current)) {
1904 err = -ERESTARTSYS;
1905 break;
1906 }
1907 }
1908
1909 spin_unlock_irq(&tu->qlock);
1910 if (err < 0)
1911 goto _error;
1912
1913 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001914 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001915 sizeof(struct snd_timer_tread))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 err = -EFAULT;
1917 goto _error;
1918 }
1919 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001920 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001921 sizeof(struct snd_timer_read))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 err = -EFAULT;
1923 goto _error;
1924 }
1925 }
1926
1927 tu->qhead %= tu->queue_size;
1928
1929 result += unit;
1930 buffer += unit;
1931
1932 spin_lock_irq(&tu->qlock);
1933 tu->qused--;
1934 }
1935 spin_unlock_irq(&tu->qlock);
1936 _error:
1937 return result > 0 ? result : err;
1938}
1939
1940static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1941{
1942 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001943 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 tu = file->private_data;
1946
1947 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 mask = 0;
1950 if (tu->qused)
1951 mask |= POLLIN | POLLRDNORM;
1952
1953 return mask;
1954}
1955
1956#ifdef CONFIG_COMPAT
1957#include "timer_compat.c"
1958#else
1959#define snd_timer_user_ioctl_compat NULL
1960#endif
1961
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001962static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 .owner = THIS_MODULE,
1965 .read = snd_timer_user_read,
1966 .open = snd_timer_user_open,
1967 .release = snd_timer_user_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001968 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 .poll = snd_timer_user_poll,
1970 .unlocked_ioctl = snd_timer_user_ioctl,
1971 .compat_ioctl = snd_timer_user_ioctl_compat,
1972 .fasync = snd_timer_user_fasync,
1973};
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975/*
1976 * ENTRY functions
1977 */
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979static int __init alsa_timer_init(void)
1980{
1981 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001984 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1985 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if ((err = snd_timer_register_system()) < 0)
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01001989 pr_err("ALSA: unable to register system timer (%i)\n", err);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001990 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1991 &snd_timer_f_ops, NULL, "timer")) < 0)
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01001992 pr_err("ALSA: unable to register timer device (%i)\n", err);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001993 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return 0;
1995}
1996
1997static void __exit alsa_timer_exit(void)
1998{
1999 struct list_head *p, *n;
2000
2001 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
2002 /* unregister the system timer */
2003 list_for_each_safe(p, n, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01002004 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
Takashi Iwaic4614822006-06-23 14:38:23 +02002005 snd_timer_free(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
Takashi Iwaie28563c2005-12-01 10:42:42 +01002007 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2009 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2010#endif
2011}
2012
2013module_init(alsa_timer_init)
2014module_exit(alsa_timer_exit)
2015
2016EXPORT_SYMBOL(snd_timer_open);
2017EXPORT_SYMBOL(snd_timer_close);
2018EXPORT_SYMBOL(snd_timer_resolution);
2019EXPORT_SYMBOL(snd_timer_start);
2020EXPORT_SYMBOL(snd_timer_stop);
2021EXPORT_SYMBOL(snd_timer_continue);
2022EXPORT_SYMBOL(snd_timer_pause);
2023EXPORT_SYMBOL(snd_timer_new);
2024EXPORT_SYMBOL(snd_timer_notify);
2025EXPORT_SYMBOL(snd_timer_global_new);
2026EXPORT_SYMBOL(snd_timer_global_free);
2027EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028EXPORT_SYMBOL(snd_timer_interrupt);