sde_hdcp_2x.c 27 KB

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