int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
int register_chrdev_region(dev_t from, unsigned count, const char *name)
void unregister_chrdev_region(dev_t from, unsigned count)
int major;/* 主设备号 */
int minor;/* 次设备号 */
dev_t devid;/* 设备号 */
if (major) {/* 定义了主设备号 */
devid = MKDEV(major, 0); /* 大部分驱动次设备号都选择 0 */
register_chrdev_region(devid, 1, "test");
}
else {/* 没有定义设备号 */
alloc_chrdev_region(&devid, 0, 1, "test"); /* 申请设备号 */
major = MAJOR(devid);/* 获取分配号的主设备号 */
minor = MINOR(devid);/* 获取分配号的次设备号 */
}
unregister_chrdev_region(devid, 1); /* 注销设备号 */
1.2 新的字符设备注册方法
的定义如下:
struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
struct cdev test_cdev;
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
struct cdev testcdev;
/* 设备操作函数 */
static struct file_operations test_fops = {
.owner = THIS_MODULE,
* 其他具体的初始项 */
};
testcdev.owner = THIS_MODULE;
cdev_init(&testcdev, &test_fops); /* 初始化 cdev 结构体变量 */
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
struct cdev testcdev;
/* 设备操作函数 */
static struct file_operations test_fops = {
.owner = THIS_MODULE,
/* 其他具体的初始项 */
};
testcdev.owner = THIS_MODULE;
cdev_init(&testcdev, &test_fops); /* 初始化 cdev 结构体变量 */
cdev_add(&testcdev, devid, 1); /* 添加字符设备 */
void cdev_del(struct cdev *p)
cdev_del(&testcdev); /* 删除 cdev */
echo /sbin/mdev > /proc/sys/kernel/hotplug
#define class_create(owner, name) \
({ \
static struct lock_class_key __key; \
__class_create(owner, name, &__key); \
})
struct class *__class_create(struct module *owner, const char *name,
struct lock_class_key *key)
根据上述代码,将宏 class_create 展开以后内容如下:
struct class *class_create (struct module *owner, const char *name)
void class_destroy(struct class *cls);
struct device *device_create(struct class *class,
struct device *parent,
dev_t devt,
void *drvdata,
const char *fmt, ...)
void device_destroy(struct class *class, dev_t devt)
参数 class 是要删除的设备所处的类,参数 devt 是要删除的设备号。
2.3 参考示例
在驱动入口函数里面创建类和设备,在驱动出口函数里面删除类和设备,参考示例如下:
struct class *class; /* 类 */
struct device *device; /* 设备 */
dev_t devid;/* 设备号 */
/* 驱动入口函数 */
static int __init led_init(void)
{
/* 创建类 */
class = class_create(THIS_MODULE, "xxx");
/* 创建设备 */
device = device_create(class, NULL, devid, NULL, "xxx");
return 0;
}
/* 驱动出口函数 */
static void __exit led_exit(void)
{
/* 删除设备 */
device_destroy(newchrled.class, newchrled.devid);
/* 删除类 */
class_destroy(newchrled.class);
}
module_init(led_init);
module_exit(led_exit);
3 设置文件私有数据
/* 设备结构体 */
struct test_dev{
dev_t devid; /* 设备号
struct cdev cdev; /* cdev */
struct class *class; /* 类*/
struct device *device; /* 设备*/
int major;/* 主设备号 */
int minor;/* 次设备号 */
};
struct test_dev testdev;
/* open 函数 */
static int test_open(struct inode *inode, struct file *filp)
{
filp->private_data = &testdev; /* 设置私有数据 */
return 0;
}
5 实验程序编写
5.1 LED 灯驱动程序编写
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define NEWCHRLED_NAME "newchrled"
#define NEWCHRLED_COUNT 1
#define LEDOFF 0 /*关闭*/
#define LEDON 1 /*打开*/
/*寄存器物理地址*/
#define CCM_CCGR1_BASE (0x020C406C)
#define SW_MUX_GPIO1_IO03_BASE (0x020E0068)
#define SW_PAD_GPIO1_IO03_BASE (0x020E02F4)
#define GPIO1_DR_BASE (0x0209C000)
#define GPIO1_GDIR_BASE (0x0209C004)
/*地址映射后的虚拟地址指针*/
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;
/*LED 设备结构体*/
struct newchrled_dev
{
struct cdev cdev; /*字符设备*/
dev_t devid; /*设备号*/
struct class *class; /*类*/
struct device *device; /*设备*/
int major; /*主设备号*/
int minor; /*此设备号*/
};
struct newchrled_dev newchrled; /*led 设备*/
/*LED 灯打开/关闭 */
static void led_switch(u8 sta)
{
u32 val = 0;
if (sta == LEDON)
{
val = readl(GPIO1_DR);
val &= ~(1 << 3); /*bit3清零,打开LED*/
writel(val, GPIO1_DR);
}
else if (sta == LEDOFF)
{
val = readl(GPIO1_DR);
val |= 1 << 3; /*bit3置1,关闭LED*/
writel(val, GPIO1_DR);
}
}
static int newchrled_open(struct inode *inode, struct file *filp)
{
filp->private_data = &newchrled;
return 0;
}
static int newchrled_release(struct inode *inode, struct file *filp)
{
struct newchrled_dev *dev = (struct newchrled_dev*)filp->private_data;
return 0;
}
static ssize_t newchrled_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
int retvalue;
unsigned char databuf[1];
retvalue = copy_from_user(databuf, buf, count);
if (retvalue < 0)
{
printk("kernel write failed! \r\n");
return -EFAULT;
}
/*判断是开灯还是关灯*/
led_switch(databuf[0]);
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;
unsigned int val = 0;
printk("newchrled_init\r\n");
/* 初始化 LED */
/* 1、寄存器地址映射 */
IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);
SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4);
GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);
/* 2、使能 GPIO1 时钟 */
val = readl(IMX6U_CCM_CCGR1);
val &= ~(3 << 26); /*先清除以前的配置bit26,27*/
val |= 3 << 26; /*bit26,27置1*/
writel(val, IMX6U_CCM_CCGR1);
/* 3、设置 GPIO1_IO03 的复用功能,将其复用为
* GPIO1_IO03,最后设置 IO 属性。
*/
writel(0x5, SW_MUX_GPIO1_IO03); /*设置复用*/
/* 寄存器 SW_PAD_GPIO1_IO03 设置 IO 属性 */
writel(0x10B0, SW_PAD_GPIO1_IO03); /*设置电气属性*/
/* 4、设置 GPIO1_IO03 为输出功能 */
val = readl(GPIO1_GDIR);
val |= 1 << 3; /*bit3置1,设置为输出*/
writel(val, GPIO1_GDIR);
/* 5、默认关闭 LED */
val = readl(GPIO1_DR);
val |= 1 << 3; /*bit3置1,关闭LED*/
writel(val, GPIO1_DR);
/*注册字符设备*/
/* 1、创建设备号 */
newchrled.major=0; /*设置为0,表示由系统申请设备号*/
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);
newchrled.major =MAJOR(newchrled.devid);
newchrled.minor =MINOR(newchrled.devid);
}
if(ret<0)
{
printk("newchrled chrdev_region err!\r\n");
goto fail_devid;
}
printk("newchrled major = %d,minor=%d\r\n",newchrled.major,newchrled.minor);
/* 2、初始化 cdev */
newchrled.cdev.owner = THIS_MODULE;
cdev_init(&newchrled.cdev,&newchrled_fops);
/* 3、添加一个 cdev */
ret= cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_COUNT);
if(ret<0)
{
goto fail_cdev;
}
/* 4、创建类 */
newchrled.class = class_create(THIS_MODULE,NEWCHRLED_NAME);
if (IS_ERR(newchrled.class))
{
ret = PTR_ERR(newchrled.class);
goto fail_class;
}
/*5.创建设备*/
newchrled.device = device_create(newchrled.class, NULL,
newchrled.devid, NULL, NEWCHRLED_NAME);
if (IS_ERR(newchrled.device))
{
ret=PTR_ERR(newchrled.device);
goto fail_device;
}
return 0;
fail_device:
class_destroy(newchrled.class);
fail_class:
cdev_del(&newchrled.cdev);
fail_cdev:
unregister_chrdev_region(newchrled.devid, NEWCHRLED_COUNT);
fail_devid:
return ret;
}
/*出口函数*/
static void __exit newchrled_exit(void)
{
unsigned int val = 0;
printk("newchrled_exit\r\n");
val = readl(GPIO1_DR);
val |= 1 << 3; /*bit3置1,打开LED*/
writel(val, GPIO1_DR);
/*1.取消地址映射*/
iounmap(IMX6U_CCM_CCGR1);
iounmap(SW_MUX_GPIO1_IO03);
iounmap(SW_PAD_GPIO1_IO03);
iounmap(GPIO1_DR);
iounmap(GPIO1_GDIR);
/*1、删除字符设备*/
cdev_del(&newchrled.cdev);
/*2、注销设备号*/
unregister_chrdev_region(newchrled.devid, NEWCHRLED_COUNT);
/*3、摧毁设备*/
device_destroy(newchrled.class, newchrled.devid);
/*4、摧毁类*/
class_destroy(newchrled.class);
}
/*注册和卸载驱动*/
module_init(newchrled_init);
module_exit(newchrled_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("supersmart");
#include
#include
#include
#include
#include
#include
#include
/*
*argc:应用程序参数个数
* argv[]:具体的参数内容,字符串形式
* ./ledAPP <0:1> 0 关灯,1 开灯
* ./ledAPP /dev/led 0 关灯
* ./ledAPP /dev/led 1 开灯
*/
#define LEDOFF 0
#define LEDON 1
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
unsigned char databuf[1];
if (argc != 3)
{
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if (fd < 0)
{
printf("file %s open failed!\r\n", filename);
return -1;
}
databuf[0] = atoi(argv[2]); /*将字符转化为数字*/
retvalue = write(fd, databuf, sizeof(databuf));
if (retvalue < 0)
{
printf("LED Control Failed ! \r\n");
close(fd);
return -1;
}
close(fd);
return 0;
}
KERNELDIR := /home/znn/linux/IMX6ULL/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga
CURRENT_PAHT := $(shell pwd)
obj-m := newchrled.o
build :kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) clean
2、编译测试 APP
输入如下命令编译测试 ledAPP.c 这个测试程序: