Linux下字符設(she)備驅動
時(shi)間:2018-09-27 來源:未(wei)知
【linux操(cao)作系統驅動(dong)分類】
字符設備: 串口,led,按(an)鍵,一次(ci)只讀取(qu)一個字節數據
塊設備(bei): 一次讀取(qu)多個字(zi)節(512字(zi)節), 硬盤(pan),內存
網絡設備: 一次(ci)讀取多字節數據
本(ben)文引用地址://fsbing.cn/emb/Column/7464.html
ls -l /dev 查看文件屬性(xing)
c開頭的叫字符設備文(wen)件(char), 串口,Led,I2C
b開頭(tou)的叫塊(kuai)設備文件(jian) (block)
sudo mknod /dev/led c 500 0 //生成一(yi)個設備(bei),跟char-read.ko關聯
制作(zuo)一個字(zi)符設備
【設備號】
設(she)備和驅(qu)動(dong)是怎么關聯到(dao)一(yi)起的?
是通(tong)過500 0 關(guan)聯到一(yi)起的
500 主(zhu)設備(bei)號(hao)(linux系統(tong)將一些常用的硬件設備(bei)進(jin)行(xing)了編號(hao),比如串(chuan)口主(zhu)設備(bei)號(hao)4 ...)
0 次設備號(hao)(第(di)一個串口 次設備號(hao)0, 1 2.....)
設備號能(neng)唯一(yi)表示一(yi)個設備,所以不可以重復(fu)
fs4412_led_drv.c 驅動文(wen)件
【如(ru)何實現一個字符設(she)備驅動】
1 因為字符設備驅(qu)動屬于一個內核模(mo)塊,所以(yi)也使用(yong)內核模(mo)塊模(mo)板
2 字符設備驅動(dong)使用(yong)cdev架(jia)構(gou)
【cdev中(zhong)重要的宏】
設備編號:
MKDEV(major, minor);
//功(gong)能:構造設(she)備編號
major 主設備號, minor 此(ci)設備號
執行完此(ci)函數,返回(hui)一個(ge)32位的設備號(hao),其中前12位是主設備號(hao),后20位是次設備號(hao)
500 1
00011111 0100 000000000000000000001
#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
MAJOR(dev_t);
//功能:取主設備號(hao)
從32位的設備號(hao)中取出主設備號(hao)
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
MINOR(dev_t); //取次設備號
從32位的設備號中取出(chu)次(ci)設備號
申請(qing)設備編號:
int register_chrdev_region(dev_t from, unsigned count, const char *name)
功能(neng):將模塊與主次設備(bei)號關聯
參數:
from,起始設備編號
count,次設備號(hao)個數
name,設備(bei)名
返回值:
成功(gong)返回(hui)0,失敗返回(hui)錯誤碼(ma)。
釋(shi)放(fang)設備編號:
void unregister_chrdev_region(dev_t from, unsigned count)
功能(neng):取消(xiao)模(mo)塊與主次設(she)備號關聯
參數:
from,起始設(she)備編號
count,次設備號個(ge)數
驅(qu)動模塊(kuai)和250 0 設備(bei)關聯
int hello_major = 250;
int hello_minor = 0;
int number_of_devices = 1;
static int __init hello_2_init (void)
{
int result;
dev_t dev = 0;
dev = MKDEV (hello_major, hello_minor);
result = register_chrdev_region (dev, number_of_devices, "hello");
if (result<0) {
printk (KERN_WARNING "hello: can't get major number %d\n", hello_major);
return result;
}
printk (KERN_INFO "Registered character driver\n");
return 0;
}
static void __exit hello_2_exit (void)
{
dev_t devno = MKDEV (hello_major, hello_minor);
unregister_chrdev_region (devno, number_of_devices);
printk (KERN_INFO "Char driver cleaned up\n");
}
module_init (hello_2_init);
module_exit (hello_2_exit);
sudo insmod hello.ko
cat /proc/devices | grep hello //linux會將注冊好的設備添(tian)加(jia)進/proc/devices文(wen)件(jian)中
sudo rmmod hello
struct cdev{
const struct file_operations *ops; //字符設備(bei)驅(qu)動結構體
dev_t dev; //主次設備號(hao)
unsigned int count; //設備數量
};
初始化字符設備(bei):
void cdev_init(struct cdev *cdev, const struct file_operations *fops);
添加設備:
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
刪除設備:
void cdev_del(struct cdev *p)
下面的(de)代(dai)碼(ma)是完(wan)成(cheng)一個簡單(dan)的(de)字(zi)符(fu)設備驅動程序(xu),此程序(xu)沒(mei)有實際功能,知識完(wan)成(cheng)了open, close函(han)數
//定義主設(she)備號,次設(she)備號
#include
#include
#include
#include
#include
#include
int hello_major = 250; //主設備號
int hello_minor = 0; //次設備號
struct cdev cdev; //c -> char dev->device 字符(fu)設備(bei)的主要結構體(ti)
static int hello_open (struct inode *inode, struct file *file) //應用成執行open函數會調用到它(ta)
{
printk ("11111111111111111111111111 device opened\n");
return 0;
}
struct file_operations hello_fops = { //將hello_open hello_close 與 open , close 關聯
.owner = THIS_MODULE,
.open = hello_open,
};
static void char_reg_setup_cdev (void) //完(wan)成cdev結構體的初(chu)始化
{
dev_t devno;
devno = MKDEV (hello_major, hello_minor); //MKDEV宏能將主次設備號放(fang)在一個32位(wei)的變(bian)量中(zhong),變(bian)成一個編(bian)號
cdev_init (&cdev, &hello_fops); //將hello_fops 放到cdev結構(gou)體中
cdev.owner = THIS_MODULE;
cdev_add (&cdev, devno , 1); //將(jiang)設備號再放到cdev結構體中, 1代(dai)表 子設備數(shu)量(liang)1個(ge)
}
static int __init hello_init (void) //注冊模塊時(shi)要執行此函數
{
int result;
dev_t devno;
devno = MKDEV (hello_major, hello_minor);
result = register_chrdev_region (devno, 1, "hello");
char_reg_setup_cdev ();
printk ("char device registered\n");
return 0;
}
static void __exit hello_exit (void)
{
dev_t devno = MKDEV (hello_major, hello_minor);
cdev_del (&cdev); //刪(shan)除cdev
unregister_chrdev_region (devno, 1); //刪除250, 0號設備
}
module_init (hello_init);
module_exit (hello_exit);
完成(cheng)一個(ge)應用程序,測試驅動
#include
#include
#include
#include
#include
#include
#include
int main()
{
int fd;
fd = open("/dev/hello", O_RDWR);
if(fd > 0)
{
printf("file open success\n");
}
else
{
printf("file open failed\n");
}
}
制作字符(fu)設備(bei)的過程
1 制作驅動(dong)模(mo)塊
1)將驅動代碼 寫到(dao)hello.c中
2)make (會生成hello.ko文件(jian))
2 sudo insmod hello.ko
3 sudo mknod /dev/hello c 250 0 //生成一個設備,跟char-read.ko關(guan)聯
4 gcc -o test test.c //將應(ying)用層的test.c 編(bian)譯生成test可執(zhi)行(xing)文件
5 sudo ./test
6 dmesg

