Linux 4.16.11
[linux/fpc-iii.git] / sound / synth / emux / emux_oss.c
blob764ff4bc20892cc23a9a2b5054c8bf77f817a1f4
1 /*
2 * Interface for OSS sequencer emulation
4 * Copyright (C) 1999 Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Changes
21 * 19990227 Steve Ratcliffe Made separate file and merged in latest
22 * midi emulation.
26 #include <linux/export.h>
27 #include <linux/uaccess.h>
28 #include <sound/core.h>
29 #include "emux_voice.h"
30 #include <sound/asoundef.h>
32 static int snd_emux_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
33 static int snd_emux_close_seq_oss(struct snd_seq_oss_arg *arg);
34 static int snd_emux_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
35 unsigned long ioarg);
36 static int snd_emux_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
37 const char __user *buf, int offs, int count);
38 static int snd_emux_reset_seq_oss(struct snd_seq_oss_arg *arg);
39 static int snd_emux_event_oss_input(struct snd_seq_event *ev, int direct,
40 void *private, int atomic, int hop);
41 static void reset_port_mode(struct snd_emux_port *port, int midi_mode);
42 static void emuspec_control(struct snd_emux *emu, struct snd_emux_port *port,
43 int cmd, unsigned char *event, int atomic, int hop);
44 static void gusspec_control(struct snd_emux *emu, struct snd_emux_port *port,
45 int cmd, unsigned char *event, int atomic, int hop);
46 static void fake_event(struct snd_emux *emu, struct snd_emux_port *port,
47 int ch, int param, int val, int atomic, int hop);
49 /* operators */
50 static struct snd_seq_oss_callback oss_callback = {
51 .owner = THIS_MODULE,
52 .open = snd_emux_open_seq_oss,
53 .close = snd_emux_close_seq_oss,
54 .ioctl = snd_emux_ioctl_seq_oss,
55 .load_patch = snd_emux_load_patch_seq_oss,
56 .reset = snd_emux_reset_seq_oss,
61 * register OSS synth
64 void
65 snd_emux_init_seq_oss(struct snd_emux *emu)
67 struct snd_seq_oss_reg *arg;
68 struct snd_seq_device *dev;
70 /* using device#1 here for avoiding conflicts with OPL3 */
71 if (snd_seq_device_new(emu->card, 1, SNDRV_SEQ_DEV_ID_OSS,
72 sizeof(struct snd_seq_oss_reg), &dev) < 0)
73 return;
75 emu->oss_synth = dev;
76 strcpy(dev->name, emu->name);
77 arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
78 arg->type = SYNTH_TYPE_SAMPLE;
79 arg->subtype = SAMPLE_TYPE_AWE32;
80 arg->nvoices = emu->max_voices;
81 arg->oper = oss_callback;
82 arg->private_data = emu;
84 /* register to OSS synth table */
85 snd_device_register(emu->card, dev);
90 * unregister
92 void
93 snd_emux_detach_seq_oss(struct snd_emux *emu)
95 if (emu->oss_synth) {
96 snd_device_free(emu->card, emu->oss_synth);
97 emu->oss_synth = NULL;
102 /* use port number as a unique soundfont client number */
103 #define SF_CLIENT_NO(p) ((p) + 0x1000)
106 * open port for OSS sequencer
108 static int
109 snd_emux_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
111 struct snd_emux *emu;
112 struct snd_emux_port *p;
113 struct snd_seq_port_callback callback;
114 char tmpname[64];
116 emu = closure;
117 if (snd_BUG_ON(!arg || !emu))
118 return -ENXIO;
120 if (!snd_emux_inc_count(emu))
121 return -EFAULT;
123 memset(&callback, 0, sizeof(callback));
124 callback.owner = THIS_MODULE;
125 callback.event_input = snd_emux_event_oss_input;
127 sprintf(tmpname, "%s OSS Port", emu->name);
128 p = snd_emux_create_port(emu, tmpname, 32,
129 1, &callback);
130 if (p == NULL) {
131 snd_printk(KERN_ERR "can't create port\n");
132 snd_emux_dec_count(emu);
133 return -ENOMEM;
136 /* fill the argument data */
137 arg->private_data = p;
138 arg->addr.client = p->chset.client;
139 arg->addr.port = p->chset.port;
140 p->oss_arg = arg;
142 reset_port_mode(p, arg->seq_mode);
144 snd_emux_reset_port(p);
145 return 0;
149 #define DEFAULT_DRUM_FLAGS ((1<<9) | (1<<25))
152 * reset port mode
154 static void
155 reset_port_mode(struct snd_emux_port *port, int midi_mode)
157 if (midi_mode) {
158 port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_MIDI;
159 port->drum_flags = DEFAULT_DRUM_FLAGS;
160 port->volume_atten = 0;
161 port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_KEYPRESS;
162 } else {
163 port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_SYNTH;
164 port->drum_flags = 0;
165 port->volume_atten = 32;
166 port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
172 * close port
174 static int
175 snd_emux_close_seq_oss(struct snd_seq_oss_arg *arg)
177 struct snd_emux *emu;
178 struct snd_emux_port *p;
180 if (snd_BUG_ON(!arg))
181 return -ENXIO;
182 p = arg->private_data;
183 if (snd_BUG_ON(!p))
184 return -ENXIO;
186 emu = p->emu;
187 if (snd_BUG_ON(!emu))
188 return -ENXIO;
190 snd_emux_sounds_off_all(p);
191 snd_soundfont_close_check(emu->sflist, SF_CLIENT_NO(p->chset.port));
192 snd_seq_event_port_detach(p->chset.client, p->chset.port);
193 snd_emux_dec_count(emu);
195 return 0;
200 * load patch
202 static int
203 snd_emux_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
204 const char __user *buf, int offs, int count)
206 struct snd_emux *emu;
207 struct snd_emux_port *p;
208 int rc;
210 if (snd_BUG_ON(!arg))
211 return -ENXIO;
212 p = arg->private_data;
213 if (snd_BUG_ON(!p))
214 return -ENXIO;
216 emu = p->emu;
217 if (snd_BUG_ON(!emu))
218 return -ENXIO;
220 if (format == GUS_PATCH)
221 rc = snd_soundfont_load_guspatch(emu->sflist, buf, count,
222 SF_CLIENT_NO(p->chset.port));
223 else if (format == SNDRV_OSS_SOUNDFONT_PATCH) {
224 struct soundfont_patch_info patch;
225 if (count < (int)sizeof(patch))
226 return -EINVAL;
227 if (copy_from_user(&patch, buf, sizeof(patch)))
228 return -EFAULT;
229 if (patch.type >= SNDRV_SFNT_LOAD_INFO &&
230 patch.type <= SNDRV_SFNT_PROBE_DATA)
231 rc = snd_soundfont_load(emu->sflist, buf, count, SF_CLIENT_NO(p->chset.port));
232 else {
233 if (emu->ops.load_fx)
234 rc = emu->ops.load_fx(emu, patch.type, patch.optarg, buf, count);
235 else
236 rc = -EINVAL;
238 } else
239 rc = 0;
240 return rc;
245 * ioctl
247 static int
248 snd_emux_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg)
250 struct snd_emux_port *p;
251 struct snd_emux *emu;
253 if (snd_BUG_ON(!arg))
254 return -ENXIO;
255 p = arg->private_data;
256 if (snd_BUG_ON(!p))
257 return -ENXIO;
259 emu = p->emu;
260 if (snd_BUG_ON(!emu))
261 return -ENXIO;
263 switch (cmd) {
264 case SNDCTL_SEQ_RESETSAMPLES:
265 snd_soundfont_remove_samples(emu->sflist);
266 return 0;
268 case SNDCTL_SYNTH_MEMAVL:
269 if (emu->memhdr)
270 return snd_util_mem_avail(emu->memhdr);
271 return 0;
274 return 0;
279 * reset device
281 static int
282 snd_emux_reset_seq_oss(struct snd_seq_oss_arg *arg)
284 struct snd_emux_port *p;
286 if (snd_BUG_ON(!arg))
287 return -ENXIO;
288 p = arg->private_data;
289 if (snd_BUG_ON(!p))
290 return -ENXIO;
291 snd_emux_reset_port(p);
292 return 0;
297 * receive raw events: only SEQ_PRIVATE is accepted.
299 static int
300 snd_emux_event_oss_input(struct snd_seq_event *ev, int direct, void *private_data,
301 int atomic, int hop)
303 struct snd_emux *emu;
304 struct snd_emux_port *p;
305 unsigned char cmd, *data;
307 p = private_data;
308 if (snd_BUG_ON(!p))
309 return -EINVAL;
310 emu = p->emu;
311 if (snd_BUG_ON(!emu))
312 return -EINVAL;
313 if (ev->type != SNDRV_SEQ_EVENT_OSS)
314 return snd_emux_event_input(ev, direct, private_data, atomic, hop);
316 data = ev->data.raw8.d;
317 /* only SEQ_PRIVATE is accepted */
318 if (data[0] != 0xfe)
319 return 0;
320 cmd = data[2] & _EMUX_OSS_MODE_VALUE_MASK;
321 if (data[2] & _EMUX_OSS_MODE_FLAG)
322 emuspec_control(emu, p, cmd, data, atomic, hop);
323 else
324 gusspec_control(emu, p, cmd, data, atomic, hop);
325 return 0;
330 * OSS/AWE driver specific h/w controls
332 static void
333 emuspec_control(struct snd_emux *emu, struct snd_emux_port *port, int cmd,
334 unsigned char *event, int atomic, int hop)
336 int voice;
337 unsigned short p1;
338 short p2;
339 int i;
340 struct snd_midi_channel *chan;
342 voice = event[3];
343 if (voice < 0 || voice >= port->chset.max_channels)
344 chan = NULL;
345 else
346 chan = &port->chset.channels[voice];
348 p1 = *(unsigned short *) &event[4];
349 p2 = *(short *) &event[6];
351 switch (cmd) {
352 #if 0 /* don't do this atomically */
353 case _EMUX_OSS_REMOVE_LAST_SAMPLES:
354 snd_soundfont_remove_unlocked(emu->sflist);
355 break;
356 #endif
357 case _EMUX_OSS_SEND_EFFECT:
358 if (chan)
359 snd_emux_send_effect_oss(port, chan, p1, p2);
360 break;
362 case _EMUX_OSS_TERMINATE_ALL:
363 snd_emux_terminate_all(emu);
364 break;
366 case _EMUX_OSS_TERMINATE_CHANNEL:
367 /*snd_emux_mute_channel(emu, chan);*/
368 break;
369 case _EMUX_OSS_RESET_CHANNEL:
370 /*snd_emux_channel_init(chset, chan);*/
371 break;
373 case _EMUX_OSS_RELEASE_ALL:
374 fake_event(emu, port, voice, MIDI_CTL_ALL_NOTES_OFF, 0, atomic, hop);
375 break;
376 case _EMUX_OSS_NOTEOFF_ALL:
377 fake_event(emu, port, voice, MIDI_CTL_ALL_SOUNDS_OFF, 0, atomic, hop);
378 break;
380 case _EMUX_OSS_INITIAL_VOLUME:
381 if (p2) {
382 port->volume_atten = (short)p1;
383 snd_emux_update_port(port, SNDRV_EMUX_UPDATE_VOLUME);
385 break;
387 case _EMUX_OSS_CHN_PRESSURE:
388 if (chan) {
389 chan->midi_pressure = p1;
390 snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_FMMOD|SNDRV_EMUX_UPDATE_FM2FRQ2);
392 break;
394 case _EMUX_OSS_CHANNEL_MODE:
395 reset_port_mode(port, p1);
396 snd_emux_reset_port(port);
397 break;
399 case _EMUX_OSS_DRUM_CHANNELS:
400 port->drum_flags = *(unsigned int*)&event[4];
401 for (i = 0; i < port->chset.max_channels; i++) {
402 chan = &port->chset.channels[i];
403 chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
405 break;
407 case _EMUX_OSS_MISC_MODE:
408 if (p1 < EMUX_MD_END)
409 port->ctrls[p1] = p2;
410 break;
411 case _EMUX_OSS_DEBUG_MODE:
412 break;
414 default:
415 if (emu->ops.oss_ioctl)
416 emu->ops.oss_ioctl(emu, cmd, p1, p2);
417 break;
422 * GUS specific h/w controls
425 #include <linux/ultrasound.h>
427 static void
428 gusspec_control(struct snd_emux *emu, struct snd_emux_port *port, int cmd,
429 unsigned char *event, int atomic, int hop)
431 int voice;
432 unsigned short p1;
433 int plong;
434 struct snd_midi_channel *chan;
436 if (port->port_mode != SNDRV_EMUX_PORT_MODE_OSS_SYNTH)
437 return;
438 if (cmd == _GUS_NUMVOICES)
439 return;
440 voice = event[3];
441 if (voice < 0 || voice >= port->chset.max_channels)
442 return;
444 chan = &port->chset.channels[voice];
446 p1 = *(unsigned short *) &event[4];
447 plong = *(int*) &event[4];
449 switch (cmd) {
450 case _GUS_VOICESAMPLE:
451 chan->midi_program = p1;
452 return;
454 case _GUS_VOICEBALA:
455 /* 0 to 15 --> 0 to 127 */
456 chan->control[MIDI_CTL_MSB_PAN] = (int)p1 << 3;
457 snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
458 return;
460 case _GUS_VOICEVOL:
461 case _GUS_VOICEVOL2:
462 /* not supported yet */
463 return;
465 case _GUS_RAMPRANGE:
466 case _GUS_RAMPRATE:
467 case _GUS_RAMPMODE:
468 case _GUS_RAMPON:
469 case _GUS_RAMPOFF:
470 /* volume ramping not supported */
471 return;
473 case _GUS_VOLUME_SCALE:
474 return;
476 case _GUS_VOICE_POS:
477 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
478 snd_emux_send_effect(port, chan, EMUX_FX_SAMPLE_START,
479 (short)(plong & 0x7fff),
480 EMUX_FX_FLAG_SET);
481 snd_emux_send_effect(port, chan, EMUX_FX_COARSE_SAMPLE_START,
482 (plong >> 15) & 0xffff,
483 EMUX_FX_FLAG_SET);
484 #endif
485 return;
491 * send an event to midi emulation
493 static void
494 fake_event(struct snd_emux *emu, struct snd_emux_port *port, int ch, int param, int val, int atomic, int hop)
496 struct snd_seq_event ev;
497 memset(&ev, 0, sizeof(ev));
498 ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
499 ev.data.control.channel = ch;
500 ev.data.control.param = param;
501 ev.data.control.value = val;
502 snd_emux_event_input(&ev, 0, port, atomic, hop);