patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / synth / emux / emux_oss.c
1 /*
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
34 static int snd_emux_open_seq_oss(snd_seq_oss_arg_t *arg, void *closure);
35 static int snd_emux_close_seq_oss(snd_seq_oss_arg_t *arg);
36 static int snd_emux_ioctl_seq_oss(snd_seq_oss_arg_t *arg, unsigned int cmd, unsigned long ioarg);
37 static int snd_emux_load_patch_seq_oss(snd_seq_oss_arg_t *arg, int format, const char __user *buf, int offs, int count);
38 static int snd_emux_reset_seq_oss(snd_seq_oss_arg_t *arg);
39 static int snd_emux_event_oss_input(snd_seq_event_t *ev, int direct, void *private, int atomic, int hop);
40 static void reset_port_mode(snd_emux_port_t *port, int midi_mode);
41 static void emuspec_control(snd_emux_t *emu, snd_emux_port_t *port, int cmd, unsigned char *event, int atomic, int hop);
42 static void gusspec_control(snd_emux_t *emu, snd_emux_port_t *port, int cmd, unsigned char *event, int atomic, int hop);
43 static void fake_event(snd_emux_t *emu, snd_emux_port_t *port, int ch, int param, int val, int atomic, int hop);
44
45 /* operators */
46 static snd_seq_oss_callback_t oss_callback = {
47         .owner = THIS_MODULE,
48         .open = snd_emux_open_seq_oss,
49         .close = snd_emux_close_seq_oss,
50         .ioctl = snd_emux_ioctl_seq_oss,
51         .load_patch = snd_emux_load_patch_seq_oss,
52         .reset = snd_emux_reset_seq_oss,
53 };
54
55
56 /*
57  * register OSS synth
58  */
59
60 void
61 snd_emux_init_seq_oss(snd_emux_t *emu)
62 {
63         snd_seq_oss_reg_t *arg;
64         snd_seq_device_t *dev;
65
66         if (snd_seq_device_new(emu->card, 0, SNDRV_SEQ_DEV_ID_OSS,
67                                sizeof(snd_seq_oss_reg_t), &dev) < 0)
68                 return;
69
70         emu->oss_synth = dev;
71         strcpy(dev->name, emu->name);
72         arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
73         arg->type = SYNTH_TYPE_SAMPLE;
74         arg->subtype = SAMPLE_TYPE_AWE32;
75         arg->nvoices = emu->max_voices;
76         arg->oper = oss_callback;
77         arg->private_data = emu;
78
79         /* register to OSS synth table */
80         snd_device_register(emu->card, dev);
81 }
82
83
84 /*
85  * unregister
86  */
87 void
88 snd_emux_detach_seq_oss(snd_emux_t *emu)
89 {
90         if (emu->oss_synth) {
91                 snd_device_free(emu->card, emu->oss_synth);
92                 emu->oss_synth = NULL;
93         }
94 }
95
96
97 /* use port number as a unique soundfont client number */
98 #define SF_CLIENT_NO(p) ((p) + 0x1000)
99
100 /*
101  * open port for OSS sequencer
102  */
103 static int
104 snd_emux_open_seq_oss(snd_seq_oss_arg_t *arg, void *closure)
105 {
106         snd_emux_t *emu;
107         snd_emux_port_t *p;
108         snd_seq_port_callback_t callback;
109         char tmpname[64];
110
111         emu = snd_magic_cast(snd_emux_t, closure, return -EINVAL);
112         snd_assert(arg != NULL && emu != NULL, return -ENXIO);
113
114         down(&emu->register_mutex);
115
116         if (!snd_emux_inc_count(emu)) {
117                 up(&emu->register_mutex);
118                 return -EFAULT;
119         }
120
121         memset(&callback, 0, sizeof(callback));
122         callback.owner = THIS_MODULE;
123         callback.event_input = snd_emux_event_oss_input;
124
125         sprintf(tmpname, "%s OSS Port", emu->name);
126         p = snd_emux_create_port(emu, tmpname, 32,
127                                  1, &callback);
128         if (p == NULL) {
129                 snd_printk("can't create port\n");
130                 snd_emux_dec_count(emu);
131                 up(&emu->register_mutex);
132                 return -ENOMEM;
133         }
134
135         /* fill the argument data */
136         arg->private_data = p;
137         arg->addr.client = p->chset.client;
138         arg->addr.port = p->chset.port;
139         p->oss_arg = arg;
140
141         reset_port_mode(p, arg->seq_mode);
142
143         snd_emux_reset_port(p);
144
145         up(&emu->register_mutex);
146         return 0;
147 }
148
149
150 #define DEFAULT_DRUM_FLAGS      ((1<<9) | (1<<25))
151
152 /*
153  * reset port mode
154  */
155 static void
156 reset_port_mode(snd_emux_port_t *port, int midi_mode)
157 {
158         if (midi_mode) {
159                 port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_MIDI;
160                 port->drum_flags = DEFAULT_DRUM_FLAGS;
161                 port->volume_atten = 0;
162                 port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_KEYPRESS;
163         } else {
164                 port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_SYNTH;
165                 port->drum_flags = 0;
166                 port->volume_atten = 32;
167                 port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
168         }
169 }
170
171
172 /*
173  * close port
174  */
175 static int
176 snd_emux_close_seq_oss(snd_seq_oss_arg_t *arg)
177 {
178         snd_emux_t *emu;
179         snd_emux_port_t *p;
180
181         snd_assert(arg != NULL, return -ENXIO);
182         p = snd_magic_cast(snd_emux_port_t, arg->private_data, return -EINVAL);
183         snd_assert(p != NULL, return -ENXIO);
184
185         emu = p->emu;
186         snd_assert(emu != NULL, return -ENXIO);
187
188         down(&emu->register_mutex);
189         snd_emux_sounds_off_all(p);
190         snd_soundfont_close_check(emu->sflist, SF_CLIENT_NO(p->chset.port));
191         snd_seq_event_port_detach(p->chset.client, p->chset.port);
192         snd_emux_dec_count(emu);
193
194         up(&emu->register_mutex);
195         return 0;
196 }
197
198
199 /*
200  * load patch
201  */
202 static int
203 snd_emux_load_patch_seq_oss(snd_seq_oss_arg_t *arg, int format,
204                             const char __user *buf, int offs, int count)
205 {
206         snd_emux_t *emu;
207         snd_emux_port_t *p;
208         int rc;
209
210         snd_assert(arg != NULL, return -ENXIO);
211         p = snd_magic_cast(snd_emux_port_t, arg->private_data, return -EINVAL);
212         snd_assert(p != NULL, return -ENXIO);
213
214         emu = p->emu;
215         snd_assert(emu != NULL, return -ENXIO);
216
217         if (format == GUS_PATCH)
218                 rc = snd_soundfont_load_guspatch(emu->sflist, buf, count,
219                                                  SF_CLIENT_NO(p->chset.port));
220         else if (format == SNDRV_OSS_SOUNDFONT_PATCH) {
221                 soundfont_patch_info_t patch;
222                 if (count < (int)sizeof(patch))
223                         rc = -EINVAL;
224                 if (copy_from_user(&patch, buf, sizeof(patch)))
225                         rc = -EFAULT;
226                 if (patch.type >= SNDRV_SFNT_LOAD_INFO &&
227                     patch.type <= SNDRV_SFNT_PROBE_DATA)
228                         rc = snd_soundfont_load(emu->sflist, buf, count, SF_CLIENT_NO(p->chset.port));
229                 else {
230                         if (emu->ops.load_fx)
231                                 rc = emu->ops.load_fx(emu, patch.type, patch.optarg, buf, count);
232                         else
233                                 rc = -EINVAL;
234                 }
235         } else
236                 rc = 0;
237         return rc;
238 }
239
240
241 /*
242  * ioctl
243  */
244 static int
245 snd_emux_ioctl_seq_oss(snd_seq_oss_arg_t *arg, unsigned int cmd, unsigned long ioarg)
246 {
247         snd_emux_port_t *p;
248         snd_emux_t *emu;
249
250         snd_assert(arg != NULL, return -ENXIO);
251         p = snd_magic_cast(snd_emux_port_t, arg->private_data, return -EINVAL);
252         snd_assert(p != NULL, return -ENXIO);
253
254         emu = p->emu;
255         snd_assert(emu != NULL, return -ENXIO);
256
257         switch (cmd) {
258         case SNDCTL_SEQ_RESETSAMPLES:
259                 snd_soundfont_remove_samples(emu->sflist);
260                 return 0;
261                         
262         case SNDCTL_SYNTH_MEMAVL:
263                 if (emu->memhdr)
264                         return snd_util_mem_avail(emu->memhdr);
265                 return 0;
266         }
267
268         return 0;
269 }
270
271
272 /*
273  * reset device
274  */
275 static int
276 snd_emux_reset_seq_oss(snd_seq_oss_arg_t *arg)
277 {
278         snd_emux_port_t *p;
279
280         snd_assert(arg != NULL, return -ENXIO);
281         p = snd_magic_cast(snd_emux_port_t, arg->private_data, return -EINVAL);
282         snd_assert(p != NULL, return -ENXIO);
283         snd_emux_reset_port(p);
284         return 0;
285 }
286
287
288 /*
289  * receive raw events: only SEQ_PRIVATE is accepted.
290  */
291 static int
292 snd_emux_event_oss_input(snd_seq_event_t *ev, int direct, void *private_data,
293                          int atomic, int hop)
294 {
295         snd_emux_t *emu;
296         snd_emux_port_t *p;
297         unsigned char cmd, *data;
298
299         p = snd_magic_cast(snd_emux_port_t, private_data, return -EINVAL);
300         snd_assert(p != NULL, return -EINVAL);
301         emu = p->emu;
302         snd_assert(emu != NULL, return -EINVAL);
303         if (ev->type != SNDRV_SEQ_EVENT_OSS)
304                 return snd_emux_event_input(ev, direct, private_data, atomic, hop);
305
306         data = ev->data.raw8.d;
307         /* only SEQ_PRIVATE is accepted */
308         if (data[0] != 0xfe)
309                 return 0;
310         cmd = data[2] & _EMUX_OSS_MODE_VALUE_MASK;
311         if (data[2] & _EMUX_OSS_MODE_FLAG)
312                 emuspec_control(emu, p, cmd, data, atomic, hop);
313         else
314                 gusspec_control(emu, p, cmd, data, atomic, hop);
315         return 0;
316 }
317
318
319 /*
320  * OSS/AWE driver specific h/w controls
321  */
322 static void
323 emuspec_control(snd_emux_t *emu, snd_emux_port_t *port, int cmd,
324                 unsigned char *event, int atomic, int hop)
325 {
326         int voice;
327         unsigned short p1;
328         short p2;
329         int i;
330         snd_midi_channel_t *chan;
331
332         voice = event[3];
333         if (voice < 0 || voice >= port->chset.max_channels)
334                 chan = NULL;
335         else
336                 chan = &port->chset.channels[voice];
337
338         p1 = *(unsigned short *) &event[4];
339         p2 = *(short *) &event[6];
340
341         switch (cmd) {
342         case _EMUX_OSS_REMOVE_LAST_SAMPLES:
343                 snd_soundfont_remove_unlocked(emu->sflist);
344                 break;
345         case _EMUX_OSS_SEND_EFFECT:
346                 if (chan)
347                         snd_emux_send_effect_oss(port, chan, p1, p2);
348                 break;
349                 
350         case _EMUX_OSS_TERMINATE_ALL:
351                 snd_emux_terminate_all(emu);
352                 break;
353
354         case _EMUX_OSS_TERMINATE_CHANNEL:
355                 /*snd_emux_mute_channel(emu, chan);*/
356                 break;
357         case _EMUX_OSS_RESET_CHANNEL:
358                 /*snd_emux_channel_init(chset, chan);*/
359                 break;
360
361         case _EMUX_OSS_RELEASE_ALL:
362                 fake_event(emu, port, voice, MIDI_CTL_ALL_NOTES_OFF, 0, atomic, hop);
363                 break;
364         case _EMUX_OSS_NOTEOFF_ALL:
365                 fake_event(emu, port, voice, MIDI_CTL_ALL_SOUNDS_OFF, 0, atomic, hop);
366                 break;
367
368         case _EMUX_OSS_INITIAL_VOLUME:
369                 if (p2) {
370                         port->volume_atten = (short)p1;
371                         snd_emux_update_port(port, SNDRV_EMUX_UPDATE_VOLUME);
372                 }
373                 break;
374
375         case _EMUX_OSS_CHN_PRESSURE:
376                 if (chan) {
377                         chan->midi_pressure = p1;
378                         snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_FMMOD|SNDRV_EMUX_UPDATE_FM2FRQ2);
379                 }
380                 break;
381
382         case _EMUX_OSS_CHANNEL_MODE:
383                 reset_port_mode(port, p1);
384                 snd_emux_reset_port(port);
385                 break;
386
387         case _EMUX_OSS_DRUM_CHANNELS:
388                 port->drum_flags = *(unsigned int*)&event[4];
389                 for (i = 0; i < port->chset.max_channels; i++) {
390                         chan = &port->chset.channels[i];
391                         chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
392                 }
393                 break;
394
395         case _EMUX_OSS_MISC_MODE:
396                 if (p1 < EMUX_MD_END)
397                         port->ctrls[p1] = p2;
398                 break;
399         case _EMUX_OSS_DEBUG_MODE:
400                 break;
401
402         default:
403                 if (emu->ops.oss_ioctl)
404                         emu->ops.oss_ioctl(emu, cmd, p1, p2);
405                 break;
406         }
407 }
408
409 /*
410  * GUS specific h/w controls
411  */
412
413 #include <linux/ultrasound.h>
414
415 static void
416 gusspec_control(snd_emux_t *emu, snd_emux_port_t *port, int cmd,
417                 unsigned char *event, int atomic, int hop)
418 {
419         int voice;
420         unsigned short p1;
421         short p2;
422         int plong;
423         snd_midi_channel_t *chan;
424
425         if (port->port_mode != SNDRV_EMUX_PORT_MODE_OSS_SYNTH)
426                 return;
427         if (cmd == _GUS_NUMVOICES)
428                 return;
429         voice = event[3];
430         if (voice < 0 || voice >= port->chset.max_channels)
431                 return;
432
433         chan = &port->chset.channels[voice];
434
435         p1 = *(unsigned short *) &event[4];
436         p2 = *(short *) &event[6];
437         plong = *(int*) &event[4];
438
439         switch (cmd) {
440         case _GUS_VOICESAMPLE:
441                 chan->midi_program = p1;
442                 return;
443
444         case _GUS_VOICEBALA:
445                 /* 0 to 15 --> 0 to 127 */
446                 chan->control[MIDI_CTL_MSB_PAN] = (int)p1 << 3;
447                 snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
448                 return;
449
450         case _GUS_VOICEVOL:
451         case _GUS_VOICEVOL2:
452                 /* not supported yet */
453                 return;
454
455         case _GUS_RAMPRANGE:
456         case _GUS_RAMPRATE:
457         case _GUS_RAMPMODE:
458         case _GUS_RAMPON:
459         case _GUS_RAMPOFF:
460                 /* volume ramping not supported */
461                 return;
462
463         case _GUS_VOLUME_SCALE:
464                 return;
465
466         case _GUS_VOICE_POS:
467 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
468                 snd_emux_send_effect(port, chan, EMUX_FX_SAMPLE_START,
469                                      (short)(plong & 0x7fff),
470                                      EMUX_FX_FLAG_SET);
471                 snd_emux_send_effect(port, chan, EMUX_FX_COARSE_SAMPLE_START,
472                                      (plong >> 15) & 0xffff,
473                                      EMUX_FX_FLAG_SET);
474 #endif
475                 return;
476         }
477 }
478
479
480 /*
481  * send an event to midi emulation
482  */
483 static void
484 fake_event(snd_emux_t *emu, snd_emux_port_t *port, int ch, int param, int val, int atomic, int hop)
485 {
486         snd_seq_event_t ev;
487         memset(&ev, 0, sizeof(ev));
488         ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
489         ev.data.control.channel = ch;
490         ev.data.control.param = param;
491         ev.data.control.value = val;
492         snd_emux_event_input(&ev, 0, port, atomic, hop);
493 }
494
495 #endif /* CONFIG_SND_SEQUENCER_OSS */