opl3_oss.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Interface for OSS sequencer emulation
  3. *
  4. * Copyright (C) 2000 Uros Bizjak <uros@kss-loka.si>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/export.h>
  21. #include "opl3_voice.h"
  22. static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
  23. static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg);
  24. static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg);
  25. static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, const char __user *buf, int offs, int count);
  26. static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg);
  27. /* operators */
  28. static struct snd_seq_oss_callback oss_callback = {
  29. .owner = THIS_MODULE,
  30. .open = snd_opl3_open_seq_oss,
  31. .close = snd_opl3_close_seq_oss,
  32. .ioctl = snd_opl3_ioctl_seq_oss,
  33. .load_patch = snd_opl3_load_patch_seq_oss,
  34. .reset = snd_opl3_reset_seq_oss,
  35. };
  36. static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
  37. void *private_data, int atomic, int hop)
  38. {
  39. struct snd_opl3 *opl3 = private_data;
  40. if (ev->type != SNDRV_SEQ_EVENT_OSS)
  41. snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
  42. return 0;
  43. }
  44. /* ------------------------------ */
  45. static void snd_opl3_oss_free_port(void *private_data)
  46. {
  47. struct snd_opl3 *opl3 = private_data;
  48. snd_midi_channel_free_set(opl3->oss_chset);
  49. }
  50. static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
  51. {
  52. struct snd_seq_port_callback callbacks;
  53. char name[32];
  54. int voices, opl_ver;
  55. voices = (opl3->hardware < OPL3_HW_OPL3) ?
  56. MAX_OPL2_VOICES : MAX_OPL3_VOICES;
  57. opl3->oss_chset = snd_midi_channel_alloc_set(voices);
  58. if (opl3->oss_chset == NULL)
  59. return -ENOMEM;
  60. opl3->oss_chset->private_data = opl3;
  61. memset(&callbacks, 0, sizeof(callbacks));
  62. callbacks.owner = THIS_MODULE;
  63. callbacks.event_input = snd_opl3_oss_event_input;
  64. callbacks.private_free = snd_opl3_oss_free_port;
  65. callbacks.private_data = opl3;
  66. opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
  67. sprintf(name, "OPL%i OSS Port", opl_ver);
  68. opl3->oss_chset->client = opl3->seq_client;
  69. opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
  70. SNDRV_SEQ_PORT_CAP_WRITE,
  71. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  72. SNDRV_SEQ_PORT_TYPE_MIDI_GM |
  73. SNDRV_SEQ_PORT_TYPE_HARDWARE |
  74. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
  75. voices, voices,
  76. name);
  77. if (opl3->oss_chset->port < 0) {
  78. int port;
  79. port = opl3->oss_chset->port;
  80. snd_midi_channel_free_set(opl3->oss_chset);
  81. return port;
  82. }
  83. return 0;
  84. }
  85. /* ------------------------------ */
  86. /* register OSS synth */
  87. void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
  88. {
  89. struct snd_seq_oss_reg *arg;
  90. struct snd_seq_device *dev;
  91. if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
  92. sizeof(struct snd_seq_oss_reg), &dev) < 0)
  93. return;
  94. opl3->oss_seq_dev = dev;
  95. strlcpy(dev->name, name, sizeof(dev->name));
  96. arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
  97. arg->type = SYNTH_TYPE_FM;
  98. if (opl3->hardware < OPL3_HW_OPL3) {
  99. arg->subtype = FM_TYPE_ADLIB;
  100. arg->nvoices = MAX_OPL2_VOICES;
  101. } else {
  102. arg->subtype = FM_TYPE_OPL3;
  103. arg->nvoices = MAX_OPL3_VOICES;
  104. }
  105. arg->oper = oss_callback;
  106. arg->private_data = opl3;
  107. if (snd_opl3_oss_create_port(opl3)) {
  108. /* register to OSS synth table */
  109. snd_device_register(opl3->card, dev);
  110. }
  111. }
  112. /* unregister */
  113. void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
  114. {
  115. if (opl3->oss_seq_dev) {
  116. /* The instance should have been released in prior */
  117. opl3->oss_seq_dev = NULL;
  118. }
  119. }
  120. /* ------------------------------ */
  121. /* open OSS sequencer */
  122. static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
  123. {
  124. struct snd_opl3 *opl3 = closure;
  125. int err;
  126. if (snd_BUG_ON(!arg))
  127. return -ENXIO;
  128. if ((err = snd_opl3_synth_setup(opl3)) < 0)
  129. return err;
  130. /* fill the argument data */
  131. arg->private_data = opl3;
  132. arg->addr.client = opl3->oss_chset->client;
  133. arg->addr.port = opl3->oss_chset->port;
  134. if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
  135. return err;
  136. opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
  137. return 0;
  138. }
  139. /* close OSS sequencer */
  140. static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
  141. {
  142. struct snd_opl3 *opl3;
  143. if (snd_BUG_ON(!arg))
  144. return -ENXIO;
  145. opl3 = arg->private_data;
  146. snd_opl3_synth_cleanup(opl3);
  147. snd_opl3_synth_use_dec(opl3);
  148. return 0;
  149. }
  150. /* load patch */
  151. /* from sound_config.h */
  152. #define SBFM_MAXINSTR 256
  153. static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
  154. const char __user *buf, int offs, int count)
  155. {
  156. struct snd_opl3 *opl3;
  157. struct sbi_instrument sbi;
  158. char name[32];
  159. int err, type;
  160. if (snd_BUG_ON(!arg))
  161. return -ENXIO;
  162. opl3 = arg->private_data;
  163. if (format == FM_PATCH)
  164. type = FM_PATCH_OPL2;
  165. else if (format == OPL3_PATCH)
  166. type = FM_PATCH_OPL3;
  167. else
  168. return -EINVAL;
  169. if (count < (int)sizeof(sbi)) {
  170. snd_printk(KERN_ERR "FM Error: Patch record too short\n");
  171. return -EINVAL;
  172. }
  173. if (copy_from_user(&sbi, buf, sizeof(sbi)))
  174. return -EFAULT;
  175. if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
  176. snd_printk(KERN_ERR "FM Error: Invalid instrument number %d\n",
  177. sbi.channel);
  178. return -EINVAL;
  179. }
  180. memset(name, 0, sizeof(name));
  181. sprintf(name, "Chan%d", sbi.channel);
  182. err = snd_opl3_load_patch(opl3, sbi.channel, 127, type, name, NULL,
  183. sbi.operators);
  184. if (err < 0)
  185. return err;
  186. return sizeof(sbi);
  187. }
  188. /* ioctl */
  189. static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
  190. unsigned long ioarg)
  191. {
  192. if (snd_BUG_ON(!arg))
  193. return -ENXIO;
  194. switch (cmd) {
  195. case SNDCTL_FM_LOAD_INSTR:
  196. snd_printk(KERN_ERR "OPL3: "
  197. "Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. "
  198. "Fix the program.\n");
  199. return -EINVAL;
  200. case SNDCTL_SYNTH_MEMAVL:
  201. return 0x7fffffff;
  202. case SNDCTL_FM_4OP_ENABLE:
  203. // handled automatically by OPL instrument type
  204. return 0;
  205. default:
  206. return -EINVAL;
  207. }
  208. return 0;
  209. }
  210. /* reset device */
  211. static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
  212. {
  213. if (snd_BUG_ON(!arg))
  214. return -ENXIO;
  215. return 0;
  216. }