Skip to main content

Introduction to Linux Kernel Device Drivers

If you toy with electronics, perhaps with microcontrollers (MCU), you might at some point in time decide to interface your microcontroller to your computer.  However, you will come to learn that it would not work for a variety of reasons: one being that you need correct voltage between the computer and the microcontroller, and the second, most critical, you need a way for the computer to recognize and speak to your device.

Well, this is where device drivers are useful.  Device drivers are the software need for your computer applications to talk to your device.  The power of a device driver and the possibilities that lie with it can not fully be summarized by myself.

So, so whatever reason you many want to write a device driver, in this post, along with others, I will demonstrate how to write a device driver.

Major and Minor Numbers

When we write a device driver, that device driver will become part of the kernel and it will have an identification number called a major number.  Like a just said, this major number is used to identify a driver.  In addition to device drivers, there is almost always a device for which the device driver will "drive".  This is to say that the device driver will need a device to operate one.

device,drive, major,minor,number,write,develop,kernel,module
Major vs Minor
Any given device is only associated with one device driver.  However, any given device driver can be associated with many devices.

Consider the image to the right.  As device drivers are loading into the kernel, the are given major numbers that uniquely identifies that device driver.  Now, as devices as plugged into the machine (computer) the are give minor numbers.  The minor numbers serve to help the device driver differentiate one device from the others that it controls.

Continually, the significance of having major and minor numbers is that it allows the kernel to know which driver to invoke when a process (an actively executing program) opens, reads,writes, or closes a device.

Practial Example

Open a terminal navigate to the /dev directory.


cd /dev

Now display the list of files (which are actually devices that are attached to or integrated with your computer) in that directory by issuing the following:
ls -l

On the 5th and 6th column of the output, are the major and minor numbers for each of these devices.

To display all active modules/drivers use:


lsmod

To get more information about any individual module that is active use:

modinfo NAME_OF_MODULE

To create a new character device with major number 64 and minor number 1, use:

mknod /dev/yourDeviceName c 64 1

Comments

  1. Thanks for your efforts

    ReplyDelete
  2. You are an excellent teacher. Thanks

    ReplyDelete
  3. wonderful videos on youtube. Really appreciate your effort. God bless! cheerz!

    ReplyDelete

Post a Comment

Popular posts from this blog

Creating local variables In Assembly

Lets go over how to create local variables inside of a pure assembly source code. Much like always, you will start with a *.asm file that looks like this: source code SECTION .data SECTION .bss SECTION .text global main ;make main available to operating system(os) main: ;create the stack frame push ebp push mov ebp, esp ;destroy the stack frame mov esp, ebp pop ebp ret So, the above is the general layout of an NASM source file.  Our goal here is to create a local variable inside of the main method.  The only way to create a local variable is by using the stack.  Why?  Because we can only declare variable in storage locations and the only available storage locations are: text, bss, and data.  However, text section is only for code, so it is out of the question.  The bss and data sections are appealing, but to declare our "local" variable in these sections will defeat the purpose of these varia...

NASM Programming

Many of you, if you are like me, might be interested in how assembly works.  You will be very surprised that assembly is very very easy, especially after you write a couple of simple programs.  But don't get me wrong, you will be frustrated at first, however that frustration, if you channel it right, will lead to serious life long learning and will give you a deeper appreciation of the beauty of assembly. For more tutorial on assembly and visualization of these information, visit my youtube channel . Okay so lets get started. We will be using Netwide Assembler (NASM) to write our program. The general format of NASM file is this: ;This is a comment SECTION .data ;declare variable here SECTION .bss ;declare actual, dynamic variable SECTION .text ;where your program code/assembly code lives ; Working with Data Section In your .data section, you can declare variables like this: nameOfVariable: db 32 ;this declares a variable names nameOfVariable...

Introduction to Linux Kernel Programming

The Linux kernel is designed as a mixture of a monolithic binary image and a micro-kernel.  This combination allows for the best of both worlds.  On the monolithic side, all the code for the kernel to work with the user and hardware is already installed and ready for fast access, but the downside is that to add more functionality you need to rebuild the entire kernel.   In a different manner, a micro-kernel is composed of small pieces  of code that can be meshed today and more pieces can be added or removed as needed.  However, the downside to micro-kernel is a slower performance. Adding a module to the Kernel Linux is organized as both monolithic, one huge binary, and micro-kernel, as you can add more functionality to it.  The process of adding more functionality to the kernel can be illustrated by the crude image to the left. The process begins by using the command insmod with the name of the kernel module you want (which usually ends with ex...