brintos

brintos / linux-shallow public Read only

0
0
Text · 2.5 KiB · 63b3055 Raw
90 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * The Virtual DVB test driver serves as a reference DVB driver and helps4 * validate the existing APIs in the media subsystem. It can also aid5 * developers working on userspace applications.6 *7 * Copyright (C) 2020 Daniel W. S. Almeida8 */9#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__10 11#include <linux/printk.h>12#include <linux/ratelimit.h>13#include <linux/string.h>14#include <linux/types.h>15 16#include "vidtv_common.h"17 18/**19 * vidtv_memcpy() - wrapper routine to be used by MPEG-TS20 *	generator, in order to avoid going past the21 *	output buffer.22 * @to:	Starting element to where a MPEG-TS packet will23 *	be copied.24 * @to_offset:	Starting position of the @to buffer to be filled.25 * @to_size:	Size of the @to buffer.26 * @from:	Starting element of the buffer to be copied.27 * @len:	Number of elements to be copy from @from buffer28 *	into @to+ @to_offset buffer.29 *30 * Note:31 *	Real digital TV demod drivers should not have memcpy32 *	wrappers. We use it here because emulating MPEG-TS33 *	generation at kernelspace requires some extra care.34 *35 * Return:36 *	Returns the number of bytes written37 */38u32 vidtv_memcpy(void *to,39		 size_t to_offset,40		 size_t to_size,41		 const void *from,42		 size_t len)43{44	if (unlikely(to_offset + len > to_size)) {45		pr_err_ratelimited("overflow detected, skipping. Try increasing the buffer size. Needed %zu, had %zu\n",46				   to_offset + len,47				   to_size);48		return 0;49	}50 51	memcpy(to + to_offset, from, len);52	return len;53}54 55/**56 * vidtv_memset() - wrapper routine to be used by MPEG-TS57 *	generator, in order to avoid going past the58 *	output buffer.59 * @to:	Starting element to set60 * @to_offset:	Starting position of the @to buffer to be filled.61 * @to_size:	Size of the @to buffer.62 * @c:		The value to set the memory to.63 * @len:	Number of elements to be copy from @from buffer64 *	into @to+ @to_offset buffer.65 *66 * Note:67 *	Real digital TV demod drivers should not have memset68 *	wrappers. We use it here because emulating MPEG-TS69 *	generation at kernelspace requires some extra care.70 *71 * Return:72 *	Returns the number of bytes written73 */74u32 vidtv_memset(void *to,75		 size_t to_offset,76		 size_t to_size,77		 const int c,78		 size_t len)79{80	if (unlikely(to_offset + len > to_size)) {81		pr_err_ratelimited("overflow detected, skipping. Try increasing the buffer size. Needed %zu, had %zu\n",82				   to_offset + len,83				   to_size);84		return 0;85	}86 87	memset(to + to_offset, c, len);88	return len;89}90