1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Routines for Gravis UltraSound soundcards 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 */ 6 7 #include <linux/init.h> 8 #include <linux/interrupt.h> 9 #include <linux/delay.h> 10 #include <linux/slab.h> 11 #include <linux/ioport.h> 12 #include <linux/module.h> 13 #include <sound/core.h> 14 #include <sound/gus.h> 15 #include <sound/control.h> 16 17 #include <asm/dma.h> 18 19 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 20 MODULE_DESCRIPTION("Routines for Gravis UltraSound soundcards"); 21 MODULE_LICENSE("GPL"); 22 23 static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches); 24 25 int snd_gus_use_inc(struct snd_gus_card * gus) 26 { 27 if (!try_module_get(gus->card->module)) 28 return 0; 29 return 1; 30 } 31 32 void snd_gus_use_dec(struct snd_gus_card * gus) 33 { 34 module_put(gus->card->module); 35 } 36 37 static int snd_gus_joystick_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 38 { 39 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 40 uinfo->count = 1; 41 uinfo->value.integer.min = 0; 42 uinfo->value.integer.max = 31; 43 return 0; 44 } 45 46 static int snd_gus_joystick_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 47 { 48 struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); 49 50 ucontrol->value.integer.value[0] = gus->joystick_dac & 31; 51 return 0; 52 } 53 54 static int snd_gus_joystick_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 55 { 56 struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); 57 unsigned long flags; 58 int change; 59 unsigned char nval; 60 61 nval = ucontrol->value.integer.value[0] & 31; 62 spin_lock_irqsave(&gus->reg_lock, flags); 63 change = gus->joystick_dac != nval; 64 gus->joystick_dac = nval; 65 snd_gf1_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac); 66 spin_unlock_irqrestore(&gus->reg_lock, flags); 67 return change; 68 } 69 70 static const struct snd_kcontrol_new snd_gus_joystick_control = { 71 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 72 .name = "Joystick Speed", 73 .info = snd_gus_joystick_info, 74 .get = snd_gus_joystick_get, 75 .put = snd_gus_joystick_put 76 }; 77 78 static void snd_gus_init_control(struct snd_gus_card *gus) 79 { 80 if (!gus->ace_flag) 81 snd_ctl_add(gus->card, snd_ctl_new1(&snd_gus_joystick_control, gus)); 82 } 83 84 /* 85 * 86 */ 87 88 static int snd_gus_free(struct snd_gus_card *gus) 89 { 90 if (gus->gf1.res_port2 == NULL) 91 goto __hw_end; 92 snd_gf1_stop(gus); 93 snd_gus_init_dma_irq(gus, 0); 94 __hw_end: 95 release_and_free_resource(gus->gf1.res_port1); 96 release_and_free_resource(gus->gf1.res_port2); 97 if (gus->gf1.irq >= 0) 98 free_irq(gus->gf1.irq, (void *) gus); 99 if (gus->gf1.dma1 >= 0) { 100 disable_dma(gus->gf1.dma1); 101 free_dma(gus->gf1.dma1); 102 } 103 if (!gus->equal_dma && gus->gf1.dma2 >= 0) { 104 disable_dma(gus->gf1.dma2); 105 free_dma(gus->gf1.dma2); 106 } 107 kfree(gus); 108 return 0; 109 } 110 111 static int snd_gus_dev_free(struct snd_device *device) 112 { 113 struct snd_gus_card *gus = device->device_data; 114 return snd_gus_free(gus); 115 } 116 117 int snd_gus_create(struct snd_card *card, 118 unsigned long port, 119 int irq, int dma1, int dma2, 120 int timer_dev, 121 int voices, 122 int pcm_channels, 123 int effect, 124 struct snd_gus_card **rgus) 125 { 126 struct snd_gus_card *gus; 127 int err; 128 static const struct snd_device_ops ops = { 129 .dev_free = snd_gus_dev_free, 130 }; 131 132 *rgus = NULL; 133 gus = kzalloc(sizeof(*gus), GFP_KERNEL); 134 if (gus == NULL) 135 return -ENOMEM; 136 spin_lock_init(&gus->reg_lock); 137 spin_lock_init(&gus->voice_alloc); 138 spin_lock_init(&gus->active_voice_lock); 139 spin_lock_init(&gus->event_lock); 140 spin_lock_init(&gus->dma_lock); 141 spin_lock_init(&gus->pcm_volume_level_lock); 142 spin_lock_init(&gus->uart_cmd_lock); 143 mutex_init(&gus->dma_mutex); 144 gus->gf1.irq = -1; 145 gus->gf1.dma1 = -1; 146 gus->gf1.dma2 = -1; 147 gus->card = card; 148 gus->gf1.port = port; 149 /* fill register variables for speedup */ 150 gus->gf1.reg_page = GUSP(gus, GF1PAGE); 151 gus->gf1.reg_regsel = GUSP(gus, GF1REGSEL); 152 gus->gf1.reg_data8 = GUSP(gus, GF1DATAHIGH); 153 gus->gf1.reg_data16 = GUSP(gus, GF1DATALOW); 154 gus->gf1.reg_irqstat = GUSP(gus, IRQSTAT); 155 gus->gf1.reg_dram = GUSP(gus, DRAM); 156 gus->gf1.reg_timerctrl = GUSP(gus, TIMERCNTRL); 157 gus->gf1.reg_timerdata = GUSP(gus, TIMERDATA); 158 /* allocate resources */ 159 if ((gus->gf1.res_port1 = request_region(port, 16, "GUS GF1 (Adlib/SB)")) == NULL) { 160 snd_printk(KERN_ERR "gus: can't grab SB port 0x%lx\n", port); 161 snd_gus_free(gus); 162 return -EBUSY; 163 } 164 if ((gus->gf1.res_port2 = request_region(port + 0x100, 12, "GUS GF1 (Synth)")) == NULL) { 165 snd_printk(KERN_ERR "gus: can't grab synth port 0x%lx\n", port + 0x100); 166 snd_gus_free(gus); 167 return -EBUSY; 168 } 169 if (irq >= 0 && request_irq(irq, snd_gus_interrupt, 0, "GUS GF1", (void *) gus)) { 170 snd_printk(KERN_ERR "gus: can't grab irq %d\n", irq); 171 snd_gus_free(gus); 172 return -EBUSY; 173 } 174 gus->gf1.irq = irq; 175 card->sync_irq = irq; 176 if (request_dma(dma1, "GUS - 1")) { 177 snd_printk(KERN_ERR "gus: can't grab DMA1 %d\n", dma1); 178 snd_gus_free(gus); 179 return -EBUSY; 180 } 181 gus->gf1.dma1 = dma1; 182 if (dma2 >= 0 && dma1 != dma2) { 183 if (request_dma(dma2, "GUS - 2")) { 184 snd_printk(KERN_ERR "gus: can't grab DMA2 %d\n", dma2); 185 snd_gus_free(gus); 186 return -EBUSY; 187 } 188 gus->gf1.dma2 = dma2; 189 } else { 190 gus->gf1.dma2 = gus->gf1.dma1; 191 gus->equal_dma = 1; 192 } 193 gus->timer_dev = timer_dev; 194 if (voices < 14) 195 voices = 14; 196 if (voices > 32) 197 voices = 32; 198 if (pcm_channels < 0) 199 pcm_channels = 0; 200 if (pcm_channels > 8) 201 pcm_channels = 8; 202 pcm_channels++; 203 pcm_channels &= ~1; 204 gus->gf1.effect = effect ? 1 : 0; 205 gus->gf1.active_voices = voices; 206 gus->gf1.pcm_channels = pcm_channels; 207 gus->gf1.volume_ramp = 25; 208 gus->gf1.smooth_pan = 1; 209 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, gus, &ops)) < 0) { 210 snd_gus_free(gus); 211 return err; 212 } 213 *rgus = gus; 214 return 0; 215 } 216 217 /* 218 * Memory detection routine for plain GF1 soundcards 219 */ 220 221 static int snd_gus_detect_memory(struct snd_gus_card * gus) 222 { 223 int l, idx, local; 224 unsigned char d; 225 226 snd_gf1_poke(gus, 0L, 0xaa); 227 snd_gf1_poke(gus, 1L, 0x55); 228 if (snd_gf1_peek(gus, 0L) != 0xaa || snd_gf1_peek(gus, 1L) != 0x55) { 229 snd_printk(KERN_ERR "plain GF1 card at 0x%lx without onboard DRAM?\n", gus->gf1.port); 230 return -ENOMEM; 231 } 232 for (idx = 1, d = 0xab; idx < 4; idx++, d++) { 233 local = idx << 18; 234 snd_gf1_poke(gus, local, d); 235 snd_gf1_poke(gus, local + 1, d + 1); 236 if (snd_gf1_peek(gus, local) != d || 237 snd_gf1_peek(gus, local + 1) != d + 1 || 238 snd_gf1_peek(gus, 0L) != 0xaa) 239 break; 240 } 241 #if 1 242 gus->gf1.memory = idx << 18; 243 #else 244 gus->gf1.memory = 256 * 1024; 245 #endif 246 for (l = 0, local = gus->gf1.memory; l < 4; l++, local -= 256 * 1024) { 247 gus->gf1.mem_alloc.banks_8[l].address = 248 gus->gf1.mem_alloc.banks_8[l].size = 0; 249 gus->gf1.mem_alloc.banks_16[l].address = l << 18; 250 gus->gf1.mem_alloc.banks_16[l].size = local > 0 ? 256 * 1024 : 0; 251 } 252 gus->gf1.mem_alloc.banks_8[0].size = gus->gf1.memory; 253 return 0; /* some memory were detected */ 254 } 255 256 static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches) 257 { 258 struct snd_card *card; 259 unsigned long flags; 260 int irq, dma1, dma2; 261 static const unsigned char irqs[16] = 262 {0, 0, 1, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7}; 263 static const unsigned char dmas[8] = 264 {6, 1, 0, 2, 0, 3, 4, 5}; 265 266 if (snd_BUG_ON(!gus)) 267 return -EINVAL; 268 card = gus->card; 269 if (snd_BUG_ON(!card)) 270 return -EINVAL; 271 272 gus->mix_cntrl_reg &= 0xf8; 273 gus->mix_cntrl_reg |= 0x01; /* disable MIC, LINE IN, enable LINE OUT */ 274 if (gus->codec_flag || gus->ess_flag) { 275 gus->mix_cntrl_reg &= ~1; /* enable LINE IN */ 276 gus->mix_cntrl_reg |= 4; /* enable MIC */ 277 } 278 dma1 = gus->gf1.dma1; 279 dma1 = abs(dma1); 280 dma1 = dmas[dma1 & 7]; 281 dma2 = gus->gf1.dma2; 282 dma2 = abs(dma2); 283 dma2 = dmas[dma2 & 7]; 284 dma1 |= gus->equal_dma ? 0x40 : (dma2 << 3); 285 286 if ((dma1 & 7) == 0 || (dma2 & 7) == 0) { 287 snd_printk(KERN_ERR "Error! DMA isn't defined.\n"); 288 return -EINVAL; 289 } 290 irq = gus->gf1.irq; 291 irq = abs(irq); 292 irq = irqs[irq & 0x0f]; 293 if (irq == 0) { 294 snd_printk(KERN_ERR "Error! IRQ isn't defined.\n"); 295 return -EINVAL; 296 } 297 irq |= 0x40; 298 #if 0 299 card->mixer.mix_ctrl_reg |= 0x10; 300 #endif 301 302 spin_lock_irqsave(&gus->reg_lock, flags); 303 outb(5, GUSP(gus, REGCNTRLS)); 304 outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG)); 305 outb(0x00, GUSP(gus, IRQDMACNTRLREG)); 306 outb(0, GUSP(gus, REGCNTRLS)); 307 spin_unlock_irqrestore(&gus->reg_lock, flags); 308 309 udelay(100); 310 311 spin_lock_irqsave(&gus->reg_lock, flags); 312 outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG)); 313 outb(dma1, GUSP(gus, IRQDMACNTRLREG)); 314 if (latches) { 315 outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG)); 316 outb(irq, GUSP(gus, IRQDMACNTRLREG)); 317 } 318 spin_unlock_irqrestore(&gus->reg_lock, flags); 319 320 udelay(100); 321 322 spin_lock_irqsave(&gus->reg_lock, flags); 323 outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG)); 324 outb(dma1, GUSP(gus, IRQDMACNTRLREG)); 325 if (latches) { 326 outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG)); 327 outb(irq, GUSP(gus, IRQDMACNTRLREG)); 328 } 329 spin_unlock_irqrestore(&gus->reg_lock, flags); 330 331 snd_gf1_delay(gus); 332 333 if (latches) 334 gus->mix_cntrl_reg |= 0x08; /* enable latches */ 335 else 336 gus->mix_cntrl_reg &= ~0x08; /* disable latches */ 337 spin_lock_irqsave(&gus->reg_lock, flags); 338 outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG)); 339 outb(0, GUSP(gus, GF1PAGE)); 340 spin_unlock_irqrestore(&gus->reg_lock, flags); 341 342 return 0; 343 } 344 345 static int snd_gus_check_version(struct snd_gus_card * gus) 346 { 347 unsigned long flags; 348 unsigned char val, rev; 349 struct snd_card *card; 350 351 card = gus->card; 352 spin_lock_irqsave(&gus->reg_lock, flags); 353 outb(0x20, GUSP(gus, REGCNTRLS)); 354 val = inb(GUSP(gus, REGCNTRLS)); 355 rev = inb(GUSP(gus, BOARDVERSION)); 356 spin_unlock_irqrestore(&gus->reg_lock, flags); 357 snd_printdd("GF1 [0x%lx] init - val = 0x%x, rev = 0x%x\n", gus->gf1.port, val, rev); 358 strcpy(card->driver, "GUS"); 359 strcpy(card->longname, "Gravis UltraSound Classic (2.4)"); 360 if ((val != 255 && (val & 0x06)) || (rev >= 5 && rev != 255)) { 361 if (rev >= 5 && rev <= 9) { 362 gus->ics_flag = 1; 363 if (rev == 5) 364 gus->ics_flipped = 1; 365 card->longname[27] = '3'; 366 card->longname[29] = rev == 5 ? '5' : '7'; 367 } 368 if (rev >= 10 && rev != 255) { 369 if (rev >= 10 && rev <= 11) { 370 strcpy(card->driver, "GUS MAX"); 371 strcpy(card->longname, "Gravis UltraSound MAX"); 372 gus->max_flag = 1; 373 } else if (rev == 0x30) { 374 strcpy(card->driver, "GUS ACE"); 375 strcpy(card->longname, "Gravis UltraSound Ace"); 376 gus->ace_flag = 1; 377 } else if (rev == 0x50) { 378 strcpy(card->driver, "GUS Extreme"); 379 strcpy(card->longname, "Gravis UltraSound Extreme"); 380 gus->ess_flag = 1; 381 } else { 382 snd_printk(KERN_ERR "unknown GF1 revision number at 0x%lx - 0x%x (0x%x)\n", gus->gf1.port, rev, val); 383 snd_printk(KERN_ERR " please - report to <perex@perex.cz>\n"); 384 } 385 } 386 } 387 strcpy(card->shortname, card->longname); 388 gus->uart_enable = 1; /* standard GUSes doesn't have midi uart trouble */ 389 snd_gus_init_control(gus); 390 return 0; 391 } 392 393 int snd_gus_initialize(struct snd_gus_card *gus) 394 { 395 int err; 396 397 if (!gus->interwave) { 398 if ((err = snd_gus_check_version(gus)) < 0) { 399 snd_printk(KERN_ERR "version check failed\n"); 400 return err; 401 } 402 if ((err = snd_gus_detect_memory(gus)) < 0) 403 return err; 404 } 405 if ((err = snd_gus_init_dma_irq(gus, 1)) < 0) 406 return err; 407 snd_gf1_start(gus); 408 gus->initialized = 1; 409 return 0; 410 } 411 412 /* gus_io.c */ 413 EXPORT_SYMBOL(snd_gf1_delay); 414 EXPORT_SYMBOL(snd_gf1_write8); 415 EXPORT_SYMBOL(snd_gf1_look8); 416 EXPORT_SYMBOL(snd_gf1_write16); 417 EXPORT_SYMBOL(snd_gf1_look16); 418 EXPORT_SYMBOL(snd_gf1_i_write8); 419 EXPORT_SYMBOL(snd_gf1_i_look8); 420 EXPORT_SYMBOL(snd_gf1_i_look16); 421 EXPORT_SYMBOL(snd_gf1_dram_addr); 422 EXPORT_SYMBOL(snd_gf1_write_addr); 423 EXPORT_SYMBOL(snd_gf1_poke); 424 EXPORT_SYMBOL(snd_gf1_peek); 425 /* gus_reset.c */ 426 EXPORT_SYMBOL(snd_gf1_alloc_voice); 427 EXPORT_SYMBOL(snd_gf1_free_voice); 428 EXPORT_SYMBOL(snd_gf1_ctrl_stop); 429 EXPORT_SYMBOL(snd_gf1_stop_voice); 430 /* gus_mixer.c */ 431 EXPORT_SYMBOL(snd_gf1_new_mixer); 432 /* gus_pcm.c */ 433 EXPORT_SYMBOL(snd_gf1_pcm_new); 434 /* gus.c */ 435 EXPORT_SYMBOL(snd_gus_use_inc); 436 EXPORT_SYMBOL(snd_gus_use_dec); 437 EXPORT_SYMBOL(snd_gus_create); 438 EXPORT_SYMBOL(snd_gus_initialize); 439 /* gus_irq.c */ 440 EXPORT_SYMBOL(snd_gus_interrupt); 441 /* gus_uart.c */ 442 EXPORT_SYMBOL(snd_gf1_rawmidi_new); 443 /* gus_dram.c */ 444 EXPORT_SYMBOL(snd_gus_dram_write); 445 EXPORT_SYMBOL(snd_gus_dram_read); 446 /* gus_volume.c */ 447 EXPORT_SYMBOL(snd_gf1_lvol_to_gvol_raw); 448 EXPORT_SYMBOL(snd_gf1_translate_freq); 449 /* gus_mem.c */ 450 EXPORT_SYMBOL(snd_gf1_mem_alloc); 451 EXPORT_SYMBOL(snd_gf1_mem_xfree); 452 EXPORT_SYMBOL(snd_gf1_mem_free); 453 EXPORT_SYMBOL(snd_gf1_mem_lock); 454