char driver node is not opening - c

I wrote a simple char driver for my beaglebone kernel
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/fs.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("mrigendra.chaubey#gmail.com");
#define DEVICE_NAME "experm"
#define CLASS_NAME "exp"
static struct class* myclass = NULL; ///< The device-driver class struct pointer
static struct device* mychardevice = NULL; ///< The device-driver device struct pointer
static int myopen(struct inode *, struct file *);
static int release(struct inode *, struct file *);
static int myioctl(struct inode *, struct file *, unsigned int cmd, unsigned long arg);
static size_t myread(struct file *,char * , size_t, loff_t *);
static size_t mywrite(struct file *,char * , size_t, loff_t *);
static dev_t mydev;
static int myopen(struct inode *nd, struct file *fp)
{
printk(KERN_INFO "myopen\n");
return 0;
}
static int myrelease(struct inode *nd, struct file *fp)
{
printk(KERN_INFO "myrelease\n");
return 0;
}
static int myioctl(struct inode *nd, struct file *fp, unsigned int cmd, unsigned long arg)
{
printk(KERN_INFO "myioctl\n");
return 0;
}
static size_t myread(struct file *fp, char *buf, size_t len, loff_t *ofs)
{
printk(KERN_INFO "myread\n");
return 0;
}
static size_t mywrite(struct file *fp, char *buf, size_t len, loff_t *ofs)
{
printk(KERN_INFO "mywrite\n");
return 0;
}
static struct file_operations fops = {
.open = myopen,
.release = myrelease,
.read = myread,
.write = mywrite,
.unlocked_ioctl = myioctl,
};
static int __init myinit(void)
{
int err;
//extern int alloc_chrdev_region(dev_t *, unsigned minor number, unsigned total, const char *);
err = alloc_chrdev_region(&mydev, 0, 1, "expermdev");
if(err<0)
{
printk(KERN_INFO "major and minor can't be created, err = %d\n", err);
return err;
}
//struct class * class_create ( struct module *owner, const char *name);
myclass = class_create(THIS_MODULE, CLASS_NAME);
if(IS_ERR(myclass))
{
unregister_chrdev(MAJOR(mydev), "expermdev");
printk(KERN_ALERT "Failed to register device class\n");
return PTR_ERR(myclass);
}
//struct device *device_create(struct class *cls, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...);
//This function can be used by char device classes. A struct device will be created in sysfs, registered to the specified class.
mychardevice = device_create(myclass, NULL, mydev, NULL, "expermdev");
if(IS_ERR(mychardevice))
{
class_destroy(myclass);
unregister_chrdev(MAJOR(mydev), "expermdev");
printk(KERN_ALERT "Failed to create the device\n");
return PTR_ERR(mychardevice);
}
printk(KERN_INFO "my device created correctly\n");
return 0;
}
static void __exit myexit(void)
{
device_destroy(mychardevice, mydev);
class_unregister(myclass);
class_destroy(myclass);
unregister_chrdev(MAJOR(mydev), "expermdev");
printk(KERN_INFO "exited\n");
}
module_init(myinit);
module_exit(myexit);
and app.c file
#include <stdio.h>
#include<fcntl.h>
int main()
{
int fp;
fp = open ("/dev/expermdev", O_RDWR);
if(fp < 0)
printf("file can't be opened\n");
else
printf("file opened\n");
return 0;
}
I am compiling the driver as a module and insmod it, also compiled app.c with the same cross compiler, and put this binary in bin directory. I ran this bin file, but it says
file can't be opened
What am I doing wrong?

Got it. I did not used cdev struture and added fops to it. After that I have to tell kernel about it.
cdev = cdev_alloc();//allocate memory to Char Device structure
cdev->ops = &fops;//link our file operations to the char device
result=cdev_add(cdev,mydev,1);//Notify the kernel abt the new device
also delete this cdev in exit function before unregistering the driver.
In register_chrdev_region we do both things at the same time, but in dynamic allocation of major and minor numbers we have to use cdev to do the rest.

Related

passing floating point by casting to int and print in ioctl implementation

so I have this code
this is what I am doing in ioctl implementation
if( copy_from_user(&value ,(struct aa*) arg, sizeof(value)) )
{
pr_err("Data Write : Err!\n");
}
__u64 a=value.a;
__u32 *b=(__u32 *)a;
pr_info("wow Value = [%x]\n", (int)b[0]);
but I am passing from userspace float so but my passed values are not correctly printing in printk
this is my program
struct aa
{
uint64_t a;
};
#define WR_VALUE _IOW('a','a',struct aa*)
#define RD_VALUE _IOR('a','b',struct aa*)
int main()
{
struct aa a;
float *f=(float[]){2,2,3};
a.a=(uint64_t)f;
printf("sizeof = %zu\n",sizeof(*f));
int fd;
int32_t value, number;
printf("*********************************\n");
printf("*******WWW.EmbeTronicX.com*******\n");
printf("\nOpening Driver\n");
fd = open("/dev/etx_device", O_RDWR);
if(fd < 0) {
printf("Cannot open device file...\n");
return 0;
}
printf("Enter the Value to send\n");
scanf("%d",&number);
printf("Writing Value to Driver\n");
ioctl(fd, WR_VALUE, (struct aa *) &a);
printf("Reading Value from Driver\n");
ioctl(fd, RD_VALUE, (struct aa*) &a);
printf("Value is %d\n", value);
printf("Closing Driver\n");
close(fd);
}
full code
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include<linux/slab.h> //kmalloc()
#include<linux/uaccess.h> //copy_to/from_user()
#include <linux/ioctl.h>
#include <asm/fpu/api.h>
struct aa
{
__u64 a;
};
#define WR_VALUE _IOW('a','a',struct aa *)
#define RD_VALUE _IOR('a','b',struct aa *)
struct aa value;
dev_t dev = 0;
static struct class *dev_class;
static struct cdev etx_cdev;
/*
** Function Prototypes
*/
static int __init etx_driver_init(void);
static void __exit etx_driver_exit(void);
static int etx_open(struct inode *inode, struct file *file);
static int etx_release(struct inode *inode, struct file *file);
static ssize_t etx_read(struct file *filp, char __user *buf, size_t len,loff_t * off);
static ssize_t etx_write(struct file *filp, const char *buf, size_t len, loff_t * off);
static long etx_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
/*
** File operation sturcture
*/
static struct file_operations fops =
{
.owner = THIS_MODULE,
.open = etx_open,
.unlocked_ioctl = etx_ioctl,
.release = etx_release,
};
/*
** This function will be called when we open the Device file
*/
static int etx_open(struct inode *inode, struct file *file)
{
return 0;
}
/*
** This function will be called when we close the Device file
*/
static int etx_release(struct inode *inode, struct file *file)
{
return 0;
}
static long etx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd) {
case WR_VALUE:
if( copy_from_user(&value ,(struct aa*) arg, sizeof(value)) )
{
pr_err("Data Write : Err!\n");
}
__u64 a=value.a;
__u32 *b=(__u32 *)a;
pr_info("wow Value = [%x]\n", (int)b[0]);
kernel_fpu_end();
break;
case RD_VALUE:
if( copy_to_user((struct aa*) arg, &value, sizeof(value)) )
{
pr_err("Data Read : Err!\n");
}
break;
default:
pr_info("Default\n");
break;
}
return 0;
}
/*
** Module Init function
*/
static int __init etx_driver_init(void)
{
/*Allocating Major number*/
if((alloc_chrdev_region(&dev, 0, 1, "etx_Dev")) <0){
pr_err("Cannot allocate major number\n");
return -1;
}
pr_info("Major = %d Minor = %d \n",MAJOR(dev), MINOR(dev));
/*Creating cdev structure*/
cdev_init(&etx_cdev,&fops);
/*Adding character device to the system*/
if((cdev_add(&etx_cdev,dev,1)) < 0){
pr_err("Cannot add the device to the system\n");
goto r_class;
}
/*Creating struct class*/
if((dev_class = class_create(THIS_MODULE,"etx_class")) == NULL){
pr_err("Cannot create the struct class\n");
goto r_class;
}
/*Creating device*/
if((device_create(dev_class,NULL,dev,NULL,"etx_device")) == NULL){
pr_err("Cannot create the Device 1\n");
goto r_device;
}
pr_info("Device Driver Insert...Done!!!\n");
return 0;
r_device:
class_destroy(dev_class);
r_class:
unregister_chrdev_region(dev,1);
return -1;
}
/*
** Module exit function
*/
static void __exit etx_driver_exit(void)
{
device_destroy(dev_class,dev);
class_destroy(dev_class);
cdev_del(&etx_cdev);
unregister_chrdev_region(dev, 1);
pr_info("Device Driver Remove...Done!!!\n");
}
module_init(etx_driver_init);
module_exit(etx_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("EmbeTronicX <embetronicx#gmail.com>");
MODULE_DESCRIPTION("Simple Linux device driver (IOCTL)");
MODULE_VERSION("1.5");
what am I doing wrong in kernel ioctl because I casted float to uint_64 like a.a=(uint64_t)f; and passed struct a to kernel now I want to read float *f elements passed from userspace through ioctl in ioctl implementation

Registering a Midi Device in Modern Kernel Versions

I am currently writing a Linux Kernel module that will create a virtual Midi device so that a user-space program can send midi signals to any DAW-like software. I found a wonderful function to help me in my task: int register_sound_midi(const struct file_operations * fops, int dev). The problem is that this function ceased to exist after Kernel Version 4.15. From V15 to V16 the register and unregister functions just disappear. I'm now dumbfounded on how to register a fake midi device from the kernel in versions post 4.15. How can I register a midi device in versions of Kernel post 4.15? Is it possible to make the module backwards compatible with older kernel versions?
Current Code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/sound.h>
#include <sound/core.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Module to create dummy midi devices.");
MODULE_VERSION("0.01");
static int dummy_midi_open(struct inode *, struct file *);
static int dummy_midi_release(struct inode *, struct file *);
static ssize_t dummy_midi_read(struct file *, char *, size_t, loff_t *);
static ssize_t dummy_midi_write(struct file *, const char *, size_t, loff_t *);
static struct file_operations dummy_midi_fops = {
.owner = THIS_MODULE,
.read = dummy_midi_read,
.write = dummy_midi_write,
.open = dummy_midi_open,
.release = dummy_midi_release
};
static int minor;
static int dummy_midi_open(struct inode *midi_inode, struct file *midi_file)
{
printk(KERN_INFO"Open\n");
return 0;
}
static int dummy_midi_release(struct inode *midi_inode, struct file *midi_file)
{
printk(KERN_INFO"Release\n");
return 0;
}
static ssize_t dummy_midi_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
printk(KERN_INFO"Read\n");
return 0;
}
static ssize_t dummy_midi_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
printk(KERN_INFO"Write\n");
return 0;
}
static int __init midimodule_init(void)
{
printk(KERN_INFO"Initializing Module\n");
minor = register_sound_midi(&dummy_midi_fops,-1);
printk(KERN_INFO"Registered MIDI Device: %d\n",minor);
return 0;
}
static void __exit midimodule_exit(void)
{
printk(KERN_INFO"Leaving now\n");
unregister_sound_midi(minor);
printk(KERN_INFO"Unregistered MIDI Device: %d\n",minor);
}
module_init(midimodule_init);
module_exit(midimodule_exit);

writing driver - "Failed to register device class"

I am trying to write a character device driver based on this tutorial.
But when trying to insert my module I get the following error:
$ sudo insmod modules/main/main.ko
insmod: ERROR: could not insert module modules/main/main.ko: Operation not permitted
$dmesg
[ 1085.409026] Failed to register device class
This is the corresponding code snippet:
#ifndef __KERNEL__
#define __KERNEL__
#endif
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#define CLASS_NAME "my_class"
#define DEV_NAME "my_device"
static int dev_open(struct inode *, struct file *);
static int dev_release(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
static int myCharClass;
static int myCharDevice;
int majorNumber;
static struct file_operations fops =
{
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release
};
int init_module(void)
{
printk("Hello world - DAO \n");
majorNumber = register_chrdev(0, DEV_NAME, &fops);
if (majorNumber < 0)
{
printk("Error when registering character device driver.\n");
return -1;
}
myCharClass = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(myCharClass))
{
unregister_chrdev(majorNumber, DEV_NAME);
printk(KERN_ALERT "Failed to register device class\n");
return -1;
}
myCharDevice = device_create(myCharClass, NULL, MKDEV(majorNumber, 0), NULL, DEV_NAME);
if (IS_ERR(myCharDevice))
{
class_destroy(myCharClass);
unregister_chrdev(majorNumber, DEV_NAME);
printk(KERN_ALERT "Failed to create the device\n");
return -1;
}
return 0;
}
void cleanup_module(void)
{
printk("<1> Ciao - DAO\n");
}
static int dev_open(struct inode *p_inode, struct file *p_file)
{
printk("Opened device.\n");
return 0;
}
static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *offset)
{
printk("Reading device.\n");
return 0;
}
static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset)
{
printk("Writing device\n");
return 0;
}
static int dev_release(struct inode *inodep, struct file *filep)
{
printk("Releasing device.\n");
return 0;
}
MODULE_LICENSE("GPL");
Does anybody know what I am doing incorrectly?
I had a look at this post, but it didn't really help me, because I didn't really what the issue is.
Thanks

IOCTL call not working with driver

I wrote a IOCTL driver and a corresponding ioctl app with a header file containing commands.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include "myioctl.h"
#include <linux/ioctl.h>
#define NAME MyCharDevice
//Function Prototypes
int NAME_open(struct inode *inode, struct file *filp);
int NAME_release(struct inode *indoe, struct file *filp);
ssize_t NAME_write(struct file *filp, char __user *Ubuff, size_t count, loff_t *offp);
ssize_t NAME_read(struct file *filp, char __user *Ubuff, size_t count, loff_t *offp);
int NAME_flush (struct file *filp);
int NAME_IOCTL (struct inode *inode, struct file *filp, unsigned long cmd, unsigned long val);
//Structure that defines the operations that the driver provides
struct file_operations fops =
{
.owner = THIS_MODULE,
.open = NAME_open,
.read = NAME_read,
.write = NAME_write,
.unlocked_ioctl = NAME_IOCTL,
.release = NAME_release,
.flush = NAME_flush,
};
//Structure for a character driver
struct cdev *my_cdev;
//Init Module
static int __init CharDevice_init(void)
{
int result;
int MAJOR,MINOR;
dev_t Mydev;
Mydev = MKDEV(255,0);//Create a device number
MAJOR=MAJOR(Mydev);
MINOR=MINOR(Mydev);
printk("\nThe Major Number is %d...THe Minor Number is %d\n",MAJOR,MINOR);
result=register_chrdev_region(Mydev,1,"MyCharDevice");//register device region.....
if(result<0)
{
printk(KERN_ALERT "\nThe Region requested for is not obtainable\n");
return(-1);
}
my_cdev = cdev_alloc();//allocate memory to Char Device structure
my_cdev->ops = &fops;//link our file operations to the char device
result=cdev_add(my_cdev,Mydev,1);//Notify the kernel abt the new device
if(result<0)
{
printk(KERN_ALERT "\nThe Char Devide has not been created......\n");
return (-1);
}
return 0;
}
//Cleanup Module
void __exit CharDevice_exit(void)
{
dev_t Mydev;
int MAJOR,MINOR;
Mydev=MKDEV(255,0);
MAJOR=MAJOR(Mydev);
MINOR=MINOR(Mydev);
printk("\nThe Major Number is %d...THe Minor Number is %d\n",MAJOR,MINOR);
unregister_chrdev_region(Mydev,1);//unregister the device numbers and the device created
cdev_del(my_cdev);
printk(KERN_ALERT "\nI have unregistered the stuff that was allocated.....Goodbye for ever.....\n");
return;
}
int NAME_IOCTL (struct inode *inode, struct file *filp, unsigned long cmd, unsigned long val)
{
int BAUD=0, STOP;
char PARITY, CONFIG;
printk ("In IOCTL\n");
printk("command = %d %d val = %d\n", cmd, SET_BAUD, val);
switch (cmd) {
case SET_BAUD:
get_user (BAUD, (int *)val);
printk ("The baud is %d", BAUD);
case SET_PARITY:
case SET_STOP:
case READ_CONFIG:
default:
return -1;
}
return 0;
}
//Open System Call
int NAME_open(struct inode *inode, struct file *filp)
{
printk(KERN_ALERT "\nThis is the Kernel....Open Call.....I have nothing to do.....but YOU ALL HAVE....HAHAHAHA...\n");
return 0;
}
//Close System Call
int NAME_release(struct inode *indoe, struct file *filp)
{
printk(KERN_ALERT "\nThis is the release method of my Character Driver......Bye Dudes......\n");
return 0;
}
//Write Functionality
ssize_t NAME_write(struct file *filp, char __user *Ubuff, size_t count, loff_t *offp)
{
char Kbuff[80];
unsigned long result;
ssize_t retval;
//strcpy(Kbuff,Ubuff);
result=copy_from_user((char *)Kbuff,(char *)Ubuff,count); //get user data
if(result==0)
{
printk(KERN_ALERT "\nMessage from the user......\n>>>> %s <<<<\n",Kbuff);
printk(KERN_ALERT "\n Data Successfully Written.....\n");
retval=count;
return retval;
}
else
{
printk(KERN_ALERT "\n Error Writing Data\n");
retval=-EFAULT;
return retval;
}
}
//read Functionality
ssize_t NAME_read(struct file *filp, char __user *Ubuff, size_t count, loff_t *offp)
{
char Kbuff[]="THis is some date from the kernel to the user....User,ENJOY......";
unsigned long result;
ssize_t retval;
//strcpy(Kbuff,Ubuff);
result=copy_to_user((char *)Ubuff,(char *)Kbuff,sizeof(Kbuff)); //copy to user
if(result==0)
{
//printk("\nMessage from the user......\n>>>> %s <<<<\n");
printk(KERN_ALERT "\n Data Successfully read.....\n");
retval=count;
return retval;
}
else
{
printk(KERN_ALERT"\n Error Writing Data to User\n");
retval=-EFAULT;
return retval;
}
}
int NAME_flush (struct file *filp)
{
printk("\n This is the close function of the file....");
return 0;
}
//Module over ride functions
module_init(CharDevice_init);
module_exit(CharDevice_exit);
header file
#define MAGIC 'x'
#define SET_BAUD _IOW(MAGIC,0, int)
#define SET_PARITY _IOW(MAGIC, 1, char)
#define SET_STOP _IOW(MAGIC, 2, int)
#define READ_CONFIG _IOR(MAGIC, 3, int)
c file
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include "myioctl.h"
int main()
{
int FileDesc, Baud=9600;
// char Ubuff[]="THis is the User Buffer......Sending Data to the Kernel....";
// char Kbuff[100];
FileDesc=open("/dev/MyCharDevice",O_RDWR);
if(FileDesc <0)
{
printf("\nError Opening Device\n");
exit(1);
}
ioctl (FileDesc, SET_BAUD, &Baud);
printf("%d %d \n", SET_BAUD, &Baud);
// write(FileDesc,Ubuff,sizeof(Ubuff));
// read(FileDesc,Kbuff,sizeof(Ubuff));
// printf("\n The Data read from the Kernel is\n>>>> %s <<<<\n",Kbuff);
close(FileDesc);
}
I am printing in driver what are the command and value of the argument that print like this
command = 1622004312 1074034688 val = 1622004312
So sent command is equal to the argument I sent. Why this is happening?
I used older IOCTL prototype in my driver.
it should be of this type
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35))
static int my_ioctl(struct inode *i, struct file *f, unsigned int cmd, unsigned long arg)
#else
static long my_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
#endif
In my case against my kernel
static long my_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
is the correct type.

endlessly looping when reading from character device

For a homework assignment, I have written a character device driver. It seems to work OK. I can read and write it. The problem is that when I read the device, it endlessly loops, printing out the contents of the message buffer over and over.
This seems like it should be fairly straight forward. Just use copy_to_user(), but it's proven to be very problematic.
Anyway, here is the code. I think the problem is in the gdev_read() function.
The printk's are there to serve as debugging as well as talking points, since I have to present the project in class.
/*
* Implement a generic character pseudo-device driver
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/types.h>
#include <linux/vmalloc.h>
#include <asm/uaccess.h>
/* you need these, or the kernel will be tainted */
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("A simple sample character device driver");
/*
* function prototypes
*/
int init_module(void);
void cleanup_module(void);
static ssize_t gdev_read(struct file *, char *, size_t, loff_t *);
static ssize_t gdev_write(struct file *, const char *, size_t, loff_t *);
static int gdev_open(struct inode *, struct file *);
static int gdev_release(struct inode *, struct file *);
/* macros */
#define TRUE 1
#define FALSE 0
#define MAX_MSG_LEN 64
/*
* global variables
*/
static dev_t dev_num; /* device number, for new device */
static char *mesg; /* buffer for message */
/* file operations structure, so my device knows how to act */
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = gdev_read,
.write = gdev_write,
.open = gdev_open,
.release = gdev_release,
};
/* character device struct. Declaired here, but initialized elsewhere */
struct cdev *gdev;
int init_module(void)
{
int err;
printk(KERN_ALERT "in init_module\n");
if(alloc_chrdev_region(&dev_num, 0, 1, "/dev/gdev")){
printk(KERN_INFO "Could not allocate device numbers\n");
printk(KERN_INFO "Module gdev not loaded\n");
return -1;
}
/* now I need to make the device and register it */
gdev = cdev_alloc();
gdev->owner = THIS_MODULE;
gdev->ops = &fops;
err = cdev_add(gdev, dev_num, 1);
if(err){
printk(KERN_NOTICE "Error %d adding gdev", err);
return err;
}
mesg = (char *)vmalloc(MAX_MSG_LEN);
printk(KERN_INFO "Module gdev successfully loaded.\n");
printk(KERN_INFO "gdev Major Number: %d\n", MAJOR(dev_num));
return 0;
}
void cleanup_module(void)
{
printk(KERN_ALERT "in cleanup_module\n");
unregister_chrdev_region(dev_num, 3);
vfree(mesg);
cdev_del( gdev );
printk(KERN_INFO "Module gdev unregistered\n");
}
static ssize_t gdev_read(struct file *filp, char *page,
size_t len, loff_t *offset)
{
ssize_t bytes = len < MAX_MSG_LEN ? len : MAX_MSG_LEN;
printk(KERN_ALERT "in gdev_read\n");
if(copy_to_user(page, mesg, bytes)){
return -EFAULT;
}
return bytes;
}
static ssize_t gdev_write(struct file *filp, const char *page,
size_t len, loff_t *offset)
{
ssize_t bytes = len < MAX_MSG_LEN ? len : MAX_MSG_LEN;
printk(KERN_ALERT "in gdev_write\n");
if(copy_from_user(mesg, page, bytes)){
return -EFAULT;
}
return bytes;
}
static int gdev_open(struct inode *inode, struct file *filp)
{
printk(KERN_ALERT "in gdev_open\n");
return 0;
}
static int gdev_release(struct inode *inode, struct file *filp)
{
printk(KERN_ALERT "in gdev_release\n");
/* doesn't do anything because it doesn't need too */
return 0;
}
If zero is not returned from read() (in your case gdev_read()), the read function will be called again.
To stop this you use the loff_t *offset parameter. Increment it by how many bytes you have read using (*offset) += bytes; after copy_to_user(). Next time read() is called, offset will be what you have incremented it to. Now just check how many bytes you have previously sent, and only send what you still have remaining. Your function should look like this:
static ssize_t gdev_read(struct file *filp, char *page,
size_t len, loff_t *offset)
{
ssize_t bytes = len < (MAX_MSG_LEN-(*offset)) ? len : (MAX_MSG_LEN-(*offset));
printk(KERN_ALERT "in gdev_read\n");
if(copy_to_user(page, mesg, bytes)){
return -EFAULT;
}
(*offset) += bytes;
return bytes;
}
You could use 'simple_read_from_buffer' function from 'linux/fs.h':
static ssize_t gdev_read(struct file *filep, char __user *buff, size_t count, loff_t *offp)
{
return simple_read_from_buffer(buff, count, offp, my_buffer, buffer_len);
}
'my_buffer' and 'buffer_len' are defined in your module.

Resources