/* (C) 2006 Daniel De Graaf
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Derived in part from drivers/ide/ide.c
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/ide.h>

MODULE_AUTHOR("Daniel De Graaf <danieldegraaf@gmail.com>");
MODULE_DESCRIPTION("IDE register and unregister through /proc");
MODULE_LICENSE("GPL");

static inline void scan_hw(void) {
	hw_regs_t hw;

	memset(&hw, 0, sizeof(hw));
	ide_init_hwif_ports(&hw, 0x170, 0x376, NULL);
	hw.irq = 15;
	ide_register_hw(&hw, NULL);
}

static inline void unreg_hw(void) {
    ide_unregister(1);
}

static int proc_write(
		struct file *file,
		const char __user *input,
		unsigned long size,
		void* data
) {
	char buf[4];
	if (size > 1)
		size = 1;

	if (copy_from_user(buf, input, size))
		return -EFAULT;

	switch (*buf) {
	case '1':
		scan_hw();
		break;
	case '0':
		unreg_hw();
		break;
	}

	return size;
}

static int __init ide_regproc_init(void) {
	struct proc_dir_entry	*proc;

	proc = create_proc_entry("ide1_register", 0200, NULL);
	if (proc == NULL)
		return -ENOMEM;
	proc->write_proc = &proc_write;

	return 0;
}

static void __exit ide_regproc_exit(void) {
	remove_proc_entry("ide1_register", NULL);
}

module_init(ide_regproc_init);
module_exit(ide_regproc_exit);
