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