2

I am starting to learn to write PCI driver and the first exercise i took was to find if a given device exists on the bus. After searching some books and internet, i was able to write down the below program which does work but i am not clear on few concepts.

1 /*
  2  Program to find a device on the PCI sub-system 
  3 */
  4 #define VENDOR_ID       0x8086
  5 #define DEVICE_ID       0x7113
  6 
  7 #include <linux/kernel.h>
  8 #include <linux/module.h>
  9 #include <linux/stddef.h>
 10 #include <linux/pci.h>
 11 #include <linux/init.h>
 12 #include <linux/cdev.h>
 13 #include <linux/device.h>
 14 
 15 #define LOG(string...) printk(KERN_NOTICE string)
 16 
 17 #define CDEV_MAJOR      227
 18 #define CDEV_MINOR      0
 19 
 20 
 21 MODULE_LICENSE("GPL");
 22 
 23 struct pci_dev *pci_dev;
 24 unsigned long mmio_addr;
 25 unsigned long reg_len;
 26 unsigned long *base_addr;
 27 
 28 int device_probe(struct pci_dev *dev, const struct pci_device_id *id);
 29 void device_remove(struct pci_dev *dev);
 30 
 31 struct pci_device_id  pci_device_id_DevicePCI[] =
 32 {

33         {VENDOR_ID, DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
 34         {}  // end of list
 35 };
 36 
 37 struct pci_driver  pci_driver_DevicePCI =
 38 {
 39   name: "MyPCIDevice",
 40   id_table: pci_device_id_DevicePCI,
 41   probe: device_probe,
 42   remove: device_remove
 43 };
 44 
 45 
 46 int init_module(void)
 47 {
 48         struct pci_dev *pdev = NULL;
 49         int ret = 0;
 50 
 51         pci_register_driver(&pci_driver_DevicePCI);
 52         pdev = pci_get_device(VENDOR_ID, DEVICE_ID, NULL);
 53         if (pdev)
 54         {
 55                 LOG("Device found ... ");
 56                 pci_dev = pdev;
 57         }
 58         else
 59         {
 60                 LOG("Device not found ... ");
 61         }
 62         return ret;

 63 
 64 
 65 }
 66 
 67 void cleanup_module(void)
 68 {
 69         pci_unregister_driver(&pci_driver_DevicePCI);
 70 
 71 }
 72 
 73 int device_probe(struct pci_dev *dev, const struct pci_device_id *id)
 74 {
 75         int ret;
 76         LOG("Devie probed");
 77         ret  = pci_enable_device(dev);
 78         if (ret < 0 ) LOG("Failed while enabling ... ");
 79 
 80         return ret;
 81 }
 82 
 83 void device_remove(struct pci_dev *dev)
 84 {
 85   pci_release_regions(dev);
 86   pci_disable_device(dev);
 87 }

Inside init_module() function, given user VID and DID device is found and if successful, struct pdev is pointing to the respective pci device. As i have read, probe() function kicks in as soon as the device is found.

Does this mean we always have to do pci_get_device() before calling the pci_enable_device()? IMO, yes but if so, how do device_probe() get reference to dev structure even though i am not passing it?

If i am 100% sure that my device exists on the system, how can i call pci_enable_device() without registering?

I am currently referring LDD3 book where they explain all the calls but for a beginner i feel it misses out how to connect the dot. Does any one have pointers where widely used pci_xx() calls are explained with neat examples?

4

1 回答 1

1

您的probe函数由 调用pci_register_driver,它搜索具有匹配设备 ID 的无人认领的设备。

您的pci_get_device电话在probe返回后发生,并且不需要。

于 2013-11-14T20:20:19.940 回答