Advertisement
wandrake

Untitled

Jun 26th, 2012
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/ioport.h> /* request_region, check_region, release_region */
  4. #include <linux/init.h>         /* Needed for the macros */
  5.  
  6. #define MAGICNUM 0x14000000
  7. #define CRIME_MAJOR_NUM 300
  8.  
  9. static int crime_open(struct inode*, struct file*);
  10. static int crime_close(struct inode*, struct file*);
  11. static ssize_t crime_write(struct file*, const char __user*, size_t, loff_t*);
  12. static ssize_t crime_read(struct file*, char*, size_t, loff_t*);
  13. static int crime_ioctl(struct inode*, struct file*, unsigned int, unsigned long);
  14.  
  15. struct file_operations crime_fops = {
  16.     .owner = THIS_MODULE,
  17.     .open = crime_open,
  18.     .release = crime_close,
  19.     .read = crime_read,
  20.     .write = crime_write,
  21.     .ioctl = crime_ioctl
  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_usr(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 ssize_t
  74. crime_ioctl(struct inode* inode, struct file* file, unsigned int cmd, unsigned long data) {
  75.     printk("crime_ioctl called with parameters %d %d\n", cmd, data);
  76.     return 0;
  77. }
  78.  
  79. module_init(crime_init);
  80. module_exit(crime_exit);
  81.  
  82. MODULE_AUTHOR("author");
  83. MODULE_DESCRIPTION("description");
  84. MODULE_LICENSE("license");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement