: From: Hasan Mukti <[EMAIL PROTECTED]>
: Subject: [programming] Akses port melalui Linux
: Ada nggak ya command untuk mengakses port di unix / linux ini....
: Apakah command setserial bisa dipake untuk membaca device laen dengan
cara
: menghubungkan pake kabel di port serial masing-masing.
: Atau ada contoh source dari C ato perl ......
: Tolong .... penting sekali
kutipan di bawah ini mungkin membantu (*dikutip dari Linux Application
Development p.475*)
--- potong disini ---
A.1 Almost Portable I/O Port Access
The first (and slower) way to do I/O port access rpeserves some of the
kernel/user abstraction. The /dev/port device can easily be used to do some
I/O port access. For instance, if you have a device attached to a parallel
port and that device does not talk over the parallel port in the same way
as a paralle printer, the easiest way to control it might be a simple
program that writes to /dev/port. Consider a mythical garage door opener
remote control(*If you have a garage door opener remote control hooked up
to your computer, you are braver than either of the authors*) that is
activated by an approximately one second strobe on the parallel port served
by the /dev/lp1 device. The following shell script, run as root, would do
the trick.
#!/bin/sh
#toggle-garage-door - activates garage door remote control
#890 is the decimal address of /dev/lp1's control port
#(0x378 + 2)
echo -e '\001\ | dd of=/dev/port bs=1 count=1 skip=890
sleep 1
echp -e '\000\ | dd of=/dev/port bs=1 count=1 skip=890
that code set bit 1 of the control port. One second later it resets it(*We
do not recommended writing drievrs as shell scripts. We just wish to point
out that device drivers are not necessarily magic, incomprehensible kernel
code. The source code for device drivers in the kernel is much more readabl
ethat thtat shell script *). It is equivalent to the following C code,
which you may find more comprehensible even though it is longer:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(void) {
unsigned char command;
int fd;
fd = open("/dev/port", O_WRONLY, 0);
command = 0x01;
lseek(fd, 0x378 + 2, SEEK_SET);
write(fd, &command, 1);
sleep(1);
command = 0x00;
lseek(fd, 0x378 + 2, SEEK_SET);
write(fd, &command, 1);
return 0;
}
A.2 Direct I/O Port Access
It is possible to execute I/O instructions from user-mode program. However,
by default, doing so will cause a segmentation fault. You must first enable
I/O port access; then you can execute I/O instruction(*Yes, this
description is rather Intel-specific. however, other Linux platforms, even
those with CPUs that do not have I/O isntructions, provide the same
functions*). What you would like to do is something more like this:
#define control(port) (port + 2)
#define LP_STROBE 0x01
...
outb(LP_STROBE, control(ox378));
sleep(1);
outb(0, control(0x378));
but you need to get permission to call outb(). These are two ways to get
that permission: ioperm() and iopl().
A.2.1 The safe way
A program running with superuser previleges can separately enable and
disable access to each I/O port in the range 0-0x3ff. For example, to allow
access to the thress ports staring with 0x378(the orts used to access
/dev/lp1), use
ioperm(0x378, 3, 1);
If you need to access only port 0x37a, you can instead use
ioperm(0x37a, 1, 1);
The ioperm() function is used to allow and deny access to ports beetween 0
and 0x3ff(*The upper limit is 0x3ff because on Linux/i386, access rights
for the first 0x400 ports are stored in the task structure. Storing rights
for all the ports in each task structure would use a disproportionaly large
amount of memory (8KB per process), and most that can be usefully accessed
from user-level programs are low ones*). It takes three arguments:
* the first port of the range.
* the number of ports in the range
* 1 to allow access to ports in the range; 0 to deny access
A.2.2 The dangerous way
A program running with superuser previlages can enable all I/O port access
at once with a single call:
iopl(3)
this move the process into "I/O protection level 3," which does two things;
It allows the process all access to the I/O bus, and it allows the process
to disable and enable interrupts with cli() and sti(), respectively. If you
do use iopl(), make sure that you never call cli()--doing so will crash
symetric multi-processing (SMP) systems(*Do not call sti(), either--but it
probably will not hurt anything*). In general, use ioperm() if possible,
and use iopl() only if you need to access ports higher than 0x3ff.
--- potong disini ---
huuh, kepanjangan ya, hehehehe
: thanks
*Grins*
--------------------------------------------------------------------------------
Utk berhenti langganan, kirim email ke [EMAIL PROTECTED]
Informasi arsip di http://www.linux.or.id/milis.php3
Pengelola dapat dihubungi lewat [EMAIL PROTECTED]