简体   繁体   中英

Linux Char Driver

Can anyone tell me how a Char Driver is bind to the corresponding physical device?

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.

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.

while registering for char driver following calls get in invoked to get major and minor numbers.

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); int cdev_add(struct cdev *p, dev_t dev, unsigned count);

Here on cdev structure initialize with file operation and respected character device.

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.

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. which will look up for the inode->rdev device in cdev_map array and will get a instance of cdev structure. with the reference to cdev it will bind the file->f_op to cdev file operation and invoke the open method for character driver.

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 . 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.

Also driver may choose to provide probe() function and let kernel find and bind all the devices which this driver is capable of supporting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM