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