在Linux设备驱动程序函数中,如何知道该驱动程序函数已被调用的次要编号或针对哪个特定设备?

vxqlmq5t  于 2024-01-06  发布在  Linux
关注(0)|答案(2)|浏览(148)

如果有三个I2C设备,如下所述,在设备驱动程序init()函数中调用以下调用register_chrdev(89,“i2c”,&i2cfops)。注意,名称是“i2c”而不是“i2c-0”/“i2c-1”/“i2c-2”。在i2cdriver_open或i2cdriver_ioctl函数中,驱动器如何知道次要编号或为哪个I2C设备调用了该函数?
请参阅下文了解更多详情。

  1. crw-r--r-- 1 0 0 89, 0 Jun 12 09:15 /dev/i2c-0
  2. crw-r--r-- 1 0 0 89, 1 Jun 12 09:15 /dev/i2c-1
  3. crw-r--r-- 1 0 0 89, 2 Jun 12 09:15 /dev/i2c-2

字符串
适用范围:

  1. int main(void)
  2. {
  3. int fd;
  4. fd = open("/dev/i2c-0");
  5. (void) ioctl(fd, ...);
  6. return 0;
  7. }


司机:

  1. static struct file_operations i2cfops;
  2. int i2cdriver_open(struct inode * inodePtr, struct file * filePtr);
  3. int i2cdriver_ioctl(struct inode * inodePtr, struct file * filePtr, unsigned int ui, unsigned long ul);
  4. int driver_init(void)
  5. {
  6. i2cfops.open = &i2cdriver_open;
  7. i2cfops.ioctl = &i2cdriver_ioctl;
  8. (void) register_chrdev(89, "i2c", &i2cfops);
  9. return 0;
  10. }
  11. int i2cdriver_open(struct inode * inodePtr, struct file * filePtr)
  12. {
  13. /*In here, how to know the minor number or for which I2C device this function has been invoked?*/
  14. }
  15. int i2cdriver_ioctl(struct inode * inodePtr, struct file * filePtr, unsigned int ui, unsigned long ul)
  16. {
  17. /*In here, how to know the minor number or for which I2C device this function has been invoked?*/
  18. }

i34xakig

i34xakig1#

作为一个一般性的注意,每当你发布关于Linux内核/驱动程序开发的问题时,总是包括你正在使用的内核版本。它使你更容易给予实际适用于你的内核的答案。
这应该能够检索您的未成年人号码:

  1. /* Add this header if you don't already have it included */
  2. #include <linux/kdev_t.h>
  3. /* Add this macro function wherever you need the minor number */
  4. unsigned int minor_num = MINOR(inodePtr -> i_rdev);

字符串
本页有MINOR宏的定义,本页有inode结构的定义以供参考。

btxsgosb

btxsgosb2#

使用同一设备驱动程序创建多个设备节点时,多个设备的主设备号相同,但次设备号不同。
要检索调用的是哪一个特定的设备,您只需要知道在文件open()函数中调用的设备的次要编号。

  1. #include <kdev_t.h>
  2. unsigned int minor_num;
  3. static int file_open(struct inode *inode, struct file *file){
  4. /*The line below is to be written in your open function*/
  5. minor_num = MINOR(inode->i_rdev);
  6. printk(KERN_INFO "Device file opened\n");
  7. return 0;
  8. }

字符串
这将为您提供给予设备编号,您可以使用该编号执行任何特定于设备的任务。

相关问题