LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2011, 09:15 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
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.
 
Old 04-22-2011, 12:20 PM   #2
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
Quote:
Originally Posted by resetreset View Post
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.
 
Old 04-22-2011, 01:22 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 04-25-2011, 09:37 AM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Quote:
Originally Posted by resetreset View Post
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
 
Old 04-26-2011, 03:21 AM   #5
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
Thanks so much for your reply.


Quote:
Originally Posted by smallpond View Post

Usually, when the kernel detects a device by its PCI ID, it looks up to see what driver supports that
Er - how?
 
Old 04-27-2011, 11:37 PM   #6
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Device driver model in Linux....? resetreset Programming 1 03-11-2010 05:55 PM
Driver TP-LINK model TL-WN510G s.tillman Linux - Wireless Networking 0 05-08-2009 08:49 PM
Linux printer driver model? plnelson Linux - Newbie 4 10-13-2004 04:40 AM
HP 5550 not listed under Model/Driver in CUPS rickenbacherus Linux - Hardware 1 06-20-2004 11:46 AM
Can't see new PPD file in model/driver list??? deepika Linux - General 0 09-08-2003 02:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration