Linux字符驱动 misc写法

系统已使用的子设备号可以在 include/linux/miscdevice.h文件中查到。
系统已使用的子设备号也可以在目标板上用过 ls /sys/dev/char命令查看。
MISC设备的主设备号是10,可以在include/uapi/linux/major.h中查到。

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/ide.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/cdev.h>
#include <linux/timer.h>
#include <linux/device.h>
#include <linux/fcntl.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define IOCTL_MAGIC 'x'
#define GENERATE_CMD(\_\_MAGIC\_\_, \_\_MAJOR\_\_, \_\_CMD\_\_) \
        ((0x3<<30) | (\_\_MAJOR\_\_<<16) | (\_\_MAGIC\_\_<<8) | \_\_CMD\_\_)

#define DEVICE_NAME     "misc-hello-drv"
#define DEVICE_COUNT    2 
#define MINOR_HELLO     0               //子设备号起始值

#define SET_CMD         GENERATE_CMD(IOCTL_MAGIC, MAJOR_HELLO, 1)
#define GET_CMD         GENERATE_CMD(IOCTL_MAGIC, MAJOR_HELLO, 2)

struct char_dev {
        dev_t   devid;
        struct cdev cdev;
        struct class *class;
        struct device *device;
        char    name[50];
};

static struct char_dev dev_info[DEVICE_COUNT];
static int hello_open(struct inode *inode_p, struct file *file_p)
{
        int imajor,iminor;
        imajor = MAJOR(inode_p->i_rdev);        //获取主设备号
        iminor = MINOR(inode_p->i_rdev);                //获取子设备号

        file_p->private_data = &dev_info[iminor];
        printk("hello open %d %d\n", imajor, iminor);
        return 0;
}
static int hello_close(struct inode *inode_p, struct file *file_p)
{
        int imajor,iminor;
        imajor = MAJOR(inode_p->i_rdev);        //获取主设备号
        iminor = MINOR(inode_p->i_rdev);                //获取子设备号
        printk("hello close %d %d\n", imajor, iminor);
}

static ssize_t hello_write(struct file *file_p, const char __user *buf, size_t len, loff_t *loff_t_p)
{
        return 0;
}
static long hello_ioctl(struct file *file_p, unsigned int cmd, unsigned long arg)
{
       uint8_t buf[100];
        switch(cmd)
        {
                case SET_CMD:
                        //TODO:
                        if(copy_from_user(buf, (void*)buf, 1) != 0)
                                return -EFAULT;
                        break;
                case GET_CMD:
                        //TODO:
                        if(copy_to_user((void*)arg, buf, 1) != 0)
                                return -EFAULT;
                        break;
                default:
                        break;
        }
        return 0;
}
static struct file_operations dev_fops = {
        .owner = THIS_MODULE,
        .open = hello_open,
        .release = hello_close,
        .write = hello_write,
        .unlocked_ioctl = hello_ioctl,
};

/*MISC device struct*/
static struct miscdevice dev_misc[DEVICE_COUNT];

static int __init hello_init(void)
{
        int i;
        //TODO:在这里初始化硬件

        for(i=0;i<DEVICE_COUNT;i++)
        {
                sprintf(dev_info[i].name, "%s%d", DEVICE_NAME, i);
                dev_misc[i].minor = 11+i;
                dev_misc[i].name = dev_info[i].name;
                dev_misc[i].fops = &dev_fops;
                misc_register(&dev_misc[i]);
        }
        return 0;
}
static void __exit hello_exit(void)
{
        int i;
        for(i=0;i<DEVICE_COUNT;i++)
        {
                misc_deregister(&dev_misc[i]);
        }
}

module_init(hello_init);
module_exit(hello_exit);

/*驱动描述信息*/
MODULE_AUTHOR("Jimbo");
MODULE_ALIAS("hello driver");
MODULE_DESCRIPTION("simple driver template");
MODULE_VERSION("v1.0");
MODULE_LICENSE("GPL");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jimbo_Zhang

有钱的捧个钱场

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值