sde_hdcp_2x.c 27 KB

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