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