LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2005, 03:09 AM   #1
halturata
Member
 
Registered: Aug 2005
Location: Sofia
Distribution: SuSE, ELinOS
Posts: 100

Rep: Reputation: 16
Which is the best way for accessing device files in C?


Hi there,

I have to write a small programm in C which talks to a VMEbus and receives respond, but I don't know which is the best way for accessing the particular vme_* device files. Is there a special C function for doing this or the standart I/O functions fopen(), fclose() etc. will also do? Can anyone give me some hints?

Thanks.
 
Old 09-06-2005, 04:26 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
You can use standard I/O routines to access device files. That's open()/close()/read()/write()/seek()/tell()/ioctl() or fopen()/fclose()/fread()/fwrite()/fseek()/ftell() depending on if you want software buffering or not.

The only thing you can't do with buffering is ioctl(), which sends special messages to the hardware device. It doesn't really make much sense to do this on buffered streams, because you don't know where the hardware is up to in processing the stream; although you can still use ioctl() by digging the file descriptor out of the FILE structure.

The reason for this is that Linux is a UNIX system, which was originally designed in C. C only provides functions for accessing files, not specific hardware devices, and the device file was created as a way of abstracting away all the hardware-specific code and making a standard form of access method that was so simple people could actually stick to it.
 
Old 09-06-2005, 04:55 AM   #3
halturata
Member
 
Registered: Aug 2005
Location: Sofia
Distribution: SuSE, ELinOS
Posts: 100

Original Poster
Rep: Reputation: 16
Quote:
Originally posted by rjlee
The only thing you can't do with buffering is ioctl(), which sends special messages to the hardware device.
What kind of special messages are those, and what they are used for? I need just an overview, that's all.
Thanks a lot!
 
Old 09-06-2005, 06:04 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
check the man page for ioctl_list
 
Old 09-06-2005, 07:59 AM   #5
halturata
Member
 
Registered: Aug 2005
Location: Sofia
Distribution: SuSE, ELinOS
Posts: 100

Original Poster
Rep: Reputation: 16
And what about this - when I create a new device file with the command
Code:
[root@host]# mknod -m755 testdev c 240 0
and then try to access it, I get the following message:
Code:
[root@host]# less -f testdev
testdev: No such device
Whats wrong with it? Or there is another way to see what's inside a device file?
 
Old 09-06-2005, 08:51 AM   #6
halturata
Member
 
Registered: Aug 2005
Location: Sofia
Distribution: SuSE, ELinOS
Posts: 100

Original Poster
Rep: Reputation: 16
An still the device file won't open and I don't know why:
Code:
int main(void){
...   
if((fopen("/dev/testdevl", "r+"))==NULL){
    perror("fopen()");
    printf("Cannot open file.\n");
    exit(1);
   }
...
}
This code returns:
Code:
fopen(): No such file or directory
Cannot open file.
And the problems is NOT that the 'testdev' device does not exist. It is present in /dev. Any ideas what is wrong with it?
 
Old 09-06-2005, 04:45 PM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
If you create a device node that points to a device that is not really there what else
were you expecting?

computers only do what there told to.
 
Old 09-07-2005, 04:25 AM   #8
halturata
Member
 
Registered: Aug 2005
Location: Sofia
Distribution: SuSE, ELinOS
Posts: 100

Original Poster
Rep: Reputation: 16
I see my stupid mistake now.
And how you make it so that the device node points to a particular device? I mean is there a command for this or so?
Thanks.
 
Old 09-07-2005, 07:17 AM   #9
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
Each device has a “major” and “minor” device number. You can pass these numbers as arguments to mknod when you create the device file node in the first place.

You can get the numbers to use from the source code for the device driver of the device you're using; many of the standard numbers are listed in the file /usr/src/linux/Documentation/devices.txt
 
Old 09-07-2005, 11:31 AM   #10
halturata
Member
 
Registered: Aug 2005
Location: Sofia
Distribution: SuSE, ELinOS
Posts: 100

Original Poster
Rep: Reputation: 16
The major and minor numers are OK, the problem is that when I try to load the driver module I get the following messages:
Code:
[root@host]# insmod ca91c042.o
Warning: ca91c042.o will taint the kernel: no license
ca91c042.o: init_module: Device or resource busy
Hint: insmod errors can be caused by incorrect module parameters, including invalid IO or IRQ parameters.
What's that "Device or resource busy"? I have created all the necessary devices...
And what about these IO and IRQ parameters? Are they to be changed somehow?
Any idea what is wrong (except me being a luser)?
 
Old 09-07-2005, 11:58 AM   #11
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
A device is a physical object that plugs (or wires) in to the computer. You've created a device-special file.

The IO and IRQ parameters are parameters passed to the kernel module to tell it which I/O port and which IRQ line to use. You'll find out the values to use from the documentation and/or jumper settings on the hardware, although most devices for the last few years will allow you to auto-detect these values. See the insmod man-page for details on passing command-line parameters to modules.

Make sure that no other module to drive this piece of hardware (device) are loaded into the kernel when you try to load this module.
 
Old 09-08-2005, 04:18 AM   #12
halturata
Member
 
Registered: Aug 2005
Location: Sofia
Distribution: SuSE, ELinOS
Posts: 100

Original Poster
Rep: Reputation: 16
Quote:
Originally posted by rjlee
Make sure that no other module to drive this piece of hardware (device) are loaded into the kernel when you try to load this module.
How can I check this? I mean when I try to insert the module and the system complains that the device is busy, does this mean that there is already a module loaded into the kernel for that particular device?

Last edited by halturata; 09-08-2005 at 04:19 AM.
 
Old 10-09-2018, 02:09 AM   #13
Siddhi Verma
LQ Newbie
 
Registered: Oct 2018
Posts: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by halturata View Post
I see my stupid mistake now.
And how you make it so that the device node points to a particular device? I mean is there a command for this or so?
Thanks.
Could you kinldy tell, how did you solve this issue? Even I am facing the same problem
 
Old 10-09-2018, 01:58 PM   #14
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,764
Blog Entries: 6

Rep: Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585
You have replied to a thread which has been abandoned for 13 years, please do not do that.

To get exposure to currently active members please post your own question with the details specific to your use case in a new thread.

Please review the Site FAQ for guidance in asking well formed questions.

Welcome to LQ!
 
Old 10-09-2018, 02:44 PM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,589

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
(Anyways it would be good idea to explain what kind of hardware you have -- I mean device drivers usually work in conjuction with some piece of hardware. (Actully, these questions don't strictly belong to programming.))
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Able to write files to MP3 player but no files written into actual device? olnex Linux - Hardware 0 11-11-2005 06:32 AM
Accessing USB Storage Device in RedHat 9 kvsmani Linux - Hardware 0 02-14-2004 12:41 AM
Accessing USB Mass storage device in Linux..?? kvsmani Linux - Hardware 0 02-11-2004 12:03 PM
accessing files scrambled2k3 Linux - Newbie 4 02-21-2003 12:32 PM
Accessing files 24giovanni Linux - Hardware 3 02-13-2003 02:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:08 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration