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