title: Simple char driver confusion
body: <p>So I have been leaning device drivers in linux and saw various examples of how to create one. The most common steps were,</p>
  
  <ol>
  <li>create file operations</li>
  <li>create major and minor numbers</li>
  <li>create a struct cdev, that will be associated later with this fops</li>
  <li>create a node either using a mknod or using sysfs, like class_create and device_create.</li>
  </ol>
  
  <p>I have a confusion, that if I don't create cdev, and just register my driver, then only create a node in sysfs, I am able to use that node with any c program. Why I should create a struct cdev in this case?</p>
  
  <p>E.G. a simple init function from this place, <a href="https://sysplay.in/blog/tag/character-device-files/" rel="nofollow noreferrer">https://sysplay.in/blog/tag/character-device-files/</a></p>
  
  <pre><code>static int __init ofcd_init(void) /* Constructor */
  {
      int ret;
      struct device *dev_ret;
  
      printk(KERN_INFO "Namaskar: ofcd registered");
      if ((ret = alloc_chrdev_region(&amp;first, 0, 1, "Shweta")) &lt; 0)
      {
          return ret;
      }
      if (IS_ERR(cl = class_create(THIS_MODULE, "chardrv")))
      {
          unregister_chrdev_region(first, 1);
          return PTR_ERR(cl);
      }
      if (IS_ERR(dev_ret = device_create(cl, NULL, first, NULL, "mynull")))
      {
          class_destroy(cl);
          unregister_chrdev_region(first, 1);
          return PTR_ERR(dev_ret);
      }
  
      cdev_init(&amp;c_dev, &amp;pugs_fops);//here I am confused. Why do it? what will happen if I don't do this, as I can use my driver still.
      if ((ret = cdev_add(&amp;c_dev, first, 1)) &lt; 0)
      {
          device_destroy(cl, first);
          class_destroy(cl);
          unregister_chrdev_region(first, 1);
          return ret;
      }
      return 0;
  }
  </code></pre>