hdmi.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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_check() - check a HDMI infoframe
  749. * @frame: HDMI infoframe
  750. *
  751. * Validates that the infoframe is consistent and updates derived fields
  752. * (eg. length) based on other fields.
  753. *
  754. * Returns 0 on success or a negative error code on failure.
  755. */
  756. int
  757. hdmi_infoframe_check(union hdmi_infoframe *frame)
  758. {
  759. switch (frame->any.type) {
  760. case HDMI_INFOFRAME_TYPE_AVI:
  761. return hdmi_avi_infoframe_check(&frame->avi);
  762. case HDMI_INFOFRAME_TYPE_SPD:
  763. return hdmi_spd_infoframe_check(&frame->spd);
  764. case HDMI_INFOFRAME_TYPE_AUDIO:
  765. return hdmi_audio_infoframe_check(&frame->audio);
  766. case HDMI_INFOFRAME_TYPE_VENDOR:
  767. return hdmi_vendor_any_infoframe_check(&frame->vendor);
  768. default:
  769. WARN(1, "Bad infoframe type %d\n", frame->any.type);
  770. return -EINVAL;
  771. }
  772. }
  773. EXPORT_SYMBOL(hdmi_infoframe_check);
  774. /**
  775. * hdmi_infoframe_pack_only() - write a HDMI infoframe to binary buffer
  776. * @frame: HDMI infoframe
  777. * @buffer: destination buffer
  778. * @size: size of buffer
  779. *
  780. * Packs the information contained in the @frame structure into a binary
  781. * representation that can be written into the corresponding controller
  782. * registers. Also computes the checksum as required by section 5.3.5 of
  783. * the HDMI 1.4 specification.
  784. *
  785. * Returns the number of bytes packed into the binary buffer or a negative
  786. * error code on failure.
  787. */
  788. ssize_t
  789. hdmi_infoframe_pack_only(const union hdmi_infoframe *frame, void *buffer, size_t size)
  790. {
  791. ssize_t length;
  792. switch (frame->any.type) {
  793. case HDMI_INFOFRAME_TYPE_AVI:
  794. length = hdmi_avi_infoframe_pack_only(&frame->avi,
  795. buffer, size);
  796. break;
  797. case HDMI_INFOFRAME_TYPE_DRM:
  798. length = hdmi_drm_infoframe_pack_only(&frame->drm,
  799. buffer, size);
  800. break;
  801. case HDMI_INFOFRAME_TYPE_SPD:
  802. length = hdmi_spd_infoframe_pack_only(&frame->spd,
  803. buffer, size);
  804. break;
  805. case HDMI_INFOFRAME_TYPE_AUDIO:
  806. length = hdmi_audio_infoframe_pack_only(&frame->audio,
  807. buffer, size);
  808. break;
  809. case HDMI_INFOFRAME_TYPE_VENDOR:
  810. length = hdmi_vendor_any_infoframe_pack_only(&frame->vendor,
  811. buffer, size);
  812. break;
  813. default:
  814. WARN(1, "Bad infoframe type %d\n", frame->any.type);
  815. length = -EINVAL;
  816. }
  817. return length;
  818. }
  819. EXPORT_SYMBOL(hdmi_infoframe_pack_only);
  820. /**
  821. * hdmi_infoframe_pack() - check a HDMI infoframe,
  822. * and write it to binary buffer
  823. * @frame: HDMI infoframe
  824. * @buffer: destination buffer
  825. * @size: size of buffer
  826. *
  827. * Validates that the infoframe is consistent and updates derived fields
  828. * (eg. length) based on other fields, after which it packs the information
  829. * contained in the @frame structure into a binary representation that
  830. * can be written into the corresponding controller registers. This function
  831. * also computes the checksum as required by section 5.3.5 of the HDMI 1.4
  832. * specification.
  833. *
  834. * Returns the number of bytes packed into the binary buffer or a negative
  835. * error code on failure.
  836. */
  837. ssize_t
  838. hdmi_infoframe_pack(union hdmi_infoframe *frame,
  839. void *buffer, size_t size)
  840. {
  841. ssize_t length;
  842. switch (frame->any.type) {
  843. case HDMI_INFOFRAME_TYPE_AVI:
  844. length = hdmi_avi_infoframe_pack(&frame->avi, buffer, size);
  845. break;
  846. case HDMI_INFOFRAME_TYPE_DRM:
  847. length = hdmi_drm_infoframe_pack(&frame->drm, buffer, size);
  848. break;
  849. case HDMI_INFOFRAME_TYPE_SPD:
  850. length = hdmi_spd_infoframe_pack(&frame->spd, buffer, size);
  851. break;
  852. case HDMI_INFOFRAME_TYPE_AUDIO:
  853. length = hdmi_audio_infoframe_pack(&frame->audio, buffer, size);
  854. break;
  855. case HDMI_INFOFRAME_TYPE_VENDOR:
  856. length = hdmi_vendor_any_infoframe_pack(&frame->vendor,
  857. buffer, size);
  858. break;
  859. default:
  860. WARN(1, "Bad infoframe type %d\n", frame->any.type);
  861. length = -EINVAL;
  862. }
  863. return length;
  864. }
  865. EXPORT_SYMBOL(hdmi_infoframe_pack);
  866. static const char *hdmi_infoframe_type_get_name(enum hdmi_infoframe_type type)
  867. {
  868. if (type < 0x80 || type > 0x9f)
  869. return "Invalid";
  870. switch (type) {
  871. case HDMI_INFOFRAME_TYPE_VENDOR:
  872. return "Vendor";
  873. case HDMI_INFOFRAME_TYPE_AVI:
  874. return "Auxiliary Video Information (AVI)";
  875. case HDMI_INFOFRAME_TYPE_SPD:
  876. return "Source Product Description (SPD)";
  877. case HDMI_INFOFRAME_TYPE_AUDIO:
  878. return "Audio";
  879. case HDMI_INFOFRAME_TYPE_DRM:
  880. return "Dynamic Range and Mastering";
  881. }
  882. return "Reserved";
  883. }
  884. static void hdmi_infoframe_log_header(const char *level,
  885. struct device *dev,
  886. const struct hdmi_any_infoframe *frame)
  887. {
  888. hdmi_log("HDMI infoframe: %s, version %u, length %u\n",
  889. hdmi_infoframe_type_get_name(frame->type),
  890. frame->version, frame->length);
  891. }
  892. static const char *hdmi_colorspace_get_name(enum hdmi_colorspace colorspace)
  893. {
  894. switch (colorspace) {
  895. case HDMI_COLORSPACE_RGB:
  896. return "RGB";
  897. case HDMI_COLORSPACE_YUV422:
  898. return "YCbCr 4:2:2";
  899. case HDMI_COLORSPACE_YUV444:
  900. return "YCbCr 4:4:4";
  901. case HDMI_COLORSPACE_YUV420:
  902. return "YCbCr 4:2:0";
  903. case HDMI_COLORSPACE_RESERVED4:
  904. return "Reserved (4)";
  905. case HDMI_COLORSPACE_RESERVED5:
  906. return "Reserved (5)";
  907. case HDMI_COLORSPACE_RESERVED6:
  908. return "Reserved (6)";
  909. case HDMI_COLORSPACE_IDO_DEFINED:
  910. return "IDO Defined";
  911. }
  912. return "Invalid";
  913. }
  914. static const char *hdmi_scan_mode_get_name(enum hdmi_scan_mode scan_mode)
  915. {
  916. switch (scan_mode) {
  917. case HDMI_SCAN_MODE_NONE:
  918. return "No Data";
  919. case HDMI_SCAN_MODE_OVERSCAN:
  920. return "Overscan";
  921. case HDMI_SCAN_MODE_UNDERSCAN:
  922. return "Underscan";
  923. case HDMI_SCAN_MODE_RESERVED:
  924. return "Reserved";
  925. }
  926. return "Invalid";
  927. }
  928. static const char *hdmi_colorimetry_get_name(enum hdmi_colorimetry colorimetry)
  929. {
  930. switch (colorimetry) {
  931. case HDMI_COLORIMETRY_NONE:
  932. return "No Data";
  933. case HDMI_COLORIMETRY_ITU_601:
  934. return "ITU601";
  935. case HDMI_COLORIMETRY_ITU_709:
  936. return "ITU709";
  937. case HDMI_COLORIMETRY_EXTENDED:
  938. return "Extended";
  939. }
  940. return "Invalid";
  941. }
  942. static const char *
  943. hdmi_picture_aspect_get_name(enum hdmi_picture_aspect picture_aspect)
  944. {
  945. switch (picture_aspect) {
  946. case HDMI_PICTURE_ASPECT_NONE:
  947. return "No Data";
  948. case HDMI_PICTURE_ASPECT_4_3:
  949. return "4:3";
  950. case HDMI_PICTURE_ASPECT_16_9:
  951. return "16:9";
  952. case HDMI_PICTURE_ASPECT_64_27:
  953. return "64:27";
  954. case HDMI_PICTURE_ASPECT_256_135:
  955. return "256:135";
  956. case HDMI_PICTURE_ASPECT_RESERVED:
  957. return "Reserved";
  958. }
  959. return "Invalid";
  960. }
  961. static const char *
  962. hdmi_active_aspect_get_name(enum hdmi_active_aspect active_aspect)
  963. {
  964. if (active_aspect < 0 || active_aspect > 0xf)
  965. return "Invalid";
  966. switch (active_aspect) {
  967. case HDMI_ACTIVE_ASPECT_16_9_TOP:
  968. return "16:9 Top";
  969. case HDMI_ACTIVE_ASPECT_14_9_TOP:
  970. return "14:9 Top";
  971. case HDMI_ACTIVE_ASPECT_16_9_CENTER:
  972. return "16:9 Center";
  973. case HDMI_ACTIVE_ASPECT_PICTURE:
  974. return "Same as Picture";
  975. case HDMI_ACTIVE_ASPECT_4_3:
  976. return "4:3";
  977. case HDMI_ACTIVE_ASPECT_16_9:
  978. return "16:9";
  979. case HDMI_ACTIVE_ASPECT_14_9:
  980. return "14:9";
  981. case HDMI_ACTIVE_ASPECT_4_3_SP_14_9:
  982. return "4:3 SP 14:9";
  983. case HDMI_ACTIVE_ASPECT_16_9_SP_14_9:
  984. return "16:9 SP 14:9";
  985. case HDMI_ACTIVE_ASPECT_16_9_SP_4_3:
  986. return "16:9 SP 4:3";
  987. }
  988. return "Reserved";
  989. }
  990. static const char *
  991. hdmi_extended_colorimetry_get_name(enum hdmi_extended_colorimetry ext_col)
  992. {
  993. switch (ext_col) {
  994. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
  995. return "xvYCC 601";
  996. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
  997. return "xvYCC 709";
  998. case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
  999. return "sYCC 601";
  1000. case HDMI_EXTENDED_COLORIMETRY_OPYCC_601:
  1001. return "opYCC 601";
  1002. case HDMI_EXTENDED_COLORIMETRY_OPRGB:
  1003. return "opRGB";
  1004. case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
  1005. return "BT.2020 Constant Luminance";
  1006. case HDMI_EXTENDED_COLORIMETRY_BT2020:
  1007. return "BT.2020";
  1008. case HDMI_EXTENDED_COLORIMETRY_RESERVED:
  1009. return "Reserved";
  1010. }
  1011. return "Invalid";
  1012. }
  1013. static const char *
  1014. hdmi_quantization_range_get_name(enum hdmi_quantization_range qrange)
  1015. {
  1016. switch (qrange) {
  1017. case HDMI_QUANTIZATION_RANGE_DEFAULT:
  1018. return "Default";
  1019. case HDMI_QUANTIZATION_RANGE_LIMITED:
  1020. return "Limited";
  1021. case HDMI_QUANTIZATION_RANGE_FULL:
  1022. return "Full";
  1023. case HDMI_QUANTIZATION_RANGE_RESERVED:
  1024. return "Reserved";
  1025. }
  1026. return "Invalid";
  1027. }
  1028. static const char *hdmi_nups_get_name(enum hdmi_nups nups)
  1029. {
  1030. switch (nups) {
  1031. case HDMI_NUPS_UNKNOWN:
  1032. return "Unknown Non-uniform Scaling";
  1033. case HDMI_NUPS_HORIZONTAL:
  1034. return "Horizontally Scaled";
  1035. case HDMI_NUPS_VERTICAL:
  1036. return "Vertically Scaled";
  1037. case HDMI_NUPS_BOTH:
  1038. return "Horizontally and Vertically Scaled";
  1039. }
  1040. return "Invalid";
  1041. }
  1042. static const char *
  1043. hdmi_ycc_quantization_range_get_name(enum hdmi_ycc_quantization_range qrange)
  1044. {
  1045. switch (qrange) {
  1046. case HDMI_YCC_QUANTIZATION_RANGE_LIMITED:
  1047. return "Limited";
  1048. case HDMI_YCC_QUANTIZATION_RANGE_FULL:
  1049. return "Full";
  1050. }
  1051. return "Invalid";
  1052. }
  1053. static const char *
  1054. hdmi_content_type_get_name(enum hdmi_content_type content_type)
  1055. {
  1056. switch (content_type) {
  1057. case HDMI_CONTENT_TYPE_GRAPHICS:
  1058. return "Graphics";
  1059. case HDMI_CONTENT_TYPE_PHOTO:
  1060. return "Photo";
  1061. case HDMI_CONTENT_TYPE_CINEMA:
  1062. return "Cinema";
  1063. case HDMI_CONTENT_TYPE_GAME:
  1064. return "Game";
  1065. }
  1066. return "Invalid";
  1067. }
  1068. static void hdmi_avi_infoframe_log(const char *level,
  1069. struct device *dev,
  1070. const struct hdmi_avi_infoframe *frame)
  1071. {
  1072. hdmi_infoframe_log_header(level, dev,
  1073. (const struct hdmi_any_infoframe *)frame);
  1074. hdmi_log(" colorspace: %s\n",
  1075. hdmi_colorspace_get_name(frame->colorspace));
  1076. hdmi_log(" scan mode: %s\n",
  1077. hdmi_scan_mode_get_name(frame->scan_mode));
  1078. hdmi_log(" colorimetry: %s\n",
  1079. hdmi_colorimetry_get_name(frame->colorimetry));
  1080. hdmi_log(" picture aspect: %s\n",
  1081. hdmi_picture_aspect_get_name(frame->picture_aspect));
  1082. hdmi_log(" active aspect: %s\n",
  1083. hdmi_active_aspect_get_name(frame->active_aspect));
  1084. hdmi_log(" itc: %s\n", frame->itc ? "IT Content" : "No Data");
  1085. hdmi_log(" extended colorimetry: %s\n",
  1086. hdmi_extended_colorimetry_get_name(frame->extended_colorimetry));
  1087. hdmi_log(" quantization range: %s\n",
  1088. hdmi_quantization_range_get_name(frame->quantization_range));
  1089. hdmi_log(" nups: %s\n", hdmi_nups_get_name(frame->nups));
  1090. hdmi_log(" video code: %u\n", frame->video_code);
  1091. hdmi_log(" ycc quantization range: %s\n",
  1092. hdmi_ycc_quantization_range_get_name(frame->ycc_quantization_range));
  1093. hdmi_log(" hdmi content type: %s\n",
  1094. hdmi_content_type_get_name(frame->content_type));
  1095. hdmi_log(" pixel repeat: %u\n", frame->pixel_repeat);
  1096. hdmi_log(" bar top %u, bottom %u, left %u, right %u\n",
  1097. frame->top_bar, frame->bottom_bar,
  1098. frame->left_bar, frame->right_bar);
  1099. }
  1100. static const char *hdmi_spd_sdi_get_name(enum hdmi_spd_sdi sdi)
  1101. {
  1102. if (sdi < 0 || sdi > 0xff)
  1103. return "Invalid";
  1104. switch (sdi) {
  1105. case HDMI_SPD_SDI_UNKNOWN:
  1106. return "Unknown";
  1107. case HDMI_SPD_SDI_DSTB:
  1108. return "Digital STB";
  1109. case HDMI_SPD_SDI_DVDP:
  1110. return "DVD Player";
  1111. case HDMI_SPD_SDI_DVHS:
  1112. return "D-VHS";
  1113. case HDMI_SPD_SDI_HDDVR:
  1114. return "HDD Videorecorder";
  1115. case HDMI_SPD_SDI_DVC:
  1116. return "DVC";
  1117. case HDMI_SPD_SDI_DSC:
  1118. return "DSC";
  1119. case HDMI_SPD_SDI_VCD:
  1120. return "Video CD";
  1121. case HDMI_SPD_SDI_GAME:
  1122. return "Game";
  1123. case HDMI_SPD_SDI_PC:
  1124. return "PC General";
  1125. case HDMI_SPD_SDI_BD:
  1126. return "Blu-Ray Disc (BD)";
  1127. case HDMI_SPD_SDI_SACD:
  1128. return "Super Audio CD";
  1129. case HDMI_SPD_SDI_HDDVD:
  1130. return "HD DVD";
  1131. case HDMI_SPD_SDI_PMP:
  1132. return "PMP";
  1133. }
  1134. return "Reserved";
  1135. }
  1136. static void hdmi_spd_infoframe_log(const char *level,
  1137. struct device *dev,
  1138. const struct hdmi_spd_infoframe *frame)
  1139. {
  1140. u8 buf[17];
  1141. hdmi_infoframe_log_header(level, dev,
  1142. (const struct hdmi_any_infoframe *)frame);
  1143. memset(buf, 0, sizeof(buf));
  1144. strncpy(buf, frame->vendor, 8);
  1145. hdmi_log(" vendor: %s\n", buf);
  1146. strncpy(buf, frame->product, 16);
  1147. hdmi_log(" product: %s\n", buf);
  1148. hdmi_log(" source device information: %s (0x%x)\n",
  1149. hdmi_spd_sdi_get_name(frame->sdi), frame->sdi);
  1150. }
  1151. static const char *
  1152. hdmi_audio_coding_type_get_name(enum hdmi_audio_coding_type coding_type)
  1153. {
  1154. switch (coding_type) {
  1155. case HDMI_AUDIO_CODING_TYPE_STREAM:
  1156. return "Refer to Stream Header";
  1157. case HDMI_AUDIO_CODING_TYPE_PCM:
  1158. return "PCM";
  1159. case HDMI_AUDIO_CODING_TYPE_AC3:
  1160. return "AC-3";
  1161. case HDMI_AUDIO_CODING_TYPE_MPEG1:
  1162. return "MPEG1";
  1163. case HDMI_AUDIO_CODING_TYPE_MP3:
  1164. return "MP3";
  1165. case HDMI_AUDIO_CODING_TYPE_MPEG2:
  1166. return "MPEG2";
  1167. case HDMI_AUDIO_CODING_TYPE_AAC_LC:
  1168. return "AAC";
  1169. case HDMI_AUDIO_CODING_TYPE_DTS:
  1170. return "DTS";
  1171. case HDMI_AUDIO_CODING_TYPE_ATRAC:
  1172. return "ATRAC";
  1173. case HDMI_AUDIO_CODING_TYPE_DSD:
  1174. return "One Bit Audio";
  1175. case HDMI_AUDIO_CODING_TYPE_EAC3:
  1176. return "Dolby Digital +";
  1177. case HDMI_AUDIO_CODING_TYPE_DTS_HD:
  1178. return "DTS-HD";
  1179. case HDMI_AUDIO_CODING_TYPE_MLP:
  1180. return "MAT (MLP)";
  1181. case HDMI_AUDIO_CODING_TYPE_DST:
  1182. return "DST";
  1183. case HDMI_AUDIO_CODING_TYPE_WMA_PRO:
  1184. return "WMA PRO";
  1185. case HDMI_AUDIO_CODING_TYPE_CXT:
  1186. return "Refer to CXT";
  1187. }
  1188. return "Invalid";
  1189. }
  1190. static const char *
  1191. hdmi_audio_sample_size_get_name(enum hdmi_audio_sample_size sample_size)
  1192. {
  1193. switch (sample_size) {
  1194. case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
  1195. return "Refer to Stream Header";
  1196. case HDMI_AUDIO_SAMPLE_SIZE_16:
  1197. return "16 bit";
  1198. case HDMI_AUDIO_SAMPLE_SIZE_20:
  1199. return "20 bit";
  1200. case HDMI_AUDIO_SAMPLE_SIZE_24:
  1201. return "24 bit";
  1202. }
  1203. return "Invalid";
  1204. }
  1205. static const char *
  1206. hdmi_audio_sample_frequency_get_name(enum hdmi_audio_sample_frequency freq)
  1207. {
  1208. switch (freq) {
  1209. case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
  1210. return "Refer to Stream Header";
  1211. case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
  1212. return "32 kHz";
  1213. case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
  1214. return "44.1 kHz (CD)";
  1215. case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
  1216. return "48 kHz";
  1217. case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
  1218. return "88.2 kHz";
  1219. case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
  1220. return "96 kHz";
  1221. case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
  1222. return "176.4 kHz";
  1223. case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
  1224. return "192 kHz";
  1225. }
  1226. return "Invalid";
  1227. }
  1228. static const char *
  1229. hdmi_audio_coding_type_ext_get_name(enum hdmi_audio_coding_type_ext ctx)
  1230. {
  1231. if (ctx < 0 || ctx > 0x1f)
  1232. return "Invalid";
  1233. switch (ctx) {
  1234. case HDMI_AUDIO_CODING_TYPE_EXT_CT:
  1235. return "Refer to CT";
  1236. case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC:
  1237. return "HE AAC";
  1238. case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2:
  1239. return "HE AAC v2";
  1240. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND:
  1241. return "MPEG SURROUND";
  1242. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC:
  1243. return "MPEG-4 HE AAC";
  1244. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_V2:
  1245. return "MPEG-4 HE AAC v2";
  1246. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC:
  1247. return "MPEG-4 AAC LC";
  1248. case HDMI_AUDIO_CODING_TYPE_EXT_DRA:
  1249. return "DRA";
  1250. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_SURROUND:
  1251. return "MPEG-4 HE AAC + MPEG Surround";
  1252. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC_SURROUND:
  1253. return "MPEG-4 AAC LC + MPEG Surround";
  1254. }
  1255. return "Reserved";
  1256. }
  1257. static void hdmi_audio_infoframe_log(const char *level,
  1258. struct device *dev,
  1259. const struct hdmi_audio_infoframe *frame)
  1260. {
  1261. hdmi_infoframe_log_header(level, dev,
  1262. (const struct hdmi_any_infoframe *)frame);
  1263. if (frame->channels)
  1264. hdmi_log(" channels: %u\n", frame->channels - 1);
  1265. else
  1266. hdmi_log(" channels: Refer to stream header\n");
  1267. hdmi_log(" coding type: %s\n",
  1268. hdmi_audio_coding_type_get_name(frame->coding_type));
  1269. hdmi_log(" sample size: %s\n",
  1270. hdmi_audio_sample_size_get_name(frame->sample_size));
  1271. hdmi_log(" sample frequency: %s\n",
  1272. hdmi_audio_sample_frequency_get_name(frame->sample_frequency));
  1273. hdmi_log(" coding type ext: %s\n",
  1274. hdmi_audio_coding_type_ext_get_name(frame->coding_type_ext));
  1275. hdmi_log(" channel allocation: 0x%x\n",
  1276. frame->channel_allocation);
  1277. hdmi_log(" level shift value: %u dB\n",
  1278. frame->level_shift_value);
  1279. hdmi_log(" downmix inhibit: %s\n",
  1280. frame->downmix_inhibit ? "Yes" : "No");
  1281. }
  1282. static void hdmi_drm_infoframe_log(const char *level,
  1283. struct device *dev,
  1284. const struct hdmi_drm_infoframe *frame)
  1285. {
  1286. int i;
  1287. hdmi_infoframe_log_header(level, dev,
  1288. (struct hdmi_any_infoframe *)frame);
  1289. hdmi_log("length: %d\n", frame->length);
  1290. hdmi_log("metadata type: %d\n", frame->metadata_type);
  1291. hdmi_log("eotf: %d\n", frame->eotf);
  1292. for (i = 0; i < 3; i++) {
  1293. hdmi_log("x[%d]: %d\n", i, frame->display_primaries[i].x);
  1294. hdmi_log("y[%d]: %d\n", i, frame->display_primaries[i].y);
  1295. }
  1296. hdmi_log("white point x: %d\n", frame->white_point.x);
  1297. hdmi_log("white point y: %d\n", frame->white_point.y);
  1298. hdmi_log("max_display_mastering_luminance: %d\n",
  1299. frame->max_display_mastering_luminance);
  1300. hdmi_log("min_display_mastering_luminance: %d\n",
  1301. frame->min_display_mastering_luminance);
  1302. hdmi_log("max_cll: %d\n", frame->max_cll);
  1303. hdmi_log("max_fall: %d\n", frame->max_fall);
  1304. }
  1305. static const char *
  1306. hdmi_3d_structure_get_name(enum hdmi_3d_structure s3d_struct)
  1307. {
  1308. if (s3d_struct < 0 || s3d_struct > 0xf)
  1309. return "Invalid";
  1310. switch (s3d_struct) {
  1311. case HDMI_3D_STRUCTURE_FRAME_PACKING:
  1312. return "Frame Packing";
  1313. case HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE:
  1314. return "Field Alternative";
  1315. case HDMI_3D_STRUCTURE_LINE_ALTERNATIVE:
  1316. return "Line Alternative";
  1317. case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL:
  1318. return "Side-by-side (Full)";
  1319. case HDMI_3D_STRUCTURE_L_DEPTH:
  1320. return "L + Depth";
  1321. case HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH:
  1322. return "L + Depth + Graphics + Graphics-depth";
  1323. case HDMI_3D_STRUCTURE_TOP_AND_BOTTOM:
  1324. return "Top-and-Bottom";
  1325. case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF:
  1326. return "Side-by-side (Half)";
  1327. default:
  1328. break;
  1329. }
  1330. return "Reserved";
  1331. }
  1332. static void
  1333. hdmi_vendor_any_infoframe_log(const char *level,
  1334. struct device *dev,
  1335. const union hdmi_vendor_any_infoframe *frame)
  1336. {
  1337. const struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
  1338. hdmi_infoframe_log_header(level, dev,
  1339. (const struct hdmi_any_infoframe *)frame);
  1340. if (frame->any.oui != HDMI_IEEE_OUI) {
  1341. hdmi_log(" not a HDMI vendor infoframe\n");
  1342. return;
  1343. }
  1344. if (hvf->vic == 0 && hvf->s3d_struct == HDMI_3D_STRUCTURE_INVALID) {
  1345. hdmi_log(" empty frame\n");
  1346. return;
  1347. }
  1348. if (hvf->vic)
  1349. hdmi_log(" HDMI VIC: %u\n", hvf->vic);
  1350. if (hvf->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
  1351. hdmi_log(" 3D structure: %s\n",
  1352. hdmi_3d_structure_get_name(hvf->s3d_struct));
  1353. if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  1354. hdmi_log(" 3D extension data: %d\n",
  1355. hvf->s3d_ext_data);
  1356. }
  1357. }
  1358. /**
  1359. * hdmi_infoframe_log() - log info of HDMI infoframe
  1360. * @level: logging level
  1361. * @dev: device
  1362. * @frame: HDMI infoframe
  1363. */
  1364. void hdmi_infoframe_log(const char *level,
  1365. struct device *dev,
  1366. const union hdmi_infoframe *frame)
  1367. {
  1368. switch (frame->any.type) {
  1369. case HDMI_INFOFRAME_TYPE_AVI:
  1370. hdmi_avi_infoframe_log(level, dev, &frame->avi);
  1371. break;
  1372. case HDMI_INFOFRAME_TYPE_SPD:
  1373. hdmi_spd_infoframe_log(level, dev, &frame->spd);
  1374. break;
  1375. case HDMI_INFOFRAME_TYPE_AUDIO:
  1376. hdmi_audio_infoframe_log(level, dev, &frame->audio);
  1377. break;
  1378. case HDMI_INFOFRAME_TYPE_VENDOR:
  1379. hdmi_vendor_any_infoframe_log(level, dev, &frame->vendor);
  1380. break;
  1381. case HDMI_INFOFRAME_TYPE_DRM:
  1382. hdmi_drm_infoframe_log(level, dev, &frame->drm);
  1383. break;
  1384. }
  1385. }
  1386. EXPORT_SYMBOL(hdmi_infoframe_log);
  1387. /**
  1388. * hdmi_avi_infoframe_unpack() - unpack binary buffer to a HDMI AVI infoframe
  1389. * @frame: HDMI AVI infoframe
  1390. * @buffer: source buffer
  1391. * @size: size of buffer
  1392. *
  1393. * Unpacks the information contained in binary @buffer into a structured
  1394. * @frame of the HDMI Auxiliary Video (AVI) information frame.
  1395. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1396. * specification.
  1397. *
  1398. * Returns 0 on success or a negative error code on failure.
  1399. */
  1400. static int hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe *frame,
  1401. const void *buffer, size_t size)
  1402. {
  1403. const u8 *ptr = buffer;
  1404. if (size < HDMI_INFOFRAME_SIZE(AVI))
  1405. return -EINVAL;
  1406. if (ptr[0] != HDMI_INFOFRAME_TYPE_AVI ||
  1407. ptr[1] != 2 ||
  1408. ptr[2] != HDMI_AVI_INFOFRAME_SIZE)
  1409. return -EINVAL;
  1410. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AVI)) != 0)
  1411. return -EINVAL;
  1412. hdmi_avi_infoframe_init(frame);
  1413. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1414. frame->colorspace = (ptr[0] >> 5) & 0x3;
  1415. if (ptr[0] & 0x10)
  1416. frame->active_aspect = ptr[1] & 0xf;
  1417. if (ptr[0] & 0x8) {
  1418. frame->top_bar = (ptr[6] << 8) | ptr[5];
  1419. frame->bottom_bar = (ptr[8] << 8) | ptr[7];
  1420. }
  1421. if (ptr[0] & 0x4) {
  1422. frame->left_bar = (ptr[10] << 8) | ptr[9];
  1423. frame->right_bar = (ptr[12] << 8) | ptr[11];
  1424. }
  1425. frame->scan_mode = ptr[0] & 0x3;
  1426. frame->colorimetry = (ptr[1] >> 6) & 0x3;
  1427. frame->picture_aspect = (ptr[1] >> 4) & 0x3;
  1428. frame->active_aspect = ptr[1] & 0xf;
  1429. frame->itc = ptr[2] & 0x80 ? true : false;
  1430. frame->extended_colorimetry = (ptr[2] >> 4) & 0x7;
  1431. frame->quantization_range = (ptr[2] >> 2) & 0x3;
  1432. frame->nups = ptr[2] & 0x3;
  1433. frame->video_code = ptr[3] & 0x7f;
  1434. frame->ycc_quantization_range = (ptr[4] >> 6) & 0x3;
  1435. frame->content_type = (ptr[4] >> 4) & 0x3;
  1436. frame->pixel_repeat = ptr[4] & 0xf;
  1437. return 0;
  1438. }
  1439. /**
  1440. * hdmi_spd_infoframe_unpack() - unpack binary buffer to a HDMI SPD infoframe
  1441. * @frame: HDMI SPD infoframe
  1442. * @buffer: source buffer
  1443. * @size: size of buffer
  1444. *
  1445. * Unpacks the information contained in binary @buffer into a structured
  1446. * @frame of the HDMI Source Product Description (SPD) information frame.
  1447. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1448. * specification.
  1449. *
  1450. * Returns 0 on success or a negative error code on failure.
  1451. */
  1452. static int hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe *frame,
  1453. const void *buffer, size_t size)
  1454. {
  1455. const u8 *ptr = buffer;
  1456. int ret;
  1457. if (size < HDMI_INFOFRAME_SIZE(SPD))
  1458. return -EINVAL;
  1459. if (ptr[0] != HDMI_INFOFRAME_TYPE_SPD ||
  1460. ptr[1] != 1 ||
  1461. ptr[2] != HDMI_SPD_INFOFRAME_SIZE) {
  1462. return -EINVAL;
  1463. }
  1464. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(SPD)) != 0)
  1465. return -EINVAL;
  1466. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1467. ret = hdmi_spd_infoframe_init(frame, ptr, ptr + 8);
  1468. if (ret)
  1469. return ret;
  1470. frame->sdi = ptr[24];
  1471. return 0;
  1472. }
  1473. /**
  1474. * hdmi_audio_infoframe_unpack() - unpack binary buffer to a HDMI AUDIO infoframe
  1475. * @frame: HDMI Audio infoframe
  1476. * @buffer: source buffer
  1477. * @size: size of buffer
  1478. *
  1479. * Unpacks the information contained in binary @buffer into a structured
  1480. * @frame of the HDMI Audio information frame.
  1481. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1482. * specification.
  1483. *
  1484. * Returns 0 on success or a negative error code on failure.
  1485. */
  1486. static int hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe *frame,
  1487. const void *buffer, size_t size)
  1488. {
  1489. const u8 *ptr = buffer;
  1490. int ret;
  1491. if (size < HDMI_INFOFRAME_SIZE(AUDIO))
  1492. return -EINVAL;
  1493. if (ptr[0] != HDMI_INFOFRAME_TYPE_AUDIO ||
  1494. ptr[1] != 1 ||
  1495. ptr[2] != HDMI_AUDIO_INFOFRAME_SIZE) {
  1496. return -EINVAL;
  1497. }
  1498. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AUDIO)) != 0)
  1499. return -EINVAL;
  1500. ret = hdmi_audio_infoframe_init(frame);
  1501. if (ret)
  1502. return ret;
  1503. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1504. frame->channels = ptr[0] & 0x7;
  1505. frame->coding_type = (ptr[0] >> 4) & 0xf;
  1506. frame->sample_size = ptr[1] & 0x3;
  1507. frame->sample_frequency = (ptr[1] >> 2) & 0x7;
  1508. frame->coding_type_ext = ptr[2] & 0x1f;
  1509. frame->channel_allocation = ptr[3];
  1510. frame->level_shift_value = (ptr[4] >> 3) & 0xf;
  1511. frame->downmix_inhibit = ptr[4] & 0x80 ? true : false;
  1512. return 0;
  1513. }
  1514. /**
  1515. * hdmi_vendor_any_infoframe_unpack() - unpack binary buffer to a HDMI
  1516. * vendor infoframe
  1517. * @frame: HDMI Vendor infoframe
  1518. * @buffer: source buffer
  1519. * @size: size of buffer
  1520. *
  1521. * Unpacks the information contained in binary @buffer into a structured
  1522. * @frame of the HDMI Vendor information frame.
  1523. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1524. * specification.
  1525. *
  1526. * Returns 0 on success or a negative error code on failure.
  1527. */
  1528. static int
  1529. hdmi_vendor_any_infoframe_unpack(union hdmi_vendor_any_infoframe *frame,
  1530. const void *buffer, size_t size)
  1531. {
  1532. const u8 *ptr = buffer;
  1533. size_t length;
  1534. int ret;
  1535. u8 hdmi_video_format;
  1536. struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
  1537. if (size < HDMI_INFOFRAME_HEADER_SIZE)
  1538. return -EINVAL;
  1539. if (ptr[0] != HDMI_INFOFRAME_TYPE_VENDOR ||
  1540. ptr[1] != 1 ||
  1541. (ptr[2] != 4 && ptr[2] != 5 && ptr[2] != 6))
  1542. return -EINVAL;
  1543. length = ptr[2];
  1544. if (size < HDMI_INFOFRAME_HEADER_SIZE + length)
  1545. return -EINVAL;
  1546. if (hdmi_infoframe_checksum(buffer,
  1547. HDMI_INFOFRAME_HEADER_SIZE + length) != 0)
  1548. return -EINVAL;
  1549. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1550. /* HDMI OUI */
  1551. if ((ptr[0] != 0x03) ||
  1552. (ptr[1] != 0x0c) ||
  1553. (ptr[2] != 0x00))
  1554. return -EINVAL;
  1555. hdmi_video_format = ptr[3] >> 5;
  1556. if (hdmi_video_format > 0x2)
  1557. return -EINVAL;
  1558. ret = hdmi_vendor_infoframe_init(hvf);
  1559. if (ret)
  1560. return ret;
  1561. hvf->length = length;
  1562. if (hdmi_video_format == 0x2) {
  1563. if (length != 5 && length != 6)
  1564. return -EINVAL;
  1565. hvf->s3d_struct = ptr[4] >> 4;
  1566. if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF) {
  1567. if (length != 6)
  1568. return -EINVAL;
  1569. hvf->s3d_ext_data = ptr[5] >> 4;
  1570. }
  1571. } else if (hdmi_video_format == 0x1) {
  1572. if (length != 5)
  1573. return -EINVAL;
  1574. hvf->vic = ptr[4];
  1575. } else {
  1576. if (length != 4)
  1577. return -EINVAL;
  1578. }
  1579. return 0;
  1580. }
  1581. /**
  1582. * hdmi_drm_infoframe_unpack_only() - unpack binary buffer of CTA-861-G DRM
  1583. * infoframe DataBytes to a HDMI DRM
  1584. * infoframe
  1585. * @frame: HDMI DRM infoframe
  1586. * @buffer: source buffer
  1587. * @size: size of buffer
  1588. *
  1589. * Unpacks CTA-861-G DRM infoframe DataBytes contained in the binary @buffer
  1590. * into a structured @frame of the HDMI Dynamic Range and Mastering (DRM)
  1591. * infoframe.
  1592. *
  1593. * Returns 0 on success or a negative error code on failure.
  1594. */
  1595. int hdmi_drm_infoframe_unpack_only(struct hdmi_drm_infoframe *frame,
  1596. const void *buffer, size_t size)
  1597. {
  1598. const u8 *ptr = buffer;
  1599. const u8 *temp;
  1600. u8 x_lsb, x_msb;
  1601. u8 y_lsb, y_msb;
  1602. int ret;
  1603. int i;
  1604. if (size < HDMI_DRM_INFOFRAME_SIZE)
  1605. return -EINVAL;
  1606. ret = hdmi_drm_infoframe_init(frame);
  1607. if (ret)
  1608. return ret;
  1609. frame->eotf = ptr[0] & 0x7;
  1610. frame->metadata_type = ptr[1] & 0x7;
  1611. temp = ptr + 2;
  1612. for (i = 0; i < 3; i++) {
  1613. x_lsb = *temp++;
  1614. x_msb = *temp++;
  1615. frame->display_primaries[i].x = (x_msb << 8) | x_lsb;
  1616. y_lsb = *temp++;
  1617. y_msb = *temp++;
  1618. frame->display_primaries[i].y = (y_msb << 8) | y_lsb;
  1619. }
  1620. frame->white_point.x = (ptr[15] << 8) | ptr[14];
  1621. frame->white_point.y = (ptr[17] << 8) | ptr[16];
  1622. frame->max_display_mastering_luminance = (ptr[19] << 8) | ptr[18];
  1623. frame->min_display_mastering_luminance = (ptr[21] << 8) | ptr[20];
  1624. frame->max_cll = (ptr[23] << 8) | ptr[22];
  1625. frame->max_fall = (ptr[25] << 8) | ptr[24];
  1626. return 0;
  1627. }
  1628. EXPORT_SYMBOL(hdmi_drm_infoframe_unpack_only);
  1629. /**
  1630. * hdmi_drm_infoframe_unpack() - unpack binary buffer to a HDMI DRM infoframe
  1631. * @frame: HDMI DRM infoframe
  1632. * @buffer: source buffer
  1633. * @size: size of buffer
  1634. *
  1635. * Unpacks the CTA-861-G DRM infoframe contained in the binary @buffer into
  1636. * a structured @frame of the HDMI Dynamic Range and Mastering (DRM)
  1637. * infoframe. It also verifies the checksum as required by section 5.3.5 of
  1638. * the HDMI 1.4 specification.
  1639. *
  1640. * Returns 0 on success or a negative error code on failure.
  1641. */
  1642. static int hdmi_drm_infoframe_unpack(struct hdmi_drm_infoframe *frame,
  1643. const void *buffer, size_t size)
  1644. {
  1645. const u8 *ptr = buffer;
  1646. int ret;
  1647. if (size < HDMI_INFOFRAME_SIZE(DRM))
  1648. return -EINVAL;
  1649. if (ptr[0] != HDMI_INFOFRAME_TYPE_DRM ||
  1650. ptr[1] != 1 ||
  1651. ptr[2] != HDMI_DRM_INFOFRAME_SIZE)
  1652. return -EINVAL;
  1653. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(DRM)) != 0)
  1654. return -EINVAL;
  1655. ret = hdmi_drm_infoframe_unpack_only(frame, ptr + HDMI_INFOFRAME_HEADER_SIZE,
  1656. size - HDMI_INFOFRAME_HEADER_SIZE);
  1657. return ret;
  1658. }
  1659. /**
  1660. * hdmi_infoframe_unpack() - unpack binary buffer to a HDMI infoframe
  1661. * @frame: HDMI infoframe
  1662. * @buffer: source buffer
  1663. * @size: size of buffer
  1664. *
  1665. * Unpacks the information contained in binary buffer @buffer into a structured
  1666. * @frame of a HDMI infoframe.
  1667. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1668. * specification.
  1669. *
  1670. * Returns 0 on success or a negative error code on failure.
  1671. */
  1672. int hdmi_infoframe_unpack(union hdmi_infoframe *frame,
  1673. const void *buffer, size_t size)
  1674. {
  1675. int ret;
  1676. const u8 *ptr = buffer;
  1677. if (size < HDMI_INFOFRAME_HEADER_SIZE)
  1678. return -EINVAL;
  1679. switch (ptr[0]) {
  1680. case HDMI_INFOFRAME_TYPE_AVI:
  1681. ret = hdmi_avi_infoframe_unpack(&frame->avi, buffer, size);
  1682. break;
  1683. case HDMI_INFOFRAME_TYPE_DRM:
  1684. ret = hdmi_drm_infoframe_unpack(&frame->drm, buffer, size);
  1685. break;
  1686. case HDMI_INFOFRAME_TYPE_SPD:
  1687. ret = hdmi_spd_infoframe_unpack(&frame->spd, buffer, size);
  1688. break;
  1689. case HDMI_INFOFRAME_TYPE_AUDIO:
  1690. ret = hdmi_audio_infoframe_unpack(&frame->audio, buffer, size);
  1691. break;
  1692. case HDMI_INFOFRAME_TYPE_VENDOR:
  1693. ret = hdmi_vendor_any_infoframe_unpack(&frame->vendor, buffer, size);
  1694. break;
  1695. default:
  1696. ret = -EINVAL;
  1697. break;
  1698. }
  1699. return ret;
  1700. }
  1701. EXPORT_SYMBOL(hdmi_infoframe_unpack);