5 * Copyright (C) by Hannu Savolainen 1993-1997
7 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
8 * Version 2 (June 1991). See the "COPYING" file distributed with this software
12 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
14 #include <linux/config.h>
15 #include <linux/string.h>
18 #include "sound_config.h"
20 #if defined(CONFIG_SEQUENCER)
22 static volatile int initialized
= 0, opened
= 0, tmr_running
= 0;
23 static volatile time_t tmr_offs
, tmr_ctr
;
24 static volatile unsigned long ticks_offs
;
25 static volatile int curr_tempo
, curr_timebase
;
26 static volatile unsigned long curr_ticks
;
27 static volatile unsigned long next_event_time
;
28 static unsigned long prev_event_time
;
29 static volatile unsigned long usecs_per_tmr
; /* Length of the current interval */
31 static struct sound_lowlev_timer
*tmr
= NULL
;
33 static unsigned long tmr2ticks(int tmr_value
)
36 * Convert timer ticks to MIDI ticks
42 tmp
= tmr_value
* usecs_per_tmr
; /* Convert to usecs */
43 scale
= (60 * 1000000) / (curr_tempo
* curr_timebase
); /* usecs per MIDI tick */
44 return (tmp
+ (scale
/ 2)) / scale
;
47 void reprogram_timer(void)
49 unsigned long usecs_per_tick
;
52 * The user is changing the timer rate before setting a timer
53 * slap, bad bad not allowed.
59 usecs_per_tick
= (60 * 1000000) / (curr_tempo
* curr_timebase
);
62 * Don't kill the system by setting too high timer rate
64 if (usecs_per_tick
< 2000)
65 usecs_per_tick
= 2000;
67 usecs_per_tmr
= tmr
->tmr_start(tmr
->dev
, usecs_per_tick
);
70 void sound_timer_syncinterval(unsigned int new_usecs
)
73 * This routine is called by the hardware level if
74 * the clock frequency has changed for some reason.
77 ticks_offs
+= tmr2ticks(tmr_ctr
);
79 usecs_per_tmr
= new_usecs
;
82 static void tmr_reset(void)
91 next_event_time
= (unsigned long) -1;
97 static int timer_open(int dev
, int mode
)
109 static void timer_close(int dev
)
111 opened
= tmr_running
= 0;
112 tmr
->tmr_disable(tmr
->dev
);
115 static int timer_event(int dev
, unsigned char *event
)
117 unsigned char cmd
= event
[1];
118 unsigned long parm
= *(int *) &event
[4];
123 parm
+= prev_event_time
;
129 if (parm
<= curr_ticks
) /* It's the time */
130 return TIMER_NOT_ARMED
;
132 next_event_time
= prev_event_time
= time
;
160 ticks_offs
+= tmr2ticks(tmr_ctr
);
168 seq_copy_to_input(event
, 8);
173 return TIMER_NOT_ARMED
;
176 static unsigned long timer_get_time(int dev
)
183 static int timer_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
189 case SNDCTL_TMR_SOURCE
:
193 case SNDCTL_TMR_START
:
198 case SNDCTL_TMR_STOP
:
202 case SNDCTL_TMR_CONTINUE
:
206 case SNDCTL_TMR_TIMEBASE
:
207 if (get_user(val
, (int *)arg
))
220 case SNDCTL_TMR_TEMPO
:
221 if (get_user(val
, (int *)arg
))
230 ticks_offs
+= tmr2ticks(tmr_ctr
);
238 case SNDCTL_SEQ_CTRLRATE
:
239 if (get_user(val
, (int *)arg
))
241 if (val
!= 0) /* Can't change */
243 val
= ((curr_tempo
* curr_timebase
) + 30) / 60;
246 case SNDCTL_SEQ_GETTIME
:
250 case SNDCTL_TMR_METRONOME
:
254 return put_user(val
, (int *)arg
);
257 static void timer_arm(int dev
, long time
)
260 time
= curr_ticks
+ 1;
261 else if (time
<= curr_ticks
) /* It's the time */
264 next_event_time
= prev_event_time
= time
;
268 static struct sound_timer_operations sound_timer
=
272 0, /* Local device link */
281 void sound_timer_interrupt(void)
286 tmr
->tmr_restart(tmr
->dev
);
292 curr_ticks
= ticks_offs
+ tmr2ticks(tmr_ctr
);
294 if (curr_ticks
>= next_event_time
)
296 next_event_time
= (unsigned long) -1;
301 void sound_timer_init(struct sound_lowlev_timer
*t
, char *name
)
307 if (t
->priority
<= tmr
->priority
)
308 return; /* There is already a similar or better timer */
315 n
= sound_alloc_timerdev();
317 n
= 0; /* Overwrite the system timer */
318 strcpy(sound_timer
.info
.name
, name
);
319 sound_timer_devs
[n
] = &sound_timer
;