閱讀753 返回首頁    go 阿裏雲 go 技術社區[雲棲]


linux驅動開發--字符設備:簡單的file_operations示例

字符設備結構struct cdev

內核使用該結構來表示一個字符設備,在<linux/cdev.h>中定義。
重要成員:

struct kobject kobj;//設備對象
struct module *owner;//該設備的擁有者驅動模塊
struct file_operations *ops;//設備操作集合
struct list_head list;//內核維護的字符設備鏈表成員
dev_t dev;//字符設備號
unsigned int count;//設備個數

文件結構 struct file
代表打開的設備文件,在<linux/fs.h>中定義。
重要成員:
const struct file_operatios *f_ops;//可以在該文件上執行的所有操作的結合
unsigned int flags;//文件被打開時傳遞的標誌
loff_t f_ops;//文件讀寫位置
void *private_data;//文件私有數據
inode結構 struct inode
用於記錄文件物理信息,不同於struct file
一個文件可以對應多個file結構,但是隻有一個inode結構。
在<linux/fs.h>中定義
重要成員:
dev_t i_rdev;//設備對應的設備號
struct cdev *i_cdev;//字符設備結構體

文件操作結構struct file_operations
實際是一個函數指針的集合,這些函數定義了能夠對設備進行的操作
這些指針指向驅動中的函數,每個函數完成一個特別的操作,不支持的操作指針留空
在<linux/fs.h>中定義

初始化cdev
void cdev_init(struct cdev *cdev, const struct file_operations *fops);
cdev:待初始化的cdev對象
fops:可以在設備上執行的操作函數集合
添加cdev
int cdev_add(struct cdev *cdev, dev_t dev, unsigned count);
cdev:待 添加到內核的cdev對象
dev:設備號
count:待添加的設備個數
返回:成功返回0,失敗返回負值
刪除cdev
void cdev_del(struct cdev *cdev);
cdev:待從內核刪除的cdev對象


/**
*Copyright (c) 2013.TianYuan
*All rights reserved.
*
*文件名稱: char_device_driver04.c
*文件標識: 字符設備驅動 :簡單的file_operations示例;完善file_operations結構體中的函數指針
*
*當前版本:1.0
*作者:wuyq 
*
*取代版本:xxx
*原作者:xxx
*完成日期:2013-11-27
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>

MODULE_LICENSE("GPL");
#define CDD_MAJOR	200//cat /proc/devices找一個尚未使用的
#define CDD_MINOR	0
#define CDD_COUNT	1
dev_t dev = 0;
u32 cdd_major = 0;
u32 cdd_minor = 0;
/*定義cdev類型的變量*/
struct cdev cdd_cdev;

int cdd_open(struct inode* inode, struct file *filp)
{
	printk("enter cdd_open!\n");
	return 0;
}

int cdd_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
	printk("enter cdd_read!\n");
	return 0;
}

int cdd_write(struct file *filp, const char __user *buf, size_t count, loff_t *offset)
{
	printk("enter cdd_write!\n");
	return 0;
}

int cdd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data)
{
	printk("enter cdd_ioctl!\n");
	return 0;
}

int cdd_release(struct inode *inode, struct file *filp)
{
	printk("enter cdd_release!\n");
	return 0;
}

struct file_operations cdd_fops = {
	.owner = THIS_MODULE,
	.open = cdd_open,
	.read = cdd_read,
	.write = cdd_write,
	.ioctl = cdd_ioctl,
	.release = cdd_release,
	};

int __init cdd_init(void)
{
	int ret = 0;
	if(cdd_major){
		dev = MKDEV(CDD_MAJOR, CDD_MINOR);//生成設備號
		//注冊設備號;1、要注冊的起始設備號2、連續注冊的設備號個數3、名字
		ret = register_chrdev_region(dev, CDD_COUNT, "cdd_demo");
	}else{
		// 動態分配設備號
		ret = alloc_chrdev_region(&dev, cdd_minor, CDD_COUNT, "cdd_demo02");
	}
	
	if(ret < 0){
		printk("register_chrdev_region failed!\n");
		goto failure_register_chrdev;
	}
	//獲取主設備號
	cdd_major = MAJOR(dev);
	printk("cdd_major = %d\n", cdd_major);

	/*初始化cdev*/
	cdev_init(&cdd_cdev, &cdd_fops);
	/*添加cdev到內核*/
	ret = cdev_add(&cdd_cdev, dev, CDD_COUNT);
	if(ret < 0){
		printk("cdev_add failed!\n");
		goto failure_cdev_add;
	}
	
	return 0;
failure_cdev_add:
	unregister_chrdev_region(dev, CDD_COUNT);
failure_register_chrdev:
	return ret;
}

void __exit cdd_exit(void)
{
/*逆序消除*/
	//從內核中刪除cdev
	cdev_del(&cdd_cdev);
	
	//注銷設備號
	unregister_chrdev_region(dev, CDD_COUNT);
}	

module_init(cdd_init);
module_exit(cdd_exit);

/**
*Copyright (c) 2013.TianYuan
*All rights reserved.
*
*文件名稱: char_device_driver04_test.c
*文件標識: 測試程序
*
*當前版本:1.0
*作者:wuyq 
*
*取代版本:xxx
*原作者:xxx
*完成日期:2013-11-27
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
/*手工創建設備節點文件
mknod /dev/cdd c 248 0
*/
int fd = 0;
char buf[10];

int main()
{
	char ch;
	
	fd = open("/dev/cdd", O_RDWR);
	if(fd < 0){
		printf("open failed!\n");
		return -1;
	}
	printf("open successed fd = %d\n", fd);
	while(1)
	{
		printf("starting to test /dev/cdd...\n");
		ch = getchar();
		getchar();//取走回車
		if(ch == 'q'){
			break;
		}
		switch(ch){
			case 'r':
				read(fd, buf, 0);
				break;
			case 'w':
				write(fd, buf, 0);
				break;
			case 'o':
				ioctl(fd, 0, 0);
				break;
			default:
				break;
		}
		sleep(1);
	}
	
	close(fd);
	return 0;
}


最後更新:2017-04-03 12:54:00

  上一篇:go C++編程規範之38:實施安全的覆蓋
  下一篇:go 在XAMPP v3.2.1中,啟動apache時,彈出“directive &#39;register_globals&#39; is no longer availble in PHP”警告。