opl3.h 12 KB

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