blob: f63967c8e95abb1696ff9c10e7b87f299218a3e7 [file] [log] [blame]
Stephen Hemmingerfbb58582018-07-20 15:11:26 -07001// SPDX-License-Identifier: GPL-2.0
Hans J. Kochbeafc542006-12-07 10:58:29 +01002/*
3 * drivers/uio/uio.c
4 *
5 * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
6 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
Hans J. Koch318af552010-10-30 00:36:47 +02007 * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de>
Hans J. Kochbeafc542006-12-07 10:58:29 +01008 * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
9 *
10 * Userspace IO
11 *
12 * Base Functions
Hans J. Kochbeafc542006-12-07 10:58:29 +010013 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/poll.h>
18#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Hans J. Kochbeafc542006-12-07 10:58:29 +010020#include <linux/mm.h>
21#include <linux/idr.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010022#include <linux/sched/signal.h>
Hans J. Kochbeafc542006-12-07 10:58:29 +010023#include <linux/string.h>
24#include <linux/kobject.h>
Eric W. Biederman91960a42010-09-14 11:38:06 -070025#include <linux/cdev.h>
Hans J. Kochbeafc542006-12-07 10:58:29 +010026#include <linux/uio_driver.h>
27
Eric W. Biederman91960a42010-09-14 11:38:06 -070028#define UIO_MAX_DEVICES (1U << MINORBITS)
Hans J. Kochbeafc542006-12-07 10:58:29 +010029
Hans J. Kochbeafc542006-12-07 10:58:29 +010030static int uio_major;
Eric W. Biederman91960a42010-09-14 11:38:06 -070031static struct cdev *uio_cdev;
Hans J. Kochbeafc542006-12-07 10:58:29 +010032static DEFINE_IDR(uio_idr);
Jan Engelhardt4f014692008-01-22 20:50:54 +010033static const struct file_operations uio_fops;
Hans J. Kochbeafc542006-12-07 10:58:29 +010034
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -060035/* Protect idr accesses */
36static DEFINE_MUTEX(minor_lock);
37
Hans J. Kochbeafc542006-12-07 10:58:29 +010038/*
39 * attributes
40 */
41
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000042struct uio_map {
43 struct kobject kobj;
44 struct uio_mem *mem;
Hans J. Kochbeafc542006-12-07 10:58:29 +010045};
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000046#define to_map(map) container_of(map, struct uio_map, kobj)
Hans J. Kochbeafc542006-12-07 10:58:29 +010047
Hans J. Koch82057792009-01-07 00:15:39 +010048static ssize_t map_name_show(struct uio_mem *mem, char *buf)
49{
50 if (unlikely(!mem->name))
51 mem->name = "";
52
53 return sprintf(buf, "%s\n", mem->name);
54}
55
Brandon Philips4f808bc2008-02-19 01:55:05 -080056static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
Hans J. Kochbeafc542006-12-07 10:58:29 +010057{
Cristian Stoicae0f11472014-10-09 15:00:27 +030058 return sprintf(buf, "%pa\n", &mem->addr);
Hans J. Kochbeafc542006-12-07 10:58:29 +010059}
60
Brandon Philips4f808bc2008-02-19 01:55:05 -080061static ssize_t map_size_show(struct uio_mem *mem, char *buf)
62{
Cristian Stoicae0f11472014-10-09 15:00:27 +030063 return sprintf(buf, "%pa\n", &mem->size);
Brandon Philips4f808bc2008-02-19 01:55:05 -080064}
65
Hans J. Koche2b39df2008-09-18 23:53:18 +020066static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
67{
Michal Sojka171058fb2017-03-16 14:50:08 +010068 return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs);
Hans J. Koche2b39df2008-09-18 23:53:18 +020069}
70
Hans J. Koche70c4122008-12-06 02:23:13 +010071struct map_sysfs_entry {
Brandon Philips4f808bc2008-02-19 01:55:05 -080072 struct attribute attr;
73 ssize_t (*show)(struct uio_mem *, char *);
74 ssize_t (*store)(struct uio_mem *, const char *, size_t);
75};
76
Hans J. Koch82057792009-01-07 00:15:39 +010077static struct map_sysfs_entry name_attribute =
78 __ATTR(name, S_IRUGO, map_name_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +010079static struct map_sysfs_entry addr_attribute =
Brandon Philips4f808bc2008-02-19 01:55:05 -080080 __ATTR(addr, S_IRUGO, map_addr_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +010081static struct map_sysfs_entry size_attribute =
Brandon Philips4f808bc2008-02-19 01:55:05 -080082 __ATTR(size, S_IRUGO, map_size_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +010083static struct map_sysfs_entry offset_attribute =
Hans J. Koche2b39df2008-09-18 23:53:18 +020084 __ATTR(offset, S_IRUGO, map_offset_show, NULL);
Hans J. Kochbeafc542006-12-07 10:58:29 +010085
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000086static struct attribute *attrs[] = {
Hans J. Koch82057792009-01-07 00:15:39 +010087 &name_attribute.attr,
Brandon Philips4f808bc2008-02-19 01:55:05 -080088 &addr_attribute.attr,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000089 &size_attribute.attr,
Hans J. Koche2b39df2008-09-18 23:53:18 +020090 &offset_attribute.attr,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000091 NULL, /* need to NULL terminate the list of attributes */
Hans J. Kochbeafc542006-12-07 10:58:29 +010092};
93
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +000094static void map_release(struct kobject *kobj)
95{
96 struct uio_map *map = to_map(kobj);
97 kfree(map);
98}
99
Brandon Philips4f808bc2008-02-19 01:55:05 -0800100static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
101 char *buf)
102{
103 struct uio_map *map = to_map(kobj);
104 struct uio_mem *mem = map->mem;
Hans J. Koche70c4122008-12-06 02:23:13 +0100105 struct map_sysfs_entry *entry;
Brandon Philips4f808bc2008-02-19 01:55:05 -0800106
Hans J. Koche70c4122008-12-06 02:23:13 +0100107 entry = container_of(attr, struct map_sysfs_entry, attr);
Brandon Philips4f808bc2008-02-19 01:55:05 -0800108
109 if (!entry->show)
110 return -EIO;
111
112 return entry->show(mem, buf);
113}
114
Emese Revfy52cf25d2010-01-19 02:58:23 +0100115static const struct sysfs_ops map_sysfs_ops = {
Brandon Philips4f808bc2008-02-19 01:55:05 -0800116 .show = map_type_show,
117};
118
Hans J. Kochbeafc542006-12-07 10:58:29 +0100119static struct kobj_type map_attr_type = {
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000120 .release = map_release,
Hans J. Koche70c4122008-12-06 02:23:13 +0100121 .sysfs_ops = &map_sysfs_ops,
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000122 .default_attrs = attrs,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100123};
124
Hans J. Koche70c4122008-12-06 02:23:13 +0100125struct uio_portio {
126 struct kobject kobj;
127 struct uio_port *port;
128};
129#define to_portio(portio) container_of(portio, struct uio_portio, kobj)
130
Hans J. Koch82057792009-01-07 00:15:39 +0100131static ssize_t portio_name_show(struct uio_port *port, char *buf)
132{
133 if (unlikely(!port->name))
134 port->name = "";
135
136 return sprintf(buf, "%s\n", port->name);
137}
138
Hans J. Koche70c4122008-12-06 02:23:13 +0100139static ssize_t portio_start_show(struct uio_port *port, char *buf)
140{
141 return sprintf(buf, "0x%lx\n", port->start);
142}
143
144static ssize_t portio_size_show(struct uio_port *port, char *buf)
145{
146 return sprintf(buf, "0x%lx\n", port->size);
147}
148
149static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
150{
151 const char *porttypes[] = {"none", "x86", "gpio", "other"};
152
153 if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
154 return -EINVAL;
155
156 return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
157}
158
159struct portio_sysfs_entry {
160 struct attribute attr;
161 ssize_t (*show)(struct uio_port *, char *);
162 ssize_t (*store)(struct uio_port *, const char *, size_t);
163};
164
Hans J. Koch82057792009-01-07 00:15:39 +0100165static struct portio_sysfs_entry portio_name_attribute =
166 __ATTR(name, S_IRUGO, portio_name_show, NULL);
Hans J. Koche70c4122008-12-06 02:23:13 +0100167static struct portio_sysfs_entry portio_start_attribute =
168 __ATTR(start, S_IRUGO, portio_start_show, NULL);
169static struct portio_sysfs_entry portio_size_attribute =
170 __ATTR(size, S_IRUGO, portio_size_show, NULL);
171static struct portio_sysfs_entry portio_porttype_attribute =
172 __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
173
174static struct attribute *portio_attrs[] = {
Hans J. Koch82057792009-01-07 00:15:39 +0100175 &portio_name_attribute.attr,
Hans J. Koche70c4122008-12-06 02:23:13 +0100176 &portio_start_attribute.attr,
177 &portio_size_attribute.attr,
178 &portio_porttype_attribute.attr,
179 NULL,
180};
181
182static void portio_release(struct kobject *kobj)
183{
184 struct uio_portio *portio = to_portio(kobj);
185 kfree(portio);
186}
187
188static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
189 char *buf)
190{
191 struct uio_portio *portio = to_portio(kobj);
192 struct uio_port *port = portio->port;
193 struct portio_sysfs_entry *entry;
194
195 entry = container_of(attr, struct portio_sysfs_entry, attr);
196
197 if (!entry->show)
198 return -EIO;
199
200 return entry->show(port, buf);
201}
202
Emese Revfy52cf25d2010-01-19 02:58:23 +0100203static const struct sysfs_ops portio_sysfs_ops = {
Hans J. Koche70c4122008-12-06 02:23:13 +0100204 .show = portio_type_show,
205};
206
207static struct kobj_type portio_attr_type = {
208 .release = portio_release,
209 .sysfs_ops = &portio_sysfs_ops,
210 .default_attrs = portio_attrs,
211};
212
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700213static ssize_t name_show(struct device *dev,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100214 struct device_attribute *attr, char *buf)
215{
216 struct uio_device *idev = dev_get_drvdata(dev);
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400217 int ret;
218
219 mutex_lock(&idev->info_lock);
220 if (!idev->info) {
221 ret = -EINVAL;
222 dev_err(dev, "the device has been unregistered\n");
223 goto out;
224 }
225
226 ret = sprintf(buf, "%s\n", idev->info->name);
227
228out:
229 mutex_unlock(&idev->info_lock);
230 return ret;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100231}
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700232static DEVICE_ATTR_RO(name);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100233
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700234static ssize_t version_show(struct device *dev,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100235 struct device_attribute *attr, char *buf)
236{
237 struct uio_device *idev = dev_get_drvdata(dev);
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400238 int ret;
239
240 mutex_lock(&idev->info_lock);
241 if (!idev->info) {
242 ret = -EINVAL;
243 dev_err(dev, "the device has been unregistered\n");
244 goto out;
245 }
246
247 ret = sprintf(buf, "%s\n", idev->info->version);
248
249out:
250 mutex_unlock(&idev->info_lock);
251 return ret;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100252}
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700253static DEVICE_ATTR_RO(version);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100254
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700255static ssize_t event_show(struct device *dev,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100256 struct device_attribute *attr, char *buf)
257{
258 struct uio_device *idev = dev_get_drvdata(dev);
Eric W. Biederman70a91562010-09-14 11:36:54 -0700259 return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
Hans J. Kochbeafc542006-12-07 10:58:29 +0100260}
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700261static DEVICE_ATTR_RO(event);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100262
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700263static struct attribute *uio_attrs[] = {
264 &dev_attr_name.attr,
265 &dev_attr_version.attr,
266 &dev_attr_event.attr,
267 NULL,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100268};
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700269ATTRIBUTE_GROUPS(uio);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100270
Eric W. Biedermanc66fdab2010-09-14 11:38:36 -0700271/* UIO class infrastructure */
272static struct class uio_class = {
273 .name = "uio",
Greg Kroah-Hartmanc9dce922013-07-24 15:05:23 -0700274 .dev_groups = uio_groups,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100275};
276
277/*
278 * device functions
279 */
280static int uio_dev_add_attributes(struct uio_device *idev)
281{
282 int ret;
Hans J. Koche70c4122008-12-06 02:23:13 +0100283 int mi, pi;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100284 int map_found = 0;
Hans J. Koche70c4122008-12-06 02:23:13 +0100285 int portio_found = 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100286 struct uio_mem *mem;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000287 struct uio_map *map;
Hans J. Koche70c4122008-12-06 02:23:13 +0100288 struct uio_port *port;
289 struct uio_portio *portio;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100290
Hans J. Kochbeafc542006-12-07 10:58:29 +0100291 for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
292 mem = &idev->info->mem[mi];
293 if (mem->size == 0)
294 break;
295 if (!map_found) {
296 map_found = 1;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000297 idev->map_dir = kobject_create_and_add("maps",
Hamish Martina93e7b32018-05-14 13:32:23 +1200298 &idev->dev.kobj);
Dan Carpenter0320a272016-04-01 14:04:23 +0300299 if (!idev->map_dir) {
300 ret = -ENOMEM;
Hans J. Koche70c4122008-12-06 02:23:13 +0100301 goto err_map;
Dan Carpenter0320a272016-04-01 14:04:23 +0300302 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100303 }
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000304 map = kzalloc(sizeof(*map), GFP_KERNEL);
Dan Carpenter0320a272016-04-01 14:04:23 +0300305 if (!map) {
306 ret = -ENOMEM;
Suman Anna0d835392017-05-09 18:58:24 -0500307 goto err_map;
Dan Carpenter0320a272016-04-01 14:04:23 +0300308 }
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700309 kobject_init(&map->kobj, &map_attr_type);
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000310 map->mem = mem;
311 mem->map = map;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700312 ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100313 if (ret)
Cong Ding75f0aef2013-10-25 00:25:53 -0700314 goto err_map_kobj;
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000315 ret = kobject_uevent(&map->kobj, KOBJ_ADD);
316 if (ret)
Suman Anna0d835392017-05-09 18:58:24 -0500317 goto err_map_kobj;
Hans J. Koche70c4122008-12-06 02:23:13 +0100318 }
319
320 for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
321 port = &idev->info->port[pi];
322 if (port->size == 0)
323 break;
324 if (!portio_found) {
325 portio_found = 1;
326 idev->portio_dir = kobject_create_and_add("portio",
Hamish Martina93e7b32018-05-14 13:32:23 +1200327 &idev->dev.kobj);
Dan Carpenter0320a272016-04-01 14:04:23 +0300328 if (!idev->portio_dir) {
329 ret = -ENOMEM;
Hans J. Koche70c4122008-12-06 02:23:13 +0100330 goto err_portio;
Dan Carpenter0320a272016-04-01 14:04:23 +0300331 }
Hans J. Koche70c4122008-12-06 02:23:13 +0100332 }
333 portio = kzalloc(sizeof(*portio), GFP_KERNEL);
Dan Carpenter0320a272016-04-01 14:04:23 +0300334 if (!portio) {
335 ret = -ENOMEM;
Suman Anna0d835392017-05-09 18:58:24 -0500336 goto err_portio;
Dan Carpenter0320a272016-04-01 14:04:23 +0300337 }
Hans J. Koche70c4122008-12-06 02:23:13 +0100338 kobject_init(&portio->kobj, &portio_attr_type);
339 portio->port = port;
340 port->portio = portio;
341 ret = kobject_add(&portio->kobj, idev->portio_dir,
342 "port%d", pi);
343 if (ret)
Cong Ding75f0aef2013-10-25 00:25:53 -0700344 goto err_portio_kobj;
Hans J. Koche70c4122008-12-06 02:23:13 +0100345 ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
346 if (ret)
Suman Anna0d835392017-05-09 18:58:24 -0500347 goto err_portio_kobj;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100348 }
349
350 return 0;
351
Hans J. Koche70c4122008-12-06 02:23:13 +0100352err_portio:
Cong Ding75f0aef2013-10-25 00:25:53 -0700353 pi--;
354err_portio_kobj:
355 for (; pi >= 0; pi--) {
Hans J. Koche70c4122008-12-06 02:23:13 +0100356 port = &idev->info->port[pi];
357 portio = port->portio;
358 kobject_put(&portio->kobj);
359 }
360 kobject_put(idev->portio_dir);
361err_map:
Cong Ding75f0aef2013-10-25 00:25:53 -0700362 mi--;
363err_map_kobj:
364 for (; mi >= 0; mi--) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100365 mem = &idev->info->mem[mi];
Greg Kroah-Hartman81e7c6a2007-12-04 22:41:54 +0000366 map = mem->map;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800367 kobject_put(&map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100368 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800369 kobject_put(idev->map_dir);
Hamish Martina93e7b32018-05-14 13:32:23 +1200370 dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100371 return ret;
372}
373
374static void uio_dev_del_attributes(struct uio_device *idev)
375{
Hans J. Koche70c4122008-12-06 02:23:13 +0100376 int i;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100377 struct uio_mem *mem;
Hans J. Koche70c4122008-12-06 02:23:13 +0100378 struct uio_port *port;
379
380 for (i = 0; i < MAX_UIO_MAPS; i++) {
381 mem = &idev->info->mem[i];
Hans J. Kochbeafc542006-12-07 10:58:29 +0100382 if (mem->size == 0)
383 break;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800384 kobject_put(&mem->map->kobj);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100385 }
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800386 kobject_put(idev->map_dir);
Hans J. Koche70c4122008-12-06 02:23:13 +0100387
388 for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
389 port = &idev->info->port[i];
390 if (port->size == 0)
391 break;
392 kobject_put(&port->portio->kobj);
393 }
394 kobject_put(idev->portio_dir);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100395}
396
397static int uio_get_minor(struct uio_device *idev)
398{
Hans J. Kochbeafc542006-12-07 10:58:29 +0100399 int retval = -ENOMEM;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100400
401 mutex_lock(&minor_lock);
Tejun Heo6d770932013-02-27 17:04:47 -0800402 retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL);
403 if (retval >= 0) {
404 idev->minor = retval;
Damian Hobson-Garcia5ed05052013-03-26 10:31:22 +0900405 retval = 0;
Tejun Heo6d770932013-02-27 17:04:47 -0800406 } else if (retval == -ENOSPC) {
Hamish Martina93e7b32018-05-14 13:32:23 +1200407 dev_err(&idev->dev, "too many uio devices\n");
Hillf Dantonc6edc422011-03-31 20:38:47 +0800408 retval = -EINVAL;
Hillf Dantonc6edc422011-03-31 20:38:47 +0800409 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100410 mutex_unlock(&minor_lock);
411 return retval;
412}
413
414static void uio_free_minor(struct uio_device *idev)
415{
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600416 mutex_lock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100417 idr_remove(&uio_idr, idev->minor);
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600418 mutex_unlock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100419}
420
421/**
422 * uio_event_notify - trigger an interrupt event
423 * @info: UIO device capabilities
424 */
425void uio_event_notify(struct uio_info *info)
426{
427 struct uio_device *idev = info->uio_dev;
428
429 atomic_inc(&idev->event);
430 wake_up_interruptible(&idev->wait);
431 kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
432}
433EXPORT_SYMBOL_GPL(uio_event_notify);
434
435/**
436 * uio_interrupt - hardware interrupt handler
437 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
438 * @dev_id: Pointer to the devices uio_device structure
439 */
440static irqreturn_t uio_interrupt(int irq, void *dev_id)
441{
442 struct uio_device *idev = (struct uio_device *)dev_id;
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400443 irqreturn_t ret;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100444
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400445 mutex_lock(&idev->info_lock);
446
447 ret = idev->info->handler(irq, idev->info);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100448 if (ret == IRQ_HANDLED)
449 uio_event_notify(idev->info);
450
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400451 mutex_unlock(&idev->info_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100452 return ret;
453}
454
455struct uio_listener {
456 struct uio_device *dev;
457 s32 event_count;
458};
459
460static int uio_open(struct inode *inode, struct file *filep)
461{
462 struct uio_device *idev;
463 struct uio_listener *listener;
464 int ret = 0;
465
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600466 mutex_lock(&minor_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100467 idev = idr_find(&uio_idr, iminor(inode));
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600468 mutex_unlock(&minor_lock);
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600469 if (!idev) {
470 ret = -ENODEV;
471 goto out;
472 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100473
Hamish Martina93e7b32018-05-14 13:32:23 +1200474 get_device(&idev->dev);
475
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600476 if (!try_module_get(idev->owner)) {
477 ret = -ENODEV;
Hamish Martina93e7b32018-05-14 13:32:23 +1200478 goto err_module_get;
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600479 }
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200480
Hans J. Kochbeafc542006-12-07 10:58:29 +0100481 listener = kmalloc(sizeof(*listener), GFP_KERNEL);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200482 if (!listener) {
483 ret = -ENOMEM;
484 goto err_alloc_listener;
485 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100486
487 listener->dev = idev;
488 listener->event_count = atomic_read(&idev->event);
489 filep->private_data = listener;
490
Xiubo Li543af582018-07-06 22:05:38 -0400491 mutex_lock(&idev->info_lock);
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400492 if (!idev->info) {
493 mutex_unlock(&idev->info_lock);
494 ret = -EINVAL;
495 goto err_alloc_listener;
496 }
497
Hamish Martina93e7b32018-05-14 13:32:23 +1200498 if (idev->info && idev->info->open)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100499 ret = idev->info->open(idev->info, inode);
Xiubo Li543af582018-07-06 22:05:38 -0400500 mutex_unlock(&idev->info_lock);
Hamish Martina93e7b32018-05-14 13:32:23 +1200501 if (ret)
502 goto err_infoopen;
503
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200504 return 0;
505
506err_infoopen:
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200507 kfree(listener);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200508
Jonathan Corbet0d4a7bc2008-08-26 17:15:45 -0600509err_alloc_listener:
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200510 module_put(idev->owner);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100511
Hamish Martina93e7b32018-05-14 13:32:23 +1200512err_module_get:
513 put_device(&idev->dev);
514
Jonathan Corbetfbc8a812008-05-15 10:39:37 -0600515out:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100516 return ret;
517}
518
519static int uio_fasync(int fd, struct file *filep, int on)
520{
521 struct uio_listener *listener = filep->private_data;
522 struct uio_device *idev = listener->dev;
523
524 return fasync_helper(fd, filep, on, &idev->async_queue);
525}
526
527static int uio_release(struct inode *inode, struct file *filep)
528{
529 int ret = 0;
530 struct uio_listener *listener = filep->private_data;
531 struct uio_device *idev = listener->dev;
532
Xiubo Li543af582018-07-06 22:05:38 -0400533 mutex_lock(&idev->info_lock);
Hamish Martina93e7b32018-05-14 13:32:23 +1200534 if (idev->info && idev->info->release)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100535 ret = idev->info->release(idev->info, inode);
Xiubo Li543af582018-07-06 22:05:38 -0400536 mutex_unlock(&idev->info_lock);
Uwe Kleine-König610ad502008-04-11 11:07:39 +0200537
538 module_put(idev->owner);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100539 kfree(listener);
Hamish Martina93e7b32018-05-14 13:32:23 +1200540 put_device(&idev->dev);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100541 return ret;
542}
543
Al Viroafc9a422017-07-03 06:39:46 -0400544static __poll_t uio_poll(struct file *filep, poll_table *wait)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100545{
546 struct uio_listener *listener = filep->private_data;
547 struct uio_device *idev = listener->dev;
Hamish Martina93e7b32018-05-14 13:32:23 +1200548 __poll_t ret = 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100549
Xiubo Li543af582018-07-06 22:05:38 -0400550 mutex_lock(&idev->info_lock);
Hamish Martina93e7b32018-05-14 13:32:23 +1200551 if (!idev->info || !idev->info->irq)
552 ret = -EIO;
Xiubo Li543af582018-07-06 22:05:38 -0400553 mutex_unlock(&idev->info_lock);
Hamish Martina93e7b32018-05-14 13:32:23 +1200554
555 if (ret)
556 return ret;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100557
558 poll_wait(filep, &idev->wait, wait);
559 if (listener->event_count != atomic_read(&idev->event))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800560 return EPOLLIN | EPOLLRDNORM;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100561 return 0;
562}
563
564static ssize_t uio_read(struct file *filep, char __user *buf,
565 size_t count, loff_t *ppos)
566{
567 struct uio_listener *listener = filep->private_data;
568 struct uio_device *idev = listener->dev;
569 DECLARE_WAITQUEUE(wait, current);
Hamish Martina93e7b32018-05-14 13:32:23 +1200570 ssize_t retval = 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100571 s32 event_count;
572
Xiubo Li543af582018-07-06 22:05:38 -0400573 mutex_lock(&idev->info_lock);
Hamish Martina93e7b32018-05-14 13:32:23 +1200574 if (!idev->info || !idev->info->irq)
575 retval = -EIO;
Xiubo Li543af582018-07-06 22:05:38 -0400576 mutex_unlock(&idev->info_lock);
Hamish Martina93e7b32018-05-14 13:32:23 +1200577
578 if (retval)
579 return retval;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100580
581 if (count != sizeof(s32))
582 return -EINVAL;
583
584 add_wait_queue(&idev->wait, &wait);
585
586 do {
587 set_current_state(TASK_INTERRUPTIBLE);
588
589 event_count = atomic_read(&idev->event);
590 if (event_count != listener->event_count) {
Michal Hockoedce5e62015-09-07 09:21:24 +0200591 __set_current_state(TASK_RUNNING);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100592 if (copy_to_user(buf, &event_count, count))
593 retval = -EFAULT;
594 else {
595 listener->event_count = event_count;
596 retval = count;
597 }
598 break;
599 }
600
601 if (filep->f_flags & O_NONBLOCK) {
602 retval = -EAGAIN;
603 break;
604 }
605
606 if (signal_pending(current)) {
607 retval = -ERESTARTSYS;
608 break;
609 }
610 schedule();
611 } while (1);
612
613 __set_current_state(TASK_RUNNING);
614 remove_wait_queue(&idev->wait, &wait);
615
616 return retval;
617}
618
Hans J. Koch328a14e2008-05-23 13:50:14 +0200619static ssize_t uio_write(struct file *filep, const char __user *buf,
620 size_t count, loff_t *ppos)
621{
622 struct uio_listener *listener = filep->private_data;
623 struct uio_device *idev = listener->dev;
624 ssize_t retval;
625 s32 irq_on;
626
Xiubo Li543af582018-07-06 22:05:38 -0400627 mutex_lock(&idev->info_lock);
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400628 if (!idev->info) {
629 retval = -EINVAL;
630 goto out;
631 }
632
Hamish Martina93e7b32018-05-14 13:32:23 +1200633 if (!idev->info || !idev->info->irq) {
Hamish Martin81daa402018-05-14 13:32:22 +1200634 retval = -EIO;
635 goto out;
636 }
Hans J. Koch328a14e2008-05-23 13:50:14 +0200637
Hamish Martin81daa402018-05-14 13:32:22 +1200638 if (count != sizeof(s32)) {
639 retval = -EINVAL;
640 goto out;
641 }
Hans J. Koch328a14e2008-05-23 13:50:14 +0200642
Hamish Martin81daa402018-05-14 13:32:22 +1200643 if (!idev->info->irqcontrol) {
644 retval = -ENOSYS;
645 goto out;
646 }
Hans J. Koch328a14e2008-05-23 13:50:14 +0200647
Hamish Martin81daa402018-05-14 13:32:22 +1200648 if (copy_from_user(&irq_on, buf, count)) {
649 retval = -EFAULT;
650 goto out;
651 }
Hans J. Koch328a14e2008-05-23 13:50:14 +0200652
653 retval = idev->info->irqcontrol(idev->info, irq_on);
654
Hamish Martin81daa402018-05-14 13:32:22 +1200655out:
Xiubo Li543af582018-07-06 22:05:38 -0400656 mutex_unlock(&idev->info_lock);
Hans J. Koch328a14e2008-05-23 13:50:14 +0200657 return retval ? retval : sizeof(s32);
658}
659
Hans J. Kochbeafc542006-12-07 10:58:29 +0100660static int uio_find_mem_index(struct vm_area_struct *vma)
661{
Hans J. Kochbeafc542006-12-07 10:58:29 +0100662 struct uio_device *idev = vma->vm_private_data;
663
Hillf Dantonf0c554f2011-03-28 23:33:26 +0200664 if (vma->vm_pgoff < MAX_UIO_MAPS) {
665 if (idev->info->mem[vma->vm_pgoff].size == 0)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100666 return -1;
Hillf Dantonf0c554f2011-03-28 23:33:26 +0200667 return (int)vma->vm_pgoff;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100668 }
669 return -1;
670}
671
Souptick Joarder9b85e952018-04-17 00:03:55 +0530672static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100673{
Dave Jiang11bac802017-02-24 14:56:41 -0800674 struct uio_device *idev = vmf->vma->vm_private_data;
Nick Piggina18b6302008-02-06 01:37:35 -0800675 struct page *page;
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200676 unsigned long offset;
Ben Hutchingse6418fc2013-10-27 21:53:40 +0000677 void *addr;
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400678 int ret = 0;
679 int mi;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100680
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400681 mutex_lock(&idev->info_lock);
682 if (!idev->info) {
683 ret = VM_FAULT_SIGBUS;
684 goto out;
685 }
686
687 mi = uio_find_mem_index(vmf->vma);
688 if (mi < 0) {
689 ret = VM_FAULT_SIGBUS;
690 goto out;
691 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100692
Andrew G. Harvey02683ff2008-09-24 01:10:02 +0200693 /*
694 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
695 * to use mem[N].
696 */
697 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
698
Ben Hutchingse6418fc2013-10-27 21:53:40 +0000699 addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100700 if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
Ben Hutchingse6418fc2013-10-27 21:53:40 +0000701 page = virt_to_page(addr);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100702 else
Ben Hutchingse6418fc2013-10-27 21:53:40 +0000703 page = vmalloc_to_page(addr);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100704 get_page(page);
Nick Piggina18b6302008-02-06 01:37:35 -0800705 vmf->page = page;
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400706
707out:
708 mutex_unlock(&idev->info_lock);
709
710 return ret;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100711}
712
Uwe Kleine-König72941512013-08-07 13:02:53 +0200713static const struct vm_operations_struct uio_logical_vm_ops = {
Nick Piggina18b6302008-02-06 01:37:35 -0800714 .fault = uio_vma_fault,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100715};
716
Uwe Kleine-König72941512013-08-07 13:02:53 +0200717static int uio_mmap_logical(struct vm_area_struct *vma)
718{
719 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
720 vma->vm_ops = &uio_logical_vm_ops;
Uwe Kleine-König72941512013-08-07 13:02:53 +0200721 return 0;
722}
723
724static const struct vm_operations_struct uio_physical_vm_ops = {
725#ifdef CONFIG_HAVE_IOREMAP_PROT
726 .access = generic_access_phys,
727#endif
728};
729
Hans J. Kochbeafc542006-12-07 10:58:29 +0100730static int uio_mmap_physical(struct vm_area_struct *vma)
731{
732 struct uio_device *idev = vma->vm_private_data;
733 int mi = uio_find_mem_index(vma);
Linus Torvalds7314e612013-10-29 10:21:34 -0700734 struct uio_mem *mem;
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400735
Hans J. Kochbeafc542006-12-07 10:58:29 +0100736 if (mi < 0)
737 return -EINVAL;
Linus Torvalds7314e612013-10-29 10:21:34 -0700738 mem = idev->info->mem + mi;
739
Linus Torvaldsb6550282013-12-02 11:50:37 -0800740 if (mem->addr & ~PAGE_MASK)
741 return -ENODEV;
Greg Kroah-Hartmanb29f6802014-06-17 16:07:08 -0700742 if (vma->vm_end - vma->vm_start > mem->size)
Linus Torvalds7314e612013-10-29 10:21:34 -0700743 return -EINVAL;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100744
Uwe Kleine-König72941512013-08-07 13:02:53 +0200745 vma->vm_ops = &uio_physical_vm_ops;
Jean-Samuel Chenardc9698d6b2008-03-14 11:28:36 +0100746 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
747
Linus Torvalds7314e612013-10-29 10:21:34 -0700748 /*
749 * We cannot use the vm_iomap_memory() helper here,
750 * because vma->vm_pgoff is the map index we looked
751 * up above in uio_find_mem_index(), rather than an
752 * actual page offset into the mmap.
753 *
754 * So we just do the physical mmap without a page
755 * offset.
756 */
Hans J. Kochbeafc542006-12-07 10:58:29 +0100757 return remap_pfn_range(vma,
758 vma->vm_start,
Linus Torvalds7314e612013-10-29 10:21:34 -0700759 mem->addr >> PAGE_SHIFT,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100760 vma->vm_end - vma->vm_start,
761 vma->vm_page_prot);
762}
763
Hans J. Kochbeafc542006-12-07 10:58:29 +0100764static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
765{
766 struct uio_listener *listener = filep->private_data;
767 struct uio_device *idev = listener->dev;
768 int mi;
769 unsigned long requested_pages, actual_pages;
770 int ret = 0;
771
772 if (vma->vm_end < vma->vm_start)
773 return -EINVAL;
774
775 vma->vm_private_data = idev;
776
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400777 mutex_lock(&idev->info_lock);
778 if (!idev->info) {
779 ret = -EINVAL;
780 goto out;
781 }
782
Hans J. Kochbeafc542006-12-07 10:58:29 +0100783 mi = uio_find_mem_index(vma);
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400784 if (mi < 0) {
785 ret = -EINVAL;
786 goto out;
787 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100788
Libin52c2dad2013-07-03 15:01:27 -0700789 requested_pages = vma_pages(vma);
Ian Abbott6da2d372009-02-24 17:22:59 +0000790 actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
791 + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400792 if (requested_pages > actual_pages) {
793 ret = -EINVAL;
794 goto out;
795 }
Hans J. Kochbeafc542006-12-07 10:58:29 +0100796
797 if (idev->info->mmap) {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100798 ret = idev->info->mmap(idev->info, vma);
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400799 goto out;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100800 }
801
802 switch (idev->info->mem[mi].memtype) {
803 case UIO_MEM_PHYS:
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400804 ret = uio_mmap_physical(vma);
805 break;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100806 case UIO_MEM_LOGICAL:
807 case UIO_MEM_VIRTUAL:
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400808 ret = uio_mmap_logical(vma);
809 break;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100810 default:
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400811 ret = -EINVAL;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100812 }
Xiubo Li57c5f4d2018-07-06 22:05:39 -0400813
814out:
815 mutex_unlock(&idev->info_lock);
816 return 0;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100817}
818
Jan Engelhardt4f014692008-01-22 20:50:54 +0100819static const struct file_operations uio_fops = {
Hans J. Kochbeafc542006-12-07 10:58:29 +0100820 .owner = THIS_MODULE,
821 .open = uio_open,
822 .release = uio_release,
823 .read = uio_read,
Hans J. Koch328a14e2008-05-23 13:50:14 +0200824 .write = uio_write,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100825 .mmap = uio_mmap,
826 .poll = uio_poll,
827 .fasync = uio_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200828 .llseek = noop_llseek,
Hans J. Kochbeafc542006-12-07 10:58:29 +0100829};
830
831static int uio_major_init(void)
832{
Eric W. Biederman91960a42010-09-14 11:38:06 -0700833 static const char name[] = "uio";
834 struct cdev *cdev = NULL;
835 dev_t uio_dev = 0;
836 int result;
837
838 result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
839 if (result)
840 goto out;
841
842 result = -ENOMEM;
843 cdev = cdev_alloc();
844 if (!cdev)
845 goto out_unregister;
846
847 cdev->owner = THIS_MODULE;
848 cdev->ops = &uio_fops;
849 kobject_set_name(&cdev->kobj, "%s", name);
850
851 result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
852 if (result)
853 goto out_put;
854
855 uio_major = MAJOR(uio_dev);
856 uio_cdev = cdev;
Wanlong Gaod80df1c2011-08-20 12:12:08 +0800857 return 0;
Eric W. Biederman91960a42010-09-14 11:38:06 -0700858out_put:
859 kobject_put(&cdev->kobj);
860out_unregister:
861 unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
Wanlong Gaod80df1c2011-08-20 12:12:08 +0800862out:
863 return result;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100864}
865
866static void uio_major_cleanup(void)
867{
Eric W. Biederman91960a42010-09-14 11:38:06 -0700868 unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
869 cdev_del(uio_cdev);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100870}
871
872static int init_uio_class(void)
873{
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700874 int ret;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100875
876 /* This is the first time in here, set everything up properly */
877 ret = uio_major_init();
878 if (ret)
879 goto exit;
880
Eric W. Biedermanc66fdab2010-09-14 11:38:36 -0700881 ret = class_register(&uio_class);
882 if (ret) {
883 printk(KERN_ERR "class_register failed for uio\n");
884 goto err_class_register;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100885 }
886 return 0;
887
Eric W. Biedermanc66fdab2010-09-14 11:38:36 -0700888err_class_register:
Hans J. Kochbeafc542006-12-07 10:58:29 +0100889 uio_major_cleanup();
890exit:
891 return ret;
892}
893
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -0700894static void release_uio_class(void)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100895{
Eric W. Biedermanc66fdab2010-09-14 11:38:36 -0700896 class_unregister(&uio_class);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100897 uio_major_cleanup();
Hans J. Kochbeafc542006-12-07 10:58:29 +0100898}
899
Hamish Martina93e7b32018-05-14 13:32:23 +1200900static void uio_device_release(struct device *dev)
901{
902 struct uio_device *idev = dev_get_drvdata(dev);
903
904 kfree(idev);
905}
906
Hans J. Kochbeafc542006-12-07 10:58:29 +0100907/**
908 * uio_register_device - register a new userspace IO device
909 * @owner: module that creates the new device
910 * @parent: parent device
911 * @info: UIO device capabilities
912 *
913 * returns zero on success or a negative error code.
914 */
915int __uio_register_device(struct module *owner,
916 struct device *parent,
917 struct uio_info *info)
918{
919 struct uio_device *idev;
920 int ret = 0;
921
922 if (!parent || !info || !info->name || !info->version)
923 return -EINVAL;
924
925 info->uio_dev = NULL;
926
Hamish Martina93e7b32018-05-14 13:32:23 +1200927 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100928 if (!idev) {
Michal Simeke6789cd2013-09-12 07:39:59 +0200929 return -ENOMEM;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100930 }
931
932 idev->owner = owner;
933 idev->info = info;
Xiubo Li543af582018-07-06 22:05:38 -0400934 mutex_init(&idev->info_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100935 init_waitqueue_head(&idev->wait);
936 atomic_set(&idev->event, 0);
937
938 ret = uio_get_minor(idev);
939 if (ret)
Michal Simeke6789cd2013-09-12 07:39:59 +0200940 return ret;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100941
Hamish Martina93e7b32018-05-14 13:32:23 +1200942 idev->dev.devt = MKDEV(uio_major, idev->minor);
943 idev->dev.class = &uio_class;
944 idev->dev.parent = parent;
945 idev->dev.release = uio_device_release;
946 dev_set_drvdata(&idev->dev, idev);
947
948 ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
949 if (ret)
Hans J. Kochbeafc542006-12-07 10:58:29 +0100950 goto err_device_create;
Hamish Martina93e7b32018-05-14 13:32:23 +1200951
952 ret = device_register(&idev->dev);
953 if (ret)
954 goto err_device_create;
Hans J. Kochbeafc542006-12-07 10:58:29 +0100955
956 ret = uio_dev_add_attributes(idev);
957 if (ret)
958 goto err_uio_dev_add_attributes;
959
960 info->uio_dev = idev;
961
Eric W. Biederman6427a762010-09-14 11:37:36 -0700962 if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
Brian Russella0871462015-03-19 17:55:26 +0000963 /*
964 * Note that we deliberately don't use devm_request_irq
965 * here. The parent module can unregister the UIO device
966 * and call pci_disable_msi, which requires that this
967 * irq has been freed. However, the device may have open
968 * FDs at the time of unregister and therefore may not be
969 * freed until they are released.
970 */
Xiubo Li9421e452018-07-06 22:05:37 -0400971 ret = request_threaded_irq(info->irq, NULL, uio_interrupt,
972 info->irq_flags, info->name, idev);
973
Hans J. Kochbeafc542006-12-07 10:58:29 +0100974 if (ret)
975 goto err_request_irq;
976 }
977
978 return 0;
979
980err_request_irq:
981 uio_dev_del_attributes(idev);
982err_uio_dev_add_attributes:
Hamish Martina93e7b32018-05-14 13:32:23 +1200983 device_unregister(&idev->dev);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100984err_device_create:
985 uio_free_minor(idev);
Hans J. Kochbeafc542006-12-07 10:58:29 +0100986 return ret;
987}
988EXPORT_SYMBOL_GPL(__uio_register_device);
989
990/**
991 * uio_unregister_device - unregister a industrial IO device
992 * @info: UIO device capabilities
993 *
994 */
995void uio_unregister_device(struct uio_info *info)
996{
997 struct uio_device *idev;
998
999 if (!info || !info->uio_dev)
1000 return;
1001
1002 idev = info->uio_dev;
1003
1004 uio_free_minor(idev);
1005
Xiubo Li57c5f4d2018-07-06 22:05:39 -04001006 mutex_lock(&idev->info_lock);
Hans J. Kochbeafc542006-12-07 10:58:29 +01001007 uio_dev_del_attributes(idev);
1008
Stephen Hemminger9ff2c132015-05-14 09:19:13 -07001009 if (info->irq && info->irq != UIO_IRQ_CUSTOM)
1010 free_irq(info->irq, idev);
Brian Russella0871462015-03-19 17:55:26 +00001011
Hamish Martina93e7b32018-05-14 13:32:23 +12001012 idev->info = NULL;
Xiubo Li543af582018-07-06 22:05:38 -04001013 mutex_unlock(&idev->info_lock);
Hamish Martina93e7b32018-05-14 13:32:23 +12001014
1015 device_unregister(&idev->dev);
Hans J. Kochbeafc542006-12-07 10:58:29 +01001016
1017 return;
1018}
1019EXPORT_SYMBOL_GPL(uio_unregister_device);
1020
1021static int __init uio_init(void)
1022{
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -07001023 return init_uio_class();
Hans J. Kochbeafc542006-12-07 10:58:29 +01001024}
1025
1026static void __exit uio_exit(void)
1027{
Eric W. Biederman3d4f9d72010-09-14 11:36:27 -07001028 release_uio_class();
Johannes Thumshirne2ef9392015-07-08 17:24:46 +02001029 idr_destroy(&uio_idr);
Hans J. Kochbeafc542006-12-07 10:58:29 +01001030}
1031
1032module_init(uio_init)
1033module_exit(uio_exit)
1034MODULE_LICENSE("GPL v2");