sde_hdcp_2x.c 27 KB

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