opl4_seq.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * OPL4 sequencer functions
  3. *
  4. * Copyright (c) 2003 by Clemens Ladisch <clemens@ladisch.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * Alternatively, this software may be distributed and/or modified under the
  17. * terms of the GNU General Public License as published by the Free Software
  18. * Foundation; either version 2 of the License, or (at your option) any later
  19. * version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  25. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "opl4_local.h"
  34. #include <linux/init.h>
  35. #include <linux/moduleparam.h>
  36. #include <sound/initval.h>
  37. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  38. MODULE_DESCRIPTION("OPL4 wavetable synth driver");
  39. MODULE_LICENSE("Dual BSD/GPL");
  40. int volume_boost = 8;
  41. module_param(volume_boost, int, 0644);
  42. MODULE_PARM_DESC(volume_boost, "Additional volume for OPL4 wavetable sounds.");
  43. static int snd_opl4_seq_use_inc(opl4_t *opl4)
  44. {
  45. if (!try_module_get(opl4->card->module))
  46. return -EFAULT;
  47. return 0;
  48. }
  49. static void snd_opl4_seq_use_dec(opl4_t *opl4)
  50. {
  51. module_put(opl4->card->module);
  52. }
  53. static int snd_opl4_seq_use(void *private_data, snd_seq_port_subscribe_t *info)
  54. {
  55. opl4_t *opl4 = private_data;
  56. int err;
  57. down(&opl4->access_mutex);
  58. if (opl4->used) {
  59. up(&opl4->access_mutex);
  60. return -EBUSY;
  61. }
  62. opl4->used++;
  63. if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
  64. err = snd_opl4_seq_use_inc(opl4);
  65. if (err < 0) {
  66. up(&opl4->access_mutex);
  67. return err;
  68. }
  69. }
  70. up(&opl4->access_mutex);
  71. snd_opl4_synth_reset(opl4);
  72. return 0;
  73. }
  74. static int snd_opl4_seq_unuse(void *private_data, snd_seq_port_subscribe_t *info)
  75. {
  76. opl4_t *opl4 = private_data;
  77. snd_opl4_synth_shutdown(opl4);
  78. down(&opl4->access_mutex);
  79. opl4->used--;
  80. up(&opl4->access_mutex);
  81. if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
  82. snd_opl4_seq_use_dec(opl4);
  83. return 0;
  84. }
  85. static snd_midi_op_t opl4_ops = {
  86. .note_on = snd_opl4_note_on,
  87. .note_off = snd_opl4_note_off,
  88. .note_terminate = snd_opl4_terminate_note,
  89. .control = snd_opl4_control,
  90. .sysex = snd_opl4_sysex,
  91. };
  92. static int snd_opl4_seq_event_input(snd_seq_event_t *ev, int direct,
  93. void *private_data, int atomic, int hop)
  94. {
  95. opl4_t *opl4 = private_data;
  96. snd_midi_process_event(&opl4_ops, ev, opl4->chset);
  97. return 0;
  98. }
  99. static void snd_opl4_seq_free_port(void *private_data)
  100. {
  101. opl4_t *opl4 = private_data;
  102. snd_midi_channel_free_set(opl4->chset);
  103. }
  104. static int snd_opl4_seq_new_device(snd_seq_device_t *dev)
  105. {
  106. opl4_t *opl4;
  107. int client;
  108. snd_seq_client_callback_t callbacks;
  109. snd_seq_client_info_t cinfo;
  110. snd_seq_port_callback_t pcallbacks;
  111. opl4 = *(opl4_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  112. if (!opl4)
  113. return -EINVAL;
  114. if (snd_yrw801_detect(opl4) < 0)
  115. return -ENODEV;
  116. opl4->chset = snd_midi_channel_alloc_set(16);
  117. if (!opl4->chset)
  118. return -ENOMEM;
  119. opl4->chset->private_data = opl4;
  120. /* allocate new client */
  121. memset(&callbacks, 0, sizeof(callbacks));
  122. callbacks.private_data = opl4;
  123. callbacks.allow_output = callbacks.allow_input = 1;
  124. client = snd_seq_create_kernel_client(opl4->card, opl4->seq_dev_num, &callbacks);
  125. if (client < 0) {
  126. snd_midi_channel_free_set(opl4->chset);
  127. return client;
  128. }
  129. opl4->seq_client = client;
  130. opl4->chset->client = client;
  131. /* change name of client */
  132. memset(&cinfo, 0, sizeof(cinfo));
  133. cinfo.client = client;
  134. cinfo.type = KERNEL_CLIENT;
  135. strcpy(cinfo.name, "OPL4 Wavetable");
  136. snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
  137. /* create new port */
  138. memset(&pcallbacks, 0, sizeof(pcallbacks));
  139. pcallbacks.owner = THIS_MODULE;
  140. pcallbacks.use = snd_opl4_seq_use;
  141. pcallbacks.unuse = snd_opl4_seq_unuse;
  142. pcallbacks.event_input = snd_opl4_seq_event_input;
  143. pcallbacks.private_free = snd_opl4_seq_free_port;
  144. pcallbacks.private_data = opl4;
  145. opl4->chset->port = snd_seq_event_port_attach(client, &pcallbacks,
  146. SNDRV_SEQ_PORT_CAP_WRITE |
  147. SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
  148. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  149. SNDRV_SEQ_PORT_TYPE_MIDI_GM,
  150. 16, 24,
  151. "OPL4 Wavetable Port");
  152. if (opl4->chset->port < 0) {
  153. int err = opl4->chset->port;
  154. snd_midi_channel_free_set(opl4->chset);
  155. snd_seq_delete_kernel_client(client);
  156. opl4->seq_client = -1;
  157. return err;
  158. }
  159. return 0;
  160. }
  161. static int snd_opl4_seq_delete_device(snd_seq_device_t *dev)
  162. {
  163. opl4_t *opl4;
  164. opl4 = *(opl4_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  165. if (!opl4)
  166. return -EINVAL;
  167. if (opl4->seq_client >= 0) {
  168. snd_seq_delete_kernel_client(opl4->seq_client);
  169. opl4->seq_client = -1;
  170. }
  171. return 0;
  172. }
  173. static int __init alsa_opl4_synth_init(void)
  174. {
  175. static snd_seq_dev_ops_t ops = {
  176. snd_opl4_seq_new_device,
  177. snd_opl4_seq_delete_device
  178. };
  179. return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL4, &ops,
  180. sizeof(opl4_t*));
  181. }
  182. static void __exit alsa_opl4_synth_exit(void)
  183. {
  184. snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL4);
  185. }
  186. module_init(alsa_opl4_synth_init)
  187. module_exit(alsa_opl4_synth_exit)