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