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