ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / dt019x.c
1
2 /*
3     dt019x.c - driver for Diamond Technologies DT-0197H based soundcards.
4     Copyright (C) 1999, 2002 by Massimo Piccioni <dafastidio@libero.it>
5
6     Generalised for soundcards based on DT-0196 and ALS-007 chips 
7     by Jonathan Woithe <jwoithe@physics.adelaide.edu.au>: June 2002.
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22 */
23
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/sched.h>
27 #include <linux/wait.h>
28 #include <linux/pnp.h>
29 #include <sound/core.h>
30 #define SNDRV_GET_ID
31 #include <sound/initval.h>
32 #include <sound/mpu401.h>
33 #include <sound/opl3.h>
34 #include <sound/sb.h>
35
36 #define chip_t sb_t
37
38 #define PFX "dt019x: "
39
40 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
41 MODULE_DESCRIPTION("Diamond Technologies DT-019X / Avance Logic ALS-007");
42 MODULE_LICENSE("GPL");
43 MODULE_CLASSES("{sound}");
44 MODULE_DEVICES("{{Diamond Technologies DT-019X},"
45                "{Avance Logic ALS-007}}");
46
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
49 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;  /* Enable this card */
50 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* PnP setup */
51 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
52 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* PnP setup */
53 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* PnP setup */
54 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* PnP setup */
55 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* PnP setup */
56
57 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
58 MODULE_PARM_DESC(index, "Index value for DT-019X based soundcard.");
59 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
60 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
61 MODULE_PARM_DESC(id, "ID string for DT-019X based soundcard.");
62 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
63 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
64 MODULE_PARM_DESC(enable, "Enable DT-019X based soundcard.");
65 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
66 MODULE_PARM(port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
67 MODULE_PARM_DESC(port, "Port # for dt019x driver.");
68 MODULE_PARM_SYNTAX(port, SNDRV_PORT12_DESC);
69 MODULE_PARM(mpu_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
70 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for dt019x driver.");
71 MODULE_PARM_SYNTAX(mpu_port, SNDRV_PORT12_DESC);
72 MODULE_PARM(fm_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
73 MODULE_PARM_DESC(fm_port, "FM port # for dt019x driver.");
74 MODULE_PARM_SYNTAX(fm_port, SNDRV_PORT12_DESC);
75 MODULE_PARM(irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
76 MODULE_PARM_DESC(irq, "IRQ # for dt019x driver.");
77 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
78 MODULE_PARM(mpu_irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
79 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for dt019x driver.");
80 MODULE_PARM_SYNTAX(mpu_irq, SNDRV_IRQ_DESC);
81 MODULE_PARM(dma8, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
82 MODULE_PARM_DESC(dma8, "8-bit DMA # for dt019x driver.");
83 MODULE_PARM_SYNTAX(dma8, SNDRV_DMA8_DESC);
84
85 struct snd_card_dt019x {
86         struct pnp_dev *dev;
87         struct pnp_dev *devmpu;
88         struct pnp_dev *devopl;
89 };
90
91 static struct pnp_card_device_id snd_dt019x_pnpids[] = {
92         /* DT197A30 */
93         { .id = "RWB1688", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" }, } },
94         /* DT0196 / ALS-007 */
95         { .id = "ALS0007", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" }, } },
96         { .id = "",  }
97 };
98
99 MODULE_DEVICE_TABLE(pnp_card, snd_dt019x_pnpids);
100
101
102 #define DRIVER_NAME     "snd-card-dt019x"
103
104
105 static int __devinit snd_card_dt019x_pnp(int dev, struct snd_card_dt019x *acard,
106                                          struct pnp_card_link *card,
107                                          const struct pnp_card_device_id *pid)
108 {
109         struct pnp_dev *pdev;
110         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
111         int err;
112
113         if (!cfg)
114                 return -ENOMEM;
115
116         acard->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
117         if (acard->dev == NULL) {
118                 kfree (cfg);
119                 return -ENODEV;
120         }
121         acard->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
122         acard->devopl = pnp_request_card_device(card, pid->devs[2].id, NULL);
123
124         pdev = acard->dev;
125         pnp_init_resource_table(cfg);
126
127         if (port[dev] != SNDRV_AUTO_PORT)
128                 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
129         if (dma8[dev] != SNDRV_AUTO_DMA)
130                 pnp_resource_change(&cfg->dma_resource[0], dma8[dev], 1);
131         if (irq[dev] != SNDRV_AUTO_IRQ)
132                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
133
134         if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
135                 snd_printk(KERN_ERR PFX "DT-019X AUDIO the requested resources are invalid, using auto config\n");
136         err = pnp_activate_dev(pdev);
137         if (err < 0) {
138                 snd_printk(KERN_ERR PFX "DT-019X AUDIO pnp configure failure\n");
139                 kfree(cfg);
140                 return err;
141         }
142
143         port[dev] = pnp_port_start(pdev, 0);
144         dma8[dev] = pnp_dma(pdev, 0);
145         irq[dev] = pnp_irq(pdev, 0);
146         snd_printdd("dt019x: found audio interface: port=0x%lx, irq=0x%x, dma=0x%x\n",
147                         port[dev],irq[dev],dma8[dev]);
148
149         pdev = acard->devmpu;
150
151         if (pdev != NULL) {
152                 pnp_init_resource_table(cfg);
153                 if (mpu_port[dev] != SNDRV_AUTO_PORT)
154                         pnp_resource_change(&cfg->port_resource[0], mpu_port[dev], 2);
155                 if (mpu_irq[dev] != SNDRV_AUTO_IRQ)
156                         pnp_resource_change(&cfg->irq_resource[0], mpu_irq[dev], 1);
157                 if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
158                         snd_printk(KERN_ERR PFX "DT-019X MPU401 the requested resources are invalid, using auto config\n");
159                 err = pnp_activate_dev(pdev);
160                 if (err < 0) {
161                         pnp_release_card_device(pdev);
162                         snd_printk(KERN_ERR PFX "DT-019X MPU401 pnp configure failure, skipping\n");
163                         goto __mpu_error;
164                 }
165                 mpu_port[dev] = pnp_port_start(pdev, 0);
166                 mpu_irq[dev] = pnp_irq(pdev, 0);
167                 snd_printdd("dt019x: found MPU-401: port=0x%lx, irq=0x%x\n",
168                                 mpu_port[dev],mpu_irq[dev]);
169         } else {
170         __mpu_error:
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 "DT-019X OPL3 the requested resources are invalid, using auto config\n");
182                 err = pnp_activate_dev(pdev);
183                 if (err < 0) {
184                         pnp_release_card_device(pdev);
185                         snd_printk(KERN_ERR PFX "DT-019X OPL3 pnp configure failure, skipping\n");
186                         goto __fm_error;
187                 }
188                 fm_port[dev] = pnp_port_start(pdev, 0);
189                 snd_printdd("dt019x: found OPL3 synth: port=0x%lx\n",fm_port[dev]);
190         } else {
191         __fm_error:
192                 acard->devopl = NULL;
193                 fm_port[dev] = -1;
194         }
195
196         kfree(cfg);
197         return 0;
198 }
199
200 static int __devinit snd_card_dt019x_probe(int dev, struct pnp_card_link *pcard, const struct pnp_card_device_id *pid)
201 {
202         int error;
203         sb_t *chip;
204         snd_card_t *card;
205         struct snd_card_dt019x *acard;
206         opl3_t *opl3;
207
208         if ((card = snd_card_new(index[dev], id[dev], THIS_MODULE,
209                                  sizeof(struct snd_card_dt019x))) == NULL)
210                 return -ENOMEM;
211         acard = (struct snd_card_dt019x *)card->private_data;
212
213         snd_card_set_dev(card, &pcard->card->dev);
214         if ((error = snd_card_dt019x_pnp(dev, acard, pcard, pid))) {
215                 snd_card_free(card);
216                 return error;
217         }
218
219         if ((error = snd_sbdsp_create(card, port[dev],
220                                       irq[dev],
221                                       snd_sb16dsp_interrupt,
222                                       dma8[dev],
223                                       -1,
224                                       SB_HW_DT019X,
225                                       &chip)) < 0) {
226                 snd_card_free(card);
227                 return error;
228         }
229
230         strcpy(card->driver, "DT-019X");
231         strcpy(card->shortname, "Diamond Tech. DT-019X");
232         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
233                 card->shortname, chip->name, chip->port,
234                 irq[dev], dma8[dev]);
235
236         if ((error = snd_sb16dsp_pcm(chip, 0, NULL)) < 0) {
237                 snd_card_free(card);
238                 return error;
239         }
240         if ((error = snd_sbmixer_new(chip)) < 0) {
241                 snd_card_free(card);
242                 return error;
243         }
244
245         if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
246                 if (mpu_irq[dev] == SNDRV_AUTO_IRQ)
247                         mpu_irq[dev] = -1;
248                 if (snd_mpu401_uart_new(card, 0,
249 /*                                      MPU401_HW_SB,*/
250                                         MPU401_HW_MPU401,
251                                         mpu_port[dev], 0,
252                                         mpu_irq[dev],
253                                         mpu_irq[dev] >= 0 ? SA_INTERRUPT : 0,
254                                         NULL) < 0)
255                         snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx ?\n", mpu_port[dev]);
256         }
257
258         if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
259                 if (snd_opl3_create(card,
260                                     fm_port[dev],
261                                     fm_port[dev] + 2,
262                                     OPL3_HW_AUTO, 0, &opl3) < 0) {
263                         snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx ?\n",
264                                    fm_port[dev], fm_port[dev] + 2);
265                 } else {
266                         if ((error = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
267                                 snd_card_free(card);
268                                 return error;
269                         }
270                         if ((error = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
271                                 snd_card_free(card);
272                                 return error;
273                         }
274                 }
275         }
276
277         if ((error = snd_card_register(card)) < 0) {
278                 snd_card_free(card);
279                 return error;
280         }
281         pnp_set_card_drvdata(pcard, card);
282         return 0;
283 }
284
285 static int __devinit snd_dt019x_pnp_probe(struct pnp_card_link *card,
286                                           const struct pnp_card_device_id *pid)
287 {
288         static int dev;
289         int res;
290
291         for ( ; dev < SNDRV_CARDS; dev++) {
292                 if (!enable[dev])
293                         continue;
294                 res = snd_card_dt019x_probe(dev, card, pid);
295                 if (res < 0)
296                         return res;
297                 dev++;
298                 return 0;
299         }
300         return -ENODEV;
301 }
302
303 static void __devexit snd_dt019x_pnp_remove(struct pnp_card_link * pcard)
304 {
305         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
306         snd_card_disconnect(card);
307         snd_card_free_in_thread(card);
308 }
309
310 static struct pnp_card_driver dt019x_pnpc_driver = {
311         .flags          = PNP_DRIVER_RES_DISABLE,
312         .name           = "dt019x",
313         .id_table       = snd_dt019x_pnpids,
314         .probe          = snd_dt019x_pnp_probe,
315         .remove         = __devexit_p(snd_dt019x_pnp_remove),
316 };
317
318 static int __init alsa_card_dt019x_init(void)
319 {
320         int cards = 0;
321
322         cards += pnp_register_card_driver(&dt019x_pnpc_driver);
323
324 #ifdef MODULE
325         if (!cards) {
326                 pnp_unregister_card_driver(&dt019x_pnpc_driver);
327                 snd_printk(KERN_ERR "no DT-019X / ALS-007 based soundcards found\n");
328         }
329 #endif
330         return cards ? 0 : -ENODEV;
331 }
332
333 static void __exit alsa_card_dt019x_exit(void)
334 {
335         pnp_unregister_card_driver(&dt019x_pnpc_driver);
336 }
337
338 module_init(alsa_card_dt019x_init)
339 module_exit(alsa_card_dt019x_exit)
340
341 #ifndef MODULE
342
343 /* format is: snd-dt019x=enable,index,id,
344                           port,mpu_port,fm_port,
345                           irq,mpu_irq,dma8,dma8_size */
346
347 static int __init alsa_card_dt019x_setup(char *str)
348 {
349         static unsigned __initdata nr_dev = 0;
350
351         if (nr_dev >= SNDRV_CARDS)
352                 return 0;
353         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
354                get_option(&str,&index[nr_dev]) == 2 &&
355                get_id(&str,&id[nr_dev]) == 2 &&
356                get_option_long(&str,&port[nr_dev]) == 2 &&
357                get_option_long(&str,&mpu_port[nr_dev]) == 2 &&
358                get_option_long(&str,&fm_port[nr_dev]) == 2 &&
359                get_option(&str,&irq[nr_dev]) == 2 &&
360                get_option(&str,&mpu_irq[nr_dev]) == 2 &&
361                get_option(&str,&dma8[nr_dev]) == 2);
362         nr_dev++;
363         return 1;
364 }
365
366 __setup("snd-dt019x=", alsa_card_dt019x_setup);
367
368 #endif /* ifndef MODULE */