Jeegar Patel
Jeegar Patel

Reputation: 27210

Need of defining ioctl in linux kernel driver

i have started to learn ioctl

i got this example

http://tldp.org/LDP/lkmpg/2.6/html/x892.html

i got total working of ioctal but i am not getting why and where we need to define ioctal for our driver.?

For that example

Instead of calling ioctl(file_desc, IOCTL_SET_MSG, message);

why we can not direct use

device_write(file, message, size, 0);

Upvotes: 0

Views: 1181

Answers (3)

Some programmer dude
Some programmer dude

Reputation: 409136

The ioctl is mainly used to set or get specific parameters or flags about the device, things like reading or writing device registers.

Imagine you have an old floppy drive. It has special registers to control things like "turn motor on or off", "bits per sector", etc. To set those registers you use the ioctl function. To write to the actual disk you use e.g. device_write.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881103

I'm pretty certain that's just because of its tutorial nature. It's trying to show you how to use ioctl. In reality, ioctl would be used for configuring the device driver or device behind it and you would write data using the "normal" method (probably write).

And, in fact, that's what the code does in that link you provide, it simply passes the information on to device_write which is something the kernel does after it copies your data into kernel space.

Upvotes: 1

Pete Fordham
Pete Fordham

Reputation: 2343

The point is that ioctl can be called from user space but device_write can only be called from inside the kernel.

Upvotes: 1

Related Questions