blob: 24fed5c78273934028e392a24d34ef611da43139 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Timers abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/delay.h>
8#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
10#include <linux/time.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010011#include <linux/mutex.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050012#include <linux/device.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040013#include <linux/module.h>
Paulo Marques543537b2005-06-23 00:09:02 -070014#include <linux/string.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <sound/core.h>
17#include <sound/timer.h>
18#include <sound/control.h>
19#include <sound/info.h>
20#include <sound/minors.h>
21#include <sound/initval.h>
22#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Takashi Iwai9f8a7652016-09-07 15:45:31 +020024/* internal flags */
25#define SNDRV_TIMER_IFLG_PAUSED 0x00010000
Takashi Iwaife1b26c2019-03-27 17:02:40 +010026#define SNDRV_TIMER_IFLG_DEAD 0x00020000
Takashi Iwai9f8a7652016-09-07 15:45:31 +020027
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010028#if IS_ENABLED(CONFIG_SND_HRTIMER)
Clemens Ladisch109fef92010-11-18 09:53:54 +010029#define DEFAULT_TIMER_LIMIT 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#else
31#define DEFAULT_TIMER_LIMIT 1
32#endif
33
34static int timer_limit = DEFAULT_TIMER_LIMIT;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010035static int timer_tstamp_monotonic = 1;
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020036MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_DESCRIPTION("ALSA timer interface");
38MODULE_LICENSE("GPL");
39module_param(timer_limit, int, 0444);
40MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010041module_param(timer_tstamp_monotonic, int, 0444);
42MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Kay Sievers03cfe6f2010-11-23 17:43:19 +010044MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
45MODULE_ALIAS("devname:snd/timer");
46
Takashi Iwai53d2f742005-11-17 13:56:05 +010047struct snd_timer_user {
48 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020049 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned long ticks;
51 unsigned long overrun;
52 int qhead;
53 int qtail;
54 int qused;
55 int queue_size;
Takashi Iwai230323d2016-01-21 17:19:31 +010056 bool disconnected;
Takashi Iwai53d2f742005-11-17 13:56:05 +010057 struct snd_timer_read *queue;
58 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 spinlock_t qlock;
60 unsigned long last_resolution;
61 unsigned int filter;
62 struct timespec tstamp; /* trigger tstamp */
63 wait_queue_head_t qchange_sleep;
64 struct fasync_struct *fasync;
Takashi Iwaiaf368022016-01-13 17:48:01 +010065 struct mutex ioctl_lock;
Takashi Iwai53d2f742005-11-17 13:56:05 +010066};
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* list of timers */
69static LIST_HEAD(snd_timer_list);
70
71/* list of slave instances */
72static LIST_HEAD(snd_timer_slave_list);
73
74/* lock for slave active lists */
75static DEFINE_SPINLOCK(slave_active_lock);
76
Takashi Iwaifdea53f2019-11-06 16:42:57 +010077#define MAX_SLAVE_INSTANCES 1000
78static int num_slaves;
79
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010080static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Takashi Iwai53d2f742005-11-17 13:56:05 +010082static int snd_timer_free(struct snd_timer *timer);
83static int snd_timer_dev_free(struct snd_device *device);
84static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020085static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwai53d2f742005-11-17 13:56:05 +010087static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/*
90 * create a timer instance with the given owner string.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
Takashi Iwai6a343672019-11-07 20:20:08 +010092struct snd_timer_instance *snd_timer_instance_new(const char *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwai53d2f742005-11-17 13:56:05 +010094 struct snd_timer_instance *timeri;
Takashi Iwai6a343672019-11-07 20:20:08 +010095
Takashi Iwaica2c0962005-09-09 14:20:23 +020096 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (timeri == NULL)
98 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -070099 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (! timeri->owner) {
101 kfree(timeri);
102 return NULL;
103 }
104 INIT_LIST_HEAD(&timeri->open_list);
105 INIT_LIST_HEAD(&timeri->active_list);
106 INIT_LIST_HEAD(&timeri->ack_list);
107 INIT_LIST_HEAD(&timeri->slave_list_head);
108 INIT_LIST_HEAD(&timeri->slave_active_head);
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return timeri;
111}
Takashi Iwai6a343672019-11-07 20:20:08 +0100112EXPORT_SYMBOL(snd_timer_instance_new);
113
114void snd_timer_instance_free(struct snd_timer_instance *timeri)
115{
116 if (timeri) {
117 if (timeri->private_free)
118 timeri->private_free(timeri);
119 kfree(timeri->owner);
120 kfree(timeri);
121 }
122}
123EXPORT_SYMBOL(snd_timer_instance_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
126 * find a timer instance from the given timer id
127 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100128static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100130 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Johannes Berg9244b2c2006-10-05 16:02:22 +0200132 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (timer->tmr_class != tid->dev_class)
134 continue;
135 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
136 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
137 (timer->card == NULL ||
138 timer->card->number != tid->card))
139 continue;
140 if (timer->tmr_device != tid->device)
141 continue;
142 if (timer->tmr_subdevice != tid->subdevice)
143 continue;
144 return timer;
145 }
146 return NULL;
147}
148
Johannes Bergee2da992008-07-09 10:28:41 +0200149#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Takashi Iwai53d2f742005-11-17 13:56:05 +0100151static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 switch (tid->dev_class) {
154 case SNDRV_TIMER_CLASS_GLOBAL:
155 if (tid->device < timer_limit)
156 request_module("snd-timer-%i", tid->device);
157 break;
158 case SNDRV_TIMER_CLASS_CARD:
159 case SNDRV_TIMER_CLASS_PCM:
160 if (tid->card < snd_ecards_limit)
161 request_module("snd-card-%i", tid->card);
162 break;
163 default:
164 break;
165 }
166}
167
168#endif
169
Takashi Iwaiebfc6de2019-11-07 20:20:06 +0100170/* move the slave if it belongs to the master; return 1 if match */
171static int check_matching_master_slave(struct snd_timer_instance *master,
172 struct snd_timer_instance *slave)
173{
174 if (slave->slave_class != master->slave_class ||
175 slave->slave_id != master->slave_id)
176 return 0;
177 if (master->timer->num_instances >= master->timer->max_instances)
178 return -EBUSY;
179 list_move_tail(&slave->open_list, &master->slave_list_head);
180 master->timer->num_instances++;
181 spin_lock_irq(&slave_active_lock);
182 spin_lock(&master->timer->lock);
183 slave->master = master;
184 slave->timer = master->timer;
185 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
186 list_add_tail(&slave->active_list, &master->slave_active_head);
187 spin_unlock(&master->timer->lock);
188 spin_unlock_irq(&slave_active_lock);
189 return 1;
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * look for a master instance matching with the slave id of the given slave.
194 * when found, relink the open_link of the slave.
195 *
196 * call this with register_mutex down.
197 */
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100198static int snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100200 struct snd_timer *timer;
201 struct snd_timer_instance *master;
Takashi Iwaiebfc6de2019-11-07 20:20:06 +0100202 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200205 list_for_each_entry(timer, &snd_timer_list, device_list) {
206 list_for_each_entry(master, &timer->open_list_head, open_list) {
Takashi Iwaiebfc6de2019-11-07 20:20:06 +0100207 err = check_matching_master_slave(master, slave);
208 if (err != 0) /* match found or error */
209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 }
Takashi Iwaiebfc6de2019-11-07 20:20:06 +0100212 out:
213 return err < 0 ? err : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*
217 * look for slave instances matching with the slave id of the given master.
218 * when found, relink the open_link of slaves.
219 *
220 * call this with register_mutex down.
221 */
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100222static int snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200224 struct snd_timer_instance *slave, *tmp;
Takashi Iwaiebfc6de2019-11-07 20:20:06 +0100225 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200228 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Takashi Iwaiebfc6de2019-11-07 20:20:06 +0100229 err = check_matching_master_slave(master, slave);
230 if (err < 0)
231 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Takashi Iwaiebfc6de2019-11-07 20:20:06 +0100233 return err < 0 ? err : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Takashi Iwai33bbb8a2019-11-07 20:20:07 +0100236static void snd_timer_close_locked(struct snd_timer_instance *timeri,
237 struct device **card_devp_to_put);
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * open a timer instance
241 * when opening a master, the slave id must be here given.
242 */
Takashi Iwai6a343672019-11-07 20:20:08 +0100243int snd_timer_open(struct snd_timer_instance *timeri,
244 struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned int slave_id)
246{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100247 struct snd_timer *timer;
Takashi Iwaia3933182019-10-30 22:42:57 +0100248 struct device *card_dev_to_put = NULL;
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100249 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200250
Takashi Iwai41672c02019-03-28 17:11:10 +0100251 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
253 /* open a slave instance */
254 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
255 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100256 pr_debug("ALSA: timer: invalid slave class %i\n",
257 tid->dev_sclass);
Takashi Iwai41672c02019-03-28 17:11:10 +0100258 err = -EINVAL;
259 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Takashi Iwaifdea53f2019-11-06 16:42:57 +0100261 if (num_slaves >= MAX_SLAVE_INSTANCES) {
262 err = -EBUSY;
263 goto unlock;
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 timeri->slave_class = tid->dev_sclass;
266 timeri->slave_id = tid->device;
267 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
268 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
Takashi Iwaifdea53f2019-11-06 16:42:57 +0100269 num_slaves++;
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100270 err = snd_timer_check_slave(timeri);
Takashi Iwai0c4f09c2019-11-11 18:36:42 +0100271 goto list_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 /* open a master instance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 timer = snd_timer_find(tid);
Johannes Bergee2da992008-07-09 10:28:41 +0200276#ifdef CONFIG_MODULES
277 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100278 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100280 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 timer = snd_timer_find(tid);
282 }
283#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200284 if (!timer) {
Takashi Iwai41672c02019-03-28 17:11:10 +0100285 err = -ENODEV;
286 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200288 if (!list_empty(&timer->open_list_head)) {
Takashi Iwaie7af6302019-11-06 17:55:47 +0100289 struct snd_timer_instance *t =
290 list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100291 struct snd_timer_instance, open_list);
Takashi Iwaie7af6302019-11-06 17:55:47 +0100292 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Takashi Iwai41672c02019-03-28 17:11:10 +0100293 err = -EBUSY;
Takashi Iwai41672c02019-03-28 17:11:10 +0100294 goto unlock;
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200295 }
296 }
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100297 if (timer->num_instances >= timer->max_instances) {
Takashi Iwai41672c02019-03-28 17:11:10 +0100298 err = -EBUSY;
299 goto unlock;
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100300 }
Takashi Iwai6a343672019-11-07 20:20:08 +0100301 if (!try_module_get(timer->module)) {
302 err = -EBUSY;
Takashi Iwai41672c02019-03-28 17:11:10 +0100303 goto unlock;
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200304 }
Takashi Iwai230323d2016-01-21 17:19:31 +0100305 /* take a card refcount for safe disconnection */
Takashi Iwai6a343672019-11-07 20:20:08 +0100306 if (timer->card) {
Takashi Iwai230323d2016-01-21 17:19:31 +0100307 get_device(&timer->card->card_dev);
Takashi Iwai6a343672019-11-07 20:20:08 +0100308 card_dev_to_put = &timer->card->card_dev;
309 }
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200310
311 if (list_empty(&timer->open_list_head) && timer->hw.open) {
Takashi Iwai41672c02019-03-28 17:11:10 +0100312 err = timer->hw.open(timer);
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200313 if (err) {
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200314 module_put(timer->module);
Takashi Iwai41672c02019-03-28 17:11:10 +0100315 goto unlock;
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200316 }
317 }
318
Takashi Iwai6a343672019-11-07 20:20:08 +0100319 timeri->timer = timer;
320 timeri->slave_class = tid->dev_sclass;
321 timeri->slave_id = slave_id;
322
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200323 list_add_tail(&timeri->open_list, &timer->open_list_head);
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100324 timer->num_instances++;
325 err = snd_timer_check_master(timeri);
Takashi Iwai0c4f09c2019-11-11 18:36:42 +0100326list_added:
Takashi Iwai6a343672019-11-07 20:20:08 +0100327 if (err < 0)
Takashi Iwaia3933182019-10-30 22:42:57 +0100328 snd_timer_close_locked(timeri, &card_dev_to_put);
Takashi Iwai41672c02019-03-28 17:11:10 +0100329
330 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100331 mutex_unlock(&register_mutex);
Takashi Iwaia3933182019-10-30 22:42:57 +0100332 /* put_device() is called after unlock for avoiding deadlock */
Takashi Iwai6a343672019-11-07 20:20:08 +0100333 if (err < 0 && card_dev_to_put)
Takashi Iwaia3933182019-10-30 22:42:57 +0100334 put_device(card_dev_to_put);
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
Takashi Iwai98856392017-06-16 16:16:05 +0200337EXPORT_SYMBOL(snd_timer_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/*
340 * close a timer instance
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100341 * call this with register_mutex down.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
Takashi Iwai33bbb8a2019-11-07 20:20:07 +0100343static void snd_timer_close_locked(struct snd_timer_instance *timeri,
344 struct device **card_devp_to_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100346 struct snd_timer *timer = timeri->timer;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200347 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100349 if (timer) {
350 spin_lock_irq(&timer->lock);
351 timeri->flags |= SNDRV_TIMER_IFLG_DEAD;
352 spin_unlock_irq(&timer->lock);
353 }
354
Takashi Iwai6a343672019-11-07 20:20:08 +0100355 if (!list_empty(&timeri->open_list)) {
356 list_del_init(&timeri->open_list);
357 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
358 num_slaves--;
359 }
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* force to stop the timer */
362 snd_timer_stop(timeri);
363
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100364 if (timer) {
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100365 timer->num_instances--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* wait, until the active callback is finished */
367 spin_lock_irq(&timer->lock);
368 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
369 spin_unlock_irq(&timer->lock);
370 udelay(10);
371 spin_lock_irq(&timer->lock);
372 }
373 spin_unlock_irq(&timer->lock);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* remove slave links */
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100376 spin_lock_irq(&slave_active_lock);
377 spin_lock(&timer->lock);
Takashi Iwai6a343672019-11-07 20:20:08 +0100378 timeri->timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200379 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
380 open_list) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200381 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100382 timer->num_instances--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 slave->master = NULL;
384 slave->timer = NULL;
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100385 list_del_init(&slave->ack_list);
386 list_del_init(&slave->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100388 spin_unlock(&timer->lock);
389 spin_unlock_irq(&slave_active_lock);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100390
391 /* slave doesn't need to release timer resources below */
392 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
393 timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100395
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100396 if (timer) {
397 if (list_empty(&timer->open_list_head) && timer->hw.close)
398 timer->hw.close(timer);
399 /* release a card refcount for safe disconnection */
400 if (timer->card)
Takashi Iwaia3933182019-10-30 22:42:57 +0100401 *card_devp_to_put = &timer->card->card_dev;
Clemens Ladischde242142005-10-12 17:12:31 +0200402 module_put(timer->module);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100405
406/*
407 * close a timer instance
408 */
Takashi Iwai33bbb8a2019-11-07 20:20:07 +0100409void snd_timer_close(struct snd_timer_instance *timeri)
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100410{
Takashi Iwaia3933182019-10-30 22:42:57 +0100411 struct device *card_dev_to_put = NULL;
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100412
413 if (snd_BUG_ON(!timeri))
Takashi Iwai33bbb8a2019-11-07 20:20:07 +0100414 return;
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100415
416 mutex_lock(&register_mutex);
Takashi Iwai33bbb8a2019-11-07 20:20:07 +0100417 snd_timer_close_locked(timeri, &card_dev_to_put);
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100418 mutex_unlock(&register_mutex);
Takashi Iwaia3933182019-10-30 22:42:57 +0100419 /* put_device() is called after unlock for avoiding deadlock */
420 if (card_dev_to_put)
421 put_device(card_dev_to_put);
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100422}
Takashi Iwai98856392017-06-16 16:16:05 +0200423EXPORT_SYMBOL(snd_timer_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Takashi Iwaifdcb5762018-05-16 23:45:33 +0200425static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
426{
427 if (timer->hw.c_resolution)
428 return timer->hw.c_resolution(timer);
429 else
430 return timer->hw.resolution;
431}
432
Takashi Iwai53d2f742005-11-17 13:56:05 +0100433unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100435 struct snd_timer * timer;
Takashi Iwai9d4d2072018-05-16 23:52:42 +0200436 unsigned long ret = 0;
437 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (timeri == NULL)
440 return 0;
Markus Elfringdd1f7ab2017-08-23 09:45:06 +0200441 timer = timeri->timer;
Takashi Iwai9d4d2072018-05-16 23:52:42 +0200442 if (timer) {
443 spin_lock_irqsave(&timer->lock, flags);
444 ret = snd_timer_hw_resolution(timer);
445 spin_unlock_irqrestore(&timer->lock, flags);
446 }
447 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
Takashi Iwai98856392017-06-16 16:16:05 +0200449EXPORT_SYMBOL(snd_timer_resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Takashi Iwai53d2f742005-11-17 13:56:05 +0100451static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Takashi Iwai9d4d2072018-05-16 23:52:42 +0200453 struct snd_timer *timer = ti->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100455 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct timespec tstamp;
457
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100458 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +0000459 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100460 else
461 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200462 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
463 event > SNDRV_TIMER_EVENT_PAUSE))
464 return;
Takashi Iwai9d4d2072018-05-16 23:52:42 +0200465 if (timer &&
466 (event == SNDRV_TIMER_EVENT_START ||
467 event == SNDRV_TIMER_EVENT_CONTINUE))
468 resolution = snd_timer_hw_resolution(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (ti->ccallback)
Jaroslav Kyselab30477d52010-03-03 11:05:55 +0100470 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
472 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (timer == NULL)
474 return;
475 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
476 return;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200477 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (ts->ccallback)
Takashi Iwai117159f2016-02-08 17:36:25 +0100479 ts->ccallback(ts, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100482/* start/continue a master timer */
483static int snd_timer_start1(struct snd_timer_instance *timeri,
484 bool start, unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100486 struct snd_timer *timer;
487 int result;
488 unsigned long flags;
489
490 timer = timeri->timer;
491 if (!timer)
492 return -EINVAL;
493
494 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100495 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
496 result = -EINVAL;
497 goto unlock;
498 }
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100499 if (timer->card && timer->card->shutdown) {
500 result = -ENODEV;
501 goto unlock;
502 }
503 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
504 SNDRV_TIMER_IFLG_START)) {
505 result = -EBUSY;
506 goto unlock;
507 }
508
509 if (start)
510 timeri->ticks = timeri->cticks = ticks;
511 else if (!timeri->cticks)
512 timeri->cticks = 1;
513 timeri->pticks = 0;
514
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100515 list_move_tail(&timeri->active_list, &timer->active_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (timer->running) {
517 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
518 goto __start_now;
519 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
520 timeri->flags |= SNDRV_TIMER_IFLG_START;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100521 result = 1; /* delayed start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 } else {
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100523 if (start)
524 timer->sticks = ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 timer->hw.start(timer);
526 __start_now:
527 timer->running++;
528 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100529 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100531 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
532 SNDRV_TIMER_EVENT_CONTINUE);
533 unlock:
534 spin_unlock_irqrestore(&timer->lock, flags);
535 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100538/* start/continue a slave timer */
539static int snd_timer_start_slave(struct snd_timer_instance *timeri,
540 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 unsigned long flags;
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100543 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100546 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
547 err = -EINVAL;
548 goto unlock;
549 }
Takashi Iwaif784beb2016-01-30 23:09:08 +0100550 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100551 err = -EBUSY;
552 goto unlock;
Takashi Iwaif784beb2016-01-30 23:09:08 +0100553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100555 if (timeri->master && timeri->timer) {
556 spin_lock(&timeri->timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200557 list_add_tail(&timeri->active_list,
558 &timeri->master->slave_active_head);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100559 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
560 SNDRV_TIMER_EVENT_CONTINUE);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100561 spin_unlock(&timeri->timer->lock);
562 }
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100563 err = 1; /* delayed start */
564 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_unlock_irqrestore(&slave_active_lock, flags);
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100569/* stop/pause a master timer */
570static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100572 struct snd_timer *timer;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100573 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 unsigned long flags;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 timer = timeri->timer;
577 if (!timer)
578 return -EINVAL;
579 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwaif784beb2016-01-30 23:09:08 +0100580 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
581 SNDRV_TIMER_IFLG_START))) {
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100582 result = -EBUSY;
583 goto unlock;
Takashi Iwaif784beb2016-01-30 23:09:08 +0100584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 list_del_init(&timeri->ack_list);
586 list_del_init(&timeri->active_list);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100587 if (timer->card && timer->card->shutdown)
588 goto unlock;
589 if (stop) {
590 timeri->cticks = timeri->ticks;
591 timeri->pticks = 0;
Takashi Iwai230323d2016-01-21 17:19:31 +0100592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
594 !(--timer->running)) {
595 timer->hw.stop(timer);
596 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
597 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
598 snd_timer_reschedule(timer, 0);
599 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
600 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
601 timer->hw.start(timer);
602 }
603 }
604 }
Takashi Iwaic3b16812016-01-14 17:01:46 +0100605 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Takashi Iwai9f8a7652016-09-07 15:45:31 +0200606 if (stop)
607 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
608 else
609 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100610 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
Ben Hutchings3ae18092018-05-17 22:34:39 +0100611 SNDRV_TIMER_EVENT_PAUSE);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100612 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 spin_unlock_irqrestore(&timer->lock, flags);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100614 return result;
615}
616
617/* stop/pause a slave timer */
618static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
619{
620 unsigned long flags;
621
622 spin_lock_irqsave(&slave_active_lock, flags);
623 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
624 spin_unlock_irqrestore(&slave_active_lock, flags);
625 return -EBUSY;
626 }
627 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
628 if (timeri->timer) {
629 spin_lock(&timeri->timer->lock);
630 list_del_init(&timeri->ack_list);
631 list_del_init(&timeri->active_list);
632 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
Ben Hutchings3ae18092018-05-17 22:34:39 +0100633 SNDRV_TIMER_EVENT_PAUSE);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100634 spin_unlock(&timeri->timer->lock);
635 }
636 spin_unlock_irqrestore(&slave_active_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638}
639
640/*
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100641 * start the timer instance
642 */
643int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
644{
645 if (timeri == NULL || ticks < 1)
646 return -EINVAL;
647 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
648 return snd_timer_start_slave(timeri, true);
649 else
650 return snd_timer_start1(timeri, true, ticks);
651}
Takashi Iwai98856392017-06-16 16:16:05 +0200652EXPORT_SYMBOL(snd_timer_start);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100653
654/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * stop the timer instance.
656 *
657 * do not call this from the timer callback!
658 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100659int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100661 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
662 return snd_timer_stop_slave(timeri, true);
663 else
664 return snd_timer_stop1(timeri, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
Takashi Iwai98856392017-06-16 16:16:05 +0200666EXPORT_SYMBOL(snd_timer_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668/*
669 * start again.. the tick is kept.
670 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100671int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Takashi Iwai9f8a7652016-09-07 15:45:31 +0200673 /* timer can continue only after pause */
674 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
675 return -EINVAL;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100678 return snd_timer_start_slave(timeri, false);
679 else
680 return snd_timer_start1(timeri, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
Takashi Iwai98856392017-06-16 16:16:05 +0200682EXPORT_SYMBOL(snd_timer_continue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684/*
685 * pause.. remember the ticks left
686 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100687int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100689 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
690 return snd_timer_stop_slave(timeri, false);
691 else
692 return snd_timer_stop1(timeri, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
Takashi Iwai98856392017-06-16 16:16:05 +0200694EXPORT_SYMBOL(snd_timer_pause);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696/*
697 * reschedule the timer
698 *
699 * start pending instances and check the scheduling ticks.
700 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
701 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100702static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100704 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Johannes Berg9244b2c2006-10-05 16:02:22 +0200707 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (ti->flags & SNDRV_TIMER_IFLG_START) {
709 ti->flags &= ~SNDRV_TIMER_IFLG_START;
710 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
711 timer->running++;
712 }
713 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
714 if (ticks > ti->cticks)
715 ticks = ti->cticks;
716 }
717 }
718 if (ticks == ~0UL) {
719 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
720 return;
721 }
722 if (ticks > timer->hw.ticks)
723 ticks = timer->hw.ticks;
724 if (ticks_left != ticks)
725 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
726 timer->sticks = ticks;
727}
728
Takashi Iwai8748b852019-03-27 16:42:51 +0100729/* call callbacks in timer ack list */
730static void snd_timer_process_callbacks(struct snd_timer *timer,
731 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100733 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 unsigned long resolution, ticks;
735
Takashi Iwai8748b852019-03-27 16:42:51 +0100736 while (!list_empty(head)) {
737 ti = list_first_entry(head, struct snd_timer_instance,
738 ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Takashi Iwaidf555312019-04-09 12:25:32 +0200740 /* remove from ack_list and make empty */
741 list_del_init(&ti->ack_list);
742
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100743 if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) {
744 ticks = ti->pticks;
745 ti->pticks = 0;
746 resolution = ti->resolution;
Takashi Iwaidf555312019-04-09 12:25:32 +0200747 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100748 spin_unlock(&timer->lock);
749 if (ti->callback)
750 ti->callback(ti, resolution, ticks);
751 spin_lock(&timer->lock);
Takashi Iwaidf555312019-04-09 12:25:32 +0200752 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Takashi Iwai8748b852019-03-27 16:42:51 +0100755}
756
Takashi Iwai7bb4a8a2019-03-27 16:51:58 +0100757/* clear pending instances from ack list */
758static void snd_timer_clear_callbacks(struct snd_timer *timer,
759 struct list_head *head)
760{
761 unsigned long flags;
762
763 spin_lock_irqsave(&timer->lock, flags);
764 while (!list_empty(head))
765 list_del_init(head->next);
766 spin_unlock_irqrestore(&timer->lock, flags);
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * timer tasklet
771 *
772 */
773static void snd_timer_tasklet(unsigned long arg)
774{
775 struct snd_timer *timer = (struct snd_timer *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 unsigned long flags;
777
Takashi Iwai7bb4a8a2019-03-27 16:51:58 +0100778 if (timer->card && timer->card->shutdown) {
779 snd_timer_clear_callbacks(timer, &timer->sack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return;
Takashi Iwai7bb4a8a2019-03-27 16:51:58 +0100781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai8748b852019-03-27 16:42:51 +0100784 snd_timer_process_callbacks(timer, &timer->sack_list_head);
Takashi Iwai2999ff52006-07-05 17:16:58 +0200785 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788/*
789 * timer interrupt
790 *
791 * ticks_left is usually equal to timer->sticks.
792 *
793 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100794void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200796 struct snd_timer_instance *ti, *ts, *tmp;
Takashi Iwai8748b852019-03-27 16:42:51 +0100797 unsigned long resolution;
798 struct list_head *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100799 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int use_tasklet = 0;
801
802 if (timer == NULL)
803 return;
804
Takashi Iwai7bb4a8a2019-03-27 16:51:58 +0100805 if (timer->card && timer->card->shutdown) {
806 snd_timer_clear_callbacks(timer, &timer->ack_list_head);
Takashi Iwai230323d2016-01-21 17:19:31 +0100807 return;
Takashi Iwai7bb4a8a2019-03-27 16:51:58 +0100808 }
Takashi Iwai230323d2016-01-21 17:19:31 +0100809
Takashi Iwaib32425a2005-11-18 18:52:14 +0100810 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* remember the current resolution */
Takashi Iwaifdcb5762018-05-16 23:45:33 +0200813 resolution = snd_timer_hw_resolution(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200816 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200817 * processed instance is relinked to done_list_head before the callback
818 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200820 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
821 active_list) {
Takashi Iwaife1b26c2019-03-27 17:02:40 +0100822 if (ti->flags & SNDRV_TIMER_IFLG_DEAD)
823 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
825 continue;
826 ti->pticks += ticks_left;
827 ti->resolution = resolution;
828 if (ti->cticks < ticks_left)
829 ti->cticks = 0;
830 else
831 ti->cticks -= ticks_left;
832 if (ti->cticks) /* not expired */
833 continue;
834 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
835 ti->cticks = ti->ticks;
836 } else {
837 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai094fd3b2016-02-04 17:06:13 +0100838 --timer->running;
839 list_del_init(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200841 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
842 (ti->flags & SNDRV_TIMER_IFLG_FAST))
843 ack_list_head = &timer->ack_list_head;
844 else
845 ack_list_head = &timer->sack_list_head;
846 if (list_empty(&ti->ack_list))
847 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200848 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 ts->pticks = ti->pticks;
850 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200851 if (list_empty(&ts->ack_list))
852 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854 }
855 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200856 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (timer->running) {
858 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
859 timer->hw.stop(timer);
860 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
861 }
862 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
863 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
864 /* restart timer */
865 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
866 timer->hw.start(timer);
867 }
868 } else {
869 timer->hw.stop(timer);
870 }
871
872 /* now process all fast callbacks */
Takashi Iwai8748b852019-03-27 16:42:51 +0100873 snd_timer_process_callbacks(timer, &timer->ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /* do we have any slow callbacks? */
876 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100877 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100880 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Takashi Iwai98856392017-06-16 16:16:05 +0200882EXPORT_SYMBOL(snd_timer_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884/*
885
886 */
887
Takashi Iwai53d2f742005-11-17 13:56:05 +0100888int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
889 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100891 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100893 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .dev_free = snd_timer_dev_free,
895 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200896 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 };
898
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200899 if (snd_BUG_ON(!tid))
900 return -EINVAL;
Srikanth K Hd10ee9c2018-07-20 11:13:51 +0530901 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
902 tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
903 if (WARN_ON(!card))
904 return -EINVAL;
905 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200906 if (rtimer)
907 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200908 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100909 if (!timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -ENOMEM;
911 timer->tmr_class = tid->dev_class;
912 timer->card = card;
913 timer->tmr_device = tid->device;
914 timer->tmr_subdevice = tid->subdevice;
915 if (id)
916 strlcpy(timer->id, id, sizeof(timer->id));
Vegard Nossum6b760bb2016-08-29 00:33:50 +0200917 timer->sticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 INIT_LIST_HEAD(&timer->device_list);
919 INIT_LIST_HEAD(&timer->open_list_head);
920 INIT_LIST_HEAD(&timer->active_list_head);
921 INIT_LIST_HEAD(&timer->ack_list_head);
922 INIT_LIST_HEAD(&timer->sack_list_head);
923 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200924 tasklet_init(&timer->task_queue, snd_timer_tasklet,
925 (unsigned long)timer);
Takashi Iwai9b7d8692017-11-05 10:07:43 +0100926 timer->max_instances = 1000; /* default limit per timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200928 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200929 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
930 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 snd_timer_free(timer);
932 return err;
933 }
934 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200935 if (rtimer)
936 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938}
Takashi Iwai98856392017-06-16 16:16:05 +0200939EXPORT_SYMBOL(snd_timer_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Takashi Iwai53d2f742005-11-17 13:56:05 +0100941static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200943 if (!timer)
944 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200945
946 mutex_lock(&register_mutex);
947 if (! list_empty(&timer->open_list_head)) {
948 struct list_head *p, *n;
949 struct snd_timer_instance *ti;
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100950 pr_warn("ALSA: timer %p is busy?\n", timer);
Takashi Iwaic4614822006-06-23 14:38:23 +0200951 list_for_each_safe(p, n, &timer->open_list_head) {
952 list_del_init(p);
953 ti = list_entry(p, struct snd_timer_instance, open_list);
954 ti->timer = NULL;
955 }
956 }
957 list_del(&timer->device_list);
958 mutex_unlock(&register_mutex);
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (timer->private_free)
961 timer->private_free(timer);
962 kfree(timer);
963 return 0;
964}
965
Takashi Iwai53d2f742005-11-17 13:56:05 +0100966static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100968 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return snd_timer_free(timer);
970}
971
Takashi Iwai53d2f742005-11-17 13:56:05 +0100972static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100974 struct snd_timer *timer = dev->device_data;
975 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200977 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
978 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
980 !timer->hw.resolution && timer->hw.c_resolution == NULL)
981 return -EINVAL;
982
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100983 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200984 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (timer1->tmr_class > timer->tmr_class)
986 break;
987 if (timer1->tmr_class < timer->tmr_class)
988 continue;
989 if (timer1->card && timer->card) {
990 if (timer1->card->number > timer->card->number)
991 break;
992 if (timer1->card->number < timer->card->number)
993 continue;
994 }
995 if (timer1->tmr_device > timer->tmr_device)
996 break;
997 if (timer1->tmr_device < timer->tmr_device)
998 continue;
999 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1000 break;
1001 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1002 continue;
1003 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001004 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EBUSY;
1006 }
Johannes Berg9244b2c2006-10-05 16:02:22 +02001007 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001008 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010}
1011
Takashi Iwaic4614822006-06-23 14:38:23 +02001012static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001014 struct snd_timer *timer = device->device_data;
Takashi Iwai230323d2016-01-21 17:19:31 +01001015 struct snd_timer_instance *ti;
1016
Takashi Iwaic4614822006-06-23 14:38:23 +02001017 mutex_lock(&register_mutex);
1018 list_del_init(&timer->device_list);
Takashi Iwai230323d2016-01-21 17:19:31 +01001019 /* wake up pending sleepers */
1020 list_for_each_entry(ti, &timer->open_list_head, open_list) {
Takashi Iwai40ed9442016-01-21 17:43:08 +01001021 if (ti->disconnect)
1022 ti->disconnect(ti);
Takashi Iwai230323d2016-01-21 17:19:31 +01001023 }
Takashi Iwaic4614822006-06-23 14:38:23 +02001024 mutex_unlock(&register_mutex);
1025 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Takashi Iwai53d2f742005-11-17 13:56:05 +01001028void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 unsigned long flags;
1031 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001032 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Takashi Iwai230323d2016-01-21 17:19:31 +01001034 if (timer->card && timer->card->shutdown)
1035 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001036 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1037 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001038 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1039 event > SNDRV_TIMER_EVENT_MRESUME))
1040 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001042 if (event == SNDRV_TIMER_EVENT_MSTART ||
1043 event == SNDRV_TIMER_EVENT_MCONTINUE ||
Takashi Iwaifdcb5762018-05-16 23:45:33 +02001044 event == SNDRV_TIMER_EVENT_MRESUME)
1045 resolution = snd_timer_hw_resolution(timer);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001046 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (ti->ccallback)
1048 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001049 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (ts->ccallback)
1051 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 spin_unlock_irqrestore(&timer->lock, flags);
1054}
Takashi Iwai98856392017-06-16 16:16:05 +02001055EXPORT_SYMBOL(snd_timer_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057/*
1058 * exported functions for global timers
1059 */
Takashi Iwai53d2f742005-11-17 13:56:05 +01001060int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001062 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1065 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1066 tid.card = -1;
1067 tid.device = device;
1068 tid.subdevice = 0;
1069 return snd_timer_new(NULL, id, &tid, rtimer);
1070}
Takashi Iwai98856392017-06-16 16:16:05 +02001071EXPORT_SYMBOL(snd_timer_global_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Takashi Iwai53d2f742005-11-17 13:56:05 +01001073int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 return snd_timer_free(timer);
1076}
Takashi Iwai98856392017-06-16 16:16:05 +02001077EXPORT_SYMBOL(snd_timer_global_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Takashi Iwai53d2f742005-11-17 13:56:05 +01001079int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001081 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 memset(&dev, 0, sizeof(dev));
1084 dev.device_data = timer;
1085 return snd_timer_dev_register(&dev);
1086}
Takashi Iwai98856392017-06-16 16:16:05 +02001087EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001089/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * System timer
1091 */
1092
1093struct snd_timer_system_private {
1094 struct timer_list tlist;
Kees Cook38e9a802017-10-04 17:53:33 -07001095 struct snd_timer *snd_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 unsigned long last_expires;
1097 unsigned long last_jiffies;
1098 unsigned long correction;
1099};
1100
Kees Cook38e9a802017-10-04 17:53:33 -07001101static void snd_timer_s_function(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Kees Cook38e9a802017-10-04 17:53:33 -07001103 struct snd_timer_system_private *priv = from_timer(priv, t,
1104 tlist);
1105 struct snd_timer *timer = priv->snd_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 unsigned long jiff = jiffies;
1107 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +02001108 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1110}
1111
Takashi Iwai53d2f742005-11-17 13:56:05 +01001112static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct snd_timer_system_private *priv;
1115 unsigned long njiff;
1116
1117 priv = (struct snd_timer_system_private *) timer->private_data;
1118 njiff = (priv->last_jiffies = jiffies);
1119 if (priv->correction > timer->sticks - 1) {
1120 priv->correction -= timer->sticks - 1;
1121 njiff++;
1122 } else {
1123 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +02001124 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Takashi Iwai4a070832016-04-01 12:28:16 +02001126 priv->last_expires = njiff;
1127 mod_timer(&priv->tlist, njiff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return 0;
1129}
1130
Takashi Iwai53d2f742005-11-17 13:56:05 +01001131static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct snd_timer_system_private *priv;
1134 unsigned long jiff;
1135
1136 priv = (struct snd_timer_system_private *) timer->private_data;
1137 del_timer(&priv->tlist);
1138 jiff = jiffies;
1139 if (time_before(jiff, priv->last_expires))
1140 timer->sticks = priv->last_expires - jiff;
1141 else
1142 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001143 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return 0;
1145}
1146
Takashi Iwaif1463572016-02-02 14:14:10 +01001147static int snd_timer_s_close(struct snd_timer *timer)
1148{
1149 struct snd_timer_system_private *priv;
1150
1151 priv = (struct snd_timer_system_private *)timer->private_data;
1152 del_timer_sync(&priv->tlist);
1153 return 0;
1154}
1155
Takashi Iwai53d2f742005-11-17 13:56:05 +01001156static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1159 .resolution = 1000000000L / HZ,
1160 .ticks = 10000000L,
Takashi Iwaif1463572016-02-02 14:14:10 +01001161 .close = snd_timer_s_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 .start = snd_timer_s_start,
1163 .stop = snd_timer_s_stop
1164};
1165
Takashi Iwai53d2f742005-11-17 13:56:05 +01001166static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 kfree(timer->private_data);
1169}
1170
1171static int snd_timer_register_system(void)
1172{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001173 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct snd_timer_system_private *priv;
1175 int err;
1176
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001177 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1178 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return err;
1180 strcpy(timer->name, "system timer");
1181 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001182 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (priv == NULL) {
1184 snd_timer_free(timer);
1185 return -ENOMEM;
1186 }
Kees Cook38e9a802017-10-04 17:53:33 -07001187 priv->snd_timer = timer;
1188 timer_setup(&priv->tlist, snd_timer_s_function, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 timer->private_data = priv;
1190 timer->private_free = snd_timer_free_system;
1191 return snd_timer_global_register(timer);
1192}
1193
Jie Yangcd6a6502015-05-27 19:45:45 +08001194#ifdef CONFIG_SND_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/*
1196 * Info interface
1197 */
1198
Takashi Iwai53d2f742005-11-17 13:56:05 +01001199static void snd_timer_proc_read(struct snd_info_entry *entry,
1200 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001202 struct snd_timer *timer;
1203 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001205 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001206 list_for_each_entry(timer, &snd_timer_list, device_list) {
Takashi Iwai230323d2016-01-21 17:19:31 +01001207 if (timer->card && timer->card->shutdown)
1208 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 switch (timer->tmr_class) {
1210 case SNDRV_TIMER_CLASS_GLOBAL:
1211 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1212 break;
1213 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001214 snd_iprintf(buffer, "C%i-%i: ",
1215 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 break;
1217 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001218 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1219 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 break;
1221 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001222 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1223 timer->card ? timer->card->number : -1,
1224 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 snd_iprintf(buffer, "%s :", timer->name);
1227 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001228 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1229 timer->hw.resolution / 1000,
1230 timer->hw.resolution % 1000,
1231 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1233 snd_iprintf(buffer, " SLAVE");
1234 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001235 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001236 snd_iprintf(buffer, " Client %s : %s\n",
1237 ti->owner ? ti->owner : "unknown",
1238 ti->flags & (SNDRV_TIMER_IFLG_START |
1239 SNDRV_TIMER_IFLG_RUNNING)
1240 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001242 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001245static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001246
1247static void __init snd_timer_proc_init(void)
1248{
1249 struct snd_info_entry *entry;
1250
1251 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1252 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001253 entry->c.text.read = snd_timer_proc_read;
1254 if (snd_info_register(entry) < 0) {
1255 snd_info_free_entry(entry);
1256 entry = NULL;
1257 }
1258 }
1259 snd_timer_proc_entry = entry;
1260}
1261
1262static void __exit snd_timer_proc_done(void)
1263{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001264 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001265}
Jie Yangcd6a6502015-05-27 19:45:45 +08001266#else /* !CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +01001267#define snd_timer_proc_init()
1268#define snd_timer_proc_done()
1269#endif
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/*
1272 * USER SPACE interface
1273 */
1274
Takashi Iwai53d2f742005-11-17 13:56:05 +01001275static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 unsigned long resolution,
1277 unsigned long ticks)
1278{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001279 struct snd_timer_user *tu = timeri->callback_data;
1280 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 spin_lock(&tu->qlock);
1284 if (tu->qused > 0) {
1285 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1286 r = &tu->queue[prev];
1287 if (r->resolution == resolution) {
1288 r->ticks += ticks;
1289 goto __wake;
1290 }
1291 }
1292 if (tu->qused >= tu->queue_size) {
1293 tu->overrun++;
1294 } else {
1295 r = &tu->queue[tu->qtail++];
1296 tu->qtail %= tu->queue_size;
1297 r->resolution = resolution;
1298 r->ticks = ticks;
1299 tu->qused++;
1300 }
1301 __wake:
1302 spin_unlock(&tu->qlock);
1303 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1304 wake_up(&tu->qchange_sleep);
1305}
1306
Takashi Iwai53d2f742005-11-17 13:56:05 +01001307static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1308 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 if (tu->qused >= tu->queue_size) {
1311 tu->overrun++;
1312 } else {
1313 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1314 tu->qtail %= tu->queue_size;
1315 tu->qused++;
1316 }
1317}
1318
Takashi Iwai53d2f742005-11-17 13:56:05 +01001319static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1320 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct timespec *tstamp,
1322 unsigned long resolution)
1323{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001324 struct snd_timer_user *tu = timeri->callback_data;
1325 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001326 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001328 if (event >= SNDRV_TIMER_EVENT_START &&
1329 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 tu->tstamp = *tstamp;
1331 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1332 return;
Kangjie Lu9a47e9c2016-05-03 16:44:20 -04001333 memset(&r1, 0, sizeof(r1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 r1.event = event;
1335 r1.tstamp = *tstamp;
1336 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001337 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001339 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1341 wake_up(&tu->qchange_sleep);
1342}
1343
Takashi Iwai40ed9442016-01-21 17:43:08 +01001344static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1345{
1346 struct snd_timer_user *tu = timeri->callback_data;
1347
1348 tu->disconnected = true;
1349 wake_up(&tu->qchange_sleep);
1350}
1351
Takashi Iwai53d2f742005-11-17 13:56:05 +01001352static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 unsigned long resolution,
1354 unsigned long ticks)
1355{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001356 struct snd_timer_user *tu = timeri->callback_data;
1357 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 struct timespec tstamp;
1359 int prev, append = 0;
1360
Dan Carpentera8c006a2017-03-31 18:22:23 +03001361 memset(&r1, 0, sizeof(r1));
Takashi Iwai07799e72005-10-10 11:49:49 +02001362 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001364 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1365 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 spin_unlock(&tu->qlock);
1367 return;
1368 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001369 if (tu->last_resolution != resolution || ticks > 0) {
1370 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +00001371 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001372 else
1373 getnstimeofday(&tstamp);
1374 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001375 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1376 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1378 r1.tstamp = tstamp;
1379 r1.val = resolution;
1380 snd_timer_user_append_to_tqueue(tu, &r1);
1381 tu->last_resolution = resolution;
1382 append++;
1383 }
1384 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1385 goto __wake;
1386 if (ticks == 0)
1387 goto __wake;
1388 if (tu->qused > 0) {
1389 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1390 r = &tu->tqueue[prev];
1391 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1392 r->tstamp = tstamp;
1393 r->val += ticks;
1394 append++;
1395 goto __wake;
1396 }
1397 }
1398 r1.event = SNDRV_TIMER_EVENT_TICK;
1399 r1.tstamp = tstamp;
1400 r1.val = ticks;
1401 snd_timer_user_append_to_tqueue(tu, &r1);
1402 append++;
1403 __wake:
1404 spin_unlock(&tu->qlock);
1405 if (append == 0)
1406 return;
1407 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1408 wake_up(&tu->qchange_sleep);
1409}
1410
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001411static int realloc_user_queue(struct snd_timer_user *tu, int size)
1412{
1413 struct snd_timer_read *queue = NULL;
1414 struct snd_timer_tread *tqueue = NULL;
1415
1416 if (tu->tread) {
1417 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1418 if (!tqueue)
1419 return -ENOMEM;
1420 } else {
1421 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1422 if (!queue)
1423 return -ENOMEM;
1424 }
1425
1426 spin_lock_irq(&tu->qlock);
1427 kfree(tu->queue);
1428 kfree(tu->tqueue);
1429 tu->queue_size = size;
1430 tu->queue = queue;
1431 tu->tqueue = tqueue;
1432 tu->qhead = tu->qtail = tu->qused = 0;
1433 spin_unlock_irq(&tu->qlock);
1434
1435 return 0;
1436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438static int snd_timer_user_open(struct inode *inode, struct file *file)
1439{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001440 struct snd_timer_user *tu;
Takashi Iwai02f48652010-04-13 11:49:04 +02001441 int err;
1442
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +03001443 err = stream_open(inode, file);
Takashi Iwai02f48652010-04-13 11:49:04 +02001444 if (err < 0)
1445 return err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001446
Takashi Iwaica2c0962005-09-09 14:20:23 +02001447 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (tu == NULL)
1449 return -ENOMEM;
1450 spin_lock_init(&tu->qlock);
1451 init_waitqueue_head(&tu->qchange_sleep);
Takashi Iwaiaf368022016-01-13 17:48:01 +01001452 mutex_init(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 tu->ticks = 1;
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001454 if (realloc_user_queue(tu, 128) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 kfree(tu);
1456 return -ENOMEM;
1457 }
1458 file->private_data = tu;
1459 return 0;
1460}
1461
1462static int snd_timer_user_release(struct inode *inode, struct file *file)
1463{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001464 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 if (file->private_data) {
1467 tu = file->private_data;
1468 file->private_data = NULL;
Takashi Iwaiaf368022016-01-13 17:48:01 +01001469 mutex_lock(&tu->ioctl_lock);
Takashi Iwai6a343672019-11-07 20:20:08 +01001470 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 snd_timer_close(tu->timeri);
Takashi Iwai6a343672019-11-07 20:20:08 +01001472 snd_timer_instance_free(tu->timeri);
1473 }
Takashi Iwaiaf368022016-01-13 17:48:01 +01001474 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 kfree(tu->queue);
1476 kfree(tu->tqueue);
1477 kfree(tu);
1478 }
1479 return 0;
1480}
1481
Takashi Iwai53d2f742005-11-17 13:56:05 +01001482static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1485 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1486 id->card = -1;
1487 id->device = -1;
1488 id->subdevice = -1;
1489}
1490
Takashi Iwai53d2f742005-11-17 13:56:05 +01001491static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 id->dev_class = timer->tmr_class;
1494 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1495 id->card = timer->card ? timer->card->number : -1;
1496 id->device = timer->tmr_device;
1497 id->subdevice = timer->tmr_subdevice;
1498}
1499
Takashi Iwai53d2f742005-11-17 13:56:05 +01001500static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001502 struct snd_timer_id id;
1503 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (copy_from_user(&id, _tid, sizeof(id)))
1507 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001508 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (id.dev_class < 0) { /* first item */
1510 if (list_empty(&snd_timer_list))
1511 snd_timer_user_zero_id(&id);
1512 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001513 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001514 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 snd_timer_user_copy_id(&id, timer);
1516 }
1517 } else {
1518 switch (id.dev_class) {
1519 case SNDRV_TIMER_CLASS_GLOBAL:
1520 id.device = id.device < 0 ? 0 : id.device + 1;
1521 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001522 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1524 snd_timer_user_copy_id(&id, timer);
1525 break;
1526 }
1527 if (timer->tmr_device >= id.device) {
1528 snd_timer_user_copy_id(&id, timer);
1529 break;
1530 }
1531 }
1532 if (p == &snd_timer_list)
1533 snd_timer_user_zero_id(&id);
1534 break;
1535 case SNDRV_TIMER_CLASS_CARD:
1536 case SNDRV_TIMER_CLASS_PCM:
1537 if (id.card < 0) {
1538 id.card = 0;
1539 } else {
Dan Carpentere8ed6822017-03-31 18:21:41 +03001540 if (id.device < 0) {
1541 id.device = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 } else {
Dan Carpentere8ed6822017-03-31 18:21:41 +03001543 if (id.subdevice < 0)
1544 id.subdevice = 0;
Takashi Iwaib41f7942018-06-25 11:09:11 +02001545 else if (id.subdevice < INT_MAX)
Dan Carpentere8ed6822017-03-31 18:21:41 +03001546 id.subdevice++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548 }
1549 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001550 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (timer->tmr_class > id.dev_class) {
1552 snd_timer_user_copy_id(&id, timer);
1553 break;
1554 }
1555 if (timer->tmr_class < id.dev_class)
1556 continue;
1557 if (timer->card->number > id.card) {
1558 snd_timer_user_copy_id(&id, timer);
1559 break;
1560 }
1561 if (timer->card->number < id.card)
1562 continue;
1563 if (timer->tmr_device > id.device) {
1564 snd_timer_user_copy_id(&id, timer);
1565 break;
1566 }
1567 if (timer->tmr_device < id.device)
1568 continue;
1569 if (timer->tmr_subdevice > id.subdevice) {
1570 snd_timer_user_copy_id(&id, timer);
1571 break;
1572 }
1573 if (timer->tmr_subdevice < id.subdevice)
1574 continue;
1575 snd_timer_user_copy_id(&id, timer);
1576 break;
1577 }
1578 if (p == &snd_timer_list)
1579 snd_timer_user_zero_id(&id);
1580 break;
1581 default:
1582 snd_timer_user_zero_id(&id);
1583 }
1584 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001585 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1587 return -EFAULT;
1588 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001591static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001592 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001594 struct snd_timer_ginfo *ginfo;
1595 struct snd_timer_id tid;
1596 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 struct list_head *p;
1598 int err = 0;
1599
Li Zefanef44a1e2009-04-10 09:43:08 +08001600 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1601 if (IS_ERR(ginfo))
1602 return PTR_ERR(ginfo);
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 tid = ginfo->tid;
1605 memset(ginfo, 0, sizeof(*ginfo));
1606 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001607 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 t = snd_timer_find(&tid);
1609 if (t != NULL) {
1610 ginfo->card = t->card ? t->card->number : -1;
1611 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1612 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1613 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1614 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1615 ginfo->resolution = t->hw.resolution;
1616 if (t->hw.resolution_min > 0) {
1617 ginfo->resolution_min = t->hw.resolution_min;
1618 ginfo->resolution_max = t->hw.resolution_max;
1619 }
1620 list_for_each(p, &t->open_list_head) {
1621 ginfo->clients++;
1622 }
1623 } else {
1624 err = -ENODEV;
1625 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001626 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1628 err = -EFAULT;
1629 kfree(ginfo);
1630 return err;
1631}
1632
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001633static int timer_set_gparams(struct snd_timer_gparams *gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001635 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int err;
1637
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001638 mutex_lock(&register_mutex);
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001639 t = snd_timer_find(&gparams->tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001640 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001642 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001644 if (!list_empty(&t->open_list_head)) {
1645 err = -EBUSY;
1646 goto _error;
1647 }
1648 if (!t->hw.set_period) {
1649 err = -ENOSYS;
1650 goto _error;
1651 }
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001652 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001653_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001654 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return err;
1656}
1657
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001658static int snd_timer_user_gparams(struct file *file,
1659 struct snd_timer_gparams __user *_gparams)
1660{
1661 struct snd_timer_gparams gparams;
1662
1663 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1664 return -EFAULT;
1665 return timer_set_gparams(&gparams);
1666}
1667
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001668static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001669 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001671 struct snd_timer_gstatus gstatus;
1672 struct snd_timer_id tid;
1673 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 int err = 0;
1675
1676 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1677 return -EFAULT;
1678 tid = gstatus.tid;
1679 memset(&gstatus, 0, sizeof(gstatus));
1680 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001681 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 t = snd_timer_find(&tid);
1683 if (t != NULL) {
Takashi Iwai9d4d2072018-05-16 23:52:42 +02001684 spin_lock_irq(&t->lock);
Takashi Iwaifdcb5762018-05-16 23:45:33 +02001685 gstatus.resolution = snd_timer_hw_resolution(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001687 t->hw.precise_resolution(t, &gstatus.resolution_num,
1688 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 } else {
1690 gstatus.resolution_num = gstatus.resolution;
1691 gstatus.resolution_den = 1000000000uL;
1692 }
Takashi Iwai9d4d2072018-05-16 23:52:42 +02001693 spin_unlock_irq(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 } else {
1695 err = -ENODEV;
1696 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001697 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1699 err = -EFAULT;
1700 return err;
1701}
1702
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001703static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001704 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001706 struct snd_timer_user *tu;
1707 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001709 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 tu = file->private_data;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001712 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 snd_timer_close(tu->timeri);
Takashi Iwai6a343672019-11-07 20:20:08 +01001714 snd_timer_instance_free(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001715 tu->timeri = NULL;
1716 }
1717 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1718 err = -EFAULT;
1719 goto __err;
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 sprintf(str, "application %i", current->pid);
1722 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1723 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Takashi Iwai6a343672019-11-07 20:20:08 +01001724 tu->timeri = snd_timer_instance_new(str);
1725 if (!tu->timeri) {
1726 err = -ENOMEM;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001727 goto __err;
Takashi Iwai6a343672019-11-07 20:20:08 +01001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001730 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1731 tu->timeri->callback = tu->tread
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001732 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001733 tu->timeri->ccallback = snd_timer_user_ccallback;
1734 tu->timeri->callback_data = (void *)tu;
1735 tu->timeri->disconnect = snd_timer_user_disconnect;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001736
Takashi Iwai6a343672019-11-07 20:20:08 +01001737 err = snd_timer_open(tu->timeri, &tselect.id, current->pid);
1738 if (err < 0) {
1739 snd_timer_instance_free(tu->timeri);
1740 tu->timeri = NULL;
1741 }
1742
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001743 __err:
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001747static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001748 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001750 struct snd_timer_user *tu;
1751 struct snd_timer_info *info;
1752 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int err = 0;
1754
1755 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001756 if (!tu->timeri)
1757 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001759 if (!t)
1760 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Takashi Iwaica2c0962005-09-09 14:20:23 +02001762 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (! info)
1764 return -ENOMEM;
1765 info->card = t->card ? t->card->number : -1;
1766 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1767 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1768 strlcpy(info->id, t->id, sizeof(info->id));
1769 strlcpy(info->name, t->name, sizeof(info->name));
1770 info->resolution = t->hw.resolution;
1771 if (copy_to_user(_info, info, sizeof(*_info)))
1772 err = -EFAULT;
1773 kfree(info);
1774 return err;
1775}
1776
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001777static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001778 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001780 struct snd_timer_user *tu;
1781 struct snd_timer_params params;
1782 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001786 if (!tu->timeri)
1787 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001789 if (!t)
1790 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (copy_from_user(&params, _params, sizeof(params)))
1792 return -EFAULT;
Takashi Iwai71321eb2017-02-28 14:49:07 +01001793 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1794 u64 resolution;
1795
1796 if (params.ticks < 1) {
1797 err = -EINVAL;
1798 goto _end;
1799 }
1800
1801 /* Don't allow resolution less than 1ms */
1802 resolution = snd_timer_resolution(tu->timeri);
1803 resolution *= params.ticks;
1804 if (resolution < 1000000) {
1805 err = -EINVAL;
1806 goto _end;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001809 if (params.queue_size > 0 &&
1810 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 err = -EINVAL;
1812 goto _end;
1813 }
1814 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1815 (1<<SNDRV_TIMER_EVENT_TICK)|
1816 (1<<SNDRV_TIMER_EVENT_START)|
1817 (1<<SNDRV_TIMER_EVENT_STOP)|
1818 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1819 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001820 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1821 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 (1<<SNDRV_TIMER_EVENT_MSTART)|
1823 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1824 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001825 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1826 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001827 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 err = -EINVAL;
1829 goto _end;
1830 }
1831 snd_timer_stop(tu->timeri);
1832 spin_lock_irq(&t->lock);
1833 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1834 SNDRV_TIMER_IFLG_EXCLUSIVE|
1835 SNDRV_TIMER_IFLG_EARLY_EVENT);
1836 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1837 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1838 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1839 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1840 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1841 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1842 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001843 if (params.queue_size > 0 &&
1844 (unsigned int)tu->queue_size != params.queue_size) {
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001845 err = realloc_user_queue(tu, params.queue_size);
1846 if (err < 0)
1847 goto _end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
Takashi Iwaid7f910b2017-06-02 17:35:30 +02001849 spin_lock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 tu->qhead = tu->qtail = tu->qused = 0;
1851 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1852 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001853 struct snd_timer_tread tread;
Kangjie Lucec8f962016-05-03 16:44:07 -04001854 memset(&tread, 0, sizeof(tread));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 tread.event = SNDRV_TIMER_EVENT_EARLY;
1856 tread.tstamp.tv_sec = 0;
1857 tread.tstamp.tv_nsec = 0;
1858 tread.val = 0;
1859 snd_timer_user_append_to_tqueue(tu, &tread);
1860 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001861 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 r->resolution = 0;
1863 r->ticks = 0;
1864 tu->qused++;
1865 tu->qtail++;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868 tu->filter = params.filter;
1869 tu->ticks = params.ticks;
Takashi Iwaid7f910b2017-06-02 17:35:30 +02001870 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 err = 0;
1872 _end:
1873 if (copy_to_user(_params, &params, sizeof(params)))
1874 return -EFAULT;
1875 return err;
1876}
1877
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001878static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001879 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001881 struct snd_timer_user *tu;
1882 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001885 if (!tu->timeri)
1886 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 memset(&status, 0, sizeof(status));
1888 status.tstamp = tu->tstamp;
1889 status.resolution = snd_timer_resolution(tu->timeri);
1890 status.lost = tu->timeri->lost;
1891 status.overrun = tu->overrun;
1892 spin_lock_irq(&tu->qlock);
1893 status.queue = tu->qused;
1894 spin_unlock_irq(&tu->qlock);
1895 if (copy_to_user(_status, &status, sizeof(status)))
1896 return -EFAULT;
1897 return 0;
1898}
1899
1900static int snd_timer_user_start(struct file *file)
1901{
1902 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001903 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001906 if (!tu->timeri)
1907 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 snd_timer_stop(tu->timeri);
1909 tu->timeri->lost = 0;
1910 tu->last_resolution = 0;
Takashi Iwai5d704b02019-03-28 17:44:53 +01001911 err = snd_timer_start(tu->timeri, tu->ticks);
1912 if (err < 0)
1913 return err;
1914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917static int snd_timer_user_stop(struct file *file)
1918{
1919 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001920 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001923 if (!tu->timeri)
1924 return -EBADFD;
Takashi Iwai5d704b02019-03-28 17:44:53 +01001925 err = snd_timer_stop(tu->timeri);
1926 if (err < 0)
1927 return err;
1928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931static int snd_timer_user_continue(struct file *file)
1932{
1933 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001934 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001937 if (!tu->timeri)
1938 return -EBADFD;
Takashi Iwai9f8a7652016-09-07 15:45:31 +02001939 /* start timer instead of continue if it's not used before */
1940 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1941 return snd_timer_user_start(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 tu->timeri->lost = 0;
Takashi Iwai5d704b02019-03-28 17:44:53 +01001943 err = snd_timer_continue(tu->timeri);
1944 if (err < 0)
1945 return err;
1946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Takashi Iwai15790a62005-05-15 15:04:14 +02001949static int snd_timer_user_pause(struct file *file)
1950{
1951 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001952 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001953
Takashi Iwai15790a62005-05-15 15:04:14 +02001954 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001955 if (!tu->timeri)
1956 return -EBADFD;
Takashi Iwai5d704b02019-03-28 17:44:53 +01001957 err = snd_timer_pause(tu->timeri);
1958 if (err < 0)
1959 return err;
1960 return 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001961}
1962
Takashi Iwai8c50b372005-05-15 15:43:54 +02001963enum {
1964 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1965 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1966 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1967 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1968};
1969
Takashi Iwaiaf368022016-01-13 17:48:01 +01001970static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001971 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001973 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 void __user *argp = (void __user *)arg;
1975 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 tu = file->private_data;
1978 switch (cmd) {
1979 case SNDRV_TIMER_IOCTL_PVERSION:
1980 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1981 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1982 return snd_timer_user_next_device(argp);
1983 case SNDRV_TIMER_IOCTL_TREAD:
1984 {
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001985 int xarg, old_tread;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001986
Takashi Iwaiaf368022016-01-13 17:48:01 +01001987 if (tu->timeri) /* too late */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return -EBUSY;
Takashi Iwaiaf368022016-01-13 17:48:01 +01001989 if (get_user(xarg, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return -EFAULT;
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001991 old_tread = tu->tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 tu->tread = xarg ? 1 : 0;
Takashi Iwai890e2cb2017-06-02 17:16:59 +02001993 if (tu->tread != old_tread &&
1994 realloc_user_queue(tu, tu->queue_size) < 0) {
1995 tu->tread = old_tread;
1996 return -ENOMEM;
1997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 0;
1999 }
2000 case SNDRV_TIMER_IOCTL_GINFO:
2001 return snd_timer_user_ginfo(file, argp);
2002 case SNDRV_TIMER_IOCTL_GPARAMS:
2003 return snd_timer_user_gparams(file, argp);
2004 case SNDRV_TIMER_IOCTL_GSTATUS:
2005 return snd_timer_user_gstatus(file, argp);
2006 case SNDRV_TIMER_IOCTL_SELECT:
2007 return snd_timer_user_tselect(file, argp);
2008 case SNDRV_TIMER_IOCTL_INFO:
2009 return snd_timer_user_info(file, argp);
2010 case SNDRV_TIMER_IOCTL_PARAMS:
2011 return snd_timer_user_params(file, argp);
2012 case SNDRV_TIMER_IOCTL_STATUS:
2013 return snd_timer_user_status(file, argp);
2014 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02002015 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return snd_timer_user_start(file);
2017 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02002018 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return snd_timer_user_stop(file);
2020 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02002021 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02002023 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02002024 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02002025 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 return -ENOTTY;
2028}
2029
Takashi Iwaiaf368022016-01-13 17:48:01 +01002030static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2031 unsigned long arg)
2032{
2033 struct snd_timer_user *tu = file->private_data;
2034 long ret;
2035
2036 mutex_lock(&tu->ioctl_lock);
2037 ret = __snd_timer_user_ioctl(file, cmd, arg);
2038 mutex_unlock(&tu->ioctl_lock);
2039 return ret;
2040}
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042static int snd_timer_user_fasync(int fd, struct file * file, int on)
2043{
Takashi Iwai53d2f742005-11-17 13:56:05 +01002044 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002047 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002050static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2051 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Takashi Iwai53d2f742005-11-17 13:56:05 +01002053 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 long result = 0, unit;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002055 int qhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01002059 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Takashi Iwaid11662f42017-06-02 15:03:38 +02002060 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 spin_lock_irq(&tu->qlock);
2062 while ((long)count - result >= unit) {
2063 while (!tu->qused) {
Ingo Molnarac6424b2017-06-20 12:06:13 +02002064 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2067 err = -EAGAIN;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002068 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070
2071 set_current_state(TASK_INTERRUPTIBLE);
2072 init_waitqueue_entry(&wait, current);
2073 add_wait_queue(&tu->qchange_sleep, &wait);
2074
2075 spin_unlock_irq(&tu->qlock);
Takashi Iwaid11662f42017-06-02 15:03:38 +02002076 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 schedule();
Takashi Iwaid11662f42017-06-02 15:03:38 +02002078 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 spin_lock_irq(&tu->qlock);
2080
2081 remove_wait_queue(&tu->qchange_sleep, &wait);
2082
Takashi Iwai230323d2016-01-21 17:19:31 +01002083 if (tu->disconnected) {
2084 err = -ENODEV;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002085 goto _error;
Takashi Iwai230323d2016-01-21 17:19:31 +01002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (signal_pending(current)) {
2088 err = -ERESTARTSYS;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002089 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091 }
2092
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002093 qhead = tu->qhead++;
2094 tu->qhead %= tu->queue_size;
Takashi Iwai3fa69932016-07-04 14:02:15 +02002095 tu->qused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 if (tu->tread) {
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002099 if (copy_to_user(buffer, &tu->tqueue[qhead],
2100 sizeof(struct snd_timer_tread)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 } else {
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002103 if (copy_to_user(buffer, &tu->queue[qhead],
2104 sizeof(struct snd_timer_read)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 spin_lock_irq(&tu->qlock);
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002109 if (err < 0)
2110 goto _error;
2111 result += unit;
2112 buffer += unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 _error:
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002115 spin_unlock_irq(&tu->qlock);
Takashi Iwaid11662f42017-06-02 15:03:38 +02002116 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return result > 0 ? result : err;
2118}
2119
Al Viro680ef722017-07-02 23:27:36 -04002120static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Al Viro680ef722017-07-02 23:27:36 -04002122 __poll_t mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01002123 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 tu = file->private_data;
2126
2127 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 mask = 0;
Takashi Iwaid7f910b2017-06-02 17:35:30 +02002130 spin_lock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (tu->qused)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002132 mask |= EPOLLIN | EPOLLRDNORM;
Takashi Iwai230323d2016-01-21 17:19:31 +01002133 if (tu->disconnected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002134 mask |= EPOLLERR;
Takashi Iwaid7f910b2017-06-02 17:35:30 +02002135 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 return mask;
2138}
2139
2140#ifdef CONFIG_COMPAT
2141#include "timer_compat.c"
2142#else
2143#define snd_timer_user_ioctl_compat NULL
2144#endif
2145
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002146static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 .owner = THIS_MODULE,
2149 .read = snd_timer_user_read,
2150 .open = snd_timer_user_open,
2151 .release = snd_timer_user_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02002152 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 .poll = snd_timer_user_poll,
2154 .unlocked_ioctl = snd_timer_user_ioctl,
2155 .compat_ioctl = snd_timer_user_ioctl_compat,
2156 .fasync = snd_timer_user_fasync,
2157};
2158
Takashi Iwai7c358602015-01-29 18:09:05 +01002159/* unregister the system timer */
2160static void snd_timer_free_all(void)
2161{
2162 struct snd_timer *timer, *n;
2163
2164 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2165 snd_timer_free(timer);
2166}
2167
Takashi Iwai89da0612015-01-29 18:12:26 +01002168static struct device timer_dev;
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170/*
2171 * ENTRY functions
2172 */
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174static int __init alsa_timer_init(void)
2175{
2176 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Takashi Iwai89da0612015-01-29 18:12:26 +01002178 snd_device_initialize(&timer_dev, NULL);
2179 dev_set_name(&timer_dev, "timer");
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002182 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2183 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01002185
Takashi Iwai7c358602015-01-29 18:09:05 +01002186 err = snd_timer_register_system();
2187 if (err < 0) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002188 pr_err("ALSA: unable to register system timer (%i)\n", err);
Markus Elfring1ae0e4c2017-08-23 09:30:41 +02002189 goto put_timer;
Takashi Iwai7c358602015-01-29 18:09:05 +01002190 }
2191
Takashi Iwai40a4b262015-01-30 08:34:58 +01002192 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2193 &snd_timer_f_ops, NULL, &timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002194 if (err < 0) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002195 pr_err("ALSA: unable to register timer device (%i)\n", err);
Takashi Iwai7c358602015-01-29 18:09:05 +01002196 snd_timer_free_all();
Markus Elfring1ae0e4c2017-08-23 09:30:41 +02002197 goto put_timer;
Takashi Iwai7c358602015-01-29 18:09:05 +01002198 }
2199
Takashi Iwaie28563c2005-12-01 10:42:42 +01002200 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return 0;
Markus Elfring1ae0e4c2017-08-23 09:30:41 +02002202
2203put_timer:
2204 put_device(&timer_dev);
2205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
2208static void __exit alsa_timer_exit(void)
2209{
Takashi Iwai40a4b262015-01-30 08:34:58 +01002210 snd_unregister_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002211 snd_timer_free_all();
Takashi Iwai89da0612015-01-29 18:12:26 +01002212 put_device(&timer_dev);
Takashi Iwaie28563c2005-12-01 10:42:42 +01002213 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2215 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2216#endif
2217}
2218
2219module_init(alsa_timer_init)
2220module_exit(alsa_timer_exit)