Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Interface for OSS sequencer emulation
0004  *
0005  *  Copyright (C) 2000 Uros Bizjak <uros@kss-loka.si>
0006  */
0007 
0008 #include <linux/export.h>
0009 #include "opl3_voice.h"
0010 
0011 static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
0012 static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg);
0013 static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg);
0014 static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, const char __user *buf, int offs, int count);
0015 static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg);
0016 
0017 /* operators */
0018 
0019 static const struct snd_seq_oss_callback oss_callback = {
0020     .owner =    THIS_MODULE,
0021     .open =     snd_opl3_open_seq_oss,
0022     .close =    snd_opl3_close_seq_oss,
0023     .ioctl =    snd_opl3_ioctl_seq_oss,
0024     .load_patch =   snd_opl3_load_patch_seq_oss,
0025     .reset =    snd_opl3_reset_seq_oss,
0026 };
0027 
0028 static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct,
0029                     void *private_data, int atomic, int hop)
0030 {
0031     struct snd_opl3 *opl3 = private_data;
0032 
0033     if (ev->type != SNDRV_SEQ_EVENT_OSS)
0034         snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
0035     return 0;
0036 }
0037 
0038 /* ------------------------------ */
0039 
0040 static void snd_opl3_oss_free_port(void *private_data)
0041 {
0042     struct snd_opl3 *opl3 = private_data;
0043 
0044     snd_midi_channel_free_set(opl3->oss_chset);
0045 }
0046 
0047 static int snd_opl3_oss_create_port(struct snd_opl3 * opl3)
0048 {
0049     struct snd_seq_port_callback callbacks;
0050     char name[32];
0051     int voices, opl_ver;
0052 
0053     voices = (opl3->hardware < OPL3_HW_OPL3) ?
0054         MAX_OPL2_VOICES : MAX_OPL3_VOICES;
0055     opl3->oss_chset = snd_midi_channel_alloc_set(voices);
0056     if (opl3->oss_chset == NULL)
0057         return -ENOMEM;
0058     opl3->oss_chset->private_data = opl3;
0059 
0060     memset(&callbacks, 0, sizeof(callbacks));
0061     callbacks.owner = THIS_MODULE;
0062     callbacks.event_input = snd_opl3_oss_event_input;
0063     callbacks.private_free = snd_opl3_oss_free_port;
0064     callbacks.private_data = opl3;
0065 
0066     opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
0067     sprintf(name, "OPL%i OSS Port", opl_ver);
0068 
0069     opl3->oss_chset->client = opl3->seq_client;
0070     opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
0071                               SNDRV_SEQ_PORT_CAP_WRITE,
0072                               SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
0073                               SNDRV_SEQ_PORT_TYPE_MIDI_GM |
0074                               SNDRV_SEQ_PORT_TYPE_HARDWARE |
0075                               SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
0076                               voices, voices,
0077                               name);
0078     if (opl3->oss_chset->port < 0) {
0079         int port;
0080         port = opl3->oss_chset->port;
0081         snd_midi_channel_free_set(opl3->oss_chset);
0082         return port;
0083     }
0084     return 0;
0085 }
0086 
0087 /* ------------------------------ */
0088 
0089 /* register OSS synth */
0090 void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name)
0091 {
0092     struct snd_seq_oss_reg *arg;
0093     struct snd_seq_device *dev;
0094 
0095     if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
0096                    sizeof(struct snd_seq_oss_reg), &dev) < 0)
0097         return;
0098 
0099     opl3->oss_seq_dev = dev;
0100     strscpy(dev->name, name, sizeof(dev->name));
0101     arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
0102     arg->type = SYNTH_TYPE_FM;
0103     if (opl3->hardware < OPL3_HW_OPL3) {
0104         arg->subtype = FM_TYPE_ADLIB;
0105         arg->nvoices = MAX_OPL2_VOICES;
0106     } else {
0107         arg->subtype = FM_TYPE_OPL3;
0108         arg->nvoices = MAX_OPL3_VOICES;
0109     }
0110     arg->oper = oss_callback;
0111     arg->private_data = opl3;
0112 
0113     if (snd_opl3_oss_create_port(opl3)) {
0114         /* register to OSS synth table */
0115         snd_device_register(opl3->card, dev);
0116     }
0117 }
0118 
0119 /* unregister */
0120 void snd_opl3_free_seq_oss(struct snd_opl3 *opl3)
0121 {
0122     if (opl3->oss_seq_dev) {
0123         /* The instance should have been released in prior */
0124         opl3->oss_seq_dev = NULL;
0125     }
0126 }
0127 
0128 /* ------------------------------ */
0129 
0130 /* open OSS sequencer */
0131 static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
0132 {
0133     struct snd_opl3 *opl3 = closure;
0134     int err;
0135 
0136     if (snd_BUG_ON(!arg))
0137         return -ENXIO;
0138 
0139     err = snd_opl3_synth_setup(opl3);
0140     if (err < 0)
0141         return err;
0142 
0143     /* fill the argument data */
0144     arg->private_data = opl3;
0145     arg->addr.client = opl3->oss_chset->client;
0146     arg->addr.port = opl3->oss_chset->port;
0147 
0148     err = snd_opl3_synth_use_inc(opl3);
0149     if (err < 0)
0150         return err;
0151 
0152     opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
0153     return 0;
0154 }
0155 
0156 /* close OSS sequencer */
0157 static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg)
0158 {
0159     struct snd_opl3 *opl3;
0160 
0161     if (snd_BUG_ON(!arg))
0162         return -ENXIO;
0163     opl3 = arg->private_data;
0164 
0165     snd_opl3_synth_cleanup(opl3);
0166 
0167     snd_opl3_synth_use_dec(opl3);
0168     return 0;
0169 }
0170 
0171 /* load patch */
0172 
0173 /* from sound_config.h */
0174 #define SBFM_MAXINSTR   256
0175 
0176 static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
0177                        const char __user *buf, int offs, int count)
0178 {
0179     struct snd_opl3 *opl3;
0180     struct sbi_instrument sbi;
0181     char name[32];
0182     int err, type;
0183 
0184     if (snd_BUG_ON(!arg))
0185         return -ENXIO;
0186     opl3 = arg->private_data;
0187 
0188     if (format == FM_PATCH)
0189         type = FM_PATCH_OPL2;
0190     else if (format == OPL3_PATCH)
0191         type = FM_PATCH_OPL3;
0192     else
0193         return -EINVAL;
0194 
0195     if (count < (int)sizeof(sbi)) {
0196         snd_printk(KERN_ERR "FM Error: Patch record too short\n");
0197         return -EINVAL;
0198     }
0199     if (copy_from_user(&sbi, buf, sizeof(sbi)))
0200         return -EFAULT;
0201 
0202     if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
0203         snd_printk(KERN_ERR "FM Error: Invalid instrument number %d\n",
0204                sbi.channel);
0205         return -EINVAL;
0206     }
0207 
0208     memset(name, 0, sizeof(name));
0209     sprintf(name, "Chan%d", sbi.channel);
0210 
0211     err = snd_opl3_load_patch(opl3, sbi.channel, 127, type, name, NULL,
0212                   sbi.operators);
0213     if (err < 0)
0214         return err;
0215 
0216     return sizeof(sbi);
0217 }
0218 
0219 /* ioctl */
0220 static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
0221                   unsigned long ioarg)
0222 {
0223     if (snd_BUG_ON(!arg))
0224         return -ENXIO;
0225     switch (cmd) {
0226         case SNDCTL_FM_LOAD_INSTR:
0227             snd_printk(KERN_ERR "OPL3: "
0228                    "Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. "
0229                    "Fix the program.\n");
0230             return -EINVAL;
0231 
0232         case SNDCTL_SYNTH_MEMAVL:
0233             return 0x7fffffff;
0234 
0235         case SNDCTL_FM_4OP_ENABLE:
0236             // handled automatically by OPL instrument type
0237             return 0;
0238 
0239         default:
0240             return -EINVAL;
0241     }
0242     return 0;
0243 }
0244 
0245 /* reset device */
0246 static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg)
0247 {
0248     if (snd_BUG_ON(!arg))
0249         return -ENXIO;
0250 
0251     return 0;
0252 }