ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <sound/core.h>
34 #ifdef CS4231
35 #include <sound/cs4231.h>
36 #else
37 #ifndef OPTi93X
38 #include <sound/ad1848.h>
39 #else
40 #include <sound/control.h>
41 #include <sound/pcm.h>
42 #endif  /* OPTi93X */
43 #endif  /* CS4231 */
44 #include <sound/mpu401.h>
45 #include <sound/opl3.h>
46 #ifndef OPTi93X
47 #include <sound/opl4.h>
48 #endif
49 #define SNDRV_LEGACY_FIND_FREE_IRQ
50 #define SNDRV_LEGACY_FIND_FREE_DMA
51 #define SNDRV_GET_ID
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_PARM(index, "i");
88 MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
89 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
90 MODULE_PARM(id, "s");
91 MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
92 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
93 //MODULE_PARM(enable, "i");
94 //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
95 //MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
96 MODULE_PARM(isapnp, "i");
97 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
98 MODULE_PARM_SYNTAX(isapnp, SNDRV_ISAPNP_DESC);
99 MODULE_PARM(port, "l");
100 MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
101 MODULE_PARM_SYNTAX(port, SNDRV_PORT_DESC);
102 MODULE_PARM(mpu_port, "l");
103 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
104 MODULE_PARM_SYNTAX(mpu_port, SNDRV_PORT_DESC);
105 MODULE_PARM(fm_port, "l");
106 MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
107 MODULE_PARM_SYNTAX(fm_port, SNDRV_PORT_DESC);
108 MODULE_PARM(irq, "i");
109 MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
110 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
111 MODULE_PARM(mpu_irq, "i");
112 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
113 MODULE_PARM_SYNTAX(mpu_irq, SNDRV_IRQ_DESC);
114 MODULE_PARM(dma1, "i");
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_PARM(dma2, "i");
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         snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
606         spin_unlock_irqrestore(&chip->lock, flags);
607
608 __skip_resources:
609         if (chip->hardware > OPTi9XX_HW_82C928) {
610                 switch (chip->mpu_port) {
611                 case 0:
612                 case -1:
613                         break;
614                 case 0x300:
615                         mpu_port_bits = 0x03;
616                         break;
617                 case 0x310:
618                         mpu_port_bits = 0x02;
619                         break;
620                 case 0x320:
621                         mpu_port_bits = 0x01;
622                         break;
623                 case 0x330:
624                         mpu_port_bits = 0x00;
625                         break;
626                 default:
627                         snd_printk("MPU-401 port 0x%lx not valid\n",
628                                 chip->mpu_port);
629                         goto __skip_mpu;
630                 }
631
632                 switch (chip->mpu_irq) {
633                 case 5:
634                         mpu_irq_bits = 0x02;
635                         break;
636                 case 7:
637                         mpu_irq_bits = 0x03;
638                         break;
639                 case 9:
640                         mpu_irq_bits = 0x00;
641                         break;
642                 case 10:
643                         mpu_irq_bits = 0x01;
644                         break;
645                 default:
646                         snd_printk("MPU-401 irq # %d not valid\n",
647                                 chip->mpu_irq);
648                         goto __skip_mpu;
649                 }
650
651                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
652                         (chip->mpu_port <= 0) ? 0x00 :
653                                 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
654                         0xf8);
655         }
656 __skip_mpu:
657
658         return 0;
659 }
660
661 #ifdef OPTi93X
662
663 #define chip_t opti93x_t
664
665 static unsigned char snd_opti93x_default_image[32] =
666 {
667         0x00,           /* 00/00 - l_mixout_outctrl */
668         0x00,           /* 01/01 - r_mixout_outctrl */
669         0x88,           /* 02/02 - l_cd_inctrl */
670         0x88,           /* 03/03 - r_cd_inctrl */
671         0x88,           /* 04/04 - l_a1/fm_inctrl */
672         0x88,           /* 05/05 - r_a1/fm_inctrl */
673         0x80,           /* 06/06 - l_dac_inctrl */
674         0x80,           /* 07/07 - r_dac_inctrl */
675         0x00,           /* 08/08 - ply_dataform_reg */
676         0x00,           /* 09/09 - if_conf */
677         0x00,           /* 0a/10 - pin_ctrl */
678         0x00,           /* 0b/11 - err_init_reg */
679         0x0a,           /* 0c/12 - id_reg */
680         0x00,           /* 0d/13 - reserved */
681         0x00,           /* 0e/14 - ply_upcount_reg */
682         0x00,           /* 0f/15 - ply_lowcount_reg */
683         0x88,           /* 10/16 - reserved/l_a1_inctrl */
684         0x88,           /* 11/17 - reserved/r_a1_inctrl */
685         0x88,           /* 12/18 - l_line_inctrl */
686         0x88,           /* 13/19 - r_line_inctrl */
687         0x88,           /* 14/20 - l_mic_inctrl */
688         0x88,           /* 15/21 - r_mic_inctrl */
689         0x80,           /* 16/22 - l_out_outctrl */
690         0x80,           /* 17/23 - r_out_outctrl */
691         0x00,           /* 18/24 - reserved */
692         0x00,           /* 19/25 - reserved */
693         0x00,           /* 1a/26 - reserved */
694         0x00,           /* 1b/27 - reserved */
695         0x00,           /* 1c/28 - cap_dataform_reg */
696         0x00,           /* 1d/29 - reserved */
697         0x00,           /* 1e/30 - cap_upcount_reg */
698         0x00            /* 1f/31 - cap_lowcount_reg */
699 };
700
701
702 static int snd_opti93x_busy_wait(opti93x_t *chip)
703 {
704         int timeout;
705
706         for (timeout = 250; timeout-- > 0; udelay(10))
707                 if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_INIT))
708                         return 0;
709
710         snd_printk("chip still busy.\n");
711         return -EBUSY;
712 }
713
714 static unsigned char snd_opti93x_in(opti93x_t *chip, unsigned char reg)
715 {
716         snd_opti93x_busy_wait(chip);
717         outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
718         return inb(OPTi93X_PORT(chip, DATA));
719 }
720
721 static void snd_opti93x_out(opti93x_t *chip, unsigned char reg,
722                             unsigned char value)
723 {
724         snd_opti93x_busy_wait(chip);
725         outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
726         outb(value, OPTi93X_PORT(chip, DATA));
727 }
728
729 static void snd_opti93x_out_image(opti93x_t *chip, unsigned char reg,
730                                   unsigned char value)
731 {
732         snd_opti93x_out(chip, reg, chip->image[reg] = value);
733 }
734
735 static void snd_opti93x_out_mask(opti93x_t *chip, unsigned char reg,
736                                  unsigned char mask, unsigned char value)
737 {
738         snd_opti93x_out_image(chip, reg,
739                 (chip->image[reg] & ~mask) | (value & mask));
740 }
741
742
743 static void snd_opti93x_mce_up(opti93x_t *chip)
744 {
745         snd_opti93x_busy_wait(chip);
746
747         chip->mce_bit = OPTi93X_MCE;
748         if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE))
749                 outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
750 }
751
752 static void snd_opti93x_mce_down(opti93x_t *chip)
753 {
754         snd_opti93x_busy_wait(chip);
755
756         chip->mce_bit = 0;
757         if (inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE)
758                 outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
759 }
760
761 #define snd_opti93x_mute_reg(chip, reg, mute)   \
762         snd_opti93x_out(chip, reg, mute ? 0x80 : chip->image[reg]);
763
764 static void snd_opti93x_mute(opti93x_t *chip, int mute)
765 {
766         unsigned long flags;
767
768         spin_lock_irqsave(&chip->lock, flags);
769
770         mute = mute ? 1 : 0;
771         if (chip->mute == mute) {
772                 spin_unlock_irqrestore(&chip->lock, flags);
773                 return;
774         }
775         chip->mute = mute;
776
777         snd_opti93x_mute_reg(chip, OPTi93X_CD_LEFT_INPUT, mute);
778         snd_opti93x_mute_reg(chip, OPTi93X_CD_RIGHT_INPUT, mute);
779         switch (chip->hardware) {
780         case OPTi9XX_HW_82C930:
781                 snd_opti93x_mute_reg(chip, OPTi930_AUX_LEFT_INPUT, mute);
782                 snd_opti93x_mute_reg(chip, OPTi930_AUX_RIGHT_INPUT, mute);
783                 break;
784         case OPTi9XX_HW_82C931:
785         case OPTi9XX_HW_82C933:
786                 snd_opti93x_mute_reg(chip, OPTi931_FM_LEFT_INPUT, mute);
787                 snd_opti93x_mute_reg(chip, OPTi931_FM_RIGHT_INPUT, mute);
788                 snd_opti93x_mute_reg(chip, OPTi931_AUX_LEFT_INPUT, mute);
789                 snd_opti93x_mute_reg(chip, OPTi931_AUX_RIGHT_INPUT, mute);
790         }
791         snd_opti93x_mute_reg(chip, OPTi93X_DAC_LEFT, mute);
792         snd_opti93x_mute_reg(chip, OPTi93X_DAC_RIGHT, mute);
793         snd_opti93x_mute_reg(chip, OPTi93X_LINE_LEFT_INPUT, mute);
794         snd_opti93x_mute_reg(chip, OPTi93X_LINE_RIGHT_INPUT, mute);
795         snd_opti93x_mute_reg(chip, OPTi93X_MIC_LEFT_INPUT, mute);
796         snd_opti93x_mute_reg(chip, OPTi93X_MIC_RIGHT_INPUT, mute);
797         snd_opti93x_mute_reg(chip, OPTi93X_OUT_LEFT, mute);
798         snd_opti93x_mute_reg(chip, OPTi93X_OUT_RIGHT, mute);
799
800         spin_unlock_irqrestore(&chip->lock, flags);
801 }
802
803
804 static unsigned int snd_opti93x_get_count(unsigned char format,
805                                           unsigned int size)
806 {
807         switch (format & 0xe0) {
808         case OPTi93X_LINEAR_16_LIT:
809         case OPTi93X_LINEAR_16_BIG:
810                 size >>= 1;
811                 break;
812         case OPTi93X_ADPCM_16:
813                 return size >> 2;
814         }
815         return (format & OPTi93X_STEREO) ? (size >> 1) : size;
816 }
817
818 static unsigned int rates[] = {  5512,  6615,  8000,  9600, 11025, 16000, 
819                                 18900, 22050, 27428, 32000, 33075, 37800,
820                                 44100, 48000 };
821 #define RATES sizeof(rates) / sizeof(rates[0])
822
823 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
824         .count = RATES,
825         .list = rates,
826         .mask = 0,
827 };
828
829 static unsigned char bits[] = {  0x01,  0x0f,  0x00,  0x0e,  0x03,  0x02,
830                                  0x05,  0x07,  0x04,  0x06,  0x0d,  0x09,
831                                  0x0b,  0x0c};
832
833 static unsigned char snd_opti93x_get_freq(unsigned int rate)
834 {
835         unsigned int i;
836
837         for (i = 0; i < RATES; i++) {
838                 if (rate == rates[i])
839                         return bits[i];
840         }
841         snd_BUG();
842         return bits[RATES-1];
843 }
844
845 static unsigned char snd_opti93x_get_format(opti93x_t *chip,
846                                             unsigned int format, int channels)
847 {
848         unsigned char retval = OPTi93X_LINEAR_8;
849
850         switch (format) {
851         case SNDRV_PCM_FORMAT_MU_LAW:
852                 retval = OPTi93X_ULAW_8;
853                 break;
854         case SNDRV_PCM_FORMAT_A_LAW:
855                 retval = OPTi93X_ALAW_8;
856                 break;
857         case SNDRV_PCM_FORMAT_S16_LE:
858                 retval = OPTi93X_LINEAR_16_LIT;
859                 break;
860         case SNDRV_PCM_FORMAT_S16_BE:
861                 retval = OPTi93X_LINEAR_16_BIG;
862                 break;
863         case SNDRV_PCM_FORMAT_IMA_ADPCM:
864                 retval = OPTi93X_ADPCM_16;
865         }
866         return (channels > 1) ? (retval | OPTi93X_STEREO) : retval;
867 }
868
869
870 static void snd_opti93x_playback_format(opti93x_t *chip, unsigned char fmt)
871 {
872         unsigned long flags;
873         unsigned char mask;
874
875         spin_lock_irqsave(&chip->lock, flags);
876         snd_opti93x_mute(chip, 1);
877
878         snd_opti93x_mce_up(chip);
879         mask = (chip->mode & OPTi93X_MODE_CAPTURE) ? 0xf0 : 0xff;
880         snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, mask, fmt);
881         snd_opti93x_mce_down(chip);
882
883         snd_opti93x_mute(chip, 0);
884         spin_unlock_irqrestore(&chip->lock, flags);
885 }
886
887 static void snd_opti93x_capture_format(opti93x_t *chip, unsigned char fmt)
888 {
889         unsigned long flags;
890
891         spin_lock_irqsave(&chip->lock, flags);
892         snd_opti93x_mute(chip, 1);
893
894         snd_opti93x_mce_up(chip);
895         if (!(chip->mode & OPTi93X_MODE_PLAY))
896                 snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, 0x0f, fmt);
897         else
898                 fmt = chip->image[OPTi93X_PLAY_FORMAT] & 0xf0;
899         snd_opti93x_out_image(chip, OPTi93X_CAPT_FORMAT, fmt);
900         snd_opti93x_mce_down(chip);
901
902         snd_opti93x_mute(chip, 0);
903         spin_unlock_irqrestore(&chip->lock, flags);
904 }
905
906
907 static int snd_opti93x_open(opti93x_t *chip, unsigned int mode)
908 {
909         unsigned long flags;
910
911         spin_lock_irqsave(&chip->lock, flags);
912
913         if (chip->mode & mode) {
914                 spin_unlock_irqrestore(&chip->lock, flags);
915                 return -EAGAIN;
916         }
917
918         if (!(chip->mode & OPTi93X_MODE_OPEN)) {
919                 outb(0x00, OPTi93X_PORT(chip, STATUS));
920                 snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL,
921                         OPTi93X_IRQ_ENABLE, OPTi93X_IRQ_ENABLE);
922                 chip->mode = mode;
923         }
924         else
925                 chip->mode |= mode;
926
927         spin_unlock_irqrestore(&chip->lock, flags);
928         return 0;
929 }
930
931 static void snd_opti93x_close(opti93x_t *chip, unsigned int mode)
932 {
933         unsigned long flags;
934
935         spin_lock_irqsave(&chip->lock, flags);
936
937         chip->mode &= ~mode;
938         if (chip->mode & OPTi93X_MODE_OPEN) {
939                 spin_unlock_irqrestore(&chip->lock, flags);
940                 return;
941         }
942
943         snd_opti93x_mute(chip, 1);
944
945         outb(0, OPTi93X_PORT(chip, STATUS));
946         snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL, OPTi93X_IRQ_ENABLE,
947                 ~OPTi93X_IRQ_ENABLE);
948
949         snd_opti93x_mce_up(chip);
950         snd_opti93x_out_image(chip, OPTi93X_IFACE_CONF, 0x00);
951         snd_opti93x_mce_down(chip);
952         chip->mode = 0;
953
954         snd_opti93x_mute(chip, 0);
955         spin_unlock_irqrestore(&chip->lock, flags);
956 }
957
958 static int snd_opti93x_trigger(snd_pcm_substream_t *substream, 
959                                unsigned char what, int cmd)
960 {
961         opti93x_t *chip = snd_pcm_substream_chip(substream);
962
963         switch (cmd) {
964         case SNDRV_PCM_TRIGGER_START:
965         case SNDRV_PCM_TRIGGER_STOP:
966         {
967                 unsigned int what = 0;
968                 struct list_head *pos;
969                 snd_pcm_substream_t *s;
970                 snd_pcm_group_for_each(pos, substream) {
971                         s = snd_pcm_group_substream_entry(pos);
972                         if (s == chip->playback_substream) {
973                                 what |= OPTi93X_PLAYBACK_ENABLE;
974                                 snd_pcm_trigger_done(s, substream);
975                         } else if (s == chip->capture_substream) {
976                                 what |= OPTi93X_CAPTURE_ENABLE;
977                                 snd_pcm_trigger_done(s, substream);
978                         }
979                 }
980                 spin_lock(&chip->lock);
981                 if (cmd == SNDRV_PCM_TRIGGER_START) {
982                         snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, what);
983                         if (what & OPTi93X_CAPTURE_ENABLE)
984                                 udelay(50);
985                 } else
986                         snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, 0x00);
987                 spin_unlock(&chip->lock);
988                 break;
989         }
990         default:
991                 return -EINVAL;
992         }
993         return 0;
994 }
995
996 static int snd_opti93x_playback_trigger(snd_pcm_substream_t *substream, int cmd)
997 {
998         return snd_opti93x_trigger(substream,
999                                    OPTi93X_PLAYBACK_ENABLE, cmd);
1000 }
1001
1002 static int snd_opti93x_capture_trigger(snd_pcm_substream_t * substream, int cmd)
1003 {
1004         return snd_opti93x_trigger(substream,
1005                                    OPTi93X_CAPTURE_ENABLE, cmd);
1006 }
1007
1008 static int snd_opti93x_hw_params(snd_pcm_substream_t * substream,
1009                                  snd_pcm_hw_params_t * hw_params)
1010 {
1011         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1012 }
1013
1014
1015 static int snd_opti93x_hw_free(snd_pcm_substream_t * substream)
1016 {
1017         snd_pcm_lib_free_pages(substream);
1018         return 0;
1019 }
1020
1021
1022 static int snd_opti93x_playback_prepare(snd_pcm_substream_t * substream)
1023 {
1024         opti93x_t *chip = snd_pcm_substream_chip(substream);
1025         snd_pcm_runtime_t *runtime = substream->runtime;
1026         unsigned long flags;
1027         unsigned char format;
1028         unsigned int count = snd_pcm_lib_period_bytes(substream);
1029         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1030
1031         spin_lock_irqsave(&chip->lock, flags);
1032
1033         chip->p_dma_size = size;
1034         snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
1035                 OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO,
1036                 ~(OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO));
1037
1038         snd_dma_program(chip->dma1, runtime->dma_addr, size,
1039                 DMA_MODE_WRITE | DMA_AUTOINIT);
1040
1041         format = snd_opti93x_get_freq(runtime->rate);
1042         format |= snd_opti93x_get_format(chip, runtime->format,
1043                 runtime->channels);
1044         snd_opti93x_playback_format(chip, format);
1045         format = chip->image[OPTi93X_PLAY_FORMAT];
1046
1047         count = snd_opti93x_get_count(format, count) - 1;
1048         snd_opti93x_out_image(chip, OPTi93X_PLAY_LWR_CNT, count);
1049         snd_opti93x_out_image(chip, OPTi93X_PLAY_UPR_CNT, count >> 8);
1050
1051         spin_unlock_irqrestore(&chip->lock, flags);
1052         return 0;
1053 }
1054
1055 static int snd_opti93x_capture_prepare(snd_pcm_substream_t *substream)
1056 {
1057         opti93x_t *chip = snd_pcm_substream_chip(substream);
1058         snd_pcm_runtime_t *runtime = substream->runtime;
1059         unsigned long flags;
1060         unsigned char format;
1061         unsigned int count = snd_pcm_lib_period_bytes(substream);
1062         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1063
1064         spin_lock_irqsave(&chip->lock, flags);
1065
1066         chip->c_dma_size = size;
1067         snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
1068                 OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO,
1069                 (unsigned char)~(OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO));
1070
1071         snd_dma_program(chip->dma2, runtime->dma_addr, size,
1072                 DMA_MODE_READ | DMA_AUTOINIT);
1073
1074         format = snd_opti93x_get_freq(runtime->rate);
1075         format |= snd_opti93x_get_format(chip, runtime->format,
1076                 runtime->channels);
1077         snd_opti93x_capture_format(chip, format);
1078         format = chip->image[OPTi93X_CAPT_FORMAT];
1079
1080         count = snd_opti93x_get_count(format, count) - 1;
1081         snd_opti93x_out_image(chip, OPTi93X_CAPT_LWR_CNT, count);
1082         snd_opti93x_out_image(chip, OPTi93X_CAPT_UPR_CNT, count >> 8);
1083
1084         spin_unlock_irqrestore(&chip->lock, flags);
1085         return 0;
1086 }
1087
1088 static snd_pcm_uframes_t snd_opti93x_playback_pointer(snd_pcm_substream_t *substream)
1089 {
1090         opti93x_t *chip = snd_pcm_substream_chip(substream);
1091         size_t ptr;
1092
1093         if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_PLAYBACK_ENABLE))
1094                 return 0;
1095
1096         ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
1097         return bytes_to_frames(substream->runtime, ptr);
1098 }
1099
1100 static snd_pcm_uframes_t snd_opti93x_capture_pointer(snd_pcm_substream_t *substream)
1101 {
1102         opti93x_t *chip = snd_pcm_substream_chip(substream);
1103         size_t ptr;
1104         
1105         if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_CAPTURE_ENABLE))
1106                 return 0;
1107
1108         ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
1109         return bytes_to_frames(substream->runtime, ptr);
1110 }
1111
1112
1113 static void snd_opti93x_overrange(opti93x_t *chip)
1114 {
1115         unsigned long flags;
1116
1117         spin_lock_irqsave(&chip->lock, flags);
1118
1119         if (snd_opti93x_in(chip, OPTi93X_ERR_INIT) & (0x08 | 0x02))
1120                 chip->capture_substream->runtime->overrange++;
1121
1122         spin_unlock_irqrestore(&chip->lock, flags);
1123 }
1124
1125 irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1126 {
1127         opti93x_t *codec = snd_magic_cast(opti93x_t, dev_id, return IRQ_NONE);
1128         unsigned char status;
1129
1130         status = snd_opti9xx_read(codec->chip, OPTi9XX_MC_REG(11));
1131         if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
1132                 snd_pcm_period_elapsed(codec->playback_substream);
1133         if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
1134                 snd_opti93x_overrange(codec);
1135                 snd_pcm_period_elapsed(codec->capture_substream);
1136         }
1137         outb(0x00, OPTi93X_PORT(codec, STATUS));
1138         return IRQ_HANDLED;
1139 }
1140
1141
1142 static snd_pcm_hardware_t snd_opti93x_playback = {
1143         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1144                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1145         .formats =              (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1146                                  SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1147         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1148         .rate_min =             5512,
1149         .rate_max =             48000,
1150         .channels_min =         1,
1151         .channels_max =         2,
1152         .buffer_bytes_max =     (128*1024),
1153         .period_bytes_min =     64,
1154         .period_bytes_max =     (128*1024),
1155         .periods_min =          1,
1156         .periods_max =          1024,
1157         .fifo_size =            0,
1158 };
1159
1160 static snd_pcm_hardware_t snd_opti93x_capture = {
1161         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1162                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1163         .formats =              (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1164                                  SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1165         .rates =                SNDRV_PCM_RATE_8000_48000,
1166         .rate_min =             5512,
1167         .rate_max =             48000,
1168         .channels_min =         1,
1169         .channels_max =         2,
1170         .buffer_bytes_max =     (128*1024),
1171         .period_bytes_min =     64,
1172         .period_bytes_max =     (128*1024),
1173         .periods_min =          1,
1174         .periods_max =          1024,
1175         .fifo_size =            0,
1176 };
1177
1178 static int snd_opti93x_playback_open(snd_pcm_substream_t *substream)
1179 {
1180         int error;
1181         opti93x_t *chip = snd_pcm_substream_chip(substream);
1182         snd_pcm_runtime_t *runtime = substream->runtime;
1183
1184         if ((error = snd_opti93x_open(chip, OPTi93X_MODE_PLAY)) < 0)
1185                 return error;
1186         snd_pcm_set_sync(substream);
1187         chip->playback_substream = substream;
1188         runtime->hw = snd_opti93x_playback;
1189         snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1190         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1191         return error;
1192 }
1193
1194 static int snd_opti93x_capture_open(snd_pcm_substream_t *substream)
1195 {
1196         int error;
1197         opti93x_t *chip = snd_pcm_substream_chip(substream);
1198         snd_pcm_runtime_t *runtime = substream->runtime;
1199
1200         if ((error = snd_opti93x_open(chip, OPTi93X_MODE_CAPTURE)) < 0)
1201                 return error;
1202         runtime->hw = snd_opti93x_capture;
1203         snd_pcm_set_sync(substream);
1204         chip->capture_substream = substream;
1205         snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1206         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1207         return error;
1208 }
1209
1210 static int snd_opti93x_playback_close(snd_pcm_substream_t *substream)
1211 {
1212         opti93x_t *chip = snd_pcm_substream_chip(substream);
1213
1214         chip->playback_substream = NULL;
1215         snd_opti93x_close(chip, OPTi93X_MODE_PLAY);
1216         return 0;
1217 }
1218
1219 static int snd_opti93x_capture_close(snd_pcm_substream_t *substream)
1220 {
1221         opti93x_t *chip = snd_pcm_substream_chip(substream);
1222
1223         chip->capture_substream = NULL;
1224         snd_opti93x_close(chip, OPTi93X_MODE_CAPTURE);
1225         return 0;
1226 }
1227
1228
1229 static void snd_opti93x_init(opti93x_t *chip)
1230 {
1231         unsigned long flags;
1232         int i;
1233
1234         spin_lock_irqsave(&chip->lock, flags);
1235         snd_opti93x_mce_up(chip);
1236
1237         for (i = 0; i < 32; i++)
1238                 snd_opti93x_out_image(chip, i, snd_opti93x_default_image[i]);
1239
1240         snd_opti93x_mce_down(chip);
1241         spin_unlock_irqrestore(&chip->lock, flags);
1242 }
1243
1244 static int snd_opti93x_probe(opti93x_t *chip)
1245 {
1246         unsigned long flags;
1247         unsigned char val;
1248
1249         spin_lock_irqsave(&chip->lock, flags);
1250         val = snd_opti93x_in(chip, OPTi93X_ID) & 0x0f;
1251         spin_unlock_irqrestore(&chip->lock, flags);
1252
1253         return (val == 0x0a) ? 0 : -ENODEV;
1254 }
1255
1256 static int snd_opti93x_free(opti93x_t *chip)
1257 {
1258         if (chip->res_port) {
1259                 release_resource(chip->res_port);
1260                 kfree_nocheck(chip->res_port);
1261         }
1262         if (chip->dma1 >= 0) {
1263                 disable_dma(chip->dma1);
1264                 free_dma(chip->dma1);
1265         }
1266         if (chip->dma2 >= 0) {
1267                 disable_dma(chip->dma2);
1268                 free_dma(chip->dma2);
1269         }
1270         if (chip->irq >= 0) {
1271           free_irq(chip->irq, chip);
1272         }
1273         snd_magic_kfree(chip);
1274         return 0;
1275 }
1276
1277 static int snd_opti93x_dev_free(snd_device_t *device)
1278 {
1279         opti93x_t *chip = snd_magic_cast(opti93x_t, device->device_data, return -ENXIO);
1280         return snd_opti93x_free(chip);
1281 }
1282
1283 static const char *snd_opti93x_chip_id(opti93x_t *codec)
1284 {
1285         switch (codec->hardware) {
1286         case OPTi9XX_HW_82C930: return "82C930";
1287         case OPTi9XX_HW_82C931: return "82C931";
1288         case OPTi9XX_HW_82C933: return "82C933";
1289         default:                return "???";
1290         }
1291 }
1292
1293 int snd_opti93x_create(snd_card_t *card, opti9xx_t *chip,
1294                        int dma1, int dma2,
1295                        opti93x_t **rcodec)
1296 {
1297         static snd_device_ops_t ops = {
1298                 .dev_free =     snd_opti93x_dev_free,
1299         };
1300         int error;
1301         opti93x_t *codec;
1302
1303         *rcodec = NULL;
1304         codec = snd_magic_kcalloc(opti93x_t, 0, GFP_KERNEL);
1305         if (codec == NULL)
1306                 return -ENOMEM;
1307         codec->irq = -1;
1308         codec->dma1 = -1;
1309         codec->dma2 = -1;
1310
1311         if ((codec->res_port = request_region(chip->wss_base + 4, 4, "OPTI93x CODEC")) == NULL) {
1312                 snd_printk(KERN_ERR "opti9xx: can't grab port 0x%lx\n", chip->wss_base + 4);
1313                 snd_opti93x_free(codec);
1314                 return -EBUSY;
1315         }
1316         if (request_dma(dma1, "OPTI93x - 1")) {
1317                 snd_printk(KERN_ERR "opti9xx: can't grab DMA1 %d\n", dma1);
1318                 snd_opti93x_free(codec);
1319                 return -EBUSY;
1320         }
1321         codec->dma1 = chip->dma1;
1322         if (request_dma(dma2, "OPTI93x - 2")) {
1323                 snd_printk(KERN_ERR "opti9xx: can't grab DMA2 %d\n", dma2);
1324                 snd_opti93x_free(codec);
1325                 return -EBUSY;
1326         }
1327         codec->dma2 = chip->dma2;
1328
1329         if (request_irq(chip->irq, snd_opti93x_interrupt, SA_INTERRUPT, DRIVER_NAME" - WSS", codec)) {
1330                 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
1331                 snd_opti93x_free(codec);
1332                 return -EBUSY;
1333         }
1334
1335         codec->card = card;
1336         codec->port = chip->wss_base + 4;
1337         codec->irq = chip->irq;
1338
1339         spin_lock_init(&codec->lock);
1340         codec->hardware = chip->hardware;
1341         codec->chip = chip;
1342
1343         if ((error = snd_opti93x_probe(codec))) {
1344                 snd_opti93x_free(codec);
1345                 return error;
1346         }
1347
1348         snd_opti93x_init(codec);
1349
1350         /* Register device */
1351         if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
1352                 snd_opti93x_free(codec);
1353                 return error;
1354         }
1355
1356         *rcodec = codec;
1357         return 0;
1358 }
1359
1360 static snd_pcm_ops_t snd_opti93x_playback_ops = {
1361         .open =         snd_opti93x_playback_open,
1362         .close =        snd_opti93x_playback_close,
1363         .ioctl =        snd_pcm_lib_ioctl,
1364         .hw_params =    snd_opti93x_hw_params,
1365         .hw_free =      snd_opti93x_hw_free,
1366         .prepare =      snd_opti93x_playback_prepare,
1367         .trigger =      snd_opti93x_playback_trigger,
1368         .pointer =      snd_opti93x_playback_pointer,
1369 };
1370
1371 static snd_pcm_ops_t snd_opti93x_capture_ops = {
1372         .open =         snd_opti93x_capture_open,
1373         .close =        snd_opti93x_capture_close,
1374         .ioctl =        snd_pcm_lib_ioctl,
1375         .hw_params =    snd_opti93x_hw_params,
1376         .hw_free =      snd_opti93x_hw_free,
1377         .prepare =      snd_opti93x_capture_prepare,
1378         .trigger =      snd_opti93x_capture_trigger,
1379         .pointer =      snd_opti93x_capture_pointer,
1380 };
1381
1382 static void snd_opti93x_pcm_free(snd_pcm_t *pcm)
1383 {
1384         opti93x_t *codec = snd_magic_cast(opti93x_t, pcm->private_data, return);
1385         codec->pcm = NULL;
1386         snd_pcm_lib_preallocate_free_for_all(pcm);
1387 }
1388
1389 int snd_opti93x_pcm(opti93x_t *codec, int device, snd_pcm_t **rpcm)
1390 {
1391         int error;
1392         snd_pcm_t *pcm;
1393
1394         if ((error = snd_pcm_new(codec->card, "OPTi 82C93X", device, 1, 1, &pcm)))
1395                 return error;
1396
1397         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_opti93x_playback_ops);
1398         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_opti93x_capture_ops);
1399
1400         pcm->private_data = codec;
1401         pcm->private_free = snd_opti93x_pcm_free;
1402         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1403
1404         strcpy(pcm->name, snd_opti93x_chip_id(codec));
1405
1406         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1407                                               snd_dma_isa_data(),
1408                                               64*1024, codec->dma1 > 3 || codec->dma2 > 3 ? 128*1024 : 64*1024);
1409
1410         codec->pcm = pcm;
1411         if (rpcm)
1412                 *rpcm = pcm;
1413         return 0;
1414 }
1415
1416 /*
1417  *  MIXER part
1418  */
1419
1420 static int snd_opti93x_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1421 {
1422         static char *texts[4] = {
1423                 "Line1", "Aux", "Mic", "Mix"
1424         };
1425
1426         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1427         uinfo->count = 2;
1428         uinfo->value.enumerated.items = 4;
1429         if (uinfo->value.enumerated.item > 3)
1430                 uinfo->value.enumerated.item = 3;
1431         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1432         return 0;
1433 }
1434
1435 static int snd_opti93x_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1436 {
1437         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1438         unsigned long flags;
1439         
1440         spin_lock_irqsave(&chip->lock, flags);
1441         ucontrol->value.enumerated.item[0] = (chip->image[OPTi93X_MIXOUT_LEFT] & OPTi93X_MIXOUT_MIXER) >> 6;
1442         ucontrol->value.enumerated.item[1] = (chip->image[OPTi93X_MIXOUT_RIGHT] & OPTi93X_MIXOUT_MIXER) >> 6;
1443         spin_unlock_irqrestore(&chip->lock, flags);
1444         return 0;
1445 }
1446
1447 static int snd_opti93x_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1448 {
1449         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1450         unsigned long flags;
1451         unsigned short left, right;
1452         int change;
1453         
1454         if (ucontrol->value.enumerated.item[0] > 3 ||
1455             ucontrol->value.enumerated.item[1] > 3)
1456                 return -EINVAL;
1457         left = ucontrol->value.enumerated.item[0] << 6;
1458         right = ucontrol->value.enumerated.item[1] << 6;
1459         spin_lock_irqsave(&chip->lock, flags);
1460         left = (chip->image[OPTi93X_MIXOUT_LEFT] & ~OPTi93X_MIXOUT_MIXER) | left;
1461         right = (chip->image[OPTi93X_MIXOUT_RIGHT] & ~OPTi93X_MIXOUT_MIXER) | right;
1462         change = left != chip->image[OPTi93X_MIXOUT_LEFT] ||
1463                  right != chip->image[OPTi93X_MIXOUT_RIGHT];
1464         snd_opti93x_out_image(chip, OPTi93X_MIXOUT_LEFT, left);
1465         snd_opti93x_out_image(chip, OPTi93X_MIXOUT_RIGHT, right);
1466         spin_unlock_irqrestore(&chip->lock, flags);
1467         return change;
1468 }
1469
1470 #if 0
1471
1472 #define OPTi93X_SINGLE(xname, xindex, reg, shift, mask, invert) \
1473 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1474   .info = snd_opti93x_info_single, \
1475   .get = snd_opti93x_get_single, .put = snd_opti93x_put_single, \
1476   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1477
1478 static int snd_opti93x_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1479 {
1480         int mask = (kcontrol->private_value >> 16) & 0xff;
1481
1482         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1483         uinfo->count = 1;
1484         uinfo->value.integer.min = 0;
1485         uinfo->value.integer.max = mask;
1486         return 0;
1487 }
1488
1489 static int snd_opti93x_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1490 {
1491         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1492         unsigned long flags;
1493         int reg = kcontrol->private_value & 0xff;
1494         int shift = (kcontrol->private_value >> 8) & 0xff;
1495         int mask = (kcontrol->private_value >> 16) & 0xff;
1496         int invert = (kcontrol->private_value >> 24) & 0xff;
1497         
1498         spin_lock_irqsave(&chip->lock, flags);
1499         ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1500         spin_unlock_irqrestore(&chip->lock, flags);
1501         if (invert)
1502                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1503         return 0;
1504 }
1505
1506 static int snd_opti93x_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1507 {
1508         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1509         unsigned long flags;
1510         int reg = kcontrol->private_value & 0xff;
1511         int shift = (kcontrol->private_value >> 8) & 0xff;
1512         int mask = (kcontrol->private_value >> 16) & 0xff;
1513         int invert = (kcontrol->private_value >> 24) & 0xff;
1514         int change;
1515         unsigned short val;
1516         
1517         val = (ucontrol->value.integer.value[0] & mask);
1518         if (invert)
1519                 val = mask - val;
1520         val <<= shift;
1521         spin_lock_irqsave(&chip->lock, flags);
1522         val = (chip->image[reg] & ~(mask << shift)) | val;
1523         change = val != chip->image[reg];
1524         snd_opti93x_out(chip, reg, val);
1525         spin_unlock_irqrestore(&chip->lock, flags);
1526         return change;
1527 }
1528
1529 #endif /* single */
1530
1531 #define OPTi93X_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1532 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1533   .info = snd_opti93x_info_double, \
1534   .get = snd_opti93x_get_double, .put = snd_opti93x_put_double, \
1535   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1536
1537 #define OPTi93X_DOUBLE_INVERT_INVERT(xctl) \
1538         do { xctl.private_value ^= 22; } while (0)
1539 #define OPTi93X_DOUBLE_CHANGE_REGS(xctl, left_reg, right_reg) \
1540         do { xctl.private_value &= ~0x0000ffff; \
1541              xctl.private_value |= left_reg | (right_reg << 8); } while (0)
1542
1543 static int snd_opti93x_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1544 {
1545         int mask = (kcontrol->private_value >> 24) & 0xff;
1546
1547         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1548         uinfo->count = 2;
1549         uinfo->value.integer.min = 0;
1550         uinfo->value.integer.max = mask;
1551         return 0;
1552 }
1553
1554 static int snd_opti93x_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1555 {
1556         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1557         unsigned long flags;
1558         int left_reg = kcontrol->private_value & 0xff;
1559         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1560         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1561         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1562         int mask = (kcontrol->private_value >> 24) & 0xff;
1563         int invert = (kcontrol->private_value >> 22) & 1;
1564         
1565         spin_lock_irqsave(&chip->lock, flags);
1566         ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1567         ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1568         spin_unlock_irqrestore(&chip->lock, flags);
1569         if (invert) {
1570                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1571                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1572         }
1573         return 0;
1574 }
1575
1576 static int snd_opti93x_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1577 {
1578         opti93x_t *chip = snd_kcontrol_chip(kcontrol);
1579         unsigned long flags;
1580         int left_reg = kcontrol->private_value & 0xff;
1581         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1582         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1583         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1584         int mask = (kcontrol->private_value >> 24) & 0xff;
1585         int invert = (kcontrol->private_value >> 22) & 1;
1586         int change;
1587         unsigned short val1, val2;
1588         
1589         val1 = ucontrol->value.integer.value[0] & mask;
1590         val2 = ucontrol->value.integer.value[1] & mask;
1591         if (invert) {
1592                 val1 = mask - val1;
1593                 val2 = mask - val2;
1594         }
1595         val1 <<= shift_left;
1596         val2 <<= shift_right;
1597         spin_lock_irqsave(&chip->lock, flags);
1598         val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1599         val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1600         change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1601         snd_opti93x_out_image(chip, left_reg, val1);
1602         snd_opti93x_out_image(chip, right_reg, val2);
1603         spin_unlock_irqrestore(&chip->lock, flags);
1604         return change;
1605 }
1606
1607 #define OPTi93X_CONTROLS (sizeof(snd_opti93x_controls)/sizeof(snd_kcontrol_new_t))
1608
1609 static snd_kcontrol_new_t snd_opti93x_controls[] = {
1610 OPTi93X_DOUBLE("Master Playback Switch", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
1611 OPTi93X_DOUBLE("Master Playback Volume", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1), 
1612 OPTi93X_DOUBLE("PCM Playback Switch", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 7, 7, 1, 1),
1613 OPTi93X_DOUBLE("PCM Playback Volume", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 0, 0, 31, 1),
1614 OPTi93X_DOUBLE("FM Playback Switch", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 7, 7, 1, 1),
1615 OPTi93X_DOUBLE("FM Playback Volume", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 1, 1, 15, 1),
1616 OPTi93X_DOUBLE("Line Playback Switch", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 7, 7, 1, 1),
1617 OPTi93X_DOUBLE("Line Playback Volume", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 1, 1, 15, 1), 
1618 OPTi93X_DOUBLE("Mic Playback Switch", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
1619 OPTi93X_DOUBLE("Mic Playback Volume", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1), 
1620 OPTi93X_DOUBLE("Mic Boost", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 5, 5, 1, 1),
1621 OPTi93X_DOUBLE("CD Playback Switch", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 7, 7, 1, 1),
1622 OPTi93X_DOUBLE("CD Playback Volume", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 1, 1, 15, 1),
1623 OPTi93X_DOUBLE("Aux Playback Switch", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
1624 OPTi93X_DOUBLE("Aux Playback Volume", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1), 
1625 OPTi93X_DOUBLE("Capture Volume", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 0, 0, 15, 0),
1626 {
1627         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1628         .name = "Capture Source",
1629         .info = snd_opti93x_info_mux,
1630         .get = snd_opti93x_get_mux,
1631         .put = snd_opti93x_put_mux,
1632 }
1633 };
1634                                         
1635 int snd_opti93x_mixer(opti93x_t *chip)
1636 {
1637         snd_card_t *card;
1638         snd_kcontrol_new_t knew;
1639         int err;
1640         unsigned int idx;
1641
1642         snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
1643
1644         card = chip->card;
1645
1646         strcpy(card->mixername, snd_opti93x_chip_id(chip));
1647
1648         for (idx = 0; idx < OPTi93X_CONTROLS; idx++) {
1649                 knew = snd_opti93x_controls[idx];
1650                 if (chip->hardware == OPTi9XX_HW_82C930) {
1651                         if (strstr(knew.name, "FM"))    /* skip FM controls */
1652                                 continue;
1653                         else if (strcmp(knew.name, "Mic Playback Volume"))
1654                                 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1655                         else if (strstr(knew.name, "Aux"))
1656                                 OPTi93X_DOUBLE_CHANGE_REGS(knew, OPTi930_AUX_LEFT_INPUT, OPTi930_AUX_RIGHT_INPUT);
1657                         else if (strcmp(knew.name, "PCM Playback Volume"))
1658                                 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1659                         else if (strcmp(knew.name, "Master Playback Volume"))
1660                                 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1661                 }
1662                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opti93x_controls[idx], chip))) < 0)
1663                         return err;
1664         }
1665         return 0;
1666 }
1667
1668 #endif /* OPTi93X */
1669
1670 static int __devinit snd_card_opti9xx_detect(snd_card_t *card, opti9xx_t *chip)
1671 {
1672         int i, err;
1673
1674 #ifndef OPTi93X
1675         for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
1676                 unsigned char value;
1677
1678                 if ((err = snd_opti9xx_init(chip, i)) < 0)
1679                         return err;
1680
1681                 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1682                         continue;
1683
1684                 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
1685                 if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
1686                         if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
1687                                 return 1;
1688
1689                 release_resource(chip->res_mc_base);
1690                 kfree_nocheck(chip->res_mc_base);
1691                 chip->res_mc_base = NULL;
1692
1693         }
1694 #else   /* OPTi93X */
1695         for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
1696                 unsigned long flags;
1697                 unsigned char value;
1698
1699                 if ((err = snd_opti9xx_init(chip, i)) < 0)
1700                         return err;
1701
1702                 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1703                         continue;
1704
1705                 spin_lock_irqsave(&chip->lock, flags);
1706                 outb(chip->password, chip->mc_base + chip->pwd_reg);
1707                 outb(((chip->mc_indir_index & (1 << 8)) >> 4) |
1708                         ((chip->mc_indir_index & 0xf0) >> 4), chip->mc_base);
1709                 spin_unlock_irqrestore(&chip->lock, flags);
1710
1711                 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
1712                 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
1713                 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
1714                         return 1;
1715
1716                 release_resource(chip->res_mc_base);
1717                 kfree_nocheck(chip->res_mc_base);
1718                 chip->res_mc_base = NULL;
1719         }
1720 #endif  /* OPTi93X */
1721
1722         return -ENODEV;
1723 }
1724
1725 #ifdef CONFIG_PNP
1726 static int __devinit snd_card_opti9xx_pnp(opti9xx_t *chip, struct pnp_card_link *card,
1727                                           const struct pnp_card_device_id *pid)
1728 {
1729         struct pnp_dev *pdev;
1730         struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
1731         int err;
1732
1733         chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1734         if (chip->dev == NULL) {
1735                 kfree(cfg);
1736                 return -EBUSY;
1737         }
1738         chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1739
1740         pdev = chip->dev;
1741         pnp_init_resource_table(cfg);
1742
1743 #ifdef OPTi93X
1744         if (port != SNDRV_AUTO_PORT)
1745                 pnp_resource_change(&cfg->port_resource[0], port + 4, 4);
1746 #else
1747         if (pid->driver_data != 0x0924 && port != SNDRV_AUTO_PORT)
1748                 pnp_resource_change(&cfg->port_resource[1], port, 4);
1749 #endif  /* OPTi93X */
1750         if (irq != SNDRV_AUTO_IRQ)
1751                 pnp_resource_change(&cfg->irq_resource[0], irq, 1);
1752         if (dma1 != SNDRV_AUTO_DMA)
1753                 pnp_resource_change(&cfg->dma_resource[0], dma1, 1);
1754 #if defined(CS4231) || defined(OPTi93X)
1755         if (dma2 != SNDRV_AUTO_DMA)
1756                 pnp_resource_change(&cfg->dma_resource[1], dma2, 1);
1757 #else
1758 #ifdef snd_opti9xx_fixup_dma2
1759         snd_opti9xx_fixup_dma2(pdev);
1760 #endif
1761 #endif  /* CS4231 || OPTi93X */
1762 #ifdef OPTi93X
1763         if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
1764                 pnp_resource_change(&cfg->port_resource[1], fm_port, 4);
1765 #else
1766         if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
1767                 pnp_resource_change(&cfg->port_resource[2], fm_port, 4);
1768 #endif
1769         if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
1770                 snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
1771         err = pnp_activate_dev(pdev);
1772         if (err < 0) {
1773                 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
1774                 kfree(cfg);
1775                 return err;
1776         }
1777
1778 #ifdef OPTi93X
1779         port = pnp_port_start(pdev, 0) - 4;
1780         fm_port = pnp_port_start(pdev, 1);
1781 #else
1782         if (pid->driver_data != 0x0924)
1783                 port = pnp_port_start(pdev, 1);
1784         fm_port = pnp_port_start(pdev, 2);
1785 #endif  /* OPTi93X */
1786         irq = pnp_irq(pdev, 0);
1787         dma1 = pnp_dma(pdev, 0);
1788 #if defined(CS4231) || defined(OPTi93X)
1789         dma2 = pnp_dma(pdev, 1);
1790 #endif  /* CS4231 || OPTi93X */
1791
1792         pdev = chip->devmpu;
1793         if (pdev && mpu_port > 0) {
1794                 pnp_init_resource_table(cfg);
1795
1796                 if (mpu_port != SNDRV_AUTO_PORT)
1797                         pnp_resource_change(&cfg->port_resource[0], mpu_port, 2);
1798                 if (mpu_irq != SNDRV_AUTO_IRQ)
1799                         pnp_resource_change(&cfg->irq_resource[0], mpu_irq, 1);
1800
1801                 if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
1802                         snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
1803                 err = pnp_activate_dev(pdev);
1804                 if (err < 0) {
1805                         snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
1806                         mpu_port = -1;
1807                         chip->devmpu = NULL;
1808                 } else {
1809                         mpu_port = pnp_port_start(pdev, 0);
1810                         mpu_irq = pnp_irq(pdev, 0);
1811                 }
1812         }
1813         kfree(cfg);
1814         return pid->driver_data;
1815 }
1816 #endif  /* CONFIG_PNP */
1817
1818 #if 0
1819 static int __devinit snd_card_opti9xx_resources(struct snd_card_opti9xx *chip,
1820                                                 snd_card_t *card)
1821 {
1822         int error, i, pnp = 0;
1823
1824 #ifdef CONFIG_PNP
1825         pnp = chip->dev != NULL;
1826 #endif  /* CONFIG_PNP */
1827
1828 #ifndef OPTi93X
1829         if (chip->chip->hardware == OPTi9XX_HW_82C928)
1830                 mpu_port = -1;
1831 #endif  /* OPTi93X */
1832         error = 0;
1833         if (!pnp && (mpu_port == SNDRV_DEFAULT_PORT1)) {
1834                 for (i = 0; possible_mpu_ports[i] != -1; i++)
1835                         if (!snd_register_ioport(card, possible_mpu_ports[i], 2,
1836                                         DRIVER_NAME" - MPU-401", NULL)) {
1837                                 mpu_port = possible_mpu_ports[i];
1838                                 break;
1839                         }
1840                 if (mpu_port == SNDRV_DEFAULT_PORT1)
1841                         error = -EBUSY;
1842         }
1843         else
1844                 error = (mpu_port == -1) ? -ENODEV :
1845                         snd_register_ioport(card, mpu_port, 2,
1846                         DRIVER_NAME" - MPU-401", NULL);
1847         if (error)
1848                 chip->chip->mpu_port = -1;
1849         else if (pnp && (irq == mpu_irq))
1850                 chip->chip->mpu_irq = mpu_irq;
1851         else if (!snd_register_interrupt(card,
1852                         DRIVER_NAME" - MPU-401",
1853                         mpu_irq, SNDRV_IRQ_TYPE_ISA,
1854                         snd_card_opti9xx_mpu_interrupt, chip,
1855                         pnp ? no_alternatives : possible_mpu_irqs,
1856                         &chip->mpuirqptr)) {
1857                 chip->chip->mpu_port = mpu_port;
1858                 chip->chip->mpu_irq = chip->mpuirqptr->irq;
1859         }
1860         else
1861                 chip->chip->mpu_port = -1;
1862
1863         if (!pnp && (port == SNDRV_DEFAULT_PORT1)) {
1864                 for (i = 0; possible_ports[i] != -1; i++)
1865                         if (!snd_register_ioport(card, possible_ports[i], 8,
1866                                         DRIVER_NAME" - WSS", NULL)) {
1867                                 port = possible_ports[i];
1868                                 break;
1869                         }
1870                 if (port == SNDRV_DEFAULT_PORT1)
1871                         return -EBUSY;
1872         }
1873         else if ((error = snd_register_ioport(card, port, 8,
1874                         DRIVER_NAME" - WSS", NULL)) < 0)
1875                 return error;
1876         chip->chip->wss_base = port;
1877         if ((error = snd_register_interrupt(card, DRIVER_NAME" - WSS",
1878                         irq, SNDRV_IRQ_TYPE_ISA,
1879                         snd_card_opti9xx_interrupt, chip,
1880                         pnp ? no_alternatives : possible_irqs,
1881                         &chip->irqptr)) < 0)
1882                 return error;
1883         chip->chip->irq = chip->irqptr->irq;
1884         if ((error = snd_register_dma_channel(card,
1885 #if defined(CS4231) || defined(OPTi93X)
1886                         DRIVER_NAME" - WSS playback",
1887 #else
1888                         DRIVER_NAME" - WSS",
1889 #endif  /* CS4231 || OPTi93X */
1890                         dma1, SNDRV_DMA_TYPE_ISA, dma1_size,
1891                         pnp ? no_alternatives : possible_dma1s,
1892                         &chip->dma1ptr)) < 0)
1893                 return error;
1894         chip->chip->dma1 = chip->dma1ptr->dma;
1895 #if defined(CS4231) || defined(OPTi93X)
1896         if ((error = snd_register_dma_channel(card, DRIVER_NAME" - WSS capture",
1897                         dma2, SNDRV_DMA_TYPE_ISA, dma2_size,
1898                         pnp ? no_alternatives :
1899                                 possible_dma2s[chip->dma1ptr->dma],
1900                         &chip->dma2ptr)) < 0)
1901                 return error;
1902         chip->chip->dma2 = chip->dma2ptr->dma;
1903 #endif  /* CS4231 || OPTi93X */
1904
1905         if (snd_register_ioport(card,
1906                         pnp ? fm_port : fm_port = 0x388, 4,
1907                         DRIVER_NAME" - OPL", NULL) < 0)
1908                 fm_port = -1;
1909         chip->chip->fm_port = fm_port;
1910
1911         return 0;
1912 }
1913 #endif
1914
1915 static void snd_card_opti9xx_free(snd_card_t *card)
1916 {
1917         opti9xx_t *chip = (opti9xx_t *)card->private_data;
1918         
1919         if (chip) {
1920                 if (chip->res_mc_base) {
1921                         release_resource(chip->res_mc_base);
1922                         kfree_nocheck(chip->res_mc_base);
1923                 }
1924         }
1925 }
1926
1927 static int __devinit snd_card_opti9xx_probe(struct pnp_card_link *pcard,
1928                                             const struct pnp_card_device_id *pid)
1929 {
1930         static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
1931         static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
1932 #ifdef OPTi93X
1933         static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
1934 #else
1935         static int possible_irqs[] = {9, 10, 11, 7, -1};
1936 #endif  /* OPTi93X */
1937         static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
1938         static int possible_dma1s[] = {3, 1, 0, -1};
1939 #if defined(CS4231) || defined(OPTi93X)
1940         static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
1941 #endif  /* CS4231 || OPTi93X */
1942         int error;
1943         opti9xx_t *chip;
1944 #if defined(OPTi93X)
1945         opti93x_t *codec;
1946 #elif defined(CS4231)
1947         cs4231_t *codec;
1948         snd_timer_t *timer;
1949 #else
1950         ad1848_t *codec;
1951 #endif
1952         snd_card_t *card;
1953         snd_pcm_t *pcm;
1954         snd_rawmidi_t *rmidi;
1955         snd_hwdep_t *synth;
1956 #ifdef CONFIG_PNP
1957         int hw;
1958 #endif  /* CONFIG_PNP */
1959
1960         if (pcard && !snd_opti9xx_first_hit)
1961                 return -EBUSY;
1962         if (!(card = snd_card_new(index, id, THIS_MODULE,
1963                                   sizeof(opti9xx_t))))
1964                 return -ENOMEM;
1965         card->private_free = snd_card_opti9xx_free;
1966         chip = (opti9xx_t *)card->private_data;
1967
1968 #ifdef CONFIG_PNP
1969         if (isapnp && pcard && (hw = snd_card_opti9xx_pnp(chip, pcard, pid)) > 0) {
1970                 switch (hw) {
1971                 case 0x0924:
1972                         hw = OPTi9XX_HW_82C924;
1973                         break;
1974                 case 0x0925:
1975                         hw = OPTi9XX_HW_82C925;
1976                         break;
1977                 case 0x0931:
1978                         hw = OPTi9XX_HW_82C931;
1979                         break;
1980                 default:
1981                         snd_card_free(card);
1982                         return -ENODEV;
1983                 }
1984
1985                 if ((error = snd_opti9xx_init(chip, hw))) {
1986                         snd_card_free(card);
1987                         return error;
1988                 }
1989                 if (hw <= OPTi9XX_HW_82C930)
1990                         chip->mc_base -= 0x80;
1991                 snd_card_set_dev(card, &pcard->card->dev);
1992         } else {
1993 #endif  /* CONFIG_PNP */
1994                 if ((error = snd_card_opti9xx_detect(card, chip)) < 0) {
1995                         snd_card_free(card);
1996                         return error;
1997                 }
1998 #ifdef CONFIG_PNP
1999         }
2000 #endif  /* CONFIG_PNP */
2001
2002         if (! chip->res_mc_base &&
2003             (chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL) {
2004                 snd_card_free(card);
2005                 return -ENOMEM;
2006         }
2007
2008         chip->wss_base = port;
2009         chip->fm_port = fm_port;
2010         chip->mpu_port = mpu_port;
2011         chip->irq = irq;
2012         chip->mpu_irq = mpu_irq;
2013         chip->dma1 = dma1;
2014 #if defined(CS4231) || defined(OPTi93X)
2015         chip->dma2 = dma2;
2016 #endif
2017
2018         if (chip->wss_base == SNDRV_AUTO_PORT) {
2019                 if ((chip->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
2020                         snd_card_free(card);
2021                         snd_printk("unable to find a free WSS port\n");
2022                         return -EBUSY;
2023                 }
2024         }
2025 #ifdef CONFIG_PNP
2026         if (!isapnp) {
2027 #endif
2028         if (chip->mpu_port == SNDRV_AUTO_PORT) {
2029                 if ((chip->mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
2030                         snd_card_free(card);
2031                         snd_printk("unable to find a free MPU401 port\n");
2032                         return -EBUSY;
2033                 }
2034         }
2035         if (chip->irq == SNDRV_AUTO_IRQ) {
2036                 if ((chip->irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2037                         snd_card_free(card);
2038                         snd_printk("unable to find a free IRQ\n");
2039                         return -EBUSY;
2040                 }
2041         }
2042         if (chip->mpu_irq == SNDRV_AUTO_IRQ) {
2043                 if ((chip->mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
2044                         snd_card_free(card);
2045                         snd_printk("unable to find a free MPU401 IRQ\n");
2046                         return -EBUSY;
2047                 }
2048         }
2049         if (chip->dma1 == SNDRV_AUTO_DMA) {
2050                 if ((chip->dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
2051                         snd_card_free(card);
2052                         snd_printk("unable to find a free DMA1\n");
2053                         return -EBUSY;
2054                 }
2055         }
2056 #if defined(CS4231) || defined(OPTi93X)
2057         if (chip->dma2 == SNDRV_AUTO_DMA) {
2058                 if ((chip->dma2 = snd_legacy_find_free_dma(possible_dma2s[chip->dma1 % 4])) < 0) {
2059                         snd_card_free(card);
2060                         snd_printk("unable to find a free DMA2\n");
2061                         return -EBUSY;
2062                 }
2063         }
2064 #endif
2065
2066 #ifdef CONFIG_PNP
2067         }
2068 #endif
2069
2070         if ((error = snd_opti9xx_configure(chip))) {
2071                 snd_card_free(card);
2072                 return error;
2073         }
2074
2075 #if defined(OPTi93X)
2076         if ((error = snd_opti93x_create(card, chip, chip->dma1, chip->dma2, &codec))) {
2077                 snd_card_free(card);
2078                 return error;
2079         }
2080         if ((error = snd_opti93x_pcm(codec, 0, &pcm)) < 0) {
2081                 snd_card_free(card);
2082                 return error;
2083         }
2084         if ((error = snd_opti93x_mixer(codec)) < 0) {
2085                 snd_card_free(card);
2086                 return error;
2087         }
2088 #elif defined(CS4231)
2089         if ((error = snd_cs4231_create(card, chip->wss_base + 4, -1,
2090                                        chip->irq, chip->dma1, chip->dma2,
2091                                        CS4231_HW_DETECT,
2092                                        0,
2093                                        &codec)) < 0) {
2094                 snd_card_free(card);
2095                 return error;
2096         }
2097         if ((error = snd_cs4231_pcm(codec, 0, &pcm)) < 0) {
2098                 snd_card_free(card);
2099                 return error;
2100         }
2101         if ((error = snd_cs4231_mixer(codec)) < 0) {
2102                 snd_card_free(card);
2103                 return error;
2104         }
2105         if ((error = snd_cs4231_timer(codec, 0, &timer)) < 0) {
2106                 snd_card_free(card);
2107                 return error;
2108         }
2109 #else
2110         if ((error = snd_ad1848_create(card, chip->wss_base + 4,
2111                                        chip->irq, chip->dma1,
2112                                        AD1848_HW_DETECT, &codec)) < 0) {
2113                 snd_card_free(card);
2114                 return error;
2115         }
2116         if ((error = snd_ad1848_pcm(codec, 0, &pcm)) < 0) {
2117                 snd_card_free(card);
2118                 return error;
2119         }
2120         if ((error = snd_ad1848_mixer(codec)) < 0) {
2121                 snd_card_free(card);
2122                 return error;
2123         }
2124 #endif
2125         strcpy(card->driver, chip->name);
2126         sprintf(card->shortname, "OPTi %s", card->driver);
2127 #if defined(CS4231) || defined(OPTi93X)
2128         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
2129                 card->shortname, pcm->name, chip->wss_base + 4,
2130                 chip->irq, chip->dma1, chip->dma2);
2131 #else
2132         sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
2133                 card->shortname, pcm->name, chip->wss_base + 4,
2134                 chip->irq, chip->dma1);
2135 #endif  /* CS4231 || OPTi93X */
2136
2137         if (chip->mpu_port <= 0 || chip->mpu_port == SNDRV_AUTO_PORT)
2138                 rmidi = NULL;
2139         else
2140                 if ((error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
2141                                 chip->mpu_port, 0, chip->mpu_irq, SA_INTERRUPT,
2142                                 &rmidi)))
2143                         snd_printk("no MPU-401 device at 0x%lx?\n", chip->mpu_port);
2144
2145         if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
2146                 opl3_t *opl3 = NULL;
2147 #ifndef OPTi93X
2148                 if (chip->hardware == OPTi9XX_HW_82C928 ||
2149                     chip->hardware == OPTi9XX_HW_82C929 ||
2150                     chip->hardware == OPTi9XX_HW_82C924) {
2151                         opl4_t *opl4;
2152                         /* assume we have an OPL4 */
2153                         snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
2154                                                0x20, 0x20);
2155                         if (snd_opl4_create(card,
2156                                             chip->fm_port,
2157                                             chip->fm_port - 8,
2158                                             2, &opl3, &opl4) < 0) {
2159                                 /* no luck, use OPL3 instead */
2160                                 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
2161                                                        0x00, 0x20);
2162                         }
2163                 }
2164 #endif  /* !OPTi93X */
2165                 if (!opl3 && snd_opl3_create(card,
2166                                              chip->fm_port,
2167                                              chip->fm_port + 2,
2168                                              OPL3_HW_AUTO, 0, &opl3) < 0) {
2169                         snd_printk("no OPL device at 0x%lx-0x%lx\n",
2170                                    chip->fm_port, chip->fm_port + 4 - 1);
2171                 }
2172                 if (opl3) {
2173                         if ((error = snd_opl3_timer_new(opl3,
2174 #ifdef CS4231
2175                                                         1, 2)) < 0) {
2176 #else
2177                                                         0, 1)) < 0) {
2178 #endif  /* CS4231 */
2179                                 snd_card_free(card);
2180                                 return error;
2181                         }
2182                         if ((error = snd_opl3_hwdep_new(opl3, 0, 1, &synth)) < 0) {
2183                                 snd_card_free(card);
2184                                 return error;
2185                         }
2186                 }
2187         }
2188
2189         if ((error = snd_card_register(card))) {
2190                 snd_card_free(card);
2191                 return error;
2192         }
2193         snd_opti9xx_first_hit = 0;
2194         if (pcard)
2195                 pnp_set_card_drvdata(pcard, card);
2196         else
2197                 snd_opti9xx_legacy = card;
2198         return 0;
2199 }
2200
2201 #ifdef CONFIG_PNP
2202 static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
2203 {
2204         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
2205
2206         snd_card_disconnect(card);
2207         snd_card_free_in_thread(card);
2208         snd_opti9xx_first_hit = 0;
2209 }
2210
2211 static struct pnp_card_driver opti9xx_pnpc_driver = {
2212         .flags          = PNP_DRIVER_RES_DISABLE,
2213         .name           = "opti9xx",
2214         .id_table       = snd_opti9xx_pnpids,
2215         .probe          = snd_card_opti9xx_probe,
2216         .remove         = __devexit_p(snd_opti9xx_pnp_remove),
2217 };
2218 #endif
2219
2220 static int __init alsa_card_opti9xx_init(void)
2221 {
2222         int cards, error;
2223
2224 #ifdef CONFIG_PNP
2225         cards = pnp_register_card_driver(&opti9xx_pnpc_driver);
2226 #else
2227         cards = 0;
2228 #endif
2229         if (cards == 0 && (error = snd_card_opti9xx_probe(NULL, NULL)) < 0) {
2230 #ifdef CONFIG_PNP
2231                 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
2232 #endif
2233 #ifdef MODULE
2234 #ifdef OPTi93X
2235                 printk(KERN_ERR "no OPTi 82C93x soundcard found\n");
2236 #else
2237                 printk(KERN_ERR "no OPTi 82C92x soundcard found\n");
2238 #endif  /* OPTi93X */
2239 #endif
2240                 return error;
2241         }
2242         return 0;
2243 }
2244
2245 static void __exit alsa_card_opti9xx_exit(void)
2246 {
2247 #ifdef CONFIG_PNP
2248         pnp_unregister_card_driver(&opti9xx_pnpc_driver);
2249 #endif
2250         if (snd_opti9xx_legacy)
2251                 snd_card_free(snd_opti9xx_legacy);
2252 }
2253
2254 module_init(alsa_card_opti9xx_init)
2255 module_exit(alsa_card_opti9xx_exit)
2256
2257 #ifndef MODULE
2258
2259 /* format is: snd-opti9xx=enable,index,id,isapnp,
2260                           port,mpu_port,fm_port,
2261                           irq,mpu_irq,
2262                           dma1,[dma2] */
2263
2264 static int __init alsa_card_opti9xx_setup(char *str)
2265 {
2266         int __attribute__ ((__unused__)) enable = 1;
2267         int __attribute__ ((__unused__)) pnp = INT_MAX;
2268
2269         (void)(get_option(&str,&enable) == 2 &&
2270                get_option(&str,&index) == 2 &&
2271                get_id(&str,&id) == 2 &&
2272                get_option(&str,&pnp) == 2 &&
2273                get_option_long(&str,&port) == 2 &&
2274                get_option_long(&str,&mpu_port) == 2 &&
2275                get_option_long(&str,&fm_port) == 2 &&
2276                get_option(&str,&irq) == 2 &&
2277                get_option(&str,&mpu_irq) == 2 &&
2278                get_option(&str,&dma1) == 2
2279 #if defined(CS4231) || defined(OPTi93X)
2280                &&
2281                get_option(&str,&dma2) == 2
2282 #endif
2283                );
2284 #ifdef CONFIG_PNP
2285         if (pnp != INT_MAX)
2286                 isapnp = pnp;
2287 #endif
2288         return 1;
2289 }
2290
2291 #if defined(OPTi93X)
2292 __setup("snd-opti93x=", alsa_card_opti9xx_setup);
2293 #elif defined(CS4231)
2294 __setup("snd-opti92x-cs4231=", alsa_card_opti9xx_setup);
2295 #else
2296 __setup("snd-opti92x-ad1848=", alsa_card_opti9xx_setup);
2297 #endif
2298
2299 #endif /* ifndef MODULE */