opl3_oss.c 6.9 KB

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