1
0
mirror of https://git.openwrt.org/openwrt/openwrt.git synced 2024-10-19 05:58:53 +02:00

Extend the gpio device driver (#5671)

SVN-Revision: 21787
This commit is contained in:
Claudio Mignanti 2010-06-13 13:27:06 +00:00
parent fb94196462
commit 35ff4c9aa8
2 changed files with 61 additions and 52 deletions

@ -17,6 +17,7 @@
* *
* Feedback, Bugs... blogic@openwrt.org * Feedback, Bugs... blogic@openwrt.org
* *
* dpg 20100106
*/ */
#include <linux/module.h> #include <linux/module.h>
@ -36,54 +37,57 @@
#define DEVNAME "gpio" #define DEVNAME "gpio"
static int dev_major; static int dev_major;
static unsigned int gpio_access_mask;
static struct class *gpiodev_class; static struct class *gpiodev_class;
/* Counter is 1, if the device is not opened and zero (or less) if opened. */
static atomic_t gpio_open_cnt = ATOMIC_INIT(1);
/* third argument of user space ioctl ('arg' here) contains the <pin> */
static int static int
gpio_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg) gpio_ioctl(struct inode * inode, struct file * file, unsigned int cmd,
unsigned long arg)
{ {
int retval = 0; int retval = 0;
if (((1 << arg) & gpio_access_mask) != (1 << arg))
{
retval = -EINVAL;
goto out;
}
switch (cmd) switch (cmd)
{ {
case GPIO_GET: case GPIO_GET:
retval = gpio_get_value(arg); retval = gpio_get_value(arg);
break; break;
case GPIO_SET: case GPIO_SET:
gpio_set_value(arg, 1); gpio_set_value(arg, 1);
break; break;
case GPIO_CLEAR: case GPIO_CLEAR:
gpio_set_value(arg, 0); gpio_set_value(arg, 0);
break; break;
case GPIO_DIR_IN: case GPIO_DIR_IN:
gpio_direction_input(arg); retval = gpio_direction_input(arg);
break; break;
case GPIO_DIR_OUT: case GPIO_DIR_OUT:
gpio_direction_output(arg, 0); retval = gpio_direction_output(arg, 0);
break;
case GPIO_DIR_HIGH:
retval = gpio_direction_output(arg, 1);
break;
case GPIO_REQUEST:
/* should be first ioctl operation on <pin> */
retval = gpio_request(arg, DRVNAME);
break;
case GPIO_FREE:
/* should be last ioctl operation on <pin> */
/* may be needed first if previous user missed this ioctl */
gpio_free(arg);
break;
case GPIO_CAN_SLEEP:
retval = gpio_cansleep(arg);
break; break;
default: default:
retval = -EINVAL; retval = -EINVAL;
/* = -ENOTTY; // correct return but ... */
break; break;
} }
out:
return retval; return retval;
} }
/* Allow co-incident opens */
static int static int
gpio_open(struct inode *inode, struct file *file) gpio_open(struct inode *inode, struct file *file)
{ {
@ -96,19 +100,6 @@ gpio_open(struct inode *inode, struct file *file)
result = -ENODEV; result = -ENODEV;
goto out; goto out;
} }
/* FIXME: We should really allow multiple applications to open the device
* at the same time, as long as the apps access different IO pins.
* The generic gpio-registration functions can be used for that.
* Two new IOCTLs have to be introduced for that. Need to check userspace
* compatibility first. --mb */
if (!atomic_dec_and_test(&gpio_open_cnt)) {
atomic_inc(&gpio_open_cnt);
printk(KERN_ERR DRVNAME ": Device with minor ID %d already in use\n", dev_minor);
result = -EBUSY;
goto out;
}
out: out:
return result; return result;
} }
@ -116,9 +107,9 @@ out:
static int static int
gpio_close(struct inode * inode, struct file * file) gpio_close(struct inode * inode, struct file * file)
{ {
smp_mb__before_atomic_inc(); /* could track all <pin>s requested by this fd and gpio_free()
atomic_inc(&gpio_open_cnt); * them here
*/
return 0; return 0;
} }
@ -140,22 +131,9 @@ gpio_probe(struct platform_device *dev)
result = -ENODEV; result = -ENODEV;
goto out; goto out;
} }
gpiodev_class = class_create(THIS_MODULE, DRVNAME); gpiodev_class = class_create(THIS_MODULE, DRVNAME);
device_create(gpiodev_class, NULL, MKDEV(dev_major, 0), dev, DEVNAME); device_create(gpiodev_class, NULL, MKDEV(dev_major, 0), dev, DEVNAME);
printk(KERN_INFO DRVNAME ": gpio device registered with major %d\n", dev_major); printk(KERN_INFO DRVNAME ": gpio device registered with major %d\n", dev_major);
if (dev->num_resources != 1)
{
printk(KERN_ERR DRVNAME ": device may only have 1 resource\n");
result = -ENODEV;
goto out;
}
gpio_access_mask = dev->resource[0].start;
printk(KERN_INFO DRVNAME ": gpio platform device registered with access mask %08X\n", gpio_access_mask);
out: out:
return result; return result;
} }
@ -182,7 +160,7 @@ gpio_mod_init(void)
{ {
int ret = platform_driver_register(&gpio_driver); int ret = platform_driver_register(&gpio_driver);
if (ret) if (ret)
printk(KERN_INFO DRVNAME ": Error registering platfom driver!"); printk(KERN_INFO DRVNAME ": Error registering platfom driver!\n");
return ret; return ret;
} }
@ -197,5 +175,5 @@ module_init (gpio_mod_init);
module_exit (gpio_mod_exit); module_exit (gpio_mod_exit);
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
MODULE_AUTHOR("John Crispin / OpenWrt"); MODULE_AUTHOR("John Crispin / OpenWrt +");
MODULE_DESCRIPTION("Character device for for generic gpio api"); MODULE_DESCRIPTION("Character device for for generic gpio api");

@ -1,11 +1,42 @@
#ifndef _GPIODEV_H__ #ifndef _GPIO_DEV_H__
#define _GPIODEV_H__ #define _GPIO_DEV_H__
/*********************************************************************
*
* This Linux kernel header is expanded from the original driver
* (gpio_dev) by John Crispin. It provides an ioctl based interface to
* GPIO pins via the /dev/gpio char device and gpiolib within the kernel.
* The third argument to each ioctl is the GPIO pin number.
*
* This driver has been tested with lk 2.6.31 and works. The original
* driver fails quietly with this version. The protocol is now a bit
* different: the ioctl(fd, GPIO_REQUEST, <pin>) should be called
* after the open("/dev/gpio", O_RDWR) to determine if the <pin> is
* already in use. If the ioctl is successful (i.e. returns 0 for not
* in use) then the <pin> is claimed by this driver and
* ioctl(fd, GPIO_FREE, <pin>) should be called prior to close(fd) .
*
* See <kernel_source>/Documentation/gpio.txt
* Note that kernel designers prefer the use of the sysfs gpio interface.
* This char driver is easier to use from code and faster.
********************************************************************/
/* This header can be included in both the user and kernel spaces */
/* The _IO macro is defined in sys/ioctl.h */
#define IOC_GPIODEV_MAGIC 'B' #define IOC_GPIODEV_MAGIC 'B'
#define GPIO_GET _IO(IOC_GPIODEV_MAGIC, 10) #define GPIO_GET _IO(IOC_GPIODEV_MAGIC, 10)
#define GPIO_SET _IO(IOC_GPIODEV_MAGIC, 11) #define GPIO_SET _IO(IOC_GPIODEV_MAGIC, 11)
#define GPIO_CLEAR _IO(IOC_GPIODEV_MAGIC, 12) #define GPIO_CLEAR _IO(IOC_GPIODEV_MAGIC, 12)
#define GPIO_DIR_IN _IO(IOC_GPIODEV_MAGIC, 13) #define GPIO_DIR_IN _IO(IOC_GPIODEV_MAGIC, 13)
#define GPIO_DIR_OUT _IO(IOC_GPIODEV_MAGIC, 14) #define GPIO_DIR_OUT _IO(IOC_GPIODEV_MAGIC, 14)
/* Sets the direction out and clears the <pin> (low) */
#define GPIO_DIR_HIGH _IO(IOC_GPIODEV_MAGIC, 15)
/* Sets the direction out and sets the <pin> (high) */
#define GPIO_REQUEST _IO(IOC_GPIODEV_MAGIC, 16)
#define GPIO_FREE _IO(IOC_GPIODEV_MAGIC, 17)
#define GPIO_CAN_SLEEP _IO(IOC_GPIODEV_MAGIC, 18)
#endif #endif