4 * The low level mixer driver for the SoundBlaster Pro and SB16 cards.
6 * Copyright by Hannu Savolainen 1994
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 2.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Modified: Hunyue Yau Jan 6 1994 Added code to support the Sound Galaxy
33 #include <i386/isa/sound/sound_config.h>
35 #if (NSB > 0) && defined(CONFIG_SBPRO)
36 #define __SB_MIXER_C__
38 #include <i386/isa/sound/sbcard.h>
39 #include <i386/isa/sound/sb_mixer.h>
43 extern int Jazz16_detected;
44 extern sound_os_info *sb_osp;
46 static int mixer_initialized = 0;
48 static int supported_rec_devices;
49 static int supported_devices;
50 static int recmask = 0;
51 static int mixer_model;
52 static int mixer_caps;
53 static mixer_tab *iomap;
56 sb_setmixer(u_int port, u_int value)
60 flags = splhigh(); /* XXX ouch... */
61 outb(MIXER_ADDR, (u_char) (port & 0xff)); /* Select register */
63 outb(MIXER_DATA, (u_char) (value & 0xff));
69 sb_getmixer(u_int port)
75 outb(MIXER_ADDR, (u_char) (port & 0xff)); /* Select register */
77 val = inb(MIXER_DATA);
85 sb_mixer_set_stereo(int mode)
87 if (!mixer_initialized)
90 sb_setmixer(OUT_FILTER, ((sb_getmixer(OUT_FILTER) & ~STEREO_DAC)
91 | (mode ? STEREO_DAC : MONO_DAC)));
96 * 0 No mixer detected.
97 * 1 Only a plain Sound Blaster Pro style mixer detected.
98 * 2 The Sound Galaxy NX Pro mixer detected.
105 int oldbass, oldtreble;
106 extern int sbc_major;
111 * Detect the mixer by changing parameters of two volume channels. If
112 * the values read back match with the values written, the mixer is
115 sb_setmixer(FM_VOL, 0xff);
116 sb_setmixer(VOC_VOL, 0x33);
118 if (sb_getmixer(FM_VOL) != 0xff)
119 return 0; /* No match */
120 if (sb_getmixer(VOC_VOL) != 0x33)
125 * Attempt to detect the SG NX Pro by check for valid bass/treble
128 oldbass = sb_getmixer(BASS_LVL);
129 oldtreble = sb_getmixer(TREBLE_LVL);
131 sb_setmixer(BASS_LVL, 0xaa);
132 sb_setmixer(TREBLE_LVL, 0x55);
134 if ((sb_getmixer(BASS_LVL) != 0xaa) ||
135 (sb_getmixer(TREBLE_LVL) != 0x55)) {
136 retcode = 1; /* 1 == Only SB Pro detected */
138 retcode = 2; /* 2 == SG NX Pro detected */
140 * Restore register in either case since SG NX Pro has EEPROM with
141 * 'preferred' values stored.
143 sb_setmixer(BASS_LVL, oldbass);
144 sb_setmixer(TREBLE_LVL, oldtreble);
147 * If the SB version is 3.X (SB Pro), assume we have a SG NX Pro 16.
148 * In this case it's good idea to disable the Disney Sound Source
149 * compatibility mode. It's useless and just causes noise every time
150 * the LPT-port is accessed.
152 * Also place the card into WSS mode.
154 if (sbc_major == 3) {
155 outb(sbc_base + 0x1c, 0x01);
156 outb(sbc_base + 0x1a, 0x00);
163 change_bits(u_char *regval, int dev, int chn, int newval)
168 mask = (1 << (*iomap)[dev][chn].nbits) - 1;
169 newval = (int) ((newval * mask) + 50) / 100; /* Scale it */
171 shift = (*iomap)[dev][chn].bitoffs - (*iomap)[dev][LEFT_CHN].nbits + 1;
173 *regval &= ~(mask << shift); /* Filter out the previous value */
174 *regval |= (newval & mask) << shift; /* Set the new value */
178 sb_mixer_get(int dev)
180 if (!((1 << dev) & supported_devices))
187 static char smw_mix_regs[] =/* Left mixer registers */
189 0x0b, /* SOUND_MIXER_VOLUME */
190 0x0d, /* SOUND_MIXER_BASS */
191 0x0d, /* SOUND_MIXER_TREBLE */
192 0x05, /* SOUND_MIXER_SYNTH */
193 0x09, /* SOUND_MIXER_PCM */
194 0x00, /* SOUND_MIXER_SPEAKER */
195 0x03, /* SOUND_MIXER_LINE */
196 0x01, /* SOUND_MIXER_MIC */
197 0x07, /* SOUND_MIXER_CD */
198 0x00, /* SOUND_MIXER_IMIX */
199 0x00, /* SOUND_MIXER_ALTPCM */
200 0x00, /* SOUND_MIXER_RECLEV */
201 0x00, /* SOUND_MIXER_IGAIN */
202 0x00, /* SOUND_MIXER_OGAIN */
203 0x00, /* SOUND_MIXER_LINE1 */
204 0x00, /* SOUND_MIXER_LINE2 */
205 0x00 /* SOUND_MIXER_LINE3 */
213 sb_setmixer(0x00, 0x18);/* Mute unused (Telephone) line */
214 sb_setmixer(0x10, 0x38);/* Config register 2 */
216 supported_devices = 0;
217 for (i = 0; i < sizeof(smw_mix_regs); i++)
218 if (smw_mix_regs[i] != 0)
219 supported_devices |= (1 << i);
221 supported_rec_devices = supported_devices &
222 ~(SOUND_MASK_BASS | SOUND_MASK_TREBLE | SOUND_MASK_PCM |
227 smw_mixer_set(int dev, int value)
229 int left = value & 0x000000ff;
230 int right = (value & 0x0000ff00) >> 8;
241 if (!(supported_devices & (1 << dev))) /* Not supported */
245 case SOUND_MIXER_VOLUME:
246 sb_setmixer(0x0b, 96 - (96 * left / 100)); /* 96=mute, 0=max */
247 sb_setmixer(0x0c, 96 - (96 * right / 100));
250 case SOUND_MIXER_BASS:
251 case SOUND_MIXER_TREBLE:
252 levels[dev] = left | (right << 8);
254 /* Set left bass and treble values */
255 val = ((levels[SOUND_MIXER_TREBLE] & 0xff) * 16 / 100) << 4;
256 val |= ((levels[SOUND_MIXER_BASS] & 0xff) * 16 / 100) & 0x0f;
257 sb_setmixer(0x0d, val);
259 /* Set right bass and treble values */
260 val = (((levels[SOUND_MIXER_TREBLE] >> 8) & 0xff) * 16 / 100) << 4;
261 val |= (((levels[SOUND_MIXER_BASS] >> 8) & 0xff) * 16 / 100) & 0x0f;
262 sb_setmixer(0x0e, val);
266 reg = smw_mix_regs[dev];
269 sb_setmixer(reg, (24 - (24 * left / 100)) | 0x20); /* 24=mute, 0=max */
270 sb_setmixer(reg + 1, (24 - (24 * right / 100)) | 0x40);
273 levels[dev] = left | (right << 8);
274 return left | (right << 8);
280 sb_mixer_set(int dev, int value)
282 int left = value & 0x000000ff;
283 int right = (value & 0x0000ff00) >> 8;
289 if (Jazz16_detected == 2)
290 return smw_mixer_set(dev, value);
301 if (!(supported_devices & (1 << dev))) /* Not supported */
304 regoffs = (*iomap)[dev][LEFT_CHN].regno;
309 val = sb_getmixer(regoffs);
310 change_bits(&val, dev, LEFT_CHN, left);
312 levels[dev] = left | (left << 8);
314 if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) { /* Change register */
315 sb_setmixer(regoffs, val); /* Save the old one */
316 regoffs = (*iomap)[dev][RIGHT_CHN].regno;
319 return left | (left << 8); /* Just left channel present */
321 val = sb_getmixer(regoffs); /* Read the new one */
323 change_bits(&val, dev, RIGHT_CHN, right);
325 sb_setmixer(regoffs, val);
327 levels[dev] = left | (right << 8);
328 return left | (right << 8);
334 sb_setmixer(RECORD_SRC, (sb_getmixer(RECORD_SRC) & ~7) | (src & 0x7));
338 set_recmask(int mask)
341 u_char regimageL, regimageR;
343 devmask = mask & supported_rec_devices;
345 switch (mixer_model) {
348 if (devmask != SOUND_MASK_MIC && devmask != SOUND_MASK_LINE &&
349 devmask != SOUND_MASK_CD) {
351 * More than one devices selected. Drop the previous
356 if (devmask != SOUND_MASK_MIC && devmask != SOUND_MASK_LINE &&
357 devmask != SOUND_MASK_CD) {
358 /* More than one devices selected. Default to mic */
359 devmask = SOUND_MASK_MIC;
361 if (devmask ^ recmask) { /* Input source changed */
368 case SOUND_MASK_LINE:
369 set_recsrc(SRC_LINE);
384 devmask = SOUND_MASK_MIC;
386 regimageL = regimageR = 0;
387 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
388 if ((1 << i) & devmask) {
389 regimageL |= sb16_recmasks_L[i];
390 regimageR |= sb16_recmasks_R[i];
392 sb_setmixer(SB16_IMASK_L, regimageL);
393 sb_setmixer(SB16_IMASK_R, regimageR);
402 sb_mixer_ioctl(int dev, u_int cmd, ioctl_arg arg)
404 if (((cmd >> 8) & 0xff) == 'M') {
406 switch (cmd & 0xff) {
407 case SOUND_MIXER_RECSRC:
408 return *(int *) arg = set_recmask((*(int *) arg));
412 return *(int *) arg = sb_mixer_set(cmd & 0xff, (*(int *) arg));
415 switch (cmd & 0xff) { /* Return parameters */
417 case SOUND_MIXER_RECSRC:
418 return *(int *) arg = recmask;
421 case SOUND_MIXER_DEVMASK:
422 return *(int *) arg = supported_devices;
425 case SOUND_MIXER_STEREODEVS:
427 return *(int *) arg = supported_devices;
429 return *(int *) arg = supported_devices & ~(SOUND_MASK_MIC | SOUND_MASK_SPEAKER);
432 case SOUND_MIXER_RECMASK:
433 return *(int *) arg = supported_rec_devices;
436 case SOUND_MIXER_CAPS:
437 return *(int *) arg = mixer_caps;
441 return *(int *) arg = sb_mixer_get(cmd & 0xff);
447 static struct mixer_operations sb_mixer_operations =
458 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
459 sb_mixer_set(i, levels[i]);
460 set_recmask(SOUND_MASK_MIC);
464 * Returns a code depending on whether a SG NX Pro was detected. 1 == Plain
465 * SB Pro 2 == SG NX Pro detected. 3 == SB16
467 * Used to update message.
470 sb_mixer_init(int major_model)
474 sb_setmixer(0x00, 0); /* Reset mixer */
476 if (!(mixer_type = detect_mixer()))
477 return 0; /* No mixer. Why? */
479 mixer_initialized = 1;
480 mixer_model = major_model;
482 switch (major_model) {
484 mixer_caps = SOUND_CAP_EXCL_INPUT;
487 if (Jazz16_detected == 2) { /* SM Wave */
488 supported_devices = 0;
489 supported_rec_devices = 0;
496 if (mixer_type == 2) { /* A SGNXPRO was detected */
497 supported_devices = SGNXPRO_MIXER_DEVICES;
498 supported_rec_devices = SGNXPRO_RECORDING_DEVICES;
499 iomap = &sgnxpro_mix;
503 supported_devices = SBPRO_MIXER_DEVICES;
504 supported_rec_devices = SBPRO_RECORDING_DEVICES;
512 supported_devices = SB16_MIXER_DEVICES;
513 supported_rec_devices = SB16_RECORDING_DEVICES;
519 printf("SB Warning: Unsupported mixer type\n");
523 if (num_mixers < MAX_MIXER_DEV)
524 mixer_devs[num_mixers++] = &sb_mixer_operations;