6

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?

makes
  • 6,438
  • 3
  • 40
  • 58

1 Answers1

8

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 6
    To expand on this, POSIX's phasing out of `ioctl` does not indicate that `ioctl` should not be used, but rather than POSIX is not interested in specifying how hardware devices should work. Since `ioctl` was also used with STREAMS, POSIX previously specified *just that aspect* of `ioctl` (not any other use with devices), but now that STREAMS are obsolescent, there's nothing left to specify. – R.. GitHub STOP HELPING ICE Sep 03 '11 at 18:14
  • Thank you - yes, that is pretty much exactly what I meant. – Jonathan Leffler Sep 03 '11 at 18:48
  • 3
    ioctl() goes way back... personally I used it in the 1980s. While the 1983 System V manuals I still have describe ioctl() the actual dirt on how to use it was found in .h files and technical manuals. I would urge you to put all the details on YOUR ioctls in the .h file for the device as a way to be sure it is always available to developers. – Gilbert Sep 03 '11 at 19:32