Linux设备驱动程序(第三版)第三章修正过的示例驱动程序

Linux设备驱动程序(第三版) 作者科波特(Corbet, j.)书本的示例代码比较零乱,而且网站上给的示例代码是旧版,且没有单独成章。学习起来比较困难,所以笔者分享一下在学习过程中调试过的模块,供有需要的读者参阅。由于侧重书本每章的知识,所以模块仅反映每章的知识点,不是一个全面的驱动模块,但对学习驱动的编写可能比看书本本身网站提供的代码会更有侧重点。希望能够交流学习。也可以通过 https://gitee.com/sanzhouzi/ldd3ex.git 下载

第三章主要是编写一个“完整”的字符设备驱动程序,但不是一个能用的驱动程序。仅作为说明编写一个驱动模块需要的基本要求。缺乏代码注释,请结合书本查看。如下:

驱动模块:

scull驱动模块主函数:main.c

#include 
#include 
#include 
#include 
#include 

#include "scull.h"

static int scull_major = SCULL_MAJOR;
static int scull_minor = SCULL_MINOR;
static int scull_nr_devs = SCULL_NR_DEVS;
static int scull_quantum = SCULL_QUANTUM;
static int scull_qset = SCULL_QSET;
static dev_t dev;
static struct scull_dev *sculldev;

module_param(scull_major, 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);

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);
	}

	dev->size = 0;
	dev->quantum = scull_quantum;
	dev->qset = scull_qset;
	dev->data = NULL;
	return 0;
}

static struct scull_qset *scull_follow(struct scull_dev *dev, int n)
{	
	while(n--) {
		if (!dev->data->next) {
			dev->data->next = 
				kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
			memset(dev->data->next, 0, sizeof(struct scull_qset));
		}
		dev->data = dev->data->next;
		continue;
	}
	return dev->data;
}

static 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) {
		scull_trim(dev);
		dev->data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
		memset(dev->data, 0, sizeof(struct scull_qset));
	}
	return 0;
}

static int scull_release(struct inode *inode, struct file *filp)
{
	return 0;
}

static ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
	struct scull_dev *dev = filp->private_data;
	struct scull_qset *dptr;
	int quantum = dev->quantum, qset = dev->qset;
	int itemsize = quantum * qset;
	int item, s_pos, q_pos, rest;
	ssize_t retval = 0;
	
	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;

	item = (long)*f_pos / itemsize;
	rest = (long)*f_pos %itemsize;
	s_pos = rest / quantum; q_pos = rest % quantum;

	dptr = scull_follow(dev, item);

	if (dptr == NULL || !dptr->data ||!dptr->data[s_pos])
		goto out;

	if (count > quantum - q_pos)
		count = quantum - q_pos;

	if (copy_to_user(buf, dptr->data[s_pos] + q_pos, count)) {
		retval = -EFAULT;
		goto out;
	}
	printk(KERN_NOTICE "%s :count = %lu\n", __func__, (long unsigned int)count);
	*f_pos += count;
	retval = count;
	out:
		up(&dev->sem);
		return retval;
}

static 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;
	struct scull_qset *dptr;
	int quantum = dev->quantum, qset = dev->qset;
	int itemsize = quantum * qset;
	int item, s_pos, q_pos, rest;
	ssize_t retval = -ENOMEM;

	if (down_interruptible(&dev->sem))
		return -ERESTARTSYS;
	item = (long)*f_pos / itemsize;
	rest = (long)*f_pos % itemsize;
	s_pos = rest / quantum;
	q_pos = rest % quantum;

	dptr = scull_follow(dev, item);

	if (dptr == NULL)
		goto out;

	if (!dptr->data) {
		dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
		if (!dptr->data)
			goto out;
		memset(dptr->data, 0, qset * sizeof(char *));
	}

	if (!dptr->data[s_pos]) {
		dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
		if (!dptr->data[s_pos])
			goto out;
	}

	if (count > quantum - q_pos)
		count = quantum - q_pos;

	if (copy_from_user(dptr->data[s_pos] + q_pos, buf, count)) {
		retval = -EFAULT;
		goto out;
	}

	*f_pos += count;
	retval = count;

	if (dev->size < *f_pos)
		dev->size = *f_pos;
	printk(KERN_NOTICE "%s: size = %lu, write count = %lu\n", __func__, 
		(long unsigned int)dev->size, (long unsigned int)count);
	out:
		up(&dev->sem);
		return retval;
	
}

static struct file_operations scull_fops = {
	.owner = THIS_MODULE,
	
	//.llseek = scull_llseek,
	.read = scull_read,
	.write = scull_write,
	//.ioctl = scull_ioctl,
	.open = scull_open,
	.release = scull_release,
	
};

/*
* Assigned dev number.
*/
static int scull_register_chrdev(void)
{
	int result;
	if (scull_major) {
		dev = MKDEV(scull_major, scull_minor);
		result = register_chrdev_region(dev, scull_nr_devs, "scull");
	} else {
		result = alloc_chrdev_region(&dev, scull_minor, scull_nr_devs, 
					     "scull");
		scull_major = MAJOR(dev);
	}
	
	return result;
}

/*
* Scull character device registration.
*/
static void scull_setup_cdev(struct scull_dev *dev, int index)
{
	int err, devno = MKDEV(scull_major, scull_minor + index);
	cdev_init(&dev->cdev, &scull_fops);
	dev->cdev.owner = THIS_MODULE;
	dev->cdev.ops = &scull_fops;
	err = cdev_add(&dev->cdev, devno, 1);
	if (err)
		printk(KERN_WARNING "Error %d adding scull%d", err, index);
}


static void scull_exit(void)
{
	int i;	
	if (sculldev) {
		for (i = 0; i < scull_nr_devs; i++) {
			scull_trim(&sculldev[i]);
			cdev_del(&sculldev[i].cdev);
		}
		kfree(sculldev);
	}

	unregister_chrdev_region(dev, scull_nr_devs);
}


static int __init scull_init(void)
{
	int result = 0;
	int i;
	result = scull_register_chrdev();
	if (result < 0) {
		printk(KERN_WARNING "scull: can't get major %d\n", scull_major);
		return result;
	}
	printk(KERN_NOTICE "%s:Get dev major number = %d\n", 
				__func__, scull_major);

	sculldev = kmalloc(scull_nr_devs * sizeof(struct scull_dev), GFP_KERNEL);
	if (!sculldev) {
		result = -ENOMEM;
		goto fail;
	}

	for (i = 0; i < scull_nr_devs; i++) {

		sculldev[i].data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
		memset(sculldev[i].data, 0, sizeof(struct scull_qset));
		sculldev[i].quantum = scull_quantum;
		sculldev[i].qset = scull_qset;
		sculldev[i].size = 0;
		sculldev[i].access_key = 0;
		sema_init(&sculldev[i].sem, 1);
		scull_setup_cdev(&sculldev[i], i);
	}

fail:
	if (result)
		scull_exit();	
	return result;
	
}

module_init(scull_init);
module_exit(scull_exit);

MODULE_LICENSE("GPL");
MODULE_VERSION("v1.0");
MODULE_AUTHOR("LiuGuiChao");
MODULE_DESCRIPTION("A test moudle!");

驱动模块头文件:scull.h

#ifndef __SCULL_H_
#define __SCULL_H_

#include 

#define SCULL_MAJOR 0
#define SCULL_MINOR 0
#define SCULL_NR_DEVS 4

#define SCULL_QUANTUM 4000
#define SCULL_QSET 1000


/*
*book ldd3 page60
*/

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

struct scull_dev {
	struct scull_qset *data;
	int quantum;
	int qset;
	unsigned long size;
	unsigned int access_key;
	struct semaphore sem;
	struct cdev cdev;
};


#endif /*__SCULL_H_*/

Makefile:

	obj-m := scull.o
	scull-objs := main.o

模块编译:

make -C linux内核源码树路径 M=`pwd` modules


模块安装脚本(请视情况修改):

#!/bin/sh
module="scull"
device="scull"
param1="scull_major"

cp /home/plg/$module.ko /lib/modules/3.5.0-FriendlyARM-g042a5377-dirty

rm -f /dev/${device}[0-3]

oldmodule=$(awk "\$2==\"$module\" {print \$2}" /proc/devices)
echo oldmoudle = $oldmodule

if [ $oldmodule == $module ]; then
    echo oldmoudle = $oldmodule
    modprobe -r $module
fi

if [ $1 ]; then
    echo modprobe $module $param1=$1
    modprobe $module $param1=$1
else
    echo modprobe $module
    modprobe $module
fi

major=$(awk "\$2==\"$module\" {print \$1}" /proc/devices)
echo major = $major
mknod /dev/${device}0 c $major 0 
mknod /dev/${device}1 c $major 1
mknod /dev/${device}2 c $major 2
mknod /dev/${device}3 c $major 3

测试代码:

#include 
#include 
#include 
#include 
#include 


#define DATASIZE  94
#define COUNT 100

int main(void)
{
	int fd;
	int i, j;
	unsigned long len;

	char *data = (char*)malloc(DATASIZE + 1);
	if (!data) {
		printf("malloc err! %m\n");
		exit(-1);
	}
	memset(data, 0, sizeof(char ) * DATASIZE + 1);

	for (i = 0; i < DATASIZE; i++) {
		data[i] = (char)i + '!';
	}
	
	fd = open("/dev/scull0", O_WRONLY);
	if (fd == -1) {
		printf("open err! %m\n");
		exit(-1);
	}

	printf("open success! fd = %d\n", fd);

	for (i = 0; i < COUNT; i++) {
		len = write(fd, data, DATASIZE);
		if (len == -1) {
			printf("write err! %m\n");
		} else {
			printf("scull_app: write len = %ld\n", len);		
		}
	}

	close(fd);
	
	fd = open("/dev/scull0", O_RDONLY);
	if (fd == -1) {
		printf("open err! %m\n");
		exit(-1);
	}
	printf("open success! fd = %d\n", fd);

	for (i = 0; i < DATASIZE; i++) {
		memset(data, 0, sizeof(char ) * DATASIZE + 1);
		len = 0;
		len = read(fd, data, DATASIZE - i);
		if (len == -1) {
			printf("read err! %m\n");
		} else {
			printf("scull_app: read[%d] len = %ld\ndata = ", i + 1, len);
			for (j = 0; j < DATASIZE; j++)
				printf("%c", data[j]);
			printf("\n");
		}
	}

	close(fd);
	
	return 0;
}

 

你可能感兴趣的:(Linux,linux,驱动程序,内核,嵌入式)