sde_hdcp_2x.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  4. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  5. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  6. */
  7. #define pr_fmt(fmt) "[sde-hdcp-2x] %s: " fmt, __func__
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include <linux/cdev.h>
  13. #include <linux/sched.h>
  14. #include <linux/list.h>
  15. #include <linux/mutex.h>
  16. #include <linux/types.h>
  17. #include <linux/device.h>
  18. #include <linux/errno.h>
  19. #include <linux/kthread.h>
  20. #include <linux/kfifo.h>
  21. #include "sde_hdcp_2x.h"
  22. #if IS_ENABLED(CONFIG_SECDP_DBG)
  23. #include <linux/secdp_logger_ex.h>
  24. #endif
  25. /* all message IDs */
  26. #define INVALID_MESSAGE 0
  27. #define AKE_INIT 2
  28. #define AKE_SEND_CERT 3
  29. #define AKE_NO_STORED_KM 4
  30. #define AKE_STORED_KM 5
  31. #define AKE_SEND_H_PRIME 7
  32. #define AKE_SEND_PAIRING_INFO 8
  33. #define LC_INIT 9
  34. #define LC_SEND_L_PRIME 10
  35. #define SKE_SEND_EKS 11
  36. #define REP_SEND_RECV_ID_LIST 12
  37. #define REP_SEND_ACK 15
  38. #define REP_STREAM_MANAGE 16
  39. #define REP_STREAM_READY 17
  40. #define SKE_SEND_TYPE_ID 18
  41. #define HDCP2P2_MAX_MESSAGES 19
  42. #define REAUTH_REQ BIT(3)
  43. #define LINK_INTEGRITY_FAILURE BIT(4)
  44. /* Temporary define to override wrong TZ value */
  45. #define AKE_SEND_CERT_MSG_DELAY 100
  46. struct sde_hdcp_2x_ctrl {
  47. DECLARE_KFIFO(cmd_q, enum sde_hdcp_2x_wakeup_cmd, 8);
  48. wait_queue_head_t wait_q;
  49. struct hdcp2_app_data app_data;
  50. u32 timeout_left;
  51. u32 wait_timeout_ms;
  52. u32 total_message_length;
  53. atomic_t enable_pending;
  54. bool no_stored_km;
  55. bool feature_supported;
  56. bool force_encryption;
  57. bool authenticated;
  58. bool resend_lc_init;
  59. bool resend_stream_manage;
  60. void *client_data;
  61. void *hdcp2_ctx;
  62. struct hdcp_transport_ops *client_ops;
  63. bool repeater_flag;
  64. bool update_stream;
  65. int last_msg;
  66. atomic_t hdcp_off;
  67. enum sde_hdcp_2x_device_type device_type;
  68. u8 min_enc_level;
  69. struct list_head stream_handles;
  70. u8 stream_count;
  71. struct task_struct *thread;
  72. struct completion response_completion;
  73. };
  74. static void sde_hdcp_2x_clean(struct sde_hdcp_2x_ctrl *hdcp);
  75. static const char *sde_hdcp_2x_message_name(int msg_id)
  76. {
  77. switch (msg_id) {
  78. case INVALID_MESSAGE: return TO_STR(INVALID_MESSAGE);
  79. case AKE_INIT: return TO_STR(AKE_INIT);
  80. case AKE_SEND_CERT: return TO_STR(AKE_SEND_CERT);
  81. case AKE_NO_STORED_KM: return TO_STR(AKE_NO_STORED_KM);
  82. case AKE_STORED_KM: return TO_STR(AKE_STORED_KM);
  83. case AKE_SEND_H_PRIME: return TO_STR(AKE_SEND_H_PRIME);
  84. case AKE_SEND_PAIRING_INFO: return TO_STR(AKE_SEND_PAIRING_INFO);
  85. case LC_INIT: return TO_STR(LC_INIT);
  86. case LC_SEND_L_PRIME: return TO_STR(LC_SEND_L_PRIME);
  87. case SKE_SEND_EKS: return TO_STR(SKE_SEND_EKS);
  88. case REP_SEND_RECV_ID_LIST: return TO_STR(REP_SEND_RECV_ID_LIST);
  89. case REP_STREAM_MANAGE: return TO_STR(REP_STREAM_MANAGE);
  90. case REP_STREAM_READY: return TO_STR(REP_STREAM_READY);
  91. case SKE_SEND_TYPE_ID: return TO_STR(SKE_SEND_TYPE_ID);
  92. default:
  93. return "UNKNOWN";
  94. }
  95. }
  96. static const struct sde_hdcp_2x_msg_data
  97. hdcp_msg_lookup[HDCP2P2_MAX_MESSAGES] = {
  98. [AKE_INIT] = { 2,
  99. { {"rtx", 0x69000, 8}, {"TxCaps", 0x69008, 3} },
  100. 0, 0 },
  101. [AKE_SEND_CERT] = { 3,
  102. { {"cert-rx", 0x6900B, 522}, {"rrx", 0x69215, 8},
  103. {"RxCaps", 0x6921D, 3} },
  104. 0, 110 },
  105. [AKE_NO_STORED_KM] = { 1,
  106. { {"Ekpub_km", 0x69220, 128} },
  107. 0, 0 },
  108. [AKE_STORED_KM] = { 2,
  109. { {"Ekh_km", 0x692A0, 16}, {"m", 0x692B0, 16} },
  110. 0, 0 },
  111. [AKE_SEND_H_PRIME] = { 1,
  112. { {"H'", 0x692C0, 32} },
  113. (1 << 1), 7 },
  114. [AKE_SEND_PAIRING_INFO] = { 1,
  115. { {"Ekh_km", 0x692E0, 16} },
  116. (1 << 2), 5 },
  117. [LC_INIT] = { 1,
  118. { {"rn", 0x692F0, 8} },
  119. 0, 0 },
  120. [LC_SEND_L_PRIME] = { 1,
  121. { {"L'", 0x692F8, 32} },
  122. 0, 0 },
  123. [SKE_SEND_EKS] = { 2,
  124. { {"Edkey_ks", 0x69318, 16}, {"riv", 0x69328, 8} },
  125. 0, 0 },
  126. [SKE_SEND_TYPE_ID] = { 1,
  127. { {"type", 0x69494, 1} },
  128. 0, 0 },
  129. [REP_SEND_RECV_ID_LIST] = { 4,
  130. { {"RxInfo", 0x69330, 2}, {"seq_num_V", 0x69332, 3},
  131. {"V'", 0x69335, 16}, {"ridlist", 0x69345, 155} },
  132. (1 << 0), 0 },
  133. [REP_SEND_ACK] = { 1,
  134. { {"V", 0x693E0, 16} },
  135. 0, 0 },
  136. [REP_STREAM_MANAGE] = { 3,
  137. { {"seq_num_M", 0x693F0, 3}, {"k", 0x693F3, 2},
  138. {"streamID_Type", 0x693F5, 126} },
  139. 0, 0 },
  140. [REP_STREAM_READY] = { 1,
  141. { {"M'", 0x69473, 32} },
  142. 0, 7 },
  143. };
  144. static int sde_hdcp_2x_get_next_message(struct sde_hdcp_2x_ctrl *hdcp,
  145. struct hdcp_transport_wakeup_data *data)
  146. {
  147. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY, hdcp->last_msg);
  148. switch (hdcp->last_msg) {
  149. case INVALID_MESSAGE:
  150. return AKE_INIT;
  151. case AKE_INIT:
  152. return AKE_SEND_CERT;
  153. case AKE_SEND_CERT:
  154. if (hdcp->no_stored_km)
  155. return AKE_NO_STORED_KM;
  156. else
  157. return AKE_STORED_KM;
  158. case AKE_STORED_KM:
  159. fallthrough;
  160. case AKE_NO_STORED_KM:
  161. return AKE_SEND_H_PRIME;
  162. case AKE_SEND_H_PRIME:
  163. if (hdcp->no_stored_km)
  164. return AKE_SEND_PAIRING_INFO;
  165. else
  166. return LC_INIT;
  167. case AKE_SEND_PAIRING_INFO:
  168. return LC_INIT;
  169. case LC_INIT:
  170. return LC_SEND_L_PRIME;
  171. case LC_SEND_L_PRIME:
  172. if (hdcp->resend_lc_init)
  173. return LC_INIT;
  174. else
  175. return SKE_SEND_EKS;
  176. case SKE_SEND_EKS:
  177. if (!hdcp->repeater_flag)
  178. return SKE_SEND_TYPE_ID;
  179. fallthrough;
  180. case SKE_SEND_TYPE_ID:
  181. if (!hdcp->repeater_flag)
  182. return SKE_SEND_TYPE_ID;
  183. fallthrough;
  184. case REP_STREAM_READY:
  185. fallthrough;
  186. case REP_SEND_ACK:
  187. if (!hdcp->repeater_flag)
  188. return INVALID_MESSAGE;
  189. if (data->cmd == HDCP_TRANSPORT_CMD_SEND_MESSAGE)
  190. return REP_STREAM_MANAGE;
  191. else
  192. return REP_SEND_RECV_ID_LIST;
  193. case REP_SEND_RECV_ID_LIST:
  194. return REP_SEND_ACK;
  195. case REP_STREAM_MANAGE:
  196. hdcp->resend_stream_manage = false;
  197. return REP_STREAM_READY;
  198. default:
  199. pr_err("Unknown message ID (%d)\n", hdcp->last_msg);
  200. return -EINVAL;
  201. }
  202. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT);
  203. return -EINVAL;
  204. }
  205. static void sde_hdcp_2x_wait_for_response(struct sde_hdcp_2x_ctrl *hdcp)
  206. {
  207. u32 timeout;
  208. switch (hdcp->last_msg) {
  209. case AKE_SEND_H_PRIME:
  210. if (hdcp->no_stored_km)
  211. hdcp->wait_timeout_ms = HZ;
  212. else
  213. hdcp->wait_timeout_ms = HZ / 4;
  214. break;
  215. case AKE_SEND_PAIRING_INFO:
  216. hdcp->wait_timeout_ms = HZ / 4;
  217. break;
  218. case REP_SEND_RECV_ID_LIST:
  219. if (!hdcp->authenticated)
  220. hdcp->wait_timeout_ms = HZ * 3;
  221. else
  222. hdcp->wait_timeout_ms = 0;
  223. break;
  224. default:
  225. hdcp->wait_timeout_ms = 0;
  226. }
  227. if (!hdcp->wait_timeout_ms)
  228. return;
  229. if (atomic_read(&hdcp->hdcp_off)) {
  230. pr_debug("invalid state: hdcp off\n");
  231. return;
  232. }
  233. reinit_completion(&hdcp->response_completion);
  234. timeout = wait_for_completion_timeout(&hdcp->response_completion,
  235. hdcp->wait_timeout_ms);
  236. if (!timeout) {
  237. pr_err("completion expired, last message = %s\n",
  238. sde_hdcp_2x_message_name(hdcp->last_msg));
  239. if (!atomic_read(&hdcp->hdcp_off))
  240. sde_hdcp_2x_clean(hdcp);
  241. }
  242. hdcp->wait_timeout_ms = 0;
  243. }
  244. static void sde_hdcp_2x_adjust_transaction_params(
  245. struct sde_hdcp_2x_ctrl *hdcp,
  246. struct hdcp_transport_wakeup_data *data)
  247. {
  248. switch (hdcp->last_msg) {
  249. case AKE_SEND_CERT:
  250. data->transaction_delay = AKE_SEND_CERT_MSG_DELAY;
  251. break;
  252. case REP_STREAM_READY:
  253. break;
  254. default:
  255. data->transaction_delay = 0;
  256. break;
  257. }
  258. data->transaction_timeout =
  259. hdcp_msg_lookup[hdcp->last_msg].transaction_timeout;
  260. pr_debug("%s: transaction delay: %ums, transaction timeout: %ums\n",
  261. sde_hdcp_2x_message_name(hdcp->last_msg),
  262. data->transaction_delay, data->transaction_timeout);
  263. }
  264. static void sde_hdcp_2x_wakeup_client(struct sde_hdcp_2x_ctrl *hdcp,
  265. struct hdcp_transport_wakeup_data *data)
  266. {
  267. int rc = 0;
  268. if (!hdcp || !hdcp->client_ops || !hdcp->client_ops->wakeup ||
  269. !data || (data->cmd == HDCP_TRANSPORT_CMD_INVALID))
  270. return;
  271. data->abort_mask = REAUTH_REQ | LINK_INTEGRITY_FAILURE;
  272. if (data->cmd == HDCP_TRANSPORT_CMD_SEND_MESSAGE ||
  273. data->cmd == HDCP_TRANSPORT_CMD_RECV_MESSAGE ||
  274. data->cmd == HDCP_TRANSPORT_CMD_LINK_POLL) {
  275. hdcp->last_msg =
  276. sde_hdcp_2x_get_next_message(hdcp, data);
  277. if (hdcp->last_msg <= INVALID_MESSAGE) {
  278. hdcp->last_msg = INVALID_MESSAGE;
  279. return;
  280. }
  281. data->message_data = &hdcp_msg_lookup[hdcp->last_msg];
  282. }
  283. sde_hdcp_2x_adjust_transaction_params(hdcp, data);
  284. rc = hdcp->client_ops->wakeup(data);
  285. if (rc)
  286. pr_err("error sending %s to client\n",
  287. hdcp_transport_cmd_to_str(data->cmd));
  288. sde_hdcp_2x_wait_for_response(hdcp);
  289. }
  290. static inline void sde_hdcp_2x_send_message(struct sde_hdcp_2x_ctrl *hdcp)
  291. {
  292. struct hdcp_transport_wakeup_data cdata = {
  293. HDCP_TRANSPORT_CMD_SEND_MESSAGE };
  294. cdata.context = hdcp->client_data;
  295. cdata.transaction_delay = hdcp->app_data.timeout;
  296. cdata.buf_len = hdcp->app_data.response.length;
  297. /* ignore the first byte as it contains the message id */
  298. cdata.buf = hdcp->app_data.response.data + 1;
  299. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  300. }
  301. static bool sde_hdcp_2x_client_feature_supported(void *data)
  302. {
  303. struct sde_hdcp_2x_ctrl *hdcp = data;
  304. while (atomic_read(&hdcp->enable_pending))
  305. usleep_range(1000, 1500);
  306. return hdcp2_feature_supported(hdcp->hdcp2_ctx);
  307. }
  308. static void sde_hdcp_2x_force_encryption(void *data, bool enable)
  309. {
  310. struct sde_hdcp_2x_ctrl *hdcp = data;
  311. if (!hdcp) {
  312. pr_err("invalid input\n");
  313. return;
  314. }
  315. hdcp->force_encryption = enable;
  316. pr_info("force_encryption=%d\n", hdcp->force_encryption);
  317. }
  318. static void sde_hdcp_2x_clean(struct sde_hdcp_2x_ctrl *hdcp)
  319. {
  320. struct list_head *element;
  321. struct sde_hdcp_stream *stream_entry;
  322. struct hdcp_transport_wakeup_data cdata = {HDCP_TRANSPORT_CMD_INVALID};
  323. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY, hdcp->authenticated);
  324. hdcp->authenticated = false;
  325. cdata.context = hdcp->client_data;
  326. cdata.cmd = HDCP_TRANSPORT_CMD_STATUS_FAILED;
  327. while (!list_empty(&hdcp->stream_handles)) {
  328. element = hdcp->stream_handles.next;
  329. list_del(element);
  330. stream_entry = list_entry(element, struct sde_hdcp_stream,
  331. list);
  332. hdcp2_close_stream(hdcp->hdcp2_ctx,
  333. stream_entry->stream_handle);
  334. kfree(stream_entry);
  335. hdcp->stream_count--;
  336. }
  337. if (!atomic_xchg(&hdcp->hdcp_off, 1))
  338. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  339. hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_STOP, &hdcp->app_data);
  340. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, hdcp->authenticated);
  341. }
  342. static u8 sde_hdcp_2x_stream_type(u8 min_enc_level)
  343. {
  344. u8 stream_type = 0;
  345. switch (min_enc_level) {
  346. case 0:
  347. case 1:
  348. stream_type = 0;
  349. break;
  350. case 2:
  351. stream_type = 1;
  352. break;
  353. default:
  354. stream_type = 0;
  355. break;
  356. }
  357. pr_debug("min_enc_level = %u, type = %u\n", min_enc_level, stream_type);
  358. return stream_type;
  359. }
  360. static void sde_hdcp_2x_send_type(struct sde_hdcp_2x_ctrl *hdcp)
  361. {
  362. if (atomic_read(&hdcp->hdcp_off)) {
  363. pr_debug("invalid state, hdcp off\n");
  364. return;
  365. }
  366. if (hdcp->repeater_flag) {
  367. pr_debug("invalid state, not receiver\n");
  368. return;
  369. }
  370. hdcp->app_data.response.data[0] = SKE_SEND_TYPE_ID;
  371. hdcp->app_data.response.data[1] =
  372. sde_hdcp_2x_stream_type(hdcp->min_enc_level);
  373. hdcp->app_data.response.length = 1;
  374. hdcp->app_data.timeout = 100;
  375. if (!atomic_read(&hdcp->hdcp_off))
  376. sde_hdcp_2x_send_message(hdcp);
  377. }
  378. static void sde_hdcp_2x_query_stream(struct sde_hdcp_2x_ctrl *hdcp)
  379. {
  380. int rc = 0;
  381. if (atomic_read(&hdcp->hdcp_off)) {
  382. pr_debug("invalid state, hdcp off\n");
  383. return;
  384. }
  385. if (!hdcp->repeater_flag) {
  386. pr_debug("invalid state, not a repeater\n");
  387. return;
  388. }
  389. if (!hdcp->authenticated &&
  390. hdcp->app_data.response.data[0] != REP_SEND_ACK) {
  391. pr_debug("invalid state. HDCP repeater not authenticated\n");
  392. return;
  393. }
  394. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_QUERY_STREAM,
  395. &hdcp->app_data);
  396. if (rc)
  397. goto exit;
  398. if (!hdcp->app_data.response.data || !hdcp->app_data.request.data) {
  399. pr_err("invalid response/request buffers\n");
  400. rc = -EINVAL;
  401. goto exit;
  402. }
  403. pr_debug("[tz]: %s\n", sde_hdcp_2x_message_name(
  404. hdcp->app_data.response.data[0]));
  405. exit:
  406. if (!rc && !atomic_read(&hdcp->hdcp_off)) {
  407. /* Modify last message to ensure the proper message is sent */
  408. hdcp->last_msg = REP_SEND_ACK;
  409. sde_hdcp_2x_send_message(hdcp);
  410. }
  411. }
  412. static void sde_hdcp_2x_initialize_command(struct sde_hdcp_2x_ctrl *hdcp,
  413. enum hdcp_transport_wakeup_cmd cmd,
  414. struct hdcp_transport_wakeup_data *cdata)
  415. {
  416. cdata->cmd = cmd;
  417. cdata->transaction_delay = hdcp->timeout_left;
  418. cdata->buf = hdcp->app_data.request.data + 1;
  419. }
  420. static void sde_hdcp_2x_msg_sent(struct sde_hdcp_2x_ctrl *hdcp)
  421. {
  422. struct hdcp_transport_wakeup_data cdata = {
  423. HDCP_TRANSPORT_CMD_INVALID,
  424. hdcp->client_data};
  425. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY, hdcp->authenticated);
  426. if (atomic_read(&hdcp->hdcp_off)) {
  427. pr_debug("invalid state, hdcp off\n");
  428. goto exit;
  429. }
  430. SDE_EVT32_EXTERNAL(hdcp->app_data.response.data[0]);
  431. switch (hdcp->app_data.response.data[0]) {
  432. case SKE_SEND_TYPE_ID:
  433. if (!hdcp2_app_comm(hdcp->hdcp2_ctx,
  434. HDCP2_CMD_EN_ENCRYPTION, &hdcp->app_data)) {
  435. hdcp->authenticated = true;
  436. if (hdcp->force_encryption)
  437. hdcp2_force_encryption(hdcp->hdcp2_ctx, 1);
  438. cdata.cmd = HDCP_TRANSPORT_CMD_STATUS_SUCCESS;
  439. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  440. }
  441. /* poll for link check */
  442. sde_hdcp_2x_initialize_command(hdcp,
  443. HDCP_TRANSPORT_CMD_LINK_POLL, &cdata);
  444. break;
  445. case SKE_SEND_EKS:
  446. if (hdcp->repeater_flag && !atomic_read(&hdcp->hdcp_off)) {
  447. /* poll for link check */
  448. sde_hdcp_2x_initialize_command(hdcp,
  449. HDCP_TRANSPORT_CMD_LINK_POLL, &cdata);
  450. } else {
  451. hdcp->app_data.response.data[0] = SKE_SEND_TYPE_ID;
  452. hdcp->app_data.response.data[1] =
  453. sde_hdcp_2x_stream_type(hdcp->min_enc_level);
  454. hdcp->app_data.response.length = 1;
  455. hdcp->app_data.timeout = 100;
  456. sde_hdcp_2x_send_message(hdcp);
  457. }
  458. break;
  459. case REP_SEND_ACK:
  460. pr_debug("Repeater authentication successful. update_stream=%d\n",
  461. hdcp->update_stream);
  462. if (hdcp->update_stream) {
  463. sde_hdcp_2x_query_stream(hdcp);
  464. hdcp->update_stream = false;
  465. } else {
  466. sde_hdcp_2x_initialize_command(hdcp,
  467. HDCP_TRANSPORT_CMD_LINK_POLL, &cdata);
  468. }
  469. break;
  470. default:
  471. cdata.cmd = HDCP_TRANSPORT_CMD_RECV_MESSAGE;
  472. cdata.transaction_delay = hdcp->app_data.timeout;
  473. cdata.buf = hdcp->app_data.request.data + 1;
  474. }
  475. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  476. exit:
  477. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, hdcp->authenticated);
  478. }
  479. static void sde_hdcp_2x_init(struct sde_hdcp_2x_ctrl *hdcp)
  480. {
  481. int rc;
  482. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_START, &hdcp->app_data);
  483. if (rc)
  484. sde_hdcp_2x_clean(hdcp);
  485. }
  486. static void sde_hdcp_2x_start_auth(struct sde_hdcp_2x_ctrl *hdcp)
  487. {
  488. int rc;
  489. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY, hdcp->authenticated);
  490. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_START_AUTH,
  491. &hdcp->app_data);
  492. if (rc) {
  493. sde_hdcp_2x_clean(hdcp);
  494. return;
  495. }
  496. pr_debug("message received from TZ: %s\n",
  497. sde_hdcp_2x_message_name(hdcp->app_data.response.data[0]));
  498. sde_hdcp_2x_send_message(hdcp);
  499. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, hdcp->authenticated);
  500. }
  501. static void sde_hdcp_2x_timeout(struct sde_hdcp_2x_ctrl *hdcp)
  502. {
  503. int rc = 0;
  504. int message_id;
  505. if (atomic_read(&hdcp->hdcp_off)) {
  506. pr_debug("invalid state, hdcp off\n");
  507. return;
  508. }
  509. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_TIMEOUT,
  510. &hdcp->app_data);
  511. if (rc)
  512. goto error;
  513. message_id = (int)hdcp->app_data.response.data[0];
  514. if (message_id == LC_INIT && !atomic_read(&hdcp->hdcp_off))
  515. sde_hdcp_2x_send_message(hdcp);
  516. return;
  517. error:
  518. if (!atomic_read(&hdcp->hdcp_off))
  519. sde_hdcp_2x_clean(hdcp);
  520. }
  521. static void sde_hdcp_2x_msg_recvd(struct sde_hdcp_2x_ctrl *hdcp)
  522. {
  523. int rc = 0;
  524. char *msg = NULL;
  525. u32 message_id_bytes = 0;
  526. u32 request_length, out_msg;
  527. struct hdcp_transport_wakeup_data cdata = {HDCP_TRANSPORT_CMD_INVALID};
  528. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY, hdcp->authenticated);
  529. if (atomic_read(&hdcp->hdcp_off)) {
  530. pr_debug("invalid state, hdcp off\n");
  531. return;
  532. }
  533. cdata.context = hdcp->client_data;
  534. request_length = hdcp->total_message_length;
  535. msg = hdcp->app_data.request.data;
  536. if (request_length == 0) {
  537. pr_err("invalid message length\n");
  538. goto exit;
  539. }
  540. if (hdcp->device_type == HDCP_TXMTR_DP ||
  541. hdcp->device_type == HDCP_TXMTR_DP_MST) {
  542. msg[0] = hdcp->last_msg;
  543. message_id_bytes = 1;
  544. }
  545. request_length += message_id_bytes;
  546. pr_debug("[sink]: %s\n", sde_hdcp_2x_message_name(msg[0]));
  547. hdcp->app_data.request.length = request_length;
  548. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_PROCESS_MSG,
  549. &hdcp->app_data);
  550. if (rc) {
  551. pr_err("failed to process sink's response to %s (%d)\n",
  552. sde_hdcp_2x_message_name(msg[0]), rc);
  553. rc = -EINVAL;
  554. goto exit;
  555. }
  556. if (msg[0] == AKE_SEND_H_PRIME && hdcp->no_stored_km) {
  557. cdata.cmd = HDCP_TRANSPORT_CMD_RECV_MESSAGE;
  558. cdata.transaction_delay = hdcp->app_data.timeout;
  559. cdata.buf = hdcp->app_data.request.data + 1;
  560. goto exit;
  561. }
  562. if (hdcp->app_data.response.length == 0)
  563. out_msg = INVALID_MESSAGE;
  564. else
  565. out_msg = (u32)hdcp->app_data.response.data[0];
  566. pr_debug("[tz]: %s\n", sde_hdcp_2x_message_name(out_msg));
  567. if (msg[0] == REP_STREAM_READY && out_msg != REP_STREAM_MANAGE) {
  568. if (hdcp->resend_stream_manage) {
  569. pr_debug("resend stream management\n");
  570. } else if (!hdcp->authenticated) {
  571. rc = hdcp2_app_comm(hdcp->hdcp2_ctx,
  572. HDCP2_CMD_EN_ENCRYPTION,
  573. &hdcp->app_data);
  574. if (!rc) {
  575. hdcp->authenticated = true;
  576. if (hdcp->force_encryption)
  577. hdcp2_force_encryption(
  578. hdcp->hdcp2_ctx, 1);
  579. cdata.cmd = HDCP_TRANSPORT_CMD_STATUS_SUCCESS;
  580. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  581. } else {
  582. pr_err("failed to enable encryption (%d)\n",
  583. rc);
  584. }
  585. }
  586. sde_hdcp_2x_initialize_command(hdcp,
  587. HDCP_TRANSPORT_CMD_LINK_POLL, &cdata);
  588. goto exit;
  589. }
  590. hdcp->resend_lc_init = false;
  591. if (msg[0] == LC_SEND_L_PRIME && out_msg == LC_INIT)
  592. hdcp->resend_lc_init = true;
  593. if (msg[0] == REP_STREAM_READY && out_msg == REP_STREAM_MANAGE)
  594. pr_debug("resend %s\n", sde_hdcp_2x_message_name(out_msg));
  595. if (out_msg == AKE_NO_STORED_KM)
  596. hdcp->no_stored_km = true;
  597. else
  598. hdcp->no_stored_km = false;
  599. if (out_msg == SKE_SEND_EKS) {
  600. hdcp->repeater_flag = hdcp->app_data.repeater_flag;
  601. hdcp->update_stream = true;
  602. }
  603. if (!atomic_read(&hdcp->hdcp_off)) {
  604. cdata.cmd = HDCP_TRANSPORT_CMD_SEND_MESSAGE;
  605. cdata.buf = hdcp->app_data.response.data + 1;
  606. cdata.buf_len = hdcp->app_data.response.length;
  607. cdata.transaction_delay = hdcp->app_data.timeout;
  608. }
  609. exit:
  610. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  611. if (rc && !atomic_read(&hdcp->hdcp_off))
  612. sde_hdcp_2x_clean(hdcp);
  613. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, hdcp->authenticated);
  614. }
  615. static struct list_head *sde_hdcp_2x_stream_present(
  616. struct sde_hdcp_2x_ctrl *hdcp, u8 stream_id, u8 virtual_channel)
  617. {
  618. struct sde_hdcp_stream *stream_entry;
  619. struct list_head *entry;
  620. bool present = false;
  621. list_for_each(entry, &hdcp->stream_handles) {
  622. stream_entry = list_entry(entry,
  623. struct sde_hdcp_stream, list);
  624. if (stream_entry->virtual_channel == virtual_channel &&
  625. stream_entry->stream_id == stream_id) {
  626. present = true;
  627. break;
  628. }
  629. }
  630. if (!present)
  631. entry = NULL;
  632. return entry;
  633. }
  634. static void sde_hdcp_2x_manage_stream(struct sde_hdcp_2x_ctrl *hdcp)
  635. {
  636. struct list_head *entry;
  637. struct list_head *element;
  638. struct sde_hdcp_stream *stream_entry;
  639. bool query_streams = false;
  640. entry = hdcp->stream_handles.next;
  641. while (entry != &hdcp->stream_handles) {
  642. stream_entry = list_entry(entry, struct sde_hdcp_stream, list);
  643. element = entry;
  644. entry = entry->next;
  645. if (!stream_entry->active) {
  646. hdcp2_close_stream(hdcp->hdcp2_ctx,
  647. stream_entry->stream_handle);
  648. hdcp->stream_count--;
  649. list_del(element);
  650. kfree(stream_entry);
  651. query_streams = true;
  652. } else if (!stream_entry->stream_handle) {
  653. if (hdcp2_open_stream(hdcp->hdcp2_ctx,
  654. stream_entry->virtual_channel,
  655. stream_entry->stream_id,
  656. &stream_entry->stream_handle))
  657. pr_err("Unable to open stream %d, virtual channel %d\n",
  658. stream_entry->stream_id,
  659. stream_entry->virtual_channel);
  660. else
  661. query_streams = true;
  662. }
  663. }
  664. if (query_streams) {
  665. if (hdcp->authenticated) {
  666. sde_hdcp_2x_query_stream(hdcp);
  667. } else if (hdcp->last_msg == REP_STREAM_MANAGE ||
  668. hdcp->last_msg == REP_STREAM_READY) {
  669. hdcp->resend_stream_manage = true;
  670. }
  671. }
  672. }
  673. static bool sde_hdcp_2x_remove_streams(struct sde_hdcp_2x_ctrl *hdcp,
  674. struct stream_info *streams, u8 num_streams)
  675. {
  676. u8 i;
  677. u8 stream_id;
  678. u8 virtual_channel;
  679. struct list_head *entry;
  680. struct sde_hdcp_stream *stream_entry;
  681. bool changed = false;
  682. for (i = 0 ; i < num_streams; i++) {
  683. stream_id = streams[i].stream_id;
  684. virtual_channel = streams[i].virtual_channel;
  685. entry = sde_hdcp_2x_stream_present(hdcp, stream_id,
  686. virtual_channel);
  687. if (!entry)
  688. continue;
  689. stream_entry = list_entry(entry, struct sde_hdcp_stream,
  690. list);
  691. if (!stream_entry->stream_handle) {
  692. /* Stream wasn't fully initialized so remove it */
  693. hdcp->stream_count--;
  694. list_del(entry);
  695. kfree(stream_entry);
  696. } else {
  697. stream_entry->active = false;
  698. }
  699. changed = true;
  700. }
  701. return changed;
  702. }
  703. static bool sde_hdcp_2x_add_streams(struct sde_hdcp_2x_ctrl *hdcp,
  704. struct stream_info *streams, u8 num_streams)
  705. {
  706. u8 i;
  707. u8 stream_id;
  708. u8 virtual_channel;
  709. struct sde_hdcp_stream *stream;
  710. bool changed = false;
  711. for (i = 0 ; i < num_streams; i++) {
  712. stream_id = streams[i].stream_id;
  713. virtual_channel = streams[i].virtual_channel;
  714. if (sde_hdcp_2x_stream_present(hdcp, stream_id,
  715. virtual_channel))
  716. continue;
  717. stream = kzalloc(sizeof(struct sde_hdcp_stream), GFP_KERNEL);
  718. if (!stream)
  719. continue;
  720. INIT_LIST_HEAD(&stream->list);
  721. stream->stream_handle = 0;
  722. stream->stream_id = stream_id;
  723. stream->virtual_channel = virtual_channel;
  724. stream->active = true;
  725. list_add(&stream->list, &hdcp->stream_handles);
  726. hdcp->stream_count++;
  727. changed = true;
  728. }
  729. return changed;
  730. }
  731. /** sde_hdcp_2x_wakeup() - wakeup the module to execute a requested command
  732. * @data: data required for executing corresponding command.
  733. *
  734. * This function is executed on caller's thread. Update the local data
  735. * and wakeup the local thread to execute the command. Once the local
  736. * thread is activated, caller's thread is returned and this function
  737. * is ready to receive next command.
  738. */
  739. static int sde_hdcp_2x_wakeup(struct sde_hdcp_2x_wakeup_data *data)
  740. {
  741. struct sde_hdcp_2x_ctrl *hdcp;
  742. int rc = 0;
  743. if (!data)
  744. return -EINVAL;
  745. hdcp = data->context;
  746. if (!hdcp)
  747. return -EINVAL;
  748. hdcp->timeout_left = data->timeout;
  749. hdcp->total_message_length = data->total_message_length;
  750. if (!completion_done(&hdcp->response_completion))
  751. complete_all(&hdcp->response_completion);
  752. switch (data->cmd) {
  753. case HDCP_2X_CMD_ENABLE:
  754. if (!atomic_cmpxchg(&hdcp->enable_pending, 0, 1)) {
  755. hdcp->device_type = data->device_type;
  756. kfifo_put(&hdcp->cmd_q, data->cmd);
  757. kthread_unpark(hdcp->thread);
  758. wake_up(&hdcp->wait_q);
  759. }
  760. break;
  761. case HDCP_2X_CMD_DISABLE:
  762. if (!atomic_xchg(&hdcp->hdcp_off, 1))
  763. kfifo_put(&hdcp->cmd_q, HDCP_2X_CMD_STOP);
  764. kfifo_put(&hdcp->cmd_q, data->cmd);
  765. kthread_park(hdcp->thread);
  766. break;
  767. case HDCP_2X_CMD_STOP:
  768. atomic_set(&hdcp->hdcp_off, 1);
  769. kfifo_put(&hdcp->cmd_q, data->cmd);
  770. kthread_park(hdcp->thread);
  771. break;
  772. case HDCP_2X_CMD_START:
  773. hdcp->no_stored_km = false;
  774. hdcp->repeater_flag = false;
  775. hdcp->update_stream = false;
  776. hdcp->authenticated = false;
  777. hdcp->last_msg = INVALID_MESSAGE;
  778. hdcp->timeout_left = 0;
  779. atomic_set(&hdcp->hdcp_off, 0);
  780. kfifo_put(&hdcp->cmd_q, data->cmd);
  781. kthread_unpark(hdcp->thread);
  782. wake_up(&hdcp->wait_q);
  783. break;
  784. case HDCP_2X_CMD_OPEN_STREAMS:
  785. if (sde_hdcp_2x_add_streams(hdcp, data->streams,
  786. data->num_streams)) {
  787. kfifo_put(&hdcp->cmd_q, data->cmd);
  788. wake_up(&hdcp->wait_q);
  789. }
  790. break;
  791. case HDCP_2X_CMD_CLOSE_STREAMS:
  792. if (sde_hdcp_2x_remove_streams(hdcp, data->streams,
  793. data->num_streams)) {
  794. kfifo_put(&hdcp->cmd_q, data->cmd);
  795. wake_up(&hdcp->wait_q);
  796. }
  797. break;
  798. case HDCP_2X_CMD_MIN_ENC_LEVEL:
  799. if (hdcp->min_enc_level != data->min_enc_level)
  800. SDE_EVT32_EXTERNAL(data->cmd, hdcp->min_enc_level, data->min_enc_level);
  801. hdcp->min_enc_level = data->min_enc_level;
  802. if (hdcp->authenticated) {
  803. kfifo_put(&hdcp->cmd_q, data->cmd);
  804. wake_up(&hdcp->wait_q);
  805. }
  806. break;
  807. default:
  808. kfifo_put(&hdcp->cmd_q, data->cmd);
  809. wake_up(&hdcp->wait_q);
  810. break;
  811. }
  812. return rc;
  813. }
  814. static void sde_hdcp_2x_enable(struct sde_hdcp_2x_ctrl *hdcp)
  815. {
  816. if (!hdcp)
  817. return;
  818. if (hdcp->hdcp2_ctx) {
  819. pr_debug("HDCP library context already acquired\n");
  820. return;
  821. }
  822. hdcp->hdcp2_ctx = hdcp2_init(hdcp->device_type);
  823. if (!hdcp->hdcp2_ctx)
  824. pr_err("Unable to acquire HDCP library handle\n");
  825. }
  826. static void sde_hdcp_2x_disable(struct sde_hdcp_2x_ctrl *hdcp)
  827. {
  828. if (!hdcp->hdcp2_ctx)
  829. return;
  830. hdcp2_deinit(hdcp->hdcp2_ctx);
  831. hdcp->hdcp2_ctx = NULL;
  832. }
  833. static int sde_hdcp_2x_main(void *data)
  834. {
  835. struct sde_hdcp_2x_ctrl *hdcp = data;
  836. enum sde_hdcp_2x_wakeup_cmd cmd;
  837. while (1) {
  838. wait_event_idle(hdcp->wait_q,
  839. !kfifo_is_empty(&hdcp->cmd_q) ||
  840. kthread_should_stop() ||
  841. kthread_should_park());
  842. if (kthread_should_stop())
  843. break;
  844. if (kfifo_is_empty(&hdcp->cmd_q) && kthread_should_park()) {
  845. kthread_parkme();
  846. continue;
  847. }
  848. if (!kfifo_get(&hdcp->cmd_q, &cmd))
  849. continue;
  850. switch (cmd) {
  851. case HDCP_2X_CMD_ENABLE:
  852. sde_hdcp_2x_enable(hdcp);
  853. atomic_set(&hdcp->enable_pending, 0);
  854. break;
  855. case HDCP_2X_CMD_DISABLE:
  856. sde_hdcp_2x_disable(hdcp);
  857. break;
  858. case HDCP_2X_CMD_START:
  859. sde_hdcp_2x_init(hdcp);
  860. break;
  861. case HDCP_2X_CMD_STOP:
  862. sde_hdcp_2x_clean(hdcp);
  863. break;
  864. case HDCP_2X_CMD_START_AUTH:
  865. sde_hdcp_2x_start_auth(hdcp);
  866. break;
  867. case HDCP_2X_CMD_MSG_SEND_SUCCESS:
  868. sde_hdcp_2x_msg_sent(hdcp);
  869. break;
  870. case HDCP_2X_CMD_MSG_SEND_FAILED:
  871. case HDCP_2X_CMD_MSG_RECV_FAILED:
  872. case HDCP_2X_CMD_LINK_FAILED:
  873. sde_hdcp_2x_clean(hdcp);
  874. break;
  875. case HDCP_2X_CMD_MSG_RECV_SUCCESS:
  876. sde_hdcp_2x_msg_recvd(hdcp);
  877. break;
  878. case HDCP_2X_CMD_MSG_RECV_TIMEOUT:
  879. sde_hdcp_2x_timeout(hdcp);
  880. break;
  881. case HDCP_2X_CMD_QUERY_STREAM_TYPE:
  882. sde_hdcp_2x_query_stream(hdcp);
  883. break;
  884. case HDCP_2X_CMD_MIN_ENC_LEVEL:
  885. if (!hdcp->repeater_flag) {
  886. sde_hdcp_2x_send_type(hdcp);
  887. break;
  888. }
  889. sde_hdcp_2x_query_stream(hdcp);
  890. break;
  891. case HDCP_2X_CMD_OPEN_STREAMS:
  892. case HDCP_2X_CMD_CLOSE_STREAMS:
  893. sde_hdcp_2x_manage_stream(hdcp);
  894. break;
  895. default:
  896. break;
  897. }
  898. }
  899. return 0;
  900. }
  901. int sde_hdcp_2x_register(struct sde_hdcp_2x_register_data *data)
  902. {
  903. int rc = 0;
  904. struct sde_hdcp_2x_ctrl *hdcp = NULL;
  905. if (!data) {
  906. pr_err("invalid input\n");
  907. return -EINVAL;
  908. }
  909. if (!data->ops) {
  910. pr_err("invalid input: txmtr context\n");
  911. return -EINVAL;
  912. }
  913. if (!data->client_ops) {
  914. pr_err("invalid input: client_ops\n");
  915. return -EINVAL;
  916. }
  917. if (!data->hdcp_data) {
  918. pr_err("invalid input: hdcp_data\n");
  919. return -EINVAL;
  920. }
  921. /* populate ops to be called by client */
  922. data->ops->feature_supported = sde_hdcp_2x_client_feature_supported;
  923. data->ops->wakeup = sde_hdcp_2x_wakeup;
  924. data->ops->force_encryption = sde_hdcp_2x_force_encryption;
  925. hdcp = kzalloc(sizeof(*hdcp), GFP_KERNEL);
  926. if (!hdcp) {
  927. rc = -ENOMEM;
  928. goto unlock;
  929. }
  930. INIT_LIST_HEAD(&hdcp->stream_handles);
  931. hdcp->client_data = data->client_data;
  932. hdcp->client_ops = data->client_ops;
  933. INIT_KFIFO(hdcp->cmd_q);
  934. init_waitqueue_head(&hdcp->wait_q);
  935. atomic_set(&hdcp->hdcp_off, 1);
  936. atomic_set(&hdcp->enable_pending, 0);
  937. init_completion(&hdcp->response_completion);
  938. *data->hdcp_data = hdcp;
  939. hdcp->thread = kthread_run(sde_hdcp_2x_main, hdcp, "hdcp_2x");
  940. if (IS_ERR(hdcp->thread)) {
  941. pr_err("unable to start lib thread\n");
  942. rc = PTR_ERR(hdcp->thread);
  943. hdcp->thread = NULL;
  944. goto error;
  945. }
  946. hdcp->force_encryption = false;
  947. return 0;
  948. error:
  949. kfree(hdcp);
  950. hdcp = NULL;
  951. unlock:
  952. return rc;
  953. }
  954. void sde_hdcp_2x_deregister(void *data)
  955. {
  956. struct sde_hdcp_2x_ctrl *hdcp = data;
  957. if (!hdcp)
  958. return;
  959. kthread_stop(hdcp->thread);
  960. sde_hdcp_2x_disable(data);
  961. kfree(hdcp);
  962. }