Advertisement
utroz

Simple Module

Jan 15th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2011 - Raphael S.Carvalho (a.k.a Utroz)
  3.  * Linux Device Drivers - A simple module
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License as published by the Free
  7.  * Software Foundation; either version 2 of the License, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful, but WITHOUT
  11.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13.  * more details.  You should have received a copy of the GNU General Public
  14.  * License along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  16.  */
  17.  
  18. /* Makefile */
  19. obj-m := module.o
  20.  
  21. /* How to set up the module */
  22. make -C /lib/modules/`uname -r`/build/ M=$PWD modules
  23. insmod module.ko howmany=10 whom="Test" // insert module
  24. dmesg or cat /var/log/messages // show module
  25. rmmod module // remove module.
  26.  
  27. /* Result in Display */
  28.  [ 6186.736200] The process is "insmod" (pid 7523)
  29.  [ 6186.736202] Kernel Version: 0020625
  30.  
  31. /* Module code */
  32. #include <linux/init.h>
  33. #include <linux/module.h>
  34. #include <linux/sched.h>
  35. #include <linux/version.h>
  36. #include <linux/kdev_t.h>
  37. #include <linux/fs.h>
  38.  
  39. static char *whom = "world";
  40. static int howmany = 1;
  41. module_param(howmany, int, S_IRUGO);
  42. module_param(whom, charp, S_IRUGO);
  43.  
  44. static dev_t major = 0;
  45.  
  46. static void inline dec_to_hex(char *s, unsigned dec)
  47. {
  48.     unsigned rest, i;  
  49.    
  50.     memset (s, '0', 6);
  51.     for (i = 6; dec >= 16; dec /= 16, i--)
  52.     s[i] = ((rest = dec % 16) >= 10) ?
  53.     rest - 10 + 'A' : rest + '0';
  54.    
  55.     s[i] = dec + '0';
  56. }  
  57.  
  58. static int __init start_module(void)
  59. {
  60.     char current_v[8] = { 0 };
  61.     int result;
  62.     dev_t dev, minor = 0;
  63.        
  64.     if(major) {
  65.         dev = MKDEV(major, minor);
  66.         result = register_chrdev_region(dev, 2, "scull");
  67.     } else {
  68.         result = alloc_chrdev_region(&dev, minor, 2, "scull");
  69.         major = MAJOR(dev);
  70.     }
  71.     if (result < 0) {
  72.         printk(KERN_WARNING "can't get major %d", major);
  73.         return result;
  74.     }
  75.    
  76.     printk(KERN_INFO "Minor: %d Major: %d", minor, major);
  77.    
  78.     dec_to_hex(current_v, LINUX_VERSION_CODE);
  79.  
  80.     printk(KERN_INFO "Whom: %s Howmany: %d", whom, howmany);
  81.    
  82.     printk(KERN_INFO "The process is \"%s\" (pid %i)\n",
  83.            current->comm, current->pid);
  84.     printk(KERN_INFO "Kernel Version: %s\n",
  85.            current_v);
  86.     return 0;
  87. }
  88.  
  89. static void __exit clean_up(void)
  90. {
  91.     unregister_chrdev_region(major, 2);
  92.     printk(KERN_ALERT "Closing the module.\n");
  93. }
  94. module_init(start_module);
  95. module_exit(clean_up);
  96.  
  97. MODULE_LICENSE("Dual BSD/GPL");
  98. MODULE_AUTHOR("Raphael S.Carvalho");
  99. MODULE_DESCRIPTION("This module is only a test.");
  100. MODULE_VERSION("0.0.1");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement