目的:通过采用platform和misc方式编写一个beep驱动。
知识回顾:
1.什么是杂项(MISC)驱动实验呢:misc是杂项的英文,意味着有些驱动是无法分类的,这个时候就可以使用杂项驱动。杂项驱动就是一个字符设备驱动,通常嵌套在platform总线驱动中。
2.当要使用misc驱动的时候,需要向Linux注册一个miscdevice设备,是一个结构体
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;
};
当设置好miscdevice的时候就需要使用misc_register函数向系统中注册一个MISC设备,函数原型int misc_register(struct miscdevice * misc)
驱动编写:
1.构建好基本的platform框架,这里我就把我构建好的放上来,有需要的可以直接拿去使用
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/poll.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
/*probe函数*/
static int miscbeep_probe(struct device *dev)
{
return 0;
}
/*remove函数*/
static int miscbeep_remove(struct device *dev)
{
return 0;
}
/*platform匹配表*/
static const struct of_device_id beep_of_mach[] = {
{
.compatible = "alientek,beep"},
{
/* Sentinel */}
};
/*platform结构体*/
static struct platform_driver miscbeep_driver = {
.driver = {
.name = "alientek,beep",
.of_match_table = beep_of_mach, //设备树匹配
},
.probe = miscbeep_probe,
.remove = miscbeep_remove,
};
/*驱动入口*/
static int __init miscbeep_init(void)
{
return platform_driver_register(&miscbeep_driver);
}
/*驱动出口*/
static void __exit miscbeep_exit(void)
{
platform_driver_unregister(&miscbeep_driver);
}
module_init(miscbeep_init);
module_exit(miscbeep_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("