opl3_oss.c 6.0 KB

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