hdmi.c 52 KB

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