Device Drivers, Part 7: Generic Hardware Access in Linux

Accessing hardware in Linux

This article, which is part of the series on Linux device drivers, talks about accessing hardware in Linux.

Shweta was all jubilant about her character driver achievements, as she entered the Linux device drivers laboratory on the second floor of her college. Many of her classmates had already read her blog and commented on her expertise. And today was a chance to show off at another level. Till now, it was all software — but today’s lab was on accessing hardware in Linux.

In the lab, students are expected to learn “by experiment” how to access different kinds of hardware in Linux, on various architectures, over multiple lab sessions. Members of the lab staff are usually reluctant to let students work on the hardware straight away without any experience — so they had prepared some presentations for the students (available here).

Generic hardware interfacing

As every one settled down in the laboratory, lab expert Priti started with an introduction to hardware interfacing in Linux. Skipping the theoretical details, the first interesting slide was about generic architecture-transparent hardware interfacing (see Figure 1).

Hardware mapping

Figure 1: Hardware mapping

The basic assumption is that the architecture is 32-bit. For others, the memory map would change accordingly. For a 32-bit address bus, the address/memory map ranges from 0 (0x00000000) to “232 – 1″ (0xFFFFFFFF). An architecture-independent layout of this memory map would be like what’s shown in Figure 1 — memory (RAM) and device regions (registers and memories of devices) mapped in an interleaved fashion. These addresses actually are architecture-dependent. For example, in an x86 architecture, the initial 3 GB (0x00000000 to 0xBFFFFFFF) is typically for RAM, and the later 1GB (0xC0000000 to 0xFFFFFFFF) for device maps. However, if the RAM is less, say 2GB, device maps could start from 2GB (0x80000000).

Run cat /proc/iomem to list the memory map on your system. Run cat /proc/meminfo to get the approximate RAM size on your system. Refer to Figure 2 for a snapshot.

Physical and bus addresses on an x86 system

Figure 2: Physical and bus addresses on an x86 system

Irrespective of the actual values, the addresses referring to RAM are termed as physical addresses, and those referring to device maps as bus addresses, since these devices are always mapped through some architecture-specific bus — for example, the PCI bus in the x86 architecture, the AMBA bus in ARM architectures, the SuperHyway bus in SuperH architectures, etc.

All the architecture-dependent values of these physical and bus addresses are either dynamically configurable, or are to be obtained from the data-sheets (i.e., hardware manuals) of the corresponding architecture processors/controllers. The interesting part is that in Linux, none of these are directly accessible, but are to be mapped to virtual addresses and then accessed through them — thus making the RAM and device accesses generic enough. The corresponding APIs (prototyped in <asm/io.h>) for mapping and unmapping the device bus addresses to virtual addresses are:

void *ioremap(unsigned long device_bus_address, unsigned long device_region_size);
void iounmap(void *virt_addr);

Once mapped to virtual addresses, it depends on the device datasheet as to which set of device registers and/or device memory to read from or write into, by adding their offsets to the virtual address returned by ioremap(). For that, the following are the APIs (also prototyped in <asm/io.h>):

unsigned int ioread8(void *virt_addr);
unsigned int ioread16(void *virt_addr);
unsigned int ioread32(void *virt_addr);
unsigned int iowrite8(u8 value, void *virt_addr);
unsigned int iowrite16(u16 value, void *virt_addr);
unsigned int iowrite32(u32 value, void *virt_addr);

Accessing the video RAM of ‘DOS’ days

After this first set of information, students were directed for the live experiments. The suggested initial experiment was with the video RAM of “DOS” days, to understand the usage of the above APIs.

Shweta got onto the system and went through /proc/iomem (as in Figure 2) and got the video RAM address, ranging from 0x000A0000 to 0x000BFFFF. She added the above APIs, with appropriate parameters, into the constructor and destructor of her existing “null” driver, to convert it into a “vram” driver. Then she added the user access to the video RAM through read and write calls of the “vram” driver; here’s her new file — video_ram.c:

#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <asm/io.h>

#define VRAM_BASE 0x000A0000
#define VRAM_SIZE 0x00020000

static void __iomem *vram;
static dev_t first;
static struct cdev c_dev;
static struct class *cl;

static int my_open(struct inode *i, struct file *f)
{
    return 0;
}
static int my_close(struct inode *i, struct file *f)
{
    return 0;
}
static ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off)
{
    int i;
    u8 byte;

    if (*off >= VRAM_SIZE)
    {
        return 0;
    }
    if (*off + len > VRAM_SIZE)
    {
        len = VRAM_SIZE - *off;
    }
    for (i = 0; i < len; i++)
    {
        byte = ioread8((u8 *)vram + *off + i);
        if (copy_to_user(buf + i, &byte, 1))
        {
            return -EFAULT;
        }
    }
    *off += len;

    return len;
}
static ssize_t my_write(struct file *f, const char __user *buf, size_t len, loff_t *off)
{
    int i;
    u8 byte;

    if (*off >= VRAM_SIZE)
    {
        return 0;
    }
    if (*off + len > VRAM_SIZE)
    {
        len = VRAM_SIZE - *off;
    }
    for (i = 0; i < len; i++)
    {
        if (copy_from_user(&byte, buf + i, 1))
        {
            return -EFAULT;
        }
        iowrite8(byte, (u8 *)vram + *off + i);
    }
    *off += len;

    return len;
}

static struct file_operations vram_fops =
{
    .owner = THIS_MODULE,
    .open = my_open,
    .release = my_close,
    .read = my_read,
    .write = my_write
};

static int __init vram_init(void) /* Constructor */
{
    if ((vram = ioremap(VRAM_BASE, VRAM_SIZE)) == NULL)
    {
        printk(KERN_ERR "Mapping video RAM failed\n");
        return -1;
    }
    if (alloc_chrdev_region(&first, 0, 1, "vram") < 0)
    {
        return -1;
    }
    if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL)
    {
        unregister_chrdev_region(first, 1);
        return -1;
    }
    if (device_create(cl, NULL, first, NULL, "vram") == NULL)
    {
        class_destroy(cl);
        unregister_chrdev_region(first, 1);
        return -1;
    }

    cdev_init(&c_dev, &vram_fops);
    if (cdev_add(&c_dev, first, 1) == -1)
    {
        device_destroy(cl, first);
        class_destroy(cl);
        unregister_chrdev_region(first, 1);
        return -1;
    }
    return 0;
}

static void __exit vram_exit(void) /* Destructor */
{
    cdev_del(&c_dev);
    device_destroy(cl, first);
    class_destroy(cl);
    unregister_chrdev_region(first, 1);
    iounmap(vram);
}

module_init(vram_init);
module_exit(vram_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>");
MODULE_DESCRIPTION("Video RAM Driver");

Summing up

Shweta then repeated the usual steps:

  1. Build the “vram” driver (video_ram.ko file) by running make with a changed Makefile.
  2. Load the driver using insmod video_ram.ko.
  3. Write into /dev/vram, say, using echo -n "0123456789" > /dev/vram.
  4. Read the /dev/vram contents using od -t x1 -v /dev/vram | less. (The usual cat /dev/vram can also be used, but that would give all the binary content. od -t x1 shows it as hexadecimal. For more details, run man od.)
  5. Unload the driver using rmmod video_ram.

With half an hour still left for the end of the practical class, Shweta decided to walk around and possibly help somebody else with their experiments.

  • yuva

    Hi Anil ,

    I am reading your article from the beginning . I am happy with your sharing. Your article is really good .

    I have some queries please clarify.

    From the following link i came to know that there are three ways for addressing memory .

    http://tldp.org/LDP/khg/HyperNews/get/devices/addrxlate.html

    I want to understand what is “virtual address” and “bus address” clearly .

    Regards,

    A.Yuvaraj

    • http://sosaysharis.wordpress.com/ Haris Ibrahim K. V.

       Hi Yuva,

      I’m a bit confused regarding the ‘bus address’ terminology. There are ‘address buses’ that carry addresses between different devices. Basically a 16-bit system having 16 parallel wires to carry this. I believe this tech has been improved by multiplexing the addresses and hence using lesser number of wires in parallel.

      Where as ‘virtual address’ refers to the address space the processes have within the virtual memory. The concept of virtual memory can be viewed as ‘enlarging the capacity of RAM’. So the part of the processes that are needed for execution are in the RAM while those parts that are not currently needed will be in their corresponding virtual addresses ready to be mapped onto the RAM.

      My answer is not accurate and can be wrong too. Please discuss and let us reach at the correct answer.

      • http://twitter.com/anil_pugalia Anil Pugalia

        Bus Addresses are the addresses coming on the bus – e.g. on PCI Bus for x86 architecture. They are the real hardware addresses. Virtual addresses are only conceptual. They only exist in the page tables used by the MMU for ultimately getting translated into hardware addresses, which could be bus addresses or physical RAM addresses.

  • yuva

    Hi Anil ,

    I am reading your article from the beginning . I am happy with your sharing. Your article is really good .

    I have some queries please clarify.

    From the following link i came to know that there are three ways for addressing memory .

    http://tldp.org/LDP/khg/HyperNews/get/devices/addrxlate.html

    I want to understand what is “virtual address” and “bus address” clearly .

    Regards,

    A.Yuvaraj

  • http://sosaysharis.wordpress.com/ Haris Ibrahim K. V.

    I believe the meaning of the output of “od -t x1 -v /dev/vram | less” should be explained so as to how it relates with the “0123456789″ that we wrote into our driver.

    • http://www.facebook.com/lokesh.walasepatil Lokesh WalasePatil

      Yes, even I m not understanding what the o/p should be. Pls someone explain it !! :)

      • http://twitter.com/anil_pugalia Anil Pugalia

        Easier way for understanding the same would be to try xxd. Just do the following:
        xxd /dev/vram | less
        What exactly we are doing with all these, is reading & display the vide ram content, in more readable ways, like in hex, in ASCII, etc

  • mrr

    Worth to mention that if you couldn’t find “Video RAM” in /proc/iomem, try to run `lspci -v` and find video device info:

    bash# lspci -v
    00:02.0 VGA compatible controller: Cirrus Logic GD 5446
    Subsystem: Qumranet, Inc. Device 1100
    Flags: fast devsel
    Memory at e0000000 (32-bit, prefetchable) [size=32M]
    Memory at e2000000 (32-bit, non-prefetchable) [size=4K]
    Expansion ROM at e2010000 [disabled] [size=64K]
    Kernel modules: cirrusfb

    and with device number ’00:02.0′ you can find it now in /proc/iomem:

    bash# grep 00:02.0 /proc/iomem
    e0000000-e1ffffff : 0000:00:02.0
    e2000000-e2000fff : 0000:00:02.0
    e2010000-e201ffff : 0000:00:02.0

    Memory at 0xe0000000 is good enough for the article example.

    • http://twitter.com/anil_pugalia Anil Pugalia

      What you pointed out is correct. But we do not want to play & goof up with the ram of the currently being used video card. And that’s where, looking for the unused video ram from the “DOS days”, which is at the location as mentioned in the article.

  • Buk

    Great tutorials but it would be better if you could give the output screen after od -t x1 -v /dev/vram | less command. İ am not sure if i did any mistake because there isn’ t much difference between writing echo 0123456789 and echo 55

    • http://www.facebook.com/anil.pugalia Anil Pugalia

      If there is no change – that’s very much possible, if the underlying system doesn’t have that Video RAM physically, present.

  • sandeep

    excellent work sir please try to upload the same with linux internals also

    • http://www.facebook.com/anil.pugalia Anil Pugalia

      Thanks. And what do you mean by upload same with linux internals?

  • plr

    Hi Anil ,

    I had a doubt regarding the read function .

    ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off)

    here len tells the number of bytes to be read .

    and what is loff_t *off field meant for ?

    I thot it is the offset from which the byte has to be read . But
    if (*off >= VRAM_SIZE)
    {
    return 0;
    }

    gives a different meaning .

    here it refers to number of bytes to be read if Im not wrong…

    Please clarify regarding this… I kno its a very small thing but Im stuck right at this point…

    • plr

      hi sir, I understood it myself… Here off is the offset from the base address .
      (off > base address) means its going beyond the mapping for vram ….

      • anil_pugalia

        Yes, you understood correct – (off_from_base_address >= VRAM_SIZE) will go beyond the VRAM mapping.

  • SATHEESARAN

    Hi Anil,

    So whatever we write to this device driver is written to memory of the device [bus address], so how would the visual display unit interprets it and shows in the screen ?

    One more thing is, I could not understand the data when I read from video driver.

    I wrote , “0123456789″ to driver and I read from driver using the command,

    xxd.

    Output goes like this,
    0000000: 0100 0000 3435 3637 3839 0000 0000 0000 ….456789……

    How could I interpret the pattern “0123456789″ from above read input ?

    Thanks,
    Satheesaran

    • http://twitter.com/anil_pugalia Anil Pugalia

      Centre is the ASCII values in hex, and right is the characters.

  • linux_bot

    how is VRAM_SIZE = 0×00020000 is calculated, when subtracted from
    0x000BFFFF – 0x000A0000 = 1ffff

    • anil_pugalia

      You need to add 1 to the result of subtraction. Why? Here’s the logic: If an address starts from A0000 and ends at A0001, then they are 2 addresses and hence the size is 2; and we calculate as follows (A0001 – A0000) + 1 = 2.

  • srikanth

    static void __iomem *vram;
    what is __iomem ? I understood that vram is void pointer, but __iomem signifies what?

  • santosh yadav

    hello everyone!

    i am trying to do gpio access and encounter segmentation fault.

    so to overcome it i will have to map the physical address to logical and for that i made use of ioremap().

    but while i am using it the kernel is unable to locate the ‘asm/io.h’ header file

    so what should i do to include the header so that it may work.

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherwise noted.
LINUX For You is powered by WordPress, which gladly sits on top of a CentOS-based LEMP stack.

Creative Commons License.