Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Timers abstract layer |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 4 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/delay.h> |
| 8 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/slab.h> |
| 10 | #include <linux/time.h> |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 11 | #include <linux/mutex.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 12 | #include <linux/device.h> |
Paul Gortmaker | 65a7721 | 2011-07-15 13:13:37 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 14 | #include <linux/string.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 15 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 24 | /* internal flags */ |
| 25 | #define SNDRV_TIMER_IFLG_PAUSED 0x00010000 |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 26 | #define SNDRV_TIMER_IFLG_DEAD 0x00020000 |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 27 | |
Takashi Iwai | 8eeaa2f | 2014-02-10 09:48:47 +0100 | [diff] [blame] | 28 | #if IS_ENABLED(CONFIG_SND_HRTIMER) |
Clemens Ladisch | 109fef9 | 2010-11-18 09:53:54 +0100 | [diff] [blame] | 29 | #define DEFAULT_TIMER_LIMIT 4 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #else |
| 31 | #define DEFAULT_TIMER_LIMIT 1 |
| 32 | #endif |
| 33 | |
| 34 | static int timer_limit = DEFAULT_TIMER_LIMIT; |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 35 | static int timer_tstamp_monotonic = 1; |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 36 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | MODULE_DESCRIPTION("ALSA timer interface"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | module_param(timer_limit, int, 0444); |
| 40 | MODULE_PARM_DESC(timer_limit, "Maximum global timers in system."); |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 41 | module_param(timer_tstamp_monotonic, int, 0444); |
| 42 | MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default)."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Kay Sievers | 03cfe6f | 2010-11-23 17:43:19 +0100 | [diff] [blame] | 44 | MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER); |
| 45 | MODULE_ALIAS("devname:snd/timer"); |
| 46 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 47 | struct snd_timer_user { |
| 48 | struct snd_timer_instance *timeri; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 49 | int tread; /* enhanced read with timestamps and events */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | unsigned long ticks; |
| 51 | unsigned long overrun; |
| 52 | int qhead; |
| 53 | int qtail; |
| 54 | int qused; |
| 55 | int queue_size; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 56 | bool disconnected; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 57 | struct snd_timer_read *queue; |
| 58 | struct snd_timer_tread *tqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | 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 Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 65 | struct mutex ioctl_lock; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 66 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | /* list of timers */ |
| 69 | static LIST_HEAD(snd_timer_list); |
| 70 | |
| 71 | /* list of slave instances */ |
| 72 | static LIST_HEAD(snd_timer_slave_list); |
| 73 | |
| 74 | /* lock for slave active lists */ |
| 75 | static DEFINE_SPINLOCK(slave_active_lock); |
| 76 | |
Takashi Iwai | fdea53f | 2019-11-06 16:42:57 +0100 | [diff] [blame] | 77 | #define MAX_SLAVE_INSTANCES 1000 |
| 78 | static int num_slaves; |
| 79 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 80 | static DEFINE_MUTEX(register_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 82 | static int snd_timer_free(struct snd_timer *timer); |
| 83 | static int snd_timer_dev_free(struct snd_device *device); |
| 84 | static int snd_timer_dev_register(struct snd_device *device); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 85 | static int snd_timer_dev_disconnect(struct snd_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 87 | static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * create a timer instance with the given owner string. |
| 91 | * when timer is not NULL, increments the module counter |
| 92 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 93 | static struct snd_timer_instance *snd_timer_instance_new(char *owner, |
| 94 | struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 96 | struct snd_timer_instance *timeri; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 97 | timeri = kzalloc(sizeof(*timeri), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | if (timeri == NULL) |
| 99 | return NULL; |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 100 | timeri->owner = kstrdup(owner, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (! timeri->owner) { |
| 102 | kfree(timeri); |
| 103 | return NULL; |
| 104 | } |
| 105 | INIT_LIST_HEAD(&timeri->open_list); |
| 106 | INIT_LIST_HEAD(&timeri->active_list); |
| 107 | INIT_LIST_HEAD(&timeri->ack_list); |
| 108 | INIT_LIST_HEAD(&timeri->slave_list_head); |
| 109 | INIT_LIST_HEAD(&timeri->slave_active_head); |
| 110 | |
| 111 | timeri->timer = timer; |
Clemens Ladisch | de24214 | 2005-10-12 17:12:31 +0200 | [diff] [blame] | 112 | if (timer && !try_module_get(timer->module)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | kfree(timeri->owner); |
| 114 | kfree(timeri); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | return timeri; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * find a timer instance from the given timer id |
| 123 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 124 | static struct snd_timer *snd_timer_find(struct snd_timer_id *tid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 126 | struct snd_timer *timer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 128 | list_for_each_entry(timer, &snd_timer_list, device_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (timer->tmr_class != tid->dev_class) |
| 130 | continue; |
| 131 | if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD || |
| 132 | timer->tmr_class == SNDRV_TIMER_CLASS_PCM) && |
| 133 | (timer->card == NULL || |
| 134 | timer->card->number != tid->card)) |
| 135 | continue; |
| 136 | if (timer->tmr_device != tid->device) |
| 137 | continue; |
| 138 | if (timer->tmr_subdevice != tid->subdevice) |
| 139 | continue; |
| 140 | return timer; |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
Johannes Berg | ee2da99 | 2008-07-09 10:28:41 +0200 | [diff] [blame] | 145 | #ifdef CONFIG_MODULES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 147 | static void snd_timer_request(struct snd_timer_id *tid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | switch (tid->dev_class) { |
| 150 | case SNDRV_TIMER_CLASS_GLOBAL: |
| 151 | if (tid->device < timer_limit) |
| 152 | request_module("snd-timer-%i", tid->device); |
| 153 | break; |
| 154 | case SNDRV_TIMER_CLASS_CARD: |
| 155 | case SNDRV_TIMER_CLASS_PCM: |
| 156 | if (tid->card < snd_ecards_limit) |
| 157 | request_module("snd-card-%i", tid->card); |
| 158 | break; |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #endif |
| 165 | |
Takashi Iwai | ebfc6de | 2019-11-07 20:20:06 +0100 | [diff] [blame^] | 166 | /* move the slave if it belongs to the master; return 1 if match */ |
| 167 | static int check_matching_master_slave(struct snd_timer_instance *master, |
| 168 | struct snd_timer_instance *slave) |
| 169 | { |
| 170 | if (slave->slave_class != master->slave_class || |
| 171 | slave->slave_id != master->slave_id) |
| 172 | return 0; |
| 173 | if (master->timer->num_instances >= master->timer->max_instances) |
| 174 | return -EBUSY; |
| 175 | list_move_tail(&slave->open_list, &master->slave_list_head); |
| 176 | master->timer->num_instances++; |
| 177 | spin_lock_irq(&slave_active_lock); |
| 178 | spin_lock(&master->timer->lock); |
| 179 | slave->master = master; |
| 180 | slave->timer = master->timer; |
| 181 | if (slave->flags & SNDRV_TIMER_IFLG_RUNNING) |
| 182 | list_add_tail(&slave->active_list, &master->slave_active_head); |
| 183 | spin_unlock(&master->timer->lock); |
| 184 | spin_unlock_irq(&slave_active_lock); |
| 185 | return 1; |
| 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | /* |
| 189 | * look for a master instance matching with the slave id of the given slave. |
| 190 | * when found, relink the open_link of the slave. |
| 191 | * |
| 192 | * call this with register_mutex down. |
| 193 | */ |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 194 | static int snd_timer_check_slave(struct snd_timer_instance *slave) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 196 | struct snd_timer *timer; |
| 197 | struct snd_timer_instance *master; |
Takashi Iwai | ebfc6de | 2019-11-07 20:20:06 +0100 | [diff] [blame^] | 198 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | /* FIXME: it's really dumb to look up all entries.. */ |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 201 | list_for_each_entry(timer, &snd_timer_list, device_list) { |
| 202 | list_for_each_entry(master, &timer->open_list_head, open_list) { |
Takashi Iwai | ebfc6de | 2019-11-07 20:20:06 +0100 | [diff] [blame^] | 203 | err = check_matching_master_slave(master, slave); |
| 204 | if (err != 0) /* match found or error */ |
| 205 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
Takashi Iwai | ebfc6de | 2019-11-07 20:20:06 +0100 | [diff] [blame^] | 208 | out: |
| 209 | return err < 0 ? err : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
| 213 | * look for slave instances matching with the slave id of the given master. |
| 214 | * when found, relink the open_link of slaves. |
| 215 | * |
| 216 | * call this with register_mutex down. |
| 217 | */ |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 218 | static int snd_timer_check_master(struct snd_timer_instance *master) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 220 | struct snd_timer_instance *slave, *tmp; |
Takashi Iwai | ebfc6de | 2019-11-07 20:20:06 +0100 | [diff] [blame^] | 221 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | /* check all pending slaves */ |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 224 | list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) { |
Takashi Iwai | ebfc6de | 2019-11-07 20:20:06 +0100 | [diff] [blame^] | 225 | err = check_matching_master_slave(master, slave); |
| 226 | if (err < 0) |
| 227 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
Takashi Iwai | ebfc6de | 2019-11-07 20:20:06 +0100 | [diff] [blame^] | 229 | return err < 0 ? err : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 232 | static int snd_timer_close_locked(struct snd_timer_instance *timeri, |
| 233 | struct device **card_devp_to_put); |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | /* |
| 236 | * open a timer instance |
| 237 | * when opening a master, the slave id must be here given. |
| 238 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 239 | int snd_timer_open(struct snd_timer_instance **ti, |
| 240 | char *owner, struct snd_timer_id *tid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | unsigned int slave_id) |
| 242 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 243 | struct snd_timer *timer; |
| 244 | struct snd_timer_instance *timeri = NULL; |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 245 | struct device *card_dev_to_put = NULL; |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 246 | int err; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 247 | |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 248 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) { |
| 250 | /* open a slave instance */ |
| 251 | if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE || |
| 252 | tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) { |
Takashi Iwai | cf74dcf | 2014-02-04 18:22:39 +0100 | [diff] [blame] | 253 | pr_debug("ALSA: timer: invalid slave class %i\n", |
| 254 | tid->dev_sclass); |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 255 | err = -EINVAL; |
| 256 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
Takashi Iwai | fdea53f | 2019-11-06 16:42:57 +0100 | [diff] [blame] | 258 | if (num_slaves >= MAX_SLAVE_INSTANCES) { |
| 259 | err = -EBUSY; |
| 260 | goto unlock; |
| 261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | timeri = snd_timer_instance_new(owner, NULL); |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 263 | if (!timeri) { |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 264 | err = -ENOMEM; |
| 265 | goto unlock; |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | timeri->slave_class = tid->dev_sclass; |
| 268 | timeri->slave_id = tid->device; |
| 269 | timeri->flags |= SNDRV_TIMER_IFLG_SLAVE; |
| 270 | list_add_tail(&timeri->open_list, &snd_timer_slave_list); |
Takashi Iwai | fdea53f | 2019-11-06 16:42:57 +0100 | [diff] [blame] | 271 | num_slaves++; |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 272 | err = snd_timer_check_slave(timeri); |
| 273 | if (err < 0) { |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 274 | snd_timer_close_locked(timeri, &card_dev_to_put); |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 275 | timeri = NULL; |
| 276 | } |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 277 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* open a master instance */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | timer = snd_timer_find(tid); |
Johannes Berg | ee2da99 | 2008-07-09 10:28:41 +0200 | [diff] [blame] | 282 | #ifdef CONFIG_MODULES |
| 283 | if (!timer) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 284 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | snd_timer_request(tid); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 286 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | timer = snd_timer_find(tid); |
| 288 | } |
| 289 | #endif |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 290 | if (!timer) { |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 291 | err = -ENODEV; |
| 292 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 294 | if (!list_empty(&timer->open_list_head)) { |
Takashi Iwai | e7af630 | 2019-11-06 17:55:47 +0100 | [diff] [blame] | 295 | struct snd_timer_instance *t = |
| 296 | list_entry(timer->open_list_head.next, |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 297 | struct snd_timer_instance, open_list); |
Takashi Iwai | e7af630 | 2019-11-06 17:55:47 +0100 | [diff] [blame] | 298 | if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) { |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 299 | err = -EBUSY; |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 300 | goto unlock; |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 301 | } |
| 302 | } |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 303 | if (timer->num_instances >= timer->max_instances) { |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 304 | err = -EBUSY; |
| 305 | goto unlock; |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 306 | } |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 307 | timeri = snd_timer_instance_new(owner, timer); |
| 308 | if (!timeri) { |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 309 | err = -ENOMEM; |
| 310 | goto unlock; |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 311 | } |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 312 | /* take a card refcount for safe disconnection */ |
| 313 | if (timer->card) |
| 314 | get_device(&timer->card->card_dev); |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 315 | timeri->slave_class = tid->dev_sclass; |
| 316 | timeri->slave_id = slave_id; |
Vegard Nossum | 8ddc056 | 2016-08-29 00:33:51 +0200 | [diff] [blame] | 317 | |
| 318 | if (list_empty(&timer->open_list_head) && timer->hw.open) { |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 319 | err = timer->hw.open(timer); |
Vegard Nossum | 8ddc056 | 2016-08-29 00:33:51 +0200 | [diff] [blame] | 320 | if (err) { |
| 321 | kfree(timeri->owner); |
| 322 | kfree(timeri); |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 323 | timeri = NULL; |
Vegard Nossum | 8ddc056 | 2016-08-29 00:33:51 +0200 | [diff] [blame] | 324 | |
| 325 | if (timer->card) |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 326 | card_dev_to_put = &timer->card->card_dev; |
Vegard Nossum | 8ddc056 | 2016-08-29 00:33:51 +0200 | [diff] [blame] | 327 | module_put(timer->module); |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 328 | goto unlock; |
Vegard Nossum | 8ddc056 | 2016-08-29 00:33:51 +0200 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 332 | list_add_tail(&timeri->open_list, &timer->open_list_head); |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 333 | timer->num_instances++; |
| 334 | err = snd_timer_check_master(timeri); |
| 335 | if (err < 0) { |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 336 | snd_timer_close_locked(timeri, &card_dev_to_put); |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 337 | timeri = NULL; |
| 338 | } |
Takashi Iwai | 41672c0 | 2019-03-28 17:11:10 +0100 | [diff] [blame] | 339 | |
| 340 | unlock: |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 341 | mutex_unlock(®ister_mutex); |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 342 | /* put_device() is called after unlock for avoiding deadlock */ |
| 343 | if (card_dev_to_put) |
| 344 | put_device(card_dev_to_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | *ti = timeri; |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 346 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 348 | EXPORT_SYMBOL(snd_timer_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | /* |
| 351 | * close a timer instance |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 352 | * call this with register_mutex down. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | */ |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 354 | static int snd_timer_close_locked(struct snd_timer_instance *timeri, |
| 355 | struct device **card_devp_to_put) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 357 | struct snd_timer *timer = timeri->timer; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 358 | struct snd_timer_instance *slave, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 360 | if (timer) { |
| 361 | spin_lock_irq(&timer->lock); |
| 362 | timeri->flags |= SNDRV_TIMER_IFLG_DEAD; |
| 363 | spin_unlock_irq(&timer->lock); |
| 364 | } |
| 365 | |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 366 | list_del(&timeri->open_list); |
Takashi Iwai | fdea53f | 2019-11-06 16:42:57 +0100 | [diff] [blame] | 367 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 368 | num_slaves--; |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | /* force to stop the timer */ |
| 371 | snd_timer_stop(timeri); |
| 372 | |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 373 | if (timer) { |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 374 | timer->num_instances--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | /* wait, until the active callback is finished */ |
| 376 | spin_lock_irq(&timer->lock); |
| 377 | while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) { |
| 378 | spin_unlock_irq(&timer->lock); |
| 379 | udelay(10); |
| 380 | spin_lock_irq(&timer->lock); |
| 381 | } |
| 382 | spin_unlock_irq(&timer->lock); |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* remove slave links */ |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 385 | spin_lock_irq(&slave_active_lock); |
| 386 | spin_lock(&timer->lock); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 387 | list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head, |
| 388 | open_list) { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 389 | list_move_tail(&slave->open_list, &snd_timer_slave_list); |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 390 | timer->num_instances--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | slave->master = NULL; |
| 392 | slave->timer = NULL; |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 393 | list_del_init(&slave->ack_list); |
| 394 | list_del_init(&slave->active_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 396 | spin_unlock(&timer->lock); |
| 397 | spin_unlock_irq(&slave_active_lock); |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 398 | |
| 399 | /* slave doesn't need to release timer resources below */ |
| 400 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 401 | timer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (timeri->private_free) |
| 405 | timeri->private_free(timeri); |
| 406 | kfree(timeri->owner); |
| 407 | kfree(timeri); |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 408 | |
| 409 | if (timer) { |
| 410 | if (list_empty(&timer->open_list_head) && timer->hw.close) |
| 411 | timer->hw.close(timer); |
| 412 | /* release a card refcount for safe disconnection */ |
| 413 | if (timer->card) |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 414 | *card_devp_to_put = &timer->card->card_dev; |
Clemens Ladisch | de24214 | 2005-10-12 17:12:31 +0200 | [diff] [blame] | 415 | module_put(timer->module); |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | } |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * close a timer instance |
| 423 | */ |
| 424 | int snd_timer_close(struct snd_timer_instance *timeri) |
| 425 | { |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 426 | struct device *card_dev_to_put = NULL; |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 427 | int err; |
| 428 | |
| 429 | if (snd_BUG_ON(!timeri)) |
| 430 | return -ENXIO; |
| 431 | |
| 432 | mutex_lock(®ister_mutex); |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 433 | err = snd_timer_close_locked(timeri, &card_dev_to_put); |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 434 | mutex_unlock(®ister_mutex); |
Takashi Iwai | a393318 | 2019-10-30 22:42:57 +0100 | [diff] [blame] | 435 | /* put_device() is called after unlock for avoiding deadlock */ |
| 436 | if (card_dev_to_put) |
| 437 | put_device(card_dev_to_put); |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 438 | return err; |
| 439 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 440 | EXPORT_SYMBOL(snd_timer_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Takashi Iwai | fdcb576 | 2018-05-16 23:45:33 +0200 | [diff] [blame] | 442 | static unsigned long snd_timer_hw_resolution(struct snd_timer *timer) |
| 443 | { |
| 444 | if (timer->hw.c_resolution) |
| 445 | return timer->hw.c_resolution(timer); |
| 446 | else |
| 447 | return timer->hw.resolution; |
| 448 | } |
| 449 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 450 | unsigned long snd_timer_resolution(struct snd_timer_instance *timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 452 | struct snd_timer * timer; |
Takashi Iwai | 9d4d207 | 2018-05-16 23:52:42 +0200 | [diff] [blame] | 453 | unsigned long ret = 0; |
| 454 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | if (timeri == NULL) |
| 457 | return 0; |
Markus Elfring | dd1f7ab | 2017-08-23 09:45:06 +0200 | [diff] [blame] | 458 | timer = timeri->timer; |
Takashi Iwai | 9d4d207 | 2018-05-16 23:52:42 +0200 | [diff] [blame] | 459 | if (timer) { |
| 460 | spin_lock_irqsave(&timer->lock, flags); |
| 461 | ret = snd_timer_hw_resolution(timer); |
| 462 | spin_unlock_irqrestore(&timer->lock, flags); |
| 463 | } |
| 464 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 466 | EXPORT_SYMBOL(snd_timer_resolution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 468 | static void snd_timer_notify1(struct snd_timer_instance *ti, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Takashi Iwai | 9d4d207 | 2018-05-16 23:52:42 +0200 | [diff] [blame] | 470 | struct snd_timer *timer = ti->timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | unsigned long resolution = 0; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 472 | struct snd_timer_instance *ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | struct timespec tstamp; |
| 474 | |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 475 | if (timer_tstamp_monotonic) |
Thomas Gleixner | 26204e0 | 2014-06-11 23:59:14 +0000 | [diff] [blame] | 476 | ktime_get_ts(&tstamp); |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 477 | else |
| 478 | getnstimeofday(&tstamp); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 479 | if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START || |
| 480 | event > SNDRV_TIMER_EVENT_PAUSE)) |
| 481 | return; |
Takashi Iwai | 9d4d207 | 2018-05-16 23:52:42 +0200 | [diff] [blame] | 482 | if (timer && |
| 483 | (event == SNDRV_TIMER_EVENT_START || |
| 484 | event == SNDRV_TIMER_EVENT_CONTINUE)) |
| 485 | resolution = snd_timer_hw_resolution(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | if (ti->ccallback) |
Jaroslav Kysela | b30477d5 | 2010-03-03 11:05:55 +0100 | [diff] [blame] | 487 | ti->ccallback(ti, event, &tstamp, resolution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | if (ti->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 489 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | if (timer == NULL) |
| 491 | return; |
| 492 | if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 493 | return; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 494 | list_for_each_entry(ts, &ti->slave_active_head, active_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (ts->ccallback) |
Takashi Iwai | 117159f | 2016-02-08 17:36:25 +0100 | [diff] [blame] | 496 | ts->ccallback(ts, event + 100, &tstamp, resolution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 499 | /* start/continue a master timer */ |
| 500 | static int snd_timer_start1(struct snd_timer_instance *timeri, |
| 501 | bool start, unsigned long ticks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 503 | struct snd_timer *timer; |
| 504 | int result; |
| 505 | unsigned long flags; |
| 506 | |
| 507 | timer = timeri->timer; |
| 508 | if (!timer) |
| 509 | return -EINVAL; |
| 510 | |
| 511 | spin_lock_irqsave(&timer->lock, flags); |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 512 | if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) { |
| 513 | result = -EINVAL; |
| 514 | goto unlock; |
| 515 | } |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 516 | if (timer->card && timer->card->shutdown) { |
| 517 | result = -ENODEV; |
| 518 | goto unlock; |
| 519 | } |
| 520 | if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING | |
| 521 | SNDRV_TIMER_IFLG_START)) { |
| 522 | result = -EBUSY; |
| 523 | goto unlock; |
| 524 | } |
| 525 | |
| 526 | if (start) |
| 527 | timeri->ticks = timeri->cticks = ticks; |
| 528 | else if (!timeri->cticks) |
| 529 | timeri->cticks = 1; |
| 530 | timeri->pticks = 0; |
| 531 | |
Nicolas Kaiser | 5b7c757 | 2011-03-16 17:10:11 +0100 | [diff] [blame] | 532 | list_move_tail(&timeri->active_list, &timer->active_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (timer->running) { |
| 534 | if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 535 | goto __start_now; |
| 536 | timer->flags |= SNDRV_TIMER_FLG_RESCHED; |
| 537 | timeri->flags |= SNDRV_TIMER_IFLG_START; |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 538 | result = 1; /* delayed start */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } else { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 540 | if (start) |
| 541 | timer->sticks = ticks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | timer->hw.start(timer); |
| 543 | __start_now: |
| 544 | timer->running++; |
| 545 | timeri->flags |= SNDRV_TIMER_IFLG_RUNNING; |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 546 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 548 | snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START : |
| 549 | SNDRV_TIMER_EVENT_CONTINUE); |
| 550 | unlock: |
| 551 | spin_unlock_irqrestore(&timer->lock, flags); |
| 552 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 555 | /* start/continue a slave timer */ |
| 556 | static int snd_timer_start_slave(struct snd_timer_instance *timeri, |
| 557 | bool start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
| 559 | unsigned long flags; |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 560 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | spin_lock_irqsave(&slave_active_lock, flags); |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 563 | if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) { |
| 564 | err = -EINVAL; |
| 565 | goto unlock; |
| 566 | } |
Takashi Iwai | f784beb | 2016-01-30 23:09:08 +0100 | [diff] [blame] | 567 | if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) { |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 568 | err = -EBUSY; |
| 569 | goto unlock; |
Takashi Iwai | f784beb | 2016-01-30 23:09:08 +0100 | [diff] [blame] | 570 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | timeri->flags |= SNDRV_TIMER_IFLG_RUNNING; |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 572 | if (timeri->master && timeri->timer) { |
| 573 | spin_lock(&timeri->timer->lock); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 574 | list_add_tail(&timeri->active_list, |
| 575 | &timeri->master->slave_active_head); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 576 | snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START : |
| 577 | SNDRV_TIMER_EVENT_CONTINUE); |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 578 | spin_unlock(&timeri->timer->lock); |
| 579 | } |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 580 | err = 1; /* delayed start */ |
| 581 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | spin_unlock_irqrestore(&slave_active_lock, flags); |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 583 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 586 | /* stop/pause a master timer */ |
| 587 | static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 589 | struct snd_timer *timer; |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 590 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | unsigned long flags; |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | timer = timeri->timer; |
| 594 | if (!timer) |
| 595 | return -EINVAL; |
| 596 | spin_lock_irqsave(&timer->lock, flags); |
Takashi Iwai | f784beb | 2016-01-30 23:09:08 +0100 | [diff] [blame] | 597 | if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING | |
| 598 | SNDRV_TIMER_IFLG_START))) { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 599 | result = -EBUSY; |
| 600 | goto unlock; |
Takashi Iwai | f784beb | 2016-01-30 23:09:08 +0100 | [diff] [blame] | 601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | list_del_init(&timeri->ack_list); |
| 603 | list_del_init(&timeri->active_list); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 604 | if (timer->card && timer->card->shutdown) |
| 605 | goto unlock; |
| 606 | if (stop) { |
| 607 | timeri->cticks = timeri->ticks; |
| 608 | timeri->pticks = 0; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) && |
| 611 | !(--timer->running)) { |
| 612 | timer->hw.stop(timer); |
| 613 | if (timer->flags & SNDRV_TIMER_FLG_RESCHED) { |
| 614 | timer->flags &= ~SNDRV_TIMER_FLG_RESCHED; |
| 615 | snd_timer_reschedule(timer, 0); |
| 616 | if (timer->flags & SNDRV_TIMER_FLG_CHANGE) { |
| 617 | timer->flags &= ~SNDRV_TIMER_FLG_CHANGE; |
| 618 | timer->hw.start(timer); |
| 619 | } |
| 620 | } |
| 621 | } |
Takashi Iwai | c3b1681 | 2016-01-14 17:01:46 +0100 | [diff] [blame] | 622 | timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START); |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 623 | if (stop) |
| 624 | timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED; |
| 625 | else |
| 626 | timeri->flags |= SNDRV_TIMER_IFLG_PAUSED; |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 627 | snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP : |
Ben Hutchings | 3ae1809 | 2018-05-17 22:34:39 +0100 | [diff] [blame] | 628 | SNDRV_TIMER_EVENT_PAUSE); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 629 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | spin_unlock_irqrestore(&timer->lock, flags); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 631 | return result; |
| 632 | } |
| 633 | |
| 634 | /* stop/pause a slave timer */ |
| 635 | static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop) |
| 636 | { |
| 637 | unsigned long flags; |
| 638 | |
| 639 | spin_lock_irqsave(&slave_active_lock, flags); |
| 640 | if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) { |
| 641 | spin_unlock_irqrestore(&slave_active_lock, flags); |
| 642 | return -EBUSY; |
| 643 | } |
| 644 | timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING; |
| 645 | if (timeri->timer) { |
| 646 | spin_lock(&timeri->timer->lock); |
| 647 | list_del_init(&timeri->ack_list); |
| 648 | list_del_init(&timeri->active_list); |
| 649 | snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP : |
Ben Hutchings | 3ae1809 | 2018-05-17 22:34:39 +0100 | [diff] [blame] | 650 | SNDRV_TIMER_EVENT_PAUSE); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 651 | spin_unlock(&timeri->timer->lock); |
| 652 | } |
| 653 | spin_unlock_irqrestore(&slave_active_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | /* |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 658 | * start the timer instance |
| 659 | */ |
| 660 | int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks) |
| 661 | { |
| 662 | if (timeri == NULL || ticks < 1) |
| 663 | return -EINVAL; |
| 664 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 665 | return snd_timer_start_slave(timeri, true); |
| 666 | else |
| 667 | return snd_timer_start1(timeri, true, ticks); |
| 668 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 669 | EXPORT_SYMBOL(snd_timer_start); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 670 | |
| 671 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | * stop the timer instance. |
| 673 | * |
| 674 | * do not call this from the timer callback! |
| 675 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 676 | int snd_timer_stop(struct snd_timer_instance *timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 678 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 679 | return snd_timer_stop_slave(timeri, true); |
| 680 | else |
| 681 | return snd_timer_stop1(timeri, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 683 | EXPORT_SYMBOL(snd_timer_stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
| 685 | /* |
| 686 | * start again.. the tick is kept. |
| 687 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 688 | int snd_timer_continue(struct snd_timer_instance *timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 690 | /* timer can continue only after pause */ |
| 691 | if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED)) |
| 692 | return -EINVAL; |
| 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 695 | return snd_timer_start_slave(timeri, false); |
| 696 | else |
| 697 | return snd_timer_start1(timeri, false, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 699 | EXPORT_SYMBOL(snd_timer_continue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | * pause.. remember the ticks left |
| 703 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 704 | int snd_timer_pause(struct snd_timer_instance * timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 706 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 707 | return snd_timer_stop_slave(timeri, false); |
| 708 | else |
| 709 | return snd_timer_stop1(timeri, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 711 | EXPORT_SYMBOL(snd_timer_pause); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * reschedule the timer |
| 715 | * |
| 716 | * start pending instances and check the scheduling ticks. |
| 717 | * when the scheduling ticks is changed set CHANGE flag to reprogram the timer. |
| 718 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 719 | static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 721 | struct snd_timer_instance *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | unsigned long ticks = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 724 | list_for_each_entry(ti, &timer->active_list_head, active_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | if (ti->flags & SNDRV_TIMER_IFLG_START) { |
| 726 | ti->flags &= ~SNDRV_TIMER_IFLG_START; |
| 727 | ti->flags |= SNDRV_TIMER_IFLG_RUNNING; |
| 728 | timer->running++; |
| 729 | } |
| 730 | if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) { |
| 731 | if (ticks > ti->cticks) |
| 732 | ticks = ti->cticks; |
| 733 | } |
| 734 | } |
| 735 | if (ticks == ~0UL) { |
| 736 | timer->flags &= ~SNDRV_TIMER_FLG_RESCHED; |
| 737 | return; |
| 738 | } |
| 739 | if (ticks > timer->hw.ticks) |
| 740 | ticks = timer->hw.ticks; |
| 741 | if (ticks_left != ticks) |
| 742 | timer->flags |= SNDRV_TIMER_FLG_CHANGE; |
| 743 | timer->sticks = ticks; |
| 744 | } |
| 745 | |
Takashi Iwai | 8748b85 | 2019-03-27 16:42:51 +0100 | [diff] [blame] | 746 | /* call callbacks in timer ack list */ |
| 747 | static void snd_timer_process_callbacks(struct snd_timer *timer, |
| 748 | struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 750 | struct snd_timer_instance *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | unsigned long resolution, ticks; |
| 752 | |
Takashi Iwai | 8748b85 | 2019-03-27 16:42:51 +0100 | [diff] [blame] | 753 | while (!list_empty(head)) { |
| 754 | ti = list_first_entry(head, struct snd_timer_instance, |
| 755 | ack_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Takashi Iwai | df55531 | 2019-04-09 12:25:32 +0200 | [diff] [blame] | 757 | /* remove from ack_list and make empty */ |
| 758 | list_del_init(&ti->ack_list); |
| 759 | |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 760 | if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) { |
| 761 | ticks = ti->pticks; |
| 762 | ti->pticks = 0; |
| 763 | resolution = ti->resolution; |
Takashi Iwai | df55531 | 2019-04-09 12:25:32 +0200 | [diff] [blame] | 764 | ti->flags |= SNDRV_TIMER_IFLG_CALLBACK; |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 765 | spin_unlock(&timer->lock); |
| 766 | if (ti->callback) |
| 767 | ti->callback(ti, resolution, ticks); |
| 768 | spin_lock(&timer->lock); |
Takashi Iwai | df55531 | 2019-04-09 12:25:32 +0200 | [diff] [blame] | 769 | ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK; |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
Takashi Iwai | 8748b85 | 2019-03-27 16:42:51 +0100 | [diff] [blame] | 772 | } |
| 773 | |
Takashi Iwai | 7bb4a8a | 2019-03-27 16:51:58 +0100 | [diff] [blame] | 774 | /* clear pending instances from ack list */ |
| 775 | static void snd_timer_clear_callbacks(struct snd_timer *timer, |
| 776 | struct list_head *head) |
| 777 | { |
| 778 | unsigned long flags; |
| 779 | |
| 780 | spin_lock_irqsave(&timer->lock, flags); |
| 781 | while (!list_empty(head)) |
| 782 | list_del_init(head->next); |
| 783 | spin_unlock_irqrestore(&timer->lock, flags); |
| 784 | } |
| 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | /* |
| 787 | * timer tasklet |
| 788 | * |
| 789 | */ |
| 790 | static void snd_timer_tasklet(unsigned long arg) |
| 791 | { |
| 792 | struct snd_timer *timer = (struct snd_timer *) arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | unsigned long flags; |
| 794 | |
Takashi Iwai | 7bb4a8a | 2019-03-27 16:51:58 +0100 | [diff] [blame] | 795 | if (timer->card && timer->card->shutdown) { |
| 796 | snd_timer_clear_callbacks(timer, &timer->sack_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | return; |
Takashi Iwai | 7bb4a8a | 2019-03-27 16:51:58 +0100 | [diff] [blame] | 798 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
| 800 | spin_lock_irqsave(&timer->lock, flags); |
Takashi Iwai | 8748b85 | 2019-03-27 16:42:51 +0100 | [diff] [blame] | 801 | snd_timer_process_callbacks(timer, &timer->sack_list_head); |
Takashi Iwai | 2999ff5 | 2006-07-05 17:16:58 +0200 | [diff] [blame] | 802 | spin_unlock_irqrestore(&timer->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
| 806 | * timer interrupt |
| 807 | * |
| 808 | * ticks_left is usually equal to timer->sticks. |
| 809 | * |
| 810 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 811 | void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 813 | struct snd_timer_instance *ti, *ts, *tmp; |
Takashi Iwai | 8748b85 | 2019-03-27 16:42:51 +0100 | [diff] [blame] | 814 | unsigned long resolution; |
| 815 | struct list_head *ack_list_head; |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 816 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | int use_tasklet = 0; |
| 818 | |
| 819 | if (timer == NULL) |
| 820 | return; |
| 821 | |
Takashi Iwai | 7bb4a8a | 2019-03-27 16:51:58 +0100 | [diff] [blame] | 822 | if (timer->card && timer->card->shutdown) { |
| 823 | snd_timer_clear_callbacks(timer, &timer->ack_list_head); |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 824 | return; |
Takashi Iwai | 7bb4a8a | 2019-03-27 16:51:58 +0100 | [diff] [blame] | 825 | } |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 826 | |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 827 | spin_lock_irqsave(&timer->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
| 829 | /* remember the current resolution */ |
Takashi Iwai | fdcb576 | 2018-05-16 23:45:33 +0200 | [diff] [blame] | 830 | resolution = snd_timer_hw_resolution(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
| 832 | /* loop for all active instances |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 833 | * Here we cannot use list_for_each_entry because the active_list of a |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 834 | * processed instance is relinked to done_list_head before the callback |
| 835 | * is called. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | */ |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 837 | list_for_each_entry_safe(ti, tmp, &timer->active_list_head, |
| 838 | active_list) { |
Takashi Iwai | fe1b26c | 2019-03-27 17:02:40 +0100 | [diff] [blame] | 839 | if (ti->flags & SNDRV_TIMER_IFLG_DEAD) |
| 840 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING)) |
| 842 | continue; |
| 843 | ti->pticks += ticks_left; |
| 844 | ti->resolution = resolution; |
| 845 | if (ti->cticks < ticks_left) |
| 846 | ti->cticks = 0; |
| 847 | else |
| 848 | ti->cticks -= ticks_left; |
| 849 | if (ti->cticks) /* not expired */ |
| 850 | continue; |
| 851 | if (ti->flags & SNDRV_TIMER_IFLG_AUTO) { |
| 852 | ti->cticks = ti->ticks; |
| 853 | } else { |
| 854 | ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING; |
Takashi Iwai | 094fd3b | 2016-02-04 17:06:13 +0100 | [diff] [blame] | 855 | --timer->running; |
| 856 | list_del_init(&ti->active_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 858 | if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) || |
| 859 | (ti->flags & SNDRV_TIMER_IFLG_FAST)) |
| 860 | ack_list_head = &timer->ack_list_head; |
| 861 | else |
| 862 | ack_list_head = &timer->sack_list_head; |
| 863 | if (list_empty(&ti->ack_list)) |
| 864 | list_add_tail(&ti->ack_list, ack_list_head); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 865 | list_for_each_entry(ts, &ti->slave_active_head, active_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | ts->pticks = ti->pticks; |
| 867 | ts->resolution = resolution; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 868 | if (list_empty(&ts->ack_list)) |
| 869 | list_add_tail(&ts->ack_list, ack_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | if (timer->flags & SNDRV_TIMER_FLG_RESCHED) |
Clemens Ladisch | cd93fe4 | 2006-07-17 16:53:57 +0200 | [diff] [blame] | 873 | snd_timer_reschedule(timer, timer->sticks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | if (timer->running) { |
| 875 | if (timer->hw.flags & SNDRV_TIMER_HW_STOP) { |
| 876 | timer->hw.stop(timer); |
| 877 | timer->flags |= SNDRV_TIMER_FLG_CHANGE; |
| 878 | } |
| 879 | if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) || |
| 880 | (timer->flags & SNDRV_TIMER_FLG_CHANGE)) { |
| 881 | /* restart timer */ |
| 882 | timer->flags &= ~SNDRV_TIMER_FLG_CHANGE; |
| 883 | timer->hw.start(timer); |
| 884 | } |
| 885 | } else { |
| 886 | timer->hw.stop(timer); |
| 887 | } |
| 888 | |
| 889 | /* now process all fast callbacks */ |
Takashi Iwai | 8748b85 | 2019-03-27 16:42:51 +0100 | [diff] [blame] | 890 | snd_timer_process_callbacks(timer, &timer->ack_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
| 892 | /* do we have any slow callbacks? */ |
| 893 | use_tasklet = !list_empty(&timer->sack_list_head); |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 894 | spin_unlock_irqrestore(&timer->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
| 896 | if (use_tasklet) |
Takashi Iwai | 1f04128 | 2008-12-18 12:17:55 +0100 | [diff] [blame] | 897 | tasklet_schedule(&timer->task_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 899 | EXPORT_SYMBOL(snd_timer_interrupt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
| 901 | /* |
| 902 | |
| 903 | */ |
| 904 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 905 | int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid, |
| 906 | struct snd_timer **rtimer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 908 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | int err; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 910 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | .dev_free = snd_timer_dev_free, |
| 912 | .dev_register = snd_timer_dev_register, |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 913 | .dev_disconnect = snd_timer_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | }; |
| 915 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 916 | if (snd_BUG_ON(!tid)) |
| 917 | return -EINVAL; |
Srikanth K H | d10ee9c | 2018-07-20 11:13:51 +0530 | [diff] [blame] | 918 | if (tid->dev_class == SNDRV_TIMER_CLASS_CARD || |
| 919 | tid->dev_class == SNDRV_TIMER_CLASS_PCM) { |
| 920 | if (WARN_ON(!card)) |
| 921 | return -EINVAL; |
| 922 | } |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 923 | if (rtimer) |
| 924 | *rtimer = NULL; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 925 | timer = kzalloc(sizeof(*timer), GFP_KERNEL); |
Takashi Iwai | ec0e993 | 2015-03-10 15:42:14 +0100 | [diff] [blame] | 926 | if (!timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return -ENOMEM; |
| 928 | timer->tmr_class = tid->dev_class; |
| 929 | timer->card = card; |
| 930 | timer->tmr_device = tid->device; |
| 931 | timer->tmr_subdevice = tid->subdevice; |
| 932 | if (id) |
| 933 | strlcpy(timer->id, id, sizeof(timer->id)); |
Vegard Nossum | 6b760bb | 2016-08-29 00:33:50 +0200 | [diff] [blame] | 934 | timer->sticks = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | INIT_LIST_HEAD(&timer->device_list); |
| 936 | INIT_LIST_HEAD(&timer->open_list_head); |
| 937 | INIT_LIST_HEAD(&timer->active_list_head); |
| 938 | INIT_LIST_HEAD(&timer->ack_list_head); |
| 939 | INIT_LIST_HEAD(&timer->sack_list_head); |
| 940 | spin_lock_init(&timer->lock); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 941 | tasklet_init(&timer->task_queue, snd_timer_tasklet, |
| 942 | (unsigned long)timer); |
Takashi Iwai | 9b7d869 | 2017-11-05 10:07:43 +0100 | [diff] [blame] | 943 | timer->max_instances = 1000; /* default limit per timer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | if (card != NULL) { |
Clemens Ladisch | de24214 | 2005-10-12 17:12:31 +0200 | [diff] [blame] | 945 | timer->module = card->module; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 946 | err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops); |
| 947 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | snd_timer_free(timer); |
| 949 | return err; |
| 950 | } |
| 951 | } |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 952 | if (rtimer) |
| 953 | *rtimer = timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | return 0; |
| 955 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 956 | EXPORT_SYMBOL(snd_timer_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 958 | static int snd_timer_free(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 960 | if (!timer) |
| 961 | return 0; |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 962 | |
| 963 | mutex_lock(®ister_mutex); |
| 964 | if (! list_empty(&timer->open_list_head)) { |
| 965 | struct list_head *p, *n; |
| 966 | struct snd_timer_instance *ti; |
Takashi Iwai | cf74dcf | 2014-02-04 18:22:39 +0100 | [diff] [blame] | 967 | pr_warn("ALSA: timer %p is busy?\n", timer); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 968 | list_for_each_safe(p, n, &timer->open_list_head) { |
| 969 | list_del_init(p); |
| 970 | ti = list_entry(p, struct snd_timer_instance, open_list); |
| 971 | ti->timer = NULL; |
| 972 | } |
| 973 | } |
| 974 | list_del(&timer->device_list); |
| 975 | mutex_unlock(®ister_mutex); |
| 976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | if (timer->private_free) |
| 978 | timer->private_free(timer); |
| 979 | kfree(timer); |
| 980 | return 0; |
| 981 | } |
| 982 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 983 | static int snd_timer_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 985 | struct snd_timer *timer = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | return snd_timer_free(timer); |
| 987 | } |
| 988 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 989 | static int snd_timer_dev_register(struct snd_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 991 | struct snd_timer *timer = dev->device_data; |
| 992 | struct snd_timer *timer1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 994 | if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop)) |
| 995 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) && |
| 997 | !timer->hw.resolution && timer->hw.c_resolution == NULL) |
| 998 | return -EINVAL; |
| 999 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1000 | mutex_lock(®ister_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1001 | list_for_each_entry(timer1, &snd_timer_list, device_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | if (timer1->tmr_class > timer->tmr_class) |
| 1003 | break; |
| 1004 | if (timer1->tmr_class < timer->tmr_class) |
| 1005 | continue; |
| 1006 | if (timer1->card && timer->card) { |
| 1007 | if (timer1->card->number > timer->card->number) |
| 1008 | break; |
| 1009 | if (timer1->card->number < timer->card->number) |
| 1010 | continue; |
| 1011 | } |
| 1012 | if (timer1->tmr_device > timer->tmr_device) |
| 1013 | break; |
| 1014 | if (timer1->tmr_device < timer->tmr_device) |
| 1015 | continue; |
| 1016 | if (timer1->tmr_subdevice > timer->tmr_subdevice) |
| 1017 | break; |
| 1018 | if (timer1->tmr_subdevice < timer->tmr_subdevice) |
| 1019 | continue; |
| 1020 | /* conflicts.. */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1021 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | return -EBUSY; |
| 1023 | } |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1024 | list_add_tail(&timer->device_list, &timer1->device_list); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1025 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | return 0; |
| 1027 | } |
| 1028 | |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 1029 | static int snd_timer_dev_disconnect(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1031 | struct snd_timer *timer = device->device_data; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 1032 | struct snd_timer_instance *ti; |
| 1033 | |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 1034 | mutex_lock(®ister_mutex); |
| 1035 | list_del_init(&timer->device_list); |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 1036 | /* wake up pending sleepers */ |
| 1037 | list_for_each_entry(ti, &timer->open_list_head, open_list) { |
Takashi Iwai | 40ed944 | 2016-01-21 17:43:08 +0100 | [diff] [blame] | 1038 | if (ti->disconnect) |
| 1039 | ti->disconnect(ti); |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 1040 | } |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 1041 | mutex_unlock(®ister_mutex); |
| 1042 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1045 | void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { |
| 1047 | unsigned long flags; |
| 1048 | unsigned long resolution = 0; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1049 | struct snd_timer_instance *ti, *ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 1051 | if (timer->card && timer->card->shutdown) |
| 1052 | return; |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 1053 | if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)) |
| 1054 | return; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1055 | if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART || |
| 1056 | event > SNDRV_TIMER_EVENT_MRESUME)) |
| 1057 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | spin_lock_irqsave(&timer->lock, flags); |
Jaroslav Kysela | a501dfa | 2005-08-16 11:09:05 +0200 | [diff] [blame] | 1059 | if (event == SNDRV_TIMER_EVENT_MSTART || |
| 1060 | event == SNDRV_TIMER_EVENT_MCONTINUE || |
Takashi Iwai | fdcb576 | 2018-05-16 23:45:33 +0200 | [diff] [blame] | 1061 | event == SNDRV_TIMER_EVENT_MRESUME) |
| 1062 | resolution = snd_timer_hw_resolution(timer); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1063 | list_for_each_entry(ti, &timer->active_list_head, active_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | if (ti->ccallback) |
| 1065 | ti->ccallback(ti, event, tstamp, resolution); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1066 | list_for_each_entry(ts, &ti->slave_active_head, active_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | if (ts->ccallback) |
| 1068 | ts->ccallback(ts, event, tstamp, resolution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | } |
| 1070 | spin_unlock_irqrestore(&timer->lock, flags); |
| 1071 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 1072 | EXPORT_SYMBOL(snd_timer_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * exported functions for global timers |
| 1076 | */ |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1077 | int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1079 | struct snd_timer_id tid; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL; |
| 1082 | tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; |
| 1083 | tid.card = -1; |
| 1084 | tid.device = device; |
| 1085 | tid.subdevice = 0; |
| 1086 | return snd_timer_new(NULL, id, &tid, rtimer); |
| 1087 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 1088 | EXPORT_SYMBOL(snd_timer_global_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1090 | int snd_timer_global_free(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
| 1092 | return snd_timer_free(timer); |
| 1093 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 1094 | EXPORT_SYMBOL(snd_timer_global_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1096 | int snd_timer_global_register(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1098 | struct snd_device dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | |
| 1100 | memset(&dev, 0, sizeof(dev)); |
| 1101 | dev.device_data = timer; |
| 1102 | return snd_timer_dev_register(&dev); |
| 1103 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 1104 | EXPORT_SYMBOL(snd_timer_global_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | * System timer |
| 1108 | */ |
| 1109 | |
| 1110 | struct snd_timer_system_private { |
| 1111 | struct timer_list tlist; |
Kees Cook | 38e9a80 | 2017-10-04 17:53:33 -0700 | [diff] [blame] | 1112 | struct snd_timer *snd_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | unsigned long last_expires; |
| 1114 | unsigned long last_jiffies; |
| 1115 | unsigned long correction; |
| 1116 | }; |
| 1117 | |
Kees Cook | 38e9a80 | 2017-10-04 17:53:33 -0700 | [diff] [blame] | 1118 | static void snd_timer_s_function(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | { |
Kees Cook | 38e9a80 | 2017-10-04 17:53:33 -0700 | [diff] [blame] | 1120 | struct snd_timer_system_private *priv = from_timer(priv, t, |
| 1121 | tlist); |
| 1122 | struct snd_timer *timer = priv->snd_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | unsigned long jiff = jiffies; |
| 1124 | if (time_after(jiff, priv->last_expires)) |
Clemens Ladisch | 6ed5eff | 2006-07-17 16:51:37 +0200 | [diff] [blame] | 1125 | priv->correction += (long)jiff - (long)priv->last_expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies); |
| 1127 | } |
| 1128 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1129 | static int snd_timer_s_start(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
| 1131 | struct snd_timer_system_private *priv; |
| 1132 | unsigned long njiff; |
| 1133 | |
| 1134 | priv = (struct snd_timer_system_private *) timer->private_data; |
| 1135 | njiff = (priv->last_jiffies = jiffies); |
| 1136 | if (priv->correction > timer->sticks - 1) { |
| 1137 | priv->correction -= timer->sticks - 1; |
| 1138 | njiff++; |
| 1139 | } else { |
| 1140 | njiff += timer->sticks - priv->correction; |
Clemens Ladisch | 17f48ec | 2006-07-17 16:50:56 +0200 | [diff] [blame] | 1141 | priv->correction = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | } |
Takashi Iwai | 4a07083 | 2016-04-01 12:28:16 +0200 | [diff] [blame] | 1143 | priv->last_expires = njiff; |
| 1144 | mod_timer(&priv->tlist, njiff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | return 0; |
| 1146 | } |
| 1147 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1148 | static int snd_timer_s_stop(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | { |
| 1150 | struct snd_timer_system_private *priv; |
| 1151 | unsigned long jiff; |
| 1152 | |
| 1153 | priv = (struct snd_timer_system_private *) timer->private_data; |
| 1154 | del_timer(&priv->tlist); |
| 1155 | jiff = jiffies; |
| 1156 | if (time_before(jiff, priv->last_expires)) |
| 1157 | timer->sticks = priv->last_expires - jiff; |
| 1158 | else |
| 1159 | timer->sticks = 1; |
Clemens Ladisch | de2696d | 2006-07-17 16:52:09 +0200 | [diff] [blame] | 1160 | priv->correction = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | return 0; |
| 1162 | } |
| 1163 | |
Takashi Iwai | f146357 | 2016-02-02 14:14:10 +0100 | [diff] [blame] | 1164 | static int snd_timer_s_close(struct snd_timer *timer) |
| 1165 | { |
| 1166 | struct snd_timer_system_private *priv; |
| 1167 | |
| 1168 | priv = (struct snd_timer_system_private *)timer->private_data; |
| 1169 | del_timer_sync(&priv->tlist); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1173 | static struct snd_timer_hardware snd_timer_system = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | { |
| 1175 | .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET, |
| 1176 | .resolution = 1000000000L / HZ, |
| 1177 | .ticks = 10000000L, |
Takashi Iwai | f146357 | 2016-02-02 14:14:10 +0100 | [diff] [blame] | 1178 | .close = snd_timer_s_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | .start = snd_timer_s_start, |
| 1180 | .stop = snd_timer_s_stop |
| 1181 | }; |
| 1182 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1183 | static void snd_timer_free_system(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
| 1185 | kfree(timer->private_data); |
| 1186 | } |
| 1187 | |
| 1188 | static int snd_timer_register_system(void) |
| 1189 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1190 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | struct snd_timer_system_private *priv; |
| 1192 | int err; |
| 1193 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1194 | err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer); |
| 1195 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | return err; |
| 1197 | strcpy(timer->name, "system timer"); |
| 1198 | timer->hw = snd_timer_system; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1199 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | if (priv == NULL) { |
| 1201 | snd_timer_free(timer); |
| 1202 | return -ENOMEM; |
| 1203 | } |
Kees Cook | 38e9a80 | 2017-10-04 17:53:33 -0700 | [diff] [blame] | 1204 | priv->snd_timer = timer; |
| 1205 | timer_setup(&priv->tlist, snd_timer_s_function, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | timer->private_data = priv; |
| 1207 | timer->private_free = snd_timer_free_system; |
| 1208 | return snd_timer_global_register(timer); |
| 1209 | } |
| 1210 | |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 1211 | #ifdef CONFIG_SND_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | /* |
| 1213 | * Info interface |
| 1214 | */ |
| 1215 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1216 | static void snd_timer_proc_read(struct snd_info_entry *entry, |
| 1217 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1219 | struct snd_timer *timer; |
| 1220 | struct snd_timer_instance *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1222 | mutex_lock(®ister_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1223 | list_for_each_entry(timer, &snd_timer_list, device_list) { |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 1224 | if (timer->card && timer->card->shutdown) |
| 1225 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | switch (timer->tmr_class) { |
| 1227 | case SNDRV_TIMER_CLASS_GLOBAL: |
| 1228 | snd_iprintf(buffer, "G%i: ", timer->tmr_device); |
| 1229 | break; |
| 1230 | case SNDRV_TIMER_CLASS_CARD: |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1231 | snd_iprintf(buffer, "C%i-%i: ", |
| 1232 | timer->card->number, timer->tmr_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | break; |
| 1234 | case SNDRV_TIMER_CLASS_PCM: |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1235 | snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number, |
| 1236 | timer->tmr_device, timer->tmr_subdevice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | break; |
| 1238 | default: |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1239 | snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class, |
| 1240 | timer->card ? timer->card->number : -1, |
| 1241 | timer->tmr_device, timer->tmr_subdevice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
| 1243 | snd_iprintf(buffer, "%s :", timer->name); |
| 1244 | if (timer->hw.resolution) |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1245 | snd_iprintf(buffer, " %lu.%03luus (%lu ticks)", |
| 1246 | timer->hw.resolution / 1000, |
| 1247 | timer->hw.resolution % 1000, |
| 1248 | timer->hw.ticks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 1250 | snd_iprintf(buffer, " SLAVE"); |
| 1251 | snd_iprintf(buffer, "\n"); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1252 | list_for_each_entry(ti, &timer->open_list_head, open_list) |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1253 | snd_iprintf(buffer, " Client %s : %s\n", |
| 1254 | ti->owner ? ti->owner : "unknown", |
| 1255 | ti->flags & (SNDRV_TIMER_IFLG_START | |
| 1256 | SNDRV_TIMER_IFLG_RUNNING) |
| 1257 | ? "running" : "stopped"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1259 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 1262 | static struct snd_info_entry *snd_timer_proc_entry; |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 1263 | |
| 1264 | static void __init snd_timer_proc_init(void) |
| 1265 | { |
| 1266 | struct snd_info_entry *entry; |
| 1267 | |
| 1268 | entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL); |
| 1269 | if (entry != NULL) { |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 1270 | entry->c.text.read = snd_timer_proc_read; |
| 1271 | if (snd_info_register(entry) < 0) { |
| 1272 | snd_info_free_entry(entry); |
| 1273 | entry = NULL; |
| 1274 | } |
| 1275 | } |
| 1276 | snd_timer_proc_entry = entry; |
| 1277 | } |
| 1278 | |
| 1279 | static void __exit snd_timer_proc_done(void) |
| 1280 | { |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 1281 | snd_info_free_entry(snd_timer_proc_entry); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 1282 | } |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 1283 | #else /* !CONFIG_SND_PROC_FS */ |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 1284 | #define snd_timer_proc_init() |
| 1285 | #define snd_timer_proc_done() |
| 1286 | #endif |
| 1287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | /* |
| 1289 | * USER SPACE interface |
| 1290 | */ |
| 1291 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1292 | static void snd_timer_user_interrupt(struct snd_timer_instance *timeri, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | unsigned long resolution, |
| 1294 | unsigned long ticks) |
| 1295 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1296 | struct snd_timer_user *tu = timeri->callback_data; |
| 1297 | struct snd_timer_read *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | int prev; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | spin_lock(&tu->qlock); |
| 1301 | if (tu->qused > 0) { |
| 1302 | prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1; |
| 1303 | r = &tu->queue[prev]; |
| 1304 | if (r->resolution == resolution) { |
| 1305 | r->ticks += ticks; |
| 1306 | goto __wake; |
| 1307 | } |
| 1308 | } |
| 1309 | if (tu->qused >= tu->queue_size) { |
| 1310 | tu->overrun++; |
| 1311 | } else { |
| 1312 | r = &tu->queue[tu->qtail++]; |
| 1313 | tu->qtail %= tu->queue_size; |
| 1314 | r->resolution = resolution; |
| 1315 | r->ticks = ticks; |
| 1316 | tu->qused++; |
| 1317 | } |
| 1318 | __wake: |
| 1319 | spin_unlock(&tu->qlock); |
| 1320 | kill_fasync(&tu->fasync, SIGIO, POLL_IN); |
| 1321 | wake_up(&tu->qchange_sleep); |
| 1322 | } |
| 1323 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1324 | static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu, |
| 1325 | struct snd_timer_tread *tread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | { |
| 1327 | if (tu->qused >= tu->queue_size) { |
| 1328 | tu->overrun++; |
| 1329 | } else { |
| 1330 | memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread)); |
| 1331 | tu->qtail %= tu->queue_size; |
| 1332 | tu->qused++; |
| 1333 | } |
| 1334 | } |
| 1335 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1336 | static void snd_timer_user_ccallback(struct snd_timer_instance *timeri, |
| 1337 | int event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | struct timespec *tstamp, |
| 1339 | unsigned long resolution) |
| 1340 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1341 | struct snd_timer_user *tu = timeri->callback_data; |
| 1342 | struct snd_timer_tread r1; |
Dan Carpenter | bfe7078 | 2010-04-28 10:29:14 +0200 | [diff] [blame] | 1343 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1345 | if (event >= SNDRV_TIMER_EVENT_START && |
| 1346 | event <= SNDRV_TIMER_EVENT_PAUSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | tu->tstamp = *tstamp; |
| 1348 | if ((tu->filter & (1 << event)) == 0 || !tu->tread) |
| 1349 | return; |
Kangjie Lu | 9a47e9c | 2016-05-03 16:44:20 -0400 | [diff] [blame] | 1350 | memset(&r1, 0, sizeof(r1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | r1.event = event; |
| 1352 | r1.tstamp = *tstamp; |
| 1353 | r1.val = resolution; |
Dan Carpenter | bfe7078 | 2010-04-28 10:29:14 +0200 | [diff] [blame] | 1354 | spin_lock_irqsave(&tu->qlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | snd_timer_user_append_to_tqueue(tu, &r1); |
Dan Carpenter | bfe7078 | 2010-04-28 10:29:14 +0200 | [diff] [blame] | 1356 | spin_unlock_irqrestore(&tu->qlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | kill_fasync(&tu->fasync, SIGIO, POLL_IN); |
| 1358 | wake_up(&tu->qchange_sleep); |
| 1359 | } |
| 1360 | |
Takashi Iwai | 40ed944 | 2016-01-21 17:43:08 +0100 | [diff] [blame] | 1361 | static void snd_timer_user_disconnect(struct snd_timer_instance *timeri) |
| 1362 | { |
| 1363 | struct snd_timer_user *tu = timeri->callback_data; |
| 1364 | |
| 1365 | tu->disconnected = true; |
| 1366 | wake_up(&tu->qchange_sleep); |
| 1367 | } |
| 1368 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1369 | static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | unsigned long resolution, |
| 1371 | unsigned long ticks) |
| 1372 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1373 | struct snd_timer_user *tu = timeri->callback_data; |
| 1374 | struct snd_timer_tread *r, r1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | struct timespec tstamp; |
| 1376 | int prev, append = 0; |
| 1377 | |
Dan Carpenter | a8c006a | 2017-03-31 18:22:23 +0300 | [diff] [blame] | 1378 | memset(&r1, 0, sizeof(r1)); |
Takashi Iwai | 07799e7 | 2005-10-10 11:49:49 +0200 | [diff] [blame] | 1379 | memset(&tstamp, 0, sizeof(tstamp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | spin_lock(&tu->qlock); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1381 | if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) | |
| 1382 | (1 << SNDRV_TIMER_EVENT_TICK))) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | spin_unlock(&tu->qlock); |
| 1384 | return; |
| 1385 | } |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 1386 | if (tu->last_resolution != resolution || ticks > 0) { |
| 1387 | if (timer_tstamp_monotonic) |
Thomas Gleixner | 26204e0 | 2014-06-11 23:59:14 +0000 | [diff] [blame] | 1388 | ktime_get_ts(&tstamp); |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 1389 | else |
| 1390 | getnstimeofday(&tstamp); |
| 1391 | } |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1392 | if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) && |
| 1393 | tu->last_resolution != resolution) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | r1.event = SNDRV_TIMER_EVENT_RESOLUTION; |
| 1395 | r1.tstamp = tstamp; |
| 1396 | r1.val = resolution; |
| 1397 | snd_timer_user_append_to_tqueue(tu, &r1); |
| 1398 | tu->last_resolution = resolution; |
| 1399 | append++; |
| 1400 | } |
| 1401 | if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0) |
| 1402 | goto __wake; |
| 1403 | if (ticks == 0) |
| 1404 | goto __wake; |
| 1405 | if (tu->qused > 0) { |
| 1406 | prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1; |
| 1407 | r = &tu->tqueue[prev]; |
| 1408 | if (r->event == SNDRV_TIMER_EVENT_TICK) { |
| 1409 | r->tstamp = tstamp; |
| 1410 | r->val += ticks; |
| 1411 | append++; |
| 1412 | goto __wake; |
| 1413 | } |
| 1414 | } |
| 1415 | r1.event = SNDRV_TIMER_EVENT_TICK; |
| 1416 | r1.tstamp = tstamp; |
| 1417 | r1.val = ticks; |
| 1418 | snd_timer_user_append_to_tqueue(tu, &r1); |
| 1419 | append++; |
| 1420 | __wake: |
| 1421 | spin_unlock(&tu->qlock); |
| 1422 | if (append == 0) |
| 1423 | return; |
| 1424 | kill_fasync(&tu->fasync, SIGIO, POLL_IN); |
| 1425 | wake_up(&tu->qchange_sleep); |
| 1426 | } |
| 1427 | |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1428 | static int realloc_user_queue(struct snd_timer_user *tu, int size) |
| 1429 | { |
| 1430 | struct snd_timer_read *queue = NULL; |
| 1431 | struct snd_timer_tread *tqueue = NULL; |
| 1432 | |
| 1433 | if (tu->tread) { |
| 1434 | tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL); |
| 1435 | if (!tqueue) |
| 1436 | return -ENOMEM; |
| 1437 | } else { |
| 1438 | queue = kcalloc(size, sizeof(*queue), GFP_KERNEL); |
| 1439 | if (!queue) |
| 1440 | return -ENOMEM; |
| 1441 | } |
| 1442 | |
| 1443 | spin_lock_irq(&tu->qlock); |
| 1444 | kfree(tu->queue); |
| 1445 | kfree(tu->tqueue); |
| 1446 | tu->queue_size = size; |
| 1447 | tu->queue = queue; |
| 1448 | tu->tqueue = tqueue; |
| 1449 | tu->qhead = tu->qtail = tu->qused = 0; |
| 1450 | spin_unlock_irq(&tu->qlock); |
| 1451 | |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | static int snd_timer_user_open(struct inode *inode, struct file *file) |
| 1456 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1457 | struct snd_timer_user *tu; |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 1458 | int err; |
| 1459 | |
Kirill Smelkov | c5bf68f | 2019-03-26 23:51:19 +0300 | [diff] [blame] | 1460 | err = stream_open(inode, file); |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 1461 | if (err < 0) |
| 1462 | return err; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1463 | |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1464 | tu = kzalloc(sizeof(*tu), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | if (tu == NULL) |
| 1466 | return -ENOMEM; |
| 1467 | spin_lock_init(&tu->qlock); |
| 1468 | init_waitqueue_head(&tu->qchange_sleep); |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1469 | mutex_init(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | tu->ticks = 1; |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1471 | if (realloc_user_queue(tu, 128) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | kfree(tu); |
| 1473 | return -ENOMEM; |
| 1474 | } |
| 1475 | file->private_data = tu; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | static int snd_timer_user_release(struct inode *inode, struct file *file) |
| 1480 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1481 | struct snd_timer_user *tu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
| 1483 | if (file->private_data) { |
| 1484 | tu = file->private_data; |
| 1485 | file->private_data = NULL; |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1486 | mutex_lock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | if (tu->timeri) |
| 1488 | snd_timer_close(tu->timeri); |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1489 | mutex_unlock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | kfree(tu->queue); |
| 1491 | kfree(tu->tqueue); |
| 1492 | kfree(tu); |
| 1493 | } |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1497 | static void snd_timer_user_zero_id(struct snd_timer_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | { |
| 1499 | id->dev_class = SNDRV_TIMER_CLASS_NONE; |
| 1500 | id->dev_sclass = SNDRV_TIMER_SCLASS_NONE; |
| 1501 | id->card = -1; |
| 1502 | id->device = -1; |
| 1503 | id->subdevice = -1; |
| 1504 | } |
| 1505 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1506 | static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | { |
| 1508 | id->dev_class = timer->tmr_class; |
| 1509 | id->dev_sclass = SNDRV_TIMER_SCLASS_NONE; |
| 1510 | id->card = timer->card ? timer->card->number : -1; |
| 1511 | id->device = timer->tmr_device; |
| 1512 | id->subdevice = timer->tmr_subdevice; |
| 1513 | } |
| 1514 | |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1515 | static int snd_timer_user_next_device(struct snd_timer_id __user *_tid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1517 | struct snd_timer_id id; |
| 1518 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | struct list_head *p; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | if (copy_from_user(&id, _tid, sizeof(id))) |
| 1522 | return -EFAULT; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1523 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | if (id.dev_class < 0) { /* first item */ |
| 1525 | if (list_empty(&snd_timer_list)) |
| 1526 | snd_timer_user_zero_id(&id); |
| 1527 | else { |
Clemens Ladisch | 9dfba38 | 2005-10-12 17:14:55 +0200 | [diff] [blame] | 1528 | timer = list_entry(snd_timer_list.next, |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1529 | struct snd_timer, device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | snd_timer_user_copy_id(&id, timer); |
| 1531 | } |
| 1532 | } else { |
| 1533 | switch (id.dev_class) { |
| 1534 | case SNDRV_TIMER_CLASS_GLOBAL: |
| 1535 | id.device = id.device < 0 ? 0 : id.device + 1; |
| 1536 | list_for_each(p, &snd_timer_list) { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1537 | timer = list_entry(p, struct snd_timer, device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) { |
| 1539 | snd_timer_user_copy_id(&id, timer); |
| 1540 | break; |
| 1541 | } |
| 1542 | if (timer->tmr_device >= id.device) { |
| 1543 | snd_timer_user_copy_id(&id, timer); |
| 1544 | break; |
| 1545 | } |
| 1546 | } |
| 1547 | if (p == &snd_timer_list) |
| 1548 | snd_timer_user_zero_id(&id); |
| 1549 | break; |
| 1550 | case SNDRV_TIMER_CLASS_CARD: |
| 1551 | case SNDRV_TIMER_CLASS_PCM: |
| 1552 | if (id.card < 0) { |
| 1553 | id.card = 0; |
| 1554 | } else { |
Dan Carpenter | e8ed682 | 2017-03-31 18:21:41 +0300 | [diff] [blame] | 1555 | if (id.device < 0) { |
| 1556 | id.device = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | } else { |
Dan Carpenter | e8ed682 | 2017-03-31 18:21:41 +0300 | [diff] [blame] | 1558 | if (id.subdevice < 0) |
| 1559 | id.subdevice = 0; |
Takashi Iwai | b41f794 | 2018-06-25 11:09:11 +0200 | [diff] [blame] | 1560 | else if (id.subdevice < INT_MAX) |
Dan Carpenter | e8ed682 | 2017-03-31 18:21:41 +0300 | [diff] [blame] | 1561 | id.subdevice++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | list_for_each(p, &snd_timer_list) { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1565 | timer = list_entry(p, struct snd_timer, device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | if (timer->tmr_class > id.dev_class) { |
| 1567 | snd_timer_user_copy_id(&id, timer); |
| 1568 | break; |
| 1569 | } |
| 1570 | if (timer->tmr_class < id.dev_class) |
| 1571 | continue; |
| 1572 | if (timer->card->number > id.card) { |
| 1573 | snd_timer_user_copy_id(&id, timer); |
| 1574 | break; |
| 1575 | } |
| 1576 | if (timer->card->number < id.card) |
| 1577 | continue; |
| 1578 | if (timer->tmr_device > id.device) { |
| 1579 | snd_timer_user_copy_id(&id, timer); |
| 1580 | break; |
| 1581 | } |
| 1582 | if (timer->tmr_device < id.device) |
| 1583 | continue; |
| 1584 | if (timer->tmr_subdevice > id.subdevice) { |
| 1585 | snd_timer_user_copy_id(&id, timer); |
| 1586 | break; |
| 1587 | } |
| 1588 | if (timer->tmr_subdevice < id.subdevice) |
| 1589 | continue; |
| 1590 | snd_timer_user_copy_id(&id, timer); |
| 1591 | break; |
| 1592 | } |
| 1593 | if (p == &snd_timer_list) |
| 1594 | snd_timer_user_zero_id(&id); |
| 1595 | break; |
| 1596 | default: |
| 1597 | snd_timer_user_zero_id(&id); |
| 1598 | } |
| 1599 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1600 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | if (copy_to_user(_tid, &id, sizeof(*_tid))) |
| 1602 | return -EFAULT; |
| 1603 | return 0; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1604 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1606 | static int snd_timer_user_ginfo(struct file *file, |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1607 | struct snd_timer_ginfo __user *_ginfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1609 | struct snd_timer_ginfo *ginfo; |
| 1610 | struct snd_timer_id tid; |
| 1611 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | struct list_head *p; |
| 1613 | int err = 0; |
| 1614 | |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 1615 | ginfo = memdup_user(_ginfo, sizeof(*ginfo)); |
| 1616 | if (IS_ERR(ginfo)) |
| 1617 | return PTR_ERR(ginfo); |
| 1618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | tid = ginfo->tid; |
| 1620 | memset(ginfo, 0, sizeof(*ginfo)); |
| 1621 | ginfo->tid = tid; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1622 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | t = snd_timer_find(&tid); |
| 1624 | if (t != NULL) { |
| 1625 | ginfo->card = t->card ? t->card->number : -1; |
| 1626 | if (t->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 1627 | ginfo->flags |= SNDRV_TIMER_FLG_SLAVE; |
| 1628 | strlcpy(ginfo->id, t->id, sizeof(ginfo->id)); |
| 1629 | strlcpy(ginfo->name, t->name, sizeof(ginfo->name)); |
| 1630 | ginfo->resolution = t->hw.resolution; |
| 1631 | if (t->hw.resolution_min > 0) { |
| 1632 | ginfo->resolution_min = t->hw.resolution_min; |
| 1633 | ginfo->resolution_max = t->hw.resolution_max; |
| 1634 | } |
| 1635 | list_for_each(p, &t->open_list_head) { |
| 1636 | ginfo->clients++; |
| 1637 | } |
| 1638 | } else { |
| 1639 | err = -ENODEV; |
| 1640 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1641 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo))) |
| 1643 | err = -EFAULT; |
| 1644 | kfree(ginfo); |
| 1645 | return err; |
| 1646 | } |
| 1647 | |
Takashi Sakamoto | 91d2178 | 2016-03-23 08:03:59 +0900 | [diff] [blame] | 1648 | static int timer_set_gparams(struct snd_timer_gparams *gparams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1650 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | int err; |
| 1652 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1653 | mutex_lock(®ister_mutex); |
Takashi Sakamoto | 91d2178 | 2016-03-23 08:03:59 +0900 | [diff] [blame] | 1654 | t = snd_timer_find(&gparams->tid); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1655 | if (!t) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | err = -ENODEV; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1657 | goto _error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | } |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1659 | if (!list_empty(&t->open_list_head)) { |
| 1660 | err = -EBUSY; |
| 1661 | goto _error; |
| 1662 | } |
| 1663 | if (!t->hw.set_period) { |
| 1664 | err = -ENOSYS; |
| 1665 | goto _error; |
| 1666 | } |
Takashi Sakamoto | 91d2178 | 2016-03-23 08:03:59 +0900 | [diff] [blame] | 1667 | err = t->hw.set_period(t, gparams->period_num, gparams->period_den); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1668 | _error: |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1669 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | return err; |
| 1671 | } |
| 1672 | |
Takashi Sakamoto | 91d2178 | 2016-03-23 08:03:59 +0900 | [diff] [blame] | 1673 | static int snd_timer_user_gparams(struct file *file, |
| 1674 | struct snd_timer_gparams __user *_gparams) |
| 1675 | { |
| 1676 | struct snd_timer_gparams gparams; |
| 1677 | |
| 1678 | if (copy_from_user(&gparams, _gparams, sizeof(gparams))) |
| 1679 | return -EFAULT; |
| 1680 | return timer_set_gparams(&gparams); |
| 1681 | } |
| 1682 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1683 | static int snd_timer_user_gstatus(struct file *file, |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1684 | struct snd_timer_gstatus __user *_gstatus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1686 | struct snd_timer_gstatus gstatus; |
| 1687 | struct snd_timer_id tid; |
| 1688 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | int err = 0; |
| 1690 | |
| 1691 | if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus))) |
| 1692 | return -EFAULT; |
| 1693 | tid = gstatus.tid; |
| 1694 | memset(&gstatus, 0, sizeof(gstatus)); |
| 1695 | gstatus.tid = tid; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1696 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | t = snd_timer_find(&tid); |
| 1698 | if (t != NULL) { |
Takashi Iwai | 9d4d207 | 2018-05-16 23:52:42 +0200 | [diff] [blame] | 1699 | spin_lock_irq(&t->lock); |
Takashi Iwai | fdcb576 | 2018-05-16 23:45:33 +0200 | [diff] [blame] | 1700 | gstatus.resolution = snd_timer_hw_resolution(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | if (t->hw.precise_resolution) { |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1702 | t->hw.precise_resolution(t, &gstatus.resolution_num, |
| 1703 | &gstatus.resolution_den); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | } else { |
| 1705 | gstatus.resolution_num = gstatus.resolution; |
| 1706 | gstatus.resolution_den = 1000000000uL; |
| 1707 | } |
Takashi Iwai | 9d4d207 | 2018-05-16 23:52:42 +0200 | [diff] [blame] | 1708 | spin_unlock_irq(&t->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | } else { |
| 1710 | err = -ENODEV; |
| 1711 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1712 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus))) |
| 1714 | err = -EFAULT; |
| 1715 | return err; |
| 1716 | } |
| 1717 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1718 | static int snd_timer_user_tselect(struct file *file, |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1719 | struct snd_timer_select __user *_tselect) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1721 | struct snd_timer_user *tu; |
| 1722 | struct snd_timer_select tselect; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | char str[32]; |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1724 | int err = 0; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | tu = file->private_data; |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1727 | if (tu->timeri) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | snd_timer_close(tu->timeri); |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1729 | tu->timeri = NULL; |
| 1730 | } |
| 1731 | if (copy_from_user(&tselect, _tselect, sizeof(tselect))) { |
| 1732 | err = -EFAULT; |
| 1733 | goto __err; |
| 1734 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | sprintf(str, "application %i", current->pid); |
| 1736 | if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE) |
| 1737 | tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1738 | err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid); |
| 1739 | if (err < 0) |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1740 | goto __err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1742 | tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST; |
| 1743 | tu->timeri->callback = tu->tread |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1744 | ? snd_timer_user_tinterrupt : snd_timer_user_interrupt; |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1745 | tu->timeri->ccallback = snd_timer_user_ccallback; |
| 1746 | tu->timeri->callback_data = (void *)tu; |
| 1747 | tu->timeri->disconnect = snd_timer_user_disconnect; |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1748 | |
| 1749 | __err: |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1750 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1753 | static int snd_timer_user_info(struct file *file, |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1754 | struct snd_timer_info __user *_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1756 | struct snd_timer_user *tu; |
| 1757 | struct snd_timer_info *info; |
| 1758 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | int err = 0; |
| 1760 | |
| 1761 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1762 | if (!tu->timeri) |
| 1763 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | t = tu->timeri->timer; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1765 | if (!t) |
| 1766 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1768 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | if (! info) |
| 1770 | return -ENOMEM; |
| 1771 | info->card = t->card ? t->card->number : -1; |
| 1772 | if (t->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 1773 | info->flags |= SNDRV_TIMER_FLG_SLAVE; |
| 1774 | strlcpy(info->id, t->id, sizeof(info->id)); |
| 1775 | strlcpy(info->name, t->name, sizeof(info->name)); |
| 1776 | info->resolution = t->hw.resolution; |
| 1777 | if (copy_to_user(_info, info, sizeof(*_info))) |
| 1778 | err = -EFAULT; |
| 1779 | kfree(info); |
| 1780 | return err; |
| 1781 | } |
| 1782 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1783 | static int snd_timer_user_params(struct file *file, |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1784 | struct snd_timer_params __user *_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1786 | struct snd_timer_user *tu; |
| 1787 | struct snd_timer_params params; |
| 1788 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | int err; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1792 | if (!tu->timeri) |
| 1793 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | t = tu->timeri->timer; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1795 | if (!t) |
| 1796 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | if (copy_from_user(¶ms, _params, sizeof(params))) |
| 1798 | return -EFAULT; |
Takashi Iwai | 71321eb | 2017-02-28 14:49:07 +0100 | [diff] [blame] | 1799 | if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) { |
| 1800 | u64 resolution; |
| 1801 | |
| 1802 | if (params.ticks < 1) { |
| 1803 | err = -EINVAL; |
| 1804 | goto _end; |
| 1805 | } |
| 1806 | |
| 1807 | /* Don't allow resolution less than 1ms */ |
| 1808 | resolution = snd_timer_resolution(tu->timeri); |
| 1809 | resolution *= params.ticks; |
| 1810 | if (resolution < 1000000) { |
| 1811 | err = -EINVAL; |
| 1812 | goto _end; |
| 1813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | } |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1815 | if (params.queue_size > 0 && |
| 1816 | (params.queue_size < 32 || params.queue_size > 1024)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | err = -EINVAL; |
| 1818 | goto _end; |
| 1819 | } |
| 1820 | if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)| |
| 1821 | (1<<SNDRV_TIMER_EVENT_TICK)| |
| 1822 | (1<<SNDRV_TIMER_EVENT_START)| |
| 1823 | (1<<SNDRV_TIMER_EVENT_STOP)| |
| 1824 | (1<<SNDRV_TIMER_EVENT_CONTINUE)| |
| 1825 | (1<<SNDRV_TIMER_EVENT_PAUSE)| |
Jaroslav Kysela | a501dfa | 2005-08-16 11:09:05 +0200 | [diff] [blame] | 1826 | (1<<SNDRV_TIMER_EVENT_SUSPEND)| |
| 1827 | (1<<SNDRV_TIMER_EVENT_RESUME)| |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | (1<<SNDRV_TIMER_EVENT_MSTART)| |
| 1829 | (1<<SNDRV_TIMER_EVENT_MSTOP)| |
| 1830 | (1<<SNDRV_TIMER_EVENT_MCONTINUE)| |
Jaroslav Kysela | 65d11d9 | 2005-08-16 13:05:43 +0200 | [diff] [blame] | 1831 | (1<<SNDRV_TIMER_EVENT_MPAUSE)| |
| 1832 | (1<<SNDRV_TIMER_EVENT_MSUSPEND)| |
Jaroslav Kysela | a501dfa | 2005-08-16 11:09:05 +0200 | [diff] [blame] | 1833 | (1<<SNDRV_TIMER_EVENT_MRESUME))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | err = -EINVAL; |
| 1835 | goto _end; |
| 1836 | } |
| 1837 | snd_timer_stop(tu->timeri); |
| 1838 | spin_lock_irq(&t->lock); |
| 1839 | tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO| |
| 1840 | SNDRV_TIMER_IFLG_EXCLUSIVE| |
| 1841 | SNDRV_TIMER_IFLG_EARLY_EVENT); |
| 1842 | if (params.flags & SNDRV_TIMER_PSFLG_AUTO) |
| 1843 | tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO; |
| 1844 | if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE) |
| 1845 | tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE; |
| 1846 | if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT) |
| 1847 | tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT; |
| 1848 | spin_unlock_irq(&t->lock); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1849 | if (params.queue_size > 0 && |
| 1850 | (unsigned int)tu->queue_size != params.queue_size) { |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1851 | err = realloc_user_queue(tu, params.queue_size); |
| 1852 | if (err < 0) |
| 1853 | goto _end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | } |
Takashi Iwai | d7f910b | 2017-06-02 17:35:30 +0200 | [diff] [blame] | 1855 | spin_lock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | tu->qhead = tu->qtail = tu->qused = 0; |
| 1857 | if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) { |
| 1858 | if (tu->tread) { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1859 | struct snd_timer_tread tread; |
Kangjie Lu | cec8f96 | 2016-05-03 16:44:07 -0400 | [diff] [blame] | 1860 | memset(&tread, 0, sizeof(tread)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | tread.event = SNDRV_TIMER_EVENT_EARLY; |
| 1862 | tread.tstamp.tv_sec = 0; |
| 1863 | tread.tstamp.tv_nsec = 0; |
| 1864 | tread.val = 0; |
| 1865 | snd_timer_user_append_to_tqueue(tu, &tread); |
| 1866 | } else { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1867 | struct snd_timer_read *r = &tu->queue[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | r->resolution = 0; |
| 1869 | r->ticks = 0; |
| 1870 | tu->qused++; |
| 1871 | tu->qtail++; |
| 1872 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | } |
| 1874 | tu->filter = params.filter; |
| 1875 | tu->ticks = params.ticks; |
Takashi Iwai | d7f910b | 2017-06-02 17:35:30 +0200 | [diff] [blame] | 1876 | spin_unlock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | err = 0; |
| 1878 | _end: |
| 1879 | if (copy_to_user(_params, ¶ms, sizeof(params))) |
| 1880 | return -EFAULT; |
| 1881 | return err; |
| 1882 | } |
| 1883 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1884 | static int snd_timer_user_status(struct file *file, |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1885 | struct snd_timer_status __user *_status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1887 | struct snd_timer_user *tu; |
| 1888 | struct snd_timer_status status; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1891 | if (!tu->timeri) |
| 1892 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | memset(&status, 0, sizeof(status)); |
| 1894 | status.tstamp = tu->tstamp; |
| 1895 | status.resolution = snd_timer_resolution(tu->timeri); |
| 1896 | status.lost = tu->timeri->lost; |
| 1897 | status.overrun = tu->overrun; |
| 1898 | spin_lock_irq(&tu->qlock); |
| 1899 | status.queue = tu->qused; |
| 1900 | spin_unlock_irq(&tu->qlock); |
| 1901 | if (copy_to_user(_status, &status, sizeof(status))) |
| 1902 | return -EFAULT; |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | static int snd_timer_user_start(struct file *file) |
| 1907 | { |
| 1908 | int err; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1909 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1912 | if (!tu->timeri) |
| 1913 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | snd_timer_stop(tu->timeri); |
| 1915 | tu->timeri->lost = 0; |
| 1916 | tu->last_resolution = 0; |
Takashi Iwai | 5d704b0 | 2019-03-28 17:44:53 +0100 | [diff] [blame] | 1917 | err = snd_timer_start(tu->timeri, tu->ticks); |
| 1918 | if (err < 0) |
| 1919 | return err; |
| 1920 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | static int snd_timer_user_stop(struct file *file) |
| 1924 | { |
| 1925 | int err; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1926 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1929 | if (!tu->timeri) |
| 1930 | return -EBADFD; |
Takashi Iwai | 5d704b0 | 2019-03-28 17:44:53 +0100 | [diff] [blame] | 1931 | err = snd_timer_stop(tu->timeri); |
| 1932 | if (err < 0) |
| 1933 | return err; |
| 1934 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | static int snd_timer_user_continue(struct file *file) |
| 1938 | { |
| 1939 | int err; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1940 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1943 | if (!tu->timeri) |
| 1944 | return -EBADFD; |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 1945 | /* start timer instead of continue if it's not used before */ |
| 1946 | if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED)) |
| 1947 | return snd_timer_user_start(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | tu->timeri->lost = 0; |
Takashi Iwai | 5d704b0 | 2019-03-28 17:44:53 +0100 | [diff] [blame] | 1949 | err = snd_timer_continue(tu->timeri); |
| 1950 | if (err < 0) |
| 1951 | return err; |
| 1952 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 1955 | static int snd_timer_user_pause(struct file *file) |
| 1956 | { |
| 1957 | int err; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1958 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1959 | |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 1960 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1961 | if (!tu->timeri) |
| 1962 | return -EBADFD; |
Takashi Iwai | 5d704b0 | 2019-03-28 17:44:53 +0100 | [diff] [blame] | 1963 | err = snd_timer_pause(tu->timeri); |
| 1964 | if (err < 0) |
| 1965 | return err; |
| 1966 | return 0; |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 1967 | } |
| 1968 | |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 1969 | enum { |
| 1970 | SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20), |
| 1971 | SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21), |
| 1972 | SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22), |
| 1973 | SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23), |
| 1974 | }; |
| 1975 | |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1976 | static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd, |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1977 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1979 | struct snd_timer_user *tu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | void __user *argp = (void __user *)arg; |
| 1981 | int __user *p = argp; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | tu = file->private_data; |
| 1984 | switch (cmd) { |
| 1985 | case SNDRV_TIMER_IOCTL_PVERSION: |
| 1986 | return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0; |
| 1987 | case SNDRV_TIMER_IOCTL_NEXT_DEVICE: |
| 1988 | return snd_timer_user_next_device(argp); |
| 1989 | case SNDRV_TIMER_IOCTL_TREAD: |
| 1990 | { |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1991 | int xarg, old_tread; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1992 | |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1993 | if (tu->timeri) /* too late */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | return -EBUSY; |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1995 | if (get_user(xarg, p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | return -EFAULT; |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1997 | old_tread = tu->tread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | tu->tread = xarg ? 1 : 0; |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1999 | if (tu->tread != old_tread && |
| 2000 | realloc_user_queue(tu, tu->queue_size) < 0) { |
| 2001 | tu->tread = old_tread; |
| 2002 | return -ENOMEM; |
| 2003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | return 0; |
| 2005 | } |
| 2006 | case SNDRV_TIMER_IOCTL_GINFO: |
| 2007 | return snd_timer_user_ginfo(file, argp); |
| 2008 | case SNDRV_TIMER_IOCTL_GPARAMS: |
| 2009 | return snd_timer_user_gparams(file, argp); |
| 2010 | case SNDRV_TIMER_IOCTL_GSTATUS: |
| 2011 | return snd_timer_user_gstatus(file, argp); |
| 2012 | case SNDRV_TIMER_IOCTL_SELECT: |
| 2013 | return snd_timer_user_tselect(file, argp); |
| 2014 | case SNDRV_TIMER_IOCTL_INFO: |
| 2015 | return snd_timer_user_info(file, argp); |
| 2016 | case SNDRV_TIMER_IOCTL_PARAMS: |
| 2017 | return snd_timer_user_params(file, argp); |
| 2018 | case SNDRV_TIMER_IOCTL_STATUS: |
| 2019 | return snd_timer_user_status(file, argp); |
| 2020 | case SNDRV_TIMER_IOCTL_START: |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 2021 | case SNDRV_TIMER_IOCTL_START_OLD: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | return snd_timer_user_start(file); |
| 2023 | case SNDRV_TIMER_IOCTL_STOP: |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 2024 | case SNDRV_TIMER_IOCTL_STOP_OLD: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | return snd_timer_user_stop(file); |
| 2026 | case SNDRV_TIMER_IOCTL_CONTINUE: |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 2027 | case SNDRV_TIMER_IOCTL_CONTINUE_OLD: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | return snd_timer_user_continue(file); |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 2029 | case SNDRV_TIMER_IOCTL_PAUSE: |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 2030 | case SNDRV_TIMER_IOCTL_PAUSE_OLD: |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 2031 | return snd_timer_user_pause(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | } |
| 2033 | return -ENOTTY; |
| 2034 | } |
| 2035 | |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 2036 | static long snd_timer_user_ioctl(struct file *file, unsigned int cmd, |
| 2037 | unsigned long arg) |
| 2038 | { |
| 2039 | struct snd_timer_user *tu = file->private_data; |
| 2040 | long ret; |
| 2041 | |
| 2042 | mutex_lock(&tu->ioctl_lock); |
| 2043 | ret = __snd_timer_user_ioctl(file, cmd, arg); |
| 2044 | mutex_unlock(&tu->ioctl_lock); |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | static int snd_timer_user_fasync(int fd, struct file * file, int on) |
| 2049 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 2050 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 2051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | tu = file->private_data; |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 2053 | return fasync_helper(fd, file, on, &tu->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | } |
| 2055 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 2056 | static ssize_t snd_timer_user_read(struct file *file, char __user *buffer, |
| 2057 | size_t count, loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | { |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 2059 | struct snd_timer_user *tu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | long result = 0, unit; |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2061 | int qhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | int err = 0; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 2063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | tu = file->private_data; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 2065 | unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read); |
Takashi Iwai | d11662f4 | 2017-06-02 15:03:38 +0200 | [diff] [blame] | 2066 | mutex_lock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | spin_lock_irq(&tu->qlock); |
| 2068 | while ((long)count - result >= unit) { |
| 2069 | while (!tu->qused) { |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 2070 | wait_queue_entry_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | |
| 2072 | if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) { |
| 2073 | err = -EAGAIN; |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2074 | goto _error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | set_current_state(TASK_INTERRUPTIBLE); |
| 2078 | init_waitqueue_entry(&wait, current); |
| 2079 | add_wait_queue(&tu->qchange_sleep, &wait); |
| 2080 | |
| 2081 | spin_unlock_irq(&tu->qlock); |
Takashi Iwai | d11662f4 | 2017-06-02 15:03:38 +0200 | [diff] [blame] | 2082 | mutex_unlock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | schedule(); |
Takashi Iwai | d11662f4 | 2017-06-02 15:03:38 +0200 | [diff] [blame] | 2084 | mutex_lock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | spin_lock_irq(&tu->qlock); |
| 2086 | |
| 2087 | remove_wait_queue(&tu->qchange_sleep, &wait); |
| 2088 | |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 2089 | if (tu->disconnected) { |
| 2090 | err = -ENODEV; |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2091 | goto _error; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 2092 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | if (signal_pending(current)) { |
| 2094 | err = -ERESTARTSYS; |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2095 | goto _error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2099 | qhead = tu->qhead++; |
| 2100 | tu->qhead %= tu->queue_size; |
Takashi Iwai | 3fa6993 | 2016-07-04 14:02:15 +0200 | [diff] [blame] | 2101 | tu->qused--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | spin_unlock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | |
| 2104 | if (tu->tread) { |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2105 | if (copy_to_user(buffer, &tu->tqueue[qhead], |
| 2106 | sizeof(struct snd_timer_tread))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | } else { |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2109 | if (copy_to_user(buffer, &tu->queue[qhead], |
| 2110 | sizeof(struct snd_timer_read))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | spin_lock_irq(&tu->qlock); |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2115 | if (err < 0) |
| 2116 | goto _error; |
| 2117 | result += unit; |
| 2118 | buffer += unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | _error: |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2121 | spin_unlock_irq(&tu->qlock); |
Takashi Iwai | d11662f4 | 2017-06-02 15:03:38 +0200 | [diff] [blame] | 2122 | mutex_unlock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | return result > 0 ? result : err; |
| 2124 | } |
| 2125 | |
Al Viro | 680ef72 | 2017-07-02 23:27:36 -0400 | [diff] [blame] | 2126 | static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | { |
Al Viro | 680ef72 | 2017-07-02 23:27:36 -0400 | [diff] [blame] | 2128 | __poll_t mask; |
Takashi Iwai | 53d2f74 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 2129 | struct snd_timer_user *tu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | |
| 2131 | tu = file->private_data; |
| 2132 | |
| 2133 | poll_wait(file, &tu->qchange_sleep, wait); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 2134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | mask = 0; |
Takashi Iwai | d7f910b | 2017-06-02 17:35:30 +0200 | [diff] [blame] | 2136 | spin_lock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | if (tu->qused) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2138 | mask |= EPOLLIN | EPOLLRDNORM; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 2139 | if (tu->disconnected) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2140 | mask |= EPOLLERR; |
Takashi Iwai | d7f910b | 2017-06-02 17:35:30 +0200 | [diff] [blame] | 2141 | spin_unlock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | |
| 2143 | return mask; |
| 2144 | } |
| 2145 | |
| 2146 | #ifdef CONFIG_COMPAT |
| 2147 | #include "timer_compat.c" |
| 2148 | #else |
| 2149 | #define snd_timer_user_ioctl_compat NULL |
| 2150 | #endif |
| 2151 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 2152 | static const struct file_operations snd_timer_f_ops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | { |
| 2154 | .owner = THIS_MODULE, |
| 2155 | .read = snd_timer_user_read, |
| 2156 | .open = snd_timer_user_open, |
| 2157 | .release = snd_timer_user_release, |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 2158 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | .poll = snd_timer_user_poll, |
| 2160 | .unlocked_ioctl = snd_timer_user_ioctl, |
| 2161 | .compat_ioctl = snd_timer_user_ioctl_compat, |
| 2162 | .fasync = snd_timer_user_fasync, |
| 2163 | }; |
| 2164 | |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2165 | /* unregister the system timer */ |
| 2166 | static void snd_timer_free_all(void) |
| 2167 | { |
| 2168 | struct snd_timer *timer, *n; |
| 2169 | |
| 2170 | list_for_each_entry_safe(timer, n, &snd_timer_list, device_list) |
| 2171 | snd_timer_free(timer); |
| 2172 | } |
| 2173 | |
Takashi Iwai | 89da061 | 2015-01-29 18:12:26 +0100 | [diff] [blame] | 2174 | static struct device timer_dev; |
| 2175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | /* |
| 2177 | * ENTRY functions |
| 2178 | */ |
| 2179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | static int __init alsa_timer_init(void) |
| 2181 | { |
| 2182 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | |
Takashi Iwai | 89da061 | 2015-01-29 18:12:26 +0100 | [diff] [blame] | 2184 | snd_device_initialize(&timer_dev, NULL); |
| 2185 | dev_set_name(&timer_dev, "timer"); |
| 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | #ifdef SNDRV_OSS_INFO_DEV_TIMERS |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 2188 | snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1, |
| 2189 | "system timer"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | #endif |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 2191 | |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2192 | err = snd_timer_register_system(); |
| 2193 | if (err < 0) { |
Takashi Iwai | cf74dcf | 2014-02-04 18:22:39 +0100 | [diff] [blame] | 2194 | pr_err("ALSA: unable to register system timer (%i)\n", err); |
Markus Elfring | 1ae0e4c | 2017-08-23 09:30:41 +0200 | [diff] [blame] | 2195 | goto put_timer; |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2196 | } |
| 2197 | |
Takashi Iwai | 40a4b26 | 2015-01-30 08:34:58 +0100 | [diff] [blame] | 2198 | err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0, |
| 2199 | &snd_timer_f_ops, NULL, &timer_dev); |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2200 | if (err < 0) { |
Takashi Iwai | cf74dcf | 2014-02-04 18:22:39 +0100 | [diff] [blame] | 2201 | pr_err("ALSA: unable to register timer device (%i)\n", err); |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2202 | snd_timer_free_all(); |
Markus Elfring | 1ae0e4c | 2017-08-23 09:30:41 +0200 | [diff] [blame] | 2203 | goto put_timer; |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2204 | } |
| 2205 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 2206 | snd_timer_proc_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | return 0; |
Markus Elfring | 1ae0e4c | 2017-08-23 09:30:41 +0200 | [diff] [blame] | 2208 | |
| 2209 | put_timer: |
| 2210 | put_device(&timer_dev); |
| 2211 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | static void __exit alsa_timer_exit(void) |
| 2215 | { |
Takashi Iwai | 40a4b26 | 2015-01-30 08:34:58 +0100 | [diff] [blame] | 2216 | snd_unregister_device(&timer_dev); |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2217 | snd_timer_free_all(); |
Takashi Iwai | 89da061 | 2015-01-29 18:12:26 +0100 | [diff] [blame] | 2218 | put_device(&timer_dev); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 2219 | snd_timer_proc_done(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | #ifdef SNDRV_OSS_INFO_DEV_TIMERS |
| 2221 | snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1); |
| 2222 | #endif |
| 2223 | } |
| 2224 | |
| 2225 | module_init(alsa_timer_init) |
| 2226 | module_exit(alsa_timer_exit) |