hdmi.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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_64_27:
  453. return "64:27";
  454. case HDMI_PICTURE_ASPECT_256_135:
  455. return "256:135";
  456. case HDMI_PICTURE_ASPECT_RESERVED:
  457. return "Reserved";
  458. }
  459. return "Invalid";
  460. }
  461. static const char *
  462. hdmi_active_aspect_get_name(enum hdmi_active_aspect active_aspect)
  463. {
  464. if (active_aspect < 0 || active_aspect > 0xf)
  465. return "Invalid";
  466. switch (active_aspect) {
  467. case HDMI_ACTIVE_ASPECT_16_9_TOP:
  468. return "16:9 Top";
  469. case HDMI_ACTIVE_ASPECT_14_9_TOP:
  470. return "14:9 Top";
  471. case HDMI_ACTIVE_ASPECT_16_9_CENTER:
  472. return "16:9 Center";
  473. case HDMI_ACTIVE_ASPECT_PICTURE:
  474. return "Same as Picture";
  475. case HDMI_ACTIVE_ASPECT_4_3:
  476. return "4:3";
  477. case HDMI_ACTIVE_ASPECT_16_9:
  478. return "16:9";
  479. case HDMI_ACTIVE_ASPECT_14_9:
  480. return "14:9";
  481. case HDMI_ACTIVE_ASPECT_4_3_SP_14_9:
  482. return "4:3 SP 14:9";
  483. case HDMI_ACTIVE_ASPECT_16_9_SP_14_9:
  484. return "16:9 SP 14:9";
  485. case HDMI_ACTIVE_ASPECT_16_9_SP_4_3:
  486. return "16:9 SP 4:3";
  487. }
  488. return "Reserved";
  489. }
  490. static const char *
  491. hdmi_extended_colorimetry_get_name(enum hdmi_extended_colorimetry ext_col)
  492. {
  493. switch (ext_col) {
  494. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
  495. return "xvYCC 601";
  496. case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
  497. return "xvYCC 709";
  498. case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
  499. return "sYCC 601";
  500. case HDMI_EXTENDED_COLORIMETRY_ADOBE_YCC_601:
  501. return "Adobe YCC 601";
  502. case HDMI_EXTENDED_COLORIMETRY_ADOBE_RGB:
  503. return "Adobe RGB";
  504. case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
  505. return "BT.2020 Constant Luminance";
  506. case HDMI_EXTENDED_COLORIMETRY_BT2020:
  507. return "BT.2020";
  508. case HDMI_EXTENDED_COLORIMETRY_RESERVED:
  509. return "Reserved";
  510. }
  511. return "Invalid";
  512. }
  513. static const char *
  514. hdmi_quantization_range_get_name(enum hdmi_quantization_range qrange)
  515. {
  516. switch (qrange) {
  517. case HDMI_QUANTIZATION_RANGE_DEFAULT:
  518. return "Default";
  519. case HDMI_QUANTIZATION_RANGE_LIMITED:
  520. return "Limited";
  521. case HDMI_QUANTIZATION_RANGE_FULL:
  522. return "Full";
  523. case HDMI_QUANTIZATION_RANGE_RESERVED:
  524. return "Reserved";
  525. }
  526. return "Invalid";
  527. }
  528. static const char *hdmi_nups_get_name(enum hdmi_nups nups)
  529. {
  530. switch (nups) {
  531. case HDMI_NUPS_UNKNOWN:
  532. return "Unknown Non-uniform Scaling";
  533. case HDMI_NUPS_HORIZONTAL:
  534. return "Horizontally Scaled";
  535. case HDMI_NUPS_VERTICAL:
  536. return "Vertically Scaled";
  537. case HDMI_NUPS_BOTH:
  538. return "Horizontally and Vertically Scaled";
  539. }
  540. return "Invalid";
  541. }
  542. static const char *
  543. hdmi_ycc_quantization_range_get_name(enum hdmi_ycc_quantization_range qrange)
  544. {
  545. switch (qrange) {
  546. case HDMI_YCC_QUANTIZATION_RANGE_LIMITED:
  547. return "Limited";
  548. case HDMI_YCC_QUANTIZATION_RANGE_FULL:
  549. return "Full";
  550. }
  551. return "Invalid";
  552. }
  553. static const char *
  554. hdmi_content_type_get_name(enum hdmi_content_type content_type)
  555. {
  556. switch (content_type) {
  557. case HDMI_CONTENT_TYPE_GRAPHICS:
  558. return "Graphics";
  559. case HDMI_CONTENT_TYPE_PHOTO:
  560. return "Photo";
  561. case HDMI_CONTENT_TYPE_CINEMA:
  562. return "Cinema";
  563. case HDMI_CONTENT_TYPE_GAME:
  564. return "Game";
  565. }
  566. return "Invalid";
  567. }
  568. /**
  569. * hdmi_avi_infoframe_log() - log info of HDMI AVI infoframe
  570. * @level: logging level
  571. * @dev: device
  572. * @frame: HDMI AVI infoframe
  573. */
  574. static void hdmi_avi_infoframe_log(const char *level,
  575. struct device *dev,
  576. struct hdmi_avi_infoframe *frame)
  577. {
  578. hdmi_infoframe_log_header(level, dev,
  579. (struct hdmi_any_infoframe *)frame);
  580. hdmi_log(" colorspace: %s\n",
  581. hdmi_colorspace_get_name(frame->colorspace));
  582. hdmi_log(" scan mode: %s\n",
  583. hdmi_scan_mode_get_name(frame->scan_mode));
  584. hdmi_log(" colorimetry: %s\n",
  585. hdmi_colorimetry_get_name(frame->colorimetry));
  586. hdmi_log(" picture aspect: %s\n",
  587. hdmi_picture_aspect_get_name(frame->picture_aspect));
  588. hdmi_log(" active aspect: %s\n",
  589. hdmi_active_aspect_get_name(frame->active_aspect));
  590. hdmi_log(" itc: %s\n", frame->itc ? "IT Content" : "No Data");
  591. hdmi_log(" extended colorimetry: %s\n",
  592. hdmi_extended_colorimetry_get_name(frame->extended_colorimetry));
  593. hdmi_log(" quantization range: %s\n",
  594. hdmi_quantization_range_get_name(frame->quantization_range));
  595. hdmi_log(" nups: %s\n", hdmi_nups_get_name(frame->nups));
  596. hdmi_log(" video code: %u\n", frame->video_code);
  597. hdmi_log(" ycc quantization range: %s\n",
  598. hdmi_ycc_quantization_range_get_name(frame->ycc_quantization_range));
  599. hdmi_log(" hdmi content type: %s\n",
  600. hdmi_content_type_get_name(frame->content_type));
  601. hdmi_log(" pixel repeat: %u\n", frame->pixel_repeat);
  602. hdmi_log(" bar top %u, bottom %u, left %u, right %u\n",
  603. frame->top_bar, frame->bottom_bar,
  604. frame->left_bar, frame->right_bar);
  605. }
  606. static const char *hdmi_spd_sdi_get_name(enum hdmi_spd_sdi sdi)
  607. {
  608. if (sdi < 0 || sdi > 0xff)
  609. return "Invalid";
  610. switch (sdi) {
  611. case HDMI_SPD_SDI_UNKNOWN:
  612. return "Unknown";
  613. case HDMI_SPD_SDI_DSTB:
  614. return "Digital STB";
  615. case HDMI_SPD_SDI_DVDP:
  616. return "DVD Player";
  617. case HDMI_SPD_SDI_DVHS:
  618. return "D-VHS";
  619. case HDMI_SPD_SDI_HDDVR:
  620. return "HDD Videorecorder";
  621. case HDMI_SPD_SDI_DVC:
  622. return "DVC";
  623. case HDMI_SPD_SDI_DSC:
  624. return "DSC";
  625. case HDMI_SPD_SDI_VCD:
  626. return "Video CD";
  627. case HDMI_SPD_SDI_GAME:
  628. return "Game";
  629. case HDMI_SPD_SDI_PC:
  630. return "PC General";
  631. case HDMI_SPD_SDI_BD:
  632. return "Blu-Ray Disc (BD)";
  633. case HDMI_SPD_SDI_SACD:
  634. return "Super Audio CD";
  635. case HDMI_SPD_SDI_HDDVD:
  636. return "HD DVD";
  637. case HDMI_SPD_SDI_PMP:
  638. return "PMP";
  639. }
  640. return "Reserved";
  641. }
  642. /**
  643. * hdmi_spd_infoframe_log() - log info of HDMI SPD infoframe
  644. * @level: logging level
  645. * @dev: device
  646. * @frame: HDMI SPD infoframe
  647. */
  648. static void hdmi_spd_infoframe_log(const char *level,
  649. struct device *dev,
  650. struct hdmi_spd_infoframe *frame)
  651. {
  652. u8 buf[17];
  653. hdmi_infoframe_log_header(level, dev,
  654. (struct hdmi_any_infoframe *)frame);
  655. memset(buf, 0, sizeof(buf));
  656. strncpy(buf, frame->vendor, 8);
  657. hdmi_log(" vendor: %s\n", buf);
  658. strncpy(buf, frame->product, 16);
  659. hdmi_log(" product: %s\n", buf);
  660. hdmi_log(" source device information: %s (0x%x)\n",
  661. hdmi_spd_sdi_get_name(frame->sdi), frame->sdi);
  662. }
  663. static const char *
  664. hdmi_audio_coding_type_get_name(enum hdmi_audio_coding_type coding_type)
  665. {
  666. switch (coding_type) {
  667. case HDMI_AUDIO_CODING_TYPE_STREAM:
  668. return "Refer to Stream Header";
  669. case HDMI_AUDIO_CODING_TYPE_PCM:
  670. return "PCM";
  671. case HDMI_AUDIO_CODING_TYPE_AC3:
  672. return "AC-3";
  673. case HDMI_AUDIO_CODING_TYPE_MPEG1:
  674. return "MPEG1";
  675. case HDMI_AUDIO_CODING_TYPE_MP3:
  676. return "MP3";
  677. case HDMI_AUDIO_CODING_TYPE_MPEG2:
  678. return "MPEG2";
  679. case HDMI_AUDIO_CODING_TYPE_AAC_LC:
  680. return "AAC";
  681. case HDMI_AUDIO_CODING_TYPE_DTS:
  682. return "DTS";
  683. case HDMI_AUDIO_CODING_TYPE_ATRAC:
  684. return "ATRAC";
  685. case HDMI_AUDIO_CODING_TYPE_DSD:
  686. return "One Bit Audio";
  687. case HDMI_AUDIO_CODING_TYPE_EAC3:
  688. return "Dolby Digital +";
  689. case HDMI_AUDIO_CODING_TYPE_DTS_HD:
  690. return "DTS-HD";
  691. case HDMI_AUDIO_CODING_TYPE_MLP:
  692. return "MAT (MLP)";
  693. case HDMI_AUDIO_CODING_TYPE_DST:
  694. return "DST";
  695. case HDMI_AUDIO_CODING_TYPE_WMA_PRO:
  696. return "WMA PRO";
  697. case HDMI_AUDIO_CODING_TYPE_CXT:
  698. return "Refer to CXT";
  699. }
  700. return "Invalid";
  701. }
  702. static const char *
  703. hdmi_audio_sample_size_get_name(enum hdmi_audio_sample_size sample_size)
  704. {
  705. switch (sample_size) {
  706. case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
  707. return "Refer to Stream Header";
  708. case HDMI_AUDIO_SAMPLE_SIZE_16:
  709. return "16 bit";
  710. case HDMI_AUDIO_SAMPLE_SIZE_20:
  711. return "20 bit";
  712. case HDMI_AUDIO_SAMPLE_SIZE_24:
  713. return "24 bit";
  714. }
  715. return "Invalid";
  716. }
  717. static const char *
  718. hdmi_audio_sample_frequency_get_name(enum hdmi_audio_sample_frequency freq)
  719. {
  720. switch (freq) {
  721. case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
  722. return "Refer to Stream Header";
  723. case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
  724. return "32 kHz";
  725. case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
  726. return "44.1 kHz (CD)";
  727. case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
  728. return "48 kHz";
  729. case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
  730. return "88.2 kHz";
  731. case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
  732. return "96 kHz";
  733. case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
  734. return "176.4 kHz";
  735. case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
  736. return "192 kHz";
  737. }
  738. return "Invalid";
  739. }
  740. static const char *
  741. hdmi_audio_coding_type_ext_get_name(enum hdmi_audio_coding_type_ext ctx)
  742. {
  743. if (ctx < 0 || ctx > 0x1f)
  744. return "Invalid";
  745. switch (ctx) {
  746. case HDMI_AUDIO_CODING_TYPE_EXT_CT:
  747. return "Refer to CT";
  748. case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC:
  749. return "HE AAC";
  750. case HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2:
  751. return "HE AAC v2";
  752. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND:
  753. return "MPEG SURROUND";
  754. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC:
  755. return "MPEG-4 HE AAC";
  756. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_V2:
  757. return "MPEG-4 HE AAC v2";
  758. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC:
  759. return "MPEG-4 AAC LC";
  760. case HDMI_AUDIO_CODING_TYPE_EXT_DRA:
  761. return "DRA";
  762. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_SURROUND:
  763. return "MPEG-4 HE AAC + MPEG Surround";
  764. case HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC_SURROUND:
  765. return "MPEG-4 AAC LC + MPEG Surround";
  766. }
  767. return "Reserved";
  768. }
  769. /**
  770. * hdmi_audio_infoframe_log() - log info of HDMI AUDIO infoframe
  771. * @level: logging level
  772. * @dev: device
  773. * @frame: HDMI AUDIO infoframe
  774. */
  775. static void hdmi_audio_infoframe_log(const char *level,
  776. struct device *dev,
  777. struct hdmi_audio_infoframe *frame)
  778. {
  779. hdmi_infoframe_log_header(level, dev,
  780. (struct hdmi_any_infoframe *)frame);
  781. if (frame->channels)
  782. hdmi_log(" channels: %u\n", frame->channels - 1);
  783. else
  784. hdmi_log(" channels: Refer to stream header\n");
  785. hdmi_log(" coding type: %s\n",
  786. hdmi_audio_coding_type_get_name(frame->coding_type));
  787. hdmi_log(" sample size: %s\n",
  788. hdmi_audio_sample_size_get_name(frame->sample_size));
  789. hdmi_log(" sample frequency: %s\n",
  790. hdmi_audio_sample_frequency_get_name(frame->sample_frequency));
  791. hdmi_log(" coding type ext: %s\n",
  792. hdmi_audio_coding_type_ext_get_name(frame->coding_type_ext));
  793. hdmi_log(" channel allocation: 0x%x\n",
  794. frame->channel_allocation);
  795. hdmi_log(" level shift value: %u dB\n",
  796. frame->level_shift_value);
  797. hdmi_log(" downmix inhibit: %s\n",
  798. frame->downmix_inhibit ? "Yes" : "No");
  799. }
  800. static const char *
  801. hdmi_3d_structure_get_name(enum hdmi_3d_structure s3d_struct)
  802. {
  803. if (s3d_struct < 0 || s3d_struct > 0xf)
  804. return "Invalid";
  805. switch (s3d_struct) {
  806. case HDMI_3D_STRUCTURE_FRAME_PACKING:
  807. return "Frame Packing";
  808. case HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE:
  809. return "Field Alternative";
  810. case HDMI_3D_STRUCTURE_LINE_ALTERNATIVE:
  811. return "Line Alternative";
  812. case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL:
  813. return "Side-by-side (Full)";
  814. case HDMI_3D_STRUCTURE_L_DEPTH:
  815. return "L + Depth";
  816. case HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH:
  817. return "L + Depth + Graphics + Graphics-depth";
  818. case HDMI_3D_STRUCTURE_TOP_AND_BOTTOM:
  819. return "Top-and-Bottom";
  820. case HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF:
  821. return "Side-by-side (Half)";
  822. default:
  823. break;
  824. }
  825. return "Reserved";
  826. }
  827. /**
  828. * hdmi_vendor_infoframe_log() - log info of HDMI VENDOR infoframe
  829. * @level: logging level
  830. * @dev: device
  831. * @frame: HDMI VENDOR infoframe
  832. */
  833. static void
  834. hdmi_vendor_any_infoframe_log(const char *level,
  835. struct device *dev,
  836. union hdmi_vendor_any_infoframe *frame)
  837. {
  838. struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
  839. hdmi_infoframe_log_header(level, dev,
  840. (struct hdmi_any_infoframe *)frame);
  841. if (frame->any.oui != HDMI_IEEE_OUI) {
  842. hdmi_log(" not a HDMI vendor infoframe\n");
  843. return;
  844. }
  845. if (hvf->vic == 0 && hvf->s3d_struct == HDMI_3D_STRUCTURE_INVALID) {
  846. hdmi_log(" empty frame\n");
  847. return;
  848. }
  849. if (hvf->vic)
  850. hdmi_log(" HDMI VIC: %u\n", hvf->vic);
  851. if (hvf->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
  852. hdmi_log(" 3D structure: %s\n",
  853. hdmi_3d_structure_get_name(hvf->s3d_struct));
  854. if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  855. hdmi_log(" 3D extension data: %d\n",
  856. hvf->s3d_ext_data);
  857. }
  858. }
  859. /**
  860. * hdmi_infoframe_log() - log info of HDMI infoframe
  861. * @level: logging level
  862. * @dev: device
  863. * @frame: HDMI infoframe
  864. */
  865. void hdmi_infoframe_log(const char *level,
  866. struct device *dev,
  867. union hdmi_infoframe *frame)
  868. {
  869. switch (frame->any.type) {
  870. case HDMI_INFOFRAME_TYPE_AVI:
  871. hdmi_avi_infoframe_log(level, dev, &frame->avi);
  872. break;
  873. case HDMI_INFOFRAME_TYPE_SPD:
  874. hdmi_spd_infoframe_log(level, dev, &frame->spd);
  875. break;
  876. case HDMI_INFOFRAME_TYPE_AUDIO:
  877. hdmi_audio_infoframe_log(level, dev, &frame->audio);
  878. break;
  879. case HDMI_INFOFRAME_TYPE_VENDOR:
  880. hdmi_vendor_any_infoframe_log(level, dev, &frame->vendor);
  881. break;
  882. }
  883. }
  884. EXPORT_SYMBOL(hdmi_infoframe_log);
  885. /**
  886. * hdmi_avi_infoframe_unpack() - unpack binary buffer to a HDMI AVI infoframe
  887. * @buffer: source buffer
  888. * @frame: HDMI AVI infoframe
  889. *
  890. * Unpacks the information contained in binary @buffer into a structured
  891. * @frame of the HDMI Auxiliary Video (AVI) information frame.
  892. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  893. * specification.
  894. *
  895. * Returns 0 on success or a negative error code on failure.
  896. */
  897. static int hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe *frame,
  898. void *buffer)
  899. {
  900. u8 *ptr = buffer;
  901. int ret;
  902. if (ptr[0] != HDMI_INFOFRAME_TYPE_AVI ||
  903. ptr[1] != 2 ||
  904. ptr[2] != HDMI_AVI_INFOFRAME_SIZE)
  905. return -EINVAL;
  906. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AVI)) != 0)
  907. return -EINVAL;
  908. ret = hdmi_avi_infoframe_init(frame);
  909. if (ret)
  910. return ret;
  911. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  912. frame->colorspace = (ptr[0] >> 5) & 0x3;
  913. if (ptr[0] & 0x10)
  914. frame->active_aspect = ptr[1] & 0xf;
  915. if (ptr[0] & 0x8) {
  916. frame->top_bar = (ptr[5] << 8) + ptr[6];
  917. frame->bottom_bar = (ptr[7] << 8) + ptr[8];
  918. }
  919. if (ptr[0] & 0x4) {
  920. frame->left_bar = (ptr[9] << 8) + ptr[10];
  921. frame->right_bar = (ptr[11] << 8) + ptr[12];
  922. }
  923. frame->scan_mode = ptr[0] & 0x3;
  924. frame->colorimetry = (ptr[1] >> 6) & 0x3;
  925. frame->picture_aspect = (ptr[1] >> 4) & 0x3;
  926. frame->active_aspect = ptr[1] & 0xf;
  927. frame->itc = ptr[2] & 0x80 ? true : false;
  928. frame->extended_colorimetry = (ptr[2] >> 4) & 0x7;
  929. frame->quantization_range = (ptr[2] >> 2) & 0x3;
  930. frame->nups = ptr[2] & 0x3;
  931. frame->video_code = ptr[3] & 0x7f;
  932. frame->ycc_quantization_range = (ptr[4] >> 6) & 0x3;
  933. frame->content_type = (ptr[4] >> 4) & 0x3;
  934. frame->pixel_repeat = ptr[4] & 0xf;
  935. return 0;
  936. }
  937. /**
  938. * hdmi_spd_infoframe_unpack() - unpack binary buffer to a HDMI SPD infoframe
  939. * @buffer: source buffer
  940. * @frame: HDMI SPD infoframe
  941. *
  942. * Unpacks the information contained in binary @buffer into a structured
  943. * @frame of the HDMI Source Product Description (SPD) information frame.
  944. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  945. * specification.
  946. *
  947. * Returns 0 on success or a negative error code on failure.
  948. */
  949. static int hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe *frame,
  950. void *buffer)
  951. {
  952. u8 *ptr = buffer;
  953. int ret;
  954. if (ptr[0] != HDMI_INFOFRAME_TYPE_SPD ||
  955. ptr[1] != 1 ||
  956. ptr[2] != HDMI_SPD_INFOFRAME_SIZE) {
  957. return -EINVAL;
  958. }
  959. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(SPD)) != 0)
  960. return -EINVAL;
  961. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  962. ret = hdmi_spd_infoframe_init(frame, ptr, ptr + 8);
  963. if (ret)
  964. return ret;
  965. frame->sdi = ptr[24];
  966. return 0;
  967. }
  968. /**
  969. * hdmi_audio_infoframe_unpack() - unpack binary buffer to a HDMI AUDIO infoframe
  970. * @buffer: source buffer
  971. * @frame: HDMI Audio infoframe
  972. *
  973. * Unpacks the information contained in binary @buffer into a structured
  974. * @frame of the HDMI Audio information frame.
  975. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  976. * specification.
  977. *
  978. * Returns 0 on success or a negative error code on failure.
  979. */
  980. static int hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe *frame,
  981. void *buffer)
  982. {
  983. u8 *ptr = buffer;
  984. int ret;
  985. if (ptr[0] != HDMI_INFOFRAME_TYPE_AUDIO ||
  986. ptr[1] != 1 ||
  987. ptr[2] != HDMI_AUDIO_INFOFRAME_SIZE) {
  988. return -EINVAL;
  989. }
  990. if (hdmi_infoframe_checksum(buffer, HDMI_INFOFRAME_SIZE(AUDIO)) != 0)
  991. return -EINVAL;
  992. ret = hdmi_audio_infoframe_init(frame);
  993. if (ret)
  994. return ret;
  995. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  996. frame->channels = ptr[0] & 0x7;
  997. frame->coding_type = (ptr[0] >> 4) & 0xf;
  998. frame->sample_size = ptr[1] & 0x3;
  999. frame->sample_frequency = (ptr[1] >> 2) & 0x7;
  1000. frame->coding_type_ext = ptr[2] & 0x1f;
  1001. frame->channel_allocation = ptr[3];
  1002. frame->level_shift_value = (ptr[4] >> 3) & 0xf;
  1003. frame->downmix_inhibit = ptr[4] & 0x80 ? true : false;
  1004. return 0;
  1005. }
  1006. /**
  1007. * hdmi_vendor_infoframe_unpack() - unpack binary buffer to a HDMI vendor infoframe
  1008. * @buffer: source buffer
  1009. * @frame: HDMI Vendor infoframe
  1010. *
  1011. * Unpacks the information contained in binary @buffer into a structured
  1012. * @frame of the HDMI Vendor information frame.
  1013. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1014. * specification.
  1015. *
  1016. * Returns 0 on success or a negative error code on failure.
  1017. */
  1018. static int
  1019. hdmi_vendor_any_infoframe_unpack(union hdmi_vendor_any_infoframe *frame,
  1020. void *buffer)
  1021. {
  1022. u8 *ptr = buffer;
  1023. size_t length;
  1024. int ret;
  1025. u8 hdmi_video_format;
  1026. struct hdmi_vendor_infoframe *hvf = &frame->hdmi;
  1027. if (ptr[0] != HDMI_INFOFRAME_TYPE_VENDOR ||
  1028. ptr[1] != 1 ||
  1029. (ptr[2] != 5 && ptr[2] != 6))
  1030. return -EINVAL;
  1031. length = ptr[2];
  1032. if (hdmi_infoframe_checksum(buffer,
  1033. HDMI_INFOFRAME_HEADER_SIZE + length) != 0)
  1034. return -EINVAL;
  1035. ptr += HDMI_INFOFRAME_HEADER_SIZE;
  1036. /* HDMI OUI */
  1037. if ((ptr[0] != 0x03) ||
  1038. (ptr[1] != 0x0c) ||
  1039. (ptr[2] != 0x00))
  1040. return -EINVAL;
  1041. hdmi_video_format = ptr[3] >> 5;
  1042. if (hdmi_video_format > 0x2)
  1043. return -EINVAL;
  1044. ret = hdmi_vendor_infoframe_init(hvf);
  1045. if (ret)
  1046. return ret;
  1047. hvf->length = length;
  1048. if (hdmi_video_format == 0x1) {
  1049. hvf->vic = ptr[4];
  1050. } else if (hdmi_video_format == 0x2) {
  1051. hvf->s3d_struct = ptr[4] >> 4;
  1052. if (hvf->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF) {
  1053. if (length == 6)
  1054. hvf->s3d_ext_data = ptr[5] >> 4;
  1055. else
  1056. return -EINVAL;
  1057. }
  1058. }
  1059. return 0;
  1060. }
  1061. /**
  1062. * hdmi_infoframe_unpack() - unpack binary buffer to a HDMI infoframe
  1063. * @buffer: source buffer
  1064. * @frame: HDMI infoframe
  1065. *
  1066. * Unpacks the information contained in binary buffer @buffer into a structured
  1067. * @frame of a HDMI infoframe.
  1068. * Also verifies the checksum as required by section 5.3.5 of the HDMI 1.4
  1069. * specification.
  1070. *
  1071. * Returns 0 on success or a negative error code on failure.
  1072. */
  1073. int hdmi_infoframe_unpack(union hdmi_infoframe *frame, void *buffer)
  1074. {
  1075. int ret;
  1076. u8 *ptr = buffer;
  1077. switch (ptr[0]) {
  1078. case HDMI_INFOFRAME_TYPE_AVI:
  1079. ret = hdmi_avi_infoframe_unpack(&frame->avi, buffer);
  1080. break;
  1081. case HDMI_INFOFRAME_TYPE_SPD:
  1082. ret = hdmi_spd_infoframe_unpack(&frame->spd, buffer);
  1083. break;
  1084. case HDMI_INFOFRAME_TYPE_AUDIO:
  1085. ret = hdmi_audio_infoframe_unpack(&frame->audio, buffer);
  1086. break;
  1087. case HDMI_INFOFRAME_TYPE_VENDOR:
  1088. ret = hdmi_vendor_any_infoframe_unpack(&frame->vendor, buffer);
  1089. break;
  1090. default:
  1091. ret = -EINVAL;
  1092. break;
  1093. }
  1094. return ret;
  1095. }
  1096. EXPORT_SYMBOL(hdmi_infoframe_unpack);