xref: /openbmc/linux/sound/drivers/opl3/opl3_oss.c (revision 8fdf9062)
1 /*
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 <linux/export.h>
22 #include "opl3_voice.h"
23 
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);
29 
30 /* operators */
31 
32 static struct snd_seq_oss_callback oss_callback = {
33 	.owner = 	THIS_MODULE,
34 	.open =		snd_opl3_open_seq_oss,
35 	.close =	snd_opl3_close_seq_oss,
36 	.ioctl =	snd_opl3_ioctl_seq_oss,
37 	.load_patch =	snd_opl3_load_patch_seq_oss,
38 	.reset =	snd_opl3_reset_seq_oss,
39 };
40 
41 static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
42 				    void *private_data, int atomic, int hop)
43 {
44 	struct snd_opl3 *opl3 = private_data;
45 
46 	if (ev->type != SNDRV_SEQ_EVENT_OSS)
47 		snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
48 	return 0;
49 }
50 
51 /* ------------------------------ */
52 
53 static void snd_opl3_oss_free_port(void *private_data)
54 {
55 	struct snd_opl3 *opl3 = private_data;
56 
57 	snd_midi_channel_free_set(opl3->oss_chset);
58 }
59 
60 static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
61 {
62 	struct snd_seq_port_callback callbacks;
63 	char name[32];
64 	int voices, opl_ver;
65 
66 	voices = (opl3->hardware < OPL3_HW_OPL3) ?
67 		MAX_OPL2_VOICES : MAX_OPL3_VOICES;
68 	opl3->oss_chset = snd_midi_channel_alloc_set(voices);
69 	if (opl3->oss_chset == NULL)
70 		return -ENOMEM;
71 	opl3->oss_chset->private_data = opl3;
72 
73 	memset(&callbacks, 0, sizeof(callbacks));
74 	callbacks.owner = THIS_MODULE;
75 	callbacks.event_input = snd_opl3_oss_event_input;
76 	callbacks.private_free = snd_opl3_oss_free_port;
77 	callbacks.private_data = opl3;
78 
79 	opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
80 	sprintf(name, "OPL%i OSS Port", opl_ver);
81 
82 	opl3->oss_chset->client = opl3->seq_client;
83 	opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
84 							  SNDRV_SEQ_PORT_CAP_WRITE,
85 							  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
86 							  SNDRV_SEQ_PORT_TYPE_MIDI_GM |
87 							  SNDRV_SEQ_PORT_TYPE_HARDWARE |
88 							  SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
89 							  voices, voices,
90 							  name);
91 	if (opl3->oss_chset->port < 0) {
92 		int port;
93 		port = opl3->oss_chset->port;
94 		snd_midi_channel_free_set(opl3->oss_chset);
95 		return port;
96 	}
97 	return 0;
98 }
99 
100 /* ------------------------------ */
101 
102 /* register OSS synth */
103 void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
104 {
105 	struct snd_seq_oss_reg *arg;
106 	struct snd_seq_device *dev;
107 
108 	if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
109 			       sizeof(struct snd_seq_oss_reg), &dev) < 0)
110 		return;
111 
112 	opl3->oss_seq_dev = dev;
113 	strlcpy(dev->name, name, sizeof(dev->name));
114 	arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
115 	arg->type = SYNTH_TYPE_FM;
116 	if (opl3->hardware < OPL3_HW_OPL3) {
117 		arg->subtype = FM_TYPE_ADLIB;
118 		arg->nvoices = MAX_OPL2_VOICES;
119 	} else {
120 		arg->subtype = FM_TYPE_OPL3;
121 		arg->nvoices = MAX_OPL3_VOICES;
122 	}
123 	arg->oper = oss_callback;
124 	arg->private_data = opl3;
125 
126 	if (snd_opl3_oss_create_port(opl3)) {
127 		/* register to OSS synth table */
128 		snd_device_register(opl3->card, dev);
129 	}
130 }
131 
132 /* unregister */
133 void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
134 {
135 	if (opl3->oss_seq_dev) {
136 		/* The instance should have been released in prior */
137 		opl3->oss_seq_dev = NULL;
138 	}
139 }
140 
141 /* ------------------------------ */
142 
143 /* open OSS sequencer */
144 static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
145 {
146 	struct snd_opl3 *opl3 = closure;
147 	int err;
148 
149 	if (snd_BUG_ON(!arg))
150 		return -ENXIO;
151 
152 	if ((err = snd_opl3_synth_setup(opl3)) < 0)
153 		return err;
154 
155 	/* fill the argument data */
156 	arg->private_data = opl3;
157 	arg->addr.client = opl3->oss_chset->client;
158 	arg->addr.port = opl3->oss_chset->port;
159 
160 	if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
161 		return err;
162 
163 	opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
164 	return 0;
165 }
166 
167 /* close OSS sequencer */
168 static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
169 {
170 	struct snd_opl3 *opl3;
171 
172 	if (snd_BUG_ON(!arg))
173 		return -ENXIO;
174 	opl3 = arg->private_data;
175 
176 	snd_opl3_synth_cleanup(opl3);
177 
178 	snd_opl3_synth_use_dec(opl3);
179 	return 0;
180 }
181 
182 /* load patch */
183 
184 /* from sound_config.h */
185 #define SBFM_MAXINSTR	256
186 
187 static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
188 				       const char __user *buf, int offs, int count)
189 {
190 	struct snd_opl3 *opl3;
191 	struct sbi_instrument sbi;
192 	char name[32];
193 	int err, type;
194 
195 	if (snd_BUG_ON(!arg))
196 		return -ENXIO;
197 	opl3 = arg->private_data;
198 
199 	if (format == FM_PATCH)
200 		type = FM_PATCH_OPL2;
201 	else if (format == OPL3_PATCH)
202 		type = FM_PATCH_OPL3;
203 	else
204 		return -EINVAL;
205 
206 	if (count < (int)sizeof(sbi)) {
207 		snd_printk(KERN_ERR "FM Error: Patch record too short\n");
208 		return -EINVAL;
209 	}
210 	if (copy_from_user(&sbi, buf, sizeof(sbi)))
211 		return -EFAULT;
212 
213 	if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
214 		snd_printk(KERN_ERR "FM Error: Invalid instrument number %d\n",
215 			   sbi.channel);
216 		return -EINVAL;
217 	}
218 
219 	memset(name, 0, sizeof(name));
220 	sprintf(name, "Chan%d", sbi.channel);
221 
222 	err = snd_opl3_load_patch(opl3, sbi.channel, 127, type, name, NULL,
223 				  sbi.operators);
224 	if (err < 0)
225 		return err;
226 
227 	return sizeof(sbi);
228 }
229 
230 /* ioctl */
231 static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
232 				  unsigned long ioarg)
233 {
234 	if (snd_BUG_ON(!arg))
235 		return -ENXIO;
236 	switch (cmd) {
237 		case SNDCTL_FM_LOAD_INSTR:
238 			snd_printk(KERN_ERR "OPL3: "
239 				   "Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. "
240 				   "Fix the program.\n");
241 			return -EINVAL;
242 
243 		case SNDCTL_SYNTH_MEMAVL:
244 			return 0x7fffffff;
245 
246 		case SNDCTL_FM_4OP_ENABLE:
247 			// handled automatically by OPL instrument type
248 			return 0;
249 
250 		default:
251 			return -EINVAL;
252 	}
253 	return 0;
254 }
255 
256 /* reset device */
257 static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
258 {
259 	if (snd_BUG_ON(!arg))
260 		return -ENXIO;
261 
262 	return 0;
263 }
264