Initial import from FreeBSD RELENG_4:
[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  */
32
33 #include <i386/isa/sound/sound_config.h>
34
35 #if (NSB > 0) && defined(CONFIG_SBPRO)
36 #define __SB_MIXER_C__
37
38 #include <i386/isa/sound/sbcard.h>
39 #include <i386/isa/sound/sb_mixer.h>
40 #undef SB_TEST_IRQ
41
42 extern int      sbc_base;
43 extern int      Jazz16_detected;
44 extern sound_os_info *sb_osp;
45
46 static int      mixer_initialized = 0;
47
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;
54
55 void
56 sb_setmixer(u_int port, u_int value)
57 {
58     u_long   flags;
59
60     flags = splhigh(); /* XXX ouch... */
61     outb(MIXER_ADDR, (u_char) (port & 0xff));   /* Select register */
62     DELAY(10);
63     outb(MIXER_DATA, (u_char) (value & 0xff));
64     DELAY(10);
65     splx(flags);
66 }
67
68 int
69 sb_getmixer(u_int port)
70 {
71     int             val;
72     u_long   flags;
73
74     flags = splhigh();
75     outb(MIXER_ADDR, (u_char) (port & 0xff));   /* Select register */
76     DELAY(10);
77     val = inb(MIXER_DATA);
78     DELAY(10);
79     splx(flags);
80
81     return val;
82 }
83
84 void
85 sb_mixer_set_stereo(int mode)
86 {
87     if (!mixer_initialized)
88         return;
89
90     sb_setmixer(OUT_FILTER, ((sb_getmixer(OUT_FILTER) & ~STEREO_DAC)
91                              | (mode ? STEREO_DAC : MONO_DAC)));
92 }
93
94 /*
95  * Returns:
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.
99  */
100 static int
101 detect_mixer(void)
102 {
103
104 #ifdef __SGNXPRO__
105     int             oldbass, oldtreble;
106     extern int      sbc_major;
107 #endif
108     int             retcode = 1;
109
110     /*
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
113      * there (is it?)
114      */
115     sb_setmixer(FM_VOL, 0xff);
116     sb_setmixer(VOC_VOL, 0x33);
117
118     if (sb_getmixer(FM_VOL) != 0xff)
119         return 0;       /* No match */
120     if (sb_getmixer(VOC_VOL) != 0x33)
121         return 0;
122
123 #ifdef __SGNXPRO__
124     /*
125      * Attempt to detect the SG NX Pro by check for valid bass/treble
126      * registers.
127      */
128     oldbass = sb_getmixer(BASS_LVL);
129     oldtreble = sb_getmixer(TREBLE_LVL);
130
131     sb_setmixer(BASS_LVL, 0xaa);
132     sb_setmixer(TREBLE_LVL, 0x55);
133
134     if ((sb_getmixer(BASS_LVL) != 0xaa) ||
135             (sb_getmixer(TREBLE_LVL) != 0x55)) {
136         retcode = 1;    /* 1 == Only SB Pro detected */
137     } else
138         retcode = 2;    /* 2 == SG NX Pro detected */
139     /*
140      * Restore register in either case since SG NX Pro has EEPROM with
141      * 'preferred' values stored.
142      */
143     sb_setmixer(BASS_LVL, oldbass);
144     sb_setmixer(TREBLE_LVL, oldtreble);
145
146     /*
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.
151      * 
152      * Also place the card into WSS mode.
153      */
154     if (sbc_major == 3) {
155         outb(sbc_base + 0x1c, 0x01);
156         outb(sbc_base + 0x1a, 0x00);
157     }
158 #endif
159     return retcode;
160 }
161
162 static void
163 change_bits(u_char *regval, int dev, int chn, int newval)
164 {
165     u_char   mask;
166     int      shift;
167
168     mask = (1 << (*iomap)[dev][chn].nbits) - 1;
169     newval = (int) ((newval * mask) + 50) / 100;        /* Scale it */
170
171     shift = (*iomap)[dev][chn].bitoffs - (*iomap)[dev][LEFT_CHN].nbits + 1;
172
173     *regval &= ~(mask << shift);        /* Filter out the previous value */
174     *regval |= (newval & mask) << shift;        /* Set the new value */
175 }
176
177 static int
178 sb_mixer_get(int dev)
179 {
180     if (!((1 << dev) & supported_devices))
181         return -(EINVAL);
182
183     return levels[dev];
184 }
185
186 #ifdef JAZZ16
187 static char     smw_mix_regs[] =/* Left mixer registers */
188 {
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 */
206 };
207
208 static void
209 smw_mixer_init(void)
210 {
211     int             i;
212
213     sb_setmixer(0x00, 0x18);/* Mute unused (Telephone) line */
214     sb_setmixer(0x10, 0x38);/* Config register 2 */
215
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);
220
221     supported_rec_devices = supported_devices &
222         ~(SOUND_MASK_BASS | SOUND_MASK_TREBLE | SOUND_MASK_PCM |
223           SOUND_MASK_VOLUME);
224 }
225
226 static int
227 smw_mixer_set(int dev, int value)
228 {
229     int             left = value & 0x000000ff;
230     int             right = (value & 0x0000ff00) >> 8;
231     int             reg, val;
232
233     if (left > 100)
234         left = 100;
235     if (right > 100)
236         right = 100;
237
238     if (dev > 31)
239         return -(EINVAL);
240
241     if (!(supported_devices & (1 << dev)))      /* Not supported */
242         return -(EINVAL);
243
244     switch (dev) {
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));
248         break;
249
250     case SOUND_MIXER_BASS:
251     case SOUND_MIXER_TREBLE:
252         levels[dev] = left | (right << 8);
253
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);
258
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);
263         break;
264
265     default:
266         reg = smw_mix_regs[dev];
267         if (reg == 0)
268             return -(EINVAL);
269         sb_setmixer(reg, (24 - (24 * left / 100)) | 0x20); /* 24=mute, 0=max */
270         sb_setmixer(reg + 1, (24 - (24 * right / 100)) | 0x40);
271     }
272
273     levels[dev] = left | (right << 8);
274     return left | (right << 8);
275 }
276
277 #endif
278
279 static int
280 sb_mixer_set(int dev, int value)
281 {
282     int             left = value & 0x000000ff;
283     int             right = (value & 0x0000ff00) >> 8;
284
285     int             regoffs;
286     u_char   val;
287
288 #ifdef JAZZ16
289     if (Jazz16_detected == 2)
290         return smw_mixer_set(dev, value);
291 #endif
292
293     if (left > 100)
294         left = 100;
295     if (right > 100)
296         right = 100;
297
298     if (dev > 31)
299         return -(EINVAL);
300
301     if (!(supported_devices & (1 << dev)))      /* Not supported */
302         return -(EINVAL);
303
304     regoffs = (*iomap)[dev][LEFT_CHN].regno;
305
306     if (regoffs == 0)
307         return -(EINVAL);
308
309     val = sb_getmixer(regoffs);
310     change_bits(&val, dev, LEFT_CHN, left);
311
312     levels[dev] = left | (left << 8);
313
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;
317
318         if (regoffs == 0)
319             return left | (left << 8);  /* Just left channel present */
320
321         val = sb_getmixer(regoffs);     /* Read the new one */
322     }
323     change_bits(&val, dev, RIGHT_CHN, right);
324
325     sb_setmixer(regoffs, val);
326
327     levels[dev] = left | (right << 8);
328     return left | (right << 8);
329 }
330
331 static void
332 set_recsrc(int src)
333 {
334     sb_setmixer(RECORD_SRC, (sb_getmixer(RECORD_SRC) & ~7) | (src & 0x7));
335 }
336
337 static int
338 set_recmask(int mask)
339 {
340     int             devmask, i;
341     u_char   regimageL, regimageR;
342
343     devmask = mask & supported_rec_devices;
344
345     switch (mixer_model) {
346     case 3:
347
348         if (devmask != SOUND_MASK_MIC && devmask != SOUND_MASK_LINE &&
349                     devmask != SOUND_MASK_CD) {
350             /*
351              * More than one devices selected. Drop the previous
352              * selection
353              */
354             devmask &= ~recmask;
355         }
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;
360         }
361         if (devmask ^ recmask) {        /* Input source changed */
362             switch (devmask) {
363
364             case SOUND_MASK_MIC:
365                 set_recsrc(SRC_MIC);
366                 break;
367
368             case SOUND_MASK_LINE:
369                 set_recsrc(SRC_LINE);
370                 break;
371
372             case SOUND_MASK_CD:
373                 set_recsrc(SRC_CD);
374                 break;
375
376             default:
377                 set_recsrc(SRC_MIC);
378             }
379         }
380         break;
381
382     case 4:
383         if (!devmask)
384             devmask = SOUND_MASK_MIC;
385
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];
391             }
392         sb_setmixer(SB16_IMASK_L, regimageL);
393         sb_setmixer(SB16_IMASK_R, regimageR);
394         break;
395     }
396
397     recmask = devmask;
398     return recmask;
399 }
400
401 static int
402 sb_mixer_ioctl(int dev, u_int cmd, ioctl_arg arg)
403 {
404     if (((cmd >> 8) & 0xff) == 'M') {
405         if (cmd & IOC_IN)
406             switch (cmd & 0xff) {
407             case SOUND_MIXER_RECSRC:
408                 return *(int *) arg = set_recmask((*(int *) arg));
409                 break;
410
411             default:
412                 return *(int *) arg = sb_mixer_set(cmd & 0xff, (*(int *) arg));
413             }
414         else
415             switch (cmd & 0xff) {       /* Return parameters */
416
417             case SOUND_MIXER_RECSRC:
418                 return *(int *) arg = recmask;
419                 break;
420
421             case SOUND_MIXER_DEVMASK:
422                 return *(int *) arg = supported_devices;
423                 break;
424
425             case SOUND_MIXER_STEREODEVS:
426                 if (Jazz16_detected)
427                     return *(int *) arg = supported_devices;
428                 else
429                     return *(int *) arg = supported_devices & ~(SOUND_MASK_MIC | SOUND_MASK_SPEAKER);
430                 break;
431
432             case SOUND_MIXER_RECMASK:
433                 return *(int *) arg = supported_rec_devices;
434                 break;
435
436             case SOUND_MIXER_CAPS:
437                 return *(int *) arg = mixer_caps;
438                 break;
439
440             default:
441                 return *(int *) arg = sb_mixer_get(cmd & 0xff);
442             }
443     } else
444         return -(EINVAL);
445 }
446
447 static struct mixer_operations sb_mixer_operations =
448 {
449         "SoundBlaster",
450         sb_mixer_ioctl
451 };
452
453 static void
454 sb_mixer_reset(void)
455 {
456     int             i;
457
458     for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
459         sb_mixer_set(i, levels[i]);
460     set_recmask(SOUND_MASK_MIC);
461 }
462
463 /*
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
466  * 
467  * Used to update message.
468  */
469 int
470 sb_mixer_init(int major_model)
471 {
472     int             mixer_type = 0;
473
474     sb_setmixer(0x00, 0);       /* Reset mixer */
475
476     if (!(mixer_type = detect_mixer()))
477         return 0;       /* No mixer. Why? */
478
479     mixer_initialized = 1;
480     mixer_model = major_model;
481
482     switch (major_model) {
483     case 3:
484         mixer_caps = SOUND_CAP_EXCL_INPUT;
485
486 #ifdef JAZZ16
487         if (Jazz16_detected == 2) {     /* SM Wave */
488             supported_devices = 0;
489             supported_rec_devices = 0;
490             iomap = &sbpro_mix;
491             smw_mixer_init();
492             mixer_type = 1;
493         } else
494 #endif
495 #ifdef __SGNXPRO__
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;
500         } else
501 #endif
502         {
503             supported_devices = SBPRO_MIXER_DEVICES;
504             supported_rec_devices = SBPRO_RECORDING_DEVICES;
505             iomap = &sbpro_mix;
506             mixer_type = 1;
507         }
508         break;
509
510     case 4:
511         mixer_caps = 0;
512         supported_devices = SB16_MIXER_DEVICES;
513         supported_rec_devices = SB16_RECORDING_DEVICES;
514         iomap = &sb16_mix;
515         mixer_type = 3;
516         break;
517
518     default:
519         printf("SB Warning: Unsupported mixer type\n");
520         return 0;
521         }
522
523         if (num_mixers < MAX_MIXER_DEV)
524                 mixer_devs[num_mixers++] = &sb_mixer_operations;
525         sb_mixer_reset();
526         return mixer_type;
527 }
528
529 #endif