1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #define MAX_NUM_DISPLAYS 24
27 
28 
29 #include "hdcp.h"
30 
31 #include "amdgpu.h"
32 #include "hdcp_psp.h"
33 
34 static void hdcp2_message_init(struct mod_hdcp *hdcp,
35 			       struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *in)
36 {
37 	in->session_handle = hdcp->auth.id;
38 	in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
39 	in->prepare.msg2_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
40 	in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
41 	in->process.msg1_desc.msg_size = 0;
42 	in->process.msg2_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
43 	in->process.msg2_desc.msg_size = 0;
44 	in->process.msg3_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
45 	in->process.msg3_desc.msg_size = 0;
46 }
47 
48 static enum mod_hdcp_status remove_display_from_topology_v2(
49 		struct mod_hdcp *hdcp, uint8_t index)
50 {
51 	struct psp_context *psp = hdcp->config.psp.handle;
52 	struct ta_dtm_shared_memory *dtm_cmd;
53 	struct mod_hdcp_display *display =
54 			get_active_display_at_index(hdcp, index);
55 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
56 
57 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
58 
59 	if (!display || !is_display_active(display))
60 		return MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
61 
62 	mutex_lock(&psp->dtm_context.mutex);
63 
64 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
65 
66 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_UPDATE_V2;
67 	dtm_cmd->dtm_in_message.topology_update_v2.display_handle = display->index;
68 	dtm_cmd->dtm_in_message.topology_update_v2.is_active = 0;
69 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
70 
71 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
72 
73 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
74 		status = MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE;
75 	} else {
76 		display->state = MOD_HDCP_DISPLAY_ACTIVE;
77 		HDCP_TOP_REMOVE_DISPLAY_TRACE(hdcp, display->index);
78 	}
79 
80 	mutex_unlock(&psp->dtm_context.mutex);
81 	return status;
82 }
83 
84 static enum mod_hdcp_status remove_display_from_topology_v3(
85 		struct mod_hdcp *hdcp, uint8_t index)
86 {
87 	struct psp_context *psp = hdcp->config.psp.handle;
88 	struct ta_dtm_shared_memory *dtm_cmd;
89 	struct mod_hdcp_display *display =
90 		get_active_display_at_index(hdcp, index);
91 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
92 
93 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
94 
95 	if (!display || !is_display_active(display))
96 		return MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
97 
98 	mutex_lock(&psp->dtm_context.mutex);
99 
100 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
101 
102 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_UPDATE_V3;
103 	dtm_cmd->dtm_in_message.topology_update_v3.display_handle = display->index;
104 	dtm_cmd->dtm_in_message.topology_update_v3.is_active = 0;
105 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
106 
107 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
108 
109 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
110 		status = remove_display_from_topology_v2(hdcp, index);
111 		if (status != MOD_HDCP_STATUS_SUCCESS)
112 			display->state = MOD_HDCP_DISPLAY_INACTIVE;
113 	} else {
114 		display->state = MOD_HDCP_DISPLAY_ACTIVE;
115 		HDCP_TOP_REMOVE_DISPLAY_TRACE(hdcp, display->index);
116 	}
117 
118 	mutex_unlock(&psp->dtm_context.mutex);
119 
120 	return status;
121 }
122 
123 static enum mod_hdcp_status add_display_to_topology_v2(
124 		struct mod_hdcp *hdcp, struct mod_hdcp_display *display)
125 {
126 	struct psp_context *psp = hdcp->config.psp.handle;
127 	struct ta_dtm_shared_memory *dtm_cmd;
128 	struct mod_hdcp_link *link = &hdcp->connection.link;
129 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
130 
131 	if (!psp->dtm_context.context.initialized) {
132 		DRM_INFO("Failed to add display topology, DTM TA is not initialized.");
133 		display->state = MOD_HDCP_DISPLAY_INACTIVE;
134 		return MOD_HDCP_STATUS_FAILURE;
135 	}
136 
137 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
138 
139 	mutex_lock(&psp->dtm_context.mutex);
140 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
141 
142 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_UPDATE_V2;
143 	dtm_cmd->dtm_in_message.topology_update_v2.display_handle = display->index;
144 	dtm_cmd->dtm_in_message.topology_update_v2.is_active = 1;
145 	dtm_cmd->dtm_in_message.topology_update_v2.controller = display->controller;
146 	dtm_cmd->dtm_in_message.topology_update_v2.ddc_line = link->ddc_line;
147 	dtm_cmd->dtm_in_message.topology_update_v2.dig_be = link->dig_be;
148 	dtm_cmd->dtm_in_message.topology_update_v2.dig_fe = display->dig_fe;
149 	if (is_dp_hdcp(hdcp))
150 		dtm_cmd->dtm_in_message.topology_update_v2.is_assr = link->dp.assr_enabled;
151 
152 	dtm_cmd->dtm_in_message.topology_update_v2.dp_mst_vcid = display->vc_id;
153 	dtm_cmd->dtm_in_message.topology_update_v2.max_hdcp_supported_version =
154 			TA_DTM_HDCP_VERSION_MAX_SUPPORTED__2_2;
155 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
156 
157 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
158 
159 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
160 		display->state = MOD_HDCP_DISPLAY_INACTIVE;
161 		status = MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE;
162 	} else {
163 		HDCP_TOP_ADD_DISPLAY_TRACE(hdcp, display->index);
164 	}
165 
166 	mutex_unlock(&psp->dtm_context.mutex);
167 	return status;
168 }
169 
170 static enum mod_hdcp_status add_display_to_topology_v3(
171 		struct mod_hdcp *hdcp, struct mod_hdcp_display *display)
172 {
173 	struct psp_context *psp = hdcp->config.psp.handle;
174 	struct ta_dtm_shared_memory *dtm_cmd;
175 	struct mod_hdcp_link *link = &hdcp->connection.link;
176 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
177 
178 	if (!psp->dtm_context.context.initialized) {
179 		DRM_INFO("Failed to add display topology, DTM TA is not initialized.");
180 		display->state = MOD_HDCP_DISPLAY_INACTIVE;
181 		return MOD_HDCP_STATUS_FAILURE;
182 	}
183 
184 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
185 
186 	mutex_lock(&psp->dtm_context.mutex);
187 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
188 
189 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_UPDATE_V3;
190 	dtm_cmd->dtm_in_message.topology_update_v3.display_handle = display->index;
191 	dtm_cmd->dtm_in_message.topology_update_v3.is_active = 1;
192 	dtm_cmd->dtm_in_message.topology_update_v3.controller = display->controller;
193 	dtm_cmd->dtm_in_message.topology_update_v3.ddc_line = link->ddc_line;
194 	dtm_cmd->dtm_in_message.topology_update_v3.link_enc = link->link_enc_idx;
195 	dtm_cmd->dtm_in_message.topology_update_v3.stream_enc = display->stream_enc_idx;
196 	if (is_dp_hdcp(hdcp))
197 		dtm_cmd->dtm_in_message.topology_update_v3.is_assr = link->dp.assr_enabled;
198 
199 	dtm_cmd->dtm_in_message.topology_update_v3.dp_mst_vcid = display->vc_id;
200 	dtm_cmd->dtm_in_message.topology_update_v3.max_hdcp_supported_version =
201 			TA_DTM_HDCP_VERSION_MAX_SUPPORTED__2_3;
202 	dtm_cmd->dtm_in_message.topology_update_v3.encoder_type = TA_DTM_ENCODER_TYPE__DIG;
203 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
204 	dtm_cmd->dtm_in_message.topology_update_v3.phy_id = link->phy_idx;
205 	dtm_cmd->dtm_in_message.topology_update_v3.link_hdcp_cap = link->hdcp_supported_informational;
206 
207 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
208 
209 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
210 		status = add_display_to_topology_v2(hdcp, display);
211 		if (status != MOD_HDCP_STATUS_SUCCESS)
212 			display->state = MOD_HDCP_DISPLAY_INACTIVE;
213 	} else {
214 		HDCP_TOP_ADD_DISPLAY_TRACE(hdcp, display->index);
215 	}
216 
217 	mutex_unlock(&psp->dtm_context.mutex);
218 
219 	return status;
220 }
221 
222 enum mod_hdcp_status mod_hdcp_remove_display_from_topology(
223 		struct mod_hdcp *hdcp, uint8_t index)
224 {
225 	enum mod_hdcp_status status = MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE;
226 
227 	if (hdcp->config.psp.caps.dtm_v3_supported)
228 		status = remove_display_from_topology_v3(hdcp, index);
229 	else
230 		status = remove_display_from_topology_v2(hdcp, index);
231 
232 	return status;
233 }
234 
235 enum mod_hdcp_status mod_hdcp_add_display_to_topology(struct mod_hdcp *hdcp,
236 					       struct mod_hdcp_display *display)
237 {
238 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
239 
240 	if (hdcp->config.psp.caps.dtm_v3_supported)
241 		status = add_display_to_topology_v3(hdcp, display);
242 	else
243 		status = add_display_to_topology_v2(hdcp, display);
244 
245 	return status;
246 }
247 
248 enum mod_hdcp_status mod_hdcp_hdcp1_create_session(struct mod_hdcp *hdcp)
249 {
250 
251 	struct psp_context *psp = hdcp->config.psp.handle;
252 	struct mod_hdcp_display *display = get_first_active_display(hdcp);
253 	struct ta_hdcp_shared_memory *hdcp_cmd;
254 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
255 
256 	if (!psp->hdcp_context.context.initialized) {
257 		DRM_ERROR("Failed to create hdcp session. HDCP TA is not initialized.");
258 		return MOD_HDCP_STATUS_FAILURE;
259 	}
260 
261 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
262 
263 	mutex_lock(&psp->hdcp_context.mutex);
264 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
265 
266 	hdcp_cmd->in_msg.hdcp1_create_session.display_handle = display->index;
267 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_CREATE_SESSION;
268 
269 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
270 
271 	hdcp->auth.id = hdcp_cmd->out_msg.hdcp1_create_session.session_handle;
272 
273 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
274 		status = MOD_HDCP_STATUS_HDCP1_CREATE_SESSION_FAILURE;
275 	} else {
276 		hdcp->auth.msg.hdcp1.ainfo = hdcp_cmd->out_msg.hdcp1_create_session.ainfo_primary;
277 		memcpy(hdcp->auth.msg.hdcp1.aksv, hdcp_cmd->out_msg.hdcp1_create_session.aksv_primary,
278 		       sizeof(hdcp->auth.msg.hdcp1.aksv));
279 		memcpy(hdcp->auth.msg.hdcp1.an, hdcp_cmd->out_msg.hdcp1_create_session.an_primary,
280 		       sizeof(hdcp->auth.msg.hdcp1.an));
281 	}
282 
283 	mutex_unlock(&psp->hdcp_context.mutex);
284 	return status;
285 }
286 
287 enum mod_hdcp_status mod_hdcp_hdcp1_destroy_session(struct mod_hdcp *hdcp)
288 {
289 
290 	struct psp_context *psp = hdcp->config.psp.handle;
291 	struct ta_hdcp_shared_memory *hdcp_cmd;
292 	uint8_t i = 0;
293 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
294 
295 	mutex_lock(&psp->hdcp_context.mutex);
296 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
297 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
298 
299 	hdcp_cmd->in_msg.hdcp1_destroy_session.session_handle = hdcp->auth.id;
300 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_DESTROY_SESSION;
301 
302 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
303 
304 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
305 		status = MOD_HDCP_STATUS_HDCP1_DESTROY_SESSION_FAILURE;
306 	} else {
307 		HDCP_TOP_HDCP1_DESTROY_SESSION_TRACE(hdcp);
308 		for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
309 			if (is_display_encryption_enabled(&hdcp->displays[i])) {
310 				hdcp->displays[i].state =
311 							MOD_HDCP_DISPLAY_ACTIVE;
312 				HDCP_HDCP1_DISABLED_TRACE(
313 					hdcp, hdcp->displays[i].index);
314 			}
315 	}
316 
317 	mutex_unlock(&psp->hdcp_context.mutex);
318 	return status;
319 }
320 
321 enum mod_hdcp_status mod_hdcp_hdcp1_validate_rx(struct mod_hdcp *hdcp)
322 {
323 	struct psp_context *psp = hdcp->config.psp.handle;
324 	struct ta_hdcp_shared_memory *hdcp_cmd;
325 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
326 
327 	mutex_lock(&psp->hdcp_context.mutex);
328 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
329 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
330 
331 	hdcp_cmd->in_msg.hdcp1_first_part_authentication.session_handle = hdcp->auth.id;
332 
333 	memcpy(hdcp_cmd->in_msg.hdcp1_first_part_authentication.bksv_primary, hdcp->auth.msg.hdcp1.bksv,
334 		TA_HDCP__HDCP1_KSV_SIZE);
335 
336 	hdcp_cmd->in_msg.hdcp1_first_part_authentication.r0_prime_primary = hdcp->auth.msg.hdcp1.r0p;
337 	hdcp_cmd->in_msg.hdcp1_first_part_authentication.bcaps = hdcp->auth.msg.hdcp1.bcaps;
338 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_FIRST_PART_AUTHENTICATION;
339 
340 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
341 
342 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
343 		status = MOD_HDCP_STATUS_HDCP1_VALIDATE_RX_FAILURE;
344 	} else if (hdcp_cmd->out_msg.hdcp1_first_part_authentication.authentication_status ==
345 	    TA_HDCP_AUTHENTICATION_STATUS__HDCP1_FIRST_PART_COMPLETE) {
346 		/* needs second part of authentication */
347 		hdcp->connection.is_repeater = 1;
348 	} else if (hdcp_cmd->out_msg.hdcp1_first_part_authentication.authentication_status ==
349 		   TA_HDCP_AUTHENTICATION_STATUS__HDCP1_AUTHENTICATED) {
350 		hdcp->connection.is_repeater = 0;
351 	} else if (hdcp_cmd->out_msg.hdcp1_first_part_authentication.authentication_status ==
352 		   TA_HDCP_AUTHENTICATION_STATUS__HDCP1_KSV_REVOKED) {
353 		hdcp->connection.is_hdcp1_revoked = 1;
354 		status = MOD_HDCP_STATUS_HDCP1_BKSV_REVOKED;
355 	} else
356 		status = MOD_HDCP_STATUS_HDCP1_VALIDATE_RX_FAILURE;
357 
358 	mutex_unlock(&psp->hdcp_context.mutex);
359 	return status;
360 }
361 
362 enum mod_hdcp_status mod_hdcp_hdcp1_enable_encryption(struct mod_hdcp *hdcp)
363 {
364 	struct psp_context *psp = hdcp->config.psp.handle;
365 	struct ta_hdcp_shared_memory *hdcp_cmd;
366 	struct mod_hdcp_display *display = get_first_active_display(hdcp);
367 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
368 
369 	mutex_lock(&psp->hdcp_context.mutex);
370 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
371 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
372 
373 	hdcp_cmd->in_msg.hdcp1_enable_encryption.session_handle = hdcp->auth.id;
374 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_ENABLE_ENCRYPTION;
375 
376 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
377 
378 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
379 		status = MOD_HDCP_STATUS_HDCP1_ENABLE_ENCRYPTION_FAILURE;
380 	} else if (!is_dp_mst_hdcp(hdcp)) {
381 		display->state = MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
382 		HDCP_HDCP1_ENABLED_TRACE(hdcp, display->index);
383 	}
384 
385 	mutex_unlock(&psp->hdcp_context.mutex);
386 	return status;
387 }
388 
389 enum mod_hdcp_status mod_hdcp_hdcp1_validate_ksvlist_vp(struct mod_hdcp *hdcp)
390 {
391 	struct psp_context *psp = hdcp->config.psp.handle;
392 	struct ta_hdcp_shared_memory *hdcp_cmd;
393 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
394 
395 	mutex_lock(&psp->hdcp_context.mutex);
396 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
397 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
398 
399 	hdcp_cmd->in_msg.hdcp1_second_part_authentication.session_handle = hdcp->auth.id;
400 
401 	hdcp_cmd->in_msg.hdcp1_second_part_authentication.ksv_list_size = hdcp->auth.msg.hdcp1.ksvlist_size;
402 	memcpy(hdcp_cmd->in_msg.hdcp1_second_part_authentication.ksv_list, hdcp->auth.msg.hdcp1.ksvlist,
403 	       hdcp->auth.msg.hdcp1.ksvlist_size);
404 
405 	memcpy(hdcp_cmd->in_msg.hdcp1_second_part_authentication.v_prime, hdcp->auth.msg.hdcp1.vp,
406 	       sizeof(hdcp->auth.msg.hdcp1.vp));
407 
408 	hdcp_cmd->in_msg.hdcp1_second_part_authentication.bstatus_binfo =
409 		is_dp_hdcp(hdcp) ? hdcp->auth.msg.hdcp1.binfo_dp : hdcp->auth.msg.hdcp1.bstatus;
410 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_SECOND_PART_AUTHENTICATION;
411 
412 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
413 
414 	if (hdcp_cmd->hdcp_status == TA_HDCP_STATUS__SUCCESS &&
415 	    hdcp_cmd->out_msg.hdcp1_second_part_authentication.authentication_status ==
416 		    TA_HDCP_AUTHENTICATION_STATUS__HDCP1_AUTHENTICATED) {
417 		status = MOD_HDCP_STATUS_SUCCESS;
418 	} else if (hdcp_cmd->out_msg.hdcp1_second_part_authentication.authentication_status ==
419 		   TA_HDCP_AUTHENTICATION_STATUS__HDCP1_KSV_REVOKED) {
420 		hdcp->connection.is_hdcp1_revoked = 1;
421 		status = MOD_HDCP_STATUS_HDCP1_KSV_LIST_REVOKED;
422 	} else {
423 		status = MOD_HDCP_STATUS_HDCP1_VALIDATE_KSV_LIST_FAILURE;
424 	}
425 
426 	mutex_unlock(&psp->hdcp_context.mutex);
427 	return status;
428 }
429 
430 enum mod_hdcp_status mod_hdcp_hdcp1_enable_dp_stream_encryption(struct mod_hdcp *hdcp)
431 {
432 
433 	struct psp_context *psp = hdcp->config.psp.handle;
434 	struct ta_hdcp_shared_memory *hdcp_cmd;
435 	int i = 0;
436 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
437 
438 	mutex_lock(&psp->hdcp_context.mutex);
439 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
440 
441 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++) {
442 
443 		if (hdcp->displays[i].adjust.disable || hdcp->displays[i].state != MOD_HDCP_DISPLAY_ACTIVE)
444 				continue;
445 
446 		memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
447 
448 		hdcp_cmd->in_msg.hdcp1_enable_dp_stream_encryption.session_handle = hdcp->auth.id;
449 		hdcp_cmd->in_msg.hdcp1_enable_dp_stream_encryption.display_handle = hdcp->displays[i].index;
450 		hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_ENABLE_DP_STREAM_ENCRYPTION;
451 
452 		psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
453 
454 		if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
455 			status = MOD_HDCP_STATUS_HDCP1_ENABLE_STREAM_ENCRYPTION_FAILURE;
456 			break;
457 		}
458 
459 		hdcp->displays[i].state = MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
460 		HDCP_HDCP1_ENABLED_TRACE(hdcp, hdcp->displays[i].index);
461 	}
462 
463 	mutex_unlock(&psp->hdcp_context.mutex);
464 	return status;
465 }
466 
467 enum mod_hdcp_status mod_hdcp_hdcp1_link_maintenance(struct mod_hdcp *hdcp)
468 {
469 	struct psp_context *psp = hdcp->config.psp.handle;
470 	struct ta_hdcp_shared_memory *hdcp_cmd;
471 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
472 
473 	mutex_lock(&psp->hdcp_context.mutex);
474 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
475 
476 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
477 
478 	hdcp_cmd->in_msg.hdcp1_get_encryption_status.session_handle = hdcp->auth.id;
479 
480 	hdcp_cmd->out_msg.hdcp1_get_encryption_status.protection_level = 0;
481 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_GET_ENCRYPTION_STATUS;
482 
483 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
484 
485 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS ||
486 			hdcp_cmd->out_msg.hdcp1_get_encryption_status.protection_level != 1)
487 		status = MOD_HDCP_STATUS_HDCP1_LINK_MAINTENANCE_FAILURE;
488 
489 	mutex_unlock(&psp->hdcp_context.mutex);
490 	return status;
491 }
492 
493 enum mod_hdcp_status mod_hdcp_hdcp2_create_session(struct mod_hdcp *hdcp)
494 {
495 	struct psp_context *psp = hdcp->config.psp.handle;
496 	struct ta_hdcp_shared_memory *hdcp_cmd;
497 	struct mod_hdcp_display *display = get_first_active_display(hdcp);
498 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
499 
500 
501 	if (!psp->hdcp_context.context.initialized) {
502 		DRM_ERROR("Failed to create hdcp session, HDCP TA is not initialized");
503 		return MOD_HDCP_STATUS_FAILURE;
504 	}
505 
506 	if (!display)
507 		return MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
508 
509 	mutex_lock(&psp->hdcp_context.mutex);
510 
511 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
512 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
513 
514 	hdcp_cmd->in_msg.hdcp2_create_session_v2.display_handle = display->index;
515 
516 	if (hdcp->connection.link.adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_0)
517 		hdcp_cmd->in_msg.hdcp2_create_session_v2.negotiate_content_type =
518 			TA_HDCP2_CONTENT_TYPE_NEGOTIATION_TYPE__FORCE_TYPE0;
519 	else if (hdcp->connection.link.adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_1)
520 		hdcp_cmd->in_msg.hdcp2_create_session_v2.negotiate_content_type =
521 			TA_HDCP2_CONTENT_TYPE_NEGOTIATION_TYPE__FORCE_TYPE1;
522 	else if (hdcp->connection.link.adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_MAX)
523 		hdcp_cmd->in_msg.hdcp2_create_session_v2.negotiate_content_type =
524 			TA_HDCP2_CONTENT_TYPE_NEGOTIATION_TYPE__MAX_SUPPORTED;
525 
526 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_CREATE_SESSION_V2;
527 
528 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
529 
530 
531 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
532 		status = MOD_HDCP_STATUS_HDCP2_CREATE_SESSION_FAILURE;
533 	else
534 		hdcp->auth.id = hdcp_cmd->out_msg.hdcp2_create_session_v2.session_handle;
535 
536 	mutex_unlock(&psp->hdcp_context.mutex);
537 	return status;
538 }
539 
540 enum mod_hdcp_status mod_hdcp_hdcp2_destroy_session(struct mod_hdcp *hdcp)
541 {
542 	struct psp_context *psp = hdcp->config.psp.handle;
543 	struct ta_hdcp_shared_memory *hdcp_cmd;
544 	uint8_t i = 0;
545 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
546 
547 	mutex_lock(&psp->hdcp_context.mutex);
548 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
549 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
550 
551 	hdcp_cmd->in_msg.hdcp2_destroy_session.session_handle = hdcp->auth.id;
552 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_DESTROY_SESSION;
553 
554 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
555 
556 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
557 		status = MOD_HDCP_STATUS_HDCP2_DESTROY_SESSION_FAILURE;
558 	} else {
559 		HDCP_TOP_HDCP2_DESTROY_SESSION_TRACE(hdcp);
560 		for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
561 			if (is_display_encryption_enabled(&hdcp->displays[i])) {
562 				hdcp->displays[i].state =
563 							MOD_HDCP_DISPLAY_ACTIVE;
564 				HDCP_HDCP2_DISABLED_TRACE(
565 					hdcp, hdcp->displays[i].index);
566 			}
567 	}
568 
569 	mutex_unlock(&psp->hdcp_context.mutex);
570 	return status;
571 }
572 
573 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_ake_init(struct mod_hdcp *hdcp)
574 {
575 	struct psp_context *psp = hdcp->config.psp.handle;
576 	struct ta_hdcp_shared_memory *hdcp_cmd;
577 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
578 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
579 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
580 
581 	mutex_lock(&psp->hdcp_context.mutex);
582 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
583 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
584 
585 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
586 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
587 
588 	hdcp2_message_init(hdcp, msg_in);
589 
590 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
591 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__AKE_INIT;
592 
593 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
594 
595 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
596 		status = MOD_HDCP_STATUS_HDCP2_PREP_AKE_INIT_FAILURE;
597 	else
598 		memcpy(&hdcp->auth.msg.hdcp2.ake_init[0], &msg_out->prepare.transmitter_message[0],
599 		       sizeof(hdcp->auth.msg.hdcp2.ake_init));
600 
601 	mutex_unlock(&psp->hdcp_context.mutex);
602 	return status;
603 }
604 
605 enum mod_hdcp_status mod_hdcp_hdcp2_validate_ake_cert(struct mod_hdcp *hdcp)
606 {
607 	struct psp_context *psp = hdcp->config.psp.handle;
608 	struct ta_hdcp_shared_memory *hdcp_cmd;
609 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
610 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
611 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
612 
613 	mutex_lock(&psp->hdcp_context.mutex);
614 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
615 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
616 
617 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
618 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
619 
620 	hdcp2_message_init(hdcp, msg_in);
621 
622 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__AKE_SEND_CERT;
623 	msg_in->process.msg1_desc.msg_size = TA_HDCP_HDCP2_MSG_ID_MAX_SIZE__AKE_SEND_CERT;
624 
625 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.ake_cert,
626 	       sizeof(hdcp->auth.msg.hdcp2.ake_cert));
627 
628 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__AKE_NO_STORED_KM;
629 	msg_in->prepare.msg2_id = TA_HDCP_HDCP2_MSG_ID__AKE_STORED_KM;
630 
631 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
632 
633 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
634 
635 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
636 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_AKE_CERT_FAILURE;
637 	} else {
638 		memcpy(hdcp->auth.msg.hdcp2.ake_no_stored_km,
639 		       &msg_out->prepare.transmitter_message[0],
640 		       sizeof(hdcp->auth.msg.hdcp2.ake_no_stored_km));
641 
642 		memcpy(hdcp->auth.msg.hdcp2.ake_stored_km,
643 		       &msg_out->prepare.transmitter_message[sizeof(hdcp->auth.msg.hdcp2.ake_no_stored_km)],
644 		       sizeof(hdcp->auth.msg.hdcp2.ake_stored_km));
645 
646 		if (msg_out->process.msg1_status ==
647 		    TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS) {
648 			hdcp->connection.is_km_stored =
649 				msg_out->process.is_km_stored ? 1 : 0;
650 			hdcp->connection.is_repeater =
651 				msg_out->process.is_repeater ? 1 : 0;
652 			status = MOD_HDCP_STATUS_SUCCESS;
653 		} else if (msg_out->process.msg1_status ==
654 			   TA_HDCP2_MSG_AUTHENTICATION_STATUS__RECEIVERID_REVOKED) {
655 			hdcp->connection.is_hdcp2_revoked = 1;
656 			status = MOD_HDCP_STATUS_HDCP2_AKE_CERT_REVOKED;
657 		}  else {
658 			status = MOD_HDCP_STATUS_HDCP2_VALIDATE_AKE_CERT_FAILURE;
659 		}
660 	}
661 	mutex_unlock(&psp->hdcp_context.mutex);
662 	return status;
663 }
664 
665 enum mod_hdcp_status mod_hdcp_hdcp2_validate_h_prime(struct mod_hdcp *hdcp)
666 {
667 	struct psp_context *psp = hdcp->config.psp.handle;
668 	struct ta_hdcp_shared_memory *hdcp_cmd;
669 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
670 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
671 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
672 
673 	mutex_lock(&psp->hdcp_context.mutex);
674 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
675 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
676 
677 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
678 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
679 
680 	hdcp2_message_init(hdcp, msg_in);
681 
682 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__AKE_SEND_H_PRIME;
683 	msg_in->process.msg1_desc.msg_size = TA_HDCP_HDCP2_MSG_ID_MAX_SIZE__AKE_SEND_H_PRIME;
684 
685 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.ake_h_prime,
686 	       sizeof(hdcp->auth.msg.hdcp2.ake_h_prime));
687 
688 	if (!hdcp->connection.is_km_stored) {
689 		msg_in->process.msg2_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__AKE_SEND_PAIRING_INFO;
690 		msg_in->process.msg2_desc.msg_size = TA_HDCP_HDCP2_MSG_ID_MAX_SIZE__AKE_SEND_PAIRING_INFO;
691 		memcpy(&msg_in->process.receiver_message[sizeof(hdcp->auth.msg.hdcp2.ake_h_prime)],
692 		       hdcp->auth.msg.hdcp2.ake_pairing_info, sizeof(hdcp->auth.msg.hdcp2.ake_pairing_info));
693 	}
694 
695 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
696 
697 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
698 
699 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
700 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_H_PRIME_FAILURE;
701 	else if (msg_out->process.msg1_status != TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS)
702 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_H_PRIME_FAILURE;
703 	else if (!hdcp->connection.is_km_stored &&
704 		   msg_out->process.msg2_status != TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS)
705 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_PAIRING_INFO_FAILURE;
706 
707 	mutex_unlock(&psp->hdcp_context.mutex);
708 	return status;
709 }
710 
711 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_lc_init(struct mod_hdcp *hdcp)
712 {
713 	struct psp_context *psp = hdcp->config.psp.handle;
714 	struct ta_hdcp_shared_memory *hdcp_cmd;
715 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
716 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
717 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
718 
719 	mutex_lock(&psp->hdcp_context.mutex);
720 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
721 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
722 
723 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
724 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
725 
726 	hdcp2_message_init(hdcp, msg_in);
727 
728 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__LC_INIT;
729 
730 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
731 
732 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
733 
734 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
735 		status = MOD_HDCP_STATUS_HDCP2_PREP_LC_INIT_FAILURE;
736 	else
737 		memcpy(hdcp->auth.msg.hdcp2.lc_init, &msg_out->prepare.transmitter_message[0],
738 		       sizeof(hdcp->auth.msg.hdcp2.lc_init));
739 
740 	mutex_unlock(&psp->hdcp_context.mutex);
741 	return status;
742 }
743 
744 enum mod_hdcp_status mod_hdcp_hdcp2_validate_l_prime(struct mod_hdcp *hdcp)
745 {
746 	struct psp_context *psp = hdcp->config.psp.handle;
747 	struct ta_hdcp_shared_memory *hdcp_cmd;
748 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
749 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
750 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
751 
752 	mutex_lock(&psp->hdcp_context.mutex);
753 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
754 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
755 
756 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
757 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
758 
759 	hdcp2_message_init(hdcp, msg_in);
760 
761 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__LC_SEND_L_PRIME;
762 	msg_in->process.msg1_desc.msg_size = TA_HDCP_HDCP2_MSG_ID_MAX_SIZE__LC_SEND_L_PRIME;
763 
764 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.lc_l_prime,
765 	       sizeof(hdcp->auth.msg.hdcp2.lc_l_prime));
766 
767 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
768 
769 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
770 
771 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS ||
772 			msg_out->process.msg1_status != TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS)
773 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_L_PRIME_FAILURE;
774 
775 	mutex_unlock(&psp->hdcp_context.mutex);
776 	return status;
777 }
778 
779 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_eks(struct mod_hdcp *hdcp)
780 {
781 	struct psp_context *psp = hdcp->config.psp.handle;
782 	struct ta_hdcp_shared_memory *hdcp_cmd;
783 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
784 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
785 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
786 
787 	mutex_lock(&psp->hdcp_context.mutex);
788 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
789 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
790 
791 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
792 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
793 
794 	hdcp2_message_init(hdcp, msg_in);
795 
796 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__SKE_SEND_EKS;
797 
798 	if (is_dp_hdcp(hdcp))
799 		msg_in->prepare.msg2_id = TA_HDCP_HDCP2_MSG_ID__SIGNAL_CONTENT_STREAM_TYPE_DP;
800 
801 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
802 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
803 
804 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
805 		status = MOD_HDCP_STATUS_HDCP2_PREP_EKS_FAILURE;
806 	} else {
807 		memcpy(hdcp->auth.msg.hdcp2.ske_eks,
808 		       &msg_out->prepare.transmitter_message[0],
809 		       sizeof(hdcp->auth.msg.hdcp2.ske_eks));
810 		msg_out->prepare.msg1_desc.msg_size =
811 			sizeof(hdcp->auth.msg.hdcp2.ske_eks);
812 
813 		if (is_dp_hdcp(hdcp)) {
814 			memcpy(hdcp->auth.msg.hdcp2.content_stream_type_dp,
815 			       &msg_out->prepare.transmitter_message[sizeof(hdcp->auth.msg.hdcp2.ske_eks)],
816 			       sizeof(hdcp->auth.msg.hdcp2.content_stream_type_dp));
817 		}
818 	}
819 	mutex_unlock(&psp->hdcp_context.mutex);
820 
821 	return status;
822 }
823 
824 enum mod_hdcp_status mod_hdcp_hdcp2_enable_encryption(struct mod_hdcp *hdcp)
825 {
826 	struct psp_context *psp = hdcp->config.psp.handle;
827 	struct ta_hdcp_shared_memory *hdcp_cmd;
828 	struct mod_hdcp_display *display = get_first_active_display(hdcp);
829 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
830 
831 	if (!display)
832 		return MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
833 
834 	mutex_lock(&psp->hdcp_context.mutex);
835 
836 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
837 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
838 
839 	hdcp_cmd->in_msg.hdcp2_set_encryption.session_handle = hdcp->auth.id;
840 
841 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_SET_ENCRYPTION;
842 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
843 
844 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
845 		status = MOD_HDCP_STATUS_HDCP2_ENABLE_ENCRYPTION_FAILURE;
846 	} else if (!is_dp_mst_hdcp(hdcp)) {
847 		display->state = MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
848 		HDCP_HDCP2_ENABLED_TRACE(hdcp, display->index);
849 	}
850 
851 	mutex_unlock(&psp->hdcp_context.mutex);
852 	return status;
853 }
854 
855 enum mod_hdcp_status mod_hdcp_hdcp2_validate_rx_id_list(struct mod_hdcp *hdcp)
856 {
857 	struct psp_context *psp = hdcp->config.psp.handle;
858 	struct ta_hdcp_shared_memory *hdcp_cmd;
859 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
860 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
861 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
862 
863 	mutex_lock(&psp->hdcp_context.mutex);
864 
865 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
866 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
867 
868 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
869 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
870 
871 	hdcp2_message_init(hdcp, msg_in);
872 
873 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__REPEATERAUTH_SEND_RECEIVERID_LIST;
874 	msg_in->process.msg1_desc.msg_size = sizeof(hdcp->auth.msg.hdcp2.rx_id_list);
875 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.rx_id_list,
876 	       sizeof(hdcp->auth.msg.hdcp2.rx_id_list));
877 
878 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__REPEATERAUTH_SEND_ACK;
879 
880 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
881 
882 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
883 
884 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
885 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_RX_ID_LIST_FAILURE;
886 	} else {
887 		memcpy(hdcp->auth.msg.hdcp2.repeater_auth_ack,
888 		       &msg_out->prepare.transmitter_message[0],
889 		       sizeof(hdcp->auth.msg.hdcp2.repeater_auth_ack));
890 
891 		if (msg_out->process.msg1_status ==
892 		    TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS) {
893 			hdcp->connection.is_km_stored = msg_out->process.is_km_stored ? 1 : 0;
894 			hdcp->connection.is_repeater = msg_out->process.is_repeater ? 1 : 0;
895 			status = MOD_HDCP_STATUS_SUCCESS;
896 		} else if (msg_out->process.msg1_status ==
897 			   TA_HDCP2_MSG_AUTHENTICATION_STATUS__RECEIVERID_REVOKED) {
898 			hdcp->connection.is_hdcp2_revoked = 1;
899 			status = MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_REVOKED;
900 		} else {
901 			status = MOD_HDCP_STATUS_HDCP2_VALIDATE_RX_ID_LIST_FAILURE;
902 		}
903 	}
904 	mutex_unlock(&psp->hdcp_context.mutex);
905 	return status;
906 }
907 
908 enum mod_hdcp_status mod_hdcp_hdcp2_enable_dp_stream_encryption(struct mod_hdcp *hdcp)
909 {
910 	struct psp_context *psp = hdcp->config.psp.handle;
911 	struct ta_hdcp_shared_memory *hdcp_cmd;
912 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
913 	uint8_t i;
914 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
915 
916 	mutex_lock(&psp->hdcp_context.mutex);
917 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
918 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
919 
920 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
921 
922 	hdcp2_message_init(hdcp, msg_in);
923 
924 
925 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++) {
926 		if (hdcp->displays[i].adjust.disable || hdcp->displays[i].state != MOD_HDCP_DISPLAY_ACTIVE)
927 				continue;
928 
929 		hdcp_cmd->in_msg.hdcp2_enable_dp_stream_encryption.display_handle = hdcp->displays[i].index;
930 		hdcp_cmd->in_msg.hdcp2_enable_dp_stream_encryption.session_handle = hdcp->auth.id;
931 
932 		hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_ENABLE_DP_STREAM_ENCRYPTION;
933 		psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
934 
935 		if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
936 			break;
937 
938 		hdcp->displays[i].state = MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
939 		HDCP_HDCP2_ENABLED_TRACE(hdcp, hdcp->displays[i].index);
940 	}
941 
942 	if (hdcp_cmd->hdcp_status == TA_HDCP_STATUS__SUCCESS)
943 		status = MOD_HDCP_STATUS_SUCCESS;
944 	else
945 		status = MOD_HDCP_STATUS_HDCP2_ENABLE_STREAM_ENCRYPTION_FAILURE;
946 
947 	mutex_unlock(&psp->hdcp_context.mutex);
948 	return status;
949 }
950 
951 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_stream_management(struct mod_hdcp *hdcp)
952 {
953 
954 	struct psp_context *psp = hdcp->config.psp.handle;
955 	struct ta_hdcp_shared_memory *hdcp_cmd;
956 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
957 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
958 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
959 
960 	mutex_lock(&psp->hdcp_context.mutex);
961 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
962 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
963 
964 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
965 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
966 
967 	hdcp2_message_init(hdcp, msg_in);
968 
969 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__REPEATERAUTH_STREAM_MANAGE;
970 
971 
972 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
973 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
974 
975 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
976 		status = MOD_HDCP_STATUS_HDCP2_PREPARE_STREAM_MANAGEMENT_FAILURE;
977 	} else {
978 		hdcp->auth.msg.hdcp2.stream_manage_size = msg_out->prepare.msg1_desc.msg_size;
979 
980 		memcpy(hdcp->auth.msg.hdcp2.repeater_auth_stream_manage,
981 		       &msg_out->prepare.transmitter_message[0],
982 		       sizeof(hdcp->auth.msg.hdcp2.repeater_auth_stream_manage));
983 	}
984 	mutex_unlock(&psp->hdcp_context.mutex);
985 	return status;
986 }
987 
988 enum mod_hdcp_status mod_hdcp_hdcp2_validate_stream_ready(struct mod_hdcp *hdcp)
989 {
990 	struct psp_context *psp = hdcp->config.psp.handle;
991 	struct ta_hdcp_shared_memory *hdcp_cmd;
992 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
993 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
994 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
995 
996 	mutex_lock(&psp->hdcp_context.mutex);
997 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
998 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
999 
1000 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
1001 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
1002 
1003 	hdcp2_message_init(hdcp, msg_in);
1004 
1005 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__REPEATERAUTH_STREAM_READY;
1006 
1007 	msg_in->process.msg1_desc.msg_size = sizeof(hdcp->auth.msg.hdcp2.repeater_auth_stream_ready);
1008 
1009 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.repeater_auth_stream_ready,
1010 	       sizeof(hdcp->auth.msg.hdcp2.repeater_auth_stream_ready));
1011 
1012 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
1013 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
1014 
1015 	if (hdcp_cmd->hdcp_status == TA_HDCP_STATUS__SUCCESS &&
1016 	    msg_out->process.msg1_status == TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS)
1017 		status = MOD_HDCP_STATUS_SUCCESS;
1018 	else
1019 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_STREAM_READY_FAILURE;
1020 
1021 	mutex_unlock(&psp->hdcp_context.mutex);
1022 	return status;
1023 }
1024