blob: 6afe29b763b747e5b9b4839e35ccddb03b668b91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sound/sys_timer.c
3 *
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
9 *
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.
13 */
14/*
15 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
16 * Andrew Veliath : adapted tmr2ticks from level 1 sequencer (avoid overflow)
17 */
18#include <linux/spinlock.h>
19#include "sound_config.h"
20
21static volatile int opened, tmr_running;
22static volatile time_t tmr_offs, tmr_ctr;
23static volatile unsigned long ticks_offs;
24static volatile int curr_tempo, curr_timebase;
25static volatile unsigned long curr_ticks;
26static volatile unsigned long next_event_time;
27static unsigned long prev_event_time;
28
29static void poll_def_tmr(unsigned long dummy);
30static DEFINE_SPINLOCK(lock);
31
32static struct timer_list def_tmr = TIMER_INITIALIZER(poll_def_tmr, 0, 0);
33
34static unsigned long
35tmr2ticks(int tmr_value)
36{
37 /*
38 * Convert timer ticks to MIDI ticks
39 */
40
41 unsigned long tmp;
42 unsigned long scale;
43
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;
49}
50
51static void
52poll_def_tmr(unsigned long dummy)
53{
54
55 if (opened)
56 {
57
58 {
59 def_tmr.expires = (1) + jiffies;
60 add_timer(&def_tmr);
61 };
62
63 if (tmr_running)
64 {
65 spin_lock(&lock);
66 tmr_ctr++;
67 curr_ticks = ticks_offs + tmr2ticks(tmr_ctr);
68
69 if (curr_ticks >= next_event_time)
70 {
71 next_event_time = (unsigned long) -1;
72 sequencer_timer(0);
73 }
74 spin_unlock(&lock);
75 }
76 }
77}
78
79static void
80tmr_reset(void)
81{
82 unsigned long flags;
83
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);
92}
93
94static int
95def_tmr_open(int dev, int mode)
96{
97 if (opened)
98 return -EBUSY;
99
100 tmr_reset();
101 curr_tempo = 60;
102 curr_timebase = 100;
103 opened = 1;
104
105 ;
106
107 {
108 def_tmr.expires = (1) + jiffies;
109 add_timer(&def_tmr);
110 };
111
112 return 0;
113}
114
115static void
116def_tmr_close(int dev)
117{
118 opened = tmr_running = 0;
119 del_timer(&def_tmr);
120}
121
122static int
123def_tmr_event(int dev, unsigned char *event)
124{
125 unsigned char cmd = event[1];
126 unsigned long parm = *(int *) &event[4];
127
128 switch (cmd)
129 {
130 case TMR_WAIT_REL:
131 parm += prev_event_time;
132 case TMR_WAIT_ABS:
133 if (parm > 0)
134 {
135 long time;
136
137 if (parm <= curr_ticks) /* It's the time */
138 return TIMER_NOT_ARMED;
139
140 time = parm;
141 next_event_time = prev_event_time = time;
142
143 return TIMER_ARMED;
144 }
145 break;
146
147 case TMR_START:
148 tmr_reset();
149 tmr_running = 1;
150 break;
151
152 case TMR_STOP:
153 tmr_running = 0;
154 break;
155
156 case TMR_CONTINUE:
157 tmr_running = 1;
158 break;
159
160 case TMR_TEMPO:
161 if (parm)
162 {
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;
171 }
172 break;
173
174 case TMR_ECHO:
175 seq_copy_to_input(event, 8);
176 break;
177
178 default:;
179 }
180
181 return TIMER_NOT_ARMED;
182}
183
184static unsigned long
185def_tmr_get_time(int dev)
186{
187 if (!opened)
188 return 0;
189
190 return curr_ticks;
191}
192
193/* same as sound_timer.c:timer_ioctl!? */
194static int def_tmr_ioctl(int dev, unsigned int cmd, void __user *arg)
195{
196 int __user *p = arg;
197 int val;
198
199 switch (cmd) {
200 case SNDCTL_TMR_SOURCE:
201 return __put_user(TMR_INTERNAL, p);
202
203 case SNDCTL_TMR_START:
204 tmr_reset();
205 tmr_running = 1;
206 return 0;
207
208 case SNDCTL_TMR_STOP:
209 tmr_running = 0;
210 return 0;
211
212 case SNDCTL_TMR_CONTINUE:
213 tmr_running = 1;
214 return 0;
215
216 case SNDCTL_TMR_TIMEBASE:
217 if (__get_user(val, p))
218 return -EFAULT;
219 if (val) {
220 if (val < 1)
221 val = 1;
222 if (val > 1000)
223 val = 1000;
224 curr_timebase = val;
225 }
226 return __put_user(curr_timebase, p);
227
228 case SNDCTL_TMR_TEMPO:
229 if (__get_user(val, p))
230 return -EFAULT;
231 if (val) {
232 if (val < 8)
233 val = 8;
234 if (val > 250)
235 val = 250;
236 tmr_offs = tmr_ctr;
237 ticks_offs += tmr2ticks(tmr_ctr);
238 tmr_ctr = 0;
239 curr_tempo = val;
240 reprogram_timer();
241 }
242 return __put_user(curr_tempo, p);
243
244 case SNDCTL_SEQ_CTRLRATE:
245 if (__get_user(val, p))
246 return -EFAULT;
247 if (val != 0) /* Can't change */
248 return -EINVAL;
249 val = ((curr_tempo * curr_timebase) + 30) / 60;
250 return __put_user(val, p);
251
252 case SNDCTL_SEQ_GETTIME:
253 return __put_user(curr_ticks, p);
254
255 case SNDCTL_TMR_METRONOME:
256 /* NOP */
257 break;
258
259 default:;
260 }
261 return -EINVAL;
262}
263
264static void
265def_tmr_arm(int dev, long time)
266{
267 if (time < 0)
268 time = curr_ticks + 1;
269 else if (time <= curr_ticks) /* It's the time */
270 return;
271
272 next_event_time = prev_event_time = time;
273
274 return;
275}
276
277struct sound_timer_operations default_sound_timer =
278{
279 .owner = THIS_MODULE,
280 .info = {"System clock", 0},
281 .priority = 0, /* Priority */
282 .devlink = 0, /* Local device link */
283 .open = def_tmr_open,
284 .close = def_tmr_close,
285 .event = def_tmr_event,
286 .get_time = def_tmr_get_time,
287 .ioctl = def_tmr_ioctl,
288 .arm_timer = def_tmr_arm
289};