linux 如何从驱动程序ioctl()中文件对象中获取js_dev?

sd2nnvve  于 12个月前  发布在  Linux
关注(0)|答案(3)|浏览(91)

我正在为一个PCIE硬件开发Linux驱动程序。内核是v4.13。对于每个设备对象,都有一堆存储在pci_set_drvdata(struct pci_dev *pdev, void *data)中的数据。
IOCtl()服务例程中,如何使用struct file * pFile取回数据?

long IOCtlService(struct file * pFile, unsigned int cmd, unsigned long arg)

谢谢

5jvtdoz2

5jvtdoz21#

如果您有多个设备,那么您可能会使用IDR来分配和跟踪次要的开发节点ID。对idr_alloc的调用接受一个指针,与ID一起存储以备将来使用。
在ioctl处理程序中,您可以使用idr_find从idr中查找指针。举例来说:

分配一个IDR并存储info

全局定义

DEFINE_MUTEX(minor_lock);
DEFINE_IDR(mydev_idr);

在您的PCI探针处理程序中

struct my_dev * info;

info = kzalloc(sizeof(struct mydev_info), GFP_KERNEL);
if (!info)
  return -ENOMEM;

mutex_lock(&minor_lock);
info->minor = idr_alloc(&mydev_idr, info, 0, MYDEV_MAX_DEVICES, GFP_KERNEL);
if (info->minor < 0)
{
  mutex_unlock(&minor_lock);
  goto out_unreg;
}
mutex_unlock(&minor_lock);

pci_set_drvdata(dev, info);

从IDR中删除存储的指针

unsigned minor = iminor(flip->f_inode);
struct my_dev * dev = (struct my_dev *)idr_find(&mydev_idr, minor);
if (!dev)
  return -EINVAL;

请确保在删除设备时释放您的IDR

mutex_lock(&minor_lock);
idr_remove(&mydev_idr, info->minor);
mutex_unlock(&minor_lock);
lmvvr0a8

lmvvr0a82#

使用ioctl()驱动程序将接受命令并处理这些命令,

ioctl(fd, cmd , INPARAM or OUTPARAM);

如何使用struct file * pFile取回数据?你想做什么手术?您可以使用ioctl.h中的命令来执行此操作。例如IOR(如果命令需要从内核空间读取某些内容)

mzaanser

mzaanser3#

struct file也有private_data,所以你可以在这里存储pci_dev,就像struct miscdev一样:

static int misc_open(struct inode *inode, struct file *file)
{
    ......
    struct miscdevice *c;

    ......

    /*
     * Place the miscdevice in the file's
     * private_data so it can be used by the
     * file operations, including f_op->open below
     */
    file->private_data = c;
}

相关问题