Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360447
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: Android平台

2014-02-24 23:11:06

mem___test.c

点击(此处)折叠或打开

  1. #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include


    #include "memdev.h"




    struct mem_dev *mem_devp; /*设备结构体指针*/


    struct cdev cdev;


    /*文件打开函数*/
    int mem_open(struct inode *inode, struct file *filp)
    {
    printk(KERN_ERR "here into open\n");
            return 0;
    }


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


    /*IO操作*/
    int memtest_ioctl(struct inode *inode, unsigned int cmd, unsigned long arg)
    {


            int err = 0;
            int ret = 0;
            int ioarg = 0;


            switch(cmd)
            {


            case MEMDEV_IOCPRINT:
                    printk("<--- CMD memtest_IOCPRINT Done--->\n\n");
                    break;




            case MEMDEV_IOCSETDATA:
                    ret = __get_user(ioarg, (int *)arg);
                    printk("<--- In Kernel memtest_IOCSETDATA ioarg = %d --->\n\n",ioarg);
                    break;


    case MEMDEV_IOTEST:
    printk(KERN_ERR "into ioctl kernel data test\n");
    struct test_data *test = (struct test_data *)arg;
    if(test == NULL || test->data == NULL || test->change == NULL)
    {
    return 0;
    printk(KERN_ERR "test is null");
    }


    struct st m_st;
    int a = 200;
    printk("%s \t %s\n",test->data,test->change);
    printk("current data len %d",test->data_len);
    char *ddd = "hello world!!!!!!!!!!!";
    memcpy(m_st.st_data,ddd,strlen(ddd));
    m_st.st_len = 50;
    copy_to_user(test->data,ddd,strlen(ddd));
    copy_to_user(test->change,ddd,strlen(ddd));
    copy_to_user(test->st,&m_st,sizeof(struct st));
    copy_to_user(&(test->data_len),&a,sizeof(int));
    break;
            default:
                    return -EINVAL;
            }
            return ret;
    }


    /*文件操作结构体*/
    static const struct file_operations mem_fops =
    {
            .owner = THIS_MODULE,
            .open = mem_open,
            .release = mem_release,
            .unlocked_ioctl = memtest_ioctl,
    };


    static struct class *my_class;
    struct device *dev;
    static int major;
    static int memtest_init(void)
    {
    major = register_chrdev(0, "mem_test",&mem_fops);
    my_class = class_create(THIS_MODULE, "mem_test");
    device_create(my_class, NULL, MKDEV(major, 4), NULL, "%s", "mem_test");
    return 0;
    }


    static void memtest_exit(void)
    {
    device_destroy(my_class,MKDEV(major,4));
    class_destroy(my_class);
    unregister_chrdev(major, "mem_test");
    return;
    }


    MODULE_AUTHOR("David Xie");
    MODULE_LICENSE("GPL");


    module_init(memtest_init);
    module_exit(memtest_exit);

app.c

点击(此处)折叠或打开

  1. #include
    #include
    #include
    #include
    #include
    #include


    #include "memdev.h"  /* 包含命令定义 */


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




            /*打开设备文件*/
            fd = open("/dev/mem_test",O_RDWR);
            if (fd < 0)
            {
                    printf("Open Dev Mem0 Error!\n");
                    return -1;
            }


    struct test_data test;


    const char a_data[1024] = {'z','z','z','z','z','z','z','z',0};
    const char p_data[1024] = {'z','z','z','z','z','z','z','z',0};
    const char *hhh = a_data;
    test.data = p_data;
    test.change= hhh;
    test.st = malloc(1024);
    test.data_len = 100;


    printf("before ioctl test data\n");
    printf("before %s\n",test.change);
    printf("before %s\n",test.data);
    printf("before int %d",test.data_len);
    int ret = ioctl(fd,MEMDEV_IOTEST,&test);
    printf("ioctl ret %d\n",ret);
    printf("after ioctl test data\n");
    printf("after %s\n",test.change);
    printf("after %s\n",test.data);
    printf("after int %d\n",test.data_len);
    printf("after %s\n",((struct st *)(test.st))->st_data);
    printf("after %d\n",((struct st *)(test.st))->st_len);




            close(fd);
            return 0;
    }

memdev.h

点击(此处)折叠或打开

  1. #ifndef _MEMDEV_H_
    #define _MEMDEV_H_


    #include


    #ifndef MEMDEV_MAJOR
    #define MEMDEV_MAJOR 0   /*预设的mem的主设备号*/
    #endif


    #ifndef MEMDEV_NR_DEVS
    #define MEMDEV_NR_DEVS 2    /*设备数*/
    #endif


    #ifndef MEMDEV_SIZE
    #define MEMDEV_SIZE 4096
    #endif
    /*mem设备描述结构体*/
    struct mem_dev                                     
    {                                                        
      char *data;                      
      unsigned long size;       
    };




    struct st
    {
    char st_data[100];
    int st_len;
    };




    struct test_data
    {
    void *st;
    void *data;
    int data_len;
    char *change;
    };


    /* 定义幻数 */
    #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_IOTEST _IOWR(MEMDEV_IOC_MAGIC, 4, int)


    #endif




makefile

点击(此处)折叠或打开

  1. obj-m := mem___test.o

  2. KDIR := /lib/modules/$(shell uname -r)/build

  3. PWD := $(shell pwd)

  4. default:
  5.     $(MAKE) -C $(KDIR) M=$(PWD) modules

  6. clean:
  7.     rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions *.order *symvers *Module.markers

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

上一篇:虚拟地址转换成物理地址 MMU寻址

下一篇:没有了

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