Find the answer to your Linux question:
Results 1 to 9 of 9
Hello Friends! I have this problem. I'm following a tutorial for developing drivers in Linux using Modules. First of all I type "mknod /dev/tutorial c 203 1". Then I compile ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    104

    error in sample Module


    Hello Friends! I have this problem. I'm following a tutorial for developing drivers in Linux using Modules. First of all I type "mknod /dev/tutorial c 203 1". Then I compile my module, "tutorial.c":
    Code:
    #include <linux/device.h>
    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/cdev.h>
    #include <linux/fs.h>
    
    MODULE_LICENSE("GPL");
    
    static int major=203;
    static int minor=1;
    dev_t dev;
    static int tutorial_open(struct inode *inode, struct file *file);
    
    struct file_operations tutorial_fops={
    
      .owner = THIS_MODULE,
      .open = tutorial_open,
    };
    
    
    
    
    
    static int tutorial_open(struct inode *inode, struct file *file)
    {
      file->private_data=dev;
      printk(KERN_INFO "abriendo dispositivo\n");
      
      
     return 0; 
    }
    
    static int tutorial_init(void) {
      struct cdev *mycdev;
      int result;
      printk(KERN_INFO "Init module\n");
      
      dev=MKDEV(major, minor);
      result = register_chrdev_region(dev,1, "tutorial");
    
      
      if(result < 0) {
        printk(KERN_INFO "Cant get major nr %d\n", major);
      }
      else
      {
        printk(KERN_INFO "get major nr %d\n", major);
        mycdev=cdev_alloc();
        cdev_init(mycdev,&tutorial_fops);
        mycdev->owner = THIS_MODULE;
        mycdev->ops = &tutorial_fops;
    
        int rc = cdev_add(mycdev,dev, 1);
        if(rc<0)
          printk(KERN_INFO "Error on cdev_add\n");
    
      }
      
      return 0;
    }
    
    
    static void tutorial_exit(void) {
    printk(KERN_INFO "Exit module\n");
    }
    module_init(tutorial_init);
    module_exit(tutorial_exit);
    And after getting "tutorial.ko", I type "insmod ./tutorial.ko". When I take a look at "/var/log/messages" I see "get major nr 203". But when I try to open my driver with "cat /dev/tutorial" to execute the "open" function, I get "invalid argument ". Then I remove the driver "rmmod tutorial", and reload with "insmod ./tutorial.ko", and when y try to open again with "cat /dev/tutorial", i get "killed", and my computer gets entirely blocked, I cant use neither the keyboard or the mouse!! Why ??

    Thank you very much!!!

  2. #2
    Just Joined!
    Join Date
    Mar 2009
    Posts
    24
    If you run cat ,you need to implement the read function as well in the file operations structure . Cat tries to read the contents of the file after opening it . So implement that and your code'll run safe

  3. #3
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    104
    Ey Shivant!! You were right!! now it works!! Thank you very much!! I'm new at developing with kernel, but i find it amazing!!

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    104
    I have other problem. This is the new code
    Code:
    #include <linux/device.h>
    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/cdev.h>
    #include <linux/fs.h>
    
    MODULE_LICENSE("GPL");
    
    static int major=203;
    static int minor=1;
    dev_t dev;
    static int tutorial_open(struct inode *inode, struct file *file);
    static ssize_t tutorial_read(struct file *filp, char __user *buf, size_t count,loff_t *f_pos);
    struct file_operations tutorial_fops={
    
      .owner = THIS_MODULE,
      .open = tutorial_open,
      .read = tutorial_read,
    };
    
    
    ssize_t tutorial_read(struct file *filp, char __user *buf, size_t count,loff_t *f_pos)
    {
      printk(KERN_INFO "Read from device\n");
      return 1;
    }
    
    
    
    static int tutorial_open(struct inode *inode, struct file *file)
    {
      file->private_data=dev;
      printk(KERN_INFO "open device\n");
      
      
     return 0; 
    }
    
    static int tutorial_init(void) {
      struct cdev *mycdev;
      int result;
      printk(KERN_INFO "Init module\n");
      
      dev=MKDEV(major, minor);
      result = register_chrdev_region(dev,1, "tutorial");
    
      
      if(result < 0) {
        printk(KERN_INFO "Cant get major nr %d\n", major);
      }
      else
      {
        printk(KERN_INFO "get major nr %d\n", major);
        mycdev=cdev_alloc();
        cdev_init(mycdev,&tutorial_fops);
        mycdev->owner = THIS_MODULE;
        mycdev->ops = &tutorial_fops;
    
        int rc = cdev_add(mycdev,dev, 1);
        if(rc<0)
          printk(KERN_INFO "Error on cdev_add\n");
    
      }
      
      return 0;
    }
    
    
    static void tutorial_exit(void) {
      printk(KERN_INFO "Exit module\n");
      unregister_chrdev_region(dev,1);
    
    }
    module_init(tutorial_init);
    module_exit(tutorial_exit);
    I load the module, and when i type "cat /dev/tutorial" it enter in an infinte loop in "tutorial_read", cause i can see the infinite message from /var/log/messages. So I type Cntrl^C, remove module with "rmmod tutorial". And when i reload the module "insmod ./tutorial.ko", and type "cat /dev/tutorial", i got "killed", and my computer gets entirely blocked again!!!

  6. #5
    Just Joined!
    Join Date
    Mar 2009
    Posts
    24
    Your read function should return 0 to indicate EOF . That'll do

  7. #6
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    104
    Yes shivanth! you were right! returning 0 didn't enter in an infinite loop. But i still getting the other error. When i remove "rmmod tutorial" and reload with "insmod ./tutorial.ko" and type "cat /dev/tutorial" I got "killed", and the system gets block.

    Thank you very much!

  8. #7
    Just Joined!
    Join Date
    Mar 2009
    Posts
    24
    You need to implement a close function as well to for proper unloadingl of the driver . Unregister the device etc is done in this function so that the driver is unloaded properly . If its not unloaded properly , the next time you try to load it , the driver is seen as registered causing the kill signal delivered to insmod

  9. #8
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    104
    Ok shivanth. I have modified the exit function "tutorial_exit". This is the new code:
    Code:
    static void tutorial_exit(void) {
      printk(KERN_INFO "Exit module\n");
      unregister_chrdev_region(dev,1);
      void cdev_del(mycdev);
    
    
    }
    I add the "cdev_del" function to properly remove the driver. But I still getting the error. When I reload the driver the second, i don't get the killed signal with "insmod ./tutorial.ko", i get the killed signal when I read for the second time "cat /dev/tutorial".

    Thank you very much

  10. #9
    Just Joined!
    Join Date
    Mar 2009
    Posts
    24
    You still haven't implemented the release method . In release, int this case , u just need to return a zero for successful closing of the device .

    And a piece of advice , google for linux device drivers 3d edition . All i just posted was straight from that . It'll surely help you . And do not stop posting if anything comes in your way

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •