//chardev.c
//////////////////////////////////////////////////////////////
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
//////////////////////////////////////////////////////////////
static int char_read(struct file *filp,char __user *buffer,size_t,loff_t *);
static int char_open(struct inode *,struct file *);
static int char_write(struct file *filp,const char __user *buffer,size_t ,loff_t*);
static int char_release(struct inode *,struct file *);
//////////////////////////////////////////////////////////////
//static int device_open_count;
static int major;
//////////////////////////////////////////////////////////////
static const struct file_operations file_ops = {
.read = char_read,
.write = char_write,
.open = char_open,
.release = char_release,
};
static int __init char_init(void)
{
int value;
major = 0;
value = register_chrdev(major, "chardev", &file_ops);
if (value < 0) {
printk("register dev failed!\n");
return value;
}
if (major == 0)
major = value;
return 0;
}
static int char_open(struct inode *inode,struct file *file)
{
// if(device_open_count == 0)
// device_open_count++;
// else{
// printk("设备已经被打开\n");
// return -1;
// }
try_module_get(THIS_MODULE);
return 0;
}
static int char_release(struct inode *inode,struct file *file)
{
// device_open_count--;
module_put(THIS_MODULE);
return 0;
}
static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset)
{
if(length < 0)
return -1;
else if(length > 12)
length = 12;
if(0 == copy_to_user(buffer,"Hello Linux!",length))
return length;
return -1;
}
static int char_write(struct file *filp,const char __user *buffer,size_t length,loff_t *offset)
{
return 0;
}
static void __exit module_close(void)
{
unregister_chrdev(major, "chardev");
}
//////////////////////////////////////////////////////////////
module_init(char_init);
module_exit(module_close);
//////////////////////////////////////////////////////////////
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
华中科技大学操作系统课程设计专用,内含5个部分: 1.1 利用系统调用实现文件拷贝(含代码和makefile) 1.2 多进程编程,实现三个子进程窗口(含代码和makefile) 2 自己的系统调用,只有系统调用实现的代码,系统调用需要自己编译内核 3 字符设备驱动的编写(含代码和makefile) 4 GTK编写系统监视器,可以监测系统很多方面(含代码和makefile) 5 虚拟文件系统(实现的比较简单)(含代码和makefile) 说明:本内容实现自己添加设备驱动,含有代码部分以及调用部分和makefile。
资源推荐
资源详情
资源评论
收起资源包目录
3-chardev-backup.rar (14个子文件)
3-chardev-backup
main.c 743B
chardev.c 2KB
.chardev.ko.cmd 262B
chardev.o 5KB
Module.symvers 0B
a.out 9KB
chardev.mod.c 1KB
.tmp_versions
chardev.mod 73B
chardev.ko 6KB
.chardev.o.cmd 31KB
.chardev.mod.o.cmd 29KB
chardev.mod.o 3KB
modules.order 44B
Makefile 159B
共 14 条
- 1
资源评论
- helloworld_22142015-07-05代码没有问题,就是要自己编译
forest_open
- 粉丝: 38
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功