vserver 1.9.3
[linux-2.6.git] / sound / isa / als100.c
1
2 /*
3     card-als100.c - driver for Avance Logic ALS100 based soundcards.
4     Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>
5
6     Thanks to Pierfrancesco 'qM2' Passerini.
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 #include <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/wait.h>
26 #include <linux/time.h>
27 #include <linux/pnp.h>
28 #include <linux/moduleparam.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/mpu401.h>
32 #include <sound/opl3.h>
33 #include <sound/sb.h>
34
35 #define PFX "als100: "
36
37 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
38 MODULE_DESCRIPTION("Avance Logic ALS1X0");
39 MODULE_LICENSE("GPL");
40 MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS100 - PRO16PNP},"
41                 "{Avance Logic,ALS110},"
42                 "{Avance Logic,ALS120},"
43                 "{Avance Logic,ALS200},"
44                 "{3D Melody,MF1000},"
45                 "{Digimate,3D Sound},"
46                 "{Avance Logic,ALS120},"
47                 "{RTL,RTL3000}}");
48
49 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
50 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
51 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
52 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* PnP setup */
53 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
54 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* PnP setup */
55 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* PnP setup */
56 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* PnP setup */
57 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* PnP setup */
58 static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;      /* PnP setup */
59 static int boot_devs;
60
61 module_param_array(index, int, boot_devs, 0444);
62 MODULE_PARM_DESC(index, "Index value for als100 based soundcard.");
63 module_param_array(id, charp, boot_devs, 0444);
64 MODULE_PARM_DESC(id, "ID string for als100 based soundcard.");
65 module_param_array(enable, bool, boot_devs, 0444);
66 MODULE_PARM_DESC(enable, "Enable als100 based soundcard.");
67 module_param_array(port, long, boot_devs, 0444);
68 MODULE_PARM_DESC(port, "Port # for als100 driver.");
69 module_param_array(mpu_port, long, boot_devs, 0444);
70 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for als100 driver.");
71 module_param_array(fm_port, long, boot_devs, 0444);
72 MODULE_PARM_DESC(fm_port, "FM port # for als100 driver.");
73 module_param_array(irq, int, boot_devs, 0444);
74 MODULE_PARM_DESC(irq, "IRQ # for als100 driver.");
75 module_param_array(mpu_irq, int, boot_devs, 0444);
76 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for als100 driver.");
77 module_param_array(dma8, int, boot_devs, 0444);
78 MODULE_PARM_DESC(dma8, "8-bit DMA # for als100 driver.");
79 module_param_array(dma16, int, boot_devs, 0444);
80 MODULE_PARM_DESC(dma16, "16-bit DMA # for als100 driver.");
81
82 struct snd_card_als100 {
83         int dev_no;
84         struct pnp_dev *dev;
85         struct pnp_dev *devmpu;
86         struct pnp_dev *devopl;
87 };
88
89 static struct pnp_card_device_id snd_als100_pnpids[] = {
90         /* ALS100 - PRO16PNP */
91         { .id = "ALS0001", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } } },
92         /* ALS110 - MF1000 - Digimate 3D Sound */
93         { .id = "ALS0110", .devs = { { "@@@1001" }, { "@X@1001" }, { "@H@1001" } } },
94         /* ALS120 */
95         { .id = "ALS0120", .devs = { { "@@@2001" }, { "@X@2001" }, { "@H@2001" } } },
96         /* ALS200 */
97         { .id = "ALS0200", .devs = { { "@@@0020" }, { "@X@0020" }, { "@H@0001" } } },
98         /* RTL3000 */
99         { .id = "RTL3000", .devs = { { "@@@2001" }, { "@X@2001" }, { "@H@2001" } } },
100         { .id = "", } /* end */
101 };
102
103 MODULE_DEVICE_TABLE(pnp_card, snd_als100_pnpids);
104
105 #define DRIVER_NAME     "snd-card-als100"
106
107 static int __devinit snd_card_als100_pnp(int dev, struct snd_card_als100 *acard,
108                                          struct pnp_card_link *card,
109                                          const struct pnp_card_device_id *id)
110 {
111         struct pnp_dev *pdev;
112         struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
113         int err;
114
115         if (!cfg)
116                 return -ENOMEM;
117         acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
118         if (acard->dev == NULL) {
119                 kfree(cfg);
120                 return -ENODEV;
121         }
122         acard->devmpu = pnp_request_card_device(card, id->devs[1].id, acard->dev);
123         acard->devopl = pnp_request_card_device(card, id->devs[2].id, acard->devmpu);
124
125         pdev = acard->dev;
126
127         pnp_init_resource_table(cfg);
128
129         /* override resources */
130         if (port[dev] != SNDRV_AUTO_PORT)
131                 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
132         if (dma8[dev] != SNDRV_AUTO_DMA)
133                 pnp_resource_change(&cfg->dma_resource[0], dma8[dev], 1);
134         if (dma16[dev] != SNDRV_AUTO_DMA)
135                 pnp_resource_change(&cfg->dma_resource[1], dma16[dev], 1);
136         if (irq[dev] != SNDRV_AUTO_IRQ)
137                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
138         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
139                 snd_printk(KERN_ERR PFX "AUDIO the requested resources are invalid, using auto config\n");
140         err = pnp_activate_dev(pdev);
141         if (err < 0) {
142                 snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n");
143                 kfree(cfg);
144                 return err;
145         }
146         port[dev] = pnp_port_start(pdev, 0);
147         dma8[dev] = pnp_dma(pdev, 1);
148         dma16[dev] = pnp_dma(pdev, 0);
149         irq[dev] = pnp_irq(pdev, 0);
150
151         pdev = acard->devmpu;
152         if (pdev != NULL) {
153                 pnp_init_resource_table(cfg);
154                 if (mpu_port[dev] != SNDRV_AUTO_PORT)
155                         pnp_resource_change(&cfg->port_resource[0], mpu_port[dev], 2);
156                 if (mpu_irq[dev] != SNDRV_AUTO_IRQ)
157                         pnp_resource_change(&cfg->irq_resource[0], mpu_irq[dev], 1);
158                 if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
159                         snd_printk(KERN_ERR PFX "MPU401 the requested resources are invalid, using auto config\n");
160                 err = pnp_activate_dev(pdev);
161                 if (err < 0)
162                         goto __mpu_error;
163                 mpu_port[dev] = pnp_port_start(pdev, 0);
164                 mpu_irq[dev] = pnp_irq(pdev, 0);
165         } else {
166              __mpu_error:
167                 if (pdev) {
168                         pnp_release_card_device(pdev);
169                         snd_printk(KERN_ERR PFX "MPU401 pnp configure failure, skipping\n");
170                 }
171                 acard->devmpu = NULL;
172                 mpu_port[dev] = -1;
173         }
174
175         pdev = acard->devopl;
176         if (pdev != NULL) {
177                 pnp_init_resource_table(cfg);
178                 if (fm_port[dev] != SNDRV_AUTO_PORT)
179                         pnp_resource_change(&cfg->port_resource[0], fm_port[dev], 4);
180                 if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
181                         snd_printk(KERN_ERR PFX "OPL3 the requested resources are invalid, using auto config\n");
182                 err = pnp_activate_dev(pdev);
183                 if (err < 0)
184                         goto __fm_error;
185                 fm_port[dev] = pnp_port_start(pdev, 0);
186         } else {
187               __fm_error:
188                 if (pdev) {
189                         pnp_release_card_device(pdev);
190                         snd_printk(KERN_ERR PFX "OPL3 pnp configure failure, skipping\n");
191                 }
192                 acard->devopl = NULL;
193                 fm_port[dev] = -1;
194         }
195
196         kfree(cfg);
197         return 0;
198 }
199
200 static int __init snd_card_als100_probe(int dev,
201                                         struct pnp_card_link *pcard,
202                                         const struct pnp_card_device_id *pid)
203 {
204         int error;
205         sb_t *chip;
206         snd_card_t *card;
207         struct snd_card_als100 *acard;
208         opl3_t *opl3;
209
210         if ((card = snd_card_new(index[dev], id[dev], THIS_MODULE,
211                                  sizeof(struct snd_card_als100))) == NULL)
212                 return -ENOMEM;
213         acard = (struct snd_card_als100 *)card->private_data;
214
215         if ((error = snd_card_als100_pnp(dev, acard, pcard, pid))) {
216                 snd_card_free(card);
217                 return error;
218         }
219         snd_card_set_dev(card, &pcard->card->dev);
220
221         if ((error = snd_sbdsp_create(card, port[dev],
222                                       irq[dev],
223                                       snd_sb16dsp_interrupt,
224                                       dma8[dev],
225                                       dma16[dev],
226                                       SB_HW_ALS100, &chip)) < 0) {
227                 snd_card_free(card);
228                 return error;
229         }
230
231         strcpy(card->driver, "ALS100");
232         strcpy(card->shortname, "Avance Logic ALS100");
233         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
234                 card->shortname, chip->name, chip->port,
235                 irq[dev], dma8[dev], dma16[dev]);
236
237         if ((error = snd_sb16dsp_pcm(chip, 0, NULL)) < 0) {
238                 snd_card_free(card);
239                 return error;
240         }
241
242         if ((error = snd_sbmixer_new(chip)) < 0) {
243                 snd_card_free(card);
244                 return error;
245         }
246
247         if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
248                 if (snd_mpu401_uart_new(card, 0, MPU401_HW_ALS100,
249                                         mpu_port[dev], 0, 
250                                         mpu_irq[dev], SA_INTERRUPT,
251                                         NULL) < 0)
252                         snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx\n", mpu_port[dev]);
253         }
254
255         if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
256                 if (snd_opl3_create(card,
257                                     fm_port[dev], fm_port[dev] + 2,
258                                     OPL3_HW_AUTO, 0, &opl3) < 0) {
259                         snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n",
260                                    fm_port[dev], fm_port[dev] + 2);
261                 } else {
262                         if ((error = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
263                                 snd_card_free(card);
264                                 return error;
265                         }
266                         if ((error = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
267                                 snd_card_free(card);
268                                 return error;
269                         }
270                 }
271         }
272
273         if ((error = snd_card_register(card)) < 0) {
274                 snd_card_free(card);
275                 return error;
276         }
277         pnp_set_card_drvdata(pcard, card);
278         return 0;
279 }
280
281 static int __devinit snd_als100_pnp_detect(struct pnp_card_link *card,
282                                            const struct pnp_card_device_id *id)
283 {
284         static int dev;
285         int res;
286
287         for ( ; dev < SNDRV_CARDS; dev++) {
288                 if (!enable[dev])
289                         continue;
290                 res = snd_card_als100_probe(dev, card, id);
291                 if (res < 0)
292                         return res;
293                 dev++;
294                 return 0;
295         }
296         return -ENODEV;
297 }
298
299 static void __devexit snd_als100_pnp_remove(struct pnp_card_link * pcard)
300 {
301         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
302
303         snd_card_disconnect(card);
304         snd_card_free_in_thread(card);
305 }
306
307 static struct pnp_card_driver als100_pnpc_driver = {
308         .flags          = PNP_DRIVER_RES_DISABLE,
309         .name           = "als100",
310         .id_table       = snd_als100_pnpids,
311         .probe          = snd_als100_pnp_detect,
312         .remove         = __devexit_p(snd_als100_pnp_remove),
313 };
314
315 static int __init alsa_card_als100_init(void)
316 {
317         int cards = 0;
318
319         cards += pnp_register_card_driver(&als100_pnpc_driver);
320 #ifdef MODULE
321         if (!cards) {
322                 pnp_unregister_card_driver(&als100_pnpc_driver);
323                 snd_printk(KERN_ERR "no ALS100 based soundcards found\n");
324         }
325 #endif
326         return cards ? 0 : -ENODEV;
327 }
328
329 static void __exit alsa_card_als100_exit(void)
330 {
331         pnp_unregister_card_driver(&als100_pnpc_driver);
332 }
333
334 module_init(alsa_card_als100_init)
335 module_exit(alsa_card_als100_exit)