Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/version.h>
- #include <linux/kernel.h>
- #include <linux/types.h>
- #include <linux/kdev_t.h>
- #include <linux/fs.h>
- #include <linux/device.h>
- #include <linux/cdev.h>
- #include <linux/uaccess.h>
- #include <asm/io.h>
- #define MAXDEVICES 4
- //#define GETSTARTADDR(x, y) start_addr_b ## x ## _c ## y
- static int start_addr_b1c1 = 0x3F000000;
- static int start_addr_b1c2 = 0x3F002000;
- static int start_addr_b2c1 = 0x3F004000;
- static int start_addr_b2c2 = 0x3F006000;
- static int FMRAM_BASE = 0x3F000000;
- static int FMRAM_SIZE = 0x00002000; /* 8k bytes */
- static char* dev_names[] = {"_B1C1", "_B1C2", "_B2C1", "_B2C2"};
- module_param(start_addr_b1c1, int, 0644);
- MODULE_PARM_DESC(start_addr, "Starting Address, buffer 1 channel 1");
- module_param(start_addr_b1c2, int, 0644);
- MODULE_PARM_DESC(start_addr, "Starting Address, buffer 1 channel 2");
- module_param(start_addr_b2c1, int, 0644);
- MODULE_PARM_DESC(start_addr, "Starting Address, buffer 2 channel 1");
- module_param(start_addr_b2c2, int, 0644);
- MODULE_PARM_DESC(start_addr, "Starting Address, buffer 2 channel 2");
- module_param_named(data_size, FMRAM_SIZE, int, 0644);
- MODULE_PARM_DESC(data_size, "Size of data, channel 2");
- static void __iomem *fmram;
- static dev_t first;
- static struct cdev c_dev;
- static struct class *cl;
- static int my_open(struct inode *i, struct file *f)
- {
- int minor = MINOR(i->i_rdev);
- printk("minor = %d", minor);
- switch (minor)
- {
- case 0: FMRAM_BASE = start_addr_b1c1; break;
- case 1: FMRAM_BASE = start_addr_b1c2; break;
- case 2: FMRAM_BASE = start_addr_b2c1; break;
- case 3: FMRAM_BASE = start_addr_b2c2; break;
- default: printk(KERN_ERR "Invalid device minor: %d\n", minor);
- }
- if ((fmram = ioremap(FMRAM_BASE, FMRAM_SIZE)) == NULL)
- {
- printk(KERN_ERR "Mapping FM RAM memory failed\n");
- return -1;
- }
- return 0;
- }
- static int my_close(struct inode *i, struct file *f)
- {
- iounmap(fmram);
- return 0;
- }
- static ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off)
- {
- int i;
- u8 byte;
- if (*off >= FMRAM_SIZE)
- {
- return 0;
- }
- if (*off + len > FMRAM_SIZE)
- {
- len = FMRAM_SIZE - *off;
- }
- for (i = 0; i < len; i++)
- {
- byte = ioread8((u8 *)fmram + *off + i);
- if (copy_to_user(buf + i, &byte, 1))
- {
- return -EFAULT;
- }
- }
- *off += len;
- return len;
- }
- static ssize_t my_write(struct file *f, const char __user *buf, size_t len, loff_t *off)
- {
- int i;
- u8 byte;
- if (*off >= FMRAM_SIZE)
- {
- return 0;
- }
- if (*off + len > FMRAM_SIZE)
- {
- len = FMRAM_SIZE - *off;
- }
- for (i = 0; i < len; i++)
- {
- if (copy_from_user(&byte, buf + i, 1))
- {
- return -EFAULT;
- }
- iowrite8(byte, (u8 *)fmram + *off + i);
- }
- *off += len;
- return len;
- }
- static struct file_operations fmram_fops =
- {
- .owner = THIS_MODULE,
- .open = my_open,
- .release = my_close,
- .read = my_read,
- .write = my_write
- };
- static int __init fmram_init(void) /* Constructor */
- {
- int i, rc = 0;
- if (alloc_chrdev_region(&first, 0, MAXDEVICES, "fmram") < 0)
- {
- return -1;
- }
- if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL)
- {
- unregister_chrdev_region(first, 1);
- return -1;
- }
- cdev_init(&c_dev, &fmram_fops);
- if (cdev_add(&c_dev, first, 1) == -1)
- {
- device_destroy(cl, first);
- class_destroy(cl);
- unregister_chrdev_region(first, 1);
- }
- for (i= 0; i< 4; i++)
- {
- if (device_create(cl, NULL, MKDEV(MAJOR(first), MINOR(first)+i), NULL, "fmram%s",
- dev_names[i]) == NULL)
- {
- rc = -1;
- break;
- }
- }
- if (rc < 0)
- {
- class_destroy(cl);
- unregister_chrdev_region(first, 1);
- return -1;
- }
- return 0;
- }
- static void __exit fmram_exit(void) /* Destructor */
- {
- int i;
- cdev_del(&c_dev);
- for(i = 0; i< 4; i++)
- device_destroy(cl, MKDEV(first, i));
- class_destroy(cl);
- unregister_chrdev_region(first, 1);
- }
- module_init(fmram_init);
- module_exit(fmram_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Prateek Sharma (b48601)");
- MODULE_DESCRIPTION("FM RAM Driver");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement