vserver 1.9.5.x5
[linux-2.6.git] / sound / isa / ad1816a / ad1816a.c
1
2 /*
3     card-ad1816a.c - driver for ADI SoundPort AD1816A based soundcards.
4     Copyright (C) 2000 by Massimo Piccioni <dafastidio@libero.it>
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 #include <sound/driver.h>
22 #include <linux/init.h>
23 #include <linux/time.h>
24 #include <linux/wait.h>
25 #include <linux/pnp.h>
26 #include <linux/moduleparam.h>
27 #include <sound/core.h>
28 #include <sound/initval.h>
29 #include <sound/ad1816a.h>
30 #include <sound/mpu401.h>
31 #include <sound/opl3.h>
32
33 #define PFX "ad1816a: "
34
35 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
36 MODULE_DESCRIPTION("AD1816A, AD1815");
37 MODULE_LICENSE("GPL");
38 MODULE_SUPPORTED_DEVICE("{{Highscreen,Sound-Boostar 16 3D},"
39                 "{Analog Devices,AD1815},"
40                 "{Analog Devices,AD1816A},"
41                 "{TerraTec,Base 64},"
42                 "{TerraTec,AudioSystem EWS64S},"
43                 "{Aztech/Newcom SC-16 3D},"
44                 "{Shark Predator ISA}}");
45
46 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 1-MAX */
47 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
48 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;   /* Enable this card */
49 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* PnP setup */
50 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
51 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* PnP setup */
52 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* Pnp setup */
53 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* Pnp setup */
54 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* PnP setup */
55 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* PnP setup */
56
57 module_param_array(index, int, NULL, 0444);
58 MODULE_PARM_DESC(index, "Index value for ad1816a based soundcard.");
59 module_param_array(id, charp, NULL, 0444);
60 MODULE_PARM_DESC(id, "ID string for ad1816a based soundcard.");
61 module_param_array(enable, bool, NULL, 0444);
62 MODULE_PARM_DESC(enable, "Enable ad1816a based soundcard.");
63 module_param_array(port, long, NULL, 0444);
64 MODULE_PARM_DESC(port, "Port # for ad1816a driver.");
65 module_param_array(mpu_port, long, NULL, 0444);
66 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ad1816a driver.");
67 module_param_array(fm_port, long, NULL, 0444);
68 MODULE_PARM_DESC(fm_port, "FM port # for ad1816a driver.");
69 module_param_array(irq, int, NULL, 0444);
70 MODULE_PARM_DESC(irq, "IRQ # for ad1816a driver.");
71 module_param_array(mpu_irq, int, NULL, 0444);
72 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for ad1816a driver.");
73 module_param_array(dma1, int, NULL, 0444);
74 MODULE_PARM_DESC(dma1, "1st DMA # for ad1816a driver.");
75 module_param_array(dma2, int, NULL, 0444);
76 MODULE_PARM_DESC(dma2, "2nd DMA # for ad1816a driver.");
77
78 struct snd_card_ad1816a {
79         struct pnp_dev *dev;
80         struct pnp_dev *devmpu;
81 };
82
83 static struct pnp_card_device_id snd_ad1816a_pnpids[] = {
84         /* Analog Devices AD1815 */
85         { .id = "ADS7150", .devs = { { .id = "ADS7150" }, { .id = "ADS7151" } } },
86         /* Analog Devices AD1816A - added by Kenneth Platz <kxp@atl.hp.com> */
87         { .id = "ADS7181", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
88         /* Analog Devices AD1816A - Aztech/Newcom SC-16 3D */
89         { .id = "AZT1022", .devs = { { .id = "AZT1018" }, { .id = "AZT2002" } } },
90         /* Highscreen Sound-Boostar 16 3D - added by Stefan Behnel */
91         { .id = "LWC1061", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
92         /* Highscreen Sound-Boostar 16 3D */
93         { .id = "MDK1605", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
94         /* Shark Predator ISA - added by Ken Arromdee */
95         { .id = "SMM7180", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
96         /* Analog Devices AD1816A - Terratec AudioSystem EWS64S */
97         { .id = "TER1112", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
98         /* Analog Devices AD1816A - Terratec Base 64 */
99         { .id = "TER1411", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } },
100         /* end */
101         { .id = "" }
102 };
103
104 MODULE_DEVICE_TABLE(pnp_card, snd_ad1816a_pnpids);
105
106
107 #define DRIVER_NAME     "snd-card-ad1816a"
108
109
110 static int __devinit snd_card_ad1816a_pnp(int dev, struct snd_card_ad1816a *acard,
111                                           struct pnp_card_link *card,
112                                           const struct pnp_card_device_id *id)
113 {
114         struct pnp_dev *pdev;
115         struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
116         int err;
117
118         acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
119         if (acard->dev == NULL) {
120                 kfree(cfg);
121                 return -EBUSY;
122         }
123         acard->devmpu = pnp_request_card_device(card, id->devs[1].id, NULL);
124         if (acard->devmpu == NULL) {
125                 kfree(cfg);
126                 return -EBUSY;
127         }
128
129         pdev = acard->dev;
130         pnp_init_resource_table(cfg);
131
132         if (port[dev] != SNDRV_AUTO_PORT)
133                 pnp_resource_change(&cfg->port_resource[2], port[dev], 16);
134         if (fm_port[dev] != SNDRV_AUTO_PORT)
135                 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
136         if (dma1[dev] != SNDRV_AUTO_DMA)
137                 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
138         if (dma2[dev] != SNDRV_AUTO_DMA)
139                 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
140         if (irq[dev] != SNDRV_AUTO_IRQ)
141                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
142
143         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
144                 snd_printk(KERN_ERR PFX "AUDIO the requested resources are invalid, using auto config\n");
145         err = pnp_activate_dev(pdev);
146         if (err < 0) {
147                 printk(KERN_ERR PFX "AUDIO PnP configure failure\n");
148                 kfree(cfg);
149                 return -EBUSY;
150         }
151
152         port[dev] = pnp_port_start(pdev, 2);
153         fm_port[dev] = pnp_port_start(pdev, 1);
154         dma1[dev] = pnp_dma(pdev, 0);
155         dma2[dev] = pnp_dma(pdev, 1);
156         irq[dev] = pnp_irq(pdev, 0);
157
158         pdev = acard->devmpu;
159         pnp_init_resource_table(cfg);
160
161         if (mpu_port[dev] != SNDRV_AUTO_PORT)
162                 pnp_resource_change(&cfg->port_resource[0], mpu_port[dev], 2);
163         if (mpu_irq[dev] != SNDRV_AUTO_IRQ)
164                 pnp_resource_change(&cfg->irq_resource[0], mpu_irq[dev], 1);
165
166         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
167                 snd_printk(KERN_ERR PFX "AUDIO the requested resources are invalid, using auto config\n");
168         err = pnp_activate_dev(pdev);
169         if (err < 0) {
170                 printk(KERN_ERR PFX "MPU401 PnP configure failure\n");
171                 mpu_port[dev] = -1;
172                 acard->devmpu = NULL;
173         } else {
174                 mpu_port[dev] = pnp_port_start(pdev, 0);
175                 mpu_irq[dev] = pnp_irq(pdev, 0);
176         }
177
178         kfree(cfg);
179         return 0;
180 }
181
182 static int __devinit snd_card_ad1816a_probe(int dev, struct pnp_card_link *pcard,
183                                             const struct pnp_card_device_id *pid)
184 {
185         int error;
186         snd_card_t *card;
187         struct snd_card_ad1816a *acard;
188         ad1816a_t *chip;
189         opl3_t *opl3;
190
191         if ((card = snd_card_new(index[dev], id[dev], THIS_MODULE,
192                                  sizeof(struct snd_card_ad1816a))) == NULL)
193                 return -ENOMEM;
194         acard = (struct snd_card_ad1816a *)card->private_data;
195
196         if ((error = snd_card_ad1816a_pnp(dev, acard, pcard, pid))) {
197                 snd_card_free(card);
198                 return error;
199         }
200         snd_card_set_dev(card, &pcard->card->dev);
201
202         if ((error = snd_ad1816a_create(card, port[dev],
203                                         irq[dev],
204                                         dma1[dev],
205                                         dma2[dev],
206                                         &chip)) < 0) {
207                 snd_card_free(card);
208                 return error;
209         }
210
211         strcpy(card->driver, "AD1816A");
212         strcpy(card->shortname, "ADI SoundPort AD1816A");
213         sprintf(card->longname, "%s, SS at 0x%lx, irq %d, dma %d&%d",
214                 card->shortname, chip->port, irq[dev], dma1[dev], dma2[dev]);
215
216         if ((error = snd_ad1816a_pcm(chip, 0, NULL)) < 0) {
217                 snd_card_free(card);
218                 return error;
219         }
220
221         if ((error = snd_ad1816a_mixer(chip)) < 0) {
222                 snd_card_free(card);
223                 return error;
224         }
225
226         if (mpu_port[dev] > 0) {
227                 if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
228                                         mpu_port[dev], 0, mpu_irq[dev], SA_INTERRUPT,
229                                         NULL) < 0)
230                         printk(KERN_ERR PFX "no MPU-401 device at 0x%lx.\n", mpu_port[dev]);
231         }
232
233         if (fm_port[dev] > 0) {
234                 if (snd_opl3_create(card,
235                                     fm_port[dev], fm_port[dev] + 2,
236                                     OPL3_HW_AUTO, 0, &opl3) < 0) {
237                         printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx.\n", fm_port[dev], fm_port[dev] + 2);
238                 } else {
239                         if ((error = snd_opl3_timer_new(opl3, 1, 2)) < 0) {
240                                 snd_card_free(card);
241                                 return error;
242                         }
243                         if ((error = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
244                                 snd_card_free(card);
245                                 return error;
246                         }
247                 }
248         }
249
250         if ((error = snd_card_register(card)) < 0) {
251                 snd_card_free(card);
252                 return error;
253         }
254         pnp_set_card_drvdata(pcard, card);
255         return 0;
256 }
257
258 static int __devinit snd_ad1816a_pnp_detect(struct pnp_card_link *card,
259                                             const struct pnp_card_device_id *id)
260 {
261         static int dev;
262         int res;
263
264         for ( ; dev < SNDRV_CARDS; dev++) {
265                 if (!enable[dev])
266                         continue;
267                 res = snd_card_ad1816a_probe(dev, card, id);
268                 if (res < 0)
269                         return res;
270                 dev++;
271                 return 0;
272         }
273         return -ENODEV;
274 }
275
276 static void __devexit snd_ad1816a_pnp_remove(struct pnp_card_link * pcard)
277 {
278         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
279
280         snd_card_disconnect(card);
281         snd_card_free_in_thread(card);
282 }
283
284 static struct pnp_card_driver ad1816a_pnpc_driver = {
285         .flags          = PNP_DRIVER_RES_DISABLE,
286         .name           = "ad1816a",
287         .id_table       = snd_ad1816a_pnpids,
288         .probe          = snd_ad1816a_pnp_detect,
289         .remove         = __devexit_p(snd_ad1816a_pnp_remove),
290 };
291
292 static int __init alsa_card_ad1816a_init(void)
293 {
294         int cards = 0;
295
296         cards += pnp_register_card_driver(&ad1816a_pnpc_driver);
297 #ifdef MODULE
298         if (!cards) {
299                 pnp_unregister_card_driver(&ad1816a_pnpc_driver);
300                 printk(KERN_ERR "no AD1816A based soundcards found.\n");
301         }
302 #endif  /* MODULE */
303         return cards ? 0 : -ENODEV;
304 }
305
306 static void __exit alsa_card_ad1816a_exit(void)
307 {
308         pnp_unregister_card_driver(&ad1816a_pnpc_driver);
309 }
310
311 module_init(alsa_card_ad1816a_init)
312 module_exit(alsa_card_ad1816a_exit)