Linux Device Drivers: Kernel to User Space communication

Inside Kernel space code:

To convert data from User space to Kernel space => copy_from_user
To send data from Kernel space to User space => copy_to_user

To read/write data to IO device below Device Driver(Firmware) => ioread32/iowrite32

The file_operations structure is defined in linux/fs.h, and holds pointers to functions defined by the driver that perform various operations on the device. Each field of the structure corresponds to the address of some function defined by the driver to handle a requested operation.
C99 way of assigning to elements of a structure.  You should use this syntax in case someone wants to port your driver. It will help with compatibility:

    struct file_operations fops = {
       .read = device_read,
       .write = device_write,
       .open = device_open,
       .release = device_release
    };

debugfs is a special filesystem (technically referred as a kernel space-user-space interface) available in Linux kernel since version 2.6.10-rc3. It is a simple to use RAM-based file system specially designed for debugging purposes. debugfs exists as a simple way for kernel developers to make information available to user space.


Inside User space code:

Open device directly from User space and read/write. 
example: open(“/dev/device_name”, O_RDWR);

Shared Memory:

Shared memory in user space => mmap(), munmap()
Shared memory in kernel space => remap_pfn_range()

PCI Device Drivers:

pci_register_driver - register a new pci driver

pci_unregister_driver - unregister a new pci driver

register_netdev - network device drivers

misc_register - char device drivers
register_blkdev - block device drivers 

module_init - driver initialization entry point

module_exit - driver exit entry point

Miscellaneous commands:

lspci –k => lists all pci device drivers
lsmod => lists all Loadable kernel modules(LKM)
modprobe => add/remove a kernel module

ref:

User space memory access from Kernel => http://www.ibm.com/developerworks/library/l-kernel-memory-access/


Linux Kernel to User space communication(copy_from-user, copy_to_user) -  http://www.makelinux.net/ldd3/chp-3-sect-7


Google Research(papers ...) - http://research.google.com/

Kernel to User space communication - http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-2.html

Use mmap() shared memory instead of copy_to_user => http://www.linuxforums.org/forum/kernel/158548-avoid-memory-copying-between-user-space-kernel-space.html

Debugfs - 
Linux Device Driver Overview - http://inbasudhakar.blogspot.com/2011/08/pci-driver-flow.html

Practical and effective sandboxing for non-root users -  http://people.csail.mit.edu/nickolai/papers/kim-mbox.pdf


packetdrill: Scriptable Network Stack Testing, from Sockets to Packets - http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en/us/pubs/archive/41316.pdf


http://linuxforthenew.blogspot.com/2013/01/why-doshould-we-use-copyfromuser-or.html