Linux Device Drivers — Chapter One | by Niranjhana Narayanan

Revealed round 20 years in the past, this basic by Alessandro Rubini offers all the data you’ll want to jot down drivers for a variety of units.
Since this guide is large and requires thorough studying to grasp and implement, I’m going to try to summarize and break down crucial factors chapter by chapter (of the third version) in my posts, so programmers can get a fundamental understanding and a place to begin to dwell deeper into the chapters of their curiosity. Let’s start with few fundamental factors adopted by Chapter One.
Transient Introduction
In any working system, a kernel is a program that manages enter/output requests from software program and interprets them into knowledge processing directions for CPU and different digital parts.
- Kernel is answerable for course of administration, reminiscence administration, filesystems, system management, networking, and many others.
Gadget drivers are packages that run in kernel mode, which means they’ll work together straight with the {hardware} of the respective system.
The primary objective of system drivers is to offer abstraction by appearing as a translator between a {hardware} system and the purposes or working programs that use it.
Why do we’d like system drivers?
- It’s harmful to present consumer purposes direct entry to the {hardware}. Person actions run in consumer area and when in want of {hardware} assets, implement a set of device-independent standardized calls. The function of the system driver is to map these calls to device-specific operations that may act on actual {hardware}.
- Programmers can then write the higher-level utility code independently of no matter particular {hardware} the end-user is utilizing.
A pleasant factor about Linux Gadget Drivers
The Gadget Driver programming interface of Linux is such that drivers may be constructed individually from the remainder of the kernel and can be utilized at runtime when wanted. This modularity makes Linux drivers simpler to jot down and keep — which brings us to Loadable Modules:
- piece of code that may be added to the kernel at runtime is known as a module.
- one of many supported modules are system drivers, which may be dynamically linked to the up and working kernel by the
insmod
program and unlinked by thermmod
program.
Unix Design: Distinction between Mechanism and Coverage
Mechanism — what capabilities are to be offered; Coverage — how these capabilities can be utilized.
A very good software program package deal ought to handle these two points with completely different components of a program and even higher, with completely different packages. Instance, the Unix administration of the graphic show is cut up between the X server (the mechanism — which is aware of {hardware}) and the session managers (implement a coverage — which is {hardware} unbiased).
When writing drivers for Linux, you will need to write code that
- separates mechanism and coverage and
- is as policy-free as doable (since completely different customers have completely different wants)
The motive force ought to cope with making the {hardware} accessible, leaving all the problems about how to make use of the {hardware} to the purposes.
Lessons of Units
Linux distinguishes between three elementary system sorts:
- Character units: char system is one that may be accessed as a stream of bytes (like a file); a char driver normally implements the open, shut, learn and write system calls. Examples of this stream abstraction are the textual content console —
/dev/console
and the serial ports —/dev/ttyS0
- Block units: block units are like a exhausting disk, that may host a filesystem. They’re handled like char units in Linux (unline in most Unix programs the place blocks must be 512 bytes or extra in size). Like char units, they enable switch of any variety of bytes; and may be accessed by filesystem nodes within the
/dev
listing — they solely differ within the kernel/software program interface the place knowledge is managed otherwise for each. - Community units: community interfaces/units are normally {hardware}, that’s able to knowledge trade with different hosts. A community driver is aware of nothing in regards to the particular person connections, it solely handles packets — sending, receiving, and many others pushed by the community subsystem of the kernel. Not like char and block units, a community interface comparable to
eth0
is just not straight mapped to a node within the filesystem; and communication between kernel and driver is completely different too — as a substitute of learn and write, the kernel calls features associated to packet transmission.
Different lessons of system drivers have been added to the kernel in current instances, comparable to FireWire drivers and I2O drivers.