Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/errno.h>
- #include <linux/kernel.h>
- #include <linux/types.h>
- #include <linux/gpio.h>
- #include <asm/uaccess.h>
- #define DEVICE_NAME "gpiomod"
- // MODULE ATRIBUTES
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("VPenkoff");
- MODULE_DESCRIPTION("GPIO mod");
- //Global Vars
- static int Major;
- // Function Declarations
- int gpiomod_open(struct inode *inode, struct file *filp);
- int gpiomod_release(struct inode *inode, struct file *filp);
- ssize_t gpiomod_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
- ssize_t gpiomod_write( struct file *filp, const char *buf, size_t count, loff_t *f_p$
- // File Operations
- struct file_operations gpiomod_fops = {
- .open = gpiomod_open,
- .write = gpiomod_write,
- .read = gpiomod_read,
- .release = gpiomod_release
- };
- // module load
- static int gpiomod_init(void) {
- Major = register_chrdev(222, DEVICE_NAME, &gpiomod_fops);
- if (Major<0){
- printk(KERN_ALERT "Device registration failed...\n");
- return -ENODEV;
- } else {
- printk(KERN_ALERT "Deivce registered...\n");
- }
- return Major;
- }
- // Method: Open
- int gpiomod_open(struct inode *inode, struct file *filp) {
- gpio_is_valid(42);
- gpio_request(42, "gpiomod");
- gpio_direction_output(42, 1);
- gpio_set_value(42, 1);
- gpio_export(42, true);
- return 0;
- }
- // Method: Release
- int gpiomod_release(struct inode *inode, struct file *filp) {
- gpio_free(42);
- return 0;
- }
- // Method: Write
- ssize_t gpiomod_write( struct file *filp, const char *buf, size_t count, loff_t *f_p$
- const char *tmp;
- char gpiomod_buffer;
- tmp=buf+count-1;
- copy_from_user(&gpiomod_buffer, tmp, 1);
- return 1;
- }
- // Method: Read
- ssize_t gpiomod_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) {
- char gpiomod_buffer;
- copy_to_user(buf, &gpiomod_buffer, 1);
- if (*f_pos == 0) {
- *f_pos+=1;
- return 1;
- } else {
- return 0;
- }
- }
- module_init(gpiomod_init);
- module_exit(gpiomod_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement