• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天下

            記錄修行的印記

            Linux設(shè)備驅(qū)動(dòng)程序?qū)W習(xí)筆記:(1) 字符設(shè)備驅(qū)動(dòng)入門(mén)1

             

             

            //這是一個(gè)最簡(jiǎn)單的字符設(shè)備驅(qū)動(dòng),入門(mén)專(zhuān)用.
            //scull1.h
            #ifndef _SCULL1_H
            #define _SCULL1_H

            #include 
            <linux/init.h>
            #include 
            <linux/module.h>
            #include 
            <linux/kernel.h>
            #include 
            <linux/types.h>
            #include 
            <linux/fs.h>
            #include 
            <linux/version.h>
            #include 
            <asm/uaccess.h>
            #include 
            <linux/cdev.h>
            #include 
            <linux/mm.h>
            #include 
            <linux/errno.h>


            #define SCULL_MAJOR 0
            #define SCULL_SIZE    0x1000
            #define SCULL_CMD_CLEAR    0x01


            struct scull_dev{
                
            struct cdev cDev;
                
            char   mem[SCULL_SIZE];
            };


            int scull_open(struct inode* inode,struct file* filp);

            ssize_t scull_read(
            struct file *filp, char __user *buf, size_t count,loff_t *f_pos);             

            ssize_t scull_write(
            struct file *filp, const char __user *buf, size_t count,loff_t *f_pos);

            int scull_release(struct inode* inode,struct file* filp);

            #endif






            //scull1.c
            #include "scull1.h"

            MODULE_LICENSE(
            "Dual BSD/GPL");
            MODULE_AUTHOR(
            "Aaron.xu");
            MODULE_DESCRIPTION(
            "hello driver test");
            MODULE_VERSION(
            "0.1");

            static int scull_major = SCULL_MAJOR;
            struct scull_dev mydev;

            struct file_operations scull_fops = 
            {
                .owner     
            = THIS_MODULE,
                .open     
            = scull_open,
                .release 
            = scull_release,
                .read     
            = scull_read,
                .write     
            = scull_write,
            };



            static void scull_setup_cdev(void)
            {
                
            int err;
                dev_t devid 
            = MKDEV(scull_major,0);

                
                cdev_init(
            &mydev.cDev,&scull_fops);
                
                printk(KERN_INFO 
            "&mydev.cDev.ops:%p \n",&mydev.cDev.ops);

                mydev.cDev.owner    
            = THIS_MODULE;
                mydev.cDev.ops        
            = &scull_fops;

                printk(KERN_INFO 
            "&mydev.cDev.ops:%p \n",&mydev.cDev.ops);

                err    
            = cdev_add(&mydev.cDev,devid,1);
                
            if (err!=0)
                {
                    printk(KERN_ERR 
            "cdev_add Error,err:%d \n",err);
                }
            }

            static int scull_init(void)
            {
                
            int err;
                dev_t devid 
            = MKDEV(scull_major,0);

                
            if (scull_major)
                {
                    err 
            = register_chrdev_region(devid,1,"scull1");
                }
                
            else
                {
                    err 
            = alloc_chrdev_region(&devid,0,1,"scull1");
                    scull_major 
            = MAJOR(devid);
                }

                
            if (err !=0 )
                {
                    printk(KERN_ERR 
            "register chrdev region error,err:%d \n",err);
                    
            return err;
                }

                scull_setup_cdev();
                
            return 0;
            }

            static void scull_exit(void)
            {
                cdev_del(
            &mydev.cDev);
                unregister_chrdev_region(MKDEV(scull_major,
            0),1);
            }


            int scull_open(struct inode* inode,struct file* filp)
            {
                filp
            ->private_data = &mydev;
                
            return 0;
            }

            ssize_t scull_read(
            struct file *filp, char __user *buf, size_t count,loff_t *f_pos)               
            {
                unsigned 
            long pos = *f_pos;
                
            int err = 0;
                
            int ret = 0;
                
            struct scull_dev* p_mydev = filp->private_data;

                
            if (pos >= SCULL_SIZE)
                {
                    
            return 0;
                }

                
            if (count > (SCULL_SIZE - pos) )
                {
                    count 
            = SCULL_SIZE - pos;
                }

                err 
            = copy_to_user(buf,p_mydev->mem+pos,count);
                
            if (err !=0 )
                {
                    ret 
            = -EFAULT;
                }
                
            else
                {
                    
            *f_pos += count;
                    ret 
            = count;
                    printk(KERN_INFO 
            "read %d byte(s) from %lu \n",ret,pos);
                }
                
            return ret;
            }

            ssize_t scull_write(
            struct file *filp, const char __user *buf, size_t count,loff_t *f_pos)
            {
                unsigned 
            long pos = *f_pos;
                
            int err = 0;
                
            int ret = 0;
                
            struct scull_dev* p_mydev = filp->private_data;

                
            if (pos >= SCULL_SIZE)
                {
                    
            return 0;
                }

                
            if (count > (SCULL_SIZE - pos) )
                {
                    count 
            = SCULL_SIZE - pos;
                }

                err 
            = copy_from_user(p_mydev->mem+pos,buf,count);
                
            if (err !=0 )
                {
                    ret 
            = -EFAULT;
                }
                
            else
                {
                    
            *f_pos += count;
                    ret 
            = count;
                    printk(KERN_INFO 
            "write %d byte(s) from %lu \n",ret,pos);
                }
                
            return ret;
            }

            int scull_release(struct inode* inode,struct file* filp)
            {
                
            return 0;
            }


            module_init(scull_init);
            module_exit(scull_exit);



            //Makefile
            obj-m    +=scull1.o
            KERNELDIR    :
            = /usr/src/linux-headers-2.6.32-5-686
            PWD    :
            =$(shell pwd)
            .PHONY: test clean all
            all:
                $(MAKE) 
            -C $(KERNELDIR) M=$(PWD) modules
            clean:
                rm 
            -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versionsm *.order *.symvers .tmp_versions

            test:
                insmod .
            /scull1.ko
                rmmod scull1
                dmesg 
            -c



            //創(chuàng)建設(shè)備節(jié)點(diǎn)
            make_dev_node
            #
            !/bin/bash
            DEVICE
            ="scull1"
            MAJOR
            =`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
            cmd
            ="mknod /dev/$DEVICE c $MAJOR 0"
            echo $cmd
            `$cmd`

             

            posted on 2012-11-08 16:04 天下 閱讀(458) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): kernel & Driver

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(4)

            隨筆分類(lèi)(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評(píng)論

            无码人妻精品一区二区三区久久| 久久99国产精品一区二区| 久久精品亚洲男人的天堂| 久久久精品久久久久久 | 性色欲网站人妻丰满中文久久不卡| 久久久久亚洲AV无码专区体验| 国产成人久久久精品二区三区| 日本五月天婷久久网站| 91久久精品视频| 久久久无码精品亚洲日韩按摩 | 激情伊人五月天久久综合| 久久九九久精品国产| 97精品伊人久久大香线蕉app| 久久久久亚洲AV综合波多野结衣 | 亚洲人成精品久久久久 | 久久久这里只有精品加勒比| 久久亚洲国产午夜精品理论片| 亚洲午夜无码AV毛片久久| 久久成人精品视频| 久久久久99精品成人片欧美| 亚洲精品国精品久久99热 | 久久精品亚洲AV久久久无码| 免费一级欧美大片久久网| 欧美一区二区精品久久| 99re久久精品国产首页2020| 中文字幕人妻色偷偷久久 | 久久精品亚洲中文字幕无码麻豆| 亚洲国产精品无码久久青草| 久久久久亚洲AV成人网| 国产99久久久久久免费看| 久久久久久综合一区中文字幕| 久久精品国产亚洲AV无码麻豆| 亚洲熟妇无码另类久久久| 精品多毛少妇人妻AV免费久久| 久久影视国产亚洲| 欧美久久一区二区三区| 亚洲国产成人久久精品99| 午夜精品久久久久久| 国内精品久久久久影院亚洲| 中文字幕久久精品| 欧美一区二区三区久久综|