Root/target/linux/generic/files/drivers/char/gpio_dev.c

1/*
2 * character device wrapper for generic gpio layer
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
17 *
18 * Feedback, Bugs... blogic@openwrt.org
19 *
20 * dpg 20100106
21 */
22
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <asm/uaccess.h>
27#include <asm/io.h>
28#include <asm/gpio.h>
29#include <asm/atomic.h>
30#include <linux/init.h>
31#include <linux/genhd.h>
32#include <linux/device.h>
33#include <linux/platform_device.h>
34#include <linux/gpio_dev.h>
35#include <linux/fs.h>
36
37#define DRVNAME "gpiodev"
38#define DEVNAME "gpio"
39
40static int dev_major;
41static struct class *gpiodev_class;
42
43
44/* third argument of user space ioctl ('arg' here) contains the <pin> */
45static int
46gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
47{
48    int retval = 0;
49
50    switch (cmd)
51    {
52    case GPIO_GET:
53        retval = gpio_get_value(arg);
54        break;
55    case GPIO_SET:
56        gpio_set_value(arg, 1);
57        break;
58    case GPIO_CLEAR:
59        gpio_set_value(arg, 0);
60        break;
61    case GPIO_DIR_IN:
62        retval = gpio_direction_input(arg);
63        break;
64    case GPIO_DIR_OUT:
65        retval = gpio_direction_output(arg, 0);
66        break;
67    case GPIO_DIR_HIGH:
68        retval = gpio_direction_output(arg, 1);
69        break;
70    case GPIO_REQUEST:
71        /* should be first ioctl operation on <pin> */
72        retval = gpio_request(arg, DRVNAME);
73        break;
74    case GPIO_FREE:
75        /* should be last ioctl operation on <pin> */
76        /* may be needed first if previous user missed this ioctl */
77        gpio_free(arg);
78        break;
79    case GPIO_CAN_SLEEP:
80        retval = gpio_cansleep(arg);
81        break;
82    default:
83        retval = -EINVAL;
84        /* = -ENOTTY; // correct return but ... */
85        break;
86    }
87    return retval;
88}
89
90/* Allow co-incident opens */
91static int
92gpio_open(struct inode *inode, struct file *file)
93{
94    int result = 0;
95    unsigned int dev_minor = MINOR(inode->i_rdev);
96
97    if (dev_minor != 0)
98    {
99        printk(KERN_ERR DRVNAME ": trying to access unknown minor device -> %d\n", dev_minor);
100        result = -ENODEV;
101        goto out;
102    }
103out:
104    return result;
105}
106
107static int
108gpio_close(struct inode * inode, struct file * file)
109{
110    /* could track all <pin>s requested by this fd and gpio_free()
111         * them here
112     */
113    return 0;
114}
115
116struct file_operations gpio_fops = {
117    unlocked_ioctl: gpio_ioctl,
118    open: gpio_open,
119    release: gpio_close
120};
121
122static int
123gpio_probe(struct platform_device *dev)
124{
125    int result = 0;
126
127    dev_major = register_chrdev(0, DEVNAME, &gpio_fops);
128    if (!dev_major)
129    {
130        printk(KERN_ERR DRVNAME ": Error whilst opening %s \n", DEVNAME);
131        result = -ENODEV;
132        goto out;
133    }
134    gpiodev_class = class_create(THIS_MODULE, DRVNAME);
135    device_create(gpiodev_class, NULL, MKDEV(dev_major, 0), dev, DEVNAME);
136    printk(KERN_INFO DRVNAME ": gpio device registered with major %d\n", dev_major);
137out:
138    return result;
139}
140
141static int
142gpio_remove(struct platform_device *dev)
143{
144    device_destroy(gpiodev_class, MKDEV(dev_major, 0));
145    class_destroy(gpiodev_class);
146    unregister_chrdev(dev_major, DEVNAME);
147    return 0;
148}
149
150static struct
151platform_driver gpio_driver = {
152    .probe = gpio_probe,
153    .remove = gpio_remove,
154    .driver = {
155        .name = "GPIODEV",
156        .owner = THIS_MODULE,
157    },
158};
159
160static int __init
161gpio_mod_init(void)
162{
163    int ret = platform_driver_register(&gpio_driver);
164    if (ret)
165        printk(KERN_INFO DRVNAME ": Error registering platfom driver!\n");
166
167    return ret;
168}
169
170static void __exit
171gpio_mod_exit(void)
172{
173    platform_driver_unregister(&gpio_driver);
174}
175
176module_init (gpio_mod_init);
177module_exit (gpio_mod_exit);
178
179MODULE_LICENSE("GPL");
180MODULE_AUTHOR("John Crispin / OpenWrt +");
181MODULE_DESCRIPTION("Character device for for generic gpio api");
182

Archive Download this file



interactive