xref: /openbmc/linux/drivers/misc/mei/hdcp/mei_hdcp.c (revision 6de298ff)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2019 Intel Corporation
4  *
5  * mei_hdcp.c: HDCP client driver for mei bus
6  *
7  * Author:
8  * Ramalingam C <ramalingam.c@intel.com>
9  */
10 
11 /**
12  * DOC: MEI_HDCP Client Driver
13  *
14  * The mei_hdcp driver acts as a translation layer between HDCP 2.2
15  * protocol  implementer (I915) and ME FW by translating HDCP2.2
16  * negotiation messages to ME FW command payloads and vice versa.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/mei.h>
22 #include <linux/mei_cl_bus.h>
23 #include <linux/component.h>
24 #include <drm/drm_connector.h>
25 #include <drm/i915_component.h>
26 #include <drm/i915_hdcp_interface.h>
27 
28 #include "mei_hdcp.h"
29 
30 /**
31  * mei_hdcp_initiate_session() - Initiate a Wired HDCP2.2 Tx Session in ME FW
32  * @dev: device corresponding to the mei_cl_device
33  * @data: Intel HW specific hdcp data
34  * @ake_data: AKE_Init msg output.
35  *
36  * Return:  0 on Success, <0 on Failure.
37  */
38 static int
39 mei_hdcp_initiate_session(struct device *dev, struct hdcp_port_data *data,
40 			  struct hdcp2_ake_init *ake_data)
41 {
42 	struct wired_cmd_initiate_hdcp2_session_in session_init_in = { { 0 } };
43 	struct wired_cmd_initiate_hdcp2_session_out
44 						session_init_out = { { 0 } };
45 	struct mei_cl_device *cldev;
46 	ssize_t byte;
47 
48 	if (!dev || !data || !ake_data)
49 		return -EINVAL;
50 
51 	cldev = to_mei_cl_device(dev);
52 
53 	session_init_in.header.api_version = HDCP_API_VERSION;
54 	session_init_in.header.command_id = WIRED_INITIATE_HDCP2_SESSION;
55 	session_init_in.header.status = FW_HDCP_STATUS_SUCCESS;
56 	session_init_in.header.buffer_len =
57 				WIRED_CMD_BUF_LEN_INITIATE_HDCP2_SESSION_IN;
58 
59 	session_init_in.port.integrated_port_type = data->port_type;
60 	session_init_in.port.physical_port = (u8)data->hdcp_ddi;
61 	session_init_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
62 	session_init_in.protocol = data->protocol;
63 
64 	byte = mei_cldev_send(cldev, (u8 *)&session_init_in,
65 			      sizeof(session_init_in));
66 	if (byte < 0) {
67 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
68 		return byte;
69 	}
70 
71 	byte = mei_cldev_recv(cldev, (u8 *)&session_init_out,
72 			      sizeof(session_init_out));
73 	if (byte < 0) {
74 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
75 		return byte;
76 	}
77 
78 	if (session_init_out.header.status != FW_HDCP_STATUS_SUCCESS) {
79 		dev_dbg(dev, "ME cmd 0x%08X Failed. Status: 0x%X\n",
80 			WIRED_INITIATE_HDCP2_SESSION,
81 			session_init_out.header.status);
82 		return -EIO;
83 	}
84 
85 	ake_data->msg_id = HDCP_2_2_AKE_INIT;
86 	ake_data->tx_caps = session_init_out.tx_caps;
87 	memcpy(ake_data->r_tx, session_init_out.r_tx, HDCP_2_2_RTX_LEN);
88 
89 	return 0;
90 }
91 
92 /**
93  * mei_hdcp_verify_receiver_cert_prepare_km() - Verify the Receiver Certificate
94  * AKE_Send_Cert and prepare AKE_Stored_Km/AKE_No_Stored_Km
95  * @dev: device corresponding to the mei_cl_device
96  * @data: Intel HW specific hdcp data
97  * @rx_cert: AKE_Send_Cert for verification
98  * @km_stored: Pairing status flag output
99  * @ek_pub_km: AKE_Stored_Km/AKE_No_Stored_Km output msg
100  * @msg_sz : size of AKE_XXXXX_Km output msg
101  *
102  * Return: 0 on Success, <0 on Failure
103  */
104 static int
105 mei_hdcp_verify_receiver_cert_prepare_km(struct device *dev,
106 					 struct hdcp_port_data *data,
107 					 struct hdcp2_ake_send_cert *rx_cert,
108 					 bool *km_stored,
109 					 struct hdcp2_ake_no_stored_km
110 								*ek_pub_km,
111 					 size_t *msg_sz)
112 {
113 	struct wired_cmd_verify_receiver_cert_in verify_rxcert_in = { { 0 } };
114 	struct wired_cmd_verify_receiver_cert_out verify_rxcert_out = { { 0 } };
115 	struct mei_cl_device *cldev;
116 	ssize_t byte;
117 
118 	if (!dev || !data || !rx_cert || !km_stored || !ek_pub_km || !msg_sz)
119 		return -EINVAL;
120 
121 	cldev = to_mei_cl_device(dev);
122 
123 	verify_rxcert_in.header.api_version = HDCP_API_VERSION;
124 	verify_rxcert_in.header.command_id = WIRED_VERIFY_RECEIVER_CERT;
125 	verify_rxcert_in.header.status = FW_HDCP_STATUS_SUCCESS;
126 	verify_rxcert_in.header.buffer_len =
127 				WIRED_CMD_BUF_LEN_VERIFY_RECEIVER_CERT_IN;
128 
129 	verify_rxcert_in.port.integrated_port_type = data->port_type;
130 	verify_rxcert_in.port.physical_port = (u8)data->hdcp_ddi;
131 	verify_rxcert_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
132 
133 	verify_rxcert_in.cert_rx = rx_cert->cert_rx;
134 	memcpy(verify_rxcert_in.r_rx, &rx_cert->r_rx, HDCP_2_2_RRX_LEN);
135 	memcpy(verify_rxcert_in.rx_caps, rx_cert->rx_caps, HDCP_2_2_RXCAPS_LEN);
136 
137 	byte = mei_cldev_send(cldev, (u8 *)&verify_rxcert_in,
138 			      sizeof(verify_rxcert_in));
139 	if (byte < 0) {
140 		dev_dbg(dev, "mei_cldev_send failed: %zd\n", byte);
141 		return byte;
142 	}
143 
144 	byte = mei_cldev_recv(cldev, (u8 *)&verify_rxcert_out,
145 			      sizeof(verify_rxcert_out));
146 	if (byte < 0) {
147 		dev_dbg(dev, "mei_cldev_recv failed: %zd\n", byte);
148 		return byte;
149 	}
150 
151 	if (verify_rxcert_out.header.status != FW_HDCP_STATUS_SUCCESS) {
152 		dev_dbg(dev, "ME cmd 0x%08X Failed. Status: 0x%X\n",
153 			WIRED_VERIFY_RECEIVER_CERT,
154 			verify_rxcert_out.header.status);
155 		return -EIO;
156 	}
157 
158 	*km_stored = !!verify_rxcert_out.km_stored;
159 	if (verify_rxcert_out.km_stored) {
160 		ek_pub_km->msg_id = HDCP_2_2_AKE_STORED_KM;
161 		*msg_sz = sizeof(struct hdcp2_ake_stored_km);
162 	} else {
163 		ek_pub_km->msg_id = HDCP_2_2_AKE_NO_STORED_KM;
164 		*msg_sz = sizeof(struct hdcp2_ake_no_stored_km);
165 	}
166 
167 	memcpy(ek_pub_km->e_kpub_km, &verify_rxcert_out.ekm_buff,
168 	       sizeof(verify_rxcert_out.ekm_buff));
169 
170 	return 0;
171 }
172 
173 /**
174  * mei_hdcp_verify_hprime() - Verify AKE_Send_H_prime at ME FW.
175  * @dev: device corresponding to the mei_cl_device
176  * @data: Intel HW specific hdcp data
177  * @rx_hprime: AKE_Send_H_prime msg for ME FW verification
178  *
179  * Return: 0 on Success, <0 on Failure
180  */
181 static int
182 mei_hdcp_verify_hprime(struct device *dev, struct hdcp_port_data *data,
183 		       struct hdcp2_ake_send_hprime *rx_hprime)
184 {
185 	struct wired_cmd_ake_send_hprime_in send_hprime_in = { { 0 } };
186 	struct wired_cmd_ake_send_hprime_out send_hprime_out = { { 0 } };
187 	struct mei_cl_device *cldev;
188 	ssize_t byte;
189 
190 	if (!dev || !data || !rx_hprime)
191 		return -EINVAL;
192 
193 	cldev = to_mei_cl_device(dev);
194 
195 	send_hprime_in.header.api_version = HDCP_API_VERSION;
196 	send_hprime_in.header.command_id = WIRED_AKE_SEND_HPRIME;
197 	send_hprime_in.header.status = FW_HDCP_STATUS_SUCCESS;
198 	send_hprime_in.header.buffer_len = WIRED_CMD_BUF_LEN_AKE_SEND_HPRIME_IN;
199 
200 	send_hprime_in.port.integrated_port_type = data->port_type;
201 	send_hprime_in.port.physical_port = (u8)data->hdcp_ddi;
202 	send_hprime_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
203 
204 	memcpy(send_hprime_in.h_prime, rx_hprime->h_prime,
205 	       HDCP_2_2_H_PRIME_LEN);
206 
207 	byte = mei_cldev_send(cldev, (u8 *)&send_hprime_in,
208 			      sizeof(send_hprime_in));
209 	if (byte < 0) {
210 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
211 		return byte;
212 	}
213 
214 	byte = mei_cldev_recv(cldev, (u8 *)&send_hprime_out,
215 			      sizeof(send_hprime_out));
216 	if (byte < 0) {
217 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
218 		return byte;
219 	}
220 
221 	if (send_hprime_out.header.status != FW_HDCP_STATUS_SUCCESS) {
222 		dev_dbg(dev, "ME cmd 0x%08X Failed. Status: 0x%X\n",
223 			WIRED_AKE_SEND_HPRIME, send_hprime_out.header.status);
224 		return -EIO;
225 	}
226 
227 	return 0;
228 }
229 
230 /**
231  * mei_hdcp_store_pairing_info() - Store pairing info received at ME FW
232  * @dev: device corresponding to the mei_cl_device
233  * @data: Intel HW specific hdcp data
234  * @pairing_info: AKE_Send_Pairing_Info msg input to ME FW
235  *
236  * Return: 0 on Success, <0 on Failure
237  */
238 static int
239 mei_hdcp_store_pairing_info(struct device *dev, struct hdcp_port_data *data,
240 			    struct hdcp2_ake_send_pairing_info *pairing_info)
241 {
242 	struct wired_cmd_ake_send_pairing_info_in pairing_info_in = { { 0 } };
243 	struct wired_cmd_ake_send_pairing_info_out pairing_info_out = { { 0 } };
244 	struct mei_cl_device *cldev;
245 	ssize_t byte;
246 
247 	if (!dev || !data || !pairing_info)
248 		return -EINVAL;
249 
250 	cldev = to_mei_cl_device(dev);
251 
252 	pairing_info_in.header.api_version = HDCP_API_VERSION;
253 	pairing_info_in.header.command_id = WIRED_AKE_SEND_PAIRING_INFO;
254 	pairing_info_in.header.status = FW_HDCP_STATUS_SUCCESS;
255 	pairing_info_in.header.buffer_len =
256 					WIRED_CMD_BUF_LEN_SEND_PAIRING_INFO_IN;
257 
258 	pairing_info_in.port.integrated_port_type = data->port_type;
259 	pairing_info_in.port.physical_port = (u8)data->hdcp_ddi;
260 	pairing_info_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
261 
262 	memcpy(pairing_info_in.e_kh_km, pairing_info->e_kh_km,
263 	       HDCP_2_2_E_KH_KM_LEN);
264 
265 	byte = mei_cldev_send(cldev, (u8 *)&pairing_info_in,
266 			      sizeof(pairing_info_in));
267 	if (byte < 0) {
268 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
269 		return byte;
270 	}
271 
272 	byte = mei_cldev_recv(cldev, (u8 *)&pairing_info_out,
273 			      sizeof(pairing_info_out));
274 	if (byte < 0) {
275 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
276 		return byte;
277 	}
278 
279 	if (pairing_info_out.header.status != FW_HDCP_STATUS_SUCCESS) {
280 		dev_dbg(dev, "ME cmd 0x%08X failed. Status: 0x%X\n",
281 			WIRED_AKE_SEND_PAIRING_INFO,
282 			pairing_info_out.header.status);
283 		return -EIO;
284 	}
285 
286 	return 0;
287 }
288 
289 /**
290  * mei_hdcp_initiate_locality_check() - Prepare LC_Init
291  * @dev: device corresponding to the mei_cl_device
292  * @data: Intel HW specific hdcp data
293  * @lc_init_data: LC_Init msg output
294  *
295  * Return: 0 on Success, <0 on Failure
296  */
297 static int
298 mei_hdcp_initiate_locality_check(struct device *dev,
299 				 struct hdcp_port_data *data,
300 				 struct hdcp2_lc_init *lc_init_data)
301 {
302 	struct wired_cmd_init_locality_check_in lc_init_in = { { 0 } };
303 	struct wired_cmd_init_locality_check_out lc_init_out = { { 0 } };
304 	struct mei_cl_device *cldev;
305 	ssize_t byte;
306 
307 	if (!dev || !data || !lc_init_data)
308 		return -EINVAL;
309 
310 	cldev = to_mei_cl_device(dev);
311 
312 	lc_init_in.header.api_version = HDCP_API_VERSION;
313 	lc_init_in.header.command_id = WIRED_INIT_LOCALITY_CHECK;
314 	lc_init_in.header.status = FW_HDCP_STATUS_SUCCESS;
315 	lc_init_in.header.buffer_len = WIRED_CMD_BUF_LEN_INIT_LOCALITY_CHECK_IN;
316 
317 	lc_init_in.port.integrated_port_type = data->port_type;
318 	lc_init_in.port.physical_port = (u8)data->hdcp_ddi;
319 	lc_init_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
320 
321 	byte = mei_cldev_send(cldev, (u8 *)&lc_init_in, sizeof(lc_init_in));
322 	if (byte < 0) {
323 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
324 		return byte;
325 	}
326 
327 	byte = mei_cldev_recv(cldev, (u8 *)&lc_init_out, sizeof(lc_init_out));
328 	if (byte < 0) {
329 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
330 		return byte;
331 	}
332 
333 	if (lc_init_out.header.status != FW_HDCP_STATUS_SUCCESS) {
334 		dev_dbg(dev, "ME cmd 0x%08X Failed. status: 0x%X\n",
335 			WIRED_INIT_LOCALITY_CHECK, lc_init_out.header.status);
336 		return -EIO;
337 	}
338 
339 	lc_init_data->msg_id = HDCP_2_2_LC_INIT;
340 	memcpy(lc_init_data->r_n, lc_init_out.r_n, HDCP_2_2_RN_LEN);
341 
342 	return 0;
343 }
344 
345 /**
346  * mei_hdcp_verify_lprime() - Verify lprime.
347  * @dev: device corresponding to the mei_cl_device
348  * @data: Intel HW specific hdcp data
349  * @rx_lprime: LC_Send_L_prime msg for ME FW verification
350  *
351  * Return: 0 on Success, <0 on Failure
352  */
353 static int
354 mei_hdcp_verify_lprime(struct device *dev, struct hdcp_port_data *data,
355 		       struct hdcp2_lc_send_lprime *rx_lprime)
356 {
357 	struct wired_cmd_validate_locality_in verify_lprime_in = { { 0 } };
358 	struct wired_cmd_validate_locality_out verify_lprime_out = { { 0 } };
359 	struct mei_cl_device *cldev;
360 	ssize_t byte;
361 
362 	if (!dev || !data || !rx_lprime)
363 		return -EINVAL;
364 
365 	cldev = to_mei_cl_device(dev);
366 
367 	verify_lprime_in.header.api_version = HDCP_API_VERSION;
368 	verify_lprime_in.header.command_id = WIRED_VALIDATE_LOCALITY;
369 	verify_lprime_in.header.status = FW_HDCP_STATUS_SUCCESS;
370 	verify_lprime_in.header.buffer_len =
371 					WIRED_CMD_BUF_LEN_VALIDATE_LOCALITY_IN;
372 
373 	verify_lprime_in.port.integrated_port_type = data->port_type;
374 	verify_lprime_in.port.physical_port = (u8)data->hdcp_ddi;
375 	verify_lprime_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
376 
377 	memcpy(verify_lprime_in.l_prime, rx_lprime->l_prime,
378 	       HDCP_2_2_L_PRIME_LEN);
379 
380 	byte = mei_cldev_send(cldev, (u8 *)&verify_lprime_in,
381 			      sizeof(verify_lprime_in));
382 	if (byte < 0) {
383 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
384 		return byte;
385 	}
386 
387 	byte = mei_cldev_recv(cldev, (u8 *)&verify_lprime_out,
388 			      sizeof(verify_lprime_out));
389 	if (byte < 0) {
390 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
391 		return byte;
392 	}
393 
394 	if (verify_lprime_out.header.status != FW_HDCP_STATUS_SUCCESS) {
395 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
396 			WIRED_VALIDATE_LOCALITY,
397 			verify_lprime_out.header.status);
398 		return -EIO;
399 	}
400 
401 	return 0;
402 }
403 
404 /**
405  * mei_hdcp_get_session_key() - Prepare SKE_Send_Eks.
406  * @dev: device corresponding to the mei_cl_device
407  * @data: Intel HW specific hdcp data
408  * @ske_data: SKE_Send_Eks msg output from ME FW.
409  *
410  * Return: 0 on Success, <0 on Failure
411  */
412 static int mei_hdcp_get_session_key(struct device *dev,
413 				    struct hdcp_port_data *data,
414 				    struct hdcp2_ske_send_eks *ske_data)
415 {
416 	struct wired_cmd_get_session_key_in get_skey_in = { { 0 } };
417 	struct wired_cmd_get_session_key_out get_skey_out = { { 0 } };
418 	struct mei_cl_device *cldev;
419 	ssize_t byte;
420 
421 	if (!dev || !data || !ske_data)
422 		return -EINVAL;
423 
424 	cldev = to_mei_cl_device(dev);
425 
426 	get_skey_in.header.api_version = HDCP_API_VERSION;
427 	get_skey_in.header.command_id = WIRED_GET_SESSION_KEY;
428 	get_skey_in.header.status = FW_HDCP_STATUS_SUCCESS;
429 	get_skey_in.header.buffer_len = WIRED_CMD_BUF_LEN_GET_SESSION_KEY_IN;
430 
431 	get_skey_in.port.integrated_port_type = data->port_type;
432 	get_skey_in.port.physical_port = (u8)data->hdcp_ddi;
433 	get_skey_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
434 
435 	byte = mei_cldev_send(cldev, (u8 *)&get_skey_in, sizeof(get_skey_in));
436 	if (byte < 0) {
437 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
438 		return byte;
439 	}
440 
441 	byte = mei_cldev_recv(cldev, (u8 *)&get_skey_out, sizeof(get_skey_out));
442 
443 	if (byte < 0) {
444 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
445 		return byte;
446 	}
447 
448 	if (get_skey_out.header.status != FW_HDCP_STATUS_SUCCESS) {
449 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
450 			WIRED_GET_SESSION_KEY, get_skey_out.header.status);
451 		return -EIO;
452 	}
453 
454 	ske_data->msg_id = HDCP_2_2_SKE_SEND_EKS;
455 	memcpy(ske_data->e_dkey_ks, get_skey_out.e_dkey_ks,
456 	       HDCP_2_2_E_DKEY_KS_LEN);
457 	memcpy(ske_data->riv, get_skey_out.r_iv, HDCP_2_2_RIV_LEN);
458 
459 	return 0;
460 }
461 
462 /**
463  * mei_hdcp_repeater_check_flow_prepare_ack() - Validate the Downstream topology
464  * and prepare rep_ack.
465  * @dev: device corresponding to the mei_cl_device
466  * @data: Intel HW specific hdcp data
467  * @rep_topology: Receiver ID List to be validated
468  * @rep_send_ack : repeater ack from ME FW.
469  *
470  * Return: 0 on Success, <0 on Failure
471  */
472 static int
473 mei_hdcp_repeater_check_flow_prepare_ack(struct device *dev,
474 					 struct hdcp_port_data *data,
475 					 struct hdcp2_rep_send_receiverid_list
476 							*rep_topology,
477 					 struct hdcp2_rep_send_ack
478 							*rep_send_ack)
479 {
480 	struct wired_cmd_verify_repeater_in verify_repeater_in = { { 0 } };
481 	struct wired_cmd_verify_repeater_out verify_repeater_out = { { 0 } };
482 	struct mei_cl_device *cldev;
483 	ssize_t byte;
484 
485 	if (!dev || !rep_topology || !rep_send_ack || !data)
486 		return -EINVAL;
487 
488 	cldev = to_mei_cl_device(dev);
489 
490 	verify_repeater_in.header.api_version = HDCP_API_VERSION;
491 	verify_repeater_in.header.command_id = WIRED_VERIFY_REPEATER;
492 	verify_repeater_in.header.status = FW_HDCP_STATUS_SUCCESS;
493 	verify_repeater_in.header.buffer_len =
494 					WIRED_CMD_BUF_LEN_VERIFY_REPEATER_IN;
495 
496 	verify_repeater_in.port.integrated_port_type = data->port_type;
497 	verify_repeater_in.port.physical_port = (u8)data->hdcp_ddi;
498 	verify_repeater_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
499 
500 	memcpy(verify_repeater_in.rx_info, rep_topology->rx_info,
501 	       HDCP_2_2_RXINFO_LEN);
502 	memcpy(verify_repeater_in.seq_num_v, rep_topology->seq_num_v,
503 	       HDCP_2_2_SEQ_NUM_LEN);
504 	memcpy(verify_repeater_in.v_prime, rep_topology->v_prime,
505 	       HDCP_2_2_V_PRIME_HALF_LEN);
506 	memcpy(verify_repeater_in.receiver_ids, rep_topology->receiver_ids,
507 	       HDCP_2_2_RECEIVER_IDS_MAX_LEN);
508 
509 	byte = mei_cldev_send(cldev, (u8 *)&verify_repeater_in,
510 			      sizeof(verify_repeater_in));
511 	if (byte < 0) {
512 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
513 		return byte;
514 	}
515 
516 	byte = mei_cldev_recv(cldev, (u8 *)&verify_repeater_out,
517 			      sizeof(verify_repeater_out));
518 	if (byte < 0) {
519 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
520 		return byte;
521 	}
522 
523 	if (verify_repeater_out.header.status != FW_HDCP_STATUS_SUCCESS) {
524 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
525 			WIRED_VERIFY_REPEATER,
526 			verify_repeater_out.header.status);
527 		return -EIO;
528 	}
529 
530 	memcpy(rep_send_ack->v, verify_repeater_out.v,
531 	       HDCP_2_2_V_PRIME_HALF_LEN);
532 	rep_send_ack->msg_id = HDCP_2_2_REP_SEND_ACK;
533 
534 	return 0;
535 }
536 
537 /**
538  * mei_hdcp_verify_mprime() - Verify mprime.
539  * @dev: device corresponding to the mei_cl_device
540  * @data: Intel HW specific hdcp data
541  * @stream_ready: RepeaterAuth_Stream_Ready msg for ME FW verification.
542  *
543  * Return: 0 on Success, <0 on Failure
544  */
545 static int mei_hdcp_verify_mprime(struct device *dev,
546 				  struct hdcp_port_data *data,
547 				  struct hdcp2_rep_stream_ready *stream_ready)
548 {
549 	struct wired_cmd_repeater_auth_stream_req_in *verify_mprime_in;
550 	struct wired_cmd_repeater_auth_stream_req_out
551 					verify_mprime_out = { { 0 } };
552 	struct mei_cl_device *cldev;
553 	ssize_t byte;
554 	size_t cmd_size;
555 
556 	if (!dev || !stream_ready || !data)
557 		return -EINVAL;
558 
559 	cldev = to_mei_cl_device(dev);
560 
561 	cmd_size = struct_size(verify_mprime_in, streams, data->k);
562 	if (cmd_size == SIZE_MAX)
563 		return -EINVAL;
564 
565 	verify_mprime_in = kzalloc(cmd_size, GFP_KERNEL);
566 	if (!verify_mprime_in)
567 		return -ENOMEM;
568 
569 	verify_mprime_in->header.api_version = HDCP_API_VERSION;
570 	verify_mprime_in->header.command_id = WIRED_REPEATER_AUTH_STREAM_REQ;
571 	verify_mprime_in->header.status = FW_HDCP_STATUS_SUCCESS;
572 	verify_mprime_in->header.buffer_len = cmd_size  - sizeof(verify_mprime_in->header);
573 
574 	verify_mprime_in->port.integrated_port_type = data->port_type;
575 	verify_mprime_in->port.physical_port = (u8)data->hdcp_ddi;
576 	verify_mprime_in->port.attached_transcoder = (u8)data->hdcp_transcoder;
577 
578 	memcpy(verify_mprime_in->m_prime, stream_ready->m_prime, HDCP_2_2_MPRIME_LEN);
579 	drm_hdcp_cpu_to_be24(verify_mprime_in->seq_num_m, data->seq_num_m);
580 
581 	memcpy(verify_mprime_in->streams, data->streams,
582 	       array_size(data->k, sizeof(*data->streams)));
583 
584 	verify_mprime_in->k = cpu_to_be16(data->k);
585 
586 	byte = mei_cldev_send(cldev, (u8 *)verify_mprime_in, cmd_size);
587 	kfree(verify_mprime_in);
588 	if (byte < 0) {
589 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
590 		return byte;
591 	}
592 
593 	byte = mei_cldev_recv(cldev, (u8 *)&verify_mprime_out,
594 			      sizeof(verify_mprime_out));
595 	if (byte < 0) {
596 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
597 		return byte;
598 	}
599 
600 	if (verify_mprime_out.header.status != FW_HDCP_STATUS_SUCCESS) {
601 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
602 			WIRED_REPEATER_AUTH_STREAM_REQ,
603 			verify_mprime_out.header.status);
604 		return -EIO;
605 	}
606 
607 	return 0;
608 }
609 
610 /**
611  * mei_hdcp_enable_authentication() - Mark a port as authenticated
612  * through ME FW
613  * @dev: device corresponding to the mei_cl_device
614  * @data: Intel HW specific hdcp data
615  *
616  * Return: 0 on Success, <0 on Failure
617  */
618 static int mei_hdcp_enable_authentication(struct device *dev,
619 					  struct hdcp_port_data *data)
620 {
621 	struct wired_cmd_enable_auth_in enable_auth_in = { { 0 } };
622 	struct wired_cmd_enable_auth_out enable_auth_out = { { 0 } };
623 	struct mei_cl_device *cldev;
624 	ssize_t byte;
625 
626 	if (!dev || !data)
627 		return -EINVAL;
628 
629 	cldev = to_mei_cl_device(dev);
630 
631 	enable_auth_in.header.api_version = HDCP_API_VERSION;
632 	enable_auth_in.header.command_id = WIRED_ENABLE_AUTH;
633 	enable_auth_in.header.status = FW_HDCP_STATUS_SUCCESS;
634 	enable_auth_in.header.buffer_len = WIRED_CMD_BUF_LEN_ENABLE_AUTH_IN;
635 
636 	enable_auth_in.port.integrated_port_type = data->port_type;
637 	enable_auth_in.port.physical_port = (u8)data->hdcp_ddi;
638 	enable_auth_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
639 	enable_auth_in.stream_type = data->streams[0].stream_type;
640 
641 	byte = mei_cldev_send(cldev, (u8 *)&enable_auth_in,
642 			      sizeof(enable_auth_in));
643 	if (byte < 0) {
644 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
645 		return byte;
646 	}
647 
648 	byte = mei_cldev_recv(cldev, (u8 *)&enable_auth_out,
649 			      sizeof(enable_auth_out));
650 	if (byte < 0) {
651 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
652 		return byte;
653 	}
654 
655 	if (enable_auth_out.header.status != FW_HDCP_STATUS_SUCCESS) {
656 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
657 			WIRED_ENABLE_AUTH, enable_auth_out.header.status);
658 		return -EIO;
659 	}
660 
661 	return 0;
662 }
663 
664 /**
665  * mei_hdcp_close_session() - Close the Wired HDCP Tx session of ME FW per port.
666  * This also disables the authenticated state of the port.
667  * @dev: device corresponding to the mei_cl_device
668  * @data: Intel HW specific hdcp data
669  *
670  * Return: 0 on Success, <0 on Failure
671  */
672 static int
673 mei_hdcp_close_session(struct device *dev, struct hdcp_port_data *data)
674 {
675 	struct wired_cmd_close_session_in session_close_in = { { 0 } };
676 	struct wired_cmd_close_session_out session_close_out = { { 0 } };
677 	struct mei_cl_device *cldev;
678 	ssize_t byte;
679 
680 	if (!dev || !data)
681 		return -EINVAL;
682 
683 	cldev = to_mei_cl_device(dev);
684 
685 	session_close_in.header.api_version = HDCP_API_VERSION;
686 	session_close_in.header.command_id = WIRED_CLOSE_SESSION;
687 	session_close_in.header.status = FW_HDCP_STATUS_SUCCESS;
688 	session_close_in.header.buffer_len =
689 				WIRED_CMD_BUF_LEN_CLOSE_SESSION_IN;
690 
691 	session_close_in.port.integrated_port_type = data->port_type;
692 	session_close_in.port.physical_port = (u8)data->hdcp_ddi;
693 	session_close_in.port.attached_transcoder = (u8)data->hdcp_transcoder;
694 
695 	byte = mei_cldev_send(cldev, (u8 *)&session_close_in,
696 			      sizeof(session_close_in));
697 	if (byte < 0) {
698 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
699 		return byte;
700 	}
701 
702 	byte = mei_cldev_recv(cldev, (u8 *)&session_close_out,
703 			      sizeof(session_close_out));
704 	if (byte < 0) {
705 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
706 		return byte;
707 	}
708 
709 	if (session_close_out.header.status != FW_HDCP_STATUS_SUCCESS) {
710 		dev_dbg(dev, "Session Close Failed. status: 0x%X\n",
711 			session_close_out.header.status);
712 		return -EIO;
713 	}
714 
715 	return 0;
716 }
717 
718 static const struct i915_hdcp_ops mei_hdcp_ops = {
719 	.owner = THIS_MODULE,
720 	.initiate_hdcp2_session = mei_hdcp_initiate_session,
721 	.verify_receiver_cert_prepare_km =
722 				mei_hdcp_verify_receiver_cert_prepare_km,
723 	.verify_hprime = mei_hdcp_verify_hprime,
724 	.store_pairing_info = mei_hdcp_store_pairing_info,
725 	.initiate_locality_check = mei_hdcp_initiate_locality_check,
726 	.verify_lprime = mei_hdcp_verify_lprime,
727 	.get_session_key = mei_hdcp_get_session_key,
728 	.repeater_check_flow_prepare_ack =
729 				mei_hdcp_repeater_check_flow_prepare_ack,
730 	.verify_mprime = mei_hdcp_verify_mprime,
731 	.enable_hdcp_authentication = mei_hdcp_enable_authentication,
732 	.close_hdcp_session = mei_hdcp_close_session,
733 };
734 
735 static int mei_component_master_bind(struct device *dev)
736 {
737 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
738 	struct i915_hdcp_arbiter *comp_arbiter = mei_cldev_get_drvdata(cldev);
739 	int ret;
740 
741 	dev_dbg(dev, "%s\n", __func__);
742 	comp_arbiter->ops = &mei_hdcp_ops;
743 	comp_arbiter->hdcp_dev = dev;
744 	ret = component_bind_all(dev, comp_arbiter);
745 	if (ret < 0)
746 		return ret;
747 
748 	return 0;
749 }
750 
751 static void mei_component_master_unbind(struct device *dev)
752 {
753 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
754 	struct i915_hdcp_arbiter *comp_arbiter = mei_cldev_get_drvdata(cldev);
755 
756 	dev_dbg(dev, "%s\n", __func__);
757 	component_unbind_all(dev, comp_arbiter);
758 }
759 
760 static const struct component_master_ops mei_component_master_ops = {
761 	.bind = mei_component_master_bind,
762 	.unbind = mei_component_master_unbind,
763 };
764 
765 /**
766  * mei_hdcp_component_match - compare function for matching mei hdcp.
767  *
768  *    The function checks if the driver is i915, the subcomponent is HDCP
769  *    and the grand parent of hdcp and the parent of i915 are the same
770  *    PCH device.
771  *
772  * @dev: master device
773  * @subcomponent: subcomponent to match (I915_COMPONENT_HDCP)
774  * @data: compare data (mei hdcp device)
775  *
776  * Return:
777  * * 1 - if components match
778  * * 0 - otherwise
779  */
780 static int mei_hdcp_component_match(struct device *dev, int subcomponent,
781 				    void *data)
782 {
783 	struct device *base = data;
784 
785 	if (!dev->driver || strcmp(dev->driver->name, "i915") ||
786 	    subcomponent != I915_COMPONENT_HDCP)
787 		return 0;
788 
789 	base = base->parent;
790 	if (!base)
791 		return 0;
792 
793 	base = base->parent;
794 	dev = dev->parent;
795 
796 	return (base && dev && dev == base);
797 }
798 
799 static int mei_hdcp_probe(struct mei_cl_device *cldev,
800 			  const struct mei_cl_device_id *id)
801 {
802 	struct i915_hdcp_arbiter *comp_arbiter;
803 	struct component_match *master_match;
804 	int ret;
805 
806 	ret = mei_cldev_enable(cldev);
807 	if (ret < 0) {
808 		dev_err(&cldev->dev, "mei_cldev_enable Failed. %d\n", ret);
809 		goto enable_err_exit;
810 	}
811 
812 	comp_arbiter = kzalloc(sizeof(*comp_arbiter), GFP_KERNEL);
813 	if (!comp_arbiter) {
814 		ret = -ENOMEM;
815 		goto err_exit;
816 	}
817 
818 	master_match = NULL;
819 	component_match_add_typed(&cldev->dev, &master_match,
820 				  mei_hdcp_component_match, &cldev->dev);
821 	if (IS_ERR_OR_NULL(master_match)) {
822 		ret = -ENOMEM;
823 		goto err_exit;
824 	}
825 
826 	mei_cldev_set_drvdata(cldev, comp_arbiter);
827 	ret = component_master_add_with_match(&cldev->dev,
828 					      &mei_component_master_ops,
829 					      master_match);
830 	if (ret < 0) {
831 		dev_err(&cldev->dev, "Master comp add failed %d\n", ret);
832 		goto err_exit;
833 	}
834 
835 	return 0;
836 
837 err_exit:
838 	mei_cldev_set_drvdata(cldev, NULL);
839 	kfree(comp_arbiter);
840 	mei_cldev_disable(cldev);
841 enable_err_exit:
842 	return ret;
843 }
844 
845 static void mei_hdcp_remove(struct mei_cl_device *cldev)
846 {
847 	struct i915_hdcp_arbiter *comp_arbiter = mei_cldev_get_drvdata(cldev);
848 	int ret;
849 
850 	component_master_del(&cldev->dev, &mei_component_master_ops);
851 	kfree(comp_arbiter);
852 	mei_cldev_set_drvdata(cldev, NULL);
853 
854 	ret = mei_cldev_disable(cldev);
855 	if (ret)
856 		dev_warn(&cldev->dev, "mei_cldev_disable() failed\n");
857 }
858 
859 #define MEI_UUID_HDCP UUID_LE(0xB638AB7E, 0x94E2, 0x4EA2, 0xA5, \
860 			      0x52, 0xD1, 0xC5, 0x4B, 0x62, 0x7F, 0x04)
861 
862 static const struct mei_cl_device_id mei_hdcp_tbl[] = {
863 	{ .uuid = MEI_UUID_HDCP, .version = MEI_CL_VERSION_ANY },
864 	{ }
865 };
866 MODULE_DEVICE_TABLE(mei, mei_hdcp_tbl);
867 
868 static struct mei_cl_driver mei_hdcp_driver = {
869 	.id_table = mei_hdcp_tbl,
870 	.name = KBUILD_MODNAME,
871 	.probe = mei_hdcp_probe,
872 	.remove	= mei_hdcp_remove,
873 };
874 
875 module_mei_cl_driver(mei_hdcp_driver);
876 
877 MODULE_AUTHOR("Intel Corporation");
878 MODULE_LICENSE("GPL");
879 MODULE_DESCRIPTION("MEI HDCP");
880