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

/*
 * sound/sb_mixer.c
 * 
 * The low level mixer driver for the SoundBlaster Pro and SB16 cards.
 * 
 * Copyright by Hannu Savolainen 1993
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer. 2.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

#include "sound_config.h"

#if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_SB) && !defined(EXCLUDE_SBPRO)
#define __SB_MIXER_C__

#include "sb.h"
#include "sb_mixer.h"
#undef SB_TEST_IRQ

extern int      sbc_base;

static int mixer_initialized = 0;

static int supported_rec_devices;
static int supported_devices;
static int recmask = 0;
static int mixer_model;
static int mixer_caps;
static mixer_tab *iomap;

void
sb_setmixer (unsigned int port, unsigned int value)
{
  unsigned long flags;

  DISABLE_INTR(flags);
  OUTB ((unsigned char)(port & 0xff), MIXER_ADDR);	/* Select register */
  tenmicrosec ();
  OUTB ((unsigned char)(value & 0xff), MIXER_DATA);
  tenmicrosec ();
  RESTORE_INTR(flags);
}

int
sb_getmixer (unsigned int port)
{
  int             val;
  unsigned long flags;

  DISABLE_INTR(flags);
  OUTB ((unsigned char)(port & 0xff), MIXER_ADDR);	/* Select register */
  tenmicrosec ();
  val = INB (MIXER_DATA);
  tenmicrosec ();
  RESTORE_INTR(flags);

  return val;
}

void
sb_mixer_set_stereo(int mode)
{
  if (!mixer_initialized) return;

  sb_setmixer (OUT_FILTER, ((sb_getmixer (OUT_FILTER) & ~STEREO_DAC)
			 | (mode ? STEREO_DAC : MONO_DAC)));
}

static int
detect_mixer (void)
{
  /*
   * Detect the mixer by changing parameters of two volume channels. If the
   * values read back match with the values written, the mixer is there (is
   * it?)
   */
  sb_setmixer (FM_VOL, 0xff);
  sb_setmixer (VOC_VOL, 0x33);

  if (sb_getmixer (FM_VOL) != 0xff)
    return 0;			/* No match */
  if (sb_getmixer (VOC_VOL) != 0x33)
    return 0;

  return 1;
}

static void
change_bits(unsigned char *regval, int dev, int chn, int newval)
{
	unsigned char mask;
	int shift;

	mask = (1 << (*iomap)[dev][chn].nbits)-1;
	newval = ((newval * mask) + 50) / 100;	/* Scale it */

	shift = (*iomap)[dev][chn].bitoffs-(*iomap)[dev][LEFT_CHN].nbits+1;

	*regval &= ~(mask << shift);	/* Filter out the previous value */
	*regval |= (newval & mask) << shift; /* Set the new value */
}

static int
sb_mixer_get(int dev)
{
	if (!((1<<dev) & supported_devices)) 
	   return RET_ERROR(EINVAL);

	return levels[dev];
}

static int
sb_mixer_set (int dev, int value)
{
	int left = value & 0x000000ff;
	int right = (value & 0x0000ff00) >> 8;

	int regoffs;
	unsigned char val;

	if (left > 100) left = 100;
	if (right > 100) right = 100;

	if (dev > 31) return RET_ERROR(EINVAL);

	if (!(supported_devices & (1 << dev)))	/* Not supported */
	   return RET_ERROR(EINVAL);

	regoffs = (*iomap)[dev][LEFT_CHN].regno;

	if (regoffs == 0)
	   return RET_ERROR(EINVAL);

	val = sb_getmixer(regoffs);
	change_bits(&val, dev, LEFT_CHN, left);

	levels[dev] = left|(left << 8);

	if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) /* Change register */
	{
		sb_setmixer(regoffs, val);	/* Save the old one */
		regoffs = (*iomap)[dev][RIGHT_CHN].regno;

		if (regoffs == 0)
		   return left|(left << 8); /* Just left channel present */

		val = sb_getmixer(regoffs);	/* Read the new one */
	}

	change_bits(&val, dev, RIGHT_CHN, right);
	sb_setmixer(regoffs, val);

	levels[dev] = left | (right << 8);
	return left | (right << 8);
}

static void
set_recsrc(int src)
{
	sb_setmixer(RECORD_SRC, (sb_getmixer(RECORD_SRC)&~7) | (src&0x7));
}

static int
set_recmask(int mask)
{
      int devmask, i;
      unsigned char regimageL, regimageR;

      devmask = mask & supported_rec_devices;

      switch (mixer_model)
      {
      case 3:

      if (devmask != SOUND_MASK_MIC &&
	  devmask != SOUND_MASK_LINE &&
	  devmask != SOUND_MASK_CD)
	{			/* More than one devices selected. Drop the
				 * previous selection */
	  devmask &= ~recmask;
	}

      if (devmask != SOUND_MASK_MIC &&
	  devmask != SOUND_MASK_LINE &&
	  devmask != SOUND_MASK_CD)
	{			/* More than one devices selected. Default to
				 * mic */
	  devmask = SOUND_MASK_MIC;
	}


      if (devmask ^ recmask)/* Input source changed */
	{
	  switch (devmask)
	    {

	    case SOUND_MASK_MIC:
	      set_recsrc (SRC_MIC);
	      break;

	    case SOUND_MASK_LINE:
	      set_recsrc (SRC_LINE);
	      break;

	    case SOUND_MASK_CD:
	      set_recsrc (SRC_CD);
	      break;

	    default:
	      set_recsrc (SRC_MIC);
	    }
	}

      break;

      case 4:
	if (!devmask) devmask = SOUND_MASK_MIC;

        regimageL = regimageR = 0;
	for (i=0;i<SOUND_MIXER_NRDEVICES;i++)
	  if ((1<<i) & devmask)
	  {
	    regimageL |= sb16_recmasks_L[i];
	    regimageR |= sb16_recmasks_R[i];
	  }
	sb_setmixer(SB16_IMASK_L, regimageL);
	sb_setmixer(SB16_IMASK_R, regimageR);
      break;
      }

      recmask = devmask;
      return recmask;
}

static int
sb_mixer_ioctl (int dev, unsigned int cmd, unsigned int arg)
{
  if (((cmd >> 8) & 0xff) == 'M')
    {
      if (cmd & IOC_IN)
        switch (cmd & 0xff)
        {
        case SOUND_MIXER_RECSRC:
           return IOCTL_OUT(arg, set_recmask(IOCTL_IN(arg)));
           break;

	default:
	   return IOCTL_OUT (arg, sb_mixer_set (cmd & 0xff, IOCTL_IN (arg)));
        }
      else
	  switch (cmd & 0xff)	/* Return parameters */
	    {

	    case SOUND_MIXER_RECSRC:
	      return IOCTL_OUT (arg, recmask);
	      break;

	    case SOUND_MIXER_DEVMASK:
	      return IOCTL_OUT (arg, supported_devices);
	      break;

	    case SOUND_MIXER_STEREODEVS:
	      return IOCTL_OUT (arg, supported_devices & 
	                              ~(SOUND_MASK_MIC|SOUND_MASK_SPEAKER));
	      break;

	    case SOUND_MIXER_RECMASK:
	      return IOCTL_OUT (arg, supported_rec_devices);
	      break;

	    case SOUND_MIXER_CAPS:
	      return IOCTL_OUT (arg, mixer_caps);
	      break;

	    default:
	      return IOCTL_OUT (arg, sb_mixer_get (cmd & 0xff));
	    }
    }
  else
    return RET_ERROR (EINVAL);
}

static struct mixer_operations sb_mixer_operations =
{
  sb_mixer_ioctl
};

static void
sb_mixer_reset(void)
{
  int i;

  for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
    sb_mixer_set (i, levels[i]);
  set_recmask(SOUND_MASK_MIC);
}

void
sb_mixer_init(int major_model)
{
	sb_setmixer(0x00, 0);	/* Reset mixer */

	if (!detect_mixer()) return;	/* No mixer. Why? */

	mixer_initialized = 1;
	mixer_model = major_model;

        switch (major_model)
        {
        case 3:
	  mixer_caps = SOUND_CAP_EXCL_INPUT;
	  supported_devices = SBPRO_MIXER_DEVICES;
	  supported_rec_devices = SBPRO_RECORDING_DEVICES;
	  iomap = &sbpro_mix;
	  break;

	case 4:
	  mixer_caps = 0;
	  supported_devices = SB16_MIXER_DEVICES;
	  supported_rec_devices = SB16_RECORDING_DEVICES;
	  iomap = &sb16_mix;
	  break;

	default:
	  printk("SB Warning: Unsupported mixer type\n");
	  return;
	}

        mixer_devs[num_mixers++] = &sb_mixer_operations;
        sb_mixer_reset();
}

#endif
前天看到了小兔,昨天是小鹿,今天是你