1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Routines for OPL2/OPL3/OPL4 control
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/slab.h>
9*4882a593Smuzhiyun #include <linux/export.h>
10*4882a593Smuzhiyun #include <linux/nospec.h>
11*4882a593Smuzhiyun #include <sound/opl3.h>
12*4882a593Smuzhiyun #include <sound/asound_fm.h>
13*4882a593Smuzhiyun #include "opl3_voice.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_SEQUENCER)
16*4882a593Smuzhiyun #define OPL3_SUPPORT_SYNTH
17*4882a593Smuzhiyun #endif
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun * There is 18 possible 2 OP voices
21*4882a593Smuzhiyun * (9 in the left and 9 in the right).
22*4882a593Smuzhiyun * The first OP is the modulator and 2nd is the carrier.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * The first three voices in the both sides may be connected
25*4882a593Smuzhiyun * with another voice to a 4 OP voice. For example voice 0
26*4882a593Smuzhiyun * can be connected with voice 3. The operators of voice 3 are
27*4882a593Smuzhiyun * used as operators 3 and 4 of the new 4 OP voice.
28*4882a593Smuzhiyun * In this case the 2 OP voice number 0 is the 'first half' and
29*4882a593Smuzhiyun * voice 3 is the second.
30*4882a593Smuzhiyun */
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun * Register offset table for OPL2/3 voices,
35*4882a593Smuzhiyun * OPL2 / one OPL3 register array side only
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun char snd_opl3_regmap[MAX_OPL2_VOICES][4] =
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun /* OP1 OP2 OP3 OP4 */
41*4882a593Smuzhiyun /* ------------------------ */
42*4882a593Smuzhiyun { 0x00, 0x03, 0x08, 0x0b },
43*4882a593Smuzhiyun { 0x01, 0x04, 0x09, 0x0c },
44*4882a593Smuzhiyun { 0x02, 0x05, 0x0a, 0x0d },
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun { 0x08, 0x0b, 0x00, 0x00 },
47*4882a593Smuzhiyun { 0x09, 0x0c, 0x00, 0x00 },
48*4882a593Smuzhiyun { 0x0a, 0x0d, 0x00, 0x00 },
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun { 0x10, 0x13, 0x00, 0x00 }, /* used by percussive voices */
51*4882a593Smuzhiyun { 0x11, 0x14, 0x00, 0x00 }, /* if the percussive mode */
52*4882a593Smuzhiyun { 0x12, 0x15, 0x00, 0x00 } /* is selected (only left reg block) */
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl3_regmap);
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * prototypes
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note);
61*4882a593Smuzhiyun static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice);
62*4882a593Smuzhiyun static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params);
63*4882a593Smuzhiyun static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode);
64*4882a593Smuzhiyun static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* ------------------------------ */
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * open the device exclusively
70*4882a593Smuzhiyun */
snd_opl3_open(struct snd_hwdep * hw,struct file * file)71*4882a593Smuzhiyun int snd_opl3_open(struct snd_hwdep * hw, struct file *file)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun return 0;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * ioctl for hwdep device:
78*4882a593Smuzhiyun */
snd_opl3_ioctl(struct snd_hwdep * hw,struct file * file,unsigned int cmd,unsigned long arg)79*4882a593Smuzhiyun int snd_opl3_ioctl(struct snd_hwdep * hw, struct file *file,
80*4882a593Smuzhiyun unsigned int cmd, unsigned long arg)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun struct snd_opl3 *opl3 = hw->private_data;
83*4882a593Smuzhiyun void __user *argp = (void __user *)arg;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (snd_BUG_ON(!opl3))
86*4882a593Smuzhiyun return -EINVAL;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun switch (cmd) {
89*4882a593Smuzhiyun /* get information */
90*4882a593Smuzhiyun case SNDRV_DM_FM_IOCTL_INFO:
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun struct snd_dm_fm_info info;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun memset(&info, 0, sizeof(info));
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun info.fm_mode = opl3->fm_mode;
97*4882a593Smuzhiyun info.rhythm = opl3->rhythm;
98*4882a593Smuzhiyun if (copy_to_user(argp, &info, sizeof(struct snd_dm_fm_info)))
99*4882a593Smuzhiyun return -EFAULT;
100*4882a593Smuzhiyun return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun case SNDRV_DM_FM_IOCTL_RESET:
104*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
105*4882a593Smuzhiyun case SNDRV_DM_FM_OSS_IOCTL_RESET:
106*4882a593Smuzhiyun #endif
107*4882a593Smuzhiyun snd_opl3_reset(opl3);
108*4882a593Smuzhiyun return 0;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun case SNDRV_DM_FM_IOCTL_PLAY_NOTE:
111*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
112*4882a593Smuzhiyun case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE:
113*4882a593Smuzhiyun #endif
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct snd_dm_fm_note note;
116*4882a593Smuzhiyun if (copy_from_user(¬e, argp, sizeof(struct snd_dm_fm_note)))
117*4882a593Smuzhiyun return -EFAULT;
118*4882a593Smuzhiyun return snd_opl3_play_note(opl3, ¬e);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun case SNDRV_DM_FM_IOCTL_SET_VOICE:
122*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
123*4882a593Smuzhiyun case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE:
124*4882a593Smuzhiyun #endif
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun struct snd_dm_fm_voice voice;
127*4882a593Smuzhiyun if (copy_from_user(&voice, argp, sizeof(struct snd_dm_fm_voice)))
128*4882a593Smuzhiyun return -EFAULT;
129*4882a593Smuzhiyun return snd_opl3_set_voice(opl3, &voice);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun case SNDRV_DM_FM_IOCTL_SET_PARAMS:
133*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
134*4882a593Smuzhiyun case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS:
135*4882a593Smuzhiyun #endif
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun struct snd_dm_fm_params params;
138*4882a593Smuzhiyun if (copy_from_user(¶ms, argp, sizeof(struct snd_dm_fm_params)))
139*4882a593Smuzhiyun return -EFAULT;
140*4882a593Smuzhiyun return snd_opl3_set_params(opl3, ¶ms);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun case SNDRV_DM_FM_IOCTL_SET_MODE:
144*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
145*4882a593Smuzhiyun case SNDRV_DM_FM_OSS_IOCTL_SET_MODE:
146*4882a593Smuzhiyun #endif
147*4882a593Smuzhiyun return snd_opl3_set_mode(opl3, (int) arg);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun case SNDRV_DM_FM_IOCTL_SET_CONNECTION:
150*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
151*4882a593Smuzhiyun case SNDRV_DM_FM_OSS_IOCTL_SET_OPL:
152*4882a593Smuzhiyun #endif
153*4882a593Smuzhiyun return snd_opl3_set_connection(opl3, (int) arg);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun #ifdef OPL3_SUPPORT_SYNTH
156*4882a593Smuzhiyun case SNDRV_DM_FM_IOCTL_CLEAR_PATCHES:
157*4882a593Smuzhiyun snd_opl3_clear_patches(opl3);
158*4882a593Smuzhiyun return 0;
159*4882a593Smuzhiyun #endif
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun #ifdef CONFIG_SND_DEBUG
162*4882a593Smuzhiyun default:
163*4882a593Smuzhiyun snd_printk(KERN_WARNING "unknown IOCTL: 0x%x\n", cmd);
164*4882a593Smuzhiyun #endif
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun return -ENOTTY;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun * close the device
171*4882a593Smuzhiyun */
snd_opl3_release(struct snd_hwdep * hw,struct file * file)172*4882a593Smuzhiyun int snd_opl3_release(struct snd_hwdep * hw, struct file *file)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun struct snd_opl3 *opl3 = hw->private_data;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun snd_opl3_reset(opl3);
177*4882a593Smuzhiyun return 0;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun #ifdef OPL3_SUPPORT_SYNTH
181*4882a593Smuzhiyun /*
182*4882a593Smuzhiyun * write the device - load patches
183*4882a593Smuzhiyun */
snd_opl3_write(struct snd_hwdep * hw,const char __user * buf,long count,loff_t * offset)184*4882a593Smuzhiyun long snd_opl3_write(struct snd_hwdep *hw, const char __user *buf, long count,
185*4882a593Smuzhiyun loff_t *offset)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun struct snd_opl3 *opl3 = hw->private_data;
188*4882a593Smuzhiyun long result = 0;
189*4882a593Smuzhiyun int err = 0;
190*4882a593Smuzhiyun struct sbi_patch inst;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun while (count >= sizeof(inst)) {
193*4882a593Smuzhiyun unsigned char type;
194*4882a593Smuzhiyun if (copy_from_user(&inst, buf, sizeof(inst)))
195*4882a593Smuzhiyun return -EFAULT;
196*4882a593Smuzhiyun if (!memcmp(inst.key, FM_KEY_SBI, 4) ||
197*4882a593Smuzhiyun !memcmp(inst.key, FM_KEY_2OP, 4))
198*4882a593Smuzhiyun type = FM_PATCH_OPL2;
199*4882a593Smuzhiyun else if (!memcmp(inst.key, FM_KEY_4OP, 4))
200*4882a593Smuzhiyun type = FM_PATCH_OPL3;
201*4882a593Smuzhiyun else /* invalid type */
202*4882a593Smuzhiyun break;
203*4882a593Smuzhiyun err = snd_opl3_load_patch(opl3, inst.prog, inst.bank, type,
204*4882a593Smuzhiyun inst.name, inst.extension,
205*4882a593Smuzhiyun inst.data);
206*4882a593Smuzhiyun if (err < 0)
207*4882a593Smuzhiyun break;
208*4882a593Smuzhiyun result += sizeof(inst);
209*4882a593Smuzhiyun count -= sizeof(inst);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun return result > 0 ? result : err;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /*
216*4882a593Smuzhiyun * Patch management
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /* offsets for SBI params */
220*4882a593Smuzhiyun #define AM_VIB 0
221*4882a593Smuzhiyun #define KSL_LEVEL 2
222*4882a593Smuzhiyun #define ATTACK_DECAY 4
223*4882a593Smuzhiyun #define SUSTAIN_RELEASE 6
224*4882a593Smuzhiyun #define WAVE_SELECT 8
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /* offset for SBI instrument */
227*4882a593Smuzhiyun #define CONNECTION 10
228*4882a593Smuzhiyun #define OFFSET_4OP 11
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun * load a patch, obviously.
232*4882a593Smuzhiyun *
233*4882a593Smuzhiyun * loaded on the given program and bank numbers with the given type
234*4882a593Smuzhiyun * (FM_PATCH_OPLx).
235*4882a593Smuzhiyun * data is the pointer of SBI record _without_ header (key and name).
236*4882a593Smuzhiyun * name is the name string of the patch.
237*4882a593Smuzhiyun * ext is the extension data of 7 bytes long (stored in name of SBI
238*4882a593Smuzhiyun * data up to offset 25), or NULL to skip.
239*4882a593Smuzhiyun * return 0 if successful or a negative error code.
240*4882a593Smuzhiyun */
snd_opl3_load_patch(struct snd_opl3 * opl3,int prog,int bank,int type,const char * name,const unsigned char * ext,const unsigned char * data)241*4882a593Smuzhiyun int snd_opl3_load_patch(struct snd_opl3 *opl3,
242*4882a593Smuzhiyun int prog, int bank, int type,
243*4882a593Smuzhiyun const char *name,
244*4882a593Smuzhiyun const unsigned char *ext,
245*4882a593Smuzhiyun const unsigned char *data)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun struct fm_patch *patch;
248*4882a593Smuzhiyun int i;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun patch = snd_opl3_find_patch(opl3, prog, bank, 1);
251*4882a593Smuzhiyun if (!patch)
252*4882a593Smuzhiyun return -ENOMEM;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun patch->type = type;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun for (i = 0; i < 2; i++) {
257*4882a593Smuzhiyun patch->inst.op[i].am_vib = data[AM_VIB + i];
258*4882a593Smuzhiyun patch->inst.op[i].ksl_level = data[KSL_LEVEL + i];
259*4882a593Smuzhiyun patch->inst.op[i].attack_decay = data[ATTACK_DECAY + i];
260*4882a593Smuzhiyun patch->inst.op[i].sustain_release = data[SUSTAIN_RELEASE + i];
261*4882a593Smuzhiyun patch->inst.op[i].wave_select = data[WAVE_SELECT + i];
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun patch->inst.feedback_connection[0] = data[CONNECTION];
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun if (type == FM_PATCH_OPL3) {
266*4882a593Smuzhiyun for (i = 0; i < 2; i++) {
267*4882a593Smuzhiyun patch->inst.op[i+2].am_vib =
268*4882a593Smuzhiyun data[OFFSET_4OP + AM_VIB + i];
269*4882a593Smuzhiyun patch->inst.op[i+2].ksl_level =
270*4882a593Smuzhiyun data[OFFSET_4OP + KSL_LEVEL + i];
271*4882a593Smuzhiyun patch->inst.op[i+2].attack_decay =
272*4882a593Smuzhiyun data[OFFSET_4OP + ATTACK_DECAY + i];
273*4882a593Smuzhiyun patch->inst.op[i+2].sustain_release =
274*4882a593Smuzhiyun data[OFFSET_4OP + SUSTAIN_RELEASE + i];
275*4882a593Smuzhiyun patch->inst.op[i+2].wave_select =
276*4882a593Smuzhiyun data[OFFSET_4OP + WAVE_SELECT + i];
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun patch->inst.feedback_connection[1] =
279*4882a593Smuzhiyun data[OFFSET_4OP + CONNECTION];
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun if (ext) {
283*4882a593Smuzhiyun patch->inst.echo_delay = ext[0];
284*4882a593Smuzhiyun patch->inst.echo_atten = ext[1];
285*4882a593Smuzhiyun patch->inst.chorus_spread = ext[2];
286*4882a593Smuzhiyun patch->inst.trnsps = ext[3];
287*4882a593Smuzhiyun patch->inst.fix_dur = ext[4];
288*4882a593Smuzhiyun patch->inst.modes = ext[5];
289*4882a593Smuzhiyun patch->inst.fix_key = ext[6];
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun if (name)
293*4882a593Smuzhiyun strlcpy(patch->name, name, sizeof(patch->name));
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun return 0;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl3_load_patch);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /*
300*4882a593Smuzhiyun * find a patch with the given program and bank numbers, returns its pointer
301*4882a593Smuzhiyun * if no matching patch is found and create_patch is set, it creates a
302*4882a593Smuzhiyun * new patch object.
303*4882a593Smuzhiyun */
snd_opl3_find_patch(struct snd_opl3 * opl3,int prog,int bank,int create_patch)304*4882a593Smuzhiyun struct fm_patch *snd_opl3_find_patch(struct snd_opl3 *opl3, int prog, int bank,
305*4882a593Smuzhiyun int create_patch)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun /* pretty dumb hash key */
308*4882a593Smuzhiyun unsigned int key = (prog + bank) % OPL3_PATCH_HASH_SIZE;
309*4882a593Smuzhiyun struct fm_patch *patch;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun for (patch = opl3->patch_table[key]; patch; patch = patch->next) {
312*4882a593Smuzhiyun if (patch->prog == prog && patch->bank == bank)
313*4882a593Smuzhiyun return patch;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun if (!create_patch)
316*4882a593Smuzhiyun return NULL;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun patch = kzalloc(sizeof(*patch), GFP_KERNEL);
319*4882a593Smuzhiyun if (!patch)
320*4882a593Smuzhiyun return NULL;
321*4882a593Smuzhiyun patch->prog = prog;
322*4882a593Smuzhiyun patch->bank = bank;
323*4882a593Smuzhiyun patch->next = opl3->patch_table[key];
324*4882a593Smuzhiyun opl3->patch_table[key] = patch;
325*4882a593Smuzhiyun return patch;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl3_find_patch);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /*
330*4882a593Smuzhiyun * Clear all patches of the given OPL3 instance
331*4882a593Smuzhiyun */
snd_opl3_clear_patches(struct snd_opl3 * opl3)332*4882a593Smuzhiyun void snd_opl3_clear_patches(struct snd_opl3 *opl3)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun int i;
335*4882a593Smuzhiyun for (i = 0; i < OPL3_PATCH_HASH_SIZE; i++) {
336*4882a593Smuzhiyun struct fm_patch *patch, *next;
337*4882a593Smuzhiyun for (patch = opl3->patch_table[i]; patch; patch = next) {
338*4882a593Smuzhiyun next = patch->next;
339*4882a593Smuzhiyun kfree(patch);
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun memset(opl3->patch_table, 0, sizeof(opl3->patch_table));
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun #endif /* OPL3_SUPPORT_SYNTH */
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /* ------------------------------ */
347*4882a593Smuzhiyun
snd_opl3_reset(struct snd_opl3 * opl3)348*4882a593Smuzhiyun void snd_opl3_reset(struct snd_opl3 * opl3)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun unsigned short opl3_reg;
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun unsigned short reg_side;
353*4882a593Smuzhiyun unsigned char voice_offset;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun int max_voices, i;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun max_voices = (opl3->hardware < OPL3_HW_OPL3) ?
358*4882a593Smuzhiyun MAX_OPL2_VOICES : MAX_OPL3_VOICES;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun for (i = 0; i < max_voices; i++) {
361*4882a593Smuzhiyun /* Get register array side and offset of voice */
362*4882a593Smuzhiyun if (i < MAX_OPL2_VOICES) {
363*4882a593Smuzhiyun /* Left register block for voices 0 .. 8 */
364*4882a593Smuzhiyun reg_side = OPL3_LEFT;
365*4882a593Smuzhiyun voice_offset = i;
366*4882a593Smuzhiyun } else {
367*4882a593Smuzhiyun /* Right register block for voices 9 .. 17 */
368*4882a593Smuzhiyun reg_side = OPL3_RIGHT;
369*4882a593Smuzhiyun voice_offset = i - MAX_OPL2_VOICES;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][0]);
372*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 1 volume */
373*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][1]);
374*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 2 volume */
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
377*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, 0x00); /* Note off */
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun opl3->max_voices = MAX_OPL2_VOICES;
381*4882a593Smuzhiyun opl3->fm_mode = SNDRV_DM_FM_MODE_OPL2;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
384*4882a593Smuzhiyun opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00); /* Melodic mode */
385*4882a593Smuzhiyun opl3->rhythm = 0;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl3_reset);
389*4882a593Smuzhiyun
snd_opl3_play_note(struct snd_opl3 * opl3,struct snd_dm_fm_note * note)390*4882a593Smuzhiyun static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun unsigned short reg_side;
393*4882a593Smuzhiyun unsigned char voice_offset;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun unsigned short opl3_reg;
396*4882a593Smuzhiyun unsigned char reg_val;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun /* Voices 0 - 8 in OPL2 mode */
399*4882a593Smuzhiyun /* Voices 0 - 17 in OPL3 mode */
400*4882a593Smuzhiyun if (note->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
401*4882a593Smuzhiyun MAX_OPL3_VOICES : MAX_OPL2_VOICES))
402*4882a593Smuzhiyun return -EINVAL;
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /* Get register array side and offset of voice */
405*4882a593Smuzhiyun if (note->voice < MAX_OPL2_VOICES) {
406*4882a593Smuzhiyun /* Left register block for voices 0 .. 8 */
407*4882a593Smuzhiyun reg_side = OPL3_LEFT;
408*4882a593Smuzhiyun voice_offset = note->voice;
409*4882a593Smuzhiyun } else {
410*4882a593Smuzhiyun /* Right register block for voices 9 .. 17 */
411*4882a593Smuzhiyun reg_side = OPL3_RIGHT;
412*4882a593Smuzhiyun voice_offset = note->voice - MAX_OPL2_VOICES;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun /* Set lower 8 bits of note frequency */
416*4882a593Smuzhiyun reg_val = (unsigned char) note->fnum;
417*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
418*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, reg_val);
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun reg_val = 0x00;
421*4882a593Smuzhiyun /* Set output sound flag */
422*4882a593Smuzhiyun if (note->key_on)
423*4882a593Smuzhiyun reg_val |= OPL3_KEYON_BIT;
424*4882a593Smuzhiyun /* Set octave */
425*4882a593Smuzhiyun reg_val |= (note->octave << 2) & OPL3_BLOCKNUM_MASK;
426*4882a593Smuzhiyun /* Set higher 2 bits of note frequency */
427*4882a593Smuzhiyun reg_val |= (unsigned char) (note->fnum >> 8) & OPL3_FNUM_HIGH_MASK;
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun /* Set OPL3 KEYON_BLOCK register of requested voice */
430*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
431*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, reg_val);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun return 0;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun
snd_opl3_set_voice(struct snd_opl3 * opl3,struct snd_dm_fm_voice * voice)437*4882a593Smuzhiyun static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun unsigned short reg_side;
440*4882a593Smuzhiyun unsigned char op_offset;
441*4882a593Smuzhiyun unsigned char voice_offset, voice_op;
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun unsigned short opl3_reg;
444*4882a593Smuzhiyun unsigned char reg_val;
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun /* Only operators 1 and 2 */
447*4882a593Smuzhiyun if (voice->op > 1)
448*4882a593Smuzhiyun return -EINVAL;
449*4882a593Smuzhiyun /* Voices 0 - 8 in OPL2 mode */
450*4882a593Smuzhiyun /* Voices 0 - 17 in OPL3 mode */
451*4882a593Smuzhiyun if (voice->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
452*4882a593Smuzhiyun MAX_OPL3_VOICES : MAX_OPL2_VOICES))
453*4882a593Smuzhiyun return -EINVAL;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun /* Get register array side and offset of voice */
456*4882a593Smuzhiyun if (voice->voice < MAX_OPL2_VOICES) {
457*4882a593Smuzhiyun /* Left register block for voices 0 .. 8 */
458*4882a593Smuzhiyun reg_side = OPL3_LEFT;
459*4882a593Smuzhiyun voice_offset = voice->voice;
460*4882a593Smuzhiyun } else {
461*4882a593Smuzhiyun /* Right register block for voices 9 .. 17 */
462*4882a593Smuzhiyun reg_side = OPL3_RIGHT;
463*4882a593Smuzhiyun voice_offset = voice->voice - MAX_OPL2_VOICES;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun /* Get register offset of operator */
466*4882a593Smuzhiyun voice_offset = array_index_nospec(voice_offset, MAX_OPL2_VOICES);
467*4882a593Smuzhiyun voice_op = array_index_nospec(voice->op, 4);
468*4882a593Smuzhiyun op_offset = snd_opl3_regmap[voice_offset][voice_op];
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun reg_val = 0x00;
471*4882a593Smuzhiyun /* Set amplitude modulation (tremolo) effect */
472*4882a593Smuzhiyun if (voice->am)
473*4882a593Smuzhiyun reg_val |= OPL3_TREMOLO_ON;
474*4882a593Smuzhiyun /* Set vibrato effect */
475*4882a593Smuzhiyun if (voice->vibrato)
476*4882a593Smuzhiyun reg_val |= OPL3_VIBRATO_ON;
477*4882a593Smuzhiyun /* Set sustaining sound phase */
478*4882a593Smuzhiyun if (voice->do_sustain)
479*4882a593Smuzhiyun reg_val |= OPL3_SUSTAIN_ON;
480*4882a593Smuzhiyun /* Set keyboard scaling bit */
481*4882a593Smuzhiyun if (voice->kbd_scale)
482*4882a593Smuzhiyun reg_val |= OPL3_KSR;
483*4882a593Smuzhiyun /* Set harmonic or frequency multiplier */
484*4882a593Smuzhiyun reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun /* Set OPL3 AM_VIB register of requested voice/operator */
487*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
488*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, reg_val);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun /* Set decreasing volume of higher notes */
491*4882a593Smuzhiyun reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
492*4882a593Smuzhiyun /* Set output volume */
493*4882a593Smuzhiyun reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun /* Set OPL3 KSL_LEVEL register of requested voice/operator */
496*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
497*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, reg_val);
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /* Set attack phase level */
500*4882a593Smuzhiyun reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
501*4882a593Smuzhiyun /* Set decay phase level */
502*4882a593Smuzhiyun reg_val |= voice->decay & OPL3_DECAY_MASK;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
505*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
506*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, reg_val);
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun /* Set sustain phase level */
509*4882a593Smuzhiyun reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
510*4882a593Smuzhiyun /* Set release phase level */
511*4882a593Smuzhiyun reg_val |= voice->release & OPL3_RELEASE_MASK;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
514*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
515*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, reg_val);
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun /* Set inter-operator feedback */
518*4882a593Smuzhiyun reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
519*4882a593Smuzhiyun /* Set inter-operator connection */
520*4882a593Smuzhiyun if (voice->connection)
521*4882a593Smuzhiyun reg_val |= OPL3_CONNECTION_BIT;
522*4882a593Smuzhiyun /* OPL-3 only */
523*4882a593Smuzhiyun if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
524*4882a593Smuzhiyun if (voice->left)
525*4882a593Smuzhiyun reg_val |= OPL3_VOICE_TO_LEFT;
526*4882a593Smuzhiyun if (voice->right)
527*4882a593Smuzhiyun reg_val |= OPL3_VOICE_TO_RIGHT;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun /* Feedback/connection bits are applicable to voice */
530*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
531*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, reg_val);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /* Select waveform */
534*4882a593Smuzhiyun reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
535*4882a593Smuzhiyun opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
536*4882a593Smuzhiyun opl3->command(opl3, opl3_reg, reg_val);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun return 0;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun
snd_opl3_set_params(struct snd_opl3 * opl3,struct snd_dm_fm_params * params)541*4882a593Smuzhiyun static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params)
542*4882a593Smuzhiyun {
543*4882a593Smuzhiyun unsigned char reg_val;
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun reg_val = 0x00;
546*4882a593Smuzhiyun /* Set keyboard split method */
547*4882a593Smuzhiyun if (params->kbd_split)
548*4882a593Smuzhiyun reg_val |= OPL3_KEYBOARD_SPLIT;
549*4882a593Smuzhiyun opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun reg_val = 0x00;
552*4882a593Smuzhiyun /* Set amplitude modulation (tremolo) depth */
553*4882a593Smuzhiyun if (params->am_depth)
554*4882a593Smuzhiyun reg_val |= OPL3_TREMOLO_DEPTH;
555*4882a593Smuzhiyun /* Set vibrato depth */
556*4882a593Smuzhiyun if (params->vib_depth)
557*4882a593Smuzhiyun reg_val |= OPL3_VIBRATO_DEPTH;
558*4882a593Smuzhiyun /* Set percussion mode */
559*4882a593Smuzhiyun if (params->rhythm) {
560*4882a593Smuzhiyun reg_val |= OPL3_PERCUSSION_ENABLE;
561*4882a593Smuzhiyun opl3->rhythm = 1;
562*4882a593Smuzhiyun } else {
563*4882a593Smuzhiyun opl3->rhythm = 0;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun /* Play percussion instruments */
566*4882a593Smuzhiyun if (params->bass)
567*4882a593Smuzhiyun reg_val |= OPL3_BASSDRUM_ON;
568*4882a593Smuzhiyun if (params->snare)
569*4882a593Smuzhiyun reg_val |= OPL3_SNAREDRUM_ON;
570*4882a593Smuzhiyun if (params->tomtom)
571*4882a593Smuzhiyun reg_val |= OPL3_TOMTOM_ON;
572*4882a593Smuzhiyun if (params->cymbal)
573*4882a593Smuzhiyun reg_val |= OPL3_CYMBAL_ON;
574*4882a593Smuzhiyun if (params->hihat)
575*4882a593Smuzhiyun reg_val |= OPL3_HIHAT_ON;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
578*4882a593Smuzhiyun return 0;
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
snd_opl3_set_mode(struct snd_opl3 * opl3,int mode)581*4882a593Smuzhiyun static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
584*4882a593Smuzhiyun return -EINVAL;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun opl3->fm_mode = mode;
587*4882a593Smuzhiyun if (opl3->hardware >= OPL3_HW_OPL3)
588*4882a593Smuzhiyun opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00); /* Clear 4-op connections */
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun return 0;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
snd_opl3_set_connection(struct snd_opl3 * opl3,int connection)593*4882a593Smuzhiyun static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun unsigned char reg_val;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun /* OPL-3 only */
598*4882a593Smuzhiyun if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
599*4882a593Smuzhiyun return -EINVAL;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
602*4882a593Smuzhiyun OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
603*4882a593Smuzhiyun /* Set 4-op connections */
604*4882a593Smuzhiyun opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun return 0;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun
609