动态注册字符GPIO驱动框架

模块加载和模块卸载函数

module_init(init);
module_exit(exit);

初始化函数和卸载函数

static int init(void)
{
	int ret = 0,i;
	dev_t num_dev;
	alloc_chrdev_region(&num_dev,minor,num,name);
	major = MAJOR(num_dev);
	myclass = class_create(THIS_MODULE,name);
	my_devices = kmalloc(sizeof(struct reg_dev),GFP_KERNEL);
	memset(my_devices,0,sizeof(struct reg_dev));
	my_devices.data = kmalloc(REGDEV_SIZE,GFP_KERNEL);
	memset(my_devices.data,0,REGDEV_SIZE);
	reg_init_cdev(&my_devices);
	device_create(myclass,NULL,MKDEV(major,minor),NULL,name);
	}
	gpio_init();
	return 0;
}

static void exit(void)
{
	cdev_del(&(my_devices.cdev));
	device_destroy(myclass,MKDEV(major,minor));
	class_destroy(myclass);
	kfree(my_devices);
	gpio_free(gpios);	
	unregister_chrdev_region(MKDEV(major,minor),num);
}

GPIO初始化函数

static int gpio_init(void){
	ret = gpio_request(gpios, "LED");
	s3c_gpio_cfgpin(gpios, S3C_GPIO_OUTPUT);
	gpio_set_value(gpios, 1);			
	return 0;
}

驱动注册函数

static void reg_init_cdev(struct reg_dev *dev){
	int dev;
	dev = MKDEV(numdev_major,numdev_minor);
	cdev_init(&dev->cdev,&fops);
	dev->cdev.owner = THIS_MODULE;
	dev->cdev.ops = &fops;
	cdev_add(&dev->cdev,dev,1);
}

file_operations结构体

struct file_operations fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = io,
};

IO控制函数

static long io(struct file *file){
	gpio_set_value(gpios, 1);
	return 0;
}
发布了6 篇原创文章 · 获赞 2 · 访问量 121

猜你喜欢

转载自blog.csdn.net/qq_44931814/article/details/105497327