1. 程式人生 > >OpenWRT(七)字元裝置驅動開發

OpenWRT(七)字元裝置驅動開發

一、字元裝置驅動

我們之前學習過驅動程式的開發,接下來我們接著深入學習字元裝置驅動程式的開發。字元裝置驅動是比較簡單的一種驅動。主要在於實現init、exit、open、read、write等函式。

二、字元裝置驅動例項

1、在package/kernel/資料夾下新建一個chardrv資料夾
2、在chardrv資料夾下新建一個Makefile檔案,內容如下:

#
# Copyright (C) 2008 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information. # include $(TOPDIR)/rules.mk include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=chardrv PKG_RELEASE:=2 include $(INCLUDE_DIR)/package.mk define KernelPackage/chardrv SUBMENU:=Other modules TITLE:=CharDrv FILES:=$(PKG_BUILD_DIR)/chardrv.ko KCONFIG:= endef define KernelPackage
/chardrv/description Kernel module for register chardrv. endef EXTRA_KCONFIG:= \ CONFIG_CHARDRV=m EXTRA_CFLAGS:= \ $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \ $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \ MAKE_OPTS
:= \ ARCH="$(LINUX_KARCH)" \ CROSS_COMPILE="$(TARGET_CROSS)" \ SUBDIRS="$(PKG_BUILD_DIR)" \ EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \ $(EXTRA_KCONFIG) 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,chardrv))

3、在chardrv資料夾下新建一個src資料夾,在src下新建一個Makefile檔案,內容為:

obj-${CONFIG_CHARDRV}  += chardrv.o

4、新建一個chardrv.c的驅動檔案,檔案內容為:

/***************************** 
*
*   字元裝置驅動程式模板
*
*******************************/


#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>


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


//加密函式引數內容: _IOW(IOW_CHAR , IOW_NUMn , IOW_TYPE)
//加密函式用於chardrv_ioctl函式中
//使用舉例:ioctl(fd , _IOW('L',0x80,long) , 0x1);
//#define NUMn chardrv
#define IOW_CHAR 'L'
#define IOW_TYPE  long
#define IOW_NUM1  0x80


//初始化函式必要資源定義
//用於初始化函式當中
//device number;
    //裝置號
    dev_t dev_num;  
//struct dev
    //字元裝置
    struct cdev chardrv_cdev;
//auto "mknode /dev/chardrv c dev_num minor_num"
//自動建立裝置物件
struct class *chardrv_class = NULL;
struct device *chardrv_device = NULL;


/**************** 結構體 file_operations 成員函式 *****************/
//open
static int chardrv_open(struct inode *inode, struct file *file)
{
    printk("chardrv drive open...\n");


    return 0;
}

//close
static int chardrv_close(struct inode *inode , struct file *file)
{
    printk("chardrv drive close...\n");


    return 0;
}

//read
static ssize_t chardrv_read(struct file *file, char __user *buffer,
            size_t len, loff_t *pos)
{
    int ret_v = 0;
    printk("chardrv drive read...\n");


    return ret_v;
}

//write
static ssize_t chardrv_write( struct file *file , const char __user *buffer,
               size_t len , loff_t *offset )
{
    int ret_v = 0;
    printk("chardrv drive write...\n");


    return ret_v;
}

//unlocked_ioctl
static int chardrv_ioctl (struct file *filp , unsigned int cmd , unsigned long arg)
{
    int ret_v = 0;
    printk("chardrv drive ioctl...\n");

    switch(cmd)
    {
        //常規:
        //cmd值自行進行修改
        case 0x1:
        {
            if(arg == 0x1) //第二條件;
            {

            }
        }
        break;

        //帶密碼保護:
        //請在"基本定義"進行必要的定義
        case _IOW(IOW_CHAR,IOW_NUM1,IOW_TYPE):
        {
            if(arg == 0x1) //第二條件
            {

            }

        }
        break;

        default:
            break;
    }

    return ret_v;
}


/***************** 結構體: file_operations ,該結構體將驅動中的函式和應用層函式關聯(例如當呼叫應用層呼叫open函式時就會呼叫驅動中的open函式)************************/
static const struct file_operations chardrv_fops = {
    .owner   = THIS_MODULE,
    .open    = chardrv_open,
    .release = chardrv_close,   
    .read    = chardrv_read,
    .write   = chardrv_write,
    .unlocked_ioctl = chardrv_ioctl,
};



//使用insmod掛載驅動時回撥
static __init int chardrv_init(void)
{
    int ret_v = 0;
    printk("mydrv drive init...\n");
    /*
    函式alloc_chrdev_region主要引數說明:
    引數1: 自動分配的裝置號
    引數2: 次裝置號
    引數3: 建立多少個裝置
    */
    if( ( ret_v = alloc_chrdev_region(&dev_num,0,1,"chardrv") ) < 0 )   //為chardrv動態分配裝置號
    {
        goto dev_reg_error;
    }

    //列印主裝置號和次裝置號
    printk("The drive info of chardrv:\nmajor: %d\nminor: %d\n",
        MAJOR(dev_num),MINOR(dev_num));   

    //關聯裝置和操作函式
    cdev_init(&chardrv_cdev,&chardrv_fops);
    //註冊裝置
    if( (ret_v = cdev_add(&chardrv_cdev,dev_num,1)) != 0 )
    {
        goto cdev_add_error;
    }
    //建立裝置類,用於自動建立裝置
    chardrv_class = class_create(THIS_MODULE,"chardrv");
    if( IS_ERR(chardrv_class) )
    {
        goto class_c_error;
    }
    //通過裝置類建立裝置
    chardrv_device = device_create(chardrv_class,NULL,dev_num,NULL,"chardrv");
    if( IS_ERR(chardrv_device) )
    {
        goto device_c_error;
    }
    printk("auto mknod success!\n");

    //------------   請在此新增您的初始化程式  --------------//




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


    //----------------------  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);
    return ret_v;

class_c_error:
    printk("class_create failed\n");
    cdev_del(&chardrv_cdev);
    unregister_chrdev_region(dev_num, 1);
    return PTR_ERR(chardrv_class);

device_c_error:
    printk("device_create failed\n");
    cdev_del(&chardrv_cdev);
    unregister_chrdev_region(dev_num, 1);
    class_destroy(chardrv_class);
    return PTR_ERR(chardrv_device);

//------------------ 請在此新增您的錯誤處理內容 ----------------//
chardrv_error:




    return -1;
//--------------------          END         -------------------//

init_success:
    printk("chardrv init success!\n");
    return 0;
}

//使用rmmod解除安裝驅動是回撥該函式
static __exit void chardrv_exit(void)
{
    printk("chardrv drive exit...\n");  


    //釋放初始化使用到的資源;
    cdev_del(&chardrv_cdev);
    unregister_chrdev_region(dev_num, 1);
    device_unregister(chardrv_device);
    class_destroy(chardrv_class);

}


/**************** module operations**********************/
//宣告載入函式和解除安裝函式
module_init(chardrv_init);
module_exit(chardrv_exit);

//some infomation
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("YANG");


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

5、最後就是編譯了,和上一節一樣進行編譯就OK了!