Merge master.kernel.org:/home/rmk/linux-2.6-serial
[linux-2.6] / 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/err.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/pnp.h>
28 #include <linux/moduleparam.h>
29 #include <sound/core.h>
30 #include <sound/cs4231.h>
31 #include <sound/mpu401.h>
32 #include <sound/opl3.h>
33 #include <sound/initval.h>
34
35 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
36 MODULE_LICENSE("GPL");
37 #ifdef CS4232
38 MODULE_DESCRIPTION("Cirrus Logic CS4232");
39 MODULE_SUPPORTED_DEVICE("{{Turtle Beach,TBS-2000},"
40                 "{Turtle Beach,Tropez Plus},"
41                 "{SIC CrystalWave 32},"
42                 "{Hewlett Packard,Omnibook 5500},"
43                 "{TerraTec,Maestro 32/96},"
44                 "{Philips,PCA70PS}}");
45 #else
46 MODULE_DESCRIPTION("Cirrus Logic CS4235-9");
47 MODULE_SUPPORTED_DEVICE("{{Crystal Semiconductors,CS4235},"
48                 "{Crystal Semiconductors,CS4236},"
49                 "{Crystal Semiconductors,CS4237},"
50                 "{Crystal Semiconductors,CS4238},"
51                 "{Crystal Semiconductors,CS4239},"
52                 "{Acer,AW37},"
53                 "{Acer,AW35/Pro},"
54                 "{Crystal,3D},"
55                 "{Crystal Computer,TidalWave128},"
56                 "{Dell,Optiplex GX1},"
57                 "{Dell,Workstation 400 sound},"
58                 "{EliteGroup,P5TX-LA sound},"
59                 "{Gallant,SC-70P},"
60                 "{Gateway,E1000 Onboard CS4236B},"
61                 "{Genius,Sound Maker 3DJ},"
62                 "{Hewlett Packard,HP6330 sound},"
63                 "{IBM,PC 300PL sound},"
64                 "{IBM,Aptiva 2137 E24},"
65                 "{IBM,IntelliStation M Pro},"
66                 "{Intel,Marlin Spike Mobo CS4235},"
67                 "{Intel PR440FX Onboard},"
68                 "{Guillemot,MaxiSound 16 PnP},"
69                 "{NewClear,3D},"
70                 "{TerraTec,AudioSystem EWS64L/XL},"
71                 "{Typhoon Soundsystem,CS4236B},"
72                 "{Turtle Beach,Malibu},"
73                 "{Unknown,Digital PC 5000 Onboard}}");
74 #endif
75
76 #ifdef CS4232
77 #define IDENT "CS4232"
78 #else
79 #define IDENT "CS4236+"
80 #endif
81
82 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
83 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
84 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
85 #ifdef CONFIG_PNP
86 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
87 #endif
88 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* PnP setup */
89 static long cport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;    /* PnP setup */
90 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* PnP setup */
91 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* PnP setup */
92 static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* PnP setup */
93 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 5,7,9,11,12,15 */
94 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;    /* 9,11,12,15 */
95 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3,5,6,7 */
96 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3,5,6,7 */
97
98 module_param_array(index, int, NULL, 0444);
99 MODULE_PARM_DESC(index, "Index value for " IDENT " soundcard.");
100 module_param_array(id, charp, NULL, 0444);
101 MODULE_PARM_DESC(id, "ID string for " IDENT " soundcard.");
102 module_param_array(enable, bool, NULL, 0444);
103 MODULE_PARM_DESC(enable, "Enable " IDENT " soundcard.");
104 #ifdef CONFIG_PNP
105 module_param_array(isapnp, bool, NULL, 0444);
106 MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard.");
107 #endif
108 module_param_array(port, long, NULL, 0444);
109 MODULE_PARM_DESC(port, "Port # for " IDENT " driver.");
110 module_param_array(cport, long, NULL, 0444);
111 MODULE_PARM_DESC(cport, "Control port # for " IDENT " driver.");
112 module_param_array(mpu_port, long, NULL, 0444);
113 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " IDENT " driver.");
114 module_param_array(fm_port, long, NULL, 0444);
115 MODULE_PARM_DESC(fm_port, "FM port # for " IDENT " driver.");
116 module_param_array(sb_port, long, NULL, 0444);
117 MODULE_PARM_DESC(sb_port, "SB port # for " IDENT " driver (optional).");
118 module_param_array(irq, int, NULL, 0444);
119 MODULE_PARM_DESC(irq, "IRQ # for " IDENT " driver.");
120 module_param_array(mpu_irq, int, NULL, 0444);
121 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " IDENT " driver.");
122 module_param_array(dma1, int, NULL, 0444);
123 MODULE_PARM_DESC(dma1, "DMA1 # for " IDENT " driver.");
124 module_param_array(dma2, int, NULL, 0444);
125 MODULE_PARM_DESC(dma2, "DMA2 # for " IDENT " driver.");
126
127 static struct platform_device *platform_devices[SNDRV_CARDS];
128 static int pnpc_registered;
129 #ifdef CS4232
130 static int pnp_registered;
131 #endif
132
133 struct snd_card_cs4236 {
134         struct snd_cs4231 *chip;
135         struct resource *res_sb_port;
136 #ifdef CONFIG_PNP
137         struct pnp_dev *wss;
138         struct pnp_dev *ctrl;
139         struct pnp_dev *mpu;
140 #endif
141 };
142
143 #ifdef CONFIG_PNP
144
145 #ifdef CS4232
146 /*
147  * PNP BIOS
148  */
149 static const struct pnp_device_id snd_cs4232_pnpbiosids[] = {
150         { .id = "CSC0100" },
151         { .id = "CSC0000" },
152         /* Guillemot Turtlebeach something appears to be cs4232 compatible
153          * (untested) */
154         { .id = "GIM0100" },
155         { .id = "" }
156 };
157 MODULE_DEVICE_TABLE(pnp, snd_cs4232_pnpbiosids);
158 #endif /* CS4232 */
159
160 #ifdef CS4232
161 #define CS423X_DRIVER           "snd_cs4232"
162 #define CS423X_ISAPNP_DRIVER    "cs4232_isapnp"
163 static struct pnp_card_device_id snd_cs423x_pnpids[] = {
164         /* Philips PCA70PS */
165         { .id = "CSC0d32", .devs = { { "CSC0000" }, { "CSC0010" }, { "PNPb006" } } },
166         /* TerraTec Maestro 32/96 (CS4232) */
167         { .id = "CSC1a32", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
168         /* HP Omnibook 5500 onboard */
169         { .id = "CSC4232", .devs = { { "CSC0000" }, { "CSC0002" }, { "CSC0003" } } },
170         /* Unnamed CS4236 card (Made in Taiwan) */
171         { .id = "CSC4236", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
172         /* Turtle Beach TBS-2000 (CS4232) */
173         { .id = "CSC7532", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSCb006" } } },
174         /* Turtle Beach Tropez Plus (CS4232) */
175         { .id = "CSC7632", .devs = { { "CSC0000" }, { "CSC0010" }, { "PNPb006" } } },
176         /* SIC CrystalWave 32 (CS4232) */
177         { .id = "CSCf032", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
178         /* --- */
179         { .id = "" }    /* end */
180 };
181 #else /* CS4236 */
182 #define CS423X_DRIVER           "snd_cs4236"
183 #define CS423X_ISAPNP_DRIVER    "cs4236_isapnp"
184 static struct pnp_card_device_id snd_cs423x_pnpids[] = {
185         /* Intel Marlin Spike Motherboard - CS4235 */
186         { .id = "CSC0225", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
187         /* Intel Marlin Spike Motherboard (#2) - CS4235 */
188         { .id = "CSC0225", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } },
189         /* Unknown Intel mainboard - CS4235 */
190         { .id = "CSC0225", .devs = { { "CSC0100" }, { "CSC0110" } } },
191         /* Genius Sound Maker 3DJ - CS4237B */
192         { .id = "CSC0437", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
193         /* Digital PC 5000 Onboard - CS4236B */
194         { .id = "CSC0735", .devs = { { "CSC0000" }, { "CSC0010" } } },
195         /* some uknown CS4236B */
196         { .id = "CSC0b35", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
197         /* Intel PR440FX Onboard sound */
198         { .id = "CSC0b36", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
199         /* CS4235 on mainboard without MPU */
200         { .id = "CSC1425", .devs = { { "CSC0100" }, { "CSC0110" } } },
201         /* Gateway E1000 Onboard CS4236B */
202         { .id = "CSC1335", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
203         /* HP 6330 Onboard sound */
204         { .id = "CSC1525", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } },
205         /* Crystal Computer TidalWave128 */
206         { .id = "CSC1e37", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
207         /* ACER AW37 - CS4235 */
208         { .id = "CSC4236", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
209         /* build-in soundcard in EliteGroup P5TX-LA motherboard - CS4237B */
210         { .id = "CSC4237", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
211         /* Crystal 3D - CS4237B */
212         { .id = "CSC4336", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
213         /* Typhoon Soundsystem PnP - CS4236B */
214         { .id = "CSC4536", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
215         /* Crystal CX4235-XQ3 EP - CS4235 */
216         { .id = "CSC4625", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } },
217         /* Crystal Semiconductors CS4237B */
218         { .id = "CSC4637", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
219         /* NewClear 3D - CX4237B-XQ3 */
220         { .id = "CSC4837", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
221         /* Dell Optiplex GX1 - CS4236B */
222         { .id = "CSC6835", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
223         /* Dell P410 motherboard - CS4236B */
224         { .id = "CSC6835", .devs = { { "CSC0000" }, { "CSC0010" } } },
225         /* Dell Workstation 400 Onboard - CS4236B */
226         { .id = "CSC6836", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
227         /* Turtle Beach Malibu - CS4237B */
228         { .id = "CSC7537", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
229         /* CS4235 - onboard */
230         { .id = "CSC8025", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } },
231         /* IBM Aptiva 2137 E24 Onboard - CS4237B */
232         { .id = "CSC8037", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
233         /* IBM IntelliStation M Pro motherboard */
234         { .id = "CSCc835", .devs = { { "CSC0000" }, { "CSC0010" } } },
235         /* Guillemot MaxiSound 16 PnP - CS4236B */
236         { .id = "CSC9836", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
237         /* Gallant SC-70P */
238         { .id = "CSC9837", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
239         /* TerraTec AudioSystem EWS64XL - CS4236B */
240         { .id = "CSCa836", .devs = { { "CSCa800" }, { "CSCa810" }, { "CSCa803" } } },
241         /* TerraTec AudioSystem EWS64XL - CS4236B */
242         { .id = "CSCa836", .devs = { { "CSCa800" }, { "CSCa810" } } },
243         /* ACER AW37/Pro - CS4235 */
244         { .id = "CSCd925", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
245         /* ACER AW35/Pro - CS4237B */
246         { .id = "CSCd937", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
247         /* CS4235 without MPU401 */
248         { .id = "CSCe825", .devs = { { "CSC0100" }, { "CSC0110" } } },
249         /* Unknown SiS530 - CS4235 */
250         { .id = "CSC4825", .devs = { { "CSC0100" }, { "CSC0110" } } },
251         /* IBM IntelliStation M Pro 6898 11U - CS4236B */
252         { .id = "CSCe835", .devs = { { "CSC0000" }, { "CSC0010" } } },
253         /* IBM PC 300PL Onboard - CS4236B */
254         { .id = "CSCe836", .devs = { { "CSC0000" }, { "CSC0010" } } },
255         /* Some noname CS4236 based card */
256         { .id = "CSCe936", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
257         /* CS4236B */
258         { .id = "CSCf235", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
259         /* CS4236B */
260         { .id = "CSCf238", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } },
261         /* --- */
262         { .id = "" }    /* end */
263 };
264 #endif
265
266 MODULE_DEVICE_TABLE(pnp_card, snd_cs423x_pnpids);
267
268 /* WSS initialization */
269 static int __devinit snd_cs423x_pnp_init_wss(int dev, struct pnp_dev *pdev,
270                                              struct pnp_resource_table *cfg)
271 {
272         int err;
273
274         pnp_init_resource_table(cfg);
275         if (port[dev] != SNDRV_AUTO_PORT)
276                 pnp_resource_change(&cfg->port_resource[0], port[dev], 4);
277         if (fm_port[dev] != SNDRV_AUTO_PORT && fm_port[dev] > 0)
278                 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
279         if (sb_port[dev] != SNDRV_AUTO_PORT)
280                 pnp_resource_change(&cfg->port_resource[2], sb_port[dev], 16);
281         if (irq[dev] != SNDRV_AUTO_IRQ)
282                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
283         if (dma1[dev] != SNDRV_AUTO_DMA)
284                 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
285         if (dma2[dev] != SNDRV_AUTO_DMA)
286                 pnp_resource_change(&cfg->dma_resource[1], dma2[dev] < 0 ? 4 : dma2[dev], 1);
287         err = pnp_manual_config_dev(pdev, cfg, 0);
288         if (err < 0)
289                 snd_printk(KERN_ERR IDENT " WSS PnP manual resources are invalid, using auto config\n");
290         err = pnp_activate_dev(pdev);
291         if (err < 0) {
292                 printk(KERN_ERR IDENT " WSS PnP configure failed for WSS (out of resources?)\n");
293                 return -EBUSY;
294         }
295         port[dev] = pnp_port_start(pdev, 0);
296         if (fm_port[dev] > 0)
297                 fm_port[dev] = pnp_port_start(pdev, 1);
298         sb_port[dev] = pnp_port_start(pdev, 2);
299         irq[dev] = pnp_irq(pdev, 0);
300         dma1[dev] = pnp_dma(pdev, 0);
301         dma2[dev] = pnp_dma(pdev, 1) == 4 ? -1 : (int)pnp_dma(pdev, 1);
302         snd_printdd("isapnp WSS: wss port=0x%lx, fm port=0x%lx, sb port=0x%lx\n",
303                         port[dev], fm_port[dev], sb_port[dev]);
304         snd_printdd("isapnp WSS: irq=%i, dma1=%i, dma2=%i\n",
305                         irq[dev], dma1[dev], dma2[dev]);
306         return 0;
307 }
308
309 /* CTRL initialization */
310 static int __devinit snd_cs423x_pnp_init_ctrl(int dev, struct pnp_dev *pdev,
311                                               struct pnp_resource_table *cfg)
312 {
313         int err;
314
315         pnp_init_resource_table(cfg);
316         if (cport[dev] != SNDRV_AUTO_PORT)
317                 pnp_resource_change(&cfg->port_resource[0], cport[dev], 8);
318         err = pnp_manual_config_dev(pdev, cfg, 0);
319         if (err < 0)
320                 snd_printk(KERN_ERR IDENT " CTRL PnP manual resources are invalid, using auto config\n");
321         err = pnp_activate_dev(pdev);
322         if (err < 0) {
323                 printk(KERN_ERR IDENT " CTRL PnP configure failed for WSS (out of resources?)\n");
324                 return -EBUSY;
325         }
326         cport[dev] = pnp_port_start(pdev, 0);
327         snd_printdd("isapnp CTRL: control port=0x%lx\n", cport[dev]);
328         return 0;
329 }
330
331 /* MPU initialization */
332 static int __devinit snd_cs423x_pnp_init_mpu(int dev, struct pnp_dev *pdev,
333                                              struct pnp_resource_table *cfg)
334 {
335         int err;
336
337         pnp_init_resource_table(cfg);
338         if (mpu_port[dev] != SNDRV_AUTO_PORT)
339                 pnp_resource_change(&cfg->port_resource[0], mpu_port[dev], 2);
340         if (mpu_irq[dev] != SNDRV_AUTO_IRQ && mpu_irq[dev] >= 0)
341                 pnp_resource_change(&cfg->irq_resource[0], mpu_irq[dev], 1);
342         err = pnp_manual_config_dev(pdev, cfg, 0);
343         if (err < 0)
344                 snd_printk(KERN_ERR IDENT " MPU401 PnP manual resources are invalid, using auto config\n");
345         err = pnp_activate_dev(pdev);
346         if (err < 0) {
347                 printk(KERN_ERR IDENT " MPU401 PnP configure failed for WSS (out of resources?)\n");
348                 mpu_port[dev] = SNDRV_AUTO_PORT;
349                 mpu_irq[dev] = SNDRV_AUTO_IRQ;
350         } else {
351                 mpu_port[dev] = pnp_port_start(pdev, 0);
352                 if (mpu_irq[dev] >= 0 &&
353                     pnp_irq_valid(pdev, 0) && pnp_irq(pdev, 0) >= 0) {
354                         mpu_irq[dev] = pnp_irq(pdev, 0);
355                 } else {
356                         mpu_irq[dev] = -1;      /* disable interrupt */
357                 }
358         }
359         snd_printdd("isapnp MPU: port=0x%lx, irq=%i\n", mpu_port[dev], mpu_irq[dev]);
360         return 0;
361 }
362
363 #ifdef CS4232
364 static int __devinit snd_card_cs4232_pnp(int dev, struct snd_card_cs4236 *acard,
365                                          struct pnp_dev *pdev)
366 {
367         struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
368
369         if (!cfg)
370                 return -ENOMEM;
371         if (snd_cs423x_pnp_init_wss(dev, acard->wss, cfg) < 0) {
372                 kfree(cfg);
373                 return -EBUSY;
374         }
375         kfree(cfg);
376         cport[dev] = -1;
377         return 0;
378 }
379 #endif
380
381 static int __devinit snd_card_cs423x_pnpc(int dev, struct snd_card_cs4236 *acard,
382                                           struct pnp_card_link *card,
383                                           const struct pnp_card_device_id *id)
384 {
385         struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
386
387         if (!cfg)
388                 return -ENOMEM;
389
390         acard->wss = pnp_request_card_device(card, id->devs[0].id, NULL);
391         if (acard->wss == NULL)
392                 goto error;
393         acard->ctrl = pnp_request_card_device(card, id->devs[1].id, NULL);
394         if (acard->ctrl == NULL)
395                 goto error;
396         if (id->devs[2].id[0]) {
397                 acard->mpu = pnp_request_card_device(card, id->devs[2].id, NULL);
398                 if (acard->mpu == NULL)
399                         goto error;
400         }
401
402         /* WSS initialization */
403         if (snd_cs423x_pnp_init_wss(dev, acard->wss, cfg) < 0)
404                 goto error;
405
406         /* CTRL initialization */
407         if (acard->ctrl && cport[dev] > 0) {
408                 if (snd_cs423x_pnp_init_ctrl(dev, acard->ctrl, cfg) < 0)
409                         goto error;
410         }
411         /* MPU initialization */
412         if (acard->mpu && mpu_port[dev] > 0) {
413                 if (snd_cs423x_pnp_init_mpu(dev, acard->ctrl, cfg) < 0)
414                         goto error;
415         }
416         kfree(cfg);
417         return 0;
418
419  error:
420         kfree(cfg);
421         return -EBUSY;
422 }
423 #endif /* CONFIG_PNP */
424
425 #ifdef CONFIG_PNP
426 #define is_isapnp_selected(dev)         isapnp[dev]
427 #else
428 #define is_isapnp_selected(dev)         0
429 #endif
430
431 static void snd_card_cs4236_free(struct snd_card *card)
432 {
433         struct snd_card_cs4236 *acard = card->private_data;
434
435         release_and_free_resource(acard->res_sb_port);
436 }
437
438 static struct snd_card *snd_cs423x_card_new(int dev)
439 {
440         struct snd_card *card;
441
442         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
443                             sizeof(struct snd_card_cs4236));
444         if (card == NULL)
445                 return NULL;
446         card->private_free = snd_card_cs4236_free;
447         return card;
448 }
449
450 static int __devinit snd_cs423x_probe(struct snd_card *card, int dev)
451 {
452         struct snd_card_cs4236 *acard;
453         struct snd_pcm *pcm;
454         struct snd_cs4231 *chip;
455         struct snd_opl3 *opl3;
456         int err;
457
458         acard = card->private_data;
459         if (sb_port[dev] > 0 && sb_port[dev] != SNDRV_AUTO_PORT)
460                 if ((acard->res_sb_port = request_region(sb_port[dev], 16, IDENT " SB")) == NULL) {
461                         printk(KERN_ERR IDENT ": unable to register SB port at 0x%lx\n", sb_port[dev]);
462                         return -EBUSY;
463                 }
464
465 #ifdef CS4232
466         if ((err = snd_cs4231_create(card,
467                                      port[dev],
468                                      cport[dev],
469                                      irq[dev],
470                                      dma1[dev],
471                                      dma2[dev],
472                                      CS4231_HW_DETECT,
473                                      0,
474                                      &chip)) < 0)
475                 return err;
476         acard->chip = chip;
477
478         if ((err = snd_cs4231_pcm(chip, 0, &pcm)) < 0)
479                 return err;
480
481         if ((err = snd_cs4231_mixer(chip)) < 0)
482                 return err;
483
484 #else /* CS4236 */
485         if ((err = snd_cs4236_create(card,
486                                      port[dev],
487                                      cport[dev],
488                                      irq[dev],
489                                      dma1[dev],
490                                      dma2[dev],
491                                      CS4231_HW_DETECT,
492                                      0,
493                                      &chip)) < 0)
494                 return err;
495         acard->chip = chip;
496
497         if ((err = snd_cs4236_pcm(chip, 0, &pcm)) < 0)
498                 return err;
499
500         if ((err = snd_cs4236_mixer(chip)) < 0)
501                 return err;
502 #endif
503         strcpy(card->driver, pcm->name);
504         strcpy(card->shortname, pcm->name);
505         sprintf(card->longname, "%s at 0x%lx, irq %i, dma %i",
506                 pcm->name,
507                 chip->port,
508                 irq[dev],
509                 dma1[dev]);
510         if (dma2[dev] >= 0)
511                 sprintf(card->longname + strlen(card->longname), "&%d", dma2[dev]);
512
513         if ((err = snd_cs4231_timer(chip, 0, NULL)) < 0)
514                 return err;
515
516         if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
517                 if (snd_opl3_create(card,
518                                     fm_port[dev], fm_port[dev] + 2,
519                                     OPL3_HW_OPL3_CS, 0, &opl3) < 0) {
520                         printk(KERN_WARNING IDENT ": OPL3 not detected\n");
521                 } else {
522                         if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
523                                 return err;
524                 }
525         }
526
527         if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
528                 if (mpu_irq[dev] == SNDRV_AUTO_IRQ)
529                         mpu_irq[dev] = -1;
530                 if (snd_mpu401_uart_new(card, 0, MPU401_HW_CS4232,
531                                         mpu_port[dev], 0,
532                                         mpu_irq[dev],
533                                         mpu_irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL) < 0)
534                         printk(KERN_WARNING IDENT ": MPU401 not detected\n");
535         }
536
537         return snd_card_register(card);
538 }
539
540 static int __init snd_cs423x_nonpnp_probe(struct platform_device *pdev)
541 {
542         int dev = pdev->id;
543         struct snd_card *card;
544         int err;
545
546         if (port[dev] == SNDRV_AUTO_PORT) {
547                 snd_printk(KERN_ERR "specify port\n");
548                 return -EINVAL;
549         }
550         if (cport[dev] == SNDRV_AUTO_PORT) {
551                 snd_printk(KERN_ERR "specify cport\n");
552                 return -EINVAL;
553         }
554
555         card = snd_cs423x_card_new(dev);
556         if (! card)
557                 return -ENOMEM;
558         snd_card_set_dev(card, &pdev->dev);
559         if ((err = snd_cs423x_probe(card, dev)) < 0) {
560                 snd_card_free(card);
561                 return err;
562         }
563         
564         platform_set_drvdata(pdev, card);
565         return 0;
566 }
567
568 static int __devexit snd_cs423x_nonpnp_remove(struct platform_device *devptr)
569 {
570         snd_card_free(platform_get_drvdata(devptr));
571         platform_set_drvdata(devptr, NULL);
572         return 0;
573 }
574
575 #ifdef CONFIG_PM
576 static int snd_cs423x_suspend(struct snd_card *card)
577 {
578         struct snd_card_cs4236 *acard = card->private_data;
579         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
580         acard->chip->suspend(acard->chip);
581         return 0;
582 }
583
584 static int snd_cs423x_resume(struct snd_card *card)
585 {
586         struct snd_card_cs4236 *acard = card->private_data;
587         acard->chip->resume(acard->chip);
588         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
589         return 0;
590 }
591
592 static int snd_cs423x_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
593 {
594         return snd_cs423x_suspend(platform_get_drvdata(dev));
595 }
596
597 static int snd_cs423x_nonpnp_resume(struct platform_device *dev)
598 {
599         return snd_cs423x_resume(platform_get_drvdata(dev));
600 }
601 #endif
602
603 static struct platform_driver cs423x_nonpnp_driver = {
604         .probe          = snd_cs423x_nonpnp_probe,
605         .remove         = __devexit_p(snd_cs423x_nonpnp_remove),
606 #ifdef CONFIG_PM
607         .suspend        = snd_cs423x_nonpnp_suspend,
608         .resume         = snd_cs423x_nonpnp_resume,
609 #endif
610         .driver         = {
611                 .name   = CS423X_DRIVER
612         },
613 };
614
615
616 #ifdef CONFIG_PNP
617 #ifdef CS4232
618 static int __devinit snd_cs4232_pnpbios_detect(struct pnp_dev *pdev,
619                                                const struct pnp_device_id *id)
620 {
621         static int dev;
622         int err;
623         struct snd_card *card;
624
625         if (pnp_device_is_isapnp(pdev))
626                 return -ENOENT; /* we have another procedure - card */
627         for (; dev < SNDRV_CARDS; dev++) {
628                 if (enable[dev] && isapnp[dev])
629                         break;
630         }
631         if (dev >= SNDRV_CARDS)
632                 return -ENODEV;
633
634         card = snd_cs423x_card_new(dev);
635         if (! card)
636                 return -ENOMEM;
637         if ((err = snd_card_cs4232_pnp(dev, card->private_data, pdev)) < 0) {
638                 printk(KERN_ERR "PnP BIOS detection failed for " IDENT "\n");
639                 snd_card_free(card);
640                 return err;
641         }
642         snd_card_set_dev(card, &pdev->dev);
643         if ((err = snd_cs423x_probe(card, dev)) < 0) {
644                 snd_card_free(card);
645                 return err;
646         }
647         pnp_set_drvdata(pdev, card);
648         dev++;
649         return 0;
650 }
651
652 static void __devexit snd_cs4232_pnp_remove(struct pnp_dev * pdev)
653 {
654         snd_card_free(pnp_get_drvdata(pdev));
655         pnp_set_drvdata(pdev, NULL);
656 }
657
658 #ifdef CONFIG_PM
659 static int snd_cs4232_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
660 {
661         return snd_cs423x_suspend(pnp_get_drvdata(pdev));
662 }
663
664 static int snd_cs4232_pnp_resume(struct pnp_dev *pdev)
665 {
666         return snd_cs423x_resume(pnp_get_drvdata(pdev));
667 }
668 #endif
669
670 static struct pnp_driver cs4232_pnp_driver = {
671         .name = "cs4232-pnpbios",
672         .id_table = snd_cs4232_pnpbiosids,
673         .probe = snd_cs4232_pnpbios_detect,
674         .remove = __devexit_p(snd_cs4232_pnp_remove),
675 #ifdef CONFIG_PM
676         .suspend        = snd_cs4232_pnp_suspend,
677         .resume         = snd_cs4232_pnp_resume,
678 #endif
679 };
680 #endif /* CS4232 */
681
682 static int __devinit snd_cs423x_pnpc_detect(struct pnp_card_link *pcard,
683                                             const struct pnp_card_device_id *pid)
684 {
685         static int dev;
686         struct snd_card *card;
687         int res;
688
689         for ( ; dev < SNDRV_CARDS; dev++) {
690                 if (enable[dev] && isapnp[dev])
691                         break;
692         }
693         if (dev >= SNDRV_CARDS)
694                 return -ENODEV;
695
696         card = snd_cs423x_card_new(dev);
697         if (! card)
698                 return -ENOMEM;
699         if ((res = snd_card_cs423x_pnpc(dev, card->private_data, pcard, pid)) < 0) {
700                 printk(KERN_ERR "isapnp detection failed and probing for " IDENT
701                        " is not supported\n");
702                 snd_card_free(card);
703                 return res;
704         }
705         snd_card_set_dev(card, &pcard->card->dev);
706         if ((res = snd_cs423x_probe(card, dev)) < 0) {
707                 snd_card_free(card);
708                 return res;
709         }
710         pnp_set_card_drvdata(pcard, card);
711         dev++;
712         return 0;
713 }
714
715 static void __devexit snd_cs423x_pnpc_remove(struct pnp_card_link * pcard)
716 {
717         snd_card_free(pnp_get_card_drvdata(pcard));
718         pnp_set_card_drvdata(pcard, NULL);
719 }
720                         
721 #ifdef CONFIG_PM
722 static int snd_cs423x_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
723 {
724         return snd_cs423x_suspend(pnp_get_card_drvdata(pcard));
725 }
726
727 static int snd_cs423x_pnpc_resume(struct pnp_card_link *pcard)
728 {
729         return snd_cs423x_resume(pnp_get_card_drvdata(pcard));
730 }
731 #endif
732
733 static struct pnp_card_driver cs423x_pnpc_driver = {
734         .flags = PNP_DRIVER_RES_DISABLE,
735         .name = CS423X_ISAPNP_DRIVER,
736         .id_table = snd_cs423x_pnpids,
737         .probe = snd_cs423x_pnpc_detect,
738         .remove = __devexit_p(snd_cs423x_pnpc_remove),
739 #ifdef CONFIG_PM
740         .suspend        = snd_cs423x_pnpc_suspend,
741         .resume         = snd_cs423x_pnpc_resume,
742 #endif
743 };
744 #endif /* CONFIG_PNP */
745
746 static void __init_or_module snd_cs423x_unregister_all(void)
747 {
748         int i;
749
750         if (pnpc_registered)
751                 pnp_unregister_card_driver(&cs423x_pnpc_driver);
752 #ifdef CS4232
753         if (pnp_registered)
754                 pnp_unregister_driver(&cs4232_pnp_driver);
755 #endif
756         for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
757                 platform_device_unregister(platform_devices[i]);
758         platform_driver_unregister(&cs423x_nonpnp_driver);
759 }
760
761 static int __init alsa_card_cs423x_init(void)
762 {
763         int i, err, cards = 0;
764
765         if ((err = platform_driver_register(&cs423x_nonpnp_driver)) < 0)
766                 return err;
767
768         for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
769                 struct platform_device *device;
770                 if (is_isapnp_selected(i))
771                         continue;
772                 device = platform_device_register_simple(CS423X_DRIVER,
773                                                          i, NULL, 0);
774                 if (IS_ERR(device)) {
775                         err = PTR_ERR(device);
776                         goto errout;
777                 }
778                 platform_devices[i] = device;
779                 cards++;
780         }
781 #ifdef CS4232
782         i = pnp_register_driver(&cs4232_pnp_driver);
783         if (i >= 0) {
784                 pnp_registered = 1;
785                 cards += i;
786         }
787 #endif
788         i = pnp_register_card_driver(&cs423x_pnpc_driver);
789         if (i >= 0) {
790                 pnpc_registered = 1;
791                 cards += i;
792         }
793         if (!cards) {
794 #ifdef MODULE
795                 printk(KERN_ERR IDENT " soundcard not found or device busy\n");
796 #endif
797                 err = -ENODEV;
798                 goto errout;
799         }
800         return 0;
801
802  errout:
803         snd_cs423x_unregister_all();
804         return err;
805 }
806
807 static void __exit alsa_card_cs423x_exit(void)
808 {
809         snd_cs423x_unregister_all();
810 }
811
812 module_init(alsa_card_cs423x_init)
813 module_exit(alsa_card_cs423x_exit)