opl3.h 12 KB

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