blob: d48f8dee2d98d9daac501d0c215c06ef048e55bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Interface for OSS sequencer emulation
3 *
4 * Copyright (C) 2000 Uros Bizjak <uros@kss-loka.si>
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
21#include "opl3_voice.h"
22#include <linux/slab.h>
23
Takashi Iwai5b1646a2005-11-17 14:13:14 +010024static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
25static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg);
26static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg);
27static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, const char __user *buf, int offs, int count);
28static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/* */
31
32static inline mm_segment_t snd_enter_user(void)
33{
34 mm_segment_t fs = get_fs();
35 set_fs(get_ds());
36 return fs;
37}
38
39static inline void snd_leave_user(mm_segment_t fs)
40{
41 set_fs(fs);
42}
43
44/* operators */
45
Takashi Iwai5b1646a2005-11-17 14:13:14 +010046extern struct snd_midi_op opl3_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai5b1646a2005-11-17 14:13:14 +010048static struct snd_seq_oss_callback oss_callback = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 .owner = THIS_MODULE,
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,
55};
56
Takashi Iwai5b1646a2005-11-17 14:13:14 +010057static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 void *private_data, int atomic, int hop)
59{
Takashi Iwai5b1646a2005-11-17 14:13:14 +010060 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (ev->type != SNDRV_SEQ_EVENT_OSS)
63 snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
64 return 0;
65}
66
67/* ------------------------------ */
68
69static void snd_opl3_oss_free_port(void *private_data)
70{
Takashi Iwai5b1646a2005-11-17 14:13:14 +010071 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 snd_midi_channel_free_set(opl3->oss_chset);
74}
75
Takashi Iwai5b1646a2005-11-17 14:13:14 +010076static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Takashi Iwai5b1646a2005-11-17 14:13:14 +010078 struct snd_seq_port_callback callbacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 char name[32];
80 int voices, opl_ver;
81
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)
86 return -ENOMEM;
87 opl3->oss_chset->private_data = opl3;
88
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;
94
95 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
96 sprintf(name, "OPL%i OSS Port", opl_ver);
97
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 |
Clemens Ladischc97f3dd2006-05-02 15:50:05 +0200102 SNDRV_SEQ_PORT_TYPE_MIDI_GM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 voices, voices,
104 name);
105 if (opl3->oss_chset->port < 0) {
Dave Jones5e315e92006-03-06 14:03:37 +0100106 int port;
107 port = opl3->oss_chset->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 snd_midi_channel_free_set(opl3->oss_chset);
Dave Jones5e315e92006-03-06 14:03:37 +0100109 return port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 return 0;
112}
113
114/* ------------------------------ */
115
116/* register OSS synth */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100117void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100119 struct snd_seq_oss_reg *arg;
120 struct snd_seq_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100123 sizeof(struct snd_seq_oss_reg), &dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return;
125
126 opl3->oss_seq_dev = dev;
127 strlcpy(dev->name, name, sizeof(dev->name));
128 arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
129 arg->type = SYNTH_TYPE_FM;
130 if (opl3->hardware < OPL3_HW_OPL3) {
131 arg->subtype = FM_TYPE_ADLIB;
132 arg->nvoices = MAX_OPL2_VOICES;
133 } else {
134 arg->subtype = FM_TYPE_OPL3;
135 arg->nvoices = MAX_OPL3_VOICES;
136 }
137 arg->oper = oss_callback;
138 arg->private_data = opl3;
139
Dave Jones5e315e92006-03-06 14:03:37 +0100140 if (snd_opl3_oss_create_port(opl3)) {
141 /* register to OSS synth table */
142 snd_device_register(opl3->card, dev);
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146/* unregister */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100147void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 if (opl3->oss_seq_dev) {
Takashi Iwaib1a3aa22006-02-24 13:03:52 -0800150 /* The instance should have been released in prior */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 opl3->oss_seq_dev = NULL;
152 }
153}
154
155/* ------------------------------ */
156
157/* open OSS sequencer */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100158static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100160 struct snd_opl3 *opl3 = closure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int err;
162
163 snd_assert(arg != NULL, return -ENXIO);
164
165 if ((err = snd_opl3_synth_setup(opl3)) < 0)
166 return err;
167
168 /* fill the argument data */
169 arg->private_data = opl3;
170 arg->addr.client = opl3->oss_chset->client;
171 arg->addr.port = opl3->oss_chset->port;
172
173 if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
174 return err;
175
176 opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
177 return 0;
178}
179
180/* close OSS sequencer */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100181static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100183 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 snd_assert(arg != NULL, return -ENXIO);
186 opl3 = arg->private_data;
187
188 snd_opl3_synth_cleanup(opl3);
189
190 snd_opl3_synth_use_dec(opl3);
191 return 0;
192}
193
194/* load patch */
195
196/* offsets for SBI params */
197#define AM_VIB 0
198#define KSL_LEVEL 2
199#define ATTACK_DECAY 4
200#define SUSTAIN_RELEASE 6
201#define WAVE_SELECT 8
202
203/* offset for SBI instrument */
204#define CONNECTION 10
205#define OFFSET_4OP 11
206
207/* from sound_config.h */
208#define SBFM_MAXINSTR 256
209
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100210static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 const char __user *buf, int offs, int count)
212{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100213 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int err = -EINVAL;
215
216 snd_assert(arg != NULL, return -ENXIO);
217 opl3 = arg->private_data;
218
219 if ((format == FM_PATCH) || (format == OPL3_PATCH)) {
220 struct sbi_instrument sbi;
221
222 size_t size;
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100223 struct snd_seq_instr_header *put;
224 struct snd_seq_instr_data *data;
225 struct fm_xinstrument *xinstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100227 struct snd_seq_event ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int i;
229
230 mm_segment_t fs;
231
232 if (count < (int)sizeof(sbi)) {
233 snd_printk("FM Error: Patch record too short\n");
234 return -EINVAL;
235 }
236 if (copy_from_user(&sbi, buf, sizeof(sbi)))
237 return -EFAULT;
238
239 if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
240 snd_printk("FM Error: Invalid instrument number %d\n", sbi.channel);
241 return -EINVAL;
242 }
243
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100244 size = sizeof(*put) + sizeof(struct fm_xinstrument);
Takashi Iwai561b2202005-09-09 14:22:34 +0200245 put = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (put == NULL)
247 return -ENOMEM;
248 /* build header */
249 data = &put->data;
250 data->type = SNDRV_SEQ_INSTR_ATYPE_DATA;
251 strcpy(data->data.format, SNDRV_SEQ_INSTR_ID_OPL2_3);
252 /* build data section */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100253 xinstr = (struct fm_xinstrument *)(data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 xinstr->stype = FM_STRU_INSTR;
255
256 for (i = 0; i < 2; i++) {
257 xinstr->op[i].am_vib = sbi.operators[AM_VIB + i];
258 xinstr->op[i].ksl_level = sbi.operators[KSL_LEVEL + i];
259 xinstr->op[i].attack_decay = sbi.operators[ATTACK_DECAY + i];
260 xinstr->op[i].sustain_release = sbi.operators[SUSTAIN_RELEASE + i];
261 xinstr->op[i].wave_select = sbi.operators[WAVE_SELECT + i];
262 }
263 xinstr->feedback_connection[0] = sbi.operators[CONNECTION];
264
265 if (format == OPL3_PATCH) {
266 xinstr->type = FM_PATCH_OPL3;
267 for (i = 0; i < 2; i++) {
268 xinstr->op[i+2].am_vib = sbi.operators[OFFSET_4OP + AM_VIB + i];
269 xinstr->op[i+2].ksl_level = sbi.operators[OFFSET_4OP + KSL_LEVEL + i];
270 xinstr->op[i+2].attack_decay = sbi.operators[OFFSET_4OP + ATTACK_DECAY + i];
271 xinstr->op[i+2].sustain_release = sbi.operators[OFFSET_4OP + SUSTAIN_RELEASE + i];
272 xinstr->op[i+2].wave_select = sbi.operators[OFFSET_4OP + WAVE_SELECT + i];
273 }
274 xinstr->feedback_connection[1] = sbi.operators[OFFSET_4OP + CONNECTION];
275 } else {
276 xinstr->type = FM_PATCH_OPL2;
277 }
278
279 put->id.instr.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3;
280 put->id.instr.bank = 127;
281 put->id.instr.prg = sbi.channel;
282 put->cmd = SNDRV_SEQ_INSTR_PUT_CMD_CREATE;
283
284 memset (&ev, 0, sizeof(ev));
285 ev.source.client = SNDRV_SEQ_CLIENT_OSS;
286 ev.dest = arg->addr;
287
288 ev.flags = SNDRV_SEQ_EVENT_LENGTH_VARUSR;
289 ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
290
291 fs = snd_enter_user();
292 __again:
293 ev.type = SNDRV_SEQ_EVENT_INSTR_PUT;
294 ev.data.ext.len = size;
295 ev.data.ext.ptr = put;
296
297 err = snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
298 opl3->seq_client, 0, 0);
299 if (err == -EBUSY) {
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100300 struct snd_seq_instr_header remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 memset (&remove, 0, sizeof(remove));
303 remove.cmd = SNDRV_SEQ_INSTR_FREE_CMD_SINGLE;
304 remove.id.instr = put->id.instr;
305
306 /* remove instrument */
307 ev.type = SNDRV_SEQ_EVENT_INSTR_FREE;
308 ev.data.ext.len = sizeof(remove);
309 ev.data.ext.ptr = &remove;
310
311 snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
312 opl3->seq_client, 0, 0);
313 goto __again;
314 }
315 snd_leave_user(fs);
316
317 kfree(put);
318 }
319 return err;
320}
321
322/* ioctl */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100323static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned long ioarg)
325{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100326 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 snd_assert(arg != NULL, return -ENXIO);
329 opl3 = arg->private_data;
330 switch (cmd) {
331 case SNDCTL_FM_LOAD_INSTR:
332 snd_printk("OPL3: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
333 return -EINVAL;
334
335 case SNDCTL_SYNTH_MEMAVL:
336 return 0x7fffffff;
337
338 case SNDCTL_FM_4OP_ENABLE:
339 // handled automatically by OPL instrument type
340 return 0;
341
342 default:
343 return -EINVAL;
344 }
345 return 0;
346}
347
348/* reset device */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100349static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100351 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 snd_assert(arg != NULL, return -ENXIO);
354 opl3 = arg->private_data;
355
356 return 0;
357}