patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / isa / opti9xx / opti92x-ad1848.c
1 /*
2     card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
3     Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
4
5     Part of this code was developed at the Italian Ministry of Air Defence,
6     Sixth Division (oh, che pace ...), Rome.
7
8     Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
9
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23 */
24
25
26 #include <sound/driver.h>
27 #include <asm/io.h>
28 #include <asm/dma.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/pnp.h>
33 #include <linux/moduleparam.h>
34 #include <sound/core.h>
35 #ifdef CS4231
36 #include <sound/cs4231.h>
37 #else
38 #ifndef OPTi93X
39 #include <sound/ad1848.h>
40 #else
41 #include <sound/control.h>
42 #include <sound/pcm.h>
43 #endif  /* OPTi93X */
44 #endif  /* CS4231 */
45 #include <sound/mpu401.h>
46 #include <sound/opl3.h>
47 #ifndef OPTi93X
48 #include <sound/opl4.h>
49 #endif
50 #define SNDRV_LEGACY_FIND_FREE_IRQ
51 #define SNDRV_LEGACY_FIND_FREE_DMA
52 #include <sound/initval.h>
53
54 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
55 MODULE_CLASSES("{sound}");
56 MODULE_LICENSE("GPL");
57 #ifdef OPTi93X
58 MODULE_DESCRIPTION("OPTi93X");
59 MODULE_DEVICES("{{OPTi,82C931/3}}");
60 #else   /* OPTi93X */
61 #ifdef CS4231
62 MODULE_DESCRIPTION("OPTi92X - CS4231");
63 MODULE_DEVICES("{{OPTi,82C924 (CS4231)},"
64                 "{OPTi,82C925 (CS4231)}}");
65 #else   /* CS4231 */
66 MODULE_DESCRIPTION("OPTi92X - AD1848");
67 MODULE_DEVICES("{{OPTi,82C924 (AD1848)},"
68                 "{OPTi,82C925 (AD1848)},"
69                 "{OAK,Mozart}}");
70 #endif  /* CS4231 */
71 #endif  /* OPTi93X */
72
73 static int index = SNDRV_DEFAULT_IDX1;  /* Index 0-MAX */
74 static char *id = SNDRV_DEFAULT_STR1;           /* ID for this card */
75 //static int enable = SNDRV_DEFAULT_ENABLE1;    /* Enable this card */
76 static int isapnp = 1;                  /* Enable ISA PnP detection */
77 static long port = SNDRV_DEFAULT_PORT1;         /* 0x530,0xe80,0xf40,0x604 */
78 static long mpu_port = SNDRV_DEFAULT_PORT1;     /* 0x300,0x310,0x320,0x330 */
79 static long fm_port = SNDRV_DEFAULT_PORT1;      /* 0x388 */
80 static int irq = SNDRV_DEFAULT_IRQ1;            /* 5,7,9,10,11 */
81 static int mpu_irq = SNDRV_DEFAULT_IRQ1;        /* 5,7,9,10 */
82 static int dma1 = SNDRV_DEFAULT_DMA1;           /* 0,1,3 */
83 #if defined(CS4231) || defined(OPTi93X)
84 static int dma2 = SNDRV_DEFAULT_DMA1;           /* 0,1,3 */
85 #endif  /* CS4231 || OPTi93X */
86
87 module_param(index, int, 0444);
88 MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
89 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
90 module_param(id, charp, 0444);
91 MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
92 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
93 //module_param(enable, bool, 0444);
94 //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
95 //MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
96 module_param(isapnp, bool, 0444);
97 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
98 MODULE_PARM_SYNTAX(isapnp, SNDRV_ISAPNP_DESC);
99 module_param(port, long, 0444);
100 MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
101 MODULE_PARM_SYNTAX(port, SNDRV_PORT_DESC);
102 module_param(mpu_port, long, 0444);
103 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
104 MODULE_PARM_SYNTAX(mpu_port, SNDRV_PORT_DESC);
105 module_param(fm_port, long, 0444);
106 MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
107 MODULE_PARM_SYNTAX(fm_port, SNDRV_PORT_DESC);
108 module_param(irq, int, 0444);
109 MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
110 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
111 module_param(mpu_irq, int, 0444);
112 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
113 MODULE_PARM_SYNTAX(mpu_irq, SNDRV_IRQ_DESC);
114 module_param(dma1, int, 0444);
115 MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
116 MODULE_PARM_SYNTAX(dma1, SNDRV_DMA_DESC);
117 #if defined(CS4231) || defined(OPTi93X)
118 module_param(dma2, int, 0444);
119 MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
120 MODULE_PARM_SYNTAX(dma2, SNDRV_DMA_DESC);
121 #endif  /* CS4231 || OPTi93X */
122
123 #define OPTi9XX_HW_DETECT       0
124 #define OPTi9XX_HW_82C928       1
125 #define OPTi9XX_HW_82C929       2
126 #define OPTi9XX_HW_82C924       3
127 #define OPTi9XX_HW_82C925       4
128 #define OPTi9XX_HW_82C930       5
129 #define OPTi9XX_HW_82C931       6
130 #define OPTi9XX_HW_82C933       7
131 #define OPTi9XX_HW_LAST         OPTi9XX_HW_82C933
132
133 #define OPTi9XX_MC_REG(n)       n
134
135 typedef struct _snd_opti9xx opti9xx_t;
136
137 #ifdef OPTi93X
138
139 #define OPTi93X_INDEX                   0x00
140 #define OPTi93X_DATA                    0x01
141 #define OPTi93X_STATUS                  0x02
142 #define OPTi93X_DDATA                   0x03
143 #define OPTi93X_PORT(chip, r)           ((chip)->port + OPTi93X_##r)
144
145 #define OPTi93X_MIXOUT_LEFT             0x00
146 #define OPTi93X_MIXOUT_RIGHT            0x01
147 #define OPTi93X_CD_LEFT_INPUT           0x02
148 #define OPTi93X_CD_RIGHT_INPUT          0x03
149 #define OPTi930_AUX_LEFT_INPUT          0x04
150 #define OPTi930_AUX_RIGHT_INPUT         0x05
151 #define OPTi931_FM_LEFT_INPUT           0x04
152 #define OPTi931_FM_RIGHT_INPUT          0x05
153 #define OPTi93X_DAC_LEFT                0x06
154 #define OPTi93X_DAC_RIGHT               0x07
155 #define OPTi93X_PLAY_FORMAT             0x08
156 #define OPTi93X_IFACE_CONF              0x09
157 #define OPTi93X_PIN_CTRL                0x0a
158 #define OPTi93X_ERR_INIT                0x0b
159 #define OPTi93X_ID                      0x0c
160 #define OPTi93X_PLAY_UPR_CNT            0x0e
161 #define OPTi93X_PLAY_LWR_CNT            0x0f
162 #define OPTi931_AUX_LEFT_INPUT          0x10
163 #define OPTi931_AUX_RIGHT_INPUT         0x11
164 #define OPTi93X_LINE_LEFT_INPUT         0x12
165 #define OPTi93X_LINE_RIGHT_INPUT        0x13
166 #define OPTi93X_MIC_LEFT_INPUT          0x14
167 #define OPTi93X_MIC_RIGHT_INPUT         0x15
168 #define OPTi93X_OUT_LEFT                0x16
169 #define OPTi93X_OUT_RIGHT               0x17
170 #define OPTi93X_CAPT_FORMAT             0x1c
171 #define OPTi93X_CAPT_UPR_CNT            0x1e
172 #define OPTi93X_CAPT_LWR_CNT            0x1f
173
174 #define OPTi93X_TRD                     0x20
175 #define OPTi93X_MCE                     0x40
176 #define OPTi93X_INIT                    0x80
177
178 #define OPTi93X_MIXOUT_MIC_GAIN         0x20
179 #define OPTi93X_MIXOUT_LINE             0x00
180 #define OPTi93X_MIXOUT_CD               0x40
181 #define OPTi93X_MIXOUT_MIC              0x80
182 #define OPTi93X_MIXOUT_MIXER            0xc0
183
184 #define OPTi93X_STEREO                  0x10
185 #define OPTi93X_LINEAR_8                0x00
186 #define OPTi93X_ULAW_8                  0x20
187 #define OPTi93X_LINEAR_16_LIT           0x40
188 #define OPTi93X_ALAW_8                  0x60
189 #define OPTi93X_ADPCM_16                0xa0
190 #define OPTi93X_LINEAR_16_BIG           0xc0
191
192 #define OPTi93X_CAPTURE_PIO             0x80
193 #define OPTi93X_PLAYBACK_PIO            0x40
194 #define OPTi93X_AUTOCALIB               0x08
195 #define OPTi93X_SINGLE_DMA              0x04
196 #define OPTi93X_CAPTURE_ENABLE          0x02
197 #define OPTi93X_PLAYBACK_ENABLE         0x01
198
199 #define OPTi93X_IRQ_ENABLE              0x02
200
201 #define OPTi93X_DMA_REQUEST             0x10
202 #define OPTi93X_CALIB_IN_PROGRESS       0x20
203
204 #define OPTi93X_IRQ_PLAYBACK            0x04
205 #define OPTi93X_IRQ_CAPTURE             0x08
206
207
208 typedef struct _snd_opti93x opti93x_t;
209
210 struct _snd_opti93x {
211         unsigned long port;
212         struct resource *res_port;
213         int irq;
214         int dma1;
215         int dma2;
216
217         opti9xx_t *chip;
218         unsigned short hardware;
219         unsigned char image[32];
220
221         unsigned char mce_bit;
222         unsigned short mode;
223         int mute;
224
225         spinlock_t lock;
226
227         snd_card_t *card;
228         snd_pcm_t *pcm;
229         snd_pcm_substream_t *playback_substream;
230         snd_pcm_substream_t *capture_substream;
231         unsigned int p_dma_size;
232         unsigned int c_dma_size;
233 };
234
235 #define OPTi93X_MODE_NONE       0x00
236 #define OPTi93X_MODE_PLAY       0x01
237 #define OPTi93X_MODE_CAPTURE    0x02
238 #define OPTi93X_MODE_OPEN       (OPTi93X_MODE_PLAY | OPTi93X_MODE_CAPTURE)
239
240 #endif /* OPTi93X */
241
242 struct _snd_opti9xx {
243         unsigned short hardware;
244         unsigned char password;
245         char name[7];
246
247         unsigned long mc_base;
248         struct resource *res_mc_base;
249         unsigned long mc_base_size;
250 #ifdef OPTi93X
251         unsigned long mc_indir_index;
252 #endif  /* OPTi93X */
253         unsigned long pwd_reg;
254
255         spinlock_t lock;
256
257         long wss_base;
258         int irq;
259         int dma1;
260 #if defined(CS4231) || defined(OPTi93X)
261         int dma2;
262 #endif  /* CS4231 || OPTi93X */
263
264         long fm_port;
265
266         long mpu_port;
267         int mpu_irq;
268
269 #ifdef CONFIG_PNP
270         struct pnp_dev *dev;
271         struct pnp_dev *devmpu;
272 #endif  /* CONFIG_PNP */
273 };
274
275 static int snd_opti9xx_first_hit = 1;
276 static snd_card_t *snd_opti9xx_legacy = SNDRV_DEFAULT_PTR1;
277
278 #ifdef CONFIG_PNP
279
280 static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
281 #ifndef OPTi93X
282         /* OPTi 82C924 */
283         { .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" } }, .driver_data = 0x0924 },
284         /* OPTi 82C925 */
285         { .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" } }, .driver_data = 0x0925 },
286 #else
287         /* OPTi 82C931/3 */
288         { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 },
289 #endif  /* OPTi93X */
290         { .id = "" }
291 };
292
293 MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
294
295 #endif  /* CONFIG_PNP */
296
297 #ifdef OPTi93X
298 #define DRIVER_NAME     "snd-card-opti93x"
299 #else
300 #define DRIVER_NAME     "snd-card-opti92x"
301 #endif  /* OPTi93X */
302
303 static char * snd_opti9xx_names[] = {
304         "unkown",
305         "82C928",       "82C929",
306         "82C924",       "82C925",
307         "82C930",       "82C931",       "82C933"
308 };
309
310
311 static long snd_legacy_find_free_ioport(long *port_table, long size)
312 {
313         while (*port_table != -1) {
314                 struct resource *res;
315                 if ((res = request_region(*port_table, size, "ALSA test")) != NULL) {
316                         release_resource(res);
317                         kfree_nocheck(res);
318                         return *port_table;
319                 }
320                 port_table++;
321         }
322         return -1;
323 }
324
325 static int __devinit snd_opti9xx_init(opti9xx_t *chip, unsigned short hardware)
326 {
327         static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
328
329         chip->hardware = hardware;
330         strcpy(chip->name, snd_opti9xx_names[hardware]);
331
332         chip->mc_base_size = opti9xx_mc_size[hardware];  
333
334         spin_lock_init(&chip->lock);
335
336         chip->wss_base = -1;
337         chip->irq = -1;
338         chip->dma1 = -1;
339 #if defined(CS4231) || defined (OPTi93X)
340         chip->dma2 = -1;
341 #endif  /* CS4231 || OPTi93X */
342         chip->fm_port = -1;
343         chip->mpu_port = -1;
344         chip->mpu_irq = -1;
345
346         switch (hardware) {
347 #ifndef OPTi93X
348         case OPTi9XX_HW_82C928:
349         case OPTi9XX_HW_82C929:
350                 chip->mc_base = 0xf8c;
351                 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
352                 chip->pwd_reg = 3;
353                 break;
354
355         case OPTi9XX_HW_82C924:
356         case OPTi9XX_HW_82C925:
357                 chip->mc_base = 0xf8c;
358                 chip->password = 0xe5;
359                 chip->pwd_reg = 3;
360                 break;
361 #else   /* OPTi93X */
362
363         case OPTi9XX_HW_82C930:
364         case OPTi9XX_HW_82C931:
365         case OPTi9XX_HW_82C933:
366                 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
367                 chip->mc_indir_index = 0xe0e;
368                 chip->password = 0xe4;
369                 chip->pwd_reg = 0;
370                 break;
371 #endif  /* OPTi93X */
372
373         default:
374                 snd_printk("chip %d not supported\n", hardware);
375                 return -ENODEV;
376         }
377         return 0;
378 }
379
380 static unsigned char snd_opti9xx_read(opti9xx_t *chip,
381                                       unsigned char reg)
382 {
383         unsigned long flags;
384         unsigned char retval = 0xff;
385
386         spin_lock_irqsave(&chip->lock, flags);
387         outb(chip->password, chip->mc_base + chip->pwd_reg);
388
389         switch (chip->hardware) {
390 #ifndef OPTi93X
391         case OPTi9XX_HW_82C924:
392         case OPTi9XX_HW_82C925:
393                 if (reg > 7) {
394                         outb(reg, chip->mc_base + 8);
395                         outb(chip->password, chip->mc_base + chip->pwd_reg);
396                         retval = inb(chip->mc_base + 9);
397                         break;
398                 }
399
400         case OPTi9XX_HW_82C928:
401         case OPTi9XX_HW_82C929:
402                 retval = inb(chip->mc_base + reg);
403                 break;
404 #else   /* OPTi93X */
405
406         case OPTi9XX_HW_82C930:
407         case OPTi9XX_HW_82C931:
408         case OPTi9XX_HW_82C933:
409                 outb(reg, chip->mc_indir_index);
410                 outb(chip->password, chip->mc_base + chip->pwd_reg);
411                 retval = inb(chip->mc_indir_index + 1);
412                 break;
413 #endif  /* OPTi93X */
414
415         default:
416                 snd_printk("chip %d not supported\n", chip->hardware);
417         }
418
419         spin_unlock_irqrestore(&chip->lock, flags);
420         return retval;
421 }
422         
423 static void snd_opti9xx_write(opti9xx_t *chip, unsigned char reg,
424                               unsigned char value)
425 {
426         unsigned long flags;
427
428         spin_lock_irqsave(&chip->lock, flags);
429         outb(chip->password, chip->mc_base + chip->pwd_reg);
430
431         switch (chip->hardware) {
432 #ifndef OPTi93X
433         case OPTi9XX_HW_82C924:
434         case OPTi9XX_HW_82C925:
435                 if (reg > 7) {
436                         outb(reg, chip->mc_base + 8);
437                         outb(chip->password, chip->mc_base + chip->pwd_reg);
438                         outb(value, chip->mc_base + 9);
439                         break;
440                 }
441
442         case OPTi9XX_HW_82C928:
443         case OPTi9XX_HW_82C929:
444                 outb(value, chip->mc_base + reg);
445                 break;
446 #else   /* OPTi93X */
447
448         case OPTi9XX_HW_82C930:
449         case OPTi9XX_HW_82C931:
450         case OPTi9XX_HW_82C933:
451                 outb(reg, chip->mc_indir_index);
452                 outb(chip->password, chip->mc_base + chip->pwd_reg);
453                 outb(value, chip->mc_indir_index + 1);
454                 break;
455 #endif  /* OPTi93X */
456
457         default:
458                 snd_printk("chip %d not supported\n", chip->hardware);
459         }
460
461         spin_unlock_irqrestore(&chip->lock, flags);
462 }
463
464
465 #define snd_opti9xx_write_mask(chip, reg, value, mask)  \
466         snd_opti9xx_write(chip, reg,                    \
467                 (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
468
469
470 static int __devinit snd_opti9xx_configure(opti9xx_t *chip)
471 {
472         unsigned char wss_base_bits;
473         unsigned char irq_bits;
474         unsigned char dma_bits;
475         unsigned char mpu_port_bits = 0;
476         unsigned char mpu_irq_bits;
477         unsigned long flags;
478
479         switch (chip->hardware) {
480 #ifndef OPTi93X
481         case OPTi9XX_HW_82C924:
482                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
483                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
484
485         case OPTi9XX_HW_82C925:
486                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
487                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
488                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
489 #ifdef CS4231
490                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
491 #else
492                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
493 #endif  /* CS4231 */
494                 break;
495
496         case OPTi9XX_HW_82C928:
497         case OPTi9XX_HW_82C929:
498                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
499                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
500                 /*
501                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
502                 */
503                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
504 #ifdef CS4231
505                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
506 #else
507                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
508 #endif  /* CS4231 */
509                 break;
510
511 #else   /* OPTi93X */
512         case OPTi9XX_HW_82C930:
513         case OPTi9XX_HW_82C931:
514         case OPTi9XX_HW_82C933:
515                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
516                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
517                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
518                         (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
519                         0x34);
520                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
521                 break;
522 #endif  /* OPTi93X */
523
524         default:
525                 snd_printk("chip %d not supported\n", chip->hardware);
526                 return -EINVAL;
527         }
528
529         switch (chip->wss_base) {
530         case 0x530:
531                 wss_base_bits = 0x00;
532                 break;
533         case 0x604:
534                 wss_base_bits = 0x03;
535                 break;
536         case 0xe80:
537                 wss_base_bits = 0x01;
538                 break;
539         case 0xf40:
540                 wss_base_bits = 0x02;
541                 break;
542         default:
543                 snd_printk("WSS port 0x%lx not valid\n", chip->wss_base);
544                 goto __skip_base;
545         }
546         snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
547
548 __skip_base:
549         switch (chip->irq) {
550 //#ifdef OPTi93X
551         case 5:
552                 irq_bits = 0x05;
553                 break;
554 //#endif        /* OPTi93X */
555         case 7:
556                 irq_bits = 0x01;
557                 break;
558         case 9:
559                 irq_bits = 0x02;
560                 break;
561         case 10:
562                 irq_bits = 0x03;
563                 break;
564         case 11:
565                 irq_bits = 0x04;
566                 break;
567         default:
568                 snd_printk("WSS irq # %d not valid\n", chip->irq);
569                 goto __skip_resources;
570         }
571
572         switch (chip->dma1) {
573         case 0:
574                 dma_bits = 0x01;
575                 break;
576         case 1:
577                 dma_bits = 0x02;
578                 break;
579         case 3:
580                 dma_bits = 0x03;
581                 break;
582         default:
583                 snd_printk("WSS dma1 # %d not valid\n", chip->dma1);
584                 goto __skip_resources;
585         }
586
587 #if defined(CS4231) || defined(OPTi93X)
588         if (chip->dma1 == chip->dma2) {
589                 snd_printk("don't want to share dmas\n");
590                 return -EBUSY;
591         }
592
593         switch (chip->dma2) {
594         case 0:
595         case 1:
596                 break;
597         default:
598                 snd_printk("WSS dma2 # %d not valid\n", chip->dma2);
599                 goto __skip_resources;
600         }
601         dma_bits |= 0x04;
602 #endif  /* CS4231 || OPTi93X */
603
604         spin_lock_irqsave(&chip->lock, flags);
605 #ifndef OPTi93X
606          outb(irq_bits << 3 | dma_bits, chip->wss_base);
607 #else /* OPTi93X */
608         snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
609 #endif /* OPTi93X */
610         spin_unlock_irqrestore(&chip->lock, flags);
611
612 __skip_resources:
613         if (chip->hardware > OPTi9XX_HW_82C928) {
614                 switch (chip->mpu_port) {
615                 case 0:
616                 case -1:
617                         break;
618                 case 0x300:
619                         mpu_port_bits = 0x03;
620                         break;
621                 case 0x310:
622                         mpu_port_bits = 0x02;
623                         break;
624                 case 0x320:
625                         mpu_port_bits = 0x01;
626                         break;
627                 case 0x330:
628                         mpu_port_bits = 0x00;
629                         break;
630                 default:
631                         snd_printk("MPU-401 port 0x%lx not valid\n",
632                                 chip->mpu_port);
633                         goto __skip_mpu;
634                 }
635
636                 switch (chip->mpu_irq) {
637                 case 5:
638                         mpu_irq_bits = 0x02;
639                         break;
640                 case 7:
641                         mpu_irq_bits = 0x03;
642                         break;
643                 case 9:
644                         mpu_irq_bits = 0x00;
645                         break;
646                 case 10:
647                         mpu_irq_bits = 0x01;
648                         break;
649                 default:
650                         snd_printk("MPU-401 irq # %d not valid\n",
651                                 chip->mpu_irq);
652                         goto __skip_mpu;
653                 }
654
655                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
656                         (chip->mpu_port <= 0) ? 0x00 :
657                                 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
658                         0xf8);
659         }
660 __skip_mpu:
661
662         return 0;
663 }
664
665 #ifdef OPTi93X
666
667 #define chip_t opti93x_t
668
669 static unsigned char snd_opti93x_default_image[32] =
670 {
671         0x00,           /* 00/00 - l_mixout_outctrl */
672         0x00,           /* 01/01 - r_mixout_outctrl */
673         0x88,           /* 02/02 - l_cd_inctrl */
674         0x88,           /* 03/03 - r_cd_inctrl */
675         0x88,           /* 04/04 - l_a1/fm_inctrl */
676         0x88,           /* 05/05 - r_a1/fm_inctrl */
677         0x80,           /* 06/06 - l_dac_inctrl */
678         0x80,           /* 07/07 - r_dac_inctrl */
679         0x00,           /* 08/08 - ply_dataform_reg */
680         0x00,           /* 09/09 - if_conf */
681         0x00,           /* 0a/10 - pin_ctrl */
682         0x00,           /* 0b/11 - err_init_reg */
683         0x0a,           /* 0c/12 - id_reg */
684         0x00,           /* 0d/13 - reserved */
685         0x00,           /* 0e/14 - ply_upcount_reg */
686         0x00,           /* 0f/15 - ply_lowcount_reg */
687         0x88,           /* 10/16 - reserved/l_a1_inctrl */
688         0x88,           /* 11/17 - reserved/r_a1_inctrl */
689         0x88,           /* 12/18 - l_line_inctrl */
690         0x88,           /* 13/19 - r_line_inctrl */
691         0x88,           /* 14/20 - l_mic_inctrl */
692         0x88,           /* 15/21 - r_mic_inctrl */
693         0x80,           /* 16/22 - l_out_outctrl */
694         0x80,           /* 17/23 - r_out_outctrl */
695         0x00,           /* 18/24 - reserved */
696         0x00,           /* 19/25 - reserved */
697         0x00,           /* 1a/26 - reserved */
698         0x00,           /* 1b/27 - reserved */
699         0x00,           /* 1c/28 - cap_dataform_reg */
700         0x00,           /* 1d/29 - reserved */
701         0x00,           /* 1e/30 - cap_upcount_reg */
702         0x00            /* 1f/31 - cap_lowcount_reg */
703 };
704
705
706 static int snd_opti93x_busy_wait(opti93x_t *chip)
707 {
708         int timeout;
709
710         for (timeout = 250; timeout-- > 0; udelay(10))
711                 if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_INIT))
712                         return 0;
713
714         snd_printk("chip still busy.\n");
715         return -EBUSY;
716 }
717
718 static unsigned char snd_opti93x_in(opti93x_t *chip, unsigned char reg)
719 {
720         snd_opti93x_busy_wait(chip);
721         outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
722         return inb(OPTi93X_PORT(chip, DATA));
723 }
724
725 static void snd_opti93x_out(opti93x_t *chip, unsigned char reg,
726                             unsigned char value)
727 {
728         snd_opti93x_busy_wait(chip);
729         outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
730         outb(value, OPTi93X_PORT(chip, DATA));
731 }
732
733 static void snd_opti93x_out_image(opti93x_t *chip, unsigned char reg,
734                                   unsigned char value)
735 {
736         snd_opti93x_out(chip, reg, chip->image[reg] = value);
737 }
738
739 static void snd_opti93x_out_mask(opti93x_t *chip, unsigned char reg,
740                                  unsigned char mask, unsigned char value)
741 {
742         snd_opti93x_out_image(chip, reg,
743                 (chip->image[reg] & ~mask) | (value & mask));
744 }
745
746
747 static void snd_opti93x_mce_up(opti93x_t *chip)
748 {
749         snd_opti93x_busy_wait(chip);
750
751         chip->mce_bit = OPTi93X_MCE;
752         if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE))
753                 outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
754 }
755
756 static void snd_opti93x_mce_down(opti93x_t *chip)
757 {
758         snd_opti93x_busy_wait(chip);
759
760         chip->mce_bit = 0;
761         if (inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE)
762                 outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
763 }
764
765 #define snd_opti93x_mute_reg(chip, reg, mute)   \
766         snd_opti93x_out(chip, reg, mute ? 0x80 : chip->image[reg]);
767
768 static void snd_opti93x_mute(opti93x_t *chip, int mute)
769 {
770         unsigned long flags;
771
772         spin_lock_irqsave(&chip->lock, flags);
773
774         mute = mute ? 1 : 0;
775         if (chip->mute == mute) {
776                 spin_unlock_irqrestore(&chip->lock, flags);
777                 return;
778         }
779         chip->mute = mute;
780
781         snd_opti93x_mute_reg(chip, OPTi93X_CD_LEFT_INPUT, mute);
782         snd_opti93x_mute_reg(chip, OPTi93X_CD_RIGHT_INPUT, mute);
783         switch (chip->hardware) {
784         case OPTi9XX_HW_82C930:
785                 snd_opti93x_mute_reg(chip, OPTi930_AUX_LEFT_INPUT, mute);
786                 snd_opti93x_mute_reg(chip, OPTi930_AUX_RIGHT_INPUT, mute);
787                 break;
788         case OPTi9XX_HW_82C931:
789         case OPTi9XX_HW_82C933:
790                 snd_opti93x_mute_reg(chip, OPTi931_FM_LEFT_INPUT, mute);
791                 snd_opti93x_mute_reg(chip, OPTi931_FM_RIGHT_INPUT, mute);
792                 snd_opti93x_mute_reg(chip, OPTi931_AUX_LEFT_INPUT, mute);
793                 snd_opti93x_mute_reg(chip, OPTi931_AUX_RIGHT_INPUT, mute);
794         }
795         snd_opti93x_mute_reg(chip, OPTi93X_DAC_LEFT, mute);
796         snd_opti93x_mute_reg(chip, OPTi93X_DAC_RIGHT, mute);
797         snd_opti93x_mute_reg(chip, OPTi93X_LINE_LEFT_INPUT, mute);
798         snd_opti93x_mute_reg(chip, OPTi93X_LINE_RIGHT_INPUT, mute);
799         snd_opti93x_mute_reg(chip, OPTi93X_MIC_LEFT_INPUT, mute);
800         snd_opti93x_mute_reg(chip, OPTi93X_MIC_RIGHT_INPUT, mute);
801         snd_opti93x_mute_reg(chip, OPTi93X_OUT_LEFT, mute);
802         snd_opti93x_mute_reg(chip, OPTi93X_OUT_RIGHT, mute);
803
804         spin_unlock_irqrestore(&chip->lock, flags);
805 }
806
807
808 static unsigned int snd_opti93x_get_count(unsigned char format,
809                                           unsigned int size)
810 {
811         switch (format & 0xe0) {
812         case OPTi93X_LINEAR_16_LIT:
813         case OPTi93X_LINEAR_16_BIG:
814                 size >>= 1;
815                 break;
816         case OPTi93X_ADPCM_16:
817                 return size >> 2;
818         }
819         return (format & OPTi93X_STEREO) ? (size >> 1) : size;
820 }
821
822 static unsigned int rates[] = {  5512,  6615,  8000,  9600, 11025, 16000, 
823                                 18900, 22050, 27428, 32000, 33075, 37800,
824                                 44100, 48000 };
825 #define RATES sizeof(rates) / sizeof(rates[0])
826
827 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
828         .count = RATES,
829         .list = rates,
830         .mask = 0,
831 };
832
833 static unsigned char bits[] = {  0x01,  0x0f,  0x00,  0x0e,  0x03,  0x02,
834                                  0x05,  0x07,  0x04,  0x06,  0x0d,  0x09,
835                                  0x0b,  0x0c};
836
837 static unsigned char snd_opti93x_get_freq(unsigned int rate)
838 {
839         unsigned int i;
840
841         for (i = 0; i < RATES; i++) {
842                 if (rate == rates[i])
843                         return bits[i];
844         }
845         snd_BUG();
846         return bits[RATES-1];
847 }
848
849 static unsigned char snd_opti93x_get_format(opti93x_t *chip,
850                                             unsigned int format, int channels)
851 {
852         unsigned char retval = OPTi93X_LINEAR_8;
853
854         switch (format) {
855         case SNDRV_PCM_FORMAT_MU_LAW:
856                 retval = OPTi93X_ULAW_8;
857                 break;
858         case SNDRV_PCM_FORMAT_A_LAW:
859                 retval = OPTi93X_ALAW_8;
860                 break;
861         case SNDRV_PCM_FORMAT_S16_LE:
862                 retval = OPTi93X_LINEAR_16_LIT;
863                 break;
864         case SNDRV_PCM_FORMAT_S16_BE:
865                 retval = OPTi93X_LINEAR_16_BIG;
866                 break;
867         case SNDRV_PCM_FORMAT_IMA_ADPCM:
868                 retval = OPTi93X_ADPCM_16;
869         }
870         return (channels > 1) ? (retval | OPTi93X_STEREO) : retval;
871 }
872
873
874 static void snd_opti93x_playback_format(opti93x_t *chip, unsigned char fmt)
875 {
876         unsigned long flags;
877         unsigned char mask;
878
879         spin_lock_irqsave(&chip->lock, flags);
880         snd_opti93x_mute(chip, 1);
881
882         snd_opti93x_mce_up(chip);
883         mask = (chip->mode & OPTi93X_MODE_CAPTURE) ? 0xf0 : 0xff;
884         snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, mask, fmt);
885         snd_opti93x_mce_down(chip);
886
887         snd_opti93x_mute(chip, 0);
888         spin_unlock_irqrestore(&chip->lock, flags);
889 }
890
891 static void snd_opti93x_capture_format(opti93x_t *chip, unsigned char fmt)
892 {
893         unsigned long flags;
894
895         spin_lock_irqsave(&chip->lock, flags);
896         snd_opti93x_mute(chip, 1);
897
898         snd_opti93x_mce_up(chip);
899         if (!(chip->mode & OPTi93X_MODE_PLAY))
900                 snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, 0x0f, fmt);
901         else
902                 fmt = chip->image[OPTi93X_PLAY_FORMAT] & 0xf0;
903         snd_opti93x_out_image(chip, OPTi93X_CAPT_FORMAT, fmt);
904         snd_opti93x_mce_down(chip);
905
906         snd_opti93x_mute(chip, 0);
907         spin_unlock_irqrestore(&chip->lock, flags);
908 }
909
910
911 static int snd_opti93x_open(opti93x_t *chip, unsigned int mode)
912 {
913         unsigned long flags;
914
915         spin_lock_irqsave(&chip->lock, flags);
916
917         if (chip->mode & mode) {
918                 spin_unlock_irqrestore(&chip->lock, flags);
919                 return -EAGAIN;
920         }
921
922         if (!(chip->mode & OPTi93X_MODE_OPEN)) {
923                 outb(0x00, OPTi93X_PORT(chip, STATUS));
924                 snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL,
925                         OPTi93X_IRQ_ENABLE, OPTi93X_IRQ_ENABLE);
926                 chip->mode = mode;
927         }
928         else
929                 chip->mode |= mode;
930
931         spin_unlock_irqrestore(&chip->lock, flags);
932         return 0;
933 }
934
935 static void snd_opti93x_close(opti93x_t *chip, unsigned int mode)
936 {
937         unsigned long flags;
938
939         spin_lock_irqsave(&chip->lock, flags);
940
941         chip->mode &= ~mode;
942         if (chip->mode & OPTi93X_MODE_OPEN) {
943                 spin_unlock_irqrestore(&chip->lock, flags);
944                 return;
945         }
946
947         snd_opti93x_mute(chip, 1);
948
949         outb(0, OPTi93X_PORT(chip, STATUS));
950         snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL, OPTi93X_IRQ_ENABLE,
951                 ~OPTi93X_IRQ_ENABLE);
952
953         snd_opti93x_mce_up(chip);
954         snd_opti93x_out_image(chip, OPTi93X_IFACE_CONF, 0x00);
955         snd_opti93x_mce_down(chip);
956         chip->mode = 0;
957
958         snd_opti93x_mute(chip, 0);
959         spin_unlock_irqrestore(&chip->lock, flags);
960 }
961
962 static int snd_opti93x_trigger(snd_pcm_substream_t *substream, 
963                                unsigned char what, int cmd)
964 {
965         opti93x_t *chip = snd_pcm_substream_chip(substream);
966
967         switch (cmd) {
968         case SNDRV_PCM_TRIGGER_START:
969         case SNDRV_PCM_TRIGGER_STOP:
970         {
971                 unsigned int what = 0;
972                 struct list_head *pos;
973                 snd_pcm_substream_t *s;
974                 snd_pcm_group_for_each(pos, substream) {
975                         s = snd_pcm_group_substream_entry(pos);
976                         if (s == chip->playback_substream) {
977                                 what |= OPTi93X_PLAYBACK_ENABLE;
978                                 snd_pcm_trigger_done(s, substream);
979                         } else if (s == chip->capture_substream) {
980                                 what |= OPTi93X_CAPTURE_ENABLE;
981                                 snd_pcm_trigger_done(s, substream);
982                         }
983                 }
984                 spin_lock(&chip->lock);
985                 if (cmd == SNDRV_PCM_TRIGGER_START) {
986                         snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, what);
987                         if (what & OPTi93X_CAPTURE_ENABLE)
988                                 udelay(50);
989                 } else
990                         snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, 0x00);
991                 spin_unlock(&chip->lock);
992                 break;
993         }
994         default:
995                 return -EINVAL;
996         }
997         return 0;
998 }
999
1000 static int snd_opti93x_playback_trigger(snd_pcm_substream_t *substream, int cmd)
1001 {
1002         return snd_opti93x_trigger(substream,
1003                                    OPTi93X_PLAYBACK_ENABLE, cmd);
1004 }
1005
1006 static int snd_opti93x_capture_trigger(snd_pcm_substream_t * substream, int cmd)
1007 {
1008         return snd_opti93x_trigger(substream,
1009                                    OPTi93X_CAPTURE_ENABLE, cmd);
1010 }
1011
1012 static int snd_opti93x_hw_params(snd_pcm_substream_t * substream,
1013                                  snd_pcm_hw_params_t * hw_params)
1014 {
1015         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1016 }
1017
1018
1019 static int snd_opti93x_hw_free(snd_pcm_substream_t * substream)
1020 {
1021         snd_pcm_lib_free_pages(substream);
1022         return 0;
1023 }
1024
1025
1026 static int snd_opti93x_playback_prepare(snd_pcm_substream_t * substream)
1027 {
1028         opti93x_t *chip = snd_pcm_substream_chip(substream);
1029         snd_pcm_runtime_t *runtime = substream->runtime;
1030         unsigned long flags;
1031         unsigned char format;
1032         unsigned int count = snd_pcm_lib_period_bytes(substream);
1033         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1034
1035         spin_lock_irqsave(&chip->lock, flags);
1036
1037         chip->p_dma_size = size;
1038         snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
1039                 OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO,
1040                 ~(OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO));
1041
1042         snd_dma_program(chip->dma1, runtime->dma_addr, size,
1043                 DMA_MODE_WRITE | DMA_AUTOINIT);
1044
1045         format = snd_opti93x_get_freq(runtime->rate);
1046         format |= snd_opti93x_get_format(chip, runtime->format,
1047                 runtime->channels);
1048         snd_opti93x_playback_format(chip, format);
1049         format = chip->image[OPTi93X_PLAY_FORMAT];
1050
1051         count = snd_opti93x_get_count(format, count) - 1;
1052         snd_opti93x_out_image(chip, OPTi93X_PLAY_LWR_CNT, count);
1053         snd_opti93x_out_image(chip, OPTi93X_PLAY_UPR_CNT, count >> 8);
1054
1055         spin_unlock_irqrestore(&chip->lock, flags);
1056         return 0;
1057 }
1058
1059 static int snd_opti93x_capture_prepare(snd_pcm_substream_t *substream)
1060 {
1061         opti93x_t *chip = snd_pcm_substream_chip(substream);
1062         snd_pcm_runtime_t *runtime = substream->runtime;
1063         unsigned long flags;
1064         unsigned char format;
1065         unsigned int count = snd_pcm_lib_period_bytes(substream);
1066         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1067
1068         spin_lock_irqsave(&chip->lock, flags);
1069
1070         chip->c_dma_size = size;
1071         snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
1072                 OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO,
1073                 (unsigned char)~(OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO));
1074
1075         snd_dma_program(chip->dma2, runtime->dma_addr, size,
1076                 DMA_MODE_READ | DMA_AUTOINIT);
1077
1078         format = snd_opti93x_get_freq(runtime->rate);
1079         format |= snd_opti93x_get_format(chip, runtime->format,
1080                 runtime->channels);
1081         snd_opti93x_capture_format(chip, format);
1082         format = chip->image[OPTi93X_CAPT_FORMAT];
1083
1084         count = snd_opti93x_get_count(format, count) - 1;
1085         snd_opti93x_out_image(chip, OPTi93X_CAPT_LWR_CNT, count);
1086         snd_opti93x_out_image(chip, OPTi93X_CAPT_UPR_CNT, count >> 8);
1087
1088         spin_unlock_irqrestore(&chip->lock, flags);
1089         return 0;
1090 }
1091
1092 static snd_pcm_uframes_t snd_opti93x_playback_pointer(snd_pcm_substream_t *substream)
1093 {
1094         opti93x_t *chip = snd_pcm_substream_chip(substream);
1095         size_t ptr;
1096
1097         if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_PLAYBACK_ENABLE))
1098                 return 0;
1099
1100         ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
1101         return bytes_to_frames(substream->runtime, ptr);
1102 }
1103
1104 static snd_pcm_uframes_t snd_opti93x_capture_pointer(snd_pcm_substream_t *substream)
1105 {
1106         opti93x_t *chip = snd_pcm_substream_chip(substream);
1107         size_t ptr;
1108         
1109         if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_CAPTURE_ENABLE))
1110                 return 0;
1111
1112         ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
1113         return bytes_to_frames(substream->runtime, ptr);
1114 }
1115
1116
1117 static void snd_opti93x_overrange(opti93x_t *chip)
1118 {
1119         unsigned long flags;
1120
1121         spin_lock_irqsave(&chip->lock, flags);
1122
1123         if (snd_opti93x_in(chip, OPTi93X_ERR_INIT) & (0x08 | 0x02))
1124                 chip->capture_substream->runtime->overrange++;
1125
1126         spin_unlock_irqrestore(&chip->lock, flags);
1127 }
1128
1129 irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1130 {
1131         opti93x_t *codec = snd_magic_cast(opti93x_t, dev_id, return IRQ_NONE);
1132         unsigned char status;
1133
1134         status = snd_opti9xx_read(codec->chip, OPTi9XX_MC_REG(11));
1135         if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
1136                 snd_pcm_period_elapsed(codec->playback_substream);
1137         if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
1138                 snd_opti93x_overrange(codec);
1139                 snd_pcm_period_elapsed(codec->capture_substream);
1140         }
1141         outb(0x00, OPTi93X_PORT(codec, STATUS));
1142         return IRQ_HANDLED;
1143 }
1144
1145
1146 static snd_pcm_hardware_t snd_opti93x_playback = {
1147         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1148                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1149         .formats =              (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1150                                  SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1151         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1152         .rate_min =             5512,
1153         .rate_max =             48000,
1154         .channels_min =         1,
1155         .channels_max =         2,
1156         .buffer_bytes_max =     (128*1024),
1157         .period_bytes_min =     64,
1158         .period_bytes_max =     (128*1024),
1159         .periods_min =          1,
1160         .periods_max =          1024,
1161         .fifo_size =            0,
1162 };
1163
1164 static snd_pcm_hardware_t snd_opti93x_capture = {
1165         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1166                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1167         .formats =              (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1168                                  SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1169         .rates =                SNDRV_PCM_RATE_8000_48000,
1170         .rate_min =             5512,
1171         .rate_max =             48000,
1172         .channels_min =         1,
1173         .channels_max =         2,
1174         .buffer_bytes_max =     (128*1024),
1175         .period_bytes_min =     64,
1176         .period_bytes_max =     (128*1024),
1177         .periods_min =          1,
1178         .periods_max =          1024,
1179         .fifo_size =            0,
1180 };
1181
1182 static int snd_opti93x_playback_open(snd_pcm_substream_t *substream)
1183 {
1184         int error;
1185         opti93x_t *chip = snd_pcm_substream_chip(substream);
1186         snd_pcm_runtime_t *runtime = substream->runtime;
1187
1188         if ((error = snd_opti93x_open(chip, OPTi93X_MODE_PLAY)) < 0)
1189                 return error;
1190         snd_pcm_set_sync(substream);
1191         chip->playback_substream = substream;
1192         runtime->hw = snd_opti93x_playback;
1193         snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1194         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1195         return error;
1196 }
1197
1198 static int snd_opti93x_capture_open(snd_pcm_substream_t *substream)
1199 {
1200         int error;
1201         opti93x_t *chip = snd_pcm_substream_chip(substream);
1202         snd_pcm_runtime_t *runtime = substream->runtime;
1203
1204         if ((error = snd_opti93x_open(chip, OPTi93X_MODE_CAPTURE)) < 0)
1205                 return error;
1206         runtime->hw = snd_opti93x_capture;
1207         snd_pcm_set_sync(substream);
1208         chip->capture_substream = substream;
1209         snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1210         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1211         return error;
1212 }
1213
1214 static int snd_opti93x_playback_close(snd_pcm_substream_t *substream)
1215 {
1216         opti93x_t *chip = snd_pcm_substream_chip(substream);
1217
1218         chip->playback_substream = NULL;
1219         snd_opti93x_close(chip, OPTi93X_MODE_PLAY);
1220         return 0;
1221 }
1222
1223 static int snd_opti93x_capture_close(snd_pcm_substream_t *substream)
1224 {
1225         opti93x_t *chip = snd_pcm_substream_chip(substream);
1226
1227         chip->capture_substream = NULL;
1228         snd_opti93x_close(chip, OPTi93X_MODE_CAPTURE);
1229         return 0;
1230 }
1231
1232
1233 static void snd_opti93x_init(opti93x_t *chip)
1234 {
1235         unsigned long flags;
1236         int i;
1237
1238         spin_lock_irqsave(&chip->lock, flags);
1239         snd_opti93x_mce_up(chip);
1240
1241         for (i = 0; i < 32; i++)
1242                 snd_opti93x_out_image(chip, i, snd_opti93x_default_image[i]);
1243
1244         snd_opti93x_mce_down(chip);
1245         spin_unlock_irqrestore(&chip->lock, flags);
1246 }
1247
1248 static int snd_opti93x_probe(opti93x_t *chip)
1249 {
1250         unsigned long flags;
1251         unsigned char val;
1252
1253         spin_lock_irqsave(&chip->lock, flags);
1254         val = snd_opti93x_in(chip, OPTi93X_ID) & 0x0f;
1255         spin_unlock_irqrestore(&chip->lock, flags);
1256
1257         return (val == 0x0a) ? 0 : -ENODEV;
1258 }
1259
1260 static int snd_opti93x_free(opti93x_t *chip)
1261 {
1262         if (chip->res_port) {
1263                 release_resource(chip->res_port);
1264                 kfree_nocheck(chip->res_port);
1265         }
1266         if (chip->dma1 >= 0) {
1267                 disable_dma(chip->dma1);
1268                 free_dma(chip->dma1);
1269         }
1270         if (chip->dma2 >= 0) {
1271                 disable_dma(chip->dma2);
1272                 free_dma(chip->dma2);
1273         }
1274         if (chip->irq >= 0) {
1275           free_irq(chip->irq, chip);
1276         }
1277         snd_magic_kfree(chip);
1278         return 0;
1279 }
1280
1281 static int snd_opti93x_dev_free(snd_device_t *device)
1282 {
1283         opti93x_t *chip = snd_magic_cast(opti93x_t, device->device_data, return -ENXIO);
1284         return snd_opti93x_free(chip);
1285 }
1286
1287 static const char *snd_opti93x_chip_id(opti93x_t *codec)
1288 {
1289         switch (codec->hardware) {
1290         case OPTi9XX_HW_82C930: return "82C930";
1291         case OPTi9XX_HW_82C931: return "82C931";
1292         case OPTi9XX_HW_82C933: return "82C933";
1293         default:                return "???";
1294         }
1295 }
1296
1297 int snd_opti93x_create(snd_card_t *card, opti9xx_t *chip,
1298                        int dma1, int dma2,
1299                        opti93x_t **rcodec)
1300 {
1301         static snd_device_ops_t ops = {
1302                 .dev_free =     snd_opti93x_dev_free,
1303         };
1304         int error;
1305         opti93x_t *codec;
1306
1307         *rcodec = NULL;
1308         codec = snd_magic_kcalloc(opti93x_t, 0, GFP_KERNEL);
1309         if (codec == NULL)
1310                 return -ENOMEM;
1311         codec->irq = -1;
1312         codec->dma1 = -1;
1313         codec->dma2 = -1;
1314
1315         if ((codec->res_port = request_region(chip->wss_base + 4, 4, "OPTI93x CODEC")) == NULL) {
1316                 snd_printk(KERN_ERR "opti9xx: can't grab port 0x%lx\n", chip->wss_base + 4);
1317                 snd_opti93x_free(codec);
1318                 return -EBUSY;
1319         }
1320         if (request_dma(dma1, "OPTI93x - 1")) {
1321                 snd_printk(KERN_ERR "opti9xx: can't grab DMA1 %d\n", dma1);
1322                 snd_opti93x_free(codec);
1323                 return -EBUSY;
1324         }
1325         codec->dma1 = chip->dma1;
1326         if (request_dma(dma2, "OPTI93x - 2")) {
1327                 snd_printk(KERN_ERR "opti9xx: can't grab DMA2 %d\n", dma2);
1328                 snd_opti93x_free(codec);
1329                 return -EBUSY;
1330         }
1331         codec->dma2 = chip->dma2;
1332
1333         if (request_irq(chip->irq, snd_opti93x_interrupt, SA_INTERRUPT, DRIVER_NAME" - WSS", codec)) {
1334                 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
1335                 snd_opti93x_free(codec);
1336                 return -EBUSY;
1337         }
1338
1339         codec->card = card;
1340         codec->port = chip->wss_base + 4;
1341         codec->irq = chip->irq;
1342
1343         spin_lock_init(&codec->lock);
1344         codec->hardware = chip->hardware;
1345         codec->chip = chip;
1346
1347         if ((error = snd_opti93x_probe(codec))) {
1348                 snd_opti93x_free(codec);
1349                 return error;
1350         }
1351
1352         snd_opti93x_init(codec);
1353
1354         /* Register device */
1355         if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
1356                 snd_opti93x_free(codec);
1357                 return error;
1358         }
1359
1360         *rcodec = codec;
1361         return 0;
1362 }
1363
1364 static snd_pcm_ops_t snd_opti93x_playback_ops = {
1365         .open =         snd_opti93x_playback_open,
1366         .close =        snd_opti93x_playback_close,
1367         .ioctl =        snd_pcm_lib_ioctl,
1368         .hw_params =    snd_opti93x_hw_params,
1369         .hw_free =      snd_opti93x_hw_free,
1370         .prepare =      snd_opti93x_playback_prepare,
1371         .trigger =      snd_opti93x_playback_trigger,
1372         .pointer =      snd_opti93x_playback_pointer,
1373 };
1374
1375 static snd_pcm_ops_t snd_opti93x_capture_ops = {
1376         .open =         snd_opti93x_capture_open,
1377         .close =        snd_opti93x_capture_close,
1378         .ioctl =        snd_pcm_lib_ioctl,
1379         .hw_params =    snd_opti93x_hw_params,
1380         .hw_free =      snd_opti93x_hw_free,
1381         .prepare =      snd_opti93x_capture_prepare,
1382         .trigger =      snd_opti93x_capture_trigger,
1383         .pointer =      snd_opti93x_capture_pointer,
1384 };
1385
1386 static void snd_opti93x_pcm_free(snd_pcm_t *pcm)
1387 {
1388         opti93x_t *codec = snd_magic_cast(opti93x_t, pcm->private_data, return);
1389         codec->pcm = NULL;
1390         snd_pcm_lib_preallocate_free_for_all(pcm);
1391 }
1392
1393 int snd_opti93x_pcm(opti93x_t *codec, int device, snd_pcm_t **rpcm)
1394 {
1395         int error;
1396         snd_pcm_t *pcm;
1397
1398         if ((error = snd_pcm_new(codec->card, "OPTi 82C93X", device, 1, 1, &pcm)))
1399                 return error;
1400
1401         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_opti93x_playback_ops);
1402         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_opti93x_capture_ops);
1403
1404         pcm->private_data = codec;
1405         pcm->private_free = snd_opti93x_pcm_free;
1406         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1407
1408         strcpy(pcm->name, snd_opti93x_chip_id(codec));
1409
1410         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1411                                               snd_dma_isa_data(),
1412                                               64*1024, codec->dma1 > 3 || codec->dma2 > 3 ? 128*1024 : 64*1024);
1413
1414         codec->pcm = pcm;
1415         if (rpcm)
1416                 *rpcm = pcm;
1417         return 0;
1418 }
1419
1420 /*
1421  *  MIXER part
1422  */
1423
1424 static int snd_opti93x_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1425 {
1426         static char *texts[4] = {
1427                 "Line1", "Aux", "Mic", "Mix"
1428         };
1429
1430         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1431         uinfo->count = 2;
1432         uinfo->value.enumerated.items = 4;
1433         if (uinfo->value.enumerated.item > 3)
1434                 uinfo->value.enumerated.item = 3;
1435         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1436         return 0;
1437 }
1438
1439 static int snd_opti93x_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1440 {
1441         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1442         unsigned long flags;
1443         
1444         spin_lock_irqsave(&chip->lock, flags);
1445         ucontrol->value.enumerated.item[0] = (chip->image[OPTi93X_MIXOUT_LEFT] & OPTi93X_MIXOUT_MIXER) >> 6;
1446         ucontrol->value.enumerated.item[1] = (chip->image[OPTi93X_MIXOUT_RIGHT] & OPTi93X_MIXOUT_MIXER) >> 6;
1447         spin_unlock_irqrestore(&chip->lock, flags);
1448         return 0;
1449 }
1450
1451 static int snd_opti93x_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1452 {
1453         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1454         unsigned long flags;
1455         unsigned short left, right;
1456         int change;
1457         
1458         if (ucontrol->value.enumerated.item[0] > 3 ||
1459             ucontrol->value.enumerated.item[1] > 3)
1460                 return -EINVAL;
1461         left = ucontrol->value.enumerated.item[0] << 6;
1462         right = ucontrol->value.enumerated.item[1] << 6;
1463         spin_lock_irqsave(&chip->lock, flags);
1464         left = (chip->image[OPTi93X_MIXOUT_LEFT] & ~OPTi93X_MIXOUT_MIXER) | left;
1465         right = (chip->image[OPTi93X_MIXOUT_RIGHT] & ~OPTi93X_MIXOUT_MIXER) | right;
1466         change = left != chip->image[OPTi93X_MIXOUT_LEFT] ||
1467                  right != chip->image[OPTi93X_MIXOUT_RIGHT];
1468         snd_opti93x_out_image(chip, OPTi93X_MIXOUT_LEFT, left);
1469         snd_opti93x_out_image(chip, OPTi93X_MIXOUT_RIGHT, right);
1470         spin_unlock_irqrestore(&chip->lock, flags);
1471         return change;
1472 }
1473
1474 #if 0
1475
1476 #define OPTi93X_SINGLE(xname, xindex, reg, shift, mask, invert) \
1477 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1478   .info = snd_opti93x_info_single, \
1479   .get = snd_opti93x_get_single, .put = snd_opti93x_put_single, \
1480   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1481
1482 static int snd_opti93x_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1483 {
1484         int mask = (kcontrol->private_value >> 16) & 0xff;
1485
1486         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1487         uinfo->count = 1;
1488         uinfo->value.integer.min = 0;
1489         uinfo->value.integer.max = mask;
1490         return 0;
1491 }
1492
1493 static int snd_opti93x_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1494 {
1495         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1496         unsigned long flags;
1497         int reg = kcontrol->private_value & 0xff;
1498         int shift = (kcontrol->private_value >> 8) & 0xff;
1499         int mask = (kcontrol->private_value >> 16) & 0xff;
1500         int invert = (kcontrol->private_value >> 24) & 0xff;
1501         
1502         spin_lock_irqsave(&chip->lock, flags);
1503         ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1504         spin_unlock_irqrestore(&chip->lock, flags);
1505         if (invert)
1506                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1507         return 0;
1508 }
1509
1510 static int snd_opti93x_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1511 {
1512         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1513         unsigned long flags;
1514         int reg = kcontrol->private_value & 0xff;
1515         int shift = (kcontrol->private_value >> 8) & 0xff;
1516         int mask = (kcontrol->private_value >> 16) & 0xff;
1517         int invert = (kcontrol->private_value >> 24) & 0xff;
1518         int change;
1519         unsigned short val;
1520         
1521         val = (ucontrol->value.integer.value[0] & mask);
1522         if (invert)
1523                 val = mask - val;
1524         val <<= shift;
1525         spin_lock_irqsave(&chip->lock, flags);
1526         val = (chip->image[reg] & ~(mask << shift)) | val;
1527         change = val != chip->image[reg];
1528         snd_opti93x_out(chip, reg, val);
1529         spin_unlock_irqrestore(&chip->lock, flags);
1530         return change;
1531 }
1532
1533 #endif /* single */
1534
1535 #define OPTi93X_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1536 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1537   .info = snd_opti93x_info_double, \
1538   .get = snd_opti93x_get_double, .put = snd_opti93x_put_double, \
1539   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1540
1541 #define OPTi93X_DOUBLE_INVERT_INVERT(xctl) \
1542         do { xctl.private_value ^= 22; } while (0)
1543 #define OPTi93X_DOUBLE_CHANGE_REGS(xctl, left_reg, right_reg) \
1544         do { xctl.private_value &= ~0x0000ffff; \
1545              xctl.private_value |= left_reg | (right_reg << 8); } while (0)
1546
1547 static int snd_opti93x_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1548 {
1549         int mask = (kcontrol->private_value >> 24) & 0xff;
1550
1551         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1552         uinfo->count = 2;
1553         uinfo->value.integer.min = 0;
1554         uinfo->value.integer.max = mask;
1555         return 0;
1556 }
1557
1558 static int snd_opti93x_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1559 {
1560         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1561         unsigned long flags;
1562         int left_reg = kcontrol->private_value & 0xff;
1563         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1564         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1565         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1566         int mask = (kcontrol->private_value >> 24) & 0xff;
1567         int invert = (kcontrol->private_value >> 22) & 1;
1568         
1569         spin_lock_irqsave(&chip->lock, flags);
1570         ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1571         ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1572         spin_unlock_irqrestore(&chip->lock, flags);
1573         if (invert) {
1574                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1575                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1576         }
1577         return 0;
1578 }
1579
1580 static int snd_opti93x_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1581 {
1582         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1583         unsigned long flags;
1584         int left_reg = kcontrol->private_value & 0xff;
1585         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1586         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1587         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1588         int mask = (kcontrol->private_value >> 24) & 0xff;
1589         int invert = (kcontrol->private_value >> 22) & 1;
1590         int change;
1591         unsigned short val1, val2;
1592         
1593         val1 = ucontrol->value.integer.value[0] & mask;
1594         val2 = ucontrol->value.integer.value[1] & mask;
1595         if (invert) {
1596                 val1 = mask - val1;
1597                 val2 = mask - val2;
1598         }
1599         val1 <<= shift_left;
1600         val2 <<= shift_right;
1601         spin_lock_irqsave(&chip->lock, flags);
1602         val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1603         val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1604         change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1605         snd_opti93x_out_image(chip, left_reg, val1);
1606         snd_opti93x_out_image(chip, right_reg, val2);
1607         spin_unlock_irqrestore(&chip->lock, flags);
1608         return change;
1609 }
1610
1611 #define OPTi93X_CONTROLS (sizeof(snd_opti93x_controls)/sizeof(snd_kcontrol_new_t))
1612
1613 static snd_kcontrol_new_t snd_opti93x_controls[] = {
1614 OPTi93X_DOUBLE("Master Playback Switch", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
1615 OPTi93X_DOUBLE("Master Playback Volume", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1), 
1616 OPTi93X_DOUBLE("PCM Playback Switch", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 7, 7, 1, 1),
1617 OPTi93X_DOUBLE("PCM Playback Volume", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 0, 0, 31, 1),
1618 OPTi93X_DOUBLE("FM Playback Switch", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 7, 7, 1, 1),
1619 OPTi93X_DOUBLE("FM Playback Volume", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 1, 1, 15, 1),
1620 OPTi93X_DOUBLE("Line Playback Switch", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 7, 7, 1, 1),
1621 OPTi93X_DOUBLE("Line Playback Volume", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 1, 1, 15, 1), 
1622 OPTi93X_DOUBLE("Mic Playback Switch", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
1623 OPTi93X_DOUBLE("Mic Playback Volume", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1), 
1624 OPTi93X_DOUBLE("Mic Boost", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 5, 5, 1, 1),
1625 OPTi93X_DOUBLE("CD Playback Switch", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 7, 7, 1, 1),
1626 OPTi93X_DOUBLE("CD Playback Volume", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 1, 1, 15, 1),
1627 OPTi93X_DOUBLE("Aux Playback Switch", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
1628 OPTi93X_DOUBLE("Aux Playback Volume", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1), 
1629 OPTi93X_DOUBLE("Capture Volume", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 0, 0, 15, 0),
1630 {
1631         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1632         .name = "Capture Source",
1633         .info = snd_opti93x_info_mux,
1634         .get = snd_opti93x_get_mux,
1635         .put = snd_opti93x_put_mux,
1636 }
1637 };
1638                                         
1639 int snd_opti93x_mixer(opti93x_t *chip)
1640 {
1641         snd_card_t *card;
1642         snd_kcontrol_new_t knew;
1643         int err;
1644         unsigned int idx;
1645
1646         snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
1647
1648         card = chip->card;
1649
1650         strcpy(card->mixername, snd_opti93x_chip_id(chip));
1651
1652         for (idx = 0; idx < OPTi93X_CONTROLS; idx++) {
1653                 knew = snd_opti93x_controls[idx];
1654                 if (chip->hardware == OPTi9XX_HW_82C930) {
1655                         if (strstr(knew.name, "FM"))    /* skip FM controls */
1656                                 continue;
1657                         else if (strcmp(knew.name, "Mic Playback Volume"))
1658                                 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1659                         else if (strstr(knew.name, "Aux"))
1660                                 OPTi93X_DOUBLE_CHANGE_REGS(knew, OPTi930_AUX_LEFT_INPUT, OPTi930_AUX_RIGHT_INPUT);
1661                         else if (strcmp(knew.name, "PCM Playback Volume"))
1662                                 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1663                         else if (strcmp(knew.name, "Master Playback Volume"))
1664                                 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1665                 }
1666                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opti93x_controls[idx], chip))) < 0)
1667                         return err;
1668         }
1669         return 0;
1670 }
1671
1672 #endif /* OPTi93X */
1673
1674 static int __devinit snd_card_opti9xx_detect(snd_card_t *card, opti9xx_t *chip)
1675 {
1676         int i, err;
1677
1678 #ifndef OPTi93X
1679         for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
1680                 unsigned char value;
1681
1682                 if ((err = snd_opti9xx_init(chip, i)) < 0)
1683                         return err;
1684
1685                 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1686                         continue;
1687
1688                 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
1689                 if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
1690                         if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
1691                                 return 1;
1692
1693                 release_resource(chip->res_mc_base);
1694                 kfree_nocheck(chip->res_mc_base);
1695                 chip->res_mc_base = NULL;
1696
1697         }
1698 #else   /* OPTi93X */
1699         for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
1700                 unsigned long flags;
1701                 unsigned char value;
1702
1703                 if ((err = snd_opti9xx_init(chip, i)) < 0)
1704                         return err;
1705
1706                 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1707                         continue;
1708
1709                 spin_lock_irqsave(&chip->lock, flags);
1710                 outb(chip->password, chip->mc_base + chip->pwd_reg);
1711                 outb(((chip->mc_indir_index & (1 << 8)) >> 4) |
1712                         ((chip->mc_indir_index & 0xf0) >> 4), chip->mc_base);
1713                 spin_unlock_irqrestore(&chip->lock, flags);
1714
1715                 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
1716                 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
1717                 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
1718                         return 1;
1719
1720                 release_resource(chip->res_mc_base);
1721                 kfree_nocheck(chip->res_mc_base);
1722                 chip->res_mc_base = NULL;
1723         }
1724 #endif  /* OPTi93X */
1725
1726         return -ENODEV;
1727 }
1728
1729 #ifdef CONFIG_PNP
1730 static int __devinit snd_card_opti9xx_pnp(opti9xx_t *chip, struct pnp_card_link *card,
1731                                           const struct pnp_card_device_id *pid)
1732 {
1733         struct pnp_dev *pdev;
1734         struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
1735         int err;
1736
1737         chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1738         if (chip->dev == NULL) {
1739                 kfree(cfg);
1740                 return -EBUSY;
1741         }
1742         chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1743
1744         pdev = chip->dev;
1745         pnp_init_resource_table(cfg);
1746
1747 #ifdef OPTi93X
1748         if (port != SNDRV_AUTO_PORT)
1749                 pnp_resource_change(&cfg->port_resource[0], port + 4, 4);
1750 #else
1751         if (pid->driver_data != 0x0924 && port != SNDRV_AUTO_PORT)
1752                 pnp_resource_change(&cfg->port_resource[1], port, 4);
1753 #endif  /* OPTi93X */
1754         if (irq != SNDRV_AUTO_IRQ)
1755                 pnp_resource_change(&cfg->irq_resource[0], irq, 1);
1756         if (dma1 != SNDRV_AUTO_DMA)
1757                 pnp_resource_change(&cfg->dma_resource[0], dma1, 1);
1758 #if defined(CS4231) || defined(OPTi93X)
1759         if (dma2 != SNDRV_AUTO_DMA)
1760                 pnp_resource_change(&cfg->dma_resource[1], dma2, 1);
1761 #else
1762 #ifdef snd_opti9xx_fixup_dma2
1763         snd_opti9xx_fixup_dma2(pdev);
1764 #endif
1765 #endif  /* CS4231 || OPTi93X */
1766 #ifdef OPTi93X
1767         if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
1768                 pnp_resource_change(&cfg->port_resource[1], fm_port, 4);
1769 #else
1770         if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
1771                 pnp_resource_change(&cfg->port_resource[2], fm_port, 4);
1772 #endif
1773         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
1774                 snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
1775         err = pnp_activate_dev(pdev);
1776         if (err < 0) {
1777                 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
1778                 kfree(cfg);
1779                 return err;
1780         }
1781
1782 #ifdef OPTi93X
1783         port = pnp_port_start(pdev, 0) - 4;
1784         fm_port = pnp_port_start(pdev, 1);
1785 #else
1786         if (pid->driver_data != 0x0924)
1787                 port = pnp_port_start(pdev, 1);
1788         fm_port = pnp_port_start(pdev, 2);
1789 #endif  /* OPTi93X */
1790         irq = pnp_irq(pdev, 0);
1791         dma1 = pnp_dma(pdev, 0);
1792 #if defined(CS4231) || defined(OPTi93X)
1793         dma2 = pnp_dma(pdev, 1);
1794 #endif  /* CS4231 || OPTi93X */
1795
1796         pdev = chip->devmpu;
1797         if (pdev && mpu_port > 0) {
1798                 pnp_init_resource_table(cfg);
1799
1800                 if (mpu_port != SNDRV_AUTO_PORT)
1801                         pnp_resource_change(&cfg->port_resource[0], mpu_port, 2);
1802                 if (mpu_irq != SNDRV_AUTO_IRQ)
1803                         pnp_resource_change(&cfg->irq_resource[0], mpu_irq, 1);
1804
1805                 if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
1806                         snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
1807                 err = pnp_activate_dev(pdev);
1808                 if (err < 0) {
1809                         snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
1810                         mpu_port = -1;
1811                         chip->devmpu = NULL;
1812                 } else {
1813                         mpu_port = pnp_port_start(pdev, 0);
1814                         mpu_irq = pnp_irq(pdev, 0);
1815                 }
1816         }
1817         kfree(cfg);
1818         return pid->driver_data;
1819 }
1820 #endif  /* CONFIG_PNP */
1821
1822 #if 0
1823 static int __devinit snd_card_opti9xx_resources(struct snd_card_opti9xx *chip,
1824                                                 snd_card_t *card)
1825 {
1826         int error, i, pnp = 0;
1827
1828 #ifdef CONFIG_PNP
1829         pnp = chip->dev != NULL;
1830 #endif  /* CONFIG_PNP */
1831
1832 #ifndef OPTi93X
1833         if (chip->chip->hardware == OPTi9XX_HW_82C928)
1834                 mpu_port = -1;
1835 #endif  /* OPTi93X */
1836         error = 0;
1837         if (!pnp && (mpu_port == SNDRV_DEFAULT_PORT1)) {
1838                 for (i = 0; possible_mpu_ports[i] != -1; i++)
1839                         if (!snd_register_ioport(card, possible_mpu_ports[i], 2,
1840                                         DRIVER_NAME" - MPU-401", NULL)) {
1841                                 mpu_port = possible_mpu_ports[i];
1842                                 break;
1843                         }
1844                 if (mpu_port == SNDRV_DEFAULT_PORT1)
1845                         error = -EBUSY;
1846         }
1847         else
1848                 error = (mpu_port == -1) ? -ENODEV :
1849                         snd_register_ioport(card, mpu_port, 2,
1850                         DRIVER_NAME" - MPU-401", NULL);
1851         if (error)
1852                 chip->chip->mpu_port = -1;
1853         else if (pnp && (irq == mpu_irq))
1854                 chip->chip->mpu_irq = mpu_irq;
1855         else if (!snd_register_interrupt(card,
1856                         DRIVER_NAME" - MPU-401",
1857                         mpu_irq, SNDRV_IRQ_TYPE_ISA,
1858                         snd_card_opti9xx_mpu_interrupt, chip,
1859                         pnp ? no_alternatives : possible_mpu_irqs,
1860                         &chip->mpuirqptr)) {
1861                 chip->chip->mpu_port = mpu_port;
1862                 chip->chip->mpu_irq = chip->mpuirqptr->irq;
1863         }
1864         else
1865                 chip->chip->mpu_port = -1;
1866
1867         if (!pnp && (port == SNDRV_DEFAULT_PORT1)) {
1868                 for (i = 0; possible_ports[i] != -1; i++)
1869                         if (!snd_register_ioport(card, possible_ports[i], 8,
1870                                         DRIVER_NAME" - WSS", NULL)) {
1871                                 port = possible_ports[i];
1872                                 break;
1873                         }
1874                 if (port == SNDRV_DEFAULT_PORT1)
1875                         return -EBUSY;
1876         }
1877         else if ((error = snd_register_ioport(card, port, 8,
1878                         DRIVER_NAME" - WSS", NULL)) < 0)
1879                 return error;
1880         chip->chip->wss_base = port;
1881         if ((error = snd_register_interrupt(card, DRIVER_NAME" - WSS",
1882                         irq, SNDRV_IRQ_TYPE_ISA,
1883                         snd_card_opti9xx_interrupt, chip,
1884                         pnp ? no_alternatives : possible_irqs,
1885                         &chip->irqptr)) < 0)
1886                 return error;
1887         chip->chip->irq = chip->irqptr->irq;
1888         if ((error = snd_register_dma_channel(card,
1889 #if defined(CS4231) || defined(OPTi93X)
1890                         DRIVER_NAME" - WSS playback",
1891 #else
1892                         DRIVER_NAME" - WSS",
1893 #endif  /* CS4231 || OPTi93X */
1894                         dma1, SNDRV_DMA_TYPE_ISA, dma1_size,
1895                         pnp ? no_alternatives : possible_dma1s,
1896                         &chip->dma1ptr)) < 0)
1897                 return error;
1898         chip->chip->dma1 = chip->dma1ptr->dma;
1899 #if defined(CS4231) || defined(OPTi93X)
1900         if ((error = snd_register_dma_channel(card, DRIVER_NAME" - WSS capture",
1901                         dma2, SNDRV_DMA_TYPE_ISA, dma2_size,
1902                         pnp ? no_alternatives :
1903                                 possible_dma2s[chip->dma1ptr->dma],
1904                         &chip->dma2ptr)) < 0)
1905                 return error;
1906         chip->chip->dma2 = chip->dma2ptr->dma;
1907 #endif  /* CS4231 || OPTi93X */
1908
1909         if (snd_register_ioport(card,
1910                         pnp ? fm_port : fm_port = 0x388, 4,
1911                         DRIVER_NAME" - OPL", NULL) < 0)
1912                 fm_port = -1;
1913         chip->chip->fm_port = fm_port;
1914
1915         return 0;
1916 }
1917 #endif
1918
1919 static void snd_card_opti9xx_free(snd_card_t *card)
1920 {
1921         opti9xx_t *chip = (opti9xx_t *)card->private_data;
1922         
1923         if (chip) {
1924                 if (chip->res_mc_base) {
1925                         release_resource(chip->res_mc_base);
1926                         kfree_nocheck(chip->res_mc_base);
1927                 }
1928         }
1929 }
1930
1931 static int __devinit snd_card_opti9xx_probe(struct pnp_card_link *pcard,
1932                                             const struct pnp_card_device_id *pid)
1933 {
1934         static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
1935         static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
1936 #ifdef OPTi93X
1937         static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
1938 #else
1939         static int possible_irqs[] = {9, 10, 11, 7, -1};
1940 #endif  /* OPTi93X */
1941         static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
1942         static int possible_dma1s[] = {3, 1, 0, -1};
1943 #if defined(CS4231) || defined(OPTi93X)
1944         static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
1945 #endif  /* CS4231 || OPTi93X */
1946         int error;
1947         opti9xx_t *chip;
1948 #if defined(OPTi93X)
1949         opti93x_t *codec;
1950 #elif defined(CS4231)
1951         cs4231_t *codec;
1952         snd_timer_t *timer;
1953 #else
1954         ad1848_t *codec;
1955 #endif
1956         snd_card_t *card;
1957         snd_pcm_t *pcm;
1958         snd_rawmidi_t *rmidi;
1959         snd_hwdep_t *synth;
1960 #ifdef CONFIG_PNP
1961         int hw;
1962 #endif  /* CONFIG_PNP */
1963
1964         if (pcard && !snd_opti9xx_first_hit)
1965                 return -EBUSY;
1966         if (!(card = snd_card_new(index, id, THIS_MODULE,
1967                                   sizeof(opti9xx_t))))
1968                 return -ENOMEM;
1969         card->private_free = snd_card_opti9xx_free;
1970         chip = (opti9xx_t *)card->private_data;
1971
1972 #ifdef CONFIG_PNP
1973         if (isapnp && pcard && (hw = snd_card_opti9xx_pnp(chip, pcard, pid)) > 0) {
1974                 switch (hw) {
1975                 case 0x0924:
1976                         hw = OPTi9XX_HW_82C924;
1977                         break;
1978                 case 0x0925:
1979                         hw = OPTi9XX_HW_82C925;
1980                         break;
1981                 case 0x0931:
1982                         hw = OPTi9XX_HW_82C931;
1983                         break;
1984                 default:
1985                         snd_card_free(card);
1986                         return -ENODEV;
1987                 }
1988
1989                 if ((error = snd_opti9xx_init(chip, hw))) {
1990                         snd_card_free(card);
1991                         return error;
1992                 }
1993                 if (hw <= OPTi9XX_HW_82C930)
1994                         chip->mc_base -= 0x80;
1995                 snd_card_set_dev(card, &pcard->card->dev);
1996         } else {
1997 #endif  /* CONFIG_PNP */
1998                 if ((error = snd_card_opti9xx_detect(card, chip)) < 0) {
1999                         snd_card_free(card);
2000                         return error;
2001                 }
2002 #ifdef CONFIG_PNP
2003         }
2004 #endif  /* CONFIG_PNP */
2005
2006         if (! chip->res_mc_base &&
2007             (chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL) {
2008                 snd_card_free(card);
2009                 return -ENOMEM;
2010         }
2011
2012         chip->wss_base = port;
2013         chip->fm_port = fm_port;
2014         chip->mpu_port = mpu_port;
2015         chip->irq = irq;
2016         chip->mpu_irq = mpu_irq;
2017         chip->dma1 = dma1;
2018 #if defined(CS4231) || defined(OPTi93X)
2019         chip->dma2 = dma2;
2020 #endif
2021
2022         if (chip->wss_base == SNDRV_AUTO_PORT) {
2023                 if ((chip->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
2024                         snd_card_free(card);
2025                         snd_printk("unable to find a free WSS port\n");
2026                         return -EBUSY;
2027                 }
2028         }
2029 #ifdef CONFIG_PNP
2030         if (!isapnp) {
2031 #endif
2032         if (chip->mpu_port == SNDRV_AUTO_PORT) {
2033                 if ((chip->mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
2034                         snd_card_free(card);
2035                         snd_printk("unable to find a free MPU401 port\n");
2036                         return -EBUSY;
2037                 }
2038         }
2039         if (chip->irq == SNDRV_AUTO_IRQ) {
2040                 if ((chip->irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2041                         snd_card_free(card);
2042                         snd_printk("unable to find a free IRQ\n");
2043                         return -EBUSY;
2044                 }
2045         }
2046         if (chip->mpu_irq == SNDRV_AUTO_IRQ) {
2047                 if ((chip->mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
2048                         snd_card_free(card);
2049                         snd_printk("unable to find a free MPU401 IRQ\n");
2050                         return -EBUSY;
2051                 }
2052         }
2053         if (chip->dma1 == SNDRV_AUTO_DMA) {
2054                 if ((chip->dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
2055                         snd_card_free(card);
2056                         snd_printk("unable to find a free DMA1\n");
2057                         return -EBUSY;
2058                 }
2059         }
2060 #if defined(CS4231) || defined(OPTi93X)
2061         if (chip->dma2 == SNDRV_AUTO_DMA) {
2062                 if ((chip->dma2 = snd_legacy_find_free_dma(possible_dma2s[chip->dma1 % 4])) < 0) {
2063                         snd_card_free(card);
2064                         snd_printk("unable to find a free DMA2\n");
2065                         return -EBUSY;
2066                 }
2067         }
2068 #endif
2069
2070 #ifdef CONFIG_PNP
2071         }
2072 #endif
2073
2074         if ((error = snd_opti9xx_configure(chip))) {
2075                 snd_card_free(card);
2076                 return error;
2077         }
2078
2079 #if defined(OPTi93X)
2080         if ((error = snd_opti93x_create(card, chip, chip->dma1, chip->dma2, &codec))) {
2081                 snd_card_free(card);
2082                 return error;
2083         }
2084         if ((error = snd_opti93x_pcm(codec, 0, &pcm)) < 0) {
2085                 snd_card_free(card);
2086                 return error;
2087         }
2088         if ((error = snd_opti93x_mixer(codec)) < 0) {
2089                 snd_card_free(card);
2090                 return error;
2091         }
2092 #elif defined(CS4231)
2093         if ((error = snd_cs4231_create(card, chip->wss_base + 4, -1,
2094                                        chip->irq, chip->dma1, chip->dma2,
2095                                        CS4231_HW_DETECT,
2096                                        0,
2097                                        &codec)) < 0) {
2098                 snd_card_free(card);
2099                 return error;
2100         }
2101         if ((error = snd_cs4231_pcm(codec, 0, &pcm)) < 0) {
2102                 snd_card_free(card);
2103                 return error;
2104         }
2105         if ((error = snd_cs4231_mixer(codec)) < 0) {
2106                 snd_card_free(card);
2107                 return error;
2108         }
2109         if ((error = snd_cs4231_timer(codec, 0, &timer)) < 0) {
2110                 snd_card_free(card);
2111                 return error;
2112         }
2113 #else
2114         if ((error = snd_ad1848_create(card, chip->wss_base + 4,
2115                                        chip->irq, chip->dma1,
2116                                        AD1848_HW_DETECT, &codec)) < 0) {
2117                 snd_card_free(card);
2118                 return error;
2119         }
2120         if ((error = snd_ad1848_pcm(codec, 0, &pcm)) < 0) {
2121                 snd_card_free(card);
2122                 return error;
2123         }
2124         if ((error = snd_ad1848_mixer(codec)) < 0) {
2125                 snd_card_free(card);
2126                 return error;
2127         }
2128 #endif
2129         strcpy(card->driver, chip->name);
2130         sprintf(card->shortname, "OPTi %s", card->driver);
2131 #if defined(CS4231) || defined(OPTi93X)
2132         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
2133                 card->shortname, pcm->name, chip->wss_base + 4,
2134                 chip->irq, chip->dma1, chip->dma2);
2135 #else
2136         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
2137                 card->shortname, pcm->name, chip->wss_base + 4,
2138                 chip->irq, chip->dma1);
2139 #endif  /* CS4231 || OPTi93X */
2140
2141         if (chip->mpu_port <= 0 || chip->mpu_port == SNDRV_AUTO_PORT)
2142                 rmidi = NULL;
2143         else
2144                 if ((error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
2145                                 chip->mpu_port, 0, chip->mpu_irq, SA_INTERRUPT,
2146                                 &rmidi)))
2147                         snd_printk("no MPU-401 device at 0x%lx?\n", chip->mpu_port);
2148
2149         if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
2150                 opl3_t *opl3 = NULL;
2151 #ifndef OPTi93X
2152                 if (chip->hardware == OPTi9XX_HW_82C928 ||
2153                     chip->hardware == OPTi9XX_HW_82C929 ||
2154                     chip->hardware == OPTi9XX_HW_82C924) {
2155                         opl4_t *opl4;
2156                         /* assume we have an OPL4 */
2157                         snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
2158                                                0x20, 0x20);
2159                         if (snd_opl4_create(card,
2160                                             chip->fm_port,
2161                                             chip->fm_port - 8,
2162                                             2, &opl3, &opl4) < 0) {
2163                                 /* no luck, use OPL3 instead */
2164                                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
2165                                                        0x00, 0x20);
2166                         }
2167                 }
2168 #endif  /* !OPTi93X */
2169                 if (!opl3 && snd_opl3_create(card,
2170                                              chip->fm_port,
2171                                              chip->fm_port + 2,
2172                                              OPL3_HW_AUTO, 0, &opl3) < 0) {
2173                         snd_printk("no OPL device at 0x%lx-0x%lx\n",
2174                                    chip->fm_port, chip->fm_port + 4 - 1);
2175                 }
2176                 if (opl3) {
2177                         if ((error = snd_opl3_timer_new(opl3,
2178 #ifdef CS4231
2179                                                         1, 2)) < 0) {
2180 #else
2181                                                         0, 1)) < 0) {
2182 #endif  /* CS4231 */
2183                                 snd_card_free(card);
2184                                 return error;
2185                         }
2186                         if ((error = snd_opl3_hwdep_new(opl3, 0, 1, &synth)) < 0) {
2187                                 snd_card_free(card);
2188                                 return error;
2189                         }
2190                 }
2191         }
2192
2193         if ((error = snd_card_register(card))) {
2194                 snd_card_free(card);
2195                 return error;
2196         }
2197         snd_opti9xx_first_hit = 0;
2198         if (pcard)
2199                 pnp_set_card_drvdata(pcard, card);
2200         else
2201                 snd_opti9xx_legacy = card;
2202         return 0;
2203 }
2204
2205 #ifdef CONFIG_PNP
2206 static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
2207 {
2208         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
2209
2210         snd_card_disconnect(card);
2211         snd_card_free_in_thread(card);
2212         snd_opti9xx_first_hit = 0;
2213 }
2214
2215 static struct pnp_card_driver opti9xx_pnpc_driver = {
2216         .flags          = PNP_DRIVER_RES_DISABLE,
2217         .name           = "opti9xx",
2218         .id_table       = snd_opti9xx_pnpids,
2219         .probe          = snd_card_opti9xx_probe,
2220         .remove         = __devexit_p(snd_opti9xx_pnp_remove),
2221 };
2222 #endif
2223
2224 static int __init alsa_card_opti9xx_init(void)
2225 {
2226         int cards, error;
2227
2228 #ifdef CONFIG_PNP
2229         cards = pnp_register_card_driver(&opti9xx_pnpc_driver);
2230 #else
2231         cards = 0;
2232 #endif
2233         if (cards == 0 && (error = snd_card_opti9xx_probe(NULL, NULL)) < 0) {
2234 #ifdef CONFIG_PNP
2235                 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
2236 #endif
2237 #ifdef MODULE
2238 #ifdef OPTi93X
2239                 printk(KERN_ERR "no OPTi 82C93x soundcard found\n");
2240 #else
2241                 printk(KERN_ERR "no OPTi 82C92x soundcard found\n");
2242 #endif  /* OPTi93X */
2243 #endif
2244                 return error;
2245         }
2246         return 0;
2247 }
2248
2249 static void __exit alsa_card_opti9xx_exit(void)
2250 {
2251 #ifdef CONFIG_PNP
2252         pnp_unregister_card_driver(&opti9xx_pnpc_driver);
2253 #endif
2254         if (snd_opti9xx_legacy)
2255                 snd_card_free(snd_opti9xx_legacy);
2256 }
2257
2258 module_init(alsa_card_opti9xx_init)
2259 module_exit(alsa_card_opti9xx_exit)