sde_hdcp_2x.c 27 KB

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