24 #include <linux/slab.h>
25 #include <linux/time.h>
27 #include <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
38 #if defined(CONFIG_SND_HRTIMER) || defined(CONFIG_SND_HRTIMER_MODULE)
39 #define DEFAULT_TIMER_LIMIT 4
40 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
41 #define DEFAULT_TIMER_LIMIT 2
43 #define DEFAULT_TIMER_LIMIT 1
47 static int timer_tstamp_monotonic = 1;
54 MODULE_PARM_DESC(timer_tstamp_monotonic,
"Use posix monotonic clock source for timestamps (default).");
95 static void snd_timer_reschedule(
struct snd_timer *
timer,
unsigned long ticks_left);
105 timeri = kzalloc(
sizeof(*timeri),
GFP_KERNEL);
109 if (! timeri->
owner) {
120 if (timer && !try_module_get(timer->
module)) {
153 #ifdef CONFIG_MODULES
159 if (tid->
device < timer_limit)
160 request_module(
"snd-timer-%i", tid->
device);
165 request_module(
"snd-card-%i", tid->
card);
192 spin_lock_irq(&slave_active_lock);
195 spin_unlock_irq(&slave_active_lock);
217 spin_lock_irq(&slave_active_lock);
223 spin_unlock_irq(&slave_active_lock);
247 timeri = snd_timer_instance_new(owner,
NULL);
256 snd_timer_check_slave(timeri);
264 timer = snd_timer_find(tid);
265 #ifdef CONFIG_MODULES
268 snd_timer_request(tid);
270 timer = snd_timer_find(tid);
285 timeri = snd_timer_instance_new(owner, timer);
293 timer->
hw.open(timer);
295 snd_timer_check_master(timeri);
302 int keep_flag,
int event);
320 spin_lock_irq(&slave_active_lock);
322 spin_unlock_irq(&slave_active_lock);
324 spin_lock_irq(&slave_active_lock);
326 spin_unlock_irq(&slave_active_lock);
331 timer = timeri->
timer;
335 spin_lock_irq(&timer->
lock);
337 spin_unlock_irq(&timer->
lock);
339 spin_lock_irq(&timer->
lock);
341 spin_unlock_irq(&timer->
lock);
346 timer->
hw.close(timer);
350 spin_lock_irq(&slave_active_lock);
352 list_move_tail(&slave->
open_list, &snd_timer_slave_list);
355 spin_unlock_irq(&slave_active_lock);
365 module_put(timer->
module);
376 if (timer->
hw.c_resolution)
377 return timer->
hw.c_resolution(timer);
378 return timer->
hw.resolution;
387 unsigned long resolution = 0;
391 if (timer_tstamp_monotonic)
413 ts->ccallback(ti, event + 100, &
tstamp, resolution);
414 spin_unlock_irqrestore(&timer->lock, flags);
418 unsigned long sticks)
420 list_move_tail(&timeri->active_list, &timer->active_list_head);
421 if (timer->running) {
428 timer->sticks = sticks;
429 timer->hw.start(timer);
445 &timeri->
master->slave_active_head);
446 spin_unlock_irqrestore(&slave_active_lock, flags);
459 if (timeri ==
NULL || ticks < 1)
462 result = snd_timer_start_slave(timeri);
466 timer = timeri->
timer;
472 result = snd_timer_start1(timer, timeri, ticks);
473 spin_unlock_irqrestore(&timer->
lock, flags);
479 int keep_flag,
int event)
491 spin_unlock_irqrestore(&slave_active_lock, flags);
495 timer = timeri->
timer;
503 timer->
hw.stop(timer);
506 snd_timer_reschedule(timer, 0);
509 timer->
hw.start(timer);
516 spin_unlock_irqrestore(&timer->
lock, flags);
519 snd_timer_notify1(timeri, event);
537 timer = timeri->
timer;
543 spin_unlock_irqrestore(&timer->
lock, flags);
559 return snd_timer_start_slave(timeri);
560 timer = timeri->
timer;
567 result = snd_timer_start1(timer, timeri, timer->
sticks);
568 spin_unlock_irqrestore(&timer->
lock, flags);
587 static void snd_timer_reschedule(
struct snd_timer * timer,
unsigned long ticks_left)
607 if (ticks > timer->
hw.ticks)
608 ticks = timer->
hw.ticks;
609 if (ticks_left != ticks)
618 static void snd_timer_tasklet(
unsigned long arg)
623 unsigned long resolution,
ticks;
640 spin_unlock(&timer->
lock);
642 ti->
callback(ti, resolution, ticks);
643 spin_lock(&timer->
lock);
646 spin_unlock_irqrestore(&timer->
lock, flags);
669 if (timer->
hw.c_resolution)
670 resolution = timer->
hw.c_resolution(timer);
672 resolution = timer->
hw.resolution;
685 if (ti->
cticks < ticks_left)
713 snd_timer_reschedule(timer, timer->
sticks);
716 timer->
hw.stop(timer);
723 timer->
hw.start(timer);
726 timer->
hw.stop(timer);
741 spin_unlock(&timer->
lock);
743 ti->
callback(ti, resolution, ticks);
744 spin_lock(&timer->
lock);
750 spin_unlock_irqrestore(&timer->
lock, flags);
767 .dev_register = snd_timer_dev_register,
768 .dev_disconnect = snd_timer_dev_disconnect,
793 (
unsigned long)timer);
795 timer->module = card->
module;
798 snd_timer_free(timer);
807 static int snd_timer_free(
struct snd_timer *timer)
835 return snd_timer_free(timer);
846 !timer->
hw.resolution && timer->
hw.c_resolution ==
NULL)
856 if (timer1->
card->number > timer->
card->number)
858 if (timer1->
card->number < timer->
card->number)
890 unsigned long resolution = 0;
902 if (timer->
hw.c_resolution)
903 resolution = timer->
hw.c_resolution(timer);
905 resolution = timer->
hw.resolution;
909 ti->
ccallback(ti, event, tstamp, resolution);
912 ts->
ccallback(ts, event, tstamp, resolution);
914 spin_unlock_irqrestore(&timer->
lock, flags);
934 return snd_timer_free(timer);
941 memset(&dev, 0,
sizeof(dev));
943 return snd_timer_dev_register(&dev);
957 static void snd_timer_s_function(
unsigned long data)
967 static int snd_timer_s_start(
struct snd_timer * timer)
986 static int snd_timer_s_stop(
struct snd_timer * timer)
1005 .resolution = 1000000000
L /
HZ,
1007 .start = snd_timer_s_start,
1008 .stop = snd_timer_s_stop
1011 static void snd_timer_free_system(
struct snd_timer *timer)
1016 static int snd_timer_register_system(
void)
1026 timer->
hw = snd_timer_system;
1029 snd_timer_free(timer);
1033 priv->
tlist.function = snd_timer_s_function;
1034 priv->
tlist.data = (
unsigned long) timer;
1040 #ifdef CONFIG_PROC_FS
1055 snd_iprintf(buffer,
"G%i: ", timer->
tmr_device);
1058 snd_iprintf(buffer,
"C%i-%i: ",
1062 snd_iprintf(buffer,
"P%i-%i-%i: ", timer->
card->number,
1066 snd_iprintf(buffer,
"?%i-%i-%i-%i: ", timer->
tmr_class,
1067 timer->
card ? timer->
card->number : -1,
1070 snd_iprintf(buffer,
"%s :", timer->
name);
1071 if (timer->
hw.resolution)
1072 snd_iprintf(buffer,
" %lu.%03luus (%lu ticks)",
1073 timer->
hw.resolution / 1000,
1074 timer->
hw.resolution % 1000,
1077 snd_iprintf(buffer,
" SLAVE");
1078 snd_iprintf(buffer,
"\n");
1080 snd_iprintf(buffer, " Client %
s : %
s\
n",
1081 ti->owner ? ti->owner : "
unknown",
1096 if (entry !=
NULL) {
1097 entry->
c.
text.read = snd_timer_proc_read;
1098 if (snd_info_register(entry) < 0) {
1099 snd_info_free_entry(entry);
1103 snd_timer_proc_entry =
entry;
1108 snd_info_free_entry(snd_timer_proc_entry);
1111 #define snd_timer_proc_init()
1112 #define snd_timer_proc_done()
1120 unsigned long resolution,
1121 unsigned long ticks)
1127 spin_lock(&tu->
qlock);
1128 if (tu->
qused > 0) {
1146 spin_unlock(&tu->
qlock);
1151 static void snd_timer_user_append_to_tqueue(
struct snd_timer_user *tu,
1166 unsigned long resolution)
1170 unsigned long flags;
1175 if ((tu->
filter & (1 << event)) == 0 || !tu->
tread)
1179 r1.val = resolution;
1181 snd_timer_user_append_to_tqueue(tu, &
r1);
1182 spin_unlock_irqrestore(&tu->
qlock, flags);
1188 unsigned long resolution,
1189 unsigned long ticks)
1194 int prev, append = 0;
1196 memset(&tstamp, 0,
sizeof(tstamp));
1197 spin_lock(&tu->
qlock);
1200 spin_unlock(&tu->
qlock);
1204 if (timer_tstamp_monotonic)
1213 r1.
val = resolution;
1214 snd_timer_user_append_to_tqueue(tu, &r1);
1222 if (tu->
qused > 0) {
1235 snd_timer_user_append_to_tqueue(tu, &r1);
1238 spin_unlock(&tu->
qlock);
1272 static int snd_timer_user_release(
struct inode *inode,
struct file *file)
1288 static void snd_timer_user_zero_id(
struct snd_timer_id *
id)
1301 id->card = timer->
card ? timer->
card->number : -1;
1306 static int snd_timer_user_next_device(
struct snd_timer_id __user *_tid)
1316 if (list_empty(&snd_timer_list))
1317 snd_timer_user_zero_id(&
id);
1321 snd_timer_user_copy_id(&
id, timer);
1326 id.device =
id.device < 0 ? 0 :
id.device + 1;
1330 snd_timer_user_copy_id(&
id, timer);
1334 snd_timer_user_copy_id(&
id, timer);
1338 if (p == &snd_timer_list)
1339 snd_timer_user_zero_id(&
id);
1363 snd_timer_user_copy_id(&
id, timer);
1368 if (timer->
card->number >
id.card) {
1369 snd_timer_user_copy_id(&
id, timer);
1372 if (timer->
card->number <
id.card)
1375 snd_timer_user_copy_id(&
id, timer);
1381 snd_timer_user_copy_id(&
id, timer);
1386 snd_timer_user_copy_id(&
id, timer);
1389 if (p == &snd_timer_list)
1390 snd_timer_user_zero_id(&
id);
1393 snd_timer_user_zero_id(&
id);
1402 static int snd_timer_user_ginfo(
struct file *file,
1413 return PTR_ERR(ginfo);
1416 memset(ginfo, 0,
sizeof(*ginfo));
1419 t = snd_timer_find(&tid);
1427 if (t->
hw.resolution_min > 0) {
1438 if (err >= 0 &&
copy_to_user(_ginfo, ginfo,
sizeof(*ginfo)))
1444 static int snd_timer_user_gparams(
struct file *file,
1454 t = snd_timer_find(&gparams.tid);
1463 if (!t->
hw.set_period) {
1467 err = t->
hw.set_period(t, gparams.period_num, gparams.period_den);
1473 static int snd_timer_user_gstatus(
struct file *file,
1484 memset(&gstatus, 0,
sizeof(gstatus));
1487 t = snd_timer_find(&tid);
1489 if (t->
hw.c_resolution)
1490 gstatus.resolution = t->
hw.c_resolution(t);
1492 gstatus.resolution = t->
hw.resolution;
1493 if (t->
hw.precise_resolution) {
1494 t->
hw.precise_resolution(t, &gstatus.resolution_num,
1495 &gstatus.resolution_den);
1497 gstatus.resolution_num = gstatus.resolution;
1498 gstatus.resolution_den = 1000000000uL;
1504 if (err >= 0 &&
copy_to_user(_gstatus, &gstatus,
sizeof(gstatus)))
1509 static int snd_timer_user_tselect(
struct file *file,
1556 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1557 tu->
timeri->ccallback = snd_timer_user_ccallback;
1558 tu->
timeri->callback_data = (
void *)tu;
1566 static int snd_timer_user_info(
struct file *file,
1596 static int snd_timer_user_params(
struct file *file,
1618 if (
params.queue_size > 0 &&
1619 (
params.queue_size < 32 ||
params.queue_size > 1024)) {
1641 spin_lock_irq(&t->
lock);
1651 spin_unlock_irq(&t->
lock);
1652 if (
params.queue_size > 0 &&
1678 tread.
tstamp.tv_nsec = 0;
1680 snd_timer_user_append_to_tqueue(tu, &tread);
1698 static int snd_timer_user_status(
struct file *file,
1708 status.tstamp = tu->tstamp;
1710 status.lost = tu->timeri->lost;
1711 status.overrun = tu->overrun;
1712 spin_lock_irq(&tu->qlock);
1713 status.queue = tu->qused;
1714 spin_unlock_irq(&tu->qlock);
1720 static int snd_timer_user_start(
struct file *file)
1734 static int snd_timer_user_stop(
struct file *file)
1745 static int snd_timer_user_continue(
struct file *file)
1757 static int snd_timer_user_pause(
struct file *file)
1775 static long snd_timer_user_ioctl(
struct file *file,
unsigned int cmd,
1787 return snd_timer_user_next_device(argp);
1801 tu->
tread = xarg ? 1 : 0;
1806 return snd_timer_user_ginfo(file, argp);
1808 return snd_timer_user_gparams(file, argp);
1810 return snd_timer_user_gstatus(file, argp);
1812 return snd_timer_user_tselect(file, argp);
1814 return snd_timer_user_info(file, argp);
1816 return snd_timer_user_params(file, argp);
1818 return snd_timer_user_status(file, argp);
1821 return snd_timer_user_start(file);
1824 return snd_timer_user_stop(file);
1827 return snd_timer_user_continue(file);
1830 return snd_timer_user_pause(file);
1835 static int snd_timer_user_fasync(
int fd,
struct file * file,
int on)
1843 static ssize_t snd_timer_user_read(
struct file *file,
char __user *buffer,
1852 spin_lock_irq(&tu->qlock);
1853 while ((long)count - result >=
unit) {
1854 while (!tu->
qused) {
1863 init_waitqueue_entry(&wait,
current);
1866 spin_unlock_irq(&tu->
qlock);
1868 spin_lock_irq(&tu->
qlock);
1872 if (signal_pending(
current)) {
1878 spin_unlock_irq(&tu->
qlock);
1901 spin_lock_irq(&tu->
qlock);
1904 spin_unlock_irq(&tu->
qlock);
1906 return result > 0 ? result :
err;
1909 static unsigned int snd_timer_user_poll(
struct file *file,
poll_table * wait)
1925 #ifdef CONFIG_COMPAT
1928 #define snd_timer_user_ioctl_compat NULL
1934 .read = snd_timer_user_read,
1935 .open = snd_timer_user_open,
1936 .release = snd_timer_user_release,
1938 .poll = snd_timer_user_poll,
1939 .unlocked_ioctl = snd_timer_user_ioctl,
1941 .fasync = snd_timer_user_fasync,
1948 static int __init alsa_timer_init(
void)
1952 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1953 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS,
SNDRV_CARDS - 1,
1957 if ((err = snd_timer_register_system()) < 0)
1961 &snd_timer_f_ops,
NULL,
"timer")) < 0)
1968 static void __exit alsa_timer_exit(
void)
1976 snd_timer_free(timer);
1979 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1980 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS,
SNDRV_CARDS - 1);