字符驱动练习

/*

驱动模块代码

*/

#include <linux/init.h>

#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#define SUCCESS 0
#define DEVICE_NAME "charDev"
#define BUF_LEN 80


MODULE_AUTHOR("HWM");
MODULE_LICENSE("GPL");
static int Major;
static int device_Open = 0;
static char msg[BUF_LEN];
static char *msg_ptr = 0;
static int open_count = 0;




static int device_open(struct inode *inode, struct file *file)
{
printk("open the character device\n");
if(device_Open)
{
return -EBUSY;
}
device_Open++;
sprintf(msg, "I already told you %d times hello!", ++open_count);
msg_ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
static int device_release(struct inode *inode, struct file *file)
{
device_Open--;
module_put(THIS_MODULE);
return SUCCESS;
}
static ssize_t device_read(struct file *p_file, __user char *buffer, 
size_t length, loff_t *offset)
{
printk("read the character device\n");
if(0 == msg_ptr)
{
return 0;
}
printk("copy string to user:%s,length:%d\n", msg_ptr, length);
return copy_to_user(buffer, msg_ptr, length);
}
static ssize_t device_write(struct file *p_file, const char *buffer, 
size_t length, loff_t *offset)
{
printk("<1>SORRY, THIS OPERATION ISN'T SUPPORTED");
return -EINVAL;
}




static struct file_operations fops ={
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};


static int __init chrdev_init(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
if(Major < 0)
{
printk("<register this character device failed with %d\n", Major);
return Major;
}
printk("<1> I was assigned major number %d ",Major);
    printk("<1> the drive,create a dev file");
printk("<1> mknod /dev/hello c %d 0.\n",Major);
printk("<1> I was assigned major number %d ",Major);
printk("<1> the device file\n");
printk("<1> Remove the file device and module when done\n");
return 0;
}
static void __exit chrdev_exit(void)
{
unregister_chrdev(Major, DEVICE_NAME);
printk("chardev exit\n");
}
module_init(chrdev_init);

module_exit(chrdev_exit);



/*

用户测试代码

*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>


int main()
{
char buff[4096] = "hello";
int fd = open("/dev/hello", O_RDWR);
int ret = read(fd, buff, sizeof(buff));
printf("%s\n", buff);
return 0;
}

内核printk的输出可以在/var/log/syslog中查看

模块代码copy_to_user的返回值为:成功:0;失败:还有多少个字节为拷贝。

里面有个小问题:当用户需要拷贝的字节数大于可以拷贝的字节数,copy_to_user如何操作的

会自动补上'\0'吗?


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值