blob: 7781c13c147635b53b06756e31b0daae50b583c0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * sound/oss/opl3.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * A low level driver for Yamaha YM3812 and OPL-3 -chips
5 *
6 *
7 * Copyright (C) by Hannu Savolainen 1993-1997
8 *
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.
12 *
13 *
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
18 * Arnaldo C. de Melo get rid of check_region, use request_region for
19 * OPL4, release it on exit, some cleanups.
20 *
21 * Status
22 * Believed to work. Badly needs rewriting a bit to support multiple
23 * OPL3 devices.
24 */
25
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/delay.h>
29
30/*
31 * Major improvements to the FM handling 30AUG92 by Rob Hooft,
32 * hooft@chem.ruu.nl
33 */
34
35#include "sound_config.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "opl3_hw.h"
38
39#define MAX_VOICE 18
40#define OFFS_4OP 11
41
42struct voice_info
43{
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 */
52};
53
54typedef struct opl_devinfo
55{
56 int base;
57 int left_io, right_io;
58 int nr_voice;
59 int lv_map[MAX_VOICE];
60
61 struct voice_info voc[MAX_VOICE];
62 struct voice_alloc_info *v_alloc;
63 struct channel_info *chn_info;
64
65 struct sbi_instrument i_map[SBFM_MAXINSTR];
66 struct sbi_instrument *act_i[MAX_VOICE];
67
68 struct synth_info fm_info;
69
70 int busy;
71 int model;
72 unsigned char cmask;
73
74 int is_opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075} opl_devinfo;
76
77static struct opl_devinfo *devc = NULL;
78
79static int detected_model;
80
81static int store_instr(int instr_no, struct sbi_instrument *instr);
82static void freq_to_fnum(int freq, int *block, int *fnum);
83static void opl3_command(int io_addr, unsigned int addr, unsigned int val);
84static int opl3_kill_note(int dev, int voice, int note, int velocity);
85
86static void enter_4op_mode(void)
87{
88 int i;
89 static int v4op[MAX_VOICE] = {
90 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17
91 };
92
93 devc->cmask = 0x3f; /* Connect all possible 4 OP voice operators */
94 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f);
95
96 for (i = 0; i < 3; i++)
97 pv_map[i].voice_mode = 4;
98 for (i = 3; i < 6; i++)
99 pv_map[i].voice_mode = 0;
100
101 for (i = 9; i < 12; i++)
102 pv_map[i].voice_mode = 4;
103 for (i = 12; i < 15; i++)
104 pv_map[i].voice_mode = 0;
105
106 for (i = 0; i < 12; i++)
107 devc->lv_map[i] = v4op[i];
108 devc->v_alloc->max_voice = devc->nr_voice = 12;
109}
110
111static int opl3_ioctl(int dev, unsigned int cmd, void __user * arg)
112{
113 struct sbi_instrument ins;
114
115 switch (cmd) {
116 case SNDCTL_FM_LOAD_INSTR:
117 printk(KERN_WARNING "Warning: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
118 if (copy_from_user(&ins, arg, sizeof(ins)))
119 return -EFAULT;
120 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
121 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
122 return -EINVAL;
123 }
124 return store_instr(ins.channel, &ins);
125
126 case SNDCTL_SYNTH_INFO:
127 devc->fm_info.nr_voices = (devc->nr_voice == 12) ? 6 : devc->nr_voice;
128 if (copy_to_user(arg, &devc->fm_info, sizeof(devc->fm_info)))
129 return -EFAULT;
130 return 0;
131
132 case SNDCTL_SYNTH_MEMAVL:
133 return 0x7fffffff;
134
135 case SNDCTL_FM_4OP_ENABLE:
136 if (devc->model == 2)
137 enter_4op_mode();
138 return 0;
139
140 default:
141 return -EINVAL;
142 }
143}
144
Adrian Bunkb5d425c2007-07-15 23:39:01 -0700145static int opl3_detect(int ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 /*
148 * This function returns 1 if the FM chip is present at the given I/O port
149 * The detection algorithm plays with the timer built in the FM chip and
150 * looks for a change in the status register.
151 *
152 * Note! The timers of the FM chip are not connected to AdLib (and compatible)
153 * boards.
154 *
155 * Note2! The chip is initialized if detected.
156 */
157
158 unsigned char stat1, signature;
159 int i;
160
161 if (devc != NULL)
162 {
163 printk(KERN_ERR "opl3: Only one OPL3 supported.\n");
164 return 0;
165 }
166
Robert P. J. Day3159f062007-02-14 00:33:16 -0800167 devc = kzalloc(sizeof(*devc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 if (devc == NULL)
170 {
171 printk(KERN_ERR "opl3: Can't allocate memory for the device control "
172 "structure \n ");
173 return 0;
174 }
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 strcpy(devc->fm_info.name, "OPL2");
177
178 if (!request_region(ioaddr, 4, devc->fm_info.name)) {
179 printk(KERN_WARNING "opl3: I/O port 0x%x already in use\n", ioaddr);
180 goto cleanup_devc;
181 }
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 devc->base = ioaddr;
184
185 /* Reset timers 1 and 2 */
186 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
187
188 /* Reset the IRQ of the FM chip */
189 opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
190
191 signature = stat1 = inb(ioaddr); /* Status register */
192
193 if (signature != 0x00 && signature != 0x06 && signature != 0x02 &&
194 signature != 0x0f)
195 {
196 MDB(printk(KERN_INFO "OPL3 not detected %x\n", signature));
197 goto cleanup_region;
198 }
199
200 if (signature == 0x06) /* OPL2 */
201 {
202 detected_model = 2;
203 }
204 else if (signature == 0x00 || signature == 0x0f) /* OPL3 or OPL4 */
205 {
206 unsigned char tmp;
207
208 detected_model = 3;
209
210 /*
211 * Detect availability of OPL4 (_experimental_). Works probably
212 * only after a cold boot. In addition the OPL4 port
213 * of the chip may not be connected to the PC bus at all.
214 */
215
216 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
217 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
218
219 if ((tmp = inb(ioaddr)) == 0x02) /* Have a OPL4 */
220 {
221 detected_model = 4;
222 }
223
224 if (request_region(ioaddr - 8, 2, "OPL4")) /* OPL4 port was free */
225 {
226 int tmp;
227
228 outb((0x02), ioaddr - 8); /* Select OPL4 ID register */
229 udelay(10);
230 tmp = inb(ioaddr - 7); /* Read it */
231 udelay(10);
232
233 if (tmp == 0x20) /* OPL4 should return 0x20 here */
234 {
235 detected_model = 4;
236 outb((0xF8), ioaddr - 8); /* Select OPL4 FM mixer control */
237 udelay(10);
238 outb((0x1B), ioaddr - 7); /* Write value */
239 udelay(10);
240 }
241 else
242 { /* release OPL4 port */
243 release_region(ioaddr - 8, 2);
244 detected_model = 3;
245 }
246 }
247 opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
248 }
249 for (i = 0; i < 9; i++)
250 opl3_command(ioaddr, KEYON_BLOCK + i, 0); /*
251 * Note off
252 */
253
254 opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
255 opl3_command(ioaddr, PERCOSSION_REGISTER, 0x00); /*
256 * Melodic mode.
257 */
258 return 1;
259cleanup_region:
260 release_region(ioaddr, 4);
261cleanup_devc:
262 kfree(devc);
263 devc = NULL;
264 return 0;
265}
266
267static int opl3_kill_note (int devno, int voice, int note, int velocity)
268{
269 struct physical_voice_info *map;
270
271 if (voice < 0 || voice >= devc->nr_voice)
272 return 0;
273
274 devc->v_alloc->map[voice] = 0;
275
276 map = &pv_map[devc->lv_map[voice]];
277 DEB(printk("Kill note %d\n", voice));
278
279 if (map->voice_mode == 0)
280 return 0;
281
282 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, devc->voc[voice].keyon_byte & ~0x20);
283 devc->voc[voice].keyon_byte = 0;
284 devc->voc[voice].bender = 0;
285 devc->voc[voice].volume = 64;
286 devc->voc[voice].panning = 0xffff; /* Not set */
287 devc->voc[voice].bender_range = 200;
288 devc->voc[voice].orig_freq = 0;
289 devc->voc[voice].current_freq = 0;
290 devc->voc[voice].mode = 0;
291 return 0;
292}
293
294#define HIHAT 0
295#define CYMBAL 1
296#define TOMTOM 2
297#define SNARE 3
298#define BDRUM 4
299#define UNDEFINED TOMTOM
300#define DEFAULT TOMTOM
301
302static int store_instr(int instr_no, struct sbi_instrument *instr)
303{
304 if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || devc->model != 2))
305 printk(KERN_WARNING "FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
306 memcpy((char *) &(devc->i_map[instr_no]), (char *) instr, sizeof(*instr));
307 return 0;
308}
309
310static int opl3_set_instr (int dev, int voice, int instr_no)
311{
312 if (voice < 0 || voice >= devc->nr_voice)
313 return 0;
314 if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
315 instr_no = 0; /* Acoustic piano (usually) */
316
317 devc->act_i[voice] = &devc->i_map[instr_no];
318 return 0;
319}
320
321/*
322 * The next table looks magical, but it certainly is not. Its values have
323 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
324 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
325 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
326 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
327 * volume -8 it was implemented as a table because it is only 128 bytes and
328 * it saves a lot of log() calculations. (RH)
329 */
330
331static char fm_volume_table[128] =
332{
333 -64, -48, -40, -35, -32, -29, -27, -26,
334 -24, -23, -21, -20, -19, -18, -18, -17,
335 -16, -15, -15, -14, -13, -13, -12, -12,
336 -11, -11, -10, -10, -10, -9, -9, -8,
337 -8, -8, -7, -7, -7, -6, -6, -6,
338 -5, -5, -5, -5, -4, -4, -4, -4,
339 -3, -3, -3, -3, -2, -2, -2, -2,
340 -2, -1, -1, -1, -1, 0, 0, 0,
341 0, 0, 0, 1, 1, 1, 1, 1,
342 1, 2, 2, 2, 2, 2, 2, 2,
343 3, 3, 3, 3, 3, 3, 3, 4,
344 4, 4, 4, 4, 4, 4, 4, 5,
345 5, 5, 5, 5, 5, 5, 5, 5,
346 6, 6, 6, 6, 6, 6, 6, 6,
347 6, 7, 7, 7, 7, 7, 7, 7,
348 7, 7, 7, 8, 8, 8, 8, 8
349};
350
351static void calc_vol(unsigned char *regbyte, int volume, int main_vol)
352{
353 int level = (~*regbyte & 0x3f);
354
355 if (main_vol > 127)
356 main_vol = 127;
357 volume = (volume * main_vol) / 127;
358
359 if (level)
360 level += fm_volume_table[volume];
361
362 if (level > 0x3f)
363 level = 0x3f;
364 if (level < 0)
365 level = 0;
366
367 *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
368}
369
370static void set_voice_volume(int voice, int volume, int main_vol)
371{
372 unsigned char vol1, vol2, vol3, vol4;
373 struct sbi_instrument *instr;
374 struct physical_voice_info *map;
375
376 if (voice < 0 || voice >= devc->nr_voice)
377 return;
378
379 map = &pv_map[devc->lv_map[voice]];
380 instr = devc->act_i[voice];
381
382 if (!instr)
383 instr = &devc->i_map[0];
384
385 if (instr->channel < 0)
386 return;
387
388 if (devc->voc[voice].mode == 0)
389 return;
390
391 if (devc->voc[voice].mode == 2)
392 {
393 vol1 = instr->operators[2];
394 vol2 = instr->operators[3];
395 if ((instr->operators[10] & 0x01))
396 {
397 calc_vol(&vol1, volume, main_vol);
398 calc_vol(&vol2, volume, main_vol);
399 }
400 else
401 {
402 calc_vol(&vol2, volume, main_vol);
403 }
404 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
405 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
406 }
407 else
408 { /*
409 * 4 OP voice
410 */
411 int connection;
412
413 vol1 = instr->operators[2];
414 vol2 = instr->operators[3];
415 vol3 = instr->operators[OFFS_4OP + 2];
416 vol4 = instr->operators[OFFS_4OP + 3];
417
418 /*
419 * The connection method for 4 OP devc->voc is defined by the rightmost
420 * bits at the offsets 10 and 10+OFFS_4OP
421 */
422
423 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
424
425 switch (connection)
426 {
427 case 0:
428 calc_vol(&vol4, volume, main_vol);
429 break;
430
431 case 1:
432 calc_vol(&vol2, volume, main_vol);
433 calc_vol(&vol4, volume, main_vol);
434 break;
435
436 case 2:
437 calc_vol(&vol1, volume, main_vol);
438 calc_vol(&vol4, volume, main_vol);
439 break;
440
441 case 3:
442 calc_vol(&vol1, volume, main_vol);
443 calc_vol(&vol3, volume, main_vol);
444 calc_vol(&vol4, volume, main_vol);
445 break;
446
447 default:
448 ;
449 }
450 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
451 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
452 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3);
453 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4);
454 }
455}
456
457static int opl3_start_note (int dev, int voice, int note, int volume)
458{
459 unsigned char data, fpc;
460 int block, fnum, freq, voice_mode, pan;
461 struct sbi_instrument *instr;
462 struct physical_voice_info *map;
463
464 if (voice < 0 || voice >= devc->nr_voice)
465 return 0;
466
467 map = &pv_map[devc->lv_map[voice]];
468 pan = devc->voc[voice].panning;
469
470 if (map->voice_mode == 0)
471 return 0;
472
473 if (note == 255) /*
474 * Just change the volume
475 */
476 {
477 set_voice_volume(voice, volume, devc->voc[voice].volume);
478 return 0;
479 }
480
481 /*
482 * Kill previous note before playing
483 */
484
485 opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff); /*
486 * Carrier
487 * volume to
488 * min
489 */
490 opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff); /*
491 * Modulator
492 * volume to
493 */
494
495 if (map->voice_mode == 4)
496 {
497 opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
498 opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
499 }
500
501 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00); /*
502 * Note
503 * off
504 */
505
506 instr = devc->act_i[voice];
507
508 if (!instr)
509 instr = &devc->i_map[0];
510
511 if (instr->channel < 0)
512 {
513 printk(KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice);
514 return 0;
515 }
516
517 if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
518 return 0; /*
519 * Cannot play
520 */
521
522 voice_mode = map->voice_mode;
523
524 if (voice_mode == 4)
525 {
526 int voice_shift;
527
528 voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
529 voice_shift += map->voice_num;
530
531 if (instr->key != OPL3_PATCH) /*
532 * Just 2 OP patch
533 */
534 {
535 voice_mode = 2;
536 devc->cmask &= ~(1 << voice_shift);
537 }
538 else
539 {
540 devc->cmask |= (1 << voice_shift);
541 }
542
543 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
544 }
545
546 /*
547 * Set Sound Characteristics
548 */
549
550 opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
551 opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
552
553 /*
554 * Set Attack/Decay
555 */
556
557 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
558 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
559
560 /*
561 * Set Sustain/Release
562 */
563
564 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
565 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
566
567 /*
568 * Set Wave Select
569 */
570
571 opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
572 opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
573
574 /*
575 * Set Feedback/Connection
576 */
577
578 fpc = instr->operators[10];
579
580 if (pan != 0xffff)
581 {
582 fpc &= ~STEREO_BITS;
583 if (pan < -64)
584 fpc |= VOICE_TO_LEFT;
585 else
586 if (pan > 64)
587 fpc |= VOICE_TO_RIGHT;
588 else
589 fpc |= (VOICE_TO_LEFT | VOICE_TO_RIGHT);
590 }
591
592 if (!(fpc & 0x30))
593 fpc |= 0x30; /*
594 * Ensure that at least one chn is enabled
595 */
596 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
597
598 /*
599 * If the voice is a 4 OP one, initialize the operators 3 and 4 also
600 */
601
602 if (voice_mode == 4)
603 {
604 /*
605 * Set Sound Characteristics
606 */
607
608 opl3_command(map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
609 opl3_command(map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
610
611 /*
612 * Set Attack/Decay
613 */
614
615 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
616 opl3_command(map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
617
618 /*
619 * Set Sustain/Release
620 */
621
622 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
623 opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
624
625 /*
626 * Set Wave Select
627 */
628
629 opl3_command(map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
630 opl3_command(map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
631
632 /*
633 * Set Feedback/Connection
634 */
635
636 fpc = instr->operators[OFFS_4OP + 10];
637 if (!(fpc & 0x30))
638 fpc |= 0x30; /*
639 * Ensure that at least one chn is enabled
640 */
641 opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
642 }
643
644 devc->voc[voice].mode = voice_mode;
645 set_voice_volume(voice, volume, devc->voc[voice].volume);
646
647 freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000;
648
649 /*
650 * Since the pitch bender may have been set before playing the note, we
651 * have to calculate the bending now.
652 */
653
654 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
655 devc->voc[voice].current_freq = freq;
656
657 freq_to_fnum(freq, &block, &fnum);
658
659 /*
660 * Play note
661 */
662
663 data = fnum & 0xff; /*
664 * Least significant bits of fnumber
665 */
666 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
667
668 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
669 devc->voc[voice].keyon_byte = data;
670 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
671 if (voice_mode == 4)
672 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
673
674 return 0;
675}
676
677static void freq_to_fnum (int freq, int *block, int *fnum)
678{
679 int f, octave;
680
681 /*
682 * Converts the note frequency to block and fnum values for the FM chip
683 */
684 /*
685 * First try to compute the block -value (octave) where the note belongs
686 */
687
688 f = freq;
689
690 octave = 5;
691
692 if (f == 0)
693 octave = 0;
694 else if (f < 261)
695 {
696 while (f < 261)
697 {
698 octave--;
699 f <<= 1;
700 }
701 }
702 else if (f > 493)
703 {
704 while (f > 493)
705 {
706 octave++;
707 f >>= 1;
708 }
709 }
710
711 if (octave > 7)
712 octave = 7;
713
714 *fnum = freq * (1 << (20 - octave)) / 49716;
715 *block = octave;
716}
717
718static void opl3_command (int io_addr, unsigned int addr, unsigned int val)
719{
720 int i;
721
722 /*
723 * The original 2-OP synth requires a quite long delay after writing to a
724 * register. The OPL-3 survives with just two INBs
725 */
726
727 outb(((unsigned char) (addr & 0xff)), io_addr);
728
729 if (devc->model != 2)
730 udelay(10);
731 else
732 for (i = 0; i < 2; i++)
733 inb(io_addr);
734
735 outb(((unsigned char) (val & 0xff)), io_addr + 1);
736
737 if (devc->model != 2)
738 udelay(30);
739 else
740 for (i = 0; i < 2; i++)
741 inb(io_addr);
742}
743
744static void opl3_reset(int devno)
745{
746 int i;
747
748 for (i = 0; i < 18; i++)
749 devc->lv_map[i] = i;
750
751 for (i = 0; i < devc->nr_voice; i++)
752 {
753 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
754 KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
755
756 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
757 KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
758
759 if (pv_map[devc->lv_map[i]].voice_mode == 4)
760 {
761 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
762 KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
763
764 opl3_command(pv_map[devc->lv_map[i]].ioaddr,
765 KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
766 }
767
768 opl3_kill_note(devno, i, 0, 64);
769 }
770
771 if (devc->model == 2)
772 {
773 devc->v_alloc->max_voice = devc->nr_voice = 18;
774
775 for (i = 0; i < 18; i++)
776 pv_map[i].voice_mode = 2;
777
778 }
779}
780
781static int opl3_open(int dev, int mode)
782{
783 int i;
784
785 if (devc->busy)
786 return -EBUSY;
787 devc->busy = 1;
788
789 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
790 devc->v_alloc->timestamp = 0;
791
792 for (i = 0; i < 18; i++)
793 {
794 devc->v_alloc->map[i] = 0;
795 devc->v_alloc->alloc_times[i] = 0;
796 }
797
798 devc->cmask = 0x00; /*
799 * Just 2 OP mode
800 */
801 if (devc->model == 2)
802 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
803 return 0;
804}
805
806static void opl3_close(int dev)
807{
808 devc->busy = 0;
809 devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
810
811 devc->fm_info.nr_drums = 0;
812 devc->fm_info.perc_mode = 0;
813
814 opl3_reset(dev);
815}
816
817static void opl3_hw_control(int dev, unsigned char *event)
818{
819}
820
821static int opl3_load_patch(int dev, int format, const char __user *addr,
822 int offs, int count, int pmgr_flag)
823{
824 struct sbi_instrument ins;
825
826 if (count <sizeof(ins))
827 {
828 printk(KERN_WARNING "FM Error: Patch record too short\n");
829 return -EINVAL;
830 }
831
832 /*
833 * What the fuck is going on here? We leave junk in the beginning
834 * of ins and then check the field pretty close to that beginning?
835 */
836 if(copy_from_user(&((char *) &ins)[offs], addr + offs, sizeof(ins) - offs))
837 return -EFAULT;
838
839 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
840 {
841 printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
842 return -EINVAL;
843 }
844 ins.key = format;
845
846 return store_instr(ins.channel, &ins);
847}
848
849static void opl3_panning(int dev, int voice, int value)
850{
851 devc->voc[voice].panning = value;
852}
853
854static void opl3_volume_method(int dev, int mode)
855{
856}
857
858#define SET_VIBRATO(cell) { \
859 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
860 if (pressure > 110) \
861 tmp |= 0x40; /* Vibrato on */ \
862 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
863
864static void opl3_aftertouch(int dev, int voice, int pressure)
865{
866 int tmp;
867 struct sbi_instrument *instr;
868 struct physical_voice_info *map;
869
870 if (voice < 0 || voice >= devc->nr_voice)
871 return;
872
873 map = &pv_map[devc->lv_map[voice]];
874
875 DEB(printk("Aftertouch %d\n", voice));
876
877 if (map->voice_mode == 0)
878 return;
879
880 /*
881 * Adjust the amount of vibrato depending the pressure
882 */
883
884 instr = devc->act_i[voice];
885
886 if (!instr)
887 instr = &devc->i_map[0];
888
889 if (devc->voc[voice].mode == 4)
890 {
891 int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
892
893 switch (connection)
894 {
895 case 0:
896 SET_VIBRATO(4);
897 break;
898
899 case 1:
900 SET_VIBRATO(2);
901 SET_VIBRATO(4);
902 break;
903
904 case 2:
905 SET_VIBRATO(1);
906 SET_VIBRATO(4);
907 break;
908
909 case 3:
910 SET_VIBRATO(1);
911 SET_VIBRATO(3);
912 SET_VIBRATO(4);
913 break;
914
915 }
916 /*
917 * Not implemented yet
918 */
919 }
920 else
921 {
922 SET_VIBRATO(1);
923
924 if ((instr->operators[10] & 0x01)) /*
925 * Additive synthesis
926 */
927 SET_VIBRATO(2);
928 }
929}
930
931#undef SET_VIBRATO
932
933static void bend_pitch(int dev, int voice, int value)
934{
935 unsigned char data;
936 int block, fnum, freq;
937 struct physical_voice_info *map;
938
939 map = &pv_map[devc->lv_map[voice]];
940
941 if (map->voice_mode == 0)
942 return;
943
944 devc->voc[voice].bender = value;
945 if (!value)
946 return;
947 if (!(devc->voc[voice].keyon_byte & 0x20))
948 return; /*
949 * Not keyed on
950 */
951
952 freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
953 devc->voc[voice].current_freq = freq;
954
955 freq_to_fnum(freq, &block, &fnum);
956
957 data = fnum & 0xff; /*
958 * Least significant bits of fnumber
959 */
960 opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
961
962 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
963 devc->voc[voice].keyon_byte = data;
964 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
965}
966
967static void opl3_controller (int dev, int voice, int ctrl_num, int value)
968{
969 if (voice < 0 || voice >= devc->nr_voice)
970 return;
971
972 switch (ctrl_num)
973 {
974 case CTRL_PITCH_BENDER:
975 bend_pitch(dev, voice, value);
976 break;
977
978 case CTRL_PITCH_BENDER_RANGE:
979 devc->voc[voice].bender_range = value;
980 break;
981
982 case CTL_MAIN_VOLUME:
983 devc->voc[voice].volume = value / 128;
984 break;
985
986 case CTL_PAN:
987 devc->voc[voice].panning = (value * 2) - 128;
988 break;
989 }
990}
991
992static void opl3_bender(int dev, int voice, int value)
993{
994 if (voice < 0 || voice >= devc->nr_voice)
995 return;
996
997 bend_pitch(dev, voice, value - 8192);
998}
999
1000static int opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info *alloc)
1001{
1002 int i, p, best, first, avail, best_time = 0x7fffffff;
1003 struct sbi_instrument *instr;
1004 int is4op;
1005 int instr_no;
1006
1007 if (chn < 0 || chn > 15)
1008 instr_no = 0;
1009 else
1010 instr_no = devc->chn_info[chn].pgm_num;
1011
1012 instr = &devc->i_map[instr_no];
1013 if (instr->channel < 0 || /* Instrument not loaded */
1014 devc->nr_voice != 12) /* Not in 4 OP mode */
1015 is4op = 0;
1016 else if (devc->nr_voice == 12) /* 4 OP mode */
1017 is4op = (instr->key == OPL3_PATCH);
1018 else
1019 is4op = 0;
1020
1021 if (is4op)
1022 {
1023 first = p = 0;
1024 avail = 6;
1025 }
1026 else
1027 {
1028 if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP only' operators first */
1029 first = p = 6;
1030 else
1031 first = p = 0;
1032 avail = devc->nr_voice;
1033 }
1034
1035 /*
1036 * Now try to find a free voice
1037 */
1038 best = first;
1039
1040 for (i = 0; i < avail; i++)
1041 {
1042 if (alloc->map[p] == 0)
1043 {
1044 return p;
1045 }
1046 if (alloc->alloc_times[p] < best_time) /* Find oldest playing note */
1047 {
1048 best_time = alloc->alloc_times[p];
1049 best = p;
1050 }
1051 p = (p + 1) % avail;
1052 }
1053
1054 /*
1055 * Insert some kind of priority mechanism here.
1056 */
1057
1058 if (best < 0)
1059 best = 0;
1060 if (best > devc->nr_voice)
1061 best -= devc->nr_voice;
1062
1063 return best; /* All devc->voc in use. Select the first one. */
1064}
1065
1066static void opl3_setup_voice(int dev, int voice, int chn)
1067{
1068 struct channel_info *info =
1069 &synth_devs[dev]->chn_info[chn];
1070
1071 opl3_set_instr(dev, voice, info->pgm_num);
1072
1073 devc->voc[voice].bender = 0;
1074 devc->voc[voice].bender_range = info->bender_range;
1075 devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
1076 devc->voc[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
1077}
1078
1079static struct synth_operations opl3_operations =
1080{
1081 .owner = THIS_MODULE,
1082 .id = "OPL",
1083 .info = NULL,
1084 .midi_dev = 0,
1085 .synth_type = SYNTH_TYPE_FM,
1086 .synth_subtype = FM_TYPE_ADLIB,
1087 .open = opl3_open,
1088 .close = opl3_close,
1089 .ioctl = opl3_ioctl,
1090 .kill_note = opl3_kill_note,
1091 .start_note = opl3_start_note,
1092 .set_instr = opl3_set_instr,
1093 .reset = opl3_reset,
1094 .hw_control = opl3_hw_control,
1095 .load_patch = opl3_load_patch,
1096 .aftertouch = opl3_aftertouch,
1097 .controller = opl3_controller,
1098 .panning = opl3_panning,
1099 .volume_method = opl3_volume_method,
1100 .bender = opl3_bender,
1101 .alloc_voice = opl3_alloc_voice,
1102 .setup_voice = opl3_setup_voice
1103};
1104
Adrian Bunkb5d425c2007-07-15 23:39:01 -07001105static int opl3_init(int ioaddr, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 int i;
1108 int me;
1109
1110 if (devc == NULL)
1111 {
1112 printk(KERN_ERR "opl3: Device control structure not initialized.\n");
1113 return -1;
1114 }
1115
1116 if ((me = sound_alloc_synthdev()) == -1)
1117 {
1118 printk(KERN_WARNING "opl3: Too many synthesizers\n");
1119 return -1;
1120 }
1121
1122 devc->nr_voice = 9;
1123
1124 devc->fm_info.device = 0;
1125 devc->fm_info.synth_type = SYNTH_TYPE_FM;
1126 devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
1127 devc->fm_info.perc_mode = 0;
1128 devc->fm_info.nr_voices = 9;
1129 devc->fm_info.nr_drums = 0;
1130 devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
1131 devc->fm_info.capabilities = 0;
1132 devc->left_io = ioaddr;
1133 devc->right_io = ioaddr + 2;
1134
1135 if (detected_model <= 2)
1136 devc->model = 1;
1137 else
1138 {
1139 devc->model = 2;
1140 if (detected_model == 4)
1141 devc->is_opl4 = 1;
1142 }
1143
1144 opl3_operations.info = &devc->fm_info;
1145
1146 synth_devs[me] = &opl3_operations;
1147
1148 if (owner)
1149 synth_devs[me]->owner = owner;
1150
1151 sequencer_init();
1152 devc->v_alloc = &opl3_operations.alloc;
1153 devc->chn_info = &opl3_operations.chn_info[0];
1154
1155 if (devc->model == 2)
1156 {
1157 if (devc->is_opl4)
1158 strcpy(devc->fm_info.name, "Yamaha OPL4/OPL3 FM");
1159 else
1160 strcpy(devc->fm_info.name, "Yamaha OPL3");
1161
1162 devc->v_alloc->max_voice = devc->nr_voice = 18;
1163 devc->fm_info.nr_drums = 0;
1164 devc->fm_info.synth_subtype = FM_TYPE_OPL3;
1165 devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
1166
1167 for (i = 0; i < 18; i++)
1168 {
1169 if (pv_map[i].ioaddr == USE_LEFT)
1170 pv_map[i].ioaddr = devc->left_io;
1171 else
1172 pv_map[i].ioaddr = devc->right_io;
1173 }
1174 opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
1175 opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
1176 }
1177 else
1178 {
1179 strcpy(devc->fm_info.name, "Yamaha OPL2");
1180 devc->v_alloc->max_voice = devc->nr_voice = 9;
1181 devc->fm_info.nr_drums = 0;
1182
1183 for (i = 0; i < 18; i++)
1184 pv_map[i].ioaddr = devc->left_io;
1185 };
1186 conf_printf2(devc->fm_info.name, ioaddr, 0, -1, -1);
1187
1188 for (i = 0; i < SBFM_MAXINSTR; i++)
1189 devc->i_map[i].channel = -1;
1190
1191 return me;
1192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194static int me;
1195
1196static int io = -1;
1197
1198module_param(io, int, 0);
1199
1200static int __init init_opl3 (void)
1201{
1202 printk(KERN_INFO "YM3812 and OPL-3 driver Copyright (C) by Hannu Savolainen, Rob Hooft 1993-1996\n");
1203
1204 if (io != -1) /* User loading pure OPL3 module */
1205 {
Adrian Bunkb5d425c2007-07-15 23:39:01 -07001206 if (!opl3_detect(io))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 {
1208 return -ENODEV;
1209 }
1210
Adrian Bunkb5d425c2007-07-15 23:39:01 -07001211 me = opl3_init(io, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
1214 return 0;
1215}
1216
1217static void __exit cleanup_opl3(void)
1218{
1219 if (devc && io != -1)
1220 {
1221 if (devc->base) {
1222 release_region(devc->base,4);
1223 if (devc->is_opl4)
1224 release_region(devc->base - 8, 2);
1225 }
1226 kfree(devc);
1227 devc = NULL;
1228 sound_unload_synthdev(me);
1229 }
1230}
1231
1232module_init(init_opl3);
1233module_exit(cleanup_opl3);
1234
1235#ifndef MODULE
1236static int __init setup_opl3(char *str)
1237{
1238 /* io */
1239 int ints[2];
1240
1241 str = get_options(str, ARRAY_SIZE(ints), ints);
1242
1243 io = ints[1];
1244
1245 return 1;
1246}
1247
1248__setup("opl3=", setup_opl3);
1249#endif
1250MODULE_LICENSE("GPL");