opl3_seq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
  3. *
  4. * Midi Sequencer interface routines for OPL2/OPL3/OPL4 FM
  5. *
  6. * OPL2/3 FM instrument loader:
  7. * alsa-tools/seq/sbiload/
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include "opl3_voice.h"
  25. #include <linux/init.h>
  26. #include <linux/moduleparam.h>
  27. #include <sound/initval.h>
  28. MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
  29. MODULE_LICENSE("GPL");
  30. MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth");
  31. int use_internal_drums = 0;
  32. module_param(use_internal_drums, bool, 0444);
  33. MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums.");
  34. int snd_opl3_synth_use_inc(struct snd_opl3 * opl3)
  35. {
  36. if (!try_module_get(opl3->card->module))
  37. return -EFAULT;
  38. return 0;
  39. }
  40. void snd_opl3_synth_use_dec(struct snd_opl3 * opl3)
  41. {
  42. module_put(opl3->card->module);
  43. }
  44. int snd_opl3_synth_setup(struct snd_opl3 * opl3)
  45. {
  46. int idx;
  47. mutex_lock(&opl3->access_mutex);
  48. if (opl3->used) {
  49. mutex_unlock(&opl3->access_mutex);
  50. return -EBUSY;
  51. }
  52. opl3->used++;
  53. mutex_unlock(&opl3->access_mutex);
  54. snd_opl3_reset(opl3);
  55. for (idx = 0; idx < MAX_OPL3_VOICES; idx++) {
  56. opl3->voices[idx].state = SNDRV_OPL3_ST_OFF;
  57. opl3->voices[idx].time = 0;
  58. opl3->voices[idx].keyon_reg = 0x00;
  59. }
  60. opl3->use_time = 0;
  61. opl3->connection_reg = 0x00;
  62. if (opl3->hardware >= OPL3_HW_OPL3) {
  63. /* Clear 4-op connections */
  64. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT,
  65. opl3->connection_reg);
  66. opl3->max_voices = MAX_OPL3_VOICES;
  67. }
  68. return 0;
  69. }
  70. void snd_opl3_synth_cleanup(struct snd_opl3 * opl3)
  71. {
  72. unsigned long flags;
  73. /* Stop system timer */
  74. spin_lock_irqsave(&opl3->sys_timer_lock, flags);
  75. if (opl3->sys_timer_status) {
  76. del_timer(&opl3->tlist);
  77. opl3->sys_timer_status = 0;
  78. }
  79. spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
  80. snd_opl3_reset(opl3);
  81. mutex_lock(&opl3->access_mutex);
  82. opl3->used--;
  83. mutex_unlock(&opl3->access_mutex);
  84. }
  85. static int snd_opl3_synth_use(void *private_data, struct snd_seq_port_subscribe * info)
  86. {
  87. struct snd_opl3 *opl3 = private_data;
  88. int err;
  89. if ((err = snd_opl3_synth_setup(opl3)) < 0)
  90. return err;
  91. if (use_internal_drums) {
  92. /* Percussion mode */
  93. opl3->voices[6].state = opl3->voices[7].state =
  94. opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
  95. snd_opl3_load_drums(opl3);
  96. opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
  97. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
  98. } else {
  99. opl3->drum_reg = 0x00;
  100. }
  101. if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
  102. if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
  103. return err;
  104. }
  105. opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
  106. return 0;
  107. }
  108. static int snd_opl3_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info)
  109. {
  110. struct snd_opl3 *opl3 = private_data;
  111. snd_opl3_synth_cleanup(opl3);
  112. if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
  113. snd_opl3_synth_use_dec(opl3);
  114. return 0;
  115. }
  116. /*
  117. * MIDI emulation operators
  118. */
  119. struct snd_midi_op opl3_ops = {
  120. .note_on = snd_opl3_note_on,
  121. .note_off = snd_opl3_note_off,
  122. .key_press = snd_opl3_key_press,
  123. .note_terminate = snd_opl3_terminate_note,
  124. .control = snd_opl3_control,
  125. .nrpn = snd_opl3_nrpn,
  126. .sysex = snd_opl3_sysex,
  127. };
  128. static int snd_opl3_synth_event_input(struct snd_seq_event * ev, int direct,
  129. void *private_data, int atomic, int hop)
  130. {
  131. struct snd_opl3 *opl3 = private_data;
  132. if (ev->type >= SNDRV_SEQ_EVENT_INSTR_BEGIN &&
  133. ev->type <= SNDRV_SEQ_EVENT_INSTR_CHANGE) {
  134. if (direct) {
  135. snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, ev,
  136. opl3->seq_client, atomic, hop);
  137. }
  138. } else {
  139. snd_midi_process_event(&opl3_ops, ev, opl3->chset);
  140. }
  141. return 0;
  142. }
  143. /* ------------------------------ */
  144. static void snd_opl3_synth_free_port(void *private_data)
  145. {
  146. struct snd_opl3 *opl3 = private_data;
  147. snd_midi_channel_free_set(opl3->chset);
  148. }
  149. static int snd_opl3_synth_create_port(struct snd_opl3 * opl3)
  150. {
  151. struct snd_seq_port_callback callbacks;
  152. char name[32];
  153. int voices, opl_ver;
  154. voices = (opl3->hardware < OPL3_HW_OPL3) ?
  155. MAX_OPL2_VOICES : MAX_OPL3_VOICES;
  156. opl3->chset = snd_midi_channel_alloc_set(16);
  157. if (opl3->chset == NULL)
  158. return -ENOMEM;
  159. opl3->chset->private_data = opl3;
  160. memset(&callbacks, 0, sizeof(callbacks));
  161. callbacks.owner = THIS_MODULE;
  162. callbacks.use = snd_opl3_synth_use;
  163. callbacks.unuse = snd_opl3_synth_unuse;
  164. callbacks.event_input = snd_opl3_synth_event_input;
  165. callbacks.private_free = snd_opl3_synth_free_port;
  166. callbacks.private_data = opl3;
  167. opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
  168. sprintf(name, "OPL%i FM Port", opl_ver);
  169. opl3->chset->client = opl3->seq_client;
  170. opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
  171. SNDRV_SEQ_PORT_CAP_WRITE |
  172. SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
  173. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  174. SNDRV_SEQ_PORT_TYPE_MIDI_GM |
  175. SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
  176. SNDRV_SEQ_PORT_TYPE_HARDWARE |
  177. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
  178. 16, voices,
  179. name);
  180. if (opl3->chset->port < 0) {
  181. int port;
  182. port = opl3->chset->port;
  183. snd_midi_channel_free_set(opl3->chset);
  184. return port;
  185. }
  186. return 0;
  187. }
  188. /* ------------------------------ */
  189. static int snd_opl3_seq_new_device(struct snd_seq_device *dev)
  190. {
  191. struct snd_opl3 *opl3;
  192. int client, err;
  193. char name[32];
  194. int opl_ver;
  195. opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  196. if (opl3 == NULL)
  197. return -EINVAL;
  198. spin_lock_init(&opl3->voice_lock);
  199. opl3->seq_client = -1;
  200. /* allocate new client */
  201. opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
  202. sprintf(name, "OPL%i FM synth", opl_ver);
  203. client = opl3->seq_client =
  204. snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num,
  205. name);
  206. if (client < 0)
  207. return client;
  208. if ((err = snd_opl3_synth_create_port(opl3)) < 0) {
  209. snd_seq_delete_kernel_client(client);
  210. opl3->seq_client = -1;
  211. return err;
  212. }
  213. /* initialize instrument list */
  214. opl3->ilist = snd_seq_instr_list_new();
  215. if (opl3->ilist == NULL) {
  216. snd_seq_delete_kernel_client(client);
  217. opl3->seq_client = -1;
  218. return -ENOMEM;
  219. }
  220. opl3->ilist->flags = SNDRV_SEQ_INSTR_FLG_DIRECT;
  221. snd_seq_fm_init(&opl3->fm_ops, NULL);
  222. /* setup system timer */
  223. init_timer(&opl3->tlist);
  224. opl3->tlist.function = snd_opl3_timer_func;
  225. opl3->tlist.data = (unsigned long) opl3;
  226. spin_lock_init(&opl3->sys_timer_lock);
  227. opl3->sys_timer_status = 0;
  228. #ifdef CONFIG_SND_SEQUENCER_OSS
  229. snd_opl3_init_seq_oss(opl3, name);
  230. #endif
  231. return 0;
  232. }
  233. static int snd_opl3_seq_delete_device(struct snd_seq_device *dev)
  234. {
  235. struct snd_opl3 *opl3;
  236. opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  237. if (opl3 == NULL)
  238. return -EINVAL;
  239. #ifdef CONFIG_SND_SEQUENCER_OSS
  240. snd_opl3_free_seq_oss(opl3);
  241. #endif
  242. if (opl3->seq_client >= 0) {
  243. snd_seq_delete_kernel_client(opl3->seq_client);
  244. opl3->seq_client = -1;
  245. }
  246. if (opl3->ilist)
  247. snd_seq_instr_list_free(&opl3->ilist);
  248. return 0;
  249. }
  250. static int __init alsa_opl3_seq_init(void)
  251. {
  252. static struct snd_seq_dev_ops ops =
  253. {
  254. snd_opl3_seq_new_device,
  255. snd_opl3_seq_delete_device
  256. };
  257. return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL3, &ops,
  258. sizeof(struct snd_opl3 *));
  259. }
  260. static void __exit alsa_opl3_seq_exit(void)
  261. {
  262. snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL3);
  263. }
  264. module_init(alsa_opl3_seq_init)
  265. module_exit(alsa_opl3_seq_exit)