Advertisement
wandrake

Untitled

Jun 26th, 2012
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>   /* Needed for the macros */
  4. /* The character device definitions are here */
  5. #include <linux/fs.h>
  6.  
  7. #define MAGICNUM 0x14000000
  8. #define CRIME_MAJOR_NUM 300
  9.  
  10. static int crime_open(struct inode*, struct file*);
  11. static int crime_close(struct inode*, struct file*);
  12. static ssize_t crime_write(struct file*, const char __user*, size_t, loff_t*);
  13. static ssize_t crime_read(struct file*, char*, size_t, loff_t*);
  14. static int crime_ioctl(struct inode*, struct file*, unsigned int, unsigned long);
  15.  
  16. struct file_operations crime_fops = {
  17.     .owner = THIS_MODULE,
  18.     .open = crime_open,
  19.     .release = crime_close,
  20.     .read = crime_read,
  21.     .write = crime_write
  22. };
  23. int port;
  24.  
  25. static int __init crime_init(void) {
  26.     int result;
  27.    
  28.     pr_debug("crime_init called\n");
  29.    
  30.     if ((result = register_chrdev(CRIME_MAJOR_NUM, "crime", &crime_fops)) < 0)
  31.         printk(KERN_ERR, "register_chrdev %d\n", result);
  32.     return result;
  33. }
  34.  
  35. static void __exit crime_exit(void) {
  36.     printk("crime_exit called, unloading module\n");
  37. }
  38.  
  39. static ssize_t
  40. crime_read(struct file* flip, char* ubuf, size_t count, loff_t* f_pos) {
  41.     ssize_t len, ret;
  42.     const char* works = "It works!\n";
  43.    
  44.     len = min(count, strlen(works));
  45.     if (copy_to_user(ubuf, works, len)) {
  46.         ret = -EFAULT;
  47.         goto free_page;
  48.     }
  49.    
  50.     *f_pos += len;
  51.     ret = len;
  52.  
  53. free_page:
  54.     return ret;
  55. }
  56.  
  57. static int crime_open(struct inode* inode, struct file* flip) {
  58.     return 0;
  59. }
  60.  
  61. static int crime_close(struct inode* inode, struct file* flip) {
  62.     return 0;
  63. }
  64.  
  65. static ssize_t
  66. crime_write(struct file* flip, const char* __user ubuf, size_t count, loff_t* f_pos) {
  67.     /* per stampare in debug ubuf bisognerebbe copiarlo prima o quantomeno
  68.     fare dei controlli se si tratta di dati grezzi etc */
  69.     printk("crime_write was called with parameter %d\n", count);
  70.     return 0;
  71. }
  72.  
  73. static int crime_ioctl(struct inode* inode, struct file* file, unsigned int cmd, unsigned long data) {
  74.     printk("crime_ioctl called with parameters %d %d\n", cmd, data);
  75.     return 0;
  76. }
  77.  
  78. module_init(crime_init);
  79. module_exit(crime_exit);
  80.  
  81. MODULE_AUTHOR("author");
  82. MODULE_DESCRIPTION("description");
  83. MODULE_LICENSE("license");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement