Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Interface for OSS sequencer emulation |
| 3 | * |
| 4 | * Copyright (C) 1999 Takashi Iwai <tiwai@suse.de> |
| 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 | * Changes |
| 21 | * 19990227 Steve Ratcliffe Made separate file and merged in latest |
| 22 | * midi emulation. |
| 23 | */ |
| 24 | |
| 25 | #include <sound/driver.h> |
| 26 | |
| 27 | #ifdef CONFIG_SND_SEQUENCER_OSS |
| 28 | |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <sound/core.h> |
| 31 | #include "emux_voice.h" |
| 32 | #include <sound/asoundef.h> |
| 33 | |
| 34 | static int snd_emux_open_seq_oss(snd_seq_oss_arg_t *arg, void *closure); |
| 35 | static int snd_emux_close_seq_oss(snd_seq_oss_arg_t *arg); |
| 36 | static int snd_emux_ioctl_seq_oss(snd_seq_oss_arg_t *arg, unsigned int cmd, unsigned long ioarg); |
| 37 | static int snd_emux_load_patch_seq_oss(snd_seq_oss_arg_t *arg, int format, const char __user *buf, int offs, int count); |
| 38 | static int snd_emux_reset_seq_oss(snd_seq_oss_arg_t *arg); |
| 39 | static int snd_emux_event_oss_input(snd_seq_event_t *ev, int direct, void *private, int atomic, int hop); |
| 40 | static void reset_port_mode(snd_emux_port_t *port, int midi_mode); |
| 41 | static void emuspec_control(snd_emux_t *emu, snd_emux_port_t *port, int cmd, unsigned char *event, int atomic, int hop); |
| 42 | static void gusspec_control(snd_emux_t *emu, snd_emux_port_t *port, int cmd, unsigned char *event, int atomic, int hop); |
| 43 | static void fake_event(snd_emux_t *emu, snd_emux_port_t *port, int ch, int param, int val, int atomic, int hop); |
| 44 | |
| 45 | /* operators */ |
| 46 | static snd_seq_oss_callback_t oss_callback = { |
| 47 | .owner = THIS_MODULE, |
| 48 | .open = snd_emux_open_seq_oss, |
| 49 | .close = snd_emux_close_seq_oss, |
| 50 | .ioctl = snd_emux_ioctl_seq_oss, |
| 51 | .load_patch = snd_emux_load_patch_seq_oss, |
| 52 | .reset = snd_emux_reset_seq_oss, |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | /* |
| 57 | * register OSS synth |
| 58 | */ |
| 59 | |
| 60 | void |
| 61 | snd_emux_init_seq_oss(snd_emux_t *emu) |
| 62 | { |
| 63 | snd_seq_oss_reg_t *arg; |
| 64 | snd_seq_device_t *dev; |
| 65 | |
| 66 | if (snd_seq_device_new(emu->card, 0, SNDRV_SEQ_DEV_ID_OSS, |
| 67 | sizeof(snd_seq_oss_reg_t), &dev) < 0) |
| 68 | return; |
| 69 | |
| 70 | emu->oss_synth = dev; |
| 71 | strcpy(dev->name, emu->name); |
| 72 | arg = SNDRV_SEQ_DEVICE_ARGPTR(dev); |
| 73 | arg->type = SYNTH_TYPE_SAMPLE; |
| 74 | arg->subtype = SAMPLE_TYPE_AWE32; |
| 75 | arg->nvoices = emu->max_voices; |
| 76 | arg->oper = oss_callback; |
| 77 | arg->private_data = emu; |
| 78 | |
| 79 | /* register to OSS synth table */ |
| 80 | snd_device_register(emu->card, dev); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * unregister |
| 86 | */ |
| 87 | void |
| 88 | snd_emux_detach_seq_oss(snd_emux_t *emu) |
| 89 | { |
| 90 | if (emu->oss_synth) { |
| 91 | snd_device_free(emu->card, emu->oss_synth); |
| 92 | emu->oss_synth = NULL; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /* use port number as a unique soundfont client number */ |
| 98 | #define SF_CLIENT_NO(p) ((p) + 0x1000) |
| 99 | |
| 100 | /* |
| 101 | * open port for OSS sequencer |
| 102 | */ |
| 103 | static int |
| 104 | snd_emux_open_seq_oss(snd_seq_oss_arg_t *arg, void *closure) |
| 105 | { |
| 106 | snd_emux_t *emu; |
| 107 | snd_emux_port_t *p; |
| 108 | snd_seq_port_callback_t callback; |
| 109 | char tmpname[64]; |
| 110 | |
| 111 | emu = closure; |
| 112 | snd_assert(arg != NULL && emu != NULL, return -ENXIO); |
| 113 | |
| 114 | down(&emu->register_mutex); |
| 115 | |
| 116 | if (!snd_emux_inc_count(emu)) { |
| 117 | up(&emu->register_mutex); |
| 118 | return -EFAULT; |
| 119 | } |
| 120 | |
| 121 | memset(&callback, 0, sizeof(callback)); |
| 122 | callback.owner = THIS_MODULE; |
| 123 | callback.event_input = snd_emux_event_oss_input; |
| 124 | |
| 125 | sprintf(tmpname, "%s OSS Port", emu->name); |
| 126 | p = snd_emux_create_port(emu, tmpname, 32, |
| 127 | 1, &callback); |
| 128 | if (p == NULL) { |
| 129 | snd_printk("can't create port\n"); |
| 130 | snd_emux_dec_count(emu); |
| 131 | up(&emu->register_mutex); |
| 132 | return -ENOMEM; |
| 133 | } |
| 134 | |
| 135 | /* fill the argument data */ |
| 136 | arg->private_data = p; |
| 137 | arg->addr.client = p->chset.client; |
| 138 | arg->addr.port = p->chset.port; |
| 139 | p->oss_arg = arg; |
| 140 | |
| 141 | reset_port_mode(p, arg->seq_mode); |
| 142 | |
| 143 | snd_emux_reset_port(p); |
| 144 | |
| 145 | up(&emu->register_mutex); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | #define DEFAULT_DRUM_FLAGS ((1<<9) | (1<<25)) |
| 151 | |
| 152 | /* |
| 153 | * reset port mode |
| 154 | */ |
| 155 | static void |
| 156 | reset_port_mode(snd_emux_port_t *port, int midi_mode) |
| 157 | { |
| 158 | if (midi_mode) { |
| 159 | port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_MIDI; |
| 160 | port->drum_flags = DEFAULT_DRUM_FLAGS; |
| 161 | port->volume_atten = 0; |
| 162 | port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_KEYPRESS; |
| 163 | } else { |
| 164 | port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_SYNTH; |
| 165 | port->drum_flags = 0; |
| 166 | port->volume_atten = 32; |
| 167 | port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /* |
| 173 | * close port |
| 174 | */ |
| 175 | static int |
| 176 | snd_emux_close_seq_oss(snd_seq_oss_arg_t *arg) |
| 177 | { |
| 178 | snd_emux_t *emu; |
| 179 | snd_emux_port_t *p; |
| 180 | |
| 181 | snd_assert(arg != NULL, return -ENXIO); |
| 182 | p = arg->private_data; |
| 183 | snd_assert(p != NULL, return -ENXIO); |
| 184 | |
| 185 | emu = p->emu; |
| 186 | snd_assert(emu != NULL, return -ENXIO); |
| 187 | |
| 188 | down(&emu->register_mutex); |
| 189 | snd_emux_sounds_off_all(p); |
| 190 | snd_soundfont_close_check(emu->sflist, SF_CLIENT_NO(p->chset.port)); |
| 191 | snd_seq_event_port_detach(p->chset.client, p->chset.port); |
| 192 | snd_emux_dec_count(emu); |
| 193 | |
| 194 | up(&emu->register_mutex); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* |
| 200 | * load patch |
| 201 | */ |
| 202 | static int |
| 203 | snd_emux_load_patch_seq_oss(snd_seq_oss_arg_t *arg, int format, |
| 204 | const char __user *buf, int offs, int count) |
| 205 | { |
| 206 | snd_emux_t *emu; |
| 207 | snd_emux_port_t *p; |
| 208 | int rc; |
| 209 | |
| 210 | snd_assert(arg != NULL, return -ENXIO); |
| 211 | p = arg->private_data; |
| 212 | snd_assert(p != NULL, return -ENXIO); |
| 213 | |
| 214 | emu = p->emu; |
| 215 | snd_assert(emu != NULL, return -ENXIO); |
| 216 | |
| 217 | if (format == GUS_PATCH) |
| 218 | rc = snd_soundfont_load_guspatch(emu->sflist, buf, count, |
| 219 | SF_CLIENT_NO(p->chset.port)); |
| 220 | else if (format == SNDRV_OSS_SOUNDFONT_PATCH) { |
| 221 | soundfont_patch_info_t patch; |
| 222 | if (count < (int)sizeof(patch)) |
| 223 | rc = -EINVAL; |
| 224 | if (copy_from_user(&patch, buf, sizeof(patch))) |
| 225 | rc = -EFAULT; |
| 226 | if (patch.type >= SNDRV_SFNT_LOAD_INFO && |
| 227 | patch.type <= SNDRV_SFNT_PROBE_DATA) |
| 228 | rc = snd_soundfont_load(emu->sflist, buf, count, SF_CLIENT_NO(p->chset.port)); |
| 229 | else { |
| 230 | if (emu->ops.load_fx) |
| 231 | rc = emu->ops.load_fx(emu, patch.type, patch.optarg, buf, count); |
| 232 | else |
| 233 | rc = -EINVAL; |
| 234 | } |
| 235 | } else |
| 236 | rc = 0; |
| 237 | return rc; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | * ioctl |
| 243 | */ |
| 244 | static int |
| 245 | snd_emux_ioctl_seq_oss(snd_seq_oss_arg_t *arg, unsigned int cmd, unsigned long ioarg) |
| 246 | { |
| 247 | snd_emux_port_t *p; |
| 248 | snd_emux_t *emu; |
| 249 | |
| 250 | snd_assert(arg != NULL, return -ENXIO); |
| 251 | p = arg->private_data; |
| 252 | snd_assert(p != NULL, return -ENXIO); |
| 253 | |
| 254 | emu = p->emu; |
| 255 | snd_assert(emu != NULL, return -ENXIO); |
| 256 | |
| 257 | switch (cmd) { |
| 258 | case SNDCTL_SEQ_RESETSAMPLES: |
| 259 | snd_soundfont_remove_samples(emu->sflist); |
| 260 | return 0; |
| 261 | |
| 262 | case SNDCTL_SYNTH_MEMAVL: |
| 263 | if (emu->memhdr) |
| 264 | return snd_util_mem_avail(emu->memhdr); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /* |
| 273 | * reset device |
| 274 | */ |
| 275 | static int |
| 276 | snd_emux_reset_seq_oss(snd_seq_oss_arg_t *arg) |
| 277 | { |
| 278 | snd_emux_port_t *p; |
| 279 | |
| 280 | snd_assert(arg != NULL, return -ENXIO); |
| 281 | p = arg->private_data; |
| 282 | snd_assert(p != NULL, return -ENXIO); |
| 283 | snd_emux_reset_port(p); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /* |
| 289 | * receive raw events: only SEQ_PRIVATE is accepted. |
| 290 | */ |
| 291 | static int |
| 292 | snd_emux_event_oss_input(snd_seq_event_t *ev, int direct, void *private_data, |
| 293 | int atomic, int hop) |
| 294 | { |
| 295 | snd_emux_t *emu; |
| 296 | snd_emux_port_t *p; |
| 297 | unsigned char cmd, *data; |
| 298 | |
| 299 | p = private_data; |
| 300 | snd_assert(p != NULL, return -EINVAL); |
| 301 | emu = p->emu; |
| 302 | snd_assert(emu != NULL, return -EINVAL); |
| 303 | if (ev->type != SNDRV_SEQ_EVENT_OSS) |
| 304 | return snd_emux_event_input(ev, direct, private_data, atomic, hop); |
| 305 | |
| 306 | data = ev->data.raw8.d; |
| 307 | /* only SEQ_PRIVATE is accepted */ |
| 308 | if (data[0] != 0xfe) |
| 309 | return 0; |
| 310 | cmd = data[2] & _EMUX_OSS_MODE_VALUE_MASK; |
| 311 | if (data[2] & _EMUX_OSS_MODE_FLAG) |
| 312 | emuspec_control(emu, p, cmd, data, atomic, hop); |
| 313 | else |
| 314 | gusspec_control(emu, p, cmd, data, atomic, hop); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /* |
| 320 | * OSS/AWE driver specific h/w controls |
| 321 | */ |
| 322 | static void |
| 323 | emuspec_control(snd_emux_t *emu, snd_emux_port_t *port, int cmd, |
| 324 | unsigned char *event, int atomic, int hop) |
| 325 | { |
| 326 | int voice; |
| 327 | unsigned short p1; |
| 328 | short p2; |
| 329 | int i; |
| 330 | snd_midi_channel_t *chan; |
| 331 | |
| 332 | voice = event[3]; |
| 333 | if (voice < 0 || voice >= port->chset.max_channels) |
| 334 | chan = NULL; |
| 335 | else |
| 336 | chan = &port->chset.channels[voice]; |
| 337 | |
| 338 | p1 = *(unsigned short *) &event[4]; |
| 339 | p2 = *(short *) &event[6]; |
| 340 | |
| 341 | switch (cmd) { |
| 342 | #if 0 /* don't do this atomically */ |
| 343 | case _EMUX_OSS_REMOVE_LAST_SAMPLES: |
| 344 | snd_soundfont_remove_unlocked(emu->sflist); |
| 345 | break; |
| 346 | #endif |
| 347 | case _EMUX_OSS_SEND_EFFECT: |
| 348 | if (chan) |
| 349 | snd_emux_send_effect_oss(port, chan, p1, p2); |
| 350 | break; |
| 351 | |
| 352 | case _EMUX_OSS_TERMINATE_ALL: |
| 353 | snd_emux_terminate_all(emu); |
| 354 | break; |
| 355 | |
| 356 | case _EMUX_OSS_TERMINATE_CHANNEL: |
| 357 | /*snd_emux_mute_channel(emu, chan);*/ |
| 358 | break; |
| 359 | case _EMUX_OSS_RESET_CHANNEL: |
| 360 | /*snd_emux_channel_init(chset, chan);*/ |
| 361 | break; |
| 362 | |
| 363 | case _EMUX_OSS_RELEASE_ALL: |
| 364 | fake_event(emu, port, voice, MIDI_CTL_ALL_NOTES_OFF, 0, atomic, hop); |
| 365 | break; |
| 366 | case _EMUX_OSS_NOTEOFF_ALL: |
| 367 | fake_event(emu, port, voice, MIDI_CTL_ALL_SOUNDS_OFF, 0, atomic, hop); |
| 368 | break; |
| 369 | |
| 370 | case _EMUX_OSS_INITIAL_VOLUME: |
| 371 | if (p2) { |
| 372 | port->volume_atten = (short)p1; |
| 373 | snd_emux_update_port(port, SNDRV_EMUX_UPDATE_VOLUME); |
| 374 | } |
| 375 | break; |
| 376 | |
| 377 | case _EMUX_OSS_CHN_PRESSURE: |
| 378 | if (chan) { |
| 379 | chan->midi_pressure = p1; |
| 380 | snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_FMMOD|SNDRV_EMUX_UPDATE_FM2FRQ2); |
| 381 | } |
| 382 | break; |
| 383 | |
| 384 | case _EMUX_OSS_CHANNEL_MODE: |
| 385 | reset_port_mode(port, p1); |
| 386 | snd_emux_reset_port(port); |
| 387 | break; |
| 388 | |
| 389 | case _EMUX_OSS_DRUM_CHANNELS: |
| 390 | port->drum_flags = *(unsigned int*)&event[4]; |
| 391 | for (i = 0; i < port->chset.max_channels; i++) { |
| 392 | chan = &port->chset.channels[i]; |
| 393 | chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0; |
| 394 | } |
| 395 | break; |
| 396 | |
| 397 | case _EMUX_OSS_MISC_MODE: |
| 398 | if (p1 < EMUX_MD_END) |
| 399 | port->ctrls[p1] = p2; |
| 400 | break; |
| 401 | case _EMUX_OSS_DEBUG_MODE: |
| 402 | break; |
| 403 | |
| 404 | default: |
| 405 | if (emu->ops.oss_ioctl) |
| 406 | emu->ops.oss_ioctl(emu, cmd, p1, p2); |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * GUS specific h/w controls |
| 413 | */ |
| 414 | |
| 415 | #include <linux/ultrasound.h> |
| 416 | |
| 417 | static void |
| 418 | gusspec_control(snd_emux_t *emu, snd_emux_port_t *port, int cmd, |
| 419 | unsigned char *event, int atomic, int hop) |
| 420 | { |
| 421 | int voice; |
| 422 | unsigned short p1; |
| 423 | short p2; |
| 424 | int plong; |
| 425 | snd_midi_channel_t *chan; |
| 426 | |
| 427 | if (port->port_mode != SNDRV_EMUX_PORT_MODE_OSS_SYNTH) |
| 428 | return; |
| 429 | if (cmd == _GUS_NUMVOICES) |
| 430 | return; |
| 431 | voice = event[3]; |
| 432 | if (voice < 0 || voice >= port->chset.max_channels) |
| 433 | return; |
| 434 | |
| 435 | chan = &port->chset.channels[voice]; |
| 436 | |
| 437 | p1 = *(unsigned short *) &event[4]; |
| 438 | p2 = *(short *) &event[6]; |
| 439 | plong = *(int*) &event[4]; |
| 440 | |
| 441 | switch (cmd) { |
| 442 | case _GUS_VOICESAMPLE: |
| 443 | chan->midi_program = p1; |
| 444 | return; |
| 445 | |
| 446 | case _GUS_VOICEBALA: |
| 447 | /* 0 to 15 --> 0 to 127 */ |
| 448 | chan->control[MIDI_CTL_MSB_PAN] = (int)p1 << 3; |
| 449 | snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN); |
| 450 | return; |
| 451 | |
| 452 | case _GUS_VOICEVOL: |
| 453 | case _GUS_VOICEVOL2: |
| 454 | /* not supported yet */ |
| 455 | return; |
| 456 | |
| 457 | case _GUS_RAMPRANGE: |
| 458 | case _GUS_RAMPRATE: |
| 459 | case _GUS_RAMPMODE: |
| 460 | case _GUS_RAMPON: |
| 461 | case _GUS_RAMPOFF: |
| 462 | /* volume ramping not supported */ |
| 463 | return; |
| 464 | |
| 465 | case _GUS_VOLUME_SCALE: |
| 466 | return; |
| 467 | |
| 468 | case _GUS_VOICE_POS: |
| 469 | #ifdef SNDRV_EMUX_USE_RAW_EFFECT |
| 470 | snd_emux_send_effect(port, chan, EMUX_FX_SAMPLE_START, |
| 471 | (short)(plong & 0x7fff), |
| 472 | EMUX_FX_FLAG_SET); |
| 473 | snd_emux_send_effect(port, chan, EMUX_FX_COARSE_SAMPLE_START, |
| 474 | (plong >> 15) & 0xffff, |
| 475 | EMUX_FX_FLAG_SET); |
| 476 | #endif |
| 477 | return; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | |
| 482 | /* |
| 483 | * send an event to midi emulation |
| 484 | */ |
| 485 | static void |
| 486 | fake_event(snd_emux_t *emu, snd_emux_port_t *port, int ch, int param, int val, int atomic, int hop) |
| 487 | { |
| 488 | snd_seq_event_t ev; |
| 489 | memset(&ev, 0, sizeof(ev)); |
| 490 | ev.type = SNDRV_SEQ_EVENT_CONTROLLER; |
| 491 | ev.data.control.channel = ch; |
| 492 | ev.data.control.param = param; |
| 493 | ev.data.control.value = val; |
| 494 | snd_emux_event_input(&ev, 0, port, atomic, hop); |
| 495 | } |
| 496 | |
| 497 | #endif /* CONFIG_SND_SEQUENCER_OSS */ |