kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / sound / isa / i386 / sb / sb_mixer.c
1 /*
2  * sound/sb_mixer.c
3  * 
4  * The low level mixer driver for the SoundBlaster Pro and SB16 cards.
5  * 
6  * Copyright by Hannu Savolainen 1994
7  * 
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.
15  * 
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
26  * SUCH DAMAGE.
27  * 
28  * Modified: Hunyue Yau      Jan 6 1994 Added code to support the Sound Galaxy
29  * NX Pro mixer.
30  * 
31  * $DragonFly: src/sys/dev/sound/isa/i386/sb/Attic/sb_mixer.c,v 1.2 2003/08/07 21:17:12 dillon Exp $
32  */
33
34 #include <i386/isa/sound/sound_config.h>
35
36 #if (NSB > 0) && defined(CONFIG_SBPRO)
37 #define __SB_MIXER_C__
38
39 #include <i386/isa/sound/sbcard.h>
40 #include "sb_mixer.h"
41 #undef SB_TEST_IRQ
42
43 extern int      sbc_base;
44 extern int      Jazz16_detected;
45 extern sound_os_info *sb_osp;
46
47 static int      mixer_initialized = 0;
48
49 static int      supported_rec_devices;
50 static int      supported_devices;
51 static int      recmask = 0;
52 static int      mixer_model;
53 static int      mixer_caps;
54 static mixer_tab *iomap;
55
56 void
57 sb_setmixer(u_int port, u_int value)
58 {
59     u_long   flags;
60
61     flags = splhigh(); /* XXX ouch... */
62     outb(MIXER_ADDR, (u_char) (port & 0xff));   /* Select register */
63     DELAY(10);
64     outb(MIXER_DATA, (u_char) (value & 0xff));
65     DELAY(10);
66     splx(flags);
67 }
68
69 int
70 sb_getmixer(u_int port)
71 {
72     int             val;
73     u_long   flags;
74
75     flags = splhigh();
76     outb(MIXER_ADDR, (u_char) (port & 0xff));   /* Select register */
77     DELAY(10);
78     val = inb(MIXER_DATA);
79     DELAY(10);
80     splx(flags);
81
82     return val;
83 }
84
85 void
86 sb_mixer_set_stereo(int mode)
87 {
88     if (!mixer_initialized)
89         return;
90
91     sb_setmixer(OUT_FILTER, ((sb_getmixer(OUT_FILTER) & ~STEREO_DAC)
92                              | (mode ? STEREO_DAC : MONO_DAC)));
93 }
94
95 /*
96  * Returns:
97  *      0       No mixer detected.
98  *      1       Only a plain Sound Blaster Pro style mixer detected.
99  *      2       The Sound Galaxy NX Pro mixer detected.
100  */
101 static int
102 detect_mixer(void)
103 {
104
105 #ifdef __SGNXPRO__
106     int             oldbass, oldtreble;
107     extern int      sbc_major;
108 #endif
109     int             retcode = 1;
110
111     /*
112      * Detect the mixer by changing parameters of two volume channels. If
113      * the values read back match with the values written, the mixer is
114      * there (is it?)
115      */
116     sb_setmixer(FM_VOL, 0xff);
117     sb_setmixer(VOC_VOL, 0x33);
118
119     if (sb_getmixer(FM_VOL) != 0xff)
120         return 0;       /* No match */
121     if (sb_getmixer(VOC_VOL) != 0x33)
122         return 0;
123
124 #ifdef __SGNXPRO__
125     /*
126      * Attempt to detect the SG NX Pro by check for valid bass/treble
127      * registers.
128      */
129     oldbass = sb_getmixer(BASS_LVL);
130     oldtreble = sb_getmixer(TREBLE_LVL);
131
132     sb_setmixer(BASS_LVL, 0xaa);
133     sb_setmixer(TREBLE_LVL, 0x55);
134
135     if ((sb_getmixer(BASS_LVL) != 0xaa) ||
136             (sb_getmixer(TREBLE_LVL) != 0x55)) {
137         retcode = 1;    /* 1 == Only SB Pro detected */
138     } else
139         retcode = 2;    /* 2 == SG NX Pro detected */
140     /*
141      * Restore register in either case since SG NX Pro has EEPROM with
142      * 'preferred' values stored.
143      */
144     sb_setmixer(BASS_LVL, oldbass);
145     sb_setmixer(TREBLE_LVL, oldtreble);
146
147     /*
148      * If the SB version is 3.X (SB Pro), assume we have a SG NX Pro 16.
149      * In this case it's good idea to disable the Disney Sound Source
150      * compatibility mode. It's useless and just causes noise every time
151      * the LPT-port is accessed.
152      * 
153      * Also place the card into WSS mode.
154      */
155     if (sbc_major == 3) {
156         outb(sbc_base + 0x1c, 0x01);
157         outb(sbc_base + 0x1a, 0x00);
158     }
159 #endif
160     return retcode;
161 }
162
163 static void
164 change_bits(u_char *regval, int dev, int chn, int newval)
165 {
166     u_char   mask;
167     int      shift;
168
169     mask = (1 << (*iomap)[dev][chn].nbits) - 1;
170     newval = (int) ((newval * mask) + 50) / 100;        /* Scale it */
171
172     shift = (*iomap)[dev][chn].bitoffs - (*iomap)[dev][LEFT_CHN].nbits + 1;
173
174     *regval &= ~(mask << shift);        /* Filter out the previous value */
175     *regval |= (newval & mask) << shift;        /* Set the new value */
176 }
177
178 static int
179 sb_mixer_get(int dev)
180 {
181     if (!((1 << dev) & supported_devices))
182         return -(EINVAL);
183
184     return levels[dev];
185 }
186
187 #ifdef JAZZ16
188 static char     smw_mix_regs[] =/* Left mixer registers */
189 {
190         0x0b,                   /* SOUND_MIXER_VOLUME */
191         0x0d,                   /* SOUND_MIXER_BASS */
192         0x0d,                   /* SOUND_MIXER_TREBLE */
193         0x05,                   /* SOUND_MIXER_SYNTH */
194         0x09,                   /* SOUND_MIXER_PCM */
195         0x00,                   /* SOUND_MIXER_SPEAKER */
196         0x03,                   /* SOUND_MIXER_LINE */
197         0x01,                   /* SOUND_MIXER_MIC */
198         0x07,                   /* SOUND_MIXER_CD */
199         0x00,                   /* SOUND_MIXER_IMIX */
200         0x00,                   /* SOUND_MIXER_ALTPCM */
201         0x00,                   /* SOUND_MIXER_RECLEV */
202         0x00,                   /* SOUND_MIXER_IGAIN */
203         0x00,                   /* SOUND_MIXER_OGAIN */
204         0x00,                   /* SOUND_MIXER_LINE1 */
205         0x00,                   /* SOUND_MIXER_LINE2 */
206         0x00                    /* SOUND_MIXER_LINE3 */
207 };
208
209 static void
210 smw_mixer_init(void)
211 {
212     int             i;
213
214     sb_setmixer(0x00, 0x18);/* Mute unused (Telephone) line */
215     sb_setmixer(0x10, 0x38);/* Config register 2 */
216
217     supported_devices = 0;
218     for (i = 0; i < sizeof(smw_mix_regs); i++)
219         if (smw_mix_regs[i] != 0)
220             supported_devices |= (1 << i);
221
222     supported_rec_devices = supported_devices &
223         ~(SOUND_MASK_BASS | SOUND_MASK_TREBLE | SOUND_MASK_PCM |
224           SOUND_MASK_VOLUME);
225 }
226
227 static int
228 smw_mixer_set(int dev, int value)
229 {
230     int             left = value & 0x000000ff;
231     int             right = (value & 0x0000ff00) >> 8;
232     int             reg, val;
233
234     if (left > 100)
235         left = 100;
236     if (right > 100)
237         right = 100;
238
239     if (dev > 31)
240         return -(EINVAL);
241
242     if (!(supported_devices & (1 << dev)))      /* Not supported */
243         return -(EINVAL);
244
245     switch (dev) {
246     case SOUND_MIXER_VOLUME:
247         sb_setmixer(0x0b, 96 - (96 * left / 100));      /* 96=mute, 0=max */
248         sb_setmixer(0x0c, 96 - (96 * right / 100));
249         break;
250
251     case SOUND_MIXER_BASS:
252     case SOUND_MIXER_TREBLE:
253         levels[dev] = left | (right << 8);
254
255         /* Set left bass and treble values */
256         val = ((levels[SOUND_MIXER_TREBLE] & 0xff) * 16 / 100) << 4;
257         val |= ((levels[SOUND_MIXER_BASS] & 0xff) * 16 / 100) & 0x0f;
258         sb_setmixer(0x0d, val);
259
260         /* Set right bass and treble values */
261         val = (((levels[SOUND_MIXER_TREBLE] >> 8) & 0xff) * 16 / 100) << 4;
262         val |= (((levels[SOUND_MIXER_BASS] >> 8) & 0xff) * 16 / 100) & 0x0f;
263         sb_setmixer(0x0e, val);
264         break;
265
266     default:
267         reg = smw_mix_regs[dev];
268         if (reg == 0)
269             return -(EINVAL);
270         sb_setmixer(reg, (24 - (24 * left / 100)) | 0x20); /* 24=mute, 0=max */
271         sb_setmixer(reg + 1, (24 - (24 * right / 100)) | 0x40);
272     }
273
274     levels[dev] = left | (right << 8);
275     return left | (right << 8);
276 }
277
278 #endif
279
280 static int
281 sb_mixer_set(int dev, int value)
282 {
283     int             left = value & 0x000000ff;
284     int             right = (value & 0x0000ff00) >> 8;
285
286     int             regoffs;
287     u_char   val;
288
289 #ifdef JAZZ16
290     if (Jazz16_detected == 2)
291         return smw_mixer_set(dev, value);
292 #endif
293
294     if (left > 100)
295         left = 100;
296     if (right > 100)
297         right = 100;
298
299     if (dev > 31)
300         return -(EINVAL);
301
302     if (!(supported_devices & (1 << dev)))      /* Not supported */
303         return -(EINVAL);
304
305     regoffs = (*iomap)[dev][LEFT_CHN].regno;
306
307     if (regoffs == 0)
308         return -(EINVAL);
309
310     val = sb_getmixer(regoffs);
311     change_bits(&val, dev, LEFT_CHN, left);
312
313     levels[dev] = left | (left << 8);
314
315     if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) {    /* Change register */
316         sb_setmixer(regoffs, val);      /* Save the old one */
317         regoffs = (*iomap)[dev][RIGHT_CHN].regno;
318
319         if (regoffs == 0)
320             return left | (left << 8);  /* Just left channel present */
321
322         val = sb_getmixer(regoffs);     /* Read the new one */
323     }
324     change_bits(&val, dev, RIGHT_CHN, right);
325
326     sb_setmixer(regoffs, val);
327
328     levels[dev] = left | (right << 8);
329     return left | (right << 8);
330 }
331
332 static void
333 set_recsrc(int src)
334 {
335     sb_setmixer(RECORD_SRC, (sb_getmixer(RECORD_SRC) & ~7) | (src & 0x7));
336 }
337
338 static int
339 set_recmask(int mask)
340 {
341     int             devmask, i;
342     u_char   regimageL, regimageR;
343
344     devmask = mask & supported_rec_devices;
345
346     switch (mixer_model) {
347     case 3:
348
349         if (devmask != SOUND_MASK_MIC && devmask != SOUND_MASK_LINE &&
350                     devmask != SOUND_MASK_CD) {
351             /*
352              * More than one devices selected. Drop the previous
353              * selection
354              */
355             devmask &= ~recmask;
356         }
357         if (devmask != SOUND_MASK_MIC && devmask != SOUND_MASK_LINE &&
358                     devmask != SOUND_MASK_CD) {
359             /* More than one devices selected. Default to mic */
360             devmask = SOUND_MASK_MIC;
361         }
362         if (devmask ^ recmask) {        /* Input source changed */
363             switch (devmask) {
364
365             case SOUND_MASK_MIC:
366                 set_recsrc(SRC_MIC);
367                 break;
368
369             case SOUND_MASK_LINE:
370                 set_recsrc(SRC_LINE);
371                 break;
372
373             case SOUND_MASK_CD:
374                 set_recsrc(SRC_CD);
375                 break;
376
377             default:
378                 set_recsrc(SRC_MIC);
379             }
380         }
381         break;
382
383     case 4:
384         if (!devmask)
385             devmask = SOUND_MASK_MIC;
386
387         regimageL = regimageR = 0;
388         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
389             if ((1 << i) & devmask) {
390                 regimageL |= sb16_recmasks_L[i];
391                 regimageR |= sb16_recmasks_R[i];
392             }
393         sb_setmixer(SB16_IMASK_L, regimageL);
394         sb_setmixer(SB16_IMASK_R, regimageR);
395         break;
396     }
397
398     recmask = devmask;
399     return recmask;
400 }
401
402 static int
403 sb_mixer_ioctl(int dev, u_int cmd, ioctl_arg arg)
404 {
405     if (((cmd >> 8) & 0xff) == 'M') {
406         if (cmd & IOC_IN)
407             switch (cmd & 0xff) {
408             case SOUND_MIXER_RECSRC:
409                 return *(int *) arg = set_recmask((*(int *) arg));
410                 break;
411
412             default:
413                 return *(int *) arg = sb_mixer_set(cmd & 0xff, (*(int *) arg));
414             }
415         else
416             switch (cmd & 0xff) {       /* Return parameters */
417
418             case SOUND_MIXER_RECSRC:
419                 return *(int *) arg = recmask;
420                 break;
421
422             case SOUND_MIXER_DEVMASK:
423                 return *(int *) arg = supported_devices;
424                 break;
425
426             case SOUND_MIXER_STEREODEVS:
427                 if (Jazz16_detected)
428                     return *(int *) arg = supported_devices;
429                 else
430                     return *(int *) arg = supported_devices & ~(SOUND_MASK_MIC | SOUND_MASK_SPEAKER);
431                 break;
432
433             case SOUND_MIXER_RECMASK:
434                 return *(int *) arg = supported_rec_devices;
435                 break;
436
437             case SOUND_MIXER_CAPS:
438                 return *(int *) arg = mixer_caps;
439                 break;
440
441             default:
442                 return *(int *) arg = sb_mixer_get(cmd & 0xff);
443             }
444     } else
445         return -(EINVAL);
446 }
447
448 static struct mixer_operations sb_mixer_operations =
449 {
450         "SoundBlaster",
451         sb_mixer_ioctl
452 };
453
454 static void
455 sb_mixer_reset(void)
456 {
457     int             i;
458
459     for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
460         sb_mixer_set(i, levels[i]);
461     set_recmask(SOUND_MASK_MIC);
462 }
463
464 /*
465  * Returns a code depending on whether a SG NX Pro was detected. 1 == Plain
466  * SB Pro 2 == SG NX Pro detected. 3 == SB16
467  * 
468  * Used to update message.
469  */
470 int
471 sb_mixer_init(int major_model)
472 {
473     int             mixer_type = 0;
474
475     sb_setmixer(0x00, 0);       /* Reset mixer */
476
477     if (!(mixer_type = detect_mixer()))
478         return 0;       /* No mixer. Why? */
479
480     mixer_initialized = 1;
481     mixer_model = major_model;
482
483     switch (major_model) {
484     case 3:
485         mixer_caps = SOUND_CAP_EXCL_INPUT;
486
487 #ifdef JAZZ16
488         if (Jazz16_detected == 2) {     /* SM Wave */
489             supported_devices = 0;
490             supported_rec_devices = 0;
491             iomap = &sbpro_mix;
492             smw_mixer_init();
493             mixer_type = 1;
494         } else
495 #endif
496 #ifdef __SGNXPRO__
497         if (mixer_type == 2) {  /* A SGNXPRO was detected */
498             supported_devices = SGNXPRO_MIXER_DEVICES;
499             supported_rec_devices = SGNXPRO_RECORDING_DEVICES;
500             iomap = &sgnxpro_mix;
501         } else
502 #endif
503         {
504             supported_devices = SBPRO_MIXER_DEVICES;
505             supported_rec_devices = SBPRO_RECORDING_DEVICES;
506             iomap = &sbpro_mix;
507             mixer_type = 1;
508         }
509         break;
510
511     case 4:
512         mixer_caps = 0;
513         supported_devices = SB16_MIXER_DEVICES;
514         supported_rec_devices = SB16_RECORDING_DEVICES;
515         iomap = &sb16_mix;
516         mixer_type = 3;
517         break;
518
519     default:
520         printf("SB Warning: Unsupported mixer type\n");
521         return 0;
522         }
523
524         if (num_mixers < MAX_MIXER_DEV)
525                 mixer_devs[num_mixers++] = &sb_mixer_operations;
526         sb_mixer_reset();
527         return mixer_type;
528 }
529
530 #endif