hdcp_smcinvoke.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include "smcinvoke_object.h"
  6. #include <include/linux/IClientEnv.h>
  7. #include <include/smci/uid/CAppClient.h>
  8. #include <include/smci/uid/CAppLoader.h>
  9. #include <include/smci/interface/IAppClient.h>
  10. #include <include/smci/interface/IAppController.h>
  11. #include <include/smci/interface/IAppLoader.h>
  12. #include <include/smci/interface/IOpener.h>
  13. #include "hdcp_main.h"
  14. #include "hdcp_smcinvoke.h"
  15. #include "hdcp1.h"
  16. #include "hdcp1_ops.h"
  17. #include "hdcp2p2.h"
  18. static int hdcp1_verify_key(struct hdcp1_smcinvoke_handle *handle)
  19. {
  20. int ret = 0;
  21. if (!handle) {
  22. pr_err("invalid HDCP 1.x handle\n");
  23. return -EINVAL;
  24. }
  25. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  26. pr_err("%s app not loaded\n", HDCP1_APP_NAME);
  27. return -EINVAL;
  28. }
  29. ret = hdcp1_verify(handle->hdcp1_app_obj, 1);
  30. if (ret)
  31. pr_err("hdcp1_verify failed error:%d\n", ret);
  32. return ret;
  33. }
  34. static int hdcp1_key_set(struct hdcp1_smcinvoke_handle *handle,
  35. uint32_t *aksv_msb, uint32_t *aksv_lsb)
  36. {
  37. int ret = 0;
  38. uint8_t *ksvRes = NULL;
  39. size_t ksvResLen = 0;
  40. ksvRes = kzalloc(HDCP1_AKSV_SIZE, GFP_KERNEL);
  41. if (!ksvRes)
  42. return -EINVAL;
  43. if (aksv_msb == NULL || aksv_lsb == NULL) {
  44. pr_err("invalid aksv\n");
  45. return -EINVAL;
  46. }
  47. if (!handle) {
  48. pr_err("invalid HDCP 1.x handle\n");
  49. return -EINVAL;
  50. }
  51. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  52. pr_err("hdcp1 app not loaded\n");
  53. return -EINVAL;
  54. }
  55. ret = hdcp1_set_key(handle->hdcp1_app_obj, ksvRes, HDCP1_AKSV_SIZE,
  56. &ksvResLen);
  57. if (ret) {
  58. pr_err("hdcp1_set_key failed ret=%d\n", ret);
  59. return -ENOKEY;
  60. }
  61. /* copy bytes into msb and lsb */
  62. *aksv_msb = ksvRes[0] << 24 | ksvRes[1] << 16 | ksvRes[2] << 8 | ksvRes[3];
  63. *aksv_lsb = ksvRes[4] << 24 | ksvRes[5] << 16 | ksvRes[6] << 8 | ksvRes[7];
  64. ret = hdcp1_validate_aksv(*aksv_msb, *aksv_lsb);
  65. if (ret)
  66. pr_err("aksv validation failed (%d)\n", ret);
  67. return ret;
  68. }
  69. int load_app(char *app_name, struct Object *app_obj,
  70. struct Object *app_controller_obj)
  71. {
  72. int ret = 0;
  73. uint8_t *buffer = NULL;
  74. struct qtee_shm shm = {0};
  75. size_t size = 0;
  76. struct Object client_env = {NULL, NULL};
  77. struct Object app_loader = {NULL, NULL};
  78. buffer = firmware_request_from_smcinvoke(app_name, &size, &shm);
  79. if (buffer == NULL) {
  80. pr_err("firmware_request_from_smcinvoke failed\n");
  81. return -EINVAL;
  82. }
  83. ret = get_client_env_object(&client_env);
  84. if (ret) {
  85. pr_err("get_client_env_object failed :%d\n", ret);
  86. client_env.invoke = NULL;
  87. client_env.context = NULL;
  88. goto error;
  89. }
  90. ret = IClientEnv_open(client_env, CAppLoader_UID, &app_loader);
  91. if (ret) {
  92. pr_err("IClientEnv_open failed :%d\n", ret);
  93. app_loader.invoke = NULL;
  94. app_loader.context = NULL;
  95. goto error;
  96. }
  97. ret = IAppLoader_loadFromBuffer(app_loader, (const void *)buffer, size,
  98. app_controller_obj);
  99. if (ret) {
  100. pr_err("IAppLoader_loadFromBuffer failed :%d\n", ret);
  101. app_controller_obj->invoke = NULL;
  102. app_controller_obj->context = NULL;
  103. goto error;
  104. }
  105. ret = IAppController_getAppObject(*app_controller_obj, app_obj);
  106. if (ret) {
  107. pr_err("IAppController_getAppObject failed :%d\n", ret);
  108. goto error;
  109. }
  110. error:
  111. qtee_shmbridge_free_shm(&shm);
  112. Object_ASSIGN_NULL(app_loader);
  113. Object_ASSIGN_NULL(client_env);
  114. return ret;
  115. }
  116. static int hdcp1_app_load(struct hdcp1_smcinvoke_handle *handle)
  117. {
  118. int ret = 0;
  119. if (!handle) {
  120. pr_err("invalid input\n");
  121. ret = -EINVAL;
  122. goto error;
  123. }
  124. if (handle->hdcp_state & HDCP_STATE_APP_LOADED)
  125. goto error;
  126. ret = load_app(HDCP1_APP_NAME, &(handle->hdcp1_app_obj),
  127. &(handle->hdcp1_appcontroller_obj));
  128. if (ret) {
  129. pr_err("hdcp1 TA load failed :%d\n", ret);
  130. goto error;
  131. }
  132. if (Object_isNull(handle->hdcp1_app_obj)) {
  133. pr_err("hdcp1_app_obj is NULL\n");
  134. ret = -EINVAL;
  135. goto error;
  136. }
  137. ret = load_app(HDCP1OPS_APP_NAME, &(handle->hdcp1ops_app_obj),
  138. &(handle->hdcp1ops_appcontroller_obj));
  139. if (ret) {
  140. pr_err("hdcp1ops TA load failed :%d\n", ret);
  141. goto error;
  142. }
  143. if (Object_isNull(handle->hdcp1ops_app_obj)) {
  144. pr_err("hdcp1ops_app_obj is NULL\n");
  145. ret = -EINVAL;
  146. goto error;
  147. }
  148. handle->hdcp_state |= HDCP_STATE_APP_LOADED;
  149. error:
  150. return ret;
  151. }
  152. static void hdcp1_app_unload(struct hdcp1_smcinvoke_handle *handle)
  153. {
  154. if (!handle || !handle->hdcp1_app_obj.context) {
  155. pr_err("invalid handle\n");
  156. return;
  157. }
  158. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  159. pr_warn("hdcp1 app not loaded\n");
  160. return;
  161. }
  162. Object_ASSIGN_NULL(handle->hdcp1_app_obj);
  163. Object_ASSIGN_NULL(handle->hdcp1_appcontroller_obj);
  164. Object_ASSIGN_NULL(handle->hdcp1ops_app_obj);
  165. Object_ASSIGN_NULL(handle->hdcp1ops_appcontroller_obj);
  166. handle->hdcp_state &= ~HDCP_STATE_APP_LOADED;
  167. pr_debug("%s app unloaded\n", HDCP1_APP_NAME);
  168. }
  169. void *hdcp1_init_smcinvoke(void)
  170. {
  171. struct hdcp1_smcinvoke_handle *handle =
  172. kzalloc(sizeof(struct hdcp1_smcinvoke_handle), GFP_KERNEL);
  173. if (!handle)
  174. goto error;
  175. error:
  176. return handle;
  177. }
  178. bool hdcp1_feature_supported_smcinvoke(void *data)
  179. {
  180. int ret = 0;
  181. bool supported = false;
  182. struct hdcp1_smcinvoke_handle *handle = data;
  183. if (!handle) {
  184. pr_err("invalid handle\n");
  185. goto error;
  186. }
  187. if (handle->feature_supported) {
  188. supported = true;
  189. goto error;
  190. }
  191. ret = hdcp1_app_load(handle);
  192. if (!ret && (handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  193. if (!hdcp1_verify_key(handle)) {
  194. pr_debug("HDCP 1.x supported\n");
  195. handle->feature_supported = true;
  196. supported = true;
  197. }
  198. hdcp1_app_unload(handle);
  199. }
  200. error:
  201. return supported;
  202. }
  203. int hdcp1_set_enc_smcinvoke(void *data, bool enable)
  204. {
  205. int ret = 0;
  206. struct hdcp1_smcinvoke_handle *handle = data;
  207. if (!handle || !handle->hdcp1_app_obj.context) {
  208. pr_err("invalid HDCP 1.x handle\n");
  209. return -EINVAL;
  210. }
  211. if (!handle->feature_supported) {
  212. pr_err("HDCP 1.x not supported\n");
  213. return -EINVAL;
  214. }
  215. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  216. pr_err("%s app not loaded\n", HDCP1_APP_NAME);
  217. return -EINVAL;
  218. }
  219. ret = hdcp1_set_encryption(handle->hdcp1_app_obj, enable);
  220. if (ret)
  221. pr_err("hdcp1_set_encryption failed :%d\n", ret);
  222. return ret;
  223. }
  224. int hdcp1_ops_notify_smcinvoke(void *data, void *topo, bool is_authenticated)
  225. {
  226. int ret = 0;
  227. struct hdcp1_smcinvoke_handle *handle = data;
  228. if (!handle || !handle->hdcp1ops_app_obj.context) {
  229. pr_err("invalid HDCP 1.x ops handle\n");
  230. return -EINVAL;
  231. }
  232. if (!handle->feature_supported) {
  233. pr_err("HDCP 1.x not supported\n");
  234. return -EINVAL;
  235. }
  236. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  237. pr_err("%s app not loaded\n", HDCP1OPS_APP_NAME);
  238. return -EINVAL;
  239. }
  240. ret = hdcp1_ops_notify_topology_change(handle->hdcp1ops_app_obj);
  241. if (ret)
  242. pr_err("hdcp1_ops_notify_topology_change failed, ret=%d\n", ret);
  243. return ret;
  244. }
  245. int hdcp1_start_smcinvoke(void *data, u32 *aksv_msb, u32 *aksv_lsb)
  246. {
  247. int ret = 0;
  248. struct hdcp1_smcinvoke_handle *handle = data;
  249. if (!aksv_msb || !aksv_lsb) {
  250. pr_err("invalid aksv output buffer\n");
  251. ret = -EINVAL;
  252. goto error;
  253. }
  254. if (!handle) {
  255. pr_err("invalid handle\n");
  256. ret = -EINVAL;
  257. goto error;
  258. }
  259. if (!handle->feature_supported) {
  260. pr_err("feature not supported\n");
  261. ret = -EINVAL;
  262. goto error;
  263. }
  264. if (handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  265. pr_debug("%s app already loaded\n", HDCP1_APP_NAME);
  266. goto error;
  267. }
  268. ret = hdcp1_app_load(handle);
  269. if (ret)
  270. goto error;
  271. ret = hdcp1_key_set(handle, aksv_msb, aksv_lsb);
  272. if (ret)
  273. goto key_error;
  274. pr_debug("success\n");
  275. return ret;
  276. key_error:
  277. hdcp1_app_unload(handle);
  278. error:
  279. return ret;
  280. }
  281. void hdcp1_stop_smcinvoke(void *data)
  282. {
  283. struct hdcp1_smcinvoke_handle *hdcp1_handle = data;
  284. if (!hdcp1_handle) {
  285. pr_err("invalid HDCP 1.x handle\n");
  286. return;
  287. }
  288. if (!(hdcp1_handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  289. pr_err("hdcp1 app not loaded\n");
  290. return;
  291. }
  292. Object_ASSIGN_NULL(hdcp1_handle->hdcp1_app_obj);
  293. Object_ASSIGN_NULL(hdcp1_handle->hdcp1_appcontroller_obj);
  294. Object_ASSIGN_NULL(hdcp1_handle->hdcp1ops_app_obj);
  295. Object_ASSIGN_NULL(hdcp1_handle->hdcp1ops_appcontroller_obj);
  296. hdcp1_handle->hdcp_state &= ~HDCP_STATE_APP_LOADED;
  297. }
  298. void *hdcp2_init_smcinvoke(u32 device_type)
  299. {
  300. struct hdcp2_smcinvoke_handle *handle =
  301. kzalloc(sizeof(struct hdcp2_smcinvoke_handle), GFP_KERNEL);
  302. if (!handle)
  303. goto error;
  304. handle->device_type = device_type;
  305. error:
  306. return handle;
  307. }
  308. void hdcp2_deinit_smcinvoke(void *ctx)
  309. {
  310. kfree_sensitive(ctx);
  311. }
  312. int hdcp_get_version(struct hdcp2_smcinvoke_handle *handle)
  313. {
  314. int ret = 0;
  315. uint32_t app_major_version = 0;
  316. uint32_t appversion = 0;
  317. if (handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  318. pr_err("hdcp2p2 TA already loaded\n");
  319. goto error;
  320. }
  321. ret = hdcp2p2_version(handle->hdcp2_app_obj, &appversion);
  322. if (ret) {
  323. pr_err("hdcp2p2_version failed :%d\n", ret);
  324. goto error;
  325. }
  326. app_major_version = HCDP_TXMTR_GET_MAJOR_VERSION(appversion);
  327. pr_debug("hdp2p2 app major version %d, app version %d\n", app_major_version,
  328. appversion);
  329. error:
  330. return ret;
  331. }
  332. int hdcp2_app_init(struct hdcp2_smcinvoke_handle *handle)
  333. {
  334. int ret = 0;
  335. uint32_t app_minor_version = 0;
  336. uint32_t clientversion = 0;
  337. uint32_t appversion = 0;
  338. if (handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  339. pr_err("hdcp2p2 TA already loaded\n");
  340. goto error;
  341. }
  342. clientversion = HDCP_CLIENT_MAKE_VERSION(HDCP_CLIENT_MAJOR_VERSION,
  343. HDCP_CLIENT_MINOR_VERSION,
  344. HDCP_CLIENT_PATCH_VERSION);
  345. ret = hdcp2p2_init(handle->hdcp2_app_obj, clientversion, &appversion);
  346. if (ret) {
  347. pr_err("hdcp2p2_init failed:%d\n", ret);
  348. goto error;
  349. }
  350. app_minor_version = HCDP_TXMTR_GET_MINOR_VERSION(appversion);
  351. if (app_minor_version != HDCP_CLIENT_MINOR_VERSION) {
  352. pr_err("client-app minor version mismatch app(%d), client(%d)\n",
  353. app_minor_version, HDCP_CLIENT_MINOR_VERSION);
  354. ret = -1;
  355. goto error;
  356. }
  357. pr_err("client version major(%d), minor(%d), patch(%d)\n",
  358. HDCP_CLIENT_MAJOR_VERSION, HDCP_CLIENT_MINOR_VERSION,
  359. HDCP_CLIENT_PATCH_VERSION);
  360. pr_err("app version major(%d), minor(%d), patch(%d)\n",
  361. HCDP_TXMTR_GET_MAJOR_VERSION(appversion),
  362. HCDP_TXMTR_GET_MINOR_VERSION(appversion),
  363. HCDP_TXMTR_GET_PATCH_VERSION(appversion));
  364. error:
  365. return ret;
  366. }
  367. int hdcp2_app_tx_init(struct hdcp2_smcinvoke_handle *handle)
  368. {
  369. int ret = 0;
  370. uint32_t ctxhandle = 0;
  371. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  372. pr_err("session not initialized\n");
  373. ret = -EINVAL;
  374. goto error;
  375. }
  376. if (handle->hdcp_state & HDCP_STATE_TXMTR_INIT) {
  377. pr_err("txmtr already initialized\n");
  378. goto error;
  379. }
  380. ret = hdcp2p2_tx_init(handle->hdcp2_app_obj, handle->session_id, &ctxhandle);
  381. if (ret) {
  382. pr_err("hdcp2p2_tx_init failed :%d\n", ret);
  383. goto error;
  384. }
  385. handle->tz_ctxhandle = ctxhandle;
  386. handle->hdcp_state |= HDCP_STATE_TXMTR_INIT;
  387. error:
  388. return ret;
  389. }
  390. int hdcp2_app_tx_deinit(struct hdcp2_smcinvoke_handle *handle)
  391. {
  392. int ret = 0;
  393. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  394. pr_err("hdcp2p2 TA not loaded\n");
  395. ret = -EINVAL;
  396. goto error;
  397. }
  398. if (!(handle->hdcp_state & HDCP_STATE_TXMTR_INIT)) {
  399. pr_err("txmtr not initialized\n");
  400. ret = -EINVAL;
  401. goto error;
  402. }
  403. ret = hdcp2p2_tx_deinit(handle->hdcp2_app_obj, handle->tz_ctxhandle);
  404. if (ret) {
  405. pr_err("hdcp2p2_tx_deinit failed :%d\n", ret);
  406. goto error;
  407. }
  408. handle->hdcp_state &= ~HDCP_STATE_TXMTR_INIT;
  409. error:
  410. return ret;
  411. }
  412. static int hdcp2_app_load(struct hdcp2_smcinvoke_handle *handle)
  413. {
  414. int ret = 0;
  415. if (!handle) {
  416. pr_err("invalid input\n");
  417. ret = -EINVAL;
  418. goto error;
  419. }
  420. if (handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  421. pr_err("hdcp2p2 TA already loaded\n");
  422. goto error;
  423. }
  424. ret = load_app(HDCP2P2_APP_NAME, &(handle->hdcp2_app_obj),
  425. &(handle->hdcp2_appcontroller_obj));
  426. if (ret) {
  427. pr_err("hdcp2p2 TA load_app failed :%d\n", ret);
  428. goto error;
  429. }
  430. if (Object_isNull(handle->hdcp2_app_obj)) {
  431. pr_err("hdcp2p2 app object is NULL\n");
  432. ret = -EINVAL;
  433. goto error;
  434. }
  435. ret = load_app(HDCPSRM_APP_NAME, &(handle->hdcpsrm_app_obj),
  436. &(handle->hdcpsrm_appcontroller_obj));
  437. if (ret == 16) {
  438. pr_err("hdcpsrm TA already loaded\n");
  439. } else if (ret) {
  440. pr_err("hdcpsrm TA load failed :%d\n", ret);
  441. goto error;
  442. }
  443. if (Object_isNull(handle->hdcpsrm_app_obj)) {
  444. pr_err("hdcpsrm app object is NULL\n");
  445. ret = -EINVAL;
  446. goto error;
  447. }
  448. ret = hdcp_get_version(handle);
  449. if (ret) {
  450. pr_err("library get version failed\n");
  451. goto error;
  452. }
  453. ret = hdcp2_app_init(handle);
  454. if (ret) {
  455. pr_err("app init failed\n");
  456. goto error;
  457. }
  458. handle->hdcp_state |= HDCP_STATE_APP_LOADED;
  459. error:
  460. return ret;
  461. }
  462. static int hdcp2_app_unload(struct hdcp2_smcinvoke_handle *handle)
  463. {
  464. int ret = 0;
  465. ret = hdcp2p2_deinit(handle->hdcp2_app_obj);
  466. if (ret) {
  467. pr_err("hdcp2p2_deinit failed:%d\n", ret);
  468. goto error;
  469. }
  470. Object_ASSIGN_NULL(handle->hdcp2_app_obj);
  471. Object_ASSIGN_NULL(handle->hdcp2_appcontroller_obj);
  472. Object_ASSIGN_NULL(handle->hdcpsrm_app_obj);
  473. Object_ASSIGN_NULL(handle->hdcpsrm_appcontroller_obj);
  474. handle->hdcp_state &= ~HDCP_STATE_APP_LOADED;
  475. error:
  476. return ret;
  477. }
  478. static int hdcp2_verify_key(struct hdcp2_smcinvoke_handle *handle)
  479. {
  480. int ret = 0;
  481. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  482. pr_err("%s app not loaded\n", HDCP2P2_APP_NAME);
  483. ret = -EINVAL;
  484. goto error;
  485. }
  486. ret = hdcp2p2_verify_key(handle->hdcp2_app_obj);
  487. if (ret) {
  488. pr_err("hdcp2p2_verify_key failed:%d\n", ret);
  489. goto error;
  490. }
  491. error:
  492. return ret;
  493. }
  494. static int hdcp2_app_session_init(struct hdcp2_smcinvoke_handle *handle)
  495. {
  496. int ret = 0;
  497. uint32_t sessionId = 0;
  498. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  499. pr_err("hdcp2p2 app not loaded\n");
  500. ret = -EINVAL;
  501. goto error;
  502. }
  503. if (handle->hdcp_state & HDCP_STATE_SESSION_INIT) {
  504. pr_err("session already initialized\n");
  505. goto error;
  506. }
  507. if (Object_isNull(handle->hdcp2_app_obj)) {
  508. pr_err("hdcp2_app_obj is NULL\n");
  509. goto error;
  510. }
  511. ret = hdcp2p2_session_init(handle->hdcp2_app_obj, handle->device_type,
  512. &sessionId);
  513. if (ret) {
  514. pr_err("hdcp2p2_session_init failed ret:%d\n", ret);
  515. goto error;
  516. }
  517. handle->session_id = sessionId;
  518. handle->hdcp_state |= HDCP_STATE_SESSION_INIT;
  519. error:
  520. return ret;
  521. }
  522. static int hdcp2_app_session_deinit(struct hdcp2_smcinvoke_handle *handle)
  523. {
  524. int ret = 0;
  525. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  526. pr_err("hdcp2p2 app not loaded\n");
  527. ret = -EINVAL;
  528. goto error;
  529. }
  530. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  531. pr_err("session not initialized\n");
  532. ret = -EINVAL;
  533. goto error;
  534. }
  535. ret = hdcp2p2_session_deinit(handle->hdcp2_app_obj, handle->session_id);
  536. if (ret) {
  537. pr_err("hdcp2p2_session_deinit failed:%d\n", ret);
  538. goto error;
  539. }
  540. handle->hdcp_state &= ~HDCP_STATE_SESSION_INIT;
  541. error:
  542. return ret;
  543. }
  544. int hdcp2_app_start_smcinvoke(void *ctx, uint32_t req_len)
  545. {
  546. struct hdcp2_smcinvoke_handle *handle = NULL;
  547. int ret = 0;
  548. handle = (struct hdcp2_smcinvoke_handle *)ctx;
  549. if (!handle) {
  550. pr_err("Invalid handle\n");
  551. ret = -EINVAL;
  552. goto error;
  553. }
  554. handle->app_data.request.data = kmalloc(MAX_RX_MESSAGE_SIZE, GFP_KERNEL);
  555. if (!handle->app_data.request.data) {
  556. ret = -EINVAL;
  557. goto error;
  558. }
  559. handle->app_data.response.data = kmalloc(MAX_TX_MESSAGE_SIZE, GFP_KERNEL);
  560. if (!handle->app_data.response.data) {
  561. ret = -EINVAL;
  562. goto error;
  563. }
  564. ret = hdcp2_app_load(handle);
  565. if (ret)
  566. goto error;
  567. ret = hdcp2_app_session_init(handle);
  568. if (ret)
  569. goto error;
  570. ret = hdcp2_app_tx_init(handle);
  571. if (ret)
  572. goto error;
  573. error:
  574. return ret;
  575. }
  576. int hdcp2_app_start_auth_smcinvoke(void *ctx, uint32_t req_len)
  577. {
  578. struct hdcp2_smcinvoke_handle *handle = NULL;
  579. int ret = 0;
  580. size_t resMsgOut = 0;
  581. uint32_t timeout = 0;
  582. uint32_t flag = 0;
  583. uint32_t ctxhandle = 0;
  584. uint8_t resMsg[MAX_TX_MESSAGE_SIZE] = {0};
  585. handle = ctx;
  586. if (!handle) {
  587. pr_err("Invalid handle\n");
  588. ret = -EINVAL;
  589. goto error;
  590. }
  591. handle->app_data.request.length = req_len;
  592. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  593. pr_err("session not initialized\n");
  594. ret = -EINVAL;
  595. goto error;
  596. }
  597. if (!(handle->hdcp_state & HDCP_STATE_TXMTR_INIT)) {
  598. pr_err("txmtr not initialized\n");
  599. ret = -EINVAL;
  600. goto error;
  601. }
  602. ret = hdcp2p2_start_auth(handle->hdcp2_app_obj, handle->tz_ctxhandle,
  603. resMsg, MAX_TX_MESSAGE_SIZE, &resMsgOut, &timeout,
  604. &flag, &ctxhandle);
  605. if (ret) {
  606. pr_err("hdcp2p2_start_auth failed :%d\n", ret);
  607. goto error;
  608. }
  609. memcpy(handle->app_data.response.data, resMsg, resMsgOut);
  610. handle->app_data.response.length = resMsgOut;
  611. handle->app_data.timeout = timeout;
  612. handle->app_data.repeater_flag = false;
  613. handle->tz_ctxhandle = ctxhandle;
  614. error:
  615. return ret;
  616. }
  617. int hdcp2_app_process_msg_smcinvoke(void *ctx, uint32_t req_len)
  618. {
  619. struct hdcp2_smcinvoke_handle *handle = NULL;
  620. int ret = 0;
  621. size_t resMsgLen = 0;
  622. uint32_t timeout = 0;
  623. uint32_t flag = 0;
  624. uint32_t state = 0;
  625. uint8_t resMsg[MAX_TX_MESSAGE_SIZE] = {0};
  626. handle = ctx;
  627. if (!handle) {
  628. pr_err("Invalid handle\n");
  629. ret = -EINVAL;
  630. goto error;
  631. }
  632. handle->app_data.request.length = req_len;
  633. if (!handle->app_data.request.data) {
  634. pr_err("invalid request buffer\n");
  635. ret = -EINVAL;
  636. goto error;
  637. }
  638. ret = hdcp2p2_rcvd_msg(
  639. handle->hdcp2_app_obj, handle->app_data.request.data,
  640. handle->app_data.request.length, handle->tz_ctxhandle, resMsg,
  641. MAX_TX_MESSAGE_SIZE, &resMsgLen, &timeout, &flag, &state);
  642. if (ret) {
  643. pr_err("hdcp2p2_rcvd_msg failed :%d\n", ret);
  644. goto error;
  645. }
  646. memcpy(handle->app_data.response.data, resMsg, resMsgLen);
  647. /* check if it's a repeater */
  648. if (flag == HDCP_TXMTR_SUBSTATE_WAITING_FOR_RECIEVERID_LIST)
  649. handle->app_data.repeater_flag = true;
  650. handle->app_data.response.length = resMsgLen;
  651. handle->app_data.timeout = timeout;
  652. error:
  653. return ret;
  654. }
  655. int hdcp2_app_timeout_smcinvoke(void *ctx, uint32_t req_len)
  656. {
  657. struct hdcp2_smcinvoke_handle *handle = NULL;
  658. int ret = 0;
  659. uint32_t timeout = 0;
  660. size_t resMsgLenOut = 0;
  661. uint8_t resMsg[MAX_TX_MESSAGE_SIZE] = {0};
  662. handle = ctx;
  663. if (!handle) {
  664. pr_err("Invalid handle\n");
  665. ret = -EINVAL;
  666. goto error;
  667. }
  668. handle->app_data.request.length = req_len;
  669. ret = hdcp2p2_send_timeout(handle->hdcp2_app_obj, handle->tz_ctxhandle,
  670. resMsg, MAX_TX_MESSAGE_SIZE, &resMsgLenOut,
  671. &timeout);
  672. if (ret) {
  673. pr_err("hdcp2p2_send_timeout failed :%d\n", ret);
  674. goto error;
  675. }
  676. memcpy(handle->app_data.response.data, resMsg, resMsgLenOut);
  677. handle->app_data.response.length = resMsgLenOut;
  678. handle->app_data.timeout = timeout;
  679. error:
  680. return ret;
  681. }
  682. int hdcp2_app_enable_encryption_smcinvoke(void *ctx, uint32_t req_len)
  683. {
  684. struct hdcp2_smcinvoke_handle *handle = NULL;
  685. int ret = 0;
  686. handle = ctx;
  687. if (!handle) {
  688. pr_err("Invalid handle\n");
  689. ret = -EINVAL;
  690. goto error;
  691. }
  692. handle->app_data.request.length = req_len;
  693. /*
  694. * wait at least 200ms before enabling encryption
  695. * as per hdcp2p2 specifications.
  696. */
  697. msleep(SLEEP_SET_HW_KEY_MS);
  698. ret = hdcp2p2_set_hw_key(handle->hdcp2_app_obj, handle->tz_ctxhandle);
  699. if (ret) {
  700. pr_err("hdcp2p2_set_hw_key failed:%d\n", ret);
  701. goto error;
  702. }
  703. handle->hdcp_state |= HDCP_STATE_AUTHENTICATED;
  704. error:
  705. return ret;
  706. }
  707. int hdcp2_app_query_stream_smcinvoke(void *ctx, uint32_t req_len)
  708. {
  709. struct hdcp2_smcinvoke_handle *handle = NULL;
  710. int ret = 0;
  711. uint32_t timeout = 0;
  712. size_t resMsgLenOut = 0;
  713. uint8_t resMsg[MAX_TX_MESSAGE_SIZE] = {0};
  714. handle = ctx;
  715. if (!handle) {
  716. pr_err("Invalid handle\n");
  717. ret = -EINVAL;
  718. goto error;
  719. }
  720. handle->app_data.request.length = req_len;
  721. ret = hdcp2p2_query_stream_type(
  722. handle->hdcp2_app_obj, handle->tz_ctxhandle, resMsg,
  723. MAX_TX_MESSAGE_SIZE, &resMsgLenOut, &timeout);
  724. if (ret) {
  725. pr_err("hdcp2p2_query_stream_type failed :%d\n", ret);
  726. goto error;
  727. }
  728. memcpy(handle->app_data.response.data, resMsg, resMsgLenOut);
  729. handle->app_data.response.length = resMsgLenOut;
  730. handle->app_data.timeout = timeout;
  731. error:
  732. return ret;
  733. }
  734. int hdcp2_app_stop_smcinvoke(void *ctx)
  735. {
  736. struct hdcp2_smcinvoke_handle *handle = NULL;
  737. int ret = 0;
  738. handle = ctx;
  739. if (!handle) {
  740. pr_err("Invalid handle\n");
  741. ret = -EINVAL;
  742. goto end;
  743. }
  744. ret = hdcp2_app_tx_deinit(handle);
  745. if (ret)
  746. goto end;
  747. ret = hdcp2_app_session_deinit(handle);
  748. if (ret)
  749. goto end;
  750. ret = hdcp2_app_unload(handle);
  751. kfree(handle->app_data.request.data);
  752. kfree(handle->app_data.response.data);
  753. end:
  754. return ret;
  755. }
  756. bool hdcp2_feature_supported_smcinvoke(void *ctx)
  757. {
  758. struct hdcp2_smcinvoke_handle *handle = NULL;
  759. int ret = 0;
  760. bool supported = false;
  761. handle = ctx;
  762. if (!handle) {
  763. pr_err("invalid input\n");
  764. ret = -EINVAL;
  765. goto error;
  766. }
  767. if (handle->feature_supported) {
  768. supported = true;
  769. goto error;
  770. }
  771. ret = hdcp2_app_load(handle);
  772. if (!ret) {
  773. if (!hdcp2_verify_key(handle)) {
  774. pr_debug("HDCP 2.2 supported\n");
  775. handle->feature_supported = true;
  776. supported = true;
  777. }
  778. hdcp2_app_unload(handle);
  779. }
  780. error:
  781. return supported;
  782. }
  783. int hdcp2_force_encryption_smcinvoke(void *ctx, uint32_t enable)
  784. {
  785. struct hdcp2_smcinvoke_handle *handle = NULL;
  786. int ret = 0;
  787. handle = ctx;
  788. if (!handle) {
  789. pr_err("Invalid handle\n");
  790. ret = -EINVAL;
  791. goto error;
  792. }
  793. if (handle->hdcp_state == HDCP_STATE_AUTHENTICATED)
  794. msleep(SLEEP_FORCE_ENCRYPTION_MS);
  795. ret = hdcp2p2_force_encryption(handle->hdcp2_app_obj, handle->tz_ctxhandle,
  796. enable);
  797. if (ret) {
  798. pr_err("hdcp2p2_force_encryption failed :%d\n", ret);
  799. goto error;
  800. }
  801. error:
  802. return ret;
  803. }
  804. int hdcp2_open_stream_smcinvoke(void *ctx, uint8_t vc_payload_id,
  805. uint8_t stream_number, uint32_t *stream_id)
  806. {
  807. struct hdcp2_smcinvoke_handle *handle = NULL;
  808. int ret = 0;
  809. uint32_t streamid = 0;
  810. handle = ctx;
  811. if (!handle) {
  812. pr_err("Invalid handle\n");
  813. ret = -EINVAL;
  814. goto error;
  815. }
  816. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  817. pr_err("session not initialized\n");
  818. ret = -EINVAL;
  819. goto error;
  820. }
  821. if (!(handle->hdcp_state & HDCP_STATE_TXMTR_INIT)) {
  822. pr_err("txmtr not initialized\n");
  823. ret = -EINVAL;
  824. goto error;
  825. }
  826. ret = hdcp2p2_session_open_stream(handle->hdcp2_app_obj,
  827. handle->session_id, vc_payload_id,
  828. stream_number, 0, &streamid);
  829. if (ret) {
  830. pr_err("hdcp2p2_session_open_stream failed :%d\n", ret);
  831. goto error;
  832. }
  833. *stream_id = streamid;
  834. error:
  835. return ret;
  836. }
  837. int hdcp2_close_stream_smcinvoke(void *ctx, uint32_t stream_id)
  838. {
  839. struct hdcp2_smcinvoke_handle *handle = NULL;
  840. int ret = 0;
  841. handle = ctx;
  842. if (!handle) {
  843. pr_err("Invalid handle\n");
  844. ret = -EINVAL;
  845. goto error;
  846. }
  847. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  848. pr_err("session not initialized\n");
  849. ret = -EINVAL;
  850. goto error;
  851. }
  852. if (!(handle->hdcp_state & HDCP_STATE_TXMTR_INIT)) {
  853. pr_err("txmtr not initialized\n");
  854. ret = -EINVAL;
  855. goto error;
  856. }
  857. ret = hdcp2p2_session_close_stream(handle->hdcp2_app_obj,
  858. handle->session_id, stream_id);
  859. if (ret) {
  860. pr_err("hdcp2p2_session_close_stream failed :%d\n", ret);
  861. goto error;
  862. }
  863. error:
  864. return ret;
  865. }
  866. int hdcp2_update_app_data_smcinvoke(void *ctx, struct hdcp2_app_data *app_data)
  867. {
  868. struct hdcp2_smcinvoke_handle *handle = NULL;
  869. int ret = 0;
  870. handle = ctx;
  871. if (!handle || !app_data) {
  872. pr_err("Invalid handle\n");
  873. return -EINVAL;
  874. }
  875. app_data->request.data = handle->app_data.request.data;
  876. app_data->request.length = handle->app_data.request.length;
  877. app_data->response.data = handle->app_data.response.data;
  878. app_data->response.length = handle->app_data.response.length;
  879. app_data->timeout = handle->app_data.timeout;
  880. app_data->repeater_flag = handle->app_data.repeater_flag;
  881. return ret;
  882. }