blob: 7ee865d6236fba3258cf6383f8df516eac14f17e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __SOUND_OPL3_H
2#define __SOUND_OPL3_H
3
4/*
5 * Definitions of the OPL-3 registers.
6 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02007 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Hannu Savolainen 1993-1996
9 *
10 *
11 * The OPL-3 mode is switched on by writing 0x01, to the offset 5
12 * of the right side.
13 *
14 * Another special register at the right side is at offset 4. It contains
15 * a bit mask defining which voices are used as 4 OP voices.
16 *
17 * The percussive mode is implemented in the left side only.
18 *
19 * With the above exceptions the both sides can be operated independently.
20 *
21 * A 4 OP voice can be created by setting the corresponding
22 * bit at offset 4 of the right side.
23 *
24 * For example setting the rightmost bit (0x01) changes the
25 * first voice on the right side to the 4 OP mode. The fourth
26 * voice is made inaccessible.
27 *
28 * If a voice is set to the 2 OP mode, it works like 2 OP modes
29 * of the original YM3812 (AdLib). In addition the voice can
30 * be connected the left, right or both stereo channels. It can
31 * even be left unconnected. This works with 4 OP voices also.
32 *
33 * The stereo connection bits are located in the FEEDBACK_CONNECTION
34 * register of the voice (0xC0-0xC8). In 4 OP voices these bits are
35 * in the second half of the voice.
36 *
37 *
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
47 *
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
51 *
52 */
53
54#include "driver.h"
55#include <linux/time.h>
Ingo Molnaref9f0a42006-01-16 16:31:42 +010056#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "core.h"
58#include "hwdep.h"
59#include "timer.h"
60#include "seq_midi_emul.h"
61#ifdef CONFIG_SND_SEQUENCER_OSS
62#include "seq_oss.h"
63#include "seq_oss_legacy.h"
64#endif
65#include "seq_device.h"
Takashi Iwai224a0332007-10-30 11:49:22 +010066#include "asound_fm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * Register numbers for the global registers
70 */
71
72#define OPL3_REG_TEST 0x01
73#define OPL3_ENABLE_WAVE_SELECT 0x20
74
75#define OPL3_REG_TIMER1 0x02
76#define OPL3_REG_TIMER2 0x03
77#define OPL3_REG_TIMER_CONTROL 0x04 /* Left side */
78#define OPL3_IRQ_RESET 0x80
79#define OPL3_TIMER1_MASK 0x40
80#define OPL3_TIMER2_MASK 0x20
81#define OPL3_TIMER1_START 0x01
82#define OPL3_TIMER2_START 0x02
83
84#define OPL3_REG_CONNECTION_SELECT 0x04 /* Right side */
85#define OPL3_LEFT_4OP_0 0x01
86#define OPL3_LEFT_4OP_1 0x02
87#define OPL3_LEFT_4OP_2 0x04
88#define OPL3_RIGHT_4OP_0 0x08
89#define OPL3_RIGHT_4OP_1 0x10
90#define OPL3_RIGHT_4OP_2 0x20
91
92#define OPL3_REG_MODE 0x05 /* Right side */
93#define OPL3_OPL3_ENABLE 0x01 /* OPL3 mode */
94#define OPL3_OPL4_ENABLE 0x02 /* OPL4 mode */
95
96#define OPL3_REG_KBD_SPLIT 0x08 /* Left side */
97#define OPL3_COMPOSITE_SINE_WAVE_MODE 0x80 /* Don't use with OPL-3? */
98#define OPL3_KEYBOARD_SPLIT 0x40
99
100#define OPL3_REG_PERCUSSION 0xbd /* Left side only */
101#define OPL3_TREMOLO_DEPTH 0x80
102#define OPL3_VIBRATO_DEPTH 0x40
103#define OPL3_PERCUSSION_ENABLE 0x20
104#define OPL3_BASSDRUM_ON 0x10
105#define OPL3_SNAREDRUM_ON 0x08
106#define OPL3_TOMTOM_ON 0x04
107#define OPL3_CYMBAL_ON 0x02
108#define OPL3_HIHAT_ON 0x01
109
110/*
111 * Offsets to the register banks for operators. To get the
112 * register number just add the operator offset to the bank offset
113 *
114 * AM/VIB/EG/KSR/Multiple (0x20 to 0x35)
115 */
116#define OPL3_REG_AM_VIB 0x20
117#define OPL3_TREMOLO_ON 0x80
118#define OPL3_VIBRATO_ON 0x40
119#define OPL3_SUSTAIN_ON 0x20
120#define OPL3_KSR 0x10 /* Key scaling rate */
121#define OPL3_MULTIPLE_MASK 0x0f /* Frequency multiplier */
122
123 /*
124 * KSL/Total level (0x40 to 0x55)
125 */
126#define OPL3_REG_KSL_LEVEL 0x40
127#define OPL3_KSL_MASK 0xc0 /* Envelope scaling bits */
128#define OPL3_TOTAL_LEVEL_MASK 0x3f /* Strength (volume) of OP */
129
130/*
131 * Attack / Decay rate (0x60 to 0x75)
132 */
133#define OPL3_REG_ATTACK_DECAY 0x60
134#define OPL3_ATTACK_MASK 0xf0
135#define OPL3_DECAY_MASK 0x0f
136
137/*
138 * Sustain level / Release rate (0x80 to 0x95)
139 */
140#define OPL3_REG_SUSTAIN_RELEASE 0x80
141#define OPL3_SUSTAIN_MASK 0xf0
142#define OPL3_RELEASE_MASK 0x0f
143
144/*
145 * Wave select (0xE0 to 0xF5)
146 */
147#define OPL3_REG_WAVE_SELECT 0xe0
148#define OPL3_WAVE_SELECT_MASK 0x07
149
150/*
151 * Offsets to the register banks for voices. Just add to the
152 * voice number to get the register number.
153 *
154 * F-Number low bits (0xA0 to 0xA8).
155 */
156#define OPL3_REG_FNUM_LOW 0xa0
157
158/*
159 * F-number high bits / Key on / Block (octave) (0xB0 to 0xB8)
160 */
161#define OPL3_REG_KEYON_BLOCK 0xb0
162#define OPL3_KEYON_BIT 0x20
163#define OPL3_BLOCKNUM_MASK 0x1c
164#define OPL3_FNUM_HIGH_MASK 0x03
165
166/*
167 * Feedback / Connection (0xc0 to 0xc8)
168 *
169 * These registers have two new bits when the OPL-3 mode
170 * is selected. These bits controls connecting the voice
171 * to the stereo channels. For 4 OP voices this bit is
172 * defined in the second half of the voice (add 3 to the
173 * register offset).
174 *
175 * For 4 OP voices the connection bit is used in the
176 * both halves (gives 4 ways to connect the operators).
177 */
178#define OPL3_REG_FEEDBACK_CONNECTION 0xc0
179#define OPL3_FEEDBACK_MASK 0x0e /* Valid just for 1st OP of a voice */
180#define OPL3_CONNECTION_BIT 0x01
181/*
182 * In the 4 OP mode there is four possible configurations how the
183 * operators can be connected together (in 2 OP modes there is just
184 * AM or FM). The 4 OP connection mode is defined by the rightmost
185 * bit of the FEEDBACK_CONNECTION (0xC0-0xC8) on the both halves.
186 *
187 * First half Second half Mode
188 *
189 * +---+
190 * v |
191 * 0 0 >+-1-+--2--3--4-->
192 *
193 *
194 *
195 * +---+
196 * | |
197 * 0 1 >+-1-+--2-+
198 * |->
199 * >--3----4-+
200 *
201 * +---+
202 * | |
203 * 1 0 >+-1-+-----+
204 * |->
205 * >--2--3--4-+
206 *
207 * +---+
208 * | |
209 * 1 1 >+-1-+--+
210 * |
211 * >--2--3-+->
212 * |
213 * >--4----+
214 */
215#define OPL3_STEREO_BITS 0x30 /* OPL-3 only */
216#define OPL3_VOICE_TO_LEFT 0x10
217#define OPL3_VOICE_TO_RIGHT 0x20
218
219/*
220
221 */
222
223#define OPL3_LEFT 0x0000
224#define OPL3_RIGHT 0x0100
225
226#define OPL3_HW_AUTO 0x0000
227#define OPL3_HW_OPL2 0x0200
228#define OPL3_HW_OPL3 0x0300
229#define OPL3_HW_OPL3_SV 0x0301 /* S3 SonicVibes */
230#define OPL3_HW_OPL3_CS 0x0302 /* CS4232/CS4236+ */
231#define OPL3_HW_OPL3_FM801 0x0303 /* FM801 */
232#define OPL3_HW_OPL3_CS4281 0x0304 /* CS4281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define OPL3_HW_OPL4 0x0400 /* YMF278B/YMF295 */
234#define OPL3_HW_OPL4_ML 0x0401 /* YMF704/YMF721 */
235#define OPL3_HW_MASK 0xff00
236
237#define MAX_OPL2_VOICES 9
238#define MAX_OPL3_VOICES 18
239
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100240struct snd_opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/*
Takashi Iwai224a0332007-10-30 11:49:22 +0100243 * Instrument record, aka "Patch"
244 */
245
246/* FM operator */
247struct fm_operator {
248 unsigned char am_vib;
249 unsigned char ksl_level;
250 unsigned char attack_decay;
251 unsigned char sustain_release;
252 unsigned char wave_select;
253} __attribute__((packed));
254
255/* Instrument data */
256struct fm_instrument {
257 struct fm_operator op[4];
258 unsigned char feedback_connection[2];
259 unsigned char echo_delay;
260 unsigned char echo_atten;
261 unsigned char chorus_spread;
262 unsigned char trnsps;
263 unsigned char fix_dur;
264 unsigned char modes;
265 unsigned char fix_key;
266};
267
268/* type */
269#define FM_PATCH_OPL2 0x01 /* OPL2 2 operators FM instrument */
270#define FM_PATCH_OPL3 0x02 /* OPL3 4 operators FM instrument */
271
272/* Instrument record */
273struct fm_patch {
274 unsigned char prog;
275 unsigned char bank;
276 unsigned char type;
277 struct fm_instrument inst;
278 char name[24];
279 struct fm_patch *next;
280};
281
282
283/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * A structure to keep track of each hardware voice
285 */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100286struct snd_opl3_voice {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int state; /* status */
288#define SNDRV_OPL3_ST_OFF 0 /* Not playing */
289#define SNDRV_OPL3_ST_ON_2OP 1 /* 2op voice is allocated */
290#define SNDRV_OPL3_ST_ON_4OP 2 /* 4op voice is allocated */
291#define SNDRV_OPL3_ST_NOT_AVAIL -1 /* voice is not available */
292
293 unsigned int time; /* An allocation time */
294 unsigned char note; /* Note currently assigned to this voice */
295
296 unsigned long note_off; /* note-off time */
297 int note_off_check; /* check note-off time */
298
299 unsigned char keyon_reg; /* KON register shadow */
300
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100301 struct snd_midi_channel *chan; /* Midi channel for this note */
302};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304struct snd_opl3 {
305 unsigned long l_port;
306 unsigned long r_port;
307 struct resource *res_l_port;
308 struct resource *res_r_port;
309 unsigned short hardware;
310 /* hardware access */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100311 void (*command) (struct snd_opl3 * opl3, unsigned short cmd, unsigned char val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned short timer_enable;
313 int seq_dev_num; /* sequencer device number */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100314 struct snd_timer *timer1;
315 struct snd_timer *timer2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 spinlock_t timer_lock;
317
318 void *private_data;
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100319 void (*private_free)(struct snd_opl3 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 spinlock_t reg_lock;
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100322 struct snd_card *card; /* The card that this belongs to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int used; /* usage flag - exclusive */
324 unsigned char fm_mode; /* OPL mode, see SNDRV_DM_FM_MODE_XXX */
325 unsigned char rhythm; /* percussion mode flag */
326 unsigned char max_voices; /* max number of voices */
327#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
328#define SNDRV_OPL3_MODE_SYNTH 0 /* OSS - voices allocated by application */
329#define SNDRV_OPL3_MODE_SEQ 1 /* ALSA - driver handles voice allocation */
330 int synth_mode; /* synth mode */
331 int seq_client;
332
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100333 struct snd_seq_device *seq_dev; /* sequencer device */
334 struct snd_midi_channel_set * chset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336#ifdef CONFIG_SND_SEQUENCER_OSS
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100337 struct snd_seq_device *oss_seq_dev; /* OSS sequencer device */
338 struct snd_midi_channel_set * oss_chset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#endif
340
Takashi Iwai224a0332007-10-30 11:49:22 +0100341#define OPL3_PATCH_HASH_SIZE 32
342 struct fm_patch *patch_table[OPL3_PATCH_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100344 struct snd_opl3_voice voices[MAX_OPL3_VOICES]; /* Voices (OPL3 'channel') */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int use_time; /* allocation counter */
346
347 unsigned short connection_reg; /* connection reg shadow */
348 unsigned char drum_reg; /* percussion reg shadow */
349
350 spinlock_t voice_lock; /* Lock for voice access */
351
352 struct timer_list tlist; /* timer for note-offs and effects */
353 int sys_timer_status; /* system timer run status */
354 spinlock_t sys_timer_lock; /* Lock for system timer access */
355#endif
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100356 struct mutex access_mutex; /* locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
359/* opl3.c */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100360void snd_opl3_interrupt(struct snd_hwdep * hw);
361int snd_opl3_new(struct snd_card *card, unsigned short hardware,
362 struct snd_opl3 **ropl3);
363int snd_opl3_init(struct snd_opl3 *opl3);
364int snd_opl3_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned long l_port, unsigned long r_port,
366 unsigned short hardware,
367 int integrated,
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100368 struct snd_opl3 ** opl3);
369int snd_opl3_timer_new(struct snd_opl3 * opl3, int timer1_dev, int timer2_dev);
370int snd_opl3_hwdep_new(struct snd_opl3 * opl3, int device, int seq_device,
371 struct snd_hwdep ** rhwdep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373/* opl3_synth */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100374int snd_opl3_open(struct snd_hwdep * hw, struct file *file);
375int snd_opl3_ioctl(struct snd_hwdep * hw, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned int cmd, unsigned long arg);
Takashi Iwai224a0332007-10-30 11:49:22 +0100377long snd_opl3_write(struct snd_hwdep *hw, const char __user *buf, long count,
378 loff_t *offset);
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100379int snd_opl3_release(struct snd_hwdep * hw, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100381void snd_opl3_reset(struct snd_opl3 * opl3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Takashi Iwai224a0332007-10-30 11:49:22 +0100383int snd_opl3_load_patch(struct snd_opl3 *opl3,
384 int prog, int bank, int type,
385 const char *name,
386 const unsigned char *ext,
387 const unsigned char *data);
388struct fm_patch *snd_opl3_find_patch(struct snd_opl3 *opl3, int prog, int bank,
389 int create_patch);
390void snd_opl3_clear_patches(struct snd_opl3 *opl3);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#endif /* __SOUND_OPL3_H */