Device drivers – character device driver Example

Device driver – as the name suggests – it controls a device. It could a physical resource or a non-physical resource. In the following articles, we will ook through some device

The character device driver is the most basic device driver that an interested developer will see and needs to learn in the Linux kernel. It is a streams driver and handles data as a stream of bytes.

The character device driver operates on a character device file in the Linux kernel. all the file operations that can be performed on a typical file such as open, close, write, read, lseek can be be performed on the character device file.

The below code provides a sample implementation for a character device driver on linux. There are many articles on the Web explaining a character device driver. This specific articles intends to provide an overview via a code snippet.

Character driver example

In this example, the following callbacks have been initialised. At the most, these four callbacks need to be present. The structure file_operations can be referred at the location- /linux/fs.h

  1. open – open an instance of the device driver. Multiple instances can be opened up-till 256
  2. close an instance of the device driver
  3. write to the device driver
  4. read from the device driver

In the example two APIs are also observed.

  1. register_chrdev – register a character device driver to the kernel
  2. unregister_chrdev – un-register a character device driver from the kernel

Each character device driver has a Major number and a Minor number. The The Major number is the same for all instances of the character device and the Minor number varies according to different instances.

In the code snippet above,

Majornumber = register_chrdev(0, DEVICE_NAME, &fileops) – do note that the major number passed as a parameter to register_chrdev is ‘0’. When ‘0’ is passed, the kernel assigns a valid free Major number. This process is easier than defining a major number on our own accord and the possibility that the major number chosen is already in use by the kernel.

An interesting point to note and observe is that, what has been registered is a device driver to access a character device. However, there is no code snippet which actually creates the character device on which the character device driver will be perform actions upon.

Also, note that for the above code, the “mknod command” needs to be called from the terminal before this device driver is used for the device.

Character Device file created via mknod command

Now the character driver is ready for operation. In the next article, we will look at a sample code that opens the character device file instance created and the operation of the character driver

Character device driver sample user space code

Comments

  1. Pingback: Kernel Thread Code example | Hitch Hiker's Guide to Learning

  2. Pingback: Character device driver sample user spacecode | Hitch Hiker's Guide to Learning

  3. Pingback: Character Device driver – creating device file instance in code | Hitch Hiker's Guide to Learning

Leave a Reply

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