opl3_seq.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <linux/module.h>
  28. #include <sound/initval.h>
  29. MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
  30. MODULE_LICENSE("GPL");
  31. MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth");
  32. bool use_internal_drums = 0;
  33. module_param(use_internal_drums, bool, 0444);
  34. MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums.");
  35. int snd_opl3_synth_use_inc(struct snd_opl3 * opl3)
  36. {
  37. if (!try_module_get(opl3->card->module))
  38. return -EFAULT;
  39. return 0;
  40. }
  41. void snd_opl3_synth_use_dec(struct snd_opl3 * opl3)
  42. {
  43. module_put(opl3->card->module);
  44. }
  45. int snd_opl3_synth_setup(struct snd_opl3 * opl3)
  46. {
  47. int idx;
  48. struct snd_hwdep *hwdep = opl3->hwdep;
  49. mutex_lock(&hwdep->open_mutex);
  50. if (hwdep->used) {
  51. mutex_unlock(&hwdep->open_mutex);
  52. return -EBUSY;
  53. }
  54. hwdep->used++;
  55. mutex_unlock(&hwdep->open_mutex);
  56. snd_opl3_reset(opl3);
  57. for (idx = 0; idx < MAX_OPL3_VOICES; idx++) {
  58. opl3->voices[idx].state = SNDRV_OPL3_ST_OFF;
  59. opl3->voices[idx].time = 0;
  60. opl3->voices[idx].keyon_reg = 0x00;
  61. }
  62. opl3->use_time = 0;
  63. opl3->connection_reg = 0x00;
  64. if (opl3->hardware >= OPL3_HW_OPL3) {
  65. /* Clear 4-op connections */
  66. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT,
  67. opl3->connection_reg);
  68. opl3->max_voices = MAX_OPL3_VOICES;
  69. }
  70. return 0;
  71. }
  72. void snd_opl3_synth_cleanup(struct snd_opl3 * opl3)
  73. {
  74. unsigned long flags;
  75. struct snd_hwdep *hwdep;
  76. /* Stop system timer */
  77. spin_lock_irqsave(&opl3->sys_timer_lock, flags);
  78. if (opl3->sys_timer_status) {
  79. del_timer(&opl3->tlist);
  80. opl3->sys_timer_status = 0;
  81. }
  82. spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
  83. snd_opl3_reset(opl3);
  84. hwdep = opl3->hwdep;
  85. mutex_lock(&hwdep->open_mutex);
  86. hwdep->used--;
  87. mutex_unlock(&hwdep->open_mutex);
  88. wake_up(&hwdep->open_wait);
  89. }
  90. static int snd_opl3_synth_use(void *private_data, struct snd_seq_port_subscribe * info)
  91. {
  92. struct snd_opl3 *opl3 = private_data;
  93. int err;
  94. if ((err = snd_opl3_synth_setup(opl3)) < 0)
  95. return err;
  96. if (use_internal_drums) {
  97. /* Percussion mode */
  98. opl3->voices[6].state = opl3->voices[7].state =
  99. opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
  100. snd_opl3_load_drums(opl3);
  101. opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
  102. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
  103. } else {
  104. opl3->drum_reg = 0x00;
  105. }
  106. if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
  107. if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
  108. return err;
  109. }
  110. opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
  111. return 0;
  112. }
  113. static int snd_opl3_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info)
  114. {
  115. struct snd_opl3 *opl3 = private_data;
  116. snd_opl3_synth_cleanup(opl3);
  117. if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
  118. snd_opl3_synth_use_dec(opl3);
  119. return 0;
  120. }
  121. /*
  122. * MIDI emulation operators
  123. */
  124. struct snd_midi_op opl3_ops = {
  125. .note_on = snd_opl3_note_on,
  126. .note_off = snd_opl3_note_off,
  127. .key_press = snd_opl3_key_press,
  128. .note_terminate = snd_opl3_terminate_note,
  129. .control = snd_opl3_control,
  130. .nrpn = snd_opl3_nrpn,
  131. .sysex = snd_opl3_sysex,
  132. };
  133. static int snd_opl3_synth_event_input(struct snd_seq_event * ev, int direct,
  134. void *private_data, int atomic, int hop)
  135. {
  136. struct snd_opl3 *opl3 = private_data;
  137. snd_midi_process_event(&opl3_ops, ev, opl3->chset);
  138. return 0;
  139. }
  140. /* ------------------------------ */
  141. static void snd_opl3_synth_free_port(void *private_data)
  142. {
  143. struct snd_opl3 *opl3 = private_data;
  144. snd_midi_channel_free_set(opl3->chset);
  145. }
  146. static int snd_opl3_synth_create_port(struct snd_opl3 * opl3)
  147. {
  148. struct snd_seq_port_callback callbacks;
  149. char name[32];
  150. int voices, opl_ver;
  151. voices = (opl3->hardware < OPL3_HW_OPL3) ?
  152. MAX_OPL2_VOICES : MAX_OPL3_VOICES;
  153. opl3->chset = snd_midi_channel_alloc_set(16);
  154. if (opl3->chset == NULL)
  155. return -ENOMEM;
  156. opl3->chset->private_data = opl3;
  157. memset(&callbacks, 0, sizeof(callbacks));
  158. callbacks.owner = THIS_MODULE;
  159. callbacks.use = snd_opl3_synth_use;
  160. callbacks.unuse = snd_opl3_synth_unuse;
  161. callbacks.event_input = snd_opl3_synth_event_input;
  162. callbacks.private_free = snd_opl3_synth_free_port;
  163. callbacks.private_data = opl3;
  164. opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
  165. sprintf(name, "OPL%i FM Port", opl_ver);
  166. opl3->chset->client = opl3->seq_client;
  167. opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
  168. SNDRV_SEQ_PORT_CAP_WRITE |
  169. SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
  170. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  171. SNDRV_SEQ_PORT_TYPE_MIDI_GM |
  172. SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
  173. SNDRV_SEQ_PORT_TYPE_HARDWARE |
  174. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
  175. 16, voices,
  176. name);
  177. if (opl3->chset->port < 0) {
  178. int port;
  179. port = opl3->chset->port;
  180. snd_midi_channel_free_set(opl3->chset);
  181. return port;
  182. }
  183. return 0;
  184. }
  185. /* ------------------------------ */
  186. static int snd_opl3_seq_probe(struct device *_dev)
  187. {
  188. struct snd_seq_device *dev = to_seq_dev(_dev);
  189. struct snd_opl3 *opl3;
  190. int client, err;
  191. char name[32];
  192. int opl_ver;
  193. opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  194. if (opl3 == NULL)
  195. return -EINVAL;
  196. spin_lock_init(&opl3->voice_lock);
  197. opl3->seq_client = -1;
  198. /* allocate new client */
  199. opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
  200. sprintf(name, "OPL%i FM synth", opl_ver);
  201. client = opl3->seq_client =
  202. snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num,
  203. name);
  204. if (client < 0)
  205. return client;
  206. if ((err = snd_opl3_synth_create_port(opl3)) < 0) {
  207. snd_seq_delete_kernel_client(client);
  208. opl3->seq_client = -1;
  209. return err;
  210. }
  211. /* setup system timer */
  212. setup_timer(&opl3->tlist, snd_opl3_timer_func, (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_remove(struct device *_dev)
  221. {
  222. struct snd_seq_device *dev = to_seq_dev(_dev);
  223. struct snd_opl3 *opl3;
  224. opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  225. if (opl3 == NULL)
  226. return -EINVAL;
  227. #ifdef CONFIG_SND_SEQUENCER_OSS
  228. snd_opl3_free_seq_oss(opl3);
  229. #endif
  230. if (opl3->seq_client >= 0) {
  231. snd_seq_delete_kernel_client(opl3->seq_client);
  232. opl3->seq_client = -1;
  233. }
  234. return 0;
  235. }
  236. static struct snd_seq_driver opl3_seq_driver = {
  237. .driver = {
  238. .name = KBUILD_MODNAME,
  239. .probe = snd_opl3_seq_probe,
  240. .remove = snd_opl3_seq_remove,
  241. },
  242. .id = SNDRV_SEQ_DEV_ID_OPL3,
  243. .argsize = sizeof(struct snd_opl3 *),
  244. };
  245. module_snd_seq_driver(opl3_seq_driver);