patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / isa / sgalaxy.c
1 /*
2  *  Driver for Aztech Sound Galaxy cards
3  *  Copyright (c) by Christopher Butler <chrisb@sandy.force9.co.uk.
4  *
5  *  I don't have documentation for this card, I based this driver on the
6  *  driver for OSS/Free included in the kernel source (drivers/sound/sgalaxy.c)
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */
23
24 #include <sound/driver.h>
25 #include <asm/dma.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/time.h>
29 #include <linux/irq.h>
30 #include <linux/moduleparam.h>
31 #include <sound/core.h>
32 #include <sound/sb.h>
33 #include <sound/ad1848.h>
34 #include <sound/control.h>
35 #define SNDRV_LEGACY_FIND_FREE_IRQ
36 #define SNDRV_LEGACY_FIND_FREE_DMA
37 #include <sound/initval.h>
38
39 MODULE_AUTHOR("Christopher Butler <chrisb@sandy.force9.co.uk>");
40 MODULE_DESCRIPTION("Aztech Sound Galaxy");
41 MODULE_LICENSE("GPL");
42 MODULE_CLASSES("{sound}");
43 MODULE_DEVICES("{{Aztech Systems,Sound Galaxy}}");
44
45 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
46 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
47 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;  /* Enable this card */
48 static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;   /* 0x220,0x240 */
49 static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* 0x530,0xe80,0xf40,0x604 */
50 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 7,9,10,11 */
51 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
52 static int boot_devs;
53
54 module_param_array(index, int, boot_devs, 0444);
55 MODULE_PARM_DESC(index, "Index value for Sound Galaxy soundcard.");
56 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
57 module_param_array(id, charp, boot_devs, 0444);
58 MODULE_PARM_DESC(id, "ID string for Sound Galaxy soundcard.");
59 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
60 module_param_array(sbport, long, boot_devs, 0444);
61 MODULE_PARM_DESC(sbport, "Port # for Sound Galaxy SB driver.");
62 MODULE_PARM_SYNTAX(sbport, SNDRV_ENABLED ",allows:{{0x220},{0x240}},dialog:list");
63 module_param_array(wssport, long, boot_devs, 0444);
64 MODULE_PARM_DESC(wssport, "Port # for Sound Galaxy WSS driver.");
65 MODULE_PARM_SYNTAX(wssport, SNDRV_ENABLED ",allows:{{0x530},{0xe80},{0xf40},{0x604}},dialog:list");
66 module_param_array(irq, int, boot_devs, 0444);
67 MODULE_PARM_DESC(irq, "IRQ # for Sound Galaxy driver.");
68 MODULE_PARM_SYNTAX(irq, SNDRV_ENABLED ",allows:{{7},{9},{10},{11}},dialog:list");
69 module_param_array(dma1, int, boot_devs, 0444);
70 MODULE_PARM_DESC(dma1, "DMA1 # for Sound Galaxy driver.");
71 MODULE_PARM_SYNTAX(dma1, SNDRV_DMA8_DESC);
72
73 #define SGALAXY_AUXC_LEFT 18
74 #define SGALAXY_AUXC_RIGHT 19
75
76 static snd_card_t *snd_sgalaxy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
77
78 /*
79
80  */
81
82 #define AD1848P1( port, x ) ( port + c_d_c_AD1848##x )
83
84 /* from lowlevel/sb/sb.c - to avoid having to allocate a sb_t for the */
85 /* short time we actually need it.. */
86
87 static int snd_sgalaxy_sbdsp_reset(unsigned long port)
88 {
89         int i;
90
91         outb(1, SBP1(port, RESET));
92         udelay(10);
93         outb(0, SBP1(port, RESET));
94         udelay(30);
95         for (i = 0; i < 1000 && !(inb(SBP1(port, DATA_AVAIL)) & 0x80); i++);
96         if (inb(SBP1(port, READ)) != 0xaa) {
97                 snd_printd("sb_reset: failed at 0x%lx!!!\n", port);
98                 return -ENODEV;
99         }
100         return 0;
101 }
102
103 static int __init snd_sgalaxy_sbdsp_command(unsigned long port, unsigned char val)
104 {
105         int i;
106         
107         for (i = 10000; i; i--)
108                 if ((inb(SBP1(port, STATUS)) & 0x80) == 0) {
109                         outb(val, SBP1(port, COMMAND));
110                         return 1;
111                 }
112
113         return 0;
114 }
115
116 static irqreturn_t snd_sgalaxy_dummy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
117 {
118         return IRQ_NONE;
119 }
120
121 static int __init snd_sgalaxy_setup_wss(unsigned long port, int irq, int dma)
122 {
123         static int interrupt_bits[] = {-1, -1, -1, -1, -1, -1, -1, 0x08, -1, 
124                                        0x10, 0x18, 0x20, -1, -1, -1, -1};
125         static int dma_bits[] = {1, 2, 0, 3};
126         int tmp, tmp1;
127
128         if ((tmp = inb(port + 3)) == 0xff)
129         {
130                 snd_printdd("I/O address dead (0x%lx)\n", port);
131                 return 0;
132         }
133 #if 0
134         snd_printdd("WSS signature = 0x%x\n", tmp);
135 #endif
136
137         if ((tmp & 0x3f) != 0x04 &&
138             (tmp & 0x3f) != 0x0f &&
139             (tmp & 0x3f) != 0x00) {
140                 snd_printdd("No WSS signature detected on port 0x%lx\n",
141                             port + 3);
142                 return 0;
143         }
144
145 #if 0
146         snd_printdd("sgalaxy - setting up IRQ/DMA for WSS\n");
147 #endif
148
149         /* initialize IRQ for WSS codec */
150         tmp = interrupt_bits[irq % 16];
151         if (tmp < 0)
152                 return -EINVAL;
153
154         if (request_irq(irq, snd_sgalaxy_dummy_interrupt, SA_INTERRUPT, "sgalaxy", NULL)) {
155                 snd_printk(KERN_ERR "sgalaxy: can't grab irq %d\n", irq);
156                 return -EIO;
157         }
158
159         outb(tmp | 0x40, port);
160         tmp1 = dma_bits[dma % 4];
161         outb(tmp | tmp1, port);
162
163         free_irq(irq, NULL);
164
165         return 0;
166 }
167
168 static int __init snd_sgalaxy_detect(int dev, int irq, int dma)
169 {
170 #if 0
171         snd_printdd("sgalaxy - switching to WSS mode\n");
172 #endif
173
174         /* switch to WSS mode */
175         snd_sgalaxy_sbdsp_reset(sbport[dev]);
176
177         snd_sgalaxy_sbdsp_command(sbport[dev], 9);
178         snd_sgalaxy_sbdsp_command(sbport[dev], 0);
179
180         udelay(400);
181         return snd_sgalaxy_setup_wss(wssport[dev], irq, dma);
182 }
183
184 static struct ad1848_mix_elem snd_sgalaxy_controls[] = {
185 AD1848_DOUBLE("Aux Playback Switch", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 7, 7, 1, 1),
186 AD1848_DOUBLE("Aux Playback Volume", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 0, 0, 31, 0)
187 };
188
189 static int __init snd_sgalaxy_mixer(ad1848_t *chip)
190 {
191         snd_card_t *card = chip->card;
192         snd_ctl_elem_id_t id1, id2;
193         unsigned int idx;
194         int err;
195
196         memset(&id1, 0, sizeof(id1));
197         memset(&id2, 0, sizeof(id2));
198         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
199         /* reassign AUX0 to LINE */
200         strcpy(id1.name, "Aux Playback Switch");
201         strcpy(id2.name, "Line Playback Switch");
202         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
203                 return err;
204         strcpy(id1.name, "Aux Playback Volume");
205         strcpy(id2.name, "Line Playback Volume");
206         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
207                 return err;
208         /* reassign AUX1 to FM */
209         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
210         strcpy(id2.name, "FM Playback Switch");
211         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
212                 return err;
213         strcpy(id1.name, "Aux Playback Volume");
214         strcpy(id2.name, "FM Playback Volume");
215         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
216                 return err;
217         /* build AUX2 input */
218         for (idx = 0; idx < ARRAY_SIZE(snd_sgalaxy_controls); idx++) {
219                 if ((err = snd_ad1848_add_ctl_elem(chip, &snd_sgalaxy_controls[idx])) < 0)
220                         return err;
221         }
222         return 0;
223 }
224
225 static int __init snd_sgalaxy_probe(int dev)
226 {
227         static int possible_irqs[] = {7, 9, 10, 11, -1};
228         static int possible_dmas[] = {1, 3, 0, -1};
229         int err, xirq, xdma1;
230         snd_card_t *card;
231         ad1848_t *chip;
232
233         if (sbport[dev] == SNDRV_AUTO_PORT) {
234                 snd_printk("specify SB port\n");
235                 return -EINVAL;
236         }
237         if (wssport[dev] == SNDRV_AUTO_PORT) {
238                 snd_printk("specify WSS port\n");
239                 return -EINVAL;
240         }
241         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
242         if (card == NULL)
243                 return -ENOMEM;
244
245         xirq = irq[dev];
246         if (xirq == SNDRV_AUTO_IRQ) {
247                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
248                         snd_card_free(card);
249                         snd_printk("unable to find a free IRQ\n");
250                         return -EBUSY;
251                 }
252         }
253         xdma1 = dma1[dev];
254         if (xdma1 == SNDRV_AUTO_DMA) {
255                 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
256                         snd_card_free(card);
257                         snd_printk("unable to find a free DMA\n");
258                         return -EBUSY;
259                 }
260         }
261
262         if ((err = snd_sgalaxy_detect(dev, xirq, xdma1)) < 0) {
263                 snd_card_free(card);
264                 return err;
265         }
266
267         if ((err = snd_ad1848_create(card, wssport[dev] + 4,
268                                      xirq, xdma1,
269                                      AD1848_HW_DETECT, &chip)) < 0) {
270                 snd_card_free(card);
271                 return err;
272         }
273
274         if ((err = snd_ad1848_pcm(chip, 0, NULL)) < 0) {
275                 snd_printdd("sgalaxy - error creating new ad1848 PCM device\n");
276                 snd_card_free(card);
277                 return err;
278         }
279         if ((err = snd_ad1848_mixer(chip)) < 0) {
280                 snd_printdd("sgalaxy - error creating new ad1848 mixer\n");
281                 snd_card_free(card);
282                 return err;
283         }
284         if (snd_sgalaxy_mixer(chip) < 0) {
285                 snd_printdd("sgalaxy - the mixer rewrite failed\n");
286                 snd_card_free(card);
287                 return err;
288         }
289
290         strcpy(card->driver, "Sound Galaxy");
291         strcpy(card->shortname, "Sound Galaxy");
292         sprintf(card->longname, "Sound Galaxy at 0x%lx, irq %d, dma %d",
293                 wssport[dev], xirq, xdma1);
294
295         if ((err = snd_card_register(card)) < 0) {
296                 snd_card_free(card);
297                 return err;
298         }
299         snd_sgalaxy_cards[dev] = card;
300         return 0;
301 }
302
303 static int __init alsa_card_sgalaxy_init(void)
304 {
305         int dev, cards;
306
307         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
308                 if (snd_sgalaxy_probe(dev) >= 0)
309                         cards++;
310         }
311         if (!cards) {
312 #ifdef MODULE
313                 snd_printk(KERN_ERR "Sound Galaxy soundcard not found or device busy\n");
314 #endif
315                 return -ENODEV;
316         }
317
318         return 0;
319 }
320
321 static void __exit alsa_card_sgalaxy_exit(void)
322 {
323         int idx;
324
325         for (idx = 0; idx < SNDRV_CARDS; idx++)
326                 snd_card_free(snd_sgalaxy_cards[idx]);
327 }
328
329 module_init(alsa_card_sgalaxy_init)
330 module_exit(alsa_card_sgalaxy_exit)