知识储备
字符设备驱动框架
相关API
字符设备驱动分步实现流程API
1.字符设备驱动结构体
struct cdev {
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
2.分配字符设备对象
(1)struct cdev cdev;
(2) struct cdev *cdev;
struct cdev *cdev_alloc(void)
功能:给字符设备驱动对象申请一片空间
参数:无
返回值:
成功返回空间首地址,失败返回NULL
cdev=cdev_alloc()
3.初始化设备驱动对象
void cdev_init(struct cdev *cdev, const struct file_operations *fops);
功能:完成cdev对象的初始化(部分初始化)
参数:
cdev:驱动对象
fops:操作方法结构体指针
返回值:无
4.指定设备号
int register_chrdev_region(dev_t from, unsigned count, const char *name)
功能:静态指定设备号
参数:
from:指定的设备号(主设备号|次设备号)
count:设备的个数
name:设备驱动的名字
返回值:成功返回0,失败返回错误码
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
const char *name)
功能:动态申请设备号
参数:
dev:设备号变量地址
baseminor:次设备号的开始值
count:次设备的数量 3 -》 0 1 2
name:设备驱动的名字
5.注册字符设备驱动
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
功能:注册字符设备驱动
p:设备驱动对象首地址
dev:设备号
count:设备数量
返回值:成功返回0,失败返回错误码
------------------------------------------------
注销字符设备驱动
1.
void cdev_del(struct cdev *p)
功能:注销字符设备驱动
参数:设备驱动对象首地址
返回值:无
2.
void unregister_chrdev_region(dev_t from, unsigned count)
功能:释放设备号
参数:
from:设备号
count:设备数量
返回值:无
3.
#include<linux/slab.h>
void kfree(void *p)
功能:释放动态申请的驱动对象空间
参数:对象首地址
返回值:无
代码实现
myled.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include "myled.h"
#define CNAME "myled"
char kbuf[128] = {};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
struct semaphore sem;
int flags = 0;
int mycdev_open(struct inode *inode, struct file *file)
{
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if (size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if (size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("数据从用户向内核拷贝失败\n");
return -EIO;
}
return size;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret, which;
ret = copy_from_user(&which, (void *)arg, sizeof(int));
if (ret)
{
printk("用户向内核拷贝数据失败\n");
return -EIO;
}
switch (cmd)
{
case LedON:
if (which == LED1)
vir_led1->ODR |= (1 << 10);
else if (which == LED2)
vir_led2->ODR |= (1 << 10);
else if (which == LED3)
vir_led3->ODR |= (1 << 8);
break;
case LedOFF:
if (which == LED1)
vir_led1->ODR &= ~(1 << 10);
else if (which == LED2)
vir_led2->ODR &= ~(1 << 10);
else if (which == LED3)
vir_led3->ODR &= ~(1 << 8);
break;
default:
printk("功能码错误\n");
break;
}
return 0;
}
int all_led_init(void)
{
vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
if (vir_led1 == NULL)
{
printk("vir_led1 映射失败\n");
return -ENOMEM;
}
printk("vir_led1 映射成功\n");
vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
if (vir_led2 == NULL)
{
printk("vir_led2 映射失败\n");
return -ENOMEM;
}
printk("vir_led2 映射成功\n");
vir_led3 = ioremap(PHY_LED3_ADDR, sizeof(gpio_t));
if (vir_led3 == NULL)
{
printk("vir_led3 映射失败\n");
return -ENOMEM;
}
printk("vir_led3 映射成功\n");
vir_rcc = ioremap(PHY_RCC_ADDR, 4);
if (vir_rcc == NULL)
{
printk("vir_rcc 映射失败\n");
return -ENOMEM;
}
printk("vir_rcc 映射成功\n");
vir_led1->MODER &= ~(3 << 20);
vir_led1->MODER |= (1 << 20);
vir_led1->ODR &= ~(1 << 10);
vir_led2->MODER &= ~(3 << 20);
vir_led2->MODER |= (1 << 20);
vir_led2->ODR &= ~(1 << 10);
vir_led3->MODER &= ~(3 << 16);
vir_led3->MODER |= (1 << 16);
vir_led3->ODR &= ~(1 << 8);
(*vir_rcc) |= (3 << 4);
return 0;
}
struct file_operations fops =
{
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.unlocked_ioctl = ioctl,
.release = mycdev_close,
};
struct cdev *cdev;
dev_t devno;
#if 0
unsigned int major = 0;
#else
unsigned int major = 500;
#endif
unsigned minor = 0;
const int count = 3;
static int __init demo_init(void)
{
int i,ret;
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("分配设备驱动对象失败\n");
ret = -ENOMEM;
goto ERR1;
}
printk("分配设备驱动对象成功\n");
cdev_init(cdev, &fops);
printk("初始化设备驱动对象成功\n");
if (major == 0)
{
ret = alloc_chrdev_region(&devno, minor, count, CNAME);
if (ret)
{
printk("动态设备号申请失败\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
printk("动态设备号申请成功\n");
}
else
{
ret = register_chrdev_region(MKDEV(major, minor), count, CNAME);
if (ret)
{
printk("静态设备号申请失败\n");
goto ERR2;
}
printk("静态设备号申请成功\n");
}
ret = cdev_add(cdev, MKDEV(major, minor), count);
if (ret)
{
printk("注册字符设备驱动失败\n");
goto ERR3;
}
printk("注册字符设备驱动成功\n");
cls = class_create(THIS_MODULE, "led");
if (IS_ERR(cls))
{
printk("创建逻辑节点目录失败\n");
ret = PTR_ERR(cls);
goto ERR4;
}
printk("创建逻辑节点目录成功\n");
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev))
{
printk("创建逻辑节点失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
printk("创建逻辑节点成功\n");
all_led_init();
return 0;
ERR5:
for (--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), count);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit demo_exit(void)
{
int i;
for (i = 0; i < count; i++)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major, minor), count);
kfree(cdev);
iounmap(vir_led1);
iounmap(vir_led2);
iounmap(vir_led3);
iounmap(vir_rcc);
unregister_chrdev(major, CNAME);
printk(KERN_ERR "kernel unload\n");
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
myled.h
#ifndef __MYLED_H__
#define __MYLED_H__
typedef struct
{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
} gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR 0X50000A28
enum LED
{
LED1=1,
LED2,
LED3
};
#define LED_ON _IO('a', 1)
#define LED_OFF _IO('a', 0)
#define LedON _IOW('a', 1, int)
#define LedOFF _IOW('a', 0, int)
#endif
test.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "myled.h"
int main(int argc, char const *argv[])
{
char buf[128] = {};
int fd1 = open("/dev/myled0", O_RDWR);
if (fd1 < 0)
{
printf("LED1设备文件打开失败\n");
exit(-1);
}
printf("LED1设备文件打开成功\n");
int fd2 = open("/dev/myled1", O_RDWR);
if (fd2 < 0)
{
printf("LED2设备文件打开失败\n");
exit(-1);
}
printf("LED2设备文件打开成功\n");
int fd3 = open("/dev/myled2", O_RDWR);
if (fd3 < 0)
{
printf("LED3设备文件打开失败\n");
exit(-1);
}
printf("LED3设备文件打开成功\n");
int which;
while (1)
{
printf("请输入控制命令: 1:开LED灯 0:关灯\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
printf("请输入要操作的灯: 1-LED1 2-LED2 3-LED3\n");
scanf("%d", &which);
getchar();
if (which == 1)
{
ioctl(fd1, LedON, &which);
sleep(1);
ioctl(fd1, LedOFF, &which);
}
else if (which == 2)
{
ioctl(fd2, LedON, &which);
sleep(1);
ioctl(fd2, LedOFF, &which);
}
else if (which == 3)
{
ioctl(fd3, LedON, &which);
sleep(1);
ioctl(fd3, LedOFF, &which);
}
}
close(fd1);
close(fd2);
close(fd3);
return 0;
}