/*
 * livepatch_main.c - kernel live patch main infrastructure
 *
 * Copyright (c) 2014 SUSE
 *  Author: Miroslav Benes <mbenes@suse.cz>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/livepatch.h>
#include <linux/module.h>
#include <linux/types.h>

#include "uname_patch/livepatch_uname.h"

/* Auto expanded KLP_PATCHES_INCLUDES: */
#include "bsc1230998/livepatch_bsc1230998.h"
#include "bsc1231196/livepatch_bsc1231196.h"
#include "bsc1231204/livepatch_bsc1231204.h"
#include "bsc1231993/livepatch_bsc1231993.h"
#include "bsc1233679/livepatch_bsc1233679.h"
#include "bsc1235218/livepatch_bsc1235218.h"
#include "bsc1235452/livepatch_bsc1235452.h"
#include "bsc1235916/livepatch_bsc1235916.h"
#include "bsc1236783/livepatch_bsc1236783.h"


static struct klp_object objs[] = {
	/* Auto expanded KLP_PATCHES_OBJS: */
	{
		.name = NULL,
		.funcs = (struct klp_func[]) {
			{
			  .old_name = __stringify(KLP_SYSCALL_SYM(newuname)),
			  .new_func = KLP_SYSCALL_SYM(klp_newuname),
			},
#ifdef KLP_ARCH_HAS_SYSCALL_COMPAT_STUBS
			{
			  .old_name = __stringify(KLP_SYSCALL_COMPAT_STUB_SYM(newuname)),
			  .new_func = KLP_SYSCALL_COMPAT_STUB_SYM(klp_newuname),
			},
#endif
			{ .old_name = __stringify(tcp_rearm_rto), .new_func = klpp_tcp_rearm_rto, },
			{ .old_name = __stringify(tcp_schedule_loss_probe), .new_func = klpp_tcp_schedule_loss_probe, },
#if IS_ENABLED(CONFIG_HID)
			{ .old_name = __stringify(hid_alloc_report_buf), .new_func = klpp_hid_alloc_report_buf, },
#endif
			{ .old_name = __stringify(inet6_create), .new_func = klpp_inet6_create, },
			{ .old_name = __stringify(mptcp_established_options), .new_func = klpp_mptcp_established_options, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_DRM_AMDGPU)
	{
		.name = "amdgpu",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(rn_notify_wm_ranges), .new_func = klpp_rn_notify_wm_ranges, },
			{ .old_name = __stringify(dal_gpio_service_open), .new_func = klpp_dal_gpio_service_open, },
			{ .old_name = __stringify(dal_gpio_service_lock), .new_func = klpp_dal_gpio_service_lock, },
			{ .old_name = __stringify(dal_gpio_service_close), .new_func = klpp_dal_gpio_service_close, },
			{ .old_name = __stringify(dal_gpio_service_unlock), .new_func = klpp_dal_gpio_service_unlock, },
			{ }
		}
	},
#endif
	{
		.name = "hsr",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(hsr_forward_skb), .new_func = klpp_hsr_forward_skb, },
			{ }
		}
	},
	{
		.name = "sch_netem",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(netem_enqueue), .new_func = klpp_netem_enqueue, },
			{ }
		}
	},
#if IS_ENABLED(CONFIG_USB_VIDEO_CLASS)
	{
		.name = "uvcvideo",
		.funcs = (struct klp_func[]) {
			{ .old_name = __stringify(uvc_parse_format), .new_func = klpp_uvc_parse_format, },
			{ }
		}
	},
#endif
	{ }
};

static struct klp_patch patch = {
	.mod = THIS_MODULE,
	.objs = objs,
	.replace = true,
};

static int __init klp_patch_init(void)
{
	int retval;

	pr_info("livepatch: initializing\n");

	retval = klp_patch_uname_init();
	if (retval)
		return retval;

	/* Auto expanded KLP_PATCHES_INIT_CALLS: */
	retval = livepatch_bsc1230998_init();
	if (retval)
		goto err_bsc1230998;

	retval = livepatch_bsc1231196_init();
	if (retval)
		goto err_bsc1231196;

	retval = livepatch_bsc1231204_init();
	if (retval)
		goto err_bsc1231204;

	retval = livepatch_bsc1231993_init();
	if (retval)
		goto err_bsc1231993;

	retval = livepatch_bsc1233679_init();
	if (retval)
		goto err_bsc1233679;

	retval = livepatch_bsc1235218_init();
	if (retval)
		goto err_bsc1235218;

	retval = livepatch_bsc1235452_init();
	if (retval)
		goto err_bsc1235452;

	retval = livepatch_bsc1235916_init();
	if (retval)
		goto err_bsc1235916;

	retval = livepatch_bsc1236783_init();
	if (retval)
		goto err_bsc1236783;

	retval = klp_enable_patch(&patch);
	if (!retval)
		return retval;

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1236783_cleanup();
err_bsc1236783:
	livepatch_bsc1235916_cleanup();
err_bsc1235916:
	livepatch_bsc1235452_cleanup();
err_bsc1235452:
	livepatch_bsc1235218_cleanup();
err_bsc1235218:
	livepatch_bsc1233679_cleanup();
err_bsc1233679:
	livepatch_bsc1231993_cleanup();
err_bsc1231993:
	livepatch_bsc1231204_cleanup();
err_bsc1231204:
	livepatch_bsc1231196_cleanup();
err_bsc1231196:
	livepatch_bsc1230998_cleanup();
err_bsc1230998:

	return retval;
}

static void __exit klp_patch_cleanup(void)
{
	pr_info("livepatch: removed\n");

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1230998_cleanup();
	livepatch_bsc1231196_cleanup();
	livepatch_bsc1231204_cleanup();
	livepatch_bsc1231993_cleanup();
	livepatch_bsc1233679_cleanup();
	livepatch_bsc1235218_cleanup();
	livepatch_bsc1235452_cleanup();
	livepatch_bsc1235916_cleanup();
	livepatch_bsc1236783_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "8f290ccdd1b00390914ddada503ec8c3fe071f82");
