How to access device from user space process
I have a conceptual question and hopefully this is the right forum for it!
In Linux, how can an user space process directly access a h/w device? (I know that a root process can do this)
Thanks a lot!!
How a device can be accessed depends upon the device type. Typically, control is via ioctl() calls. Some devices have memory space that can be mapped into a user process, provided the process has permissions of course.
For devices with memory-mapped registers, the device's physical address range can be mapped directly into a user process.
Does this give you an idea?
Thanks for the reply.
I conceptually do understand the idea of mapping the device's memory into user space memory. But what I don't understand is what happens next. If the process writes some data to that memory, how does the device know that there is some data waiting for it? The process can make an appropriate system call and let the kernel tell the device. But is it possible for the process to talk to the device directly without "kernel intervention".
This is a wide question, because different device classes behave differently, and have different characteristics and abilities.
There are two ways for a process to cross the user/kernel space: data transfer via system calls or direct memory mapping.
When using memory mapping, there are obvious synchronization concerns, and one has to code properly to ensure that your user process does not interfere with the device driver/device interaction. Again, how this is done very much depends upon the device. Generally, this type of coding is done, for example, to read a device's EEPROM values, or blast screen data into a frame buffer.
The device and device drivers aren't usually sitting around waiting for user space data; rather, they react to events such as interrupts, and the interrupt handler for the device does what it needs to do. They also respond (indirectly) to system calls, dispatched into the appropriate standard device entry point (eg. ioctl, read, write, flush, seek, etc.).
Devices are controlled via various hardware registers: for example, set a bit, and they do something, set another bit and they do something else, read a register, and the device returns the value and clears a register (clear on read). Some devices such as Ethernet devices have rings of buffers, where network data is loaded into the next available ring buffer, and the ring buffer next/last pointer is updated, and then the device responds. There are all sorts of combinations. This may be what you mean by "talk" to the device without kernel intervention.
If you really have an interest in this stuff, consider getting the Linux device driver book (O'Reilly) and study some device drivers. It will also *require* that you read a device's specification and programming interface.
|All times are GMT -5. The time now is 07:52 PM.|