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