blob: 18dabfcc954944383a6e249760a087055dec486e [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 Iwai8d155be2016-01-21 17:19:31 +010068 bool disconnected;
Takashi Iwai53d2f742005-11-17 13:56:05 +010069 struct snd_timer_read *queue;
70 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 spinlock_t qlock;
72 unsigned long last_resolution;
73 unsigned int filter;
74 struct timespec tstamp; /* trigger tstamp */
75 wait_queue_head_t qchange_sleep;
76 struct fasync_struct *fasync;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010077 struct mutex tread_sem;
Takashi Iwai53d2f742005-11-17 13:56:05 +010078};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* list of timers */
81static LIST_HEAD(snd_timer_list);
82
83/* list of slave instances */
84static LIST_HEAD(snd_timer_slave_list);
85
86/* lock for slave active lists */
87static DEFINE_SPINLOCK(slave_active_lock);
88
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010089static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwai53d2f742005-11-17 13:56:05 +010091static int snd_timer_free(struct snd_timer *timer);
92static int snd_timer_dev_free(struct snd_device *device);
93static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020094static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Takashi Iwai53d2f742005-11-17 13:56:05 +010096static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * create a timer instance with the given owner string.
100 * when timer is not NULL, increments the module counter
101 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100102static struct snd_timer_instance *snd_timer_instance_new(char *owner,
103 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100105 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200106 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (timeri == NULL)
108 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700109 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (! timeri->owner) {
111 kfree(timeri);
112 return NULL;
113 }
114 INIT_LIST_HEAD(&timeri->open_list);
115 INIT_LIST_HEAD(&timeri->active_list);
116 INIT_LIST_HEAD(&timeri->ack_list);
117 INIT_LIST_HEAD(&timeri->slave_list_head);
118 INIT_LIST_HEAD(&timeri->slave_active_head);
119
120 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200121 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 kfree(timeri->owner);
123 kfree(timeri);
124 return NULL;
125 }
126
127 return timeri;
128}
129
130/*
131 * find a timer instance from the given timer id
132 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100133static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100135 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Johannes Berg9244b2c2006-10-05 16:02:22 +0200137 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (timer->tmr_class != tid->dev_class)
139 continue;
140 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
141 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
142 (timer->card == NULL ||
143 timer->card->number != tid->card))
144 continue;
145 if (timer->tmr_device != tid->device)
146 continue;
147 if (timer->tmr_subdevice != tid->subdevice)
148 continue;
149 return timer;
150 }
151 return NULL;
152}
153
Johannes Bergee2da992008-07-09 10:28:41 +0200154#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Takashi Iwai53d2f742005-11-17 13:56:05 +0100156static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 switch (tid->dev_class) {
159 case SNDRV_TIMER_CLASS_GLOBAL:
160 if (tid->device < timer_limit)
161 request_module("snd-timer-%i", tid->device);
162 break;
163 case SNDRV_TIMER_CLASS_CARD:
164 case SNDRV_TIMER_CLASS_PCM:
165 if (tid->card < snd_ecards_limit)
166 request_module("snd-card-%i", tid->card);
167 break;
168 default:
169 break;
170 }
171}
172
173#endif
174
175/*
176 * look for a master instance matching with the slave id of the given slave.
177 * when found, relink the open_link of the slave.
178 *
179 * call this with register_mutex down.
180 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100181static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100183 struct snd_timer *timer;
184 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200187 list_for_each_entry(timer, &snd_timer_list, device_list) {
188 list_for_each_entry(master, &timer->open_list_head, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (slave->slave_class == master->slave_class &&
190 slave->slave_id == master->slave_id) {
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100191 list_move_tail(&slave->open_list,
192 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 spin_lock_irq(&slave_active_lock);
194 slave->master = master;
195 slave->timer = master->timer;
196 spin_unlock_irq(&slave_active_lock);
197 return;
198 }
199 }
200 }
201}
202
203/*
204 * look for slave instances matching with the slave id of the given master.
205 * when found, relink the open_link of slaves.
206 *
207 * call this with register_mutex down.
208 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100209static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200211 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200214 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (slave->slave_class == master->slave_class &&
216 slave->slave_id == master->slave_id) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200217 list_move_tail(&slave->open_list, &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 spin_lock_irq(&slave_active_lock);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100219 spin_lock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 slave->master = master;
221 slave->timer = master->timer;
222 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200223 list_add_tail(&slave->active_list,
224 &master->slave_active_head);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100225 spin_unlock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 spin_unlock_irq(&slave_active_lock);
227 }
228 }
229}
230
231/*
232 * open a timer instance
233 * when opening a master, the slave id must be here given.
234 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100235int snd_timer_open(struct snd_timer_instance **ti,
236 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned int slave_id)
238{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100239 struct snd_timer *timer;
240 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
243 /* open a slave instance */
244 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
245 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100246 pr_debug("ALSA: timer: invalid slave class %i\n",
247 tid->dev_sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EINVAL;
249 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100250 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200252 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100253 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200254 return -ENOMEM;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 timeri->slave_class = tid->dev_sclass;
257 timeri->slave_id = tid->device;
258 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
259 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
260 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100261 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *ti = timeri;
263 return 0;
264 }
265
266 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100267 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 timer = snd_timer_find(tid);
Johannes Bergee2da992008-07-09 10:28:41 +0200269#ifdef CONFIG_MODULES
270 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100271 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100273 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 timer = snd_timer_find(tid);
275 }
276#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200277 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100278 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -ENODEV;
280 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200281 if (!list_empty(&timer->open_list_head)) {
282 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100283 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200284 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100285 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200286 return -EBUSY;
287 }
288 }
289 timeri = snd_timer_instance_new(owner, timer);
290 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100291 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200292 return -ENOMEM;
293 }
Takashi Iwai8d155be2016-01-21 17:19:31 +0100294 /* take a card refcount for safe disconnection */
295 if (timer->card)
296 get_device(&timer->card->card_dev);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200297 timeri->slave_class = tid->dev_sclass;
298 timeri->slave_id = slave_id;
Vegard Nossum14e3a782016-08-29 00:33:51 +0200299
300 if (list_empty(&timer->open_list_head) && timer->hw.open) {
301 int err = timer->hw.open(timer);
302 if (err) {
303 kfree(timeri->owner);
304 kfree(timeri);
305
306 if (timer->card)
307 put_device(&timer->card->card_dev);
308 module_put(timer->module);
309 mutex_unlock(&register_mutex);
310 return err;
311 }
312 }
313
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200314 list_add_tail(&timeri->open_list, &timer->open_list_head);
315 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100316 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *ti = timeri;
318 return 0;
319}
320
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100321static int _snd_timer_stop(struct snd_timer_instance *timeri, int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/*
324 * close a timer instance
325 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100326int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100328 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200329 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Takashi Iwai00728892008-08-14 07:51:57 +0200331 if (snd_BUG_ON(!timeri))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200332 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* force to stop the timer */
335 snd_timer_stop(timeri);
336
337 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
338 /* wait, until the active callback is finished */
339 spin_lock_irq(&slave_active_lock);
340 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
341 spin_unlock_irq(&slave_active_lock);
342 udelay(10);
343 spin_lock_irq(&slave_active_lock);
344 }
345 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100346 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 list_del(&timeri->open_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100348 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else {
350 timer = timeri->timer;
Takashi Iwai94094c82011-08-08 12:28:22 +0200351 if (snd_BUG_ON(!timer))
352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* wait, until the active callback is finished */
354 spin_lock_irq(&timer->lock);
355 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
356 spin_unlock_irq(&timer->lock);
357 udelay(10);
358 spin_lock_irq(&timer->lock);
359 }
360 spin_unlock_irq(&timer->lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100361 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 list_del(&timeri->open_list);
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100363 if (list_empty(&timer->open_list_head) &&
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200364 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 timer->hw.close(timer);
366 /* remove slave links */
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100367 spin_lock_irq(&slave_active_lock);
368 spin_lock(&timer->lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200369 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
370 open_list) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200371 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 slave->master = NULL;
373 slave->timer = NULL;
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100374 list_del_init(&slave->ack_list);
375 list_del_init(&slave->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100377 spin_unlock(&timer->lock);
378 spin_unlock_irq(&slave_active_lock);
Takashi Iwai8d155be2016-01-21 17:19:31 +0100379 /* release a card refcount for safe disconnection */
380 if (timer->card)
381 put_device(&timer->card->card_dev);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100382 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Takashi Iwai94094c82011-08-08 12:28:22 +0200384 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (timeri->private_free)
386 timeri->private_free(timeri);
387 kfree(timeri->owner);
388 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200389 if (timer)
390 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
Takashi Iwai53d2f742005-11-17 13:56:05 +0100394unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100396 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (timeri == NULL)
399 return 0;
400 if ((timer = timeri->timer) != NULL) {
401 if (timer->hw.c_resolution)
402 return timer->hw.c_resolution(timer);
403 return timer->hw.resolution;
404 }
405 return 0;
406}
407
Takashi Iwai53d2f742005-11-17 13:56:05 +0100408static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100410 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 unsigned long flags;
412 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100413 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct timespec tstamp;
415
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100416 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +0000417 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100418 else
419 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200420 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
421 event > SNDRV_TIMER_EVENT_PAUSE))
422 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200423 if (event == SNDRV_TIMER_EVENT_START ||
424 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 resolution = snd_timer_resolution(ti);
426 if (ti->ccallback)
Jaroslav Kyselab30477d2010-03-03 11:05:55 +0100427 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
429 return;
430 timer = ti->timer;
431 if (timer == NULL)
432 return;
433 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
434 return;
435 spin_lock_irqsave(&timer->lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200436 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (ts->ccallback)
Takashi Iwai77d0a942016-02-08 17:36:25 +0100438 ts->ccallback(ts, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 spin_unlock_irqrestore(&timer->lock, flags);
440}
441
Takashi Iwai53d2f742005-11-17 13:56:05 +0100442static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200443 unsigned long sticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100445 list_move_tail(&timeri->active_list, &timer->active_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (timer->running) {
447 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
448 goto __start_now;
449 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
450 timeri->flags |= SNDRV_TIMER_IFLG_START;
451 return 1; /* delayed start */
452 } else {
453 timer->sticks = sticks;
454 timer->hw.start(timer);
455 __start_now:
456 timer->running++;
457 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
458 return 0;
459 }
460}
461
Takashi Iwai53d2f742005-11-17 13:56:05 +0100462static int snd_timer_start_slave(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 unsigned long flags;
465
466 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100467 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
468 spin_unlock_irqrestore(&slave_active_lock, flags);
469 return -EBUSY;
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100472 if (timeri->master && timeri->timer) {
473 spin_lock(&timeri->timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200474 list_add_tail(&timeri->active_list,
475 &timeri->master->slave_active_head);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100476 spin_unlock(&timeri->timer->lock);
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 spin_unlock_irqrestore(&slave_active_lock, flags);
479 return 1; /* delayed start */
480}
481
482/*
483 * start the timer instance
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200484 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100485int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100487 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int result = -EINVAL;
489 unsigned long flags;
490
491 if (timeri == NULL || ticks < 1)
492 return -EINVAL;
493 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
494 result = snd_timer_start_slave(timeri);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100495 if (result >= 0)
496 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return result;
498 }
499 timer = timeri->timer;
500 if (timer == NULL)
501 return -EINVAL;
Takashi Iwai8d155be2016-01-21 17:19:31 +0100502 if (timer->card && timer->card->shutdown)
503 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100505 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
506 SNDRV_TIMER_IFLG_START)) {
507 result = -EBUSY;
508 goto unlock;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 timeri->ticks = timeri->cticks = ticks;
511 timeri->pticks = 0;
512 result = snd_timer_start1(timer, timeri, ticks);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100513 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 spin_unlock_irqrestore(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100515 if (result >= 0)
516 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return result;
518}
519
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100520static int _snd_timer_stop(struct snd_timer_instance *timeri, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100522 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 unsigned long flags;
524
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200525 if (snd_BUG_ON(!timeri))
526 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100529 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100530 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
531 spin_unlock_irqrestore(&slave_active_lock, flags);
532 return -EBUSY;
533 }
Takashi Iwai89e9cac2016-02-09 12:02:32 +0100534 if (timeri->timer)
535 spin_lock(&timeri->timer->lock);
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100536 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
537 list_del_init(&timeri->ack_list);
538 list_del_init(&timeri->active_list);
Takashi Iwai89e9cac2016-02-09 12:02:32 +0100539 if (timeri->timer)
540 spin_unlock(&timeri->timer->lock);
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100541 spin_unlock_irqrestore(&slave_active_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto __end;
543 }
544 timer = timeri->timer;
545 if (!timer)
546 return -EINVAL;
547 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100548 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
549 SNDRV_TIMER_IFLG_START))) {
550 spin_unlock_irqrestore(&timer->lock, flags);
551 return -EBUSY;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 list_del_init(&timeri->ack_list);
554 list_del_init(&timeri->active_list);
Takashi Iwai8d155be2016-01-21 17:19:31 +0100555 if (timer->card && timer->card->shutdown) {
556 spin_unlock_irqrestore(&timer->lock, flags);
557 return 0;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
560 !(--timer->running)) {
561 timer->hw.stop(timer);
562 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
563 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
564 snd_timer_reschedule(timer, 0);
565 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
566 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
567 timer->hw.start(timer);
568 }
569 }
570 }
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100571 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 spin_unlock_irqrestore(&timer->lock, flags);
573 __end:
574 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
575 snd_timer_notify1(timeri, event);
576 return 0;
577}
578
579/*
580 * stop the timer instance.
581 *
582 * do not call this from the timer callback!
583 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100584int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100586 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 unsigned long flags;
588 int err;
589
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100590 err = _snd_timer_stop(timeri, SNDRV_TIMER_EVENT_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (err < 0)
592 return err;
593 timer = timeri->timer;
Takashi Iwai0584ffa2011-08-08 12:24:46 +0200594 if (!timer)
595 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_lock_irqsave(&timer->lock, flags);
597 timeri->cticks = timeri->ticks;
598 timeri->pticks = 0;
599 spin_unlock_irqrestore(&timer->lock, flags);
600 return 0;
601}
602
603/*
604 * start again.. the tick is kept.
605 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100606int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100608 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int result = -EINVAL;
610 unsigned long flags;
611
612 if (timeri == NULL)
613 return result;
614 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
615 return snd_timer_start_slave(timeri);
616 timer = timeri->timer;
617 if (! timer)
618 return -EINVAL;
Takashi Iwai8d155be2016-01-21 17:19:31 +0100619 if (timer->card && timer->card->shutdown)
620 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100622 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
623 result = -EBUSY;
624 goto unlock;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!timeri->cticks)
627 timeri->cticks = 1;
628 timeri->pticks = 0;
629 result = snd_timer_start1(timer, timeri, timer->sticks);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100630 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 spin_unlock_irqrestore(&timer->lock, flags);
632 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
633 return result;
634}
635
636/*
637 * pause.. remember the ticks left
638 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100639int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100641 return _snd_timer_stop(timeri, SNDRV_TIMER_EVENT_PAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/*
645 * reschedule the timer
646 *
647 * start pending instances and check the scheduling ticks.
648 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
649 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100650static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100652 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Johannes Berg9244b2c2006-10-05 16:02:22 +0200655 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (ti->flags & SNDRV_TIMER_IFLG_START) {
657 ti->flags &= ~SNDRV_TIMER_IFLG_START;
658 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
659 timer->running++;
660 }
661 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
662 if (ticks > ti->cticks)
663 ticks = ti->cticks;
664 }
665 }
666 if (ticks == ~0UL) {
667 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
668 return;
669 }
670 if (ticks > timer->hw.ticks)
671 ticks = timer->hw.ticks;
672 if (ticks_left != ticks)
673 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
674 timer->sticks = ticks;
675}
676
677/*
678 * timer tasklet
679 *
680 */
681static void snd_timer_tasklet(unsigned long arg)
682{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100683 struct snd_timer *timer = (struct snd_timer *) arg;
684 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct list_head *p;
686 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200687 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Takashi Iwai8d155be2016-01-21 17:19:31 +0100689 if (timer->card && timer->card->shutdown)
690 return;
691
Takashi Iwai2999ff52006-07-05 17:16:58 +0200692 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* now process all callbacks */
694 while (!list_empty(&timer->sack_list_head)) {
695 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100696 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* remove from ack_list and make empty */
699 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ticks = ti->pticks;
702 ti->pticks = 0;
703 resolution = ti->resolution;
704
705 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
706 spin_unlock(&timer->lock);
707 if (ti->callback)
708 ti->callback(ti, resolution, ticks);
709 spin_lock(&timer->lock);
710 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
711 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200712 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715/*
716 * timer interrupt
717 *
718 * ticks_left is usually equal to timer->sticks.
719 *
720 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100721void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200723 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200725 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100726 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int use_tasklet = 0;
728
729 if (timer == NULL)
730 return;
731
Takashi Iwai8d155be2016-01-21 17:19:31 +0100732 if (timer->card && timer->card->shutdown)
733 return;
734
Takashi Iwaib32425a2005-11-18 18:52:14 +0100735 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /* remember the current resolution */
738 if (timer->hw.c_resolution)
739 resolution = timer->hw.c_resolution(timer);
740 else
741 resolution = timer->hw.resolution;
742
743 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200744 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200745 * processed instance is relinked to done_list_head before the callback
746 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200748 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
749 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
751 continue;
752 ti->pticks += ticks_left;
753 ti->resolution = resolution;
754 if (ti->cticks < ticks_left)
755 ti->cticks = 0;
756 else
757 ti->cticks -= ticks_left;
758 if (ti->cticks) /* not expired */
759 continue;
760 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
761 ti->cticks = ti->ticks;
762 } else {
763 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai2562c292016-02-04 17:06:13 +0100764 --timer->running;
765 list_del_init(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200767 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
768 (ti->flags & SNDRV_TIMER_IFLG_FAST))
769 ack_list_head = &timer->ack_list_head;
770 else
771 ack_list_head = &timer->sack_list_head;
772 if (list_empty(&ti->ack_list))
773 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200774 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ts->pticks = ti->pticks;
776 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200777 if (list_empty(&ts->ack_list))
778 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 }
781 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200782 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (timer->running) {
784 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
785 timer->hw.stop(timer);
786 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
787 }
788 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
789 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
790 /* restart timer */
791 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
792 timer->hw.start(timer);
793 }
794 } else {
795 timer->hw.stop(timer);
796 }
797
798 /* now process all fast callbacks */
799 while (!list_empty(&timer->ack_list_head)) {
800 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100801 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* remove from ack_list and make empty */
804 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ticks = ti->pticks;
807 ti->pticks = 0;
808
809 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
810 spin_unlock(&timer->lock);
811 if (ti->callback)
812 ti->callback(ti, resolution, ticks);
813 spin_lock(&timer->lock);
814 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
815 }
816
817 /* do we have any slow callbacks? */
818 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100819 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100822 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825/*
826
827 */
828
Takashi Iwai53d2f742005-11-17 13:56:05 +0100829int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
830 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100832 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100834 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .dev_free = snd_timer_dev_free,
836 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200837 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 };
839
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200840 if (snd_BUG_ON(!tid))
841 return -EINVAL;
842 if (rtimer)
843 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200844 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100845 if (timer == NULL) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100846 pr_err("ALSA: timer: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 timer->tmr_class = tid->dev_class;
850 timer->card = card;
851 timer->tmr_device = tid->device;
852 timer->tmr_subdevice = tid->subdevice;
853 if (id)
854 strlcpy(timer->id, id, sizeof(timer->id));
Vegard Nossuma987c622016-08-29 00:33:50 +0200855 timer->sticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 INIT_LIST_HEAD(&timer->device_list);
857 INIT_LIST_HEAD(&timer->open_list_head);
858 INIT_LIST_HEAD(&timer->active_list_head);
859 INIT_LIST_HEAD(&timer->ack_list_head);
860 INIT_LIST_HEAD(&timer->sack_list_head);
861 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200862 tasklet_init(&timer->task_queue, snd_timer_tasklet,
863 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200865 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200866 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
867 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 snd_timer_free(timer);
869 return err;
870 }
871 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200872 if (rtimer)
873 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return 0;
875}
876
Takashi Iwai53d2f742005-11-17 13:56:05 +0100877static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200879 if (!timer)
880 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200881
882 mutex_lock(&register_mutex);
883 if (! list_empty(&timer->open_list_head)) {
884 struct list_head *p, *n;
885 struct snd_timer_instance *ti;
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100886 pr_warn("ALSA: timer %p is busy?\n", timer);
Takashi Iwaic4614822006-06-23 14:38:23 +0200887 list_for_each_safe(p, n, &timer->open_list_head) {
888 list_del_init(p);
889 ti = list_entry(p, struct snd_timer_instance, open_list);
890 ti->timer = NULL;
891 }
892 }
893 list_del(&timer->device_list);
894 mutex_unlock(&register_mutex);
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (timer->private_free)
897 timer->private_free(timer);
898 kfree(timer);
899 return 0;
900}
901
Takashi Iwai53d2f742005-11-17 13:56:05 +0100902static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100904 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return snd_timer_free(timer);
906}
907
Takashi Iwai53d2f742005-11-17 13:56:05 +0100908static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100910 struct snd_timer *timer = dev->device_data;
911 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200913 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
914 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
916 !timer->hw.resolution && timer->hw.c_resolution == NULL)
917 return -EINVAL;
918
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100919 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200920 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (timer1->tmr_class > timer->tmr_class)
922 break;
923 if (timer1->tmr_class < timer->tmr_class)
924 continue;
925 if (timer1->card && timer->card) {
926 if (timer1->card->number > timer->card->number)
927 break;
928 if (timer1->card->number < timer->card->number)
929 continue;
930 }
931 if (timer1->tmr_device > timer->tmr_device)
932 break;
933 if (timer1->tmr_device < timer->tmr_device)
934 continue;
935 if (timer1->tmr_subdevice > timer->tmr_subdevice)
936 break;
937 if (timer1->tmr_subdevice < timer->tmr_subdevice)
938 continue;
939 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100940 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -EBUSY;
942 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200943 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100944 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946}
947
Takashi Iwai8d155be2016-01-21 17:19:31 +0100948/* just for reference in snd_timer_dev_disconnect() below */
949static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
950 int event, struct timespec *tstamp,
951 unsigned long resolution);
952
Takashi Iwaic4614822006-06-23 14:38:23 +0200953static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100955 struct snd_timer *timer = device->device_data;
Takashi Iwai8d155be2016-01-21 17:19:31 +0100956 struct snd_timer_instance *ti;
957
Takashi Iwaic4614822006-06-23 14:38:23 +0200958 mutex_lock(&register_mutex);
959 list_del_init(&timer->device_list);
Takashi Iwai8d155be2016-01-21 17:19:31 +0100960 /* wake up pending sleepers */
961 list_for_each_entry(ti, &timer->open_list_head, open_list) {
962 /* FIXME: better to have a ti.disconnect() op */
963 if (ti->ccallback == snd_timer_user_ccallback) {
964 struct snd_timer_user *tu = ti->callback_data;
965
966 tu->disconnected = true;
967 wake_up(&tu->qchange_sleep);
968 }
969 }
Takashi Iwaic4614822006-06-23 14:38:23 +0200970 mutex_unlock(&register_mutex);
971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Takashi Iwai53d2f742005-11-17 13:56:05 +0100974void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 unsigned long flags;
977 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100978 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Takashi Iwai8d155be2016-01-21 17:19:31 +0100980 if (timer->card && timer->card->shutdown)
981 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200982 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
983 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200984 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
985 event > SNDRV_TIMER_EVENT_MRESUME))
986 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200988 if (event == SNDRV_TIMER_EVENT_MSTART ||
989 event == SNDRV_TIMER_EVENT_MCONTINUE ||
990 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (timer->hw.c_resolution)
992 resolution = timer->hw.c_resolution(timer);
993 else
994 resolution = timer->hw.resolution;
995 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200996 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (ti->ccallback)
998 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200999 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (ts->ccallback)
1001 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 spin_unlock_irqrestore(&timer->lock, flags);
1004}
1005
1006/*
1007 * exported functions for global timers
1008 */
Takashi Iwai53d2f742005-11-17 13:56:05 +01001009int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001011 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1014 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1015 tid.card = -1;
1016 tid.device = device;
1017 tid.subdevice = 0;
1018 return snd_timer_new(NULL, id, &tid, rtimer);
1019}
1020
Takashi Iwai53d2f742005-11-17 13:56:05 +01001021int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 return snd_timer_free(timer);
1024}
1025
Takashi Iwai53d2f742005-11-17 13:56:05 +01001026int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001028 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 memset(&dev, 0, sizeof(dev));
1031 dev.device_data = timer;
1032 return snd_timer_dev_register(&dev);
1033}
1034
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001035/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * System timer
1037 */
1038
1039struct snd_timer_system_private {
1040 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 unsigned long last_expires;
1042 unsigned long last_jiffies;
1043 unsigned long correction;
1044};
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static void snd_timer_s_function(unsigned long data)
1047{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001048 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct snd_timer_system_private *priv = timer->private_data;
1050 unsigned long jiff = jiffies;
1051 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +02001052 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1054}
1055
Takashi Iwai53d2f742005-11-17 13:56:05 +01001056static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 struct snd_timer_system_private *priv;
1059 unsigned long njiff;
1060
1061 priv = (struct snd_timer_system_private *) timer->private_data;
1062 njiff = (priv->last_jiffies = jiffies);
1063 if (priv->correction > timer->sticks - 1) {
1064 priv->correction -= timer->sticks - 1;
1065 njiff++;
1066 } else {
1067 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +02001068 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Takashi Iwaid4f95a02016-04-01 12:28:16 +02001070 priv->last_expires = njiff;
1071 mod_timer(&priv->tlist, njiff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return 0;
1073}
1074
Takashi Iwai53d2f742005-11-17 13:56:05 +01001075static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct snd_timer_system_private *priv;
1078 unsigned long jiff;
1079
1080 priv = (struct snd_timer_system_private *) timer->private_data;
1081 del_timer(&priv->tlist);
1082 jiff = jiffies;
1083 if (time_before(jiff, priv->last_expires))
1084 timer->sticks = priv->last_expires - jiff;
1085 else
1086 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001087 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return 0;
1089}
1090
Takashi Iwai53d2f742005-11-17 13:56:05 +01001091static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1094 .resolution = 1000000000L / HZ,
1095 .ticks = 10000000L,
1096 .start = snd_timer_s_start,
1097 .stop = snd_timer_s_stop
1098};
1099
Takashi Iwai53d2f742005-11-17 13:56:05 +01001100static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 kfree(timer->private_data);
1103}
1104
1105static int snd_timer_register_system(void)
1106{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001107 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct snd_timer_system_private *priv;
1109 int err;
1110
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001111 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1112 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return err;
1114 strcpy(timer->name, "system timer");
1115 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001116 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (priv == NULL) {
1118 snd_timer_free(timer);
1119 return -ENOMEM;
1120 }
1121 init_timer(&priv->tlist);
1122 priv->tlist.function = snd_timer_s_function;
1123 priv->tlist.data = (unsigned long) timer;
1124 timer->private_data = priv;
1125 timer->private_free = snd_timer_free_system;
1126 return snd_timer_global_register(timer);
1127}
1128
Takashi Iwaie28563c2005-12-01 10:42:42 +01001129#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * Info interface
1132 */
1133
Takashi Iwai53d2f742005-11-17 13:56:05 +01001134static void snd_timer_proc_read(struct snd_info_entry *entry,
1135 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001137 struct snd_timer *timer;
1138 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001140 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001141 list_for_each_entry(timer, &snd_timer_list, device_list) {
Takashi Iwai8d155be2016-01-21 17:19:31 +01001142 if (timer->card && timer->card->shutdown)
1143 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 switch (timer->tmr_class) {
1145 case SNDRV_TIMER_CLASS_GLOBAL:
1146 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1147 break;
1148 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001149 snd_iprintf(buffer, "C%i-%i: ",
1150 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001153 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1154 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001157 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1158 timer->card ? timer->card->number : -1,
1159 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 snd_iprintf(buffer, "%s :", timer->name);
1162 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001163 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1164 timer->hw.resolution / 1000,
1165 timer->hw.resolution % 1000,
1166 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1168 snd_iprintf(buffer, " SLAVE");
1169 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001170 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001171 snd_iprintf(buffer, " Client %s : %s\n",
1172 ti->owner ? ti->owner : "unknown",
1173 ti->flags & (SNDRV_TIMER_IFLG_START |
1174 SNDRV_TIMER_IFLG_RUNNING)
1175 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001177 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001180static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001181
1182static void __init snd_timer_proc_init(void)
1183{
1184 struct snd_info_entry *entry;
1185
1186 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1187 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001188 entry->c.text.read = snd_timer_proc_read;
1189 if (snd_info_register(entry) < 0) {
1190 snd_info_free_entry(entry);
1191 entry = NULL;
1192 }
1193 }
1194 snd_timer_proc_entry = entry;
1195}
1196
1197static void __exit snd_timer_proc_done(void)
1198{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001199 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001200}
1201#else /* !CONFIG_PROC_FS */
1202#define snd_timer_proc_init()
1203#define snd_timer_proc_done()
1204#endif
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/*
1207 * USER SPACE interface
1208 */
1209
Takashi Iwai53d2f742005-11-17 13:56:05 +01001210static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 unsigned long resolution,
1212 unsigned long ticks)
1213{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001214 struct snd_timer_user *tu = timeri->callback_data;
1215 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 spin_lock(&tu->qlock);
1219 if (tu->qused > 0) {
1220 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1221 r = &tu->queue[prev];
1222 if (r->resolution == resolution) {
1223 r->ticks += ticks;
1224 goto __wake;
1225 }
1226 }
1227 if (tu->qused >= tu->queue_size) {
1228 tu->overrun++;
1229 } else {
1230 r = &tu->queue[tu->qtail++];
1231 tu->qtail %= tu->queue_size;
1232 r->resolution = resolution;
1233 r->ticks = ticks;
1234 tu->qused++;
1235 }
1236 __wake:
1237 spin_unlock(&tu->qlock);
1238 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1239 wake_up(&tu->qchange_sleep);
1240}
1241
Takashi Iwai53d2f742005-11-17 13:56:05 +01001242static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1243 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 if (tu->qused >= tu->queue_size) {
1246 tu->overrun++;
1247 } else {
1248 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1249 tu->qtail %= tu->queue_size;
1250 tu->qused++;
1251 }
1252}
1253
Takashi Iwai53d2f742005-11-17 13:56:05 +01001254static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1255 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 struct timespec *tstamp,
1257 unsigned long resolution)
1258{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001259 struct snd_timer_user *tu = timeri->callback_data;
1260 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001261 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001263 if (event >= SNDRV_TIMER_EVENT_START &&
1264 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 tu->tstamp = *tstamp;
1266 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1267 return;
1268 r1.event = event;
1269 r1.tstamp = *tstamp;
1270 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001271 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001273 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1275 wake_up(&tu->qchange_sleep);
1276}
1277
Takashi Iwai53d2f742005-11-17 13:56:05 +01001278static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 unsigned long resolution,
1280 unsigned long ticks)
1281{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001282 struct snd_timer_user *tu = timeri->callback_data;
1283 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 struct timespec tstamp;
1285 int prev, append = 0;
1286
Takashi Iwai07799e72005-10-10 11:49:49 +02001287 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001289 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1290 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 spin_unlock(&tu->qlock);
1292 return;
1293 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001294 if (tu->last_resolution != resolution || ticks > 0) {
1295 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +00001296 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001297 else
1298 getnstimeofday(&tstamp);
1299 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001300 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1301 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1303 r1.tstamp = tstamp;
1304 r1.val = resolution;
1305 snd_timer_user_append_to_tqueue(tu, &r1);
1306 tu->last_resolution = resolution;
1307 append++;
1308 }
1309 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1310 goto __wake;
1311 if (ticks == 0)
1312 goto __wake;
1313 if (tu->qused > 0) {
1314 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1315 r = &tu->tqueue[prev];
1316 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1317 r->tstamp = tstamp;
1318 r->val += ticks;
1319 append++;
1320 goto __wake;
1321 }
1322 }
1323 r1.event = SNDRV_TIMER_EVENT_TICK;
1324 r1.tstamp = tstamp;
1325 r1.val = ticks;
1326 snd_timer_user_append_to_tqueue(tu, &r1);
1327 append++;
1328 __wake:
1329 spin_unlock(&tu->qlock);
1330 if (append == 0)
1331 return;
1332 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1333 wake_up(&tu->qchange_sleep);
1334}
1335
1336static int snd_timer_user_open(struct inode *inode, struct file *file)
1337{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001338 struct snd_timer_user *tu;
Takashi Iwai02f48652010-04-13 11:49:04 +02001339 int err;
1340
1341 err = nonseekable_open(inode, file);
1342 if (err < 0)
1343 return err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001344
Takashi Iwaica2c0962005-09-09 14:20:23 +02001345 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (tu == NULL)
1347 return -ENOMEM;
1348 spin_lock_init(&tu->qlock);
1349 init_waitqueue_head(&tu->qchange_sleep);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001350 mutex_init(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 tu->ticks = 1;
1352 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001353 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001354 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (tu->queue == NULL) {
1356 kfree(tu);
1357 return -ENOMEM;
1358 }
1359 file->private_data = tu;
1360 return 0;
1361}
1362
1363static int snd_timer_user_release(struct inode *inode, struct file *file)
1364{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001365 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 if (file->private_data) {
1368 tu = file->private_data;
1369 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (tu->timeri)
1371 snd_timer_close(tu->timeri);
1372 kfree(tu->queue);
1373 kfree(tu->tqueue);
1374 kfree(tu);
1375 }
1376 return 0;
1377}
1378
Takashi Iwai53d2f742005-11-17 13:56:05 +01001379static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1382 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1383 id->card = -1;
1384 id->device = -1;
1385 id->subdevice = -1;
1386}
1387
Takashi Iwai53d2f742005-11-17 13:56:05 +01001388static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 id->dev_class = timer->tmr_class;
1391 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1392 id->card = timer->card ? timer->card->number : -1;
1393 id->device = timer->tmr_device;
1394 id->subdevice = timer->tmr_subdevice;
1395}
1396
Takashi Iwai53d2f742005-11-17 13:56:05 +01001397static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001399 struct snd_timer_id id;
1400 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (copy_from_user(&id, _tid, sizeof(id)))
1404 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001405 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (id.dev_class < 0) { /* first item */
1407 if (list_empty(&snd_timer_list))
1408 snd_timer_user_zero_id(&id);
1409 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001410 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001411 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 snd_timer_user_copy_id(&id, timer);
1413 }
1414 } else {
1415 switch (id.dev_class) {
1416 case SNDRV_TIMER_CLASS_GLOBAL:
1417 id.device = id.device < 0 ? 0 : id.device + 1;
1418 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001419 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1421 snd_timer_user_copy_id(&id, timer);
1422 break;
1423 }
1424 if (timer->tmr_device >= id.device) {
1425 snd_timer_user_copy_id(&id, timer);
1426 break;
1427 }
1428 }
1429 if (p == &snd_timer_list)
1430 snd_timer_user_zero_id(&id);
1431 break;
1432 case SNDRV_TIMER_CLASS_CARD:
1433 case SNDRV_TIMER_CLASS_PCM:
1434 if (id.card < 0) {
1435 id.card = 0;
1436 } else {
1437 if (id.card < 0) {
1438 id.card = 0;
1439 } else {
1440 if (id.device < 0) {
1441 id.device = 0;
1442 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001443 if (id.subdevice < 0) {
1444 id.subdevice = 0;
1445 } else {
1446 id.subdevice++;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 }
1450 }
1451 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001452 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (timer->tmr_class > id.dev_class) {
1454 snd_timer_user_copy_id(&id, timer);
1455 break;
1456 }
1457 if (timer->tmr_class < id.dev_class)
1458 continue;
1459 if (timer->card->number > id.card) {
1460 snd_timer_user_copy_id(&id, timer);
1461 break;
1462 }
1463 if (timer->card->number < id.card)
1464 continue;
1465 if (timer->tmr_device > id.device) {
1466 snd_timer_user_copy_id(&id, timer);
1467 break;
1468 }
1469 if (timer->tmr_device < id.device)
1470 continue;
1471 if (timer->tmr_subdevice > id.subdevice) {
1472 snd_timer_user_copy_id(&id, timer);
1473 break;
1474 }
1475 if (timer->tmr_subdevice < id.subdevice)
1476 continue;
1477 snd_timer_user_copy_id(&id, timer);
1478 break;
1479 }
1480 if (p == &snd_timer_list)
1481 snd_timer_user_zero_id(&id);
1482 break;
1483 default:
1484 snd_timer_user_zero_id(&id);
1485 }
1486 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001487 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1489 return -EFAULT;
1490 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001493static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001494 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001496 struct snd_timer_ginfo *ginfo;
1497 struct snd_timer_id tid;
1498 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct list_head *p;
1500 int err = 0;
1501
Li Zefanef44a1e2009-04-10 09:43:08 +08001502 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1503 if (IS_ERR(ginfo))
1504 return PTR_ERR(ginfo);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 tid = ginfo->tid;
1507 memset(ginfo, 0, sizeof(*ginfo));
1508 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001509 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 t = snd_timer_find(&tid);
1511 if (t != NULL) {
1512 ginfo->card = t->card ? t->card->number : -1;
1513 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1514 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1515 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1516 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1517 ginfo->resolution = t->hw.resolution;
1518 if (t->hw.resolution_min > 0) {
1519 ginfo->resolution_min = t->hw.resolution_min;
1520 ginfo->resolution_max = t->hw.resolution_max;
1521 }
1522 list_for_each(p, &t->open_list_head) {
1523 ginfo->clients++;
1524 }
1525 } else {
1526 err = -ENODEV;
1527 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001528 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1530 err = -EFAULT;
1531 kfree(ginfo);
1532 return err;
1533}
1534
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001535static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001536 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001538 struct snd_timer_gparams gparams;
1539 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int err;
1541
1542 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1543 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001544 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001546 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001548 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001550 if (!list_empty(&t->open_list_head)) {
1551 err = -EBUSY;
1552 goto _error;
1553 }
1554 if (!t->hw.set_period) {
1555 err = -ENOSYS;
1556 goto _error;
1557 }
1558 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1559_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001560 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return err;
1562}
1563
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001564static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001565 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001567 struct snd_timer_gstatus gstatus;
1568 struct snd_timer_id tid;
1569 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 int err = 0;
1571
1572 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1573 return -EFAULT;
1574 tid = gstatus.tid;
1575 memset(&gstatus, 0, sizeof(gstatus));
1576 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001577 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 t = snd_timer_find(&tid);
1579 if (t != NULL) {
1580 if (t->hw.c_resolution)
1581 gstatus.resolution = t->hw.c_resolution(t);
1582 else
1583 gstatus.resolution = t->hw.resolution;
1584 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001585 t->hw.precise_resolution(t, &gstatus.resolution_num,
1586 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 } else {
1588 gstatus.resolution_num = gstatus.resolution;
1589 gstatus.resolution_den = 1000000000uL;
1590 }
1591 } else {
1592 err = -ENODEV;
1593 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001594 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1596 err = -EFAULT;
1597 return err;
1598}
1599
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001600static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001601 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001603 struct snd_timer_user *tu;
1604 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001606 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 tu = file->private_data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001609 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001610 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001612 tu->timeri = NULL;
1613 }
1614 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1615 err = -EFAULT;
1616 goto __err;
1617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 sprintf(str, "application %i", current->pid);
1619 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1620 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001621 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1622 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001623 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Jesper Juhl4d572772005-05-30 17:30:32 +02001625 kfree(tu->queue);
1626 tu->queue = NULL;
1627 kfree(tu->tqueue);
1628 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001630 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001631 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001632 if (tu->tqueue == NULL)
1633 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001635 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001636 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001637 if (tu->queue == NULL)
1638 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001640
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001641 if (err < 0) {
1642 snd_timer_close(tu->timeri);
1643 tu->timeri = NULL;
1644 } else {
1645 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001646 tu->timeri->callback = tu->tread
1647 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001648 tu->timeri->ccallback = snd_timer_user_ccallback;
1649 tu->timeri->callback_data = (void *)tu;
1650 }
1651
1652 __err:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001653 mutex_unlock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001654 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001657static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001658 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001660 struct snd_timer_user *tu;
1661 struct snd_timer_info *info;
1662 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 int err = 0;
1664
1665 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001666 if (!tu->timeri)
1667 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001669 if (!t)
1670 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Takashi Iwaica2c0962005-09-09 14:20:23 +02001672 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (! info)
1674 return -ENOMEM;
1675 info->card = t->card ? t->card->number : -1;
1676 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1677 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1678 strlcpy(info->id, t->id, sizeof(info->id));
1679 strlcpy(info->name, t->name, sizeof(info->name));
1680 info->resolution = t->hw.resolution;
1681 if (copy_to_user(_info, info, sizeof(*_info)))
1682 err = -EFAULT;
1683 kfree(info);
1684 return err;
1685}
1686
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001687static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001688 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001690 struct snd_timer_user *tu;
1691 struct snd_timer_params params;
1692 struct snd_timer *t;
1693 struct snd_timer_read *tr;
1694 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001698 if (!tu->timeri)
1699 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001701 if (!t)
1702 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (copy_from_user(&params, _params, sizeof(params)))
1704 return -EFAULT;
1705 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1706 err = -EINVAL;
1707 goto _end;
1708 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001709 if (params.queue_size > 0 &&
1710 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 err = -EINVAL;
1712 goto _end;
1713 }
1714 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1715 (1<<SNDRV_TIMER_EVENT_TICK)|
1716 (1<<SNDRV_TIMER_EVENT_START)|
1717 (1<<SNDRV_TIMER_EVENT_STOP)|
1718 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1719 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001720 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1721 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 (1<<SNDRV_TIMER_EVENT_MSTART)|
1723 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1724 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001725 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1726 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001727 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 err = -EINVAL;
1729 goto _end;
1730 }
1731 snd_timer_stop(tu->timeri);
1732 spin_lock_irq(&t->lock);
1733 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1734 SNDRV_TIMER_IFLG_EXCLUSIVE|
1735 SNDRV_TIMER_IFLG_EARLY_EVENT);
1736 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1737 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1738 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1739 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1740 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1741 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1742 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001743 if (params.queue_size > 0 &&
1744 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001746 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1747 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (ttr) {
1749 kfree(tu->tqueue);
1750 tu->queue_size = params.queue_size;
1751 tu->tqueue = ttr;
1752 }
1753 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001754 tr = kmalloc(params.queue_size * sizeof(*tr),
1755 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (tr) {
1757 kfree(tu->queue);
1758 tu->queue_size = params.queue_size;
1759 tu->queue = tr;
1760 }
1761 }
1762 }
1763 tu->qhead = tu->qtail = tu->qused = 0;
1764 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1765 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001766 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 tread.event = SNDRV_TIMER_EVENT_EARLY;
1768 tread.tstamp.tv_sec = 0;
1769 tread.tstamp.tv_nsec = 0;
1770 tread.val = 0;
1771 snd_timer_user_append_to_tqueue(tu, &tread);
1772 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001773 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 r->resolution = 0;
1775 r->ticks = 0;
1776 tu->qused++;
1777 tu->qtail++;
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 tu->filter = params.filter;
1781 tu->ticks = params.ticks;
1782 err = 0;
1783 _end:
1784 if (copy_to_user(_params, &params, sizeof(params)))
1785 return -EFAULT;
1786 return err;
1787}
1788
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001789static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001790 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001792 struct snd_timer_user *tu;
1793 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001796 if (!tu->timeri)
1797 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 memset(&status, 0, sizeof(status));
1799 status.tstamp = tu->tstamp;
1800 status.resolution = snd_timer_resolution(tu->timeri);
1801 status.lost = tu->timeri->lost;
1802 status.overrun = tu->overrun;
1803 spin_lock_irq(&tu->qlock);
1804 status.queue = tu->qused;
1805 spin_unlock_irq(&tu->qlock);
1806 if (copy_to_user(_status, &status, sizeof(status)))
1807 return -EFAULT;
1808 return 0;
1809}
1810
1811static int snd_timer_user_start(struct file *file)
1812{
1813 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001814 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001817 if (!tu->timeri)
1818 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 snd_timer_stop(tu->timeri);
1820 tu->timeri->lost = 0;
1821 tu->last_resolution = 0;
1822 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1823}
1824
1825static int snd_timer_user_stop(struct file *file)
1826{
1827 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001828 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001831 if (!tu->timeri)
1832 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1834}
1835
1836static int snd_timer_user_continue(struct file *file)
1837{
1838 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001839 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001842 if (!tu->timeri)
1843 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 tu->timeri->lost = 0;
1845 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1846}
1847
Takashi Iwai15790a62005-05-15 15:04:14 +02001848static int snd_timer_user_pause(struct file *file)
1849{
1850 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001851 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001852
Takashi Iwai15790a62005-05-15 15:04:14 +02001853 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001854 if (!tu->timeri)
1855 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001856 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001857}
1858
Takashi Iwai8c50b372005-05-15 15:43:54 +02001859enum {
1860 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1861 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1862 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1863 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1864};
1865
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001866static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1867 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001869 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 void __user *argp = (void __user *)arg;
1871 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 tu = file->private_data;
1874 switch (cmd) {
1875 case SNDRV_TIMER_IOCTL_PVERSION:
1876 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1877 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1878 return snd_timer_user_next_device(argp);
1879 case SNDRV_TIMER_IOCTL_TREAD:
1880 {
1881 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001882
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001883 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001884 if (tu->timeri) { /* too late */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001885 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001887 }
1888 if (get_user(xarg, p)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001889 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 tu->tread = xarg ? 1 : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001893 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return 0;
1895 }
1896 case SNDRV_TIMER_IOCTL_GINFO:
1897 return snd_timer_user_ginfo(file, argp);
1898 case SNDRV_TIMER_IOCTL_GPARAMS:
1899 return snd_timer_user_gparams(file, argp);
1900 case SNDRV_TIMER_IOCTL_GSTATUS:
1901 return snd_timer_user_gstatus(file, argp);
1902 case SNDRV_TIMER_IOCTL_SELECT:
1903 return snd_timer_user_tselect(file, argp);
1904 case SNDRV_TIMER_IOCTL_INFO:
1905 return snd_timer_user_info(file, argp);
1906 case SNDRV_TIMER_IOCTL_PARAMS:
1907 return snd_timer_user_params(file, argp);
1908 case SNDRV_TIMER_IOCTL_STATUS:
1909 return snd_timer_user_status(file, argp);
1910 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001911 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return snd_timer_user_start(file);
1913 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001914 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return snd_timer_user_stop(file);
1916 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001917 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001919 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001920 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001921 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923 return -ENOTTY;
1924}
1925
1926static int snd_timer_user_fasync(int fd, struct file * file, int on)
1927{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001928 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001931 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001934static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1935 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001937 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 long result = 0, unit;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001939 int qhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001943 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 spin_lock_irq(&tu->qlock);
1945 while ((long)count - result >= unit) {
1946 while (!tu->qused) {
1947 wait_queue_t wait;
1948
1949 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1950 err = -EAGAIN;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001951 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
1954 set_current_state(TASK_INTERRUPTIBLE);
1955 init_waitqueue_entry(&wait, current);
1956 add_wait_queue(&tu->qchange_sleep, &wait);
1957
1958 spin_unlock_irq(&tu->qlock);
1959 schedule();
1960 spin_lock_irq(&tu->qlock);
1961
1962 remove_wait_queue(&tu->qchange_sleep, &wait);
1963
Takashi Iwai8d155be2016-01-21 17:19:31 +01001964 if (tu->disconnected) {
1965 err = -ENODEV;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001966 goto _error;
Takashi Iwai8d155be2016-01-21 17:19:31 +01001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (signal_pending(current)) {
1969 err = -ERESTARTSYS;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001970 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972 }
1973
Takashi Iwai0f97e402016-02-08 17:26:58 +01001974 qhead = tu->qhead++;
1975 tu->qhead %= tu->queue_size;
Takashi Iwaia851e562016-07-04 14:02:15 +02001976 tu->qused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (tu->tread) {
Takashi Iwai0f97e402016-02-08 17:26:58 +01001980 if (copy_to_user(buffer, &tu->tqueue[qhead],
1981 sizeof(struct snd_timer_tread)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 } else {
Takashi Iwai0f97e402016-02-08 17:26:58 +01001984 if (copy_to_user(buffer, &tu->queue[qhead],
1985 sizeof(struct snd_timer_read)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 spin_lock_irq(&tu->qlock);
Takashi Iwai0f97e402016-02-08 17:26:58 +01001990 if (err < 0)
1991 goto _error;
1992 result += unit;
1993 buffer += unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 _error:
Takashi Iwai0f97e402016-02-08 17:26:58 +01001996 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return result > 0 ? result : err;
1998}
1999
2000static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2001{
2002 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01002003 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 tu = file->private_data;
2006
2007 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 mask = 0;
2010 if (tu->qused)
2011 mask |= POLLIN | POLLRDNORM;
Takashi Iwai8d155be2016-01-21 17:19:31 +01002012 if (tu->disconnected)
2013 mask |= POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 return mask;
2016}
2017
2018#ifdef CONFIG_COMPAT
2019#include "timer_compat.c"
2020#else
2021#define snd_timer_user_ioctl_compat NULL
2022#endif
2023
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002024static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
2026 .owner = THIS_MODULE,
2027 .read = snd_timer_user_read,
2028 .open = snd_timer_user_open,
2029 .release = snd_timer_user_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02002030 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 .poll = snd_timer_user_poll,
2032 .unlocked_ioctl = snd_timer_user_ioctl,
2033 .compat_ioctl = snd_timer_user_ioctl_compat,
2034 .fasync = snd_timer_user_fasync,
2035};
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/*
2038 * ENTRY functions
2039 */
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041static int __init alsa_timer_init(void)
2042{
2043 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002046 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2047 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if ((err = snd_timer_register_system()) < 0)
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002051 pr_err("ALSA: unable to register system timer (%i)\n", err);
Clemens Ladischf87135f2005-11-20 14:06:59 +01002052 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2053 &snd_timer_f_ops, NULL, "timer")) < 0)
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002054 pr_err("ALSA: unable to register timer device (%i)\n", err);
Takashi Iwaie28563c2005-12-01 10:42:42 +01002055 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return 0;
2057}
2058
2059static void __exit alsa_timer_exit(void)
2060{
2061 struct list_head *p, *n;
2062
2063 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
2064 /* unregister the system timer */
2065 list_for_each_safe(p, n, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01002066 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
Takashi Iwaic4614822006-06-23 14:38:23 +02002067 snd_timer_free(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Takashi Iwaie28563c2005-12-01 10:42:42 +01002069 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2071 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2072#endif
2073}
2074
2075module_init(alsa_timer_init)
2076module_exit(alsa_timer_exit)
2077
2078EXPORT_SYMBOL(snd_timer_open);
2079EXPORT_SYMBOL(snd_timer_close);
2080EXPORT_SYMBOL(snd_timer_resolution);
2081EXPORT_SYMBOL(snd_timer_start);
2082EXPORT_SYMBOL(snd_timer_stop);
2083EXPORT_SYMBOL(snd_timer_continue);
2084EXPORT_SYMBOL(snd_timer_pause);
2085EXPORT_SYMBOL(snd_timer_new);
2086EXPORT_SYMBOL(snd_timer_notify);
2087EXPORT_SYMBOL(snd_timer_global_new);
2088EXPORT_SYMBOL(snd_timer_global_free);
2089EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090EXPORT_SYMBOL(snd_timer_interrupt);