hdmi.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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/bug.h>
  25. #include <linux/errno.h>
  26. #include <linux/export.h>
  27. #include <linux/hdmi.h>
  28. #include <linux/string.h>
  29. #include <linux/device.h>
  30. #define hdmi_log(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
  31. static u8 hdmi_infoframe_checksum(const u8 *ptr, size_t size)
  32. {
  33. u8 csum = 0;
  34. size_t i;
  35. /* compute checksum */
  36. for (i = 0; i < size; i++)
  37. csum += ptr[i];
  38. return 256 - csum;
  39. }
  40. static void hdmi_infoframe_set_checksum(void *buffer, size_t size)
  41. {
  42. u8 *ptr = buffer;
  43. ptr[3] = hdmi_infoframe_checksum(buffer, size);
  44. }
  45. /**
  46. * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
  47. * @frame: HDMI AVI infoframe
  48. *
  49. * Returns 0 on success or a negative error code on failure.
  50. */
  51. int hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
  52. {
  53. memset(frame, 0, sizeof(*frame));
  54. frame->type = HDMI_INFOFRAME_TYPE_AVI;
  55. frame->version = 2;
  56. frame->length = HDMI_AVI_INFOFRAME_SIZE;
  57. return 0;
  58. }
  59. EXPORT_SYMBOL(hdmi_avi_infoframe_init);
  60. static int hdmi_avi_infoframe_check_only(const struct hdmi_avi_infoframe *frame)
  61. {
  62. if (frame->type != HDMI_INFOFRAME_TYPE_AVI ||
  63. frame->version != 2 ||
  64. frame->length != HDMI_AVI_INFOFRAME_SIZE)
  65. return -EINVAL;
  66. if (frame->picture_aspect > HDMI_PICTURE_ASPECT_16_9)
  67. return -EINVAL;
  68. return 0;
  69. }
  70. /**
  71. * hdmi_avi_infoframe_check() - check a HDMI AVI infoframe
  72. * @frame: HDMI AVI infoframe
  73. *
  74. * Validates that the infoframe is consistent and updates derived fields
  75. * (eg. length) based on other fields.
  76. *
  77. * Returns 0 on success or a negative error code on failure.
  78. */
  79. int hdmi_avi_infoframe_check(struct hdmi_avi_infoframe *frame)
  80. {
  81. return hdmi_avi_infoframe_check_only(frame);
  82. }
  83. EXPORT_SYMBOL(hdmi_avi_infoframe_check);
  84. /**
  85. * hdmi_avi_infoframe_pack_only() - write HDMI AVI infoframe to binary buffer
  86. * @frame: HDMI AVI infoframe
  87. * @buffer: destination buffer
  88. * @size: size of buffer
  89. *
  90. * Packs the information contained in the @frame structure into a binary
  91. * representation that can be written into the corresponding controller
  92. * registers. Also computes the checksum as required by section 5.3.5 of
  93. * the HDMI 1.4 specification.
  94. *
  95. * Returns the number of bytes packed into the binary buffer or a negative
  96. * error code on failure.
  97. */
  98. ssize_t hdmi_avi_infoframe_pack_only(const struct hdmi_avi_infoframe *frame,
  99. void *buffer, size_t size)
  100. {
  101. u8 *ptr = buffer;
  102. size_t length;
  103. int ret;
  104. ret = hdmi_avi_infoframe_check_only(frame);
  105. if (ret)
  106. return ret;
  107. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  108. if (size < length)
  109. return -ENOSPC;
  110. memset(buffer, 0, size);
  111. ptr[0] = frame->type;
  112. ptr[1] = frame->version;
  113. ptr[2] = frame->length;
  114. ptr[3] = 0; /* checksum */
  115. /* start infoframe payload */
  116. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  117. ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
  118. /*
  119. * Data byte 1, bit 4 has to be set if we provide the active format
  120. * aspect ratio
  121. */
  122. if (frame->active_aspect & 0xf)
  123. ptr[0] |= BIT(4);
  124. /* Bit 3 and 2 indicate if we transmit horizontal/vertical bar data */
  125. if (frame->top_bar || frame->bottom_bar)
  126. ptr[0] |= BIT(3);
  127. if (frame->left_bar || frame->right_bar)
  128. ptr[0] |= BIT(2);
  129. ptr[1] = ((frame->colorimetry & 0x3) << 6) |
  130. ((frame->picture_aspect & 0x3) << 4) |
  131. (frame->active_aspect & 0xf);
  132. ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
  133. ((frame->quantization_range & 0x3) << 2) |
  134. (frame->nups & 0x3);
  135. if (frame->itc)
  136. ptr[2] |= BIT(7);
  137. ptr[3] = frame->video_code & 0x7f;
  138. ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
  139. ((frame->content_type & 0x3) << 4) |
  140. (frame->pixel_repeat & 0xf);
  141. ptr[5] = frame->top_bar & 0xff;
  142. ptr[6] = (frame->top_bar >> 8) & 0xff;
  143. ptr[7] = frame->bottom_bar & 0xff;
  144. ptr[8] = (frame->bottom_bar >> 8) & 0xff;
  145. ptr[9] = frame->left_bar & 0xff;
  146. ptr[10] = (frame->left_bar >> 8) & 0xff;
  147. ptr[11] = frame->right_bar & 0xff;
  148. ptr[12] = (frame->right_bar >> 8) & 0xff;
  149. hdmi_infoframe_set_checksum(buffer, length);
  150. return length;
  151. }
  152. EXPORT_SYMBOL(hdmi_avi_infoframe_pack_only);
  153. /**
  154. * hdmi_avi_infoframe_pack() - check a HDMI AVI infoframe,
  155. * and write it to binary buffer
  156. * @frame: HDMI AVI infoframe
  157. * @buffer: destination buffer
  158. * @size: size of buffer
  159. *
  160. * Validates that the infoframe is consistent and updates derived fields
  161. * (eg. length) based on other fields, after which it packs the information
  162. * contained in the @frame structure into a binary representation that
  163. * can be written into the corresponding controller registers. This function
  164. * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
  165. * specification.
  166. *
  167. * Returns the number of bytes packed into the binary buffer or a negative
  168. * error code on failure.
  169. */
  170. ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame,
  171. void *buffer, size_t size)
  172. {
  173. int ret;
  174. ret = hdmi_avi_infoframe_check(frame);
  175. if (ret)
  176. return ret;
  177. return hdmi_avi_infoframe_pack_only(frame, buffer, size);
  178. }
  179. EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
  180. /**
  181. * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
  182. * @frame: HDMI SPD infoframe
  183. * @vendor: vendor string
  184. * @product: product string
  185. *
  186. * Returns 0 on success or a negative error code on failure.
  187. */
  188. int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
  189. const char *vendor, const char *product)
  190. {
  191. memset(frame, 0, sizeof(*frame));
  192. frame->type = HDMI_INFOFRAME_TYPE_SPD;
  193. frame->version = 1;
  194. frame->length = HDMI_SPD_INFOFRAME_SIZE;
  195. strncpy(frame->vendor, vendor, sizeof(frame->vendor));
  196. strncpy(frame->product, product, sizeof(frame->product));
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(hdmi_spd_infoframe_init);
  200. static int hdmi_spd_infoframe_check_only(const struct hdmi_spd_infoframe *frame)
  201. {
  202. if (frame->type != HDMI_INFOFRAME_TYPE_SPD ||
  203. frame->version != 1 ||
  204. frame->length != HDMI_SPD_INFOFRAME_SIZE)
  205. return -EINVAL;
  206. return 0;
  207. }
  208. /**
  209. * hdmi_spd_infoframe_check() - check a HDMI SPD infoframe
  210. * @frame: HDMI SPD infoframe
  211. *
  212. * Validates that the infoframe is consistent and updates derived fields
  213. * (eg. length) based on other fields.
  214. *
  215. * Returns 0 on success or a negative error code on failure.
  216. */
  217. int hdmi_spd_infoframe_check(struct hdmi_spd_infoframe *frame)
  218. {
  219. return hdmi_spd_infoframe_check_only(frame);
  220. }
  221. EXPORT_SYMBOL(hdmi_spd_infoframe_check);
  222. /**
  223. * hdmi_spd_infoframe_pack_only() - write HDMI SPD infoframe to binary buffer
  224. * @frame: HDMI SPD infoframe
  225. * @buffer: destination buffer
  226. * @size: size of buffer
  227. *
  228. * Packs the information contained in the @frame structure into a binary
  229. * representation that can be written into the corresponding controller
  230. * registers. Also computes the checksum as required by section 5.3.5 of
  231. * the HDMI 1.4 specification.
  232. *
  233. * Returns the number of bytes packed into the binary buffer or a negative
  234. * error code on failure.
  235. */
  236. ssize_t hdmi_spd_infoframe_pack_only(const struct hdmi_spd_infoframe *frame,
  237. void *buffer, size_t size)
  238. {
  239. u8 *ptr = buffer;
  240. size_t length;
  241. int ret;
  242. ret = hdmi_spd_infoframe_check_only(frame);
  243. if (ret)
  244. return ret;
  245. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  246. if (size < length)
  247. return -ENOSPC;
  248. memset(buffer, 0, size);
  249. ptr[0] = frame->type;
  250. ptr[1] = frame->version;
  251. ptr[2] = frame->length;
  252. ptr[3] = 0; /* checksum */
  253. /* start infoframe payload */
  254. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  255. memcpy(ptr, frame->vendor, sizeof(frame->vendor));
  256. memcpy(ptr + 8, frame->product, sizeof(frame->product));
  257. ptr[24] = frame->sdi;
  258. hdmi_infoframe_set_checksum(buffer, length);
  259. return length;
  260. }
  261. EXPORT_SYMBOL(hdmi_spd_infoframe_pack_only);
  262. /**
  263. * hdmi_spd_infoframe_pack() - check a HDMI SPD infoframe,
  264. * and write it to binary buffer
  265. * @frame: HDMI SPD infoframe
  266. * @buffer: destination buffer
  267. * @size: size of buffer
  268. *
  269. * Validates that the infoframe is consistent and updates derived fields
  270. * (eg. length) based on other fields, after which it packs the information
  271. * contained in the @frame structure into a binary representation that
  272. * can be written into the corresponding controller registers. This function
  273. * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
  274. * specification.
  275. *
  276. * Returns the number of bytes packed into the binary buffer or a negative
  277. * error code on failure.
  278. */
  279. ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame,
  280. void *buffer, size_t size)
  281. {
  282. int ret;
  283. ret = hdmi_spd_infoframe_check(frame);
  284. if (ret)
  285. return ret;
  286. return hdmi_spd_infoframe_pack_only(frame, buffer, size);
  287. }
  288. EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
  289. /**
  290. * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
  291. * @frame: HDMI audio infoframe
  292. *
  293. * Returns 0 on success or a negative error code on failure.
  294. */
  295. int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
  296. {
  297. memset(frame, 0, sizeof(*frame));
  298. frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
  299. frame->version = 1;
  300. frame->length = HDMI_AUDIO_INFOFRAME_SIZE;
  301. return 0;
  302. }
  303. EXPORT_SYMBOL(hdmi_audio_infoframe_init);
  304. static int hdmi_audio_infoframe_check_only(const struct hdmi_audio_infoframe *frame)
  305. {
  306. if (frame->type != HDMI_INFOFRAME_TYPE_AUDIO ||
  307. frame->version != 1 ||
  308. frame->length != HDMI_AUDIO_INFOFRAME_SIZE)
  309. return -EINVAL;
  310. return 0;
  311. }
  312. /**
  313. * hdmi_audio_infoframe_check() - check a HDMI audio infoframe
  314. * @frame: HDMI audio infoframe
  315. *
  316. * Validates that the infoframe is consistent and updates derived fields
  317. * (eg. length) based on other fields.
  318. *
  319. * Returns 0 on success or a negative error code on failure.
  320. */
  321. int hdmi_audio_infoframe_check(struct hdmi_audio_infoframe *frame)
  322. {
  323. return hdmi_audio_infoframe_check_only(frame);
  324. }
  325. EXPORT_SYMBOL(hdmi_audio_infoframe_check);
  326. /**
  327. * hdmi_audio_infoframe_pack_only() - write HDMI audio infoframe to binary buffer
  328. * @frame: HDMI audio infoframe
  329. * @buffer: destination buffer
  330. * @size: size of buffer
  331. *
  332. * Packs the information contained in the @frame structure into a binary
  333. * representation that can be written into the corresponding controller
  334. * registers. Also computes the checksum as required by section 5.3.5 of
  335. * the HDMI 1.4 specification.
  336. *
  337. * Returns the number of bytes packed into the binary buffer or a negative
  338. * error code on failure.
  339. */
  340. ssize_t hdmi_audio_infoframe_pack_only(const struct hdmi_audio_infoframe *frame,
  341. void *buffer, size_t size)
  342. {
  343. unsigned char channels;
  344. u8 *ptr = buffer;
  345. size_t length;
  346. int ret;
  347. ret = hdmi_audio_infoframe_check_only(frame);
  348. if (ret)
  349. return ret;
  350. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  351. if (size < length)
  352. return -ENOSPC;
  353. memset(buffer, 0, size);
  354. if (frame->channels >= 2)
  355. channels = frame->channels - 1;
  356. else
  357. channels = 0;
  358. ptr[0] = frame->type;
  359. ptr[1] = frame->version;
  360. ptr[2] = frame->length;
  361. ptr[3] = 0; /* checksum */
  362. /* start infoframe payload */
  363. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  364. ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
  365. ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
  366. (frame->sample_size & 0x3);
  367. ptr[2] = frame->coding_type_ext & 0x1f;
  368. ptr[3] = frame->channel_allocation;
  369. ptr[4] = (frame->level_shift_value & 0xf) << 3;
  370. if (frame->downmix_inhibit)
  371. ptr[4] |= BIT(7);
  372. hdmi_infoframe_set_checksum(buffer, length);
  373. return length;
  374. }
  375. EXPORT_SYMBOL(hdmi_audio_infoframe_pack_only);
  376. /**
  377. * hdmi_audio_infoframe_pack() - check a HDMI Audio infoframe,
  378. * and write it to binary buffer
  379. * @frame: HDMI Audio infoframe
  380. * @buffer: destination buffer
  381. * @size: size of buffer
  382. *
  383. * Validates that the infoframe is consistent and updates derived fields
  384. * (eg. length) based on other fields, after which it packs the information
  385. * contained in the @frame structure into a binary representation that
  386. * can be written into the corresponding controller registers. This function
  387. * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
  388. * specification.
  389. *
  390. * Returns the number of bytes packed into the binary buffer or a negative
  391. * error code on failure.
  392. */
  393. ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
  394. void *buffer, size_t size)
  395. {
  396. int ret;
  397. ret = hdmi_audio_infoframe_check(frame);
  398. if (ret)
  399. return ret;
  400. return hdmi_audio_infoframe_pack_only(frame, buffer, size);
  401. }
  402. EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
  403. /**
  404. * hdmi_vendor_infoframe_init() - initialize an HDMI vendor infoframe
  405. * @frame: HDMI vendor infoframe
  406. *
  407. * Returns 0 on success or a negative error code on failure.
  408. */
  409. int hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe *frame)
  410. {
  411. memset(frame, 0, sizeof(*frame));
  412. frame->type = HDMI_INFOFRAME_TYPE_VENDOR;
  413. frame->version = 1;
  414. frame->oui = HDMI_IEEE_OUI;
  415. /*
  416. * 0 is a valid value for s3d_struct, so we use a special "not set"
  417. * value
  418. */
  419. frame->s3d_struct = HDMI_3D_STRUCTURE_INVALID;
  420. frame->length = 4;
  421. return 0;
  422. }
  423. EXPORT_SYMBOL(hdmi_vendor_infoframe_init);
  424. static int hdmi_vendor_infoframe_length(const struct hdmi_vendor_infoframe *frame)
  425. {
  426. /* for side by side (half) we also need to provide 3D_Ext_Data */
  427. if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  428. return 6;
  429. else if (frame->vic != 0 || frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
  430. return 5;
  431. else
  432. return 4;
  433. }
  434. static int hdmi_vendor_infoframe_check_only(const struct hdmi_vendor_infoframe *frame)
  435. {
  436. if (frame->type != HDMI_INFOFRAME_TYPE_VENDOR ||
  437. frame->version != 1 ||
  438. frame->oui != HDMI_IEEE_OUI)
  439. return -EINVAL;
  440. /* only one of those can be supplied */
  441. if (frame->vic != 0 && frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
  442. return -EINVAL;
  443. if (frame->length != hdmi_vendor_infoframe_length(frame))
  444. return -EINVAL;
  445. return 0;
  446. }
  447. /**
  448. * hdmi_vendor_infoframe_check() - check a HDMI vendor infoframe
  449. * @frame: HDMI infoframe
  450. *
  451. * Validates that the infoframe is consistent and updates derived fields
  452. * (eg. length) based on other fields.
  453. *
  454. * Returns 0 on success or a negative error code on failure.
  455. */
  456. int hdmi_vendor_infoframe_check(struct hdmi_vendor_infoframe *frame)
  457. {
  458. frame->length = hdmi_vendor_infoframe_length(frame);
  459. return hdmi_vendor_infoframe_check_only(frame);
  460. }
  461. EXPORT_SYMBOL(hdmi_vendor_infoframe_check);
  462. /**
  463. * hdmi_vendor_infoframe_pack_only() - write a HDMI vendor infoframe to binary buffer
  464. * @frame: HDMI infoframe
  465. * @buffer: destination buffer
  466. * @size: size of buffer
  467. *
  468. * Packs the information contained in the @frame structure into a binary
  469. * representation that can be written into the corresponding controller
  470. * registers. Also computes the checksum as required by section 5.3.5 of
  471. * the HDMI 1.4 specification.
  472. *
  473. * Returns the number of bytes packed into the binary buffer or a negative
  474. * error code on failure.
  475. */
  476. ssize_t hdmi_vendor_infoframe_pack_only(const struct hdmi_vendor_infoframe *frame,
  477. void *buffer, size_t size)
  478. {
  479. u8 *ptr = buffer;
  480. size_t length;
  481. int ret;
  482. ret = hdmi_vendor_infoframe_check_only(frame);
  483. if (ret)
  484. return ret;
  485. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  486. if (size < length)
  487. return -ENOSPC;
  488. memset(buffer, 0, size);
  489. ptr[0] = frame->type;
  490. ptr[1] = frame->version;
  491. ptr[2] = frame->length;
  492. ptr[3] = 0; /* checksum */
  493. /* HDMI OUI */
  494. ptr[4] = 0x03;
  495. ptr[5] = 0x0c;
  496. ptr[6] = 0x00;
  497. if (frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
  498. ptr[7] = 0x2 << 5; /* video format */
  499. ptr[8] = (frame->s3d_struct & 0xf) << 4;
  500. if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  501. ptr[9] = (frame->s3d_ext_data & 0xf) << 4;
  502. } else if (frame->vic) {
  503. ptr[7] = 0x1 << 5; /* video format */
  504. ptr[8] = frame->vic;
  505. } else {
  506. ptr[7] = 0x0 << 5; /* video format */
  507. }
  508. hdmi_infoframe_set_checksum(buffer, length);
  509. return length;
  510. }
  511. EXPORT_SYMBOL(hdmi_vendor_infoframe_pack_only);
  512. /**
  513. * hdmi_vendor_infoframe_pack() - check a HDMI Vendor infoframe,
  514. * and write it to binary buffer
  515. * @frame: HDMI Vendor infoframe
  516. * @buffer: destination buffer
  517. * @size: size of buffer
  518. *
  519. * Validates that the infoframe is consistent and updates derived fields
  520. * (eg. length) based on other fields, after which it packs the information
  521. * contained in the @frame structure into a binary representation that
  522. * can be written into the corresponding controller registers. This function
  523. * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
  524. * specification.
  525. *
  526. * Returns the number of bytes packed into the binary buffer or a negative
  527. * error code on failure.
  528. */
  529. ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
  530. void *buffer, size_t size)
  531. {
  532. int ret;
  533. ret = hdmi_vendor_infoframe_check(frame);
  534. if (ret)
  535. return ret;
  536. return hdmi_vendor_infoframe_pack_only(frame, buffer, size);
  537. }
  538. EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);
  539. static int
  540. hdmi_vendor_any_infoframe_check_only(const union hdmi_vendor_any_infoframe *frame)
  541. {
  542. if (frame->any.type != HDMI_INFOFRAME_TYPE_VENDOR ||
  543. frame->any.version != 1)
  544. return -EINVAL;
  545. return 0;
  546. }
  547. /**
  548. * hdmi_drm_infoframe_init() - initialize an HDMI Dynaminc Range and
  549. * mastering infoframe
  550. * @frame: HDMI DRM infoframe
  551. *
  552. * Returns 0 on success or a negative error code on failure.
  553. */
  554. int hdmi_drm_infoframe_init(struct hdmi_drm_infoframe *frame)
  555. {
  556. memset(frame, 0, sizeof(*frame));
  557. frame->type = HDMI_INFOFRAME_TYPE_DRM;
  558. frame->version = 1;
  559. frame->length = HDMI_DRM_INFOFRAME_SIZE;
  560. return 0;
  561. }
  562. EXPORT_SYMBOL(hdmi_drm_infoframe_init);
  563. static int hdmi_drm_infoframe_check_only(const struct hdmi_drm_infoframe *frame)
  564. {
  565. if (frame->type != HDMI_INFOFRAME_TYPE_DRM ||
  566. frame->version != 1)
  567. return -EINVAL;
  568. if (frame->length != HDMI_DRM_INFOFRAME_SIZE)
  569. return -EINVAL;
  570. return 0;
  571. }
  572. /**
  573. * hdmi_drm_infoframe_check() - check a HDMI DRM infoframe
  574. * @frame: HDMI DRM infoframe
  575. *
  576. * Validates that the infoframe is consistent.
  577. * Returns 0 on success or a negative error code on failure.
  578. */
  579. int hdmi_drm_infoframe_check(struct hdmi_drm_infoframe *frame)
  580. {
  581. return hdmi_drm_infoframe_check_only(frame);
  582. }
  583. EXPORT_SYMBOL(hdmi_drm_infoframe_check);
  584. /**
  585. * hdmi_drm_infoframe_pack_only() - write HDMI DRM infoframe to binary buffer
  586. * @frame: HDMI DRM infoframe
  587. * @buffer: destination buffer
  588. * @size: size of buffer
  589. *
  590. * Packs the information contained in the @frame structure into a binary
  591. * representation that can be written into the corresponding controller
  592. * registers. Also computes the checksum as required by section 5.3.5 of
  593. * the HDMI 1.4 specification.
  594. *
  595. * Returns the number of bytes packed into the binary buffer or a negative
  596. * error code on failure.
  597. */
  598. ssize_t hdmi_drm_infoframe_pack_only(const struct hdmi_drm_infoframe *frame,
  599. void *buffer, size_t size)
  600. {
  601. u8 *ptr = buffer;
  602. size_t length;
  603. int i;
  604. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  605. if (size < length)
  606. return -ENOSPC;
  607. memset(buffer, 0, size);
  608. ptr[0] = frame->type;
  609. ptr[1] = frame->version;
  610. ptr[2] = frame->length;
  611. ptr[3] = 0; /* checksum */
  612. /* start infoframe payload */
  613. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  614. *ptr++ = frame->eotf;
  615. *ptr++ = frame->metadata_type;
  616. for (i = 0; i < 3; i++) {
  617. *ptr++ = frame->display_primaries[i].x;
  618. *ptr++ = frame->display_primaries[i].x >> 8;
  619. *ptr++ = frame->display_primaries[i].y;
  620. *ptr++ = frame->display_primaries[i].y >> 8;
  621. }
  622. *ptr++ = frame->white_point.x;
  623. *ptr++ = frame->white_point.x >> 8;
  624. *ptr++ = frame->white_point.y;
  625. *ptr++ = frame->white_point.y >> 8;
  626. *ptr++ = frame->max_display_mastering_luminance;
  627. *ptr++ = frame->max_display_mastering_luminance >> 8;
  628. *ptr++ = frame->min_display_mastering_luminance;
  629. *ptr++ = frame->min_display_mastering_luminance >> 8;
  630. *ptr++ = frame->max_cll;
  631. *ptr++ = frame->max_cll >> 8;
  632. *ptr++ = frame->max_fall;
  633. *ptr++ = frame->max_fall >> 8;
  634. hdmi_infoframe_set_checksum(buffer, length);
  635. return length;
  636. }
  637. EXPORT_SYMBOL(hdmi_drm_infoframe_pack_only);
  638. /**
  639. * hdmi_drm_infoframe_pack() - check a HDMI DRM infoframe,
  640. * and write it to binary buffer
  641. * @frame: HDMI DRM infoframe
  642. * @buffer: destination buffer
  643. * @size: size of buffer
  644. *
  645. * Validates that the infoframe is consistent and updates derived fields
  646. * (eg. length) based on other fields, after which it packs the information
  647. * contained in the @frame structure into a binary representation that
  648. * can be written into the corresponding controller registers. This function
  649. * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
  650. * specification.
  651. *
  652. * Returns the number of bytes packed into the binary buffer or a negative
  653. * error code on failure.
  654. */
  655. ssize_t hdmi_drm_infoframe_pack(struct hdmi_drm_infoframe *frame,
  656. void *buffer, size_t size)
  657. {
  658. int ret;
  659. ret = hdmi_drm_infoframe_check(frame);
  660. if (ret)
  661. return ret;
  662. return hdmi_drm_infoframe_pack_only(frame, buffer, size);
  663. }
  664. EXPORT_SYMBOL(hdmi_drm_infoframe_pack);
  665. /*
  666. * hdmi_vendor_any_infoframe_check() - check a vendor infoframe
  667. */
  668. static int
  669. hdmi_vendor_any_infoframe_check(union hdmi_vendor_any_infoframe *frame)
  670. {
  671. int ret;
  672. ret = hdmi_vendor_any_infoframe_check_only(frame);
  673. if (ret)
  674. return ret;
  675. /* we only know about HDMI vendor infoframes */
  676. if (frame->any.oui != HDMI_IEEE_OUI)
  677. return -EINVAL;
  678. return hdmi_vendor_infoframe_check(&frame->hdmi);
  679. }
  680. /*
  681. * hdmi_vendor_any_infoframe_pack_only() - write a vendor infoframe to binary buffer
  682. */
  683. static ssize_t
  684. hdmi_vendor_any_infoframe_pack_only(const union hdmi_vendor_any_infoframe *frame,
  685. void *buffer, size_t size)
  686. {
  687. int ret;
  688. ret = hdmi_vendor_any_infoframe_check_only(frame);
  689. if (ret)
  690. return ret;
  691. /* we only know about HDMI vendor infoframes */
  692. if (frame->any.oui != HDMI_IEEE_OUI)
  693. return -EINVAL;
  694. return hdmi_vendor_infoframe_pack_only(&frame->hdmi, buffer, size);
  695. }
  696. /*
  697. * hdmi_vendor_any_infoframe_pack() - check a vendor infoframe,
  698. * and write it to binary buffer
  699. */
  700. static ssize_t
  701. hdmi_vendor_any_infoframe_pack(union hdmi_vendor_any_infoframe *frame,
  702. void *buffer, size_t size)
  703. {
  704. int ret;
  705. ret = hdmi_vendor_any_infoframe_check(frame);
  706. if (ret)
  707. return ret;
  708. return hdmi_vendor_any_infoframe_pack_only(frame, buffer, size);
  709. }
  710. /**
  711. * hdmi_infoframe_check() - check a HDMI infoframe
  712. * @frame: HDMI infoframe
  713. *
  714. * Validates that the infoframe is consistent and updates derived fields
  715. * (eg. length) based on other fields.
  716. *
  717. * Returns 0 on success or a negative error code on failure.
  718. */
  719. int
  720. hdmi_infoframe_check(union hdmi_infoframe *frame)
  721. {
  722. switch (frame->any.type) {
  723. case HDMI_INFOFRAME_TYPE_AVI:
  724. return hdmi_avi_infoframe_check(&frame->avi);
  725. case HDMI_INFOFRAME_TYPE_SPD:
  726. return hdmi_spd_infoframe_check(&frame->spd);
  727. case HDMI_INFOFRAME_TYPE_AUDIO:
  728. return hdmi_audio_infoframe_check(&frame->audio);
  729. case HDMI_INFOFRAME_TYPE_VENDOR:
  730. return hdmi_vendor_any_infoframe_check(&frame->vendor);
  731. default:
  732. WARN(1, "Bad infoframe type %d\n", frame->any.type);
  733. return -EINVAL;
  734. }
  735. }
  736. EXPORT_SYMBOL(hdmi_infoframe_check);
  737. /**
  738. * hdmi_infoframe_pack_only() - write a HDMI infoframe to binary buffer
  739. * @frame: HDMI infoframe
  740. * @buffer: destination buffer
  741. * @size: size of buffer
  742. *
  743. * Packs the information contained in the @frame structure into a binary
  744. * representation that can be written into the corresponding controller
  745. * registers. Also computes the checksum as required by section 5.3.5 of
  746. * the HDMI 1.4 specification.
  747. *
  748. * Returns the number of bytes packed into the binary buffer or a negative
  749. * error code on failure.
  750. */
  751. ssize_t
  752. hdmi_infoframe_pack_only(const union hdmi_infoframe *frame, void *buffer, size_t size)
  753. {
  754. ssize_t length;
  755. switch (frame->any.type) {
  756. case HDMI_INFOFRAME_TYPE_AVI:
  757. length = hdmi_avi_infoframe_pack_only(&frame->avi,
  758. buffer, size);
  759. break;
  760. case HDMI_INFOFRAME_TYPE_DRM:
  761. length = hdmi_drm_infoframe_pack_only(&frame->drm,
  762. buffer, size);
  763. break;
  764. case HDMI_INFOFRAME_TYPE_SPD:
  765. length = hdmi_spd_infoframe_pack_only(&frame->spd,
  766. buffer, size);
  767. break;
  768. case HDMI_INFOFRAME_TYPE_AUDIO:
  769. length = hdmi_audio_infoframe_pack_only(&frame->audio,
  770. buffer, size);
  771. break;
  772. case HDMI_INFOFRAME_TYPE_VENDOR:
  773. length = hdmi_vendor_any_infoframe_pack_only(&frame->vendor,
  774. buffer, size);
  775. break;
  776. default:
  777. WARN(1, "Bad infoframe type %d\n", frame->any.type);
  778. length = -EINVAL;
  779. }
  780. return length;
  781. }
  782. EXPORT_SYMBOL(hdmi_infoframe_pack_only);
  783. /**
  784. * hdmi_infoframe_pack() - check a HDMI infoframe,
  785. * and write it to binary buffer
  786. * @frame: HDMI infoframe
  787. * @buffer: destination buffer
  788. * @size: size of buffer
  789. *
  790. * Validates that the infoframe is consistent and updates derived fields
  791. * (eg. length) based on other fields, after which it packs the information
  792. * contained in the @frame structure into a binary representation that
  793. * can be written into the corresponding controller registers. This function
  794. * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
  795. * specification.
  796. *
  797. * Returns the number of bytes packed into the binary buffer or a negative
  798. * error code on failure.
  799. */
  800. ssize_t
  801. hdmi_infoframe_pack(union hdmi_infoframe *frame,
  802. void *buffer, size_t size)
  803. {
  804. ssize_t length;
  805. switch (frame->any.type) {
  806. case HDMI_INFOFRAME_TYPE_AVI:
  807. length = hdmi_avi_infoframe_pack(&frame->avi, buffer, size);
  808. break;
  809. case HDMI_INFOFRAME_TYPE_DRM:
  810. length = hdmi_drm_infoframe_pack(&frame->drm, buffer, size);
  811. break;
  812. case HDMI_INFOFRAME_TYPE_SPD:
  813. length = hdmi_spd_infoframe_pack(&frame->spd, buffer, size);
  814. break;
  815. case HDMI_INFOFRAME_TYPE_AUDIO:
  816. length = hdmi_audio_infoframe_pack(&frame->audio, buffer, size);
  817. break;
  818. case HDMI_INFOFRAME_TYPE_VENDOR:
  819. length = hdmi_vendor_any_infoframe_pack(&frame->vendor,
  820. buffer, size);
  821. break;
  822. default:
  823. WARN(1, "Bad infoframe type %d\n", frame->any.type);
  824. length = -EINVAL;
  825. }
  826. return length;
  827. }
  828. EXPORT_SYMBOL(hdmi_infoframe_pack);
  829. static const char *hdmi_infoframe_type_get_name(enum hdmi_infoframe_type type)
  830. {
  831. if (type < 0x80 || type > 0x9f)
  832. return "Invalid";
  833. switch (type) {
  834. case HDMI_INFOFRAME_TYPE_VENDOR:
  835. return "Vendor";
  836. case HDMI_INFOFRAME_TYPE_AVI:
  837. return "Auxiliary Video Information (AVI)";
  838. case HDMI_INFOFRAME_TYPE_SPD:
  839. return "Source Product Description (SPD)";
  840. case HDMI_INFOFRAME_TYPE_AUDIO:
  841. return "Audio";
  842. case HDMI_INFOFRAME_TYPE_DRM:
  843. return "Dynamic Range and Mastering";
  844. }
  845. return "Reserved";
  846. }
  847. static void hdmi_infoframe_log_header(const char *level,
  848. struct device *dev,
  849. const struct hdmi_any_infoframe *frame)
  850. {
  851. hdmi_log("HDMI infoframe: %s, version %u, length %u\n",
  852. hdmi_infoframe_type_get_name(frame->type),
  853. frame->version, frame->length);
  854. }
  855. static const char *hdmi_colorspace_get_name(enum hdmi_colorspace colorspace)
  856. {
  857. switch (colorspace) {
  858. case HDMI_COLORSPACE_RGB:
  859. return "RGB";
  860. case HDMI_COLORSPACE_YUV422:
  861. return "YCbCr 4:2:2";
  862. case HDMI_COLORSPACE_YUV444:
  863. return "YCbCr 4:4:4";
  864. case HDMI_COLORSPACE_YUV420:
  865. return "YCbCr 4:2:0";
  866. case HDMI_COLORSPACE_RESERVED4:
  867. return "Reserved (4)";
  868. case HDMI_COLORSPACE_RESERVED5:
  869. return "Reserved (5)";
  870. case HDMI_COLORSPACE_RESERVED6:
  871. return "Reserved (6)";
  872. case HDMI_COLORSPACE_IDO_DEFINED:
  873. return "IDO Defined";
  874. }
  875. return "Invalid";
  876. }
  877. static const char *hdmi_scan_mode_get_name(enum hdmi_scan_mode scan_mode)
  878. {
  879. switch (scan_mode) {
  880. case HDMI_SCAN_MODE_NONE:
  881. return "No Data";
  882. case HDMI_SCAN_MODE_OVERSCAN:
  883. return "Overscan";
  884. case HDMI_SCAN_MODE_UNDERSCAN:
  885. return "Underscan";
  886. case HDMI_SCAN_MODE_RESERVED:
  887. return "Reserved";
  888. }
  889. return "Invalid";
  890. }
  891. static const char *hdmi_colorimetry_get_name(enum hdmi_colorimetry colorimetry)
  892. {
  893. switch (colorimetry) {
  894. case HDMI_COLORIMETRY_NONE:
  895. return "No Data";
  896. case HDMI_COLORIMETRY_ITU_601:
  897. return "ITU601";
  898. case HDMI_COLORIMETRY_ITU_709:
  899. return "ITU709";
  900. case HDMI_COLORIMETRY_EXTENDED:
  901. return "Extended";
  902. }
  903. return "Invalid";
  904. }
  905. static const char *
  906. hdmi_picture_aspect_get_name(enum hdmi_picture_aspect picture_aspect)
  907. {
  908. switch (picture_aspect) {
  909. case HDMI_PICTURE_ASPECT_NONE:
  910. return "No Data";
  911. case HDMI_PICTURE_ASPECT_4_3:
  912. return "4:3";
  913. case HDMI_PICTURE_ASPECT_16_9:
  914. return "16:9";
  915. case HDMI_PICTURE_ASPECT_64_27:
  916. return "64:27";
  917. case HDMI_PICTURE_ASPECT_256_135:
  918. return "256:135";
  919. case HDMI_PICTURE_ASPECT_RESERVED:
  920. return "Reserved";
  921. }
  922. return "Invalid";
  923. }
  924. static const char *
  925. hdmi_active_aspect_get_name(enum hdmi_active_aspect active_aspect)
  926. {
  927. if (active_aspect < 0 || active_aspect > 0xf)
  928. return "Invalid";
  929. switch (active_aspect) {
  930. case HDMI_ACTIVE_ASPECT_16_9_TOP:
  931. return "16:9 Top";
  932. case HDMI_ACTIVE_ASPECT_14_9_TOP:
  933. return "14:9 Top";
  934. case HDMI_ACTIVE_ASPECT_16_9_CENTER:
  935. return "16:9 Center";
  936. case HDMI_ACTIVE_ASPECT_PICTURE:
  937. return "Same as Picture";
  938. case HDMI_ACTIVE_ASPECT_4_3:
  939. return "4:3";
  940. case HDMI_ACTIVE_ASPECT_16_9:
  941. return "16:9";
  942. case HDMI_ACTIVE_ASPECT_14_9:
  943. return "14:9";
  944. case HDMI_ACTIVE_ASPECT_4_3_SP_14_9:
  945. return "4:3 SP 14:9";
  946. case HDMI_ACTIVE_ASPECT_16_9_SP_14_9:
  947. return "16:9 SP 14:9";
  948. case HDMI_ACTIVE_ASPECT_16_9_SP_4_3:
  949. return "16:9 SP 4:3";
  950. }
  951. return "Reserved";
  952. }
  953. static const char *
  954. hdmi_extended_colorimetry_get_name(enum hdmi_extended_colorimetry ext_col)
  955. {
  956. switch (ext_col) {
  957. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
  958. return "xvYCC 601";
  959. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
  960. return "xvYCC 709";
  961. case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
  962. return "sYCC 601";
  963. case HDMI_EXTENDED_COLORIMETRY_OPYCC_601:
  964. return "opYCC 601";
  965. case HDMI_EXTENDED_COLORIMETRY_OPRGB:
  966. return "opRGB";
  967. case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
  968. return "BT.2020 Constant Luminance";
  969. case HDMI_EXTENDED_COLORIMETRY_BT2020:
  970. return "BT.2020";
  971. case HDMI_EXTENDED_COLORIMETRY_RESERVED:
  972. return "Reserved";
  973. }
  974. return "Invalid";
  975. }
  976. static const char *
  977. hdmi_quantization_range_get_name(enum hdmi_quantization_range qrange)
  978. {
  979. switch (qrange) {
  980. case HDMI_QUANTIZATION_RANGE_DEFAULT:
  981. return "Default";
  982. case HDMI_QUANTIZATION_RANGE_LIMITED:
  983. return "Limited";
  984. case HDMI_QUANTIZATION_RANGE_FULL:
  985. return "Full";
  986. case HDMI_QUANTIZATION_RANGE_RESERVED:
  987. return "Reserved";
  988. }
  989. return "Invalid";
  990. }
  991. static const char *hdmi_nups_get_name(enum hdmi_nups nups)
  992. {
  993. switch (nups) {
  994. case HDMI_NUPS_UNKNOWN:
  995. return "Unknown Non-uniform Scaling";
  996. case HDMI_NUPS_HORIZONTAL:
  997. return "Horizontally Scaled";
  998. case HDMI_NUPS_VERTICAL:
  999. return "Vertically Scaled";
  1000. case HDMI_NUPS_BOTH:
  1001. return "Horizontally and Vertically Scaled";
  1002. }
  1003. return "Invalid";
  1004. }
  1005. static const char *
  1006. hdmi_ycc_quantization_range_get_name(enum hdmi_ycc_quantization_range qrange)
  1007. {
  1008. switch (qrange) {
  1009. case HDMI_YCC_QUANTIZATION_RANGE_LIMITED:
  1010. return "Limited";
  1011. case HDMI_YCC_QUANTIZATION_RANGE_FULL:
  1012. return "Full";
  1013. }
  1014. return "Invalid";
  1015. }
  1016. static const char *
  1017. hdmi_content_type_get_name(enum hdmi_content_type content_type)
  1018. {
  1019. switch (content_type) {
  1020. case HDMI_CONTENT_TYPE_GRAPHICS:
  1021. return "Graphics";
  1022. case HDMI_CONTENT_TYPE_PHOTO:
  1023. return "Photo";
  1024. case HDMI_CONTENT_TYPE_CINEMA:
  1025. return "Cinema";
  1026. case HDMI_CONTENT_TYPE_GAME:
  1027. return "Game";
  1028. }
  1029. return "Invalid";
  1030. }
  1031. static void hdmi_avi_infoframe_log(const char *level,
  1032. struct device *dev,
  1033. const struct hdmi_avi_infoframe *frame)
  1034. {
  1035. hdmi_infoframe_log_header(level, dev,
  1036. (const struct hdmi_any_infoframe *)frame);
  1037. hdmi_log(" colorspace: %s\n",
  1038. hdmi_colorspace_get_name(frame->colorspace));
  1039. hdmi_log(" scan mode: %s\n",
  1040. hdmi_scan_mode_get_name(frame->scan_mode));
  1041. hdmi_log(" colorimetry: %s\n",
  1042. hdmi_colorimetry_get_name(frame->colorimetry));
  1043. hdmi_log(" picture aspect: %s\n",
  1044. hdmi_picture_aspect_get_name(frame->picture_aspect));
  1045. hdmi_log(" active aspect: %s\n",
  1046. hdmi_active_aspect_get_name(frame->active_aspect));
  1047. hdmi_log(" itc: %s\n", frame->itc ? "IT Content" : "No Data");
  1048. hdmi_log(" extended colorimetry: %s\n",
  1049. hdmi_extended_colorimetry_get_name(frame->extended_colorimetry));
  1050. hdmi_log(" quantization range: %s\n",
  1051. hdmi_quantization_range_get_name(frame->quantization_range));
  1052. hdmi_log(" nups: %s\n", hdmi_nups_get_name(frame->nups));
  1053. hdmi_log(" video code: %u\n", frame->video_code);
  1054. hdmi_log(" ycc quantization range: %s\n",
  1055. hdmi_ycc_quantization_range_get_name(frame->ycc_quantization_range));
  1056. hdmi_log(" hdmi content type: %s\n",
  1057. hdmi_content_type_get_name(frame->content_type));
  1058. hdmi_log(" pixel repeat: %u\n", frame->pixel_repeat);
  1059. hdmi_log(" bar top %u, bottom %u, left %u, right %u\n",
  1060. frame->top_bar, frame->bottom_bar,
  1061. frame->left_bar, frame->right_bar);
  1062. }
  1063. static const char *hdmi_spd_sdi_get_name(enum hdmi_spd_sdi sdi)
  1064. {
  1065. if (sdi < 0 || sdi > 0xff)
  1066. return "Invalid";
  1067. switch (sdi) {
  1068. case HDMI_SPD_SDI_UNKNOWN:
  1069. return "Unknown";
  1070. case HDMI_SPD_SDI_DSTB:
  1071. return "Digital STB";
  1072. case HDMI_SPD_SDI_DVDP:
  1073. return "DVD Player";
  1074. case HDMI_SPD_SDI_DVHS:
  1075. return "D-VHS";
  1076. case HDMI_SPD_SDI_HDDVR:
  1077. return "HDD Videorecorder";
  1078. case HDMI_SPD_SDI_DVC:
  1079. return "DVC";
  1080. case HDMI_SPD_SDI_DSC:
  1081. return "DSC";
  1082. case HDMI_SPD_SDI_VCD:
  1083. return "Video CD";
  1084. case HDMI_SPD_SDI_GAME:
  1085. return "Game";
  1086. case HDMI_SPD_SDI_PC:
  1087. return "PC General";
  1088. case HDMI_SPD_SDI_BD:
  1089. return "Blu-Ray Disc (BD)";
  1090. case HDMI_SPD_SDI_SACD:
  1091. return "Super Audio CD";
  1092. case HDMI_SPD_SDI_HDDVD:
  1093. return "HD DVD";
  1094. case HDMI_SPD_SDI_PMP:
  1095. return "PMP";
  1096. }
  1097. return "Reserved";
  1098. }
  1099. static void hdmi_spd_infoframe_log(const char *level,
  1100. struct device *dev,
  1101. const struct hdmi_spd_infoframe *frame)
  1102. {
  1103. u8 buf[17];
  1104. hdmi_infoframe_log_header(level, dev,
  1105. (const struct hdmi_any_infoframe *)frame);
  1106. memset(buf, 0, sizeof(buf));
  1107. strncpy(buf, frame->vendor, 8);
  1108. hdmi_log(" vendor: %s\n", buf);
  1109. strncpy(buf, frame->product, 16);
  1110. hdmi_log(" product: %s\n", buf);
  1111. hdmi_log(" source device information: %s (0x%x)\n",
  1112. hdmi_spd_sdi_get_name(frame->sdi), frame->sdi);
  1113. }
  1114. static const char *
  1115. hdmi_audio_coding_type_get_name(enum hdmi_audio_coding_type coding_type)
  1116. {
  1117. switch (coding_type) {
  1118. case HDMI_AUDIO_CODING_TYPE_STREAM:
  1119. return "Refer to Stream Header";
  1120. case HDMI_AUDIO_CODING_TYPE_PCM:
  1121. return "PCM";
  1122. case HDMI_AUDIO_CODING_TYPE_AC3:
  1123. return "AC-3";
  1124. case HDMI_AUDIO_CODING_TYPE_MPEG1:
  1125. return "MPEG1";
  1126. case HDMI_AUDIO_CODING_TYPE_MP3:
  1127. return "MP3";
  1128. case HDMI_AUDIO_CODING_TYPE_MPEG2:
  1129. return "MPEG2";
  1130. case HDMI_AUDIO_CODING_TYPE_AAC_LC:
  1131. return "AAC";
  1132. case HDMI_AUDIO_CODING_TYPE_DTS:
  1133. return "DTS";
  1134. case HDMI_AUDIO_CODING_TYPE_ATRAC:
  1135. return "ATRAC";
  1136. case HDMI_AUDIO_CODING_TYPE_DSD:
  1137. return "One Bit Audio";
  1138. case HDMI_AUDIO_CODING_TYPE_EAC3:
  1139. return "Dolby Digital +";
  1140. case HDMI_AUDIO_CODING_TYPE_DTS_HD:
  1141. return "DTS-HD";
  1142. case HDMI_AUDIO_CODING_TYPE_MLP:
  1143. return "MAT (MLP)";
  1144. case HDMI_AUDIO_CODING_TYPE_DST:
  1145. return "DST";
  1146. case HDMI_AUDIO_CODING_TYPE_WMA_PRO:
  1147. return "WMA PRO";
  1148. case HDMI_AUDIO_CODING_TYPE_CXT:
  1149. return "Refer to CXT";
  1150. }
  1151. return "Invalid";
  1152. }
  1153. static const char *
  1154. hdmi_audio_sample_size_get_name(enum hdmi_audio_sample_size sample_size)
  1155. {
  1156. switch (sample_size) {
  1157. case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
  1158. return "Refer to Stream Header";
  1159. case HDMI_AUDIO_SAMPLE_SIZE_16:
  1160. return "16 bit";
  1161. case HDMI_AUDIO_SAMPLE_SIZE_20:
  1162. return "20 bit";
  1163. case HDMI_AUDIO_SAMPLE_SIZE_24:
  1164. return "24 bit";
  1165. }
  1166. return "Invalid";
  1167. }
  1168. static const char *
  1169. hdmi_audio_sample_frequency_get_name(enum hdmi_audio_sample_frequency freq)
  1170. {
  1171. switch (freq) {
  1172. case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
  1173. return "Refer to Stream Header";
  1174. case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
  1175. return "32 kHz";
  1176. case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
  1177. return "44.1 kHz (CD)";
  1178. case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
  1179. return "48 kHz";
  1180. case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
  1181. return "88.2 kHz";
  1182. case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
  1183. return "96 kHz";
  1184. case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
  1185. return "176.4 kHz";
  1186. case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
  1187. return "192 kHz";
  1188. }
  1189. return "Invalid";
  1190. }
  1191. static const char *
  1192. hdmi_audio_coding_type_ext_get_name(enum hdmi_audio_coding_type_ext ctx)
  1193. {
  1194. if (ctx < 0 || ctx > 0x1f)
  1195. return "Invalid";
  1196. switch (ctx) {
  1197. case HDMI_AUDIO_CODING_TYPE_EXT_CT:
  1198. return "Refer to CT";
  1199. case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC:
  1200. return "HE AAC";
  1201. case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2:
  1202. return "HE AAC v2";
  1203. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND:
  1204. return "MPEG SURROUND";
  1205. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC:
  1206. return "MPEG-4 HE AAC";
  1207. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_V2:
  1208. return "MPEG-4 HE AAC v2";
  1209. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC:
  1210. return "MPEG-4 AAC LC";
  1211. case HDMI_AUDIO_CODING_TYPE_EXT_DRA:
  1212. return "DRA";
  1213. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_SURROUND:
  1214. return "MPEG-4 HE AAC + MPEG Surround";
  1215. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC_SURROUND:
  1216. return "MPEG-4 AAC LC + MPEG Surround";
  1217. }
  1218. return "Reserved";
  1219. }
  1220. static void hdmi_audio_infoframe_log(const char *level,
  1221. struct device *dev,
  1222. const struct hdmi_audio_infoframe *frame)
  1223. {
  1224. hdmi_infoframe_log_header(level, dev,
  1225. (const struct hdmi_any_infoframe *)frame);
  1226. if (frame->channels)
  1227. hdmi_log(" channels: %u\n", frame->channels - 1);
  1228. else
  1229. hdmi_log(" channels: Refer to stream header\n");
  1230. hdmi_log(" coding type: %s\n",
  1231. hdmi_audio_coding_type_get_name(frame->coding_type));
  1232. hdmi_log(" sample size: %s\n",
  1233. hdmi_audio_sample_size_get_name(frame->sample_size));
  1234. hdmi_log(" sample frequency: %s\n",
  1235. hdmi_audio_sample_frequency_get_name(frame->sample_frequency));
  1236. hdmi_log(" coding type ext: %s\n",
  1237. hdmi_audio_coding_type_ext_get_name(frame->coding_type_ext));
  1238. hdmi_log(" channel allocation: 0x%x\n",
  1239. frame->channel_allocation);
  1240. hdmi_log(" level shift value: %u dB\n",
  1241. frame->level_shift_value);
  1242. hdmi_log(" downmix inhibit: %s\n",
  1243. frame->downmix_inhibit ? "Yes" : "No");
  1244. }
  1245. static void hdmi_drm_infoframe_log(const char *level,
  1246. struct device *dev,
  1247. const struct hdmi_drm_infoframe *frame)
  1248. {
  1249. int i;
  1250. hdmi_infoframe_log_header(level, dev,
  1251. (struct hdmi_any_infoframe *)frame);
  1252. hdmi_log("length: %d\n", frame->length);
  1253. hdmi_log("metadata type: %d\n", frame->metadata_type);
  1254. hdmi_log("eotf: %d\n", frame->eotf);
  1255. for (i = 0; i < 3; i++) {
  1256. hdmi_log("x[%d]: %d\n", i, frame->display_primaries[i].x);
  1257. hdmi_log("y[%d]: %d\n", i, frame->display_primaries[i].y);
  1258. }
  1259. hdmi_log("white point x: %d\n", frame->white_point.x);
  1260. hdmi_log("white point y: %d\n", frame->white_point.y);
  1261. hdmi_log("max_display_mastering_luminance: %d\n",
  1262. frame->max_display_mastering_luminance);
  1263. hdmi_log("min_display_mastering_luminance: %d\n",
  1264. frame->min_display_mastering_luminance);
  1265. hdmi_log("max_cll: %d\n", frame->max_cll);
  1266. hdmi_log("max_fall: %d\n", frame->max_fall);
  1267. }
  1268. static const char *
  1269. hdmi_3d_structure_get_name(enum hdmi_3d_structure s3d_struct)
  1270. {
  1271. if (s3d_struct < 0 || s3d_struct > 0xf)
  1272. return "Invalid";
  1273. switch (s3d_struct) {
  1274. case HDMI_3D_STRUCTURE_FRAME_PACKING:
  1275. return "Frame Packing";
  1276. case HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE:
  1277. return "Field Alternative";
  1278. case HDMI_3D_STRUCTURE_LINE_ALTERNATIVE:
  1279. return "Line Alternative";
  1280. case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL:
  1281. return "Side-by-side (Full)";
  1282. case HDMI_3D_STRUCTURE_L_DEPTH:
  1283. return "L + Depth";
  1284. case HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH:
  1285. return "L + Depth + Graphics + Graphics-depth";
  1286. case HDMI_3D_STRUCTURE_TOP_AND_BOTTOM:
  1287. return "Top-and-Bottom";
  1288. case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF:
  1289. return "Side-by-side (Half)";
  1290. default:
  1291. break;
  1292. }
  1293. return "Reserved";
  1294. }
  1295. static void
  1296. hdmi_vendor_any_infoframe_log(const char *level,
  1297. struct device *dev,
  1298. const union hdmi_vendor_any_infoframe *frame)
  1299. {
  1300. const struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
  1301. hdmi_infoframe_log_header(level, dev,
  1302. (const struct hdmi_any_infoframe *)frame);
  1303. if (frame->any.oui != HDMI_IEEE_OUI) {
  1304. hdmi_log(" not a HDMI vendor infoframe\n");
  1305. return;
  1306. }
  1307. if (hvf->vic == 0 && hvf->s3d_struct == HDMI_3D_STRUCTURE_INVALID) {
  1308. hdmi_log(" empty frame\n");
  1309. return;
  1310. }
  1311. if (hvf->vic)
  1312. hdmi_log(" HDMI VIC: %u\n", hvf->vic);
  1313. if (hvf->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
  1314. hdmi_log(" 3D structure: %s\n",
  1315. hdmi_3d_structure_get_name(hvf->s3d_struct));
  1316. if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  1317. hdmi_log(" 3D extension data: %d\n",
  1318. hvf->s3d_ext_data);
  1319. }
  1320. }
  1321. /**
  1322. * hdmi_infoframe_log() - log info of HDMI infoframe
  1323. * @level: logging level
  1324. * @dev: device
  1325. * @frame: HDMI infoframe
  1326. */
  1327. void hdmi_infoframe_log(const char *level,
  1328. struct device *dev,
  1329. const union hdmi_infoframe *frame)
  1330. {
  1331. switch (frame->any.type) {
  1332. case HDMI_INFOFRAME_TYPE_AVI:
  1333. hdmi_avi_infoframe_log(level, dev, &frame->avi);
  1334. break;
  1335. case HDMI_INFOFRAME_TYPE_SPD:
  1336. hdmi_spd_infoframe_log(level, dev, &frame->spd);
  1337. break;
  1338. case HDMI_INFOFRAME_TYPE_AUDIO:
  1339. hdmi_audio_infoframe_log(level, dev, &frame->audio);
  1340. break;
  1341. case HDMI_INFOFRAME_TYPE_VENDOR:
  1342. hdmi_vendor_any_infoframe_log(level, dev, &frame->vendor);
  1343. break;
  1344. case HDMI_INFOFRAME_TYPE_DRM:
  1345. hdmi_drm_infoframe_log(level, dev, &frame->drm);
  1346. break;
  1347. }
  1348. }
  1349. EXPORT_SYMBOL(hdmi_infoframe_log);
  1350. /**
  1351. * hdmi_avi_infoframe_unpack() - unpack binary buffer to a HDMI AVI infoframe
  1352. * @frame: HDMI AVI infoframe
  1353. * @buffer: source buffer
  1354. * @size: size of buffer
  1355. *
  1356. * Unpacks the information contained in binary @buffer into a structured
  1357. * @frame of the HDMI Auxiliary Video (AVI) information frame.
  1358. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1359. * specification.
  1360. *
  1361. * Returns 0 on success or a negative error code on failure.
  1362. */
  1363. static int hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe *frame,
  1364. const void *buffer, size_t size)
  1365. {
  1366. const u8 *ptr = buffer;
  1367. int ret;
  1368. if (size < HDMI_INFOFRAME_SIZE(AVI))
  1369. return -EINVAL;
  1370. if (ptr[0] != HDMI_INFOFRAME_TYPE_AVI ||
  1371. ptr[1] != 2 ||
  1372. ptr[2] != HDMI_AVI_INFOFRAME_SIZE)
  1373. return -EINVAL;
  1374. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AVI)) != 0)
  1375. return -EINVAL;
  1376. ret = hdmi_avi_infoframe_init(frame);
  1377. if (ret)
  1378. return ret;
  1379. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1380. frame->colorspace = (ptr[0] >> 5) & 0x3;
  1381. if (ptr[0] & 0x10)
  1382. frame->active_aspect = ptr[1] & 0xf;
  1383. if (ptr[0] & 0x8) {
  1384. frame->top_bar = (ptr[6] << 8) | ptr[5];
  1385. frame->bottom_bar = (ptr[8] << 8) | ptr[7];
  1386. }
  1387. if (ptr[0] & 0x4) {
  1388. frame->left_bar = (ptr[10] << 8) | ptr[9];
  1389. frame->right_bar = (ptr[12] << 8) | ptr[11];
  1390. }
  1391. frame->scan_mode = ptr[0] & 0x3;
  1392. frame->colorimetry = (ptr[1] >> 6) & 0x3;
  1393. frame->picture_aspect = (ptr[1] >> 4) & 0x3;
  1394. frame->active_aspect = ptr[1] & 0xf;
  1395. frame->itc = ptr[2] & 0x80 ? true : false;
  1396. frame->extended_colorimetry = (ptr[2] >> 4) & 0x7;
  1397. frame->quantization_range = (ptr[2] >> 2) & 0x3;
  1398. frame->nups = ptr[2] & 0x3;
  1399. frame->video_code = ptr[3] & 0x7f;
  1400. frame->ycc_quantization_range = (ptr[4] >> 6) & 0x3;
  1401. frame->content_type = (ptr[4] >> 4) & 0x3;
  1402. frame->pixel_repeat = ptr[4] & 0xf;
  1403. return 0;
  1404. }
  1405. /**
  1406. * hdmi_spd_infoframe_unpack() - unpack binary buffer to a HDMI SPD infoframe
  1407. * @frame: HDMI SPD infoframe
  1408. * @buffer: source buffer
  1409. * @size: size of buffer
  1410. *
  1411. * Unpacks the information contained in binary @buffer into a structured
  1412. * @frame of the HDMI Source Product Description (SPD) information frame.
  1413. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1414. * specification.
  1415. *
  1416. * Returns 0 on success or a negative error code on failure.
  1417. */
  1418. static int hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe *frame,
  1419. const void *buffer, size_t size)
  1420. {
  1421. const u8 *ptr = buffer;
  1422. int ret;
  1423. if (size < HDMI_INFOFRAME_SIZE(SPD))
  1424. return -EINVAL;
  1425. if (ptr[0] != HDMI_INFOFRAME_TYPE_SPD ||
  1426. ptr[1] != 1 ||
  1427. ptr[2] != HDMI_SPD_INFOFRAME_SIZE) {
  1428. return -EINVAL;
  1429. }
  1430. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(SPD)) != 0)
  1431. return -EINVAL;
  1432. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1433. ret = hdmi_spd_infoframe_init(frame, ptr, ptr + 8);
  1434. if (ret)
  1435. return ret;
  1436. frame->sdi = ptr[24];
  1437. return 0;
  1438. }
  1439. /**
  1440. * hdmi_audio_infoframe_unpack() - unpack binary buffer to a HDMI AUDIO infoframe
  1441. * @frame: HDMI Audio infoframe
  1442. * @buffer: source buffer
  1443. * @size: size of buffer
  1444. *
  1445. * Unpacks the information contained in binary @buffer into a structured
  1446. * @frame of the HDMI Audio information frame.
  1447. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1448. * specification.
  1449. *
  1450. * Returns 0 on success or a negative error code on failure.
  1451. */
  1452. static int hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe *frame,
  1453. const void *buffer, size_t size)
  1454. {
  1455. const u8 *ptr = buffer;
  1456. int ret;
  1457. if (size < HDMI_INFOFRAME_SIZE(AUDIO))
  1458. return -EINVAL;
  1459. if (ptr[0] != HDMI_INFOFRAME_TYPE_AUDIO ||
  1460. ptr[1] != 1 ||
  1461. ptr[2] != HDMI_AUDIO_INFOFRAME_SIZE) {
  1462. return -EINVAL;
  1463. }
  1464. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AUDIO)) != 0)
  1465. return -EINVAL;
  1466. ret = hdmi_audio_infoframe_init(frame);
  1467. if (ret)
  1468. return ret;
  1469. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1470. frame->channels = ptr[0] & 0x7;
  1471. frame->coding_type = (ptr[0] >> 4) & 0xf;
  1472. frame->sample_size = ptr[1] & 0x3;
  1473. frame->sample_frequency = (ptr[1] >> 2) & 0x7;
  1474. frame->coding_type_ext = ptr[2] & 0x1f;
  1475. frame->channel_allocation = ptr[3];
  1476. frame->level_shift_value = (ptr[4] >> 3) & 0xf;
  1477. frame->downmix_inhibit = ptr[4] & 0x80 ? true : false;
  1478. return 0;
  1479. }
  1480. /**
  1481. * hdmi_vendor_infoframe_unpack() - unpack binary buffer to a HDMI vendor infoframe
  1482. * @frame: HDMI Vendor infoframe
  1483. * @buffer: source buffer
  1484. * @size: size of buffer
  1485. *
  1486. * Unpacks the information contained in binary @buffer into a structured
  1487. * @frame of the HDMI Vendor information frame.
  1488. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1489. * specification.
  1490. *
  1491. * Returns 0 on success or a negative error code on failure.
  1492. */
  1493. static int
  1494. hdmi_vendor_any_infoframe_unpack(union hdmi_vendor_any_infoframe *frame,
  1495. const void *buffer, size_t size)
  1496. {
  1497. const u8 *ptr = buffer;
  1498. size_t length;
  1499. int ret;
  1500. u8 hdmi_video_format;
  1501. struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
  1502. if (size < HDMI_INFOFRAME_HEADER_SIZE)
  1503. return -EINVAL;
  1504. if (ptr[0] != HDMI_INFOFRAME_TYPE_VENDOR ||
  1505. ptr[1] != 1 ||
  1506. (ptr[2] != 4 && ptr[2] != 5 && ptr[2] != 6))
  1507. return -EINVAL;
  1508. length = ptr[2];
  1509. if (size < HDMI_INFOFRAME_HEADER_SIZE + length)
  1510. return -EINVAL;
  1511. if (hdmi_infoframe_checksum(buffer,
  1512. HDMI_INFOFRAME_HEADER_SIZE + length) != 0)
  1513. return -EINVAL;
  1514. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1515. /* HDMI OUI */
  1516. if ((ptr[0] != 0x03) ||
  1517. (ptr[1] != 0x0c) ||
  1518. (ptr[2] != 0x00))
  1519. return -EINVAL;
  1520. hdmi_video_format = ptr[3] >> 5;
  1521. if (hdmi_video_format > 0x2)
  1522. return -EINVAL;
  1523. ret = hdmi_vendor_infoframe_init(hvf);
  1524. if (ret)
  1525. return ret;
  1526. hvf->length = length;
  1527. if (hdmi_video_format == 0x2) {
  1528. if (length != 5 && length != 6)
  1529. return -EINVAL;
  1530. hvf->s3d_struct = ptr[4] >> 4;
  1531. if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF) {
  1532. if (length != 6)
  1533. return -EINVAL;
  1534. hvf->s3d_ext_data = ptr[5] >> 4;
  1535. }
  1536. } else if (hdmi_video_format == 0x1) {
  1537. if (length != 5)
  1538. return -EINVAL;
  1539. hvf->vic = ptr[4];
  1540. } else {
  1541. if (length != 4)
  1542. return -EINVAL;
  1543. }
  1544. return 0;
  1545. }
  1546. /**
  1547. * hdmi_drm_infoframe_unpack() - unpack binary buffer to a HDMI DRM infoframe
  1548. * @frame: HDMI DRM infoframe
  1549. * @buffer: source buffer
  1550. * @size: size of buffer
  1551. *
  1552. * Unpacks the information contained in binary @buffer into a structured
  1553. * @frame of the HDMI Dynamic Range and Mastering (DRM) information frame.
  1554. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1555. * specification.
  1556. *
  1557. * Returns 0 on success or a negative error code on failure.
  1558. */
  1559. static int hdmi_drm_infoframe_unpack(struct hdmi_drm_infoframe *frame,
  1560. const void *buffer, size_t size)
  1561. {
  1562. const u8 *ptr = buffer;
  1563. const u8 *temp;
  1564. u8 x_lsb, x_msb;
  1565. u8 y_lsb, y_msb;
  1566. int ret;
  1567. int i;
  1568. if (size < HDMI_INFOFRAME_SIZE(DRM))
  1569. return -EINVAL;
  1570. if (ptr[0] != HDMI_INFOFRAME_TYPE_DRM ||
  1571. ptr[1] != 1 ||
  1572. ptr[2] != HDMI_DRM_INFOFRAME_SIZE)
  1573. return -EINVAL;
  1574. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(DRM)) != 0)
  1575. return -EINVAL;
  1576. ret = hdmi_drm_infoframe_init(frame);
  1577. if (ret)
  1578. return ret;
  1579. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1580. frame->eotf = ptr[0] & 0x7;
  1581. frame->metadata_type = ptr[1] & 0x7;
  1582. temp = ptr + 2;
  1583. for (i = 0; i < 3; i++) {
  1584. x_lsb = *temp++;
  1585. x_msb = *temp++;
  1586. frame->display_primaries[i].x = (x_msb << 8) | x_lsb;
  1587. y_lsb = *temp++;
  1588. y_msb = *temp++;
  1589. frame->display_primaries[i].y = (y_msb << 8) | y_lsb;
  1590. }
  1591. frame->white_point.x = (ptr[15] << 8) | ptr[14];
  1592. frame->white_point.y = (ptr[17] << 8) | ptr[16];
  1593. frame->max_display_mastering_luminance = (ptr[19] << 8) | ptr[18];
  1594. frame->min_display_mastering_luminance = (ptr[21] << 8) | ptr[20];
  1595. frame->max_cll = (ptr[23] << 8) | ptr[22];
  1596. frame->max_fall = (ptr[25] << 8) | ptr[24];
  1597. return 0;
  1598. }
  1599. /**
  1600. * hdmi_infoframe_unpack() - unpack binary buffer to a HDMI infoframe
  1601. * @frame: HDMI infoframe
  1602. * @buffer: source buffer
  1603. * @size: size of buffer
  1604. *
  1605. * Unpacks the information contained in binary buffer @buffer into a structured
  1606. * @frame of a HDMI infoframe.
  1607. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1608. * specification.
  1609. *
  1610. * Returns 0 on success or a negative error code on failure.
  1611. */
  1612. int hdmi_infoframe_unpack(union hdmi_infoframe *frame,
  1613. const void *buffer, size_t size)
  1614. {
  1615. int ret;
  1616. const u8 *ptr = buffer;
  1617. if (size < HDMI_INFOFRAME_HEADER_SIZE)
  1618. return -EINVAL;
  1619. switch (ptr[0]) {
  1620. case HDMI_INFOFRAME_TYPE_AVI:
  1621. ret = hdmi_avi_infoframe_unpack(&frame->avi, buffer, size);
  1622. break;
  1623. case HDMI_INFOFRAME_TYPE_DRM:
  1624. ret = hdmi_drm_infoframe_unpack(&frame->drm, buffer, size);
  1625. break;
  1626. case HDMI_INFOFRAME_TYPE_SPD:
  1627. ret = hdmi_spd_infoframe_unpack(&frame->spd, buffer, size);
  1628. break;
  1629. case HDMI_INFOFRAME_TYPE_AUDIO:
  1630. ret = hdmi_audio_infoframe_unpack(&frame->audio, buffer, size);
  1631. break;
  1632. case HDMI_INFOFRAME_TYPE_VENDOR:
  1633. ret = hdmi_vendor_any_infoframe_unpack(&frame->vendor, buffer, size);
  1634. break;
  1635. default:
  1636. ret = -EINVAL;
  1637. break;
  1638. }
  1639. return ret;
  1640. }
  1641. EXPORT_SYMBOL(hdmi_infoframe_unpack);