opl3_seq.c 7.4 KB

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