ALSA: oxfw: move model-specific members from common structure
[deliverable/linux.git] / sound / firewire / oxfw / oxfw-spkr.c
1 /*
2 * oxfw-spkr.c - a part of driver for OXFW970/971 based devices
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
7
8 #include "oxfw.h"
9
10 struct fw_spkr {
11 bool mute;
12 s16 volume[6];
13 s16 volume_min;
14 s16 volume_max;
15 };
16
17 enum control_action { CTL_READ, CTL_WRITE };
18 enum control_attribute {
19 CTL_MIN = 0x02,
20 CTL_MAX = 0x03,
21 CTL_CURRENT = 0x10,
22 };
23
24 static int avc_audio_feature_mute(struct fw_unit *unit, u8 fb_id, bool *value,
25 enum control_action action)
26 {
27 u8 *buf;
28 u8 response_ok;
29 int err;
30
31 buf = kmalloc(11, GFP_KERNEL);
32 if (!buf)
33 return -ENOMEM;
34
35 if (action == CTL_READ) {
36 buf[0] = 0x01; /* AV/C, STATUS */
37 response_ok = 0x0c; /* STABLE */
38 } else {
39 buf[0] = 0x00; /* AV/C, CONTROL */
40 response_ok = 0x09; /* ACCEPTED */
41 }
42 buf[1] = 0x08; /* audio unit 0 */
43 buf[2] = 0xb8; /* FUNCTION BLOCK */
44 buf[3] = 0x81; /* function block type: feature */
45 buf[4] = fb_id; /* function block ID */
46 buf[5] = 0x10; /* control attribute: current */
47 buf[6] = 0x02; /* selector length */
48 buf[7] = 0x00; /* audio channel number */
49 buf[8] = 0x01; /* control selector: mute */
50 buf[9] = 0x01; /* control data length */
51 if (action == CTL_READ)
52 buf[10] = 0xff;
53 else
54 buf[10] = *value ? 0x70 : 0x60;
55
56 err = fcp_avc_transaction(unit, buf, 11, buf, 11, 0x3fe);
57 if (err < 0)
58 goto error;
59 if (err < 11) {
60 dev_err(&unit->device, "short FCP response\n");
61 err = -EIO;
62 goto error;
63 }
64 if (buf[0] != response_ok) {
65 dev_err(&unit->device, "mute command failed\n");
66 err = -EIO;
67 goto error;
68 }
69 if (action == CTL_READ)
70 *value = buf[10] == 0x70;
71
72 err = 0;
73
74 error:
75 kfree(buf);
76
77 return err;
78 }
79
80 static int avc_audio_feature_volume(struct fw_unit *unit, u8 fb_id, s16 *value,
81 unsigned int channel,
82 enum control_attribute attribute,
83 enum control_action action)
84 {
85 u8 *buf;
86 u8 response_ok;
87 int err;
88
89 buf = kmalloc(12, GFP_KERNEL);
90 if (!buf)
91 return -ENOMEM;
92
93 if (action == CTL_READ) {
94 buf[0] = 0x01; /* AV/C, STATUS */
95 response_ok = 0x0c; /* STABLE */
96 } else {
97 buf[0] = 0x00; /* AV/C, CONTROL */
98 response_ok = 0x09; /* ACCEPTED */
99 }
100 buf[1] = 0x08; /* audio unit 0 */
101 buf[2] = 0xb8; /* FUNCTION BLOCK */
102 buf[3] = 0x81; /* function block type: feature */
103 buf[4] = fb_id; /* function block ID */
104 buf[5] = attribute; /* control attribute */
105 buf[6] = 0x02; /* selector length */
106 buf[7] = channel; /* audio channel number */
107 buf[8] = 0x02; /* control selector: volume */
108 buf[9] = 0x02; /* control data length */
109 if (action == CTL_READ) {
110 buf[10] = 0xff;
111 buf[11] = 0xff;
112 } else {
113 buf[10] = *value >> 8;
114 buf[11] = *value;
115 }
116
117 err = fcp_avc_transaction(unit, buf, 12, buf, 12, 0x3fe);
118 if (err < 0)
119 goto error;
120 if (err < 12) {
121 dev_err(&unit->device, "short FCP response\n");
122 err = -EIO;
123 goto error;
124 }
125 if (buf[0] != response_ok) {
126 dev_err(&unit->device, "volume command failed\n");
127 err = -EIO;
128 goto error;
129 }
130 if (action == CTL_READ)
131 *value = (buf[10] << 8) | buf[11];
132
133 err = 0;
134
135 error:
136 kfree(buf);
137
138 return err;
139 }
140
141 static int spkr_mute_get(struct snd_kcontrol *control,
142 struct snd_ctl_elem_value *value)
143 {
144 struct snd_oxfw *oxfw = control->private_data;
145 struct fw_spkr *spkr = oxfw->spec;
146
147 value->value.integer.value[0] = !spkr->mute;
148
149 return 0;
150 }
151
152 static int spkr_mute_put(struct snd_kcontrol *control,
153 struct snd_ctl_elem_value *value)
154 {
155 struct snd_oxfw *oxfw = control->private_data;
156 struct fw_spkr *spkr = oxfw->spec;
157 bool mute;
158 int err;
159
160 mute = !value->value.integer.value[0];
161
162 if (mute == spkr->mute)
163 return 0;
164
165 err = avc_audio_feature_mute(oxfw->unit, oxfw->device_info->mute_fb_id,
166 &mute, CTL_WRITE);
167 if (err < 0)
168 return err;
169 spkr->mute = mute;
170
171 return 1;
172 }
173
174 static int spkr_volume_info(struct snd_kcontrol *control,
175 struct snd_ctl_elem_info *info)
176 {
177 struct snd_oxfw *oxfw = control->private_data;
178 struct fw_spkr *spkr = oxfw->spec;
179
180 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
181 info->count = oxfw->device_info->mixer_channels;
182 info->value.integer.min = spkr->volume_min;
183 info->value.integer.max = spkr->volume_max;
184
185 return 0;
186 }
187
188 static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 };
189
190 static int spkr_volume_get(struct snd_kcontrol *control,
191 struct snd_ctl_elem_value *value)
192 {
193 struct snd_oxfw *oxfw = control->private_data;
194 struct fw_spkr *spkr = oxfw->spec;
195 unsigned int i;
196
197 for (i = 0; i < oxfw->device_info->mixer_channels; ++i)
198 value->value.integer.value[channel_map[i]] = spkr->volume[i];
199
200 return 0;
201 }
202
203 static int spkr_volume_put(struct snd_kcontrol *control,
204 struct snd_ctl_elem_value *value)
205 {
206 struct snd_oxfw *oxfw = control->private_data;
207 struct fw_spkr *spkr = oxfw->spec;
208 unsigned int i, changed_channels;
209 bool equal_values = true;
210 s16 volume;
211 int err;
212
213 for (i = 0; i < oxfw->device_info->mixer_channels; ++i) {
214 if (value->value.integer.value[i] < spkr->volume_min ||
215 value->value.integer.value[i] > spkr->volume_max)
216 return -EINVAL;
217 if (value->value.integer.value[i] !=
218 value->value.integer.value[0])
219 equal_values = false;
220 }
221
222 changed_channels = 0;
223 for (i = 0; i < oxfw->device_info->mixer_channels; ++i)
224 if (value->value.integer.value[channel_map[i]] !=
225 spkr->volume[i])
226 changed_channels |= 1 << (i + 1);
227
228 if (equal_values && changed_channels != 0)
229 changed_channels = 1 << 0;
230
231 for (i = 0; i <= oxfw->device_info->mixer_channels; ++i) {
232 volume = value->value.integer.value[channel_map[i ? i - 1 : 0]];
233 if (changed_channels & (1 << i)) {
234 err = avc_audio_feature_volume(oxfw->unit,
235 oxfw->device_info->mute_fb_id,
236 &volume,
237 i, CTL_CURRENT, CTL_WRITE);
238 if (err < 0)
239 return err;
240 }
241 if (i > 0)
242 spkr->volume[i - 1] = volume;
243 }
244
245 return changed_channels != 0;
246 }
247
248 int snd_oxfw_add_spkr(struct snd_oxfw *oxfw)
249 {
250 static const struct snd_kcontrol_new controls[] = {
251 {
252 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
253 .name = "PCM Playback Switch",
254 .info = snd_ctl_boolean_mono_info,
255 .get = spkr_mute_get,
256 .put = spkr_mute_put,
257 },
258 {
259 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
260 .name = "PCM Playback Volume",
261 .info = spkr_volume_info,
262 .get = spkr_volume_get,
263 .put = spkr_volume_put,
264 },
265 };
266 struct fw_spkr *spkr;
267 unsigned int i, first_ch;
268 int err;
269
270 spkr = kzalloc(sizeof(struct fw_spkr), GFP_KERNEL);
271 if (spkr == NULL)
272 return -ENOMEM;
273 oxfw->spec = spkr;
274
275 err = avc_audio_feature_volume(oxfw->unit,
276 oxfw->device_info->volume_fb_id,
277 &spkr->volume_min,
278 0, CTL_MIN, CTL_READ);
279 if (err < 0)
280 return err;
281 err = avc_audio_feature_volume(oxfw->unit,
282 oxfw->device_info->volume_fb_id,
283 &spkr->volume_max,
284 0, CTL_MAX, CTL_READ);
285 if (err < 0)
286 return err;
287
288 err = avc_audio_feature_mute(oxfw->unit, oxfw->device_info->mute_fb_id,
289 &spkr->mute, CTL_READ);
290 if (err < 0)
291 return err;
292
293 first_ch = oxfw->device_info->mixer_channels == 1 ? 0 : 1;
294 for (i = 0; i < oxfw->device_info->mixer_channels; ++i) {
295 err = avc_audio_feature_volume(oxfw->unit,
296 oxfw->device_info->volume_fb_id,
297 &spkr->volume[i],
298 first_ch + i, CTL_CURRENT, CTL_READ);
299 if (err < 0)
300 return err;
301 }
302
303 for (i = 0; i < ARRAY_SIZE(controls); ++i) {
304 err = snd_ctl_add(oxfw->card,
305 snd_ctl_new1(&controls[i], oxfw));
306 if (err < 0)
307 return err;
308 }
309
310 return 0;
311 }
This page took 0.036707 seconds and 5 git commands to generate.