Make GET_SAVED_SP lowercase.
[linux-2.6/linux-mips.git] / sound / oss / sys_timer.c
blobb3d42dcef13c486e364708e01a513355db4e9aa5
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 <linux/spinlock.h>
19 #include "sound_config.h"
21 static volatile int opened = 0, tmr_running = 0;
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 spinlock_t lock=SPIN_LOCK_UNLOCKED;
32 static struct timer_list def_tmr = TIMER_INITIALIZER(poll_def_tmr, 0, 0);
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 spin_lock(&lock);
66 tmr_ctr++;
67 curr_ticks = ticks_offs + tmr2ticks(tmr_ctr);
69 if (curr_ticks >= next_event_time)
71 next_event_time = (unsigned long) -1;
72 sequencer_timer(0);
74 spin_unlock(&lock);
79 static void
80 tmr_reset(void)
82 unsigned long flags;
84 spin_lock_irqsave(&lock,flags);
85 tmr_offs = 0;
86 ticks_offs = 0;
87 tmr_ctr = 0;
88 next_event_time = (unsigned long) -1;
89 prev_event_time = 0;
90 curr_ticks = 0;
91 spin_unlock_irqrestore(&lock,flags);
94 static int
95 def_tmr_open(int dev, int mode)
97 if (opened)
98 return -EBUSY;
100 tmr_reset();
101 curr_tempo = 60;
102 curr_timebase = 100;
103 opened = 1;
108 def_tmr.expires = (1) + jiffies;
109 add_timer(&def_tmr);
112 return 0;
115 static void
116 def_tmr_close(int dev)
118 opened = tmr_running = 0;
119 del_timer(&def_tmr);;
122 static int
123 def_tmr_event(int dev, unsigned char *event)
125 unsigned char cmd = event[1];
126 unsigned long parm = *(int *) &event[4];
128 switch (cmd)
130 case TMR_WAIT_REL:
131 parm += prev_event_time;
132 case TMR_WAIT_ABS:
133 if (parm > 0)
135 long time;
137 if (parm <= curr_ticks) /* It's the time */
138 return TIMER_NOT_ARMED;
140 time = parm;
141 next_event_time = prev_event_time = time;
143 return TIMER_ARMED;
145 break;
147 case TMR_START:
148 tmr_reset();
149 tmr_running = 1;
150 break;
152 case TMR_STOP:
153 tmr_running = 0;
154 break;
156 case TMR_CONTINUE:
157 tmr_running = 1;
158 break;
160 case TMR_TEMPO:
161 if (parm)
163 if (parm < 8)
164 parm = 8;
165 if (parm > 360)
166 parm = 360;
167 tmr_offs = tmr_ctr;
168 ticks_offs += tmr2ticks(tmr_ctr);
169 tmr_ctr = 0;
170 curr_tempo = parm;
172 break;
174 case TMR_ECHO:
175 seq_copy_to_input(event, 8);
176 break;
178 default:;
181 return TIMER_NOT_ARMED;
184 static unsigned long
185 def_tmr_get_time(int dev)
187 if (!opened)
188 return 0;
190 return curr_ticks;
193 /* same as sound_timer.c:timer_ioctl!? */
194 static int def_tmr_ioctl(int dev, unsigned int cmd, caddr_t arg)
196 int val;
198 switch (cmd) {
199 case SNDCTL_TMR_SOURCE:
200 return __put_user(TMR_INTERNAL, (int *)arg);
202 case SNDCTL_TMR_START:
203 tmr_reset();
204 tmr_running = 1;
205 return 0;
207 case SNDCTL_TMR_STOP:
208 tmr_running = 0;
209 return 0;
211 case SNDCTL_TMR_CONTINUE:
212 tmr_running = 1;
213 return 0;
215 case SNDCTL_TMR_TIMEBASE:
216 if (__get_user(val, (int *)arg))
217 return -EFAULT;
218 if (val) {
219 if (val < 1)
220 val = 1;
221 if (val > 1000)
222 val = 1000;
223 curr_timebase = val;
225 return __put_user(curr_timebase, (int *)arg);
227 case SNDCTL_TMR_TEMPO:
228 if (__get_user(val, (int *)arg))
229 return -EFAULT;
230 if (val) {
231 if (val < 8)
232 val = 8;
233 if (val > 250)
234 val = 250;
235 tmr_offs = tmr_ctr;
236 ticks_offs += tmr2ticks(tmr_ctr);
237 tmr_ctr = 0;
238 curr_tempo = val;
239 reprogram_timer();
241 return __put_user(curr_tempo, (int *)arg);
243 case SNDCTL_SEQ_CTRLRATE:
244 if (__get_user(val, (int *)arg))
245 return -EFAULT;
246 if (val != 0) /* Can't change */
247 return -EINVAL;
248 val = ((curr_tempo * curr_timebase) + 30) / 60;
249 return __put_user(val, (int *)arg);
251 case SNDCTL_SEQ_GETTIME:
252 return __put_user(curr_ticks, (int *)arg);
254 case SNDCTL_TMR_METRONOME:
255 /* NOP */
256 break;
258 default:;
260 return -EINVAL;
263 static void
264 def_tmr_arm(int dev, long time)
266 if (time < 0)
267 time = curr_ticks + 1;
268 else if (time <= curr_ticks) /* It's the time */
269 return;
271 next_event_time = prev_event_time = time;
273 return;
276 struct sound_timer_operations default_sound_timer =
278 .owner = THIS_MODULE,
279 .info = {"System clock", 0},
280 .priority = 0, /* Priority */
281 .devlink = 0, /* Local device link */
282 .open = def_tmr_open,
283 .close = def_tmr_close,
284 .event = def_tmr_event,
285 .get_time = def_tmr_get_time,
286 .ioctl = def_tmr_ioctl,
287 .arm_timer = def_tmr_arm