Saurabh's Linux Blog

Sunday, September 28, 2014

There are many points to take care while writing a portable linux driver.One of the key point I wanted to mention here is the usage of some portable API's which will take care of the automatic endianness conversion for ensuring a portable driver.

Many times during kernel programming we interact with hardware devices for
exchanging bytes of data.It can be the case that we receive data from
hardware, we pack them and copy to application buffer and vice-versa.

Suppose I consider the case where I read some chunks of data from hardware and I need to copy the data to application buffer.I do not know that if its a little endian or a big endian processor where my driver is running and I only know that my application wants data in big endian format.

So if my driver was running on a little endian processor and I directly copy data to application buffer then it wrong.

So I use a API provided by linux kernel: cpu_to_be32()

Now this API takes as argument a 32-bit data and returns me the big endian format 32-bit data and therefore now I am happy, not to care about the native endianness.

Please have a look at some more API's in the below link and enjoy writing a portable linux driver:

Please have a look for such API's in below link:
http://lxr.free-electrons.com/source/include/linux/byteorder/generic.h

Though various optimized and non optimized methods are available on internet, I have one more way of determining endianness of native processor.I guess it is one more non optimized method but yeah it is one method.

Sunday, August 3, 2014

The kernel block layer gets the block I/O requests from the user for the disk access.The kernel does not issue block I/O requests to the disk in the order they are received or as soon as they are received. Instead, it performs operations called merging and sorting to greatly improve the performance of the system as a whole. The subsystem of the kernel that performs these operations is called the I/O scheduler.

Kernel mainly uses below scheduling policies for disk access:

1. Complete Fair Queuing (CFQ)

2. Anticipatory

3. Noop

4. Deadline

Now suppose I want to get information about the current scheduling policy for one of my block device say CDROM.

As we know CDROM is represent in Linux as sr0 (can be sr1, sr2 etc. depending upon the number of same device type connected at that moment) node under /device directory.

Tuesday, June 3, 2014

Sometimes we want to query about our devices present in our computer. e.g. Hard Disks , DVD drives etc.These devices comes into the family of SCSI (Small Computer Systems Interface) devices.So we can use some of the SCSI commands to query the status of the SCSI devices.I use linux system to demonstrate this.First installation of sg3-utils package is required.You can get more information on sg3-utils package by clicking on below link:http://sg.danny.cz/sg/sg3_utils.htmlSo first step is to install sg3-utils package using command: sudo apt-get install sg3-utilsE.g. a-saurabh@a-saurabh:~$ sudo apt-get install sg3-utilsNow once the package is installed , we get access to numerous and helpful utilities which makes possible to send SCSI commands from terminal to SCSI devices.Suppose I want to see serial number of my hard disk, I can issue the below command.a-saurabh@a-saurabh:~$ sudo sg_inq -p 0x80 /dev/sdaVPD INQUIRY: Unit serial number page Unit serial number: WD-WMC2E7086390

Here we query about the supported inquiry pages by the device.We use -p option in the sg_inq command for specifying the page which is being queried,Likewise we can request information for more inquiry pages from SCSI devices.

Tuesday, May 20, 2014

Below script reads an input file line by line and displays each line with a half a second delay:

#Please provide file to be read as input while executing script

#!/bin/bash

count=$(cat $1 | wc -l)

for (( i=1; i<=$count; i++ ))

do

value=$(cat $1 | head -$i | tail -1)

echo $value

sleep 0.5

done

Above script reads each line and stores the same in value variable and henceforth value can be utilized for many other manipulations.
There are many other standard methods of reading a file specially with read line in while loop but I had prepared my own method to read the file. :)

Thursday, April 17, 2014

Before pushing you kernel code to GIT you need to make the code error and warning free.There are some standard scripts in linux kernel repository to point out the errors and warnings.One of them is checkpatch.pl script.My shell scripts will try to clean some of these pointed error and warnings.Here's my first script to remove CamelCase from kernel code.Please refer to the link for details on CamelCase: http://en.wikipedia.org/wiki/CamelCaseShell Script for removing CamelCase from code:#!/bin/bash#Here, arg#1 is string whose CamelCase needs to be removed.#arg2 is the file in which CamelCase needs to be removed.var1=$1var2=$(echo $var1 | tr [:upper:] [:lower:])sed -i "s/\<$var1\>/$var2/g" $2Above scripts actually converts the CamelCases in your file to lower cases so you will not get any warning now with checkpatch.pl