/* * Device driver for the VGA key-switchiing accelerator
 *
 * A Platform device implemented using the misc subsystem

 * Lanxiang Hu
 *
 * References:
 * Linux source: Documentation/driver-model/platform.txt
 *               drivers/misc/arm-charlcd.c
 * http://www.linuxforu.com/tag/linux-device-drivers/
 * http://free-electrons.com/docs/
 *
 * "make" to build
 * insmod key_switching.ko
 *
 * Check code style with
 * checkpatch.pl --file --no-tree key_switching.c
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include "key_switching.h"

#define DRIVER_NAME "key_switching"

/* Device registers */
#define OP_TYPE(x) ((x))
#define WIDTH(x) ((x)+4)
#define LENGTH(x) ((x)+8)
#define ELL(x) ((x)+12)
#define INPUT(x) ((x)+16)
#define OUTPUT(x) ((x)+20)
#define OUTPUT_LENGTH(x) ((x)+24)
#define OUTPUT_WIDTH(x) ((x)+28)
#define OUTPUT_DONE(x) ((x)+32)

/*
 * Information about our device
 */
struct key_switching_dev {
	struct resource res; /* Resource: our registers */
	void __iomem *virtbase; /* Where registers can be accessed in memory */
		key_switching_loading_t load_info;
		key_switching_status_t status_info;
		key_switching_output_t output_info;
} dev;

/*
 * Write segments of a single digit
 * Assumes digit is in range and the device information has been set up
 */
static void write_op_type(key_switching_loading_t load_info)
{
	iowrite8(load_info->input, OP_TYPE(dev.virtbase) );
	dev.load_info = *load_info;
}

static void write_width(key_switching_loading_t load_info)
{
	iowrite8(load_info->input, WIDTH(dev.virtbase) );
	dev.load_info = *load_info;
}

static void write_length(key_switching_loading_t load_info)
{
	iowrite8(load_info->input, LENGTH(dev.virtbase) );
	dev.load_info = *load_info;
}

static void write_ell(key_switching_loading_t load_info)
{
	iowrite8(load_info->input, ELL(dev.virtbase) );
	dev.load_info = *load_info;
}

static void write_input(key_switching_loading_t load_info)
{
	iowrite32(load_info->input, INPUT(dev.virtbase) );
	dev.load_info = *load_info;
}

static void set_reset(key_switching_status_t status_info)
{
	iowrite8(status_info->reset, RESET(dev.virtbase) );
	dev.status_info = *status_info;
}

static void set_all_loaded(key_switching_status_t status_info)
{
	iowrite8(status_info->all_loaded, LOADED(dev.virtbase) );
	dev.status_info = *status_info;
}

static void read_output(key_switching_output_t output_info)
{	
	ioread32(OUTPUT(dev.virtbase) );
	output_info = dev.output_info;
}

static void read_length(key_switching_output_t output_info)
{	
	ioread8(OUTPUT_LENGTH(dev.virtbase) );
	output_info = dev.output_info;
}

static void read_width(key_switching_output_t output_info)
{	
	ioread8(OUTPUT_WIDTH(dev.virtbase) );
	output_info = dev.output_info;
}

static void read_done(key_switching_output_t output_info)
{	
	ioread8(OUTPUT_DONE(dev.virtbase) );
	output_info = dev.output_info;
}

/*
 * Handle ioctl() calls from userspace:
 * Read or write the segments on single digits.
 * Note extensive error checking of arguments
 */
static long key_switching_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
	key_switching_arg_t ksa;

	switch (cmd) {
	case KEY_SWITCHING_WRITE:
		if (copy_from_user(&ksa, (key_switching_arg_t *) arg,
				   sizeof(key_switching_arg_t)))
			return -EACCES;
		write_input(&ksa.load_info);

	case KEY_SWITCHING_WRITE_OP_TYPE:
		if (copy_from_user(&ksa, (key_switching_arg_t *) arg,
				   sizeof(key_switching_arg_t)))
			return -EACCES;
		write_op_type(&ksa.load_info);
		break;
	
	case KEY_SWITCHING_WRITE_WIDTH:
		if (copy_from_user(&ksa, (key_switching_arg_t *) arg,
				   sizeof(key_switching_arg_t)))
			return -EACCES;
		write_width(&ksa.load_info);
		break;
	
	case KEY_SWITCHING_WRITE_LENGTH:
		if (copy_from_user(&ksa, (key_switching_arg_t *) arg,
				   sizeof(key_switching_arg_t)))
			return -EACCES;
		write_length(&ksa.load_info);
		break;
	
	case KEY_SWITCHING_WRITE_ELL:
		if (copy_from_user(&ksa, (key_switching_arg_t *) arg,
				   sizeof(key_switching_arg_t)))
			return -EACCES;
		write_ell(&ksa.load_info);
		break;

	case KEY_SWITCHING_READ:
		if (copy_to_user((key_switching_arg_t *) arg, &ska,
				 sizeof(key_switching_arg_t)))
			return -EACCES;
		read_output(&ksa.output_info);
		break;
	
	case KEY_SWITCHING_READ_WIDTH:
		if (copy_to_user((key_switching_arg_t *) arg, &ska,
				 sizeof(key_switching_arg_t)))
			return -EACCES;
		read_width(&ksa.output_info);
		break;
	
	case KEY_SWITCHING_READ_LENGTH:
		if (copy_to_user((key_switching_arg_t *) arg, &ska,
				 sizeof(key_switching_arg_t)))
			return -EACCES;
		read_length(&ksa.output_info);
		break;
	
	case KEY_SWITCHING_READ_DONE:
		if (copy_to_user((key_switching_arg_t *) arg, &ska,
				 sizeof(key_switching_arg_t)))
			return -EACCES;
		read_done(&ksa.output_info);
		break;

	case KEY_SWITCHING_WRITE_RESET:
	  	if (copy_from_user(&ksa, (key_switching_arg_t *) arg,
				   sizeof(key_switching_arg_t)))
			return -EACCES;
		set_reset(&ksa.status_info);
		break;
	
	case KEY_SWITCHING_WRITE_LOADED:
	  	if (copy_from_user(&ksa, (key_switching_arg_t *) arg,
				   sizeof(key_switching_arg_t)))
			return -EACCES;
		set_all_loaded(&ksa.status_info);
		break;
	
	default:
		return -EINVAL;
	}

	return 0;
}

/* The operations our device knows how to do */
static const struct file_operations key_switching_fops = {
	.owner		= THIS_MODULE,
	.unlocked_ioctl = key_switching_ioctl,
};

/* Information about our device for the "misc" framework -- like a char dev */
static struct miscdevice key_switching_misc_device = {
	.minor		= MISC_DYNAMIC_MINOR,
	.name		= DRIVER_NAME,
	.fops		= &key_switching_fops,
};

/*
 * Initialization code: get resources (registers) and have the accelerator ready
 */
static int __init key_switching_probe(struct platform_device *pdev)
{	
	key_switching_status_t load_reset = {0x1, 0x0, 0x0};
	int ret;

	/* Register ourselves as a misc device: creates /dev/key_switching */
	ret = misc_register(&key_switching_misc_device);

	/* Get the address of our registers from the device tree */
	ret = of_address_to_resource(pdev->dev.of_node, 0, &dev.res);
	if (ret) {
		ret = -ENOENT;
		goto out_deregister;
	}

	/* Make sure we can use these registers */
	if (request_mem_region(dev.res.start, resource_size(&dev.res),
			       DRIVER_NAME) == NULL) {
		ret = -EBUSY;
		goto out_deregister;
	}

	/* Arrange access to our registers */
	dev.virtbase = of_iomap(pdev->dev.of_node, 0);
	if (dev.virtbase == NULL) {
		ret = -ENOMEM;
		goto out_release_mem_region;
	}
        
	/* reset */
    loading_input(&load_reset);

	return 0;

out_release_mem_region:
	release_mem_region(dev.res.start, resource_size(&dev.res));
out_deregister:
	misc_deregister(&key_switching_misc_device);
	return ret;
}

/* Clean-up code: release resources */
static int key_switching_remove(struct platform_device *pdev)
{
	iounmap(dev.virtbase);
	release_mem_region(dev.res.start, resource_size(&dev.res));
	misc_deregister(&key_switching_misc_device);
	return 0;
}

/* Which "compatible" string(s) to search for in the Device Tree */
#ifdef CONFIG_OF
static const struct of_device_id key_switching_of_match[] = {
	{ .compatible = "csee4840-sonny-hu,key_switching_1" },
	{},
};
MODULE_DEVICE_TABLE(of, key_switching_of_match);
#endif

/* Information for registering ourselves as a "platform" driver */
static struct platform_driver key_switching_driver = {
	.driver	= {
		.name	= DRIVER_NAME,
		.owner	= THIS_MODULE,
		.of_match_table = of_match_ptr(key_switching_of_match),
	},
	.remove	= __exit_p(key_switching_remove),
};

/* Called when the module is loaded: set things up */
static int __init key_switching_init(void)
{
	pr_info(DRIVER_NAME ": init\n");
	return platform_driver_probe(&key_switching_driver, key_switching_probe);
}

/* Calball when the module is unloaded: release resources */
static void __exit key_switching_exit(void)
{
	platform_driver_unregister(&key_switching_driver);
	pr_info(DRIVER_NAME ": exit\n");
}

module_init(key_switching_init);
module_exit(key_switching_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lanxiang Hu");
MODULE_DESCRIPTION("key-switchiing accelerator");
