Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement for the ioctl() function

I am developing a device driver I/O model for small microcontroller applications, using POSIX as a guideline for interface design. I implemented ioctl() as a means of controlling driver/hardware parameters – for example UART baud rate, I2C slave address, etc.

I notice, that POSIX:2008 lists ioctl() and <stropts.h> as obsolescent. What is the recommended alternative mechanism for communicating with a device driver?

like image 658
makes Avatar asked Sep 03 '11 17:09

makes


People also ask

Is ioctl deprecated?

As I understand it, ioctls aren't totally deprecated, but sysfs is preferred (?) I need to read/write sets of values simultaneously i.e. through one sysfs entry. I've read that this isn't ideal, but is acceptable if necessary (?)

What is ioctl () in Linux?

In computing, ioctl (an abbreviation of input/output control) is a system call for device-specific input/output operations and other operations which cannot be expressed by regular system calls. It takes a parameter specifying a request code; the effect of a call depends completely on the request code.

Why do we need ioctl?

The ioctl function is useful for implementing a device driver to set the configuration on the device. e.g. a printer that has configuration options to check and set the font family, font size etc. ioctl could be used to get the current font as well as set the font to a new one.

Is ioctl blocking call?

The IOCTL call has many functions; establishing blocking mode is only one of its functions. The value in COMMAND determines which function IOCTL will perform. The REQARG of 0 specifies non-blocking (a REQARG of 1 would request that socket S be set to blocking mode).


1 Answers

POSIX only defines a very limited subset of ioctl() functionality - that related to STREAMS. Since the STREAMS facility is obsolescent, the interface to it is also obsolescent in POSIX.

However, ioctl() has been part of Unix since 'forever' (it was certainly in 7th Edition UNIX, and I am tolerably certain it was not new even then). It is 'the way' to control device drivers after they are open. The only issue is that such interfaces and controls are not standardized.

You could take a look at the <termios.h> files for a set of functions written to control terminals. I expect that the typical implementation uses ioctl() or other similar specialized mechanisms, but the interface was made general when it was standardized (the <termios.h> interface is not identical to any earlier interface, either the 7th Edition or the System III or any other interface). If you wanted to, you could write standard functions atop your ioctl() interface that your users would use; you would implement those functions to call onto your ioctl() interface.

So, ioctl() isn't going away; it is the correct way to control device drivers. POSIX has a slightly different agenda, that's all.

like image 189
Jonathan Leffler Avatar answered Sep 20 '22 11:09

Jonathan Leffler