commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / sound / isa / gus / gus_main.c
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
34 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
35 MODULE_DESCRIPTION("Routines for Gravis UltraSound soundcards");
36 MODULE_LICENSE("GPL");
37
38 #define chip_t snd_gus_card_t
39
40 static int snd_gus_init_dma_irq(snd_gus_card_t * gus, int latches);
41
42 int snd_gus_use_inc(snd_gus_card_t * gus)
43 {
44         if (!try_module_get(gus->card->module))
45                 return 0;
46         return 1;
47 }
48
49 void snd_gus_use_dec(snd_gus_card_t * gus)
50 {
51         module_put(gus->card->module);
52 }
53
54 static int snd_gus_joystick_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
55 {
56         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
57         uinfo->count = 1;
58         uinfo->value.integer.min = 0;
59         uinfo->value.integer.max = 31;
60         return 0;
61 }
62
63 static int snd_gus_joystick_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
64 {
65         snd_gus_card_t *gus = snd_kcontrol_chip(kcontrol);
66         
67         ucontrol->value.integer.value[0] = gus->joystick_dac & 31;
68         return 0;
69 }
70
71 static int snd_gus_joystick_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
72 {
73         snd_gus_card_t *gus = snd_kcontrol_chip(kcontrol);
74         unsigned long flags;
75         int change;
76         unsigned char nval;
77         
78         nval = ucontrol->value.integer.value[0] & 31;
79         spin_lock_irqsave(&gus->reg_lock, flags);
80         change = gus->joystick_dac != nval;
81         gus->joystick_dac = nval;
82         snd_gf1_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
83         spin_unlock_irqrestore(&gus->reg_lock, flags);
84         return change;
85 }
86
87 static snd_kcontrol_new_t snd_gus_joystick_control = {
88         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
89         .name = "Joystick Speed",
90         .info = snd_gus_joystick_info,
91         .get = snd_gus_joystick_get,
92         .put = snd_gus_joystick_put
93 };
94
95 static void snd_gus_init_control(snd_gus_card_t *gus)
96 {
97         if (!gus->ace_flag)
98                 snd_ctl_add(gus->card, snd_ctl_new1(&snd_gus_joystick_control, gus));
99 }
100
101 /*
102  *
103  */
104
105 static int snd_gus_free(snd_gus_card_t *gus)
106 {
107         if (gus->gf1.res_port2 == NULL)
108                 goto __hw_end;
109 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
110         if (gus->seq_dev) {
111                 snd_device_free(gus->card, gus->seq_dev);
112                 gus->seq_dev = NULL;
113         }
114 #endif
115         snd_gf1_stop(gus);
116         snd_gus_init_dma_irq(gus, 0);
117       __hw_end:
118         if (gus->gf1.res_port1) {
119                 release_resource(gus->gf1.res_port1);
120                 kfree_nocheck(gus->gf1.res_port1);
121         }
122         if (gus->gf1.res_port2) {
123                 release_resource(gus->gf1.res_port2);
124                 kfree_nocheck(gus->gf1.res_port2);
125         }
126         if (gus->gf1.irq >= 0)
127                 free_irq(gus->gf1.irq, (void *) gus);
128         if (gus->gf1.dma1 >= 0) {
129                 disable_dma(gus->gf1.dma1);
130                 free_dma(gus->gf1.dma1);
131         }
132         if (!gus->equal_dma && gus->gf1.dma2 >= 0) {
133                 disable_dma(gus->gf1.dma2);
134                 free_dma(gus->gf1.dma2);
135         }
136         snd_magic_kfree(gus);
137         return 0;
138 }
139
140 static int snd_gus_dev_free(snd_device_t *device)
141 {
142         snd_gus_card_t *gus = snd_magic_cast(snd_gus_card_t, device->device_data, return -ENXIO);
143         return snd_gus_free(gus);
144 }
145
146 int snd_gus_create(snd_card_t * card,
147                    unsigned long port,
148                    int irq, int dma1, int dma2,
149                    int timer_dev,
150                    int voices,
151                    int pcm_channels,
152                    int effect,
153                    snd_gus_card_t **rgus)
154 {
155         snd_gus_card_t *gus;
156         int err;
157         static snd_device_ops_t ops = {
158                 .dev_free =     snd_gus_dev_free,
159         };
160
161         *rgus = NULL;
162         gus = snd_magic_kcalloc(snd_gus_card_t, 0, GFP_KERNEL);
163         if (gus == NULL)
164                 return -ENOMEM;
165         gus->gf1.irq = -1;
166         gus->gf1.dma1 = -1;
167         gus->gf1.dma2 = -1;
168         gus->card = card;
169         gus->gf1.port = port;
170         /* fill register variables for speedup */
171         gus->gf1.reg_page = GUSP(gus, GF1PAGE);
172         gus->gf1.reg_regsel = GUSP(gus, GF1REGSEL);
173         gus->gf1.reg_data8 = GUSP(gus, GF1DATAHIGH);
174         gus->gf1.reg_data16 = GUSP(gus, GF1DATALOW);
175         gus->gf1.reg_irqstat = GUSP(gus, IRQSTAT);
176         gus->gf1.reg_dram = GUSP(gus, DRAM);
177         gus->gf1.reg_timerctrl = GUSP(gus, TIMERCNTRL);
178         gus->gf1.reg_timerdata = GUSP(gus, TIMERDATA);
179         /* allocate resources */
180         if ((gus->gf1.res_port1 = request_region(port, 16, "GUS GF1 (Adlib/SB)")) == NULL) {
181                 snd_gus_free(gus);
182                 return -EBUSY;
183         }
184         if ((gus->gf1.res_port2 = request_region(port + 0x100, 12, "GUS GF1 (Synth)")) == NULL) {
185                 snd_gus_free(gus);
186                 return -EBUSY;
187         }
188         if (irq >= 0 && request_irq(irq, snd_gus_interrupt, SA_INTERRUPT, "GUS GF1", (void *) gus)) {
189                 snd_gus_free(gus);
190                 return -EBUSY;
191         }
192         gus->gf1.irq = irq;
193         if (request_dma(dma1, "GUS - 1")) {
194                 snd_gus_free(gus);
195                 return -EBUSY;
196         }
197         gus->gf1.dma1 = dma1;
198         if (dma2 >= 0 && dma1 != dma2) {
199                 if (request_dma(dma2, "GUS - 2")) {
200                         snd_gus_free(gus);
201                         return -EBUSY;
202                 }
203                 gus->gf1.dma2 = dma2;
204         } else {
205                 gus->gf1.dma2 = gus->gf1.dma1;
206                 gus->equal_dma = 1;
207         }
208         gus->timer_dev = timer_dev;
209         if (voices < 14)
210                 voices = 14;
211         if (voices > 32)
212                 voices = 32;
213         if (pcm_channels < 0)
214                 pcm_channels = 0;
215         if (pcm_channels > 8)
216                 pcm_channels = 8;
217         pcm_channels++;
218         pcm_channels &= ~1;
219         gus->gf1.effect = effect ? 1 : 0;
220         gus->gf1.active_voices = voices;
221         gus->gf1.pcm_channels = pcm_channels;
222         gus->gf1.volume_ramp = 25;
223         gus->gf1.smooth_pan = 1;
224         spin_lock_init(&gus->reg_lock);
225         spin_lock_init(&gus->voice_alloc);
226         spin_lock_init(&gus->active_voice_lock);
227         spin_lock_init(&gus->event_lock);
228         spin_lock_init(&gus->dma_lock);
229         spin_lock_init(&gus->pcm_volume_level_lock);
230         spin_lock_init(&gus->uart_cmd_lock);
231         init_MUTEX(&gus->dma_mutex);
232         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, gus, &ops)) < 0) {
233                 snd_gus_free(gus);
234                 return err;
235         }
236         *rgus = gus;
237         return 0;
238 }
239
240 /*
241  *  Memory detection routine for plain GF1 soundcards
242  */
243
244 static int snd_gus_detect_memory(snd_gus_card_t * gus)
245 {
246         int l, idx, local;
247         unsigned char d;
248
249         snd_gf1_poke(gus, 0L, 0xaa);
250         snd_gf1_poke(gus, 1L, 0x55);
251         if (snd_gf1_peek(gus, 0L) != 0xaa || snd_gf1_peek(gus, 1L) != 0x55) {
252                 snd_printk("plain GF1 card at 0x%lx without onboard DRAM?\n", gus->gf1.port);
253                 return -ENOMEM;
254         }
255         for (idx = 1, d = 0xab; idx < 4; idx++, d++) {
256                 local = idx << 18;
257                 snd_gf1_poke(gus, local, d);
258                 snd_gf1_poke(gus, local + 1, d + 1);
259                 if (snd_gf1_peek(gus, local) != d ||
260                     snd_gf1_peek(gus, local + 1) != d + 1 ||
261                     snd_gf1_peek(gus, 0L) != 0xaa)
262                         break;
263         }
264 #if 1
265         gus->gf1.memory = idx << 18;
266 #else
267         gus->gf1.memory = 256 * 1024;
268 #endif
269         for (l = 0, local = gus->gf1.memory; l < 4; l++, local -= 256 * 1024) {
270                 gus->gf1.mem_alloc.banks_8[l].address =
271                     gus->gf1.mem_alloc.banks_8[l].size = 0;
272                 gus->gf1.mem_alloc.banks_16[l].address = l << 18;
273                 gus->gf1.mem_alloc.banks_16[l].size = local > 0 ? 256 * 1024 : 0;
274         }
275         gus->gf1.mem_alloc.banks_8[0].size = gus->gf1.memory;
276         return 0;               /* some memory were detected */
277 }
278
279 static int snd_gus_init_dma_irq(snd_gus_card_t * gus, int latches)
280 {
281         snd_card_t *card;
282         unsigned long flags;
283         int irq, dma1, dma2;
284         static unsigned char irqs[16] =
285                 {0, 0, 1, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7};
286         static unsigned char dmas[8] =
287                 {6, 1, 0, 2, 0, 3, 4, 5};
288
289         snd_assert(gus != NULL, return -EINVAL);
290         card = gus->card;
291         snd_assert(card != NULL, return -EINVAL);
292
293         gus->mix_cntrl_reg &= 0xf8;
294         gus->mix_cntrl_reg |= 0x01;     /* disable MIC, LINE IN, enable LINE OUT */
295         if (gus->codec_flag || gus->ess_flag) {
296                 gus->mix_cntrl_reg &= ~1;       /* enable LINE IN */
297                 gus->mix_cntrl_reg |= 4;        /* enable MIC */
298         }
299         dma1 = gus->gf1.dma1;
300         dma1 = dma1 < 0 ? -dma1 : dma1;
301         dma1 = dmas[dma1 & 7];
302         dma2 = gus->gf1.dma2;
303         dma2 = dma2 < 0 ? -dma2 : dma2;
304         dma2 = dmas[dma2 & 7];
305 #if 0
306         printk("dma1 = %i, dma2 = %i\n", gus->gf1.dma1, gus->gf1.dma2);
307 #endif
308         dma1 |= gus->equal_dma ? 0x40 : (dma2 << 3);
309
310         if ((dma1 & 7) == 0 || (dma2 & 7) == 0) {
311                 snd_printk("Error! DMA isn't defined.\n");
312                 return -EINVAL;
313         }
314         irq = gus->gf1.irq;
315         irq = irq < 0 ? -irq : irq;
316         irq = irqs[irq & 0x0f];
317         if (irq == 0) {
318                 snd_printk("Error! IRQ isn't defined.\n");
319                 return -EINVAL;
320         }
321         irq |= 0x40;
322 #if 0
323         card->mixer.mix_ctrl_reg |= 0x10;
324 #endif
325
326         spin_lock_irqsave(&gus->reg_lock, flags);
327         outb(5, GUSP(gus, REGCNTRLS));
328         outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
329         outb(0x00, GUSP(gus, IRQDMACNTRLREG));
330         outb(0, GUSP(gus, REGCNTRLS));
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         udelay(100);
345
346         spin_lock_irqsave(&gus->reg_lock, flags);
347         outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
348         outb(dma1, GUSP(gus, IRQDMACNTRLREG));
349         if (latches) {
350                 outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
351                 outb(irq, GUSP(gus, IRQDMACNTRLREG));
352         }
353         spin_unlock_irqrestore(&gus->reg_lock, flags);
354
355         snd_gf1_delay(gus);
356
357         if (latches)
358                 gus->mix_cntrl_reg |= 0x08;     /* enable latches */
359         else
360                 gus->mix_cntrl_reg &= ~0x08;    /* disable latches */
361         spin_lock_irqsave(&gus->reg_lock, flags);
362         outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
363         outb(0, GUSP(gus, GF1PAGE));
364         spin_unlock_irqrestore(&gus->reg_lock, flags);
365
366         return 0;
367 }
368
369 static int snd_gus_check_version(snd_gus_card_t * gus)
370 {
371         unsigned long flags;
372         unsigned char val, rev;
373         snd_card_t *card;
374
375         card = gus->card;
376         spin_lock_irqsave(&gus->reg_lock, flags);
377         outb(0x20, GUSP(gus, REGCNTRLS));
378         val = inb(GUSP(gus, REGCNTRLS));
379         rev = inb(GUSP(gus, BOARDVERSION));
380         spin_unlock_irqrestore(&gus->reg_lock, flags);
381         snd_printdd("GF1 [0x%lx] init - val = 0x%x, rev = 0x%x\n", gus->gf1.port, val, rev);
382         strcpy(card->driver, "GUS");
383         strcpy(card->longname, "Gravis UltraSound Classic (2.4)");
384         if ((val != 255 && (val & 0x06)) || (rev >= 5 && rev != 255)) {
385                 if (rev >= 5 && rev <= 9) {
386                         gus->ics_flag = 1;
387                         if (rev == 5)
388                                 gus->ics_flipped = 1;
389                         card->longname[27] = '3';
390                         card->longname[29] = rev == 5 ? '5' : '7';
391                 }
392                 if (rev >= 10 && rev != 255) {
393                         if (rev >= 10 && rev <= 11) {
394                                 strcpy(card->driver, "GUS MAX");
395                                 strcpy(card->longname, "Gravis UltraSound MAX");
396                                 gus->max_flag = 1;
397                         } else if (rev == 0x30) {
398                                 strcpy(card->driver, "GUS ACE");
399                                 strcpy(card->longname, "Gravis UltraSound Ace");
400                                 gus->ace_flag = 1;
401                         } else if (rev == 0x50) {
402                                 strcpy(card->driver, "GUS Extreme");
403                                 strcpy(card->longname, "Gravis UltraSound Extreme");
404                                 gus->ess_flag = 1;
405                         } else {
406                                 snd_printk("unknown GF1 revision number at 0x%lx - 0x%x (0x%x)\n", gus->gf1.port, rev, val);
407                                 snd_printk("  please - report to <perex@suse.cz>\n");
408                         }
409                 }
410         }
411         strcpy(card->shortname, card->longname);
412         gus->uart_enable = 1;   /* standard GUSes doesn't have midi uart trouble */
413         snd_gus_init_control(gus);
414         return 0;
415 }
416
417 static void snd_gus_seq_dev_free(snd_seq_device_t *seq_dev)
418 {
419         snd_gus_card_t *gus = snd_magic_cast(snd_gus_card_t, seq_dev->private_data, return);
420         gus->seq_dev = NULL;
421 }
422
423 int snd_gus_initialize(snd_gus_card_t *gus)
424 {
425         int err;
426
427         if (!gus->interwave) {
428                 if ((err = snd_gus_check_version(gus)) < 0) {
429                         snd_printk("version check failed\n");
430                         return err;
431                 }
432                 if ((err = snd_gus_detect_memory(gus)) < 0)
433                         return err;
434         }
435         if ((err = snd_gus_init_dma_irq(gus, 1)) < 0)
436                 return err;
437 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
438         if (snd_seq_device_new(gus->card, 1, SNDRV_SEQ_DEV_ID_GUS,
439                                sizeof(snd_gus_card_t*), &gus->seq_dev) >= 0) {
440                 strcpy(gus->seq_dev->name, "GUS");
441                 *(snd_gus_card_t**)SNDRV_SEQ_DEVICE_ARGPTR(gus->seq_dev) = gus;
442                 gus->seq_dev->private_data = gus;
443                 gus->seq_dev->private_free = snd_gus_seq_dev_free;
444         }
445 #endif
446         snd_gf1_start(gus);
447         gus->initialized = 1;
448         return 0;
449 }
450
451   /* gus_io.c */
452 EXPORT_SYMBOL(snd_gf1_delay);
453 EXPORT_SYMBOL(snd_gf1_write8);
454 EXPORT_SYMBOL(snd_gf1_look8);
455 EXPORT_SYMBOL(snd_gf1_write16);
456 EXPORT_SYMBOL(snd_gf1_look16);
457 EXPORT_SYMBOL(snd_gf1_i_write8);
458 EXPORT_SYMBOL(snd_gf1_i_look8);
459 EXPORT_SYMBOL(snd_gf1_i_write16);
460 EXPORT_SYMBOL(snd_gf1_i_look16);
461 EXPORT_SYMBOL(snd_gf1_dram_addr);
462 EXPORT_SYMBOL(snd_gf1_write_addr);
463 EXPORT_SYMBOL(snd_gf1_poke);
464 EXPORT_SYMBOL(snd_gf1_peek);
465   /* gus_reset.c */
466 EXPORT_SYMBOL(snd_gf1_alloc_voice);
467 EXPORT_SYMBOL(snd_gf1_free_voice);
468 EXPORT_SYMBOL(snd_gf1_ctrl_stop);
469 EXPORT_SYMBOL(snd_gf1_stop_voice);
470 EXPORT_SYMBOL(snd_gf1_start);
471 EXPORT_SYMBOL(snd_gf1_stop);
472   /* gus_mixer.c */
473 EXPORT_SYMBOL(snd_gf1_new_mixer);
474   /* gus_pcm.c */
475 EXPORT_SYMBOL(snd_gf1_pcm_new);
476   /* gus.c */
477 EXPORT_SYMBOL(snd_gus_use_inc);
478 EXPORT_SYMBOL(snd_gus_use_dec);
479 EXPORT_SYMBOL(snd_gus_create);
480 EXPORT_SYMBOL(snd_gus_initialize);
481   /* gus_irq.c */
482 EXPORT_SYMBOL(snd_gus_interrupt);
483   /* gus_uart.c */
484 EXPORT_SYMBOL(snd_gf1_rawmidi_new);
485   /* gus_dram.c */
486 EXPORT_SYMBOL(snd_gus_dram_write);
487 EXPORT_SYMBOL(snd_gus_dram_read);
488   /* gus_volume.c */
489 EXPORT_SYMBOL(snd_gf1_lvol_to_gvol_raw);
490 EXPORT_SYMBOL(snd_gf1_translate_freq);
491   /* gus_mem.c */
492 EXPORT_SYMBOL(snd_gf1_mem_alloc);
493 EXPORT_SYMBOL(snd_gf1_mem_xfree);
494 EXPORT_SYMBOL(snd_gf1_mem_free);
495 EXPORT_SYMBOL(snd_gf1_mem_lock);
496
497 /*
498  *  INIT part
499  */
500
501 static int __init alsa_gus_init(void)
502 {
503         return 0;
504 }
505
506 static void __exit alsa_gus_exit(void)
507 {
508 }
509
510 module_init(alsa_gus_init)
511 module_exit(alsa_gus_exit)