1/*
2 *  Routines for Gravis UltraSound soundcards
3 *  Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/ioport.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@suse.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 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#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && \
108	defined(CONFIG_SND_SEQUENCER_MODULE))
109	if (gus->seq_dev) {
110		snd_device_free(gus->card, gus->seq_dev);
111		gus->seq_dev = NULL;
112	}
113#endif
114	snd_gf1_stop(gus);
115	snd_gus_init_dma_irq(gus, 0);
116      __hw_end:
117	release_and_free_resource(gus->gf1.res_port1);
118	release_and_free_resource(gus->gf1.res_port2);
119	if (gus->gf1.irq >= 0)
120		free_irq(gus->gf1.irq, (void *) gus);
121	if (gus->gf1.dma1 >= 0) {
122		disable_dma(gus->gf1.dma1);
123		free_dma(gus->gf1.dma1);
124	}
125	if (!gus->equal_dma && gus->gf1.dma2 >= 0) {
126		disable_dma(gus->gf1.dma2);
127		free_dma(gus->gf1.dma2);
128	}
129	kfree(gus);
130	return 0;
131}
132
133static int snd_gus_dev_free(struct snd_device *device)
134{
135	struct snd_gus_card *gus = device->device_data;
136	return snd_gus_free(gus);
137}
138
139int snd_gus_create(struct snd_card *card,
140		   unsigned long port,
141		   int irq, int dma1, int dma2,
142		   int timer_dev,
143		   int voices,
144		   int pcm_channels,
145		   int effect,
146		   struct snd_gus_card **rgus)
147{
148	struct snd_gus_card *gus;
149	int err;
150	static struct snd_device_ops ops = {
151		.dev_free =	snd_gus_dev_free,
152	};
153
154	*rgus = NULL;
155	gus = kzalloc(sizeof(*gus), GFP_KERNEL);
156	if (gus == NULL)
157		return -ENOMEM;
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	spin_lock_init(&gus->reg_lock);
223	spin_lock_init(&gus->voice_alloc);
224	spin_lock_init(&gus->active_voice_lock);
225	spin_lock_init(&gus->event_lock);
226	spin_lock_init(&gus->dma_lock);
227	spin_lock_init(&gus->pcm_volume_level_lock);
228	spin_lock_init(&gus->uart_cmd_lock);
229	mutex_init(&gus->dma_mutex);
230	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, gus, &ops)) < 0) {
231		snd_gus_free(gus);
232		return err;
233	}
234	*rgus = gus;
235	return 0;
236}
237
238/*
239 *  Memory detection routine for plain GF1 soundcards
240 */
241
242static int snd_gus_detect_memory(struct snd_gus_card * gus)
243{
244	int l, idx, local;
245	unsigned char d;
246
247	snd_gf1_poke(gus, 0L, 0xaa);
248	snd_gf1_poke(gus, 1L, 0x55);
249	if (snd_gf1_peek(gus, 0L) != 0xaa || snd_gf1_peek(gus, 1L) != 0x55) {
250		snd_printk(KERN_ERR "plain GF1 card at 0x%lx without onboard DRAM?\n", gus->gf1.port);
251		return -ENOMEM;
252	}
253	for (idx = 1, d = 0xab; idx < 4; idx++, d++) {
254		local = idx << 18;
255		snd_gf1_poke(gus, local, d);
256		snd_gf1_poke(gus, local + 1, d + 1);
257		if (snd_gf1_peek(gus, local) != d ||
258		    snd_gf1_peek(gus, local + 1) != d + 1 ||
259		    snd_gf1_peek(gus, 0L) != 0xaa)
260			break;
261	}
262	gus->gf1.memory = idx << 18;
263	for (l = 0, local = gus->gf1.memory; l < 4; l++, local -= 256 * 1024) {
264		gus->gf1.mem_alloc.banks_8[l].address =
265		    gus->gf1.mem_alloc.banks_8[l].size = 0;
266		gus->gf1.mem_alloc.banks_16[l].address = l << 18;
267		gus->gf1.mem_alloc.banks_16[l].size = local > 0 ? 256 * 1024 : 0;
268	}
269	gus->gf1.mem_alloc.banks_8[0].size = gus->gf1.memory;
270	return 0;		/* some memory were detected */
271}
272
273static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches)
274{
275	struct snd_card *card;
276	unsigned long flags;
277	int irq, dma1, dma2;
278	static unsigned char irqs[16] =
279		{0, 0, 1, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7};
280	static unsigned char dmas[8] =
281		{6, 1, 0, 2, 0, 3, 4, 5};
282
283	snd_assert(gus != NULL, return -EINVAL);
284	card = gus->card;
285	snd_assert(card != NULL, return -EINVAL);
286
287	gus->mix_cntrl_reg &= 0xf8;
288	gus->mix_cntrl_reg |= 0x01;	/* disable MIC, LINE IN, enable LINE OUT */
289	if (gus->codec_flag || gus->ess_flag) {
290		gus->mix_cntrl_reg &= ~1;	/* enable LINE IN */
291		gus->mix_cntrl_reg |= 4;	/* enable MIC */
292	}
293	dma1 = gus->gf1.dma1;
294	dma1 = abs(dma1);
295	dma1 = dmas[dma1 & 7];
296	dma2 = gus->gf1.dma2;
297	dma2 = abs(dma2);
298	dma2 = dmas[dma2 & 7];
299	dma1 |= gus->equal_dma ? 0x40 : (dma2 << 3);
300
301	if ((dma1 & 7) == 0 || (dma2 & 7) == 0) {
302		snd_printk(KERN_ERR "Error! DMA isn't defined.\n");
303		return -EINVAL;
304	}
305	irq = gus->gf1.irq;
306	irq = abs(irq);
307	irq = irqs[irq & 0x0f];
308	if (irq == 0) {
309		snd_printk(KERN_ERR "Error! IRQ isn't defined.\n");
310		return -EINVAL;
311	}
312	irq |= 0x40;
313
314	spin_lock_irqsave(&gus->reg_lock, flags);
315	outb(5, GUSP(gus, REGCNTRLS));
316	outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
317	outb(0x00, GUSP(gus, IRQDMACNTRLREG));
318	outb(0, GUSP(gus, REGCNTRLS));
319	spin_unlock_irqrestore(&gus->reg_lock, flags);
320
321	udelay(100);
322
323	spin_lock_irqsave(&gus->reg_lock, flags);
324	outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
325	outb(dma1, GUSP(gus, IRQDMACNTRLREG));
326	if (latches) {
327		outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
328		outb(irq, GUSP(gus, IRQDMACNTRLREG));
329	}
330	spin_unlock_irqrestore(&gus->reg_lock, flags);
331
332	udelay(100);
333
334	spin_lock_irqsave(&gus->reg_lock, flags);
335	outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
336	outb(dma1, GUSP(gus, IRQDMACNTRLREG));
337	if (latches) {
338		outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
339		outb(irq, GUSP(gus, IRQDMACNTRLREG));
340	}
341	spin_unlock_irqrestore(&gus->reg_lock, flags);
342
343	snd_gf1_delay(gus);
344
345	if (latches)
346		gus->mix_cntrl_reg |= 0x08;	/* enable latches */
347	else
348		gus->mix_cntrl_reg &= ~0x08;	/* disable latches */
349	spin_lock_irqsave(&gus->reg_lock, flags);
350	outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
351	outb(0, GUSP(gus, GF1PAGE));
352	spin_unlock_irqrestore(&gus->reg_lock, flags);
353
354	return 0;
355}
356
357static int snd_gus_check_version(struct snd_gus_card * gus)
358{
359	unsigned long flags;
360	unsigned char val, rev;
361	struct snd_card *card;
362
363	card = gus->card;
364	spin_lock_irqsave(&gus->reg_lock, flags);
365	outb(0x20, GUSP(gus, REGCNTRLS));
366	val = inb(GUSP(gus, REGCNTRLS));
367	rev = inb(GUSP(gus, BOARDVERSION));
368	spin_unlock_irqrestore(&gus->reg_lock, flags);
369	snd_printdd("GF1 [0x%lx] init - val = 0x%x, rev = 0x%x\n", gus->gf1.port, val, rev);
370	strcpy(card->driver, "GUS");
371	strcpy(card->longname, "Gravis UltraSound Classic (2.4)");
372	if ((val != 255 && (val & 0x06)) || (rev >= 5 && rev != 255)) {
373		if (rev >= 5 && rev <= 9) {
374			gus->ics_flag = 1;
375			if (rev == 5)
376				gus->ics_flipped = 1;
377			card->longname[27] = '3';
378			card->longname[29] = rev == 5 ? '5' : '7';
379		}
380		if (rev >= 10 && rev != 255) {
381			if (rev >= 10 && rev <= 11) {
382				strcpy(card->driver, "GUS MAX");
383				strcpy(card->longname, "Gravis UltraSound MAX");
384				gus->max_flag = 1;
385			} else if (rev == 0x30) {
386				strcpy(card->driver, "GUS ACE");
387				strcpy(card->longname, "Gravis UltraSound Ace");
388				gus->ace_flag = 1;
389			} else if (rev == 0x50) {
390				strcpy(card->driver, "GUS Extreme");
391				strcpy(card->longname, "Gravis UltraSound Extreme");
392				gus->ess_flag = 1;
393			} else {
394				snd_printk(KERN_ERR "unknown GF1 revision number at 0x%lx - 0x%x (0x%x)\n", gus->gf1.port, rev, val);
395				snd_printk(KERN_ERR "  please - report to <perex@suse.cz>\n");
396			}
397		}
398	}
399	strcpy(card->shortname, card->longname);
400	gus->uart_enable = 1;	/* standard GUSes doesn't have midi uart trouble */
401	snd_gus_init_control(gus);
402	return 0;
403}
404
405#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && \
406	defined(CONFIG_SND_SEQUENCER_MODULE))
407static void snd_gus_seq_dev_free(struct snd_seq_device *seq_dev)
408{
409	struct snd_gus_card *gus = seq_dev->private_data;
410	gus->seq_dev = NULL;
411}
412#endif
413
414int snd_gus_initialize(struct snd_gus_card *gus)
415{
416	int err;
417
418	if (!gus->interwave) {
419		if ((err = snd_gus_check_version(gus)) < 0) {
420			snd_printk(KERN_ERR "version check failed\n");
421			return err;
422		}
423		if ((err = snd_gus_detect_memory(gus)) < 0)
424			return err;
425	}
426	if ((err = snd_gus_init_dma_irq(gus, 1)) < 0)
427		return err;
428#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && \
429	defined(CONFIG_SND_SEQUENCER_MODULE))
430	if (snd_seq_device_new(gus->card, 1, SNDRV_SEQ_DEV_ID_GUS,
431			       sizeof(struct snd_gus_card *), &gus->seq_dev) >= 0) {
432		strcpy(gus->seq_dev->name, "GUS");
433		*(struct snd_gus_card **)SNDRV_SEQ_DEVICE_ARGPTR(gus->seq_dev) = gus;
434		gus->seq_dev->private_data = gus;
435		gus->seq_dev->private_free = snd_gus_seq_dev_free;
436	}
437#endif
438	snd_gf1_start(gus);
439	gus->initialized = 1;
440	return 0;
441}
442
443  /* gus_io.c */
444EXPORT_SYMBOL(snd_gf1_delay);
445EXPORT_SYMBOL(snd_gf1_write8);
446EXPORT_SYMBOL(snd_gf1_look8);
447EXPORT_SYMBOL(snd_gf1_write16);
448EXPORT_SYMBOL(snd_gf1_look16);
449EXPORT_SYMBOL(snd_gf1_i_write8);
450EXPORT_SYMBOL(snd_gf1_i_look8);
451EXPORT_SYMBOL(snd_gf1_i_look16);
452EXPORT_SYMBOL(snd_gf1_dram_addr);
453EXPORT_SYMBOL(snd_gf1_write_addr);
454EXPORT_SYMBOL(snd_gf1_poke);
455EXPORT_SYMBOL(snd_gf1_peek);
456  /* gus_reset.c */
457EXPORT_SYMBOL(snd_gf1_alloc_voice);
458EXPORT_SYMBOL(snd_gf1_free_voice);
459EXPORT_SYMBOL(snd_gf1_ctrl_stop);
460EXPORT_SYMBOL(snd_gf1_stop_voice);
461  /* gus_mixer.c */
462EXPORT_SYMBOL(snd_gf1_new_mixer);
463  /* gus_pcm.c */
464EXPORT_SYMBOL(snd_gf1_pcm_new);
465  /* gus.c */
466EXPORT_SYMBOL(snd_gus_use_inc);
467EXPORT_SYMBOL(snd_gus_use_dec);
468EXPORT_SYMBOL(snd_gus_create);
469EXPORT_SYMBOL(snd_gus_initialize);
470  /* gus_irq.c */
471EXPORT_SYMBOL(snd_gus_interrupt);
472  /* gus_uart.c */
473EXPORT_SYMBOL(snd_gf1_rawmidi_new);
474  /* gus_dram.c */
475EXPORT_SYMBOL(snd_gus_dram_write);
476EXPORT_SYMBOL(snd_gus_dram_read);
477  /* gus_volume.c */
478EXPORT_SYMBOL(snd_gf1_lvol_to_gvol_raw);
479EXPORT_SYMBOL(snd_gf1_translate_freq);
480  /* gus_mem.c */
481EXPORT_SYMBOL(snd_gf1_mem_alloc);
482EXPORT_SYMBOL(snd_gf1_mem_xfree);
483EXPORT_SYMBOL(snd_gf1_mem_free);
484EXPORT_SYMBOL(snd_gf1_mem_lock);
485
486/*
487 *  INIT part
488 */
489
490static int __init alsa_gus_init(void)
491{
492	return 0;
493}
494
495static void __exit alsa_gus_exit(void)
496{
497}
498
499module_init(alsa_gus_init)
500module_exit(alsa_gus_exit)
501