hdmi_infoframe_api.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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. #ifdef __KERNEL__
  24. #include <linux/bitops.h>
  25. #include <linux/bug.h>
  26. #include <linux/errno.h>
  27. //#include <linux/export.h>
  28. #include <linux/string.h>
  29. #include <linux/device.h>
  30. #include "hdmi_infoframe_api.h"
  31. #else
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include "hdmi_infoframe_api.h"
  35. #endif
  36. #ifdef __KERNEL__
  37. //#define dev_printk printk
  38. //#define hdmi_log(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
  39. #define hdmi_log printk
  40. #else
  41. #define BIT(x) 1<<x;
  42. #define hdmi_log printf
  43. #endif
  44. static u8 hdmi_infoframe_checksum(u8 *ptr, size_t size)
  45. {
  46. u8 csum = 0;
  47. size_t i;
  48. /* compute checksum */
  49. for (i = 0; i < size; i++)
  50. csum += ptr[i];
  51. return 256 - csum;
  52. }
  53. static void hdmi_infoframe_set_checksum(void *buffer, size_t size)
  54. {
  55. u8 *ptr = buffer;
  56. ptr[3] = hdmi_infoframe_checksum(buffer, size);
  57. }
  58. /**
  59. * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
  60. * @frame: HDMI AVI infoframe
  61. *
  62. * Returns 0 on success or a negative error code on failure.
  63. */
  64. int hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
  65. {
  66. memset(frame, 0, sizeof(*frame));
  67. frame->type = HDMI_INFOFRAME_TYPE_AVI;
  68. frame->version = 2;
  69. frame->length = HDMI_AVI_INFOFRAME_SIZE;
  70. return 0;
  71. }
  72. //EXPORT_SYMBOL(hdmi_avi_infoframe_init);
  73. /**
  74. * hdmi_avi_infoframe_pack() - write HDMI AVI infoframe to binary buffer
  75. * @frame: HDMI AVI infoframe
  76. * @buffer: destination buffer
  77. * @size: size of buffer
  78. *
  79. * Packs the information contained in the @frame structure into a binary
  80. * representation that can be written into the corresponding controller
  81. * registers. Also computes the checksum as required by section 5.3.5 of
  82. * the HDMI 1.4 specification.
  83. *
  84. * Returns the number of bytes packed into the binary buffer or a negative
  85. * error code on failure.
  86. */
  87. ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame, void *buffer,
  88. size_t size)
  89. {
  90. u8 *ptr = buffer;
  91. size_t length;
  92. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  93. if (size < length)
  94. return -ENOSPC;
  95. memset(buffer, 0, size);
  96. ptr[0] = frame->type;
  97. ptr[1] = frame->version;
  98. ptr[2] = frame->length;
  99. ptr[3] = 0; /* checksum */
  100. /* start infoframe payload */
  101. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  102. ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
  103. /*
  104. * Data byte 1, bit 4 has to be set if we provide the active format
  105. * aspect ratio
  106. */
  107. if (frame->active_aspect & 0xf)
  108. ptr[0] |= BIT(4);
  109. /* Bit 3 and 2 indicate if we transmit horizontal/vertical bar data */
  110. if (frame->top_bar || frame->bottom_bar)
  111. ptr[0] |= BIT(3);
  112. if (frame->left_bar || frame->right_bar)
  113. ptr[0] |= BIT(2);
  114. ptr[1] = ((frame->colorimetry & 0x3) << 6) |
  115. ((frame->picture_aspect & 0x3) << 4) |
  116. (frame->active_aspect & 0xf);
  117. ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
  118. ((frame->quantization_range & 0x3) << 2) |
  119. (frame->nups & 0x3);
  120. if (frame->itc)
  121. ptr[2] |= BIT(7);
  122. ptr[3] = frame->video_code & 0x7f;
  123. ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
  124. ((frame->content_type & 0x3) << 4) |
  125. (frame->pixel_repeat & 0xf);
  126. ptr[5] = frame->top_bar & 0xff;
  127. ptr[6] = (frame->top_bar >> 8) & 0xff;
  128. ptr[7] = frame->bottom_bar & 0xff;
  129. ptr[8] = (frame->bottom_bar >> 8) & 0xff;
  130. ptr[9] = frame->left_bar & 0xff;
  131. ptr[10] = (frame->left_bar >> 8) & 0xff;
  132. ptr[11] = frame->right_bar & 0xff;
  133. ptr[12] = (frame->right_bar >> 8) & 0xff;
  134. hdmi_infoframe_set_checksum(buffer, length);
  135. return length;
  136. }
  137. //EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
  138. /**
  139. * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
  140. * @frame: HDMI SPD infoframe
  141. * @vendor: vendor string
  142. * @product: product string
  143. *
  144. * Returns 0 on success or a negative error code on failure.
  145. */
  146. int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
  147. const char *vendor, const char *product, const char *sdi)
  148. {
  149. memset(frame, 0, sizeof(*frame));
  150. frame->type = HDMI_INFOFRAME_TYPE_SPD;
  151. frame->version = 1;
  152. frame->length = HDMI_SPD_INFOFRAME_SIZE;
  153. strncpy(frame->vendor, vendor, sizeof(frame->vendor));
  154. strncpy(frame->product, product, sizeof(frame->product));
  155. strncpy((char *)&frame->sdi, sdi, sizeof(frame->sdi));
  156. return 0;
  157. }
  158. //EXPORT_SYMBOL(hdmi_spd_infoframe_init);
  159. /**
  160. * hdmi_spd_infoframe_pack() - write HDMI SPD infoframe to binary buffer
  161. * @frame: HDMI SPD infoframe
  162. * @buffer: destination buffer
  163. * @size: size of buffer
  164. *
  165. * Packs the information contained in the @frame structure into a binary
  166. * representation that can be written into the corresponding controller
  167. * registers. Also computes the checksum as required by section 5.3.5 of
  168. * the HDMI 1.4 specification.
  169. *
  170. * Returns the number of bytes packed into the binary buffer or a negative
  171. * error code on failure.
  172. */
  173. ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame, void *buffer,
  174. size_t size)
  175. {
  176. u8 *ptr = buffer;
  177. size_t length;
  178. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  179. if (size < length)
  180. return -ENOSPC;
  181. memset(buffer, 0, size);
  182. ptr[0] = frame->type;
  183. ptr[1] = frame->version;
  184. ptr[2] = frame->length;
  185. ptr[3] = 0; /* checksum */
  186. /* start infoframe payload */
  187. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  188. memcpy(ptr, frame->vendor, sizeof(frame->vendor));
  189. memcpy(ptr + 8, frame->product, sizeof(frame->product));
  190. ptr[24+HDMI_INFOFRAME_HEADER_SIZE] = frame->sdi;
  191. hdmi_infoframe_set_checksum(buffer, length);
  192. return length;
  193. }
  194. //EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
  195. /**
  196. * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
  197. * @frame: HDMI audio infoframe
  198. *
  199. * Returns 0 on success or a negative error code on failure.
  200. */
  201. int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
  202. {
  203. memset(frame, 0, sizeof(*frame));
  204. frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
  205. frame->version = 1;
  206. frame->length = HDMI_AUDIO_INFOFRAME_SIZE;
  207. return 0;
  208. }
  209. //EXPORT_SYMBOL(hdmi_audio_infoframe_init);
  210. /**
  211. * hdmi_audio_infoframe_pack() - write HDMI audio infoframe to binary buffer
  212. * @frame: HDMI audio infoframe
  213. * @buffer: destination buffer
  214. * @size: size of buffer
  215. *
  216. * Packs the information contained in the @frame structure into a binary
  217. * representation that can be written into the corresponding controller
  218. * registers. Also computes the checksum as required by section 5.3.5 of
  219. * the HDMI 1.4 specification.
  220. *
  221. * Returns the number of bytes packed into the binary buffer or a negative
  222. * error code on failure.
  223. */
  224. ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
  225. void *buffer, size_t size)
  226. {
  227. unsigned char channels;
  228. u8 *ptr = buffer;
  229. size_t length;
  230. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  231. if (size < length)
  232. return -ENOSPC;
  233. memset(buffer, 0, size);
  234. if (frame->channels >= 2)
  235. channels = frame->channels - 1;
  236. else
  237. channels = 0;
  238. ptr[0] = frame->type;
  239. ptr[1] = frame->version;
  240. ptr[2] = frame->length;
  241. ptr[3] = 0; /* checksum */
  242. /* start infoframe payload */
  243. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  244. ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
  245. ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
  246. (frame->sample_size & 0x3);
  247. ptr[2] = frame->coding_type_ext & 0x1f;
  248. ptr[3] = frame->channel_allocation;
  249. ptr[4] = (frame->level_shift_value & 0xf) << 3;
  250. if (frame->downmix_inhibit)
  251. ptr[4] |= BIT(7);
  252. hdmi_infoframe_set_checksum(buffer, length);
  253. return length;
  254. }
  255. //EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
  256. /**
  257. * hdmi_vendor_infoframe_init() - initialize an HDMI vendor infoframe
  258. * @frame: HDMI vendor infoframe
  259. *
  260. * Returns 0 on success or a negative error code on failure.
  261. */
  262. int hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe *frame)
  263. {
  264. memset(frame, 0, sizeof(*frame));
  265. frame->type = HDMI_INFOFRAME_TYPE_VENDOR;
  266. frame->version = 1;
  267. frame->oui = HDMI_IEEE_OUI;
  268. /*
  269. * 0 is a valid value for s3d_struct, so we use a special "not set"
  270. * value
  271. */
  272. frame->s3d_struct = HDMI_3D_STRUCTURE_INVALID;
  273. return 0;
  274. }
  275. //EXPORT_SYMBOL(hdmi_vendor_infoframe_init);
  276. /**
  277. * hdmi_vendor_infoframe_pack() - write a HDMI vendor infoframe to binary buffer
  278. * @frame: HDMI infoframe
  279. * @buffer: destination buffer
  280. * @size: size of buffer
  281. *
  282. * Packs the information contained in the @frame structure into a binary
  283. * representation that can be written into the corresponding controller
  284. * registers. Also computes the checksum as required by section 5.3.5 of
  285. * the HDMI 1.4 specification.
  286. *
  287. * Returns the number of bytes packed into the binary buffer or a negative
  288. * error code on failure.
  289. */
  290. ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
  291. void *buffer, size_t size)
  292. {
  293. u8 *ptr = buffer;
  294. size_t length;
  295. /* empty info frame */
  296. if (frame->vic == 0 && frame->s3d_struct == HDMI_3D_STRUCTURE_INVALID)
  297. return -EINVAL;
  298. /* only one of those can be supplied */
  299. if (frame->vic != 0 && frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
  300. return -EINVAL;
  301. /* for side by side (half) we also need to provide 3D_Ext_Data */
  302. if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  303. frame->length = 6;
  304. else
  305. frame->length = 5;
  306. length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
  307. if (size < length)
  308. return -ENOSPC;
  309. memset(buffer, 0, size);
  310. ptr[0] = frame->type;
  311. ptr[1] = frame->version;
  312. ptr[2] = frame->length;
  313. ptr[3] = 0; /* checksum */
  314. /* HDMI OUI */
  315. ptr[4] = 0x03;
  316. ptr[5] = 0x0c;
  317. ptr[6] = 0x00;
  318. if (frame->vic) {
  319. ptr[7] = 0x1 << 5; /* video format */
  320. ptr[8] = frame->vic;
  321. } else {
  322. ptr[7] = 0x2 << 5; /* video format */
  323. ptr[8] = (frame->s3d_struct & 0xf) << 4;
  324. if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  325. ptr[9] = (frame->s3d_ext_data & 0xf) << 4;
  326. }
  327. hdmi_infoframe_set_checksum(buffer, length);
  328. return length;
  329. }
  330. //EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);
  331. /*
  332. * hdmi_vendor_any_infoframe_pack() - write a vendor infoframe to binary buffer
  333. */
  334. static ssize_t
  335. hdmi_vendor_any_infoframe_pack(union hdmi_vendor_any_infoframe *frame,
  336. void *buffer, size_t size)
  337. {
  338. /* we only know about HDMI vendor infoframes */
  339. if (frame->any.oui != HDMI_IEEE_OUI)
  340. return -EINVAL;
  341. return hdmi_vendor_infoframe_pack(&frame->hdmi, buffer, size);
  342. }
  343. /**
  344. * hdmi_infoframe_pack() - write a HDMI infoframe to binary buffer
  345. * @frame: HDMI infoframe
  346. * @buffer: destination buffer
  347. * @size: size of buffer
  348. *
  349. * Packs the information contained in the @frame structure into a binary
  350. * representation that can be written into the corresponding controller
  351. * registers. Also computes the checksum as required by section 5.3.5 of
  352. * the HDMI 1.4 specification.
  353. *
  354. * Returns the number of bytes packed into the binary buffer or a negative
  355. * error code on failure.
  356. */
  357. ssize_t
  358. hdmi_infoframe_pack(union hdmi_infoframe *frame, void *buffer, size_t size)
  359. {
  360. ssize_t length;
  361. switch (frame->any.type) {
  362. case HDMI_INFOFRAME_TYPE_AVI:
  363. length = hdmi_avi_infoframe_pack(&frame->avi, buffer, size);
  364. break;
  365. case HDMI_INFOFRAME_TYPE_SPD:
  366. length = hdmi_spd_infoframe_pack(&frame->spd, buffer, size);
  367. break;
  368. case HDMI_INFOFRAME_TYPE_AUDIO:
  369. length = hdmi_audio_infoframe_pack(&frame->audio, buffer, size);
  370. break;
  371. case HDMI_INFOFRAME_TYPE_VENDOR:
  372. length = hdmi_vendor_any_infoframe_pack(&frame->vendor,
  373. buffer, size);
  374. break;
  375. default:
  376. hdmi_log("Bad infoframe type %d\n", frame->any.type);
  377. length = -EINVAL;
  378. }
  379. return length;
  380. }
  381. //EXPORT_SYMBOL(hdmi_infoframe_pack);
  382. static const char *hdmi_infoframe_type_get_name(enum hdmi_infoframe_type type)
  383. {
  384. if (type < 0x80 || type > 0x9f)
  385. return "Invalid";
  386. switch (type) {
  387. case HDMI_INFOFRAME_TYPE_VENDOR:
  388. return "Vendor";
  389. case HDMI_INFOFRAME_TYPE_AVI:
  390. return "Auxiliary Video Information (AVI)";
  391. case HDMI_INFOFRAME_TYPE_SPD:
  392. return "Source Product Description (SPD)";
  393. case HDMI_INFOFRAME_TYPE_AUDIO:
  394. return "Audio";
  395. default:
  396. return "";
  397. }
  398. return "Reserved";
  399. }
  400. static void hdmi_infoframe_log_header(const char *level,
  401. struct device *dev,
  402. struct hdmi_any_infoframe *frame)
  403. {
  404. hdmi_log("HDMI infoframe: %s, version %u, length %u\n",
  405. hdmi_infoframe_type_get_name(frame->type),
  406. frame->version, frame->length);
  407. }
  408. static const char *hdmi_colorspace_get_name(enum hdmi_colorspace colorspace)
  409. {
  410. switch (colorspace) {
  411. case HDMI_COLORSPACE_RGB:
  412. return "RGB";
  413. case HDMI_COLORSPACE_YUV422:
  414. return "YCbCr 4:2:2";
  415. case HDMI_COLORSPACE_YUV444:
  416. return "YCbCr 4:4:4";
  417. case HDMI_COLORSPACE_YUV420:
  418. return "YCbCr 4:2:0";
  419. case HDMI_COLORSPACE_RESERVED4:
  420. return "Reserved (4)";
  421. case HDMI_COLORSPACE_RESERVED5:
  422. return "Reserved (5)";
  423. case HDMI_COLORSPACE_RESERVED6:
  424. return "Reserved (6)";
  425. case HDMI_COLORSPACE_IDO_DEFINED:
  426. return "IDO Defined";
  427. }
  428. return "Invalid";
  429. }
  430. static const char *hdmi_scan_mode_get_name(enum hdmi_scan_mode scan_mode)
  431. {
  432. switch (scan_mode) {
  433. case HDMI_SCAN_MODE_NONE:
  434. return "No Data";
  435. case HDMI_SCAN_MODE_OVERSCAN:
  436. return "Overscan";
  437. case HDMI_SCAN_MODE_UNDERSCAN:
  438. return "Underscan";
  439. case HDMI_SCAN_MODE_RESERVED:
  440. return "Reserved";
  441. }
  442. return "Invalid";
  443. }
  444. static const char *hdmi_colorimetry_get_name(enum hdmi_colorimetry colorimetry)
  445. {
  446. switch (colorimetry) {
  447. case HDMI_COLORIMETRY_NONE:
  448. return "No Data";
  449. case HDMI_COLORIMETRY_ITU_601:
  450. return "ITU601";
  451. case HDMI_COLORIMETRY_ITU_709:
  452. return "ITU709";
  453. case HDMI_COLORIMETRY_EXTENDED:
  454. return "Extended";
  455. }
  456. return "Invalid";
  457. }
  458. static const char *
  459. hdmi_picture_aspect_get_name(enum hdmi_picture_aspect picture_aspect)
  460. {
  461. switch (picture_aspect) {
  462. case HDMI_PICTURE_ASPECT_NONE:
  463. return "No Data";
  464. case HDMI_PICTURE_ASPECT_4_3:
  465. return "4:3";
  466. case HDMI_PICTURE_ASPECT_16_9:
  467. return "16:9";
  468. case HDMI_PICTURE_ASPECT_64_27:
  469. return "64:27";
  470. case HDMI_PICTURE_ASPECT_256_135:
  471. return "256:135";
  472. case HDMI_PICTURE_ASPECT_RESERVED:
  473. return "Reserved";
  474. }
  475. return "Invalid";
  476. }
  477. static const char *
  478. hdmi_active_aspect_get_name(enum hdmi_active_aspect active_aspect)
  479. {
  480. if ((int)active_aspect < 0 || active_aspect > 0xf)
  481. return "Invalid";
  482. switch (active_aspect) {
  483. case HDMI_ACTIVE_ASPECT_16_9_TOP:
  484. return "16:9 Top";
  485. case HDMI_ACTIVE_ASPECT_14_9_TOP:
  486. return "14:9 Top";
  487. case HDMI_ACTIVE_ASPECT_16_9_CENTER:
  488. return "16:9 Center";
  489. case HDMI_ACTIVE_ASPECT_PICTURE:
  490. return "Same as Picture";
  491. case HDMI_ACTIVE_ASPECT_4_3:
  492. return "4:3";
  493. case HDMI_ACTIVE_ASPECT_16_9:
  494. return "16:9";
  495. case HDMI_ACTIVE_ASPECT_14_9:
  496. return "14:9";
  497. case HDMI_ACTIVE_ASPECT_4_3_SP_14_9:
  498. return "4:3 SP 14:9";
  499. case HDMI_ACTIVE_ASPECT_16_9_SP_14_9:
  500. return "16:9 SP 14:9";
  501. case HDMI_ACTIVE_ASPECT_16_9_SP_4_3:
  502. return "16:9 SP 4:3";
  503. }
  504. return "Reserved";
  505. }
  506. static const char *
  507. hdmi_extended_colorimetry_get_name(enum hdmi_extended_colorimetry ext_col)
  508. {
  509. switch (ext_col) {
  510. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
  511. return "xvYCC 601";
  512. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
  513. return "xvYCC 709";
  514. case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
  515. return "sYCC 601";
  516. case HDMI_EXTENDED_COLORIMETRY_ADOBE_YCC_601:
  517. return "Adobe YCC 601";
  518. case HDMI_EXTENDED_COLORIMETRY_ADOBE_RGB:
  519. return "Adobe RGB";
  520. case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
  521. return "BT.2020 Constant Luminance";
  522. case HDMI_EXTENDED_COLORIMETRY_BT2020:
  523. return "BT.2020";
  524. case HDMI_EXTENDED_COLORIMETRY_RESERVED:
  525. return "Reserved";
  526. }
  527. return "Invalid";
  528. }
  529. static const char *
  530. hdmi_quantization_range_get_name(enum hdmi_quantization_range qrange)
  531. {
  532. switch (qrange) {
  533. case HDMI_QUANTIZATION_RANGE_DEFAULT:
  534. return "Default";
  535. case HDMI_QUANTIZATION_RANGE_LIMITED:
  536. return "Limited";
  537. case HDMI_QUANTIZATION_RANGE_FULL:
  538. return "Full";
  539. case HDMI_QUANTIZATION_RANGE_RESERVED:
  540. return "Reserved";
  541. }
  542. return "Invalid";
  543. }
  544. static const char *hdmi_nups_get_name(enum hdmi_nups nups)
  545. {
  546. switch (nups) {
  547. case HDMI_NUPS_UNKNOWN:
  548. return "Unknown Non-uniform Scaling";
  549. case HDMI_NUPS_HORIZONTAL:
  550. return "Horizontally Scaled";
  551. case HDMI_NUPS_VERTICAL:
  552. return "Vertically Scaled";
  553. case HDMI_NUPS_BOTH:
  554. return "Horizontally and Vertically Scaled";
  555. }
  556. return "Invalid";
  557. }
  558. static const char *
  559. hdmi_ycc_quantization_range_get_name(enum hdmi_ycc_quantization_range qrange)
  560. {
  561. switch (qrange) {
  562. case HDMI_YCC_QUANTIZATION_RANGE_LIMITED:
  563. return "Limited";
  564. case HDMI_YCC_QUANTIZATION_RANGE_FULL:
  565. return "Full";
  566. }
  567. return "Invalid";
  568. }
  569. static const char *
  570. hdmi_content_type_get_name(enum hdmi_content_type content_type)
  571. {
  572. switch (content_type) {
  573. case HDMI_CONTENT_TYPE_GRAPHICS:
  574. return "Graphics";
  575. case HDMI_CONTENT_TYPE_PHOTO:
  576. return "Photo";
  577. case HDMI_CONTENT_TYPE_CINEMA:
  578. return "Cinema";
  579. case HDMI_CONTENT_TYPE_GAME:
  580. return "Game";
  581. }
  582. return "Invalid";
  583. }
  584. /**
  585. * hdmi_avi_infoframe_log() - log info of HDMI AVI infoframe
  586. * @level: logging level
  587. * @dev: device
  588. * @frame: HDMI AVI infoframe
  589. */
  590. static void hdmi_avi_infoframe_log(const char *level,
  591. struct device *dev,
  592. struct hdmi_avi_infoframe *frame)
  593. {
  594. hdmi_infoframe_log_header(level, dev,
  595. (struct hdmi_any_infoframe *)frame);
  596. hdmi_log(" colorspace: %s\n",
  597. hdmi_colorspace_get_name(frame->colorspace));
  598. hdmi_log(" scan mode: %s\n",
  599. hdmi_scan_mode_get_name(frame->scan_mode));
  600. hdmi_log(" colorimetry: %s\n",
  601. hdmi_colorimetry_get_name(frame->colorimetry));
  602. hdmi_log(" picture aspect: %s\n",
  603. hdmi_picture_aspect_get_name(frame->picture_aspect));
  604. hdmi_log(" active aspect: %s\n",
  605. hdmi_active_aspect_get_name(frame->active_aspect));
  606. hdmi_log(" itc: %s\n", frame->itc ? "IT Content" : "No Data");
  607. hdmi_log(" extended colorimetry: %s\n",
  608. hdmi_extended_colorimetry_get_name(frame->extended_colorimetry));
  609. hdmi_log(" quantization range: %s\n",
  610. hdmi_quantization_range_get_name(frame->quantization_range));
  611. hdmi_log(" nups: %s\n", hdmi_nups_get_name(frame->nups));
  612. hdmi_log(" video code: %u\n", frame->video_code);
  613. hdmi_log(" ycc quantization range: %s\n",
  614. hdmi_ycc_quantization_range_get_name(frame->ycc_quantization_range));
  615. hdmi_log(" hdmi content type: %s\n",
  616. hdmi_content_type_get_name(frame->content_type));
  617. hdmi_log(" pixel repeat: %u\n", frame->pixel_repeat);
  618. hdmi_log(" bar top %u, bottom %u, left %u, right %u\n",
  619. frame->top_bar, frame->bottom_bar,
  620. frame->left_bar, frame->right_bar);
  621. }
  622. static const char *hdmi_spd_sdi_get_name(enum hdmi_spd_sdi sdi)
  623. {
  624. if ((int)sdi < 0 || sdi > 0xff)
  625. return "Invalid";
  626. switch (sdi) {
  627. case HDMI_SPD_SDI_UNKNOWN:
  628. return "Unknown";
  629. case HDMI_SPD_SDI_DSTB:
  630. return "Digital STB";
  631. case HDMI_SPD_SDI_DVDP:
  632. return "DVD Player";
  633. case HDMI_SPD_SDI_DVHS:
  634. return "D-VHS";
  635. case HDMI_SPD_SDI_HDDVR:
  636. return "HDD Videorecorder";
  637. case HDMI_SPD_SDI_DVC:
  638. return "DVC";
  639. case HDMI_SPD_SDI_DSC:
  640. return "DSC";
  641. case HDMI_SPD_SDI_VCD:
  642. return "Video CD";
  643. case HDMI_SPD_SDI_GAME:
  644. return "Game";
  645. case HDMI_SPD_SDI_PC:
  646. return "PC General";
  647. case HDMI_SPD_SDI_BD:
  648. return "Blu-Ray Disc (BD)";
  649. case HDMI_SPD_SDI_SACD:
  650. return "Super Audio CD";
  651. case HDMI_SPD_SDI_HDDVD:
  652. return "HD DVD";
  653. case HDMI_SPD_SDI_PMP:
  654. return "PMP";
  655. }
  656. return "Reserved";
  657. }
  658. /**
  659. * hdmi_spd_infoframe_log() - log info of HDMI SPD infoframe
  660. * @level: logging level
  661. * @dev: device
  662. * @frame: HDMI SPD infoframe
  663. */
  664. static void hdmi_spd_infoframe_log(const char *level,
  665. struct device *dev,
  666. struct hdmi_spd_infoframe *frame)
  667. {
  668. u8 buf[17];
  669. hdmi_infoframe_log_header(level, dev,
  670. (struct hdmi_any_infoframe *)frame);
  671. memset(buf, 0, sizeof(buf));
  672. strncpy((char*)buf, frame->vendor, 8);
  673. hdmi_log(" vendor: %s\n", buf);
  674. strncpy((char*)buf, frame->product, 16);
  675. hdmi_log(" product: %s\n", buf);
  676. hdmi_log(" source device information: %s (0x%x)\n",
  677. hdmi_spd_sdi_get_name(frame->sdi), frame->sdi);
  678. }
  679. static const char *
  680. hdmi_audio_coding_type_get_name(enum hdmi_audio_coding_type coding_type)
  681. {
  682. switch (coding_type) {
  683. case HDMI_AUDIO_CODING_TYPE_STREAM:
  684. return "Refer to Stream Header";
  685. case HDMI_AUDIO_CODING_TYPE_PCM:
  686. return "PCM";
  687. case HDMI_AUDIO_CODING_TYPE_AC3:
  688. return "AC-3";
  689. case HDMI_AUDIO_CODING_TYPE_MPEG1:
  690. return "MPEG1";
  691. case HDMI_AUDIO_CODING_TYPE_MP3:
  692. return "MP3";
  693. case HDMI_AUDIO_CODING_TYPE_MPEG2:
  694. return "MPEG2";
  695. case HDMI_AUDIO_CODING_TYPE_AAC_LC:
  696. return "AAC";
  697. case HDMI_AUDIO_CODING_TYPE_DTS:
  698. return "DTS";
  699. case HDMI_AUDIO_CODING_TYPE_ATRAC:
  700. return "ATRAC";
  701. case HDMI_AUDIO_CODING_TYPE_DSD:
  702. return "One Bit Audio";
  703. case HDMI_AUDIO_CODING_TYPE_EAC3:
  704. return "Dolby Digital +";
  705. case HDMI_AUDIO_CODING_TYPE_DTS_HD:
  706. return "DTS-HD";
  707. case HDMI_AUDIO_CODING_TYPE_MLP:
  708. return "MAT (MLP)";
  709. case HDMI_AUDIO_CODING_TYPE_DST:
  710. return "DST";
  711. case HDMI_AUDIO_CODING_TYPE_WMA_PRO:
  712. return "WMA PRO";
  713. case HDMI_AUDIO_CODING_TYPE_CXT:
  714. return "Refer to CXT";
  715. }
  716. return "Invalid";
  717. }
  718. static const char *
  719. hdmi_audio_sample_size_get_name(enum hdmi_audio_sample_size sample_size)
  720. {
  721. switch (sample_size) {
  722. case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
  723. return "Refer to Stream Header";
  724. case HDMI_AUDIO_SAMPLE_SIZE_16:
  725. return "16 bit";
  726. case HDMI_AUDIO_SAMPLE_SIZE_20:
  727. return "20 bit";
  728. case HDMI_AUDIO_SAMPLE_SIZE_24:
  729. return "24 bit";
  730. }
  731. return "Invalid";
  732. }
  733. static const char *
  734. hdmi_audio_sample_frequency_get_name(enum hdmi_audio_sample_frequency freq)
  735. {
  736. switch (freq) {
  737. case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
  738. return "Refer to Stream Header";
  739. case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
  740. return "32 kHz";
  741. case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
  742. return "44.1 kHz (CD)";
  743. case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
  744. return "48 kHz";
  745. case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
  746. return "88.2 kHz";
  747. case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
  748. return "96 kHz";
  749. case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
  750. return "176.4 kHz";
  751. case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
  752. return "192 kHz";
  753. }
  754. return "Invalid";
  755. }
  756. static const char *
  757. hdmi_audio_coding_type_ext_get_name(enum hdmi_audio_coding_type_ext ctx)
  758. {
  759. if ((int)ctx < 0 || ctx > 0x1f)
  760. return "Invalid";
  761. switch (ctx) {
  762. case HDMI_AUDIO_CODING_TYPE_EXT_CT:
  763. return "Refer to CT";
  764. case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC:
  765. return "HE AAC";
  766. case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2:
  767. return "HE AAC v2";
  768. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND:
  769. return "MPEG SURROUND";
  770. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC:
  771. return "MPEG-4 HE AAC";
  772. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_V2:
  773. return "MPEG-4 HE AAC v2";
  774. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC:
  775. return "MPEG-4 AAC LC";
  776. case HDMI_AUDIO_CODING_TYPE_EXT_DRA:
  777. return "DRA";
  778. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_SURROUND:
  779. return "MPEG-4 HE AAC + MPEG Surround";
  780. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC_SURROUND:
  781. return "MPEG-4 AAC LC + MPEG Surround";
  782. }
  783. return "Reserved";
  784. }
  785. /**
  786. * hdmi_audio_infoframe_log() - log info of HDMI AUDIO infoframe
  787. * @level: logging level
  788. * @dev: device
  789. * @frame: HDMI AUDIO infoframe
  790. */
  791. static void hdmi_audio_infoframe_log(const char *level,
  792. struct device *dev,
  793. struct hdmi_audio_infoframe *frame)
  794. {
  795. hdmi_infoframe_log_header(level, dev,
  796. (struct hdmi_any_infoframe *)frame);
  797. if (frame->channels)
  798. hdmi_log(" channels: %u\n", frame->channels - 1);
  799. else
  800. hdmi_log(" channels: Refer to stream header\n");
  801. hdmi_log(" coding type: %s\n",
  802. hdmi_audio_coding_type_get_name(frame->coding_type));
  803. hdmi_log(" sample size: %s\n",
  804. hdmi_audio_sample_size_get_name(frame->sample_size));
  805. hdmi_log(" sample frequency: %s\n",
  806. hdmi_audio_sample_frequency_get_name(frame->sample_frequency));
  807. hdmi_log(" coding type ext: %s\n",
  808. hdmi_audio_coding_type_ext_get_name(frame->coding_type_ext));
  809. hdmi_log(" channel allocation: 0x%x\n",
  810. frame->channel_allocation);
  811. hdmi_log(" level shift value: %u dB\n",
  812. frame->level_shift_value);
  813. hdmi_log(" downmix inhibit: %s\n",
  814. frame->downmix_inhibit ? "Yes" : "No");
  815. }
  816. static const char *
  817. hdmi_3d_structure_get_name(enum hdmi_3d_structure s3d_struct)
  818. {
  819. if (s3d_struct < 0 || s3d_struct > 0xf)
  820. return "Invalid";
  821. switch (s3d_struct) {
  822. case HDMI_3D_STRUCTURE_FRAME_PACKING:
  823. return "Frame Packing";
  824. case HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE:
  825. return "Field Alternative";
  826. case HDMI_3D_STRUCTURE_LINE_ALTERNATIVE:
  827. return "Line Alternative";
  828. case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL:
  829. return "Side-by-side (Full)";
  830. case HDMI_3D_STRUCTURE_L_DEPTH:
  831. return "L + Depth";
  832. case HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH:
  833. return "L + Depth + Graphics + Graphics-depth";
  834. case HDMI_3D_STRUCTURE_TOP_AND_BOTTOM:
  835. return "Top-and-Bottom";
  836. case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF:
  837. return "Side-by-side (Half)";
  838. default:
  839. break;
  840. }
  841. return "Reserved";
  842. }
  843. /**
  844. * hdmi_vendor_infoframe_log() - log info of HDMI VENDOR infoframe
  845. * @level: logging level
  846. * @dev: device
  847. * @frame: HDMI VENDOR infoframe
  848. */
  849. static void
  850. hdmi_vendor_any_infoframe_log(const char *level,
  851. struct device *dev,
  852. union hdmi_vendor_any_infoframe *frame)
  853. {
  854. struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
  855. hdmi_infoframe_log_header(level, dev,
  856. (struct hdmi_any_infoframe *)frame);
  857. if (frame->any.oui != HDMI_IEEE_OUI) {
  858. hdmi_log(" not a HDMI vendor infoframe\n");
  859. return;
  860. }
  861. if (hvf->vic == 0 && hvf->s3d_struct == HDMI_3D_STRUCTURE_INVALID) {
  862. hdmi_log(" empty frame\n");
  863. return;
  864. }
  865. if (hvf->vic)
  866. hdmi_log(" HDMI VIC: %u\n", hvf->vic);
  867. if (hvf->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
  868. hdmi_log(" 3D structure: %s\n",
  869. hdmi_3d_structure_get_name(hvf->s3d_struct));
  870. if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  871. hdmi_log(" 3D extension data: %d\n",
  872. hvf->s3d_ext_data);
  873. }
  874. }
  875. /**
  876. * hdmi_infoframe_log() - log info of HDMI infoframe
  877. * @level: logging level
  878. * @dev: device
  879. * @frame: HDMI infoframe
  880. */
  881. void hdmi_infoframe_log(const char *level,
  882. struct device *dev,
  883. union hdmi_infoframe *frame)
  884. {
  885. switch (frame->any.type) {
  886. case HDMI_INFOFRAME_TYPE_AVI:
  887. hdmi_avi_infoframe_log(level, dev, &frame->avi);
  888. break;
  889. case HDMI_INFOFRAME_TYPE_SPD:
  890. hdmi_spd_infoframe_log(level, dev, &frame->spd);
  891. break;
  892. case HDMI_INFOFRAME_TYPE_AUDIO:
  893. hdmi_audio_infoframe_log(level, dev, &frame->audio);
  894. break;
  895. case HDMI_INFOFRAME_TYPE_VENDOR:
  896. hdmi_vendor_any_infoframe_log(level, dev, &frame->vendor);
  897. break;
  898. default:
  899. break;
  900. }
  901. }
  902. //EXPORT_SYMBOL(hdmi_infoframe_log);
  903. /**
  904. * hdmi_avi_infoframe_unpack() - unpack binary buffer to a HDMI AVI infoframe
  905. * @buffer: source buffer
  906. * @frame: HDMI AVI infoframe
  907. *
  908. * Unpacks the information contained in binary @buffer into a structured
  909. * @frame of the HDMI Auxiliary Video (AVI) information frame.
  910. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  911. * specification.
  912. *
  913. * Returns 0 on success or a negative error code on failure.
  914. */
  915. static int hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe *frame,
  916. void *buffer)
  917. {
  918. u8 *ptr = buffer;
  919. int ret;
  920. if (ptr[0] != HDMI_INFOFRAME_TYPE_AVI ||
  921. ptr[1] != 2 ||
  922. ptr[2] != HDMI_AVI_INFOFRAME_SIZE)
  923. return -EINVAL;
  924. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AVI)) != 0)
  925. return -EINVAL;
  926. ret = hdmi_avi_infoframe_init(frame);
  927. if (ret)
  928. return ret;
  929. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  930. frame->colorspace = (ptr[0] >> 5) & 0x3;
  931. if (ptr[0] & 0x10)
  932. frame->active_aspect = ptr[1] & 0xf;
  933. if (ptr[0] & 0x8) {
  934. frame->top_bar = (ptr[5] << 8) + ptr[6];
  935. frame->bottom_bar = (ptr[7] << 8) + ptr[8];
  936. }
  937. if (ptr[0] & 0x4) {
  938. frame->left_bar = (ptr[9] << 8) + ptr[10];
  939. frame->right_bar = (ptr[11] << 8) + ptr[12];
  940. }
  941. frame->scan_mode = ptr[0] & 0x3;
  942. frame->colorimetry = (ptr[1] >> 6) & 0x3;
  943. frame->picture_aspect = (ptr[1] >> 4) & 0x3;
  944. frame->active_aspect = ptr[1] & 0xf;
  945. frame->itc = ptr[2] & 0x80 ? true : false;
  946. frame->extended_colorimetry = (ptr[2] >> 4) & 0x7;
  947. frame->quantization_range = (ptr[2] >> 2) & 0x3;
  948. frame->nups = ptr[2] & 0x3;
  949. frame->video_code = ptr[3] & 0x7f;
  950. frame->ycc_quantization_range = (ptr[4] >> 6) & 0x3;
  951. frame->content_type = (ptr[4] >> 4) & 0x3;
  952. frame->pixel_repeat = ptr[4] & 0xf;
  953. return 0;
  954. }
  955. /**
  956. * hdmi_spd_infoframe_unpack() - unpack binary buffer to a HDMI SPD infoframe
  957. * @buffer: source buffer
  958. * @frame: HDMI SPD infoframe
  959. *
  960. * Unpacks the information contained in binary @buffer into a structured
  961. * @frame of the HDMI Source Product Description (SPD) information frame.
  962. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  963. * specification.
  964. *
  965. * Returns 0 on success or a negative error code on failure.
  966. */
  967. static int hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe *frame,
  968. void *buffer)
  969. {
  970. u8 *ptr = buffer;
  971. int ret;
  972. if (ptr[0] != HDMI_INFOFRAME_TYPE_SPD ||
  973. ptr[1] != 1 ||
  974. ptr[2] != HDMI_SPD_INFOFRAME_SIZE) {
  975. return -EINVAL;
  976. }
  977. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(SPD)) != 0)
  978. return -EINVAL;
  979. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  980. ret = hdmi_spd_infoframe_init(frame, (const char*)ptr, (const char*)ptr + 8, (const char*)ptr + 24);
  981. if (ret)
  982. return ret;
  983. //frame->sdi = ptr[24];
  984. return 0;
  985. }
  986. /**
  987. * hdmi_audio_infoframe_unpack() - unpack binary buffer to a HDMI AUDIO infoframe
  988. * @buffer: source buffer
  989. * @frame: HDMI Audio infoframe
  990. *
  991. * Unpacks the information contained in binary @buffer into a structured
  992. * @frame of the HDMI Audio information frame.
  993. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  994. * specification.
  995. *
  996. * Returns 0 on success or a negative error code on failure.
  997. */
  998. static int hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe *frame,
  999. void *buffer)
  1000. {
  1001. u8 *ptr = buffer;
  1002. int ret;
  1003. if (ptr[0] != HDMI_INFOFRAME_TYPE_AUDIO ||
  1004. ptr[1] != 1 ||
  1005. ptr[2] != HDMI_AUDIO_INFOFRAME_SIZE) {
  1006. return -EINVAL;
  1007. }
  1008. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AUDIO)) != 0)
  1009. return -EINVAL;
  1010. ret = hdmi_audio_infoframe_init(frame);
  1011. if (ret)
  1012. return ret;
  1013. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1014. frame->channels = ptr[0] & 0x7;
  1015. frame->coding_type = (ptr[0] >> 4) & 0xf;
  1016. frame->sample_size = ptr[1] & 0x3;
  1017. frame->sample_frequency = (ptr[1] >> 2) & 0x7;
  1018. frame->coding_type_ext = ptr[2] & 0x1f;
  1019. frame->channel_allocation = ptr[3];
  1020. frame->level_shift_value = (ptr[4] >> 3) & 0xf;
  1021. frame->downmix_inhibit = ptr[4] & 0x80 ? true : false;
  1022. return 0;
  1023. }
  1024. /**
  1025. * hdmi_vendor_infoframe_unpack() - unpack binary buffer to a HDMI vendor infoframe
  1026. * @buffer: source buffer
  1027. * @frame: HDMI Vendor infoframe
  1028. *
  1029. * Unpacks the information contained in binary @buffer into a structured
  1030. * @frame of the HDMI Vendor information frame.
  1031. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1032. * specification.
  1033. *
  1034. * Returns 0 on success or a negative error code on failure.
  1035. */
  1036. static int
  1037. hdmi_vendor_any_infoframe_unpack(union hdmi_vendor_any_infoframe *frame,
  1038. void *buffer)
  1039. {
  1040. u8 *ptr = buffer;
  1041. size_t length;
  1042. int ret;
  1043. u8 hdmi_video_format;
  1044. struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
  1045. if (ptr[0] != HDMI_INFOFRAME_TYPE_VENDOR ||
  1046. ptr[1] != 1 ||
  1047. (ptr[2] != 5 && ptr[2] != 6))
  1048. return -EINVAL;
  1049. length = ptr[2];
  1050. if (hdmi_infoframe_checksum(buffer,
  1051. HDMI_INFOFRAME_HEADER_SIZE + length) != 0)
  1052. return -EINVAL;
  1053. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1054. /* HDMI OUI */
  1055. if ((ptr[0] != 0x03) ||
  1056. (ptr[1] != 0x0c) ||
  1057. (ptr[2] != 0x00))
  1058. return -EINVAL;
  1059. hdmi_video_format = ptr[3] >> 5;
  1060. if (hdmi_video_format > 0x2)
  1061. return -EINVAL;
  1062. ret = hdmi_vendor_infoframe_init(hvf);
  1063. if (ret)
  1064. return ret;
  1065. hvf->length = length;
  1066. if (hdmi_video_format == 0x1) {
  1067. hvf->vic = ptr[4];
  1068. } else if (hdmi_video_format == 0x2) {
  1069. hvf->s3d_struct = ptr[4] >> 4;
  1070. if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF) {
  1071. if (length == 6)
  1072. hvf->s3d_ext_data = ptr[5] >> 4;
  1073. else
  1074. return -EINVAL;
  1075. }
  1076. }
  1077. return 0;
  1078. }
  1079. /**
  1080. * hdmi_infoframe_unpack() - unpack binary buffer to a HDMI infoframe
  1081. * @buffer: source buffer
  1082. * @frame: HDMI infoframe
  1083. *
  1084. * Unpacks the information contained in binary buffer @buffer into a structured
  1085. * @frame of a HDMI infoframe.
  1086. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1087. * specification.
  1088. *
  1089. * Returns 0 on success or a negative error code on failure.
  1090. */
  1091. int hdmi_infoframe_unpack(union hdmi_infoframe *frame, void *buffer)
  1092. {
  1093. int ret;
  1094. u8 *ptr = buffer;
  1095. switch (ptr[0]) {
  1096. case HDMI_INFOFRAME_TYPE_AVI:
  1097. ret = hdmi_avi_infoframe_unpack(&frame->avi, buffer);
  1098. break;
  1099. case HDMI_INFOFRAME_TYPE_SPD:
  1100. ret = hdmi_spd_infoframe_unpack(&frame->spd, buffer);
  1101. break;
  1102. case HDMI_INFOFRAME_TYPE_AUDIO:
  1103. ret = hdmi_audio_infoframe_unpack(&frame->audio, buffer);
  1104. break;
  1105. case HDMI_INFOFRAME_TYPE_VENDOR:
  1106. ret = hdmi_vendor_any_infoframe_unpack(&frame->vendor, buffer);
  1107. break;
  1108. default:
  1109. ret = -EINVAL;
  1110. break;
  1111. }
  1112. return ret;
  1113. }
  1114. //EXPORT_SYMBOL(hdmi_infoframe_unpack);