patch-2_6_7-vs1_9_1_12
[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 <linux/moduleparam.h>
30 #include <sound/core.h>
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 static int boot_devs;
57
58 module_param_array(index, int, boot_devs, 0444);
59 MODULE_PARM_DESC(index, "Index value for DT-019X based soundcard.");
60 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
61 module_param_array(id, charp, boot_devs, 0444);
62 MODULE_PARM_DESC(id, "ID string for DT-019X based soundcard.");
63 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
64 module_param_array(enable, bool, boot_devs, 0444);
65 MODULE_PARM_DESC(enable, "Enable DT-019X based soundcard.");
66 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
67 module_param_array(port, long, boot_devs, 0444);
68 MODULE_PARM_DESC(port, "Port # for dt019x driver.");
69 MODULE_PARM_SYNTAX(port, SNDRV_PORT12_DESC);
70 module_param_array(mpu_port, long, boot_devs, 0444);
71 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for dt019x driver.");
72 MODULE_PARM_SYNTAX(mpu_port, SNDRV_PORT12_DESC);
73 module_param_array(fm_port, long, boot_devs, 0444);
74 MODULE_PARM_DESC(fm_port, "FM port # for dt019x driver.");
75 MODULE_PARM_SYNTAX(fm_port, SNDRV_PORT12_DESC);
76 module_param_array(irq, int, boot_devs, 0444);
77 MODULE_PARM_DESC(irq, "IRQ # for dt019x driver.");
78 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
79 module_param_array(mpu_irq, int, boot_devs, 0444);
80 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for dt019x driver.");
81 MODULE_PARM_SYNTAX(mpu_irq, SNDRV_IRQ_DESC);
82 module_param_array(dma8, int, boot_devs, 0444);
83 MODULE_PARM_DESC(dma8, "8-bit DMA # for dt019x driver.");
84 MODULE_PARM_SYNTAX(dma8, SNDRV_DMA8_DESC);
85
86 struct snd_card_dt019x {
87         struct pnp_dev *dev;
88         struct pnp_dev *devmpu;
89         struct pnp_dev *devopl;
90 };
91
92 static struct pnp_card_device_id snd_dt019x_pnpids[] = {
93         /* DT197A30 */
94         { .id = "RWB1688", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" }, } },
95         /* DT0196 / ALS-007 */
96         { .id = "ALS0007", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" }, } },
97         { .id = "",  }
98 };
99
100 MODULE_DEVICE_TABLE(pnp_card, snd_dt019x_pnpids);
101
102
103 #define DRIVER_NAME     "snd-card-dt019x"
104
105
106 static int __devinit snd_card_dt019x_pnp(int dev, struct snd_card_dt019x *acard,
107                                          struct pnp_card_link *card,
108                                          const struct pnp_card_device_id *pid)
109 {
110         struct pnp_dev *pdev;
111         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
112         int err;
113
114         if (!cfg)
115                 return -ENOMEM;
116
117         acard->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
118         if (acard->dev == NULL) {
119                 kfree (cfg);
120                 return -ENODEV;
121         }
122         acard->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
123         acard->devopl = pnp_request_card_device(card, pid->devs[2].id, NULL);
124
125         pdev = acard->dev;
126         pnp_init_resource_table(cfg);
127
128         if (port[dev] != SNDRV_AUTO_PORT)
129                 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
130         if (dma8[dev] != SNDRV_AUTO_DMA)
131                 pnp_resource_change(&cfg->dma_resource[0], dma8[dev], 1);
132         if (irq[dev] != SNDRV_AUTO_IRQ)
133                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
134
135         if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
136                 snd_printk(KERN_ERR PFX "DT-019X AUDIO the requested resources are invalid, using auto config\n");
137         err = pnp_activate_dev(pdev);
138         if (err < 0) {
139                 snd_printk(KERN_ERR PFX "DT-019X AUDIO pnp configure failure\n");
140                 kfree(cfg);
141                 return err;
142         }
143
144         port[dev] = pnp_port_start(pdev, 0);
145         dma8[dev] = pnp_dma(pdev, 0);
146         irq[dev] = pnp_irq(pdev, 0);
147         snd_printdd("dt019x: found audio interface: port=0x%lx, irq=0x%x, dma=0x%x\n",
148                         port[dev],irq[dev],dma8[dev]);
149
150         pdev = acard->devmpu;
151
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 "DT-019X MPU401 the requested resources are invalid, using auto config\n");
160                 err = pnp_activate_dev(pdev);
161                 if (err < 0) {
162                         pnp_release_card_device(pdev);
163                         snd_printk(KERN_ERR PFX "DT-019X MPU401 pnp configure failure, skipping\n");
164                         goto __mpu_error;
165                 }
166                 mpu_port[dev] = pnp_port_start(pdev, 0);
167                 mpu_irq[dev] = pnp_irq(pdev, 0);
168                 snd_printdd("dt019x: found MPU-401: port=0x%lx, irq=0x%x\n",
169                                 mpu_port[dev],mpu_irq[dev]);
170         } else {
171         __mpu_error:
172                 acard->devmpu = NULL;
173                 mpu_port[dev] = -1;
174         }
175
176         pdev = acard->devopl;
177         if (pdev != NULL) {
178                 pnp_init_resource_table(cfg);
179                 if (fm_port[dev] != SNDRV_AUTO_PORT)
180                         pnp_resource_change(&cfg->port_resource[0], fm_port[dev], 4);
181                 if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
182                         snd_printk(KERN_ERR PFX "DT-019X OPL3 the requested resources are invalid, using auto config\n");
183                 err = pnp_activate_dev(pdev);
184                 if (err < 0) {
185                         pnp_release_card_device(pdev);
186                         snd_printk(KERN_ERR PFX "DT-019X OPL3 pnp configure failure, skipping\n");
187                         goto __fm_error;
188                 }
189                 fm_port[dev] = pnp_port_start(pdev, 0);
190                 snd_printdd("dt019x: found OPL3 synth: port=0x%lx\n",fm_port[dev]);
191         } else {
192         __fm_error:
193                 acard->devopl = NULL;
194                 fm_port[dev] = -1;
195         }
196
197         kfree(cfg);
198         return 0;
199 }
200
201 static int __devinit snd_card_dt019x_probe(int dev, struct pnp_card_link *pcard, const struct pnp_card_device_id *pid)
202 {
203         int error;
204         sb_t *chip;
205         snd_card_t *card;
206         struct snd_card_dt019x *acard;
207         opl3_t *opl3;
208
209         if ((card = snd_card_new(index[dev], id[dev], THIS_MODULE,
210                                  sizeof(struct snd_card_dt019x))) == NULL)
211                 return -ENOMEM;
212         acard = (struct snd_card_dt019x *)card->private_data;
213
214         snd_card_set_dev(card, &pcard->card->dev);
215         if ((error = snd_card_dt019x_pnp(dev, acard, pcard, pid))) {
216                 snd_card_free(card);
217                 return error;
218         }
219
220         if ((error = snd_sbdsp_create(card, port[dev],
221                                       irq[dev],
222                                       snd_sb16dsp_interrupt,
223                                       dma8[dev],
224                                       -1,
225                                       SB_HW_DT019X,
226                                       &chip)) < 0) {
227                 snd_card_free(card);
228                 return error;
229         }
230
231         strcpy(card->driver, "DT-019X");
232         strcpy(card->shortname, "Diamond Tech. DT-019X");
233         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
234                 card->shortname, chip->name, chip->port,
235                 irq[dev], dma8[dev]);
236
237         if ((error = snd_sb16dsp_pcm(chip, 0, NULL)) < 0) {
238                 snd_card_free(card);
239                 return error;
240         }
241         if ((error = snd_sbmixer_new(chip)) < 0) {
242                 snd_card_free(card);
243                 return error;
244         }
245
246         if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
247                 if (mpu_irq[dev] == SNDRV_AUTO_IRQ)
248                         mpu_irq[dev] = -1;
249                 if (snd_mpu401_uart_new(card, 0,
250 /*                                      MPU401_HW_SB,*/
251                                         MPU401_HW_MPU401,
252                                         mpu_port[dev], 0,
253                                         mpu_irq[dev],
254                                         mpu_irq[dev] >= 0 ? SA_INTERRUPT : 0,
255                                         NULL) < 0)
256                         snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx ?\n", mpu_port[dev]);
257         }
258
259         if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
260                 if (snd_opl3_create(card,
261                                     fm_port[dev],
262                                     fm_port[dev] + 2,
263                                     OPL3_HW_AUTO, 0, &opl3) < 0) {
264                         snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx ?\n",
265                                    fm_port[dev], fm_port[dev] + 2);
266                 } else {
267                         if ((error = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
268                                 snd_card_free(card);
269                                 return error;
270                         }
271                         if ((error = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
272                                 snd_card_free(card);
273                                 return error;
274                         }
275                 }
276         }
277
278         if ((error = snd_card_register(card)) < 0) {
279                 snd_card_free(card);
280                 return error;
281         }
282         pnp_set_card_drvdata(pcard, card);
283         return 0;
284 }
285
286 static int __devinit snd_dt019x_pnp_probe(struct pnp_card_link *card,
287                                           const struct pnp_card_device_id *pid)
288 {
289         static int dev;
290         int res;
291
292         for ( ; dev < SNDRV_CARDS; dev++) {
293                 if (!enable[dev])
294                         continue;
295                 res = snd_card_dt019x_probe(dev, card, pid);
296                 if (res < 0)
297                         return res;
298                 dev++;
299                 return 0;
300         }
301         return -ENODEV;
302 }
303
304 static void __devexit snd_dt019x_pnp_remove(struct pnp_card_link * pcard)
305 {
306         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
307         snd_card_disconnect(card);
308         snd_card_free_in_thread(card);
309 }
310
311 static struct pnp_card_driver dt019x_pnpc_driver = {
312         .flags          = PNP_DRIVER_RES_DISABLE,
313         .name           = "dt019x",
314         .id_table       = snd_dt019x_pnpids,
315         .probe          = snd_dt019x_pnp_probe,
316         .remove         = __devexit_p(snd_dt019x_pnp_remove),
317 };
318
319 static int __init alsa_card_dt019x_init(void)
320 {
321         int cards = 0;
322
323         cards += pnp_register_card_driver(&dt019x_pnpc_driver);
324
325 #ifdef MODULE
326         if (!cards) {
327                 pnp_unregister_card_driver(&dt019x_pnpc_driver);
328                 snd_printk(KERN_ERR "no DT-019X / ALS-007 based soundcards found\n");
329         }
330 #endif
331         return cards ? 0 : -ENODEV;
332 }
333
334 static void __exit alsa_card_dt019x_exit(void)
335 {
336         pnp_unregister_card_driver(&dt019x_pnpc_driver);
337 }
338
339 module_init(alsa_card_dt019x_init)
340 module_exit(alsa_card_dt019x_exit)