opl3_oss.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. #include <linux/slab.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_SYNTH,
  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. snd_assert(arg != NULL, return -ENXIO);
  138. if ((err = snd_opl3_synth_setup(opl3)) < 0)
  139. return err;
  140. /* fill the argument data */
  141. arg->private_data = opl3;
  142. arg->addr.client = opl3->oss_chset->client;
  143. arg->addr.port = opl3->oss_chset->port;
  144. if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
  145. return err;
  146. opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
  147. return 0;
  148. }
  149. /* close OSS sequencer */
  150. static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
  151. {
  152. struct snd_opl3 *opl3;
  153. snd_assert(arg != NULL, return -ENXIO);
  154. opl3 = arg->private_data;
  155. snd_opl3_synth_cleanup(opl3);
  156. snd_opl3_synth_use_dec(opl3);
  157. return 0;
  158. }
  159. /* load patch */
  160. /* offsets for SBI params */
  161. #define AM_VIB 0
  162. #define KSL_LEVEL 2
  163. #define ATTACK_DECAY 4
  164. #define SUSTAIN_RELEASE 6
  165. #define WAVE_SELECT 8
  166. /* offset for SBI instrument */
  167. #define CONNECTION 10
  168. #define OFFSET_4OP 11
  169. /* from sound_config.h */
  170. #define SBFM_MAXINSTR 256
  171. static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
  172. const char __user *buf, int offs, int count)
  173. {
  174. struct snd_opl3 *opl3;
  175. int err = -EINVAL;
  176. snd_assert(arg != NULL, return -ENXIO);
  177. opl3 = arg->private_data;
  178. if ((format == FM_PATCH) || (format == OPL3_PATCH)) {
  179. struct sbi_instrument sbi;
  180. size_t size;
  181. struct snd_seq_instr_header *put;
  182. struct snd_seq_instr_data *data;
  183. struct fm_xinstrument *xinstr;
  184. struct snd_seq_event ev;
  185. int i;
  186. mm_segment_t fs;
  187. if (count < (int)sizeof(sbi)) {
  188. snd_printk("FM Error: Patch record too short\n");
  189. return -EINVAL;
  190. }
  191. if (copy_from_user(&sbi, buf, sizeof(sbi)))
  192. return -EFAULT;
  193. if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
  194. snd_printk("FM Error: Invalid instrument number %d\n", sbi.channel);
  195. return -EINVAL;
  196. }
  197. size = sizeof(*put) + sizeof(struct fm_xinstrument);
  198. put = kzalloc(size, GFP_KERNEL);
  199. if (put == NULL)
  200. return -ENOMEM;
  201. /* build header */
  202. data = &put->data;
  203. data->type = SNDRV_SEQ_INSTR_ATYPE_DATA;
  204. strcpy(data->data.format, SNDRV_SEQ_INSTR_ID_OPL2_3);
  205. /* build data section */
  206. xinstr = (struct fm_xinstrument *)(data + 1);
  207. xinstr->stype = FM_STRU_INSTR;
  208. for (i = 0; i < 2; i++) {
  209. xinstr->op[i].am_vib = sbi.operators[AM_VIB + i];
  210. xinstr->op[i].ksl_level = sbi.operators[KSL_LEVEL + i];
  211. xinstr->op[i].attack_decay = sbi.operators[ATTACK_DECAY + i];
  212. xinstr->op[i].sustain_release = sbi.operators[SUSTAIN_RELEASE + i];
  213. xinstr->op[i].wave_select = sbi.operators[WAVE_SELECT + i];
  214. }
  215. xinstr->feedback_connection[0] = sbi.operators[CONNECTION];
  216. if (format == OPL3_PATCH) {
  217. xinstr->type = FM_PATCH_OPL3;
  218. for (i = 0; i < 2; i++) {
  219. xinstr->op[i+2].am_vib = sbi.operators[OFFSET_4OP + AM_VIB + i];
  220. xinstr->op[i+2].ksl_level = sbi.operators[OFFSET_4OP + KSL_LEVEL + i];
  221. xinstr->op[i+2].attack_decay = sbi.operators[OFFSET_4OP + ATTACK_DECAY + i];
  222. xinstr->op[i+2].sustain_release = sbi.operators[OFFSET_4OP + SUSTAIN_RELEASE + i];
  223. xinstr->op[i+2].wave_select = sbi.operators[OFFSET_4OP + WAVE_SELECT + i];
  224. }
  225. xinstr->feedback_connection[1] = sbi.operators[OFFSET_4OP + CONNECTION];
  226. } else {
  227. xinstr->type = FM_PATCH_OPL2;
  228. }
  229. put->id.instr.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3;
  230. put->id.instr.bank = 127;
  231. put->id.instr.prg = sbi.channel;
  232. put->cmd = SNDRV_SEQ_INSTR_PUT_CMD_CREATE;
  233. memset (&ev, 0, sizeof(ev));
  234. ev.source.client = SNDRV_SEQ_CLIENT_OSS;
  235. ev.dest = arg->addr;
  236. ev.flags = SNDRV_SEQ_EVENT_LENGTH_VARUSR;
  237. ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
  238. fs = snd_enter_user();
  239. __again:
  240. ev.type = SNDRV_SEQ_EVENT_INSTR_PUT;
  241. ev.data.ext.len = size;
  242. ev.data.ext.ptr = put;
  243. err = snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
  244. opl3->seq_client, 0, 0);
  245. if (err == -EBUSY) {
  246. struct snd_seq_instr_header remove;
  247. memset (&remove, 0, sizeof(remove));
  248. remove.cmd = SNDRV_SEQ_INSTR_FREE_CMD_SINGLE;
  249. remove.id.instr = put->id.instr;
  250. /* remove instrument */
  251. ev.type = SNDRV_SEQ_EVENT_INSTR_FREE;
  252. ev.data.ext.len = sizeof(remove);
  253. ev.data.ext.ptr = &remove;
  254. snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
  255. opl3->seq_client, 0, 0);
  256. goto __again;
  257. }
  258. snd_leave_user(fs);
  259. kfree(put);
  260. }
  261. return err;
  262. }
  263. /* ioctl */
  264. static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
  265. unsigned long ioarg)
  266. {
  267. struct snd_opl3 *opl3;
  268. snd_assert(arg != NULL, return -ENXIO);
  269. opl3 = arg->private_data;
  270. switch (cmd) {
  271. case SNDCTL_FM_LOAD_INSTR:
  272. snd_printk("OPL3: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
  273. return -EINVAL;
  274. case SNDCTL_SYNTH_MEMAVL:
  275. return 0x7fffffff;
  276. case SNDCTL_FM_4OP_ENABLE:
  277. // handled automatically by OPL instrument type
  278. return 0;
  279. default:
  280. return -EINVAL;
  281. }
  282. return 0;
  283. }
  284. /* reset device */
  285. static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
  286. {
  287. struct snd_opl3 *opl3;
  288. snd_assert(arg != NULL, return -ENXIO);
  289. opl3 = arg->private_data;
  290. return 0;
  291. }