Linux Audio

Check our new training course

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