第一個相對完整的驅動實踐編寫
需求:
1.使用雜項裝置完成一個蜂鳴器的驅動。
2.完成一個上層測試應用。
應用要求:在上層應用中傳入引數1為開啟蜂鳴器,傳入引數0為 關閉蜂鳴器。
buzzer.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#define GPIO5_DR 0x020AC000 //蜂鳴器實體地址
unsigned int *vir_gpio5_dr; //蜂鳴器虛擬地址
int misc_open(struct inode *inode, struct file *file)
{
printk("hello misc_open\n");
return 0;
}
int misc_release(struct inode *inode, struct file *file)
{
printk("hello mise_release bye bye\n");
return 0;
}
ssize_t misc_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff_t)
{
char kbuf[64] = "heheh";
if (copy_to_user(ubuf, kbuf, strlen(kbuf) + 1) != 0)
{
printk("copy_to_user error\n");
return -1;
}
printk("hello misc_read bye bye\n");
return 0;
}
ssize_t misc_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff_t)
{
char kbuf[64] = {0};
if (copy_from_user(kbuf, ubuf, size) != 0)
{
printk("copy_from_user error\n");
return -1;
}
printk("hello misc_write bye bye\n");
if(kbuf[0] == 1)
{
*vir_gpio5_dr |= 0x02;
}else
{
*vir_gpio5_dr &= ~0x02;
}
return 0;
}
/* 檔案操作集 */
struct file_operations misc_fops = {
.owner = THIS_MODULE, // 當前模組
.open = misc_open,
.release = misc_release,
.write = misc_write,
.read = misc_read,
};
/* 雜項裝置結構體 */
struct miscdevice misc_dev = {
.minor = MISC_DYNAMIC_MINOR, // 動態分配次裝置號
.name = "hello_mise", // 裝置節點的名字
.fops = &misc_fops // 檔案操作集
};
static int mise_init(void)
{
int ret;
ret = misc_register(&misc_dev); // 註冊雜項裝置
if (ret < 0)
{
printk("misc register is error!\n");
return -1;
}
printk("mise register is ok!\n");
vir_gpio5_dr = ioremap(GPIO5_DR, 4); //將實體地址轉化為虛擬地址
if(vir_gpio5_dr == NULL)
{
printk("GPIO5_DR is error!\n");
return -EBUSY;
}
printk("GPIO5_DR is ok!\n");
return 0;
}
static void mise_exit(void)
{
misc_deregister(&misc_dev); // 登出雜項裝置
printk("misc gooodbye!\n");
iounmap(vir_gpio5_dr); //解除安裝雜項裝置
}
module_init(mise_init);
module_exit(mise_exit);
MODULE_LICENSE("GPL");
app.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int fd;
char buf[64] = {0};
fd = open("/dev/hello_mise", O_RDWR);
if (fd < 0)
{
perror("open error\n");
return fd;
}
buf[0] = atoi(argv[1]);
write(fd, buf, strlen(buf)+1);
close(fd);
return 0;
}
Makefile
obj-m+=buzzer.o
KDIR:=/home/mzx/imx6ull/linux-imx-rel_imx_4.1.15_2.1.0_ga
PWD?=$(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules