Dan
Dan

Reputation: 627

ioctl vs kernel modules in Linux

I know that kernel modules are used to write device drivers. You can add new system calls to the Linux kernel and use it to communicate with other devices.

I also read that ioctl is a system call used in linux to implement system calls which are not available in the kernel by default.

My question is, why wouldn't you just write a new kernel module for your device instead of using ioctl? why would ioctl b useful where kernel modules exist?

Upvotes: 1

Views: 1958

Answers (2)

that other guy
that other guy

Reputation: 123420

You will need to write a kernel driver in either case, but you can choose between adding a new syscall and adding a ioctl.

Let's say you want to add a feature to get the tuner settings for a video capturing device.

If you implement it as a syscall:

  • You can't just load a module, you need to change the kernel itself
  • Hundreds of drivers could each add dozens of syscalls each, kludging up the table with thousands of global functions that must be kept forever.
  • For the driver to have any reach, you will need to convince kernel maintainers that this burden is worthwhile.
  • You will need to upstream the definition into glibc, and people must upgrade before they can write programs for it

If you implement it as an ioctl:

  • You can build your module for an existing kernel and let users load it, without having to get kernel maintainers involved
  • All functions are simple per-driver constants in the applicable header file, where they can easily be added or removed
  • Everyone can start programming with it just by including the header

Since an ioctl is much easier, more flexible, and exactly meant for all these driver specific function calls, this is generally the preferred method.

Upvotes: 3

I also read that ioctl is a system call used in linux to implement system calls which are not available in the kernel by default.

This is incorrect.

System calls are (for Linux) listed in syscalls(2) (there are hundreds of them between user space and kernel land) and ioctl(2) is one of them. Read also wikipage on ioctl and on Unix philosophy and Linux Assembler HowTo

In practice, ioctl is mostly used on device files, and used for things which are not a read(2) or a write(2) of bytes. For example, a sound is made by writing bytes to /dev/audio, but to change the volume you'll use some ioctl. See also fcntl(2) playing a similar role.

Input/output could also happen (somehow indirectly ...) thru mmap(2) and related virtual address space system calls.

For much more, read Advanced Linux Programming and Operating Systems: Three Easy Pieces. Look into Osdev for more hints about coding your own OS.

A kernel module could implement new devices, or new ioctl, etc... See kernelnewbies for more. I tend to believe it might sometimes add a few new syscalls (but this was false in older linux kernels like 3.x ones)

Linux is mostly open source. Please download then look inside source code. See also Linux From Scratch.

IIRC, Linux kernel 1.0 did not have any kernel modules. But that was around 1995.

Upvotes: 0

Related Questions