1.申请设备号:
之前用的是register_chrdev(LED_MAJOR, LED_NAME, &led_fops);手动申请很不方便
使用alloc_chrdev_region函数申请设备号,手动申请的话要先查询是否有空余的设备号,很不方便,用此函数内核会自动将将空余设备号给你,释放设备号用unregister_chrdev_region
如果指定主设备号则
用register_chrdev_region函数,需要用MKDEV构建完整的dev_t,卸载也用unregister_chrdev_region
2.代码:
驱动代码:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#define NEWCHRLED_NAME "newchrled"
#define NEWCHRLED_COUNT 1
// 实际地址
#define PMU_GRF_BASE (0xFDC20000)
#define PMU_GRF_GPIO0C_IOMUX_L (PMU_GRF_BASE + 0x0010)
#define PMU_GRF_GPIO0C_DS_0 (PMU_GRF_BASE + 0X0090)
#define GPIO0_BASE (0xFDD60000)
#define GPIO0_SWPORT_DR_H (GPIO0_BASE + 0X0004)
#define GPIO0_SWPORT_DDR_H (GPIO0_BASE + 0X000C)
#define LEDOPEN 1
#define LEDCLOSE 0
/* 映射后的寄存器虚拟地址指针 */
static void __iomem *PMU_GRF_GPIO0C_IOMUX_L_PI;
static void __iomem *PMU_GRF_GPIO0C_DS_0_PI;
static void __iomem *GPIO0_SWPORT_DR_H_PI;
static void __iomem *GPIO0_SWPORT_DDR_H_PI;
// led gpio初始化操作
void gpio_init(void){
u32 val = 0;
// 设置GPIO0_c0为GPIO功能
val = readl(PMU_GRF_GPIO0C_IOMUX_L_PI);
val &= ~(0x7 << 0); //最低三位置0
val |= ((0x7 << 16) | (0x0 << 0)); // 16 17 18位置1其他不变,bit2:0:0,用作GPIO0_C0
writel(val, PMU_GRF_GPIO0C_IOMUX_L_PI);
// 设置GPIO_C0驱动能力为level5
val = readl(PMU_GRF_GPIO0C_DS_0_PI);
val &= ~(0x3f << 0); // 0 ~ 5置0
val |= ((0x3f << 16) | (0x3f << 0)); // 16 ~ 21置1,0~5置1同时用作GPIO0c0
writel(val, PMU_GRF_GPIO0C_DS_0_PI);
// 设置GPIOO0_c0为输出
val = readl(GPIO0_SWPORT_DDR_H_PI);
val &= ~(0x1 << 0); // 0置0
val |= ((0x1 << 16) | (0x1 << 0)); // 16置1,0置1
writel(val, GPIO0_SWPORT_DDR_H_PI);
// 设置GPIO_c0为低电平,关闭LED
val = readl(GPIO0_SWPORT_DR_H_PI);
val &= ~(0x1 << 0);
val |= ((0x1 << 16) | (0x0 << 0));
writel(val, GPIO0_SWPORT_DR_H_PI);
}
// 开关
void led_switch(int status){
u32 val = 0;
if(status == LEDOPEN){
// 开灯
val = readl(GPIO0_SWPORT_DR_H_PI);
val &= ~(0X1 << 0); /* bit0 清零*/
val |= ((0X1 << 16) | (0X1 << 0)); /* bit16 置1,允许写bit0,
bit0,高电平*/
writel(val, GPIO0_SWPORT_DR_H_PI);
}else if(status == LEDCLOSE){
// 关灯
val = readl(GPIO0_SWPORT_DR_H_PI);
val &= ~(0X1 << 0); /* bit0 清零*/
val |= ((0X1 << 16) | (0X0 << 0)); /* bit16 置1,允许写bit0,
bit0,低电平 */
writel(val, GPIO0_SWPORT_DR_H_PI);
}
}
// 真实物理地址映射虚拟内存函数
void led_remap(void){
PMU_GRF_GPIO0C_IOMUX_L_PI = ioremap(PMU_GRF_GPIO0C_IOMUX_L, 4);
PMU_GRF_GPIO0C_DS_0_PI = ioremap(PMU_GRF_GPIO0C_DS_0, 4);
GPIO0_SWPORT_DR_H_PI = ioremap(GPIO0_SWPORT_DR_H, 4);
GPIO0_SWPORT_DDR_H_PI = ioremap(GPIO0_SWPORT_DDR_H, 4);
}
// 释放
void led_releaseMap(void){
// 取消地址映射
iounmap(PMU_GRF_GPIO0C_IOMUX_L_PI);
iounmap(PMU_GRF_GPIO0C_DS_0_PI);
iounmap(GPIO0_SWPORT_DR_H_PI);
iounmap(GPIO0_SWPORT_DDR_H_PI);
}
/*LED 设备结构体*/
struct newchrled_dev{
struct cdev cdev; // 字符设备
dev_t devid; // 设备号
int major; // 主设备号
int minor; // 次设备号
};
struct newchrled_dev newchrled; // led设备
static int newchrled_open(struct inode* inode, struct file* filp){
return 0;
}
static int newchrled_release(struct inode* inode, struct file* filp){
return 0;
}
static ssize_t newchrled_write(struct file* filp, const char __user* buf, size_t count, loff_t* ppos){
int ret;
unsigned char databuf[1];
unsigned char state;
ret = copy_from_user(databuf, buf, count);
if(ret < 0){
printk("kernel write failed\r\n");
return -EFAULT;
}
state = databuf[0];
if(state == LEDOPEN){
led_switch(LEDOPEN);
}else if(state == LEDCLOSE){
led_switch(LEDCLOSE);
}
return 0;
}
static const struct file_operations newchrled_fops = {
.owner = THIS_MODULE,
.write = newchrled_write,
.open = newchrled_open,
.release = newchrled_release,
};
/*入口*/
static int __init newchrled_init(void){
int ret = 0;
printk("new chrled init\r\n");
/*初始化虚拟内存,初始化gpio*/
led_remap();
gpio_init();
// led初始化
if(newchrled.major){ // 给定主设备号
newchrled.devid = MKDEV(newchrled.major, 0); // 次设备号从零开始
ret = register_chrdev_region(newchrled.devid, NEWCHRLED_COUNT, NEWCHRLED_NAME); // 注册一个
}else{ // 自己申请
ret = alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_COUNT, NEWCHRLED_NAME); //由0开始申请1个
newchrled.major = MAJOR(newchrled.devid);
newchrled.minor = MINOR(newchrled.devid);
}
if(ret < 0){
printk("newchrled chrdev_region err\r\n");
return -1;
}
printk("newchrled major = %d, minor= %d\r\n", newchrled.major, newchrled.minor);
newchrled.cdev.owner = THIS_MODULE;
cdev_init(&newchrled.cdev, &newchrled_fops); // 初始化完
ret = cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_COUNT);
return 0;
}
/*出口*/
static void __exit newchrled_exit(void){
printk("new chrled exit\r\n");
led_releaseMap();
// 删除字符设备
cdev_del(&newchrled.cdev);
// 注销设备号
unregister_chrdev_region(newchrled.devid, NEWCHRLED_COUNT);
}
/* 注册驱动和卸载驱动*/
module_init(newchrled_init);
module_exit(newchrled_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("narnat");
核心主要在这个部分:
if(newchrled.major){ // 给定主设备号
newchrled.devid = MKDEV(newchrled.major, 0); // 次设备号从零开始
ret = register_chrdev_region(newchrled.devid, NEWCHRLED_COUNT, NEWCHRLED_NAME); // 注册一个
}else{ // 自己申请
ret = alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_COUNT, NEWCHRLED_NAME); //由0开始申请1个
newchrled.major = MAJOR(newchrled.devid);
newchrled.minor = MINOR(newchrled.devid);
}
if(ret < 0){
printk("newchrled chrdev_region err\r\n");
return -1;
}
如果给定了主设备号则根据设备号注册一个MKDEV是获取设备号,注册设备用设备号,否则向内核申请
alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_COUNT, NEWCHRLED_NAME)是申请设备号0
假设alloc_chrdev_region 分配的主设备号是 250,firstminor = 0,NEWCHRLED_COUNT = 3,那么分配的设备号范围是:
主设备号:250
次设备号:0、1、2
cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_COUNT);才是注册一个字符设备