0129-bcm2835-implement-channel-map-API.patch 14 KB

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