opl3_oss.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. snd_midi_channel_free_set(opl3->oss_chset);
  90. return opl3->oss_chset->port;
  91. }
  92. return 0;
  93. }
  94. /* ------------------------------ */
  95. /* register OSS synth */
  96. void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
  97. {
  98. struct snd_seq_oss_reg *arg;
  99. struct snd_seq_device *dev;
  100. if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
  101. sizeof(struct snd_seq_oss_reg), &dev) < 0)
  102. return;
  103. opl3->oss_seq_dev = dev;
  104. strlcpy(dev->name, name, sizeof(dev->name));
  105. arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
  106. arg->type = SYNTH_TYPE_FM;
  107. if (opl3->hardware < OPL3_HW_OPL3) {
  108. arg->subtype = FM_TYPE_ADLIB;
  109. arg->nvoices = MAX_OPL2_VOICES;
  110. } else {
  111. arg->subtype = FM_TYPE_OPL3;
  112. arg->nvoices = MAX_OPL3_VOICES;
  113. }
  114. arg->oper = oss_callback;
  115. arg->private_data = opl3;
  116. snd_opl3_oss_create_port(opl3);
  117. /* register to OSS synth table */
  118. snd_device_register(opl3->card, dev);
  119. }
  120. /* unregister */
  121. void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
  122. {
  123. if (opl3->oss_seq_dev) {
  124. snd_device_free(opl3->card, opl3->oss_seq_dev);
  125. opl3->oss_seq_dev = NULL;
  126. }
  127. }
  128. /* ------------------------------ */
  129. /* open OSS sequencer */
  130. static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
  131. {
  132. struct snd_opl3 *opl3 = closure;
  133. int err;
  134. snd_assert(arg != NULL, return -ENXIO);
  135. if ((err = snd_opl3_synth_setup(opl3)) < 0)
  136. return err;
  137. /* fill the argument data */
  138. arg->private_data = opl3;
  139. arg->addr.client = opl3->oss_chset->client;
  140. arg->addr.port = opl3->oss_chset->port;
  141. if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
  142. return err;
  143. opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
  144. return 0;
  145. }
  146. /* close OSS sequencer */
  147. static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
  148. {
  149. struct snd_opl3 *opl3;
  150. snd_assert(arg != NULL, return -ENXIO);
  151. opl3 = arg->private_data;
  152. snd_opl3_synth_cleanup(opl3);
  153. snd_opl3_synth_use_dec(opl3);
  154. return 0;
  155. }
  156. /* load patch */
  157. /* offsets for SBI params */
  158. #define AM_VIB 0
  159. #define KSL_LEVEL 2
  160. #define ATTACK_DECAY 4
  161. #define SUSTAIN_RELEASE 6
  162. #define WAVE_SELECT 8
  163. /* offset for SBI instrument */
  164. #define CONNECTION 10
  165. #define OFFSET_4OP 11
  166. /* from sound_config.h */
  167. #define SBFM_MAXINSTR 256
  168. static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
  169. const char __user *buf, int offs, int count)
  170. {
  171. struct snd_opl3 *opl3;
  172. int err = -EINVAL;
  173. snd_assert(arg != NULL, return -ENXIO);
  174. opl3 = arg->private_data;
  175. if ((format == FM_PATCH) || (format == OPL3_PATCH)) {
  176. struct sbi_instrument sbi;
  177. size_t size;
  178. struct snd_seq_instr_header *put;
  179. struct snd_seq_instr_data *data;
  180. struct fm_xinstrument *xinstr;
  181. struct snd_seq_event ev;
  182. int i;
  183. mm_segment_t fs;
  184. if (count < (int)sizeof(sbi)) {
  185. snd_printk("FM Error: Patch record too short\n");
  186. return -EINVAL;
  187. }
  188. if (copy_from_user(&sbi, buf, sizeof(sbi)))
  189. return -EFAULT;
  190. if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
  191. snd_printk("FM Error: Invalid instrument number %d\n", sbi.channel);
  192. return -EINVAL;
  193. }
  194. size = sizeof(*put) + sizeof(struct fm_xinstrument);
  195. put = kzalloc(size, GFP_KERNEL);
  196. if (put == NULL)
  197. return -ENOMEM;
  198. /* build header */
  199. data = &put->data;
  200. data->type = SNDRV_SEQ_INSTR_ATYPE_DATA;
  201. strcpy(data->data.format, SNDRV_SEQ_INSTR_ID_OPL2_3);
  202. /* build data section */
  203. xinstr = (struct fm_xinstrument *)(data + 1);
  204. xinstr->stype = FM_STRU_INSTR;
  205. for (i = 0; i < 2; i++) {
  206. xinstr->op[i].am_vib = sbi.operators[AM_VIB + i];
  207. xinstr->op[i].ksl_level = sbi.operators[KSL_LEVEL + i];
  208. xinstr->op[i].attack_decay = sbi.operators[ATTACK_DECAY + i];
  209. xinstr->op[i].sustain_release = sbi.operators[SUSTAIN_RELEASE + i];
  210. xinstr->op[i].wave_select = sbi.operators[WAVE_SELECT + i];
  211. }
  212. xinstr->feedback_connection[0] = sbi.operators[CONNECTION];
  213. if (format == OPL3_PATCH) {
  214. xinstr->type = FM_PATCH_OPL3;
  215. for (i = 0; i < 2; i++) {
  216. xinstr->op[i+2].am_vib = sbi.operators[OFFSET_4OP + AM_VIB + i];
  217. xinstr->op[i+2].ksl_level = sbi.operators[OFFSET_4OP + KSL_LEVEL + i];
  218. xinstr->op[i+2].attack_decay = sbi.operators[OFFSET_4OP + ATTACK_DECAY + i];
  219. xinstr->op[i+2].sustain_release = sbi.operators[OFFSET_4OP + SUSTAIN_RELEASE + i];
  220. xinstr->op[i+2].wave_select = sbi.operators[OFFSET_4OP + WAVE_SELECT + i];
  221. }
  222. xinstr->feedback_connection[1] = sbi.operators[OFFSET_4OP + CONNECTION];
  223. } else {
  224. xinstr->type = FM_PATCH_OPL2;
  225. }
  226. put->id.instr.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3;
  227. put->id.instr.bank = 127;
  228. put->id.instr.prg = sbi.channel;
  229. put->cmd = SNDRV_SEQ_INSTR_PUT_CMD_CREATE;
  230. memset (&ev, 0, sizeof(ev));
  231. ev.source.client = SNDRV_SEQ_CLIENT_OSS;
  232. ev.dest = arg->addr;
  233. ev.flags = SNDRV_SEQ_EVENT_LENGTH_VARUSR;
  234. ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
  235. fs = snd_enter_user();
  236. __again:
  237. ev.type = SNDRV_SEQ_EVENT_INSTR_PUT;
  238. ev.data.ext.len = size;
  239. ev.data.ext.ptr = put;
  240. err = snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
  241. opl3->seq_client, 0, 0);
  242. if (err == -EBUSY) {
  243. struct snd_seq_instr_header remove;
  244. memset (&remove, 0, sizeof(remove));
  245. remove.cmd = SNDRV_SEQ_INSTR_FREE_CMD_SINGLE;
  246. remove.id.instr = put->id.instr;
  247. /* remove instrument */
  248. ev.type = SNDRV_SEQ_EVENT_INSTR_FREE;
  249. ev.data.ext.len = sizeof(remove);
  250. ev.data.ext.ptr = &remove;
  251. snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
  252. opl3->seq_client, 0, 0);
  253. goto __again;
  254. }
  255. snd_leave_user(fs);
  256. kfree(put);
  257. }
  258. return err;
  259. }
  260. /* ioctl */
  261. static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
  262. unsigned long ioarg)
  263. {
  264. struct snd_opl3 *opl3;
  265. snd_assert(arg != NULL, return -ENXIO);
  266. opl3 = arg->private_data;
  267. switch (cmd) {
  268. case SNDCTL_FM_LOAD_INSTR:
  269. snd_printk("OPL3: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
  270. return -EINVAL;
  271. case SNDCTL_SYNTH_MEMAVL:
  272. return 0x7fffffff;
  273. case SNDCTL_FM_4OP_ENABLE:
  274. // handled automatically by OPL instrument type
  275. return 0;
  276. default:
  277. return -EINVAL;
  278. }
  279. return 0;
  280. }
  281. /* reset device */
  282. static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
  283. {
  284. struct snd_opl3 *opl3;
  285. snd_assert(arg != NULL, return -ENXIO);
  286. opl3 = arg->private_data;
  287. return 0;
  288. }