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