加入星計(jì)劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴(kuò)散
  • 作品版權(quán)保護(hù)
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長期合作伙伴
立即加入
  • 正文
    • 11.7  實(shí)驗(yàn)內(nèi)容——test驅(qū)動
  • 相關(guān)推薦
  • 電子產(chǎn)業(yè)圖譜
申請入駐 產(chǎn)業(yè)圖譜

嵌入式Linux設(shè)備驅(qū)動開發(fā)之:實(shí)驗(yàn)內(nèi)容——test驅(qū)動

2013/09/13
1
閱讀需 21 分鐘
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點(diǎn)資訊討論

 

11.7  實(shí)驗(yàn)內(nèi)容——test驅(qū)動

1.實(shí)驗(yàn)?zāi)康?/h4>

該實(shí)驗(yàn)是編寫最簡單的字符驅(qū)動程序,這里的設(shè)備也就是一段內(nèi)存,實(shí)現(xiàn)簡單的讀寫功能,并列出常用格式的Makefile以及驅(qū)動的加載和卸載腳本。讀者可以熟悉字符設(shè)備驅(qū)動的整個(gè)編寫流程。

2.實(shí)驗(yàn)內(nèi)容

該實(shí)驗(yàn)要求實(shí)現(xiàn)對虛擬設(shè)備(一段內(nèi)存)的打開、關(guān)閉、讀寫的操作,并要通過編寫測試程序來測試虛擬設(shè)備及其驅(qū)動運(yùn)行是否正常。

3.實(shí)驗(yàn)步驟

(1)編寫代碼。

這個(gè)簡單的驅(qū)動程序的源代碼如下所示:

/* test_drv.c */

#include <linux/module.h>

#include <linux/init.h>

#include <linux/fs.h>

#include <linux/kernel.h>

#include <linux/slab.h>

#include <linux/types.h>

#include <linux/errno.h>

#include <linux/cdev.h>

#include <asm/uaccess.h>

#define     TEST_DEVICE_NAME    "test_dev"

#define        BUFF_SZ                1024

/*全局變量*/

static struct cdev test_dev;

unsigned int major =0;

static char *data = NULL;

/*讀函數(shù)*/

static ssize_t test_read(struct file *file, 

                                 char *buf, size_t count, loff_t *f_pos)

{

    int len;

    if (count < 0 )

    {

        return -EINVAL;

    }

    len = strlen(data);

    count = (len > count)?count:len;

    if (copy_to_user(buf, data, count)) /* 將內(nèi)核緩沖的數(shù)據(jù)拷貝到用戶空間*/

    {

        return -EFAULT;

    }

    return count;

}

/*寫函數(shù)*/

static ssize_t test_write(struct file *file, const char *buffer, 

                                       size_t count, loff_t *f_pos)

{

    if(count < 0)

    {

        return -EINVAL;

    }

    memset(data, 0, BUFF_SZ);

        count = (BUFF_SZ > count)?count:BUFF_SZ;

    if (copy_from_user(data, buffer, count)) /* 將用戶緩沖的數(shù)據(jù)復(fù)制到內(nèi)核空間*/

    {

        return -EFAULT;

    }

    return count;

}

/*打開函數(shù)*/

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

{

    printk("This is open operationn");

    /* 分配并初始化緩沖區(qū)*/

    data = (char*)kmalloc(sizeof(char) * BUFF_SZ, GFP_KERNEL);

    if (!data)

    {

        return -ENOMEM;

    }

    memset(data, 0, BUFF_SZ); 

    return 0;

}

/*關(guān)閉函數(shù)*/

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

{

    printk("This is release operationn");

    if (data)

    {

        kfree(data); /* 釋放緩沖區(qū)*/

        data = NULL; /* 防止出現(xiàn)野指針 */

    }

    return 0;

}

/* 創(chuàng)建、初始化字符設(shè)備,并且注冊到系統(tǒng)*/

static void test_setup_cdev(struct cdev *dev, int minor,

        struct file_operations *fops)

{

    int err, devno = MKDEV(major, minor);

    cdev_init(dev, fops);

    dev->owner = THIS_MODULE;

    dev->ops = fops;

    err = cdev_add (dev, devno, 1);

    if (err)

    {

        printk (KERN_NOTICE "Error %d adding test %d", err, minor);

    }

}

 

/* 虛擬設(shè)備的file_operations結(jié)構(gòu) */

static struct file_operations test_fops = 

{

    .owner   = THIS_MODULE,

    .read    = test_read,

    .write   = test_write,

    .open    = test_open,

    .release = test_release,

};

/*模塊注冊入口*/

int init_module(void)

{

    int result;

    dev_t dev = MKDEV(major, 0);

    if (major)

    {/* 靜態(tài)注冊一個(gè)設(shè)備,設(shè)備號先前指定好,并設(shè)定設(shè)備名,用cat /proc/devices來查看 */

        result = register_chrdev_region(dev, 1, TEST_DEVICE_NAME);

    }

    else 

    {

        result = alloc_chrdev_region(&dev, 0, 1, TEST_DEVICE_NAME);

    }

    if (result < 0) 

    {

        printk(KERN_WARNING "Test device: unable to get major %dn", major);

        return result;

    }

    test_setup_cdev(&test_dev, 0, &test_fops);

    printk("The major of the test device is %dn", major);

    return 0;

}

/*卸載模塊*/

void cleanup_module(void) 

{

    cdev_del(&test_dev);

    unregister_chrdev_region(MKDEV(major, 0), 1);

    printk("Test device uninstalledn");

}

(2)編譯代碼。

虛擬設(shè)備的驅(qū)動程序的Makefile如下所示:

ifeq ($(KERNELRELEASE),)

KERNELDIR ?= /lib/modules/$(shell uname -r)/build /*內(nèi)核代碼編譯路徑*/

PWD := $(shell pwd)

modules:

    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:

    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:

    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else

    obj-m := test_drv.o    /* 將生成的模塊為test_drv.ko*/

endif

(3)加載和卸載模塊。

通過下面兩個(gè)腳本代碼分別實(shí)現(xiàn)驅(qū)動模塊的加載和卸載。

加載腳本test_drv_load如下所示:

#!/bin/sh

# 驅(qū)動模塊名稱

module="test_drv"    

# 設(shè)備名稱。在/proc/devices中出現(xiàn)

device="test_dev"    

# 設(shè)備文件的屬性

mode="664"            

group="david"        

# 刪除已存在的設(shè)備節(jié)點(diǎn)

rm -f /dev/${device} 

# 加載驅(qū)動模塊

/sbin/insmod -f ./$module.ko $* || exit 1

# 查到創(chuàng)建設(shè)備的主設(shè)備號

major=`cat /proc/devices | awk "\$2=="$device" {print \$1}"`

# 創(chuàng)建設(shè)備文件節(jié)點(diǎn)

mknod /dev/${device} c $major 0

# 設(shè)置設(shè)備文件屬性

chgrp $group /dev/${device}

chmod $mode  /dev/${device}

卸載腳本test_drv_unload如下所示:

#!/bin/sh

module="test_drv"    

device="test_dev"

# 卸載驅(qū)動模塊

/sbin/rmmod $module $* || exit 1

# 刪除設(shè)備文件

rm -f /dev/${device}

exit 0

 

(6)編寫測試代碼。

最后一步是編寫測試代碼,也就是用戶空間的程序,該程序調(diào)用設(shè)備驅(qū)動來測試驅(qū)動的運(yùn)行是否正常。以下實(shí)例只實(shí)現(xiàn)了簡單的讀寫功能,測試代碼如下所示:

/* test.c */

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

#include <fcntl.h>

#define     TEST_DEVICE_FILENAME        "/dev/test_dev"        /* 設(shè)備文件名*/

#define        BUFF_SZ                        1024                /* 緩沖大小 */

int main()

{

    int fd, nwrite, nread;

    char buff[BUFF_SZ];        /*緩沖區(qū)*/

    /* 打開設(shè)備文件 */

    fd = open(TEST_DEVICE_FILENAME, O_RDWR);

    if (fd < 0)

    {

        perror("open");

        exit(1);

    }

        

    do

    {

        printf("Input some words to kernel(enter 'quit' to exit):");

        memset(buff, 0, BUFF_SZ);

        if (fgets(buff, BUFF_SZ, stdin) == NULL)

        {

            perror("fgets");

            break;

        }

        buff[strlen(buff) - 1] = '