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/config.h>
21 #include "sound_config.h"
23 #ifdef CONFIG_SEQUENCER
25 static volatile int opened
= 0, tmr_running
= 0;
26 static volatile time_t tmr_offs
, tmr_ctr
;
27 static volatile unsigned long ticks_offs
;
28 static volatile int curr_tempo
, curr_timebase
;
29 static volatile unsigned long curr_ticks
;
30 static volatile unsigned long next_event_time
;
31 static unsigned long prev_event_time
;
33 static void poll_def_tmr(unsigned long dummy
);
36 static struct timer_list def_tmr
=
37 {NULL
, NULL
, 0, 0, poll_def_tmr
};
40 tmr2ticks(int tmr_value
)
43 * Convert timer ticks to MIDI ticks
49 /* tmr_value (ticks per sec) *
50 1000000 (usecs per sec) / HZ (ticks per sec) -=> usecs */
51 tmp
= tmr_value
* (1000000 / HZ
);
52 scale
= (60 * 1000000) / (curr_tempo
* curr_timebase
); /* usecs per MIDI tick */
53 return (tmp
+ scale
/ 2) / scale
;
57 poll_def_tmr(unsigned long dummy
)
64 def_tmr
.expires
= (1) + jiffies
;
71 curr_ticks
= ticks_offs
+ tmr2ticks(tmr_ctr
);
73 if (curr_ticks
>= next_event_time
)
75 next_event_time
= (unsigned long) -1;
92 next_event_time
= (unsigned long) -1;
99 def_tmr_open(int dev
, int mode
)
112 def_tmr
.expires
= (1) + jiffies
;
120 def_tmr_close(int dev
)
122 opened
= tmr_running
= 0;
123 del_timer(&def_tmr
);;
127 def_tmr_event(int dev
, unsigned char *event
)
129 unsigned char cmd
= event
[1];
130 unsigned long parm
= *(int *) &event
[4];
135 parm
+= prev_event_time
;
141 if (parm
<= curr_ticks
) /* It's the time */
142 return TIMER_NOT_ARMED
;
145 next_event_time
= prev_event_time
= time
;
172 ticks_offs
+= tmr2ticks(tmr_ctr
);
179 seq_copy_to_input(event
, 8);
185 return TIMER_NOT_ARMED
;
189 def_tmr_get_time(int dev
)
197 /* same as sound_timer.c:timer_ioctl!? */
198 static int def_tmr_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
203 case SNDCTL_TMR_SOURCE
:
204 return __put_user(TMR_INTERNAL
, (int *)arg
);
206 case SNDCTL_TMR_START
:
211 case SNDCTL_TMR_STOP
:
215 case SNDCTL_TMR_CONTINUE
:
219 case SNDCTL_TMR_TIMEBASE
:
220 if (__get_user(val
, (int *)arg
))
229 return __put_user(curr_timebase
, (int *)arg
);
231 case SNDCTL_TMR_TEMPO
:
232 if (__get_user(val
, (int *)arg
))
240 ticks_offs
+= tmr2ticks(tmr_ctr
);
245 return __put_user(curr_tempo
, (int *)arg
);
247 case SNDCTL_SEQ_CTRLRATE
:
248 if (__get_user(val
, (int *)arg
))
250 if (val
!= 0) /* Can't change */
252 val
= ((curr_tempo
* curr_timebase
) + 30) / 60;
253 return __put_user(val
, (int *)arg
);
255 case SNDCTL_SEQ_GETTIME
:
256 return __put_user(curr_ticks
, (int *)arg
);
258 case SNDCTL_TMR_METRONOME
:
268 def_tmr_arm(int dev
, long time
)
271 time
= curr_ticks
+ 1;
272 else if (time
<= curr_ticks
) /* It's the time */
275 next_event_time
= prev_event_time
= time
;
280 struct sound_timer_operations default_sound_timer
=
284 0, /* Local device link */