150 lines
3.3 KiB
C
150 lines
3.3 KiB
C
/*
|
|
* relay.c - driver for relay11 Test
|
|
*
|
|
* Copyright (C) 2012 Loongson Corporation
|
|
*
|
|
* 2013-5-29
|
|
*/
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/moduleparam.h>
|
|
#include <linux/miscdevice.h>
|
|
#include <linux/types.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/module.h>
|
|
#include <linux/interrupt.h>
|
|
#include <linux/ioport.h>
|
|
#include <linux/init.h>
|
|
#include <linux/err.h>
|
|
#include <linux/rcupdate.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/cdev.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/io.h>
|
|
#include <linux/of.h>
|
|
#include <linux/completion.h>
|
|
#include <asm/io.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/fcntl.h>
|
|
#include <asm/uaccess.h>
|
|
#include <linux/gpio.h>
|
|
#include <linux/time.h>
|
|
|
|
#define GPIORELAY1 47
|
|
#define GPIORELAY2 46
|
|
#define GPIORELAY3 0
|
|
#define GPIORELAY4 1
|
|
|
|
static int relay_open(struct inode *inode, struct file *file)
|
|
{
|
|
printk(KERN_ALERT"########relay open succesful#############\n");
|
|
gpio_request(GPIORELAY1,"relay1");
|
|
gpio_request(GPIORELAY2,"relay2");
|
|
gpio_request(GPIORELAY3,"relay3");
|
|
gpio_request(GPIORELAY4,"relay4");
|
|
// gpio_direction_input(GPIOECHO);
|
|
gpio_direction_output(GPIORELAY1,1);
|
|
gpio_direction_output(GPIORELAY2,1);
|
|
gpio_direction_output(GPIORELAY3,1);
|
|
gpio_direction_output(GPIORELAY4,1);
|
|
return 0;
|
|
}
|
|
|
|
static int relay_close(struct inode *inode, struct file *file)
|
|
{
|
|
gpio_free(GPIORELAY1);
|
|
gpio_free(GPIORELAY2);
|
|
gpio_free(GPIORELAY3);
|
|
gpio_free(GPIORELAY4);
|
|
printk(KERN_ALERT"########relay close succesful###########\n");
|
|
return 0;
|
|
}
|
|
|
|
static ssize_t relay_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
|
|
{
|
|
/*
|
|
unsigned int count=size;
|
|
if(copy_to_user(buf,(void*)(relay),count));
|
|
{
|
|
return -EFAULT;
|
|
}
|
|
*/
|
|
return 0;
|
|
}
|
|
|
|
static ssize_t relay_write(struct file *file, const char __user *buf, size_t count, loff_t *ptr)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static long relay_ioctl( struct file *file, unsigned int cmd, unsigned long arg)
|
|
{
|
|
// printk(KERN_ALERT"in ioctl func!");
|
|
switch(cmd){
|
|
case 0:
|
|
// printk(KERN_ALERT"in ioctl 0!\n");
|
|
// gpio_direction_output(GPIORELAY2,arg);
|
|
break;
|
|
case 1:
|
|
printk(KERN_ALERT"in ioctl 1!\n");
|
|
gpio_direction_output(GPIORELAY1,arg);
|
|
/*
|
|
void __user *p=(void __user *)arg;
|
|
relay();
|
|
copy_to_user(p,send,sizeof(send));
|
|
*/
|
|
break;
|
|
case 2:
|
|
printk(KERN_ALERT"in ioctl 2!\n");
|
|
gpio_direction_output(GPIORELAY2,arg);
|
|
break;
|
|
case 3:
|
|
printk(KERN_ALERT"in ioctl 3!\n");
|
|
gpio_direction_output(GPIORELAY3,arg);
|
|
break;
|
|
case 4:
|
|
printk(KERN_ALERT"in ioctl 4!\n");
|
|
gpio_direction_output(GPIORELAY4,arg);
|
|
break;
|
|
default:
|
|
printk(KERN_ALERT"in ioctl default!\n");
|
|
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static const struct file_operations relay_ops = {
|
|
.owner = THIS_MODULE,
|
|
.open = relay_open,
|
|
.release = relay_close,
|
|
.read = relay_read,
|
|
.write = relay_write,
|
|
.unlocked_ioctl = relay_ioctl
|
|
};
|
|
|
|
static struct miscdevice relay_miscdev = {
|
|
MISC_DYNAMIC_MINOR,
|
|
"relay",
|
|
&relay_ops,
|
|
};
|
|
|
|
static int __init relay_init(void)
|
|
{
|
|
if(misc_register(&relay_miscdev))
|
|
{
|
|
printk(KERN_ALERT"###############: Couldn't register device 0, %d.\n", 255);
|
|
return -EBUSY;
|
|
}
|
|
printk(KERN_ALERT"register succesful\n");
|
|
return 0;
|
|
}
|
|
|
|
static void __exit relay_exit(void)
|
|
{
|
|
misc_deregister(&relay_miscdev);
|
|
}
|
|
|
|
module_init(relay_init);
|
|
module_exit(relay_exit);
|
|
MODULE_LICENSE("GPL");
|