blob: d55eefce44c173e9a3639522b34fa8038730c82e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 <sound/opl3.h>
23#include <sound/asound_fm.h>
24
25/*
26 * There is 18 possible 2 OP voices
27 * (9 in the left and 9 in the right).
28 * The first OP is the modulator and 2nd is the carrier.
29 *
30 * The first three voices in the both sides may be connected
31 * with another voice to a 4 OP voice. For example voice 0
32 * can be connected with voice 3. The operators of voice 3 are
33 * used as operators 3 and 4 of the new 4 OP voice.
34 * In this case the 2 OP voice number 0 is the 'first half' and
35 * voice 3 is the second.
36 */
37
38
39/*
40 * Register offset table for OPL2/3 voices,
41 * OPL2 / one OPL3 register array side only
42 */
43
44char snd_opl3_regmap[MAX_OPL2_VOICES][4] =
45{
46/* OP1 OP2 OP3 OP4 */
47/* ------------------------ */
48 { 0x00, 0x03, 0x08, 0x0b },
49 { 0x01, 0x04, 0x09, 0x0c },
50 { 0x02, 0x05, 0x0a, 0x0d },
51
52 { 0x08, 0x0b, 0x00, 0x00 },
53 { 0x09, 0x0c, 0x00, 0x00 },
54 { 0x0a, 0x0d, 0x00, 0x00 },
55
56 { 0x10, 0x13, 0x00, 0x00 }, /* used by percussive voices */
57 { 0x11, 0x14, 0x00, 0x00 }, /* if the percussive mode */
58 { 0x12, 0x15, 0x00, 0x00 } /* is selected (only left reg block) */
59};
60
Takashi Iwaiac19e192006-04-28 15:13:39 +020061EXPORT_SYMBOL(snd_opl3_regmap);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * prototypes
65 */
Takashi Iwai5b1646a2005-11-17 14:13:14 +010066static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note);
67static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice);
68static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params);
69static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode);
70static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* ------------------------------ */
73
74/*
75 * open the device exclusively
76 */
Takashi Iwai5b1646a2005-11-17 14:13:14 +010077int snd_opl3_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Takashi Iwai5b1646a2005-11-17 14:13:14 +010079 struct snd_opl3 *opl3 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Ingo Molnaref9f0a42006-01-16 16:31:42 +010081 mutex_lock(&opl3->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (opl3->used) {
Ingo Molnaref9f0a42006-01-16 16:31:42 +010083 mutex_unlock(&opl3->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return -EAGAIN;
85 }
86 opl3->used++;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010087 mutex_unlock(&opl3->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return 0;
90}
91
92/*
93 * ioctl for hwdep device:
94 */
Takashi Iwai5b1646a2005-11-17 14:13:14 +010095int snd_opl3_ioctl(struct snd_hwdep * hw, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned int cmd, unsigned long arg)
97{
Takashi Iwai5b1646a2005-11-17 14:13:14 +010098 struct snd_opl3 *opl3 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 void __user *argp = (void __user *)arg;
100
101 snd_assert(opl3 != NULL, return -EINVAL);
102
103 switch (cmd) {
104 /* get information */
105 case SNDRV_DM_FM_IOCTL_INFO:
106 {
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100107 struct snd_dm_fm_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 info.fm_mode = opl3->fm_mode;
110 info.rhythm = opl3->rhythm;
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100111 if (copy_to_user(argp, &info, sizeof(struct snd_dm_fm_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 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 {
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100128 struct snd_dm_fm_note note;
129 if (copy_from_user(&note, argp, sizeof(struct snd_dm_fm_note)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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 {
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100139 struct snd_dm_fm_voice voice;
140 if (copy_from_user(&voice, argp, sizeof(struct snd_dm_fm_voice)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 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 {
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100150 struct snd_dm_fm_params params;
151 if (copy_from_user(&params, argp, sizeof(struct snd_dm_fm_params)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 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
Takashi Iwai224a0332007-10-30 11:49:22 +0100168 case SNDRV_DM_FM_IOCTL_CLEAR_PATCHES:
169 snd_opl3_clear_patches(opl3);
170 return 0;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#ifdef CONFIG_SND_DEBUG
173 default:
174 snd_printk("unknown IOCTL: 0x%x\n", cmd);
175#endif
176 }
177 return -ENOTTY;
178}
179
180/*
181 * close the device
182 */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100183int snd_opl3_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100185 struct snd_opl3 *opl3 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 snd_opl3_reset(opl3);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100188 mutex_lock(&opl3->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 opl3->used--;
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100190 mutex_unlock(&opl3->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return 0;
193}
194
Takashi Iwai224a0332007-10-30 11:49:22 +0100195/*
196 * write the device - load patches
197 */
198long snd_opl3_write(struct snd_hwdep *hw, const char __user *buf, long count,
199 loff_t *offset)
200{
201 struct snd_opl3 *opl3 = hw->private_data;
202 long result = 0;
203 int err = 0;
204 struct sbi_patch inst;
205
206 while (count >= sizeof(inst)) {
207 unsigned char type;
208 if (copy_from_user(&inst, buf, sizeof(inst)))
209 return -EFAULT;
210 if (!memcmp(inst.key, FM_KEY_SBI, 4) ||
211 !memcmp(inst.key, FM_KEY_2OP, 4))
212 type = FM_PATCH_OPL2;
213 else if (!memcmp(inst.key, FM_KEY_4OP, 4))
214 type = FM_PATCH_OPL3;
215 else /* invalid type */
216 break;
217 err = snd_opl3_load_patch(opl3, inst.prog, inst.bank, type,
218 inst.name, inst.extension,
219 inst.data);
220 if (err < 0)
221 break;
222 result += sizeof(inst);
223 count -= sizeof(inst);
224 }
225 return result > 0 ? result : err;
226}
227
228
229/*
230 * Patch management
231 */
232
233/* offsets for SBI params */
234#define AM_VIB 0
235#define KSL_LEVEL 2
236#define ATTACK_DECAY 4
237#define SUSTAIN_RELEASE 6
238#define WAVE_SELECT 8
239
240/* offset for SBI instrument */
241#define CONNECTION 10
242#define OFFSET_4OP 11
243
244/*
245 * load a patch, obviously.
246 *
247 * loaded on the given program and bank numbers with the given type
248 * (FM_PATCH_OPLx).
249 * data is the pointer of SBI record _without_ header (key and name).
250 * name is the name string of the patch.
251 * ext is the extension data of 7 bytes long (stored in name of SBI
252 * data up to offset 25), or NULL to skip.
253 * return 0 if successful or a negative error code.
254 */
255int snd_opl3_load_patch(struct snd_opl3 *opl3,
256 int prog, int bank, int type,
257 const char *name,
258 const unsigned char *ext,
259 const unsigned char *data)
260{
261 struct fm_patch *patch;
262 int i;
263
264 patch = snd_opl3_find_patch(opl3, prog, bank, 1);
265 if (!patch)
266 return -ENOMEM;
267
268 patch->type = type;
269
270 for (i = 0; i < 2; i++) {
271 patch->inst.op[i].am_vib = data[AM_VIB + i];
272 patch->inst.op[i].ksl_level = data[KSL_LEVEL + i];
273 patch->inst.op[i].attack_decay = data[ATTACK_DECAY + i];
274 patch->inst.op[i].sustain_release = data[SUSTAIN_RELEASE + i];
275 patch->inst.op[i].wave_select = data[WAVE_SELECT + i];
276 }
277 patch->inst.feedback_connection[0] = data[CONNECTION];
278
279 if (type == FM_PATCH_OPL3) {
280 for (i = 0; i < 2; i++) {
281 patch->inst.op[i+2].am_vib =
282 data[OFFSET_4OP + AM_VIB + i];
283 patch->inst.op[i+2].ksl_level =
284 data[OFFSET_4OP + KSL_LEVEL + i];
285 patch->inst.op[i+2].attack_decay =
286 data[OFFSET_4OP + ATTACK_DECAY + i];
287 patch->inst.op[i+2].sustain_release =
288 data[OFFSET_4OP + SUSTAIN_RELEASE + i];
289 patch->inst.op[i+2].wave_select =
290 data[OFFSET_4OP + WAVE_SELECT + i];
291 }
292 patch->inst.feedback_connection[1] =
293 data[OFFSET_4OP + CONNECTION];
294 }
295
296 if (ext) {
297 patch->inst.echo_delay = ext[0];
298 patch->inst.echo_atten = ext[1];
299 patch->inst.chorus_spread = ext[2];
300 patch->inst.trnsps = ext[3];
301 patch->inst.fix_dur = ext[4];
302 patch->inst.modes = ext[5];
303 patch->inst.fix_key = ext[6];
304 }
305
306 if (name)
307 strlcpy(patch->name, name, sizeof(patch->name));
308
309 return 0;
310}
311EXPORT_SYMBOL(snd_opl3_load_patch);
312
313/*
314 * find a patch with the given program and bank numbers, returns its pointer
315 * if no matching patch is found and create_patch is set, it creates a
316 * new patch object.
317 */
318struct fm_patch *snd_opl3_find_patch(struct snd_opl3 *opl3, int prog, int bank,
319 int create_patch)
320{
321 /* pretty dumb hash key */
322 unsigned int key = (prog + bank) % OPL3_PATCH_HASH_SIZE;
323 struct fm_patch *patch;
324
325 for (patch = opl3->patch_table[key]; patch; patch = patch->next) {
326 if (patch->prog == prog && patch->bank == bank)
327 return patch;
328 }
329 if (!create_patch)
330 return NULL;
331
332 patch = kzalloc(sizeof(*patch), GFP_KERNEL);
333 if (!patch)
334 return NULL;
335 patch->prog = prog;
336 patch->bank = bank;
337 patch->next = opl3->patch_table[key];
338 opl3->patch_table[key] = patch;
339 return patch;
340}
341EXPORT_SYMBOL(snd_opl3_find_patch);
342
343/*
344 * Clear all patches of the given OPL3 instance
345 */
346void snd_opl3_clear_patches(struct snd_opl3 *opl3)
347{
348 int i;
349 for (i = 0; i < OPL3_PATCH_HASH_SIZE; i++) {
350 struct fm_patch *patch, *next;
351 for (patch = opl3->patch_table[i]; patch; patch = next) {
352 next = patch->next;
353 kfree(patch);
354 }
355 }
356 memset(opl3->patch_table, 0, sizeof(opl3->patch_table));
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/* ------------------------------ */
360
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100361void snd_opl3_reset(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
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
Takashi Iwaiac19e192006-04-28 15:13:39 +0200401EXPORT_SYMBOL(snd_opl3_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100403static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
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
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100450static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 unsigned short reg_side;
453 unsigned char op_offset;
454 unsigned char voice_offset;
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 op_offset = snd_opl3_regmap[voice_offset][voice->op];
480
481 reg_val = 0x00;
482 /* Set amplitude modulation (tremolo) effect */
483 if (voice->am)
484 reg_val |= OPL3_TREMOLO_ON;
485 /* Set vibrato effect */
486 if (voice->vibrato)
487 reg_val |= OPL3_VIBRATO_ON;
488 /* Set sustaining sound phase */
489 if (voice->do_sustain)
490 reg_val |= OPL3_SUSTAIN_ON;
491 /* Set keyboard scaling bit */
492 if (voice->kbd_scale)
493 reg_val |= OPL3_KSR;
494 /* Set harmonic or frequency multiplier */
495 reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
496
497 /* Set OPL3 AM_VIB register of requested voice/operator */
498 opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
499 opl3->command(opl3, opl3_reg, reg_val);
500
501 /* Set decreasing volume of higher notes */
502 reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
503 /* Set output volume */
504 reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
505
506 /* Set OPL3 KSL_LEVEL register of requested voice/operator */
507 opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
508 opl3->command(opl3, opl3_reg, reg_val);
509
510 /* Set attack phase level */
511 reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
512 /* Set decay phase level */
513 reg_val |= voice->decay & OPL3_DECAY_MASK;
514
515 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
516 opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
517 opl3->command(opl3, opl3_reg, reg_val);
518
519 /* Set sustain phase level */
520 reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
521 /* Set release phase level */
522 reg_val |= voice->release & OPL3_RELEASE_MASK;
523
524 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
525 opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
526 opl3->command(opl3, opl3_reg, reg_val);
527
528 /* Set inter-operator feedback */
529 reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
530 /* Set inter-operator connection */
531 if (voice->connection)
532 reg_val |= OPL3_CONNECTION_BIT;
533 /* OPL-3 only */
534 if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
535 if (voice->left)
536 reg_val |= OPL3_VOICE_TO_LEFT;
537 if (voice->right)
538 reg_val |= OPL3_VOICE_TO_RIGHT;
539 }
540 /* Feedback/connection bits are applicable to voice */
541 opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
542 opl3->command(opl3, opl3_reg, reg_val);
543
544 /* Select waveform */
545 reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
546 opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
547 opl3->command(opl3, opl3_reg, reg_val);
548
549 return 0;
550}
551
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100552static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 unsigned char reg_val;
555
556 reg_val = 0x00;
557 /* Set keyboard split method */
558 if (params->kbd_split)
559 reg_val |= OPL3_KEYBOARD_SPLIT;
560 opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
561
562 reg_val = 0x00;
563 /* Set amplitude modulation (tremolo) depth */
564 if (params->am_depth)
565 reg_val |= OPL3_TREMOLO_DEPTH;
566 /* Set vibrato depth */
567 if (params->vib_depth)
568 reg_val |= OPL3_VIBRATO_DEPTH;
569 /* Set percussion mode */
570 if (params->rhythm) {
571 reg_val |= OPL3_PERCUSSION_ENABLE;
572 opl3->rhythm = 1;
573 } else {
574 opl3->rhythm = 0;
575 }
576 /* Play percussion instruments */
577 if (params->bass)
578 reg_val |= OPL3_BASSDRUM_ON;
579 if (params->snare)
580 reg_val |= OPL3_SNAREDRUM_ON;
581 if (params->tomtom)
582 reg_val |= OPL3_TOMTOM_ON;
583 if (params->cymbal)
584 reg_val |= OPL3_CYMBAL_ON;
585 if (params->hihat)
586 reg_val |= OPL3_HIHAT_ON;
587
588 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
589 return 0;
590}
591
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100592static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
595 return -EINVAL;
596
597 opl3->fm_mode = mode;
598 if (opl3->hardware >= OPL3_HW_OPL3)
599 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00); /* Clear 4-op connections */
600
601 return 0;
602}
603
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100604static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 unsigned char reg_val;
607
608 /* OPL-3 only */
609 if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
610 return -EINVAL;
611
612 reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
613 OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
614 /* Set 4-op connections */
615 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
616
617 return 0;
618}
Takashi Iwaiac19e192006-04-28 15:13:39 +0200619