2

Is it possible to mmap /dev/port? I'm getting 'No such device' when I try.

Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import mmap
>>> os.open('/dev/port', os.O_RDWR|os.O_NDELAY)
3
>>> mapfd = mmap.mmap(3, 0xfff)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
mmap.error: [Errno 19] No such device
>>> 

I've been able to mmap a regular file with the same options.

tMC
  • 18,105
  • 14
  • 62
  • 98

2 Answers2

4

Errno 19 is listed as "No such device" (Linux), or "Operation not supported by device" (FreeBSD).

Looking at the source code for /dev/port in drivers/char/mem.c, especially the struct file_operations, you'll see:

770 #ifdef CONFIG_DEVPORT
771 static const struct file_operations port_fops = {
772         .llseek         = memory_lseek,
773         .read           = read_port,
774         .write          = write_port,
775         .open           = open_port,
776 };
777 #endif

This device doesn't support mmap. Only opening, seeking, reading and writing.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
0

As has been pointed out, /dev/port isn't mmap-able. But seeing as how you're using python -- let's harness the true power of dynamic types! Why not create an mmap-like object which supports the same interface, but uses lseek underneath?

Brian Cain
  • 14,403
  • 3
  • 50
  • 88