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 #include <linux/delay.h>
27 
28 #include "hdcp.h"
29 
30 static inline enum mod_hdcp_status check_receiver_id_list_ready(struct mod_hdcp *hdcp)
31 {
32 	uint8_t is_ready = 0;
33 
34 	if (is_dp_hdcp(hdcp))
35 		is_ready = HDCP_2_2_DP_RXSTATUS_READY(hdcp->auth.msg.hdcp2.rxstatus_dp) ? 1 : 0;
36 	else
37 		is_ready = (HDCP_2_2_HDMI_RXSTATUS_READY(hdcp->auth.msg.hdcp2.rxstatus[0]) &&
38 				(HDCP_2_2_HDMI_RXSTATUS_MSG_SZ_HI(hdcp->auth.msg.hdcp2.rxstatus[1]) << 8 |
39 						hdcp->auth.msg.hdcp2.rxstatus[0])) ? 1 : 0;
40 	return is_ready ? MOD_HDCP_STATUS_SUCCESS :
41 			MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_NOT_READY;
42 }
43 
44 static inline enum mod_hdcp_status check_hdcp2_capable(struct mod_hdcp *hdcp)
45 {
46 	enum mod_hdcp_status status;
47 
48 	if (is_dp_hdcp(hdcp))
49 		status = (hdcp->auth.msg.hdcp2.rxcaps_dp[2] & HDCP_2_2_RX_CAPS_VERSION_VAL) &&
50 				HDCP_2_2_DP_HDCP_CAPABLE(hdcp->auth.msg.hdcp2.rxcaps_dp[0]) ?
51 				MOD_HDCP_STATUS_SUCCESS :
52 				MOD_HDCP_STATUS_HDCP2_NOT_CAPABLE;
53 	else
54 		status = (hdcp->auth.msg.hdcp2.hdcp2version_hdmi & HDCP_2_2_HDMI_SUPPORT_MASK) ?
55 				MOD_HDCP_STATUS_SUCCESS :
56 				MOD_HDCP_STATUS_HDCP2_NOT_CAPABLE;
57 	return status;
58 }
59 
60 static inline enum mod_hdcp_status check_reauthentication_request(
61 		struct mod_hdcp *hdcp)
62 {
63 	uint8_t ret = 0;
64 
65 	if (is_dp_hdcp(hdcp))
66 		ret = HDCP_2_2_DP_RXSTATUS_REAUTH_REQ(hdcp->auth.msg.hdcp2.rxstatus_dp) ?
67 				MOD_HDCP_STATUS_HDCP2_REAUTH_REQUEST :
68 				MOD_HDCP_STATUS_SUCCESS;
69 	else
70 		ret = HDCP_2_2_HDMI_RXSTATUS_REAUTH_REQ(hdcp->auth.msg.hdcp2.rxstatus[0]) ?
71 				MOD_HDCP_STATUS_HDCP2_REAUTH_REQUEST :
72 				MOD_HDCP_STATUS_SUCCESS;
73 	return ret;
74 }
75 
76 static inline enum mod_hdcp_status check_link_integrity_failure_dp(
77 		struct mod_hdcp *hdcp)
78 {
79 	return HDCP_2_2_DP_RXSTATUS_LINK_FAILED(hdcp->auth.msg.hdcp2.rxstatus_dp) ?
80 			MOD_HDCP_STATUS_HDCP2_REAUTH_LINK_INTEGRITY_FAILURE :
81 			MOD_HDCP_STATUS_SUCCESS;
82 }
83 
84 static enum mod_hdcp_status check_ake_cert_available(struct mod_hdcp *hdcp)
85 {
86 	enum mod_hdcp_status status;
87 	uint16_t size;
88 
89 	if (is_dp_hdcp(hdcp)) {
90 		status = MOD_HDCP_STATUS_SUCCESS;
91 	} else {
92 		status = mod_hdcp_read_rxstatus(hdcp);
93 		if (status == MOD_HDCP_STATUS_SUCCESS) {
94 			size = HDCP_2_2_HDMI_RXSTATUS_MSG_SZ_HI(hdcp->auth.msg.hdcp2.rxstatus[1]) << 8 |
95 			       hdcp->auth.msg.hdcp2.rxstatus[0];
96 			status = (size == sizeof(hdcp->auth.msg.hdcp2.ake_cert)) ?
97 					MOD_HDCP_STATUS_SUCCESS :
98 					MOD_HDCP_STATUS_HDCP2_AKE_CERT_PENDING;
99 		}
100 	}
101 	return status;
102 }
103 
104 static enum mod_hdcp_status check_h_prime_available(struct mod_hdcp *hdcp)
105 {
106 	enum mod_hdcp_status status;
107 	uint8_t size;
108 
109 	status = mod_hdcp_read_rxstatus(hdcp);
110 	if (status != MOD_HDCP_STATUS_SUCCESS)
111 		goto out;
112 
113 	if (is_dp_hdcp(hdcp)) {
114 		status = HDCP_2_2_DP_RXSTATUS_H_PRIME(hdcp->auth.msg.hdcp2.rxstatus_dp) ?
115 				MOD_HDCP_STATUS_SUCCESS :
116 				MOD_HDCP_STATUS_HDCP2_H_PRIME_PENDING;
117 	} else {
118 		size = HDCP_2_2_HDMI_RXSTATUS_MSG_SZ_HI(hdcp->auth.msg.hdcp2.rxstatus[1]) << 8 |
119 		       hdcp->auth.msg.hdcp2.rxstatus[0];
120 		status = (size == sizeof(hdcp->auth.msg.hdcp2.ake_h_prime)) ?
121 				MOD_HDCP_STATUS_SUCCESS :
122 				MOD_HDCP_STATUS_HDCP2_H_PRIME_PENDING;
123 	}
124 out:
125 	return status;
126 }
127 
128 static enum mod_hdcp_status check_pairing_info_available(struct mod_hdcp *hdcp)
129 {
130 	enum mod_hdcp_status status;
131 	uint8_t size;
132 
133 	status = mod_hdcp_read_rxstatus(hdcp);
134 	if (status != MOD_HDCP_STATUS_SUCCESS)
135 		goto out;
136 
137 	if (is_dp_hdcp(hdcp)) {
138 		status = HDCP_2_2_DP_RXSTATUS_PAIRING(hdcp->auth.msg.hdcp2.rxstatus_dp) ?
139 				MOD_HDCP_STATUS_SUCCESS :
140 				MOD_HDCP_STATUS_HDCP2_PAIRING_INFO_PENDING;
141 	} else {
142 		size = HDCP_2_2_HDMI_RXSTATUS_MSG_SZ_HI(hdcp->auth.msg.hdcp2.rxstatus[1]) << 8 |
143 		       hdcp->auth.msg.hdcp2.rxstatus[0];
144 		status = (size == sizeof(hdcp->auth.msg.hdcp2.ake_pairing_info)) ?
145 				MOD_HDCP_STATUS_SUCCESS :
146 				MOD_HDCP_STATUS_HDCP2_PAIRING_INFO_PENDING;
147 	}
148 out:
149 	return status;
150 }
151 
152 static enum mod_hdcp_status poll_l_prime_available(struct mod_hdcp *hdcp)
153 {
154 	enum mod_hdcp_status status;
155 	uint8_t size;
156 	uint16_t max_wait = 20; // units of ms
157 	uint16_t num_polls = 5;
158 	uint16_t wait_time = max_wait / num_polls;
159 
160 	if (is_dp_hdcp(hdcp))
161 		status = MOD_HDCP_STATUS_INVALID_OPERATION;
162 	else
163 		for (; num_polls; num_polls--) {
164 			msleep(wait_time);
165 
166 			status = mod_hdcp_read_rxstatus(hdcp);
167 			if (status != MOD_HDCP_STATUS_SUCCESS)
168 				break;
169 
170 			size = HDCP_2_2_HDMI_RXSTATUS_MSG_SZ_HI(hdcp->auth.msg.hdcp2.rxstatus[1]) << 8 |
171 			       hdcp->auth.msg.hdcp2.rxstatus[0];
172 			status = (size == sizeof(hdcp->auth.msg.hdcp2.lc_l_prime)) ?
173 					MOD_HDCP_STATUS_SUCCESS :
174 					MOD_HDCP_STATUS_HDCP2_L_PRIME_PENDING;
175 			if (status == MOD_HDCP_STATUS_SUCCESS)
176 				break;
177 		}
178 	return status;
179 }
180 
181 static enum mod_hdcp_status check_stream_ready_available(struct mod_hdcp *hdcp)
182 {
183 	enum mod_hdcp_status status;
184 	uint8_t size;
185 
186 	if (is_dp_hdcp(hdcp)) {
187 		status = MOD_HDCP_STATUS_INVALID_OPERATION;
188 	} else {
189 		status = mod_hdcp_read_rxstatus(hdcp);
190 		if (status != MOD_HDCP_STATUS_SUCCESS)
191 			goto out;
192 		size = HDCP_2_2_HDMI_RXSTATUS_MSG_SZ_HI(hdcp->auth.msg.hdcp2.rxstatus[1]) << 8 |
193 		       hdcp->auth.msg.hdcp2.rxstatus[0];
194 		status = (size == sizeof(hdcp->auth.msg.hdcp2.repeater_auth_stream_ready)) ?
195 				MOD_HDCP_STATUS_SUCCESS :
196 				MOD_HDCP_STATUS_HDCP2_STREAM_READY_PENDING;
197 	}
198 out:
199 	return status;
200 }
201 
202 static inline uint8_t get_device_count(struct mod_hdcp *hdcp)
203 {
204 	return HDCP_2_2_DEV_COUNT_LO(hdcp->auth.msg.hdcp2.rx_id_list[2]) +
205 			(HDCP_2_2_DEV_COUNT_HI(hdcp->auth.msg.hdcp2.rx_id_list[1]) << 4);
206 }
207 
208 static enum mod_hdcp_status check_device_count(struct mod_hdcp *hdcp)
209 {
210 	/* device count must be greater than or equal to tracked hdcp displays */
211 	return (get_device_count(hdcp) < get_added_display_count(hdcp)) ?
212 			MOD_HDCP_STATUS_HDCP2_DEVICE_COUNT_MISMATCH_FAILURE :
213 			MOD_HDCP_STATUS_SUCCESS;
214 }
215 
216 static uint8_t process_rxstatus(struct mod_hdcp *hdcp,
217 		struct mod_hdcp_event_context *event_ctx,
218 		struct mod_hdcp_transition_input_hdcp2 *input,
219 		enum mod_hdcp_status *status)
220 {
221 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_rxstatus,
222 			&input->rxstatus_read, status,
223 			hdcp, "rxstatus_read"))
224 		goto out;
225 	if (!mod_hdcp_execute_and_set(check_reauthentication_request,
226 			&input->reauth_request_check, status,
227 			hdcp, "reauth_request_check"))
228 		goto out;
229 	if (is_dp_hdcp(hdcp)) {
230 		if (!mod_hdcp_execute_and_set(check_link_integrity_failure_dp,
231 				&input->link_integrity_check_dp, status,
232 				hdcp, "link_integrity_check_dp"))
233 			goto out;
234 	}
235 	if (hdcp->connection.is_repeater)
236 		if (check_receiver_id_list_ready(hdcp) ==
237 				MOD_HDCP_STATUS_SUCCESS) {
238 			HDCP_INPUT_PASS_TRACE(hdcp, "rx_id_list_ready");
239 			event_ctx->rx_id_list_ready = 1;
240 			if (is_dp_hdcp(hdcp))
241 				hdcp->auth.msg.hdcp2.rx_id_list_size =
242 						sizeof(hdcp->auth.msg.hdcp2.rx_id_list);
243 			else
244 				hdcp->auth.msg.hdcp2.rx_id_list_size =
245 					HDCP_2_2_HDMI_RXSTATUS_MSG_SZ_HI(hdcp->auth.msg.hdcp2.rxstatus[1]) << 8 |
246 					hdcp->auth.msg.hdcp2.rxstatus[0];
247 		}
248 out:
249 	return (*status == MOD_HDCP_STATUS_SUCCESS);
250 }
251 
252 static enum mod_hdcp_status known_hdcp2_capable_rx(struct mod_hdcp *hdcp,
253 		struct mod_hdcp_event_context *event_ctx,
254 		struct mod_hdcp_transition_input_hdcp2 *input)
255 {
256 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
257 
258 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
259 		event_ctx->unexpected_event = 1;
260 		goto out;
261 	}
262 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_hdcp2version,
263 			&input->hdcp2version_read, &status,
264 			hdcp, "hdcp2version_read"))
265 		goto out;
266 	if (!mod_hdcp_execute_and_set(check_hdcp2_capable,
267 			&input->hdcp2_capable_check, &status,
268 			hdcp, "hdcp2_capable"))
269 		goto out;
270 out:
271 	return status;
272 }
273 
274 static enum mod_hdcp_status send_ake_init(struct mod_hdcp *hdcp,
275 		struct mod_hdcp_event_context *event_ctx,
276 		struct mod_hdcp_transition_input_hdcp2 *input)
277 {
278 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
279 
280 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
281 		event_ctx->unexpected_event = 1;
282 		goto out;
283 	}
284 	if (!mod_hdcp_execute_and_set(mod_hdcp_add_display_topology,
285 			&input->add_topology, &status,
286 			hdcp, "add_topology"))
287 		goto out;
288 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_create_session,
289 			&input->create_session, &status,
290 			hdcp, "create_session"))
291 		goto out;
292 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_prepare_ake_init,
293 			&input->ake_init_prepare, &status,
294 			hdcp, "ake_init_prepare"))
295 		goto out;
296 	if (!mod_hdcp_execute_and_set(mod_hdcp_write_ake_init,
297 			&input->ake_init_write, &status,
298 			hdcp, "ake_init_write"))
299 		goto out;
300 out:
301 	return status;
302 }
303 
304 static enum mod_hdcp_status validate_ake_cert(struct mod_hdcp *hdcp,
305 		struct mod_hdcp_event_context *event_ctx,
306 		struct mod_hdcp_transition_input_hdcp2 *input)
307 {
308 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
309 
310 
311 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
312 			event_ctx->event != MOD_HDCP_EVENT_WATCHDOG_TIMEOUT) {
313 		event_ctx->unexpected_event = 1;
314 		goto out;
315 	}
316 
317 	if (is_hdmi_dvi_sl_hdcp(hdcp))
318 		if (!mod_hdcp_execute_and_set(check_ake_cert_available,
319 				&input->ake_cert_available, &status,
320 				hdcp, "ake_cert_available"))
321 			goto out;
322 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_ake_cert,
323 			&input->ake_cert_read, &status,
324 			hdcp, "ake_cert_read"))
325 		goto out;
326 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_validate_ake_cert,
327 			&input->ake_cert_validation, &status,
328 			hdcp, "ake_cert_validation"))
329 		goto out;
330 out:
331 	return status;
332 }
333 
334 static enum mod_hdcp_status send_no_stored_km(struct mod_hdcp *hdcp,
335 		struct mod_hdcp_event_context *event_ctx,
336 		struct mod_hdcp_transition_input_hdcp2 *input)
337 {
338 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
339 
340 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
341 		event_ctx->unexpected_event = 1;
342 		goto out;
343 	}
344 
345 	if (!mod_hdcp_execute_and_set(mod_hdcp_write_no_stored_km,
346 			&input->no_stored_km_write, &status,
347 			hdcp, "no_stored_km_write"))
348 		goto out;
349 out:
350 	return status;
351 }
352 
353 static enum mod_hdcp_status read_h_prime(struct mod_hdcp *hdcp,
354 		struct mod_hdcp_event_context *event_ctx,
355 		struct mod_hdcp_transition_input_hdcp2 *input)
356 {
357 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
358 
359 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
360 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ &&
361 			event_ctx->event != MOD_HDCP_EVENT_WATCHDOG_TIMEOUT) {
362 		event_ctx->unexpected_event = 1;
363 		goto out;
364 	}
365 
366 	if (!mod_hdcp_execute_and_set(check_h_prime_available,
367 			&input->h_prime_available, &status,
368 			hdcp, "h_prime_available"))
369 		goto out;
370 
371 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_h_prime,
372 			&input->h_prime_read, &status,
373 			hdcp, "h_prime_read"))
374 		goto out;
375 out:
376 	return status;
377 }
378 
379 static enum mod_hdcp_status read_pairing_info_and_validate_h_prime(
380 		struct mod_hdcp *hdcp,
381 		struct mod_hdcp_event_context *event_ctx,
382 		struct mod_hdcp_transition_input_hdcp2 *input)
383 {
384 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
385 
386 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
387 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ &&
388 			event_ctx->event != MOD_HDCP_EVENT_WATCHDOG_TIMEOUT) {
389 		event_ctx->unexpected_event = 1;
390 		goto out;
391 	}
392 
393 	if (!mod_hdcp_execute_and_set(check_pairing_info_available,
394 			&input->pairing_available, &status,
395 			hdcp, "pairing_available"))
396 		goto out;
397 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_pairing_info,
398 			&input->pairing_info_read, &status,
399 			hdcp, "pairing_info_read"))
400 		goto out;
401 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_validate_h_prime,
402 			&input->h_prime_validation, &status,
403 			hdcp, "h_prime_validation"))
404 		goto out;
405 out:
406 	return status;
407 }
408 
409 static enum mod_hdcp_status send_stored_km(struct mod_hdcp *hdcp,
410 		struct mod_hdcp_event_context *event_ctx,
411 		struct mod_hdcp_transition_input_hdcp2 *input)
412 {
413 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
414 
415 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
416 		event_ctx->unexpected_event = 1;
417 		goto out;
418 	}
419 
420 	if (!mod_hdcp_execute_and_set(mod_hdcp_write_stored_km,
421 			&input->stored_km_write, &status,
422 			hdcp, "stored_km_write"))
423 		goto out;
424 out:
425 	return status;
426 }
427 
428 static enum mod_hdcp_status validate_h_prime(struct mod_hdcp *hdcp,
429 		struct mod_hdcp_event_context *event_ctx,
430 		struct mod_hdcp_transition_input_hdcp2 *input)
431 {
432 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
433 
434 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
435 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ &&
436 			event_ctx->event != MOD_HDCP_EVENT_WATCHDOG_TIMEOUT) {
437 		event_ctx->unexpected_event = 1;
438 		goto out;
439 	}
440 
441 	if (!mod_hdcp_execute_and_set(check_h_prime_available,
442 			&input->h_prime_available, &status,
443 			hdcp, "h_prime_available"))
444 		goto out;
445 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_h_prime,
446 			&input->h_prime_read, &status,
447 			hdcp, "h_prime_read"))
448 		goto out;
449 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_validate_h_prime,
450 			&input->h_prime_validation, &status,
451 			hdcp, "h_prime_validation"))
452 		goto out;
453 out:
454 	return status;
455 }
456 
457 static enum mod_hdcp_status locality_check(struct mod_hdcp *hdcp,
458 		struct mod_hdcp_event_context *event_ctx,
459 		struct mod_hdcp_transition_input_hdcp2 *input)
460 {
461 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
462 
463 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
464 		event_ctx->unexpected_event = 1;
465 		goto out;
466 	}
467 
468 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_prepare_lc_init,
469 			&input->lc_init_prepare, &status,
470 			hdcp, "lc_init_prepare"))
471 		goto out;
472 	if (!mod_hdcp_execute_and_set(mod_hdcp_write_lc_init,
473 			&input->lc_init_write, &status,
474 			 hdcp, "lc_init_write"))
475 		goto out;
476 	if (is_dp_hdcp(hdcp))
477 		msleep(16);
478 	else
479 		if (!mod_hdcp_execute_and_set(poll_l_prime_available,
480 				&input->l_prime_available_poll, &status,
481 				hdcp, "l_prime_available_poll"))
482 			goto out;
483 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_l_prime,
484 			&input->l_prime_read, &status,
485 			hdcp, "l_prime_read"))
486 		goto out;
487 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_validate_l_prime,
488 			&input->l_prime_validation, &status,
489 			hdcp, "l_prime_validation"))
490 		goto out;
491 out:
492 	return status;
493 }
494 
495 static enum mod_hdcp_status exchange_ks_and_test_for_repeater(struct mod_hdcp *hdcp,
496 		struct mod_hdcp_event_context *event_ctx,
497 		struct mod_hdcp_transition_input_hdcp2 *input)
498 {
499 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
500 
501 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
502 		event_ctx->unexpected_event = 1;
503 		goto out;
504 	}
505 
506 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_prepare_eks,
507 			&input->eks_prepare, &status,
508 			hdcp, "eks_prepare"))
509 		goto out;
510 	if (!mod_hdcp_execute_and_set(mod_hdcp_write_eks,
511 			&input->eks_write, &status,
512 			hdcp, "eks_write"))
513 		goto out;
514 out:
515 	return status;
516 }
517 
518 static enum mod_hdcp_status enable_encryption(struct mod_hdcp *hdcp,
519 		struct mod_hdcp_event_context *event_ctx,
520 		struct mod_hdcp_transition_input_hdcp2 *input)
521 {
522 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
523 
524 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
525 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ) {
526 		event_ctx->unexpected_event = 1;
527 		goto out;
528 	}
529 	if (event_ctx->event == MOD_HDCP_EVENT_CPIRQ) {
530 		process_rxstatus(hdcp, event_ctx, input, &status);
531 		goto out;
532 	}
533 
534 	if (is_hdmi_dvi_sl_hdcp(hdcp)) {
535 		if (!process_rxstatus(hdcp, event_ctx, input, &status))
536 			goto out;
537 		if (event_ctx->rx_id_list_ready)
538 			goto out;
539 	}
540 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_enable_encryption,
541 			&input->enable_encryption, &status,
542 			hdcp, "enable_encryption"))
543 		goto out;
544 	if (is_dp_mst_hdcp(hdcp)) {
545 		if (!mod_hdcp_execute_and_set(
546 				mod_hdcp_hdcp2_enable_dp_stream_encryption,
547 				&input->stream_encryption_dp, &status,
548 				hdcp, "stream_encryption_dp"))
549 			goto out;
550 	}
551 out:
552 	return status;
553 }
554 
555 static enum mod_hdcp_status authenticated(struct mod_hdcp *hdcp,
556 		struct mod_hdcp_event_context *event_ctx,
557 		struct mod_hdcp_transition_input_hdcp2 *input)
558 {
559 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
560 
561 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
562 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ) {
563 		event_ctx->unexpected_event = 1;
564 		goto out;
565 	}
566 
567 	if (!process_rxstatus(hdcp, event_ctx, input, &status))
568 		goto out;
569 	if (event_ctx->rx_id_list_ready)
570 		goto out;
571 out:
572 	return status;
573 }
574 
575 static enum mod_hdcp_status wait_for_rx_id_list(struct mod_hdcp *hdcp,
576 		struct mod_hdcp_event_context *event_ctx,
577 		struct mod_hdcp_transition_input_hdcp2 *input)
578 {
579 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
580 
581 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
582 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ &&
583 			event_ctx->event != MOD_HDCP_EVENT_WATCHDOG_TIMEOUT) {
584 		event_ctx->unexpected_event = 1;
585 		goto out;
586 	}
587 
588 	if (!process_rxstatus(hdcp, event_ctx, input, &status))
589 		goto out;
590 	if (!event_ctx->rx_id_list_ready) {
591 		status = MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_NOT_READY;
592 		goto out;
593 	}
594 out:
595 	return status;
596 }
597 
598 static enum mod_hdcp_status verify_rx_id_list_and_send_ack(struct mod_hdcp *hdcp,
599 		struct mod_hdcp_event_context *event_ctx,
600 		struct mod_hdcp_transition_input_hdcp2 *input)
601 {
602 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
603 
604 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
605 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ) {
606 		event_ctx->unexpected_event = 1;
607 		goto out;
608 	}
609 	if (event_ctx->event == MOD_HDCP_EVENT_CPIRQ) {
610 		process_rxstatus(hdcp, event_ctx, input, &status);
611 		goto out;
612 	}
613 
614 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_rx_id_list,
615 			&input->rx_id_list_read,
616 			&status, hdcp, "receiver_id_list_read"))
617 		goto out;
618 	if (!mod_hdcp_execute_and_set(check_device_count,
619 			&input->device_count_check,
620 			&status, hdcp, "device_count_check"))
621 		goto out;
622 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_validate_rx_id_list,
623 			&input->rx_id_list_validation,
624 			&status, hdcp, "rx_id_list_validation"))
625 		goto out;
626 	if (!mod_hdcp_execute_and_set(mod_hdcp_write_repeater_auth_ack,
627 			&input->repeater_auth_ack_write,
628 			&status, hdcp, "repeater_auth_ack_write"))
629 		goto out;
630 out:
631 	return status;
632 }
633 
634 static enum mod_hdcp_status send_stream_management(struct mod_hdcp *hdcp,
635 		struct mod_hdcp_event_context *event_ctx,
636 		struct mod_hdcp_transition_input_hdcp2 *input)
637 {
638 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
639 
640 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
641 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ) {
642 		event_ctx->unexpected_event = 1;
643 		goto out;
644 	}
645 	if (event_ctx->event == MOD_HDCP_EVENT_CPIRQ) {
646 		process_rxstatus(hdcp, event_ctx, input, &status);
647 		goto out;
648 	}
649 
650 	if (is_hdmi_dvi_sl_hdcp(hdcp)) {
651 		if (!process_rxstatus(hdcp, event_ctx, input, &status))
652 			goto out;
653 		if (event_ctx->rx_id_list_ready)
654 			goto out;
655 	}
656 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_prepare_stream_management,
657 			&input->prepare_stream_manage,
658 			&status, hdcp, "prepare_stream_manage"))
659 		goto out;
660 
661 	if (!mod_hdcp_execute_and_set(mod_hdcp_write_stream_manage,
662 			&input->stream_manage_write,
663 			&status, hdcp, "stream_manage_write"))
664 		goto out;
665 out:
666 	return status;
667 }
668 
669 static enum mod_hdcp_status validate_stream_ready(struct mod_hdcp *hdcp,
670 		struct mod_hdcp_event_context *event_ctx,
671 		struct mod_hdcp_transition_input_hdcp2 *input)
672 {
673 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
674 
675 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
676 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ &&
677 			event_ctx->event != MOD_HDCP_EVENT_WATCHDOG_TIMEOUT) {
678 		event_ctx->unexpected_event = 1;
679 		goto out;
680 	}
681 	if (event_ctx->event == MOD_HDCP_EVENT_CPIRQ) {
682 		process_rxstatus(hdcp, event_ctx, input, &status);
683 		goto out;
684 	}
685 
686 	if (is_hdmi_dvi_sl_hdcp(hdcp)) {
687 		if (!process_rxstatus(hdcp, event_ctx, input, &status))
688 			goto out;
689 		if (event_ctx->rx_id_list_ready) {
690 			goto out;
691 		}
692 	}
693 	if (is_hdmi_dvi_sl_hdcp(hdcp))
694 		if (!mod_hdcp_execute_and_set(check_stream_ready_available,
695 				&input->stream_ready_available,
696 				&status, hdcp, "stream_ready_available"))
697 			goto out;
698 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_stream_ready,
699 			&input->stream_ready_read,
700 			&status, hdcp, "stream_ready_read"))
701 		goto out;
702 	if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp2_validate_stream_ready,
703 			&input->stream_ready_validation,
704 			&status, hdcp, "stream_ready_validation"))
705 		goto out;
706 
707 out:
708 	return status;
709 }
710 
711 static enum mod_hdcp_status determine_rx_hdcp_capable_dp(struct mod_hdcp *hdcp,
712 		struct mod_hdcp_event_context *event_ctx,
713 		struct mod_hdcp_transition_input_hdcp2 *input)
714 {
715 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
716 
717 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
718 		event_ctx->unexpected_event = 1;
719 		goto out;
720 	}
721 
722 	if (!mod_hdcp_execute_and_set(mod_hdcp_read_rxcaps,
723 			&input->rx_caps_read_dp,
724 			&status, hdcp, "rx_caps_read_dp"))
725 		goto out;
726 	if (!mod_hdcp_execute_and_set(check_hdcp2_capable,
727 			&input->hdcp2_capable_check, &status,
728 			hdcp, "hdcp2_capable_check"))
729 		goto out;
730 out:
731 	return status;
732 }
733 
734 static enum mod_hdcp_status send_content_stream_type_dp(struct mod_hdcp *hdcp,
735 		struct mod_hdcp_event_context *event_ctx,
736 		struct mod_hdcp_transition_input_hdcp2 *input)
737 {
738 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
739 
740 	if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK &&
741 			event_ctx->event != MOD_HDCP_EVENT_CPIRQ) {
742 		event_ctx->unexpected_event = 1;
743 		goto out;
744 	}
745 
746 	if (!process_rxstatus(hdcp, event_ctx, input, &status))
747 		goto out;
748 	if (!mod_hdcp_execute_and_set(mod_hdcp_write_content_type,
749 			&input->content_stream_type_write, &status,
750 			hdcp, "content_stream_type_write"))
751 		goto out;
752 out:
753 	return status;
754 }
755 
756 enum mod_hdcp_status mod_hdcp_hdcp2_execution(struct mod_hdcp *hdcp,
757 	struct mod_hdcp_event_context *event_ctx,
758 	struct mod_hdcp_transition_input_hdcp2 *input)
759 {
760 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
761 
762 	switch (current_state(hdcp)) {
763 	case H2_A0_KNOWN_HDCP2_CAPABLE_RX:
764 		status = known_hdcp2_capable_rx(hdcp, event_ctx, input);
765 		break;
766 	case H2_A1_SEND_AKE_INIT:
767 		status = send_ake_init(hdcp, event_ctx, input);
768 		break;
769 	case H2_A1_VALIDATE_AKE_CERT:
770 		status = validate_ake_cert(hdcp, event_ctx, input);
771 		break;
772 	case H2_A1_SEND_NO_STORED_KM:
773 		status = send_no_stored_km(hdcp, event_ctx, input);
774 		break;
775 	case H2_A1_READ_H_PRIME:
776 		status = read_h_prime(hdcp, event_ctx, input);
777 		break;
778 	case H2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME:
779 		status = read_pairing_info_and_validate_h_prime(hdcp,
780 				event_ctx, input);
781 		break;
782 	case H2_A1_SEND_STORED_KM:
783 		status = send_stored_km(hdcp, event_ctx, input);
784 		break;
785 	case H2_A1_VALIDATE_H_PRIME:
786 		status = validate_h_prime(hdcp, event_ctx, input);
787 		break;
788 	case H2_A2_LOCALITY_CHECK:
789 		status = locality_check(hdcp, event_ctx, input);
790 		break;
791 	case H2_A3_EXCHANGE_KS_AND_TEST_FOR_REPEATER:
792 		status = exchange_ks_and_test_for_repeater(hdcp, event_ctx, input);
793 		break;
794 	case H2_ENABLE_ENCRYPTION:
795 		status = enable_encryption(hdcp, event_ctx, input);
796 		break;
797 	case H2_A5_AUTHENTICATED:
798 		status = authenticated(hdcp, event_ctx, input);
799 		break;
800 	case H2_A6_WAIT_FOR_RX_ID_LIST:
801 		status = wait_for_rx_id_list(hdcp, event_ctx, input);
802 		break;
803 	case H2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK:
804 		status = verify_rx_id_list_and_send_ack(hdcp, event_ctx, input);
805 		break;
806 	case H2_A9_SEND_STREAM_MANAGEMENT:
807 		status = send_stream_management(hdcp, event_ctx, input);
808 		break;
809 	case H2_A9_VALIDATE_STREAM_READY:
810 		status = validate_stream_ready(hdcp, event_ctx, input);
811 		break;
812 	default:
813 		status = MOD_HDCP_STATUS_INVALID_STATE;
814 		break;
815 	}
816 
817 	return status;
818 }
819 
820 enum mod_hdcp_status mod_hdcp_hdcp2_dp_execution(struct mod_hdcp *hdcp,
821 	struct mod_hdcp_event_context *event_ctx,
822 	struct mod_hdcp_transition_input_hdcp2 *input)
823 {
824 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
825 
826 	switch (current_state(hdcp)) {
827 	case D2_A0_DETERMINE_RX_HDCP_CAPABLE:
828 		status = determine_rx_hdcp_capable_dp(hdcp, event_ctx, input);
829 		break;
830 	case D2_A1_SEND_AKE_INIT:
831 		status = send_ake_init(hdcp, event_ctx, input);
832 		break;
833 	case D2_A1_VALIDATE_AKE_CERT:
834 		status = validate_ake_cert(hdcp, event_ctx, input);
835 		break;
836 	case D2_A1_SEND_NO_STORED_KM:
837 		status = send_no_stored_km(hdcp, event_ctx, input);
838 		break;
839 	case D2_A1_READ_H_PRIME:
840 		status = read_h_prime(hdcp, event_ctx, input);
841 		break;
842 	case D2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME:
843 		status = read_pairing_info_and_validate_h_prime(hdcp,
844 				event_ctx, input);
845 		break;
846 	case D2_A1_SEND_STORED_KM:
847 		status = send_stored_km(hdcp, event_ctx, input);
848 		break;
849 	case D2_A1_VALIDATE_H_PRIME:
850 		status = validate_h_prime(hdcp, event_ctx, input);
851 		break;
852 	case D2_A2_LOCALITY_CHECK:
853 		status = locality_check(hdcp, event_ctx, input);
854 		break;
855 	case D2_A34_EXCHANGE_KS_AND_TEST_FOR_REPEATER:
856 		status = exchange_ks_and_test_for_repeater(hdcp,
857 				event_ctx, input);
858 		break;
859 	case D2_SEND_CONTENT_STREAM_TYPE:
860 		status = send_content_stream_type_dp(hdcp, event_ctx, input);
861 		break;
862 	case D2_ENABLE_ENCRYPTION:
863 		status = enable_encryption(hdcp, event_ctx, input);
864 		break;
865 	case D2_A5_AUTHENTICATED:
866 		status = authenticated(hdcp, event_ctx, input);
867 		break;
868 	case D2_A6_WAIT_FOR_RX_ID_LIST:
869 		status = wait_for_rx_id_list(hdcp, event_ctx, input);
870 		break;
871 	case D2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK:
872 		status = verify_rx_id_list_and_send_ack(hdcp, event_ctx, input);
873 		break;
874 	case D2_A9_SEND_STREAM_MANAGEMENT:
875 		status = send_stream_management(hdcp, event_ctx, input);
876 		break;
877 	case D2_A9_VALIDATE_STREAM_READY:
878 		status = validate_stream_ready(hdcp, event_ctx, input);
879 		break;
880 	default:
881 		status = MOD_HDCP_STATUS_INVALID_STATE;
882 		break;
883 	}
884 
885 	return status;
886 }
887