opl3.h 11 KB

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