xref: /OK3568_Linux_fs/kernel/drivers/media/platform/rockchip/hdmirx/rk_hdmirx_hdcp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
4  *
5  * Author: Shunqing Chen <csq@rock-chips.com>
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/rockchip/rockchip_sip.h>
14 #include <linux/sched.h>
15 #include <linux/soc/rockchip/rk_vendor_storage.h>
16 #include <linux/slab.h>
17 
18 #include "rk_hdmirx.h"
19 #include "rk_hdmirx_hdcp.h"
20 
21 enum rk_hdmirx_hdcp_state {
22 	HDMIRX_HDCP_DISABLED,
23 	HDMIRX_HDCP_AUTH_START,
24 	HDMIRX_HDCP_AUTH_SUCCESS,
25 	HDMIRX_HDCP_AUTH_FAIL,
26 };
27 
28 static struct rk_hdmirx_hdcp *g_hdmirx_hdcp;
29 
hdmirx_hdcp_write(struct rk_hdmirx_hdcp * hdcp,int reg,u32 val)30 static void hdmirx_hdcp_write(struct rk_hdmirx_hdcp *hdcp, int reg, u32 val)
31 {
32 	hdcp->write(hdcp->hdmirx, reg, val);
33 }
34 
hdmirx_hdcp_read(struct rk_hdmirx_hdcp * hdcp,int reg)35 static u32 hdmirx_hdcp_read(struct rk_hdmirx_hdcp *hdcp, int reg)
36 {
37 	return hdcp->read(hdcp->hdmirx, reg);
38 }
39 
hdmirx_hdcp_update_bits(struct rk_hdmirx_hdcp * hdcp,int reg,u32 mask,u32 data)40 static void hdmirx_hdcp_update_bits(struct rk_hdmirx_hdcp *hdcp, int reg,
41 				    u32 mask, u32 data)
42 {
43 	u32 val = hdmirx_hdcp_read(hdcp, reg) & ~mask;
44 
45 	val |= (data & mask);
46 	hdmirx_hdcp_write(hdcp, reg, val);
47 }
48 
hdcp_load_keys_cb(struct rk_hdmirx_hdcp * hdcp)49 static int hdcp_load_keys_cb(struct rk_hdmirx_hdcp *hdcp)
50 {
51 	int size;
52 	u8 hdcp_vendor_data[VENDOR_DATA_SIZE + 1];
53 	struct hdcp_key_data_t *key_data;
54 	void __iomem *base;
55 
56 	size = rk_vendor_read(HDMIRX_HDCP1X_ID, hdcp_vendor_data,
57 			      VENDOR_DATA_SIZE);
58 	if (size < (HDCP_KEY_SIZE + HDCP_KEY_SEED_SIZE)) {
59 		dev_dbg(hdcp->dev, "HDCP: read size %d\n", size);
60 		return -EINVAL;
61 	}
62 
63 	key_data = (struct hdcp_key_data_t *)hdcp_vendor_data;
64 	if ((key_data->signature != HDCP_SIG_MAGIC)
65 	    || !(key_data->flags & HDCP_FLG_AES))
66 		hdcp->aes_encrypt = false;
67 	else
68 		hdcp->aes_encrypt = true;
69 
70 	base = sip_hdcp_request_share_memory(HDMI_RX);
71 	if (!base)
72 		return -ENOMEM;
73 
74 	memcpy_toio(base, hdcp_vendor_data, size);
75 	hdcp->keys_is_load = true;
76 
77 	return 0;
78 }
79 
rk_hdmirx_hdcp_load_key(struct rk_hdmirx_hdcp * hdcp)80 static int rk_hdmirx_hdcp_load_key(struct rk_hdmirx_hdcp *hdcp)
81 {
82 	int ret = 0;
83 
84 	hdcp->status = HDMIRX_HDCP_DISABLED;
85 	if (!hdcp->keys_is_load)
86 		ret = hdcp_load_keys_cb(hdcp);
87 
88 	if (ret && !hdcp->test_key_load)
89 		return -EINVAL;
90 
91 	hdcp->status = HDMIRX_HDCP_AUTH_START;
92 	if (hdcp->aes_encrypt)
93 		sip_hdcp_config(HDCP_FUNC_KEY_LOAD, HDMI_RX, 0);
94 	else
95 		sip_hdcp_config(HDCP_FUNC_KEY_LOAD, HDMI_RX, 1);
96 
97 	hdmirx_hdcp_update_bits(hdcp, HDCP2_CONFIG,
98 				HDCP2_CONNECTED |
99 				HDCP2_SWITCH_OVR_VALUE,
100 				HDCP2_CONNECTED);
101 
102 	return 0;
103 }
104 
rk_hdmirx_hdcp1x_start(struct rk_hdmirx_hdcp * hdcp)105 static int rk_hdmirx_hdcp1x_start(struct rk_hdmirx_hdcp *hdcp)
106 {
107 	rk_hdmirx_hdcp_load_key(hdcp);
108 
109 	dev_dbg(hdcp->dev, "%s success\n", __func__);
110 	return 0;
111 }
112 
rk_hdmirx_hdcp1x_stop(struct rk_hdmirx_hdcp * hdcp)113 static int rk_hdmirx_hdcp1x_stop(struct rk_hdmirx_hdcp *hdcp)
114 {
115 	if (hdcp->status == HDMIRX_HDCP_DISABLED)
116 		return 0;
117 
118 	hdmirx_hdcp_update_bits(hdcp, GLOBAL_SWENABLE, HDCP_ENABLE, 0);
119 	hdcp->status = HDMIRX_HDCP_DISABLED;
120 
121 	return 0;
122 }
123 
rk_hdmirx_hdcp2_hpd_config(struct rk_hdmirx_hdcp * hdcp,bool en)124 static void rk_hdmirx_hdcp2_hpd_config(struct rk_hdmirx_hdcp *hdcp, bool en)
125 {
126 	if (hdcp->tx_5v_power(hdcp->hdmirx))
127 		hdcp->hpd_config(hdcp->hdmirx, en);
128 }
129 
rk_hdmirx_hdcp2x_start(struct rk_hdmirx_hdcp * hdcp)130 static int rk_hdmirx_hdcp2x_start(struct rk_hdmirx_hdcp *hdcp)
131 {
132 	hdmirx_hdcp_update_bits(hdcp, HDCP2_CONFIG,
133 				HDCP2_SWITCH_OVR_VALUE |
134 				HDCP2_SWITCH_LCK |
135 				HDCP2_SWITCH_OVR_EN,
136 				HDCP2_SWITCH_LCK);
137 	if (hdcp->tx_5v_power(hdcp->hdmirx))
138 		hdmirx_hdcp_write(hdcp, HDCP2_ESM_P0_GPIO_IN, 0x2);
139 
140 	dev_dbg(hdcp->dev, "%s success\n", __func__);
141 	return 0;
142 }
143 
rk_hdmirx_hdcp2x_stop(struct rk_hdmirx_hdcp * hdcp)144 static int rk_hdmirx_hdcp2x_stop(struct rk_hdmirx_hdcp *hdcp)
145 {
146 	hdmirx_hdcp_write(hdcp, HDCP2_ESM_P0_GPIO_IN, 0x0);
147 	hdmirx_hdcp_update_bits(hdcp, HDCP2_CONFIG,
148 				HDCP2_SWITCH_OVR_VALUE |
149 				HDCP2_SWITCH_LCK |
150 				HDCP2_SWITCH_OVR_EN,
151 				HDCP2_SWITCH_OVR_EN);
152 
153 	return 0;
154 }
155 
rk_hdmirx_hdcp2_connect_ctrl(struct rk_hdmirx_hdcp * hdcp,bool en)156 static void rk_hdmirx_hdcp2_connect_ctrl(struct rk_hdmirx_hdcp *hdcp, bool en)
157 {
158 	if (hdcp->enable != HDCP_2X_ENABLE)
159 		return;
160 	if (en) {
161 		hdmirx_hdcp_write(hdcp, HDCP2_ESM_P0_GPIO_IN, 0x2);
162 		hdmirx_hdcp_update_bits(hdcp, HDCP2_CONFIG, HDCP2_CONNECTED,
163 					HDCP2_CONNECTED);
164 	} else {
165 		hdmirx_hdcp_write(hdcp, HDCP2_ESM_P0_GPIO_IN, 0x0);
166 		hdmirx_hdcp_update_bits(hdcp, HDCP2_CONFIG,
167 					HDCP2_CONNECTED, 0);
168 	}
169 }
170 
rk_hdmirx_hdcp_start(struct rk_hdmirx_hdcp * hdcp)171 static int rk_hdmirx_hdcp_start(struct rk_hdmirx_hdcp *hdcp)
172 {
173 	if (hdcp->enable == HDCP_2X_ENABLE) {
174 		rk_hdmirx_hdcp1x_start(hdcp);
175 		rk_hdmirx_hdcp2x_start(hdcp);
176 		return 0;
177 	}
178 	hdmirx_hdcp_update_bits(hdcp, HDCP2_CONFIG,
179 				HDCP2_SWITCH_OVR_EN,
180 				HDCP2_SWITCH_OVR_EN);
181 	if (hdcp->enable == HDCP_1X_ENABLE)
182 		rk_hdmirx_hdcp1x_start(hdcp);
183 
184 	return 0;
185 }
186 
rk_hdmirx_hdcp_stop(struct rk_hdmirx_hdcp * hdcp)187 static int rk_hdmirx_hdcp_stop(struct rk_hdmirx_hdcp *hdcp)
188 {
189 	if (!hdcp->enable)
190 		return 0;
191 
192 	if (hdcp->enable == HDCP_2X_ENABLE)
193 		rk_hdmirx_hdcp2x_stop(hdcp);
194 	rk_hdmirx_hdcp1x_stop(hdcp);
195 	dev_dbg(hdcp->dev, "hdcp stop\n");
196 
197 	return 0;
198 }
199 
enable_show(struct device * device,struct device_attribute * attr,char * buf)200 static ssize_t enable_show(struct device *device,
201 				struct device_attribute *attr, char *buf)
202 {
203 	u8 enable = 0;
204 	struct rk_hdmirx_hdcp *hdcp = g_hdmirx_hdcp;
205 
206 	if (hdcp)
207 		enable = hdcp->enable;
208 
209 	return snprintf(buf, PAGE_SIZE, "%d\n", enable);
210 }
211 
enable_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)212 static ssize_t enable_store(struct device *device,
213 			    struct device_attribute *attr,
214 			    const char *buf, size_t count)
215 {
216 	int enable;
217 	struct rk_hdmirx_hdcp *hdcp = g_hdmirx_hdcp;
218 
219 	if (!hdcp)
220 		return -EINVAL;
221 
222 	if (kstrtoint(buf, 10, &enable))
223 		return -EINVAL;
224 
225 	if (enable > hdcp->hdcp_support)
226 		return count;
227 
228 	if (hdcp->enable != enable) {
229 		rk_hdmirx_hdcp2_hpd_config(hdcp, false);
230 		if (enable == HDCP_2X_ENABLE) {
231 			rk_hdmirx_hdcp1x_start(hdcp);
232 			rk_hdmirx_hdcp2x_start(hdcp);
233 		} else if (enable == HDCP_1X_ENABLE) {
234 			if (hdcp->enable == HDCP_2X_ENABLE)
235 				rk_hdmirx_hdcp2x_stop(hdcp);
236 			rk_hdmirx_hdcp1x_start(hdcp);
237 		} else {
238 			rk_hdmirx_hdcp_stop(hdcp);
239 		}
240 		msleep(300);
241 		rk_hdmirx_hdcp2_hpd_config(hdcp, true);
242 		hdcp->enable = enable;
243 	}
244 
245 	return count;
246 }
247 
248 static DEVICE_ATTR_RW(enable);
249 
status_show(struct device * device,struct device_attribute * attr,char * buf)250 static ssize_t status_show(struct device *device,
251 			   struct device_attribute *attr, char *buf)
252 {
253 	int status = HDMIRX_HDCP_DISABLED;
254 	struct rk_hdmirx_hdcp *hdcp = g_hdmirx_hdcp;
255 	u32 val;
256 	int dectypt, n = 0;
257 
258 	if (!hdcp)
259 		return 0;
260 
261 	if (!hdcp->enable)
262 		return snprintf(buf, PAGE_SIZE, "HDCP Disable\n");
263 
264 	if (hdcp->enable == HDCP_2X_ENABLE) {
265 		dectypt = hdmirx_hdcp_read(hdcp, HDCP2_STATUS) & BIT(0);
266 		if (dectypt) {
267 			val = hdmirx_hdcp_read(hdcp, HDCP2_ESM_P0_GPIO_OUT);
268 			if (val & BIT(2))
269 				n += snprintf(buf + n, PAGE_SIZE - n,
270 					      "HDCP2.3: Authenticated success\n");
271 			else
272 				n += snprintf(buf + n, PAGE_SIZE - n,
273 					      "HDCP2.3: Authenticated failed\n");
274 			return n;
275 		}
276 		n += snprintf(buf, PAGE_SIZE, "HDCP2.3: No dectypted\n");
277 	}
278 
279 	status = hdcp->status;
280 	if (status == HDMIRX_HDCP_AUTH_START) {
281 		val = hdmirx_hdcp_read(hdcp, HDCP14_STATUS);
282 		if ((val & 0x3) == 0)
283 			status = HDMIRX_HDCP_DISABLED;
284 		else if ((val & 0x3) == 0x1)
285 			status = HDMIRX_HDCP_AUTH_START;
286 		else if (val & BIT(8))
287 			status = HDMIRX_HDCP_AUTH_SUCCESS;
288 		else
289 			status = HDMIRX_HDCP_AUTH_FAIL;
290 	}
291 
292 	if (status == HDMIRX_HDCP_AUTH_START)
293 		n += snprintf(buf + n, PAGE_SIZE, "HDCP1.4: Authenticated start\n");
294 	else if (status == HDMIRX_HDCP_AUTH_SUCCESS)
295 		n += snprintf(buf + n, PAGE_SIZE, "HDCP1.4: Authenticated success\n");
296 	else if (status == HDMIRX_HDCP_AUTH_FAIL)
297 		n += snprintf(buf + n, PAGE_SIZE, "HDCP1.4: Authenticated failed\n");
298 	else
299 		n += snprintf(buf + n, PAGE_SIZE, "HDCP1.4: Unknown status\n");
300 
301 	return n;
302 }
303 
304 static DEVICE_ATTR_RO(status);
305 
support_show(struct device * device,struct device_attribute * attr,char * buf)306 static ssize_t support_show(struct device *device,
307 			    struct device_attribute *attr, char *buf)
308 {
309 	struct rk_hdmirx_hdcp *hdcp = g_hdmirx_hdcp;
310 
311 	return snprintf(buf, PAGE_SIZE, "%d\n", hdcp->hdcp_support);
312 }
313 
314 static DEVICE_ATTR_RO(support);
315 
test_key1x_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)316 static ssize_t test_key1x_store(struct device *device,
317 				struct device_attribute *attr,
318 				const char *buf, size_t count)
319 {
320 	struct rk_hdmirx_hdcp *hdcp = g_hdmirx_hdcp;
321 	u8 hdcp_vendor_data[VENDOR_DATA_SIZE + 1];
322 	struct hdcp_key_data_t *key_data;
323 	void __iomem *base;
324 	char str[4] = {0};
325 	int ret, len, i;
326 
327 	len = count / 2;
328 	if (len < (HDCP_KEY_SIZE + HDCP_KEY_SEED_SIZE))
329 		return -EINVAL;
330 
331 	if (len > VENDOR_DATA_SIZE)
332 		len = VENDOR_DATA_SIZE;
333 
334 	for (i = 0; i < len; i++) {
335 		long c = -1;
336 
337 		memcpy(str, &buf[i * 2], 2);
338 		ret = kstrtol(str, 16, &c);
339 		if (ret || c > 255 || c < 0)
340 			return -EINVAL;
341 		hdcp_vendor_data[i] = (u8)(c & 0xff);
342 	}
343 
344 	key_data = (struct hdcp_key_data_t *)hdcp_vendor_data;
345 	if ((key_data->signature != HDCP_SIG_MAGIC)
346 	    || !(key_data->flags & HDCP_FLG_AES))
347 		hdcp->aes_encrypt = false;
348 	else
349 		hdcp->aes_encrypt = true;
350 
351 	base = sip_hdcp_request_share_memory(HDMI_RX);
352 	if (!base)
353 		return -ENOMEM;
354 	memcpy_toio(base, hdcp_vendor_data, len);
355 	hdcp->test_key_load = true;
356 
357 	if (!hdcp->enable)
358 		return count;
359 
360 	rk_hdmirx_hdcp2_hpd_config(hdcp, false);
361 	rk_hdmirx_hdcp_start(hdcp);
362 	msleep(300);
363 	rk_hdmirx_hdcp2_hpd_config(hdcp, true);
364 
365 	return count;
366 }
367 
368 static DEVICE_ATTR_WO(test_key1x);
369 
rk_hdmirx_hdcp_register(struct rk_hdmirx_hdcp * hdcp_data)370 struct rk_hdmirx_hdcp *rk_hdmirx_hdcp_register(struct rk_hdmirx_hdcp *hdcp_data)
371 {
372 	int ret = 0;
373 	struct rk_hdmirx_hdcp *hdcp;
374 
375 	if (!hdcp_data)
376 		return NULL;
377 
378 
379 	hdcp = devm_kzalloc(hdcp_data->dev, sizeof(*hdcp), GFP_KERNEL);
380 	if (!hdcp)
381 		return NULL;
382 
383 	hdcp->hdmirx = hdcp_data->hdmirx;
384 	hdcp->write = hdcp_data->write;
385 	hdcp->read = hdcp_data->read;
386 	hdcp->tx_5v_power = hdcp_data->tx_5v_power;
387 	hdcp->hpd_config = hdcp_data->hpd_config;
388 	hdcp->enable = hdcp_data->enable;
389 	hdcp->hdcp_support = hdcp_data->enable;
390 	hdcp->dev = hdcp_data->dev;
391 	g_hdmirx_hdcp = hdcp;
392 	hdcp->mdev.minor = MISC_DYNAMIC_MINOR;
393 	hdcp->mdev.name = "hdmirx_hdcp";
394 	hdcp->mdev.mode = 0666;
395 
396 	if (misc_register(&hdcp->mdev)) {
397 		dev_err(hdcp->dev, "HDCP: Could not add character driver\n");
398 		return NULL;
399 	}
400 
401 	ret = device_create_file(hdcp->mdev.this_device, &dev_attr_enable);
402 	if (ret) {
403 		dev_err(hdcp->dev, "HDCP: Could not add sys file enable\n");
404 		goto error0;
405 	}
406 
407 	ret = device_create_file(hdcp->mdev.this_device, &dev_attr_status);
408 	if (ret) {
409 		dev_err(hdcp->dev, "HDCP: Could not add sys file status\n");
410 		goto error1;
411 	}
412 
413 	ret = device_create_file(hdcp->mdev.this_device, &dev_attr_support);
414 	if (ret) {
415 		dev_err(hdcp->dev, "HDCP: Could not add sys file support\n");
416 		goto error2;
417 	}
418 
419 	ret = device_create_file(hdcp->mdev.this_device, &dev_attr_test_key1x);
420 	if (ret) {
421 		dev_err(hdcp->dev, "HDCP: Could not add sys file test_key1x\n");
422 		goto error2;
423 	}
424 
425 	hdcp->hdcp_start = rk_hdmirx_hdcp_start;
426 	hdcp->hdcp_stop = rk_hdmirx_hdcp_stop;
427 	hdcp->hdcp2_connect_ctrl = rk_hdmirx_hdcp2_connect_ctrl;
428 	dev_info(hdcp->dev, "%s success\n", __func__);
429 	return hdcp;
430 
431 error2:
432 	device_remove_file(hdcp->mdev.this_device, &dev_attr_status);
433 error1:
434 	device_remove_file(hdcp->mdev.this_device, &dev_attr_enable);
435 error0:
436 	misc_deregister(&hdcp->mdev);
437 	return NULL;
438 }
439 
rk_hdmirx_hdcp_unregister(struct rk_hdmirx_hdcp * hdcp)440 void rk_hdmirx_hdcp_unregister(struct rk_hdmirx_hdcp *hdcp)
441 {
442 	device_remove_file(hdcp->mdev.this_device, &dev_attr_enable);
443 	device_remove_file(hdcp->mdev.this_device, &dev_attr_status);
444 	device_remove_file(hdcp->mdev.this_device, &dev_attr_support);
445 	device_remove_file(hdcp->mdev.this_device, &dev_attr_test_key1x);
446 	misc_deregister(&hdcp->mdev);
447 }
448