1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Gravis UltraSound Classic soundcard 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 */ 6 7 #include <linux/init.h> 8 #include <linux/err.h> 9 #include <linux/isa.h> 10 #include <linux/delay.h> 11 #include <linux/time.h> 12 #include <linux/module.h> 13 #include <asm/dma.h> 14 #include <sound/core.h> 15 #include <sound/gus.h> 16 #define SNDRV_LEGACY_FIND_FREE_IRQ 17 #define SNDRV_LEGACY_FIND_FREE_DMA 18 #include <sound/initval.h> 19 20 #define CRD_NAME "Gravis UltraSound Classic" 21 #define DEV_NAME "gusclassic" 22 23 MODULE_DESCRIPTION(CRD_NAME); 24 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 25 MODULE_LICENSE("GPL"); 26 27 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 28 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 29 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ 30 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */ 31 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,5,9,11,12,15 */ 32 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ 33 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ 34 static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; 35 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ 36 static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24}; 37 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; 38 39 module_param_array(index, int, NULL, 0444); 40 MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); 41 module_param_array(id, charp, NULL, 0444); 42 MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); 43 module_param_array(enable, bool, NULL, 0444); 44 MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); 45 module_param_hw_array(port, long, ioport, NULL, 0444); 46 MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); 47 module_param_hw_array(irq, int, irq, NULL, 0444); 48 MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver."); 49 module_param_hw_array(dma1, int, dma, NULL, 0444); 50 MODULE_PARM_DESC(dma1, "DMA1 # for " CRD_NAME " driver."); 51 module_param_hw_array(dma2, int, dma, NULL, 0444); 52 MODULE_PARM_DESC(dma2, "DMA2 # for " CRD_NAME " driver."); 53 module_param_array(joystick_dac, int, NULL, 0444); 54 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for " CRD_NAME " driver."); 55 module_param_array(channels, int, NULL, 0444); 56 MODULE_PARM_DESC(channels, "GF1 channels for " CRD_NAME " driver."); 57 module_param_array(pcm_channels, int, NULL, 0444); 58 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for " CRD_NAME " driver."); 59 60 static int snd_gusclassic_match(struct device *dev, unsigned int n) 61 { 62 return enable[n]; 63 } 64 65 static int snd_gusclassic_create(struct snd_card *card, 66 struct device *dev, unsigned int n, 67 struct snd_gus_card **rgus) 68 { 69 static const long possible_ports[] = {0x220, 0x230, 0x240, 0x250, 0x260}; 70 static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, 4, -1}; 71 static const int possible_dmas[] = {5, 6, 7, 1, 3, -1}; 72 73 int i, error; 74 75 if (irq[n] == SNDRV_AUTO_IRQ) { 76 irq[n] = snd_legacy_find_free_irq(possible_irqs); 77 if (irq[n] < 0) { 78 dev_err(dev, "unable to find a free IRQ\n"); 79 return -EBUSY; 80 } 81 } 82 if (dma1[n] == SNDRV_AUTO_DMA) { 83 dma1[n] = snd_legacy_find_free_dma(possible_dmas); 84 if (dma1[n] < 0) { 85 dev_err(dev, "unable to find a free DMA1\n"); 86 return -EBUSY; 87 } 88 } 89 if (dma2[n] == SNDRV_AUTO_DMA) { 90 dma2[n] = snd_legacy_find_free_dma(possible_dmas); 91 if (dma2[n] < 0) { 92 dev_err(dev, "unable to find a free DMA2\n"); 93 return -EBUSY; 94 } 95 } 96 97 if (port[n] != SNDRV_AUTO_PORT) 98 return snd_gus_create(card, port[n], irq[n], dma1[n], dma2[n], 99 0, channels[n], pcm_channels[n], 0, rgus); 100 101 i = 0; 102 do { 103 port[n] = possible_ports[i]; 104 error = snd_gus_create(card, port[n], irq[n], dma1[n], dma2[n], 105 0, channels[n], pcm_channels[n], 0, rgus); 106 } while (error < 0 && ++i < ARRAY_SIZE(possible_ports)); 107 108 return error; 109 } 110 111 static int snd_gusclassic_detect(struct snd_gus_card *gus) 112 { 113 unsigned char d; 114 115 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ 116 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) { 117 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); 118 return -ENODEV; 119 } 120 udelay(160); 121 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ 122 udelay(160); 123 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) { 124 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); 125 return -ENODEV; 126 } 127 return 0; 128 } 129 130 static int snd_gusclassic_probe(struct device *dev, unsigned int n) 131 { 132 struct snd_card *card; 133 struct snd_gus_card *gus; 134 int error; 135 136 error = snd_card_new(dev, index[n], id[n], THIS_MODULE, 0, &card); 137 if (error < 0) 138 return error; 139 140 if (pcm_channels[n] < 2) 141 pcm_channels[n] = 2; 142 143 error = snd_gusclassic_create(card, dev, n, &gus); 144 if (error < 0) 145 goto out; 146 147 error = snd_gusclassic_detect(gus); 148 if (error < 0) 149 goto out; 150 151 gus->joystick_dac = joystick_dac[n]; 152 153 error = snd_gus_initialize(gus); 154 if (error < 0) 155 goto out; 156 157 error = -ENODEV; 158 if (gus->max_flag || gus->ess_flag) { 159 dev_err(dev, "GUS Classic or ACE soundcard was " 160 "not detected at 0x%lx\n", gus->gf1.port); 161 goto out; 162 } 163 164 error = snd_gf1_new_mixer(gus); 165 if (error < 0) 166 goto out; 167 168 error = snd_gf1_pcm_new(gus, 0, 0); 169 if (error < 0) 170 goto out; 171 172 if (!gus->ace_flag) { 173 error = snd_gf1_rawmidi_new(gus, 0); 174 if (error < 0) 175 goto out; 176 } 177 178 sprintf(card->longname + strlen(card->longname), 179 " at 0x%lx, irq %d, dma %d", 180 gus->gf1.port, gus->gf1.irq, gus->gf1.dma1); 181 182 if (gus->gf1.dma2 >= 0) 183 sprintf(card->longname + strlen(card->longname), 184 "&%d", gus->gf1.dma2); 185 186 error = snd_card_register(card); 187 if (error < 0) 188 goto out; 189 190 dev_set_drvdata(dev, card); 191 return 0; 192 193 out: snd_card_free(card); 194 return error; 195 } 196 197 static void snd_gusclassic_remove(struct device *dev, unsigned int n) 198 { 199 snd_card_free(dev_get_drvdata(dev)); 200 } 201 202 static struct isa_driver snd_gusclassic_driver = { 203 .match = snd_gusclassic_match, 204 .probe = snd_gusclassic_probe, 205 .remove = snd_gusclassic_remove, 206 #if 0 /* FIXME */ 207 .suspend = snd_gusclassic_suspend, 208 .remove = snd_gusclassic_remove, 209 #endif 210 .driver = { 211 .name = DEV_NAME 212 } 213 }; 214 215 module_isa_driver(snd_gusclassic_driver, SNDRV_CARDS); 216