opl3_oss.c 6.9 KB

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