This source file includes following definitions.
- drm_hdcp_print_ksv
- drm_hdcp_get_revoked_ksv_count
- drm_hdcp_get_revoked_ksvs
- get_vrl_length
- drm_hdcp_parse_hdcp1_srm
- drm_hdcp_parse_hdcp2_srm
- is_srm_version_hdcp1
- is_srm_version_hdcp2
- drm_hdcp_srm_update
- drm_hdcp_request_srm
- drm_hdcp_check_ksvs_revoked
- drm_setup_hdcp_srm
- drm_teardown_hdcp_srm
- DRM_ENUM_NAME_FN
- drm_hdcp_update_content_protection
1
2
3
4
5
6
7
8
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gfp.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/firmware.h>
15
16 #include <drm/drm_hdcp.h>
17 #include <drm/drm_sysfs.h>
18 #include <drm/drm_print.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_property.h>
21 #include <drm/drm_mode_object.h>
22 #include <drm/drm_connector.h>
23
24 #include "drm_internal.h"
25
26 static struct hdcp_srm {
27 u32 revoked_ksv_cnt;
28 u8 *revoked_ksv_list;
29
30
31 struct mutex mutex;
32 } *srm_data;
33
34 static inline void drm_hdcp_print_ksv(const u8 *ksv)
35 {
36 DRM_DEBUG("\t%#02x, %#02x, %#02x, %#02x, %#02x\n",
37 ksv[0], ksv[1], ksv[2], ksv[3], ksv[4]);
38 }
39
40 static u32 drm_hdcp_get_revoked_ksv_count(const u8 *buf, u32 vrls_length)
41 {
42 u32 parsed_bytes = 0, ksv_count = 0, vrl_ksv_cnt, vrl_sz;
43
44 while (parsed_bytes < vrls_length) {
45 vrl_ksv_cnt = *buf;
46 ksv_count += vrl_ksv_cnt;
47
48 vrl_sz = (vrl_ksv_cnt * DRM_HDCP_KSV_LEN) + 1;
49 buf += vrl_sz;
50 parsed_bytes += vrl_sz;
51 }
52
53
54
55
56
57 if (parsed_bytes != vrls_length)
58 ksv_count = 0;
59
60 return ksv_count;
61 }
62
63 static u32 drm_hdcp_get_revoked_ksvs(const u8 *buf, u8 *revoked_ksv_list,
64 u32 vrls_length)
65 {
66 u32 parsed_bytes = 0, ksv_count = 0;
67 u32 vrl_ksv_cnt, vrl_ksv_sz, vrl_idx = 0;
68
69 do {
70 vrl_ksv_cnt = *buf;
71 vrl_ksv_sz = vrl_ksv_cnt * DRM_HDCP_KSV_LEN;
72
73 buf++;
74
75 DRM_DEBUG("vrl: %d, Revoked KSVs: %d\n", vrl_idx++,
76 vrl_ksv_cnt);
77 memcpy(revoked_ksv_list, buf, vrl_ksv_sz);
78
79 ksv_count += vrl_ksv_cnt;
80 revoked_ksv_list += vrl_ksv_sz;
81 buf += vrl_ksv_sz;
82
83 parsed_bytes += (vrl_ksv_sz + 1);
84 } while (parsed_bytes < vrls_length);
85
86 return ksv_count;
87 }
88
89 static inline u32 get_vrl_length(const u8 *buf)
90 {
91 return drm_hdcp_be24_to_cpu(buf);
92 }
93
94 static int drm_hdcp_parse_hdcp1_srm(const u8 *buf, size_t count)
95 {
96 struct hdcp_srm_header *header;
97 u32 vrl_length, ksv_count;
98
99 if (count < (sizeof(struct hdcp_srm_header) +
100 DRM_HDCP_1_4_VRL_LENGTH_SIZE + DRM_HDCP_1_4_DCP_SIG_SIZE)) {
101 DRM_ERROR("Invalid blob length\n");
102 return -EINVAL;
103 }
104
105 header = (struct hdcp_srm_header *)buf;
106 DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
107 header->srm_id,
108 be16_to_cpu(header->srm_version), header->srm_gen_no);
109
110 WARN_ON(header->reserved);
111
112 buf = buf + sizeof(*header);
113 vrl_length = get_vrl_length(buf);
114 if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
115 vrl_length < (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
116 DRM_HDCP_1_4_DCP_SIG_SIZE)) {
117 DRM_ERROR("Invalid blob length or vrl length\n");
118 return -EINVAL;
119 }
120
121
122 vrl_length -= (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
123 DRM_HDCP_1_4_DCP_SIG_SIZE);
124
125 if (!vrl_length) {
126 DRM_ERROR("No vrl found\n");
127 return -EINVAL;
128 }
129
130 buf += DRM_HDCP_1_4_VRL_LENGTH_SIZE;
131 ksv_count = drm_hdcp_get_revoked_ksv_count(buf, vrl_length);
132 if (!ksv_count) {
133 DRM_DEBUG("Revoked KSV count is 0\n");
134 return count;
135 }
136
137 kfree(srm_data->revoked_ksv_list);
138 srm_data->revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN,
139 GFP_KERNEL);
140 if (!srm_data->revoked_ksv_list) {
141 DRM_ERROR("Out of Memory\n");
142 return -ENOMEM;
143 }
144
145 if (drm_hdcp_get_revoked_ksvs(buf, srm_data->revoked_ksv_list,
146 vrl_length) != ksv_count) {
147 srm_data->revoked_ksv_cnt = 0;
148 kfree(srm_data->revoked_ksv_list);
149 return -EINVAL;
150 }
151
152 srm_data->revoked_ksv_cnt = ksv_count;
153 return count;
154 }
155
156 static int drm_hdcp_parse_hdcp2_srm(const u8 *buf, size_t count)
157 {
158 struct hdcp_srm_header *header;
159 u32 vrl_length, ksv_count, ksv_sz;
160
161 if (count < (sizeof(struct hdcp_srm_header) +
162 DRM_HDCP_2_VRL_LENGTH_SIZE + DRM_HDCP_2_DCP_SIG_SIZE)) {
163 DRM_ERROR("Invalid blob length\n");
164 return -EINVAL;
165 }
166
167 header = (struct hdcp_srm_header *)buf;
168 DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
169 header->srm_id & DRM_HDCP_SRM_ID_MASK,
170 be16_to_cpu(header->srm_version), header->srm_gen_no);
171
172 if (header->reserved)
173 return -EINVAL;
174
175 buf = buf + sizeof(*header);
176 vrl_length = get_vrl_length(buf);
177
178 if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
179 vrl_length < (DRM_HDCP_2_VRL_LENGTH_SIZE +
180 DRM_HDCP_2_DCP_SIG_SIZE)) {
181 DRM_ERROR("Invalid blob length or vrl length\n");
182 return -EINVAL;
183 }
184
185
186 vrl_length -= (DRM_HDCP_2_VRL_LENGTH_SIZE +
187 DRM_HDCP_2_DCP_SIG_SIZE);
188
189 if (!vrl_length) {
190 DRM_ERROR("No vrl found\n");
191 return -EINVAL;
192 }
193
194 buf += DRM_HDCP_2_VRL_LENGTH_SIZE;
195 ksv_count = (*buf << 2) | DRM_HDCP_2_KSV_COUNT_2_LSBITS(*(buf + 1));
196 if (!ksv_count) {
197 DRM_DEBUG("Revoked KSV count is 0\n");
198 return count;
199 }
200
201 kfree(srm_data->revoked_ksv_list);
202 srm_data->revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN,
203 GFP_KERNEL);
204 if (!srm_data->revoked_ksv_list) {
205 DRM_ERROR("Out of Memory\n");
206 return -ENOMEM;
207 }
208
209 ksv_sz = ksv_count * DRM_HDCP_KSV_LEN;
210 buf += DRM_HDCP_2_NO_OF_DEV_PLUS_RESERVED_SZ;
211
212 DRM_DEBUG("Revoked KSVs: %d\n", ksv_count);
213 memcpy(srm_data->revoked_ksv_list, buf, ksv_sz);
214
215 srm_data->revoked_ksv_cnt = ksv_count;
216 return count;
217 }
218
219 static inline bool is_srm_version_hdcp1(const u8 *buf)
220 {
221 return *buf == (u8)(DRM_HDCP_1_4_SRM_ID << 4);
222 }
223
224 static inline bool is_srm_version_hdcp2(const u8 *buf)
225 {
226 return *buf == (u8)(DRM_HDCP_2_SRM_ID << 4 | DRM_HDCP_2_INDICATOR);
227 }
228
229 static void drm_hdcp_srm_update(const u8 *buf, size_t count)
230 {
231 if (count < sizeof(struct hdcp_srm_header))
232 return;
233
234 if (is_srm_version_hdcp1(buf))
235 drm_hdcp_parse_hdcp1_srm(buf, count);
236 else if (is_srm_version_hdcp2(buf))
237 drm_hdcp_parse_hdcp2_srm(buf, count);
238 }
239
240 static void drm_hdcp_request_srm(struct drm_device *drm_dev)
241 {
242 char fw_name[36] = "display_hdcp_srm.bin";
243 const struct firmware *fw;
244
245 int ret;
246
247 ret = request_firmware_direct(&fw, (const char *)fw_name,
248 drm_dev->dev);
249 if (ret < 0)
250 goto exit;
251
252 if (fw->size && fw->data)
253 drm_hdcp_srm_update(fw->data, fw->size);
254
255 exit:
256 release_firmware(fw);
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284 bool drm_hdcp_check_ksvs_revoked(struct drm_device *drm_dev, u8 *ksvs,
285 u32 ksv_count)
286 {
287 u32 rev_ksv_cnt, cnt, i, j;
288 u8 *rev_ksv_list;
289
290 if (!srm_data)
291 return false;
292
293 mutex_lock(&srm_data->mutex);
294 drm_hdcp_request_srm(drm_dev);
295
296 rev_ksv_cnt = srm_data->revoked_ksv_cnt;
297 rev_ksv_list = srm_data->revoked_ksv_list;
298
299
300 if (!rev_ksv_cnt || !rev_ksv_list) {
301 mutex_unlock(&srm_data->mutex);
302 return false;
303 }
304
305 for (cnt = 0; cnt < ksv_count; cnt++) {
306 rev_ksv_list = srm_data->revoked_ksv_list;
307 for (i = 0; i < rev_ksv_cnt; i++) {
308 for (j = 0; j < DRM_HDCP_KSV_LEN; j++)
309 if (ksvs[j] != rev_ksv_list[j]) {
310 break;
311 } else if (j == (DRM_HDCP_KSV_LEN - 1)) {
312 DRM_DEBUG("Revoked KSV is ");
313 drm_hdcp_print_ksv(ksvs);
314 mutex_unlock(&srm_data->mutex);
315 return true;
316 }
317
318 rev_ksv_list += DRM_HDCP_KSV_LEN;
319 }
320
321
322 ksvs += DRM_HDCP_KSV_LEN;
323 }
324 mutex_unlock(&srm_data->mutex);
325 return false;
326 }
327 EXPORT_SYMBOL_GPL(drm_hdcp_check_ksvs_revoked);
328
329 int drm_setup_hdcp_srm(struct class *drm_class)
330 {
331 srm_data = kzalloc(sizeof(*srm_data), GFP_KERNEL);
332 if (!srm_data)
333 return -ENOMEM;
334 mutex_init(&srm_data->mutex);
335
336 return 0;
337 }
338
339 void drm_teardown_hdcp_srm(struct class *drm_class)
340 {
341 if (srm_data) {
342 kfree(srm_data->revoked_ksv_list);
343 kfree(srm_data);
344 }
345 }
346
347 static struct drm_prop_enum_list drm_cp_enum_list[] = {
348 { DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
349 { DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
350 { DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
351 };
352 DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
353
354 static struct drm_prop_enum_list drm_hdcp_content_type_enum_list[] = {
355 { DRM_MODE_HDCP_CONTENT_TYPE0, "HDCP Type0" },
356 { DRM_MODE_HDCP_CONTENT_TYPE1, "HDCP Type1" },
357 };
358 DRM_ENUM_NAME_FN(drm_get_hdcp_content_type_name,
359 drm_hdcp_content_type_enum_list)
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391 int drm_connector_attach_content_protection_property(
392 struct drm_connector *connector, bool hdcp_content_type)
393 {
394 struct drm_device *dev = connector->dev;
395 struct drm_property *prop =
396 dev->mode_config.content_protection_property;
397
398 if (!prop)
399 prop = drm_property_create_enum(dev, 0, "Content Protection",
400 drm_cp_enum_list,
401 ARRAY_SIZE(drm_cp_enum_list));
402 if (!prop)
403 return -ENOMEM;
404
405 drm_object_attach_property(&connector->base, prop,
406 DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
407 dev->mode_config.content_protection_property = prop;
408
409 if (!hdcp_content_type)
410 return 0;
411
412 prop = dev->mode_config.hdcp_content_type_property;
413 if (!prop)
414 prop = drm_property_create_enum(dev, 0, "HDCP Content Type",
415 drm_hdcp_content_type_enum_list,
416 ARRAY_SIZE(
417 drm_hdcp_content_type_enum_list));
418 if (!prop)
419 return -ENOMEM;
420
421 drm_object_attach_property(&connector->base, prop,
422 DRM_MODE_HDCP_CONTENT_TYPE0);
423 dev->mode_config.hdcp_content_type_property = prop;
424
425 return 0;
426 }
427 EXPORT_SYMBOL(drm_connector_attach_content_protection_property);
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443 void drm_hdcp_update_content_protection(struct drm_connector *connector,
444 u64 val)
445 {
446 struct drm_device *dev = connector->dev;
447 struct drm_connector_state *state = connector->state;
448
449 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
450 if (state->content_protection == val)
451 return;
452
453 state->content_protection = val;
454 drm_sysfs_connector_status_event(connector,
455 dev->mode_config.content_protection_property);
456 }
457 EXPORT_SYMBOL(drm_hdcp_update_content_protection);