Note that there are some explanatory texts on larger screens.

plurals
  1. POhow does open works for normal file and device drivers
    primarykey
    data
    text
    <p>Currently, I am learning Linux device drivers. And got stuck over how opening a device file works ? </p> <p>What I got until now... Consider the a simple code that opens a normal file..</p> <pre><code>#incldue&lt;stdio.h&gt; int main() { FILE fp; char buffer[20]; fp = fopen(/home/yoggi/foo.txt, "r"); fread(buffer, 5, 1, fp); } </code></pre> <p>In above program, The fopen(), c-library function, is a wrapper function to the system call <strong>open()</strong>, which intern calls <strong>sys_open() or file_open() in VFS layer</strong> function. As linux supports a number of file system, virtual file system then transfer the control to actual file system handler to the opening that file. </p> <pre><code>1) How does virtual file system(VFS) get to know on which file system the underline file resides? 2) How does it then calls the file_open or open function of that particular filesystem to open file. </code></pre> <p>In case of device drivers similar things happens. Suppose a simple device driver.</p> <pre><code>#include &lt;linux/module.h&gt; // othher includes... static dev_t first; // Global variable for the first device number static struct cdev c_dev; // Global variable for the character device structure static struct class *cl; // Global variable for the device class static int my_open(struct inode *i, struct file *f) { printk(KERN_INFO "Driver: open()\n"); return 0; } static ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off) { printk(KERN_INFO "Driver: read()\n"); return 0; } struct file_operations pugs_fops = { .owner = THIS_MODULE, .open = my_open, .read = my_read, }; static int __init ofcd_init(void) /* Constructor */ { printk(KERN_INFO "Namaskar: ofcd registered"); if (alloc_chrdev_region(&amp;first, 0, 1, "Shweta") &lt; 0) { return -1; } if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL) { unregister_chrdev_region(first, 1); return -1; } if (device_create(cl, NULL, first, NULL, "mynull") == NULL) { class_destroy(cl); unregister_chrdev_region(first, 1); return -1; } cdev_init(&amp;c_dev, &amp;pugs_fops); if (cdev_add(&amp;c_dev, first, 1) == -1) { device_destroy(cl, first); class_destroy(cl); unregister_chrdev_region(first, 1); return -1; } return 0; } static void __exit ofcd_exit(void) /* Destructor */ { cdev_del(&amp;c_dev); device_destroy(cl, first); class_destroy(cl); unregister_chrdev_region(first, 1); printk(KERN_INFO "Alvida: ofcd unregistered"); } module_init(ofcd_init); module_exit(ofcd_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Anil Kumar Pugalia &lt;email_at_sarika-pugs_dot_com&gt;"); MODULE_DESCRIPTION("Our First Character Driver"); </code></pre> <p>Firstly we allocate major minor numbers for the device. Register for the range of device files and Linking the device file operations to the device driver functions. </p> <p>Some of the term I didn't get are..</p> <pre><code>1) What does actually cdev_add() do? in terms of registering a device to the kernel. 2) Registering a device to the kernel means? 3) How does a open(/dev/mynull, O_RONLY); called on a device file actually calls the open function of driver which is mapped while initializing the device by calling routine cdev_init(&amp;c_dev, &amp;pugs_fops); ? </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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