/*
 * Copyright © 2021 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/idr.h>
#include <linux/dma-buf.h>
#include <linux/dma-heap.h>
#include <backport/bp_module_version.h>

#define DRIVER_AUTHOR	"Intel Graphics"
#define DRIVER_DESC	"Intel Graphics"
#define BUILD_DATE	"20210608"

#ifndef CPTCFG_BASE_KERNEL_NAME
#error "You need a CPTCFG_BASE_KERNEL_NAME"
#endif

#ifndef CPTCFG_DII_KERNEL_HEAD
#error "You need a CPTCFG_DII_KERNEL_HEAD"
#endif

#ifndef CPTCFG_BACKPORTS_RELEASE_TAG
#error "You need a CPTCFG_BACKPORTS_RELEASE_TAG"
#endif

extern int sync_debugfs_init(void);

void dependency_symbol(void)
{
}
EXPORT_SYMBOL_GPL(dependency_symbol);

static int __init dmabuf_backport_init(void)
{
	printk(KERN_INFO "DMABUF-COMPAT BACKPORTED INIT\n");
#ifdef CPTCFG_SW_SYNC
	sync_debugfs_init();
#endif
	dma_buf_init();
#if !IS_ENABLED(CPTCFG_DMABUF_HEAPS)
	dma_heap_init();
#endif

	printk(KERN_INFO "Loading dma-buf modules backported from " CPTCFG_DII_KERNEL_TAG "\n");
#ifdef BACKPORTS_GIT_TRACKED
	printk(KERN_INFO BACKPORTS_GIT_TRACKED "\n");
#else
	printk(KERN_INFO "DMA BUF backport generated by backports.git " CPTCFG_BACKPORTS_RELEASE_TAG "\n");
#endif /* BACKPORTS_GIT_TRACKED */

	return 0;
}

static void __exit dmabuf_backport_exit(void)
{
	dma_heap_deinit();
	dma_buf_deinit();
	printk(KERN_INFO "DMABUF-COMPAT BACKPORTED EXIT\n");
}

module_init(dmabuf_backport_init);
module_exit(dmabuf_backport_exit);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_VERSION(BACKPORT_MOD_VER);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL and additional rights");
