LDD3 讀書筆記 之 第 3 章 字元驅動

來源:互聯網
上載者:User

   本章介紹了下面符號和標頭檔. struct file_operations 和 struct file 中的成員的列表這裡不重複了.

#include <linux/types.h>
dev_t

dev_t 是用來在核心裡代表裝置號的類型.

int MAJOR(dev_t dev);
int MINOR(dev_t dev);

從裝置編號中抽取主次編號的宏.

dev_t MKDEV(unsigned int major, unsigned int minor);

從主次編號來建立 dev_t 資料項目的宏定義.

#include <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 unregister_chrdev_region(dev_t first, unsigned int count);

允許驅動分配和釋放裝置編號的範圍的函數. register_chrdev_region 應當用在事Crowdsourced Security Testing道需要的主編號時; 對於動態分配, 使用 alloc_chrdev_region 代替.

int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);

老的( 2.6 之前) 字元裝置註冊函數. 它在 2.6 核心中被類比, 但是不應當給新代碼使用. 如果主編號不是 0, 可以不變地用它; 否則一個動態編號被分配給這個裝置.

int unregister_chrdev(unsigned int major, const char *name);

恢複一個由 register_chrdev 所作的註冊的函數. major 和 name 字串必須包含之前用來註冊裝置時同樣的值.

struct file_operations;
struct file;
struct inode;

大部分裝置驅動使用的 3 個重要資料結構. file_operations 結構持有一個字元驅動的方法; struct file 代表一個開啟的檔案, struct inode 代表磁碟上的一個檔案.

#include <linux/cdev.h>
struct cdev *cdev_alloc(void);
void cdev_init(struct cdev *dev, struct file_operations *fops);
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
void cdev_del(struct cdev *dev);

cdev 結構管理的函數, 它代表核心中的字元裝置.

#include <linux/kernel.h>
container_of(pointer, type, field);

一個傳統宏定義, 可用來擷取一個結構指標, 從它裡麵包含的某個其他結構的指標.

#include <asm/uaccess.h>

這個包含檔案聲明核心代碼使用的函數來移動資料到和從使用者空間.

unsigned long copy_from_user (void *to, const void *from, unsigned long count);
unsigned long copy_to_user (void *to, const void *from, unsigned long count);

在使用者空間和核心空間拷貝資料.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.