Merge branches 'release' and 'hp-cid' into release
[deliverable/linux.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 <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
44 char 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
61 EXPORT_SYMBOL(snd_opl3_regmap);
62
63 /*
64 * prototypes
65 */
66 static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note);
67 static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice);
68 static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params);
69 static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode);
70 static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection);
71
72 /* ------------------------------ */
73
74 /*
75 * open the device exclusively
76 */
77 int snd_opl3_open(struct snd_hwdep * hw, struct file *file)
78 {
79 return 0;
80 }
81
82 /*
83 * ioctl for hwdep device:
84 */
85 int snd_opl3_ioctl(struct snd_hwdep * hw, struct file *file,
86 unsigned int cmd, unsigned long arg)
87 {
88 struct snd_opl3 *opl3 = hw->private_data;
89 void __user *argp = (void __user *)arg;
90
91 snd_assert(opl3 != NULL, return -EINVAL);
92
93 switch (cmd) {
94 /* get information */
95 case SNDRV_DM_FM_IOCTL_INFO:
96 {
97 struct snd_dm_fm_info info;
98
99 info.fm_mode = opl3->fm_mode;
100 info.rhythm = opl3->rhythm;
101 if (copy_to_user(argp, &info, sizeof(struct snd_dm_fm_info)))
102 return -EFAULT;
103 return 0;
104 }
105
106 case SNDRV_DM_FM_IOCTL_RESET:
107 #ifdef CONFIG_SND_OSSEMUL
108 case SNDRV_DM_FM_OSS_IOCTL_RESET:
109 #endif
110 snd_opl3_reset(opl3);
111 return 0;
112
113 case SNDRV_DM_FM_IOCTL_PLAY_NOTE:
114 #ifdef CONFIG_SND_OSSEMUL
115 case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE:
116 #endif
117 {
118 struct snd_dm_fm_note note;
119 if (copy_from_user(&note, argp, sizeof(struct snd_dm_fm_note)))
120 return -EFAULT;
121 return snd_opl3_play_note(opl3, &note);
122 }
123
124 case SNDRV_DM_FM_IOCTL_SET_VOICE:
125 #ifdef CONFIG_SND_OSSEMUL
126 case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE:
127 #endif
128 {
129 struct snd_dm_fm_voice voice;
130 if (copy_from_user(&voice, argp, sizeof(struct snd_dm_fm_voice)))
131 return -EFAULT;
132 return snd_opl3_set_voice(opl3, &voice);
133 }
134
135 case SNDRV_DM_FM_IOCTL_SET_PARAMS:
136 #ifdef CONFIG_SND_OSSEMUL
137 case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS:
138 #endif
139 {
140 struct snd_dm_fm_params params;
141 if (copy_from_user(&params, argp, sizeof(struct snd_dm_fm_params)))
142 return -EFAULT;
143 return snd_opl3_set_params(opl3, &params);
144 }
145
146 case SNDRV_DM_FM_IOCTL_SET_MODE:
147 #ifdef CONFIG_SND_OSSEMUL
148 case SNDRV_DM_FM_OSS_IOCTL_SET_MODE:
149 #endif
150 return snd_opl3_set_mode(opl3, (int) arg);
151
152 case SNDRV_DM_FM_IOCTL_SET_CONNECTION:
153 #ifdef CONFIG_SND_OSSEMUL
154 case SNDRV_DM_FM_OSS_IOCTL_SET_OPL:
155 #endif
156 return snd_opl3_set_connection(opl3, (int) arg);
157
158 case SNDRV_DM_FM_IOCTL_CLEAR_PATCHES:
159 snd_opl3_clear_patches(opl3);
160 return 0;
161
162 #ifdef CONFIG_SND_DEBUG
163 default:
164 snd_printk("unknown IOCTL: 0x%x\n", cmd);
165 #endif
166 }
167 return -ENOTTY;
168 }
169
170 /*
171 * close the device
172 */
173 int snd_opl3_release(struct snd_hwdep * hw, struct file *file)
174 {
175 struct snd_opl3 *opl3 = hw->private_data;
176
177 snd_opl3_reset(opl3);
178 return 0;
179 }
180
181 /*
182 * write the device - load patches
183 */
184 long snd_opl3_write(struct snd_hwdep *hw, const char __user *buf, long count,
185 loff_t *offset)
186 {
187 struct snd_opl3 *opl3 = hw->private_data;
188 long result = 0;
189 int err = 0;
190 struct sbi_patch inst;
191
192 while (count >= sizeof(inst)) {
193 unsigned char type;
194 if (copy_from_user(&inst, buf, sizeof(inst)))
195 return -EFAULT;
196 if (!memcmp(inst.key, FM_KEY_SBI, 4) ||
197 !memcmp(inst.key, FM_KEY_2OP, 4))
198 type = FM_PATCH_OPL2;
199 else if (!memcmp(inst.key, FM_KEY_4OP, 4))
200 type = FM_PATCH_OPL3;
201 else /* invalid type */
202 break;
203 err = snd_opl3_load_patch(opl3, inst.prog, inst.bank, type,
204 inst.name, inst.extension,
205 inst.data);
206 if (err < 0)
207 break;
208 result += sizeof(inst);
209 count -= sizeof(inst);
210 }
211 return result > 0 ? result : err;
212 }
213
214
215 /*
216 * Patch management
217 */
218
219 /* offsets for SBI params */
220 #define AM_VIB 0
221 #define KSL_LEVEL 2
222 #define ATTACK_DECAY 4
223 #define SUSTAIN_RELEASE 6
224 #define WAVE_SELECT 8
225
226 /* offset for SBI instrument */
227 #define CONNECTION 10
228 #define OFFSET_4OP 11
229
230 /*
231 * load a patch, obviously.
232 *
233 * loaded on the given program and bank numbers with the given type
234 * (FM_PATCH_OPLx).
235 * data is the pointer of SBI record _without_ header (key and name).
236 * name is the name string of the patch.
237 * ext is the extension data of 7 bytes long (stored in name of SBI
238 * data up to offset 25), or NULL to skip.
239 * return 0 if successful or a negative error code.
240 */
241 int snd_opl3_load_patch(struct snd_opl3 *opl3,
242 int prog, int bank, int type,
243 const char *name,
244 const unsigned char *ext,
245 const unsigned char *data)
246 {
247 struct fm_patch *patch;
248 int i;
249
250 patch = snd_opl3_find_patch(opl3, prog, bank, 1);
251 if (!patch)
252 return -ENOMEM;
253
254 patch->type = type;
255
256 for (i = 0; i < 2; i++) {
257 patch->inst.op[i].am_vib = data[AM_VIB + i];
258 patch->inst.op[i].ksl_level = data[KSL_LEVEL + i];
259 patch->inst.op[i].attack_decay = data[ATTACK_DECAY + i];
260 patch->inst.op[i].sustain_release = data[SUSTAIN_RELEASE + i];
261 patch->inst.op[i].wave_select = data[WAVE_SELECT + i];
262 }
263 patch->inst.feedback_connection[0] = data[CONNECTION];
264
265 if (type == FM_PATCH_OPL3) {
266 for (i = 0; i < 2; i++) {
267 patch->inst.op[i+2].am_vib =
268 data[OFFSET_4OP + AM_VIB + i];
269 patch->inst.op[i+2].ksl_level =
270 data[OFFSET_4OP + KSL_LEVEL + i];
271 patch->inst.op[i+2].attack_decay =
272 data[OFFSET_4OP + ATTACK_DECAY + i];
273 patch->inst.op[i+2].sustain_release =
274 data[OFFSET_4OP + SUSTAIN_RELEASE + i];
275 patch->inst.op[i+2].wave_select =
276 data[OFFSET_4OP + WAVE_SELECT + i];
277 }
278 patch->inst.feedback_connection[1] =
279 data[OFFSET_4OP + CONNECTION];
280 }
281
282 if (ext) {
283 patch->inst.echo_delay = ext[0];
284 patch->inst.echo_atten = ext[1];
285 patch->inst.chorus_spread = ext[2];
286 patch->inst.trnsps = ext[3];
287 patch->inst.fix_dur = ext[4];
288 patch->inst.modes = ext[5];
289 patch->inst.fix_key = ext[6];
290 }
291
292 if (name)
293 strlcpy(patch->name, name, sizeof(patch->name));
294
295 return 0;
296 }
297 EXPORT_SYMBOL(snd_opl3_load_patch);
298
299 /*
300 * find a patch with the given program and bank numbers, returns its pointer
301 * if no matching patch is found and create_patch is set, it creates a
302 * new patch object.
303 */
304 struct fm_patch *snd_opl3_find_patch(struct snd_opl3 *opl3, int prog, int bank,
305 int create_patch)
306 {
307 /* pretty dumb hash key */
308 unsigned int key = (prog + bank) % OPL3_PATCH_HASH_SIZE;
309 struct fm_patch *patch;
310
311 for (patch = opl3->patch_table[key]; patch; patch = patch->next) {
312 if (patch->prog == prog && patch->bank == bank)
313 return patch;
314 }
315 if (!create_patch)
316 return NULL;
317
318 patch = kzalloc(sizeof(*patch), GFP_KERNEL);
319 if (!patch)
320 return NULL;
321 patch->prog = prog;
322 patch->bank = bank;
323 patch->next = opl3->patch_table[key];
324 opl3->patch_table[key] = patch;
325 return patch;
326 }
327 EXPORT_SYMBOL(snd_opl3_find_patch);
328
329 /*
330 * Clear all patches of the given OPL3 instance
331 */
332 void snd_opl3_clear_patches(struct snd_opl3 *opl3)
333 {
334 int i;
335 for (i = 0; i < OPL3_PATCH_HASH_SIZE; i++) {
336 struct fm_patch *patch, *next;
337 for (patch = opl3->patch_table[i]; patch; patch = next) {
338 next = patch->next;
339 kfree(patch);
340 }
341 }
342 memset(opl3->patch_table, 0, sizeof(opl3->patch_table));
343 }
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;
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 op_offset = snd_opl3_regmap[voice_offset][voice->op];
466
467 reg_val = 0x00;
468 /* Set amplitude modulation (tremolo) effect */
469 if (voice->am)
470 reg_val |= OPL3_TREMOLO_ON;
471 /* Set vibrato effect */
472 if (voice->vibrato)
473 reg_val |= OPL3_VIBRATO_ON;
474 /* Set sustaining sound phase */
475 if (voice->do_sustain)
476 reg_val |= OPL3_SUSTAIN_ON;
477 /* Set keyboard scaling bit */
478 if (voice->kbd_scale)
479 reg_val |= OPL3_KSR;
480 /* Set harmonic or frequency multiplier */
481 reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
482
483 /* Set OPL3 AM_VIB register of requested voice/operator */
484 opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
485 opl3->command(opl3, opl3_reg, reg_val);
486
487 /* Set decreasing volume of higher notes */
488 reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
489 /* Set output volume */
490 reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
491
492 /* Set OPL3 KSL_LEVEL register of requested voice/operator */
493 opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
494 opl3->command(opl3, opl3_reg, reg_val);
495
496 /* Set attack phase level */
497 reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
498 /* Set decay phase level */
499 reg_val |= voice->decay & OPL3_DECAY_MASK;
500
501 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
502 opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
503 opl3->command(opl3, opl3_reg, reg_val);
504
505 /* Set sustain phase level */
506 reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
507 /* Set release phase level */
508 reg_val |= voice->release & OPL3_RELEASE_MASK;
509
510 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
511 opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
512 opl3->command(opl3, opl3_reg, reg_val);
513
514 /* Set inter-operator feedback */
515 reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
516 /* Set inter-operator connection */
517 if (voice->connection)
518 reg_val |= OPL3_CONNECTION_BIT;
519 /* OPL-3 only */
520 if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
521 if (voice->left)
522 reg_val |= OPL3_VOICE_TO_LEFT;
523 if (voice->right)
524 reg_val |= OPL3_VOICE_TO_RIGHT;
525 }
526 /* Feedback/connection bits are applicable to voice */
527 opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
528 opl3->command(opl3, opl3_reg, reg_val);
529
530 /* Select waveform */
531 reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
532 opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
533 opl3->command(opl3, opl3_reg, reg_val);
534
535 return 0;
536 }
537
538 static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params)
539 {
540 unsigned char reg_val;
541
542 reg_val = 0x00;
543 /* Set keyboard split method */
544 if (params->kbd_split)
545 reg_val |= OPL3_KEYBOARD_SPLIT;
546 opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
547
548 reg_val = 0x00;
549 /* Set amplitude modulation (tremolo) depth */
550 if (params->am_depth)
551 reg_val |= OPL3_TREMOLO_DEPTH;
552 /* Set vibrato depth */
553 if (params->vib_depth)
554 reg_val |= OPL3_VIBRATO_DEPTH;
555 /* Set percussion mode */
556 if (params->rhythm) {
557 reg_val |= OPL3_PERCUSSION_ENABLE;
558 opl3->rhythm = 1;
559 } else {
560 opl3->rhythm = 0;
561 }
562 /* Play percussion instruments */
563 if (params->bass)
564 reg_val |= OPL3_BASSDRUM_ON;
565 if (params->snare)
566 reg_val |= OPL3_SNAREDRUM_ON;
567 if (params->tomtom)
568 reg_val |= OPL3_TOMTOM_ON;
569 if (params->cymbal)
570 reg_val |= OPL3_CYMBAL_ON;
571 if (params->hihat)
572 reg_val |= OPL3_HIHAT_ON;
573
574 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
575 return 0;
576 }
577
578 static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode)
579 {
580 if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
581 return -EINVAL;
582
583 opl3->fm_mode = mode;
584 if (opl3->hardware >= OPL3_HW_OPL3)
585 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00); /* Clear 4-op connections */
586
587 return 0;
588 }
589
590 static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection)
591 {
592 unsigned char reg_val;
593
594 /* OPL-3 only */
595 if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
596 return -EINVAL;
597
598 reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
599 OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
600 /* Set 4-op connections */
601 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
602
603 return 0;
604 }
605
This page took 0.059255 seconds and 6 git commands to generate.