File: opti9xx.c

package info (click to toggle)
alsadriver 0.4.1i-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,256 kB
  • ctags: 15,024
  • sloc: ansic: 96,846; makefile: 1,232; sh: 1,051; perl: 54
file content (326 lines) | stat: -rw-r--r-- 8,396 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

/*
    Detection routines for OPTi 82C9xx chips/cards.
    Copyright (C) 1998-99 by Massimo Piccioni <piccio@caronte.csr.unibo.it>
    Copyright (C) 1999 by Lorenzo Piccioni

    Part of this code was developed at the Italian Ministry of Air Defence,
    Sixth Division (oh, che pace ...), Rome.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "../include/driver.h"

#ifndef ESUCCESS
#define ESUCCESS	0
#endif	/* ESUCCESS */

#define opti9xx_printd( args... )	snd_printd( "opti9xx: " ##args )

enum chipsets {
	c928 = 0,	c929 = 1,	c924 = 2,	c925 = 3,
	c930 = 4,	c931 = 5,	c933 = 6,	unsupported = c933
};

static struct {
	char *name;
	unsigned char pwd;
	int mc_base;
	char mc_regs;
	char mc_size;
	int pwd_reg;
} chip_info[] = {
	{ "82C928",		0xe2,	0xf8d,	6,	6,	2 },
	{ "82C929",		0xe3,	0xf8d,	6,	6,	2 },
	{ "82C924",		0xe5,	0xf8d,	12,	9,	2 },
	{ "82C925",		0xe5,	0xf8d,	14,	9,	2 },
	{ "82C930",		0xe4,	0xf8f,	12,	1,	0 },
	{ "82C931",		0xe4,	0xf8d,	26,	1,	0 },
	{ "unsupported",	0x00,	0x000,	0,	0,	0 }
};

#define OPTi9XX_CHIP		chip_info[opti9xx_chip].name
#define OPTi9XX_MC_BASE		chip_info[opti9xx_chip].mc_base
#define OPTi9XX_MC_REGS		chip_info[opti9xx_chip].mc_regs
#define OPTi9XX_MC_SIZE		chip_info[opti9xx_chip].mc_size
#define OPTi9XX_PASSWD		chip_info[opti9xx_chip].pwd

#define OPTi9XX_MC(x)		x
#define OPTi92X_MC_REG(x)	OPTi9XX_MC_BASE + OPTi9XX_MC(x - 1)
#define OPTi9XX_PWD_REG		OPTi9XX_MC_BASE + \
				chip_info[opti9xx_chip].pwd_reg

#define OPTi924_MC_INDEX_IREG	OPTi92X_MC_REG(8)
#define OPTi924_MC_DATA_IREG	OPTi92X_MC_REG(9)
#define OPTi924_MC_DREGS	7

#define OPTi93X_MC_INDEX_IREG	opti93x_mc_index
#define OPTi93X_MC_DATA_IREG	OPTi93X_MC_INDEX_IREG + 1
#define OPTi93X_MC_IREGS	2

#define OPTi93X_ENABLE_PWD	0x00
#define OPTi93X_INDEX_BITS	((OPTi93X_MC_INDEX_IREG & (1 << 8)) >> 4)|\
				((OPTi93X_MC_INDEX_IREG & (0xf0)) >> 4)

snd_card_t *opti9xx_card = NULL;
int opti9xx_chip = unsupported;
int opti93x_mc_index = 0xe0e;		/* 0x[ef][0..f]e are valid values */ 

#ifdef CONFIG_ISAPNP
static unsigned int snd_opti9xx_isapnp_table[] = {
	/* c924 */
	c924,
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_DEVICE(0x0924)),    /* device */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x0000)),  /* audio  */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x0001)),  /* game   */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x0002)),  /* mpu401 */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x0003)),  /* cdrom  */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x0004)),  /* modem  */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x0005)),  /* master */
	0,
	/* c931 */
	c931,
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_DEVICE(0x0931)),    /* device */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x9310)),  /* audio  */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x0001)),  /* game   */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0x0002)),  /* mpu401 */
	(ISAPNP_VENDOR('O','P','T')<<16|ISAPNP_FUNCTION(0xffff)),  /* aux0   */
	0,
	0	/* that's all */
};
#endif	/* CONFIG_ISAPNP */


unsigned char snd_opti9xx_read(unsigned int idx)
{
	unsigned long flags;
	unsigned char retval = 0xff;

	if (idx > OPTi9XX_MC_REGS)
		return retval;

	save_flags(flags);
	cli();

	outb(OPTi9XX_PASSWD, OPTi9XX_PWD_REG);
	switch (opti9xx_chip) {
	case c924:
	case c925:
		if (idx > OPTi924_MC_DREGS) {
			outb(idx, OPTi924_MC_INDEX_IREG);
			outb(OPTi9XX_PASSWD, OPTi9XX_PWD_REG);
			retval = inb(OPTi924_MC_DATA_IREG);
			break;
		}
	case c928:
	case c929:
		retval = inb(OPTi92X_MC_REG(idx));
		break;
	case c930:
	case c931:
		outb(idx, OPTi93X_MC_INDEX_IREG);
		outb(OPTi9XX_PASSWD, OPTi9XX_PWD_REG);
		retval = inb(OPTi93X_MC_DATA_IREG);
		break;
	default:	/* uff */
	}

	restore_flags(flags);
	return retval;
}

void snd_opti9xx_write(unsigned int idx, unsigned char value)
{
	unsigned long flags;

	if (idx > OPTi9XX_MC_REGS)
		return;

	save_flags(flags);
	cli();

	outb(OPTi9XX_PASSWD, OPTi9XX_PWD_REG);
	switch (opti9xx_chip) {
	case c924:
	case c925:
		if (idx > OPTi924_MC_DREGS) {
			outb(idx, OPTi924_MC_INDEX_IREG);
			outb(OPTi9XX_PASSWD, OPTi9XX_PWD_REG);
			outb(value, OPTi924_MC_DATA_IREG);
			break;
		}
	case c928:
	case c929:
		outb(value, OPTi92X_MC_REG(idx));
		break;
	case c930:
	case c931:
		outb(idx, OPTi93X_MC_INDEX_IREG);
		outb(OPTi9XX_PASSWD, OPTi9XX_PWD_REG);
		outb(value, OPTi93X_MC_DATA_IREG);
		break;
	default:	/* uff */
	}

	restore_flags(flags);
}


int snd_opti92x_detect_isa(void)
{
	for (opti9xx_chip = c928; opti9xx_chip < c930; opti9xx_chip++) {
		unsigned char value, check;

		if (snd_register_ioport(opti9xx_card,
			OPTi9XX_MC_BASE, OPTi9XX_MC_SIZE,
			"opti92x - mc regs", NULL))
			continue;

		value = snd_opti9xx_read(OPTi9XX_MC(1));
		check = inb(OPTi9XX_MC_BASE + OPTi9XX_MC(1));
		if ((value != 0xff) && (value != check))
			if (value == snd_opti9xx_read(OPTi9XX_MC(1)))
				return ESUCCESS;

		snd_unregister_ioports(opti9xx_card);
	}

	opti9xx_chip = unsupported;
	return -ENODEV;
}

int snd_opti93x_detect_isa(void)
{
	int error = -ENODEV;
	int hb, lb;

	for (hb = 0x00; (hb < 0x02) && error; hb++)
		for (lb = 0x00; (lb < 0x10) && error; lb++) {
			opti93x_mc_index = 0xe0e + (hb << 8) + (lb << 4);

			error = snd_register_ioport(opti9xx_card,
				OPTi93X_MC_INDEX_IREG, OPTi93X_MC_IREGS,
				"opti93x - mc regs", NULL);
		}

	if (error)
		return error;

	/* the 82C931 chip may appear as an 82C930 */
	for (opti9xx_chip = c931; opti9xx_chip >= c930; opti9xx_chip--) {
		unsigned char value, check;
		int i;

		if (snd_register_ioport(opti9xx_card,
			OPTi9XX_MC_BASE, OPTi9XX_MC_SIZE,
			"opti93x - mc regs", NULL))
			continue;

		outb(OPTi9XX_PASSWD, OPTi9XX_PWD_REG);
		outb(OPTi93X_ENABLE_PWD | OPTi93X_INDEX_BITS,
			OPTi9XX_MC_BASE);

		value = snd_opti9xx_read(OPTi9XX_MC(7));
		check = ~value;
		snd_opti9xx_write(OPTi9XX_MC(7), check);

		if (snd_opti9xx_read(OPTi9XX_MC(7)) == check) {
			for (i = 1; i <= OPTi9XX_MC_REGS; i++) {
				value = snd_opti9xx_read(OPTi9XX_MC(i));
				check = snd_opti9xx_read(OPTi9XX_MC(i +
					OPTi9XX_MC_REGS));

				if ((value != 0xff) && (check != 0xff))
					break;
			}

			if (i > OPTi9XX_MC_REGS)
				return ESUCCESS;
		}

		snd_unregister_ioports(opti9xx_card);
		if ((error = snd_register_ioport(opti9xx_card,
			OPTi93X_MC_INDEX_IREG, OPTi93X_MC_IREGS,
			"opti93x - mc regs", NULL)))
			return error;
	}

	opti9xx_chip = unsupported;
	return -ENODEV;
}

int snd_opti9xx_detect_isa(void)
{
	if (snd_opti92x_detect_isa() && snd_opti93x_detect_isa()) {
		opti9xx_printd("cannot find any OPTi based ISA soundcard\n");
		return -ENODEV;
	}

	opti9xx_printd("found an OPTi %s chip (ISA)\n", OPTi9XX_CHIP);
	return ESUCCESS;
}

#ifdef CONFIG_ISAPNP
int snd_opti9xx_detect_pnp(void)
{
	int i;
	unsigned int tmp;

	for (i = 0; (opti9xx_chip = tmp = snd_opti9xx_isapnp_table[i++]);) {
		tmp = snd_opti9xx_isapnp_table[i++];

		if ((isapnp_find_card(tmp >> 16, tmp & 0xffff, NULL))) {
			opti9xx_printd("found an OPTi %s chip (PnP)\n",
				OPTi9XX_CHIP);
			return ESUCCESS;
		}

		while (snd_opti9xx_isapnp_table[i++]);
	}

	opti9xx_printd("cannot find any OPTi based PnP soundcard\n");
	return -ENODEV;
}
#endif	/* CONFIG_ISAPNP */

int snd_detect_opti9xx(snd_card_t *card, char *rname,
	unsigned int *rtype, unsigned short *rport)
{
	static int found = 0;

	if (found) {
		found = 0;
		snd_unregister_ioports(card);
		return -EBUSY;
	}

	opti9xx_card = card;

#ifdef CONFIG_ISAPNP
	if (snd_opti9xx_detect_pnp())
#endif	/* CONFIG_ISAPNP */
		if (snd_opti9xx_detect_isa()) {
			snd_unregister_ioports(opti9xx_card);
			return -ENODEV;
		}

	sprintf(rname, "OPTi %s based soundcard", OPTi9XX_CHIP);
	*rport = OPTi9XX_MC_BASE;
	*rtype = SND_CARD_TYPE_OPTI9XX;

	found++;
	return ESUCCESS;
}