2 * Copyright 2019 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
28 static void push_error_status(struct mod_hdcp *hdcp,
29 enum mod_hdcp_status status)
31 struct mod_hdcp_trace *trace = &hdcp->connection.trace;
33 if (trace->error_count < MAX_NUM_OF_ERROR_TRACE) {
34 trace->errors[trace->error_count].status = status;
35 trace->errors[trace->error_count].state_id = hdcp->state.id;
37 HDCP_ERROR_TRACE(hdcp, status);
41 hdcp->connection.hdcp1_retry_count++;
42 } else if (is_hdcp2(hdcp)) {
43 hdcp->connection.hdcp2_retry_count++;
47 static uint8_t is_cp_desired_hdcp1(struct mod_hdcp *hdcp)
49 int i, is_auth_needed = 0;
51 /* if all displays on the link don't need authentication,
54 for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++) {
55 if (hdcp->displays[i].state != MOD_HDCP_DISPLAY_INACTIVE &&
56 !hdcp->displays[i].adjust.disable) {
62 return (hdcp->connection.hdcp1_retry_count < MAX_NUM_OF_ATTEMPTS) &&
64 !hdcp->connection.link.adjust.hdcp1.disable &&
65 !hdcp->connection.is_hdcp1_revoked;
68 static uint8_t is_cp_desired_hdcp2(struct mod_hdcp *hdcp)
70 int i, is_auth_needed = 0;
72 /* if all displays on the link don't need authentication,
75 for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++) {
76 if (hdcp->displays[i].state != MOD_HDCP_DISPLAY_INACTIVE &&
77 !hdcp->displays[i].adjust.disable) {
83 return (hdcp->connection.hdcp2_retry_count < MAX_NUM_OF_ATTEMPTS) &&
85 !hdcp->connection.link.adjust.hdcp2.disable &&
86 !hdcp->connection.is_hdcp2_revoked;
89 static enum mod_hdcp_status execution(struct mod_hdcp *hdcp,
90 struct mod_hdcp_event_context *event_ctx,
91 union mod_hdcp_transition_input *input)
93 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
95 if (is_in_initialized_state(hdcp)) {
96 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
97 event_ctx->unexpected_event = 1;
100 /* initialize transition input */
101 memset(input, 0, sizeof(union mod_hdcp_transition_input));
102 } else if (is_in_cp_not_desired_state(hdcp)) {
103 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
104 event_ctx->unexpected_event = 1;
107 } else if (is_in_hdcp1_states(hdcp)) {
108 status = mod_hdcp_hdcp1_execution(hdcp, event_ctx, &input->hdcp1);
109 } else if (is_in_hdcp1_dp_states(hdcp)) {
110 status = mod_hdcp_hdcp1_dp_execution(hdcp,
111 event_ctx, &input->hdcp1);
112 } else if (is_in_hdcp2_states(hdcp)) {
113 status = mod_hdcp_hdcp2_execution(hdcp, event_ctx, &input->hdcp2);
114 } else if (is_in_hdcp2_dp_states(hdcp)) {
115 status = mod_hdcp_hdcp2_dp_execution(hdcp,
116 event_ctx, &input->hdcp2);
118 event_ctx->unexpected_event = 1;
125 static enum mod_hdcp_status transition(struct mod_hdcp *hdcp,
126 struct mod_hdcp_event_context *event_ctx,
127 union mod_hdcp_transition_input *input,
128 struct mod_hdcp_output *output)
130 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
132 if (event_ctx->unexpected_event)
135 if (is_in_initialized_state(hdcp)) {
136 if (is_dp_hdcp(hdcp))
137 if (is_cp_desired_hdcp2(hdcp)) {
138 callback_in_ms(0, output);
139 set_state_id(hdcp, output, D2_A0_DETERMINE_RX_HDCP_CAPABLE);
140 } else if (is_cp_desired_hdcp1(hdcp)) {
141 callback_in_ms(0, output);
142 set_state_id(hdcp, output, D1_A0_DETERMINE_RX_HDCP_CAPABLE);
144 callback_in_ms(0, output);
145 set_state_id(hdcp, output, HDCP_CP_NOT_DESIRED);
147 else if (is_hdmi_dvi_sl_hdcp(hdcp))
148 if (is_cp_desired_hdcp2(hdcp)) {
149 callback_in_ms(0, output);
150 set_state_id(hdcp, output, H2_A0_KNOWN_HDCP2_CAPABLE_RX);
151 } else if (is_cp_desired_hdcp1(hdcp)) {
152 callback_in_ms(0, output);
153 set_state_id(hdcp, output, H1_A0_WAIT_FOR_ACTIVE_RX);
155 callback_in_ms(0, output);
156 set_state_id(hdcp, output, HDCP_CP_NOT_DESIRED);
159 callback_in_ms(0, output);
160 set_state_id(hdcp, output, HDCP_CP_NOT_DESIRED);
162 } else if (is_in_cp_not_desired_state(hdcp)) {
163 increment_stay_counter(hdcp);
164 } else if (is_in_hdcp1_states(hdcp)) {
165 status = mod_hdcp_hdcp1_transition(hdcp,
166 event_ctx, &input->hdcp1, output);
167 } else if (is_in_hdcp1_dp_states(hdcp)) {
168 status = mod_hdcp_hdcp1_dp_transition(hdcp,
169 event_ctx, &input->hdcp1, output);
170 } else if (is_in_hdcp2_states(hdcp)) {
171 status = mod_hdcp_hdcp2_transition(hdcp,
172 event_ctx, &input->hdcp2, output);
173 } else if (is_in_hdcp2_dp_states(hdcp)) {
174 status = mod_hdcp_hdcp2_dp_transition(hdcp,
175 event_ctx, &input->hdcp2, output);
177 status = MOD_HDCP_STATUS_INVALID_STATE;
183 static enum mod_hdcp_status reset_authentication(struct mod_hdcp *hdcp,
184 struct mod_hdcp_output *output)
186 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
188 if (is_hdcp1(hdcp)) {
189 if (hdcp->auth.trans_input.hdcp1.create_session != UNKNOWN) {
190 /* TODO - update psp to unify create session failure
191 * recovery between hdcp1 and 2.
193 mod_hdcp_hdcp1_destroy_session(hdcp);
197 HDCP_TOP_RESET_AUTH_TRACE(hdcp);
198 memset(&hdcp->auth, 0, sizeof(struct mod_hdcp_authentication));
199 memset(&hdcp->state, 0, sizeof(struct mod_hdcp_state));
200 set_state_id(hdcp, output, HDCP_INITIALIZED);
201 } else if (is_hdcp2(hdcp)) {
202 if (hdcp->auth.trans_input.hdcp2.create_session == PASS) {
203 status = mod_hdcp_hdcp2_destroy_session(hdcp);
204 if (status != MOD_HDCP_STATUS_SUCCESS) {
205 output->callback_needed = 0;
206 output->watchdog_timer_needed = 0;
211 HDCP_TOP_RESET_AUTH_TRACE(hdcp);
212 memset(&hdcp->auth, 0, sizeof(struct mod_hdcp_authentication));
213 memset(&hdcp->state, 0, sizeof(struct mod_hdcp_state));
214 set_state_id(hdcp, output, HDCP_INITIALIZED);
215 } else if (is_in_cp_not_desired_state(hdcp)) {
216 HDCP_TOP_RESET_AUTH_TRACE(hdcp);
217 memset(&hdcp->auth, 0, sizeof(struct mod_hdcp_authentication));
218 memset(&hdcp->state, 0, sizeof(struct mod_hdcp_state));
219 set_state_id(hdcp, output, HDCP_INITIALIZED);
223 /* stop callback and watchdog requests from previous authentication*/
224 output->watchdog_timer_stop = 1;
225 output->callback_stop = 1;
229 static enum mod_hdcp_status reset_connection(struct mod_hdcp *hdcp,
230 struct mod_hdcp_output *output)
232 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
234 memset(output, 0, sizeof(struct mod_hdcp_output));
236 status = reset_authentication(hdcp, output);
237 if (status != MOD_HDCP_STATUS_SUCCESS)
240 if (current_state(hdcp) != HDCP_UNINITIALIZED) {
241 HDCP_TOP_RESET_CONN_TRACE(hdcp);
242 set_state_id(hdcp, output, HDCP_UNINITIALIZED);
244 memset(&hdcp->connection, 0, sizeof(hdcp->connection));
250 * Implementation of functions in mod_hdcp.h
252 size_t mod_hdcp_get_memory_size(void)
254 return sizeof(struct mod_hdcp);
257 enum mod_hdcp_status mod_hdcp_setup(struct mod_hdcp *hdcp,
258 struct mod_hdcp_config *config)
260 struct mod_hdcp_output output;
261 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
263 memset(hdcp, 0, sizeof(struct mod_hdcp));
264 memset(&output, 0, sizeof(output));
265 hdcp->config = *config;
266 HDCP_TOP_INTERFACE_TRACE(hdcp);
267 status = reset_connection(hdcp, &output);
268 if (status != MOD_HDCP_STATUS_SUCCESS)
269 push_error_status(hdcp, status);
273 enum mod_hdcp_status mod_hdcp_teardown(struct mod_hdcp *hdcp)
275 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
276 struct mod_hdcp_output output;
278 HDCP_TOP_INTERFACE_TRACE(hdcp);
279 memset(&output, 0, sizeof(output));
280 status = reset_connection(hdcp, &output);
281 if (status == MOD_HDCP_STATUS_SUCCESS)
282 memset(hdcp, 0, sizeof(struct mod_hdcp));
284 push_error_status(hdcp, status);
288 enum mod_hdcp_status mod_hdcp_add_display(struct mod_hdcp *hdcp,
289 struct mod_hdcp_link *link, struct mod_hdcp_display *display,
290 struct mod_hdcp_output *output)
292 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
293 struct mod_hdcp_display *display_container = NULL;
295 HDCP_TOP_INTERFACE_TRACE_WITH_INDEX(hdcp, display->index);
296 memset(output, 0, sizeof(struct mod_hdcp_output));
298 /* skip inactive display */
299 if (display->state != MOD_HDCP_DISPLAY_ACTIVE) {
300 status = MOD_HDCP_STATUS_SUCCESS;
304 /* check existing display container */
305 if (get_active_display_at_index(hdcp, display->index)) {
306 status = MOD_HDCP_STATUS_SUCCESS;
310 /* find an empty display container */
311 display_container = get_empty_display_container(hdcp);
312 if (!display_container) {
313 status = MOD_HDCP_STATUS_DISPLAY_OUT_OF_BOUND;
317 /* reset existing authentication status */
318 status = reset_authentication(hdcp, output);
319 if (status != MOD_HDCP_STATUS_SUCCESS)
322 /* reset retry counters */
323 reset_retry_counts(hdcp);
325 /* reset error trace */
326 memset(&hdcp->connection.trace, 0, sizeof(hdcp->connection.trace));
328 /* add display to connection */
329 hdcp->connection.link = *link;
330 *display_container = *display;
331 status = mod_hdcp_add_display_to_topology(hdcp, display_container);
333 if (status != MOD_HDCP_STATUS_SUCCESS)
336 /* request authentication */
337 if (current_state(hdcp) != HDCP_INITIALIZED)
338 set_state_id(hdcp, output, HDCP_INITIALIZED);
339 callback_in_ms(hdcp->connection.link.adjust.auth_delay * 1000, output);
341 if (status != MOD_HDCP_STATUS_SUCCESS)
342 push_error_status(hdcp, status);
347 enum mod_hdcp_status mod_hdcp_remove_display(struct mod_hdcp *hdcp,
348 uint8_t index, struct mod_hdcp_output *output)
350 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
351 struct mod_hdcp_display *display = NULL;
353 HDCP_TOP_INTERFACE_TRACE_WITH_INDEX(hdcp, index);
354 memset(output, 0, sizeof(struct mod_hdcp_output));
356 /* find display in connection */
357 display = get_active_display_at_index(hdcp, index);
359 status = MOD_HDCP_STATUS_SUCCESS;
363 /* stop current authentication */
364 status = reset_authentication(hdcp, output);
365 if (status != MOD_HDCP_STATUS_SUCCESS)
368 /* clear retry counters */
369 reset_retry_counts(hdcp);
371 /* reset error trace */
372 memset(&hdcp->connection.trace, 0, sizeof(hdcp->connection.trace));
375 status = mod_hdcp_remove_display_from_topology(hdcp, index);
376 if (status != MOD_HDCP_STATUS_SUCCESS)
378 memset(display, 0, sizeof(struct mod_hdcp_display));
380 /* request authentication when connection is not reset */
381 if (current_state(hdcp) != HDCP_UNINITIALIZED)
382 callback_in_ms(hdcp->connection.link.adjust.auth_delay * 1000,
385 if (status != MOD_HDCP_STATUS_SUCCESS)
386 push_error_status(hdcp, status);
390 enum mod_hdcp_status mod_hdcp_query_display(struct mod_hdcp *hdcp,
391 uint8_t index, struct mod_hdcp_display_query *query)
393 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
394 struct mod_hdcp_display *display = NULL;
396 /* find display in connection */
397 display = get_active_display_at_index(hdcp, index);
399 status = MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
404 query->link = &hdcp->connection.link;
405 query->display = display;
406 query->trace = &hdcp->connection.trace;
407 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
409 if (is_display_encryption_enabled(display)) {
410 if (is_hdcp1(hdcp)) {
411 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP1_ON;
412 } else if (is_hdcp2(hdcp)) {
413 if (query->link->adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_0)
414 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE0_ON;
415 else if (query->link->adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_1)
416 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE1_ON;
418 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP2_ON;
421 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
428 enum mod_hdcp_status mod_hdcp_reset_connection(struct mod_hdcp *hdcp,
429 struct mod_hdcp_output *output)
431 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
433 HDCP_TOP_INTERFACE_TRACE(hdcp);
434 status = reset_connection(hdcp, output);
435 if (status != MOD_HDCP_STATUS_SUCCESS)
436 push_error_status(hdcp, status);
441 enum mod_hdcp_status mod_hdcp_process_event(struct mod_hdcp *hdcp,
442 enum mod_hdcp_event event, struct mod_hdcp_output *output)
444 enum mod_hdcp_status exec_status, trans_status, reset_status, status;
445 struct mod_hdcp_event_context event_ctx;
447 HDCP_EVENT_TRACE(hdcp, event);
448 memset(output, 0, sizeof(struct mod_hdcp_output));
449 memset(&event_ctx, 0, sizeof(struct mod_hdcp_event_context));
450 event_ctx.event = event;
452 /* execute and transition */
453 exec_status = execution(hdcp, &event_ctx, &hdcp->auth.trans_input);
454 trans_status = transition(
455 hdcp, &event_ctx, &hdcp->auth.trans_input, output);
456 if (trans_status == MOD_HDCP_STATUS_SUCCESS) {
457 status = MOD_HDCP_STATUS_SUCCESS;
458 } else if (exec_status == MOD_HDCP_STATUS_SUCCESS) {
459 status = MOD_HDCP_STATUS_INTERNAL_POLICY_FAILURE;
460 push_error_status(hdcp, status);
462 status = exec_status;
463 push_error_status(hdcp, status);
466 /* reset authentication if needed */
467 if (trans_status == MOD_HDCP_STATUS_RESET_NEEDED) {
468 HDCP_FULL_DDC_TRACE(hdcp);
469 reset_status = reset_authentication(hdcp, output);
470 if (reset_status != MOD_HDCP_STATUS_SUCCESS)
471 push_error_status(hdcp, reset_status);
476 enum mod_hdcp_operation_mode mod_hdcp_signal_type_to_operation_mode(
477 enum signal_type signal)
479 enum mod_hdcp_operation_mode mode = MOD_HDCP_MODE_OFF;
482 case SIGNAL_TYPE_DVI_SINGLE_LINK:
483 case SIGNAL_TYPE_HDMI_TYPE_A:
484 mode = MOD_HDCP_MODE_DEFAULT;
486 case SIGNAL_TYPE_EDP:
487 case SIGNAL_TYPE_DISPLAY_PORT:
488 case SIGNAL_TYPE_DISPLAY_PORT_MST:
489 mode = MOD_HDCP_MODE_DP;