copy_to_user
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#define MISC_NAME "test"
#define BOADRID_READ 9999
long misc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
unsigned int __user *argp = (unsigned int __user *)arg;
unsigned int boardid = 90;
int ret =1;
switch(cmd)
{
case BOADRID_READ:
ret = copy_to_user(argp,&boardid, sizeof(unsigned int));
if(ret)
{
printk("ret : %d\n",ret);
printk("IOC_VDEC_GET_VIDEO_LOSS error\n");
return -1;
}
break;
default:
return -1;
}
return 0;
}
int misc_close(struct inode * inode, struct file * file)
{
return 0;
}
static int misc_open(struct inode *inode, struct file *file)
{
printk("misc open\n");
return 0;
}
static const struct file_operations misc_fops =
{
.owner = THIS_MODULE,
.open = misc_open,
.unlocked_ioctl = misc_ioctl,
.release = misc_close
};
static struct miscdevice misc_dev =
{
.minor = MISC_DYNAMIC_MINOR,
.name = MISC_NAME,
.fops = &misc_fops,
};
static int __init misc_init(void)
{
int ret;
ret = misc_register(&misc_dev);
if (ret)
{
printk("misc_register error\n");
return ret;
}
return 0;
}
static void __exit misc_exit(void)
{
misc_deregister(&misc_dev);
}
module_init(misc_init);
module_exit(misc_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("XXX");
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BOADRID_READ 9999
int main(int argc, const char *argv[])
{
int fd;
int ret;
int id;
char * file = "/dev/test";
fd = open(file, O_RDWR);
if (fd < 0)
{
printf("open %s failed!\n", file);
return -1;
}
ret = ioctl(fd,BOADRID_READ,&id);
if(ret < 0){
printf("get board id failed!!!\n");
}else{
printf("get board id sueecssed!!!\n");
}
printf("%d\n",id);
return 0;
}
Makefile
ifneq ($(KERNELRELEASE),)
obj-m:=test.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.symvers *.cmd *.cmd.o
endif