现在的位置: 首页 > 综合 > 正文

misc device

2018年01月11日 ⁄ 综合 ⁄ 共 1626字 ⁄ 字号 评论关闭

1.mybinder.c

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h> 
#include <linux/ioctl.h> 
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h> 
#include <linux/gpio.h>

static int mybinder_open(struct inode *nodp, struct file *filp)
{
    return 0;
}
static int mybinder_mmap(struct file *filp, struct vm_area_struct *vma)
{
    return 0;
}

static long mybinder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
    return 0;
}

static unsigned int mybinder_poll(struct file *filp,struct poll_table_struct *wait)
{
    return 0;
}
static int mybinder_flush(struct file *filp, fl_owner_t id)
{
    return 0;
}
static int mybinder_release(struct inode *nodp, struct file *filp)
{
    return 0;
}

static const struct file_operations mybinder_fops = {
        .owner          = THIS_MODULE,
        .poll           = mybinder_poll,
        .unlocked_ioctl = mybinder_ioctl,
        .mmap           = mybinder_mmap,
        .open           = mybinder_open,
        .flush          = mybinder_flush,
        .release        = mybinder_release,
};

static struct miscdevice mybinder_miscdev = {
        .minor = MISC_DYNAMIC_MINOR,
        .name = "mybinder",
        .fops = &mybinder_fops
};

static int __init mybinder_init(void)
{
    int ret;

    ret = misc_register(&mybinder_miscdev);
    return ret;
}

device_initcall(mybinder_init);

MODULE_LICENSE("GPL v2");

2.main.c

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

int main(int argc , char *argv[])
{
    int fd;

    fd = open("/dev/mybinder",O_RDWR);
    if(fd < 0)
    {
        printf("open failn");
        exit(1);
    }

    printf("fd = %d\n",fd);

    close(fd);

    return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.