首页 > 代码库 > misc设备驱动模板-linux

misc设备驱动模板-linux


               闲来无事,丢个misc驱动模板就跑 技术分享


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/delay.h>

#include <linux/gpio.h>
#include <mach/gpio.h>
#include <plat/gpio-cfg.h>


#define DEVICE_NAME "misc_dev0"


static long misc_dev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	// undefined
	return 0;
}

static struct file_operations misc_dev_fops = {
	.owner			= THIS_MODULE,
	.unlocked_ioctl	= misc_dev_ioctl,
}

static struct miscdevice misc_dev_dev = {
	.minor			= MISC_DYNAMIC_MINOR,
	.name			= DEVICE_NAME,
	.fops			= &misc_dev_fops,
}

static int __init misc_dev_init(void) {
	int ret = -1;
	/* power of eth wifi mic... */
	ret = gpio_request(EXYNOS5260_GPX0(1), "Peripheral Power Ctrl");
	if(ret)
	{
		printk("%s: request GPIO %d failed, ret = %d\n", DEVICE_NAME, gpio_request(EXYNOS5260_GPX0(1), ret));
		return ret;		
	}
	s3c_gpio_cfgpin(EXYNOS5260_GPX0(1), S3C_GPIO_OUTPUT);
	gpio_set_value(EXYNOS5260_GPX0(1), 1);

	misc_register(&misc_dev_dev);

	printk(DEVICE_NAME"\tinitialized\n");

	return ret;
}

static void __exit misc_dev_exit(void) {
	int i;

	misc_deregister(&misc_dev_dev);
}

module_init(misc_dev_init);
module_exit(misc_dev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Morphy Inc.");


misc设备驱动模板-linux