LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2010, 04:38 AM   #1
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Rep: Reputation: 16
Question Character device mounting


Hi all,
I've a character driver.I used the code from the web-site
http://www.faqs.org/docs/kernel/x571.html
I can compile it and , mount it with
Code:
mknod
command.It's ok , it's working.
But what i want is to mount it in code.
It shall be mounted automatically when it's compiled.

How can it be done ?
 
Old 03-31-2010, 04:52 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dufresne View Post
Hi all,
I've a character driver.I used the code from the web-site
http://www.faqs.org/docs/kernel/x571.html
I can compile it and , mount it with
Code:
mknod
command.It's ok , it's working.
But what i want is to mount it in code.
It shall be mounted automatically when it's compiled.

How can it be done ?
If you can do something (execute a command) by hand, you can do it from a script, can't you ?
 
Old 03-31-2010, 05:05 AM   #3
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Sergei Steshenko View Post
If you can do something (execute a command) by hand, you can do it from a script, can't you ?
Is it the only way , to write additional script ?
Is there any way to do it without writing additional script?
What i want is to do it by only single C code.
How can i execute mknod command in my module code ?

Last edited by Dufresne; 03-31-2010 at 05:07 AM.
 
Old 03-31-2010, 05:32 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dufresne View Post
Is it the only way , to write additional script ?
Is there any way to do it without writing additional script?
What i want is to do it by only single C code.
How can i execute mknod command in my module code ?
I don't think you correctly understand how a driver works. Driver is a piece of code called by some other piece of code.

Why do you want the single piece of "C" code ? What's the ultimate goal ?
 
Old 03-31-2010, 05:38 AM   #5
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Sergei Steshenko View Post
I don't think you correctly understand how a driver works. Driver is a piece of code called by some other piece of code.

Why do you want the single piece of "C" code ? What's the ultimate goal ?
Actually,i dont know the goal.it's a homework .
So ,there isn't any way to execute a shell command in a module code, is it ?
 
Old 03-31-2010, 06:22 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dufresne View Post
Actually,i dont know the goal.it's a homework .
So ,there isn't any way to execute a shell command in a module code, is it ?
You question is equivalent to: "Can a module spawn a process ?". I haven't heard of this, but recheck.

Still, you have a chicken-egg problem. Until kernel module is installed, it's just a file as any other file. As a file it can exhibit no initiative.

You better publish the homework text exactly - if it's in English.
 
Old 03-31-2010, 06:39 AM   #7
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Original Poster
Rep: Reputation: 16
Well, it isn't in English ,making tranlastion might lead misunderstanding the problem.
So i found this;
http://www.faqs.org/docs/kernel/x571.html#AEN692
In this web site , where i took character device driver example code , it says ;
Quote:
If you pass a major number of 0 to register_chrdev, the return value will be the dynamically allocated major number. The downside is that you can't make a device file in advance, since you don't know what the major number will be. There are a couple of ways to do this. First, the driver itself can print the newly assigned number and we can make the device file by hand. Second, the newly registered device will have an entry in /proc/devices, and we can either make the device file by hand or write a shell script to read the file in and make the device file. The third method is we can have our driver make the the device file using the mknod system call after a successful registration and rm during the call to cleanup_module.
First mentioned way , the way i did , our driver prints assigned number and we made device file with mknod command and it works.
Second way , is using shell script which shall read entry from /proc/devices/ , which isn't wanted.
Third way , is the wanted way , "our driver make the the device file using the mknod system call after a successful registration."
How can we achieve this third way ?
 
Old 03-31-2010, 06:42 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dufresne View Post
Well, it isn't in English ,making tranlastion might lead misunderstanding the problem.
So i found this;
http://www.faqs.org/docs/kernel/x571.html#AEN692
In this web site , where i took character device driver example code , it says ;

First mentioned way , the way i did , our driver prints assigned number and we made device file with mknod command and it works.
Second way , is using shell script which shall read entry from /proc/devices/ , which isn't wanted.
Third way , is the wanted way , "our driver make the the device file using the mknod system call after a successful registration."
How can we achieve this third way ?
man 2 mknod
 
Old 03-31-2010, 07:48 AM   #9
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Original Poster
Rep: Reputation: 16
It says that,
Quote:
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int mknod(const char *pathname, mode_t mode, dev_t dev);
But when i invoke mknod in init_module() function, i get a compile error which says
Quote:
error: implicit declaration of function ‘mknod’
 
Old 03-31-2010, 09:29 AM   #10
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Original Poster
Rep: Reputation: 16
The problem is solved .
Thanks for help , Sergei Steshenko,
It's solved without executing shell command.It also sounds nonsense that executing shell commands in a module.
I'll copy-paste solution here, maybe later, it'll help people

Last edited by Dufresne; 03-31-2010 at 09:30 AM.
 
Old 04-01-2010, 03:37 AM   #11
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Original Poster
Rep: Reputation: 16
Solution :
Code:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h> 
#include <asm/uaccess.h>  
#include <linux/device.h>  

/* Function Prototypes 		*/
int init_module(void);				// Initialize module
void cleanup_module(void);		        				
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 *); 

#define SUCCESS 0
#define ERROR -1
#define DEVICE_NAME "char01" 
#define BUF_LEN 80

static int Major; 
static int Device_Open = 0; 

static char msg[BUF_LEN]; 
static char *msg_Ptr;

static struct class *dev_Class =NULL;  

static struct file_operations fops ={
	.read = device_read,                           
	.write = device_write,                        
	.open = device_open,
	.release = device_release
};

int init_module(void)
{
	struct device *chr_dev =NULL ;  
	Major = register_chrdev(0, DEVICE_NAME, &fops);
	   if (Major < 0) {
	     printk ("Registering the character device failed with %d\n", Major);
	     return Major;
	   }
	
	   printk("<1>I was assigned major number %d.  To talk to\n", Major);
	   printk("<1>the driver, create a dev file with\n");
		 printk("'mknod /dev/hello c %d 0'.\n", Major);
	   printk("<1>Try various minor numbers.  Try to cat and echo to\n");
		 printk("the device file.\n");
	   printk("<1>Remove the device file and module when done.\n");

	dev_Class = class_create(THIS_MODULE,DEVICE_NAME); 
	
	if( dev_Class == NULL)
	{
		printk( KERN_ALERT "Error!Class couldn't be created!\n" );
		return ERROR ;
	}
	printk( KERN_INFO "Class created!\n" );

	chr_dev = device_create( dev_Class , NULL , MKDEV(Major,0),NULL,DEVICE_NAME);
	
	if( chr_dev == NULL ) 
	{
		printk( KERN_ALERT "Error!Device file couldnt be created\n" );
		return ERROR ;
	}
	printk( KERN_INFO "Device created.Now it can be reached from \dev path\n" );
	return SUCCESS;
}

static void unregister(void) 
{
	unregister_chrdev(Major,DEVICE_NAME);
	
	printk(KERN_WARNING "Major :%d, Device :%s \n",Major, DEVICE_NAME);
	
}

void cleanup_module(void)
{
     int ret ;
     
     device_destroy(dev_Class,MKDEV(Major,0));
     class_destroy(dev_Class);       
     ret = unregister_chrdev(Major, DEVICE_NAME);
     if (ret < 0) printk("Error in unregister_chrdev: %d\n", ret);
}


static int device_open(struct inode *inode, struct file *file)
{
	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 int device_release(struct inode *inode, struct file *file)
{
	Device_Open--; 
	module_put(THIS_MODULE);
	return 0;
}


static ssize_t device_read(struct file *filp, 
				char *buffer,	
				size_t length,	
				loff_t * offset)

{

	int bytes_read = 0;

	if (*msg_Ptr == 0)
		return 0;

	while (length && *msg_Ptr) {
		put_user(*(msg_Ptr++), buffer++);
	
		length--;
		bytes_read++;
	}
	return bytes_read;
}
static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
	printk(KERN_WARNING "Not supported.\n");
	return -EINVAL;
}

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Nazmi Altun");
MODULE_DESCRIPTION("CHARACTER DEVICE");
;

Last edited by Dufresne; 04-01-2010 at 03:39 AM.
 
1 members found this post helpful.
  


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
how to read from a character device Aquarius_Girl Linux - General 3 10-05-2009 10:33 PM
Difference between using Raw block device with O_DIRECT and Raw Character Device srithi Linux - Newbie 1 08-19-2009 10:52 AM
Difference between Block device and Character device quartslove Fedora 2 11-29-2006 08:50 PM
Character device driver calsoft_pg Linux - Software 0 01-03-2006 12:17 PM
How to change character device file name? taphos Linux - General 2 03-28-2004 08:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration