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