1f90cf607SDaniel W. S. Almeida // SPDX-License-Identifier: GPL-2.0
2f90cf607SDaniel W. S. Almeida /*
3f90cf607SDaniel W. S. Almeida * The Virtual DVB test driver serves as a reference DVB driver and helps
4f90cf607SDaniel W. S. Almeida * validate the existing APIs in the media subsystem. It can also aid
5f90cf607SDaniel W. S. Almeida * developers working on userspace applications.
6f90cf607SDaniel W. S. Almeida *
7f90cf607SDaniel W. S. Almeida * Copyright (C) 2020 Daniel W. S. Almeida
8f90cf607SDaniel W. S. Almeida */
9f90cf607SDaniel W. S. Almeida #define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
10f90cf607SDaniel W. S. Almeida
11f90cf607SDaniel W. S. Almeida #include <linux/printk.h>
12f90cf607SDaniel W. S. Almeida #include <linux/ratelimit.h>
13f90cf607SDaniel W. S. Almeida #include <linux/string.h>
14f90cf607SDaniel W. S. Almeida #include <linux/types.h>
15f90cf607SDaniel W. S. Almeida
16f90cf607SDaniel W. S. Almeida #include "vidtv_common.h"
17f90cf607SDaniel W. S. Almeida
18f90cf607SDaniel W. S. Almeida /**
19f90cf607SDaniel W. S. Almeida * vidtv_memcpy() - wrapper routine to be used by MPEG-TS
20f90cf607SDaniel W. S. Almeida * generator, in order to avoid going past the
21f90cf607SDaniel W. S. Almeida * output buffer.
22f90cf607SDaniel W. S. Almeida * @to: Starting element to where a MPEG-TS packet will
23f90cf607SDaniel W. S. Almeida * be copied.
24f90cf607SDaniel W. S. Almeida * @to_offset: Starting position of the @to buffer to be filled.
25f90cf607SDaniel W. S. Almeida * @to_size: Size of the @to buffer.
26f90cf607SDaniel W. S. Almeida * @from: Starting element of the buffer to be copied.
27f90cf607SDaniel W. S. Almeida * @len: Number of elements to be copy from @from buffer
28f90cf607SDaniel W. S. Almeida * into @to+ @to_offset buffer.
29f90cf607SDaniel W. S. Almeida *
30f90cf607SDaniel W. S. Almeida * Note:
31f90cf607SDaniel W. S. Almeida * Real digital TV demod drivers should not have memcpy
32f90cf607SDaniel W. S. Almeida * wrappers. We use it here because emulating MPEG-TS
33f90cf607SDaniel W. S. Almeida * generation at kernelspace requires some extra care.
34f90cf607SDaniel W. S. Almeida *
35f90cf607SDaniel W. S. Almeida * Return:
36f90cf607SDaniel W. S. Almeida * Returns the number of bytes written
37f90cf607SDaniel W. S. Almeida */
vidtv_memcpy(void * to,size_t to_offset,size_t to_size,const void * from,size_t len)38f90cf607SDaniel W. S. Almeida u32 vidtv_memcpy(void *to,
39f90cf607SDaniel W. S. Almeida size_t to_offset,
40f90cf607SDaniel W. S. Almeida size_t to_size,
41f90cf607SDaniel W. S. Almeida const void *from,
42f90cf607SDaniel W. S. Almeida size_t len)
43f90cf607SDaniel W. S. Almeida {
44f90cf607SDaniel W. S. Almeida if (unlikely(to_offset + len > to_size)) {
452e2fa2c5SMauro Carvalho Chehab pr_err_ratelimited("overflow detected, skipping. Try increasing the buffer size. Needed %zu, had %zu\n",
46f90cf607SDaniel W. S. Almeida to_offset + len,
47f90cf607SDaniel W. S. Almeida to_size);
48f90cf607SDaniel W. S. Almeida return 0;
49f90cf607SDaniel W. S. Almeida }
50f90cf607SDaniel W. S. Almeida
51f90cf607SDaniel W. S. Almeida memcpy(to + to_offset, from, len);
52f90cf607SDaniel W. S. Almeida return len;
53f90cf607SDaniel W. S. Almeida }
54f90cf607SDaniel W. S. Almeida
55f90cf607SDaniel W. S. Almeida /**
56f90cf607SDaniel W. S. Almeida * vidtv_memset() - wrapper routine to be used by MPEG-TS
57f90cf607SDaniel W. S. Almeida * generator, in order to avoid going past the
58f90cf607SDaniel W. S. Almeida * output buffer.
59f90cf607SDaniel W. S. Almeida * @to: Starting element to set
60f90cf607SDaniel W. S. Almeida * @to_offset: Starting position of the @to buffer to be filled.
61f90cf607SDaniel W. S. Almeida * @to_size: Size of the @to buffer.
62f90cf607SDaniel W. S. Almeida * @c: The value to set the memory to.
63f90cf607SDaniel W. S. Almeida * @len: Number of elements to be copy from @from buffer
64f90cf607SDaniel W. S. Almeida * into @to+ @to_offset buffer.
65f90cf607SDaniel W. S. Almeida *
66f90cf607SDaniel W. S. Almeida * Note:
67f90cf607SDaniel W. S. Almeida * Real digital TV demod drivers should not have memset
68f90cf607SDaniel W. S. Almeida * wrappers. We use it here because emulating MPEG-TS
69f90cf607SDaniel W. S. Almeida * generation at kernelspace requires some extra care.
70f90cf607SDaniel W. S. Almeida *
71f90cf607SDaniel W. S. Almeida * Return:
72f90cf607SDaniel W. S. Almeida * Returns the number of bytes written
73f90cf607SDaniel W. S. Almeida */
vidtv_memset(void * to,size_t to_offset,size_t to_size,const int c,size_t len)74f90cf607SDaniel W. S. Almeida u32 vidtv_memset(void *to,
75f90cf607SDaniel W. S. Almeida size_t to_offset,
76f90cf607SDaniel W. S. Almeida size_t to_size,
77f90cf607SDaniel W. S. Almeida const int c,
78f90cf607SDaniel W. S. Almeida size_t len)
79f90cf607SDaniel W. S. Almeida {
80f90cf607SDaniel W. S. Almeida if (unlikely(to_offset + len > to_size)) {
812e2fa2c5SMauro Carvalho Chehab pr_err_ratelimited("overflow detected, skipping. Try increasing the buffer size. Needed %zu, had %zu\n",
82f90cf607SDaniel W. S. Almeida to_offset + len,
83f90cf607SDaniel W. S. Almeida to_size);
84f90cf607SDaniel W. S. Almeida return 0;
85f90cf607SDaniel W. S. Almeida }
86f90cf607SDaniel W. S. Almeida
87f90cf607SDaniel W. S. Almeida memset(to + to_offset, c, len);
88f90cf607SDaniel W. S. Almeida return len;
89f90cf607SDaniel W. S. Almeida }
90