| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
|  | 21 | #include "opl3_voice.h" | 
|  | 22 | #include <linux/slab.h> | 
|  | 23 |  | 
|  | 24 | static int snd_opl3_open_seq_oss(snd_seq_oss_arg_t *arg, void *closure); | 
|  | 25 | static int snd_opl3_close_seq_oss(snd_seq_oss_arg_t *arg); | 
|  | 26 | static int snd_opl3_ioctl_seq_oss(snd_seq_oss_arg_t *arg, unsigned int cmd, unsigned long ioarg); | 
|  | 27 | static int snd_opl3_load_patch_seq_oss(snd_seq_oss_arg_t *arg, int format, const char __user *buf, int offs, int count); | 
|  | 28 | static int snd_opl3_reset_seq_oss(snd_seq_oss_arg_t *arg); | 
|  | 29 |  | 
|  | 30 | /* */ | 
|  | 31 |  | 
|  | 32 | static inline mm_segment_t snd_enter_user(void) | 
|  | 33 | { | 
|  | 34 | mm_segment_t fs = get_fs(); | 
|  | 35 | set_fs(get_ds()); | 
|  | 36 | return fs; | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | static inline void snd_leave_user(mm_segment_t fs) | 
|  | 40 | { | 
|  | 41 | set_fs(fs); | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | /* operators */ | 
|  | 45 |  | 
|  | 46 | extern snd_midi_op_t opl3_ops; | 
|  | 47 |  | 
|  | 48 | static snd_seq_oss_callback_t oss_callback = { | 
|  | 49 | .owner = 	THIS_MODULE, | 
|  | 50 | .open =		snd_opl3_open_seq_oss, | 
|  | 51 | .close =	snd_opl3_close_seq_oss, | 
|  | 52 | .ioctl =	snd_opl3_ioctl_seq_oss, | 
|  | 53 | .load_patch =	snd_opl3_load_patch_seq_oss, | 
|  | 54 | .reset =	snd_opl3_reset_seq_oss, | 
|  | 55 | }; | 
|  | 56 |  | 
|  | 57 | static int snd_opl3_oss_event_input(snd_seq_event_t *ev, int direct, | 
|  | 58 | void *private_data, int atomic, int hop) | 
|  | 59 | { | 
|  | 60 | opl3_t *opl3 = private_data; | 
|  | 61 |  | 
|  | 62 | if (ev->type != SNDRV_SEQ_EVENT_OSS) | 
|  | 63 | snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset); | 
|  | 64 | return 0; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | /* ------------------------------ */ | 
|  | 68 |  | 
|  | 69 | static void snd_opl3_oss_free_port(void *private_data) | 
|  | 70 | { | 
|  | 71 | opl3_t *opl3 = private_data; | 
|  | 72 |  | 
|  | 73 | snd_midi_channel_free_set(opl3->oss_chset); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | static int snd_opl3_oss_create_port(opl3_t * opl3) | 
|  | 77 | { | 
|  | 78 | snd_seq_port_callback_t callbacks; | 
|  | 79 | char name[32]; | 
|  | 80 | int voices, opl_ver; | 
|  | 81 |  | 
|  | 82 | voices = (opl3->hardware < OPL3_HW_OPL3) ? | 
|  | 83 | MAX_OPL2_VOICES : MAX_OPL3_VOICES; | 
|  | 84 | opl3->oss_chset = snd_midi_channel_alloc_set(voices); | 
|  | 85 | if (opl3->oss_chset == NULL) | 
|  | 86 | return -ENOMEM; | 
|  | 87 | opl3->oss_chset->private_data = opl3; | 
|  | 88 |  | 
|  | 89 | memset(&callbacks, 0, sizeof(callbacks)); | 
|  | 90 | callbacks.owner = THIS_MODULE; | 
|  | 91 | callbacks.event_input = snd_opl3_oss_event_input; | 
|  | 92 | callbacks.private_free = snd_opl3_oss_free_port; | 
|  | 93 | callbacks.private_data = opl3; | 
|  | 94 |  | 
|  | 95 | opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8; | 
|  | 96 | sprintf(name, "OPL%i OSS Port", opl_ver); | 
|  | 97 |  | 
|  | 98 | opl3->oss_chset->client = opl3->seq_client; | 
|  | 99 | opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks, | 
|  | 100 | SNDRV_SEQ_PORT_CAP_WRITE, | 
|  | 101 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | | 
|  | 102 | SNDRV_SEQ_PORT_TYPE_MIDI_GM | | 
|  | 103 | SNDRV_SEQ_PORT_TYPE_SYNTH, | 
|  | 104 | voices, voices, | 
|  | 105 | name); | 
|  | 106 | if (opl3->oss_chset->port < 0) { | 
|  | 107 | snd_midi_channel_free_set(opl3->oss_chset); | 
|  | 108 | return opl3->oss_chset->port; | 
|  | 109 | } | 
|  | 110 | return 0; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | /* ------------------------------ */ | 
|  | 114 |  | 
|  | 115 | /* register OSS synth */ | 
|  | 116 | void snd_opl3_init_seq_oss(opl3_t *opl3, char *name) | 
|  | 117 | { | 
|  | 118 | snd_seq_oss_reg_t *arg; | 
|  | 119 | snd_seq_device_t *dev; | 
|  | 120 |  | 
|  | 121 | if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS, | 
|  | 122 | sizeof(snd_seq_oss_reg_t), &dev) < 0) | 
|  | 123 | return; | 
|  | 124 |  | 
|  | 125 | opl3->oss_seq_dev = dev; | 
|  | 126 | strlcpy(dev->name, name, sizeof(dev->name)); | 
|  | 127 | arg = SNDRV_SEQ_DEVICE_ARGPTR(dev); | 
|  | 128 | arg->type = SYNTH_TYPE_FM; | 
|  | 129 | if (opl3->hardware < OPL3_HW_OPL3) { | 
|  | 130 | arg->subtype = FM_TYPE_ADLIB; | 
|  | 131 | arg->nvoices = MAX_OPL2_VOICES; | 
|  | 132 | } else { | 
|  | 133 | arg->subtype = FM_TYPE_OPL3; | 
|  | 134 | arg->nvoices = MAX_OPL3_VOICES; | 
|  | 135 | } | 
|  | 136 | arg->oper = oss_callback; | 
|  | 137 | arg->private_data = opl3; | 
|  | 138 |  | 
|  | 139 | snd_opl3_oss_create_port(opl3); | 
|  | 140 |  | 
|  | 141 | /* register to OSS synth table */ | 
|  | 142 | snd_device_register(opl3->card, dev); | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | /* unregister */ | 
|  | 146 | void snd_opl3_free_seq_oss(opl3_t *opl3) | 
|  | 147 | { | 
|  | 148 | if (opl3->oss_seq_dev) { | 
|  | 149 | snd_device_free(opl3->card, opl3->oss_seq_dev); | 
|  | 150 | opl3->oss_seq_dev = NULL; | 
|  | 151 | } | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | /* ------------------------------ */ | 
|  | 155 |  | 
|  | 156 | /* open OSS sequencer */ | 
|  | 157 | static int snd_opl3_open_seq_oss(snd_seq_oss_arg_t *arg, void *closure) | 
|  | 158 | { | 
|  | 159 | opl3_t *opl3 = closure; | 
|  | 160 | int err; | 
|  | 161 |  | 
|  | 162 | snd_assert(arg != NULL, return -ENXIO); | 
|  | 163 |  | 
|  | 164 | if ((err = snd_opl3_synth_setup(opl3)) < 0) | 
|  | 165 | return err; | 
|  | 166 |  | 
|  | 167 | /* fill the argument data */ | 
|  | 168 | arg->private_data = opl3; | 
|  | 169 | arg->addr.client = opl3->oss_chset->client; | 
|  | 170 | arg->addr.port = opl3->oss_chset->port; | 
|  | 171 |  | 
|  | 172 | if ((err = snd_opl3_synth_use_inc(opl3)) < 0) | 
|  | 173 | return err; | 
|  | 174 |  | 
|  | 175 | opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH; | 
|  | 176 | return 0; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | /* close OSS sequencer */ | 
|  | 180 | static int snd_opl3_close_seq_oss(snd_seq_oss_arg_t *arg) | 
|  | 181 | { | 
|  | 182 | opl3_t *opl3; | 
|  | 183 |  | 
|  | 184 | snd_assert(arg != NULL, return -ENXIO); | 
|  | 185 | opl3 = arg->private_data; | 
|  | 186 |  | 
|  | 187 | snd_opl3_synth_cleanup(opl3); | 
|  | 188 |  | 
|  | 189 | snd_opl3_synth_use_dec(opl3); | 
|  | 190 | return 0; | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | /* load patch */ | 
|  | 194 |  | 
|  | 195 | /* offsets for SBI params */ | 
|  | 196 | #define AM_VIB		0 | 
|  | 197 | #define KSL_LEVEL	2 | 
|  | 198 | #define ATTACK_DECAY	4 | 
|  | 199 | #define SUSTAIN_RELEASE	6 | 
|  | 200 | #define WAVE_SELECT	8 | 
|  | 201 |  | 
|  | 202 | /* offset for SBI instrument */ | 
|  | 203 | #define CONNECTION	10 | 
|  | 204 | #define OFFSET_4OP	11 | 
|  | 205 |  | 
|  | 206 | /* from sound_config.h */ | 
|  | 207 | #define SBFM_MAXINSTR	256 | 
|  | 208 |  | 
|  | 209 | static int snd_opl3_load_patch_seq_oss(snd_seq_oss_arg_t *arg, int format, | 
|  | 210 | const char __user *buf, int offs, int count) | 
|  | 211 | { | 
|  | 212 | opl3_t *opl3; | 
|  | 213 | int err = -EINVAL; | 
|  | 214 |  | 
|  | 215 | snd_assert(arg != NULL, return -ENXIO); | 
|  | 216 | opl3 = arg->private_data; | 
|  | 217 |  | 
|  | 218 | if ((format == FM_PATCH) || (format == OPL3_PATCH)) { | 
|  | 219 | struct sbi_instrument sbi; | 
|  | 220 |  | 
|  | 221 | size_t size; | 
|  | 222 | snd_seq_instr_header_t *put; | 
|  | 223 | snd_seq_instr_data_t *data; | 
|  | 224 | fm_xinstrument_t *xinstr; | 
|  | 225 |  | 
|  | 226 | snd_seq_event_t ev; | 
|  | 227 | int i; | 
|  | 228 |  | 
|  | 229 | mm_segment_t fs; | 
|  | 230 |  | 
|  | 231 | if (count < (int)sizeof(sbi)) { | 
|  | 232 | snd_printk("FM Error: Patch record too short\n"); | 
|  | 233 | return -EINVAL; | 
|  | 234 | } | 
|  | 235 | if (copy_from_user(&sbi, buf, sizeof(sbi))) | 
|  | 236 | return -EFAULT; | 
|  | 237 |  | 
|  | 238 | if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) { | 
|  | 239 | snd_printk("FM Error: Invalid instrument number %d\n", sbi.channel); | 
|  | 240 | return -EINVAL; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | size = sizeof(*put) + sizeof(fm_xinstrument_t); | 
| Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 244 | put = kzalloc(size, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (put == NULL) | 
|  | 246 | return -ENOMEM; | 
|  | 247 | /* build header */ | 
|  | 248 | data = &put->data; | 
|  | 249 | data->type = SNDRV_SEQ_INSTR_ATYPE_DATA; | 
|  | 250 | strcpy(data->data.format, SNDRV_SEQ_INSTR_ID_OPL2_3); | 
|  | 251 | /* build data section */ | 
|  | 252 | xinstr = (fm_xinstrument_t *)(data + 1); | 
|  | 253 | xinstr->stype = FM_STRU_INSTR; | 
|  | 254 |  | 
|  | 255 | for (i = 0; i < 2; i++) { | 
|  | 256 | xinstr->op[i].am_vib = sbi.operators[AM_VIB + i]; | 
|  | 257 | xinstr->op[i].ksl_level = sbi.operators[KSL_LEVEL + i]; | 
|  | 258 | xinstr->op[i].attack_decay = sbi.operators[ATTACK_DECAY + i]; | 
|  | 259 | xinstr->op[i].sustain_release = sbi.operators[SUSTAIN_RELEASE + i]; | 
|  | 260 | xinstr->op[i].wave_select = sbi.operators[WAVE_SELECT + i]; | 
|  | 261 | } | 
|  | 262 | xinstr->feedback_connection[0] = sbi.operators[CONNECTION]; | 
|  | 263 |  | 
|  | 264 | if (format == OPL3_PATCH) { | 
|  | 265 | xinstr->type = FM_PATCH_OPL3; | 
|  | 266 | for (i = 0; i < 2; i++) { | 
|  | 267 | xinstr->op[i+2].am_vib = sbi.operators[OFFSET_4OP + AM_VIB + i]; | 
|  | 268 | xinstr->op[i+2].ksl_level = sbi.operators[OFFSET_4OP + KSL_LEVEL + i]; | 
|  | 269 | xinstr->op[i+2].attack_decay = sbi.operators[OFFSET_4OP + ATTACK_DECAY + i]; | 
|  | 270 | xinstr->op[i+2].sustain_release = sbi.operators[OFFSET_4OP + SUSTAIN_RELEASE + i]; | 
|  | 271 | xinstr->op[i+2].wave_select = sbi.operators[OFFSET_4OP + WAVE_SELECT + i]; | 
|  | 272 | } | 
|  | 273 | xinstr->feedback_connection[1] = sbi.operators[OFFSET_4OP + CONNECTION]; | 
|  | 274 | } else { | 
|  | 275 | xinstr->type = FM_PATCH_OPL2; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | put->id.instr.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3; | 
|  | 279 | put->id.instr.bank = 127; | 
|  | 280 | put->id.instr.prg = sbi.channel; | 
|  | 281 | put->cmd = SNDRV_SEQ_INSTR_PUT_CMD_CREATE; | 
|  | 282 |  | 
|  | 283 | memset (&ev, 0, sizeof(ev)); | 
|  | 284 | ev.source.client = SNDRV_SEQ_CLIENT_OSS; | 
|  | 285 | ev.dest = arg->addr; | 
|  | 286 |  | 
|  | 287 | ev.flags = SNDRV_SEQ_EVENT_LENGTH_VARUSR; | 
|  | 288 | ev.queue = SNDRV_SEQ_QUEUE_DIRECT; | 
|  | 289 |  | 
|  | 290 | fs = snd_enter_user(); | 
|  | 291 | __again: | 
|  | 292 | ev.type = SNDRV_SEQ_EVENT_INSTR_PUT; | 
|  | 293 | ev.data.ext.len = size; | 
|  | 294 | ev.data.ext.ptr = put; | 
|  | 295 |  | 
|  | 296 | err = snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev, | 
|  | 297 | opl3->seq_client, 0, 0); | 
|  | 298 | if (err == -EBUSY) { | 
|  | 299 | snd_seq_instr_header_t remove; | 
|  | 300 |  | 
|  | 301 | memset (&remove, 0, sizeof(remove)); | 
|  | 302 | remove.cmd = SNDRV_SEQ_INSTR_FREE_CMD_SINGLE; | 
|  | 303 | remove.id.instr = put->id.instr; | 
|  | 304 |  | 
|  | 305 | /* remove instrument */ | 
|  | 306 | ev.type = SNDRV_SEQ_EVENT_INSTR_FREE; | 
|  | 307 | ev.data.ext.len = sizeof(remove); | 
|  | 308 | ev.data.ext.ptr = &remove; | 
|  | 309 |  | 
|  | 310 | snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev, | 
|  | 311 | opl3->seq_client, 0, 0); | 
|  | 312 | goto __again; | 
|  | 313 | } | 
|  | 314 | snd_leave_user(fs); | 
|  | 315 |  | 
|  | 316 | kfree(put); | 
|  | 317 | } | 
|  | 318 | return err; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | /* ioctl */ | 
|  | 322 | static int snd_opl3_ioctl_seq_oss(snd_seq_oss_arg_t *arg, unsigned int cmd, | 
|  | 323 | unsigned long ioarg) | 
|  | 324 | { | 
|  | 325 | opl3_t *opl3; | 
|  | 326 |  | 
|  | 327 | snd_assert(arg != NULL, return -ENXIO); | 
|  | 328 | opl3 = arg->private_data; | 
|  | 329 | switch (cmd) { | 
|  | 330 | case SNDCTL_FM_LOAD_INSTR: | 
|  | 331 | snd_printk("OPL3: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n"); | 
|  | 332 | return -EINVAL; | 
|  | 333 |  | 
|  | 334 | case SNDCTL_SYNTH_MEMAVL: | 
|  | 335 | return 0x7fffffff; | 
|  | 336 |  | 
|  | 337 | case SNDCTL_FM_4OP_ENABLE: | 
|  | 338 | // handled automatically by OPL instrument type | 
|  | 339 | return 0; | 
|  | 340 |  | 
|  | 341 | default: | 
|  | 342 | return -EINVAL; | 
|  | 343 | } | 
|  | 344 | return 0; | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | /* reset device */ | 
|  | 348 | static int snd_opl3_reset_seq_oss(snd_seq_oss_arg_t *arg) | 
|  | 349 | { | 
|  | 350 | opl3_t *opl3; | 
|  | 351 |  | 
|  | 352 | snd_assert(arg != NULL, return -ENXIO); | 
|  | 353 | opl3 = arg->private_data; | 
|  | 354 |  | 
|  | 355 | return 0; | 
|  | 356 | } |