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