Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are linux IO ports software abstractions or real hardware ports

I am learning linux device drivers. I learned about ports. I am confused that are ports in linux a software abstraction or are real hardware ports.

In the below device driver I wrote a code to allocate a port and to write or read.

The port is allocated even if there is no new hardware connected. What can be the reason?

Another problem is that when I write into the port using oub() and when I read from port using inb() then I always get the value 255 irrespective of what I wrote into the port.

#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/ioport.h>

struct resource *p;

static int start(void)
{
    printk("module registered\n");
    p=request_region(0x0062, 1, "my_port");
    if(p==NULL)
    {
            printk(KERN_ALERT "port allocation failed\n");
            return 0;
    }
    outb(12, 0x0062);
    return 0;
}

static void stop(void)    
{
    printk("module unregistered\n");
    unsigned a;
    a=inb(0x0062);
    printk("%d\n", a);
    release_region(0x0062, 1);
}

module_init(start);
module_exit(stop);
MODULE_LICENSE("GPL");

I allocate the port and write into it while inserting the module and read from it while unregistering the module.

Thanks in advance for any help

like image 776
Debesh Mohanty Avatar asked Aug 31 '25 01:08

Debesh Mohanty


1 Answers

Unfortunately, in the course of the years, CPU manufacturers did not agree on a single mechanism for accessing devices' registers. There are therefore two modes, and a single hardware architecture can implement one mode or the other. These two modes are

  • Port-mapped I/O (PMIO):
    • Separate address space for memory and I/O
    • Implemented by a few CPU manufacturers (x86 included)
    • Concept of I/O ports
  • Memory-mapped I/O (MMIO):
    • Same address space for memory and I/O
    • Most CPU manufacturers (e.g., ARM)
    • The difference between registers and memory is transparent to software
    • Concept of I/O memory

The book Linux Device Drivers 3rd edition (freely available on-line) illustrates this difference and how to access each kind of I/O from a kernel driver.

like image 138
Claudio Avatar answered Sep 02 '25 15:09

Claudio