vserver 1.9.3
[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 <linux/moduleparam.h>
28 #include <sound/core.h>
29 #include <sound/es1688.h>
30 #include <sound/mpu401.h>
31 #include <sound/opl3.h>
32 #define SNDRV_LEGACY_AUTO_PROBE
33 #define SNDRV_LEGACY_FIND_FREE_IRQ
34 #define SNDRV_LEGACY_FIND_FREE_DMA
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_SUPPORTED_DEVICE("{{ESS,ES688 PnP AudioDrive,pnp:ESS0100},"
41                 "{ESS,ES1688 PnP AudioDrive,pnp:ESS0102},"
42                 "{ESS,ES688 AudioDrive,pnp:ESS6881},"
43                 "{ESS,ES1688 AudioDrive,pnp:ESS1681}}");
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 port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0x220,0x240,0x260 */
49 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
50 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 5,7,9,10 */
51 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* 5,7,9,10 */
52 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
53 static int boot_devs;
54
55 module_param_array(index, int, boot_devs, 0444);
56 MODULE_PARM_DESC(index, "Index value for ESx688 soundcard.");
57 module_param_array(id, charp, boot_devs, 0444);
58 MODULE_PARM_DESC(id, "ID string for ESx688 soundcard.");
59 module_param_array(enable, bool, boot_devs, 0444);
60 MODULE_PARM_DESC(enable, "Enable ESx688 soundcard.");
61 module_param_array(port, long, boot_devs, 0444);
62 MODULE_PARM_DESC(port, "Port # for ESx688 driver.");
63 module_param_array(mpu_port, long, boot_devs, 0444);
64 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ESx688 driver.");
65 module_param_array(irq, int, boot_devs, 0444);
66 MODULE_PARM_DESC(irq, "IRQ # for ESx688 driver.");
67 module_param_array(mpu_irq, int, boot_devs, 0444);
68 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for ESx688 driver.");
69 module_param_array(dma8, int, boot_devs, 0444);
70 MODULE_PARM_DESC(dma8, "8-bit DMA # for ESx688 driver.");
71
72 static snd_card_t *snd_audiodrive_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
73
74
75 static int __init snd_audiodrive_probe(int dev)
76 {
77         static int possible_irqs[] = {5, 9, 10, 7, -1};
78         static int possible_dmas[] = {1, 3, 0, -1};
79         int xirq, xdma, xmpu_irq;
80         snd_card_t *card;
81         es1688_t *chip;
82         opl3_t *opl3;
83         snd_pcm_t *pcm;
84         int err;
85
86         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
87         if (card == NULL)
88                 return -ENOMEM;
89
90         xirq = irq[dev];
91         if (xirq == SNDRV_AUTO_IRQ) {
92                 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
93                         snd_card_free(card);
94                         snd_printk("unable to find a free IRQ\n");
95                         return -EBUSY;
96                 }
97         }
98         xmpu_irq = mpu_irq[dev];
99         xdma = dma8[dev];
100         if (xdma == SNDRV_AUTO_DMA) {
101                 if ((xdma = snd_legacy_find_free_dma(possible_dmas)) < 0) {
102                         snd_card_free(card);
103                         snd_printk("unable to find a free DMA\n");
104                         return -EBUSY;
105                 }
106         }
107
108         if ((err = snd_es1688_create(card, port[dev], mpu_port[dev],
109                                      xirq, xmpu_irq, xdma,
110                                      ES1688_HW_AUTO, &chip)) < 0) {
111                 snd_card_free(card);
112                 return err;
113         }
114         if ((err = snd_es1688_pcm(chip, 0, &pcm)) < 0) {
115                 snd_card_free(card);
116                 return err;
117         }
118         if ((err = snd_es1688_mixer(chip)) < 0) {
119                 snd_card_free(card);
120                 return err;
121         }
122
123         strcpy(card->driver, "ES1688");
124         strcpy(card->shortname, pcm->name);
125         sprintf(card->longname, "%s at 0x%lx, irq %i, dma %i", pcm->name, chip->port, xirq, xdma);
126
127         if ((snd_opl3_create(card, chip->port, chip->port + 2, OPL3_HW_OPL3, 0, &opl3)) < 0) {
128                 printk(KERN_ERR "es1688: opl3 not detected at 0x%lx\n", chip->port);
129         } else {
130                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
131                         snd_card_free(card);
132                         return err;
133                 }
134         }
135
136         if (xmpu_irq >= 0 && xmpu_irq != SNDRV_AUTO_IRQ && chip->mpu_port > 0) {
137                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
138                                                chip->mpu_port, 0,
139                                                xmpu_irq,
140                                                SA_INTERRUPT,
141                                                NULL)) < 0) {
142                         snd_card_free(card);
143                         return err;
144                 }
145         }
146         if ((err = snd_card_register(card)) < 0) {
147                 snd_card_free(card);
148                 return err;
149         }
150         snd_audiodrive_cards[dev] = card;
151         return 0;
152
153 }
154
155 static int __init snd_audiodrive_legacy_auto_probe(unsigned long xport)
156 {
157         static int dev;
158         int res;
159         
160         for ( ; dev < SNDRV_CARDS; dev++) {
161                 if (!enable[dev] || port[dev] != SNDRV_AUTO_PORT)
162                         continue;
163                 port[dev] = xport;
164                 res = snd_audiodrive_probe(dev);
165                 if (res < 0)
166                         port[dev] = SNDRV_AUTO_PORT;
167                 return res;
168         }
169         return -ENODEV;
170 }
171
172 static int __init alsa_card_es1688_init(void)
173 {
174         static unsigned long possible_ports[] = {0x220, 0x240, 0x260, -1};
175         int dev, cards = 0, i;
176
177         for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
178                 if (port[dev] == SNDRV_AUTO_PORT)
179                         continue;
180                 if (snd_audiodrive_probe(dev) >= 0)
181                         cards++;
182         }
183         i = snd_legacy_auto_probe(possible_ports, snd_audiodrive_legacy_auto_probe);
184         if (i > 0)
185                 cards += i;
186
187         if (!cards) {
188 #ifdef MODULE
189                 printk(KERN_ERR "ESS AudioDrive ES1688 soundcard not found or device busy\n");
190 #endif
191                 return -ENODEV;
192         }
193         return 0;
194 }
195
196 static void __exit alsa_card_es1688_exit(void)
197 {
198         int idx;
199
200         for (idx = 0; idx < SNDRV_CARDS; idx++)
201                 snd_card_free(snd_audiodrive_cards[idx]);
202 }
203
204 module_init(alsa_card_es1688_init)
205 module_exit(alsa_card_es1688_exit)