1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Gravis UltraSound MAX 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 #include <sound/wss.h> 17 #define SNDRV_LEGACY_FIND_FREE_IRQ 18 #define SNDRV_LEGACY_FIND_FREE_DMA 19 #include <sound/initval.h> 20 21 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 22 MODULE_DESCRIPTION("Gravis UltraSound MAX"); 23 MODULE_LICENSE("GPL"); 24 25 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 26 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 27 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ 28 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */ 29 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */ 30 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ 31 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ 32 static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; 33 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ 34 static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24}; 35 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; 36 37 module_param_array(index, int, NULL, 0444); 38 MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard."); 39 module_param_array(id, charp, NULL, 0444); 40 MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard."); 41 module_param_array(enable, bool, NULL, 0444); 42 MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard."); 43 module_param_hw_array(port, long, ioport, NULL, 0444); 44 MODULE_PARM_DESC(port, "Port # for GUS MAX driver."); 45 module_param_hw_array(irq, int, irq, NULL, 0444); 46 MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver."); 47 module_param_hw_array(dma1, int, dma, NULL, 0444); 48 MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver."); 49 module_param_hw_array(dma2, int, dma, NULL, 0444); 50 MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver."); 51 module_param_array(joystick_dac, int, NULL, 0444); 52 MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver."); 53 module_param_array(channels, int, NULL, 0444); 54 MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver."); 55 module_param_array(pcm_channels, int, NULL, 0444); 56 MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver."); 57 58 struct snd_gusmax { 59 int irq; 60 struct snd_card *card; 61 struct snd_gus_card *gus; 62 struct snd_wss *wss; 63 unsigned short gus_status_reg; 64 unsigned short pcm_status_reg; 65 }; 66 67 #define PFX "gusmax: " 68 69 static int snd_gusmax_detect(struct snd_gus_card *gus) 70 { 71 unsigned char d; 72 73 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ 74 d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); 75 if ((d & 0x07) != 0) { 76 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); 77 return -ENODEV; 78 } 79 udelay(160); 80 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ 81 udelay(160); 82 d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); 83 if ((d & 0x07) != 1) { 84 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); 85 return -ENODEV; 86 } 87 88 return 0; 89 } 90 91 static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id) 92 { 93 struct snd_gusmax *maxcard = dev_id; 94 int loop, max = 5; 95 int handled = 0; 96 97 do { 98 loop = 0; 99 if (inb(maxcard->gus_status_reg)) { 100 handled = 1; 101 snd_gus_interrupt(irq, maxcard->gus); 102 loop++; 103 } 104 if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */ 105 handled = 1; 106 snd_wss_interrupt(irq, maxcard->wss); 107 loop++; 108 } 109 } while (loop && --max > 0); 110 return IRQ_RETVAL(handled); 111 } 112 113 static void snd_gusmax_init(int dev, struct snd_card *card, 114 struct snd_gus_card *gus) 115 { 116 gus->equal_irq = 1; 117 gus->codec_flag = 1; 118 gus->joystick_dac = joystick_dac[dev]; 119 /* init control register */ 120 gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f; 121 if (gus->gf1.dma1 > 3) 122 gus->max_cntrl_val |= 0x10; 123 if (gus->gf1.dma2 > 3) 124 gus->max_cntrl_val |= 0x20; 125 gus->max_cntrl_val |= 0x40; 126 outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT)); 127 } 128 129 static int snd_gusmax_mixer(struct snd_wss *chip) 130 { 131 struct snd_card *card = chip->card; 132 struct snd_ctl_elem_id id1, id2; 133 int err; 134 135 memset(&id1, 0, sizeof(id1)); 136 memset(&id2, 0, sizeof(id2)); 137 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 138 /* reassign AUXA to SYNTHESIZER */ 139 strcpy(id1.name, "Aux Playback Switch"); 140 strcpy(id2.name, "Synth Playback Switch"); 141 err = snd_ctl_rename_id(card, &id1, &id2); 142 if (err < 0) 143 return err; 144 strcpy(id1.name, "Aux Playback Volume"); 145 strcpy(id2.name, "Synth Playback Volume"); 146 err = snd_ctl_rename_id(card, &id1, &id2); 147 if (err < 0) 148 return err; 149 /* reassign AUXB to CD */ 150 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; 151 strcpy(id2.name, "CD Playback Switch"); 152 err = snd_ctl_rename_id(card, &id1, &id2); 153 if (err < 0) 154 return err; 155 strcpy(id1.name, "Aux Playback Volume"); 156 strcpy(id2.name, "CD Playback Volume"); 157 err = snd_ctl_rename_id(card, &id1, &id2); 158 if (err < 0) 159 return err; 160 #if 0 161 /* reassign Mono Input to MIC */ 162 if (snd_mixer_group_rename(mixer, 163 SNDRV_MIXER_IN_MONO, 0, 164 SNDRV_MIXER_IN_MIC, 0) < 0) 165 goto __error; 166 if (snd_mixer_elem_rename(mixer, 167 SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT, 168 SNDRV_MIXER_IN_MIC, 0) < 0) 169 goto __error; 170 if (snd_mixer_elem_rename(mixer, 171 "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1, 172 "Mic Capture Volume", 0) < 0) 173 goto __error; 174 if (snd_mixer_elem_rename(mixer, 175 "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1, 176 "Mic Capture Switch", 0) < 0) 177 goto __error; 178 #endif 179 return 0; 180 } 181 182 static void snd_gusmax_free(struct snd_card *card) 183 { 184 struct snd_gusmax *maxcard = card->private_data; 185 186 if (maxcard == NULL) 187 return; 188 if (maxcard->irq >= 0) 189 free_irq(maxcard->irq, (void *)maxcard); 190 } 191 192 static int snd_gusmax_match(struct device *pdev, unsigned int dev) 193 { 194 return enable[dev]; 195 } 196 197 static int snd_gusmax_probe(struct device *pdev, unsigned int dev) 198 { 199 static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1}; 200 static const int possible_dmas[] = {5, 6, 7, 1, 3, -1}; 201 int xirq, xdma1, xdma2, err; 202 struct snd_card *card; 203 struct snd_gus_card *gus = NULL; 204 struct snd_wss *wss; 205 struct snd_gusmax *maxcard; 206 207 err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE, 208 sizeof(struct snd_gusmax), &card); 209 if (err < 0) 210 return err; 211 card->private_free = snd_gusmax_free; 212 maxcard = card->private_data; 213 maxcard->card = card; 214 maxcard->irq = -1; 215 216 xirq = irq[dev]; 217 if (xirq == SNDRV_AUTO_IRQ) { 218 xirq = snd_legacy_find_free_irq(possible_irqs); 219 if (xirq < 0) { 220 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n"); 221 err = -EBUSY; 222 goto _err; 223 } 224 } 225 xdma1 = dma1[dev]; 226 if (xdma1 == SNDRV_AUTO_DMA) { 227 xdma1 = snd_legacy_find_free_dma(possible_dmas); 228 if (xdma1 < 0) { 229 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n"); 230 err = -EBUSY; 231 goto _err; 232 } 233 } 234 xdma2 = dma2[dev]; 235 if (xdma2 == SNDRV_AUTO_DMA) { 236 xdma2 = snd_legacy_find_free_dma(possible_dmas); 237 if (xdma2 < 0) { 238 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n"); 239 err = -EBUSY; 240 goto _err; 241 } 242 } 243 244 if (port[dev] != SNDRV_AUTO_PORT) { 245 err = snd_gus_create(card, 246 port[dev], 247 -xirq, xdma1, xdma2, 248 0, channels[dev], 249 pcm_channels[dev], 250 0, &gus); 251 } else { 252 static const unsigned long possible_ports[] = { 253 0x220, 0x230, 0x240, 0x250, 0x260 254 }; 255 int i; 256 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { 257 err = snd_gus_create(card, 258 possible_ports[i], 259 -xirq, xdma1, xdma2, 260 0, channels[dev], 261 pcm_channels[dev], 262 0, &gus); 263 if (err >= 0) { 264 port[dev] = possible_ports[i]; 265 break; 266 } 267 } 268 } 269 if (err < 0) 270 goto _err; 271 272 err = snd_gusmax_detect(gus); 273 if (err < 0) 274 goto _err; 275 276 maxcard->gus_status_reg = gus->gf1.reg_irqstat; 277 maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2; 278 snd_gusmax_init(dev, card, gus); 279 err = snd_gus_initialize(gus); 280 if (err < 0) 281 goto _err; 282 283 if (!gus->max_flag) { 284 snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port); 285 err = -ENODEV; 286 goto _err; 287 } 288 289 if (request_irq(xirq, snd_gusmax_interrupt, 0, "GUS MAX", (void *)maxcard)) { 290 snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq); 291 err = -EBUSY; 292 goto _err; 293 } 294 maxcard->irq = xirq; 295 card->sync_irq = maxcard->irq; 296 297 err = snd_wss_create(card, 298 gus->gf1.port + 0x10c, -1, xirq, 299 xdma2 < 0 ? xdma1 : xdma2, xdma1, 300 WSS_HW_DETECT, 301 WSS_HWSHARE_IRQ | 302 WSS_HWSHARE_DMA1 | 303 WSS_HWSHARE_DMA2, 304 &wss); 305 if (err < 0) 306 goto _err; 307 308 err = snd_wss_pcm(wss, 0); 309 if (err < 0) 310 goto _err; 311 312 err = snd_wss_mixer(wss); 313 if (err < 0) 314 goto _err; 315 316 err = snd_wss_timer(wss, 2); 317 if (err < 0) 318 goto _err; 319 320 if (pcm_channels[dev] > 0) { 321 err = snd_gf1_pcm_new(gus, 1, 1); 322 if (err < 0) 323 goto _err; 324 } 325 err = snd_gusmax_mixer(wss); 326 if (err < 0) 327 goto _err; 328 329 err = snd_gf1_rawmidi_new(gus, 0); 330 if (err < 0) 331 goto _err; 332 333 sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1); 334 if (xdma2 >= 0) 335 sprintf(card->longname + strlen(card->longname), "&%i", xdma2); 336 337 err = snd_card_register(card); 338 if (err < 0) 339 goto _err; 340 341 maxcard->gus = gus; 342 maxcard->wss = wss; 343 344 dev_set_drvdata(pdev, card); 345 return 0; 346 347 _err: 348 snd_card_free(card); 349 return err; 350 } 351 352 static void snd_gusmax_remove(struct device *devptr, unsigned int dev) 353 { 354 snd_card_free(dev_get_drvdata(devptr)); 355 } 356 357 #define DEV_NAME "gusmax" 358 359 static struct isa_driver snd_gusmax_driver = { 360 .match = snd_gusmax_match, 361 .probe = snd_gusmax_probe, 362 .remove = snd_gusmax_remove, 363 /* FIXME: suspend/resume */ 364 .driver = { 365 .name = DEV_NAME 366 }, 367 }; 368 369 module_isa_driver(snd_gusmax_driver, SNDRV_CARDS); 370