blob: 57beb74f5b9dbe35242fc821b366a62c168a43c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for Gravis UltraSound Classic soundcard
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
Takashi Iwai654aa662005-11-17 17:13:43 +010024#include <linux/err.h>
25#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/delay.h>
27#include <linux/time.h>
28#include <linux/moduleparam.h>
Takashi Iwai654aa662005-11-17 17:13:43 +010029#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/gus.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define SNDRV_LEGACY_FIND_FREE_IRQ
33#define SNDRV_LEGACY_FIND_FREE_DMA
34#include <sound/initval.h>
35
36MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
37MODULE_DESCRIPTION("Gravis UltraSound Classic");
38MODULE_LICENSE("GPL");
39MODULE_SUPPORTED_DEVICE("{{Gravis,UltraSound Classic}}");
40
41static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
42static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
43static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
44static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */
45static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,5,9,11,12,15 */
46static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
47static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
48static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29};
49 /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */
50static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24};
51static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
52
53module_param_array(index, int, NULL, 0444);
54MODULE_PARM_DESC(index, "Index value for GUS Classic soundcard.");
55module_param_array(id, charp, NULL, 0444);
56MODULE_PARM_DESC(id, "ID string for GUS Classic soundcard.");
57module_param_array(enable, bool, NULL, 0444);
58MODULE_PARM_DESC(enable, "Enable GUS Classic soundcard.");
59module_param_array(port, long, NULL, 0444);
60MODULE_PARM_DESC(port, "Port # for GUS Classic driver.");
61module_param_array(irq, int, NULL, 0444);
62MODULE_PARM_DESC(irq, "IRQ # for GUS Classic driver.");
63module_param_array(dma1, int, NULL, 0444);
64MODULE_PARM_DESC(dma1, "DMA1 # for GUS Classic driver.");
65module_param_array(dma2, int, NULL, 0444);
66MODULE_PARM_DESC(dma2, "DMA2 # for GUS Classic driver.");
67module_param_array(joystick_dac, int, NULL, 0444);
68MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS Classic driver.");
69module_param_array(channels, int, NULL, 0444);
70MODULE_PARM_DESC(channels, "GF1 channels for GUS Classic driver.");
71module_param_array(pcm_channels, int, NULL, 0444);
72MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS Classic driver.");
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Takashi Iwai43bcd972005-09-05 17:19:20 +020075#define PFX "gusclassic: "
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Takashi Iwai5e2da202005-11-17 14:36:44 +010077static int __init snd_gusclassic_detect(struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Takashi Iwai43bcd972005-09-05 17:19:20 +020079 unsigned char d;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Takashi Iwai43bcd972005-09-05 17:19:20 +020081 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */
82 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 0) {
83 snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return -ENODEV;
Takashi Iwai43bcd972005-09-05 17:19:20 +020085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 udelay(160);
87 snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */
88 udelay(160);
Takashi Iwai43bcd972005-09-05 17:19:20 +020089 if (((d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)) & 0x07) != 1) {
90 snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -ENODEV;
Takashi Iwai43bcd972005-09-05 17:19:20 +020092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94}
95
Takashi Iwai5e2da202005-11-17 14:36:44 +010096static void __init snd_gusclassic_init(int dev, struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 gus->equal_irq = 0;
99 gus->codec_flag = 0;
100 gus->max_flag = 0;
101 gus->joystick_dac = joystick_dac[dev];
102}
103
Takashi Iwai654aa662005-11-17 17:13:43 +0100104static int __init snd_gusclassic_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Takashi Iwai654aa662005-11-17 17:13:43 +0100106 int dev = pdev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 static int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, 4, -1};
108 static int possible_dmas[] = {5, 6, 7, 1, 3, -1};
109 int xirq, xdma1, xdma2;
Takashi Iwai5e2da202005-11-17 14:36:44 +0100110 struct snd_card *card;
Takashi Iwai5e2da202005-11-17 14:36:44 +0100111 struct snd_gus_card *gus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int err;
113
114 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
115 if (card == NULL)
116 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (pcm_channels[dev] < 2)
118 pcm_channels[dev] = 2;
119
120 xirq = irq[dev];
121 if (xirq == SNDRV_AUTO_IRQ) {
122 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200123 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
124 err = -EBUSY;
125 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127 }
128 xdma1 = dma1[dev];
129 if (xdma1 == SNDRV_AUTO_DMA) {
130 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200131 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
132 err = -EBUSY;
133 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 }
136 xdma2 = dma2[dev];
137 if (xdma2 == SNDRV_AUTO_DMA) {
138 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200139 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
140 err = -EBUSY;
141 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 }
144
Takashi Iwai654aa662005-11-17 17:13:43 +0100145 if (port[dev] != SNDRV_AUTO_PORT) {
146 err = snd_gus_create(card,
147 port[dev],
148 xirq, xdma1, xdma2,
149 0, channels[dev], pcm_channels[dev],
150 0, &gus);
151 } else {
152 /* auto-probe legacy ports */
153 static unsigned long possible_ports[] = {
154 0x220, 0x230, 0x240, 0x250, 0x260,
155 };
156 int i;
157 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
158 err = snd_gus_create(card,
159 possible_ports[i],
160 xirq, xdma1, xdma2,
161 0, channels[dev], pcm_channels[dev],
162 0, &gus);
163 if (err >= 0) {
164 port[dev] = possible_ports[i];
165 break;
166 }
167 }
168 }
169 if (err < 0)
Takashi Iwai43bcd972005-09-05 17:19:20 +0200170 goto _err;
171
172 if ((err = snd_gusclassic_detect(gus)) < 0)
173 goto _err;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 snd_gusclassic_init(dev, gus);
Takashi Iwai43bcd972005-09-05 17:19:20 +0200176 if ((err = snd_gus_initialize(gus)) < 0)
177 goto _err;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (gus->max_flag || gus->ess_flag) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200180 snd_printk(KERN_ERR PFX "GUS Classic or ACE soundcard was not detected at 0x%lx\n", gus->gf1.port);
181 err = -ENODEV;
182 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Takashi Iwai43bcd972005-09-05 17:19:20 +0200184
185 if ((err = snd_gf1_new_mixer(gus)) < 0)
186 goto _err;
187
188 if ((err = snd_gf1_pcm_new(gus, 0, 0, NULL)) < 0)
189 goto _err;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!gus->ace_flag) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200192 if ((err = snd_gf1_rawmidi_new(gus, 0, NULL)) < 0)
193 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %d, dma %d", gus->gf1.port, xirq, xdma1);
196 if (dma2 >= 0)
197 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
Takashi Iwai43bcd972005-09-05 17:19:20 +0200198
Takashi Iwai654aa662005-11-17 17:13:43 +0100199 snd_card_set_dev(card, &pdev->dev);
Takashi Iwai43bcd972005-09-05 17:19:20 +0200200
201 if ((err = snd_card_register(card)) < 0)
202 goto _err;
203
Takashi Iwai654aa662005-11-17 17:13:43 +0100204 platform_set_drvdata(pdev, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 0;
Takashi Iwai43bcd972005-09-05 17:19:20 +0200206
207 _err:
208 snd_card_free(card);
209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Takashi Iwai654aa662005-11-17 17:13:43 +0100212static int snd_gusclassic_remove(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Takashi Iwai654aa662005-11-17 17:13:43 +0100214 snd_card_free(platform_get_drvdata(devptr));
215 platform_set_drvdata(devptr, NULL);
216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Takashi Iwai654aa662005-11-17 17:13:43 +0100219#define GUSCLASSIC_DRIVER "snd_gusclassic"
220
221static struct platform_driver snd_gusclassic_driver = {
222 .probe = snd_gusclassic_probe,
223 .remove = snd_gusclassic_remove,
224 /* FIXME: suspend/resume */
225 .driver = {
226 .name = GUSCLASSIC_DRIVER
227 },
228};
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int __init alsa_card_gusclassic_init(void)
231{
Takashi Iwai654aa662005-11-17 17:13:43 +0100232 int i, cards, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Takashi Iwai654aa662005-11-17 17:13:43 +0100234 err = platform_driver_register(&snd_gusclassic_driver);
235 if (err < 0)
236 return err;
237
238 cards = 0;
239 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
240 struct platform_device *device;
241 device = platform_device_register_simple(GUSCLASSIC_DRIVER,
242 i, NULL, 0);
243 if (IS_ERR(device)) {
244 err = PTR_ERR(device);
245 goto errout;
246 }
247 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!cards) {
250#ifdef MODULE
251 printk(KERN_ERR "GUS Classic soundcard not found or device busy\n");
252#endif
Takashi Iwai654aa662005-11-17 17:13:43 +0100253 err = -ENODEV;
254 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 return 0;
Takashi Iwai654aa662005-11-17 17:13:43 +0100257
258 errout:
259 platform_driver_unregister(&snd_gusclassic_driver);
260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263static void __exit alsa_card_gusclassic_exit(void)
264{
Takashi Iwai654aa662005-11-17 17:13:43 +0100265 platform_driver_unregister(&snd_gusclassic_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268module_init(alsa_card_gusclassic_init)
269module_exit(alsa_card_gusclassic_exit)