1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
6 /* Flag for mono controls. */
11 /* Whether or not the bits in the channel are inverted. */
15 static struct ac97_chn_desc {
25 { AC97_MASTER_VOL_STEREO, SOUND_MIXER_VOLUME, 0x3f, ST, SOUND_MASK_VOLUME, 5, 0x0000, INV },
26 { AC97_MASTER_VOL_MONO, SOUND_MIXER_PHONEOUT, 0x3f, MO, SOUND_MASK_PHONEOUT, 6, 0x0000, INV },
27 { AC97_MASTER_TONE, SOUND_MIXER_TREBLE, 0x0f, MO, SOUND_MASK_TREBLE, -1, 0x00ff, INV },
28 { AC97_MASTER_TONE, SOUND_MIXER_BASS, 0x0f, MO, SOUND_MASK_BASS, -1, 0xff00, INV },
29 { AC97_PCBEEP_VOL, SOUND_MIXER_SPEAKER, 0x0f, MO, SOUND_MASK_SPEAKER, -1, 0x001e, INV },
30 { AC97_PHONE_VOL, SOUND_MIXER_PHONEIN, 0x1f, MO, SOUND_MASK_PHONEIN, 7, 0x0000, INV },
31 { AC97_MIC_VOL, SOUND_MIXER_MIC, 0x1f, MO, SOUND_MASK_MIC, 0, 0x0000, INV },
32 { AC97_LINEIN_VOL, SOUND_MIXER_LINE, 0x1f, ST, SOUND_MASK_LINE, 4, 0x0000, INV },
33 { AC97_CD_VOL, SOUND_MIXER_CD, 0x1f, ST, SOUND_MASK_CD, 1, 0x0000, INV },
34 { AC97_VIDEO_VOL, SOUND_MIXER_VIDEO, 0x1f, ST, SOUND_MASK_VIDEO, 2, 0x0000, INV },
35 { AC97_AUX_VOL, SOUND_MIXER_LINE1, 0x1f, ST, SOUND_MASK_LINE1, 3, 0x0000, INV },
36 { AC97_PCMOUT_VOL, SOUND_MIXER_PCM, 0x1f, ST, SOUND_MASK_PCM, -1, 0x0000, INV },
37 { AC97_RECORD_GAIN, SOUND_MIXER_IGAIN, 0x0f, ST, SOUND_MASK_IGAIN, -1, 0x0000, NINV },
38 { -1, -1, 0xff, 0, 0, -1, 0x0000, 0 },
41 static struct ac97_chn_desc *
42 ac97_find_chndesc (struct ac97_hwint *dev, int oss_channel)
46 for (x = 0; mixerRegs[x].oss_channel != -1; x++) {
47 if (mixerRegs[x].oss_channel == oss_channel)
55 ac97_is_valid_channel (struct ac97_hwint *dev, struct ac97_chn_desc *chn)
57 return (dev->last_written_mixer_values[chn->ac97_regnum / 2]
58 != AC97_REG_UNSUPPORTED);
62 ac97_init (struct ac97_hwint *dev)
67 /* Clear out the arrays of cached values. */
68 for (x = 0; x < AC97_REG_CNT; x++)
69 dev->last_written_mixer_values[x] = AC97_REGVAL_UNKNOWN;
71 for (x = 0; x < SOUND_MIXER_NRDEVICES; x++)
72 dev->last_written_OSS_values[x] = AC97_REGVAL_UNKNOWN;
74 /* Clear the device masks. */
75 dev->mixer_devmask = 0;
76 dev->mixer_stereomask = 0;
77 dev->mixer_recmask = 0;
79 /* ??? Do a "standard reset" via register 0? */
81 /* Hardware-dependent reset. */
82 if (dev->reset_device (dev))
85 /* Check the mixer device capabilities. */
86 reg0 = dev->read_reg (dev, AC97_RESET);
91 /* Check for support for treble/bass controls. */
93 dev->last_written_mixer_values[AC97_MASTER_TONE / 2]
94 = AC97_REG_UNSUPPORTED;
97 /* ??? There may be other tests here? */
99 /* Fill in the device masks. */
100 for (x = 0; mixerRegs[x].ac97_regnum != -1; x++) {
101 if (ac97_is_valid_channel (dev, mixerRegs + x)) {
102 dev->mixer_devmask |= mixerRegs[x].oss_mask;
104 if (mixerRegs[x].is_stereo)
105 dev->mixer_stereomask |= mixerRegs[x].oss_mask;
107 if (mixerRegs[x].recordNum != -1)
108 dev->mixer_recmask |= mixerRegs[x].oss_mask;
115 /* Return the contents of register REG; use the cache if the value in it
116 is valid. Returns a negative error code on failure. */
118 ac97_get_register (struct ac97_hwint *dev, u8 reg)
120 if (reg > 127 || (reg & 1))
123 /* See if it's in the cache, or if it's just plain invalid. */
124 switch (dev->last_written_mixer_values[reg / 2]) {
125 case AC97_REG_UNSUPPORTED:
128 case AC97_REGVAL_UNKNOWN:
129 dev->last_written_mixer_values[reg / 2] = dev->read_reg (dev, reg);
134 return dev->last_written_mixer_values[reg / 2];
137 /* Write VALUE to AC97 register REG, and cache its value in the last-written
138 cache. Returns a negative error code on failure, or 0 on success. */
140 ac97_put_register (struct ac97_hwint *dev, u8 reg, u16 value)
142 if (reg > 127 || (reg & 1))
145 if (dev->last_written_mixer_values[reg / 2] == AC97_REG_UNSUPPORTED)
148 int res = dev->write_reg (dev, reg, value);
150 dev->last_written_mixer_values[reg / 2] = value;
158 /* Scale VALUE (a value fro 0 to MAXVAL) to a value from 0-100. If
159 IS_STEREO is set, VALUE is a stereo value; the left channel value
160 is in the lower 8 bits, and the right channel value is in the upper
163 A negative error code is returned on failure, or the unsigned
164 scaled value on success. */
167 ac97_scale_to_oss_val (int value, int maxval, int is_stereo, int inv)
170 if (value & AC97_MUTE)
174 return (ac97_scale_to_oss_val (value & 255, maxval, 0, inv) << 8)
175 | (ac97_scale_to_oss_val ((value >> 8) & 255, maxval, 0, inv) << 0);
181 value = maxval - value;
183 i = (value * 100 + (maxval / 2)) / maxval;
193 ac97_scale_from_oss_val (int value, int maxval, int is_stereo, int inv)
196 return (ac97_scale_from_oss_val (value & 255, maxval, 0, inv) << 8)
197 | (ac97_scale_from_oss_val ((value >> 8) & 255, maxval, 0, inv) << 0);
199 int i = ((value & 255) * maxval + 50) / 100;
211 ac97_set_mixer (struct ac97_hwint *dev, int oss_channel, u16 oss_value)
214 struct ac97_chn_desc *channel = ac97_find_chndesc (dev, oss_channel);
219 if (! ac97_is_valid_channel (dev, channel))
221 scaled_value = ac97_scale_from_oss_val (oss_value, channel->maxval,
223 channel->is_inverted);
224 if (scaled_value < 0)
227 if (channel->regmask != 0) {
230 int oldval = ac97_get_register (dev, channel->ac97_regnum);
234 for (mv = channel->regmask; ! (mv & 1); mv >>= 1)
237 scaled_value &= channel->regmask;
238 scaled_value |= (oldval & ~channel->regmask);
240 result = ac97_put_register (dev, channel->ac97_regnum, scaled_value);
242 dev->last_written_OSS_values[oss_channel] = oss_value;
247 ac97_get_mixer_scaled (struct ac97_hwint *dev, int oss_channel)
249 struct ac97_chn_desc *channel = ac97_find_chndesc (dev, oss_channel);
255 if (! ac97_is_valid_channel (dev, channel))
258 regval = ac97_get_register (dev, channel->ac97_regnum);
263 if (channel->regmask != 0) {
266 regval &= channel->regmask;
268 for (mv = channel->regmask; ! (mv & 1); mv >>= 1)
271 return ac97_scale_to_oss_val (regval, channel->maxval,
273 channel->is_inverted);
277 ac97_get_recmask (struct ac97_hwint *dev)
279 int recReg = ac97_get_register (dev, AC97_RECORD_SELECT);
285 for (x = 0; mixerRegs[x].ac97_regnum >= 0; x++) {
286 if (mixerRegs[x].recordNum == (recReg & 7))
287 return mixerRegs[x].oss_mask;
294 ac97_set_recmask (struct ac97_hwint *dev, int oss_recmask)
298 if (oss_recmask == 0)
299 oss_recmask = SOUND_MIXER_MIC;
301 for (x = 0; mixerRegs[x].ac97_regnum >= 0; x++) {
302 if ((mixerRegs[x].recordNum >= 0)
303 && (oss_recmask & mixerRegs[x].oss_mask))
306 if (mixerRegs[x].ac97_regnum < 0)
309 int regval = (mixerRegs[x].recordNum << 8) | mixerRegs[x].recordNum;
310 int res = ac97_put_register (dev, AC97_RECORD_SELECT, regval);
312 return ac97_get_recmask (dev);
318 /* Set the mixer DEV to the list of values in VALUE_LIST. Return 0 on
319 success, or a negative error code. */
321 ac97_set_values (struct ac97_hwint *dev,
322 struct ac97_mixer_value_list *value_list)
326 for (x = 0; value_list[x].oss_channel != -1; x++) {
327 int chnum = value_list[x].oss_channel;
328 struct ac97_chn_desc *chent = ac97_find_chndesc (dev, chnum);
333 if (chent->is_stereo)
334 val = (value_list[x].value.stereo.right << 8)
335 | value_list[x].value.stereo.left;
337 /* We do this so the returned value looks OK in the
338 mixer app. It's not necessary otherwise. */
339 val = (value_list[x].value.mono << 8)
340 | value_list[x].value.mono;
342 res = ac97_set_mixer (dev, chnum, val);
353 ac97_mixer_ioctl (struct ac97_hwint *dev, unsigned int cmd, void __user *arg)
358 case SOUND_MIXER_READ_RECSRC:
359 ret = ac97_get_recmask (dev);
362 case SOUND_MIXER_WRITE_RECSRC:
364 if (get_user (ret, (int __user *) arg))
367 ret = ac97_set_recmask (dev, ret);
371 case SOUND_MIXER_READ_CAPS:
372 ret = SOUND_CAP_EXCL_INPUT;
375 case SOUND_MIXER_READ_DEVMASK:
376 ret = dev->mixer_devmask;
379 case SOUND_MIXER_READ_RECMASK:
380 ret = dev->mixer_recmask;
383 case SOUND_MIXER_READ_STEREODEVS:
384 ret = dev->mixer_stereomask;
388 /* Read or write request. */
390 if (_IOC_TYPE (cmd) == 'M') {
391 int dir = _SIOC_DIR (cmd);
392 int channel = _IOC_NR (cmd);
394 if (channel >= 0 && channel < SOUND_MIXER_NRDEVICES) {
396 if (dir & _SIOC_WRITE) {
398 if (get_user (val, (int __user *) arg) == 0)
399 ret = ac97_set_mixer (dev, channel, val);
403 if (ret >= 0 && (dir & _SIOC_READ)) {
404 if (dev->last_written_OSS_values[channel]
405 == AC97_REGVAL_UNKNOWN)
406 dev->last_written_OSS_values[channel]
407 = ac97_get_mixer_scaled (dev, channel);
408 ret = dev->last_written_OSS_values[channel];
418 return put_user(ret, (int __user *) arg);
421 EXPORT_SYMBOL(ac97_init);
422 EXPORT_SYMBOL(ac97_set_values);
423 EXPORT_SYMBOL(ac97_put_register);
424 EXPORT_SYMBOL(ac97_mixer_ioctl);
425 MODULE_LICENSE("GPL");