27 #include <linux/slab.h>
28 #include <linux/module.h>
80 static int detected_model;
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)
91 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17
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];
112 static int opl3_ioctl(
int dev,
unsigned int cmd,
void __user *
arg)
118 printk(
KERN_WARNING "Warning: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
125 return store_instr(
ins.channel, &
ins);
137 if (devc->
model == 2)
146 static int opl3_detect(
int ioaddr)
172 printk(
KERN_ERR "opl3: Can't allocate memory for the device control "
192 signature = stat1 =
inb(ioaddr);
194 if (signature != 0x00 && signature != 0x06 && signature != 0x02 &&
201 if (signature == 0x06)
205 else if (signature == 0x00 || signature == 0x0f)
220 if ((tmp =
inb(ioaddr)) == 0x02)
229 outb((0x02), ioaddr - 8);
231 tmp =
inb(ioaddr - 7);
237 outb((0xF8), ioaddr - 8);
239 outb((0x1B), ioaddr - 7);
250 for (i = 0; i < 9; i++)
268 static int opl3_kill_note (
int devno,
int voice,
int note,
int velocity)
277 map = &pv_map[devc->
lv_map[voice]];
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;
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
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)
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;
380 map = &pv_map[devc->
lv_map[voice]];
381 instr = devc->
act_i[voice];
384 instr = &devc->
i_map[0];
389 if (devc->
voc[voice].mode == 0)
392 if (devc->
voc[voice].mode == 2)
398 calc_vol(&vol1, volume, main_vol);
399 calc_vol(&vol2, volume, main_vol);
403 calc_vol(&vol2, volume, main_vol);
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);
458 static int opl3_start_note (
int dev,
int voice,
int note,
int volume)
460 unsigned char data, fpc;
468 map = &pv_map[devc->
lv_map[voice]];
469 pan = devc->
voc[voice].panning;
478 set_voice_volume(voice, volume, devc->
voc[voice].volume);
507 instr = devc->
act_i[voice];
510 instr = &devc->
i_map[0];
514 printk(
KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice);
537 devc->
cmask &= ~(1 << voice_shift);
541 devc->
cmask |= (1 << voice_shift);
646 set_voice_volume(voice, volume, devc->
voc[voice].volume);
656 devc->
voc[voice].current_freq =
freq;
658 freq_to_fnum(freq, &block, &fnum);
669 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
670 devc->
voc[voice].keyon_byte =
data;
678 static void freq_to_fnum (
int freq,
int *block,
int *fnum)
715 *fnum = freq * (1 << (20 - octave)) / 49716;
719 static void opl3_command (
int io_addr,
unsigned int addr,
unsigned int val)
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++)
754 opl3_command(pv_map[devc->
lv_map[i]].ioaddr,
757 opl3_command(pv_map[devc->
lv_map[i]].ioaddr,
760 if (pv_map[devc->
lv_map[i]].voice_mode == 4)
762 opl3_command(pv_map[devc->
lv_map[i]].ioaddr,
765 opl3_command(pv_map[devc->
lv_map[i]].ioaddr,
769 opl3_kill_note(devno, i, 0, 64);
772 if (devc->
model == 2)
776 for (i = 0; i < 18; i++)
777 pv_map[i].voice_mode = 2;
782 static int opl3_open(
int dev,
int mode)
793 for (i = 0; i < 18; i++)
802 if (devc->
model == 2)
807 static void opl3_close(
int dev)
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)
827 if (count <
sizeof(
ins))
843 return store_instr(
ins.channel, &
ins);
846 static void opl3_panning(
int dev,
int voice,
int value)
855 static void opl3_volume_method(
int dev,
int mode)
859 #define SET_VIBRATO(cell) { \
860 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
861 if (pressure > 110) \
863 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
865 static void opl3_aftertouch(
int dev,
int voice,
int pressure)
874 map = &pv_map[devc->
lv_map[voice]];
885 instr = devc->
act_i[voice];
888 instr = &devc->
i_map[0];
890 if (devc->
voc[voice].mode == 4)
934 static void bend_pitch(
int dev,
int voice,
int value)
940 map = &pv_map[devc->
lv_map[voice]];
948 if (!(devc->
voc[voice].keyon_byte & 0x20))
954 devc->
voc[voice].current_freq =
freq;
956 freq_to_fnum(freq, &block, &fnum);
963 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
964 devc->
voc[voice].keyon_byte =
data;
968 static void opl3_controller (
int dev,
int voice,
int ctrl_num,
int value)
976 bend_pitch(dev, voice, value);
980 devc->
voc[voice].bender_range =
value;
984 devc->
voc[voice].volume = value / 128;
988 devc->
voc[voice].panning = (value * 2) - 128;
993 static void opl3_bender(
int dev,
int voice,
int value)
998 bend_pitch(dev, voice, value - 8192);
1008 if (chn < 0 || chn > 15)
1013 instr = &devc->
i_map[instr_no];
1041 for (i = 0; i <
avail; i++)
1043 if (alloc->
map[p] == 0)
1052 p = (p + 1) % avail;
1067 static void opl3_setup_voice(
int dev,
int voice,
int chn)
1074 if (chn < 0 || chn > 15)
1079 opl3_set_instr(dev, voice, info->
pgm_num);
1081 devc->
voc[voice].bender = 0;
1096 .close = opl3_close,
1097 .ioctl = opl3_ioctl,
1098 .kill_note = opl3_kill_note,
1099 .start_note = opl3_start_note,
1100 .set_instr = opl3_set_instr,
1101 .reset = opl3_reset,
1102 .hw_control = opl3_hw_control,
1103 .load_patch = opl3_load_patch,
1104 .aftertouch = opl3_aftertouch,
1105 .controller = opl3_controller,
1106 .panning = opl3_panning,
1107 .volume_method = opl3_volume_method,
1108 .bender = opl3_bender,
1109 .alloc_voice = opl3_alloc_voice,
1110 .setup_voice = opl3_setup_voice
1113 static int opl3_init(
int ioaddr,
struct module *
owner)
1120 printk(
KERN_ERR "opl3: Device control structure not initialized.\n");
1139 devc->
fm_info.capabilities = 0;
1143 if (detected_model <= 2)
1148 if (detected_model == 4)
1163 if (devc->
model == 2)
1175 for (i = 0; i < 18; i++)
1191 for (i = 0; i < 18; i++)
1192 pv_map[i].ioaddr = devc->
left_io;
1197 devc->
i_map[i].channel = -1;
1208 static int __init init_opl3 (
void)
1210 printk(
KERN_INFO "YM3812 and OPL-3 driver Copyright (C) by Hannu Savolainen, Rob Hooft 1993-1996\n");
1214 if (!opl3_detect(io))
1225 static void __exit cleanup_opl3(
void)
1227 if (devc && io != -1)