linux/sound/drivers/opl3/opl3_oss.c
<<
>>
Prefs
   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
  24static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
  25static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg);
  26static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg);
  27static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, const char __user *buf, int offs, int count);
  28static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg);
  29
  30/* */
  31
  32static 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
  39static inline void snd_leave_user(mm_segment_t fs)
  40{
  41        set_fs(fs);
  42}
  43
  44/* operators */
  45
  46extern struct snd_midi_op opl3_ops;
  47
  48static struct snd_seq_oss_callback 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
  57static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
  58                                    void *private_data, int atomic, int hop)
  59{
  60        struct snd_opl3 *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
  69static void snd_opl3_oss_free_port(void *private_data)
  70{
  71        struct snd_opl3 *opl3 = private_data;
  72
  73        snd_midi_channel_free_set(opl3->oss_chset);
  74}
  75
  76static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
  77{
  78        struct snd_seq_port_callback 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_HARDWARE |
 104                                                          SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
 105                                                          voices, voices,
 106                                                          name);
 107        if (opl3->oss_chset->port < 0) {
 108                int port;
 109                port = opl3->oss_chset->port;
 110                snd_midi_channel_free_set(opl3->oss_chset);
 111                return port;
 112        }
 113        return 0;
 114}
 115
 116/* ------------------------------ */
 117
 118/* register OSS synth */
 119void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
 120{
 121        struct snd_seq_oss_reg *arg;
 122        struct snd_seq_device *dev;
 123
 124        if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
 125                               sizeof(struct snd_seq_oss_reg), &dev) < 0)
 126                return;
 127
 128        opl3->oss_seq_dev = dev;
 129        strlcpy(dev->name, name, sizeof(dev->name));
 130        arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
 131        arg->type = SYNTH_TYPE_FM;
 132        if (opl3->hardware < OPL3_HW_OPL3) {
 133                arg->subtype = FM_TYPE_ADLIB;
 134                arg->nvoices = MAX_OPL2_VOICES;
 135        } else {
 136                arg->subtype = FM_TYPE_OPL3;
 137                arg->nvoices = MAX_OPL3_VOICES;
 138        }
 139        arg->oper = oss_callback;
 140        arg->private_data = opl3;
 141
 142        if (snd_opl3_oss_create_port(opl3)) {
 143                /* register to OSS synth table */
 144                snd_device_register(opl3->card, dev);
 145        }
 146}
 147
 148/* unregister */
 149void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
 150{
 151        if (opl3->oss_seq_dev) {
 152                /* The instance should have been released in prior */
 153                opl3->oss_seq_dev = NULL;
 154        }
 155}
 156
 157/* ------------------------------ */
 158
 159/* open OSS sequencer */
 160static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
 161{
 162        struct snd_opl3 *opl3 = closure;
 163        int err;
 164
 165        snd_assert(arg != NULL, return -ENXIO);
 166
 167        if ((err = snd_opl3_synth_setup(opl3)) < 0)
 168                return err;
 169
 170        /* fill the argument data */
 171        arg->private_data = opl3;
 172        arg->addr.client = opl3->oss_chset->client;
 173        arg->addr.port = opl3->oss_chset->port;
 174
 175        if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
 176                return err;
 177
 178        opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
 179        return 0;
 180}
 181
 182/* close OSS sequencer */
 183static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
 184{
 185        struct snd_opl3 *opl3;
 186
 187        snd_assert(arg != NULL, return -ENXIO);
 188        opl3 = arg->private_data;
 189
 190        snd_opl3_synth_cleanup(opl3);
 191
 192        snd_opl3_synth_use_dec(opl3);
 193        return 0;
 194}
 195
 196/* load patch */
 197
 198/* offsets for SBI params */
 199#define AM_VIB          0
 200#define KSL_LEVEL       2
 201#define ATTACK_DECAY    4
 202#define SUSTAIN_RELEASE 6
 203#define WAVE_SELECT     8
 204
 205/* offset for SBI instrument */
 206#define CONNECTION      10
 207#define OFFSET_4OP      11
 208
 209/* from sound_config.h */
 210#define SBFM_MAXINSTR   256
 211
 212static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
 213                                       const char __user *buf, int offs, int count)
 214{
 215        struct snd_opl3 *opl3;
 216        int err = -EINVAL;
 217
 218        snd_assert(arg != NULL, return -ENXIO);
 219        opl3 = arg->private_data;
 220
 221        if ((format == FM_PATCH) || (format == OPL3_PATCH)) {
 222                struct sbi_instrument sbi;
 223
 224                size_t size;
 225                struct snd_seq_instr_header *put;
 226                struct snd_seq_instr_data *data;
 227                struct fm_xinstrument *xinstr;
 228
 229                struct snd_seq_event ev;
 230                int i;
 231
 232                mm_segment_t fs;
 233
 234                if (count < (int)sizeof(sbi)) {
 235                        snd_printk("FM Error: Patch record too short\n");
 236                        return -EINVAL;
 237                }
 238                if (copy_from_user(&sbi, buf, sizeof(sbi)))
 239                        return -EFAULT;
 240
 241                if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
 242                        snd_printk("FM Error: Invalid instrument number %d\n", sbi.channel);
 243                        return -EINVAL;
 244                }
 245
 246                size = sizeof(*put) + sizeof(struct fm_xinstrument);
 247                put = kzalloc(size, GFP_KERNEL);
 248                if (put == NULL)
 249                        return -ENOMEM;
 250                /* build header */
 251                data = &put->data;
 252                data->type = SNDRV_SEQ_INSTR_ATYPE_DATA;
 253                strcpy(data->data.format, SNDRV_SEQ_INSTR_ID_OPL2_3);
 254                /* build data section */
 255                xinstr = (struct fm_xinstrument *)(data + 1);
 256                xinstr->stype = FM_STRU_INSTR;
 257        
 258                for (i = 0; i < 2; i++) {
 259                        xinstr->op[i].am_vib = sbi.operators[AM_VIB + i];
 260                        xinstr->op[i].ksl_level = sbi.operators[KSL_LEVEL + i];
 261                        xinstr->op[i].attack_decay = sbi.operators[ATTACK_DECAY + i];
 262                        xinstr->op[i].sustain_release = sbi.operators[SUSTAIN_RELEASE + i];
 263                        xinstr->op[i].wave_select = sbi.operators[WAVE_SELECT + i];
 264                }
 265                xinstr->feedback_connection[0] = sbi.operators[CONNECTION];
 266
 267                if (format == OPL3_PATCH) {
 268                        xinstr->type = FM_PATCH_OPL3;
 269                        for (i = 0; i < 2; i++) {
 270                                xinstr->op[i+2].am_vib = sbi.operators[OFFSET_4OP + AM_VIB + i];
 271                                xinstr->op[i+2].ksl_level = sbi.operators[OFFSET_4OP + KSL_LEVEL + i];
 272                                xinstr->op[i+2].attack_decay = sbi.operators[OFFSET_4OP + ATTACK_DECAY + i];
 273                                xinstr->op[i+2].sustain_release = sbi.operators[OFFSET_4OP + SUSTAIN_RELEASE + i];
 274                                xinstr->op[i+2].wave_select = sbi.operators[OFFSET_4OP + WAVE_SELECT + i];
 275                        }
 276                        xinstr->feedback_connection[1] = sbi.operators[OFFSET_4OP + CONNECTION];
 277                } else {
 278                        xinstr->type = FM_PATCH_OPL2;
 279                }
 280
 281                put->id.instr.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3;
 282                put->id.instr.bank = 127;
 283                put->id.instr.prg = sbi.channel;
 284                put->cmd = SNDRV_SEQ_INSTR_PUT_CMD_CREATE;
 285
 286                memset (&ev, 0, sizeof(ev));
 287                ev.source.client = SNDRV_SEQ_CLIENT_OSS;
 288                ev.dest = arg->addr; 
 289
 290                ev.flags = SNDRV_SEQ_EVENT_LENGTH_VARUSR;
 291                ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
 292
 293                fs = snd_enter_user();
 294        __again:
 295                ev.type = SNDRV_SEQ_EVENT_INSTR_PUT;
 296                ev.data.ext.len = size;
 297                ev.data.ext.ptr = put;
 298
 299                err = snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
 300                                    opl3->seq_client, 0, 0);
 301                if (err == -EBUSY) {
 302                        struct snd_seq_instr_header remove;
 303
 304                        memset (&remove, 0, sizeof(remove));
 305                        remove.cmd = SNDRV_SEQ_INSTR_FREE_CMD_SINGLE;
 306                        remove.id.instr = put->id.instr;
 307
 308                        /* remove instrument */
 309                        ev.type = SNDRV_SEQ_EVENT_INSTR_FREE;
 310                        ev.data.ext.len = sizeof(remove);
 311                        ev.data.ext.ptr = &remove;
 312
 313                        snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
 314                                            opl3->seq_client, 0, 0);
 315                        goto __again;
 316                }
 317                snd_leave_user(fs);
 318
 319                kfree(put);
 320        }
 321        return err;
 322}
 323
 324/* ioctl */
 325static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
 326                                  unsigned long ioarg)
 327{
 328        struct snd_opl3 *opl3;
 329
 330        snd_assert(arg != NULL, return -ENXIO);
 331        opl3 = arg->private_data;
 332        switch (cmd) {
 333                case SNDCTL_FM_LOAD_INSTR:
 334                        snd_printk("OPL3: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
 335                        return -EINVAL;
 336
 337                case SNDCTL_SYNTH_MEMAVL:
 338                        return 0x7fffffff;
 339
 340                case SNDCTL_FM_4OP_ENABLE:
 341                        // handled automatically by OPL instrument type
 342                        return 0;
 343
 344                default:
 345                        return -EINVAL;
 346        }
 347        return 0;
 348}
 349
 350/* reset device */
 351static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
 352{
 353        struct snd_opl3 *opl3;
 354
 355        snd_assert(arg != NULL, return -ENXIO);
 356        opl3 = arg->private_data;
 357
 358        return 0;
 359}
 360