ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / vx222 / vx222.c
1 /*
2  * Driver for Digigram VX222 V2/Mic PCI soundcards
3  *
4  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20
21 #include <sound/driver.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/pci.h>
25 #include <linux/slab.h>
26 #include <sound/core.h>
27 #define SNDRV_GET_ID
28 #include <sound/initval.h>
29 #include "vx222.h"
30
31 #define chip_t vx_core_t
32
33 #define CARD_NAME "VX222"
34
35 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
36 MODULE_DESCRIPTION("Digigram VX222 V2/Mic");
37 MODULE_LICENSE("GPL");
38 MODULE_CLASSES("{sound}");
39 MODULE_DEVICES("{{Digigram," CARD_NAME "}}");
40
41 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
42 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
43 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
44 static int mic[SNDRV_CARDS]; /* microphone */
45 static int ibl[SNDRV_CARDS]; /* microphone */
46
47 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
48 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
49 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
50 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
51 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
52 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
53 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
54 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
55 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
56 MODULE_PARM(mic, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
57 MODULE_PARM_DESC(mic, "Enable Microphone.");
58 MODULE_PARM_SYNTAX(mic, SNDRV_ENABLED "," SNDRV_BOOLEAN_FALSE_DESC);
59 MODULE_PARM(ibl, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
60 MODULE_PARM_DESC(ibl, "Capture IBL size.");
61 MODULE_PARM_SYNTAX(ibl, SNDRV_ENABLED);
62
63 /*
64  */
65
66 enum {
67         VX_PCI_VX222_OLD,
68         VX_PCI_VX222_NEW
69 };
70
71 static struct pci_device_id snd_vx222_ids[] = {
72         { 0x10b5, 0x9050, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VX_PCI_VX222_OLD, },   /* PLX */
73         { 0x10b5, 0x9030, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VX_PCI_VX222_NEW, },   /* PLX */
74         { 0, }
75 };
76
77 MODULE_DEVICE_TABLE(pci, snd_vx222_ids);
78
79
80 /*
81  */
82
83 static struct snd_vx_hardware vx222_old_hw = {
84
85         .name = "VX222/Old",
86         .type = VX_TYPE_BOARD,
87         /* hw specs */
88         .num_codecs = 1,
89         .num_ins = 1,
90         .num_outs = 1,
91         .output_level_max = VX_ANALOG_OUT_LEVEL_MAX,
92 };
93
94 static struct snd_vx_hardware vx222_v2_hw = {
95
96         .name = "VX222/v2",
97         .type = VX_TYPE_V2,
98         /* hw specs */
99         .num_codecs = 1,
100         .num_ins = 1,
101         .num_outs = 1,
102         .output_level_max = VX2_AKM_LEVEL_MAX,
103 };
104
105 static struct snd_vx_hardware vx222_mic_hw = {
106
107         .name = "VX222/Mic",
108         .type = VX_TYPE_MIC,
109         /* hw specs */
110         .num_codecs = 1,
111         .num_ins = 1,
112         .num_outs = 1,
113         .output_level_max = VX2_AKM_LEVEL_MAX,
114 };
115
116
117 /*
118  */
119 static int snd_vx222_free(vx_core_t *chip)
120 {
121         int i;
122         struct snd_vx222 *vx = (struct snd_vx222 *)chip;
123
124         if (chip->irq >= 0)
125                 free_irq(chip->irq, (void*)chip);
126         for (i = 0; i < 2; i++) {
127                 if (vx->port_res[i]) {
128                         release_resource(vx->port_res[i]);
129                         kfree_nocheck(vx->port_res[i]);
130                 }
131         }
132         snd_magic_kfree(chip);
133         return 0;
134 }
135
136 static int snd_vx222_dev_free(snd_device_t *device)
137 {
138         vx_core_t *chip = snd_magic_cast(vx_core_t, device->device_data, return -ENXIO);
139         return snd_vx222_free(chip);
140 }
141
142
143 static int __devinit snd_vx222_create(snd_card_t *card, struct pci_dev *pci,
144                                       struct snd_vx_hardware *hw,
145                                       struct snd_vx222 **rchip)
146 {
147         vx_core_t *chip;
148         struct snd_vx222 *vx;
149         int i, err;
150         static snd_device_ops_t ops = {
151                 .dev_free =     snd_vx222_dev_free,
152         };
153         struct snd_vx_ops *vx_ops;
154
155         /* enable PCI device */
156         if ((err = pci_enable_device(pci)) < 0)
157                 return err;
158         pci_set_master(pci);
159
160         vx_ops = hw->type == VX_TYPE_BOARD ? &vx222_old_ops : &vx222_ops;
161         chip = snd_vx_create(card, hw, vx_ops,
162                              sizeof(struct snd_vx222) - sizeof(vx_core_t));
163         if (! chip)
164                 return -ENOMEM;
165         vx = (struct snd_vx222 *)chip;
166
167         for (i = 0; i < 2; i++) {
168                 if (!(pci_resource_flags(pci, i + 1) & IORESOURCE_IO)) {
169                         snd_printk(KERN_ERR "invalid i/o resource %d\n", i + 1);
170                         snd_vx222_free(chip);
171                         return -ENOMEM;
172                 }
173                 vx->port[i] = pci_resource_start(pci, i + 1);
174                 if ((vx->port_res[i] = request_region(vx->port[i], 0x60,
175                                                       CARD_NAME)) == NULL) {
176                         snd_printk(KERN_ERR "unable to grab port 0x%lx\n", vx->port[i]);
177                         snd_vx222_free(chip);
178                         return -EBUSY;
179                 }
180         }
181
182         if (request_irq(pci->irq, snd_vx_irq_handler, SA_INTERRUPT|SA_SHIRQ,
183                         CARD_NAME, (void *) chip)) {
184                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
185                 snd_vx222_free(chip);
186                 return -EBUSY;
187         }
188         chip->irq = pci->irq;
189
190         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
191                 snd_vx222_free(chip);
192                 return err;
193         }
194
195         snd_card_set_dev(card, &pci->dev);
196
197         *rchip = vx;
198         return 0;
199 }
200
201
202 static int __devinit snd_vx222_probe(struct pci_dev *pci,
203                                      const struct pci_device_id *pci_id)
204 {
205         static int dev;
206         snd_card_t *card;
207         struct snd_vx_hardware *hw;
208         struct snd_vx222 *vx;
209         int err;
210
211         if (dev >= SNDRV_CARDS)
212                 return -ENODEV;
213         if (!enable[dev]) {
214                 dev++;
215                 return -ENOENT;
216         }
217
218         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
219         if (card == NULL)
220                 return -ENOMEM;
221
222         switch ((int)pci_id->driver_data) {
223         case VX_PCI_VX222_OLD:
224                 hw = &vx222_old_hw;
225                 break;
226         case VX_PCI_VX222_NEW:
227         default:
228                 if (mic[dev])
229                         hw = &vx222_mic_hw;
230                 else
231                         hw = &vx222_v2_hw;
232                 break;
233         }
234         if ((err = snd_vx222_create(card, pci, hw, &vx)) < 0) {
235                 snd_card_free(card);
236                 return err;
237         }
238         vx->core.ibl.size = ibl[dev];
239
240         sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %i",
241                 card->shortname, vx->port[0], vx->port[1], vx->core.irq);
242         snd_printdd("%s at 0x%lx & 0x%lx, irq %i\n",
243                     card->shortname, vx->port[0], vx->port[1], vx->core.irq);
244
245         if ((err = snd_vx_hwdep_new(&vx->core)) < 0) {
246                 snd_card_free(card);
247                 return err;
248         }
249
250         if ((err = snd_card_register(card)) < 0) {
251                 snd_card_free(card);
252                 return err;
253         }
254
255         pci_set_drvdata(pci, card);
256         dev++;
257         return 0;
258 }
259
260 static void __devexit snd_vx222_remove(struct pci_dev *pci)
261 {
262         snd_card_free(pci_get_drvdata(pci));
263         pci_set_drvdata(pci, NULL);
264 }
265
266 static struct pci_driver driver = {
267         .name = "Digigram VX222",
268         .id_table = snd_vx222_ids,
269         .probe = snd_vx222_probe,
270         .remove = __devexit_p(snd_vx222_remove),
271 };
272
273 static int __init alsa_card_vx222_init(void)
274 {
275         int err;
276
277         if ((err = pci_module_init(&driver)) < 0) {
278 #ifdef MODULE
279                 printk(KERN_ERR "Digigram VX222 soundcard not found or device busy\n");
280 #endif
281                 return err;
282         }
283         return 0;
284 }
285
286 static void __exit alsa_card_vx222_exit(void)
287 {
288         pci_unregister_driver(&driver);
289 }
290
291 module_init(alsa_card_vx222_init)
292 module_exit(alsa_card_vx222_exit)
293
294 #ifndef MODULE
295
296 /* format is: snd-vx222=enable,index,id */
297
298 static int __init alsa_card_vx222_setup(char *str)
299 {
300         static unsigned __initdata nr_dev = 0;
301
302         if (nr_dev >= SNDRV_CARDS)
303                 return 0;
304         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
305                get_option(&str,&index[nr_dev]) == 2 &&
306                get_id(&str,&id[nr_dev]) == 2);
307         nr_dev++;
308         return 1;
309 }
310
311 __setup("snd-vx222=", alsa_card_vx222_setup);
312
313 #endif /* ifndef MODULE */