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
15 * Thomas Sailer ioctl code reworked (vmalloc/vfree removed)
16 * Alan Cox modularisation, fixed sound_mem allocs.
17 * Christoph Hellwig Adapted to module_init/module_exit
18 * Arnaldo C. de Melo get rid of check_region, use request_region for
19 * OPL4, release it on exit, some cleanups.
22 * Believed to work. Badly needs rewriting a bit to support multiple
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
32 * Major improvements to the FM handling 30AUG92 by Rob Hooft,
36 #include "sound_config.h"
45 unsigned char keyon_byte;
48 unsigned long orig_freq;
49 unsigned long current_freq;
52 int panning; /* 0xffff means not set */
55 typedef struct opl_devinfo
58 int left_io, right_io;
60 int lv_map[MAX_VOICE];
62 struct voice_info voc[MAX_VOICE];
63 struct voice_alloc_info *v_alloc;
64 struct channel_info *chn_info;
66 struct sbi_instrument i_map[SBFM_MAXINSTR];
67 struct sbi_instrument *act_i[MAX_VOICE];
69 struct synth_info fm_info;
78 static struct opl_devinfo *devc = NULL;
80 static int detected_model;
82 static int store_instr(int instr_no, struct sbi_instrument *instr);
83 static void freq_to_fnum(int freq, int *block, int *fnum);
84 static void opl3_command(int io_addr, unsigned int addr, unsigned int val);
85 static int opl3_kill_note(int dev, int voice, int note, int velocity);
87 static void enter_4op_mode(void)
90 static int v4op[MAX_VOICE] = {
91 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17
94 devc->cmask = 0x3f; /* Connect all possible 4 OP voice operators */
95 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f);
97 for (i = 0; i < 3; i++)
98 pv_map[i].voice_mode = 4;
99 for (i = 3; i < 6; i++)
100 pv_map[i].voice_mode = 0;
102 for (i = 9; i < 12; i++)
103 pv_map[i].voice_mode = 4;
104 for (i = 12; i < 15; i++)
105 pv_map[i].voice_mode = 0;
107 for (i = 0; i < 12; i++)
108 devc->lv_map[i] = v4op[i];
109 devc->v_alloc->max_voice = devc->nr_voice = 12;
112 static int opl3_ioctl(int dev, unsigned int cmd, void __user * arg)
114 struct sbi_instrument ins;
117 case SNDCTL_FM_LOAD_INSTR:
118 printk(KERN_WARNING "Warning: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
119 if (copy_from_user(&ins, arg, sizeof(ins)))
121 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
122 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
125 return store_instr(ins.channel, &ins);
127 case SNDCTL_SYNTH_INFO:
128 devc->fm_info.nr_voices = (devc->nr_voice == 12) ? 6 : devc->nr_voice;
129 if (copy_to_user(arg, &devc->fm_info, sizeof(devc->fm_info)))
133 case SNDCTL_SYNTH_MEMAVL:
136 case SNDCTL_FM_4OP_ENABLE:
137 if (devc->model == 2)
146 static int opl3_detect(int ioaddr)
149 * This function returns 1 if the FM chip is present at the given I/O port
150 * The detection algorithm plays with the timer built in the FM chip and
151 * looks for a change in the status register.
153 * Note! The timers of the FM chip are not connected to AdLib (and compatible)
156 * Note2! The chip is initialized if detected.
159 unsigned char stat1, signature;
164 printk(KERN_ERR "opl3: Only one OPL3 supported.\n");
168 devc = kzalloc(sizeof(*devc), GFP_KERNEL);
172 printk(KERN_ERR "opl3: Can't allocate memory for the device control "
177 strcpy(devc->fm_info.name, "OPL2");
179 if (!request_region(ioaddr, 4, devc->fm_info.name)) {
180 printk(KERN_WARNING "opl3: I/O port 0x%x already in use\n", ioaddr);
186 /* Reset timers 1 and 2 */
187 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
189 /* Reset the IRQ of the FM chip */
190 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
192 signature = stat1 = inb(ioaddr); /* Status register */
194 if (signature != 0x00 && signature != 0x06 && signature != 0x02 &&
197 MDB(printk(KERN_INFO "OPL3 not detected %x\n", signature));
201 if (signature == 0x06) /* OPL2 */
205 else if (signature == 0x00 || signature == 0x0f) /* OPL3 or OPL4 */
212 * Detect availability of OPL4 (_experimental_). Works probably
213 * only after a cold boot. In addition the OPL4 port
214 * of the chip may not be connected to the PC bus at all.
217 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
218 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
220 if ((tmp = inb(ioaddr)) == 0x02) /* Have a OPL4 */
225 if (request_region(ioaddr - 8, 2, "OPL4")) /* OPL4 port was free */
229 outb((0x02), ioaddr - 8); /* Select OPL4 ID register */
231 tmp = inb(ioaddr - 7); /* Read it */
234 if (tmp == 0x20) /* OPL4 should return 0x20 here */
237 outb((0xF8), ioaddr - 8); /* Select OPL4 FM mixer control */
239 outb((0x1B), ioaddr - 7); /* Write value */
243 { /* release OPL4 port */
244 release_region(ioaddr - 8, 2);
248 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
250 for (i = 0; i < 9; i++)
251 opl3_command(ioaddr, KEYON_BLOCK + i, 0); /*
255 opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
256 opl3_command(ioaddr, PERCOSSION_REGISTER, 0x00); /*
261 release_region(ioaddr, 4);
268 static int opl3_kill_note (int devno, int voice, int note, int velocity)
270 struct physical_voice_info *map;
272 if (voice < 0 || voice >= devc->nr_voice)
275 devc->v_alloc->map[voice] = 0;
277 map = &pv_map[devc->lv_map[voice]];
278 DEB(printk("Kill note %d\n", voice));
280 if (map->voice_mode == 0)
283 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, devc->voc[voice].keyon_byte & ~0x20);
284 devc->voc[voice].keyon_byte = 0;
285 devc->voc[voice].bender = 0;
286 devc->voc[voice].volume = 64;
287 devc->voc[voice].panning = 0xffff; /* Not set */
288 devc->voc[voice].bender_range = 200;
289 devc->voc[voice].orig_freq = 0;
290 devc->voc[voice].current_freq = 0;
291 devc->voc[voice].mode = 0;
300 #define UNDEFINED TOMTOM
301 #define DEFAULT TOMTOM
303 static int store_instr(int instr_no, struct sbi_instrument *instr)
305 if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || devc->model != 2))
306 printk(KERN_WARNING "FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
307 memcpy((char *) &(devc->i_map[instr_no]), (char *) instr, sizeof(*instr));
311 static int opl3_set_instr (int dev, int voice, int instr_no)
313 if (voice < 0 || voice >= devc->nr_voice)
315 if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
316 instr_no = 0; /* Acoustic piano (usually) */
318 devc->act_i[voice] = &devc->i_map[instr_no];
323 * The next table looks magical, but it certainly is not. Its values have
324 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
325 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
326 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
327 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
328 * volume -8 it was implemented as a table because it is only 128 bytes and
329 * it saves a lot of log() calculations. (RH)
332 static char fm_volume_table[128] =
334 -64, -48, -40, -35, -32, -29, -27, -26,
335 -24, -23, -21, -20, -19, -18, -18, -17,
336 -16, -15, -15, -14, -13, -13, -12, -12,
337 -11, -11, -10, -10, -10, -9, -9, -8,
338 -8, -8, -7, -7, -7, -6, -6, -6,
339 -5, -5, -5, -5, -4, -4, -4, -4,
340 -3, -3, -3, -3, -2, -2, -2, -2,
341 -2, -1, -1, -1, -1, 0, 0, 0,
342 0, 0, 0, 1, 1, 1, 1, 1,
343 1, 2, 2, 2, 2, 2, 2, 2,
344 3, 3, 3, 3, 3, 3, 3, 4,
345 4, 4, 4, 4, 4, 4, 4, 5,
346 5, 5, 5, 5, 5, 5, 5, 5,
347 6, 6, 6, 6, 6, 6, 6, 6,
348 6, 7, 7, 7, 7, 7, 7, 7,
349 7, 7, 7, 8, 8, 8, 8, 8
352 static void calc_vol(unsigned char *regbyte, int volume, int main_vol)
354 int level = (~*regbyte & 0x3f);
358 volume = (volume * main_vol) / 127;
361 level += fm_volume_table[volume];
368 *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
371 static void set_voice_volume(int voice, int volume, int main_vol)
373 unsigned char vol1, vol2, vol3, vol4;
374 struct sbi_instrument *instr;
375 struct physical_voice_info *map;
377 if (voice < 0 || voice >= devc->nr_voice)
380 map = &pv_map[devc->lv_map[voice]];
381 instr = devc->act_i[voice];
384 instr = &devc->i_map[0];
386 if (instr->channel < 0)
389 if (devc->voc[voice].mode == 0)
392 if (devc->voc[voice].mode == 2)
394 vol1 = instr->operators[2];
395 vol2 = instr->operators[3];
396 if ((instr->operators[10] & 0x01))
398 calc_vol(&vol1, volume, main_vol);
399 calc_vol(&vol2, volume, main_vol);
403 calc_vol(&vol2, volume, main_vol);
405 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
406 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
414 vol1 = instr->operators[2];
415 vol2 = instr->operators[3];
416 vol3 = instr->operators[OFFS_4OP + 2];
417 vol4 = instr->operators[OFFS_4OP + 3];
420 * The connection method for 4 OP devc->voc is defined by the rightmost
421 * bits at the offsets 10 and 10+OFFS_4OP
424 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
429 calc_vol(&vol4, volume, main_vol);
433 calc_vol(&vol2, volume, main_vol);
434 calc_vol(&vol4, volume, main_vol);
438 calc_vol(&vol1, volume, main_vol);
439 calc_vol(&vol4, volume, main_vol);
443 calc_vol(&vol1, volume, main_vol);
444 calc_vol(&vol3, volume, main_vol);
445 calc_vol(&vol4, volume, main_vol);
451 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
452 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
453 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3);
454 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4);
458 static int opl3_start_note (int dev, int voice, int note, int volume)
460 unsigned char data, fpc;
461 int block, fnum, freq, voice_mode, pan;
462 struct sbi_instrument *instr;
463 struct physical_voice_info *map;
465 if (voice < 0 || voice >= devc->nr_voice)
468 map = &pv_map[devc->lv_map[voice]];
469 pan = devc->voc[voice].panning;
471 if (map->voice_mode == 0)
475 * Just change the volume
478 set_voice_volume(voice, volume, devc->voc[voice].volume);
483 * Kill previous note before playing
486 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff); /*
491 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff); /*
496 if (map->voice_mode == 4)
498 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
499 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
502 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00); /*
507 instr = devc->act_i[voice];
510 instr = &devc->i_map[0];
512 if (instr->channel < 0)
514 printk(KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice);
518 if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
523 voice_mode = map->voice_mode;
529 voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
530 voice_shift += map->voice_num;
532 if (instr->key != OPL3_PATCH) /*
537 devc->cmask &= ~(1 << voice_shift);
541 devc->cmask |= (1 << voice_shift);
544 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
548 * Set Sound Characteristics
551 opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
552 opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
558 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
559 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
562 * Set Sustain/Release
565 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
566 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
572 opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
573 opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
576 * Set Feedback/Connection
579 fpc = instr->operators[10];
585 fpc |= VOICE_TO_LEFT;
588 fpc |= VOICE_TO_RIGHT;
590 fpc |= (VOICE_TO_LEFT | VOICE_TO_RIGHT);
595 * Ensure that at least one chn is enabled
597 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
600 * If the voice is a 4 OP one, initialize the operators 3 and 4 also
606 * Set Sound Characteristics
609 opl3_command(map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
610 opl3_command(map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
616 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
617 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
620 * Set Sustain/Release
623 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
624 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
630 opl3_command(map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
631 opl3_command(map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
634 * Set Feedback/Connection
637 fpc = instr->operators[OFFS_4OP + 10];
640 * Ensure that at least one chn is enabled
642 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
645 devc->voc[voice].mode = voice_mode;
646 set_voice_volume(voice, volume, devc->voc[voice].volume);
648 freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000;
651 * Since the pitch bender may have been set before playing the note, we
652 * have to calculate the bending now.
655 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
656 devc->voc[voice].current_freq = freq;
658 freq_to_fnum(freq, &block, &fnum);
664 data = fnum & 0xff; /*
665 * Least significant bits of fnumber
667 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
669 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
670 devc->voc[voice].keyon_byte = data;
671 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
673 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
678 static void freq_to_fnum (int freq, int *block, int *fnum)
683 * Converts the note frequency to block and fnum values for the FM chip
686 * First try to compute the block -value (octave) where the note belongs
715 *fnum = freq * (1 << (20 - octave)) / 49716;
719 static void opl3_command (int io_addr, unsigned int addr, unsigned int val)
724 * The original 2-OP synth requires a quite long delay after writing to a
725 * register. The OPL-3 survives with just two INBs
728 outb(((unsigned char) (addr & 0xff)), io_addr);
730 if (devc->model != 2)
733 for (i = 0; i < 2; i++)
736 outb(((unsigned char) (val & 0xff)), io_addr + 1);
738 if (devc->model != 2)
741 for (i = 0; i < 2; i++)
745 static void opl3_reset(int devno)
749 for (i = 0; i < 18; i++)
752 for (i = 0; i < devc->nr_voice; i++)
754 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
755 KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
757 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
758 KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
760 if (pv_map[devc->lv_map[i]].voice_mode == 4)
762 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
763 KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
765 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
766 KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
769 opl3_kill_note(devno, i, 0, 64);
772 if (devc->model == 2)
774 devc->v_alloc->max_voice = devc->nr_voice = 18;
776 for (i = 0; i < 18; i++)
777 pv_map[i].voice_mode = 2;
782 static int opl3_open(int dev, int mode)
790 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
791 devc->v_alloc->timestamp = 0;
793 for (i = 0; i < 18; i++)
795 devc->v_alloc->map[i] = 0;
796 devc->v_alloc->alloc_times[i] = 0;
799 devc->cmask = 0x00; /*
802 if (devc->model == 2)
803 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
807 static void opl3_close(int dev)
810 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
812 devc->fm_info.nr_drums = 0;
813 devc->fm_info.perc_mode = 0;
818 static void opl3_hw_control(int dev, unsigned char *event)
822 static int opl3_load_patch(int dev, int format, const char __user *addr,
823 int count, int pmgr_flag)
825 struct sbi_instrument ins;
827 if (count <sizeof(ins))
829 printk(KERN_WARNING "FM Error: Patch record too short\n");
833 if (copy_from_user(&ins, addr, sizeof(ins)))
836 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
838 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
843 return store_instr(ins.channel, &ins);
846 static void opl3_panning(int dev, int voice, int value)
848 devc->voc[voice].panning = value;
851 static void opl3_volume_method(int dev, int mode)
855 #define SET_VIBRATO(cell) { \
856 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
857 if (pressure > 110) \
858 tmp |= 0x40; /* Vibrato on */ \
859 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
861 static void opl3_aftertouch(int dev, int voice, int pressure)
864 struct sbi_instrument *instr;
865 struct physical_voice_info *map;
867 if (voice < 0 || voice >= devc->nr_voice)
870 map = &pv_map[devc->lv_map[voice]];
872 DEB(printk("Aftertouch %d\n", voice));
874 if (map->voice_mode == 0)
878 * Adjust the amount of vibrato depending the pressure
881 instr = devc->act_i[voice];
884 instr = &devc->i_map[0];
886 if (devc->voc[voice].mode == 4)
888 int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
914 * Not implemented yet
921 if ((instr->operators[10] & 0x01)) /*
930 static void bend_pitch(int dev, int voice, int value)
933 int block, fnum, freq;
934 struct physical_voice_info *map;
936 map = &pv_map[devc->lv_map[voice]];
938 if (map->voice_mode == 0)
941 devc->voc[voice].bender = value;
944 if (!(devc->voc[voice].keyon_byte & 0x20))
949 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
950 devc->voc[voice].current_freq = freq;
952 freq_to_fnum(freq, &block, &fnum);
954 data = fnum & 0xff; /*
955 * Least significant bits of fnumber
957 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
959 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
960 devc->voc[voice].keyon_byte = data;
961 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
964 static void opl3_controller (int dev, int voice, int ctrl_num, int value)
966 if (voice < 0 || voice >= devc->nr_voice)
971 case CTRL_PITCH_BENDER:
972 bend_pitch(dev, voice, value);
975 case CTRL_PITCH_BENDER_RANGE:
976 devc->voc[voice].bender_range = value;
979 case CTL_MAIN_VOLUME:
980 devc->voc[voice].volume = value / 128;
984 devc->voc[voice].panning = (value * 2) - 128;
989 static void opl3_bender(int dev, int voice, int value)
991 if (voice < 0 || voice >= devc->nr_voice)
994 bend_pitch(dev, voice, value - 8192);
997 static int opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info *alloc)
999 int i, p, best, first, avail, best_time = 0x7fffffff;
1000 struct sbi_instrument *instr;
1004 if (chn < 0 || chn > 15)
1007 instr_no = devc->chn_info[chn].pgm_num;
1009 instr = &devc->i_map[instr_no];
1010 if (instr->channel < 0 || /* Instrument not loaded */
1011 devc->nr_voice != 12) /* Not in 4 OP mode */
1013 else if (devc->nr_voice == 12) /* 4 OP mode */
1014 is4op = (instr->key == OPL3_PATCH);
1025 if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP only' operators first */
1029 avail = devc->nr_voice;
1033 * Now try to find a free voice
1037 for (i = 0; i < avail; i++)
1039 if (alloc->map[p] == 0)
1043 if (alloc->alloc_times[p] < best_time) /* Find oldest playing note */
1045 best_time = alloc->alloc_times[p];
1048 p = (p + 1) % avail;
1052 * Insert some kind of priority mechanism here.
1057 if (best > devc->nr_voice)
1058 best -= devc->nr_voice;
1060 return best; /* All devc->voc in use. Select the first one. */
1063 static void opl3_setup_voice(int dev, int voice, int chn)
1065 struct channel_info *info =
1066 &synth_devs[dev]->chn_info[chn];
1068 opl3_set_instr(dev, voice, info->pgm_num);
1070 devc->voc[voice].bender = 0;
1071 devc->voc[voice].bender_range = info->bender_range;
1072 devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
1073 devc->voc[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
1076 static struct synth_operations opl3_operations =
1078 .owner = THIS_MODULE,
1082 .synth_type = SYNTH_TYPE_FM,
1083 .synth_subtype = FM_TYPE_ADLIB,
1085 .close = opl3_close,
1086 .ioctl = opl3_ioctl,
1087 .kill_note = opl3_kill_note,
1088 .start_note = opl3_start_note,
1089 .set_instr = opl3_set_instr,
1090 .reset = opl3_reset,
1091 .hw_control = opl3_hw_control,
1092 .load_patch = opl3_load_patch,
1093 .aftertouch = opl3_aftertouch,
1094 .controller = opl3_controller,
1095 .panning = opl3_panning,
1096 .volume_method = opl3_volume_method,
1097 .bender = opl3_bender,
1098 .alloc_voice = opl3_alloc_voice,
1099 .setup_voice = opl3_setup_voice
1102 static int opl3_init(int ioaddr, struct module *owner)
1109 printk(KERN_ERR "opl3: Device control structure not initialized.\n");
1113 if ((me = sound_alloc_synthdev()) == -1)
1115 printk(KERN_WARNING "opl3: Too many synthesizers\n");
1121 devc->fm_info.device = 0;
1122 devc->fm_info.synth_type = SYNTH_TYPE_FM;
1123 devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
1124 devc->fm_info.perc_mode = 0;
1125 devc->fm_info.nr_voices = 9;
1126 devc->fm_info.nr_drums = 0;
1127 devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
1128 devc->fm_info.capabilities = 0;
1129 devc->left_io = ioaddr;
1130 devc->right_io = ioaddr + 2;
1132 if (detected_model <= 2)
1137 if (detected_model == 4)
1141 opl3_operations.info = &devc->fm_info;
1143 synth_devs[me] = &opl3_operations;
1146 synth_devs[me]->owner = owner;
1149 devc->v_alloc = &opl3_operations.alloc;
1150 devc->chn_info = &opl3_operations.chn_info[0];
1152 if (devc->model == 2)
1155 strcpy(devc->fm_info.name, "Yamaha OPL4/OPL3 FM");
1157 strcpy(devc->fm_info.name, "Yamaha OPL3");
1159 devc->v_alloc->max_voice = devc->nr_voice = 18;
1160 devc->fm_info.nr_drums = 0;
1161 devc->fm_info.synth_subtype = FM_TYPE_OPL3;
1162 devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
1164 for (i = 0; i < 18; i++)
1166 if (pv_map[i].ioaddr == USE_LEFT)
1167 pv_map[i].ioaddr = devc->left_io;
1169 pv_map[i].ioaddr = devc->right_io;
1171 opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
1172 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
1176 strcpy(devc->fm_info.name, "Yamaha OPL2");
1177 devc->v_alloc->max_voice = devc->nr_voice = 9;
1178 devc->fm_info.nr_drums = 0;
1180 for (i = 0; i < 18; i++)
1181 pv_map[i].ioaddr = devc->left_io;
1183 conf_printf2(devc->fm_info.name, ioaddr, 0, -1, -1);
1185 for (i = 0; i < SBFM_MAXINSTR; i++)
1186 devc->i_map[i].channel = -1;
1195 module_param(io, int, 0);
1197 static int __init init_opl3 (void)
1199 printk(KERN_INFO "YM3812 and OPL-3 driver Copyright (C) by Hannu Savolainen, Rob Hooft 1993-1996\n");
1201 if (io != -1) /* User loading pure OPL3 module */
1203 if (!opl3_detect(io))
1208 me = opl3_init(io, THIS_MODULE);
1214 static void __exit cleanup_opl3(void)
1216 if (devc && io != -1)
1219 release_region(devc->base,4);
1221 release_region(devc->base - 8, 2);
1225 sound_unload_synthdev(me);
1229 module_init(init_opl3);
1230 module_exit(cleanup_opl3);
1233 static int __init setup_opl3(char *str)
1238 str = get_options(str, ARRAY_SIZE(ints), ints);
1245 __setup("opl3=", setup_opl3);
1247 MODULE_LICENSE("GPL");