aboutsummaryrefslogtreecommitdiff
path: root/sound/oss/ics2101.c
blob: 45918df150b39096298c5d8eb542daee5ad1a319 (plain)
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
/*
 * sound/oss/ics2101.c
 *
 * Driver for the ICS2101 mixer of GUS v3.7.
 *
 *
 * Copyright (C) by Hannu Savolainen 1993-1997
 *
 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
 * Version 2 (June 1991). See the "COPYING" file distributed with this software
 * for more info.
 *
 *
 * Thomas Sailer   : ioctl code reworked (vmalloc/vfree removed)
 * Bartlomiej Zolnierkiewicz : added __init to ics2101_mixer_init()
 */
#include <linux/init.h>
#include <linux/spinlock.h>
#include "sound_config.h"

#include <linux/ultrasound.h>

#include "gus.h"
#include "gus_hw.h"

#define MIX_DEVS	(SOUND_MASK_MIC|SOUND_MASK_LINE| \
			 SOUND_MASK_SYNTH| \
  			 SOUND_MASK_CD | SOUND_MASK_VOLUME)

extern int     *gus_osp;
extern int      gus_base;
extern spinlock_t gus_lock;
static int      volumes[ICS_MIXDEVS];
static int      left_fix[ICS_MIXDEVS] =
{1, 1, 1, 2, 1, 2};
static int      right_fix[ICS_MIXDEVS] =
{2, 2, 2, 1, 2, 1};

static int scale_vol(int vol)
{
	/*
	 *  Experimental volume scaling by Risto Kankkunen.
	 *  This should give smoother volume response than just
	 *  a plain multiplication.
	 */
	 
	int e;

	if (vol < 0)
		vol = 0;
	if (vol > 100)
		vol = 100;
	vol = (31 * vol + 50) / 100;
	e = 0;
	if (vol)
	{
		while (vol < 16)
		{
			vol <<= 1;
			e--;
		}
		vol -= 16;
		e += 7;
	}
	return ((e << 4) + vol);
}

static void write_mix(int dev, int chn, int vol)
{
	int *selector;
	unsigned long flags;
	int ctrl_addr = dev << 3;
	int attn_addr = dev << 3;

	vol = scale_vol(vol);

	if (chn == CHN_LEFT)
	{
		selector = left_fix;
		ctrl_addr |= 0x00;
		attn_addr |= 0x02;
	}
	else
	{
		selector = right_fix;
		ctrl_addr |= 0x01;
		attn_addr |= 0x03;
	}

	spin_lock_irqsave(&gus_lock, flags);
	outb((ctrl_addr), u_MixSelect);
	outb((selector[dev]), u_MixData);
	outb((attn_addr), u_MixSelect);
	outb(((unsigned char) vol), u_MixData);
	spin_unlock_irqrestore(&gus_lock,flags);
}

static int set_volumes(int dev, int vol)
{
	int left = vol & 0x00ff;
	int right = (vol >> 8) & 0x00ff;

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

	write_mix(dev, CHN_LEFT, left);
	write_mix(dev, CHN_RIGHT, right);

	vol = left + (right << 8);
	volumes[dev] = vol;
	return vol;
}

static int ics2101_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
{
	int val;
	
	if (((cmd >> 8) & 0xff) == 'M') {
		if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
			
			if (get_user(val, (int __user *)arg))
				return -EFAULT;
			switch (cmd & 0xff) {
			case SOUND_MIXER_RECSRC:
				return gus_default_mixer_ioctl(dev, cmd, arg);

			case SOUND_MIXER_MIC:
				val = set_volumes(DEV_MIC, val);
				break;
				
			case SOUND_MIXER_CD:
				val = set_volumes(DEV_CD, val);
				break;

			case SOUND_MIXER_LINE:
				val = set_volumes(DEV_LINE, val);
				break;

			case SOUND_MIXER_SYNTH:
				val = set_volumes(DEV_GF1, val);
				break;

			case SOUND_MIXER_VOLUME:
				val = set_volumes(DEV_VOL, val);
				break;

			default:
				return -EINVAL;
			}
			return put_user(val, (int __user *)arg);
		} else {
			switch (cmd & 0xff) {
				/*
				 * Return parameters
				 */
			case SOUND_MIXER_RECSRC:
				return gus_default_mixer_ioctl(dev, cmd, arg);

			case SOUND_MIXER_DEVMASK:
				val = MIX_DEVS; 
				break;

			case SOUND_MIXER_STEREODEVS:
				val = SOUND_MASK_LINE | SOUND_MASK_CD | SOUND_MASK_SYNTH | SOUND_MASK_VOLUME | SOUND_MASK_MIC; 
				break;

			case SOUND_MIXER_RECMASK:
				val = SOUND_MASK_MIC | SOUND_MASK_LINE; 
				break;
				
			case SOUND_MIXER_CAPS:
				val = 0; 
				break;

			case SOUND_MIXER_MIC:
				val = volumes[DEV_MIC];
				break;
				
			case SOUND_MIXER_LINE:
				val = volumes[DEV_LINE];
				break;

			case SOUND_MIXER_CD:
				val = volumes[DEV_CD];
				break;

			case SOUND_MIXER_VOLUME:
				val = volumes[DEV_VOL];
				break;

			case SOUND_MIXER_SYNTH:
				val = volumes[DEV_GF1]; 
				break;

			default:
				return -EINVAL;
			}
			return put_user(val, (int __user *)arg);
		}
	}
	return -EINVAL;
}

static struct mixer_operations ics2101_mixer_operations =
{
	.owner	= THIS_MODULE,
	.id	= "ICS2101",
	.name	= "ICS2101 Multimedia Mixer",
	.ioctl	= ics2101_mixer_ioctl
};

int __init ics2101_mixer_init(void)
{
	int i;
	int n;

	if ((n = sound_alloc_mixerdev()) != -1)
	{
		mixer_devs[n] = &ics2101_mixer_operations;

		/*
		 * Some GUS v3.7 cards had some channels flipped. Disable
		 * the flipping feature if the model id is other than 5.
		 */

		if (inb(u_MixSelect) != 5)
		{
			for (i = 0; i < ICS_MIXDEVS; i++)
				left_fix[i] = 1;
			for (i = 0; i < ICS_MIXDEVS; i++)
				right_fix[i] = 2;
		}
		set_volumes(DEV_GF1, 0x5a5a);
		set_volumes(DEV_CD, 0x5a5a);
		set_volumes(DEV_MIC, 0x0000);
		set_volumes(DEV_LINE, 0x5a5a);
		set_volumes(DEV_VOL, 0x5a5a);
		set_volumes(DEV_UNUSED, 0x0000);
	}
	return n;
}