LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-15-2013, 07:30 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 283

Rep: Reputation: 32
CHAR driver's open function issue


I have written my sample char driver code where a dynamic device node /dev/testNode is created and I am trying to open this from a program in user space. But I am not getting "DEVICE OPENED ...." message in my log. So I am not sure if my open function "myopen" is called.
And help will be appreciated.


test.c :
Code:
#include<linux/module.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<linux/delay.h> //msleep
#include<linux/device.h> //device_create

#define COUNT 1
#define NAME "testNode"
static struct class *cl;
static struct cdev *cdev;
dev_t dev;


int myopen(struct inode *inode, struct file *file)
{
        int minor=MINOR(inode->i_rdev);
        int major=MAJOR(inode->i_rdev);
        printk(KERN_ALERT "DEVICE OPENED : major=%d minor=%d",major, minor);
        msleep(1);
        return 0;
}

static struct file_operations fops = {
        .owner=THIS_MODULE,
        .open=myopen,
};

static int start()
{
        int res;
        res=alloc_chrdev_region(&dev, 0, COUNT, NAME);
if(res <0)
        {
                printk(KERN_ALERT "Unable to allocate space");
                return res;
        }
        else
                printk(KERN_ALERT " Got Major=%d Minor=%d", MAJOR(dev), MINOR(dev));

        cdev = cdev_alloc();
        cdev->owner=THIS_MODULE;
        cdev->ops=&fops;
        res=cdev_add(cdev, dev, COUNT);

        //Creating device node for major number we got
        cl = class_create(THIS_MODULE,NAME);
        if(IS_ERR(cl))
        {
                printk(KERN_ALERT "device class NOT created ...");
                unregister_chrdev_region(cdev->dev,COUNT);
                cdev_del(cdev);
                return -1;
        }
        else
                printk(KERN_ALERT "device class created ...");

        if (device_create(cl, NULL, cdev->dev, NULL, NAME) ==NULL)
        {
                class_destroy(cl);
                unregister_chrdev_region(cdev->dev,COUNT);
                return -1;
        }

        printk(KERN_ALERT "START MODULE ...");
return 0;
}


static void leave()
{
        printk(KERN_ALERT "LEAVING MODULE ... %d %d",MAJOR(cdev->dev),MINOR(cdev->dev));
        device_destroy(cl, dev);
        class_destroy(cl);
        cdev_del(cdev);
        unregister_chrdev_region(cdev->dev,COUNT);
}


module_init(start);
module_exit(leave);
MODULE_LICENSE("GPL");
MODULE_OWNER("Barun Kumar Parichha");
Makefile:
Code:
obj-m +=test.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
My User space C code to open /dev/testNode:
Code:
#include<stdio.h>

int main()
{
        FILE * fd;
        fd=fopen("/dev/testNode","r");
        if(fd <0)
        {
                printf("\nError opening node ");
                perror("fopen");
                return 0;
        }
        else
                printf("\n SUCCESS");

        return 0;
}
 
Old 02-17-2013, 12:50 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 1,465

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Your code looks pretty good. The only thing I see missing is "\n" on the end of the printk's.
 
Old 02-18-2013, 02:53 AM   #3
sanupdas
LQ Newbie
 
Registered: Feb 2013
Posts: 1

Rep: Reputation: Disabled
the code seems all perfect to me. i have tried it in my laptop running ubuntu 12.04 and its showing DEVICE OPENED when checking through dmesg command. may be some other issue wid you like permission.
 
Old 02-18-2013, 11:32 PM   #4
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 283

Original Poster
Rep: Reputation: 32
Actually I have only sudo permision (not root) in my machine.
But when I tried with low level system calls (open/read/write), it works perfectly.

Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdlib.h>
#include <stdio.h>

int main() {

        int fh=-1;
        char buffer[65];
        int gotten;

        fh = open("/dev/testNode",O_RDWR|O_APPEND);
	if(fh >=0)
		printf ("File handle %d\n",fh);
	else
	{
		printf("Unable to open file");
		perror("open");
		return 0;
	}

        gotten = read(fh,buffer,64);
	if(gotten >0)
	{
                buffer[gotten] = '\0';
                printf("******%s",buffer);
	}
}
Thanks for looking into my code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with char driver write function - infinite loop Cherubim Linux - Kernel 1 10-13-2009 01:08 AM
Suse Enterprise Server 11: ifconfig does not call my Network driver's open function pvpnguyen Programming 1 09-03-2009 04:21 PM
c swapping char* in function zefram Programming 6 08-27-2006 01:28 AM
C : Function to modify pointer to char introuble Programming 2 06-21-2006 12:24 PM
help passing char* to function call PTBmilo Programming 4 03-16-2003 07:05 PM


All times are GMT -5. The time now is 01:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration