1 /*
2  * Copyright 2019 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 #ifndef HDCP_H_
27 #define HDCP_H_
28 
29 #include "mod_hdcp.h"
30 #include "hdcp_log.h"
31 
32 #include <drm/drm_hdcp.h>
33 #include <drm/drm_dp_helper.h>
34 
35 enum mod_hdcp_trans_input_result {
36 	UNKNOWN = 0,
37 	PASS,
38 	FAIL
39 };
40 
41 struct mod_hdcp_transition_input_hdcp1 {
42 	uint8_t bksv_read;
43 	uint8_t bksv_validation;
44 	uint8_t create_session;
45 	uint8_t an_write;
46 	uint8_t aksv_write;
47 	uint8_t ainfo_write;
48 	uint8_t bcaps_read;
49 	uint8_t r0p_read;
50 	uint8_t rx_validation;
51 	uint8_t encryption;
52 	uint8_t link_maintenance;
53 	uint8_t ready_check;
54 	uint8_t bstatus_read;
55 	uint8_t max_cascade_check;
56 	uint8_t max_devs_check;
57 	uint8_t device_count_check;
58 	uint8_t ksvlist_read;
59 	uint8_t vp_read;
60 	uint8_t ksvlist_vp_validation;
61 
62 	uint8_t hdcp_capable_dp;
63 	uint8_t binfo_read_dp;
64 	uint8_t r0p_available_dp;
65 	uint8_t link_integrity_check;
66 	uint8_t reauth_request_check;
67 	uint8_t stream_encryption_dp;
68 };
69 
70 struct mod_hdcp_transition_input_hdcp2 {
71 	uint8_t hdcp2version_read;
72 	uint8_t hdcp2_capable_check;
73 	uint8_t create_session;
74 	uint8_t ake_init_prepare;
75 	uint8_t ake_init_write;
76 	uint8_t rxstatus_read;
77 	uint8_t ake_cert_available;
78 	uint8_t ake_cert_read;
79 	uint8_t ake_cert_validation;
80 	uint8_t stored_km_write;
81 	uint8_t no_stored_km_write;
82 	uint8_t h_prime_available;
83 	uint8_t h_prime_read;
84 	uint8_t pairing_available;
85 	uint8_t pairing_info_read;
86 	uint8_t h_prime_validation;
87 	uint8_t lc_init_prepare;
88 	uint8_t lc_init_write;
89 	uint8_t l_prime_available_poll;
90 	uint8_t l_prime_read;
91 	uint8_t l_prime_validation;
92 	uint8_t eks_prepare;
93 	uint8_t eks_write;
94 	uint8_t enable_encryption;
95 	uint8_t reauth_request_check;
96 	uint8_t rx_id_list_read;
97 	uint8_t device_count_check;
98 	uint8_t rx_id_list_validation;
99 	uint8_t repeater_auth_ack_write;
100 	uint8_t prepare_stream_manage;
101 	uint8_t stream_manage_write;
102 	uint8_t stream_ready_available;
103 	uint8_t stream_ready_read;
104 	uint8_t stream_ready_validation;
105 
106 	uint8_t rx_caps_read_dp;
107 	uint8_t content_stream_type_write;
108 	uint8_t link_integrity_check_dp;
109 	uint8_t stream_encryption_dp;
110 };
111 
112 union mod_hdcp_transition_input {
113 	struct mod_hdcp_transition_input_hdcp1 hdcp1;
114 	struct mod_hdcp_transition_input_hdcp2 hdcp2;
115 };
116 
117 struct mod_hdcp_message_hdcp1 {
118 	uint8_t		an[8];
119 	uint8_t		aksv[5];
120 	uint8_t		ainfo;
121 	uint8_t		bksv[5];
122 	uint16_t	r0p;
123 	uint8_t		bcaps;
124 	uint16_t	bstatus;
125 	uint8_t		ksvlist[635];
126 	uint16_t	ksvlist_size;
127 	uint8_t		vp[20];
128 
129 	uint16_t	binfo_dp;
130 };
131 
132 struct mod_hdcp_message_hdcp2 {
133 	uint8_t		hdcp2version_hdmi;
134 	uint8_t		rxcaps_dp[3];
135 	uint8_t		rxstatus[2];
136 
137 	uint8_t		ake_init[12];
138 	uint8_t		ake_cert[534];
139 	uint8_t		ake_no_stored_km[129];
140 	uint8_t		ake_stored_km[33];
141 	uint8_t		ake_h_prime[33];
142 	uint8_t		ake_pairing_info[17];
143 	uint8_t		lc_init[9];
144 	uint8_t		lc_l_prime[33];
145 	uint8_t		ske_eks[25];
146 	uint8_t		rx_id_list[177]; // 22 + 5 * 31
147 	uint16_t	rx_id_list_size;
148 	uint8_t		repeater_auth_ack[17];
149 	uint8_t		repeater_auth_stream_manage[68]; // 6 + 2 * 31
150 	uint16_t	stream_manage_size;
151 	uint8_t		repeater_auth_stream_ready[33];
152 	uint8_t		rxstatus_dp;
153 	uint8_t		content_stream_type_dp[2];
154 };
155 
156 union mod_hdcp_message {
157 	struct mod_hdcp_message_hdcp1 hdcp1;
158 	struct mod_hdcp_message_hdcp2 hdcp2;
159 };
160 
161 struct mod_hdcp_auth_counters {
162 	uint8_t stream_management_retry_count;
163 };
164 
165 /* contains values per connection */
166 struct mod_hdcp_connection {
167 	struct mod_hdcp_link link;
168 	uint8_t is_repeater;
169 	uint8_t is_km_stored;
170 	uint8_t is_hdcp1_revoked;
171 	uint8_t is_hdcp2_revoked;
172 	struct mod_hdcp_trace trace;
173 	uint8_t hdcp1_retry_count;
174 	uint8_t hdcp2_retry_count;
175 };
176 
177 /* contains values per authentication cycle */
178 struct mod_hdcp_authentication {
179 	uint32_t id;
180 	union mod_hdcp_message msg;
181 	union mod_hdcp_transition_input trans_input;
182 	struct mod_hdcp_auth_counters count;
183 };
184 
185 /* contains values per state change */
186 struct mod_hdcp_state {
187 	uint8_t id;
188 	uint32_t stay_count;
189 };
190 
191 /* per event in a state */
192 struct mod_hdcp_event_context {
193 	enum mod_hdcp_event event;
194 	uint8_t rx_id_list_ready;
195 	uint8_t unexpected_event;
196 };
197 
198 struct mod_hdcp {
199 	/* per link */
200 	struct mod_hdcp_config config;
201 	/* per connection */
202 	struct mod_hdcp_connection connection;
203 	/* per displays */
204 	struct mod_hdcp_display displays[MAX_NUM_OF_DISPLAYS];
205 	/* per authentication attempt */
206 	struct mod_hdcp_authentication auth;
207 	/* per state in an authentication */
208 	struct mod_hdcp_state state;
209 	/* reserved memory buffer */
210 	uint8_t buf[2025];
211 };
212 
213 enum mod_hdcp_initial_state_id {
214 	HDCP_UNINITIALIZED = 0x0,
215 	HDCP_INITIAL_STATE_START = HDCP_UNINITIALIZED,
216 	HDCP_INITIALIZED,
217 	HDCP_CP_NOT_DESIRED,
218 	HDCP_INITIAL_STATE_END = HDCP_CP_NOT_DESIRED
219 };
220 
221 enum mod_hdcp_hdcp1_state_id {
222 	HDCP1_STATE_START = HDCP_INITIAL_STATE_END,
223 	H1_A0_WAIT_FOR_ACTIVE_RX,
224 	H1_A1_EXCHANGE_KSVS,
225 	H1_A2_COMPUTATIONS_A3_VALIDATE_RX_A6_TEST_FOR_REPEATER,
226 	H1_A45_AUTHENTICATED,
227 	H1_A8_WAIT_FOR_READY,
228 	H1_A9_READ_KSV_LIST,
229 	HDCP1_STATE_END = H1_A9_READ_KSV_LIST
230 };
231 
232 enum mod_hdcp_hdcp1_dp_state_id {
233 	HDCP1_DP_STATE_START = HDCP1_STATE_END,
234 	D1_A0_DETERMINE_RX_HDCP_CAPABLE,
235 	D1_A1_EXCHANGE_KSVS,
236 	D1_A23_WAIT_FOR_R0_PRIME,
237 	D1_A2_COMPUTATIONS_A3_VALIDATE_RX_A5_TEST_FOR_REPEATER,
238 	D1_A4_AUTHENTICATED,
239 	D1_A6_WAIT_FOR_READY,
240 	D1_A7_READ_KSV_LIST,
241 	HDCP1_DP_STATE_END = D1_A7_READ_KSV_LIST,
242 };
243 
244 enum mod_hdcp_hdcp2_state_id {
245 	HDCP2_STATE_START = HDCP1_DP_STATE_END,
246 	H2_A0_KNOWN_HDCP2_CAPABLE_RX,
247 	H2_A1_SEND_AKE_INIT,
248 	H2_A1_VALIDATE_AKE_CERT,
249 	H2_A1_SEND_NO_STORED_KM,
250 	H2_A1_READ_H_PRIME,
251 	H2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME,
252 	H2_A1_SEND_STORED_KM,
253 	H2_A1_VALIDATE_H_PRIME,
254 	H2_A2_LOCALITY_CHECK,
255 	H2_A3_EXCHANGE_KS_AND_TEST_FOR_REPEATER,
256 	H2_ENABLE_ENCRYPTION,
257 	H2_A5_AUTHENTICATED,
258 	H2_A6_WAIT_FOR_RX_ID_LIST,
259 	H2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK,
260 	H2_A9_SEND_STREAM_MANAGEMENT,
261 	H2_A9_VALIDATE_STREAM_READY,
262 	HDCP2_STATE_END = H2_A9_VALIDATE_STREAM_READY,
263 };
264 
265 enum mod_hdcp_hdcp2_dp_state_id {
266 	HDCP2_DP_STATE_START = HDCP2_STATE_END,
267 	D2_A0_DETERMINE_RX_HDCP_CAPABLE,
268 	D2_A1_SEND_AKE_INIT,
269 	D2_A1_VALIDATE_AKE_CERT,
270 	D2_A1_SEND_NO_STORED_KM,
271 	D2_A1_READ_H_PRIME,
272 	D2_A1_READ_PAIRING_INFO_AND_VALIDATE_H_PRIME,
273 	D2_A1_SEND_STORED_KM,
274 	D2_A1_VALIDATE_H_PRIME,
275 	D2_A2_LOCALITY_CHECK,
276 	D2_A34_EXCHANGE_KS_AND_TEST_FOR_REPEATER,
277 	D2_SEND_CONTENT_STREAM_TYPE,
278 	D2_ENABLE_ENCRYPTION,
279 	D2_A5_AUTHENTICATED,
280 	D2_A6_WAIT_FOR_RX_ID_LIST,
281 	D2_A78_VERIFY_RX_ID_LIST_AND_SEND_ACK,
282 	D2_A9_SEND_STREAM_MANAGEMENT,
283 	D2_A9_VALIDATE_STREAM_READY,
284 	HDCP2_DP_STATE_END = D2_A9_VALIDATE_STREAM_READY,
285 	HDCP_STATE_END = HDCP2_DP_STATE_END,
286 };
287 
288 /* hdcp1 executions and transitions */
289 typedef enum mod_hdcp_status (*mod_hdcp_action)(struct mod_hdcp *hdcp);
290 uint8_t mod_hdcp_execute_and_set(
291 		mod_hdcp_action func, uint8_t *flag,
292 		enum mod_hdcp_status *status, struct mod_hdcp *hdcp, char *str);
293 enum mod_hdcp_status mod_hdcp_hdcp1_execution(struct mod_hdcp *hdcp,
294 	struct mod_hdcp_event_context *event_ctx,
295 	struct mod_hdcp_transition_input_hdcp1 *input);
296 enum mod_hdcp_status mod_hdcp_hdcp1_dp_execution(struct mod_hdcp *hdcp,
297 	struct mod_hdcp_event_context *event_ctx,
298 	struct mod_hdcp_transition_input_hdcp1 *input);
299 enum mod_hdcp_status mod_hdcp_hdcp1_transition(struct mod_hdcp *hdcp,
300 	struct mod_hdcp_event_context *event_ctx,
301 	struct mod_hdcp_transition_input_hdcp1 *input,
302 	struct mod_hdcp_output *output);
303 enum mod_hdcp_status mod_hdcp_hdcp1_dp_transition(struct mod_hdcp *hdcp,
304 	struct mod_hdcp_event_context *event_ctx,
305 	struct mod_hdcp_transition_input_hdcp1 *input,
306 	struct mod_hdcp_output *output);
307 
308 /* hdcp2 executions and transitions */
309 enum mod_hdcp_status mod_hdcp_hdcp2_execution(struct mod_hdcp *hdcp,
310 	struct mod_hdcp_event_context *event_ctx,
311 	struct mod_hdcp_transition_input_hdcp2 *input);
312 enum mod_hdcp_status mod_hdcp_hdcp2_dp_execution(struct mod_hdcp *hdcp,
313 	struct mod_hdcp_event_context *event_ctx,
314 	struct mod_hdcp_transition_input_hdcp2 *input);
315 enum mod_hdcp_status mod_hdcp_hdcp2_transition(struct mod_hdcp *hdcp,
316 	struct mod_hdcp_event_context *event_ctx,
317 	struct mod_hdcp_transition_input_hdcp2 *input,
318 	struct mod_hdcp_output *output);
319 enum mod_hdcp_status mod_hdcp_hdcp2_dp_transition(struct mod_hdcp *hdcp,
320 	struct mod_hdcp_event_context *event_ctx,
321 	struct mod_hdcp_transition_input_hdcp2 *input,
322 	struct mod_hdcp_output *output);
323 
324 /* log functions */
325 void mod_hdcp_dump_binary_message(uint8_t *msg, uint32_t msg_size,
326 		uint8_t *buf, uint32_t buf_size);
327 /* TODO: add adjustment log */
328 
329 /* psp functions */
330 enum mod_hdcp_status mod_hdcp_add_display_to_topology(
331 		struct mod_hdcp *hdcp, struct mod_hdcp_display *display);
332 enum mod_hdcp_status mod_hdcp_remove_display_from_topology(
333 		struct mod_hdcp *hdcp, uint8_t index);
334 enum mod_hdcp_status mod_hdcp_hdcp1_create_session(struct mod_hdcp *hdcp);
335 enum mod_hdcp_status mod_hdcp_hdcp1_destroy_session(struct mod_hdcp *hdcp);
336 enum mod_hdcp_status mod_hdcp_hdcp1_validate_rx(struct mod_hdcp *hdcp);
337 enum mod_hdcp_status mod_hdcp_hdcp1_enable_encryption(struct mod_hdcp *hdcp);
338 enum mod_hdcp_status mod_hdcp_hdcp1_validate_ksvlist_vp(struct mod_hdcp *hdcp);
339 enum mod_hdcp_status mod_hdcp_hdcp1_enable_dp_stream_encryption(
340 	struct mod_hdcp *hdcp);
341 enum mod_hdcp_status mod_hdcp_hdcp1_link_maintenance(struct mod_hdcp *hdcp);
342 enum mod_hdcp_status mod_hdcp_hdcp1_get_link_encryption_status(struct mod_hdcp *hdcp,
343 							       enum mod_hdcp_encryption_status *encryption_status);
344 enum mod_hdcp_status mod_hdcp_hdcp2_create_session(struct mod_hdcp *hdcp);
345 enum mod_hdcp_status mod_hdcp_hdcp2_destroy_session(struct mod_hdcp *hdcp);
346 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_ake_init(struct mod_hdcp *hdcp);
347 enum mod_hdcp_status mod_hdcp_hdcp2_validate_ake_cert(struct mod_hdcp *hdcp);
348 enum mod_hdcp_status mod_hdcp_hdcp2_validate_h_prime(struct mod_hdcp *hdcp);
349 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_lc_init(struct mod_hdcp *hdcp);
350 enum mod_hdcp_status mod_hdcp_hdcp2_validate_l_prime(struct mod_hdcp *hdcp);
351 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_eks(struct mod_hdcp *hdcp);
352 enum mod_hdcp_status mod_hdcp_hdcp2_enable_encryption(struct mod_hdcp *hdcp);
353 enum mod_hdcp_status mod_hdcp_hdcp2_validate_rx_id_list(struct mod_hdcp *hdcp);
354 enum mod_hdcp_status mod_hdcp_hdcp2_enable_dp_stream_encryption(
355 		struct mod_hdcp *hdcp);
356 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_stream_management(
357 		struct mod_hdcp *hdcp);
358 enum mod_hdcp_status mod_hdcp_hdcp2_validate_stream_ready(
359 		struct mod_hdcp *hdcp);
360 
361 /* ddc functions */
362 enum mod_hdcp_status mod_hdcp_read_bksv(struct mod_hdcp *hdcp);
363 enum mod_hdcp_status mod_hdcp_read_bcaps(struct mod_hdcp *hdcp);
364 enum mod_hdcp_status mod_hdcp_read_bstatus(struct mod_hdcp *hdcp);
365 enum mod_hdcp_status mod_hdcp_read_r0p(struct mod_hdcp *hdcp);
366 enum mod_hdcp_status mod_hdcp_read_ksvlist(struct mod_hdcp *hdcp);
367 enum mod_hdcp_status mod_hdcp_read_vp(struct mod_hdcp *hdcp);
368 enum mod_hdcp_status mod_hdcp_read_binfo(struct mod_hdcp *hdcp);
369 enum mod_hdcp_status mod_hdcp_write_aksv(struct mod_hdcp *hdcp);
370 enum mod_hdcp_status mod_hdcp_write_ainfo(struct mod_hdcp *hdcp);
371 enum mod_hdcp_status mod_hdcp_write_an(struct mod_hdcp *hdcp);
372 enum mod_hdcp_status mod_hdcp_read_hdcp2version(struct mod_hdcp *hdcp);
373 enum mod_hdcp_status mod_hdcp_read_rxcaps(struct mod_hdcp *hdcp);
374 enum mod_hdcp_status mod_hdcp_read_rxstatus(struct mod_hdcp *hdcp);
375 enum mod_hdcp_status mod_hdcp_read_ake_cert(struct mod_hdcp *hdcp);
376 enum mod_hdcp_status mod_hdcp_read_h_prime(struct mod_hdcp *hdcp);
377 enum mod_hdcp_status mod_hdcp_read_pairing_info(struct mod_hdcp *hdcp);
378 enum mod_hdcp_status mod_hdcp_read_l_prime(struct mod_hdcp *hdcp);
379 enum mod_hdcp_status mod_hdcp_read_rx_id_list(struct mod_hdcp *hdcp);
380 enum mod_hdcp_status mod_hdcp_read_stream_ready(struct mod_hdcp *hdcp);
381 enum mod_hdcp_status mod_hdcp_write_ake_init(struct mod_hdcp *hdcp);
382 enum mod_hdcp_status mod_hdcp_write_no_stored_km(struct mod_hdcp *hdcp);
383 enum mod_hdcp_status mod_hdcp_write_stored_km(struct mod_hdcp *hdcp);
384 enum mod_hdcp_status mod_hdcp_write_lc_init(struct mod_hdcp *hdcp);
385 enum mod_hdcp_status mod_hdcp_write_eks(struct mod_hdcp *hdcp);
386 enum mod_hdcp_status mod_hdcp_write_repeater_auth_ack(struct mod_hdcp *hdcp);
387 enum mod_hdcp_status mod_hdcp_write_stream_manage(struct mod_hdcp *hdcp);
388 enum mod_hdcp_status mod_hdcp_write_content_type(struct mod_hdcp *hdcp);
389 
390 /* hdcp version helpers */
391 static inline uint8_t is_dp_hdcp(struct mod_hdcp *hdcp)
392 {
393 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DP);
394 }
395 
396 static inline uint8_t is_dp_mst_hdcp(struct mod_hdcp *hdcp)
397 {
398 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DP &&
399 			hdcp->connection.link.dp.mst_supported);
400 }
401 
402 static inline uint8_t is_hdmi_dvi_sl_hdcp(struct mod_hdcp *hdcp)
403 {
404 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DEFAULT);
405 }
406 
407 /* hdcp state helpers */
408 static inline uint8_t current_state(struct mod_hdcp *hdcp)
409 {
410 	return hdcp->state.id;
411 }
412 
413 static inline void set_state_id(struct mod_hdcp *hdcp,
414 		struct mod_hdcp_output *output, uint8_t id)
415 {
416 	memset(&hdcp->state, 0, sizeof(hdcp->state));
417 	hdcp->state.id = id;
418 	/* callback timer should be reset per state */
419 	output->callback_stop = 1;
420 	output->watchdog_timer_stop = 1;
421 	HDCP_NEXT_STATE_TRACE(hdcp, id, output);
422 }
423 
424 static inline uint8_t is_in_hdcp1_states(struct mod_hdcp *hdcp)
425 {
426 	return (current_state(hdcp) > HDCP1_STATE_START &&
427 			current_state(hdcp) <= HDCP1_STATE_END);
428 }
429 
430 static inline uint8_t is_in_hdcp1_dp_states(struct mod_hdcp *hdcp)
431 {
432 	return (current_state(hdcp) > HDCP1_DP_STATE_START &&
433 			current_state(hdcp) <= HDCP1_DP_STATE_END);
434 }
435 
436 static inline uint8_t is_in_hdcp2_states(struct mod_hdcp *hdcp)
437 {
438 	return (current_state(hdcp) > HDCP2_STATE_START &&
439 			current_state(hdcp) <= HDCP2_STATE_END);
440 }
441 
442 static inline uint8_t is_in_hdcp2_dp_states(struct mod_hdcp *hdcp)
443 {
444 	return (current_state(hdcp) > HDCP2_DP_STATE_START &&
445 			current_state(hdcp) <= HDCP2_DP_STATE_END);
446 }
447 
448 static inline uint8_t is_hdcp1(struct mod_hdcp *hdcp)
449 {
450 	return (is_in_hdcp1_states(hdcp) || is_in_hdcp1_dp_states(hdcp));
451 }
452 
453 static inline uint8_t is_hdcp2(struct mod_hdcp *hdcp)
454 {
455 	return (is_in_hdcp2_states(hdcp) || is_in_hdcp2_dp_states(hdcp));
456 }
457 
458 static inline uint8_t is_in_cp_not_desired_state(struct mod_hdcp *hdcp)
459 {
460 	return current_state(hdcp) == HDCP_CP_NOT_DESIRED;
461 }
462 
463 static inline uint8_t is_in_initialized_state(struct mod_hdcp *hdcp)
464 {
465 	return current_state(hdcp) == HDCP_INITIALIZED;
466 }
467 
468 /* transition operation helpers */
469 static inline void increment_stay_counter(struct mod_hdcp *hdcp)
470 {
471 	hdcp->state.stay_count++;
472 }
473 
474 static inline void fail_and_restart_in_ms(uint16_t time,
475 		enum mod_hdcp_status *status,
476 		struct mod_hdcp_output *output)
477 {
478 	output->callback_needed = 1;
479 	output->callback_delay = time;
480 	output->watchdog_timer_needed = 0;
481 	output->watchdog_timer_delay = 0;
482 	*status = MOD_HDCP_STATUS_RESET_NEEDED;
483 }
484 
485 static inline void callback_in_ms(uint16_t time, struct mod_hdcp_output *output)
486 {
487 	output->callback_needed = 1;
488 	output->callback_delay = time;
489 }
490 
491 static inline void set_watchdog_in_ms(struct mod_hdcp *hdcp, uint16_t time,
492 		struct mod_hdcp_output *output)
493 {
494 	output->watchdog_timer_needed = 1;
495 	output->watchdog_timer_delay = time;
496 }
497 
498 /* connection topology helpers */
499 static inline uint8_t is_display_active(struct mod_hdcp_display *display)
500 {
501 	return display->state >= MOD_HDCP_DISPLAY_ACTIVE;
502 }
503 
504 static inline uint8_t is_display_encryption_enabled(struct mod_hdcp_display *display)
505 {
506 	return display->state >= MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
507 }
508 
509 static inline uint8_t get_active_display_count(struct mod_hdcp *hdcp)
510 {
511 	uint8_t active_count = 0;
512 	uint8_t i;
513 
514 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
515 		if (is_display_active(&hdcp->displays[i]))
516 			active_count++;
517 	return active_count;
518 }
519 
520 static inline struct mod_hdcp_display *get_first_active_display(
521 		struct mod_hdcp *hdcp)
522 {
523 	uint8_t i;
524 	struct mod_hdcp_display *display = NULL;
525 
526 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
527 		if (is_display_active(&hdcp->displays[i])) {
528 			display = &hdcp->displays[i];
529 			break;
530 		}
531 	return display;
532 }
533 
534 static inline struct mod_hdcp_display *get_active_display_at_index(
535 		struct mod_hdcp *hdcp, uint8_t index)
536 {
537 	uint8_t i;
538 	struct mod_hdcp_display *display = NULL;
539 
540 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
541 		if (hdcp->displays[i].index == index &&
542 				is_display_active(&hdcp->displays[i])) {
543 			display = &hdcp->displays[i];
544 			break;
545 		}
546 	return display;
547 }
548 
549 static inline struct mod_hdcp_display *get_empty_display_container(
550 		struct mod_hdcp *hdcp)
551 {
552 	uint8_t i;
553 	struct mod_hdcp_display *display = NULL;
554 
555 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
556 		if (!is_display_active(&hdcp->displays[i])) {
557 			display = &hdcp->displays[i];
558 			break;
559 		}
560 	return display;
561 }
562 
563 static inline void reset_retry_counts(struct mod_hdcp *hdcp)
564 {
565 	hdcp->connection.hdcp1_retry_count = 0;
566 	hdcp->connection.hdcp2_retry_count = 0;
567 }
568 
569 #endif /* HDCP_H_ */
570