cgroup: implement cgroup v2 thread support
[linux/fpc-iii.git] / sound / firewire / oxfw / oxfw-spkr.c
blobcb905af0660dd29b119fcc2de8bc876fff004308
1 /*
2 * oxfw-spkr.c - a part of driver for OXFW970/971 based devices
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
8 #include "oxfw.h"
10 struct fw_spkr {
11 bool mute;
12 s16 volume[6];
13 s16 volume_min;
14 s16 volume_max;
16 unsigned int mixer_channels;
17 u8 mute_fb_id;
18 u8 volume_fb_id;
21 enum control_action { CTL_READ, CTL_WRITE };
22 enum control_attribute {
23 CTL_MIN = 0x02,
24 CTL_MAX = 0x03,
25 CTL_CURRENT = 0x10,
28 static int avc_audio_feature_mute(struct fw_unit *unit, u8 fb_id, bool *value,
29 enum control_action action)
31 u8 *buf;
32 u8 response_ok;
33 int err;
35 buf = kmalloc(11, GFP_KERNEL);
36 if (!buf)
37 return -ENOMEM;
39 if (action == CTL_READ) {
40 buf[0] = 0x01; /* AV/C, STATUS */
41 response_ok = 0x0c; /* STABLE */
42 } else {
43 buf[0] = 0x00; /* AV/C, CONTROL */
44 response_ok = 0x09; /* ACCEPTED */
46 buf[1] = 0x08; /* audio unit 0 */
47 buf[2] = 0xb8; /* FUNCTION BLOCK */
48 buf[3] = 0x81; /* function block type: feature */
49 buf[4] = fb_id; /* function block ID */
50 buf[5] = 0x10; /* control attribute: current */
51 buf[6] = 0x02; /* selector length */
52 buf[7] = 0x00; /* audio channel number */
53 buf[8] = 0x01; /* control selector: mute */
54 buf[9] = 0x01; /* control data length */
55 if (action == CTL_READ)
56 buf[10] = 0xff;
57 else
58 buf[10] = *value ? 0x70 : 0x60;
60 err = fcp_avc_transaction(unit, buf, 11, buf, 11, 0x3fe);
61 if (err < 0)
62 goto error;
63 if (err < 11) {
64 dev_err(&unit->device, "short FCP response\n");
65 err = -EIO;
66 goto error;
68 if (buf[0] != response_ok) {
69 dev_err(&unit->device, "mute command failed\n");
70 err = -EIO;
71 goto error;
73 if (action == CTL_READ)
74 *value = buf[10] == 0x70;
76 err = 0;
78 error:
79 kfree(buf);
81 return err;
84 static int avc_audio_feature_volume(struct fw_unit *unit, u8 fb_id, s16 *value,
85 unsigned int channel,
86 enum control_attribute attribute,
87 enum control_action action)
89 u8 *buf;
90 u8 response_ok;
91 int err;
93 buf = kmalloc(12, GFP_KERNEL);
94 if (!buf)
95 return -ENOMEM;
97 if (action == CTL_READ) {
98 buf[0] = 0x01; /* AV/C, STATUS */
99 response_ok = 0x0c; /* STABLE */
100 } else {
101 buf[0] = 0x00; /* AV/C, CONTROL */
102 response_ok = 0x09; /* ACCEPTED */
104 buf[1] = 0x08; /* audio unit 0 */
105 buf[2] = 0xb8; /* FUNCTION BLOCK */
106 buf[3] = 0x81; /* function block type: feature */
107 buf[4] = fb_id; /* function block ID */
108 buf[5] = attribute; /* control attribute */
109 buf[6] = 0x02; /* selector length */
110 buf[7] = channel; /* audio channel number */
111 buf[8] = 0x02; /* control selector: volume */
112 buf[9] = 0x02; /* control data length */
113 if (action == CTL_READ) {
114 buf[10] = 0xff;
115 buf[11] = 0xff;
116 } else {
117 buf[10] = *value >> 8;
118 buf[11] = *value;
121 err = fcp_avc_transaction(unit, buf, 12, buf, 12, 0x3fe);
122 if (err < 0)
123 goto error;
124 if (err < 12) {
125 dev_err(&unit->device, "short FCP response\n");
126 err = -EIO;
127 goto error;
129 if (buf[0] != response_ok) {
130 dev_err(&unit->device, "volume command failed\n");
131 err = -EIO;
132 goto error;
134 if (action == CTL_READ)
135 *value = (buf[10] << 8) | buf[11];
137 err = 0;
139 error:
140 kfree(buf);
142 return err;
145 static int spkr_mute_get(struct snd_kcontrol *control,
146 struct snd_ctl_elem_value *value)
148 struct snd_oxfw *oxfw = control->private_data;
149 struct fw_spkr *spkr = oxfw->spec;
151 value->value.integer.value[0] = !spkr->mute;
153 return 0;
156 static int spkr_mute_put(struct snd_kcontrol *control,
157 struct snd_ctl_elem_value *value)
159 struct snd_oxfw *oxfw = control->private_data;
160 struct fw_spkr *spkr = oxfw->spec;
161 bool mute;
162 int err;
164 mute = !value->value.integer.value[0];
166 if (mute == spkr->mute)
167 return 0;
169 err = avc_audio_feature_mute(oxfw->unit, spkr->mute_fb_id, &mute,
170 CTL_WRITE);
171 if (err < 0)
172 return err;
173 spkr->mute = mute;
175 return 1;
178 static int spkr_volume_info(struct snd_kcontrol *control,
179 struct snd_ctl_elem_info *info)
181 struct snd_oxfw *oxfw = control->private_data;
182 struct fw_spkr *spkr = oxfw->spec;
184 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
185 info->count = spkr->mixer_channels;
186 info->value.integer.min = spkr->volume_min;
187 info->value.integer.max = spkr->volume_max;
189 return 0;
192 static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 };
194 static int spkr_volume_get(struct snd_kcontrol *control,
195 struct snd_ctl_elem_value *value)
197 struct snd_oxfw *oxfw = control->private_data;
198 struct fw_spkr *spkr = oxfw->spec;
199 unsigned int i;
201 for (i = 0; i < spkr->mixer_channels; ++i)
202 value->value.integer.value[channel_map[i]] = spkr->volume[i];
204 return 0;
207 static int spkr_volume_put(struct snd_kcontrol *control,
208 struct snd_ctl_elem_value *value)
210 struct snd_oxfw *oxfw = control->private_data;
211 struct fw_spkr *spkr = oxfw->spec;
212 unsigned int i, changed_channels;
213 bool equal_values = true;
214 s16 volume;
215 int err;
217 for (i = 0; i < spkr->mixer_channels; ++i) {
218 if (value->value.integer.value[i] < spkr->volume_min ||
219 value->value.integer.value[i] > spkr->volume_max)
220 return -EINVAL;
221 if (value->value.integer.value[i] !=
222 value->value.integer.value[0])
223 equal_values = false;
226 changed_channels = 0;
227 for (i = 0; i < spkr->mixer_channels; ++i)
228 if (value->value.integer.value[channel_map[i]] !=
229 spkr->volume[i])
230 changed_channels |= 1 << (i + 1);
232 if (equal_values && changed_channels != 0)
233 changed_channels = 1 << 0;
235 for (i = 0; i <= spkr->mixer_channels; ++i) {
236 volume = value->value.integer.value[channel_map[i ? i - 1 : 0]];
237 if (changed_channels & (1 << i)) {
238 err = avc_audio_feature_volume(oxfw->unit,
239 spkr->volume_fb_id, &volume,
240 i, CTL_CURRENT, CTL_WRITE);
241 if (err < 0)
242 return err;
244 if (i > 0)
245 spkr->volume[i - 1] = volume;
248 return changed_channels != 0;
251 int snd_oxfw_add_spkr(struct snd_oxfw *oxfw, bool is_lacie)
253 static const struct snd_kcontrol_new controls[] = {
255 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
256 .name = "PCM Playback Switch",
257 .info = snd_ctl_boolean_mono_info,
258 .get = spkr_mute_get,
259 .put = spkr_mute_put,
262 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
263 .name = "PCM Playback Volume",
264 .info = spkr_volume_info,
265 .get = spkr_volume_get,
266 .put = spkr_volume_put,
269 struct fw_spkr *spkr;
270 unsigned int i, first_ch;
271 int err;
273 spkr = kzalloc(sizeof(struct fw_spkr), GFP_KERNEL);
274 if (spkr == NULL)
275 return -ENOMEM;
276 oxfw->spec = spkr;
278 if (is_lacie) {
279 spkr->mixer_channels = 1;
280 spkr->mute_fb_id = 0x01;
281 spkr->volume_fb_id = 0x01;
282 } else {
283 spkr->mixer_channels = 6;
284 spkr->mute_fb_id = 0x01;
285 spkr->volume_fb_id = 0x02;
288 err = avc_audio_feature_volume(oxfw->unit, spkr->volume_fb_id,
289 &spkr->volume_min, 0, CTL_MIN, CTL_READ);
290 if (err < 0)
291 return err;
292 err = avc_audio_feature_volume(oxfw->unit, spkr->volume_fb_id,
293 &spkr->volume_max, 0, CTL_MAX, CTL_READ);
294 if (err < 0)
295 return err;
297 err = avc_audio_feature_mute(oxfw->unit, spkr->mute_fb_id, &spkr->mute,
298 CTL_READ);
299 if (err < 0)
300 return err;
302 first_ch = spkr->mixer_channels == 1 ? 0 : 1;
303 for (i = 0; i < spkr->mixer_channels; ++i) {
304 err = avc_audio_feature_volume(oxfw->unit, spkr->volume_fb_id,
305 &spkr->volume[i], first_ch + i,
306 CTL_CURRENT, CTL_READ);
307 if (err < 0)
308 return err;
311 for (i = 0; i < ARRAY_SIZE(controls); ++i) {
312 err = snd_ctl_add(oxfw->card,
313 snd_ctl_new1(&controls[i], oxfw));
314 if (err < 0)
315 return err;
318 return 0;