uio.c 21.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6
/*
 * drivers/uio/uio.c
 *
 * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7
 * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de>
8 9 10 11 12 13 14 15 16 17 18
 * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
 *
 * Userspace IO
 *
 * Base Functions
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/device.h>
19
#include <linux/slab.h>
20 21
#include <linux/mm.h>
#include <linux/idr.h>
22
#include <linux/sched/signal.h>
23 24
#include <linux/string.h>
#include <linux/kobject.h>
25
#include <linux/cdev.h>
26 27
#include <linux/uio_driver.h>

28
#define UIO_MAX_DEVICES		(1U << MINORBITS)
29 30

static int uio_major;
31
static struct cdev *uio_cdev;
32
static DEFINE_IDR(uio_idr);
33
static const struct file_operations uio_fops;
34

Jonathan Corbet's avatar
Jonathan Corbet committed
35 36 37
/* Protect idr accesses */
static DEFINE_MUTEX(minor_lock);

38 39 40 41
/*
 * attributes
 */

Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
42 43 44
struct uio_map {
	struct kobject kobj;
	struct uio_mem *mem;
45
};
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
46
#define to_map(map) container_of(map, struct uio_map, kobj)
47

48 49 50 51 52 53 54 55
static ssize_t map_name_show(struct uio_mem *mem, char *buf)
{
	if (unlikely(!mem->name))
		mem->name = "";

	return sprintf(buf, "%s\n", mem->name);
}

56
static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
57
{
58
	return sprintf(buf, "%pa\n", &mem->addr);
59
}
60

61 62
static ssize_t map_size_show(struct uio_mem *mem, char *buf)
{
63
	return sprintf(buf, "%pa\n", &mem->size);
64 65
}

66 67
static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
{
68
	return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs);
69 70
}

71
struct map_sysfs_entry {
72 73 74 75 76
	struct attribute attr;
	ssize_t (*show)(struct uio_mem *, char *);
	ssize_t (*store)(struct uio_mem *, const char *, size_t);
};

77 78
static struct map_sysfs_entry name_attribute =
	__ATTR(name, S_IRUGO, map_name_show, NULL);
79
static struct map_sysfs_entry addr_attribute =
80
	__ATTR(addr, S_IRUGO, map_addr_show, NULL);
81
static struct map_sysfs_entry size_attribute =
82
	__ATTR(size, S_IRUGO, map_size_show, NULL);
83
static struct map_sysfs_entry offset_attribute =
84
	__ATTR(offset, S_IRUGO, map_offset_show, NULL);
85

Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
86
static struct attribute *attrs[] = {
87
	&name_attribute.attr,
88
	&addr_attribute.attr,
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
89
	&size_attribute.attr,
90
	&offset_attribute.attr,
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
91
	NULL,	/* need to NULL terminate the list of attributes */
92 93
};

Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
94 95 96 97 98 99
static void map_release(struct kobject *kobj)
{
	struct uio_map *map = to_map(kobj);
	kfree(map);
}

100 101 102 103 104
static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
			     char *buf)
{
	struct uio_map *map = to_map(kobj);
	struct uio_mem *mem = map->mem;
105
	struct map_sysfs_entry *entry;
106

107
	entry = container_of(attr, struct map_sysfs_entry, attr);
108 109 110 111 112 113 114

	if (!entry->show)
		return -EIO;

	return entry->show(mem, buf);
}

115
static const struct sysfs_ops map_sysfs_ops = {
116 117 118
	.show = map_type_show,
};

119
static struct kobj_type map_attr_type = {
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
120
	.release	= map_release,
121
	.sysfs_ops	= &map_sysfs_ops,
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
122
	.default_attrs	= attrs,
123 124
};

125 126 127 128 129 130
struct uio_portio {
	struct kobject kobj;
	struct uio_port *port;
};
#define to_portio(portio) container_of(portio, struct uio_portio, kobj)

131 132 133 134 135 136 137 138
static ssize_t portio_name_show(struct uio_port *port, char *buf)
{
	if (unlikely(!port->name))
		port->name = "";

	return sprintf(buf, "%s\n", port->name);
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
static ssize_t portio_start_show(struct uio_port *port, char *buf)
{
	return sprintf(buf, "0x%lx\n", port->start);
}

static ssize_t portio_size_show(struct uio_port *port, char *buf)
{
	return sprintf(buf, "0x%lx\n", port->size);
}

static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
{
	const char *porttypes[] = {"none", "x86", "gpio", "other"};

	if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
		return -EINVAL;

	return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
}

struct portio_sysfs_entry {
	struct attribute attr;
	ssize_t (*show)(struct uio_port *, char *);
	ssize_t (*store)(struct uio_port *, const char *, size_t);
};

165 166
static struct portio_sysfs_entry portio_name_attribute =
	__ATTR(name, S_IRUGO, portio_name_show, NULL);
167 168 169 170 171 172 173 174
static struct portio_sysfs_entry portio_start_attribute =
	__ATTR(start, S_IRUGO, portio_start_show, NULL);
static struct portio_sysfs_entry portio_size_attribute =
	__ATTR(size, S_IRUGO, portio_size_show, NULL);
static struct portio_sysfs_entry portio_porttype_attribute =
	__ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);

static struct attribute *portio_attrs[] = {
175
	&portio_name_attribute.attr,
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	&portio_start_attribute.attr,
	&portio_size_attribute.attr,
	&portio_porttype_attribute.attr,
	NULL,
};

static void portio_release(struct kobject *kobj)
{
	struct uio_portio *portio = to_portio(kobj);
	kfree(portio);
}

static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
			     char *buf)
{
	struct uio_portio *portio = to_portio(kobj);
	struct uio_port *port = portio->port;
	struct portio_sysfs_entry *entry;

	entry = container_of(attr, struct portio_sysfs_entry, attr);

	if (!entry->show)
		return -EIO;

	return entry->show(port, buf);
}

203
static const struct sysfs_ops portio_sysfs_ops = {
204 205 206 207 208 209 210 211 212
	.show = portio_type_show,
};

static struct kobj_type portio_attr_type = {
	.release	= portio_release,
	.sysfs_ops	= &portio_sysfs_ops,
	.default_attrs	= portio_attrs,
};

213
static ssize_t name_show(struct device *dev,
214 215 216
			 struct device_attribute *attr, char *buf)
{
	struct uio_device *idev = dev_get_drvdata(dev);
217 218 219 220 221 222 223 224 225 226 227 228 229 230
	int ret;

	mutex_lock(&idev->info_lock);
	if (!idev->info) {
		ret = -EINVAL;
		dev_err(dev, "the device has been unregistered\n");
		goto out;
	}

	ret = sprintf(buf, "%s\n", idev->info->name);

out:
	mutex_unlock(&idev->info_lock);
	return ret;
231
}
232
static DEVICE_ATTR_RO(name);
233

234
static ssize_t version_show(struct device *dev,
235 236 237
			    struct device_attribute *attr, char *buf)
{
	struct uio_device *idev = dev_get_drvdata(dev);
238 239 240 241 242 243 244 245 246 247 248 249 250 251
	int ret;

	mutex_lock(&idev->info_lock);
	if (!idev->info) {
		ret = -EINVAL;
		dev_err(dev, "the device has been unregistered\n");
		goto out;
	}

	ret = sprintf(buf, "%s\n", idev->info->version);

out:
	mutex_unlock(&idev->info_lock);
	return ret;
252
}
253
static DEVICE_ATTR_RO(version);
254

255
static ssize_t event_show(struct device *dev,
256 257 258
			  struct device_attribute *attr, char *buf)
{
	struct uio_device *idev = dev_get_drvdata(dev);
259
	return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
260
}
261
static DEVICE_ATTR_RO(event);
262

263 264 265 266 267
static struct attribute *uio_attrs[] = {
	&dev_attr_name.attr,
	&dev_attr_version.attr,
	&dev_attr_event.attr,
	NULL,
268
};
269
ATTRIBUTE_GROUPS(uio);
270

271 272 273
/* UIO class infrastructure */
static struct class uio_class = {
	.name = "uio",
274
	.dev_groups = uio_groups,
275 276
};

277 278
bool uio_class_registered;

279 280 281 282 283 284
/*
 * device functions
 */
static int uio_dev_add_attributes(struct uio_device *idev)
{
	int ret;
285
	int mi, pi;
286
	int map_found = 0;
287
	int portio_found = 0;
288
	struct uio_mem *mem;
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
289
	struct uio_map *map;
290 291
	struct uio_port *port;
	struct uio_portio *portio;
292 293 294 295 296 297 298

	for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
		mem = &idev->info->mem[mi];
		if (mem->size == 0)
			break;
		if (!map_found) {
			map_found = 1;
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
299
			idev->map_dir = kobject_create_and_add("maps",
300
							&idev->dev.kobj);
301 302
			if (!idev->map_dir) {
				ret = -ENOMEM;
303
				goto err_map;
304
			}
305
		}
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
306
		map = kzalloc(sizeof(*map), GFP_KERNEL);
307 308
		if (!map) {
			ret = -ENOMEM;
309
			goto err_map;
310
		}
311
		kobject_init(&map->kobj, &map_attr_type);
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
312 313
		map->mem = mem;
		mem->map = map;
314
		ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
315
		if (ret)
Cong Ding's avatar
Cong Ding committed
316
			goto err_map_kobj;
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
317
		ret = kobject_uevent(&map->kobj, KOBJ_ADD);
318
		if (ret)
319
			goto err_map_kobj;
320 321 322 323 324 325 326 327 328
	}

	for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
		port = &idev->info->port[pi];
		if (port->size == 0)
			break;
		if (!portio_found) {
			portio_found = 1;
			idev->portio_dir = kobject_create_and_add("portio",
329
							&idev->dev.kobj);
330 331
			if (!idev->portio_dir) {
				ret = -ENOMEM;
332
				goto err_portio;
333
			}
334 335
		}
		portio = kzalloc(sizeof(*portio), GFP_KERNEL);
336 337
		if (!portio) {
			ret = -ENOMEM;
338
			goto err_portio;
339
		}
340 341 342 343 344 345
		kobject_init(&portio->kobj, &portio_attr_type);
		portio->port = port;
		port->portio = portio;
		ret = kobject_add(&portio->kobj, idev->portio_dir,
							"port%d", pi);
		if (ret)
Cong Ding's avatar
Cong Ding committed
346
			goto err_portio_kobj;
347 348
		ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
		if (ret)
349
			goto err_portio_kobj;
350 351 352 353
	}

	return 0;

354
err_portio:
Cong Ding's avatar
Cong Ding committed
355 356 357
	pi--;
err_portio_kobj:
	for (; pi >= 0; pi--) {
358 359 360 361 362 363
		port = &idev->info->port[pi];
		portio = port->portio;
		kobject_put(&portio->kobj);
	}
	kobject_put(idev->portio_dir);
err_map:
Cong Ding's avatar
Cong Ding committed
364 365 366
	mi--;
err_map_kobj:
	for (; mi >= 0; mi--) {
367
		mem = &idev->info->mem[mi];
Greg Kroah-Hartman's avatar
Greg Kroah-Hartman committed
368
		map = mem->map;
369
		kobject_put(&map->kobj);
370
	}
371
	kobject_put(idev->map_dir);
372
	dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret);
373 374 375 376 377
	return ret;
}

static void uio_dev_del_attributes(struct uio_device *idev)
{
378
	int i;
379
	struct uio_mem *mem;
380 381 382 383
	struct uio_port *port;

	for (i = 0; i < MAX_UIO_MAPS; i++) {
		mem = &idev->info->mem[i];
384 385
		if (mem->size == 0)
			break;
386
		kobject_put(&mem->map->kobj);
387
	}
388
	kobject_put(idev->map_dir);
389 390 391 392 393 394 395 396

	for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
		port = &idev->info->port[i];
		if (port->size == 0)
			break;
		kobject_put(&port->portio->kobj);
	}
	kobject_put(idev->portio_dir);
397 398 399 400 401 402 403
}

static int uio_get_minor(struct uio_device *idev)
{
	int retval = -ENOMEM;

	mutex_lock(&minor_lock);
Tejun Heo's avatar
Tejun Heo committed
404 405 406
	retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL);
	if (retval >= 0) {
		idev->minor = retval;
407
		retval = 0;
Tejun Heo's avatar
Tejun Heo committed
408
	} else if (retval == -ENOSPC) {
409
		dev_err(&idev->dev, "too many uio devices\n");
410 411
		retval = -EINVAL;
	}
412 413 414 415 416 417
	mutex_unlock(&minor_lock);
	return retval;
}

static void uio_free_minor(struct uio_device *idev)
{
Jonathan Corbet's avatar
Jonathan Corbet committed
418
	mutex_lock(&minor_lock);
419
	idr_remove(&uio_idr, idev->minor);
Jonathan Corbet's avatar
Jonathan Corbet committed
420
	mutex_unlock(&minor_lock);
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
}

/**
 * uio_event_notify - trigger an interrupt event
 * @info: UIO device capabilities
 */
void uio_event_notify(struct uio_info *info)
{
	struct uio_device *idev = info->uio_dev;

	atomic_inc(&idev->event);
	wake_up_interruptible(&idev->wait);
	kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
}
EXPORT_SYMBOL_GPL(uio_event_notify);

/**
 * uio_interrupt - hardware interrupt handler
 * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
 * @dev_id: Pointer to the devices uio_device structure
 */
static irqreturn_t uio_interrupt(int irq, void *dev_id)
{
	struct uio_device *idev = (struct uio_device *)dev_id;
445 446 447
	irqreturn_t ret;

	ret = idev->info->handler(irq, idev->info);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	if (ret == IRQ_HANDLED)
		uio_event_notify(idev->info);

	return ret;
}

struct uio_listener {
	struct uio_device *dev;
	s32 event_count;
};

static int uio_open(struct inode *inode, struct file *filep)
{
	struct uio_device *idev;
	struct uio_listener *listener;
	int ret = 0;

Jonathan Corbet's avatar
Jonathan Corbet committed
465
	mutex_lock(&minor_lock);
466
	idev = idr_find(&uio_idr, iminor(inode));
Jonathan Corbet's avatar
Jonathan Corbet committed
467
	mutex_unlock(&minor_lock);
468 469 470 471
	if (!idev) {
		ret = -ENODEV;
		goto out;
	}
472

473 474
	get_device(&idev->dev);

475 476
	if (!try_module_get(idev->owner)) {
		ret = -ENODEV;
477
		goto err_module_get;
478
	}
479

480
	listener = kmalloc(sizeof(*listener), GFP_KERNEL);
481 482 483 484
	if (!listener) {
		ret = -ENOMEM;
		goto err_alloc_listener;
	}
485 486 487 488 489

	listener->dev = idev;
	listener->event_count = atomic_read(&idev->event);
	filep->private_data = listener;

490
	mutex_lock(&idev->info_lock);
491 492 493 494 495 496
	if (!idev->info) {
		mutex_unlock(&idev->info_lock);
		ret = -EINVAL;
		goto err_alloc_listener;
	}

497
	if (idev->info && idev->info->open)
498
		ret = idev->info->open(idev->info, inode);
499
	mutex_unlock(&idev->info_lock);
500 501 502
	if (ret)
		goto err_infoopen;

503 504 505 506 507
	return 0;

err_infoopen:
	kfree(listener);

Jonathan Corbet's avatar
Jonathan Corbet committed
508
err_alloc_listener:
509
	module_put(idev->owner);
510

511 512 513
err_module_get:
	put_device(&idev->dev);

514
out:
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	return ret;
}

static int uio_fasync(int fd, struct file *filep, int on)
{
	struct uio_listener *listener = filep->private_data;
	struct uio_device *idev = listener->dev;

	return fasync_helper(fd, filep, on, &idev->async_queue);
}

static int uio_release(struct inode *inode, struct file *filep)
{
	int ret = 0;
	struct uio_listener *listener = filep->private_data;
	struct uio_device *idev = listener->dev;

532
	mutex_lock(&idev->info_lock);
533
	if (idev->info && idev->info->release)
534
		ret = idev->info->release(idev->info, inode);
535
	mutex_unlock(&idev->info_lock);
536 537

	module_put(idev->owner);
538
	kfree(listener);
539
	put_device(&idev->dev);
540 541 542
	return ret;
}

543
static __poll_t uio_poll(struct file *filep, poll_table *wait)
544 545 546
{
	struct uio_listener *listener = filep->private_data;
	struct uio_device *idev = listener->dev;
547
	__poll_t ret = 0;
548

549
	mutex_lock(&idev->info_lock);
550 551
	if (!idev->info || !idev->info->irq)
		ret = -EIO;
552
	mutex_unlock(&idev->info_lock);
553 554 555

	if (ret)
		return ret;
556 557 558

	poll_wait(filep, &idev->wait, wait);
	if (listener->event_count != atomic_read(&idev->event))
559
		return EPOLLIN | EPOLLRDNORM;
560 561 562 563 564 565 566 567 568
	return 0;
}

static ssize_t uio_read(struct file *filep, char __user *buf,
			size_t count, loff_t *ppos)
{
	struct uio_listener *listener = filep->private_data;
	struct uio_device *idev = listener->dev;
	DECLARE_WAITQUEUE(wait, current);
569
	ssize_t retval = 0;
570 571
	s32 event_count;

572
	mutex_lock(&idev->info_lock);
573 574
	if (!idev->info || !idev->info->irq)
		retval = -EIO;
575
	mutex_unlock(&idev->info_lock);
576 577 578

	if (retval)
		return retval;
579 580 581 582 583 584 585 586 587 588 589

	if (count != sizeof(s32))
		return -EINVAL;

	add_wait_queue(&idev->wait, &wait);

	do {
		set_current_state(TASK_INTERRUPTIBLE);

		event_count = atomic_read(&idev->event);
		if (event_count != listener->event_count) {
590
			__set_current_state(TASK_RUNNING);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
			if (copy_to_user(buf, &event_count, count))
				retval = -EFAULT;
			else {
				listener->event_count = event_count;
				retval = count;
			}
			break;
		}

		if (filep->f_flags & O_NONBLOCK) {
			retval = -EAGAIN;
			break;
		}

		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
		schedule();
	} while (1);

	__set_current_state(TASK_RUNNING);
	remove_wait_queue(&idev->wait, &wait);

	return retval;
}

618 619 620 621 622 623 624 625
static ssize_t uio_write(struct file *filep, const char __user *buf,
			size_t count, loff_t *ppos)
{
	struct uio_listener *listener = filep->private_data;
	struct uio_device *idev = listener->dev;
	ssize_t retval;
	s32 irq_on;

626 627 628 629 630 631
	if (count != sizeof(s32))
		return -EINVAL;

	if (copy_from_user(&irq_on, buf, count))
		return -EFAULT;

632
	mutex_lock(&idev->info_lock);
633 634 635 636 637
	if (!idev->info) {
		retval = -EINVAL;
		goto out;
	}

638
	if (!idev->info || !idev->info->irq) {
639 640 641
		retval = -EIO;
		goto out;
	}
642

643 644 645 646
	if (!idev->info->irqcontrol) {
		retval = -ENOSYS;
		goto out;
	}
647 648 649

	retval = idev->info->irqcontrol(idev->info, irq_on);

650
out:
651
	mutex_unlock(&idev->info_lock);
652 653 654
	return retval ? retval : sizeof(s32);
}

655 656 657 658
static int uio_find_mem_index(struct vm_area_struct *vma)
{
	struct uio_device *idev = vma->vm_private_data;

659 660
	if (vma->vm_pgoff < MAX_UIO_MAPS) {
		if (idev->info->mem[vma->vm_pgoff].size == 0)
661
			return -1;
662
		return (int)vma->vm_pgoff;
663 664 665 666
	}
	return -1;
}

667
static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
668
{
669
	struct uio_device *idev = vmf->vma->vm_private_data;
Nick Piggin's avatar
Nick Piggin committed
670
	struct page *page;
671
	unsigned long offset;
672
	void *addr;
673
	vm_fault_t ret = 0;
674
	int mi;
675

676 677 678 679 680 681 682 683 684 685 686
	mutex_lock(&idev->info_lock);
	if (!idev->info) {
		ret = VM_FAULT_SIGBUS;
		goto out;
	}

	mi = uio_find_mem_index(vmf->vma);
	if (mi < 0) {
		ret = VM_FAULT_SIGBUS;
		goto out;
	}
687

688 689 690 691 692 693
	/*
	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
	 * to use mem[N].
	 */
	offset = (vmf->pgoff - mi) << PAGE_SHIFT;

694
	addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset;
695
	if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
696
		page = virt_to_page(addr);
697
	else
698
		page = vmalloc_to_page(addr);
699
	get_page(page);
Nick Piggin's avatar
Nick Piggin committed
700
	vmf->page = page;
701 702 703 704 705

out:
	mutex_unlock(&idev->info_lock);

	return ret;
706 707
}

708
static const struct vm_operations_struct uio_logical_vm_ops = {
Nick Piggin's avatar
Nick Piggin committed
709
	.fault = uio_vma_fault,
710 711
};

712 713 714 715 716 717 718 719 720 721 722 723 724
static int uio_mmap_logical(struct vm_area_struct *vma)
{
	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
	vma->vm_ops = &uio_logical_vm_ops;
	return 0;
}

static const struct vm_operations_struct uio_physical_vm_ops = {
#ifdef CONFIG_HAVE_IOREMAP_PROT
	.access = generic_access_phys,
#endif
};

725 726 727 728
static int uio_mmap_physical(struct vm_area_struct *vma)
{
	struct uio_device *idev = vma->vm_private_data;
	int mi = uio_find_mem_index(vma);
729
	struct uio_mem *mem;
730

731 732
	if (mi < 0)
		return -EINVAL;
733
	mem = idev->info->mem + mi;
734

735 736
	if (mem->addr & ~PAGE_MASK)
		return -ENODEV;
737
	if (vma->vm_end - vma->vm_start > mem->size)
738
		return -EINVAL;
739

740
	vma->vm_ops = &uio_physical_vm_ops;
741
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
742

743 744 745 746 747 748 749 750 751
	/*
	 * We cannot use the vm_iomap_memory() helper here,
	 * because vma->vm_pgoff is the map index we looked
	 * up above in uio_find_mem_index(), rather than an
	 * actual page offset into the mmap.
	 *
	 * So we just do the physical mmap without a page
	 * offset.
	 */
752 753
	return remap_pfn_range(vma,
			       vma->vm_start,
754
			       mem->addr >> PAGE_SHIFT,
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
			       vma->vm_end - vma->vm_start,
			       vma->vm_page_prot);
}

static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
{
	struct uio_listener *listener = filep->private_data;
	struct uio_device *idev = listener->dev;
	int mi;
	unsigned long requested_pages, actual_pages;
	int ret = 0;

	if (vma->vm_end < vma->vm_start)
		return -EINVAL;

	vma->vm_private_data = idev;

772 773 774 775 776 777
	mutex_lock(&idev->info_lock);
	if (!idev->info) {
		ret = -EINVAL;
		goto out;
	}

778
	mi = uio_find_mem_index(vma);
779 780 781 782
	if (mi < 0) {
		ret = -EINVAL;
		goto out;
	}
783

784
	requested_pages = vma_pages(vma);
785 786
	actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
			+ idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
787 788 789 790
	if (requested_pages > actual_pages) {
		ret = -EINVAL;
		goto out;
	}
791 792 793

	if (idev->info->mmap) {
		ret = idev->info->mmap(idev->info, vma);
794
		goto out;
795 796 797 798
	}

	switch (idev->info->mem[mi].memtype) {
		case UIO_MEM_PHYS:
799 800
			ret = uio_mmap_physical(vma);
			break;
801 802
		case UIO_MEM_LOGICAL:
		case UIO_MEM_VIRTUAL:
803 804
			ret = uio_mmap_logical(vma);
			break;
805
		default:
806
			ret = -EINVAL;
807
	}
808 809 810

out:
	mutex_unlock(&idev->info_lock);
811
	return ret;
812 813
}

814
static const struct file_operations uio_fops = {
815 816 817 818
	.owner		= THIS_MODULE,
	.open		= uio_open,
	.release	= uio_release,
	.read		= uio_read,
819
	.write		= uio_write,
820 821 822
	.mmap		= uio_mmap,
	.poll		= uio_poll,
	.fasync		= uio_fasync,
823
	.llseek		= noop_llseek,
824 825 826 827
};

static int uio_major_init(void)
{
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
	static const char name[] = "uio";
	struct cdev *cdev = NULL;
	dev_t uio_dev = 0;
	int result;

	result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
	if (result)
		goto out;

	result = -ENOMEM;
	cdev = cdev_alloc();
	if (!cdev)
		goto out_unregister;

	cdev->owner = THIS_MODULE;
	cdev->ops = &uio_fops;
	kobject_set_name(&cdev->kobj, "%s", name);

	result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
	if (result)
		goto out_put;

	uio_major = MAJOR(uio_dev);
	uio_cdev = cdev;
852
	return 0;
853 854 855 856
out_put:
	kobject_put(&cdev->kobj);
out_unregister:
	unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
857 858
out:
	return result;
859 860 861 862
}

static void uio_major_cleanup(void)
{
863 864
	unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
	cdev_del(uio_cdev);
865 866 867 868
}

static int init_uio_class(void)
{
869
	int ret;
870 871 872 873 874 875

	/* This is the first time in here, set everything up properly */
	ret = uio_major_init();
	if (ret)
		goto exit;

876 877 878 879
	ret = class_register(&uio_class);
	if (ret) {
		printk(KERN_ERR "class_register failed for uio\n");
		goto err_class_register;
880
	}
881 882 883

	uio_class_registered = true;

884 885
	return 0;

886
err_class_register:
887 888 889 890 891
	uio_major_cleanup();
exit:
	return ret;
}

892
static void release_uio_class(void)
893
{
894
	uio_class_registered = false;
895
	class_unregister(&uio_class);
896 897 898
	uio_major_cleanup();
}

899 900 901 902 903 904 905
static void uio_device_release(struct device *dev)
{
	struct uio_device *idev = dev_get_drvdata(dev);

	kfree(idev);
}

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
/**
 * uio_register_device - register a new userspace IO device
 * @owner:	module that creates the new device
 * @parent:	parent device
 * @info:	UIO device capabilities
 *
 * returns zero on success or a negative error code.
 */
int __uio_register_device(struct module *owner,
			  struct device *parent,
			  struct uio_info *info)
{
	struct uio_device *idev;
	int ret = 0;

921 922 923
	if (!uio_class_registered)
		return -EPROBE_DEFER;

924 925 926 927 928
	if (!parent || !info || !info->name || !info->version)
		return -EINVAL;

	info->uio_dev = NULL;

929
	idev = kzalloc(sizeof(*idev), GFP_KERNEL);
930
	if (!idev) {
931
		return -ENOMEM;
932 933 934 935
	}

	idev->owner = owner;
	idev->info = info;
936
	mutex_init(&idev->info_lock);
937 938 939 940 941
	init_waitqueue_head(&idev->wait);
	atomic_set(&idev->event, 0);

	ret = uio_get_minor(idev);
	if (ret)
942
		return ret;
943

944 945 946 947 948 949 950 951 952 953 954 955
	idev->dev.devt = MKDEV(uio_major, idev->minor);
	idev->dev.class = &uio_class;
	idev->dev.parent = parent;
	idev->dev.release = uio_device_release;
	dev_set_drvdata(&idev->dev, idev);

	ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
	if (ret)
		goto err_device_create;

	ret = device_register(&idev->dev);
	if (ret)
956 957 958 959 960 961
		goto err_device_create;

	ret = uio_dev_add_attributes(idev);
	if (ret)
		goto err_uio_dev_add_attributes;

962
	if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
963 964 965 966 967 968 969 970
		/*
		 * Note that we deliberately don't use devm_request_irq
		 * here. The parent module can unregister the UIO device
		 * and call pci_disable_msi, which requires that this
		 * irq has been freed. However, the device may have open
		 * FDs at the time of unregister and therefore may not be
		 * freed until they are released.
		 */
971 972
		ret = request_irq(info->irq, uio_interrupt,
				  info->irq_flags, info->name, idev);
973 974 975 976
		if (ret)
			goto err_request_irq;
	}

977
	info->uio_dev = idev;
978 979 980 981 982
	return 0;

err_request_irq:
	uio_dev_del_attributes(idev);
err_uio_dev_add_attributes:
983
	device_unregister(&idev->dev);
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
err_device_create:
	uio_free_minor(idev);
	return ret;
}
EXPORT_SYMBOL_GPL(__uio_register_device);

/**
 * uio_unregister_device - unregister a industrial IO device
 * @info:	UIO device capabilities
 *
 */
void uio_unregister_device(struct uio_info *info)
{
	struct uio_device *idev;

	if (!info || !info->uio_dev)
		return;

	idev = info->uio_dev;

	uio_free_minor(idev);

1006
	mutex_lock(&idev->info_lock);
1007 1008
	uio_dev_del_attributes(idev);

1009 1010
	if (info->irq && info->irq != UIO_IRQ_CUSTOM)
		free_irq(info->irq, idev);
1011

1012
	idev->info = NULL;
1013
	mutex_unlock(&idev->info_lock);
1014 1015

	device_unregister(&idev->dev);
1016 1017 1018 1019 1020 1021 1022

	return;
}
EXPORT_SYMBOL_GPL(uio_unregister_device);

static int __init uio_init(void)
{
1023
	return init_uio_class();
1024 1025 1026 1027
}

static void __exit uio_exit(void)
{
1028
	release_uio_class();
1029
	idr_destroy(&uio_idr);
1030 1031 1032 1033 1034
}

module_init(uio_init)
module_exit(uio_exit)
MODULE_LICENSE("GPL v2");