#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/major.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/version.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#define __BCM_2711__
#if defined(__BCM_2837__)
#define BCM_IO_BASE 0x3F000000
#elif defined (__BCM_2711__)
#define BCM_IO_BASE 0xFE000000
#elif defined(__BCM_2835__)
#define BCM_IO_BASE 0x20000000
#endif
#define GPIO_BASE (BCM_IO_BASE + 0x200000)
#if defined (__BCM_2837__) || defined(__BCM_2835__)
#define GPIO_SIZE (180)
#elif defined (__BCM_2711__)
#define GPIO_SIZE (240)
#endif
#define GPIO_IN(g) ( *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) )
#define GPIO_OUT(g) ( *(gpio+((g)/10)) |= (1<<(((g)%10)*3)) )
#define GPIO_SET(g) (*(gpio+7) = 1<<g)
#define GPIO_CLR(g) (*(gpio+10) = 1<<g)
#define GPIO_GET(g) (*(gpio+13) & (1<<g))
#define IOCTRL_LED_ON 1000
#define IOCTRL_LED_OFF 1001
#define IOCTRL_EXIT 1002
#define BUFF_SIZE 1024
#define MAJOR_NUMBER 256
#define DEVICE_NAME "gpio_led"
#define GPIO_LED 18
#define GPIO_SW 24
static char *buffer = NULL;
static int sz_data = 0;
volatile unsigned *gpio;
struct cdev gpio_cdev;
static int switch_irq;
static irqreturn_t isr_func(int irq, void *data){
if (irq == switch_irq && !gpio_get_value(GPIO_LED)){
gpio_set_value(GPIO_LED, 1);
}
else if (irq == switch_irq && gpio_get_value(GPIO_LED)){
gpio_set_value(GPIO_LED, 0);
}
return IRQ_HANDLED;
}
static int my_open(struct inode *inode, struct file *filp){
printk("device driver open! \n");
try_module_get(THIS_MODULE);
return 0;
}
static int my_release(struct inode *inode, struct file *filp)
{
printk("<Ko> device driver release! \n");
module_put(THIS_MODULE);
return 0;
}
static ssize_t my_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
printk("<Ko> write to buffer! %s\n", buf);
if(BUFF_SIZE < count){
sz_data = BUFF_SIZE;
}
sz_data = count;
strncpy(buffer, buf, sz_data);
printk("<Ko> buffer : %s, %d\n",buffer, sz_data);
return count;
}
static ssize_t my_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
printk(KERN_DEBUG "<Ko> from, buffer %s\n", buffer);
if(!strcmp(buffer, "1")){
copy_to_user(buf, "LED ON", 7);
}
else if(!strcmp(buffer,"2")){
copy_to_user(buf,"LED OFF",8);
}
return sz_data;
}
static long my_ioctl(struct file *flip, unsigned int cmd, unsigned long arg){
printk(KERN_DEBUG "<Ko> cmd : %d \n" ,cmd);
printk(KERN_DEBUG "IOCTRL_LED_ON : %d \n" ,IOCTRL_LED_ON);
switch(cmd){
case IOCTRL_LED_ON:
printk(KERN_INFO "LED ON in kernel\n");
gpio_set_value(GPIO_LED, 1);
break;
case IOCTRL_LED_OFF:
printk(KERN_INFO "LED OFF in kernel\n");
gpio_set_value(GPIO_LED, 0);
break;
case IOCTRL_EXIT:
printk(KERN_INFO "App exit\n");
break;
default:
printk(KERN_INFO "unknown menu!\n");
break;
}
return 0;
}
static struct file_operations gpio_fops = {
.owner = THIS_MODULE,
.read = my_read,
.write = my_write,
.open = my_open,
.release = my_release,
.unlocked_ioctl = my_ioctl
};
int __init my_init(void) {
dev_t devno;
unsigned int count;
static void* map = NULL;
int err;
printk(KERN_INFO "Hello module!\n");
devno = MKDEV(MAJOR_NUMBER, 0);
register_chrdev_region(devno, 1, DEVICE_NAME);
cdev_init(&gpio_cdev, &gpio_fops);
gpio_cdev.owner = THIS_MODULE;
count = 1;
err = cdev_add(&gpio_cdev, devno, count);
if (err < 0) {
printk(KERN_ERR "ERROR : Device Add\n");
return -1;
}
gpio_request(GPIO_LED, "LED");
gpio_direction_output(GPIO_LED, 0);
gpio_request(GPIO_SW, "SWITCH");
switch_irq = gpio_to_irq(GPIO_SW);
request_irq(switch_irq, isr_func, IRQF_TRIGGER_RISING | IRQF_TRIGGER_NONE, "switch", NULL);
return 0;
}
void __exit my_exit(void){
dev_t devno = MKDEV(MAJOR_NUMBER, 0);
unregister_chrdev_region(devno, 1);
cdev_del(&gpio_cdev);
free_irq(switch_irq, NULL);
gpio_free(GPIO_SW);
gpio_free(GPIO_LED);
printk(KERN_INFO "Bye module!\n");
}
module_init(my_init);
module_exit(my_exit);
MODULE_AUTHOR("Ko");
MODULE_LICENSE("GPL v2");