ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / es1688 / es1688.c
1 /*
2  *  Driver for generic ESS AudioDrive ESx688 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 <asm/dma.h>
24 #include <linux/init.h>
25 #include <linux/time.h>
26 #include <linux/wait.h>
27 #include <sound/core.h>
28 #include <sound/es1688.h>
29 #include <sound/mpu401.h>
30 #include <sound/opl3.h>
31 #define SNDRV_LEGACY_AUTO_PROBE
32 #define SNDRV_LEGACY_FIND_FREE_IRQ
33 #define SNDRV_LEGACY_FIND_FREE_DMA
34 #define SNDRV_GET_ID
35 #include <sound/initval.h>
36
37 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
38 MODULE_DESCRIPTION("ESS ESx688 AudioDrive");
39 MODULE_LICENSE("GPL");
40 MODULE_CLASSES("{sound}");
41 MODULE_DEVICES("{{ESS,ES688 PnP AudioDrive,pnp:ESS0100},"
42                 "{ESS,ES1688 PnP AudioDrive,pnp:ESS0102},"
43                 "{ESS,ES688 AudioDrive,pnp:ESS6881},"
44                 "{ESS,ES1688 AudioDrive,pnp:ESS1681}}");
45
46 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
47 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
48 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;  /* Enable this card */
49 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0x220,0x240,0x260 */
50 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
51 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 5,7,9,10 */
52 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* 5,7,9,10 */
53 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
54
55 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
56 MODULE_PARM_DESC(index, "Index value for ESx688 soundcard.");
57 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
58 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
59 MODULE_PARM_DESC(id, "ID string for ESx688 soundcard.");
60 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
61 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
62 MODULE_PARM_DESC(enable, "Enable ESx688 soundcard.");
63 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
64 MODULE_PARM(port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
65 MODULE_PARM_DESC(port, "Port # for ESx688 driver.");
66 MODULE_PARM_SYNTAX(port, SNDRV_PORT12_DESC);
67 MODULE_PARM(mpu_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
68 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ESx688 driver.");
69 MODULE_PARM_SYNTAX(mpu_port, SNDRV_PORT12_DESC);
70 MODULE_PARM(irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
71 MODULE_PARM_DESC(irq, "IRQ # for ESx688 driver.");
72 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
73 MODULE_PARM(mpu_irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
74 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for ESx688 driver.");
75 MODULE_PARM_SYNTAX(mpu_irq, SNDRV_IRQ_DESC);
76 MODULE_PARM(dma8, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
77 MODULE_PARM_DESC(dma8, "8-bit DMA # for ESx688 driver.");
78 MODULE_PARM_SYNTAX(dma8, SNDRV_DMA8_DESC);
79
80 static snd_card_t *snd_audiodrive_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
81
82
83 static int __init snd_audiodrive_probe(int dev)
84 {
85         static int possible_irqs[] = {5, 9, 10, 7, -1};
86         static int possible_dmas[] = {1, 3, 0, -1};
87         int xirq, xdma, xmpu_irq;
88         snd_card_t *card;
89         es1688_t *chip;
90         opl3_t *opl3;
91         snd_pcm_t *pcm;
92         int err;
93
94         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
95         if (card == NULL)
96                 return -ENOMEM;
97
98         xirq = irq[dev];
99         if (xirq == SNDRV_AUTO_IRQ) {
100                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
101                         snd_card_free(card);
102                         snd_printk("unable to find a free IRQ\n");
103                         return -EBUSY;
104                 }
105         }
106         xmpu_irq = mpu_irq[dev];
107         xdma = dma8[dev];
108         if (xdma == SNDRV_AUTO_DMA) {
109                 if ((xdma = snd_legacy_find_free_dma(possible_dmas)) < 0) {
110                         snd_card_free(card);
111                         snd_printk("unable to find a free DMA\n");
112                         return -EBUSY;
113                 }
114         }
115
116         if ((err = snd_es1688_create(card, port[dev], mpu_port[dev],
117                                      xirq, xmpu_irq, xdma,
118                                      ES1688_HW_AUTO, &chip)) < 0) {
119                 snd_card_free(card);
120                 return err;
121         }
122         if ((err = snd_es1688_pcm(chip, 0, &pcm)) < 0) {
123                 snd_card_free(card);
124                 return err;
125         }
126         if ((err = snd_es1688_mixer(chip)) < 0) {
127                 snd_card_free(card);
128                 return err;
129         }
130
131         strcpy(card->driver, "ES1688");
132         strcpy(card->shortname, pcm->name);
133         sprintf(card->longname, "%s at 0x%lx, irq %i, dma %i", pcm->name, chip->port, xirq, xdma);
134
135         if ((snd_opl3_create(card, chip->port, chip->port + 2, OPL3_HW_OPL3, 0, &opl3)) < 0) {
136                 printk(KERN_ERR "es1688: opl3 not detected at 0x%lx\n", chip->port);
137         } else {
138                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
139                         snd_card_free(card);
140                         return err;
141                 }
142         }
143
144         if (xmpu_irq >= 0 && xmpu_irq != SNDRV_AUTO_IRQ && chip->mpu_port > 0) {
145                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
146                                                chip->mpu_port, 0,
147                                                xmpu_irq,
148                                                SA_INTERRUPT,
149                                                NULL)) < 0) {
150                         snd_card_free(card);
151                         return err;
152                 }
153         }
154         if ((err = snd_card_register(card)) < 0) {
155                 snd_card_free(card);
156                 return err;
157         }
158         snd_audiodrive_cards[dev] = card;
159         return 0;
160
161 }
162
163 static int __init snd_audiodrive_legacy_auto_probe(unsigned long xport)
164 {
165         static int dev;
166         int res;
167         
168         for ( ; dev < SNDRV_CARDS; dev++) {
169                 if (!enable[dev] || port[dev] != SNDRV_AUTO_PORT)
170                         continue;
171                 port[dev] = xport;
172                 res = snd_audiodrive_probe(dev);
173                 if (res < 0)
174                         port[dev] = SNDRV_AUTO_PORT;
175                 return res;
176         }
177         return -ENODEV;
178 }
179
180 static int __init alsa_card_es1688_init(void)
181 {
182         static unsigned long possible_ports[] = {0x220, 0x240, 0x260, -1};
183         int dev, cards = 0, i;
184
185         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
186                 if (port[dev] == SNDRV_AUTO_PORT)
187                         continue;
188                 if (snd_audiodrive_probe(dev) >= 0)
189                         cards++;
190         }
191         i = snd_legacy_auto_probe(possible_ports, snd_audiodrive_legacy_auto_probe);
192         if (i > 0)
193                 cards += i;
194
195         if (!cards) {
196 #ifdef MODULE
197                 printk(KERN_ERR "ESS AudioDrive ES1688 soundcard not found or device busy\n");
198 #endif
199                 return -ENODEV;
200         }
201         return 0;
202 }
203
204 static void __exit alsa_card_es1688_exit(void)
205 {
206         int idx;
207
208         for (idx = 0; idx < SNDRV_CARDS; idx++)
209                 snd_card_free(snd_audiodrive_cards[idx]);
210 }
211
212 module_init(alsa_card_es1688_init)
213 module_exit(alsa_card_es1688_exit)
214
215 #ifndef MODULE
216
217 /* format is: snd-es1688=enable,index,id,
218                          port,mpu_port,
219                          irq,mpu_irq,
220                          dma8 */
221
222 static int __init alsa_card_es1688_setup(char *str)
223 {
224         static unsigned __initdata nr_dev = 0;
225
226         if (nr_dev >= SNDRV_CARDS)
227                 return 0;
228         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
229                get_option(&str,&index[nr_dev]) == 2 &&
230                get_id(&str,&id[nr_dev]) == 2 &&
231                get_option_long(&str,&port[nr_dev]) == 2 &&
232                get_option_long(&str,&mpu_port[nr_dev]) == 2 &&
233                get_option(&str,&irq[nr_dev]) == 2 &&
234                get_option(&str,&mpu_irq[nr_dev]) == 2 &&
235                get_option(&str,&dma8[nr_dev]) == 2);
236         nr_dev++;
237         return 1;
238 }
239
240 __setup("snd-es1688=", alsa_card_es1688_setup);
241
242 #endif /* ifndef MODULE */