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 add_topology;
45 	uint8_t create_session;
46 	uint8_t an_write;
47 	uint8_t aksv_write;
48 	uint8_t ainfo_write;
49 	uint8_t bcaps_read;
50 	uint8_t r0p_read;
51 	uint8_t rx_validation;
52 	uint8_t encryption;
53 	uint8_t link_maintenance;
54 	uint8_t ready_check;
55 	uint8_t bstatus_read;
56 	uint8_t max_cascade_check;
57 	uint8_t max_devs_check;
58 	uint8_t device_count_check;
59 	uint8_t ksvlist_read;
60 	uint8_t vp_read;
61 	uint8_t ksvlist_vp_validation;
62 
63 	uint8_t hdcp_capable_dp;
64 	uint8_t binfo_read_dp;
65 	uint8_t r0p_available_dp;
66 	uint8_t link_integrity_check;
67 	uint8_t reauth_request_check;
68 	uint8_t stream_encryption_dp;
69 };
70 
71 struct mod_hdcp_transition_input_hdcp2 {
72 	uint8_t hdcp2version_read;
73 	uint8_t hdcp2_capable_check;
74 	uint8_t add_topology;
75 	uint8_t create_session;
76 	uint8_t ake_init_prepare;
77 	uint8_t ake_init_write;
78 	uint8_t rxstatus_read;
79 	uint8_t ake_cert_available;
80 	uint8_t ake_cert_read;
81 	uint8_t ake_cert_validation;
82 	uint8_t stored_km_write;
83 	uint8_t no_stored_km_write;
84 	uint8_t h_prime_available;
85 	uint8_t h_prime_read;
86 	uint8_t pairing_available;
87 	uint8_t pairing_info_read;
88 	uint8_t h_prime_validation;
89 	uint8_t lc_init_prepare;
90 	uint8_t lc_init_write;
91 	uint8_t l_prime_available_poll;
92 	uint8_t l_prime_read;
93 	uint8_t l_prime_validation;
94 	uint8_t eks_prepare;
95 	uint8_t eks_write;
96 	uint8_t enable_encryption;
97 	uint8_t reauth_request_check;
98 	uint8_t rx_id_list_read;
99 	uint8_t device_count_check;
100 	uint8_t rx_id_list_validation;
101 	uint8_t repeater_auth_ack_write;
102 	uint8_t prepare_stream_manage;
103 	uint8_t stream_manage_write;
104 	uint8_t stream_ready_available;
105 	uint8_t stream_ready_read;
106 	uint8_t stream_ready_validation;
107 
108 	uint8_t rx_caps_read_dp;
109 	uint8_t content_stream_type_write;
110 	uint8_t link_integrity_check_dp;
111 	uint8_t stream_encryption_dp;
112 };
113 
114 union mod_hdcp_transition_input {
115 	struct mod_hdcp_transition_input_hdcp1 hdcp1;
116 	struct mod_hdcp_transition_input_hdcp2 hdcp2;
117 };
118 
119 struct mod_hdcp_message_hdcp1 {
120 	uint8_t		an[8];
121 	uint8_t		aksv[5];
122 	uint8_t		ainfo;
123 	uint8_t		bksv[5];
124 	uint16_t	r0p;
125 	uint8_t		bcaps;
126 	uint16_t	bstatus;
127 	uint8_t		ksvlist[635];
128 	uint16_t	ksvlist_size;
129 	uint8_t		vp[20];
130 
131 	uint16_t	binfo_dp;
132 };
133 
134 struct mod_hdcp_message_hdcp2 {
135 	uint8_t		hdcp2version_hdmi;
136 	uint8_t		rxcaps_dp[3];
137 	uint8_t		rxstatus[2];
138 
139 	uint8_t		ake_init[12];
140 	uint8_t		ake_cert[534];
141 	uint8_t		ake_no_stored_km[129];
142 	uint8_t		ake_stored_km[33];
143 	uint8_t		ake_h_prime[33];
144 	uint8_t		ake_pairing_info[17];
145 	uint8_t		lc_init[9];
146 	uint8_t		lc_l_prime[33];
147 	uint8_t		ske_eks[25];
148 	uint8_t		rx_id_list[177]; // 22 + 5 * 31
149 	uint16_t	rx_id_list_size;
150 	uint8_t		repeater_auth_ack[17];
151 	uint8_t		repeater_auth_stream_manage[68]; // 6 + 2 * 31
152 	uint16_t	stream_manage_size;
153 	uint8_t		repeater_auth_stream_ready[33];
154 	uint8_t		rxstatus_dp;
155 	uint8_t		content_stream_type_dp[2];
156 };
157 
158 union mod_hdcp_message {
159 	struct mod_hdcp_message_hdcp1 hdcp1;
160 	struct mod_hdcp_message_hdcp2 hdcp2;
161 };
162 
163 struct mod_hdcp_auth_counters {
164 	uint8_t stream_management_retry_count;
165 };
166 
167 /* contains values per connection */
168 struct mod_hdcp_connection {
169 	struct mod_hdcp_link link;
170 	struct mod_hdcp_display displays[MAX_NUM_OF_DISPLAYS];
171 	uint8_t is_repeater;
172 	uint8_t is_km_stored;
173 	uint8_t is_hdcp2_revoked;
174 	struct mod_hdcp_trace trace;
175 	uint8_t hdcp1_retry_count;
176 	uint8_t hdcp2_retry_count;
177 };
178 
179 /* contains values per authentication cycle */
180 struct mod_hdcp_authentication {
181 	uint32_t id;
182 	union mod_hdcp_message msg;
183 	union mod_hdcp_transition_input trans_input;
184 	struct mod_hdcp_auth_counters count;
185 };
186 
187 /* contains values per state change */
188 struct mod_hdcp_state {
189 	uint8_t id;
190 	uint32_t stay_count;
191 };
192 
193 /* per event in a state */
194 struct mod_hdcp_event_context {
195 	enum mod_hdcp_event event;
196 	uint8_t rx_id_list_ready;
197 	uint8_t unexpected_event;
198 };
199 
200 struct mod_hdcp {
201 	/* per link */
202 	struct mod_hdcp_config config;
203 	/* per connection */
204 	struct mod_hdcp_connection connection;
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_topology(
331 		struct mod_hdcp *hdcp);
332 enum mod_hdcp_status mod_hdcp_remove_display_topology(
333 		struct mod_hdcp *hdcp);
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 enum mod_hdcp_status mod_hdcp_hdcp2_get_link_encryption_status(struct mod_hdcp *hdcp,
361 							       enum mod_hdcp_encryption_status *encryption_status);
362 
363 /* ddc functions */
364 enum mod_hdcp_status mod_hdcp_read_bksv(struct mod_hdcp *hdcp);
365 enum mod_hdcp_status mod_hdcp_read_bcaps(struct mod_hdcp *hdcp);
366 enum mod_hdcp_status mod_hdcp_read_bstatus(struct mod_hdcp *hdcp);
367 enum mod_hdcp_status mod_hdcp_read_r0p(struct mod_hdcp *hdcp);
368 enum mod_hdcp_status mod_hdcp_read_ksvlist(struct mod_hdcp *hdcp);
369 enum mod_hdcp_status mod_hdcp_read_vp(struct mod_hdcp *hdcp);
370 enum mod_hdcp_status mod_hdcp_read_binfo(struct mod_hdcp *hdcp);
371 enum mod_hdcp_status mod_hdcp_write_aksv(struct mod_hdcp *hdcp);
372 enum mod_hdcp_status mod_hdcp_write_ainfo(struct mod_hdcp *hdcp);
373 enum mod_hdcp_status mod_hdcp_write_an(struct mod_hdcp *hdcp);
374 enum mod_hdcp_status mod_hdcp_read_hdcp2version(struct mod_hdcp *hdcp);
375 enum mod_hdcp_status mod_hdcp_read_rxcaps(struct mod_hdcp *hdcp);
376 enum mod_hdcp_status mod_hdcp_read_rxstatus(struct mod_hdcp *hdcp);
377 enum mod_hdcp_status mod_hdcp_read_ake_cert(struct mod_hdcp *hdcp);
378 enum mod_hdcp_status mod_hdcp_read_h_prime(struct mod_hdcp *hdcp);
379 enum mod_hdcp_status mod_hdcp_read_pairing_info(struct mod_hdcp *hdcp);
380 enum mod_hdcp_status mod_hdcp_read_l_prime(struct mod_hdcp *hdcp);
381 enum mod_hdcp_status mod_hdcp_read_rx_id_list(struct mod_hdcp *hdcp);
382 enum mod_hdcp_status mod_hdcp_read_stream_ready(struct mod_hdcp *hdcp);
383 enum mod_hdcp_status mod_hdcp_write_ake_init(struct mod_hdcp *hdcp);
384 enum mod_hdcp_status mod_hdcp_write_no_stored_km(struct mod_hdcp *hdcp);
385 enum mod_hdcp_status mod_hdcp_write_stored_km(struct mod_hdcp *hdcp);
386 enum mod_hdcp_status mod_hdcp_write_lc_init(struct mod_hdcp *hdcp);
387 enum mod_hdcp_status mod_hdcp_write_eks(struct mod_hdcp *hdcp);
388 enum mod_hdcp_status mod_hdcp_write_repeater_auth_ack(struct mod_hdcp *hdcp);
389 enum mod_hdcp_status mod_hdcp_write_stream_manage(struct mod_hdcp *hdcp);
390 enum mod_hdcp_status mod_hdcp_write_content_type(struct mod_hdcp *hdcp);
391 
392 /* hdcp version helpers */
393 static inline uint8_t is_dp_hdcp(struct mod_hdcp *hdcp)
394 {
395 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DP ||
396 			hdcp->connection.link.mode == MOD_HDCP_MODE_DP_MST);
397 }
398 
399 static inline uint8_t is_dp_mst_hdcp(struct mod_hdcp *hdcp)
400 {
401 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DP_MST);
402 }
403 
404 static inline uint8_t is_hdmi_dvi_sl_hdcp(struct mod_hdcp *hdcp)
405 {
406 	return (hdcp->connection.link.mode == MOD_HDCP_MODE_DEFAULT);
407 }
408 
409 /* hdcp state helpers */
410 static inline uint8_t current_state(struct mod_hdcp *hdcp)
411 {
412 	return hdcp->state.id;
413 }
414 
415 static inline void set_state_id(struct mod_hdcp *hdcp,
416 		struct mod_hdcp_output *output, uint8_t id)
417 {
418 	memset(&hdcp->state, 0, sizeof(hdcp->state));
419 	hdcp->state.id = id;
420 	/* callback timer should be reset per state */
421 	output->callback_stop = 1;
422 	output->watchdog_timer_stop = 1;
423 	HDCP_NEXT_STATE_TRACE(hdcp, id, output);
424 }
425 
426 static inline uint8_t is_in_hdcp1_states(struct mod_hdcp *hdcp)
427 {
428 	return (current_state(hdcp) > HDCP1_STATE_START &&
429 			current_state(hdcp) <= HDCP1_STATE_END);
430 }
431 
432 static inline uint8_t is_in_hdcp1_dp_states(struct mod_hdcp *hdcp)
433 {
434 	return (current_state(hdcp) > HDCP1_DP_STATE_START &&
435 			current_state(hdcp) <= HDCP1_DP_STATE_END);
436 }
437 
438 static inline uint8_t is_in_hdcp2_states(struct mod_hdcp *hdcp)
439 {
440 	return (current_state(hdcp) > HDCP2_STATE_START &&
441 			current_state(hdcp) <= HDCP2_STATE_END);
442 }
443 
444 static inline uint8_t is_in_hdcp2_dp_states(struct mod_hdcp *hdcp)
445 {
446 	return (current_state(hdcp) > HDCP2_DP_STATE_START &&
447 			current_state(hdcp) <= HDCP2_DP_STATE_END);
448 }
449 
450 static inline uint8_t is_hdcp1(struct mod_hdcp *hdcp)
451 {
452 	return (is_in_hdcp1_states(hdcp) || is_in_hdcp1_dp_states(hdcp));
453 }
454 
455 static inline uint8_t is_hdcp2(struct mod_hdcp *hdcp)
456 {
457 	return (is_in_hdcp2_states(hdcp) || is_in_hdcp2_dp_states(hdcp));
458 }
459 
460 static inline uint8_t is_in_cp_not_desired_state(struct mod_hdcp *hdcp)
461 {
462 	return current_state(hdcp) == HDCP_CP_NOT_DESIRED;
463 }
464 
465 static inline uint8_t is_in_initialized_state(struct mod_hdcp *hdcp)
466 {
467 	return current_state(hdcp) == HDCP_INITIALIZED;
468 }
469 
470 /* transition operation helpers */
471 static inline void increment_stay_counter(struct mod_hdcp *hdcp)
472 {
473 	hdcp->state.stay_count++;
474 }
475 
476 static inline void fail_and_restart_in_ms(uint16_t time,
477 		enum mod_hdcp_status *status,
478 		struct mod_hdcp_output *output)
479 {
480 	output->callback_needed = 1;
481 	output->callback_delay = time;
482 	output->watchdog_timer_needed = 0;
483 	output->watchdog_timer_delay = 0;
484 	*status = MOD_HDCP_STATUS_RESET_NEEDED;
485 }
486 
487 static inline void callback_in_ms(uint16_t time, struct mod_hdcp_output *output)
488 {
489 	output->callback_needed = 1;
490 	output->callback_delay = time;
491 }
492 
493 static inline void set_watchdog_in_ms(struct mod_hdcp *hdcp, uint16_t time,
494 		struct mod_hdcp_output *output)
495 {
496 	output->watchdog_timer_needed = 1;
497 	output->watchdog_timer_delay = time;
498 }
499 
500 /* connection topology helpers */
501 static inline uint8_t is_display_active(struct mod_hdcp_display *display)
502 {
503 	return display->state >= MOD_HDCP_DISPLAY_ACTIVE;
504 }
505 
506 static inline uint8_t is_display_added(struct mod_hdcp_display *display)
507 {
508 	return display->state >= MOD_HDCP_DISPLAY_ACTIVE_AND_ADDED;
509 }
510 
511 static inline uint8_t is_display_encryption_enabled(struct mod_hdcp_display *display)
512 {
513 	return display->state >= MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
514 }
515 
516 static inline uint8_t get_active_display_count(struct mod_hdcp *hdcp)
517 {
518 	uint8_t added_count = 0;
519 	uint8_t i;
520 
521 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
522 		if (is_display_active(&hdcp->connection.displays[i]))
523 			added_count++;
524 	return added_count;
525 }
526 
527 static inline uint8_t get_added_display_count(struct mod_hdcp *hdcp)
528 {
529 	uint8_t added_count = 0;
530 	uint8_t i;
531 
532 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
533 		if (is_display_added(&hdcp->connection.displays[i]))
534 			added_count++;
535 	return added_count;
536 }
537 
538 static inline struct mod_hdcp_display *get_first_added_display(
539 		struct mod_hdcp *hdcp)
540 {
541 	uint8_t i;
542 	struct mod_hdcp_display *display = NULL;
543 
544 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
545 		if (is_display_added(&hdcp->connection.displays[i])) {
546 			display = &hdcp->connection.displays[i];
547 			break;
548 		}
549 	return display;
550 }
551 
552 static inline struct mod_hdcp_display *get_active_display_at_index(
553 		struct mod_hdcp *hdcp, uint8_t index)
554 {
555 	uint8_t i;
556 	struct mod_hdcp_display *display = NULL;
557 
558 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
559 		if (hdcp->connection.displays[i].index == index &&
560 				is_display_active(&hdcp->connection.displays[i])) {
561 			display = &hdcp->connection.displays[i];
562 			break;
563 		}
564 	return display;
565 }
566 
567 static inline struct mod_hdcp_display *get_empty_display_container(
568 		struct mod_hdcp *hdcp)
569 {
570 	uint8_t i;
571 	struct mod_hdcp_display *display = NULL;
572 
573 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
574 		if (!is_display_active(&hdcp->connection.displays[i])) {
575 			display = &hdcp->connection.displays[i];
576 			break;
577 		}
578 	return display;
579 }
580 
581 static inline void reset_retry_counts(struct mod_hdcp *hdcp)
582 {
583 	hdcp->connection.hdcp1_retry_count = 0;
584 	hdcp->connection.hdcp2_retry_count = 0;
585 }
586 
587 #endif /* HDCP_H_ */
588