blob: 19f657d4fc063dd17e5bb8a0e962376663a44b51 [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 *
7 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
8 * 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>
56#include "core.h"
57#include "hwdep.h"
58#include "timer.h"
59#include "seq_midi_emul.h"
60#ifdef CONFIG_SND_SEQUENCER_OSS
61#include "seq_oss.h"
62#include "seq_oss_legacy.h"
63#endif
64#include "seq_device.h"
65#include "ainstr_fm.h"
66
67/*
68 * Register numbers for the global registers
69 */
70
71#define OPL3_REG_TEST 0x01
72#define OPL3_ENABLE_WAVE_SELECT 0x20
73
74#define OPL3_REG_TIMER1 0x02
75#define OPL3_REG_TIMER2 0x03
76#define OPL3_REG_TIMER_CONTROL 0x04 /* Left side */
77#define OPL3_IRQ_RESET 0x80
78#define OPL3_TIMER1_MASK 0x40
79#define OPL3_TIMER2_MASK 0x20
80#define OPL3_TIMER1_START 0x01
81#define OPL3_TIMER2_START 0x02
82
83#define OPL3_REG_CONNECTION_SELECT 0x04 /* Right side */
84#define OPL3_LEFT_4OP_0 0x01
85#define OPL3_LEFT_4OP_1 0x02
86#define OPL3_LEFT_4OP_2 0x04
87#define OPL3_RIGHT_4OP_0 0x08
88#define OPL3_RIGHT_4OP_1 0x10
89#define OPL3_RIGHT_4OP_2 0x20
90
91#define OPL3_REG_MODE 0x05 /* Right side */
92#define OPL3_OPL3_ENABLE 0x01 /* OPL3 mode */
93#define OPL3_OPL4_ENABLE 0x02 /* OPL4 mode */
94
95#define OPL3_REG_KBD_SPLIT 0x08 /* Left side */
96#define OPL3_COMPOSITE_SINE_WAVE_MODE 0x80 /* Don't use with OPL-3? */
97#define OPL3_KEYBOARD_SPLIT 0x40
98
99#define OPL3_REG_PERCUSSION 0xbd /* Left side only */
100#define OPL3_TREMOLO_DEPTH 0x80
101#define OPL3_VIBRATO_DEPTH 0x40
102#define OPL3_PERCUSSION_ENABLE 0x20
103#define OPL3_BASSDRUM_ON 0x10
104#define OPL3_SNAREDRUM_ON 0x08
105#define OPL3_TOMTOM_ON 0x04
106#define OPL3_CYMBAL_ON 0x02
107#define OPL3_HIHAT_ON 0x01
108
109/*
110 * Offsets to the register banks for operators. To get the
111 * register number just add the operator offset to the bank offset
112 *
113 * AM/VIB/EG/KSR/Multiple (0x20 to 0x35)
114 */
115#define OPL3_REG_AM_VIB 0x20
116#define OPL3_TREMOLO_ON 0x80
117#define OPL3_VIBRATO_ON 0x40
118#define OPL3_SUSTAIN_ON 0x20
119#define OPL3_KSR 0x10 /* Key scaling rate */
120#define OPL3_MULTIPLE_MASK 0x0f /* Frequency multiplier */
121
122 /*
123 * KSL/Total level (0x40 to 0x55)
124 */
125#define OPL3_REG_KSL_LEVEL 0x40
126#define OPL3_KSL_MASK 0xc0 /* Envelope scaling bits */
127#define OPL3_TOTAL_LEVEL_MASK 0x3f /* Strength (volume) of OP */
128
129/*
130 * Attack / Decay rate (0x60 to 0x75)
131 */
132#define OPL3_REG_ATTACK_DECAY 0x60
133#define OPL3_ATTACK_MASK 0xf0
134#define OPL3_DECAY_MASK 0x0f
135
136/*
137 * Sustain level / Release rate (0x80 to 0x95)
138 */
139#define OPL3_REG_SUSTAIN_RELEASE 0x80
140#define OPL3_SUSTAIN_MASK 0xf0
141#define OPL3_RELEASE_MASK 0x0f
142
143/*
144 * Wave select (0xE0 to 0xF5)
145 */
146#define OPL3_REG_WAVE_SELECT 0xe0
147#define OPL3_WAVE_SELECT_MASK 0x07
148
149/*
150 * Offsets to the register banks for voices. Just add to the
151 * voice number to get the register number.
152 *
153 * F-Number low bits (0xA0 to 0xA8).
154 */
155#define OPL3_REG_FNUM_LOW 0xa0
156
157/*
158 * F-number high bits / Key on / Block (octave) (0xB0 to 0xB8)
159 */
160#define OPL3_REG_KEYON_BLOCK 0xb0
161#define OPL3_KEYON_BIT 0x20
162#define OPL3_BLOCKNUM_MASK 0x1c
163#define OPL3_FNUM_HIGH_MASK 0x03
164
165/*
166 * Feedback / Connection (0xc0 to 0xc8)
167 *
168 * These registers have two new bits when the OPL-3 mode
169 * is selected. These bits controls connecting the voice
170 * to the stereo channels. For 4 OP voices this bit is
171 * defined in the second half of the voice (add 3 to the
172 * register offset).
173 *
174 * For 4 OP voices the connection bit is used in the
175 * both halves (gives 4 ways to connect the operators).
176 */
177#define OPL3_REG_FEEDBACK_CONNECTION 0xc0
178#define OPL3_FEEDBACK_MASK 0x0e /* Valid just for 1st OP of a voice */
179#define OPL3_CONNECTION_BIT 0x01
180/*
181 * In the 4 OP mode there is four possible configurations how the
182 * operators can be connected together (in 2 OP modes there is just
183 * AM or FM). The 4 OP connection mode is defined by the rightmost
184 * bit of the FEEDBACK_CONNECTION (0xC0-0xC8) on the both halves.
185 *
186 * First half Second half Mode
187 *
188 * +---+
189 * v |
190 * 0 0 >+-1-+--2--3--4-->
191 *
192 *
193 *
194 * +---+
195 * | |
196 * 0 1 >+-1-+--2-+
197 * |->
198 * >--3----4-+
199 *
200 * +---+
201 * | |
202 * 1 0 >+-1-+-----+
203 * |->
204 * >--2--3--4-+
205 *
206 * +---+
207 * | |
208 * 1 1 >+-1-+--+
209 * |
210 * >--2--3-+->
211 * |
212 * >--4----+
213 */
214#define OPL3_STEREO_BITS 0x30 /* OPL-3 only */
215#define OPL3_VOICE_TO_LEFT 0x10
216#define OPL3_VOICE_TO_RIGHT 0x20
217
218/*
219
220 */
221
222#define OPL3_LEFT 0x0000
223#define OPL3_RIGHT 0x0100
224
225#define OPL3_HW_AUTO 0x0000
226#define OPL3_HW_OPL2 0x0200
227#define OPL3_HW_OPL3 0x0300
228#define OPL3_HW_OPL3_SV 0x0301 /* S3 SonicVibes */
229#define OPL3_HW_OPL3_CS 0x0302 /* CS4232/CS4236+ */
230#define OPL3_HW_OPL3_FM801 0x0303 /* FM801 */
231#define OPL3_HW_OPL3_CS4281 0x0304 /* CS4281 */
232#define OPL3_HW_OPL3_PC98 0x0305 /* PC9800 */
233#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
240typedef struct snd_opl3 opl3_t;
241
242/*
243 * A structure to keep track of each hardware voice
244 */
245typedef struct snd_opl3_voice {
246 int state; /* status */
247#define SNDRV_OPL3_ST_OFF 0 /* Not playing */
248#define SNDRV_OPL3_ST_ON_2OP 1 /* 2op voice is allocated */
249#define SNDRV_OPL3_ST_ON_4OP 2 /* 4op voice is allocated */
250#define SNDRV_OPL3_ST_NOT_AVAIL -1 /* voice is not available */
251
252 unsigned int time; /* An allocation time */
253 unsigned char note; /* Note currently assigned to this voice */
254
255 unsigned long note_off; /* note-off time */
256 int note_off_check; /* check note-off time */
257
258 unsigned char keyon_reg; /* KON register shadow */
259
260 snd_midi_channel_t *chan; /* Midi channel for this note */
261} snd_opl3_voice_t;
262
263struct snd_opl3 {
264 unsigned long l_port;
265 unsigned long r_port;
266 struct resource *res_l_port;
267 struct resource *res_r_port;
268 unsigned short hardware;
269 /* hardware access */
270 void (*command) (opl3_t * opl3, unsigned short cmd, unsigned char val);
271 unsigned short timer_enable;
272 int seq_dev_num; /* sequencer device number */
273 snd_timer_t *timer1;
274 snd_timer_t *timer2;
275 spinlock_t timer_lock;
276
277 void *private_data;
278 void (*private_free)(opl3_t *);
279
280 spinlock_t reg_lock;
281 snd_card_t *card; /* The card that this belongs to */
282 int used; /* usage flag - exclusive */
283 unsigned char fm_mode; /* OPL mode, see SNDRV_DM_FM_MODE_XXX */
284 unsigned char rhythm; /* percussion mode flag */
285 unsigned char max_voices; /* max number of voices */
286#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
287#define SNDRV_OPL3_MODE_SYNTH 0 /* OSS - voices allocated by application */
288#define SNDRV_OPL3_MODE_SEQ 1 /* ALSA - driver handles voice allocation */
289 int synth_mode; /* synth mode */
290 int seq_client;
291
292 snd_seq_device_t *seq_dev; /* sequencer device */
293 snd_midi_channel_set_t * chset;
294
295#ifdef CONFIG_SND_SEQUENCER_OSS
296 snd_seq_device_t *oss_seq_dev; /* OSS sequencer device */
297 snd_midi_channel_set_t * oss_chset;
298#endif
299
300 snd_seq_kinstr_ops_t fm_ops;
301 snd_seq_kinstr_list_t *ilist;
302
303 snd_opl3_voice_t voices[MAX_OPL3_VOICES]; /* Voices (OPL3 'channel') */
304 int use_time; /* allocation counter */
305
306 unsigned short connection_reg; /* connection reg shadow */
307 unsigned char drum_reg; /* percussion reg shadow */
308
309 spinlock_t voice_lock; /* Lock for voice access */
310
311 struct timer_list tlist; /* timer for note-offs and effects */
312 int sys_timer_status; /* system timer run status */
313 spinlock_t sys_timer_lock; /* Lock for system timer access */
314#endif
315 struct semaphore access_mutex; /* locking */
316};
317
318/* opl3.c */
319void snd_opl3_interrupt(snd_hwdep_t * hw);
320int snd_opl3_new(snd_card_t *card, unsigned short hardware, opl3_t **ropl3);
321int snd_opl3_init(opl3_t *opl3);
322int snd_opl3_create(snd_card_t * card,
323 unsigned long l_port, unsigned long r_port,
324 unsigned short hardware,
325 int integrated,
326 opl3_t ** opl3);
327int snd_opl3_timer_new(opl3_t * opl3, int timer1_dev, int timer2_dev);
328int snd_opl3_hwdep_new(opl3_t * opl3, int device, int seq_device,
329 snd_hwdep_t ** rhwdep);
330
331/* opl3_synth */
332int snd_opl3_open(snd_hwdep_t * hw, struct file *file);
333int snd_opl3_ioctl(snd_hwdep_t * hw, struct file *file,
334 unsigned int cmd, unsigned long arg);
335int snd_opl3_release(snd_hwdep_t * hw, struct file *file);
336
337void snd_opl3_reset(opl3_t * opl3);
338
339#endif /* __SOUND_OPL3_H */