copy_to_user()和copy_from_user()实现driver到user和user到driver的数据传送

driveruser之间经常需要数据传输,使用copy_to_user()copy_from_user()来实现driveruseruserdriver的数据传送。

函数原型:

unsigned  long  copy_to_user(void *to,  const void  __user  *from,  usigned long  count);

unsigned  long  copy_from_user(void __user *to,  const void *from,  usigned long  count);

 

代码实例:

hello.c:

#include <linux/init.h>

#include <linux/module.h>

#include <linux/fs.h>

#include <linux/cdev.h>

#include <asm/uaccess.h>

 

MODULE_LICENSE("Dual BSD/GPL");

 

static int count = 10;

static char *init_mesg = "hello,world\n";

static char *exit_mesg = "goodbye\n";

static int major = 252;

static int minor = 0;

dev_t devnum;

int static hello_dev_open(struct inode *inode, struct file *file)

{

         printk("file open in hello_dev_open......finished!\n");

        

         return 0;

}

 

 

int static hello_dev_release(struct inode *inode, struct file *file)

{

         printk("file release in hello_dev_release......finished!\n");

         return 0;

}

 

 

 

ssize_t hello_dev_read(struct file *file, char __user *buf,size_t count, loff_t *offset)

{

  char alpha[27];

  int i, cnt;

  memset(alpha, 0, 27);

  for(i = 0; i < 26; i++)

         alpha[i] = 'a' + i;

 

  if(count > 26)

         cnt = 26;

  else

         cnt = count;

//使用copy_to_user ()函数从driver读数据到user

  if(!copy_to_user((char *)buf, alpha, cnt))

         return cnt;

  else

     return -1;

}

 

 

ssize_t hello_dev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)

{

  char alpha[27];

  int cnt;

  memset(alpha, 0, 27);

  if(count > 26)

         cnt = 26;

  else

       cnt = count;

//使用copy_from_user()函数从user写数据到driver

  if(!copy_from_user((char *)alpha, buf, cnt))

  {  

         printk(alpha);

         printk("\n");

         return cnt;

  }  

  else

         return -1;

}

 

 

static struct cdev hello_dev;

static struct file_operations fops ={

         .owner = THIS_MODULE,

         .open  = hello_dev_open,

         .release = hello_dev_release,

         .read = hello_dev_read,

         .write = hello_dev_write,

};

 

static int __init hello_init(void)

{

  int i;

  int ret;

  for(i = 0; i < count; i++)

         printk(init_mesg);

//ret = register_chrdev_region(MKDEV(major,minor), 1, "hello_dev");

  ret = alloc_chrdev_region(&devnum, 10, 1, "hello_dev");

  if(!ret)

  {

       major = MAJOR(devnum);

         minor = MINOR(devnum);

         printk("major = %d; minor = %d\n", major, minor);

  }

 

  cdev_init(&hello_dev, &fops);

  ret = cdev_add(&hello_dev, devnum, 1);

 

  return ret;

}

 

void hello_exit(void)

{

  printk(exit_mesg);

  cdev_del(&hello_dev);

  unregister_chrdev_region(MKDEV(major, minor),1);

 

  return;

}

 

void hello(void)

{

         printk("good mornig1\n");

}

 

module_param(count, int, S_IRUGO);

module_param(init_mesg, charp, S_IRUGO);

module_param(exit_mesg, charp, S_IRUGO);

 

EXPORT_SYMBOL_GPL(hello);

module_init(hello_init);

module_exit(hello_exit);

 

测试程序:

main.c:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <fcntl.h>

#include <string.h>

#define DEVNAME "/dev/hello"

 

 

int main()

{

  char alpha[27];

  int fd,i;

 

  memset(alpha, 0, 27);

  for(i = 0; i < 26; i++)

         alpha[i] = 'A' + i;

 

  fd = open(DEVNAME, O_RDWR);//在系统内创建DEVNAME文件的时候,需要使用内核注册设备是使用的major和minor设备号,这也就是应用层和内核层的纽带(这个注释是转载时的个人理解,不知道对不对?非原作者的意见。)

  if(fd == -1)

  {

         printf("file %s is opening......failure!", DEVNAME);

  }

  else

  {

         printf("file %s is opening......successfully!\nits fd is %d\n", DEVNAME, fd);

  }

 

  getchar();

  printf("write A-Z to kernel......\n");

  write(fd, alpha, 26);

 

  getchar();

  printf("read datas from kernel.......\n");

  read(fd, alpha, 26);

  printf("%s\n", alpha);

 

  getchar();

  close(fd);

 

  return 0;

}

编译:gcc  main.c  -o main

模块加载并创建结点后(过程参考前面日志),运行./main

结果如下:

file /dev/hello is opening……successfully!

Its fd is 3

(输入回车)

write  A-Z  to  kernel……

(输入回车)

read  datas  from  kernel……

abcdefghijklmnopqrstuvwxyz

(输入回车)

 

 

输入dmesg命令,结果如下:

file open in hello_dev_open……finished!

ABCDEFGHIJKLMNOPQRSTUVWXYZ

file release in hello_dev_open……finished!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值