Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-11-2008, 07:02 AM   #1
Registered: Aug 2008
Location: Tiruchirappalli, India
Distribution: UBUNTU 11.04
Posts: 368

Rep: Reputation: 31
How to test the Char device program?

i compiled the following program without error.The concept of the program is reading the data from the device file...i dont know how to check this output..can anyone help me to check this program output and the explanation why?....Thank you

This is the program which i compiled it

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

int init_module(void);
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 DEVICE_NAME "chardev"
#define BUF_LEN 80
static int Major;
static int Device_Open = 0;
static char msg[BUF_LEN];
static char *msg_Ptr;

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

int init_module(void)
Major = register_chrdev(0, DEVICE_NAME, &fops);

if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", Major);
return Major;

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;

void cleanup_module(void)

int ret = unregister_chrdev(Major, DEVICE_NAME);
if (ret < 0)
printk(KERN_ALERT "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;

sprintf(msg, "I already told you %d times Hello world!\n", counter++);
msg_Ptr = msg;

return SUCCESS;
static int device_release(struct inode *inode, struct file *file)
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++);
return bytes_read;

static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
return -EINVAL;

Last edited by shariefbe; 09-11-2008 at 07:04 AM.
Old 09-15-2008, 01:03 AM   #2
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185
Blog Entries: 2

Rep: Reputation: 31
You need an user level test prog to test the driver I guess


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
char device programming shariefbe Linux - Newbie 1 10-04-2008 03:33 PM
how to test a simple char driver in FC5? agpperera Linux - Newbie 2 10-17-2007 01:16 AM
Doubt in char device driver for linux 2.6 tomkaith13 Programming 6 11-27-2006 04:46 AM
char device module the_uplink2x Programming 2 05-24-2005 10:53 PM
char device i810_rng djgerbavore Linux - Newbie 1 08-18-2004 08:33 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:40 AM.

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