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