简单linux字符设备驱动程序

2023-08-31,

本文代码参考《LINUX设备驱动程序》第三章 字符设备驱动程序
本文中的“字符设备”是一段大小为PAGE_SIZE的内存空间
功能:向字符设备写入字符串;从字符设备读出字符串
代码:
1. scull.c

 #include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <asm/uaccess.h> #include "scull.h" int scull_major = SCULL_MAJOR;
int scull_minor = ; module_param(scull_major, int, S_IRUGO);
module_param(scull_minor, int, S_IRUGO); struct scull_dev *scull_device; int scull_trim(struct scull_dev *dev)
{
if (dev)
{
if (dev->data)
{
kfree(dev->data);
}
dev->data = NULL;
dev->size = ;
}
return ;
} int scull_open(struct inode *inode, struct file *filp)
{
struct scull_dev *dev; dev = container_of(inode->i_cdev, struct scull_dev, cdev);
filp->private_data = dev; if ((filp->f_flags & O_ACCMODE) == O_WRONLY)
{
if (down_interruptible(&dev->sem))
{
return -ERESTARTSYS;
}
scull_trim(dev);
up(&dev->sem);
} return ;
} int scull_release(struct inode *inode, struct file *filp)
{
return ;
} ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
struct scull_dev *dev = filp->private_data;
ssize_t retval = ; if (down_interruptible(&dev->sem))
{
return -ERESTARTSYS;
}
if (*f_pos >= dev->size)
{
goto out;
}
if (*f_pos + count > dev->size)
{
count = dev->size - *f_pos;
} if (!dev->data)
{
goto out;
} if (copy_to_user(buf, dev->data + *f_pos, count))
{
retval = -EFAULT;
goto out;
} *f_pos += count;
retval = count; out:
up(&dev->sem);
return retval;
} ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
struct scull_dev *dev = filp->private_data;
ssize_t retval = -ENOMEM; if (down_interruptible(&dev->sem))
{
return -ERESTARTSYS;
} if (!dev->data)
{
dev->data = kmalloc(SCULL_BUFFER_SIZE, GFP_KERNEL);
if (!dev->data)
{
goto out;
}
memset(dev->data, , SCULL_BUFFER_SIZE);
} if (count > SCULL_BUFFER_SIZE - dev->size)
{
count = SCULL_BUFFER_SIZE - dev->size;
} if (copy_from_user(dev->data + dev->size, buf, count))
{
retval = -EFAULT;
goto out;
} dev->size += count;
retval = count; out:
up(&dev->sem);
return retval;
} loff_t scull_llseek(struct file *filp, loff_t off, int whence)
{
struct scull_dev *dev = filp->private_data;
loff_t newpos; switch(whence)
{
case :
newpos = off;
break;
case :
newpos = filp->f_pos + off;
break;
case :
newpos = dev->size + off;
break;
default:
return -EINVAL;
}
if (newpos < )
{
return -EINVAL;
}
filp->f_pos = newpos;
return newpos;
} struct file_operations scull_fops = {
.owner = THIS_MODULE,
.llseek = scull_llseek,
.read = scull_read,
.write = scull_write,
.open = scull_open,
.release = scull_release,
}; void scull_cleanup_module(void)
{
dev_t devno = MKDEV(scull_major, scull_minor); if (scull_device)
{
scull_trim(scull_device);
cdev_del(&scull_device->cdev);
kfree(scull_device);
}
unregister_chrdev_region(devno, );
} static void scull_setup_cdev(struct scull_dev *dev)
{
int err, devno = MKDEV(scull_major, scull_minor); cdev_init(&dev->cdev, &scull_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &scull_fops;
err = cdev_add(&dev->cdev, devno, ); if (err)
{
printk(KERN_NOTICE "Error %d adding scull", err);
}
} static int __init scull_init_module(void)
{
int result;
dev_t dev = ; if (scull_major)
{
dev = MKDEV(scull_major, scull_minor);
result = register_chrdev_region(dev, , "scull");
}
else
{
result = alloc_chrdev_region(&dev, scull_minor, , "scull");
scull_major = MAJOR(dev);
}
if (result < )
{
printk(KERN_WARNING "scull: can't get major %d\n", scull_major);
return result;
} scull_device = kmalloc(sizeof(struct scull_dev), GFP_KERNEL);
if (!scull_device)
{
result = -ENOMEM;
goto fail;
}
memset(scull_device, , sizeof(struct scull_dev)); init_MUTEX(&scull_device->sem); scull_setup_cdev(scull_device); return ; fail:
scull_cleanup_module();
return result;
} module_init(scull_init_module);
module_exit(scull_cleanup_module); MODULE_LICENSE("GPL");

2. scull.h

 #ifndef _SCULL_H
#define _SCULL_H #define SCULL_MAJOR 0
#define SCULL_BUFFER_SIZE PAGE_SIZE struct scull_dev {
char *data;
unsigned long size;
struct semaphore sem;
struct cdev cdev;
}; #endif

3. Makefile

 obj-m += scull.o

 CURRENT_PATH:=$(shell pwd)
LINUX_KERNEL:=$(shell uname -r)
LINUX_KERNEL_PATH:=/usr/src/kernels/$(LINUX_KERNEL) all:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean

4. 验证效果

1) make

2) insmod scull.ko

3) cat /proc/devices | grep scull    //获取字符设备scull的主设备号,我的是248

4) cd /dev/

5) mknod scull c 248 0

6) echo hello, world > scull    //向字符设备写入“hello, world”

7) cat scull    //读出字符设备中的内容,这里应该显示“hello, world”

简单linux字符设备驱动程序的相关教程结束。

《简单linux字符设备驱动程序.doc》

下载本文的Word格式文档,以方便收藏与打印。