2 * Interface for OSS sequencer emulation
4 * Copyright (C) 2000 Uros Bizjak <uros@kss-loka.si>
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
21 #include "opl3_voice.h"
22 #include <linux/slab.h>
24 static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
25 static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg);
26 static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg);
27 static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, const char __user *buf, int offs, int count);
28 static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg);
32 static inline mm_segment_t snd_enter_user(void)
34 mm_segment_t fs = get_fs();
39 static inline void snd_leave_user(mm_segment_t fs)
46 extern struct snd_midi_op opl3_ops;
48 static struct snd_seq_oss_callback oss_callback = {
50 .open = snd_opl3_open_seq_oss,
51 .close = snd_opl3_close_seq_oss,
52 .ioctl = snd_opl3_ioctl_seq_oss,
53 .load_patch = snd_opl3_load_patch_seq_oss,
54 .reset = snd_opl3_reset_seq_oss,
57 static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
58 void *private_data, int atomic, int hop)
60 struct snd_opl3 *opl3 = private_data;
62 if (ev->type != SNDRV_SEQ_EVENT_OSS)
63 snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
67 /* ------------------------------ */
69 static void snd_opl3_oss_free_port(void *private_data)
71 struct snd_opl3 *opl3 = private_data;
73 snd_midi_channel_free_set(opl3->oss_chset);
76 static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
78 struct snd_seq_port_callback callbacks;
82 voices = (opl3->hardware < OPL3_HW_OPL3) ?
83 MAX_OPL2_VOICES : MAX_OPL3_VOICES;
84 opl3->oss_chset = snd_midi_channel_alloc_set(voices);
85 if (opl3->oss_chset == NULL)
87 opl3->oss_chset->private_data = opl3;
89 memset(&callbacks, 0, sizeof(callbacks));
90 callbacks.owner = THIS_MODULE;
91 callbacks.event_input = snd_opl3_oss_event_input;
92 callbacks.private_free = snd_opl3_oss_free_port;
93 callbacks.private_data = opl3;
95 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
96 sprintf(name, "OPL%i OSS Port", opl_ver);
98 opl3->oss_chset->client = opl3->seq_client;
99 opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
100 SNDRV_SEQ_PORT_CAP_WRITE,
101 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
102 SNDRV_SEQ_PORT_TYPE_MIDI_GM |
103 SNDRV_SEQ_PORT_TYPE_HARDWARE |
104 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
107 if (opl3->oss_chset->port < 0) {
109 port = opl3->oss_chset->port;
110 snd_midi_channel_free_set(opl3->oss_chset);
116 /* ------------------------------ */
118 /* register OSS synth */
119 void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
121 struct snd_seq_oss_reg *arg;
122 struct snd_seq_device *dev;
124 if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
125 sizeof(struct snd_seq_oss_reg), &dev) < 0)
128 opl3->oss_seq_dev = dev;
129 strlcpy(dev->name, name, sizeof(dev->name));
130 arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
131 arg->type = SYNTH_TYPE_FM;
132 if (opl3->hardware < OPL3_HW_OPL3) {
133 arg->subtype = FM_TYPE_ADLIB;
134 arg->nvoices = MAX_OPL2_VOICES;
136 arg->subtype = FM_TYPE_OPL3;
137 arg->nvoices = MAX_OPL3_VOICES;
139 arg->oper = oss_callback;
140 arg->private_data = opl3;
142 if (snd_opl3_oss_create_port(opl3)) {
143 /* register to OSS synth table */
144 snd_device_register(opl3->card, dev);
149 void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
151 if (opl3->oss_seq_dev) {
152 /* The instance should have been released in prior */
153 opl3->oss_seq_dev = NULL;
157 /* ------------------------------ */
159 /* open OSS sequencer */
160 static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
162 struct snd_opl3 *opl3 = closure;
165 snd_assert(arg != NULL, return -ENXIO);
167 if ((err = snd_opl3_synth_setup(opl3)) < 0)
170 /* fill the argument data */
171 arg->private_data = opl3;
172 arg->addr.client = opl3->oss_chset->client;
173 arg->addr.port = opl3->oss_chset->port;
175 if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
178 opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
182 /* close OSS sequencer */
183 static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
185 struct snd_opl3 *opl3;
187 snd_assert(arg != NULL, return -ENXIO);
188 opl3 = arg->private_data;
190 snd_opl3_synth_cleanup(opl3);
192 snd_opl3_synth_use_dec(opl3);
198 /* from sound_config.h */
199 #define SBFM_MAXINSTR 256
201 static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
202 const char __user *buf, int offs, int count)
204 struct snd_opl3 *opl3;
205 struct sbi_instrument sbi;
209 snd_assert(arg != NULL, return -ENXIO);
210 opl3 = arg->private_data;
212 if (format == FM_PATCH)
213 type = FM_PATCH_OPL2;
214 else if (format == OPL3_PATCH)
215 type = FM_PATCH_OPL3;
219 if (count < (int)sizeof(sbi)) {
220 snd_printk("FM Error: Patch record too short\n");
223 if (copy_from_user(&sbi, buf, sizeof(sbi)))
226 if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
227 snd_printk("FM Error: Invalid instrument number %d\n",
232 memset(name, 0, sizeof(name));
233 sprintf(name, "Chan%d", sbi.channel);
235 err = snd_opl3_load_patch(opl3, sbi.channel, 127, type, name, NULL,
244 static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
247 struct snd_opl3 *opl3;
249 snd_assert(arg != NULL, return -ENXIO);
250 opl3 = arg->private_data;
252 case SNDCTL_FM_LOAD_INSTR:
253 snd_printk("OPL3: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
256 case SNDCTL_SYNTH_MEMAVL:
259 case SNDCTL_FM_4OP_ENABLE:
260 // handled automatically by OPL instrument type
270 static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
272 struct snd_opl3 *opl3;
274 snd_assert(arg != NULL, return -ENXIO);
275 opl3 = arg->private_data;