Question
Im trying to write a driver device in linux where it is a stack of 10bytes. When the device is written into, the stack will
Im trying to write a driver device in linux where it is a stack of 10bytes. When the device is written into, the stack will push only ASCII lower case onto the stack i.e (97-122) and store them from the buffer. Here is what I have so far, and so the only thing i think that would need to be modify is the ssize_t memory_write method. I am confuse on how to access the string from the buffer and storing it.
/* Necessary includes for device drivers */
#include
#include "/usr/src/linux-headers-4.13.0-16-generic/include/linux/configfs.h"
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include "/usr/src/linux-headers-4.13.0-16-generic/include/linux/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, const 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, "memory", &memory_fops);
if (result < 0) {
printk(
"<1>memory: cannot obtain major number %d ", memory_major);
return result;
}
/* Allocating memory for the buffer */
memory_buffer = kmalloc(10, GFP_KERNEL);
if (!memory_buffer) {
result = -ENOMEM;
goto fail;
}
memset(memory_buffer, 0, 10);
printk("<1>Inserting memory module ");
return 0;
fail:
memory_exit();
return result;
}
void memory_exit(void) {
/* Freeing the major number */
unregister_chrdev(memory_major, "memory");
/* Freeing buffer memory */
if (memory_buffer) {
kfree(memory_buffer);
}
printk("<1>Removing memory module ");
}
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, const char *buf,
size_t count, loff_t *f_pos) {
char *tmp; int chA=97, chZ=122; if(buf>= chA || buf<=122) //something goes here but im not figuring it out
tmp=...
copy_from_user(memory_buffer,tmp,10);
return 1;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started