hdmi.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (C) 2012 Avionic Design GmbH
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/errno.h>
  10. #include <linux/export.h>
  11. #include <linux/hdmi.h>
  12. #include <linux/string.h>
  13. static void hdmi_infoframe_checksum(void *buffer, size_t size)
  14. {
  15. u8 *ptr = buffer;
  16. u8 csum = 0;
  17. size_t i;
  18. /* compute checksum */
  19. for (i = 0; i < size; i++)
  20. csum += ptr[i];
  21. ptr[3] = 256 - csum;
  22. }
  23. /**
  24. * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
  25. * @frame: HDMI AVI infoframe
  26. *
  27. * Returns 0 on success or a negative error code on failure.
  28. */
  29. int hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
  30. {
  31. memset(frame, 0, sizeof(*frame));
  32. frame->type = HDMI_INFOFRAME_TYPE_AVI;
  33. frame->version = 2;
  34. frame->length = 13;
  35. return 0;
  36. }
  37. EXPORT_SYMBOL(hdmi_avi_infoframe_init);
  38. /**
  39. * hdmi_avi_infoframe_pack() - write HDMI AVI infoframe to binary buffer
  40. * @frame: HDMI AVI infoframe
  41. * @buffer: destination buffer
  42. * @size: size of buffer
  43. *
  44. * Packs the information contained in the @frame structure into a binary
  45. * representation that can be written into the corresponding controller
  46. * registers. Also computes the checksum as required by section 5.3.5 of
  47. * the HDMI 1.4 specification.
  48. *
  49. * Returns the number of bytes packed into the binary buffer or a negative
  50. * error code on failure.
  51. */
  52. ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame, void *buffer,
  53. size_t size)
  54. {
  55. u8 *ptr = buffer;
  56. size_t length;
  57. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  58. if (size < length)
  59. return -ENOSPC;
  60. memset(buffer, 0, length);
  61. ptr[0] = frame->type;
  62. ptr[1] = frame->version;
  63. ptr[2] = frame->length;
  64. ptr[3] = 0; /* checksum */
  65. /* start infoframe payload */
  66. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  67. ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
  68. if (frame->active_info_valid)
  69. ptr[0] |= BIT(4);
  70. if (frame->horizontal_bar_valid)
  71. ptr[0] |= BIT(3);
  72. if (frame->vertical_bar_valid)
  73. ptr[0] |= BIT(2);
  74. ptr[1] = ((frame->colorimetry & 0x3) << 6) |
  75. ((frame->picture_aspect & 0x3) << 4) |
  76. (frame->active_aspect & 0xf);
  77. ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
  78. ((frame->quantization_range & 0x3) << 2) |
  79. (frame->nups & 0x3);
  80. if (frame->itc)
  81. ptr[2] |= BIT(7);
  82. ptr[3] = frame->video_code & 0x7f;
  83. ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
  84. ((frame->content_type & 0x3) << 4) |
  85. (frame->pixel_repeat & 0xf);
  86. ptr[5] = frame->top_bar & 0xff;
  87. ptr[6] = (frame->top_bar >> 8) & 0xff;
  88. ptr[7] = frame->bottom_bar & 0xff;
  89. ptr[8] = (frame->bottom_bar >> 8) & 0xff;
  90. ptr[9] = frame->left_bar & 0xff;
  91. ptr[10] = (frame->left_bar >> 8) & 0xff;
  92. ptr[11] = frame->right_bar & 0xff;
  93. ptr[12] = (frame->right_bar >> 8) & 0xff;
  94. hdmi_infoframe_checksum(buffer, length);
  95. return length;
  96. }
  97. EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
  98. /**
  99. * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
  100. * @frame: HDMI SPD infoframe
  101. * @vendor: vendor string
  102. * @product: product string
  103. *
  104. * Returns 0 on success or a negative error code on failure.
  105. */
  106. int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
  107. const char *vendor, const char *product)
  108. {
  109. memset(frame, 0, sizeof(*frame));
  110. frame->type = HDMI_INFOFRAME_TYPE_SPD;
  111. frame->version = 1;
  112. frame->length = 25;
  113. strncpy(frame->vendor, vendor, sizeof(frame->vendor));
  114. strncpy(frame->product, product, sizeof(frame->product));
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(hdmi_spd_infoframe_init);
  118. /**
  119. * hdmi_spd_infoframe_pack() - write HDMI SPD infoframe to binary buffer
  120. * @frame: HDMI SPD infoframe
  121. * @buffer: destination buffer
  122. * @size: size of buffer
  123. *
  124. * Packs the information contained in the @frame structure into a binary
  125. * representation that can be written into the corresponding controller
  126. * registers. Also computes the checksum as required by section 5.3.5 of
  127. * the HDMI 1.4 specification.
  128. *
  129. * Returns the number of bytes packed into the binary buffer or a negative
  130. * error code on failure.
  131. */
  132. ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame, void *buffer,
  133. size_t size)
  134. {
  135. u8 *ptr = buffer;
  136. size_t length;
  137. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  138. if (size < length)
  139. return -ENOSPC;
  140. memset(buffer, 0, length);
  141. ptr[0] = frame->type;
  142. ptr[1] = frame->version;
  143. ptr[2] = frame->length;
  144. ptr[3] = 0; /* checksum */
  145. /* start infoframe payload */
  146. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  147. memcpy(ptr, frame->vendor, sizeof(frame->vendor));
  148. memcpy(ptr + 8, frame->product, sizeof(frame->product));
  149. ptr[24] = frame->sdi;
  150. hdmi_infoframe_checksum(buffer, length);
  151. return length;
  152. }
  153. EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
  154. /**
  155. * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
  156. * @frame: HDMI audio infoframe
  157. *
  158. * Returns 0 on success or a negative error code on failure.
  159. */
  160. int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
  161. {
  162. memset(frame, 0, sizeof(*frame));
  163. frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
  164. frame->version = 1;
  165. frame->length = 10;
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(hdmi_audio_infoframe_init);
  169. /**
  170. * hdmi_audio_infoframe_pack() - write HDMI audio infoframe to binary buffer
  171. * @frame: HDMI audio infoframe
  172. * @buffer: destination buffer
  173. * @size: size of buffer
  174. *
  175. * Packs the information contained in the @frame structure into a binary
  176. * representation that can be written into the corresponding controller
  177. * registers. Also computes the checksum as required by section 5.3.5 of
  178. * the HDMI 1.4 specification.
  179. *
  180. * Returns the number of bytes packed into the binary buffer or a negative
  181. * error code on failure.
  182. */
  183. ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
  184. void *buffer, size_t size)
  185. {
  186. unsigned char channels;
  187. u8 *ptr = buffer;
  188. size_t length;
  189. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  190. if (size < length)
  191. return -ENOSPC;
  192. memset(buffer, 0, length);
  193. if (frame->channels >= 2)
  194. channels = frame->channels - 1;
  195. else
  196. channels = 0;
  197. ptr[0] = frame->type;
  198. ptr[1] = frame->version;
  199. ptr[2] = frame->length;
  200. ptr[3] = 0; /* checksum */
  201. /* start infoframe payload */
  202. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  203. ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
  204. ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
  205. (frame->sample_size & 0x3);
  206. ptr[2] = frame->coding_type_ext & 0x1f;
  207. ptr[3] = frame->channel_allocation;
  208. ptr[4] = (frame->level_shift_value & 0xf) << 3;
  209. if (frame->downmix_inhibit)
  210. ptr[4] |= BIT(7);
  211. hdmi_infoframe_checksum(buffer, length);
  212. return length;
  213. }
  214. EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
  215. /**
  216. * hdmi_vendor_infoframe_pack() - write a HDMI vendor infoframe to binary
  217. * buffer
  218. * @frame: HDMI vendor infoframe
  219. * @buffer: destination buffer
  220. * @size: size of buffer
  221. *
  222. * Packs the information contained in the @frame structure into a binary
  223. * representation that can be written into the corresponding controller
  224. * registers. Also computes the checksum as required by section 5.3.5 of
  225. * the HDMI 1.4 specification.
  226. *
  227. * Returns the number of bytes packed into the binary buffer or a negative
  228. * error code on failure.
  229. */
  230. ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
  231. void *buffer, size_t size)
  232. {
  233. u8 *ptr = buffer;
  234. size_t length;
  235. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  236. if (size < length)
  237. return -ENOSPC;
  238. memset(buffer, 0, length);
  239. ptr[0] = frame->type;
  240. ptr[1] = frame->version;
  241. ptr[2] = frame->length;
  242. ptr[3] = 0; /* checksum */
  243. memcpy(&ptr[HDMI_INFOFRAME_HEADER_SIZE], frame->data, frame->length);
  244. hdmi_infoframe_checksum(buffer, length);
  245. return length;
  246. }
  247. EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);