Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | /* Necessary includes for drivers */ |
| 2 | #include <linux/init.h> |
| 3 | //#include <linux/config.h> |
| 4 | #include <linux/module.h> |
| 5 | #include <linux/kernel.h> /* printk() */ |
| 6 | #include <linux/slab.h> /* kmalloc() */ |
| 7 | #include <linux/fs.h> /* everything... */ |
| 8 | #include <linux/errno.h> /* error codes */ |
| 9 | #include <linux/types.h> /* size_t */ |
| 10 | #include <linux/proc_fs.h> |
| 11 | #include <linux/fcntl.h> /* O_ACCMODE */ |
| 12 | #include <linux/ioport.h> |
| 13 | #include <asm/system.h> /* cli(), *_flags */ |
| 14 | #include <asm/uaccess.h> /* copy_from/to_user */ |
| 15 | #include <asm/io.h> /* inb, outb */ |
| 16 | |
| 17 | |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | |
| 20 | /* Declaration of memory.c functions */ |
| 21 | int memory_open(struct inode *inode, struct file *filp); |
| 22 | int memory_release(struct inode *inode, struct file *filp); |
| 23 | ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t *f_pos); |
| 24 | ssize_t memory_write(struct file *filp, char *buf, size_t count, loff_t *f_pos); |
| 25 | void memory_exit(void); |
| 26 | int memory_init(void); |
| 27 | |
| 28 | /* Structure that declares the usual file */ |
| 29 | /* access functions */ |
| 30 | struct file_operations memory_fops = { |
| 31 | read: memory_read, |
| 32 | write: memory_write, |
| 33 | open: memory_open, |
| 34 | release: memory_release |
| 35 | }; |
| 36 | |
| 37 | /* Declaration of the init and exit functions */ |
| 38 | module_init(memory_init); |
| 39 | module_exit(memory_exit); |
| 40 | |
| 41 | /* Global variables of the driver */ |
| 42 | /* Major number */ |
| 43 | int memory_major = 60; |
| 44 | /* Buffer to store data */ |
| 45 | char *memory_buffer; |
| 46 | |
| 47 | int memory_init(void) { |
| 48 | |
| 49 | int result; |
| 50 | |
| 51 | /* Registering device */ |
| 52 | result = register_chrdev(memory_major, "memory", &memory_fops); |
| 53 | if (result < 0) { |
| 54 | printk( |
| 55 | "<1>memory: cannot obtain major number %d\n", memory_major); |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | /* Allocating memory for the buffer */ |
| 60 | memory_buffer = kmalloc(1, GFP_KERNEL); |
| 61 | if (!memory_buffer) { |
| 62 | result = -ENOMEM; |
| 63 | goto fail; |
| 64 | } |
| 65 | memset(memory_buffer, 0, 1); |
| 66 | |
| 67 | printk("<1>Inserting memory module\n"); |
| 68 | return 0; |
| 69 | |
| 70 | fail: |
| 71 | memory_exit(); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | void memory_exit(void) { |
| 76 | /* Freeing the major number */ |
| 77 | unregister_chrdev(memory_major, "memory"); |
| 78 | |
| 79 | /* Freeing buffer memory */ |
| 80 | if (memory_buffer) { |
| 81 | kfree(memory_buffer); |
| 82 | } |
| 83 | |
| 84 | printk("<1>Removing memory module\n"); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | int memory_open(struct inode *inode, struct file *filp) { |
| 89 | |
| 90 | /* Success */ |
| 91 | return 0; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | int memory_release(struct inode *inode, struct file *filp) { |
| 96 | |
| 97 | /* Success */ |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | ssize_t memory_read(struct file *filp, char *buf, |
| 102 | size_t count, loff_t *f_pos) { |
| 103 | |
| 104 | /* Transfering data to user space */ |
| 105 | copy_to_user(buf,memory_buffer,1); |
| 106 | |
| 107 | /* Changing reading position as best suits */ |
| 108 | if (*f_pos == 0) { |
| 109 | *f_pos+=1; |
| 110 | return 1; |
| 111 | } else { |
| 112 | return 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | ssize_t memory_write( struct file *filp, char *buf, |
| 117 | size_t count, loff_t *f_pos) { |
| 118 | |
| 119 | char *tmp; |
| 120 | |
| 121 | tmp=buf+count-1; |
| 122 | copy_from_user(memory_buffer,tmp,1); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 |
Branches:
master
