opl3.h 12 KB

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