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