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
);
58 rec
->oss_timebase
= 100;
67 * if no more timer exists, stop the queue.
70 snd_seq_oss_timer_delete(struct seq_oss_timer
*rec
)
73 snd_seq_oss_timer_stop(rec
);
80 * process one timing event
81 * return 1 : event proceseed -- skip this event
82 * 0 : not a timer event -- enqueue this event
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
) {
92 parm
+= rec
->cur_tick
;
94 /* continue to next */
98 } else if (parm
>= rec
->cur_tick
) {
100 rec
->cur_tick
= parm
;
102 return 1; /* skip this event */
105 snd_seq_oss_timer_start(rec
);
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
;
125 * convert tempo units
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
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
));
145 ev
.source
.client
= dp
->cseq
;
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
;
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);
184 snd_seq_oss_timer_stop(struct seq_oss_timer
*timer
)
186 if (! timer
->running
)
188 send_timer_event(timer
->dp
, SNDRV_SEQ_EVENT_STOP
, 0);
198 snd_seq_oss_timer_continue(struct seq_oss_timer
*timer
)
202 send_timer_event(timer
->dp
, SNDRV_SEQ_EVENT_CONTINUE
, 0);
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
);
221 send_timer_event(timer
->dp
, SNDRV_SEQ_EVENT_TEMPO
, timer
->tempo
);
230 snd_seq_oss_timer_ioctl(struct seq_oss_timer
*timer
, unsigned int cmd
, int __user
*arg
)
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
))
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 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
))
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
))
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
);
275 case SNDCTL_TMR_METRONOME
:
276 case SNDCTL_TMR_SELECT
:
277 case SNDCTL_TMR_SOURCE
:
278 debug_printk(("timer XXX\n"));