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 /* Reset the mixer to the currently saved settings. */
117 ac97_reset (struct ac97_hwint
*dev
)
121 if (dev
->reset_device (dev
))
124 /* Now set the registers back to their last-written values. */
125 for (x
= 0; mixerRegs
[x
].ac97_regnum
!= -1; x
++) {
126 int regnum
= mixerRegs
[x
].ac97_regnum
;
127 int value
= dev
->last_written_mixer_values
[regnum
/ 2];
129 ac97_put_register (dev
, regnum
, value
);
134 /* Return the contents of register REG; use the cache if the value in it
135 is valid. Returns a negative error code on failure. */
137 ac97_get_register (struct ac97_hwint
*dev
, u8 reg
)
139 if (reg
> 127 || (reg
& 1))
142 /* See if it's in the cache, or if it's just plain invalid. */
143 switch (dev
->last_written_mixer_values
[reg
/ 2]) {
144 case AC97_REG_UNSUPPORTED
:
147 case AC97_REGVAL_UNKNOWN
:
148 dev
->last_written_mixer_values
[reg
/ 2] = dev
->read_reg (dev
, reg
);
153 return dev
->last_written_mixer_values
[reg
/ 2];
156 /* Write VALUE to AC97 register REG, and cache its value in the last-written
157 cache. Returns a negative error code on failure, or 0 on success. */
159 ac97_put_register (struct ac97_hwint
*dev
, u8 reg
, u16 value
)
161 if (reg
> 127 || (reg
& 1))
164 if (dev
->last_written_mixer_values
[reg
/ 2] == AC97_REG_UNSUPPORTED
)
167 int res
= dev
->write_reg (dev
, reg
, value
);
169 dev
->last_written_mixer_values
[reg
/ 2] = value
;
177 /* Scale VALUE (a value fro 0 to MAXVAL) to a value from 0-100. If
178 IS_STEREO is set, VALUE is a stereo value; the left channel value
179 is in the lower 8 bits, and the right channel value is in the upper
182 A negative error code is returned on failure, or the unsigned
183 scaled value on success. */
186 ac97_scale_to_oss_val (int value
, int maxval
, int is_stereo
, int inv
)
189 if (value
& AC97_MUTE
)
193 return (ac97_scale_to_oss_val (value
& 255, maxval
, 0, inv
) << 8)
194 | (ac97_scale_to_oss_val ((value
>> 8) & 255, maxval
, 0, inv
) << 0);
200 value
= maxval
- value
;
202 i
= (value
* 100 + (maxval
/ 2)) / maxval
;
212 ac97_scale_from_oss_val (int value
, int maxval
, int is_stereo
, int inv
)
215 return (ac97_scale_from_oss_val (value
& 255, maxval
, 0, inv
) << 8)
216 | (ac97_scale_from_oss_val ((value
>> 8) & 255, maxval
, 0, inv
) << 0);
218 int i
= ((value
& 255) * maxval
+ 50) / 100;
230 ac97_set_mixer (struct ac97_hwint
*dev
, int oss_channel
, u16 oss_value
)
233 struct ac97_chn_desc
*channel
= ac97_find_chndesc (dev
, oss_channel
);
238 if (! ac97_is_valid_channel (dev
, channel
))
240 scaled_value
= ac97_scale_from_oss_val (oss_value
, channel
->maxval
,
242 channel
->is_inverted
);
243 if (scaled_value
< 0)
246 if (channel
->regmask
!= 0) {
249 int oldval
= ac97_get_register (dev
, channel
->ac97_regnum
);
253 for (mv
= channel
->regmask
; ! (mv
& 1); mv
>>= 1)
256 scaled_value
&= channel
->regmask
;
257 scaled_value
|= (oldval
& ~channel
->regmask
);
259 result
= ac97_put_register (dev
, channel
->ac97_regnum
, scaled_value
);
261 dev
->last_written_OSS_values
[oss_channel
] = oss_value
;
266 ac97_get_mixer_scaled (struct ac97_hwint
*dev
, int oss_channel
)
268 struct ac97_chn_desc
*channel
= ac97_find_chndesc (dev
, oss_channel
);
274 if (! ac97_is_valid_channel (dev
, channel
))
277 regval
= ac97_get_register (dev
, channel
->ac97_regnum
);
282 if (channel
->regmask
!= 0) {
285 regval
&= channel
->regmask
;
287 for (mv
= channel
->regmask
; ! (mv
& 1); mv
>>= 1)
290 return ac97_scale_to_oss_val (regval
, channel
->maxval
,
292 channel
->is_inverted
);
296 ac97_get_recmask (struct ac97_hwint
*dev
)
298 int recReg
= ac97_get_register (dev
, AC97_RECORD_SELECT
);
304 for (x
= 0; mixerRegs
[x
].ac97_regnum
>= 0; x
++) {
305 if (mixerRegs
[x
].recordNum
== (recReg
& 7))
306 return mixerRegs
[x
].oss_mask
;
313 ac97_set_recmask (struct ac97_hwint
*dev
, int oss_recmask
)
317 if (oss_recmask
== 0)
318 oss_recmask
= SOUND_MIXER_MIC
;
320 for (x
= 0; mixerRegs
[x
].ac97_regnum
>= 0; x
++) {
321 if ((mixerRegs
[x
].recordNum
>= 0)
322 && (oss_recmask
& mixerRegs
[x
].oss_mask
))
325 if (mixerRegs
[x
].ac97_regnum
< 0)
328 int regval
= (mixerRegs
[x
].recordNum
<< 8) | mixerRegs
[x
].recordNum
;
329 int res
= ac97_put_register (dev
, AC97_RECORD_SELECT
, regval
);
331 return ac97_get_recmask (dev
);
337 /* Set the mixer DEV to the list of values in VALUE_LIST. Return 0 on
338 success, or a negative error code. */
340 ac97_set_values (struct ac97_hwint
*dev
,
341 struct ac97_mixer_value_list
*value_list
)
345 for (x
= 0; value_list
[x
].oss_channel
!= -1; x
++) {
346 int chnum
= value_list
[x
].oss_channel
;
347 struct ac97_chn_desc
*chent
= ac97_find_chndesc (dev
, chnum
);
352 if (chent
->is_stereo
)
353 val
= (value_list
[x
].value
.stereo
.right
<< 8)
354 | value_list
[x
].value
.stereo
.left
;
356 /* We do this so the returned value looks OK in the
357 mixer app. It's not necessary otherwise. */
358 val
= (value_list
[x
].value
.mono
<< 8)
359 | value_list
[x
].value
.mono
;
361 res
= ac97_set_mixer (dev
, chnum
, val
);
372 ac97_mixer_ioctl (struct ac97_hwint
*dev
, unsigned int cmd
, void __user
*arg
)
377 case SOUND_MIXER_READ_RECSRC
:
378 ret
= ac97_get_recmask (dev
);
381 case SOUND_MIXER_WRITE_RECSRC
:
383 if (get_user (ret
, (int __user
*) arg
))
386 ret
= ac97_set_recmask (dev
, ret
);
390 case SOUND_MIXER_READ_CAPS
:
391 ret
= SOUND_CAP_EXCL_INPUT
;
394 case SOUND_MIXER_READ_DEVMASK
:
395 ret
= dev
->mixer_devmask
;
398 case SOUND_MIXER_READ_RECMASK
:
399 ret
= dev
->mixer_recmask
;
402 case SOUND_MIXER_READ_STEREODEVS
:
403 ret
= dev
->mixer_stereomask
;
407 /* Read or write request. */
409 if (_IOC_TYPE (cmd
) == 'M') {
410 int dir
= _SIOC_DIR (cmd
);
411 int channel
= _IOC_NR (cmd
);
413 if (channel
>= 0 && channel
< SOUND_MIXER_NRDEVICES
) {
415 if (dir
& _SIOC_WRITE
) {
417 if (get_user (val
, (int __user
*) arg
) == 0)
418 ret
= ac97_set_mixer (dev
, channel
, val
);
422 if (ret
>= 0 && (dir
& _SIOC_READ
)) {
423 if (dev
->last_written_OSS_values
[channel
]
424 == AC97_REGVAL_UNKNOWN
)
425 dev
->last_written_OSS_values
[channel
]
426 = ac97_get_mixer_scaled (dev
, channel
);
427 ret
= dev
->last_written_OSS_values
[channel
];
437 return put_user(ret
, (int __user
*) arg
);
440 EXPORT_SYMBOL(ac97_init
);
441 EXPORT_SYMBOL(ac97_set_values
);
442 EXPORT_SYMBOL(ac97_put_register
);
443 EXPORT_SYMBOL(ac97_mixer_ioctl
);
444 EXPORT_SYMBOL(ac97_reset
);
445 MODULE_LICENSE("GPL");