This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: vpenkoff on Sep 3rd, 2012  |  syntax: C  |  size: 2.27 KB  |  views: 89  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/errno.h>
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/gpio.h>
  8. #include <asm/uaccess.h>
  9. #define DEVICE_NAME "gpiomod"
  10.  
  11.  
  12. // MODULE ATRIBUTES
  13. MODULE_LICENSE("GPL");
  14. MODULE_AUTHOR("VPenkoff");
  15. MODULE_DESCRIPTION("GPIO mod");
  16.  
  17. //Global Vars
  18. static int Major;
  19.  
  20. // Function Declarations
  21. int gpiomod_open(struct inode *inode, struct file *filp);
  22. int gpiomod_release(struct inode *inode, struct file *filp);
  23. ssize_t gpiomod_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
  24. ssize_t gpiomod_write( struct file *filp, const char *buf, size_t count, loff_t *f_p$
  25.  
  26.  
  27. // File Operations
  28. struct file_operations gpiomod_fops = {
  29.         .open = gpiomod_open,
  30.         .write = gpiomod_write,
  31.         .read = gpiomod_read,
  32.         .release = gpiomod_release
  33. };
  34.  
  35.  
  36. // module load
  37. static int gpiomod_init(void) {
  38.         Major = register_chrdev(222, DEVICE_NAME, &gpiomod_fops);
  39.         if (Major<0){
  40.                 printk(KERN_ALERT "Device registration failed...\n");
  41.                 return -ENODEV;
  42.         } else {
  43.                 printk(KERN_ALERT "Deivce registered...\n");
  44.         }
  45.         return Major;
  46. }
  47.  
  48.  
  49. // Method: Open
  50. int gpiomod_open(struct inode *inode, struct file *filp) {
  51.  
  52.         gpio_is_valid(42);
  53.         gpio_request(42, "gpiomod");
  54.         gpio_direction_output(42, 1);
  55.         gpio_set_value(42, 1);
  56.         gpio_export(42, true);
  57.         return 0;
  58. }
  59.  
  60. // Method: Release
  61.  
  62. int gpiomod_release(struct inode *inode, struct file *filp) {
  63.  
  64.         gpio_free(42);
  65.         return 0;
  66. }
  67.  
  68. // Method: Write
  69. ssize_t gpiomod_write( struct file *filp, const char *buf, size_t count, loff_t *f_p$
  70.        
  71.         const char *tmp;
  72.         char gpiomod_buffer;
  73.         tmp=buf+count-1;
  74.         copy_from_user(&gpiomod_buffer, tmp, 1);
  75.         return 1;
  76. }
  77. // Method: Read
  78.  
  79. ssize_t gpiomod_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) {
  80.  
  81.         char gpiomod_buffer;
  82.         copy_to_user(buf, &gpiomod_buffer, 1);
  83.         if (*f_pos == 0) {
  84.                 *f_pos+=1;
  85.                 return 1;
  86.         } else {
  87.                 return 0;
  88.         }
  89. }
  90.  
  91. module_init(gpiomod_init);
  92. module_exit(gpiomod_exit);
clone this paste RAW Paste Data