Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Driver for Gravis UltraSound Extreme soundcards
0004  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0005  */
0006 
0007 #include <linux/init.h>
0008 #include <linux/err.h>
0009 #include <linux/isa.h>
0010 #include <linux/delay.h>
0011 #include <linux/time.h>
0012 #include <linux/module.h>
0013 #include <asm/dma.h>
0014 #include <sound/core.h>
0015 #include <sound/gus.h>
0016 #include <sound/es1688.h>
0017 #include <sound/mpu401.h>
0018 #include <sound/opl3.h>
0019 #define SNDRV_LEGACY_AUTO_PROBE
0020 #define SNDRV_LEGACY_FIND_FREE_IRQ
0021 #define SNDRV_LEGACY_FIND_FREE_DMA
0022 #include <sound/initval.h>
0023 
0024 #define CRD_NAME "Gravis UltraSound Extreme"
0025 #define DEV_NAME "gusextreme"
0026 
0027 MODULE_DESCRIPTION(CRD_NAME);
0028 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0029 MODULE_LICENSE("GPL");
0030 
0031 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;  /* Index 0-MAX */
0032 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;   /* ID for this card */
0033 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
0034 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
0035 static long gf1_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x210,0x220,0x230,0x240,0x250,0x260,0x270 */
0036 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x300,0x310,0x320 */
0037 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* 5,7,9,10 */
0038 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* 5,7,9,10 */
0039 static int gf1_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* 2,3,5,9,11,12,15 */
0040 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;   /* 0,1,3 */
0041 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
0042 static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
0043                 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
0044 static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
0045 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
0046 
0047 module_param_array(index, int, NULL, 0444);
0048 MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
0049 module_param_array(id, charp, NULL, 0444);
0050 MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
0051 module_param_array(enable, bool, NULL, 0444);
0052 MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
0053 module_param_hw_array(port, long, ioport, NULL, 0444);
0054 MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
0055 module_param_hw_array(gf1_port, long, ioport, NULL, 0444);
0056 MODULE_PARM_DESC(gf1_port, "GF1 port # for " CRD_NAME " driver (optional).");
0057 module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
0058 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver.");
0059 module_param_hw_array(irq, int, irq, NULL, 0444);
0060 MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
0061 module_param_hw_array(mpu_irq, int, irq, NULL, 0444);
0062 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver.");
0063 module_param_hw_array(gf1_irq, int, irq, NULL, 0444);
0064 MODULE_PARM_DESC(gf1_irq, "GF1 IRQ # for " CRD_NAME " driver.");
0065 module_param_hw_array(dma8, int, dma, NULL, 0444);
0066 MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver.");
0067 module_param_hw_array(dma1, int, dma, NULL, 0444);
0068 MODULE_PARM_DESC(dma1, "GF1 DMA # for " CRD_NAME " driver.");
0069 module_param_array(joystick_dac, int, NULL, 0444);
0070 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for " CRD_NAME " driver.");
0071 module_param_array(channels, int, NULL, 0444);
0072 MODULE_PARM_DESC(channels, "GF1 channels for " CRD_NAME " driver.");
0073 module_param_array(pcm_channels, int, NULL, 0444);
0074 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for " CRD_NAME " driver.");
0075 
0076 static int snd_gusextreme_match(struct device *dev, unsigned int n)
0077 {
0078     return enable[n];
0079 }
0080 
0081 static int snd_gusextreme_es1688_create(struct snd_card *card,
0082                     struct snd_es1688 *chip,
0083                     struct device *dev, unsigned int n)
0084 {
0085     static const long possible_ports[] = {0x220, 0x240, 0x260};
0086     static const int possible_irqs[] = {5, 9, 10, 7, -1};
0087     static const int possible_dmas[] = {1, 3, 0, -1};
0088 
0089     int i, error;
0090 
0091     if (irq[n] == SNDRV_AUTO_IRQ) {
0092         irq[n] = snd_legacy_find_free_irq(possible_irqs);
0093         if (irq[n] < 0) {
0094             dev_err(dev, "unable to find a free IRQ for ES1688\n");
0095             return -EBUSY;
0096         }
0097     }
0098     if (dma8[n] == SNDRV_AUTO_DMA) {
0099         dma8[n] = snd_legacy_find_free_dma(possible_dmas);
0100         if (dma8[n] < 0) {
0101             dev_err(dev, "unable to find a free DMA for ES1688\n");
0102             return -EBUSY;
0103         }
0104     }
0105 
0106     if (port[n] != SNDRV_AUTO_PORT)
0107         return snd_es1688_create(card, chip, port[n], mpu_port[n],
0108                 irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688);
0109 
0110     i = 0;
0111     do {
0112         port[n] = possible_ports[i];
0113         error = snd_es1688_create(card, chip, port[n], mpu_port[n],
0114                 irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688);
0115     } while (error < 0 && ++i < ARRAY_SIZE(possible_ports));
0116 
0117     return error;
0118 }
0119 
0120 static int snd_gusextreme_gus_card_create(struct snd_card *card,
0121                       struct device *dev, unsigned int n,
0122                       struct snd_gus_card **rgus)
0123 {
0124     static const int possible_irqs[] = {11, 12, 15, 9, 5, 7, 3, -1};
0125     static const int possible_dmas[] = {5, 6, 7, 3, 1, -1};
0126 
0127     if (gf1_irq[n] == SNDRV_AUTO_IRQ) {
0128         gf1_irq[n] = snd_legacy_find_free_irq(possible_irqs);
0129         if (gf1_irq[n] < 0) {
0130             dev_err(dev, "unable to find a free IRQ for GF1\n");
0131             return -EBUSY;
0132         }
0133     }
0134     if (dma1[n] == SNDRV_AUTO_DMA) {
0135         dma1[n] = snd_legacy_find_free_dma(possible_dmas);
0136         if (dma1[n] < 0) {
0137             dev_err(dev, "unable to find a free DMA for GF1\n");
0138             return -EBUSY;
0139         }
0140     }
0141     return snd_gus_create(card, gf1_port[n], gf1_irq[n], dma1[n], -1,
0142             0, channels[n], pcm_channels[n], 0, rgus);
0143 }
0144 
0145 static int snd_gusextreme_detect(struct snd_gus_card *gus,
0146                  struct snd_es1688 *es1688)
0147 {
0148     unsigned long flags;
0149     unsigned char d;
0150 
0151     /*
0152      * This is main stuff - enable access to GF1 chip...
0153      * I'm not sure, if this will work for card which have
0154      * ES1688 chip in another place than 0x220.
0155          *
0156          * I used reverse-engineering in DOSEMU. [--jk]
0157      *
0158      * ULTRINIT.EXE:
0159      * 0x230 = 0,2,3
0160      * 0x240 = 2,0,1
0161      * 0x250 = 2,0,3
0162      * 0x260 = 2,2,1
0163      */
0164 
0165     spin_lock_irqsave(&es1688->mixer_lock, flags);
0166     snd_es1688_mixer_write(es1688, 0x40, 0x0b); /* don't change!!! */
0167     spin_unlock_irqrestore(&es1688->mixer_lock, flags);
0168 
0169     spin_lock_irqsave(&es1688->reg_lock, flags);
0170     outb(gus->gf1.port & 0x040 ? 2 : 0, ES1688P(es1688, INIT1));
0171     outb(0, 0x201);
0172     outb(gus->gf1.port & 0x020 ? 2 : 0, ES1688P(es1688, INIT1));
0173     outb(0, 0x201);
0174     outb(gus->gf1.port & 0x010 ? 3 : 1, ES1688P(es1688, INIT1));
0175     spin_unlock_irqrestore(&es1688->reg_lock, flags);
0176 
0177     udelay(100);
0178 
0179     snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0);   /* reset GF1 */
0180     d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET);
0181     if ((d & 0x07) != 0) {
0182         snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
0183         return -EIO;
0184     }
0185     udelay(160);
0186     snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);   /* release reset */
0187     udelay(160);
0188     d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET);
0189     if ((d & 0x07) != 1) {
0190         snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
0191         return -EIO;
0192     }
0193 
0194     return 0;
0195 }
0196 
0197 static int snd_gusextreme_mixer(struct snd_card *card)
0198 {
0199     struct snd_ctl_elem_id id1, id2;
0200     int error;
0201 
0202     memset(&id1, 0, sizeof(id1));
0203     memset(&id2, 0, sizeof(id2));
0204     id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
0205 
0206     /* reassign AUX to SYNTHESIZER */
0207     strcpy(id1.name, "Aux Playback Volume");
0208     strcpy(id2.name, "Synth Playback Volume");
0209     error = snd_ctl_rename_id(card, &id1, &id2);
0210     if (error < 0)
0211         return error;
0212 
0213     /* reassign Master Playback Switch to Synth Playback Switch */
0214     strcpy(id1.name, "Master Playback Switch");
0215     strcpy(id2.name, "Synth Playback Switch");
0216     error = snd_ctl_rename_id(card, &id1, &id2);
0217     if (error < 0)
0218         return error;
0219 
0220     return 0;
0221 }
0222 
0223 static int snd_gusextreme_probe(struct device *dev, unsigned int n)
0224 {
0225     struct snd_card *card;
0226     struct snd_gus_card *gus;
0227     struct snd_es1688 *es1688;
0228     struct snd_opl3 *opl3;
0229     int error;
0230 
0231     error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE,
0232                   sizeof(struct snd_es1688), &card);
0233     if (error < 0)
0234         return error;
0235 
0236     es1688 = card->private_data;
0237 
0238     if (mpu_port[n] == SNDRV_AUTO_PORT)
0239         mpu_port[n] = 0;
0240 
0241     if (mpu_irq[n] > 15)
0242         mpu_irq[n] = -1;
0243 
0244     error = snd_gusextreme_es1688_create(card, es1688, dev, n);
0245     if (error < 0)
0246         return error;
0247 
0248     if (gf1_port[n] < 0)
0249         gf1_port[n] = es1688->port + 0x20;
0250 
0251     error = snd_gusextreme_gus_card_create(card, dev, n, &gus);
0252     if (error < 0)
0253         return error;
0254 
0255     error = snd_gusextreme_detect(gus, es1688);
0256     if (error < 0)
0257         return error;
0258 
0259     gus->joystick_dac = joystick_dac[n];
0260 
0261     error = snd_gus_initialize(gus);
0262     if (error < 0)
0263         return error;
0264 
0265     error = -ENODEV;
0266     if (!gus->ess_flag) {
0267         dev_err(dev, "GUS Extreme soundcard was not "
0268             "detected at 0x%lx\n", gus->gf1.port);
0269         return error;
0270     }
0271     gus->codec_flag = 1;
0272 
0273     error = snd_es1688_pcm(card, es1688, 0);
0274     if (error < 0)
0275         return error;
0276 
0277     error = snd_es1688_mixer(card, es1688);
0278     if (error < 0)
0279         return error;
0280 
0281     snd_component_add(card, "ES1688");
0282 
0283     if (pcm_channels[n] > 0) {
0284         error = snd_gf1_pcm_new(gus, 1, 1);
0285         if (error < 0)
0286             return error;
0287     }
0288 
0289     error = snd_gf1_new_mixer(gus);
0290     if (error < 0)
0291         return error;
0292 
0293     error = snd_gusextreme_mixer(card);
0294     if (error < 0)
0295         return error;
0296 
0297     if (snd_opl3_create(card, es1688->port, es1688->port + 2,
0298             OPL3_HW_OPL3, 0, &opl3) < 0)
0299         dev_warn(dev, "opl3 not detected at 0x%lx\n", es1688->port);
0300     else {
0301         error = snd_opl3_hwdep_new(opl3, 0, 2, NULL);
0302         if (error < 0)
0303             return error;
0304     }
0305 
0306     if (es1688->mpu_port >= 0x300) {
0307         error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
0308                 es1688->mpu_port, 0, mpu_irq[n], NULL);
0309         if (error < 0)
0310             return error;
0311     }
0312 
0313     sprintf(card->longname, "Gravis UltraSound Extreme at 0x%lx, "
0314         "irq %i&%i, dma %i&%i", es1688->port,
0315         gus->gf1.irq, es1688->irq, gus->gf1.dma1, es1688->dma8);
0316 
0317     error = snd_card_register(card);
0318     if (error < 0)
0319         return error;
0320 
0321     dev_set_drvdata(dev, card);
0322     return 0;
0323 }
0324 
0325 static struct isa_driver snd_gusextreme_driver = {
0326     .match      = snd_gusextreme_match,
0327     .probe      = snd_gusextreme_probe,
0328 #if 0   /* FIXME */
0329     .suspend    = snd_gusextreme_suspend,
0330     .resume     = snd_gusextreme_resume,
0331 #endif
0332     .driver     = {
0333         .name   = DEV_NAME
0334     }
0335 };
0336 
0337 module_isa_driver(snd_gusextreme_driver, SNDRV_CARDS);