Chinaunix首页 | 论坛 | 博客
  • 博客访问: 455910
  • 博文数量: 113
  • 博客积分: 446
  • 博客等级: 下士
  • 技术积分: 1229
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-09 16:01
个人简介

Let's go!!!!!

文章分类

全部博文(113)

文章存档

2019年(5)

2018年(4)

2017年(9)

2016年(5)

2015年(39)

2014年(6)

2013年(28)

2012年(17)

分类: LINUX

2013-06-13 13:02:03

驱动程序:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#ifndef MEMDEV_MAJOR
#define MEMDEV_MAJOR 0  
#endif

#ifndef MEMDEV_NR_DEVS
#define MEMDEV_NR_DEVS 2    
#endif

#ifndef MEMDEV_SIZE
#define MEMDEV_SIZE 4096
#endif

struct mem_dev                                    
{                                                        
 char *data;                      
 unsigned long size;      
};

#define MEMDEV_IOC_MAGIC  'k'
#define MEMDEV_IOCPRINT   _IO(MEMDEV_IOC_MAGIC, 1)
#define MEMDEV_IOCGETDATA _IOR(MEMDEV_IOC_MAGIC, 2, int)
#define MEMDEV_IOCSETDATA _IOW(MEMDEV_IOC_MAGIC, 3, int)
#define MEMDEV_IOC_MAXNR 3

static int mem_major = MEMDEV_MAJOR;

module_param(mem_major, int, S_IRUGO);

struct mem_dev *mem_devp;

struct cdev cdev;

int mem_open(struct inode *inode, struct file *filp)
{
   struct mem_dev *dev;
   
   int num = MINOR(inode->i_rdev);

   if (num >= MEMDEV_NR_DEVS)
           return -ENODEV;
   dev = &mem_devp[num];
   
   filp->private_data = dev;
   
   return 0;
}

int mem_release(struct inode *inode, struct file *filp)
{
 return 0;
}

long memdev_ioctl(struct file *filp,unsigned int cmd, unsigned long arg)
{
   int err = 0;
   int ret = 0;
   int ioarg = 0;
 
   if (_IOC_TYPE(cmd) != MEMDEV_IOC_MAGIC)
       return -EINVAL;
   if (_IOC_NR(cmd) > MEMDEV_IOC_MAXNR)
       return -EINVAL;

   if (_IOC_DIR(cmd) & _IOC_READ)
       err = !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd));
   else if (_IOC_DIR(cmd) & _IOC_WRITE)
       err = !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd));
   if (err)
       return -EFAULT;

   switch(cmd) {
     case MEMDEV_IOCPRINT:
      printk("<--- CMD MEMDEV_IOCPRINT Done--->\n\n");
       break;
     case MEMDEV_IOCGETDATA:
       ioarg = 1101;
       ret = __put_user(ioarg, (int *)arg);
       break;
     case MEMDEV_IOCSETDATA:
       ret = __get_user(ioarg, (int *)arg);
       printk("<--- In Kernel MEMDEV_IOCSETDATA ioarg = %d --->\n\n",ioarg);
       break;
     default:  
       return -EINVAL;
   }
   return ret;
}

static const struct file_operations mem_fops =
{
 .owner = THIS_MODULE,
 .open = mem_open,
 .release = mem_release,
 .unlocked_ioctl = memdev_ioctl,
};

static int memdev_init(void)
{
 int result;
 int i;

 dev_t devno = MKDEV(mem_major, 0);

 if (mem_major)
   result = register_chrdev_region(devno, 2, "memdev");
 else  
 {
   result = alloc_chrdev_region(&devno, 0, 2, "memdev");
   mem_major = MAJOR(devno);
 }  
 
 if (result < 0)
   return result;

 cdev_init(&cdev, &mem_fops);
 cdev.owner = THIS_MODULE;
 cdev.ops = &mem_fops;
 
 cdev_add(&cdev, MKDEV(mem_major, 0), MEMDEV_NR_DEVS);
 
 mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL);
 if (!mem_devp)  
 {
   result =  - ENOMEM;
   goto fail_malloc;
 }
 memset(mem_devp, 0, sizeof(struct mem_dev));
 
 for (i=0; i < MEMDEV_NR_DEVS; i++)
 {
       mem_devp[i].size = MEMDEV_SIZE;
       mem_devp[i].data = kmalloc(MEMDEV_SIZE, GFP_KERNEL);
       memset(mem_devp[i].data, 0, MEMDEV_SIZE);
 }
   
 return 0;

fail_malloc:
 unregister_chrdev_region(MKDEV(mem_major, 0), 2);
 return result;
}

static void memdev_exit(void)
{
 cdev_del(&cdev);  
 kfree(mem_devp);    
 unregister_chrdev_region(MKDEV(mem_major, 0), 2);
}

MODULE_AUTHOR("CJ");
MODULE_LICENSE("GPL");

module_init(memdev_init);
module_exit(memdev_exit);


应用程序:
#include
#include
#include
#include
#include
#include

#define MEMDEV_IOC_MAGIC  'k'
#define MEMDEV_IOCPRINT   _IO(MEMDEV_IOC_MAGIC, 1)
#define MEMDEV_IOCGETDATA _IOR(MEMDEV_IOC_MAGIC, 2, int)
#define MEMDEV_IOCSETDATA _IOW(MEMDEV_IOC_MAGIC, 3, int)
#define MEMDEV_IOC_MAXNR 3

int main()
{
int fd = 0;
unsigned int cmd;
unsigned long  arg = 0;
char Buf[4096];

fd = open("/dev/memdev0",O_RDWR);
if (fd < 0)
{
printf("Open Dev Mem0 Error!\n");
return -1;
}

cmd = MEMDEV_IOCPRINT;
if (ioctl(fd, cmd, &arg) < 0)
       {
        printf("Call cmd MEMDEV_IOCPRINT fail\n");
        return -1;
}

cmd = MEMDEV_IOCSETDATA;
arg = 2007;
if (ioctl(fd, cmd, &arg) < 0)
       {
        printf("Call cmd MEMDEV_IOCSETDATA fail\n");
        return -1;
}

cmd = MEMDEV_IOCGETDATA;
if (ioctl(fd, cmd, &arg) < 0)
       {
        printf("Call cmd MEMDEV_IOCGETDATA fail\n");
        return -1;
}
printf("<--- In User Space MEMDEV_IOCGETDATA Get Data is %d --->\n\n",arg);

close(fd);
return 0;
}


Makefile:
LINUX_SRC ?= ~/kernel/linux-3.0
CROSS_COMPILE = arm-linux-

PWD := $(shell pwd)

EXTRA_CFLAGS+=-DARCH_ARM
EXTRA_CFLAGS+=-D__KERNEL__
EXTRA_CFLAGS+=-DMODULE

obj-m += memdev.o

modules:
@make -C $(LINUX_SRC) M=$(PWD) modules
@rm modulesi*  Module* *.o *mod* -rf
clean:
@rm *.ko -rf


运行结果:
/lib/modules/3.0.0 >: ./a.out
<--- CMD MEMDEV_IOCPRINT Done--->

<--- In Kernel MEMDEV_IOCSETDATA ioarg = 2007 --->

<--- In User Space MEMDEV_IOCGETDATA Get Data is 1101 --->




阅读(874) | 评论(0) | 转发(0) |
0

上一篇:内核模块

下一篇:开平方函数

给主人留下些什么吧!~~