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