1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OSS compatible sequencer driver
5 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
8 #include "seq_oss_device.h"
9 #include "seq_oss_synth.h"
10 #include "seq_oss_midi.h"
11 #include "seq_oss_event.h"
12 #include "seq_oss_timer.h"
13 #include <sound/seq_oss_legacy.h>
14 #include "seq_oss_readq.h"
15 #include "seq_oss_writeq.h"
16 #include <linux/nospec.h>
22 static int extended_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev);
23 static int chn_voice_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
24 static int chn_common_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
25 static int timing_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
26 static int local_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
27 static int old_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev);
28 static int note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev);
29 static int note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev);
30 static int set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev);
31 static int set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev);
32 static int set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev);
36 * convert an OSS event to ALSA event
38 * non-zero : invalid - ignored
42 snd_seq_oss_process_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
46 return extended_event(dp, q, ev);
49 return chn_voice_event(dp, q, ev);
52 return chn_common_event(dp, q, ev);
55 return timing_event(dp, q, ev);
58 return local_event(dp, q, ev);
61 return snd_seq_oss_synth_sysex(dp, q->x.dev, q->x.buf, ev);
64 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
67 if (! is_write_mode(dp->file_mode))
69 if (snd_seq_oss_midi_open(dp, q->s.dev, SNDRV_SEQ_OSS_FILE_WRITE))
71 if (snd_seq_oss_midi_filemode(dp, q->s.dev) & SNDRV_SEQ_OSS_FILE_WRITE)
72 return snd_seq_oss_midi_putc(dp, q->s.dev, q->s.parm1, ev);
76 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
78 return set_echo_event(dp, q, ev);
81 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
83 return snd_seq_oss_synth_raw_event(dp, q->c[1], q->c, ev);
86 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
88 return old_event(dp, q, ev);
93 /* old type events: mode1 only */
95 old_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
99 return note_off_event(dp, 0, q->n.chn, q->n.note, q->n.vel, ev);
102 return note_on_event(dp, 0, q->n.chn, q->n.note, q->n.vel, ev);
109 return set_control_event(dp, 0, SNDRV_SEQ_EVENT_PGMCHANGE,
110 q->n.chn, 0, q->n.note, ev);
113 return snd_seq_oss_timer_reset(dp->timer);
119 /* 8bytes extended event: mode1 only */
121 extended_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
127 return note_off_event(dp, q->e.dev, q->e.chn, q->e.p1, q->e.p2, ev);
130 return note_on_event(dp, q->e.dev, q->e.chn, q->e.p1, q->e.p2, ev);
133 return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_PGMCHANGE,
134 q->e.chn, 0, q->e.p1, ev);
137 return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CHANPRESS,
138 q->e.chn, 0, q->e.p1, ev);
141 /* convert -128:127 to 0:127 */
143 val = (val + 128) / 2;
144 return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CONTROLLER,
145 q->e.chn, CTL_PAN, val, ev);
148 val = ((short)q->e.p3 << 8) | (short)q->e.p2;
150 case CTRL_PITCH_BENDER: /* SEQ1 V2 control */
152 return set_control_event(dp, q->e.dev,
153 SNDRV_SEQ_EVENT_PITCHBEND,
154 q->e.chn, 0, val, ev);
155 case CTRL_PITCH_BENDER_RANGE:
156 /* conversion: 100/semitone -> 128/semitone */
157 return set_control_event(dp, q->e.dev,
158 SNDRV_SEQ_EVENT_REGPARAM,
159 q->e.chn, 0, val*128/100, ev);
161 return set_control_event(dp, q->e.dev,
162 SNDRV_SEQ_EVENT_CONTROL14,
163 q->e.chn, q->e.p1, val, ev);
167 return snd_seq_oss_synth_raw_event(dp, q->e.dev, q->c, ev);
173 /* channel voice events: mode1 and 2 */
175 chn_voice_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
181 return note_on_event(dp, q->v.dev, q->v.chn, q->v.note, q->v.parm, ev);
184 return note_off_event(dp, q->v.dev, q->v.chn, q->v.note, q->v.parm, ev);
186 case MIDI_KEY_PRESSURE:
187 return set_note_event(dp, q->v.dev, SNDRV_SEQ_EVENT_KEYPRESS,
188 q->v.chn, q->v.note, q->v.parm, ev);
194 /* channel common events: mode1 and 2 */
196 chn_common_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
201 case MIDI_PGM_CHANGE:
202 return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_PGMCHANGE,
203 q->l.chn, 0, q->l.p1, ev);
205 case MIDI_CTL_CHANGE:
206 return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_CONTROLLER,
207 q->l.chn, q->l.p1, q->l.val, ev);
209 case MIDI_PITCH_BEND:
210 /* conversion: 0:0x3fff -> -0x2000:0x1fff */
211 return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_PITCHBEND,
212 q->l.chn, 0, q->l.val - 8192, ev);
214 case MIDI_CHN_PRESSURE:
215 return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_CHANPRESS,
216 q->l.chn, 0, q->l.val, ev);
221 /* timer events: mode1 and mode2 */
223 timing_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
227 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
228 return set_echo_event(dp, q, ev);
231 memset(&tmp, 0, sizeof(tmp));
232 /* XXX: only for little-endian! */
233 tmp.echo = (q->t.time << 8) | SEQ_ECHO;
234 return set_echo_event(dp, &tmp, ev);
239 return snd_seq_oss_timer_stop(dp->timer);
244 return snd_seq_oss_timer_continue(dp->timer);
249 return snd_seq_oss_timer_tempo(dp->timer, q->t.time);
256 /* local events: mode1 and 2 */
258 local_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
264 * process note-on event for OSS synth
265 * three different modes are available:
266 * - SNDRV_SEQ_OSS_PROCESS_EVENTS (for one-voice per channel mode)
267 * Accept note 255 as volume change.
268 * - SNDRV_SEQ_OSS_PASS_EVENTS
269 * Pass all events to lowlevel driver anyway
270 * - SNDRV_SEQ_OSS_PROCESS_KEYPRESS (mostly for Emu8000)
271 * Use key-pressure if note >= 128
274 note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev)
276 struct seq_oss_synthinfo *info;
278 info = snd_seq_oss_synth_info(dp, dev);
282 switch (info->arg.event_passing) {
283 case SNDRV_SEQ_OSS_PROCESS_EVENTS:
284 if (! info->ch || ch < 0 || ch >= info->nr_voices) {
286 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
289 ch = array_index_nospec(ch, info->nr_voices);
290 if (note == 255 && info->ch[ch].note >= 0) {
294 /* set volume to zero -- note off */
295 // type = SNDRV_SEQ_EVENT_NOTEOFF;
297 if (info->ch[ch].vel)
298 /* sample already started -- volume change */
299 type = SNDRV_SEQ_EVENT_KEYPRESS;
301 /* sample not started -- start now */
302 type = SNDRV_SEQ_EVENT_NOTEON;
303 info->ch[ch].vel = vel;
304 return set_note_event(dp, dev, type, ch, info->ch[ch].note, vel, ev);
305 } else if (note >= 128)
306 return -EINVAL; /* invalid */
308 if (note != info->ch[ch].note && info->ch[ch].note >= 0)
309 /* note changed - note off at beginning */
310 set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, info->ch[ch].note, 0, ev);
311 /* set current status */
312 info->ch[ch].note = note;
313 info->ch[ch].vel = vel;
314 if (vel) /* non-zero velocity - start the note now */
315 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
318 case SNDRV_SEQ_OSS_PASS_EVENTS:
319 /* pass the event anyway */
320 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
322 case SNDRV_SEQ_OSS_PROCESS_KEYPRESS:
323 if (note >= 128) /* key pressure: shifted by 128 */
324 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_KEYPRESS, ch, note - 128, vel, ev);
325 else /* normal note-on event */
326 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
332 * process note-off event for OSS synth
335 note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev)
337 struct seq_oss_synthinfo *info;
339 info = snd_seq_oss_synth_info(dp, dev);
343 switch (info->arg.event_passing) {
344 case SNDRV_SEQ_OSS_PROCESS_EVENTS:
345 if (! info->ch || ch < 0 || ch >= info->nr_voices) {
347 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
350 ch = array_index_nospec(ch, info->nr_voices);
351 if (info->ch[ch].note >= 0) {
352 note = info->ch[ch].note;
353 info->ch[ch].vel = 0;
354 info->ch[ch].note = -1;
355 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev);
357 return -EINVAL; /* invalid */
359 case SNDRV_SEQ_OSS_PASS_EVENTS:
360 case SNDRV_SEQ_OSS_PROCESS_KEYPRESS:
361 /* pass the event anyway */
362 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev);
369 * create a note event
372 set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev)
374 if (!snd_seq_oss_synth_info(dp, dev))
378 snd_seq_oss_synth_addr(dp, dev, ev);
379 ev->data.note.channel = ch;
380 ev->data.note.note = note;
381 ev->data.note.velocity = vel;
387 * create a control event
390 set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev)
392 if (!snd_seq_oss_synth_info(dp, dev))
396 snd_seq_oss_synth_addr(dp, dev, ev);
397 ev->data.control.channel = ch;
398 ev->data.control.param = param;
399 ev->data.control.value = val;
405 * create an echo event
408 set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev)
410 ev->type = SNDRV_SEQ_EVENT_ECHO;
411 /* echo back to itself */
412 snd_seq_oss_fill_addr(dp, ev, dp->addr.client, dp->addr.port);
413 memcpy(&ev->data, rec, LONG_EVENT_SIZE);
418 * event input callback from ALSA sequencer:
419 * the echo event is processed here.
422 snd_seq_oss_event_input(struct snd_seq_event *ev, int direct, void *private_data,
425 struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
428 if (ev->type != SNDRV_SEQ_EVENT_ECHO)
429 return snd_seq_oss_midi_input(ev, direct, private_data);
431 if (ev->source.client != dp->cseq)
432 return 0; /* ignored */
434 rec = (union evrec*)&ev->data;
435 if (rec->s.code == SEQ_SYNCTIMER) {
437 snd_seq_oss_writeq_wakeup(dp->writeq, rec->t.time);
440 /* echo back event */
441 if (dp->readq == NULL)
443 snd_seq_oss_readq_put_event(dp->readq, rec);