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