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>
26 #include <linux/slab.h>
30 #define MIN_OSS_TEMPO 8
31 #define MAX_OSS_TEMPO 360
32 #define MIN_OSS_TIMEBASE 1
33 #define MAX_OSS_TIMEBASE 1000
37 static void calc_alsa_tempo(struct seq_oss_timer *timer);
38 static int send_timer_event(struct seq_oss_devinfo *dp, int type, int value);
42 * create and register a new timer.
43 * if queue is not started yet, start it.
45 struct seq_oss_timer *
46 snd_seq_oss_timer_new(struct seq_oss_devinfo *dp)
48 struct seq_oss_timer *rec;
50 rec = kzalloc(sizeof(*rec), GFP_KERNEL);
59 rec->oss_timebase = 100;
68 * if no more timer exists, stop the queue.
71 snd_seq_oss_timer_delete(struct seq_oss_timer *rec)
74 snd_seq_oss_timer_stop(rec);
81 * process one timing event
82 * return 1 : event proceseed -- skip this event
83 * 0 : not a timer event -- enqueue this event
86 snd_seq_oss_process_timer_event(struct seq_oss_timer *rec, union evrec *ev)
88 abstime_t parm = ev->t.time;
90 if (ev->t.code == EV_TIMING) {
93 parm += rec->cur_tick;
95 /* fall through and continue to next */
99 } else if (parm >= rec->cur_tick) {
101 rec->cur_tick = parm;
103 return 1; /* skip this event */
106 snd_seq_oss_timer_start(rec);
110 } else if (ev->s.code == SEQ_WAIT) {
111 /* time = from 1 to 3 bytes */
112 parm = (ev->echo >> 8) & 0xffffff;
113 if (parm > rec->cur_tick) {
114 /* set next event time */
115 rec->cur_tick = parm;
126 * convert tempo units
129 calc_alsa_tempo(struct seq_oss_timer *timer)
131 timer->tempo = (60 * 1000000) / timer->oss_tempo;
132 timer->ppq = timer->oss_timebase;
137 * dispatch a timer event
140 send_timer_event(struct seq_oss_devinfo *dp, int type, int value)
142 struct snd_seq_event ev;
144 memset(&ev, 0, sizeof(ev));
146 ev.source.client = dp->cseq;
148 ev.dest.client = SNDRV_SEQ_CLIENT_SYSTEM;
149 ev.dest.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
150 ev.queue = dp->queue;
151 ev.data.queue.queue = dp->queue;
152 ev.data.queue.param.value = value;
153 return snd_seq_kernel_client_dispatch(dp->cseq, &ev, 1, 0);
157 * set queue tempo and start queue
160 snd_seq_oss_timer_start(struct seq_oss_timer *timer)
162 struct seq_oss_devinfo *dp = timer->dp;
163 struct snd_seq_queue_tempo tmprec;
166 snd_seq_oss_timer_stop(timer);
168 memset(&tmprec, 0, sizeof(tmprec));
169 tmprec.queue = dp->queue;
170 tmprec.ppq = timer->ppq;
171 tmprec.tempo = timer->tempo;
172 snd_seq_set_queue_tempo(dp->cseq, &tmprec);
174 send_timer_event(dp, SNDRV_SEQ_EVENT_START, 0);
185 snd_seq_oss_timer_stop(struct seq_oss_timer *timer)
187 if (! timer->running)
189 send_timer_event(timer->dp, SNDRV_SEQ_EVENT_STOP, 0);
199 snd_seq_oss_timer_continue(struct seq_oss_timer *timer)
203 send_timer_event(timer->dp, SNDRV_SEQ_EVENT_CONTINUE, 0);
213 snd_seq_oss_timer_tempo(struct seq_oss_timer *timer, int value)
215 if (value < MIN_OSS_TEMPO)
216 value = MIN_OSS_TEMPO;
217 else if (value > MAX_OSS_TEMPO)
218 value = MAX_OSS_TEMPO;
219 timer->oss_tempo = value;
220 calc_alsa_tempo(timer);
222 send_timer_event(timer->dp, SNDRV_SEQ_EVENT_TEMPO, timer->tempo);
231 snd_seq_oss_timer_ioctl(struct seq_oss_timer *timer, unsigned int cmd, int __user *arg)
235 if (cmd == SNDCTL_SEQ_CTRLRATE) {
236 /* if *arg == 0, just return the current rate */
237 if (get_user(value, arg))
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)
249 case SNDCTL_TMR_START:
250 return snd_seq_oss_timer_start(timer);
251 case SNDCTL_TMR_STOP:
252 return snd_seq_oss_timer_stop(timer);
253 case SNDCTL_TMR_CONTINUE:
254 return snd_seq_oss_timer_continue(timer);
255 case SNDCTL_TMR_TEMPO:
256 if (get_user(value, arg))
258 return snd_seq_oss_timer_tempo(timer, value);
259 case SNDCTL_TMR_TIMEBASE:
260 if (get_user(value, arg))
262 if (value < MIN_OSS_TIMEBASE)
263 value = MIN_OSS_TIMEBASE;
264 else if (value > MAX_OSS_TIMEBASE)
265 value = MAX_OSS_TIMEBASE;
266 timer->oss_timebase = value;
267 calc_alsa_tempo(timer);
270 case SNDCTL_TMR_METRONOME:
271 case SNDCTL_TMR_SELECT:
272 case SNDCTL_TMR_SOURCE: