hdcp_smcinvoke.c 23 KB

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