0

I have seen in pci device drivers this line

   module_pci_driver(cp_driver);

But in other pci device drivers this like

     module_init(rtl8139_init_module);

both lines found at the end of driver's .c file in different drivers

What I know is: I can create a pci device driver with __init but I can also create a pci device driver without __init

i.e. [Realtek Ethernet has two drivers in Linux source

1) 139cp.c (without __init)

2) 8139too.c with __init].

I assume that the main difference between the two is simply that if I have to use a pci device driver right after loading of the driver module with insmod command so I use an implementation of a device driver with __init.

Question On the contrary, if I just want to load the pci device driver but not use it, Then should I create a pci device driver with module_pci_driver() (So no need to add __init)? And what does it do(module_pci_driver)? How its different from pci driver with __init

I like to know I may have a misconception anyone please clarify. Also does the probe function of both type of drivers will run when I load the driver with insmod command? When? if yes than what's the difference since most configuring of device is done in proble function

To sum up When initialization happens in driver with module_pci_driver(cp_driver); since they dont have __init implemented. What command used

user786
  • 3,902
  • 4
  • 40
  • 72

1 Answers1

1

module_pci_driver() is a helper meant for drivers that don't do anything special in their module init and exit (i.e. modules whose init and exit functions just register/unregister).

It generates the init and exit functions for you, reducing some boilerplate.

In this specific case, the 8139too driver might do something in addition to registering the driver (in this case, logging the driver name), so isn't using module_pci_driver()

The probe function is called for existing or new devices that match the ID table and aren't already owned (see How To Write Linux PCI Drivers for details). It returns a value indicating if the driver will be taking ownership of the device (either 0 or an error code).

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
  • in general, in linux kernel modules or device drivers does 0 always specifies as `true` and 1 always specifies as `false`? Please clarify – user786 Jan 11 '21 at 16:19
  • In general, 0 is success, and a negative value is an error code (for functions that return error codes) – Hasturkun Jan 11 '21 at 16:27
  • you posted very great documentation for pci drivers as a link. Is there any similar type of documentation for especially network Ethernet driver. same html format from kernel.org with more details on latest ethernet driver api. all I found is with very short descriptions on https://www.kernel.org/doc/htmldocs/networking/API-struct-net-device.html – user786 Jan 11 '21 at 16:56
  • There's more around there, like [this](https://www.kernel.org/doc/html/latest/networking/netdevices.html) and [this](https://www.kernel.org/doc/html/latest/networking/kapi.html) (from [The Linux Networking Documentation](https://www.kernel.org/doc/html/latest/networking/index.html)) for instance. – Hasturkun Jan 11 '21 at 20:19
  • if I want 2 implement something `like module_pci_driver()` in Userspace application.Is it possible. if NOT then how to implement it in kernel.can I implement it in my low level programming and provide ability to drivers so they can export their init or some function like `module_pci_driver` to my low level kernel module or firmware.I assume,function assigned2 module_pci_driver(Function) is called from elsewhere (probably from low level kernel stack/may be from firmware).please clarify.I am new 2 kernel development.also where is implementation & #define of `module_pci_driver` in linux source – user786 Jan 12 '21 at 05:15
  • You might want to read the [UIO](https://www.kernel.org/doc/html/v4.14/driver-api/uio-howto.html) documentation. For anything else, I suggest you ask a new, specific question. – Hasturkun Jan 12 '21 at 09:00
  • I did asked this question some time ago. https://stackoverflow.com/questions/65642626/need-to-debug-mmap-so-where-is-the-object-file-for-mmap-or-module-containing-mma Still no replies. Can u please take a look at this. and thank you for the link. I will search more on kernel.org which u mentioned – user786 Jan 12 '21 at 13:17