background preloader

Kernel module

Facebook Twitter

Complete Device Driver Info

Registring driver. Major and minor number. Diff module and driver. Device Driver Basics. The HyperNews Linux KHG Discussion Pages Device Driver Basics We will assume that you decide that you do not wish to write a user-space device, and would rather implement your device in the kernel.

Device Driver Basics

You will probably be writing writing two files, a .c file and a .h file, and possibly modifying other files as well, as will be described below. We will refer to your files as foo.c and foo.h, and your driver will be the foo driver. Namespace One of the first things you will need to do, before writing any code, is to name your device. Allocating memory Memory allocation in the kernel is a little different from memory allocation in normal user-level programs. Memory is provided in pieces whose size is a power of 2, except that pieces larger than 128 bytes are allocated in blocks whose size is a power of 2 minus some small amount for overhead. To free memory allocated with kmalloc(), use one of two functions: kfree() or kfree_s(). Be gentle when you use kmalloc. Character vs. block devices. What is a Device Driver? The HyperNews Linux KHG Discussion Pages What is a Device Driver?

What is a Device Driver?

Making hardware work is tedious. To write to a hard disk, for example, requires that you write magic numbers in magic places, wait for the hard drive to say that it is ready to receive data, and then feed it the data it wants, very carefully. To write to a floppy disk is even harder, and requires that the program supervise the floppy disk drive almost constantly while it is running.

Instead of putting code in each application you write to control each device, you share the code between applications. All versions of Unix have an abstract way of reading and writing devices. All devices controlled by the same device driver are given the same major number, and of those with the same major number, different devices are distinguished by different minor numbers. Modules. Table of Contents, Show Frames, No Frames This chapter describes how the Linux kernel can dynamically load functions, for example filesystems, only when they are needed.


Linux is a monolithic kernel; that is, it is one, single, large program where all the functional components of the kernel have access to all of its internal data structures and routines. The alternative is to have a micro-kernel structure where the functional pieces of the kernel are broken out into separate units with strict communication mechanisms between them. This makes adding new components into the kernel via the configuration process rather time consuming. Say you wanted to use a SCSI driver for an NCR 810 SCSI and you had not built it into the kernel. Drivers. Table of Contents, Show Frames, No Frames One of the purposes of an operating system is to hide the peculiarities of the system's hardware devices from its users.


For example the Virtual File System presents a uniform view of the mounted filesystems irrespective of the underlying physical devices. This chapter describes how the Linux kernel manages the physical devices in the system. The CPU is not the only intelligent device in the system, every physical device has its own hardware controller. The keyboard, mouse and serial ports are controlled by a SuperIO chip, the IDE disks by an IDE controller, SCSI disks by a SCSI controller and so on. One of the basic features of is that it abstracts the handling of devices. Linux supports three types of hardware device: character, block and network. Pointers for kernel modules. How Do Modules Get Into The Kernel? You can see what modules are already loaded into the kernel by running lsmod, which gets its information by reading the file /proc/modules.

How Do Modules Get Into The Kernel?

How do these modules find their way into the kernel? When the kernel needs a feature that is not resident in the kernel, the kernel module daemon kmod execs modprobe to load the module in. modprobe is passed a string in one of two forms: A module name like softdog or ppp.A more generic identifier like char-major-10-30. If modprobe is handed a generic identifier, it first looks for that string in the file /etc/modprobe.conf. If it finds an alias line like: it knows that the generic identifier refers to the module softdog.ko. Next, modprobe looks through the file /lib/modules/version/modules.dep, to see if other modules must be loaded before the requested module may be loaded. Or: Linux distros provide modprobe, insmod and depmod as a package called module-init-tools.

An introduction to Linux kernel programming - Lesson 4: Writing and running your first kernel module. Printer-friendly version Finally, your first kernel module Having taken the first three lessons in this course to lay the foundations for kernel programming, we're now in a position to write, compile and load our first kernel module.

An introduction to Linux kernel programming - Lesson 4: Writing and running your first kernel module

And to be perfectly clear, what we're going to be doing in this lesson is the absolute bare minimum to do just that. What I mean by that is that we're going to write the simplest, most unproductive, content-free module imaginable. It will have no purpose and it will do nothing when it's loaded, but that's fine because, once we can do that, everything that follows in the rest of this course is nothing more than adding more and more features to our modules. NOTE: This is the final lesson in this course that is available for free. And on that note, let's get to our first kernel module. How are we going to do this? I'm going to change the way I normally explain how to do this. What software do you need to install? $ sudo apt-get install build-essential $ make. The Linux Kernel Module Programming Guide. Peter Jay SalzmanMichael BurianOri Pomerantz Copyright © 2001 Peter Jay Salzman 2007-05-18 ver 2.6.4 The Linux Kernel Module Programming Guide is a free book; you may reproduce and/or modify it under the terms of the Open Software License, version 1.1.

The Linux Kernel Module Programming Guide

You can obtain a copy of this license at This book is distributed in the hope it will be useful, but without any warranty, without even the implied warranty of merchantability or fitness for a particular purpose. The author encourages wide distribution of this book for personal or commercial use, provided the above copyright notice remains intact and the method adheres to the provisions of the Open Software License.

Derivative works and translations of this document must be placed under the Open Software License, and the original copyright notice must remain intact. If you publish or distribute this book commercially, donations, royalties, and/or printed copies are greatly appreciated by the author and the Linux Documentation Project (LDP). Writing a Linux Kernel Module — Part 2: A Character Device. Writing a Linux Kernel Module — Part 1: Introduction.