sde_hdcp_2x.c 26 KB

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