Linux驱动开发(十二):MISC杂项设备驱动

简介

杂项设备驱动,就是最简单的字符设备驱动,通常嵌套在platform总线驱动中实现复杂的驱动
主设备号都为10,不同的设备使用不同的从设备号
MISC设备驱动用于解决字符设备驱动不断增加设备号紧张的问题
MISC设备会自动创建cdev,不需要以前那样手动创建
采用MISC设备驱动可以简化字符设备驱动的编写

驱动框架

我们需要向Linux内核注册一个miscdevice设备,miscdevice是一个结构体

miscdevice

定义在\linux\miscdevice.h中,描述了misc设备

struct miscdevice  {
	int minor;
	const char *name;
	const struct file_operations *fops;
	struct list_head list;
	struct device *parent;
	struct device *this_device;
	const struct attribute_group **groups;
	const char *nodename;
	umode_t mode;
};

我们需要设置minor、name、fops这三个成员变量
\linux\miscdevice.h中也预定义了一些子设备号,可以从中指定也可以自己定义

#define PSMOUSE_MINOR		1
#define MS_BUSMOUSE_MINOR	2	/* unused */
#define ATIXL_BUSMOUSE_MINOR	3	/* unused */
/*#define AMIGAMOUSE_MINOR	4	FIXME OBSOLETE */
#define ATARIMOUSE_MINOR	5	/* unused */
#define SUN_MOUSE_MINOR		6	/* unused */
#define APOLLO_MOUSE_MINOR	7	/* unused */
#define PC110PAD_MINOR		9	/* unused */
/*#define ADB_MOUSE_MINOR	10	FIXME OBSOLETE */
#define WATCHDOG_MINOR		130	/* Watchdog timer     */
#define TEMP_MINOR		131	/* Temperature Sensor */
#define RTC_MINOR		135
#define EFI_RTC_MINOR		136	/* EFI Time services */
#define VHCI_MINOR		137
#define SUN_OPENPROM_MINOR	139
#define DMAPI_MINOR		140	/* unused */
#define NVRAM_MINOR		144
#define SGI_MMTIMER		153
#define STORE_QUEUE_MINOR	155	/* unused */
#define I2O_MINOR		166
#define MICROCODE_MINOR		184
#define VFIO_MINOR		196
#define TUN_MINOR		200
#define CUSE_MINOR		203
#define MWAVE_MINOR		219	/* ACP/Mwave Modem */
#define MPT_MINOR		220
#define MPT2SAS_MINOR		221
#define MPT3SAS_MINOR		222
#define UINPUT_MINOR		223
#define MISC_MCELOG_MINOR	227
#define HPET_MINOR		228
#define FUSE_MINOR		229
#define KVM_MINOR		232
#define BTRFS_MINOR		234
#define AUTOFS_MINOR		235
#define MAPPER_CTRL_MINOR	236
#define LOOP_CTRL_MINOR		237
#define VHOST_NET_MINOR		238
#define UHID_MINOR		239
#define MISC_DYNAMIC_MINOR	255

与字符设备的对比

注册

设置好miscdevice结构体后使用misc_register向系统注册一个MISC设备

int misc_register(struct miscdevice *misc);

原先我们需要

alloc_chrdev_region(); /* 申请设备号 */ 
cdev_init(); /* 初始化cdev */ 
cdev_add(); /* 添加cdev */ 
class_create(); /* 创建类 */ 
device_create(); /* 创建设备 */

卸载

当我们卸载驱动的时候调用misc_deregister来注销MISC设备

int misc_deregister(struct miscdevice *misc);

原先我们需要

cdev_del(); /* 删除cdev */ 
unregister_chrdev_region(); /* 注销设备号 */ 
device_destroy(); /* 删除设备 */ 
class_destroy(); /* 删除类 */

实验代码与分析

实验代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

#define MISCBEEP_MINOR 144
#define MISCBEEP_NAME "miscbeep"
#define BEEPOFF 0
#define BEEPON 1

struct miscbeep_dev {
    dev_t devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    struct device_node *nd;
    int beep_gpio;
};

struct miscbeep_dev miscbeep;


static int miscbeep_open(struct inode *inode,struct file *filp)
{
    printk(KERN_EMERG "miscbeep_open enter!\n");
    filp->private_data = &miscbeep;
    return 0;
}

static ssize_t miscbeep_write(struct file *filp,const char __user *buf,size_t cnt,loff_t *offt)
{
    int retvalue;
    unsigned char databuf[2];
    unsigned char beepstat;
    struct miscbeep_dev *dev = (struct miscbeep_dev *)filp->private_data;
    printk(KERN_EMERG "miscbeep_write enter!\n");

    retvalue = copy_from_user(databuf, buf, cnt);
    if(retvalue < 0)
    {
        printk(KERN_EMERG "kernel write failed!\n");
        return -EFAULT;
    }

    beepstat = databuf[0];
    if(beepstat == BEEPON)
    {
        gpio_set_value(dev->beep_gpio, 0);
    }
    else if(beepstat == BEEPOFF)
    {
        gpio_set_value(dev->beep_gpio, 1);
    }
    return 0;
}

static struct file_operations miscbeep_fops = {
    .owner = THIS_MODULE,
    .open = miscbeep_open,
    .write = miscbeep_write,
};

static struct miscdevice beep_miscdev = {
    .minor = MISCBEEP_MINOR,
    .name = MISCBEEP_NAME,
    .fops = &miscbeep_fops,
};


static int miscbeep_probe(struct platform_device *dev)
{
    int ret = 0;

    printk(KERN_EMERG "miscbeep_probe enter!\n");
    /*1.Get device node*/
    miscbeep.nd = of_find_node_by_path("/beep");
    if(miscbeep.nd  == NULL)
    {
        printk(KERN_EMERG "beep node not find!\n");
        return -EINVAL;
    }

    /*2.get gpio property*/
    miscbeep.beep_gpio = of_get_named_gpio(miscbeep.nd, "beep-gpio", 0);
    if(miscbeep.beep_gpio < 0)
    {
        printk(KERN_EMERG "can't get beep-gpio!\n");
        return -EINVAL;     
    }

    /*3.set gpio output,output high default,close beep*/
    ret = gpio_direction_output(miscbeep.beep_gpio, 1);
    if(ret < 0)
    {
        printk(KERN_EMERG "can't set beep-gpio!\n");
    }
    ret = misc_register(&beep_miscdev);
    if(ret < 0)
    {
        printk(KERN_EMERG "misc device register failed!\n");
        return -EINVAL;           
    }

   return 0;
}

static int miscbeep_remove(struct platform_device *dev)
{
    /*close beep*/
    gpio_set_value(miscbeep.beep_gpio, 1);

    /*deregister misc driver*/
    misc_deregister(&beep_miscdev);
    return 0;
}


static const struct of_device_id miscbeep_of_match[] = {
    { .compatible = "atkalpha-beep"},
    { /**/ }
};

static struct platform_driver miscbeep_driver = {
    .driver = {
        .name = "im6ul-beep",
        .of_match_table = miscbeep_of_match,
    },
    .probe = miscbeep_probe,
    .remove = miscbeep_remove,
};

static int __init miscdriver_init(void)
{
    return platform_driver_register(&miscbeep_driver);
}

static void __exit miscdriver_exit(void)
{
    platform_driver_unregister(&miscbeep_driver);
}

module_init(miscdriver_init);
module_exit(miscdriver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("GYY");

代码分析

  • 总体来看我们可以发现,还是platform驱动的结构,需要定义platform_driver ,在init和exit函数中,做的是platform设备的注册和卸载
  • 我们定义了miscbeep_dev 结构体来表示一个我们的设备,这里其实值用到了设备树节点nd和GPIO用于操作蜂鸣器
  • 我们还需要定义一个miscdevice结构体类型的变量代表misc设备
  • 与之前platform驱动的区别之处就在于probe函数和remove函数中所做的事情,在probe函数中,我们只做了GPIO的获取以及初始化,然后调用misc_register来注册MISC设备,在remove函数中我们调用misc_deregister来卸载MISC设备
  • 在open和write函数中的操作就是和普通驱动中是相同的

你可能感兴趣的:(arm+linux开发)