Which is the best way for accessing device files in C?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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
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)?
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.
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?
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
(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.))
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.