sde_hdcp_2x.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. kfree(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. if (atomic_read(&hdcp->hdcp_off)) {
  417. pr_debug("invalid state, hdcp off\n");
  418. goto exit;
  419. }
  420. SDE_EVT32_EXTERNAL(hdcp->app_data.response.data[0]);
  421. switch (hdcp->app_data.response.data[0]) {
  422. case SKE_SEND_TYPE_ID:
  423. if (!hdcp2_app_comm(hdcp->hdcp2_ctx,
  424. HDCP2_CMD_EN_ENCRYPTION, &hdcp->app_data)) {
  425. hdcp->authenticated = true;
  426. if (hdcp->force_encryption)
  427. hdcp2_force_encryption(hdcp->hdcp2_ctx, 1);
  428. cdata.cmd = HDCP_TRANSPORT_CMD_STATUS_SUCCESS;
  429. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  430. }
  431. /* poll for link check */
  432. sde_hdcp_2x_initialize_command(hdcp,
  433. HDCP_TRANSPORT_CMD_LINK_POLL, &cdata);
  434. break;
  435. case SKE_SEND_EKS:
  436. if (hdcp->repeater_flag && !atomic_read(&hdcp->hdcp_off)) {
  437. /* poll for link check */
  438. sde_hdcp_2x_initialize_command(hdcp,
  439. HDCP_TRANSPORT_CMD_LINK_POLL, &cdata);
  440. } else {
  441. hdcp->app_data.response.data[0] = SKE_SEND_TYPE_ID;
  442. hdcp->app_data.response.data[1] =
  443. sde_hdcp_2x_stream_type(hdcp->min_enc_level);
  444. hdcp->app_data.response.length = 1;
  445. hdcp->app_data.timeout = 100;
  446. sde_hdcp_2x_send_message(hdcp);
  447. }
  448. break;
  449. case REP_SEND_ACK:
  450. pr_debug("Repeater authentication successful. update_stream=%d\n",
  451. hdcp->update_stream);
  452. if (hdcp->update_stream) {
  453. sde_hdcp_2x_query_stream(hdcp);
  454. hdcp->update_stream = false;
  455. } else {
  456. sde_hdcp_2x_initialize_command(hdcp,
  457. HDCP_TRANSPORT_CMD_LINK_POLL, &cdata);
  458. }
  459. break;
  460. default:
  461. cdata.cmd = HDCP_TRANSPORT_CMD_RECV_MESSAGE;
  462. cdata.transaction_delay = hdcp->app_data.timeout;
  463. cdata.buf = hdcp->app_data.request.data + 1;
  464. }
  465. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  466. exit:
  467. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, hdcp->authenticated);
  468. }
  469. static void sde_hdcp_2x_init(struct sde_hdcp_2x_ctrl *hdcp)
  470. {
  471. int rc;
  472. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_START, &hdcp->app_data);
  473. if (rc)
  474. sde_hdcp_2x_clean(hdcp);
  475. }
  476. static void sde_hdcp_2x_start_auth(struct sde_hdcp_2x_ctrl *hdcp)
  477. {
  478. int rc;
  479. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY, hdcp->authenticated);
  480. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_START_AUTH,
  481. &hdcp->app_data);
  482. if (rc) {
  483. sde_hdcp_2x_clean(hdcp);
  484. return;
  485. }
  486. pr_debug("message received from TZ: %s\n",
  487. sde_hdcp_2x_message_name(hdcp->app_data.response.data[0]));
  488. sde_hdcp_2x_send_message(hdcp);
  489. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, hdcp->authenticated);
  490. }
  491. static void sde_hdcp_2x_timeout(struct sde_hdcp_2x_ctrl *hdcp)
  492. {
  493. int rc = 0;
  494. int message_id;
  495. if (atomic_read(&hdcp->hdcp_off)) {
  496. pr_debug("invalid state, hdcp off\n");
  497. return;
  498. }
  499. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_TIMEOUT,
  500. &hdcp->app_data);
  501. if (rc)
  502. goto error;
  503. message_id = (int)hdcp->app_data.response.data[0];
  504. if (message_id == LC_INIT && !atomic_read(&hdcp->hdcp_off))
  505. sde_hdcp_2x_send_message(hdcp);
  506. return;
  507. error:
  508. if (!atomic_read(&hdcp->hdcp_off))
  509. sde_hdcp_2x_clean(hdcp);
  510. }
  511. static void sde_hdcp_2x_msg_recvd(struct sde_hdcp_2x_ctrl *hdcp)
  512. {
  513. int rc = 0;
  514. char *msg = NULL;
  515. u32 message_id_bytes = 0;
  516. u32 request_length, out_msg;
  517. struct hdcp_transport_wakeup_data cdata = {HDCP_TRANSPORT_CMD_INVALID};
  518. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY, hdcp->authenticated);
  519. if (atomic_read(&hdcp->hdcp_off)) {
  520. pr_debug("invalid state, hdcp off\n");
  521. return;
  522. }
  523. cdata.context = hdcp->client_data;
  524. request_length = hdcp->total_message_length;
  525. msg = hdcp->app_data.request.data;
  526. if (request_length == 0) {
  527. pr_err("invalid message length\n");
  528. goto exit;
  529. }
  530. if (hdcp->device_type == HDCP_TXMTR_DP ||
  531. hdcp->device_type == HDCP_TXMTR_DP_MST) {
  532. msg[0] = hdcp->last_msg;
  533. message_id_bytes = 1;
  534. }
  535. request_length += message_id_bytes;
  536. pr_debug("[sink]: %s\n", sde_hdcp_2x_message_name(msg[0]));
  537. hdcp->app_data.request.length = request_length;
  538. rc = hdcp2_app_comm(hdcp->hdcp2_ctx, HDCP2_CMD_PROCESS_MSG,
  539. &hdcp->app_data);
  540. if (rc) {
  541. pr_err("failed to process sink's response to %s (%d)\n",
  542. sde_hdcp_2x_message_name(msg[0]), rc);
  543. rc = -EINVAL;
  544. goto exit;
  545. }
  546. if (msg[0] == AKE_SEND_H_PRIME && hdcp->no_stored_km) {
  547. cdata.cmd = HDCP_TRANSPORT_CMD_RECV_MESSAGE;
  548. cdata.transaction_delay = hdcp->app_data.timeout;
  549. cdata.buf = hdcp->app_data.request.data + 1;
  550. goto exit;
  551. }
  552. if (hdcp->app_data.response.length == 0)
  553. out_msg = INVALID_MESSAGE;
  554. else
  555. out_msg = (u32)hdcp->app_data.response.data[0];
  556. pr_debug("[tz]: %s\n", sde_hdcp_2x_message_name(out_msg));
  557. if (msg[0] == REP_STREAM_READY && out_msg != REP_STREAM_MANAGE) {
  558. if (hdcp->resend_stream_manage) {
  559. pr_debug("resend stream management\n");
  560. } else if (!hdcp->authenticated) {
  561. rc = hdcp2_app_comm(hdcp->hdcp2_ctx,
  562. HDCP2_CMD_EN_ENCRYPTION,
  563. &hdcp->app_data);
  564. if (!rc) {
  565. hdcp->authenticated = true;
  566. if (hdcp->force_encryption)
  567. hdcp2_force_encryption(
  568. hdcp->hdcp2_ctx, 1);
  569. cdata.cmd = HDCP_TRANSPORT_CMD_STATUS_SUCCESS;
  570. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  571. } else {
  572. pr_err("failed to enable encryption (%d)\n",
  573. rc);
  574. }
  575. }
  576. sde_hdcp_2x_initialize_command(hdcp,
  577. HDCP_TRANSPORT_CMD_LINK_POLL, &cdata);
  578. goto exit;
  579. }
  580. hdcp->resend_lc_init = false;
  581. if (msg[0] == LC_SEND_L_PRIME && out_msg == LC_INIT)
  582. hdcp->resend_lc_init = true;
  583. if (msg[0] == REP_STREAM_READY && out_msg == REP_STREAM_MANAGE)
  584. pr_debug("resend %s\n", sde_hdcp_2x_message_name(out_msg));
  585. if (out_msg == AKE_NO_STORED_KM)
  586. hdcp->no_stored_km = true;
  587. else
  588. hdcp->no_stored_km = false;
  589. if (out_msg == SKE_SEND_EKS) {
  590. hdcp->repeater_flag = hdcp->app_data.repeater_flag;
  591. hdcp->update_stream = true;
  592. }
  593. if (!atomic_read(&hdcp->hdcp_off)) {
  594. cdata.cmd = HDCP_TRANSPORT_CMD_SEND_MESSAGE;
  595. cdata.buf = hdcp->app_data.response.data + 1;
  596. cdata.buf_len = hdcp->app_data.response.length;
  597. cdata.transaction_delay = hdcp->app_data.timeout;
  598. }
  599. exit:
  600. sde_hdcp_2x_wakeup_client(hdcp, &cdata);
  601. if (rc && !atomic_read(&hdcp->hdcp_off))
  602. sde_hdcp_2x_clean(hdcp);
  603. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, hdcp->authenticated);
  604. }
  605. static struct list_head *sde_hdcp_2x_stream_present(
  606. struct sde_hdcp_2x_ctrl *hdcp, u8 stream_id, u8 virtual_channel)
  607. {
  608. struct sde_hdcp_stream *stream_entry;
  609. struct list_head *entry;
  610. bool present = false;
  611. list_for_each(entry, &hdcp->stream_handles) {
  612. stream_entry = list_entry(entry,
  613. struct sde_hdcp_stream, list);
  614. if (stream_entry->virtual_channel == virtual_channel &&
  615. stream_entry->stream_id == stream_id) {
  616. present = true;
  617. break;
  618. }
  619. }
  620. if (!present)
  621. entry = NULL;
  622. return entry;
  623. }
  624. static void sde_hdcp_2x_manage_stream(struct sde_hdcp_2x_ctrl *hdcp)
  625. {
  626. struct list_head *entry;
  627. struct list_head *element;
  628. struct sde_hdcp_stream *stream_entry;
  629. bool query_streams = false;
  630. entry = hdcp->stream_handles.next;
  631. while (entry != &hdcp->stream_handles) {
  632. stream_entry = list_entry(entry, struct sde_hdcp_stream, list);
  633. element = entry;
  634. entry = entry->next;
  635. if (!stream_entry->active) {
  636. hdcp2_close_stream(hdcp->hdcp2_ctx,
  637. stream_entry->stream_handle);
  638. hdcp->stream_count--;
  639. list_del(element);
  640. kfree(stream_entry);
  641. query_streams = true;
  642. } else if (!stream_entry->stream_handle) {
  643. if (hdcp2_open_stream(hdcp->hdcp2_ctx,
  644. stream_entry->virtual_channel,
  645. stream_entry->stream_id,
  646. &stream_entry->stream_handle))
  647. pr_err("Unable to open stream %d, virtual channel %d\n",
  648. stream_entry->stream_id,
  649. stream_entry->virtual_channel);
  650. else
  651. query_streams = true;
  652. }
  653. }
  654. if (query_streams) {
  655. if (hdcp->authenticated) {
  656. sde_hdcp_2x_query_stream(hdcp);
  657. } else if (hdcp->last_msg == REP_STREAM_MANAGE ||
  658. hdcp->last_msg == REP_STREAM_READY) {
  659. hdcp->resend_stream_manage = true;
  660. }
  661. }
  662. }
  663. static bool sde_hdcp_2x_remove_streams(struct sde_hdcp_2x_ctrl *hdcp,
  664. struct stream_info *streams, u8 num_streams)
  665. {
  666. u8 i;
  667. u8 stream_id;
  668. u8 virtual_channel;
  669. struct list_head *entry;
  670. struct sde_hdcp_stream *stream_entry;
  671. bool changed = false;
  672. for (i = 0 ; i < num_streams; i++) {
  673. stream_id = streams[i].stream_id;
  674. virtual_channel = streams[i].virtual_channel;
  675. entry = sde_hdcp_2x_stream_present(hdcp, stream_id,
  676. virtual_channel);
  677. if (!entry)
  678. continue;
  679. stream_entry = list_entry(entry, struct sde_hdcp_stream,
  680. list);
  681. if (!stream_entry->stream_handle) {
  682. /* Stream wasn't fully initialized so remove it */
  683. hdcp->stream_count--;
  684. list_del(entry);
  685. kfree(stream_entry);
  686. } else {
  687. stream_entry->active = false;
  688. }
  689. changed = true;
  690. }
  691. return changed;
  692. }
  693. static bool sde_hdcp_2x_add_streams(struct sde_hdcp_2x_ctrl *hdcp,
  694. struct stream_info *streams, u8 num_streams)
  695. {
  696. u8 i;
  697. u8 stream_id;
  698. u8 virtual_channel;
  699. struct sde_hdcp_stream *stream;
  700. bool changed = false;
  701. for (i = 0 ; i < num_streams; i++) {
  702. stream_id = streams[i].stream_id;
  703. virtual_channel = streams[i].virtual_channel;
  704. if (sde_hdcp_2x_stream_present(hdcp, stream_id,
  705. virtual_channel))
  706. continue;
  707. stream = kzalloc(sizeof(struct sde_hdcp_stream), GFP_KERNEL);
  708. if (!stream)
  709. continue;
  710. INIT_LIST_HEAD(&stream->list);
  711. stream->stream_handle = 0;
  712. stream->stream_id = stream_id;
  713. stream->virtual_channel = virtual_channel;
  714. stream->active = true;
  715. list_add(&stream->list, &hdcp->stream_handles);
  716. hdcp->stream_count++;
  717. changed = true;
  718. }
  719. return changed;
  720. }
  721. /** sde_hdcp_2x_wakeup() - wakeup the module to execute a requested command
  722. * @data: data required for executing corresponding command.
  723. *
  724. * This function is executed on caller's thread. Update the local data
  725. * and wakeup the local thread to execute the command. Once the local
  726. * thread is activated, caller's thread is returned and this function
  727. * is ready to receive next command.
  728. */
  729. static int sde_hdcp_2x_wakeup(struct sde_hdcp_2x_wakeup_data *data)
  730. {
  731. struct sde_hdcp_2x_ctrl *hdcp;
  732. int rc = 0;
  733. if (!data)
  734. return -EINVAL;
  735. hdcp = data->context;
  736. if (!hdcp)
  737. return -EINVAL;
  738. hdcp->timeout_left = data->timeout;
  739. hdcp->total_message_length = data->total_message_length;
  740. if (!completion_done(&hdcp->response_completion))
  741. complete_all(&hdcp->response_completion);
  742. switch (data->cmd) {
  743. case HDCP_2X_CMD_ENABLE:
  744. if (!atomic_cmpxchg(&hdcp->enable_pending, 0, 1)) {
  745. hdcp->device_type = data->device_type;
  746. kfifo_put(&hdcp->cmd_q, data->cmd);
  747. kthread_unpark(hdcp->thread);
  748. wake_up(&hdcp->wait_q);
  749. }
  750. break;
  751. case HDCP_2X_CMD_DISABLE:
  752. if (!atomic_xchg(&hdcp->hdcp_off, 1))
  753. kfifo_put(&hdcp->cmd_q, HDCP_2X_CMD_STOP);
  754. kfifo_put(&hdcp->cmd_q, data->cmd);
  755. kthread_park(hdcp->thread);
  756. break;
  757. case HDCP_2X_CMD_STOP:
  758. atomic_set(&hdcp->hdcp_off, 1);
  759. kfifo_put(&hdcp->cmd_q, data->cmd);
  760. kthread_park(hdcp->thread);
  761. break;
  762. case HDCP_2X_CMD_START:
  763. hdcp->no_stored_km = false;
  764. hdcp->repeater_flag = false;
  765. hdcp->update_stream = false;
  766. hdcp->authenticated = false;
  767. hdcp->last_msg = INVALID_MESSAGE;
  768. hdcp->timeout_left = 0;
  769. atomic_set(&hdcp->hdcp_off, 0);
  770. kfifo_put(&hdcp->cmd_q, data->cmd);
  771. kthread_unpark(hdcp->thread);
  772. wake_up(&hdcp->wait_q);
  773. break;
  774. case HDCP_2X_CMD_OPEN_STREAMS:
  775. if (sde_hdcp_2x_add_streams(hdcp, data->streams,
  776. data->num_streams)) {
  777. kfifo_put(&hdcp->cmd_q, data->cmd);
  778. wake_up(&hdcp->wait_q);
  779. }
  780. break;
  781. case HDCP_2X_CMD_CLOSE_STREAMS:
  782. if (sde_hdcp_2x_remove_streams(hdcp, data->streams,
  783. data->num_streams)) {
  784. kfifo_put(&hdcp->cmd_q, data->cmd);
  785. wake_up(&hdcp->wait_q);
  786. }
  787. break;
  788. case HDCP_2X_CMD_MIN_ENC_LEVEL:
  789. hdcp->min_enc_level = data->min_enc_level;
  790. if (hdcp->authenticated) {
  791. kfifo_put(&hdcp->cmd_q, data->cmd);
  792. wake_up(&hdcp->wait_q);
  793. }
  794. break;
  795. default:
  796. kfifo_put(&hdcp->cmd_q, data->cmd);
  797. wake_up(&hdcp->wait_q);
  798. break;
  799. }
  800. return rc;
  801. }
  802. static void sde_hdcp_2x_enable(struct sde_hdcp_2x_ctrl *hdcp)
  803. {
  804. if (!hdcp)
  805. return;
  806. if (hdcp->hdcp2_ctx) {
  807. pr_debug("HDCP library context already acquired\n");
  808. return;
  809. }
  810. hdcp->hdcp2_ctx = hdcp2_init(hdcp->device_type);
  811. if (!hdcp->hdcp2_ctx)
  812. pr_err("Unable to acquire HDCP library handle\n");
  813. }
  814. static void sde_hdcp_2x_disable(struct sde_hdcp_2x_ctrl *hdcp)
  815. {
  816. if (!hdcp->hdcp2_ctx)
  817. return;
  818. hdcp2_deinit(hdcp->hdcp2_ctx);
  819. hdcp->hdcp2_ctx = NULL;
  820. }
  821. static int sde_hdcp_2x_main(void *data)
  822. {
  823. struct sde_hdcp_2x_ctrl *hdcp = data;
  824. enum sde_hdcp_2x_wakeup_cmd cmd;
  825. while (1) {
  826. wait_event(hdcp->wait_q,
  827. !kfifo_is_empty(&hdcp->cmd_q) ||
  828. kthread_should_stop() ||
  829. kthread_should_park());
  830. if (kthread_should_stop())
  831. break;
  832. if (kfifo_is_empty(&hdcp->cmd_q) && kthread_should_park()) {
  833. kthread_parkme();
  834. continue;
  835. }
  836. if (!kfifo_get(&hdcp->cmd_q, &cmd))
  837. continue;
  838. switch (cmd) {
  839. case HDCP_2X_CMD_ENABLE:
  840. sde_hdcp_2x_enable(hdcp);
  841. atomic_set(&hdcp->enable_pending, 0);
  842. break;
  843. case HDCP_2X_CMD_DISABLE:
  844. sde_hdcp_2x_disable(hdcp);
  845. break;
  846. case HDCP_2X_CMD_START:
  847. sde_hdcp_2x_init(hdcp);
  848. break;
  849. case HDCP_2X_CMD_STOP:
  850. sde_hdcp_2x_clean(hdcp);
  851. break;
  852. case HDCP_2X_CMD_START_AUTH:
  853. sde_hdcp_2x_start_auth(hdcp);
  854. break;
  855. case HDCP_2X_CMD_MSG_SEND_SUCCESS:
  856. sde_hdcp_2x_msg_sent(hdcp);
  857. break;
  858. case HDCP_2X_CMD_MSG_SEND_FAILED:
  859. case HDCP_2X_CMD_MSG_RECV_FAILED:
  860. case HDCP_2X_CMD_LINK_FAILED:
  861. sde_hdcp_2x_clean(hdcp);
  862. break;
  863. case HDCP_2X_CMD_MSG_RECV_SUCCESS:
  864. sde_hdcp_2x_msg_recvd(hdcp);
  865. break;
  866. case HDCP_2X_CMD_MSG_RECV_TIMEOUT:
  867. sde_hdcp_2x_timeout(hdcp);
  868. break;
  869. case HDCP_2X_CMD_QUERY_STREAM_TYPE:
  870. sde_hdcp_2x_query_stream(hdcp);
  871. break;
  872. case HDCP_2X_CMD_MIN_ENC_LEVEL:
  873. if (!hdcp->repeater_flag) {
  874. sde_hdcp_2x_send_type(hdcp);
  875. break;
  876. }
  877. sde_hdcp_2x_query_stream(hdcp);
  878. break;
  879. case HDCP_2X_CMD_OPEN_STREAMS:
  880. case HDCP_2X_CMD_CLOSE_STREAMS:
  881. sde_hdcp_2x_manage_stream(hdcp);
  882. break;
  883. default:
  884. break;
  885. }
  886. }
  887. return 0;
  888. }
  889. int sde_hdcp_2x_register(struct sde_hdcp_2x_register_data *data)
  890. {
  891. int rc = 0;
  892. struct sde_hdcp_2x_ctrl *hdcp = NULL;
  893. if (!data) {
  894. pr_err("invalid input\n");
  895. return -EINVAL;
  896. }
  897. if (!data->ops) {
  898. pr_err("invalid input: txmtr context\n");
  899. return -EINVAL;
  900. }
  901. if (!data->client_ops) {
  902. pr_err("invalid input: client_ops\n");
  903. return -EINVAL;
  904. }
  905. if (!data->hdcp_data) {
  906. pr_err("invalid input: hdcp_data\n");
  907. return -EINVAL;
  908. }
  909. /* populate ops to be called by client */
  910. data->ops->feature_supported = sde_hdcp_2x_client_feature_supported;
  911. data->ops->wakeup = sde_hdcp_2x_wakeup;
  912. data->ops->force_encryption = sde_hdcp_2x_force_encryption;
  913. hdcp = kzalloc(sizeof(*hdcp), GFP_KERNEL);
  914. if (!hdcp) {
  915. rc = -ENOMEM;
  916. goto unlock;
  917. }
  918. INIT_LIST_HEAD(&hdcp->stream_handles);
  919. hdcp->client_data = data->client_data;
  920. hdcp->client_ops = data->client_ops;
  921. INIT_KFIFO(hdcp->cmd_q);
  922. init_waitqueue_head(&hdcp->wait_q);
  923. atomic_set(&hdcp->hdcp_off, 1);
  924. atomic_set(&hdcp->enable_pending, 0);
  925. init_completion(&hdcp->response_completion);
  926. *data->hdcp_data = hdcp;
  927. hdcp->thread = kthread_run(sde_hdcp_2x_main, hdcp, "hdcp_2x");
  928. if (IS_ERR(hdcp->thread)) {
  929. pr_err("unable to start lib thread\n");
  930. rc = PTR_ERR(hdcp->thread);
  931. hdcp->thread = NULL;
  932. goto error;
  933. }
  934. hdcp->force_encryption = false;
  935. return 0;
  936. error:
  937. kfree(hdcp);
  938. hdcp = NULL;
  939. unlock:
  940. return rc;
  941. }
  942. void sde_hdcp_2x_deregister(void *data)
  943. {
  944. struct sde_hdcp_2x_ctrl *hdcp = data;
  945. if (!hdcp)
  946. return;
  947. kthread_stop(hdcp->thread);
  948. sde_hdcp_2x_disable(data);
  949. kfree(hdcp);
  950. }