hdcp.h 18.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright 2019 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: AMD
 *
 */

#ifndef HDCP_H_
#define HDCP_H_

#include "mod_hdcp.h"
#include "hdcp_log.h"

32 33 34
#include <drm/drm_hdcp.h>
#include <drm/drm_dp_helper.h>

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
enum mod_hdcp_trans_input_result {
	UNKNOWN = 0,
	PASS,
	FAIL
};

struct mod_hdcp_transition_input_hdcp1 {
	uint8_t bksv_read;
	uint8_t bksv_validation;
	uint8_t create_session;
	uint8_t an_write;
	uint8_t aksv_write;
	uint8_t ainfo_write;
	uint8_t bcaps_read;
	uint8_t r0p_read;
	uint8_t rx_validation;
	uint8_t encryption;
	uint8_t link_maintenance;
	uint8_t ready_check;
	uint8_t bstatus_read;
	uint8_t max_cascade_check;
	uint8_t max_devs_check;
	uint8_t device_count_check;
	uint8_t ksvlist_read;
	uint8_t vp_read;
	uint8_t ksvlist_vp_validation;

	uint8_t hdcp_capable_dp;
	uint8_t binfo_read_dp;
	uint8_t r0p_available_dp;
65
	uint8_t link_integrity_check;
66 67 68 69
	uint8_t reauth_request_check;
	uint8_t stream_encryption_dp;
};

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
struct mod_hdcp_transition_input_hdcp2 {
	uint8_t hdcp2version_read;
	uint8_t hdcp2_capable_check;
	uint8_t create_session;
	uint8_t ake_init_prepare;
	uint8_t ake_init_write;
	uint8_t rxstatus_read;
	uint8_t ake_cert_available;
	uint8_t ake_cert_read;
	uint8_t ake_cert_validation;
	uint8_t stored_km_write;
	uint8_t no_stored_km_write;
	uint8_t h_prime_available;
	uint8_t h_prime_read;
	uint8_t pairing_available;
	uint8_t pairing_info_read;
	uint8_t h_prime_validation;
	uint8_t lc_init_prepare;
	uint8_t lc_init_write;
	uint8_t l_prime_available_poll;
	uint8_t l_prime_read;
	uint8_t l_prime_validation;
	uint8_t eks_prepare;
	uint8_t eks_write;
	uint8_t enable_encryption;
	uint8_t reauth_request_check;
	uint8_t rx_id_list_read;
	uint8_t device_count_check;
	uint8_t rx_id_list_validation;
	uint8_t repeater_auth_ack_write;
	uint8_t prepare_stream_manage;
	uint8_t stream_manage_write;
	uint8_t stream_ready_available;
	uint8_t stream_ready_read;
	uint8_t stream_ready_validation;

	uint8_t rx_caps_read_dp;
	uint8_t content_stream_type_write;
	uint8_t link_integrity_check_dp;
	uint8_t stream_encryption_dp;
};

112 113
union mod_hdcp_transition_input {
	struct mod_hdcp_transition_input_hdcp1 hdcp1;
114
	struct mod_hdcp_transition_input_hdcp2 hdcp2;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
};

struct mod_hdcp_message_hdcp1 {
	uint8_t		an[8];
	uint8_t		aksv[5];
	uint8_t		ainfo;
	uint8_t		bksv[5];
	uint16_t	r0p;
	uint8_t		bcaps;
	uint16_t	bstatus;
	uint8_t		ksvlist[635];
	uint16_t	ksvlist_size;
	uint8_t		vp[20];

	uint16_t	binfo_dp;
};

132 133 134
struct mod_hdcp_message_hdcp2 {
	uint8_t		hdcp2version_hdmi;
	uint8_t		rxcaps_dp[3];
135
	uint8_t		rxstatus[2];
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

	uint8_t		ake_init[12];
	uint8_t		ake_cert[534];
	uint8_t		ake_no_stored_km[129];
	uint8_t		ake_stored_km[33];
	uint8_t		ake_h_prime[33];
	uint8_t		ake_pairing_info[17];
	uint8_t		lc_init[9];
	uint8_t		lc_l_prime[33];
	uint8_t		ske_eks[25];
	uint8_t		rx_id_list[177]; // 22 + 5 * 31
	uint16_t	rx_id_list_size;
	uint8_t		repeater_auth_ack[17];
	uint8_t		repeater_auth_stream_manage[68]; // 6 + 2 * 31
	uint16_t	stream_manage_size;
	uint8_t		repeater_auth_stream_ready[33];
152
	uint8_t		rxstatus_dp;
153 154 155
	uint8_t		content_stream_type_dp[2];
};

156 157
union mod_hdcp_message {
	struct mod_hdcp_message_hdcp1 hdcp1;
158
	struct mod_hdcp_message_hdcp2 hdcp2;
159 160 161 162 163 164 165 166 167 168 169 170
};

struct mod_hdcp_auth_counters {
	uint8_t stream_management_retry_count;
};

/* contains values per connection */
struct mod_hdcp_connection {
	struct mod_hdcp_link link;
	struct mod_hdcp_display displays[MAX_NUM_OF_DISPLAYS];
	uint8_t is_repeater;
	uint8_t is_km_stored;
171
	uint8_t is_hdcp1_revoked;
172
	uint8_t is_hdcp2_revoked;
173 174
	struct mod_hdcp_trace trace;
	uint8_t hdcp1_retry_count;
175
	uint8_t hdcp2_retry_count;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
};

/* contains values per authentication cycle */
struct mod_hdcp_authentication {
	uint32_t id;
	union mod_hdcp_message msg;
	union mod_hdcp_transition_input trans_input;
	struct mod_hdcp_auth_counters count;
};

/* contains values per state change */
struct mod_hdcp_state {
	uint8_t id;
	uint32_t stay_count;
};

/* per event in a state */
struct mod_hdcp_event_context {
	enum mod_hdcp_event event;
	uint8_t rx_id_list_ready;
	uint8_t unexpected_event;
};

struct mod_hdcp {
	/* per link */
	struct mod_hdcp_config config;
	/* per connection */
	struct mod_hdcp_connection connection;
	/* per authentication attempt */
	struct mod_hdcp_authentication auth;
	/* per state in an authentication */
	struct mod_hdcp_state state;
	/* reserved memory buffer */
	uint8_t buf[2025];
};

enum mod_hdcp_initial_state_id {
	HDCP_UNINITIALIZED = 0x0,
	HDCP_INITIAL_STATE_START = HDCP_UNINITIALIZED,
	HDCP_INITIALIZED,
	HDCP_CP_NOT_DESIRED,
	HDCP_INITIAL_STATE_END = HDCP_CP_NOT_DESIRED
};

enum mod_hdcp_hdcp1_state_id {
	HDCP1_STATE_START = HDCP_INITIAL_STATE_END,
	H1_A0_WAIT_FOR_ACTIVE_RX,
	H1_A1_EXCHANGE_KSVS,
	H1_A2_COMPUTATIONS_A3_VALIDATE_RX_A6_TEST_FOR_REPEATER,
225
	H1_A45_AUTHENTICATED,
226 227 228 229 230 231 232 233 234 235 236
	H1_A8_WAIT_FOR_READY,
	H1_A9_READ_KSV_LIST,
	HDCP1_STATE_END = H1_A9_READ_KSV_LIST
};

enum mod_hdcp_hdcp1_dp_state_id {
	HDCP1_DP_STATE_START = HDCP1_STATE_END,
	D1_A0_DETERMINE_RX_HDCP_CAPABLE,
	D1_A1_EXCHANGE_KSVS,
	D1_A23_WAIT_FOR_R0_PRIME,
	D1_A2_COMPUTATIONS_A3_VALIDATE_RX_A5_TEST_FOR_REPEATER,
237
	D1_A4_AUTHENTICATED,
238 239 240 241 242
	D1_A6_WAIT_FOR_READY,
	D1_A7_READ_KSV_LIST,
	HDCP1_DP_STATE_END = D1_A7_READ_KSV_LIST,
};

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
enum mod_hdcp_hdcp2_state_id {
	HDCP2_STATE_START = HDCP1_DP_STATE_END,
	H2_A0_KNOWN_HDCP2_CAPABLE_RX,
	H2_A1_SEND_AKE_INIT,
	H2_A1_VALIDATE_AKE_CERT,
	H2_A1_SEND_NO_STORED_KM,
	H2_A1_READ_H_PRIME,
	H2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME,
	H2_A1_SEND_STORED_KM,
	H2_A1_VALIDATE_H_PRIME,
	H2_A2_LOCALITY_CHECK,
	H2_A3_EXCHANGE_KS_AND_TEST_FOR_REPEATER,
	H2_ENABLE_ENCRYPTION,
	H2_A5_AUTHENTICATED,
	H2_A6_WAIT_FOR_RX_ID_LIST,
	H2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK,
	H2_A9_SEND_STREAM_MANAGEMENT,
	H2_A9_VALIDATE_STREAM_READY,
	HDCP2_STATE_END = H2_A9_VALIDATE_STREAM_READY,
};

enum mod_hdcp_hdcp2_dp_state_id {
	HDCP2_DP_STATE_START = HDCP2_STATE_END,
	D2_A0_DETERMINE_RX_HDCP_CAPABLE,
	D2_A1_SEND_AKE_INIT,
	D2_A1_VALIDATE_AKE_CERT,
	D2_A1_SEND_NO_STORED_KM,
	D2_A1_READ_H_PRIME,
	D2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME,
	D2_A1_SEND_STORED_KM,
	D2_A1_VALIDATE_H_PRIME,
	D2_A2_LOCALITY_CHECK,
	D2_A34_EXCHANGE_KS_AND_TEST_FOR_REPEATER,
	D2_SEND_CONTENT_STREAM_TYPE,
	D2_ENABLE_ENCRYPTION,
	D2_A5_AUTHENTICATED,
	D2_A6_WAIT_FOR_RX_ID_LIST,
	D2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK,
	D2_A9_SEND_STREAM_MANAGEMENT,
	D2_A9_VALIDATE_STREAM_READY,
	HDCP2_DP_STATE_END = D2_A9_VALIDATE_STREAM_READY,
	HDCP_STATE_END = HDCP2_DP_STATE_END,
};

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/* hdcp1 executions and transitions */
typedef enum mod_hdcp_status (*mod_hdcp_action)(struct mod_hdcp *hdcp);
uint8_t mod_hdcp_execute_and_set(
		mod_hdcp_action func, uint8_t *flag,
		enum mod_hdcp_status *status, struct mod_hdcp *hdcp, char *str);
enum mod_hdcp_status mod_hdcp_hdcp1_execution(struct mod_hdcp *hdcp,
	struct mod_hdcp_event_context *event_ctx,
	struct mod_hdcp_transition_input_hdcp1 *input);
enum mod_hdcp_status mod_hdcp_hdcp1_dp_execution(struct mod_hdcp *hdcp,
	struct mod_hdcp_event_context *event_ctx,
	struct mod_hdcp_transition_input_hdcp1 *input);
enum mod_hdcp_status mod_hdcp_hdcp1_transition(struct mod_hdcp *hdcp,
	struct mod_hdcp_event_context *event_ctx,
	struct mod_hdcp_transition_input_hdcp1 *input,
	struct mod_hdcp_output *output);
enum mod_hdcp_status mod_hdcp_hdcp1_dp_transition(struct mod_hdcp *hdcp,
	struct mod_hdcp_event_context *event_ctx,
	struct mod_hdcp_transition_input_hdcp1 *input,
	struct mod_hdcp_output *output);

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/* hdcp2 executions and transitions */
enum mod_hdcp_status mod_hdcp_hdcp2_execution(struct mod_hdcp *hdcp,
	struct mod_hdcp_event_context *event_ctx,
	struct mod_hdcp_transition_input_hdcp2 *input);
enum mod_hdcp_status mod_hdcp_hdcp2_dp_execution(struct mod_hdcp *hdcp,
	struct mod_hdcp_event_context *event_ctx,
	struct mod_hdcp_transition_input_hdcp2 *input);
enum mod_hdcp_status mod_hdcp_hdcp2_transition(struct mod_hdcp *hdcp,
	struct mod_hdcp_event_context *event_ctx,
	struct mod_hdcp_transition_input_hdcp2 *input,
	struct mod_hdcp_output *output);
enum mod_hdcp_status mod_hdcp_hdcp2_dp_transition(struct mod_hdcp *hdcp,
	struct mod_hdcp_event_context *event_ctx,
	struct mod_hdcp_transition_input_hdcp2 *input,
	struct mod_hdcp_output *output);

323 324 325 326 327 328
/* log functions */
void mod_hdcp_dump_binary_message(uint8_t *msg, uint32_t msg_size,
		uint8_t *buf, uint32_t buf_size);
/* TODO: add adjustment log */

/* psp functions */
329 330 331 332
enum mod_hdcp_status mod_hdcp_add_display_to_topology(
		struct mod_hdcp *hdcp, uint8_t index);
enum mod_hdcp_status mod_hdcp_remove_display_from_topology(
		struct mod_hdcp *hdcp, uint8_t index);
333 334 335 336 337 338 339 340 341 342
enum mod_hdcp_status mod_hdcp_hdcp1_create_session(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp1_destroy_session(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp1_validate_rx(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp1_enable_encryption(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp1_validate_ksvlist_vp(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp1_enable_dp_stream_encryption(
	struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp1_link_maintenance(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp1_get_link_encryption_status(struct mod_hdcp *hdcp,
							       enum mod_hdcp_encryption_status *encryption_status);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
enum mod_hdcp_status mod_hdcp_hdcp2_create_session(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_destroy_session(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_prepare_ake_init(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_validate_ake_cert(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_validate_h_prime(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_prepare_lc_init(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_validate_l_prime(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_prepare_eks(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_enable_encryption(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_validate_rx_id_list(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_enable_dp_stream_encryption(
		struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_prepare_stream_management(
		struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_validate_stream_ready(
		struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_hdcp2_get_link_encryption_status(struct mod_hdcp *hdcp,
							       enum mod_hdcp_encryption_status *encryption_status);

362 363 364 365 366 367 368 369 370 371 372
/* ddc functions */
enum mod_hdcp_status mod_hdcp_read_bksv(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_bcaps(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_bstatus(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_r0p(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_ksvlist(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_vp(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_binfo(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_aksv(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_ainfo(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_an(struct mod_hdcp *hdcp);
373
enum mod_hdcp_status mod_hdcp_read_hdcp2version(struct mod_hdcp *hdcp);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
enum mod_hdcp_status mod_hdcp_read_rxcaps(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_rxstatus(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_ake_cert(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_h_prime(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_pairing_info(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_l_prime(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_rx_id_list(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_read_stream_ready(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_ake_init(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_no_stored_km(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_stored_km(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_lc_init(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_eks(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_repeater_auth_ack(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_stream_manage(struct mod_hdcp *hdcp);
enum mod_hdcp_status mod_hdcp_write_content_type(struct mod_hdcp *hdcp);

/* hdcp version helpers */
static inline uint8_t is_dp_hdcp(struct mod_hdcp *hdcp)
{
	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DP ||
			hdcp->connection.link.mode == MOD_HDCP_MODE_DP_MST);
}

static inline uint8_t is_dp_mst_hdcp(struct mod_hdcp *hdcp)
{
	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DP_MST);
}

static inline uint8_t is_hdmi_dvi_sl_hdcp(struct mod_hdcp *hdcp)
{
	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DEFAULT);
}

/* hdcp state helpers */
static inline uint8_t current_state(struct mod_hdcp *hdcp)
{
	return hdcp->state.id;
}

static inline void set_state_id(struct mod_hdcp *hdcp,
		struct mod_hdcp_output *output, uint8_t id)
{
	memset(&hdcp->state, 0, sizeof(hdcp->state));
	hdcp->state.id = id;
	/* callback timer should be reset per state */
	output->callback_stop = 1;
	output->watchdog_timer_stop = 1;
	HDCP_NEXT_STATE_TRACE(hdcp, id, output);
}

static inline uint8_t is_in_hdcp1_states(struct mod_hdcp *hdcp)
{
	return (current_state(hdcp) > HDCP1_STATE_START &&
			current_state(hdcp) <= HDCP1_STATE_END);
}

static inline uint8_t is_in_hdcp1_dp_states(struct mod_hdcp *hdcp)
{
	return (current_state(hdcp) > HDCP1_DP_STATE_START &&
			current_state(hdcp) <= HDCP1_DP_STATE_END);
}

437 438 439 440 441 442 443 444 445 446 447 448
static inline uint8_t is_in_hdcp2_states(struct mod_hdcp *hdcp)
{
	return (current_state(hdcp) > HDCP2_STATE_START &&
			current_state(hdcp) <= HDCP2_STATE_END);
}

static inline uint8_t is_in_hdcp2_dp_states(struct mod_hdcp *hdcp)
{
	return (current_state(hdcp) > HDCP2_DP_STATE_START &&
			current_state(hdcp) <= HDCP2_DP_STATE_END);
}

449 450 451 452 453
static inline uint8_t is_hdcp1(struct mod_hdcp *hdcp)
{
	return (is_in_hdcp1_states(hdcp) || is_in_hdcp1_dp_states(hdcp));
}

454 455 456 457 458
static inline uint8_t is_hdcp2(struct mod_hdcp *hdcp)
{
	return (is_in_hdcp2_states(hdcp) || is_in_hdcp2_dp_states(hdcp));
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
static inline uint8_t is_in_cp_not_desired_state(struct mod_hdcp *hdcp)
{
	return current_state(hdcp) == HDCP_CP_NOT_DESIRED;
}

static inline uint8_t is_in_initialized_state(struct mod_hdcp *hdcp)
{
	return current_state(hdcp) == HDCP_INITIALIZED;
}

/* transition operation helpers */
static inline void increment_stay_counter(struct mod_hdcp *hdcp)
{
	hdcp->state.stay_count++;
}

static inline void fail_and_restart_in_ms(uint16_t time,
		enum mod_hdcp_status *status,
		struct mod_hdcp_output *output)
{
	output->callback_needed = 1;
	output->callback_delay = time;
	output->watchdog_timer_needed = 0;
	output->watchdog_timer_delay = 0;
	*status = MOD_HDCP_STATUS_RESET_NEEDED;
}

static inline void callback_in_ms(uint16_t time, struct mod_hdcp_output *output)
{
	output->callback_needed = 1;
	output->callback_delay = time;
}

static inline void set_watchdog_in_ms(struct mod_hdcp *hdcp, uint16_t time,
		struct mod_hdcp_output *output)
{
	output->watchdog_timer_needed = 1;
	output->watchdog_timer_delay = time;
}

/* connection topology helpers */
static inline uint8_t is_display_active(struct mod_hdcp_display *display)
{
	return display->state >= MOD_HDCP_DISPLAY_ACTIVE;
}

static inline uint8_t is_display_added(struct mod_hdcp_display *display)
{
	return display->state >= MOD_HDCP_DISPLAY_ACTIVE_AND_ADDED;
}

static inline uint8_t is_display_encryption_enabled(struct mod_hdcp_display *display)
{
	return display->state >= MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
}

static inline uint8_t get_active_display_count(struct mod_hdcp *hdcp)
{
	uint8_t added_count = 0;
	uint8_t i;

	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
		if (is_display_active(&hdcp->connection.displays[i]))
			added_count++;
	return added_count;
}

static inline uint8_t get_added_display_count(struct mod_hdcp *hdcp)
{
	uint8_t added_count = 0;
	uint8_t i;

	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
		if (is_display_added(&hdcp->connection.displays[i]))
			added_count++;
	return added_count;
}

static inline struct mod_hdcp_display *get_first_added_display(
		struct mod_hdcp *hdcp)
{
	uint8_t i;
	struct mod_hdcp_display *display = NULL;

	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
		if (is_display_added(&hdcp->connection.displays[i])) {
			display = &hdcp->connection.displays[i];
			break;
		}
	return display;
}

static inline struct mod_hdcp_display *get_active_display_at_index(
		struct mod_hdcp *hdcp, uint8_t index)
{
	uint8_t i;
	struct mod_hdcp_display *display = NULL;

	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
		if (hdcp->connection.displays[i].index == index &&
				is_display_active(&hdcp->connection.displays[i])) {
			display = &hdcp->connection.displays[i];
			break;
		}
	return display;
}

static inline struct mod_hdcp_display *get_empty_display_container(
		struct mod_hdcp *hdcp)
{
	uint8_t i;
	struct mod_hdcp_display *display = NULL;

	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
		if (!is_display_active(&hdcp->connection.displays[i])) {
			display = &hdcp->connection.displays[i];
			break;
		}
	return display;
}

static inline void reset_retry_counts(struct mod_hdcp *hdcp)
{
	hdcp->connection.hdcp1_retry_count = 0;
583
	hdcp->connection.hdcp2_retry_count = 0;
584 585 586
}

#endif /* HDCP_H_ */