2015年3月31日 星期二

LDD3 潤飾 3.9. quick reference

3.9. 快速參考

本章介紹了下面symbols和header files. struct file_operations 和struct file 中的成員的列表這裡不重複了.
#include <linux/types.h>
dev_t
dev_t 是用來在內核裡代表device number的類型.
int MAJOR(dev_t dev);
int MINOR(dev_t dev);
從device number中抽取major number,minor number的巨集.
dev_t MKDEV(unsigned int major, unsigned int minor);
從major & minor number來建立dev_t 數據項的巨集定義.
#include <linux/fs.h>
"filesystem"header是編寫device driver需要的header. 許多重要的函數和數據結構在此定義.
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);
允許driver分配和釋放device number的範圍的函數. register_chrdev_region 應當用在事先知道需要的major number時; 對於動態分配, 使用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 結構持有一個char driver的method; 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 結構管理的函數, 它代表內核中的char device.
#include <linux/kernel.h>
container_of(pointer, type, field);
一個傳統巨集定義, 可用來獲取一個結構pointer, 從它裡面包含的某個其他結構的pointer.
#include <asm/uaccess.h>
這個包含文件聲明內核代碼使用的函數來移動數據到user space和從user space.
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);
在user space和kernel space拷貝數據.

沒有留言: