IOCTL

Ioctl() is a system call to invoke input output controls to a device file. It takes an integer code which signifies the control operation and an argument as pointer to provide inputs for the operation. Same input pointer can be used to get the output from the operation. It returns zero on success and non-zero if some error happened or if this ioctl code is not implemented by kernel/driver.

IOCTL low layer

It passes argument buffer from user mode and jumps to kernel mode using software interrupt. Kernel calls driver ioctl routine. Driver copies argument to kernel buffer. Driver can do register manipulation if needed and copies return buffer to user mode. Driver can return error if ioctl code is not implemented in driver or argument is not in proper format. Kernel returns call and switch context to user mode and control returns back to user code.

PARPORT IOCTL codes

  • Data register
    • PPRDATA - read 8 bit data register
    • PPWDATA - write 8 bit data register
  • Data Direction
    • PPDATADIR mode=0 - Sets data direction bits to write mode
    • PPDATADIR mode=FFh/255 - Sets data direction bits to read mode
  • Control register
    • PPRCONTROL - Reads control register
    • PPWCONTROL - Writes to control register
    • PPFCONTROL - atomic modify control register(read write at once)
  • Status register
    • PPRSTATUS - Reads the status register

Source Code

Here is a small example to invoke ioctl to parallel port of PC. We have connected a LED to PIN 1 (strobe) of parallel port (LPT0) with a 1K resistor to ground. We are executing ioctl code PPFCONTROL to parallel port 0 with argument of strobe control bit. First we are giving strobe bit as 1 and then with some delay we are setting 0. This will create a sequence of on and off of the strobe LED.
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/ioctl.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#define DEVICE "/dev/parport0"
int main(int argc, char **argv)
10  {
11    struct ppdev_frob_struct frob;
12    int fd;
13    int mode;
14                                                                              
15    if((fd=open(DEVICE, O_RDWR)) < 0) {
16      fprintf(stderr, "can not open %s\n", DEVICE);
17      return 10;
18    }
19    if(ioctl(fd, PPCLAIM)) {
20      perror("PPCLAIM");
21      close(fd);
22      return 10;
23    }
24    
25    frob.mask = PARPORT_CONTROL_STROBE;  /* change only this pin ! */
26    while(1) {
27      frob.val = PARPORT_CONTROL_STROBE;  /* set STROBE PIN... */
28      printf("LED is on\n");
29      fflush(stdout);
30      ioctl(fd, PPFCONTROL, &frob);
31      usleep(500);
32      frob.val = 0;        /* and clear STROBE PIN */
33      printf("LED is off\n");
34      fflush(stdout);
35      ioctl(fd, PPFCONTROL, &frob);
36      usleep(500);
37    }
38    ioctl(fd, PPRELEASE);
39    close(fd);
40    return 0;
41  }

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

poll, select, fcntl, fputs,fgets,fgetc,fputc, printf, scanf, ioctl, mmap, DOS low level IO access, DOS BIOS int86, sleep/delay,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan