Linux字符设备驱动
字符设备驱动框架
模块加载时调用init函数
模块卸载时调用exit函数
驱动编译方法
1.
vi drivers/char/Kconfig
make menuconfig
make modules生成.ko文件
将.ko文件移动到开发板上
sudo /etc/init.d/networking restart 重启网络
自己添加设备号以及设备节点
register_chrdev函数
创建设备
static inline int register_chrdev(unsigned int major, const char *name,
const struct file_operations *fops)
参数:
major:主设备号
cat /proc/devices 打印设备
name:设备名
fops:结构体
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 *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
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 *, struct dentry *, 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 **);
};
mknod /dev/first_device c 200 1
unregister_chrdev函数
static inline void unregister_chrdev(unsigned int major, const char *name)
参数:
major主设备号
name:设备名
LED驱动
int led_device_open(struct inode *pnoed, struct file *fp)
{
printk(“open success\n”);
return 0;
}
int led_device_release(struct inode *pnode, struct file *fp)
{
printk(“release success\n”);
return 0;
}
ssize_t led_device_read(struct file *fp, char __user *puser, size_t len, loff_t *offset)
{
printk(“read success\n”);
return 0;
}
ssize_t led_device_write(struct file *fp, const char __user *puser, size_t len, loff_t *offset)
write(fd, &c, 1);
puser指向c;
len指向1;
copy_from_user函数
static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
参数
to:拷到哪
from:从哪拷
len:拷多少
ioremap函数
ioremap(0x56000000, 4);
自己添加设备号以及次设备号代码示例
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<asm/io.h>
#include<asm/uaccess.h>
#define DEVICE_MAJOR 200
#define DEVICE_NAME "led_device"
unsigned int *regGPBCON;
unsigned int *regGPBDAT;
void led_on(unsigned char n)
{
//0000 1010
int t;
t = *regGPBDAT;
t &= ~(0x0F << 5);
n &= 0x0F;
n ^= 0x0F; //0000 0101
t |= (n << 5);
*regGPBDAT = t;
}
int led_device_open(struct inode *pnoed, struct file *fp)
{
printk("open success\n");
return 0;
}
int led_device_release(struct inode *pnode, struct file *fp)
{
printk("release success\n");
return 0;
}
ssize_t led_device_read(struct file *fp, char __user *puser, size_t len, loff_t *offset)
{
printk("read success\n");
return 0;
}
ssize_t led_device_write(struct file *fp, const char __user *puser, size_t len, loff_t *offset)
{
unsigned int c;
copy_from_user(&c, puser, len);
led_on(c);
return 0;
}
struct file_operations fops =
{
.read = led_device_read,
.write = led_device_write,
.open = led_device_open,
.release = led_device_release,
.owner = THIS_MODULE
};
static int __init init_led_driver(void)
{
register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &fops);
regGPBCON = ioremap(0x56000010, 4);
regGPBDAT = ioremap(0x56000014, 4);
*regGPBCON &= ~(0x03<<10 | 0x03<<12 | 0x03 << 14 | 0x03 << 16);
*regGPBCON |= (0x01<<10) | (0x01<<12) | (0x01<<14) | (0x01<<16);
printk("first_driver_init\n");
return 0;
}
static void __exit exit_led_driver(void)
{
unregister_chrdev(DEVICE_MAJOR, DEVICE_NAME);
printk("exit first_driver_exit\n");
}
module_init(init_led_driver);
module_exit(exit_led_driver);
MODULE_LICENSE("GPL");
自动申请设备号
alloc_chrdev_region函数
1.自动申请设备号函数
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
const char *name);
参数
dev_t设备号的数据类型
dev:传递一个dev_t类型的变量地址
baseminor:子设备号
count:申请子设备个数
name:设备名
打印主设备号与子设备号数
printk("%u %u\n", dev_num >> 20, dev_num & 0xFFFFF);
cdev_init函数
2.初始化cdev中的变量
void cdev_init(struct cdev *cdev, const struct file_operations *fops);
参数
cdev:struct cdev类型变量地址
fops:文件操作结构体变量指针
cdev_add函数
3.将自动获得设备号的设备添加到字符设备驱动中
int cdev_add(struct cdev *p, dev_t dev, unsigned count);
参数
p:struct cdev类型变量地址
dev:dev类型变量
count:申请个数
struct cdev {
struct kobject kobj;
struct module *owner; THIS_MODULE
const struct file_operations *ops; 文件操作结构体
struct list_head list; 链表头
dev_t dev; 设备号
unsigned int count;
};
cdev_del函数
4.删除字符设备
void cdev_del(struct cdev *p)
5.创建class
#define class_create(owner, name)
参数
owner:owenr指向模块的指针,这里只需传THIS_MODULE
name:为该设备的类型起个名字
该函数返回struct class *,用宏IS_ERR判断是否合法
6.添加device
struct device *device_create(struct class *class, struct device *parent, dev_t
devt, void *drvdata, const char *fmt, …);
参数:
class:struct class 类型变量地址
parent:依赖
devt:设备号变量
7.删除class
void class_destroy(struct class *cls)
8.删除device
void device_destroy(struct class *class, dev_t devt)
自动申请设备号代码示例
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<asm/io.h>
#include<asm/uaccess.h>
#include<linux/cdev.h>
#include<linux/device.h>
#define DEVICE_NAME "led_device"
unsigned int *regGPBCON;
unsigned int *regGPBDAT;
void led_on(unsigned char n)
{
//0000 1010
int t;
t = *regGPBDAT;
t &= ~(0x0F << 5);
n &= 0x0F;
n ^= 0x0F; //0000 0101
t |= (n << 5);
*regGPBDAT = t;
}
int led_device_open(struct inode *pnoed, struct file *fp)
{
printk("open success\n");
return 0;
}
int led_device_release(struct inode *pnode, struct file *fp)
{
printk("release success\n");
return 0;
}
ssize_t led_device_read(struct file *fp, char __user *puser, size_t len, loff_t *offset)
{
printk("read success\n");
return 0;
}
ssize_t led_device_write(struct file *fp, const char __user *puser, size_t len, loff_t *offset)
{
unsigned int c;
copy_from_user(&c, puser, len);
led_on(c);
return len;
}
struct file_operations fops =
{
.read = led_device_read,
.write = led_device_write,
.open = led_device_open,
.release = led_device_release,
.owner = THIS_MODULE
};
dev_t dev_num;
struct cdev dev_led;
struct class *pclass;
struct device *pdevice;
static int __init init_led_driver(void)
{
alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME);
printk("%u %u\n", dev_num >> 20, dev_num & 0xFFFFF);
cdev_init(&dev_led, &fops);
cdev_add(&dev_led, dev_num, 1);
pclass = class_create(THIS_MODULE, "led_class");
if(IS_ERR(pclass))
{
cdev_del(&dev_led);
unregister_chrdev_region(dev_num, 1);
return -1;
}
pdevice = device_create(pclass, NULL, dev_num, NULL, "led_device");
if(pdevice == NULL)
{
class_destroy(pclass);
cdev_del(&dev_led);
unregister_chrdev_region(dev_num, 1);
return -1;
}
regGPBCON = ioremap(0x56000010, 4);
regGPBDAT = ioremap(0x56000014, 4);
*regGPBCON &= ~(0x03<<10 | 0x03<<12 | 0x03 << 14 | 0x03 << 16);
*regGPBCON |= (0x01<<10) | (0x01<<12) | (0x01<<14) | (0x01<<16);
printk("first_driver_init\n");
return 0;
}
static void __exit exit_led_driver(void)
{
iounmap(regGPBCON);
iounmap(regGPBDAT);
device_destroy(pclass, dev_num);
class_destroy(pclass);
cdev_del(&dev_led);
unregister_chrdev_region(dev_num, 1);
printk("exit first_driver_exit\n");
}
module_init(init_led_driver);
module_exit(exit_led_driver);
MODULE_LICENSE("GPL");
混杂设备驱动
misc_register函数
1.创建混杂设备
int misc_register(struct miscdevice * misc)
struct miscdevice {
int minor;次设备号 MISC_DYNAMIC_MINOR(系统自动分配次设备号)
const char *name; dev设备文件名
const struct file_operations *fops;文件操作结构体
struct list_head list;链表头
struct device *parent;
struct device *this_device;
const char *nodename;
mode_t mode;
};
misc_deregister函数
2.删除混杂设备
int misc_deregister(struct miscdevice * misc)
混杂设备驱动代码示例
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/miscdevice.h>
#include<asm/uaccess.h>
#include<asm/io.h>
unsigned int *regGPBCON;
unsigned int *regGPBDAT;
void led_on(unsigned char n)
{
//0000 1010
int t;
t = *regGPBDAT;
t &= ~(0x0F << 5);
n &= 0x0F;
n ^= 0x0F; //0000 0101
t |= (n << 5);
*regGPBDAT = t;
}
static ssize_t led_misc_device_read(struct file *fp, char __user *puser, size_t len, loff_t *offset)
{
return 0;
}
static ssize_t led_misc_device_write(struct file *fp, const char __user *puser, size_t len, loff_t *offset)
{
unsigned int c;
copy_from_user(&c, puser, len);
led_on(c);
return len;
}
int led_misc_device_open(struct inode *pnode, struct file *fp)
{
return 0;
}
int led_misc_device_release(struct inode *pnode, struct file *fp)
{
return 0;
}
struct file_operations fops =
{
.owner = THIS_MODULE,
.read = led_misc_device_read,
.write = led_misc_device_write,
.open = led_misc_device_open,
.release = led_misc_device_release
};
static struct miscdevice led_device =
{
.minor = MISC_DYNAMIC_MINOR,
.name = "led_device",
.fops = &fops,
};
static int __init led_driver_init(void)
{
misc_register(&led_device);
regGPBCON = ioremap(0x56000010, 4);
regGPBDAT = ioremap(0x56000014, 4);
*regGPBCON &= ~(0x03<<10 | 0x03<<12 | 0x03 << 14 | 0x03 << 16);
*regGPBCON |= (0x01<<10) | (0x01<<12) | (0x01<<14) | (0x01<<16);
return 0;
}
static void __exit led_driver_exit(void)
{
iounmap(regGPBCON);
iounmap(regGPBDAT);
misc_deregister(&led_device);
}
module_init(led_driver_init);
module_exit(led_driver_exit);
MODULE_LICENSE("GPL");