hdmi.c 33 KB

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