2 * sound/oss/sys_timer.c
4 * The default timer for the Level 2 sequencer interface
5 * Uses the (1/HZ sec) timer of kernel.
8 * Copyright (C) by Hannu Savolainen 1993-1997
10 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
11 * Version 2 (June 1991). See the "COPYING" file distributed with this software
15 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
16 * Andrew Veliath : adapted tmr2ticks from level 1 sequencer (avoid overflow)
18 #include <linux/spinlock.h>
19 #include "sound_config.h"
21 static volatile int opened, tmr_running;
22 static volatile time_t tmr_offs, tmr_ctr;
23 static volatile unsigned long ticks_offs;
24 static volatile int curr_tempo, curr_timebase;
25 static volatile unsigned long curr_ticks;
26 static volatile unsigned long next_event_time;
27 static unsigned long prev_event_time;
29 static void poll_def_tmr(unsigned long dummy);
30 static DEFINE_SPINLOCK(lock);
31 static DEFINE_TIMER(def_tmr, poll_def_tmr, 0, 0);
34 tmr2ticks(int tmr_value)
37 * Convert timer ticks to MIDI ticks
43 /* tmr_value (ticks per sec) *
44 1000000 (usecs per sec) / HZ (ticks per sec) -=> usecs */
45 tmp = tmr_value * (1000000 / HZ);
46 scale = (60 * 1000000) / (curr_tempo * curr_timebase); /* usecs per MIDI tick */
47 return (tmp + scale / 2) / scale;
51 poll_def_tmr(unsigned long dummy)
58 def_tmr.expires = (1) + jiffies;
66 curr_ticks = ticks_offs + tmr2ticks(tmr_ctr);
68 if (curr_ticks >= next_event_time)
70 next_event_time = (unsigned long) -1;
83 spin_lock_irqsave(&lock,flags);
87 next_event_time = (unsigned long) -1;
90 spin_unlock_irqrestore(&lock,flags);
94 def_tmr_open(int dev, int mode)
104 def_tmr.expires = (1) + jiffies;
112 def_tmr_close(int dev)
114 opened = tmr_running = 0;
119 def_tmr_event(int dev, unsigned char *event)
121 unsigned char cmd = event[1];
122 unsigned long parm = *(int *) &event[4];
127 parm += prev_event_time;
133 if (parm <= curr_ticks) /* It's the time */
134 return TIMER_NOT_ARMED;
137 next_event_time = prev_event_time = time;
164 ticks_offs += tmr2ticks(tmr_ctr);
171 seq_copy_to_input(event, 8);
177 return TIMER_NOT_ARMED;
181 def_tmr_get_time(int dev)
189 /* same as sound_timer.c:timer_ioctl!? */
190 static int def_tmr_ioctl(int dev, unsigned int cmd, void __user *arg)
196 case SNDCTL_TMR_SOURCE:
197 return __put_user(TMR_INTERNAL, p);
199 case SNDCTL_TMR_START:
204 case SNDCTL_TMR_STOP:
208 case SNDCTL_TMR_CONTINUE:
212 case SNDCTL_TMR_TIMEBASE:
213 if (__get_user(val, p))
222 return __put_user(curr_timebase, p);
224 case SNDCTL_TMR_TEMPO:
225 if (__get_user(val, p))
233 ticks_offs += tmr2ticks(tmr_ctr);
238 return __put_user(curr_tempo, p);
240 case SNDCTL_SEQ_CTRLRATE:
241 if (__get_user(val, p))
243 if (val != 0) /* Can't change */
245 val = ((curr_tempo * curr_timebase) + 30) / 60;
246 return __put_user(val, p);
248 case SNDCTL_SEQ_GETTIME:
249 return __put_user(curr_ticks, p);
251 case SNDCTL_TMR_METRONOME:
261 def_tmr_arm(int dev, long time)
264 time = curr_ticks + 1;
265 else if (time <= curr_ticks) /* It's the time */
268 next_event_time = prev_event_time = time;
273 struct sound_timer_operations default_sound_timer =
275 .owner = THIS_MODULE,
276 .info = {"System clock", 0},
277 .priority = 0, /* Priority */
278 .devlink = 0, /* Local device link */
279 .open = def_tmr_open,
280 .close = def_tmr_close,
281 .event = def_tmr_event,
282 .get_time = def_tmr_get_time,
283 .ioctl = def_tmr_ioctl,
284 .arm_timer = def_tmr_arm