sde_hdcp_2x.c 25 KB

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