Linux字符驱动模板

系统已使用的主设备号可以在include/uapi/linux/major.h中查到。
在目标板上可以通过cat /proc/devices命令查看。

#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 <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     "hello-drv"
#define DEVICE_COUNT    2 
#define MAJOR_HELLO     300
#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);                //获取子设备号

        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,
};
static int __init hello_init(void)
{
        int i;
        //TODO:在这里初始化硬件

        for(i=0;i<DEVICE_COUNT;i++)
        {
                dev_info[i].cdev.owner = THIS_MODULE;
                /*注册设备号*/
                dev_info[i].devid = MKDEV(MAJOR_HELLO, MINOR_HELLO+i);
                sprintf(dev_info[i].name, "%s%d", DEVICE_NAME, i);
                printk("Hello init %d %s\n", dev_info[i].devid, dev_info[i].name );
                register_chrdev_region(dev_info[i].devid, 1, dev_info[i].name);
                /*初始化字符设备结构体*/
                cdev_init(&dev_info[i].cdev, &dev_fops);
                /*注册字符设备*/
                cdev_add(&dev_info[i].cdev, dev_info[i].devid, 1);
                /*创建类*/
                dev_info[i].class = class_create(THIS_MODULE, dev_info[i].name);
                if(IS_ERR(dev_info[i].class))
                {
                        printk("Create class failed! devid=%d\n", dev_info[i].devid);
                        return PTR_ERR(dev_info[i].class);
                }
                /*创建设备节点*/
                dev_info[i].device = device_create(dev_info[i].class, NULL, dev_info[i].devid, NULL, dev_info[i].name);
                if(IS_ERR(dev_info[i].device))
                {
                        printk("Create device node failed! devid=%d\n", dev_info[i].devid);
                        return PTR_ERR(dev_info[i].device);
                }
        }
        return 0;
}
static void __exit hello_exit(void)
{
        int i;
        for(i=0;i<DEVICE_COUNT;i++)
        {
                /*注销字符设备*/
                cdev_del(&dev_info[i].cdev);
                /*注销设备号*/
                unregister_chrdev_region(dev_info[i].devid, 1);
                /*删除设备节点*/
                device_destroy(dev_info[i].class, dev_info[i].devid);
                /*删除类*/
                class_destroy(dev_info[i].class);
        }
}

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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jimbo_Zhang

有钱的捧个钱场

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

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

打赏作者

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

抵扣说明:

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

余额充值