|
| 1 | +#include <linux/init.h> |
| 2 | +#include <linux/module.h> |
| 3 | +#include <linux/types.h> |
| 4 | +#include <linux/kernel.h> |
| 5 | +#include <linux/fs.h> |
| 6 | +#include <linux/cdev.h> |
| 7 | +#include <linux/sched.h> |
| 8 | +#include <linux/device.h> |
| 9 | +#include <linux/slab.h> |
| 10 | +#include <asm/current.h> |
| 11 | +#include <linux/uaccess.h> |
| 12 | + |
| 13 | +MODULE_LICENSE("Dual BSD/GPL"); |
| 14 | + |
| 15 | +#define DRIVER_NAME "chardev" |
| 16 | +#define BUFFER_SIZE 256 |
| 17 | + |
| 18 | +static const unsigned int MINOR_BASE = 0; |
| 19 | +static const unsigned int MINOR_NUM = 2; |
| 20 | +static unsigned int chardev_major; |
| 21 | +static struct cdev chardev_cdev; |
| 22 | +static struct class *chardev_class = NULL; |
| 23 | + |
| 24 | +static int chardev_open(struct inode *, struct file *); |
| 25 | +static int chardev_release(struct inode *, struct file *); |
| 26 | +static ssize_t chardev_read(struct file *, char *, size_t, loff_t *); |
| 27 | +static ssize_t chardev_write(struct file *, const char *, size_t, loff_t *); |
| 28 | + |
| 29 | +struct file_operations chardev_fops = { |
| 30 | + .open = chardev_open, |
| 31 | + .release = chardev_release, |
| 32 | + .read = chardev_read, |
| 33 | + .write = chardev_write, |
| 34 | +}; |
| 35 | + |
| 36 | +struct data { |
| 37 | + unsigned char buffer[BUFFER_SIZE]; |
| 38 | +}; |
| 39 | + |
| 40 | +static int chardev_init(void) |
| 41 | +{ |
| 42 | + int alloc_ret = 0; |
| 43 | + int cdev_err = 0; |
| 44 | + int minor; |
| 45 | + dev_t dev; |
| 46 | + |
| 47 | + printk("The chardev_init() function has been called."); |
| 48 | + |
| 49 | + alloc_ret = alloc_chrdev_region(&dev, MINOR_BASE, MINOR_NUM, DRIVER_NAME); |
| 50 | + if (alloc_ret != 0) { |
| 51 | + printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret); |
| 52 | + return -1; |
| 53 | + } |
| 54 | + //Get the major number value in dev. |
| 55 | + chardev_major = MAJOR(dev); |
| 56 | + dev = MKDEV(chardev_major, MINOR_BASE); |
| 57 | + |
| 58 | + //initialize a cdev structure |
| 59 | + cdev_init(&chardev_cdev, &chardev_fops); |
| 60 | + chardev_cdev.owner = THIS_MODULE; |
| 61 | + |
| 62 | + //add a char device to the system |
| 63 | + cdev_err = cdev_add(&chardev_cdev, dev, MINOR_NUM); |
| 64 | + if (cdev_err != 0) { |
| 65 | + printk(KERN_ERR "cdev_add = %d\n", alloc_ret); |
| 66 | + unregister_chrdev_region(dev, MINOR_NUM); |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + chardev_class = class_create(THIS_MODULE, "chardev"); |
| 71 | + if (IS_ERR(chardev_class)) { |
| 72 | + printk(KERN_ERR "class_create\n"); |
| 73 | + cdev_del(&chardev_cdev); |
| 74 | + unregister_chrdev_region(dev, MINOR_NUM); |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + for (minor = MINOR_BASE; minor < MINOR_BASE + MINOR_NUM; minor++) { |
| 79 | + device_create(chardev_class, NULL, MKDEV(chardev_major, minor), NULL, "chardev%d", minor); |
| 80 | + } |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static void chardev_exit(void) |
| 86 | +{ |
| 87 | + int minor; |
| 88 | + dev_t dev = MKDEV(chardev_major, MINOR_BASE); |
| 89 | + |
| 90 | + printk("The chardev_exit() function has been called."); |
| 91 | + |
| 92 | + for (minor = MINOR_BASE; minor < MINOR_BASE + MINOR_NUM; minor++) { |
| 93 | + device_destroy(chardev_class, MKDEV(chardev_major, minor)); |
| 94 | + } |
| 95 | + |
| 96 | + class_destroy(chardev_class); |
| 97 | + cdev_del(&chardev_cdev); |
| 98 | + unregister_chrdev_region(dev, MINOR_NUM); |
| 99 | +} |
| 100 | + |
| 101 | +static int chardev_open(struct inode *inode, struct file *file) |
| 102 | +{ |
| 103 | + char *str = "helloworld"; |
| 104 | + int ret; |
| 105 | + |
| 106 | + struct data *p = kmalloc(sizeof(struct data), GFP_KERNEL); |
| 107 | + |
| 108 | + printk("The chardev_open() function has been called."); |
| 109 | + |
| 110 | + if (p == NULL) { |
| 111 | + printk(KERN_ERR "kmalloc - Null"); |
| 112 | + return -ENOMEM; |
| 113 | + } |
| 114 | + |
| 115 | + ret = strlcpy(p->buffer, str, sizeof(p->buffer)); |
| 116 | + if(ret > strlen(str)){ |
| 117 | + printk(KERN_ERR "strlcpy - too long (%d)",ret); |
| 118 | + } |
| 119 | + |
| 120 | + file->private_data = p; |
| 121 | + return 0; |
| 122 | +} |
| 123 | + |
| 124 | +static int chardev_release(struct inode *inode, struct file *file) |
| 125 | +{ |
| 126 | + printk("The chardev_release() function has been called."); |
| 127 | + if (file->private_data) { |
| 128 | + kfree(file->private_data); |
| 129 | + file->private_data = NULL; |
| 130 | + } |
| 131 | + return 0; |
| 132 | +} |
| 133 | + |
| 134 | +static ssize_t chardev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) |
| 135 | +{ |
| 136 | + struct data *p = filp->private_data; |
| 137 | + |
| 138 | + printk("The chardev_write() function has been called."); |
| 139 | + printk("Before calling the copy_to_user() function : %p, %s",p->buffer,p->buffer); |
| 140 | + if (copy_from_user(p->buffer, buf, count) != 0) { |
| 141 | + return -EFAULT; |
| 142 | + } |
| 143 | + printk("After calling the copy_to_user() function : %p, %s",p->buffer,p->buffer); |
| 144 | + return count; |
| 145 | +} |
| 146 | + |
| 147 | +static ssize_t chardev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) |
| 148 | +{ |
| 149 | + struct data *p = filp->private_data; |
| 150 | + |
| 151 | + printk("The chardev_read() function has been called."); |
| 152 | + |
| 153 | + if(count > BUFFER_SIZE){ |
| 154 | + count = BUFFER_SIZE; |
| 155 | + } |
| 156 | + |
| 157 | + if (copy_to_user(buf, p->buffer, count) != 0) { |
| 158 | + return -EFAULT; |
| 159 | + } |
| 160 | + |
| 161 | + return count; |
| 162 | +} |
| 163 | + |
| 164 | +module_init(chardev_init); |
| 165 | +module_exit(chardev_exit); |
0 commit comments