LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-19-2008, 07:38 PM   #1
AustinMarton
Member
 
Registered: May 2007
Location: New Zealand
Distribution: Fedora at home, Ubuntu 10.04 at work
Posts: 88

Rep: Reputation: 16
Need help understanding how a device driver interacts with user space programs


Hello,
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:
Code:
fh = open("/dev/zl504xx",O_RDWR);
   if (fh)

   {

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

     close(fh);
   }

   else

     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:
Code:
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!!
 
Old 11-21-2008, 04:29 AM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Linux device driver programming can be a bit tricky. I would recommend you to read http://lwn.net/Kernel/LDD3/, 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:

Code:
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.
 
  


Reply

Tags
device, drivers, kernel, modules, nios, uclinux, userspace


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sharing Memory between driver space and user space ian b davidson Programming 1 11-10-2008 06:35 AM
User Space Device Driver Development ameya.verma Linux - Software 0 04-02-2008 02:26 AM
Interfacing a USB device driver into user space? ddraper Linux - Kernel 2 02-19-2008 04:33 PM
User Space Device Driver jvogel Linux - Kernel 4 10-16-2007 05:46 PM
on modules and user-space programs h/w Programming 2 01-06-2004 03:42 PM


All times are GMT -5. The time now is 01:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration