1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/module.h>
29#include <linux/delay.h>
30
31
32
33
34
35
36#include "sound_config.h"
37
38#include "opl3_hw.h"
39
40#define MAX_VOICE 18
41#define OFFS_4OP 11
42
43struct voice_info
44{
45 unsigned char keyon_byte;
46 long bender;
47 long bender_range;
48 unsigned long orig_freq;
49 unsigned long current_freq;
50 int volume;
51 int mode;
52 int panning;
53};
54
55typedef struct opl_devinfo
56{
57 int base;
58 int left_io, right_io;
59 int nr_voice;
60 int lv_map[MAX_VOICE];
61
62 struct voice_info voc[MAX_VOICE];
63 struct voice_alloc_info *v_alloc;
64 struct channel_info *chn_info;
65
66 struct sbi_instrument i_map[SBFM_MAXINSTR];
67 struct sbi_instrument *act_i[MAX_VOICE];
68
69 struct synth_info fm_info;
70
71 int busy;
72 int model;
73 unsigned char cmask;
74
75 int is_opl4;
76} opl_devinfo;
77
78static struct opl_devinfo *devc = NULL;
79
80static int detected_model;
81
82static int store_instr(int instr_no, struct sbi_instrument *instr);
83static void freq_to_fnum(int freq, int *block, int *fnum);
84static void opl3_command(int io_addr, unsigned int addr, unsigned int val);
85static int opl3_kill_note(int dev, int voice, int note, int velocity);
86
87static void enter_4op_mode(void)
88{
89 int i;
90 static int v4op[MAX_VOICE] = {
91 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17
92 };
93
94 devc->cmask = 0x3f;
95 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f);
96
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;
101
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;
106
107 for (i = 0; i < 12; i++)
108 devc->lv_map[i] = v4op[i];
109 devc->v_alloc->max_voice = devc->nr_voice = 12;
110}
111
112static int opl3_ioctl(int dev, unsigned int cmd, void __user * arg)
113{
114 struct sbi_instrument ins;
115
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;
124 }
125 return store_instr(ins.channel, &ins);
126
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;
132
133 case SNDCTL_SYNTH_MEMAVL:
134 return 0x7fffffff;
135
136 case SNDCTL_FM_4OP_ENABLE:
137 if (devc->model == 2)
138 enter_4op_mode();
139 return 0;
140
141 default:
142 return -EINVAL;
143 }
144}
145
146static int opl3_detect(int ioaddr)
147{
148
149
150
151
152
153
154
155
156
157
158
159 unsigned char stat1, signature;
160 int i;
161
162 if (devc != NULL)
163 {
164 printk(KERN_ERR "opl3: Only one OPL3 supported.\n");
165 return 0;
166 }
167
168 devc = kzalloc(sizeof(*devc), GFP_KERNEL);
169
170 if (devc == NULL)
171 {
172 printk(KERN_ERR "opl3: Can't allocate memory for the device control "
173 "structure \n ");
174 return 0;
175 }
176
177 strcpy(devc->fm_info.name, "OPL2");
178
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);
181 goto cleanup_devc;
182 }
183
184 devc->base = ioaddr;
185
186
187 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
188
189
190 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
191
192 signature = stat1 = inb(ioaddr);
193
194 if (signature != 0x00 && signature != 0x06 && signature != 0x02 &&
195 signature != 0x0f)
196 {
197 MDB(printk(KERN_INFO "OPL3 not detected %x\n", signature));
198 goto cleanup_region;
199 }
200
201 if (signature == 0x06)
202 {
203 detected_model = 2;
204 }
205 else if (signature == 0x00 || signature == 0x0f)
206 {
207 unsigned char tmp;
208
209 detected_model = 3;
210
211
212
213
214
215
216
217 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
218 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
219
220 if ((tmp = inb(ioaddr)) == 0x02)
221 {
222 detected_model = 4;
223 }
224
225 if (request_region(ioaddr - 8, 2, "OPL4"))
226 {
227 int tmp;
228
229 outb((0x02), ioaddr - 8);
230 udelay(10);
231 tmp = inb(ioaddr - 7);
232 udelay(10);
233
234 if (tmp == 0x20)
235 {
236 detected_model = 4;
237 outb((0xF8), ioaddr - 8);
238 udelay(10);
239 outb((0x1B), ioaddr - 7);
240 udelay(10);
241 }
242 else
243 {
244 release_region(ioaddr - 8, 2);
245 detected_model = 3;
246 }
247 }
248 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
249 }
250 for (i = 0; i < 9; i++)
251 opl3_command(ioaddr, KEYON_BLOCK + i, 0);
252
253
254
255 opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
256 opl3_command(ioaddr, PERCOSSION_REGISTER, 0x00);
257
258
259 return 1;
260cleanup_region:
261 release_region(ioaddr, 4);
262cleanup_devc:
263 kfree(devc);
264 devc = NULL;
265 return 0;
266}
267
268static int opl3_kill_note (int devno, int voice, int note, int velocity)
269{
270 struct physical_voice_info *map;
271
272 if (voice < 0 || voice >= devc->nr_voice)
273 return 0;
274
275 devc->v_alloc->map[voice] = 0;
276
277 map = &pv_map[devc->lv_map[voice]];
278 DEB(printk("Kill note %d\n", voice));
279
280 if (map->voice_mode == 0)
281 return 0;
282
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;
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;
292 return 0;
293}
294
295#define HIHAT 0
296#define CYMBAL 1
297#define TOMTOM 2
298#define SNARE 3
299#define BDRUM 4
300#define UNDEFINED TOMTOM
301#define DEFAULT TOMTOM
302
303static int store_instr(int instr_no, struct sbi_instrument *instr)
304{
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));
308 return 0;
309}
310
311static int opl3_set_instr (int dev, int voice, int instr_no)
312{
313 if (voice < 0 || voice >= devc->nr_voice)
314 return 0;
315 if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
316 instr_no = 0;
317
318 devc->act_i[voice] = &devc->i_map[instr_no];
319 return 0;
320}
321
322
323
324
325
326
327
328
329
330
331
332static char fm_volume_table[128] =
333{
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
350};
351
352static void calc_vol(unsigned char *regbyte, int volume, int main_vol)
353{
354 int level = (~*regbyte & 0x3f);
355
356 if (main_vol > 127)
357 main_vol = 127;
358 volume = (volume * main_vol) / 127;
359
360 if (level)
361 level += fm_volume_table[volume];
362
363 if (level > 0x3f)
364 level = 0x3f;
365 if (level < 0)
366 level = 0;
367
368 *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
369}
370
371static void set_voice_volume(int voice, int volume, int main_vol)
372{
373 unsigned char vol1, vol2, vol3, vol4;
374 struct sbi_instrument *instr;
375 struct physical_voice_info *map;
376
377 if (voice < 0 || voice >= devc->nr_voice)
378 return;
379
380 map = &pv_map[devc->lv_map[voice]];
381 instr = devc->act_i[voice];
382
383 if (!instr)
384 instr = &devc->i_map[0];
385
386 if (instr->channel < 0)
387 return;
388
389 if (devc->voc[voice].mode == 0)
390 return;
391
392 if (devc->voc[voice].mode == 2)
393 {
394 vol1 = instr->operators[2];
395 vol2 = instr->operators[3];
396 if ((instr->operators[10] & 0x01))
397 {
398 calc_vol(&vol1, volume, main_vol);
399 calc_vol(&vol2, volume, main_vol);
400 }
401 else
402 {
403 calc_vol(&vol2, volume, main_vol);
404 }
405 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
406 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
407 }
408 else
409 {
410
411
412 int connection;
413
414 vol1 = instr->operators[2];
415 vol2 = instr->operators[3];
416 vol3 = instr->operators[OFFS_4OP + 2];
417 vol4 = instr->operators[OFFS_4OP + 3];
418
419
420
421
422
423
424 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
425
426 switch (connection)
427 {
428 case 0:
429 calc_vol(&vol4, volume, main_vol);
430 break;
431
432 case 1:
433 calc_vol(&vol2, volume, main_vol);
434 calc_vol(&vol4, volume, main_vol);
435 break;
436
437 case 2:
438 calc_vol(&vol1, volume, main_vol);
439 calc_vol(&vol4, volume, main_vol);
440 break;
441
442 case 3:
443 calc_vol(&vol1, volume, main_vol);
444 calc_vol(&vol3, volume, main_vol);
445 calc_vol(&vol4, volume, main_vol);
446 break;
447
448 default:
449 ;
450 }
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);
455 }
456}
457
458static int opl3_start_note (int dev, int voice, int note, int volume)
459{
460 unsigned char data, fpc;
461 int block, fnum, freq, voice_mode, pan;
462 struct sbi_instrument *instr;
463 struct physical_voice_info *map;
464
465 if (voice < 0 || voice >= devc->nr_voice)
466 return 0;
467
468 map = &pv_map[devc->lv_map[voice]];
469 pan = devc->voc[voice].panning;
470
471 if (map->voice_mode == 0)
472 return 0;
473
474 if (note == 255)
475
476
477 {
478 set_voice_volume(voice, volume, devc->voc[voice].volume);
479 return 0;
480 }
481
482
483
484
485
486 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff);
487
488
489
490
491 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff);
492
493
494
495
496 if (map->voice_mode == 4)
497 {
498 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
499 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
500 }
501
502 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);
503
504
505
506
507 instr = devc->act_i[voice];
508
509 if (!instr)
510 instr = &devc->i_map[0];
511
512 if (instr->channel < 0)
513 {
514 printk(KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice);
515 return 0;
516 }
517
518 if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
519 return 0;
520
521
522
523 voice_mode = map->voice_mode;
524
525 if (voice_mode == 4)
526 {
527 int voice_shift;
528
529 voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
530 voice_shift += map->voice_num;
531
532 if (instr->key != OPL3_PATCH)
533
534
535 {
536 voice_mode = 2;
537 devc->cmask &= ~(1 << voice_shift);
538 }
539 else
540 {
541 devc->cmask |= (1 << voice_shift);
542 }
543
544 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
545 }
546
547
548
549
550
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]);
553
554
555
556
557
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]);
560
561
562
563
564
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]);
567
568
569
570
571
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]);
574
575
576
577
578
579 fpc = instr->operators[10];
580
581 if (pan != 0xffff)
582 {
583 fpc &= ~STEREO_BITS;
584 if (pan < -64)
585 fpc |= VOICE_TO_LEFT;
586 else
587 if (pan > 64)
588 fpc |= VOICE_TO_RIGHT;
589 else
590 fpc |= (VOICE_TO_LEFT | VOICE_TO_RIGHT);
591 }
592
593 if (!(fpc & 0x30))
594 fpc |= 0x30;
595
596
597 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
598
599
600
601
602
603 if (voice_mode == 4)
604 {
605
606
607
608
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]);
611
612
613
614
615
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]);
618
619
620
621
622
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]);
625
626
627
628
629
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]);
632
633
634
635
636
637 fpc = instr->operators[OFFS_4OP + 10];
638 if (!(fpc & 0x30))
639 fpc |= 0x30;
640
641
642 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
643 }
644
645 devc->voc[voice].mode = voice_mode;
646 set_voice_volume(voice, volume, devc->voc[voice].volume);
647
648 freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000;
649
650
651
652
653
654
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;
657
658 freq_to_fnum(freq, &block, &fnum);
659
660
661
662
663
664 data = fnum & 0xff;
665
666
667 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
668
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);
672 if (voice_mode == 4)
673 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
674
675 return 0;
676}
677
678static void freq_to_fnum (int freq, int *block, int *fnum)
679{
680 int f, octave;
681
682
683
684
685
686
687
688
689 f = freq;
690
691 octave = 5;
692
693 if (f == 0)
694 octave = 0;
695 else if (f < 261)
696 {
697 while (f < 261)
698 {
699 octave--;
700 f <<= 1;
701 }
702 }
703 else if (f > 493)
704 {
705 while (f > 493)
706 {
707 octave++;
708 f >>= 1;
709 }
710 }
711
712 if (octave > 7)
713 octave = 7;
714
715 *fnum = freq * (1 << (20 - octave)) / 49716;
716 *block = octave;
717}
718
719static void opl3_command (int io_addr, unsigned int addr, unsigned int val)
720{
721 int i;
722
723
724
725
726
727
728 outb(((unsigned char) (addr & 0xff)), io_addr);
729
730 if (devc->model != 2)
731 udelay(10);
732 else
733 for (i = 0; i < 2; i++)
734 inb(io_addr);
735
736 outb(((unsigned char) (val & 0xff)), io_addr + 1);
737
738 if (devc->model != 2)
739 udelay(30);
740 else
741 for (i = 0; i < 2; i++)
742 inb(io_addr);
743}
744
745static void opl3_reset(int devno)
746{
747 int i;
748
749 for (i = 0; i < 18; i++)
750 devc->lv_map[i] = i;
751
752 for (i = 0; i < devc->nr_voice; i++)
753 {
754 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
755 KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
756
757 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
758 KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
759
760 if (pv_map[devc->lv_map[i]].voice_mode == 4)
761 {
762 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
763 KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
764
765 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
766 KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
767 }
768
769 opl3_kill_note(devno, i, 0, 64);
770 }
771
772 if (devc->model == 2)
773 {
774 devc->v_alloc->max_voice = devc->nr_voice = 18;
775
776 for (i = 0; i < 18; i++)
777 pv_map[i].voice_mode = 2;
778
779 }
780}
781
782static int opl3_open(int dev, int mode)
783{
784 int i;
785
786 if (devc->busy)
787 return -EBUSY;
788 devc->busy = 1;
789
790 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
791 devc->v_alloc->timestamp = 0;
792
793 for (i = 0; i < 18; i++)
794 {
795 devc->v_alloc->map[i] = 0;
796 devc->v_alloc->alloc_times[i] = 0;
797 }
798
799 devc->cmask = 0x00;
800
801
802 if (devc->model == 2)
803 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
804 return 0;
805}
806
807static void opl3_close(int dev)
808{
809 devc->busy = 0;
810 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
811
812 devc->fm_info.nr_drums = 0;
813 devc->fm_info.perc_mode = 0;
814
815 opl3_reset(dev);
816}
817
818static void opl3_hw_control(int dev, unsigned char *event)
819{
820}
821
822static int opl3_load_patch(int dev, int format, const char __user *addr,
823 int count, int pmgr_flag)
824{
825 struct sbi_instrument ins;
826
827 if (count <sizeof(ins))
828 {
829 printk(KERN_WARNING "FM Error: Patch record too short\n");
830 return -EINVAL;
831 }
832
833 if (copy_from_user(&ins, addr, sizeof(ins)))
834 return -EFAULT;
835
836 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
837 {
838 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
839 return -EINVAL;
840 }
841 ins.key = format;
842
843 return store_instr(ins.channel, &ins);
844}
845
846static void opl3_panning(int dev, int voice, int value)
847{
848
849 if (voice < 0 || voice >= devc->nr_voice)
850 return;
851
852 devc->voc[voice].panning = value;
853}
854
855static void opl3_volume_method(int dev, int mode)
856{
857}
858
859#define SET_VIBRATO(cell) { \
860 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
861 if (pressure > 110) \
862 tmp |= 0x40; \
863 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
864
865static void opl3_aftertouch(int dev, int voice, int pressure)
866{
867 int tmp;
868 struct sbi_instrument *instr;
869 struct physical_voice_info *map;
870
871 if (voice < 0 || voice >= devc->nr_voice)
872 return;
873
874 map = &pv_map[devc->lv_map[voice]];
875
876 DEB(printk("Aftertouch %d\n", voice));
877
878 if (map->voice_mode == 0)
879 return;
880
881
882
883
884
885 instr = devc->act_i[voice];
886
887 if (!instr)
888 instr = &devc->i_map[0];
889
890 if (devc->voc[voice].mode == 4)
891 {
892 int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
893
894 switch (connection)
895 {
896 case 0:
897 SET_VIBRATO(4);
898 break;
899
900 case 1:
901 SET_VIBRATO(2);
902 SET_VIBRATO(4);
903 break;
904
905 case 2:
906 SET_VIBRATO(1);
907 SET_VIBRATO(4);
908 break;
909
910 case 3:
911 SET_VIBRATO(1);
912 SET_VIBRATO(3);
913 SET_VIBRATO(4);
914 break;
915
916 }
917
918
919
920 }
921 else
922 {
923 SET_VIBRATO(1);
924
925 if ((instr->operators[10] & 0x01))
926
927
928 SET_VIBRATO(2);
929 }
930}
931
932#undef SET_VIBRATO
933
934static void bend_pitch(int dev, int voice, int value)
935{
936 unsigned char data;
937 int block, fnum, freq;
938 struct physical_voice_info *map;
939
940 map = &pv_map[devc->lv_map[voice]];
941
942 if (map->voice_mode == 0)
943 return;
944
945 devc->voc[voice].bender = value;
946 if (!value)
947 return;
948 if (!(devc->voc[voice].keyon_byte & 0x20))
949 return;
950
951
952
953 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
954 devc->voc[voice].current_freq = freq;
955
956 freq_to_fnum(freq, &block, &fnum);
957
958 data = fnum & 0xff;
959
960
961 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
962
963 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
964 devc->voc[voice].keyon_byte = data;
965 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
966}
967
968static void opl3_controller (int dev, int voice, int ctrl_num, int value)
969{
970 if (voice < 0 || voice >= devc->nr_voice)
971 return;
972
973 switch (ctrl_num)
974 {
975 case CTRL_PITCH_BENDER:
976 bend_pitch(dev, voice, value);
977 break;
978
979 case CTRL_PITCH_BENDER_RANGE:
980 devc->voc[voice].bender_range = value;
981 break;
982
983 case CTL_MAIN_VOLUME:
984 devc->voc[voice].volume = value / 128;
985 break;
986
987 case CTL_PAN:
988 devc->voc[voice].panning = (value * 2) - 128;
989 break;
990 }
991}
992
993static void opl3_bender(int dev, int voice, int value)
994{
995 if (voice < 0 || voice >= devc->nr_voice)
996 return;
997
998 bend_pitch(dev, voice, value - 8192);
999}
1000
1001static int opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info *alloc)
1002{
1003 int i, p, best, first, avail, best_time = 0x7fffffff;
1004 struct sbi_instrument *instr;
1005 int is4op;
1006 int instr_no;
1007
1008 if (chn < 0 || chn > 15)
1009 instr_no = 0;
1010 else
1011 instr_no = devc->chn_info[chn].pgm_num;
1012
1013 instr = &devc->i_map[instr_no];
1014 if (instr->channel < 0 ||
1015 devc->nr_voice != 12)
1016 is4op = 0;
1017 else if (devc->nr_voice == 12)
1018 is4op = (instr->key == OPL3_PATCH);
1019 else
1020 is4op = 0;
1021
1022 if (is4op)
1023 {
1024 first = p = 0;
1025 avail = 6;
1026 }
1027 else
1028 {
1029 if (devc->nr_voice == 12)
1030 first = p = 6;
1031 else
1032 first = p = 0;
1033 avail = devc->nr_voice;
1034 }
1035
1036
1037
1038
1039 best = first;
1040
1041 for (i = 0; i < avail; i++)
1042 {
1043 if (alloc->map[p] == 0)
1044 {
1045 return p;
1046 }
1047 if (alloc->alloc_times[p] < best_time)
1048 {
1049 best_time = alloc->alloc_times[p];
1050 best = p;
1051 }
1052 p = (p + 1) % avail;
1053 }
1054
1055
1056
1057
1058
1059 if (best < 0)
1060 best = 0;
1061 if (best > devc->nr_voice)
1062 best -= devc->nr_voice;
1063
1064 return best;
1065}
1066
1067static void opl3_setup_voice(int dev, int voice, int chn)
1068{
1069 struct channel_info *info;
1070
1071 if (voice < 0 || voice >= devc->nr_voice)
1072 return;
1073
1074 if (chn < 0 || chn > 15)
1075 return;
1076
1077 info = &synth_devs[dev]->chn_info[chn];
1078
1079 opl3_set_instr(dev, voice, info->pgm_num);
1080
1081 devc->voc[voice].bender = 0;
1082 devc->voc[voice].bender_range = info->bender_range;
1083 devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
1084 devc->voc[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
1085}
1086
1087static struct synth_operations opl3_operations =
1088{
1089 .owner = THIS_MODULE,
1090 .id = "OPL",
1091 .info = NULL,
1092 .midi_dev = 0,
1093 .synth_type = SYNTH_TYPE_FM,
1094 .synth_subtype = FM_TYPE_ADLIB,
1095 .open = opl3_open,
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
1111};
1112
1113static int opl3_init(int ioaddr, struct module *owner)
1114{
1115 int i;
1116 int me;
1117
1118 if (devc == NULL)
1119 {
1120 printk(KERN_ERR "opl3: Device control structure not initialized.\n");
1121 return -1;
1122 }
1123
1124 if ((me = sound_alloc_synthdev()) == -1)
1125 {
1126 printk(KERN_WARNING "opl3: Too many synthesizers\n");
1127 return -1;
1128 }
1129
1130 devc->nr_voice = 9;
1131
1132 devc->fm_info.device = 0;
1133 devc->fm_info.synth_type = SYNTH_TYPE_FM;
1134 devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
1135 devc->fm_info.perc_mode = 0;
1136 devc->fm_info.nr_voices = 9;
1137 devc->fm_info.nr_drums = 0;
1138 devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
1139 devc->fm_info.capabilities = 0;
1140 devc->left_io = ioaddr;
1141 devc->right_io = ioaddr + 2;
1142
1143 if (detected_model <= 2)
1144 devc->model = 1;
1145 else
1146 {
1147 devc->model = 2;
1148 if (detected_model == 4)
1149 devc->is_opl4 = 1;
1150 }
1151
1152 opl3_operations.info = &devc->fm_info;
1153
1154 synth_devs[me] = &opl3_operations;
1155
1156 if (owner)
1157 synth_devs[me]->owner = owner;
1158
1159 sequencer_init();
1160 devc->v_alloc = &opl3_operations.alloc;
1161 devc->chn_info = &opl3_operations.chn_info[0];
1162
1163 if (devc->model == 2)
1164 {
1165 if (devc->is_opl4)
1166 strcpy(devc->fm_info.name, "Yamaha OPL4/OPL3 FM");
1167 else
1168 strcpy(devc->fm_info.name, "Yamaha OPL3");
1169
1170 devc->v_alloc->max_voice = devc->nr_voice = 18;
1171 devc->fm_info.nr_drums = 0;
1172 devc->fm_info.synth_subtype = FM_TYPE_OPL3;
1173 devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
1174
1175 for (i = 0; i < 18; i++)
1176 {
1177 if (pv_map[i].ioaddr == USE_LEFT)
1178 pv_map[i].ioaddr = devc->left_io;
1179 else
1180 pv_map[i].ioaddr = devc->right_io;
1181 }
1182 opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
1183 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
1184 }
1185 else
1186 {
1187 strcpy(devc->fm_info.name, "Yamaha OPL2");
1188 devc->v_alloc->max_voice = devc->nr_voice = 9;
1189 devc->fm_info.nr_drums = 0;
1190
1191 for (i = 0; i < 18; i++)
1192 pv_map[i].ioaddr = devc->left_io;
1193 };
1194 conf_printf2(devc->fm_info.name, ioaddr, 0, -1, -1);
1195
1196 for (i = 0; i < SBFM_MAXINSTR; i++)
1197 devc->i_map[i].channel = -1;
1198
1199 return me;
1200}
1201
1202static int me;
1203
1204static int io = -1;
1205
1206module_param(io, int, 0);
1207
1208static int __init init_opl3 (void)
1209{
1210 printk(KERN_INFO "YM3812 and OPL-3 driver Copyright (C) by Hannu Savolainen, Rob Hooft 1993-1996\n");
1211
1212 if (io != -1)
1213 {
1214 if (!opl3_detect(io))
1215 {
1216 return -ENODEV;
1217 }
1218
1219 me = opl3_init(io, THIS_MODULE);
1220 }
1221
1222 return 0;
1223}
1224
1225static void __exit cleanup_opl3(void)
1226{
1227 if (devc && io != -1)
1228 {
1229 if (devc->base) {
1230 release_region(devc->base,4);
1231 if (devc->is_opl4)
1232 release_region(devc->base - 8, 2);
1233 }
1234 kfree(devc);
1235 devc = NULL;
1236 sound_unload_synthdev(me);
1237 }
1238}
1239
1240module_init(init_opl3);
1241module_exit(cleanup_opl3);
1242
1243#ifndef MODULE
1244static int __init setup_opl3(char *str)
1245{
1246
1247 int ints[2];
1248
1249 str = get_options(str, ARRAY_SIZE(ints), ints);
1250
1251 io = ints[1];
1252
1253 return 1;
1254}
1255
1256__setup("opl3=", setup_opl3);
1257#endif
1258MODULE_LICENSE("GPL");
1259