In Linux device driver function, how to know the minor number or for which particular device this driver function has been invoked? -


if there 3 i2c devices mentioned below , in device driver init() function following call register_chrdev(89, "i2c", &i2cfops) invoked. note, name "i2c" not "i2c-0"/"i2c-1"/"i2c-2". how in i2cdriver_open or i2cdriver_ioctl function drive know minor number or i2c device function has been invoked?

please refer below more details.

crw-r--r--    1 0        0         89,   0 jun 12 09:15 /dev/i2c-0 crw-r--r--    1 0        0         89,   1 jun 12 09:15 /dev/i2c-1 crw-r--r--    1 0        0         89,   2 jun 12 09:15 /dev/i2c-2 

application:

int main(void) {   int fd;   fd = open("/dev/i2c-0");   (void) ioctl(fd, ...);   return 0; } 

driver:

static struct file_operations i2cfops; int i2cdriver_open(struct inode * inodeptr, struct file * fileptr); int i2cdriver_ioctl(struct inode * inodeptr, struct file * fileptr, unsigned int ui, unsigned long ul); int driver_init(void) {   i2cfops.open = &i2cdriver_open;   i2cfops.ioctl = &i2cdriver_ioctl;   (void) register_chrdev(89, "i2c", &i2cfops);   return 0; } int i2cdriver_open(struct inode * inodeptr, struct file * fileptr) {   /*in here, how know minor number or i2c device function has been invoked?*/ } int i2cdriver_ioctl(struct inode * inodeptr, struct file * fileptr, unsigned int ui, unsigned long ul) {   /*in here, how know minor number or i2c device function has been invoked?*/ } 

as general note, whenever you're posting questions linux kernel/driver development, include kernel version you're working with. makes easier give answers work kernel.

this should able retrieve minor number:

/* add header if don't have included */ #include <linux/kdev_t.h>  /* add macro function wherever need minor number */ unsigned int minor_num = minor(inodeptr -> i_rdev); 

this page has definition of minor macro, , this page has definition of inode structure reference.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -