blob: a9b9a277e00c1bda4bd85038cc45842ac2425ee0 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010030#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/timer.h>
33#include <sound/control.h>
34#include <sound/info.h>
35#include <sound/minors.h>
36#include <sound/initval.h>
37#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Takashi Iwai9f8a7652016-09-07 15:45:31 +020039/* internal flags */
40#define SNDRV_TIMER_IFLG_PAUSED 0x00010000
41
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010042#if IS_ENABLED(CONFIG_SND_HRTIMER)
Clemens Ladisch109fef92010-11-18 09:53:54 +010043#define DEFAULT_TIMER_LIMIT 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
45#define DEFAULT_TIMER_LIMIT 1
46#endif
47
48static int timer_limit = DEFAULT_TIMER_LIMIT;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010049static int timer_tstamp_monotonic = 1;
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020050MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_DESCRIPTION("ALSA timer interface");
52MODULE_LICENSE("GPL");
53module_param(timer_limit, int, 0444);
54MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010055module_param(timer_tstamp_monotonic, int, 0444);
56MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Kay Sievers03cfe6f2010-11-23 17:43:19 +010058MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
59MODULE_ALIAS("devname:snd/timer");
60
Takashi Iwai53d2f742005-11-17 13:56:05 +010061struct snd_timer_user {
62 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020063 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 unsigned long ticks;
65 unsigned long overrun;
66 int qhead;
67 int qtail;
68 int qused;
69 int queue_size;
Takashi Iwai230323d2016-01-21 17:19:31 +010070 bool disconnected;
Takashi Iwai53d2f742005-11-17 13:56:05 +010071 struct snd_timer_read *queue;
72 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 spinlock_t qlock;
74 unsigned long last_resolution;
75 unsigned int filter;
76 struct timespec tstamp; /* trigger tstamp */
77 wait_queue_head_t qchange_sleep;
78 struct fasync_struct *fasync;
Takashi Iwaiaf368022016-01-13 17:48:01 +010079 struct mutex ioctl_lock;
Takashi Iwai53d2f742005-11-17 13:56:05 +010080};
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* list of timers */
83static LIST_HEAD(snd_timer_list);
84
85/* list of slave instances */
86static LIST_HEAD(snd_timer_slave_list);
87
88/* lock for slave active lists */
89static DEFINE_SPINLOCK(slave_active_lock);
90
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010091static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Takashi Iwai53d2f742005-11-17 13:56:05 +010093static int snd_timer_free(struct snd_timer *timer);
94static int snd_timer_dev_free(struct snd_device *device);
95static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020096static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Takashi Iwai53d2f742005-11-17 13:56:05 +010098static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
101 * create a timer instance with the given owner string.
102 * when timer is not NULL, increments the module counter
103 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100104static struct snd_timer_instance *snd_timer_instance_new(char *owner,
105 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100107 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200108 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (timeri == NULL)
110 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700111 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (! timeri->owner) {
113 kfree(timeri);
114 return NULL;
115 }
116 INIT_LIST_HEAD(&timeri->open_list);
117 INIT_LIST_HEAD(&timeri->active_list);
118 INIT_LIST_HEAD(&timeri->ack_list);
119 INIT_LIST_HEAD(&timeri->slave_list_head);
120 INIT_LIST_HEAD(&timeri->slave_active_head);
121
122 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200123 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 kfree(timeri->owner);
125 kfree(timeri);
126 return NULL;
127 }
128
129 return timeri;
130}
131
132/*
133 * find a timer instance from the given timer id
134 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100135static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100137 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Johannes Berg9244b2c2006-10-05 16:02:22 +0200139 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (timer->tmr_class != tid->dev_class)
141 continue;
142 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
143 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
144 (timer->card == NULL ||
145 timer->card->number != tid->card))
146 continue;
147 if (timer->tmr_device != tid->device)
148 continue;
149 if (timer->tmr_subdevice != tid->subdevice)
150 continue;
151 return timer;
152 }
153 return NULL;
154}
155
Johannes Bergee2da992008-07-09 10:28:41 +0200156#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Takashi Iwai53d2f742005-11-17 13:56:05 +0100158static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 switch (tid->dev_class) {
161 case SNDRV_TIMER_CLASS_GLOBAL:
162 if (tid->device < timer_limit)
163 request_module("snd-timer-%i", tid->device);
164 break;
165 case SNDRV_TIMER_CLASS_CARD:
166 case SNDRV_TIMER_CLASS_PCM:
167 if (tid->card < snd_ecards_limit)
168 request_module("snd-card-%i", tid->card);
169 break;
170 default:
171 break;
172 }
173}
174
175#endif
176
177/*
178 * look for a master instance matching with the slave id of the given slave.
179 * when found, relink the open_link of the slave.
180 *
181 * call this with register_mutex down.
182 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100183static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100185 struct snd_timer *timer;
186 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200189 list_for_each_entry(timer, &snd_timer_list, device_list) {
190 list_for_each_entry(master, &timer->open_list_head, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (slave->slave_class == master->slave_class &&
192 slave->slave_id == master->slave_id) {
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100193 list_move_tail(&slave->open_list,
194 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 spin_lock_irq(&slave_active_lock);
196 slave->master = master;
197 slave->timer = master->timer;
198 spin_unlock_irq(&slave_active_lock);
199 return;
200 }
201 }
202 }
203}
204
205/*
206 * look for slave instances matching with the slave id of the given master.
207 * when found, relink the open_link of slaves.
208 *
209 * call this with register_mutex down.
210 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100211static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200213 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200216 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (slave->slave_class == master->slave_class &&
218 slave->slave_id == master->slave_id) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200219 list_move_tail(&slave->open_list, &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 spin_lock_irq(&slave_active_lock);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100221 spin_lock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 slave->master = master;
223 slave->timer = master->timer;
224 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200225 list_add_tail(&slave->active_list,
226 &master->slave_active_head);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100227 spin_unlock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spin_unlock_irq(&slave_active_lock);
229 }
230 }
231}
232
233/*
234 * open a timer instance
235 * when opening a master, the slave id must be here given.
236 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100237int snd_timer_open(struct snd_timer_instance **ti,
238 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 unsigned int slave_id)
240{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100241 struct snd_timer *timer;
242 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
245 /* open a slave instance */
246 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
247 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100248 pr_debug("ALSA: timer: invalid slave class %i\n",
249 tid->dev_sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return -EINVAL;
251 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100252 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200254 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100255 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200256 return -ENOMEM;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 timeri->slave_class = tid->dev_sclass;
259 timeri->slave_id = tid->device;
260 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
261 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
262 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100263 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *ti = timeri;
265 return 0;
266 }
267
268 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100269 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 timer = snd_timer_find(tid);
Johannes Bergee2da992008-07-09 10:28:41 +0200271#ifdef CONFIG_MODULES
272 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100273 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100275 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 timer = snd_timer_find(tid);
277 }
278#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200279 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100280 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -ENODEV;
282 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200283 if (!list_empty(&timer->open_list_head)) {
284 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100285 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200286 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100287 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200288 return -EBUSY;
289 }
290 }
291 timeri = snd_timer_instance_new(owner, timer);
292 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100293 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200294 return -ENOMEM;
295 }
Takashi Iwai230323d2016-01-21 17:19:31 +0100296 /* take a card refcount for safe disconnection */
297 if (timer->card)
298 get_device(&timer->card->card_dev);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200299 timeri->slave_class = tid->dev_sclass;
300 timeri->slave_id = slave_id;
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200301
302 if (list_empty(&timer->open_list_head) && timer->hw.open) {
303 int err = timer->hw.open(timer);
304 if (err) {
305 kfree(timeri->owner);
306 kfree(timeri);
307
308 if (timer->card)
309 put_device(&timer->card->card_dev);
310 module_put(timer->module);
311 mutex_unlock(&register_mutex);
312 return err;
313 }
314 }
315
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200316 list_add_tail(&timeri->open_list, &timer->open_list_head);
317 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100318 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *ti = timeri;
320 return 0;
321}
Takashi Iwai98856392017-06-16 16:16:05 +0200322EXPORT_SYMBOL(snd_timer_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * close a timer instance
326 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100327int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100329 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200330 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Takashi Iwai00728892008-08-14 07:51:57 +0200332 if (snd_BUG_ON(!timeri))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200333 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100335 mutex_lock(&register_mutex);
336 list_del(&timeri->open_list);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* force to stop the timer */
339 snd_timer_stop(timeri);
340
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100341 timer = timeri->timer;
342 if (timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* wait, until the active callback is finished */
344 spin_lock_irq(&timer->lock);
345 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
346 spin_unlock_irq(&timer->lock);
347 udelay(10);
348 spin_lock_irq(&timer->lock);
349 }
350 spin_unlock_irq(&timer->lock);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* remove slave links */
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100353 spin_lock_irq(&slave_active_lock);
354 spin_lock(&timer->lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200355 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
356 open_list) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200357 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 slave->master = NULL;
359 slave->timer = NULL;
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100360 list_del_init(&slave->ack_list);
361 list_del_init(&slave->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100363 spin_unlock(&timer->lock);
364 spin_unlock_irq(&slave_active_lock);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100365
366 /* slave doesn't need to release timer resources below */
367 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
368 timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (timeri->private_free)
372 timeri->private_free(timeri);
373 kfree(timeri->owner);
374 kfree(timeri);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100375
376 if (timer) {
377 if (list_empty(&timer->open_list_head) && timer->hw.close)
378 timer->hw.close(timer);
379 /* release a card refcount for safe disconnection */
380 if (timer->card)
381 put_device(&timer->card->card_dev);
Clemens Ladischde242142005-10-12 17:12:31 +0200382 module_put(timer->module);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100383 }
384
385 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
387}
Takashi Iwai98856392017-06-16 16:16:05 +0200388EXPORT_SYMBOL(snd_timer_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Takashi Iwai53d2f742005-11-17 13:56:05 +0100390unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100392 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (timeri == NULL)
395 return 0;
396 if ((timer = timeri->timer) != NULL) {
397 if (timer->hw.c_resolution)
398 return timer->hw.c_resolution(timer);
399 return timer->hw.resolution;
400 }
401 return 0;
402}
Takashi Iwai98856392017-06-16 16:16:05 +0200403EXPORT_SYMBOL(snd_timer_resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Takashi Iwai53d2f742005-11-17 13:56:05 +0100405static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100407 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100409 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct timespec tstamp;
411
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100412 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +0000413 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100414 else
415 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200416 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
417 event > SNDRV_TIMER_EVENT_PAUSE))
418 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200419 if (event == SNDRV_TIMER_EVENT_START ||
420 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 resolution = snd_timer_resolution(ti);
422 if (ti->ccallback)
Jaroslav Kyselab30477d52010-03-03 11:05:55 +0100423 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
425 return;
426 timer = ti->timer;
427 if (timer == NULL)
428 return;
429 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
430 return;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200431 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (ts->ccallback)
Takashi Iwai117159f2016-02-08 17:36:25 +0100433 ts->ccallback(ts, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100436/* start/continue a master timer */
437static int snd_timer_start1(struct snd_timer_instance *timeri,
438 bool start, unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100440 struct snd_timer *timer;
441 int result;
442 unsigned long flags;
443
444 timer = timeri->timer;
445 if (!timer)
446 return -EINVAL;
447
448 spin_lock_irqsave(&timer->lock, flags);
449 if (timer->card && timer->card->shutdown) {
450 result = -ENODEV;
451 goto unlock;
452 }
453 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
454 SNDRV_TIMER_IFLG_START)) {
455 result = -EBUSY;
456 goto unlock;
457 }
458
459 if (start)
460 timeri->ticks = timeri->cticks = ticks;
461 else if (!timeri->cticks)
462 timeri->cticks = 1;
463 timeri->pticks = 0;
464
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100465 list_move_tail(&timeri->active_list, &timer->active_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (timer->running) {
467 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
468 goto __start_now;
469 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
470 timeri->flags |= SNDRV_TIMER_IFLG_START;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100471 result = 1; /* delayed start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 } else {
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100473 if (start)
474 timer->sticks = ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 timer->hw.start(timer);
476 __start_now:
477 timer->running++;
478 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100479 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100481 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
482 SNDRV_TIMER_EVENT_CONTINUE);
483 unlock:
484 spin_unlock_irqrestore(&timer->lock, flags);
485 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100488/* start/continue a slave timer */
489static int snd_timer_start_slave(struct snd_timer_instance *timeri,
490 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 unsigned long flags;
493
494 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwaif784beb2016-01-30 23:09:08 +0100495 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
496 spin_unlock_irqrestore(&slave_active_lock, flags);
497 return -EBUSY;
498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100500 if (timeri->master && timeri->timer) {
501 spin_lock(&timeri->timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200502 list_add_tail(&timeri->active_list,
503 &timeri->master->slave_active_head);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100504 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
505 SNDRV_TIMER_EVENT_CONTINUE);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100506 spin_unlock(&timeri->timer->lock);
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 spin_unlock_irqrestore(&slave_active_lock, flags);
509 return 1; /* delayed start */
510}
511
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100512/* stop/pause a master timer */
513static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100515 struct snd_timer *timer;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100516 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unsigned long flags;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 timer = timeri->timer;
520 if (!timer)
521 return -EINVAL;
522 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwaif784beb2016-01-30 23:09:08 +0100523 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
524 SNDRV_TIMER_IFLG_START))) {
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100525 result = -EBUSY;
526 goto unlock;
Takashi Iwaif784beb2016-01-30 23:09:08 +0100527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 list_del_init(&timeri->ack_list);
529 list_del_init(&timeri->active_list);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100530 if (timer->card && timer->card->shutdown)
531 goto unlock;
532 if (stop) {
533 timeri->cticks = timeri->ticks;
534 timeri->pticks = 0;
Takashi Iwai230323d2016-01-21 17:19:31 +0100535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
537 !(--timer->running)) {
538 timer->hw.stop(timer);
539 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
540 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
541 snd_timer_reschedule(timer, 0);
542 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
543 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
544 timer->hw.start(timer);
545 }
546 }
547 }
Takashi Iwaic3b16812016-01-14 17:01:46 +0100548 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Takashi Iwai9f8a7652016-09-07 15:45:31 +0200549 if (stop)
550 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
551 else
552 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100553 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
554 SNDRV_TIMER_EVENT_CONTINUE);
555 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 spin_unlock_irqrestore(&timer->lock, flags);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100557 return result;
558}
559
560/* stop/pause a slave timer */
561static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
562{
563 unsigned long flags;
564
565 spin_lock_irqsave(&slave_active_lock, flags);
566 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
567 spin_unlock_irqrestore(&slave_active_lock, flags);
568 return -EBUSY;
569 }
570 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
571 if (timeri->timer) {
572 spin_lock(&timeri->timer->lock);
573 list_del_init(&timeri->ack_list);
574 list_del_init(&timeri->active_list);
575 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
576 SNDRV_TIMER_EVENT_CONTINUE);
577 spin_unlock(&timeri->timer->lock);
578 }
579 spin_unlock_irqrestore(&slave_active_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581}
582
583/*
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100584 * start the timer instance
585 */
586int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
587{
588 if (timeri == NULL || ticks < 1)
589 return -EINVAL;
590 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
591 return snd_timer_start_slave(timeri, true);
592 else
593 return snd_timer_start1(timeri, true, ticks);
594}
Takashi Iwai98856392017-06-16 16:16:05 +0200595EXPORT_SYMBOL(snd_timer_start);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100596
597/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * stop the timer instance.
599 *
600 * do not call this from the timer callback!
601 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100602int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100604 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
605 return snd_timer_stop_slave(timeri, true);
606 else
607 return snd_timer_stop1(timeri, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
Takashi Iwai98856392017-06-16 16:16:05 +0200609EXPORT_SYMBOL(snd_timer_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/*
612 * start again.. the tick is kept.
613 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100614int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwai9f8a7652016-09-07 15:45:31 +0200616 /* timer can continue only after pause */
617 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
618 return -EINVAL;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100621 return snd_timer_start_slave(timeri, false);
622 else
623 return snd_timer_start1(timeri, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Takashi Iwai98856392017-06-16 16:16:05 +0200625EXPORT_SYMBOL(snd_timer_continue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627/*
628 * pause.. remember the ticks left
629 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100630int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100632 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
633 return snd_timer_stop_slave(timeri, false);
634 else
635 return snd_timer_stop1(timeri, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
Takashi Iwai98856392017-06-16 16:16:05 +0200637EXPORT_SYMBOL(snd_timer_pause);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/*
640 * reschedule the timer
641 *
642 * start pending instances and check the scheduling ticks.
643 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
644 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100645static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100647 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Johannes Berg9244b2c2006-10-05 16:02:22 +0200650 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (ti->flags & SNDRV_TIMER_IFLG_START) {
652 ti->flags &= ~SNDRV_TIMER_IFLG_START;
653 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
654 timer->running++;
655 }
656 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
657 if (ticks > ti->cticks)
658 ticks = ti->cticks;
659 }
660 }
661 if (ticks == ~0UL) {
662 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
663 return;
664 }
665 if (ticks > timer->hw.ticks)
666 ticks = timer->hw.ticks;
667 if (ticks_left != ticks)
668 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
669 timer->sticks = ticks;
670}
671
672/*
673 * timer tasklet
674 *
675 */
676static void snd_timer_tasklet(unsigned long arg)
677{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100678 struct snd_timer *timer = (struct snd_timer *) arg;
679 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct list_head *p;
681 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200682 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Takashi Iwai230323d2016-01-21 17:19:31 +0100684 if (timer->card && timer->card->shutdown)
685 return;
686
Takashi Iwai2999ff52006-07-05 17:16:58 +0200687 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* now process all callbacks */
689 while (!list_empty(&timer->sack_list_head)) {
690 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100691 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* remove from ack_list and make empty */
694 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ticks = ti->pticks;
697 ti->pticks = 0;
698 resolution = ti->resolution;
699
700 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
701 spin_unlock(&timer->lock);
702 if (ti->callback)
703 ti->callback(ti, resolution, ticks);
704 spin_lock(&timer->lock);
705 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
706 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200707 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
711 * timer interrupt
712 *
713 * ticks_left is usually equal to timer->sticks.
714 *
715 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100716void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200718 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200720 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100721 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int use_tasklet = 0;
723
724 if (timer == NULL)
725 return;
726
Takashi Iwai230323d2016-01-21 17:19:31 +0100727 if (timer->card && timer->card->shutdown)
728 return;
729
Takashi Iwaib32425a2005-11-18 18:52:14 +0100730 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* remember the current resolution */
733 if (timer->hw.c_resolution)
734 resolution = timer->hw.c_resolution(timer);
735 else
736 resolution = timer->hw.resolution;
737
738 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200739 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200740 * processed instance is relinked to done_list_head before the callback
741 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200743 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
744 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
746 continue;
747 ti->pticks += ticks_left;
748 ti->resolution = resolution;
749 if (ti->cticks < ticks_left)
750 ti->cticks = 0;
751 else
752 ti->cticks -= ticks_left;
753 if (ti->cticks) /* not expired */
754 continue;
755 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
756 ti->cticks = ti->ticks;
757 } else {
758 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai094fd3b2016-02-04 17:06:13 +0100759 --timer->running;
760 list_del_init(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200762 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
763 (ti->flags & SNDRV_TIMER_IFLG_FAST))
764 ack_list_head = &timer->ack_list_head;
765 else
766 ack_list_head = &timer->sack_list_head;
767 if (list_empty(&ti->ack_list))
768 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200769 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 ts->pticks = ti->pticks;
771 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200772 if (list_empty(&ts->ack_list))
773 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 }
776 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200777 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (timer->running) {
779 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
780 timer->hw.stop(timer);
781 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
782 }
783 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
784 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
785 /* restart timer */
786 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
787 timer->hw.start(timer);
788 }
789 } else {
790 timer->hw.stop(timer);
791 }
792
793 /* now process all fast callbacks */
794 while (!list_empty(&timer->ack_list_head)) {
795 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100796 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* remove from ack_list and make empty */
799 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ticks = ti->pticks;
802 ti->pticks = 0;
803
804 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
805 spin_unlock(&timer->lock);
806 if (ti->callback)
807 ti->callback(ti, resolution, ticks);
808 spin_lock(&timer->lock);
809 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
810 }
811
812 /* do we have any slow callbacks? */
813 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100814 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100817 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
Takashi Iwai98856392017-06-16 16:16:05 +0200819EXPORT_SYMBOL(snd_timer_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821/*
822
823 */
824
Takashi Iwai53d2f742005-11-17 13:56:05 +0100825int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
826 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100828 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100830 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .dev_free = snd_timer_dev_free,
832 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200833 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 };
835
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200836 if (snd_BUG_ON(!tid))
837 return -EINVAL;
838 if (rtimer)
839 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200840 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100841 if (!timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -ENOMEM;
843 timer->tmr_class = tid->dev_class;
844 timer->card = card;
845 timer->tmr_device = tid->device;
846 timer->tmr_subdevice = tid->subdevice;
847 if (id)
848 strlcpy(timer->id, id, sizeof(timer->id));
Vegard Nossum6b760bb2016-08-29 00:33:50 +0200849 timer->sticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 INIT_LIST_HEAD(&timer->device_list);
851 INIT_LIST_HEAD(&timer->open_list_head);
852 INIT_LIST_HEAD(&timer->active_list_head);
853 INIT_LIST_HEAD(&timer->ack_list_head);
854 INIT_LIST_HEAD(&timer->sack_list_head);
855 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200856 tasklet_init(&timer->task_queue, snd_timer_tasklet,
857 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200859 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200860 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
861 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 snd_timer_free(timer);
863 return err;
864 }
865 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200866 if (rtimer)
867 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
Takashi Iwai98856392017-06-16 16:16:05 +0200870EXPORT_SYMBOL(snd_timer_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Takashi Iwai53d2f742005-11-17 13:56:05 +0100872static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200874 if (!timer)
875 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200876
877 mutex_lock(&register_mutex);
878 if (! list_empty(&timer->open_list_head)) {
879 struct list_head *p, *n;
880 struct snd_timer_instance *ti;
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100881 pr_warn("ALSA: timer %p is busy?\n", timer);
Takashi Iwaic4614822006-06-23 14:38:23 +0200882 list_for_each_safe(p, n, &timer->open_list_head) {
883 list_del_init(p);
884 ti = list_entry(p, struct snd_timer_instance, open_list);
885 ti->timer = NULL;
886 }
887 }
888 list_del(&timer->device_list);
889 mutex_unlock(&register_mutex);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (timer->private_free)
892 timer->private_free(timer);
893 kfree(timer);
894 return 0;
895}
896
Takashi Iwai53d2f742005-11-17 13:56:05 +0100897static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100899 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return snd_timer_free(timer);
901}
902
Takashi Iwai53d2f742005-11-17 13:56:05 +0100903static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100905 struct snd_timer *timer = dev->device_data;
906 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200908 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
909 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
911 !timer->hw.resolution && timer->hw.c_resolution == NULL)
912 return -EINVAL;
913
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100914 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200915 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (timer1->tmr_class > timer->tmr_class)
917 break;
918 if (timer1->tmr_class < timer->tmr_class)
919 continue;
920 if (timer1->card && timer->card) {
921 if (timer1->card->number > timer->card->number)
922 break;
923 if (timer1->card->number < timer->card->number)
924 continue;
925 }
926 if (timer1->tmr_device > timer->tmr_device)
927 break;
928 if (timer1->tmr_device < timer->tmr_device)
929 continue;
930 if (timer1->tmr_subdevice > timer->tmr_subdevice)
931 break;
932 if (timer1->tmr_subdevice < timer->tmr_subdevice)
933 continue;
934 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100935 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -EBUSY;
937 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200938 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100939 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
Takashi Iwaic4614822006-06-23 14:38:23 +0200943static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100945 struct snd_timer *timer = device->device_data;
Takashi Iwai230323d2016-01-21 17:19:31 +0100946 struct snd_timer_instance *ti;
947
Takashi Iwaic4614822006-06-23 14:38:23 +0200948 mutex_lock(&register_mutex);
949 list_del_init(&timer->device_list);
Takashi Iwai230323d2016-01-21 17:19:31 +0100950 /* wake up pending sleepers */
951 list_for_each_entry(ti, &timer->open_list_head, open_list) {
Takashi Iwai40ed9442016-01-21 17:43:08 +0100952 if (ti->disconnect)
953 ti->disconnect(ti);
Takashi Iwai230323d2016-01-21 17:19:31 +0100954 }
Takashi Iwaic4614822006-06-23 14:38:23 +0200955 mutex_unlock(&register_mutex);
956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Takashi Iwai53d2f742005-11-17 13:56:05 +0100959void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 unsigned long flags;
962 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100963 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Takashi Iwai230323d2016-01-21 17:19:31 +0100965 if (timer->card && timer->card->shutdown)
966 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200967 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
968 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200969 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
970 event > SNDRV_TIMER_EVENT_MRESUME))
971 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200973 if (event == SNDRV_TIMER_EVENT_MSTART ||
974 event == SNDRV_TIMER_EVENT_MCONTINUE ||
975 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (timer->hw.c_resolution)
977 resolution = timer->hw.c_resolution(timer);
978 else
979 resolution = timer->hw.resolution;
980 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200981 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (ti->ccallback)
983 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200984 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (ts->ccallback)
986 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 spin_unlock_irqrestore(&timer->lock, flags);
989}
Takashi Iwai98856392017-06-16 16:16:05 +0200990EXPORT_SYMBOL(snd_timer_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992/*
993 * exported functions for global timers
994 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100995int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100997 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1000 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1001 tid.card = -1;
1002 tid.device = device;
1003 tid.subdevice = 0;
1004 return snd_timer_new(NULL, id, &tid, rtimer);
1005}
Takashi Iwai98856392017-06-16 16:16:05 +02001006EXPORT_SYMBOL(snd_timer_global_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Takashi Iwai53d2f742005-11-17 13:56:05 +01001008int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 return snd_timer_free(timer);
1011}
Takashi Iwai98856392017-06-16 16:16:05 +02001012EXPORT_SYMBOL(snd_timer_global_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Takashi Iwai53d2f742005-11-17 13:56:05 +01001014int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001016 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 memset(&dev, 0, sizeof(dev));
1019 dev.device_data = timer;
1020 return snd_timer_dev_register(&dev);
1021}
Takashi Iwai98856392017-06-16 16:16:05 +02001022EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001024/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * System timer
1026 */
1027
1028struct snd_timer_system_private {
1029 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 unsigned long last_expires;
1031 unsigned long last_jiffies;
1032 unsigned long correction;
1033};
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035static void snd_timer_s_function(unsigned long data)
1036{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001037 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct snd_timer_system_private *priv = timer->private_data;
1039 unsigned long jiff = jiffies;
1040 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +02001041 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1043}
1044
Takashi Iwai53d2f742005-11-17 13:56:05 +01001045static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct snd_timer_system_private *priv;
1048 unsigned long njiff;
1049
1050 priv = (struct snd_timer_system_private *) timer->private_data;
1051 njiff = (priv->last_jiffies = jiffies);
1052 if (priv->correction > timer->sticks - 1) {
1053 priv->correction -= timer->sticks - 1;
1054 njiff++;
1055 } else {
1056 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +02001057 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Takashi Iwai4a070832016-04-01 12:28:16 +02001059 priv->last_expires = njiff;
1060 mod_timer(&priv->tlist, njiff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return 0;
1062}
1063
Takashi Iwai53d2f742005-11-17 13:56:05 +01001064static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct snd_timer_system_private *priv;
1067 unsigned long jiff;
1068
1069 priv = (struct snd_timer_system_private *) timer->private_data;
1070 del_timer(&priv->tlist);
1071 jiff = jiffies;
1072 if (time_before(jiff, priv->last_expires))
1073 timer->sticks = priv->last_expires - jiff;
1074 else
1075 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001076 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 0;
1078}
1079
Takashi Iwaif1463572016-02-02 14:14:10 +01001080static int snd_timer_s_close(struct snd_timer *timer)
1081{
1082 struct snd_timer_system_private *priv;
1083
1084 priv = (struct snd_timer_system_private *)timer->private_data;
1085 del_timer_sync(&priv->tlist);
1086 return 0;
1087}
1088
Takashi Iwai53d2f742005-11-17 13:56:05 +01001089static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1092 .resolution = 1000000000L / HZ,
1093 .ticks = 10000000L,
Takashi Iwaif1463572016-02-02 14:14:10 +01001094 .close = snd_timer_s_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 .start = snd_timer_s_start,
1096 .stop = snd_timer_s_stop
1097};
1098
Takashi Iwai53d2f742005-11-17 13:56:05 +01001099static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 kfree(timer->private_data);
1102}
1103
1104static int snd_timer_register_system(void)
1105{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001106 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct snd_timer_system_private *priv;
1108 int err;
1109
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001110 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1111 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return err;
1113 strcpy(timer->name, "system timer");
1114 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001115 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (priv == NULL) {
1117 snd_timer_free(timer);
1118 return -ENOMEM;
1119 }
Takashi Iwaif169c102015-01-19 11:26:25 +01001120 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 timer->private_data = priv;
1122 timer->private_free = snd_timer_free_system;
1123 return snd_timer_global_register(timer);
1124}
1125
Jie Yangcd6a6502015-05-27 19:45:45 +08001126#ifdef CONFIG_SND_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/*
1128 * Info interface
1129 */
1130
Takashi Iwai53d2f742005-11-17 13:56:05 +01001131static void snd_timer_proc_read(struct snd_info_entry *entry,
1132 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001134 struct snd_timer *timer;
1135 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001137 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001138 list_for_each_entry(timer, &snd_timer_list, device_list) {
Takashi Iwai230323d2016-01-21 17:19:31 +01001139 if (timer->card && timer->card->shutdown)
1140 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 switch (timer->tmr_class) {
1142 case SNDRV_TIMER_CLASS_GLOBAL:
1143 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1144 break;
1145 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001146 snd_iprintf(buffer, "C%i-%i: ",
1147 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001150 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1151 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001154 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1155 timer->card ? timer->card->number : -1,
1156 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 snd_iprintf(buffer, "%s :", timer->name);
1159 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001160 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1161 timer->hw.resolution / 1000,
1162 timer->hw.resolution % 1000,
1163 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1165 snd_iprintf(buffer, " SLAVE");
1166 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001167 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001168 snd_iprintf(buffer, " Client %s : %s\n",
1169 ti->owner ? ti->owner : "unknown",
1170 ti->flags & (SNDRV_TIMER_IFLG_START |
1171 SNDRV_TIMER_IFLG_RUNNING)
1172 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001174 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001177static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001178
1179static void __init snd_timer_proc_init(void)
1180{
1181 struct snd_info_entry *entry;
1182
1183 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1184 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001185 entry->c.text.read = snd_timer_proc_read;
1186 if (snd_info_register(entry) < 0) {
1187 snd_info_free_entry(entry);
1188 entry = NULL;
1189 }
1190 }
1191 snd_timer_proc_entry = entry;
1192}
1193
1194static void __exit snd_timer_proc_done(void)
1195{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001196 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001197}
Jie Yangcd6a6502015-05-27 19:45:45 +08001198#else /* !CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +01001199#define snd_timer_proc_init()
1200#define snd_timer_proc_done()
1201#endif
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203/*
1204 * USER SPACE interface
1205 */
1206
Takashi Iwai53d2f742005-11-17 13:56:05 +01001207static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 unsigned long resolution,
1209 unsigned long ticks)
1210{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001211 struct snd_timer_user *tu = timeri->callback_data;
1212 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 spin_lock(&tu->qlock);
1216 if (tu->qused > 0) {
1217 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1218 r = &tu->queue[prev];
1219 if (r->resolution == resolution) {
1220 r->ticks += ticks;
1221 goto __wake;
1222 }
1223 }
1224 if (tu->qused >= tu->queue_size) {
1225 tu->overrun++;
1226 } else {
1227 r = &tu->queue[tu->qtail++];
1228 tu->qtail %= tu->queue_size;
1229 r->resolution = resolution;
1230 r->ticks = ticks;
1231 tu->qused++;
1232 }
1233 __wake:
1234 spin_unlock(&tu->qlock);
1235 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1236 wake_up(&tu->qchange_sleep);
1237}
1238
Takashi Iwai53d2f742005-11-17 13:56:05 +01001239static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1240 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 if (tu->qused >= tu->queue_size) {
1243 tu->overrun++;
1244 } else {
1245 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1246 tu->qtail %= tu->queue_size;
1247 tu->qused++;
1248 }
1249}
1250
Takashi Iwai53d2f742005-11-17 13:56:05 +01001251static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1252 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 struct timespec *tstamp,
1254 unsigned long resolution)
1255{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001256 struct snd_timer_user *tu = timeri->callback_data;
1257 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001258 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001260 if (event >= SNDRV_TIMER_EVENT_START &&
1261 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 tu->tstamp = *tstamp;
1263 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1264 return;
Kangjie Lu9a47e9c2016-05-03 16:44:20 -04001265 memset(&r1, 0, sizeof(r1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 r1.event = event;
1267 r1.tstamp = *tstamp;
1268 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001269 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001271 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1273 wake_up(&tu->qchange_sleep);
1274}
1275
Takashi Iwai40ed9442016-01-21 17:43:08 +01001276static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1277{
1278 struct snd_timer_user *tu = timeri->callback_data;
1279
1280 tu->disconnected = true;
1281 wake_up(&tu->qchange_sleep);
1282}
1283
Takashi Iwai53d2f742005-11-17 13:56:05 +01001284static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 unsigned long resolution,
1286 unsigned long ticks)
1287{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001288 struct snd_timer_user *tu = timeri->callback_data;
1289 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 struct timespec tstamp;
1291 int prev, append = 0;
1292
Dan Carpentera8c006a2017-03-31 18:22:23 +03001293 memset(&r1, 0, sizeof(r1));
Takashi Iwai07799e72005-10-10 11:49:49 +02001294 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001296 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1297 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 spin_unlock(&tu->qlock);
1299 return;
1300 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001301 if (tu->last_resolution != resolution || ticks > 0) {
1302 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +00001303 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001304 else
1305 getnstimeofday(&tstamp);
1306 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001307 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1308 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1310 r1.tstamp = tstamp;
1311 r1.val = resolution;
1312 snd_timer_user_append_to_tqueue(tu, &r1);
1313 tu->last_resolution = resolution;
1314 append++;
1315 }
1316 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1317 goto __wake;
1318 if (ticks == 0)
1319 goto __wake;
1320 if (tu->qused > 0) {
1321 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1322 r = &tu->tqueue[prev];
1323 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1324 r->tstamp = tstamp;
1325 r->val += ticks;
1326 append++;
1327 goto __wake;
1328 }
1329 }
1330 r1.event = SNDRV_TIMER_EVENT_TICK;
1331 r1.tstamp = tstamp;
1332 r1.val = ticks;
1333 snd_timer_user_append_to_tqueue(tu, &r1);
1334 append++;
1335 __wake:
1336 spin_unlock(&tu->qlock);
1337 if (append == 0)
1338 return;
1339 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1340 wake_up(&tu->qchange_sleep);
1341}
1342
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001343static int realloc_user_queue(struct snd_timer_user *tu, int size)
1344{
1345 struct snd_timer_read *queue = NULL;
1346 struct snd_timer_tread *tqueue = NULL;
1347
1348 if (tu->tread) {
1349 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1350 if (!tqueue)
1351 return -ENOMEM;
1352 } else {
1353 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1354 if (!queue)
1355 return -ENOMEM;
1356 }
1357
1358 spin_lock_irq(&tu->qlock);
1359 kfree(tu->queue);
1360 kfree(tu->tqueue);
1361 tu->queue_size = size;
1362 tu->queue = queue;
1363 tu->tqueue = tqueue;
1364 tu->qhead = tu->qtail = tu->qused = 0;
1365 spin_unlock_irq(&tu->qlock);
1366
1367 return 0;
1368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370static int snd_timer_user_open(struct inode *inode, struct file *file)
1371{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001372 struct snd_timer_user *tu;
Takashi Iwai02f48652010-04-13 11:49:04 +02001373 int err;
1374
1375 err = nonseekable_open(inode, file);
1376 if (err < 0)
1377 return err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001378
Takashi Iwaica2c0962005-09-09 14:20:23 +02001379 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (tu == NULL)
1381 return -ENOMEM;
1382 spin_lock_init(&tu->qlock);
1383 init_waitqueue_head(&tu->qchange_sleep);
Takashi Iwaiaf368022016-01-13 17:48:01 +01001384 mutex_init(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 tu->ticks = 1;
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001386 if (realloc_user_queue(tu, 128) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 kfree(tu);
1388 return -ENOMEM;
1389 }
1390 file->private_data = tu;
1391 return 0;
1392}
1393
1394static int snd_timer_user_release(struct inode *inode, struct file *file)
1395{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001396 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 if (file->private_data) {
1399 tu = file->private_data;
1400 file->private_data = NULL;
Takashi Iwaiaf368022016-01-13 17:48:01 +01001401 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (tu->timeri)
1403 snd_timer_close(tu->timeri);
Takashi Iwaiaf368022016-01-13 17:48:01 +01001404 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 kfree(tu->queue);
1406 kfree(tu->tqueue);
1407 kfree(tu);
1408 }
1409 return 0;
1410}
1411
Takashi Iwai53d2f742005-11-17 13:56:05 +01001412static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1415 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1416 id->card = -1;
1417 id->device = -1;
1418 id->subdevice = -1;
1419}
1420
Takashi Iwai53d2f742005-11-17 13:56:05 +01001421static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 id->dev_class = timer->tmr_class;
1424 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1425 id->card = timer->card ? timer->card->number : -1;
1426 id->device = timer->tmr_device;
1427 id->subdevice = timer->tmr_subdevice;
1428}
1429
Takashi Iwai53d2f742005-11-17 13:56:05 +01001430static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001432 struct snd_timer_id id;
1433 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (copy_from_user(&id, _tid, sizeof(id)))
1437 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001438 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (id.dev_class < 0) { /* first item */
1440 if (list_empty(&snd_timer_list))
1441 snd_timer_user_zero_id(&id);
1442 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001443 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001444 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 snd_timer_user_copy_id(&id, timer);
1446 }
1447 } else {
1448 switch (id.dev_class) {
1449 case SNDRV_TIMER_CLASS_GLOBAL:
1450 id.device = id.device < 0 ? 0 : id.device + 1;
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 > SNDRV_TIMER_CLASS_GLOBAL) {
1454 snd_timer_user_copy_id(&id, timer);
1455 break;
1456 }
1457 if (timer->tmr_device >= id.device) {
1458 snd_timer_user_copy_id(&id, timer);
1459 break;
1460 }
1461 }
1462 if (p == &snd_timer_list)
1463 snd_timer_user_zero_id(&id);
1464 break;
1465 case SNDRV_TIMER_CLASS_CARD:
1466 case SNDRV_TIMER_CLASS_PCM:
1467 if (id.card < 0) {
1468 id.card = 0;
1469 } else {
Dan Carpentere8ed6822017-03-31 18:21:41 +03001470 if (id.device < 0) {
1471 id.device = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 } else {
Dan Carpentere8ed6822017-03-31 18:21:41 +03001473 if (id.subdevice < 0)
1474 id.subdevice = 0;
1475 else
1476 id.subdevice++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 }
1479 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001480 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (timer->tmr_class > id.dev_class) {
1482 snd_timer_user_copy_id(&id, timer);
1483 break;
1484 }
1485 if (timer->tmr_class < id.dev_class)
1486 continue;
1487 if (timer->card->number > id.card) {
1488 snd_timer_user_copy_id(&id, timer);
1489 break;
1490 }
1491 if (timer->card->number < id.card)
1492 continue;
1493 if (timer->tmr_device > id.device) {
1494 snd_timer_user_copy_id(&id, timer);
1495 break;
1496 }
1497 if (timer->tmr_device < id.device)
1498 continue;
1499 if (timer->tmr_subdevice > id.subdevice) {
1500 snd_timer_user_copy_id(&id, timer);
1501 break;
1502 }
1503 if (timer->tmr_subdevice < id.subdevice)
1504 continue;
1505 snd_timer_user_copy_id(&id, timer);
1506 break;
1507 }
1508 if (p == &snd_timer_list)
1509 snd_timer_user_zero_id(&id);
1510 break;
1511 default:
1512 snd_timer_user_zero_id(&id);
1513 }
1514 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001515 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1517 return -EFAULT;
1518 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001521static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001522 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001524 struct snd_timer_ginfo *ginfo;
1525 struct snd_timer_id tid;
1526 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 struct list_head *p;
1528 int err = 0;
1529
Li Zefanef44a1e2009-04-10 09:43:08 +08001530 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1531 if (IS_ERR(ginfo))
1532 return PTR_ERR(ginfo);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 tid = ginfo->tid;
1535 memset(ginfo, 0, sizeof(*ginfo));
1536 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001537 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 t = snd_timer_find(&tid);
1539 if (t != NULL) {
1540 ginfo->card = t->card ? t->card->number : -1;
1541 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1542 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1543 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1544 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1545 ginfo->resolution = t->hw.resolution;
1546 if (t->hw.resolution_min > 0) {
1547 ginfo->resolution_min = t->hw.resolution_min;
1548 ginfo->resolution_max = t->hw.resolution_max;
1549 }
1550 list_for_each(p, &t->open_list_head) {
1551 ginfo->clients++;
1552 }
1553 } else {
1554 err = -ENODEV;
1555 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001556 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1558 err = -EFAULT;
1559 kfree(ginfo);
1560 return err;
1561}
1562
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001563static int timer_set_gparams(struct snd_timer_gparams *gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001565 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 int err;
1567
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001568 mutex_lock(&register_mutex);
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001569 t = snd_timer_find(&gparams->tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001570 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001572 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001574 if (!list_empty(&t->open_list_head)) {
1575 err = -EBUSY;
1576 goto _error;
1577 }
1578 if (!t->hw.set_period) {
1579 err = -ENOSYS;
1580 goto _error;
1581 }
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001582 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001583_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001584 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return err;
1586}
1587
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001588static int snd_timer_user_gparams(struct file *file,
1589 struct snd_timer_gparams __user *_gparams)
1590{
1591 struct snd_timer_gparams gparams;
1592
1593 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1594 return -EFAULT;
1595 return timer_set_gparams(&gparams);
1596}
1597
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001598static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001599 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001601 struct snd_timer_gstatus gstatus;
1602 struct snd_timer_id tid;
1603 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int err = 0;
1605
1606 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1607 return -EFAULT;
1608 tid = gstatus.tid;
1609 memset(&gstatus, 0, sizeof(gstatus));
1610 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001611 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 t = snd_timer_find(&tid);
1613 if (t != NULL) {
1614 if (t->hw.c_resolution)
1615 gstatus.resolution = t->hw.c_resolution(t);
1616 else
1617 gstatus.resolution = t->hw.resolution;
1618 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001619 t->hw.precise_resolution(t, &gstatus.resolution_num,
1620 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 } else {
1622 gstatus.resolution_num = gstatus.resolution;
1623 gstatus.resolution_den = 1000000000uL;
1624 }
1625 } else {
1626 err = -ENODEV;
1627 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001628 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1630 err = -EFAULT;
1631 return err;
1632}
1633
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001634static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001635 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001637 struct snd_timer_user *tu;
1638 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001640 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 tu = file->private_data;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001643 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001645 tu->timeri = NULL;
1646 }
1647 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1648 err = -EFAULT;
1649 goto __err;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 sprintf(str, "application %i", current->pid);
1652 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1653 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001654 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1655 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001656 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001658 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1659 tu->timeri->callback = tu->tread
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001660 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001661 tu->timeri->ccallback = snd_timer_user_ccallback;
1662 tu->timeri->callback_data = (void *)tu;
1663 tu->timeri->disconnect = snd_timer_user_disconnect;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001664
1665 __err:
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001666 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001669static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001670 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001672 struct snd_timer_user *tu;
1673 struct snd_timer_info *info;
1674 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int err = 0;
1676
1677 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001678 if (!tu->timeri)
1679 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001681 if (!t)
1682 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Takashi Iwaica2c0962005-09-09 14:20:23 +02001684 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (! info)
1686 return -ENOMEM;
1687 info->card = t->card ? t->card->number : -1;
1688 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1689 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1690 strlcpy(info->id, t->id, sizeof(info->id));
1691 strlcpy(info->name, t->name, sizeof(info->name));
1692 info->resolution = t->hw.resolution;
1693 if (copy_to_user(_info, info, sizeof(*_info)))
1694 err = -EFAULT;
1695 kfree(info);
1696 return err;
1697}
1698
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001699static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001700 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001702 struct snd_timer_user *tu;
1703 struct snd_timer_params params;
1704 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001708 if (!tu->timeri)
1709 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001711 if (!t)
1712 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (copy_from_user(&params, _params, sizeof(params)))
1714 return -EFAULT;
Takashi Iwai71321eb2017-02-28 14:49:07 +01001715 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1716 u64 resolution;
1717
1718 if (params.ticks < 1) {
1719 err = -EINVAL;
1720 goto _end;
1721 }
1722
1723 /* Don't allow resolution less than 1ms */
1724 resolution = snd_timer_resolution(tu->timeri);
1725 resolution *= params.ticks;
1726 if (resolution < 1000000) {
1727 err = -EINVAL;
1728 goto _end;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001731 if (params.queue_size > 0 &&
1732 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 err = -EINVAL;
1734 goto _end;
1735 }
1736 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1737 (1<<SNDRV_TIMER_EVENT_TICK)|
1738 (1<<SNDRV_TIMER_EVENT_START)|
1739 (1<<SNDRV_TIMER_EVENT_STOP)|
1740 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1741 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001742 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1743 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 (1<<SNDRV_TIMER_EVENT_MSTART)|
1745 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1746 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001747 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1748 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001749 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 err = -EINVAL;
1751 goto _end;
1752 }
1753 snd_timer_stop(tu->timeri);
1754 spin_lock_irq(&t->lock);
1755 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1756 SNDRV_TIMER_IFLG_EXCLUSIVE|
1757 SNDRV_TIMER_IFLG_EARLY_EVENT);
1758 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1759 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1760 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1761 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1762 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1763 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1764 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001765 if (params.queue_size > 0 &&
1766 (unsigned int)tu->queue_size != params.queue_size) {
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001767 err = realloc_user_queue(tu, params.queue_size);
1768 if (err < 0)
1769 goto _end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Takashi Iwaid7f910b2017-06-02 17:35:30 +02001771 spin_lock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 tu->qhead = tu->qtail = tu->qused = 0;
1773 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1774 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001775 struct snd_timer_tread tread;
Kangjie Lucec8f962016-05-03 16:44:07 -04001776 memset(&tread, 0, sizeof(tread));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 tread.event = SNDRV_TIMER_EVENT_EARLY;
1778 tread.tstamp.tv_sec = 0;
1779 tread.tstamp.tv_nsec = 0;
1780 tread.val = 0;
1781 snd_timer_user_append_to_tqueue(tu, &tread);
1782 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001783 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 r->resolution = 0;
1785 r->ticks = 0;
1786 tu->qused++;
1787 tu->qtail++;
1788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 tu->filter = params.filter;
1791 tu->ticks = params.ticks;
Takashi Iwaid7f910b2017-06-02 17:35:30 +02001792 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 err = 0;
1794 _end:
1795 if (copy_to_user(_params, &params, sizeof(params)))
1796 return -EFAULT;
1797 return err;
1798}
1799
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001800static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001801 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001803 struct snd_timer_user *tu;
1804 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001807 if (!tu->timeri)
1808 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 memset(&status, 0, sizeof(status));
1810 status.tstamp = tu->tstamp;
1811 status.resolution = snd_timer_resolution(tu->timeri);
1812 status.lost = tu->timeri->lost;
1813 status.overrun = tu->overrun;
1814 spin_lock_irq(&tu->qlock);
1815 status.queue = tu->qused;
1816 spin_unlock_irq(&tu->qlock);
1817 if (copy_to_user(_status, &status, sizeof(status)))
1818 return -EFAULT;
1819 return 0;
1820}
1821
1822static int snd_timer_user_start(struct file *file)
1823{
1824 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001825 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001828 if (!tu->timeri)
1829 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 snd_timer_stop(tu->timeri);
1831 tu->timeri->lost = 0;
1832 tu->last_resolution = 0;
1833 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1834}
1835
1836static int snd_timer_user_stop(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 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1845}
1846
1847static int snd_timer_user_continue(struct file *file)
1848{
1849 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001850 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001853 if (!tu->timeri)
1854 return -EBADFD;
Takashi Iwai9f8a7652016-09-07 15:45:31 +02001855 /* start timer instead of continue if it's not used before */
1856 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1857 return snd_timer_user_start(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 tu->timeri->lost = 0;
1859 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1860}
1861
Takashi Iwai15790a62005-05-15 15:04:14 +02001862static int snd_timer_user_pause(struct file *file)
1863{
1864 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001865 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001866
Takashi Iwai15790a62005-05-15 15:04:14 +02001867 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001868 if (!tu->timeri)
1869 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001870 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001871}
1872
Takashi Iwai8c50b372005-05-15 15:43:54 +02001873enum {
1874 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1875 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1876 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1877 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1878};
1879
Takashi Iwaiaf368022016-01-13 17:48:01 +01001880static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001881 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001883 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 void __user *argp = (void __user *)arg;
1885 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 tu = file->private_data;
1888 switch (cmd) {
1889 case SNDRV_TIMER_IOCTL_PVERSION:
1890 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1891 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1892 return snd_timer_user_next_device(argp);
1893 case SNDRV_TIMER_IOCTL_TREAD:
1894 {
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001895 int xarg, old_tread;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001896
Takashi Iwaiaf368022016-01-13 17:48:01 +01001897 if (tu->timeri) /* too late */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return -EBUSY;
Takashi Iwaiaf368022016-01-13 17:48:01 +01001899 if (get_user(xarg, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return -EFAULT;
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001901 old_tread = tu->tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 tu->tread = xarg ? 1 : 0;
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001903 if (tu->tread != old_tread &&
1904 realloc_user_queue(tu, tu->queue_size) < 0) {
1905 tu->tread = old_tread;
1906 return -ENOMEM;
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return 0;
1909 }
1910 case SNDRV_TIMER_IOCTL_GINFO:
1911 return snd_timer_user_ginfo(file, argp);
1912 case SNDRV_TIMER_IOCTL_GPARAMS:
1913 return snd_timer_user_gparams(file, argp);
1914 case SNDRV_TIMER_IOCTL_GSTATUS:
1915 return snd_timer_user_gstatus(file, argp);
1916 case SNDRV_TIMER_IOCTL_SELECT:
1917 return snd_timer_user_tselect(file, argp);
1918 case SNDRV_TIMER_IOCTL_INFO:
1919 return snd_timer_user_info(file, argp);
1920 case SNDRV_TIMER_IOCTL_PARAMS:
1921 return snd_timer_user_params(file, argp);
1922 case SNDRV_TIMER_IOCTL_STATUS:
1923 return snd_timer_user_status(file, argp);
1924 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001925 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return snd_timer_user_start(file);
1927 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001928 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return snd_timer_user_stop(file);
1930 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001931 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001933 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001934 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001935 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937 return -ENOTTY;
1938}
1939
Takashi Iwaiaf368022016-01-13 17:48:01 +01001940static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1941 unsigned long arg)
1942{
1943 struct snd_timer_user *tu = file->private_data;
1944 long ret;
1945
1946 mutex_lock(&tu->ioctl_lock);
1947 ret = __snd_timer_user_ioctl(file, cmd, arg);
1948 mutex_unlock(&tu->ioctl_lock);
1949 return ret;
1950}
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952static int snd_timer_user_fasync(int fd, struct file * file, int on)
1953{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001954 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001957 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001960static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1961 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001963 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 long result = 0, unit;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01001965 int qhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001969 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Takashi Iwaid11662f42017-06-02 15:03:38 +02001970 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 spin_lock_irq(&tu->qlock);
1972 while ((long)count - result >= unit) {
1973 while (!tu->qused) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02001974 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1977 err = -EAGAIN;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01001978 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980
1981 set_current_state(TASK_INTERRUPTIBLE);
1982 init_waitqueue_entry(&wait, current);
1983 add_wait_queue(&tu->qchange_sleep, &wait);
1984
1985 spin_unlock_irq(&tu->qlock);
Takashi Iwaid11662f42017-06-02 15:03:38 +02001986 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 schedule();
Takashi Iwaid11662f42017-06-02 15:03:38 +02001988 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 spin_lock_irq(&tu->qlock);
1990
1991 remove_wait_queue(&tu->qchange_sleep, &wait);
1992
Takashi Iwai230323d2016-01-21 17:19:31 +01001993 if (tu->disconnected) {
1994 err = -ENODEV;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01001995 goto _error;
Takashi Iwai230323d2016-01-21 17:19:31 +01001996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (signal_pending(current)) {
1998 err = -ERESTARTSYS;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01001999 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001 }
2002
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002003 qhead = tu->qhead++;
2004 tu->qhead %= tu->queue_size;
Takashi Iwai3fa69932016-07-04 14:02:15 +02002005 tu->qused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 if (tu->tread) {
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002009 if (copy_to_user(buffer, &tu->tqueue[qhead],
2010 sizeof(struct snd_timer_tread)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 } else {
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002013 if (copy_to_user(buffer, &tu->queue[qhead],
2014 sizeof(struct snd_timer_read)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 spin_lock_irq(&tu->qlock);
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002019 if (err < 0)
2020 goto _error;
2021 result += unit;
2022 buffer += unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 _error:
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002025 spin_unlock_irq(&tu->qlock);
Takashi Iwaid11662f42017-06-02 15:03:38 +02002026 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return result > 0 ? result : err;
2028}
2029
2030static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2031{
2032 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01002033 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 tu = file->private_data;
2036
2037 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 mask = 0;
Takashi Iwaid7f910b2017-06-02 17:35:30 +02002040 spin_lock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (tu->qused)
2042 mask |= POLLIN | POLLRDNORM;
Takashi Iwai230323d2016-01-21 17:19:31 +01002043 if (tu->disconnected)
2044 mask |= POLLERR;
Takashi Iwaid7f910b2017-06-02 17:35:30 +02002045 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 return mask;
2048}
2049
2050#ifdef CONFIG_COMPAT
2051#include "timer_compat.c"
2052#else
2053#define snd_timer_user_ioctl_compat NULL
2054#endif
2055
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002056static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 .owner = THIS_MODULE,
2059 .read = snd_timer_user_read,
2060 .open = snd_timer_user_open,
2061 .release = snd_timer_user_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02002062 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 .poll = snd_timer_user_poll,
2064 .unlocked_ioctl = snd_timer_user_ioctl,
2065 .compat_ioctl = snd_timer_user_ioctl_compat,
2066 .fasync = snd_timer_user_fasync,
2067};
2068
Takashi Iwai7c358602015-01-29 18:09:05 +01002069/* unregister the system timer */
2070static void snd_timer_free_all(void)
2071{
2072 struct snd_timer *timer, *n;
2073
2074 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2075 snd_timer_free(timer);
2076}
2077
Takashi Iwai89da0612015-01-29 18:12:26 +01002078static struct device timer_dev;
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080/*
2081 * ENTRY functions
2082 */
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084static int __init alsa_timer_init(void)
2085{
2086 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Takashi Iwai89da0612015-01-29 18:12:26 +01002088 snd_device_initialize(&timer_dev, NULL);
2089 dev_set_name(&timer_dev, "timer");
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002092 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2093 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01002095
Takashi Iwai7c358602015-01-29 18:09:05 +01002096 err = snd_timer_register_system();
2097 if (err < 0) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002098 pr_err("ALSA: unable to register system timer (%i)\n", err);
Takashi Iwai89da0612015-01-29 18:12:26 +01002099 put_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002100 return err;
2101 }
2102
Takashi Iwai40a4b262015-01-30 08:34:58 +01002103 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2104 &snd_timer_f_ops, NULL, &timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002105 if (err < 0) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002106 pr_err("ALSA: unable to register timer device (%i)\n", err);
Takashi Iwai7c358602015-01-29 18:09:05 +01002107 snd_timer_free_all();
Takashi Iwai89da0612015-01-29 18:12:26 +01002108 put_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002109 return err;
2110 }
2111
Takashi Iwaie28563c2005-12-01 10:42:42 +01002112 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 0;
2114}
2115
2116static void __exit alsa_timer_exit(void)
2117{
Takashi Iwai40a4b262015-01-30 08:34:58 +01002118 snd_unregister_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002119 snd_timer_free_all();
Takashi Iwai89da0612015-01-29 18:12:26 +01002120 put_device(&timer_dev);
Takashi Iwaie28563c2005-12-01 10:42:42 +01002121 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2123 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2124#endif
2125}
2126
2127module_init(alsa_timer_init)
2128module_exit(alsa_timer_exit)