标签:节点 pen span ret sign raw struct 读写 his
demo_read,demo_write 函数完成驱动的读写接口功能,do_write 函数实现将用户写入的数据逆序排列,通过读取函数读取转换后的数据。这里只是演示接口的实现过程和内核驱动对用户的数据的处理。
代码:
#define DEVICE_NAME "demo"
static ssize_t demo_write(struct file *filp,const char * buffer, size_t count)
{
char drv_buf[];
copy_from_user(drv_buf , buffer, count);
…
}
static ssize_t demo_read(struct file *filp,char *buffer,size_t count,loff_t *ppos)
{
char drv_buf[];
copy_to_user(buffer, drv_buf,count);
….
}
static int demo_ioctl(struct inode *inode, struct file *file,unsigned int cmd, unsigned long arg)
{
}
static int demo_open(struct inode *inode, struct file *file)
{
}
static int demo_release(struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT;
DPRINTK("device release\n");
return 0;
}
static struct file_operations demo_fops = {
owner: THIS_MODULE,
write:demo_write,
read: demo_read,
ioctl: demo_ioctl,
open: demo_open,
release:demo_release,
};
#ifdef CONFIG_DEVFS_FS
static devfs_handle_t devfs_demo_dir, devfs_demoraw;
#endif
static int __init demo_init(void)
{
int result;
#ifdef CONFIG_DEVFS_FS
devfs_demo_dir = devfs_mk_dir(NULL, "demo", NULL);
devfs_demoraw = devfs_register(devfs_demo_dir, "0", DEVFS_FL_DEFAULT,
demo_Major, demo_MINOR, S_IFCHR | S_IRUSR | S_IWUSR,&demo_fops, NULL);
#else
SET_MODULE_OWNER(&demo_fops);
result = register_chrdev(demo_Major, "scullc", &demo_fops);
if (result < 0) return result;
if (demo_Major == 0) demo_Major = result; /* dynamic */
#endif
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit demo_exit(void)
{
unregister_chrdev(demo_major, "demo");
kfree(demo_devices);
printk(DEVICE_NAME " unloaded\n");
}
module_init(demo_init);
module_exit(demo_exit);
20145209&20145309信息安全系统设计基础实验报告 (4)
标签:节点 pen span ret sign raw struct 读写 his
原文地址:http://www.cnblogs.com/zym0728/p/6132246.html