File Index
Files and directories
Sign in
FILE

gus_main.c

File preview with syntax highlighting.

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