rtc: rtc-omap footprint shrinkage
[linux-ginger.git] / sound / core / seq / oss / seq_oss_timer.c
blobc440fdacec93dc752caff10a0c312f343e0e98b2
1 /*
2 * OSS compatible sequencer driver
4 * Timer control routines
6 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "seq_oss_timer.h"
24 #include "seq_oss_event.h"
25 #include <sound/seq_oss_legacy.h>
29 #define MIN_OSS_TEMPO 8
30 #define MAX_OSS_TEMPO 360
31 #define MIN_OSS_TIMEBASE 1
32 #define MAX_OSS_TIMEBASE 1000
36 static void calc_alsa_tempo(struct seq_oss_timer *timer);
37 static int send_timer_event(struct seq_oss_devinfo *dp, int type, int value);
41 * create and register a new timer.
42 * if queue is not started yet, start it.
44 struct seq_oss_timer *
45 snd_seq_oss_timer_new(struct seq_oss_devinfo *dp)
47 struct seq_oss_timer *rec;
49 rec = kzalloc(sizeof(*rec), GFP_KERNEL);
50 if (rec == NULL)
51 return NULL;
53 rec->dp = dp;
54 rec->cur_tick = 0;
55 rec->realtime = 0;
56 rec->running = 0;
57 rec->oss_tempo = 60;
58 rec->oss_timebase = 100;
59 calc_alsa_tempo(rec);
61 return rec;
66 * delete timer.
67 * if no more timer exists, stop the queue.
69 void
70 snd_seq_oss_timer_delete(struct seq_oss_timer *rec)
72 if (rec) {
73 snd_seq_oss_timer_stop(rec);
74 kfree(rec);
80 * process one timing event
81 * return 1 : event proceseed -- skip this event
82 * 0 : not a timer event -- enqueue this event
84 int
85 snd_seq_oss_process_timer_event(struct seq_oss_timer *rec, union evrec *ev)
87 abstime_t parm = ev->t.time;
89 if (ev->t.code == EV_TIMING) {
90 switch (ev->t.cmd) {
91 case TMR_WAIT_REL:
92 parm += rec->cur_tick;
93 rec->realtime = 0;
94 /* continue to next */
95 case TMR_WAIT_ABS:
96 if (parm == 0) {
97 rec->realtime = 1;
98 } else if (parm >= rec->cur_tick) {
99 rec->realtime = 0;
100 rec->cur_tick = parm;
102 return 1; /* skip this event */
104 case TMR_START:
105 snd_seq_oss_timer_start(rec);
106 return 1;
109 } else if (ev->s.code == SEQ_WAIT) {
110 /* time = from 1 to 3 bytes */
111 parm = (ev->echo >> 8) & 0xffffff;
112 if (parm > rec->cur_tick) {
113 /* set next event time */
114 rec->cur_tick = parm;
115 rec->realtime = 0;
117 return 1;
120 return 0;
125 * convert tempo units
127 static void
128 calc_alsa_tempo(struct seq_oss_timer *timer)
130 timer->tempo = (60 * 1000000) / timer->oss_tempo;
131 timer->ppq = timer->oss_timebase;
136 * dispatch a timer event
138 static int
139 send_timer_event(struct seq_oss_devinfo *dp, int type, int value)
141 struct snd_seq_event ev;
143 memset(&ev, 0, sizeof(ev));
144 ev.type = type;
145 ev.source.client = dp->cseq;
146 ev.source.port = 0;
147 ev.dest.client = SNDRV_SEQ_CLIENT_SYSTEM;
148 ev.dest.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
149 ev.queue = dp->queue;
150 ev.data.queue.queue = dp->queue;
151 ev.data.queue.param.value = value;
152 return snd_seq_kernel_client_dispatch(dp->cseq, &ev, 1, 0);
156 * set queue tempo and start queue
159 snd_seq_oss_timer_start(struct seq_oss_timer *timer)
161 struct seq_oss_devinfo *dp = timer->dp;
162 struct snd_seq_queue_tempo tmprec;
164 if (timer->running)
165 snd_seq_oss_timer_stop(timer);
167 memset(&tmprec, 0, sizeof(tmprec));
168 tmprec.queue = dp->queue;
169 tmprec.ppq = timer->ppq;
170 tmprec.tempo = timer->tempo;
171 snd_seq_set_queue_tempo(dp->cseq, &tmprec);
173 send_timer_event(dp, SNDRV_SEQ_EVENT_START, 0);
174 timer->running = 1;
175 timer->cur_tick = 0;
176 return 0;
181 * stop queue
184 snd_seq_oss_timer_stop(struct seq_oss_timer *timer)
186 if (! timer->running)
187 return 0;
188 send_timer_event(timer->dp, SNDRV_SEQ_EVENT_STOP, 0);
189 timer->running = 0;
190 return 0;
195 * continue queue
198 snd_seq_oss_timer_continue(struct seq_oss_timer *timer)
200 if (timer->running)
201 return 0;
202 send_timer_event(timer->dp, SNDRV_SEQ_EVENT_CONTINUE, 0);
203 timer->running = 1;
204 return 0;
209 * change queue tempo
212 snd_seq_oss_timer_tempo(struct seq_oss_timer *timer, int value)
214 if (value < MIN_OSS_TEMPO)
215 value = MIN_OSS_TEMPO;
216 else if (value > MAX_OSS_TEMPO)
217 value = MAX_OSS_TEMPO;
218 timer->oss_tempo = value;
219 calc_alsa_tempo(timer);
220 if (timer->running)
221 send_timer_event(timer->dp, SNDRV_SEQ_EVENT_TEMPO, timer->tempo);
222 return 0;
227 * ioctls
230 snd_seq_oss_timer_ioctl(struct seq_oss_timer *timer, unsigned int cmd, int __user *arg)
232 int value;
234 if (cmd == SNDCTL_SEQ_CTRLRATE) {
235 debug_printk(("ctrl rate\n"));
236 /* if *arg == 0, just return the current rate */
237 if (get_user(value, arg))
238 return -EFAULT;
239 if (value)
240 return -EINVAL;
241 value = ((timer->oss_tempo * timer->oss_timebase) + 30) / 60;
242 return put_user(value, arg) ? -EFAULT : 0;
245 if (timer->dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH)
246 return 0;
248 switch (cmd) {
249 case SNDCTL_TMR_START:
250 debug_printk(("timer start\n"));
251 return snd_seq_oss_timer_start(timer);
252 case SNDCTL_TMR_STOP:
253 debug_printk(("timer stop\n"));
254 return snd_seq_oss_timer_stop(timer);
255 case SNDCTL_TMR_CONTINUE:
256 debug_printk(("timer continue\n"));
257 return snd_seq_oss_timer_continue(timer);
258 case SNDCTL_TMR_TEMPO:
259 debug_printk(("timer tempo\n"));
260 if (get_user(value, arg))
261 return -EFAULT;
262 return snd_seq_oss_timer_tempo(timer, value);
263 case SNDCTL_TMR_TIMEBASE:
264 debug_printk(("timer timebase\n"));
265 if (get_user(value, arg))
266 return -EFAULT;
267 if (value < MIN_OSS_TIMEBASE)
268 value = MIN_OSS_TIMEBASE;
269 else if (value > MAX_OSS_TIMEBASE)
270 value = MAX_OSS_TIMEBASE;
271 timer->oss_timebase = value;
272 calc_alsa_tempo(timer);
273 return 0;
275 case SNDCTL_TMR_METRONOME:
276 case SNDCTL_TMR_SELECT:
277 case SNDCTL_TMR_SOURCE:
278 debug_printk(("timer XXX\n"));
279 /* not supported */
280 return 0;
282 return 0;