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