当前位置: 技术问答>linux和unix
请教大家给程序注释:回答者给分100
来源: 互联网 发布时间:2016-02-25
本文导语: 请详细解释一下,给个注释: /* driver/char/skeleton.c * this is a skeleton char device driver. * Any problem pls contact support@hhcn.com */ #include #include #include #include #include #include #include #include #include #include #...
请详细解释一下,给个注释:
/* driver/char/skeleton.c
* this is a skeleton char device driver.
* Any problem pls contact support@hhcn.com
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IOWRITE 0xf021
#define IOCLEAR 0xf022
#define SKELETON_MAJOR 220
#define STRLEN 100
char skeleton_drvinfo[100];
devfs_handle_t devfs_skeleton;
int skeleton_open(struct inode *, struct file *);
int skeleton_release(struct inode *, struct file *);
int skeleton_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
ssize_t skeleton_read(struct file *, char * , size_t , loff_t *);
ssize_t skeleton_write(struct file *, const char *, size_t , loff_t *);
static struct file_operations skeleton_fops = {
ioctl: skeleton_ioctl,
open: skeleton_open,
read: skeleton_read,
write: skeleton_write,
release: skeleton_release,
};
/*
* Open/close code for raw IO.
*/
int skeleton_open(struct inode *inode, struct file *filp)
{
printk("open okn");
return 0;
}
ssize_t skeleton_read(struct file *filp, char * buf,
size_t size, loff_t *offp)
{
char * _buf;
_buf = skeleton_drvinfo;
copy_to_user(buf,_buf,sizeof(skeleton_drvinfo));
// printk("write: %sn",_buf);
// printk("sizeof(skeleton_drvinfo):%dn",sizeof(skeleton_drvinfo));
return 0;
}
ssize_t skeleton_write(struct file *filp, const char *buf,
size_t size, loff_t *offp)
{
char * _buf;
_buf = skeleton_drvinfo;
copy_from_user(_buf,buf,sizeof(skeleton_drvinfo));
// printk("write: %sn",_buf);
// printk("sizeof(skeleton_drvinfo):%dn",sizeof(skeleton_drvinfo));
return 0;
}
//__ioremap
int skeleton_release(struct inode *inode, struct file *filp)
{
printk("release okn");
return 0;
}
/*
* Deal with ioctls against the raw-device control interface, to bind
* and unbind other raw devices.
*/
int skeleton_ioctl(struct inode *inode,
struct file *flip,
unsigned int command,
unsigned long arg)
{
int err = 0;
switch (command) {
case IOWRITE:
printk("write okn");
return 0;
case IOCLEAR:
printk("clear okn");
return 0;
default:
err = -EINVAL;
}
return err;
}
int __init skeleton_init(void)
{
devfs_skeleton =
devfs_register(NULL,"skeleton",DEVFS_FL_DEFAULT,
SKELETON_MAJOR, 0,
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
&skeleton_fops,NULL);
return 0;
}
void __exit skeleton_exit(void)
{
devfs_unregister(devfs_skeleton);
// return 0;
}
//__initcall(skeleton_init);
module_init(skeleton_init);
module_exit(skeleton_exit);
/* driver/char/skeleton.c
* this is a skeleton char device driver.
* Any problem pls contact support@hhcn.com
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IOWRITE 0xf021
#define IOCLEAR 0xf022
#define SKELETON_MAJOR 220
#define STRLEN 100
char skeleton_drvinfo[100];
devfs_handle_t devfs_skeleton;
int skeleton_open(struct inode *, struct file *);
int skeleton_release(struct inode *, struct file *);
int skeleton_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
ssize_t skeleton_read(struct file *, char * , size_t , loff_t *);
ssize_t skeleton_write(struct file *, const char *, size_t , loff_t *);
static struct file_operations skeleton_fops = {
ioctl: skeleton_ioctl,
open: skeleton_open,
read: skeleton_read,
write: skeleton_write,
release: skeleton_release,
};
/*
* Open/close code for raw IO.
*/
int skeleton_open(struct inode *inode, struct file *filp)
{
printk("open okn");
return 0;
}
ssize_t skeleton_read(struct file *filp, char * buf,
size_t size, loff_t *offp)
{
char * _buf;
_buf = skeleton_drvinfo;
copy_to_user(buf,_buf,sizeof(skeleton_drvinfo));
// printk("write: %sn",_buf);
// printk("sizeof(skeleton_drvinfo):%dn",sizeof(skeleton_drvinfo));
return 0;
}
ssize_t skeleton_write(struct file *filp, const char *buf,
size_t size, loff_t *offp)
{
char * _buf;
_buf = skeleton_drvinfo;
copy_from_user(_buf,buf,sizeof(skeleton_drvinfo));
// printk("write: %sn",_buf);
// printk("sizeof(skeleton_drvinfo):%dn",sizeof(skeleton_drvinfo));
return 0;
}
//__ioremap
int skeleton_release(struct inode *inode, struct file *filp)
{
printk("release okn");
return 0;
}
/*
* Deal with ioctls against the raw-device control interface, to bind
* and unbind other raw devices.
*/
int skeleton_ioctl(struct inode *inode,
struct file *flip,
unsigned int command,
unsigned long arg)
{
int err = 0;
switch (command) {
case IOWRITE:
printk("write okn");
return 0;
case IOCLEAR:
printk("clear okn");
return 0;
default:
err = -EINVAL;
}
return err;
}
int __init skeleton_init(void)
{
devfs_skeleton =
devfs_register(NULL,"skeleton",DEVFS_FL_DEFAULT,
SKELETON_MAJOR, 0,
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
&skeleton_fops,NULL);
return 0;
}
void __exit skeleton_exit(void)
{
devfs_unregister(devfs_skeleton);
// return 0;
}
//__initcall(skeleton_init);
module_init(skeleton_init);
module_exit(skeleton_exit);
|
//请详细解释一下,给个注释:
/* driver/char/skeleton.c
* this is a skeleton char device driver.
* Any problem pls contact support@hhcn.com
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IOWRITE 0xf021 //宏,在IOCTL中使用
#define IOCLEAR 0xf022 //宏,在IOCTL中使用
#define SKELETON_MAJOR 220 //主设备号
#define STRLEN 100 //
char skeleton_drvinfo[100]; //定义了一个数据缓冲,实际上在这个驱动程序里什么也没做
devfs_handle_t devfs_skeleton;
//下面是5个标准对外接口函数原型的声明
//将在file_operations结构中的函数指针一一对应。
int skeleton_open(struct inode *, struct file *);
int skeleton_release(struct inode *, struct file *);
int skeleton_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
ssize_t skeleton_read(struct file *, char * , size_t , loff_t *);
ssize_t skeleton_write(struct file *, const char *, size_t , loff_t *);
//对应上面声明的5个函数(当然不止这些),这个结构在向内核注册设备驱动时需要用到
//当应用程序条用标准的函数ioctl,open...时,实际上是调用了你自己实现的skeletion_ioctl,skeletion_open....
static struct file_operations skeleton_fops = {
ioctl: skeleton_ioctl,
open: skeleton_open,
read: skeleton_read,
write: skeleton_write,
release: skeleton_release,
};
/*
* Open/close code for raw IO.
*/
//打开函数,这里实际上什么也没做,只是输出了一个提示信息,框架嘛,说白了就是什么也没做
int skeleton_open(struct inode *inode, struct file *filp)
{
printk("open okn");
return 0;
}
//读函数。使用了一个copy_to_user,从名字能猜到他的作用。这是一个内核函数,因为在内核态不能用memcpy等...
//在内核编程中,有一套专门用于内核的函数,这是其中一个,与这个函数对应的还有一个叫copy_from_user的函数
ssize_t skeleton_read(struct file *filp, char * buf,
size_t size, loff_t *offp)
{
char * _buf;
_buf = skeleton_drvinfo;
copy_to_user(buf,_buf,sizeof(skeleton_drvinfo));
// printk("write: %sn",_buf);
// printk("sizeof(skeleton_drvinfo):%dn",sizeof(skeleton_drvinfo));
return 0;
}
//同上
ssize_t skeleton_write(struct file *filp, const char *buf,
size_t size, loff_t *offp)
{
char * _buf;
_buf = skeleton_drvinfo;
copy_from_user(_buf,buf,sizeof(skeleton_drvinfo));
// printk("write: %sn",_buf);
// printk("sizeof(skeleton_drvinfo):%dn",sizeof(skeleton_drvinfo));
return 0;
}
//__ioremap
//这个'__ioremap'是谁加在这里的?在这里没有用,与代码也不符
//release,实际上也是什么也没做。实际上它应该做的工作跟skeletion_open正好相反
int skeleton_release(struct inode *inode, struct file *filp)
{
printk("release okn");
return 0;
}
/*
* Deal with ioctls against the raw-device control interface, to bind
* and unbind other raw devices.
*/
//ioctl,这个ioctl也没干什么,但比前面的open和read,write强了一些。
//它根据调用ioctl时传进来的参数switch一下判断是输出write ok还是clear ok,
//起码算是用户和内核有了一点交互,但仅此而已.
int skeleton_ioctl(struct inode *inode,
struct file *flip,
unsigned int command,
unsigned long arg)
{
int err = 0;
switch (command) {
case IOWRITE:
printk("write okn");
return 0;
case IOCLEAR:
printk("clear okn");
return 0;
default:
err = -EINVAL;
}
return err;
}
//init 这个函数将在你执行insmod xxx.o(2.6内核为xxx.ko)时执行
//这里注册了一个名为skeleton的设备,主设备号为SKELETON_MAJOR,从设备号为0,对外接口为skeleton_fops中的5个函数
//其他参数请参考函数的使用手册
//另外字符设备驱动可以参考register_chrdev或者lloc_chrdev_region与register_chrdev_region(2.6)
//还有devfs_mk_cdev也是上手必备函数,最好学会使用
int __init skeleton_init(void)
{
devfs_skeleton =
devfs_register(NULL,"skeleton",DEVFS_FL_DEFAULT,
SKELETON_MAJOR, 0,
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
&skeleton_fops,NULL);
return 0;
}
//这个函数将在你使用rmmod xxx.o(xxx.ko)时调用,过程与init相反
void __exit skeleton_exit(void)
{
devfs_unregister(devfs_skeleton);
// return 0;
}
//__initcall(skeleton_init);
module_init(skeleton_init); //模块初始化,将初始化函数指为skeleton_init
module_exit(skeleton_exit); //退出,同上
/* driver/char/skeleton.c
* this is a skeleton char device driver.
* Any problem pls contact support@hhcn.com
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IOWRITE 0xf021 //宏,在IOCTL中使用
#define IOCLEAR 0xf022 //宏,在IOCTL中使用
#define SKELETON_MAJOR 220 //主设备号
#define STRLEN 100 //
char skeleton_drvinfo[100]; //定义了一个数据缓冲,实际上在这个驱动程序里什么也没做
devfs_handle_t devfs_skeleton;
//下面是5个标准对外接口函数原型的声明
//将在file_operations结构中的函数指针一一对应。
int skeleton_open(struct inode *, struct file *);
int skeleton_release(struct inode *, struct file *);
int skeleton_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
ssize_t skeleton_read(struct file *, char * , size_t , loff_t *);
ssize_t skeleton_write(struct file *, const char *, size_t , loff_t *);
//对应上面声明的5个函数(当然不止这些),这个结构在向内核注册设备驱动时需要用到
//当应用程序条用标准的函数ioctl,open...时,实际上是调用了你自己实现的skeletion_ioctl,skeletion_open....
static struct file_operations skeleton_fops = {
ioctl: skeleton_ioctl,
open: skeleton_open,
read: skeleton_read,
write: skeleton_write,
release: skeleton_release,
};
/*
* Open/close code for raw IO.
*/
//打开函数,这里实际上什么也没做,只是输出了一个提示信息,框架嘛,说白了就是什么也没做
int skeleton_open(struct inode *inode, struct file *filp)
{
printk("open okn");
return 0;
}
//读函数。使用了一个copy_to_user,从名字能猜到他的作用。这是一个内核函数,因为在内核态不能用memcpy等...
//在内核编程中,有一套专门用于内核的函数,这是其中一个,与这个函数对应的还有一个叫copy_from_user的函数
ssize_t skeleton_read(struct file *filp, char * buf,
size_t size, loff_t *offp)
{
char * _buf;
_buf = skeleton_drvinfo;
copy_to_user(buf,_buf,sizeof(skeleton_drvinfo));
// printk("write: %sn",_buf);
// printk("sizeof(skeleton_drvinfo):%dn",sizeof(skeleton_drvinfo));
return 0;
}
//同上
ssize_t skeleton_write(struct file *filp, const char *buf,
size_t size, loff_t *offp)
{
char * _buf;
_buf = skeleton_drvinfo;
copy_from_user(_buf,buf,sizeof(skeleton_drvinfo));
// printk("write: %sn",_buf);
// printk("sizeof(skeleton_drvinfo):%dn",sizeof(skeleton_drvinfo));
return 0;
}
//__ioremap
//这个'__ioremap'是谁加在这里的?在这里没有用,与代码也不符
//release,实际上也是什么也没做。实际上它应该做的工作跟skeletion_open正好相反
int skeleton_release(struct inode *inode, struct file *filp)
{
printk("release okn");
return 0;
}
/*
* Deal with ioctls against the raw-device control interface, to bind
* and unbind other raw devices.
*/
//ioctl,这个ioctl也没干什么,但比前面的open和read,write强了一些。
//它根据调用ioctl时传进来的参数switch一下判断是输出write ok还是clear ok,
//起码算是用户和内核有了一点交互,但仅此而已.
int skeleton_ioctl(struct inode *inode,
struct file *flip,
unsigned int command,
unsigned long arg)
{
int err = 0;
switch (command) {
case IOWRITE:
printk("write okn");
return 0;
case IOCLEAR:
printk("clear okn");
return 0;
default:
err = -EINVAL;
}
return err;
}
//init 这个函数将在你执行insmod xxx.o(2.6内核为xxx.ko)时执行
//这里注册了一个名为skeleton的设备,主设备号为SKELETON_MAJOR,从设备号为0,对外接口为skeleton_fops中的5个函数
//其他参数请参考函数的使用手册
//另外字符设备驱动可以参考register_chrdev或者lloc_chrdev_region与register_chrdev_region(2.6)
//还有devfs_mk_cdev也是上手必备函数,最好学会使用
int __init skeleton_init(void)
{
devfs_skeleton =
devfs_register(NULL,"skeleton",DEVFS_FL_DEFAULT,
SKELETON_MAJOR, 0,
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
&skeleton_fops,NULL);
return 0;
}
//这个函数将在你使用rmmod xxx.o(xxx.ko)时调用,过程与init相反
void __exit skeleton_exit(void)
{
devfs_unregister(devfs_skeleton);
// return 0;
}
//__initcall(skeleton_init);
module_init(skeleton_init); //模块初始化,将初始化函数指为skeleton_init
module_exit(skeleton_exit); //退出,同上
|
这是一个简单的驱动实现,
你若是没学习过驱动开发,
有注释也没用。
你若是没学习过驱动开发,
有注释也没用。
|
这是最简单的一个驱动框架,只要在相应的函数中填入自己的代码就可以了
/* 句保证了在linux启动时会调用skeleton_init函数,可以在这里做一些初始化的工作*/
module_init(skeleton_init);
skeleton_*开头的其它函数是操作系统使用的回调函数,当在用户程序中调用open, read, write, ioctl函数时系统将调用相应的函数。
/* 句保证了在linux启动时会调用skeleton_init函数,可以在这里做一些初始化的工作*/
module_init(skeleton_init);
skeleton_*开头的其它函数是操作系统使用的回调函数,当在用户程序中调用open, read, write, ioctl函数时系统将调用相应的函数。
|
http://soft.yesky.com/lesson/1/2648001.shtml
你不如去这里看看. 很容易入门.
你不如去这里看看. 很容易入门.
|
这个是很简单的驱动程序,可以参考《linux设备驱动程序》这本书的第三章那个字符驱动程序。不是很难的。
|
这是一个字符设备驱动框架,没有实现任何实际的作用,但提供了一个很好的样板。ioctl也有一点与用户的交互。
|
这个简单啊,网上搜一下就得到答案了.
|