hdmi.c 32 KB

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