4e79963dd01f019b213a30f994dd0472853207d1
[linux-flexiantxendom0-3.2.10.git] / sound / isa / cs423x / cs4236.c
1 /*
2  *  Driver for generic CS4232/CS4235/CS4236/CS4236B/CS4237B/CS4238B/CS4239 chips
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
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
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/pnp.h>
26 #include <sound/core.h>
27 #include <sound/cs4231.h>
28 #include <sound/mpu401.h>
29 #include <sound/opl3.h>
30 #define SNDRV_GET_ID
31 #include <sound/initval.h>
32
33 #define chip_t cs4231_t
34
35 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
36 MODULE_LICENSE("GPL");
37 MODULE_CLASSES("{sound}");
38 #ifdef CS4232
39 MODULE_DESCRIPTION("Cirrus Logic CS4232");
40 MODULE_DEVICES("{{Turtle Beach,TBS-2000},"
41                 "{Turtle Beach,Tropez Plus},"
42                 "{SIC CrystalWave 32},"
43                 "{Hewlett Packard,Omnibook 5500},"
44                 "{TerraTec,Maestro 32/96},"
45                 "{Philips,PCA70PS}}");
46 #else
47 MODULE_DESCRIPTION("Cirrus Logic CS4235-9");
48 MODULE_DEVICES("{{Crystal Semiconductors,CS4235},"
49                 "{Crystal Semiconductors,CS4236},"
50                 "{Crystal Semiconductors,CS4237},"
51                 "{Crystal Semiconductors,CS4238},"
52                 "{Crystal Semiconductors,CS4239},"
53                 "{Acer,AW37},"
54                 "{Acer,AW35/Pro},"
55                 "{Crystal,3D},"
56                 "{Crystal Computer,TidalWave128},"
57                 "{Dell,Optiplex GX1},"
58                 "{Dell,Workstation 400 sound},"
59                 "{EliteGroup,P5TX-LA sound},"
60                 "{Gallant,SC-70P},"
61                 "{Gateway,E1000 Onboard CS4236B},"
62                 "{Genius,Sound Maker 3DJ},"
63                 "{Hewlett Packard,HP6330 sound},"
64                 "{IBM,PC 300PL sound},"
65                 "{IBM,Aptiva 2137 E24},"
66                 "{IBM,IntelliStation M Pro},"
67                 "{Intel,Marlin Spike Mobo CS4235},"
68                 "{Intel PR440FX Onboard},"
69                 "{Guillemot,MaxiSound 16 PnP},"
70                 "{NewClear,3D},"
71                 "{TerraTec,AudioSystem EWS64L/XL},"
72                 "{Typhoon Soundsystem,CS4236B},"
73                 "{Turtle Beach,Malibu},"
74                 "{Unknown,Digital PC 5000 Onboard}}");
75 #endif
76
77 #ifdef CS4232
78 #define IDENT "CS4232"
79 #else
80 #define IDENT "CS4236+"
81 #endif
82
83 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
84 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
85 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
86 #ifdef CONFIG_PNP
87 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
88 #endif
89 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* PnP setup */
90 static long cport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;    /* PnP setup */
91 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* PnP setup */
92 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* PnP setup */
93 static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* PnP setup */
94 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 5,7,9,11,12,15 */
95 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* 9,11,12,15 */
96 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3,5,6,7 */
97 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3,5,6,7 */
98
99 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
100 MODULE_PARM_DESC(index, "Index value for " IDENT " soundcard.");
101 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
102 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
103 MODULE_PARM_DESC(id, "ID string for " IDENT " soundcard.");
104 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
105 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
106 MODULE_PARM_DESC(enable, "Enable " IDENT " soundcard.");
107 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
108 #ifdef CONFIG_PNP
109 MODULE_PARM(isapnp, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
110 MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard.");
111 MODULE_PARM_SYNTAX(isapnp, SNDRV_ISAPNP_DESC);
112 #endif
113 MODULE_PARM(port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
114 MODULE_PARM_DESC(port, "Port # for " IDENT " driver.");
115 MODULE_PARM_SYNTAX(port, SNDRV_PORT12_DESC);
116 MODULE_PARM(cport, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
117 MODULE_PARM_DESC(cport, "Control port # for " IDENT " driver.");
118 MODULE_PARM_SYNTAX(cport, SNDRV_PORT12_DESC);
119 MODULE_PARM(mpu_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
120 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " IDENT " driver.");
121 MODULE_PARM_SYNTAX(mpu_port, SNDRV_PORT12_DESC);
122 MODULE_PARM(fm_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
123 MODULE_PARM_DESC(fm_port, "FM port # for " IDENT " driver.");
124 MODULE_PARM_SYNTAX(fm_port, SNDRV_PORT12_DESC);
125 MODULE_PARM(sb_port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
126 MODULE_PARM_DESC(sb_port, "SB port # for " IDENT " driver (optional).");
127 MODULE_PARM_SYNTAX(sb_port, SNDRV_PORT12_DESC);
128 MODULE_PARM(irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
129 MODULE_PARM_DESC(irq, "IRQ # for " IDENT " driver.");
130 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
131 MODULE_PARM(mpu_irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
132 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " IDENT " driver.");
133 MODULE_PARM_SYNTAX(mpu_irq, SNDRV_IRQ_DESC);
134 MODULE_PARM(dma1, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
135 MODULE_PARM_DESC(dma1, "DMA1 # for " IDENT " driver.");
136 MODULE_PARM_SYNTAX(dma1, SNDRV_DMA_DESC);
137 MODULE_PARM(dma2, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
138 MODULE_PARM_DESC(dma2, "DMA2 # for " IDENT " driver.");
139 MODULE_PARM_SYNTAX(dma2, SNDRV_DMA_DESC);
140
141 struct snd_card_cs4236 {
142         struct resource *res_sb_port;
143 #ifdef CONFIG_PNP
144         struct pnp_dev *wss;
145         struct pnp_dev *ctrl;
146         struct pnp_dev *mpu;
147 #endif
148 };
149
150 static snd_card_t *snd_cs4236_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
151
152 #ifdef CONFIG_PNP
153
154 #define ISAPNP_CS4232(_va, _vb, _vc, _device, _wss, _ctrl, _mpu401) \
155         { \
156                 ISAPNP_CARD_ID(_va, _vb, _vc, _device), \
157                 .devs = { ISAPNP_DEVICE_ID(_va, _vb, _vc, _wss), \
158                           ISAPNP_DEVICE_ID(_va, _vb, _vc, _ctrl), \
159                           ISAPNP_DEVICE_ID(_va, _vb, _vc, _mpu401) } \
160         }
161 #define ISAPNP_CS4232_1(_va, _vb, _vc, _device, _wss, _ctrl, _mpu401) \
162         { \
163                 ISAPNP_CARD_ID(_va, _vb, _vc, _device), \
164                 .devs = { ISAPNP_DEVICE_ID(_va, _vb, _vc, _wss), \
165                           ISAPNP_DEVICE_ID(_va, _vb, _vc, _ctrl), \
166                           ISAPNP_DEVICE_ID('P', 'N', 'P', _mpu401) } \
167         }
168 #define ISAPNP_CS4232_WOMPU(_va, _vb, _vc, _device, _wss, _ctrl) \
169         { \
170                 ISAPNP_CARD_ID(_va, _vb, _vc, _device), \
171                 .devs = { ISAPNP_DEVICE_ID(_va, _vb, _vc, _wss), \
172                           ISAPNP_DEVICE_ID(_va, _vb, _vc, _ctrl) } \
173         }
174
175
176 #ifdef CS4232
177 static struct pnp_card_device_id snd_cs423x_pnpids[] __devinitdata = {
178         /* Philips PCA70PS */
179         { .id = "CSC0d32", .devs = { { "CSC0000" }, { "CSC0010" }, { "PNPb006" } } },
180         /* TerraTec Maestro 32/96 (CS4232) */
181         { .id = "CSC1a32", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
182         /* HP Omnibook 5500 onboard */
183         { .id = "CSC4232", .devs = { { "CSC0000" }, { "CSC0002" }, { "CSC0003" } } },
184         /* Unnamed CS4236 card (Made in Taiwan) */
185         { .id = "CSC4236", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
186         /* Turtle Beach TBS-2000 (CS4232) */
187         { .id = "CSC7532", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSCb006" } } },
188         /* Turtle Beach Tropez Plus (CS4232) */
189         { .id = "CSC7632", .devs = { { "CSC0000" }, { "CSC0010" }, { "PNPb006" } } },
190         /* SIC CrystalWave 32 (CS4232) */
191         { .id = "CSCf032", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
192         /* --- */
193         { .id = "" }    /* end */
194 };
195 #else /* CS4236 */
196 static struct pnp_card_device_id snd_cs423x_pnpids[] __devinitdata = {
197         /* Intel Marlin Spike Motherboard - CS4235 */
198         { .id = "CSC0225", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
199         /* Intel Marlin Spike Motherboard (#2) - CS4235 */
200         { .id = "CSC0225", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } },
201         /* Genius Sound Maker 3DJ - CS4237B */
202         { .id = "CSC0437", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
203         /* Digital PC 5000 Onboard - CS4236B */
204         { .id = "CSC0735", .devs = { { "CSC0000" }, { "CSC0010" } } },
205         /* some uknown CS4236B */
206         { .id = "CSC0b35", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
207         /* Intel PR440FX Onboard sound */
208         { .id = "CSC0b36", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
209         /* CS4235 on mainboard without MPU */
210         { .id = "CSC1425", .devs = { { "CSC0100" }, { "CSC0110" } } },
211         /* Gateway E1000 Onboard CS4236B */
212         { .id = "CSC1335", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
213         /* HP 6330 Onboard sound */
214         { .id = "CSC1525", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } },
215         /* Crystal Computer TidalWave128 */
216         { .id = "CSC1e37", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
217         /* ACER AW37 - CS4235 */
218         { .id = "CSC4236", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
219         /* build-in soundcard in EliteGroup P5TX-LA motherboard - CS4237B */
220         { .id = "CSC4237", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
221         /* Crystal 3D - CS4237B */
222         { .id = "CSC4336", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
223         /* Typhoon Soundsystem PnP - CS4236B */
224         { .id = "CSC4536", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
225         /* Crystal CX4235-XQ3 EP - CS4235 */
226         { .id = "CSC4625", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } },
227         /* Crystal Semiconductors CS4237B */
228         { .id = "CSC4637", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
229         /* NewClear 3D - CX4237B-XQ3 */
230         { .id = "CSC4837", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
231         /* Dell Optiplex GX1 - CS4236B */
232         { .id = "CSC6835", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
233         /* Dell P410 motherboard - CS4236B */
234         { .id = "CSC6835", .devs = { { "CSC0000" }, { "CSC0010" } } },
235         /* Dell Workstation 400 Onboard - CS4236B */
236         { .id = "CSC6836", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
237         /* Turtle Beach Malibu - CS4237B */
238         { .id = "CSC7537", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
239         /* CS4235 - onboard */
240         { .id = "CSC8025", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } },
241         /* IBM Aptiva 2137 E24 Onboard - CS4237B */
242         { .id = "CSC8037", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
243         /* IBM IntelliStation M Pro motherboard */
244         { .id = "CSCc835", .devs = { { "CSC0000" }, { "CSC0010" } } },
245         /* Guillemot MaxiSound 16 PnP - CS4236B */
246         { .id = "CSC9836", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
247         /* Gallant SC-70P */
248         { .id = "CSC9837", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
249         /* TerraTec AudioSystem EWS64XL - CS4236B */
250         { .id = "CSCa836", .devs = { { "CSCa800" }, { "CSCa810" }, { "CSCa803" } } },
251         /* TerraTec AudioSystem EWS64XL - CS4236B */
252         { .id = "CSCa836", .devs = { { "CSCa800" }, { "CSCa810" } } },
253         /* ACER AW37/Pro - CS4235 */
254         { .id = "CSCd925", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
255         /* ACER AW35/Pro - CS4237B */
256         { .id = "CSCd937", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
257         /* CS4235 without MPU401 */
258         { .id = "CSCe825", .devs = { { "CSC0100" }, { "CSC0110" } } },
259         /* IBM IntelliStation M Pro 6898 11U - CS4236B */
260         { .id = "CSCe835", .devs = { { "CSC0000" }, { "CSC0010" } } },
261         /* IBM PC 300PL Onboard - CS4236B */
262         { .id = "CSCe836", .devs = { { "CSC0000" }, { "CSC0010" } } },
263         /* Some noname CS4236 based card */
264         { .id = "CSCe936", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
265         /* CS4236B */
266         { .id = "CSCf235", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
267         /* CS4236B */
268         { .id = "CSCf238", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
269         /* --- */
270         { .id = "" }    /* end */
271 };
272 #endif
273
274 MODULE_DEVICE_TABLE(pnp_card, snd_cs423x_pnpids);
275
276 static int __devinit snd_card_cs4236_pnp(int dev, struct snd_card_cs4236 *acard,
277                                          struct pnp_card_link *card,
278                                          const struct pnp_card_device_id *id)
279 {
280         struct pnp_dev *pdev;
281         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
282         int err;
283
284         if (!cfg)
285                 return -ENOMEM;
286
287         acard->wss = pnp_request_card_device(card, id->devs[0].id, NULL);
288         if (acard->wss == NULL) {
289                 kfree(cfg);
290                 return -EBUSY;
291         }
292         acard->ctrl = pnp_request_card_device(card, id->devs[1].id, NULL);
293         if (acard->ctrl == NULL) {
294                 kfree(cfg);
295                 return -EBUSY;
296         }
297         if (id->devs[2].id[0]) {
298                 acard->mpu = pnp_request_card_device(card, id->devs[2].id, NULL);
299                 if (acard->mpu == NULL) {
300                         kfree(cfg);
301                         return -EBUSY;
302                 }
303         }
304
305         /* WSS initialization */
306         pdev = acard->wss;
307         pnp_init_resource_table(cfg);
308         if (port[dev] != SNDRV_AUTO_PORT)
309                 pnp_resource_change(&cfg->port_resource[0], port[dev], 4);
310         if (fm_port[dev] != SNDRV_AUTO_PORT && fm_port[dev] >= 0)
311                 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
312         if (sb_port[dev] != SNDRV_AUTO_PORT)
313                 pnp_resource_change(&cfg->port_resource[2], sb_port[dev], 16);
314         if (irq[dev] != SNDRV_AUTO_IRQ)
315                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
316         if (dma1[dev] != SNDRV_AUTO_DMA)
317                 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
318         if (dma2[dev] != SNDRV_AUTO_DMA)
319                 pnp_resource_change(&cfg->dma_resource[1], dma2[dev] < 0 ? 4 : dma2[dev], 1);
320         err = pnp_manual_config_dev(pdev, cfg, 0);
321         if (err < 0)
322                 snd_printk(KERN_ERR IDENT " WSS PnP manual resources are invalid, using auto config\n");
323         err = pnp_activate_dev(pdev);
324         if (err < 0) {
325                 kfree(cfg);
326                 printk(KERN_ERR IDENT " WSS PnP configure failed for WSS (out of resources?)\n");
327                 return -EBUSY;
328         }
329         port[dev] = pnp_port_start(pdev, 0);
330         if (fm_port[dev] >= 0)
331                 fm_port[dev] = pnp_port_start(pdev, 1);
332         sb_port[dev] = pnp_port_start(pdev, 2);
333         irq[dev] = pnp_irq(pdev, 0);
334         dma1[dev] = pnp_dma(pdev, 0);
335         dma2[dev] = pnp_dma(pdev, 1) == 4 ? -1 : (int)pnp_dma(pdev, 1);
336         snd_printdd("isapnp WSS: wss port=0x%lx, fm port=0x%lx, sb port=0x%lx\n",
337                         port[dev], fm_port[dev], sb_port[dev]);
338         snd_printdd("isapnp WSS: irq=%i, dma1=%i, dma2=%i\n",
339                         irq[dev], dma1[dev], dma2[dev]);
340         /* CTRL initialization */
341         if (acard->ctrl && cport[dev] >= 0) {
342                 pdev = acard->ctrl;
343                 pnp_init_resource_table(cfg);
344                 if (cport[dev] != SNDRV_AUTO_PORT)
345                         pnp_resource_change(&cfg->port_resource[0], cport[dev], 8);
346                 err = pnp_manual_config_dev(pdev, cfg, 0);
347                 if (err < 0)
348                         snd_printk(KERN_ERR IDENT " CTRL PnP manual resources are invalid, using auto config\n");
349                 err = pnp_activate_dev(pdev);
350                 if (err < 0) {
351                         kfree(cfg);
352                         printk(KERN_ERR IDENT " CTRL PnP configure failed for WSS (out of resources?)\n");
353                         return -EBUSY;
354                 }
355                 cport[dev] = pnp_port_start(pdev, 0);
356                 snd_printdd("isapnp CTRL: control port=0x%lx\n", cport[dev]);
357         }
358         /* MPU initialization */
359         if (acard->mpu && mpu_port[dev] >= 0) {
360                 pdev = acard->mpu;
361                 pnp_init_resource_table(cfg);
362                 if (mpu_port[dev] != SNDRV_AUTO_PORT)
363                         pnp_resource_change(&cfg->port_resource[0], mpu_port[dev], 2);
364                 if (mpu_irq[dev] != SNDRV_AUTO_IRQ && mpu_irq[dev] >= 0)
365                         pnp_resource_change(&cfg->irq_resource[0], mpu_irq[dev], 1);
366                 err = pnp_manual_config_dev(pdev, cfg, 0);
367                 if (err < 0)
368                         snd_printk(KERN_ERR IDENT " MPU401 PnP manual resources are invalid, using auto config\n");
369                 err = pnp_activate_dev(pdev);
370                 if (err < 0) {
371                         printk(KERN_ERR IDENT " MPU401 PnP configure failed for WSS (out of resources?)\n");
372                         mpu_port[dev] = SNDRV_AUTO_PORT;
373                         mpu_irq[dev] = SNDRV_AUTO_IRQ;
374                 } else {
375                         mpu_port[dev] = pnp_port_start(pdev, 0);
376                         if (pnp_irq_valid(pdev, 0) && pnp_irq(pdev, 0) >= 0) {
377                                 mpu_irq[dev] = pnp_irq(pdev, 0);
378                         } else {
379                                 mpu_irq[dev] = -1;      /* disable interrupt */
380                         }
381                 }
382                 snd_printdd("isapnp MPU: port=0x%lx, irq=%i\n", mpu_port[dev], mpu_irq[dev]);
383         }
384         kfree(cfg);
385         return 0;
386 }
387 #endif /* CONFIG_PNP */
388
389 static void snd_card_cs4236_free(snd_card_t *card)
390 {
391         struct snd_card_cs4236 *acard = (struct snd_card_cs4236 *)card->private_data;
392
393         if (acard) {
394                 if (acard->res_sb_port) {
395                         release_resource(acard->res_sb_port);
396                         kfree_nocheck(acard->res_sb_port);
397                 }
398         }
399 }
400
401 static int __devinit snd_card_cs423x_probe(int dev, struct pnp_card_link *pcard,
402                                            const struct pnp_card_device_id *pid)
403 {
404         snd_card_t *card;
405         struct snd_card_cs4236 *acard;
406         snd_pcm_t *pcm = NULL;
407         cs4231_t *chip;
408         opl3_t *opl3;
409         int err;
410
411 #ifdef CONFIG_PNP
412         if (!isapnp[dev]) {
413 #endif
414                 if (port[dev] == SNDRV_AUTO_PORT) {
415                         snd_printk("specify port\n");
416                         return -EINVAL;
417                 }
418                 if (cport[dev] == SNDRV_AUTO_PORT) {
419                         snd_printk("specify cport\n");
420                         return -EINVAL;
421                 }
422 #ifdef CONFIG_PNP
423         }
424 #endif
425         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
426                             sizeof(struct snd_card_cs4236));
427         if (card == NULL)
428                 return -ENOMEM;
429         acard = (struct snd_card_cs4236 *)card->private_data;
430         card->private_free = snd_card_cs4236_free;
431 #ifdef CONFIG_PNP
432         if (isapnp[dev] && (err = snd_card_cs4236_pnp(dev, acard, pcard, pid))<0) {
433                 printk(KERN_ERR "isapnp detection failed and probing for " IDENT " is not supported\n");
434                 snd_card_free(card);
435                 return -ENXIO;
436         }
437 #endif
438         if (mpu_port[dev] < 0)
439                 mpu_port[dev] = SNDRV_AUTO_PORT;
440         if (fm_port[dev] < 0)
441                 fm_port[dev] = SNDRV_AUTO_PORT;
442         if (sb_port[dev] < 0)
443                 sb_port[dev] = SNDRV_AUTO_PORT;
444         if (sb_port[dev] != SNDRV_AUTO_PORT)
445                 if ((acard->res_sb_port = request_region(sb_port[dev], 16, IDENT " SB")) == NULL) {
446                         printk(KERN_ERR IDENT ": unable to register SB port at 0x%lx\n", sb_port[dev]);
447                         snd_card_free(card);
448                         return -ENOMEM;
449                 }
450
451 #ifdef CS4232
452         if ((err = snd_cs4231_create(card,
453                                      port[dev],
454                                      cport[dev],
455                                      irq[dev],
456                                      dma1[dev],
457                                      dma2[dev],
458                                      CS4231_HW_DETECT,
459                                      0,
460                                      &chip)) < 0) {
461                 snd_card_free(card);
462                 return err;
463         }
464         if ((err = snd_cs4231_pcm(chip, 0, &pcm)) < 0) {
465                 snd_card_free(card);
466                 return err;
467         }
468         if ((err = snd_cs4231_mixer(chip)) < 0) {
469                 snd_card_free(card);
470                 return err;
471         }
472
473 #else /* CS4236 */
474         if ((err = snd_cs4236_create(card,
475                                      port[dev],
476                                      cport[dev],
477                                      irq[dev],
478                                      dma1[dev],
479                                      dma2[dev],
480                                      CS4231_HW_DETECT,
481                                      0,
482                                      &chip)) < 0) {
483                 snd_card_free(card);
484                 return err;
485         }
486         if ((err = snd_cs4236_pcm(chip, 0, &pcm)) < 0) {
487                 snd_card_free(card);
488                 return err;
489         }
490         if ((err = snd_cs4236_mixer(chip)) < 0) {
491                 snd_card_free(card);
492                 return err;
493         }
494 #endif
495
496         if ((err = snd_cs4231_timer(chip, 0, NULL)) < 0) {
497                 snd_card_free(card);
498                 return err;
499         }
500
501         if (fm_port[dev] != SNDRV_AUTO_PORT) {
502                 if (snd_opl3_create(card,
503                                     fm_port[dev], fm_port[dev] + 2,
504                                     OPL3_HW_OPL3_CS, 0, &opl3) < 0) {
505                         printk(KERN_ERR IDENT ": OPL3 not detected\n");
506                 } else {
507                         if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
508                                 snd_card_free(card);
509                                 return err;
510                         }
511                 }
512         }
513
514         if (mpu_port[dev] != SNDRV_AUTO_PORT) {
515                 if (snd_mpu401_uart_new(card, 0, MPU401_HW_CS4232,
516                                         mpu_port[dev], 0,
517                                         mpu_irq[dev],
518                                         mpu_irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL) < 0)
519                         printk(KERN_ERR IDENT ": MPU401 not detected\n");
520         }
521         strcpy(card->driver, pcm->name);
522         strcpy(card->shortname, pcm->name);
523         sprintf(card->longname, "%s at 0x%lx, irq %i, dma %i",
524                 pcm->name,
525                 chip->port,
526                 irq[dev],
527                 dma1[dev]);
528         if (dma1[dev] >= 0)
529                 sprintf(card->longname + strlen(card->longname), "&%d", dma2[dev]);
530         if ((err = snd_card_register(card)) < 0) {
531                 snd_card_free(card);
532                 return err;
533         }
534         if (pcard)
535                 pnp_set_card_drvdata(pcard, card);
536         else
537                 snd_cs4236_legacy[dev] = card;
538         return 0;
539 }
540
541 #ifdef CONFIG_PNP
542 static int __devinit snd_cs423x_pnp_detect(struct pnp_card_link *card,
543                                            const struct pnp_card_device_id *id)
544 {
545         static int dev;
546         int res;
547
548         for ( ; dev < SNDRV_CARDS; dev++) {
549                 if (!enable[dev])
550                         continue;
551                 res = snd_card_cs423x_probe(dev, card, id);
552                 if (res < 0)
553                         return res;
554                 dev++;
555                 return 0;
556         }
557         return -ENODEV;
558 }
559
560 static void __devexit snd_cs423x_pnp_remove(struct pnp_card_link * pcard)
561 {
562         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
563         
564         snd_card_disconnect(card);
565         snd_card_free_in_thread(card);
566 }
567                         
568 static struct pnp_card_driver cs423x_pnpc_driver = {
569         .flags = PNP_DRIVER_RES_DISABLE,
570         .name = "cs423x",
571         .id_table = snd_cs423x_pnpids,
572         .probe = snd_cs423x_pnp_detect,
573         .remove = __devexit_p(snd_cs423x_pnp_remove),
574 };
575 #endif /* CONFIG_PNP */
576
577 static int __init alsa_card_cs423x_init(void)
578 {
579         int dev, cards = 0;
580
581         for (dev = 0; dev < SNDRV_CARDS; dev++) {
582                 if (!enable[dev])
583                         continue;
584 #ifdef CONFIG_PNP
585                 if (isapnp[dev])
586                         continue;
587 #endif
588                 if (snd_card_cs423x_probe(dev, NULL, NULL) >= 0)
589                         cards++;
590         }
591 #ifdef CONFIG_PNP
592         cards += pnp_register_card_driver(&cs423x_pnpc_driver);
593 #endif
594         if (!cards) {
595 #ifdef CONFIG_PNP
596                 pnp_unregister_card_driver(&cs423x_pnpc_driver);
597 #endif
598 #ifdef MODULE
599                 printk(KERN_ERR IDENT " soundcard not found or device busy\n");
600 #endif
601                 return -ENODEV;
602         }
603         return 0;
604 }
605
606 static void __exit alsa_card_cs423x_exit(void)
607 {
608         int idx;
609
610 #ifdef CONFIG_PNP
611         /* PnP cards first */
612         pnp_unregister_card_driver(&cs423x_pnpc_driver);
613 #endif
614         for (idx = 0; idx < SNDRV_CARDS; idx++)
615                 snd_card_free(snd_cs4236_legacy[idx]);
616 }
617
618 module_init(alsa_card_cs423x_init)
619 module_exit(alsa_card_cs423x_exit)
620
621 #ifndef MODULE
622
623 /* format is: snd-cs4232=enable,index,id,isapnp,port,
624                          cport,mpu_port,fm_port,sb_port,
625                          irq,mpu_irq,dma1,dma2 */
626 /* format is: snd-cs4236=enable,index,id,isapnp,port,
627                          cport,mpu_port,fm_port,sb_port,
628                          irq,mpu_irq,dma1,dma2 */
629
630 static int __init alsa_card_cs423x_setup(char *str)
631 {
632         static unsigned __initdata nr_dev = 0;
633         int __attribute__ ((__unused__)) pnp = INT_MAX;
634
635         if (nr_dev >= SNDRV_CARDS)
636                 return 0;
637         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
638                get_option(&str,&index[nr_dev]) == 2 &&
639                get_id(&str,&id[nr_dev]) == 2 &&
640                get_option(&str,&pnp) == 2 &&
641                get_option(&str,(int *)&port[nr_dev]) == 2 &&
642                get_option(&str,(int *)&cport[nr_dev]) == 2 &&
643                get_option(&str,(int *)&mpu_port[nr_dev]) == 2 &&
644                get_option(&str,(int *)&fm_port[nr_dev]) == 2 &&
645                get_option(&str,(int *)&sb_port[nr_dev]) == 2 &&
646                get_option(&str,&irq[nr_dev]) == 2 &&
647                get_option(&str,&mpu_irq[nr_dev]) == 2 &&
648                get_option(&str,&dma1[nr_dev]) == 2 &&
649                get_option(&str,&dma2[nr_dev]) == 2);
650 #ifdef CONFIG_PNP
651         if (pnp != INT_MAX)
652                 isapnp[nr_dev] = pnp;
653 #endif
654         nr_dev++;
655         return 1;
656 }
657
658 #ifdef CS4232
659 __setup("snd-cs4232=", alsa_card_cs423x_setup);
660 #else /* CS4236 */
661 __setup("snd-cs4236=", alsa_card_cs423x_setup);
662 #endif
663
664 #endif /* ifndef MODULE */