Linux 4.1.16
[linux/fpc-iii.git] / sound / firewire / oxfw / oxfw-control.c
blob02a1cb90f20d00515829377993503b2369affb3e
1 /*
2 * oxfw_stream.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 enum control_action { CTL_READ, CTL_WRITE };
11 enum control_attribute {
12 CTL_MIN = 0x02,
13 CTL_MAX = 0x03,
14 CTL_CURRENT = 0x10,
17 static int oxfw_mute_command(struct snd_oxfw *oxfw, bool *value,
18 enum control_action action)
20 u8 *buf;
21 u8 response_ok;
22 int err;
24 buf = kmalloc(11, GFP_KERNEL);
25 if (!buf)
26 return -ENOMEM;
28 if (action == CTL_READ) {
29 buf[0] = 0x01; /* AV/C, STATUS */
30 response_ok = 0x0c; /* STABLE */
31 } else {
32 buf[0] = 0x00; /* AV/C, CONTROL */
33 response_ok = 0x09; /* ACCEPTED */
35 buf[1] = 0x08; /* audio unit 0 */
36 buf[2] = 0xb8; /* FUNCTION BLOCK */
37 buf[3] = 0x81; /* function block type: feature */
38 buf[4] = oxfw->device_info->mute_fb_id; /* function block ID */
39 buf[5] = 0x10; /* control attribute: current */
40 buf[6] = 0x02; /* selector length */
41 buf[7] = 0x00; /* audio channel number */
42 buf[8] = 0x01; /* control selector: mute */
43 buf[9] = 0x01; /* control data length */
44 if (action == CTL_READ)
45 buf[10] = 0xff;
46 else
47 buf[10] = *value ? 0x70 : 0x60;
49 err = fcp_avc_transaction(oxfw->unit, buf, 11, buf, 11, 0x3fe);
50 if (err < 0)
51 goto error;
52 if (err < 11) {
53 dev_err(&oxfw->unit->device, "short FCP response\n");
54 err = -EIO;
55 goto error;
57 if (buf[0] != response_ok) {
58 dev_err(&oxfw->unit->device, "mute command failed\n");
59 err = -EIO;
60 goto error;
62 if (action == CTL_READ)
63 *value = buf[10] == 0x70;
65 err = 0;
67 error:
68 kfree(buf);
70 return err;
73 static int oxfw_volume_command(struct snd_oxfw *oxfw, s16 *value,
74 unsigned int channel,
75 enum control_attribute attribute,
76 enum control_action action)
78 u8 *buf;
79 u8 response_ok;
80 int err;
82 buf = kmalloc(12, GFP_KERNEL);
83 if (!buf)
84 return -ENOMEM;
86 if (action == CTL_READ) {
87 buf[0] = 0x01; /* AV/C, STATUS */
88 response_ok = 0x0c; /* STABLE */
89 } else {
90 buf[0] = 0x00; /* AV/C, CONTROL */
91 response_ok = 0x09; /* ACCEPTED */
93 buf[1] = 0x08; /* audio unit 0 */
94 buf[2] = 0xb8; /* FUNCTION BLOCK */
95 buf[3] = 0x81; /* function block type: feature */
96 buf[4] = oxfw->device_info->volume_fb_id; /* function block ID */
97 buf[5] = attribute; /* control attribute */
98 buf[6] = 0x02; /* selector length */
99 buf[7] = channel; /* audio channel number */
100 buf[8] = 0x02; /* control selector: volume */
101 buf[9] = 0x02; /* control data length */
102 if (action == CTL_READ) {
103 buf[10] = 0xff;
104 buf[11] = 0xff;
105 } else {
106 buf[10] = *value >> 8;
107 buf[11] = *value;
110 err = fcp_avc_transaction(oxfw->unit, buf, 12, buf, 12, 0x3fe);
111 if (err < 0)
112 goto error;
113 if (err < 12) {
114 dev_err(&oxfw->unit->device, "short FCP response\n");
115 err = -EIO;
116 goto error;
118 if (buf[0] != response_ok) {
119 dev_err(&oxfw->unit->device, "volume command failed\n");
120 err = -EIO;
121 goto error;
123 if (action == CTL_READ)
124 *value = (buf[10] << 8) | buf[11];
126 err = 0;
128 error:
129 kfree(buf);
131 return err;
134 static int oxfw_mute_get(struct snd_kcontrol *control,
135 struct snd_ctl_elem_value *value)
137 struct snd_oxfw *oxfw = control->private_data;
139 value->value.integer.value[0] = !oxfw->mute;
141 return 0;
144 static int oxfw_mute_put(struct snd_kcontrol *control,
145 struct snd_ctl_elem_value *value)
147 struct snd_oxfw *oxfw = control->private_data;
148 bool mute;
149 int err;
151 mute = !value->value.integer.value[0];
153 if (mute == oxfw->mute)
154 return 0;
156 err = oxfw_mute_command(oxfw, &mute, CTL_WRITE);
157 if (err < 0)
158 return err;
159 oxfw->mute = mute;
161 return 1;
164 static int oxfw_volume_info(struct snd_kcontrol *control,
165 struct snd_ctl_elem_info *info)
167 struct snd_oxfw *oxfw = control->private_data;
169 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
170 info->count = oxfw->device_info->mixer_channels;
171 info->value.integer.min = oxfw->volume_min;
172 info->value.integer.max = oxfw->volume_max;
174 return 0;
177 static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 };
179 static int oxfw_volume_get(struct snd_kcontrol *control,
180 struct snd_ctl_elem_value *value)
182 struct snd_oxfw *oxfw = control->private_data;
183 unsigned int i;
185 for (i = 0; i < oxfw->device_info->mixer_channels; ++i)
186 value->value.integer.value[channel_map[i]] = oxfw->volume[i];
188 return 0;
191 static int oxfw_volume_put(struct snd_kcontrol *control,
192 struct snd_ctl_elem_value *value)
194 struct snd_oxfw *oxfw = control->private_data;
195 unsigned int i, changed_channels;
196 bool equal_values = true;
197 s16 volume;
198 int err;
200 for (i = 0; i < oxfw->device_info->mixer_channels; ++i) {
201 if (value->value.integer.value[i] < oxfw->volume_min ||
202 value->value.integer.value[i] > oxfw->volume_max)
203 return -EINVAL;
204 if (value->value.integer.value[i] !=
205 value->value.integer.value[0])
206 equal_values = false;
209 changed_channels = 0;
210 for (i = 0; i < oxfw->device_info->mixer_channels; ++i)
211 if (value->value.integer.value[channel_map[i]] !=
212 oxfw->volume[i])
213 changed_channels |= 1 << (i + 1);
215 if (equal_values && changed_channels != 0)
216 changed_channels = 1 << 0;
218 for (i = 0; i <= oxfw->device_info->mixer_channels; ++i) {
219 volume = value->value.integer.value[channel_map[i ? i - 1 : 0]];
220 if (changed_channels & (1 << i)) {
221 err = oxfw_volume_command(oxfw, &volume, i,
222 CTL_CURRENT, CTL_WRITE);
223 if (err < 0)
224 return err;
226 if (i > 0)
227 oxfw->volume[i - 1] = volume;
230 return changed_channels != 0;
233 int snd_oxfw_create_mixer(struct snd_oxfw *oxfw)
235 static const struct snd_kcontrol_new controls[] = {
237 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
238 .name = "PCM Playback Switch",
239 .info = snd_ctl_boolean_mono_info,
240 .get = oxfw_mute_get,
241 .put = oxfw_mute_put,
244 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
245 .name = "PCM Playback Volume",
246 .info = oxfw_volume_info,
247 .get = oxfw_volume_get,
248 .put = oxfw_volume_put,
251 unsigned int i, first_ch;
252 int err;
254 err = oxfw_volume_command(oxfw, &oxfw->volume_min,
255 0, CTL_MIN, CTL_READ);
256 if (err < 0)
257 return err;
258 err = oxfw_volume_command(oxfw, &oxfw->volume_max,
259 0, CTL_MAX, CTL_READ);
260 if (err < 0)
261 return err;
263 err = oxfw_mute_command(oxfw, &oxfw->mute, CTL_READ);
264 if (err < 0)
265 return err;
267 first_ch = oxfw->device_info->mixer_channels == 1 ? 0 : 1;
268 for (i = 0; i < oxfw->device_info->mixer_channels; ++i) {
269 err = oxfw_volume_command(oxfw, &oxfw->volume[i],
270 first_ch + i, CTL_CURRENT, CTL_READ);
271 if (err < 0)
272 return err;
275 for (i = 0; i < ARRAY_SIZE(controls); ++i) {
276 err = snd_ctl_add(oxfw->card,
277 snd_ctl_new1(&controls[i], oxfw));
278 if (err < 0)
279 return err;
282 return 0;