LDD3《Linux设备驱动》中的最简单的字符设备驱动实现与测试

源代码如下:
#include <linux/config.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>

#include <linux/cdev.h>
#include <asm/uaccess.h>
//#include "scull.h"

#define SCULL_MAJOR 0   /* dynamic major by default */
#define SCULL_NR_DEVS 4    /* scull0 through scull3 */
#define SCULL_P_NR_DEVS 4  /* scullpipe0 through scullpipe3 */
#define SCULL_QUANTUM 4000
#define SCULL_QSET    1000

struct scull_qset {
	void **data;
	struct scull_qset *next;
};

struct scull_dev {
	struct scull_qset *data;  /* Pointer to first quantum set */
	int quantum;              /* the current quantum size */
	int qset;                 /* the current array size */
	unsigned long size;       /* amount of data stored here */
	unsigned int access_key;  /* used by sculluid and scullpriv */
	struct semaphore sem;     /* mutual exclusion semaphore     */
	struct cdev cdev;	  /* Char device structure		*/
};

int	scull_major = SCULL_MAJOR;
int	scull_minor = 0;
int	scull_nr_devs = SCULL_NR_DEVS;
int	scull_quantum = SCULL_QUANTUM;
int	scull_qset = SCULL_QSET;

module_param(scull_major, int, S_IRUGO);
module_param(scull_minor, int, S_IRUGO);
module_param(scull_nr_devs,int, S_IRUGO);
module_param(scull_quantum, int, S_IRUGO);
module_param(scull_qset,int,S_IRUGO);

MODULE_AUTHOR("BG2BKK");
MODULE_LICENSE("Dual BSD/GPL");


struct scull_dev *scull_devices;

int	scull_trim(struct scull_dev *dev)
{
	struct	scull_qset *next,*dptr;
	int	qset = dev->qset;
	int 	i;
	for(dptr = dev->data; dptr;dptr = next)
	{
		if(dptr->data){
			for (i=0;i<qset;i++)
				kfree(dptr->data[i]);
			kfree(dptr->data);
			dptr->data = NULL;
		}
		next = dptr->next;
		kfree(dptr);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值