宋宝华 《Linux设备驱动开发详解》示例代码之fifo字符设备驱动

本文展示了宋宝华《Linux设备驱动开发详解》中的fifo字符设备驱动代码,包括scull.c、load_scull.sh、unload_scull.sh和test.sh四个部分。在运行测试脚本后,通过echo命令向/dev/scull写入数据,当超过设备缓冲区16B限制时,只能读取,无法再写入。
摘要由CSDN通过智能技术生成

驱动代码如下:

scull.c

#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/gfp.h>
#include <linux/poll.h>
#include <asm/uaccess.h>

#define	SCULL_MAJOR	252
#define SCULL_NAME	"scull"
#define MAX_DATA	0x10

static	int scull_major = SCULL_MAJOR;
struct scull_dev {
	struct cdev cdev;
	unsigned char data[MAX_DATA];
	struct semaphore sem;
	unsigned int current_len;
	wait_queue_head_t r_wait;
	wait_queue_head_t w_wait;
};

MODULE_LICENSE("GPL");
MODULE_AUTHOR("BG2BKK");

struct	scull_dev *scull_devp;

int	scull_open(struct inode *inode, struct file *filp)
{
	struct scull_dev *dev = container_of(inode->i_cdev, struct scull_dev, cdev);
	filp->private_data = dev;
	printk(KERN_ALERT "open the scull device\n");
	return 0;
}

int	scull_release(struct inode *inode, struct file *filp)
{
	printk(KERN_ALERT "close the scull device\n");
	return 0;
}

ssize_t	scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_ops)
{
	struct scull_dev *dev = filp->private_data;
	int ret = 0;
	DECLARE_WAITQUEUE(wait, current);

	down(&dev->sem);
	add_wait_queue(&dev->r_wait, &wait);
	while(dev->current_len == 0)
	{
		if(filp->f_flags & O_NONBLOCK)
		{
			ret = -EAGAIN;
			goto out;
		}
		__set_current_state(TASK_INTERRUPTIBLE);
		up(&de
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值