GitHub - backendeveloper/Linux-Character-Device
Driver on

GitHub – backendeveloper/Linux-Character-Device

Be aware:

The submit helps perceive how you can write a tool driver, the importance of a tool file and its function within the interplay between a person program and machine driver.

Introduction and a few fundamentals

###Introduction
The gadgets in UNIX fall in two categories- Character gadgets and Block gadgets. Character gadgets might be in comparison with regular recordsdata in that we are able to learn/write arbitrary bytes at a time (though for many half, looking for will not be supported).They work with a stream of bytes. Block gadgets, however, function on blocks of information, not arbitrary bytes. Common block dimension is 512 bytes or bigger powers of two. Nonetheless, block gadgets might be accessed the identical was as character gadgets, the driving force does the block administration. (Networking gadgets don’t belong to those classes, the interface supplied by these drivers in fully completely different from that of char/block gadgets)

In our instance, we will probably be growing a personality machine represented by the machine file /dev/myDev. The mechanisms for creating this file will probably be defined later.

###Underneath the hood

Now how does Linux know which driver is related to which file? For that, every machine and its machine file has related to it, a novel Main quantity and a Minor quantity. No two gadgets have the identical main quantity. When a tool file is opened, Linux examines its main quantity and forwards the decision to the driving force registered for that machine. Subsequent requires learn/write/shut too are processed by the identical driver. So far as kernel is anxious, solely main quantity is vital. Minor quantity is used to establish the precise machine occasion if the driving force controls a couple of machine of a sort.

To know the main, minor variety of gadgets, use the ls – l command as proven under.

$ ls -l /dev/random 

A Linux driver is a Linux module which might be loaded and linked to the kernel at runtime. The motive force operates in kernel area and turns into a part of the kernel as soon as loaded, the kernel being monolithic. It may well then entry the symbols exported by the kernel.

When the machine driver module is loaded, the driving force first registers itself as a driver for a selected machine specifying a selected Main quantity.

It makes use of the decision register_chrdev perform for registration. The decision takes the Main quantity, Minor quantity, machine title and an handle of a construction of the sort file_operations(mentioned later) as argument. In our instance, we will probably be utilizing a serious variety of 89 . The selection of main quantity is unfair but it surely must be distinctive on the system.

The syntax of register_chrdev is

**int register_chrdev(unsigned int main,const char title,struct file_operations fops)

Driver is unregistered by calling the unregister_chrdev perform.

Since machine driver is a kernel module, it ought to implement init_module and cleanup_module features. The register_chrdev name is finished within the init_module perform and unregister_chrdev name is finished within the cleanup_module perform.

When register_chrdev name is finished, the fourth argument is a construction that accommodates the addresses of those callback features, callbacks for open, learn, write, shut system calls. The construction is of the sort file_operations and has four essential fields that ought to be set – learn,write,open and launch. Every area have to be assigned an handle of a perform that may be referred to as when open, learn,write , shut system calls are referred to as respectively. For eg

// construction containing callbacks
static struct file_operations fops =
{
	.learn = dev_read, // handle of dev_read
	.open = dev_open, // handle of dev_open
	.write = dev_write, // handle of dev_write
	.relase = dev_rls, // handle of dev_rls
};

Creating a tool file

A tool file is a particular file. It may well’t simply be created utilizing cat or gedit or shell redirection for that matter. The shell command mknod is often used to create machine file. The syntax of mknod is
mknod path sort main minor

                         mknod path sort main minor

Be aware:

  • path : path the place the file to be created. It’s not needed that the machine file must be created within the /dev listing. It’s a mere conference. A tool file might be created nearly wherever.
  • sort : ‘c’ or ‘b’ . Whether or not the machine being represented is a personality machine or a block machine. In our instance, we will probably be simulating a personality machine and therefore we select ‘c’.
  • main, minor : the main and minor variety of the machine.

Heres how

[email protected]:/residence/kaan# mknod /dev/myDev c 89 l
[email protected]:/residence/kaan# chmod a+r+w /dev/myDev

Be aware:

  • chmod, although not needed is finished as a result of, if not performed, solely processes will root permission can learn or write to our machine file.

The motive force code

Given under is the code of the machine driver

Imgur
Machine Driver Code


Compiling the driving force

A Linux module can’t simply be compiled the best way we compile regular C recordsdata. cc filename.c gained’t work. Compiling a Linux module is a separate course of by its personal. We use the assistance of kernel Makefile for compilation. The makefile we will probably be utilizing is.

Imgur
makefile for module compilation

Right here, we’re making use of the kbuild mechanism used for compiling the kernel.
The results of compilation is a ko file (kernel object) which may then be loaded dynamically when required.


Loading and unloading the driving force

As soon as the compilation is full, we are able to use both insmod or modprobe command ( insmod myDev.ko or modprobe myDev.ko, in fact assuming the present listing accommodates the compiled module). The distinction between insmod and modprobe is that modprobe routinely reads our module to search out any dependencies on different modules and masses them earlier than loading our module (these modules have to be current in the usual path although!). insmod lacks this function.

To check if the driving force has been loaded efficiently, do cat /proc/modules and cat /proc/gadgets. We should always see our module title within the first case and machine title within the second.

Imgur
cat /proc/modules

Imgur
cat /proc/modules

To unload the driving force, use rmmod command. (rmmod myDev.ko)


Testing the driving force

To check the driving force, we strive writing one thing to the machine file after which studying it. For instance,

Imgur
Testing the driving force

See the output. (The rationale for the ‘ugly’ output is as a result of echo routinely writes a newline character to the tip of string. When the driving force reverses the string, the newline is shifted to the entrance of the string and there’s no newline on the finish. Therefore the outcome being ‘ugly’)

To see how this may be performed from our program, I wrote a demo program given under

Imgur
Interacting with the driving force

Compile it usually(or run make take a look at) and run ./take a look at some_string and see the output.

Imgur
Testing the driving force

Be aware:

  • You must be root to compile the module, load the module and unload the module.
  • This driver interface introduced right here is an outdated one, there’s a newer one however the outdated one continues to be supported.

Leave a Reply

Your email address will not be published. Required fields are marked *