Advertisement
Guest User

SRAM Map to files driver

a guest
Feb 6th, 2015
2,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.49 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/moduleparam.h>
  3. #include <linux/version.h>
  4. #include <linux/kernel.h>
  5. #include <linux/types.h>
  6. #include <linux/kdev_t.h>
  7. #include <linux/fs.h>
  8. #include <linux/device.h>
  9. #include <linux/cdev.h>
  10. #include <linux/uaccess.h>
  11. #include <asm/io.h>
  12.  
  13. #define MAXDEVICES 4
  14. //#define GETSTARTADDR(x, y) start_addr_b ## x ## _c ## y
  15. static int start_addr_b1c1 = 0x3F000000;
  16. static int start_addr_b1c2 = 0x3F002000;
  17. static int start_addr_b2c1 = 0x3F004000;
  18. static int start_addr_b2c2 = 0x3F006000;
  19. static int FMRAM_BASE = 0x3F000000;
  20. static int FMRAM_SIZE = 0x00002000; /* 8k bytes */
  21. static char* dev_names[] = {"_B1C1", "_B1C2", "_B2C1", "_B2C2"};
  22.  
  23. module_param(start_addr_b1c1, int, 0644);
  24. MODULE_PARM_DESC(start_addr, "Starting Address, buffer 1 channel 1");
  25. module_param(start_addr_b1c2, int, 0644);
  26. MODULE_PARM_DESC(start_addr, "Starting Address, buffer 1 channel 2");
  27. module_param(start_addr_b2c1, int, 0644);
  28. MODULE_PARM_DESC(start_addr, "Starting Address, buffer 2 channel 1");
  29. module_param(start_addr_b2c2, int, 0644);
  30. MODULE_PARM_DESC(start_addr, "Starting Address, buffer 2 channel 2");
  31. module_param_named(data_size, FMRAM_SIZE, int, 0644);
  32. MODULE_PARM_DESC(data_size, "Size of data, channel 2");
  33.  
  34. static void __iomem *fmram;
  35. static dev_t first;
  36. static struct cdev c_dev;
  37. static struct class *cl;
  38.  
  39. static int my_open(struct inode *i, struct file *f)
  40. {  
  41.     int minor = MINOR(i->i_rdev);
  42.     printk("minor = %d", minor);
  43.     switch (minor)
  44.     {
  45.         case 0: FMRAM_BASE = start_addr_b1c1; break;
  46.         case 1: FMRAM_BASE = start_addr_b1c2; break;
  47.         case 2: FMRAM_BASE = start_addr_b2c1; break;
  48.         case 3: FMRAM_BASE = start_addr_b2c2; break;
  49.         default: printk(KERN_ERR "Invalid device minor: %d\n", minor);
  50.     }
  51.     if ((fmram = ioremap(FMRAM_BASE, FMRAM_SIZE)) == NULL)
  52.     {
  53.         printk(KERN_ERR "Mapping FM RAM memory failed\n");
  54.         return -1;
  55.     }
  56.     return 0;
  57. }
  58.  
  59. static int my_close(struct inode *i, struct file *f)
  60. {
  61.     iounmap(fmram);
  62.     return 0;
  63. }
  64.  
  65. static ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off)
  66. {
  67.     int i;
  68.     u8 byte;
  69.  
  70.     if (*off >= FMRAM_SIZE)
  71.     {
  72.         return 0;
  73.     }
  74.     if (*off + len > FMRAM_SIZE)
  75.     {
  76.         len = FMRAM_SIZE - *off;
  77.     }
  78.     for (i = 0; i < len; i++)
  79.     {
  80.         byte = ioread8((u8 *)fmram + *off + i);
  81.         if (copy_to_user(buf + i, &byte, 1))
  82.         {
  83.             return -EFAULT;
  84.         }
  85.     }
  86.     *off += len;
  87.  
  88.     return len;
  89. }
  90.  
  91. static ssize_t my_write(struct file *f, const char __user *buf, size_t len, loff_t *off)
  92. {
  93.     int i;
  94.     u8 byte;
  95.  
  96.     if (*off >= FMRAM_SIZE)
  97.     {
  98.         return 0;
  99.     }
  100.     if (*off + len > FMRAM_SIZE)
  101.     {
  102.         len = FMRAM_SIZE - *off;
  103.     }
  104.     for (i = 0; i < len; i++)
  105.     {
  106.         if (copy_from_user(&byte, buf + i, 1))
  107.         {
  108.             return -EFAULT;
  109.         }
  110.         iowrite8(byte, (u8 *)fmram + *off + i);
  111.     }
  112.     *off += len;
  113.  
  114.     return len;
  115. }
  116.  
  117. static struct file_operations fmram_fops =
  118. {
  119.     .owner = THIS_MODULE,
  120.     .open = my_open,
  121.     .release = my_close,
  122.     .read = my_read,
  123.     .write = my_write
  124. };
  125.  
  126. static int __init fmram_init(void) /* Constructor */
  127. {
  128.  
  129.     int i, rc = 0;
  130.     if (alloc_chrdev_region(&first, 0, MAXDEVICES, "fmram") < 0)
  131.     {
  132.         return -1;
  133.     }
  134.     if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL)
  135.     {
  136.         unregister_chrdev_region(first, 1);
  137.         return -1;
  138.     }
  139.    
  140.     cdev_init(&c_dev, &fmram_fops);
  141.     if (cdev_add(&c_dev, first, 1) == -1)
  142.     {
  143.             device_destroy(cl, first);
  144.             class_destroy(cl);
  145.             unregister_chrdev_region(first, 1);
  146.  
  147.     }
  148.     for (i= 0; i< 4; i++)
  149.     {
  150.         if (device_create(cl, NULL, MKDEV(MAJOR(first), MINOR(first)+i), NULL, "fmram%s",
  151.                     dev_names[i]) == NULL)
  152.         {
  153.             rc = -1;
  154.             break;
  155.         }
  156.  
  157.  
  158.     }
  159.     if (rc < 0)
  160.     {
  161.         class_destroy(cl);
  162.         unregister_chrdev_region(first, 1);
  163.         return -1;
  164.     }
  165.    
  166.     return 0;
  167. }
  168.  
  169. static void __exit fmram_exit(void) /* Destructor */
  170. {
  171.     int i;
  172.     cdev_del(&c_dev);
  173.     for(i = 0; i< 4; i++)
  174.         device_destroy(cl, MKDEV(first, i));
  175.     class_destroy(cl);
  176.     unregister_chrdev_region(first, 1);
  177. }
  178.  
  179. module_init(fmram_init);
  180. module_exit(fmram_exit);
  181.  
  182. MODULE_LICENSE("GPL");
  183. MODULE_AUTHOR("Prateek Sharma (b48601)");
  184. MODULE_DESCRIPTION("FM RAM Driver");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement