hdmi.c 51 KB

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