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
23MODULE_DESCRIPTION(CRD_NAME);
24MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
25MODULE_LICENSE("GPL");
26MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound Classic}}");
27
28static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
29static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
30static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;	/* Enable this card */
31static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x220,0x230,0x240,0x250,0x260 */
32static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 3,5,9,11,12,15 */
33static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 1,3,5,6,7 */
34static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 1,3,5,6,7 */
35static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
36				/* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
37static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
38static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
39
40module_param_array(index, int, NULL, 0444);
41MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
42module_param_array(id, charp, NULL, 0444);
43MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
44module_param_array(enable, bool, NULL, 0444);
45MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
46module_param_hw_array(port, long, ioport, NULL, 0444);
47MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
48module_param_hw_array(irq, int, irq, NULL, 0444);
49MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
50module_param_hw_array(dma1, int, dma, NULL, 0444);
51MODULE_PARM_DESC(dma1, "DMA1 # for " CRD_NAME " driver.");
52module_param_hw_array(dma2, int, dma, NULL, 0444);
53MODULE_PARM_DESC(dma2, "DMA2 # for " CRD_NAME " driver.");
54module_param_array(joystick_dac, int, NULL, 0444);
55MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for " CRD_NAME " driver.");
56module_param_array(channels, int, NULL, 0444);
57MODULE_PARM_DESC(channels, "GF1 channels for " CRD_NAME " driver.");
58module_param_array(pcm_channels, int, NULL, 0444);
59MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for " CRD_NAME " driver.");
60
61static int snd_gusclassic_match(struct device *dev, unsigned int n)
62{
63	return enable[n];
64}
65
66static 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
112static 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
131static 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
194out:	snd_card_free(card);
195	return error;
196}
197
198static int snd_gusclassic_remove(struct device *dev, unsigned int n)
199{
200	snd_card_free(dev_get_drvdata(dev));
201	return 0;
202}
203
204static 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
217module_isa_driver(snd_gusclassic_driver, SNDRV_CARDS);
218