The Next Step in writing a device driver for Linux

Having understood on how to create a simple Kernel module here – <Writing a Simple Kernel Module>, we shall try and look at how Linux characterizes the different Kernel driver types.

The most commonly seen device drivers are

  1. Character device drivers
  2. Block device drivers
  3. Network device drivers

We shall look at platform drivers, miscellaneous drivers etc in later articles

Character Devices

A character device is a device which handles a stream of bytes. The driver that handles the character device is termed as a char driver. The char driver usually will support primarily the following system call functionalities which are as similar to operating on a file :-

  1. open
  2. close
  3. read and
  4. write

The serial port /dev/tty0 is an example of a char device. Char devices are accessed similar to a file however the char device file is just a channel to speak to the device.

An example read of the character device tty0 in linux provides the following output

Block devices

A block device is a device which handles blocks of data.  As mentioned in Linux Device Drivers – “A block device is something that can host a filesystem, such as a disk. In most Unix systems, a block device can be accessed only as multiples of a block, where a block is usually one kilobyte of data or another power of 2.”

A block device can also be opened similar to a character device and the interface that a block device provides is the same as a character device. The difference being that any number of bytes an be accessed/transferred at a single time period.

Block devices are also represented as files in the linux OS.  An example of a block device is a storage disk. An example read of the hard disk partition in Linux gives the following output

Networking Devices

Network class devices are interfaces to access hardware so that packets can be routed to and from the device. A Networking class device is not saved as a file in linux. They are assigned unique interface names such as eth0/eth1/wlan0 etc – these do not have an entry in the file system. An output of the ifconfig command is shown below which provides the currently operating network interfaces on the system

We will try and look at how some of these drivers can be explained via examples in the coming articles

Passing Data to a kernel module – module_param

Comments

Leave a Reply

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