patch-2_6_7-vs1_9_1_12
[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 <linux/moduleparam.h>
27 #include <sound/core.h>
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 static int boot_devs;
47
48 module_param_array(index, int, boot_devs, 0444);
49 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
50 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
51 module_param_array(id, charp, boot_devs, 0444);
52 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
53 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
54 module_param_array(enable, bool, boot_devs, 0444);
55 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
56 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
57 module_param_array(mic, bool, boot_devs, 0444);
58 MODULE_PARM_DESC(mic, "Enable Microphone.");
59 MODULE_PARM_SYNTAX(mic, SNDRV_ENABLED "," SNDRV_BOOLEAN_FALSE_DESC);
60 module_param_array(ibl, int, boot_devs, 0444);
61 MODULE_PARM_DESC(ibl, "Capture IBL size.");
62 MODULE_PARM_SYNTAX(ibl, SNDRV_ENABLED);
63
64 /*
65  */
66
67 enum {
68         VX_PCI_VX222_OLD,
69         VX_PCI_VX222_NEW
70 };
71
72 static struct pci_device_id snd_vx222_ids[] = {
73         { 0x10b5, 0x9050, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VX_PCI_VX222_OLD, },   /* PLX */
74         { 0x10b5, 0x9030, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VX_PCI_VX222_NEW, },   /* PLX */
75         { 0, }
76 };
77
78 MODULE_DEVICE_TABLE(pci, snd_vx222_ids);
79
80
81 /*
82  */
83
84 static struct snd_vx_hardware vx222_old_hw = {
85
86         .name = "VX222/Old",
87         .type = VX_TYPE_BOARD,
88         /* hw specs */
89         .num_codecs = 1,
90         .num_ins = 1,
91         .num_outs = 1,
92         .output_level_max = VX_ANALOG_OUT_LEVEL_MAX,
93 };
94
95 static struct snd_vx_hardware vx222_v2_hw = {
96
97         .name = "VX222/v2",
98         .type = VX_TYPE_V2,
99         /* hw specs */
100         .num_codecs = 1,
101         .num_ins = 1,
102         .num_outs = 1,
103         .output_level_max = VX2_AKM_LEVEL_MAX,
104 };
105
106 static struct snd_vx_hardware vx222_mic_hw = {
107
108         .name = "VX222/Mic",
109         .type = VX_TYPE_MIC,
110         /* hw specs */
111         .num_codecs = 1,
112         .num_ins = 1,
113         .num_outs = 1,
114         .output_level_max = VX2_AKM_LEVEL_MAX,
115 };
116
117
118 /*
119  */
120 static int snd_vx222_free(vx_core_t *chip)
121 {
122         int i;
123         struct snd_vx222 *vx = (struct snd_vx222 *)chip;
124
125         if (chip->irq >= 0)
126                 free_irq(chip->irq, (void*)chip);
127         for (i = 0; i < 2; i++) {
128                 if (vx->port_res[i]) {
129                         release_resource(vx->port_res[i]);
130                         kfree_nocheck(vx->port_res[i]);
131                 }
132         }
133         snd_magic_kfree(chip);
134         return 0;
135 }
136
137 static int snd_vx222_dev_free(snd_device_t *device)
138 {
139         vx_core_t *chip = snd_magic_cast(vx_core_t, device->device_data, return -ENXIO);
140         return snd_vx222_free(chip);
141 }
142
143
144 static int __devinit snd_vx222_create(snd_card_t *card, struct pci_dev *pci,
145                                       struct snd_vx_hardware *hw,
146                                       struct snd_vx222 **rchip)
147 {
148         vx_core_t *chip;
149         struct snd_vx222 *vx;
150         int i, err;
151         static snd_device_ops_t ops = {
152                 .dev_free =     snd_vx222_dev_free,
153         };
154         struct snd_vx_ops *vx_ops;
155
156         /* enable PCI device */
157         if ((err = pci_enable_device(pci)) < 0)
158                 return err;
159         pci_set_master(pci);
160
161         vx_ops = hw->type == VX_TYPE_BOARD ? &vx222_old_ops : &vx222_ops;
162         chip = snd_vx_create(card, hw, vx_ops,
163                              sizeof(struct snd_vx222) - sizeof(vx_core_t));
164         if (! chip)
165                 return -ENOMEM;
166         vx = (struct snd_vx222 *)chip;
167
168         for (i = 0; i < 2; i++) {
169                 if (!(pci_resource_flags(pci, i + 1) & IORESOURCE_IO)) {
170                         snd_printk(KERN_ERR "invalid i/o resource %d\n", i + 1);
171                         snd_vx222_free(chip);
172                         return -ENOMEM;
173                 }
174                 vx->port[i] = pci_resource_start(pci, i + 1);
175                 if ((vx->port_res[i] = request_region(vx->port[i], 0x60,
176                                                       CARD_NAME)) == NULL) {
177                         snd_printk(KERN_ERR "unable to grab port 0x%lx\n", vx->port[i]);
178                         snd_vx222_free(chip);
179                         return -EBUSY;
180                 }
181         }
182
183         if (request_irq(pci->irq, snd_vx_irq_handler, SA_INTERRUPT|SA_SHIRQ,
184                         CARD_NAME, (void *) chip)) {
185                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
186                 snd_vx222_free(chip);
187                 return -EBUSY;
188         }
189         chip->irq = pci->irq;
190
191         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
192                 snd_vx222_free(chip);
193                 return err;
194         }
195
196         snd_card_set_dev(card, &pci->dev);
197
198         *rchip = vx;
199         return 0;
200 }
201
202
203 static int __devinit snd_vx222_probe(struct pci_dev *pci,
204                                      const struct pci_device_id *pci_id)
205 {
206         static int dev;
207         snd_card_t *card;
208         struct snd_vx_hardware *hw;
209         struct snd_vx222 *vx;
210         int err;
211
212         if (dev >= SNDRV_CARDS)
213                 return -ENODEV;
214         if (!enable[dev]) {
215                 dev++;
216                 return -ENOENT;
217         }
218
219         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
220         if (card == NULL)
221                 return -ENOMEM;
222
223         switch ((int)pci_id->driver_data) {
224         case VX_PCI_VX222_OLD:
225                 hw = &vx222_old_hw;
226                 break;
227         case VX_PCI_VX222_NEW:
228         default:
229                 if (mic[dev])
230                         hw = &vx222_mic_hw;
231                 else
232                         hw = &vx222_v2_hw;
233                 break;
234         }
235         if ((err = snd_vx222_create(card, pci, hw, &vx)) < 0) {
236                 snd_card_free(card);
237                 return err;
238         }
239         vx->core.ibl.size = ibl[dev];
240
241         sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %i",
242                 card->shortname, vx->port[0], vx->port[1], vx->core.irq);
243         snd_printdd("%s at 0x%lx & 0x%lx, irq %i\n",
244                     card->shortname, vx->port[0], vx->port[1], vx->core.irq);
245
246         if ((err = snd_vx_hwdep_new(&vx->core)) < 0) {
247                 snd_card_free(card);
248                 return err;
249         }
250
251         if ((err = snd_card_register(card)) < 0) {
252                 snd_card_free(card);
253                 return err;
254         }
255
256         pci_set_drvdata(pci, card);
257         dev++;
258         return 0;
259 }
260
261 static void __devexit snd_vx222_remove(struct pci_dev *pci)
262 {
263         snd_card_free(pci_get_drvdata(pci));
264         pci_set_drvdata(pci, NULL);
265 }
266
267 static struct pci_driver driver = {
268         .name = "Digigram VX222",
269         .id_table = snd_vx222_ids,
270         .probe = snd_vx222_probe,
271         .remove = __devexit_p(snd_vx222_remove),
272 };
273
274 static int __init alsa_card_vx222_init(void)
275 {
276         return pci_module_init(&driver);
277 }
278
279 static void __exit alsa_card_vx222_exit(void)
280 {
281         pci_unregister_driver(&driver);
282 }
283
284 module_init(alsa_card_vx222_init)
285 module_exit(alsa_card_vx222_exit)