Renjith G Asked: 2011-01-19 02:51:35 +0800 CST2011-01-19 02:51:35 +0800 CST 2011-01-19 02:51:35 +0800 CST Usage of inb, inl, inw to access kernel space from user space 772 Could you please give me some user space examples of using inb, inl, inw to access kernel space in ubuntu? /Kanu drivers 2 Answers Voted Stefano Palazzo 2011-01-19T04:41:30+08:002011-01-19T04:41:30+08:00 These functions (or macros) are defined in <asm/io.h>. You need to tell gcc to -Optimise the code for them to be inlined (I think). For their usage, type man inw Makelinux.net also has a guide on how to use them. I can't give you an example since it depends on the hardware you're using, but this should get you started. If anyone is interested in it, there's also a Python module, portio, to provide the same functionality. Kees Cook 2011-07-25T10:23:05+08:002011-07-25T10:23:05+08:00 To use IO port commands (in/out), you need to run as root and have reserved access via the ioperm() call: http://manpages.ubuntu.com/ioperm If this is beep.c: /* Copyright 2011, Kees Cook <[email protected]>, License: GPLv2 */ #include <unistd.h> #include <stdio.h> #include <sys/io.h> int main() { unsigned char orig, bits; /* gain access to speaker control port */ if (ioperm(0x61, 0x61, 1) < 0) { perror("0x61"); return 1; } /* gain access to speaker frequency port */ if (ioperm(0x42, 0x42, 1) < 0) { perror("0x42"); return 2; } /* turn on speaker */ orig = bits = inb(0x61); bits |= 3; outb(bits, 0x61); /* set 1000 Hz frequency */ bits = 0xA9; outb(bits, 0x42); bits = 0x04; outb(bits, 0x42); /* listen to the beep */ sleep(1); /* restore speaker bits to turn off speaker */ outb(orig, 0x61); return 0; } $ make beep $ sudo ./beep
These functions (or macros) are defined in
<asm/io.h>
. You need to tell gcc to-O
ptimise the code for them to be inlined (I think).For their usage, type
Makelinux.net also has a guide on how to use them.
I can't give you an example since it depends on the hardware you're using, but this should get you started.
If anyone is interested in it, there's also a Python module, portio, to provide the same functionality.
To use IO port commands (in/out), you need to run as root and have reserved access via the ioperm() call: http://manpages.ubuntu.com/ioperm
If this is
beep.c
:$ make beep
$ sudo ./beep