嵌入式Linux字符设备驱动开发流程——以LED为例
1.设备模块加载及卸载
2.静态申请设备号
3.动态申请设备号
4.注册字符类设备
5.生成字符设备节点
6.完善字符类设备驱动
7.根据完善后的模板编写相关设备驱动
前言
留空
头文件
#include <linux/fs.h> //文件操作的结构体
文件的结构体
文件(路径):未知
/*
* NOTE:
* all file operations except setlease can be called without
* the big kernel lock held in all filesystems.
*/
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
/* remove by cym 20130408 support for MT660.ko */
#if 0
//#ifdef CONFIG_SMM6260_MODEM
#if 1// liang, Pixtree also need to use ioctl interface...
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
#endif
#endif
/* end remove */
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
/* add by cym 20130408 support for MT6260 and Pixtree */
#if defined(CONFIG_SMM6260_MODEM) || defined(CONFIG_USE_GPIO_AS_I2C)
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
#endif
/* end add */
};
文件的结构体的常用参数
struct file_operations {
struct module *owner;
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, int datasync);
};
驱动程序
#include <linux/module.h> //模块头文件
#include <linux/kernel.h> //内核头文件
#include <linux/init.h> //内核初始化
#include <linux/fs.h> //字符设备函数
#include <linux/cdev.h> //字符设备描述
#include <linux/kdev_t.h> //系列设备号处理宏
#include <linux/slab.h> //内存分配头文件
#include <linux/device.h> //设备类节点头文件
#define DEVICE_NAME "leds" //字符设备名称
#define NODE_NAME "led"
#define DEVICE_MINOR_NUM 2 //字符设备数量
#define DEV_MAJOR 0 //主设备号
#define DEV_MINOR 0 //次设备号,0为自动分配
static int leds_major = DEV_MAJOR; //主设备号变量
static int leds_minor = DEV_MINOR; //次设备号变量
static dev_t leds_dev; //设备号
struct cdev *leds_cdev; //字符设备结构体变量
static struct class *leds_class; //类结构体变量
static int leds_open(struct inode *inode, struct file *file)
{
int ret = 0;
printk(KERN_EMERG "leds ops open is ok\n");
return ret;
}
static int leds_release(struct inode *inode, struct file *file)
{
int ret = 0;
printk(KERN_EMERG "leds ops release is ok\n");
return ret;
}
static ssize_t leds_read(struct file *file, char __user *buff, size_t count, loff_t *offp)
{
int ret = 0;
printk(KERN_EMERG "leds ops read is ok\n");
return ret;
}
static ssize_t leds_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
int ret = 0;
printk(KERN_EMERG "leds ops write is ok\n");
return ret;
}
static long leds_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
long ret = 0;
printk(KERN_EMERG "%s: cmd = %d, arg = %d\n", __FUNCTION__, cmd, (int)arg);
return ret;
}
struct file_operations leds_fops = {
.owner = THIS_MODULE,
.open = leds_open,
.release = leds_release,
.read = leds_read,
.write = leds_write,
.unlocked_ioctl = leds_ioctl,
};
static __init int leds_init(void)
{
int ret = 0;
int i;
ret = alloc_chrdev_region(&leds_dev, leds_minor, DEVICE_MINOR_NUM, DEVICE_NAME); //动态分配
if(ret < 0){
printk(KERN_EMERG "register_chrdev_region req %d is failed!\n", DEV_MAJOR);
return ret;
}
leds_major = MAJOR(leds_dev); //主设备号
leds_minor = MINOR(leds_dev); //次设备号
printk(KERN_EMERG "leds chrdev major=%d, minor=%d\n", leds_major, leds_minor);
leds_class = class_create(THIS_MODULE,DEVICE_NAME); //类节点创建
leds_cdev = kmalloc(DEVICE_MINOR_NUM * sizeof(struct cdev), GFP_KERNEL); //申请内存
if(leds_cdev == NULL)
{
printk(KERN_EMERG "kmalloc failed");
unregister_chrdev_region(leds_dev, DEVICE_MINOR_NUM);
return -ENOMEM;
}
//memset(leds_cdev, 0, DEVICE_MINOR_NUM * sizeof(struct dev_cdev));
for(i=0; i<DEVICE_MINOR_NUM; i++){
cdev_init(&leds_cdev[i], &leds_fops); //字符类设备初始化
leds_cdev[i].owner = THIS_MODULE;
leds_cdev[i].ops = &leds_fops;
ret = cdev_add(&leds_cdev[i], MKDEV(leds_major, leds_minor+i), 1); //注册到设备
device_create(leds_class, NULL, MKDEV(leds_major, leds_minor+i), NULL, NODE_NAME"%d", i); //设备节点创建
if(ret < 0){
printk(KERN_EMERG "cdev_add %d failed!\n", i);
}
else{
printk(KERN_EMERG "cdev_add %d success!\n", i);
}
}
return ret;
}
static __exit void leds_exit(void)
{
int i;
for(i=0; i<DEVICE_MINOR_NUM; i++){
cdev_del(&leds_cdev[i]); //注销设备
device_destroy(leds_class, MKDEV(leds_major, leds_minor+i)); //注销设备节点
}
class_destroy(leds_class); //类节点注销
kfree(leds_cdev); //释放内存
unregister_chrdev_region(leds_dev, DEVICE_MINOR_NUM); //注销设备号
printk(KERN_EMERG "leds chrdev exit \n");
}
module_init(leds_init);
module_exit(leds_exit);
MODULE_LICENSE("GPL");
应用程序
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv)
{
int fd,ret=0;
char *led0 = "/dev/led0";
char *led1 = "/dev/led1";
if((fd = open(led0, O_RDWR|O_NOCTTY|O_NDELAY))<0){
printf("open %s failed\n",led0);
exit(1);
}
else{
printf("open %s success! fd=%d\n", led0, fd);
}
close(fd);
if((fd = open(led1, O_RDWR|O_NOCTTY|O_NDELAY))<0){
printf("open %s failed\n",led1);
exit(1);
}
else{
printf("open %s success! fd=%d\n", led1, fd);
}
close(fd);
return ret;
}
编译
make
加载编译后的模块
insmod leds.ko
查看字符设备节点
ls /dev/l*
执行应用程序
./leds_app 0 0
结束语
以上则为嵌入式Linux字符设备LED及应用程序编写的相关内容。
如果文章对您有帮助,欢迎移至上方按钮打赏博主;