inb & outb woes...

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
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

Re: inb & outb woes...
Quoted text here. Click to load it


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.

Quoted text here. Click to load it

                                        Regards, Jens
--
      _  _____  _____
     | ||_   _||_   _|         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
Quoted text here. Click to load it

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...

Quoted text here. Click to load it

Re: inb & outb woes...
Quoted text here. Click to load it

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
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
Quoted text here. Click to load it

Well, I wasn't able to get outb() to work correctly, although now it
seems that the main problem was between the chair and the keyboard :)
Works fine now (knock wood)  Thanks for your help

Site Timeline