/dev/port - how does it work?

/dev/port - how does it work?

Post by Martin Duspiv » Sun, 17 Sep 2000 04:00:00



Hello!

I need a direct access to some I/O ports but when I do outb(..) my app
crashes. I've read that /dev/port can be used, but I don't know how.
If I want to write byte "b" to port "p" how can I do it??
Please, help me!

Thanks

 
 
 

/dev/port - how does it work?

Post by arne » Sun, 17 Sep 2000 04:00:00



> Hello!

> I need a direct access to some I/O ports but when I do outb(..) my app
> crashes. I've read that /dev/port can be used, but I don't know how.
> If I want to write byte "b" to port "p" how can I do it??
> Please, help me!

> Thanks

It depends on what kind of device you're trying to access.

The standard way for communication with a device is to have a device
driver (either compiled into the kernel or a module) associate a major
and a minor number to the device. These numbers simply state what kind
of device you have, the major number divides all devices into classes
such as 'pointer devices', 'disks', etc.

You can't write to I/O ports directly in Linux, since this would violate
all protection mechanisms, and in a multi-user environment this would
create absolute mayhem. Your device driver defines what you can do with
the device, and all actions go through it.

When your driver has been loaded, you can create a 'device' file under
/dev, my doing mknod. You must tell mknod which major and minor number
to use, and it will create an ordinary file.
When you open this file and write to it, all data will go to the driver
and (hopefully) to your device. Same thing when you read from it.
Depending on what kind of device you have, you might want to do other
things than reading and writing, which can be done through ioctl().
Exactly what ioctldoes depends on what the driver allowes you to do
through it, it's not a generic function like open, read and write.
There's much more to it than this, writing device drivers is an art in
itself, but this is basically what it comes down to.

If you're trying to write "reset" to your serial port for instance, a
simple "echo reset > /dev/ttyS0" will do the trick. In C, you would do

        handle = open("/dev/ttyS0");
        write(handle,"reset",5);
        close(handle);

--
Arne Joris
#603 9747 106st
T5K 1B4 Edmonton, AB


 
 
 

/dev/port - how does it work?

Post by Nate Eldredg » Sun, 17 Sep 2000 04:00:00



> Hello!

> I need a direct access to some I/O ports but when I do outb(..) my app
> crashes. I've read that /dev/port can be used, but I don't know how.
> If I want to write byte "b" to port "p" how can I do it??

Read the IO-Port-Programming mini-HOWTO.

--

Nate Eldredge

 
 
 

/dev/port - how does it work?

Post by Martin Duspiv » Sun, 17 Sep 2000 04:00:00


Quote:

>You can't write to I/O ports directly in Linux, since this would violate
>all protection mechanisms, and in a multi-user environment this would
>create absolute mayhem.

I've just experimented this way:
    f = open("/dev/port", O_RDWR);
    lseek(f, port, SEEK_SET);
    write(f, value, sizeof(char));
and it WORKED !!
(I ran it as root, because of access rights to /dev/port...)

Quote:>Your device driver defines what you can do with
>the device, and all actions go through it.

>When your driver has been loaded, you can create a 'device' file under
>/dev, my doing mknod. You must tell mknod which major and minor number
>to use, and it will create an ordinary file.
>When you open this file and write to it, all data will go to the driver
>and (hopefully) to your device. Same thing when you read from it.
>Depending on what kind of device you have, you might want to do other
>things than reading and writing, which can be done through ioctl().
>Exactly what ioctldoes depends on what the driver allowes you to do
>through it, it's not a generic function like open, read and write.

I know about this way. But I'm writing a software for a PIC microcontrollers
toolkit and I don't wanna complicate it by writing a kernel module. All of
this project is experimental anyway....
 
 
 

/dev/port - how does it work?

Post by Keith Wrigh » Mon, 18 Sep 2000 13:08:08



> >You can't write to I/O ports directly in Linux, since this would violate
> >all protection mechanisms, and in a multi-user environment this would
> >create absolute mayhem.

> I've just experimented this way:
>     f = open("/dev/port", O_RDWR);
>     lseek(f, port, SEEK_SET);
>     write(f, value, sizeof(char));
> and it WORKED !!
> (I ran it as root, because of access rights to /dev/port...)

Good stuff!

Quote:

> >Your device driver defines what you can do with
> >the device, and all actions go through it.

> >When your driver has been loaded, you can create a 'device' file under
> >/dev, my doing mknod. You must tell mknod which major and minor number

       <and so forth>

Quote:> I know about this way. But I'm writing a software for a PIC microcontrollers
> toolkit and I don't wanna complicate it by writing a kernel module. All of
> this project is experimental anyway....

What arne told you was basically right.  What he forgot to say is
that it has already been done.  To see the driver for /dev/port
look for "port_fops" in /usr/src/linux-2.???/drivers/char/mem.c.

So if you need to experiment with changes, or just understand what's
happening you can start with that code.
--

Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
         ---  Food, Shelter, Source code.  ---

 
 
 

/dev/port - how does it work?

Post by Victor Wagn » Mon, 18 Sep 2000 04:00:00


:>
:> I need a direct access to some I/O ports but when I do outb(..) my app
:> crashes. I've read that /dev/port can be used, but I don't know how.
:> If I want to write byte "b" to port "p" how can I do it??
:> Please, help me!
:>
:> Thanks

: It depends on what kind of device you're trying to access.

: The standard way for communication with a device is to have a device
: driver (either compiled into the kernel or a module) associate a major
: and a minor number to the device. These numbers simply state what kind
: of device you have, the major number divides all devices into classes
: such as 'pointer devices', 'disks', etc.

: You can't write to I/O ports directly in Linux, since this would violate

Not "cannot" but basically "shouldnot". There are exceptions. X server
is most notable one. It is user space program, which accesses videocard
directly, and typically in time-critical manner.

So, it uses outb and inb. Moreover, if you are developing driver for
some new hardware it might be desirable to debug all the
device-communication issues in user-space program before converting it
to kernel module.

So, there _IS_ the way to access ports from user-space.

See ioperm system call.

There is even special document on this topic - Linux IO-Port Programming
HOWTO.
`

--

 
 
 

/dev/port - how does it work?

Post by n.elliot » Fri, 13 Oct 2000 04:00:00



> Hello!

> I need a direct access to some I/O ports but when I do outb(..) my app
> crashes. I've read that /dev/port can be used, but I don't know how.
> If I want to write byte "b" to port "p" how can I do it??
> Please, help me!

> Thanks

You need to give more information.
how about including the essential part of your code.?

Have you looked at the mini HOWTO's ?
There is one about io port access.

Essentially you need ioperm for the ports you need to access
and root privileges to do this.
hth
norm

 
 
 

1. LILO, DOS on /dev/hda1, LINUX /dev/hdb1

How must LILO be configured to successfully boot Linux, when drive
hda is totally DOS, and drive hdb is totally Linux?  If boot is from
BMR (hda), it tries to load Linux from hda -- of course it fails.  If boot
is from root Linux (hdb), it doesn't even see it (never looks);
boots straight from DOS.

There must be a parameter somewhere....



2. .F comression ?

3. What is the difference between /dev/sg0, /dev/scd0 /dev/hdd and /dev/cdrom ?

4. Basic network problem

5. Parallel port /dev files don't work

6. Help! XF86 on compaq prolinea 4/33

7. Why doesn't the serial port /dev/cua3 work?

8. Mitsumi 1X problem

9. /dev/dsp, /dev/audio, /dev/midi., /dev/sndsta

10. serial ports work with DOS but don't probe with FreeBSD 2.2.2

11. DTC 2130D 2nd EIDE Port only works after booting with DOS

12. /dev/midi, /dev/sequencer do not work

13. /dev/cdrom (or /dev/sbpcd) not working