sde_hdcp_2x.c 27 KB

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