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