rpcd: iwinfo plugin fixes
[openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0131-bcm2835-implement-channel-map-API.patch
1 From 7c7d2db494fbf1fd0b014dab0bb4c5c740f90442 Mon Sep 17 00:00:00 2001
2 From: wm4 <wm4@nowhere>
3 Date: Wed, 13 Jan 2016 19:43:12 +0100
4 Subject: [PATCH 131/170] bcm2835: implement channel map API
5
6 Report all layouts supported by the HDMI protocol to userspace.
7 Make the videocore set the correct layout according to the
8 userspace request.
9
10 Some code taken from patch_hdmi.c. In particular, the HDMI channel
11 layout table was copied without changes - with the idea in mind that
12 hopefully it can be shared one day. Or at least updating it will be
13 simpler.
14
15 In my tests, everything appears to work, except when outputting
16 FL FR RL RR. Then my receiver outputs RL on both the RL and RR
17 speakers, while RR is never heard.
18 ---
19  sound/arm/bcm2835-ctl.c   | 276 ++++++++++++++++++++++++++++++++++++++++++++++
20  sound/arm/bcm2835-pcm.c   |  22 +++-
21  sound/arm/bcm2835-vchiq.c |  13 +++
22  sound/arm/bcm2835.h       |   4 +
23  4 files changed, 311 insertions(+), 4 deletions(-)
24
25 --- a/sound/arm/bcm2835-ctl.c
26 +++ b/sound/arm/bcm2835-ctl.c
27 @@ -300,6 +300,281 @@ static struct snd_kcontrol_new snd_bcm28
28         },
29  };
30  
31 +struct cea_channel_speaker_allocation {
32 +       int ca_index;
33 +       int speakers[8];
34 +};
35 +
36 +#define FL     SNDRV_CHMAP_FL
37 +#define FR     SNDRV_CHMAP_FR
38 +#define RL     SNDRV_CHMAP_RL
39 +#define RR     SNDRV_CHMAP_RR
40 +#define LFE    SNDRV_CHMAP_LFE
41 +#define FC     SNDRV_CHMAP_FC
42 +#define RLC    SNDRV_CHMAP_RLC
43 +#define RRC    SNDRV_CHMAP_RRC
44 +#define RC     SNDRV_CHMAP_RC
45 +#define FLC    SNDRV_CHMAP_FLC
46 +#define FRC    SNDRV_CHMAP_FRC
47 +#define FLH    SNDRV_CHMAP_TFL
48 +#define FRH    SNDRV_CHMAP_TFR
49 +#define FLW    SNDRV_CHMAP_FLW
50 +#define FRW    SNDRV_CHMAP_FRW
51 +#define TC     SNDRV_CHMAP_TC
52 +#define FCH    SNDRV_CHMAP_TFC
53 +
54 +/*
55 + * CEA-861 channel maps
56 + *
57 + * Stolen from sound/pci/hda/patch_hdmi.c
58 + * (unlike the source, this uses SNDRV_* constants directly, as by the
59 + *  map_tables array in patch_hdmi.c)
60 + * Unknown entries use 0, which unfortunately is SNDRV_CHMAP_UNKNOWN instead
61 + * of SNDRV_CHMAP_NA.
62 + */
63 +static struct cea_channel_speaker_allocation channel_allocations[] = {
64 +/*                       channel:   7     6    5    4    3     2    1    0  */
65 +{ .ca_index = 0x00,  .speakers = {   0,    0,   0,   0,   0,    0,  FR,  FL } },
66 +                                /* 2.1 */
67 +{ .ca_index = 0x01,  .speakers = {   0,    0,   0,   0,   0,  LFE,  FR,  FL } },
68 +                                /* Dolby Surround */
69 +{ .ca_index = 0x02,  .speakers = {   0,    0,   0,   0,  FC,    0,  FR,  FL } },
70 +                                /* surround40 */
71 +{ .ca_index = 0x08,  .speakers = {   0,    0,  RR,  RL,   0,    0,  FR,  FL } },
72 +                                /* surround41 */
73 +{ .ca_index = 0x09,  .speakers = {   0,    0,  RR,  RL,   0,  LFE,  FR,  FL } },
74 +                                /* surround50 */
75 +{ .ca_index = 0x0a,  .speakers = {   0,    0,  RR,  RL,  FC,    0,  FR,  FL } },
76 +                                /* surround51 */
77 +{ .ca_index = 0x0b,  .speakers = {   0,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
78 +                                /* 6.1 */
79 +{ .ca_index = 0x0f,  .speakers = {   0,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
80 +                                /* surround71 */
81 +{ .ca_index = 0x13,  .speakers = { RRC,  RLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
82 +
83 +{ .ca_index = 0x03,  .speakers = {   0,    0,   0,   0,  FC,  LFE,  FR,  FL } },
84 +{ .ca_index = 0x04,  .speakers = {   0,    0,   0,  RC,   0,    0,  FR,  FL } },
85 +{ .ca_index = 0x05,  .speakers = {   0,    0,   0,  RC,   0,  LFE,  FR,  FL } },
86 +{ .ca_index = 0x06,  .speakers = {   0,    0,   0,  RC,  FC,    0,  FR,  FL } },
87 +{ .ca_index = 0x07,  .speakers = {   0,    0,   0,  RC,  FC,  LFE,  FR,  FL } },
88 +{ .ca_index = 0x0c,  .speakers = {   0,   RC,  RR,  RL,   0,    0,  FR,  FL } },
89 +{ .ca_index = 0x0d,  .speakers = {   0,   RC,  RR,  RL,   0,  LFE,  FR,  FL } },
90 +{ .ca_index = 0x0e,  .speakers = {   0,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
91 +{ .ca_index = 0x10,  .speakers = { RRC,  RLC,  RR,  RL,   0,    0,  FR,  FL } },
92 +{ .ca_index = 0x11,  .speakers = { RRC,  RLC,  RR,  RL,   0,  LFE,  FR,  FL } },
93 +{ .ca_index = 0x12,  .speakers = { RRC,  RLC,  RR,  RL,  FC,    0,  FR,  FL } },
94 +{ .ca_index = 0x14,  .speakers = { FRC,  FLC,   0,   0,   0,    0,  FR,  FL } },
95 +{ .ca_index = 0x15,  .speakers = { FRC,  FLC,   0,   0,   0,  LFE,  FR,  FL } },
96 +{ .ca_index = 0x16,  .speakers = { FRC,  FLC,   0,   0,  FC,    0,  FR,  FL } },
97 +{ .ca_index = 0x17,  .speakers = { FRC,  FLC,   0,   0,  FC,  LFE,  FR,  FL } },
98 +{ .ca_index = 0x18,  .speakers = { FRC,  FLC,   0,  RC,   0,    0,  FR,  FL } },
99 +{ .ca_index = 0x19,  .speakers = { FRC,  FLC,   0,  RC,   0,  LFE,  FR,  FL } },
100 +{ .ca_index = 0x1a,  .speakers = { FRC,  FLC,   0,  RC,  FC,    0,  FR,  FL } },
101 +{ .ca_index = 0x1b,  .speakers = { FRC,  FLC,   0,  RC,  FC,  LFE,  FR,  FL } },
102 +{ .ca_index = 0x1c,  .speakers = { FRC,  FLC,  RR,  RL,   0,    0,  FR,  FL } },
103 +{ .ca_index = 0x1d,  .speakers = { FRC,  FLC,  RR,  RL,   0,  LFE,  FR,  FL } },
104 +{ .ca_index = 0x1e,  .speakers = { FRC,  FLC,  RR,  RL,  FC,    0,  FR,  FL } },
105 +{ .ca_index = 0x1f,  .speakers = { FRC,  FLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
106 +{ .ca_index = 0x20,  .speakers = {   0,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
107 +{ .ca_index = 0x21,  .speakers = {   0,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
108 +{ .ca_index = 0x22,  .speakers = {  TC,    0,  RR,  RL,  FC,    0,  FR,  FL } },
109 +{ .ca_index = 0x23,  .speakers = {  TC,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
110 +{ .ca_index = 0x24,  .speakers = { FRH,  FLH,  RR,  RL,   0,    0,  FR,  FL } },
111 +{ .ca_index = 0x25,  .speakers = { FRH,  FLH,  RR,  RL,   0,  LFE,  FR,  FL } },
112 +{ .ca_index = 0x26,  .speakers = { FRW,  FLW,  RR,  RL,   0,    0,  FR,  FL } },
113 +{ .ca_index = 0x27,  .speakers = { FRW,  FLW,  RR,  RL,   0,  LFE,  FR,  FL } },
114 +{ .ca_index = 0x28,  .speakers = {  TC,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
115 +{ .ca_index = 0x29,  .speakers = {  TC,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
116 +{ .ca_index = 0x2a,  .speakers = { FCH,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
117 +{ .ca_index = 0x2b,  .speakers = { FCH,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
118 +{ .ca_index = 0x2c,  .speakers = {  TC,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
119 +{ .ca_index = 0x2d,  .speakers = {  TC,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
120 +{ .ca_index = 0x2e,  .speakers = { FRH,  FLH,  RR,  RL,  FC,    0,  FR,  FL } },
121 +{ .ca_index = 0x2f,  .speakers = { FRH,  FLH,  RR,  RL,  FC,  LFE,  FR,  FL } },
122 +{ .ca_index = 0x30,  .speakers = { FRW,  FLW,  RR,  RL,  FC,    0,  FR,  FL } },
123 +{ .ca_index = 0x31,  .speakers = { FRW,  FLW,  RR,  RL,  FC,  LFE,  FR,  FL } },
124 +};
125 +
126 +static int snd_bcm2835_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
127 +                                    unsigned int size, unsigned int __user *tlv)
128 +{
129 +       unsigned int __user *dst;
130 +       int count = 0;
131 +       int i;
132 +
133 +       if (size < 8)
134 +               return -ENOMEM;
135 +       if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
136 +               return -EFAULT;
137 +       size -= 8;
138 +       dst = tlv + 2;
139 +       for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
140 +               struct cea_channel_speaker_allocation *ch = &channel_allocations[i];
141 +               int num_chs = 0;
142 +               int chs_bytes;
143 +               int c;
144 +
145 +               for (c = 0; c < 8; c++) {
146 +                       if (ch->speakers[c])
147 +                               num_chs++;
148 +               }
149 +
150 +               chs_bytes = num_chs * 4;
151 +               if (size < 8)
152 +                       return -ENOMEM;
153 +               if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
154 +                   put_user(chs_bytes, dst + 1))
155 +                       return -EFAULT;
156 +               dst += 2;
157 +               size -= 8;
158 +               count += 8;
159 +               if (size < chs_bytes)
160 +                       return -ENOMEM;
161 +               size -= chs_bytes;
162 +               count += chs_bytes;
163 +               for (c = 0; c < 8; c++) {
164 +                       int sp = ch->speakers[7 - c];
165 +                       if (sp) {
166 +                               if (put_user(sp, dst))
167 +                                       return -EFAULT;
168 +                               dst++;
169 +                       }
170 +               }
171 +       }
172 +       if (put_user(count, tlv + 1))
173 +               return -EFAULT;
174 +       return 0;
175 +}
176 +
177 +static int snd_bcm2835_chmap_ctl_get(struct snd_kcontrol *kcontrol,
178 +                                    struct snd_ctl_elem_value *ucontrol)
179 +{
180 +       struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
181 +       bcm2835_chip_t *chip = info->private_data;
182 +       unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
183 +       struct snd_pcm_substream *substream = snd_pcm_chmap_substream(info, idx);
184 +       struct cea_channel_speaker_allocation *ch = NULL;
185 +       int cur = 0;
186 +       int i;
187 +
188 +       if (!substream || !substream->runtime)
189 +               return -ENODEV;
190 +
191 +       for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
192 +               if (channel_allocations[i].ca_index == chip->cea_chmap)
193 +                       ch = &channel_allocations[i];
194 +       }
195 +
196 +       /* If no layout was set yet, return a dummy. Apparently the userspace
197 +        * API will be confused if we don't. */
198 +       if (!ch)
199 +               ch = &channel_allocations[0];
200 +
201 +       for (i = 0; i < 8; i++) {
202 +               if (ch->speakers[7 - i])
203 +                       ucontrol->value.integer.value[cur++] = ch->speakers[7 - i];
204 +       }
205 +       while (cur < 8)
206 +               ucontrol->value.integer.value[cur++] = SNDRV_CHMAP_NA;
207 +       return 0;
208 +}
209 +
210 +static int snd_bcm2835_chmap_ctl_put(struct snd_kcontrol *kcontrol,
211 +                                    struct snd_ctl_elem_value *ucontrol)
212 +{
213 +       struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
214 +       bcm2835_chip_t *chip = info->private_data;
215 +       unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
216 +       struct snd_pcm_substream *substream = snd_pcm_chmap_substream(info, idx);
217 +       int i, prepared = 0, cea_chmap = -1;
218 +       int remap[8];
219 +
220 +       if (!substream || !substream->runtime)
221 +               return -ENODEV;
222 +
223 +       switch (substream->runtime->status->state) {
224 +       case SNDRV_PCM_STATE_OPEN:
225 +       case SNDRV_PCM_STATE_SETUP:
226 +               break;
227 +       case SNDRV_PCM_STATE_PREPARED:
228 +               prepared = 1;
229 +               break;
230 +       default:
231 +               return -EBUSY;
232 +       }
233 +
234 +       for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
235 +               struct cea_channel_speaker_allocation *ch = &channel_allocations[i];
236 +               int matches = 1;
237 +               int cur = 0;
238 +               int x;
239 +               memset(remap, 0, sizeof(remap));
240 +               for (x = 0; x < substream->runtime->channels; x++) {
241 +                       int sp = ucontrol->value.integer.value[x];
242 +                       while (cur < 8 && !ch->speakers[7 - cur])
243 +                               cur++;
244 +                       if (cur >= 8) {
245 +                               /* user has more channels than ch */
246 +                               matches = 0;
247 +                               break;
248 +                       }
249 +                       if (ch->speakers[7 - cur] != sp) {
250 +                               matches = 0;
251 +                               break;
252 +                       }
253 +                       remap[x] = cur;
254 +                       cur++;
255 +               }
256 +               for (x = cur; x < 8; x++) {
257 +                       if (ch->speakers[7 - x]) {
258 +                               /* ch has more channels than user */
259 +                               matches = 0;
260 +                               break;
261 +                       }
262 +               }
263 +               if (matches) {
264 +                       cea_chmap = ch->ca_index;
265 +                       break;
266 +               }
267 +       }
268 +
269 +       if (cea_chmap < 0)
270 +               return -EINVAL;
271 +
272 +       /* don't change the layout if another substream is active */
273 +       if (chip->opened != (1 << substream->number) && chip->cea_chmap != cea_chmap)
274 +               return -EBUSY; /* unsure whether this is a good error code */
275 +
276 +       chip->cea_chmap = cea_chmap;
277 +       for (i = 0; i < 8; i++)
278 +               chip->map_channels[i] = remap[i];
279 +       if (prepared)
280 +               snd_bcm2835_pcm_prepare_again(substream);
281 +       return 0;
282 +}
283 +
284 +static int snd_bcm2835_add_chmap_ctl(bcm2835_chip_t * chip)
285 +{
286 +       struct snd_pcm_chmap *chmap;
287 +       struct snd_kcontrol *kctl;
288 +       int err, i;
289 +
290 +       err = snd_pcm_add_chmap_ctls(chip->pcm,
291 +                                    SNDRV_PCM_STREAM_PLAYBACK,
292 +                                    NULL, 8, 0, &chmap);
293 +       if (err < 0)
294 +               return err;
295 +       /* override handlers */
296 +       chmap->private_data = chip;
297 +       kctl = chmap->kctl;
298 +       for (i = 0; i < kctl->count; i++)
299 +               kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
300 +       kctl->get = snd_bcm2835_chmap_ctl_get;
301 +       kctl->put = snd_bcm2835_chmap_ctl_put;
302 +       kctl->tlv.c = snd_bcm2835_chmap_ctl_tlv;
303 +       return 0;
304 +}
305 +
306  int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
307  {
308         int err;
309 @@ -313,6 +588,7 @@ int snd_bcm2835_new_ctl(bcm2835_chip_t *
310                 if (err < 0)
311                         return err;
312         }
313 +       snd_bcm2835_add_chmap_ctl(chip);
314         for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) {
315                 err = snd_ctl_add(chip->card,
316                                 snd_ctl_new1(&snd_bcm2835_spdif[idx], chip));
317 --- a/sound/arm/bcm2835-pcm.c
318 +++ b/sound/arm/bcm2835-pcm.c
319 @@ -231,6 +231,9 @@ static int snd_bcm2835_playback_open_gen
320  
321         chip->alsa_stream[idx] = alsa_stream;
322  
323 +       if (!chip->opened)
324 +               chip->cea_chmap = -1;
325 +
326         chip->opened |= (1 << idx);
327         alsa_stream->open = 1;
328         alsa_stream->draining = 1;
329 @@ -341,8 +344,7 @@ static int snd_bcm2835_pcm_hw_free(struc
330         return snd_pcm_lib_free_pages(substream);
331  }
332  
333 -/* prepare callback */
334 -static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
335 +int snd_bcm2835_pcm_prepare_again(struct snd_pcm_substream *substream)
336  {
337         bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
338         struct snd_pcm_runtime *runtime = substream->runtime;
339 @@ -350,8 +352,6 @@ static int snd_bcm2835_pcm_prepare(struc
340         int channels;
341         int err;
342  
343 -       audio_info(" .. IN\n");
344 -
345         /* notify the vchiq that it should enter spdif passthrough mode by
346          * setting channels=0 (see
347          * https://github.com/raspberrypi/linux/issues/528) */
348 @@ -367,6 +367,20 @@ static int snd_bcm2835_pcm_prepare(struc
349                 audio_error(" error setting hw params\n");
350         }
351  
352 +       return err;
353 +}
354 +
355 +/* prepare callback */
356 +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
357 +{
358 +       bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
359 +       struct snd_pcm_runtime *runtime = substream->runtime;
360 +       bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
361 +
362 +       audio_info(" .. IN\n");
363 +
364 +       snd_bcm2835_pcm_prepare_again(substream);
365 +
366         bcm2835_audio_setup(alsa_stream);
367  
368         /* in preparation of the stream, set the controls (volume level) of the stream */
369 --- a/sound/arm/bcm2835-vchiq.c
370 +++ b/sound/arm/bcm2835-vchiq.c
371 @@ -570,6 +570,8 @@ int bcm2835_audio_set_params(bcm2835_als
372         VC_AUDIO_MSG_T m;
373         AUDIO_INSTANCE_T *instance = alsa_stream->instance;
374         int32_t success;
375 +       uint32_t chmap_value;
376 +       int i;
377         int ret;
378         LOG_DBG(" .. IN\n");
379  
380 @@ -593,10 +595,21 @@ int bcm2835_audio_set_params(bcm2835_als
381  
382         instance->result = -1;
383  
384 +       if (alsa_stream->chip->cea_chmap >= 0) {
385 +               chmap_value = (unsigned)alsa_stream->chip->cea_chmap << 24;
386 +       } else {
387 +               chmap_value = 0; /* force stereo */
388 +               for (i = 0; i < 8; i++)
389 +                       alsa_stream->chip->map_channels[i] = i;
390 +       }
391 +       for (i = 0; i < 8; i++)
392 +               chmap_value |= alsa_stream->chip->map_channels[i] << (i * 3);
393 +
394         m.type = VC_AUDIO_MSG_TYPE_CONFIG;
395         m.u.config.channels = channels;
396         m.u.config.samplerate = samplerate;
397         m.u.config.bps = bps;
398 +       m.u.config.channelmap = chmap_value;
399  
400         /* Create the message available completion */
401         init_completion(&instance->msg_avail_comp);
402 --- a/sound/arm/bcm2835.h
403 +++ b/sound/arm/bcm2835.h
404 @@ -107,6 +107,8 @@ typedef struct bcm2835_chip {
405         int old_volume; /* stores the volume value whist muted */
406         int dest;
407         int mute;
408 +       int cea_chmap; /* currently requested Audio InfoFrame Data Byte 4 */
409 +       int map_channels[8];
410  
411         unsigned int opened;
412         unsigned int spdif_status;
413 @@ -149,6 +151,8 @@ int snd_bcm2835_new_ctl(bcm2835_chip_t *
414  int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
415  int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip);
416  
417 +int snd_bcm2835_pcm_prepare_again(struct snd_pcm_substream *substream);
418 +
419  int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
420  int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
421  int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,