Re: character device driver reading only last character of buffer

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

 



Since you people think me to be a newbie or moron or some thing like copy pasting the code so I am explaining each and every line of it what it is doing but still lectures are lectures and funda's don't work show me real thing or else do not reply.



#include <linux/init.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 */

The section declared above is used to include header files.

MODULE_LICENSE("Dual BSD/GPL");

MODULE_LICENSE is an exported symbol.

int bond_open(struct inode *inode, struct file *filp);
int bond_release(struct inode *inode, struct file *filp);
ssize_t bond_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t bond_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);

just above defined four functions which will be used in structure file_operations which is of type defined in fs.h


void bond_exit(void);
int bond_init(void);

my init and exit modules of code.

struct file_operations bond_fops = {
  .read = bond_read,
  .write = bond_write,
  .open = bond_open,
  .release = bond_release
declared as above as per new C99 rules so that rest of the file_operations which are not defined be declared NULL
};

above  operations corresponding to the system calls an application can apply to a file : file operations as in fs.h

module_init(bond_init);
module_exit(bond_exit);

my init and exit modules

int bond_major = 60;

there is another way alloc_chrdrv but I am using register_chrdrv so defining the major number


char *bond_buffer;

device buffer to store data when user programs access it

int bond_init(void) {
  int result;


  result = register_chrdev(bond_major, "bond", &bond_fops);
  if (result < 0) {
    printk(KERN_ALERT  "memory: cannot obtain major number %d\n", bond_major);
    return result;
  }


  bond_buffer = kmalloc(14, GFP_KERNEL);
giving 14 bytes to buffer three types GFP_KERNEL,GFP_ATOMIC one more I forgot

  if (!bond_buffer) {
    result = -ENOMEM;
    goto fail;
  }
in case of error above will stop execution

  memset(bond_buffer, 0, 14);

filling all the bytes of memory Claros pointed me here that if I ever used malloc function in my life so mentioning this was important for him.


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

  fail:
    bond_exit();
    return result;
same when returns a -ve value if fail to register the major number 60
}




void bond_exit(void) {

  unregister_chrdev(bond_major, "bond");


  if (bond_buffer) {
    kfree(bond_buffer);
  }

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

}

just above is a clean up module when driver exits or unloads


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


  return 0;
}

will be needed when a process opens the file

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

  return 0;
}

when releasing device driver

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

  copy_to_user(buf,bond_buffer,count<14 ? count:14);
This function works as given here
http://www.gnugeneration.com/mirrors/kernel-api/r4299.html
and an example of this in the current kernel is linux-2.6/drivers/char/snsc.c
they implemented a function scdrv_write I implemented bond_write both implementations are different
do you people get that or still some lecture is missing.
}

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

  copy_from_user(bond_buffer,buf,count<14 ? count : 14);
from the userspace buf copy the bytes whose total number is equal to count to bond_buffer
  return 1;
}

The above fundas and lectures do not work.You people give lectures or show attitude but only if the code works.

[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