Linux Driver Development How-to: Get Started Live!

06 Aug



Ever wanted to get into the world of Linux Driver Development and started with LDD book:

  1. Then looked at the ‘Hello World’ driver module, finished it successfully. Later after introspection found it was too easy and did not learn much ‘critical’!
  2. After reading a few pages, eventually realized its not for you and left the journey?

If this is the case, go ahead and read further..

Well, its not that easy or that difficult. However ‘careful’ is the word one should always remember, as any small misadventure can ruin the system. Just follow this:

  • Take a deep breath
  • Just understand what you are doing
  • Leave the mentality of user space development, outside the room
  • Follow the best practices in the kernel space development

Here, I’m going to take a live module from Linux kernel space as an example to get started and show how to tweak and learn more in the process of ‘Linux Driver Development’!

  • SDIO UART Driver

    You can find this driver module at drivers/mmc/card/sdio_uart.c in the Linux source. Thanks to Nicolas Pitre and Co. this driver module is a loadable kernel module, which means this driver you can tweak, build, and install, all this.. while the kernel is running!

    You can download the Linux kernel source from Untar the tar ball at a preferred location. Then create a directory, say ~/ldd_live, and copy the sdio_uart.c from drivers/mmc/card/ location.

    Now we’ve got the source. However we need to tweak and then build, for that we need a Makefile. Let’s create it.

    I have my Linux kernel source at /home/thecottonsilk/linux-2.6.36/. So, my Makefile uses the source path as below.

    File Name:Makefile

    obj-m += sdio_uart.o
    all :
             make -C /home/thecottonsilk/linux-2.6.36/    SUBDIRS=$(PWD)    modules
    clean :
             make -C /home/thecottonsilk/linux-2.6.36/    SUBDIRS=$(PWD)    clean

    As you would know, this Makefile, allows two possibilities:

    1. To build the driver module, and
    2. To clean the driver binary and other intermediate files, as needed.

    Now let’s build the driver module to confirm Makefile works.
    Follow the command under the ldd_live:

    >cd ~/ldd_live
    sdio_uart.c Makefile

    You can see, Makefile creates many intermediate files and then the required file sdio_uart.ko. This .ko file is the one which would help us load the SDIO UART module.

  • Install the LKM

    Use following command to first remove the existing SDIO UART module if any.

    >rmmod sdio_uart

    Use following command to install the SDIO UART LKM.

    >insmod ./sdio_uart.ko

  • Important SDIO UART Driver APIs

    Important meaning important for this article, otherwise all the APIs are required and that is why it exists there! SDIO UART driver has two APIs sdio_uart_receive_chars() and sdio_uart_transmit_chars() for reading and writing data to a SDIO client respectively.

    You can refer the source for further details.

  • Tweak SDIO UART Driver

    Here, I will just go by ‘Hello World’ example way, and add only one ‘printf’, ouch..I’m sorry, old habits die hard! As you would know, at Kernel space we use ‘prink’ statement to output a string.

    So, here we go:

    Add following printk statement under sdio_uart_transmit_chars() as below:

    len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
    printk(KERN_DEBUG “Attempting to write %d byte(s) \n”, len);

    Let’s save the changes and build module.

    > make clean
    > make

    Let’s install the LKM.

    > rmmod sdio_uart
    > insmod ./sdio_uart.ko

  • Test SDIO UART Driver

    Now that the LKM is loaded, let’s verify that whatever change we made is working inside the kernel or not. This can be done by inserting a SDIO card into the SD/SDIO slot of your system (I’m assuming your system has one).

    Let’s confirm the SDIO card is initialized, by using following ‘dmesg’ command:

    > dmesg

    If the SDIO card is successfully initialized, at the end of the dmesg output you should see the acknowledgement of SDIO card being recognized. Otherwise, you should see some error message because of timeout error, etc. One thing can be tried out under such circumstances is to re-insert the card and verify.

    For more details, you can go through following references:

    1. SDIO Specification Version 2.0
    2. To understand the SDIO protocol.

    3. SDIO info
    4. Visit the SD/SDIO website for more information on SDIO.

    5. drivers/mmc/card/sdio_uart.c
    6. Best documentation is the source – people say. I agree very minimally. That statement is true provided the code is well-written. Well, the SDIO UART driver is easy to understand, provided you know the details like, tty layer, SDIO protocol, other dependent source under driver/mmc/core, etc.

    7. LDD Book
    8. To understand the intricacies of the driver development.

Fine then, ALL THE BEST!

Intentionally I have left some of the minor details, so that there will be some queries in the comments! 🙂


Posted by on August 6, 2011 in Linux, Technical


Tags: , , , ,

2 responses to “Linux Driver Development How-to: Get Started Live!

  1. ripple

    August 7, 2011 at 11:31 am

    Awesome information here.. Good work… Will keep tab on this post.. Thanks

    • maynature

      August 8, 2011 at 3:45 am

      Thank you, ripple!
      Critical comments are always welcome to improve the sharing.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: