opl3_oss.c 6.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  Interface for OSS sequencer emulation
 *
 *  Copyright (C) 2000 Uros Bizjak <uros@kss-loka.si>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include "opl3_voice.h"
#include <linux/slab.h>

24 25 26 27 28
static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg);
static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg);
static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, const char __user *buf, int offs, int count);
static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg);
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

/* */

static inline mm_segment_t snd_enter_user(void)
{
	mm_segment_t fs = get_fs();
	set_fs(get_ds());
	return fs;
}

static inline void snd_leave_user(mm_segment_t fs)
{
	set_fs(fs);
}

/* operators */

46
extern struct snd_midi_op opl3_ops;
L
Linus Torvalds 已提交
47

48
static struct snd_seq_oss_callback oss_callback = {
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56
	.owner = 	THIS_MODULE,
	.open =		snd_opl3_open_seq_oss,
	.close =	snd_opl3_close_seq_oss,
	.ioctl =	snd_opl3_ioctl_seq_oss,
	.load_patch =	snd_opl3_load_patch_seq_oss,
	.reset =	snd_opl3_reset_seq_oss,
};

57
static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
L
Linus Torvalds 已提交
58 59
				    void *private_data, int atomic, int hop)
{
60
	struct snd_opl3 *opl3 = private_data;
L
Linus Torvalds 已提交
61 62 63 64 65 66 67 68 69 70

	if (ev->type != SNDRV_SEQ_EVENT_OSS)
		snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
	return 0;
}

/* ------------------------------ */

static void snd_opl3_oss_free_port(void *private_data)
{
71
	struct snd_opl3 *opl3 = private_data;
L
Linus Torvalds 已提交
72 73 74 75

	snd_midi_channel_free_set(opl3->oss_chset);
}

76
static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
L
Linus Torvalds 已提交
77
{
78
	struct snd_seq_port_callback callbacks;
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	char name[32];
	int voices, opl_ver;

	voices = (opl3->hardware < OPL3_HW_OPL3) ?
		MAX_OPL2_VOICES : MAX_OPL3_VOICES;
	opl3->oss_chset = snd_midi_channel_alloc_set(voices);
	if (opl3->oss_chset == NULL)
		return -ENOMEM;
	opl3->oss_chset->private_data = opl3;

	memset(&callbacks, 0, sizeof(callbacks));
	callbacks.owner = THIS_MODULE;
	callbacks.event_input = snd_opl3_oss_event_input;
	callbacks.private_free = snd_opl3_oss_free_port;
	callbacks.private_data = opl3;

	opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
	sprintf(name, "OPL%i OSS Port", opl_ver);

	opl3->oss_chset->client = opl3->seq_client;
	opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
							  SNDRV_SEQ_PORT_CAP_WRITE,
							  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
102 103 104
							  SNDRV_SEQ_PORT_TYPE_MIDI_GM |
							  SNDRV_SEQ_PORT_TYPE_HARDWARE |
							  SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
L
Linus Torvalds 已提交
105 106 107
							  voices, voices,
							  name);
	if (opl3->oss_chset->port < 0) {
108 109
		int port;
		port = opl3->oss_chset->port;
L
Linus Torvalds 已提交
110
		snd_midi_channel_free_set(opl3->oss_chset);
111
		return port;
L
Linus Torvalds 已提交
112 113 114 115 116 117 118
	}
	return 0;
}

/* ------------------------------ */

/* register OSS synth */
119
void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
L
Linus Torvalds 已提交
120
{
121 122
	struct snd_seq_oss_reg *arg;
	struct snd_seq_device *dev;
L
Linus Torvalds 已提交
123 124

	if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
125
			       sizeof(struct snd_seq_oss_reg), &dev) < 0)
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
		return;

	opl3->oss_seq_dev = dev;
	strlcpy(dev->name, name, sizeof(dev->name));
	arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
	arg->type = SYNTH_TYPE_FM;
	if (opl3->hardware < OPL3_HW_OPL3) {
		arg->subtype = FM_TYPE_ADLIB;
		arg->nvoices = MAX_OPL2_VOICES;
	} else {
		arg->subtype = FM_TYPE_OPL3;
		arg->nvoices = MAX_OPL3_VOICES;
	}
	arg->oper = oss_callback;
	arg->private_data = opl3;

142 143 144 145
	if (snd_opl3_oss_create_port(opl3)) {
		/* register to OSS synth table */
		snd_device_register(opl3->card, dev);
	}
L
Linus Torvalds 已提交
146 147 148
}

/* unregister */
149
void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
L
Linus Torvalds 已提交
150 151
{
	if (opl3->oss_seq_dev) {
152
		/* The instance should have been released in prior */
L
Linus Torvalds 已提交
153 154 155 156 157 158 159
		opl3->oss_seq_dev = NULL;
	}
}

/* ------------------------------ */

/* open OSS sequencer */
160
static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
L
Linus Torvalds 已提交
161
{
162
	struct snd_opl3 *opl3 = closure;
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	int err;

	snd_assert(arg != NULL, return -ENXIO);

	if ((err = snd_opl3_synth_setup(opl3)) < 0)
		return err;

	/* fill the argument data */
	arg->private_data = opl3;
	arg->addr.client = opl3->oss_chset->client;
	arg->addr.port = opl3->oss_chset->port;

	if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
		return err;

	opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
	return 0;
}

/* close OSS sequencer */
183
static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
L
Linus Torvalds 已提交
184
{
185
	struct snd_opl3 *opl3;
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

	snd_assert(arg != NULL, return -ENXIO);
	opl3 = arg->private_data;

	snd_opl3_synth_cleanup(opl3);

	snd_opl3_synth_use_dec(opl3);
	return 0;
}

/* load patch */

/* from sound_config.h */
#define SBFM_MAXINSTR	256

201
static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
L
Linus Torvalds 已提交
202 203
				       const char __user *buf, int offs, int count)
{
204
	struct snd_opl3 *opl3;
205 206 207
	struct sbi_instrument sbi;
	char name[32];
	int err, type;
L
Linus Torvalds 已提交
208 209 210 211

	snd_assert(arg != NULL, return -ENXIO);
	opl3 = arg->private_data;

212 213 214 215 216 217
	if (format == FM_PATCH)
		type = FM_PATCH_OPL2;
	else if (format == OPL3_PATCH)
		type = FM_PATCH_OPL3;
	else
		return -EINVAL;
L
Linus Torvalds 已提交
218

219 220 221 222 223 224
	if (count < (int)sizeof(sbi)) {
		snd_printk("FM Error: Patch record too short\n");
		return -EINVAL;
	}
	if (copy_from_user(&sbi, buf, sizeof(sbi)))
		return -EFAULT;
L
Linus Torvalds 已提交
225

226 227 228 229 230
	if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
		snd_printk("FM Error: Invalid instrument number %d\n",
			   sbi.channel);
		return -EINVAL;
	}
L
Linus Torvalds 已提交
231

232 233
	memset(name, 0, sizeof(name));
	sprintf(name, "Chan%d", sbi.channel);
L
Linus Torvalds 已提交
234

235 236 237 238
	err = snd_opl3_load_patch(opl3, sbi.channel, 127, type, name, NULL,
				  sbi.operators);
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
239

240
	return sizeof(sbi);
L
Linus Torvalds 已提交
241 242 243
}

/* ioctl */
244
static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
L
Linus Torvalds 已提交
245 246
				  unsigned long ioarg)
{
247
	struct snd_opl3 *opl3;
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

	snd_assert(arg != NULL, return -ENXIO);
	opl3 = arg->private_data;
	switch (cmd) {
		case SNDCTL_FM_LOAD_INSTR:
			snd_printk("OPL3: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
			return -EINVAL;

		case SNDCTL_SYNTH_MEMAVL:
			return 0x7fffffff;

		case SNDCTL_FM_4OP_ENABLE:
			// handled automatically by OPL instrument type
			return 0;

		default:
			return -EINVAL;
	}
	return 0;
}

/* reset device */
270
static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
L
Linus Torvalds 已提交
271
{
272
	struct snd_opl3 *opl3;
L
Linus Torvalds 已提交
273 274 275 276 277 278

	snd_assert(arg != NULL, return -ENXIO);
	opl3 = arg->private_data;

	return 0;
}