xref: /openbmc/linux/drivers/misc/mei/hdcp/mei_hdcp.c (revision 1d05334d)
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/uuid.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_mei_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 = ME_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->fw_ddi;
61 	session_init_in.port.attached_transcoder = (u8)data->fw_tc;
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 != ME_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 = ME_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->fw_ddi;
131 	verify_rxcert_in.port.attached_transcoder = (u8)data->fw_tc;
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 != ME_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 = ME_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->fw_ddi;
202 	send_hprime_in.port.attached_transcoder = (u8)data->fw_tc;
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 != ME_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 = ME_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->fw_ddi;
260 	pairing_info_in.port.attached_transcoder = (u8)data->fw_tc;
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 != ME_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 = ME_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->fw_ddi;
319 	lc_init_in.port.attached_transcoder = (u8)data->fw_tc;
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 != ME_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 = ME_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->fw_ddi;
375 	verify_lprime_in.port.attached_transcoder = (u8)data->fw_tc;
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 != ME_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 = ME_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->fw_ddi;
433 	get_skey_in.port.attached_transcoder = (u8)data->fw_tc;
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 != ME_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 = ME_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->fw_ddi;
498 	verify_repeater_in.port.attached_transcoder = (u8)data->fw_tc;
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 != ME_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
550 					verify_mprime_in = { { 0 } };
551 	struct wired_cmd_repeater_auth_stream_req_out
552 					verify_mprime_out = { { 0 } };
553 	struct mei_cl_device *cldev;
554 	ssize_t byte;
555 
556 	if (!dev || !stream_ready || !data)
557 		return -EINVAL;
558 
559 	cldev = to_mei_cl_device(dev);
560 
561 	verify_mprime_in.header.api_version = HDCP_API_VERSION;
562 	verify_mprime_in.header.command_id = WIRED_REPEATER_AUTH_STREAM_REQ;
563 	verify_mprime_in.header.status = ME_HDCP_STATUS_SUCCESS;
564 	verify_mprime_in.header.buffer_len =
565 			WIRED_CMD_BUF_LEN_REPEATER_AUTH_STREAM_REQ_MIN_IN;
566 
567 	verify_mprime_in.port.integrated_port_type = data->port_type;
568 	verify_mprime_in.port.physical_port = (u8)data->fw_ddi;
569 	verify_mprime_in.port.attached_transcoder = (u8)data->fw_tc;
570 
571 	memcpy(verify_mprime_in.m_prime, stream_ready->m_prime,
572 	       HDCP_2_2_MPRIME_LEN);
573 	drm_hdcp_cpu_to_be24(verify_mprime_in.seq_num_m, data->seq_num_m);
574 	memcpy(verify_mprime_in.streams, data->streams,
575 	       (data->k * sizeof(struct hdcp2_streamid_type)));
576 
577 	verify_mprime_in.k = cpu_to_be16(data->k);
578 
579 	byte = mei_cldev_send(cldev, (u8 *)&verify_mprime_in,
580 			      sizeof(verify_mprime_in));
581 	if (byte < 0) {
582 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
583 		return byte;
584 	}
585 
586 	byte = mei_cldev_recv(cldev, (u8 *)&verify_mprime_out,
587 			      sizeof(verify_mprime_out));
588 	if (byte < 0) {
589 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
590 		return byte;
591 	}
592 
593 	if (verify_mprime_out.header.status != ME_HDCP_STATUS_SUCCESS) {
594 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
595 			WIRED_REPEATER_AUTH_STREAM_REQ,
596 			verify_mprime_out.header.status);
597 		return -EIO;
598 	}
599 
600 	return 0;
601 }
602 
603 /**
604  * mei_hdcp_enable_authentication() - Mark a port as authenticated
605  * through ME FW
606  * @dev: device corresponding to the mei_cl_device
607  * @data: Intel HW specific hdcp data
608  *
609  * Return: 0 on Success, <0 on Failure
610  */
611 static int mei_hdcp_enable_authentication(struct device *dev,
612 					  struct hdcp_port_data *data)
613 {
614 	struct wired_cmd_enable_auth_in enable_auth_in = { { 0 } };
615 	struct wired_cmd_enable_auth_out enable_auth_out = { { 0 } };
616 	struct mei_cl_device *cldev;
617 	ssize_t byte;
618 
619 	if (!dev || !data)
620 		return -EINVAL;
621 
622 	cldev = to_mei_cl_device(dev);
623 
624 	enable_auth_in.header.api_version = HDCP_API_VERSION;
625 	enable_auth_in.header.command_id = WIRED_ENABLE_AUTH;
626 	enable_auth_in.header.status = ME_HDCP_STATUS_SUCCESS;
627 	enable_auth_in.header.buffer_len = WIRED_CMD_BUF_LEN_ENABLE_AUTH_IN;
628 
629 	enable_auth_in.port.integrated_port_type = data->port_type;
630 	enable_auth_in.port.physical_port = (u8)data->fw_ddi;
631 	enable_auth_in.port.attached_transcoder = (u8)data->fw_tc;
632 	enable_auth_in.stream_type = data->streams[0].stream_type;
633 
634 	byte = mei_cldev_send(cldev, (u8 *)&enable_auth_in,
635 			      sizeof(enable_auth_in));
636 	if (byte < 0) {
637 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
638 		return byte;
639 	}
640 
641 	byte = mei_cldev_recv(cldev, (u8 *)&enable_auth_out,
642 			      sizeof(enable_auth_out));
643 	if (byte < 0) {
644 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
645 		return byte;
646 	}
647 
648 	if (enable_auth_out.header.status != ME_HDCP_STATUS_SUCCESS) {
649 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
650 			WIRED_ENABLE_AUTH, enable_auth_out.header.status);
651 		return -EIO;
652 	}
653 
654 	return 0;
655 }
656 
657 /**
658  * mei_hdcp_close_session() - Close the Wired HDCP Tx session of ME FW per port.
659  * This also disables the authenticated state of the port.
660  * @dev: device corresponding to the mei_cl_device
661  * @data: Intel HW specific hdcp data
662  *
663  * Return: 0 on Success, <0 on Failure
664  */
665 static int
666 mei_hdcp_close_session(struct device *dev, struct hdcp_port_data *data)
667 {
668 	struct wired_cmd_close_session_in session_close_in = { { 0 } };
669 	struct wired_cmd_close_session_out session_close_out = { { 0 } };
670 	struct mei_cl_device *cldev;
671 	ssize_t byte;
672 
673 	if (!dev || !data)
674 		return -EINVAL;
675 
676 	cldev = to_mei_cl_device(dev);
677 
678 	session_close_in.header.api_version = HDCP_API_VERSION;
679 	session_close_in.header.command_id = WIRED_CLOSE_SESSION;
680 	session_close_in.header.status = ME_HDCP_STATUS_SUCCESS;
681 	session_close_in.header.buffer_len =
682 				WIRED_CMD_BUF_LEN_CLOSE_SESSION_IN;
683 
684 	session_close_in.port.integrated_port_type = data->port_type;
685 	session_close_in.port.physical_port = (u8)data->fw_ddi;
686 	session_close_in.port.attached_transcoder = (u8)data->fw_tc;
687 
688 	byte = mei_cldev_send(cldev, (u8 *)&session_close_in,
689 			      sizeof(session_close_in));
690 	if (byte < 0) {
691 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
692 		return byte;
693 	}
694 
695 	byte = mei_cldev_recv(cldev, (u8 *)&session_close_out,
696 			      sizeof(session_close_out));
697 	if (byte < 0) {
698 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
699 		return byte;
700 	}
701 
702 	if (session_close_out.header.status != ME_HDCP_STATUS_SUCCESS) {
703 		dev_dbg(dev, "Session Close Failed. status: 0x%X\n",
704 			session_close_out.header.status);
705 		return -EIO;
706 	}
707 
708 	return 0;
709 }
710 
711 static const struct i915_hdcp_component_ops mei_hdcp_ops = {
712 	.owner = THIS_MODULE,
713 	.initiate_hdcp2_session = mei_hdcp_initiate_session,
714 	.verify_receiver_cert_prepare_km =
715 				mei_hdcp_verify_receiver_cert_prepare_km,
716 	.verify_hprime = mei_hdcp_verify_hprime,
717 	.store_pairing_info = mei_hdcp_store_pairing_info,
718 	.initiate_locality_check = mei_hdcp_initiate_locality_check,
719 	.verify_lprime = mei_hdcp_verify_lprime,
720 	.get_session_key = mei_hdcp_get_session_key,
721 	.repeater_check_flow_prepare_ack =
722 				mei_hdcp_repeater_check_flow_prepare_ack,
723 	.verify_mprime = mei_hdcp_verify_mprime,
724 	.enable_hdcp_authentication = mei_hdcp_enable_authentication,
725 	.close_hdcp_session = mei_hdcp_close_session,
726 };
727 
728 static int mei_component_master_bind(struct device *dev)
729 {
730 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
731 	struct i915_hdcp_comp_master *comp_master =
732 						mei_cldev_get_drvdata(cldev);
733 	int ret;
734 
735 	dev_dbg(dev, "%s\n", __func__);
736 	comp_master->ops = &mei_hdcp_ops;
737 	comp_master->mei_dev = dev;
738 	ret = component_bind_all(dev, comp_master);
739 	if (ret < 0)
740 		return ret;
741 
742 	return 0;
743 }
744 
745 static void mei_component_master_unbind(struct device *dev)
746 {
747 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
748 	struct i915_hdcp_comp_master *comp_master =
749 						mei_cldev_get_drvdata(cldev);
750 
751 	dev_dbg(dev, "%s\n", __func__);
752 	component_unbind_all(dev, comp_master);
753 }
754 
755 static const struct component_master_ops mei_component_master_ops = {
756 	.bind = mei_component_master_bind,
757 	.unbind = mei_component_master_unbind,
758 };
759 
760 /**
761  * mei_hdcp_component_match - compare function for matching mei hdcp.
762  *
763  *    The function checks if the driver is i915, the subcomponent is HDCP
764  *    and the grand parent of hdcp and the parent of i915 are the same
765  *    PCH device.
766  *
767  * @dev: master device
768  * @subcomponent: subcomponent to match (I915_COMPONENT_HDCP)
769  * @data: compare data (mei hdcp device)
770  *
771  * Return:
772  * * 1 - if components match
773  * * 0 - otherwise
774  */
775 static int mei_hdcp_component_match(struct device *dev, int subcomponent,
776 				    void *data)
777 {
778 	struct device *base = data;
779 
780 	if (strcmp(dev->driver->name, "i915") ||
781 	    subcomponent != I915_COMPONENT_HDCP)
782 		return 0;
783 
784 	base = base->parent;
785 	if (!base)
786 		return 0;
787 
788 	base = base->parent;
789 	dev = dev->parent;
790 
791 	return (base && dev && dev == base);
792 }
793 
794 static int mei_hdcp_probe(struct mei_cl_device *cldev,
795 			  const struct mei_cl_device_id *id)
796 {
797 	struct i915_hdcp_comp_master *comp_master;
798 	struct component_match *master_match;
799 	int ret;
800 
801 	ret = mei_cldev_enable(cldev);
802 	if (ret < 0) {
803 		dev_err(&cldev->dev, "mei_cldev_enable Failed. %d\n", ret);
804 		goto enable_err_exit;
805 	}
806 
807 	comp_master = kzalloc(sizeof(*comp_master), GFP_KERNEL);
808 	if (!comp_master) {
809 		ret = -ENOMEM;
810 		goto err_exit;
811 	}
812 
813 	master_match = NULL;
814 	component_match_add_typed(&cldev->dev, &master_match,
815 				  mei_hdcp_component_match, &cldev->dev);
816 	if (IS_ERR_OR_NULL(master_match)) {
817 		ret = -ENOMEM;
818 		goto err_exit;
819 	}
820 
821 	mei_cldev_set_drvdata(cldev, comp_master);
822 	ret = component_master_add_with_match(&cldev->dev,
823 					      &mei_component_master_ops,
824 					      master_match);
825 	if (ret < 0) {
826 		dev_err(&cldev->dev, "Master comp add failed %d\n", ret);
827 		goto err_exit;
828 	}
829 
830 	return 0;
831 
832 err_exit:
833 	mei_cldev_set_drvdata(cldev, NULL);
834 	kfree(comp_master);
835 	mei_cldev_disable(cldev);
836 enable_err_exit:
837 	return ret;
838 }
839 
840 static int mei_hdcp_remove(struct mei_cl_device *cldev)
841 {
842 	struct i915_hdcp_comp_master *comp_master =
843 						mei_cldev_get_drvdata(cldev);
844 
845 	component_master_del(&cldev->dev, &mei_component_master_ops);
846 	kfree(comp_master);
847 	mei_cldev_set_drvdata(cldev, NULL);
848 
849 	return mei_cldev_disable(cldev);
850 }
851 
852 #define MEI_UUID_HDCP GUID_INIT(0xB638AB7E, 0x94E2, 0x4EA2, 0xA5, \
853 				0x52, 0xD1, 0xC5, 0x4B, 0x62, 0x7F, 0x04)
854 
855 static struct mei_cl_device_id mei_hdcp_tbl[] = {
856 	{ .uuid = MEI_UUID_HDCP, .version = MEI_CL_VERSION_ANY },
857 	{ }
858 };
859 MODULE_DEVICE_TABLE(mei, mei_hdcp_tbl);
860 
861 static struct mei_cl_driver mei_hdcp_driver = {
862 	.id_table = mei_hdcp_tbl,
863 	.name = KBUILD_MODNAME,
864 	.probe = mei_hdcp_probe,
865 	.remove	= mei_hdcp_remove,
866 };
867 
868 module_mei_cl_driver(mei_hdcp_driver);
869 
870 MODULE_AUTHOR("Intel Corporation");
871 MODULE_LICENSE("GPL");
872 MODULE_DESCRIPTION("MEI HDCP");
873