Merge with 2.4.0-test3-pre4.
[linux-2.6/linux-mips.git] / drivers / sound / sys_timer.c
blob2d0cc953f37d5ffa49f913c821c354ac79f1ef08
1 /*
2 * sound/sys_timer.c
4 * The default timer for the Level 2 sequencer interface
5 * Uses the (1/HZ sec) timer of kernel.
6 */
7 /*
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
12 * for more info.
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};
34 static unsigned long
35 tmr2ticks(int tmr_value)
38 * Convert timer ticks to MIDI ticks
41 unsigned long tmp;
42 unsigned long scale;
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;
51 static void
52 poll_def_tmr(unsigned long dummy)
55 if (opened)
59 def_tmr.expires = (1) + jiffies;
60 add_timer(&def_tmr);
63 if (tmr_running)
65 tmr_ctr++;
66 curr_ticks = ticks_offs + tmr2ticks(tmr_ctr);
68 if (curr_ticks >= next_event_time)
70 next_event_time = (unsigned long) -1;
71 sequencer_timer(0);
77 static void
78 tmr_reset(void)
80 unsigned long flags;
82 save_flags(flags);
83 cli();
84 tmr_offs = 0;
85 ticks_offs = 0;
86 tmr_ctr = 0;
87 next_event_time = (unsigned long) -1;
88 prev_event_time = 0;
89 curr_ticks = 0;
90 restore_flags(flags);
93 static int
94 def_tmr_open(int dev, int mode)
96 if (opened)
97 return -EBUSY;
99 tmr_reset();
100 curr_tempo = 60;
101 curr_timebase = 100;
102 opened = 1;
107 def_tmr.expires = (1) + jiffies;
108 add_timer(&def_tmr);
111 return 0;
114 static void
115 def_tmr_close(int dev)
117 opened = tmr_running = 0;
118 del_timer(&def_tmr);;
121 static int
122 def_tmr_event(int dev, unsigned char *event)
124 unsigned char cmd = event[1];
125 unsigned long parm = *(int *) &event[4];
127 switch (cmd)
129 case TMR_WAIT_REL:
130 parm += prev_event_time;
131 case TMR_WAIT_ABS:
132 if (parm > 0)
134 long time;
136 if (parm <= curr_ticks) /* It's the time */
137 return TIMER_NOT_ARMED;
139 time = parm;
140 next_event_time = prev_event_time = time;
142 return TIMER_ARMED;
144 break;
146 case TMR_START:
147 tmr_reset();
148 tmr_running = 1;
149 break;
151 case TMR_STOP:
152 tmr_running = 0;
153 break;
155 case TMR_CONTINUE:
156 tmr_running = 1;
157 break;
159 case TMR_TEMPO:
160 if (parm)
162 if (parm < 8)
163 parm = 8;
164 if (parm > 360)
165 parm = 360;
166 tmr_offs = tmr_ctr;
167 ticks_offs += tmr2ticks(tmr_ctr);
168 tmr_ctr = 0;
169 curr_tempo = parm;
171 break;
173 case TMR_ECHO:
174 seq_copy_to_input(event, 8);
175 break;
177 default:;
180 return TIMER_NOT_ARMED;
183 static unsigned long
184 def_tmr_get_time(int dev)
186 if (!opened)
187 return 0;
189 return curr_ticks;
192 /* same as sound_timer.c:timer_ioctl!? */
193 static int def_tmr_ioctl(int dev, unsigned int cmd, caddr_t arg)
195 int val;
197 switch (cmd) {
198 case SNDCTL_TMR_SOURCE:
199 return __put_user(TMR_INTERNAL, (int *)arg);
201 case SNDCTL_TMR_START:
202 tmr_reset();
203 tmr_running = 1;
204 return 0;
206 case SNDCTL_TMR_STOP:
207 tmr_running = 0;
208 return 0;
210 case SNDCTL_TMR_CONTINUE:
211 tmr_running = 1;
212 return 0;
214 case SNDCTL_TMR_TIMEBASE:
215 if (__get_user(val, (int *)arg))
216 return -EFAULT;
217 if (val) {
218 if (val < 1)
219 val = 1;
220 if (val > 1000)
221 val = 1000;
222 curr_timebase = val;
224 return __put_user(curr_timebase, (int *)arg);
226 case SNDCTL_TMR_TEMPO:
227 if (__get_user(val, (int *)arg))
228 return -EFAULT;
229 if (val) {
230 if (val < 8)
231 val = 8;
232 if (val > 250)
233 val = 250;
234 tmr_offs = tmr_ctr;
235 ticks_offs += tmr2ticks(tmr_ctr);
236 tmr_ctr = 0;
237 curr_tempo = val;
238 reprogram_timer();
240 return __put_user(curr_tempo, (int *)arg);
242 case SNDCTL_SEQ_CTRLRATE:
243 if (__get_user(val, (int *)arg))
244 return -EFAULT;
245 if (val != 0) /* Can't change */
246 return -EINVAL;
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:
254 /* NOP */
255 break;
257 default:;
259 return -EINVAL;
262 static void
263 def_tmr_arm(int dev, long time)
265 if (time < 0)
266 time = curr_ticks + 1;
267 else if (time <= curr_ticks) /* It's the time */
268 return;
270 next_event_time = prev_event_time = time;
272 return;
275 struct sound_timer_operations default_sound_timer =
277 {"System clock", 0},
278 0, /* Priority */
279 0, /* Local device link */
280 def_tmr_open,
281 def_tmr_close,
282 def_tmr_event,
283 def_tmr_get_time,
284 def_tmr_ioctl,
285 def_tmr_arm