| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Driver for Gravis UltraSound Extreme soundcards |
| 4 |
* Copyright (c) by Jaroslav Kysela <[email protected]> |
| 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/es1688.h> |
| 17 |
#include <sound/mpu401.h> |
| 18 |
#include <sound/opl3.h> |
| 19 |
#define SNDRV_LEGACY_AUTO_PROBE |
| 20 |
#define SNDRV_LEGACY_FIND_FREE_IRQ |
| 21 |
#define SNDRV_LEGACY_FIND_FREE_DMA |
| 22 |
#include <sound/initval.h> |
| 23 |
|
| 24 |
#define CRD_NAME "Gravis UltraSound Extreme" |
| 25 |
#define DEV_NAME "gusextreme" |
| 26 |
|
| 27 |
MODULE_DESCRIPTION(CRD_NAME); |
| 28 |
MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); |
| 29 |
MODULE_LICENSE("GPL"); |
| 30 |
|
| 31 |
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 32 |
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
| 33 |
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ |
| 34 |
static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */ |
| 35 |
static long gf1_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x210,0x220,0x230,0x240,0x250,0x260,0x270 */ |
| 36 |
static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x300,0x310,0x320 */ |
| 37 |
static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ |
| 38 |
static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ |
| 39 |
static int gf1_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */ |
| 40 |
static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */ |
| 41 |
static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; |
| 42 |
static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; |
| 43 |
/* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ |
| 44 |
static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24}; |
| 45 |
static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; |
| 46 |
|
| 47 |
module_param_array(index, int, NULL, 0444); |
| 48 |
MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); |
| 49 |
module_param_array(id, charp, NULL, 0444); |
| 50 |
MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); |
| 51 |
module_param_array(enable, bool, NULL, 0444); |
| 52 |
MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); |
| 53 |
module_param_hw_array(port, long, ioport, NULL, 0444); |
| 54 |
MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); |
| 55 |
module_param_hw_array(gf1_port, long, ioport, NULL, 0444); |
| 56 |
MODULE_PARM_DESC(gf1_port, "GF1 port # for " CRD_NAME " driver (optional)."); |
| 57 |
module_param_hw_array(mpu_port, long, ioport, NULL, 0444); |
| 58 |
MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver."); |
| 59 |
module_param_hw_array(irq, int, irq, NULL, 0444); |
| 60 |
MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver."); |
| 61 |
module_param_hw_array(mpu_irq, int, irq, NULL, 0444); |
| 62 |
MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver."); |
| 63 |
module_param_hw_array(gf1_irq, int, irq, NULL, 0444); |
| 64 |
MODULE_PARM_DESC(gf1_irq, "GF1 IRQ # for " CRD_NAME " driver."); |
| 65 |
module_param_hw_array(dma8, int, dma, NULL, 0444); |
| 66 |
MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver."); |
| 67 |
module_param_hw_array(dma1, int, dma, NULL, 0444); |
| 68 |
MODULE_PARM_DESC(dma1, "GF1 DMA # for " CRD_NAME " driver."); |
| 69 |
module_param_array(joystick_dac, int, NULL, 0444); |
| 70 |
MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for " CRD_NAME " driver."); |
| 71 |
module_param_array(channels, int, NULL, 0444); |
| 72 |
MODULE_PARM_DESC(channels, "GF1 channels for " CRD_NAME " driver."); |
| 73 |
module_param_array(pcm_channels, int, NULL, 0444); |
| 74 |
MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for " CRD_NAME " driver."); |
| 75 |
|
| 76 |
static int snd_gusextreme_match(struct device *dev, unsigned int n) |
| 77 |
{ |
| 78 |
return enable[n]; |
| 79 |
} |
| 80 |
|
| 81 |
static int snd_gusextreme_es1688_create(struct snd_card *card, |
| 82 |
struct snd_es1688 *chip, |
| 83 |
struct device *dev, unsigned int n) |
| 84 |
{ |
| 85 |
static const long possible_ports[] = {0x220, 0x240, 0x260}; |
| 86 |
static const int possible_irqs[] = {5, 9, 10, 7, -1}; |
| 87 |
static const int possible_dmas[] = {1, 3, 0, -1}; |
| 88 |
|
| 89 |
int i, error; |
| 90 |
|
| 91 |
if (irq[n] == SNDRV_AUTO_IRQ) { |
| 92 |
irq[n] = snd_legacy_find_free_irq(possible_irqs); |
| 93 |
if (irq[n] < 0) { |
| 94 |
dev_err(dev, "unable to find a free IRQ for ES1688\n"); |
| 95 |
return -EBUSY; |
| 96 |
} |
| 97 |
} |
| 98 |
if (dma8[n] == SNDRV_AUTO_DMA) { |
| 99 |
dma8[n] = snd_legacy_find_free_dma(possible_dmas); |
| 100 |
if (dma8[n] < 0) { |
| 101 |
dev_err(dev, "unable to find a free DMA for ES1688\n"); |
| 102 |
return -EBUSY; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if (port[n] != SNDRV_AUTO_PORT) |
| 107 |
return snd_es1688_create(card, chip, port[n], mpu_port[n], |
| 108 |
irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688); |
| 109 |
|
| 110 |
i = 0; |
| 111 |
do { |
| 112 |
port[n] = possible_ports[i]; |
| 113 |
error = snd_es1688_create(card, chip, port[n], mpu_port[n], |
| 114 |
irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688); |
| 115 |
} while (error < 0 && ++i < ARRAY_SIZE(possible_ports)); |
| 116 |
|
| 117 |
return error; |
| 118 |
} |
| 119 |
|
| 120 |
static int snd_gusextreme_gus_card_create(struct snd_card *card, |
| 121 |
struct device *dev, unsigned int n, |
| 122 |
struct snd_gus_card **rgus) |
| 123 |
{ |
| 124 |
static const int possible_irqs[] = {11, 12, 15, 9, 5, 7, 3, -1}; |
| 125 |
static const int possible_dmas[] = {5, 6, 7, 3, 1, -1}; |
| 126 |
|
| 127 |
if (gf1_irq[n] == SNDRV_AUTO_IRQ) { |
| 128 |
gf1_irq[n] = snd_legacy_find_free_irq(possible_irqs); |
| 129 |
if (gf1_irq[n] < 0) { |
| 130 |
dev_err(dev, "unable to find a free IRQ for GF1\n"); |
| 131 |
return -EBUSY; |
| 132 |
} |
| 133 |
} |
| 134 |
if (dma1[n] == SNDRV_AUTO_DMA) { |
| 135 |
dma1[n] = snd_legacy_find_free_dma(possible_dmas); |
| 136 |
if (dma1[n] < 0) { |
| 137 |
dev_err(dev, "unable to find a free DMA for GF1\n"); |
| 138 |
return -EBUSY; |
| 139 |
} |
| 140 |
} |
| 141 |
return snd_gus_create(card, gf1_port[n], gf1_irq[n], dma1[n], -1, |
| 142 |
0, channels[n], pcm_channels[n], 0, rgus); |
| 143 |
} |
| 144 |
|
| 145 |
static int snd_gusextreme_detect(struct snd_gus_card *gus, |
| 146 |
struct snd_es1688 *es1688) |
| 147 |
{ |
| 148 |
unsigned char d; |
| 149 |
|
| 150 |
/* |
| 151 |
* This is main stuff - enable access to GF1 chip... |
| 152 |
* I'm not sure, if this will work for card which have |
| 153 |
* ES1688 chip in another place than 0x220. |
| 154 |
* |
| 155 |
* I used reverse-engineering in DOSEMU. [--jk] |
| 156 |
* |
| 157 |
* ULTRINIT.EXE: |
| 158 |
* 0x230 = 0,2,3 |
| 159 |
* 0x240 = 2,0,1 |
| 160 |
* 0x250 = 2,0,3 |
| 161 |
* 0x260 = 2,2,1 |
| 162 |
*/ |
| 163 |
|
| 164 |
scoped_guard(spinlock_irqsave, &es1688->mixer_lock) { |
| 165 |
snd_es1688_mixer_write(es1688, 0x40, 0x0b); /* don't change!!! */ |
| 166 |
} |
| 167 |
|
| 168 |
scoped_guard(spinlock_irqsave, &es1688->reg_lock) { |
| 169 |
outb(gus->gf1.port & 0x040 ? 2 : 0, ES1688P(es1688, INIT1)); |
| 170 |
outb(0, 0x201); |
| 171 |
outb(gus->gf1.port & 0x020 ? 2 : 0, ES1688P(es1688, INIT1)); |
| 172 |
outb(0, 0x201); |
| 173 |
outb(gus->gf1.port & 0x010 ? 3 : 1, ES1688P(es1688, INIT1)); |
| 174 |
} |
| 175 |
|
| 176 |
udelay(100); |
| 177 |
|
| 178 |
snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ |
| 179 |
d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); |
| 180 |
if ((d & 0x07) != 0) { |
| 181 |
dev_dbg(gus->card->dev, "[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); |
| 182 |
return -EIO; |
| 183 |
} |
| 184 |
udelay(160); |
| 185 |
snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ |
| 186 |
udelay(160); |
| 187 |
d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); |
| 188 |
if ((d & 0x07) != 1) { |
| 189 |
dev_dbg(gus->card->dev, "[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); |
| 190 |
return -EIO; |
| 191 |
} |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
static int snd_gusextreme_mixer(struct snd_card *card) |
| 197 |
{ |
| 198 |
struct snd_ctl_elem_id id1, id2; |
| 199 |
int error; |
| 200 |
|
| 201 |
memset(&id1, 0, sizeof(id1)); |
| 202 |
memset(&id2, 0, sizeof(id2)); |
| 203 |
id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 204 |
|
| 205 |
/* reassign AUX to SYNTHESIZER */ |
| 206 |
strscpy(id1.name, "Aux Playback Volume"); |
| 207 |
strscpy(id2.name, "Synth Playback Volume"); |
| 208 |
error = snd_ctl_rename_id(card, &id1, &id2); |
| 209 |
if (error < 0) |
| 210 |
return error; |
| 211 |
|
| 212 |
/* reassign Master Playback Switch to Synth Playback Switch */ |
| 213 |
strscpy(id1.name, "Master Playback Switch"); |
| 214 |
strscpy(id2.name, "Synth Playback Switch"); |
| 215 |
error = snd_ctl_rename_id(card, &id1, &id2); |
| 216 |
if (error < 0) |
| 217 |
return error; |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static int snd_gusextreme_probe(struct device *dev, unsigned int n) |
| 223 |
{ |
| 224 |
struct snd_card *card; |
| 225 |
struct snd_gus_card *gus; |
| 226 |
struct snd_es1688 *es1688; |
| 227 |
struct snd_opl3 *opl3; |
| 228 |
int error; |
| 229 |
|
| 230 |
error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE, |
| 231 |
sizeof(struct snd_es1688), &card); |
| 232 |
if (error < 0) |
| 233 |
return error; |
| 234 |
|
| 235 |
es1688 = card->private_data; |
| 236 |
|
| 237 |
if (mpu_port[n] == SNDRV_AUTO_PORT) |
| 238 |
mpu_port[n] = 0; |
| 239 |
|
| 240 |
if (mpu_irq[n] > 15) |
| 241 |
mpu_irq[n] = -1; |
| 242 |
|
| 243 |
error = snd_gusextreme_es1688_create(card, es1688, dev, n); |
| 244 |
if (error < 0) |
| 245 |
return error; |
| 246 |
|
| 247 |
if (gf1_port[n] < 0) |
| 248 |
gf1_port[n] = es1688->port + 0x20; |
| 249 |
|
| 250 |
error = snd_gusextreme_gus_card_create(card, dev, n, &gus); |
| 251 |
if (error < 0) |
| 252 |
return error; |
| 253 |
|
| 254 |
error = snd_gusextreme_detect(gus, es1688); |
| 255 |
if (error < 0) |
| 256 |
return error; |
| 257 |
|
| 258 |
gus->joystick_dac = joystick_dac[n]; |
| 259 |
|
| 260 |
error = snd_gus_initialize(gus); |
| 261 |
if (error < 0) |
| 262 |
return error; |
| 263 |
|
| 264 |
error = -ENODEV; |
| 265 |
if (!gus->ess_flag) { |
| 266 |
dev_err(dev, "GUS Extreme soundcard was not " |
| 267 |
"detected at 0x%lx\n", gus->gf1.port); |
| 268 |
return error; |
| 269 |
} |
| 270 |
gus->codec_flag = 1; |
| 271 |
|
| 272 |
error = snd_es1688_pcm(card, es1688, 0); |
| 273 |
if (error < 0) |
| 274 |
return error; |
| 275 |
|
| 276 |
error = snd_es1688_mixer(card, es1688); |
| 277 |
if (error < 0) |
| 278 |
return error; |
| 279 |
|
| 280 |
snd_component_add(card, "ES1688"); |
| 281 |
|
| 282 |
if (pcm_channels[n] > 0) { |
| 283 |
error = snd_gf1_pcm_new(gus, 1, 1); |
| 284 |
if (error < 0) |
| 285 |
return error; |
| 286 |
} |
| 287 |
|
| 288 |
error = snd_gf1_new_mixer(gus); |
| 289 |
if (error < 0) |
| 290 |
return error; |
| 291 |
|
| 292 |
error = snd_gusextreme_mixer(card); |
| 293 |
if (error < 0) |
| 294 |
return error; |
| 295 |
|
| 296 |
if (snd_opl3_create(card, es1688->port, es1688->port + 2, |
| 297 |
OPL3_HW_OPL3, 0, &opl3) < 0) |
| 298 |
dev_warn(dev, "opl3 not detected at 0x%lx\n", es1688->port); |
| 299 |
else { |
| 300 |
error = snd_opl3_hwdep_new(opl3, 0, 2, NULL); |
| 301 |
if (error < 0) |
| 302 |
return error; |
| 303 |
} |
| 304 |
|
| 305 |
if (es1688->mpu_port >= 0x300) { |
| 306 |
error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688, |
| 307 |
es1688->mpu_port, 0, mpu_irq[n], NULL); |
| 308 |
if (error < 0) |
| 309 |
return error; |
| 310 |
} |
| 311 |
|
| 312 |
sprintf(card->longname, "Gravis UltraSound Extreme at 0x%lx, " |
| 313 |
"irq %i&%i, dma %i&%i", es1688->port, |
| 314 |
gus->gf1.irq, es1688->irq, gus->gf1.dma1, es1688->dma8); |
| 315 |
|
| 316 |
error = snd_card_register(card); |
| 317 |
if (error < 0) |
| 318 |
return error; |
| 319 |
|
| 320 |
dev_set_drvdata(dev, card); |
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
static struct isa_driver snd_gusextreme_driver = { |
| 325 |
.match = snd_gusextreme_match, |
| 326 |
.probe = snd_gusextreme_probe, |
| 327 |
#if 0 /* FIXME */ |
| 328 |
.suspend = snd_gusextreme_suspend, |
| 329 |
.resume = snd_gusextreme_resume, |
| 330 |
#endif |
| 331 |
.driver = { |
| 332 |
.name = DEV_NAME |
| 333 |
} |
| 334 |
}; |
| 335 |
|
| 336 |
module_isa_driver(snd_gusextreme_driver, SNDRV_CARDS); |