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