Introduction of ioctl( )
Input/output Control (ioctl, in short) is a common operation, or system call, available in most driver categories. It is a ‘one bill fits all’ kind of system call. If there is not other system call that meets a particular requirements, the ioctl () is the one to use. Practical examples include volume control for an audio device , display configuration for a video device, reading device registers, and son on –basically, anything to do with enough for any kind of operation ( for example, for debugging a driver by querying driver data structures). The question is: how can all this are achieved by single function prototype? The trick lies in sung its two key parameters: command and argument. The command is a number representing an operation. The argument is the corresponding parameter for the operation. The ioctl() function implementation does a ‘switch ... case’ over the command to implement the corresponding functionality. The following has been its prototype in the Linux kernel for quite some time:
int ioctl(struct inode *I, sturct file *f, unsigned int cmd, unsigned long arg);However, from kernel 2.6.35, it change to:
Long ioctl (struct file *f, unsigned int cmd, unsigned long arg);
If there is a need for more arguments, all of them are put in a structure, and a pointer to the structure, and a pointer to the structure becomes the ‘one’ command argument. Whether integer or pointer, the argument is take as a long integer in kernel-space, and accordingly type-cast and processed.
Ioctl () is typically implemented as part of the corresponding driver, and then an appropriate function pointer is initialized with it, exactly as with other system call like open (), read (), etc. For example, the character drives, it is the ioctl or unlocked ioctl (since kernel 2.6.35) function pointer field in the struct file_operations that is to be initialized.
Again, lik other system calls, it can be equivalently invoked from user-space using the ioctl() system call, prototyped in <sys/ioctl.h> as:
int ioctl(in fd, int cmd, …);
Here, cmd is the same as what is implemented in the driver’s ioctl (), and the variable argument construct(…) is a hack to be able to pass any type of argument ( thought only one) to the driver’s ioctl(). Other parameters will be ignored.
Querying driver –internals variables
To better understand the boring theory explained below, here’s the code set for the ‘debugging a driver’ example mentioned earlier. This driver has three static global variables status, dignity, and ego, which need to be queried and possibly operated from an application. The header and query_ioctl.h defines the corresponding commands and command argument type. A listing follows:
#ifndef QUERY_IOCTL_H
#define QUERY_IOCTL_H
#include<linux/ioctl.h>
typedef struct
{
int status, dignity, ego;
}query_arg_t;
#define QUERY_GET_VARIABLES _IOR(‘q’, 1, query_arg_t *)
#difine QUERY_CLR_VARIABLES _IO(‘q’, 2)
#endif
Using these, the driver’s ioctl() implementation in query_ioctl.c would be as fllows:
static int status =1, dignity=3, ego=5;
#if (LINUX_VERSION_CDE < KERNEL_VERSION(2,6,35))
static int my_ioctl(struct inode *I, struct file *f, unsigned int cmd, unsigned long arg)
#else
Static long my_ioctl(struct file *f, unsigned int cmd,unsigned long arg)
#endif
{
query_arg_t q;
switch (cmd)
{
Case QUERY_GET_VARIABLES;
q.status = status;
q.dignity=dignity;
q.ego=ego;
if (copy_to_user((query_arg_t *)arg, &q, sizeof(query_arg_t))
{
return –EACCES;
}
break;
case QUERY_CLR_VARIABLES:
status=0;
dignity=0;
ego=0;
break;
default:
return –EINVAL;
}
return 0;
}
And finally, the corresponding invocation functions from the application query_app.c would be as follows:
#include <stdio.h>
#include <sys/ioctl.h>
#include “query_ioctl.h”
void get_vars(int fd)
{
query_arg_t q;
if (ioctl(fd, QUERY_GET_VARIABLES, &q) == -1)
{
perror("query_apps ioctl get");
` }
else
{
printf("Status : %d\n", q.status):
printf("Dignity: %d\n", q.dignity);
printf("Ego : %d\n", q.ego);
}
}
void clr_vars(int fd)
{
if(ioctl(fd, QUERY_CLR_VARIABLES) == -1)
{
perror("query_apps ioctl clr");
}
}
The complete code of the above three files is available at http://linuxforu.com/article_source_code/aug11/ldd_code.tar.bz2, where the required file and other instructions also available.
Defining the ioctl() commands
1. The direction of command operation - read, write, both or none--filled by the corresponding macro (_IOR, _IOW, _IOWR, _IO).
2. The size of the command argument -- computed using sizeof() with the command argument's type--the third argument to these macros.
3. The 8-bit magic number--to render the commands unique enough--typically an ASCII character the first argument to these macros)
4. The original command umber--the actual command number (1,2,3,...), defined as per our requirement- the second argument to these macros.
More detail regarding the same can be finding at <asm-generic/ioctl.h> for implementation.
No comments:
Post a Comment