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