中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. 新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 嵌入式Linux設備驅動開發(fā)之:實驗內容——test驅動

        嵌入式Linux設備驅動開發(fā)之:實驗內容——test驅動

        作者: 時間:2013-09-13 來源:網(wǎng)絡 收藏

        本文引用地址:http://www.antipu.com.cn/article/257106.htm

        11.7實驗內容——

        1.實驗目的

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

        2.實驗內容

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

        3.實驗步驟

        (1)編寫代碼。

        這個簡單的驅動程序的源代碼如下所示:

        /*test_drv.c*/

        #includelinux/module.h>

        #includelinux/init.h>

        #includelinux/fs.h>

        #includelinux/kernel.h>

        #includelinux/slab.h>

        #includelinux/types.h>

        #includelinux/errno.h>

        #includelinux/cdev.h>

        #includeasm/uaccess.h>

        #defineTEST_DEVICE_NAMEtest_dev

        #defineBUFF_SZ1024

        /*全局變量*/

        staticstructcdevtest_dev;

        unsignedintmajor=0;

        staticchar*data=NULL;

        /*讀函數(shù)*/

        staticssize_ttest_read(structfile*file,

        char*buf,size_tcount,loff_t*f_pos)

        {

        intlen;

        if(count0)

        {

        return-EINVAL;

        }

        len=strlen(data);

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

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

        {

        return-EFAULT;

        }

        returncount;

        }

        /*寫函數(shù)*/

        staticssize_ttest_write(structfile*file,constchar*buffer,

        size_tcount,loff_t*f_pos)

        {

        if(count0)

        {

        return-EINVAL;

        }

        memset(data,0,BUFF_SZ);

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

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

        {

        return-EFAULT;

        }

        returncount;

        }

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

        staticinttest_open(structinode*inode,structfile*file)

        {

        printk(Thisisopenoperationn);

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

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

        if(!data)

        {

        return-ENOMEM;

        }

        memset(data,0,BUFF_SZ);

        return0;

        }

        /*關閉函數(shù)*/

        staticinttest_release(structinode*inode,structfile*file)

        {

        printk(Thisisreleaseoperationn);

        if(data)

        {

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

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

        }

        return0;

        }

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

        staticvoidtest_setup_cdev(structcdev*dev,intminor,

        structfile_operations*fops)

        {

        interr,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_NOTICEError%daddingtest%d,err,minor);

        }

        }

        linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)

        linux相關文章:linux教程



        上一頁 1 2 3 4 下一頁

        評論


        相關推薦

        技術專區(qū)

        關閉