hdcp_qseecom.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2022, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[hdcp-qseecom] %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/io.h>
  15. #include <linux/types.h>
  16. #include <linux/device.h>
  17. #include <linux/sched.h>
  18. #include <linux/delay.h>
  19. #include <linux/completion.h>
  20. #include <linux/errno.h>
  21. #include <linux/kthread.h>
  22. #include "linux/hdcp_qseecom.h"
  23. #include "misc/qseecom_kernel.h"
  24. #define HDCP2P2_APP_NAME "hdcp2p2"
  25. #define HDCP1_APP_NAME "hdcp1"
  26. #define HDCP1OPS_APP_NAME "ops"
  27. #define HDCPSRM_APP_NAME "hdcpsrm"
  28. #define QSEECOM_SBUFF_SIZE 0x1000
  29. #define MAX_REC_ID_LIST_SIZE 160
  30. #define MAX_TX_MESSAGE_SIZE 129
  31. #define MAX_RX_MESSAGE_SIZE 534
  32. #define MAX_TOPOLOGY_ELEMS 32
  33. #define HDCP1_NOTIFY_TOPOLOGY 1
  34. #define HDCP1_AKSV_SIZE 8
  35. #define HDCP1_SET_KEY 202
  36. #define HDCP1_KEY_VERIFY 204
  37. #define HDCP1_SET_ENC 205
  38. #define BITS_8_IN_BYTES 1
  39. #define BITS_16_IN_BYTES 2
  40. #define BITS_24_IN_BYTES 3
  41. #define BITS_32_IN_BYTES 4
  42. #define BITS_40_IN_BYTES 5
  43. #define BITS_64_IN_BYTES 8
  44. #define BITS_128_IN_BYTES 16
  45. #define BITS_160_IN_BYTES 20
  46. #define BITS_256_IN_BYTES 32
  47. #define BITS_1024_IN_BYTES 128
  48. #define BITS_3072_IN_BYTES 384
  49. #define TXCAPS_SIZE 3
  50. #define RXCAPS_SIZE 3
  51. #define RXINFO_SIZE 2
  52. #define SEQ_NUM_V_SIZE 3
  53. #define RCVR_ID_SIZE BITS_40_IN_BYTES
  54. #define MAX_RCVR_IDS_ALLOWED_IN_LIST 31
  55. #define MAX_RCVR_ID_LIST_SIZE \
  56. (RCVR_ID_SIZE*MAX_RCVR_IDS_ALLOWED_IN_LIST)
  57. /*
  58. * minimum wait as per standard is 200 ms. keep it 300 ms
  59. * to be on safe side.
  60. */
  61. #define SLEEP_SET_HW_KEY_MS 300
  62. /* Wait 200ms after authentication */
  63. #define SLEEP_FORCE_ENCRYPTION_MS 200
  64. /* hdcp command status */
  65. #define HDCP_SUCCESS 0
  66. /* DP device type */
  67. #define DEVICE_TYPE_DP 0x8002
  68. const char *HdcpErrors[] = {
  69. "HDCP_SUCCESS",
  70. "HDCP_FAIL",
  71. "HDCP_BAD_PARAM",
  72. "HDCP_DEVICE_TYPE_UNSUPPORTED",
  73. "HDCP_INVALID_COMMAND",
  74. "HDCP_INVALID_COMMAND_HANDLE",
  75. "HDCP_ERROR_SIZE_IN",
  76. "HDCP_ERROR_SIZE_OUT",
  77. "HDCP_DATA_SIZE_INSUFFICIENT",
  78. "HDCP_UNSUPPORTED_RX_VERSION",
  79. "HDCP_WRONG_RX_CAPAB_MASK",
  80. "HDCP_WRONG_RX_RSVD",
  81. "HDCP_WRONG_RX_HDCP_CAPABLE",
  82. "HDCP_RSA_SIGNATURE_VERIFY_FAILED",
  83. "HDCP_VERIFY_H_PRIME_FAILED",
  84. "HDCP_LC_FAILED",
  85. "HDCP_MESSAGE_TIMEOUT",
  86. "HDCP_COUNTER_ROLL_OVER",
  87. "HDCP_WRONG_RXINFO_RSVD",
  88. "HDCP_RXINFO_MAX_DEVS",
  89. "HDCP_RXINFO_MAX_CASCADE",
  90. "HDCP_WRONG_INITIAL_SEQ_NUM_V",
  91. "HDCP_SEQ_NUM_V_ROLL_OVER",
  92. "HDCP_WRONG_SEQ_NUM_V",
  93. "HDCP_VERIFY_V_FAILED",
  94. "HDCP_RPT_METHOD_INVOKED",
  95. "HDCP_RPT_STRM_LEN_WRONG",
  96. "HDCP_VERIFY_STRM_M_FAILED",
  97. "HDCP_TRANSMITTER_NOT_FOUND",
  98. "HDCP_SESSION_NOT_FOUND",
  99. "HDCP_MAX_SESSION_EXCEEDED",
  100. "HDCP_MAX_CONNECTION_EXCEEDED",
  101. "HDCP_MAX_STREAMS_EXCEEDED",
  102. "HDCP_MAX_DEVICES",
  103. "HDCP_ALLOC_FAILED",
  104. "HDCP_CONNECTION_NOT_FOUND",
  105. "HDCP_HASH_FAILED",
  106. "HDCP_BN_FAILED",
  107. "HDCP_ENCRYPT_KM_FAILED",
  108. "HDCP_DECRYPT_KM_FAILED",
  109. "HDCP_HMAC_FAILED",
  110. "HDCP_GET_RANDOM_FAILED",
  111. "HDCP_INVALID_KEY_HEADER",
  112. "HDCP_INVALID_KEY_LC_HASH",
  113. "HDCP_INVALID_KEY_HASH",
  114. "HDCP_KEY_WRITE_FAILED",
  115. "HDCP_KEY_READ_FAILED",
  116. "HDCP_KEY_DECRYPT_FAILED",
  117. "HDCP_TEST_KEY_ON_SECURE_DEVICE",
  118. "HDCP_KEY_VERSION_UNSUPPORTED",
  119. "HDCP_RXID_NOT_FOUND",
  120. "HDCP_STORAGE_INIT_FAILED",
  121. "HDCP_STORAGE_FILE_OPEN_FAILED",
  122. "HDCP_STORAGE_FILE_READ_FAILED",
  123. "HDCP_STORAGE_FILE_WRITE_FAILED",
  124. "HDCP_STORAGE_ID_UNSUPPORTED",
  125. "HDCP_MUTUAL_EXCLUSIVE_DEVICE_PRESENT",
  126. "HDCP_INVALID_STATE",
  127. "HDCP_CONFIG_READ_FAILED",
  128. "HDCP_OPEN_TZ_SERVICE_FAILED",
  129. "HDCP_HW_CLOCK_OFF",
  130. "HDCP_SET_HW_KEY_FAILED",
  131. "HDCP_CLEAR_HW_KEY_FAILED",
  132. "HDCP_GET_CONTENT_LEVEL_FAILED",
  133. "HDCP_STREAMID_INUSE",
  134. "HDCP_STREAM_NOT_FOUND",
  135. "HDCP_FORCE_ENCRYPTION_FAILED",
  136. "HDCP_STREAMNUMBER_INUSE"
  137. };
  138. /* flags set by tz in response message */
  139. #define HDCP_TXMTR_SUBSTATE_INIT 0
  140. #define HDCP_TXMTR_SUBSTATE_WAITING_FOR_RECIEVERID_LIST 1
  141. #define HDCP_TXMTR_SUBSTATE_PROCESSED_RECIEVERID_LIST 2
  142. #define HDCP_TXMTR_SUBSTATE_WAITING_FOR_STREAM_READY_MESSAGE 3
  143. #define HDCP_TXMTR_SUBSTATE_REPEATER_AUTH_COMPLETE 4
  144. #define HDCP_DEVICE_ID 0x0008000
  145. #define HDCP_CREATE_DEVICE_ID(x) (HDCP_DEVICE_ID | (x))
  146. #define HDCP_TXMTR_HDMI HDCP_CREATE_DEVICE_ID(1)
  147. #define HDCP_TXMTR_SERVICE_ID 0x0001000
  148. #define SERVICE_CREATE_CMD(x) (HDCP_TXMTR_SERVICE_ID | x)
  149. #define HCDP_TXMTR_GET_MAJOR_VERSION(v) (((v) >> 16) & 0xFF)
  150. #define HCDP_TXMTR_GET_MINOR_VERSION(v) (((v) >> 8) & 0xFF)
  151. #define HCDP_TXMTR_GET_PATCH_VERSION(v) ((v) & 0xFF)
  152. #define HDCP_CLIENT_MAJOR_VERSION 2
  153. #define HDCP_CLIENT_MINOR_VERSION 1
  154. #define HDCP_CLIENT_PATCH_VERSION 0
  155. #define HDCP_CLIENT_MAKE_VERSION(maj, min, patch) \
  156. ((((maj) & 0xFF) << 16) | (((min) & 0xFF) << 8) | ((patch) & 0xFF))
  157. #define hdcp2_app_init_var(x) \
  158. struct hdcp_##x##_req *req_buf = NULL; \
  159. struct hdcp_##x##_rsp *rsp_buf = NULL; \
  160. if (!handle->qseecom_handle) { \
  161. pr_err("invalid qseecom_handle while processing %s\n", #x); \
  162. rc = -EINVAL; \
  163. goto error; \
  164. } \
  165. req_buf = (struct hdcp_##x##_req *) handle->qseecom_handle->sbuf; \
  166. rsp_buf = (struct hdcp_##x##_rsp *) (handle->qseecom_handle->sbuf + \
  167. QSEECOM_ALIGN(sizeof(struct hdcp_##x##_req))); \
  168. req_buf->commandid = hdcp_cmd_##x
  169. #define hdcp2_app_process_cmd(x) \
  170. ({ \
  171. int rc = qseecom_send_command(handle->qseecom_handle, \
  172. req_buf, QSEECOM_ALIGN(sizeof(struct hdcp_##x##_req)), \
  173. rsp_buf, QSEECOM_ALIGN(sizeof(struct hdcp_##x##_rsp))); \
  174. if ((rc < 0) || (rsp_buf->status != HDCP_SUCCESS)) { \
  175. pr_err("qseecom cmd %s failed with err = %d, status = %d:%s\n" \
  176. , #x, rc, rsp_buf->status, \
  177. hdcp_cmd_status_to_str(rsp_buf->status)); \
  178. rc = -EINVAL; \
  179. } \
  180. rc; \
  181. })
  182. enum {
  183. hdcp_cmd_tx_init = SERVICE_CREATE_CMD(1),
  184. hdcp_cmd_tx_init_v1 = SERVICE_CREATE_CMD(1),
  185. hdcp_cmd_tx_deinit = SERVICE_CREATE_CMD(2),
  186. hdcp_cmd_rcvd_msg = SERVICE_CREATE_CMD(3),
  187. hdcp_cmd_send_timeout = SERVICE_CREATE_CMD(4),
  188. hdcp_cmd_set_hw_key = SERVICE_CREATE_CMD(5),
  189. hdcp_cmd_query_stream_type = SERVICE_CREATE_CMD(6),
  190. hdcp_cmd_init_v1 = SERVICE_CREATE_CMD(11),
  191. hdcp_cmd_init = SERVICE_CREATE_CMD(11),
  192. hdcp_cmd_deinit = SERVICE_CREATE_CMD(12),
  193. hdcp_cmd_version = SERVICE_CREATE_CMD(14),
  194. hdcp_cmd_verify_key = SERVICE_CREATE_CMD(15),
  195. hdcp_cmd_session_init = SERVICE_CREATE_CMD(16),
  196. hdcp_cmd_session_deinit = SERVICE_CREATE_CMD(17),
  197. hdcp_cmd_start_auth = SERVICE_CREATE_CMD(18),
  198. hdcp_cmd_session_open_stream = SERVICE_CREATE_CMD(20),
  199. hdcp_cmd_session_close_stream = SERVICE_CREATE_CMD(21),
  200. hdcp_cmd_force_encryption = SERVICE_CREATE_CMD(22),
  201. };
  202. enum hdcp_state {
  203. HDCP_STATE_INIT = 0x00,
  204. HDCP_STATE_APP_LOADED = 0x01,
  205. HDCP_STATE_SESSION_INIT = 0x02,
  206. HDCP_STATE_TXMTR_INIT = 0x04,
  207. HDCP_STATE_AUTHENTICATED = 0x08,
  208. HDCP_STATE_ERROR = 0x10
  209. };
  210. enum hdcp_element {
  211. HDCP_TYPE_UNKNOWN,
  212. HDCP_TYPE_RECEIVER,
  213. HDCP_TYPE_REPEATER,
  214. };
  215. enum hdcp_version {
  216. HDCP_VERSION_UNKNOWN,
  217. HDCP_VERSION_2_2,
  218. HDCP_VERSION_1_4
  219. };
  220. struct receiver_info {
  221. unsigned char rcvrInfo[RCVR_ID_SIZE];
  222. enum hdcp_element elem_type;
  223. enum hdcp_version hdcp_version;
  224. };
  225. struct topology_info {
  226. unsigned int nNumRcvrs;
  227. struct receiver_info rcvinfo[MAX_TOPOLOGY_ELEMS];
  228. };
  229. struct __attribute__ ((__packed__)) hdcp1_key_set_req {
  230. uint32_t commandid;
  231. };
  232. struct __attribute__ ((__packed__)) hdcp1_key_set_rsp {
  233. uint32_t commandid;
  234. uint32_t ret;
  235. uint8_t ksv[HDCP1_AKSV_SIZE];
  236. };
  237. struct __attribute__ ((__packed__)) hdcp_version_req {
  238. uint32_t commandid;
  239. };
  240. struct __attribute__ ((__packed__)) hdcp_version_rsp {
  241. uint32_t status;
  242. uint32_t commandId;
  243. uint32_t appversion;
  244. };
  245. struct __attribute__ ((__packed__)) hdcp_verify_key_req {
  246. uint32_t commandid;
  247. };
  248. struct __attribute__ ((__packed__)) hdcp_verify_key_rsp {
  249. uint32_t status;
  250. uint32_t commandId;
  251. };
  252. struct __attribute__ ((__packed__)) hdcp1_key_verify_req {
  253. uint32_t commandid;
  254. uint32_t key_type;
  255. };
  256. struct __attribute__ ((__packed__)) hdcp1_key_verify_rsp {
  257. uint32_t commandId;
  258. uint32_t ret;
  259. };
  260. struct __attribute__ ((__packed__)) hdcp_init_v1_req {
  261. uint32_t commandid;
  262. };
  263. struct __attribute__ ((__packed__)) hdcp_init_v1_rsp {
  264. uint32_t status;
  265. uint32_t commandid;
  266. uint32_t ctxhandle;
  267. uint32_t timeout;
  268. uint32_t msglen;
  269. uint8_t message[MAX_TX_MESSAGE_SIZE];
  270. };
  271. struct __attribute__ ((__packed__)) hdcp_init_req {
  272. uint32_t commandid;
  273. uint32_t clientversion;
  274. };
  275. struct __attribute__ ((__packed__)) hdcp_init_rsp {
  276. uint32_t status;
  277. uint32_t commandid;
  278. uint32_t appversion;
  279. };
  280. struct __attribute__ ((__packed__)) hdcp_deinit_req {
  281. uint32_t commandid;
  282. };
  283. struct __attribute__ ((__packed__)) hdcp_deinit_rsp {
  284. uint32_t status;
  285. uint32_t commandid;
  286. };
  287. struct __attribute__ ((__packed__)) hdcp_session_init_req {
  288. uint32_t commandid;
  289. uint32_t deviceid;
  290. };
  291. struct __attribute__ ((__packed__)) hdcp_session_init_rsp {
  292. uint32_t status;
  293. uint32_t commandid;
  294. uint32_t sessionid;
  295. };
  296. struct __attribute__ ((__packed__)) hdcp_session_deinit_req {
  297. uint32_t commandid;
  298. uint32_t sessionid;
  299. };
  300. struct __attribute__ ((__packed__)) hdcp_session_deinit_rsp {
  301. uint32_t status;
  302. uint32_t commandid;
  303. };
  304. struct __attribute__ ((__packed__)) hdcp_tx_init_v1_req {
  305. uint32_t commandid;
  306. };
  307. struct __attribute__ ((__packed__)) hdcp_tx_init_v1_rsp {
  308. uint32_t status;
  309. uint32_t commandid;
  310. uint32_t ctxhandle;
  311. uint32_t timeout;
  312. uint32_t msglen;
  313. uint8_t message[MAX_TX_MESSAGE_SIZE];
  314. };
  315. struct __attribute__ ((__packed__)) hdcp_tx_init_req {
  316. uint32_t commandid;
  317. uint32_t sessionid;
  318. };
  319. struct __attribute__ ((__packed__)) hdcp_tx_init_rsp {
  320. uint32_t status;
  321. uint32_t commandid;
  322. uint32_t ctxhandle;
  323. };
  324. struct __attribute__ ((__packed__)) hdcp_tx_deinit_req {
  325. uint32_t commandid;
  326. uint32_t ctxhandle;
  327. };
  328. struct __attribute__ ((__packed__)) hdcp_tx_deinit_rsp {
  329. uint32_t status;
  330. uint32_t commandid;
  331. };
  332. struct __attribute__ ((__packed__)) hdcp_rcvd_msg_req {
  333. uint32_t commandid;
  334. uint32_t ctxhandle;
  335. uint32_t msglen;
  336. uint8_t msg[MAX_RX_MESSAGE_SIZE];
  337. };
  338. struct __attribute__ ((__packed__)) hdcp_rcvd_msg_rsp {
  339. uint32_t status;
  340. uint32_t commandid;
  341. uint32_t state;
  342. uint32_t timeout;
  343. uint32_t flag;
  344. uint32_t msglen;
  345. uint8_t msg[MAX_TX_MESSAGE_SIZE];
  346. };
  347. struct __attribute__ ((__packed__)) hdcp_set_hw_key_req {
  348. uint32_t commandid;
  349. uint32_t ctxhandle;
  350. };
  351. struct __attribute__ ((__packed__)) hdcp_set_hw_key_rsp {
  352. uint32_t status;
  353. uint32_t commandid;
  354. };
  355. struct __attribute__ ((__packed__)) hdcp_send_timeout_req {
  356. uint32_t commandid;
  357. uint32_t ctxhandle;
  358. };
  359. struct __attribute__ ((__packed__)) hdcp_send_timeout_rsp {
  360. uint32_t status;
  361. uint32_t commandid;
  362. uint32_t timeout;
  363. uint32_t msglen;
  364. uint8_t message[MAX_TX_MESSAGE_SIZE];
  365. };
  366. struct __attribute__ ((__packed__)) hdcp_query_stream_type_req {
  367. uint32_t commandid;
  368. uint32_t ctxhandle;
  369. };
  370. struct __attribute__ ((__packed__)) hdcp_query_stream_type_rsp {
  371. uint32_t status;
  372. uint32_t commandid;
  373. uint32_t timeout;
  374. uint32_t msglen;
  375. uint8_t msg[MAX_TX_MESSAGE_SIZE];
  376. };
  377. struct __attribute__ ((__packed__)) hdcp_set_stream_type_req {
  378. uint32_t commandid;
  379. uint32_t ctxhandle;
  380. uint8_t streamtype;
  381. };
  382. struct __attribute__ ((__packed__)) hdcp_set_stream_type_rsp {
  383. uint32_t status;
  384. uint32_t commandid;
  385. uint32_t timeout;
  386. uint32_t msglen;
  387. uint8_t message[MAX_TX_MESSAGE_SIZE];
  388. };
  389. struct __attribute__ ((__packed__)) hdcp_update_srm_req {
  390. uint32_t commandid;
  391. uint32_t ctxhandle;
  392. uint32_t srmoffset;
  393. uint32_t srmlength;
  394. };
  395. struct __attribute__ ((__packed__)) hdcp_update_srm_rsp {
  396. uint32_t status;
  397. uint32_t commandid;
  398. };
  399. struct __attribute__ ((__packed__)) hdcp_get_topology_req {
  400. uint32_t commandid;
  401. uint32_t ctxhandle;
  402. };
  403. struct __attribute__ ((__packed__)) hdcp_get_topology_rsp {
  404. uint32_t status;
  405. uint32_t commandid;
  406. struct topology_info topologyinfo;
  407. };
  408. struct __attribute__ ((__packed__)) rxvr_info_struct {
  409. uint8_t rcvrCert[522];
  410. uint8_t rrx[BITS_64_IN_BYTES];
  411. uint8_t rxcaps[RXCAPS_SIZE];
  412. bool repeater;
  413. };
  414. struct __attribute__ ((__packed__)) repeater_info_struct {
  415. uint8_t RxInfo[RXINFO_SIZE];
  416. uint8_t seq_num_V[SEQ_NUM_V_SIZE];
  417. bool seq_num_V_Rollover_flag;
  418. uint8_t ReceiverIDList[MAX_RCVR_ID_LIST_SIZE];
  419. uint32_t ReceiverIDListLen;
  420. };
  421. struct __attribute__ ((__packed__)) hdcp1_set_enc_req {
  422. uint32_t commandid;
  423. uint32_t enable;
  424. };
  425. struct __attribute__ ((__packed__)) hdcp1_set_enc_rsp {
  426. uint32_t commandid;
  427. uint32_t ret;
  428. };
  429. struct __attribute__ ((__packed__)) hdcp1_ops_notify_req {
  430. uint32_t commandid;
  431. uint32_t device_type;
  432. uint8_t recv_id_list[MAX_REC_ID_LIST_SIZE];
  433. int32_t recv_id_len;
  434. struct hdcp1_topology topology;
  435. bool is_authenticated;
  436. };
  437. struct __attribute__ ((__packed__)) hdcp1_ops_notify_rsp {
  438. uint32_t commandid;
  439. uint32_t ret;
  440. };
  441. struct __attribute__ ((__packed__)) hdcp_start_auth_req {
  442. uint32_t commandid;
  443. uint32_t ctxHandle;
  444. };
  445. struct __attribute__ ((__packed__)) hdcp_start_auth_rsp {
  446. uint32_t status;
  447. uint32_t commandid;
  448. uint32_t ctxhandle;
  449. uint32_t timeout;
  450. uint32_t msglen;
  451. uint8_t message[MAX_TX_MESSAGE_SIZE];
  452. };
  453. struct __attribute__((__packed__)) hdcp_session_open_stream_req {
  454. uint32_t commandid;
  455. uint32_t sessionid;
  456. uint32_t vcpayloadid;
  457. uint32_t stream_number;
  458. uint32_t streamMediaType;
  459. };
  460. struct __attribute__((__packed__)) hdcp_session_open_stream_rsp {
  461. uint32_t status;
  462. uint32_t commandid;
  463. uint32_t streamid;
  464. };
  465. struct __attribute__((__packed__)) hdcp_session_close_stream_req {
  466. uint32_t commandid;
  467. uint32_t sessionid;
  468. uint32_t streamid;
  469. };
  470. struct __attribute__((__packed__)) hdcp_session_close_stream_rsp {
  471. uint32_t status;
  472. uint32_t commandid;
  473. };
  474. struct __attribute__((__packed__)) hdcp_force_encryption_req {
  475. uint32_t commandid;
  476. uint32_t ctxhandle;
  477. uint32_t enable;
  478. };
  479. struct __attribute__ ((__packed__)) hdcp_force_encryption_rsp {
  480. uint32_t status;
  481. uint32_t commandid;
  482. };
  483. struct hdcp2_handle {
  484. struct hdcp2_app_data app_data;
  485. uint32_t tz_ctxhandle;
  486. bool feature_supported;
  487. enum hdcp_state hdcp_state;
  488. struct qseecom_handle *qseecom_handle;
  489. struct qseecom_handle *hdcpsrm_qseecom_handle;
  490. uint32_t session_id;
  491. bool legacy_app;
  492. uint32_t device_type;
  493. char *app_name;
  494. int (*app_init)(struct hdcp2_handle *handle);
  495. int (*tx_init)(struct hdcp2_handle *handle);
  496. };
  497. /*
  498. * struct hdcp1_handle - handle for HDCP 1.x client
  499. * @qseecom_handle - for sending commands to qseecom
  500. * @hdcpops_handle - for sending commands to ops TA
  501. * @feature_supported - set to true if the platform supports HDCP 1.x
  502. * @device_type - the interface type (HDMI or DisplayPort)
  503. */
  504. struct hdcp1_handle {
  505. struct qseecom_handle *qseecom_handle;
  506. struct qseecom_handle *hdcpops_handle;
  507. bool feature_supported;
  508. uint32_t device_type;
  509. enum hdcp_state hdcp_state;
  510. char *app_name;
  511. };
  512. #define HDCP_CMD_STATUS_TO_STR(x) #x
  513. static const char *hdcp_cmd_status_to_str(uint32_t err)
  514. {
  515. int len = ARRAY_SIZE(HdcpErrors);
  516. if (err >= 0 && err < len)
  517. return HdcpErrors[err];
  518. else
  519. return "";
  520. }
  521. static int hdcp_get_version(struct hdcp2_handle *handle)
  522. {
  523. int rc = 0;
  524. uint32_t app_major_version = 0;
  525. hdcp2_app_init_var(version);
  526. if (handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  527. pr_err("library already loaded\n");
  528. goto error;
  529. }
  530. rc = hdcp2_app_process_cmd(version);
  531. if (rc)
  532. goto error;
  533. app_major_version = HCDP_TXMTR_GET_MAJOR_VERSION(rsp_buf->appversion);
  534. pr_debug("hdp2p2 app major version %d, app version %d\n",
  535. app_major_version, rsp_buf->appversion);
  536. if (app_major_version == 1)
  537. handle->legacy_app = true;
  538. error:
  539. return rc;
  540. }
  541. static int hdcp2_app_init_legacy(struct hdcp2_handle *handle)
  542. {
  543. int rc = 0;
  544. hdcp2_app_init_var(init_v1);
  545. if (!handle->legacy_app) {
  546. pr_err("wrong init function\n");
  547. rc = -EINVAL;
  548. goto error;
  549. }
  550. if (handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  551. pr_err("library already loaded\n");
  552. goto error;
  553. }
  554. rc = hdcp2_app_process_cmd(init_v1);
  555. if (rc)
  556. goto error;
  557. pr_debug("success\n");
  558. error:
  559. return rc;
  560. }
  561. static int hdcp2_app_init(struct hdcp2_handle *handle)
  562. {
  563. int rc = 0;
  564. uint32_t app_minor_version = 0;
  565. hdcp2_app_init_var(init);
  566. if (handle->legacy_app) {
  567. pr_err("wrong init function\n");
  568. rc = -EINVAL;
  569. goto error;
  570. }
  571. if (handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  572. pr_err("library already loaded\n");
  573. goto error;
  574. }
  575. req_buf->clientversion =
  576. HDCP_CLIENT_MAKE_VERSION(HDCP_CLIENT_MAJOR_VERSION,
  577. HDCP_CLIENT_MINOR_VERSION,
  578. HDCP_CLIENT_PATCH_VERSION);
  579. rc = hdcp2_app_process_cmd(init);
  580. if (rc)
  581. goto error;
  582. app_minor_version = HCDP_TXMTR_GET_MINOR_VERSION(rsp_buf->appversion);
  583. if (app_minor_version != HDCP_CLIENT_MINOR_VERSION) {
  584. pr_err
  585. ("client-app minor version mismatch app(%d), client(%d)\n",
  586. app_minor_version, HDCP_CLIENT_MINOR_VERSION);
  587. rc = -1;
  588. goto error;
  589. }
  590. pr_debug("success\n");
  591. pr_debug("client version major(%d), minor(%d), patch(%d)\n",
  592. HDCP_CLIENT_MAJOR_VERSION, HDCP_CLIENT_MINOR_VERSION,
  593. HDCP_CLIENT_PATCH_VERSION);
  594. pr_debug("app version major(%d), minor(%d), patch(%d)\n",
  595. HCDP_TXMTR_GET_MAJOR_VERSION(rsp_buf->appversion),
  596. HCDP_TXMTR_GET_MINOR_VERSION(rsp_buf->appversion),
  597. HCDP_TXMTR_GET_PATCH_VERSION(rsp_buf->appversion));
  598. error:
  599. return rc;
  600. }
  601. static int hdcp2_app_tx_init(struct hdcp2_handle *handle)
  602. {
  603. int rc = 0;
  604. hdcp2_app_init_var(tx_init);
  605. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  606. pr_err("session not initialized\n");
  607. rc = -EINVAL;
  608. goto error;
  609. }
  610. if (handle->hdcp_state & HDCP_STATE_TXMTR_INIT) {
  611. pr_err("txmtr already initialized\n");
  612. goto error;
  613. }
  614. req_buf->sessionid = handle->session_id;
  615. rc = hdcp2_app_process_cmd(tx_init);
  616. if (rc)
  617. goto error;
  618. handle->tz_ctxhandle = rsp_buf->ctxhandle;
  619. handle->hdcp_state |= HDCP_STATE_TXMTR_INIT;
  620. pr_debug("success\n");
  621. error:
  622. return rc;
  623. }
  624. static int hdcp2_app_tx_init_legacy(struct hdcp2_handle *handle)
  625. {
  626. int rc = 0;
  627. hdcp2_app_init_var(tx_init_v1);
  628. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  629. pr_err("app not loaded\n");
  630. rc = -EINVAL;
  631. goto error;
  632. }
  633. if (handle->hdcp_state & HDCP_STATE_TXMTR_INIT) {
  634. pr_err("txmtr already initialized\n");
  635. goto error;
  636. }
  637. rc = hdcp2_app_process_cmd(tx_init_v1);
  638. if (rc)
  639. goto error;
  640. handle->app_data.response.data = rsp_buf->message;
  641. handle->app_data.response.length = rsp_buf->msglen;
  642. handle->app_data.timeout = rsp_buf->timeout;
  643. handle->tz_ctxhandle = rsp_buf->ctxhandle;
  644. handle->hdcp_state |= HDCP_STATE_TXMTR_INIT;
  645. pr_debug("success\n");
  646. error:
  647. return rc;
  648. }
  649. static int hdcp2_app_load(struct hdcp2_handle *handle)
  650. {
  651. int rc = 0;
  652. if (!handle) {
  653. pr_err("invalid input\n");
  654. rc = -EINVAL;
  655. goto error;
  656. }
  657. if (handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  658. pr_err("%s app already loaded\n", handle->app_name);
  659. goto error;
  660. }
  661. rc = qseecom_start_app(&handle->qseecom_handle,
  662. handle->app_name, QSEECOM_SBUFF_SIZE);
  663. if (rc) {
  664. pr_err("qseecom_start_app failed for HDCP2P2 (%d)\n", rc);
  665. goto error;
  666. }
  667. rc = qseecom_start_app(&handle->hdcpsrm_qseecom_handle,
  668. HDCPSRM_APP_NAME, QSEECOM_SBUFF_SIZE);
  669. if (rc) {
  670. pr_err("qseecom_start_app failed for HDCPSRM (%d)\n", rc);
  671. goto hdcpsrm_error;
  672. }
  673. pr_debug("qseecom_start_app success\n");
  674. rc = hdcp_get_version(handle);
  675. if (rc) {
  676. pr_err("library get version failed\n");
  677. goto get_version_error;
  678. }
  679. if (handle->legacy_app) {
  680. handle->app_init = hdcp2_app_init_legacy;
  681. handle->tx_init = hdcp2_app_tx_init_legacy;
  682. } else {
  683. handle->app_init = hdcp2_app_init;
  684. handle->tx_init = hdcp2_app_tx_init;
  685. }
  686. rc = handle->app_init(handle);
  687. if (rc) {
  688. pr_err("app init failed\n");
  689. goto get_version_error;
  690. }
  691. handle->hdcp_state |= HDCP_STATE_APP_LOADED;
  692. return rc;
  693. get_version_error:
  694. qseecom_shutdown_app(&handle->hdcpsrm_qseecom_handle);
  695. hdcpsrm_error:
  696. qseecom_shutdown_app(&handle->qseecom_handle);
  697. error:
  698. return rc;
  699. }
  700. static int hdcp2_app_unload(struct hdcp2_handle *handle)
  701. {
  702. int rc = 0;
  703. hdcp2_app_init_var(deinit);
  704. hdcp2_app_process_cmd(deinit);
  705. /* deallocate the resources for qseecom HDCPSRM handle */
  706. rc = qseecom_shutdown_app(&handle->hdcpsrm_qseecom_handle);
  707. if (rc)
  708. pr_err("qseecom_shutdown_app failed for HDCPSRM (%d)\n", rc);
  709. /* deallocate the resources for qseecom HDCP2P2 handle */
  710. rc = qseecom_shutdown_app(&handle->qseecom_handle);
  711. if (rc) {
  712. pr_err("qseecom_shutdown_app failed for HDCP2P2 (%d)\n", rc);
  713. return rc;
  714. }
  715. handle->hdcp_state &= ~HDCP_STATE_APP_LOADED;
  716. pr_debug("%s app unloaded\n", handle->app_name);
  717. return rc;
  718. error:
  719. qseecom_shutdown_app(&handle->hdcpsrm_qseecom_handle);
  720. return rc;
  721. }
  722. static int hdcp2_verify_key(struct hdcp2_handle *handle)
  723. {
  724. int rc = 0;
  725. hdcp2_app_init_var(verify_key);
  726. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  727. pr_err("%s app not loaded\n", handle->app_name);
  728. rc = -EINVAL;
  729. goto error;
  730. }
  731. rc = hdcp2_app_process_cmd(verify_key);
  732. pr_debug("verify_key = %d\n", rc);
  733. error:
  734. return rc;
  735. }
  736. bool hdcp2_feature_supported(void *data)
  737. {
  738. int rc = 0;
  739. bool supported = false;
  740. struct hdcp2_handle *handle = data;
  741. if (!handle) {
  742. pr_err("invalid input\n");
  743. rc = -EINVAL;
  744. goto error;
  745. }
  746. if (handle->feature_supported) {
  747. supported = true;
  748. goto error;
  749. }
  750. rc = hdcp2_app_load(handle);
  751. if (!rc) {
  752. if (!hdcp2_verify_key(handle)) {
  753. pr_debug("HDCP 2.2 supported\n");
  754. handle->feature_supported = true;
  755. supported = true;
  756. }
  757. hdcp2_app_unload(handle);
  758. }
  759. error:
  760. return supported;
  761. }
  762. EXPORT_SYMBOL(hdcp2_feature_supported);
  763. static int hdcp2_app_session_init(struct hdcp2_handle *handle)
  764. {
  765. int rc = 0;
  766. hdcp2_app_init_var(session_init);
  767. if (!handle->qseecom_handle || !handle->qseecom_handle->sbuf) {
  768. pr_err("invalid handle\n");
  769. rc = -EINVAL;
  770. goto error;
  771. }
  772. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  773. pr_err("%s app not loaded\n", handle->app_name);
  774. rc = -EINVAL;
  775. goto error;
  776. }
  777. if (handle->hdcp_state & HDCP_STATE_SESSION_INIT) {
  778. pr_err("session already initialized\n");
  779. goto error;
  780. }
  781. req_buf->deviceid = handle->device_type;
  782. rc = hdcp2_app_process_cmd(session_init);
  783. if (rc)
  784. goto error;
  785. pr_debug("session id %d\n", rsp_buf->sessionid);
  786. handle->session_id = rsp_buf->sessionid;
  787. handle->hdcp_state |= HDCP_STATE_SESSION_INIT;
  788. pr_debug("success\n");
  789. error:
  790. return rc;
  791. }
  792. static int hdcp2_app_session_deinit(struct hdcp2_handle *handle)
  793. {
  794. int rc = 0;
  795. hdcp2_app_init_var(session_deinit);
  796. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  797. pr_err("%s app not loaded\n", handle->app_name);
  798. rc = -EINVAL;
  799. goto error;
  800. }
  801. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  802. pr_err("session not initialized\n");
  803. rc = -EINVAL;
  804. goto error;
  805. }
  806. req_buf->sessionid = handle->session_id;
  807. rc = hdcp2_app_process_cmd(session_deinit);
  808. if (rc)
  809. goto error;
  810. handle->hdcp_state &= ~HDCP_STATE_SESSION_INIT;
  811. pr_debug("success\n");
  812. error:
  813. return rc;
  814. }
  815. static int hdcp2_app_tx_deinit(struct hdcp2_handle *handle)
  816. {
  817. int rc = 0;
  818. hdcp2_app_init_var(tx_deinit);
  819. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  820. pr_err("%s app not loaded\n", handle->app_name);
  821. rc = -EINVAL;
  822. goto error;
  823. }
  824. if (!(handle->hdcp_state & HDCP_STATE_TXMTR_INIT)) {
  825. pr_err("txmtr not initialized\n");
  826. rc = -EINVAL;
  827. goto error;
  828. }
  829. req_buf->ctxhandle = handle->tz_ctxhandle;
  830. rc = hdcp2_app_process_cmd(tx_deinit);
  831. if (rc)
  832. goto error;
  833. handle->hdcp_state &= ~HDCP_STATE_TXMTR_INIT;
  834. pr_debug("success\n");
  835. error:
  836. return rc;
  837. }
  838. static int hdcp2_app_start_auth(struct hdcp2_handle *handle)
  839. {
  840. int rc = 0;
  841. hdcp2_app_init_var(start_auth);
  842. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  843. pr_err("session not initialized\n");
  844. rc = -EINVAL;
  845. goto error;
  846. }
  847. if (!(handle->hdcp_state & HDCP_STATE_TXMTR_INIT)) {
  848. pr_err("txmtr not initialized\n");
  849. rc = -EINVAL;
  850. goto error;
  851. }
  852. req_buf->ctxHandle = handle->tz_ctxhandle;
  853. rc = hdcp2_app_process_cmd(start_auth);
  854. if (rc)
  855. goto error;
  856. handle->app_data.response.data = rsp_buf->message;
  857. handle->app_data.response.length = rsp_buf->msglen;
  858. handle->app_data.timeout = rsp_buf->timeout;
  859. handle->app_data.repeater_flag = false;
  860. handle->tz_ctxhandle = rsp_buf->ctxhandle;
  861. pr_debug("success\n");
  862. error:
  863. return rc;
  864. }
  865. static int hdcp2_app_start(struct hdcp2_handle *handle)
  866. {
  867. int rc = 0;
  868. rc = hdcp2_app_load(handle);
  869. if (rc)
  870. goto error;
  871. if (!handle->legacy_app) {
  872. rc = hdcp2_app_session_init(handle);
  873. if (rc)
  874. goto error;
  875. }
  876. if (handle->tx_init == NULL) {
  877. pr_err("invalid txmtr init function pointer\n");
  878. rc = -EINVAL;
  879. goto error;
  880. }
  881. rc = handle->tx_init(handle);
  882. error:
  883. return rc;
  884. }
  885. static int hdcp2_app_stop(struct hdcp2_handle *handle)
  886. {
  887. int rc = 0;
  888. rc = hdcp2_app_tx_deinit(handle);
  889. if (rc)
  890. goto end;
  891. if (!handle->legacy_app) {
  892. rc = hdcp2_app_session_deinit(handle);
  893. if (rc)
  894. goto end;
  895. }
  896. rc = hdcp2_app_unload(handle);
  897. end:
  898. return rc;
  899. }
  900. static int hdcp2_app_process_msg(struct hdcp2_handle *handle)
  901. {
  902. int rc = 0;
  903. hdcp2_app_init_var(rcvd_msg);
  904. if (!handle->app_data.request.data) {
  905. pr_err("invalid request buffer\n");
  906. rc = -EINVAL;
  907. goto error;
  908. }
  909. req_buf->msglen = handle->app_data.request.length;
  910. req_buf->ctxhandle = handle->tz_ctxhandle;
  911. rc = hdcp2_app_process_cmd(rcvd_msg);
  912. if (rc)
  913. goto error;
  914. /* check if it's a repeater */
  915. if (rsp_buf->flag == HDCP_TXMTR_SUBSTATE_WAITING_FOR_RECIEVERID_LIST)
  916. handle->app_data.repeater_flag = true;
  917. handle->app_data.response.data = rsp_buf->msg;
  918. handle->app_data.response.length = rsp_buf->msglen;
  919. handle->app_data.timeout = rsp_buf->timeout;
  920. error:
  921. return rc;
  922. }
  923. static int hdcp2_app_timeout(struct hdcp2_handle *handle)
  924. {
  925. int rc = 0;
  926. hdcp2_app_init_var(send_timeout);
  927. rc = hdcp2_app_process_cmd(send_timeout);
  928. if (rc)
  929. goto error;
  930. handle->app_data.response.data = rsp_buf->message;
  931. handle->app_data.response.length = rsp_buf->msglen;
  932. handle->app_data.timeout = rsp_buf->timeout;
  933. error:
  934. return rc;
  935. }
  936. static int hdcp2_app_enable_encryption(struct hdcp2_handle *handle)
  937. {
  938. int rc = 0;
  939. hdcp2_app_init_var(set_hw_key);
  940. /*
  941. * wait at least 200ms before enabling encryption
  942. * as per hdcp2p2 specifications.
  943. */
  944. msleep(SLEEP_SET_HW_KEY_MS);
  945. req_buf->ctxhandle = handle->tz_ctxhandle;
  946. rc = hdcp2_app_process_cmd(set_hw_key);
  947. if (rc)
  948. goto error;
  949. handle->hdcp_state |= HDCP_STATE_AUTHENTICATED;
  950. pr_debug("success\n");
  951. return rc;
  952. error:
  953. return rc;
  954. }
  955. static int hdcp2_force_encryption_utility(struct hdcp2_handle *handle,
  956. uint32_t enable)
  957. {
  958. int rc = 0;
  959. hdcp2_app_init_var(force_encryption);
  960. if (handle->hdcp_state == HDCP_STATE_AUTHENTICATED)
  961. msleep(SLEEP_FORCE_ENCRYPTION_MS);
  962. req_buf->ctxhandle = handle->tz_ctxhandle;
  963. req_buf->enable = enable;
  964. rc = hdcp2_app_process_cmd(force_encryption);
  965. if (rc || (rsp_buf->commandid != hdcp_cmd_force_encryption))
  966. goto error;
  967. return 0;
  968. error:
  969. return rc;
  970. }
  971. int hdcp2_force_encryption(void *ctx, uint32_t enable)
  972. {
  973. int rc = 0;
  974. struct hdcp2_handle *handle = NULL;
  975. if (!ctx) {
  976. pr_err("invalid input\n");
  977. return -EINVAL;
  978. }
  979. handle = ctx;
  980. rc = hdcp2_force_encryption_utility(handle, enable);
  981. if (rc)
  982. goto error;
  983. pr_debug("success\n");
  984. return 0;
  985. error:
  986. pr_err("failed, rc=%d\n", rc);
  987. return rc;
  988. }
  989. EXPORT_SYMBOL(hdcp2_force_encryption);
  990. static int hdcp2_app_query_stream(struct hdcp2_handle *handle)
  991. {
  992. int rc = 0;
  993. hdcp2_app_init_var(query_stream_type);
  994. req_buf->ctxhandle = handle->tz_ctxhandle;
  995. rc = hdcp2_app_process_cmd(query_stream_type);
  996. if (rc)
  997. goto error;
  998. handle->app_data.response.data = rsp_buf->msg;
  999. handle->app_data.response.length = rsp_buf->msglen;
  1000. handle->app_data.timeout = rsp_buf->timeout;
  1001. error:
  1002. return rc;
  1003. }
  1004. static unsigned char *hdcp2_get_recv_buf(struct hdcp2_handle *handle)
  1005. {
  1006. struct hdcp_rcvd_msg_req *req_buf;
  1007. req_buf = (struct hdcp_rcvd_msg_req *)(handle->qseecom_handle->sbuf);
  1008. return req_buf->msg;
  1009. }
  1010. int hdcp2_app_comm(void *ctx, enum hdcp2_app_cmd cmd,
  1011. struct hdcp2_app_data *app_data)
  1012. {
  1013. struct hdcp2_handle *handle = NULL;
  1014. int rc = 0;
  1015. if (!ctx || !app_data) {
  1016. pr_err("invalid input\n");
  1017. return -EINVAL;
  1018. }
  1019. handle = ctx;
  1020. handle->app_data.request.length = app_data->request.length;
  1021. pr_debug("command %s\n", hdcp2_app_cmd_str(cmd));
  1022. switch (cmd) {
  1023. case HDCP2_CMD_START:
  1024. rc = hdcp2_app_start(handle);
  1025. break;
  1026. case HDCP2_CMD_START_AUTH:
  1027. rc = hdcp2_app_start_auth(handle);
  1028. break;
  1029. case HDCP2_CMD_PROCESS_MSG:
  1030. rc = hdcp2_app_process_msg(handle);
  1031. break;
  1032. case HDCP2_CMD_TIMEOUT:
  1033. rc = hdcp2_app_timeout(handle);
  1034. break;
  1035. case HDCP2_CMD_EN_ENCRYPTION:
  1036. rc = hdcp2_app_enable_encryption(handle);
  1037. break;
  1038. case HDCP2_CMD_QUERY_STREAM:
  1039. rc = hdcp2_app_query_stream(handle);
  1040. break;
  1041. case HDCP2_CMD_STOP:
  1042. rc = hdcp2_app_stop(handle);
  1043. default:
  1044. goto error;
  1045. }
  1046. if (rc)
  1047. goto error;
  1048. handle->app_data.request.data = hdcp2_get_recv_buf(handle);
  1049. app_data->request.data = handle->app_data.request.data;
  1050. app_data->request.length = handle->app_data.request.length;
  1051. app_data->response.data = handle->app_data.response.data;
  1052. app_data->response.length = handle->app_data.response.length;
  1053. app_data->timeout = handle->app_data.timeout;
  1054. app_data->repeater_flag = handle->app_data.repeater_flag;
  1055. error:
  1056. return rc;
  1057. }
  1058. EXPORT_SYMBOL(hdcp2_app_comm);
  1059. static int hdcp2_open_stream_helper(struct hdcp2_handle *handle,
  1060. uint8_t vc_payload_id,
  1061. uint8_t stream_number,
  1062. uint32_t *stream_id)
  1063. {
  1064. int rc = 0;
  1065. hdcp2_app_init_var(session_open_stream);
  1066. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  1067. pr_err("session not initialized\n");
  1068. rc = -EINVAL;
  1069. goto error;
  1070. }
  1071. if (!(handle->hdcp_state & HDCP_STATE_TXMTR_INIT)) {
  1072. pr_err("txmtr not initialized\n");
  1073. rc = -EINVAL;
  1074. goto error;
  1075. }
  1076. req_buf->sessionid = handle->session_id;
  1077. req_buf->vcpayloadid = vc_payload_id;
  1078. req_buf->stream_number = stream_number;
  1079. req_buf->streamMediaType = 0;
  1080. rc = hdcp2_app_process_cmd(session_open_stream);
  1081. if (rc)
  1082. goto error;
  1083. *stream_id = rsp_buf->streamid;
  1084. pr_debug("success\n");
  1085. error:
  1086. return rc;
  1087. }
  1088. int hdcp2_open_stream(void *ctx, uint8_t vc_payload_id, uint8_t stream_number,
  1089. uint32_t *stream_id)
  1090. {
  1091. struct hdcp2_handle *handle = NULL;
  1092. if (!ctx) {
  1093. pr_err("invalid input\n");
  1094. return -EINVAL;
  1095. }
  1096. handle = ctx;
  1097. return hdcp2_open_stream_helper(handle, vc_payload_id, stream_number,
  1098. stream_id);
  1099. }
  1100. EXPORT_SYMBOL(hdcp2_open_stream);
  1101. static int hdcp2_close_stream_helper(struct hdcp2_handle *handle,
  1102. uint32_t stream_id)
  1103. {
  1104. int rc = 0;
  1105. hdcp2_app_init_var(session_close_stream);
  1106. if (!(handle->hdcp_state & HDCP_STATE_SESSION_INIT)) {
  1107. pr_err("session not initialized\n");
  1108. rc = -EINVAL;
  1109. goto error;
  1110. }
  1111. if (!(handle->hdcp_state & HDCP_STATE_TXMTR_INIT)) {
  1112. pr_err("txmtr not initialized\n");
  1113. rc = -EINVAL;
  1114. goto error;
  1115. }
  1116. req_buf->sessionid = handle->session_id;
  1117. req_buf->streamid = stream_id;
  1118. rc = hdcp2_app_process_cmd(session_close_stream);
  1119. if (rc)
  1120. goto error;
  1121. pr_debug("success\n");
  1122. error:
  1123. return rc;
  1124. }
  1125. int hdcp2_close_stream(void *ctx, uint32_t stream_id)
  1126. {
  1127. struct hdcp2_handle *handle = NULL;
  1128. if (!ctx) {
  1129. pr_err("invalid input\n");
  1130. return -EINVAL;
  1131. }
  1132. handle = ctx;
  1133. return hdcp2_close_stream_helper(handle, stream_id);
  1134. }
  1135. EXPORT_SYMBOL(hdcp2_close_stream);
  1136. void *hdcp2_init(u32 device_type)
  1137. {
  1138. struct hdcp2_handle *handle = NULL;
  1139. handle = kzalloc(sizeof(struct hdcp2_handle), GFP_KERNEL);
  1140. if (!handle)
  1141. goto error;
  1142. handle->device_type = device_type;
  1143. handle->app_name = HDCP2P2_APP_NAME;
  1144. error:
  1145. return handle;
  1146. }
  1147. EXPORT_SYMBOL(hdcp2_init);
  1148. void hdcp2_deinit(void *ctx)
  1149. {
  1150. kfree_sensitive(ctx);
  1151. }
  1152. EXPORT_SYMBOL(hdcp2_deinit);
  1153. void *hdcp1_init(void)
  1154. {
  1155. struct hdcp1_handle *handle =
  1156. kzalloc(sizeof(struct hdcp1_handle), GFP_KERNEL);
  1157. if (!handle)
  1158. goto error;
  1159. handle->app_name = HDCP1_APP_NAME;
  1160. error:
  1161. return handle;
  1162. }
  1163. EXPORT_SYMBOL(hdcp1_init);
  1164. void hdcp1_deinit(void *data)
  1165. {
  1166. kfree(data);
  1167. }
  1168. EXPORT_SYMBOL(hdcp1_deinit);
  1169. static int hdcp1_count_ones(u8 *array, u8 len)
  1170. {
  1171. int i, j, count = 0;
  1172. for (i = 0; i < len; i++)
  1173. for (j = 0; j < 8; j++)
  1174. count += (((array[i] >> j) & 0x1) ? 1 : 0);
  1175. return count;
  1176. }
  1177. static int hdcp1_validate_aksv(u32 aksv_msb, u32 aksv_lsb)
  1178. {
  1179. int const number_of_ones = 20;
  1180. u8 aksv[5];
  1181. pr_debug("AKSV=%02x%08x\n", aksv_msb, aksv_lsb);
  1182. aksv[0] = aksv_lsb & 0xFF;
  1183. aksv[1] = (aksv_lsb >> 8) & 0xFF;
  1184. aksv[2] = (aksv_lsb >> 16) & 0xFF;
  1185. aksv[3] = (aksv_lsb >> 24) & 0xFF;
  1186. aksv[4] = aksv_msb & 0xFF;
  1187. /* check there are 20 ones in AKSV */
  1188. if (hdcp1_count_ones(aksv, 5) != number_of_ones) {
  1189. pr_err("AKSV bit count failed\n");
  1190. return -EINVAL;
  1191. }
  1192. return 0;
  1193. }
  1194. static int hdcp1_set_key(struct hdcp1_handle *hdcp1_handle, u32 *aksv_msb,
  1195. u32 *aksv_lsb)
  1196. {
  1197. int rc = 0;
  1198. struct hdcp1_key_set_req *key_set_req;
  1199. struct hdcp1_key_set_rsp *key_set_rsp;
  1200. struct qseecom_handle *handle = NULL;
  1201. if (aksv_msb == NULL || aksv_lsb == NULL) {
  1202. pr_err("invalid aksv\n");
  1203. return -EINVAL;
  1204. }
  1205. if (!hdcp1_handle || !hdcp1_handle->qseecom_handle) {
  1206. pr_err("invalid HDCP 1.x handle\n");
  1207. return -EINVAL;
  1208. }
  1209. if (!(hdcp1_handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  1210. pr_err("%s app not loaded\n", hdcp1_handle->app_name);
  1211. return -EINVAL;
  1212. }
  1213. handle = hdcp1_handle->qseecom_handle;
  1214. /* set keys and request aksv */
  1215. key_set_req = (struct hdcp1_key_set_req *)handle->sbuf;
  1216. key_set_req->commandid = HDCP1_SET_KEY;
  1217. key_set_rsp = (struct hdcp1_key_set_rsp *)(handle->sbuf +
  1218. QSEECOM_ALIGN(sizeof(struct hdcp1_key_set_req)));
  1219. rc = qseecom_send_command(handle, key_set_req,
  1220. QSEECOM_ALIGN(sizeof
  1221. (struct hdcp1_key_set_req)),
  1222. key_set_rsp,
  1223. QSEECOM_ALIGN(sizeof
  1224. (struct hdcp1_key_set_rsp)));
  1225. if (rc < 0) {
  1226. pr_err("qseecom cmd failed err=%d\n", rc);
  1227. return -ENOKEY;
  1228. }
  1229. rc = key_set_rsp->ret;
  1230. if (rc) {
  1231. pr_err("set key cmd failed, rsp=%d\n", key_set_rsp->ret);
  1232. return -ENOKEY;
  1233. }
  1234. /* copy bytes into msb and lsb */
  1235. *aksv_msb = key_set_rsp->ksv[0] << 24 | key_set_rsp->ksv[1] << 16 |
  1236. key_set_rsp->ksv[2] << 8 | key_set_rsp->ksv[3];
  1237. *aksv_lsb = key_set_rsp->ksv[4] << 24 | key_set_rsp->ksv[5] << 16 |
  1238. key_set_rsp->ksv[6] << 8 | key_set_rsp->ksv[7];
  1239. rc = hdcp1_validate_aksv(*aksv_msb, *aksv_lsb);
  1240. if (rc) {
  1241. pr_err("aksv validation failed (%d)\n", rc);
  1242. return rc;
  1243. }
  1244. return 0;
  1245. }
  1246. static int hdcp1_app_load(struct hdcp1_handle *handle)
  1247. {
  1248. int rc = 0;
  1249. if (!handle) {
  1250. pr_err("invalid handle\n");
  1251. goto error;
  1252. }
  1253. rc = qseecom_start_app(&handle->qseecom_handle, handle->app_name,
  1254. QSEECOM_SBUFF_SIZE);
  1255. if (rc) {
  1256. pr_err("%s app load failed (%d)\n", handle->app_name, rc);
  1257. goto error;
  1258. }
  1259. rc = qseecom_start_app(&handle->hdcpops_handle, HDCP1OPS_APP_NAME,
  1260. QSEECOM_SBUFF_SIZE);
  1261. if (rc) {
  1262. pr_warn("%s app load failed (%d)\n", HDCP1OPS_APP_NAME, rc);
  1263. handle->hdcpops_handle = NULL;
  1264. }
  1265. handle->hdcp_state |= HDCP_STATE_APP_LOADED;
  1266. pr_debug("%s app loaded\n", handle->app_name);
  1267. error:
  1268. return rc;
  1269. }
  1270. static void hdcp1_app_unload(struct hdcp1_handle *handle)
  1271. {
  1272. int rc = 0;
  1273. if (!handle || !handle->qseecom_handle) {
  1274. pr_err("invalid handle\n");
  1275. return;
  1276. }
  1277. if (!(handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  1278. pr_warn("%s app not loaded\n", handle->app_name);
  1279. return;
  1280. }
  1281. if (handle->hdcpops_handle) {
  1282. /* deallocate the resources for HDCP 1.x ops handle */
  1283. rc = qseecom_shutdown_app(&handle->hdcpops_handle);
  1284. if (rc)
  1285. pr_warn("%s app unload failed (%d)\n", HDCP1OPS_APP_NAME, rc);
  1286. }
  1287. /* deallocate the resources for qseecom HDCP 1.x handle */
  1288. rc = qseecom_shutdown_app(&handle->qseecom_handle);
  1289. if (rc) {
  1290. pr_err("%s app unload failed (%d)\n", handle->app_name, rc);
  1291. return;
  1292. }
  1293. handle->hdcp_state &= ~HDCP_STATE_APP_LOADED;
  1294. pr_debug("%s app unloaded\n", handle->app_name);
  1295. }
  1296. static int hdcp1_verify_key(struct hdcp1_handle *hdcp1_handle)
  1297. {
  1298. int rc = 0;
  1299. struct hdcp1_key_verify_req *key_verify_req;
  1300. struct hdcp1_key_verify_rsp *key_verify_rsp;
  1301. struct qseecom_handle *handle = NULL;
  1302. if (!hdcp1_handle || !hdcp1_handle->qseecom_handle) {
  1303. pr_err("invalid HDCP 1.x handle\n");
  1304. return -EINVAL;
  1305. }
  1306. if (!(hdcp1_handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  1307. pr_err("%s app not loaded\n", hdcp1_handle->app_name);
  1308. return -EINVAL;
  1309. }
  1310. handle = hdcp1_handle->qseecom_handle;
  1311. key_verify_req = (struct hdcp1_key_verify_req *)handle->sbuf;
  1312. key_verify_req->commandid = HDCP1_KEY_VERIFY;
  1313. key_verify_rsp = (struct hdcp1_key_verify_rsp *)(handle->sbuf +
  1314. QSEECOM_ALIGN(sizeof(struct hdcp1_key_verify_req)));
  1315. rc = qseecom_send_command(handle, key_verify_req,
  1316. QSEECOM_ALIGN(sizeof
  1317. (struct hdcp1_key_verify_req)),
  1318. key_verify_rsp,
  1319. QSEECOM_ALIGN(sizeof
  1320. (struct hdcp1_key_set_rsp)));
  1321. if (rc < 0) {
  1322. pr_err("command HDCP1_KEY_VERIFY failed (%d)\n", rc);
  1323. return -EINVAL;
  1324. }
  1325. rc = key_verify_rsp->ret;
  1326. if (rc) {
  1327. pr_err("key_verify failed, rsp=%d\n", key_verify_rsp->ret);
  1328. return -EINVAL;
  1329. }
  1330. pr_debug("success\n");
  1331. return 0;
  1332. }
  1333. bool hdcp1_feature_supported(void *data)
  1334. {
  1335. bool supported = false;
  1336. struct hdcp1_handle *handle = data;
  1337. int rc = 0;
  1338. if (!handle) {
  1339. pr_err("invalid handle\n");
  1340. goto error;
  1341. }
  1342. if (handle->feature_supported) {
  1343. supported = true;
  1344. goto error;
  1345. }
  1346. rc = hdcp1_app_load(handle);
  1347. if (!rc && (handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  1348. if (!hdcp1_verify_key(handle)) {
  1349. pr_debug("HDCP 1.x supported\n");
  1350. handle->feature_supported = true;
  1351. supported = true;
  1352. }
  1353. hdcp1_app_unload(handle);
  1354. }
  1355. error:
  1356. return supported;
  1357. }
  1358. EXPORT_SYMBOL(hdcp1_feature_supported);
  1359. int hdcp1_set_enc(void *data, bool enable)
  1360. {
  1361. int rc = 0;
  1362. struct hdcp1_set_enc_req *set_enc_req;
  1363. struct hdcp1_set_enc_rsp *set_enc_rsp;
  1364. struct hdcp1_handle *hdcp1_handle = data;
  1365. struct qseecom_handle *handle = NULL;
  1366. if (!hdcp1_handle || !hdcp1_handle->qseecom_handle) {
  1367. pr_err("invalid HDCP 1.x handle\n");
  1368. return -EINVAL;
  1369. }
  1370. if (!hdcp1_handle->feature_supported) {
  1371. pr_err("HDCP 1.x not supported\n");
  1372. return -EINVAL;
  1373. }
  1374. if (!(hdcp1_handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  1375. pr_err("%s app not loaded\n", hdcp1_handle->app_name);
  1376. return -EINVAL;
  1377. }
  1378. handle = hdcp1_handle->qseecom_handle;
  1379. /* set keys and request aksv */
  1380. set_enc_req = (struct hdcp1_set_enc_req *)handle->sbuf;
  1381. set_enc_req->commandid = HDCP1_SET_ENC;
  1382. set_enc_req->enable = enable;
  1383. set_enc_rsp = (struct hdcp1_set_enc_rsp *)(handle->sbuf +
  1384. QSEECOM_ALIGN(sizeof(struct hdcp1_set_enc_req)));
  1385. rc = qseecom_send_command(handle, set_enc_req,
  1386. QSEECOM_ALIGN(sizeof
  1387. (struct hdcp1_set_enc_req)),
  1388. set_enc_rsp,
  1389. QSEECOM_ALIGN(sizeof
  1390. (struct hdcp1_set_enc_rsp)));
  1391. if (rc < 0) {
  1392. pr_err("qseecom cmd failed err=%d\n", rc);
  1393. return -EINVAL;
  1394. }
  1395. rc = set_enc_rsp->ret;
  1396. if (rc) {
  1397. pr_err("enc cmd failed, rsp=%d\n", set_enc_rsp->ret);
  1398. return -EINVAL;
  1399. }
  1400. pr_debug("success\n");
  1401. return 0;
  1402. }
  1403. EXPORT_SYMBOL(hdcp1_set_enc);
  1404. int hdcp1_ops_notify(void *data, void *topo, bool is_authenticated)
  1405. {
  1406. int rc = 0;
  1407. struct hdcp1_ops_notify_req *ops_notify_req;
  1408. struct hdcp1_ops_notify_rsp *ops_notify_rsp;
  1409. struct hdcp1_handle *hdcp1_handle = data;
  1410. struct qseecom_handle *handle = NULL;
  1411. struct hdcp1_topology *topology = NULL;
  1412. if (!hdcp1_handle || !hdcp1_handle->hdcpops_handle) {
  1413. pr_err("invalid HDCP 1.x ops handle\n");
  1414. return -EINVAL;
  1415. }
  1416. if (!hdcp1_handle->feature_supported) {
  1417. pr_err("HDCP 1.x not supported\n");
  1418. return -EINVAL;
  1419. }
  1420. if (!(hdcp1_handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  1421. pr_err("%s app not loaded\n", HDCP1OPS_APP_NAME);
  1422. return -EINVAL;
  1423. }
  1424. handle = hdcp1_handle->hdcpops_handle;
  1425. topology = (struct hdcp1_topology *)topo;
  1426. /* set keys and request aksv */
  1427. ops_notify_req = (struct hdcp1_ops_notify_req *)handle->sbuf;
  1428. ops_notify_req->commandid = HDCP1_NOTIFY_TOPOLOGY;
  1429. ops_notify_req->device_type = DEVICE_TYPE_DP;
  1430. ops_notify_req->is_authenticated = is_authenticated;
  1431. ops_notify_req->topology.depth = topology->depth;
  1432. ops_notify_req->topology.device_count = topology->device_count;
  1433. ops_notify_req->topology.max_devices_exceeded = topology->max_devices_exceeded;
  1434. ops_notify_req->topology.max_cascade_exceeded = topology->max_cascade_exceeded;
  1435. /*
  1436. * For hdcp1.4 below two nodes are not applicable but as
  1437. * TZ ops ta talks with other drivers with same structure
  1438. * and want to maintain same interface across hdcp versions,
  1439. * we are setting the values to 0.
  1440. */
  1441. ops_notify_req->topology.hdcp2LegacyDeviceDownstream = 0;
  1442. ops_notify_req->topology.hdcp1DeviceDownstream = 0;
  1443. memset(ops_notify_req->recv_id_list, 0, sizeof(uint8_t) * MAX_REC_ID_LIST_SIZE);
  1444. ops_notify_rsp = (struct hdcp1_ops_notify_rsp *)(handle->sbuf +
  1445. QSEECOM_ALIGN(sizeof(struct hdcp1_ops_notify_req)));
  1446. rc = qseecom_send_command(handle, ops_notify_req,
  1447. QSEECOM_ALIGN(sizeof(struct hdcp1_ops_notify_req)),
  1448. ops_notify_rsp,
  1449. QSEECOM_ALIGN(sizeof(struct hdcp1_ops_notify_rsp)));
  1450. rc = ops_notify_rsp->ret;
  1451. if (rc < 0) {
  1452. pr_warn("Ops notify cmd failed, rsp=%d\n", ops_notify_rsp->ret);
  1453. return -EINVAL;
  1454. }
  1455. pr_debug("ops notify success\n");
  1456. return 0;
  1457. }
  1458. EXPORT_SYMBOL(hdcp1_ops_notify);
  1459. int hdcp1_start(void *data, u32 *aksv_msb, u32 *aksv_lsb)
  1460. {
  1461. int rc = 0;
  1462. struct hdcp1_handle *hdcp1_handle = data;
  1463. if (!aksv_msb || !aksv_lsb) {
  1464. pr_err("invalid aksv output buffer\n");
  1465. rc = -EINVAL;
  1466. goto error;
  1467. }
  1468. if (!hdcp1_handle) {
  1469. pr_err("invalid handle\n");
  1470. rc = -EINVAL;
  1471. goto error;
  1472. }
  1473. if (!hdcp1_handle->feature_supported) {
  1474. pr_err("feature not supported\n");
  1475. rc = -EINVAL;
  1476. goto error;
  1477. }
  1478. if (hdcp1_handle->hdcp_state & HDCP_STATE_APP_LOADED) {
  1479. pr_debug("%s app already loaded\n", hdcp1_handle->app_name);
  1480. goto error;
  1481. }
  1482. rc = hdcp1_app_load(hdcp1_handle);
  1483. if (rc)
  1484. goto error;
  1485. rc = hdcp1_set_key(hdcp1_handle, aksv_msb, aksv_lsb);
  1486. if (rc)
  1487. goto key_error;
  1488. pr_debug("success\n");
  1489. return rc;
  1490. key_error:
  1491. hdcp1_app_unload(hdcp1_handle);
  1492. error:
  1493. return rc;
  1494. }
  1495. EXPORT_SYMBOL(hdcp1_start);
  1496. void hdcp1_stop(void *data)
  1497. {
  1498. struct hdcp1_handle *hdcp1_handle = data;
  1499. if (!hdcp1_handle || !hdcp1_handle->qseecom_handle || !hdcp1_handle->hdcpops_handle) {
  1500. pr_err("invalid handle\n");
  1501. return;
  1502. }
  1503. if (!(hdcp1_handle->hdcp_state & HDCP_STATE_APP_LOADED)) {
  1504. pr_debug("%s app not loaded\n", hdcp1_handle->app_name);
  1505. return;
  1506. }
  1507. hdcp1_app_unload(hdcp1_handle);
  1508. }
  1509. EXPORT_SYMBOL(hdcp1_stop);
  1510. static int __init hdcp_module_init(void){ return 0; }
  1511. static void __exit hdcp_module_exit(void){ return; }
  1512. MODULE_LICENSE("GPL v2");
  1513. MODULE_DESCRIPTION("HDCP driver");
  1514. module_init(hdcp_module_init);
  1515. module_exit(hdcp_module_exit);