内核态数据与用户态数据互传(2)

cdev_class = class_create(THIS_MODULE,"my_chardev");
    if(IS_ERR(cdev_class))
    {
        printk("Create class fail!\n");
        unregister_chrdev_region(devno,1);
        return -1;
    }
    else
    {
        printk("Create class sucess!\n");
    }

device_create(cdev_class,NULL,devno,0,"my_chardev");
   
    return 0;
}
static void my_cdev_exit(void)
{
    device_destroy(cdev_class,devno);
    class_destroy(cdev_class);
    cdev_del(&cdev);
    unregister_chrdev_region(devno,1);
    printk("my_cdev_exit sucess!\n");
}
module_init(my_cdev_init);
module_exit(my_cdev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("YEFEI");
MODULE_DESCRIPTION("YEFEI Driver");

--------------------------------------------

#ifndef __MY_CDEV_H__
#define __MY_CDEV_H__

#define LED_MAGIC 'L'
#define LED_ON _IOW(LED_MAGIC,0,int)
#define LED_OFF _IOR(LED_MAGIC,1,int *)

#endif

--------------------------------------------

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include "my_cdev.h"

int main(int argc,char *argv[])
{
    int fd;
    int cmd;
    long ret = 0;
    unsigned long param = 0;
    if(argc < 2)
    {
        printf("Please enter secend param!\n");
        return 0;
    }
    cmd = atoi(argv[1]);
    fd = open("/dev/my_chardev",O_RDWR);
    if(fd < 0)
    {
        printf("Open dev/my_chardev fail!\n");
        close(fd);
        return 0;
    }
    switch(cmd)
    {
        case 1:
            param = 500;
            ret = ioctl(fd,LED_ON,&param);
            break;
        case 2:
            ret = ioctl(fd,LED_OFF,&param);
            printf("ret is %d. read data is %d.\n",ret,param);
            break;
        default:
            break;
    }
    close(fd);
    return 0;
}

--------------------------------------------

1 obj-m := my_char_dev.o
2 KDIR := /home/win/dn377org/trunk/bcm7252/linux/
3 all:
4    make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm
5 clean:
6    rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/15469.html