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