Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- #include <linux/kernel.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- #include <linux/fs.h>
- #include <linux/interrupt.h>
- #include <linux/kdev_t.h>
- #include <linux/types.h>
- #include <linux/uaccess.h>
- #include <linux/proc_fs.h>
- #define PROCFS_MAX_SIZE 1024
- #define PROCFS_NAME "gusproc"
- MODULE_LICENSE("Dual BSD/GPL");
- char data[100];
- static struct proc_dir_entry *gusproc;
- static char procfs_buffer[PROCFS_MAX_SIZE];
- static unsigned long procfs_buffer_size = 0;
- static ssize_t dev_read(struct file *file, char* buf, size_t len, loff_t* offset) {
- if (*offset >= sizeof(data)) return 0;
- copy_to_user(buf, data, sizeof(data));
- *offset += len;
- return len;
- }
- static ssize_t dev_write(struct file *file, const char* buf, size_t len, loff_t* offset) {
- memcpy(data, buf, len<=100 ? len : 99);
- data[len<=100 ? len : 99]='\0';
- printk(KERN_INFO "%s", data);
- return len;
- }
- static struct file_operations fops = {
- .read = dev_read,
- .write = dev_write,
- };
- static struct file_operations procfs_ops = {
- .read = dev_read,
- .write = dev_write,
- };
- int procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data) {
- int ret;
- printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME);
- if (offset > 0) ret = 0;
- else {
- memcpy(buffer, procfs_buffer, procfs_buffer_size);
- ret = procfs_buffer_size;
- }
- return ret;
- }
- int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) {
- procfs_buffer_size = count;
- if (procfs_buffer_size > PROCFS_MAX_SIZE) procfs_buffer_size = PROCFS_MAX_SIZE;
- if (copy_from_user(procfs_buffer, buffer, procfs_buffer_size)) return -EFAULT;
- return procfs_buffer_size;
- }
- uint8_t major;
- static struct class* myclass = NULL;
- static int hello_init(void) {
- //Chardev
- printk(KERN_INFO "Hello, world\n");
- major = register_chrdev(0, "mydev", &fops);
- printk(KERN_INFO "MAJOR = %d", major);
- myclass = class_create(THIS_MODULE, "myclass");
- device_create(myclass, NULL, MKDEV(major, 0), NULL, "mydev%d", 0);
- //Procfs
- gusproc = proc_create(PROCFS_NAME, 0, NULL, &procfs_ops);
- if (!gusproc) {
- remove_proc_entry(PROCFS_NAME, NULL);
- printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
- PROCFS_NAME);
- return -ENOMEM;
- }
- printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME);
- return 0;
- }
- static void hello_exit(void) {
- //Chardev
- device_destroy(myclass, MKDEV(major, 0));
- class_unregister(myclass);
- class_destroy(myclass);
- unregister_chrdev_region(MKDEV(major, 0), 1);
- printk(KERN_INFO "Goodbye, cruel world\n");
- //Procfs
- remove_proc_entry(PROCFS_NAME, NULL);
- printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
- }
- module_init(hello_init);
- module_exit(hello_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement