读书笔记整理1-----向内核添加源代码

c10---向内核添加源代码


一。浏览源代码
1.系统文件相关
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;
atomic_long_t f_count;
unsigned int f_flags;
fmode_t f_mode;
loff_t f_pos;
struct fown_struct f_owner;
const struct cred *f_cred;
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
};


struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
};




const struct file_operations random_fops = {
.read  = random_read,
.write = random_write,
.poll  = random_poll,
.unlocked_ioctl = random_ioctl,
.fasync = random_fasync,
};


const struct file_operations urandom_fops = {
.read  = urandom_read,
.write = random_write,
.unlocked_ioctl = random_ioctl,
.fasync = random_fasync,
};


struct inode_operations {
int (*create) (struct inode *,struct dentry *,int, struct nameidata *);
struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameidata *);
int (*link) (struct dentry *,struct inode *,struct dentry *);
int (*unlink) (struct inode *,struct dentry *);
int (*symlink) (struct inode *,struct dentry *,const char *);
int (*mkdir) (struct inode *,struct dentry *,int);
int (*rmdir) (struct inode *,struct dentry *);
int (*mknod) (struct inode *,struct dentry *,int,dev_t);
int (*rename) (struct inode *, struct dentry *,
struct inode *, struct dentry *);
int (*readlink) (struct dentry *, char __user *,int);
void * (*follow_link) (struct dentry *, struct nameidata *);
void (*put_link) (struct dentry *, struct nameidata *, void *);
void (*truncate) (struct inode *);
int (*permission) (struct inode *, int);
int (*setattr) (struct dentry *, struct iattr *);
int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
ssize_t (*listxattr) (struct dentry *, char *, size_t);
int (*removexattr) (struct dentry *, const char *);
void (*truncate_range)(struct inode *, loff_t, loff_t);
long (*fallocate)(struct inode *inode, int mode, loff_t offset,
 loff_t len);
int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
     u64 len);
};


2.用户空间和内核空间


copy_to_user()
copy_from_user()


3.等待队列(声明一个等待队列,等待某种条件的驱动程序延迟到条件满足时在进行处理)
使用两个数据结构---等待队列和等待队列头


4.工作队列和中断
模块初始化时request_irq()请求中断号,同时注册中断处理函数,这里注册的是上半部分中断处理,只完成
严格时限的工作(接收中断,从中断处理程序的上半部分传递给下半部分),余下的部分以 任务 的
形式插入工作队列,以便等待处理。
工作队列在模块初始化时要注册。DECLARE_WROK()或INIT_WORK(),在进程上下文中执行,


tasklets和工作队列相似,但需完全在中断上下文中运行。DECLARE_TASKLET(),tasklet_schedule()
不同的tasklet通信,需要用自旋锁。


4.设备模型和sysfs系统文件
文件系统跟踪现有设备,并分类:块设备、输入设备和总线设备。也跟踪系统已有的驱动程序以及他和设备如何关联。


sysfs涉及kobject和kset。
driver_register()
driver_unregister()




二。编写源代码(编写驱动时常见问题)


1.设备基础
int register_chrdev(unsigned int major,const char* name,struct file_operations *fops)


2.符号输出----内核符号表
EXPORT_SYMBOL


3.ioctl
eg{
#define MYDRIVER_IOC_MAGIC 'g'
#define MYDRIVER_IOC_OP1 _IO(MYDRIVER_IOC_MAGIC,0)
#define MYDRIVER_IOC_OP2 _IOW(MYDRIVER_IOC_MAGIC,1)
#define MYDRIVER_IOC_OP3 _IOW(MYDRIVER_IOC_MAGIC,2)
#define MYDRIVER_IOC_OP4 _IORW(MYDRIVER_IOC_MAGIC,3)
}


这样来调用,从用户空间
ioctl(fd,MYDRIVER_IOC_OP1,NULL);
ioctl(fd,MYDRIVER_IOC_OP2,&mydata);


get_user() put_user()


4.轮询和中断-------spinlock,自旋锁,保护内核临界区代码


eg:
irq--------     irq_handler(){
spin_lock_irqsave(&driver_lock,flags);
......
spin_unlock_irqrestore(&driver_lock,flags);}
ioctl------     ioctl_func(){
spin_lock_irq(&driver_lock);
......
spin_unlock_irq(&driver_lock);}

5.工作队列和tasklet
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值