opl3_oss.c 9.4 KB

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