字符設備是3大類設備(字符設備、塊設備和網絡設備)中較簡單的一類設備,其驅動程序中完成的主要工作是初始化、添加和刪除cdev結構體,申請和釋放設備號,以及填充 file_operations結構體中的操作函數,實現file_operations結構體中的read()、write()和ioctl()等函數是驅動設計的主體工作。
/*
* 虛擬字符設備globalmem實例:
* 在globalmem字符設備驅動中會分配一片大小為 GLOBALMEM_SIZE(4KB)
* 的內存空間,並在驅動中提供針對該片內存的讀寫、控制和定位函數,以供用戶空間的進程能通過
* Linux系統調用訪問這片內存。
*/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#define DEV_NAME "globalmem" /* /dev中顯示的設備名 */
#define DEV_MAJOR 0 /* 指定主設備號,為0則動態獲取 */
/* ioctl用的控制字 */
#define GLOBALMEM_MAGIC 'M'
#define MEM_CLEAR _IO(GLOBALMEM_MAGIC, 0)
/*--------------------------------------------------------------------- local vars */
/*globalmem設備結構體*/
typedef struct {
struct cdev cdev; /* 字符設備cdev結構體*/
#define MEM_SIZE 0x1000 /*全局內存最大4K字節*/
unsigned char mem[MEM_SIZE]; /*全局內存*/
struct semaphore sem; /*並發控制用的信號量*/
} globalmem_dev_t;
static int globalmem_major = DEV_MAJOR;
static globalmem_dev_t *globalmem_devp; /*設備結構體指針*/
/*--------------------------------------------------------------------- file operations */
/*文件打開函數*/
static int globalmem_open(struct inode *inodep, struct file *filep)
{
/* 獲取dev指針 */
globalmem_dev_t *dev = container_of(inodep->i_cdev, globalmem_dev_t, cdev);
filep->private_data = dev;
return 0;
}
/*文件釋放函數*/
static int globalmem_release(struct inode *inodep, struct file *filep)
{
return 0;
}
/*讀函數*/
static ssize_t globalmem_read(struct file *filep, char __user *buf, size_t len, loff_t *ppos)
{
globalmem_dev_t *dev = filep->private_data;
unsigned long p = *ppos;
int ret = 0;
/*分析和獲取有效的長度*/
if (p > MEM_SIZE) {
printk(KERN_EMERG "%s: overflow!\n", __func__);
return - ENOMEM;
}
if (len > MEM_SIZE - p) {
len = MEM_SIZE - p;
}
if (down_interruptible(&dev->sem)) /* 獲得信號量*/
return - ERESTARTSYS;
/*內核空間->用戶空間*/
if (copy_to_user(buf, (void*)(dev->mem + p), len)) {
ret = - EFAULT;
}else{
*ppos += len;
printk(KERN_INFO "%s: read %d bytes from %d\n", DEV_NAME, (int)len, (int)p);
ret = len;
}
up(&dev->sem); /* 釋放信號量*/
return ret;
}
/*寫函數*/
static ssize_t globalmem_write(struct file *filep, const char __user *buf, size_t len, loff_t *ppos)
{
globalmem_dev_t *dev = filep->private_data;
int ret = 0;
unsigned long p = *ppos;
if (p > MEM_SIZE) {
printk(KERN_EMERG "%s: overflow!\n", __func__);
return - ENOMEM;
}
if (len > MEM_SIZE - p) {
len = MEM_SIZE - p;
}
if (down_interruptible(&dev->sem)) /* 獲得信號量*/
return - ERESTARTSYS;
/*用戶空間->內核空間*/
if (copy_from_user(dev->mem + p, buf, len)) {
ret = - EFAULT;
}else{
*ppos += len;
printk(KERN_INFO "%s: written %d bytes from %d\n", DEV_NAME, (int)len, (int)p);
ret = len;
}
up(&dev->sem); /* 釋放信號量*/
return ret;
}
/* seek文件定位函數 */
static loff_t globalmem_llseek(struct file *filep, loff_t offset, int start)
{
globalmem_dev_t *dev = filep->private_data;
int ret = 0;
if (down_interruptible(&dev->sem)) /* 獲得信號量*/
return - ERESTARTSYS;
switch (start) {
case SEEK_SET:
if (offset < 0 || offset > MEM_SIZE) {
printk(KERN_EMERG "%s: overflow!\n", __func__);
return - ENOMEM;
}
ret = filep->f_pos = offset;
break;
case SEEK_CUR:
if ((filep->f_pos + offset) < 0 || (filep->f_pos + offset) > MEM_SIZE) {
printk(KERN_EMERG "%s: overflow!\n", __func__);
return - ENOMEM;
}
ret = filep->f_pos += offset;
break;
default:
return - EINVAL;
break;
}
up(&dev->sem); /* 釋放信號量*/
printk(KERN_INFO "%s: set cur to %d.\n", DEV_NAME, ret);
return ret;
}
/* ioctl設備控制函數 */
static long globalmem_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
{
globalmem_dev_t *dev = filep->private_data;
switch (cmd) {
case MEM_CLEAR:
if (down_interruptible(&dev->sem)) /* 獲得信號量*/
return - ERESTARTSYS;
memset(dev->mem, 0, MEM_SIZE);
up(&dev->sem); /* 釋放信號量*/
printk(KERN_INFO "%s: clear.\n", DEV_NAME);
break;
default:
return - EINVAL;
}
return 0;
}
/*文件操作結構體*/
static const struct file_operations globalmem_fops = {
.owner = THIS_MODULE,
.open = globalmem_open,
.release = globalmem_release,
.read = globalmem_read,
.write = globalmem_write,
.llseek = globalmem_llseek,
.compat_ioctl = globalmem_ioctl
};
/*---------------------------------------------------------------------*/
/*初始化並注冊cdev*/
static int globalmem_setup(globalmem_dev_t *dev, int minor)
{
int ret = 0;
dev_t devno = MKDEV(globalmem_major, minor);
cdev_init(&dev->cdev, &globalmem_fops);
dev->cdev.owner = THIS_MODULE;
ret = cdev_add(&dev->cdev, devno, 1);
if (ret) {
printk(KERN_NOTICE "%s: Error %d dev %d.\n", DEV_NAME, ret, minor);
}
return ret;
}
/*設備驅動模塊加載函數*/
static int __init globalmem_init(void)
{
int ret = 0;
dev_t devno;
/* 申請設備號*/
if(globalmem_major){
devno = MKDEV(globalmem_major, 0);
ret = register_chrdev_region(devno, 2, DEV_NAME);
}else{ /* 動態申請設備號 */
ret = alloc_chrdev_region(&devno, 0, 2, DEV_NAME);
globalmem_major = MAJOR(devno);
}
if (ret < 0) {
return ret;
}
/* 動態申請設備結構體的內存,創建兩個設備 */
globalmem_devp = kmalloc(2*sizeof(globalmem_dev_t), GFP_KERNEL);
if (!globalmem_devp) {
unregister_chrdev_region(devno, 2);
return - ENOMEM;
}
ret |= globalmem_setup(&globalmem_devp[0], 0); /* globalmem0 */
ret |= globalmem_setup(&globalmem_devp[1], 1); /* globalmem1 */
if(ret)
return ret;
init_MUTEX(&globalmem_devp[0].sem); /*初始化信號量*/
init_MUTEX(&globalmem_devp[1].sem);
printk(KERN_INFO "globalmem: up %d,%d.\n", MAJOR(devno), MINOR(devno));
return 0;
}
/*模塊卸載函數*/
static void __exit globalmem_exit(void)
{
cdev_del(&globalmem_devp[0].cdev);
cdev_del(&globalmem_devp[1].cdev);
kfree(globalmem_devp);
unregister_chrdev_region(MKDEV(globalmem_major, 0), 2);
printk(KERN_INFO "globalmem: down.\n");
}
/* 定義參數 */
module_param(globalmem_major, int, S_IRUGO);
module_init(globalmem_init);
module_exit(globalmem_exit);
/* 模塊描述及聲明 */
MODULE_AUTHOR("Archie Xie <[email protected]>");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("A char device module just for demo.");
MODULE_ALIAS("cdev gmem");
MODULE_VERSION("1.0");
插入模塊
insmod globalmem.ko
創建設備節點(後續例程會展示自動創建節點的方法)
cat /proc/devices 找到主設備號major
mknod /dev/globalmem0 c major 0 和 /dev/globalmem1 c major 1
讀寫測試
echo "hello world" > /dev/globalmem
cat /dev/globalmem