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