linux字符设备驱动

来源:互联网 发布:谷歌浏览器 mac版 编辑:程序博客网 时间:2024/04/29 17:53

1、struct inode──字符设备驱动相关的重要结构介绍

内核中用inode结构表示具体的文件,而用file结构表示打开的文件描述符。Linux2.6.27内核中,inode结构体具体定义如下:
struct inode
 {
struct hlist_node    i_hash;
struct list_head    i_list;
struct list_head    i_sb_list;
struct list_head    i_dentry;
unsigned long        i_ino;
atomic_t        i_count;
unsigned int        i_nlink;
uid_t            i_uid;
gid_t            i_gid;
 dev_t            i_rdev;   //该成员表示设备文件的inode结构,它包含了真正的设备编号。
u64            i_version;
loff_t            i_size;
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t        i_size_seqcount;
#endif
struct timespec        i_atime;
struct timespec        i_mtime;
struct timespec        i_ctime;
unsigned int        i_blkbits;
blkcnt_t        i_blocks;
unsigned short          i_bytes;
umode_t            i_mode;
spinlock_t        i_lock;    /* i_blocks, i_bytes, maybe i_size */
struct mutex        i_mutex;
struct rw_semaphore    i_alloc_sem;
const struct inode_operations    *i_op;
const struct file_operations    *i_fop;    /* former ->i_op->default_file_ops */
struct super_block    *i_sb;
struct file_lock    *i_flock;
struct address_space    *i_mapping;
struct address_space    i_data;
#ifdef CONFIG_QUOTA
struct dquot        *i_dquot[MAXQUOTAS];
#endif
struct list_head    i_devices;
union {
struct pipe_inode_info    *i_pipe;
struct block_device    *i_bdev;
 struct cdev        *i_cdev; //该成员表示字符设备的内核的 内部结构。当inode指向一个字符设备文件时,该成员包含了指向struct cdev结构的指针,其中cdev结构是字符设备结构体。
};
int            i_cindex;

__u32            i_generation;

#ifdef CONFIG_DNOTIFY
unsigned long        i_dnotify_mask; /* Directory notify events */
struct dnotify_struct    *i_dnotify; /* for directory notifications */
#endif

#ifdef CONFIG_INOTIFY
struct list_head    inotify_watches; /* watches on this inode */
struct mutex        inotify_mutex;    /* protects the watches list */
#endif

unsigned long        i_state;
unsigned long        dirtied_when;    /* jiffies of first dirtying */

unsigned int        i_flags;

atomic_t        i_writecount;
#ifdef CONFIG_SECURITY
void            *i_security;
#endif
void            *i_private; /* fs or device private pointer */
};

2、struct file ──字符设备驱动相关重要结构

文件结构 代表一个打开的文件描述符,它不是专门给驱动程序使用的,系统中每一个打开的文件在内核中都有一个关联的struct file。它由内核在open时创建,并传递给在文件上操作的任何函数,知道最后关闭。当文件的所有实例都关闭之后,内核释放这个数据结构。
struct file 
{
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union {
struct list_head    fu_list;
struct rcu_head     fu_rcuhead;
} f_u;
struct path        f_path;
#define f_dentry    f_path.dentry   //该成员是对应的 目录结构 。
#define f_vfsmnt    f_path.mnt
const struct file_operations    *f_op;  //该操作 是定义文件关联的操作的。内核在执行open时对这个 指针赋值。 
atomic_long_t        f_count;
 unsigned int         f_flags;  //该成员是文件标志。 
mode_t            f_mode;
loff_t            f_pos;
struct fown_struct    f_owner;
unsigned int        f_uid, f_gid;
struct file_ra_state    f_ra;

u64            f_version;
#ifdef CONFIG_SECURITY
void            *f_security;
#endif
/* needed for tty driver, and maybe others */
void            *private_data;//该成员是系统调用时保存状态信息非常有用的资源。 

#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head    f_ep_links;
spinlock_t        f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space    *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};

-------------------------------------------


加载模块:
1)申请一个设备号,下面注册设备时需要用。
(
静态:
int register_chrdev_region(dev_t from, unsigned count, const char 


*name)
动态:
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, 


const char *name)
)


2)注册设备。
2.1分配一个cdev结构体。
(
方法一:直接定义:
struct cdev test_cdev;


方法二:调用函数:struct cdev* cdev_alloc(void)


)




2.2初始化cdev结构体,使结构体与文件操作结构体(fops)对应起关系。


(
void cdev_init(struct cdev *cdev, const struct file_operations 


*fops)
参数:
cdev:之前我定义的cdev结构体;
fops:设备对应的文件操作结构体。


一般的,还要手工定义结构体成员owner。
struct file_operations test_fops;
cdev_init(&test_cdev, &test_fops);
test_cdev->owner = THIS_OWNER //指定模块的所属


)
2.3添加cdev结构体到内核。
(




将cdev结构体与设备号关联起来:
int cdev_add(struct cdev *cdev, dev_t dev, unsigned count)
参数:
cdev:指定要被添加的cdev结构体;
dev:对应的设备号
count:从设备号dev开始添加count个设备.




)






卸载模块:
1)注销设备,即从内核中删除指定的cdev。
2)注销设备号。

0 0
原创粉丝点击