iTop4412驱动开发流程记录
- 设备注册
驱动注册的头文件,包含驱动的结构体和注册和卸载的函数
#include <linux/platform_device.h>
struct platform_device
platform_device_register(struct platform_device *)
platform_device_unregister(struct platform_device *) - 驱动注册
驱动注册使用结构体platform_driver头文件
#include <linux/platform_device.h>
struct platform_driver
platform_driver_register(struct platform_device *)
platform_driver_unregister(struct platform_device *) - 注册杂项设备,生成设备节点
直接使用封装好的杂项设备,那么就可以减少一步注册主设备号的过程
生成设备节点和注册设备没有关系,名字可以不一样,不注册设备也能生成设备节点
杂项设备主设备号已经固定
杂项设备注册头文件
#include <linux/miscdevice.h>
struct miscdevice
结构体常用的参数
– .minor设备号
– .name生成设备节点的名称
– .fops指向一个设备节点文件
misc_register(struct miscdevice *);一般在驱动注册里的probe里
misc_deregister(struct miscdevice *);一般在驱动注册里的remove里
struct file_operations *fops结构体头文件
#include <linux/fs.h>
struct file_operations *fops结构体必选的是参数是
– .owner一般是THIS_MODULE,
– .open打开文件函数
– .release关闭文件函数
另外
– .unlocked_ioctl对GPIO的操作,应用向底层驱动传值
- 实际应用
LED驱动 - 在驱动的probe中申请gpio,配置为输出模式,并设定初始化的led的gpio口的值
static int hello_probe(struct platform_device *pdv){
int ret;
printk(KERN_EMERG "\tinitialized\n");
ret = gpio_request(EXYNOS4_GPL2(0),"LEDS");
if(ret < 0){
printk(KERN_EMERG "gpio_request EXYNOS4_GPL2(0) failed!\n");
return ret;
}
s3c_gpio_cfgpin(EXYNOS4_GPL2(0),S3C_GPIO_OUTPUT);
gpio_set_value(EXYNOS4_GPL2(0),0);
misc_register(&hello_dev);
return 0;
}
- 在 file_operations.unlocked_ioctl定义的函数中定义cmd的操作
static long hello_ioctl( struct file *files, unsigned int cmd, unsigned long arg){
printk("cmd is %d,arg is %d\n",cmd,arg);
if(cmd > 1){
printk(KERN_EMERG "cmd is 0 or 1\n");
}
if(arg > 1){
printk(KERN_EMERG "arg is only 1\n");
}
gpio_set_value(EXYNOS4_GPL2(0),cmd);
return 0;
}
- 编写调用函数
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
main(){
int fd;
char *hello_node = "/dev/hello_ctl";
/*O_RDWR只读打开,O_NDELAY非阻塞方式*/
if((fd = open(hello_node,O_RDWR|O_NDELAY))<0){
printf("APP open %s failed",hello_node);
}
else{
printf("APP open %s success",hello_node);
ioctl(fd,1,1);
sleep(3);
ioctl(fd,0,1);
sleep(3);
ioctl(fd,1,1);
}
close(fd);
}
- 字符类设备
-
申请字符类设备号
-静态申请字符类设备号
字符设备函数在文件“include/linux/fs.h”中
register_chrdev_region() //静态申请的函数
宏定义MKDEV的头文件“include/linux/kdev_t.h”
MKDEV提取主次设备号,生成dev_t类型的设备号(高12位为主设备号,低20位为次设备号)
include/linux/cdev.h
– cdev类型是是字符设备描述的结构
– 其中的设备号必须用“dev_t”类型来描述,高12位为主设备号,低20位为
次设备号-动态申请字符类设备号
字符设备函数在文件“include/linux/fs.h”中
alloc_chrdev_region() 是动态分配主次设备号
宏定义MAJOR提取dev_t数据中的主设备号
宏定义MINOR提取dev_t数据中的次设备号 -
注册字符类设备
-
file_operations实现相关操作
-
驱动注销
-
字符设备驱动实现流程图: