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