add extra sanity checks in madwifi
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / files / drivers / gpio / 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 */
21
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <asm/uaccess.h>
26 #include <asm/io.h>
27 #include <asm/gpio.h>
28 #include <linux/init.h>
29 #include <linux/genhd.h>
30 #include <linux/device.h>
31 #include <linux/platform_device.h>
32 #include <linux/gpio_dev.h>
33
34 #define DRVNAME "gpiodev"
35 #define DEVNAME "gpio"
36
37 static int dev_major;
38 static int gpio_is_open = 0;
39 unsigned int gpio_access_mask = 0;
40 static struct class *gpio_class;
41
42 static int
43 gpio_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
44 {
45 int retval = 0;
46
47 if ((arg & gpio_access_mask) != arg)
48 {
49 retval = -EINVAL;
50 goto out;
51 }
52
53 switch (cmd)
54 {
55 case GPIO_GET:
56 retval = gpio_get_value(arg);
57 break;
58
59 case GPIO_SET:
60 gpio_set_value(arg, 0);
61 break;
62
63 case GPIO_CLEAR:
64 gpio_set_value(arg, 0);
65 break;
66
67 case GPIO_DIR_IN:
68 gpio_direction_input(arg);
69 break;
70
71 case GPIO_DIR_OUT:
72 gpio_direction_output(arg, 0);
73 break;
74
75 default:
76 retval = -EINVAL;
77 break;
78 }
79
80 out:
81 return retval;
82 }
83
84 static int
85 gpio_open(struct inode *inode, struct file *file)
86 {
87 int result = 0;
88 unsigned int dev_minor = MINOR(inode->i_rdev);
89
90 if (dev_minor != 0)
91 {
92 printk(KERN_ERR DRVNAME ": trying to access unknown minor device -> %d\n", dev_minor);
93 result = -ENODEV;
94 goto out;
95 }
96
97 if (gpio_is_open)
98 {
99 printk(KERN_ERR DRVNAME ": Device with minor ID %d already in use\n", dev_minor);
100 result = -EBUSY;
101 goto out;
102 }
103
104 gpio_is_open = 1;
105
106 out:
107 return result;
108 }
109
110 static int
111 gpio_close(struct inode * inode, struct file * file)
112 {
113 gpio_is_open = 0;
114
115 return 0;
116 }
117
118 struct file_operations gpio_fops = {
119 ioctl: gpio_ioctl,
120 open: gpio_open,
121 release: gpio_close
122 };
123
124 static int
125 gpio_probe(struct platform_device *dev)
126 {
127 int result = 0;
128
129 dev_major = register_chrdev(0, DEVNAME, &gpio_fops);
130 if (!dev_major)
131 {
132 printk(KERN_ERR DRVNAME ": Error whilst opening %s \n", DEVNAME);
133 result = -ENODEV;
134 goto out;
135 }
136
137 gpio_class = class_create(THIS_MODULE, DEVNAME);
138 class_device_create(gpio_class, NULL, MKDEV(dev_major, 0), NULL, DEVNAME);
139
140 printk(KERN_INFO DRVNAME ": gpio device registered with major %d\n", dev_major);
141
142 if (dev->num_resources != 1)
143 {
144 printk(KERN_ERR DRVNAME ": device may only have 1 resource\n");
145 result = -ENODEV;
146 goto out;
147 }
148
149 gpio_access_mask = dev->resource[0].start;
150
151 printk(KERN_INFO DRVNAME ": gpio platform device registered with access mask %08X\n", gpio_access_mask);
152 out:
153 return result;
154 }
155
156 static int
157 gpio_remove(struct platform_device *dev)
158 {
159 unregister_chrdev(dev_major, DEVNAME);
160 return 0;
161 }
162
163 static struct
164 platform_driver gpio_driver = {
165 .probe = gpio_probe,
166 .remove = gpio_remove,
167 .driver = {
168 .name = "GPIODEV",
169 .owner = THIS_MODULE,
170 },
171 };
172
173 static int __init
174 gpio_mod_init(void)
175 {
176 int ret = platform_driver_register(&gpio_driver);
177 if (ret)
178 printk(KERN_INFO DRVNAME ": Error registering platfom driver!");
179
180 return ret;
181 }
182
183 static void __exit
184 gpio_mod_exit(void)
185 {
186 platform_driver_unregister(&gpio_driver);
187 }
188
189 module_init (gpio_mod_init);
190 module_exit (gpio_mod_exit);
191
192 MODULE_LICENSE("GPL");
193 MODULE_AUTHOR("John Crispin / OpenWrt");
194 MODULE_DESCRIPTION("Character device for for generic gpio api");