LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 09-23-2010, 11:59 AM   #1
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Rep: Reputation: 9
my device driver is dropping characters


I wrote a small hello world type of character device driver.
When I type echo -n "abcdef" > /dev/bond
and do a cat /dev/bond
then only last "f" of above input abcdef is displayed rest nothing is displayed.
I asked this question earlier and some people suggested me some modifications
I have done and experimented all that but I am unable to catch the error.

Can some one point out the error?
Here is the code

Code:
/* Necessary includes for device drivers */
#include <linux/init.h>
//#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_from/to_user */

MODULE_LICENSE("Dual BSD/GPL");

/* Declaration of memory.c functions */
int memory_open(struct inode *inode, struct file *filp);
int memory_release(struct inode *inode, struct file *filp);
ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t memory_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);
void memory_exit(void);
int memory_init(void);

/* Structure that declares the usual file */
/* access functions */
struct file_operations memory_fops = {
  read: memory_read,
  write: memory_write,
  open: memory_open,
  release: memory_release
};
/* Declaration of the init and exit functions */
module_init(memory_init);
module_exit(memory_exit);

/* Global variables of the driver */
/* Major number */
int memory_major = 60;
/* Buffer to store data */
char *memory_buffer;

int memory_init(void) {
  int result;

  /* Registering device */
  result = register_chrdev(memory_major, "bond", &memory_fops);
  if (result < 0) {
    printk(KERN_ALERT  "memory: cannot obtain major number %d\n", memory_major);
    return result;
  }

  /* Allocating memory for the buffer */
  memory_buffer = kmalloc(1, GFP_KERNEL); 
  if (!memory_buffer) { 
    result = -ENOMEM;
    goto fail; 
  } 
  memset(memory_buffer, 0, 10);

  printk(KERN_ALERT "Inserting bond module\n"); 
  return 0;

  fail: 
    memory_exit(); 
    return result;
}


void memory_exit(void) {
  /* Freeing the major number */
  unregister_chrdev(memory_major, "bond");

  /* Freeing buffer memory */
  if (memory_buffer) {
    kfree(memory_buffer);
  }

  printk( KERN_ALERT "Removing bond module\n");

}


int memory_open(struct inode *inode, struct file *filp) {

  /* Success */
  return 0;
}


int memory_release(struct inode *inode, struct file *filp) {
 
  /* Success */
  return 0;
}


ssize_t memory_read(struct file *filp, char *buf, 
                    size_t count, loff_t *f_pos) { 
 
  /* Transfering data to user space */ 
  copy_to_user(buf,memory_buffer,10);

  /* Changing reading position as best suits */ 
  if (*f_pos == 0) { 
    *f_pos+=1; 
    return 1; 
  } else { 
    return 0; 
  }
}

ssize_t memory_write( struct file *filp, char *buf,
                      size_t count, loff_t *f_pos) {

  char *tmp;

  tmp=buf+count-1;
  copy_from_user(memory_buffer,tmp,10);
  return 1;
}
 
Old 09-26-2010, 03:14 AM   #2
linux_hy
Member
 
Registered: Oct 2006
Posts: 66

Rep: Reputation: -2
it maybe as following the red font

ssize_t memory_write( struct file *filp, char *buf,
size_t count, loff_t *f_pos) {

char *tmp;

//tmp=buf+count-1;
tmp=buf;
copy_from_user(memory_buffer,tmp,10);
return 1;
}
 
Old 09-26-2010, 03:18 AM   #3
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
What error are you expecting when your driver does not return any error code? Put the necessary negative error return values, and ensure your variables point to valid memory addresses.

Regards,
archieval
 
Old 09-26-2010, 03:53 AM   #4
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Original Poster
Rep: Reputation: 9
Ok here are the mistakes I did
Mistake 1)
Code:
 memory_buffer = kmalloc(1, GFP_KERNEL);
allocated just 1 byte
Mistake 2)
Code:
  /* Changing reading position as best suits */
  if (*f_pos == 0) {
    *f_pos+=1;
    return 1;
  } else {
    return 0;
  }
read one character when *f_pos is at the start of file and otherwise reads nothing:
Mistake 3
in fucntion memory_write only saving the last character written:
Code:
...
  tmp=buf+count-1; // see this is the last one in "buf"
  copy_from_user(memory_buffer,tmp,1); // and that's all you copy
So these were the mistakes.
The functions that worked was
Code:
ssize_t memory_read(struct file *filp, char __user *buf, 
		size_t count, loff_t *f_pos) 
{
	if (*f_pos > 0)
		return 0;
	if (count > strlen(memory_buffer))
		count = strlen(memory_buffer);
	copy_to_user(buf,memory_buffer,count);
	*f_pos = *f_pos + count;

	return count; 
}
Similarly
Code:
ssize_t memory_write( struct file *filp, const char __user *buf,
		size_t count, loff_t *f_pos) 
{
	copy_from_user(memory_buffer, buf, count);
	return count;
}
 
  


Reply



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
my driver is dropping characters jamesbon Programming 2 09-18-2010 08:33 AM
LXer: AMD Dropping R300-R500 Support In Catalyst Driver LXer Syndicated Linux News 0 03-05-2009 03:00 AM
Multiple Device driver from single device driver???? rickhg12hs Linux - Kernel 3 05-25-2008 12:11 AM
Wrong characters mounting USB storage device jordicm Debian 1 01-15-2007 05:49 AM
Dropping Characters - kshell liguorir Linux - Software 3 11-16-2003 05:22 AM

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

All times are GMT -5. The time now is 03:06 AM.

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