design shared memory System V - c

I need a shared memory which should have 2 fields
mh_data header;
and
a variable data field of any type of any lenght
I used the code->
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
//#include <conio.h>
#define ERROR -1
#define SUCCESS 0
typedef char int8;
typedef unsigned int u_int32;
typedef unsigned short u_int16;
typedef int error_code;
typedef union {
u_int32 group_user; /* group/user numbers */
struct {
u_int16 grp; /* group number */
u_int16 usr; /* user number */
} grp_usr;
} owner_id;
typedef struct mh_com {
u_int16
m_sync, /* sync bytes */
m_sysrev; /* system revision check value */
u_int32
m_size; /* module size */
owner_id
m_owner; /* group/user id */
u_int32
m_name; /* offset to module name */
u_int16
m_access, /* access permissions */
m_tylan, /* type/lang */
m_attrev, /* attributes/revision */
m_edit; /* edition */
u_int32
m_needs, /* module hardware requirements flags (reserved) */
m_share, /* shared data offset */
m_symbol, /* symbol table offset */
m_exec, /* offset to execution entry point */
m_excpt, /* offset to exception entry point */
m_data, /* data storage requirement */
m_stack, /* stack size */
m_idata, /* offset to initialized data */
m_idref, /* offset to data reference lists */
m_init, /* initialization routine offset */
m_term; /* termination routine offset */
u_int32
m_dbias, /* data area bias */
m_cbias; /* code area bias */
u_int16
m_ident; /* ident code for ident program */
char
m_spare[8]; /* reserved bytes */
u_int16
m_parity; /* header parity */
int8 m_shm_obj[256];
} mh_com, *Mh_com;
typedef mh_com mh_data;
error_code _os_datmod(
const char *mod_name,
u_int32 sz,
u_int16 *attr_rev,
u_int16 *type_lang,
u_int32 perm,
void **mod_data,
mh_data **mod_head)
{
struct datmod
{
mh_data header;
int size;
/*union Data
{
char str[sz];
}data;*/
char data[sz];
//void* dataPtr;
};
int fd;
int len;
struct stat shm_mod_stat;
void *addr;
len = strlen(mod_name);
/* Open new POSIX shared memory object */
if ((fd = shm_open(mod_name, O_RDWR |O_CREAT, /* mode_t */ 0666)) == -1)
{
/* TODO: If mod_name path is invalid, then return EOS_PNNF */
return ERROR;
}
if(ftruncate(fd, sizeof(struct datmod))==-1)
{
perror("ftruncate");
shm_unlink(mod_name);
return ERROR;
}
/* do mmap */
addr = (struct datmod *)mmap(NULL, sizeof(struct datmod), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED)
{
shm_unlink(mod_name);
return ERROR;
}
((struct datmod *)addr)->size = sz;
/* mmaped address */
*mod_data = &(((struct datmod *)addr)->data);
*mod_head = &(((struct datmod *)addr)->header);
//memset((((union Data*)(*mod_data))->str),0,sz);
memset(((*(char **)mod_data)),0,sz);
//(*mod_head)->m_shm_obj = (int8 *) malloc(len + 1);
memcpy(&((*mod_head)->m_shm_obj), (mod_name), len);
(*mod_head)->m_shm_obj[len] = '\0';
close(fd);
return SUCCESS;
}
int main()
{
void* data;
mh_data * header;
char str[] = "/Sandeep";
_os_datmod(str,50,0,0,0,&data, &header);
struct DATA
{
int i;
float j;
}*dat;
dat = (struct DATA *)(data);
dat->i = 5;
dat->j = 4.2;
//strcpy((char*)data , str);
int res = fork();
if(res==0)
{
_os_datmod(str,50,0,0,0,&data, &header);
dat = (struct DATA *)(data);
printf("i=%d,j=%f\n",dat->i,dat->j);
//printf("%s",(char *)data);
}
printf("hello world\n");
}
but it prints
hello world
i=0,j=0.000000
hello world
I also need to change
struct datmod
{
mh_data header;
int size;
/*union Data
{
char str[sz];
}data;*/
char data[sz];
//void* dataPtr;
};
so that
the data field can extend to any length and any type.
So, please suggest

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

how to use the ioctl() function with audio CD drives

I am trying to write a C program using the I/O call system in Ubuntu.
I found this documentation, CDROM API from Linux-sxs.org, but I don't understand where to find those arguments.
Can you please give me an example about how to use the ioctl() function?
struct cdrom_read_audio ra
{
union cdrom_addr addr; /* REQUIRED frame address */
u_char addr_format; /* REQUIRED .....CDROM_LBA or CDROM_MSF */
int nframes; /* REQUIRED number of 2352-byte-frames to read*/
u_char *buf; /* REQUIRED frame buffer (size: nframes*2352 bytes) */
};
if (ioctl(cdrom, CDROMREADAUDIO, &ra)<0)
{
perror("ioctl");
exit(1);
}
According to the kernel documentation for the cdrom driver, cdrom.txt, the format of the command is as follows:
CDROMREADAUDIO (struct cdrom_read_audio)
usage:
struct cdrom_read_audio ra;
ioctl(fd, CDROMREADAUDIO, &ra);
inputs:
cdrom_read_audio structure containing read start
point and length
outputs:
audio data, returned to buffer indicated by ra
error return:
EINVAL format not CDROM_MSF or CDROM_LBA
EINVAL nframes not in range [1 75]
ENXIO drive has no queue (probably means invalid fd)
ENOMEM out of memory
The format of the cdrom_read_audio struct can be found in cdrom.h:
/* This struct is used by the CDROMREADAUDIO ioctl */
struct cdrom_read_audio
{
union cdrom_addr addr; /* frame address */
__u8 addr_format; /* CDROM_LBA or CDROM_MSF */
int nframes; /* number of 2352-byte-frames to read at once */
__u8 __user *buf; /* frame buffer (size: nframes*2352 bytes) */
};
It uses a union cdrom_addr type, defined in the same file:
/* Address in either MSF or logical format */
union cdrom_addr
{
struct cdrom_msf0 msf;
int lba;
};
Here we have a choice - use MSF (Mintues-Seconds-Frames) or LBA (Logical Block Addressing). Since you're reading audio, you'll probably want MSF. struct cdrom_msf0 can also be found in the header file:
/* Address in MSF format */
struct cdrom_msf0
{
__u8 minute;
__u8 second;
__u8 frame;
};
With this research, we can write a simple test:
#include <sys/ioctl.h> //Provides ioctl()
#include <linux/cdrom.h> //Provides struct and #defines
#include <unistd.h> //Provides open() and close()
#include <sys/types.h> //Provides file-related #defines and functions
#include <sys/stat.h> //Ditto
#include <fcntl.h> //Ditto
#include <stdlib.h> //Provides malloc()
#include <string.h> //Provides memset()
#include <stdint.h> //Provides uint8_t, etc
#include <errno.h> //Provides errno
#include <stdio.h> //Provides printf(), fprintf()
int main()
{
int fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
if (errno != 0)
{
fprintf(stderr, "Error opening file: %u\n", errno);
return -1;
}
struct cdrom_msf0 time; //The start read time ...
time.minute = 2;
time.second = 45;
time.frame = 0;
union cdrom_addr address; //... in a union
address.msf = time;
struct cdrom_read_audio ra; //Our data object
ra.addr = address; //With the start time
ra.addr_format = CDROM_MSF; //We used MSF
ra.nframes = CD_FRAMES; //A second - 75 frames (the most we can read at a time anyway)
uint8_t* buff = malloc(CD_FRAMES * CD_FRAMESIZE_RAW); //Frames per second (75) * bytes per frame (2352)
memset(buff, 0, CD_FRAMES * CD_FRAMESIZE_RAW); //Make sure it's empty
ra.buf = buff; //Set our buffer in our object
if (ioctl(fd, CDROMREADAUDIO, &ra) != 0) //The ioctl call
{
fprintf(stderr, "Error giving ioctl command: %u\n", errno);
return -1;
}
for (int frame = 0; frame < CD_FRAMES; frame++) //A hexdump (could be a real use for the data)
{
printf("Frame %u:", frame);
for (int byte = 0; byte < CD_FRAMESIZE_RAW; byte++)
{
printf(" %.2X", buff[frame * CD_FRAMESIZE_RAW + byte]);
}
printf("\n");
}
close(fd); //Close our file
return 0; //And exit
}
Make sure you use an audio CD, or the ioctl call will throw EIO (with a CD-ROM, for example). In reality, you might write this data to file, or process it. Either way, you'd likely end up reading more than one second using a loop.

Protecting shared memory segment between kernel and user space

I have shared memory segment created in kernel using mmap. I need to access this mapped memory from both kernel and user space. What mechanism should I use to protect the memory from concurrent access ?
I want to have something like:
Kernel module:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <linux/mm.h>
#ifndef VM_RESERVED
# define VM_RESERVED (VM_DONTEXPAND | VM_DONTDUMP)
#endif
struct dentry *file;
struct mmap_info
{
char *data;
int reference;
};
void mmap_open(struct vm_area_struct *vma)
{
struct mmap_info *info = (struct mmap_info *)vma->vm_private_data;
info->reference++;
}
void mmap_close(struct vm_area_struct *vma)
{
struct mmap_info *info = (struct mmap_info *)vma->vm_private_data;
info->reference--;
}
static int mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
struct page *page;
struct mmap_info *info;
info = (struct mmap_info *)vma->vm_private_data;
if (!info->data)
{
printk("No data\n");
return 0;
}
page = virt_to_page(info->data);
get_page(page);
vmf->page = page;
return 0;
}
struct vm_operations_struct mmap_vm_ops =
{
.open = mmap_open,
.close = mmap_close,
.fault = mmap_fault,
};
int op_mmap(struct file *filp, struct vm_area_struct *vma)
{
vma->vm_ops = &mmap_vm_ops;
vma->vm_flags |= VM_RESERVED;
vma->vm_private_data = filp->private_data;
mmap_open(vma);
return 0;
}
int mmapfop_close(struct inode *inode, struct file *filp)
{
struct mmap_info *info = filp->private_data;
free_page((unsigned long)info->data);
kfree(info);
filp->private_data = NULL;
return 0;
}
int mmapfop_open(struct inode *inode, struct file *filp)
{
struct mmap_info *info = kmalloc(sizeof(struct mmap_info), GFP_KERNEL);
info->data = (char *)get_zeroed_page(GFP_KERNEL);
memcpy(info->data, "hello from kernel this is file: ", 32);
memcpy(info->data + 32, filp->f_dentry->d_name.name, strlen(filp->f_dentry->d_name.name));
/* assign this info struct to the file */
filp->private_data = info;
return 0;
}
static const struct file_operations mmap_fops = {
.open = mmapfop_open,
.release = mmapfop_close,
.mmap = op_mmap,
};
static int __init mmapexample_module_init(void)
{
file = debugfs_create_file("mmap_example", 0644, NULL, NULL, &mmap_fops);
return 0;
}
static void __exit mmapexample_module_exit(void)
{
debugfs_remove(file);
}
module_init(mmapexample_module_init);
module_exit(mmapexample_module_exit);
MODULE_LICENSE("GPL");
User space:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#define PAGE_SIZE 4096
int main ( int argc, char **argv )
{
int configfd;
char * address = NULL;
configfd = open("/sys/kernel/debug/mmap_example", O_RDWR);
if(configfd < 0)
{
perror("Open call failed");
return -1;
}
address = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, configfd, 0);
if (address == MAP_FAILED)
{
perror("mmap operation failed");
return -1;
}
printf("Initial message: %s\n", address);
memcpy(address + 11 , "*user*", 6);
printf("Changed message: %s\n", address);
close(configfd);
return 0;
}
but with locks.
Kernel space and user space have no shared mechanisms for concurrent access protection. If you want them, you need to implement them by yourself.
It can be some sort of mutex, implemented within you kernel module, and accessed from user space via special ioctl requests:
Kernel:
DECLARE_WAIT_QUEUE_HEAD(wq);
int my_mutex_val = 0;
/*
* Lock mutex.
*
* May be used directly by the kernel or via 'ioctl(MY_CMD_LOCK)' by user.
*/
void my_mutex_lock(void)
{
spin_lock(&wq.lock);
wait_event_interruptible_locked(&wq, my_mutex_val == 0);
my_mutex_val = 1;
spin_unlock(&wq.lock);
}
/*
* Unlock mutex.
*
* May be used directly by the kernel or via 'ioctl(MY_CMD_UNLOCK)' by user.
*/
void my_mutex_unlock(void)
{
spin_lock(&wq.lock);
my_mutex_val = 0;
wake_up(&wq);
spin_unlock(&wq.lock);
}
long unlocked_ioctl (struct file * filp, unsigned int cmd, unsigned long val)
{
switch(cmd) {
case MY_CMD_LOCK:
my_mutex_lock();
break;
case MY_CMD_UNLOCK:
my_mutex_unlock();
break;
}
}
User:
int main()
{
...
ioctl(MY_CMD_LOCK);
<read data>
ioctl(MY_CMD_UNLOCK);
...
}
It can be some sort of spinlock, which value is stored in mmap-ed area (so visible both for kernel space and user space).
In any case, kernel module should be prepared for the case, when user space application doesn't follow locking conventions. This, probably, would cancel any expectation about mmap-ed area content, generated by the kernel, but kernel module shouldn't crash in that case. [This is why standard kernel's struct mutex is not used in the code above: user space may use it incorrectly].
The problem with the ioctl is you need a kernel switch every time you want to access the share info->data. If that is okay then the ioctl is good - but then why not just do a standard character read/write file operation instead?
You can also try a lock-less mechanism. In the shared info->data area add a barrier variable. When the user needs access, it will do an atomic_compare_and_xchg on the barrier variable until it is set to 0 (unused) and then set it to 1. When the kernel needs access it will do the same but set it to 2. See the gcc atomic builtin documentation.

gcc Warning Passing argument 2 of 'alt_16550_fifo_write' makes pointer from integer without a cast

I am currently working on a project involving a Cyclone V ARM Cortex A9 Processor and an external device. I am relatively new in C Programming. I am using the UART interface of the processor to send and receive data from the external with help of APIs in C. When i compile my code, I get warnings that I am passing arguments that make pointer from integer without a cast in the function alt_16550_fifo_write(). Can someone please help?
Below is my code
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions
#include <stdbool.h>
#include <sys/types.h>
#include "ergo.h"
#include "alt_clock_manager.h"
#include "hwlib.h"
#include "alt_clock_group.h"
#include "alt_hwlibs_ver.h"
#include "alt_16550_uart.h"
#include "uart.h"
#include "socal/alt_clkmgr.h"
#include "socal/alt_rstmgr.h"
#include "socal/alt_uart.h"
#include "socal/hps.h"
#include "socal/socal.h"
/* commands to control the ergo bike */
#define ERGO_CMD_GET_ADDRESS 0x11
#define ERGO_CMD_RUN_DATA 0x40
#define ERGO_CMD_SET_WATT 0x51
#define UART_MAX_DATA 20
#define enable_init TRUE
/*Global Variables*/
ergo_run_data_t ergo_run_data;
u_int8_t ergo_adr_int;
/* External Functions that are called in the main function*/
static ALT_STATUS_CODE alt_16550_reset_helper(ALT_16550_HANDLE_t * handle, bool enable_init);
static inline uint32_t alt_read_word_helper(const void * addr);
static ALT_STATUS_CODE alt_16550_write_divisor_helper(ALT_16550_HANDLE_t * handle,uint32_t divisor);
ALT_STATUS_CODE alt_clk_clock_enable(ALT_CLK_t ALT_CLK_L4_SP);
ALT_STATUS_CODE alt_clk_is_enabled(ALT_CLK_t ALT_CLK_L4_SP);
ALT_STATUS_CODE alt_clk_freq_get(ALT_CLK_t ALT_CLK_L4_SP,alt_freq_t* freq);
ALT_STATUS_CODE alt_16550_fifo_write(ALT_16550_HANDLE_t * handle,const char * buffer,size_t count);
void ergo_get_address(ALT_16550_HANDLE_t * handle);
void ergo_get_run_data(void);
void ergo_set_watt(u_int8_t ergo_adr_int, u_int8_t watt);
void ergo_reset(ALT_16550_HANDLE_t * handle,u_int8_t ergo_adr_int);
void ergo_break(void);
/*function to enable the SOCFPGA UART Clock*/
ALT_STATUS_CODE alt_clk_clock_enable(ALT_CLK_t ALT_CLK_L4_SP)
{
if (alt_clk_clock_enable(ALT_CLK_L4_SP) != ALT_E_ERROR)
{
return ALT_E_SUCCESS; // The operation was successfull
}
else
{
return ALT_E_ERROR; // The operation was not successfull
}
}
/*Function to check whether the SOCFPGA Clock is enabled*/
ALT_STATUS_CODE alt_clk_is_enabled(ALT_CLK_t ALT_CLK_L4_SP)
{
ALT_16550_HANDLE_t * handle;
handle->clock_freq = 0;
if (alt_clk_is_enabled(ALT_CLK_L4_SP) != ALT_E_TRUE)
{
return ALT_E_BAD_CLK;
}
else
{
ALT_STATUS_CODE status;
status = alt_clk_freq_get(ALT_CLK_L4_SP, &handle->clock_freq);
}
}
//function to get the clock frequency
ALT_STATUS_CODE alt_clk_freq_get(ALT_CLK_t ALT_CLK_L4_SP,alt_freq_t* freq)
{
ALT_16550_HANDLE_t * handle;
handle->clock_freq = 0;
ALT_STATUS_CODE status;
status = alt_clk_freq_get(ALT_CLK_L4_SP, &handle->clock_freq);
if (status != ALT_E_SUCCESS)
{
return status;
}
}
struct uart_data_t
{
size_t tx_count; /*amount of data to send*/
char tx_buffer[UART_MAX_DATA]; /*data to send*/
size_t rx_count; /*amount of data to send*/
char rx_buffer[UART_MAX_DATA]; /*data received*/
}uart_data_t;
/*==========================UART functions======================*/
/*----------------------------- uart_init() -------------------------*/
/**
* Übergabeparameter: -
* Return: -
* Funktion: Initialisiert UART-Schnittstelle
*---------------------------------------------------------------------*/
int main()
{
/* Open File Descriptor */
int USB = open( "/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NONBLOCK);
//Error Handling
if ( USB < 0 )
{
printf("Error beim oeffnen");
}
// Configure Port
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
// Error Handling
if ( tcgetattr ( USB, &tty ) != 0 )
{
printf("error beim tcgetattr");
}
// Save old tty parameters
tty_old = tty;
ALT_16550_HANDLE_t * handle;
handle->data;
handle->fcr;
handle->clock_freq;
handle->location; //ALT_UART0_ADDR
handle->device;
//ALT_16550_DEVICE_SOCFPGA_UART0 = 0; //This option selects UART0 in the SoC FPGA
ALT_16550_DEVICE_t device;
ALT_STATUS_CODE status;
alt_freq_t clock_freq;
void *location;
const void * addr;
bool enable_init;
uint32_t baudrate = ALT_16550_BAUDRATE_9600;
uint32_t divisor; //((handle->clock_freq + (8 * baudrate)) / (16 * baudrate));
printf("Program start \n");
// Enable the UART Clock
alt_clk_clock_enable(ALT_CLK_L4_SP);
// Helper function to reset and Initialise the UART (UART 0)
alt_16550_reset_helper(handle, enable_init);
// Helper function to carryout the actual register read.
alt_read_word_helper(addr);
//Helper function to write the divisor in Hardware
alt_16550_write_divisor_helper(handle,divisor);
//Enable the UART (UART 0)
alt_16550_enable(handle);
//Enable the FIFO
alt_16550_fifo_enable(handle);
//Get the Ergometer address
ergo_get_address(handle);
return 0;
}
/*--------------------------- ergo_get_adr() ------------------------*/
/**
* Übergabeparameter: -
* Return: -
* Funktion: Holen der Ergometer-Adreesse (1 Byte)
*---------------------------------------------------------------------*/
void ergo_get_address(ALT_16550_HANDLE_t * handle)
{
struct uart_data_t data;
/* build up data frame for address request */
data.tx_count = 1; // amount of data to send
data.tx_buffer[0] = ERGO_CMD_GET_ADDRESS;
data.rx_count = 2; /*amount of data to receive*/
/* get address from ergo bike */
alt_16550_fifo_write(handle, *ERGO_CMD_GET_ADDRESS,1);
alt_16550_fifo_read(handle,data.rx_buffer,2);
/* save ergo address if the bike responded */
if(data.rx_buffer[0] == ERGO_CMD_GET_ADDRESS)
{
ergo_adr_int = data.rx_buffer[1];
printf("%d\n",data.rx_buffer[1]);
}
/* wait for 50ms */
ergo_break();
return;
}
/*---------------------------- ergo_reset() -------------------------*/
/**
* Übergabeparameter: u_int8_t ergo_adr_int
* Return: -
* Funktion: Setzt Ergometer zurück
*---------------------------------------------------------------------*/
void ergo_reset(ALT_16550_HANDLE_t * handle,u_int8_t ergo_adr_int)
{
alt_16550_fifo_write(handle,0x12,1);
alt_16550_fifo_write(handle,ergo_adr_int,1);
ergo_break();
return;
}
/*---------------------------- ergo_break() -------------------------*/
/**
* Übergabeparameter: -
* Return: -
* Funktion: Wait for about 50 ms
*---------------------------------------------------------------------*/
void ergo_break(void)
{
u_int16_t d1;
u_int8_t d2;
//wait for ~50 ms
for(d1=0; d1 < 65535; d1++)
{
for(d2=0; d2 < 64; d2++)
{
}
}
return;
}
In your code, your function prototype is
ALT_STATUS_CODE alt_16550_fifo_write
(ALT_16550_HANDLE_t * handle,const char * buffer,size_t count);
the second parameter being const char * buffer.
While calling this function, you're using
void ergo_reset(ALT_16550_HANDLE_t * handle,u_int8_t ergo_adr_int)
{
alt_16550_fifo_write(handle,0x12,1);
alt_16550_fifo_write(handle,ergo_adr_int,1);
ergo_break();
return;
}
here, 0x12 and ergo_adr_int are not of type const char *.
0x12 is an integer constant (hexadecimal constant, to be precise) #.
ergo_adr_int is of u_int8_t type.
Hence the mismatch and the warning(s).
To Resolve
You need to pass a const char * variable as the second argument of the fuunction.
# :: As per C11 standard document, chapter 6.4.4.1, Integer constants,
hexadecimal-constant:
hexadecimal-prefix hexadecimal-digit
hexadecimal-constant hexadecimal-digit
Where
hexadecimal-prefix: one of
0x 0X
and
hexadecimal-digit: one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
Here is the declaration of you alt_16550_fifo_write function
ALT_STATUS_CODE alt_16550_fifo_write(ALT_16550_HANDLE_t * handle,const char * buffer,size_t count);
The second parameter is of a pointer type but in your program you are always passing integer values:
alt_16550_fifo_write(handle,0x12,1);
alt_16550_fifo_write(handle,ergo_adr_int,1);
You need to pass a pointer to a char. For example:
alt_16550_fifo_write(handle, &(char) {0x12}, 1);
or
char x = 0x12;
alt_16550_fifo_write(handle, &x, 1);
Prototype for alt_16550_fifo_write() is:
ALT_STATUS_CODE alt_16550_fifo_write(ALT_16550_HANDLE_t * handle,
const char * buffer, size_t count);
You are using it at two places:
alt_16550_fifo_write(handle, *ERGO_CMD_GET_ADDRESS,1);
...
alt_16550_fifo_write(handle,0x12,1);
The second parameter is wrong. It should be const char * buffer, for example some string.

Write timer in Linux device driver

I'm newbies with the module linux.
I try to create a counter module where the counter is increment on timer callback.
The result of the counter must be send to an other module (a memory module).
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <asm/system.h>
#include <asm/uaccess.h>
MODULE_AUTHOR("Helene");
MODULE_DESCRIPTION("Module memory");
MODULE_SUPPORTED_DEVICE("none");
MODULE_LICENSE("Dual BSD/GPL");
/* Global variables of the driver */
/* Buffer to store data */
char *memory_buffer;
int result;
struct file_operations memory_fops;
int memory_open(struct inode *inode, struct file *filp) {
// printk(KERN_DEBUG "Opening memory module\n");
return 0;
}
int memory_release(struct inode *inode, struct file *filp) {
// printk(KERN_DEBUG "Releasing of memory module\n");
return 0;
}
ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t *f_pos){
// printk(KERN_DEBUG "Reading memory module : %s\n", buf);
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;
}
ssize_t memory_write( struct file *filp, const char *buf, size_t count, loff_t *f_pos) {
// printk(KERN_DEBUG "Writing memory module : %s\n", buf);
copy_from_user(memory_buffer, buf, count);
return count;
}
static int __init memory_init(void) {
/* Registering device */
result = register_chrdev(0, "memory", &memory_fops);
if (result < 0) {
printk(KERN_DEBUG "memory: cannot obtain major number \n");
return result;
}
/* Allocating memory for the buffer */
memory_buffer = kmalloc(1, GFP_KERNEL);
if (!memory_buffer) {
result = -ENOMEM;
goto fail;
}
memset(memory_buffer, 0, 1);
printk(KERN_ALERT "Inserting memory module : %d\n", result);
return 0;
fail:
//memory_exit();
return result;
}
static void __exit memory_exit(void) {
/* Freeing the major number */
unregister_chrdev(result, "memory");
/* Freeing buffer memory */
if (memory_buffer) {
kfree(memory_buffer);
}
printk(KERN_DEBUG "Removing memory module\n");
}
struct file_operations memory_fops = {
owner: THIS_MODULE,
read: memory_read,
write: memory_write,
open: memory_open,
release: memory_release
};
module_init(memory_init);
module_exit(memory_exit);
The memory module works. My problem is when I call the function : filp_open/fp->f_op->write/filp_close on the function timer callback.
I have test these functions out of the timer callback and it's work.
Why the filp_open function (& co) don't work on timer callback function ?
#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 */
MODULE_LICENSE("GPL");
static struct timer_list my_timer;
int cptNbClic ;
int result;
struct file_operations timer_fops;
int write_file_system(struct file *fp, char * buf){
int nb;
mm_segment_t old_fs=get_fs();
set_fs(get_ds());
nb = fp->f_op->write(fp,buf ,10, &fp->f_pos);
set_fs(old_fs);
return nb;
}
void writeInMemory(void){
// printk(KERN_DEBUG "busy %d\n", busy);
int nbwrite;
char buf[3];
int fmemory;
fmemory=filp_open ("/dev/memory", O_WRONLY | O_APPEND | O_CREAT,0); //don't work on this function
if (fmemory==NULL){//verification de l'ouverture
printk(KERN_ALERT "filp_open error input memory!!.\n");
return -1;
}
sprintf(buf, "%d", cptNbClic);
printk(KERN_DEBUG "%d\n", cptNbClic);
nbwrite = write_file_system(fmemory, buf);
filp_close(fmemory, 0);
}
void my_timer_callback( unsigned long data )
{
cptNbClic++;
printk(KERN_DEBUG "cptNbClic %d\n", cptNbClic);
writeInMemory();
setup_timer(&my_timer, my_timer_callback, 0);
mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000));
}
static int timer_open(struct inode *inode, struct file *filp) {
/* setup your timer to call my_timer_callback */
cptNbClic = 0;
setup_timer(&my_timer, my_timer_callback, 0);
/* setup timer interval to 200 msecs */
mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000));
return 0;
}
static int timer_release(struct inode *inode, struct file *filp) {
/* Success */
printk(KERN_DEBUG "Releasing of cpt module\n");
del_timer(&my_timer);
return 0;
}
static int __init timer_init(void) {
/* Registering device */
result = register_chrdev(0, "timer", &timer_fops);
if (result < 0) {
printk(KERN_DEBUG "timer: cannot obtain major number \n");
return result;
}
printk(KERN_ALERT "Inserting timer module : %d\n", result);
return 0;
}
static void __exit timer_exit(void) {
unregister_chrdev(result, "timer");
printk(KERN_DEBUG "Removing timer module\n");
}
struct file_operations timer_fops = {
owner: THIS_MODULE,
open: timer_open,
release: timer_release
};
/* Declaration of the init and exit functions */
module_init(timer_init);
module_exit(timer_exit);
Sorry for my bad english
No need to call setup_timer function in your my_timer_callback().Already timer is setup. If you want a recurring timer then just again call mod_timer() in your handler which will updates your timer expire value and your timer happily runs again and again till del_timer() call.

Resources