Skip to content

Instantly share code, notes, and snippets.

@deepakkarki
Created June 8, 2014 03:57
Show Gist options
  • Save deepakkarki/77ec898f642336bc4b66 to your computer and use it in GitHub Desktop.
Save deepakkarki/77ec898f642336bc4b66 to your computer and use it in GitHub Desktop.
___________C Code - user space______________
int *src = (int *)mmap (NULL, 4096, PROT_READ, MAP_SHARED, m_fd, 0);
___________Kernel space driver_______________
/*
*
* BIN FILE SYSFS - for mmap'ing. share mem b/w userspace, kernel and PRU
*
*/
#define BIN_ATTR(_name,_mode,_size,_read,_write,_mmap) { \
.attr = { .name = __stringify(_name), .mode = _mode }, \
.size = _size, \
.read = _read, \
.write = _write, \
.mmap = _mmap, \
}
void pru_vma_open(struct vm_area_struct *vma)
{
printk(KERN_NOTICE "PRU Speak VMA open, virt %lx, phys %lx\n",
vma->vm_start, vma->vm_pgoff);
}
void pru_vma_close(struct vm_area_struct *vma)
{
printk(KERN_NOTICE "PRU Speak VMA close.\n");
}
static struct vm_operations_struct pru_remap_vm_ops = {
.open = pru_vma_open,
.close = pru_vma_close,
};
static ssize_t bin_file_read(struct file * f, struct kobject *kobj, struct bin_attribute *attr, char *buffer, loff_t pos, size_t size)
{
printk(KERN_INFO "READ called on BIN FILE\n");
return (ssize_t)0;
}
static ssize_t bin_file_write(struct file *f, struct kobject *kobj, struct bin_attribute *attr, char *buffer, loff_t pos, size_t size)
{
printk(KERN_INFO "WRITE called on BIN FILE\n");
return size;
}
static int bin_file_mmap(struct file *f, struct kobject *kobj, struct bin_attribute *attr, struct vm_area_struct *vma)
{
//printk(KERN_INFO "MMAP called on BIN FILE, PAGE_SHIFT value : %d\n", PAGE_SHIFT);
if (remap_pfn_range(vma, vma->vm_start,(unsigned long)((int)shm.paddr >> PAGE_SHIFT), vma->vm_end - vma->vm_start, vma->vm_page_prot))
{
printk("MMAP failed\n");
return -EAGAIN;
}
vma->vm_ops = &pru_remap_vm_ops;
pru_vma_open(vma);
printk("physical address that was to be maped %x\n: ", (int)shm.paddr);
printk("MMAP successful\n");
return 0;
}
static struct bin_attribute pru_speak_bin_attr = BIN_ATTR(pru_speak_binfile, S_IWUSR | S_IRUGO, PAGE_SIZE,
bin_file_read, bin_file_write, bin_file_mmap);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment