opl3_oss.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. extern struct snd_midi_op opl3_ops;
  29. static struct snd_seq_oss_callback oss_callback = {
  30. .owner = THIS_MODULE,
  31. .open = snd_opl3_open_seq_oss,
  32. .close = snd_opl3_close_seq_oss,
  33. .ioctl = snd_opl3_ioctl_seq_oss,
  34. .load_patch = snd_opl3_load_patch_seq_oss,
  35. .reset = snd_opl3_reset_seq_oss,
  36. };
  37. static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
  38. void *private_data, int atomic, int hop)
  39. {
  40. struct snd_opl3 *opl3 = private_data;
  41. if (ev->type != SNDRV_SEQ_EVENT_OSS)
  42. snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
  43. return 0;
  44. }
  45. /* ------------------------------ */
  46. static void snd_opl3_oss_free_port(void *private_data)
  47. {
  48. struct snd_opl3 *opl3 = private_data;
  49. snd_midi_channel_free_set(opl3->oss_chset);
  50. }
  51. static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
  52. {
  53. struct snd_seq_port_callback callbacks;
  54. char name[32];
  55. int voices, opl_ver;
  56. voices = (opl3->hardware < OPL3_HW_OPL3) ?
  57. MAX_OPL2_VOICES : MAX_OPL3_VOICES;
  58. opl3->oss_chset = snd_midi_channel_alloc_set(voices);
  59. if (opl3->oss_chset == NULL)
  60. return -ENOMEM;
  61. opl3->oss_chset->private_data = opl3;
  62. memset(&callbacks, 0, sizeof(callbacks));
  63. callbacks.owner = THIS_MODULE;
  64. callbacks.event_input = snd_opl3_oss_event_input;
  65. callbacks.private_free = snd_opl3_oss_free_port;
  66. callbacks.private_data = opl3;
  67. opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
  68. sprintf(name, "OPL%i OSS Port", opl_ver);
  69. opl3->oss_chset->client = opl3->seq_client;
  70. opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
  71. SNDRV_SEQ_PORT_CAP_WRITE,
  72. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  73. SNDRV_SEQ_PORT_TYPE_MIDI_GM |
  74. SNDRV_SEQ_PORT_TYPE_HARDWARE |
  75. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
  76. voices, voices,
  77. name);
  78. if (opl3->oss_chset->port < 0) {
  79. int port;
  80. port = opl3->oss_chset->port;
  81. snd_midi_channel_free_set(opl3->oss_chset);
  82. return port;
  83. }
  84. return 0;
  85. }
  86. /* ------------------------------ */
  87. /* register OSS synth */
  88. void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
  89. {
  90. struct snd_seq_oss_reg *arg;
  91. struct snd_seq_device *dev;
  92. if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
  93. sizeof(struct snd_seq_oss_reg), &dev) < 0)
  94. return;
  95. opl3->oss_seq_dev = dev;
  96. strlcpy(dev->name, name, sizeof(dev->name));
  97. arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
  98. arg->type = SYNTH_TYPE_FM;
  99. if (opl3->hardware < OPL3_HW_OPL3) {
  100. arg->subtype = FM_TYPE_ADLIB;
  101. arg->nvoices = MAX_OPL2_VOICES;
  102. } else {
  103. arg->subtype = FM_TYPE_OPL3;
  104. arg->nvoices = MAX_OPL3_VOICES;
  105. }
  106. arg->oper = oss_callback;
  107. arg->private_data = opl3;
  108. if (snd_opl3_oss_create_port(opl3)) {
  109. /* register to OSS synth table */
  110. snd_device_register(opl3->card, dev);
  111. }
  112. }
  113. /* unregister */
  114. void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
  115. {
  116. if (opl3->oss_seq_dev) {
  117. /* The instance should have been released in prior */
  118. opl3->oss_seq_dev = NULL;
  119. }
  120. }
  121. /* ------------------------------ */
  122. /* open OSS sequencer */
  123. static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
  124. {
  125. struct snd_opl3 *opl3 = closure;
  126. int err;
  127. if (snd_BUG_ON(!arg))
  128. return -ENXIO;
  129. if ((err = snd_opl3_synth_setup(opl3)) < 0)
  130. return err;
  131. /* fill the argument data */
  132. arg->private_data = opl3;
  133. arg->addr.client = opl3->oss_chset->client;
  134. arg->addr.port = opl3->oss_chset->port;
  135. if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
  136. return err;
  137. opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
  138. return 0;
  139. }
  140. /* close OSS sequencer */
  141. static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
  142. {
  143. struct snd_opl3 *opl3;
  144. if (snd_BUG_ON(!arg))
  145. return -ENXIO;
  146. opl3 = arg->private_data;
  147. snd_opl3_synth_cleanup(opl3);
  148. snd_opl3_synth_use_dec(opl3);
  149. return 0;
  150. }
  151. /* load patch */
  152. /* from sound_config.h */
  153. #define SBFM_MAXINSTR 256
  154. static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
  155. const char __user *buf, int offs, int count)
  156. {
  157. struct snd_opl3 *opl3;
  158. struct sbi_instrument sbi;
  159. char name[32];
  160. int err, type;
  161. if (snd_BUG_ON(!arg))
  162. return -ENXIO;
  163. opl3 = arg->private_data;
  164. if (format == FM_PATCH)
  165. type = FM_PATCH_OPL2;
  166. else if (format == OPL3_PATCH)
  167. type = FM_PATCH_OPL3;
  168. else
  169. return -EINVAL;
  170. if (count < (int)sizeof(sbi)) {
  171. snd_printk(KERN_ERR "FM Error: Patch record too short\n");
  172. return -EINVAL;
  173. }
  174. if (copy_from_user(&sbi, buf, sizeof(sbi)))
  175. return -EFAULT;
  176. if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
  177. snd_printk(KERN_ERR "FM Error: Invalid instrument number %d\n",
  178. sbi.channel);
  179. return -EINVAL;
  180. }
  181. memset(name, 0, sizeof(name));
  182. sprintf(name, "Chan%d", sbi.channel);
  183. err = snd_opl3_load_patch(opl3, sbi.channel, 127, type, name, NULL,
  184. sbi.operators);
  185. if (err < 0)
  186. return err;
  187. return sizeof(sbi);
  188. }
  189. /* ioctl */
  190. static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
  191. unsigned long ioarg)
  192. {
  193. struct snd_opl3 *opl3;
  194. if (snd_BUG_ON(!arg))
  195. return -ENXIO;
  196. opl3 = arg->private_data;
  197. switch (cmd) {
  198. case SNDCTL_FM_LOAD_INSTR:
  199. snd_printk(KERN_ERR "OPL3: "
  200. "Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. "
  201. "Fix the program.\n");
  202. return -EINVAL;
  203. case SNDCTL_SYNTH_MEMAVL:
  204. return 0x7fffffff;
  205. case SNDCTL_FM_4OP_ENABLE:
  206. // handled automatically by OPL instrument type
  207. return 0;
  208. default:
  209. return -EINVAL;
  210. }
  211. return 0;
  212. }
  213. /* reset device */
  214. static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
  215. {
  216. struct snd_opl3 *opl3;
  217. if (snd_BUG_ON(!arg))
  218. return -ENXIO;
  219. opl3 = arg->private_data;
  220. return 0;
  221. }