Import 2.4.0-test6pre3
[davej-history.git] / drivers / sound / opl3.c
blob951dfe4d3611ce6d3e72696f7e93d0a76a10fcd6
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.
17 * Christoph Hellwig Adapted to module_init/module_exit
19 * Status
20 * Believed to work. Badly needs rewriting a bit to support multiple
21 * OPL3 devices.
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/delay.h>
29 * Major improvements to the FM handling 30AUG92 by Rob Hooft,
30 * hooft@chem.ruu.nl
33 #include "sound_config.h"
34 #include "soundmodule.h"
36 #include "opl3.h"
37 #include "opl3_hw.h"
39 #define MAX_VOICE 18
40 #define OFFS_4OP 11
42 struct voice_info
44 unsigned char keyon_byte;
45 long bender;
46 long bender_range;
47 unsigned long orig_freq;
48 unsigned long current_freq;
49 int volume;
50 int mode;
51 int panning; /* 0xffff means not set */
54 typedef struct opl_devinfo
56 int base;
57 int left_io, right_io;
58 int nr_voice;
59 int lv_map[MAX_VOICE];
61 struct voice_info voc[MAX_VOICE];
62 struct voice_alloc_info *v_alloc;
63 struct channel_info *chn_info;
65 struct sbi_instrument i_map[SBFM_MAXINSTR];
66 struct sbi_instrument *act_i[MAX_VOICE];
68 struct synth_info fm_info;
70 int busy;
71 int model;
72 unsigned char cmask;
74 int is_opl4;
75 int *osp;
76 } opl_devinfo;
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)
89 int i;
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, caddr_t arg)
114 struct sbi_instrument ins;
116 switch (cmd) {
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)))
120 return -EFAULT;
121 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
122 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
123 return -EINVAL;
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)))
130 return -EFAULT;
131 return 0;
133 case SNDCTL_SYNTH_MEMAVL:
134 return 0x7fffffff;
136 case SNDCTL_FM_4OP_ENABLE:
137 if (devc->model == 2)
138 enter_4op_mode();
139 return 0;
141 default:
142 return -EINVAL;
146 int opl3_detect(int ioaddr, int *osp)
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)
154 * boards.
156 * Note2! The chip is initialized if detected.
159 unsigned char stat1, signature;
160 int i;
162 if (devc != NULL)
164 printk(KERN_ERR "opl3: Only one OPL3 supported.\n");
165 return 0;
168 devc = (struct opl_devinfo *)kmalloc(sizeof(*devc), GFP_KERNEL);
170 if (devc == NULL)
172 printk(KERN_ERR "opl3: Can't allocate memory for the device control "
173 "structure \n ");
174 return 0;
176 devc->osp = osp;
177 devc->base = ioaddr;
179 /* Reset timers 1 and 2 */
180 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
182 /* Reset the IRQ of the FM chip */
183 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
185 signature = stat1 = inb(ioaddr); /* Status register */
187 if (signature != 0x00 && signature != 0x06 && signature != 0x02 &&
188 signature != 0x0f)
190 MDB(printk(KERN_INFO "OPL3 not detected %x\n", signature));
191 return 0;
194 if (signature == 0x06) /* OPL2 */
196 detected_model = 2;
198 else if (signature == 0x00 || signature == 0x0f) /* OPL3 or OPL4 */
200 unsigned char tmp;
202 detected_model = 3;
205 * Detect availability of OPL4 (_experimental_). Works probably
206 * only after a cold boot. In addition the OPL4 port
207 * of the chip may not be connected to the PC bus at all.
210 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
211 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
213 if ((tmp = inb(ioaddr)) == 0x02) /* Have a OPL4 */
215 detected_model = 4;
218 if (!check_region(ioaddr - 8, 2)) /* OPL4 port is free */
220 int tmp;
222 outb((0x02), ioaddr - 8); /* Select OPL4 ID register */
223 udelay(10);
224 tmp = inb(ioaddr - 7); /* Read it */
225 udelay(10);
227 if (tmp == 0x20) /* OPL4 should return 0x20 here */
229 detected_model = 4;
230 outb((0xF8), ioaddr - 8); /* Select OPL4 FM mixer control */
231 udelay(10);
232 outb((0x1B), ioaddr - 7); /* Write value */
233 udelay(10);
235 else
236 detected_model = 3;
238 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
240 for (i = 0; i < 9; i++)
241 opl3_command(ioaddr, KEYON_BLOCK + i, 0); /*
242 * Note off
245 opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
246 opl3_command(ioaddr, PERCOSSION_REGISTER, 0x00); /*
247 * Melodic mode.
249 return 1;
252 static int opl3_kill_note (int devno, int voice, int note, int velocity)
254 struct physical_voice_info *map;
256 if (voice < 0 || voice >= devc->nr_voice)
257 return 0;
259 devc->v_alloc->map[voice] = 0;
261 map = &pv_map[devc->lv_map[voice]];
262 DEB(printk("Kill note %d\n", voice));
264 if (map->voice_mode == 0)
265 return 0;
267 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, devc->voc[voice].keyon_byte & ~0x20);
268 devc->voc[voice].keyon_byte = 0;
269 devc->voc[voice].bender = 0;
270 devc->voc[voice].volume = 64;
271 devc->voc[voice].panning = 0xffff; /* Not set */
272 devc->voc[voice].bender_range = 200;
273 devc->voc[voice].orig_freq = 0;
274 devc->voc[voice].current_freq = 0;
275 devc->voc[voice].mode = 0;
276 return 0;
279 #define HIHAT 0
280 #define CYMBAL 1
281 #define TOMTOM 2
282 #define SNARE 3
283 #define BDRUM 4
284 #define UNDEFINED TOMTOM
285 #define DEFAULT TOMTOM
287 static int store_instr(int instr_no, struct sbi_instrument *instr)
289 if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || devc->model != 2))
290 printk(KERN_WARNING "FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
291 memcpy((char *) &(devc->i_map[instr_no]), (char *) instr, sizeof(*instr));
292 return 0;
295 static int opl3_set_instr (int dev, int voice, int instr_no)
297 if (voice < 0 || voice >= devc->nr_voice)
298 return 0;
299 if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
300 instr_no = 0; /* Acoustic piano (usually) */
302 devc->act_i[voice] = &devc->i_map[instr_no];
303 return 0;
307 * The next table looks magical, but it certainly is not. Its values have
308 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
309 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
310 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
311 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
312 * volume -8 it was implemented as a table because it is only 128 bytes and
313 * it saves a lot of log() calculations. (RH)
316 static char fm_volume_table[128] =
318 -64, -48, -40, -35, -32, -29, -27, -26,
319 -24, -23, -21, -20, -19, -18, -18, -17,
320 -16, -15, -15, -14, -13, -13, -12, -12,
321 -11, -11, -10, -10, -10, -9, -9, -8,
322 -8, -8, -7, -7, -7, -6, -6, -6,
323 -5, -5, -5, -5, -4, -4, -4, -4,
324 -3, -3, -3, -3, -2, -2, -2, -2,
325 -2, -1, -1, -1, -1, 0, 0, 0,
326 0, 0, 0, 1, 1, 1, 1, 1,
327 1, 2, 2, 2, 2, 2, 2, 2,
328 3, 3, 3, 3, 3, 3, 3, 4,
329 4, 4, 4, 4, 4, 4, 4, 5,
330 5, 5, 5, 5, 5, 5, 5, 5,
331 6, 6, 6, 6, 6, 6, 6, 6,
332 6, 7, 7, 7, 7, 7, 7, 7,
333 7, 7, 7, 8, 8, 8, 8, 8
336 static void calc_vol(unsigned char *regbyte, int volume, int main_vol)
338 int level = (~*regbyte & 0x3f);
340 if (main_vol > 127)
341 main_vol = 127;
342 volume = (volume * main_vol) / 127;
344 if (level)
345 level += fm_volume_table[volume];
347 if (level > 0x3f)
348 level = 0x3f;
349 if (level < 0)
350 level = 0;
352 *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
355 static void set_voice_volume(int voice, int volume, int main_vol)
357 unsigned char vol1, vol2, vol3, vol4;
358 struct sbi_instrument *instr;
359 struct physical_voice_info *map;
361 if (voice < 0 || voice >= devc->nr_voice)
362 return;
364 map = &pv_map[devc->lv_map[voice]];
365 instr = devc->act_i[voice];
367 if (!instr)
368 instr = &devc->i_map[0];
370 if (instr->channel < 0)
371 return;
373 if (devc->voc[voice].mode == 0)
374 return;
376 if (devc->voc[voice].mode == 2)
378 vol1 = instr->operators[2];
379 vol2 = instr->operators[3];
380 if ((instr->operators[10] & 0x01))
382 calc_vol(&vol1, volume, main_vol);
383 calc_vol(&vol2, volume, main_vol);
385 else
387 calc_vol(&vol2, volume, main_vol);
389 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
390 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
392 else
393 { /*
394 * 4 OP voice
396 int connection;
398 vol1 = instr->operators[2];
399 vol2 = instr->operators[3];
400 vol3 = instr->operators[OFFS_4OP + 2];
401 vol4 = instr->operators[OFFS_4OP + 3];
404 * The connection method for 4 OP devc->voc is defined by the rightmost
405 * bits at the offsets 10 and 10+OFFS_4OP
408 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
410 switch (connection)
412 case 0:
413 calc_vol(&vol4, volume, main_vol);
414 break;
416 case 1:
417 calc_vol(&vol2, volume, main_vol);
418 calc_vol(&vol4, volume, main_vol);
419 break;
421 case 2:
422 calc_vol(&vol1, volume, main_vol);
423 calc_vol(&vol4, volume, main_vol);
424 break;
426 case 3:
427 calc_vol(&vol1, volume, main_vol);
428 calc_vol(&vol3, volume, main_vol);
429 calc_vol(&vol4, volume, main_vol);
430 break;
432 default:
435 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
436 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
437 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3);
438 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4);
442 static int opl3_start_note (int dev, int voice, int note, int volume)
444 unsigned char data, fpc;
445 int block, fnum, freq, voice_mode, pan;
446 struct sbi_instrument *instr;
447 struct physical_voice_info *map;
449 if (voice < 0 || voice >= devc->nr_voice)
450 return 0;
452 map = &pv_map[devc->lv_map[voice]];
453 pan = devc->voc[voice].panning;
455 if (map->voice_mode == 0)
456 return 0;
458 if (note == 255) /*
459 * Just change the volume
462 set_voice_volume(voice, volume, devc->voc[voice].volume);
463 return 0;
467 * Kill previous note before playing
470 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff); /*
471 * Carrier
472 * volume to
473 * min
475 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff); /*
476 * Modulator
477 * volume to
480 if (map->voice_mode == 4)
482 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
483 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
486 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00); /*
487 * Note
488 * off
491 instr = devc->act_i[voice];
493 if (!instr)
494 instr = &devc->i_map[0];
496 if (instr->channel < 0)
498 printk(KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice);
499 return 0;
502 if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
503 return 0; /*
504 * Cannot play
507 voice_mode = map->voice_mode;
509 if (voice_mode == 4)
511 int voice_shift;
513 voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
514 voice_shift += map->voice_num;
516 if (instr->key != OPL3_PATCH) /*
517 * Just 2 OP patch
520 voice_mode = 2;
521 devc->cmask &= ~(1 << voice_shift);
523 else
525 devc->cmask |= (1 << voice_shift);
528 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
532 * Set Sound Characteristics
535 opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
536 opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
539 * Set Attack/Decay
542 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
543 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
546 * Set Sustain/Release
549 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
550 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
553 * Set Wave Select
556 opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
557 opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
560 * Set Feedback/Connection
563 fpc = instr->operators[10];
565 if (pan != 0xffff)
567 fpc &= ~STEREO_BITS;
568 if (pan < -64)
569 fpc |= VOICE_TO_LEFT;
570 else
571 if (pan > 64)
572 fpc |= VOICE_TO_RIGHT;
573 else
574 fpc |= (VOICE_TO_LEFT | VOICE_TO_RIGHT);
577 if (!(fpc & 0x30))
578 fpc |= 0x30; /*
579 * Ensure that at least one chn is enabled
581 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
584 * If the voice is a 4 OP one, initialize the operators 3 and 4 also
587 if (voice_mode == 4)
590 * Set Sound Characteristics
593 opl3_command(map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
594 opl3_command(map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
597 * Set Attack/Decay
600 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
601 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
604 * Set Sustain/Release
607 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
608 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
611 * Set Wave Select
614 opl3_command(map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
615 opl3_command(map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
618 * Set Feedback/Connection
621 fpc = instr->operators[OFFS_4OP + 10];
622 if (!(fpc & 0x30))
623 fpc |= 0x30; /*
624 * Ensure that at least one chn is enabled
626 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
629 devc->voc[voice].mode = voice_mode;
630 set_voice_volume(voice, volume, devc->voc[voice].volume);
632 freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000;
635 * Since the pitch bender may have been set before playing the note, we
636 * have to calculate the bending now.
639 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
640 devc->voc[voice].current_freq = freq;
642 freq_to_fnum(freq, &block, &fnum);
645 * Play note
648 data = fnum & 0xff; /*
649 * Least significant bits of fnumber
651 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
653 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
654 devc->voc[voice].keyon_byte = data;
655 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
656 if (voice_mode == 4)
657 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
659 return 0;
662 static void freq_to_fnum (int freq, int *block, int *fnum)
664 int f, octave;
667 * Converts the note frequency to block and fnum values for the FM chip
670 * First try to compute the block -value (octave) where the note belongs
673 f = freq;
675 octave = 5;
677 if (f == 0)
678 octave = 0;
679 else if (f < 261)
681 while (f < 261)
683 octave--;
684 f <<= 1;
687 else if (f > 493)
689 while (f > 493)
691 octave++;
692 f >>= 1;
696 if (octave > 7)
697 octave = 7;
699 *fnum = freq * (1 << (20 - octave)) / 49716;
700 *block = octave;
703 static void opl3_command (int io_addr, unsigned int addr, unsigned int val)
705 int i;
708 * The original 2-OP synth requires a quite long delay after writing to a
709 * register. The OPL-3 survives with just two INBs
712 outb(((unsigned char) (addr & 0xff)), io_addr);
714 if (devc->model != 2)
715 udelay(10);
716 else
717 for (i = 0; i < 2; i++)
718 inb(io_addr);
720 outb(((unsigned char) (val & 0xff)), io_addr + 1);
722 if (devc->model != 2)
723 udelay(30);
724 else
725 for (i = 0; i < 2; i++)
726 inb(io_addr);
729 static void opl3_reset(int devno)
731 int i;
733 for (i = 0; i < 18; i++)
734 devc->lv_map[i] = i;
736 for (i = 0; i < devc->nr_voice; i++)
738 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
739 KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
741 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
742 KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
744 if (pv_map[devc->lv_map[i]].voice_mode == 4)
746 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
747 KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
749 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
750 KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
753 opl3_kill_note(devno, i, 0, 64);
756 if (devc->model == 2)
758 devc->v_alloc->max_voice = devc->nr_voice = 18;
760 for (i = 0; i < 18; i++)
761 pv_map[i].voice_mode = 2;
766 static int opl3_open(int dev, int mode)
768 int i;
770 if (devc->busy)
771 return -EBUSY;
772 MOD_INC_USE_COUNT;
773 devc->busy = 1;
775 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
776 devc->v_alloc->timestamp = 0;
778 for (i = 0; i < 18; i++)
780 devc->v_alloc->map[i] = 0;
781 devc->v_alloc->alloc_times[i] = 0;
784 devc->cmask = 0x00; /*
785 * Just 2 OP mode
787 if (devc->model == 2)
788 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
789 return 0;
792 static void opl3_close(int dev)
794 devc->busy = 0;
795 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
797 devc->fm_info.nr_drums = 0;
798 devc->fm_info.perc_mode = 0;
800 opl3_reset(dev);
801 MOD_DEC_USE_COUNT;
804 static void opl3_hw_control(int dev, unsigned char *event)
808 static int opl3_load_patch(int dev, int format, const char *addr,
809 int offs, int count, int pmgr_flag)
811 struct sbi_instrument ins;
813 if (count <sizeof(ins))
815 printk(KERN_WARNING "FM Error: Patch record too short\n");
816 return -EINVAL;
819 if(copy_from_user(&((char *) &ins)[offs], &(addr)[offs], sizeof(ins) - offs))
820 return -EFAULT;
822 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
824 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
825 return -EINVAL;
827 ins.key = format;
829 return store_instr(ins.channel, &ins);
832 static void opl3_panning(int dev, int voice, int value)
834 devc->voc[voice].panning = value;
837 static void opl3_volume_method(int dev, int mode)
841 #define SET_VIBRATO(cell) { \
842 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
843 if (pressure > 110) \
844 tmp |= 0x40; /* Vibrato on */ \
845 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
847 static void opl3_aftertouch(int dev, int voice, int pressure)
849 int tmp;
850 struct sbi_instrument *instr;
851 struct physical_voice_info *map;
853 if (voice < 0 || voice >= devc->nr_voice)
854 return;
856 map = &pv_map[devc->lv_map[voice]];
858 DEB(printk("Aftertouch %d\n", voice));
860 if (map->voice_mode == 0)
861 return;
864 * Adjust the amount of vibrato depending the pressure
867 instr = devc->act_i[voice];
869 if (!instr)
870 instr = &devc->i_map[0];
872 if (devc->voc[voice].mode == 4)
874 int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
876 switch (connection)
878 case 0:
879 SET_VIBRATO(4);
880 break;
882 case 1:
883 SET_VIBRATO(2);
884 SET_VIBRATO(4);
885 break;
887 case 2:
888 SET_VIBRATO(1);
889 SET_VIBRATO(4);
890 break;
892 case 3:
893 SET_VIBRATO(1);
894 SET_VIBRATO(3);
895 SET_VIBRATO(4);
896 break;
900 * Not implemented yet
903 else
905 SET_VIBRATO(1);
907 if ((instr->operators[10] & 0x01)) /*
908 * Additive synthesis
910 SET_VIBRATO(2);
914 #undef SET_VIBRATO
916 static void bend_pitch(int dev, int voice, int value)
918 unsigned char data;
919 int block, fnum, freq;
920 struct physical_voice_info *map;
922 map = &pv_map[devc->lv_map[voice]];
924 if (map->voice_mode == 0)
925 return;
927 devc->voc[voice].bender = value;
928 if (!value)
929 return;
930 if (!(devc->voc[voice].keyon_byte & 0x20))
931 return; /*
932 * Not keyed on
935 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
936 devc->voc[voice].current_freq = freq;
938 freq_to_fnum(freq, &block, &fnum);
940 data = fnum & 0xff; /*
941 * Least significant bits of fnumber
943 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
945 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
946 devc->voc[voice].keyon_byte = data;
947 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
950 static void opl3_controller (int dev, int voice, int ctrl_num, int value)
952 if (voice < 0 || voice >= devc->nr_voice)
953 return;
955 switch (ctrl_num)
957 case CTRL_PITCH_BENDER:
958 bend_pitch(dev, voice, value);
959 break;
961 case CTRL_PITCH_BENDER_RANGE:
962 devc->voc[voice].bender_range = value;
963 break;
965 case CTL_MAIN_VOLUME:
966 devc->voc[voice].volume = value / 128;
967 break;
969 case CTL_PAN:
970 devc->voc[voice].panning = (value * 2) - 128;
971 break;
975 static void opl3_bender(int dev, int voice, int value)
977 if (voice < 0 || voice >= devc->nr_voice)
978 return;
980 bend_pitch(dev, voice, value - 8192);
983 static int opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info *alloc)
985 int i, p, best, first, avail, best_time = 0x7fffffff;
986 struct sbi_instrument *instr;
987 int is4op;
988 int instr_no;
990 if (chn < 0 || chn > 15)
991 instr_no = 0;
992 else
993 instr_no = devc->chn_info[chn].pgm_num;
995 instr = &devc->i_map[instr_no];
996 if (instr->channel < 0 || /* Instrument not loaded */
997 devc->nr_voice != 12) /* Not in 4 OP mode */
998 is4op = 0;
999 else if (devc->nr_voice == 12) /* 4 OP mode */
1000 is4op = (instr->key == OPL3_PATCH);
1001 else
1002 is4op = 0;
1004 if (is4op)
1006 first = p = 0;
1007 avail = 6;
1009 else
1011 if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP only' operators first */
1012 first = p = 6;
1013 else
1014 first = p = 0;
1015 avail = devc->nr_voice;
1019 * Now try to find a free voice
1021 best = first;
1023 for (i = 0; i < avail; i++)
1025 if (alloc->map[p] == 0)
1027 return p;
1029 if (alloc->alloc_times[p] < best_time) /* Find oldest playing note */
1031 best_time = alloc->alloc_times[p];
1032 best = p;
1034 p = (p + 1) % avail;
1038 * Insert some kind of priority mechanism here.
1041 if (best < 0)
1042 best = 0;
1043 if (best > devc->nr_voice)
1044 best -= devc->nr_voice;
1046 return best; /* All devc->voc in use. Select the first one. */
1049 static void opl3_setup_voice(int dev, int voice, int chn)
1051 struct channel_info *info =
1052 &synth_devs[dev]->chn_info[chn];
1054 opl3_set_instr(dev, voice, info->pgm_num);
1056 devc->voc[voice].bender = 0;
1057 devc->voc[voice].bender_range = info->bender_range;
1058 devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
1059 devc->voc[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
1062 static struct synth_operations opl3_operations =
1064 "OPL",
1065 NULL,
1067 SYNTH_TYPE_FM,
1068 FM_TYPE_ADLIB,
1069 opl3_open,
1070 opl3_close,
1071 opl3_ioctl,
1072 opl3_kill_note,
1073 opl3_start_note,
1074 opl3_set_instr,
1075 opl3_reset,
1076 opl3_hw_control,
1077 opl3_load_patch,
1078 opl3_aftertouch,
1079 opl3_controller,
1080 opl3_panning,
1081 opl3_volume_method,
1082 opl3_bender,
1083 opl3_alloc_voice,
1084 opl3_setup_voice
1087 int opl3_init(int ioaddr, int *osp)
1089 int i;
1090 int me;
1092 if (devc == NULL)
1094 printk(KERN_ERR "opl3: Device control structure not initialized.\n");
1095 return -1;
1098 if ((me = sound_alloc_synthdev()) == -1)
1100 printk(KERN_WARNING "opl3: Too many synthesizers\n");
1101 return -1;
1104 memset((char *) devc, 0x00, sizeof(*devc));
1105 devc->osp = osp;
1106 devc->base = ioaddr;
1108 devc->nr_voice = 9;
1109 strcpy(devc->fm_info.name, "OPL2");
1111 devc->fm_info.device = 0;
1112 devc->fm_info.synth_type = SYNTH_TYPE_FM;
1113 devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
1114 devc->fm_info.perc_mode = 0;
1115 devc->fm_info.nr_voices = 9;
1116 devc->fm_info.nr_drums = 0;
1117 devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
1118 devc->fm_info.capabilities = 0;
1119 devc->left_io = ioaddr;
1120 devc->right_io = ioaddr + 2;
1122 if (detected_model <= 2)
1123 devc->model = 1;
1124 else
1126 devc->model = 2;
1127 if (detected_model == 4)
1128 devc->is_opl4 = 1;
1131 opl3_operations.info = &devc->fm_info;
1133 synth_devs[me] = &opl3_operations;
1134 sequencer_init();
1135 devc->v_alloc = &opl3_operations.alloc;
1136 devc->chn_info = &opl3_operations.chn_info[0];
1138 if (devc->model == 2)
1140 if (devc->is_opl4)
1141 strcpy(devc->fm_info.name, "Yamaha OPL4/OPL3 FM");
1142 else
1143 strcpy(devc->fm_info.name, "Yamaha OPL3");
1145 devc->v_alloc->max_voice = devc->nr_voice = 18;
1146 devc->fm_info.nr_drums = 0;
1147 devc->fm_info.synth_subtype = FM_TYPE_OPL3;
1148 devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
1150 for (i = 0; i < 18; i++)
1152 if (pv_map[i].ioaddr == USE_LEFT)
1153 pv_map[i].ioaddr = devc->left_io;
1154 else
1155 pv_map[i].ioaddr = devc->right_io;
1157 opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
1158 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
1160 else
1162 strcpy(devc->fm_info.name, "Yamaha OPL2");
1163 devc->v_alloc->max_voice = devc->nr_voice = 9;
1164 devc->fm_info.nr_drums = 0;
1166 for (i = 0; i < 18; i++)
1167 pv_map[i].ioaddr = devc->left_io;
1169 conf_printf2(devc->fm_info.name, ioaddr, 0, -1, -1);
1171 for (i = 0; i < SBFM_MAXINSTR; i++)
1172 devc->i_map[i].channel = -1;
1174 return me;
1177 EXPORT_SYMBOL(opl3_init);
1178 EXPORT_SYMBOL(opl3_detect);
1180 static int me;
1182 static int io = -1;
1184 MODULE_PARM(io, "i");
1186 static int __init init_opl3 (void)
1188 printk(KERN_INFO "YM3812 and OPL-3 driver Copyright (C) by Hannu Savolainen, Rob Hooft 1993-1996\n");
1190 if (io != -1) /* User loading pure OPL3 module */
1192 if (check_region(io, 4))
1194 printk(KERN_WARNING "opl3: I/O port 0x%x already in use\n", io);
1195 return 0;
1197 if (!opl3_detect(io, NULL))
1199 return -ENODEV;
1201 me = opl3_init(io, NULL);
1202 request_region(io, 4, devc->fm_info.name);
1205 SOUND_LOCK;
1206 return 0;
1209 static void __exit cleanup_opl3(void)
1211 if (devc && io != -1)
1213 if(devc->base)
1214 release_region(devc->base,4);
1215 kfree(devc);
1216 devc = NULL;
1217 sound_unload_synthdev(me);
1219 SOUND_LOCK_END;
1222 module_init(init_opl3);
1223 module_exit(cleanup_opl3);
1225 #ifndef MODULE
1226 static int __init setup_opl3(char *str)
1228 /* io */
1229 int ints[2];
1231 str = get_options(str, ARRAY_SIZE(ints), ints);
1233 io = ints[1];
1235 return 1;
1238 __setup("opl3=", setup_opl3);
1239 #endif