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 "sound_config.h"
20 static volatile int opened
= 0, tmr_running
= 0;
21 static volatile time_t tmr_offs
, tmr_ctr
;
22 static volatile unsigned long ticks_offs
;
23 static volatile int curr_tempo
, curr_timebase
;
24 static volatile unsigned long curr_ticks
;
25 static volatile unsigned long next_event_time
;
26 static unsigned long prev_event_time
;
28 static void poll_def_tmr(unsigned long dummy
);
31 static struct timer_list def_tmr
=
32 {function
: poll_def_tmr
};
35 tmr2ticks(int tmr_value
)
38 * Convert timer ticks to MIDI ticks
44 /* tmr_value (ticks per sec) *
45 1000000 (usecs per sec) / HZ (ticks per sec) -=> usecs */
46 tmp
= tmr_value
* (1000000 / HZ
);
47 scale
= (60 * 1000000) / (curr_tempo
* curr_timebase
); /* usecs per MIDI tick */
48 return (tmp
+ scale
/ 2) / scale
;
52 poll_def_tmr(unsigned long dummy
)
59 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;
87 next_event_time
= (unsigned long) -1;
94 def_tmr_open(int dev
, int mode
)
107 def_tmr
.expires
= (1) + jiffies
;
115 def_tmr_close(int dev
)
117 opened
= tmr_running
= 0;
118 del_timer(&def_tmr
);;
122 def_tmr_event(int dev
, unsigned char *event
)
124 unsigned char cmd
= event
[1];
125 unsigned long parm
= *(int *) &event
[4];
130 parm
+= prev_event_time
;
136 if (parm
<= curr_ticks
) /* It's the time */
137 return TIMER_NOT_ARMED
;
140 next_event_time
= prev_event_time
= time
;
167 ticks_offs
+= tmr2ticks(tmr_ctr
);
174 seq_copy_to_input(event
, 8);
180 return TIMER_NOT_ARMED
;
184 def_tmr_get_time(int dev
)
192 /* same as sound_timer.c:timer_ioctl!? */
193 static int def_tmr_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
198 case SNDCTL_TMR_SOURCE
:
199 return __put_user(TMR_INTERNAL
, (int *)arg
);
201 case SNDCTL_TMR_START
:
206 case SNDCTL_TMR_STOP
:
210 case SNDCTL_TMR_CONTINUE
:
214 case SNDCTL_TMR_TIMEBASE
:
215 if (__get_user(val
, (int *)arg
))
224 return __put_user(curr_timebase
, (int *)arg
);
226 case SNDCTL_TMR_TEMPO
:
227 if (__get_user(val
, (int *)arg
))
235 ticks_offs
+= tmr2ticks(tmr_ctr
);
240 return __put_user(curr_tempo
, (int *)arg
);
242 case SNDCTL_SEQ_CTRLRATE
:
243 if (__get_user(val
, (int *)arg
))
245 if (val
!= 0) /* Can't change */
247 val
= ((curr_tempo
* curr_timebase
) + 30) / 60;
248 return __put_user(val
, (int *)arg
);
250 case SNDCTL_SEQ_GETTIME
:
251 return __put_user(curr_ticks
, (int *)arg
);
253 case SNDCTL_TMR_METRONOME
:
263 def_tmr_arm(int dev
, long time
)
266 time
= curr_ticks
+ 1;
267 else if (time
<= curr_ticks
) /* It's the time */
270 next_event_time
= prev_event_time
= time
;
275 struct sound_timer_operations default_sound_timer
=
279 0, /* Local device link */