《Linux Device Drivers》第三章 字元裝置驅動程式——note

來源:互聯網
上載者:User

標籤:io   os   使用   ar   檔案   資料   sp   on   c   

  • 主裝置號和次裝置號
    • 那些名稱被稱為特殊檔案、裝置檔案,或者簡單稱之為檔案系統樹的節點,它們通常位於/dev目錄
    • 通常而言,主裝置號標識裝置對應的驅動程式
    • 一個主裝置號對應一個驅動程式
    • 裝置編號的內部表達
      • dev_t(<linux/types.h>)
      • dev_t是一個32位的數,12位表示主裝置號,其餘20位表示次裝置號
      • <linux/kdev_t.h>
        • MAJOR(dev_t dev);
        • MINOR(dev_t dev);
        • MKDEV(int major, int minor);
    • 分配和釋放裝置編號
      • <linux/fs.h>
        • int register_chrdev_region(dev_t first, unsigned int count, char *name);
        • int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name);
        • void u nregister_chrdev_resion(dev_t first, unsigned int count);
    • 動態分配主裝置號
      • 驅動程式應該始終使用alloc_chrdev_region而不是register_chrdev_region函數
      • 缺點是:由於分配的主裝置號不能保證始終一致,所以無法預先建立裝置節點
      • /proc/devices
      • 分配主裝置號的最佳方式
        • 預設採用動態分配,同時保留在載入甚至是編譯時間指定主裝置號的餘地

  • 一些重要的資料結構
    • 三個重要的核心資料結構
      • file_operations
      • file
      • inode
    • 檔案操作
      • file_operations結構用來將驅動程式操作串連到裝置編號
      • <linux/fs.h>
      • file_operations結構或者指向這類結構的指標稱為fops
        • 每個欄位必須指向驅動程式中實現特定操作的函數
      • struct module *owner
      • loff_t (*llseek) (struct file *, loff_t, int);
      • ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
      • ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
      • ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
      • ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t *);
      • int (*readdir) (struct file *, ,void *, filldir_t);
      • unsigned int (*poll) (struct file *, struct poll_table_struct *);
      • int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
      • int (*mmap) (struct file *, struct vm_area_struct *);
      • int (*open) (struct inode *, struct file *);
      • int (*flush) (struct file *);
      • int (*release) (struct inode *, struct file *);
      • int (*fsync) (struct file *, struct dentry *, int);
      • int (*aio_fsync) (struct kiocb *, int);
      • int (*fasync) (int, struct file *, int);
      • int (*lock) (struct file *, int, struct file_lock *);
      • ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
      • ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
      • ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
      • ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
      • unsigned long (*get_unmapped_area) (struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
      • int (*check_flags) (int)
      • int (*dir_notify) (struct file *, unsigned long);
    • file結構
      • file結構代表一個開啟的檔案
      • <linux/fs.h>
      • 與使用者空間程式中的FILE沒有任何關聯
      • 指向struct file的指標通常被稱為file或filp
      • 欄位
        • mode_t f_mode
        • loff_t f_pos
        • unsigned int f_flags
        • struct file_operations *f_op
        • void *private_data
        • struct dentry *f_dentry
    • inode結構
      • 核心用inode結構在內部表示檔案
      • 對單個檔案,可能會有許多個表示開啟的檔案描述符的file結構
      • 欄位
        • dev_t i_rdev
        • struct cdev *i_cdev
  • 字元裝置的註冊
    • <linux/cdev.h>
    • 擷取一個獨立的cdev結構
      • struct cdev *my_cdev = cdev_alloc();
      • my_cdev->ops = &my_fops;
      • owner
    • struct cdev *cdev_alloc(void);
    • void cdev_init(struct cdev *cdev, struct file_operations *fops);
    • int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
    • void cdev_del(struct cdev *dev);
  • open和release
    • open方法
      • 主要工作
        • 檢查裝置特定的錯誤
        • 如果裝置是首次開啟,則對其進行初始化
        • 如有必要,更新f_op指標
        • 分配並填寫置於flip->private_data裡的資料結構
      • int (*open)(struct inode *inode, struct file *filp);
      • <linux/kernel.h>
        • container_of(pointer, continer_type, container_field);
    • release方法
      • 主要工作
        • 釋放由open分配的、儲存在filp->private_data中的所有內容
        • 在最後一次關閉操作時關閉裝置
  • scull的記憶體使用量
    • <linux/slab.h>
      • void *kmalloc(size_t size, int flags);
      • void kfree(void *ptr);
      • 不應該將非kmalloc返回的指標傳遞給kfree
      • 將NULL指標傳遞給kfree是合法的
  • read和write
    • ssize_t read(struct file *filp, char __user *buff, size_t count, loff_t *offp);
    • ssize_t write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);
    • 核心代碼不能直接引用使用者空間的指標
      • 隨著驅動程式所啟動並執行架構的不同或者核心配置的不同,在核心模式中運行時,使用者空間的指標可能是無效的
      • 即使該指標在核心空間中代表相同的東西,但使用者空間的記憶體是分頁的,而在系統調用被調用時,涉及到的記憶體可能根本不在RAM中
      • 使用者空間的指標由使用者程式提供,該程式可能存在缺陷或者是個惡意程式
    • 訪問使用者空間的緩衝區應始終通過核心提供的專用函數完成
      • <asm/uaccess.h>
      • unsigned long copy_to_user(void __user *to, const void * from, unsigned long count)
      • unsigned long copy_from_user(void *to, const void __user *from, unsigned long count);
    • 訪問使用者空間的任何函數都必須是可重新進入的,必須能和其他驅動程式函數並發執行,必須處於能夠合法休眠的狀態
    • read方法
      • 如果返回值等於傳遞read系統調用的count參數,則說明所請求的位元組數傳輸成功完成了
      • 如果返回值是正的,但比count小,說明部分資料轉送成功
      • 如果返回值為,則表示已經到達了檔案尾
      • 負值意味著發生了錯誤,該值指明了發生了什麼錯誤,錯誤碼在<linux/error.h>中定義
      • 現在還沒有資料,但以後可能會有
    • write方法
    • readv和writev
      • ssize_t (*readv) (struct file * filp, const struct iovec *iov, unsigned long count, loff_t *ppos);
      • ssize_t (*writev) (struct file *filp, const struct iovec *iov, unsigned long count, loff_t *ppos);
      • iovec結構
        • void __user *iov_base;
        • __kernel_size_t iov_len

《Linux Device Drivers》第三章 字元裝置驅動程式——note

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.