char device driver总结

概览:
    第一步:注册设备号                                              信息#tail -f /var/log/message
        注册函数:
            register_chrdev_region() 或                             查看#lsmod
            alloc_chrdev_region()    或                             查看#cat /proc/devices
            register_chrdev()
        注销函数:
            unregist_chrdev_region() 或
            unregister_chrdev()
   
    第二步:初始化cdev并添加到系统
        初始化cdev
            静态初始化 cdev_init() 或
            动态初始化 cdev_alloc()
        添加到系统函数
            cdev_add()
        从系统删除函数
            cdev_del()
           
    第三步:创建设备节点
        创建类
            class_create()          将放于/sysfs                    查看#ls /sys/class
        删除类
            class_destroy()
       
        创建节点
            device_create() 或 class_device_create()  将存放于/dev  查看#ls /dev
        删除节点
            device_destroy() 或 class_device_destroy()
   
    第四步:简单示例

/***************************************************************************************************
                                   第一步:注册设备号
***************************************************************************************************/
Linux内核中所有已分配的字符设备编号都记录在一个名为 chrdevs 散列表里。
    该散列表中的每一个元素是一个 char_device_struct 结构,它的定义如下:

  

static struct char_device_struct
    {
        struct char_device_struct *next;    // 指向散列冲突链表中的下一个元素的指针
        unsigned    int major;              // 主设备号
        unsigned    int baseminor;          // 起始次设备号
        int minorct;                        // 设备编号的范围大小
        char    name[64];                   // 处理该设备编号范围内的设备驱动的名称
        struct file_operations *fops;       // 没有使用
        struct cdev *cdev;                  // 指向字符设备驱动程序描述符的指针
    }*chrdevs[CHRDEV_MAJOR_HASH_SIZE];

  

    1 每一个主设备有一个会分配一个此结构,可以有多个次设备号。次设备是依次递增的。
    2 内核提供了5个函数来来管理字符设备编号。
   
            register_chrdev_region()        指定初始值
            alloc_chrdev_region()           动态分配
            register_chrdev()               指定设备号
            他们都会调用 __register_chrdev_region() 来注册一组设备编号范围(一个char_device_struct结构),我们使用其中一个即可。
           
            unregist_chrdev_region()        释放都用此函数
            unregister_chrdev()             都调用了 __unregister_chrdev_region() 来注销设备

        注册:
               register_chrdev_region(dev_t first,unsigned int count,char *name)
                first :要分配的设备编号范围的初始值(次设备号常设为0);
                count :连续编号范围.
                Name  :编号相关联的设备名称. (/proc/devices);


            int alloc_chrdev_region(dev_t *dev,unsigned int firstminor,unsigned int count,char *name);
                *dev        :存放返回的设备号
                firstminor  :第一个次设备号的号数,常为0;

            int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)
                major :要注册的设备号, 若为0则自动分配一个
                name  :设备名
                *fops :以后再聊

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux Device Driver (3edtion)原版 1. An Introduction to Device Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Role of the Device Driver 2 Splitting the Kernel 4 Classes of Devices and Modules 5 Security Issues 8 Version Numbering 10 License Terms 11 Joining the Kernel Development Community 12 Overview of the Book 12 2. Building and Running Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Setting Up Your Test System 15 The Hello World Module 16 Kernel Modules Versus Applications 18 Compiling and Loading 22 The Kernel Symbol Table 28 Preliminaries 30 Initialization and Shutdown 31 Module Parameters 35 Doing It in User Space 37 Quick Reference 39 3. Char Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The Design of scull 42 Major and Minor Numbers 43 Some Important Data Structures 49 ,ldr3TOC.fm.4587 Page v Thursday, January 20, 2005 9:30 AMvi | Table of Contents Char Device Registration 55 open and release 58 scull’s Memory Usage 60 read and write 63 Playing with the New Devices 70 Quick Reference 70 4. Debugging Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Debugging Support in the Kernel 73 Debugging by Printing 75 Debugging by Querying 82 Debugging by Watching 91 Debugging System Faults 93 Debuggers and Related Tools 99 5. Concurrency and Race Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Pitfalls in scull 107 Concurrency and Its Management 107 Semaphores and Mutexes 109 Completions 114 Spinlocks 116 Locking Traps 121 Alternatives to Locking 123 Quick Reference 130 6. Advanced Char Driver Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 ioctl 135 Blocking I/O 147 poll and select 163 Asynchronous Notification 169 Seeking a Device 171 Access Control on a Device File 173 Quick Reference 179

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值