opl3_seq.c 6.7 KB

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