LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why is the kernel driver model the way it is? (https://www.linuxquestions.org/questions/programming-9/why-is-the-kernel-driver-model-the-way-it-is-876515/)

resetreset 04-22-2011 09:15 AM

Why is the kernel driver model the way it is?
 
Hi,
I couldn't get to sleep last night, so I was thinking about this - to
write a device driver, you need to INSMOD something into the kernel
right? Well, why did they architecture it this way, instead of having
a separate format for it, like Windows VxD or something? How does the
insmodding work? A device driver would need the functionality to
either write something TO the device, or read something FROM the
device, right? Well, how exactly is it structured, i.e. how does the
program hand the data to be written (for example) to the driver? And
when something is read, how does it get it from the driver?

Thanks a lot for your reply.

jthill 04-22-2011 12:20 PM

Quote:

Originally Posted by resetreset (Post 4332642)
why did they architecture it this way

The doc and howtos for driver model and loadable drivers are very high on every Google search I could concoct for your question. Here's one of the better ones. There's also the Documentation directory in the kernel repo, kernel-docs.txt might be a good place to start getting your bearings more generally.

theNbomr 04-22-2011 01:22 PM

I think you are confusing the concept of loadable modules and passing data to/from the kernel/module. insmod or modprobe are used to load the kernel module (or rmmod, to unload it). Once the module is loaded into the kernel, it can serve it's function as a device driver. The device it writes to is probably some hardware, and it uses whatever means it needs to send & receive data to/from the device.

For communicating with the userspace side, the kernel provides all of the data, as arguments to the functions in the entry table. There are different collections of standard interfaces implemented by device drivers: character & block types, principally. These define a standard set of functions and arguments which your driver must provide and register upon loading. The kernel will dispatch to your driver functions according to the needs specified when a userspace application makes system calls. When appropriate, these functions will receive or send data, which may ultimately end up transferring to/from the userspace application (or maybe just to another driver; you don't need to care, the kernel figures it out).

The link posted by jthill explains this quite well.

--- rod.

smallpond 04-25-2011 09:37 AM

Quote:

Originally Posted by resetreset (Post 4332642)
Hi,
I couldn't get to sleep last night, so I was thinking about this - to
write a device driver, you need to INSMOD something into the kernel
right?

Nope. To write a driver you need an editor and a copy of
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html.

Quote:

Well, why did they architecture it this way, instead of having
a separate format for it, like Windows VxD or something? How does the
insmodding work?
Usually, when the kernel detects a device by its PCI ID, it looks up to see what driver supports that and uses insmod to load the code for that driver. You can also specify other modules to load using /etc/modprobe.conf.

Quote:

A device driver would need the functionality to
either write something TO the device, or read something FROM the
device, right? Well, how exactly is it structured, i.e. how does the
program hand the data to be written (for example) to the driver? And
when something is read, how does it get it from the driver?
There are different methods, but the most common is for the module when it is loaded to create a device in the /dev directory. The user program opens the dev and reads or writes to it. The standard UNIX model is:
  • open - provides user space with a handle for the device
  • close - free the handle
  • read - transfer data from the driver to user space
  • write - transfer data from user space to the kernel driver

resetreset 04-26-2011 03:21 AM

Thanks so much for your reply.


Quote:

Originally Posted by smallpond (Post 4335604)

Usually, when the kernel detects a device by its PCI ID, it looks up to see what driver supports that

Er - how?

smallpond 04-27-2011 11:37 PM

Have a look at the Documentation directory in the kernel sources for a description of how stuff works in the kernel.

The file pci.txt contains the details on how the bus probe works and how PCI id is used to load drivers.


All times are GMT -5. The time now is 11:14 PM.