Import 2.3.28
[davej-history.git] / drivers / sound / opl3.c
blob7e005e754a097debd35f1cf081d30a9d387221fc
1 /*
2 * sound/opl3.c
4 * A low level driver for Yamaha YM3812 and OPL-3 -chips
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
14 * Changes
15 * Thomas Sailer ioctl code reworked (vmalloc/vfree removed)
16 * Alan Cox modularisation, fixed sound_mem allocs.
18 * Status
19 * Believed to work. Badly needs rewriting a bit to support multiple
20 * OPL3 devices.
23 #include <linux/module.h>
24 #include <linux/delay.h>
27 * Major improvements to the FM handling 30AUG92 by Rob Hooft,
28 * hooft@chem.ruu.nl
31 #include "sound_config.h"
32 #include "soundmodule.h"
34 #include "opl3.h"
36 #define MAX_VOICE 18
37 #define OFFS_4OP 11
39 struct voice_info
41 unsigned char keyon_byte;
42 long bender;
43 long bender_range;
44 unsigned long orig_freq;
45 unsigned long current_freq;
46 int volume;
47 int mode;
48 int panning; /* 0xffff means not set */
51 typedef struct opl_devinfo
53 int base;
54 int left_io, right_io;
55 int nr_voice;
56 int lv_map[MAX_VOICE];
58 struct voice_info voc[MAX_VOICE];
59 struct voice_alloc_info *v_alloc;
60 struct channel_info *chn_info;
62 struct sbi_instrument i_map[SBFM_MAXINSTR];
63 struct sbi_instrument *act_i[MAX_VOICE];
65 struct synth_info fm_info;
67 int busy;
68 int model;
69 unsigned char cmask;
71 int is_opl4;
72 int *osp;
73 } opl_devinfo;
75 static struct opl_devinfo *devc = NULL;
77 static int detected_model;
79 static int store_instr(int instr_no, struct sbi_instrument *instr);
80 static void freq_to_fnum(int freq, int *block, int *fnum);
81 static void opl3_command(int io_addr, unsigned int addr, unsigned int val);
82 static int opl3_kill_note(int dev, int voice, int note, int velocity);
84 static void enter_4op_mode(void)
86 int i;
87 static int v4op[MAX_VOICE] = {
88 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17
91 devc->cmask = 0x3f; /* Connect all possible 4 OP voice operators */
92 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f);
94 for (i = 0; i < 3; i++)
95 pv_map[i].voice_mode = 4;
96 for (i = 3; i < 6; i++)
97 pv_map[i].voice_mode = 0;
99 for (i = 9; i < 12; i++)
100 pv_map[i].voice_mode = 4;
101 for (i = 12; i < 15; i++)
102 pv_map[i].voice_mode = 0;
104 for (i = 0; i < 12; i++)
105 devc->lv_map[i] = v4op[i];
106 devc->v_alloc->max_voice = devc->nr_voice = 12;
109 static int opl3_ioctl(int dev, unsigned int cmd, caddr_t arg)
111 struct sbi_instrument ins;
113 switch (cmd) {
114 case SNDCTL_FM_LOAD_INSTR:
115 printk(KERN_WARNING "Warning: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
116 if (copy_from_user(&ins, arg, sizeof(ins)))
117 return -EFAULT;
118 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
119 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
120 return -EINVAL;
122 return store_instr(ins.channel, &ins);
124 case SNDCTL_SYNTH_INFO:
125 devc->fm_info.nr_voices = (devc->nr_voice == 12) ? 6 : devc->nr_voice;
126 if (copy_to_user(arg, &devc->fm_info, sizeof(devc->fm_info)))
127 return -EFAULT;
128 return 0;
130 case SNDCTL_SYNTH_MEMAVL:
131 return 0x7fffffff;
133 case SNDCTL_FM_4OP_ENABLE:
134 if (devc->model == 2)
135 enter_4op_mode();
136 return 0;
138 default:
139 return -EINVAL;
143 int opl3_detect(int ioaddr, int *osp)
146 * This function returns 1 if the FM chip is present at the given I/O port
147 * The detection algorithm plays with the timer built in the FM chip and
148 * looks for a change in the status register.
150 * Note! The timers of the FM chip are not connected to AdLib (and compatible)
151 * boards.
153 * Note2! The chip is initialized if detected.
156 unsigned char stat1, signature;
157 int i;
159 if (devc != NULL)
161 printk(KERN_ERR "opl3: Only one OPL3 supported.\n");
162 return 0;
165 devc = (struct opl_devinfo *)kmalloc(sizeof(*devc), GFP_KERNEL);
167 if (devc == NULL)
169 printk(KERN_ERR "opl3: Can't allocate memory for the device control "
170 "structure \n ");
171 return 0;
173 devc->osp = osp;
174 devc->base = ioaddr;
176 /* Reset timers 1 and 2 */
177 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
179 /* Reset the IRQ of the FM chip */
180 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
182 signature = stat1 = inb(ioaddr); /* Status register */
184 if (signature != 0x00 && signature != 0x06 && signature != 0x02 &&
185 signature != 0x0f)
187 MDB(printk(KERN_INFO "OPL3 not detected %x\n", signature));
188 return 0;
191 if (signature == 0x06) /* OPL2 */
193 detected_model = 2;
195 else if (signature == 0x00 || signature == 0x0f) /* OPL3 or OPL4 */
197 unsigned char tmp;
199 detected_model = 3;
202 * Detect availability of OPL4 (_experimental_). Works probably
203 * only after a cold boot. In addition the OPL4 port
204 * of the chip may not be connected to the PC bus at all.
207 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
208 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
210 if ((tmp = inb(ioaddr)) == 0x02) /* Have a OPL4 */
212 detected_model = 4;
215 if (!check_region(ioaddr - 8, 2)) /* OPL4 port is free */
217 int tmp;
219 outb((0x02), ioaddr - 8); /* Select OPL4 ID register */
220 udelay(10);
221 tmp = inb(ioaddr - 7); /* Read it */
222 udelay(10);
224 if (tmp == 0x20) /* OPL4 should return 0x20 here */
226 detected_model = 4;
227 outb((0xF8), ioaddr - 8); /* Select OPL4 FM mixer control */
228 udelay(10);
229 outb((0x1B), ioaddr - 7); /* Write value */
230 udelay(10);
232 else
233 detected_model = 3;
235 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
237 for (i = 0; i < 9; i++)
238 opl3_command(ioaddr, KEYON_BLOCK + i, 0); /*
239 * Note off
242 opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
243 opl3_command(ioaddr, PERCOSSION_REGISTER, 0x00); /*
244 * Melodic mode.
246 return 1;
249 static int opl3_kill_note (int devno, int voice, int note, int velocity)
251 struct physical_voice_info *map;
253 if (voice < 0 || voice >= devc->nr_voice)
254 return 0;
256 devc->v_alloc->map[voice] = 0;
258 map = &pv_map[devc->lv_map[voice]];
259 DEB(printk("Kill note %d\n", voice));
261 if (map->voice_mode == 0)
262 return 0;
264 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, devc->voc[voice].keyon_byte & ~0x20);
265 devc->voc[voice].keyon_byte = 0;
266 devc->voc[voice].bender = 0;
267 devc->voc[voice].volume = 64;
268 devc->voc[voice].panning = 0xffff; /* Not set */
269 devc->voc[voice].bender_range = 200;
270 devc->voc[voice].orig_freq = 0;
271 devc->voc[voice].current_freq = 0;
272 devc->voc[voice].mode = 0;
273 return 0;
276 #define HIHAT 0
277 #define CYMBAL 1
278 #define TOMTOM 2
279 #define SNARE 3
280 #define BDRUM 4
281 #define UNDEFINED TOMTOM
282 #define DEFAULT TOMTOM
284 static int store_instr(int instr_no, struct sbi_instrument *instr)
286 if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || devc->model != 2))
287 printk(KERN_WARNING "FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
288 memcpy((char *) &(devc->i_map[instr_no]), (char *) instr, sizeof(*instr));
289 return 0;
292 static int opl3_set_instr (int dev, int voice, int instr_no)
294 if (voice < 0 || voice >= devc->nr_voice)
295 return 0;
296 if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
297 instr_no = 0; /* Acoustic piano (usually) */
299 devc->act_i[voice] = &devc->i_map[instr_no];
300 return 0;
304 * The next table looks magical, but it certainly is not. Its values have
305 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
306 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
307 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
308 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
309 * volume -8 it was implemented as a table because it is only 128 bytes and
310 * it saves a lot of log() calculations. (RH)
313 static char fm_volume_table[128] =
315 -64, -48, -40, -35, -32, -29, -27, -26,
316 -24, -23, -21, -20, -19, -18, -18, -17,
317 -16, -15, -15, -14, -13, -13, -12, -12,
318 -11, -11, -10, -10, -10, -9, -9, -8,
319 -8, -8, -7, -7, -7, -6, -6, -6,
320 -5, -5, -5, -5, -4, -4, -4, -4,
321 -3, -3, -3, -3, -2, -2, -2, -2,
322 -2, -1, -1, -1, -1, 0, 0, 0,
323 0, 0, 0, 1, 1, 1, 1, 1,
324 1, 2, 2, 2, 2, 2, 2, 2,
325 3, 3, 3, 3, 3, 3, 3, 4,
326 4, 4, 4, 4, 4, 4, 4, 5,
327 5, 5, 5, 5, 5, 5, 5, 5,
328 6, 6, 6, 6, 6, 6, 6, 6,
329 6, 7, 7, 7, 7, 7, 7, 7,
330 7, 7, 7, 8, 8, 8, 8, 8
333 static void calc_vol(unsigned char *regbyte, int volume, int main_vol)
335 int level = (~*regbyte & 0x3f);
337 if (main_vol > 127)
338 main_vol = 127;
339 volume = (volume * main_vol) / 127;
341 if (level)
342 level += fm_volume_table[volume];
344 if (level > 0x3f)
345 level = 0x3f;
346 if (level < 0)
347 level = 0;
349 *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
352 static void set_voice_volume(int voice, int volume, int main_vol)
354 unsigned char vol1, vol2, vol3, vol4;
355 struct sbi_instrument *instr;
356 struct physical_voice_info *map;
358 if (voice < 0 || voice >= devc->nr_voice)
359 return;
361 map = &pv_map[devc->lv_map[voice]];
362 instr = devc->act_i[voice];
364 if (!instr)
365 instr = &devc->i_map[0];
367 if (instr->channel < 0)
368 return;
370 if (devc->voc[voice].mode == 0)
371 return;
373 if (devc->voc[voice].mode == 2)
375 vol1 = instr->operators[2];
376 vol2 = instr->operators[3];
377 if ((instr->operators[10] & 0x01))
379 calc_vol(&vol1, volume, main_vol);
380 calc_vol(&vol2, volume, main_vol);
382 else
384 calc_vol(&vol2, volume, main_vol);
386 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
387 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
389 else
390 { /*
391 * 4 OP voice
393 int connection;
395 vol1 = instr->operators[2];
396 vol2 = instr->operators[3];
397 vol3 = instr->operators[OFFS_4OP + 2];
398 vol4 = instr->operators[OFFS_4OP + 3];
401 * The connection method for 4 OP devc->voc is defined by the rightmost
402 * bits at the offsets 10 and 10+OFFS_4OP
405 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
407 switch (connection)
409 case 0:
410 calc_vol(&vol4, volume, main_vol);
411 break;
413 case 1:
414 calc_vol(&vol2, volume, main_vol);
415 calc_vol(&vol4, volume, main_vol);
416 break;
418 case 2:
419 calc_vol(&vol1, volume, main_vol);
420 calc_vol(&vol4, volume, main_vol);
421 break;
423 case 3:
424 calc_vol(&vol1, volume, main_vol);
425 calc_vol(&vol3, volume, main_vol);
426 calc_vol(&vol4, volume, main_vol);
427 break;
429 default:
432 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
433 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
434 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3);
435 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4);
439 static int opl3_start_note (int dev, int voice, int note, int volume)
441 unsigned char data, fpc;
442 int block, fnum, freq, voice_mode, pan;
443 struct sbi_instrument *instr;
444 struct physical_voice_info *map;
446 if (voice < 0 || voice >= devc->nr_voice)
447 return 0;
449 map = &pv_map[devc->lv_map[voice]];
450 pan = devc->voc[voice].panning;
452 if (map->voice_mode == 0)
453 return 0;
455 if (note == 255) /*
456 * Just change the volume
459 set_voice_volume(voice, volume, devc->voc[voice].volume);
460 return 0;
464 * Kill previous note before playing
467 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff); /*
468 * Carrier
469 * volume to
470 * min
472 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff); /*
473 * Modulator
474 * volume to
477 if (map->voice_mode == 4)
479 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
480 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
483 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00); /*
484 * Note
485 * off
488 instr = devc->act_i[voice];
490 if (!instr)
491 instr = &devc->i_map[0];
493 if (instr->channel < 0)
495 printk(KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice);
496 return 0;
499 if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
500 return 0; /*
501 * Cannot play
504 voice_mode = map->voice_mode;
506 if (voice_mode == 4)
508 int voice_shift;
510 voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
511 voice_shift += map->voice_num;
513 if (instr->key != OPL3_PATCH) /*
514 * Just 2 OP patch
517 voice_mode = 2;
518 devc->cmask &= ~(1 << voice_shift);
520 else
522 devc->cmask |= (1 << voice_shift);
525 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
529 * Set Sound Characteristics
532 opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
533 opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
536 * Set Attack/Decay
539 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
540 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
543 * Set Sustain/Release
546 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
547 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
550 * Set Wave Select
553 opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
554 opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
557 * Set Feedback/Connection
560 fpc = instr->operators[10];
562 if (pan != 0xffff)
564 fpc &= ~STEREO_BITS;
565 if (pan < -64)
566 fpc |= VOICE_TO_LEFT;
567 else
568 if (pan > 64)
569 fpc |= VOICE_TO_RIGHT;
570 else
571 fpc |= (VOICE_TO_LEFT | VOICE_TO_RIGHT);
574 if (!(fpc & 0x30))
575 fpc |= 0x30; /*
576 * Ensure that at least one chn is enabled
578 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
581 * If the voice is a 4 OP one, initialize the operators 3 and 4 also
584 if (voice_mode == 4)
587 * Set Sound Characteristics
590 opl3_command(map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
591 opl3_command(map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
594 * Set Attack/Decay
597 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
598 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
601 * Set Sustain/Release
604 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
605 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
608 * Set Wave Select
611 opl3_command(map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
612 opl3_command(map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
615 * Set Feedback/Connection
618 fpc = instr->operators[OFFS_4OP + 10];
619 if (!(fpc & 0x30))
620 fpc |= 0x30; /*
621 * Ensure that at least one chn is enabled
623 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
626 devc->voc[voice].mode = voice_mode;
627 set_voice_volume(voice, volume, devc->voc[voice].volume);
629 freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000;
632 * Since the pitch bender may have been set before playing the note, we
633 * have to calculate the bending now.
636 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
637 devc->voc[voice].current_freq = freq;
639 freq_to_fnum(freq, &block, &fnum);
642 * Play note
645 data = fnum & 0xff; /*
646 * Least significant bits of fnumber
648 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
650 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
651 devc->voc[voice].keyon_byte = data;
652 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
653 if (voice_mode == 4)
654 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
656 return 0;
659 static void freq_to_fnum (int freq, int *block, int *fnum)
661 int f, octave;
664 * Converts the note frequency to block and fnum values for the FM chip
667 * First try to compute the block -value (octave) where the note belongs
670 f = freq;
672 octave = 5;
674 if (f == 0)
675 octave = 0;
676 else if (f < 261)
678 while (f < 261)
680 octave--;
681 f <<= 1;
684 else if (f > 493)
686 while (f > 493)
688 octave++;
689 f >>= 1;
693 if (octave > 7)
694 octave = 7;
696 *fnum = freq * (1 << (20 - octave)) / 49716;
697 *block = octave;
700 static void opl3_command (int io_addr, unsigned int addr, unsigned int val)
702 int i;
705 * The original 2-OP synth requires a quite long delay after writing to a
706 * register. The OPL-3 survives with just two INBs
709 outb(((unsigned char) (addr & 0xff)), io_addr);
711 if (devc->model != 2)
712 udelay(10);
713 else
714 for (i = 0; i < 2; i++)
715 inb(io_addr);
717 outb(((unsigned char) (val & 0xff)), io_addr + 1);
719 if (devc->model != 2)
720 udelay(30);
721 else
722 for (i = 0; i < 2; i++)
723 inb(io_addr);
726 static void opl3_reset(int devno)
728 int i;
730 for (i = 0; i < 18; i++)
731 devc->lv_map[i] = i;
733 for (i = 0; i < devc->nr_voice; i++)
735 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
736 KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
738 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
739 KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
741 if (pv_map[devc->lv_map[i]].voice_mode == 4)
743 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
744 KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
746 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
747 KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
750 opl3_kill_note(devno, i, 0, 64);
753 if (devc->model == 2)
755 devc->v_alloc->max_voice = devc->nr_voice = 18;
757 for (i = 0; i < 18; i++)
758 pv_map[i].voice_mode = 2;
763 static int opl3_open(int dev, int mode)
765 int i;
767 if (devc->busy)
768 return -EBUSY;
769 MOD_INC_USE_COUNT;
770 devc->busy = 1;
772 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
773 devc->v_alloc->timestamp = 0;
775 for (i = 0; i < 18; i++)
777 devc->v_alloc->map[i] = 0;
778 devc->v_alloc->alloc_times[i] = 0;
781 devc->cmask = 0x00; /*
782 * Just 2 OP mode
784 if (devc->model == 2)
785 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
786 return 0;
789 static void opl3_close(int dev)
791 devc->busy = 0;
792 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
794 devc->fm_info.nr_drums = 0;
795 devc->fm_info.perc_mode = 0;
797 opl3_reset(dev);
798 MOD_DEC_USE_COUNT;
801 static void opl3_hw_control(int dev, unsigned char *event)
805 static int opl3_load_patch(int dev, int format, const char *addr,
806 int offs, int count, int pmgr_flag)
808 struct sbi_instrument ins;
810 if (count <sizeof(ins))
812 printk(KERN_WARNING "FM Error: Patch record too short\n");
813 return -EINVAL;
816 if(copy_from_user(&((char *) &ins)[offs], &(addr)[offs], sizeof(ins) - offs))
817 return -EFAULT;
819 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
821 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
822 return -EINVAL;
824 ins.key = format;
826 return store_instr(ins.channel, &ins);
829 static void opl3_panning(int dev, int voice, int value)
831 devc->voc[voice].panning = value;
834 static void opl3_volume_method(int dev, int mode)
838 #define SET_VIBRATO(cell) { \
839 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
840 if (pressure > 110) \
841 tmp |= 0x40; /* Vibrato on */ \
842 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
844 static void opl3_aftertouch(int dev, int voice, int pressure)
846 int tmp;
847 struct sbi_instrument *instr;
848 struct physical_voice_info *map;
850 if (voice < 0 || voice >= devc->nr_voice)
851 return;
853 map = &pv_map[devc->lv_map[voice]];
855 DEB(printk("Aftertouch %d\n", voice));
857 if (map->voice_mode == 0)
858 return;
861 * Adjust the amount of vibrato depending the pressure
864 instr = devc->act_i[voice];
866 if (!instr)
867 instr = &devc->i_map[0];
869 if (devc->voc[voice].mode == 4)
871 int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
873 switch (connection)
875 case 0:
876 SET_VIBRATO(4);
877 break;
879 case 1:
880 SET_VIBRATO(2);
881 SET_VIBRATO(4);
882 break;
884 case 2:
885 SET_VIBRATO(1);
886 SET_VIBRATO(4);
887 break;
889 case 3:
890 SET_VIBRATO(1);
891 SET_VIBRATO(3);
892 SET_VIBRATO(4);
893 break;
897 * Not implemented yet
900 else
902 SET_VIBRATO(1);
904 if ((instr->operators[10] & 0x01)) /*
905 * Additive synthesis
907 SET_VIBRATO(2);
911 #undef SET_VIBRATO
913 static void bend_pitch(int dev, int voice, int value)
915 unsigned char data;
916 int block, fnum, freq;
917 struct physical_voice_info *map;
919 map = &pv_map[devc->lv_map[voice]];
921 if (map->voice_mode == 0)
922 return;
924 devc->voc[voice].bender = value;
925 if (!value)
926 return;
927 if (!(devc->voc[voice].keyon_byte & 0x20))
928 return; /*
929 * Not keyed on
932 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
933 devc->voc[voice].current_freq = freq;
935 freq_to_fnum(freq, &block, &fnum);
937 data = fnum & 0xff; /*
938 * Least significant bits of fnumber
940 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
942 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
943 devc->voc[voice].keyon_byte = data;
944 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
947 static void opl3_controller (int dev, int voice, int ctrl_num, int value)
949 if (voice < 0 || voice >= devc->nr_voice)
950 return;
952 switch (ctrl_num)
954 case CTRL_PITCH_BENDER:
955 bend_pitch(dev, voice, value);
956 break;
958 case CTRL_PITCH_BENDER_RANGE:
959 devc->voc[voice].bender_range = value;
960 break;
962 case CTL_MAIN_VOLUME:
963 devc->voc[voice].volume = value / 128;
964 break;
966 case CTL_PAN:
967 devc->voc[voice].panning = (value * 2) - 128;
968 break;
972 static void opl3_bender(int dev, int voice, int value)
974 if (voice < 0 || voice >= devc->nr_voice)
975 return;
977 bend_pitch(dev, voice, value - 8192);
980 static int opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info *alloc)
982 int i, p, best, first, avail, best_time = 0x7fffffff;
983 struct sbi_instrument *instr;
984 int is4op;
985 int instr_no;
987 if (chn < 0 || chn > 15)
988 instr_no = 0;
989 else
990 instr_no = devc->chn_info[chn].pgm_num;
992 instr = &devc->i_map[instr_no];
993 if (instr->channel < 0 || /* Instrument not loaded */
994 devc->nr_voice != 12) /* Not in 4 OP mode */
995 is4op = 0;
996 else if (devc->nr_voice == 12) /* 4 OP mode */
997 is4op = (instr->key == OPL3_PATCH);
998 else
999 is4op = 0;
1001 if (is4op)
1003 first = p = 0;
1004 avail = 6;
1006 else
1008 if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP only' operators first */
1009 first = p = 6;
1010 else
1011 first = p = 0;
1012 avail = devc->nr_voice;
1016 * Now try to find a free voice
1018 best = first;
1020 for (i = 0; i < avail; i++)
1022 if (alloc->map[p] == 0)
1024 return p;
1026 if (alloc->alloc_times[p] < best_time) /* Find oldest playing note */
1028 best_time = alloc->alloc_times[p];
1029 best = p;
1031 p = (p + 1) % avail;
1035 * Insert some kind of priority mechanism here.
1038 if (best < 0)
1039 best = 0;
1040 if (best > devc->nr_voice)
1041 best -= devc->nr_voice;
1043 return best; /* All devc->voc in use. Select the first one. */
1046 static void opl3_setup_voice(int dev, int voice, int chn)
1048 struct channel_info *info =
1049 &synth_devs[dev]->chn_info[chn];
1051 opl3_set_instr(dev, voice, info->pgm_num);
1053 devc->voc[voice].bender = 0;
1054 devc->voc[voice].bender_range = info->bender_range;
1055 devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
1056 devc->voc[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
1059 static struct synth_operations opl3_operations =
1061 "OPL",
1062 NULL,
1064 SYNTH_TYPE_FM,
1065 FM_TYPE_ADLIB,
1066 opl3_open,
1067 opl3_close,
1068 opl3_ioctl,
1069 opl3_kill_note,
1070 opl3_start_note,
1071 opl3_set_instr,
1072 opl3_reset,
1073 opl3_hw_control,
1074 opl3_load_patch,
1075 opl3_aftertouch,
1076 opl3_controller,
1077 opl3_panning,
1078 opl3_volume_method,
1079 opl3_bender,
1080 opl3_alloc_voice,
1081 opl3_setup_voice
1084 int opl3_init(int ioaddr, int *osp)
1086 int i;
1087 int me;
1089 if (devc == NULL)
1091 printk(KERN_ERR "opl3: Device control structure not initialized.\n");
1092 return -1;
1095 if ((me = sound_alloc_synthdev()) == -1)
1097 printk(KERN_WARNING "opl3: Too many synthesizers\n");
1098 return -1;
1101 memset((char *) devc, 0x00, sizeof(*devc));
1102 devc->osp = osp;
1103 devc->base = ioaddr;
1105 devc->nr_voice = 9;
1106 strcpy(devc->fm_info.name, "OPL2");
1108 devc->fm_info.device = 0;
1109 devc->fm_info.synth_type = SYNTH_TYPE_FM;
1110 devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
1111 devc->fm_info.perc_mode = 0;
1112 devc->fm_info.nr_voices = 9;
1113 devc->fm_info.nr_drums = 0;
1114 devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
1115 devc->fm_info.capabilities = 0;
1116 devc->left_io = ioaddr;
1117 devc->right_io = ioaddr + 2;
1119 if (detected_model <= 2)
1120 devc->model = 1;
1121 else
1123 devc->model = 2;
1124 if (detected_model == 4)
1125 devc->is_opl4 = 1;
1128 opl3_operations.info = &devc->fm_info;
1130 synth_devs[me] = &opl3_operations;
1131 sequencer_init();
1132 devc->v_alloc = &opl3_operations.alloc;
1133 devc->chn_info = &opl3_operations.chn_info[0];
1135 if (devc->model == 2)
1137 if (devc->is_opl4)
1138 strcpy(devc->fm_info.name, "Yamaha OPL4/OPL3 FM");
1139 else
1140 strcpy(devc->fm_info.name, "Yamaha OPL3");
1142 devc->v_alloc->max_voice = devc->nr_voice = 18;
1143 devc->fm_info.nr_drums = 0;
1144 devc->fm_info.synth_subtype = FM_TYPE_OPL3;
1145 devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
1147 for (i = 0; i < 18; i++)
1149 if (pv_map[i].ioaddr == USE_LEFT)
1150 pv_map[i].ioaddr = devc->left_io;
1151 else
1152 pv_map[i].ioaddr = devc->right_io;
1154 opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
1155 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
1157 else
1159 strcpy(devc->fm_info.name, "Yamaha OPL2");
1160 devc->v_alloc->max_voice = devc->nr_voice = 9;
1161 devc->fm_info.nr_drums = 0;
1163 for (i = 0; i < 18; i++)
1164 pv_map[i].ioaddr = devc->left_io;
1166 conf_printf2(devc->fm_info.name, ioaddr, 0, -1, -1);
1168 for (i = 0; i < SBFM_MAXINSTR; i++)
1169 devc->i_map[i].channel = -1;
1171 return me;
1174 #ifdef MODULE
1177 * We provide OPL3 functions.
1180 int io = -1;
1181 int me;
1183 int init_module (void)
1185 printk(KERN_INFO "YM3812 and OPL-3 driver Copyright (C) by Hannu Savolainen, Rob Hooft 1993-1996\n");
1186 if (io != -1) /* User loading pure OPL3 module */
1188 if (check_region(io, 4))
1190 printk(KERN_WARNING "opl3: I/O port 0x%x already in use\n", io);
1191 return 0;
1193 if (!opl3_detect(io, NULL))
1195 return -ENODEV;
1197 me = opl3_init(io, NULL);
1198 request_region(io,4,devc->fm_info.name);
1201 SOUND_LOCK;
1202 return 0;
1205 void cleanup_module(void)
1207 if (devc)
1209 if(devc->base)
1210 release_region(devc->base,4);
1211 kfree(devc);
1212 devc = NULL;
1213 sound_unload_synthdev(me);
1215 SOUND_LOCK_END;
1218 MODULE_PARM(io, "i");
1220 #endif
1222 EXPORT_SYMBOL(opl3_init);
1223 EXPORT_SYMBOL(opl3_detect);