[英]Linux Char Driver
Can anyone tell me how a Char Driver is bind to the corresponding physical device? 谁能告诉我Char驱动程序如何绑定到相应的物理设备?
Also, I would like to know where inside a char driver we are specifying the physical device related information, which can be used by kernel to do the binding. 另外,我想知道我们在char驱动程序中的哪个位置指定了物理设备相关的信息,内核可以使用这些信息来进行绑定。
Thanks !! 谢谢 !!
A global array — bdev_map for block and cdev_map for character devices — is used to implement a hash table, which employs the device major number as hash key. 全局数组(用于块的bdev_map和用于字符设备的cdev_map)用于实现哈希表,该哈希表使用设备主数作为哈希键。
while registering for char driver following calls get in invoked to get major and minor numbers. 在调用char in驱动程序进行注册后,调用get in调用以获取主数字和副数字。
int register_chrdev_region(dev_t from, unsigned count, const char *name) int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name); int register_chrdev_region(dev_t from,unsigned count,const char * name)int alloc_chrdev_region(dev_t * dev,unsigned baseminor,unsigned count,const char * name);
After a device number range has been obtained, the device needs to be activated by adding it to the character device database. 获得设备编号范围后,需要通过将其添加到字符设备数据库中来激活该设备。
void cdev_init(struct cdev *cdev, const struct file_operations *fops); 无效的cdev_init(struct cdev * cdev,const struct file_operations * fops); int cdev_add(struct cdev *p, dev_t dev, unsigned count); int cdev_add(struct cdev * p,dev_t dev,unsigned count);复制代码
Here on cdev structure initialize with file operation and respected character device. 在cdev结构上,这里用文件操作和相关的字符设备进行初始化。
Whenever a device file is opened, the various filesystem implementations invoke the init_special_inode function to create the inode for a block or character device file. 每当打开设备文件时,各种文件系统实现都会调用init_special_inode函数来为块或字符设备文件创建inode。
void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
{
inode->i_mode = mode;
if (S_ISCHR(mode)) {
inode->i_fop = &def_chr_fops;
inode->i_rdev = rdev;
} else if (S_ISBLK(mode)) {
inode->i_fop = &def_blk_fops;
inode->i_rdev = rdev;
}
else
printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
mode);
}
now the default_chr_fpos chrdev_open () method will get invoked. 现在将调用default_chr_fpos chrdev_open ()方法。 which will look up for the inode->rdev device in cdev_map array and will get a instance of cdev structure. 它会在cdev_map数组中查找inode-> rdev设备,并获取cdev结构的实例。 with the reference to cdev it will bind the file->f_op to cdev file operation and invoke the open method for character driver. 引用cdev时,它将文件-> f_op绑定到cdev文件操作,并调用字符驱动程序的open方法。
In a character driver like I2C client driver, We specify the slave address in the client structure's "addr" field and then call i2c_master_send() or i2c_master_receive() on this client . 在I2C客户端驱动程序之类的字符驱动程序中,我们在客户端结构的“ addr”字段中指定从站地址,然后在此客户端上调用i2c_master_send()或i2c_master_receive()。 This calls will ultimately go to the main adapter controlling that line and the adapter then communicates with the device specified by the slave address. 该调用最终将转到控制该线路的主适配器,然后适配器与从属地址指定的设备进行通信。
And the binding of drivers operations is done mainly with cdev_init() and cdev_add() functions. 驱动程序操作的绑定主要通过cdev_init()和cdev_add()函数完成。
Also driver may choose to provide probe() function and let kernel find and bind all the devices which this driver is capable of supporting. 驱动程序也可以选择提供probe()函数,并让内核查找并绑定该驱动程序能够支持的所有设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.