Re: usage count

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hi,

Here is the code .....

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
 
#include <linux/fs.h>
 
#define DEV_NAME "conceptdev"
 
static int char_open(struct inode *inode, struct file *file);
static int char_release(struct inode *inode, struct file *file);
 
struct file_operations char_fops =
        {
                .open = char_open,
                .release = char_release
        };
 
int major;
 
static int
char_open(
                struct inode *inode,    /*What Inode is this ?*/
                struct file * file              /*What File is this ? */
                ) {
        int mjr, mnr;
 
        mjr = MAJOR(inode->i_rdev);
        mnr = MINOR(inode->i_rdev);
 
        printk(KERN_ALERT"So you want to open: %d \n", mnr);
        MOD_INC_USE_COUNT;
        return 0;
}
 
static int
char_release(struct inode *inode, struct file *file) {
        printk(KERN_ALERT"Closing Time !\n");
        MOD_DEC_USE_COUNT;
        return 0;
}
 
 
static int
char_init() {
 
        major = register_chrdev(
                        0,                      /*Gimme a Major Number
dynamically*/
                        DEV_NAME,       /*This is What I will call it*/
                        &char_fops      /*This is what the Special
'File' for                                                  this
device will have as its operations*/
                        );
 
        if(major < 0) {
                printk(KERN_ALERT"Device registration failed\n");
                return -ENODEV;
        }
        printk(KERN_ALERT"I have registered under: %d.\n To talk to
me, you need to get hold of my 'Device Special File'. Since you used
the Dynamic Method, you will have to manually create this file.\n Want
me to tell you how?\n", major);
 
        return 0;
}
 
static void
char_exit() {
        int ret;
        ret = unregister_chrdev(major, DEV_NAME);
        if(ret < 0) {
                printk(KERN_ALERT"Cannot Unregister. This->%d is why\n", ret);
        }
}
 
module_init(char_init);
module_exit(char_exit);
MODULE_LICENSE("GPL");

//--------------------------------------------------------------------------------------->

if i do lsmod it will show usage count zero.....then i created a node using 
mknod /dev/trial2 c MAJOR MINOR and open this node using cat....and
then i have done lsmod again but it still shows usage count zero....


On 9/5/05, Thomas Petazzoni <thomas.petazzoni@xxxxxxxx> wrote:
> Hi,
> 
> manish rangankar wrote:
> 
> > Now, when i  access the OPEN this node multiple times it's count
> > should be increase...
> 
> Which kernel version are you using ?
> 
> AFAIK, in 2.4.x, one had to call MOD_INC_USE_COUNT in the open() method
> of the character driver. But in 2.6.x, I think this is done
> automagically. Isn't it ?
> 
> Maybe you should have a look at Linux Device Drivers, the 3rd edition is
> available online.
> 
> Sincerly,
> 
> Thomas
> -- 
> Thomas Petazzoni
> thomas.petazzoni@xxxxxxxx
>

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux