blob: 3436816727c8190ff0f5e5960c38f62c7c01e886 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Interface for OSS sequencer emulation
3 *
4 * Copyright (C) 1999 Takashi Iwai <tiwai@suse.de>
5 *
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.
10 *
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.
15 *
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
19 *
20 * Changes
21 * 19990227 Steve Ratcliffe Made separate file and merged in latest
22 * midi emulation.
23 */
24
25#include <sound/driver.h>
26
27#ifdef CONFIG_SND_SEQUENCER_OSS
28
29#include <asm/uaccess.h>
30#include <sound/core.h>
31#include "emux_voice.h"
32#include <sound/asoundef.h>
33
Takashi Iwai03da3122005-11-17 14:24:47 +010034static int snd_emux_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
35static int snd_emux_close_seq_oss(struct snd_seq_oss_arg *arg);
36static int snd_emux_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
37 unsigned long ioarg);
38static int snd_emux_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
39 const char __user *buf, int offs, int count);
40static int snd_emux_reset_seq_oss(struct snd_seq_oss_arg *arg);
41static int snd_emux_event_oss_input(struct snd_seq_event *ev, int direct,
42 void *private, int atomic, int hop);
43static void reset_port_mode(struct snd_emux_port *port, int midi_mode);
44static void emuspec_control(struct snd_emux *emu, struct snd_emux_port *port,
45 int cmd, unsigned char *event, int atomic, int hop);
46static void gusspec_control(struct snd_emux *emu, struct snd_emux_port *port,
47 int cmd, unsigned char *event, int atomic, int hop);
48static void fake_event(struct snd_emux *emu, struct snd_emux_port *port,
49 int ch, int param, int val, int atomic, int hop);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* operators */
Takashi Iwai03da3122005-11-17 14:24:47 +010052static struct snd_seq_oss_callback oss_callback = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .owner = THIS_MODULE,
54 .open = snd_emux_open_seq_oss,
55 .close = snd_emux_close_seq_oss,
56 .ioctl = snd_emux_ioctl_seq_oss,
57 .load_patch = snd_emux_load_patch_seq_oss,
58 .reset = snd_emux_reset_seq_oss,
59};
60
61
62/*
63 * register OSS synth
64 */
65
66void
Takashi Iwai03da3122005-11-17 14:24:47 +010067snd_emux_init_seq_oss(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Takashi Iwai03da3122005-11-17 14:24:47 +010069 struct snd_seq_oss_reg *arg;
70 struct snd_seq_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 if (snd_seq_device_new(emu->card, 0, SNDRV_SEQ_DEV_ID_OSS,
Takashi Iwai03da3122005-11-17 14:24:47 +010073 sizeof(struct snd_seq_oss_reg), &dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return;
75
76 emu->oss_synth = dev;
77 strcpy(dev->name, emu->name);
78 arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
79 arg->type = SYNTH_TYPE_SAMPLE;
80 arg->subtype = SAMPLE_TYPE_AWE32;
81 arg->nvoices = emu->max_voices;
82 arg->oper = oss_callback;
83 arg->private_data = emu;
84
85 /* register to OSS synth table */
86 snd_device_register(emu->card, dev);
87}
88
89
90/*
91 * unregister
92 */
93void
Takashi Iwai03da3122005-11-17 14:24:47 +010094snd_emux_detach_seq_oss(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 if (emu->oss_synth) {
97 snd_device_free(emu->card, emu->oss_synth);
98 emu->oss_synth = NULL;
99 }
100}
101
102
103/* use port number as a unique soundfont client number */
104#define SF_CLIENT_NO(p) ((p) + 0x1000)
105
106/*
107 * open port for OSS sequencer
108 */
109static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100110snd_emux_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Takashi Iwai03da3122005-11-17 14:24:47 +0100112 struct snd_emux *emu;
113 struct snd_emux_port *p;
114 struct snd_seq_port_callback callback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 char tmpname[64];
116
117 emu = closure;
118 snd_assert(arg != NULL && emu != NULL, return -ENXIO);
119
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100120 mutex_lock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (!snd_emux_inc_count(emu)) {
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100123 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -EFAULT;
125 }
126
127 memset(&callback, 0, sizeof(callback));
128 callback.owner = THIS_MODULE;
129 callback.event_input = snd_emux_event_oss_input;
130
131 sprintf(tmpname, "%s OSS Port", emu->name);
132 p = snd_emux_create_port(emu, tmpname, 32,
133 1, &callback);
134 if (p == NULL) {
135 snd_printk("can't create port\n");
136 snd_emux_dec_count(emu);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100137 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -ENOMEM;
139 }
140
141 /* fill the argument data */
142 arg->private_data = p;
143 arg->addr.client = p->chset.client;
144 arg->addr.port = p->chset.port;
145 p->oss_arg = arg;
146
147 reset_port_mode(p, arg->seq_mode);
148
149 snd_emux_reset_port(p);
150
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100151 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return 0;
153}
154
155
156#define DEFAULT_DRUM_FLAGS ((1<<9) | (1<<25))
157
158/*
159 * reset port mode
160 */
161static void
Takashi Iwai03da3122005-11-17 14:24:47 +0100162reset_port_mode(struct snd_emux_port *port, int midi_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 if (midi_mode) {
165 port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_MIDI;
166 port->drum_flags = DEFAULT_DRUM_FLAGS;
167 port->volume_atten = 0;
168 port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_KEYPRESS;
169 } else {
170 port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_SYNTH;
171 port->drum_flags = 0;
172 port->volume_atten = 32;
173 port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
174 }
175}
176
177
178/*
179 * close port
180 */
181static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100182snd_emux_close_seq_oss(struct snd_seq_oss_arg *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Takashi Iwai03da3122005-11-17 14:24:47 +0100184 struct snd_emux *emu;
185 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 snd_assert(arg != NULL, return -ENXIO);
188 p = arg->private_data;
189 snd_assert(p != NULL, return -ENXIO);
190
191 emu = p->emu;
192 snd_assert(emu != NULL, return -ENXIO);
193
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100194 mutex_lock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 snd_emux_sounds_off_all(p);
196 snd_soundfont_close_check(emu->sflist, SF_CLIENT_NO(p->chset.port));
197 snd_seq_event_port_detach(p->chset.client, p->chset.port);
198 snd_emux_dec_count(emu);
199
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100200 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202}
203
204
205/*
206 * load patch
207 */
208static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100209snd_emux_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 const char __user *buf, int offs, int count)
211{
Takashi Iwai03da3122005-11-17 14:24:47 +0100212 struct snd_emux *emu;
213 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int rc;
215
216 snd_assert(arg != NULL, return -ENXIO);
217 p = arg->private_data;
218 snd_assert(p != NULL, return -ENXIO);
219
220 emu = p->emu;
221 snd_assert(emu != NULL, return -ENXIO);
222
223 if (format == GUS_PATCH)
224 rc = snd_soundfont_load_guspatch(emu->sflist, buf, count,
225 SF_CLIENT_NO(p->chset.port));
226 else if (format == SNDRV_OSS_SOUNDFONT_PATCH) {
Takashi Iwai03da3122005-11-17 14:24:47 +0100227 struct soundfont_patch_info patch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (count < (int)sizeof(patch))
229 rc = -EINVAL;
230 if (copy_from_user(&patch, buf, sizeof(patch)))
231 rc = -EFAULT;
232 if (patch.type >= SNDRV_SFNT_LOAD_INFO &&
233 patch.type <= SNDRV_SFNT_PROBE_DATA)
234 rc = snd_soundfont_load(emu->sflist, buf, count, SF_CLIENT_NO(p->chset.port));
235 else {
236 if (emu->ops.load_fx)
237 rc = emu->ops.load_fx(emu, patch.type, patch.optarg, buf, count);
238 else
239 rc = -EINVAL;
240 }
241 } else
242 rc = 0;
243 return rc;
244}
245
246
247/*
248 * ioctl
249 */
250static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100251snd_emux_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Takashi Iwai03da3122005-11-17 14:24:47 +0100253 struct snd_emux_port *p;
254 struct snd_emux *emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 snd_assert(arg != NULL, return -ENXIO);
257 p = arg->private_data;
258 snd_assert(p != NULL, return -ENXIO);
259
260 emu = p->emu;
261 snd_assert(emu != NULL, return -ENXIO);
262
263 switch (cmd) {
264 case SNDCTL_SEQ_RESETSAMPLES:
265 snd_soundfont_remove_samples(emu->sflist);
266 return 0;
267
268 case SNDCTL_SYNTH_MEMAVL:
269 if (emu->memhdr)
270 return snd_util_mem_avail(emu->memhdr);
271 return 0;
272 }
273
274 return 0;
275}
276
277
278/*
279 * reset device
280 */
281static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100282snd_emux_reset_seq_oss(struct snd_seq_oss_arg *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Takashi Iwai03da3122005-11-17 14:24:47 +0100284 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 snd_assert(arg != NULL, return -ENXIO);
287 p = arg->private_data;
288 snd_assert(p != NULL, return -ENXIO);
289 snd_emux_reset_port(p);
290 return 0;
291}
292
293
294/*
295 * receive raw events: only SEQ_PRIVATE is accepted.
296 */
297static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100298snd_emux_event_oss_input(struct snd_seq_event *ev, int direct, void *private_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int atomic, int hop)
300{
Takashi Iwai03da3122005-11-17 14:24:47 +0100301 struct snd_emux *emu;
302 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 unsigned char cmd, *data;
304
305 p = private_data;
306 snd_assert(p != NULL, return -EINVAL);
307 emu = p->emu;
308 snd_assert(emu != NULL, return -EINVAL);
309 if (ev->type != SNDRV_SEQ_EVENT_OSS)
310 return snd_emux_event_input(ev, direct, private_data, atomic, hop);
311
312 data = ev->data.raw8.d;
313 /* only SEQ_PRIVATE is accepted */
314 if (data[0] != 0xfe)
315 return 0;
316 cmd = data[2] & _EMUX_OSS_MODE_VALUE_MASK;
317 if (data[2] & _EMUX_OSS_MODE_FLAG)
318 emuspec_control(emu, p, cmd, data, atomic, hop);
319 else
320 gusspec_control(emu, p, cmd, data, atomic, hop);
321 return 0;
322}
323
324
325/*
326 * OSS/AWE driver specific h/w controls
327 */
328static void
Takashi Iwai03da3122005-11-17 14:24:47 +0100329emuspec_control(struct snd_emux *emu, struct snd_emux_port *port, int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned char *event, int atomic, int hop)
331{
332 int voice;
333 unsigned short p1;
334 short p2;
335 int i;
Takashi Iwai03da3122005-11-17 14:24:47 +0100336 struct snd_midi_channel *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 voice = event[3];
339 if (voice < 0 || voice >= port->chset.max_channels)
340 chan = NULL;
341 else
342 chan = &port->chset.channels[voice];
343
344 p1 = *(unsigned short *) &event[4];
345 p2 = *(short *) &event[6];
346
347 switch (cmd) {
348#if 0 /* don't do this atomically */
349 case _EMUX_OSS_REMOVE_LAST_SAMPLES:
350 snd_soundfont_remove_unlocked(emu->sflist);
351 break;
352#endif
353 case _EMUX_OSS_SEND_EFFECT:
354 if (chan)
355 snd_emux_send_effect_oss(port, chan, p1, p2);
356 break;
357
358 case _EMUX_OSS_TERMINATE_ALL:
359 snd_emux_terminate_all(emu);
360 break;
361
362 case _EMUX_OSS_TERMINATE_CHANNEL:
363 /*snd_emux_mute_channel(emu, chan);*/
364 break;
365 case _EMUX_OSS_RESET_CHANNEL:
366 /*snd_emux_channel_init(chset, chan);*/
367 break;
368
369 case _EMUX_OSS_RELEASE_ALL:
370 fake_event(emu, port, voice, MIDI_CTL_ALL_NOTES_OFF, 0, atomic, hop);
371 break;
372 case _EMUX_OSS_NOTEOFF_ALL:
373 fake_event(emu, port, voice, MIDI_CTL_ALL_SOUNDS_OFF, 0, atomic, hop);
374 break;
375
376 case _EMUX_OSS_INITIAL_VOLUME:
377 if (p2) {
378 port->volume_atten = (short)p1;
379 snd_emux_update_port(port, SNDRV_EMUX_UPDATE_VOLUME);
380 }
381 break;
382
383 case _EMUX_OSS_CHN_PRESSURE:
384 if (chan) {
385 chan->midi_pressure = p1;
386 snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_FMMOD|SNDRV_EMUX_UPDATE_FM2FRQ2);
387 }
388 break;
389
390 case _EMUX_OSS_CHANNEL_MODE:
391 reset_port_mode(port, p1);
392 snd_emux_reset_port(port);
393 break;
394
395 case _EMUX_OSS_DRUM_CHANNELS:
396 port->drum_flags = *(unsigned int*)&event[4];
397 for (i = 0; i < port->chset.max_channels; i++) {
398 chan = &port->chset.channels[i];
399 chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
400 }
401 break;
402
403 case _EMUX_OSS_MISC_MODE:
404 if (p1 < EMUX_MD_END)
405 port->ctrls[p1] = p2;
406 break;
407 case _EMUX_OSS_DEBUG_MODE:
408 break;
409
410 default:
411 if (emu->ops.oss_ioctl)
412 emu->ops.oss_ioctl(emu, cmd, p1, p2);
413 break;
414 }
415}
416
417/*
418 * GUS specific h/w controls
419 */
420
421#include <linux/ultrasound.h>
422
423static void
Takashi Iwai03da3122005-11-17 14:24:47 +0100424gusspec_control(struct snd_emux *emu, struct snd_emux_port *port, int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 unsigned char *event, int atomic, int hop)
426{
427 int voice;
428 unsigned short p1;
429 short p2;
430 int plong;
Takashi Iwai03da3122005-11-17 14:24:47 +0100431 struct snd_midi_channel *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (port->port_mode != SNDRV_EMUX_PORT_MODE_OSS_SYNTH)
434 return;
435 if (cmd == _GUS_NUMVOICES)
436 return;
437 voice = event[3];
438 if (voice < 0 || voice >= port->chset.max_channels)
439 return;
440
441 chan = &port->chset.channels[voice];
442
443 p1 = *(unsigned short *) &event[4];
444 p2 = *(short *) &event[6];
445 plong = *(int*) &event[4];
446
447 switch (cmd) {
448 case _GUS_VOICESAMPLE:
449 chan->midi_program = p1;
450 return;
451
452 case _GUS_VOICEBALA:
453 /* 0 to 15 --> 0 to 127 */
454 chan->control[MIDI_CTL_MSB_PAN] = (int)p1 << 3;
455 snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
456 return;
457
458 case _GUS_VOICEVOL:
459 case _GUS_VOICEVOL2:
460 /* not supported yet */
461 return;
462
463 case _GUS_RAMPRANGE:
464 case _GUS_RAMPRATE:
465 case _GUS_RAMPMODE:
466 case _GUS_RAMPON:
467 case _GUS_RAMPOFF:
468 /* volume ramping not supported */
469 return;
470
471 case _GUS_VOLUME_SCALE:
472 return;
473
474 case _GUS_VOICE_POS:
475#ifdef SNDRV_EMUX_USE_RAW_EFFECT
476 snd_emux_send_effect(port, chan, EMUX_FX_SAMPLE_START,
477 (short)(plong & 0x7fff),
478 EMUX_FX_FLAG_SET);
479 snd_emux_send_effect(port, chan, EMUX_FX_COARSE_SAMPLE_START,
480 (plong >> 15) & 0xffff,
481 EMUX_FX_FLAG_SET);
482#endif
483 return;
484 }
485}
486
487
488/*
489 * send an event to midi emulation
490 */
491static void
Takashi Iwai03da3122005-11-17 14:24:47 +0100492fake_event(struct snd_emux *emu, struct snd_emux_port *port, int ch, int param, int val, int atomic, int hop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Takashi Iwai03da3122005-11-17 14:24:47 +0100494 struct snd_seq_event ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 memset(&ev, 0, sizeof(ev));
496 ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
497 ev.data.control.channel = ch;
498 ev.data.control.param = param;
499 ev.data.control.value = val;
500 snd_emux_event_input(&ev, 0, port, atomic, hop);
501}
502
503#endif /* CONFIG_SND_SEQUENCER_OSS */