hdmi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2012 Avionic Design GmbH
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the
  12. * next paragraph) shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/bitops.h>
  24. #include <linux/errno.h>
  25. #include <linux/export.h>
  26. #include <linux/hdmi.h>
  27. #include <linux/string.h>
  28. static void hdmi_infoframe_checksum(void *buffer, size_t size)
  29. {
  30. u8 *ptr = buffer;
  31. u8 csum = 0;
  32. size_t i;
  33. /* compute checksum */
  34. for (i = 0; i < size; i++)
  35. csum += ptr[i];
  36. ptr[3] = 256 - csum;
  37. }
  38. /**
  39. * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
  40. * @frame: HDMI AVI infoframe
  41. *
  42. * Returns 0 on success or a negative error code on failure.
  43. */
  44. int hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
  45. {
  46. memset(frame, 0, sizeof(*frame));
  47. frame->type = HDMI_INFOFRAME_TYPE_AVI;
  48. frame->version = 2;
  49. frame->length = 13;
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(hdmi_avi_infoframe_init);
  53. /**
  54. * hdmi_avi_infoframe_pack() - write HDMI AVI infoframe to binary buffer
  55. * @frame: HDMI AVI infoframe
  56. * @buffer: destination buffer
  57. * @size: size of buffer
  58. *
  59. * Packs the information contained in the @frame structure into a binary
  60. * representation that can be written into the corresponding controller
  61. * registers. Also computes the checksum as required by section 5.3.5 of
  62. * the HDMI 1.4 specification.
  63. *
  64. * Returns the number of bytes packed into the binary buffer or a negative
  65. * error code on failure.
  66. */
  67. ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame, void *buffer,
  68. size_t size)
  69. {
  70. u8 *ptr = buffer;
  71. size_t length;
  72. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  73. if (size < length)
  74. return -ENOSPC;
  75. memset(buffer, 0, length);
  76. ptr[0] = frame->type;
  77. ptr[1] = frame->version;
  78. ptr[2] = frame->length;
  79. ptr[3] = 0; /* checksum */
  80. /* start infoframe payload */
  81. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  82. ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
  83. if (frame->active_info_valid)
  84. ptr[0] |= BIT(4);
  85. if (frame->horizontal_bar_valid)
  86. ptr[0] |= BIT(3);
  87. if (frame->vertical_bar_valid)
  88. ptr[0] |= BIT(2);
  89. ptr[1] = ((frame->colorimetry & 0x3) << 6) |
  90. ((frame->picture_aspect & 0x3) << 4) |
  91. (frame->active_aspect & 0xf);
  92. ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
  93. ((frame->quantization_range & 0x3) << 2) |
  94. (frame->nups & 0x3);
  95. if (frame->itc)
  96. ptr[2] |= BIT(7);
  97. ptr[3] = frame->video_code & 0x7f;
  98. ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
  99. ((frame->content_type & 0x3) << 4) |
  100. (frame->pixel_repeat & 0xf);
  101. ptr[5] = frame->top_bar & 0xff;
  102. ptr[6] = (frame->top_bar >> 8) & 0xff;
  103. ptr[7] = frame->bottom_bar & 0xff;
  104. ptr[8] = (frame->bottom_bar >> 8) & 0xff;
  105. ptr[9] = frame->left_bar & 0xff;
  106. ptr[10] = (frame->left_bar >> 8) & 0xff;
  107. ptr[11] = frame->right_bar & 0xff;
  108. ptr[12] = (frame->right_bar >> 8) & 0xff;
  109. hdmi_infoframe_checksum(buffer, length);
  110. return length;
  111. }
  112. EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
  113. /**
  114. * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
  115. * @frame: HDMI SPD infoframe
  116. * @vendor: vendor string
  117. * @product: product string
  118. *
  119. * Returns 0 on success or a negative error code on failure.
  120. */
  121. int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
  122. const char *vendor, const char *product)
  123. {
  124. memset(frame, 0, sizeof(*frame));
  125. frame->type = HDMI_INFOFRAME_TYPE_SPD;
  126. frame->version = 1;
  127. frame->length = 25;
  128. strncpy(frame->vendor, vendor, sizeof(frame->vendor));
  129. strncpy(frame->product, product, sizeof(frame->product));
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(hdmi_spd_infoframe_init);
  133. /**
  134. * hdmi_spd_infoframe_pack() - write HDMI SPD infoframe to binary buffer
  135. * @frame: HDMI SPD infoframe
  136. * @buffer: destination buffer
  137. * @size: size of buffer
  138. *
  139. * Packs the information contained in the @frame structure into a binary
  140. * representation that can be written into the corresponding controller
  141. * registers. Also computes the checksum as required by section 5.3.5 of
  142. * the HDMI 1.4 specification.
  143. *
  144. * Returns the number of bytes packed into the binary buffer or a negative
  145. * error code on failure.
  146. */
  147. ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame, void *buffer,
  148. size_t size)
  149. {
  150. u8 *ptr = buffer;
  151. size_t length;
  152. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  153. if (size < length)
  154. return -ENOSPC;
  155. memset(buffer, 0, length);
  156. ptr[0] = frame->type;
  157. ptr[1] = frame->version;
  158. ptr[2] = frame->length;
  159. ptr[3] = 0; /* checksum */
  160. /* start infoframe payload */
  161. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  162. memcpy(ptr, frame->vendor, sizeof(frame->vendor));
  163. memcpy(ptr + 8, frame->product, sizeof(frame->product));
  164. ptr[24] = frame->sdi;
  165. hdmi_infoframe_checksum(buffer, length);
  166. return length;
  167. }
  168. EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
  169. /**
  170. * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
  171. * @frame: HDMI audio infoframe
  172. *
  173. * Returns 0 on success or a negative error code on failure.
  174. */
  175. int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
  176. {
  177. memset(frame, 0, sizeof(*frame));
  178. frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
  179. frame->version = 1;
  180. frame->length = 10;
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(hdmi_audio_infoframe_init);
  184. /**
  185. * hdmi_audio_infoframe_pack() - write HDMI audio infoframe to binary buffer
  186. * @frame: HDMI audio infoframe
  187. * @buffer: destination buffer
  188. * @size: size of buffer
  189. *
  190. * Packs the information contained in the @frame structure into a binary
  191. * representation that can be written into the corresponding controller
  192. * registers. Also computes the checksum as required by section 5.3.5 of
  193. * the HDMI 1.4 specification.
  194. *
  195. * Returns the number of bytes packed into the binary buffer or a negative
  196. * error code on failure.
  197. */
  198. ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
  199. void *buffer, size_t size)
  200. {
  201. unsigned char channels;
  202. u8 *ptr = buffer;
  203. size_t length;
  204. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  205. if (size < length)
  206. return -ENOSPC;
  207. memset(buffer, 0, length);
  208. if (frame->channels >= 2)
  209. channels = frame->channels - 1;
  210. else
  211. channels = 0;
  212. ptr[0] = frame->type;
  213. ptr[1] = frame->version;
  214. ptr[2] = frame->length;
  215. ptr[3] = 0; /* checksum */
  216. /* start infoframe payload */
  217. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  218. ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
  219. ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
  220. (frame->sample_size & 0x3);
  221. ptr[2] = frame->coding_type_ext & 0x1f;
  222. ptr[3] = frame->channel_allocation;
  223. ptr[4] = (frame->level_shift_value & 0xf) << 3;
  224. if (frame->downmix_inhibit)
  225. ptr[4] |= BIT(7);
  226. hdmi_infoframe_checksum(buffer, length);
  227. return length;
  228. }
  229. EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
  230. /**
  231. * hdmi_vendor_infoframe_pack() - write a HDMI vendor infoframe to binary
  232. * buffer
  233. * @frame: HDMI vendor infoframe
  234. * @buffer: destination buffer
  235. * @size: size of buffer
  236. *
  237. * Packs the information contained in the @frame structure into a binary
  238. * representation that can be written into the corresponding controller
  239. * registers. Also computes the checksum as required by section 5.3.5 of
  240. * the HDMI 1.4 specification.
  241. *
  242. * Returns the number of bytes packed into the binary buffer or a negative
  243. * error code on failure.
  244. */
  245. ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
  246. void *buffer, size_t size)
  247. {
  248. u8 *ptr = buffer;
  249. size_t length;
  250. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  251. if (size < length)
  252. return -ENOSPC;
  253. memset(buffer, 0, length);
  254. ptr[0] = frame->type;
  255. ptr[1] = frame->version;
  256. ptr[2] = frame->length;
  257. ptr[3] = 0; /* checksum */
  258. memcpy(&ptr[HDMI_INFOFRAME_HEADER_SIZE], frame->data, frame->length);
  259. hdmi_infoframe_checksum(buffer, length);
  260. return length;
  261. }
  262. EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);