1#undef FKS_LOGGING
2#undef FKS_TEST
3
4/*
5 * tabs should be 4 spaces, in vi(m): set tabstop=4
6 *
7 * TODO: 	consistency speed calculations!!
8 *			cleanup!
9 * ????:	Did I break MIDI support?
10 *
11 * History:
12 *
13 * Rolf Fokkens	 (Dec 20 1998):	ES188x recording level support on a per
14 * fokkensr@vertis.nl			input basis.
15 *				 (Dec 24 1998):	Recognition of ES1788, ES1887, ES1888,
16 *								ES1868, ES1869 and ES1878. Could be used for
17 *								specific handling in the future. All except
18 *								ES1887 and ES1888 and ES688 are handled like
19 *								ES1688.
20 *				 (Dec 27 1998):	RECLEV for all (?) ES1688+ chips. ES188x now
21 *								have the "Dec 20" support + RECLEV
22 *				 (Jan  2 1999):	Preparation for Full Duplex. This means
23 *								Audio 2 is now used for playback when dma16
24 *								is specified. The next step would be to use
25 *								Audio 1 and Audio 2 at the same time.
26 *				 (Jan  9 1999):	Put all ESS stuff into sb_ess.[ch], this
27 *								includes both the ESS stuff that has been in
28 *								sb_*[ch] before I touched it and the ESS support
29 *								I added later
30 *				 (Jan 23 1999):	Full Duplex seems to work. I wrote a small
31 *								test proggy which works OK. Haven't found
32 *								any applications to test it though. So why did
33 *								I bother to create it anyway?? :) Just for
34 *								fun.
35 *				 (May  2 1999):	I tried to be too smart by "introducing"
36 *								ess_calc_best_speed (). The idea was that two
37 *								dividers could be used to setup a samplerate,
38 *								ess_calc_best_speed () would choose the best.
39 *								This works for playback, but results in
40 *								recording problems for high samplerates. I
41 *								fixed this by removing ess_calc_best_speed ()
42 *								and just doing what the documentation says.
43 * Andy Sloane   (Jun  4 1999): Stole some code from ALSA to fix the playback
44 * andy@guildsoftware.com		speed on ES1869, ES1879, ES1887, and ES1888.
45 * 								1879's were previously ignored by this driver;
46 * 								added (untested) support for those.
47 * Cvetan Ivanov (Oct 27 1999): Fixed ess_dsp_init to call ess_set_dma_hw for
48 * zezo@inet.bg					_ALL_ ESS models, not only ES1887
49 *
50 * This files contains ESS chip specifics. It's based on the existing ESS
51 * handling as it resided in sb_common.c, sb_mixer.c and sb_audio.c. This
52 * file adds features like:
53 * - Chip Identification (as shown in /proc/sound)
54 * - RECLEV support for ES1688 and later
55 * - 6 bits playback level support chips later than ES1688
56 * - Recording level support on a per-device basis for ES1887
57 * - Full-Duplex for ES1887
58 *
59 * Full duplex is enabled by specifying dma16. While the normal dma must
60 * be one of 0, 1 or 3, dma16 can be one of 0, 1, 3 or 5. DMA 5 is a 16 bit
61 * DMA channel, while the others are 8 bit..
62 *
63 * ESS detection isn't full proof (yet). If it fails an additional module
64 * parameter esstype can be specified to be one of the following:
65 * -1, 0, 688, 1688, 1868, 1869, 1788, 1887, 1888
66 * -1 means: mimic 2.0 behaviour,
67 *  0 means: auto detect.
68 *   others: explicitly specify chip
69 * -1 is default, cause auto detect still doesn't work.
70 */
71
72/*
73 * About the documentation
74 *
75 * I don't know if the chips all are OK, but the documentation is buggy. 'cause
76 * I don't have all the cips myself, there's a lot I cannot verify. I'll try to
77 * keep track of my latest insights about his here. If you have additional info,
78 * please enlighten me (fokkensr@vertis.nl)!
79 *
80 * I had the impression that ES1688 also has 6 bit master volume control. The
81 * documentation about ES1888 (rev C, october '95) claims that ES1888 has
82 * the following features ES1688 doesn't have:
83 * - 6 bit master volume
84 * - Full Duplex
85 * So ES1688 apparently doesn't have 6 bit master volume control, but the
86 * ES1688 does have RECLEV control. Makes me wonder: does ES688 have it too?
87 * Without RECLEV ES688 won't be much fun I guess.
88 *
89 * From the ES1888 (rev C, october '95) documentation I got the impression
90 * that registers 0x68 to 0x6e don't exist which means: no recording volume
91 * controls. To my surprise the ES888 documentation (1/14/96) claims that
92 * ES888 does have these record mixer registers, but that ES1888 doesn't have
93 * 0x69 and 0x6b. So the rest should be there.
94 *
95 * I'm trying to get ES1887 Full Duplex. Audio 2 is playback only, while Audio 2
96 * is both record and playback. I think I should use Audio 2 for all playback.
97 *
98 * The documentation is an adventure: it's close but not fully accurate. I
99 * found out that after a reset some registers are *NOT* reset, though the
100 * docs say the would be. Interresting ones are 0x7f, 0x7d and 0x7a. They are
101 * related to the Audio 2 channel. I also was suprised about the consequenses
102 * of writing 0x00 to 0x7f (which should be done by reset): The ES1887 moves
103 * into ES1888 mode. This means that it claims IRQ 11, which happens to be my
104 * ISDN adapter. Needless to say it no longer worked. I now understand why
105 * after rebooting 0x7f already was 0x05, the value of my choice: the BIOS
106 * did it.
107 *
108 * Oh, and this is another trap: in ES1887 docs mixer register 0x70 is decribed
109 * as if it's exactly the same as register 0xa1. This is *NOT* true. The
110 * description of 0x70 in ES1869 docs is accurate however.
111 * Well, the assumption about ES1869 was wrong: register 0x70 is very much
112 * like register 0xa1, except that bit 7 is allways 1, whatever you want
113 * it to be.
114 *
115 * When using audio 2 mixer register 0x72 seems te be meaningless. Only 0xa2
116 * has effect.
117 *
118 * Software reset not being able to reset all registers is great! Especially
119 * the fact that register 0x78 isn't reset is great when you wanna change back
120 * to single dma operation (simplex): audio 2 is still operation, and uses the
121 * same dma as audio 1: your ess changes into a funny echo machine.
122 *
123 * Received the new that ES1688 is detected as a ES1788. Did some thinking:
124 * the ES1887 detection scheme suggests in step 2 to try if bit 3 of register
125 * 0x64 can be changed. This is inaccurate, first I inverted the * check: "If
126 * can be modified, it's a 1688", which lead to a correct detection
127 * of my ES1887. It resulted however in bad detection of 1688 (reported by mail)
128 * and 1868 (if no PnP detection first): they result in a 1788 being detected.
129 * I don't have docs on 1688, but I do have docs on 1868: The documentation is
130 * probably inaccurate in the fact that I should check bit 2, not bit 3. This
131 * is what I do now.
132 */
133
134/*
135 * About recognition of ESS chips
136 *
137 * The distinction of ES688, ES1688, ES1788, ES1887 and ES1888 is described in
138 * a (preliminary ??) datasheet on ES1887. It's aim is to identify ES1887, but
139 * during detection the text claims that "this chip may be ..." when a step
140 * fails. This scheme is used to distinct between the above chips.
141 * It appears however that some PnP chips like ES1868 are recognized as ES1788
142 * by the ES1887 detection scheme. These PnP chips can be detected in another
143 * way however: ES1868, ES1869 and ES1878 can be recognized (full proof I think)
144 * by repeatedly reading mixer register 0x40. This is done by ess_identify in
145 * sb_common.c.
146 * This results in the following detection steps:
147 * - distinct between ES688 and ES1688+ (as always done in this driver)
148 *   if ES688 we're ready
149 * - try to detect ES1868, ES1869 or ES1878
150 *   if successful we're ready
151 * - try to detect ES1888, ES1887 or ES1788
152 *   if successful we're ready
153 * - Dunno. Must be 1688. Will do in general
154 *
155 * About RECLEV support:
156 *
157 * The existing ES1688 support didn't take care of the ES1688+ recording
158 * levels very well. Whenever a device was selected (recmask) for recording
159 * it's recording level was loud, and it couldn't be changed. The fact that
160 * internal register 0xb4 could take care of RECLEV, didn't work meaning until
161 * it's value was restored every time the chip was reset; this reset the
162 * value of 0xb4 too. I guess that's what 4front also had (have?) trouble with.
163 *
164 * About ES1887 support:
165 *
166 * The ES1887 has separate registers to control the recording levels, for all
167 * inputs. The ES1887 specific software makes these levels the same as their
168 * corresponding playback levels, unless recmask says they aren't recorded. In
169 * the latter case the recording volumes are 0.
170 * Now recording levels of inputs can be controlled, by changing the playback
171 * levels. Futhermore several devices can be recorded together (which is not
172 * possible with the ES1688.
173 * Besides the separate recording level control for each input, the common
174 * recordig level can also be controlled by RECLEV as described above.
175 *
176 * Not only ES1887 have this recording mixer. I know the following from the
177 * documentation:
178 * ES688	no
179 * ES1688	no
180 * ES1868	no
181 * ES1869	yes
182 * ES1878	no
183 * ES1879	yes
184 * ES1888	no/yes	Contradicting documentation; most recent: yes
185 * ES1946	yes		This is a PCI chip; not handled by this driver
186 */
187
188#include <linux/delay.h>
189#include <linux/spinlock.h>
190
191#include "sound_config.h"
192#include "sb_mixer.h"
193#include "sb.h"
194
195#include "sb_ess.h"
196
197#define ESSTYPE_LIKE20	-1		/* Mimic 2.0 behaviour					*/
198#define ESSTYPE_DETECT	0		/* Mimic 2.0 behaviour					*/
199
200#define SUBMDL_ES1788	0x10	/* Subtype ES1788 for specific handling */
201#define SUBMDL_ES1868	0x11	/* Subtype ES1868 for specific handling */
202#define SUBMDL_ES1869	0x12	/* Subtype ES1869 for specific handling */
203#define SUBMDL_ES1878	0x13	/* Subtype ES1878 for specific handling */
204#define SUBMDL_ES1879	0x16    /* ES1879 was initially forgotten */
205#define SUBMDL_ES1887	0x14	/* Subtype ES1887 for specific handling */
206#define SUBMDL_ES1888	0x15	/* Subtype ES1888 for specific handling */
207
208#define SB_CAP_ES18XX_RATE 0x100
209
210#define ES1688_CLOCK1 795444 /* 128 - div */
211#define ES1688_CLOCK2 397722 /* 256 - div */
212#define ES18XX_CLOCK1 793800 /* 128 - div */
213#define ES18XX_CLOCK2 768000 /* 256 - div */
214
215#ifdef FKS_LOGGING
216static void ess_show_mixerregs (sb_devc *devc);
217#endif
218static int ess_read (sb_devc * devc, unsigned char reg);
219static int ess_write (sb_devc * devc, unsigned char reg, unsigned char data);
220static void ess_chgmixer
221	(sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val);
222
223/****************************************************************************
224 *																			*
225 *									ESS audio								*
226 *																			*
227 ****************************************************************************/
228
229struct ess_command {short cmd; short data;};
230
231/*
232 * Commands for initializing Audio 1 for input (record)
233 */
234static struct ess_command ess_i08m[] =		/* input 8 bit mono */
235	{ {0xb7, 0x51}, {0xb7, 0xd0}, {-1, 0} };
236static struct ess_command ess_i16m[] =		/* input 16 bit mono */
237	{ {0xb7, 0x71}, {0xb7, 0xf4}, {-1, 0} };
238static struct ess_command ess_i08s[] =		/* input 8 bit stereo */
239	{ {0xb7, 0x51}, {0xb7, 0x98}, {-1, 0} };
240static struct ess_command ess_i16s[] =		/* input 16 bit stereo */
241	{ {0xb7, 0x71}, {0xb7, 0xbc}, {-1, 0} };
242
243static struct ess_command *ess_inp_cmds[] =
244	{ ess_i08m, ess_i16m, ess_i08s, ess_i16s };
245
246
247/*
248 * Commands for initializing Audio 1 for output (playback)
249 */
250static struct ess_command ess_o08m[] =		/* output 8 bit mono */
251	{ {0xb6, 0x80}, {0xb7, 0x51}, {0xb7, 0xd0}, {-1, 0} };
252static struct ess_command ess_o16m[] =		/* output 16 bit mono */
253	{ {0xb6, 0x00}, {0xb7, 0x71}, {0xb7, 0xf4}, {-1, 0} };
254static struct ess_command ess_o08s[] =		/* output 8 bit stereo */
255	{ {0xb6, 0x80}, {0xb7, 0x51}, {0xb7, 0x98}, {-1, 0} };
256static struct ess_command ess_o16s[] =		/* output 16 bit stereo */
257	{ {0xb6, 0x00}, {0xb7, 0x71}, {0xb7, 0xbc}, {-1, 0} };
258
259static struct ess_command *ess_out_cmds[] =
260	{ ess_o08m, ess_o16m, ess_o08s, ess_o16s };
261
262static void ess_exec_commands
263	(sb_devc *devc, struct ess_command *cmdtab[])
264{
265	struct ess_command *cmd;
266
267	cmd = cmdtab [ ((devc->channels != 1) << 1) + (devc->bits != AFMT_U8) ];
268
269	while (cmd->cmd != -1) {
270		ess_write (devc, cmd->cmd, cmd->data);
271		cmd++;
272	}
273}
274
275static void ess_change
276	(sb_devc *devc, unsigned int reg, unsigned int mask, unsigned int val)
277{
278	int value;
279
280	value = ess_read (devc, reg);
281	value = (value & ~mask) | (val & mask);
282	ess_write (devc, reg, value);
283}
284
285static void ess_set_output_parms
286	(int dev, unsigned long buf, int nr_bytes, int intrflag)
287{
288	sb_devc *devc = audio_devs[dev]->devc;
289
290	if (devc->duplex) {
291		devc->trg_buf_16 = buf;
292		devc->trg_bytes_16 = nr_bytes;
293		devc->trg_intrflag_16 = intrflag;
294		devc->irq_mode_16 = IMODE_OUTPUT;
295	} else {
296		devc->trg_buf = buf;
297		devc->trg_bytes = nr_bytes;
298		devc->trg_intrflag = intrflag;
299		devc->irq_mode = IMODE_OUTPUT;
300	}
301}
302
303static void ess_set_input_parms
304	(int dev, unsigned long buf, int count, int intrflag)
305{
306	sb_devc *devc = audio_devs[dev]->devc;
307
308	devc->trg_buf = buf;
309	devc->trg_bytes = count;
310	devc->trg_intrflag = intrflag;
311	devc->irq_mode = IMODE_INPUT;
312}
313
314static int ess_calc_div (int clock, int revert, int *speedp, int *diffp)
315{
316	int divider;
317	int speed, diff;
318	int retval;
319
320	speed   = *speedp;
321	divider = (clock + speed / 2) / speed;
322	retval  = revert - divider;
323	if (retval > revert - 1) {
324		retval  = revert - 1;
325		divider = revert - retval;
326	}
327	/* This line is suggested. Must be wrong I think
328	*speedp = (clock + divider / 2) / divider;
329	So I chose the next one */
330
331	*speedp	= clock / divider;
332	diff	= speed - *speedp;
333	if (diff < 0) diff =-diff;
334	*diffp  = diff;
335
336	return retval;
337}
338
339static int ess_calc_best_speed
340	(int clock1, int rev1, int clock2, int rev2, int *divp, int *speedp)
341{
342	int speed1 = *speedp, speed2 = *speedp;
343	int div1, div2;
344	int diff1, diff2;
345	int retval;
346
347	div1 = ess_calc_div (clock1, rev1, &speed1, &diff1);
348	div2 = ess_calc_div (clock2, rev2, &speed2, &diff2);
349
350	if (diff1 < diff2) {
351		*divp   = div1;
352		*speedp = speed1;
353		retval  = 1;
354	} else {
355	/*	*divp   = div2; */
356		*divp   = 0x80 | div2;
357		*speedp = speed2;
358		retval  = 2;
359	}
360
361	return retval;
362}
363
364/*
365 * Depending on the audiochannel ESS devices can
366 * have different clock settings. These are made consistent for duplex
367 * however.
368 * callers of ess_speed only do an audionum suggestion, which means
369 * input suggests 1, output suggests 2. This suggestion is only true
370 * however when doing duplex.
371 */
372static void ess_common_speed (sb_devc *devc, int *speedp, int *divp)
373{
374	int diff = 0, div;
375
376	if (devc->duplex) {
377		/*
378		 * The 0x80 is important for the first audio channel
379		 */
380		if (devc->submodel == SUBMDL_ES1888) {
381			div = 0x80 | ess_calc_div (795500, 256, speedp, &diff);
382		} else {
383			div = 0x80 | ess_calc_div (795500, 128, speedp, &diff);
384		}
385	} else if(devc->caps & SB_CAP_ES18XX_RATE) {
386		if (devc->submodel == SUBMDL_ES1888) {
387			ess_calc_best_speed(397700, 128, 795500, 256,
388						&div, speedp);
389		} else {
390			ess_calc_best_speed(ES18XX_CLOCK1, 128, ES18XX_CLOCK2, 256,
391						&div, speedp);
392		}
393	} else {
394		if (*speedp > 22000) {
395			div = 0x80 | ess_calc_div (ES1688_CLOCK1, 256, speedp, &diff);
396		} else {
397			div = 0x00 | ess_calc_div (ES1688_CLOCK2, 128, speedp, &diff);
398		}
399	}
400	*divp = div;
401}
402
403static void ess_speed (sb_devc *devc, int audionum)
404{
405	int speed;
406	int div, div2;
407
408	ess_common_speed (devc, &(devc->speed), &div);
409
410#ifdef FKS_REG_LOGGING
411printk (KERN_INFO "FKS: ess_speed (%d) b speed = %d, div=%x\n", audionum, devc->speed, div);
412#endif
413
414	/* Set filter roll-off to 90% of speed/2 */
415	speed = (devc->speed * 9) / 20;
416
417	div2 = 256 - 7160000 / (speed * 82);
418
419	if (!devc->duplex) audionum = 1;
420
421	if (audionum == 1) {
422		/* Change behaviour of register A1 *
423		sb_chg_mixer(devc, 0x71, 0x20, 0x20)
424		* For ES1869 only??? */
425		ess_write (devc, 0xa1, div);
426		ess_write (devc, 0xa2, div2);
427	} else {
428		ess_setmixer (devc, 0x70, div);
429		/*
430		 * FKS: fascinating: 0x72 doesn't seem to work.
431		 */
432		ess_write (devc, 0xa2, div2);
433		ess_setmixer (devc, 0x72, div2);
434	}
435}
436
437static int ess_audio_prepare_for_input(int dev, int bsize, int bcount)
438{
439	sb_devc *devc = audio_devs[dev]->devc;
440
441	ess_speed(devc, 1);
442
443	sb_dsp_command(devc, DSP_CMD_SPKOFF);
444
445	ess_write (devc, 0xb8, 0x0e);	/* Auto init DMA mode */
446	ess_change (devc, 0xa8, 0x03, 3 - devc->channels);	/* Mono/stereo */
447	ess_write (devc, 0xb9, 2);	/* Demand mode (4 bytes/DMA request) */
448
449	ess_exec_commands (devc, ess_inp_cmds);
450
451	ess_change (devc, 0xb1, 0xf0, 0x50);
452	ess_change (devc, 0xb2, 0xf0, 0x50);
453
454	devc->trigger_bits = 0;
455	return 0;
456}
457
458static int ess_audio_prepare_for_output_audio1 (int dev, int bsize, int bcount)
459{
460	sb_devc *devc = audio_devs[dev]->devc;
461
462	sb_dsp_reset(devc);
463	ess_speed(devc, 1);
464	ess_write (devc, 0xb8, 4);	/* Auto init DMA mode */
465	ess_change (devc, 0xa8, 0x03, 3 - devc->channels);	/* Mono/stereo */
466	ess_write (devc, 0xb9, 2);	/* Demand mode (4 bytes/request) */
467
468	ess_exec_commands (devc, ess_out_cmds);
469
470	ess_change (devc, 0xb1, 0xf0, 0x50);	/* Enable DMA */
471	ess_change (devc, 0xb2, 0xf0, 0x50);	/* Enable IRQ */
472
473	sb_dsp_command(devc, DSP_CMD_SPKON);	/* There be sound! */
474
475	devc->trigger_bits = 0;
476	return 0;
477}
478
479static int ess_audio_prepare_for_output_audio2 (int dev, int bsize, int bcount)
480{
481	sb_devc *devc = audio_devs[dev]->devc;
482	unsigned char bits;
483
484/* FKS: qqq
485	sb_dsp_reset(devc);
486*/
487
488	/*
489	 * Auto-Initialize:
490	 * DMA mode + demand mode (8 bytes/request, yes I want it all!)
491	 * But leave 16-bit DMA bit untouched!
492	 */
493	ess_chgmixer (devc, 0x78, 0xd0, 0xd0);
494
495	ess_speed(devc, 2);
496
497	/* bits 4:3 on ES1887 represent recording source. Keep them! */
498	bits = ess_getmixer (devc, 0x7a) & 0x18;
499
500	/* Set stereo/mono */
501	if (devc->channels != 1) bits |= 0x02;
502
503	/* Init DACs; UNSIGNED mode for 8 bit; SIGNED mode for 16 bit */
504	if (devc->bits != AFMT_U8) bits |= 0x05;	/* 16 bit */
505
506	/* Enable DMA, IRQ will be shared (hopefully)*/
507	bits |= 0x60;
508
509	ess_setmixer (devc, 0x7a, bits);
510
511	ess_mixer_reload (devc, SOUND_MIXER_PCM);	/* There be sound! */
512
513	devc->trigger_bits = 0;
514	return 0;
515}
516
517static int ess_audio_prepare_for_output(int dev, int bsize, int bcount)
518{
519	sb_devc *devc = audio_devs[dev]->devc;
520
521#ifdef FKS_REG_LOGGING
522printk(KERN_INFO "ess_audio_prepare_for_output: dma_out=%d,dma_in=%d\n"
523, audio_devs[dev]->dmap_out->dma, audio_devs[dev]->dmap_in->dma);
524#endif
525
526	if (devc->duplex) {
527		return ess_audio_prepare_for_output_audio2 (dev, bsize, bcount);
528	} else {
529		return ess_audio_prepare_for_output_audio1 (dev, bsize, bcount);
530	}
531}
532
533static void ess_audio_halt_xfer(int dev)
534{
535	unsigned long flags;
536	sb_devc *devc = audio_devs[dev]->devc;
537
538	spin_lock_irqsave(&devc->lock, flags);
539	sb_dsp_reset(devc);
540	spin_unlock_irqrestore(&devc->lock, flags);
541
542	/*
543	 * Audio 2 may still be operational! Creates awful sounds!
544	 */
545	if (devc->duplex) ess_chgmixer(devc, 0x78, 0x03, 0x00);
546}
547
548static void ess_audio_start_input
549	(int dev, unsigned long buf, int nr_bytes, int intrflag)
550{
551	int count = nr_bytes;
552	sb_devc *devc = audio_devs[dev]->devc;
553	short c = -nr_bytes;
554
555	/*
556	 * Start a DMA input to the buffer pointed by dmaqtail
557	 */
558
559	if (audio_devs[dev]->dmap_in->dma > 3) count >>= 1;
560	count--;
561
562	devc->irq_mode = IMODE_INPUT;
563
564	ess_write (devc, 0xa4, (unsigned char) ((unsigned short) c & 0xff));
565	ess_write (devc, 0xa5, (unsigned char) (((unsigned short) c >> 8) & 0xff));
566
567	ess_change (devc, 0xb8, 0x0f, 0x0f);	/* Go */
568	devc->intr_active = 1;
569}
570
571static void ess_audio_output_block_audio1
572	(int dev, unsigned long buf, int nr_bytes, int intrflag)
573{
574	int count = nr_bytes;
575	sb_devc *devc = audio_devs[dev]->devc;
576	short c = -nr_bytes;
577
578	if (audio_devs[dev]->dmap_out->dma > 3)
579		count >>= 1;
580	count--;
581
582	devc->irq_mode = IMODE_OUTPUT;
583
584	ess_write (devc, 0xa4, (unsigned char) ((unsigned short) c & 0xff));
585	ess_write (devc, 0xa5, (unsigned char) (((unsigned short) c >> 8) & 0xff));
586
587	ess_change (devc, 0xb8, 0x05, 0x05);	/* Go */
588	devc->intr_active = 1;
589}
590
591static void ess_audio_output_block_audio2
592	(int dev, unsigned long buf, int nr_bytes, int intrflag)
593{
594	int count = nr_bytes;
595	sb_devc *devc = audio_devs[dev]->devc;
596	short c = -nr_bytes;
597
598	if (audio_devs[dev]->dmap_out->dma > 3) count >>= 1;
599	count--;
600
601	ess_setmixer (devc, 0x74, (unsigned char) ((unsigned short) c & 0xff));
602	ess_setmixer (devc, 0x76, (unsigned char) (((unsigned short) c >> 8) & 0xff));
603	ess_chgmixer (devc, 0x78, 0x03, 0x03);   /* Go */
604
605	devc->irq_mode_16 = IMODE_OUTPUT;
606		devc->intr_active_16 = 1;
607}
608
609static void ess_audio_output_block
610	(int dev, unsigned long buf, int nr_bytes, int intrflag)
611{
612	sb_devc *devc = audio_devs[dev]->devc;
613
614	if (devc->duplex) {
615		ess_audio_output_block_audio2 (dev, buf, nr_bytes, intrflag);
616	} else {
617		ess_audio_output_block_audio1 (dev, buf, nr_bytes, intrflag);
618	}
619}
620
621/*
622 * FKS: the if-statements for both bits and bits_16 are quite alike.
623 * Combine this...
624 */
625static void ess_audio_trigger(int dev, int bits)
626{
627	sb_devc *devc = audio_devs[dev]->devc;
628
629	int bits_16 = bits & devc->irq_mode_16;
630	bits &= devc->irq_mode;
631
632	if (!bits && !bits_16) {
633		/* FKS oh oh.... wrong?? for dma 16? */
634		sb_dsp_command(devc, 0xd0);	/* Halt DMA */
635	}
636
637	if (bits) {
638		switch (devc->irq_mode)
639		{
640			case IMODE_INPUT:
641				ess_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
642					devc->trg_intrflag);
643				break;
644
645			case IMODE_OUTPUT:
646				ess_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
647					devc->trg_intrflag);
648				break;
649		}
650	}
651
652	if (bits_16) {
653		switch (devc->irq_mode_16) {
654		case IMODE_INPUT:
655			ess_audio_start_input(dev, devc->trg_buf_16, devc->trg_bytes_16,
656					devc->trg_intrflag_16);
657			break;
658
659		case IMODE_OUTPUT:
660			ess_audio_output_block(dev, devc->trg_buf_16, devc->trg_bytes_16,
661					devc->trg_intrflag_16);
662			break;
663		}
664	}
665
666	devc->trigger_bits = bits | bits_16;
667}
668
669static int ess_audio_set_speed(int dev, int speed)
670{
671	sb_devc *devc = audio_devs[dev]->devc;
672	int minspeed, maxspeed, dummydiv;
673
674	if (speed > 0) {
675		minspeed = (devc->duplex ? 6215  : 5000 );
676		maxspeed = (devc->duplex ? 44100 : 48000);
677		if (speed < minspeed) speed = minspeed;
678		if (speed > maxspeed) speed = maxspeed;
679
680		ess_common_speed (devc, &speed, &dummydiv);
681
682		devc->speed = speed;
683	}
684	return devc->speed;
685}
686
687/*
688 * FKS: This is a one-on-one copy of sb1_audio_set_bits
689 */
690static unsigned int ess_audio_set_bits(int dev, unsigned int bits)
691{
692	sb_devc *devc = audio_devs[dev]->devc;
693
694	if (bits != 0) {
695		if (bits == AFMT_U8 || bits == AFMT_S16_LE) {
696			devc->bits = bits;
697		} else {
698			devc->bits = AFMT_U8;
699		}
700	}
701
702	return devc->bits;
703}
704
705/*
706 * FKS: This is a one-on-one copy of sbpro_audio_set_channels
707 * (*) Modified it!!
708 */
709static short ess_audio_set_channels(int dev, short channels)
710{
711	sb_devc *devc = audio_devs[dev]->devc;
712
713	if (channels == 1 || channels == 2) devc->channels = channels;
714
715	return devc->channels;
716}
717
718static struct audio_driver ess_audio_driver =   /* ESS ES688/1688 */
719{
720	owner:			THIS_MODULE,
721	open:			sb_audio_open,
722	close:			sb_audio_close,
723	output_block:	ess_set_output_parms,
724	start_input:	ess_set_input_parms,
725	prepare_for_input:	ess_audio_prepare_for_input,
726	prepare_for_output:	ess_audio_prepare_for_output,
727	halt_io:		ess_audio_halt_xfer,
728	trigger:		ess_audio_trigger,
729	set_speed:		ess_audio_set_speed,
730	set_bits:		ess_audio_set_bits,
731	set_channels:	ess_audio_set_channels
732};
733
734/*
735 * ess_audio_init must be called from sb_audio_init
736 */
737struct audio_driver *ess_audio_init
738		(sb_devc *devc, int *audio_flags, int *format_mask)
739{
740	*audio_flags = DMA_AUTOMODE;
741	*format_mask |= AFMT_S16_LE;
742
743	if (devc->duplex) {
744		int tmp_dma;
745		/*
746		 * sb_audio_init thinks dma8 is for playback and
747		 * dma16 is for record. Not now! So swap them.
748		 */
749		tmp_dma		= devc->dma16;
750		devc->dma16	= devc->dma8;
751		devc->dma8	= tmp_dma;
752
753		*audio_flags |= DMA_DUPLEX;
754	}
755
756	return &ess_audio_driver;
757}
758
759/****************************************************************************
760 *																			*
761 *								ESS common									*
762 *																			*
763 ****************************************************************************/
764static void ess_handle_channel
765	(char *channel, int dev, int intr_active, unsigned char flag, int irq_mode)
766{
767	if (!intr_active || !flag) return;
768#ifdef FKS_REG_LOGGING
769printk(KERN_INFO "FKS: ess_handle_channel %s irq_mode=%d\n", channel, irq_mode);
770#endif
771	switch (irq_mode) {
772		case IMODE_OUTPUT:
773			DMAbuf_outputintr (dev, 1);
774			break;
775
776		case IMODE_INPUT:
777			DMAbuf_inputintr (dev);
778			break;
779
780		case IMODE_INIT:
781			break;
782
783		default:;
784			/* printk(KERN_WARN "ESS: Unexpected interrupt\n"); */
785	}
786}
787
788/*
789 * FKS: TODO!!! Finish this!
790 *
791 * I think midi stuff uses uart401, without interrupts.
792 * So IMODE_MIDI isn't a value for devc->irq_mode.
793 */
794void ess_intr (sb_devc *devc)
795{
796	int				status;
797	unsigned char	src;
798
799	if (devc->submodel == SUBMDL_ES1887) {
800		src = ess_getmixer (devc, 0x7f) >> 4;
801	} else {
802		src = 0xff;
803	}
804
805#ifdef FKS_REG_LOGGING
806printk(KERN_INFO "FKS: sbintr src=%x\n",(int)src);
807#endif
808	ess_handle_channel
809		( "Audio 1"
810		, devc->dev, devc->intr_active   , src & 0x01, devc->irq_mode   );
811	ess_handle_channel
812		( "Audio 2"
813		, devc->dev, devc->intr_active_16, src & 0x02, devc->irq_mode_16);
814	/*
815	 * Acknowledge interrupts
816	 */
817	if (devc->submodel == SUBMDL_ES1887 && (src & 0x02)) {
818		ess_chgmixer (devc, 0x7a, 0x80, 0x00);
819	}
820
821	if (src & 0x01) {
822		status = inb(DSP_DATA_AVAIL);
823	}
824}
825
826static void ess_extended (sb_devc * devc)
827{
828	/* Enable extended mode */
829
830	sb_dsp_command(devc, 0xc6);
831}
832
833static int ess_write (sb_devc * devc, unsigned char reg, unsigned char data)
834{
835#ifdef FKS_REG_LOGGING
836printk(KERN_INFO "FKS: write reg %x: %x\n", reg, data);
837#endif
838	/* Write a byte to an extended mode register of ES1688 */
839
840	if (!sb_dsp_command(devc, reg))
841		return 0;
842
843	return sb_dsp_command(devc, data);
844}
845
846static int ess_read (sb_devc * devc, unsigned char reg)
847{
848	/* Read a byte from an extended mode register of ES1688 */
849
850	/* Read register command */
851	if (!sb_dsp_command(devc, 0xc0)) return -1;
852
853	if (!sb_dsp_command(devc, reg )) return -1;
854
855	return sb_dsp_get_byte(devc);
856}
857
858int ess_dsp_reset(sb_devc * devc)
859{
860	int loopc;
861
862#ifdef FKS_REG_LOGGING
863printk(KERN_INFO "FKS: ess_dsp_reset 1\n");
864ess_show_mixerregs (devc);
865#endif
866
867	DEB(printk("Entered ess_dsp_reset()\n"));
868
869	outb(3, DSP_RESET); /* Reset FIFO too */
870
871	udelay(10);
872	outb(0, DSP_RESET);
873	udelay(30);
874
875	for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
876
877	if (inb(DSP_READ) != 0xAA) {
878		DDB(printk("sb: No response to RESET\n"));
879		return 0;   /* Sorry */
880	}
881	ess_extended (devc);
882
883	DEB(printk("sb_dsp_reset() OK\n"));
884
885#ifdef FKS_LOGGING
886printk(KERN_INFO "FKS: dsp_reset 2\n");
887ess_show_mixerregs (devc);
888#endif
889
890	return 1;
891}
892
893static int ess_irq_bits (int irq)
894{
895	switch (irq) {
896	case 2:
897	case 9:
898		return 0;
899
900	case 5:
901		return 1;
902
903	case 7:
904		return 2;
905
906	case 10:
907		return 3;
908
909	default:
910		printk(KERN_ERR "ESS1688: Invalid IRQ %d\n", irq);
911		return -1;
912	}
913}
914
915/*
916 *	Set IRQ configuration register for all ESS models
917 */
918static int ess_common_set_irq_hw (sb_devc * devc)
919{
920	int irq_bits;
921
922	if ((irq_bits = ess_irq_bits (devc->irq)) == -1) return 0;
923
924	if (!ess_write (devc, 0xb1, 0x50 | (irq_bits << 2))) {
925		printk(KERN_ERR "ES1688: Failed to write to IRQ config register\n");
926		return 0;
927	}
928	return 1;
929}
930
931/*
932 * I wanna use modern ES1887 mixer irq handling. Funny is the
933 * fact that my BIOS wants the same. But suppose someone's BIOS
934 * doesn't do this!
935 * This is independent of duplex. If there's a 1887 this will
936 * prevent it from going into 1888 mode.
937 */
938static void ess_es1887_set_irq_hw (sb_devc * devc)
939{
940	int irq_bits;
941
942	if ((irq_bits = ess_irq_bits (devc->irq)) == -1) return;
943
944	ess_chgmixer (devc, 0x7f, 0x0f, 0x01 | ((irq_bits + 1) << 1));
945}
946
947static int ess_set_irq_hw (sb_devc * devc)
948{
949	if (devc->submodel == SUBMDL_ES1887) ess_es1887_set_irq_hw (devc);
950
951	return ess_common_set_irq_hw (devc);
952}
953
954#ifdef FKS_TEST
955
956/*
957 * FKS_test:
958 *	for ES1887: 00, 18, non wr bits: 0001 1000
959 *	for ES1868: 00, b8, non wr bits: 1011 1000
960 *	for ES1888: 00, f8, non wr bits: 1111 1000
961 *	for ES1688: 00, f8, non wr bits: 1111 1000
962 *	+   ES968
963 */
964
965static void FKS_test (sb_devc * devc)
966{
967	int val1, val2;
968	val1 = ess_getmixer (devc, 0x64);
969	ess_setmixer (devc, 0x64, ~val1);
970	val2 = ess_getmixer (devc, 0x64) ^ ~val1;
971	ess_setmixer (devc, 0x64, val1);
972	val1 ^= ess_getmixer (devc, 0x64);
973printk (KERN_INFO "FKS: FKS_test %02x, %02x\n", (val1 & 0x0ff), (val2 & 0x0ff));
974};
975#endif
976
977static unsigned int ess_identify (sb_devc * devc)
978{
979	unsigned int val;
980	unsigned long flags;
981
982	spin_lock_irqsave(&devc->lock, flags);
983	outb(((unsigned char) (0x40 & 0xff)), MIXER_ADDR);
984
985	udelay(20);
986	val  = inb(MIXER_DATA) << 8;
987	udelay(20);
988	val |= inb(MIXER_DATA);
989	udelay(20);
990	spin_unlock_irqrestore(&devc->lock, flags);
991
992	return val;
993}
994
995/*
996 * ESS technology describes a detection scheme in their docs. It involves
997 * fiddling with the bits in certain mixer registers. ess_probe is supposed
998 * to help.
999 *
1000 * FKS: tracing shows ess_probe writes wrong value to 0x64. Bit 3 reads 1, but
1001 * should be written 0 only. Check this.
1002 */
1003static int ess_probe (sb_devc * devc, int reg, int xorval)
1004{
1005	int  val1, val2, val3;
1006
1007	val1 = ess_getmixer (devc, reg);
1008	val2 = val1 ^ xorval;
1009	ess_setmixer (devc, reg, val2);
1010	val3 = ess_getmixer (devc, reg);
1011	ess_setmixer (devc, reg, val1);
1012
1013	return (val2 == val3);
1014}
1015
1016int ess_init(sb_devc * devc, struct address_info *hw_config)
1017{
1018	unsigned char cfg;
1019	int ess_major = 0, ess_minor = 0;
1020	int i;
1021	static char name[100], modelname[10];
1022
1023	/*
1024	 * Try to detect ESS chips.
1025	 */
1026
1027	sb_dsp_command(devc, 0xe7); /* Return identification */
1028
1029	for (i = 1000; i; i--) {
1030		if (inb(DSP_DATA_AVAIL) & 0x80) {
1031			if (ess_major == 0) {
1032				ess_major = inb(DSP_READ);
1033			} else {
1034				ess_minor = inb(DSP_READ);
1035				break;
1036			}
1037		}
1038	}
1039
1040	if (ess_major == 0) return 0;
1041
1042	if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80) {
1043		sprintf(name, "ESS ES488 AudioDrive (rev %d)",
1044			ess_minor & 0x0f);
1045		hw_config->name = name;
1046		devc->model = MDL_SBPRO;
1047		return 1;
1048	}
1049
1050	/*
1051	 * This the detection heuristic of ESS technology, though somewhat
1052	 * changed to actually make it work.
1053	 * This results in the following detection steps:
1054	 * - distinct between ES688 and ES1688+ (as always done in this driver)
1055	 *   if ES688 we're ready
1056	 * - try to detect ES1868, ES1869 or ES1878 (ess_identify)
1057	 *   if successful we're ready
1058	 * - try to detect ES1888, ES1887 or ES1788 (aim: detect ES1887)
1059	 *   if successful we're ready
1060	 * - Dunno. Must be 1688. Will do in general
1061	 *
1062	 * This is the most BETA part of the software: Will the detection
1063	 * always work?
1064	 */
1065	devc->model = MDL_ESS;
1066	devc->submodel = ess_minor & 0x0f;
1067
1068	if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80) {
1069		char *chip = NULL;
1070		int submodel = -1;
1071
1072		switch (devc->sbmo.esstype) {
1073		case ESSTYPE_DETECT:
1074		case ESSTYPE_LIKE20:
1075			break;
1076		case 688:
1077			submodel = 0x00;
1078			break;
1079		case 1688:
1080			submodel = 0x08;
1081			break;
1082		case 1868:
1083			submodel = SUBMDL_ES1868;
1084			break;
1085		case 1869:
1086			submodel = SUBMDL_ES1869;
1087			break;
1088		case 1788:
1089			submodel = SUBMDL_ES1788;
1090			break;
1091		case 1878:
1092			submodel = SUBMDL_ES1878;
1093			break;
1094		case 1879:
1095			submodel = SUBMDL_ES1879;
1096			break;
1097		case 1887:
1098			submodel = SUBMDL_ES1887;
1099			break;
1100		case 1888:
1101			submodel = SUBMDL_ES1888;
1102			break;
1103		default:
1104			printk (KERN_ERR "Invalid esstype=%d specified\n", devc->sbmo.esstype);
1105			return 0;
1106		};
1107		if (submodel != -1) {
1108			devc->submodel = submodel;
1109			sprintf (modelname, "ES%d", devc->sbmo.esstype);
1110			chip = modelname;
1111		};
1112		if (chip == NULL && (ess_minor & 0x0f) < 8) {
1113			chip = "ES688";
1114		};
1115#ifdef FKS_TEST
1116FKS_test (devc);
1117#endif
1118		/*
1119		 * If Nothing detected yet, and we want 2.0 behaviour...
1120		 * Then let's assume it's ES1688.
1121		 */
1122		if (chip == NULL && devc->sbmo.esstype == ESSTYPE_LIKE20) {
1123			chip = "ES1688";
1124		};
1125
1126		if (chip == NULL) {
1127			int type;
1128
1129			type = ess_identify (devc);
1130
1131			switch (type) {
1132			case 0x1868:
1133				chip = "ES1868";
1134				devc->submodel = SUBMDL_ES1868;
1135				break;
1136			case 0x1869:
1137				chip = "ES1869";
1138				devc->submodel = SUBMDL_ES1869;
1139				break;
1140			case 0x1878:
1141				chip = "ES1878";
1142				devc->submodel = SUBMDL_ES1878;
1143				break;
1144			case 0x1879:
1145				chip = "ES1879";
1146				devc->submodel = SUBMDL_ES1879;
1147				break;
1148			default:
1149				if ((type & 0x00ff) != ((type >> 8) & 0x00ff)) {
1150					printk ("ess_init: Unrecognized %04x\n", type);
1151				}
1152			};
1153		};
1154		/*
1155		 * the probing of bit 2 is my idea. The ES1887 docs want me to probe
1156		 * bit 3. This results in ES1688 being detected as ES1788.
1157		 * Bit 2 is for "Enable HWV IRQE", but as ES(1)688 chips don't have
1158		 * HardWare Volume, I think they don't have this IRQE.
1159		 */
1160		if (chip == NULL && ess_probe(devc, 0x64, (1 << 2))) {
1161			if (ess_probe (devc, 0x70, 0x7f)) {
1162				if (ess_probe (devc, 0x64, (1 << 5))) {
1163					chip = "ES1887";
1164					devc->submodel = SUBMDL_ES1887;
1165				} else {
1166					chip = "ES1888";
1167					devc->submodel = SUBMDL_ES1888;
1168				}
1169			} else {
1170				chip = "ES1788";
1171				devc->submodel = SUBMDL_ES1788;
1172			}
1173		};
1174		if (chip == NULL) {
1175			chip = "ES1688";
1176		};
1177
1178	    printk ( KERN_INFO "ESS chip %s %s%s\n"
1179               , chip
1180               , ( devc->sbmo.esstype == ESSTYPE_DETECT || devc->sbmo.esstype == ESSTYPE_LIKE20
1181                 ? "detected"
1182                 : "specified"
1183                 )
1184               , ( devc->sbmo.esstype == ESSTYPE_LIKE20
1185                 ? " (kernel 2.0 compatible)"
1186                 : ""
1187                 )
1188               );
1189
1190		sprintf(name,"ESS %s AudioDrive (rev %d)", chip, ess_minor & 0x0f);
1191	} else {
1192		strcpy(name, "Jazz16");
1193	}
1194
1195	/* AAS: info stolen from ALSA: these boards have different clocks */
1196	switch(devc->submodel) {
1197/* APPARENTLY NOT 1869 AND 1887
1198		case SUBMDL_ES1869:
1199		case SUBMDL_ES1887:
1200*/
1201		case SUBMDL_ES1888:
1202			devc->caps |= SB_CAP_ES18XX_RATE;
1203			break;
1204	}
1205
1206	hw_config->name = name;
1207	/* FKS: sb_dsp_reset to enable extended mode???? */
1208	sb_dsp_reset(devc); /* Turn on extended mode */
1209
1210	/*
1211	 *  Enable joystick and OPL3
1212	 */
1213	cfg = ess_getmixer (devc, 0x40);
1214	ess_setmixer (devc, 0x40, cfg | 0x03);
1215	if (devc->submodel >= 8) {		/* ES1688 */
1216		devc->caps |= SB_NO_MIDI;   /* ES1688 uses MPU401 MIDI mode */
1217	}
1218	sb_dsp_reset (devc);
1219
1220	/*
1221	 * This is important! If it's not done, the IRQ probe in sb_dsp_init
1222	 * may fail.
1223	 */
1224	return ess_set_irq_hw (devc);
1225}
1226
1227static int ess_set_dma_hw(sb_devc * devc)
1228{
1229	unsigned char cfg, dma_bits = 0, dma16_bits;
1230	int dma;
1231
1232#ifdef FKS_LOGGING
1233printk(KERN_INFO "ess_set_dma_hw: dma8=%d,dma16=%d,dup=%d\n"
1234, devc->dma8, devc->dma16, devc->duplex);
1235#endif
1236
1237	/*
1238	 * FKS: It seems as if this duplex flag isn't set yet. Check it.
1239	 */
1240	dma = devc->dma8;
1241
1242	if (dma > 3 || dma < 0 || dma == 2) {
1243		dma_bits = 0;
1244		printk(KERN_ERR "ESS1688: Invalid DMA8 %d\n", dma);
1245		return 0;
1246	} else {
1247		/* Extended mode DMA enable */
1248		cfg = 0x50;
1249
1250		if (dma == 3) {
1251			dma_bits = 3;
1252		} else {
1253			dma_bits = dma + 1;
1254		}
1255	}
1256
1257	if (!ess_write (devc, 0xb2, cfg | (dma_bits << 2))) {
1258		printk(KERN_ERR "ESS1688: Failed to write to DMA config register\n");
1259		return 0;
1260	}
1261
1262	if (devc->duplex) {
1263		dma = devc->dma16;
1264		dma16_bits = 0;
1265
1266		if (dma >= 0) {
1267			switch (dma) {
1268			case 0:
1269				dma_bits = 0x04;
1270				break;
1271			case 1:
1272				dma_bits = 0x05;
1273				break;
1274			case 3:
1275				dma_bits = 0x06;
1276				break;
1277			case 5:
1278				dma_bits   = 0x07;
1279				dma16_bits = 0x20;
1280				break;
1281			default:
1282				printk(KERN_ERR "ESS1887: Invalid DMA16 %d\n", dma);
1283				return 0;
1284			};
1285			ess_chgmixer (devc, 0x78, 0x20, dma16_bits);
1286			ess_chgmixer (devc, 0x7d, 0x07, dma_bits);
1287		}
1288	}
1289	return 1;
1290}
1291
1292/*
1293 * This one is called from sb_dsp_init.
1294 *
1295 * Return values:
1296 *  0: Failed
1297 *  1: Succeeded or doesn't apply (not SUBMDL_ES1887)
1298 */
1299int ess_dsp_init (sb_devc *devc, struct address_info *hw_config)
1300{
1301	/*
1302	 * Caller also checks this, but anyway
1303	 */
1304	if (devc->model != MDL_ESS) {
1305		printk (KERN_INFO "ess_dsp_init for non ESS chip\n");
1306		return 1;
1307	}
1308	/*
1309	 * This for ES1887 to run Full Duplex. Actually ES1888
1310	 * is allowed to do so too. I have no idea yet if this
1311	 * will work for ES1888 however.
1312	 *
1313	 * For SB16 having both dma8 and dma16 means enable
1314	 * Full Duplex. Let's try this for ES1887 too
1315	 *
1316	 */
1317	if (devc->submodel == SUBMDL_ES1887) {
1318		if (hw_config->dma2 != -1) {
1319			devc->dma16 = hw_config->dma2;
1320		}
1321		/*
1322		 * devc->duplex initialization is put here, cause
1323		 * ess_set_dma_hw needs it.
1324		 */
1325		if (devc->dma8 != devc->dma16 && devc->dma16 != -1) {
1326			devc->duplex = 1;
1327		}
1328	}
1329	if (!ess_set_dma_hw (devc)) {
1330		free_irq(devc->irq, devc);
1331		return 0;
1332	}
1333	return 1;
1334}
1335
1336/****************************************************************************
1337 *																			*
1338 *									ESS mixer								*
1339 *																			*
1340 ****************************************************************************/
1341
1342#define ES688_RECORDING_DEVICES	\
1343			( SOUND_MASK_LINE	| SOUND_MASK_MIC	| SOUND_MASK_CD		)
1344#define ES688_MIXER_DEVICES		\
1345			( SOUND_MASK_SYNTH	| SOUND_MASK_PCM	| SOUND_MASK_LINE	\
1346			| SOUND_MASK_MIC	| SOUND_MASK_CD		| SOUND_MASK_VOLUME	\
1347			| SOUND_MASK_LINE2	| SOUND_MASK_SPEAKER					)
1348
1349#define ES1688_RECORDING_DEVICES	\
1350			( ES688_RECORDING_DEVICES					)
1351#define ES1688_MIXER_DEVICES		\
1352			( ES688_MIXER_DEVICES | SOUND_MASK_RECLEV	)
1353
1354#define ES1887_RECORDING_DEVICES	\
1355			( ES1688_RECORDING_DEVICES | SOUND_MASK_LINE2 | SOUND_MASK_SYNTH)
1356#define ES1887_MIXER_DEVICES		\
1357			( ES1688_MIXER_DEVICES											)
1358
1359/*
1360 * Mixer registers of ES1887
1361 *
1362 * These registers specifically take care of recording levels. To make the
1363 * mapping from playback devices to recording devices every recording
1364 * devices = playback device + ES_REC_MIXER_RECDIFF
1365 */
1366#define ES_REC_MIXER_RECBASE	(SOUND_MIXER_LINE3 + 1)
1367#define ES_REC_MIXER_RECDIFF	(ES_REC_MIXER_RECBASE - SOUND_MIXER_SYNTH)
1368
1369#define ES_REC_MIXER_RECSYNTH	(SOUND_MIXER_SYNTH	 + ES_REC_MIXER_RECDIFF)
1370#define ES_REC_MIXER_RECPCM		(SOUND_MIXER_PCM	 + ES_REC_MIXER_RECDIFF)
1371#define ES_REC_MIXER_RECSPEAKER	(SOUND_MIXER_SPEAKER + ES_REC_MIXER_RECDIFF)
1372#define ES_REC_MIXER_RECLINE	(SOUND_MIXER_LINE	 + ES_REC_MIXER_RECDIFF)
1373#define ES_REC_MIXER_RECMIC		(SOUND_MIXER_MIC	 + ES_REC_MIXER_RECDIFF)
1374#define ES_REC_MIXER_RECCD		(SOUND_MIXER_CD		 + ES_REC_MIXER_RECDIFF)
1375#define ES_REC_MIXER_RECIMIX	(SOUND_MIXER_IMIX	 + ES_REC_MIXER_RECDIFF)
1376#define ES_REC_MIXER_RECALTPCM	(SOUND_MIXER_ALTPCM	 + ES_REC_MIXER_RECDIFF)
1377#define ES_REC_MIXER_RECRECLEV	(SOUND_MIXER_RECLEV	 + ES_REC_MIXER_RECDIFF)
1378#define ES_REC_MIXER_RECIGAIN	(SOUND_MIXER_IGAIN	 + ES_REC_MIXER_RECDIFF)
1379#define ES_REC_MIXER_RECOGAIN	(SOUND_MIXER_OGAIN	 + ES_REC_MIXER_RECDIFF)
1380#define ES_REC_MIXER_RECLINE1	(SOUND_MIXER_LINE1	 + ES_REC_MIXER_RECDIFF)
1381#define ES_REC_MIXER_RECLINE2	(SOUND_MIXER_LINE2	 + ES_REC_MIXER_RECDIFF)
1382#define ES_REC_MIXER_RECLINE3	(SOUND_MIXER_LINE3	 + ES_REC_MIXER_RECDIFF)
1383
1384static mixer_tab es688_mix = {
1385MIX_ENT(SOUND_MIXER_VOLUME,			0x32, 7, 4, 0x32, 3, 4),
1386MIX_ENT(SOUND_MIXER_BASS,			0x00, 0, 0, 0x00, 0, 0),
1387MIX_ENT(SOUND_MIXER_TREBLE,			0x00, 0, 0, 0x00, 0, 0),
1388MIX_ENT(SOUND_MIXER_SYNTH,			0x36, 7, 4, 0x36, 3, 4),
1389MIX_ENT(SOUND_MIXER_PCM,			0x14, 7, 4, 0x14, 3, 4),
1390MIX_ENT(SOUND_MIXER_SPEAKER,		0x3c, 2, 3, 0x00, 0, 0),
1391MIX_ENT(SOUND_MIXER_LINE,			0x3e, 7, 4, 0x3e, 3, 4),
1392MIX_ENT(SOUND_MIXER_MIC,			0x1a, 7, 4, 0x1a, 3, 4),
1393MIX_ENT(SOUND_MIXER_CD,				0x38, 7, 4, 0x38, 3, 4),
1394MIX_ENT(SOUND_MIXER_IMIX,			0x00, 0, 0, 0x00, 0, 0),
1395MIX_ENT(SOUND_MIXER_ALTPCM,			0x00, 0, 0, 0x00, 0, 0),
1396MIX_ENT(SOUND_MIXER_RECLEV,			0x00, 0, 0, 0x00, 0, 0),
1397MIX_ENT(SOUND_MIXER_IGAIN,			0x00, 0, 0, 0x00, 0, 0),
1398MIX_ENT(SOUND_MIXER_OGAIN,			0x00, 0, 0, 0x00, 0, 0),
1399MIX_ENT(SOUND_MIXER_LINE1,			0x00, 0, 0, 0x00, 0, 0),
1400MIX_ENT(SOUND_MIXER_LINE2,			0x3a, 7, 4, 0x3a, 3, 4),
1401MIX_ENT(SOUND_MIXER_LINE3,			0x00, 0, 0, 0x00, 0, 0)
1402};
1403
1404/*
1405 * The ES1688 specifics... hopefully correct...
1406 * - 6 bit master volume
1407 *   I was wrong, ES1888 docs say ES1688 didn't have it.
1408 * - RECLEV control
1409 * These may apply to ES688 too. I have no idea.
1410 */
1411static mixer_tab es1688_mix = {
1412MIX_ENT(SOUND_MIXER_VOLUME,			0x32, 7, 4, 0x32, 3, 4),
1413MIX_ENT(SOUND_MIXER_BASS,			0x00, 0, 0, 0x00, 0, 0),
1414MIX_ENT(SOUND_MIXER_TREBLE,			0x00, 0, 0, 0x00, 0, 0),
1415MIX_ENT(SOUND_MIXER_SYNTH,			0x36, 7, 4, 0x36, 3, 4),
1416MIX_ENT(SOUND_MIXER_PCM,			0x14, 7, 4, 0x14, 3, 4),
1417MIX_ENT(SOUND_MIXER_SPEAKER,		0x3c, 2, 3, 0x00, 0, 0),
1418MIX_ENT(SOUND_MIXER_LINE,			0x3e, 7, 4, 0x3e, 3, 4),
1419MIX_ENT(SOUND_MIXER_MIC,			0x1a, 7, 4, 0x1a, 3, 4),
1420MIX_ENT(SOUND_MIXER_CD,				0x38, 7, 4, 0x38, 3, 4),
1421MIX_ENT(SOUND_MIXER_IMIX,			0x00, 0, 0, 0x00, 0, 0),
1422MIX_ENT(SOUND_MIXER_ALTPCM,			0x00, 0, 0, 0x00, 0, 0),
1423MIX_ENT(SOUND_MIXER_RECLEV,			0xb4, 7, 4, 0xb4, 3, 4),
1424MIX_ENT(SOUND_MIXER_IGAIN,			0x00, 0, 0, 0x00, 0, 0),
1425MIX_ENT(SOUND_MIXER_OGAIN,			0x00, 0, 0, 0x00, 0, 0),
1426MIX_ENT(SOUND_MIXER_LINE1,			0x00, 0, 0, 0x00, 0, 0),
1427MIX_ENT(SOUND_MIXER_LINE2,			0x3a, 7, 4, 0x3a, 3, 4),
1428MIX_ENT(SOUND_MIXER_LINE3,			0x00, 0, 0, 0x00, 0, 0)
1429};
1430
1431static mixer_tab es1688later_mix = {
1432MIX_ENT(SOUND_MIXER_VOLUME,			0x60, 5, 6, 0x62, 5, 6),
1433MIX_ENT(SOUND_MIXER_BASS,			0x00, 0, 0, 0x00, 0, 0),
1434MIX_ENT(SOUND_MIXER_TREBLE,			0x00, 0, 0, 0x00, 0, 0),
1435MIX_ENT(SOUND_MIXER_SYNTH,			0x36, 7, 4, 0x36, 3, 4),
1436MIX_ENT(SOUND_MIXER_PCM,			0x14, 7, 4, 0x14, 3, 4),
1437MIX_ENT(SOUND_MIXER_SPEAKER,		0x3c, 2, 3, 0x00, 0, 0),
1438MIX_ENT(SOUND_MIXER_LINE,			0x3e, 7, 4, 0x3e, 3, 4),
1439MIX_ENT(SOUND_MIXER_MIC,			0x1a, 7, 4, 0x1a, 3, 4),
1440MIX_ENT(SOUND_MIXER_CD,				0x38, 7, 4, 0x38, 3, 4),
1441MIX_ENT(SOUND_MIXER_IMIX,			0x00, 0, 0, 0x00, 0, 0),
1442MIX_ENT(SOUND_MIXER_ALTPCM,			0x00, 0, 0, 0x00, 0, 0),
1443MIX_ENT(SOUND_MIXER_RECLEV,			0xb4, 7, 4, 0xb4, 3, 4),
1444MIX_ENT(SOUND_MIXER_IGAIN,			0x00, 0, 0, 0x00, 0, 0),
1445MIX_ENT(SOUND_MIXER_OGAIN,			0x00, 0, 0, 0x00, 0, 0),
1446MIX_ENT(SOUND_MIXER_LINE1,			0x00, 0, 0, 0x00, 0, 0),
1447MIX_ENT(SOUND_MIXER_LINE2,			0x3a, 7, 4, 0x3a, 3, 4),
1448MIX_ENT(SOUND_MIXER_LINE3,			0x00, 0, 0, 0x00, 0, 0)
1449};
1450
1451/*
1452 * This one is for all ESS chips with a record mixer.
1453 * It's not used (yet) however
1454 */
1455static mixer_tab es_rec_mix = {
1456MIX_ENT(SOUND_MIXER_VOLUME,			0x60, 5, 6, 0x62, 5, 6),
1457MIX_ENT(SOUND_MIXER_BASS,			0x00, 0, 0, 0x00, 0, 0),
1458MIX_ENT(SOUND_MIXER_TREBLE,			0x00, 0, 0, 0x00, 0, 0),
1459MIX_ENT(SOUND_MIXER_SYNTH,			0x36, 7, 4, 0x36, 3, 4),
1460MIX_ENT(SOUND_MIXER_PCM,			0x14, 7, 4, 0x14, 3, 4),
1461MIX_ENT(SOUND_MIXER_SPEAKER,		0x3c, 2, 3, 0x00, 0, 0),
1462MIX_ENT(SOUND_MIXER_LINE,			0x3e, 7, 4, 0x3e, 3, 4),
1463MIX_ENT(SOUND_MIXER_MIC,			0x1a, 7, 4, 0x1a, 3, 4),
1464MIX_ENT(SOUND_MIXER_CD,				0x38, 7, 4, 0x38, 3, 4),
1465MIX_ENT(SOUND_MIXER_IMIX,			0x00, 0, 0, 0x00, 0, 0),
1466MIX_ENT(SOUND_MIXER_ALTPCM,			0x00, 0, 0, 0x00, 0, 0),
1467MIX_ENT(SOUND_MIXER_RECLEV,			0xb4, 7, 4, 0xb4, 3, 4),
1468MIX_ENT(SOUND_MIXER_IGAIN,			0x00, 0, 0, 0x00, 0, 0),
1469MIX_ENT(SOUND_MIXER_OGAIN,			0x00, 0, 0, 0x00, 0, 0),
1470MIX_ENT(SOUND_MIXER_LINE1,			0x00, 0, 0, 0x00, 0, 0),
1471MIX_ENT(SOUND_MIXER_LINE2,			0x3a, 7, 4, 0x3a, 3, 4),
1472MIX_ENT(SOUND_MIXER_LINE3,			0x00, 0, 0, 0x00, 0, 0),
1473MIX_ENT(ES_REC_MIXER_RECSYNTH,		0x6b, 7, 4, 0x6b, 3, 4),
1474MIX_ENT(ES_REC_MIXER_RECPCM,		0x00, 0, 0, 0x00, 0, 0),
1475MIX_ENT(ES_REC_MIXER_RECSPEAKER,	0x00, 0, 0, 0x00, 0, 0),
1476MIX_ENT(ES_REC_MIXER_RECLINE,		0x6e, 7, 4, 0x6e, 3, 4),
1477MIX_ENT(ES_REC_MIXER_RECMIC,		0x68, 7, 4, 0x68, 3, 4),
1478MIX_ENT(ES_REC_MIXER_RECCD,			0x6a, 7, 4, 0x6a, 3, 4),
1479MIX_ENT(ES_REC_MIXER_RECIMIX,		0x00, 0, 0, 0x00, 0, 0),
1480MIX_ENT(ES_REC_MIXER_RECALTPCM,		0x00, 0, 0, 0x00, 0, 0),
1481MIX_ENT(ES_REC_MIXER_RECRECLEV,		0x00, 0, 0, 0x00, 0, 0),
1482MIX_ENT(ES_REC_MIXER_RECIGAIN,		0x00, 0, 0, 0x00, 0, 0),
1483MIX_ENT(ES_REC_MIXER_RECOGAIN,		0x00, 0, 0, 0x00, 0, 0),
1484MIX_ENT(ES_REC_MIXER_RECLINE1,		0x00, 0, 0, 0x00, 0, 0),
1485MIX_ENT(ES_REC_MIXER_RECLINE2,		0x6c, 7, 4, 0x6c, 3, 4),
1486MIX_ENT(ES_REC_MIXER_RECLINE3,		0x00, 0, 0, 0x00, 0, 0)
1487};
1488
1489/*
1490 * This one is for ES1887. It's little different from es_rec_mix: it
1491 * has 0x7c for PCM playback level. This is because ES1887 uses
1492 * Audio 2 for playback.
1493 */
1494static mixer_tab es1887_mix = {
1495MIX_ENT(SOUND_MIXER_VOLUME,			0x60, 5, 6, 0x62, 5, 6),
1496MIX_ENT(SOUND_MIXER_BASS,			0x00, 0, 0, 0x00, 0, 0),
1497MIX_ENT(SOUND_MIXER_TREBLE,			0x00, 0, 0, 0x00, 0, 0),
1498MIX_ENT(SOUND_MIXER_SYNTH,			0x36, 7, 4, 0x36, 3, 4),
1499MIX_ENT(SOUND_MIXER_PCM,			0x7c, 7, 4, 0x7c, 3, 4),
1500MIX_ENT(SOUND_MIXER_SPEAKER,		0x3c, 2, 3, 0x00, 0, 0),
1501MIX_ENT(SOUND_MIXER_LINE,			0x3e, 7, 4, 0x3e, 3, 4),
1502MIX_ENT(SOUND_MIXER_MIC,			0x1a, 7, 4, 0x1a, 3, 4),
1503MIX_ENT(SOUND_MIXER_CD,				0x38, 7, 4, 0x38, 3, 4),
1504MIX_ENT(SOUND_MIXER_IMIX,			0x00, 0, 0, 0x00, 0, 0),
1505MIX_ENT(SOUND_MIXER_ALTPCM,			0x00, 0, 0, 0x00, 0, 0),
1506MIX_ENT(SOUND_MIXER_RECLEV,			0xb4, 7, 4, 0xb4, 3, 4),
1507MIX_ENT(SOUND_MIXER_IGAIN,			0x00, 0, 0, 0x00, 0, 0),
1508MIX_ENT(SOUND_MIXER_OGAIN,			0x00, 0, 0, 0x00, 0, 0),
1509MIX_ENT(SOUND_MIXER_LINE1,			0x00, 0, 0, 0x00, 0, 0),
1510MIX_ENT(SOUND_MIXER_LINE2,			0x3a, 7, 4, 0x3a, 3, 4),
1511MIX_ENT(SOUND_MIXER_LINE3,			0x00, 0, 0, 0x00, 0, 0),
1512MIX_ENT(ES_REC_MIXER_RECSYNTH,		0x6b, 7, 4, 0x6b, 3, 4),
1513MIX_ENT(ES_REC_MIXER_RECPCM,		0x00, 0, 0, 0x00, 0, 0),
1514MIX_ENT(ES_REC_MIXER_RECSPEAKER,	0x00, 0, 0, 0x00, 0, 0),
1515MIX_ENT(ES_REC_MIXER_RECLINE,		0x6e, 7, 4, 0x6e, 3, 4),
1516MIX_ENT(ES_REC_MIXER_RECMIC,		0x68, 7, 4, 0x68, 3, 4),
1517MIX_ENT(ES_REC_MIXER_RECCD,			0x6a, 7, 4, 0x6a, 3, 4),
1518MIX_ENT(ES_REC_MIXER_RECIMIX,		0x00, 0, 0, 0x00, 0, 0),
1519MIX_ENT(ES_REC_MIXER_RECALTPCM,		0x00, 0, 0, 0x00, 0, 0),
1520MIX_ENT(ES_REC_MIXER_RECRECLEV,		0x00, 0, 0, 0x00, 0, 0),
1521MIX_ENT(ES_REC_MIXER_RECIGAIN,		0x00, 0, 0, 0x00, 0, 0),
1522MIX_ENT(ES_REC_MIXER_RECOGAIN,		0x00, 0, 0, 0x00, 0, 0),
1523MIX_ENT(ES_REC_MIXER_RECLINE1,		0x00, 0, 0, 0x00, 0, 0),
1524MIX_ENT(ES_REC_MIXER_RECLINE2,		0x6c, 7, 4, 0x6c, 3, 4),
1525MIX_ENT(ES_REC_MIXER_RECLINE3,		0x00, 0, 0, 0x00, 0, 0)
1526};
1527
1528static int ess_has_rec_mixer (int submodel)
1529{
1530	switch (submodel) {
1531	case SUBMDL_ES1887:
1532		return 1;
1533	default:
1534		return 0;
1535	};
1536};
1537
1538#ifdef FKS_LOGGING
1539static int ess_mixer_mon_regs[]
1540	= { 0x70, 0x71, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7d, 0x7f
1541	  , 0xa1, 0xa2, 0xa4, 0xa5, 0xa8, 0xa9
1542	  , 0xb1, 0xb2, 0xb4, 0xb5, 0xb6, 0xb7, 0xb9
1543	  , 0x00};
1544
1545static void ess_show_mixerregs (sb_devc *devc)
1546{
1547	int *mp = ess_mixer_mon_regs;
1548
1549return;
1550
1551	while (*mp != 0) {
1552		printk (KERN_INFO "res (%x)=%x\n", *mp, (int)(ess_getmixer (devc, *mp)));
1553		mp++;
1554	}
1555}
1556#endif
1557
1558void ess_setmixer (sb_devc * devc, unsigned int port, unsigned int value)
1559{
1560	unsigned long flags;
1561
1562#ifdef FKS_LOGGING
1563printk(KERN_INFO "FKS: write mixer %x: %x\n", port, value);
1564#endif
1565
1566	spin_lock_irqsave(&devc->lock, flags);
1567	if (port >= 0xa0) {
1568		ess_write (devc, port, value);
1569	} else {
1570		outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
1571
1572		udelay(20);
1573		outb(((unsigned char) (value & 0xff)), MIXER_DATA);
1574		udelay(20);
1575	};
1576	spin_unlock_irqrestore(&devc->lock, flags);
1577}
1578
1579unsigned int ess_getmixer (sb_devc * devc, unsigned int port)
1580{
1581	unsigned int val;
1582	unsigned long flags;
1583
1584	spin_lock_irqsave(&devc->lock, flags);
1585
1586	if (port >= 0xa0) {
1587		val = ess_read (devc, port);
1588	} else {
1589		outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
1590
1591		udelay(20);
1592		val = inb(MIXER_DATA);
1593		udelay(20);
1594	}
1595	spin_unlock_irqrestore(&devc->lock, flags);
1596
1597	return val;
1598}
1599
1600static void ess_chgmixer
1601	(sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
1602{
1603	int value;
1604
1605	value = ess_getmixer (devc, reg);
1606	value = (value & ~mask) | (val & mask);
1607	ess_setmixer (devc, reg, value);
1608}
1609
1610/*
1611 * ess_mixer_init must be called from sb_mixer_init
1612 */
1613void ess_mixer_init (sb_devc * devc)
1614{
1615	devc->mixer_caps = SOUND_CAP_EXCL_INPUT;
1616
1617	/*
1618	* Take care of ES1887 specifics...
1619	*/
1620	switch (devc->submodel) {
1621	case SUBMDL_ES1887:
1622		devc->supported_devices		= ES1887_MIXER_DEVICES;
1623		devc->supported_rec_devices	= ES1887_RECORDING_DEVICES;
1624#ifdef FKS_LOGGING
1625printk (KERN_INFO "FKS: ess_mixer_init dup = %d\n", devc->duplex);
1626#endif
1627		if (devc->duplex) {
1628			devc->iomap				= &es1887_mix;
1629		} else {
1630			devc->iomap				= &es_rec_mix;
1631		}
1632		break;
1633	default:
1634		if (devc->submodel < 8) {
1635			devc->supported_devices		= ES688_MIXER_DEVICES;
1636			devc->supported_rec_devices	= ES688_RECORDING_DEVICES;
1637			devc->iomap					= &es688_mix;
1638		} else {
1639			/*
1640			 * es1688 has 4 bits master vol.
1641			 * later chips have 6 bits (?)
1642			 */
1643			devc->supported_devices		= ES1688_MIXER_DEVICES;
1644			devc->supported_rec_devices	= ES1688_RECORDING_DEVICES;
1645			if (devc->submodel < 0x10) {
1646				devc->iomap				= &es1688_mix;
1647			} else {
1648				devc->iomap				= &es1688later_mix;
1649			}
1650		}
1651	}
1652}
1653
1654/*
1655 * Changing playback levels at an ESS chip with record mixer means having to
1656 * take care of recording levels of recorded inputs (devc->recmask) too!
1657 */
1658int ess_mixer_set(sb_devc *devc, int dev, int left, int right)
1659{
1660	if (ess_has_rec_mixer (devc->submodel) && (devc->recmask & (1 << dev))) {
1661		sb_common_mixer_set (devc, dev + ES_REC_MIXER_RECDIFF, left, right);
1662	}
1663	return sb_common_mixer_set (devc, dev, left, right);
1664}
1665
1666/*
1667 * After a sb_dsp_reset extended register 0xb4 (RECLEV) is reset too. After
1668 * sb_dsp_reset RECLEV has to be restored. This is where ess_mixer_reload
1669 * helps.
1670 */
1671void ess_mixer_reload (sb_devc *devc, int dev)
1672{
1673	int left, right, value;
1674
1675	value = devc->levels[dev];
1676	left  = value & 0x000000ff;
1677	right = (value & 0x0000ff00) >> 8;
1678
1679	sb_common_mixer_set(devc, dev, left, right);
1680}
1681
1682int es_rec_set_recmask(sb_devc * devc, int mask)
1683{
1684	int i, i_mask, cur_mask, diff_mask;
1685	int value, left, right;
1686
1687#ifdef FKS_LOGGING
1688printk (KERN_INFO "FKS: es_rec_set_recmask mask = %x\n", mask);
1689#endif
1690	/*
1691	 * Changing the recmask on an ESS chip with recording mixer means:
1692	 * (1) Find the differences
1693	 * (2) For "turned-on"  inputs: make the recording level the playback level
1694	 * (3) For "turned-off" inputs: make the recording level zero
1695	 */
1696	cur_mask  = devc->recmask;
1697	diff_mask = (cur_mask ^ mask);
1698
1699	for (i = 0; i < 32; i++) {
1700		i_mask = (1 << i);
1701		if (diff_mask & i_mask) {	/* Difference? (1)  */
1702			if (mask & i_mask) {	/* Turn it on  (2)  */
1703				value = devc->levels[i];
1704				left  = value & 0x000000ff;
1705				right = (value & 0x0000ff00) >> 8;
1706			} else {				/* Turn it off (3)  */
1707				left  = 0;
1708				left  = 0;
1709				right = 0;
1710			}
1711			sb_common_mixer_set(devc, i + ES_REC_MIXER_RECDIFF, left, right);
1712		}
1713	}
1714	return mask;
1715}
1716
1717int ess_set_recmask(sb_devc * devc, int *mask)
1718{
1719	/* This applies to ESS chips with record mixers only! */
1720
1721	if (ess_has_rec_mixer (devc->submodel)) {
1722		*mask	= es_rec_set_recmask (devc, *mask);
1723		return 1;									/* Applied		*/
1724	} else {
1725		return 0;									/* Not applied	*/
1726	}
1727}
1728
1729/*
1730 * ess_mixer_reset must be called from sb_mixer_reset
1731 */
1732int ess_mixer_reset (sb_devc * devc)
1733{
1734	/*
1735	 * Separate actions for ESS chips with a record mixer:
1736	 */
1737	if (ess_has_rec_mixer (devc->submodel)) {
1738		switch (devc->submodel) {
1739		case SUBMDL_ES1887:
1740			/*
1741			 * Separate actions for ES1887:
1742			 * Change registers 7a and 1c to make the record mixer the
1743			 * actual recording source.
1744			 */
1745			ess_chgmixer(devc, 0x7a, 0x18, 0x08);
1746			ess_chgmixer(devc, 0x1c, 0x07, 0x07);
1747			break;
1748		};
1749		/*
1750		 * Call set_recmask for proper initialization
1751		 */
1752		devc->recmask = devc->supported_rec_devices;
1753		es_rec_set_recmask(devc, 0);
1754		devc->recmask = 0;
1755
1756		return 1;	/* We took care of recmask.				*/
1757	} else {
1758		return 0;	/* We didn't take care; caller do it	*/
1759	}
1760}
1761
1762/****************************************************************************
1763 *																			*
1764 *								ESS midi									*
1765 *																			*
1766 ****************************************************************************/
1767
1768/*
1769 * FKS: IRQ may be shared. Hm. And if so? Then What?
1770 */
1771int ess_midi_init(sb_devc * devc, struct address_info *hw_config)
1772{
1773	unsigned char   cfg, tmp;
1774
1775	cfg = ess_getmixer (devc, 0x40) & 0x03;
1776
1777	if (devc->submodel < 8) {
1778		ess_setmixer (devc, 0x40, cfg | 0x03);	/* Enable OPL3 & joystick */
1779		return 0;  					 /* ES688 doesn't support MPU401 mode */
1780	}
1781	tmp = (hw_config->io_base & 0x0f0) >> 4;
1782
1783	if (tmp > 3) {
1784		ess_setmixer (devc, 0x40, cfg);
1785		return 0;
1786	}
1787	cfg |= tmp << 3;
1788
1789	tmp = 1;		/* MPU enabled without interrupts */
1790
1791	/* May be shared: if so the value is -ve */
1792
1793	switch (abs(hw_config->irq)) {
1794		case 9:
1795			tmp = 0x4;
1796			break;
1797		case 5:
1798			tmp = 0x5;
1799			break;
1800		case 7:
1801			tmp = 0x6;
1802			break;
1803		case 10:
1804			tmp = 0x7;
1805			break;
1806		default:
1807			return 0;
1808	}
1809
1810	cfg |= tmp << 5;
1811	ess_setmixer (devc, 0x40, cfg | 0x03);
1812
1813	return 1;
1814}
1815
1816