opl3_seq.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. struct snd_hwdep *hwdep = opl3->hwdep;
  48. mutex_lock(&hwdep->open_mutex);
  49. if (hwdep->used) {
  50. mutex_unlock(&hwdep->open_mutex);
  51. return -EBUSY;
  52. }
  53. hwdep->used++;
  54. mutex_unlock(&hwdep->open_mutex);
  55. snd_opl3_reset(opl3);
  56. for (idx = 0; idx < MAX_OPL3_VOICES; idx++) {
  57. opl3->voices[idx].state = SNDRV_OPL3_ST_OFF;
  58. opl3->voices[idx].time = 0;
  59. opl3->voices[idx].keyon_reg = 0x00;
  60. }
  61. opl3->use_time = 0;
  62. opl3->connection_reg = 0x00;
  63. if (opl3->hardware >= OPL3_HW_OPL3) {
  64. /* Clear 4-op connections */
  65. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT,
  66. opl3->connection_reg);
  67. opl3->max_voices = MAX_OPL3_VOICES;
  68. }
  69. return 0;
  70. }
  71. void snd_opl3_synth_cleanup(struct snd_opl3 * opl3)
  72. {
  73. unsigned long flags;
  74. struct snd_hwdep *hwdep;
  75. /* Stop system timer */
  76. spin_lock_irqsave(&opl3->sys_timer_lock, flags);
  77. if (opl3->sys_timer_status) {
  78. del_timer(&opl3->tlist);
  79. opl3->sys_timer_status = 0;
  80. }
  81. spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
  82. snd_opl3_reset(opl3);
  83. hwdep = opl3->hwdep;
  84. mutex_lock(&hwdep->open_mutex);
  85. hwdep->used--;
  86. mutex_unlock(&hwdep->open_mutex);
  87. wake_up(&hwdep->open_wait);
  88. }
  89. static int snd_opl3_synth_use(void *private_data, struct snd_seq_port_subscribe * info)
  90. {
  91. struct snd_opl3 *opl3 = private_data;
  92. int err;
  93. if ((err = snd_opl3_synth_setup(opl3)) < 0)
  94. return err;
  95. if (use_internal_drums) {
  96. /* Percussion mode */
  97. opl3->voices[6].state = opl3->voices[7].state =
  98. opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
  99. snd_opl3_load_drums(opl3);
  100. opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
  101. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
  102. } else {
  103. opl3->drum_reg = 0x00;
  104. }
  105. if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
  106. if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
  107. return err;
  108. }
  109. opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
  110. return 0;
  111. }
  112. static int snd_opl3_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info)
  113. {
  114. struct snd_opl3 *opl3 = private_data;
  115. snd_opl3_synth_cleanup(opl3);
  116. if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
  117. snd_opl3_synth_use_dec(opl3);
  118. return 0;
  119. }
  120. /*
  121. * MIDI emulation operators
  122. */
  123. struct snd_midi_op opl3_ops = {
  124. .note_on = snd_opl3_note_on,
  125. .note_off = snd_opl3_note_off,
  126. .key_press = snd_opl3_key_press,
  127. .note_terminate = snd_opl3_terminate_note,
  128. .control = snd_opl3_control,
  129. .nrpn = snd_opl3_nrpn,
  130. .sysex = snd_opl3_sysex,
  131. };
  132. static int snd_opl3_synth_event_input(struct snd_seq_event * ev, int direct,
  133. void *private_data, int atomic, int hop)
  134. {
  135. struct snd_opl3 *opl3 = private_data;
  136. snd_midi_process_event(&opl3_ops, ev, opl3->chset);
  137. return 0;
  138. }
  139. /* ------------------------------ */
  140. static void snd_opl3_synth_free_port(void *private_data)
  141. {
  142. struct snd_opl3 *opl3 = private_data;
  143. snd_midi_channel_free_set(opl3->chset);
  144. }
  145. static int snd_opl3_synth_create_port(struct snd_opl3 * opl3)
  146. {
  147. struct snd_seq_port_callback callbacks;
  148. char name[32];
  149. int voices, opl_ver;
  150. voices = (opl3->hardware < OPL3_HW_OPL3) ?
  151. MAX_OPL2_VOICES : MAX_OPL3_VOICES;
  152. opl3->chset = snd_midi_channel_alloc_set(16);
  153. if (opl3->chset == NULL)
  154. return -ENOMEM;
  155. opl3->chset->private_data = opl3;
  156. memset(&callbacks, 0, sizeof(callbacks));
  157. callbacks.owner = THIS_MODULE;
  158. callbacks.use = snd_opl3_synth_use;
  159. callbacks.unuse = snd_opl3_synth_unuse;
  160. callbacks.event_input = snd_opl3_synth_event_input;
  161. callbacks.private_free = snd_opl3_synth_free_port;
  162. callbacks.private_data = opl3;
  163. opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
  164. sprintf(name, "OPL%i FM Port", opl_ver);
  165. opl3->chset->client = opl3->seq_client;
  166. opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
  167. SNDRV_SEQ_PORT_CAP_WRITE |
  168. SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
  169. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  170. SNDRV_SEQ_PORT_TYPE_MIDI_GM |
  171. SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
  172. SNDRV_SEQ_PORT_TYPE_HARDWARE |
  173. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
  174. 16, voices,
  175. name);
  176. if (opl3->chset->port < 0) {
  177. int port;
  178. port = opl3->chset->port;
  179. snd_midi_channel_free_set(opl3->chset);
  180. return port;
  181. }
  182. return 0;
  183. }
  184. /* ------------------------------ */
  185. static int snd_opl3_seq_new_device(struct snd_seq_device *dev)
  186. {
  187. struct snd_opl3 *opl3;
  188. int client, err;
  189. char name[32];
  190. int opl_ver;
  191. opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  192. if (opl3 == NULL)
  193. return -EINVAL;
  194. spin_lock_init(&opl3->voice_lock);
  195. opl3->seq_client = -1;
  196. /* allocate new client */
  197. opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
  198. sprintf(name, "OPL%i FM synth", opl_ver);
  199. client = opl3->seq_client =
  200. snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num,
  201. name);
  202. if (client < 0)
  203. return client;
  204. if ((err = snd_opl3_synth_create_port(opl3)) < 0) {
  205. snd_seq_delete_kernel_client(client);
  206. opl3->seq_client = -1;
  207. return err;
  208. }
  209. /* setup system timer */
  210. init_timer(&opl3->tlist);
  211. opl3->tlist.function = snd_opl3_timer_func;
  212. opl3->tlist.data = (unsigned long) opl3;
  213. spin_lock_init(&opl3->sys_timer_lock);
  214. opl3->sys_timer_status = 0;
  215. #ifdef CONFIG_SND_SEQUENCER_OSS
  216. snd_opl3_init_seq_oss(opl3, name);
  217. #endif
  218. return 0;
  219. }
  220. static int snd_opl3_seq_delete_device(struct snd_seq_device *dev)
  221. {
  222. struct snd_opl3 *opl3;
  223. opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  224. if (opl3 == NULL)
  225. return -EINVAL;
  226. #ifdef CONFIG_SND_SEQUENCER_OSS
  227. snd_opl3_free_seq_oss(opl3);
  228. #endif
  229. if (opl3->seq_client >= 0) {
  230. snd_seq_delete_kernel_client(opl3->seq_client);
  231. opl3->seq_client = -1;
  232. }
  233. return 0;
  234. }
  235. static int __init alsa_opl3_seq_init(void)
  236. {
  237. static struct snd_seq_dev_ops ops =
  238. {
  239. snd_opl3_seq_new_device,
  240. snd_opl3_seq_delete_device
  241. };
  242. return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL3, &ops,
  243. sizeof(struct snd_opl3 *));
  244. }
  245. static void __exit alsa_opl3_seq_exit(void)
  246. {
  247. snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL3);
  248. }
  249. module_init(alsa_opl3_seq_init)
  250. module_exit(alsa_opl3_seq_exit)