hdmi.c 33 KB

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