| 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) || defined TARGET_OS2)
|
|---|
| 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 |
|
|---|
| 38 | char 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 |
|
|---|
| 55 | EXPORT_SYMBOL(snd_opl3_regmap);
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | * prototypes
|
|---|
| 59 | */
|
|---|
| 60 | static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note);
|
|---|
| 61 | static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice);
|
|---|
| 62 | static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params);
|
|---|
| 63 | static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode);
|
|---|
| 64 | static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection);
|
|---|
| 65 |
|
|---|
| 66 | /* ------------------------------ */
|
|---|
| 67 |
|
|---|
| 68 | /*
|
|---|
| 69 | * open the device exclusively
|
|---|
| 70 | */
|
|---|
| 71 | int snd_opl3_open(struct snd_hwdep * hw, struct file *file)
|
|---|
| 72 | {
|
|---|
| 73 | return 0;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /*
|
|---|
| 77 | * ioctl for hwdep device:
|
|---|
| 78 | */
|
|---|
| 79 | int 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(¬e, argp, sizeof(struct snd_dm_fm_note)))
|
|---|
| 117 | return -EFAULT;
|
|---|
| 118 | return snd_opl3_play_note(opl3, ¬e);
|
|---|
| 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(¶ms, argp, sizeof(struct snd_dm_fm_params)))
|
|---|
| 139 | return -EFAULT;
|
|---|
| 140 | return snd_opl3_set_params(opl3, ¶ms);
|
|---|
| 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 | default:
|
|---|
| 162 | dev_dbg(opl3->card->dev, "unknown IOCTL: 0x%x\n", cmd);
|
|---|
| 163 | }
|
|---|
| 164 | return -ENOTTY;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /*
|
|---|
| 168 | * close the device
|
|---|
| 169 | */
|
|---|
| 170 | int snd_opl3_release(struct snd_hwdep * hw, struct file *file)
|
|---|
| 171 | {
|
|---|
| 172 | struct snd_opl3 *opl3 = hw->private_data;
|
|---|
| 173 |
|
|---|
| 174 | snd_opl3_reset(opl3);
|
|---|
| 175 | return 0;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | #ifdef OPL3_SUPPORT_SYNTH
|
|---|
| 179 | /*
|
|---|
| 180 | * write the device - load patches
|
|---|
| 181 | */
|
|---|
| 182 | long snd_opl3_write(struct snd_hwdep *hw, const char __user *buf, long count,
|
|---|
| 183 | loff_t *offset)
|
|---|
| 184 | {
|
|---|
| 185 | struct snd_opl3 *opl3 = hw->private_data;
|
|---|
| 186 | long result = 0;
|
|---|
| 187 | int err = 0;
|
|---|
| 188 | struct sbi_patch inst;
|
|---|
| 189 |
|
|---|
| 190 | while (count >= sizeof(inst)) {
|
|---|
| 191 | unsigned char type;
|
|---|
| 192 | if (copy_from_user(&inst, buf, sizeof(inst)))
|
|---|
| 193 | return -EFAULT;
|
|---|
| 194 | if (!memcmp(inst.key, FM_KEY_SBI, 4) ||
|
|---|
| 195 | !memcmp(inst.key, FM_KEY_2OP, 4))
|
|---|
| 196 | type = FM_PATCH_OPL2;
|
|---|
| 197 | else if (!memcmp(inst.key, FM_KEY_4OP, 4))
|
|---|
| 198 | type = FM_PATCH_OPL3;
|
|---|
| 199 | else /* invalid type */
|
|---|
| 200 | break;
|
|---|
| 201 | err = snd_opl3_load_patch(opl3, inst.prog, inst.bank, type,
|
|---|
| 202 | inst.name, inst.extension,
|
|---|
| 203 | inst.data);
|
|---|
| 204 | if (err < 0)
|
|---|
| 205 | break;
|
|---|
| 206 | result += sizeof(inst);
|
|---|
| 207 | count -= sizeof(inst);
|
|---|
| 208 | }
|
|---|
| 209 | return result > 0 ? result : err;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 | /*
|
|---|
| 214 | * Patch management
|
|---|
| 215 | */
|
|---|
| 216 |
|
|---|
| 217 | /* offsets for SBI params */
|
|---|
| 218 | #define AM_VIB 0
|
|---|
| 219 | #define KSL_LEVEL 2
|
|---|
| 220 | #define ATTACK_DECAY 4
|
|---|
| 221 | #define SUSTAIN_RELEASE 6
|
|---|
| 222 | #define WAVE_SELECT 8
|
|---|
| 223 |
|
|---|
| 224 | /* offset for SBI instrument */
|
|---|
| 225 | #define CONNECTION 10
|
|---|
| 226 | #define OFFSET_4OP 11
|
|---|
| 227 |
|
|---|
| 228 | /*
|
|---|
| 229 | * load a patch, obviously.
|
|---|
| 230 | *
|
|---|
| 231 | * loaded on the given program and bank numbers with the given type
|
|---|
| 232 | * (FM_PATCH_OPLx).
|
|---|
| 233 | * data is the pointer of SBI record _without_ header (key and name).
|
|---|
| 234 | * name is the name string of the patch.
|
|---|
| 235 | * ext is the extension data of 7 bytes long (stored in name of SBI
|
|---|
| 236 | * data up to offset 25), or NULL to skip.
|
|---|
| 237 | * return 0 if successful or a negative error code.
|
|---|
| 238 | */
|
|---|
| 239 | int snd_opl3_load_patch(struct snd_opl3 *opl3,
|
|---|
| 240 | int prog, int bank, int type,
|
|---|
| 241 | const char *name,
|
|---|
| 242 | const unsigned char *ext,
|
|---|
| 243 | const unsigned char *data)
|
|---|
| 244 | {
|
|---|
| 245 | struct fm_patch *patch;
|
|---|
| 246 | int i;
|
|---|
| 247 |
|
|---|
| 248 | patch = snd_opl3_find_patch(opl3, prog, bank, 1);
|
|---|
| 249 | if (!patch)
|
|---|
| 250 | return -ENOMEM;
|
|---|
| 251 |
|
|---|
| 252 | patch->type = type;
|
|---|
| 253 |
|
|---|
| 254 | for (i = 0; i < 2; i++) {
|
|---|
| 255 | patch->inst.op[i].am_vib = data[AM_VIB + i];
|
|---|
| 256 | patch->inst.op[i].ksl_level = data[KSL_LEVEL + i];
|
|---|
| 257 | patch->inst.op[i].attack_decay = data[ATTACK_DECAY + i];
|
|---|
| 258 | patch->inst.op[i].sustain_release = data[SUSTAIN_RELEASE + i];
|
|---|
| 259 | patch->inst.op[i].wave_select = data[WAVE_SELECT + i];
|
|---|
| 260 | }
|
|---|
| 261 | patch->inst.feedback_connection[0] = data[CONNECTION];
|
|---|
| 262 |
|
|---|
| 263 | if (type == FM_PATCH_OPL3) {
|
|---|
| 264 | for (i = 0; i < 2; i++) {
|
|---|
| 265 | patch->inst.op[i+2].am_vib =
|
|---|
| 266 | data[OFFSET_4OP + AM_VIB + i];
|
|---|
| 267 | patch->inst.op[i+2].ksl_level =
|
|---|
| 268 | data[OFFSET_4OP + KSL_LEVEL + i];
|
|---|
| 269 | patch->inst.op[i+2].attack_decay =
|
|---|
| 270 | data[OFFSET_4OP + ATTACK_DECAY + i];
|
|---|
| 271 | patch->inst.op[i+2].sustain_release =
|
|---|
| 272 | data[OFFSET_4OP + SUSTAIN_RELEASE + i];
|
|---|
| 273 | patch->inst.op[i+2].wave_select =
|
|---|
| 274 | data[OFFSET_4OP + WAVE_SELECT + i];
|
|---|
| 275 | }
|
|---|
| 276 | patch->inst.feedback_connection[1] =
|
|---|
| 277 | data[OFFSET_4OP + CONNECTION];
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | if (ext) {
|
|---|
| 281 | patch->inst.echo_delay = ext[0];
|
|---|
| 282 | patch->inst.echo_atten = ext[1];
|
|---|
| 283 | patch->inst.chorus_spread = ext[2];
|
|---|
| 284 | patch->inst.trnsps = ext[3];
|
|---|
| 285 | patch->inst.fix_dur = ext[4];
|
|---|
| 286 | patch->inst.modes = ext[5];
|
|---|
| 287 | patch->inst.fix_key = ext[6];
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | if (name)
|
|---|
| 291 | strscpy(patch->name, name, sizeof(patch->name));
|
|---|
| 292 |
|
|---|
| 293 | return 0;
|
|---|
| 294 | }
|
|---|
| 295 | EXPORT_SYMBOL(snd_opl3_load_patch);
|
|---|
| 296 |
|
|---|
| 297 | /*
|
|---|
| 298 | * find a patch with the given program and bank numbers, returns its pointer
|
|---|
| 299 | * if no matching patch is found and create_patch is set, it creates a
|
|---|
| 300 | * new patch object.
|
|---|
| 301 | */
|
|---|
| 302 |
|
|---|
| 303 | struct fm_patch *snd_opl3_find_patch(struct snd_opl3 *opl3, int prog, int bank,
|
|---|
| 304 | int create_patch)
|
|---|
| 305 | {
|
|---|
| 306 | /* pretty dumb hash key */
|
|---|
| 307 | unsigned int key = (prog + bank) % OPL3_PATCH_HASH_SIZE;
|
|---|
| 308 | struct fm_patch *patch;
|
|---|
| 309 |
|
|---|
| 310 | for (patch = opl3->patch_table[key]; patch; patch = patch->next) {
|
|---|
| 311 | if (patch->prog == prog && patch->bank == bank)
|
|---|
| 312 | return patch;
|
|---|
| 313 | }
|
|---|
| 314 | if (!create_patch)
|
|---|
| 315 | return NULL;
|
|---|
| 316 |
|
|---|
| 317 | patch = kzalloc(sizeof(*patch), GFP_KERNEL);
|
|---|
| 318 | if (!patch)
|
|---|
| 319 | return NULL;
|
|---|
| 320 | patch->prog = prog;
|
|---|
| 321 | patch->bank = bank;
|
|---|
| 322 | patch->next = opl3->patch_table[key];
|
|---|
| 323 | opl3->patch_table[key] = patch;
|
|---|
| 324 | return patch;
|
|---|
| 325 | }
|
|---|
| 326 | EXPORT_SYMBOL(snd_opl3_find_patch);
|
|---|
| 327 |
|
|---|
| 328 | /*
|
|---|
| 329 | * Clear all patches of the given OPL3 instance
|
|---|
| 330 | */
|
|---|
| 331 | void snd_opl3_clear_patches(struct snd_opl3 *opl3)
|
|---|
| 332 | {
|
|---|
| 333 | int i;
|
|---|
| 334 | for (i = 0; i < OPL3_PATCH_HASH_SIZE; i++) {
|
|---|
| 335 | struct fm_patch *patch, *next;
|
|---|
| 336 | for (patch = opl3->patch_table[i]; patch; patch = next) {
|
|---|
| 337 | next = patch->next;
|
|---|
| 338 | kfree(patch);
|
|---|
| 339 | }
|
|---|
| 340 | }
|
|---|
| 341 | memset(opl3->patch_table, 0, sizeof(opl3->patch_table));
|
|---|
| 342 | }
|
|---|
| 343 | #endif /* OPL3_SUPPORT_SYNTH */
|
|---|
| 344 |
|
|---|
| 345 | /* ------------------------------ */
|
|---|
| 346 |
|
|---|
| 347 | void snd_opl3_reset(struct snd_opl3 * opl3)
|
|---|
| 348 | {
|
|---|
| 349 | unsigned short opl3_reg;
|
|---|
| 350 |
|
|---|
| 351 | unsigned short reg_side;
|
|---|
| 352 | unsigned char voice_offset;
|
|---|
| 353 |
|
|---|
| 354 | int max_voices, i;
|
|---|
| 355 |
|
|---|
| 356 | max_voices = (opl3->hardware < OPL3_HW_OPL3) ?
|
|---|
| 357 | MAX_OPL2_VOICES : MAX_OPL3_VOICES;
|
|---|
| 358 |
|
|---|
| 359 | for (i = 0; i < max_voices; i++) {
|
|---|
| 360 | /* Get register array side and offset of voice */
|
|---|
| 361 | if (i < MAX_OPL2_VOICES) {
|
|---|
| 362 | /* Left register block for voices 0 .. 8 */
|
|---|
| 363 | reg_side = OPL3_LEFT;
|
|---|
| 364 | voice_offset = i;
|
|---|
| 365 | } else {
|
|---|
| 366 | /* Right register block for voices 9 .. 17 */
|
|---|
| 367 | reg_side = OPL3_RIGHT;
|
|---|
| 368 | voice_offset = i - MAX_OPL2_VOICES;
|
|---|
| 369 | }
|
|---|
| 370 | opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][0]);
|
|---|
| 371 | opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 1 volume */
|
|---|
| 372 | opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][1]);
|
|---|
| 373 | opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 2 volume */
|
|---|
| 374 |
|
|---|
| 375 | opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
|
|---|
| 376 | opl3->command(opl3, opl3_reg, 0x00); /* Note off */
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | opl3->max_voices = MAX_OPL2_VOICES;
|
|---|
| 380 | opl3->fm_mode = SNDRV_DM_FM_MODE_OPL2;
|
|---|
| 381 |
|
|---|
| 382 | opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
|
|---|
| 383 | opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00); /* Melodic mode */
|
|---|
| 384 | opl3->rhythm = 0;
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | EXPORT_SYMBOL(snd_opl3_reset);
|
|---|
| 388 |
|
|---|
| 389 | static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note)
|
|---|
| 390 | {
|
|---|
| 391 | unsigned short reg_side;
|
|---|
| 392 | unsigned char voice_offset;
|
|---|
| 393 |
|
|---|
| 394 | unsigned short opl3_reg;
|
|---|
| 395 | unsigned char reg_val;
|
|---|
| 396 |
|
|---|
| 397 | /* Voices 0 - 8 in OPL2 mode */
|
|---|
| 398 | /* Voices 0 - 17 in OPL3 mode */
|
|---|
| 399 | if (note->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
|
|---|
| 400 | MAX_OPL3_VOICES : MAX_OPL2_VOICES))
|
|---|
| 401 | return -EINVAL;
|
|---|
| 402 |
|
|---|
| 403 | /* Get register array side and offset of voice */
|
|---|
| 404 | if (note->voice < MAX_OPL2_VOICES) {
|
|---|
| 405 | /* Left register block for voices 0 .. 8 */
|
|---|
| 406 | reg_side = OPL3_LEFT;
|
|---|
| 407 | voice_offset = note->voice;
|
|---|
| 408 | } else {
|
|---|
| 409 | /* Right register block for voices 9 .. 17 */
|
|---|
| 410 | reg_side = OPL3_RIGHT;
|
|---|
| 411 | voice_offset = note->voice - MAX_OPL2_VOICES;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | /* Set lower 8 bits of note frequency */
|
|---|
| 415 | reg_val = (unsigned char) note->fnum;
|
|---|
| 416 | opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
|
|---|
| 417 | opl3->command(opl3, opl3_reg, reg_val);
|
|---|
| 418 |
|
|---|
| 419 | reg_val = 0x00;
|
|---|
| 420 | /* Set output sound flag */
|
|---|
| 421 | if (note->key_on)
|
|---|
| 422 | reg_val |= OPL3_KEYON_BIT;
|
|---|
| 423 | /* Set octave */
|
|---|
| 424 | reg_val |= (note->octave << 2) & OPL3_BLOCKNUM_MASK;
|
|---|
| 425 | /* Set higher 2 bits of note frequency */
|
|---|
| 426 | reg_val |= (unsigned char) (note->fnum >> 8) & OPL3_FNUM_HIGH_MASK;
|
|---|
| 427 |
|
|---|
| 428 | /* Set OPL3 KEYON_BLOCK register of requested voice */
|
|---|
| 429 | opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
|
|---|
| 430 | opl3->command(opl3, opl3_reg, reg_val);
|
|---|
| 431 |
|
|---|
| 432 | return 0;
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 | static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice)
|
|---|
| 437 | {
|
|---|
| 438 | unsigned short reg_side;
|
|---|
| 439 | unsigned char op_offset;
|
|---|
| 440 | unsigned char voice_offset, voice_op;
|
|---|
| 441 |
|
|---|
| 442 | unsigned short opl3_reg;
|
|---|
| 443 | unsigned char reg_val;
|
|---|
| 444 |
|
|---|
| 445 | /* Only operators 1 and 2 */
|
|---|
| 446 | if (voice->op > 1)
|
|---|
| 447 | return -EINVAL;
|
|---|
| 448 | /* Voices 0 - 8 in OPL2 mode */
|
|---|
| 449 | /* Voices 0 - 17 in OPL3 mode */
|
|---|
| 450 | if (voice->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
|
|---|
| 451 | MAX_OPL3_VOICES : MAX_OPL2_VOICES))
|
|---|
| 452 | return -EINVAL;
|
|---|
| 453 |
|
|---|
| 454 | /* Get register array side and offset of voice */
|
|---|
| 455 | if (voice->voice < MAX_OPL2_VOICES) {
|
|---|
| 456 | /* Left register block for voices 0 .. 8 */
|
|---|
| 457 | reg_side = OPL3_LEFT;
|
|---|
| 458 | voice_offset = voice->voice;
|
|---|
| 459 | } else {
|
|---|
| 460 | /* Right register block for voices 9 .. 17 */
|
|---|
| 461 | reg_side = OPL3_RIGHT;
|
|---|
| 462 | voice_offset = voice->voice - MAX_OPL2_VOICES;
|
|---|
| 463 | }
|
|---|
| 464 | /* Get register offset of operator */
|
|---|
| 465 | voice_offset = array_index_nospec(voice_offset, MAX_OPL2_VOICES);
|
|---|
| 466 | voice_op = array_index_nospec(voice->op, 4);
|
|---|
| 467 | op_offset = snd_opl3_regmap[voice_offset][voice_op];
|
|---|
| 468 |
|
|---|
| 469 | reg_val = 0x00;
|
|---|
| 470 | /* Set amplitude modulation (tremolo) effect */
|
|---|
| 471 | if (voice->am)
|
|---|
| 472 | reg_val |= OPL3_TREMOLO_ON;
|
|---|
| 473 | /* Set vibrato effect */
|
|---|
| 474 | if (voice->vibrato)
|
|---|
| 475 | reg_val |= OPL3_VIBRATO_ON;
|
|---|
| 476 | /* Set sustaining sound phase */
|
|---|
| 477 | if (voice->do_sustain)
|
|---|
| 478 | reg_val |= OPL3_SUSTAIN_ON;
|
|---|
| 479 | /* Set keyboard scaling bit */
|
|---|
| 480 | if (voice->kbd_scale)
|
|---|
| 481 | reg_val |= OPL3_KSR;
|
|---|
| 482 | /* Set harmonic or frequency multiplier */
|
|---|
| 483 | reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
|
|---|
| 484 |
|
|---|
| 485 | /* Set OPL3 AM_VIB register of requested voice/operator */
|
|---|
| 486 | opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
|
|---|
| 487 | opl3->command(opl3, opl3_reg, reg_val);
|
|---|
| 488 |
|
|---|
| 489 | /* Set decreasing volume of higher notes */
|
|---|
| 490 | reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
|
|---|
| 491 | /* Set output volume */
|
|---|
| 492 | reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
|
|---|
| 493 |
|
|---|
| 494 | /* Set OPL3 KSL_LEVEL register of requested voice/operator */
|
|---|
| 495 | opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
|
|---|
| 496 | opl3->command(opl3, opl3_reg, reg_val);
|
|---|
| 497 |
|
|---|
| 498 | /* Set attack phase level */
|
|---|
| 499 | reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
|
|---|
| 500 | /* Set decay phase level */
|
|---|
| 501 | reg_val |= voice->decay & OPL3_DECAY_MASK;
|
|---|
| 502 |
|
|---|
| 503 | /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
|
|---|
| 504 | opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
|
|---|
| 505 | opl3->command(opl3, opl3_reg, reg_val);
|
|---|
| 506 |
|
|---|
| 507 | /* Set sustain phase level */
|
|---|
| 508 | reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
|
|---|
| 509 | /* Set release phase level */
|
|---|
| 510 | reg_val |= voice->release & OPL3_RELEASE_MASK;
|
|---|
| 511 |
|
|---|
| 512 | /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
|
|---|
| 513 | opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
|
|---|
| 514 | opl3->command(opl3, opl3_reg, reg_val);
|
|---|
| 515 |
|
|---|
| 516 | /* Set inter-operator feedback */
|
|---|
| 517 | reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
|
|---|
| 518 | /* Set inter-operator connection */
|
|---|
| 519 | if (voice->connection)
|
|---|
| 520 | reg_val |= OPL3_CONNECTION_BIT;
|
|---|
| 521 | /* OPL-3 only */
|
|---|
| 522 | if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
|
|---|
| 523 | if (voice->left)
|
|---|
| 524 | reg_val |= OPL3_VOICE_TO_LEFT;
|
|---|
| 525 | if (voice->right)
|
|---|
| 526 | reg_val |= OPL3_VOICE_TO_RIGHT;
|
|---|
| 527 | }
|
|---|
| 528 | /* Feedback/connection bits are applicable to voice */
|
|---|
| 529 | opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
|
|---|
| 530 | opl3->command(opl3, opl3_reg, reg_val);
|
|---|
| 531 |
|
|---|
| 532 | /* Select waveform */
|
|---|
| 533 | reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
|
|---|
| 534 | opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
|
|---|
| 535 | opl3->command(opl3, opl3_reg, reg_val);
|
|---|
| 536 |
|
|---|
| 537 | return 0;
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params)
|
|---|
| 541 | {
|
|---|
| 542 | unsigned char reg_val;
|
|---|
| 543 |
|
|---|
| 544 | reg_val = 0x00;
|
|---|
| 545 | /* Set keyboard split method */
|
|---|
| 546 | if (params->kbd_split)
|
|---|
| 547 | reg_val |= OPL3_KEYBOARD_SPLIT;
|
|---|
| 548 | opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
|
|---|
| 549 |
|
|---|
| 550 | reg_val = 0x00;
|
|---|
| 551 | /* Set amplitude modulation (tremolo) depth */
|
|---|
| 552 | if (params->am_depth)
|
|---|
| 553 | reg_val |= OPL3_TREMOLO_DEPTH;
|
|---|
| 554 | /* Set vibrato depth */
|
|---|
| 555 | if (params->vib_depth)
|
|---|
| 556 | reg_val |= OPL3_VIBRATO_DEPTH;
|
|---|
| 557 | /* Set percussion mode */
|
|---|
| 558 | if (params->rhythm) {
|
|---|
| 559 | reg_val |= OPL3_PERCUSSION_ENABLE;
|
|---|
| 560 | opl3->rhythm = 1;
|
|---|
| 561 | } else {
|
|---|
| 562 | opl3->rhythm = 0;
|
|---|
| 563 | }
|
|---|
| 564 | /* Play percussion instruments */
|
|---|
| 565 | if (params->bass)
|
|---|
| 566 | reg_val |= OPL3_BASSDRUM_ON;
|
|---|
| 567 | if (params->snare)
|
|---|
| 568 | reg_val |= OPL3_SNAREDRUM_ON;
|
|---|
| 569 | if (params->tomtom)
|
|---|
| 570 | reg_val |= OPL3_TOMTOM_ON;
|
|---|
| 571 | if (params->cymbal)
|
|---|
| 572 | reg_val |= OPL3_CYMBAL_ON;
|
|---|
| 573 | if (params->hihat)
|
|---|
| 574 | reg_val |= OPL3_HIHAT_ON;
|
|---|
| 575 |
|
|---|
| 576 | opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
|
|---|
| 577 | return 0;
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode)
|
|---|
| 581 | {
|
|---|
| 582 | if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
|
|---|
| 583 | return -EINVAL;
|
|---|
| 584 |
|
|---|
| 585 | opl3->fm_mode = mode;
|
|---|
| 586 | if (opl3->hardware >= OPL3_HW_OPL3)
|
|---|
| 587 | opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00); /* Clear 4-op connections */
|
|---|
| 588 |
|
|---|
| 589 | return 0;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection)
|
|---|
| 593 | {
|
|---|
| 594 | unsigned char reg_val;
|
|---|
| 595 |
|
|---|
| 596 | /* OPL-3 only */
|
|---|
| 597 | if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
|
|---|
| 598 | return -EINVAL;
|
|---|
| 599 |
|
|---|
| 600 | reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
|
|---|
| 601 | OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
|
|---|
| 602 | /* Set 4-op connections */
|
|---|
| 603 | opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
|
|---|
| 604 |
|
|---|
| 605 | return 0;
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|