Do you have a question? Post it now! No Registration Necessary
Subject
- Posted on
inb & outb woes...
- 07-11-2003
July 11, 2003, 4:39 pm

I'm trying to write a short program that will display/modify I/O ports
depending on the number of command line args... this is for a PC104
board I'm tinkering with. It's not working as expected and I can't
find documentation more in depth than the man page... here's the
code... yeah, it's crude...
#include<stdio.h>
#include<asm/io.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
int main(int argc, char **argv)
{
unsigned int addr, val;
switch (argc) {
case 2:
addr = strtol(argv[1], (char**)NULL, 16);
if (addr < 0x400 && ioperm(addr, 1, 1) < 0) {
printf("ioperm failed: %s\n", strerror(errno));
return -1;
} else if (iopl(3) < 0) {
printf("iopl failed: %s\n", strerror(errno));
return -1;
}
printf("address 0x%x : value 0x%02x\n", addr, inb(addr));
break;
case 3:
addr = strtol(argv[1], (char**)NULL, 16);
val = strtol(argv[2], (char**)NULL, 16);
if (addr < 0x400 && ioperm(addr, 1, 1) < 0) {
printf("ioperm failed: %s\n", strerror(errno));
return -1;
} else if (iopl(3) < 0) {
printf("iopl failed: %s\n", strerror(errno));
return -1;
}
outb(val&0xff, addr);
break;
default:
printf("invalid args\n");
return -1;
break;
}
return 0;
}
For what it's worth, I've tried compiling with both -O and -O2, I run
as root, etc. Any suggestions, help, references to documentation or
anything else would be greatly appreciated. TIA
depending on the number of command line args... this is for a PC104
board I'm tinkering with. It's not working as expected and I can't
find documentation more in depth than the man page... here's the
code... yeah, it's crude...
#include<stdio.h>
#include<asm/io.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
int main(int argc, char **argv)
{
unsigned int addr, val;
switch (argc) {
case 2:
addr = strtol(argv[1], (char**)NULL, 16);
if (addr < 0x400 && ioperm(addr, 1, 1) < 0) {
printf("ioperm failed: %s\n", strerror(errno));
return -1;
} else if (iopl(3) < 0) {
printf("iopl failed: %s\n", strerror(errno));
return -1;
}
printf("address 0x%x : value 0x%02x\n", addr, inb(addr));
break;
case 3:
addr = strtol(argv[1], (char**)NULL, 16);
val = strtol(argv[2], (char**)NULL, 16);
if (addr < 0x400 && ioperm(addr, 1, 1) < 0) {
printf("ioperm failed: %s\n", strerror(errno));
return -1;
} else if (iopl(3) < 0) {
printf("iopl failed: %s\n", strerror(errno));
return -1;
}
outb(val&0xff, addr);
break;
default:
printf("invalid args\n");
return -1;
break;
}
return 0;
}
For what it's worth, I've tried compiling with both -O and -O2, I run
as root, etc. Any suggestions, help, references to documentation or
anything else would be greatly appreciated. TIA

Re: inb & outb woes...

If addr >= 0x400, ioperm() is never called and then the iopl() in
the else-if clause will fail. If you change the '&&' to '||' (which
also would make more sense) it probably will work.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| snipped-for-privacy@physik.fu-berlin.de
_ _____ _____
| ||_ _||_ _| snipped-for-privacy@physik.fu-berlin.de
We've slightly trimmed the long signature. Click to see the full one.

Re: inb & outb woes...
snipped-for-privacy@physik.fu-berlin.de wrote in message

This is by design... ioperm sets the port access permission bits for
the first 0x3ff i/o ports. If I have an address > 0x3ff, then the
plan is to call iopl (rather than ioperm), which, according to the man
page, grants access to all the i/o ports. At least this is my
understanding from the man pages...


This is by design... ioperm sets the port access permission bits for
the first 0x3ff i/o ports. If I have an address > 0x3ff, then the
plan is to call iopl (rather than ioperm), which, according to the man
page, grants access to all the i/o ports. At least this is my
understanding from the man pages...


Re: inb & outb woes...

Sorry, I was a bit too careless. So I now compiled your program (after
removing '#include<asm/io.h>' and replacing it by '#include <sys/io.h>'
(that's where ioperm() and iopl() are declared) and adding '#include
<stdlib.h>' (for strtol()) to keep the compiler from complaining) and,
if I run it as root, it works without any error messages getting
output, both when I read and write. What exactly are you expecting it
to do and what happens?
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| snipped-for-privacy@physik.fu-berlin.de
_ _____ _____
| ||_ _||_ _| snipped-for-privacy@physik.fu-berlin.de
We've slightly trimmed the long signature. Click to see the full one.
Site Timeline
- » uCLinux on Samsung S3C4510B (ARM7TDMI) based wireless router
- — Next thread in » Embedded Linux
-
- » Re: Have ext3 on SanDisk CF but can't disable write-back caching as kernel instructs
- — Previous thread in » Embedded Linux
-
- » Crosscompiling for ARM: reloc type R_ARM_ABS32 is not supported for PIC - ...
- — Newest thread in » Embedded Linux
-
- » Condensateur de démarrage 25µF pour remplacer u n 10µF ?
- — The site's Newest Thread. Posted in » Electronics (French)
-