Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2011 - Raphael S.Carvalho (a.k.a Utroz)
- * Linux Device Drivers - A simple module
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details. You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
- */
- /* Makefile */
- obj-m := module.o
- /* How to set up the module */
- make -C /lib/modules/`uname -r`/build/ M=$PWD modules
- insmod module.ko howmany=10 whom="Test" // insert module
- dmesg or cat /var/log/messages // show module
- rmmod module // remove module.
- /* Result in Display */
- [ 6186.736200] The process is "insmod" (pid 7523)
- [ 6186.736202] Kernel Version: 0020625
- /* Module code */
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/sched.h>
- #include <linux/version.h>
- #include <linux/kdev_t.h>
- #include <linux/fs.h>
- static char *whom = "world";
- static int howmany = 1;
- module_param(howmany, int, S_IRUGO);
- module_param(whom, charp, S_IRUGO);
- static dev_t major = 0;
- static void inline dec_to_hex(char *s, unsigned dec)
- {
- unsigned rest, i;
- memset (s, '0', 6);
- for (i = 6; dec >= 16; dec /= 16, i--)
- s[i] = ((rest = dec % 16) >= 10) ?
- rest - 10 + 'A' : rest + '0';
- s[i] = dec + '0';
- }
- static int __init start_module(void)
- {
- char current_v[8] = { 0 };
- int result;
- dev_t dev, minor = 0;
- if(major) {
- dev = MKDEV(major, minor);
- result = register_chrdev_region(dev, 2, "scull");
- } else {
- result = alloc_chrdev_region(&dev, minor, 2, "scull");
- major = MAJOR(dev);
- }
- if (result < 0) {
- printk(KERN_WARNING "can't get major %d", major);
- return result;
- }
- printk(KERN_INFO "Minor: %d Major: %d", minor, major);
- dec_to_hex(current_v, LINUX_VERSION_CODE);
- printk(KERN_INFO "Whom: %s Howmany: %d", whom, howmany);
- printk(KERN_INFO "The process is \"%s\" (pid %i)\n",
- current->comm, current->pid);
- printk(KERN_INFO "Kernel Version: %s\n",
- current_v);
- return 0;
- }
- static void __exit clean_up(void)
- {
- unregister_chrdev_region(major, 2);
- printk(KERN_ALERT "Closing the module.\n");
- }
- module_init(start_module);
- module_exit(clean_up);
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_AUTHOR("Raphael S.Carvalho");
- MODULE_DESCRIPTION("This module is only a test.");
- MODULE_VERSION("0.0.1");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement