2 * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
4 * Routines for OPL2/OPL3/OPL4 control
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.
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.
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
22 #include <sound/opl3.h>
23 #include <sound/asound_fm.h>
25 <<<<<<< HEAD
:sound
/drivers
/opl3
/opl3_synth
.c
27 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
28 #define OPL3_SUPPORT_SYNTH
31 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:sound
/drivers
/opl3
/opl3_synth
.c
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.
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.
47 * Register offset table for OPL2/3 voices,
48 * OPL2 / one OPL3 register array side only
51 char snd_opl3_regmap
[MAX_OPL2_VOICES
][4] =
54 /* ------------------------ */
55 { 0x00, 0x03, 0x08, 0x0b },
56 { 0x01, 0x04, 0x09, 0x0c },
57 { 0x02, 0x05, 0x0a, 0x0d },
59 { 0x08, 0x0b, 0x00, 0x00 },
60 { 0x09, 0x0c, 0x00, 0x00 },
61 { 0x0a, 0x0d, 0x00, 0x00 },
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) */
68 EXPORT_SYMBOL(snd_opl3_regmap
);
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
);
79 /* ------------------------------ */
82 * open the device exclusively
84 int snd_opl3_open(struct snd_hwdep
* hw
, struct file
*file
)
90 * ioctl for hwdep device:
92 int snd_opl3_ioctl(struct snd_hwdep
* hw
, struct file
*file
,
93 unsigned int cmd
, unsigned long arg
)
95 struct snd_opl3
*opl3
= hw
->private_data
;
96 void __user
*argp
= (void __user
*)arg
;
98 snd_assert(opl3
!= NULL
, return -EINVAL
);
101 /* get information */
102 case SNDRV_DM_FM_IOCTL_INFO
:
104 struct snd_dm_fm_info info
;
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
)))
113 case SNDRV_DM_FM_IOCTL_RESET
:
114 #ifdef CONFIG_SND_OSSEMUL
115 case SNDRV_DM_FM_OSS_IOCTL_RESET
:
117 snd_opl3_reset(opl3
);
120 case SNDRV_DM_FM_IOCTL_PLAY_NOTE
:
121 #ifdef CONFIG_SND_OSSEMUL
122 case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE
:
125 struct snd_dm_fm_note note
;
126 if (copy_from_user(¬e
, argp
, sizeof(struct snd_dm_fm_note
)))
128 return snd_opl3_play_note(opl3
, ¬e
);
131 case SNDRV_DM_FM_IOCTL_SET_VOICE
:
132 #ifdef CONFIG_SND_OSSEMUL
133 case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE
:
136 struct snd_dm_fm_voice voice
;
137 if (copy_from_user(&voice
, argp
, sizeof(struct snd_dm_fm_voice
)))
139 return snd_opl3_set_voice(opl3
, &voice
);
142 case SNDRV_DM_FM_IOCTL_SET_PARAMS
:
143 #ifdef CONFIG_SND_OSSEMUL
144 case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS
:
147 struct snd_dm_fm_params params
;
148 if (copy_from_user(¶ms
, argp
, sizeof(struct snd_dm_fm_params
)))
150 return snd_opl3_set_params(opl3
, ¶ms
);
153 case SNDRV_DM_FM_IOCTL_SET_MODE
:
154 #ifdef CONFIG_SND_OSSEMUL
155 case SNDRV_DM_FM_OSS_IOCTL_SET_MODE
:
157 return snd_opl3_set_mode(opl3
, (int) arg
);
159 case SNDRV_DM_FM_IOCTL_SET_CONNECTION
:
160 #ifdef CONFIG_SND_OSSEMUL
161 case SNDRV_DM_FM_OSS_IOCTL_SET_OPL
:
163 return snd_opl3_set_connection(opl3
, (int) arg
);
165 <<<<<<< HEAD
:sound
/drivers
/opl3
/opl3_synth
.c
167 #ifdef OPL3_SUPPORT_SYNTH
168 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:sound
/drivers
/opl3
/opl3_synth
.c
169 case SNDRV_DM_FM_IOCTL_CLEAR_PATCHES
:
170 snd_opl3_clear_patches(opl3
);
172 <<<<<<< HEAD
:sound
/drivers
/opl3
/opl3_synth
.c
175 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:sound
/drivers
/opl3
/opl3_synth
.c
177 #ifdef CONFIG_SND_DEBUG
179 snd_printk("unknown IOCTL: 0x%x\n", cmd
);
188 int snd_opl3_release(struct snd_hwdep
* hw
, struct file
*file
)
190 struct snd_opl3
*opl3
= hw
->private_data
;
192 snd_opl3_reset(opl3
);
196 <<<<<<< HEAD
:sound
/drivers
/opl3
/opl3_synth
.c
198 #ifdef OPL3_SUPPORT_SYNTH
199 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:sound
/drivers
/opl3
/opl3_synth
.c
201 * write the device - load patches
203 long snd_opl3_write(struct snd_hwdep
*hw
, const char __user
*buf
, long count
,
206 struct snd_opl3
*opl3
= hw
->private_data
;
209 struct sbi_patch inst
;
211 while (count
>= sizeof(inst
)) {
213 if (copy_from_user(&inst
, buf
, sizeof(inst
)))
215 if (!memcmp(inst
.key
, FM_KEY_SBI
, 4) ||
216 !memcmp(inst
.key
, FM_KEY_2OP
, 4))
217 type
= FM_PATCH_OPL2
;
218 else if (!memcmp(inst
.key
, FM_KEY_4OP
, 4))
219 type
= FM_PATCH_OPL3
;
220 else /* invalid type */
222 err
= snd_opl3_load_patch(opl3
, inst
.prog
, inst
.bank
, type
,
223 inst
.name
, inst
.extension
,
227 result
+= sizeof(inst
);
228 count
-= sizeof(inst
);
230 return result
> 0 ? result
: err
;
238 /* offsets for SBI params */
241 #define ATTACK_DECAY 4
242 #define SUSTAIN_RELEASE 6
243 #define WAVE_SELECT 8
245 /* offset for SBI instrument */
246 #define CONNECTION 10
247 #define OFFSET_4OP 11
250 * load a patch, obviously.
252 * loaded on the given program and bank numbers with the given type
254 * data is the pointer of SBI record _without_ header (key and name).
255 * name is the name string of the patch.
256 * ext is the extension data of 7 bytes long (stored in name of SBI
257 * data up to offset 25), or NULL to skip.
258 * return 0 if successful or a negative error code.
260 int snd_opl3_load_patch(struct snd_opl3
*opl3
,
261 int prog
, int bank
, int type
,
263 const unsigned char *ext
,
264 const unsigned char *data
)
266 struct fm_patch
*patch
;
269 patch
= snd_opl3_find_patch(opl3
, prog
, bank
, 1);
275 for (i
= 0; i
< 2; i
++) {
276 patch
->inst
.op
[i
].am_vib
= data
[AM_VIB
+ i
];
277 patch
->inst
.op
[i
].ksl_level
= data
[KSL_LEVEL
+ i
];
278 patch
->inst
.op
[i
].attack_decay
= data
[ATTACK_DECAY
+ i
];
279 patch
->inst
.op
[i
].sustain_release
= data
[SUSTAIN_RELEASE
+ i
];
280 patch
->inst
.op
[i
].wave_select
= data
[WAVE_SELECT
+ i
];
282 patch
->inst
.feedback_connection
[0] = data
[CONNECTION
];
284 if (type
== FM_PATCH_OPL3
) {
285 for (i
= 0; i
< 2; i
++) {
286 patch
->inst
.op
[i
+2].am_vib
=
287 data
[OFFSET_4OP
+ AM_VIB
+ i
];
288 patch
->inst
.op
[i
+2].ksl_level
=
289 data
[OFFSET_4OP
+ KSL_LEVEL
+ i
];
290 patch
->inst
.op
[i
+2].attack_decay
=
291 data
[OFFSET_4OP
+ ATTACK_DECAY
+ i
];
292 patch
->inst
.op
[i
+2].sustain_release
=
293 data
[OFFSET_4OP
+ SUSTAIN_RELEASE
+ i
];
294 patch
->inst
.op
[i
+2].wave_select
=
295 data
[OFFSET_4OP
+ WAVE_SELECT
+ i
];
297 patch
->inst
.feedback_connection
[1] =
298 data
[OFFSET_4OP
+ CONNECTION
];
302 patch
->inst
.echo_delay
= ext
[0];
303 patch
->inst
.echo_atten
= ext
[1];
304 patch
->inst
.chorus_spread
= ext
[2];
305 patch
->inst
.trnsps
= ext
[3];
306 patch
->inst
.fix_dur
= ext
[4];
307 patch
->inst
.modes
= ext
[5];
308 patch
->inst
.fix_key
= ext
[6];
312 strlcpy(patch
->name
, name
, sizeof(patch
->name
));
316 EXPORT_SYMBOL(snd_opl3_load_patch
);
319 * find a patch with the given program and bank numbers, returns its pointer
320 * if no matching patch is found and create_patch is set, it creates a
323 struct fm_patch
*snd_opl3_find_patch(struct snd_opl3
*opl3
, int prog
, int bank
,
326 /* pretty dumb hash key */
327 unsigned int key
= (prog
+ bank
) % OPL3_PATCH_HASH_SIZE
;
328 struct fm_patch
*patch
;
330 for (patch
= opl3
->patch_table
[key
]; patch
; patch
= patch
->next
) {
331 if (patch
->prog
== prog
&& patch
->bank
== bank
)
337 patch
= kzalloc(sizeof(*patch
), GFP_KERNEL
);
342 patch
->next
= opl3
->patch_table
[key
];
343 opl3
->patch_table
[key
] = patch
;
346 EXPORT_SYMBOL(snd_opl3_find_patch
);
349 * Clear all patches of the given OPL3 instance
351 void snd_opl3_clear_patches(struct snd_opl3
*opl3
)
354 for (i
= 0; i
< OPL3_PATCH_HASH_SIZE
; i
++) {
355 struct fm_patch
*patch
, *next
;
356 for (patch
= opl3
->patch_table
[i
]; patch
; patch
= next
) {
361 memset(opl3
->patch_table
, 0, sizeof(opl3
->patch_table
));
363 <<<<<<< HEAD
:sound
/drivers
/opl3
/opl3_synth
.c
365 #endif /* OPL3_SUPPORT_SYNTH */
366 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:sound
/drivers
/opl3
/opl3_synth
.c
368 /* ------------------------------ */
370 void snd_opl3_reset(struct snd_opl3
* opl3
)
372 unsigned short opl3_reg
;
374 unsigned short reg_side
;
375 unsigned char voice_offset
;
379 max_voices
= (opl3
->hardware
< OPL3_HW_OPL3
) ?
380 MAX_OPL2_VOICES
: MAX_OPL3_VOICES
;
382 for (i
= 0; i
< max_voices
; i
++) {
383 /* Get register array side and offset of voice */
384 if (i
< MAX_OPL2_VOICES
) {
385 /* Left register block for voices 0 .. 8 */
386 reg_side
= OPL3_LEFT
;
389 /* Right register block for voices 9 .. 17 */
390 reg_side
= OPL3_RIGHT
;
391 voice_offset
= i
- MAX_OPL2_VOICES
;
393 opl3_reg
= reg_side
| (OPL3_REG_KSL_LEVEL
+ snd_opl3_regmap
[voice_offset
][0]);
394 opl3
->command(opl3
, opl3_reg
, OPL3_TOTAL_LEVEL_MASK
); /* Operator 1 volume */
395 opl3_reg
= reg_side
| (OPL3_REG_KSL_LEVEL
+ snd_opl3_regmap
[voice_offset
][1]);
396 opl3
->command(opl3
, opl3_reg
, OPL3_TOTAL_LEVEL_MASK
); /* Operator 2 volume */
398 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
399 opl3
->command(opl3
, opl3_reg
, 0x00); /* Note off */
402 opl3
->max_voices
= MAX_OPL2_VOICES
;
403 opl3
->fm_mode
= SNDRV_DM_FM_MODE_OPL2
;
405 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_TEST
, OPL3_ENABLE_WAVE_SELECT
);
406 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
, 0x00); /* Melodic mode */
410 EXPORT_SYMBOL(snd_opl3_reset
);
412 static int snd_opl3_play_note(struct snd_opl3
* opl3
, struct snd_dm_fm_note
* note
)
414 unsigned short reg_side
;
415 unsigned char voice_offset
;
417 unsigned short opl3_reg
;
418 unsigned char reg_val
;
420 /* Voices 0 - 8 in OPL2 mode */
421 /* Voices 0 - 17 in OPL3 mode */
422 if (note
->voice
>= ((opl3
->fm_mode
== SNDRV_DM_FM_MODE_OPL3
) ?
423 MAX_OPL3_VOICES
: MAX_OPL2_VOICES
))
426 /* Get register array side and offset of voice */
427 if (note
->voice
< MAX_OPL2_VOICES
) {
428 /* Left register block for voices 0 .. 8 */
429 reg_side
= OPL3_LEFT
;
430 voice_offset
= note
->voice
;
432 /* Right register block for voices 9 .. 17 */
433 reg_side
= OPL3_RIGHT
;
434 voice_offset
= note
->voice
- MAX_OPL2_VOICES
;
437 /* Set lower 8 bits of note frequency */
438 reg_val
= (unsigned char) note
->fnum
;
439 opl3_reg
= reg_side
| (OPL3_REG_FNUM_LOW
+ voice_offset
);
440 opl3
->command(opl3
, opl3_reg
, reg_val
);
443 /* Set output sound flag */
445 reg_val
|= OPL3_KEYON_BIT
;
447 reg_val
|= (note
->octave
<< 2) & OPL3_BLOCKNUM_MASK
;
448 /* Set higher 2 bits of note frequency */
449 reg_val
|= (unsigned char) (note
->fnum
>> 8) & OPL3_FNUM_HIGH_MASK
;
451 /* Set OPL3 KEYON_BLOCK register of requested voice */
452 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
453 opl3
->command(opl3
, opl3_reg
, reg_val
);
459 static int snd_opl3_set_voice(struct snd_opl3
* opl3
, struct snd_dm_fm_voice
* voice
)
461 unsigned short reg_side
;
462 unsigned char op_offset
;
463 unsigned char voice_offset
;
465 unsigned short opl3_reg
;
466 unsigned char reg_val
;
468 /* Only operators 1 and 2 */
471 /* Voices 0 - 8 in OPL2 mode */
472 /* Voices 0 - 17 in OPL3 mode */
473 if (voice
->voice
>= ((opl3
->fm_mode
== SNDRV_DM_FM_MODE_OPL3
) ?
474 MAX_OPL3_VOICES
: MAX_OPL2_VOICES
))
477 /* Get register array side and offset of voice */
478 if (voice
->voice
< MAX_OPL2_VOICES
) {
479 /* Left register block for voices 0 .. 8 */
480 reg_side
= OPL3_LEFT
;
481 voice_offset
= voice
->voice
;
483 /* Right register block for voices 9 .. 17 */
484 reg_side
= OPL3_RIGHT
;
485 voice_offset
= voice
->voice
- MAX_OPL2_VOICES
;
487 /* Get register offset of operator */
488 op_offset
= snd_opl3_regmap
[voice_offset
][voice
->op
];
491 /* Set amplitude modulation (tremolo) effect */
493 reg_val
|= OPL3_TREMOLO_ON
;
494 /* Set vibrato effect */
496 reg_val
|= OPL3_VIBRATO_ON
;
497 /* Set sustaining sound phase */
498 if (voice
->do_sustain
)
499 reg_val
|= OPL3_SUSTAIN_ON
;
500 /* Set keyboard scaling bit */
501 if (voice
->kbd_scale
)
503 /* Set harmonic or frequency multiplier */
504 reg_val
|= voice
->harmonic
& OPL3_MULTIPLE_MASK
;
506 /* Set OPL3 AM_VIB register of requested voice/operator */
507 opl3_reg
= reg_side
| (OPL3_REG_AM_VIB
+ op_offset
);
508 opl3
->command(opl3
, opl3_reg
, reg_val
);
510 /* Set decreasing volume of higher notes */
511 reg_val
= (voice
->scale_level
<< 6) & OPL3_KSL_MASK
;
512 /* Set output volume */
513 reg_val
|= ~voice
->volume
& OPL3_TOTAL_LEVEL_MASK
;
515 /* Set OPL3 KSL_LEVEL register of requested voice/operator */
516 opl3_reg
= reg_side
| (OPL3_REG_KSL_LEVEL
+ op_offset
);
517 opl3
->command(opl3
, opl3_reg
, reg_val
);
519 /* Set attack phase level */
520 reg_val
= (voice
->attack
<< 4) & OPL3_ATTACK_MASK
;
521 /* Set decay phase level */
522 reg_val
|= voice
->decay
& OPL3_DECAY_MASK
;
524 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
525 opl3_reg
= reg_side
| (OPL3_REG_ATTACK_DECAY
+ op_offset
);
526 opl3
->command(opl3
, opl3_reg
, reg_val
);
528 /* Set sustain phase level */
529 reg_val
= (voice
->sustain
<< 4) & OPL3_SUSTAIN_MASK
;
530 /* Set release phase level */
531 reg_val
|= voice
->release
& OPL3_RELEASE_MASK
;
533 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
534 opl3_reg
= reg_side
| (OPL3_REG_SUSTAIN_RELEASE
+ op_offset
);
535 opl3
->command(opl3
, opl3_reg
, reg_val
);
537 /* Set inter-operator feedback */
538 reg_val
= (voice
->feedback
<< 1) & OPL3_FEEDBACK_MASK
;
539 /* Set inter-operator connection */
540 if (voice
->connection
)
541 reg_val
|= OPL3_CONNECTION_BIT
;
543 if (opl3
->fm_mode
== SNDRV_DM_FM_MODE_OPL3
) {
545 reg_val
|= OPL3_VOICE_TO_LEFT
;
547 reg_val
|= OPL3_VOICE_TO_RIGHT
;
549 /* Feedback/connection bits are applicable to voice */
550 opl3_reg
= reg_side
| (OPL3_REG_FEEDBACK_CONNECTION
+ voice_offset
);
551 opl3
->command(opl3
, opl3_reg
, reg_val
);
553 /* Select waveform */
554 reg_val
= voice
->waveform
& OPL3_WAVE_SELECT_MASK
;
555 opl3_reg
= reg_side
| (OPL3_REG_WAVE_SELECT
+ op_offset
);
556 opl3
->command(opl3
, opl3_reg
, reg_val
);
561 static int snd_opl3_set_params(struct snd_opl3
* opl3
, struct snd_dm_fm_params
* params
)
563 unsigned char reg_val
;
566 /* Set keyboard split method */
567 if (params
->kbd_split
)
568 reg_val
|= OPL3_KEYBOARD_SPLIT
;
569 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_KBD_SPLIT
, reg_val
);
572 /* Set amplitude modulation (tremolo) depth */
573 if (params
->am_depth
)
574 reg_val
|= OPL3_TREMOLO_DEPTH
;
575 /* Set vibrato depth */
576 if (params
->vib_depth
)
577 reg_val
|= OPL3_VIBRATO_DEPTH
;
578 /* Set percussion mode */
579 if (params
->rhythm
) {
580 reg_val
|= OPL3_PERCUSSION_ENABLE
;
585 /* Play percussion instruments */
587 reg_val
|= OPL3_BASSDRUM_ON
;
589 reg_val
|= OPL3_SNAREDRUM_ON
;
591 reg_val
|= OPL3_TOMTOM_ON
;
593 reg_val
|= OPL3_CYMBAL_ON
;
595 reg_val
|= OPL3_HIHAT_ON
;
597 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
, reg_val
);
601 static int snd_opl3_set_mode(struct snd_opl3
* opl3
, int mode
)
603 if ((mode
== SNDRV_DM_FM_MODE_OPL3
) && (opl3
->hardware
< OPL3_HW_OPL3
))
606 opl3
->fm_mode
= mode
;
607 if (opl3
->hardware
>= OPL3_HW_OPL3
)
608 opl3
->command(opl3
, OPL3_RIGHT
| OPL3_REG_CONNECTION_SELECT
, 0x00); /* Clear 4-op connections */
613 static int snd_opl3_set_connection(struct snd_opl3
* opl3
, int connection
)
615 unsigned char reg_val
;
618 if (opl3
->fm_mode
!= SNDRV_DM_FM_MODE_OPL3
)
621 reg_val
= connection
& (OPL3_RIGHT_4OP_0
| OPL3_RIGHT_4OP_1
| OPL3_RIGHT_4OP_2
|
622 OPL3_LEFT_4OP_0
| OPL3_LEFT_4OP_1
| OPL3_LEFT_4OP_2
);
623 /* Set 4-op connections */
624 opl3
->command(opl3
, OPL3_RIGHT
| OPL3_REG_CONNECTION_SELECT
, reg_val
);