Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>ioctl</code> function is useful when one is implementing a device driver to set the configuration on the device. e.g. a printer has configuration options to check and set font, font size etc. <code>ioctl</code> could be used to get current font as well as set font to another one. In user application make use of <code>ioctl</code> to send a code to a printer telling it to return the current font or to set font to a new one.</p> <pre><code>int ioctl(int fd, int request, ...) </code></pre> <ol> <li><code>fd</code> is file descriptor, the one returned by open</li> <li><code>request</code> is request code. e.g GETFONT will get current font from printer, SETFONT will set font on a printer.</li> <li>third argument is <code>void *</code>. Depending on second argument, the third may or may not be present. e.g. if second argument is SETFONT, third argument may give font name as ARIAL.</li> </ol> <p>So now int request is not just a macro, one is required to generate request code to be used by user application and device driver module to determine which configuration on device must be played with. One sends a request code using <code>ioctl</code> from user application and then uses the request code in device driver module to determine which action to perform.</p> <p>A request code has 4 main parts</p> <pre><code> 1. A Magic number - 8 bits 2. A sequence number - 8 bits 3. Argument type (typically 14 bits), if any. 4. Direction of data transfer (2 bits). </code></pre> <p>If request code is SETFONT to set font on a printer, the direction for data transfer will be from user application to device driver module. User sends font name Arial to printer. If request code is GETFONT, direction is from printer to user application.</p> <p>To generate request code Linux provide some predefined function like macros.</p> <p>1.<code>_IO(MAGIC, SEQ_NO)</code> both are 8 bits, 0 to 255, e.g. let us say we want to pause printer. This does not require adata transfer. So we would generate request code as below</p> <pre><code> #define PRIN_MAGIC 'P' #define NUM 0 #define PAUSE_PRIN __IO(PRIN_MAGIC, NUM) </code></pre> <p>Now use <code>ioctl</code> as</p> <pre><code> ret_val = ioctl(fd, PAUSE_PRIN); </code></pre> <p>Corresponding system call in driver module will receive the code and pause the printer.</p> <ol start="2"> <li><p><code>__IOW(MAGIC, SEQ_NO, TYPE)</code> <code>MAGIC</code> and <code>SEQ_NO</code> are same as above, but third part gives the type of next argument, recall the third argument of <code>ioctl</code> is <code>void *</code>. W in <code>__IOW</code> indicates that direction of data is from user application to driver module. Let us take an example, Suppose one is telling printer to set font to Arial.</p> <pre><code>#define PRIN_MAGIC 'S' #define SEQ_NO 1 #define SETFONT __IOW(PRIN_MAGIC, SEQ_NO, unsigned long) </code></pre></li> </ol> <p>further,</p> <pre><code> char *font = "Arial"; ret_val = ioctl(fd, SETFONT, font); </code></pre> <p>Now <code>font</code> is a pointer, which means it is an address best represented as <code>unsigned long</code>, hence the third part of <code>_IOW</code> mentions type as such. Also, this address of font is passed to corresponding system call implemented in device driver module as <code>unsigned long</code> and we need to cast it to proper type before using it. Kernel space can access user space and hence this works. other two function like macros are <code>__IOR(MAGIC, SEQ_NO, TYPE)</code> and <code>__IORW(MAGIC, SEQ_NO, TYPE)</code> where direction of data flow will be from kernel space to user space and both ways respectively.</p> <p>Please let me know if this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload