Linux Audio

Check our new training course

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