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