天天看點

字元裝置驅動-2

參考:宋寶華 Linux裝置驅動開發詳解  嵌入式Linux驅動開發教程

開發環境:ubuntu16.04

核心版本:4.13.0-36-generic

主要目的:學習字元裝置的編寫

疑問:

為何cat /dev/globalmem會執行兩次??

file_opration中的函數和open()、 release()、read()、write()調用關系是怎樣的,什麼時候調用到open()的??

1- 代碼

//cdev_1.c檔案
#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 <linux/slab.h> //包含kmalloc()和kfree()
#include<asm/io.h>
#include <asm/switch_to.h> //3.3以後的核心使用這個代替#include <asm/system.h>,shell下使用uname -r檢視核心版本
#include <linux/uaccess.h> //4.1.13為這個

#define GLOBALMEM_SIZE 0x1000 /*操作的mem記憶體大小,全局變量大小4k*/
#define MEM_CLEAR 0x1         /*ioctrl指令碼*/
#define GLOBALMEM_MAJOR 243 /*設定globalmem的主裝置号,檢視已經注冊的避免沖突cat /proc/devices*/

static int globalmem_major = GLOBALMEM_MAJOR;
/*globalmem結構體*/
struct globalmem_dev
{
	struct cdev cdev; //字元裝置結構體
	unsigned char mem[GLOBALMEM_SIZE]; //大小
};

struct globalmem_dev *globalmem_devp;

/*以下為檔案操作:打開、釋放、ioctrl、讀寫、定位*/

/*檔案打開操作*/
int globalmem_open(struct inode *inode, struct file *filp)
{
	filp->private_data = globalmem_devp;
	return 0;	
}

/*檔案釋放*/
int globalmem_release(struct inode *inode, struct file *filp)
{
	return 0;
}

/*ioctrl操作*/
/*
在新的核心中使用long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
或者long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
書中使用的老核心,用的int globalmem_ioctl(struct inode *inodep,...)
新核心改為long 以及去掉第一個inode參數
*/
static long globalmem_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)//和2.6核心不一樣!!
{
	struct globalmem_dev *dev = filp->private_data;
	
	switch(cmd)
	{
		case MEM_CLEAR:
		memset(dev->mem,0,GLOBALMEM_SIZE);
		printk(KERN_INFO "globalmem is set to zero\n");
		break;
		
		default:
		return -EINVAL;
	}
	
	return 0;
}

/*讀函數*/
static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
	int ret =0;
	unsigned long p = *ppos;
	unsigned int count = size;
	struct globalmem_dev *dev = filp->private_data;
	
	/*參數檢查*/
	if(p >= GLOBALMEM_SIZE)
		return count ? -ENXIO : 0;
	if(count > GLOBALMEM_SIZE - p)
		count = GLOBALMEM_SIZE - p;
	
	/*資料拷貝*/
	if(copy_to_user(buf,(dev->mem +p),count)) //copy_to_user(buff, dev->mem + p, count)
	{
		ret = -EFAULT;
	}
	else
	{
		*ppos += count; //更新位置、傳回讀到位元組數
		ret = count; 
		printk(KERN_INFO "read %u bytes from %lu\n", count,p);
	}	
	
	return ret;	
}


/*寫函數*/
static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
	int ret =0;
	unsigned long p = *ppos;
	unsigned int count = size;
	struct globalmem_dev *dev = filp->private_data;
		
	/*參數檢查*/
	if(p >= GLOBALMEM_SIZE)
		return count ? -ENXIO : 0;
	if(count > GLOBALMEM_SIZE - p)
		count = GLOBALMEM_SIZE - p;
	
	/*資料拷貝*/
	if(copy_from_user((dev->mem +p),buf,count)) //copy_to_user(buff, dev->mem + p, count)
	{
		ret = -EFAULT;
	}
	else
	{
		*ppos += count; //更新位置、傳回讀到位元組數
		ret = count; 
		printk(KERN_INFO "write %u bytes from %lu\n", count,p);
	}	
	
	return ret;	
}

/*檔案定位*/
static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig)
{
	loff_t ret = 0;
	
	switch(orig)
	{
		/*從檔案開頭偏移*/
		case 0: 
		if(offset < 0)
		{
			ret = -EINVAL;
			break;
		}
		if((unsigned int)offset > GLOBALMEM_SIZE)
		{
			ret = -EINVAL;
			break;
		}
		filp->f_pos = (unsigned int )offset;
		ret = filp->f_pos;
		break;
		
		/*從目前位置偏移*/
		case 1:
		if(filp->f_pos + offset < 0)
		{
			ret = -EINVAL;
			break;
		}
		if((filp->f_pos + offset) > GLOBALMEM_SIZE)
		{
			ret = -EINVAL;
			break;
		}
		filp->f_pos += (unsigned int )offset;
		ret = filp->f_pos;
		break;
		
		default:
		ret = -EINVAL;			
		break;
	}	

	return ret;	
}

/*檔案操作結構體*/
static const struct file_operations globalmem_fops = 
{
	.owner = THIS_MODULE,
	.llseek = globalmem_llseek,
	.read = globalmem_read,
	.write = globalmem_write,
	.unlocked_ioctl = globalmem_ioctl,
	.open = globalmem_open,
	.release = globalmem_release,
};

/*初始化并向核心注冊cdev*/
static void globalmem_setup_cdev(struct globalmem_dev *dev, int index)
{
	int err;
	int devno = MKDEV(globalmem_major,index);
	
	cdev_init(&dev->cdev,&globalmem_fops); //初始化cdev
	dev->cdev.ops = &globalmem_fops;
	err = cdev_add(&dev->cdev,devno,1);//向核心注冊cdev
	if(err)
	{
		printk(KERN_NOTICE "Err %d adding LED%d",err,index);		
	}	
}


/*裝置驅動子產品加載*/
int globalmem_init(void)
{
	int result;	
	dev_t devno = MKDEV(globalmem_major,0);//如果有主裝置号,先求出裝置号,向系統注冊
	
	if(globalmem_major)
	{
		result = register_chrdev_region(devno,1,"globalmem"); //向核心靜态申請裝置号,裝置号devno已知
	}
	else
	{
		result = alloc_chrdev_region(&devno,0,1,"globalmem");//動态申請裝置号,
		globalmem_major = MAJOR(devno);//根據申請到的裝置号求出主裝置号
	}	
	if(result < 0)
	{
		return result;
	}
	
	/*申請記憶體*/
	globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL);
	if(!globalmem_devp)
	{
		result = -ENOMEM;
		goto fail_malloc;
	}
	memset(globalmem_devp,0,sizeof(struct globalmem_dev));
	
	globalmem_setup_cdev(globalmem_devp,0);//向核心注冊cdev
	return 0;
	
fail_malloc:unregister_chrdev_region(devno,1);//解注冊申請的裝置号
	return result;
	
}

/*子產品解除安裝*/
void globalmem_exit(void)
{
	cdev_del(&globalmem_devp->cdev);//解注冊cdev
	kfree(globalmem_devp);
	unregister_chrdev_region(MKDEV(globalmem_major,0), 1);
}

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

module_param(globalmem_major, int, S_IRUGO);

module_init(globalmem_init);
module_exit(globalmem_exit);
           

2- Makefile檔案

ifneq  ($(KERNELRELEASE),)
obj-m:=cdev_1.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
all:
	make -C $(KDIR) M=$(PWD) modules
clean:
	rm -f *.ko *.o *.symvers *.cmd *.cmd.o
endif
           

3- 驗證

a- 打開一個dmesg監視終端

watch "dmesg |tail -20"

b- 将子產品加載進核心

insmode globalmem.ko  //将子產品編進核心

cat /proc/devices |grep globalmem //檢視是否編進了核心

c- 建立裝置節點

mknod /dev/globalmem c 243 0

mknode用法詳見文章https://blog.csdn.net/a1010256340/article/details/83088870

ls /dev |grep globalmem //檢視是否建立建立了該裝置節點

d- 讀寫裝置節點

echo "hello world" > /dev/globalmem  //調用write()函數,向裝置節點寫,dmesg監視視窗有列印資訊

cat /dev/globalmem   //讀裝置節點,調用read()函數,dmesg監視視窗有列印資訊。有如下列印,是正常情況(但是還不了解)

詳見文章分析:https://blog.csdn.net/qiaoliang328/article/details/4874238

字元裝置驅動-2

4- 遇到問題

a-  asm/system.h頭檔案新核心變更為#include <asm/switch_to.h>

編譯提示: asm/system.h: No such file or directory

使用uname -r檢視核心版本,版本大于3.3使用#include <asm/switch_to.h>替換#include <asm/system.h頭>

b- kmalloc未包含頭檔案

編譯提示:‘kmalloc’ [-Werror=implicit-function-declaration]

包含頭檔案:#include <linux/slab.h>

c- ioctl新老核心定義不一緻

編譯提示:

initialization from incompatible pointer type [-Werror=incompatible-pointer-types]

 initialization from incompatible pointer type

在新的核心中使用long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);

或者long (*compat_ioctl) (struct file *, unsigned int, unsigned long);

書中使用的老核心,用的int globalmem_ioctl(struct inode *inodep,struct file *, unsigned int, unsigned long)

新核心改為long 以及去掉第一個inode參數

5- 總結

學習了簡單字元裝置相關的資料結構及案例編寫

有些東西可能還了解不了,先學會寫,寫多了就了解了。

自己動手,豐衣足食!