Well today we're gonna have some game with ioctl and some related functions though we're gonna focused our game on this IOCTL. 
ioctl is a syscall commonly used for controlling devices under nix system. This ioctl function can be in user space or kernel space.
on User Space this ioctl function is useful to perform some operations which can't be performed by other regular syscalls.
ioctl function used to control any devices i/o .. IOCTL = Device Input and the  Output Controller.
for more complete explanations just take a look : man ioctl.

have a good visit into my linux b0x:
============
ev1lut10n@ev1l:~$ cat /usr/include/sys/ioctl.h | grep 'int ioctl'
extern int ioctl (int __fd, unsigned long int __request, ...) __THROW;
============

- considering above code the first parameter will be file descriptor number.
- and the second will be our device dependent request code, say it :  command !
- and for next argument as we may be untyped pointer in memory
since there are so many benefits of ioctl we will limit here only in 3 parts:

[Sample IOCTL Code in C]

1. Device Controlling

ok make sure u include our beloved header at /usr/include/sys/ioctl.h:
===================
#ifdef HAVE_SYS_IOCTL
#include  <sys/ioctl.h> 
#endif
===================
#ifdef HAVE_SYS_IOCTL, where the ifdef directive is used to check whether we have ioctl or not.

there are some functions related to this ioctl() such as:
- open()
when u use this funct don't forget it needs  fcntl.h
======================================
root@mywisdom-Vostro1310:/usr/include# cat fcntl.h | grep 'open ('
extern int open (__const char *__file, int __oflag, ...) __nonnull ((1)
=======================================
this function mostly used to open a file descriptor before we use ioctl syscall

here is a simple example, ex you wanna open /dev/cdrom:

if(fd=open("/dev/cdrom",O_RDONLY))

so what's )_RDONLY. check out this flags desc below:
============================
O_RDONLY        Only read operations permitted
O_WRONLY        Only write operations permitted
O_RDWR          Read and Write operations both permitted
O_NONBLOCK      Non-blocking, applies to open operation only
O_APPEND        All writes go to end of file
O_CREAT         Create file if it doesn't already exist
O_TRUNC         Delete existing contents of file
O_EXCL          Open fails if file already exists
O_SHLOCK        Get a "shared lock" on the file
O_EXLOCK        Get an "exclusive lock" on the file
O_DIRECT        Try to avoid all caching of operations
O_FSYNC         All writes immediately effective, no buffering
O_NOFOLLOW      If file is symbolic link, open it, don't follow it

and the next parameter is optional where it's usually indicated the permissions

- close()
this will be used to close our FD that we've opened using open() func


Here's sample for stopping and ejecting the cdrom device:
====================
/**some cdrom operation using ioctl by ev1lut10n**/
#include <stdio.h>
#include <stdlib.h>
#include  <fcntl.h> 
#include  <linux/cdrom.h> 
#include  <sys/stat.h> 
#include  <sys/types.h> 
#include  <unistd.h> 
#ifdef HAVE_SYS_IOCTL
#include  <sys/ioctl.h> 
#endif
char menu();
int main () 
 {
 int pilihan; 
 int fd;
 fd=open ("/dev/cdrom", O_RDONLY);
 
        pilihan=menu();
      
 if(pilihan==97)
 {
       fprintf(stdout,"\nStopping cdrom device.using ioctl syscall..\n");
       ioctl  (fd, CDROMSTOP,0); 
 }
 else
 {  
     
      fprintf(stdout,"\nEjecting cdrom device.using ioctl syscall..\n");
       ioctl  (fd, CDROMEJECT,0); 
 }
close (fd); 
return 0; 
} 

char menu()
{
 char select_me;
 fprintf(stdout,"\nby: ev1lut10n");
 fprintf(stdout,"\nWatcha gonna do ?? (type a or b)\n");
 fprintf(stdout,"\na ---- stopping currently playing cd");
 fprintf(stdout,"\nb ---- eject cdrom ");
 fprintf(stdout,"\ntype a or b :");
 select_me=getchar();
 return select_me;
}

====================

AS u may see above we use 2 command for controlling cdrom device:
ioctl  (fd, CDROMSTOP,0);  where here we use ioctl syscall to stop our cdrom device
 and 
 ioctl  (fd, CDROMEJECT,0);  where we use ioctl syscall to eject our cdrom device
before our ioctl we already have an open file descriptor :
============================
fd=open ("/dev/cdrom", O_RDONLY);
============================

where we've opened /dev/cdrom using  O_RDONLY flag


for more complete linux cdrom ioctl programming you may see any command related to this device at:
=======
root@ev1l:/home/mywisdom/c/ioctl# cat /usr/include/linux/cdrom.h | grep "#define CDROM"
#define CDROMPAUSE  0x5301 /* Pause Audio Operation */ 
#define CDROMRESUME  0x5302 /* Resume paused Audio Operation */
#define CDROMPLAYMSF  0x5303 /* Play Audio MSF (struct cdrom_msf) */
#define CDROMPLAYTRKIND  0x5304 /* Play Audio Track/index 
#define CDROMREADTOCHDR  0x5305 /* Read TOC header 
#define CDROMREADTOCENTRY 0x5306 /* Read TOC entry 
#define CDROMSTOP  0x5307 /* Stop the cdrom drive */
#define CDROMSTART  0x5308 /* Start the cdrom drive */
#define CDROMEJECT  0x5309 /* Ejects the cdrom media */
#define CDROMVOLCTRL  0x530a /* Control output volume 
#define CDROMSUBCHNL  0x530b /* Read subchannel data 
#define CDROMREADMODE2  0x530c /* Read CDROM mode 2 data (2336 Bytes) 
#define CDROMREADMODE1  0x530d /* Read CDROM mode 1 data (2048 Bytes)
#define CDROMREADAUDIO  0x530e /* (struct cdrom_read_audio) */
#define CDROMEJECT_SW  0x530f /* enable(1)/disable(0) auto-ejecting */
#define CDROMMULTISESSION 0x5310 /* Obtain the start-of-last-session 
#define CDROM_GET_MCN  0x5311 /* Obtain the "Universal Product Code" 
#define CDROM_GET_UPC  CDROM_GET_MCN  /* This one is deprecated, 
#define CDROMRESET  0x5312 /* hard-reset the drive */
#define CDROMVOLREAD  0x5313 /* Get the drive's volume setting 
#define CDROMREADRAW  0x5314 /* read data in raw mode (2352 Bytes)
#define CDROMREADCOOKED  0x5315 /* read data in cooked mode */
#define CDROMSEEK  0x5316  /* seek msf address */
#define CDROMPLAYBLK  0x5317 /* (struct cdrom_blk) */
#define CDROMREADALL  0x5318 /* read all 2646 bytes */
#define CDROMGETSPINDOWN        0x531d
#define CDROMSETSPINDOWN        0x531e
#define CDROMCLOSETRAY  0x5319 /* pendant of CDROMEJECT */
#define CDROM_SET_OPTIONS 0x5320  /* Set behavior options */
#define CDROM_CLEAR_OPTIONS 0x5321  /* Clear behavior options */
#define CDROM_SELECT_SPEED 0x5322  /* Set the CD-ROM speed */
#define CDROM_SELECT_DISC 0x5323  /* Select disc (for juke-boxes) */
#define CDROM_MEDIA_CHANGED 0x5325  /* Check is media changed  */
#define CDROM_DRIVE_STATUS 0x5326  /* Get tray position, etc. */
#define CDROM_DISC_STATUS 0x5327  /* Get disc type, etc. */
#define CDROM_CHANGER_NSLOTS    0x5328  /* Get number of slots */
#define CDROM_LOCKDOOR  0x5329  /* lock or unlock door */
#define CDROM_DEBUG  0x5330 /* Turn debug messages on/off */
#define CDROM_GET_CAPABILITY 0x5331 /* get capabilities */
#define CDROMAUDIOBUFSIZ        0x5382 /* set the audio buffer size */
#define CDROM_SEND_PACKET 0x5393 /* send a packet to the drive */
#define CDROM_NEXT_WRITABLE 0x5394 /* get next writable block */
#define CDROM_LAST_WRITTEN 0x5395 /* get last block written on disc */
======
the above sample we focus on kernel 2.6
and for those of you who getting interested for kernel 3.0 cdrom programing, you may also find uselful material at:
==============
http://www.mjmwired.net/kernel/Documentation/ioctl/cdrom.txt
==============

on success our ioctl function will return other than -1 !!!