sde_hdcp_2x.c 25 KB

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