Commit | Line | Data |
---|---|---|
31514bfb | 1 | /* |
f3a0e32a | 2 | * oxfw-spkr.c - a part of driver for OXFW970/971 based devices |
31514bfb TS |
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 | ||
40540de5 TS |
10 | struct fw_spkr { |
11 | bool mute; | |
12 | s16 volume[6]; | |
13 | s16 volume_min; | |
14 | s16 volume_max; | |
15 | }; | |
16 | ||
31514bfb TS |
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 | ||
eab8e4e4 TS |
24 | static int avc_audio_feature_mute(struct fw_unit *unit, u8 fb_id, bool *value, |
25 | enum control_action action) | |
31514bfb TS |
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 */ | |
eab8e4e4 | 45 | buf[4] = fb_id; /* function block ID */ |
31514bfb TS |
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 | ||
eab8e4e4 | 56 | err = fcp_avc_transaction(unit, buf, 11, buf, 11, 0x3fe); |
31514bfb TS |
57 | if (err < 0) |
58 | goto error; | |
59 | if (err < 11) { | |
eab8e4e4 | 60 | dev_err(&unit->device, "short FCP response\n"); |
31514bfb TS |
61 | err = -EIO; |
62 | goto error; | |
63 | } | |
64 | if (buf[0] != response_ok) { | |
eab8e4e4 | 65 | dev_err(&unit->device, "mute command failed\n"); |
31514bfb TS |
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 | ||
eab8e4e4 TS |
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) | |
31514bfb TS |
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 */ | |
eab8e4e4 | 103 | buf[4] = fb_id; /* function block ID */ |
31514bfb TS |
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 | ||
eab8e4e4 | 117 | err = fcp_avc_transaction(unit, buf, 12, buf, 12, 0x3fe); |
31514bfb TS |
118 | if (err < 0) |
119 | goto error; | |
120 | if (err < 12) { | |
eab8e4e4 | 121 | dev_err(&unit->device, "short FCP response\n"); |
31514bfb TS |
122 | err = -EIO; |
123 | goto error; | |
124 | } | |
125 | if (buf[0] != response_ok) { | |
eab8e4e4 | 126 | dev_err(&unit->device, "volume command failed\n"); |
31514bfb TS |
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 | ||
29aa09ac | 141 | static int spkr_mute_get(struct snd_kcontrol *control, |
31514bfb TS |
142 | struct snd_ctl_elem_value *value) |
143 | { | |
144 | struct snd_oxfw *oxfw = control->private_data; | |
40540de5 | 145 | struct fw_spkr *spkr = oxfw->spec; |
31514bfb | 146 | |
40540de5 | 147 | value->value.integer.value[0] = !spkr->mute; |
31514bfb TS |
148 | |
149 | return 0; | |
150 | } | |
151 | ||
29aa09ac | 152 | static int spkr_mute_put(struct snd_kcontrol *control, |
31514bfb TS |
153 | struct snd_ctl_elem_value *value) |
154 | { | |
155 | struct snd_oxfw *oxfw = control->private_data; | |
40540de5 | 156 | struct fw_spkr *spkr = oxfw->spec; |
31514bfb TS |
157 | bool mute; |
158 | int err; | |
159 | ||
160 | mute = !value->value.integer.value[0]; | |
161 | ||
40540de5 | 162 | if (mute == spkr->mute) |
31514bfb TS |
163 | return 0; |
164 | ||
eab8e4e4 TS |
165 | err = avc_audio_feature_mute(oxfw->unit, oxfw->device_info->mute_fb_id, |
166 | &mute, CTL_WRITE); | |
31514bfb TS |
167 | if (err < 0) |
168 | return err; | |
40540de5 | 169 | spkr->mute = mute; |
31514bfb TS |
170 | |
171 | return 1; | |
172 | } | |
173 | ||
29aa09ac | 174 | static int spkr_volume_info(struct snd_kcontrol *control, |
31514bfb TS |
175 | struct snd_ctl_elem_info *info) |
176 | { | |
177 | struct snd_oxfw *oxfw = control->private_data; | |
40540de5 | 178 | struct fw_spkr *spkr = oxfw->spec; |
31514bfb TS |
179 | |
180 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
181 | info->count = oxfw->device_info->mixer_channels; | |
40540de5 TS |
182 | info->value.integer.min = spkr->volume_min; |
183 | info->value.integer.max = spkr->volume_max; | |
31514bfb TS |
184 | |
185 | return 0; | |
186 | } | |
187 | ||
188 | static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 }; | |
189 | ||
29aa09ac | 190 | static int spkr_volume_get(struct snd_kcontrol *control, |
31514bfb TS |
191 | struct snd_ctl_elem_value *value) |
192 | { | |
193 | struct snd_oxfw *oxfw = control->private_data; | |
40540de5 | 194 | struct fw_spkr *spkr = oxfw->spec; |
31514bfb TS |
195 | unsigned int i; |
196 | ||
197 | for (i = 0; i < oxfw->device_info->mixer_channels; ++i) | |
40540de5 | 198 | value->value.integer.value[channel_map[i]] = spkr->volume[i]; |
31514bfb TS |
199 | |
200 | return 0; | |
201 | } | |
202 | ||
29aa09ac | 203 | static int spkr_volume_put(struct snd_kcontrol *control, |
31514bfb TS |
204 | struct snd_ctl_elem_value *value) |
205 | { | |
206 | struct snd_oxfw *oxfw = control->private_data; | |
40540de5 | 207 | struct fw_spkr *spkr = oxfw->spec; |
31514bfb TS |
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) { | |
40540de5 TS |
214 | if (value->value.integer.value[i] < spkr->volume_min || |
215 | value->value.integer.value[i] > spkr->volume_max) | |
31514bfb TS |
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]] != | |
40540de5 | 225 | spkr->volume[i]) |
31514bfb TS |
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)) { | |
eab8e4e4 TS |
234 | err = avc_audio_feature_volume(oxfw->unit, |
235 | oxfw->device_info->mute_fb_id, | |
236 | &volume, | |
237 | i, CTL_CURRENT, CTL_WRITE); | |
31514bfb TS |
238 | if (err < 0) |
239 | return err; | |
240 | } | |
241 | if (i > 0) | |
40540de5 | 242 | spkr->volume[i - 1] = volume; |
31514bfb TS |
243 | } |
244 | ||
245 | return changed_channels != 0; | |
246 | } | |
247 | ||
29aa09ac | 248 | int snd_oxfw_add_spkr(struct snd_oxfw *oxfw) |
31514bfb TS |
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, | |
29aa09ac TS |
255 | .get = spkr_mute_get, |
256 | .put = spkr_mute_put, | |
31514bfb TS |
257 | }, |
258 | { | |
259 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
260 | .name = "PCM Playback Volume", | |
29aa09ac TS |
261 | .info = spkr_volume_info, |
262 | .get = spkr_volume_get, | |
263 | .put = spkr_volume_put, | |
31514bfb TS |
264 | }, |
265 | }; | |
40540de5 | 266 | struct fw_spkr *spkr; |
31514bfb TS |
267 | unsigned int i, first_ch; |
268 | int err; | |
269 | ||
40540de5 TS |
270 | spkr = kzalloc(sizeof(struct fw_spkr), GFP_KERNEL); |
271 | if (spkr == NULL) | |
272 | return -ENOMEM; | |
273 | oxfw->spec = spkr; | |
274 | ||
eab8e4e4 TS |
275 | err = avc_audio_feature_volume(oxfw->unit, |
276 | oxfw->device_info->volume_fb_id, | |
40540de5 TS |
277 | &spkr->volume_min, |
278 | 0, CTL_MIN, CTL_READ); | |
31514bfb TS |
279 | if (err < 0) |
280 | return err; | |
eab8e4e4 TS |
281 | err = avc_audio_feature_volume(oxfw->unit, |
282 | oxfw->device_info->volume_fb_id, | |
40540de5 TS |
283 | &spkr->volume_max, |
284 | 0, CTL_MAX, CTL_READ); | |
31514bfb TS |
285 | if (err < 0) |
286 | return err; | |
287 | ||
eab8e4e4 | 288 | err = avc_audio_feature_mute(oxfw->unit, oxfw->device_info->mute_fb_id, |
40540de5 | 289 | &spkr->mute, CTL_READ); |
31514bfb TS |
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) { | |
eab8e4e4 TS |
295 | err = avc_audio_feature_volume(oxfw->unit, |
296 | oxfw->device_info->volume_fb_id, | |
40540de5 | 297 | &spkr->volume[i], |
29aa09ac | 298 | first_ch + i, CTL_CURRENT, CTL_READ); |
31514bfb TS |
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 | } |