ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <sound/core.h>
31 #include <sound/sb.h>
32 #include <sound/ad1848.h>
33 #include <sound/control.h>
34 #define SNDRV_LEGACY_FIND_FREE_IRQ
35 #define SNDRV_LEGACY_FIND_FREE_DMA
36 #define SNDRV_GET_ID
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
53 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
54 MODULE_PARM_DESC(index, "Index value for Sound Galaxy soundcard.");
55 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
56 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
57 MODULE_PARM_DESC(id, "ID string for Sound Galaxy soundcard.");
58 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
59 MODULE_PARM(sbport, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
60 MODULE_PARM_DESC(sbport, "Port # for Sound Galaxy SB driver.");
61 MODULE_PARM_SYNTAX(sbport, SNDRV_ENABLED ",allows:{{0x220},{0x240}},dialog:list");
62 MODULE_PARM(wssport, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
63 MODULE_PARM_DESC(wssport, "Port # for Sound Galaxy WSS driver.");
64 MODULE_PARM_SYNTAX(wssport, SNDRV_ENABLED ",allows:{{0x530},{0xe80},{0xf40},{0x604}},dialog:list");
65 MODULE_PARM(irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
66 MODULE_PARM_DESC(irq, "IRQ # for Sound Galaxy driver.");
67 MODULE_PARM_SYNTAX(irq, SNDRV_ENABLED ",allows:{{7},{9},{10},{11}},dialog:list");
68 MODULE_PARM(dma1, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
69 MODULE_PARM_DESC(dma1, "DMA1 # for Sound Galaxy driver.");
70 MODULE_PARM_SYNTAX(dma1, SNDRV_DMA8_DESC);
71
72 #define SGALAXY_AUXC_LEFT 18
73 #define SGALAXY_AUXC_RIGHT 19
74
75 static snd_card_t *snd_sgalaxy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
76
77 /*
78
79  */
80
81 #define AD1848P1( port, x ) ( port + c_d_c_AD1848##x )
82
83 /* from lowlevel/sb/sb.c - to avoid having to allocate a sb_t for the */
84 /* short time we actually need it.. */
85
86 static int snd_sgalaxy_sbdsp_reset(unsigned long port)
87 {
88         int i;
89
90         outb(1, SBP1(port, RESET));
91         udelay(10);
92         outb(0, SBP1(port, RESET));
93         udelay(30);
94         for (i = 0; i < 1000 && !(inb(SBP1(port, DATA_AVAIL)) & 0x80); i++);
95         if (inb(SBP1(port, READ)) != 0xaa) {
96                 snd_printd("sb_reset: failed at 0x%lx!!!\n", port);
97                 return -ENODEV;
98         }
99         return 0;
100 }
101
102 static int __init snd_sgalaxy_sbdsp_command(unsigned long port, unsigned char val)
103 {
104         int i;
105         
106         for (i = 10000; i; i--)
107                 if ((inb(SBP1(port, STATUS)) & 0x80) == 0) {
108                         outb(val, SBP1(port, COMMAND));
109                         return 1;
110                 }
111
112         return 0;
113 }
114
115 static irqreturn_t snd_sgalaxy_dummy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
116 {
117         return IRQ_NONE;
118 }
119
120 static int __init snd_sgalaxy_setup_wss(unsigned long port, int irq, int dma)
121 {
122         static int interrupt_bits[] = {-1, -1, -1, -1, -1, -1, -1, 0x08, -1, 
123                                        0x10, 0x18, 0x20, -1, -1, -1, -1};
124         static int dma_bits[] = {1, 2, 0, 3};
125         int tmp, tmp1;
126
127         if ((tmp = inb(port + 3)) == 0xff)
128         {
129                 snd_printdd("I/O address dead (0x%lx)\n", port);
130                 return 0;
131         }
132 #if 0
133         snd_printdd("WSS signature = 0x%x\n", tmp);
134 #endif
135
136         if ((tmp & 0x3f) != 0x04 &&
137             (tmp & 0x3f) != 0x0f &&
138             (tmp & 0x3f) != 0x00) {
139                 snd_printdd("No WSS signature detected on port 0x%lx\n",
140                             port + 3);
141                 return 0;
142         }
143
144 #if 0
145         snd_printdd("sgalaxy - setting up IRQ/DMA for WSS\n");
146 #endif
147
148         /* initialize IRQ for WSS codec */
149         tmp = interrupt_bits[irq % 16];
150         if (tmp < 0)
151                 return -EINVAL;
152
153         if (request_irq(irq, snd_sgalaxy_dummy_interrupt, SA_INTERRUPT, "sgalaxy", NULL)) {
154                 snd_printk(KERN_ERR "sgalaxy: can't grab irq %d\n", irq);
155                 return -EIO;
156         }
157
158         outb(tmp | 0x40, port);
159         tmp1 = dma_bits[dma % 4];
160         outb(tmp | tmp1, port);
161
162         free_irq(irq, NULL);
163
164         return 0;
165 }
166
167 static int __init snd_sgalaxy_detect(int dev, int irq, int dma)
168 {
169 #if 0
170         snd_printdd("sgalaxy - switching to WSS mode\n");
171 #endif
172
173         /* switch to WSS mode */
174         snd_sgalaxy_sbdsp_reset(sbport[dev]);
175
176         snd_sgalaxy_sbdsp_command(sbport[dev], 9);
177         snd_sgalaxy_sbdsp_command(sbport[dev], 0);
178
179         udelay(400);
180         return snd_sgalaxy_setup_wss(wssport[dev], irq, dma);
181 }
182
183 static struct ad1848_mix_elem snd_sgalaxy_controls[] = {
184 AD1848_DOUBLE("Aux Playback Switch", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 7, 7, 1, 1),
185 AD1848_DOUBLE("Aux Playback Volume", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 0, 0, 31, 0)
186 };
187
188 static int __init snd_sgalaxy_mixer(ad1848_t *chip)
189 {
190         snd_card_t *card = chip->card;
191         snd_ctl_elem_id_t id1, id2;
192         unsigned int idx;
193         int err;
194
195         memset(&id1, 0, sizeof(id1));
196         memset(&id2, 0, sizeof(id2));
197         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
198         /* reassign AUX0 to LINE */
199         strcpy(id1.name, "Aux Playback Switch");
200         strcpy(id2.name, "Line Playback Switch");
201         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
202                 return err;
203         strcpy(id1.name, "Aux Playback Volume");
204         strcpy(id2.name, "Line Playback Volume");
205         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
206                 return err;
207         /* reassign AUX1 to FM */
208         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
209         strcpy(id2.name, "FM Playback Switch");
210         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
211                 return err;
212         strcpy(id1.name, "Aux Playback Volume");
213         strcpy(id2.name, "FM Playback Volume");
214         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
215                 return err;
216         /* build AUX2 input */
217         for (idx = 0; idx < ARRAY_SIZE(snd_sgalaxy_controls); idx++) {
218                 if ((err = snd_ad1848_add_ctl_elem(chip, &snd_sgalaxy_controls[idx])) < 0)
219                         return err;
220         }
221         return 0;
222 }
223
224 static int __init snd_sgalaxy_probe(int dev)
225 {
226         static int possible_irqs[] = {7, 9, 10, 11, -1};
227         static int possible_dmas[] = {1, 3, 0, -1};
228         int err, xirq, xdma1;
229         snd_card_t *card;
230         ad1848_t *chip;
231
232         if (sbport[dev] == SNDRV_AUTO_PORT) {
233                 snd_printk("specify SB port\n");
234                 return -EINVAL;
235         }
236         if (wssport[dev] == SNDRV_AUTO_PORT) {
237                 snd_printk("specify WSS port\n");
238                 return -EINVAL;
239         }
240         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
241         if (card == NULL)
242                 return -ENOMEM;
243
244         xirq = irq[dev];
245         if (xirq == SNDRV_AUTO_IRQ) {
246                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
247                         snd_card_free(card);
248                         snd_printk("unable to find a free IRQ\n");
249                         return -EBUSY;
250                 }
251         }
252         xdma1 = dma1[dev];
253         if (xdma1 == SNDRV_AUTO_DMA) {
254                 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
255                         snd_card_free(card);
256                         snd_printk("unable to find a free DMA\n");
257                         return -EBUSY;
258                 }
259         }
260
261         if ((err = snd_sgalaxy_detect(dev, xirq, xdma1)) < 0) {
262                 snd_card_free(card);
263                 return err;
264         }
265
266         if ((err = snd_ad1848_create(card, wssport[dev] + 4,
267                                      xirq, xdma1,
268                                      AD1848_HW_DETECT, &chip)) < 0) {
269                 snd_card_free(card);
270                 return err;
271         }
272
273         if ((err = snd_ad1848_pcm(chip, 0, NULL)) < 0) {
274                 snd_printdd("sgalaxy - error creating new ad1848 PCM device\n");
275                 snd_card_free(card);
276                 return err;
277         }
278         if ((err = snd_ad1848_mixer(chip)) < 0) {
279                 snd_printdd("sgalaxy - error creating new ad1848 mixer\n");
280                 snd_card_free(card);
281                 return err;
282         }
283         if (snd_sgalaxy_mixer(chip) < 0) {
284                 snd_printdd("sgalaxy - the mixer rewrite failed\n");
285                 snd_card_free(card);
286                 return err;
287         }
288
289         strcpy(card->driver, "Sound Galaxy");
290         strcpy(card->shortname, "Sound Galaxy");
291         sprintf(card->longname, "Sound Galaxy at 0x%lx, irq %d, dma %d",
292                 wssport[dev], xirq, xdma1);
293
294         if ((err = snd_card_register(card)) < 0) {
295                 snd_card_free(card);
296                 return err;
297         }
298         snd_sgalaxy_cards[dev] = card;
299         return 0;
300 }
301
302 static int __init alsa_card_sgalaxy_init(void)
303 {
304         int dev, cards;
305
306         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
307                 if (snd_sgalaxy_probe(dev) >= 0)
308                         cards++;
309         }
310         if (!cards) {
311 #ifdef MODULE
312                 snd_printk(KERN_ERR "Sound Galaxy soundcard not found or device busy\n");
313 #endif
314                 return -ENODEV;
315         }
316
317         return 0;
318 }
319
320 static void __exit alsa_card_sgalaxy_exit(void)
321 {
322         int idx;
323
324         for (idx = 0; idx < SNDRV_CARDS; idx++)
325                 snd_card_free(snd_sgalaxy_cards[idx]);
326 }
327
328 module_init(alsa_card_sgalaxy_init)
329 module_exit(alsa_card_sgalaxy_exit)
330
331 #ifndef MODULE
332
333 /* format is: snd-sgalaxy=enable,index,id,
334                           sbport,wssport,
335                           irq,dma1 */
336
337 static int __init alsa_card_sgalaxy_setup(char *str)
338 {
339         static unsigned __initdata nr_dev = 0;
340
341         if (nr_dev >= SNDRV_CARDS)
342                 return 0;
343         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
344                get_option(&str,&index[nr_dev]) == 2 &&
345                get_id(&str,&id[nr_dev]) == 2 &&
346                get_option_long(&str,&sbport[nr_dev]) == 2 &&
347                get_option_long(&str,&wssport[nr_dev]) == 2 &&
348                get_option(&str,&irq[nr_dev]) == 2 &&
349                get_option(&str,&dma1[nr_dev]) == 2);
350         nr_dev++;
351         return 1;
352 }
353
354 __setup("snd-sgalaxy=", alsa_card_sgalaxy_setup);
355
356 #endif /* ifndef MODULE */