-   Linux - General (
-   -   Need help understanding how a device driver interacts with user space programs (

AustinMarton 11-19-2008 07:38 PM

Need help understanding how a device driver interacts with user space programs
I am having trouble understanding the way that a device driver interacts with user space. We are writing a char driver to send configuration information to a switch IC (ZL50408) in an embedded system, running uClinux on a NIOS2 processor.

Someone has contributed a large portion of the driver, but I am unsure of the way it operates. I know every driver has the init and cleanup/release functions, they work when we load the module, and we are able to access registers and read/set the values by placing code in the init function.

Once the module has been loaded, when are the other functions called? When a user space application attempts to write to the file in the /dev directory, I assume the write function specified in the file_operations structure is what is called?

The author of the driver has made two different file_operations structures
so I am confused how the kernel knows which one to use if there is only one entry in the /dev directory?

I am trying to write a user space application to test the driver, I simply want to write and read from a few registers. This is an example I found in a book and have been playing around with:

fh = open("/dev/zl504xx",O_RDWR);
  if (fh)


    cnt = read(fh,bfr,1);
    printf("Data read: %x\n", bfr[0]);



    printf("Fail open device\n");

Is this read command what I want to be using?
I am confused because the read function in our driver has 4 arguments:

static ssize_t zl_read_cfg(struct file *filp, char __user *buf, size_t len, loff_t *off)
And my last point of confusion, we are wanting to have a user space application with does the switch management, it will set certain registers to change different settings. This seems quite different to the 'stream' nature of a file? For example, to examine the 5th byte in memory, would we access the 5th character in the file in /dev ?

Thanks, answers or hints to ANY of the above questions would be MUCH appreciated!!

vladmihaisima 11-21-2008 04:29 AM

Linux device driver programming can be a bit tricky. I would recommend you to read, or at least parts of it.

Now, about your questions, I would use the "ioctl" interface. For charachter device drivers this means a call that looks like:


int device_ioctl(struct inode *inode,struct file *file,unsigned int ioctl_num,unsigned long ioctl_param)
The first 2 parameters are related to the file the userspace application opened in /dev. If you have just one device and you access it from one application you can ignore them. So you have two parameters (the ioctl_num and ioctl_param) and a return value. You could define for ioctl_num some (arbitrary) values like IOCTL_REG_READ and IOCTL_REG_WRITE and do 'your work' in the function.

You can find more infos and examples in Chapter 6 from the first link.

All times are GMT -5. The time now is 07:46 PM.