1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OSS compatible sequencer driver
5 * Timer control routines
7 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
10 #include "seq_oss_timer.h"
11 #include "seq_oss_event.h"
12 #include <sound/seq_oss_legacy.h>
13 #include <linux/slab.h>
17 #define MIN_OSS_TEMPO 8
18 #define MAX_OSS_TEMPO 360
19 #define MIN_OSS_TIMEBASE 1
20 #define MAX_OSS_TIMEBASE 1000
24 static void calc_alsa_tempo(struct seq_oss_timer
*timer
);
25 static int send_timer_event(struct seq_oss_devinfo
*dp
, int type
, int value
);
29 * create and register a new timer.
30 * if queue is not started yet, start it.
32 struct seq_oss_timer
*
33 snd_seq_oss_timer_new(struct seq_oss_devinfo
*dp
)
35 struct seq_oss_timer
*rec
;
37 rec
= kzalloc(sizeof(*rec
), GFP_KERNEL
);
46 rec
->oss_timebase
= 100;
55 * if no more timer exists, stop the queue.
58 snd_seq_oss_timer_delete(struct seq_oss_timer
*rec
)
61 snd_seq_oss_timer_stop(rec
);
68 * process one timing event
69 * return 1 : event proceseed -- skip this event
70 * 0 : not a timer event -- enqueue this event
73 snd_seq_oss_process_timer_event(struct seq_oss_timer
*rec
, union evrec
*ev
)
75 abstime_t parm
= ev
->t
.time
;
77 if (ev
->t
.code
== EV_TIMING
) {
80 parm
+= rec
->cur_tick
;
86 } else if (parm
>= rec
->cur_tick
) {
90 return 1; /* skip this event */
93 snd_seq_oss_timer_start(rec
);
97 } else if (ev
->s
.code
== SEQ_WAIT
) {
98 /* time = from 1 to 3 bytes */
99 parm
= (ev
->echo
>> 8) & 0xffffff;
100 if (parm
> rec
->cur_tick
) {
101 /* set next event time */
102 rec
->cur_tick
= parm
;
113 * convert tempo units
116 calc_alsa_tempo(struct seq_oss_timer
*timer
)
118 timer
->tempo
= (60 * 1000000) / timer
->oss_tempo
;
119 timer
->ppq
= timer
->oss_timebase
;
124 * dispatch a timer event
127 send_timer_event(struct seq_oss_devinfo
*dp
, int type
, int value
)
129 struct snd_seq_event ev
;
131 memset(&ev
, 0, sizeof(ev
));
133 ev
.source
.client
= dp
->cseq
;
135 ev
.dest
.client
= SNDRV_SEQ_CLIENT_SYSTEM
;
136 ev
.dest
.port
= SNDRV_SEQ_PORT_SYSTEM_TIMER
;
137 ev
.queue
= dp
->queue
;
138 ev
.data
.queue
.queue
= dp
->queue
;
139 ev
.data
.queue
.param
.value
= value
;
140 return snd_seq_kernel_client_dispatch(dp
->cseq
, &ev
, 1, 0);
144 * set queue tempo and start queue
147 snd_seq_oss_timer_start(struct seq_oss_timer
*timer
)
149 struct seq_oss_devinfo
*dp
= timer
->dp
;
150 struct snd_seq_queue_tempo tmprec
;
153 snd_seq_oss_timer_stop(timer
);
155 memset(&tmprec
, 0, sizeof(tmprec
));
156 tmprec
.queue
= dp
->queue
;
157 tmprec
.ppq
= timer
->ppq
;
158 tmprec
.tempo
= timer
->tempo
;
159 snd_seq_set_queue_tempo(dp
->cseq
, &tmprec
);
161 send_timer_event(dp
, SNDRV_SEQ_EVENT_START
, 0);
172 snd_seq_oss_timer_stop(struct seq_oss_timer
*timer
)
174 if (! timer
->running
)
176 send_timer_event(timer
->dp
, SNDRV_SEQ_EVENT_STOP
, 0);
186 snd_seq_oss_timer_continue(struct seq_oss_timer
*timer
)
190 send_timer_event(timer
->dp
, SNDRV_SEQ_EVENT_CONTINUE
, 0);
200 snd_seq_oss_timer_tempo(struct seq_oss_timer
*timer
, int value
)
202 if (value
< MIN_OSS_TEMPO
)
203 value
= MIN_OSS_TEMPO
;
204 else if (value
> MAX_OSS_TEMPO
)
205 value
= MAX_OSS_TEMPO
;
206 timer
->oss_tempo
= value
;
207 calc_alsa_tempo(timer
);
209 send_timer_event(timer
->dp
, SNDRV_SEQ_EVENT_TEMPO
, timer
->tempo
);
218 snd_seq_oss_timer_ioctl(struct seq_oss_timer
*timer
, unsigned int cmd
, int __user
*arg
)
222 if (cmd
== SNDCTL_SEQ_CTRLRATE
) {
223 /* if *arg == 0, just return the current rate */
224 if (get_user(value
, arg
))
228 value
= ((timer
->oss_tempo
* timer
->oss_timebase
) + 30) / 60;
229 return put_user(value
, arg
) ? -EFAULT
: 0;
232 if (timer
->dp
->seq_mode
== SNDRV_SEQ_OSS_MODE_SYNTH
)
236 case SNDCTL_TMR_START
:
237 return snd_seq_oss_timer_start(timer
);
238 case SNDCTL_TMR_STOP
:
239 return snd_seq_oss_timer_stop(timer
);
240 case SNDCTL_TMR_CONTINUE
:
241 return snd_seq_oss_timer_continue(timer
);
242 case SNDCTL_TMR_TEMPO
:
243 if (get_user(value
, arg
))
245 return snd_seq_oss_timer_tempo(timer
, value
);
246 case SNDCTL_TMR_TIMEBASE
:
247 if (get_user(value
, arg
))
249 if (value
< MIN_OSS_TIMEBASE
)
250 value
= MIN_OSS_TIMEBASE
;
251 else if (value
> MAX_OSS_TIMEBASE
)
252 value
= MAX_OSS_TIMEBASE
;
253 timer
->oss_timebase
= value
;
254 calc_alsa_tempo(timer
);
257 case SNDCTL_TMR_METRONOME
:
258 case SNDCTL_TMR_SELECT
:
259 case SNDCTL_TMR_SOURCE
: