mdss_hdmi_hdcp.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /* Copyright (c) 2010-2013 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/types.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <linux/stat.h>
  17. #include <linux/module.h>
  18. #include "mdss_hdmi_hdcp.h"
  19. #ifndef CONFIG_VIDEO_MHL_V2
  20. #include "video/msm_hdmi_hdcp_mgr.h"
  21. #endif
  22. #include "mdss_hdmi_edid.h"
  23. #define HDCP_STATE_NAME (hdcp_state_name(hdcp_ctrl->hdcp_state))
  24. /* HDCP Keys state based on HDMI_HDCP_LINK0_STATUS:KEYS_STATE */
  25. #define HDCP_KEYS_STATE_NO_KEYS 0
  26. #define HDCP_KEYS_STATE_NOT_CHECKED 1
  27. #define HDCP_KEYS_STATE_CHECKING 2
  28. #define HDCP_KEYS_STATE_VALID 3
  29. #define HDCP_KEYS_STATE_AKSV_NOT_VALID 4
  30. #define HDCP_KEYS_STATE_CHKSUM_MISMATCH 5
  31. #define HDCP_KEYS_STATE_PROD_AKSV 6
  32. #define HDCP_KEYS_STATE_RESERVED 7
  33. #define HDCP_INT_CLR (BIT(1) | BIT(5) | BIT(7) | BIT(9) | BIT(13))
  34. #ifdef CONFIG_VIDEO_MHL_V2
  35. struct hdmi_hdcp_ctrl *hdcp_ctrl_global = NULL;
  36. EXPORT_SYMBOL(hdcp_ctrl_global);
  37. extern int hdmi_hpd_status(void);
  38. #endif
  39. const char *hdcp_state_name(enum hdmi_hdcp_state hdcp_state)
  40. {
  41. switch (hdcp_state) {
  42. case HDCP_STATE_INACTIVE: return "HDCP_STATE_INACTIVE";
  43. case HDCP_STATE_AUTHENTICATING: return "HDCP_STATE_AUTHENTICATING";
  44. case HDCP_STATE_AUTHENTICATED: return "HDCP_STATE_AUTHENTICATED";
  45. case HDCP_STATE_AUTH_FAIL: return "HDCP_STATE_AUTH_FAIL";
  46. default: return "???";
  47. }
  48. } /* hdcp_state_name */
  49. static int hdmi_hdcp_count_one(u8 *array, u8 len)
  50. {
  51. int i, j, count = 0;
  52. for (i = 0; i < len; i++)
  53. for (j = 0; j < 8; j++)
  54. count += (((array[i] >> j) & 0x1) ? 1 : 0);
  55. return count;
  56. } /* hdmi_hdcp_count_one */
  57. static void reset_hdcp_ddc_failures(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  58. {
  59. int hdcp_ddc_ctrl1_reg;
  60. int hdcp_ddc_status;
  61. int failure;
  62. int nack0;
  63. struct dss_io_data *io;
  64. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io) {
  65. DEV_ERR("%s: invalid input\n", __func__);
  66. return;
  67. }
  68. io = hdcp_ctrl->init_data.core_io;
  69. /* Check for any DDC transfer failures */
  70. hdcp_ddc_status = DSS_REG_R(io, HDMI_HDCP_DDC_STATUS);
  71. failure = (hdcp_ddc_status >> 16) & 0x1;
  72. nack0 = (hdcp_ddc_status >> 14) & 0x1;
  73. DEV_DBG("%s: %s: On Entry: HDCP_DDC_STATUS=0x%x, FAIL=%d, NACK0=%d\n",
  74. __func__, HDCP_STATE_NAME, hdcp_ddc_status, failure, nack0);
  75. if (failure == 0x1) {
  76. /*
  77. * Indicates that the last HDCP HW DDC transfer failed.
  78. * This occurs when a transfer is attempted with HDCP DDC
  79. * disabled (HDCP_DDC_DISABLE=1) or the number of retries
  80. * matches HDCP_DDC_RETRY_CNT.
  81. * Failure occured, let's clear it.
  82. */
  83. DEV_DBG("%s: %s: DDC failure detected.HDCP_DDC_STATUS=0x%08x\n",
  84. __func__, HDCP_STATE_NAME, hdcp_ddc_status);
  85. /* First, Disable DDC */
  86. DSS_REG_W(io, HDMI_HDCP_DDC_CTRL_0, BIT(0));
  87. /* ACK the Failure to Clear it */
  88. hdcp_ddc_ctrl1_reg = DSS_REG_R(io, HDMI_HDCP_DDC_CTRL_1);
  89. DSS_REG_W(io, HDMI_HDCP_DDC_CTRL_1,
  90. hdcp_ddc_ctrl1_reg | BIT(0));
  91. /* Check if the FAILURE got Cleared */
  92. hdcp_ddc_status = DSS_REG_R(io, HDMI_HDCP_DDC_STATUS);
  93. hdcp_ddc_status = (hdcp_ddc_status >> 16) & BIT(0);
  94. if (hdcp_ddc_status == 0x0)
  95. DEV_DBG("%s: %s: HDCP DDC Failure cleared\n", __func__,
  96. HDCP_STATE_NAME);
  97. else
  98. DEV_WARN("%s: %s: Unable to clear HDCP DDC Failure",
  99. __func__, HDCP_STATE_NAME);
  100. /* Re-Enable HDCP DDC */
  101. DSS_REG_W(io, HDMI_HDCP_DDC_CTRL_0, 0);
  102. }
  103. if (nack0 == 0x1) {
  104. DEV_DBG("%s: %s: Before: HDMI_DDC_SW_STATUS=0x%08x\n", __func__,
  105. HDCP_STATE_NAME, DSS_REG_R(io, HDMI_DDC_SW_STATUS));
  106. /* Reset HDMI DDC software status */
  107. DSS_REG_W_ND(io, HDMI_DDC_CTRL,
  108. DSS_REG_R(io, HDMI_DDC_CTRL) | BIT(3));
  109. msleep(20);
  110. DSS_REG_W_ND(io, HDMI_DDC_CTRL,
  111. DSS_REG_R(io, HDMI_DDC_CTRL) & ~(BIT(3)));
  112. /* Reset HDMI DDC Controller */
  113. DSS_REG_W_ND(io, HDMI_DDC_CTRL,
  114. DSS_REG_R(io, HDMI_DDC_CTRL) | BIT(1));
  115. msleep(20);
  116. DSS_REG_W_ND(io, HDMI_DDC_CTRL,
  117. DSS_REG_R(io, HDMI_DDC_CTRL) & ~BIT(1));
  118. DEV_DBG("%s: %s: After: HDMI_DDC_SW_STATUS=0x%08x\n", __func__,
  119. HDCP_STATE_NAME, DSS_REG_R(io, HDMI_DDC_SW_STATUS));
  120. }
  121. hdcp_ddc_status = DSS_REG_R(io, HDMI_HDCP_DDC_STATUS);
  122. failure = (hdcp_ddc_status >> 16) & BIT(0);
  123. nack0 = (hdcp_ddc_status >> 14) & BIT(0);
  124. DEV_DBG("%s: %s: On Exit: HDCP_DDC_STATUS=0x%x, FAIL=%d, NACK0=%d\n",
  125. __func__, HDCP_STATE_NAME, hdcp_ddc_status, failure, nack0);
  126. } /* reset_hdcp_ddc_failures */
  127. static void hdmi_hdcp_hw_ddc_clean(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  128. {
  129. struct dss_io_data *io = NULL;
  130. u32 hdcp_ddc_status, ddc_hw_status;
  131. u32 ddc_xfer_done, ddc_xfer_req, ddc_hw_done;
  132. u32 ddc_hw_not_ready;
  133. u32 timeout_count;
  134. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io) {
  135. DEV_ERR("%s: invalid input\n", __func__);
  136. return;
  137. }
  138. io = hdcp_ctrl->init_data.core_io;
  139. if (!io->base) {
  140. DEV_ERR("%s: core io not inititalized\n", __func__);
  141. return;
  142. }
  143. if (DSS_REG_R(io, HDMI_DDC_HW_STATUS) != 0) {
  144. /* Wait to be clean on DDC HW engine */
  145. timeout_count = 100;
  146. do {
  147. hdcp_ddc_status = DSS_REG_R(io, HDMI_HDCP_DDC_STATUS);
  148. ddc_hw_status = DSS_REG_R(io, HDMI_DDC_HW_STATUS);
  149. ddc_xfer_done = (hdcp_ddc_status & BIT(10)) ;
  150. ddc_xfer_req = (hdcp_ddc_status & BIT(4)) ;
  151. ddc_hw_done = (ddc_hw_status & BIT(3)) ;
  152. ddc_hw_not_ready = ((ddc_xfer_done != 1) ||
  153. (ddc_xfer_req != 0) || (ddc_hw_done != 1));
  154. DEV_DBG("%s: %s: timeout count(%d):ddc hw%sready\n",
  155. __func__, HDCP_STATE_NAME, timeout_count,
  156. ddc_hw_not_ready ? " not " : " ");
  157. DEV_DBG("hdcp_ddc_status[0x%x], ddc_hw_status[0x%x]\n",
  158. hdcp_ddc_status, ddc_hw_status);
  159. if (ddc_hw_not_ready)
  160. msleep(20);
  161. } while (ddc_hw_not_ready && --timeout_count);
  162. }
  163. } /* hdmi_hdcp_hw_ddc_clean */
  164. static int hdmi_hdcp_authentication_part1(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  165. {
  166. int rc;
  167. u32 qfprom_aksv_lsb, qfprom_aksv_msb;
  168. u32 link0_aksv_0, link0_aksv_1;
  169. u32 link0_bksv_0, link0_bksv_1;
  170. u32 link0_an_0, link0_an_1;
  171. u32 timeout_count;
  172. bool is_match;
  173. bool stale_an = false;
  174. struct dss_io_data *io;
  175. u8 aksv[5], *bksv = NULL;
  176. u8 an[8];
  177. u8 bcaps;
  178. struct hdmi_tx_ddc_data ddc_data;
  179. u32 link0_status, an_ready, keys_state;
  180. u8 buf[0xFF];
  181. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io ||
  182. !hdcp_ctrl->init_data.qfprom_io) {
  183. DEV_ERR("%s: invalid input\n", __func__);
  184. rc = -EINVAL;
  185. goto error;
  186. }
  187. #if !defined (CONFIG_VIDEO_MHL_V2) || defined (CONFIG_VIDEO_MHL_SII8246)
  188. if (HDCP_STATE_AUTHENTICATING != hdcp_ctrl->hdcp_state) {
  189. DEV_DBG("%s: %s: invalid state. returning\n", __func__,
  190. HDCP_STATE_NAME);
  191. rc = -EINVAL;
  192. goto error;
  193. }
  194. #endif
  195. bksv = hdcp_ctrl->current_tp.bksv;
  196. io = hdcp_ctrl->init_data.core_io;
  197. /* Fetch aksv from QFPROM, this info should be public. */
  198. qfprom_aksv_lsb = DSS_REG_R(hdcp_ctrl->init_data.qfprom_io,
  199. HDCP_KSV_LSB);
  200. qfprom_aksv_msb = DSS_REG_R(hdcp_ctrl->init_data.qfprom_io,
  201. HDCP_KSV_MSB);
  202. aksv[0] = qfprom_aksv_lsb & 0xFF;
  203. aksv[1] = (qfprom_aksv_lsb >> 8) & 0xFF;
  204. aksv[2] = (qfprom_aksv_lsb >> 16) & 0xFF;
  205. aksv[3] = (qfprom_aksv_lsb >> 24) & 0xFF;
  206. aksv[4] = qfprom_aksv_msb & 0xFF;
  207. /* check there are 20 ones in AKSV */
  208. if (hdmi_hdcp_count_one(aksv, 5) != 20) {
  209. DEV_ERR("%s: %s: AKSV QFPROM doesn't have 20 1's, 20 0's\n",
  210. __func__, HDCP_STATE_NAME);
  211. DEV_ERR("%s: %s: QFPROM AKSV chk failed (AKSV=%02x%08x)\n",
  212. __func__, HDCP_STATE_NAME, qfprom_aksv_msb,
  213. qfprom_aksv_lsb);
  214. rc = -EINVAL;
  215. goto error;
  216. }
  217. DEV_DBG("%s: %s: AKSV=%02x%08x\n", __func__, HDCP_STATE_NAME,
  218. qfprom_aksv_msb, qfprom_aksv_lsb);
  219. /*
  220. * Write AKSV read from QFPROM to the HDCP registers.
  221. * This step is needed for HDCP authentication and must be
  222. * written before enabling HDCP.
  223. */
  224. DSS_REG_W(io, HDMI_HDCP_SW_LOWER_AKSV, qfprom_aksv_lsb);
  225. DSS_REG_W(io, HDMI_HDCP_SW_UPPER_AKSV, qfprom_aksv_msb);
  226. /* Check to see if link0_Status has stale values for An ready bit */
  227. link0_status = DSS_REG_R(io, HDMI_HDCP_LINK0_STATUS);
  228. DEV_DBG("%s: %s: Before enabling cipher Link0_status=0x%08x\n",
  229. __func__, HDCP_STATE_NAME, link0_status);
  230. if (link0_status & (BIT(8) | BIT(9))) {
  231. DEV_DBG("%s: %s: An ready even before enabling HDCP\n",
  232. __func__, HDCP_STATE_NAME);
  233. stale_an = true;
  234. }
  235. /*
  236. * Read BCAPS
  237. * We need to first try to read an HDCP register on the sink to see if
  238. * the sink is ready for HDCP authentication
  239. */
  240. memset(&ddc_data, 0, sizeof(ddc_data));
  241. ddc_data.dev_addr = 0x74;
  242. ddc_data.offset = 0x40;
  243. ddc_data.data_buf = &bcaps;
  244. ddc_data.data_len = 1;
  245. ddc_data.request_len = 1;
  246. ddc_data.retry = 5;
  247. ddc_data.what = "Bcaps";
  248. ddc_data.no_align = true;
  249. rc = hdmi_ddc_read(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  250. if (rc) {
  251. DEV_ERR("%s: %s: BCAPS read failed\n", __func__,
  252. HDCP_STATE_NAME);
  253. goto error;
  254. }
  255. DEV_DBG("%s: %s: BCAPS=%02x\n", __func__, HDCP_STATE_NAME, bcaps);
  256. /* receiver (0), repeater (1) */
  257. hdcp_ctrl->current_tp.ds_type =
  258. (bcaps & BIT(6)) >> 6 ? DS_REPEATER : DS_RECEIVER;
  259. /*
  260. * HDCP setup prior to enabling HDCP_CTRL.
  261. * Setup seed values for random number An.
  262. */
  263. DSS_REG_W(io, HDMI_HDCP_ENTROPY_CTRL0, 0xB1FFB0FF);
  264. DSS_REG_W(io, HDMI_HDCP_ENTROPY_CTRL1, 0xF00DFACE);
  265. /* Disable the RngCipher state */
  266. DSS_REG_W(io, HDMI_HDCP_DEBUG_CTRL,
  267. DSS_REG_R(io, HDMI_HDCP_DEBUG_CTRL) & ~(BIT(2)));
  268. DEV_DBG("%s: %s: HDCP_DEBUG_CTRL=0x%08x\n", __func__, HDCP_STATE_NAME,
  269. DSS_REG_R(io, HDMI_HDCP_DEBUG_CTRL));
  270. /* Ensure that all register writes are completed before
  271. * enabling HDCP cipher
  272. */
  273. wmb();
  274. /*
  275. * Enable HDCP
  276. * This needs to be done as early as possible in order for the
  277. * hardware to make An available to read
  278. */
  279. DSS_REG_W(io, HDMI_HDCP_CTRL, BIT(0));
  280. /* Clear any DDC failures from previous tries */
  281. reset_hdcp_ddc_failures(hdcp_ctrl);
  282. /* Write BCAPS to the hardware */
  283. DSS_REG_W(io, HDMI_HDCP_RCVPORT_DATA12, bcaps);
  284. /*
  285. * If we had stale values for the An ready bit, it should most
  286. * likely be cleared now after enabling HDCP cipher
  287. */
  288. link0_status = DSS_REG_R(io, HDMI_HDCP_LINK0_STATUS);
  289. DEV_DBG("%s: %s: After enabling HDCP Link0_Status=0x%08x\n",
  290. __func__, HDCP_STATE_NAME, link0_status);
  291. if (!(link0_status & (BIT(8) | BIT(9)))) {
  292. DEV_DBG("%s: %s: An not ready after enabling HDCP\n",
  293. __func__, HDCP_STATE_NAME);
  294. stale_an = false;
  295. }
  296. /* Wait for HDCP keys to be checked and validated */
  297. timeout_count = 100;
  298. keys_state = (link0_status >> 28) & 0x7;
  299. while ((keys_state != HDCP_KEYS_STATE_VALID) &&
  300. --timeout_count) {
  301. link0_status = DSS_REG_R(io, HDMI_HDCP_LINK0_STATUS);
  302. keys_state = (link0_status >> 28) & 0x7;
  303. DEV_DBG("%s: %s: Keys not ready(%d). s=%d\n, l0=%0x08x",
  304. __func__, HDCP_STATE_NAME, timeout_count,
  305. keys_state, link0_status);
  306. msleep(20);
  307. }
  308. if (!timeout_count) {
  309. DEV_ERR("%s: %s: Invalid Keys State: %d\n", __func__,
  310. HDCP_STATE_NAME, keys_state);
  311. rc = -EINVAL;
  312. goto error;
  313. }
  314. /*
  315. * 1.1_Features turned off by default.
  316. * No need to write AInfo since 1.1_Features is disabled.
  317. */
  318. DSS_REG_W(io, HDMI_HDCP_RCVPORT_DATA4, 0);
  319. /* Wait for An0 and An1 bit to be ready */
  320. timeout_count = 100;
  321. do {
  322. link0_status = DSS_REG_R(io, HDMI_HDCP_LINK0_STATUS);
  323. an_ready = (link0_status & BIT(8)) && (link0_status & BIT(9));
  324. if (!an_ready) {
  325. DEV_DBG("%s: %s: An not ready(%d). l0_status=0x%08x\n",
  326. __func__, HDCP_STATE_NAME, timeout_count,
  327. link0_status);
  328. msleep(20);
  329. }
  330. } while (!an_ready && --timeout_count);
  331. if (!timeout_count) {
  332. rc = -ETIMEDOUT;
  333. DEV_ERR("%s: %s: timedout, An0=%ld, An1=%ld\n", __func__,
  334. HDCP_STATE_NAME, (link0_status & BIT(8)) >> 8,
  335. (link0_status & BIT(9)) >> 9);
  336. goto error;
  337. }
  338. /*
  339. * In cases where An_ready bits had stale values, it would be
  340. * better to delay reading of An to avoid any potential of this
  341. * read being blocked
  342. */
  343. if (stale_an) {
  344. msleep(200);
  345. stale_an = false;
  346. }
  347. /* Read An0 and An1 */
  348. link0_an_0 = DSS_REG_R(io, HDMI_HDCP_RCVPORT_DATA5);
  349. link0_an_1 = DSS_REG_R(io, HDMI_HDCP_RCVPORT_DATA6);
  350. /* Read AKSV */
  351. link0_aksv_0 = DSS_REG_R(io, HDMI_HDCP_RCVPORT_DATA3);
  352. link0_aksv_1 = DSS_REG_R(io, HDMI_HDCP_RCVPORT_DATA4);
  353. /* Copy An and AKSV to byte arrays for transmission */
  354. aksv[0] = link0_aksv_0 & 0xFF;
  355. aksv[1] = (link0_aksv_0 >> 8) & 0xFF;
  356. aksv[2] = (link0_aksv_0 >> 16) & 0xFF;
  357. aksv[3] = (link0_aksv_0 >> 24) & 0xFF;
  358. aksv[4] = link0_aksv_1 & 0xFF;
  359. an[0] = link0_an_0 & 0xFF;
  360. an[1] = (link0_an_0 >> 8) & 0xFF;
  361. an[2] = (link0_an_0 >> 16) & 0xFF;
  362. an[3] = (link0_an_0 >> 24) & 0xFF;
  363. an[4] = link0_an_1 & 0xFF;
  364. an[5] = (link0_an_1 >> 8) & 0xFF;
  365. an[6] = (link0_an_1 >> 16) & 0xFF;
  366. an[7] = (link0_an_1 >> 24) & 0xFF;
  367. /* Write An to offset 0x18 */
  368. memset(&ddc_data, 0, sizeof(ddc_data));
  369. ddc_data.dev_addr = 0x74;
  370. ddc_data.offset = 0x18;
  371. ddc_data.data_buf = an;
  372. ddc_data.data_len = 8;
  373. ddc_data.what = "An";
  374. rc = hdmi_ddc_write(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  375. if (rc) {
  376. DEV_ERR("%s: %s: An write failed\n", __func__, HDCP_STATE_NAME);
  377. goto error;
  378. }
  379. /* Write AKSV to offset 0x10 */
  380. memset(&ddc_data, 0, sizeof(ddc_data));
  381. ddc_data.dev_addr = 0x74;
  382. ddc_data.offset = 0x10;
  383. ddc_data.data_buf = aksv;
  384. ddc_data.data_len = 5;
  385. ddc_data.what = "Aksv";
  386. rc = hdmi_ddc_write(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  387. if (rc) {
  388. DEV_ERR("%s: %s: AKSV write failed\n", __func__,
  389. HDCP_STATE_NAME);
  390. goto error;
  391. }
  392. DEV_DBG("%s: %s: Link0-AKSV=%02x%08x\n", __func__,
  393. HDCP_STATE_NAME, link0_aksv_1 & 0xFF, link0_aksv_0);
  394. /* Read BKSV at offset 0x00 */
  395. memset(&ddc_data, 0, sizeof(ddc_data));
  396. ddc_data.dev_addr = 0x74;
  397. ddc_data.offset = 0x00;
  398. ddc_data.data_buf = bksv;
  399. ddc_data.data_len = 5;
  400. ddc_data.request_len = 5;
  401. ddc_data.retry = 5;
  402. ddc_data.what = "Bksv";
  403. ddc_data.no_align = true;
  404. rc = hdmi_ddc_read(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  405. if (rc) {
  406. DEV_ERR("%s: %s: BKSV read failed\n", __func__,
  407. HDCP_STATE_NAME);
  408. goto error;
  409. }
  410. /* check there are 20 ones in BKSV */
  411. if (hdmi_hdcp_count_one(bksv, 5) != 20) {
  412. DEV_ERR("%s: %s: BKSV doesn't have 20 1's and 20 0's\n",
  413. __func__, HDCP_STATE_NAME);
  414. DEV_ERR("%s: %s: BKSV chk fail. BKSV=%02x%02x%02x%02x%02x\n",
  415. __func__, HDCP_STATE_NAME, bksv[4], bksv[3], bksv[2],
  416. bksv[1], bksv[0]);
  417. rc = -EINVAL;
  418. goto error;
  419. }
  420. link0_bksv_0 = bksv[3];
  421. link0_bksv_0 = (link0_bksv_0 << 8) | bksv[2];
  422. link0_bksv_0 = (link0_bksv_0 << 8) | bksv[1];
  423. link0_bksv_0 = (link0_bksv_0 << 8) | bksv[0];
  424. link0_bksv_1 = bksv[4];
  425. DEV_DBG("%s: %s: BKSV=%02x%08x\n", __func__, HDCP_STATE_NAME,
  426. link0_bksv_1, link0_bksv_0);
  427. /* Write BKSV read from sink to HDCP registers */
  428. DSS_REG_W(io, HDMI_HDCP_RCVPORT_DATA0, link0_bksv_0);
  429. DSS_REG_W(io, HDMI_HDCP_RCVPORT_DATA1, link0_bksv_1);
  430. /* Enable HDCP interrupts and ack/clear any stale interrupts */
  431. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, 0xE6);
  432. /*
  433. * HDCP Compliace Test case 1A-01:
  434. * Wait here at least 100ms before reading R0'
  435. */
  436. msleep(125);
  437. /* Read R0' at offset 0x08 */
  438. memset(buf, 0, sizeof(buf));
  439. memset(&ddc_data, 0, sizeof(ddc_data));
  440. ddc_data.dev_addr = 0x74;
  441. ddc_data.offset = 0x08;
  442. ddc_data.data_buf = buf;
  443. ddc_data.data_len = 2;
  444. ddc_data.request_len = 2;
  445. ddc_data.retry = 5;
  446. ddc_data.what = "R0'";
  447. ddc_data.no_align = true;
  448. rc = hdmi_ddc_read(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  449. if (rc) {
  450. DEV_ERR("%s: %s: R0' read failed\n", __func__, HDCP_STATE_NAME);
  451. goto error;
  452. }
  453. DEV_DBG("%s: %s: R0'=%02x%02x\n", __func__, HDCP_STATE_NAME,
  454. buf[1], buf[0]);
  455. /* Write R0' to HDCP registers and check to see if it is a match */
  456. INIT_COMPLETION(hdcp_ctrl->r0_checked);
  457. DSS_REG_W(io, HDMI_HDCP_RCVPORT_DATA2_0, (((u32)buf[1]) << 8) | buf[0]);
  458. timeout_count = wait_for_completion_timeout(
  459. &hdcp_ctrl->r0_checked, HZ*2);
  460. link0_status = DSS_REG_R(io, HDMI_HDCP_LINK0_STATUS);
  461. is_match = link0_status & BIT(12);
  462. if (!is_match) {
  463. DEV_DBG("%s: %s: Link0_Status=0x%08x\n", __func__,
  464. HDCP_STATE_NAME, link0_status);
  465. if (!timeout_count) {
  466. DEV_ERR("%s: %s: Timeout. No R0 mtch. R0'=%02x%02x\n",
  467. __func__, HDCP_STATE_NAME, buf[1], buf[0]);
  468. rc = -ETIMEDOUT;
  469. goto error;
  470. } else {
  471. DEV_ERR("%s: %s: R0 mismatch. R0'=%02x%02x\n", __func__,
  472. HDCP_STATE_NAME, buf[1], buf[0]);
  473. rc = -EINVAL;
  474. goto error;
  475. }
  476. } else {
  477. DEV_DBG("%s: %s: R0 matches\n", __func__, HDCP_STATE_NAME);
  478. }
  479. error:
  480. if (rc) {
  481. DEV_ERR("%s: %s: Authentication Part I failed\n", __func__,
  482. HDCP_STATE_NAME);
  483. } else {
  484. /* Enable HDCP Encryption */
  485. DEV_INFO("%s: %s: Authentication Part I successful\n",
  486. __func__, HDCP_STATE_NAME);
  487. #ifdef CONFIG_VIDEO_MHL_V2
  488. DSS_REG_W(io, HDMI_HDCP_CTRL, BIT(0));
  489. /* if REPEATER (Bit 6), perform Part2 Authentication */
  490. if (!(bcaps & BIT(6))) {
  491. DEV_INFO("%s: %s: auth part II skipped, no repeater\n",
  492. __func__, HDCP_STATE_NAME);
  493. } else {
  494. /* Wait until READY bit is set in BCAPS */
  495. timeout_count = 50;
  496. while (!(bcaps & BIT(5)) && timeout_count) {
  497. msleep(100);
  498. timeout_count--;
  499. /* Read BCAPS at offset 0x40 */
  500. memset(&ddc_data, 0, sizeof(ddc_data));
  501. ddc_data.dev_addr = 0x74;
  502. ddc_data.offset = 0x40;
  503. ddc_data.data_buf = &bcaps;
  504. ddc_data.data_len = 1;
  505. ddc_data.request_len = 1;
  506. ddc_data.retry = 5;
  507. ddc_data.what = "Bcaps";
  508. ddc_data.no_align = false;
  509. rc = hdmi_ddc_read(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  510. if (rc) {
  511. DEV_ERR("%s: %s: BCAPS read failed\n", __func__,
  512. HDCP_STATE_NAME);
  513. break;
  514. }
  515. }
  516. }
  517. if (timeout_count == 0)
  518. rc = -EINVAL;
  519. #else
  520. DSS_REG_W(io, HDMI_HDCP_CTRL, BIT(0) | BIT(8));
  521. #endif
  522. }
  523. return rc;
  524. } /* hdmi_hdcp_authentication_part1 */
  525. #ifdef CONFIG_VIDEO_MHL_V2
  526. static int hdcp_return_value;
  527. static void hdmi_hdcp_auth_part1_work(struct work_struct *work)
  528. {
  529. struct delayed_work *dw = to_delayed_work(work);
  530. struct hdmi_hdcp_ctrl *hdcp_ctrl = container_of(dw,
  531. struct hdmi_hdcp_ctrl, hdcp_auth_work);
  532. if (!hdcp_ctrl) {
  533. DEV_ERR("%s: invalid input\n", __func__);
  534. return;
  535. }
  536. hdcp_return_value =
  537. hdmi_hdcp_authentication_part1(hdcp_ctrl);
  538. }
  539. int hdmi_hdcp_authentication_part1_start(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  540. {
  541. queue_delayed_work(hdcp_ctrl->init_data.workq,
  542. &hdcp_ctrl->hdcp_auth_work, 0);
  543. flush_delayed_work(&hdcp_ctrl->hdcp_auth_work);
  544. return hdcp_return_value;
  545. }
  546. #endif
  547. #define READ_WRITE_V_H(off, name, reg) \
  548. do { \
  549. ddc_data.offset = (off); \
  550. memset(what, 0, sizeof(what)); \
  551. snprintf(what, sizeof(what), (name)); \
  552. rc = hdmi_ddc_read(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data); \
  553. if (rc) { \
  554. DEV_ERR("%s: %s: Read %s failed\n", __func__, HDCP_STATE_NAME, \
  555. what); \
  556. goto error; \
  557. } \
  558. DEV_DBG("%s: %s: %s: buf[0]=%x, buf[1]=%x, buf[2]=%x, buf[3]=%x\n", \
  559. __func__, HDCP_STATE_NAME, what, buf[0], buf[1], \
  560. buf[2], buf[3]); \
  561. DSS_REG_W(io, (reg), \
  562. (buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0])); \
  563. } while (0);
  564. #ifndef CONFIG_VIDEO_MHL_V2
  565. static int hdmi_hdcp_transfer_v_h(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  566. {
  567. char what[20];
  568. int rc = 0;
  569. u8 buf[4];
  570. struct hdmi_tx_ddc_data ddc_data;
  571. struct dss_io_data *io;
  572. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io) {
  573. DEV_ERR("%s: invalid input\n", __func__);
  574. return -EINVAL;
  575. }
  576. io = hdcp_ctrl->init_data.core_io;
  577. memset(&ddc_data, 0, sizeof(ddc_data));
  578. ddc_data.dev_addr = 0x74;
  579. ddc_data.data_buf = buf;
  580. ddc_data.data_len = 4;
  581. ddc_data.request_len = 4;
  582. ddc_data.retry = 5;
  583. ddc_data.what = what;
  584. ddc_data.no_align = true;
  585. /* Read V'.HO 4 Byte at offset 0x20 */
  586. READ_WRITE_V_H(0x20, "V' H0", HDMI_HDCP_RCVPORT_DATA7);
  587. /* Read V'.H1 4 Byte at offset 0x24 */
  588. READ_WRITE_V_H(0x24, "V' H1", HDMI_HDCP_RCVPORT_DATA8);
  589. /* Read V'.H2 4 Byte at offset 0x28 */
  590. READ_WRITE_V_H(0x28, "V' H2", HDMI_HDCP_RCVPORT_DATA9);
  591. /* Read V'.H3 4 Byte at offset 0x2C */
  592. READ_WRITE_V_H(0x2C, "V' H3", HDMI_HDCP_RCVPORT_DATA10);
  593. /* Read V'.H4 4 Byte at offset 0x30 */
  594. READ_WRITE_V_H(0x30, "V' H4", HDMI_HDCP_RCVPORT_DATA11);
  595. error:
  596. return rc;
  597. }
  598. static int hdmi_hdcp_authentication_part2(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  599. {
  600. int rc, cnt, i;
  601. struct hdmi_tx_ddc_data ddc_data;
  602. u32 timeout_count, down_stream_devices = 0;
  603. u32 repeater_cascade_depth = 0;
  604. u8 buf[0xFF];
  605. u8 *ksv_fifo = NULL;
  606. u8 bcaps;
  607. u16 bstatus, max_devs_exceeded = 0, max_cascade_exceeded = 0;
  608. u32 link0_status;
  609. u32 ksv_bytes;
  610. struct dss_io_data *io;
  611. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io) {
  612. DEV_ERR("%s: invalid input\n", __func__);
  613. rc = -EINVAL;
  614. goto error;
  615. }
  616. if (HDCP_STATE_AUTHENTICATING != hdcp_ctrl->hdcp_state) {
  617. DEV_DBG("%s: %s: invalid state. returning\n", __func__,
  618. HDCP_STATE_NAME);
  619. rc = -EINVAL;
  620. goto error;
  621. }
  622. ksv_fifo = hdcp_ctrl->current_tp.ksv_list;
  623. io = hdcp_ctrl->init_data.core_io;
  624. memset(buf, 0, sizeof(buf));
  625. memset(ksv_fifo, 0,
  626. sizeof(hdcp_ctrl->current_tp.ksv_list));
  627. /*
  628. * Wait until READY bit is set in BCAPS, as per HDCP specifications
  629. * maximum permitted time to check for READY bit is five seconds.
  630. */
  631. timeout_count = 50;
  632. do {
  633. timeout_count--;
  634. /* Read BCAPS at offset 0x40 */
  635. memset(&ddc_data, 0, sizeof(ddc_data));
  636. ddc_data.dev_addr = 0x74;
  637. ddc_data.offset = 0x40;
  638. ddc_data.data_buf = &bcaps;
  639. ddc_data.data_len = 1;
  640. ddc_data.request_len = 1;
  641. ddc_data.retry = 5;
  642. ddc_data.what = "Bcaps";
  643. ddc_data.no_align = false;
  644. rc = hdmi_ddc_read(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  645. if (rc) {
  646. DEV_ERR("%s: %s: BCAPS read failed\n", __func__,
  647. HDCP_STATE_NAME);
  648. goto error;
  649. }
  650. msleep(100);
  651. } while (!(bcaps & BIT(5)) && timeout_count);
  652. /* Read BSTATUS at offset 0x41 */
  653. memset(&ddc_data, 0, sizeof(ddc_data));
  654. ddc_data.dev_addr = 0x74;
  655. ddc_data.offset = 0x41;
  656. ddc_data.data_buf = buf;
  657. ddc_data.data_len = 2;
  658. ddc_data.request_len = 2;
  659. ddc_data.retry = 5;
  660. ddc_data.what = "Bstatuss";
  661. ddc_data.no_align = false;
  662. rc = hdmi_ddc_read(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  663. if (rc) {
  664. DEV_ERR("%s: %s: BSTATUS read failed\n", __func__,
  665. HDCP_STATE_NAME);
  666. goto error;
  667. }
  668. bstatus = buf[1];
  669. bstatus = (bstatus << 8) | buf[0];
  670. /* Write BSTATUS and BCAPS to HDCP registers */
  671. DSS_REG_W(io, HDMI_HDCP_RCVPORT_DATA12, bcaps | (bstatus << 8));
  672. down_stream_devices = bstatus & 0x7F;
  673. if (down_stream_devices == 0) {
  674. /*
  675. * If no downstream devices are attached to the repeater
  676. * then part II fails.
  677. * todo: The other approach would be to continue PART II.
  678. */
  679. DEV_ERR("%s: %s: No downstream devices\n", __func__,
  680. HDCP_STATE_NAME);
  681. rc = -EINVAL;
  682. goto error;
  683. }
  684. /* Cascaded repeater depth */
  685. repeater_cascade_depth = (bstatus >> 8) & 0x7;
  686. /*
  687. * HDCP Compliance 1B-05:
  688. * Check if no. of devices connected to repeater
  689. * exceed max_devices_connected from bit 7 of Bstatus.
  690. */
  691. max_devs_exceeded = (bstatus & BIT(7)) >> 7;
  692. if (max_devs_exceeded == 0x01) {
  693. DEV_ERR("%s: %s: no. of devs connected exceeds max allowed",
  694. __func__, HDCP_STATE_NAME);
  695. rc = -EINVAL;
  696. goto error;
  697. }
  698. /*
  699. * HDCP Compliance 1B-06:
  700. * Check if no. of cascade connected to repeater
  701. * exceed max_cascade_connected from bit 11 of Bstatus.
  702. */
  703. max_cascade_exceeded = (bstatus & BIT(11)) >> 11;
  704. if (max_cascade_exceeded == 0x01) {
  705. DEV_ERR("%s: %s: no. of cascade conn exceeds max allowed",
  706. __func__, HDCP_STATE_NAME);
  707. rc = -EINVAL;
  708. goto error;
  709. }
  710. /*
  711. * Read KSV FIFO over DDC
  712. * Key Slection vector FIFO Used to pull downstream KSVs
  713. * from HDCP Repeaters.
  714. * All bytes (DEVICE_COUNT * 5) must be read in a single,
  715. * auto incrementing access.
  716. * All bytes read as 0x00 for HDCP Receivers that are not
  717. * HDCP Repeaters (REPEATER == 0).
  718. */
  719. ksv_bytes = 5 * down_stream_devices;
  720. memset(&ddc_data, 0, sizeof(ddc_data));
  721. ddc_data.dev_addr = 0x74;
  722. ddc_data.offset = 0x43;
  723. ddc_data.data_buf = ksv_fifo;
  724. ddc_data.data_len = ksv_bytes;
  725. ddc_data.request_len = ksv_bytes;
  726. ddc_data.retry = 5;
  727. ddc_data.what = "KSV FIFO";
  728. ddc_data.no_align = true;
  729. cnt = 0;
  730. do {
  731. rc = hdmi_ddc_read(hdcp_ctrl->init_data.ddc_ctrl, &ddc_data);
  732. if (rc) {
  733. DEV_ERR("%s: %s: KSV FIFO read failed\n", __func__,
  734. HDCP_STATE_NAME);
  735. /*
  736. * HDCP Compliace Test case 1B-01:
  737. * Wait here until all the ksv bytes have been
  738. * read from the KSV FIFO register.
  739. */
  740. msleep(25);
  741. } else {
  742. break;
  743. }
  744. cnt++;
  745. } while (cnt != 20);
  746. if (cnt == 20)
  747. goto error;
  748. rc = hdmi_hdcp_transfer_v_h(hdcp_ctrl);
  749. if (rc)
  750. goto error;
  751. /*
  752. * Write KSV FIFO to HDCP_SHA_DATA.
  753. * This is done 1 byte at time starting with the LSB.
  754. * On the very last byte write, the HDCP_SHA_DATA_DONE bit[0]
  755. */
  756. /* First, reset SHA engine */
  757. DSS_REG_W(io, HDMI_HDCP_SHA_CTRL, 1);
  758. /* Next, enable SHA engine, SEL=DIGA_HDCP */
  759. DSS_REG_W(io, HDMI_HDCP_SHA_CTRL, 0);
  760. for (i = 0; i < ksv_bytes - 1; i++) {
  761. /* Write KSV byte and do not set DONE bit[0] */
  762. DSS_REG_W_ND(io, HDMI_HDCP_SHA_DATA, ksv_fifo[i] << 16);
  763. /*
  764. * Once 64 bytes have been written, we need to poll for
  765. * HDCP_SHA_BLOCK_DONE before writing any further
  766. */
  767. if (i && !((i + 1) % 64)) {
  768. timeout_count = 100;
  769. while (!(DSS_REG_R(io, HDMI_HDCP_SHA_STATUS) & BIT(0))
  770. && (--timeout_count)) {
  771. DEV_DBG("%s: %s: Wrote 64 bytes KVS FIFO\n",
  772. __func__, HDCP_STATE_NAME);
  773. DEV_DBG("%s: %s: HDCP_SHA_STATUS=%08x\n",
  774. __func__, HDCP_STATE_NAME,
  775. DSS_REG_R(io, HDMI_HDCP_SHA_STATUS));
  776. msleep(20);
  777. }
  778. if (!timeout_count) {
  779. rc = -ETIMEDOUT;
  780. DEV_ERR("%s: %s: Write KSV FIFO timedout",
  781. __func__, HDCP_STATE_NAME);
  782. goto error;
  783. }
  784. }
  785. }
  786. /* Write l to DONE bit[0] */
  787. DSS_REG_W_ND(io, HDMI_HDCP_SHA_DATA,
  788. (ksv_fifo[ksv_bytes - 1] << 16) | 0x1);
  789. /* Now wait for HDCP_SHA_COMP_DONE */
  790. timeout_count = 100;
  791. while ((0x10 != (DSS_REG_R(io, HDMI_HDCP_SHA_STATUS)
  792. & 0xFFFFFF10)) && --timeout_count)
  793. msleep(20);
  794. if (!timeout_count) {
  795. rc = -ETIMEDOUT;
  796. DEV_ERR("%s: %s: SHA computation timedout", __func__,
  797. HDCP_STATE_NAME);
  798. goto error;
  799. }
  800. /* Wait for V_MATCHES */
  801. timeout_count = 100;
  802. link0_status = DSS_REG_R(io, HDMI_HDCP_LINK0_STATUS);
  803. while (((link0_status & BIT(20)) != BIT(20)) && --timeout_count) {
  804. DEV_DBG("%s: %s: Waiting for V_MATCHES(%d). l0_status=0x%08x\n",
  805. __func__, HDCP_STATE_NAME, timeout_count, link0_status);
  806. msleep(20);
  807. link0_status = DSS_REG_R(io, HDMI_HDCP_LINK0_STATUS);
  808. }
  809. if (!timeout_count) {
  810. rc = -ETIMEDOUT;
  811. DEV_ERR("%s: %s: HDCP V Match timedout", __func__,
  812. HDCP_STATE_NAME);
  813. goto error;
  814. }
  815. error:
  816. if (rc)
  817. DEV_ERR("%s: %s: Authentication Part II failed\n", __func__,
  818. HDCP_STATE_NAME);
  819. else
  820. DEV_INFO("%s: %s: Authentication Part II successful\n",
  821. __func__, HDCP_STATE_NAME);
  822. /* Update topology information */
  823. hdcp_ctrl->current_tp.dev_count = down_stream_devices;
  824. hdcp_ctrl->current_tp.max_cascade_exceeded = max_cascade_exceeded;
  825. hdcp_ctrl->current_tp.max_dev_exceeded = max_devs_exceeded;
  826. hdcp_ctrl->current_tp.depth = repeater_cascade_depth;
  827. return rc;
  828. } /* hdmi_hdcp_authentication_part2 */
  829. static void hdmi_hdcp_cache_topology(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  830. {
  831. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io) {
  832. DEV_ERR("%s: invalid input\n", __func__);
  833. return;
  834. }
  835. memcpy((void *)&hdcp_ctrl->cached_tp,
  836. (void *) &hdcp_ctrl->current_tp,
  837. sizeof(hdcp_ctrl->cached_tp));
  838. }
  839. static void hdmi_hdcp_notify_topology(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  840. {
  841. char a[16], b[16];
  842. char *envp[] = {
  843. [0] = "HDCP_MGR_EVENT=MSG_READY",
  844. [1] = a,
  845. [2] = b,
  846. NULL,
  847. };
  848. snprintf(envp[1], 16, "%d", (int)DOWN_CHECK_TOPOLOGY);
  849. snprintf(envp[2], 16, "%d", (int)HDCP_V1_TX);
  850. kobject_uevent_env(hdcp_ctrl->init_data.sysfs_kobj, KOBJ_CHANGE, envp);
  851. DEV_DBG("%s Event Sent: %s msgID = %s srcID = %s\n", __func__,
  852. envp[0], envp[1], envp[2]);
  853. }
  854. #endif
  855. static void hdmi_hdcp_int_work(struct work_struct *work)
  856. {
  857. struct hdmi_hdcp_ctrl *hdcp_ctrl = container_of(work,
  858. struct hdmi_hdcp_ctrl, hdcp_int_work);
  859. if (!hdcp_ctrl) {
  860. DEV_ERR("%s: invalid input\n", __func__);
  861. return;
  862. }
  863. mutex_lock(hdcp_ctrl->init_data.mutex);
  864. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTH_FAIL;
  865. mutex_unlock(hdcp_ctrl->init_data.mutex);
  866. if (hdcp_ctrl->init_data.notify_status) {
  867. hdcp_ctrl->init_data.notify_status(
  868. hdcp_ctrl->init_data.cb_data,
  869. hdcp_ctrl->hdcp_state);
  870. }
  871. } /* hdmi_hdcp_int_work */
  872. #ifndef CONFIG_VIDEO_MHL_V2
  873. static void hdmi_hdcp_auth_work(struct work_struct *work)
  874. {
  875. int rc;
  876. struct delayed_work *dw = to_delayed_work(work);
  877. struct hdmi_hdcp_ctrl *hdcp_ctrl = container_of(dw,
  878. struct hdmi_hdcp_ctrl, hdcp_auth_work);
  879. struct dss_io_data *io;
  880. if (!hdcp_ctrl) {
  881. DEV_ERR("%s: invalid input\n", __func__);
  882. return;
  883. }
  884. if (HDCP_STATE_AUTHENTICATING != hdcp_ctrl->hdcp_state) {
  885. DEV_DBG("%s: %s: invalid state. returning\n", __func__,
  886. HDCP_STATE_NAME);
  887. return;
  888. }
  889. io = hdcp_ctrl->init_data.core_io;
  890. /* Enabling Software DDC */
  891. DSS_REG_W_ND(io, HDMI_DDC_ARBITRATION , DSS_REG_R(io,
  892. HDMI_DDC_ARBITRATION) & ~(BIT(4)));
  893. rc = hdmi_hdcp_authentication_part1(hdcp_ctrl);
  894. if (rc) {
  895. DEV_DBG("%s: %s: HDCP Auth Part I failed\n", __func__,
  896. HDCP_STATE_NAME);
  897. goto error;
  898. }
  899. if (hdcp_ctrl->current_tp.ds_type == DS_REPEATER) {
  900. rc = hdmi_hdcp_authentication_part2(hdcp_ctrl);
  901. if (rc) {
  902. DEV_DBG("%s: %s: HDCP Auth Part II failed\n", __func__,
  903. HDCP_STATE_NAME);
  904. goto error;
  905. }
  906. } else {
  907. DEV_INFO("%s: Downstream device is not a repeater\n", __func__);
  908. }
  909. /* Disabling software DDC before going into part3 to make sure
  910. * there is no Arbitration between software and hardware for DDC */
  911. DSS_REG_W_ND(io, HDMI_DDC_ARBITRATION , DSS_REG_R(io,
  912. HDMI_DDC_ARBITRATION) | (BIT(4)));
  913. error:
  914. /*
  915. * Ensure that the state did not change during authentication.
  916. * If it did, it means that deauthenticate/reauthenticate was
  917. * called. In that case, this function need not notify HDMI Tx
  918. * of the result
  919. */
  920. mutex_lock(hdcp_ctrl->init_data.mutex);
  921. if (HDCP_STATE_AUTHENTICATING == hdcp_ctrl->hdcp_state) {
  922. if (rc) {
  923. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTH_FAIL;
  924. } else {
  925. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTHENTICATED;
  926. hdcp_ctrl->auth_retries = 0;
  927. hdmi_hdcp_cache_topology(hdcp_ctrl);
  928. hdmi_hdcp_notify_topology(hdcp_ctrl);
  929. }
  930. mutex_unlock(hdcp_ctrl->init_data.mutex);
  931. /* Notify HDMI Tx controller of the result */
  932. DEV_DBG("%s: %s: Notifying HDMI Tx of auth result\n",
  933. __func__, HDCP_STATE_NAME);
  934. if (hdcp_ctrl->init_data.notify_status) {
  935. hdcp_ctrl->init_data.notify_status(
  936. hdcp_ctrl->init_data.cb_data,
  937. hdcp_ctrl->hdcp_state);
  938. }
  939. } else {
  940. DEV_DBG("%s: %s: HDCP state changed during authentication\n",
  941. __func__, HDCP_STATE_NAME);
  942. mutex_unlock(hdcp_ctrl->init_data.mutex);
  943. }
  944. return;
  945. } /* hdmi_hdcp_auth_work */
  946. #endif
  947. int hdmi_hdcp_authenticate(void *input)
  948. {
  949. struct hdmi_hdcp_ctrl *hdcp_ctrl = (struct hdmi_hdcp_ctrl *)input;
  950. if (!hdcp_ctrl) {
  951. DEV_ERR("%s: invalid input\n", __func__);
  952. return -EINVAL;
  953. }
  954. if (HDCP_STATE_INACTIVE != hdcp_ctrl->hdcp_state) {
  955. DEV_DBG("%s: %s: already active or activating. returning\n",
  956. __func__, HDCP_STATE_NAME);
  957. return 0;
  958. }
  959. DEV_DBG("%s: %s: Queuing work to start HDCP authentication", __func__,
  960. HDCP_STATE_NAME);
  961. mutex_lock(hdcp_ctrl->init_data.mutex);
  962. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTHENTICATING;
  963. mutex_unlock(hdcp_ctrl->init_data.mutex);
  964. queue_delayed_work(hdcp_ctrl->init_data.workq,
  965. &hdcp_ctrl->hdcp_auth_work, 0);
  966. return 0;
  967. } /* hdmi_hdcp_authenticate */
  968. /*
  969. * Only retries defined times then abort current authenticating process
  970. * Send check_topology message to notify any hdcpmanager's client of non-
  971. * hdcp authenticated data link so the client can tear down any active secure
  972. * playback.
  973. * Reduce hdcp link to regular hdmi data link with hdcp disabled so any
  974. * un-secure like UI & menu still can be sent over HDMI and display.
  975. */
  976. #define AUTH_RETRIES_TIME (30)
  977. static int hdmi_msm_if_abort_reauth(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  978. {
  979. int ret = 0;
  980. if (!hdcp_ctrl) {
  981. DEV_ERR("%s: invalid input\n", __func__);
  982. return -EINVAL;
  983. }
  984. if (++hdcp_ctrl->auth_retries == AUTH_RETRIES_TIME) {
  985. mutex_lock(hdcp_ctrl->init_data.mutex);
  986. hdcp_ctrl->hdcp_state = HDCP_STATE_INACTIVE;
  987. mutex_unlock(hdcp_ctrl->init_data.mutex);
  988. hdcp_ctrl->auth_retries = 0;
  989. ret = -ERANGE;
  990. }
  991. return ret;
  992. }
  993. int hdmi_hdcp_reauthenticate(void *input)
  994. {
  995. struct hdmi_hdcp_ctrl *hdcp_ctrl = (struct hdmi_hdcp_ctrl *)input;
  996. struct dss_io_data *io;
  997. u32 ret = 0;
  998. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io) {
  999. DEV_ERR("%s: invalid input\n", __func__);
  1000. return -EINVAL;
  1001. }
  1002. io = hdcp_ctrl->init_data.core_io;
  1003. if (HDCP_STATE_AUTH_FAIL != hdcp_ctrl->hdcp_state) {
  1004. DEV_DBG("%s: %s: invalid state. returning\n", __func__,
  1005. HDCP_STATE_NAME);
  1006. return 0;
  1007. }
  1008. /*
  1009. * Disable HPD circuitry.
  1010. * This is needed to reset the HDCP cipher engine so that when we
  1011. * attempt a re-authentication, HW would clear the AN0_READY and
  1012. * AN1_READY bits in HDMI_HDCP_LINK0_STATUS register
  1013. */
  1014. DSS_REG_W(io, HDMI_HPD_CTRL, DSS_REG_R(hdcp_ctrl->init_data.core_io,
  1015. HDMI_HPD_CTRL) & ~BIT(28));
  1016. /* Disable HDCP interrupts */
  1017. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, 0);
  1018. DSS_REG_W(io, HDMI_HDCP_RESET, BIT(0));
  1019. /* Wait to be clean on DDC HW engine */
  1020. hdmi_hdcp_hw_ddc_clean(hdcp_ctrl);
  1021. /* Disable encryption and disable the HDCP block */
  1022. DSS_REG_W(io, HDMI_HDCP_CTRL, 0);
  1023. /* Enable HPD circuitry */
  1024. DSS_REG_W(hdcp_ctrl->init_data.core_io, HDMI_HPD_CTRL,
  1025. DSS_REG_R(hdcp_ctrl->init_data.core_io,
  1026. HDMI_HPD_CTRL) | BIT(28));
  1027. ret = hdmi_msm_if_abort_reauth(hdcp_ctrl);
  1028. if (ret) {
  1029. DEV_ERR("%s: abort reauthentication!\n", __func__);
  1030. return ret;
  1031. }
  1032. /* Restart authentication attempt */
  1033. DEV_DBG("%s: %s: Scheduling work to start HDCP authentication",
  1034. __func__, HDCP_STATE_NAME);
  1035. mutex_lock(hdcp_ctrl->init_data.mutex);
  1036. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTHENTICATING;
  1037. mutex_unlock(hdcp_ctrl->init_data.mutex);
  1038. queue_delayed_work(hdcp_ctrl->init_data.workq,
  1039. &hdcp_ctrl->hdcp_auth_work, HZ/2);
  1040. return ret;
  1041. } /* hdmi_hdcp_reauthenticate */
  1042. void hdmi_hdcp_off(void *input)
  1043. {
  1044. struct hdmi_hdcp_ctrl *hdcp_ctrl = (struct hdmi_hdcp_ctrl *)input;
  1045. struct dss_io_data *io;
  1046. int rc = 0;
  1047. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io) {
  1048. DEV_ERR("%s: invalid input\n", __func__);
  1049. return;
  1050. }
  1051. io = hdcp_ctrl->init_data.core_io;
  1052. if (HDCP_STATE_INACTIVE == hdcp_ctrl->hdcp_state) {
  1053. DEV_DBG("%s: %s: inactive. returning\n", __func__,
  1054. HDCP_STATE_NAME);
  1055. return;
  1056. }
  1057. /*
  1058. * Disable HDCP interrupts.
  1059. * Also, need to set the state to inactive here so that any ongoing
  1060. * reauth works will know that the HDCP session has been turned off.
  1061. */
  1062. mutex_lock(hdcp_ctrl->init_data.mutex);
  1063. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, 0);
  1064. hdcp_ctrl->hdcp_state = HDCP_STATE_INACTIVE;
  1065. mutex_unlock(hdcp_ctrl->init_data.mutex);
  1066. /*
  1067. * Cancel any pending auth/reauth attempts.
  1068. * If one is ongoing, this will wait for it to finish.
  1069. * No more reauthentiaction attempts will be scheduled since we
  1070. * set the currect state to inactive.
  1071. */
  1072. rc = cancel_delayed_work_sync(&hdcp_ctrl->hdcp_auth_work);
  1073. if (rc)
  1074. DEV_DBG("%s: %s: Deleted hdcp auth work\n", __func__,
  1075. HDCP_STATE_NAME);
  1076. rc = cancel_work_sync(&hdcp_ctrl->hdcp_int_work);
  1077. if (rc)
  1078. DEV_DBG("%s: %s: Deleted hdcp int work\n", __func__,
  1079. HDCP_STATE_NAME);
  1080. DSS_REG_W(io, HDMI_HDCP_RESET, BIT(0));
  1081. /* Disable encryption and disable the HDCP block */
  1082. DSS_REG_W(io, HDMI_HDCP_CTRL, 0);
  1083. DEV_DBG("%s: %s: HDCP: Off\n", __func__, HDCP_STATE_NAME);
  1084. } /* hdmi_hdcp_off */
  1085. int hdmi_hdcp_isr(void *input)
  1086. {
  1087. struct hdmi_hdcp_ctrl *hdcp_ctrl = (struct hdmi_hdcp_ctrl *)input;
  1088. int rc = 0;
  1089. struct dss_io_data *io;
  1090. u32 hdcp_int_val;
  1091. if (!hdcp_ctrl || !hdcp_ctrl->init_data.core_io) {
  1092. DEV_ERR("%s: invalid input\n", __func__);
  1093. rc = -EINVAL;
  1094. goto error;
  1095. }
  1096. io = hdcp_ctrl->init_data.core_io;
  1097. hdcp_int_val = DSS_REG_R(io, HDMI_HDCP_INT_CTRL);
  1098. /* Ignore HDCP interrupts if HDCP is disabled */
  1099. if (HDCP_STATE_INACTIVE == hdcp_ctrl->hdcp_state) {
  1100. DEV_ERR("%s: HDCP inactive. Just clear int and return.\n",
  1101. __func__);
  1102. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, HDCP_INT_CLR);
  1103. return 0;
  1104. }
  1105. if (hdcp_int_val & BIT(0)) {
  1106. /* AUTH_SUCCESS_INT */
  1107. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, (hdcp_int_val | BIT(1)));
  1108. DEV_INFO("%s: %s: AUTH_SUCCESS_INT received\n", __func__,
  1109. HDCP_STATE_NAME);
  1110. if (HDCP_STATE_AUTHENTICATING == hdcp_ctrl->hdcp_state)
  1111. complete_all(&hdcp_ctrl->r0_checked);
  1112. }
  1113. if (hdcp_int_val & BIT(4)) {
  1114. /* AUTH_FAIL_INT */
  1115. u32 link_status = DSS_REG_R(io, HDMI_HDCP_LINK0_STATUS);
  1116. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, (hdcp_int_val | BIT(5)));
  1117. DEV_INFO("%s: %s: AUTH_FAIL_INT rcvd, LINK0_STATUS=0x%08x\n",
  1118. __func__, HDCP_STATE_NAME, link_status);
  1119. if (HDCP_STATE_AUTHENTICATED == hdcp_ctrl->hdcp_state) {
  1120. /* Inform HDMI Tx of the failure */
  1121. queue_work(hdcp_ctrl->init_data.workq,
  1122. &hdcp_ctrl->hdcp_int_work);
  1123. /* todo: print debug log with auth fail reason */
  1124. } else if (HDCP_STATE_AUTHENTICATING == hdcp_ctrl->hdcp_state) {
  1125. complete_all(&hdcp_ctrl->r0_checked);
  1126. }
  1127. /* Clear AUTH_FAIL_INFO as well */
  1128. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, (hdcp_int_val | BIT(7)));
  1129. }
  1130. if (hdcp_int_val & BIT(8)) {
  1131. /* DDC_XFER_REQ_INT */
  1132. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, (hdcp_int_val | BIT(9)));
  1133. DEV_INFO("%s: %s: DDC_XFER_REQ_INT received\n", __func__,
  1134. HDCP_STATE_NAME);
  1135. }
  1136. if (hdcp_int_val & BIT(12)) {
  1137. /* DDC_XFER_DONE_INT */
  1138. DSS_REG_W(io, HDMI_HDCP_INT_CTRL, (hdcp_int_val | BIT(13)));
  1139. DEV_INFO("%s: %s: DDC_XFER_DONE received\n", __func__,
  1140. HDCP_STATE_NAME);
  1141. }
  1142. error:
  1143. return rc;
  1144. } /* hdmi_hdcp_isr */
  1145. static ssize_t hdmi_hdcp_sysfs_rda_status(struct device *dev,
  1146. struct device_attribute *attr, char *buf)
  1147. {
  1148. ssize_t ret;
  1149. struct hdmi_hdcp_ctrl *hdcp_ctrl =
  1150. hdmi_get_featuredata_from_sysfs_dev(dev, HDMI_TX_FEAT_HDCP);
  1151. if (!hdcp_ctrl) {
  1152. DEV_ERR("%s: invalid input\n", __func__);
  1153. return -EINVAL;
  1154. }
  1155. mutex_lock(hdcp_ctrl->init_data.mutex);
  1156. ret = snprintf(buf, PAGE_SIZE, "%d\n", hdcp_ctrl->hdcp_state);
  1157. DEV_DBG("%s: '%d'\n", __func__, hdcp_ctrl->hdcp_state);
  1158. mutex_unlock(hdcp_ctrl->init_data.mutex);
  1159. return ret;
  1160. } /* hdmi_hdcp_sysfs_rda_hdcp*/
  1161. static ssize_t hdmi_hdcp_sysfs_rda_tp(struct device *dev,
  1162. struct device_attribute *attr, char *buf)
  1163. {
  1164. ssize_t ret = 0;
  1165. struct hdmi_hdcp_ctrl *hdcp_ctrl =
  1166. hdmi_get_featuredata_from_sysfs_dev(dev, HDMI_TX_FEAT_HDCP);
  1167. if (!hdcp_ctrl) {
  1168. DEV_ERR("%s: invalid input\n", __func__);
  1169. return -EINVAL;
  1170. }
  1171. switch (hdcp_ctrl->tp_msgid) {
  1172. case DOWN_CHECK_TOPOLOGY:
  1173. case DOWN_REQUEST_TOPOLOGY:
  1174. buf[MSG_ID_IDX] = hdcp_ctrl->tp_msgid;
  1175. buf[RET_CODE_IDX] = HDCP_AUTHED;
  1176. ret = HEADER_LEN;
  1177. memcpy(buf + HEADER_LEN, &hdcp_ctrl->cached_tp,
  1178. sizeof(struct HDCP_V2V1_MSG_TOPOLOGY));
  1179. ret += sizeof(struct HDCP_V2V1_MSG_TOPOLOGY);
  1180. /* clear the flag once data is read back to user space*/
  1181. hdcp_ctrl->tp_msgid = -1;
  1182. break;
  1183. default:
  1184. ret = -EINVAL;
  1185. }
  1186. return ret;
  1187. } /* hdmi_hdcp_sysfs_rda_tp*/
  1188. static ssize_t hdmi_hdcp_sysfs_wta_tp(struct device *dev,
  1189. struct device_attribute *attr, const char *buf, size_t count)
  1190. {
  1191. int msgid = 0;
  1192. ssize_t ret = count;
  1193. struct hdmi_hdcp_ctrl *hdcp_ctrl =
  1194. hdmi_get_featuredata_from_sysfs_dev(dev, HDMI_TX_FEAT_HDCP);
  1195. if (!hdcp_ctrl || !buf) {
  1196. DEV_ERR("%s: invalid input\n", __func__);
  1197. return -EINVAL;
  1198. }
  1199. msgid = buf[0];
  1200. switch (msgid) {
  1201. case DOWN_CHECK_TOPOLOGY:
  1202. case DOWN_REQUEST_TOPOLOGY:
  1203. hdcp_ctrl->tp_msgid = msgid;
  1204. break;
  1205. /* more cases added here */
  1206. default:
  1207. ret = -EINVAL;
  1208. }
  1209. return ret;
  1210. } /* hdmi_tx_sysfs_wta_hpd */
  1211. static DEVICE_ATTR(status, S_IRUGO, hdmi_hdcp_sysfs_rda_status, NULL);
  1212. static DEVICE_ATTR(tp, S_IRUGO | S_IWUSR, hdmi_hdcp_sysfs_rda_tp,
  1213. hdmi_hdcp_sysfs_wta_tp);
  1214. static struct attribute *hdmi_hdcp_fs_attrs[] = {
  1215. &dev_attr_status.attr,
  1216. &dev_attr_tp.attr,
  1217. NULL,
  1218. };
  1219. static struct attribute_group hdmi_hdcp_fs_attr_group = {
  1220. .name = "hdcp",
  1221. .attrs = hdmi_hdcp_fs_attrs,
  1222. };
  1223. void hdmi_hdcp_deinit(void *input)
  1224. {
  1225. struct hdmi_hdcp_ctrl *hdcp_ctrl = (struct hdmi_hdcp_ctrl *)input;
  1226. if (!hdcp_ctrl) {
  1227. DEV_ERR("%s: invalid input\n", __func__);
  1228. return;
  1229. }
  1230. sysfs_remove_group(hdcp_ctrl->init_data.sysfs_kobj,
  1231. &hdmi_hdcp_fs_attr_group);
  1232. kfree(hdcp_ctrl);
  1233. } /* hdmi_hdcp_deinit */
  1234. void *hdmi_hdcp_init(struct hdmi_hdcp_init_data *init_data)
  1235. {
  1236. struct hdmi_hdcp_ctrl *hdcp_ctrl = NULL;
  1237. if (!init_data || !init_data->core_io || !init_data->qfprom_io ||
  1238. !init_data->mutex || !init_data->ddc_ctrl ||
  1239. !init_data->notify_status || !init_data->workq ||
  1240. !init_data->cb_data) {
  1241. DEV_ERR("%s: invalid input\n", __func__);
  1242. goto error;
  1243. }
  1244. hdcp_ctrl = kzalloc(sizeof(*hdcp_ctrl), GFP_KERNEL);
  1245. if (!hdcp_ctrl) {
  1246. DEV_ERR("%s: Out of memory\n", __func__);
  1247. goto error;
  1248. }
  1249. hdcp_ctrl->init_data = *init_data;
  1250. if (sysfs_create_group(init_data->sysfs_kobj,
  1251. &hdmi_hdcp_fs_attr_group)) {
  1252. DEV_ERR("%s: hdcp sysfs group creation failed\n", __func__);
  1253. goto error;
  1254. }
  1255. #ifdef CONFIG_VIDEO_MHL_V2
  1256. INIT_DELAYED_WORK(&hdcp_ctrl->hdcp_auth_work, hdmi_hdcp_auth_part1_work);
  1257. #else
  1258. INIT_DELAYED_WORK(&hdcp_ctrl->hdcp_auth_work, hdmi_hdcp_auth_work);
  1259. #endif
  1260. INIT_WORK(&hdcp_ctrl->hdcp_int_work, hdmi_hdcp_int_work);
  1261. hdcp_ctrl->hdcp_state = HDCP_STATE_INACTIVE;
  1262. init_completion(&hdcp_ctrl->r0_checked);
  1263. DEV_DBG("%s: HDCP module initialized. HDCP_STATE=%s", __func__,
  1264. HDCP_STATE_NAME);
  1265. #ifdef CONFIG_VIDEO_MHL_V2
  1266. hdcp_ctrl_global = hdcp_ctrl;
  1267. #endif
  1268. error:
  1269. return (void *)hdcp_ctrl;
  1270. } /* hdmi_hdcp_init */