title: insmod: ERROR: Could not insert module : No such device
body: <p>I'm trying to implement character device driver in C at Linux.
  My code is as follows:</p>
  
  <pre><code>#include&lt;linux/device.h&gt;
  #include&lt;linux/init.h&gt;
  #include&lt;linux/kernel.h&gt;
  #include&lt;linux/module.h&gt;
  #include&lt;linux/fs.h&gt;
  #include&lt;linux/err.h&gt;
  #include&lt;asm/uaccess.h&gt;
  #define SUCCESS 0
  #define DEVICE_NAME "chardev"
  #define BUF_LEN 80
  
  MODULE_LICENSE("GPL");
  
  static int Major;
  static char msg[BUF_LEN]={0};
  static short s_o_msg;
  static int Device_Open = 0;
  static struct class* chardevClass = NULL;
  static struct device* chardevDevice = NULL;
  static char *msg_Ptr;
  
  static int device_open(struct inode *, struct file *);
  static int device_release(struct inode *, struct file *);
  static ssize_t device_read(struct file *, char *, size_t, loff_t *);
  static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
  
  
  
  static struct file_operations fops = {
          .read = device_read,
          .write = device_write,
          .open = device_open,
          .release = device_release
  };
  
  static int __init chardev_init(void){
      Major = register_chrdev(0, DEVICE_NAME, &amp;fops);
  
      if (Major &lt; 0) {
          printk(KERN_ALERT "Registering char device failed with %d\n", Major);
          return Major;
      }
  
      chardevDevice = device_create(chardevClass, NULL, MKDEV(Major,0), NULL, DEVICE_NAME);
      if (IS_ERR(chardevDevice)) {
          class_destroy(chardevClass);
          unregister_chrdev(Major, DEVICE_NAME);
          printk(KERN_ALERT
          "Failed to create the device\n");
          return PTR_ERR(chardevDevice);
      }
  
      printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major);
      printk(KERN_INFO "the driver, create a dev file with\n");
      printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);
      printk(KERN_INFO "Try various minor numbers. Try to cat and echo to\n");
      printk(KERN_INFO "the device file.\n");
      printk(KERN_INFO "Remove the device file and module when done.\n");
  
      return SUCCESS;
  }
  
  static void __exit chardev_exit(void){
      device_destroy(chardevClass, MKDEV(Major, 0));
      class_unregister(chardevClass);
      class_destroy(chardevClass);
      unregister_chrdev(Major, DEVICE_NAME);
      printk(KERN_INFO "Goodbye!\n");
  }
  
  static int device_open(struct inode *inodep, struct file *filep)
  {
      static int counter = 0;
  
      if (Device_Open)
          return -EBUSY;
  
      Device_Open++;
      sprintf(msg, "I already told you %d times Hello world!\n", counter++);
      msg_Ptr = msg;
      try_module_get(THIS_MODULE);
  
      return SUCCESS;
  }
  
  static ssize_t device_read(struct file *filep, char *buffer, size_t length, loff_t * offset){
  
      int bytes_read = 0;
  
      if (*msg_Ptr == 0)
          return 0;
  
      while (length &amp;&amp; *msg_Ptr) {
  
          put_user(*(msg_Ptr++), buffer++);
  
          length--;
          bytes_read++;
      }
  
      return bytes_read;
  }
  
  static int device_release(struct inode *inodep, struct file *filep)
  {
      Device_Open--;
  
      module_put(THIS_MODULE);
  
      return 0;
  }
  
  
  
  static ssize_t device_write(struct file *filep, const char *buffer, size_t length, loff_t *offset){
      sprintf(msg, "%s(%zu letters)", buffer, length);
      s_o_msg = strlen(msg);
      printk(KERN_INFO "Received %zu characters from the user\n", length);
      return length;
  }
  
  module_init(chardev_init);
  module_exit(chardev_exit);
  </code></pre>
  
  <p>then, I compile this with following command and to this moment everything looks just fine:</p>
  
  <pre><code>obj-m := memory.o
  
  all:
      make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
  
  clean:
      make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  </code></pre>
  
  <p>but when I tried to run this module with</p>
  
  <pre><code>sudo /sbin/insmod memory.ko
  </code></pre>
  
  <p>I get an error : </p>
  
  <pre><code>insmod: ERROR: Could not insert module : No such device
  </code></pre>
  
  <p>Could you explain me please what I'm doing wrong and what should I do to run this module properly?</p>
  
  <p>Thanks a lot.</p>