2019-11-29(openwrt系統(tǒng)驅(qū)動開發(fā)B)

在/package/kernel目錄下新建一個目錄mydrv,下面有一個Makefile和一個src目錄

在src目下下有三個文件,Kconfig,Makefile,mydrv.c?

下面分別介紹每一個文件

1. 第一個Makefile

include $(TOPDIR)/rules.mk

include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=mydrv

PKG_RELEASE:=1

include $(INCLUDE_DIR)/package.mk

define KernelPackage/mydrv

? SUBMENU:=Other modules

? TITLE:=mydrv

? FILES:=$(PKG_BUILD_DIR)/mydrv.ko

? KCONFIG:=

endef

define KernelPackage/mydrv/description

? This is a mydrv drivers

endef

MAKE_OPTS:= \

?? ?ARCH="$(LINUX_KARCH)" \

?? ?CROSS_COMPILE="$(TARGET_CROSS)" \

?? ?SUBDIRS="$(PKG_BUILD_DIR)"

define Build/Prepare

?? ?mkdir -p $(PKG_BUILD_DIR)

?? ?$(CP) ./src/* $(PKG_BUILD_DIR)/

endef

define Build/Compile

?? ?$(MAKE) -C "$(LINUX_DIR)" \

?? ?$(MAKE_OPTS) modules

endef

$(eval $(call KernelPackage,mydrv))

2. src下的Kconfig

config MYDRV

? ? tristate "Just a mydrv"

? ? help

? ? This is a example, for debugging kernel model.

? ? If unsure, say N.

3. src下的Makefile

?obj-m += mydrv.o

4. src下的源碼文件mydrv.c

/*****************************?

*

* ? 驅(qū)動程序模板

* ? 版本:V1

* ? 使用方法(末行模式下):

* ? :%s/mydrv/"你的驅(qū)動名稱"/g

*

*******************************/

#include <linux/mm.h>

#include <linux/miscdevice.h>

#include <linux/slab.h>

#include <linux/vmalloc.h>

#include <linux/mman.h>

#include <linux/random.h>

#include <linux/init.h>

#include <linux/raw.h>

#include <linux/tty.h>

#include <linux/capability.h>

#include <linux/ptrace.h>

#include <linux/device.h>

#include <linux/highmem.h>

#include <linux/crash_dump.h>

#include <linux/backing-dev.h>

#include <linux/bootmem.h>

#include <linux/splice.h>

#include <linux/pfn.h>

#include <linux/export.h>

#include <linux/io.h>

#include <linux/aio.h>

#include <linux/kernel.h>

#include <linux/module.h>

#include <asm/uaccess.h>

#include <linux/ioctl.h>

#define MYLEDS_LED1_ON ?0

#define MYLEDS_LED1_OFF 1

volatile unsigned long *GPIOMODE;

volatile unsigned long *GPIO11_DIR;

volatile unsigned long *GPIO11_SETDATA;

volatile unsigned long *GPIO11_RESETDATA;

/**************** ?基本定義 **********************/

//內(nèi)核空間緩沖區(qū)定義

#if 0

?? ?#define KB_MAX_SIZE 20

?? ?#define kbuf[KB_MAX_SIZE];

#endif

//加密函數(shù)參數(shù)內(nèi)容: _IOW(IOW_CHAR , IOW_NUMn , IOW_TYPE)

//加密函數(shù)用于mydrv_ioctl函數(shù)中

//使用舉例:ioctl(fd , _IOW('L',0x80,long) , 0x1);

//#define NUMn mydrv , if you need!

#define IOW_CHAR 'L'

#define IOW_TYPE ?long

#define IOW_NUM1 ?0x80

//初始化函數(shù)必要資源定義

//用于初始化函數(shù)當中

//device number;

?? ?dev_t dev_num;

//struct dev

?? ?struct cdev mydrv_cdev;

//auto "mknode /dev/mydrv c dev_num minor_num"

struct class *mydrv_class = NULL;

struct device *mydrv_device = NULL;

/**************** 結(jié)構(gòu)體 file_operations 成員函數(shù) *****************/

/*======================================================================

======================================================================*/

//open

static int mydrv_open(struct inode *inode, struct file *file)

{

?? ?printk("mydrv drive open...\n");

?? ?*GPIO11_RESETDATA |=(1u<<11);

?? ?return 0;

}

/*======================================================================

======================================================================*/

//close

static int mydrv_close(struct inode *inode , struct file *file)

{

?? ?printk("mydrv drive close...\n");

?? ?return 0;

}

/*======================================================================

======================================================================*/

//read

static ssize_t mydrv_read(struct file *file, char __user *buffer,

?? ??? ??? ?size_t len, loff_t *pos)

{

?? ?int ret_v = 0;

?? ?printk("mydrv drive read...\n");

?? ?return ret_v;

}

/*======================================================================

======================================================================*/

//write

static ssize_t mydrv_write( struct file *file , const char __user *buffer,

?? ??? ??? ? ? size_t len , loff_t *offset )

{

?? ?int ret_v = 0;

?? ?printk("mydrv drive write...\n");

?? ?return ret_v;

}

/*======================================================================

======================================================================*/

//unlocked_ioctl

static int mydrv_ioctl (struct file *filp , unsigned int cmd , unsigned long arg)

{

?? ?int ret_v = 0;

?? ?printk("mydrv drive ioctl...\n");

?? ?switch(cmd)

?? ?{

?? ??? ?//常規(guī):

?? ??? ?//cmd值自行進行修改

?? ??? ? case MYLEDS_LED1_ON:

?? ??? ? ? ? ?*GPIO11_RESETDATA |= (1u<<11);

?? ??? ? break;

?? ??? ? case MYLEDS_LED1_OFF:

? ? ? ? ? ? ?*GPIO11_SETDATA |= (1u<<11);

?? ??? ? break;

?? ??? ?//帶密碼保護:

?? ??? ?//請在"基本定義"進行必要的定義

?? ??? ?case _IOW(IOW_CHAR,IOW_NUM1,IOW_TYPE):

?? ??? ?{

?? ??? ??? ?if(arg == 0x1) //第二條件

?? ??? ??? ?{


?? ??? ??? ?}

?? ??? ?}

?? ??? ?break;

?? ??? ?default:

?? ??? ??? ?break;

?? ?}

?? ?return ret_v;

}

/***************** 結(jié)構(gòu)體: file_operations ************************/

/*======================================================================

//struct

======================================================================*/

static const struct file_operations mydrv_fops = {

?? ?.owner ? = THIS_MODULE,

?? ?.open?? ? = mydrv_open,

?? ?.release = mydrv_close,?? ?

?? ?.read?? ? = mydrv_read,

?? ?.write ? = mydrv_write,

?? ?.unlocked_ioctl?? ?= mydrv_ioctl,

};

/************* ?functions: init , exit*******************/

//條件值變量,用于指示資源是否正常使用

unsigned char init_flag = 0;

unsigned char add_code_flag = 0;

/*======================================================================

//init

======================================================================*/

static __init int mydrv_init(void)

{

?? ?int ret_v = 0;

?? ?printk("mydrv drive init...\n");

?? ?//函數(shù)alloc_chrdev_region主要參數(shù)說明:

?? ?//參數(shù)2: 次設備號

?? ?//參數(shù)3: 創(chuàng)建多少個設備

?? ?if( ( ret_v = alloc_chrdev_region(&dev_num,0,1,"mydrv") ) < 0 )

?? ?{

?? ??? ?goto dev_reg_error;

?? ?}

?? ?init_flag = 1; //標示設備創(chuàng)建成功;

?? ?printk("The drive info of mydrv:\nmajor: %d\nminor: %d\n",

?? ??? ?MAJOR(dev_num),MINOR(dev_num));

?? ?cdev_init(&mydrv_cdev,&mydrv_fops);

?? ?if( (ret_v = cdev_add(&mydrv_cdev,dev_num,1)) != 0 )

?? ?{

?? ??? ?goto cdev_add_error;

?? ?}

?? ?mydrv_class = class_create(THIS_MODULE,"mydrv");

?? ?if( IS_ERR(mydrv_class) )

?? ?{

?? ??? ?goto class_c_error;

?? ?}

?? ?mydrv_device = device_create(mydrv_class,NULL,dev_num,NULL,"mydrv");

?? ?if( IS_ERR(mydrv_device) )

?? ?{

?? ??? ?goto device_c_error;

?? ?}

?? ?printk("auto mknod success!\n");

?? ?//------------ ? 請在此添加您的初始化程序 ?--------------//??

?? ?//GPIOMODE=(volatile unsigned long *)ioremap(0x10000060,4);//gpio 復用時需要配置

? ? GPIO11_DIR=(volatile unsigned long *)ioremap(0x10000600,4);

?? ?GPIO11_SETDATA=(volatile unsigned long *)ioremap(0x10000630,4);

? ? GPIO11_RESETDATA=(volatile unsigned long *)ioremap(0x10000640,4);? ?

?? ?//*GPIOMODE |= (1u<<11);

?? ?*GPIO11_DIR |= (1u<<11);

? ? ? ? //如果需要做錯誤處理,請:goto mydrv_error;?? ?

?? ? add_code_flag = 1;

?? ?//---------------------- ?END ?---------------------------//?

?? ?goto init_success;

dev_reg_error:

?? ?printk("alloc_chrdev_region failed\n");?? ?

?? ?return ret_v;

cdev_add_error:

?? ?printk("cdev_add failed\n");

??? ?unregister_chrdev_region(dev_num, 1);

?? ?init_flag = 0;

?? ?return ret_v;

class_c_error:

?? ?printk("class_create failed\n");

?? ?cdev_del(&mydrv_cdev);

??? ?unregister_chrdev_region(dev_num, 1);

?? ?init_flag = 0;

?? ?return PTR_ERR(mydrv_class);

device_c_error:

?? ?printk("device_create failed\n");

?? ?cdev_del(&mydrv_cdev);

??? ?unregister_chrdev_region(dev_num, 1);

?? ?class_destroy(mydrv_class);

?? ?init_flag = 0;

?? ?return PTR_ERR(mydrv_device);

//------------------ 請在此添加您的錯誤處理內(nèi)容 ----------------//

mydrv_error:

?? ?printk("mydrv drive close...\n");

?? ?add_code_flag = 0;

?? ?return -1;

//-------------------- ? ? ? ? ?END ? ? ? ? -------------------//?

init_success:

?? ?printk("mydrv init success!\n");

?? ?return 0;

}

/*======================================================================

//exit

======================================================================*/

static __exit void mydrv_exit(void)

{

?? ?printk("mydrv drive exit...\n");?? ?

?? ?if(add_code_flag == 1)

??? ?{ ??

? ? ? ? ? ?//---------- ? 請在這里釋放您的程序占有的資源 ? ---------//

?? ? ? ?printk("free your resources...\n");?? ? ? ? ? ? ? ? ??

?? ? ?? ? //iounmap(GPIOMODE);

?? ??? ? iounmap(GPIO11_DIR);

?? ??? ? iounmap(GPIO11_SETDATA);

?? ??? ? iounmap(GPIO11_RESETDATA);

?? ? ? ?printk("free finish\n");?? ??? ? ? ? ? ? ? ? ??

?? ? ? ?//---------------------- ? ? END ? ? ?-------------------//

?? ?}?? ??? ??? ??? ??? ? ? ? ? ? ? ?

?? ?if(init_flag == 1)

?? ?{

?? ??? ?//釋放初始化使用到的資源;

?? ??? ?cdev_del(&mydrv_cdev);

??? ??? ?unregister_chrdev_region(dev_num, 1);

?? ??? ?device_unregister(mydrv_device);

?? ??? ?class_destroy(mydrv_class);

?? ?}

}

/**************** module operations**********************/

//module loading

module_init(mydrv_init);

module_exit(mydrv_exit);

//some infomation

MODULE_LICENSE("GPL v2");

MODULE_AUTHOR("from Jafy");

MODULE_DESCRIPTION("mydrv drive");

/********************* ?The End ***************************/


make menuconfig 下kernel 選擇Other ,選擇mydrv

保存,

make? package/kernel/mydrv/compile V=s

或者直接

make -j1 V=s 編譯,然后把mydrv.ko 文件copy到開發(fā)板/tmp目錄下,運行

insmod mydrv.ko?

顯示驅(qū)動注冊成功,類注冊成功

可以在/dev 以及/sys/class 目錄下查看

————————————————

版權(quán)聲明:本文為CSDN博主「forAllforMe」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/cym_anhui/article/details/87915841

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容